include/
[binutils.git] / bfd / elflink.c
blob8320fd87831e8d7cbc30c189a7611f7cac816e3c
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;
92 if (! info->executable
93 && ! bfd_elf_link_record_dynamic_symbol (info, h))
94 return FALSE;
96 elf_hash_table (info)->hgot = h;
99 /* The first bit of the global offset table is the header. */
100 s->size += bed->got_header_size + bed->got_symbol_offset;
102 return TRUE;
105 /* Create some sections which will be filled in with dynamic linking
106 information. ABFD is an input file which requires dynamic sections
107 to be created. The dynamic sections take up virtual memory space
108 when the final executable is run, so we need to create them before
109 addresses are assigned to the output sections. We work out the
110 actual contents and size of these sections later. */
112 bfd_boolean
113 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
115 flagword flags;
116 register asection *s;
117 struct elf_link_hash_entry *h;
118 struct bfd_link_hash_entry *bh;
119 const struct elf_backend_data *bed;
121 if (! is_elf_hash_table (info->hash))
122 return FALSE;
124 if (elf_hash_table (info)->dynamic_sections_created)
125 return TRUE;
127 /* Make sure that all dynamic sections use the same input BFD. */
128 if (elf_hash_table (info)->dynobj == NULL)
129 elf_hash_table (info)->dynobj = abfd;
130 else
131 abfd = elf_hash_table (info)->dynobj;
133 bed = get_elf_backend_data (abfd);
135 flags = bed->dynamic_sec_flags;
137 /* A dynamically linked executable has a .interp section, but a
138 shared library does not. */
139 if (info->executable)
141 s = bfd_make_section (abfd, ".interp");
142 if (s == NULL
143 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
144 return FALSE;
147 if (! info->traditional_format)
149 s = bfd_make_section (abfd, ".eh_frame_hdr");
150 if (s == NULL
151 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
152 || ! bfd_set_section_alignment (abfd, s, 2))
153 return FALSE;
154 elf_hash_table (info)->eh_info.hdr_sec = s;
157 /* Create sections to hold version informations. These are removed
158 if they are not needed. */
159 s = bfd_make_section (abfd, ".gnu.version_d");
160 if (s == NULL
161 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
162 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
163 return FALSE;
165 s = bfd_make_section (abfd, ".gnu.version");
166 if (s == NULL
167 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
168 || ! bfd_set_section_alignment (abfd, s, 1))
169 return FALSE;
171 s = bfd_make_section (abfd, ".gnu.version_r");
172 if (s == NULL
173 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
174 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
175 return FALSE;
177 s = bfd_make_section (abfd, ".dynsym");
178 if (s == NULL
179 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
180 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
181 return FALSE;
183 s = bfd_make_section (abfd, ".dynstr");
184 if (s == NULL
185 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
186 return FALSE;
188 /* Create a strtab to hold the dynamic symbol names. */
189 if (elf_hash_table (info)->dynstr == NULL)
191 elf_hash_table (info)->dynstr = _bfd_elf_strtab_init ();
192 if (elf_hash_table (info)->dynstr == NULL)
193 return FALSE;
196 s = bfd_make_section (abfd, ".dynamic");
197 if (s == NULL
198 || ! bfd_set_section_flags (abfd, s, flags)
199 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
200 return FALSE;
202 /* The special symbol _DYNAMIC is always set to the start of the
203 .dynamic section. This call occurs before we have processed the
204 symbols for any dynamic object, so we don't have to worry about
205 overriding a dynamic definition. We could set _DYNAMIC in a
206 linker script, but we only want to define it if we are, in fact,
207 creating a .dynamic section. We don't want to define it if there
208 is no .dynamic section, since on some ELF platforms the start up
209 code examines it to decide how to initialize the process. */
210 bh = NULL;
211 if (! (_bfd_generic_link_add_one_symbol
212 (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, 0, NULL, FALSE,
213 get_elf_backend_data (abfd)->collect, &bh)))
214 return FALSE;
215 h = (struct elf_link_hash_entry *) bh;
216 h->def_regular = 1;
217 h->type = STT_OBJECT;
219 if (! info->executable
220 && ! bfd_elf_link_record_dynamic_symbol (info, h))
221 return FALSE;
223 s = bfd_make_section (abfd, ".hash");
224 if (s == NULL
225 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
226 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
227 return FALSE;
228 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
230 /* Let the backend create the rest of the sections. This lets the
231 backend set the right flags. The backend will normally create
232 the .got and .plt sections. */
233 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
234 return FALSE;
236 elf_hash_table (info)->dynamic_sections_created = TRUE;
238 return TRUE;
241 /* Create dynamic sections when linking against a dynamic object. */
243 bfd_boolean
244 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
246 flagword flags, pltflags;
247 asection *s;
248 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
250 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
251 .rel[a].bss sections. */
252 flags = bed->dynamic_sec_flags;
254 pltflags = flags;
255 pltflags |= SEC_CODE;
256 if (bed->plt_not_loaded)
257 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
258 if (bed->plt_readonly)
259 pltflags |= SEC_READONLY;
261 s = bfd_make_section (abfd, ".plt");
262 if (s == NULL
263 || ! bfd_set_section_flags (abfd, s, pltflags)
264 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
265 return FALSE;
267 if (bed->want_plt_sym)
269 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
270 .plt section. */
271 struct elf_link_hash_entry *h;
272 struct bfd_link_hash_entry *bh = NULL;
274 if (! (_bfd_generic_link_add_one_symbol
275 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s, 0, NULL,
276 FALSE, get_elf_backend_data (abfd)->collect, &bh)))
277 return FALSE;
278 h = (struct elf_link_hash_entry *) bh;
279 h->def_regular = 1;
280 h->type = STT_OBJECT;
282 if (! info->executable
283 && ! bfd_elf_link_record_dynamic_symbol (info, h))
284 return FALSE;
287 s = bfd_make_section (abfd,
288 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
289 if (s == NULL
290 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
291 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
292 return FALSE;
294 if (! _bfd_elf_create_got_section (abfd, info))
295 return FALSE;
297 if (bed->want_dynbss)
299 /* The .dynbss section is a place to put symbols which are defined
300 by dynamic objects, are referenced by regular objects, and are
301 not functions. We must allocate space for them in the process
302 image and use a R_*_COPY reloc to tell the dynamic linker to
303 initialize them at run time. The linker script puts the .dynbss
304 section into the .bss section of the final image. */
305 s = bfd_make_section (abfd, ".dynbss");
306 if (s == NULL
307 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC | SEC_LINKER_CREATED))
308 return FALSE;
310 /* The .rel[a].bss section holds copy relocs. This section is not
311 normally needed. We need to create it here, though, so that the
312 linker will map it to an output section. We can't just create it
313 only if we need it, because we will not know whether we need it
314 until we have seen all the input files, and the first time the
315 main linker code calls BFD after examining all the input files
316 (size_dynamic_sections) the input sections have already been
317 mapped to the output sections. If the section turns out not to
318 be needed, we can discard it later. We will never need this
319 section when generating a shared object, since they do not use
320 copy relocs. */
321 if (! info->shared)
323 s = bfd_make_section (abfd,
324 (bed->default_use_rela_p
325 ? ".rela.bss" : ".rel.bss"));
326 if (s == NULL
327 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
328 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
329 return FALSE;
333 return TRUE;
336 /* Record a new dynamic symbol. We record the dynamic symbols as we
337 read the input files, since we need to have a list of all of them
338 before we can determine the final sizes of the output sections.
339 Note that we may actually call this function even though we are not
340 going to output any dynamic symbols; in some cases we know that a
341 symbol should be in the dynamic symbol table, but only if there is
342 one. */
344 bfd_boolean
345 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
346 struct elf_link_hash_entry *h)
348 if (h->dynindx == -1)
350 struct elf_strtab_hash *dynstr;
351 char *p;
352 const char *name;
353 bfd_size_type indx;
355 /* XXX: The ABI draft says the linker must turn hidden and
356 internal symbols into STB_LOCAL symbols when producing the
357 DSO. However, if ld.so honors st_other in the dynamic table,
358 this would not be necessary. */
359 switch (ELF_ST_VISIBILITY (h->other))
361 case STV_INTERNAL:
362 case STV_HIDDEN:
363 if (h->root.type != bfd_link_hash_undefined
364 && h->root.type != bfd_link_hash_undefweak)
366 h->forced_local = 1;
367 return TRUE;
370 default:
371 break;
374 h->dynindx = elf_hash_table (info)->dynsymcount;
375 ++elf_hash_table (info)->dynsymcount;
377 dynstr = elf_hash_table (info)->dynstr;
378 if (dynstr == NULL)
380 /* Create a strtab to hold the dynamic symbol names. */
381 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
382 if (dynstr == NULL)
383 return FALSE;
386 /* We don't put any version information in the dynamic string
387 table. */
388 name = h->root.root.string;
389 p = strchr (name, ELF_VER_CHR);
390 if (p != NULL)
391 /* We know that the p points into writable memory. In fact,
392 there are only a few symbols that have read-only names, being
393 those like _GLOBAL_OFFSET_TABLE_ that are created specially
394 by the backends. Most symbols will have names pointing into
395 an ELF string table read from a file, or to objalloc memory. */
396 *p = 0;
398 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
400 if (p != NULL)
401 *p = ELF_VER_CHR;
403 if (indx == (bfd_size_type) -1)
404 return FALSE;
405 h->dynstr_index = indx;
408 return TRUE;
411 /* Record an assignment to a symbol made by a linker script. We need
412 this in case some dynamic object refers to this symbol. */
414 bfd_boolean
415 bfd_elf_record_link_assignment (bfd *output_bfd ATTRIBUTE_UNUSED,
416 struct bfd_link_info *info,
417 const char *name,
418 bfd_boolean provide)
420 struct elf_link_hash_entry *h;
422 if (!is_elf_hash_table (info->hash))
423 return TRUE;
425 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, TRUE, FALSE);
426 if (h == NULL)
427 return FALSE;
429 /* Since we're defining the symbol, don't let it seem to have not
430 been defined. record_dynamic_symbol and size_dynamic_sections
431 may depend on this.
432 ??? Changing bfd_link_hash_undefined to bfd_link_hash_new (or
433 to bfd_link_hash_undefweak, see linker.c:link_action) runs the risk
434 of some later symbol manipulation setting the symbol back to
435 bfd_link_hash_undefined, and the linker trying to add the symbol to
436 the undefs list twice. */
437 if (h->root.type == bfd_link_hash_undefweak
438 || h->root.type == bfd_link_hash_undefined)
439 h->root.type = bfd_link_hash_new;
441 if (h->root.type == bfd_link_hash_new)
442 h->non_elf = 0;
444 /* If this symbol is being provided by the linker script, and it is
445 currently defined by a dynamic object, but not by a regular
446 object, then mark it as undefined so that the generic linker will
447 force the correct value. */
448 if (provide
449 && h->def_dynamic
450 && !h->def_regular)
451 h->root.type = bfd_link_hash_undefined;
453 /* If this symbol is not being provided by the linker script, and it is
454 currently defined by a dynamic object, but not by a regular object,
455 then clear out any version information because the symbol will not be
456 associated with the dynamic object any more. */
457 if (!provide
458 && h->def_dynamic
459 && !h->def_regular)
460 h->verinfo.verdef = NULL;
462 h->def_regular = 1;
464 if ((h->def_dynamic
465 || h->ref_dynamic
466 || info->shared)
467 && h->dynindx == -1)
469 if (! bfd_elf_link_record_dynamic_symbol (info, h))
470 return FALSE;
472 /* If this is a weak defined symbol, and we know a corresponding
473 real symbol from the same dynamic object, make sure the real
474 symbol is also made into a dynamic symbol. */
475 if (h->u.weakdef != NULL
476 && h->u.weakdef->dynindx == -1)
478 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
479 return FALSE;
483 return TRUE;
486 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
487 success, and 2 on a failure caused by attempting to record a symbol
488 in a discarded section, eg. a discarded link-once section symbol. */
491 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
492 bfd *input_bfd,
493 long input_indx)
495 bfd_size_type amt;
496 struct elf_link_local_dynamic_entry *entry;
497 struct elf_link_hash_table *eht;
498 struct elf_strtab_hash *dynstr;
499 unsigned long dynstr_index;
500 char *name;
501 Elf_External_Sym_Shndx eshndx;
502 char esym[sizeof (Elf64_External_Sym)];
504 if (! is_elf_hash_table (info->hash))
505 return 0;
507 /* See if the entry exists already. */
508 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
509 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
510 return 1;
512 amt = sizeof (*entry);
513 entry = bfd_alloc (input_bfd, amt);
514 if (entry == NULL)
515 return 0;
517 /* Go find the symbol, so that we can find it's name. */
518 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
519 1, input_indx, &entry->isym, esym, &eshndx))
521 bfd_release (input_bfd, entry);
522 return 0;
525 if (entry->isym.st_shndx != SHN_UNDEF
526 && (entry->isym.st_shndx < SHN_LORESERVE
527 || entry->isym.st_shndx > SHN_HIRESERVE))
529 asection *s;
531 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
532 if (s == NULL || bfd_is_abs_section (s->output_section))
534 /* We can still bfd_release here as nothing has done another
535 bfd_alloc. We can't do this later in this function. */
536 bfd_release (input_bfd, entry);
537 return 2;
541 name = (bfd_elf_string_from_elf_section
542 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
543 entry->isym.st_name));
545 dynstr = elf_hash_table (info)->dynstr;
546 if (dynstr == NULL)
548 /* Create a strtab to hold the dynamic symbol names. */
549 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
550 if (dynstr == NULL)
551 return 0;
554 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
555 if (dynstr_index == (unsigned long) -1)
556 return 0;
557 entry->isym.st_name = dynstr_index;
559 eht = elf_hash_table (info);
561 entry->next = eht->dynlocal;
562 eht->dynlocal = entry;
563 entry->input_bfd = input_bfd;
564 entry->input_indx = input_indx;
565 eht->dynsymcount++;
567 /* Whatever binding the symbol had before, it's now local. */
568 entry->isym.st_info
569 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
571 /* The dynindx will be set at the end of size_dynamic_sections. */
573 return 1;
576 /* Return the dynindex of a local dynamic symbol. */
578 long
579 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
580 bfd *input_bfd,
581 long input_indx)
583 struct elf_link_local_dynamic_entry *e;
585 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
586 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
587 return e->dynindx;
588 return -1;
591 /* This function is used to renumber the dynamic symbols, if some of
592 them are removed because they are marked as local. This is called
593 via elf_link_hash_traverse. */
595 static bfd_boolean
596 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
597 void *data)
599 size_t *count = data;
601 if (h->root.type == bfd_link_hash_warning)
602 h = (struct elf_link_hash_entry *) h->root.u.i.link;
604 if (h->dynindx != -1)
605 h->dynindx = ++(*count);
607 return TRUE;
610 /* Return true if the dynamic symbol for a given section should be
611 omitted when creating a shared library. */
612 bfd_boolean
613 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
614 struct bfd_link_info *info,
615 asection *p)
617 switch (elf_section_data (p)->this_hdr.sh_type)
619 case SHT_PROGBITS:
620 case SHT_NOBITS:
621 /* If sh_type is yet undecided, assume it could be
622 SHT_PROGBITS/SHT_NOBITS. */
623 case SHT_NULL:
624 if (strcmp (p->name, ".got") == 0
625 || strcmp (p->name, ".got.plt") == 0
626 || strcmp (p->name, ".plt") == 0)
628 asection *ip;
629 bfd *dynobj = elf_hash_table (info)->dynobj;
631 if (dynobj != NULL
632 && (ip = bfd_get_section_by_name (dynobj, p->name))
633 != NULL
634 && (ip->flags & SEC_LINKER_CREATED)
635 && ip->output_section == p)
636 return TRUE;
638 return FALSE;
640 /* There shouldn't be section relative relocations
641 against any other section. */
642 default:
643 return TRUE;
647 /* Assign dynsym indices. In a shared library we generate a section
648 symbol for each output section, which come first. Next come all of
649 the back-end allocated local dynamic syms, followed by the rest of
650 the global symbols. */
652 unsigned long
653 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
655 unsigned long dynsymcount = 0;
657 if (info->shared)
659 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
660 asection *p;
661 for (p = output_bfd->sections; p ; p = p->next)
662 if ((p->flags & SEC_EXCLUDE) == 0
663 && (p->flags & SEC_ALLOC) != 0
664 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
665 elf_section_data (p)->dynindx = ++dynsymcount;
668 if (elf_hash_table (info)->dynlocal)
670 struct elf_link_local_dynamic_entry *p;
671 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
672 p->dynindx = ++dynsymcount;
675 elf_link_hash_traverse (elf_hash_table (info),
676 elf_link_renumber_hash_table_dynsyms,
677 &dynsymcount);
679 /* There is an unused NULL entry at the head of the table which
680 we must account for in our count. Unless there weren't any
681 symbols, which means we'll have no table at all. */
682 if (dynsymcount != 0)
683 ++dynsymcount;
685 return elf_hash_table (info)->dynsymcount = dynsymcount;
688 /* This function is called when we want to define a new symbol. It
689 handles the various cases which arise when we find a definition in
690 a dynamic object, or when there is already a definition in a
691 dynamic object. The new symbol is described by NAME, SYM, PSEC,
692 and PVALUE. We set SYM_HASH to the hash table entry. We set
693 OVERRIDE if the old symbol is overriding a new definition. We set
694 TYPE_CHANGE_OK if it is OK for the type to change. We set
695 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
696 change, we mean that we shouldn't warn if the type or size does
697 change. */
699 bfd_boolean
700 _bfd_elf_merge_symbol (bfd *abfd,
701 struct bfd_link_info *info,
702 const char *name,
703 Elf_Internal_Sym *sym,
704 asection **psec,
705 bfd_vma *pvalue,
706 struct elf_link_hash_entry **sym_hash,
707 bfd_boolean *skip,
708 bfd_boolean *override,
709 bfd_boolean *type_change_ok,
710 bfd_boolean *size_change_ok)
712 asection *sec;
713 struct elf_link_hash_entry *h;
714 struct elf_link_hash_entry *flip;
715 int bind;
716 bfd *oldbfd;
717 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
718 bfd_boolean newweak, oldweak;
720 *skip = FALSE;
721 *override = FALSE;
723 sec = *psec;
724 bind = ELF_ST_BIND (sym->st_info);
726 if (! bfd_is_und_section (sec))
727 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
728 else
729 h = ((struct elf_link_hash_entry *)
730 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
731 if (h == NULL)
732 return FALSE;
733 *sym_hash = h;
735 /* This code is for coping with dynamic objects, and is only useful
736 if we are doing an ELF link. */
737 if (info->hash->creator != abfd->xvec)
738 return TRUE;
740 /* For merging, we only care about real symbols. */
742 while (h->root.type == bfd_link_hash_indirect
743 || h->root.type == bfd_link_hash_warning)
744 h = (struct elf_link_hash_entry *) h->root.u.i.link;
746 /* If we just created the symbol, mark it as being an ELF symbol.
747 Other than that, there is nothing to do--there is no merge issue
748 with a newly defined symbol--so we just return. */
750 if (h->root.type == bfd_link_hash_new)
752 h->non_elf = 0;
753 return TRUE;
756 /* OLDBFD is a BFD associated with the existing symbol. */
758 switch (h->root.type)
760 default:
761 oldbfd = NULL;
762 break;
764 case bfd_link_hash_undefined:
765 case bfd_link_hash_undefweak:
766 oldbfd = h->root.u.undef.abfd;
767 break;
769 case bfd_link_hash_defined:
770 case bfd_link_hash_defweak:
771 oldbfd = h->root.u.def.section->owner;
772 break;
774 case bfd_link_hash_common:
775 oldbfd = h->root.u.c.p->section->owner;
776 break;
779 /* In cases involving weak versioned symbols, we may wind up trying
780 to merge a symbol with itself. Catch that here, to avoid the
781 confusion that results if we try to override a symbol with
782 itself. The additional tests catch cases like
783 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
784 dynamic object, which we do want to handle here. */
785 if (abfd == oldbfd
786 && ((abfd->flags & DYNAMIC) == 0
787 || !h->def_regular))
788 return TRUE;
790 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
791 respectively, is from a dynamic object. */
793 if ((abfd->flags & DYNAMIC) != 0)
794 newdyn = TRUE;
795 else
796 newdyn = FALSE;
798 if (oldbfd != NULL)
799 olddyn = (oldbfd->flags & DYNAMIC) != 0;
800 else
802 asection *hsec;
804 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
805 indices used by MIPS ELF. */
806 switch (h->root.type)
808 default:
809 hsec = NULL;
810 break;
812 case bfd_link_hash_defined:
813 case bfd_link_hash_defweak:
814 hsec = h->root.u.def.section;
815 break;
817 case bfd_link_hash_common:
818 hsec = h->root.u.c.p->section;
819 break;
822 if (hsec == NULL)
823 olddyn = FALSE;
824 else
825 olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0;
828 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
829 respectively, appear to be a definition rather than reference. */
831 if (bfd_is_und_section (sec) || bfd_is_com_section (sec))
832 newdef = FALSE;
833 else
834 newdef = TRUE;
836 if (h->root.type == bfd_link_hash_undefined
837 || h->root.type == bfd_link_hash_undefweak
838 || h->root.type == bfd_link_hash_common)
839 olddef = FALSE;
840 else
841 olddef = TRUE;
843 /* We need to remember if a symbol has a definition in a dynamic
844 object or is weak in all dynamic objects. Internal and hidden
845 visibility will make it unavailable to dynamic objects. */
846 if (newdyn && !h->dynamic_def)
848 if (!bfd_is_und_section (sec))
849 h->dynamic_def = 1;
850 else
852 /* Check if this symbol is weak in all dynamic objects. If it
853 is the first time we see it in a dynamic object, we mark
854 if it is weak. Otherwise, we clear it. */
855 if (!h->ref_dynamic)
857 if (bind == STB_WEAK)
858 h->dynamic_weak = 1;
860 else if (bind != STB_WEAK)
861 h->dynamic_weak = 0;
865 /* If the old symbol has non-default visibility, we ignore the new
866 definition from a dynamic object. */
867 if (newdyn
868 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
869 && !bfd_is_und_section (sec))
871 *skip = TRUE;
872 /* Make sure this symbol is dynamic. */
873 h->ref_dynamic = 1;
874 /* A protected symbol has external availability. Make sure it is
875 recorded as dynamic.
877 FIXME: Should we check type and size for protected symbol? */
878 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
879 return bfd_elf_link_record_dynamic_symbol (info, h);
880 else
881 return TRUE;
883 else if (!newdyn
884 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
885 && h->def_dynamic)
887 /* If the new symbol with non-default visibility comes from a
888 relocatable file and the old definition comes from a dynamic
889 object, we remove the old definition. */
890 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
891 h = *sym_hash;
893 if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root)
894 && bfd_is_und_section (sec))
896 /* If the new symbol is undefined and the old symbol was
897 also undefined before, we need to make sure
898 _bfd_generic_link_add_one_symbol doesn't mess
899 up the linker hash table undefs list. Since the old
900 definition came from a dynamic object, it is still on the
901 undefs list. */
902 h->root.type = bfd_link_hash_undefined;
903 h->root.u.undef.abfd = abfd;
905 else
907 h->root.type = bfd_link_hash_new;
908 h->root.u.undef.abfd = NULL;
911 if (h->def_dynamic)
913 h->def_dynamic = 0;
914 h->ref_dynamic = 1;
915 h->dynamic_def = 1;
917 /* FIXME: Should we check type and size for protected symbol? */
918 h->size = 0;
919 h->type = 0;
920 return TRUE;
923 /* Differentiate strong and weak symbols. */
924 newweak = bind == STB_WEAK;
925 oldweak = (h->root.type == bfd_link_hash_defweak
926 || h->root.type == bfd_link_hash_undefweak);
928 /* If a new weak symbol definition comes from a regular file and the
929 old symbol comes from a dynamic library, we treat the new one as
930 strong. Similarly, an old weak symbol definition from a regular
931 file is treated as strong when the new symbol comes from a dynamic
932 library. Further, an old weak symbol from a dynamic library is
933 treated as strong if the new symbol is from a dynamic library.
934 This reflects the way glibc's ld.so works.
936 Do this before setting *type_change_ok or *size_change_ok so that
937 we warn properly when dynamic library symbols are overridden. */
939 if (newdef && !newdyn && olddyn)
940 newweak = FALSE;
941 if (olddef && newdyn)
942 oldweak = FALSE;
944 /* It's OK to change the type if either the existing symbol or the
945 new symbol is weak. A type change is also OK if the old symbol
946 is undefined and the new symbol is defined. */
948 if (oldweak
949 || newweak
950 || (newdef
951 && h->root.type == bfd_link_hash_undefined))
952 *type_change_ok = TRUE;
954 /* It's OK to change the size if either the existing symbol or the
955 new symbol is weak, or if the old symbol is undefined. */
957 if (*type_change_ok
958 || h->root.type == bfd_link_hash_undefined)
959 *size_change_ok = TRUE;
961 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
962 symbol, respectively, appears to be a common symbol in a dynamic
963 object. If a symbol appears in an uninitialized section, and is
964 not weak, and is not a function, then it may be a common symbol
965 which was resolved when the dynamic object was created. We want
966 to treat such symbols specially, because they raise special
967 considerations when setting the symbol size: if the symbol
968 appears as a common symbol in a regular object, and the size in
969 the regular object is larger, we must make sure that we use the
970 larger size. This problematic case can always be avoided in C,
971 but it must be handled correctly when using Fortran shared
972 libraries.
974 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
975 likewise for OLDDYNCOMMON and OLDDEF.
977 Note that this test is just a heuristic, and that it is quite
978 possible to have an uninitialized symbol in a shared object which
979 is really a definition, rather than a common symbol. This could
980 lead to some minor confusion when the symbol really is a common
981 symbol in some regular object. However, I think it will be
982 harmless. */
984 if (newdyn
985 && newdef
986 && !newweak
987 && (sec->flags & SEC_ALLOC) != 0
988 && (sec->flags & SEC_LOAD) == 0
989 && sym->st_size > 0
990 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
991 newdyncommon = TRUE;
992 else
993 newdyncommon = FALSE;
995 if (olddyn
996 && olddef
997 && h->root.type == bfd_link_hash_defined
998 && h->def_dynamic
999 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1000 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1001 && h->size > 0
1002 && h->type != STT_FUNC)
1003 olddyncommon = TRUE;
1004 else
1005 olddyncommon = FALSE;
1007 /* If both the old and the new symbols look like common symbols in a
1008 dynamic object, set the size of the symbol to the larger of the
1009 two. */
1011 if (olddyncommon
1012 && newdyncommon
1013 && sym->st_size != h->size)
1015 /* Since we think we have two common symbols, issue a multiple
1016 common warning if desired. Note that we only warn if the
1017 size is different. If the size is the same, we simply let
1018 the old symbol override the new one as normally happens with
1019 symbols defined in dynamic objects. */
1021 if (! ((*info->callbacks->multiple_common)
1022 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1023 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1024 return FALSE;
1026 if (sym->st_size > h->size)
1027 h->size = sym->st_size;
1029 *size_change_ok = TRUE;
1032 /* If we are looking at a dynamic object, and we have found a
1033 definition, we need to see if the symbol was already defined by
1034 some other object. If so, we want to use the existing
1035 definition, and we do not want to report a multiple symbol
1036 definition error; we do this by clobbering *PSEC to be
1037 bfd_und_section_ptr.
1039 We treat a common symbol as a definition if the symbol in the
1040 shared library is a function, since common symbols always
1041 represent variables; this can cause confusion in principle, but
1042 any such confusion would seem to indicate an erroneous program or
1043 shared library. We also permit a common symbol in a regular
1044 object to override a weak symbol in a shared object. */
1046 if (newdyn
1047 && newdef
1048 && (olddef
1049 || (h->root.type == bfd_link_hash_common
1050 && (newweak
1051 || ELF_ST_TYPE (sym->st_info) == STT_FUNC))))
1053 *override = TRUE;
1054 newdef = FALSE;
1055 newdyncommon = FALSE;
1057 *psec = sec = bfd_und_section_ptr;
1058 *size_change_ok = TRUE;
1060 /* If we get here when the old symbol is a common symbol, then
1061 we are explicitly letting it override a weak symbol or
1062 function in a dynamic object, and we don't want to warn about
1063 a type change. If the old symbol is a defined symbol, a type
1064 change warning may still be appropriate. */
1066 if (h->root.type == bfd_link_hash_common)
1067 *type_change_ok = TRUE;
1070 /* Handle the special case of an old common symbol merging with a
1071 new symbol which looks like a common symbol in a shared object.
1072 We change *PSEC and *PVALUE to make the new symbol look like a
1073 common symbol, and let _bfd_generic_link_add_one_symbol will do
1074 the right thing. */
1076 if (newdyncommon
1077 && h->root.type == bfd_link_hash_common)
1079 *override = TRUE;
1080 newdef = FALSE;
1081 newdyncommon = FALSE;
1082 *pvalue = sym->st_size;
1083 *psec = sec = bfd_com_section_ptr;
1084 *size_change_ok = TRUE;
1087 /* If the old symbol is from a dynamic object, and the new symbol is
1088 a definition which is not from a dynamic object, then the new
1089 symbol overrides the old symbol. Symbols from regular files
1090 always take precedence over symbols from dynamic objects, even if
1091 they are defined after the dynamic object in the link.
1093 As above, we again permit a common symbol in a regular object to
1094 override a definition in a shared object if the shared object
1095 symbol is a function or is weak. */
1097 flip = NULL;
1098 if (! newdyn
1099 && (newdef
1100 || (bfd_is_com_section (sec)
1101 && (oldweak
1102 || h->type == STT_FUNC)))
1103 && olddyn
1104 && olddef
1105 && h->def_dynamic)
1107 /* Change the hash table entry to undefined, and let
1108 _bfd_generic_link_add_one_symbol do the right thing with the
1109 new definition. */
1111 h->root.type = bfd_link_hash_undefined;
1112 h->root.u.undef.abfd = h->root.u.def.section->owner;
1113 *size_change_ok = TRUE;
1115 olddef = FALSE;
1116 olddyncommon = FALSE;
1118 /* We again permit a type change when a common symbol may be
1119 overriding a function. */
1121 if (bfd_is_com_section (sec))
1122 *type_change_ok = TRUE;
1124 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1125 flip = *sym_hash;
1126 else
1127 /* This union may have been set to be non-NULL when this symbol
1128 was seen in a dynamic object. We must force the union to be
1129 NULL, so that it is correct for a regular symbol. */
1130 h->verinfo.vertree = NULL;
1133 /* Handle the special case of a new common symbol merging with an
1134 old symbol that looks like it might be a common symbol defined in
1135 a shared object. Note that we have already handled the case in
1136 which a new common symbol should simply override the definition
1137 in the shared library. */
1139 if (! newdyn
1140 && bfd_is_com_section (sec)
1141 && olddyncommon)
1143 /* It would be best if we could set the hash table entry to a
1144 common symbol, but we don't know what to use for the section
1145 or the alignment. */
1146 if (! ((*info->callbacks->multiple_common)
1147 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1148 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1149 return FALSE;
1151 /* If the presumed common symbol in the dynamic object is
1152 larger, pretend that the new symbol has its size. */
1154 if (h->size > *pvalue)
1155 *pvalue = h->size;
1157 /* FIXME: We no longer know the alignment required by the symbol
1158 in the dynamic object, so we just wind up using the one from
1159 the regular object. */
1161 olddef = FALSE;
1162 olddyncommon = FALSE;
1164 h->root.type = bfd_link_hash_undefined;
1165 h->root.u.undef.abfd = h->root.u.def.section->owner;
1167 *size_change_ok = TRUE;
1168 *type_change_ok = TRUE;
1170 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1171 flip = *sym_hash;
1172 else
1173 h->verinfo.vertree = NULL;
1176 if (flip != NULL)
1178 /* Handle the case where we had a versioned symbol in a dynamic
1179 library and now find a definition in a normal object. In this
1180 case, we make the versioned symbol point to the normal one. */
1181 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1182 flip->root.type = h->root.type;
1183 h->root.type = bfd_link_hash_indirect;
1184 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1185 (*bed->elf_backend_copy_indirect_symbol) (bed, flip, h);
1186 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1187 if (h->def_dynamic)
1189 h->def_dynamic = 0;
1190 flip->ref_dynamic = 1;
1194 return TRUE;
1197 /* This function is called to create an indirect symbol from the
1198 default for the symbol with the default version if needed. The
1199 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1200 set DYNSYM if the new indirect symbol is dynamic. */
1202 bfd_boolean
1203 _bfd_elf_add_default_symbol (bfd *abfd,
1204 struct bfd_link_info *info,
1205 struct elf_link_hash_entry *h,
1206 const char *name,
1207 Elf_Internal_Sym *sym,
1208 asection **psec,
1209 bfd_vma *value,
1210 bfd_boolean *dynsym,
1211 bfd_boolean override)
1213 bfd_boolean type_change_ok;
1214 bfd_boolean size_change_ok;
1215 bfd_boolean skip;
1216 char *shortname;
1217 struct elf_link_hash_entry *hi;
1218 struct bfd_link_hash_entry *bh;
1219 const struct elf_backend_data *bed;
1220 bfd_boolean collect;
1221 bfd_boolean dynamic;
1222 char *p;
1223 size_t len, shortlen;
1224 asection *sec;
1226 /* If this symbol has a version, and it is the default version, we
1227 create an indirect symbol from the default name to the fully
1228 decorated name. This will cause external references which do not
1229 specify a version to be bound to this version of the symbol. */
1230 p = strchr (name, ELF_VER_CHR);
1231 if (p == NULL || p[1] != ELF_VER_CHR)
1232 return TRUE;
1234 if (override)
1236 /* We are overridden by an old definition. We need to check if we
1237 need to create the indirect symbol from the default name. */
1238 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1239 FALSE, FALSE);
1240 BFD_ASSERT (hi != NULL);
1241 if (hi == h)
1242 return TRUE;
1243 while (hi->root.type == bfd_link_hash_indirect
1244 || hi->root.type == bfd_link_hash_warning)
1246 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1247 if (hi == h)
1248 return TRUE;
1252 bed = get_elf_backend_data (abfd);
1253 collect = bed->collect;
1254 dynamic = (abfd->flags & DYNAMIC) != 0;
1256 shortlen = p - name;
1257 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1258 if (shortname == NULL)
1259 return FALSE;
1260 memcpy (shortname, name, shortlen);
1261 shortname[shortlen] = '\0';
1263 /* We are going to create a new symbol. Merge it with any existing
1264 symbol with this name. For the purposes of the merge, act as
1265 though we were defining the symbol we just defined, although we
1266 actually going to define an indirect symbol. */
1267 type_change_ok = FALSE;
1268 size_change_ok = FALSE;
1269 sec = *psec;
1270 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1271 &hi, &skip, &override, &type_change_ok,
1272 &size_change_ok))
1273 return FALSE;
1275 if (skip)
1276 goto nondefault;
1278 if (! override)
1280 bh = &hi->root;
1281 if (! (_bfd_generic_link_add_one_symbol
1282 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1283 0, name, FALSE, collect, &bh)))
1284 return FALSE;
1285 hi = (struct elf_link_hash_entry *) bh;
1287 else
1289 /* In this case the symbol named SHORTNAME is overriding the
1290 indirect symbol we want to add. We were planning on making
1291 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1292 is the name without a version. NAME is the fully versioned
1293 name, and it is the default version.
1295 Overriding means that we already saw a definition for the
1296 symbol SHORTNAME in a regular object, and it is overriding
1297 the symbol defined in the dynamic object.
1299 When this happens, we actually want to change NAME, the
1300 symbol we just added, to refer to SHORTNAME. This will cause
1301 references to NAME in the shared object to become references
1302 to SHORTNAME in the regular object. This is what we expect
1303 when we override a function in a shared object: that the
1304 references in the shared object will be mapped to the
1305 definition in the regular object. */
1307 while (hi->root.type == bfd_link_hash_indirect
1308 || hi->root.type == bfd_link_hash_warning)
1309 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1311 h->root.type = bfd_link_hash_indirect;
1312 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1313 if (h->def_dynamic)
1315 h->def_dynamic = 0;
1316 hi->ref_dynamic = 1;
1317 if (hi->ref_regular
1318 || hi->def_regular)
1320 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1321 return FALSE;
1325 /* Now set HI to H, so that the following code will set the
1326 other fields correctly. */
1327 hi = h;
1330 /* If there is a duplicate definition somewhere, then HI may not
1331 point to an indirect symbol. We will have reported an error to
1332 the user in that case. */
1334 if (hi->root.type == bfd_link_hash_indirect)
1336 struct elf_link_hash_entry *ht;
1338 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1339 (*bed->elf_backend_copy_indirect_symbol) (bed, ht, hi);
1341 /* See if the new flags lead us to realize that the symbol must
1342 be dynamic. */
1343 if (! *dynsym)
1345 if (! dynamic)
1347 if (info->shared
1348 || hi->ref_dynamic)
1349 *dynsym = TRUE;
1351 else
1353 if (hi->ref_regular)
1354 *dynsym = TRUE;
1359 /* We also need to define an indirection from the nondefault version
1360 of the symbol. */
1362 nondefault:
1363 len = strlen (name);
1364 shortname = bfd_hash_allocate (&info->hash->table, len);
1365 if (shortname == NULL)
1366 return FALSE;
1367 memcpy (shortname, name, shortlen);
1368 memcpy (shortname + shortlen, p + 1, len - shortlen);
1370 /* Once again, merge with any existing symbol. */
1371 type_change_ok = FALSE;
1372 size_change_ok = FALSE;
1373 sec = *psec;
1374 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1375 &hi, &skip, &override, &type_change_ok,
1376 &size_change_ok))
1377 return FALSE;
1379 if (skip)
1380 return TRUE;
1382 if (override)
1384 /* Here SHORTNAME is a versioned name, so we don't expect to see
1385 the type of override we do in the case above unless it is
1386 overridden by a versioned definition. */
1387 if (hi->root.type != bfd_link_hash_defined
1388 && hi->root.type != bfd_link_hash_defweak)
1389 (*_bfd_error_handler)
1390 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1391 abfd, shortname);
1393 else
1395 bh = &hi->root;
1396 if (! (_bfd_generic_link_add_one_symbol
1397 (info, abfd, shortname, BSF_INDIRECT,
1398 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1399 return FALSE;
1400 hi = (struct elf_link_hash_entry *) bh;
1402 /* If there is a duplicate definition somewhere, then HI may not
1403 point to an indirect symbol. We will have reported an error
1404 to the user in that case. */
1406 if (hi->root.type == bfd_link_hash_indirect)
1408 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
1410 /* See if the new flags lead us to realize that the symbol
1411 must be dynamic. */
1412 if (! *dynsym)
1414 if (! dynamic)
1416 if (info->shared
1417 || hi->ref_dynamic)
1418 *dynsym = TRUE;
1420 else
1422 if (hi->ref_regular)
1423 *dynsym = TRUE;
1429 return TRUE;
1432 /* This routine is used to export all defined symbols into the dynamic
1433 symbol table. It is called via elf_link_hash_traverse. */
1435 bfd_boolean
1436 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1438 struct elf_info_failed *eif = data;
1440 /* Ignore indirect symbols. These are added by the versioning code. */
1441 if (h->root.type == bfd_link_hash_indirect)
1442 return TRUE;
1444 if (h->root.type == bfd_link_hash_warning)
1445 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1447 if (h->dynindx == -1
1448 && (h->def_regular
1449 || h->ref_regular))
1451 struct bfd_elf_version_tree *t;
1452 struct bfd_elf_version_expr *d;
1454 for (t = eif->verdefs; t != NULL; t = t->next)
1456 if (t->globals.list != NULL)
1458 d = (*t->match) (&t->globals, NULL, h->root.root.string);
1459 if (d != NULL)
1460 goto doit;
1463 if (t->locals.list != NULL)
1465 d = (*t->match) (&t->locals, NULL, h->root.root.string);
1466 if (d != NULL)
1467 return TRUE;
1471 if (!eif->verdefs)
1473 doit:
1474 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1476 eif->failed = TRUE;
1477 return FALSE;
1482 return TRUE;
1485 /* Look through the symbols which are defined in other shared
1486 libraries and referenced here. Update the list of version
1487 dependencies. This will be put into the .gnu.version_r section.
1488 This function is called via elf_link_hash_traverse. */
1490 bfd_boolean
1491 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1492 void *data)
1494 struct elf_find_verdep_info *rinfo = data;
1495 Elf_Internal_Verneed *t;
1496 Elf_Internal_Vernaux *a;
1497 bfd_size_type amt;
1499 if (h->root.type == bfd_link_hash_warning)
1500 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1502 /* We only care about symbols defined in shared objects with version
1503 information. */
1504 if (!h->def_dynamic
1505 || h->def_regular
1506 || h->dynindx == -1
1507 || h->verinfo.verdef == NULL)
1508 return TRUE;
1510 /* See if we already know about this version. */
1511 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1513 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1514 continue;
1516 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1517 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1518 return TRUE;
1520 break;
1523 /* This is a new version. Add it to tree we are building. */
1525 if (t == NULL)
1527 amt = sizeof *t;
1528 t = bfd_zalloc (rinfo->output_bfd, amt);
1529 if (t == NULL)
1531 rinfo->failed = TRUE;
1532 return FALSE;
1535 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1536 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1537 elf_tdata (rinfo->output_bfd)->verref = t;
1540 amt = sizeof *a;
1541 a = bfd_zalloc (rinfo->output_bfd, amt);
1543 /* Note that we are copying a string pointer here, and testing it
1544 above. If bfd_elf_string_from_elf_section is ever changed to
1545 discard the string data when low in memory, this will have to be
1546 fixed. */
1547 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1549 a->vna_flags = h->verinfo.verdef->vd_flags;
1550 a->vna_nextptr = t->vn_auxptr;
1552 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1553 ++rinfo->vers;
1555 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1557 t->vn_auxptr = a;
1559 return TRUE;
1562 /* Figure out appropriate versions for all the symbols. We may not
1563 have the version number script until we have read all of the input
1564 files, so until that point we don't know which symbols should be
1565 local. This function is called via elf_link_hash_traverse. */
1567 bfd_boolean
1568 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1570 struct elf_assign_sym_version_info *sinfo;
1571 struct bfd_link_info *info;
1572 const struct elf_backend_data *bed;
1573 struct elf_info_failed eif;
1574 char *p;
1575 bfd_size_type amt;
1577 sinfo = data;
1578 info = sinfo->info;
1580 if (h->root.type == bfd_link_hash_warning)
1581 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1583 /* Fix the symbol flags. */
1584 eif.failed = FALSE;
1585 eif.info = info;
1586 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1588 if (eif.failed)
1589 sinfo->failed = TRUE;
1590 return FALSE;
1593 /* We only need version numbers for symbols defined in regular
1594 objects. */
1595 if (!h->def_regular)
1596 return TRUE;
1598 bed = get_elf_backend_data (sinfo->output_bfd);
1599 p = strchr (h->root.root.string, ELF_VER_CHR);
1600 if (p != NULL && h->verinfo.vertree == NULL)
1602 struct bfd_elf_version_tree *t;
1603 bfd_boolean hidden;
1605 hidden = TRUE;
1607 /* There are two consecutive ELF_VER_CHR characters if this is
1608 not a hidden symbol. */
1609 ++p;
1610 if (*p == ELF_VER_CHR)
1612 hidden = FALSE;
1613 ++p;
1616 /* If there is no version string, we can just return out. */
1617 if (*p == '\0')
1619 if (hidden)
1620 h->hidden = 1;
1621 return TRUE;
1624 /* Look for the version. If we find it, it is no longer weak. */
1625 for (t = sinfo->verdefs; t != NULL; t = t->next)
1627 if (strcmp (t->name, p) == 0)
1629 size_t len;
1630 char *alc;
1631 struct bfd_elf_version_expr *d;
1633 len = p - h->root.root.string;
1634 alc = bfd_malloc (len);
1635 if (alc == NULL)
1636 return FALSE;
1637 memcpy (alc, h->root.root.string, len - 1);
1638 alc[len - 1] = '\0';
1639 if (alc[len - 2] == ELF_VER_CHR)
1640 alc[len - 2] = '\0';
1642 h->verinfo.vertree = t;
1643 t->used = TRUE;
1644 d = NULL;
1646 if (t->globals.list != NULL)
1647 d = (*t->match) (&t->globals, NULL, alc);
1649 /* See if there is anything to force this symbol to
1650 local scope. */
1651 if (d == NULL && t->locals.list != NULL)
1653 d = (*t->match) (&t->locals, NULL, alc);
1654 if (d != NULL
1655 && h->dynindx != -1
1656 && info->shared
1657 && ! info->export_dynamic)
1658 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1661 free (alc);
1662 break;
1666 /* If we are building an application, we need to create a
1667 version node for this version. */
1668 if (t == NULL && info->executable)
1670 struct bfd_elf_version_tree **pp;
1671 int version_index;
1673 /* If we aren't going to export this symbol, we don't need
1674 to worry about it. */
1675 if (h->dynindx == -1)
1676 return TRUE;
1678 amt = sizeof *t;
1679 t = bfd_zalloc (sinfo->output_bfd, amt);
1680 if (t == NULL)
1682 sinfo->failed = TRUE;
1683 return FALSE;
1686 t->name = p;
1687 t->name_indx = (unsigned int) -1;
1688 t->used = TRUE;
1690 version_index = 1;
1691 /* Don't count anonymous version tag. */
1692 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1693 version_index = 0;
1694 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1695 ++version_index;
1696 t->vernum = version_index;
1698 *pp = t;
1700 h->verinfo.vertree = t;
1702 else if (t == NULL)
1704 /* We could not find the version for a symbol when
1705 generating a shared archive. Return an error. */
1706 (*_bfd_error_handler)
1707 (_("%B: undefined versioned symbol name %s"),
1708 sinfo->output_bfd, h->root.root.string);
1709 bfd_set_error (bfd_error_bad_value);
1710 sinfo->failed = TRUE;
1711 return FALSE;
1714 if (hidden)
1715 h->hidden = 1;
1718 /* If we don't have a version for this symbol, see if we can find
1719 something. */
1720 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
1722 struct bfd_elf_version_tree *t;
1723 struct bfd_elf_version_tree *local_ver;
1724 struct bfd_elf_version_expr *d;
1726 /* See if can find what version this symbol is in. If the
1727 symbol is supposed to be local, then don't actually register
1728 it. */
1729 local_ver = NULL;
1730 for (t = sinfo->verdefs; t != NULL; t = t->next)
1732 if (t->globals.list != NULL)
1734 bfd_boolean matched;
1736 matched = FALSE;
1737 d = NULL;
1738 while ((d = (*t->match) (&t->globals, d,
1739 h->root.root.string)) != NULL)
1740 if (d->symver)
1741 matched = TRUE;
1742 else
1744 /* There is a version without definition. Make
1745 the symbol the default definition for this
1746 version. */
1747 h->verinfo.vertree = t;
1748 local_ver = NULL;
1749 d->script = 1;
1750 break;
1752 if (d != NULL)
1753 break;
1754 else if (matched)
1755 /* There is no undefined version for this symbol. Hide the
1756 default one. */
1757 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1760 if (t->locals.list != NULL)
1762 d = NULL;
1763 while ((d = (*t->match) (&t->locals, d,
1764 h->root.root.string)) != NULL)
1766 local_ver = t;
1767 /* If the match is "*", keep looking for a more
1768 explicit, perhaps even global, match.
1769 XXX: Shouldn't this be !d->wildcard instead? */
1770 if (d->pattern[0] != '*' || d->pattern[1] != '\0')
1771 break;
1774 if (d != NULL)
1775 break;
1779 if (local_ver != NULL)
1781 h->verinfo.vertree = local_ver;
1782 if (h->dynindx != -1
1783 && info->shared
1784 && ! info->export_dynamic)
1786 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1791 return TRUE;
1794 /* Read and swap the relocs from the section indicated by SHDR. This
1795 may be either a REL or a RELA section. The relocations are
1796 translated into RELA relocations and stored in INTERNAL_RELOCS,
1797 which should have already been allocated to contain enough space.
1798 The EXTERNAL_RELOCS are a buffer where the external form of the
1799 relocations should be stored.
1801 Returns FALSE if something goes wrong. */
1803 static bfd_boolean
1804 elf_link_read_relocs_from_section (bfd *abfd,
1805 asection *sec,
1806 Elf_Internal_Shdr *shdr,
1807 void *external_relocs,
1808 Elf_Internal_Rela *internal_relocs)
1810 const struct elf_backend_data *bed;
1811 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
1812 const bfd_byte *erela;
1813 const bfd_byte *erelaend;
1814 Elf_Internal_Rela *irela;
1815 Elf_Internal_Shdr *symtab_hdr;
1816 size_t nsyms;
1818 /* Position ourselves at the start of the section. */
1819 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
1820 return FALSE;
1822 /* Read the relocations. */
1823 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
1824 return FALSE;
1826 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1827 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
1829 bed = get_elf_backend_data (abfd);
1831 /* Convert the external relocations to the internal format. */
1832 if (shdr->sh_entsize == bed->s->sizeof_rel)
1833 swap_in = bed->s->swap_reloc_in;
1834 else if (shdr->sh_entsize == bed->s->sizeof_rela)
1835 swap_in = bed->s->swap_reloca_in;
1836 else
1838 bfd_set_error (bfd_error_wrong_format);
1839 return FALSE;
1842 erela = external_relocs;
1843 erelaend = erela + shdr->sh_size;
1844 irela = internal_relocs;
1845 while (erela < erelaend)
1847 bfd_vma r_symndx;
1849 (*swap_in) (abfd, erela, irela);
1850 r_symndx = ELF32_R_SYM (irela->r_info);
1851 if (bed->s->arch_size == 64)
1852 r_symndx >>= 24;
1853 if ((size_t) r_symndx >= nsyms)
1855 (*_bfd_error_handler)
1856 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
1857 " for offset 0x%lx in section `%A'"),
1858 abfd, sec,
1859 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
1860 bfd_set_error (bfd_error_bad_value);
1861 return FALSE;
1863 irela += bed->s->int_rels_per_ext_rel;
1864 erela += shdr->sh_entsize;
1867 return TRUE;
1870 /* Read and swap the relocs for a section O. They may have been
1871 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1872 not NULL, they are used as buffers to read into. They are known to
1873 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
1874 the return value is allocated using either malloc or bfd_alloc,
1875 according to the KEEP_MEMORY argument. If O has two relocation
1876 sections (both REL and RELA relocations), then the REL_HDR
1877 relocations will appear first in INTERNAL_RELOCS, followed by the
1878 REL_HDR2 relocations. */
1880 Elf_Internal_Rela *
1881 _bfd_elf_link_read_relocs (bfd *abfd,
1882 asection *o,
1883 void *external_relocs,
1884 Elf_Internal_Rela *internal_relocs,
1885 bfd_boolean keep_memory)
1887 Elf_Internal_Shdr *rel_hdr;
1888 void *alloc1 = NULL;
1889 Elf_Internal_Rela *alloc2 = NULL;
1890 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1892 if (elf_section_data (o)->relocs != NULL)
1893 return elf_section_data (o)->relocs;
1895 if (o->reloc_count == 0)
1896 return NULL;
1898 rel_hdr = &elf_section_data (o)->rel_hdr;
1900 if (internal_relocs == NULL)
1902 bfd_size_type size;
1904 size = o->reloc_count;
1905 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
1906 if (keep_memory)
1907 internal_relocs = bfd_alloc (abfd, size);
1908 else
1909 internal_relocs = alloc2 = bfd_malloc (size);
1910 if (internal_relocs == NULL)
1911 goto error_return;
1914 if (external_relocs == NULL)
1916 bfd_size_type size = rel_hdr->sh_size;
1918 if (elf_section_data (o)->rel_hdr2)
1919 size += elf_section_data (o)->rel_hdr2->sh_size;
1920 alloc1 = bfd_malloc (size);
1921 if (alloc1 == NULL)
1922 goto error_return;
1923 external_relocs = alloc1;
1926 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
1927 external_relocs,
1928 internal_relocs))
1929 goto error_return;
1930 if (elf_section_data (o)->rel_hdr2
1931 && (!elf_link_read_relocs_from_section
1932 (abfd, o,
1933 elf_section_data (o)->rel_hdr2,
1934 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
1935 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
1936 * bed->s->int_rels_per_ext_rel))))
1937 goto error_return;
1939 /* Cache the results for next time, if we can. */
1940 if (keep_memory)
1941 elf_section_data (o)->relocs = internal_relocs;
1943 if (alloc1 != NULL)
1944 free (alloc1);
1946 /* Don't free alloc2, since if it was allocated we are passing it
1947 back (under the name of internal_relocs). */
1949 return internal_relocs;
1951 error_return:
1952 if (alloc1 != NULL)
1953 free (alloc1);
1954 if (alloc2 != NULL)
1955 free (alloc2);
1956 return NULL;
1959 /* Compute the size of, and allocate space for, REL_HDR which is the
1960 section header for a section containing relocations for O. */
1962 bfd_boolean
1963 _bfd_elf_link_size_reloc_section (bfd *abfd,
1964 Elf_Internal_Shdr *rel_hdr,
1965 asection *o)
1967 bfd_size_type reloc_count;
1968 bfd_size_type num_rel_hashes;
1970 /* Figure out how many relocations there will be. */
1971 if (rel_hdr == &elf_section_data (o)->rel_hdr)
1972 reloc_count = elf_section_data (o)->rel_count;
1973 else
1974 reloc_count = elf_section_data (o)->rel_count2;
1976 num_rel_hashes = o->reloc_count;
1977 if (num_rel_hashes < reloc_count)
1978 num_rel_hashes = reloc_count;
1980 /* That allows us to calculate the size of the section. */
1981 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
1983 /* The contents field must last into write_object_contents, so we
1984 allocate it with bfd_alloc rather than malloc. Also since we
1985 cannot be sure that the contents will actually be filled in,
1986 we zero the allocated space. */
1987 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
1988 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
1989 return FALSE;
1991 /* We only allocate one set of hash entries, so we only do it the
1992 first time we are called. */
1993 if (elf_section_data (o)->rel_hashes == NULL
1994 && num_rel_hashes)
1996 struct elf_link_hash_entry **p;
1998 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
1999 if (p == NULL)
2000 return FALSE;
2002 elf_section_data (o)->rel_hashes = p;
2005 return TRUE;
2008 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2009 originated from the section given by INPUT_REL_HDR) to the
2010 OUTPUT_BFD. */
2012 bfd_boolean
2013 _bfd_elf_link_output_relocs (bfd *output_bfd,
2014 asection *input_section,
2015 Elf_Internal_Shdr *input_rel_hdr,
2016 Elf_Internal_Rela *internal_relocs)
2018 Elf_Internal_Rela *irela;
2019 Elf_Internal_Rela *irelaend;
2020 bfd_byte *erel;
2021 Elf_Internal_Shdr *output_rel_hdr;
2022 asection *output_section;
2023 unsigned int *rel_countp = NULL;
2024 const struct elf_backend_data *bed;
2025 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2027 output_section = input_section->output_section;
2028 output_rel_hdr = NULL;
2030 if (elf_section_data (output_section)->rel_hdr.sh_entsize
2031 == input_rel_hdr->sh_entsize)
2033 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2034 rel_countp = &elf_section_data (output_section)->rel_count;
2036 else if (elf_section_data (output_section)->rel_hdr2
2037 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2038 == input_rel_hdr->sh_entsize))
2040 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2041 rel_countp = &elf_section_data (output_section)->rel_count2;
2043 else
2045 (*_bfd_error_handler)
2046 (_("%B: relocation size mismatch in %B section %A"),
2047 output_bfd, input_section->owner, input_section);
2048 bfd_set_error (bfd_error_wrong_object_format);
2049 return FALSE;
2052 bed = get_elf_backend_data (output_bfd);
2053 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2054 swap_out = bed->s->swap_reloc_out;
2055 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2056 swap_out = bed->s->swap_reloca_out;
2057 else
2058 abort ();
2060 erel = output_rel_hdr->contents;
2061 erel += *rel_countp * input_rel_hdr->sh_entsize;
2062 irela = internal_relocs;
2063 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2064 * bed->s->int_rels_per_ext_rel);
2065 while (irela < irelaend)
2067 (*swap_out) (output_bfd, irela, erel);
2068 irela += bed->s->int_rels_per_ext_rel;
2069 erel += input_rel_hdr->sh_entsize;
2072 /* Bump the counter, so that we know where to add the next set of
2073 relocations. */
2074 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2076 return TRUE;
2079 /* Fix up the flags for a symbol. This handles various cases which
2080 can only be fixed after all the input files are seen. This is
2081 currently called by both adjust_dynamic_symbol and
2082 assign_sym_version, which is unnecessary but perhaps more robust in
2083 the face of future changes. */
2085 bfd_boolean
2086 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2087 struct elf_info_failed *eif)
2089 /* If this symbol was mentioned in a non-ELF file, try to set
2090 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2091 permit a non-ELF file to correctly refer to a symbol defined in
2092 an ELF dynamic object. */
2093 if (h->non_elf)
2095 while (h->root.type == bfd_link_hash_indirect)
2096 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2098 if (h->root.type != bfd_link_hash_defined
2099 && h->root.type != bfd_link_hash_defweak)
2101 h->ref_regular = 1;
2102 h->ref_regular_nonweak = 1;
2104 else
2106 if (h->root.u.def.section->owner != NULL
2107 && (bfd_get_flavour (h->root.u.def.section->owner)
2108 == bfd_target_elf_flavour))
2110 h->ref_regular = 1;
2111 h->ref_regular_nonweak = 1;
2113 else
2114 h->def_regular = 1;
2117 if (h->dynindx == -1
2118 && (h->def_dynamic
2119 || h->ref_dynamic))
2121 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2123 eif->failed = TRUE;
2124 return FALSE;
2128 else
2130 /* Unfortunately, NON_ELF is only correct if the symbol
2131 was first seen in a non-ELF file. Fortunately, if the symbol
2132 was first seen in an ELF file, we're probably OK unless the
2133 symbol was defined in a non-ELF file. Catch that case here.
2134 FIXME: We're still in trouble if the symbol was first seen in
2135 a dynamic object, and then later in a non-ELF regular object. */
2136 if ((h->root.type == bfd_link_hash_defined
2137 || h->root.type == bfd_link_hash_defweak)
2138 && !h->def_regular
2139 && (h->root.u.def.section->owner != NULL
2140 ? (bfd_get_flavour (h->root.u.def.section->owner)
2141 != bfd_target_elf_flavour)
2142 : (bfd_is_abs_section (h->root.u.def.section)
2143 && !h->def_dynamic)))
2144 h->def_regular = 1;
2147 /* If this is a final link, and the symbol was defined as a common
2148 symbol in a regular object file, and there was no definition in
2149 any dynamic object, then the linker will have allocated space for
2150 the symbol in a common section but the DEF_REGULAR
2151 flag will not have been set. */
2152 if (h->root.type == bfd_link_hash_defined
2153 && !h->def_regular
2154 && h->ref_regular
2155 && !h->def_dynamic
2156 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2157 h->def_regular = 1;
2159 /* If -Bsymbolic was used (which means to bind references to global
2160 symbols to the definition within the shared object), and this
2161 symbol was defined in a regular object, then it actually doesn't
2162 need a PLT entry. Likewise, if the symbol has non-default
2163 visibility. If the symbol has hidden or internal visibility, we
2164 will force it local. */
2165 if (h->needs_plt
2166 && eif->info->shared
2167 && is_elf_hash_table (eif->info->hash)
2168 && (eif->info->symbolic
2169 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2170 && h->def_regular)
2172 const struct elf_backend_data *bed;
2173 bfd_boolean force_local;
2175 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2177 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2178 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2179 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2182 /* If a weak undefined symbol has non-default visibility, we also
2183 hide it from the dynamic linker. */
2184 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2185 && h->root.type == bfd_link_hash_undefweak)
2187 const struct elf_backend_data *bed;
2188 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2189 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2192 /* If this is a weak defined symbol in a dynamic object, and we know
2193 the real definition in the dynamic object, copy interesting flags
2194 over to the real definition. */
2195 if (h->u.weakdef != NULL)
2197 struct elf_link_hash_entry *weakdef;
2199 weakdef = h->u.weakdef;
2200 if (h->root.type == bfd_link_hash_indirect)
2201 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2203 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2204 || h->root.type == bfd_link_hash_defweak);
2205 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2206 || weakdef->root.type == bfd_link_hash_defweak);
2207 BFD_ASSERT (weakdef->def_dynamic);
2209 /* If the real definition is defined by a regular object file,
2210 don't do anything special. See the longer description in
2211 _bfd_elf_adjust_dynamic_symbol, below. */
2212 if (weakdef->def_regular)
2213 h->u.weakdef = NULL;
2214 else
2216 const struct elf_backend_data *bed;
2218 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2219 (*bed->elf_backend_copy_indirect_symbol) (bed, weakdef, h);
2223 return TRUE;
2226 /* Make the backend pick a good value for a dynamic symbol. This is
2227 called via elf_link_hash_traverse, and also calls itself
2228 recursively. */
2230 bfd_boolean
2231 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2233 struct elf_info_failed *eif = data;
2234 bfd *dynobj;
2235 const struct elf_backend_data *bed;
2237 if (! is_elf_hash_table (eif->info->hash))
2238 return FALSE;
2240 if (h->root.type == bfd_link_hash_warning)
2242 h->plt = elf_hash_table (eif->info)->init_offset;
2243 h->got = elf_hash_table (eif->info)->init_offset;
2245 /* When warning symbols are created, they **replace** the "real"
2246 entry in the hash table, thus we never get to see the real
2247 symbol in a hash traversal. So look at it now. */
2248 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2251 /* Ignore indirect symbols. These are added by the versioning code. */
2252 if (h->root.type == bfd_link_hash_indirect)
2253 return TRUE;
2255 /* Fix the symbol flags. */
2256 if (! _bfd_elf_fix_symbol_flags (h, eif))
2257 return FALSE;
2259 /* If this symbol does not require a PLT entry, and it is not
2260 defined by a dynamic object, or is not referenced by a regular
2261 object, ignore it. We do have to handle a weak defined symbol,
2262 even if no regular object refers to it, if we decided to add it
2263 to the dynamic symbol table. FIXME: Do we normally need to worry
2264 about symbols which are defined by one dynamic object and
2265 referenced by another one? */
2266 if (!h->needs_plt
2267 && (h->def_regular
2268 || !h->def_dynamic
2269 || (!h->ref_regular
2270 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2272 h->plt = elf_hash_table (eif->info)->init_offset;
2273 return TRUE;
2276 /* If we've already adjusted this symbol, don't do it again. This
2277 can happen via a recursive call. */
2278 if (h->dynamic_adjusted)
2279 return TRUE;
2281 /* Don't look at this symbol again. Note that we must set this
2282 after checking the above conditions, because we may look at a
2283 symbol once, decide not to do anything, and then get called
2284 recursively later after REF_REGULAR is set below. */
2285 h->dynamic_adjusted = 1;
2287 /* If this is a weak definition, and we know a real definition, and
2288 the real symbol is not itself defined by a regular object file,
2289 then get a good value for the real definition. We handle the
2290 real symbol first, for the convenience of the backend routine.
2292 Note that there is a confusing case here. If the real definition
2293 is defined by a regular object file, we don't get the real symbol
2294 from the dynamic object, but we do get the weak symbol. If the
2295 processor backend uses a COPY reloc, then if some routine in the
2296 dynamic object changes the real symbol, we will not see that
2297 change in the corresponding weak symbol. This is the way other
2298 ELF linkers work as well, and seems to be a result of the shared
2299 library model.
2301 I will clarify this issue. Most SVR4 shared libraries define the
2302 variable _timezone and define timezone as a weak synonym. The
2303 tzset call changes _timezone. If you write
2304 extern int timezone;
2305 int _timezone = 5;
2306 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2307 you might expect that, since timezone is a synonym for _timezone,
2308 the same number will print both times. However, if the processor
2309 backend uses a COPY reloc, then actually timezone will be copied
2310 into your process image, and, since you define _timezone
2311 yourself, _timezone will not. Thus timezone and _timezone will
2312 wind up at different memory locations. The tzset call will set
2313 _timezone, leaving timezone unchanged. */
2315 if (h->u.weakdef != NULL)
2317 /* If we get to this point, we know there is an implicit
2318 reference by a regular object file via the weak symbol H.
2319 FIXME: Is this really true? What if the traversal finds
2320 H->U.WEAKDEF before it finds H? */
2321 h->u.weakdef->ref_regular = 1;
2323 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2324 return FALSE;
2327 /* If a symbol has no type and no size and does not require a PLT
2328 entry, then we are probably about to do the wrong thing here: we
2329 are probably going to create a COPY reloc for an empty object.
2330 This case can arise when a shared object is built with assembly
2331 code, and the assembly code fails to set the symbol type. */
2332 if (h->size == 0
2333 && h->type == STT_NOTYPE
2334 && !h->needs_plt)
2335 (*_bfd_error_handler)
2336 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2337 h->root.root.string);
2339 dynobj = elf_hash_table (eif->info)->dynobj;
2340 bed = get_elf_backend_data (dynobj);
2341 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2343 eif->failed = TRUE;
2344 return FALSE;
2347 return TRUE;
2350 /* Adjust all external symbols pointing into SEC_MERGE sections
2351 to reflect the object merging within the sections. */
2353 bfd_boolean
2354 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2356 asection *sec;
2358 if (h->root.type == bfd_link_hash_warning)
2359 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2361 if ((h->root.type == bfd_link_hash_defined
2362 || h->root.type == bfd_link_hash_defweak)
2363 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2364 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2366 bfd *output_bfd = data;
2368 h->root.u.def.value =
2369 _bfd_merged_section_offset (output_bfd,
2370 &h->root.u.def.section,
2371 elf_section_data (sec)->sec_info,
2372 h->root.u.def.value);
2375 return TRUE;
2378 /* Returns false if the symbol referred to by H should be considered
2379 to resolve local to the current module, and true if it should be
2380 considered to bind dynamically. */
2382 bfd_boolean
2383 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2384 struct bfd_link_info *info,
2385 bfd_boolean ignore_protected)
2387 bfd_boolean binding_stays_local_p;
2389 if (h == NULL)
2390 return FALSE;
2392 while (h->root.type == bfd_link_hash_indirect
2393 || h->root.type == bfd_link_hash_warning)
2394 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2396 /* If it was forced local, then clearly it's not dynamic. */
2397 if (h->dynindx == -1)
2398 return FALSE;
2399 if (h->forced_local)
2400 return FALSE;
2402 /* Identify the cases where name binding rules say that a
2403 visible symbol resolves locally. */
2404 binding_stays_local_p = info->executable || info->symbolic;
2406 switch (ELF_ST_VISIBILITY (h->other))
2408 case STV_INTERNAL:
2409 case STV_HIDDEN:
2410 return FALSE;
2412 case STV_PROTECTED:
2413 /* Proper resolution for function pointer equality may require
2414 that these symbols perhaps be resolved dynamically, even though
2415 we should be resolving them to the current module. */
2416 if (!ignore_protected)
2417 binding_stays_local_p = TRUE;
2418 break;
2420 default:
2421 break;
2424 /* If it isn't defined locally, then clearly it's dynamic. */
2425 if (!h->def_regular)
2426 return TRUE;
2428 /* Otherwise, the symbol is dynamic if binding rules don't tell
2429 us that it remains local. */
2430 return !binding_stays_local_p;
2433 /* Return true if the symbol referred to by H should be considered
2434 to resolve local to the current module, and false otherwise. Differs
2435 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2436 undefined symbols and weak symbols. */
2438 bfd_boolean
2439 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2440 struct bfd_link_info *info,
2441 bfd_boolean local_protected)
2443 /* If it's a local sym, of course we resolve locally. */
2444 if (h == NULL)
2445 return TRUE;
2447 /* Common symbols that become definitions don't get the DEF_REGULAR
2448 flag set, so test it first, and don't bail out. */
2449 if (ELF_COMMON_DEF_P (h))
2450 /* Do nothing. */;
2451 /* If we don't have a definition in a regular file, then we can't
2452 resolve locally. The sym is either undefined or dynamic. */
2453 else if (!h->def_regular)
2454 return FALSE;
2456 /* Forced local symbols resolve locally. */
2457 if (h->forced_local)
2458 return TRUE;
2460 /* As do non-dynamic symbols. */
2461 if (h->dynindx == -1)
2462 return TRUE;
2464 /* At this point, we know the symbol is defined and dynamic. In an
2465 executable it must resolve locally, likewise when building symbolic
2466 shared libraries. */
2467 if (info->executable || info->symbolic)
2468 return TRUE;
2470 /* Now deal with defined dynamic symbols in shared libraries. Ones
2471 with default visibility might not resolve locally. */
2472 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2473 return FALSE;
2475 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2476 if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED)
2477 return TRUE;
2479 /* Function pointer equality tests may require that STV_PROTECTED
2480 symbols be treated as dynamic symbols, even when we know that the
2481 dynamic linker will resolve them locally. */
2482 return local_protected;
2485 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2486 aligned. Returns the first TLS output section. */
2488 struct bfd_section *
2489 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2491 struct bfd_section *sec, *tls;
2492 unsigned int align = 0;
2494 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2495 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2496 break;
2497 tls = sec;
2499 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2500 if (sec->alignment_power > align)
2501 align = sec->alignment_power;
2503 elf_hash_table (info)->tls_sec = tls;
2505 /* Ensure the alignment of the first section is the largest alignment,
2506 so that the tls segment starts aligned. */
2507 if (tls != NULL)
2508 tls->alignment_power = align;
2510 return tls;
2513 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2514 static bfd_boolean
2515 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2516 Elf_Internal_Sym *sym)
2518 /* Local symbols do not count, but target specific ones might. */
2519 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2520 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2521 return FALSE;
2523 /* Function symbols do not count. */
2524 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
2525 return FALSE;
2527 /* If the section is undefined, then so is the symbol. */
2528 if (sym->st_shndx == SHN_UNDEF)
2529 return FALSE;
2531 /* If the symbol is defined in the common section, then
2532 it is a common definition and so does not count. */
2533 if (sym->st_shndx == SHN_COMMON)
2534 return FALSE;
2536 /* If the symbol is in a target specific section then we
2537 must rely upon the backend to tell us what it is. */
2538 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2539 /* FIXME - this function is not coded yet:
2541 return _bfd_is_global_symbol_definition (abfd, sym);
2543 Instead for now assume that the definition is not global,
2544 Even if this is wrong, at least the linker will behave
2545 in the same way that it used to do. */
2546 return FALSE;
2548 return TRUE;
2551 /* Search the symbol table of the archive element of the archive ABFD
2552 whose archive map contains a mention of SYMDEF, and determine if
2553 the symbol is defined in this element. */
2554 static bfd_boolean
2555 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2557 Elf_Internal_Shdr * hdr;
2558 bfd_size_type symcount;
2559 bfd_size_type extsymcount;
2560 bfd_size_type extsymoff;
2561 Elf_Internal_Sym *isymbuf;
2562 Elf_Internal_Sym *isym;
2563 Elf_Internal_Sym *isymend;
2564 bfd_boolean result;
2566 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2567 if (abfd == NULL)
2568 return FALSE;
2570 if (! bfd_check_format (abfd, bfd_object))
2571 return FALSE;
2573 /* If we have already included the element containing this symbol in the
2574 link then we do not need to include it again. Just claim that any symbol
2575 it contains is not a definition, so that our caller will not decide to
2576 (re)include this element. */
2577 if (abfd->archive_pass)
2578 return FALSE;
2580 /* Select the appropriate symbol table. */
2581 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2582 hdr = &elf_tdata (abfd)->symtab_hdr;
2583 else
2584 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2586 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2588 /* The sh_info field of the symtab header tells us where the
2589 external symbols start. We don't care about the local symbols. */
2590 if (elf_bad_symtab (abfd))
2592 extsymcount = symcount;
2593 extsymoff = 0;
2595 else
2597 extsymcount = symcount - hdr->sh_info;
2598 extsymoff = hdr->sh_info;
2601 if (extsymcount == 0)
2602 return FALSE;
2604 /* Read in the symbol table. */
2605 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2606 NULL, NULL, NULL);
2607 if (isymbuf == NULL)
2608 return FALSE;
2610 /* Scan the symbol table looking for SYMDEF. */
2611 result = FALSE;
2612 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2614 const char *name;
2616 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
2617 isym->st_name);
2618 if (name == NULL)
2619 break;
2621 if (strcmp (name, symdef->name) == 0)
2623 result = is_global_data_symbol_definition (abfd, isym);
2624 break;
2628 free (isymbuf);
2630 return result;
2633 /* Add an entry to the .dynamic table. */
2635 bfd_boolean
2636 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
2637 bfd_vma tag,
2638 bfd_vma val)
2640 struct elf_link_hash_table *hash_table;
2641 const struct elf_backend_data *bed;
2642 asection *s;
2643 bfd_size_type newsize;
2644 bfd_byte *newcontents;
2645 Elf_Internal_Dyn dyn;
2647 hash_table = elf_hash_table (info);
2648 if (! is_elf_hash_table (hash_table))
2649 return FALSE;
2651 bed = get_elf_backend_data (hash_table->dynobj);
2652 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2653 BFD_ASSERT (s != NULL);
2655 newsize = s->size + bed->s->sizeof_dyn;
2656 newcontents = bfd_realloc (s->contents, newsize);
2657 if (newcontents == NULL)
2658 return FALSE;
2660 dyn.d_tag = tag;
2661 dyn.d_un.d_val = val;
2662 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
2664 s->size = newsize;
2665 s->contents = newcontents;
2667 return TRUE;
2670 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2671 otherwise just check whether one already exists. Returns -1 on error,
2672 1 if a DT_NEEDED tag already exists, and 0 on success. */
2674 static int
2675 elf_add_dt_needed_tag (struct bfd_link_info *info,
2676 const char *soname,
2677 bfd_boolean do_it)
2679 struct elf_link_hash_table *hash_table;
2680 bfd_size_type oldsize;
2681 bfd_size_type strindex;
2683 hash_table = elf_hash_table (info);
2684 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
2685 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
2686 if (strindex == (bfd_size_type) -1)
2687 return -1;
2689 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
2691 asection *sdyn;
2692 const struct elf_backend_data *bed;
2693 bfd_byte *extdyn;
2695 bed = get_elf_backend_data (hash_table->dynobj);
2696 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2697 BFD_ASSERT (sdyn != NULL);
2699 for (extdyn = sdyn->contents;
2700 extdyn < sdyn->contents + sdyn->size;
2701 extdyn += bed->s->sizeof_dyn)
2703 Elf_Internal_Dyn dyn;
2705 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
2706 if (dyn.d_tag == DT_NEEDED
2707 && dyn.d_un.d_val == strindex)
2709 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2710 return 1;
2715 if (do_it)
2717 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
2718 return -1;
2720 else
2721 /* We were just checking for existence of the tag. */
2722 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2724 return 0;
2727 /* Sort symbol by value and section. */
2728 static int
2729 elf_sort_symbol (const void *arg1, const void *arg2)
2731 const struct elf_link_hash_entry *h1;
2732 const struct elf_link_hash_entry *h2;
2733 bfd_signed_vma vdiff;
2735 h1 = *(const struct elf_link_hash_entry **) arg1;
2736 h2 = *(const struct elf_link_hash_entry **) arg2;
2737 vdiff = h1->root.u.def.value - h2->root.u.def.value;
2738 if (vdiff != 0)
2739 return vdiff > 0 ? 1 : -1;
2740 else
2742 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
2743 if (sdiff != 0)
2744 return sdiff > 0 ? 1 : -1;
2746 return 0;
2749 /* This function is used to adjust offsets into .dynstr for
2750 dynamic symbols. This is called via elf_link_hash_traverse. */
2752 static bfd_boolean
2753 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
2755 struct elf_strtab_hash *dynstr = data;
2757 if (h->root.type == bfd_link_hash_warning)
2758 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2760 if (h->dynindx != -1)
2761 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
2762 return TRUE;
2765 /* Assign string offsets in .dynstr, update all structures referencing
2766 them. */
2768 static bfd_boolean
2769 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
2771 struct elf_link_hash_table *hash_table = elf_hash_table (info);
2772 struct elf_link_local_dynamic_entry *entry;
2773 struct elf_strtab_hash *dynstr = hash_table->dynstr;
2774 bfd *dynobj = hash_table->dynobj;
2775 asection *sdyn;
2776 bfd_size_type size;
2777 const struct elf_backend_data *bed;
2778 bfd_byte *extdyn;
2780 _bfd_elf_strtab_finalize (dynstr);
2781 size = _bfd_elf_strtab_size (dynstr);
2783 bed = get_elf_backend_data (dynobj);
2784 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2785 BFD_ASSERT (sdyn != NULL);
2787 /* Update all .dynamic entries referencing .dynstr strings. */
2788 for (extdyn = sdyn->contents;
2789 extdyn < sdyn->contents + sdyn->size;
2790 extdyn += bed->s->sizeof_dyn)
2792 Elf_Internal_Dyn dyn;
2794 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
2795 switch (dyn.d_tag)
2797 case DT_STRSZ:
2798 dyn.d_un.d_val = size;
2799 break;
2800 case DT_NEEDED:
2801 case DT_SONAME:
2802 case DT_RPATH:
2803 case DT_RUNPATH:
2804 case DT_FILTER:
2805 case DT_AUXILIARY:
2806 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
2807 break;
2808 default:
2809 continue;
2811 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
2814 /* Now update local dynamic symbols. */
2815 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
2816 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
2817 entry->isym.st_name);
2819 /* And the rest of dynamic symbols. */
2820 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
2822 /* Adjust version definitions. */
2823 if (elf_tdata (output_bfd)->cverdefs)
2825 asection *s;
2826 bfd_byte *p;
2827 bfd_size_type i;
2828 Elf_Internal_Verdef def;
2829 Elf_Internal_Verdaux defaux;
2831 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
2832 p = s->contents;
2835 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
2836 &def);
2837 p += sizeof (Elf_External_Verdef);
2838 for (i = 0; i < def.vd_cnt; ++i)
2840 _bfd_elf_swap_verdaux_in (output_bfd,
2841 (Elf_External_Verdaux *) p, &defaux);
2842 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
2843 defaux.vda_name);
2844 _bfd_elf_swap_verdaux_out (output_bfd,
2845 &defaux, (Elf_External_Verdaux *) p);
2846 p += sizeof (Elf_External_Verdaux);
2849 while (def.vd_next);
2852 /* Adjust version references. */
2853 if (elf_tdata (output_bfd)->verref)
2855 asection *s;
2856 bfd_byte *p;
2857 bfd_size_type i;
2858 Elf_Internal_Verneed need;
2859 Elf_Internal_Vernaux needaux;
2861 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
2862 p = s->contents;
2865 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
2866 &need);
2867 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
2868 _bfd_elf_swap_verneed_out (output_bfd, &need,
2869 (Elf_External_Verneed *) p);
2870 p += sizeof (Elf_External_Verneed);
2871 for (i = 0; i < need.vn_cnt; ++i)
2873 _bfd_elf_swap_vernaux_in (output_bfd,
2874 (Elf_External_Vernaux *) p, &needaux);
2875 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
2876 needaux.vna_name);
2877 _bfd_elf_swap_vernaux_out (output_bfd,
2878 &needaux,
2879 (Elf_External_Vernaux *) p);
2880 p += sizeof (Elf_External_Vernaux);
2883 while (need.vn_next);
2886 return TRUE;
2889 /* Add symbols from an ELF object file to the linker hash table. */
2891 static bfd_boolean
2892 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
2894 bfd_boolean (*add_symbol_hook)
2895 (bfd *, struct bfd_link_info *, Elf_Internal_Sym *,
2896 const char **, flagword *, asection **, bfd_vma *);
2897 bfd_boolean (*check_relocs)
2898 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
2899 bfd_boolean (*check_directives)
2900 (bfd *, struct bfd_link_info *);
2901 bfd_boolean collect;
2902 Elf_Internal_Shdr *hdr;
2903 bfd_size_type symcount;
2904 bfd_size_type extsymcount;
2905 bfd_size_type extsymoff;
2906 struct elf_link_hash_entry **sym_hash;
2907 bfd_boolean dynamic;
2908 Elf_External_Versym *extversym = NULL;
2909 Elf_External_Versym *ever;
2910 struct elf_link_hash_entry *weaks;
2911 struct elf_link_hash_entry **nondeflt_vers = NULL;
2912 bfd_size_type nondeflt_vers_cnt = 0;
2913 Elf_Internal_Sym *isymbuf = NULL;
2914 Elf_Internal_Sym *isym;
2915 Elf_Internal_Sym *isymend;
2916 const struct elf_backend_data *bed;
2917 bfd_boolean add_needed;
2918 struct elf_link_hash_table * hash_table;
2919 bfd_size_type amt;
2921 hash_table = elf_hash_table (info);
2923 bed = get_elf_backend_data (abfd);
2924 add_symbol_hook = bed->elf_add_symbol_hook;
2925 collect = bed->collect;
2927 if ((abfd->flags & DYNAMIC) == 0)
2928 dynamic = FALSE;
2929 else
2931 dynamic = TRUE;
2933 /* You can't use -r against a dynamic object. Also, there's no
2934 hope of using a dynamic object which does not exactly match
2935 the format of the output file. */
2936 if (info->relocatable
2937 || !is_elf_hash_table (hash_table)
2938 || hash_table->root.creator != abfd->xvec)
2940 if (info->relocatable)
2941 bfd_set_error (bfd_error_invalid_operation);
2942 else
2943 bfd_set_error (bfd_error_wrong_format);
2944 goto error_return;
2948 /* As a GNU extension, any input sections which are named
2949 .gnu.warning.SYMBOL are treated as warning symbols for the given
2950 symbol. This differs from .gnu.warning sections, which generate
2951 warnings when they are included in an output file. */
2952 if (info->executable)
2954 asection *s;
2956 for (s = abfd->sections; s != NULL; s = s->next)
2958 const char *name;
2960 name = bfd_get_section_name (abfd, s);
2961 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
2963 char *msg;
2964 bfd_size_type sz;
2965 bfd_size_type prefix_len;
2966 const char * gnu_warning_prefix = _("warning: ");
2968 name += sizeof ".gnu.warning." - 1;
2970 /* If this is a shared object, then look up the symbol
2971 in the hash table. If it is there, and it is already
2972 been defined, then we will not be using the entry
2973 from this shared object, so we don't need to warn.
2974 FIXME: If we see the definition in a regular object
2975 later on, we will warn, but we shouldn't. The only
2976 fix is to keep track of what warnings we are supposed
2977 to emit, and then handle them all at the end of the
2978 link. */
2979 if (dynamic)
2981 struct elf_link_hash_entry *h;
2983 h = elf_link_hash_lookup (hash_table, name,
2984 FALSE, FALSE, TRUE);
2986 /* FIXME: What about bfd_link_hash_common? */
2987 if (h != NULL
2988 && (h->root.type == bfd_link_hash_defined
2989 || h->root.type == bfd_link_hash_defweak))
2991 /* We don't want to issue this warning. Clobber
2992 the section size so that the warning does not
2993 get copied into the output file. */
2994 s->size = 0;
2995 continue;
2999 sz = s->size;
3000 prefix_len = strlen (gnu_warning_prefix);
3001 msg = bfd_alloc (abfd, prefix_len + sz + 1);
3002 if (msg == NULL)
3003 goto error_return;
3005 strcpy (msg, gnu_warning_prefix);
3006 if (! bfd_get_section_contents (abfd, s, msg + prefix_len, 0, sz))
3007 goto error_return;
3009 msg[prefix_len + sz] = '\0';
3011 if (! (_bfd_generic_link_add_one_symbol
3012 (info, abfd, name, BSF_WARNING, s, 0, msg,
3013 FALSE, collect, NULL)))
3014 goto error_return;
3016 if (! info->relocatable)
3018 /* Clobber the section size so that the warning does
3019 not get copied into the output file. */
3020 s->size = 0;
3026 add_needed = TRUE;
3027 if (! dynamic)
3029 /* If we are creating a shared library, create all the dynamic
3030 sections immediately. We need to attach them to something,
3031 so we attach them to this BFD, provided it is the right
3032 format. FIXME: If there are no input BFD's of the same
3033 format as the output, we can't make a shared library. */
3034 if (info->shared
3035 && is_elf_hash_table (hash_table)
3036 && hash_table->root.creator == abfd->xvec
3037 && ! hash_table->dynamic_sections_created)
3039 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3040 goto error_return;
3043 else if (!is_elf_hash_table (hash_table))
3044 goto error_return;
3045 else
3047 asection *s;
3048 const char *soname = NULL;
3049 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3050 int ret;
3052 /* ld --just-symbols and dynamic objects don't mix very well.
3053 Test for --just-symbols by looking at info set up by
3054 _bfd_elf_link_just_syms. */
3055 if ((s = abfd->sections) != NULL
3056 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3057 goto error_return;
3059 /* If this dynamic lib was specified on the command line with
3060 --as-needed in effect, then we don't want to add a DT_NEEDED
3061 tag unless the lib is actually used. Similary for libs brought
3062 in by another lib's DT_NEEDED. When --no-add-needed is used
3063 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3064 any dynamic library in DT_NEEDED tags in the dynamic lib at
3065 all. */
3066 add_needed = (elf_dyn_lib_class (abfd)
3067 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3068 | DYN_NO_NEEDED)) == 0;
3070 s = bfd_get_section_by_name (abfd, ".dynamic");
3071 if (s != NULL)
3073 bfd_byte *dynbuf;
3074 bfd_byte *extdyn;
3075 int elfsec;
3076 unsigned long shlink;
3078 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3079 goto error_free_dyn;
3081 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3082 if (elfsec == -1)
3083 goto error_free_dyn;
3084 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3086 for (extdyn = dynbuf;
3087 extdyn < dynbuf + s->size;
3088 extdyn += bed->s->sizeof_dyn)
3090 Elf_Internal_Dyn dyn;
3092 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3093 if (dyn.d_tag == DT_SONAME)
3095 unsigned int tagv = dyn.d_un.d_val;
3096 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3097 if (soname == NULL)
3098 goto error_free_dyn;
3100 if (dyn.d_tag == DT_NEEDED)
3102 struct bfd_link_needed_list *n, **pn;
3103 char *fnm, *anm;
3104 unsigned int tagv = dyn.d_un.d_val;
3106 amt = sizeof (struct bfd_link_needed_list);
3107 n = bfd_alloc (abfd, amt);
3108 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3109 if (n == NULL || fnm == NULL)
3110 goto error_free_dyn;
3111 amt = strlen (fnm) + 1;
3112 anm = bfd_alloc (abfd, amt);
3113 if (anm == NULL)
3114 goto error_free_dyn;
3115 memcpy (anm, fnm, amt);
3116 n->name = anm;
3117 n->by = abfd;
3118 n->next = NULL;
3119 for (pn = & hash_table->needed;
3120 *pn != NULL;
3121 pn = &(*pn)->next)
3123 *pn = n;
3125 if (dyn.d_tag == DT_RUNPATH)
3127 struct bfd_link_needed_list *n, **pn;
3128 char *fnm, *anm;
3129 unsigned int tagv = dyn.d_un.d_val;
3131 amt = sizeof (struct bfd_link_needed_list);
3132 n = bfd_alloc (abfd, amt);
3133 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3134 if (n == NULL || fnm == NULL)
3135 goto error_free_dyn;
3136 amt = strlen (fnm) + 1;
3137 anm = bfd_alloc (abfd, amt);
3138 if (anm == NULL)
3139 goto error_free_dyn;
3140 memcpy (anm, fnm, amt);
3141 n->name = anm;
3142 n->by = abfd;
3143 n->next = NULL;
3144 for (pn = & runpath;
3145 *pn != NULL;
3146 pn = &(*pn)->next)
3148 *pn = n;
3150 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3151 if (!runpath && dyn.d_tag == DT_RPATH)
3153 struct bfd_link_needed_list *n, **pn;
3154 char *fnm, *anm;
3155 unsigned int tagv = dyn.d_un.d_val;
3157 amt = sizeof (struct bfd_link_needed_list);
3158 n = bfd_alloc (abfd, amt);
3159 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3160 if (n == NULL || fnm == NULL)
3161 goto error_free_dyn;
3162 amt = strlen (fnm) + 1;
3163 anm = bfd_alloc (abfd, amt);
3164 if (anm == NULL)
3166 error_free_dyn:
3167 free (dynbuf);
3168 goto error_return;
3170 memcpy (anm, fnm, amt);
3171 n->name = anm;
3172 n->by = abfd;
3173 n->next = NULL;
3174 for (pn = & rpath;
3175 *pn != NULL;
3176 pn = &(*pn)->next)
3178 *pn = n;
3182 free (dynbuf);
3185 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3186 frees all more recently bfd_alloc'd blocks as well. */
3187 if (runpath)
3188 rpath = runpath;
3190 if (rpath)
3192 struct bfd_link_needed_list **pn;
3193 for (pn = & hash_table->runpath;
3194 *pn != NULL;
3195 pn = &(*pn)->next)
3197 *pn = rpath;
3200 /* We do not want to include any of the sections in a dynamic
3201 object in the output file. We hack by simply clobbering the
3202 list of sections in the BFD. This could be handled more
3203 cleanly by, say, a new section flag; the existing
3204 SEC_NEVER_LOAD flag is not the one we want, because that one
3205 still implies that the section takes up space in the output
3206 file. */
3207 bfd_section_list_clear (abfd);
3209 /* If this is the first dynamic object found in the link, create
3210 the special sections required for dynamic linking. */
3211 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3212 goto error_return;
3214 /* Find the name to use in a DT_NEEDED entry that refers to this
3215 object. If the object has a DT_SONAME entry, we use it.
3216 Otherwise, if the generic linker stuck something in
3217 elf_dt_name, we use that. Otherwise, we just use the file
3218 name. */
3219 if (soname == NULL || *soname == '\0')
3221 soname = elf_dt_name (abfd);
3222 if (soname == NULL || *soname == '\0')
3223 soname = bfd_get_filename (abfd);
3226 /* Save the SONAME because sometimes the linker emulation code
3227 will need to know it. */
3228 elf_dt_name (abfd) = soname;
3230 ret = elf_add_dt_needed_tag (info, soname, add_needed);
3231 if (ret < 0)
3232 goto error_return;
3234 /* If we have already included this dynamic object in the
3235 link, just ignore it. There is no reason to include a
3236 particular dynamic object more than once. */
3237 if (ret > 0)
3238 return TRUE;
3241 /* If this is a dynamic object, we always link against the .dynsym
3242 symbol table, not the .symtab symbol table. The dynamic linker
3243 will only see the .dynsym symbol table, so there is no reason to
3244 look at .symtab for a dynamic object. */
3246 if (! dynamic || elf_dynsymtab (abfd) == 0)
3247 hdr = &elf_tdata (abfd)->symtab_hdr;
3248 else
3249 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3251 symcount = hdr->sh_size / bed->s->sizeof_sym;
3253 /* The sh_info field of the symtab header tells us where the
3254 external symbols start. We don't care about the local symbols at
3255 this point. */
3256 if (elf_bad_symtab (abfd))
3258 extsymcount = symcount;
3259 extsymoff = 0;
3261 else
3263 extsymcount = symcount - hdr->sh_info;
3264 extsymoff = hdr->sh_info;
3267 sym_hash = NULL;
3268 if (extsymcount != 0)
3270 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3271 NULL, NULL, NULL);
3272 if (isymbuf == NULL)
3273 goto error_return;
3275 /* We store a pointer to the hash table entry for each external
3276 symbol. */
3277 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3278 sym_hash = bfd_alloc (abfd, amt);
3279 if (sym_hash == NULL)
3280 goto error_free_sym;
3281 elf_sym_hashes (abfd) = sym_hash;
3284 if (dynamic)
3286 /* Read in any version definitions. */
3287 if (! _bfd_elf_slurp_version_tables (abfd))
3288 goto error_free_sym;
3290 /* Read in the symbol versions, but don't bother to convert them
3291 to internal format. */
3292 if (elf_dynversym (abfd) != 0)
3294 Elf_Internal_Shdr *versymhdr;
3296 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3297 extversym = bfd_malloc (versymhdr->sh_size);
3298 if (extversym == NULL)
3299 goto error_free_sym;
3300 amt = versymhdr->sh_size;
3301 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3302 || bfd_bread (extversym, amt, abfd) != amt)
3303 goto error_free_vers;
3307 weaks = NULL;
3309 ever = extversym != NULL ? extversym + extsymoff : NULL;
3310 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3311 isym < isymend;
3312 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3314 int bind;
3315 bfd_vma value;
3316 asection *sec;
3317 flagword flags;
3318 const char *name;
3319 struct elf_link_hash_entry *h;
3320 bfd_boolean definition;
3321 bfd_boolean size_change_ok;
3322 bfd_boolean type_change_ok;
3323 bfd_boolean new_weakdef;
3324 bfd_boolean override;
3325 unsigned int old_alignment;
3326 bfd *old_bfd;
3328 override = FALSE;
3330 flags = BSF_NO_FLAGS;
3331 sec = NULL;
3332 value = isym->st_value;
3333 *sym_hash = NULL;
3335 bind = ELF_ST_BIND (isym->st_info);
3336 if (bind == STB_LOCAL)
3338 /* This should be impossible, since ELF requires that all
3339 global symbols follow all local symbols, and that sh_info
3340 point to the first global symbol. Unfortunately, Irix 5
3341 screws this up. */
3342 continue;
3344 else if (bind == STB_GLOBAL)
3346 if (isym->st_shndx != SHN_UNDEF
3347 && isym->st_shndx != SHN_COMMON)
3348 flags = BSF_GLOBAL;
3350 else if (bind == STB_WEAK)
3351 flags = BSF_WEAK;
3352 else
3354 /* Leave it up to the processor backend. */
3357 if (isym->st_shndx == SHN_UNDEF)
3358 sec = bfd_und_section_ptr;
3359 else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
3361 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3362 if (sec == NULL)
3363 sec = bfd_abs_section_ptr;
3364 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3365 value -= sec->vma;
3367 else if (isym->st_shndx == SHN_ABS)
3368 sec = bfd_abs_section_ptr;
3369 else if (isym->st_shndx == SHN_COMMON)
3371 sec = bfd_com_section_ptr;
3372 /* What ELF calls the size we call the value. What ELF
3373 calls the value we call the alignment. */
3374 value = isym->st_size;
3376 else
3378 /* Leave it up to the processor backend. */
3381 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3382 isym->st_name);
3383 if (name == NULL)
3384 goto error_free_vers;
3386 if (isym->st_shndx == SHN_COMMON
3387 && ELF_ST_TYPE (isym->st_info) == STT_TLS)
3389 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3391 if (tcomm == NULL)
3393 tcomm = bfd_make_section (abfd, ".tcommon");
3394 if (tcomm == NULL
3395 || !bfd_set_section_flags (abfd, tcomm, (SEC_ALLOC
3396 | SEC_IS_COMMON
3397 | SEC_LINKER_CREATED
3398 | SEC_THREAD_LOCAL)))
3399 goto error_free_vers;
3401 sec = tcomm;
3403 else if (add_symbol_hook)
3405 if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec,
3406 &value))
3407 goto error_free_vers;
3409 /* The hook function sets the name to NULL if this symbol
3410 should be skipped for some reason. */
3411 if (name == NULL)
3412 continue;
3415 /* Sanity check that all possibilities were handled. */
3416 if (sec == NULL)
3418 bfd_set_error (bfd_error_bad_value);
3419 goto error_free_vers;
3422 if (bfd_is_und_section (sec)
3423 || bfd_is_com_section (sec))
3424 definition = FALSE;
3425 else
3426 definition = TRUE;
3428 size_change_ok = FALSE;
3429 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
3430 old_alignment = 0;
3431 old_bfd = NULL;
3433 if (is_elf_hash_table (hash_table))
3435 Elf_Internal_Versym iver;
3436 unsigned int vernum = 0;
3437 bfd_boolean skip;
3439 if (ever != NULL)
3441 _bfd_elf_swap_versym_in (abfd, ever, &iver);
3442 vernum = iver.vs_vers & VERSYM_VERSION;
3444 /* If this is a hidden symbol, or if it is not version
3445 1, we append the version name to the symbol name.
3446 However, we do not modify a non-hidden absolute
3447 symbol, because it might be the version symbol
3448 itself. FIXME: What if it isn't? */
3449 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
3450 || (vernum > 1 && ! bfd_is_abs_section (sec)))
3452 const char *verstr;
3453 size_t namelen, verlen, newlen;
3454 char *newname, *p;
3456 if (isym->st_shndx != SHN_UNDEF)
3458 if (vernum > elf_tdata (abfd)->dynverdef_hdr.sh_info)
3460 (*_bfd_error_handler)
3461 (_("%B: %s: invalid version %u (max %d)"),
3462 abfd, name, vernum,
3463 elf_tdata (abfd)->dynverdef_hdr.sh_info);
3464 bfd_set_error (bfd_error_bad_value);
3465 goto error_free_vers;
3467 else if (vernum > 1)
3468 verstr =
3469 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
3470 else
3471 verstr = "";
3473 else
3475 /* We cannot simply test for the number of
3476 entries in the VERNEED section since the
3477 numbers for the needed versions do not start
3478 at 0. */
3479 Elf_Internal_Verneed *t;
3481 verstr = NULL;
3482 for (t = elf_tdata (abfd)->verref;
3483 t != NULL;
3484 t = t->vn_nextref)
3486 Elf_Internal_Vernaux *a;
3488 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3490 if (a->vna_other == vernum)
3492 verstr = a->vna_nodename;
3493 break;
3496 if (a != NULL)
3497 break;
3499 if (verstr == NULL)
3501 (*_bfd_error_handler)
3502 (_("%B: %s: invalid needed version %d"),
3503 abfd, name, vernum);
3504 bfd_set_error (bfd_error_bad_value);
3505 goto error_free_vers;
3509 namelen = strlen (name);
3510 verlen = strlen (verstr);
3511 newlen = namelen + verlen + 2;
3512 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3513 && isym->st_shndx != SHN_UNDEF)
3514 ++newlen;
3516 newname = bfd_alloc (abfd, newlen);
3517 if (newname == NULL)
3518 goto error_free_vers;
3519 memcpy (newname, name, namelen);
3520 p = newname + namelen;
3521 *p++ = ELF_VER_CHR;
3522 /* If this is a defined non-hidden version symbol,
3523 we add another @ to the name. This indicates the
3524 default version of the symbol. */
3525 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3526 && isym->st_shndx != SHN_UNDEF)
3527 *p++ = ELF_VER_CHR;
3528 memcpy (p, verstr, verlen + 1);
3530 name = newname;
3534 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
3535 sym_hash, &skip, &override,
3536 &type_change_ok, &size_change_ok))
3537 goto error_free_vers;
3539 if (skip)
3540 continue;
3542 if (override)
3543 definition = FALSE;
3545 h = *sym_hash;
3546 while (h->root.type == bfd_link_hash_indirect
3547 || h->root.type == bfd_link_hash_warning)
3548 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3550 /* Remember the old alignment if this is a common symbol, so
3551 that we don't reduce the alignment later on. We can't
3552 check later, because _bfd_generic_link_add_one_symbol
3553 will set a default for the alignment which we want to
3554 override. We also remember the old bfd where the existing
3555 definition comes from. */
3556 switch (h->root.type)
3558 default:
3559 break;
3561 case bfd_link_hash_defined:
3562 case bfd_link_hash_defweak:
3563 old_bfd = h->root.u.def.section->owner;
3564 break;
3566 case bfd_link_hash_common:
3567 old_bfd = h->root.u.c.p->section->owner;
3568 old_alignment = h->root.u.c.p->alignment_power;
3569 break;
3572 if (elf_tdata (abfd)->verdef != NULL
3573 && ! override
3574 && vernum > 1
3575 && definition)
3576 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
3579 if (! (_bfd_generic_link_add_one_symbol
3580 (info, abfd, name, flags, sec, value, NULL, FALSE, collect,
3581 (struct bfd_link_hash_entry **) sym_hash)))
3582 goto error_free_vers;
3584 h = *sym_hash;
3585 while (h->root.type == bfd_link_hash_indirect
3586 || h->root.type == bfd_link_hash_warning)
3587 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3588 *sym_hash = h;
3590 new_weakdef = FALSE;
3591 if (dynamic
3592 && definition
3593 && (flags & BSF_WEAK) != 0
3594 && ELF_ST_TYPE (isym->st_info) != STT_FUNC
3595 && is_elf_hash_table (hash_table)
3596 && h->u.weakdef == NULL)
3598 /* Keep a list of all weak defined non function symbols from
3599 a dynamic object, using the weakdef field. Later in this
3600 function we will set the weakdef field to the correct
3601 value. We only put non-function symbols from dynamic
3602 objects on this list, because that happens to be the only
3603 time we need to know the normal symbol corresponding to a
3604 weak symbol, and the information is time consuming to
3605 figure out. If the weakdef field is not already NULL,
3606 then this symbol was already defined by some previous
3607 dynamic object, and we will be using that previous
3608 definition anyhow. */
3610 h->u.weakdef = weaks;
3611 weaks = h;
3612 new_weakdef = TRUE;
3615 /* Set the alignment of a common symbol. */
3616 if (isym->st_shndx == SHN_COMMON
3617 && h->root.type == bfd_link_hash_common)
3619 unsigned int align;
3621 align = bfd_log2 (isym->st_value);
3622 if (align > old_alignment
3623 /* Permit an alignment power of zero if an alignment of one
3624 is specified and no other alignments have been specified. */
3625 || (isym->st_value == 1 && old_alignment == 0))
3626 h->root.u.c.p->alignment_power = align;
3627 else
3628 h->root.u.c.p->alignment_power = old_alignment;
3631 if (is_elf_hash_table (hash_table))
3633 bfd_boolean dynsym;
3635 /* Check the alignment when a common symbol is involved. This
3636 can change when a common symbol is overridden by a normal
3637 definition or a common symbol is ignored due to the old
3638 normal definition. We need to make sure the maximum
3639 alignment is maintained. */
3640 if ((old_alignment || isym->st_shndx == SHN_COMMON)
3641 && h->root.type != bfd_link_hash_common)
3643 unsigned int common_align;
3644 unsigned int normal_align;
3645 unsigned int symbol_align;
3646 bfd *normal_bfd;
3647 bfd *common_bfd;
3649 symbol_align = ffs (h->root.u.def.value) - 1;
3650 if (h->root.u.def.section->owner != NULL
3651 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3653 normal_align = h->root.u.def.section->alignment_power;
3654 if (normal_align > symbol_align)
3655 normal_align = symbol_align;
3657 else
3658 normal_align = symbol_align;
3660 if (old_alignment)
3662 common_align = old_alignment;
3663 common_bfd = old_bfd;
3664 normal_bfd = abfd;
3666 else
3668 common_align = bfd_log2 (isym->st_value);
3669 common_bfd = abfd;
3670 normal_bfd = old_bfd;
3673 if (normal_align < common_align)
3674 (*_bfd_error_handler)
3675 (_("Warning: alignment %u of symbol `%s' in %B"
3676 " is smaller than %u in %B"),
3677 normal_bfd, common_bfd,
3678 1 << normal_align, name, 1 << common_align);
3681 /* Remember the symbol size and type. */
3682 if (isym->st_size != 0
3683 && (definition || h->size == 0))
3685 if (h->size != 0 && h->size != isym->st_size && ! size_change_ok)
3686 (*_bfd_error_handler)
3687 (_("Warning: size of symbol `%s' changed"
3688 " from %lu in %B to %lu in %B"),
3689 old_bfd, abfd,
3690 name, (unsigned long) h->size,
3691 (unsigned long) isym->st_size);
3693 h->size = isym->st_size;
3696 /* If this is a common symbol, then we always want H->SIZE
3697 to be the size of the common symbol. The code just above
3698 won't fix the size if a common symbol becomes larger. We
3699 don't warn about a size change here, because that is
3700 covered by --warn-common. */
3701 if (h->root.type == bfd_link_hash_common)
3702 h->size = h->root.u.c.size;
3704 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
3705 && (definition || h->type == STT_NOTYPE))
3707 if (h->type != STT_NOTYPE
3708 && h->type != ELF_ST_TYPE (isym->st_info)
3709 && ! type_change_ok)
3710 (*_bfd_error_handler)
3711 (_("Warning: type of symbol `%s' changed"
3712 " from %d to %d in %B"),
3713 abfd, name, h->type, ELF_ST_TYPE (isym->st_info));
3715 h->type = ELF_ST_TYPE (isym->st_info);
3718 /* If st_other has a processor-specific meaning, specific
3719 code might be needed here. We never merge the visibility
3720 attribute with the one from a dynamic object. */
3721 if (bed->elf_backend_merge_symbol_attribute)
3722 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
3723 dynamic);
3725 if (isym->st_other != 0 && !dynamic)
3727 unsigned char hvis, symvis, other, nvis;
3729 /* Take the balance of OTHER from the definition. */
3730 other = (definition ? isym->st_other : h->other);
3731 other &= ~ ELF_ST_VISIBILITY (-1);
3733 /* Combine visibilities, using the most constraining one. */
3734 hvis = ELF_ST_VISIBILITY (h->other);
3735 symvis = ELF_ST_VISIBILITY (isym->st_other);
3736 if (! hvis)
3737 nvis = symvis;
3738 else if (! symvis)
3739 nvis = hvis;
3740 else
3741 nvis = hvis < symvis ? hvis : symvis;
3743 h->other = other | nvis;
3746 /* Set a flag in the hash table entry indicating the type of
3747 reference or definition we just found. Keep a count of
3748 the number of dynamic symbols we find. A dynamic symbol
3749 is one which is referenced or defined by both a regular
3750 object and a shared object. */
3751 dynsym = FALSE;
3752 if (! dynamic)
3754 if (! definition)
3756 h->ref_regular = 1;
3757 if (bind != STB_WEAK)
3758 h->ref_regular_nonweak = 1;
3760 else
3761 h->def_regular = 1;
3762 if (! info->executable
3763 || h->def_dynamic
3764 || h->ref_dynamic)
3765 dynsym = TRUE;
3767 else
3769 if (! definition)
3770 h->ref_dynamic = 1;
3771 else
3772 h->def_dynamic = 1;
3773 if (h->def_regular
3774 || h->ref_regular
3775 || (h->u.weakdef != NULL
3776 && ! new_weakdef
3777 && h->u.weakdef->dynindx != -1))
3778 dynsym = TRUE;
3781 /* Check to see if we need to add an indirect symbol for
3782 the default name. */
3783 if (definition || h->root.type == bfd_link_hash_common)
3784 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
3785 &sec, &value, &dynsym,
3786 override))
3787 goto error_free_vers;
3789 if (definition && !dynamic)
3791 char *p = strchr (name, ELF_VER_CHR);
3792 if (p != NULL && p[1] != ELF_VER_CHR)
3794 /* Queue non-default versions so that .symver x, x@FOO
3795 aliases can be checked. */
3796 if (! nondeflt_vers)
3798 amt = (isymend - isym + 1)
3799 * sizeof (struct elf_link_hash_entry *);
3800 nondeflt_vers = bfd_malloc (amt);
3802 nondeflt_vers [nondeflt_vers_cnt++] = h;
3806 if (dynsym && h->dynindx == -1)
3808 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3809 goto error_free_vers;
3810 if (h->u.weakdef != NULL
3811 && ! new_weakdef
3812 && h->u.weakdef->dynindx == -1)
3814 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
3815 goto error_free_vers;
3818 else if (dynsym && h->dynindx != -1)
3819 /* If the symbol already has a dynamic index, but
3820 visibility says it should not be visible, turn it into
3821 a local symbol. */
3822 switch (ELF_ST_VISIBILITY (h->other))
3824 case STV_INTERNAL:
3825 case STV_HIDDEN:
3826 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
3827 dynsym = FALSE;
3828 break;
3831 if (!add_needed
3832 && definition
3833 && dynsym
3834 && h->ref_regular)
3836 int ret;
3837 const char *soname = elf_dt_name (abfd);
3839 /* A symbol from a library loaded via DT_NEEDED of some
3840 other library is referenced by a regular object.
3841 Add a DT_NEEDED entry for it. Issue an error if
3842 --no-add-needed is used. */
3843 if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
3845 (*_bfd_error_handler)
3846 (_("%s: invalid DSO for symbol `%s' definition"),
3847 abfd, name);
3848 bfd_set_error (bfd_error_bad_value);
3849 goto error_free_vers;
3852 add_needed = TRUE;
3853 ret = elf_add_dt_needed_tag (info, soname, add_needed);
3854 if (ret < 0)
3855 goto error_free_vers;
3857 BFD_ASSERT (ret == 0);
3862 /* Now that all the symbols from this input file are created, handle
3863 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
3864 if (nondeflt_vers != NULL)
3866 bfd_size_type cnt, symidx;
3868 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
3870 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
3871 char *shortname, *p;
3873 p = strchr (h->root.root.string, ELF_VER_CHR);
3874 if (p == NULL
3875 || (h->root.type != bfd_link_hash_defined
3876 && h->root.type != bfd_link_hash_defweak))
3877 continue;
3879 amt = p - h->root.root.string;
3880 shortname = bfd_malloc (amt + 1);
3881 memcpy (shortname, h->root.root.string, amt);
3882 shortname[amt] = '\0';
3884 hi = (struct elf_link_hash_entry *)
3885 bfd_link_hash_lookup (&hash_table->root, shortname,
3886 FALSE, FALSE, FALSE);
3887 if (hi != NULL
3888 && hi->root.type == h->root.type
3889 && hi->root.u.def.value == h->root.u.def.value
3890 && hi->root.u.def.section == h->root.u.def.section)
3892 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
3893 hi->root.type = bfd_link_hash_indirect;
3894 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
3895 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
3896 sym_hash = elf_sym_hashes (abfd);
3897 if (sym_hash)
3898 for (symidx = 0; symidx < extsymcount; ++symidx)
3899 if (sym_hash[symidx] == hi)
3901 sym_hash[symidx] = h;
3902 break;
3905 free (shortname);
3907 free (nondeflt_vers);
3908 nondeflt_vers = NULL;
3911 if (extversym != NULL)
3913 free (extversym);
3914 extversym = NULL;
3917 if (isymbuf != NULL)
3918 free (isymbuf);
3919 isymbuf = NULL;
3921 /* Now set the weakdefs field correctly for all the weak defined
3922 symbols we found. The only way to do this is to search all the
3923 symbols. Since we only need the information for non functions in
3924 dynamic objects, that's the only time we actually put anything on
3925 the list WEAKS. We need this information so that if a regular
3926 object refers to a symbol defined weakly in a dynamic object, the
3927 real symbol in the dynamic object is also put in the dynamic
3928 symbols; we also must arrange for both symbols to point to the
3929 same memory location. We could handle the general case of symbol
3930 aliasing, but a general symbol alias can only be generated in
3931 assembler code, handling it correctly would be very time
3932 consuming, and other ELF linkers don't handle general aliasing
3933 either. */
3934 if (weaks != NULL)
3936 struct elf_link_hash_entry **hpp;
3937 struct elf_link_hash_entry **hppend;
3938 struct elf_link_hash_entry **sorted_sym_hash;
3939 struct elf_link_hash_entry *h;
3940 size_t sym_count;
3942 /* Since we have to search the whole symbol list for each weak
3943 defined symbol, search time for N weak defined symbols will be
3944 O(N^2). Binary search will cut it down to O(NlogN). */
3945 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3946 sorted_sym_hash = bfd_malloc (amt);
3947 if (sorted_sym_hash == NULL)
3948 goto error_return;
3949 sym_hash = sorted_sym_hash;
3950 hpp = elf_sym_hashes (abfd);
3951 hppend = hpp + extsymcount;
3952 sym_count = 0;
3953 for (; hpp < hppend; hpp++)
3955 h = *hpp;
3956 if (h != NULL
3957 && h->root.type == bfd_link_hash_defined
3958 && h->type != STT_FUNC)
3960 *sym_hash = h;
3961 sym_hash++;
3962 sym_count++;
3966 qsort (sorted_sym_hash, sym_count,
3967 sizeof (struct elf_link_hash_entry *),
3968 elf_sort_symbol);
3970 while (weaks != NULL)
3972 struct elf_link_hash_entry *hlook;
3973 asection *slook;
3974 bfd_vma vlook;
3975 long ilook;
3976 size_t i, j, idx;
3978 hlook = weaks;
3979 weaks = hlook->u.weakdef;
3980 hlook->u.weakdef = NULL;
3982 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
3983 || hlook->root.type == bfd_link_hash_defweak
3984 || hlook->root.type == bfd_link_hash_common
3985 || hlook->root.type == bfd_link_hash_indirect);
3986 slook = hlook->root.u.def.section;
3987 vlook = hlook->root.u.def.value;
3989 ilook = -1;
3990 i = 0;
3991 j = sym_count;
3992 while (i < j)
3994 bfd_signed_vma vdiff;
3995 idx = (i + j) / 2;
3996 h = sorted_sym_hash [idx];
3997 vdiff = vlook - h->root.u.def.value;
3998 if (vdiff < 0)
3999 j = idx;
4000 else if (vdiff > 0)
4001 i = idx + 1;
4002 else
4004 long sdiff = slook->id - h->root.u.def.section->id;
4005 if (sdiff < 0)
4006 j = idx;
4007 else if (sdiff > 0)
4008 i = idx + 1;
4009 else
4011 ilook = idx;
4012 break;
4017 /* We didn't find a value/section match. */
4018 if (ilook == -1)
4019 continue;
4021 for (i = ilook; i < sym_count; i++)
4023 h = sorted_sym_hash [i];
4025 /* Stop if value or section doesn't match. */
4026 if (h->root.u.def.value != vlook
4027 || h->root.u.def.section != slook)
4028 break;
4029 else if (h != hlook)
4031 hlook->u.weakdef = h;
4033 /* If the weak definition is in the list of dynamic
4034 symbols, make sure the real definition is put
4035 there as well. */
4036 if (hlook->dynindx != -1 && h->dynindx == -1)
4038 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4039 goto error_return;
4042 /* If the real definition is in the list of dynamic
4043 symbols, make sure the weak definition is put
4044 there as well. If we don't do this, then the
4045 dynamic loader might not merge the entries for the
4046 real definition and the weak definition. */
4047 if (h->dynindx != -1 && hlook->dynindx == -1)
4049 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4050 goto error_return;
4052 break;
4057 free (sorted_sym_hash);
4060 check_directives = get_elf_backend_data (abfd)->check_directives;
4061 if (check_directives)
4062 check_directives (abfd, info);
4064 /* If this object is the same format as the output object, and it is
4065 not a shared library, then let the backend look through the
4066 relocs.
4068 This is required to build global offset table entries and to
4069 arrange for dynamic relocs. It is not required for the
4070 particular common case of linking non PIC code, even when linking
4071 against shared libraries, but unfortunately there is no way of
4072 knowing whether an object file has been compiled PIC or not.
4073 Looking through the relocs is not particularly time consuming.
4074 The problem is that we must either (1) keep the relocs in memory,
4075 which causes the linker to require additional runtime memory or
4076 (2) read the relocs twice from the input file, which wastes time.
4077 This would be a good case for using mmap.
4079 I have no idea how to handle linking PIC code into a file of a
4080 different format. It probably can't be done. */
4081 check_relocs = get_elf_backend_data (abfd)->check_relocs;
4082 if (! dynamic
4083 && is_elf_hash_table (hash_table)
4084 && hash_table->root.creator == abfd->xvec
4085 && check_relocs != NULL)
4087 asection *o;
4089 for (o = abfd->sections; o != NULL; o = o->next)
4091 Elf_Internal_Rela *internal_relocs;
4092 bfd_boolean ok;
4094 if ((o->flags & SEC_RELOC) == 0
4095 || o->reloc_count == 0
4096 || ((info->strip == strip_all || info->strip == strip_debugger)
4097 && (o->flags & SEC_DEBUGGING) != 0)
4098 || bfd_is_abs_section (o->output_section))
4099 continue;
4101 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4102 info->keep_memory);
4103 if (internal_relocs == NULL)
4104 goto error_return;
4106 ok = (*check_relocs) (abfd, info, o, internal_relocs);
4108 if (elf_section_data (o)->relocs != internal_relocs)
4109 free (internal_relocs);
4111 if (! ok)
4112 goto error_return;
4116 /* If this is a non-traditional link, try to optimize the handling
4117 of the .stab/.stabstr sections. */
4118 if (! dynamic
4119 && ! info->traditional_format
4120 && is_elf_hash_table (hash_table)
4121 && (info->strip != strip_all && info->strip != strip_debugger))
4123 asection *stabstr;
4125 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4126 if (stabstr != NULL)
4128 bfd_size_type string_offset = 0;
4129 asection *stab;
4131 for (stab = abfd->sections; stab; stab = stab->next)
4132 if (strncmp (".stab", stab->name, 5) == 0
4133 && (!stab->name[5] ||
4134 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4135 && (stab->flags & SEC_MERGE) == 0
4136 && !bfd_is_abs_section (stab->output_section))
4138 struct bfd_elf_section_data *secdata;
4140 secdata = elf_section_data (stab);
4141 if (! _bfd_link_section_stabs (abfd,
4142 &hash_table->stab_info,
4143 stab, stabstr,
4144 &secdata->sec_info,
4145 &string_offset))
4146 goto error_return;
4147 if (secdata->sec_info)
4148 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4153 if (is_elf_hash_table (hash_table))
4155 /* Add this bfd to the loaded list. */
4156 struct elf_link_loaded_list *n;
4158 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4159 if (n == NULL)
4160 goto error_return;
4161 n->abfd = abfd;
4162 n->next = hash_table->loaded;
4163 hash_table->loaded = n;
4166 return TRUE;
4168 error_free_vers:
4169 if (nondeflt_vers != NULL)
4170 free (nondeflt_vers);
4171 if (extversym != NULL)
4172 free (extversym);
4173 error_free_sym:
4174 if (isymbuf != NULL)
4175 free (isymbuf);
4176 error_return:
4177 return FALSE;
4180 /* Return the linker hash table entry of a symbol that might be
4181 satisfied by an archive symbol. Return -1 on error. */
4183 struct elf_link_hash_entry *
4184 _bfd_elf_archive_symbol_lookup (bfd *abfd,
4185 struct bfd_link_info *info,
4186 const char *name)
4188 struct elf_link_hash_entry *h;
4189 char *p, *copy;
4190 size_t len, first;
4192 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4193 if (h != NULL)
4194 return h;
4196 /* If this is a default version (the name contains @@), look up the
4197 symbol again with only one `@' as well as without the version.
4198 The effect is that references to the symbol with and without the
4199 version will be matched by the default symbol in the archive. */
4201 p = strchr (name, ELF_VER_CHR);
4202 if (p == NULL || p[1] != ELF_VER_CHR)
4203 return h;
4205 /* First check with only one `@'. */
4206 len = strlen (name);
4207 copy = bfd_alloc (abfd, len);
4208 if (copy == NULL)
4209 return (struct elf_link_hash_entry *) 0 - 1;
4211 first = p - name + 1;
4212 memcpy (copy, name, first);
4213 memcpy (copy + first, name + first + 1, len - first);
4215 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
4216 if (h == NULL)
4218 /* We also need to check references to the symbol without the
4219 version. */
4220 copy[first - 1] = '\0';
4221 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4222 FALSE, FALSE, FALSE);
4225 bfd_release (abfd, copy);
4226 return h;
4229 /* Add symbols from an ELF archive file to the linker hash table. We
4230 don't use _bfd_generic_link_add_archive_symbols because of a
4231 problem which arises on UnixWare. The UnixWare libc.so is an
4232 archive which includes an entry libc.so.1 which defines a bunch of
4233 symbols. The libc.so archive also includes a number of other
4234 object files, which also define symbols, some of which are the same
4235 as those defined in libc.so.1. Correct linking requires that we
4236 consider each object file in turn, and include it if it defines any
4237 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4238 this; it looks through the list of undefined symbols, and includes
4239 any object file which defines them. When this algorithm is used on
4240 UnixWare, it winds up pulling in libc.so.1 early and defining a
4241 bunch of symbols. This means that some of the other objects in the
4242 archive are not included in the link, which is incorrect since they
4243 precede libc.so.1 in the archive.
4245 Fortunately, ELF archive handling is simpler than that done by
4246 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4247 oddities. In ELF, if we find a symbol in the archive map, and the
4248 symbol is currently undefined, we know that we must pull in that
4249 object file.
4251 Unfortunately, we do have to make multiple passes over the symbol
4252 table until nothing further is resolved. */
4254 static bfd_boolean
4255 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
4257 symindex c;
4258 bfd_boolean *defined = NULL;
4259 bfd_boolean *included = NULL;
4260 carsym *symdefs;
4261 bfd_boolean loop;
4262 bfd_size_type amt;
4263 const struct elf_backend_data *bed;
4264 struct elf_link_hash_entry * (*archive_symbol_lookup)
4265 (bfd *, struct bfd_link_info *, const char *);
4267 if (! bfd_has_map (abfd))
4269 /* An empty archive is a special case. */
4270 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4271 return TRUE;
4272 bfd_set_error (bfd_error_no_armap);
4273 return FALSE;
4276 /* Keep track of all symbols we know to be already defined, and all
4277 files we know to be already included. This is to speed up the
4278 second and subsequent passes. */
4279 c = bfd_ardata (abfd)->symdef_count;
4280 if (c == 0)
4281 return TRUE;
4282 amt = c;
4283 amt *= sizeof (bfd_boolean);
4284 defined = bfd_zmalloc (amt);
4285 included = bfd_zmalloc (amt);
4286 if (defined == NULL || included == NULL)
4287 goto error_return;
4289 symdefs = bfd_ardata (abfd)->symdefs;
4290 bed = get_elf_backend_data (abfd);
4291 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
4295 file_ptr last;
4296 symindex i;
4297 carsym *symdef;
4298 carsym *symdefend;
4300 loop = FALSE;
4301 last = -1;
4303 symdef = symdefs;
4304 symdefend = symdef + c;
4305 for (i = 0; symdef < symdefend; symdef++, i++)
4307 struct elf_link_hash_entry *h;
4308 bfd *element;
4309 struct bfd_link_hash_entry *undefs_tail;
4310 symindex mark;
4312 if (defined[i] || included[i])
4313 continue;
4314 if (symdef->file_offset == last)
4316 included[i] = TRUE;
4317 continue;
4320 h = archive_symbol_lookup (abfd, info, symdef->name);
4321 if (h == (struct elf_link_hash_entry *) 0 - 1)
4322 goto error_return;
4324 if (h == NULL)
4325 continue;
4327 if (h->root.type == bfd_link_hash_common)
4329 /* We currently have a common symbol. The archive map contains
4330 a reference to this symbol, so we may want to include it. We
4331 only want to include it however, if this archive element
4332 contains a definition of the symbol, not just another common
4333 declaration of it.
4335 Unfortunately some archivers (including GNU ar) will put
4336 declarations of common symbols into their archive maps, as
4337 well as real definitions, so we cannot just go by the archive
4338 map alone. Instead we must read in the element's symbol
4339 table and check that to see what kind of symbol definition
4340 this is. */
4341 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
4342 continue;
4344 else if (h->root.type != bfd_link_hash_undefined)
4346 if (h->root.type != bfd_link_hash_undefweak)
4347 defined[i] = TRUE;
4348 continue;
4351 /* We need to include this archive member. */
4352 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4353 if (element == NULL)
4354 goto error_return;
4356 if (! bfd_check_format (element, bfd_object))
4357 goto error_return;
4359 /* Doublecheck that we have not included this object
4360 already--it should be impossible, but there may be
4361 something wrong with the archive. */
4362 if (element->archive_pass != 0)
4364 bfd_set_error (bfd_error_bad_value);
4365 goto error_return;
4367 element->archive_pass = 1;
4369 undefs_tail = info->hash->undefs_tail;
4371 if (! (*info->callbacks->add_archive_element) (info, element,
4372 symdef->name))
4373 goto error_return;
4374 if (! bfd_link_add_symbols (element, info))
4375 goto error_return;
4377 /* If there are any new undefined symbols, we need to make
4378 another pass through the archive in order to see whether
4379 they can be defined. FIXME: This isn't perfect, because
4380 common symbols wind up on undefs_tail and because an
4381 undefined symbol which is defined later on in this pass
4382 does not require another pass. This isn't a bug, but it
4383 does make the code less efficient than it could be. */
4384 if (undefs_tail != info->hash->undefs_tail)
4385 loop = TRUE;
4387 /* Look backward to mark all symbols from this object file
4388 which we have already seen in this pass. */
4389 mark = i;
4392 included[mark] = TRUE;
4393 if (mark == 0)
4394 break;
4395 --mark;
4397 while (symdefs[mark].file_offset == symdef->file_offset);
4399 /* We mark subsequent symbols from this object file as we go
4400 on through the loop. */
4401 last = symdef->file_offset;
4404 while (loop);
4406 free (defined);
4407 free (included);
4409 return TRUE;
4411 error_return:
4412 if (defined != NULL)
4413 free (defined);
4414 if (included != NULL)
4415 free (included);
4416 return FALSE;
4419 /* Given an ELF BFD, add symbols to the global hash table as
4420 appropriate. */
4422 bfd_boolean
4423 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
4425 switch (bfd_get_format (abfd))
4427 case bfd_object:
4428 return elf_link_add_object_symbols (abfd, info);
4429 case bfd_archive:
4430 return elf_link_add_archive_symbols (abfd, info);
4431 default:
4432 bfd_set_error (bfd_error_wrong_format);
4433 return FALSE;
4437 /* This function will be called though elf_link_hash_traverse to store
4438 all hash value of the exported symbols in an array. */
4440 static bfd_boolean
4441 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
4443 unsigned long **valuep = data;
4444 const char *name;
4445 char *p;
4446 unsigned long ha;
4447 char *alc = NULL;
4449 if (h->root.type == bfd_link_hash_warning)
4450 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4452 /* Ignore indirect symbols. These are added by the versioning code. */
4453 if (h->dynindx == -1)
4454 return TRUE;
4456 name = h->root.root.string;
4457 p = strchr (name, ELF_VER_CHR);
4458 if (p != NULL)
4460 alc = bfd_malloc (p - name + 1);
4461 memcpy (alc, name, p - name);
4462 alc[p - name] = '\0';
4463 name = alc;
4466 /* Compute the hash value. */
4467 ha = bfd_elf_hash (name);
4469 /* Store the found hash value in the array given as the argument. */
4470 *(*valuep)++ = ha;
4472 /* And store it in the struct so that we can put it in the hash table
4473 later. */
4474 h->u.elf_hash_value = ha;
4476 if (alc != NULL)
4477 free (alc);
4479 return TRUE;
4482 /* Array used to determine the number of hash table buckets to use
4483 based on the number of symbols there are. If there are fewer than
4484 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4485 fewer than 37 we use 17 buckets, and so forth. We never use more
4486 than 32771 buckets. */
4488 static const size_t elf_buckets[] =
4490 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4491 16411, 32771, 0
4494 /* Compute bucket count for hashing table. We do not use a static set
4495 of possible tables sizes anymore. Instead we determine for all
4496 possible reasonable sizes of the table the outcome (i.e., the
4497 number of collisions etc) and choose the best solution. The
4498 weighting functions are not too simple to allow the table to grow
4499 without bounds. Instead one of the weighting factors is the size.
4500 Therefore the result is always a good payoff between few collisions
4501 (= short chain lengths) and table size. */
4502 static size_t
4503 compute_bucket_count (struct bfd_link_info *info)
4505 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
4506 size_t best_size = 0;
4507 unsigned long int *hashcodes;
4508 unsigned long int *hashcodesp;
4509 unsigned long int i;
4510 bfd_size_type amt;
4512 /* Compute the hash values for all exported symbols. At the same
4513 time store the values in an array so that we could use them for
4514 optimizations. */
4515 amt = dynsymcount;
4516 amt *= sizeof (unsigned long int);
4517 hashcodes = bfd_malloc (amt);
4518 if (hashcodes == NULL)
4519 return 0;
4520 hashcodesp = hashcodes;
4522 /* Put all hash values in HASHCODES. */
4523 elf_link_hash_traverse (elf_hash_table (info),
4524 elf_collect_hash_codes, &hashcodesp);
4526 /* We have a problem here. The following code to optimize the table
4527 size requires an integer type with more the 32 bits. If
4528 BFD_HOST_U_64_BIT is set we know about such a type. */
4529 #ifdef BFD_HOST_U_64_BIT
4530 if (info->optimize)
4532 unsigned long int nsyms = hashcodesp - hashcodes;
4533 size_t minsize;
4534 size_t maxsize;
4535 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
4536 unsigned long int *counts ;
4537 bfd *dynobj = elf_hash_table (info)->dynobj;
4538 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
4540 /* Possible optimization parameters: if we have NSYMS symbols we say
4541 that the hashing table must at least have NSYMS/4 and at most
4542 2*NSYMS buckets. */
4543 minsize = nsyms / 4;
4544 if (minsize == 0)
4545 minsize = 1;
4546 best_size = maxsize = nsyms * 2;
4548 /* Create array where we count the collisions in. We must use bfd_malloc
4549 since the size could be large. */
4550 amt = maxsize;
4551 amt *= sizeof (unsigned long int);
4552 counts = bfd_malloc (amt);
4553 if (counts == NULL)
4555 free (hashcodes);
4556 return 0;
4559 /* Compute the "optimal" size for the hash table. The criteria is a
4560 minimal chain length. The minor criteria is (of course) the size
4561 of the table. */
4562 for (i = minsize; i < maxsize; ++i)
4564 /* Walk through the array of hashcodes and count the collisions. */
4565 BFD_HOST_U_64_BIT max;
4566 unsigned long int j;
4567 unsigned long int fact;
4569 memset (counts, '\0', i * sizeof (unsigned long int));
4571 /* Determine how often each hash bucket is used. */
4572 for (j = 0; j < nsyms; ++j)
4573 ++counts[hashcodes[j] % i];
4575 /* For the weight function we need some information about the
4576 pagesize on the target. This is information need not be 100%
4577 accurate. Since this information is not available (so far) we
4578 define it here to a reasonable default value. If it is crucial
4579 to have a better value some day simply define this value. */
4580 # ifndef BFD_TARGET_PAGESIZE
4581 # define BFD_TARGET_PAGESIZE (4096)
4582 # endif
4584 /* We in any case need 2 + NSYMS entries for the size values and
4585 the chains. */
4586 max = (2 + nsyms) * (bed->s->arch_size / 8);
4588 # if 1
4589 /* Variant 1: optimize for short chains. We add the squares
4590 of all the chain lengths (which favors many small chain
4591 over a few long chains). */
4592 for (j = 0; j < i; ++j)
4593 max += counts[j] * counts[j];
4595 /* This adds penalties for the overall size of the table. */
4596 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4597 max *= fact * fact;
4598 # else
4599 /* Variant 2: Optimize a lot more for small table. Here we
4600 also add squares of the size but we also add penalties for
4601 empty slots (the +1 term). */
4602 for (j = 0; j < i; ++j)
4603 max += (1 + counts[j]) * (1 + counts[j]);
4605 /* The overall size of the table is considered, but not as
4606 strong as in variant 1, where it is squared. */
4607 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4608 max *= fact;
4609 # endif
4611 /* Compare with current best results. */
4612 if (max < best_chlen)
4614 best_chlen = max;
4615 best_size = i;
4619 free (counts);
4621 else
4622 #endif /* defined (BFD_HOST_U_64_BIT) */
4624 /* This is the fallback solution if no 64bit type is available or if we
4625 are not supposed to spend much time on optimizations. We select the
4626 bucket count using a fixed set of numbers. */
4627 for (i = 0; elf_buckets[i] != 0; i++)
4629 best_size = elf_buckets[i];
4630 if (dynsymcount < elf_buckets[i + 1])
4631 break;
4635 /* Free the arrays we needed. */
4636 free (hashcodes);
4638 return best_size;
4641 /* Set up the sizes and contents of the ELF dynamic sections. This is
4642 called by the ELF linker emulation before_allocation routine. We
4643 must set the sizes of the sections before the linker sets the
4644 addresses of the various sections. */
4646 bfd_boolean
4647 bfd_elf_size_dynamic_sections (bfd *output_bfd,
4648 const char *soname,
4649 const char *rpath,
4650 const char *filter_shlib,
4651 const char * const *auxiliary_filters,
4652 struct bfd_link_info *info,
4653 asection **sinterpptr,
4654 struct bfd_elf_version_tree *verdefs)
4656 bfd_size_type soname_indx;
4657 bfd *dynobj;
4658 const struct elf_backend_data *bed;
4659 struct elf_assign_sym_version_info asvinfo;
4661 *sinterpptr = NULL;
4663 soname_indx = (bfd_size_type) -1;
4665 if (!is_elf_hash_table (info->hash))
4666 return TRUE;
4668 elf_tdata (output_bfd)->relro = info->relro;
4669 if (info->execstack)
4670 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
4671 else if (info->noexecstack)
4672 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
4673 else
4675 bfd *inputobj;
4676 asection *notesec = NULL;
4677 int exec = 0;
4679 for (inputobj = info->input_bfds;
4680 inputobj;
4681 inputobj = inputobj->link_next)
4683 asection *s;
4685 if (inputobj->flags & DYNAMIC)
4686 continue;
4687 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
4688 if (s)
4690 if (s->flags & SEC_CODE)
4691 exec = PF_X;
4692 notesec = s;
4694 else
4695 exec = PF_X;
4697 if (notesec)
4699 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
4700 if (exec && info->relocatable
4701 && notesec->output_section != bfd_abs_section_ptr)
4702 notesec->output_section->flags |= SEC_CODE;
4706 /* Any syms created from now on start with -1 in
4707 got.refcount/offset and plt.refcount/offset. */
4708 elf_hash_table (info)->init_refcount = elf_hash_table (info)->init_offset;
4710 /* The backend may have to create some sections regardless of whether
4711 we're dynamic or not. */
4712 bed = get_elf_backend_data (output_bfd);
4713 if (bed->elf_backend_always_size_sections
4714 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
4715 return FALSE;
4717 dynobj = elf_hash_table (info)->dynobj;
4719 /* If there were no dynamic objects in the link, there is nothing to
4720 do here. */
4721 if (dynobj == NULL)
4722 return TRUE;
4724 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
4725 return FALSE;
4727 if (elf_hash_table (info)->dynamic_sections_created)
4729 struct elf_info_failed eif;
4730 struct elf_link_hash_entry *h;
4731 asection *dynstr;
4732 struct bfd_elf_version_tree *t;
4733 struct bfd_elf_version_expr *d;
4734 bfd_boolean all_defined;
4736 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
4737 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
4739 if (soname != NULL)
4741 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
4742 soname, TRUE);
4743 if (soname_indx == (bfd_size_type) -1
4744 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
4745 return FALSE;
4748 if (info->symbolic)
4750 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
4751 return FALSE;
4752 info->flags |= DF_SYMBOLIC;
4755 if (rpath != NULL)
4757 bfd_size_type indx;
4759 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
4760 TRUE);
4761 if (indx == (bfd_size_type) -1
4762 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
4763 return FALSE;
4765 if (info->new_dtags)
4767 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
4768 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
4769 return FALSE;
4773 if (filter_shlib != NULL)
4775 bfd_size_type indx;
4777 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
4778 filter_shlib, TRUE);
4779 if (indx == (bfd_size_type) -1
4780 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
4781 return FALSE;
4784 if (auxiliary_filters != NULL)
4786 const char * const *p;
4788 for (p = auxiliary_filters; *p != NULL; p++)
4790 bfd_size_type indx;
4792 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
4793 *p, TRUE);
4794 if (indx == (bfd_size_type) -1
4795 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
4796 return FALSE;
4800 eif.info = info;
4801 eif.verdefs = verdefs;
4802 eif.failed = FALSE;
4804 /* If we are supposed to export all symbols into the dynamic symbol
4805 table (this is not the normal case), then do so. */
4806 if (info->export_dynamic)
4808 elf_link_hash_traverse (elf_hash_table (info),
4809 _bfd_elf_export_symbol,
4810 &eif);
4811 if (eif.failed)
4812 return FALSE;
4815 /* Make all global versions with definition. */
4816 for (t = verdefs; t != NULL; t = t->next)
4817 for (d = t->globals.list; d != NULL; d = d->next)
4818 if (!d->symver && d->symbol)
4820 const char *verstr, *name;
4821 size_t namelen, verlen, newlen;
4822 char *newname, *p;
4823 struct elf_link_hash_entry *newh;
4825 name = d->symbol;
4826 namelen = strlen (name);
4827 verstr = t->name;
4828 verlen = strlen (verstr);
4829 newlen = namelen + verlen + 3;
4831 newname = bfd_malloc (newlen);
4832 if (newname == NULL)
4833 return FALSE;
4834 memcpy (newname, name, namelen);
4836 /* Check the hidden versioned definition. */
4837 p = newname + namelen;
4838 *p++ = ELF_VER_CHR;
4839 memcpy (p, verstr, verlen + 1);
4840 newh = elf_link_hash_lookup (elf_hash_table (info),
4841 newname, FALSE, FALSE,
4842 FALSE);
4843 if (newh == NULL
4844 || (newh->root.type != bfd_link_hash_defined
4845 && newh->root.type != bfd_link_hash_defweak))
4847 /* Check the default versioned definition. */
4848 *p++ = ELF_VER_CHR;
4849 memcpy (p, verstr, verlen + 1);
4850 newh = elf_link_hash_lookup (elf_hash_table (info),
4851 newname, FALSE, FALSE,
4852 FALSE);
4854 free (newname);
4856 /* Mark this version if there is a definition and it is
4857 not defined in a shared object. */
4858 if (newh != NULL
4859 && !newh->def_dynamic
4860 && (newh->root.type == bfd_link_hash_defined
4861 || newh->root.type == bfd_link_hash_defweak))
4862 d->symver = 1;
4865 /* Attach all the symbols to their version information. */
4866 asvinfo.output_bfd = output_bfd;
4867 asvinfo.info = info;
4868 asvinfo.verdefs = verdefs;
4869 asvinfo.failed = FALSE;
4871 elf_link_hash_traverse (elf_hash_table (info),
4872 _bfd_elf_link_assign_sym_version,
4873 &asvinfo);
4874 if (asvinfo.failed)
4875 return FALSE;
4877 if (!info->allow_undefined_version)
4879 /* Check if all global versions have a definition. */
4880 all_defined = TRUE;
4881 for (t = verdefs; t != NULL; t = t->next)
4882 for (d = t->globals.list; d != NULL; d = d->next)
4883 if (!d->symver && !d->script)
4885 (*_bfd_error_handler)
4886 (_("%s: undefined version: %s"),
4887 d->pattern, t->name);
4888 all_defined = FALSE;
4891 if (!all_defined)
4893 bfd_set_error (bfd_error_bad_value);
4894 return FALSE;
4898 /* Find all symbols which were defined in a dynamic object and make
4899 the backend pick a reasonable value for them. */
4900 elf_link_hash_traverse (elf_hash_table (info),
4901 _bfd_elf_adjust_dynamic_symbol,
4902 &eif);
4903 if (eif.failed)
4904 return FALSE;
4906 /* Add some entries to the .dynamic section. We fill in some of the
4907 values later, in bfd_elf_final_link, but we must add the entries
4908 now so that we know the final size of the .dynamic section. */
4910 /* If there are initialization and/or finalization functions to
4911 call then add the corresponding DT_INIT/DT_FINI entries. */
4912 h = (info->init_function
4913 ? elf_link_hash_lookup (elf_hash_table (info),
4914 info->init_function, FALSE,
4915 FALSE, FALSE)
4916 : NULL);
4917 if (h != NULL
4918 && (h->ref_regular
4919 || h->def_regular))
4921 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
4922 return FALSE;
4924 h = (info->fini_function
4925 ? elf_link_hash_lookup (elf_hash_table (info),
4926 info->fini_function, FALSE,
4927 FALSE, FALSE)
4928 : NULL);
4929 if (h != NULL
4930 && (h->ref_regular
4931 || h->def_regular))
4933 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
4934 return FALSE;
4937 if (bfd_get_section_by_name (output_bfd, ".preinit_array") != NULL)
4939 /* DT_PREINIT_ARRAY is not allowed in shared library. */
4940 if (! info->executable)
4942 bfd *sub;
4943 asection *o;
4945 for (sub = info->input_bfds; sub != NULL;
4946 sub = sub->link_next)
4947 for (o = sub->sections; o != NULL; o = o->next)
4948 if (elf_section_data (o)->this_hdr.sh_type
4949 == SHT_PREINIT_ARRAY)
4951 (*_bfd_error_handler)
4952 (_("%B: .preinit_array section is not allowed in DSO"),
4953 sub);
4954 break;
4957 bfd_set_error (bfd_error_nonrepresentable_section);
4958 return FALSE;
4961 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
4962 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
4963 return FALSE;
4965 if (bfd_get_section_by_name (output_bfd, ".init_array") != NULL)
4967 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
4968 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
4969 return FALSE;
4971 if (bfd_get_section_by_name (output_bfd, ".fini_array") != NULL)
4973 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
4974 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
4975 return FALSE;
4978 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
4979 /* If .dynstr is excluded from the link, we don't want any of
4980 these tags. Strictly, we should be checking each section
4981 individually; This quick check covers for the case where
4982 someone does a /DISCARD/ : { *(*) }. */
4983 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
4985 bfd_size_type strsize;
4987 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
4988 if (!_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)
4989 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
4990 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
4991 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
4992 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
4993 bed->s->sizeof_sym))
4994 return FALSE;
4998 /* The backend must work out the sizes of all the other dynamic
4999 sections. */
5000 if (bed->elf_backend_size_dynamic_sections
5001 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5002 return FALSE;
5004 if (elf_hash_table (info)->dynamic_sections_created)
5006 bfd_size_type dynsymcount;
5007 asection *s;
5008 size_t bucketcount = 0;
5009 size_t hash_entry_size;
5010 unsigned int dtagcount;
5012 /* Set up the version definition section. */
5013 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5014 BFD_ASSERT (s != NULL);
5016 /* We may have created additional version definitions if we are
5017 just linking a regular application. */
5018 verdefs = asvinfo.verdefs;
5020 /* Skip anonymous version tag. */
5021 if (verdefs != NULL && verdefs->vernum == 0)
5022 verdefs = verdefs->next;
5024 if (verdefs == NULL)
5025 _bfd_strip_section_from_output (info, s);
5026 else
5028 unsigned int cdefs;
5029 bfd_size_type size;
5030 struct bfd_elf_version_tree *t;
5031 bfd_byte *p;
5032 Elf_Internal_Verdef def;
5033 Elf_Internal_Verdaux defaux;
5035 cdefs = 0;
5036 size = 0;
5038 /* Make space for the base version. */
5039 size += sizeof (Elf_External_Verdef);
5040 size += sizeof (Elf_External_Verdaux);
5041 ++cdefs;
5043 for (t = verdefs; t != NULL; t = t->next)
5045 struct bfd_elf_version_deps *n;
5047 size += sizeof (Elf_External_Verdef);
5048 size += sizeof (Elf_External_Verdaux);
5049 ++cdefs;
5051 for (n = t->deps; n != NULL; n = n->next)
5052 size += sizeof (Elf_External_Verdaux);
5055 s->size = size;
5056 s->contents = bfd_alloc (output_bfd, s->size);
5057 if (s->contents == NULL && s->size != 0)
5058 return FALSE;
5060 /* Fill in the version definition section. */
5062 p = s->contents;
5064 def.vd_version = VER_DEF_CURRENT;
5065 def.vd_flags = VER_FLG_BASE;
5066 def.vd_ndx = 1;
5067 def.vd_cnt = 1;
5068 def.vd_aux = sizeof (Elf_External_Verdef);
5069 def.vd_next = (sizeof (Elf_External_Verdef)
5070 + sizeof (Elf_External_Verdaux));
5072 if (soname_indx != (bfd_size_type) -1)
5074 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5075 soname_indx);
5076 def.vd_hash = bfd_elf_hash (soname);
5077 defaux.vda_name = soname_indx;
5079 else
5081 const char *name;
5082 bfd_size_type indx;
5084 name = basename (output_bfd->filename);
5085 def.vd_hash = bfd_elf_hash (name);
5086 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5087 name, FALSE);
5088 if (indx == (bfd_size_type) -1)
5089 return FALSE;
5090 defaux.vda_name = indx;
5092 defaux.vda_next = 0;
5094 _bfd_elf_swap_verdef_out (output_bfd, &def,
5095 (Elf_External_Verdef *) p);
5096 p += sizeof (Elf_External_Verdef);
5097 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5098 (Elf_External_Verdaux *) p);
5099 p += sizeof (Elf_External_Verdaux);
5101 for (t = verdefs; t != NULL; t = t->next)
5103 unsigned int cdeps;
5104 struct bfd_elf_version_deps *n;
5105 struct elf_link_hash_entry *h;
5106 struct bfd_link_hash_entry *bh;
5108 cdeps = 0;
5109 for (n = t->deps; n != NULL; n = n->next)
5110 ++cdeps;
5112 /* Add a symbol representing this version. */
5113 bh = NULL;
5114 if (! (_bfd_generic_link_add_one_symbol
5115 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5116 0, NULL, FALSE,
5117 get_elf_backend_data (dynobj)->collect, &bh)))
5118 return FALSE;
5119 h = (struct elf_link_hash_entry *) bh;
5120 h->non_elf = 0;
5121 h->def_regular = 1;
5122 h->type = STT_OBJECT;
5123 h->verinfo.vertree = t;
5125 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5126 return FALSE;
5128 def.vd_version = VER_DEF_CURRENT;
5129 def.vd_flags = 0;
5130 if (t->globals.list == NULL
5131 && t->locals.list == NULL
5132 && ! t->used)
5133 def.vd_flags |= VER_FLG_WEAK;
5134 def.vd_ndx = t->vernum + 1;
5135 def.vd_cnt = cdeps + 1;
5136 def.vd_hash = bfd_elf_hash (t->name);
5137 def.vd_aux = sizeof (Elf_External_Verdef);
5138 def.vd_next = 0;
5139 if (t->next != NULL)
5140 def.vd_next = (sizeof (Elf_External_Verdef)
5141 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5143 _bfd_elf_swap_verdef_out (output_bfd, &def,
5144 (Elf_External_Verdef *) p);
5145 p += sizeof (Elf_External_Verdef);
5147 defaux.vda_name = h->dynstr_index;
5148 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5149 h->dynstr_index);
5150 defaux.vda_next = 0;
5151 if (t->deps != NULL)
5152 defaux.vda_next = sizeof (Elf_External_Verdaux);
5153 t->name_indx = defaux.vda_name;
5155 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5156 (Elf_External_Verdaux *) p);
5157 p += sizeof (Elf_External_Verdaux);
5159 for (n = t->deps; n != NULL; n = n->next)
5161 if (n->version_needed == NULL)
5163 /* This can happen if there was an error in the
5164 version script. */
5165 defaux.vda_name = 0;
5167 else
5169 defaux.vda_name = n->version_needed->name_indx;
5170 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5171 defaux.vda_name);
5173 if (n->next == NULL)
5174 defaux.vda_next = 0;
5175 else
5176 defaux.vda_next = sizeof (Elf_External_Verdaux);
5178 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5179 (Elf_External_Verdaux *) p);
5180 p += sizeof (Elf_External_Verdaux);
5184 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
5185 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
5186 return FALSE;
5188 elf_tdata (output_bfd)->cverdefs = cdefs;
5191 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
5193 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
5194 return FALSE;
5196 else if (info->flags & DF_BIND_NOW)
5198 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
5199 return FALSE;
5202 if (info->flags_1)
5204 if (info->executable)
5205 info->flags_1 &= ~ (DF_1_INITFIRST
5206 | DF_1_NODELETE
5207 | DF_1_NOOPEN);
5208 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
5209 return FALSE;
5212 /* Work out the size of the version reference section. */
5214 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
5215 BFD_ASSERT (s != NULL);
5217 struct elf_find_verdep_info sinfo;
5219 sinfo.output_bfd = output_bfd;
5220 sinfo.info = info;
5221 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
5222 if (sinfo.vers == 0)
5223 sinfo.vers = 1;
5224 sinfo.failed = FALSE;
5226 elf_link_hash_traverse (elf_hash_table (info),
5227 _bfd_elf_link_find_version_dependencies,
5228 &sinfo);
5230 if (elf_tdata (output_bfd)->verref == NULL)
5231 _bfd_strip_section_from_output (info, s);
5232 else
5234 Elf_Internal_Verneed *t;
5235 unsigned int size;
5236 unsigned int crefs;
5237 bfd_byte *p;
5239 /* Build the version definition section. */
5240 size = 0;
5241 crefs = 0;
5242 for (t = elf_tdata (output_bfd)->verref;
5243 t != NULL;
5244 t = t->vn_nextref)
5246 Elf_Internal_Vernaux *a;
5248 size += sizeof (Elf_External_Verneed);
5249 ++crefs;
5250 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5251 size += sizeof (Elf_External_Vernaux);
5254 s->size = size;
5255 s->contents = bfd_alloc (output_bfd, s->size);
5256 if (s->contents == NULL)
5257 return FALSE;
5259 p = s->contents;
5260 for (t = elf_tdata (output_bfd)->verref;
5261 t != NULL;
5262 t = t->vn_nextref)
5264 unsigned int caux;
5265 Elf_Internal_Vernaux *a;
5266 bfd_size_type indx;
5268 caux = 0;
5269 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5270 ++caux;
5272 t->vn_version = VER_NEED_CURRENT;
5273 t->vn_cnt = caux;
5274 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5275 elf_dt_name (t->vn_bfd) != NULL
5276 ? elf_dt_name (t->vn_bfd)
5277 : basename (t->vn_bfd->filename),
5278 FALSE);
5279 if (indx == (bfd_size_type) -1)
5280 return FALSE;
5281 t->vn_file = indx;
5282 t->vn_aux = sizeof (Elf_External_Verneed);
5283 if (t->vn_nextref == NULL)
5284 t->vn_next = 0;
5285 else
5286 t->vn_next = (sizeof (Elf_External_Verneed)
5287 + caux * sizeof (Elf_External_Vernaux));
5289 _bfd_elf_swap_verneed_out (output_bfd, t,
5290 (Elf_External_Verneed *) p);
5291 p += sizeof (Elf_External_Verneed);
5293 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5295 a->vna_hash = bfd_elf_hash (a->vna_nodename);
5296 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5297 a->vna_nodename, FALSE);
5298 if (indx == (bfd_size_type) -1)
5299 return FALSE;
5300 a->vna_name = indx;
5301 if (a->vna_nextptr == NULL)
5302 a->vna_next = 0;
5303 else
5304 a->vna_next = sizeof (Elf_External_Vernaux);
5306 _bfd_elf_swap_vernaux_out (output_bfd, a,
5307 (Elf_External_Vernaux *) p);
5308 p += sizeof (Elf_External_Vernaux);
5312 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
5313 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
5314 return FALSE;
5316 elf_tdata (output_bfd)->cverrefs = crefs;
5320 /* Assign dynsym indicies. In a shared library we generate a
5321 section symbol for each output section, which come first.
5322 Next come all of the back-end allocated local dynamic syms,
5323 followed by the rest of the global symbols. */
5325 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
5327 /* Work out the size of the symbol version section. */
5328 s = bfd_get_section_by_name (dynobj, ".gnu.version");
5329 BFD_ASSERT (s != NULL);
5330 if (dynsymcount == 0
5331 || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL))
5333 _bfd_strip_section_from_output (info, s);
5334 /* The DYNSYMCOUNT might have changed if we were going to
5335 output a dynamic symbol table entry for S. */
5336 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
5338 else
5340 s->size = dynsymcount * sizeof (Elf_External_Versym);
5341 s->contents = bfd_zalloc (output_bfd, s->size);
5342 if (s->contents == NULL)
5343 return FALSE;
5345 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
5346 return FALSE;
5349 /* Set the size of the .dynsym and .hash sections. We counted
5350 the number of dynamic symbols in elf_link_add_object_symbols.
5351 We will build the contents of .dynsym and .hash when we build
5352 the final symbol table, because until then we do not know the
5353 correct value to give the symbols. We built the .dynstr
5354 section as we went along in elf_link_add_object_symbols. */
5355 s = bfd_get_section_by_name (dynobj, ".dynsym");
5356 BFD_ASSERT (s != NULL);
5357 s->size = dynsymcount * bed->s->sizeof_sym;
5358 s->contents = bfd_alloc (output_bfd, s->size);
5359 if (s->contents == NULL && s->size != 0)
5360 return FALSE;
5362 if (dynsymcount != 0)
5364 Elf_Internal_Sym isym;
5366 /* The first entry in .dynsym is a dummy symbol. */
5367 isym.st_value = 0;
5368 isym.st_size = 0;
5369 isym.st_name = 0;
5370 isym.st_info = 0;
5371 isym.st_other = 0;
5372 isym.st_shndx = 0;
5373 bed->s->swap_symbol_out (output_bfd, &isym, s->contents, 0);
5376 /* Compute the size of the hashing table. As a side effect this
5377 computes the hash values for all the names we export. */
5378 bucketcount = compute_bucket_count (info);
5380 s = bfd_get_section_by_name (dynobj, ".hash");
5381 BFD_ASSERT (s != NULL);
5382 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
5383 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
5384 s->contents = bfd_zalloc (output_bfd, s->size);
5385 if (s->contents == NULL)
5386 return FALSE;
5388 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
5389 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
5390 s->contents + hash_entry_size);
5392 elf_hash_table (info)->bucketcount = bucketcount;
5394 s = bfd_get_section_by_name (dynobj, ".dynstr");
5395 BFD_ASSERT (s != NULL);
5397 elf_finalize_dynstr (output_bfd, info);
5399 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5401 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
5402 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
5403 return FALSE;
5406 return TRUE;
5409 /* Final phase of ELF linker. */
5411 /* A structure we use to avoid passing large numbers of arguments. */
5413 struct elf_final_link_info
5415 /* General link information. */
5416 struct bfd_link_info *info;
5417 /* Output BFD. */
5418 bfd *output_bfd;
5419 /* Symbol string table. */
5420 struct bfd_strtab_hash *symstrtab;
5421 /* .dynsym section. */
5422 asection *dynsym_sec;
5423 /* .hash section. */
5424 asection *hash_sec;
5425 /* symbol version section (.gnu.version). */
5426 asection *symver_sec;
5427 /* Buffer large enough to hold contents of any section. */
5428 bfd_byte *contents;
5429 /* Buffer large enough to hold external relocs of any section. */
5430 void *external_relocs;
5431 /* Buffer large enough to hold internal relocs of any section. */
5432 Elf_Internal_Rela *internal_relocs;
5433 /* Buffer large enough to hold external local symbols of any input
5434 BFD. */
5435 bfd_byte *external_syms;
5436 /* And a buffer for symbol section indices. */
5437 Elf_External_Sym_Shndx *locsym_shndx;
5438 /* Buffer large enough to hold internal local symbols of any input
5439 BFD. */
5440 Elf_Internal_Sym *internal_syms;
5441 /* Array large enough to hold a symbol index for each local symbol
5442 of any input BFD. */
5443 long *indices;
5444 /* Array large enough to hold a section pointer for each local
5445 symbol of any input BFD. */
5446 asection **sections;
5447 /* Buffer to hold swapped out symbols. */
5448 bfd_byte *symbuf;
5449 /* And one for symbol section indices. */
5450 Elf_External_Sym_Shndx *symshndxbuf;
5451 /* Number of swapped out symbols in buffer. */
5452 size_t symbuf_count;
5453 /* Number of symbols which fit in symbuf. */
5454 size_t symbuf_size;
5455 /* And same for symshndxbuf. */
5456 size_t shndxbuf_size;
5459 /* This struct is used to pass information to elf_link_output_extsym. */
5461 struct elf_outext_info
5463 bfd_boolean failed;
5464 bfd_boolean localsyms;
5465 struct elf_final_link_info *finfo;
5468 /* When performing a relocatable link, the input relocations are
5469 preserved. But, if they reference global symbols, the indices
5470 referenced must be updated. Update all the relocations in
5471 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5473 static void
5474 elf_link_adjust_relocs (bfd *abfd,
5475 Elf_Internal_Shdr *rel_hdr,
5476 unsigned int count,
5477 struct elf_link_hash_entry **rel_hash)
5479 unsigned int i;
5480 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5481 bfd_byte *erela;
5482 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5483 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5484 bfd_vma r_type_mask;
5485 int r_sym_shift;
5487 if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
5489 swap_in = bed->s->swap_reloc_in;
5490 swap_out = bed->s->swap_reloc_out;
5492 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
5494 swap_in = bed->s->swap_reloca_in;
5495 swap_out = bed->s->swap_reloca_out;
5497 else
5498 abort ();
5500 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
5501 abort ();
5503 if (bed->s->arch_size == 32)
5505 r_type_mask = 0xff;
5506 r_sym_shift = 8;
5508 else
5510 r_type_mask = 0xffffffff;
5511 r_sym_shift = 32;
5514 erela = rel_hdr->contents;
5515 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
5517 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
5518 unsigned int j;
5520 if (*rel_hash == NULL)
5521 continue;
5523 BFD_ASSERT ((*rel_hash)->indx >= 0);
5525 (*swap_in) (abfd, erela, irela);
5526 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
5527 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
5528 | (irela[j].r_info & r_type_mask));
5529 (*swap_out) (abfd, irela, erela);
5533 struct elf_link_sort_rela
5535 union {
5536 bfd_vma offset;
5537 bfd_vma sym_mask;
5538 } u;
5539 enum elf_reloc_type_class type;
5540 /* We use this as an array of size int_rels_per_ext_rel. */
5541 Elf_Internal_Rela rela[1];
5544 static int
5545 elf_link_sort_cmp1 (const void *A, const void *B)
5547 const struct elf_link_sort_rela *a = A;
5548 const struct elf_link_sort_rela *b = B;
5549 int relativea, relativeb;
5551 relativea = a->type == reloc_class_relative;
5552 relativeb = b->type == reloc_class_relative;
5554 if (relativea < relativeb)
5555 return 1;
5556 if (relativea > relativeb)
5557 return -1;
5558 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
5559 return -1;
5560 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
5561 return 1;
5562 if (a->rela->r_offset < b->rela->r_offset)
5563 return -1;
5564 if (a->rela->r_offset > b->rela->r_offset)
5565 return 1;
5566 return 0;
5569 static int
5570 elf_link_sort_cmp2 (const void *A, const void *B)
5572 const struct elf_link_sort_rela *a = A;
5573 const struct elf_link_sort_rela *b = B;
5574 int copya, copyb;
5576 if (a->u.offset < b->u.offset)
5577 return -1;
5578 if (a->u.offset > b->u.offset)
5579 return 1;
5580 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
5581 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
5582 if (copya < copyb)
5583 return -1;
5584 if (copya > copyb)
5585 return 1;
5586 if (a->rela->r_offset < b->rela->r_offset)
5587 return -1;
5588 if (a->rela->r_offset > b->rela->r_offset)
5589 return 1;
5590 return 0;
5593 static size_t
5594 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
5596 asection *reldyn;
5597 bfd_size_type count, size;
5598 size_t i, ret, sort_elt, ext_size;
5599 bfd_byte *sort, *s_non_relative, *p;
5600 struct elf_link_sort_rela *sq;
5601 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5602 int i2e = bed->s->int_rels_per_ext_rel;
5603 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5604 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5605 struct bfd_link_order *lo;
5606 bfd_vma r_sym_mask;
5608 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
5609 if (reldyn == NULL || reldyn->size == 0)
5611 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
5612 if (reldyn == NULL || reldyn->size == 0)
5613 return 0;
5614 ext_size = bed->s->sizeof_rel;
5615 swap_in = bed->s->swap_reloc_in;
5616 swap_out = bed->s->swap_reloc_out;
5618 else
5620 ext_size = bed->s->sizeof_rela;
5621 swap_in = bed->s->swap_reloca_in;
5622 swap_out = bed->s->swap_reloca_out;
5624 count = reldyn->size / ext_size;
5626 size = 0;
5627 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
5628 if (lo->type == bfd_indirect_link_order)
5630 asection *o = lo->u.indirect.section;
5631 size += o->size;
5634 if (size != reldyn->size)
5635 return 0;
5637 sort_elt = (sizeof (struct elf_link_sort_rela)
5638 + (i2e - 1) * sizeof (Elf_Internal_Rela));
5639 sort = bfd_zmalloc (sort_elt * count);
5640 if (sort == NULL)
5642 (*info->callbacks->warning)
5643 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
5644 return 0;
5647 if (bed->s->arch_size == 32)
5648 r_sym_mask = ~(bfd_vma) 0xff;
5649 else
5650 r_sym_mask = ~(bfd_vma) 0xffffffff;
5652 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
5653 if (lo->type == bfd_indirect_link_order)
5655 bfd_byte *erel, *erelend;
5656 asection *o = lo->u.indirect.section;
5658 erel = o->contents;
5659 erelend = o->contents + o->size;
5660 p = sort + o->output_offset / ext_size * sort_elt;
5661 while (erel < erelend)
5663 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
5664 (*swap_in) (abfd, erel, s->rela);
5665 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
5666 s->u.sym_mask = r_sym_mask;
5667 p += sort_elt;
5668 erel += ext_size;
5672 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
5674 for (i = 0, p = sort; i < count; i++, p += sort_elt)
5676 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
5677 if (s->type != reloc_class_relative)
5678 break;
5680 ret = i;
5681 s_non_relative = p;
5683 sq = (struct elf_link_sort_rela *) s_non_relative;
5684 for (; i < count; i++, p += sort_elt)
5686 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
5687 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
5688 sq = sp;
5689 sp->u.offset = sq->rela->r_offset;
5692 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
5694 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
5695 if (lo->type == bfd_indirect_link_order)
5697 bfd_byte *erel, *erelend;
5698 asection *o = lo->u.indirect.section;
5700 erel = o->contents;
5701 erelend = o->contents + o->size;
5702 p = sort + o->output_offset / ext_size * sort_elt;
5703 while (erel < erelend)
5705 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
5706 (*swap_out) (abfd, s->rela, erel);
5707 p += sort_elt;
5708 erel += ext_size;
5712 free (sort);
5713 *psec = reldyn;
5714 return ret;
5717 /* Flush the output symbols to the file. */
5719 static bfd_boolean
5720 elf_link_flush_output_syms (struct elf_final_link_info *finfo,
5721 const struct elf_backend_data *bed)
5723 if (finfo->symbuf_count > 0)
5725 Elf_Internal_Shdr *hdr;
5726 file_ptr pos;
5727 bfd_size_type amt;
5729 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
5730 pos = hdr->sh_offset + hdr->sh_size;
5731 amt = finfo->symbuf_count * bed->s->sizeof_sym;
5732 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
5733 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
5734 return FALSE;
5736 hdr->sh_size += amt;
5737 finfo->symbuf_count = 0;
5740 return TRUE;
5743 /* Add a symbol to the output symbol table. */
5745 static bfd_boolean
5746 elf_link_output_sym (struct elf_final_link_info *finfo,
5747 const char *name,
5748 Elf_Internal_Sym *elfsym,
5749 asection *input_sec,
5750 struct elf_link_hash_entry *h)
5752 bfd_byte *dest;
5753 Elf_External_Sym_Shndx *destshndx;
5754 bfd_boolean (*output_symbol_hook)
5755 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
5756 struct elf_link_hash_entry *);
5757 const struct elf_backend_data *bed;
5759 bed = get_elf_backend_data (finfo->output_bfd);
5760 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
5761 if (output_symbol_hook != NULL)
5763 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
5764 return FALSE;
5767 if (name == NULL || *name == '\0')
5768 elfsym->st_name = 0;
5769 else if (input_sec->flags & SEC_EXCLUDE)
5770 elfsym->st_name = 0;
5771 else
5773 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
5774 name, TRUE, FALSE);
5775 if (elfsym->st_name == (unsigned long) -1)
5776 return FALSE;
5779 if (finfo->symbuf_count >= finfo->symbuf_size)
5781 if (! elf_link_flush_output_syms (finfo, bed))
5782 return FALSE;
5785 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
5786 destshndx = finfo->symshndxbuf;
5787 if (destshndx != NULL)
5789 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
5791 bfd_size_type amt;
5793 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
5794 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
5795 if (destshndx == NULL)
5796 return FALSE;
5797 memset ((char *) destshndx + amt, 0, amt);
5798 finfo->shndxbuf_size *= 2;
5800 destshndx += bfd_get_symcount (finfo->output_bfd);
5803 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
5804 finfo->symbuf_count += 1;
5805 bfd_get_symcount (finfo->output_bfd) += 1;
5807 return TRUE;
5810 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
5811 allowing an unsatisfied unversioned symbol in the DSO to match a
5812 versioned symbol that would normally require an explicit version.
5813 We also handle the case that a DSO references a hidden symbol
5814 which may be satisfied by a versioned symbol in another DSO. */
5816 static bfd_boolean
5817 elf_link_check_versioned_symbol (struct bfd_link_info *info,
5818 const struct elf_backend_data *bed,
5819 struct elf_link_hash_entry *h)
5821 bfd *abfd;
5822 struct elf_link_loaded_list *loaded;
5824 if (!is_elf_hash_table (info->hash))
5825 return FALSE;
5827 switch (h->root.type)
5829 default:
5830 abfd = NULL;
5831 break;
5833 case bfd_link_hash_undefined:
5834 case bfd_link_hash_undefweak:
5835 abfd = h->root.u.undef.abfd;
5836 if ((abfd->flags & DYNAMIC) == 0
5837 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
5838 return FALSE;
5839 break;
5841 case bfd_link_hash_defined:
5842 case bfd_link_hash_defweak:
5843 abfd = h->root.u.def.section->owner;
5844 break;
5846 case bfd_link_hash_common:
5847 abfd = h->root.u.c.p->section->owner;
5848 break;
5850 BFD_ASSERT (abfd != NULL);
5852 for (loaded = elf_hash_table (info)->loaded;
5853 loaded != NULL;
5854 loaded = loaded->next)
5856 bfd *input;
5857 Elf_Internal_Shdr *hdr;
5858 bfd_size_type symcount;
5859 bfd_size_type extsymcount;
5860 bfd_size_type extsymoff;
5861 Elf_Internal_Shdr *versymhdr;
5862 Elf_Internal_Sym *isym;
5863 Elf_Internal_Sym *isymend;
5864 Elf_Internal_Sym *isymbuf;
5865 Elf_External_Versym *ever;
5866 Elf_External_Versym *extversym;
5868 input = loaded->abfd;
5870 /* We check each DSO for a possible hidden versioned definition. */
5871 if (input == abfd
5872 || (input->flags & DYNAMIC) == 0
5873 || elf_dynversym (input) == 0)
5874 continue;
5876 hdr = &elf_tdata (input)->dynsymtab_hdr;
5878 symcount = hdr->sh_size / bed->s->sizeof_sym;
5879 if (elf_bad_symtab (input))
5881 extsymcount = symcount;
5882 extsymoff = 0;
5884 else
5886 extsymcount = symcount - hdr->sh_info;
5887 extsymoff = hdr->sh_info;
5890 if (extsymcount == 0)
5891 continue;
5893 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
5894 NULL, NULL, NULL);
5895 if (isymbuf == NULL)
5896 return FALSE;
5898 /* Read in any version definitions. */
5899 versymhdr = &elf_tdata (input)->dynversym_hdr;
5900 extversym = bfd_malloc (versymhdr->sh_size);
5901 if (extversym == NULL)
5902 goto error_ret;
5904 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
5905 || (bfd_bread (extversym, versymhdr->sh_size, input)
5906 != versymhdr->sh_size))
5908 free (extversym);
5909 error_ret:
5910 free (isymbuf);
5911 return FALSE;
5914 ever = extversym + extsymoff;
5915 isymend = isymbuf + extsymcount;
5916 for (isym = isymbuf; isym < isymend; isym++, ever++)
5918 const char *name;
5919 Elf_Internal_Versym iver;
5920 unsigned short version_index;
5922 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
5923 || isym->st_shndx == SHN_UNDEF)
5924 continue;
5926 name = bfd_elf_string_from_elf_section (input,
5927 hdr->sh_link,
5928 isym->st_name);
5929 if (strcmp (name, h->root.root.string) != 0)
5930 continue;
5932 _bfd_elf_swap_versym_in (input, ever, &iver);
5934 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
5936 /* If we have a non-hidden versioned sym, then it should
5937 have provided a definition for the undefined sym. */
5938 abort ();
5941 version_index = iver.vs_vers & VERSYM_VERSION;
5942 if (version_index == 1 || version_index == 2)
5944 /* This is the base or first version. We can use it. */
5945 free (extversym);
5946 free (isymbuf);
5947 return TRUE;
5951 free (extversym);
5952 free (isymbuf);
5955 return FALSE;
5958 /* Add an external symbol to the symbol table. This is called from
5959 the hash table traversal routine. When generating a shared object,
5960 we go through the symbol table twice. The first time we output
5961 anything that might have been forced to local scope in a version
5962 script. The second time we output the symbols that are still
5963 global symbols. */
5965 static bfd_boolean
5966 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
5968 struct elf_outext_info *eoinfo = data;
5969 struct elf_final_link_info *finfo = eoinfo->finfo;
5970 bfd_boolean strip;
5971 Elf_Internal_Sym sym;
5972 asection *input_sec;
5973 const struct elf_backend_data *bed;
5975 if (h->root.type == bfd_link_hash_warning)
5977 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5978 if (h->root.type == bfd_link_hash_new)
5979 return TRUE;
5982 /* Decide whether to output this symbol in this pass. */
5983 if (eoinfo->localsyms)
5985 if (!h->forced_local)
5986 return TRUE;
5988 else
5990 if (h->forced_local)
5991 return TRUE;
5994 bed = get_elf_backend_data (finfo->output_bfd);
5996 /* If we have an undefined symbol reference here then it must have
5997 come from a shared library that is being linked in. (Undefined
5998 references in regular files have already been handled). If we
5999 are reporting errors for this situation then do so now. */
6000 if (h->root.type == bfd_link_hash_undefined
6001 && h->ref_dynamic
6002 && !h->ref_regular
6003 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
6004 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
6006 if (! ((*finfo->info->callbacks->undefined_symbol)
6007 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
6008 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
6010 eoinfo->failed = TRUE;
6011 return FALSE;
6015 /* We should also warn if a forced local symbol is referenced from
6016 shared libraries. */
6017 if (! finfo->info->relocatable
6018 && (! finfo->info->shared)
6019 && h->forced_local
6020 && h->ref_dynamic
6021 && !h->dynamic_def
6022 && !h->dynamic_weak
6023 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
6025 (*_bfd_error_handler)
6026 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6027 finfo->output_bfd, h->root.u.def.section->owner,
6028 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
6029 ? "internal"
6030 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
6031 ? "hidden" : "local",
6032 h->root.root.string);
6033 eoinfo->failed = TRUE;
6034 return FALSE;
6037 /* We don't want to output symbols that have never been mentioned by
6038 a regular file, or that we have been told to strip. However, if
6039 h->indx is set to -2, the symbol is used by a reloc and we must
6040 output it. */
6041 if (h->indx == -2)
6042 strip = FALSE;
6043 else if ((h->def_dynamic
6044 || h->ref_dynamic)
6045 && !h->def_regular
6046 && !h->ref_regular)
6047 strip = TRUE;
6048 else if (finfo->info->strip == strip_all)
6049 strip = TRUE;
6050 else if (finfo->info->strip == strip_some
6051 && bfd_hash_lookup (finfo->info->keep_hash,
6052 h->root.root.string, FALSE, FALSE) == NULL)
6053 strip = TRUE;
6054 else if (finfo->info->strip_discarded
6055 && (h->root.type == bfd_link_hash_defined
6056 || h->root.type == bfd_link_hash_defweak)
6057 && elf_discarded_section (h->root.u.def.section))
6058 strip = TRUE;
6059 else
6060 strip = FALSE;
6062 /* If we're stripping it, and it's not a dynamic symbol, there's
6063 nothing else to do unless it is a forced local symbol. */
6064 if (strip
6065 && h->dynindx == -1
6066 && !h->forced_local)
6067 return TRUE;
6069 sym.st_value = 0;
6070 sym.st_size = h->size;
6071 sym.st_other = h->other;
6072 if (h->forced_local)
6073 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
6074 else if (h->root.type == bfd_link_hash_undefweak
6075 || h->root.type == bfd_link_hash_defweak)
6076 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
6077 else
6078 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
6080 switch (h->root.type)
6082 default:
6083 case bfd_link_hash_new:
6084 case bfd_link_hash_warning:
6085 abort ();
6086 return FALSE;
6088 case bfd_link_hash_undefined:
6089 case bfd_link_hash_undefweak:
6090 input_sec = bfd_und_section_ptr;
6091 sym.st_shndx = SHN_UNDEF;
6092 break;
6094 case bfd_link_hash_defined:
6095 case bfd_link_hash_defweak:
6097 input_sec = h->root.u.def.section;
6098 if (input_sec->output_section != NULL)
6100 sym.st_shndx =
6101 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
6102 input_sec->output_section);
6103 if (sym.st_shndx == SHN_BAD)
6105 (*_bfd_error_handler)
6106 (_("%B: could not find output section %A for input section %A"),
6107 finfo->output_bfd, input_sec->output_section, input_sec);
6108 eoinfo->failed = TRUE;
6109 return FALSE;
6112 /* ELF symbols in relocatable files are section relative,
6113 but in nonrelocatable files they are virtual
6114 addresses. */
6115 sym.st_value = h->root.u.def.value + input_sec->output_offset;
6116 if (! finfo->info->relocatable)
6118 sym.st_value += input_sec->output_section->vma;
6119 if (h->type == STT_TLS)
6121 /* STT_TLS symbols are relative to PT_TLS segment
6122 base. */
6123 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6124 sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6128 else
6130 BFD_ASSERT (input_sec->owner == NULL
6131 || (input_sec->owner->flags & DYNAMIC) != 0);
6132 sym.st_shndx = SHN_UNDEF;
6133 input_sec = bfd_und_section_ptr;
6136 break;
6138 case bfd_link_hash_common:
6139 input_sec = h->root.u.c.p->section;
6140 sym.st_shndx = SHN_COMMON;
6141 sym.st_value = 1 << h->root.u.c.p->alignment_power;
6142 break;
6144 case bfd_link_hash_indirect:
6145 /* These symbols are created by symbol versioning. They point
6146 to the decorated version of the name. For example, if the
6147 symbol foo@@GNU_1.2 is the default, which should be used when
6148 foo is used with no version, then we add an indirect symbol
6149 foo which points to foo@@GNU_1.2. We ignore these symbols,
6150 since the indirected symbol is already in the hash table. */
6151 return TRUE;
6154 /* Give the processor backend a chance to tweak the symbol value,
6155 and also to finish up anything that needs to be done for this
6156 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6157 forced local syms when non-shared is due to a historical quirk. */
6158 if ((h->dynindx != -1
6159 || h->forced_local)
6160 && ((finfo->info->shared
6161 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6162 || h->root.type != bfd_link_hash_undefweak))
6163 || !h->forced_local)
6164 && elf_hash_table (finfo->info)->dynamic_sections_created)
6166 if (! ((*bed->elf_backend_finish_dynamic_symbol)
6167 (finfo->output_bfd, finfo->info, h, &sym)))
6169 eoinfo->failed = TRUE;
6170 return FALSE;
6174 /* If we are marking the symbol as undefined, and there are no
6175 non-weak references to this symbol from a regular object, then
6176 mark the symbol as weak undefined; if there are non-weak
6177 references, mark the symbol as strong. We can't do this earlier,
6178 because it might not be marked as undefined until the
6179 finish_dynamic_symbol routine gets through with it. */
6180 if (sym.st_shndx == SHN_UNDEF
6181 && h->ref_regular
6182 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
6183 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
6185 int bindtype;
6187 if (h->ref_regular_nonweak)
6188 bindtype = STB_GLOBAL;
6189 else
6190 bindtype = STB_WEAK;
6191 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
6194 /* If a non-weak symbol with non-default visibility is not defined
6195 locally, it is a fatal error. */
6196 if (! finfo->info->relocatable
6197 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
6198 && ELF_ST_BIND (sym.st_info) != STB_WEAK
6199 && h->root.type == bfd_link_hash_undefined
6200 && !h->def_regular)
6202 (*_bfd_error_handler)
6203 (_("%B: %s symbol `%s' isn't defined"),
6204 finfo->output_bfd,
6205 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
6206 ? "protected"
6207 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
6208 ? "internal" : "hidden",
6209 h->root.root.string);
6210 eoinfo->failed = TRUE;
6211 return FALSE;
6214 /* If this symbol should be put in the .dynsym section, then put it
6215 there now. We already know the symbol index. We also fill in
6216 the entry in the .hash section. */
6217 if (h->dynindx != -1
6218 && elf_hash_table (finfo->info)->dynamic_sections_created)
6220 size_t bucketcount;
6221 size_t bucket;
6222 size_t hash_entry_size;
6223 bfd_byte *bucketpos;
6224 bfd_vma chain;
6225 bfd_byte *esym;
6227 sym.st_name = h->dynstr_index;
6228 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
6229 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
6231 bucketcount = elf_hash_table (finfo->info)->bucketcount;
6232 bucket = h->u.elf_hash_value % bucketcount;
6233 hash_entry_size
6234 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
6235 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
6236 + (bucket + 2) * hash_entry_size);
6237 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
6238 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
6239 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
6240 ((bfd_byte *) finfo->hash_sec->contents
6241 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
6243 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
6245 Elf_Internal_Versym iversym;
6246 Elf_External_Versym *eversym;
6248 if (!h->def_regular)
6250 if (h->verinfo.verdef == NULL)
6251 iversym.vs_vers = 0;
6252 else
6253 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
6255 else
6257 if (h->verinfo.vertree == NULL)
6258 iversym.vs_vers = 1;
6259 else
6260 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
6263 if (h->hidden)
6264 iversym.vs_vers |= VERSYM_HIDDEN;
6266 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
6267 eversym += h->dynindx;
6268 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
6272 /* If we're stripping it, then it was just a dynamic symbol, and
6273 there's nothing else to do. */
6274 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
6275 return TRUE;
6277 h->indx = bfd_get_symcount (finfo->output_bfd);
6279 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
6281 eoinfo->failed = TRUE;
6282 return FALSE;
6285 return TRUE;
6288 /* Return TRUE if special handling is done for relocs in SEC against
6289 symbols defined in discarded sections. */
6291 static bfd_boolean
6292 elf_section_ignore_discarded_relocs (asection *sec)
6294 const struct elf_backend_data *bed;
6296 switch (sec->sec_info_type)
6298 case ELF_INFO_TYPE_STABS:
6299 case ELF_INFO_TYPE_EH_FRAME:
6300 return TRUE;
6301 default:
6302 break;
6305 bed = get_elf_backend_data (sec->owner);
6306 if (bed->elf_backend_ignore_discarded_relocs != NULL
6307 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
6308 return TRUE;
6310 return FALSE;
6313 /* Return TRUE if we should complain about a reloc in SEC against a
6314 symbol defined in a discarded section. */
6316 static bfd_boolean
6317 elf_section_complain_discarded (asection *sec)
6319 if (strncmp (".stab", sec->name, 5) == 0
6320 && (!sec->name[5] ||
6321 (sec->name[5] == '.' && ISDIGIT (sec->name[6]))))
6322 return FALSE;
6324 if (strcmp (".eh_frame", sec->name) == 0)
6325 return FALSE;
6327 if (strcmp (".gcc_except_table", sec->name) == 0)
6328 return FALSE;
6330 if (strcmp (".PARISC.unwind", sec->name) == 0)
6331 return FALSE;
6333 if (strcmp (".fixup", sec->name) == 0)
6334 return FALSE;
6336 return TRUE;
6339 /* Find a match between a section and a member of a section group. */
6341 static asection *
6342 match_group_member (asection *sec, asection *group)
6344 asection *first = elf_next_in_group (group);
6345 asection *s = first;
6347 while (s != NULL)
6349 if (bfd_elf_match_symbols_in_sections (s, sec))
6350 return s;
6352 if (s == first)
6353 break;
6356 return NULL;
6359 /* Link an input file into the linker output file. This function
6360 handles all the sections and relocations of the input file at once.
6361 This is so that we only have to read the local symbols once, and
6362 don't have to keep them in memory. */
6364 static bfd_boolean
6365 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
6367 bfd_boolean (*relocate_section)
6368 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
6369 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
6370 bfd *output_bfd;
6371 Elf_Internal_Shdr *symtab_hdr;
6372 size_t locsymcount;
6373 size_t extsymoff;
6374 Elf_Internal_Sym *isymbuf;
6375 Elf_Internal_Sym *isym;
6376 Elf_Internal_Sym *isymend;
6377 long *pindex;
6378 asection **ppsection;
6379 asection *o;
6380 const struct elf_backend_data *bed;
6381 bfd_boolean emit_relocs;
6382 struct elf_link_hash_entry **sym_hashes;
6384 output_bfd = finfo->output_bfd;
6385 bed = get_elf_backend_data (output_bfd);
6386 relocate_section = bed->elf_backend_relocate_section;
6388 /* If this is a dynamic object, we don't want to do anything here:
6389 we don't want the local symbols, and we don't want the section
6390 contents. */
6391 if ((input_bfd->flags & DYNAMIC) != 0)
6392 return TRUE;
6394 emit_relocs = (finfo->info->relocatable
6395 || finfo->info->emitrelocations
6396 || bed->elf_backend_emit_relocs);
6398 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6399 if (elf_bad_symtab (input_bfd))
6401 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
6402 extsymoff = 0;
6404 else
6406 locsymcount = symtab_hdr->sh_info;
6407 extsymoff = symtab_hdr->sh_info;
6410 /* Read the local symbols. */
6411 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
6412 if (isymbuf == NULL && locsymcount != 0)
6414 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
6415 finfo->internal_syms,
6416 finfo->external_syms,
6417 finfo->locsym_shndx);
6418 if (isymbuf == NULL)
6419 return FALSE;
6422 /* Find local symbol sections and adjust values of symbols in
6423 SEC_MERGE sections. Write out those local symbols we know are
6424 going into the output file. */
6425 isymend = isymbuf + locsymcount;
6426 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
6427 isym < isymend;
6428 isym++, pindex++, ppsection++)
6430 asection *isec;
6431 const char *name;
6432 Elf_Internal_Sym osym;
6434 *pindex = -1;
6436 if (elf_bad_symtab (input_bfd))
6438 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
6440 *ppsection = NULL;
6441 continue;
6445 if (isym->st_shndx == SHN_UNDEF)
6446 isec = bfd_und_section_ptr;
6447 else if (isym->st_shndx < SHN_LORESERVE
6448 || isym->st_shndx > SHN_HIRESERVE)
6450 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
6451 if (isec
6452 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
6453 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
6454 isym->st_value =
6455 _bfd_merged_section_offset (output_bfd, &isec,
6456 elf_section_data (isec)->sec_info,
6457 isym->st_value);
6459 else if (isym->st_shndx == SHN_ABS)
6460 isec = bfd_abs_section_ptr;
6461 else if (isym->st_shndx == SHN_COMMON)
6462 isec = bfd_com_section_ptr;
6463 else
6465 /* Who knows? */
6466 isec = NULL;
6469 *ppsection = isec;
6471 /* Don't output the first, undefined, symbol. */
6472 if (ppsection == finfo->sections)
6473 continue;
6475 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6477 /* We never output section symbols. Instead, we use the
6478 section symbol of the corresponding section in the output
6479 file. */
6480 continue;
6483 /* If we are stripping all symbols, we don't want to output this
6484 one. */
6485 if (finfo->info->strip == strip_all)
6486 continue;
6488 /* If we are discarding all local symbols, we don't want to
6489 output this one. If we are generating a relocatable output
6490 file, then some of the local symbols may be required by
6491 relocs; we output them below as we discover that they are
6492 needed. */
6493 if (finfo->info->discard == discard_all)
6494 continue;
6496 /* If this symbol is defined in a section which we are
6497 discarding, we don't need to keep it, but note that
6498 linker_mark is only reliable for sections that have contents.
6499 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6500 as well as linker_mark. */
6501 if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
6502 && isec != NULL
6503 && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
6504 || (! finfo->info->relocatable
6505 && (isec->flags & SEC_EXCLUDE) != 0)))
6506 continue;
6508 /* Get the name of the symbol. */
6509 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
6510 isym->st_name);
6511 if (name == NULL)
6512 return FALSE;
6514 /* See if we are discarding symbols with this name. */
6515 if ((finfo->info->strip == strip_some
6516 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
6517 == NULL))
6518 || (((finfo->info->discard == discard_sec_merge
6519 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
6520 || finfo->info->discard == discard_l)
6521 && bfd_is_local_label_name (input_bfd, name)))
6522 continue;
6524 /* If we get here, we are going to output this symbol. */
6526 osym = *isym;
6528 /* Adjust the section index for the output file. */
6529 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
6530 isec->output_section);
6531 if (osym.st_shndx == SHN_BAD)
6532 return FALSE;
6534 *pindex = bfd_get_symcount (output_bfd);
6536 /* ELF symbols in relocatable files are section relative, but
6537 in executable files they are virtual addresses. Note that
6538 this code assumes that all ELF sections have an associated
6539 BFD section with a reasonable value for output_offset; below
6540 we assume that they also have a reasonable value for
6541 output_section. Any special sections must be set up to meet
6542 these requirements. */
6543 osym.st_value += isec->output_offset;
6544 if (! finfo->info->relocatable)
6546 osym.st_value += isec->output_section->vma;
6547 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
6549 /* STT_TLS symbols are relative to PT_TLS segment base. */
6550 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6551 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6555 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
6556 return FALSE;
6559 /* Relocate the contents of each section. */
6560 sym_hashes = elf_sym_hashes (input_bfd);
6561 for (o = input_bfd->sections; o != NULL; o = o->next)
6563 bfd_byte *contents;
6565 if (! o->linker_mark)
6567 /* This section was omitted from the link. */
6568 continue;
6571 if ((o->flags & SEC_HAS_CONTENTS) == 0
6572 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
6573 continue;
6575 if ((o->flags & SEC_LINKER_CREATED) != 0)
6577 /* Section was created by _bfd_elf_link_create_dynamic_sections
6578 or somesuch. */
6579 continue;
6582 /* Get the contents of the section. They have been cached by a
6583 relaxation routine. Note that o is a section in an input
6584 file, so the contents field will not have been set by any of
6585 the routines which work on output files. */
6586 if (elf_section_data (o)->this_hdr.contents != NULL)
6587 contents = elf_section_data (o)->this_hdr.contents;
6588 else
6590 bfd_size_type amt = o->rawsize ? o->rawsize : o->size;
6592 contents = finfo->contents;
6593 if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt))
6594 return FALSE;
6597 if ((o->flags & SEC_RELOC) != 0)
6599 Elf_Internal_Rela *internal_relocs;
6600 bfd_vma r_type_mask;
6601 int r_sym_shift;
6603 /* Get the swapped relocs. */
6604 internal_relocs
6605 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
6606 finfo->internal_relocs, FALSE);
6607 if (internal_relocs == NULL
6608 && o->reloc_count > 0)
6609 return FALSE;
6611 if (bed->s->arch_size == 32)
6613 r_type_mask = 0xff;
6614 r_sym_shift = 8;
6616 else
6618 r_type_mask = 0xffffffff;
6619 r_sym_shift = 32;
6622 /* Run through the relocs looking for any against symbols
6623 from discarded sections and section symbols from
6624 removed link-once sections. Complain about relocs
6625 against discarded sections. Zero relocs against removed
6626 link-once sections. Preserve debug information as much
6627 as we can. */
6628 if (!elf_section_ignore_discarded_relocs (o))
6630 Elf_Internal_Rela *rel, *relend;
6631 bfd_boolean complain = elf_section_complain_discarded (o);
6633 rel = internal_relocs;
6634 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
6635 for ( ; rel < relend; rel++)
6637 unsigned long r_symndx = rel->r_info >> r_sym_shift;
6638 asection **ps, *sec;
6639 struct elf_link_hash_entry *h = NULL;
6640 const char *sym_name;
6642 if (r_symndx == STN_UNDEF)
6643 continue;
6645 if (r_symndx >= locsymcount
6646 || (elf_bad_symtab (input_bfd)
6647 && finfo->sections[r_symndx] == NULL))
6649 h = sym_hashes[r_symndx - extsymoff];
6650 while (h->root.type == bfd_link_hash_indirect
6651 || h->root.type == bfd_link_hash_warning)
6652 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6654 if (h->root.type != bfd_link_hash_defined
6655 && h->root.type != bfd_link_hash_defweak)
6656 continue;
6658 ps = &h->root.u.def.section;
6659 sym_name = h->root.root.string;
6661 else
6663 Elf_Internal_Sym *sym = isymbuf + r_symndx;
6664 ps = &finfo->sections[r_symndx];
6665 sym_name = bfd_elf_local_sym_name (input_bfd, sym);
6668 /* Complain if the definition comes from a
6669 discarded section. */
6670 if ((sec = *ps) != NULL && elf_discarded_section (sec))
6672 if ((o->flags & SEC_DEBUGGING) != 0)
6674 BFD_ASSERT (r_symndx != 0);
6676 /* Try to preserve debug information.
6677 FIXME: This is quite broken. Modifying
6678 the symbol here means we will be changing
6679 all uses of the symbol, not just those in
6680 debug sections. The only thing that makes
6681 this half reasonable is that debug sections
6682 tend to come after other sections. Of
6683 course, that doesn't help with globals.
6684 ??? All link-once sections of the same name
6685 ought to define the same set of symbols, so
6686 it would seem that globals ought to always
6687 be defined in the kept section. */
6688 if (sec->kept_section != NULL)
6690 asection *member;
6692 /* Check if it is a linkonce section or
6693 member of a comdat group. */
6694 if (elf_sec_group (sec) == NULL
6695 && sec->size == sec->kept_section->size)
6697 *ps = sec->kept_section;
6698 continue;
6700 else if (elf_sec_group (sec) != NULL
6701 && (member = match_group_member (sec, sec->kept_section))
6702 && sec->size == member->size)
6704 *ps = member;
6705 continue;
6709 else if (complain)
6711 (*_bfd_error_handler)
6712 (_("`%s' referenced in section `%A' of %B: "
6713 "defined in discarded section `%A' of %B\n"),
6714 o, input_bfd, sec, sec->owner, sym_name);
6717 /* Remove the symbol reference from the reloc, but
6718 don't kill the reloc completely. This is so that
6719 a zero value will be written into the section,
6720 which may have non-zero contents put there by the
6721 assembler. Zero in things like an eh_frame fde
6722 pc_begin allows stack unwinders to recognize the
6723 fde as bogus. */
6724 rel->r_info &= r_type_mask;
6725 rel->r_addend = 0;
6730 /* Relocate the section by invoking a back end routine.
6732 The back end routine is responsible for adjusting the
6733 section contents as necessary, and (if using Rela relocs
6734 and generating a relocatable output file) adjusting the
6735 reloc addend as necessary.
6737 The back end routine does not have to worry about setting
6738 the reloc address or the reloc symbol index.
6740 The back end routine is given a pointer to the swapped in
6741 internal symbols, and can access the hash table entries
6742 for the external symbols via elf_sym_hashes (input_bfd).
6744 When generating relocatable output, the back end routine
6745 must handle STB_LOCAL/STT_SECTION symbols specially. The
6746 output symbol is going to be a section symbol
6747 corresponding to the output section, which will require
6748 the addend to be adjusted. */
6750 if (! (*relocate_section) (output_bfd, finfo->info,
6751 input_bfd, o, contents,
6752 internal_relocs,
6753 isymbuf,
6754 finfo->sections))
6755 return FALSE;
6757 if (emit_relocs)
6759 Elf_Internal_Rela *irela;
6760 Elf_Internal_Rela *irelaend;
6761 bfd_vma last_offset;
6762 struct elf_link_hash_entry **rel_hash;
6763 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
6764 unsigned int next_erel;
6765 bfd_boolean (*reloc_emitter)
6766 (bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *);
6767 bfd_boolean rela_normal;
6769 input_rel_hdr = &elf_section_data (o)->rel_hdr;
6770 rela_normal = (bed->rela_normal
6771 && (input_rel_hdr->sh_entsize
6772 == bed->s->sizeof_rela));
6774 /* Adjust the reloc addresses and symbol indices. */
6776 irela = internal_relocs;
6777 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
6778 rel_hash = (elf_section_data (o->output_section)->rel_hashes
6779 + elf_section_data (o->output_section)->rel_count
6780 + elf_section_data (o->output_section)->rel_count2);
6781 last_offset = o->output_offset;
6782 if (!finfo->info->relocatable)
6783 last_offset += o->output_section->vma;
6784 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
6786 unsigned long r_symndx;
6787 asection *sec;
6788 Elf_Internal_Sym sym;
6790 if (next_erel == bed->s->int_rels_per_ext_rel)
6792 rel_hash++;
6793 next_erel = 0;
6796 irela->r_offset = _bfd_elf_section_offset (output_bfd,
6797 finfo->info, o,
6798 irela->r_offset);
6799 if (irela->r_offset >= (bfd_vma) -2)
6801 /* This is a reloc for a deleted entry or somesuch.
6802 Turn it into an R_*_NONE reloc, at the same
6803 offset as the last reloc. elf_eh_frame.c and
6804 elf_bfd_discard_info rely on reloc offsets
6805 being ordered. */
6806 irela->r_offset = last_offset;
6807 irela->r_info = 0;
6808 irela->r_addend = 0;
6809 continue;
6812 irela->r_offset += o->output_offset;
6814 /* Relocs in an executable have to be virtual addresses. */
6815 if (!finfo->info->relocatable)
6816 irela->r_offset += o->output_section->vma;
6818 last_offset = irela->r_offset;
6820 r_symndx = irela->r_info >> r_sym_shift;
6821 if (r_symndx == STN_UNDEF)
6822 continue;
6824 if (r_symndx >= locsymcount
6825 || (elf_bad_symtab (input_bfd)
6826 && finfo->sections[r_symndx] == NULL))
6828 struct elf_link_hash_entry *rh;
6829 unsigned long indx;
6831 /* This is a reloc against a global symbol. We
6832 have not yet output all the local symbols, so
6833 we do not know the symbol index of any global
6834 symbol. We set the rel_hash entry for this
6835 reloc to point to the global hash table entry
6836 for this symbol. The symbol index is then
6837 set at the end of bfd_elf_final_link. */
6838 indx = r_symndx - extsymoff;
6839 rh = elf_sym_hashes (input_bfd)[indx];
6840 while (rh->root.type == bfd_link_hash_indirect
6841 || rh->root.type == bfd_link_hash_warning)
6842 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
6844 /* Setting the index to -2 tells
6845 elf_link_output_extsym that this symbol is
6846 used by a reloc. */
6847 BFD_ASSERT (rh->indx < 0);
6848 rh->indx = -2;
6850 *rel_hash = rh;
6852 continue;
6855 /* This is a reloc against a local symbol. */
6857 *rel_hash = NULL;
6858 sym = isymbuf[r_symndx];
6859 sec = finfo->sections[r_symndx];
6860 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
6862 /* I suppose the backend ought to fill in the
6863 section of any STT_SECTION symbol against a
6864 processor specific section. */
6865 r_symndx = 0;
6866 if (bfd_is_abs_section (sec))
6868 else if (sec == NULL || sec->owner == NULL)
6870 bfd_set_error (bfd_error_bad_value);
6871 return FALSE;
6873 else
6875 asection *osec = sec->output_section;
6877 /* If we have discarded a section, the output
6878 section will be the absolute section. In
6879 case of discarded link-once and discarded
6880 SEC_MERGE sections, use the kept section. */
6881 if (bfd_is_abs_section (osec)
6882 && sec->kept_section != NULL
6883 && sec->kept_section->output_section != NULL)
6885 osec = sec->kept_section->output_section;
6886 irela->r_addend -= osec->vma;
6889 if (!bfd_is_abs_section (osec))
6891 r_symndx = osec->target_index;
6892 BFD_ASSERT (r_symndx != 0);
6896 /* Adjust the addend according to where the
6897 section winds up in the output section. */
6898 if (rela_normal)
6899 irela->r_addend += sec->output_offset;
6901 else
6903 if (finfo->indices[r_symndx] == -1)
6905 unsigned long shlink;
6906 const char *name;
6907 asection *osec;
6909 if (finfo->info->strip == strip_all)
6911 /* You can't do ld -r -s. */
6912 bfd_set_error (bfd_error_invalid_operation);
6913 return FALSE;
6916 /* This symbol was skipped earlier, but
6917 since it is needed by a reloc, we
6918 must output it now. */
6919 shlink = symtab_hdr->sh_link;
6920 name = (bfd_elf_string_from_elf_section
6921 (input_bfd, shlink, sym.st_name));
6922 if (name == NULL)
6923 return FALSE;
6925 osec = sec->output_section;
6926 sym.st_shndx =
6927 _bfd_elf_section_from_bfd_section (output_bfd,
6928 osec);
6929 if (sym.st_shndx == SHN_BAD)
6930 return FALSE;
6932 sym.st_value += sec->output_offset;
6933 if (! finfo->info->relocatable)
6935 sym.st_value += osec->vma;
6936 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
6938 /* STT_TLS symbols are relative to PT_TLS
6939 segment base. */
6940 BFD_ASSERT (elf_hash_table (finfo->info)
6941 ->tls_sec != NULL);
6942 sym.st_value -= (elf_hash_table (finfo->info)
6943 ->tls_sec->vma);
6947 finfo->indices[r_symndx]
6948 = bfd_get_symcount (output_bfd);
6950 if (! elf_link_output_sym (finfo, name, &sym, sec,
6951 NULL))
6952 return FALSE;
6955 r_symndx = finfo->indices[r_symndx];
6958 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
6959 | (irela->r_info & r_type_mask));
6962 /* Swap out the relocs. */
6963 if (bed->elf_backend_emit_relocs
6964 && !(finfo->info->relocatable
6965 || finfo->info->emitrelocations))
6966 reloc_emitter = bed->elf_backend_emit_relocs;
6967 else
6968 reloc_emitter = _bfd_elf_link_output_relocs;
6970 if (input_rel_hdr->sh_size != 0
6971 && ! (*reloc_emitter) (output_bfd, o, input_rel_hdr,
6972 internal_relocs))
6973 return FALSE;
6975 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
6976 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
6978 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
6979 * bed->s->int_rels_per_ext_rel);
6980 if (! (*reloc_emitter) (output_bfd, o, input_rel_hdr2,
6981 internal_relocs))
6982 return FALSE;
6987 /* Write out the modified section contents. */
6988 if (bed->elf_backend_write_section
6989 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
6991 /* Section written out. */
6993 else switch (o->sec_info_type)
6995 case ELF_INFO_TYPE_STABS:
6996 if (! (_bfd_write_section_stabs
6997 (output_bfd,
6998 &elf_hash_table (finfo->info)->stab_info,
6999 o, &elf_section_data (o)->sec_info, contents)))
7000 return FALSE;
7001 break;
7002 case ELF_INFO_TYPE_MERGE:
7003 if (! _bfd_write_merged_section (output_bfd, o,
7004 elf_section_data (o)->sec_info))
7005 return FALSE;
7006 break;
7007 case ELF_INFO_TYPE_EH_FRAME:
7009 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
7010 o, contents))
7011 return FALSE;
7013 break;
7014 default:
7016 if (! (o->flags & SEC_EXCLUDE)
7017 && ! bfd_set_section_contents (output_bfd, o->output_section,
7018 contents,
7019 (file_ptr) o->output_offset,
7020 o->size))
7021 return FALSE;
7023 break;
7027 return TRUE;
7030 /* Generate a reloc when linking an ELF file. This is a reloc
7031 requested by the linker, and does come from any input file. This
7032 is used to build constructor and destructor tables when linking
7033 with -Ur. */
7035 static bfd_boolean
7036 elf_reloc_link_order (bfd *output_bfd,
7037 struct bfd_link_info *info,
7038 asection *output_section,
7039 struct bfd_link_order *link_order)
7041 reloc_howto_type *howto;
7042 long indx;
7043 bfd_vma offset;
7044 bfd_vma addend;
7045 struct elf_link_hash_entry **rel_hash_ptr;
7046 Elf_Internal_Shdr *rel_hdr;
7047 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7048 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
7049 bfd_byte *erel;
7050 unsigned int i;
7052 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
7053 if (howto == NULL)
7055 bfd_set_error (bfd_error_bad_value);
7056 return FALSE;
7059 addend = link_order->u.reloc.p->addend;
7061 /* Figure out the symbol index. */
7062 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
7063 + elf_section_data (output_section)->rel_count
7064 + elf_section_data (output_section)->rel_count2);
7065 if (link_order->type == bfd_section_reloc_link_order)
7067 indx = link_order->u.reloc.p->u.section->target_index;
7068 BFD_ASSERT (indx != 0);
7069 *rel_hash_ptr = NULL;
7071 else
7073 struct elf_link_hash_entry *h;
7075 /* Treat a reloc against a defined symbol as though it were
7076 actually against the section. */
7077 h = ((struct elf_link_hash_entry *)
7078 bfd_wrapped_link_hash_lookup (output_bfd, info,
7079 link_order->u.reloc.p->u.name,
7080 FALSE, FALSE, TRUE));
7081 if (h != NULL
7082 && (h->root.type == bfd_link_hash_defined
7083 || h->root.type == bfd_link_hash_defweak))
7085 asection *section;
7087 section = h->root.u.def.section;
7088 indx = section->output_section->target_index;
7089 *rel_hash_ptr = NULL;
7090 /* It seems that we ought to add the symbol value to the
7091 addend here, but in practice it has already been added
7092 because it was passed to constructor_callback. */
7093 addend += section->output_section->vma + section->output_offset;
7095 else if (h != NULL)
7097 /* Setting the index to -2 tells elf_link_output_extsym that
7098 this symbol is used by a reloc. */
7099 h->indx = -2;
7100 *rel_hash_ptr = h;
7101 indx = 0;
7103 else
7105 if (! ((*info->callbacks->unattached_reloc)
7106 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
7107 return FALSE;
7108 indx = 0;
7112 /* If this is an inplace reloc, we must write the addend into the
7113 object file. */
7114 if (howto->partial_inplace && addend != 0)
7116 bfd_size_type size;
7117 bfd_reloc_status_type rstat;
7118 bfd_byte *buf;
7119 bfd_boolean ok;
7120 const char *sym_name;
7122 size = bfd_get_reloc_size (howto);
7123 buf = bfd_zmalloc (size);
7124 if (buf == NULL)
7125 return FALSE;
7126 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
7127 switch (rstat)
7129 case bfd_reloc_ok:
7130 break;
7132 default:
7133 case bfd_reloc_outofrange:
7134 abort ();
7136 case bfd_reloc_overflow:
7137 if (link_order->type == bfd_section_reloc_link_order)
7138 sym_name = bfd_section_name (output_bfd,
7139 link_order->u.reloc.p->u.section);
7140 else
7141 sym_name = link_order->u.reloc.p->u.name;
7142 if (! ((*info->callbacks->reloc_overflow)
7143 (info, sym_name, howto->name, addend, NULL, NULL, 0)))
7145 free (buf);
7146 return FALSE;
7148 break;
7150 ok = bfd_set_section_contents (output_bfd, output_section, buf,
7151 link_order->offset, size);
7152 free (buf);
7153 if (! ok)
7154 return FALSE;
7157 /* The address of a reloc is relative to the section in a
7158 relocatable file, and is a virtual address in an executable
7159 file. */
7160 offset = link_order->offset;
7161 if (! info->relocatable)
7162 offset += output_section->vma;
7164 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7166 irel[i].r_offset = offset;
7167 irel[i].r_info = 0;
7168 irel[i].r_addend = 0;
7170 if (bed->s->arch_size == 32)
7171 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
7172 else
7173 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
7175 rel_hdr = &elf_section_data (output_section)->rel_hdr;
7176 erel = rel_hdr->contents;
7177 if (rel_hdr->sh_type == SHT_REL)
7179 erel += (elf_section_data (output_section)->rel_count
7180 * bed->s->sizeof_rel);
7181 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
7183 else
7185 irel[0].r_addend = addend;
7186 erel += (elf_section_data (output_section)->rel_count
7187 * bed->s->sizeof_rela);
7188 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
7191 ++elf_section_data (output_section)->rel_count;
7193 return TRUE;
7197 /* Get the output vma of the section pointed to by the sh_link field. */
7199 static bfd_vma
7200 elf_get_linked_section_vma (struct bfd_link_order *p)
7202 Elf_Internal_Shdr **elf_shdrp;
7203 asection *s;
7204 int elfsec;
7206 s = p->u.indirect.section;
7207 elf_shdrp = elf_elfsections (s->owner);
7208 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
7209 elfsec = elf_shdrp[elfsec]->sh_link;
7210 /* PR 290:
7211 The Intel C compiler generates SHT_IA_64_UNWIND with
7212 SHF_LINK_ORDER. But it doesn't set theh sh_link or
7213 sh_info fields. Hence we could get the situation
7214 where elfsec is 0. */
7215 if (elfsec == 0)
7217 const struct elf_backend_data *bed
7218 = get_elf_backend_data (s->owner);
7219 if (bed->link_order_error_handler)
7220 bed->link_order_error_handler
7221 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
7222 return 0;
7224 else
7226 s = elf_shdrp[elfsec]->bfd_section;
7227 return s->output_section->vma + s->output_offset;
7232 /* Compare two sections based on the locations of the sections they are
7233 linked to. Used by elf_fixup_link_order. */
7235 static int
7236 compare_link_order (const void * a, const void * b)
7238 bfd_vma apos;
7239 bfd_vma bpos;
7241 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
7242 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
7243 if (apos < bpos)
7244 return -1;
7245 return apos > bpos;
7249 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7250 order as their linked sections. Returns false if this could not be done
7251 because an output section includes both ordered and unordered
7252 sections. Ideally we'd do this in the linker proper. */
7254 static bfd_boolean
7255 elf_fixup_link_order (bfd *abfd, asection *o)
7257 int seen_linkorder;
7258 int seen_other;
7259 int n;
7260 struct bfd_link_order *p;
7261 bfd *sub;
7262 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7263 int elfsec;
7264 struct bfd_link_order **sections;
7265 asection *s;
7266 bfd_vma offset;
7268 seen_other = 0;
7269 seen_linkorder = 0;
7270 for (p = o->link_order_head; p != NULL; p = p->next)
7272 if (p->type == bfd_indirect_link_order
7273 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
7274 == bfd_target_elf_flavour)
7275 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
7277 s = p->u.indirect.section;
7278 elfsec = _bfd_elf_section_from_bfd_section (sub, s);
7279 if (elfsec != -1
7280 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER)
7281 seen_linkorder++;
7282 else
7283 seen_other++;
7285 else
7286 seen_other++;
7289 if (!seen_linkorder)
7290 return TRUE;
7292 if (seen_other && seen_linkorder)
7294 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
7296 bfd_set_error (bfd_error_bad_value);
7297 return FALSE;
7300 sections = (struct bfd_link_order **)
7301 xmalloc (seen_linkorder * sizeof (struct bfd_link_order *));
7302 seen_linkorder = 0;
7304 for (p = o->link_order_head; p != NULL; p = p->next)
7306 sections[seen_linkorder++] = p;
7308 /* Sort the input sections in the order of their linked section. */
7309 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
7310 compare_link_order);
7312 /* Change the offsets of the sections. */
7313 offset = 0;
7314 for (n = 0; n < seen_linkorder; n++)
7316 s = sections[n]->u.indirect.section;
7317 offset &= ~(bfd_vma)((1 << s->alignment_power) - 1);
7318 s->output_offset = offset;
7319 sections[n]->offset = offset;
7320 offset += sections[n]->size;
7323 return TRUE;
7327 /* Do the final step of an ELF link. */
7329 bfd_boolean
7330 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
7332 bfd_boolean dynamic;
7333 bfd_boolean emit_relocs;
7334 bfd *dynobj;
7335 struct elf_final_link_info finfo;
7336 register asection *o;
7337 register struct bfd_link_order *p;
7338 register bfd *sub;
7339 bfd_size_type max_contents_size;
7340 bfd_size_type max_external_reloc_size;
7341 bfd_size_type max_internal_reloc_count;
7342 bfd_size_type max_sym_count;
7343 bfd_size_type max_sym_shndx_count;
7344 file_ptr off;
7345 Elf_Internal_Sym elfsym;
7346 unsigned int i;
7347 Elf_Internal_Shdr *symtab_hdr;
7348 Elf_Internal_Shdr *symtab_shndx_hdr;
7349 Elf_Internal_Shdr *symstrtab_hdr;
7350 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7351 struct elf_outext_info eoinfo;
7352 bfd_boolean merged;
7353 size_t relativecount = 0;
7354 asection *reldyn = 0;
7355 bfd_size_type amt;
7357 if (! is_elf_hash_table (info->hash))
7358 return FALSE;
7360 if (info->shared)
7361 abfd->flags |= DYNAMIC;
7363 dynamic = elf_hash_table (info)->dynamic_sections_created;
7364 dynobj = elf_hash_table (info)->dynobj;
7366 emit_relocs = (info->relocatable
7367 || info->emitrelocations
7368 || bed->elf_backend_emit_relocs);
7370 finfo.info = info;
7371 finfo.output_bfd = abfd;
7372 finfo.symstrtab = _bfd_elf_stringtab_init ();
7373 if (finfo.symstrtab == NULL)
7374 return FALSE;
7376 if (! dynamic)
7378 finfo.dynsym_sec = NULL;
7379 finfo.hash_sec = NULL;
7380 finfo.symver_sec = NULL;
7382 else
7384 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
7385 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
7386 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
7387 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
7388 /* Note that it is OK if symver_sec is NULL. */
7391 finfo.contents = NULL;
7392 finfo.external_relocs = NULL;
7393 finfo.internal_relocs = NULL;
7394 finfo.external_syms = NULL;
7395 finfo.locsym_shndx = NULL;
7396 finfo.internal_syms = NULL;
7397 finfo.indices = NULL;
7398 finfo.sections = NULL;
7399 finfo.symbuf = NULL;
7400 finfo.symshndxbuf = NULL;
7401 finfo.symbuf_count = 0;
7402 finfo.shndxbuf_size = 0;
7404 /* Count up the number of relocations we will output for each output
7405 section, so that we know the sizes of the reloc sections. We
7406 also figure out some maximum sizes. */
7407 max_contents_size = 0;
7408 max_external_reloc_size = 0;
7409 max_internal_reloc_count = 0;
7410 max_sym_count = 0;
7411 max_sym_shndx_count = 0;
7412 merged = FALSE;
7413 for (o = abfd->sections; o != NULL; o = o->next)
7415 struct bfd_elf_section_data *esdo = elf_section_data (o);
7416 o->reloc_count = 0;
7418 for (p = o->link_order_head; p != NULL; p = p->next)
7420 unsigned int reloc_count = 0;
7421 struct bfd_elf_section_data *esdi = NULL;
7422 unsigned int *rel_count1;
7424 if (p->type == bfd_section_reloc_link_order
7425 || p->type == bfd_symbol_reloc_link_order)
7426 reloc_count = 1;
7427 else if (p->type == bfd_indirect_link_order)
7429 asection *sec;
7431 sec = p->u.indirect.section;
7432 esdi = elf_section_data (sec);
7434 /* Mark all sections which are to be included in the
7435 link. This will normally be every section. We need
7436 to do this so that we can identify any sections which
7437 the linker has decided to not include. */
7438 sec->linker_mark = TRUE;
7440 if (sec->flags & SEC_MERGE)
7441 merged = TRUE;
7443 if (info->relocatable || info->emitrelocations)
7444 reloc_count = sec->reloc_count;
7445 else if (bed->elf_backend_count_relocs)
7447 Elf_Internal_Rela * relocs;
7449 relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
7450 info->keep_memory);
7452 reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs);
7454 if (elf_section_data (o)->relocs != relocs)
7455 free (relocs);
7458 if (sec->rawsize > max_contents_size)
7459 max_contents_size = sec->rawsize;
7460 if (sec->size > max_contents_size)
7461 max_contents_size = sec->size;
7463 /* We are interested in just local symbols, not all
7464 symbols. */
7465 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
7466 && (sec->owner->flags & DYNAMIC) == 0)
7468 size_t sym_count;
7470 if (elf_bad_symtab (sec->owner))
7471 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
7472 / bed->s->sizeof_sym);
7473 else
7474 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
7476 if (sym_count > max_sym_count)
7477 max_sym_count = sym_count;
7479 if (sym_count > max_sym_shndx_count
7480 && elf_symtab_shndx (sec->owner) != 0)
7481 max_sym_shndx_count = sym_count;
7483 if ((sec->flags & SEC_RELOC) != 0)
7485 size_t ext_size;
7487 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
7488 if (ext_size > max_external_reloc_size)
7489 max_external_reloc_size = ext_size;
7490 if (sec->reloc_count > max_internal_reloc_count)
7491 max_internal_reloc_count = sec->reloc_count;
7496 if (reloc_count == 0)
7497 continue;
7499 o->reloc_count += reloc_count;
7501 /* MIPS may have a mix of REL and RELA relocs on sections.
7502 To support this curious ABI we keep reloc counts in
7503 elf_section_data too. We must be careful to add the
7504 relocations from the input section to the right output
7505 count. FIXME: Get rid of one count. We have
7506 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7507 rel_count1 = &esdo->rel_count;
7508 if (esdi != NULL)
7510 bfd_boolean same_size;
7511 bfd_size_type entsize1;
7513 entsize1 = esdi->rel_hdr.sh_entsize;
7514 BFD_ASSERT (entsize1 == bed->s->sizeof_rel
7515 || entsize1 == bed->s->sizeof_rela);
7516 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
7518 if (!same_size)
7519 rel_count1 = &esdo->rel_count2;
7521 if (esdi->rel_hdr2 != NULL)
7523 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
7524 unsigned int alt_count;
7525 unsigned int *rel_count2;
7527 BFD_ASSERT (entsize2 != entsize1
7528 && (entsize2 == bed->s->sizeof_rel
7529 || entsize2 == bed->s->sizeof_rela));
7531 rel_count2 = &esdo->rel_count2;
7532 if (!same_size)
7533 rel_count2 = &esdo->rel_count;
7535 /* The following is probably too simplistic if the
7536 backend counts output relocs unusually. */
7537 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
7538 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
7539 *rel_count2 += alt_count;
7540 reloc_count -= alt_count;
7543 *rel_count1 += reloc_count;
7546 if (o->reloc_count > 0)
7547 o->flags |= SEC_RELOC;
7548 else
7550 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7551 set it (this is probably a bug) and if it is set
7552 assign_section_numbers will create a reloc section. */
7553 o->flags &=~ SEC_RELOC;
7556 /* If the SEC_ALLOC flag is not set, force the section VMA to
7557 zero. This is done in elf_fake_sections as well, but forcing
7558 the VMA to 0 here will ensure that relocs against these
7559 sections are handled correctly. */
7560 if ((o->flags & SEC_ALLOC) == 0
7561 && ! o->user_set_vma)
7562 o->vma = 0;
7565 if (! info->relocatable && merged)
7566 elf_link_hash_traverse (elf_hash_table (info),
7567 _bfd_elf_link_sec_merge_syms, abfd);
7569 /* Figure out the file positions for everything but the symbol table
7570 and the relocs. We set symcount to force assign_section_numbers
7571 to create a symbol table. */
7572 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
7573 BFD_ASSERT (! abfd->output_has_begun);
7574 if (! _bfd_elf_compute_section_file_positions (abfd, info))
7575 goto error_return;
7577 /* Set sizes, and assign file positions for reloc sections. */
7578 for (o = abfd->sections; o != NULL; o = o->next)
7580 if ((o->flags & SEC_RELOC) != 0)
7582 if (!(_bfd_elf_link_size_reloc_section
7583 (abfd, &elf_section_data (o)->rel_hdr, o)))
7584 goto error_return;
7586 if (elf_section_data (o)->rel_hdr2
7587 && !(_bfd_elf_link_size_reloc_section
7588 (abfd, elf_section_data (o)->rel_hdr2, o)))
7589 goto error_return;
7592 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
7593 to count upwards while actually outputting the relocations. */
7594 elf_section_data (o)->rel_count = 0;
7595 elf_section_data (o)->rel_count2 = 0;
7598 _bfd_elf_assign_file_positions_for_relocs (abfd);
7600 /* We have now assigned file positions for all the sections except
7601 .symtab and .strtab. We start the .symtab section at the current
7602 file position, and write directly to it. We build the .strtab
7603 section in memory. */
7604 bfd_get_symcount (abfd) = 0;
7605 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7606 /* sh_name is set in prep_headers. */
7607 symtab_hdr->sh_type = SHT_SYMTAB;
7608 /* sh_flags, sh_addr and sh_size all start off zero. */
7609 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
7610 /* sh_link is set in assign_section_numbers. */
7611 /* sh_info is set below. */
7612 /* sh_offset is set just below. */
7613 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
7615 off = elf_tdata (abfd)->next_file_pos;
7616 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
7618 /* Note that at this point elf_tdata (abfd)->next_file_pos is
7619 incorrect. We do not yet know the size of the .symtab section.
7620 We correct next_file_pos below, after we do know the size. */
7622 /* Allocate a buffer to hold swapped out symbols. This is to avoid
7623 continuously seeking to the right position in the file. */
7624 if (! info->keep_memory || max_sym_count < 20)
7625 finfo.symbuf_size = 20;
7626 else
7627 finfo.symbuf_size = max_sym_count;
7628 amt = finfo.symbuf_size;
7629 amt *= bed->s->sizeof_sym;
7630 finfo.symbuf = bfd_malloc (amt);
7631 if (finfo.symbuf == NULL)
7632 goto error_return;
7633 if (elf_numsections (abfd) > SHN_LORESERVE)
7635 /* Wild guess at number of output symbols. realloc'd as needed. */
7636 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
7637 finfo.shndxbuf_size = amt;
7638 amt *= sizeof (Elf_External_Sym_Shndx);
7639 finfo.symshndxbuf = bfd_zmalloc (amt);
7640 if (finfo.symshndxbuf == NULL)
7641 goto error_return;
7644 /* Start writing out the symbol table. The first symbol is always a
7645 dummy symbol. */
7646 if (info->strip != strip_all
7647 || emit_relocs)
7649 elfsym.st_value = 0;
7650 elfsym.st_size = 0;
7651 elfsym.st_info = 0;
7652 elfsym.st_other = 0;
7653 elfsym.st_shndx = SHN_UNDEF;
7654 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
7655 NULL))
7656 goto error_return;
7659 #if 0
7660 /* Some standard ELF linkers do this, but we don't because it causes
7661 bootstrap comparison failures. */
7662 /* Output a file symbol for the output file as the second symbol.
7663 We output this even if we are discarding local symbols, although
7664 I'm not sure if this is correct. */
7665 elfsym.st_value = 0;
7666 elfsym.st_size = 0;
7667 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
7668 elfsym.st_other = 0;
7669 elfsym.st_shndx = SHN_ABS;
7670 if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd),
7671 &elfsym, bfd_abs_section_ptr, NULL))
7672 goto error_return;
7673 #endif
7675 /* Output a symbol for each section. We output these even if we are
7676 discarding local symbols, since they are used for relocs. These
7677 symbols have no names. We store the index of each one in the
7678 index field of the section, so that we can find it again when
7679 outputting relocs. */
7680 if (info->strip != strip_all
7681 || emit_relocs)
7683 elfsym.st_size = 0;
7684 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
7685 elfsym.st_other = 0;
7686 for (i = 1; i < elf_numsections (abfd); i++)
7688 o = bfd_section_from_elf_index (abfd, i);
7689 if (o != NULL)
7690 o->target_index = bfd_get_symcount (abfd);
7691 elfsym.st_shndx = i;
7692 if (info->relocatable || o == NULL)
7693 elfsym.st_value = 0;
7694 else
7695 elfsym.st_value = o->vma;
7696 if (! elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
7697 goto error_return;
7698 if (i == SHN_LORESERVE - 1)
7699 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
7703 /* Allocate some memory to hold information read in from the input
7704 files. */
7705 if (max_contents_size != 0)
7707 finfo.contents = bfd_malloc (max_contents_size);
7708 if (finfo.contents == NULL)
7709 goto error_return;
7712 if (max_external_reloc_size != 0)
7714 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
7715 if (finfo.external_relocs == NULL)
7716 goto error_return;
7719 if (max_internal_reloc_count != 0)
7721 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
7722 amt *= sizeof (Elf_Internal_Rela);
7723 finfo.internal_relocs = bfd_malloc (amt);
7724 if (finfo.internal_relocs == NULL)
7725 goto error_return;
7728 if (max_sym_count != 0)
7730 amt = max_sym_count * bed->s->sizeof_sym;
7731 finfo.external_syms = bfd_malloc (amt);
7732 if (finfo.external_syms == NULL)
7733 goto error_return;
7735 amt = max_sym_count * sizeof (Elf_Internal_Sym);
7736 finfo.internal_syms = bfd_malloc (amt);
7737 if (finfo.internal_syms == NULL)
7738 goto error_return;
7740 amt = max_sym_count * sizeof (long);
7741 finfo.indices = bfd_malloc (amt);
7742 if (finfo.indices == NULL)
7743 goto error_return;
7745 amt = max_sym_count * sizeof (asection *);
7746 finfo.sections = bfd_malloc (amt);
7747 if (finfo.sections == NULL)
7748 goto error_return;
7751 if (max_sym_shndx_count != 0)
7753 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
7754 finfo.locsym_shndx = bfd_malloc (amt);
7755 if (finfo.locsym_shndx == NULL)
7756 goto error_return;
7759 if (elf_hash_table (info)->tls_sec)
7761 bfd_vma base, end = 0;
7762 asection *sec;
7764 for (sec = elf_hash_table (info)->tls_sec;
7765 sec && (sec->flags & SEC_THREAD_LOCAL);
7766 sec = sec->next)
7768 bfd_vma size = sec->size;
7770 if (size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0)
7772 struct bfd_link_order *o;
7774 for (o = sec->link_order_head; o != NULL; o = o->next)
7775 if (size < o->offset + o->size)
7776 size = o->offset + o->size;
7778 end = sec->vma + size;
7780 base = elf_hash_table (info)->tls_sec->vma;
7781 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
7782 elf_hash_table (info)->tls_size = end - base;
7785 /* Reorder SHF_LINK_ORDER sections. */
7786 for (o = abfd->sections; o != NULL; o = o->next)
7788 if (!elf_fixup_link_order (abfd, o))
7789 return FALSE;
7792 /* Since ELF permits relocations to be against local symbols, we
7793 must have the local symbols available when we do the relocations.
7794 Since we would rather only read the local symbols once, and we
7795 would rather not keep them in memory, we handle all the
7796 relocations for a single input file at the same time.
7798 Unfortunately, there is no way to know the total number of local
7799 symbols until we have seen all of them, and the local symbol
7800 indices precede the global symbol indices. This means that when
7801 we are generating relocatable output, and we see a reloc against
7802 a global symbol, we can not know the symbol index until we have
7803 finished examining all the local symbols to see which ones we are
7804 going to output. To deal with this, we keep the relocations in
7805 memory, and don't output them until the end of the link. This is
7806 an unfortunate waste of memory, but I don't see a good way around
7807 it. Fortunately, it only happens when performing a relocatable
7808 link, which is not the common case. FIXME: If keep_memory is set
7809 we could write the relocs out and then read them again; I don't
7810 know how bad the memory loss will be. */
7812 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
7813 sub->output_has_begun = FALSE;
7814 for (o = abfd->sections; o != NULL; o = o->next)
7816 for (p = o->link_order_head; p != NULL; p = p->next)
7818 if (p->type == bfd_indirect_link_order
7819 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
7820 == bfd_target_elf_flavour)
7821 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
7823 if (! sub->output_has_begun)
7825 if (! elf_link_input_bfd (&finfo, sub))
7826 goto error_return;
7827 sub->output_has_begun = TRUE;
7830 else if (p->type == bfd_section_reloc_link_order
7831 || p->type == bfd_symbol_reloc_link_order)
7833 if (! elf_reloc_link_order (abfd, info, o, p))
7834 goto error_return;
7836 else
7838 if (! _bfd_default_link_order (abfd, info, o, p))
7839 goto error_return;
7844 /* Output any global symbols that got converted to local in a
7845 version script or due to symbol visibility. We do this in a
7846 separate step since ELF requires all local symbols to appear
7847 prior to any global symbols. FIXME: We should only do this if
7848 some global symbols were, in fact, converted to become local.
7849 FIXME: Will this work correctly with the Irix 5 linker? */
7850 eoinfo.failed = FALSE;
7851 eoinfo.finfo = &finfo;
7852 eoinfo.localsyms = TRUE;
7853 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
7854 &eoinfo);
7855 if (eoinfo.failed)
7856 return FALSE;
7858 /* That wrote out all the local symbols. Finish up the symbol table
7859 with the global symbols. Even if we want to strip everything we
7860 can, we still need to deal with those global symbols that got
7861 converted to local in a version script. */
7863 /* The sh_info field records the index of the first non local symbol. */
7864 symtab_hdr->sh_info = bfd_get_symcount (abfd);
7866 if (dynamic
7867 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
7869 Elf_Internal_Sym sym;
7870 bfd_byte *dynsym = finfo.dynsym_sec->contents;
7871 long last_local = 0;
7873 /* Write out the section symbols for the output sections. */
7874 if (info->shared)
7876 asection *s;
7878 sym.st_size = 0;
7879 sym.st_name = 0;
7880 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
7881 sym.st_other = 0;
7883 for (s = abfd->sections; s != NULL; s = s->next)
7885 int indx;
7886 bfd_byte *dest;
7887 long dynindx;
7889 dynindx = elf_section_data (s)->dynindx;
7890 if (dynindx <= 0)
7891 continue;
7892 indx = elf_section_data (s)->this_idx;
7893 BFD_ASSERT (indx > 0);
7894 sym.st_shndx = indx;
7895 sym.st_value = s->vma;
7896 dest = dynsym + dynindx * bed->s->sizeof_sym;
7897 if (last_local < dynindx)
7898 last_local = dynindx;
7899 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
7903 /* Write out the local dynsyms. */
7904 if (elf_hash_table (info)->dynlocal)
7906 struct elf_link_local_dynamic_entry *e;
7907 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
7909 asection *s;
7910 bfd_byte *dest;
7912 sym.st_size = e->isym.st_size;
7913 sym.st_other = e->isym.st_other;
7915 /* Copy the internal symbol as is.
7916 Note that we saved a word of storage and overwrote
7917 the original st_name with the dynstr_index. */
7918 sym = e->isym;
7920 if (e->isym.st_shndx != SHN_UNDEF
7921 && (e->isym.st_shndx < SHN_LORESERVE
7922 || e->isym.st_shndx > SHN_HIRESERVE))
7924 s = bfd_section_from_elf_index (e->input_bfd,
7925 e->isym.st_shndx);
7927 sym.st_shndx =
7928 elf_section_data (s->output_section)->this_idx;
7929 sym.st_value = (s->output_section->vma
7930 + s->output_offset
7931 + e->isym.st_value);
7934 if (last_local < e->dynindx)
7935 last_local = e->dynindx;
7937 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
7938 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
7942 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
7943 last_local + 1;
7946 /* We get the global symbols from the hash table. */
7947 eoinfo.failed = FALSE;
7948 eoinfo.localsyms = FALSE;
7949 eoinfo.finfo = &finfo;
7950 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
7951 &eoinfo);
7952 if (eoinfo.failed)
7953 return FALSE;
7955 /* If backend needs to output some symbols not present in the hash
7956 table, do it now. */
7957 if (bed->elf_backend_output_arch_syms)
7959 typedef bfd_boolean (*out_sym_func)
7960 (void *, const char *, Elf_Internal_Sym *, asection *,
7961 struct elf_link_hash_entry *);
7963 if (! ((*bed->elf_backend_output_arch_syms)
7964 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
7965 return FALSE;
7968 /* Flush all symbols to the file. */
7969 if (! elf_link_flush_output_syms (&finfo, bed))
7970 return FALSE;
7972 /* Now we know the size of the symtab section. */
7973 off += symtab_hdr->sh_size;
7975 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
7976 if (symtab_shndx_hdr->sh_name != 0)
7978 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
7979 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
7980 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
7981 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
7982 symtab_shndx_hdr->sh_size = amt;
7984 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
7985 off, TRUE);
7987 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
7988 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
7989 return FALSE;
7993 /* Finish up and write out the symbol string table (.strtab)
7994 section. */
7995 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
7996 /* sh_name was set in prep_headers. */
7997 symstrtab_hdr->sh_type = SHT_STRTAB;
7998 symstrtab_hdr->sh_flags = 0;
7999 symstrtab_hdr->sh_addr = 0;
8000 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
8001 symstrtab_hdr->sh_entsize = 0;
8002 symstrtab_hdr->sh_link = 0;
8003 symstrtab_hdr->sh_info = 0;
8004 /* sh_offset is set just below. */
8005 symstrtab_hdr->sh_addralign = 1;
8007 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
8008 elf_tdata (abfd)->next_file_pos = off;
8010 if (bfd_get_symcount (abfd) > 0)
8012 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
8013 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
8014 return FALSE;
8017 /* Adjust the relocs to have the correct symbol indices. */
8018 for (o = abfd->sections; o != NULL; o = o->next)
8020 if ((o->flags & SEC_RELOC) == 0)
8021 continue;
8023 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
8024 elf_section_data (o)->rel_count,
8025 elf_section_data (o)->rel_hashes);
8026 if (elf_section_data (o)->rel_hdr2 != NULL)
8027 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
8028 elf_section_data (o)->rel_count2,
8029 (elf_section_data (o)->rel_hashes
8030 + elf_section_data (o)->rel_count));
8032 /* Set the reloc_count field to 0 to prevent write_relocs from
8033 trying to swap the relocs out itself. */
8034 o->reloc_count = 0;
8037 if (dynamic && info->combreloc && dynobj != NULL)
8038 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
8040 /* If we are linking against a dynamic object, or generating a
8041 shared library, finish up the dynamic linking information. */
8042 if (dynamic)
8044 bfd_byte *dyncon, *dynconend;
8046 /* Fix up .dynamic entries. */
8047 o = bfd_get_section_by_name (dynobj, ".dynamic");
8048 BFD_ASSERT (o != NULL);
8050 dyncon = o->contents;
8051 dynconend = o->contents + o->size;
8052 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
8054 Elf_Internal_Dyn dyn;
8055 const char *name;
8056 unsigned int type;
8058 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
8060 switch (dyn.d_tag)
8062 default:
8063 continue;
8064 case DT_NULL:
8065 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
8067 switch (elf_section_data (reldyn)->this_hdr.sh_type)
8069 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
8070 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
8071 default: continue;
8073 dyn.d_un.d_val = relativecount;
8074 relativecount = 0;
8075 break;
8077 continue;
8079 case DT_INIT:
8080 name = info->init_function;
8081 goto get_sym;
8082 case DT_FINI:
8083 name = info->fini_function;
8084 get_sym:
8086 struct elf_link_hash_entry *h;
8088 h = elf_link_hash_lookup (elf_hash_table (info), name,
8089 FALSE, FALSE, TRUE);
8090 if (h != NULL
8091 && (h->root.type == bfd_link_hash_defined
8092 || h->root.type == bfd_link_hash_defweak))
8094 dyn.d_un.d_val = h->root.u.def.value;
8095 o = h->root.u.def.section;
8096 if (o->output_section != NULL)
8097 dyn.d_un.d_val += (o->output_section->vma
8098 + o->output_offset);
8099 else
8101 /* The symbol is imported from another shared
8102 library and does not apply to this one. */
8103 dyn.d_un.d_val = 0;
8105 break;
8108 continue;
8110 case DT_PREINIT_ARRAYSZ:
8111 name = ".preinit_array";
8112 goto get_size;
8113 case DT_INIT_ARRAYSZ:
8114 name = ".init_array";
8115 goto get_size;
8116 case DT_FINI_ARRAYSZ:
8117 name = ".fini_array";
8118 get_size:
8119 o = bfd_get_section_by_name (abfd, name);
8120 if (o == NULL)
8122 (*_bfd_error_handler)
8123 (_("%B: could not find output section %s"), abfd, name);
8124 goto error_return;
8126 if (o->size == 0)
8127 (*_bfd_error_handler)
8128 (_("warning: %s section has zero size"), name);
8129 dyn.d_un.d_val = o->size;
8130 break;
8132 case DT_PREINIT_ARRAY:
8133 name = ".preinit_array";
8134 goto get_vma;
8135 case DT_INIT_ARRAY:
8136 name = ".init_array";
8137 goto get_vma;
8138 case DT_FINI_ARRAY:
8139 name = ".fini_array";
8140 goto get_vma;
8142 case DT_HASH:
8143 name = ".hash";
8144 goto get_vma;
8145 case DT_STRTAB:
8146 name = ".dynstr";
8147 goto get_vma;
8148 case DT_SYMTAB:
8149 name = ".dynsym";
8150 goto get_vma;
8151 case DT_VERDEF:
8152 name = ".gnu.version_d";
8153 goto get_vma;
8154 case DT_VERNEED:
8155 name = ".gnu.version_r";
8156 goto get_vma;
8157 case DT_VERSYM:
8158 name = ".gnu.version";
8159 get_vma:
8160 o = bfd_get_section_by_name (abfd, name);
8161 if (o == NULL)
8163 (*_bfd_error_handler)
8164 (_("%B: could not find output section %s"), abfd, name);
8165 goto error_return;
8167 dyn.d_un.d_ptr = o->vma;
8168 break;
8170 case DT_REL:
8171 case DT_RELA:
8172 case DT_RELSZ:
8173 case DT_RELASZ:
8174 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
8175 type = SHT_REL;
8176 else
8177 type = SHT_RELA;
8178 dyn.d_un.d_val = 0;
8179 for (i = 1; i < elf_numsections (abfd); i++)
8181 Elf_Internal_Shdr *hdr;
8183 hdr = elf_elfsections (abfd)[i];
8184 if (hdr->sh_type == type
8185 && (hdr->sh_flags & SHF_ALLOC) != 0)
8187 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
8188 dyn.d_un.d_val += hdr->sh_size;
8189 else
8191 if (dyn.d_un.d_val == 0
8192 || hdr->sh_addr < dyn.d_un.d_val)
8193 dyn.d_un.d_val = hdr->sh_addr;
8197 break;
8199 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
8203 /* If we have created any dynamic sections, then output them. */
8204 if (dynobj != NULL)
8206 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
8207 goto error_return;
8209 for (o = dynobj->sections; o != NULL; o = o->next)
8211 if ((o->flags & SEC_HAS_CONTENTS) == 0
8212 || o->size == 0
8213 || o->output_section == bfd_abs_section_ptr)
8214 continue;
8215 if ((o->flags & SEC_LINKER_CREATED) == 0)
8217 /* At this point, we are only interested in sections
8218 created by _bfd_elf_link_create_dynamic_sections. */
8219 continue;
8221 if (elf_hash_table (info)->stab_info.stabstr == o)
8222 continue;
8223 if (elf_hash_table (info)->eh_info.hdr_sec == o)
8224 continue;
8225 if ((elf_section_data (o->output_section)->this_hdr.sh_type
8226 != SHT_STRTAB)
8227 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
8229 if (! bfd_set_section_contents (abfd, o->output_section,
8230 o->contents,
8231 (file_ptr) o->output_offset,
8232 o->size))
8233 goto error_return;
8235 else
8237 /* The contents of the .dynstr section are actually in a
8238 stringtab. */
8239 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
8240 if (bfd_seek (abfd, off, SEEK_SET) != 0
8241 || ! _bfd_elf_strtab_emit (abfd,
8242 elf_hash_table (info)->dynstr))
8243 goto error_return;
8248 if (info->relocatable)
8250 bfd_boolean failed = FALSE;
8252 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
8253 if (failed)
8254 goto error_return;
8257 /* If we have optimized stabs strings, output them. */
8258 if (elf_hash_table (info)->stab_info.stabstr != NULL)
8260 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
8261 goto error_return;
8264 if (info->eh_frame_hdr)
8266 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
8267 goto error_return;
8270 if (finfo.symstrtab != NULL)
8271 _bfd_stringtab_free (finfo.symstrtab);
8272 if (finfo.contents != NULL)
8273 free (finfo.contents);
8274 if (finfo.external_relocs != NULL)
8275 free (finfo.external_relocs);
8276 if (finfo.internal_relocs != NULL)
8277 free (finfo.internal_relocs);
8278 if (finfo.external_syms != NULL)
8279 free (finfo.external_syms);
8280 if (finfo.locsym_shndx != NULL)
8281 free (finfo.locsym_shndx);
8282 if (finfo.internal_syms != NULL)
8283 free (finfo.internal_syms);
8284 if (finfo.indices != NULL)
8285 free (finfo.indices);
8286 if (finfo.sections != NULL)
8287 free (finfo.sections);
8288 if (finfo.symbuf != NULL)
8289 free (finfo.symbuf);
8290 if (finfo.symshndxbuf != NULL)
8291 free (finfo.symshndxbuf);
8292 for (o = abfd->sections; o != NULL; o = o->next)
8294 if ((o->flags & SEC_RELOC) != 0
8295 && elf_section_data (o)->rel_hashes != NULL)
8296 free (elf_section_data (o)->rel_hashes);
8299 elf_tdata (abfd)->linker = TRUE;
8301 return TRUE;
8303 error_return:
8304 if (finfo.symstrtab != NULL)
8305 _bfd_stringtab_free (finfo.symstrtab);
8306 if (finfo.contents != NULL)
8307 free (finfo.contents);
8308 if (finfo.external_relocs != NULL)
8309 free (finfo.external_relocs);
8310 if (finfo.internal_relocs != NULL)
8311 free (finfo.internal_relocs);
8312 if (finfo.external_syms != NULL)
8313 free (finfo.external_syms);
8314 if (finfo.locsym_shndx != NULL)
8315 free (finfo.locsym_shndx);
8316 if (finfo.internal_syms != NULL)
8317 free (finfo.internal_syms);
8318 if (finfo.indices != NULL)
8319 free (finfo.indices);
8320 if (finfo.sections != NULL)
8321 free (finfo.sections);
8322 if (finfo.symbuf != NULL)
8323 free (finfo.symbuf);
8324 if (finfo.symshndxbuf != NULL)
8325 free (finfo.symshndxbuf);
8326 for (o = abfd->sections; o != NULL; o = o->next)
8328 if ((o->flags & SEC_RELOC) != 0
8329 && elf_section_data (o)->rel_hashes != NULL)
8330 free (elf_section_data (o)->rel_hashes);
8333 return FALSE;
8336 /* Garbage collect unused sections. */
8338 /* The mark phase of garbage collection. For a given section, mark
8339 it and any sections in this section's group, and all the sections
8340 which define symbols to which it refers. */
8342 typedef asection * (*gc_mark_hook_fn)
8343 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
8344 struct elf_link_hash_entry *, Elf_Internal_Sym *);
8346 bfd_boolean
8347 _bfd_elf_gc_mark (struct bfd_link_info *info,
8348 asection *sec,
8349 gc_mark_hook_fn gc_mark_hook)
8351 bfd_boolean ret;
8352 asection *group_sec;
8354 sec->gc_mark = 1;
8356 /* Mark all the sections in the group. */
8357 group_sec = elf_section_data (sec)->next_in_group;
8358 if (group_sec && !group_sec->gc_mark)
8359 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
8360 return FALSE;
8362 /* Look through the section relocs. */
8363 ret = TRUE;
8364 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
8366 Elf_Internal_Rela *relstart, *rel, *relend;
8367 Elf_Internal_Shdr *symtab_hdr;
8368 struct elf_link_hash_entry **sym_hashes;
8369 size_t nlocsyms;
8370 size_t extsymoff;
8371 bfd *input_bfd = sec->owner;
8372 const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
8373 Elf_Internal_Sym *isym = NULL;
8374 int r_sym_shift;
8376 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8377 sym_hashes = elf_sym_hashes (input_bfd);
8379 /* Read the local symbols. */
8380 if (elf_bad_symtab (input_bfd))
8382 nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym;
8383 extsymoff = 0;
8385 else
8386 extsymoff = nlocsyms = symtab_hdr->sh_info;
8388 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
8389 if (isym == NULL && nlocsyms != 0)
8391 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
8392 NULL, NULL, NULL);
8393 if (isym == NULL)
8394 return FALSE;
8397 /* Read the relocations. */
8398 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
8399 info->keep_memory);
8400 if (relstart == NULL)
8402 ret = FALSE;
8403 goto out1;
8405 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
8407 if (bed->s->arch_size == 32)
8408 r_sym_shift = 8;
8409 else
8410 r_sym_shift = 32;
8412 for (rel = relstart; rel < relend; rel++)
8414 unsigned long r_symndx;
8415 asection *rsec;
8416 struct elf_link_hash_entry *h;
8418 r_symndx = rel->r_info >> r_sym_shift;
8419 if (r_symndx == 0)
8420 continue;
8422 if (r_symndx >= nlocsyms
8423 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
8425 h = sym_hashes[r_symndx - extsymoff];
8426 while (h->root.type == bfd_link_hash_indirect
8427 || h->root.type == bfd_link_hash_warning)
8428 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8429 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
8431 else
8433 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
8436 if (rsec && !rsec->gc_mark)
8438 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
8439 rsec->gc_mark = 1;
8440 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
8442 ret = FALSE;
8443 goto out2;
8448 out2:
8449 if (elf_section_data (sec)->relocs != relstart)
8450 free (relstart);
8451 out1:
8452 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
8454 if (! info->keep_memory)
8455 free (isym);
8456 else
8457 symtab_hdr->contents = (unsigned char *) isym;
8461 return ret;
8464 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8466 static bfd_boolean
8467 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *idxptr)
8469 int *idx = idxptr;
8471 if (h->root.type == bfd_link_hash_warning)
8472 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8474 if (h->dynindx != -1
8475 && ((h->root.type != bfd_link_hash_defined
8476 && h->root.type != bfd_link_hash_defweak)
8477 || h->root.u.def.section->gc_mark))
8478 h->dynindx = (*idx)++;
8480 return TRUE;
8483 /* The sweep phase of garbage collection. Remove all garbage sections. */
8485 typedef bfd_boolean (*gc_sweep_hook_fn)
8486 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
8488 static bfd_boolean
8489 elf_gc_sweep (struct bfd_link_info *info, gc_sweep_hook_fn gc_sweep_hook)
8491 bfd *sub;
8493 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8495 asection *o;
8497 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
8498 continue;
8500 for (o = sub->sections; o != NULL; o = o->next)
8502 /* Keep debug and special sections. */
8503 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
8504 || (o->flags & (SEC_ALLOC | SEC_LOAD)) == 0)
8505 o->gc_mark = 1;
8507 if (o->gc_mark)
8508 continue;
8510 /* Skip sweeping sections already excluded. */
8511 if (o->flags & SEC_EXCLUDE)
8512 continue;
8514 /* Since this is early in the link process, it is simple
8515 to remove a section from the output. */
8516 o->flags |= SEC_EXCLUDE;
8518 /* But we also have to update some of the relocation
8519 info we collected before. */
8520 if (gc_sweep_hook
8521 && (o->flags & SEC_RELOC) && o->reloc_count > 0)
8523 Elf_Internal_Rela *internal_relocs;
8524 bfd_boolean r;
8526 internal_relocs
8527 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
8528 info->keep_memory);
8529 if (internal_relocs == NULL)
8530 return FALSE;
8532 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
8534 if (elf_section_data (o)->relocs != internal_relocs)
8535 free (internal_relocs);
8537 if (!r)
8538 return FALSE;
8543 /* Remove the symbols that were in the swept sections from the dynamic
8544 symbol table. GCFIXME: Anyone know how to get them out of the
8545 static symbol table as well? */
8547 int i = 0;
8549 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, &i);
8551 elf_hash_table (info)->dynsymcount = i;
8554 return TRUE;
8557 /* Propagate collected vtable information. This is called through
8558 elf_link_hash_traverse. */
8560 static bfd_boolean
8561 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
8563 if (h->root.type == bfd_link_hash_warning)
8564 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8566 /* Those that are not vtables. */
8567 if (h->vtable == NULL || h->vtable->parent == NULL)
8568 return TRUE;
8570 /* Those vtables that do not have parents, we cannot merge. */
8571 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
8572 return TRUE;
8574 /* If we've already been done, exit. */
8575 if (h->vtable->used && h->vtable->used[-1])
8576 return TRUE;
8578 /* Make sure the parent's table is up to date. */
8579 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
8581 if (h->vtable->used == NULL)
8583 /* None of this table's entries were referenced. Re-use the
8584 parent's table. */
8585 h->vtable->used = h->vtable->parent->vtable->used;
8586 h->vtable->size = h->vtable->parent->vtable->size;
8588 else
8590 size_t n;
8591 bfd_boolean *cu, *pu;
8593 /* Or the parent's entries into ours. */
8594 cu = h->vtable->used;
8595 cu[-1] = TRUE;
8596 pu = h->vtable->parent->vtable->used;
8597 if (pu != NULL)
8599 const struct elf_backend_data *bed;
8600 unsigned int log_file_align;
8602 bed = get_elf_backend_data (h->root.u.def.section->owner);
8603 log_file_align = bed->s->log_file_align;
8604 n = h->vtable->parent->vtable->size >> log_file_align;
8605 while (n--)
8607 if (*pu)
8608 *cu = TRUE;
8609 pu++;
8610 cu++;
8615 return TRUE;
8618 static bfd_boolean
8619 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
8621 asection *sec;
8622 bfd_vma hstart, hend;
8623 Elf_Internal_Rela *relstart, *relend, *rel;
8624 const struct elf_backend_data *bed;
8625 unsigned int log_file_align;
8627 if (h->root.type == bfd_link_hash_warning)
8628 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8630 /* Take care of both those symbols that do not describe vtables as
8631 well as those that are not loaded. */
8632 if (h->vtable == NULL || h->vtable->parent == NULL)
8633 return TRUE;
8635 BFD_ASSERT (h->root.type == bfd_link_hash_defined
8636 || h->root.type == bfd_link_hash_defweak);
8638 sec = h->root.u.def.section;
8639 hstart = h->root.u.def.value;
8640 hend = hstart + h->size;
8642 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
8643 if (!relstart)
8644 return *(bfd_boolean *) okp = FALSE;
8645 bed = get_elf_backend_data (sec->owner);
8646 log_file_align = bed->s->log_file_align;
8648 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
8650 for (rel = relstart; rel < relend; ++rel)
8651 if (rel->r_offset >= hstart && rel->r_offset < hend)
8653 /* If the entry is in use, do nothing. */
8654 if (h->vtable->used
8655 && (rel->r_offset - hstart) < h->vtable->size)
8657 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
8658 if (h->vtable->used[entry])
8659 continue;
8661 /* Otherwise, kill it. */
8662 rel->r_offset = rel->r_info = rel->r_addend = 0;
8665 return TRUE;
8668 /* Mark sections containing dynamically referenced symbols. This is called
8669 through elf_link_hash_traverse. */
8671 static bfd_boolean
8672 elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h,
8673 void *okp ATTRIBUTE_UNUSED)
8675 if (h->root.type == bfd_link_hash_warning)
8676 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8678 if ((h->root.type == bfd_link_hash_defined
8679 || h->root.type == bfd_link_hash_defweak)
8680 && h->ref_dynamic)
8681 h->root.u.def.section->flags |= SEC_KEEP;
8683 return TRUE;
8686 /* Do mark and sweep of unused sections. */
8688 bfd_boolean
8689 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
8691 bfd_boolean ok = TRUE;
8692 bfd *sub;
8693 asection * (*gc_mark_hook)
8694 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
8695 struct elf_link_hash_entry *h, Elf_Internal_Sym *);
8697 if (!get_elf_backend_data (abfd)->can_gc_sections
8698 || info->relocatable
8699 || info->emitrelocations
8700 || info->shared
8701 || !is_elf_hash_table (info->hash))
8703 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
8704 return TRUE;
8707 /* Apply transitive closure to the vtable entry usage info. */
8708 elf_link_hash_traverse (elf_hash_table (info),
8709 elf_gc_propagate_vtable_entries_used,
8710 &ok);
8711 if (!ok)
8712 return FALSE;
8714 /* Kill the vtable relocations that were not used. */
8715 elf_link_hash_traverse (elf_hash_table (info),
8716 elf_gc_smash_unused_vtentry_relocs,
8717 &ok);
8718 if (!ok)
8719 return FALSE;
8721 /* Mark dynamically referenced symbols. */
8722 if (elf_hash_table (info)->dynamic_sections_created)
8723 elf_link_hash_traverse (elf_hash_table (info),
8724 elf_gc_mark_dynamic_ref_symbol,
8725 &ok);
8726 if (!ok)
8727 return FALSE;
8729 /* Grovel through relocs to find out who stays ... */
8730 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
8731 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8733 asection *o;
8735 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
8736 continue;
8738 for (o = sub->sections; o != NULL; o = o->next)
8740 if (o->flags & SEC_KEEP)
8742 /* _bfd_elf_discard_section_eh_frame knows how to discard
8743 orphaned FDEs so don't mark sections referenced by the
8744 EH frame section. */
8745 if (strcmp (o->name, ".eh_frame") == 0)
8746 o->gc_mark = 1;
8747 else if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
8748 return FALSE;
8753 /* ... and mark SEC_EXCLUDE for those that go. */
8754 if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook))
8755 return FALSE;
8757 return TRUE;
8760 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
8762 bfd_boolean
8763 bfd_elf_gc_record_vtinherit (bfd *abfd,
8764 asection *sec,
8765 struct elf_link_hash_entry *h,
8766 bfd_vma offset)
8768 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
8769 struct elf_link_hash_entry **search, *child;
8770 bfd_size_type extsymcount;
8771 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8773 /* The sh_info field of the symtab header tells us where the
8774 external symbols start. We don't care about the local symbols at
8775 this point. */
8776 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
8777 if (!elf_bad_symtab (abfd))
8778 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
8780 sym_hashes = elf_sym_hashes (abfd);
8781 sym_hashes_end = sym_hashes + extsymcount;
8783 /* Hunt down the child symbol, which is in this section at the same
8784 offset as the relocation. */
8785 for (search = sym_hashes; search != sym_hashes_end; ++search)
8787 if ((child = *search) != NULL
8788 && (child->root.type == bfd_link_hash_defined
8789 || child->root.type == bfd_link_hash_defweak)
8790 && child->root.u.def.section == sec
8791 && child->root.u.def.value == offset)
8792 goto win;
8795 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
8796 abfd, sec, (unsigned long) offset);
8797 bfd_set_error (bfd_error_invalid_operation);
8798 return FALSE;
8800 win:
8801 if (!child->vtable)
8803 child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable));
8804 if (!child->vtable)
8805 return FALSE;
8807 if (!h)
8809 /* This *should* only be the absolute section. It could potentially
8810 be that someone has defined a non-global vtable though, which
8811 would be bad. It isn't worth paging in the local symbols to be
8812 sure though; that case should simply be handled by the assembler. */
8814 child->vtable->parent = (struct elf_link_hash_entry *) -1;
8816 else
8817 child->vtable->parent = h;
8819 return TRUE;
8822 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
8824 bfd_boolean
8825 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
8826 asection *sec ATTRIBUTE_UNUSED,
8827 struct elf_link_hash_entry *h,
8828 bfd_vma addend)
8830 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8831 unsigned int log_file_align = bed->s->log_file_align;
8833 if (!h->vtable)
8835 h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable));
8836 if (!h->vtable)
8837 return FALSE;
8840 if (addend >= h->vtable->size)
8842 size_t size, bytes, file_align;
8843 bfd_boolean *ptr = h->vtable->used;
8845 /* While the symbol is undefined, we have to be prepared to handle
8846 a zero size. */
8847 file_align = 1 << log_file_align;
8848 if (h->root.type == bfd_link_hash_undefined)
8849 size = addend + file_align;
8850 else
8852 size = h->size;
8853 if (addend >= size)
8855 /* Oops! We've got a reference past the defined end of
8856 the table. This is probably a bug -- shall we warn? */
8857 size = addend + file_align;
8860 size = (size + file_align - 1) & -file_align;
8862 /* Allocate one extra entry for use as a "done" flag for the
8863 consolidation pass. */
8864 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
8866 if (ptr)
8868 ptr = bfd_realloc (ptr - 1, bytes);
8870 if (ptr != NULL)
8872 size_t oldbytes;
8874 oldbytes = (((h->vtable->size >> log_file_align) + 1)
8875 * sizeof (bfd_boolean));
8876 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
8879 else
8880 ptr = bfd_zmalloc (bytes);
8882 if (ptr == NULL)
8883 return FALSE;
8885 /* And arrange for that done flag to be at index -1. */
8886 h->vtable->used = ptr + 1;
8887 h->vtable->size = size;
8890 h->vtable->used[addend >> log_file_align] = TRUE;
8892 return TRUE;
8895 struct alloc_got_off_arg {
8896 bfd_vma gotoff;
8897 unsigned int got_elt_size;
8900 /* We need a special top-level link routine to convert got reference counts
8901 to real got offsets. */
8903 static bfd_boolean
8904 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
8906 struct alloc_got_off_arg *gofarg = arg;
8908 if (h->root.type == bfd_link_hash_warning)
8909 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8911 if (h->got.refcount > 0)
8913 h->got.offset = gofarg->gotoff;
8914 gofarg->gotoff += gofarg->got_elt_size;
8916 else
8917 h->got.offset = (bfd_vma) -1;
8919 return TRUE;
8922 /* And an accompanying bit to work out final got entry offsets once
8923 we're done. Should be called from final_link. */
8925 bfd_boolean
8926 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
8927 struct bfd_link_info *info)
8929 bfd *i;
8930 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8931 bfd_vma gotoff;
8932 unsigned int got_elt_size = bed->s->arch_size / 8;
8933 struct alloc_got_off_arg gofarg;
8935 if (! is_elf_hash_table (info->hash))
8936 return FALSE;
8938 /* The GOT offset is relative to the .got section, but the GOT header is
8939 put into the .got.plt section, if the backend uses it. */
8940 if (bed->want_got_plt)
8941 gotoff = 0;
8942 else
8943 gotoff = bed->got_header_size;
8945 /* Do the local .got entries first. */
8946 for (i = info->input_bfds; i; i = i->link_next)
8948 bfd_signed_vma *local_got;
8949 bfd_size_type j, locsymcount;
8950 Elf_Internal_Shdr *symtab_hdr;
8952 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
8953 continue;
8955 local_got = elf_local_got_refcounts (i);
8956 if (!local_got)
8957 continue;
8959 symtab_hdr = &elf_tdata (i)->symtab_hdr;
8960 if (elf_bad_symtab (i))
8961 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
8962 else
8963 locsymcount = symtab_hdr->sh_info;
8965 for (j = 0; j < locsymcount; ++j)
8967 if (local_got[j] > 0)
8969 local_got[j] = gotoff;
8970 gotoff += got_elt_size;
8972 else
8973 local_got[j] = (bfd_vma) -1;
8977 /* Then the global .got entries. .plt refcounts are handled by
8978 adjust_dynamic_symbol */
8979 gofarg.gotoff = gotoff;
8980 gofarg.got_elt_size = got_elt_size;
8981 elf_link_hash_traverse (elf_hash_table (info),
8982 elf_gc_allocate_got_offsets,
8983 &gofarg);
8984 return TRUE;
8987 /* Many folk need no more in the way of final link than this, once
8988 got entry reference counting is enabled. */
8990 bfd_boolean
8991 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
8993 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
8994 return FALSE;
8996 /* Invoke the regular ELF backend linker to do all the work. */
8997 return bfd_elf_final_link (abfd, info);
9000 bfd_boolean
9001 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
9003 struct elf_reloc_cookie *rcookie = cookie;
9005 if (rcookie->bad_symtab)
9006 rcookie->rel = rcookie->rels;
9008 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
9010 unsigned long r_symndx;
9012 if (! rcookie->bad_symtab)
9013 if (rcookie->rel->r_offset > offset)
9014 return FALSE;
9015 if (rcookie->rel->r_offset != offset)
9016 continue;
9018 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
9019 if (r_symndx == SHN_UNDEF)
9020 return TRUE;
9022 if (r_symndx >= rcookie->locsymcount
9023 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
9025 struct elf_link_hash_entry *h;
9027 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
9029 while (h->root.type == bfd_link_hash_indirect
9030 || h->root.type == bfd_link_hash_warning)
9031 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9033 if ((h->root.type == bfd_link_hash_defined
9034 || h->root.type == bfd_link_hash_defweak)
9035 && elf_discarded_section (h->root.u.def.section))
9036 return TRUE;
9037 else
9038 return FALSE;
9040 else
9042 /* It's not a relocation against a global symbol,
9043 but it could be a relocation against a local
9044 symbol for a discarded section. */
9045 asection *isec;
9046 Elf_Internal_Sym *isym;
9048 /* Need to: get the symbol; get the section. */
9049 isym = &rcookie->locsyms[r_symndx];
9050 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
9052 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
9053 if (isec != NULL && elf_discarded_section (isec))
9054 return TRUE;
9057 return FALSE;
9059 return FALSE;
9062 /* Discard unneeded references to discarded sections.
9063 Returns TRUE if any section's size was changed. */
9064 /* This function assumes that the relocations are in sorted order,
9065 which is true for all known assemblers. */
9067 bfd_boolean
9068 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
9070 struct elf_reloc_cookie cookie;
9071 asection *stab, *eh;
9072 Elf_Internal_Shdr *symtab_hdr;
9073 const struct elf_backend_data *bed;
9074 bfd *abfd;
9075 unsigned int count;
9076 bfd_boolean ret = FALSE;
9078 if (info->traditional_format
9079 || !is_elf_hash_table (info->hash))
9080 return FALSE;
9082 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
9084 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
9085 continue;
9087 bed = get_elf_backend_data (abfd);
9089 if ((abfd->flags & DYNAMIC) != 0)
9090 continue;
9092 eh = bfd_get_section_by_name (abfd, ".eh_frame");
9093 if (info->relocatable
9094 || (eh != NULL
9095 && (eh->size == 0
9096 || bfd_is_abs_section (eh->output_section))))
9097 eh = NULL;
9099 stab = bfd_get_section_by_name (abfd, ".stab");
9100 if (stab != NULL
9101 && (stab->size == 0
9102 || bfd_is_abs_section (stab->output_section)
9103 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
9104 stab = NULL;
9106 if (stab == NULL
9107 && eh == NULL
9108 && bed->elf_backend_discard_info == NULL)
9109 continue;
9111 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9112 cookie.abfd = abfd;
9113 cookie.sym_hashes = elf_sym_hashes (abfd);
9114 cookie.bad_symtab = elf_bad_symtab (abfd);
9115 if (cookie.bad_symtab)
9117 cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9118 cookie.extsymoff = 0;
9120 else
9122 cookie.locsymcount = symtab_hdr->sh_info;
9123 cookie.extsymoff = symtab_hdr->sh_info;
9126 if (bed->s->arch_size == 32)
9127 cookie.r_sym_shift = 8;
9128 else
9129 cookie.r_sym_shift = 32;
9131 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
9132 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
9134 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9135 cookie.locsymcount, 0,
9136 NULL, NULL, NULL);
9137 if (cookie.locsyms == NULL)
9138 return FALSE;
9141 if (stab != NULL)
9143 cookie.rels = NULL;
9144 count = stab->reloc_count;
9145 if (count != 0)
9146 cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
9147 info->keep_memory);
9148 if (cookie.rels != NULL)
9150 cookie.rel = cookie.rels;
9151 cookie.relend = cookie.rels;
9152 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9153 if (_bfd_discard_section_stabs (abfd, stab,
9154 elf_section_data (stab)->sec_info,
9155 bfd_elf_reloc_symbol_deleted_p,
9156 &cookie))
9157 ret = TRUE;
9158 if (elf_section_data (stab)->relocs != cookie.rels)
9159 free (cookie.rels);
9163 if (eh != NULL)
9165 cookie.rels = NULL;
9166 count = eh->reloc_count;
9167 if (count != 0)
9168 cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
9169 info->keep_memory);
9170 cookie.rel = cookie.rels;
9171 cookie.relend = cookie.rels;
9172 if (cookie.rels != NULL)
9173 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9175 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
9176 bfd_elf_reloc_symbol_deleted_p,
9177 &cookie))
9178 ret = TRUE;
9180 if (cookie.rels != NULL
9181 && elf_section_data (eh)->relocs != cookie.rels)
9182 free (cookie.rels);
9185 if (bed->elf_backend_discard_info != NULL
9186 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
9187 ret = TRUE;
9189 if (cookie.locsyms != NULL
9190 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
9192 if (! info->keep_memory)
9193 free (cookie.locsyms);
9194 else
9195 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
9199 if (info->eh_frame_hdr
9200 && !info->relocatable
9201 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
9202 ret = TRUE;
9204 return ret;
9207 struct already_linked_section
9209 asection *sec;
9210 asection *linked;
9213 /* Check if the member of a single member comdat group matches a
9214 linkonce section and vice versa. */
9215 static bfd_boolean
9216 try_match_symbols_in_sections
9217 (struct bfd_section_already_linked_hash_entry *h, void *info)
9219 struct bfd_section_already_linked *l;
9220 struct already_linked_section *s
9221 = (struct already_linked_section *) info;
9223 if (elf_sec_group (s->sec) == NULL)
9225 /* It is a linkonce section. Try to match it with the member of a
9226 single member comdat group. */
9227 for (l = h->entry; l != NULL; l = l->next)
9228 if ((l->sec->flags & SEC_GROUP))
9230 asection *first = elf_next_in_group (l->sec);
9232 if (first != NULL
9233 && elf_next_in_group (first) == first
9234 && bfd_elf_match_symbols_in_sections (first, s->sec))
9236 s->linked = first;
9237 return FALSE;
9241 else
9243 /* It is the member of a single member comdat group. Try to match
9244 it with a linkonce section. */
9245 for (l = h->entry; l != NULL; l = l->next)
9246 if ((l->sec->flags & SEC_GROUP) == 0
9247 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
9248 && bfd_elf_match_symbols_in_sections (l->sec, s->sec))
9250 s->linked = l->sec;
9251 return FALSE;
9255 return TRUE;
9258 static bfd_boolean
9259 already_linked (asection *sec, asection *group)
9261 struct already_linked_section result;
9263 result.sec = sec;
9264 result.linked = NULL;
9266 bfd_section_already_linked_table_traverse
9267 (try_match_symbols_in_sections, &result);
9269 if (result.linked)
9271 sec->output_section = bfd_abs_section_ptr;
9272 sec->kept_section = result.linked;
9274 /* Also discard the group section. */
9275 if (group)
9276 group->output_section = bfd_abs_section_ptr;
9278 return TRUE;
9281 return FALSE;
9284 void
9285 _bfd_elf_section_already_linked (bfd *abfd, struct bfd_section * sec)
9287 flagword flags;
9288 const char *name;
9289 struct bfd_section_already_linked *l;
9290 struct bfd_section_already_linked_hash_entry *already_linked_list;
9291 asection *group;
9293 /* A single member comdat group section may be discarded by a
9294 linkonce section. See below. */
9295 if (sec->output_section == bfd_abs_section_ptr)
9296 return;
9298 flags = sec->flags;
9300 /* Check if it belongs to a section group. */
9301 group = elf_sec_group (sec);
9303 /* Return if it isn't a linkonce section nor a member of a group. A
9304 comdat group section also has SEC_LINK_ONCE set. */
9305 if ((flags & SEC_LINK_ONCE) == 0 && group == NULL)
9306 return;
9308 if (group)
9310 /* If this is the member of a single member comdat group, check if
9311 the group should be discarded. */
9312 if (elf_next_in_group (sec) == sec
9313 && (group->flags & SEC_LINK_ONCE) != 0)
9314 sec = group;
9315 else
9316 return;
9319 /* FIXME: When doing a relocatable link, we may have trouble
9320 copying relocations in other sections that refer to local symbols
9321 in the section being discarded. Those relocations will have to
9322 be converted somehow; as of this writing I'm not sure that any of
9323 the backends handle that correctly.
9325 It is tempting to instead not discard link once sections when
9326 doing a relocatable link (technically, they should be discarded
9327 whenever we are building constructors). However, that fails,
9328 because the linker winds up combining all the link once sections
9329 into a single large link once section, which defeats the purpose
9330 of having link once sections in the first place.
9332 Also, not merging link once sections in a relocatable link
9333 causes trouble for MIPS ELF, which relies on link once semantics
9334 to handle the .reginfo section correctly. */
9336 name = bfd_get_section_name (abfd, sec);
9338 already_linked_list = bfd_section_already_linked_table_lookup (name);
9340 for (l = already_linked_list->entry; l != NULL; l = l->next)
9342 /* We may have 3 different sections on the list: group section,
9343 comdat section and linkonce section. SEC may be a linkonce or
9344 group section. We match a group section with a group section,
9345 a linkonce section with a linkonce section, and ignore comdat
9346 section. */
9347 if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
9348 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
9350 /* The section has already been linked. See if we should
9351 issue a warning. */
9352 switch (flags & SEC_LINK_DUPLICATES)
9354 default:
9355 abort ();
9357 case SEC_LINK_DUPLICATES_DISCARD:
9358 break;
9360 case SEC_LINK_DUPLICATES_ONE_ONLY:
9361 (*_bfd_error_handler)
9362 (_("%B: ignoring duplicate section `%A'\n"),
9363 abfd, sec);
9364 break;
9366 case SEC_LINK_DUPLICATES_SAME_SIZE:
9367 if (sec->size != l->sec->size)
9368 (*_bfd_error_handler)
9369 (_("%B: duplicate section `%A' has different size\n"),
9370 abfd, sec);
9371 break;
9373 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
9374 if (sec->size != l->sec->size)
9375 (*_bfd_error_handler)
9376 (_("%B: duplicate section `%A' has different size\n"),
9377 abfd, sec);
9378 else if (sec->size != 0)
9380 bfd_byte *sec_contents, *l_sec_contents;
9382 if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
9383 (*_bfd_error_handler)
9384 (_("%B: warning: could not read contents of section `%A'\n"),
9385 abfd, sec);
9386 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
9387 &l_sec_contents))
9388 (*_bfd_error_handler)
9389 (_("%B: warning: could not read contents of section `%A'\n"),
9390 l->sec->owner, l->sec);
9391 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
9392 (*_bfd_error_handler)
9393 (_("%B: warning: duplicate section `%A' has different contents\n"),
9394 abfd, sec);
9396 if (sec_contents)
9397 free (sec_contents);
9398 if (l_sec_contents)
9399 free (l_sec_contents);
9401 break;
9404 /* Set the output_section field so that lang_add_section
9405 does not create a lang_input_section structure for this
9406 section. Since there might be a symbol in the section
9407 being discarded, we must retain a pointer to the section
9408 which we are really going to use. */
9409 sec->output_section = bfd_abs_section_ptr;
9410 sec->kept_section = l->sec;
9412 if (flags & SEC_GROUP)
9414 asection *first = elf_next_in_group (sec);
9415 asection *s = first;
9417 while (s != NULL)
9419 s->output_section = bfd_abs_section_ptr;
9420 /* Record which group discards it. */
9421 s->kept_section = l->sec;
9422 s = elf_next_in_group (s);
9423 /* These lists are circular. */
9424 if (s == first)
9425 break;
9429 return;
9433 if (group)
9435 /* If this is the member of a single member comdat group and the
9436 group hasn't be discarded, we check if it matches a linkonce
9437 section. We only record the discarded comdat group. Otherwise
9438 the undiscarded group will be discarded incorrectly later since
9439 itself has been recorded. */
9440 if (! already_linked (elf_next_in_group (sec), group))
9441 return;
9443 else
9444 /* There is no direct match. But for linkonce section, we should
9445 check if there is a match with comdat group member. We always
9446 record the linkonce section, discarded or not. */
9447 already_linked (sec, group);
9449 /* This is the first section with this name. Record it. */
9450 bfd_section_already_linked_table_insert (already_linked_list, sec);