* gmon.h, alpha.c, vax.c: Remove #if 0'd chunks.
[binutils.git] / bfd / elflink.c
blob7a02d3415f04b4a42fb5dfb7500f33e1820cd090
1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
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 some sections which will be filled in with dynamic linking
107 information. ABFD is an input file which requires dynamic sections
108 to be created. The dynamic sections take up virtual memory space
109 when the final executable is run, so we need to create them before
110 addresses are assigned to the output sections. We work out the
111 actual contents and size of these sections later. */
113 bfd_boolean
114 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
116 flagword flags;
117 register asection *s;
118 struct elf_link_hash_entry *h;
119 struct bfd_link_hash_entry *bh;
120 const struct elf_backend_data *bed;
122 if (! is_elf_hash_table (info->hash))
123 return FALSE;
125 if (elf_hash_table (info)->dynamic_sections_created)
126 return TRUE;
128 /* Make sure that all dynamic sections use the same input BFD. */
129 if (elf_hash_table (info)->dynobj == NULL)
130 elf_hash_table (info)->dynobj = abfd;
131 else
132 abfd = elf_hash_table (info)->dynobj;
134 bed = get_elf_backend_data (abfd);
136 flags = bed->dynamic_sec_flags;
138 /* A dynamically linked executable has a .interp section, but a
139 shared library does not. */
140 if (info->executable)
142 s = bfd_make_section (abfd, ".interp");
143 if (s == NULL
144 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
145 return FALSE;
148 if (! info->traditional_format)
150 s = bfd_make_section (abfd, ".eh_frame_hdr");
151 if (s == NULL
152 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
153 || ! bfd_set_section_alignment (abfd, s, 2))
154 return FALSE;
155 elf_hash_table (info)->eh_info.hdr_sec = s;
158 /* Create sections to hold version informations. These are removed
159 if they are not needed. */
160 s = bfd_make_section (abfd, ".gnu.version_d");
161 if (s == NULL
162 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
163 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
164 return FALSE;
166 s = bfd_make_section (abfd, ".gnu.version");
167 if (s == NULL
168 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
169 || ! bfd_set_section_alignment (abfd, s, 1))
170 return FALSE;
172 s = bfd_make_section (abfd, ".gnu.version_r");
173 if (s == NULL
174 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
175 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
176 return FALSE;
178 s = bfd_make_section (abfd, ".dynsym");
179 if (s == NULL
180 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
181 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
182 return FALSE;
184 s = bfd_make_section (abfd, ".dynstr");
185 if (s == NULL
186 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
187 return FALSE;
189 /* Create a strtab to hold the dynamic symbol names. */
190 if (elf_hash_table (info)->dynstr == NULL)
192 elf_hash_table (info)->dynstr = _bfd_elf_strtab_init ();
193 if (elf_hash_table (info)->dynstr == NULL)
194 return FALSE;
197 s = bfd_make_section (abfd, ".dynamic");
198 if (s == NULL
199 || ! bfd_set_section_flags (abfd, s, flags)
200 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
201 return FALSE;
203 /* The special symbol _DYNAMIC is always set to the start of the
204 .dynamic section. This call occurs before we have processed the
205 symbols for any dynamic object, so we don't have to worry about
206 overriding a dynamic definition. We could set _DYNAMIC in a
207 linker script, but we only want to define it if we are, in fact,
208 creating a .dynamic section. We don't want to define it if there
209 is no .dynamic section, since on some ELF platforms the start up
210 code examines it to decide how to initialize the process. */
211 bh = NULL;
212 if (! (_bfd_generic_link_add_one_symbol
213 (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, 0, NULL, FALSE,
214 get_elf_backend_data (abfd)->collect, &bh)))
215 return FALSE;
216 h = (struct elf_link_hash_entry *) bh;
217 h->def_regular = 1;
218 h->type = STT_OBJECT;
220 if (! info->executable
221 && ! bfd_elf_link_record_dynamic_symbol (info, h))
222 return FALSE;
224 s = bfd_make_section (abfd, ".hash");
225 if (s == NULL
226 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
227 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
228 return FALSE;
229 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
231 /* Let the backend create the rest of the sections. This lets the
232 backend set the right flags. The backend will normally create
233 the .got and .plt sections. */
234 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
235 return FALSE;
237 elf_hash_table (info)->dynamic_sections_created = TRUE;
239 return TRUE;
242 /* Create dynamic sections when linking against a dynamic object. */
244 bfd_boolean
245 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
247 flagword flags, pltflags;
248 asection *s;
249 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
251 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
252 .rel[a].bss sections. */
253 flags = bed->dynamic_sec_flags;
255 pltflags = flags;
256 if (bed->plt_not_loaded)
257 /* We do not clear SEC_ALLOC here because we still want the OS to
258 allocate space for the section; it's just that there's nothing
259 to read in from the object file. */
260 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
261 else
262 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
263 if (bed->plt_readonly)
264 pltflags |= SEC_READONLY;
266 s = bfd_make_section (abfd, ".plt");
267 if (s == NULL
268 || ! bfd_set_section_flags (abfd, s, pltflags)
269 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
270 return FALSE;
272 if (bed->want_plt_sym)
274 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
275 .plt section. */
276 struct elf_link_hash_entry *h;
277 struct bfd_link_hash_entry *bh = NULL;
279 if (! (_bfd_generic_link_add_one_symbol
280 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s, 0, NULL,
281 FALSE, get_elf_backend_data (abfd)->collect, &bh)))
282 return FALSE;
283 h = (struct elf_link_hash_entry *) bh;
284 h->def_regular = 1;
285 h->type = STT_OBJECT;
287 if (! info->executable
288 && ! bfd_elf_link_record_dynamic_symbol (info, h))
289 return FALSE;
292 s = bfd_make_section (abfd,
293 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
294 if (s == NULL
295 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
296 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
297 return FALSE;
299 if (! _bfd_elf_create_got_section (abfd, info))
300 return FALSE;
302 if (bed->want_dynbss)
304 /* The .dynbss section is a place to put symbols which are defined
305 by dynamic objects, are referenced by regular objects, and are
306 not functions. We must allocate space for them in the process
307 image and use a R_*_COPY reloc to tell the dynamic linker to
308 initialize them at run time. The linker script puts the .dynbss
309 section into the .bss section of the final image. */
310 s = bfd_make_section (abfd, ".dynbss");
311 if (s == NULL
312 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC | SEC_LINKER_CREATED))
313 return FALSE;
315 /* The .rel[a].bss section holds copy relocs. This section is not
316 normally needed. We need to create it here, though, so that the
317 linker will map it to an output section. We can't just create it
318 only if we need it, because we will not know whether we need it
319 until we have seen all the input files, and the first time the
320 main linker code calls BFD after examining all the input files
321 (size_dynamic_sections) the input sections have already been
322 mapped to the output sections. If the section turns out not to
323 be needed, we can discard it later. We will never need this
324 section when generating a shared object, since they do not use
325 copy relocs. */
326 if (! info->shared)
328 s = bfd_make_section (abfd,
329 (bed->default_use_rela_p
330 ? ".rela.bss" : ".rel.bss"));
331 if (s == NULL
332 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
333 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
334 return FALSE;
338 return TRUE;
341 /* Record a new dynamic symbol. We record the dynamic symbols as we
342 read the input files, since we need to have a list of all of them
343 before we can determine the final sizes of the output sections.
344 Note that we may actually call this function even though we are not
345 going to output any dynamic symbols; in some cases we know that a
346 symbol should be in the dynamic symbol table, but only if there is
347 one. */
349 bfd_boolean
350 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
351 struct elf_link_hash_entry *h)
353 if (h->dynindx == -1)
355 struct elf_strtab_hash *dynstr;
356 char *p;
357 const char *name;
358 bfd_size_type indx;
360 /* XXX: The ABI draft says the linker must turn hidden and
361 internal symbols into STB_LOCAL symbols when producing the
362 DSO. However, if ld.so honors st_other in the dynamic table,
363 this would not be necessary. */
364 switch (ELF_ST_VISIBILITY (h->other))
366 case STV_INTERNAL:
367 case STV_HIDDEN:
368 if (h->root.type != bfd_link_hash_undefined
369 && h->root.type != bfd_link_hash_undefweak)
371 h->forced_local = 1;
372 return TRUE;
375 default:
376 break;
379 h->dynindx = elf_hash_table (info)->dynsymcount;
380 ++elf_hash_table (info)->dynsymcount;
382 dynstr = elf_hash_table (info)->dynstr;
383 if (dynstr == NULL)
385 /* Create a strtab to hold the dynamic symbol names. */
386 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
387 if (dynstr == NULL)
388 return FALSE;
391 /* We don't put any version information in the dynamic string
392 table. */
393 name = h->root.root.string;
394 p = strchr (name, ELF_VER_CHR);
395 if (p != NULL)
396 /* We know that the p points into writable memory. In fact,
397 there are only a few symbols that have read-only names, being
398 those like _GLOBAL_OFFSET_TABLE_ that are created specially
399 by the backends. Most symbols will have names pointing into
400 an ELF string table read from a file, or to objalloc memory. */
401 *p = 0;
403 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
405 if (p != NULL)
406 *p = ELF_VER_CHR;
408 if (indx == (bfd_size_type) -1)
409 return FALSE;
410 h->dynstr_index = indx;
413 return TRUE;
416 /* Record an assignment to a symbol made by a linker script. We need
417 this in case some dynamic object refers to this symbol. */
419 bfd_boolean
420 bfd_elf_record_link_assignment (bfd *output_bfd ATTRIBUTE_UNUSED,
421 struct bfd_link_info *info,
422 const char *name,
423 bfd_boolean provide)
425 struct elf_link_hash_entry *h;
427 if (!is_elf_hash_table (info->hash))
428 return TRUE;
430 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, TRUE, FALSE);
431 if (h == NULL)
432 return FALSE;
434 /* Since we're defining the symbol, don't let it seem to have not
435 been defined. record_dynamic_symbol and size_dynamic_sections
436 may depend on this.
437 ??? Changing bfd_link_hash_undefined to bfd_link_hash_new (or
438 to bfd_link_hash_undefweak, see linker.c:link_action) runs the risk
439 of some later symbol manipulation setting the symbol back to
440 bfd_link_hash_undefined, and the linker trying to add the symbol to
441 the undefs list twice. */
442 if (h->root.type == bfd_link_hash_undefweak
443 || h->root.type == bfd_link_hash_undefined)
444 h->root.type = bfd_link_hash_new;
446 if (h->root.type == bfd_link_hash_new)
447 h->non_elf = 0;
449 /* If this symbol is being provided by the linker script, and it is
450 currently defined by a dynamic object, but not by a regular
451 object, then mark it as undefined so that the generic linker will
452 force the correct value. */
453 if (provide
454 && h->def_dynamic
455 && !h->def_regular)
456 h->root.type = bfd_link_hash_undefined;
458 /* If this symbol is not being provided by the linker script, and it is
459 currently defined by a dynamic object, but not by a regular object,
460 then clear out any version information because the symbol will not be
461 associated with the dynamic object any more. */
462 if (!provide
463 && h->def_dynamic
464 && !h->def_regular)
465 h->verinfo.verdef = NULL;
467 h->def_regular = 1;
469 if ((h->def_dynamic
470 || h->ref_dynamic
471 || info->shared)
472 && h->dynindx == -1)
474 if (! bfd_elf_link_record_dynamic_symbol (info, h))
475 return FALSE;
477 /* If this is a weak defined symbol, and we know a corresponding
478 real symbol from the same dynamic object, make sure the real
479 symbol is also made into a dynamic symbol. */
480 if (h->u.weakdef != NULL
481 && h->u.weakdef->dynindx == -1)
483 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
484 return FALSE;
488 return TRUE;
491 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
492 success, and 2 on a failure caused by attempting to record a symbol
493 in a discarded section, eg. a discarded link-once section symbol. */
496 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
497 bfd *input_bfd,
498 long input_indx)
500 bfd_size_type amt;
501 struct elf_link_local_dynamic_entry *entry;
502 struct elf_link_hash_table *eht;
503 struct elf_strtab_hash *dynstr;
504 unsigned long dynstr_index;
505 char *name;
506 Elf_External_Sym_Shndx eshndx;
507 char esym[sizeof (Elf64_External_Sym)];
509 if (! is_elf_hash_table (info->hash))
510 return 0;
512 /* See if the entry exists already. */
513 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
514 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
515 return 1;
517 amt = sizeof (*entry);
518 entry = bfd_alloc (input_bfd, amt);
519 if (entry == NULL)
520 return 0;
522 /* Go find the symbol, so that we can find it's name. */
523 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
524 1, input_indx, &entry->isym, esym, &eshndx))
526 bfd_release (input_bfd, entry);
527 return 0;
530 if (entry->isym.st_shndx != SHN_UNDEF
531 && (entry->isym.st_shndx < SHN_LORESERVE
532 || entry->isym.st_shndx > SHN_HIRESERVE))
534 asection *s;
536 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
537 if (s == NULL || bfd_is_abs_section (s->output_section))
539 /* We can still bfd_release here as nothing has done another
540 bfd_alloc. We can't do this later in this function. */
541 bfd_release (input_bfd, entry);
542 return 2;
546 name = (bfd_elf_string_from_elf_section
547 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
548 entry->isym.st_name));
550 dynstr = elf_hash_table (info)->dynstr;
551 if (dynstr == NULL)
553 /* Create a strtab to hold the dynamic symbol names. */
554 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
555 if (dynstr == NULL)
556 return 0;
559 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
560 if (dynstr_index == (unsigned long) -1)
561 return 0;
562 entry->isym.st_name = dynstr_index;
564 eht = elf_hash_table (info);
566 entry->next = eht->dynlocal;
567 eht->dynlocal = entry;
568 entry->input_bfd = input_bfd;
569 entry->input_indx = input_indx;
570 eht->dynsymcount++;
572 /* Whatever binding the symbol had before, it's now local. */
573 entry->isym.st_info
574 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
576 /* The dynindx will be set at the end of size_dynamic_sections. */
578 return 1;
581 /* Return the dynindex of a local dynamic symbol. */
583 long
584 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
585 bfd *input_bfd,
586 long input_indx)
588 struct elf_link_local_dynamic_entry *e;
590 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
591 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
592 return e->dynindx;
593 return -1;
596 /* This function is used to renumber the dynamic symbols, if some of
597 them are removed because they are marked as local. This is called
598 via elf_link_hash_traverse. */
600 static bfd_boolean
601 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
602 void *data)
604 size_t *count = data;
606 if (h->root.type == bfd_link_hash_warning)
607 h = (struct elf_link_hash_entry *) h->root.u.i.link;
609 if (h->dynindx != -1)
610 h->dynindx = ++(*count);
612 return TRUE;
615 /* Return true if the dynamic symbol for a given section should be
616 omitted when creating a shared library. */
617 bfd_boolean
618 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
619 struct bfd_link_info *info,
620 asection *p)
622 switch (elf_section_data (p)->this_hdr.sh_type)
624 case SHT_PROGBITS:
625 case SHT_NOBITS:
626 /* If sh_type is yet undecided, assume it could be
627 SHT_PROGBITS/SHT_NOBITS. */
628 case SHT_NULL:
629 if (strcmp (p->name, ".got") == 0
630 || strcmp (p->name, ".got.plt") == 0
631 || strcmp (p->name, ".plt") == 0)
633 asection *ip;
634 bfd *dynobj = elf_hash_table (info)->dynobj;
636 if (dynobj != NULL
637 && (ip = bfd_get_section_by_name (dynobj, p->name)) != NULL
638 && (ip->flags & SEC_LINKER_CREATED)
639 && ip->output_section == p)
640 return TRUE;
642 return FALSE;
644 /* There shouldn't be section relative relocations
645 against any other section. */
646 default:
647 return TRUE;
651 /* Assign dynsym indices. In a shared library we generate a section
652 symbol for each output section, which come first. Next come all of
653 the back-end allocated local dynamic syms, followed by the rest of
654 the global symbols. */
656 unsigned long
657 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
659 unsigned long dynsymcount = 0;
661 if (info->shared)
663 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
664 asection *p;
665 for (p = output_bfd->sections; p ; p = p->next)
666 if ((p->flags & SEC_EXCLUDE) == 0
667 && (p->flags & SEC_ALLOC) != 0
668 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
669 elf_section_data (p)->dynindx = ++dynsymcount;
672 if (elf_hash_table (info)->dynlocal)
674 struct elf_link_local_dynamic_entry *p;
675 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
676 p->dynindx = ++dynsymcount;
679 elf_link_hash_traverse (elf_hash_table (info),
680 elf_link_renumber_hash_table_dynsyms,
681 &dynsymcount);
683 /* There is an unused NULL entry at the head of the table which
684 we must account for in our count. Unless there weren't any
685 symbols, which means we'll have no table at all. */
686 if (dynsymcount != 0)
687 ++dynsymcount;
689 return elf_hash_table (info)->dynsymcount = dynsymcount;
692 /* This function is called when we want to define a new symbol. It
693 handles the various cases which arise when we find a definition in
694 a dynamic object, or when there is already a definition in a
695 dynamic object. The new symbol is described by NAME, SYM, PSEC,
696 and PVALUE. We set SYM_HASH to the hash table entry. We set
697 OVERRIDE if the old symbol is overriding a new definition. We set
698 TYPE_CHANGE_OK if it is OK for the type to change. We set
699 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
700 change, we mean that we shouldn't warn if the type or size does
701 change. */
703 bfd_boolean
704 _bfd_elf_merge_symbol (bfd *abfd,
705 struct bfd_link_info *info,
706 const char *name,
707 Elf_Internal_Sym *sym,
708 asection **psec,
709 bfd_vma *pvalue,
710 struct elf_link_hash_entry **sym_hash,
711 bfd_boolean *skip,
712 bfd_boolean *override,
713 bfd_boolean *type_change_ok,
714 bfd_boolean *size_change_ok)
716 asection *sec, *oldsec;
717 struct elf_link_hash_entry *h;
718 struct elf_link_hash_entry *flip;
719 int bind;
720 bfd *oldbfd;
721 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
722 bfd_boolean newweak, oldweak, old_asneeded;
724 *skip = FALSE;
725 *override = FALSE;
727 sec = *psec;
728 bind = ELF_ST_BIND (sym->st_info);
730 if (! bfd_is_und_section (sec))
731 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
732 else
733 h = ((struct elf_link_hash_entry *)
734 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
735 if (h == NULL)
736 return FALSE;
737 *sym_hash = h;
739 /* This code is for coping with dynamic objects, and is only useful
740 if we are doing an ELF link. */
741 if (info->hash->creator != abfd->xvec)
742 return TRUE;
744 /* For merging, we only care about real symbols. */
746 while (h->root.type == bfd_link_hash_indirect
747 || h->root.type == bfd_link_hash_warning)
748 h = (struct elf_link_hash_entry *) h->root.u.i.link;
750 /* If we just created the symbol, mark it as being an ELF symbol.
751 Other than that, there is nothing to do--there is no merge issue
752 with a newly defined symbol--so we just return. */
754 if (h->root.type == bfd_link_hash_new)
756 h->non_elf = 0;
757 return TRUE;
760 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
761 existing symbol. */
763 switch (h->root.type)
765 default:
766 oldbfd = NULL;
767 oldsec = NULL;
768 break;
770 case bfd_link_hash_undefined:
771 case bfd_link_hash_undefweak:
772 oldbfd = h->root.u.undef.abfd;
773 oldsec = NULL;
774 break;
776 case bfd_link_hash_defined:
777 case bfd_link_hash_defweak:
778 oldbfd = h->root.u.def.section->owner;
779 oldsec = h->root.u.def.section;
780 break;
782 case bfd_link_hash_common:
783 oldbfd = h->root.u.c.p->section->owner;
784 oldsec = h->root.u.c.p->section;
785 break;
788 /* In cases involving weak versioned symbols, we may wind up trying
789 to merge a symbol with itself. Catch that here, to avoid the
790 confusion that results if we try to override a symbol with
791 itself. The additional tests catch cases like
792 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
793 dynamic object, which we do want to handle here. */
794 if (abfd == oldbfd
795 && ((abfd->flags & DYNAMIC) == 0
796 || !h->def_regular))
797 return TRUE;
799 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
800 respectively, is from a dynamic object. */
802 if ((abfd->flags & DYNAMIC) != 0)
803 newdyn = TRUE;
804 else
805 newdyn = FALSE;
807 if (oldbfd != NULL)
808 olddyn = (oldbfd->flags & DYNAMIC) != 0;
809 else
811 asection *hsec;
813 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
814 indices used by MIPS ELF. */
815 switch (h->root.type)
817 default:
818 hsec = NULL;
819 break;
821 case bfd_link_hash_defined:
822 case bfd_link_hash_defweak:
823 hsec = h->root.u.def.section;
824 break;
826 case bfd_link_hash_common:
827 hsec = h->root.u.c.p->section;
828 break;
831 if (hsec == NULL)
832 olddyn = FALSE;
833 else
834 olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0;
837 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
838 respectively, appear to be a definition rather than reference. */
840 if (bfd_is_und_section (sec) || bfd_is_com_section (sec))
841 newdef = FALSE;
842 else
843 newdef = TRUE;
845 if (h->root.type == bfd_link_hash_undefined
846 || h->root.type == bfd_link_hash_undefweak
847 || h->root.type == bfd_link_hash_common)
848 olddef = FALSE;
849 else
850 olddef = TRUE;
852 /* If the old definition came from an as-needed dynamic library which
853 wasn't found to be needed, treat the sym as undefined. */
854 old_asneeded = FALSE;
855 if (newdyn
856 && olddyn
857 && (elf_dyn_lib_class (oldbfd) & DYN_AS_NEEDED) != 0)
858 old_asneeded = TRUE;
860 /* Check TLS symbol. */
861 if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)
862 && ELF_ST_TYPE (sym->st_info) != h->type)
864 bfd *ntbfd, *tbfd;
865 bfd_boolean ntdef, tdef;
866 asection *ntsec, *tsec;
868 if (h->type == STT_TLS)
870 ntbfd = abfd;
871 ntsec = sec;
872 ntdef = newdef;
873 tbfd = oldbfd;
874 tsec = oldsec;
875 tdef = olddef;
877 else
879 ntbfd = oldbfd;
880 ntsec = oldsec;
881 ntdef = olddef;
882 tbfd = abfd;
883 tsec = sec;
884 tdef = newdef;
887 if (tdef && ntdef)
888 (*_bfd_error_handler)
889 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
890 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
891 else if (!tdef && !ntdef)
892 (*_bfd_error_handler)
893 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
894 tbfd, ntbfd, h->root.root.string);
895 else if (tdef)
896 (*_bfd_error_handler)
897 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
898 tbfd, tsec, ntbfd, h->root.root.string);
899 else
900 (*_bfd_error_handler)
901 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
902 tbfd, ntbfd, ntsec, h->root.root.string);
904 bfd_set_error (bfd_error_bad_value);
905 return FALSE;
908 /* We need to remember if a symbol has a definition in a dynamic
909 object or is weak in all dynamic objects. Internal and hidden
910 visibility will make it unavailable to dynamic objects. */
911 if (newdyn && !h->dynamic_def)
913 if (!bfd_is_und_section (sec))
914 h->dynamic_def = 1;
915 else
917 /* Check if this symbol is weak in all dynamic objects. If it
918 is the first time we see it in a dynamic object, we mark
919 if it is weak. Otherwise, we clear it. */
920 if (!h->ref_dynamic)
922 if (bind == STB_WEAK)
923 h->dynamic_weak = 1;
925 else if (bind != STB_WEAK)
926 h->dynamic_weak = 0;
930 /* If the old symbol has non-default visibility, we ignore the new
931 definition from a dynamic object. */
932 if (newdyn
933 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
934 && !bfd_is_und_section (sec))
936 *skip = TRUE;
937 /* Make sure this symbol is dynamic. */
938 h->ref_dynamic = 1;
939 /* A protected symbol has external availability. Make sure it is
940 recorded as dynamic.
942 FIXME: Should we check type and size for protected symbol? */
943 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
944 return bfd_elf_link_record_dynamic_symbol (info, h);
945 else
946 return TRUE;
948 else if (!newdyn
949 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
950 && h->def_dynamic)
952 /* If the new symbol with non-default visibility comes from a
953 relocatable file and the old definition comes from a dynamic
954 object, we remove the old definition. */
955 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
956 h = *sym_hash;
958 if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root)
959 && bfd_is_und_section (sec))
961 /* If the new symbol is undefined and the old symbol was
962 also undefined before, we need to make sure
963 _bfd_generic_link_add_one_symbol doesn't mess
964 up the linker hash table undefs list. Since the old
965 definition came from a dynamic object, it is still on the
966 undefs list. */
967 h->root.type = bfd_link_hash_undefined;
968 h->root.u.undef.abfd = abfd;
970 else
972 h->root.type = bfd_link_hash_new;
973 h->root.u.undef.abfd = NULL;
976 if (h->def_dynamic)
978 h->def_dynamic = 0;
979 h->ref_dynamic = 1;
980 h->dynamic_def = 1;
982 /* FIXME: Should we check type and size for protected symbol? */
983 h->size = 0;
984 h->type = 0;
985 return TRUE;
988 /* Differentiate strong and weak symbols. */
989 newweak = bind == STB_WEAK;
990 oldweak = (h->root.type == bfd_link_hash_defweak
991 || h->root.type == bfd_link_hash_undefweak);
993 /* If a new weak symbol definition comes from a regular file and the
994 old symbol comes from a dynamic library, we treat the new one as
995 strong. Similarly, an old weak symbol definition from a regular
996 file is treated as strong when the new symbol comes from a dynamic
997 library. Further, an old weak symbol from a dynamic library is
998 treated as strong if the new symbol is from a dynamic library.
999 This reflects the way glibc's ld.so works.
1001 Do this before setting *type_change_ok or *size_change_ok so that
1002 we warn properly when dynamic library symbols are overridden. */
1004 if (newdef && !newdyn && olddyn)
1005 newweak = FALSE;
1006 if (olddef && newdyn)
1007 oldweak = FALSE;
1009 /* It's OK to change the type if either the existing symbol or the
1010 new symbol is weak. A type change is also OK if the old symbol
1011 is undefined and the new symbol is defined. */
1013 if (oldweak
1014 || newweak
1015 || (newdef
1016 && h->root.type == bfd_link_hash_undefined))
1017 *type_change_ok = TRUE;
1019 /* It's OK to change the size if either the existing symbol or the
1020 new symbol is weak, or if the old symbol is undefined. */
1022 if (*type_change_ok
1023 || h->root.type == bfd_link_hash_undefined)
1024 *size_change_ok = TRUE;
1026 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1027 symbol, respectively, appears to be a common symbol in a dynamic
1028 object. If a symbol appears in an uninitialized section, and is
1029 not weak, and is not a function, then it may be a common symbol
1030 which was resolved when the dynamic object was created. We want
1031 to treat such symbols specially, because they raise special
1032 considerations when setting the symbol size: if the symbol
1033 appears as a common symbol in a regular object, and the size in
1034 the regular object is larger, we must make sure that we use the
1035 larger size. This problematic case can always be avoided in C,
1036 but it must be handled correctly when using Fortran shared
1037 libraries.
1039 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1040 likewise for OLDDYNCOMMON and OLDDEF.
1042 Note that this test is just a heuristic, and that it is quite
1043 possible to have an uninitialized symbol in a shared object which
1044 is really a definition, rather than a common symbol. This could
1045 lead to some minor confusion when the symbol really is a common
1046 symbol in some regular object. However, I think it will be
1047 harmless. */
1049 if (newdyn
1050 && newdef
1051 && !newweak
1052 && (sec->flags & SEC_ALLOC) != 0
1053 && (sec->flags & SEC_LOAD) == 0
1054 && sym->st_size > 0
1055 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
1056 newdyncommon = TRUE;
1057 else
1058 newdyncommon = FALSE;
1060 if (olddyn
1061 && olddef
1062 && !old_asneeded
1063 && h->root.type == bfd_link_hash_defined
1064 && h->def_dynamic
1065 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1066 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1067 && h->size > 0
1068 && h->type != STT_FUNC)
1069 olddyncommon = TRUE;
1070 else
1071 olddyncommon = FALSE;
1073 /* If both the old and the new symbols look like common symbols in a
1074 dynamic object, set the size of the symbol to the larger of the
1075 two. */
1077 if (olddyncommon
1078 && newdyncommon
1079 && sym->st_size != h->size)
1081 /* Since we think we have two common symbols, issue a multiple
1082 common warning if desired. Note that we only warn if the
1083 size is different. If the size is the same, we simply let
1084 the old symbol override the new one as normally happens with
1085 symbols defined in dynamic objects. */
1087 if (! ((*info->callbacks->multiple_common)
1088 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1089 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1090 return FALSE;
1092 if (sym->st_size > h->size)
1093 h->size = sym->st_size;
1095 *size_change_ok = TRUE;
1098 /* If we are looking at a dynamic object, and we have found a
1099 definition, we need to see if the symbol was already defined by
1100 some other object. If so, we want to use the existing
1101 definition, and we do not want to report a multiple symbol
1102 definition error; we do this by clobbering *PSEC to be
1103 bfd_und_section_ptr.
1105 We treat a common symbol as a definition if the symbol in the
1106 shared library is a function, since common symbols always
1107 represent variables; this can cause confusion in principle, but
1108 any such confusion would seem to indicate an erroneous program or
1109 shared library. We also permit a common symbol in a regular
1110 object to override a weak symbol in a shared object. */
1112 if (newdyn
1113 && newdef
1114 && ((olddef && !old_asneeded)
1115 || (h->root.type == bfd_link_hash_common
1116 && (newweak
1117 || ELF_ST_TYPE (sym->st_info) == STT_FUNC))))
1119 *override = TRUE;
1120 newdef = FALSE;
1121 newdyncommon = FALSE;
1123 *psec = sec = bfd_und_section_ptr;
1124 *size_change_ok = TRUE;
1126 /* If we get here when the old symbol is a common symbol, then
1127 we are explicitly letting it override a weak symbol or
1128 function in a dynamic object, and we don't want to warn about
1129 a type change. If the old symbol is a defined symbol, a type
1130 change warning may still be appropriate. */
1132 if (h->root.type == bfd_link_hash_common)
1133 *type_change_ok = TRUE;
1136 /* Handle the special case of an old common symbol merging with a
1137 new symbol which looks like a common symbol in a shared object.
1138 We change *PSEC and *PVALUE to make the new symbol look like a
1139 common symbol, and let _bfd_generic_link_add_one_symbol will do
1140 the right thing. */
1142 if (newdyncommon
1143 && h->root.type == bfd_link_hash_common)
1145 *override = TRUE;
1146 newdef = FALSE;
1147 newdyncommon = FALSE;
1148 *pvalue = sym->st_size;
1149 *psec = sec = bfd_com_section_ptr;
1150 *size_change_ok = TRUE;
1153 /* If the old symbol is from a dynamic object, and the new symbol is
1154 a definition which is not from a dynamic object, then the new
1155 symbol overrides the old symbol. Symbols from regular files
1156 always take precedence over symbols from dynamic objects, even if
1157 they are defined after the dynamic object in the link.
1159 As above, we again permit a common symbol in a regular object to
1160 override a definition in a shared object if the shared object
1161 symbol is a function or is weak. */
1163 flip = NULL;
1164 if ((!newdyn || old_asneeded)
1165 && (newdef
1166 || (bfd_is_com_section (sec)
1167 && (oldweak
1168 || h->type == STT_FUNC)))
1169 && olddyn
1170 && olddef
1171 && h->def_dynamic)
1173 /* Change the hash table entry to undefined, and let
1174 _bfd_generic_link_add_one_symbol do the right thing with the
1175 new definition. */
1177 h->root.type = bfd_link_hash_undefined;
1178 h->root.u.undef.abfd = h->root.u.def.section->owner;
1179 *size_change_ok = TRUE;
1181 olddef = FALSE;
1182 olddyncommon = FALSE;
1184 /* We again permit a type change when a common symbol may be
1185 overriding a function. */
1187 if (bfd_is_com_section (sec))
1188 *type_change_ok = TRUE;
1190 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1191 flip = *sym_hash;
1192 else
1193 /* This union may have been set to be non-NULL when this symbol
1194 was seen in a dynamic object. We must force the union to be
1195 NULL, so that it is correct for a regular symbol. */
1196 h->verinfo.vertree = NULL;
1199 /* Handle the special case of a new common symbol merging with an
1200 old symbol that looks like it might be a common symbol defined in
1201 a shared object. Note that we have already handled the case in
1202 which a new common symbol should simply override the definition
1203 in the shared library. */
1205 if (! newdyn
1206 && bfd_is_com_section (sec)
1207 && olddyncommon)
1209 /* It would be best if we could set the hash table entry to a
1210 common symbol, but we don't know what to use for the section
1211 or the alignment. */
1212 if (! ((*info->callbacks->multiple_common)
1213 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1214 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1215 return FALSE;
1217 /* If the presumed common symbol in the dynamic object is
1218 larger, pretend that the new symbol has its size. */
1220 if (h->size > *pvalue)
1221 *pvalue = h->size;
1223 /* FIXME: We no longer know the alignment required by the symbol
1224 in the dynamic object, so we just wind up using the one from
1225 the regular object. */
1227 olddef = FALSE;
1228 olddyncommon = FALSE;
1230 h->root.type = bfd_link_hash_undefined;
1231 h->root.u.undef.abfd = h->root.u.def.section->owner;
1233 *size_change_ok = TRUE;
1234 *type_change_ok = TRUE;
1236 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1237 flip = *sym_hash;
1238 else
1239 h->verinfo.vertree = NULL;
1242 if (flip != NULL)
1244 /* Handle the case where we had a versioned symbol in a dynamic
1245 library and now find a definition in a normal object. In this
1246 case, we make the versioned symbol point to the normal one. */
1247 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1248 flip->root.type = h->root.type;
1249 h->root.type = bfd_link_hash_indirect;
1250 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1251 (*bed->elf_backend_copy_indirect_symbol) (bed, flip, h);
1252 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1253 if (h->def_dynamic)
1255 h->def_dynamic = 0;
1256 flip->ref_dynamic = 1;
1260 return TRUE;
1263 /* This function is called to create an indirect symbol from the
1264 default for the symbol with the default version if needed. The
1265 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1266 set DYNSYM if the new indirect symbol is dynamic. */
1268 bfd_boolean
1269 _bfd_elf_add_default_symbol (bfd *abfd,
1270 struct bfd_link_info *info,
1271 struct elf_link_hash_entry *h,
1272 const char *name,
1273 Elf_Internal_Sym *sym,
1274 asection **psec,
1275 bfd_vma *value,
1276 bfd_boolean *dynsym,
1277 bfd_boolean override)
1279 bfd_boolean type_change_ok;
1280 bfd_boolean size_change_ok;
1281 bfd_boolean skip;
1282 char *shortname;
1283 struct elf_link_hash_entry *hi;
1284 struct bfd_link_hash_entry *bh;
1285 const struct elf_backend_data *bed;
1286 bfd_boolean collect;
1287 bfd_boolean dynamic;
1288 char *p;
1289 size_t len, shortlen;
1290 asection *sec;
1292 /* If this symbol has a version, and it is the default version, we
1293 create an indirect symbol from the default name to the fully
1294 decorated name. This will cause external references which do not
1295 specify a version to be bound to this version of the symbol. */
1296 p = strchr (name, ELF_VER_CHR);
1297 if (p == NULL || p[1] != ELF_VER_CHR)
1298 return TRUE;
1300 if (override)
1302 /* We are overridden by an old definition. We need to check if we
1303 need to create the indirect symbol from the default name. */
1304 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1305 FALSE, FALSE);
1306 BFD_ASSERT (hi != NULL);
1307 if (hi == h)
1308 return TRUE;
1309 while (hi->root.type == bfd_link_hash_indirect
1310 || hi->root.type == bfd_link_hash_warning)
1312 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1313 if (hi == h)
1314 return TRUE;
1318 bed = get_elf_backend_data (abfd);
1319 collect = bed->collect;
1320 dynamic = (abfd->flags & DYNAMIC) != 0;
1322 shortlen = p - name;
1323 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1324 if (shortname == NULL)
1325 return FALSE;
1326 memcpy (shortname, name, shortlen);
1327 shortname[shortlen] = '\0';
1329 /* We are going to create a new symbol. Merge it with any existing
1330 symbol with this name. For the purposes of the merge, act as
1331 though we were defining the symbol we just defined, although we
1332 actually going to define an indirect symbol. */
1333 type_change_ok = FALSE;
1334 size_change_ok = FALSE;
1335 sec = *psec;
1336 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1337 &hi, &skip, &override, &type_change_ok,
1338 &size_change_ok))
1339 return FALSE;
1341 if (skip)
1342 goto nondefault;
1344 if (! override)
1346 bh = &hi->root;
1347 if (! (_bfd_generic_link_add_one_symbol
1348 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1349 0, name, FALSE, collect, &bh)))
1350 return FALSE;
1351 hi = (struct elf_link_hash_entry *) bh;
1353 else
1355 /* In this case the symbol named SHORTNAME is overriding the
1356 indirect symbol we want to add. We were planning on making
1357 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1358 is the name without a version. NAME is the fully versioned
1359 name, and it is the default version.
1361 Overriding means that we already saw a definition for the
1362 symbol SHORTNAME in a regular object, and it is overriding
1363 the symbol defined in the dynamic object.
1365 When this happens, we actually want to change NAME, the
1366 symbol we just added, to refer to SHORTNAME. This will cause
1367 references to NAME in the shared object to become references
1368 to SHORTNAME in the regular object. This is what we expect
1369 when we override a function in a shared object: that the
1370 references in the shared object will be mapped to the
1371 definition in the regular object. */
1373 while (hi->root.type == bfd_link_hash_indirect
1374 || hi->root.type == bfd_link_hash_warning)
1375 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1377 h->root.type = bfd_link_hash_indirect;
1378 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1379 if (h->def_dynamic)
1381 h->def_dynamic = 0;
1382 hi->ref_dynamic = 1;
1383 if (hi->ref_regular
1384 || hi->def_regular)
1386 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1387 return FALSE;
1391 /* Now set HI to H, so that the following code will set the
1392 other fields correctly. */
1393 hi = h;
1396 /* If there is a duplicate definition somewhere, then HI may not
1397 point to an indirect symbol. We will have reported an error to
1398 the user in that case. */
1400 if (hi->root.type == bfd_link_hash_indirect)
1402 struct elf_link_hash_entry *ht;
1404 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1405 (*bed->elf_backend_copy_indirect_symbol) (bed, ht, hi);
1407 /* See if the new flags lead us to realize that the symbol must
1408 be dynamic. */
1409 if (! *dynsym)
1411 if (! dynamic)
1413 if (info->shared
1414 || hi->ref_dynamic)
1415 *dynsym = TRUE;
1417 else
1419 if (hi->ref_regular)
1420 *dynsym = TRUE;
1425 /* We also need to define an indirection from the nondefault version
1426 of the symbol. */
1428 nondefault:
1429 len = strlen (name);
1430 shortname = bfd_hash_allocate (&info->hash->table, len);
1431 if (shortname == NULL)
1432 return FALSE;
1433 memcpy (shortname, name, shortlen);
1434 memcpy (shortname + shortlen, p + 1, len - shortlen);
1436 /* Once again, merge with any existing symbol. */
1437 type_change_ok = FALSE;
1438 size_change_ok = FALSE;
1439 sec = *psec;
1440 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1441 &hi, &skip, &override, &type_change_ok,
1442 &size_change_ok))
1443 return FALSE;
1445 if (skip)
1446 return TRUE;
1448 if (override)
1450 /* Here SHORTNAME is a versioned name, so we don't expect to see
1451 the type of override we do in the case above unless it is
1452 overridden by a versioned definition. */
1453 if (hi->root.type != bfd_link_hash_defined
1454 && hi->root.type != bfd_link_hash_defweak)
1455 (*_bfd_error_handler)
1456 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1457 abfd, shortname);
1459 else
1461 bh = &hi->root;
1462 if (! (_bfd_generic_link_add_one_symbol
1463 (info, abfd, shortname, BSF_INDIRECT,
1464 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1465 return FALSE;
1466 hi = (struct elf_link_hash_entry *) bh;
1468 /* If there is a duplicate definition somewhere, then HI may not
1469 point to an indirect symbol. We will have reported an error
1470 to the user in that case. */
1472 if (hi->root.type == bfd_link_hash_indirect)
1474 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
1476 /* See if the new flags lead us to realize that the symbol
1477 must be dynamic. */
1478 if (! *dynsym)
1480 if (! dynamic)
1482 if (info->shared
1483 || hi->ref_dynamic)
1484 *dynsym = TRUE;
1486 else
1488 if (hi->ref_regular)
1489 *dynsym = TRUE;
1495 return TRUE;
1498 /* This routine is used to export all defined symbols into the dynamic
1499 symbol table. It is called via elf_link_hash_traverse. */
1501 bfd_boolean
1502 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1504 struct elf_info_failed *eif = data;
1506 /* Ignore indirect symbols. These are added by the versioning code. */
1507 if (h->root.type == bfd_link_hash_indirect)
1508 return TRUE;
1510 if (h->root.type == bfd_link_hash_warning)
1511 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1513 if (h->dynindx == -1
1514 && (h->def_regular
1515 || h->ref_regular))
1517 struct bfd_elf_version_tree *t;
1518 struct bfd_elf_version_expr *d;
1520 for (t = eif->verdefs; t != NULL; t = t->next)
1522 if (t->globals.list != NULL)
1524 d = (*t->match) (&t->globals, NULL, h->root.root.string);
1525 if (d != NULL)
1526 goto doit;
1529 if (t->locals.list != NULL)
1531 d = (*t->match) (&t->locals, NULL, h->root.root.string);
1532 if (d != NULL)
1533 return TRUE;
1537 if (!eif->verdefs)
1539 doit:
1540 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1542 eif->failed = TRUE;
1543 return FALSE;
1548 return TRUE;
1551 /* Look through the symbols which are defined in other shared
1552 libraries and referenced here. Update the list of version
1553 dependencies. This will be put into the .gnu.version_r section.
1554 This function is called via elf_link_hash_traverse. */
1556 bfd_boolean
1557 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1558 void *data)
1560 struct elf_find_verdep_info *rinfo = data;
1561 Elf_Internal_Verneed *t;
1562 Elf_Internal_Vernaux *a;
1563 bfd_size_type amt;
1565 if (h->root.type == bfd_link_hash_warning)
1566 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1568 /* We only care about symbols defined in shared objects with version
1569 information. */
1570 if (!h->def_dynamic
1571 || h->def_regular
1572 || h->dynindx == -1
1573 || h->verinfo.verdef == NULL)
1574 return TRUE;
1576 /* See if we already know about this version. */
1577 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1579 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1580 continue;
1582 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1583 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1584 return TRUE;
1586 break;
1589 /* This is a new version. Add it to tree we are building. */
1591 if (t == NULL)
1593 amt = sizeof *t;
1594 t = bfd_zalloc (rinfo->output_bfd, amt);
1595 if (t == NULL)
1597 rinfo->failed = TRUE;
1598 return FALSE;
1601 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1602 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1603 elf_tdata (rinfo->output_bfd)->verref = t;
1606 amt = sizeof *a;
1607 a = bfd_zalloc (rinfo->output_bfd, amt);
1609 /* Note that we are copying a string pointer here, and testing it
1610 above. If bfd_elf_string_from_elf_section is ever changed to
1611 discard the string data when low in memory, this will have to be
1612 fixed. */
1613 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1615 a->vna_flags = h->verinfo.verdef->vd_flags;
1616 a->vna_nextptr = t->vn_auxptr;
1618 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1619 ++rinfo->vers;
1621 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1623 t->vn_auxptr = a;
1625 return TRUE;
1628 /* Figure out appropriate versions for all the symbols. We may not
1629 have the version number script until we have read all of the input
1630 files, so until that point we don't know which symbols should be
1631 local. This function is called via elf_link_hash_traverse. */
1633 bfd_boolean
1634 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1636 struct elf_assign_sym_version_info *sinfo;
1637 struct bfd_link_info *info;
1638 const struct elf_backend_data *bed;
1639 struct elf_info_failed eif;
1640 char *p;
1641 bfd_size_type amt;
1643 sinfo = data;
1644 info = sinfo->info;
1646 if (h->root.type == bfd_link_hash_warning)
1647 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1649 /* Fix the symbol flags. */
1650 eif.failed = FALSE;
1651 eif.info = info;
1652 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1654 if (eif.failed)
1655 sinfo->failed = TRUE;
1656 return FALSE;
1659 /* We only need version numbers for symbols defined in regular
1660 objects. */
1661 if (!h->def_regular)
1662 return TRUE;
1664 bed = get_elf_backend_data (sinfo->output_bfd);
1665 p = strchr (h->root.root.string, ELF_VER_CHR);
1666 if (p != NULL && h->verinfo.vertree == NULL)
1668 struct bfd_elf_version_tree *t;
1669 bfd_boolean hidden;
1671 hidden = TRUE;
1673 /* There are two consecutive ELF_VER_CHR characters if this is
1674 not a hidden symbol. */
1675 ++p;
1676 if (*p == ELF_VER_CHR)
1678 hidden = FALSE;
1679 ++p;
1682 /* If there is no version string, we can just return out. */
1683 if (*p == '\0')
1685 if (hidden)
1686 h->hidden = 1;
1687 return TRUE;
1690 /* Look for the version. If we find it, it is no longer weak. */
1691 for (t = sinfo->verdefs; t != NULL; t = t->next)
1693 if (strcmp (t->name, p) == 0)
1695 size_t len;
1696 char *alc;
1697 struct bfd_elf_version_expr *d;
1699 len = p - h->root.root.string;
1700 alc = bfd_malloc (len);
1701 if (alc == NULL)
1702 return FALSE;
1703 memcpy (alc, h->root.root.string, len - 1);
1704 alc[len - 1] = '\0';
1705 if (alc[len - 2] == ELF_VER_CHR)
1706 alc[len - 2] = '\0';
1708 h->verinfo.vertree = t;
1709 t->used = TRUE;
1710 d = NULL;
1712 if (t->globals.list != NULL)
1713 d = (*t->match) (&t->globals, NULL, alc);
1715 /* See if there is anything to force this symbol to
1716 local scope. */
1717 if (d == NULL && t->locals.list != NULL)
1719 d = (*t->match) (&t->locals, NULL, alc);
1720 if (d != NULL
1721 && h->dynindx != -1
1722 && info->shared
1723 && ! info->export_dynamic)
1724 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1727 free (alc);
1728 break;
1732 /* If we are building an application, we need to create a
1733 version node for this version. */
1734 if (t == NULL && info->executable)
1736 struct bfd_elf_version_tree **pp;
1737 int version_index;
1739 /* If we aren't going to export this symbol, we don't need
1740 to worry about it. */
1741 if (h->dynindx == -1)
1742 return TRUE;
1744 amt = sizeof *t;
1745 t = bfd_zalloc (sinfo->output_bfd, amt);
1746 if (t == NULL)
1748 sinfo->failed = TRUE;
1749 return FALSE;
1752 t->name = p;
1753 t->name_indx = (unsigned int) -1;
1754 t->used = TRUE;
1756 version_index = 1;
1757 /* Don't count anonymous version tag. */
1758 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1759 version_index = 0;
1760 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1761 ++version_index;
1762 t->vernum = version_index;
1764 *pp = t;
1766 h->verinfo.vertree = t;
1768 else if (t == NULL)
1770 /* We could not find the version for a symbol when
1771 generating a shared archive. Return an error. */
1772 (*_bfd_error_handler)
1773 (_("%B: undefined versioned symbol name %s"),
1774 sinfo->output_bfd, h->root.root.string);
1775 bfd_set_error (bfd_error_bad_value);
1776 sinfo->failed = TRUE;
1777 return FALSE;
1780 if (hidden)
1781 h->hidden = 1;
1784 /* If we don't have a version for this symbol, see if we can find
1785 something. */
1786 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
1788 struct bfd_elf_version_tree *t;
1789 struct bfd_elf_version_tree *local_ver;
1790 struct bfd_elf_version_expr *d;
1792 /* See if can find what version this symbol is in. If the
1793 symbol is supposed to be local, then don't actually register
1794 it. */
1795 local_ver = NULL;
1796 for (t = sinfo->verdefs; t != NULL; t = t->next)
1798 if (t->globals.list != NULL)
1800 bfd_boolean matched;
1802 matched = FALSE;
1803 d = NULL;
1804 while ((d = (*t->match) (&t->globals, d,
1805 h->root.root.string)) != NULL)
1806 if (d->symver)
1807 matched = TRUE;
1808 else
1810 /* There is a version without definition. Make
1811 the symbol the default definition for this
1812 version. */
1813 h->verinfo.vertree = t;
1814 local_ver = NULL;
1815 d->script = 1;
1816 break;
1818 if (d != NULL)
1819 break;
1820 else if (matched)
1821 /* There is no undefined version for this symbol. Hide the
1822 default one. */
1823 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1826 if (t->locals.list != NULL)
1828 d = NULL;
1829 while ((d = (*t->match) (&t->locals, d,
1830 h->root.root.string)) != NULL)
1832 local_ver = t;
1833 /* If the match is "*", keep looking for a more
1834 explicit, perhaps even global, match.
1835 XXX: Shouldn't this be !d->wildcard instead? */
1836 if (d->pattern[0] != '*' || d->pattern[1] != '\0')
1837 break;
1840 if (d != NULL)
1841 break;
1845 if (local_ver != NULL)
1847 h->verinfo.vertree = local_ver;
1848 if (h->dynindx != -1
1849 && info->shared
1850 && ! info->export_dynamic)
1852 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1857 return TRUE;
1860 /* Read and swap the relocs from the section indicated by SHDR. This
1861 may be either a REL or a RELA section. The relocations are
1862 translated into RELA relocations and stored in INTERNAL_RELOCS,
1863 which should have already been allocated to contain enough space.
1864 The EXTERNAL_RELOCS are a buffer where the external form of the
1865 relocations should be stored.
1867 Returns FALSE if something goes wrong. */
1869 static bfd_boolean
1870 elf_link_read_relocs_from_section (bfd *abfd,
1871 asection *sec,
1872 Elf_Internal_Shdr *shdr,
1873 void *external_relocs,
1874 Elf_Internal_Rela *internal_relocs)
1876 const struct elf_backend_data *bed;
1877 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
1878 const bfd_byte *erela;
1879 const bfd_byte *erelaend;
1880 Elf_Internal_Rela *irela;
1881 Elf_Internal_Shdr *symtab_hdr;
1882 size_t nsyms;
1884 /* Position ourselves at the start of the section. */
1885 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
1886 return FALSE;
1888 /* Read the relocations. */
1889 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
1890 return FALSE;
1892 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1893 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
1895 bed = get_elf_backend_data (abfd);
1897 /* Convert the external relocations to the internal format. */
1898 if (shdr->sh_entsize == bed->s->sizeof_rel)
1899 swap_in = bed->s->swap_reloc_in;
1900 else if (shdr->sh_entsize == bed->s->sizeof_rela)
1901 swap_in = bed->s->swap_reloca_in;
1902 else
1904 bfd_set_error (bfd_error_wrong_format);
1905 return FALSE;
1908 erela = external_relocs;
1909 erelaend = erela + shdr->sh_size;
1910 irela = internal_relocs;
1911 while (erela < erelaend)
1913 bfd_vma r_symndx;
1915 (*swap_in) (abfd, erela, irela);
1916 r_symndx = ELF32_R_SYM (irela->r_info);
1917 if (bed->s->arch_size == 64)
1918 r_symndx >>= 24;
1919 if ((size_t) r_symndx >= nsyms)
1921 (*_bfd_error_handler)
1922 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
1923 " for offset 0x%lx in section `%A'"),
1924 abfd, sec,
1925 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
1926 bfd_set_error (bfd_error_bad_value);
1927 return FALSE;
1929 irela += bed->s->int_rels_per_ext_rel;
1930 erela += shdr->sh_entsize;
1933 return TRUE;
1936 /* Read and swap the relocs for a section O. They may have been
1937 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1938 not NULL, they are used as buffers to read into. They are known to
1939 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
1940 the return value is allocated using either malloc or bfd_alloc,
1941 according to the KEEP_MEMORY argument. If O has two relocation
1942 sections (both REL and RELA relocations), then the REL_HDR
1943 relocations will appear first in INTERNAL_RELOCS, followed by the
1944 REL_HDR2 relocations. */
1946 Elf_Internal_Rela *
1947 _bfd_elf_link_read_relocs (bfd *abfd,
1948 asection *o,
1949 void *external_relocs,
1950 Elf_Internal_Rela *internal_relocs,
1951 bfd_boolean keep_memory)
1953 Elf_Internal_Shdr *rel_hdr;
1954 void *alloc1 = NULL;
1955 Elf_Internal_Rela *alloc2 = NULL;
1956 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1958 if (elf_section_data (o)->relocs != NULL)
1959 return elf_section_data (o)->relocs;
1961 if (o->reloc_count == 0)
1962 return NULL;
1964 rel_hdr = &elf_section_data (o)->rel_hdr;
1966 if (internal_relocs == NULL)
1968 bfd_size_type size;
1970 size = o->reloc_count;
1971 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
1972 if (keep_memory)
1973 internal_relocs = bfd_alloc (abfd, size);
1974 else
1975 internal_relocs = alloc2 = bfd_malloc (size);
1976 if (internal_relocs == NULL)
1977 goto error_return;
1980 if (external_relocs == NULL)
1982 bfd_size_type size = rel_hdr->sh_size;
1984 if (elf_section_data (o)->rel_hdr2)
1985 size += elf_section_data (o)->rel_hdr2->sh_size;
1986 alloc1 = bfd_malloc (size);
1987 if (alloc1 == NULL)
1988 goto error_return;
1989 external_relocs = alloc1;
1992 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
1993 external_relocs,
1994 internal_relocs))
1995 goto error_return;
1996 if (elf_section_data (o)->rel_hdr2
1997 && (!elf_link_read_relocs_from_section
1998 (abfd, o,
1999 elf_section_data (o)->rel_hdr2,
2000 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2001 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2002 * bed->s->int_rels_per_ext_rel))))
2003 goto error_return;
2005 /* Cache the results for next time, if we can. */
2006 if (keep_memory)
2007 elf_section_data (o)->relocs = internal_relocs;
2009 if (alloc1 != NULL)
2010 free (alloc1);
2012 /* Don't free alloc2, since if it was allocated we are passing it
2013 back (under the name of internal_relocs). */
2015 return internal_relocs;
2017 error_return:
2018 if (alloc1 != NULL)
2019 free (alloc1);
2020 if (alloc2 != NULL)
2021 free (alloc2);
2022 return NULL;
2025 /* Compute the size of, and allocate space for, REL_HDR which is the
2026 section header for a section containing relocations for O. */
2028 bfd_boolean
2029 _bfd_elf_link_size_reloc_section (bfd *abfd,
2030 Elf_Internal_Shdr *rel_hdr,
2031 asection *o)
2033 bfd_size_type reloc_count;
2034 bfd_size_type num_rel_hashes;
2036 /* Figure out how many relocations there will be. */
2037 if (rel_hdr == &elf_section_data (o)->rel_hdr)
2038 reloc_count = elf_section_data (o)->rel_count;
2039 else
2040 reloc_count = elf_section_data (o)->rel_count2;
2042 num_rel_hashes = o->reloc_count;
2043 if (num_rel_hashes < reloc_count)
2044 num_rel_hashes = reloc_count;
2046 /* That allows us to calculate the size of the section. */
2047 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
2049 /* The contents field must last into write_object_contents, so we
2050 allocate it with bfd_alloc rather than malloc. Also since we
2051 cannot be sure that the contents will actually be filled in,
2052 we zero the allocated space. */
2053 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
2054 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2055 return FALSE;
2057 /* We only allocate one set of hash entries, so we only do it the
2058 first time we are called. */
2059 if (elf_section_data (o)->rel_hashes == NULL
2060 && num_rel_hashes)
2062 struct elf_link_hash_entry **p;
2064 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
2065 if (p == NULL)
2066 return FALSE;
2068 elf_section_data (o)->rel_hashes = p;
2071 return TRUE;
2074 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2075 originated from the section given by INPUT_REL_HDR) to the
2076 OUTPUT_BFD. */
2078 bfd_boolean
2079 _bfd_elf_link_output_relocs (bfd *output_bfd,
2080 asection *input_section,
2081 Elf_Internal_Shdr *input_rel_hdr,
2082 Elf_Internal_Rela *internal_relocs)
2084 Elf_Internal_Rela *irela;
2085 Elf_Internal_Rela *irelaend;
2086 bfd_byte *erel;
2087 Elf_Internal_Shdr *output_rel_hdr;
2088 asection *output_section;
2089 unsigned int *rel_countp = NULL;
2090 const struct elf_backend_data *bed;
2091 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2093 output_section = input_section->output_section;
2094 output_rel_hdr = NULL;
2096 if (elf_section_data (output_section)->rel_hdr.sh_entsize
2097 == input_rel_hdr->sh_entsize)
2099 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2100 rel_countp = &elf_section_data (output_section)->rel_count;
2102 else if (elf_section_data (output_section)->rel_hdr2
2103 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2104 == input_rel_hdr->sh_entsize))
2106 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2107 rel_countp = &elf_section_data (output_section)->rel_count2;
2109 else
2111 (*_bfd_error_handler)
2112 (_("%B: relocation size mismatch in %B section %A"),
2113 output_bfd, input_section->owner, input_section);
2114 bfd_set_error (bfd_error_wrong_object_format);
2115 return FALSE;
2118 bed = get_elf_backend_data (output_bfd);
2119 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2120 swap_out = bed->s->swap_reloc_out;
2121 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2122 swap_out = bed->s->swap_reloca_out;
2123 else
2124 abort ();
2126 erel = output_rel_hdr->contents;
2127 erel += *rel_countp * input_rel_hdr->sh_entsize;
2128 irela = internal_relocs;
2129 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2130 * bed->s->int_rels_per_ext_rel);
2131 while (irela < irelaend)
2133 (*swap_out) (output_bfd, irela, erel);
2134 irela += bed->s->int_rels_per_ext_rel;
2135 erel += input_rel_hdr->sh_entsize;
2138 /* Bump the counter, so that we know where to add the next set of
2139 relocations. */
2140 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2142 return TRUE;
2145 /* Fix up the flags for a symbol. This handles various cases which
2146 can only be fixed after all the input files are seen. This is
2147 currently called by both adjust_dynamic_symbol and
2148 assign_sym_version, which is unnecessary but perhaps more robust in
2149 the face of future changes. */
2151 bfd_boolean
2152 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2153 struct elf_info_failed *eif)
2155 /* If this symbol was mentioned in a non-ELF file, try to set
2156 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2157 permit a non-ELF file to correctly refer to a symbol defined in
2158 an ELF dynamic object. */
2159 if (h->non_elf)
2161 while (h->root.type == bfd_link_hash_indirect)
2162 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2164 if (h->root.type != bfd_link_hash_defined
2165 && h->root.type != bfd_link_hash_defweak)
2167 h->ref_regular = 1;
2168 h->ref_regular_nonweak = 1;
2170 else
2172 if (h->root.u.def.section->owner != NULL
2173 && (bfd_get_flavour (h->root.u.def.section->owner)
2174 == bfd_target_elf_flavour))
2176 h->ref_regular = 1;
2177 h->ref_regular_nonweak = 1;
2179 else
2180 h->def_regular = 1;
2183 if (h->dynindx == -1
2184 && (h->def_dynamic
2185 || h->ref_dynamic))
2187 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2189 eif->failed = TRUE;
2190 return FALSE;
2194 else
2196 /* Unfortunately, NON_ELF is only correct if the symbol
2197 was first seen in a non-ELF file. Fortunately, if the symbol
2198 was first seen in an ELF file, we're probably OK unless the
2199 symbol was defined in a non-ELF file. Catch that case here.
2200 FIXME: We're still in trouble if the symbol was first seen in
2201 a dynamic object, and then later in a non-ELF regular object. */
2202 if ((h->root.type == bfd_link_hash_defined
2203 || h->root.type == bfd_link_hash_defweak)
2204 && !h->def_regular
2205 && (h->root.u.def.section->owner != NULL
2206 ? (bfd_get_flavour (h->root.u.def.section->owner)
2207 != bfd_target_elf_flavour)
2208 : (bfd_is_abs_section (h->root.u.def.section)
2209 && !h->def_dynamic)))
2210 h->def_regular = 1;
2213 /* If this is a final link, and the symbol was defined as a common
2214 symbol in a regular object file, and there was no definition in
2215 any dynamic object, then the linker will have allocated space for
2216 the symbol in a common section but the DEF_REGULAR
2217 flag will not have been set. */
2218 if (h->root.type == bfd_link_hash_defined
2219 && !h->def_regular
2220 && h->ref_regular
2221 && !h->def_dynamic
2222 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2223 h->def_regular = 1;
2225 /* If -Bsymbolic was used (which means to bind references to global
2226 symbols to the definition within the shared object), and this
2227 symbol was defined in a regular object, then it actually doesn't
2228 need a PLT entry. Likewise, if the symbol has non-default
2229 visibility. If the symbol has hidden or internal visibility, we
2230 will force it local. */
2231 if (h->needs_plt
2232 && eif->info->shared
2233 && is_elf_hash_table (eif->info->hash)
2234 && (eif->info->symbolic
2235 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2236 && h->def_regular)
2238 const struct elf_backend_data *bed;
2239 bfd_boolean force_local;
2241 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2243 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2244 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2245 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2248 /* If a weak undefined symbol has non-default visibility, we also
2249 hide it from the dynamic linker. */
2250 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2251 && h->root.type == bfd_link_hash_undefweak)
2253 const struct elf_backend_data *bed;
2254 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2255 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2258 /* If this is a weak defined symbol in a dynamic object, and we know
2259 the real definition in the dynamic object, copy interesting flags
2260 over to the real definition. */
2261 if (h->u.weakdef != NULL)
2263 struct elf_link_hash_entry *weakdef;
2265 weakdef = h->u.weakdef;
2266 if (h->root.type == bfd_link_hash_indirect)
2267 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2269 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2270 || h->root.type == bfd_link_hash_defweak);
2271 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2272 || weakdef->root.type == bfd_link_hash_defweak);
2273 BFD_ASSERT (weakdef->def_dynamic);
2275 /* If the real definition is defined by a regular object file,
2276 don't do anything special. See the longer description in
2277 _bfd_elf_adjust_dynamic_symbol, below. */
2278 if (weakdef->def_regular)
2279 h->u.weakdef = NULL;
2280 else
2282 const struct elf_backend_data *bed;
2284 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2285 (*bed->elf_backend_copy_indirect_symbol) (bed, weakdef, h);
2289 return TRUE;
2292 /* Make the backend pick a good value for a dynamic symbol. This is
2293 called via elf_link_hash_traverse, and also calls itself
2294 recursively. */
2296 bfd_boolean
2297 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2299 struct elf_info_failed *eif = data;
2300 bfd *dynobj;
2301 const struct elf_backend_data *bed;
2303 if (! is_elf_hash_table (eif->info->hash))
2304 return FALSE;
2306 if (h->root.type == bfd_link_hash_warning)
2308 h->plt = elf_hash_table (eif->info)->init_offset;
2309 h->got = elf_hash_table (eif->info)->init_offset;
2311 /* When warning symbols are created, they **replace** the "real"
2312 entry in the hash table, thus we never get to see the real
2313 symbol in a hash traversal. So look at it now. */
2314 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2317 /* Ignore indirect symbols. These are added by the versioning code. */
2318 if (h->root.type == bfd_link_hash_indirect)
2319 return TRUE;
2321 /* Fix the symbol flags. */
2322 if (! _bfd_elf_fix_symbol_flags (h, eif))
2323 return FALSE;
2325 /* If this symbol does not require a PLT entry, and it is not
2326 defined by a dynamic object, or is not referenced by a regular
2327 object, ignore it. We do have to handle a weak defined symbol,
2328 even if no regular object refers to it, if we decided to add it
2329 to the dynamic symbol table. FIXME: Do we normally need to worry
2330 about symbols which are defined by one dynamic object and
2331 referenced by another one? */
2332 if (!h->needs_plt
2333 && (h->def_regular
2334 || !h->def_dynamic
2335 || (!h->ref_regular
2336 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2338 h->plt = elf_hash_table (eif->info)->init_offset;
2339 return TRUE;
2342 /* If we've already adjusted this symbol, don't do it again. This
2343 can happen via a recursive call. */
2344 if (h->dynamic_adjusted)
2345 return TRUE;
2347 /* Don't look at this symbol again. Note that we must set this
2348 after checking the above conditions, because we may look at a
2349 symbol once, decide not to do anything, and then get called
2350 recursively later after REF_REGULAR is set below. */
2351 h->dynamic_adjusted = 1;
2353 /* If this is a weak definition, and we know a real definition, and
2354 the real symbol is not itself defined by a regular object file,
2355 then get a good value for the real definition. We handle the
2356 real symbol first, for the convenience of the backend routine.
2358 Note that there is a confusing case here. If the real definition
2359 is defined by a regular object file, we don't get the real symbol
2360 from the dynamic object, but we do get the weak symbol. If the
2361 processor backend uses a COPY reloc, then if some routine in the
2362 dynamic object changes the real symbol, we will not see that
2363 change in the corresponding weak symbol. This is the way other
2364 ELF linkers work as well, and seems to be a result of the shared
2365 library model.
2367 I will clarify this issue. Most SVR4 shared libraries define the
2368 variable _timezone and define timezone as a weak synonym. The
2369 tzset call changes _timezone. If you write
2370 extern int timezone;
2371 int _timezone = 5;
2372 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2373 you might expect that, since timezone is a synonym for _timezone,
2374 the same number will print both times. However, if the processor
2375 backend uses a COPY reloc, then actually timezone will be copied
2376 into your process image, and, since you define _timezone
2377 yourself, _timezone will not. Thus timezone and _timezone will
2378 wind up at different memory locations. The tzset call will set
2379 _timezone, leaving timezone unchanged. */
2381 if (h->u.weakdef != NULL)
2383 /* If we get to this point, we know there is an implicit
2384 reference by a regular object file via the weak symbol H.
2385 FIXME: Is this really true? What if the traversal finds
2386 H->U.WEAKDEF before it finds H? */
2387 h->u.weakdef->ref_regular = 1;
2389 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2390 return FALSE;
2393 /* If a symbol has no type and no size and does not require a PLT
2394 entry, then we are probably about to do the wrong thing here: we
2395 are probably going to create a COPY reloc for an empty object.
2396 This case can arise when a shared object is built with assembly
2397 code, and the assembly code fails to set the symbol type. */
2398 if (h->size == 0
2399 && h->type == STT_NOTYPE
2400 && !h->needs_plt)
2401 (*_bfd_error_handler)
2402 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2403 h->root.root.string);
2405 dynobj = elf_hash_table (eif->info)->dynobj;
2406 bed = get_elf_backend_data (dynobj);
2407 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2409 eif->failed = TRUE;
2410 return FALSE;
2413 return TRUE;
2416 /* Adjust all external symbols pointing into SEC_MERGE sections
2417 to reflect the object merging within the sections. */
2419 bfd_boolean
2420 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2422 asection *sec;
2424 if (h->root.type == bfd_link_hash_warning)
2425 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2427 if ((h->root.type == bfd_link_hash_defined
2428 || h->root.type == bfd_link_hash_defweak)
2429 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2430 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2432 bfd *output_bfd = data;
2434 h->root.u.def.value =
2435 _bfd_merged_section_offset (output_bfd,
2436 &h->root.u.def.section,
2437 elf_section_data (sec)->sec_info,
2438 h->root.u.def.value);
2441 return TRUE;
2444 /* Returns false if the symbol referred to by H should be considered
2445 to resolve local to the current module, and true if it should be
2446 considered to bind dynamically. */
2448 bfd_boolean
2449 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2450 struct bfd_link_info *info,
2451 bfd_boolean ignore_protected)
2453 bfd_boolean binding_stays_local_p;
2455 if (h == NULL)
2456 return FALSE;
2458 while (h->root.type == bfd_link_hash_indirect
2459 || h->root.type == bfd_link_hash_warning)
2460 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2462 /* If it was forced local, then clearly it's not dynamic. */
2463 if (h->dynindx == -1)
2464 return FALSE;
2465 if (h->forced_local)
2466 return FALSE;
2468 /* Identify the cases where name binding rules say that a
2469 visible symbol resolves locally. */
2470 binding_stays_local_p = info->executable || info->symbolic;
2472 switch (ELF_ST_VISIBILITY (h->other))
2474 case STV_INTERNAL:
2475 case STV_HIDDEN:
2476 return FALSE;
2478 case STV_PROTECTED:
2479 /* Proper resolution for function pointer equality may require
2480 that these symbols perhaps be resolved dynamically, even though
2481 we should be resolving them to the current module. */
2482 if (!ignore_protected || h->type != STT_FUNC)
2483 binding_stays_local_p = TRUE;
2484 break;
2486 default:
2487 break;
2490 /* If it isn't defined locally, then clearly it's dynamic. */
2491 if (!h->def_regular)
2492 return TRUE;
2494 /* Otherwise, the symbol is dynamic if binding rules don't tell
2495 us that it remains local. */
2496 return !binding_stays_local_p;
2499 /* Return true if the symbol referred to by H should be considered
2500 to resolve local to the current module, and false otherwise. Differs
2501 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2502 undefined symbols and weak symbols. */
2504 bfd_boolean
2505 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2506 struct bfd_link_info *info,
2507 bfd_boolean local_protected)
2509 /* If it's a local sym, of course we resolve locally. */
2510 if (h == NULL)
2511 return TRUE;
2513 /* Common symbols that become definitions don't get the DEF_REGULAR
2514 flag set, so test it first, and don't bail out. */
2515 if (ELF_COMMON_DEF_P (h))
2516 /* Do nothing. */;
2517 /* If we don't have a definition in a regular file, then we can't
2518 resolve locally. The sym is either undefined or dynamic. */
2519 else if (!h->def_regular)
2520 return FALSE;
2522 /* Forced local symbols resolve locally. */
2523 if (h->forced_local)
2524 return TRUE;
2526 /* As do non-dynamic symbols. */
2527 if (h->dynindx == -1)
2528 return TRUE;
2530 /* At this point, we know the symbol is defined and dynamic. In an
2531 executable it must resolve locally, likewise when building symbolic
2532 shared libraries. */
2533 if (info->executable || info->symbolic)
2534 return TRUE;
2536 /* Now deal with defined dynamic symbols in shared libraries. Ones
2537 with default visibility might not resolve locally. */
2538 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2539 return FALSE;
2541 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2542 if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED)
2543 return TRUE;
2545 /* STV_PROTECTED non-function symbols are local. */
2546 if (h->type != STT_FUNC)
2547 return TRUE;
2549 /* Function pointer equality tests may require that STV_PROTECTED
2550 symbols be treated as dynamic symbols, even when we know that the
2551 dynamic linker will resolve them locally. */
2552 return local_protected;
2555 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2556 aligned. Returns the first TLS output section. */
2558 struct bfd_section *
2559 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2561 struct bfd_section *sec, *tls;
2562 unsigned int align = 0;
2564 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2565 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2566 break;
2567 tls = sec;
2569 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2570 if (sec->alignment_power > align)
2571 align = sec->alignment_power;
2573 elf_hash_table (info)->tls_sec = tls;
2575 /* Ensure the alignment of the first section is the largest alignment,
2576 so that the tls segment starts aligned. */
2577 if (tls != NULL)
2578 tls->alignment_power = align;
2580 return tls;
2583 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2584 static bfd_boolean
2585 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2586 Elf_Internal_Sym *sym)
2588 /* Local symbols do not count, but target specific ones might. */
2589 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2590 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2591 return FALSE;
2593 /* Function symbols do not count. */
2594 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
2595 return FALSE;
2597 /* If the section is undefined, then so is the symbol. */
2598 if (sym->st_shndx == SHN_UNDEF)
2599 return FALSE;
2601 /* If the symbol is defined in the common section, then
2602 it is a common definition and so does not count. */
2603 if (sym->st_shndx == SHN_COMMON)
2604 return FALSE;
2606 /* If the symbol is in a target specific section then we
2607 must rely upon the backend to tell us what it is. */
2608 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2609 /* FIXME - this function is not coded yet:
2611 return _bfd_is_global_symbol_definition (abfd, sym);
2613 Instead for now assume that the definition is not global,
2614 Even if this is wrong, at least the linker will behave
2615 in the same way that it used to do. */
2616 return FALSE;
2618 return TRUE;
2621 /* Search the symbol table of the archive element of the archive ABFD
2622 whose archive map contains a mention of SYMDEF, and determine if
2623 the symbol is defined in this element. */
2624 static bfd_boolean
2625 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2627 Elf_Internal_Shdr * hdr;
2628 bfd_size_type symcount;
2629 bfd_size_type extsymcount;
2630 bfd_size_type extsymoff;
2631 Elf_Internal_Sym *isymbuf;
2632 Elf_Internal_Sym *isym;
2633 Elf_Internal_Sym *isymend;
2634 bfd_boolean result;
2636 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2637 if (abfd == NULL)
2638 return FALSE;
2640 if (! bfd_check_format (abfd, bfd_object))
2641 return FALSE;
2643 /* If we have already included the element containing this symbol in the
2644 link then we do not need to include it again. Just claim that any symbol
2645 it contains is not a definition, so that our caller will not decide to
2646 (re)include this element. */
2647 if (abfd->archive_pass)
2648 return FALSE;
2650 /* Select the appropriate symbol table. */
2651 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2652 hdr = &elf_tdata (abfd)->symtab_hdr;
2653 else
2654 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2656 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2658 /* The sh_info field of the symtab header tells us where the
2659 external symbols start. We don't care about the local symbols. */
2660 if (elf_bad_symtab (abfd))
2662 extsymcount = symcount;
2663 extsymoff = 0;
2665 else
2667 extsymcount = symcount - hdr->sh_info;
2668 extsymoff = hdr->sh_info;
2671 if (extsymcount == 0)
2672 return FALSE;
2674 /* Read in the symbol table. */
2675 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2676 NULL, NULL, NULL);
2677 if (isymbuf == NULL)
2678 return FALSE;
2680 /* Scan the symbol table looking for SYMDEF. */
2681 result = FALSE;
2682 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2684 const char *name;
2686 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
2687 isym->st_name);
2688 if (name == NULL)
2689 break;
2691 if (strcmp (name, symdef->name) == 0)
2693 result = is_global_data_symbol_definition (abfd, isym);
2694 break;
2698 free (isymbuf);
2700 return result;
2703 /* Add an entry to the .dynamic table. */
2705 bfd_boolean
2706 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
2707 bfd_vma tag,
2708 bfd_vma val)
2710 struct elf_link_hash_table *hash_table;
2711 const struct elf_backend_data *bed;
2712 asection *s;
2713 bfd_size_type newsize;
2714 bfd_byte *newcontents;
2715 Elf_Internal_Dyn dyn;
2717 hash_table = elf_hash_table (info);
2718 if (! is_elf_hash_table (hash_table))
2719 return FALSE;
2721 if (info->warn_shared_textrel && info->shared && tag == DT_TEXTREL)
2722 _bfd_error_handler
2723 (_("warning: creating a DT_TEXTREL in a shared object."));
2725 bed = get_elf_backend_data (hash_table->dynobj);
2726 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2727 BFD_ASSERT (s != NULL);
2729 newsize = s->size + bed->s->sizeof_dyn;
2730 newcontents = bfd_realloc (s->contents, newsize);
2731 if (newcontents == NULL)
2732 return FALSE;
2734 dyn.d_tag = tag;
2735 dyn.d_un.d_val = val;
2736 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
2738 s->size = newsize;
2739 s->contents = newcontents;
2741 return TRUE;
2744 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2745 otherwise just check whether one already exists. Returns -1 on error,
2746 1 if a DT_NEEDED tag already exists, and 0 on success. */
2748 static int
2749 elf_add_dt_needed_tag (struct bfd_link_info *info,
2750 const char *soname,
2751 bfd_boolean do_it)
2753 struct elf_link_hash_table *hash_table;
2754 bfd_size_type oldsize;
2755 bfd_size_type strindex;
2757 hash_table = elf_hash_table (info);
2758 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
2759 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
2760 if (strindex == (bfd_size_type) -1)
2761 return -1;
2763 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
2765 asection *sdyn;
2766 const struct elf_backend_data *bed;
2767 bfd_byte *extdyn;
2769 bed = get_elf_backend_data (hash_table->dynobj);
2770 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2771 BFD_ASSERT (sdyn != NULL);
2773 for (extdyn = sdyn->contents;
2774 extdyn < sdyn->contents + sdyn->size;
2775 extdyn += bed->s->sizeof_dyn)
2777 Elf_Internal_Dyn dyn;
2779 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
2780 if (dyn.d_tag == DT_NEEDED
2781 && dyn.d_un.d_val == strindex)
2783 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2784 return 1;
2789 if (do_it)
2791 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
2792 return -1;
2794 else
2795 /* We were just checking for existence of the tag. */
2796 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2798 return 0;
2801 /* Sort symbol by value and section. */
2802 static int
2803 elf_sort_symbol (const void *arg1, const void *arg2)
2805 const struct elf_link_hash_entry *h1;
2806 const struct elf_link_hash_entry *h2;
2807 bfd_signed_vma vdiff;
2809 h1 = *(const struct elf_link_hash_entry **) arg1;
2810 h2 = *(const struct elf_link_hash_entry **) arg2;
2811 vdiff = h1->root.u.def.value - h2->root.u.def.value;
2812 if (vdiff != 0)
2813 return vdiff > 0 ? 1 : -1;
2814 else
2816 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
2817 if (sdiff != 0)
2818 return sdiff > 0 ? 1 : -1;
2820 return 0;
2823 /* This function is used to adjust offsets into .dynstr for
2824 dynamic symbols. This is called via elf_link_hash_traverse. */
2826 static bfd_boolean
2827 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
2829 struct elf_strtab_hash *dynstr = data;
2831 if (h->root.type == bfd_link_hash_warning)
2832 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2834 if (h->dynindx != -1)
2835 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
2836 return TRUE;
2839 /* Assign string offsets in .dynstr, update all structures referencing
2840 them. */
2842 static bfd_boolean
2843 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
2845 struct elf_link_hash_table *hash_table = elf_hash_table (info);
2846 struct elf_link_local_dynamic_entry *entry;
2847 struct elf_strtab_hash *dynstr = hash_table->dynstr;
2848 bfd *dynobj = hash_table->dynobj;
2849 asection *sdyn;
2850 bfd_size_type size;
2851 const struct elf_backend_data *bed;
2852 bfd_byte *extdyn;
2854 _bfd_elf_strtab_finalize (dynstr);
2855 size = _bfd_elf_strtab_size (dynstr);
2857 bed = get_elf_backend_data (dynobj);
2858 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2859 BFD_ASSERT (sdyn != NULL);
2861 /* Update all .dynamic entries referencing .dynstr strings. */
2862 for (extdyn = sdyn->contents;
2863 extdyn < sdyn->contents + sdyn->size;
2864 extdyn += bed->s->sizeof_dyn)
2866 Elf_Internal_Dyn dyn;
2868 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
2869 switch (dyn.d_tag)
2871 case DT_STRSZ:
2872 dyn.d_un.d_val = size;
2873 break;
2874 case DT_NEEDED:
2875 case DT_SONAME:
2876 case DT_RPATH:
2877 case DT_RUNPATH:
2878 case DT_FILTER:
2879 case DT_AUXILIARY:
2880 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
2881 break;
2882 default:
2883 continue;
2885 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
2888 /* Now update local dynamic symbols. */
2889 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
2890 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
2891 entry->isym.st_name);
2893 /* And the rest of dynamic symbols. */
2894 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
2896 /* Adjust version definitions. */
2897 if (elf_tdata (output_bfd)->cverdefs)
2899 asection *s;
2900 bfd_byte *p;
2901 bfd_size_type i;
2902 Elf_Internal_Verdef def;
2903 Elf_Internal_Verdaux defaux;
2905 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
2906 p = s->contents;
2909 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
2910 &def);
2911 p += sizeof (Elf_External_Verdef);
2912 if (def.vd_aux != sizeof (Elf_External_Verdef))
2913 continue;
2914 for (i = 0; i < def.vd_cnt; ++i)
2916 _bfd_elf_swap_verdaux_in (output_bfd,
2917 (Elf_External_Verdaux *) p, &defaux);
2918 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
2919 defaux.vda_name);
2920 _bfd_elf_swap_verdaux_out (output_bfd,
2921 &defaux, (Elf_External_Verdaux *) p);
2922 p += sizeof (Elf_External_Verdaux);
2925 while (def.vd_next);
2928 /* Adjust version references. */
2929 if (elf_tdata (output_bfd)->verref)
2931 asection *s;
2932 bfd_byte *p;
2933 bfd_size_type i;
2934 Elf_Internal_Verneed need;
2935 Elf_Internal_Vernaux needaux;
2937 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
2938 p = s->contents;
2941 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
2942 &need);
2943 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
2944 _bfd_elf_swap_verneed_out (output_bfd, &need,
2945 (Elf_External_Verneed *) p);
2946 p += sizeof (Elf_External_Verneed);
2947 for (i = 0; i < need.vn_cnt; ++i)
2949 _bfd_elf_swap_vernaux_in (output_bfd,
2950 (Elf_External_Vernaux *) p, &needaux);
2951 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
2952 needaux.vna_name);
2953 _bfd_elf_swap_vernaux_out (output_bfd,
2954 &needaux,
2955 (Elf_External_Vernaux *) p);
2956 p += sizeof (Elf_External_Vernaux);
2959 while (need.vn_next);
2962 return TRUE;
2965 /* Add symbols from an ELF object file to the linker hash table. */
2967 static bfd_boolean
2968 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
2970 bfd_boolean (*add_symbol_hook)
2971 (bfd *, struct bfd_link_info *, Elf_Internal_Sym *,
2972 const char **, flagword *, asection **, bfd_vma *);
2973 bfd_boolean (*check_relocs)
2974 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
2975 bfd_boolean (*check_directives)
2976 (bfd *, struct bfd_link_info *);
2977 bfd_boolean collect;
2978 Elf_Internal_Shdr *hdr;
2979 bfd_size_type symcount;
2980 bfd_size_type extsymcount;
2981 bfd_size_type extsymoff;
2982 struct elf_link_hash_entry **sym_hash;
2983 bfd_boolean dynamic;
2984 Elf_External_Versym *extversym = NULL;
2985 Elf_External_Versym *ever;
2986 struct elf_link_hash_entry *weaks;
2987 struct elf_link_hash_entry **nondeflt_vers = NULL;
2988 bfd_size_type nondeflt_vers_cnt = 0;
2989 Elf_Internal_Sym *isymbuf = NULL;
2990 Elf_Internal_Sym *isym;
2991 Elf_Internal_Sym *isymend;
2992 const struct elf_backend_data *bed;
2993 bfd_boolean add_needed;
2994 struct elf_link_hash_table * hash_table;
2995 bfd_size_type amt;
2997 hash_table = elf_hash_table (info);
2999 bed = get_elf_backend_data (abfd);
3000 add_symbol_hook = bed->elf_add_symbol_hook;
3001 collect = bed->collect;
3003 if ((abfd->flags & DYNAMIC) == 0)
3004 dynamic = FALSE;
3005 else
3007 dynamic = TRUE;
3009 /* You can't use -r against a dynamic object. Also, there's no
3010 hope of using a dynamic object which does not exactly match
3011 the format of the output file. */
3012 if (info->relocatable
3013 || !is_elf_hash_table (hash_table)
3014 || hash_table->root.creator != abfd->xvec)
3016 if (info->relocatable)
3017 bfd_set_error (bfd_error_invalid_operation);
3018 else
3019 bfd_set_error (bfd_error_wrong_format);
3020 goto error_return;
3024 /* As a GNU extension, any input sections which are named
3025 .gnu.warning.SYMBOL are treated as warning symbols for the given
3026 symbol. This differs from .gnu.warning sections, which generate
3027 warnings when they are included in an output file. */
3028 if (info->executable)
3030 asection *s;
3032 for (s = abfd->sections; s != NULL; s = s->next)
3034 const char *name;
3036 name = bfd_get_section_name (abfd, s);
3037 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
3039 char *msg;
3040 bfd_size_type sz;
3041 bfd_size_type prefix_len;
3042 const char * gnu_warning_prefix = _("warning: ");
3044 name += sizeof ".gnu.warning." - 1;
3046 /* If this is a shared object, then look up the symbol
3047 in the hash table. If it is there, and it is already
3048 been defined, then we will not be using the entry
3049 from this shared object, so we don't need to warn.
3050 FIXME: If we see the definition in a regular object
3051 later on, we will warn, but we shouldn't. The only
3052 fix is to keep track of what warnings we are supposed
3053 to emit, and then handle them all at the end of the
3054 link. */
3055 if (dynamic)
3057 struct elf_link_hash_entry *h;
3059 h = elf_link_hash_lookup (hash_table, name,
3060 FALSE, FALSE, TRUE);
3062 /* FIXME: What about bfd_link_hash_common? */
3063 if (h != NULL
3064 && (h->root.type == bfd_link_hash_defined
3065 || h->root.type == bfd_link_hash_defweak))
3067 /* We don't want to issue this warning. Clobber
3068 the section size so that the warning does not
3069 get copied into the output file. */
3070 s->size = 0;
3071 continue;
3075 sz = s->size;
3076 prefix_len = strlen (gnu_warning_prefix);
3077 msg = bfd_alloc (abfd, prefix_len + sz + 1);
3078 if (msg == NULL)
3079 goto error_return;
3081 strcpy (msg, gnu_warning_prefix);
3082 if (! bfd_get_section_contents (abfd, s, msg + prefix_len, 0, sz))
3083 goto error_return;
3085 msg[prefix_len + sz] = '\0';
3087 if (! (_bfd_generic_link_add_one_symbol
3088 (info, abfd, name, BSF_WARNING, s, 0, msg,
3089 FALSE, collect, NULL)))
3090 goto error_return;
3092 if (! info->relocatable)
3094 /* Clobber the section size so that the warning does
3095 not get copied into the output file. */
3096 s->size = 0;
3102 add_needed = TRUE;
3103 if (! dynamic)
3105 /* If we are creating a shared library, create all the dynamic
3106 sections immediately. We need to attach them to something,
3107 so we attach them to this BFD, provided it is the right
3108 format. FIXME: If there are no input BFD's of the same
3109 format as the output, we can't make a shared library. */
3110 if (info->shared
3111 && is_elf_hash_table (hash_table)
3112 && hash_table->root.creator == abfd->xvec
3113 && ! hash_table->dynamic_sections_created)
3115 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3116 goto error_return;
3119 else if (!is_elf_hash_table (hash_table))
3120 goto error_return;
3121 else
3123 asection *s;
3124 const char *soname = NULL;
3125 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3126 int ret;
3128 /* ld --just-symbols and dynamic objects don't mix very well.
3129 Test for --just-symbols by looking at info set up by
3130 _bfd_elf_link_just_syms. */
3131 if ((s = abfd->sections) != NULL
3132 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3133 goto error_return;
3135 /* If this dynamic lib was specified on the command line with
3136 --as-needed in effect, then we don't want to add a DT_NEEDED
3137 tag unless the lib is actually used. Similary for libs brought
3138 in by another lib's DT_NEEDED. When --no-add-needed is used
3139 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3140 any dynamic library in DT_NEEDED tags in the dynamic lib at
3141 all. */
3142 add_needed = (elf_dyn_lib_class (abfd)
3143 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3144 | DYN_NO_NEEDED)) == 0;
3146 s = bfd_get_section_by_name (abfd, ".dynamic");
3147 if (s != NULL)
3149 bfd_byte *dynbuf;
3150 bfd_byte *extdyn;
3151 int elfsec;
3152 unsigned long shlink;
3154 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3155 goto error_free_dyn;
3157 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3158 if (elfsec == -1)
3159 goto error_free_dyn;
3160 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3162 for (extdyn = dynbuf;
3163 extdyn < dynbuf + s->size;
3164 extdyn += bed->s->sizeof_dyn)
3166 Elf_Internal_Dyn dyn;
3168 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3169 if (dyn.d_tag == DT_SONAME)
3171 unsigned int tagv = dyn.d_un.d_val;
3172 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3173 if (soname == NULL)
3174 goto error_free_dyn;
3176 if (dyn.d_tag == DT_NEEDED)
3178 struct bfd_link_needed_list *n, **pn;
3179 char *fnm, *anm;
3180 unsigned int tagv = dyn.d_un.d_val;
3182 amt = sizeof (struct bfd_link_needed_list);
3183 n = bfd_alloc (abfd, amt);
3184 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3185 if (n == NULL || fnm == NULL)
3186 goto error_free_dyn;
3187 amt = strlen (fnm) + 1;
3188 anm = bfd_alloc (abfd, amt);
3189 if (anm == NULL)
3190 goto error_free_dyn;
3191 memcpy (anm, fnm, amt);
3192 n->name = anm;
3193 n->by = abfd;
3194 n->next = NULL;
3195 for (pn = & hash_table->needed;
3196 *pn != NULL;
3197 pn = &(*pn)->next)
3199 *pn = n;
3201 if (dyn.d_tag == DT_RUNPATH)
3203 struct bfd_link_needed_list *n, **pn;
3204 char *fnm, *anm;
3205 unsigned int tagv = dyn.d_un.d_val;
3207 amt = sizeof (struct bfd_link_needed_list);
3208 n = bfd_alloc (abfd, amt);
3209 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3210 if (n == NULL || fnm == NULL)
3211 goto error_free_dyn;
3212 amt = strlen (fnm) + 1;
3213 anm = bfd_alloc (abfd, amt);
3214 if (anm == NULL)
3215 goto error_free_dyn;
3216 memcpy (anm, fnm, amt);
3217 n->name = anm;
3218 n->by = abfd;
3219 n->next = NULL;
3220 for (pn = & runpath;
3221 *pn != NULL;
3222 pn = &(*pn)->next)
3224 *pn = n;
3226 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3227 if (!runpath && dyn.d_tag == DT_RPATH)
3229 struct bfd_link_needed_list *n, **pn;
3230 char *fnm, *anm;
3231 unsigned int tagv = dyn.d_un.d_val;
3233 amt = sizeof (struct bfd_link_needed_list);
3234 n = bfd_alloc (abfd, amt);
3235 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3236 if (n == NULL || fnm == NULL)
3237 goto error_free_dyn;
3238 amt = strlen (fnm) + 1;
3239 anm = bfd_alloc (abfd, amt);
3240 if (anm == NULL)
3242 error_free_dyn:
3243 free (dynbuf);
3244 goto error_return;
3246 memcpy (anm, fnm, amt);
3247 n->name = anm;
3248 n->by = abfd;
3249 n->next = NULL;
3250 for (pn = & rpath;
3251 *pn != NULL;
3252 pn = &(*pn)->next)
3254 *pn = n;
3258 free (dynbuf);
3261 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3262 frees all more recently bfd_alloc'd blocks as well. */
3263 if (runpath)
3264 rpath = runpath;
3266 if (rpath)
3268 struct bfd_link_needed_list **pn;
3269 for (pn = & hash_table->runpath;
3270 *pn != NULL;
3271 pn = &(*pn)->next)
3273 *pn = rpath;
3276 /* We do not want to include any of the sections in a dynamic
3277 object in the output file. We hack by simply clobbering the
3278 list of sections in the BFD. This could be handled more
3279 cleanly by, say, a new section flag; the existing
3280 SEC_NEVER_LOAD flag is not the one we want, because that one
3281 still implies that the section takes up space in the output
3282 file. */
3283 bfd_section_list_clear (abfd);
3285 /* If this is the first dynamic object found in the link, create
3286 the special sections required for dynamic linking. */
3287 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3288 goto error_return;
3290 /* Find the name to use in a DT_NEEDED entry that refers to this
3291 object. If the object has a DT_SONAME entry, we use it.
3292 Otherwise, if the generic linker stuck something in
3293 elf_dt_name, we use that. Otherwise, we just use the file
3294 name. */
3295 if (soname == NULL || *soname == '\0')
3297 soname = elf_dt_name (abfd);
3298 if (soname == NULL || *soname == '\0')
3299 soname = bfd_get_filename (abfd);
3302 /* Save the SONAME because sometimes the linker emulation code
3303 will need to know it. */
3304 elf_dt_name (abfd) = soname;
3306 ret = elf_add_dt_needed_tag (info, soname, add_needed);
3307 if (ret < 0)
3308 goto error_return;
3310 /* If we have already included this dynamic object in the
3311 link, just ignore it. There is no reason to include a
3312 particular dynamic object more than once. */
3313 if (ret > 0)
3314 return TRUE;
3317 /* If this is a dynamic object, we always link against the .dynsym
3318 symbol table, not the .symtab symbol table. The dynamic linker
3319 will only see the .dynsym symbol table, so there is no reason to
3320 look at .symtab for a dynamic object. */
3322 if (! dynamic || elf_dynsymtab (abfd) == 0)
3323 hdr = &elf_tdata (abfd)->symtab_hdr;
3324 else
3325 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3327 symcount = hdr->sh_size / bed->s->sizeof_sym;
3329 /* The sh_info field of the symtab header tells us where the
3330 external symbols start. We don't care about the local symbols at
3331 this point. */
3332 if (elf_bad_symtab (abfd))
3334 extsymcount = symcount;
3335 extsymoff = 0;
3337 else
3339 extsymcount = symcount - hdr->sh_info;
3340 extsymoff = hdr->sh_info;
3343 sym_hash = NULL;
3344 if (extsymcount != 0)
3346 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3347 NULL, NULL, NULL);
3348 if (isymbuf == NULL)
3349 goto error_return;
3351 /* We store a pointer to the hash table entry for each external
3352 symbol. */
3353 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3354 sym_hash = bfd_alloc (abfd, amt);
3355 if (sym_hash == NULL)
3356 goto error_free_sym;
3357 elf_sym_hashes (abfd) = sym_hash;
3360 if (dynamic)
3362 /* Read in any version definitions. */
3363 if (!_bfd_elf_slurp_version_tables (abfd,
3364 info->default_imported_symver))
3365 goto error_free_sym;
3367 /* Read in the symbol versions, but don't bother to convert them
3368 to internal format. */
3369 if (elf_dynversym (abfd) != 0)
3371 Elf_Internal_Shdr *versymhdr;
3373 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3374 extversym = bfd_malloc (versymhdr->sh_size);
3375 if (extversym == NULL)
3376 goto error_free_sym;
3377 amt = versymhdr->sh_size;
3378 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3379 || bfd_bread (extversym, amt, abfd) != amt)
3380 goto error_free_vers;
3384 weaks = NULL;
3386 ever = extversym != NULL ? extversym + extsymoff : NULL;
3387 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3388 isym < isymend;
3389 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3391 int bind;
3392 bfd_vma value;
3393 asection *sec;
3394 flagword flags;
3395 const char *name;
3396 struct elf_link_hash_entry *h;
3397 bfd_boolean definition;
3398 bfd_boolean size_change_ok;
3399 bfd_boolean type_change_ok;
3400 bfd_boolean new_weakdef;
3401 bfd_boolean override;
3402 unsigned int old_alignment;
3403 bfd *old_bfd;
3405 override = FALSE;
3407 flags = BSF_NO_FLAGS;
3408 sec = NULL;
3409 value = isym->st_value;
3410 *sym_hash = NULL;
3412 bind = ELF_ST_BIND (isym->st_info);
3413 if (bind == STB_LOCAL)
3415 /* This should be impossible, since ELF requires that all
3416 global symbols follow all local symbols, and that sh_info
3417 point to the first global symbol. Unfortunately, Irix 5
3418 screws this up. */
3419 continue;
3421 else if (bind == STB_GLOBAL)
3423 if (isym->st_shndx != SHN_UNDEF
3424 && isym->st_shndx != SHN_COMMON)
3425 flags = BSF_GLOBAL;
3427 else if (bind == STB_WEAK)
3428 flags = BSF_WEAK;
3429 else
3431 /* Leave it up to the processor backend. */
3434 if (isym->st_shndx == SHN_UNDEF)
3435 sec = bfd_und_section_ptr;
3436 else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
3438 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3439 if (sec == NULL)
3440 sec = bfd_abs_section_ptr;
3441 else if (sec->kept_section)
3443 /* Symbols from discarded section are undefined. */
3444 sec = bfd_und_section_ptr;
3445 isym->st_shndx = SHN_UNDEF;
3447 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3448 value -= sec->vma;
3450 else if (isym->st_shndx == SHN_ABS)
3451 sec = bfd_abs_section_ptr;
3452 else if (isym->st_shndx == SHN_COMMON)
3454 sec = bfd_com_section_ptr;
3455 /* What ELF calls the size we call the value. What ELF
3456 calls the value we call the alignment. */
3457 value = isym->st_size;
3459 else
3461 /* Leave it up to the processor backend. */
3464 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3465 isym->st_name);
3466 if (name == NULL)
3467 goto error_free_vers;
3469 if (isym->st_shndx == SHN_COMMON
3470 && ELF_ST_TYPE (isym->st_info) == STT_TLS)
3472 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3474 if (tcomm == NULL)
3476 tcomm = bfd_make_section (abfd, ".tcommon");
3477 if (tcomm == NULL
3478 || !bfd_set_section_flags (abfd, tcomm, (SEC_ALLOC
3479 | SEC_IS_COMMON
3480 | SEC_LINKER_CREATED
3481 | SEC_THREAD_LOCAL)))
3482 goto error_free_vers;
3484 sec = tcomm;
3486 else if (add_symbol_hook)
3488 if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec,
3489 &value))
3490 goto error_free_vers;
3492 /* The hook function sets the name to NULL if this symbol
3493 should be skipped for some reason. */
3494 if (name == NULL)
3495 continue;
3498 /* Sanity check that all possibilities were handled. */
3499 if (sec == NULL)
3501 bfd_set_error (bfd_error_bad_value);
3502 goto error_free_vers;
3505 if (bfd_is_und_section (sec)
3506 || bfd_is_com_section (sec))
3507 definition = FALSE;
3508 else
3509 definition = TRUE;
3511 size_change_ok = FALSE;
3512 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
3513 old_alignment = 0;
3514 old_bfd = NULL;
3516 if (is_elf_hash_table (hash_table))
3518 Elf_Internal_Versym iver;
3519 unsigned int vernum = 0;
3520 bfd_boolean skip;
3522 if (ever == NULL)
3524 if (info->default_imported_symver)
3525 /* Use the default symbol version created earlier. */
3526 iver.vs_vers = elf_tdata (abfd)->cverdefs;
3527 else
3528 iver.vs_vers = 0;
3530 else
3531 _bfd_elf_swap_versym_in (abfd, ever, &iver);
3533 vernum = iver.vs_vers & VERSYM_VERSION;
3535 /* If this is a hidden symbol, or if it is not version
3536 1, we append the version name to the symbol name.
3537 However, we do not modify a non-hidden absolute
3538 symbol, because it might be the version symbol
3539 itself. FIXME: What if it isn't? */
3540 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
3541 || (vernum > 1 && ! bfd_is_abs_section (sec)))
3543 const char *verstr;
3544 size_t namelen, verlen, newlen;
3545 char *newname, *p;
3547 if (isym->st_shndx != SHN_UNDEF)
3549 if (vernum > elf_tdata (abfd)->cverdefs)
3550 verstr = NULL;
3551 else if (vernum > 1)
3552 verstr =
3553 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
3554 else
3555 verstr = "";
3557 if (verstr == NULL)
3559 (*_bfd_error_handler)
3560 (_("%B: %s: invalid version %u (max %d)"),
3561 abfd, name, vernum,
3562 elf_tdata (abfd)->cverdefs);
3563 bfd_set_error (bfd_error_bad_value);
3564 goto error_free_vers;
3567 else
3569 /* We cannot simply test for the number of
3570 entries in the VERNEED section since the
3571 numbers for the needed versions do not start
3572 at 0. */
3573 Elf_Internal_Verneed *t;
3575 verstr = NULL;
3576 for (t = elf_tdata (abfd)->verref;
3577 t != NULL;
3578 t = t->vn_nextref)
3580 Elf_Internal_Vernaux *a;
3582 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3584 if (a->vna_other == vernum)
3586 verstr = a->vna_nodename;
3587 break;
3590 if (a != NULL)
3591 break;
3593 if (verstr == NULL)
3595 (*_bfd_error_handler)
3596 (_("%B: %s: invalid needed version %d"),
3597 abfd, name, vernum);
3598 bfd_set_error (bfd_error_bad_value);
3599 goto error_free_vers;
3603 namelen = strlen (name);
3604 verlen = strlen (verstr);
3605 newlen = namelen + verlen + 2;
3606 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3607 && isym->st_shndx != SHN_UNDEF)
3608 ++newlen;
3610 newname = bfd_alloc (abfd, newlen);
3611 if (newname == NULL)
3612 goto error_free_vers;
3613 memcpy (newname, name, namelen);
3614 p = newname + namelen;
3615 *p++ = ELF_VER_CHR;
3616 /* If this is a defined non-hidden version symbol,
3617 we add another @ to the name. This indicates the
3618 default version of the symbol. */
3619 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3620 && isym->st_shndx != SHN_UNDEF)
3621 *p++ = ELF_VER_CHR;
3622 memcpy (p, verstr, verlen + 1);
3624 name = newname;
3627 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
3628 sym_hash, &skip, &override,
3629 &type_change_ok, &size_change_ok))
3630 goto error_free_vers;
3632 if (skip)
3633 continue;
3635 if (override)
3636 definition = FALSE;
3638 h = *sym_hash;
3639 while (h->root.type == bfd_link_hash_indirect
3640 || h->root.type == bfd_link_hash_warning)
3641 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3643 /* Remember the old alignment if this is a common symbol, so
3644 that we don't reduce the alignment later on. We can't
3645 check later, because _bfd_generic_link_add_one_symbol
3646 will set a default for the alignment which we want to
3647 override. We also remember the old bfd where the existing
3648 definition comes from. */
3649 switch (h->root.type)
3651 default:
3652 break;
3654 case bfd_link_hash_defined:
3655 case bfd_link_hash_defweak:
3656 old_bfd = h->root.u.def.section->owner;
3657 break;
3659 case bfd_link_hash_common:
3660 old_bfd = h->root.u.c.p->section->owner;
3661 old_alignment = h->root.u.c.p->alignment_power;
3662 break;
3665 if (elf_tdata (abfd)->verdef != NULL
3666 && ! override
3667 && vernum > 1
3668 && definition)
3669 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
3672 if (! (_bfd_generic_link_add_one_symbol
3673 (info, abfd, name, flags, sec, value, NULL, FALSE, collect,
3674 (struct bfd_link_hash_entry **) sym_hash)))
3675 goto error_free_vers;
3677 h = *sym_hash;
3678 while (h->root.type == bfd_link_hash_indirect
3679 || h->root.type == bfd_link_hash_warning)
3680 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3681 *sym_hash = h;
3683 new_weakdef = FALSE;
3684 if (dynamic
3685 && definition
3686 && (flags & BSF_WEAK) != 0
3687 && ELF_ST_TYPE (isym->st_info) != STT_FUNC
3688 && is_elf_hash_table (hash_table)
3689 && h->u.weakdef == NULL)
3691 /* Keep a list of all weak defined non function symbols from
3692 a dynamic object, using the weakdef field. Later in this
3693 function we will set the weakdef field to the correct
3694 value. We only put non-function symbols from dynamic
3695 objects on this list, because that happens to be the only
3696 time we need to know the normal symbol corresponding to a
3697 weak symbol, and the information is time consuming to
3698 figure out. If the weakdef field is not already NULL,
3699 then this symbol was already defined by some previous
3700 dynamic object, and we will be using that previous
3701 definition anyhow. */
3703 h->u.weakdef = weaks;
3704 weaks = h;
3705 new_weakdef = TRUE;
3708 /* Set the alignment of a common symbol. */
3709 if (isym->st_shndx == SHN_COMMON
3710 && h->root.type == bfd_link_hash_common)
3712 unsigned int align;
3714 align = bfd_log2 (isym->st_value);
3715 if (align > old_alignment
3716 /* Permit an alignment power of zero if an alignment of one
3717 is specified and no other alignments have been specified. */
3718 || (isym->st_value == 1 && old_alignment == 0))
3719 h->root.u.c.p->alignment_power = align;
3720 else
3721 h->root.u.c.p->alignment_power = old_alignment;
3724 if (is_elf_hash_table (hash_table))
3726 bfd_boolean dynsym;
3728 /* Check the alignment when a common symbol is involved. This
3729 can change when a common symbol is overridden by a normal
3730 definition or a common symbol is ignored due to the old
3731 normal definition. We need to make sure the maximum
3732 alignment is maintained. */
3733 if ((old_alignment || isym->st_shndx == SHN_COMMON)
3734 && h->root.type != bfd_link_hash_common)
3736 unsigned int common_align;
3737 unsigned int normal_align;
3738 unsigned int symbol_align;
3739 bfd *normal_bfd;
3740 bfd *common_bfd;
3742 symbol_align = ffs (h->root.u.def.value) - 1;
3743 if (h->root.u.def.section->owner != NULL
3744 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3746 normal_align = h->root.u.def.section->alignment_power;
3747 if (normal_align > symbol_align)
3748 normal_align = symbol_align;
3750 else
3751 normal_align = symbol_align;
3753 if (old_alignment)
3755 common_align = old_alignment;
3756 common_bfd = old_bfd;
3757 normal_bfd = abfd;
3759 else
3761 common_align = bfd_log2 (isym->st_value);
3762 common_bfd = abfd;
3763 normal_bfd = old_bfd;
3766 if (normal_align < common_align)
3767 (*_bfd_error_handler)
3768 (_("Warning: alignment %u of symbol `%s' in %B"
3769 " is smaller than %u in %B"),
3770 normal_bfd, common_bfd,
3771 1 << normal_align, name, 1 << common_align);
3774 /* Remember the symbol size and type. */
3775 if (isym->st_size != 0
3776 && (definition || h->size == 0))
3778 if (h->size != 0 && h->size != isym->st_size && ! size_change_ok)
3779 (*_bfd_error_handler)
3780 (_("Warning: size of symbol `%s' changed"
3781 " from %lu in %B to %lu in %B"),
3782 old_bfd, abfd,
3783 name, (unsigned long) h->size,
3784 (unsigned long) isym->st_size);
3786 h->size = isym->st_size;
3789 /* If this is a common symbol, then we always want H->SIZE
3790 to be the size of the common symbol. The code just above
3791 won't fix the size if a common symbol becomes larger. We
3792 don't warn about a size change here, because that is
3793 covered by --warn-common. */
3794 if (h->root.type == bfd_link_hash_common)
3795 h->size = h->root.u.c.size;
3797 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
3798 && (definition || h->type == STT_NOTYPE))
3800 if (h->type != STT_NOTYPE
3801 && h->type != ELF_ST_TYPE (isym->st_info)
3802 && ! type_change_ok)
3803 (*_bfd_error_handler)
3804 (_("Warning: type of symbol `%s' changed"
3805 " from %d to %d in %B"),
3806 abfd, name, h->type, ELF_ST_TYPE (isym->st_info));
3808 h->type = ELF_ST_TYPE (isym->st_info);
3811 /* If st_other has a processor-specific meaning, specific
3812 code might be needed here. We never merge the visibility
3813 attribute with the one from a dynamic object. */
3814 if (bed->elf_backend_merge_symbol_attribute)
3815 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
3816 dynamic);
3818 /* If this symbol has default visibility and the user has requested
3819 we not re-export it, then mark it as hidden. */
3820 if (definition && !dynamic
3821 && (abfd->no_export
3822 || (abfd->my_archive && abfd->my_archive->no_export))
3823 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
3824 isym->st_other = STV_HIDDEN | (isym->st_other & ~ ELF_ST_VISIBILITY (-1));
3826 if (isym->st_other != 0 && !dynamic)
3828 unsigned char hvis, symvis, other, nvis;
3830 /* Take the balance of OTHER from the definition. */
3831 other = (definition ? isym->st_other : h->other);
3832 other &= ~ ELF_ST_VISIBILITY (-1);
3834 /* Combine visibilities, using the most constraining one. */
3835 hvis = ELF_ST_VISIBILITY (h->other);
3836 symvis = ELF_ST_VISIBILITY (isym->st_other);
3837 if (! hvis)
3838 nvis = symvis;
3839 else if (! symvis)
3840 nvis = hvis;
3841 else
3842 nvis = hvis < symvis ? hvis : symvis;
3844 h->other = other | nvis;
3847 /* Set a flag in the hash table entry indicating the type of
3848 reference or definition we just found. Keep a count of
3849 the number of dynamic symbols we find. A dynamic symbol
3850 is one which is referenced or defined by both a regular
3851 object and a shared object. */
3852 dynsym = FALSE;
3853 if (! dynamic)
3855 if (! definition)
3857 h->ref_regular = 1;
3858 if (bind != STB_WEAK)
3859 h->ref_regular_nonweak = 1;
3861 else
3862 h->def_regular = 1;
3863 if (! info->executable
3864 || h->def_dynamic
3865 || h->ref_dynamic)
3866 dynsym = TRUE;
3868 else
3870 if (! definition)
3871 h->ref_dynamic = 1;
3872 else
3873 h->def_dynamic = 1;
3874 if (h->def_regular
3875 || h->ref_regular
3876 || (h->u.weakdef != NULL
3877 && ! new_weakdef
3878 && h->u.weakdef->dynindx != -1))
3879 dynsym = TRUE;
3882 /* Check to see if we need to add an indirect symbol for
3883 the default name. */
3884 if (definition || h->root.type == bfd_link_hash_common)
3885 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
3886 &sec, &value, &dynsym,
3887 override))
3888 goto error_free_vers;
3890 if (definition && !dynamic)
3892 char *p = strchr (name, ELF_VER_CHR);
3893 if (p != NULL && p[1] != ELF_VER_CHR)
3895 /* Queue non-default versions so that .symver x, x@FOO
3896 aliases can be checked. */
3897 if (! nondeflt_vers)
3899 amt = (isymend - isym + 1)
3900 * sizeof (struct elf_link_hash_entry *);
3901 nondeflt_vers = bfd_malloc (amt);
3903 nondeflt_vers [nondeflt_vers_cnt++] = h;
3907 if (dynsym && h->dynindx == -1)
3909 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3910 goto error_free_vers;
3911 if (h->u.weakdef != NULL
3912 && ! new_weakdef
3913 && h->u.weakdef->dynindx == -1)
3915 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
3916 goto error_free_vers;
3919 else if (dynsym && h->dynindx != -1)
3920 /* If the symbol already has a dynamic index, but
3921 visibility says it should not be visible, turn it into
3922 a local symbol. */
3923 switch (ELF_ST_VISIBILITY (h->other))
3925 case STV_INTERNAL:
3926 case STV_HIDDEN:
3927 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
3928 dynsym = FALSE;
3929 break;
3932 if (!add_needed
3933 && definition
3934 && dynsym
3935 && h->ref_regular)
3937 int ret;
3938 const char *soname = elf_dt_name (abfd);
3940 /* A symbol from a library loaded via DT_NEEDED of some
3941 other library is referenced by a regular object.
3942 Add a DT_NEEDED entry for it. Issue an error if
3943 --no-add-needed is used. */
3944 if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
3946 (*_bfd_error_handler)
3947 (_("%s: invalid DSO for symbol `%s' definition"),
3948 abfd, name);
3949 bfd_set_error (bfd_error_bad_value);
3950 goto error_free_vers;
3953 elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED;
3955 add_needed = TRUE;
3956 ret = elf_add_dt_needed_tag (info, soname, add_needed);
3957 if (ret < 0)
3958 goto error_free_vers;
3960 BFD_ASSERT (ret == 0);
3965 /* Now that all the symbols from this input file are created, handle
3966 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
3967 if (nondeflt_vers != NULL)
3969 bfd_size_type cnt, symidx;
3971 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
3973 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
3974 char *shortname, *p;
3976 p = strchr (h->root.root.string, ELF_VER_CHR);
3977 if (p == NULL
3978 || (h->root.type != bfd_link_hash_defined
3979 && h->root.type != bfd_link_hash_defweak))
3980 continue;
3982 amt = p - h->root.root.string;
3983 shortname = bfd_malloc (amt + 1);
3984 memcpy (shortname, h->root.root.string, amt);
3985 shortname[amt] = '\0';
3987 hi = (struct elf_link_hash_entry *)
3988 bfd_link_hash_lookup (&hash_table->root, shortname,
3989 FALSE, FALSE, FALSE);
3990 if (hi != NULL
3991 && hi->root.type == h->root.type
3992 && hi->root.u.def.value == h->root.u.def.value
3993 && hi->root.u.def.section == h->root.u.def.section)
3995 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
3996 hi->root.type = bfd_link_hash_indirect;
3997 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
3998 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
3999 sym_hash = elf_sym_hashes (abfd);
4000 if (sym_hash)
4001 for (symidx = 0; symidx < extsymcount; ++symidx)
4002 if (sym_hash[symidx] == hi)
4004 sym_hash[symidx] = h;
4005 break;
4008 free (shortname);
4010 free (nondeflt_vers);
4011 nondeflt_vers = NULL;
4014 if (extversym != NULL)
4016 free (extversym);
4017 extversym = NULL;
4020 if (isymbuf != NULL)
4021 free (isymbuf);
4022 isymbuf = NULL;
4024 /* Now set the weakdefs field correctly for all the weak defined
4025 symbols we found. The only way to do this is to search all the
4026 symbols. Since we only need the information for non functions in
4027 dynamic objects, that's the only time we actually put anything on
4028 the list WEAKS. We need this information so that if a regular
4029 object refers to a symbol defined weakly in a dynamic object, the
4030 real symbol in the dynamic object is also put in the dynamic
4031 symbols; we also must arrange for both symbols to point to the
4032 same memory location. We could handle the general case of symbol
4033 aliasing, but a general symbol alias can only be generated in
4034 assembler code, handling it correctly would be very time
4035 consuming, and other ELF linkers don't handle general aliasing
4036 either. */
4037 if (weaks != NULL)
4039 struct elf_link_hash_entry **hpp;
4040 struct elf_link_hash_entry **hppend;
4041 struct elf_link_hash_entry **sorted_sym_hash;
4042 struct elf_link_hash_entry *h;
4043 size_t sym_count;
4045 /* Since we have to search the whole symbol list for each weak
4046 defined symbol, search time for N weak defined symbols will be
4047 O(N^2). Binary search will cut it down to O(NlogN). */
4048 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4049 sorted_sym_hash = bfd_malloc (amt);
4050 if (sorted_sym_hash == NULL)
4051 goto error_return;
4052 sym_hash = sorted_sym_hash;
4053 hpp = elf_sym_hashes (abfd);
4054 hppend = hpp + extsymcount;
4055 sym_count = 0;
4056 for (; hpp < hppend; hpp++)
4058 h = *hpp;
4059 if (h != NULL
4060 && h->root.type == bfd_link_hash_defined
4061 && h->type != STT_FUNC)
4063 *sym_hash = h;
4064 sym_hash++;
4065 sym_count++;
4069 qsort (sorted_sym_hash, sym_count,
4070 sizeof (struct elf_link_hash_entry *),
4071 elf_sort_symbol);
4073 while (weaks != NULL)
4075 struct elf_link_hash_entry *hlook;
4076 asection *slook;
4077 bfd_vma vlook;
4078 long ilook;
4079 size_t i, j, idx;
4081 hlook = weaks;
4082 weaks = hlook->u.weakdef;
4083 hlook->u.weakdef = NULL;
4085 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4086 || hlook->root.type == bfd_link_hash_defweak
4087 || hlook->root.type == bfd_link_hash_common
4088 || hlook->root.type == bfd_link_hash_indirect);
4089 slook = hlook->root.u.def.section;
4090 vlook = hlook->root.u.def.value;
4092 ilook = -1;
4093 i = 0;
4094 j = sym_count;
4095 while (i < j)
4097 bfd_signed_vma vdiff;
4098 idx = (i + j) / 2;
4099 h = sorted_sym_hash [idx];
4100 vdiff = vlook - h->root.u.def.value;
4101 if (vdiff < 0)
4102 j = idx;
4103 else if (vdiff > 0)
4104 i = idx + 1;
4105 else
4107 long sdiff = slook->id - h->root.u.def.section->id;
4108 if (sdiff < 0)
4109 j = idx;
4110 else if (sdiff > 0)
4111 i = idx + 1;
4112 else
4114 ilook = idx;
4115 break;
4120 /* We didn't find a value/section match. */
4121 if (ilook == -1)
4122 continue;
4124 for (i = ilook; i < sym_count; i++)
4126 h = sorted_sym_hash [i];
4128 /* Stop if value or section doesn't match. */
4129 if (h->root.u.def.value != vlook
4130 || h->root.u.def.section != slook)
4131 break;
4132 else if (h != hlook)
4134 hlook->u.weakdef = h;
4136 /* If the weak definition is in the list of dynamic
4137 symbols, make sure the real definition is put
4138 there as well. */
4139 if (hlook->dynindx != -1 && h->dynindx == -1)
4141 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4142 goto error_return;
4145 /* If the real definition is in the list of dynamic
4146 symbols, make sure the weak definition is put
4147 there as well. If we don't do this, then the
4148 dynamic loader might not merge the entries for the
4149 real definition and the weak definition. */
4150 if (h->dynindx != -1 && hlook->dynindx == -1)
4152 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4153 goto error_return;
4155 break;
4160 free (sorted_sym_hash);
4163 check_directives = get_elf_backend_data (abfd)->check_directives;
4164 if (check_directives)
4165 check_directives (abfd, info);
4167 /* If this object is the same format as the output object, and it is
4168 not a shared library, then let the backend look through the
4169 relocs.
4171 This is required to build global offset table entries and to
4172 arrange for dynamic relocs. It is not required for the
4173 particular common case of linking non PIC code, even when linking
4174 against shared libraries, but unfortunately there is no way of
4175 knowing whether an object file has been compiled PIC or not.
4176 Looking through the relocs is not particularly time consuming.
4177 The problem is that we must either (1) keep the relocs in memory,
4178 which causes the linker to require additional runtime memory or
4179 (2) read the relocs twice from the input file, which wastes time.
4180 This would be a good case for using mmap.
4182 I have no idea how to handle linking PIC code into a file of a
4183 different format. It probably can't be done. */
4184 check_relocs = get_elf_backend_data (abfd)->check_relocs;
4185 if (! dynamic
4186 && is_elf_hash_table (hash_table)
4187 && hash_table->root.creator == abfd->xvec
4188 && check_relocs != NULL)
4190 asection *o;
4192 for (o = abfd->sections; o != NULL; o = o->next)
4194 Elf_Internal_Rela *internal_relocs;
4195 bfd_boolean ok;
4197 if ((o->flags & SEC_RELOC) == 0
4198 || o->reloc_count == 0
4199 || ((info->strip == strip_all || info->strip == strip_debugger)
4200 && (o->flags & SEC_DEBUGGING) != 0)
4201 || bfd_is_abs_section (o->output_section))
4202 continue;
4204 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4205 info->keep_memory);
4206 if (internal_relocs == NULL)
4207 goto error_return;
4209 ok = (*check_relocs) (abfd, info, o, internal_relocs);
4211 if (elf_section_data (o)->relocs != internal_relocs)
4212 free (internal_relocs);
4214 if (! ok)
4215 goto error_return;
4219 /* If this is a non-traditional link, try to optimize the handling
4220 of the .stab/.stabstr sections. */
4221 if (! dynamic
4222 && ! info->traditional_format
4223 && is_elf_hash_table (hash_table)
4224 && (info->strip != strip_all && info->strip != strip_debugger))
4226 asection *stabstr;
4228 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4229 if (stabstr != NULL)
4231 bfd_size_type string_offset = 0;
4232 asection *stab;
4234 for (stab = abfd->sections; stab; stab = stab->next)
4235 if (strncmp (".stab", stab->name, 5) == 0
4236 && (!stab->name[5] ||
4237 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4238 && (stab->flags & SEC_MERGE) == 0
4239 && !bfd_is_abs_section (stab->output_section))
4241 struct bfd_elf_section_data *secdata;
4243 secdata = elf_section_data (stab);
4244 if (! _bfd_link_section_stabs (abfd,
4245 &hash_table->stab_info,
4246 stab, stabstr,
4247 &secdata->sec_info,
4248 &string_offset))
4249 goto error_return;
4250 if (secdata->sec_info)
4251 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4256 if (is_elf_hash_table (hash_table))
4258 /* Add this bfd to the loaded list. */
4259 struct elf_link_loaded_list *n;
4261 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4262 if (n == NULL)
4263 goto error_return;
4264 n->abfd = abfd;
4265 n->next = hash_table->loaded;
4266 hash_table->loaded = n;
4269 return TRUE;
4271 error_free_vers:
4272 if (nondeflt_vers != NULL)
4273 free (nondeflt_vers);
4274 if (extversym != NULL)
4275 free (extversym);
4276 error_free_sym:
4277 if (isymbuf != NULL)
4278 free (isymbuf);
4279 error_return:
4280 return FALSE;
4283 /* Return the linker hash table entry of a symbol that might be
4284 satisfied by an archive symbol. Return -1 on error. */
4286 struct elf_link_hash_entry *
4287 _bfd_elf_archive_symbol_lookup (bfd *abfd,
4288 struct bfd_link_info *info,
4289 const char *name)
4291 struct elf_link_hash_entry *h;
4292 char *p, *copy;
4293 size_t len, first;
4295 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4296 if (h != NULL)
4297 return h;
4299 /* If this is a default version (the name contains @@), look up the
4300 symbol again with only one `@' as well as without the version.
4301 The effect is that references to the symbol with and without the
4302 version will be matched by the default symbol in the archive. */
4304 p = strchr (name, ELF_VER_CHR);
4305 if (p == NULL || p[1] != ELF_VER_CHR)
4306 return h;
4308 /* First check with only one `@'. */
4309 len = strlen (name);
4310 copy = bfd_alloc (abfd, len);
4311 if (copy == NULL)
4312 return (struct elf_link_hash_entry *) 0 - 1;
4314 first = p - name + 1;
4315 memcpy (copy, name, first);
4316 memcpy (copy + first, name + first + 1, len - first);
4318 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
4319 if (h == NULL)
4321 /* We also need to check references to the symbol without the
4322 version. */
4323 copy[first - 1] = '\0';
4324 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4325 FALSE, FALSE, FALSE);
4328 bfd_release (abfd, copy);
4329 return h;
4332 /* Add symbols from an ELF archive file to the linker hash table. We
4333 don't use _bfd_generic_link_add_archive_symbols because of a
4334 problem which arises on UnixWare. The UnixWare libc.so is an
4335 archive which includes an entry libc.so.1 which defines a bunch of
4336 symbols. The libc.so archive also includes a number of other
4337 object files, which also define symbols, some of which are the same
4338 as those defined in libc.so.1. Correct linking requires that we
4339 consider each object file in turn, and include it if it defines any
4340 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4341 this; it looks through the list of undefined symbols, and includes
4342 any object file which defines them. When this algorithm is used on
4343 UnixWare, it winds up pulling in libc.so.1 early and defining a
4344 bunch of symbols. This means that some of the other objects in the
4345 archive are not included in the link, which is incorrect since they
4346 precede libc.so.1 in the archive.
4348 Fortunately, ELF archive handling is simpler than that done by
4349 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4350 oddities. In ELF, if we find a symbol in the archive map, and the
4351 symbol is currently undefined, we know that we must pull in that
4352 object file.
4354 Unfortunately, we do have to make multiple passes over the symbol
4355 table until nothing further is resolved. */
4357 static bfd_boolean
4358 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
4360 symindex c;
4361 bfd_boolean *defined = NULL;
4362 bfd_boolean *included = NULL;
4363 carsym *symdefs;
4364 bfd_boolean loop;
4365 bfd_size_type amt;
4366 const struct elf_backend_data *bed;
4367 struct elf_link_hash_entry * (*archive_symbol_lookup)
4368 (bfd *, struct bfd_link_info *, const char *);
4370 if (! bfd_has_map (abfd))
4372 /* An empty archive is a special case. */
4373 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4374 return TRUE;
4375 bfd_set_error (bfd_error_no_armap);
4376 return FALSE;
4379 /* Keep track of all symbols we know to be already defined, and all
4380 files we know to be already included. This is to speed up the
4381 second and subsequent passes. */
4382 c = bfd_ardata (abfd)->symdef_count;
4383 if (c == 0)
4384 return TRUE;
4385 amt = c;
4386 amt *= sizeof (bfd_boolean);
4387 defined = bfd_zmalloc (amt);
4388 included = bfd_zmalloc (amt);
4389 if (defined == NULL || included == NULL)
4390 goto error_return;
4392 symdefs = bfd_ardata (abfd)->symdefs;
4393 bed = get_elf_backend_data (abfd);
4394 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
4398 file_ptr last;
4399 symindex i;
4400 carsym *symdef;
4401 carsym *symdefend;
4403 loop = FALSE;
4404 last = -1;
4406 symdef = symdefs;
4407 symdefend = symdef + c;
4408 for (i = 0; symdef < symdefend; symdef++, i++)
4410 struct elf_link_hash_entry *h;
4411 bfd *element;
4412 struct bfd_link_hash_entry *undefs_tail;
4413 symindex mark;
4415 if (defined[i] || included[i])
4416 continue;
4417 if (symdef->file_offset == last)
4419 included[i] = TRUE;
4420 continue;
4423 h = archive_symbol_lookup (abfd, info, symdef->name);
4424 if (h == (struct elf_link_hash_entry *) 0 - 1)
4425 goto error_return;
4427 if (h == NULL)
4428 continue;
4430 if (h->root.type == bfd_link_hash_common)
4432 /* We currently have a common symbol. The archive map contains
4433 a reference to this symbol, so we may want to include it. We
4434 only want to include it however, if this archive element
4435 contains a definition of the symbol, not just another common
4436 declaration of it.
4438 Unfortunately some archivers (including GNU ar) will put
4439 declarations of common symbols into their archive maps, as
4440 well as real definitions, so we cannot just go by the archive
4441 map alone. Instead we must read in the element's symbol
4442 table and check that to see what kind of symbol definition
4443 this is. */
4444 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
4445 continue;
4447 else if (h->root.type != bfd_link_hash_undefined)
4449 if (h->root.type != bfd_link_hash_undefweak)
4450 defined[i] = TRUE;
4451 continue;
4454 /* We need to include this archive member. */
4455 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4456 if (element == NULL)
4457 goto error_return;
4459 if (! bfd_check_format (element, bfd_object))
4460 goto error_return;
4462 /* Doublecheck that we have not included this object
4463 already--it should be impossible, but there may be
4464 something wrong with the archive. */
4465 if (element->archive_pass != 0)
4467 bfd_set_error (bfd_error_bad_value);
4468 goto error_return;
4470 element->archive_pass = 1;
4472 undefs_tail = info->hash->undefs_tail;
4474 if (! (*info->callbacks->add_archive_element) (info, element,
4475 symdef->name))
4476 goto error_return;
4477 if (! bfd_link_add_symbols (element, info))
4478 goto error_return;
4480 /* If there are any new undefined symbols, we need to make
4481 another pass through the archive in order to see whether
4482 they can be defined. FIXME: This isn't perfect, because
4483 common symbols wind up on undefs_tail and because an
4484 undefined symbol which is defined later on in this pass
4485 does not require another pass. This isn't a bug, but it
4486 does make the code less efficient than it could be. */
4487 if (undefs_tail != info->hash->undefs_tail)
4488 loop = TRUE;
4490 /* Look backward to mark all symbols from this object file
4491 which we have already seen in this pass. */
4492 mark = i;
4495 included[mark] = TRUE;
4496 if (mark == 0)
4497 break;
4498 --mark;
4500 while (symdefs[mark].file_offset == symdef->file_offset);
4502 /* We mark subsequent symbols from this object file as we go
4503 on through the loop. */
4504 last = symdef->file_offset;
4507 while (loop);
4509 free (defined);
4510 free (included);
4512 return TRUE;
4514 error_return:
4515 if (defined != NULL)
4516 free (defined);
4517 if (included != NULL)
4518 free (included);
4519 return FALSE;
4522 /* Given an ELF BFD, add symbols to the global hash table as
4523 appropriate. */
4525 bfd_boolean
4526 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
4528 switch (bfd_get_format (abfd))
4530 case bfd_object:
4531 return elf_link_add_object_symbols (abfd, info);
4532 case bfd_archive:
4533 return elf_link_add_archive_symbols (abfd, info);
4534 default:
4535 bfd_set_error (bfd_error_wrong_format);
4536 return FALSE;
4540 /* This function will be called though elf_link_hash_traverse to store
4541 all hash value of the exported symbols in an array. */
4543 static bfd_boolean
4544 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
4546 unsigned long **valuep = data;
4547 const char *name;
4548 char *p;
4549 unsigned long ha;
4550 char *alc = NULL;
4552 if (h->root.type == bfd_link_hash_warning)
4553 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4555 /* Ignore indirect symbols. These are added by the versioning code. */
4556 if (h->dynindx == -1)
4557 return TRUE;
4559 name = h->root.root.string;
4560 p = strchr (name, ELF_VER_CHR);
4561 if (p != NULL)
4563 alc = bfd_malloc (p - name + 1);
4564 memcpy (alc, name, p - name);
4565 alc[p - name] = '\0';
4566 name = alc;
4569 /* Compute the hash value. */
4570 ha = bfd_elf_hash (name);
4572 /* Store the found hash value in the array given as the argument. */
4573 *(*valuep)++ = ha;
4575 /* And store it in the struct so that we can put it in the hash table
4576 later. */
4577 h->u.elf_hash_value = ha;
4579 if (alc != NULL)
4580 free (alc);
4582 return TRUE;
4585 /* Array used to determine the number of hash table buckets to use
4586 based on the number of symbols there are. If there are fewer than
4587 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4588 fewer than 37 we use 17 buckets, and so forth. We never use more
4589 than 32771 buckets. */
4591 static const size_t elf_buckets[] =
4593 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4594 16411, 32771, 0
4597 /* Compute bucket count for hashing table. We do not use a static set
4598 of possible tables sizes anymore. Instead we determine for all
4599 possible reasonable sizes of the table the outcome (i.e., the
4600 number of collisions etc) and choose the best solution. The
4601 weighting functions are not too simple to allow the table to grow
4602 without bounds. Instead one of the weighting factors is the size.
4603 Therefore the result is always a good payoff between few collisions
4604 (= short chain lengths) and table size. */
4605 static size_t
4606 compute_bucket_count (struct bfd_link_info *info)
4608 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
4609 size_t best_size = 0;
4610 unsigned long int *hashcodes;
4611 unsigned long int *hashcodesp;
4612 unsigned long int i;
4613 bfd_size_type amt;
4615 /* Compute the hash values for all exported symbols. At the same
4616 time store the values in an array so that we could use them for
4617 optimizations. */
4618 amt = dynsymcount;
4619 amt *= sizeof (unsigned long int);
4620 hashcodes = bfd_malloc (amt);
4621 if (hashcodes == NULL)
4622 return 0;
4623 hashcodesp = hashcodes;
4625 /* Put all hash values in HASHCODES. */
4626 elf_link_hash_traverse (elf_hash_table (info),
4627 elf_collect_hash_codes, &hashcodesp);
4629 /* We have a problem here. The following code to optimize the table
4630 size requires an integer type with more the 32 bits. If
4631 BFD_HOST_U_64_BIT is set we know about such a type. */
4632 #ifdef BFD_HOST_U_64_BIT
4633 if (info->optimize)
4635 unsigned long int nsyms = hashcodesp - hashcodes;
4636 size_t minsize;
4637 size_t maxsize;
4638 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
4639 unsigned long int *counts ;
4640 bfd *dynobj = elf_hash_table (info)->dynobj;
4641 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
4643 /* Possible optimization parameters: if we have NSYMS symbols we say
4644 that the hashing table must at least have NSYMS/4 and at most
4645 2*NSYMS buckets. */
4646 minsize = nsyms / 4;
4647 if (minsize == 0)
4648 minsize = 1;
4649 best_size = maxsize = nsyms * 2;
4651 /* Create array where we count the collisions in. We must use bfd_malloc
4652 since the size could be large. */
4653 amt = maxsize;
4654 amt *= sizeof (unsigned long int);
4655 counts = bfd_malloc (amt);
4656 if (counts == NULL)
4658 free (hashcodes);
4659 return 0;
4662 /* Compute the "optimal" size for the hash table. The criteria is a
4663 minimal chain length. The minor criteria is (of course) the size
4664 of the table. */
4665 for (i = minsize; i < maxsize; ++i)
4667 /* Walk through the array of hashcodes and count the collisions. */
4668 BFD_HOST_U_64_BIT max;
4669 unsigned long int j;
4670 unsigned long int fact;
4672 memset (counts, '\0', i * sizeof (unsigned long int));
4674 /* Determine how often each hash bucket is used. */
4675 for (j = 0; j < nsyms; ++j)
4676 ++counts[hashcodes[j] % i];
4678 /* For the weight function we need some information about the
4679 pagesize on the target. This is information need not be 100%
4680 accurate. Since this information is not available (so far) we
4681 define it here to a reasonable default value. If it is crucial
4682 to have a better value some day simply define this value. */
4683 # ifndef BFD_TARGET_PAGESIZE
4684 # define BFD_TARGET_PAGESIZE (4096)
4685 # endif
4687 /* We in any case need 2 + NSYMS entries for the size values and
4688 the chains. */
4689 max = (2 + nsyms) * (bed->s->arch_size / 8);
4691 # if 1
4692 /* Variant 1: optimize for short chains. We add the squares
4693 of all the chain lengths (which favors many small chain
4694 over a few long chains). */
4695 for (j = 0; j < i; ++j)
4696 max += counts[j] * counts[j];
4698 /* This adds penalties for the overall size of the table. */
4699 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4700 max *= fact * fact;
4701 # else
4702 /* Variant 2: Optimize a lot more for small table. Here we
4703 also add squares of the size but we also add penalties for
4704 empty slots (the +1 term). */
4705 for (j = 0; j < i; ++j)
4706 max += (1 + counts[j]) * (1 + counts[j]);
4708 /* The overall size of the table is considered, but not as
4709 strong as in variant 1, where it is squared. */
4710 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4711 max *= fact;
4712 # endif
4714 /* Compare with current best results. */
4715 if (max < best_chlen)
4717 best_chlen = max;
4718 best_size = i;
4722 free (counts);
4724 else
4725 #endif /* defined (BFD_HOST_U_64_BIT) */
4727 /* This is the fallback solution if no 64bit type is available or if we
4728 are not supposed to spend much time on optimizations. We select the
4729 bucket count using a fixed set of numbers. */
4730 for (i = 0; elf_buckets[i] != 0; i++)
4732 best_size = elf_buckets[i];
4733 if (dynsymcount < elf_buckets[i + 1])
4734 break;
4738 /* Free the arrays we needed. */
4739 free (hashcodes);
4741 return best_size;
4744 /* Set up the sizes and contents of the ELF dynamic sections. This is
4745 called by the ELF linker emulation before_allocation routine. We
4746 must set the sizes of the sections before the linker sets the
4747 addresses of the various sections. */
4749 bfd_boolean
4750 bfd_elf_size_dynamic_sections (bfd *output_bfd,
4751 const char *soname,
4752 const char *rpath,
4753 const char *filter_shlib,
4754 const char * const *auxiliary_filters,
4755 struct bfd_link_info *info,
4756 asection **sinterpptr,
4757 struct bfd_elf_version_tree *verdefs)
4759 bfd_size_type soname_indx;
4760 bfd *dynobj;
4761 const struct elf_backend_data *bed;
4762 struct elf_assign_sym_version_info asvinfo;
4764 *sinterpptr = NULL;
4766 soname_indx = (bfd_size_type) -1;
4768 if (!is_elf_hash_table (info->hash))
4769 return TRUE;
4771 elf_tdata (output_bfd)->relro = info->relro;
4772 if (info->execstack)
4773 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
4774 else if (info->noexecstack)
4775 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
4776 else
4778 bfd *inputobj;
4779 asection *notesec = NULL;
4780 int exec = 0;
4782 for (inputobj = info->input_bfds;
4783 inputobj;
4784 inputobj = inputobj->link_next)
4786 asection *s;
4788 if (inputobj->flags & DYNAMIC)
4789 continue;
4790 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
4791 if (s)
4793 if (s->flags & SEC_CODE)
4794 exec = PF_X;
4795 notesec = s;
4797 else
4798 exec = PF_X;
4800 if (notesec)
4802 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
4803 if (exec && info->relocatable
4804 && notesec->output_section != bfd_abs_section_ptr)
4805 notesec->output_section->flags |= SEC_CODE;
4809 /* Any syms created from now on start with -1 in
4810 got.refcount/offset and plt.refcount/offset. */
4811 elf_hash_table (info)->init_refcount = elf_hash_table (info)->init_offset;
4813 /* The backend may have to create some sections regardless of whether
4814 we're dynamic or not. */
4815 bed = get_elf_backend_data (output_bfd);
4816 if (bed->elf_backend_always_size_sections
4817 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
4818 return FALSE;
4820 dynobj = elf_hash_table (info)->dynobj;
4822 /* If there were no dynamic objects in the link, there is nothing to
4823 do here. */
4824 if (dynobj == NULL)
4825 return TRUE;
4827 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
4828 return FALSE;
4830 if (elf_hash_table (info)->dynamic_sections_created)
4832 struct elf_info_failed eif;
4833 struct elf_link_hash_entry *h;
4834 asection *dynstr;
4835 struct bfd_elf_version_tree *t;
4836 struct bfd_elf_version_expr *d;
4837 bfd_boolean all_defined;
4839 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
4840 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
4842 if (soname != NULL)
4844 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
4845 soname, TRUE);
4846 if (soname_indx == (bfd_size_type) -1
4847 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
4848 return FALSE;
4851 if (info->symbolic)
4853 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
4854 return FALSE;
4855 info->flags |= DF_SYMBOLIC;
4858 if (rpath != NULL)
4860 bfd_size_type indx;
4862 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
4863 TRUE);
4864 if (indx == (bfd_size_type) -1
4865 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
4866 return FALSE;
4868 if (info->new_dtags)
4870 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
4871 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
4872 return FALSE;
4876 if (filter_shlib != NULL)
4878 bfd_size_type indx;
4880 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
4881 filter_shlib, TRUE);
4882 if (indx == (bfd_size_type) -1
4883 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
4884 return FALSE;
4887 if (auxiliary_filters != NULL)
4889 const char * const *p;
4891 for (p = auxiliary_filters; *p != NULL; p++)
4893 bfd_size_type indx;
4895 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
4896 *p, TRUE);
4897 if (indx == (bfd_size_type) -1
4898 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
4899 return FALSE;
4903 eif.info = info;
4904 eif.verdefs = verdefs;
4905 eif.failed = FALSE;
4907 /* If we are supposed to export all symbols into the dynamic symbol
4908 table (this is not the normal case), then do so. */
4909 if (info->export_dynamic)
4911 elf_link_hash_traverse (elf_hash_table (info),
4912 _bfd_elf_export_symbol,
4913 &eif);
4914 if (eif.failed)
4915 return FALSE;
4918 /* Make all global versions with definition. */
4919 for (t = verdefs; t != NULL; t = t->next)
4920 for (d = t->globals.list; d != NULL; d = d->next)
4921 if (!d->symver && d->symbol)
4923 const char *verstr, *name;
4924 size_t namelen, verlen, newlen;
4925 char *newname, *p;
4926 struct elf_link_hash_entry *newh;
4928 name = d->symbol;
4929 namelen = strlen (name);
4930 verstr = t->name;
4931 verlen = strlen (verstr);
4932 newlen = namelen + verlen + 3;
4934 newname = bfd_malloc (newlen);
4935 if (newname == NULL)
4936 return FALSE;
4937 memcpy (newname, name, namelen);
4939 /* Check the hidden versioned definition. */
4940 p = newname + namelen;
4941 *p++ = ELF_VER_CHR;
4942 memcpy (p, verstr, verlen + 1);
4943 newh = elf_link_hash_lookup (elf_hash_table (info),
4944 newname, FALSE, FALSE,
4945 FALSE);
4946 if (newh == NULL
4947 || (newh->root.type != bfd_link_hash_defined
4948 && newh->root.type != bfd_link_hash_defweak))
4950 /* Check the default versioned definition. */
4951 *p++ = ELF_VER_CHR;
4952 memcpy (p, verstr, verlen + 1);
4953 newh = elf_link_hash_lookup (elf_hash_table (info),
4954 newname, FALSE, FALSE,
4955 FALSE);
4957 free (newname);
4959 /* Mark this version if there is a definition and it is
4960 not defined in a shared object. */
4961 if (newh != NULL
4962 && !newh->def_dynamic
4963 && (newh->root.type == bfd_link_hash_defined
4964 || newh->root.type == bfd_link_hash_defweak))
4965 d->symver = 1;
4968 /* Attach all the symbols to their version information. */
4969 asvinfo.output_bfd = output_bfd;
4970 asvinfo.info = info;
4971 asvinfo.verdefs = verdefs;
4972 asvinfo.failed = FALSE;
4974 elf_link_hash_traverse (elf_hash_table (info),
4975 _bfd_elf_link_assign_sym_version,
4976 &asvinfo);
4977 if (asvinfo.failed)
4978 return FALSE;
4980 if (!info->allow_undefined_version)
4982 /* Check if all global versions have a definition. */
4983 all_defined = TRUE;
4984 for (t = verdefs; t != NULL; t = t->next)
4985 for (d = t->globals.list; d != NULL; d = d->next)
4986 if (!d->symver && !d->script)
4988 (*_bfd_error_handler)
4989 (_("%s: undefined version: %s"),
4990 d->pattern, t->name);
4991 all_defined = FALSE;
4994 if (!all_defined)
4996 bfd_set_error (bfd_error_bad_value);
4997 return FALSE;
5001 /* Find all symbols which were defined in a dynamic object and make
5002 the backend pick a reasonable value for them. */
5003 elf_link_hash_traverse (elf_hash_table (info),
5004 _bfd_elf_adjust_dynamic_symbol,
5005 &eif);
5006 if (eif.failed)
5007 return FALSE;
5009 /* Add some entries to the .dynamic section. We fill in some of the
5010 values later, in bfd_elf_final_link, but we must add the entries
5011 now so that we know the final size of the .dynamic section. */
5013 /* If there are initialization and/or finalization functions to
5014 call then add the corresponding DT_INIT/DT_FINI entries. */
5015 h = (info->init_function
5016 ? elf_link_hash_lookup (elf_hash_table (info),
5017 info->init_function, FALSE,
5018 FALSE, FALSE)
5019 : NULL);
5020 if (h != NULL
5021 && (h->ref_regular
5022 || h->def_regular))
5024 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5025 return FALSE;
5027 h = (info->fini_function
5028 ? elf_link_hash_lookup (elf_hash_table (info),
5029 info->fini_function, FALSE,
5030 FALSE, FALSE)
5031 : NULL);
5032 if (h != NULL
5033 && (h->ref_regular
5034 || h->def_regular))
5036 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5037 return FALSE;
5040 if (bfd_get_section_by_name (output_bfd, ".preinit_array") != NULL)
5042 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5043 if (! info->executable)
5045 bfd *sub;
5046 asection *o;
5048 for (sub = info->input_bfds; sub != NULL;
5049 sub = sub->link_next)
5050 for (o = sub->sections; o != NULL; o = o->next)
5051 if (elf_section_data (o)->this_hdr.sh_type
5052 == SHT_PREINIT_ARRAY)
5054 (*_bfd_error_handler)
5055 (_("%B: .preinit_array section is not allowed in DSO"),
5056 sub);
5057 break;
5060 bfd_set_error (bfd_error_nonrepresentable_section);
5061 return FALSE;
5064 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5065 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5066 return FALSE;
5068 if (bfd_get_section_by_name (output_bfd, ".init_array") != NULL)
5070 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5071 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5072 return FALSE;
5074 if (bfd_get_section_by_name (output_bfd, ".fini_array") != NULL)
5076 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5077 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5078 return FALSE;
5081 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
5082 /* If .dynstr is excluded from the link, we don't want any of
5083 these tags. Strictly, we should be checking each section
5084 individually; This quick check covers for the case where
5085 someone does a /DISCARD/ : { *(*) }. */
5086 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5088 bfd_size_type strsize;
5090 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5091 if (!_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)
5092 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5093 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5094 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5095 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5096 bed->s->sizeof_sym))
5097 return FALSE;
5101 /* The backend must work out the sizes of all the other dynamic
5102 sections. */
5103 if (bed->elf_backend_size_dynamic_sections
5104 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5105 return FALSE;
5107 if (elf_hash_table (info)->dynamic_sections_created)
5109 bfd_size_type dynsymcount;
5110 asection *s;
5111 size_t bucketcount = 0;
5112 size_t hash_entry_size;
5113 unsigned int dtagcount;
5115 /* Set up the version definition section. */
5116 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5117 BFD_ASSERT (s != NULL);
5119 /* We may have created additional version definitions if we are
5120 just linking a regular application. */
5121 verdefs = asvinfo.verdefs;
5123 /* Skip anonymous version tag. */
5124 if (verdefs != NULL && verdefs->vernum == 0)
5125 verdefs = verdefs->next;
5127 if (verdefs == NULL && !info->create_default_symver)
5128 _bfd_strip_section_from_output (info, s);
5129 else
5131 unsigned int cdefs;
5132 bfd_size_type size;
5133 struct bfd_elf_version_tree *t;
5134 bfd_byte *p;
5135 Elf_Internal_Verdef def;
5136 Elf_Internal_Verdaux defaux;
5137 struct bfd_link_hash_entry *bh;
5138 struct elf_link_hash_entry *h;
5139 const char *name;
5141 cdefs = 0;
5142 size = 0;
5144 /* Make space for the base version. */
5145 size += sizeof (Elf_External_Verdef);
5146 size += sizeof (Elf_External_Verdaux);
5147 ++cdefs;
5149 /* Make space for the default version. */
5150 if (info->create_default_symver)
5152 size += sizeof (Elf_External_Verdef);
5153 ++cdefs;
5156 for (t = verdefs; t != NULL; t = t->next)
5158 struct bfd_elf_version_deps *n;
5160 size += sizeof (Elf_External_Verdef);
5161 size += sizeof (Elf_External_Verdaux);
5162 ++cdefs;
5164 for (n = t->deps; n != NULL; n = n->next)
5165 size += sizeof (Elf_External_Verdaux);
5168 s->size = size;
5169 s->contents = bfd_alloc (output_bfd, s->size);
5170 if (s->contents == NULL && s->size != 0)
5171 return FALSE;
5173 /* Fill in the version definition section. */
5175 p = s->contents;
5177 def.vd_version = VER_DEF_CURRENT;
5178 def.vd_flags = VER_FLG_BASE;
5179 def.vd_ndx = 1;
5180 def.vd_cnt = 1;
5181 if (info->create_default_symver)
5183 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
5184 def.vd_next = sizeof (Elf_External_Verdef);
5186 else
5188 def.vd_aux = sizeof (Elf_External_Verdef);
5189 def.vd_next = (sizeof (Elf_External_Verdef)
5190 + sizeof (Elf_External_Verdaux));
5193 if (soname_indx != (bfd_size_type) -1)
5195 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5196 soname_indx);
5197 def.vd_hash = bfd_elf_hash (soname);
5198 defaux.vda_name = soname_indx;
5199 name = soname;
5201 else
5203 bfd_size_type indx;
5205 name = basename (output_bfd->filename);
5206 def.vd_hash = bfd_elf_hash (name);
5207 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5208 name, FALSE);
5209 if (indx == (bfd_size_type) -1)
5210 return FALSE;
5211 defaux.vda_name = indx;
5213 defaux.vda_next = 0;
5215 _bfd_elf_swap_verdef_out (output_bfd, &def,
5216 (Elf_External_Verdef *) p);
5217 p += sizeof (Elf_External_Verdef);
5218 if (info->create_default_symver)
5220 /* Add a symbol representing this version. */
5221 bh = NULL;
5222 if (! (_bfd_generic_link_add_one_symbol
5223 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
5224 0, NULL, FALSE,
5225 get_elf_backend_data (dynobj)->collect, &bh)))
5226 return FALSE;
5227 h = (struct elf_link_hash_entry *) bh;
5228 h->non_elf = 0;
5229 h->def_regular = 1;
5230 h->type = STT_OBJECT;
5231 h->verinfo.vertree = NULL;
5233 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5234 return FALSE;
5236 /* Create a duplicate of the base version with the same
5237 aux block, but different flags. */
5238 def.vd_flags = 0;
5239 def.vd_ndx = 2;
5240 def.vd_aux = sizeof (Elf_External_Verdef);
5241 if (verdefs)
5242 def.vd_next = (sizeof (Elf_External_Verdef)
5243 + sizeof (Elf_External_Verdaux));
5244 else
5245 def.vd_next = 0;
5246 _bfd_elf_swap_verdef_out (output_bfd, &def,
5247 (Elf_External_Verdef *) p);
5248 p += sizeof (Elf_External_Verdef);
5250 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5251 (Elf_External_Verdaux *) p);
5252 p += sizeof (Elf_External_Verdaux);
5254 for (t = verdefs; t != NULL; t = t->next)
5256 unsigned int cdeps;
5257 struct bfd_elf_version_deps *n;
5259 cdeps = 0;
5260 for (n = t->deps; n != NULL; n = n->next)
5261 ++cdeps;
5263 /* Add a symbol representing this version. */
5264 bh = NULL;
5265 if (! (_bfd_generic_link_add_one_symbol
5266 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5267 0, NULL, FALSE,
5268 get_elf_backend_data (dynobj)->collect, &bh)))
5269 return FALSE;
5270 h = (struct elf_link_hash_entry *) bh;
5271 h->non_elf = 0;
5272 h->def_regular = 1;
5273 h->type = STT_OBJECT;
5274 h->verinfo.vertree = t;
5276 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5277 return FALSE;
5279 def.vd_version = VER_DEF_CURRENT;
5280 def.vd_flags = 0;
5281 if (t->globals.list == NULL
5282 && t->locals.list == NULL
5283 && ! t->used)
5284 def.vd_flags |= VER_FLG_WEAK;
5285 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5286 def.vd_cnt = cdeps + 1;
5287 def.vd_hash = bfd_elf_hash (t->name);
5288 def.vd_aux = sizeof (Elf_External_Verdef);
5289 def.vd_next = 0;
5290 if (t->next != NULL)
5291 def.vd_next = (sizeof (Elf_External_Verdef)
5292 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5294 _bfd_elf_swap_verdef_out (output_bfd, &def,
5295 (Elf_External_Verdef *) p);
5296 p += sizeof (Elf_External_Verdef);
5298 defaux.vda_name = h->dynstr_index;
5299 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5300 h->dynstr_index);
5301 defaux.vda_next = 0;
5302 if (t->deps != NULL)
5303 defaux.vda_next = sizeof (Elf_External_Verdaux);
5304 t->name_indx = defaux.vda_name;
5306 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5307 (Elf_External_Verdaux *) p);
5308 p += sizeof (Elf_External_Verdaux);
5310 for (n = t->deps; n != NULL; n = n->next)
5312 if (n->version_needed == NULL)
5314 /* This can happen if there was an error in the
5315 version script. */
5316 defaux.vda_name = 0;
5318 else
5320 defaux.vda_name = n->version_needed->name_indx;
5321 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5322 defaux.vda_name);
5324 if (n->next == NULL)
5325 defaux.vda_next = 0;
5326 else
5327 defaux.vda_next = sizeof (Elf_External_Verdaux);
5329 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5330 (Elf_External_Verdaux *) p);
5331 p += sizeof (Elf_External_Verdaux);
5335 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
5336 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
5337 return FALSE;
5339 elf_tdata (output_bfd)->cverdefs = cdefs;
5342 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
5344 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
5345 return FALSE;
5347 else if (info->flags & DF_BIND_NOW)
5349 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
5350 return FALSE;
5353 if (info->flags_1)
5355 if (info->executable)
5356 info->flags_1 &= ~ (DF_1_INITFIRST
5357 | DF_1_NODELETE
5358 | DF_1_NOOPEN);
5359 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
5360 return FALSE;
5363 /* Work out the size of the version reference section. */
5365 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
5366 BFD_ASSERT (s != NULL);
5368 struct elf_find_verdep_info sinfo;
5370 sinfo.output_bfd = output_bfd;
5371 sinfo.info = info;
5372 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
5373 if (sinfo.vers == 0)
5374 sinfo.vers = 1;
5375 sinfo.failed = FALSE;
5377 elf_link_hash_traverse (elf_hash_table (info),
5378 _bfd_elf_link_find_version_dependencies,
5379 &sinfo);
5381 if (elf_tdata (output_bfd)->verref == NULL)
5382 _bfd_strip_section_from_output (info, s);
5383 else
5385 Elf_Internal_Verneed *t;
5386 unsigned int size;
5387 unsigned int crefs;
5388 bfd_byte *p;
5390 /* Build the version definition section. */
5391 size = 0;
5392 crefs = 0;
5393 for (t = elf_tdata (output_bfd)->verref;
5394 t != NULL;
5395 t = t->vn_nextref)
5397 Elf_Internal_Vernaux *a;
5399 size += sizeof (Elf_External_Verneed);
5400 ++crefs;
5401 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5402 size += sizeof (Elf_External_Vernaux);
5405 s->size = size;
5406 s->contents = bfd_alloc (output_bfd, s->size);
5407 if (s->contents == NULL)
5408 return FALSE;
5410 p = s->contents;
5411 for (t = elf_tdata (output_bfd)->verref;
5412 t != NULL;
5413 t = t->vn_nextref)
5415 unsigned int caux;
5416 Elf_Internal_Vernaux *a;
5417 bfd_size_type indx;
5419 caux = 0;
5420 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5421 ++caux;
5423 t->vn_version = VER_NEED_CURRENT;
5424 t->vn_cnt = caux;
5425 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5426 elf_dt_name (t->vn_bfd) != NULL
5427 ? elf_dt_name (t->vn_bfd)
5428 : basename (t->vn_bfd->filename),
5429 FALSE);
5430 if (indx == (bfd_size_type) -1)
5431 return FALSE;
5432 t->vn_file = indx;
5433 t->vn_aux = sizeof (Elf_External_Verneed);
5434 if (t->vn_nextref == NULL)
5435 t->vn_next = 0;
5436 else
5437 t->vn_next = (sizeof (Elf_External_Verneed)
5438 + caux * sizeof (Elf_External_Vernaux));
5440 _bfd_elf_swap_verneed_out (output_bfd, t,
5441 (Elf_External_Verneed *) p);
5442 p += sizeof (Elf_External_Verneed);
5444 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5446 a->vna_hash = bfd_elf_hash (a->vna_nodename);
5447 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5448 a->vna_nodename, FALSE);
5449 if (indx == (bfd_size_type) -1)
5450 return FALSE;
5451 a->vna_name = indx;
5452 if (a->vna_nextptr == NULL)
5453 a->vna_next = 0;
5454 else
5455 a->vna_next = sizeof (Elf_External_Vernaux);
5457 _bfd_elf_swap_vernaux_out (output_bfd, a,
5458 (Elf_External_Vernaux *) p);
5459 p += sizeof (Elf_External_Vernaux);
5463 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
5464 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
5465 return FALSE;
5467 elf_tdata (output_bfd)->cverrefs = crefs;
5471 /* Assign dynsym indicies. In a shared library we generate a
5472 section symbol for each output section, which come first.
5473 Next come all of the back-end allocated local dynamic syms,
5474 followed by the rest of the global symbols. */
5476 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
5478 /* Work out the size of the symbol version section. */
5479 s = bfd_get_section_by_name (dynobj, ".gnu.version");
5480 BFD_ASSERT (s != NULL);
5481 if (dynsymcount == 0
5482 || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL
5483 && !info->create_default_symver))
5485 _bfd_strip_section_from_output (info, s);
5486 /* The DYNSYMCOUNT might have changed if we were going to
5487 output a dynamic symbol table entry for S. */
5488 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
5490 else
5492 s->size = dynsymcount * sizeof (Elf_External_Versym);
5493 s->contents = bfd_zalloc (output_bfd, s->size);
5494 if (s->contents == NULL)
5495 return FALSE;
5497 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
5498 return FALSE;
5501 /* Set the size of the .dynsym and .hash sections. We counted
5502 the number of dynamic symbols in elf_link_add_object_symbols.
5503 We will build the contents of .dynsym and .hash when we build
5504 the final symbol table, because until then we do not know the
5505 correct value to give the symbols. We built the .dynstr
5506 section as we went along in elf_link_add_object_symbols. */
5507 s = bfd_get_section_by_name (dynobj, ".dynsym");
5508 BFD_ASSERT (s != NULL);
5509 s->size = dynsymcount * bed->s->sizeof_sym;
5510 s->contents = bfd_alloc (output_bfd, s->size);
5511 if (s->contents == NULL && s->size != 0)
5512 return FALSE;
5514 if (dynsymcount != 0)
5516 Elf_Internal_Sym isym;
5518 /* The first entry in .dynsym is a dummy symbol. */
5519 isym.st_value = 0;
5520 isym.st_size = 0;
5521 isym.st_name = 0;
5522 isym.st_info = 0;
5523 isym.st_other = 0;
5524 isym.st_shndx = 0;
5525 bed->s->swap_symbol_out (output_bfd, &isym, s->contents, 0);
5528 /* Compute the size of the hashing table. As a side effect this
5529 computes the hash values for all the names we export. */
5530 bucketcount = compute_bucket_count (info);
5532 s = bfd_get_section_by_name (dynobj, ".hash");
5533 BFD_ASSERT (s != NULL);
5534 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
5535 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
5536 s->contents = bfd_zalloc (output_bfd, s->size);
5537 if (s->contents == NULL)
5538 return FALSE;
5540 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
5541 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
5542 s->contents + hash_entry_size);
5544 elf_hash_table (info)->bucketcount = bucketcount;
5546 s = bfd_get_section_by_name (dynobj, ".dynstr");
5547 BFD_ASSERT (s != NULL);
5549 elf_finalize_dynstr (output_bfd, info);
5551 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5553 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
5554 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
5555 return FALSE;
5558 return TRUE;
5561 /* Final phase of ELF linker. */
5563 /* A structure we use to avoid passing large numbers of arguments. */
5565 struct elf_final_link_info
5567 /* General link information. */
5568 struct bfd_link_info *info;
5569 /* Output BFD. */
5570 bfd *output_bfd;
5571 /* Symbol string table. */
5572 struct bfd_strtab_hash *symstrtab;
5573 /* .dynsym section. */
5574 asection *dynsym_sec;
5575 /* .hash section. */
5576 asection *hash_sec;
5577 /* symbol version section (.gnu.version). */
5578 asection *symver_sec;
5579 /* Buffer large enough to hold contents of any section. */
5580 bfd_byte *contents;
5581 /* Buffer large enough to hold external relocs of any section. */
5582 void *external_relocs;
5583 /* Buffer large enough to hold internal relocs of any section. */
5584 Elf_Internal_Rela *internal_relocs;
5585 /* Buffer large enough to hold external local symbols of any input
5586 BFD. */
5587 bfd_byte *external_syms;
5588 /* And a buffer for symbol section indices. */
5589 Elf_External_Sym_Shndx *locsym_shndx;
5590 /* Buffer large enough to hold internal local symbols of any input
5591 BFD. */
5592 Elf_Internal_Sym *internal_syms;
5593 /* Array large enough to hold a symbol index for each local symbol
5594 of any input BFD. */
5595 long *indices;
5596 /* Array large enough to hold a section pointer for each local
5597 symbol of any input BFD. */
5598 asection **sections;
5599 /* Buffer to hold swapped out symbols. */
5600 bfd_byte *symbuf;
5601 /* And one for symbol section indices. */
5602 Elf_External_Sym_Shndx *symshndxbuf;
5603 /* Number of swapped out symbols in buffer. */
5604 size_t symbuf_count;
5605 /* Number of symbols which fit in symbuf. */
5606 size_t symbuf_size;
5607 /* And same for symshndxbuf. */
5608 size_t shndxbuf_size;
5611 /* This struct is used to pass information to elf_link_output_extsym. */
5613 struct elf_outext_info
5615 bfd_boolean failed;
5616 bfd_boolean localsyms;
5617 struct elf_final_link_info *finfo;
5620 /* When performing a relocatable link, the input relocations are
5621 preserved. But, if they reference global symbols, the indices
5622 referenced must be updated. Update all the relocations in
5623 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5625 static void
5626 elf_link_adjust_relocs (bfd *abfd,
5627 Elf_Internal_Shdr *rel_hdr,
5628 unsigned int count,
5629 struct elf_link_hash_entry **rel_hash)
5631 unsigned int i;
5632 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5633 bfd_byte *erela;
5634 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5635 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5636 bfd_vma r_type_mask;
5637 int r_sym_shift;
5639 if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
5641 swap_in = bed->s->swap_reloc_in;
5642 swap_out = bed->s->swap_reloc_out;
5644 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
5646 swap_in = bed->s->swap_reloca_in;
5647 swap_out = bed->s->swap_reloca_out;
5649 else
5650 abort ();
5652 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
5653 abort ();
5655 if (bed->s->arch_size == 32)
5657 r_type_mask = 0xff;
5658 r_sym_shift = 8;
5660 else
5662 r_type_mask = 0xffffffff;
5663 r_sym_shift = 32;
5666 erela = rel_hdr->contents;
5667 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
5669 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
5670 unsigned int j;
5672 if (*rel_hash == NULL)
5673 continue;
5675 BFD_ASSERT ((*rel_hash)->indx >= 0);
5677 (*swap_in) (abfd, erela, irela);
5678 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
5679 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
5680 | (irela[j].r_info & r_type_mask));
5681 (*swap_out) (abfd, irela, erela);
5685 struct elf_link_sort_rela
5687 union {
5688 bfd_vma offset;
5689 bfd_vma sym_mask;
5690 } u;
5691 enum elf_reloc_type_class type;
5692 /* We use this as an array of size int_rels_per_ext_rel. */
5693 Elf_Internal_Rela rela[1];
5696 static int
5697 elf_link_sort_cmp1 (const void *A, const void *B)
5699 const struct elf_link_sort_rela *a = A;
5700 const struct elf_link_sort_rela *b = B;
5701 int relativea, relativeb;
5703 relativea = a->type == reloc_class_relative;
5704 relativeb = b->type == reloc_class_relative;
5706 if (relativea < relativeb)
5707 return 1;
5708 if (relativea > relativeb)
5709 return -1;
5710 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
5711 return -1;
5712 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
5713 return 1;
5714 if (a->rela->r_offset < b->rela->r_offset)
5715 return -1;
5716 if (a->rela->r_offset > b->rela->r_offset)
5717 return 1;
5718 return 0;
5721 static int
5722 elf_link_sort_cmp2 (const void *A, const void *B)
5724 const struct elf_link_sort_rela *a = A;
5725 const struct elf_link_sort_rela *b = B;
5726 int copya, copyb;
5728 if (a->u.offset < b->u.offset)
5729 return -1;
5730 if (a->u.offset > b->u.offset)
5731 return 1;
5732 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
5733 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
5734 if (copya < copyb)
5735 return -1;
5736 if (copya > copyb)
5737 return 1;
5738 if (a->rela->r_offset < b->rela->r_offset)
5739 return -1;
5740 if (a->rela->r_offset > b->rela->r_offset)
5741 return 1;
5742 return 0;
5745 static size_t
5746 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
5748 asection *reldyn;
5749 bfd_size_type count, size;
5750 size_t i, ret, sort_elt, ext_size;
5751 bfd_byte *sort, *s_non_relative, *p;
5752 struct elf_link_sort_rela *sq;
5753 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5754 int i2e = bed->s->int_rels_per_ext_rel;
5755 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5756 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5757 struct bfd_link_order *lo;
5758 bfd_vma r_sym_mask;
5760 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
5761 if (reldyn == NULL || reldyn->size == 0)
5763 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
5764 if (reldyn == NULL || reldyn->size == 0)
5765 return 0;
5766 ext_size = bed->s->sizeof_rel;
5767 swap_in = bed->s->swap_reloc_in;
5768 swap_out = bed->s->swap_reloc_out;
5770 else
5772 ext_size = bed->s->sizeof_rela;
5773 swap_in = bed->s->swap_reloca_in;
5774 swap_out = bed->s->swap_reloca_out;
5776 count = reldyn->size / ext_size;
5778 size = 0;
5779 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
5780 if (lo->type == bfd_indirect_link_order)
5782 asection *o = lo->u.indirect.section;
5783 size += o->size;
5786 if (size != reldyn->size)
5787 return 0;
5789 sort_elt = (sizeof (struct elf_link_sort_rela)
5790 + (i2e - 1) * sizeof (Elf_Internal_Rela));
5791 sort = bfd_zmalloc (sort_elt * count);
5792 if (sort == NULL)
5794 (*info->callbacks->warning)
5795 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
5796 return 0;
5799 if (bed->s->arch_size == 32)
5800 r_sym_mask = ~(bfd_vma) 0xff;
5801 else
5802 r_sym_mask = ~(bfd_vma) 0xffffffff;
5804 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
5805 if (lo->type == bfd_indirect_link_order)
5807 bfd_byte *erel, *erelend;
5808 asection *o = lo->u.indirect.section;
5810 if (o->contents == NULL && o->size != 0)
5812 /* This is a reloc section that is being handled as a normal
5813 section. See bfd_section_from_shdr. We can't combine
5814 relocs in this case. */
5815 free (sort);
5816 return 0;
5818 erel = o->contents;
5819 erelend = o->contents + o->size;
5820 p = sort + o->output_offset / ext_size * sort_elt;
5821 while (erel < erelend)
5823 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
5824 (*swap_in) (abfd, erel, s->rela);
5825 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
5826 s->u.sym_mask = r_sym_mask;
5827 p += sort_elt;
5828 erel += ext_size;
5832 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
5834 for (i = 0, p = sort; i < count; i++, p += sort_elt)
5836 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
5837 if (s->type != reloc_class_relative)
5838 break;
5840 ret = i;
5841 s_non_relative = p;
5843 sq = (struct elf_link_sort_rela *) s_non_relative;
5844 for (; i < count; i++, p += sort_elt)
5846 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
5847 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
5848 sq = sp;
5849 sp->u.offset = sq->rela->r_offset;
5852 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
5854 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
5855 if (lo->type == bfd_indirect_link_order)
5857 bfd_byte *erel, *erelend;
5858 asection *o = lo->u.indirect.section;
5860 erel = o->contents;
5861 erelend = o->contents + o->size;
5862 p = sort + o->output_offset / ext_size * sort_elt;
5863 while (erel < erelend)
5865 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
5866 (*swap_out) (abfd, s->rela, erel);
5867 p += sort_elt;
5868 erel += ext_size;
5872 free (sort);
5873 *psec = reldyn;
5874 return ret;
5877 /* Flush the output symbols to the file. */
5879 static bfd_boolean
5880 elf_link_flush_output_syms (struct elf_final_link_info *finfo,
5881 const struct elf_backend_data *bed)
5883 if (finfo->symbuf_count > 0)
5885 Elf_Internal_Shdr *hdr;
5886 file_ptr pos;
5887 bfd_size_type amt;
5889 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
5890 pos = hdr->sh_offset + hdr->sh_size;
5891 amt = finfo->symbuf_count * bed->s->sizeof_sym;
5892 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
5893 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
5894 return FALSE;
5896 hdr->sh_size += amt;
5897 finfo->symbuf_count = 0;
5900 return TRUE;
5903 /* Add a symbol to the output symbol table. */
5905 static bfd_boolean
5906 elf_link_output_sym (struct elf_final_link_info *finfo,
5907 const char *name,
5908 Elf_Internal_Sym *elfsym,
5909 asection *input_sec,
5910 struct elf_link_hash_entry *h)
5912 bfd_byte *dest;
5913 Elf_External_Sym_Shndx *destshndx;
5914 bfd_boolean (*output_symbol_hook)
5915 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
5916 struct elf_link_hash_entry *);
5917 const struct elf_backend_data *bed;
5919 bed = get_elf_backend_data (finfo->output_bfd);
5920 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
5921 if (output_symbol_hook != NULL)
5923 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
5924 return FALSE;
5927 if (name == NULL || *name == '\0')
5928 elfsym->st_name = 0;
5929 else if (input_sec->flags & SEC_EXCLUDE)
5930 elfsym->st_name = 0;
5931 else
5933 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
5934 name, TRUE, FALSE);
5935 if (elfsym->st_name == (unsigned long) -1)
5936 return FALSE;
5939 if (finfo->symbuf_count >= finfo->symbuf_size)
5941 if (! elf_link_flush_output_syms (finfo, bed))
5942 return FALSE;
5945 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
5946 destshndx = finfo->symshndxbuf;
5947 if (destshndx != NULL)
5949 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
5951 bfd_size_type amt;
5953 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
5954 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
5955 if (destshndx == NULL)
5956 return FALSE;
5957 memset ((char *) destshndx + amt, 0, amt);
5958 finfo->shndxbuf_size *= 2;
5960 destshndx += bfd_get_symcount (finfo->output_bfd);
5963 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
5964 finfo->symbuf_count += 1;
5965 bfd_get_symcount (finfo->output_bfd) += 1;
5967 return TRUE;
5970 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
5971 allowing an unsatisfied unversioned symbol in the DSO to match a
5972 versioned symbol that would normally require an explicit version.
5973 We also handle the case that a DSO references a hidden symbol
5974 which may be satisfied by a versioned symbol in another DSO. */
5976 static bfd_boolean
5977 elf_link_check_versioned_symbol (struct bfd_link_info *info,
5978 const struct elf_backend_data *bed,
5979 struct elf_link_hash_entry *h)
5981 bfd *abfd;
5982 struct elf_link_loaded_list *loaded;
5984 if (!is_elf_hash_table (info->hash))
5985 return FALSE;
5987 switch (h->root.type)
5989 default:
5990 abfd = NULL;
5991 break;
5993 case bfd_link_hash_undefined:
5994 case bfd_link_hash_undefweak:
5995 abfd = h->root.u.undef.abfd;
5996 if ((abfd->flags & DYNAMIC) == 0
5997 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
5998 return FALSE;
5999 break;
6001 case bfd_link_hash_defined:
6002 case bfd_link_hash_defweak:
6003 abfd = h->root.u.def.section->owner;
6004 break;
6006 case bfd_link_hash_common:
6007 abfd = h->root.u.c.p->section->owner;
6008 break;
6010 BFD_ASSERT (abfd != NULL);
6012 for (loaded = elf_hash_table (info)->loaded;
6013 loaded != NULL;
6014 loaded = loaded->next)
6016 bfd *input;
6017 Elf_Internal_Shdr *hdr;
6018 bfd_size_type symcount;
6019 bfd_size_type extsymcount;
6020 bfd_size_type extsymoff;
6021 Elf_Internal_Shdr *versymhdr;
6022 Elf_Internal_Sym *isym;
6023 Elf_Internal_Sym *isymend;
6024 Elf_Internal_Sym *isymbuf;
6025 Elf_External_Versym *ever;
6026 Elf_External_Versym *extversym;
6028 input = loaded->abfd;
6030 /* We check each DSO for a possible hidden versioned definition. */
6031 if (input == abfd
6032 || (input->flags & DYNAMIC) == 0
6033 || elf_dynversym (input) == 0)
6034 continue;
6036 hdr = &elf_tdata (input)->dynsymtab_hdr;
6038 symcount = hdr->sh_size / bed->s->sizeof_sym;
6039 if (elf_bad_symtab (input))
6041 extsymcount = symcount;
6042 extsymoff = 0;
6044 else
6046 extsymcount = symcount - hdr->sh_info;
6047 extsymoff = hdr->sh_info;
6050 if (extsymcount == 0)
6051 continue;
6053 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
6054 NULL, NULL, NULL);
6055 if (isymbuf == NULL)
6056 return FALSE;
6058 /* Read in any version definitions. */
6059 versymhdr = &elf_tdata (input)->dynversym_hdr;
6060 extversym = bfd_malloc (versymhdr->sh_size);
6061 if (extversym == NULL)
6062 goto error_ret;
6064 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
6065 || (bfd_bread (extversym, versymhdr->sh_size, input)
6066 != versymhdr->sh_size))
6068 free (extversym);
6069 error_ret:
6070 free (isymbuf);
6071 return FALSE;
6074 ever = extversym + extsymoff;
6075 isymend = isymbuf + extsymcount;
6076 for (isym = isymbuf; isym < isymend; isym++, ever++)
6078 const char *name;
6079 Elf_Internal_Versym iver;
6080 unsigned short version_index;
6082 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
6083 || isym->st_shndx == SHN_UNDEF)
6084 continue;
6086 name = bfd_elf_string_from_elf_section (input,
6087 hdr->sh_link,
6088 isym->st_name);
6089 if (strcmp (name, h->root.root.string) != 0)
6090 continue;
6092 _bfd_elf_swap_versym_in (input, ever, &iver);
6094 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
6096 /* If we have a non-hidden versioned sym, then it should
6097 have provided a definition for the undefined sym. */
6098 abort ();
6101 version_index = iver.vs_vers & VERSYM_VERSION;
6102 if (version_index == 1 || version_index == 2)
6104 /* This is the base or first version. We can use it. */
6105 free (extversym);
6106 free (isymbuf);
6107 return TRUE;
6111 free (extversym);
6112 free (isymbuf);
6115 return FALSE;
6118 /* Add an external symbol to the symbol table. This is called from
6119 the hash table traversal routine. When generating a shared object,
6120 we go through the symbol table twice. The first time we output
6121 anything that might have been forced to local scope in a version
6122 script. The second time we output the symbols that are still
6123 global symbols. */
6125 static bfd_boolean
6126 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
6128 struct elf_outext_info *eoinfo = data;
6129 struct elf_final_link_info *finfo = eoinfo->finfo;
6130 bfd_boolean strip;
6131 Elf_Internal_Sym sym;
6132 asection *input_sec;
6133 const struct elf_backend_data *bed;
6135 if (h->root.type == bfd_link_hash_warning)
6137 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6138 if (h->root.type == bfd_link_hash_new)
6139 return TRUE;
6142 /* Decide whether to output this symbol in this pass. */
6143 if (eoinfo->localsyms)
6145 if (!h->forced_local)
6146 return TRUE;
6148 else
6150 if (h->forced_local)
6151 return TRUE;
6154 bed = get_elf_backend_data (finfo->output_bfd);
6156 /* If we have an undefined symbol reference here then it must have
6157 come from a shared library that is being linked in. (Undefined
6158 references in regular files have already been handled). If we
6159 are reporting errors for this situation then do so now. */
6160 if (h->root.type == bfd_link_hash_undefined
6161 && h->ref_dynamic
6162 && !h->ref_regular
6163 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
6164 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
6166 if (! ((*finfo->info->callbacks->undefined_symbol)
6167 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
6168 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
6170 eoinfo->failed = TRUE;
6171 return FALSE;
6175 /* We should also warn if a forced local symbol is referenced from
6176 shared libraries. */
6177 if (! finfo->info->relocatable
6178 && (! finfo->info->shared)
6179 && h->forced_local
6180 && h->ref_dynamic
6181 && !h->dynamic_def
6182 && !h->dynamic_weak
6183 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
6185 (*_bfd_error_handler)
6186 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6187 finfo->output_bfd, h->root.u.def.section->owner,
6188 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
6189 ? "internal"
6190 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
6191 ? "hidden" : "local",
6192 h->root.root.string);
6193 eoinfo->failed = TRUE;
6194 return FALSE;
6197 /* We don't want to output symbols that have never been mentioned by
6198 a regular file, or that we have been told to strip. However, if
6199 h->indx is set to -2, the symbol is used by a reloc and we must
6200 output it. */
6201 if (h->indx == -2)
6202 strip = FALSE;
6203 else if ((h->def_dynamic
6204 || h->ref_dynamic)
6205 && !h->def_regular
6206 && !h->ref_regular)
6207 strip = TRUE;
6208 else if (finfo->info->strip == strip_all)
6209 strip = TRUE;
6210 else if (finfo->info->strip == strip_some
6211 && bfd_hash_lookup (finfo->info->keep_hash,
6212 h->root.root.string, FALSE, FALSE) == NULL)
6213 strip = TRUE;
6214 else if (finfo->info->strip_discarded
6215 && (h->root.type == bfd_link_hash_defined
6216 || h->root.type == bfd_link_hash_defweak)
6217 && elf_discarded_section (h->root.u.def.section))
6218 strip = TRUE;
6219 else
6220 strip = FALSE;
6222 /* If we're stripping it, and it's not a dynamic symbol, there's
6223 nothing else to do unless it is a forced local symbol. */
6224 if (strip
6225 && h->dynindx == -1
6226 && !h->forced_local)
6227 return TRUE;
6229 sym.st_value = 0;
6230 sym.st_size = h->size;
6231 sym.st_other = h->other;
6232 if (h->forced_local)
6233 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
6234 else if (h->root.type == bfd_link_hash_undefweak
6235 || h->root.type == bfd_link_hash_defweak)
6236 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
6237 else
6238 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
6240 switch (h->root.type)
6242 default:
6243 case bfd_link_hash_new:
6244 case bfd_link_hash_warning:
6245 abort ();
6246 return FALSE;
6248 case bfd_link_hash_undefined:
6249 case bfd_link_hash_undefweak:
6250 input_sec = bfd_und_section_ptr;
6251 sym.st_shndx = SHN_UNDEF;
6252 break;
6254 case bfd_link_hash_defined:
6255 case bfd_link_hash_defweak:
6257 input_sec = h->root.u.def.section;
6258 if (input_sec->output_section != NULL)
6260 sym.st_shndx =
6261 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
6262 input_sec->output_section);
6263 if (sym.st_shndx == SHN_BAD)
6265 (*_bfd_error_handler)
6266 (_("%B: could not find output section %A for input section %A"),
6267 finfo->output_bfd, input_sec->output_section, input_sec);
6268 eoinfo->failed = TRUE;
6269 return FALSE;
6272 /* ELF symbols in relocatable files are section relative,
6273 but in nonrelocatable files they are virtual
6274 addresses. */
6275 sym.st_value = h->root.u.def.value + input_sec->output_offset;
6276 if (! finfo->info->relocatable)
6278 sym.st_value += input_sec->output_section->vma;
6279 if (h->type == STT_TLS)
6281 /* STT_TLS symbols are relative to PT_TLS segment
6282 base. */
6283 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6284 sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6288 else
6290 BFD_ASSERT (input_sec->owner == NULL
6291 || (input_sec->owner->flags & DYNAMIC) != 0);
6292 sym.st_shndx = SHN_UNDEF;
6293 input_sec = bfd_und_section_ptr;
6296 break;
6298 case bfd_link_hash_common:
6299 input_sec = h->root.u.c.p->section;
6300 sym.st_shndx = SHN_COMMON;
6301 sym.st_value = 1 << h->root.u.c.p->alignment_power;
6302 break;
6304 case bfd_link_hash_indirect:
6305 /* These symbols are created by symbol versioning. They point
6306 to the decorated version of the name. For example, if the
6307 symbol foo@@GNU_1.2 is the default, which should be used when
6308 foo is used with no version, then we add an indirect symbol
6309 foo which points to foo@@GNU_1.2. We ignore these symbols,
6310 since the indirected symbol is already in the hash table. */
6311 return TRUE;
6314 /* Give the processor backend a chance to tweak the symbol value,
6315 and also to finish up anything that needs to be done for this
6316 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6317 forced local syms when non-shared is due to a historical quirk. */
6318 if ((h->dynindx != -1
6319 || h->forced_local)
6320 && ((finfo->info->shared
6321 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6322 || h->root.type != bfd_link_hash_undefweak))
6323 || !h->forced_local)
6324 && elf_hash_table (finfo->info)->dynamic_sections_created)
6326 if (! ((*bed->elf_backend_finish_dynamic_symbol)
6327 (finfo->output_bfd, finfo->info, h, &sym)))
6329 eoinfo->failed = TRUE;
6330 return FALSE;
6334 /* If we are marking the symbol as undefined, and there are no
6335 non-weak references to this symbol from a regular object, then
6336 mark the symbol as weak undefined; if there are non-weak
6337 references, mark the symbol as strong. We can't do this earlier,
6338 because it might not be marked as undefined until the
6339 finish_dynamic_symbol routine gets through with it. */
6340 if (sym.st_shndx == SHN_UNDEF
6341 && h->ref_regular
6342 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
6343 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
6345 int bindtype;
6347 if (h->ref_regular_nonweak)
6348 bindtype = STB_GLOBAL;
6349 else
6350 bindtype = STB_WEAK;
6351 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
6354 /* If a non-weak symbol with non-default visibility is not defined
6355 locally, it is a fatal error. */
6356 if (! finfo->info->relocatable
6357 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
6358 && ELF_ST_BIND (sym.st_info) != STB_WEAK
6359 && h->root.type == bfd_link_hash_undefined
6360 && !h->def_regular)
6362 (*_bfd_error_handler)
6363 (_("%B: %s symbol `%s' isn't defined"),
6364 finfo->output_bfd,
6365 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
6366 ? "protected"
6367 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
6368 ? "internal" : "hidden",
6369 h->root.root.string);
6370 eoinfo->failed = TRUE;
6371 return FALSE;
6374 /* If this symbol should be put in the .dynsym section, then put it
6375 there now. We already know the symbol index. We also fill in
6376 the entry in the .hash section. */
6377 if (h->dynindx != -1
6378 && elf_hash_table (finfo->info)->dynamic_sections_created)
6380 size_t bucketcount;
6381 size_t bucket;
6382 size_t hash_entry_size;
6383 bfd_byte *bucketpos;
6384 bfd_vma chain;
6385 bfd_byte *esym;
6387 sym.st_name = h->dynstr_index;
6388 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
6389 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
6391 bucketcount = elf_hash_table (finfo->info)->bucketcount;
6392 bucket = h->u.elf_hash_value % bucketcount;
6393 hash_entry_size
6394 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
6395 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
6396 + (bucket + 2) * hash_entry_size);
6397 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
6398 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
6399 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
6400 ((bfd_byte *) finfo->hash_sec->contents
6401 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
6403 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
6405 Elf_Internal_Versym iversym;
6406 Elf_External_Versym *eversym;
6408 if (!h->def_regular)
6410 if (h->verinfo.verdef == NULL)
6411 iversym.vs_vers = 0;
6412 else
6413 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
6415 else
6417 if (h->verinfo.vertree == NULL)
6418 iversym.vs_vers = 1;
6419 else
6420 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
6421 if (finfo->info->create_default_symver)
6422 iversym.vs_vers++;
6425 if (h->hidden)
6426 iversym.vs_vers |= VERSYM_HIDDEN;
6428 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
6429 eversym += h->dynindx;
6430 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
6434 /* If we're stripping it, then it was just a dynamic symbol, and
6435 there's nothing else to do. */
6436 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
6437 return TRUE;
6439 h->indx = bfd_get_symcount (finfo->output_bfd);
6441 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
6443 eoinfo->failed = TRUE;
6444 return FALSE;
6447 return TRUE;
6450 /* Return TRUE if special handling is done for relocs in SEC against
6451 symbols defined in discarded sections. */
6453 static bfd_boolean
6454 elf_section_ignore_discarded_relocs (asection *sec)
6456 const struct elf_backend_data *bed;
6458 switch (sec->sec_info_type)
6460 case ELF_INFO_TYPE_STABS:
6461 case ELF_INFO_TYPE_EH_FRAME:
6462 return TRUE;
6463 default:
6464 break;
6467 bed = get_elf_backend_data (sec->owner);
6468 if (bed->elf_backend_ignore_discarded_relocs != NULL
6469 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
6470 return TRUE;
6472 return FALSE;
6475 enum action_discarded
6477 COMPLAIN = 1,
6478 PRETEND = 2
6481 /* Return a mask saying how ld should treat relocations in SEC against
6482 symbols defined in discarded sections. If this function returns
6483 COMPLAIN set, ld will issue a warning message. If this function
6484 returns PRETEND set, and the discarded section was link-once and the
6485 same size as the kept link-once section, ld will pretend that the
6486 symbol was actually defined in the kept section. Otherwise ld will
6487 zero the reloc (at least that is the intent, but some cooperation by
6488 the target dependent code is needed, particularly for REL targets). */
6490 static unsigned int
6491 elf_action_discarded (asection *sec)
6493 if (sec->flags & SEC_DEBUGGING)
6494 return PRETEND;
6496 if (strcmp (".eh_frame", sec->name) == 0)
6497 return 0;
6499 if (strcmp (".gcc_except_table", sec->name) == 0)
6500 return 0;
6502 if (strcmp (".PARISC.unwind", sec->name) == 0)
6503 return 0;
6505 if (strcmp (".fixup", sec->name) == 0)
6506 return 0;
6508 return COMPLAIN | PRETEND;
6511 /* Find a match between a section and a member of a section group. */
6513 static asection *
6514 match_group_member (asection *sec, asection *group)
6516 asection *first = elf_next_in_group (group);
6517 asection *s = first;
6519 while (s != NULL)
6521 if (bfd_elf_match_symbols_in_sections (s, sec))
6522 return s;
6524 if (s == first)
6525 break;
6528 return NULL;
6531 /* Link an input file into the linker output file. This function
6532 handles all the sections and relocations of the input file at once.
6533 This is so that we only have to read the local symbols once, and
6534 don't have to keep them in memory. */
6536 static bfd_boolean
6537 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
6539 bfd_boolean (*relocate_section)
6540 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
6541 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
6542 bfd *output_bfd;
6543 Elf_Internal_Shdr *symtab_hdr;
6544 size_t locsymcount;
6545 size_t extsymoff;
6546 Elf_Internal_Sym *isymbuf;
6547 Elf_Internal_Sym *isym;
6548 Elf_Internal_Sym *isymend;
6549 long *pindex;
6550 asection **ppsection;
6551 asection *o;
6552 const struct elf_backend_data *bed;
6553 bfd_boolean emit_relocs;
6554 struct elf_link_hash_entry **sym_hashes;
6556 output_bfd = finfo->output_bfd;
6557 bed = get_elf_backend_data (output_bfd);
6558 relocate_section = bed->elf_backend_relocate_section;
6560 /* If this is a dynamic object, we don't want to do anything here:
6561 we don't want the local symbols, and we don't want the section
6562 contents. */
6563 if ((input_bfd->flags & DYNAMIC) != 0)
6564 return TRUE;
6566 emit_relocs = (finfo->info->relocatable
6567 || finfo->info->emitrelocations
6568 || bed->elf_backend_emit_relocs);
6570 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6571 if (elf_bad_symtab (input_bfd))
6573 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
6574 extsymoff = 0;
6576 else
6578 locsymcount = symtab_hdr->sh_info;
6579 extsymoff = symtab_hdr->sh_info;
6582 /* Read the local symbols. */
6583 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
6584 if (isymbuf == NULL && locsymcount != 0)
6586 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
6587 finfo->internal_syms,
6588 finfo->external_syms,
6589 finfo->locsym_shndx);
6590 if (isymbuf == NULL)
6591 return FALSE;
6594 /* Find local symbol sections and adjust values of symbols in
6595 SEC_MERGE sections. Write out those local symbols we know are
6596 going into the output file. */
6597 isymend = isymbuf + locsymcount;
6598 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
6599 isym < isymend;
6600 isym++, pindex++, ppsection++)
6602 asection *isec;
6603 const char *name;
6604 Elf_Internal_Sym osym;
6606 *pindex = -1;
6608 if (elf_bad_symtab (input_bfd))
6610 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
6612 *ppsection = NULL;
6613 continue;
6617 if (isym->st_shndx == SHN_UNDEF)
6618 isec = bfd_und_section_ptr;
6619 else if (isym->st_shndx < SHN_LORESERVE
6620 || isym->st_shndx > SHN_HIRESERVE)
6622 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
6623 if (isec
6624 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
6625 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
6626 isym->st_value =
6627 _bfd_merged_section_offset (output_bfd, &isec,
6628 elf_section_data (isec)->sec_info,
6629 isym->st_value);
6631 else if (isym->st_shndx == SHN_ABS)
6632 isec = bfd_abs_section_ptr;
6633 else if (isym->st_shndx == SHN_COMMON)
6634 isec = bfd_com_section_ptr;
6635 else
6637 /* Who knows? */
6638 isec = NULL;
6641 *ppsection = isec;
6643 /* Don't output the first, undefined, symbol. */
6644 if (ppsection == finfo->sections)
6645 continue;
6647 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6649 /* We never output section symbols. Instead, we use the
6650 section symbol of the corresponding section in the output
6651 file. */
6652 continue;
6655 /* If we are stripping all symbols, we don't want to output this
6656 one. */
6657 if (finfo->info->strip == strip_all)
6658 continue;
6660 /* If we are discarding all local symbols, we don't want to
6661 output this one. If we are generating a relocatable output
6662 file, then some of the local symbols may be required by
6663 relocs; we output them below as we discover that they are
6664 needed. */
6665 if (finfo->info->discard == discard_all)
6666 continue;
6668 /* If this symbol is defined in a section which we are
6669 discarding, we don't need to keep it, but note that
6670 linker_mark is only reliable for sections that have contents.
6671 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6672 as well as linker_mark. */
6673 if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
6674 && isec != NULL
6675 && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
6676 || (! finfo->info->relocatable
6677 && (isec->flags & SEC_EXCLUDE) != 0)))
6678 continue;
6680 /* Get the name of the symbol. */
6681 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
6682 isym->st_name);
6683 if (name == NULL)
6684 return FALSE;
6686 /* See if we are discarding symbols with this name. */
6687 if ((finfo->info->strip == strip_some
6688 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
6689 == NULL))
6690 || (((finfo->info->discard == discard_sec_merge
6691 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
6692 || finfo->info->discard == discard_l)
6693 && bfd_is_local_label_name (input_bfd, name)))
6694 continue;
6696 /* If we get here, we are going to output this symbol. */
6698 osym = *isym;
6700 /* Adjust the section index for the output file. */
6701 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
6702 isec->output_section);
6703 if (osym.st_shndx == SHN_BAD)
6704 return FALSE;
6706 *pindex = bfd_get_symcount (output_bfd);
6708 /* ELF symbols in relocatable files are section relative, but
6709 in executable files they are virtual addresses. Note that
6710 this code assumes that all ELF sections have an associated
6711 BFD section with a reasonable value for output_offset; below
6712 we assume that they also have a reasonable value for
6713 output_section. Any special sections must be set up to meet
6714 these requirements. */
6715 osym.st_value += isec->output_offset;
6716 if (! finfo->info->relocatable)
6718 osym.st_value += isec->output_section->vma;
6719 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
6721 /* STT_TLS symbols are relative to PT_TLS segment base. */
6722 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6723 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6727 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
6728 return FALSE;
6731 /* Relocate the contents of each section. */
6732 sym_hashes = elf_sym_hashes (input_bfd);
6733 for (o = input_bfd->sections; o != NULL; o = o->next)
6735 bfd_byte *contents;
6737 if (! o->linker_mark)
6739 /* This section was omitted from the link. */
6740 continue;
6743 if ((o->flags & SEC_HAS_CONTENTS) == 0
6744 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
6745 continue;
6747 if ((o->flags & SEC_LINKER_CREATED) != 0)
6749 /* Section was created by _bfd_elf_link_create_dynamic_sections
6750 or somesuch. */
6751 continue;
6754 /* Get the contents of the section. They have been cached by a
6755 relaxation routine. Note that o is a section in an input
6756 file, so the contents field will not have been set by any of
6757 the routines which work on output files. */
6758 if (elf_section_data (o)->this_hdr.contents != NULL)
6759 contents = elf_section_data (o)->this_hdr.contents;
6760 else
6762 bfd_size_type amt = o->rawsize ? o->rawsize : o->size;
6764 contents = finfo->contents;
6765 if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt))
6766 return FALSE;
6769 if ((o->flags & SEC_RELOC) != 0)
6771 Elf_Internal_Rela *internal_relocs;
6772 bfd_vma r_type_mask;
6773 int r_sym_shift;
6775 /* Get the swapped relocs. */
6776 internal_relocs
6777 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
6778 finfo->internal_relocs, FALSE);
6779 if (internal_relocs == NULL
6780 && o->reloc_count > 0)
6781 return FALSE;
6783 if (bed->s->arch_size == 32)
6785 r_type_mask = 0xff;
6786 r_sym_shift = 8;
6788 else
6790 r_type_mask = 0xffffffff;
6791 r_sym_shift = 32;
6794 /* Run through the relocs looking for any against symbols
6795 from discarded sections and section symbols from
6796 removed link-once sections. Complain about relocs
6797 against discarded sections. Zero relocs against removed
6798 link-once sections. Preserve debug information as much
6799 as we can. */
6800 if (!elf_section_ignore_discarded_relocs (o))
6802 Elf_Internal_Rela *rel, *relend;
6803 unsigned int action = elf_action_discarded (o);
6805 rel = internal_relocs;
6806 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
6807 for ( ; rel < relend; rel++)
6809 unsigned long r_symndx = rel->r_info >> r_sym_shift;
6810 asection **ps, *sec;
6811 struct elf_link_hash_entry *h = NULL;
6812 const char *sym_name;
6814 if (r_symndx == STN_UNDEF)
6815 continue;
6817 if (r_symndx >= locsymcount
6818 || (elf_bad_symtab (input_bfd)
6819 && finfo->sections[r_symndx] == NULL))
6821 h = sym_hashes[r_symndx - extsymoff];
6822 while (h->root.type == bfd_link_hash_indirect
6823 || h->root.type == bfd_link_hash_warning)
6824 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6826 if (h->root.type != bfd_link_hash_defined
6827 && h->root.type != bfd_link_hash_defweak)
6828 continue;
6830 ps = &h->root.u.def.section;
6831 sym_name = h->root.root.string;
6833 else
6835 Elf_Internal_Sym *sym = isymbuf + r_symndx;
6836 ps = &finfo->sections[r_symndx];
6837 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym);
6840 /* Complain if the definition comes from a
6841 discarded section. */
6842 if ((sec = *ps) != NULL && elf_discarded_section (sec))
6844 asection *kept;
6846 BFD_ASSERT (r_symndx != 0);
6847 if (action & COMPLAIN)
6849 (*_bfd_error_handler)
6850 (_("`%s' referenced in section `%A' of %B: "
6851 "defined in discarded section `%A' of %B\n"),
6852 o, input_bfd, sec, sec->owner, sym_name);
6855 /* Try to do the best we can to support buggy old
6856 versions of gcc. If we've warned, or this is
6857 debugging info, pretend that the symbol is
6858 really defined in the kept linkonce section.
6859 FIXME: This is quite broken. Modifying the
6860 symbol here means we will be changing all later
6861 uses of the symbol, not just in this section.
6862 The only thing that makes this half reasonable
6863 is that we warn in non-debug sections, and
6864 debug sections tend to come after other
6865 sections. */
6866 kept = sec->kept_section;
6867 if (kept != NULL && (action & PRETEND))
6869 if (elf_sec_group (sec) != NULL)
6870 kept = match_group_member (sec, kept);
6871 if (kept != NULL
6872 && sec->size == kept->size)
6874 *ps = kept;
6875 continue;
6879 /* Remove the symbol reference from the reloc, but
6880 don't kill the reloc completely. This is so that
6881 a zero value will be written into the section,
6882 which may have non-zero contents put there by the
6883 assembler. Zero in things like an eh_frame fde
6884 pc_begin allows stack unwinders to recognize the
6885 fde as bogus. */
6886 rel->r_info &= r_type_mask;
6887 rel->r_addend = 0;
6892 /* Relocate the section by invoking a back end routine.
6894 The back end routine is responsible for adjusting the
6895 section contents as necessary, and (if using Rela relocs
6896 and generating a relocatable output file) adjusting the
6897 reloc addend as necessary.
6899 The back end routine does not have to worry about setting
6900 the reloc address or the reloc symbol index.
6902 The back end routine is given a pointer to the swapped in
6903 internal symbols, and can access the hash table entries
6904 for the external symbols via elf_sym_hashes (input_bfd).
6906 When generating relocatable output, the back end routine
6907 must handle STB_LOCAL/STT_SECTION symbols specially. The
6908 output symbol is going to be a section symbol
6909 corresponding to the output section, which will require
6910 the addend to be adjusted. */
6912 if (! (*relocate_section) (output_bfd, finfo->info,
6913 input_bfd, o, contents,
6914 internal_relocs,
6915 isymbuf,
6916 finfo->sections))
6917 return FALSE;
6919 if (emit_relocs)
6921 Elf_Internal_Rela *irela;
6922 Elf_Internal_Rela *irelaend;
6923 bfd_vma last_offset;
6924 struct elf_link_hash_entry **rel_hash;
6925 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
6926 unsigned int next_erel;
6927 bfd_boolean (*reloc_emitter)
6928 (bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *);
6929 bfd_boolean rela_normal;
6931 input_rel_hdr = &elf_section_data (o)->rel_hdr;
6932 rela_normal = (bed->rela_normal
6933 && (input_rel_hdr->sh_entsize
6934 == bed->s->sizeof_rela));
6936 /* Adjust the reloc addresses and symbol indices. */
6938 irela = internal_relocs;
6939 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
6940 rel_hash = (elf_section_data (o->output_section)->rel_hashes
6941 + elf_section_data (o->output_section)->rel_count
6942 + elf_section_data (o->output_section)->rel_count2);
6943 last_offset = o->output_offset;
6944 if (!finfo->info->relocatable)
6945 last_offset += o->output_section->vma;
6946 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
6948 unsigned long r_symndx;
6949 asection *sec;
6950 Elf_Internal_Sym sym;
6952 if (next_erel == bed->s->int_rels_per_ext_rel)
6954 rel_hash++;
6955 next_erel = 0;
6958 irela->r_offset = _bfd_elf_section_offset (output_bfd,
6959 finfo->info, o,
6960 irela->r_offset);
6961 if (irela->r_offset >= (bfd_vma) -2)
6963 /* This is a reloc for a deleted entry or somesuch.
6964 Turn it into an R_*_NONE reloc, at the same
6965 offset as the last reloc. elf_eh_frame.c and
6966 elf_bfd_discard_info rely on reloc offsets
6967 being ordered. */
6968 irela->r_offset = last_offset;
6969 irela->r_info = 0;
6970 irela->r_addend = 0;
6971 continue;
6974 irela->r_offset += o->output_offset;
6976 /* Relocs in an executable have to be virtual addresses. */
6977 if (!finfo->info->relocatable)
6978 irela->r_offset += o->output_section->vma;
6980 last_offset = irela->r_offset;
6982 r_symndx = irela->r_info >> r_sym_shift;
6983 if (r_symndx == STN_UNDEF)
6984 continue;
6986 if (r_symndx >= locsymcount
6987 || (elf_bad_symtab (input_bfd)
6988 && finfo->sections[r_symndx] == NULL))
6990 struct elf_link_hash_entry *rh;
6991 unsigned long indx;
6993 /* This is a reloc against a global symbol. We
6994 have not yet output all the local symbols, so
6995 we do not know the symbol index of any global
6996 symbol. We set the rel_hash entry for this
6997 reloc to point to the global hash table entry
6998 for this symbol. The symbol index is then
6999 set at the end of bfd_elf_final_link. */
7000 indx = r_symndx - extsymoff;
7001 rh = elf_sym_hashes (input_bfd)[indx];
7002 while (rh->root.type == bfd_link_hash_indirect
7003 || rh->root.type == bfd_link_hash_warning)
7004 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
7006 /* Setting the index to -2 tells
7007 elf_link_output_extsym that this symbol is
7008 used by a reloc. */
7009 BFD_ASSERT (rh->indx < 0);
7010 rh->indx = -2;
7012 *rel_hash = rh;
7014 continue;
7017 /* This is a reloc against a local symbol. */
7019 *rel_hash = NULL;
7020 sym = isymbuf[r_symndx];
7021 sec = finfo->sections[r_symndx];
7022 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
7024 /* I suppose the backend ought to fill in the
7025 section of any STT_SECTION symbol against a
7026 processor specific section. */
7027 r_symndx = 0;
7028 if (bfd_is_abs_section (sec))
7030 else if (sec == NULL || sec->owner == NULL)
7032 bfd_set_error (bfd_error_bad_value);
7033 return FALSE;
7035 else
7037 asection *osec = sec->output_section;
7039 /* If we have discarded a section, the output
7040 section will be the absolute section. In
7041 case of discarded link-once and discarded
7042 SEC_MERGE sections, use the kept section. */
7043 if (bfd_is_abs_section (osec)
7044 && sec->kept_section != NULL
7045 && sec->kept_section->output_section != NULL)
7047 osec = sec->kept_section->output_section;
7048 irela->r_addend -= osec->vma;
7051 if (!bfd_is_abs_section (osec))
7053 r_symndx = osec->target_index;
7054 BFD_ASSERT (r_symndx != 0);
7058 /* Adjust the addend according to where the
7059 section winds up in the output section. */
7060 if (rela_normal)
7061 irela->r_addend += sec->output_offset;
7063 else
7065 if (finfo->indices[r_symndx] == -1)
7067 unsigned long shlink;
7068 const char *name;
7069 asection *osec;
7071 if (finfo->info->strip == strip_all)
7073 /* You can't do ld -r -s. */
7074 bfd_set_error (bfd_error_invalid_operation);
7075 return FALSE;
7078 /* This symbol was skipped earlier, but
7079 since it is needed by a reloc, we
7080 must output it now. */
7081 shlink = symtab_hdr->sh_link;
7082 name = (bfd_elf_string_from_elf_section
7083 (input_bfd, shlink, sym.st_name));
7084 if (name == NULL)
7085 return FALSE;
7087 osec = sec->output_section;
7088 sym.st_shndx =
7089 _bfd_elf_section_from_bfd_section (output_bfd,
7090 osec);
7091 if (sym.st_shndx == SHN_BAD)
7092 return FALSE;
7094 sym.st_value += sec->output_offset;
7095 if (! finfo->info->relocatable)
7097 sym.st_value += osec->vma;
7098 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
7100 /* STT_TLS symbols are relative to PT_TLS
7101 segment base. */
7102 BFD_ASSERT (elf_hash_table (finfo->info)
7103 ->tls_sec != NULL);
7104 sym.st_value -= (elf_hash_table (finfo->info)
7105 ->tls_sec->vma);
7109 finfo->indices[r_symndx]
7110 = bfd_get_symcount (output_bfd);
7112 if (! elf_link_output_sym (finfo, name, &sym, sec,
7113 NULL))
7114 return FALSE;
7117 r_symndx = finfo->indices[r_symndx];
7120 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
7121 | (irela->r_info & r_type_mask));
7124 /* Swap out the relocs. */
7125 if (bed->elf_backend_emit_relocs
7126 && !(finfo->info->relocatable
7127 || finfo->info->emitrelocations))
7128 reloc_emitter = bed->elf_backend_emit_relocs;
7129 else
7130 reloc_emitter = _bfd_elf_link_output_relocs;
7132 if (input_rel_hdr->sh_size != 0
7133 && ! (*reloc_emitter) (output_bfd, o, input_rel_hdr,
7134 internal_relocs))
7135 return FALSE;
7137 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
7138 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
7140 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
7141 * bed->s->int_rels_per_ext_rel);
7142 if (! (*reloc_emitter) (output_bfd, o, input_rel_hdr2,
7143 internal_relocs))
7144 return FALSE;
7149 /* Write out the modified section contents. */
7150 if (bed->elf_backend_write_section
7151 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
7153 /* Section written out. */
7155 else switch (o->sec_info_type)
7157 case ELF_INFO_TYPE_STABS:
7158 if (! (_bfd_write_section_stabs
7159 (output_bfd,
7160 &elf_hash_table (finfo->info)->stab_info,
7161 o, &elf_section_data (o)->sec_info, contents)))
7162 return FALSE;
7163 break;
7164 case ELF_INFO_TYPE_MERGE:
7165 if (! _bfd_write_merged_section (output_bfd, o,
7166 elf_section_data (o)->sec_info))
7167 return FALSE;
7168 break;
7169 case ELF_INFO_TYPE_EH_FRAME:
7171 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
7172 o, contents))
7173 return FALSE;
7175 break;
7176 default:
7178 if (! (o->flags & SEC_EXCLUDE)
7179 && ! bfd_set_section_contents (output_bfd, o->output_section,
7180 contents,
7181 (file_ptr) o->output_offset,
7182 o->size))
7183 return FALSE;
7185 break;
7189 return TRUE;
7192 /* Generate a reloc when linking an ELF file. This is a reloc
7193 requested by the linker, and does come from any input file. This
7194 is used to build constructor and destructor tables when linking
7195 with -Ur. */
7197 static bfd_boolean
7198 elf_reloc_link_order (bfd *output_bfd,
7199 struct bfd_link_info *info,
7200 asection *output_section,
7201 struct bfd_link_order *link_order)
7203 reloc_howto_type *howto;
7204 long indx;
7205 bfd_vma offset;
7206 bfd_vma addend;
7207 struct elf_link_hash_entry **rel_hash_ptr;
7208 Elf_Internal_Shdr *rel_hdr;
7209 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7210 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
7211 bfd_byte *erel;
7212 unsigned int i;
7214 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
7215 if (howto == NULL)
7217 bfd_set_error (bfd_error_bad_value);
7218 return FALSE;
7221 addend = link_order->u.reloc.p->addend;
7223 /* Figure out the symbol index. */
7224 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
7225 + elf_section_data (output_section)->rel_count
7226 + elf_section_data (output_section)->rel_count2);
7227 if (link_order->type == bfd_section_reloc_link_order)
7229 indx = link_order->u.reloc.p->u.section->target_index;
7230 BFD_ASSERT (indx != 0);
7231 *rel_hash_ptr = NULL;
7233 else
7235 struct elf_link_hash_entry *h;
7237 /* Treat a reloc against a defined symbol as though it were
7238 actually against the section. */
7239 h = ((struct elf_link_hash_entry *)
7240 bfd_wrapped_link_hash_lookup (output_bfd, info,
7241 link_order->u.reloc.p->u.name,
7242 FALSE, FALSE, TRUE));
7243 if (h != NULL
7244 && (h->root.type == bfd_link_hash_defined
7245 || h->root.type == bfd_link_hash_defweak))
7247 asection *section;
7249 section = h->root.u.def.section;
7250 indx = section->output_section->target_index;
7251 *rel_hash_ptr = NULL;
7252 /* It seems that we ought to add the symbol value to the
7253 addend here, but in practice it has already been added
7254 because it was passed to constructor_callback. */
7255 addend += section->output_section->vma + section->output_offset;
7257 else if (h != NULL)
7259 /* Setting the index to -2 tells elf_link_output_extsym that
7260 this symbol is used by a reloc. */
7261 h->indx = -2;
7262 *rel_hash_ptr = h;
7263 indx = 0;
7265 else
7267 if (! ((*info->callbacks->unattached_reloc)
7268 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
7269 return FALSE;
7270 indx = 0;
7274 /* If this is an inplace reloc, we must write the addend into the
7275 object file. */
7276 if (howto->partial_inplace && addend != 0)
7278 bfd_size_type size;
7279 bfd_reloc_status_type rstat;
7280 bfd_byte *buf;
7281 bfd_boolean ok;
7282 const char *sym_name;
7284 size = bfd_get_reloc_size (howto);
7285 buf = bfd_zmalloc (size);
7286 if (buf == NULL)
7287 return FALSE;
7288 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
7289 switch (rstat)
7291 case bfd_reloc_ok:
7292 break;
7294 default:
7295 case bfd_reloc_outofrange:
7296 abort ();
7298 case bfd_reloc_overflow:
7299 if (link_order->type == bfd_section_reloc_link_order)
7300 sym_name = bfd_section_name (output_bfd,
7301 link_order->u.reloc.p->u.section);
7302 else
7303 sym_name = link_order->u.reloc.p->u.name;
7304 if (! ((*info->callbacks->reloc_overflow)
7305 (info, NULL, sym_name, howto->name, addend, NULL,
7306 NULL, (bfd_vma) 0)))
7308 free (buf);
7309 return FALSE;
7311 break;
7313 ok = bfd_set_section_contents (output_bfd, output_section, buf,
7314 link_order->offset, size);
7315 free (buf);
7316 if (! ok)
7317 return FALSE;
7320 /* The address of a reloc is relative to the section in a
7321 relocatable file, and is a virtual address in an executable
7322 file. */
7323 offset = link_order->offset;
7324 if (! info->relocatable)
7325 offset += output_section->vma;
7327 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7329 irel[i].r_offset = offset;
7330 irel[i].r_info = 0;
7331 irel[i].r_addend = 0;
7333 if (bed->s->arch_size == 32)
7334 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
7335 else
7336 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
7338 rel_hdr = &elf_section_data (output_section)->rel_hdr;
7339 erel = rel_hdr->contents;
7340 if (rel_hdr->sh_type == SHT_REL)
7342 erel += (elf_section_data (output_section)->rel_count
7343 * bed->s->sizeof_rel);
7344 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
7346 else
7348 irel[0].r_addend = addend;
7349 erel += (elf_section_data (output_section)->rel_count
7350 * bed->s->sizeof_rela);
7351 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
7354 ++elf_section_data (output_section)->rel_count;
7356 return TRUE;
7360 /* Get the output vma of the section pointed to by the sh_link field. */
7362 static bfd_vma
7363 elf_get_linked_section_vma (struct bfd_link_order *p)
7365 Elf_Internal_Shdr **elf_shdrp;
7366 asection *s;
7367 int elfsec;
7369 s = p->u.indirect.section;
7370 elf_shdrp = elf_elfsections (s->owner);
7371 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
7372 elfsec = elf_shdrp[elfsec]->sh_link;
7373 /* PR 290:
7374 The Intel C compiler generates SHT_IA_64_UNWIND with
7375 SHF_LINK_ORDER. But it doesn't set theh sh_link or
7376 sh_info fields. Hence we could get the situation
7377 where elfsec is 0. */
7378 if (elfsec == 0)
7380 const struct elf_backend_data *bed
7381 = get_elf_backend_data (s->owner);
7382 if (bed->link_order_error_handler)
7383 bed->link_order_error_handler
7384 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
7385 return 0;
7387 else
7389 s = elf_shdrp[elfsec]->bfd_section;
7390 return s->output_section->vma + s->output_offset;
7395 /* Compare two sections based on the locations of the sections they are
7396 linked to. Used by elf_fixup_link_order. */
7398 static int
7399 compare_link_order (const void * a, const void * b)
7401 bfd_vma apos;
7402 bfd_vma bpos;
7404 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
7405 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
7406 if (apos < bpos)
7407 return -1;
7408 return apos > bpos;
7412 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7413 order as their linked sections. Returns false if this could not be done
7414 because an output section includes both ordered and unordered
7415 sections. Ideally we'd do this in the linker proper. */
7417 static bfd_boolean
7418 elf_fixup_link_order (bfd *abfd, asection *o)
7420 int seen_linkorder;
7421 int seen_other;
7422 int n;
7423 struct bfd_link_order *p;
7424 bfd *sub;
7425 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7426 int elfsec;
7427 struct bfd_link_order **sections;
7428 asection *s;
7429 bfd_vma offset;
7431 seen_other = 0;
7432 seen_linkorder = 0;
7433 for (p = o->link_order_head; p != NULL; p = p->next)
7435 if (p->type == bfd_indirect_link_order
7436 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
7437 == bfd_target_elf_flavour)
7438 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
7440 s = p->u.indirect.section;
7441 elfsec = _bfd_elf_section_from_bfd_section (sub, s);
7442 if (elfsec != -1
7443 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER)
7444 seen_linkorder++;
7445 else
7446 seen_other++;
7448 else
7449 seen_other++;
7452 if (!seen_linkorder)
7453 return TRUE;
7455 if (seen_other && seen_linkorder)
7457 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
7459 bfd_set_error (bfd_error_bad_value);
7460 return FALSE;
7463 sections = (struct bfd_link_order **)
7464 xmalloc (seen_linkorder * sizeof (struct bfd_link_order *));
7465 seen_linkorder = 0;
7467 for (p = o->link_order_head; p != NULL; p = p->next)
7469 sections[seen_linkorder++] = p;
7471 /* Sort the input sections in the order of their linked section. */
7472 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
7473 compare_link_order);
7475 /* Change the offsets of the sections. */
7476 offset = 0;
7477 for (n = 0; n < seen_linkorder; n++)
7479 s = sections[n]->u.indirect.section;
7480 offset &= ~(bfd_vma)((1 << s->alignment_power) - 1);
7481 s->output_offset = offset;
7482 sections[n]->offset = offset;
7483 offset += sections[n]->size;
7486 return TRUE;
7490 /* Do the final step of an ELF link. */
7492 bfd_boolean
7493 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
7495 bfd_boolean dynamic;
7496 bfd_boolean emit_relocs;
7497 bfd *dynobj;
7498 struct elf_final_link_info finfo;
7499 register asection *o;
7500 register struct bfd_link_order *p;
7501 register bfd *sub;
7502 bfd_size_type max_contents_size;
7503 bfd_size_type max_external_reloc_size;
7504 bfd_size_type max_internal_reloc_count;
7505 bfd_size_type max_sym_count;
7506 bfd_size_type max_sym_shndx_count;
7507 file_ptr off;
7508 Elf_Internal_Sym elfsym;
7509 unsigned int i;
7510 Elf_Internal_Shdr *symtab_hdr;
7511 Elf_Internal_Shdr *symtab_shndx_hdr;
7512 Elf_Internal_Shdr *symstrtab_hdr;
7513 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7514 struct elf_outext_info eoinfo;
7515 bfd_boolean merged;
7516 size_t relativecount = 0;
7517 asection *reldyn = 0;
7518 bfd_size_type amt;
7520 if (! is_elf_hash_table (info->hash))
7521 return FALSE;
7523 if (info->shared)
7524 abfd->flags |= DYNAMIC;
7526 dynamic = elf_hash_table (info)->dynamic_sections_created;
7527 dynobj = elf_hash_table (info)->dynobj;
7529 emit_relocs = (info->relocatable
7530 || info->emitrelocations
7531 || bed->elf_backend_emit_relocs);
7533 finfo.info = info;
7534 finfo.output_bfd = abfd;
7535 finfo.symstrtab = _bfd_elf_stringtab_init ();
7536 if (finfo.symstrtab == NULL)
7537 return FALSE;
7539 if (! dynamic)
7541 finfo.dynsym_sec = NULL;
7542 finfo.hash_sec = NULL;
7543 finfo.symver_sec = NULL;
7545 else
7547 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
7548 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
7549 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
7550 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
7551 /* Note that it is OK if symver_sec is NULL. */
7554 finfo.contents = NULL;
7555 finfo.external_relocs = NULL;
7556 finfo.internal_relocs = NULL;
7557 finfo.external_syms = NULL;
7558 finfo.locsym_shndx = NULL;
7559 finfo.internal_syms = NULL;
7560 finfo.indices = NULL;
7561 finfo.sections = NULL;
7562 finfo.symbuf = NULL;
7563 finfo.symshndxbuf = NULL;
7564 finfo.symbuf_count = 0;
7565 finfo.shndxbuf_size = 0;
7567 /* Count up the number of relocations we will output for each output
7568 section, so that we know the sizes of the reloc sections. We
7569 also figure out some maximum sizes. */
7570 max_contents_size = 0;
7571 max_external_reloc_size = 0;
7572 max_internal_reloc_count = 0;
7573 max_sym_count = 0;
7574 max_sym_shndx_count = 0;
7575 merged = FALSE;
7576 for (o = abfd->sections; o != NULL; o = o->next)
7578 struct bfd_elf_section_data *esdo = elf_section_data (o);
7579 o->reloc_count = 0;
7581 for (p = o->link_order_head; p != NULL; p = p->next)
7583 unsigned int reloc_count = 0;
7584 struct bfd_elf_section_data *esdi = NULL;
7585 unsigned int *rel_count1;
7587 if (p->type == bfd_section_reloc_link_order
7588 || p->type == bfd_symbol_reloc_link_order)
7589 reloc_count = 1;
7590 else if (p->type == bfd_indirect_link_order)
7592 asection *sec;
7594 sec = p->u.indirect.section;
7595 esdi = elf_section_data (sec);
7597 /* Mark all sections which are to be included in the
7598 link. This will normally be every section. We need
7599 to do this so that we can identify any sections which
7600 the linker has decided to not include. */
7601 sec->linker_mark = TRUE;
7603 if (sec->flags & SEC_MERGE)
7604 merged = TRUE;
7606 if (info->relocatable || info->emitrelocations)
7607 reloc_count = sec->reloc_count;
7608 else if (bed->elf_backend_count_relocs)
7610 Elf_Internal_Rela * relocs;
7612 relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
7613 info->keep_memory);
7615 reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs);
7617 if (elf_section_data (o)->relocs != relocs)
7618 free (relocs);
7621 if (sec->rawsize > max_contents_size)
7622 max_contents_size = sec->rawsize;
7623 if (sec->size > max_contents_size)
7624 max_contents_size = sec->size;
7626 /* We are interested in just local symbols, not all
7627 symbols. */
7628 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
7629 && (sec->owner->flags & DYNAMIC) == 0)
7631 size_t sym_count;
7633 if (elf_bad_symtab (sec->owner))
7634 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
7635 / bed->s->sizeof_sym);
7636 else
7637 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
7639 if (sym_count > max_sym_count)
7640 max_sym_count = sym_count;
7642 if (sym_count > max_sym_shndx_count
7643 && elf_symtab_shndx (sec->owner) != 0)
7644 max_sym_shndx_count = sym_count;
7646 if ((sec->flags & SEC_RELOC) != 0)
7648 size_t ext_size;
7650 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
7651 if (ext_size > max_external_reloc_size)
7652 max_external_reloc_size = ext_size;
7653 if (sec->reloc_count > max_internal_reloc_count)
7654 max_internal_reloc_count = sec->reloc_count;
7659 if (reloc_count == 0)
7660 continue;
7662 o->reloc_count += reloc_count;
7664 /* MIPS may have a mix of REL and RELA relocs on sections.
7665 To support this curious ABI we keep reloc counts in
7666 elf_section_data too. We must be careful to add the
7667 relocations from the input section to the right output
7668 count. FIXME: Get rid of one count. We have
7669 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7670 rel_count1 = &esdo->rel_count;
7671 if (esdi != NULL)
7673 bfd_boolean same_size;
7674 bfd_size_type entsize1;
7676 entsize1 = esdi->rel_hdr.sh_entsize;
7677 BFD_ASSERT (entsize1 == bed->s->sizeof_rel
7678 || entsize1 == bed->s->sizeof_rela);
7679 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
7681 if (!same_size)
7682 rel_count1 = &esdo->rel_count2;
7684 if (esdi->rel_hdr2 != NULL)
7686 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
7687 unsigned int alt_count;
7688 unsigned int *rel_count2;
7690 BFD_ASSERT (entsize2 != entsize1
7691 && (entsize2 == bed->s->sizeof_rel
7692 || entsize2 == bed->s->sizeof_rela));
7694 rel_count2 = &esdo->rel_count2;
7695 if (!same_size)
7696 rel_count2 = &esdo->rel_count;
7698 /* The following is probably too simplistic if the
7699 backend counts output relocs unusually. */
7700 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
7701 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
7702 *rel_count2 += alt_count;
7703 reloc_count -= alt_count;
7706 *rel_count1 += reloc_count;
7709 if (o->reloc_count > 0)
7710 o->flags |= SEC_RELOC;
7711 else
7713 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7714 set it (this is probably a bug) and if it is set
7715 assign_section_numbers will create a reloc section. */
7716 o->flags &=~ SEC_RELOC;
7719 /* If the SEC_ALLOC flag is not set, force the section VMA to
7720 zero. This is done in elf_fake_sections as well, but forcing
7721 the VMA to 0 here will ensure that relocs against these
7722 sections are handled correctly. */
7723 if ((o->flags & SEC_ALLOC) == 0
7724 && ! o->user_set_vma)
7725 o->vma = 0;
7728 if (! info->relocatable && merged)
7729 elf_link_hash_traverse (elf_hash_table (info),
7730 _bfd_elf_link_sec_merge_syms, abfd);
7732 /* Figure out the file positions for everything but the symbol table
7733 and the relocs. We set symcount to force assign_section_numbers
7734 to create a symbol table. */
7735 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
7736 BFD_ASSERT (! abfd->output_has_begun);
7737 if (! _bfd_elf_compute_section_file_positions (abfd, info))
7738 goto error_return;
7740 /* Set sizes, and assign file positions for reloc sections. */
7741 for (o = abfd->sections; o != NULL; o = o->next)
7743 if ((o->flags & SEC_RELOC) != 0)
7745 if (!(_bfd_elf_link_size_reloc_section
7746 (abfd, &elf_section_data (o)->rel_hdr, o)))
7747 goto error_return;
7749 if (elf_section_data (o)->rel_hdr2
7750 && !(_bfd_elf_link_size_reloc_section
7751 (abfd, elf_section_data (o)->rel_hdr2, o)))
7752 goto error_return;
7755 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
7756 to count upwards while actually outputting the relocations. */
7757 elf_section_data (o)->rel_count = 0;
7758 elf_section_data (o)->rel_count2 = 0;
7761 _bfd_elf_assign_file_positions_for_relocs (abfd);
7763 /* We have now assigned file positions for all the sections except
7764 .symtab and .strtab. We start the .symtab section at the current
7765 file position, and write directly to it. We build the .strtab
7766 section in memory. */
7767 bfd_get_symcount (abfd) = 0;
7768 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7769 /* sh_name is set in prep_headers. */
7770 symtab_hdr->sh_type = SHT_SYMTAB;
7771 /* sh_flags, sh_addr and sh_size all start off zero. */
7772 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
7773 /* sh_link is set in assign_section_numbers. */
7774 /* sh_info is set below. */
7775 /* sh_offset is set just below. */
7776 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
7778 off = elf_tdata (abfd)->next_file_pos;
7779 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
7781 /* Note that at this point elf_tdata (abfd)->next_file_pos is
7782 incorrect. We do not yet know the size of the .symtab section.
7783 We correct next_file_pos below, after we do know the size. */
7785 /* Allocate a buffer to hold swapped out symbols. This is to avoid
7786 continuously seeking to the right position in the file. */
7787 if (! info->keep_memory || max_sym_count < 20)
7788 finfo.symbuf_size = 20;
7789 else
7790 finfo.symbuf_size = max_sym_count;
7791 amt = finfo.symbuf_size;
7792 amt *= bed->s->sizeof_sym;
7793 finfo.symbuf = bfd_malloc (amt);
7794 if (finfo.symbuf == NULL)
7795 goto error_return;
7796 if (elf_numsections (abfd) > SHN_LORESERVE)
7798 /* Wild guess at number of output symbols. realloc'd as needed. */
7799 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
7800 finfo.shndxbuf_size = amt;
7801 amt *= sizeof (Elf_External_Sym_Shndx);
7802 finfo.symshndxbuf = bfd_zmalloc (amt);
7803 if (finfo.symshndxbuf == NULL)
7804 goto error_return;
7807 /* Start writing out the symbol table. The first symbol is always a
7808 dummy symbol. */
7809 if (info->strip != strip_all
7810 || emit_relocs)
7812 elfsym.st_value = 0;
7813 elfsym.st_size = 0;
7814 elfsym.st_info = 0;
7815 elfsym.st_other = 0;
7816 elfsym.st_shndx = SHN_UNDEF;
7817 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
7818 NULL))
7819 goto error_return;
7822 #if 0
7823 /* Some standard ELF linkers do this, but we don't because it causes
7824 bootstrap comparison failures. */
7825 /* Output a file symbol for the output file as the second symbol.
7826 We output this even if we are discarding local symbols, although
7827 I'm not sure if this is correct. */
7828 elfsym.st_value = 0;
7829 elfsym.st_size = 0;
7830 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
7831 elfsym.st_other = 0;
7832 elfsym.st_shndx = SHN_ABS;
7833 if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd),
7834 &elfsym, bfd_abs_section_ptr, NULL))
7835 goto error_return;
7836 #endif
7838 /* Output a symbol for each section. We output these even if we are
7839 discarding local symbols, since they are used for relocs. These
7840 symbols have no names. We store the index of each one in the
7841 index field of the section, so that we can find it again when
7842 outputting relocs. */
7843 if (info->strip != strip_all
7844 || emit_relocs)
7846 elfsym.st_size = 0;
7847 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
7848 elfsym.st_other = 0;
7849 for (i = 1; i < elf_numsections (abfd); i++)
7851 o = bfd_section_from_elf_index (abfd, i);
7852 if (o != NULL)
7853 o->target_index = bfd_get_symcount (abfd);
7854 elfsym.st_shndx = i;
7855 if (info->relocatable || o == NULL)
7856 elfsym.st_value = 0;
7857 else
7858 elfsym.st_value = o->vma;
7859 if (! elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
7860 goto error_return;
7861 if (i == SHN_LORESERVE - 1)
7862 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
7866 /* Allocate some memory to hold information read in from the input
7867 files. */
7868 if (max_contents_size != 0)
7870 finfo.contents = bfd_malloc (max_contents_size);
7871 if (finfo.contents == NULL)
7872 goto error_return;
7875 if (max_external_reloc_size != 0)
7877 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
7878 if (finfo.external_relocs == NULL)
7879 goto error_return;
7882 if (max_internal_reloc_count != 0)
7884 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
7885 amt *= sizeof (Elf_Internal_Rela);
7886 finfo.internal_relocs = bfd_malloc (amt);
7887 if (finfo.internal_relocs == NULL)
7888 goto error_return;
7891 if (max_sym_count != 0)
7893 amt = max_sym_count * bed->s->sizeof_sym;
7894 finfo.external_syms = bfd_malloc (amt);
7895 if (finfo.external_syms == NULL)
7896 goto error_return;
7898 amt = max_sym_count * sizeof (Elf_Internal_Sym);
7899 finfo.internal_syms = bfd_malloc (amt);
7900 if (finfo.internal_syms == NULL)
7901 goto error_return;
7903 amt = max_sym_count * sizeof (long);
7904 finfo.indices = bfd_malloc (amt);
7905 if (finfo.indices == NULL)
7906 goto error_return;
7908 amt = max_sym_count * sizeof (asection *);
7909 finfo.sections = bfd_malloc (amt);
7910 if (finfo.sections == NULL)
7911 goto error_return;
7914 if (max_sym_shndx_count != 0)
7916 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
7917 finfo.locsym_shndx = bfd_malloc (amt);
7918 if (finfo.locsym_shndx == NULL)
7919 goto error_return;
7922 if (elf_hash_table (info)->tls_sec)
7924 bfd_vma base, end = 0;
7925 asection *sec;
7927 for (sec = elf_hash_table (info)->tls_sec;
7928 sec && (sec->flags & SEC_THREAD_LOCAL);
7929 sec = sec->next)
7931 bfd_vma size = sec->size;
7933 if (size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0)
7935 struct bfd_link_order *o;
7937 for (o = sec->link_order_head; o != NULL; o = o->next)
7938 if (size < o->offset + o->size)
7939 size = o->offset + o->size;
7941 end = sec->vma + size;
7943 base = elf_hash_table (info)->tls_sec->vma;
7944 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
7945 elf_hash_table (info)->tls_size = end - base;
7948 /* Reorder SHF_LINK_ORDER sections. */
7949 for (o = abfd->sections; o != NULL; o = o->next)
7951 if (!elf_fixup_link_order (abfd, o))
7952 return FALSE;
7955 /* Since ELF permits relocations to be against local symbols, we
7956 must have the local symbols available when we do the relocations.
7957 Since we would rather only read the local symbols once, and we
7958 would rather not keep them in memory, we handle all the
7959 relocations for a single input file at the same time.
7961 Unfortunately, there is no way to know the total number of local
7962 symbols until we have seen all of them, and the local symbol
7963 indices precede the global symbol indices. This means that when
7964 we are generating relocatable output, and we see a reloc against
7965 a global symbol, we can not know the symbol index until we have
7966 finished examining all the local symbols to see which ones we are
7967 going to output. To deal with this, we keep the relocations in
7968 memory, and don't output them until the end of the link. This is
7969 an unfortunate waste of memory, but I don't see a good way around
7970 it. Fortunately, it only happens when performing a relocatable
7971 link, which is not the common case. FIXME: If keep_memory is set
7972 we could write the relocs out and then read them again; I don't
7973 know how bad the memory loss will be. */
7975 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
7976 sub->output_has_begun = FALSE;
7977 for (o = abfd->sections; o != NULL; o = o->next)
7979 for (p = o->link_order_head; p != NULL; p = p->next)
7981 if (p->type == bfd_indirect_link_order
7982 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
7983 == bfd_target_elf_flavour)
7984 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
7986 if (! sub->output_has_begun)
7988 if (! elf_link_input_bfd (&finfo, sub))
7989 goto error_return;
7990 sub->output_has_begun = TRUE;
7993 else if (p->type == bfd_section_reloc_link_order
7994 || p->type == bfd_symbol_reloc_link_order)
7996 if (! elf_reloc_link_order (abfd, info, o, p))
7997 goto error_return;
7999 else
8001 if (! _bfd_default_link_order (abfd, info, o, p))
8002 goto error_return;
8007 /* Output any global symbols that got converted to local in a
8008 version script or due to symbol visibility. We do this in a
8009 separate step since ELF requires all local symbols to appear
8010 prior to any global symbols. FIXME: We should only do this if
8011 some global symbols were, in fact, converted to become local.
8012 FIXME: Will this work correctly with the Irix 5 linker? */
8013 eoinfo.failed = FALSE;
8014 eoinfo.finfo = &finfo;
8015 eoinfo.localsyms = TRUE;
8016 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
8017 &eoinfo);
8018 if (eoinfo.failed)
8019 return FALSE;
8021 /* That wrote out all the local symbols. Finish up the symbol table
8022 with the global symbols. Even if we want to strip everything we
8023 can, we still need to deal with those global symbols that got
8024 converted to local in a version script. */
8026 /* The sh_info field records the index of the first non local symbol. */
8027 symtab_hdr->sh_info = bfd_get_symcount (abfd);
8029 if (dynamic
8030 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
8032 Elf_Internal_Sym sym;
8033 bfd_byte *dynsym = finfo.dynsym_sec->contents;
8034 long last_local = 0;
8036 /* Write out the section symbols for the output sections. */
8037 if (info->shared)
8039 asection *s;
8041 sym.st_size = 0;
8042 sym.st_name = 0;
8043 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8044 sym.st_other = 0;
8046 for (s = abfd->sections; s != NULL; s = s->next)
8048 int indx;
8049 bfd_byte *dest;
8050 long dynindx;
8052 dynindx = elf_section_data (s)->dynindx;
8053 if (dynindx <= 0)
8054 continue;
8055 indx = elf_section_data (s)->this_idx;
8056 BFD_ASSERT (indx > 0);
8057 sym.st_shndx = indx;
8058 sym.st_value = s->vma;
8059 dest = dynsym + dynindx * bed->s->sizeof_sym;
8060 if (last_local < dynindx)
8061 last_local = dynindx;
8062 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
8066 /* Write out the local dynsyms. */
8067 if (elf_hash_table (info)->dynlocal)
8069 struct elf_link_local_dynamic_entry *e;
8070 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
8072 asection *s;
8073 bfd_byte *dest;
8075 sym.st_size = e->isym.st_size;
8076 sym.st_other = e->isym.st_other;
8078 /* Copy the internal symbol as is.
8079 Note that we saved a word of storage and overwrote
8080 the original st_name with the dynstr_index. */
8081 sym = e->isym;
8083 if (e->isym.st_shndx != SHN_UNDEF
8084 && (e->isym.st_shndx < SHN_LORESERVE
8085 || e->isym.st_shndx > SHN_HIRESERVE))
8087 s = bfd_section_from_elf_index (e->input_bfd,
8088 e->isym.st_shndx);
8090 sym.st_shndx =
8091 elf_section_data (s->output_section)->this_idx;
8092 sym.st_value = (s->output_section->vma
8093 + s->output_offset
8094 + e->isym.st_value);
8097 if (last_local < e->dynindx)
8098 last_local = e->dynindx;
8100 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
8101 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
8105 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
8106 last_local + 1;
8109 /* We get the global symbols from the hash table. */
8110 eoinfo.failed = FALSE;
8111 eoinfo.localsyms = FALSE;
8112 eoinfo.finfo = &finfo;
8113 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
8114 &eoinfo);
8115 if (eoinfo.failed)
8116 return FALSE;
8118 /* If backend needs to output some symbols not present in the hash
8119 table, do it now. */
8120 if (bed->elf_backend_output_arch_syms)
8122 typedef bfd_boolean (*out_sym_func)
8123 (void *, const char *, Elf_Internal_Sym *, asection *,
8124 struct elf_link_hash_entry *);
8126 if (! ((*bed->elf_backend_output_arch_syms)
8127 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
8128 return FALSE;
8131 /* Flush all symbols to the file. */
8132 if (! elf_link_flush_output_syms (&finfo, bed))
8133 return FALSE;
8135 /* Now we know the size of the symtab section. */
8136 off += symtab_hdr->sh_size;
8138 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
8139 if (symtab_shndx_hdr->sh_name != 0)
8141 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8142 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8143 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8144 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
8145 symtab_shndx_hdr->sh_size = amt;
8147 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
8148 off, TRUE);
8150 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
8151 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
8152 return FALSE;
8156 /* Finish up and write out the symbol string table (.strtab)
8157 section. */
8158 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8159 /* sh_name was set in prep_headers. */
8160 symstrtab_hdr->sh_type = SHT_STRTAB;
8161 symstrtab_hdr->sh_flags = 0;
8162 symstrtab_hdr->sh_addr = 0;
8163 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
8164 symstrtab_hdr->sh_entsize = 0;
8165 symstrtab_hdr->sh_link = 0;
8166 symstrtab_hdr->sh_info = 0;
8167 /* sh_offset is set just below. */
8168 symstrtab_hdr->sh_addralign = 1;
8170 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
8171 elf_tdata (abfd)->next_file_pos = off;
8173 if (bfd_get_symcount (abfd) > 0)
8175 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
8176 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
8177 return FALSE;
8180 /* Adjust the relocs to have the correct symbol indices. */
8181 for (o = abfd->sections; o != NULL; o = o->next)
8183 if ((o->flags & SEC_RELOC) == 0)
8184 continue;
8186 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
8187 elf_section_data (o)->rel_count,
8188 elf_section_data (o)->rel_hashes);
8189 if (elf_section_data (o)->rel_hdr2 != NULL)
8190 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
8191 elf_section_data (o)->rel_count2,
8192 (elf_section_data (o)->rel_hashes
8193 + elf_section_data (o)->rel_count));
8195 /* Set the reloc_count field to 0 to prevent write_relocs from
8196 trying to swap the relocs out itself. */
8197 o->reloc_count = 0;
8200 if (dynamic && info->combreloc && dynobj != NULL)
8201 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
8203 /* If we are linking against a dynamic object, or generating a
8204 shared library, finish up the dynamic linking information. */
8205 if (dynamic)
8207 bfd_byte *dyncon, *dynconend;
8209 /* Fix up .dynamic entries. */
8210 o = bfd_get_section_by_name (dynobj, ".dynamic");
8211 BFD_ASSERT (o != NULL);
8213 dyncon = o->contents;
8214 dynconend = o->contents + o->size;
8215 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
8217 Elf_Internal_Dyn dyn;
8218 const char *name;
8219 unsigned int type;
8221 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
8223 switch (dyn.d_tag)
8225 default:
8226 continue;
8227 case DT_NULL:
8228 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
8230 switch (elf_section_data (reldyn)->this_hdr.sh_type)
8232 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
8233 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
8234 default: continue;
8236 dyn.d_un.d_val = relativecount;
8237 relativecount = 0;
8238 break;
8240 continue;
8242 case DT_INIT:
8243 name = info->init_function;
8244 goto get_sym;
8245 case DT_FINI:
8246 name = info->fini_function;
8247 get_sym:
8249 struct elf_link_hash_entry *h;
8251 h = elf_link_hash_lookup (elf_hash_table (info), name,
8252 FALSE, FALSE, TRUE);
8253 if (h != NULL
8254 && (h->root.type == bfd_link_hash_defined
8255 || h->root.type == bfd_link_hash_defweak))
8257 dyn.d_un.d_val = h->root.u.def.value;
8258 o = h->root.u.def.section;
8259 if (o->output_section != NULL)
8260 dyn.d_un.d_val += (o->output_section->vma
8261 + o->output_offset);
8262 else
8264 /* The symbol is imported from another shared
8265 library and does not apply to this one. */
8266 dyn.d_un.d_val = 0;
8268 break;
8271 continue;
8273 case DT_PREINIT_ARRAYSZ:
8274 name = ".preinit_array";
8275 goto get_size;
8276 case DT_INIT_ARRAYSZ:
8277 name = ".init_array";
8278 goto get_size;
8279 case DT_FINI_ARRAYSZ:
8280 name = ".fini_array";
8281 get_size:
8282 o = bfd_get_section_by_name (abfd, name);
8283 if (o == NULL)
8285 (*_bfd_error_handler)
8286 (_("%B: could not find output section %s"), abfd, name);
8287 goto error_return;
8289 if (o->size == 0)
8290 (*_bfd_error_handler)
8291 (_("warning: %s section has zero size"), name);
8292 dyn.d_un.d_val = o->size;
8293 break;
8295 case DT_PREINIT_ARRAY:
8296 name = ".preinit_array";
8297 goto get_vma;
8298 case DT_INIT_ARRAY:
8299 name = ".init_array";
8300 goto get_vma;
8301 case DT_FINI_ARRAY:
8302 name = ".fini_array";
8303 goto get_vma;
8305 case DT_HASH:
8306 name = ".hash";
8307 goto get_vma;
8308 case DT_STRTAB:
8309 name = ".dynstr";
8310 goto get_vma;
8311 case DT_SYMTAB:
8312 name = ".dynsym";
8313 goto get_vma;
8314 case DT_VERDEF:
8315 name = ".gnu.version_d";
8316 goto get_vma;
8317 case DT_VERNEED:
8318 name = ".gnu.version_r";
8319 goto get_vma;
8320 case DT_VERSYM:
8321 name = ".gnu.version";
8322 get_vma:
8323 o = bfd_get_section_by_name (abfd, name);
8324 if (o == NULL)
8326 (*_bfd_error_handler)
8327 (_("%B: could not find output section %s"), abfd, name);
8328 goto error_return;
8330 dyn.d_un.d_ptr = o->vma;
8331 break;
8333 case DT_REL:
8334 case DT_RELA:
8335 case DT_RELSZ:
8336 case DT_RELASZ:
8337 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
8338 type = SHT_REL;
8339 else
8340 type = SHT_RELA;
8341 dyn.d_un.d_val = 0;
8342 for (i = 1; i < elf_numsections (abfd); i++)
8344 Elf_Internal_Shdr *hdr;
8346 hdr = elf_elfsections (abfd)[i];
8347 if (hdr->sh_type == type
8348 && (hdr->sh_flags & SHF_ALLOC) != 0)
8350 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
8351 dyn.d_un.d_val += hdr->sh_size;
8352 else
8354 if (dyn.d_un.d_val == 0
8355 || hdr->sh_addr < dyn.d_un.d_val)
8356 dyn.d_un.d_val = hdr->sh_addr;
8360 break;
8362 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
8366 /* If we have created any dynamic sections, then output them. */
8367 if (dynobj != NULL)
8369 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
8370 goto error_return;
8372 for (o = dynobj->sections; o != NULL; o = o->next)
8374 if ((o->flags & SEC_HAS_CONTENTS) == 0
8375 || o->size == 0
8376 || o->output_section == bfd_abs_section_ptr)
8377 continue;
8378 if ((o->flags & SEC_LINKER_CREATED) == 0)
8380 /* At this point, we are only interested in sections
8381 created by _bfd_elf_link_create_dynamic_sections. */
8382 continue;
8384 if (elf_hash_table (info)->stab_info.stabstr == o)
8385 continue;
8386 if (elf_hash_table (info)->eh_info.hdr_sec == o)
8387 continue;
8388 if ((elf_section_data (o->output_section)->this_hdr.sh_type
8389 != SHT_STRTAB)
8390 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
8392 if (! bfd_set_section_contents (abfd, o->output_section,
8393 o->contents,
8394 (file_ptr) o->output_offset,
8395 o->size))
8396 goto error_return;
8398 else
8400 /* The contents of the .dynstr section are actually in a
8401 stringtab. */
8402 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
8403 if (bfd_seek (abfd, off, SEEK_SET) != 0
8404 || ! _bfd_elf_strtab_emit (abfd,
8405 elf_hash_table (info)->dynstr))
8406 goto error_return;
8411 if (info->relocatable)
8413 bfd_boolean failed = FALSE;
8415 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
8416 if (failed)
8417 goto error_return;
8420 /* If we have optimized stabs strings, output them. */
8421 if (elf_hash_table (info)->stab_info.stabstr != NULL)
8423 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
8424 goto error_return;
8427 if (info->eh_frame_hdr)
8429 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
8430 goto error_return;
8433 if (finfo.symstrtab != NULL)
8434 _bfd_stringtab_free (finfo.symstrtab);
8435 if (finfo.contents != NULL)
8436 free (finfo.contents);
8437 if (finfo.external_relocs != NULL)
8438 free (finfo.external_relocs);
8439 if (finfo.internal_relocs != NULL)
8440 free (finfo.internal_relocs);
8441 if (finfo.external_syms != NULL)
8442 free (finfo.external_syms);
8443 if (finfo.locsym_shndx != NULL)
8444 free (finfo.locsym_shndx);
8445 if (finfo.internal_syms != NULL)
8446 free (finfo.internal_syms);
8447 if (finfo.indices != NULL)
8448 free (finfo.indices);
8449 if (finfo.sections != NULL)
8450 free (finfo.sections);
8451 if (finfo.symbuf != NULL)
8452 free (finfo.symbuf);
8453 if (finfo.symshndxbuf != NULL)
8454 free (finfo.symshndxbuf);
8455 for (o = abfd->sections; o != NULL; o = o->next)
8457 if ((o->flags & SEC_RELOC) != 0
8458 && elf_section_data (o)->rel_hashes != NULL)
8459 free (elf_section_data (o)->rel_hashes);
8462 elf_tdata (abfd)->linker = TRUE;
8464 return TRUE;
8466 error_return:
8467 if (finfo.symstrtab != NULL)
8468 _bfd_stringtab_free (finfo.symstrtab);
8469 if (finfo.contents != NULL)
8470 free (finfo.contents);
8471 if (finfo.external_relocs != NULL)
8472 free (finfo.external_relocs);
8473 if (finfo.internal_relocs != NULL)
8474 free (finfo.internal_relocs);
8475 if (finfo.external_syms != NULL)
8476 free (finfo.external_syms);
8477 if (finfo.locsym_shndx != NULL)
8478 free (finfo.locsym_shndx);
8479 if (finfo.internal_syms != NULL)
8480 free (finfo.internal_syms);
8481 if (finfo.indices != NULL)
8482 free (finfo.indices);
8483 if (finfo.sections != NULL)
8484 free (finfo.sections);
8485 if (finfo.symbuf != NULL)
8486 free (finfo.symbuf);
8487 if (finfo.symshndxbuf != NULL)
8488 free (finfo.symshndxbuf);
8489 for (o = abfd->sections; o != NULL; o = o->next)
8491 if ((o->flags & SEC_RELOC) != 0
8492 && elf_section_data (o)->rel_hashes != NULL)
8493 free (elf_section_data (o)->rel_hashes);
8496 return FALSE;
8499 /* Garbage collect unused sections. */
8501 /* The mark phase of garbage collection. For a given section, mark
8502 it and any sections in this section's group, and all the sections
8503 which define symbols to which it refers. */
8505 typedef asection * (*gc_mark_hook_fn)
8506 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
8507 struct elf_link_hash_entry *, Elf_Internal_Sym *);
8509 bfd_boolean
8510 _bfd_elf_gc_mark (struct bfd_link_info *info,
8511 asection *sec,
8512 gc_mark_hook_fn gc_mark_hook)
8514 bfd_boolean ret;
8515 asection *group_sec;
8517 sec->gc_mark = 1;
8519 /* Mark all the sections in the group. */
8520 group_sec = elf_section_data (sec)->next_in_group;
8521 if (group_sec && !group_sec->gc_mark)
8522 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
8523 return FALSE;
8525 /* Look through the section relocs. */
8526 ret = TRUE;
8527 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
8529 Elf_Internal_Rela *relstart, *rel, *relend;
8530 Elf_Internal_Shdr *symtab_hdr;
8531 struct elf_link_hash_entry **sym_hashes;
8532 size_t nlocsyms;
8533 size_t extsymoff;
8534 bfd *input_bfd = sec->owner;
8535 const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
8536 Elf_Internal_Sym *isym = NULL;
8537 int r_sym_shift;
8539 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8540 sym_hashes = elf_sym_hashes (input_bfd);
8542 /* Read the local symbols. */
8543 if (elf_bad_symtab (input_bfd))
8545 nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym;
8546 extsymoff = 0;
8548 else
8549 extsymoff = nlocsyms = symtab_hdr->sh_info;
8551 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
8552 if (isym == NULL && nlocsyms != 0)
8554 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
8555 NULL, NULL, NULL);
8556 if (isym == NULL)
8557 return FALSE;
8560 /* Read the relocations. */
8561 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
8562 info->keep_memory);
8563 if (relstart == NULL)
8565 ret = FALSE;
8566 goto out1;
8568 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
8570 if (bed->s->arch_size == 32)
8571 r_sym_shift = 8;
8572 else
8573 r_sym_shift = 32;
8575 for (rel = relstart; rel < relend; rel++)
8577 unsigned long r_symndx;
8578 asection *rsec;
8579 struct elf_link_hash_entry *h;
8581 r_symndx = rel->r_info >> r_sym_shift;
8582 if (r_symndx == 0)
8583 continue;
8585 if (r_symndx >= nlocsyms
8586 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
8588 h = sym_hashes[r_symndx - extsymoff];
8589 while (h->root.type == bfd_link_hash_indirect
8590 || h->root.type == bfd_link_hash_warning)
8591 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8592 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
8594 else
8596 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
8599 if (rsec && !rsec->gc_mark)
8601 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
8602 rsec->gc_mark = 1;
8603 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
8605 ret = FALSE;
8606 goto out2;
8611 out2:
8612 if (elf_section_data (sec)->relocs != relstart)
8613 free (relstart);
8614 out1:
8615 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
8617 if (! info->keep_memory)
8618 free (isym);
8619 else
8620 symtab_hdr->contents = (unsigned char *) isym;
8624 return ret;
8627 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8629 static bfd_boolean
8630 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *idxptr)
8632 int *idx = idxptr;
8634 if (h->root.type == bfd_link_hash_warning)
8635 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8637 if (h->dynindx != -1
8638 && ((h->root.type != bfd_link_hash_defined
8639 && h->root.type != bfd_link_hash_defweak)
8640 || h->root.u.def.section->gc_mark))
8641 h->dynindx = (*idx)++;
8643 return TRUE;
8646 /* The sweep phase of garbage collection. Remove all garbage sections. */
8648 typedef bfd_boolean (*gc_sweep_hook_fn)
8649 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
8651 static bfd_boolean
8652 elf_gc_sweep (struct bfd_link_info *info, gc_sweep_hook_fn gc_sweep_hook)
8654 bfd *sub;
8656 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8658 asection *o;
8660 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
8661 continue;
8663 for (o = sub->sections; o != NULL; o = o->next)
8665 /* Keep debug and special sections. */
8666 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
8667 || (o->flags & (SEC_ALLOC | SEC_LOAD)) == 0)
8668 o->gc_mark = 1;
8670 if (o->gc_mark)
8671 continue;
8673 /* Skip sweeping sections already excluded. */
8674 if (o->flags & SEC_EXCLUDE)
8675 continue;
8677 /* Since this is early in the link process, it is simple
8678 to remove a section from the output. */
8679 o->flags |= SEC_EXCLUDE;
8681 /* But we also have to update some of the relocation
8682 info we collected before. */
8683 if (gc_sweep_hook
8684 && (o->flags & SEC_RELOC) && o->reloc_count > 0)
8686 Elf_Internal_Rela *internal_relocs;
8687 bfd_boolean r;
8689 internal_relocs
8690 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
8691 info->keep_memory);
8692 if (internal_relocs == NULL)
8693 return FALSE;
8695 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
8697 if (elf_section_data (o)->relocs != internal_relocs)
8698 free (internal_relocs);
8700 if (!r)
8701 return FALSE;
8706 /* Remove the symbols that were in the swept sections from the dynamic
8707 symbol table. GCFIXME: Anyone know how to get them out of the
8708 static symbol table as well? */
8710 int i = 0;
8712 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, &i);
8714 elf_hash_table (info)->dynsymcount = i;
8717 return TRUE;
8720 /* Propagate collected vtable information. This is called through
8721 elf_link_hash_traverse. */
8723 static bfd_boolean
8724 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
8726 if (h->root.type == bfd_link_hash_warning)
8727 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8729 /* Those that are not vtables. */
8730 if (h->vtable == NULL || h->vtable->parent == NULL)
8731 return TRUE;
8733 /* Those vtables that do not have parents, we cannot merge. */
8734 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
8735 return TRUE;
8737 /* If we've already been done, exit. */
8738 if (h->vtable->used && h->vtable->used[-1])
8739 return TRUE;
8741 /* Make sure the parent's table is up to date. */
8742 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
8744 if (h->vtable->used == NULL)
8746 /* None of this table's entries were referenced. Re-use the
8747 parent's table. */
8748 h->vtable->used = h->vtable->parent->vtable->used;
8749 h->vtable->size = h->vtable->parent->vtable->size;
8751 else
8753 size_t n;
8754 bfd_boolean *cu, *pu;
8756 /* Or the parent's entries into ours. */
8757 cu = h->vtable->used;
8758 cu[-1] = TRUE;
8759 pu = h->vtable->parent->vtable->used;
8760 if (pu != NULL)
8762 const struct elf_backend_data *bed;
8763 unsigned int log_file_align;
8765 bed = get_elf_backend_data (h->root.u.def.section->owner);
8766 log_file_align = bed->s->log_file_align;
8767 n = h->vtable->parent->vtable->size >> log_file_align;
8768 while (n--)
8770 if (*pu)
8771 *cu = TRUE;
8772 pu++;
8773 cu++;
8778 return TRUE;
8781 static bfd_boolean
8782 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
8784 asection *sec;
8785 bfd_vma hstart, hend;
8786 Elf_Internal_Rela *relstart, *relend, *rel;
8787 const struct elf_backend_data *bed;
8788 unsigned int log_file_align;
8790 if (h->root.type == bfd_link_hash_warning)
8791 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8793 /* Take care of both those symbols that do not describe vtables as
8794 well as those that are not loaded. */
8795 if (h->vtable == NULL || h->vtable->parent == NULL)
8796 return TRUE;
8798 BFD_ASSERT (h->root.type == bfd_link_hash_defined
8799 || h->root.type == bfd_link_hash_defweak);
8801 sec = h->root.u.def.section;
8802 hstart = h->root.u.def.value;
8803 hend = hstart + h->size;
8805 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
8806 if (!relstart)
8807 return *(bfd_boolean *) okp = FALSE;
8808 bed = get_elf_backend_data (sec->owner);
8809 log_file_align = bed->s->log_file_align;
8811 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
8813 for (rel = relstart; rel < relend; ++rel)
8814 if (rel->r_offset >= hstart && rel->r_offset < hend)
8816 /* If the entry is in use, do nothing. */
8817 if (h->vtable->used
8818 && (rel->r_offset - hstart) < h->vtable->size)
8820 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
8821 if (h->vtable->used[entry])
8822 continue;
8824 /* Otherwise, kill it. */
8825 rel->r_offset = rel->r_info = rel->r_addend = 0;
8828 return TRUE;
8831 /* Mark sections containing dynamically referenced symbols. This is called
8832 through elf_link_hash_traverse. */
8834 static bfd_boolean
8835 elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h,
8836 void *okp ATTRIBUTE_UNUSED)
8838 if (h->root.type == bfd_link_hash_warning)
8839 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8841 if ((h->root.type == bfd_link_hash_defined
8842 || h->root.type == bfd_link_hash_defweak)
8843 && h->ref_dynamic)
8844 h->root.u.def.section->flags |= SEC_KEEP;
8846 return TRUE;
8849 /* Do mark and sweep of unused sections. */
8851 bfd_boolean
8852 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
8854 bfd_boolean ok = TRUE;
8855 bfd *sub;
8856 asection * (*gc_mark_hook)
8857 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
8858 struct elf_link_hash_entry *h, Elf_Internal_Sym *);
8860 if (!get_elf_backend_data (abfd)->can_gc_sections
8861 || info->relocatable
8862 || info->emitrelocations
8863 || info->shared
8864 || !is_elf_hash_table (info->hash))
8866 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
8867 return TRUE;
8870 /* Apply transitive closure to the vtable entry usage info. */
8871 elf_link_hash_traverse (elf_hash_table (info),
8872 elf_gc_propagate_vtable_entries_used,
8873 &ok);
8874 if (!ok)
8875 return FALSE;
8877 /* Kill the vtable relocations that were not used. */
8878 elf_link_hash_traverse (elf_hash_table (info),
8879 elf_gc_smash_unused_vtentry_relocs,
8880 &ok);
8881 if (!ok)
8882 return FALSE;
8884 /* Mark dynamically referenced symbols. */
8885 if (elf_hash_table (info)->dynamic_sections_created)
8886 elf_link_hash_traverse (elf_hash_table (info),
8887 elf_gc_mark_dynamic_ref_symbol,
8888 &ok);
8889 if (!ok)
8890 return FALSE;
8892 /* Grovel through relocs to find out who stays ... */
8893 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
8894 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8896 asection *o;
8898 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
8899 continue;
8901 for (o = sub->sections; o != NULL; o = o->next)
8903 if (o->flags & SEC_KEEP)
8905 /* _bfd_elf_discard_section_eh_frame knows how to discard
8906 orphaned FDEs so don't mark sections referenced by the
8907 EH frame section. */
8908 if (strcmp (o->name, ".eh_frame") == 0)
8909 o->gc_mark = 1;
8910 else if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
8911 return FALSE;
8916 /* ... and mark SEC_EXCLUDE for those that go. */
8917 if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook))
8918 return FALSE;
8920 return TRUE;
8923 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
8925 bfd_boolean
8926 bfd_elf_gc_record_vtinherit (bfd *abfd,
8927 asection *sec,
8928 struct elf_link_hash_entry *h,
8929 bfd_vma offset)
8931 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
8932 struct elf_link_hash_entry **search, *child;
8933 bfd_size_type extsymcount;
8934 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8936 /* The sh_info field of the symtab header tells us where the
8937 external symbols start. We don't care about the local symbols at
8938 this point. */
8939 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
8940 if (!elf_bad_symtab (abfd))
8941 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
8943 sym_hashes = elf_sym_hashes (abfd);
8944 sym_hashes_end = sym_hashes + extsymcount;
8946 /* Hunt down the child symbol, which is in this section at the same
8947 offset as the relocation. */
8948 for (search = sym_hashes; search != sym_hashes_end; ++search)
8950 if ((child = *search) != NULL
8951 && (child->root.type == bfd_link_hash_defined
8952 || child->root.type == bfd_link_hash_defweak)
8953 && child->root.u.def.section == sec
8954 && child->root.u.def.value == offset)
8955 goto win;
8958 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
8959 abfd, sec, (unsigned long) offset);
8960 bfd_set_error (bfd_error_invalid_operation);
8961 return FALSE;
8963 win:
8964 if (!child->vtable)
8966 child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable));
8967 if (!child->vtable)
8968 return FALSE;
8970 if (!h)
8972 /* This *should* only be the absolute section. It could potentially
8973 be that someone has defined a non-global vtable though, which
8974 would be bad. It isn't worth paging in the local symbols to be
8975 sure though; that case should simply be handled by the assembler. */
8977 child->vtable->parent = (struct elf_link_hash_entry *) -1;
8979 else
8980 child->vtable->parent = h;
8982 return TRUE;
8985 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
8987 bfd_boolean
8988 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
8989 asection *sec ATTRIBUTE_UNUSED,
8990 struct elf_link_hash_entry *h,
8991 bfd_vma addend)
8993 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8994 unsigned int log_file_align = bed->s->log_file_align;
8996 if (!h->vtable)
8998 h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable));
8999 if (!h->vtable)
9000 return FALSE;
9003 if (addend >= h->vtable->size)
9005 size_t size, bytes, file_align;
9006 bfd_boolean *ptr = h->vtable->used;
9008 /* While the symbol is undefined, we have to be prepared to handle
9009 a zero size. */
9010 file_align = 1 << log_file_align;
9011 if (h->root.type == bfd_link_hash_undefined)
9012 size = addend + file_align;
9013 else
9015 size = h->size;
9016 if (addend >= size)
9018 /* Oops! We've got a reference past the defined end of
9019 the table. This is probably a bug -- shall we warn? */
9020 size = addend + file_align;
9023 size = (size + file_align - 1) & -file_align;
9025 /* Allocate one extra entry for use as a "done" flag for the
9026 consolidation pass. */
9027 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
9029 if (ptr)
9031 ptr = bfd_realloc (ptr - 1, bytes);
9033 if (ptr != NULL)
9035 size_t oldbytes;
9037 oldbytes = (((h->vtable->size >> log_file_align) + 1)
9038 * sizeof (bfd_boolean));
9039 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
9042 else
9043 ptr = bfd_zmalloc (bytes);
9045 if (ptr == NULL)
9046 return FALSE;
9048 /* And arrange for that done flag to be at index -1. */
9049 h->vtable->used = ptr + 1;
9050 h->vtable->size = size;
9053 h->vtable->used[addend >> log_file_align] = TRUE;
9055 return TRUE;
9058 struct alloc_got_off_arg {
9059 bfd_vma gotoff;
9060 unsigned int got_elt_size;
9063 /* We need a special top-level link routine to convert got reference counts
9064 to real got offsets. */
9066 static bfd_boolean
9067 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
9069 struct alloc_got_off_arg *gofarg = arg;
9071 if (h->root.type == bfd_link_hash_warning)
9072 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9074 if (h->got.refcount > 0)
9076 h->got.offset = gofarg->gotoff;
9077 gofarg->gotoff += gofarg->got_elt_size;
9079 else
9080 h->got.offset = (bfd_vma) -1;
9082 return TRUE;
9085 /* And an accompanying bit to work out final got entry offsets once
9086 we're done. Should be called from final_link. */
9088 bfd_boolean
9089 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
9090 struct bfd_link_info *info)
9092 bfd *i;
9093 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9094 bfd_vma gotoff;
9095 unsigned int got_elt_size = bed->s->arch_size / 8;
9096 struct alloc_got_off_arg gofarg;
9098 if (! is_elf_hash_table (info->hash))
9099 return FALSE;
9101 /* The GOT offset is relative to the .got section, but the GOT header is
9102 put into the .got.plt section, if the backend uses it. */
9103 if (bed->want_got_plt)
9104 gotoff = 0;
9105 else
9106 gotoff = bed->got_header_size;
9108 /* Do the local .got entries first. */
9109 for (i = info->input_bfds; i; i = i->link_next)
9111 bfd_signed_vma *local_got;
9112 bfd_size_type j, locsymcount;
9113 Elf_Internal_Shdr *symtab_hdr;
9115 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
9116 continue;
9118 local_got = elf_local_got_refcounts (i);
9119 if (!local_got)
9120 continue;
9122 symtab_hdr = &elf_tdata (i)->symtab_hdr;
9123 if (elf_bad_symtab (i))
9124 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9125 else
9126 locsymcount = symtab_hdr->sh_info;
9128 for (j = 0; j < locsymcount; ++j)
9130 if (local_got[j] > 0)
9132 local_got[j] = gotoff;
9133 gotoff += got_elt_size;
9135 else
9136 local_got[j] = (bfd_vma) -1;
9140 /* Then the global .got entries. .plt refcounts are handled by
9141 adjust_dynamic_symbol */
9142 gofarg.gotoff = gotoff;
9143 gofarg.got_elt_size = got_elt_size;
9144 elf_link_hash_traverse (elf_hash_table (info),
9145 elf_gc_allocate_got_offsets,
9146 &gofarg);
9147 return TRUE;
9150 /* Many folk need no more in the way of final link than this, once
9151 got entry reference counting is enabled. */
9153 bfd_boolean
9154 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
9156 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
9157 return FALSE;
9159 /* Invoke the regular ELF backend linker to do all the work. */
9160 return bfd_elf_final_link (abfd, info);
9163 bfd_boolean
9164 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
9166 struct elf_reloc_cookie *rcookie = cookie;
9168 if (rcookie->bad_symtab)
9169 rcookie->rel = rcookie->rels;
9171 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
9173 unsigned long r_symndx;
9175 if (! rcookie->bad_symtab)
9176 if (rcookie->rel->r_offset > offset)
9177 return FALSE;
9178 if (rcookie->rel->r_offset != offset)
9179 continue;
9181 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
9182 if (r_symndx == SHN_UNDEF)
9183 return TRUE;
9185 if (r_symndx >= rcookie->locsymcount
9186 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
9188 struct elf_link_hash_entry *h;
9190 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
9192 while (h->root.type == bfd_link_hash_indirect
9193 || h->root.type == bfd_link_hash_warning)
9194 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9196 if ((h->root.type == bfd_link_hash_defined
9197 || h->root.type == bfd_link_hash_defweak)
9198 && elf_discarded_section (h->root.u.def.section))
9199 return TRUE;
9200 else
9201 return FALSE;
9203 else
9205 /* It's not a relocation against a global symbol,
9206 but it could be a relocation against a local
9207 symbol for a discarded section. */
9208 asection *isec;
9209 Elf_Internal_Sym *isym;
9211 /* Need to: get the symbol; get the section. */
9212 isym = &rcookie->locsyms[r_symndx];
9213 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
9215 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
9216 if (isec != NULL && elf_discarded_section (isec))
9217 return TRUE;
9220 return FALSE;
9222 return FALSE;
9225 /* Discard unneeded references to discarded sections.
9226 Returns TRUE if any section's size was changed. */
9227 /* This function assumes that the relocations are in sorted order,
9228 which is true for all known assemblers. */
9230 bfd_boolean
9231 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
9233 struct elf_reloc_cookie cookie;
9234 asection *stab, *eh;
9235 Elf_Internal_Shdr *symtab_hdr;
9236 const struct elf_backend_data *bed;
9237 bfd *abfd;
9238 unsigned int count;
9239 bfd_boolean ret = FALSE;
9241 if (info->traditional_format
9242 || !is_elf_hash_table (info->hash))
9243 return FALSE;
9245 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
9247 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
9248 continue;
9250 bed = get_elf_backend_data (abfd);
9252 if ((abfd->flags & DYNAMIC) != 0)
9253 continue;
9255 eh = bfd_get_section_by_name (abfd, ".eh_frame");
9256 if (info->relocatable
9257 || (eh != NULL
9258 && (eh->size == 0
9259 || bfd_is_abs_section (eh->output_section))))
9260 eh = NULL;
9262 stab = bfd_get_section_by_name (abfd, ".stab");
9263 if (stab != NULL
9264 && (stab->size == 0
9265 || bfd_is_abs_section (stab->output_section)
9266 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
9267 stab = NULL;
9269 if (stab == NULL
9270 && eh == NULL
9271 && bed->elf_backend_discard_info == NULL)
9272 continue;
9274 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9275 cookie.abfd = abfd;
9276 cookie.sym_hashes = elf_sym_hashes (abfd);
9277 cookie.bad_symtab = elf_bad_symtab (abfd);
9278 if (cookie.bad_symtab)
9280 cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9281 cookie.extsymoff = 0;
9283 else
9285 cookie.locsymcount = symtab_hdr->sh_info;
9286 cookie.extsymoff = symtab_hdr->sh_info;
9289 if (bed->s->arch_size == 32)
9290 cookie.r_sym_shift = 8;
9291 else
9292 cookie.r_sym_shift = 32;
9294 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
9295 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
9297 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9298 cookie.locsymcount, 0,
9299 NULL, NULL, NULL);
9300 if (cookie.locsyms == NULL)
9301 return FALSE;
9304 if (stab != NULL)
9306 cookie.rels = NULL;
9307 count = stab->reloc_count;
9308 if (count != 0)
9309 cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
9310 info->keep_memory);
9311 if (cookie.rels != NULL)
9313 cookie.rel = cookie.rels;
9314 cookie.relend = cookie.rels;
9315 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9316 if (_bfd_discard_section_stabs (abfd, stab,
9317 elf_section_data (stab)->sec_info,
9318 bfd_elf_reloc_symbol_deleted_p,
9319 &cookie))
9320 ret = TRUE;
9321 if (elf_section_data (stab)->relocs != cookie.rels)
9322 free (cookie.rels);
9326 if (eh != NULL)
9328 cookie.rels = NULL;
9329 count = eh->reloc_count;
9330 if (count != 0)
9331 cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
9332 info->keep_memory);
9333 cookie.rel = cookie.rels;
9334 cookie.relend = cookie.rels;
9335 if (cookie.rels != NULL)
9336 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9338 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
9339 bfd_elf_reloc_symbol_deleted_p,
9340 &cookie))
9341 ret = TRUE;
9343 if (cookie.rels != NULL
9344 && elf_section_data (eh)->relocs != cookie.rels)
9345 free (cookie.rels);
9348 if (bed->elf_backend_discard_info != NULL
9349 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
9350 ret = TRUE;
9352 if (cookie.locsyms != NULL
9353 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
9355 if (! info->keep_memory)
9356 free (cookie.locsyms);
9357 else
9358 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
9362 if (info->eh_frame_hdr
9363 && !info->relocatable
9364 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
9365 ret = TRUE;
9367 return ret;
9370 void
9371 _bfd_elf_section_already_linked (bfd *abfd, struct bfd_section * sec)
9373 flagword flags;
9374 const char *name, *p;
9375 struct bfd_section_already_linked *l;
9376 struct bfd_section_already_linked_hash_entry *already_linked_list;
9377 asection *group;
9379 /* A single member comdat group section may be discarded by a
9380 linkonce section. See below. */
9381 if (sec->output_section == bfd_abs_section_ptr)
9382 return;
9384 flags = sec->flags;
9386 /* Check if it belongs to a section group. */
9387 group = elf_sec_group (sec);
9389 /* Return if it isn't a linkonce section nor a member of a group. A
9390 comdat group section also has SEC_LINK_ONCE set. */
9391 if ((flags & SEC_LINK_ONCE) == 0 && group == NULL)
9392 return;
9394 if (group)
9396 /* If this is the member of a single member comdat group, check if
9397 the group should be discarded. */
9398 if (elf_next_in_group (sec) == sec
9399 && (group->flags & SEC_LINK_ONCE) != 0)
9400 sec = group;
9401 else
9402 return;
9405 /* FIXME: When doing a relocatable link, we may have trouble
9406 copying relocations in other sections that refer to local symbols
9407 in the section being discarded. Those relocations will have to
9408 be converted somehow; as of this writing I'm not sure that any of
9409 the backends handle that correctly.
9411 It is tempting to instead not discard link once sections when
9412 doing a relocatable link (technically, they should be discarded
9413 whenever we are building constructors). However, that fails,
9414 because the linker winds up combining all the link once sections
9415 into a single large link once section, which defeats the purpose
9416 of having link once sections in the first place.
9418 Also, not merging link once sections in a relocatable link
9419 causes trouble for MIPS ELF, which relies on link once semantics
9420 to handle the .reginfo section correctly. */
9422 name = bfd_get_section_name (abfd, sec);
9424 if (strncmp (name, ".gnu.linkonce.", sizeof (".gnu.linkonce.") - 1) == 0
9425 && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
9426 p++;
9427 else
9428 p = name;
9430 already_linked_list = bfd_section_already_linked_table_lookup (p);
9432 for (l = already_linked_list->entry; l != NULL; l = l->next)
9434 /* We may have 3 different sections on the list: group section,
9435 comdat section and linkonce section. SEC may be a linkonce or
9436 group section. We match a group section with a group section,
9437 a linkonce section with a linkonce section, and ignore comdat
9438 section. */
9439 if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
9440 && strcmp (name, l->sec->name) == 0
9441 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
9443 /* The section has already been linked. See if we should
9444 issue a warning. */
9445 switch (flags & SEC_LINK_DUPLICATES)
9447 default:
9448 abort ();
9450 case SEC_LINK_DUPLICATES_DISCARD:
9451 break;
9453 case SEC_LINK_DUPLICATES_ONE_ONLY:
9454 (*_bfd_error_handler)
9455 (_("%B: ignoring duplicate section `%A'\n"),
9456 abfd, sec);
9457 break;
9459 case SEC_LINK_DUPLICATES_SAME_SIZE:
9460 if (sec->size != l->sec->size)
9461 (*_bfd_error_handler)
9462 (_("%B: duplicate section `%A' has different size\n"),
9463 abfd, sec);
9464 break;
9466 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
9467 if (sec->size != l->sec->size)
9468 (*_bfd_error_handler)
9469 (_("%B: duplicate section `%A' has different size\n"),
9470 abfd, sec);
9471 else if (sec->size != 0)
9473 bfd_byte *sec_contents, *l_sec_contents;
9475 if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
9476 (*_bfd_error_handler)
9477 (_("%B: warning: could not read contents of section `%A'\n"),
9478 abfd, sec);
9479 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
9480 &l_sec_contents))
9481 (*_bfd_error_handler)
9482 (_("%B: warning: could not read contents of section `%A'\n"),
9483 l->sec->owner, l->sec);
9484 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
9485 (*_bfd_error_handler)
9486 (_("%B: warning: duplicate section `%A' has different contents\n"),
9487 abfd, sec);
9489 if (sec_contents)
9490 free (sec_contents);
9491 if (l_sec_contents)
9492 free (l_sec_contents);
9494 break;
9497 /* Set the output_section field so that lang_add_section
9498 does not create a lang_input_section structure for this
9499 section. Since there might be a symbol in the section
9500 being discarded, we must retain a pointer to the section
9501 which we are really going to use. */
9502 sec->output_section = bfd_abs_section_ptr;
9503 sec->kept_section = l->sec;
9505 if (flags & SEC_GROUP)
9507 asection *first = elf_next_in_group (sec);
9508 asection *s = first;
9510 while (s != NULL)
9512 s->output_section = bfd_abs_section_ptr;
9513 /* Record which group discards it. */
9514 s->kept_section = l->sec;
9515 s = elf_next_in_group (s);
9516 /* These lists are circular. */
9517 if (s == first)
9518 break;
9522 return;
9526 if (group)
9528 /* If this is the member of a single member comdat group and the
9529 group hasn't be discarded, we check if it matches a linkonce
9530 section. We only record the discarded comdat group. Otherwise
9531 the undiscarded group will be discarded incorrectly later since
9532 itself has been recorded. */
9533 for (l = already_linked_list->entry; l != NULL; l = l->next)
9534 if ((l->sec->flags & SEC_GROUP) == 0
9535 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
9536 && bfd_elf_match_symbols_in_sections (l->sec,
9537 elf_next_in_group (sec)))
9539 elf_next_in_group (sec)->output_section = bfd_abs_section_ptr;
9540 elf_next_in_group (sec)->kept_section = l->sec;
9541 group->output_section = bfd_abs_section_ptr;
9542 break;
9544 if (l == NULL)
9545 return;
9547 else
9548 /* There is no direct match. But for linkonce section, we should
9549 check if there is a match with comdat group member. We always
9550 record the linkonce section, discarded or not. */
9551 for (l = already_linked_list->entry; l != NULL; l = l->next)
9552 if (l->sec->flags & SEC_GROUP)
9554 asection *first = elf_next_in_group (l->sec);
9556 if (first != NULL
9557 && elf_next_in_group (first) == first
9558 && bfd_elf_match_symbols_in_sections (first, sec))
9560 sec->output_section = bfd_abs_section_ptr;
9561 sec->kept_section = l->sec;
9562 break;
9566 /* This is the first section with this name. Record it. */
9567 bfd_section_already_linked_table_insert (already_linked_list, sec);