* Makefile.am: Run "make dep-am".
[binutils.git] / bfd / elflink.c
blob93323664657e1cbd93cafbff4b095b500ca411de
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->weakdef != NULL
476 && h->weakdef->dynindx == -1)
478 if (! bfd_elf_link_record_dynamic_symbol (info, h->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.und_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 /* FIXME: What if the new symbol is weak undefined? */
904 h->root.u.undef.abfd = abfd;
906 else
908 h->root.type = bfd_link_hash_new;
909 h->root.u.undef.abfd = NULL;
912 if (h->def_dynamic)
914 h->def_dynamic = 0;
915 h->ref_dynamic = 1;
916 h->dynamic_def = 1;
918 /* FIXME: Should we check type and size for protected symbol? */
919 h->size = 0;
920 h->type = 0;
921 return TRUE;
924 /* Differentiate strong and weak symbols. */
925 newweak = bind == STB_WEAK;
926 oldweak = (h->root.type == bfd_link_hash_defweak
927 || h->root.type == bfd_link_hash_undefweak);
929 /* If a new weak symbol definition comes from a regular file and the
930 old symbol comes from a dynamic library, we treat the new one as
931 strong. Similarly, an old weak symbol definition from a regular
932 file is treated as strong when the new symbol comes from a dynamic
933 library. Further, an old weak symbol from a dynamic library is
934 treated as strong if the new symbol is from a dynamic library.
935 This reflects the way glibc's ld.so works.
937 Do this before setting *type_change_ok or *size_change_ok so that
938 we warn properly when dynamic library symbols are overridden. */
940 if (newdef && !newdyn && olddyn)
941 newweak = FALSE;
942 if (olddef && newdyn)
943 oldweak = FALSE;
945 /* It's OK to change the type if either the existing symbol or the
946 new symbol is weak. A type change is also OK if the old symbol
947 is undefined and the new symbol is defined. */
949 if (oldweak
950 || newweak
951 || (newdef
952 && h->root.type == bfd_link_hash_undefined))
953 *type_change_ok = TRUE;
955 /* It's OK to change the size if either the existing symbol or the
956 new symbol is weak, or if the old symbol is undefined. */
958 if (*type_change_ok
959 || h->root.type == bfd_link_hash_undefined)
960 *size_change_ok = TRUE;
962 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
963 symbol, respectively, appears to be a common symbol in a dynamic
964 object. If a symbol appears in an uninitialized section, and is
965 not weak, and is not a function, then it may be a common symbol
966 which was resolved when the dynamic object was created. We want
967 to treat such symbols specially, because they raise special
968 considerations when setting the symbol size: if the symbol
969 appears as a common symbol in a regular object, and the size in
970 the regular object is larger, we must make sure that we use the
971 larger size. This problematic case can always be avoided in C,
972 but it must be handled correctly when using Fortran shared
973 libraries.
975 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
976 likewise for OLDDYNCOMMON and OLDDEF.
978 Note that this test is just a heuristic, and that it is quite
979 possible to have an uninitialized symbol in a shared object which
980 is really a definition, rather than a common symbol. This could
981 lead to some minor confusion when the symbol really is a common
982 symbol in some regular object. However, I think it will be
983 harmless. */
985 if (newdyn
986 && newdef
987 && !newweak
988 && (sec->flags & SEC_ALLOC) != 0
989 && (sec->flags & SEC_LOAD) == 0
990 && sym->st_size > 0
991 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
992 newdyncommon = TRUE;
993 else
994 newdyncommon = FALSE;
996 if (olddyn
997 && olddef
998 && h->root.type == bfd_link_hash_defined
999 && h->def_dynamic
1000 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1001 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1002 && h->size > 0
1003 && h->type != STT_FUNC)
1004 olddyncommon = TRUE;
1005 else
1006 olddyncommon = FALSE;
1008 /* If both the old and the new symbols look like common symbols in a
1009 dynamic object, set the size of the symbol to the larger of the
1010 two. */
1012 if (olddyncommon
1013 && newdyncommon
1014 && sym->st_size != h->size)
1016 /* Since we think we have two common symbols, issue a multiple
1017 common warning if desired. Note that we only warn if the
1018 size is different. If the size is the same, we simply let
1019 the old symbol override the new one as normally happens with
1020 symbols defined in dynamic objects. */
1022 if (! ((*info->callbacks->multiple_common)
1023 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1024 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1025 return FALSE;
1027 if (sym->st_size > h->size)
1028 h->size = sym->st_size;
1030 *size_change_ok = TRUE;
1033 /* If we are looking at a dynamic object, and we have found a
1034 definition, we need to see if the symbol was already defined by
1035 some other object. If so, we want to use the existing
1036 definition, and we do not want to report a multiple symbol
1037 definition error; we do this by clobbering *PSEC to be
1038 bfd_und_section_ptr.
1040 We treat a common symbol as a definition if the symbol in the
1041 shared library is a function, since common symbols always
1042 represent variables; this can cause confusion in principle, but
1043 any such confusion would seem to indicate an erroneous program or
1044 shared library. We also permit a common symbol in a regular
1045 object to override a weak symbol in a shared object. */
1047 if (newdyn
1048 && newdef
1049 && (olddef
1050 || (h->root.type == bfd_link_hash_common
1051 && (newweak
1052 || ELF_ST_TYPE (sym->st_info) == STT_FUNC))))
1054 *override = TRUE;
1055 newdef = FALSE;
1056 newdyncommon = FALSE;
1058 *psec = sec = bfd_und_section_ptr;
1059 *size_change_ok = TRUE;
1061 /* If we get here when the old symbol is a common symbol, then
1062 we are explicitly letting it override a weak symbol or
1063 function in a dynamic object, and we don't want to warn about
1064 a type change. If the old symbol is a defined symbol, a type
1065 change warning may still be appropriate. */
1067 if (h->root.type == bfd_link_hash_common)
1068 *type_change_ok = TRUE;
1071 /* Handle the special case of an old common symbol merging with a
1072 new symbol which looks like a common symbol in a shared object.
1073 We change *PSEC and *PVALUE to make the new symbol look like a
1074 common symbol, and let _bfd_generic_link_add_one_symbol will do
1075 the right thing. */
1077 if (newdyncommon
1078 && h->root.type == bfd_link_hash_common)
1080 *override = TRUE;
1081 newdef = FALSE;
1082 newdyncommon = FALSE;
1083 *pvalue = sym->st_size;
1084 *psec = sec = bfd_com_section_ptr;
1085 *size_change_ok = TRUE;
1088 /* If the old symbol is from a dynamic object, and the new symbol is
1089 a definition which is not from a dynamic object, then the new
1090 symbol overrides the old symbol. Symbols from regular files
1091 always take precedence over symbols from dynamic objects, even if
1092 they are defined after the dynamic object in the link.
1094 As above, we again permit a common symbol in a regular object to
1095 override a definition in a shared object if the shared object
1096 symbol is a function or is weak. */
1098 flip = NULL;
1099 if (! newdyn
1100 && (newdef
1101 || (bfd_is_com_section (sec)
1102 && (oldweak
1103 || h->type == STT_FUNC)))
1104 && olddyn
1105 && olddef
1106 && h->def_dynamic)
1108 /* Change the hash table entry to undefined, and let
1109 _bfd_generic_link_add_one_symbol do the right thing with the
1110 new definition. */
1112 h->root.type = bfd_link_hash_undefined;
1113 h->root.u.undef.abfd = h->root.u.def.section->owner;
1114 *size_change_ok = TRUE;
1116 olddef = FALSE;
1117 olddyncommon = FALSE;
1119 /* We again permit a type change when a common symbol may be
1120 overriding a function. */
1122 if (bfd_is_com_section (sec))
1123 *type_change_ok = TRUE;
1125 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1126 flip = *sym_hash;
1127 else
1128 /* This union may have been set to be non-NULL when this symbol
1129 was seen in a dynamic object. We must force the union to be
1130 NULL, so that it is correct for a regular symbol. */
1131 h->verinfo.vertree = NULL;
1134 /* Handle the special case of a new common symbol merging with an
1135 old symbol that looks like it might be a common symbol defined in
1136 a shared object. Note that we have already handled the case in
1137 which a new common symbol should simply override the definition
1138 in the shared library. */
1140 if (! newdyn
1141 && bfd_is_com_section (sec)
1142 && olddyncommon)
1144 /* It would be best if we could set the hash table entry to a
1145 common symbol, but we don't know what to use for the section
1146 or the alignment. */
1147 if (! ((*info->callbacks->multiple_common)
1148 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1149 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1150 return FALSE;
1152 /* If the presumed common symbol in the dynamic object is
1153 larger, pretend that the new symbol has its size. */
1155 if (h->size > *pvalue)
1156 *pvalue = h->size;
1158 /* FIXME: We no longer know the alignment required by the symbol
1159 in the dynamic object, so we just wind up using the one from
1160 the regular object. */
1162 olddef = FALSE;
1163 olddyncommon = FALSE;
1165 h->root.type = bfd_link_hash_undefined;
1166 h->root.u.undef.abfd = h->root.u.def.section->owner;
1168 *size_change_ok = TRUE;
1169 *type_change_ok = TRUE;
1171 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1172 flip = *sym_hash;
1173 else
1174 h->verinfo.vertree = NULL;
1177 if (flip != NULL)
1179 /* Handle the case where we had a versioned symbol in a dynamic
1180 library and now find a definition in a normal object. In this
1181 case, we make the versioned symbol point to the normal one. */
1182 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1183 flip->root.type = h->root.type;
1184 h->root.type = bfd_link_hash_indirect;
1185 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1186 (*bed->elf_backend_copy_indirect_symbol) (bed, flip, h);
1187 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1188 if (h->def_dynamic)
1190 h->def_dynamic = 0;
1191 flip->ref_dynamic = 1;
1195 return TRUE;
1198 /* This function is called to create an indirect symbol from the
1199 default for the symbol with the default version if needed. The
1200 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1201 set DYNSYM if the new indirect symbol is dynamic. */
1203 bfd_boolean
1204 _bfd_elf_add_default_symbol (bfd *abfd,
1205 struct bfd_link_info *info,
1206 struct elf_link_hash_entry *h,
1207 const char *name,
1208 Elf_Internal_Sym *sym,
1209 asection **psec,
1210 bfd_vma *value,
1211 bfd_boolean *dynsym,
1212 bfd_boolean override)
1214 bfd_boolean type_change_ok;
1215 bfd_boolean size_change_ok;
1216 bfd_boolean skip;
1217 char *shortname;
1218 struct elf_link_hash_entry *hi;
1219 struct bfd_link_hash_entry *bh;
1220 const struct elf_backend_data *bed;
1221 bfd_boolean collect;
1222 bfd_boolean dynamic;
1223 char *p;
1224 size_t len, shortlen;
1225 asection *sec;
1227 /* If this symbol has a version, and it is the default version, we
1228 create an indirect symbol from the default name to the fully
1229 decorated name. This will cause external references which do not
1230 specify a version to be bound to this version of the symbol. */
1231 p = strchr (name, ELF_VER_CHR);
1232 if (p == NULL || p[1] != ELF_VER_CHR)
1233 return TRUE;
1235 if (override)
1237 /* We are overridden by an old definition. We need to check if we
1238 need to create the indirect symbol from the default name. */
1239 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1240 FALSE, FALSE);
1241 BFD_ASSERT (hi != NULL);
1242 if (hi == h)
1243 return TRUE;
1244 while (hi->root.type == bfd_link_hash_indirect
1245 || hi->root.type == bfd_link_hash_warning)
1247 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1248 if (hi == h)
1249 return TRUE;
1253 bed = get_elf_backend_data (abfd);
1254 collect = bed->collect;
1255 dynamic = (abfd->flags & DYNAMIC) != 0;
1257 shortlen = p - name;
1258 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1259 if (shortname == NULL)
1260 return FALSE;
1261 memcpy (shortname, name, shortlen);
1262 shortname[shortlen] = '\0';
1264 /* We are going to create a new symbol. Merge it with any existing
1265 symbol with this name. For the purposes of the merge, act as
1266 though we were defining the symbol we just defined, although we
1267 actually going to define an indirect symbol. */
1268 type_change_ok = FALSE;
1269 size_change_ok = FALSE;
1270 sec = *psec;
1271 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1272 &hi, &skip, &override, &type_change_ok,
1273 &size_change_ok))
1274 return FALSE;
1276 if (skip)
1277 goto nondefault;
1279 if (! override)
1281 bh = &hi->root;
1282 if (! (_bfd_generic_link_add_one_symbol
1283 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1284 0, name, FALSE, collect, &bh)))
1285 return FALSE;
1286 hi = (struct elf_link_hash_entry *) bh;
1288 else
1290 /* In this case the symbol named SHORTNAME is overriding the
1291 indirect symbol we want to add. We were planning on making
1292 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1293 is the name without a version. NAME is the fully versioned
1294 name, and it is the default version.
1296 Overriding means that we already saw a definition for the
1297 symbol SHORTNAME in a regular object, and it is overriding
1298 the symbol defined in the dynamic object.
1300 When this happens, we actually want to change NAME, the
1301 symbol we just added, to refer to SHORTNAME. This will cause
1302 references to NAME in the shared object to become references
1303 to SHORTNAME in the regular object. This is what we expect
1304 when we override a function in a shared object: that the
1305 references in the shared object will be mapped to the
1306 definition in the regular object. */
1308 while (hi->root.type == bfd_link_hash_indirect
1309 || hi->root.type == bfd_link_hash_warning)
1310 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1312 h->root.type = bfd_link_hash_indirect;
1313 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1314 if (h->def_dynamic)
1316 h->def_dynamic = 0;
1317 hi->ref_dynamic = 1;
1318 if (hi->ref_regular
1319 || hi->def_regular)
1321 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1322 return FALSE;
1326 /* Now set HI to H, so that the following code will set the
1327 other fields correctly. */
1328 hi = h;
1331 /* If there is a duplicate definition somewhere, then HI may not
1332 point to an indirect symbol. We will have reported an error to
1333 the user in that case. */
1335 if (hi->root.type == bfd_link_hash_indirect)
1337 struct elf_link_hash_entry *ht;
1339 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1340 (*bed->elf_backend_copy_indirect_symbol) (bed, ht, hi);
1342 /* See if the new flags lead us to realize that the symbol must
1343 be dynamic. */
1344 if (! *dynsym)
1346 if (! dynamic)
1348 if (info->shared
1349 || hi->ref_dynamic)
1350 *dynsym = TRUE;
1352 else
1354 if (hi->ref_regular)
1355 *dynsym = TRUE;
1360 /* We also need to define an indirection from the nondefault version
1361 of the symbol. */
1363 nondefault:
1364 len = strlen (name);
1365 shortname = bfd_hash_allocate (&info->hash->table, len);
1366 if (shortname == NULL)
1367 return FALSE;
1368 memcpy (shortname, name, shortlen);
1369 memcpy (shortname + shortlen, p + 1, len - shortlen);
1371 /* Once again, merge with any existing symbol. */
1372 type_change_ok = FALSE;
1373 size_change_ok = FALSE;
1374 sec = *psec;
1375 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1376 &hi, &skip, &override, &type_change_ok,
1377 &size_change_ok))
1378 return FALSE;
1380 if (skip)
1381 return TRUE;
1383 if (override)
1385 /* Here SHORTNAME is a versioned name, so we don't expect to see
1386 the type of override we do in the case above unless it is
1387 overridden by a versioned definition. */
1388 if (hi->root.type != bfd_link_hash_defined
1389 && hi->root.type != bfd_link_hash_defweak)
1390 (*_bfd_error_handler)
1391 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1392 abfd, shortname);
1394 else
1396 bh = &hi->root;
1397 if (! (_bfd_generic_link_add_one_symbol
1398 (info, abfd, shortname, BSF_INDIRECT,
1399 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1400 return FALSE;
1401 hi = (struct elf_link_hash_entry *) bh;
1403 /* If there is a duplicate definition somewhere, then HI may not
1404 point to an indirect symbol. We will have reported an error
1405 to the user in that case. */
1407 if (hi->root.type == bfd_link_hash_indirect)
1409 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
1411 /* See if the new flags lead us to realize that the symbol
1412 must be dynamic. */
1413 if (! *dynsym)
1415 if (! dynamic)
1417 if (info->shared
1418 || hi->ref_dynamic)
1419 *dynsym = TRUE;
1421 else
1423 if (hi->ref_regular)
1424 *dynsym = TRUE;
1430 return TRUE;
1433 /* This routine is used to export all defined symbols into the dynamic
1434 symbol table. It is called via elf_link_hash_traverse. */
1436 bfd_boolean
1437 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1439 struct elf_info_failed *eif = data;
1441 /* Ignore indirect symbols. These are added by the versioning code. */
1442 if (h->root.type == bfd_link_hash_indirect)
1443 return TRUE;
1445 if (h->root.type == bfd_link_hash_warning)
1446 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1448 if (h->dynindx == -1
1449 && (h->def_regular
1450 || h->ref_regular))
1452 struct bfd_elf_version_tree *t;
1453 struct bfd_elf_version_expr *d;
1455 for (t = eif->verdefs; t != NULL; t = t->next)
1457 if (t->globals.list != NULL)
1459 d = (*t->match) (&t->globals, NULL, h->root.root.string);
1460 if (d != NULL)
1461 goto doit;
1464 if (t->locals.list != NULL)
1466 d = (*t->match) (&t->locals, NULL, h->root.root.string);
1467 if (d != NULL)
1468 return TRUE;
1472 if (!eif->verdefs)
1474 doit:
1475 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1477 eif->failed = TRUE;
1478 return FALSE;
1483 return TRUE;
1486 /* Look through the symbols which are defined in other shared
1487 libraries and referenced here. Update the list of version
1488 dependencies. This will be put into the .gnu.version_r section.
1489 This function is called via elf_link_hash_traverse. */
1491 bfd_boolean
1492 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1493 void *data)
1495 struct elf_find_verdep_info *rinfo = data;
1496 Elf_Internal_Verneed *t;
1497 Elf_Internal_Vernaux *a;
1498 bfd_size_type amt;
1500 if (h->root.type == bfd_link_hash_warning)
1501 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1503 /* We only care about symbols defined in shared objects with version
1504 information. */
1505 if (!h->def_dynamic
1506 || h->def_regular
1507 || h->dynindx == -1
1508 || h->verinfo.verdef == NULL)
1509 return TRUE;
1511 /* See if we already know about this version. */
1512 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1514 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1515 continue;
1517 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1518 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1519 return TRUE;
1521 break;
1524 /* This is a new version. Add it to tree we are building. */
1526 if (t == NULL)
1528 amt = sizeof *t;
1529 t = bfd_zalloc (rinfo->output_bfd, amt);
1530 if (t == NULL)
1532 rinfo->failed = TRUE;
1533 return FALSE;
1536 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1537 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1538 elf_tdata (rinfo->output_bfd)->verref = t;
1541 amt = sizeof *a;
1542 a = bfd_zalloc (rinfo->output_bfd, amt);
1544 /* Note that we are copying a string pointer here, and testing it
1545 above. If bfd_elf_string_from_elf_section is ever changed to
1546 discard the string data when low in memory, this will have to be
1547 fixed. */
1548 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1550 a->vna_flags = h->verinfo.verdef->vd_flags;
1551 a->vna_nextptr = t->vn_auxptr;
1553 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1554 ++rinfo->vers;
1556 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1558 t->vn_auxptr = a;
1560 return TRUE;
1563 /* Figure out appropriate versions for all the symbols. We may not
1564 have the version number script until we have read all of the input
1565 files, so until that point we don't know which symbols should be
1566 local. This function is called via elf_link_hash_traverse. */
1568 bfd_boolean
1569 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1571 struct elf_assign_sym_version_info *sinfo;
1572 struct bfd_link_info *info;
1573 const struct elf_backend_data *bed;
1574 struct elf_info_failed eif;
1575 char *p;
1576 bfd_size_type amt;
1578 sinfo = data;
1579 info = sinfo->info;
1581 if (h->root.type == bfd_link_hash_warning)
1582 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1584 /* Fix the symbol flags. */
1585 eif.failed = FALSE;
1586 eif.info = info;
1587 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1589 if (eif.failed)
1590 sinfo->failed = TRUE;
1591 return FALSE;
1594 /* We only need version numbers for symbols defined in regular
1595 objects. */
1596 if (!h->def_regular)
1597 return TRUE;
1599 bed = get_elf_backend_data (sinfo->output_bfd);
1600 p = strchr (h->root.root.string, ELF_VER_CHR);
1601 if (p != NULL && h->verinfo.vertree == NULL)
1603 struct bfd_elf_version_tree *t;
1604 bfd_boolean hidden;
1606 hidden = TRUE;
1608 /* There are two consecutive ELF_VER_CHR characters if this is
1609 not a hidden symbol. */
1610 ++p;
1611 if (*p == ELF_VER_CHR)
1613 hidden = FALSE;
1614 ++p;
1617 /* If there is no version string, we can just return out. */
1618 if (*p == '\0')
1620 if (hidden)
1621 h->hidden = 1;
1622 return TRUE;
1625 /* Look for the version. If we find it, it is no longer weak. */
1626 for (t = sinfo->verdefs; t != NULL; t = t->next)
1628 if (strcmp (t->name, p) == 0)
1630 size_t len;
1631 char *alc;
1632 struct bfd_elf_version_expr *d;
1634 len = p - h->root.root.string;
1635 alc = bfd_malloc (len);
1636 if (alc == NULL)
1637 return FALSE;
1638 memcpy (alc, h->root.root.string, len - 1);
1639 alc[len - 1] = '\0';
1640 if (alc[len - 2] == ELF_VER_CHR)
1641 alc[len - 2] = '\0';
1643 h->verinfo.vertree = t;
1644 t->used = TRUE;
1645 d = NULL;
1647 if (t->globals.list != NULL)
1648 d = (*t->match) (&t->globals, NULL, alc);
1650 /* See if there is anything to force this symbol to
1651 local scope. */
1652 if (d == NULL && t->locals.list != NULL)
1654 d = (*t->match) (&t->locals, NULL, alc);
1655 if (d != NULL
1656 && h->dynindx != -1
1657 && info->shared
1658 && ! info->export_dynamic)
1659 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1662 free (alc);
1663 break;
1667 /* If we are building an application, we need to create a
1668 version node for this version. */
1669 if (t == NULL && info->executable)
1671 struct bfd_elf_version_tree **pp;
1672 int version_index;
1674 /* If we aren't going to export this symbol, we don't need
1675 to worry about it. */
1676 if (h->dynindx == -1)
1677 return TRUE;
1679 amt = sizeof *t;
1680 t = bfd_zalloc (sinfo->output_bfd, amt);
1681 if (t == NULL)
1683 sinfo->failed = TRUE;
1684 return FALSE;
1687 t->name = p;
1688 t->name_indx = (unsigned int) -1;
1689 t->used = TRUE;
1691 version_index = 1;
1692 /* Don't count anonymous version tag. */
1693 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1694 version_index = 0;
1695 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1696 ++version_index;
1697 t->vernum = version_index;
1699 *pp = t;
1701 h->verinfo.vertree = t;
1703 else if (t == NULL)
1705 /* We could not find the version for a symbol when
1706 generating a shared archive. Return an error. */
1707 (*_bfd_error_handler)
1708 (_("%B: undefined versioned symbol name %s"),
1709 sinfo->output_bfd, h->root.root.string);
1710 bfd_set_error (bfd_error_bad_value);
1711 sinfo->failed = TRUE;
1712 return FALSE;
1715 if (hidden)
1716 h->hidden = 1;
1719 /* If we don't have a version for this symbol, see if we can find
1720 something. */
1721 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
1723 struct bfd_elf_version_tree *t;
1724 struct bfd_elf_version_tree *local_ver;
1725 struct bfd_elf_version_expr *d;
1727 /* See if can find what version this symbol is in. If the
1728 symbol is supposed to be local, then don't actually register
1729 it. */
1730 local_ver = NULL;
1731 for (t = sinfo->verdefs; t != NULL; t = t->next)
1733 if (t->globals.list != NULL)
1735 bfd_boolean matched;
1737 matched = FALSE;
1738 d = NULL;
1739 while ((d = (*t->match) (&t->globals, d,
1740 h->root.root.string)) != NULL)
1741 if (d->symver)
1742 matched = TRUE;
1743 else
1745 /* There is a version without definition. Make
1746 the symbol the default definition for this
1747 version. */
1748 h->verinfo.vertree = t;
1749 local_ver = NULL;
1750 d->script = 1;
1751 break;
1753 if (d != NULL)
1754 break;
1755 else if (matched)
1756 /* There is no undefined version for this symbol. Hide the
1757 default one. */
1758 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1761 if (t->locals.list != NULL)
1763 d = NULL;
1764 while ((d = (*t->match) (&t->locals, d,
1765 h->root.root.string)) != NULL)
1767 local_ver = t;
1768 /* If the match is "*", keep looking for a more
1769 explicit, perhaps even global, match.
1770 XXX: Shouldn't this be !d->wildcard instead? */
1771 if (d->pattern[0] != '*' || d->pattern[1] != '\0')
1772 break;
1775 if (d != NULL)
1776 break;
1780 if (local_ver != NULL)
1782 h->verinfo.vertree = local_ver;
1783 if (h->dynindx != -1
1784 && info->shared
1785 && ! info->export_dynamic)
1787 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1792 return TRUE;
1795 /* Read and swap the relocs from the section indicated by SHDR. This
1796 may be either a REL or a RELA section. The relocations are
1797 translated into RELA relocations and stored in INTERNAL_RELOCS,
1798 which should have already been allocated to contain enough space.
1799 The EXTERNAL_RELOCS are a buffer where the external form of the
1800 relocations should be stored.
1802 Returns FALSE if something goes wrong. */
1804 static bfd_boolean
1805 elf_link_read_relocs_from_section (bfd *abfd,
1806 asection *sec,
1807 Elf_Internal_Shdr *shdr,
1808 void *external_relocs,
1809 Elf_Internal_Rela *internal_relocs)
1811 const struct elf_backend_data *bed;
1812 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
1813 const bfd_byte *erela;
1814 const bfd_byte *erelaend;
1815 Elf_Internal_Rela *irela;
1816 Elf_Internal_Shdr *symtab_hdr;
1817 size_t nsyms;
1819 /* Position ourselves at the start of the section. */
1820 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
1821 return FALSE;
1823 /* Read the relocations. */
1824 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
1825 return FALSE;
1827 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1828 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
1830 bed = get_elf_backend_data (abfd);
1832 /* Convert the external relocations to the internal format. */
1833 if (shdr->sh_entsize == bed->s->sizeof_rel)
1834 swap_in = bed->s->swap_reloc_in;
1835 else if (shdr->sh_entsize == bed->s->sizeof_rela)
1836 swap_in = bed->s->swap_reloca_in;
1837 else
1839 bfd_set_error (bfd_error_wrong_format);
1840 return FALSE;
1843 erela = external_relocs;
1844 erelaend = erela + shdr->sh_size;
1845 irela = internal_relocs;
1846 while (erela < erelaend)
1848 bfd_vma r_symndx;
1850 (*swap_in) (abfd, erela, irela);
1851 r_symndx = ELF32_R_SYM (irela->r_info);
1852 if (bed->s->arch_size == 64)
1853 r_symndx >>= 24;
1854 if ((size_t) r_symndx >= nsyms)
1856 (*_bfd_error_handler)
1857 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
1858 " for offset 0x%lx in section `%A'"),
1859 abfd, sec,
1860 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
1861 bfd_set_error (bfd_error_bad_value);
1862 return FALSE;
1864 irela += bed->s->int_rels_per_ext_rel;
1865 erela += shdr->sh_entsize;
1868 return TRUE;
1871 /* Read and swap the relocs for a section O. They may have been
1872 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1873 not NULL, they are used as buffers to read into. They are known to
1874 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
1875 the return value is allocated using either malloc or bfd_alloc,
1876 according to the KEEP_MEMORY argument. If O has two relocation
1877 sections (both REL and RELA relocations), then the REL_HDR
1878 relocations will appear first in INTERNAL_RELOCS, followed by the
1879 REL_HDR2 relocations. */
1881 Elf_Internal_Rela *
1882 _bfd_elf_link_read_relocs (bfd *abfd,
1883 asection *o,
1884 void *external_relocs,
1885 Elf_Internal_Rela *internal_relocs,
1886 bfd_boolean keep_memory)
1888 Elf_Internal_Shdr *rel_hdr;
1889 void *alloc1 = NULL;
1890 Elf_Internal_Rela *alloc2 = NULL;
1891 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1893 if (elf_section_data (o)->relocs != NULL)
1894 return elf_section_data (o)->relocs;
1896 if (o->reloc_count == 0)
1897 return NULL;
1899 rel_hdr = &elf_section_data (o)->rel_hdr;
1901 if (internal_relocs == NULL)
1903 bfd_size_type size;
1905 size = o->reloc_count;
1906 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
1907 if (keep_memory)
1908 internal_relocs = bfd_alloc (abfd, size);
1909 else
1910 internal_relocs = alloc2 = bfd_malloc (size);
1911 if (internal_relocs == NULL)
1912 goto error_return;
1915 if (external_relocs == NULL)
1917 bfd_size_type size = rel_hdr->sh_size;
1919 if (elf_section_data (o)->rel_hdr2)
1920 size += elf_section_data (o)->rel_hdr2->sh_size;
1921 alloc1 = bfd_malloc (size);
1922 if (alloc1 == NULL)
1923 goto error_return;
1924 external_relocs = alloc1;
1927 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
1928 external_relocs,
1929 internal_relocs))
1930 goto error_return;
1931 if (elf_section_data (o)->rel_hdr2
1932 && (!elf_link_read_relocs_from_section
1933 (abfd, o,
1934 elf_section_data (o)->rel_hdr2,
1935 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
1936 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
1937 * bed->s->int_rels_per_ext_rel))))
1938 goto error_return;
1940 /* Cache the results for next time, if we can. */
1941 if (keep_memory)
1942 elf_section_data (o)->relocs = internal_relocs;
1944 if (alloc1 != NULL)
1945 free (alloc1);
1947 /* Don't free alloc2, since if it was allocated we are passing it
1948 back (under the name of internal_relocs). */
1950 return internal_relocs;
1952 error_return:
1953 if (alloc1 != NULL)
1954 free (alloc1);
1955 if (alloc2 != NULL)
1956 free (alloc2);
1957 return NULL;
1960 /* Compute the size of, and allocate space for, REL_HDR which is the
1961 section header for a section containing relocations for O. */
1963 bfd_boolean
1964 _bfd_elf_link_size_reloc_section (bfd *abfd,
1965 Elf_Internal_Shdr *rel_hdr,
1966 asection *o)
1968 bfd_size_type reloc_count;
1969 bfd_size_type num_rel_hashes;
1971 /* Figure out how many relocations there will be. */
1972 if (rel_hdr == &elf_section_data (o)->rel_hdr)
1973 reloc_count = elf_section_data (o)->rel_count;
1974 else
1975 reloc_count = elf_section_data (o)->rel_count2;
1977 num_rel_hashes = o->reloc_count;
1978 if (num_rel_hashes < reloc_count)
1979 num_rel_hashes = reloc_count;
1981 /* That allows us to calculate the size of the section. */
1982 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
1984 /* The contents field must last into write_object_contents, so we
1985 allocate it with bfd_alloc rather than malloc. Also since we
1986 cannot be sure that the contents will actually be filled in,
1987 we zero the allocated space. */
1988 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
1989 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
1990 return FALSE;
1992 /* We only allocate one set of hash entries, so we only do it the
1993 first time we are called. */
1994 if (elf_section_data (o)->rel_hashes == NULL
1995 && num_rel_hashes)
1997 struct elf_link_hash_entry **p;
1999 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
2000 if (p == NULL)
2001 return FALSE;
2003 elf_section_data (o)->rel_hashes = p;
2006 return TRUE;
2009 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2010 originated from the section given by INPUT_REL_HDR) to the
2011 OUTPUT_BFD. */
2013 bfd_boolean
2014 _bfd_elf_link_output_relocs (bfd *output_bfd,
2015 asection *input_section,
2016 Elf_Internal_Shdr *input_rel_hdr,
2017 Elf_Internal_Rela *internal_relocs)
2019 Elf_Internal_Rela *irela;
2020 Elf_Internal_Rela *irelaend;
2021 bfd_byte *erel;
2022 Elf_Internal_Shdr *output_rel_hdr;
2023 asection *output_section;
2024 unsigned int *rel_countp = NULL;
2025 const struct elf_backend_data *bed;
2026 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2028 output_section = input_section->output_section;
2029 output_rel_hdr = NULL;
2031 if (elf_section_data (output_section)->rel_hdr.sh_entsize
2032 == input_rel_hdr->sh_entsize)
2034 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2035 rel_countp = &elf_section_data (output_section)->rel_count;
2037 else if (elf_section_data (output_section)->rel_hdr2
2038 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2039 == input_rel_hdr->sh_entsize))
2041 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2042 rel_countp = &elf_section_data (output_section)->rel_count2;
2044 else
2046 (*_bfd_error_handler)
2047 (_("%B: relocation size mismatch in %B section %A"),
2048 output_bfd, input_section->owner, input_section);
2049 bfd_set_error (bfd_error_wrong_object_format);
2050 return FALSE;
2053 bed = get_elf_backend_data (output_bfd);
2054 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2055 swap_out = bed->s->swap_reloc_out;
2056 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2057 swap_out = bed->s->swap_reloca_out;
2058 else
2059 abort ();
2061 erel = output_rel_hdr->contents;
2062 erel += *rel_countp * input_rel_hdr->sh_entsize;
2063 irela = internal_relocs;
2064 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2065 * bed->s->int_rels_per_ext_rel);
2066 while (irela < irelaend)
2068 (*swap_out) (output_bfd, irela, erel);
2069 irela += bed->s->int_rels_per_ext_rel;
2070 erel += input_rel_hdr->sh_entsize;
2073 /* Bump the counter, so that we know where to add the next set of
2074 relocations. */
2075 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2077 return TRUE;
2080 /* Fix up the flags for a symbol. This handles various cases which
2081 can only be fixed after all the input files are seen. This is
2082 currently called by both adjust_dynamic_symbol and
2083 assign_sym_version, which is unnecessary but perhaps more robust in
2084 the face of future changes. */
2086 bfd_boolean
2087 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2088 struct elf_info_failed *eif)
2090 /* If this symbol was mentioned in a non-ELF file, try to set
2091 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2092 permit a non-ELF file to correctly refer to a symbol defined in
2093 an ELF dynamic object. */
2094 if (h->non_elf)
2096 while (h->root.type == bfd_link_hash_indirect)
2097 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2099 if (h->root.type != bfd_link_hash_defined
2100 && h->root.type != bfd_link_hash_defweak)
2102 h->ref_regular = 1;
2103 h->ref_regular_nonweak = 1;
2105 else
2107 if (h->root.u.def.section->owner != NULL
2108 && (bfd_get_flavour (h->root.u.def.section->owner)
2109 == bfd_target_elf_flavour))
2111 h->ref_regular = 1;
2112 h->ref_regular_nonweak = 1;
2114 else
2115 h->def_regular = 1;
2118 if (h->dynindx == -1
2119 && (h->def_dynamic
2120 || h->ref_dynamic))
2122 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2124 eif->failed = TRUE;
2125 return FALSE;
2129 else
2131 /* Unfortunately, NON_ELF is only correct if the symbol
2132 was first seen in a non-ELF file. Fortunately, if the symbol
2133 was first seen in an ELF file, we're probably OK unless the
2134 symbol was defined in a non-ELF file. Catch that case here.
2135 FIXME: We're still in trouble if the symbol was first seen in
2136 a dynamic object, and then later in a non-ELF regular object. */
2137 if ((h->root.type == bfd_link_hash_defined
2138 || h->root.type == bfd_link_hash_defweak)
2139 && !h->def_regular
2140 && (h->root.u.def.section->owner != NULL
2141 ? (bfd_get_flavour (h->root.u.def.section->owner)
2142 != bfd_target_elf_flavour)
2143 : (bfd_is_abs_section (h->root.u.def.section)
2144 && !h->def_dynamic)))
2145 h->def_regular = 1;
2148 /* If this is a final link, and the symbol was defined as a common
2149 symbol in a regular object file, and there was no definition in
2150 any dynamic object, then the linker will have allocated space for
2151 the symbol in a common section but the DEF_REGULAR
2152 flag will not have been set. */
2153 if (h->root.type == bfd_link_hash_defined
2154 && !h->def_regular
2155 && h->ref_regular
2156 && !h->def_dynamic
2157 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2158 h->def_regular = 1;
2160 /* If -Bsymbolic was used (which means to bind references to global
2161 symbols to the definition within the shared object), and this
2162 symbol was defined in a regular object, then it actually doesn't
2163 need a PLT entry. Likewise, if the symbol has non-default
2164 visibility. If the symbol has hidden or internal visibility, we
2165 will force it local. */
2166 if (h->needs_plt
2167 && eif->info->shared
2168 && is_elf_hash_table (eif->info->hash)
2169 && (eif->info->symbolic
2170 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2171 && h->def_regular)
2173 const struct elf_backend_data *bed;
2174 bfd_boolean force_local;
2176 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2178 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2179 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2180 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2183 /* If a weak undefined symbol has non-default visibility, we also
2184 hide it from the dynamic linker. */
2185 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2186 && h->root.type == bfd_link_hash_undefweak)
2188 const struct elf_backend_data *bed;
2189 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2190 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2193 /* If this is a weak defined symbol in a dynamic object, and we know
2194 the real definition in the dynamic object, copy interesting flags
2195 over to the real definition. */
2196 if (h->weakdef != NULL)
2198 struct elf_link_hash_entry *weakdef;
2200 weakdef = h->weakdef;
2201 if (h->root.type == bfd_link_hash_indirect)
2202 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2204 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2205 || h->root.type == bfd_link_hash_defweak);
2206 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2207 || weakdef->root.type == bfd_link_hash_defweak);
2208 BFD_ASSERT (weakdef->def_dynamic);
2210 /* If the real definition is defined by a regular object file,
2211 don't do anything special. See the longer description in
2212 _bfd_elf_adjust_dynamic_symbol, below. */
2213 if (weakdef->def_regular)
2214 h->weakdef = NULL;
2215 else
2217 const struct elf_backend_data *bed;
2219 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2220 (*bed->elf_backend_copy_indirect_symbol) (bed, weakdef, h);
2224 return TRUE;
2227 /* Make the backend pick a good value for a dynamic symbol. This is
2228 called via elf_link_hash_traverse, and also calls itself
2229 recursively. */
2231 bfd_boolean
2232 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2234 struct elf_info_failed *eif = data;
2235 bfd *dynobj;
2236 const struct elf_backend_data *bed;
2238 if (! is_elf_hash_table (eif->info->hash))
2239 return FALSE;
2241 if (h->root.type == bfd_link_hash_warning)
2243 h->plt = elf_hash_table (eif->info)->init_offset;
2244 h->got = elf_hash_table (eif->info)->init_offset;
2246 /* When warning symbols are created, they **replace** the "real"
2247 entry in the hash table, thus we never get to see the real
2248 symbol in a hash traversal. So look at it now. */
2249 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2252 /* Ignore indirect symbols. These are added by the versioning code. */
2253 if (h->root.type == bfd_link_hash_indirect)
2254 return TRUE;
2256 /* Fix the symbol flags. */
2257 if (! _bfd_elf_fix_symbol_flags (h, eif))
2258 return FALSE;
2260 /* If this symbol does not require a PLT entry, and it is not
2261 defined by a dynamic object, or is not referenced by a regular
2262 object, ignore it. We do have to handle a weak defined symbol,
2263 even if no regular object refers to it, if we decided to add it
2264 to the dynamic symbol table. FIXME: Do we normally need to worry
2265 about symbols which are defined by one dynamic object and
2266 referenced by another one? */
2267 if (!h->needs_plt
2268 && (h->def_regular
2269 || !h->def_dynamic
2270 || (!h->ref_regular
2271 && (h->weakdef == NULL || h->weakdef->dynindx == -1))))
2273 h->plt = elf_hash_table (eif->info)->init_offset;
2274 return TRUE;
2277 /* If we've already adjusted this symbol, don't do it again. This
2278 can happen via a recursive call. */
2279 if (h->dynamic_adjusted)
2280 return TRUE;
2282 /* Don't look at this symbol again. Note that we must set this
2283 after checking the above conditions, because we may look at a
2284 symbol once, decide not to do anything, and then get called
2285 recursively later after REF_REGULAR is set below. */
2286 h->dynamic_adjusted = 1;
2288 /* If this is a weak definition, and we know a real definition, and
2289 the real symbol is not itself defined by a regular object file,
2290 then get a good value for the real definition. We handle the
2291 real symbol first, for the convenience of the backend routine.
2293 Note that there is a confusing case here. If the real definition
2294 is defined by a regular object file, we don't get the real symbol
2295 from the dynamic object, but we do get the weak symbol. If the
2296 processor backend uses a COPY reloc, then if some routine in the
2297 dynamic object changes the real symbol, we will not see that
2298 change in the corresponding weak symbol. This is the way other
2299 ELF linkers work as well, and seems to be a result of the shared
2300 library model.
2302 I will clarify this issue. Most SVR4 shared libraries define the
2303 variable _timezone and define timezone as a weak synonym. The
2304 tzset call changes _timezone. If you write
2305 extern int timezone;
2306 int _timezone = 5;
2307 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2308 you might expect that, since timezone is a synonym for _timezone,
2309 the same number will print both times. However, if the processor
2310 backend uses a COPY reloc, then actually timezone will be copied
2311 into your process image, and, since you define _timezone
2312 yourself, _timezone will not. Thus timezone and _timezone will
2313 wind up at different memory locations. The tzset call will set
2314 _timezone, leaving timezone unchanged. */
2316 if (h->weakdef != NULL)
2318 /* If we get to this point, we know there is an implicit
2319 reference by a regular object file via the weak symbol H.
2320 FIXME: Is this really true? What if the traversal finds
2321 H->WEAKDEF before it finds H? */
2322 h->weakdef->ref_regular = 1;
2324 if (! _bfd_elf_adjust_dynamic_symbol (h->weakdef, eif))
2325 return FALSE;
2328 /* If a symbol has no type and no size and does not require a PLT
2329 entry, then we are probably about to do the wrong thing here: we
2330 are probably going to create a COPY reloc for an empty object.
2331 This case can arise when a shared object is built with assembly
2332 code, and the assembly code fails to set the symbol type. */
2333 if (h->size == 0
2334 && h->type == STT_NOTYPE
2335 && !h->needs_plt)
2336 (*_bfd_error_handler)
2337 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2338 h->root.root.string);
2340 dynobj = elf_hash_table (eif->info)->dynobj;
2341 bed = get_elf_backend_data (dynobj);
2342 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2344 eif->failed = TRUE;
2345 return FALSE;
2348 return TRUE;
2351 /* Adjust all external symbols pointing into SEC_MERGE sections
2352 to reflect the object merging within the sections. */
2354 bfd_boolean
2355 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2357 asection *sec;
2359 if (h->root.type == bfd_link_hash_warning)
2360 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2362 if ((h->root.type == bfd_link_hash_defined
2363 || h->root.type == bfd_link_hash_defweak)
2364 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2365 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2367 bfd *output_bfd = data;
2369 h->root.u.def.value =
2370 _bfd_merged_section_offset (output_bfd,
2371 &h->root.u.def.section,
2372 elf_section_data (sec)->sec_info,
2373 h->root.u.def.value);
2376 return TRUE;
2379 /* Returns false if the symbol referred to by H should be considered
2380 to resolve local to the current module, and true if it should be
2381 considered to bind dynamically. */
2383 bfd_boolean
2384 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2385 struct bfd_link_info *info,
2386 bfd_boolean ignore_protected)
2388 bfd_boolean binding_stays_local_p;
2390 if (h == NULL)
2391 return FALSE;
2393 while (h->root.type == bfd_link_hash_indirect
2394 || h->root.type == bfd_link_hash_warning)
2395 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2397 /* If it was forced local, then clearly it's not dynamic. */
2398 if (h->dynindx == -1)
2399 return FALSE;
2400 if (h->forced_local)
2401 return FALSE;
2403 /* Identify the cases where name binding rules say that a
2404 visible symbol resolves locally. */
2405 binding_stays_local_p = info->executable || info->symbolic;
2407 switch (ELF_ST_VISIBILITY (h->other))
2409 case STV_INTERNAL:
2410 case STV_HIDDEN:
2411 return FALSE;
2413 case STV_PROTECTED:
2414 /* Proper resolution for function pointer equality may require
2415 that these symbols perhaps be resolved dynamically, even though
2416 we should be resolving them to the current module. */
2417 if (!ignore_protected)
2418 binding_stays_local_p = TRUE;
2419 break;
2421 default:
2422 break;
2425 /* If it isn't defined locally, then clearly it's dynamic. */
2426 if (!h->def_regular)
2427 return TRUE;
2429 /* Otherwise, the symbol is dynamic if binding rules don't tell
2430 us that it remains local. */
2431 return !binding_stays_local_p;
2434 /* Return true if the symbol referred to by H should be considered
2435 to resolve local to the current module, and false otherwise. Differs
2436 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2437 undefined symbols and weak symbols. */
2439 bfd_boolean
2440 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2441 struct bfd_link_info *info,
2442 bfd_boolean local_protected)
2444 /* If it's a local sym, of course we resolve locally. */
2445 if (h == NULL)
2446 return TRUE;
2448 /* Common symbols that become definitions don't get the DEF_REGULAR
2449 flag set, so test it first, and don't bail out. */
2450 if (ELF_COMMON_DEF_P (h))
2451 /* Do nothing. */;
2452 /* If we don't have a definition in a regular file, then we can't
2453 resolve locally. The sym is either undefined or dynamic. */
2454 else if (!h->def_regular)
2455 return FALSE;
2457 /* Forced local symbols resolve locally. */
2458 if (h->forced_local)
2459 return TRUE;
2461 /* As do non-dynamic symbols. */
2462 if (h->dynindx == -1)
2463 return TRUE;
2465 /* At this point, we know the symbol is defined and dynamic. In an
2466 executable it must resolve locally, likewise when building symbolic
2467 shared libraries. */
2468 if (info->executable || info->symbolic)
2469 return TRUE;
2471 /* Now deal with defined dynamic symbols in shared libraries. Ones
2472 with default visibility might not resolve locally. */
2473 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2474 return FALSE;
2476 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2477 if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED)
2478 return TRUE;
2480 /* Function pointer equality tests may require that STV_PROTECTED
2481 symbols be treated as dynamic symbols, even when we know that the
2482 dynamic linker will resolve them locally. */
2483 return local_protected;
2486 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2487 aligned. Returns the first TLS output section. */
2489 struct bfd_section *
2490 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2492 struct bfd_section *sec, *tls;
2493 unsigned int align = 0;
2495 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2496 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2497 break;
2498 tls = sec;
2500 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2501 if (sec->alignment_power > align)
2502 align = sec->alignment_power;
2504 elf_hash_table (info)->tls_sec = tls;
2506 /* Ensure the alignment of the first section is the largest alignment,
2507 so that the tls segment starts aligned. */
2508 if (tls != NULL)
2509 tls->alignment_power = align;
2511 return tls;
2514 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2515 static bfd_boolean
2516 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2517 Elf_Internal_Sym *sym)
2519 /* Local symbols do not count, but target specific ones might. */
2520 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2521 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2522 return FALSE;
2524 /* Function symbols do not count. */
2525 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
2526 return FALSE;
2528 /* If the section is undefined, then so is the symbol. */
2529 if (sym->st_shndx == SHN_UNDEF)
2530 return FALSE;
2532 /* If the symbol is defined in the common section, then
2533 it is a common definition and so does not count. */
2534 if (sym->st_shndx == SHN_COMMON)
2535 return FALSE;
2537 /* If the symbol is in a target specific section then we
2538 must rely upon the backend to tell us what it is. */
2539 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2540 /* FIXME - this function is not coded yet:
2542 return _bfd_is_global_symbol_definition (abfd, sym);
2544 Instead for now assume that the definition is not global,
2545 Even if this is wrong, at least the linker will behave
2546 in the same way that it used to do. */
2547 return FALSE;
2549 return TRUE;
2552 /* Search the symbol table of the archive element of the archive ABFD
2553 whose archive map contains a mention of SYMDEF, and determine if
2554 the symbol is defined in this element. */
2555 static bfd_boolean
2556 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2558 Elf_Internal_Shdr * hdr;
2559 bfd_size_type symcount;
2560 bfd_size_type extsymcount;
2561 bfd_size_type extsymoff;
2562 Elf_Internal_Sym *isymbuf;
2563 Elf_Internal_Sym *isym;
2564 Elf_Internal_Sym *isymend;
2565 bfd_boolean result;
2567 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2568 if (abfd == NULL)
2569 return FALSE;
2571 if (! bfd_check_format (abfd, bfd_object))
2572 return FALSE;
2574 /* If we have already included the element containing this symbol in the
2575 link then we do not need to include it again. Just claim that any symbol
2576 it contains is not a definition, so that our caller will not decide to
2577 (re)include this element. */
2578 if (abfd->archive_pass)
2579 return FALSE;
2581 /* Select the appropriate symbol table. */
2582 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2583 hdr = &elf_tdata (abfd)->symtab_hdr;
2584 else
2585 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2587 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2589 /* The sh_info field of the symtab header tells us where the
2590 external symbols start. We don't care about the local symbols. */
2591 if (elf_bad_symtab (abfd))
2593 extsymcount = symcount;
2594 extsymoff = 0;
2596 else
2598 extsymcount = symcount - hdr->sh_info;
2599 extsymoff = hdr->sh_info;
2602 if (extsymcount == 0)
2603 return FALSE;
2605 /* Read in the symbol table. */
2606 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2607 NULL, NULL, NULL);
2608 if (isymbuf == NULL)
2609 return FALSE;
2611 /* Scan the symbol table looking for SYMDEF. */
2612 result = FALSE;
2613 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2615 const char *name;
2617 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
2618 isym->st_name);
2619 if (name == NULL)
2620 break;
2622 if (strcmp (name, symdef->name) == 0)
2624 result = is_global_data_symbol_definition (abfd, isym);
2625 break;
2629 free (isymbuf);
2631 return result;
2634 /* Add an entry to the .dynamic table. */
2636 bfd_boolean
2637 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
2638 bfd_vma tag,
2639 bfd_vma val)
2641 struct elf_link_hash_table *hash_table;
2642 const struct elf_backend_data *bed;
2643 asection *s;
2644 bfd_size_type newsize;
2645 bfd_byte *newcontents;
2646 Elf_Internal_Dyn dyn;
2648 hash_table = elf_hash_table (info);
2649 if (! is_elf_hash_table (hash_table))
2650 return FALSE;
2652 bed = get_elf_backend_data (hash_table->dynobj);
2653 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2654 BFD_ASSERT (s != NULL);
2656 newsize = s->size + bed->s->sizeof_dyn;
2657 newcontents = bfd_realloc (s->contents, newsize);
2658 if (newcontents == NULL)
2659 return FALSE;
2661 dyn.d_tag = tag;
2662 dyn.d_un.d_val = val;
2663 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
2665 s->size = newsize;
2666 s->contents = newcontents;
2668 return TRUE;
2671 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2672 otherwise just check whether one already exists. Returns -1 on error,
2673 1 if a DT_NEEDED tag already exists, and 0 on success. */
2675 static int
2676 elf_add_dt_needed_tag (struct bfd_link_info *info,
2677 const char *soname,
2678 bfd_boolean do_it)
2680 struct elf_link_hash_table *hash_table;
2681 bfd_size_type oldsize;
2682 bfd_size_type strindex;
2684 hash_table = elf_hash_table (info);
2685 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
2686 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
2687 if (strindex == (bfd_size_type) -1)
2688 return -1;
2690 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
2692 asection *sdyn;
2693 const struct elf_backend_data *bed;
2694 bfd_byte *extdyn;
2696 bed = get_elf_backend_data (hash_table->dynobj);
2697 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2698 BFD_ASSERT (sdyn != NULL);
2700 for (extdyn = sdyn->contents;
2701 extdyn < sdyn->contents + sdyn->size;
2702 extdyn += bed->s->sizeof_dyn)
2704 Elf_Internal_Dyn dyn;
2706 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
2707 if (dyn.d_tag == DT_NEEDED
2708 && dyn.d_un.d_val == strindex)
2710 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2711 return 1;
2716 if (do_it)
2718 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
2719 return -1;
2721 else
2722 /* We were just checking for existence of the tag. */
2723 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2725 return 0;
2728 /* Sort symbol by value and section. */
2729 static int
2730 elf_sort_symbol (const void *arg1, const void *arg2)
2732 const struct elf_link_hash_entry *h1;
2733 const struct elf_link_hash_entry *h2;
2734 bfd_signed_vma vdiff;
2736 h1 = *(const struct elf_link_hash_entry **) arg1;
2737 h2 = *(const struct elf_link_hash_entry **) arg2;
2738 vdiff = h1->root.u.def.value - h2->root.u.def.value;
2739 if (vdiff != 0)
2740 return vdiff > 0 ? 1 : -1;
2741 else
2743 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
2744 if (sdiff != 0)
2745 return sdiff > 0 ? 1 : -1;
2747 return 0;
2750 /* This function is used to adjust offsets into .dynstr for
2751 dynamic symbols. This is called via elf_link_hash_traverse. */
2753 static bfd_boolean
2754 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
2756 struct elf_strtab_hash *dynstr = data;
2758 if (h->root.type == bfd_link_hash_warning)
2759 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2761 if (h->dynindx != -1)
2762 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
2763 return TRUE;
2766 /* Assign string offsets in .dynstr, update all structures referencing
2767 them. */
2769 static bfd_boolean
2770 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
2772 struct elf_link_hash_table *hash_table = elf_hash_table (info);
2773 struct elf_link_local_dynamic_entry *entry;
2774 struct elf_strtab_hash *dynstr = hash_table->dynstr;
2775 bfd *dynobj = hash_table->dynobj;
2776 asection *sdyn;
2777 bfd_size_type size;
2778 const struct elf_backend_data *bed;
2779 bfd_byte *extdyn;
2781 _bfd_elf_strtab_finalize (dynstr);
2782 size = _bfd_elf_strtab_size (dynstr);
2784 bed = get_elf_backend_data (dynobj);
2785 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2786 BFD_ASSERT (sdyn != NULL);
2788 /* Update all .dynamic entries referencing .dynstr strings. */
2789 for (extdyn = sdyn->contents;
2790 extdyn < sdyn->contents + sdyn->size;
2791 extdyn += bed->s->sizeof_dyn)
2793 Elf_Internal_Dyn dyn;
2795 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
2796 switch (dyn.d_tag)
2798 case DT_STRSZ:
2799 dyn.d_un.d_val = size;
2800 break;
2801 case DT_NEEDED:
2802 case DT_SONAME:
2803 case DT_RPATH:
2804 case DT_RUNPATH:
2805 case DT_FILTER:
2806 case DT_AUXILIARY:
2807 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
2808 break;
2809 default:
2810 continue;
2812 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
2815 /* Now update local dynamic symbols. */
2816 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
2817 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
2818 entry->isym.st_name);
2820 /* And the rest of dynamic symbols. */
2821 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
2823 /* Adjust version definitions. */
2824 if (elf_tdata (output_bfd)->cverdefs)
2826 asection *s;
2827 bfd_byte *p;
2828 bfd_size_type i;
2829 Elf_Internal_Verdef def;
2830 Elf_Internal_Verdaux defaux;
2832 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
2833 p = s->contents;
2836 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
2837 &def);
2838 p += sizeof (Elf_External_Verdef);
2839 for (i = 0; i < def.vd_cnt; ++i)
2841 _bfd_elf_swap_verdaux_in (output_bfd,
2842 (Elf_External_Verdaux *) p, &defaux);
2843 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
2844 defaux.vda_name);
2845 _bfd_elf_swap_verdaux_out (output_bfd,
2846 &defaux, (Elf_External_Verdaux *) p);
2847 p += sizeof (Elf_External_Verdaux);
2850 while (def.vd_next);
2853 /* Adjust version references. */
2854 if (elf_tdata (output_bfd)->verref)
2856 asection *s;
2857 bfd_byte *p;
2858 bfd_size_type i;
2859 Elf_Internal_Verneed need;
2860 Elf_Internal_Vernaux needaux;
2862 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
2863 p = s->contents;
2866 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
2867 &need);
2868 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
2869 _bfd_elf_swap_verneed_out (output_bfd, &need,
2870 (Elf_External_Verneed *) p);
2871 p += sizeof (Elf_External_Verneed);
2872 for (i = 0; i < need.vn_cnt; ++i)
2874 _bfd_elf_swap_vernaux_in (output_bfd,
2875 (Elf_External_Vernaux *) p, &needaux);
2876 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
2877 needaux.vna_name);
2878 _bfd_elf_swap_vernaux_out (output_bfd,
2879 &needaux,
2880 (Elf_External_Vernaux *) p);
2881 p += sizeof (Elf_External_Vernaux);
2884 while (need.vn_next);
2887 return TRUE;
2890 /* Add symbols from an ELF object file to the linker hash table. */
2892 static bfd_boolean
2893 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
2895 bfd_boolean (*add_symbol_hook)
2896 (bfd *, struct bfd_link_info *, Elf_Internal_Sym *,
2897 const char **, flagword *, asection **, bfd_vma *);
2898 bfd_boolean (*check_relocs)
2899 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
2900 bfd_boolean (*check_directives)
2901 (bfd *, struct bfd_link_info *);
2902 bfd_boolean collect;
2903 Elf_Internal_Shdr *hdr;
2904 bfd_size_type symcount;
2905 bfd_size_type extsymcount;
2906 bfd_size_type extsymoff;
2907 struct elf_link_hash_entry **sym_hash;
2908 bfd_boolean dynamic;
2909 Elf_External_Versym *extversym = NULL;
2910 Elf_External_Versym *ever;
2911 struct elf_link_hash_entry *weaks;
2912 struct elf_link_hash_entry **nondeflt_vers = NULL;
2913 bfd_size_type nondeflt_vers_cnt = 0;
2914 Elf_Internal_Sym *isymbuf = NULL;
2915 Elf_Internal_Sym *isym;
2916 Elf_Internal_Sym *isymend;
2917 const struct elf_backend_data *bed;
2918 bfd_boolean add_needed;
2919 struct elf_link_hash_table * hash_table;
2920 bfd_size_type amt;
2922 hash_table = elf_hash_table (info);
2924 bed = get_elf_backend_data (abfd);
2925 add_symbol_hook = bed->elf_add_symbol_hook;
2926 collect = bed->collect;
2928 if ((abfd->flags & DYNAMIC) == 0)
2929 dynamic = FALSE;
2930 else
2932 dynamic = TRUE;
2934 /* You can't use -r against a dynamic object. Also, there's no
2935 hope of using a dynamic object which does not exactly match
2936 the format of the output file. */
2937 if (info->relocatable
2938 || !is_elf_hash_table (hash_table)
2939 || hash_table->root.creator != abfd->xvec)
2941 if (info->relocatable)
2942 bfd_set_error (bfd_error_invalid_operation);
2943 else
2944 bfd_set_error (bfd_error_wrong_format);
2945 goto error_return;
2949 /* As a GNU extension, any input sections which are named
2950 .gnu.warning.SYMBOL are treated as warning symbols for the given
2951 symbol. This differs from .gnu.warning sections, which generate
2952 warnings when they are included in an output file. */
2953 if (info->executable)
2955 asection *s;
2957 for (s = abfd->sections; s != NULL; s = s->next)
2959 const char *name;
2961 name = bfd_get_section_name (abfd, s);
2962 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
2964 char *msg;
2965 bfd_size_type sz;
2966 bfd_size_type prefix_len;
2967 const char * gnu_warning_prefix = _("warning: ");
2969 name += sizeof ".gnu.warning." - 1;
2971 /* If this is a shared object, then look up the symbol
2972 in the hash table. If it is there, and it is already
2973 been defined, then we will not be using the entry
2974 from this shared object, so we don't need to warn.
2975 FIXME: If we see the definition in a regular object
2976 later on, we will warn, but we shouldn't. The only
2977 fix is to keep track of what warnings we are supposed
2978 to emit, and then handle them all at the end of the
2979 link. */
2980 if (dynamic)
2982 struct elf_link_hash_entry *h;
2984 h = elf_link_hash_lookup (hash_table, name,
2985 FALSE, FALSE, TRUE);
2987 /* FIXME: What about bfd_link_hash_common? */
2988 if (h != NULL
2989 && (h->root.type == bfd_link_hash_defined
2990 || h->root.type == bfd_link_hash_defweak))
2992 /* We don't want to issue this warning. Clobber
2993 the section size so that the warning does not
2994 get copied into the output file. */
2995 s->size = 0;
2996 continue;
3000 sz = s->size;
3001 prefix_len = strlen (gnu_warning_prefix);
3002 msg = bfd_alloc (abfd, prefix_len + sz + 1);
3003 if (msg == NULL)
3004 goto error_return;
3006 strcpy (msg, gnu_warning_prefix);
3007 if (! bfd_get_section_contents (abfd, s, msg + prefix_len, 0, sz))
3008 goto error_return;
3010 msg[prefix_len + sz] = '\0';
3012 if (! (_bfd_generic_link_add_one_symbol
3013 (info, abfd, name, BSF_WARNING, s, 0, msg,
3014 FALSE, collect, NULL)))
3015 goto error_return;
3017 if (! info->relocatable)
3019 /* Clobber the section size so that the warning does
3020 not get copied into the output file. */
3021 s->size = 0;
3027 add_needed = TRUE;
3028 if (! dynamic)
3030 /* If we are creating a shared library, create all the dynamic
3031 sections immediately. We need to attach them to something,
3032 so we attach them to this BFD, provided it is the right
3033 format. FIXME: If there are no input BFD's of the same
3034 format as the output, we can't make a shared library. */
3035 if (info->shared
3036 && is_elf_hash_table (hash_table)
3037 && hash_table->root.creator == abfd->xvec
3038 && ! hash_table->dynamic_sections_created)
3040 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3041 goto error_return;
3044 else if (!is_elf_hash_table (hash_table))
3045 goto error_return;
3046 else
3048 asection *s;
3049 const char *soname = NULL;
3050 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3051 int ret;
3053 /* ld --just-symbols and dynamic objects don't mix very well.
3054 Test for --just-symbols by looking at info set up by
3055 _bfd_elf_link_just_syms. */
3056 if ((s = abfd->sections) != NULL
3057 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3058 goto error_return;
3060 /* If this dynamic lib was specified on the command line with
3061 --as-needed in effect, then we don't want to add a DT_NEEDED
3062 tag unless the lib is actually used. Similary for libs brought
3063 in by another lib's DT_NEEDED. When --no-add-needed is used
3064 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3065 any dynamic library in DT_NEEDED tags in the dynamic lib at
3066 all. */
3067 add_needed = (elf_dyn_lib_class (abfd)
3068 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3069 | DYN_NO_NEEDED)) == 0;
3071 s = bfd_get_section_by_name (abfd, ".dynamic");
3072 if (s != NULL)
3074 bfd_byte *dynbuf;
3075 bfd_byte *extdyn;
3076 int elfsec;
3077 unsigned long shlink;
3079 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3080 goto error_free_dyn;
3082 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3083 if (elfsec == -1)
3084 goto error_free_dyn;
3085 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3087 for (extdyn = dynbuf;
3088 extdyn < dynbuf + s->size;
3089 extdyn += bed->s->sizeof_dyn)
3091 Elf_Internal_Dyn dyn;
3093 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3094 if (dyn.d_tag == DT_SONAME)
3096 unsigned int tagv = dyn.d_un.d_val;
3097 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3098 if (soname == NULL)
3099 goto error_free_dyn;
3101 if (dyn.d_tag == DT_NEEDED)
3103 struct bfd_link_needed_list *n, **pn;
3104 char *fnm, *anm;
3105 unsigned int tagv = dyn.d_un.d_val;
3107 amt = sizeof (struct bfd_link_needed_list);
3108 n = bfd_alloc (abfd, amt);
3109 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3110 if (n == NULL || fnm == NULL)
3111 goto error_free_dyn;
3112 amt = strlen (fnm) + 1;
3113 anm = bfd_alloc (abfd, amt);
3114 if (anm == NULL)
3115 goto error_free_dyn;
3116 memcpy (anm, fnm, amt);
3117 n->name = anm;
3118 n->by = abfd;
3119 n->next = NULL;
3120 for (pn = & hash_table->needed;
3121 *pn != NULL;
3122 pn = &(*pn)->next)
3124 *pn = n;
3126 if (dyn.d_tag == DT_RUNPATH)
3128 struct bfd_link_needed_list *n, **pn;
3129 char *fnm, *anm;
3130 unsigned int tagv = dyn.d_un.d_val;
3132 amt = sizeof (struct bfd_link_needed_list);
3133 n = bfd_alloc (abfd, amt);
3134 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3135 if (n == NULL || fnm == NULL)
3136 goto error_free_dyn;
3137 amt = strlen (fnm) + 1;
3138 anm = bfd_alloc (abfd, amt);
3139 if (anm == NULL)
3140 goto error_free_dyn;
3141 memcpy (anm, fnm, amt);
3142 n->name = anm;
3143 n->by = abfd;
3144 n->next = NULL;
3145 for (pn = & runpath;
3146 *pn != NULL;
3147 pn = &(*pn)->next)
3149 *pn = n;
3151 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3152 if (!runpath && dyn.d_tag == DT_RPATH)
3154 struct bfd_link_needed_list *n, **pn;
3155 char *fnm, *anm;
3156 unsigned int tagv = dyn.d_un.d_val;
3158 amt = sizeof (struct bfd_link_needed_list);
3159 n = bfd_alloc (abfd, amt);
3160 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3161 if (n == NULL || fnm == NULL)
3162 goto error_free_dyn;
3163 amt = strlen (fnm) + 1;
3164 anm = bfd_alloc (abfd, amt);
3165 if (anm == NULL)
3167 error_free_dyn:
3168 free (dynbuf);
3169 goto error_return;
3171 memcpy (anm, fnm, amt);
3172 n->name = anm;
3173 n->by = abfd;
3174 n->next = NULL;
3175 for (pn = & rpath;
3176 *pn != NULL;
3177 pn = &(*pn)->next)
3179 *pn = n;
3183 free (dynbuf);
3186 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3187 frees all more recently bfd_alloc'd blocks as well. */
3188 if (runpath)
3189 rpath = runpath;
3191 if (rpath)
3193 struct bfd_link_needed_list **pn;
3194 for (pn = & hash_table->runpath;
3195 *pn != NULL;
3196 pn = &(*pn)->next)
3198 *pn = rpath;
3201 /* We do not want to include any of the sections in a dynamic
3202 object in the output file. We hack by simply clobbering the
3203 list of sections in the BFD. This could be handled more
3204 cleanly by, say, a new section flag; the existing
3205 SEC_NEVER_LOAD flag is not the one we want, because that one
3206 still implies that the section takes up space in the output
3207 file. */
3208 bfd_section_list_clear (abfd);
3210 /* If this is the first dynamic object found in the link, create
3211 the special sections required for dynamic linking. */
3212 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3213 goto error_return;
3215 /* Find the name to use in a DT_NEEDED entry that refers to this
3216 object. If the object has a DT_SONAME entry, we use it.
3217 Otherwise, if the generic linker stuck something in
3218 elf_dt_name, we use that. Otherwise, we just use the file
3219 name. */
3220 if (soname == NULL || *soname == '\0')
3222 soname = elf_dt_name (abfd);
3223 if (soname == NULL || *soname == '\0')
3224 soname = bfd_get_filename (abfd);
3227 /* Save the SONAME because sometimes the linker emulation code
3228 will need to know it. */
3229 elf_dt_name (abfd) = soname;
3231 ret = elf_add_dt_needed_tag (info, soname, add_needed);
3232 if (ret < 0)
3233 goto error_return;
3235 /* If we have already included this dynamic object in the
3236 link, just ignore it. There is no reason to include a
3237 particular dynamic object more than once. */
3238 if (ret > 0)
3239 return TRUE;
3242 /* If this is a dynamic object, we always link against the .dynsym
3243 symbol table, not the .symtab symbol table. The dynamic linker
3244 will only see the .dynsym symbol table, so there is no reason to
3245 look at .symtab for a dynamic object. */
3247 if (! dynamic || elf_dynsymtab (abfd) == 0)
3248 hdr = &elf_tdata (abfd)->symtab_hdr;
3249 else
3250 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3252 symcount = hdr->sh_size / bed->s->sizeof_sym;
3254 /* The sh_info field of the symtab header tells us where the
3255 external symbols start. We don't care about the local symbols at
3256 this point. */
3257 if (elf_bad_symtab (abfd))
3259 extsymcount = symcount;
3260 extsymoff = 0;
3262 else
3264 extsymcount = symcount - hdr->sh_info;
3265 extsymoff = hdr->sh_info;
3268 sym_hash = NULL;
3269 if (extsymcount != 0)
3271 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3272 NULL, NULL, NULL);
3273 if (isymbuf == NULL)
3274 goto error_return;
3276 /* We store a pointer to the hash table entry for each external
3277 symbol. */
3278 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3279 sym_hash = bfd_alloc (abfd, amt);
3280 if (sym_hash == NULL)
3281 goto error_free_sym;
3282 elf_sym_hashes (abfd) = sym_hash;
3285 if (dynamic)
3287 /* Read in any version definitions. */
3288 if (! _bfd_elf_slurp_version_tables (abfd))
3289 goto error_free_sym;
3291 /* Read in the symbol versions, but don't bother to convert them
3292 to internal format. */
3293 if (elf_dynversym (abfd) != 0)
3295 Elf_Internal_Shdr *versymhdr;
3297 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3298 extversym = bfd_malloc (versymhdr->sh_size);
3299 if (extversym == NULL)
3300 goto error_free_sym;
3301 amt = versymhdr->sh_size;
3302 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3303 || bfd_bread (extversym, amt, abfd) != amt)
3304 goto error_free_vers;
3308 weaks = NULL;
3310 ever = extversym != NULL ? extversym + extsymoff : NULL;
3311 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3312 isym < isymend;
3313 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3315 int bind;
3316 bfd_vma value;
3317 asection *sec;
3318 flagword flags;
3319 const char *name;
3320 struct elf_link_hash_entry *h;
3321 bfd_boolean definition;
3322 bfd_boolean size_change_ok;
3323 bfd_boolean type_change_ok;
3324 bfd_boolean new_weakdef;
3325 bfd_boolean override;
3326 unsigned int old_alignment;
3327 bfd *old_bfd;
3329 override = FALSE;
3331 flags = BSF_NO_FLAGS;
3332 sec = NULL;
3333 value = isym->st_value;
3334 *sym_hash = NULL;
3336 bind = ELF_ST_BIND (isym->st_info);
3337 if (bind == STB_LOCAL)
3339 /* This should be impossible, since ELF requires that all
3340 global symbols follow all local symbols, and that sh_info
3341 point to the first global symbol. Unfortunately, Irix 5
3342 screws this up. */
3343 continue;
3345 else if (bind == STB_GLOBAL)
3347 if (isym->st_shndx != SHN_UNDEF
3348 && isym->st_shndx != SHN_COMMON)
3349 flags = BSF_GLOBAL;
3351 else if (bind == STB_WEAK)
3352 flags = BSF_WEAK;
3353 else
3355 /* Leave it up to the processor backend. */
3358 if (isym->st_shndx == SHN_UNDEF)
3359 sec = bfd_und_section_ptr;
3360 else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
3362 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3363 if (sec == NULL)
3364 sec = bfd_abs_section_ptr;
3365 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3366 value -= sec->vma;
3368 else if (isym->st_shndx == SHN_ABS)
3369 sec = bfd_abs_section_ptr;
3370 else if (isym->st_shndx == SHN_COMMON)
3372 sec = bfd_com_section_ptr;
3373 /* What ELF calls the size we call the value. What ELF
3374 calls the value we call the alignment. */
3375 value = isym->st_size;
3377 else
3379 /* Leave it up to the processor backend. */
3382 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3383 isym->st_name);
3384 if (name == NULL)
3385 goto error_free_vers;
3387 if (isym->st_shndx == SHN_COMMON
3388 && ELF_ST_TYPE (isym->st_info) == STT_TLS)
3390 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3392 if (tcomm == NULL)
3394 tcomm = bfd_make_section (abfd, ".tcommon");
3395 if (tcomm == NULL
3396 || !bfd_set_section_flags (abfd, tcomm, (SEC_ALLOC
3397 | SEC_IS_COMMON
3398 | SEC_LINKER_CREATED
3399 | SEC_THREAD_LOCAL)))
3400 goto error_free_vers;
3402 sec = tcomm;
3404 else if (add_symbol_hook)
3406 if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec,
3407 &value))
3408 goto error_free_vers;
3410 /* The hook function sets the name to NULL if this symbol
3411 should be skipped for some reason. */
3412 if (name == NULL)
3413 continue;
3416 /* Sanity check that all possibilities were handled. */
3417 if (sec == NULL)
3419 bfd_set_error (bfd_error_bad_value);
3420 goto error_free_vers;
3423 if (bfd_is_und_section (sec)
3424 || bfd_is_com_section (sec))
3425 definition = FALSE;
3426 else
3427 definition = TRUE;
3429 size_change_ok = FALSE;
3430 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
3431 old_alignment = 0;
3432 old_bfd = NULL;
3434 if (is_elf_hash_table (hash_table))
3436 Elf_Internal_Versym iver;
3437 unsigned int vernum = 0;
3438 bfd_boolean skip;
3440 if (ever != NULL)
3442 _bfd_elf_swap_versym_in (abfd, ever, &iver);
3443 vernum = iver.vs_vers & VERSYM_VERSION;
3445 /* If this is a hidden symbol, or if it is not version
3446 1, we append the version name to the symbol name.
3447 However, we do not modify a non-hidden absolute
3448 symbol, because it might be the version symbol
3449 itself. FIXME: What if it isn't? */
3450 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
3451 || (vernum > 1 && ! bfd_is_abs_section (sec)))
3453 const char *verstr;
3454 size_t namelen, verlen, newlen;
3455 char *newname, *p;
3457 if (isym->st_shndx != SHN_UNDEF)
3459 if (vernum > elf_tdata (abfd)->dynverdef_hdr.sh_info)
3461 (*_bfd_error_handler)
3462 (_("%B: %s: invalid version %u (max %d)"),
3463 abfd, name, vernum,
3464 elf_tdata (abfd)->dynverdef_hdr.sh_info);
3465 bfd_set_error (bfd_error_bad_value);
3466 goto error_free_vers;
3468 else if (vernum > 1)
3469 verstr =
3470 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
3471 else
3472 verstr = "";
3474 else
3476 /* We cannot simply test for the number of
3477 entries in the VERNEED section since the
3478 numbers for the needed versions do not start
3479 at 0. */
3480 Elf_Internal_Verneed *t;
3482 verstr = NULL;
3483 for (t = elf_tdata (abfd)->verref;
3484 t != NULL;
3485 t = t->vn_nextref)
3487 Elf_Internal_Vernaux *a;
3489 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3491 if (a->vna_other == vernum)
3493 verstr = a->vna_nodename;
3494 break;
3497 if (a != NULL)
3498 break;
3500 if (verstr == NULL)
3502 (*_bfd_error_handler)
3503 (_("%B: %s: invalid needed version %d"),
3504 abfd, name, vernum);
3505 bfd_set_error (bfd_error_bad_value);
3506 goto error_free_vers;
3510 namelen = strlen (name);
3511 verlen = strlen (verstr);
3512 newlen = namelen + verlen + 2;
3513 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3514 && isym->st_shndx != SHN_UNDEF)
3515 ++newlen;
3517 newname = bfd_alloc (abfd, newlen);
3518 if (newname == NULL)
3519 goto error_free_vers;
3520 memcpy (newname, name, namelen);
3521 p = newname + namelen;
3522 *p++ = ELF_VER_CHR;
3523 /* If this is a defined non-hidden version symbol,
3524 we add another @ to the name. This indicates the
3525 default version of the symbol. */
3526 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3527 && isym->st_shndx != SHN_UNDEF)
3528 *p++ = ELF_VER_CHR;
3529 memcpy (p, verstr, verlen + 1);
3531 name = newname;
3535 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
3536 sym_hash, &skip, &override,
3537 &type_change_ok, &size_change_ok))
3538 goto error_free_vers;
3540 if (skip)
3541 continue;
3543 if (override)
3544 definition = FALSE;
3546 h = *sym_hash;
3547 while (h->root.type == bfd_link_hash_indirect
3548 || h->root.type == bfd_link_hash_warning)
3549 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3551 /* Remember the old alignment if this is a common symbol, so
3552 that we don't reduce the alignment later on. We can't
3553 check later, because _bfd_generic_link_add_one_symbol
3554 will set a default for the alignment which we want to
3555 override. We also remember the old bfd where the existing
3556 definition comes from. */
3557 switch (h->root.type)
3559 default:
3560 break;
3562 case bfd_link_hash_defined:
3563 case bfd_link_hash_defweak:
3564 old_bfd = h->root.u.def.section->owner;
3565 break;
3567 case bfd_link_hash_common:
3568 old_bfd = h->root.u.c.p->section->owner;
3569 old_alignment = h->root.u.c.p->alignment_power;
3570 break;
3573 if (elf_tdata (abfd)->verdef != NULL
3574 && ! override
3575 && vernum > 1
3576 && definition)
3577 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
3580 if (! (_bfd_generic_link_add_one_symbol
3581 (info, abfd, name, flags, sec, value, NULL, FALSE, collect,
3582 (struct bfd_link_hash_entry **) sym_hash)))
3583 goto error_free_vers;
3585 h = *sym_hash;
3586 while (h->root.type == bfd_link_hash_indirect
3587 || h->root.type == bfd_link_hash_warning)
3588 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3589 *sym_hash = h;
3591 new_weakdef = FALSE;
3592 if (dynamic
3593 && definition
3594 && (flags & BSF_WEAK) != 0
3595 && ELF_ST_TYPE (isym->st_info) != STT_FUNC
3596 && is_elf_hash_table (hash_table)
3597 && h->weakdef == NULL)
3599 /* Keep a list of all weak defined non function symbols from
3600 a dynamic object, using the weakdef field. Later in this
3601 function we will set the weakdef field to the correct
3602 value. We only put non-function symbols from dynamic
3603 objects on this list, because that happens to be the only
3604 time we need to know the normal symbol corresponding to a
3605 weak symbol, and the information is time consuming to
3606 figure out. If the weakdef field is not already NULL,
3607 then this symbol was already defined by some previous
3608 dynamic object, and we will be using that previous
3609 definition anyhow. */
3611 h->weakdef = weaks;
3612 weaks = h;
3613 new_weakdef = TRUE;
3616 /* Set the alignment of a common symbol. */
3617 if (isym->st_shndx == SHN_COMMON
3618 && h->root.type == bfd_link_hash_common)
3620 unsigned int align;
3622 align = bfd_log2 (isym->st_value);
3623 if (align > old_alignment
3624 /* Permit an alignment power of zero if an alignment of one
3625 is specified and no other alignments have been specified. */
3626 || (isym->st_value == 1 && old_alignment == 0))
3627 h->root.u.c.p->alignment_power = align;
3628 else
3629 h->root.u.c.p->alignment_power = old_alignment;
3632 if (is_elf_hash_table (hash_table))
3634 bfd_boolean dynsym;
3636 /* Check the alignment when a common symbol is involved. This
3637 can change when a common symbol is overridden by a normal
3638 definition or a common symbol is ignored due to the old
3639 normal definition. We need to make sure the maximum
3640 alignment is maintained. */
3641 if ((old_alignment || isym->st_shndx == SHN_COMMON)
3642 && h->root.type != bfd_link_hash_common)
3644 unsigned int common_align;
3645 unsigned int normal_align;
3646 unsigned int symbol_align;
3647 bfd *normal_bfd;
3648 bfd *common_bfd;
3650 symbol_align = ffs (h->root.u.def.value) - 1;
3651 if (h->root.u.def.section->owner != NULL
3652 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3654 normal_align = h->root.u.def.section->alignment_power;
3655 if (normal_align > symbol_align)
3656 normal_align = symbol_align;
3658 else
3659 normal_align = symbol_align;
3661 if (old_alignment)
3663 common_align = old_alignment;
3664 common_bfd = old_bfd;
3665 normal_bfd = abfd;
3667 else
3669 common_align = bfd_log2 (isym->st_value);
3670 common_bfd = abfd;
3671 normal_bfd = old_bfd;
3674 if (normal_align < common_align)
3675 (*_bfd_error_handler)
3676 (_("Warning: alignment %u of symbol `%s' in %B"
3677 " is smaller than %u in %B"),
3678 normal_bfd, common_bfd,
3679 1 << normal_align, name, 1 << common_align);
3682 /* Remember the symbol size and type. */
3683 if (isym->st_size != 0
3684 && (definition || h->size == 0))
3686 if (h->size != 0 && h->size != isym->st_size && ! size_change_ok)
3687 (*_bfd_error_handler)
3688 (_("Warning: size of symbol `%s' changed"
3689 " from %lu in %B to %lu in %B"),
3690 old_bfd, abfd,
3691 name, (unsigned long) h->size,
3692 (unsigned long) isym->st_size);
3694 h->size = isym->st_size;
3697 /* If this is a common symbol, then we always want H->SIZE
3698 to be the size of the common symbol. The code just above
3699 won't fix the size if a common symbol becomes larger. We
3700 don't warn about a size change here, because that is
3701 covered by --warn-common. */
3702 if (h->root.type == bfd_link_hash_common)
3703 h->size = h->root.u.c.size;
3705 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
3706 && (definition || h->type == STT_NOTYPE))
3708 if (h->type != STT_NOTYPE
3709 && h->type != ELF_ST_TYPE (isym->st_info)
3710 && ! type_change_ok)
3711 (*_bfd_error_handler)
3712 (_("Warning: type of symbol `%s' changed"
3713 " from %d to %d in %B"),
3714 abfd, name, h->type, ELF_ST_TYPE (isym->st_info));
3716 h->type = ELF_ST_TYPE (isym->st_info);
3719 /* If st_other has a processor-specific meaning, specific
3720 code might be needed here. We never merge the visibility
3721 attribute with the one from a dynamic object. */
3722 if (bed->elf_backend_merge_symbol_attribute)
3723 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
3724 dynamic);
3726 if (isym->st_other != 0 && !dynamic)
3728 unsigned char hvis, symvis, other, nvis;
3730 /* Take the balance of OTHER from the definition. */
3731 other = (definition ? isym->st_other : h->other);
3732 other &= ~ ELF_ST_VISIBILITY (-1);
3734 /* Combine visibilities, using the most constraining one. */
3735 hvis = ELF_ST_VISIBILITY (h->other);
3736 symvis = ELF_ST_VISIBILITY (isym->st_other);
3737 if (! hvis)
3738 nvis = symvis;
3739 else if (! symvis)
3740 nvis = hvis;
3741 else
3742 nvis = hvis < symvis ? hvis : symvis;
3744 h->other = other | nvis;
3747 /* Set a flag in the hash table entry indicating the type of
3748 reference or definition we just found. Keep a count of
3749 the number of dynamic symbols we find. A dynamic symbol
3750 is one which is referenced or defined by both a regular
3751 object and a shared object. */
3752 dynsym = FALSE;
3753 if (! dynamic)
3755 if (! definition)
3757 h->ref_regular = 1;
3758 if (bind != STB_WEAK)
3759 h->ref_regular_nonweak = 1;
3761 else
3762 h->def_regular = 1;
3763 if (! info->executable
3764 || h->def_dynamic
3765 || h->ref_dynamic)
3766 dynsym = TRUE;
3768 else
3770 if (! definition)
3771 h->ref_dynamic = 1;
3772 else
3773 h->def_dynamic = 1;
3774 if (h->def_regular
3775 || h->ref_regular
3776 || (h->weakdef != NULL
3777 && ! new_weakdef
3778 && h->weakdef->dynindx != -1))
3779 dynsym = TRUE;
3782 /* Check to see if we need to add an indirect symbol for
3783 the default name. */
3784 if (definition || h->root.type == bfd_link_hash_common)
3785 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
3786 &sec, &value, &dynsym,
3787 override))
3788 goto error_free_vers;
3790 if (definition && !dynamic)
3792 char *p = strchr (name, ELF_VER_CHR);
3793 if (p != NULL && p[1] != ELF_VER_CHR)
3795 /* Queue non-default versions so that .symver x, x@FOO
3796 aliases can be checked. */
3797 if (! nondeflt_vers)
3799 amt = (isymend - isym + 1)
3800 * sizeof (struct elf_link_hash_entry *);
3801 nondeflt_vers = bfd_malloc (amt);
3803 nondeflt_vers [nondeflt_vers_cnt++] = h;
3807 if (dynsym && h->dynindx == -1)
3809 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3810 goto error_free_vers;
3811 if (h->weakdef != NULL
3812 && ! new_weakdef
3813 && h->weakdef->dynindx == -1)
3815 if (! bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
3816 goto error_free_vers;
3819 else if (dynsym && h->dynindx != -1)
3820 /* If the symbol already has a dynamic index, but
3821 visibility says it should not be visible, turn it into
3822 a local symbol. */
3823 switch (ELF_ST_VISIBILITY (h->other))
3825 case STV_INTERNAL:
3826 case STV_HIDDEN:
3827 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
3828 dynsym = FALSE;
3829 break;
3832 if (!add_needed
3833 && definition
3834 && dynsym
3835 && h->ref_regular)
3837 int ret;
3838 const char *soname = elf_dt_name (abfd);
3840 /* A symbol from a library loaded via DT_NEEDED of some
3841 other library is referenced by a regular object.
3842 Add a DT_NEEDED entry for it. Issue an error if
3843 --no-add-needed is used. */
3844 if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
3846 (*_bfd_error_handler)
3847 (_("%s: invalid DSO for symbol `%s' definition"),
3848 abfd, name);
3849 bfd_set_error (bfd_error_bad_value);
3850 goto error_free_vers;
3853 add_needed = TRUE;
3854 ret = elf_add_dt_needed_tag (info, soname, add_needed);
3855 if (ret < 0)
3856 goto error_free_vers;
3858 BFD_ASSERT (ret == 0);
3863 /* Now that all the symbols from this input file are created, handle
3864 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
3865 if (nondeflt_vers != NULL)
3867 bfd_size_type cnt, symidx;
3869 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
3871 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
3872 char *shortname, *p;
3874 p = strchr (h->root.root.string, ELF_VER_CHR);
3875 if (p == NULL
3876 || (h->root.type != bfd_link_hash_defined
3877 && h->root.type != bfd_link_hash_defweak))
3878 continue;
3880 amt = p - h->root.root.string;
3881 shortname = bfd_malloc (amt + 1);
3882 memcpy (shortname, h->root.root.string, amt);
3883 shortname[amt] = '\0';
3885 hi = (struct elf_link_hash_entry *)
3886 bfd_link_hash_lookup (&hash_table->root, shortname,
3887 FALSE, FALSE, FALSE);
3888 if (hi != NULL
3889 && hi->root.type == h->root.type
3890 && hi->root.u.def.value == h->root.u.def.value
3891 && hi->root.u.def.section == h->root.u.def.section)
3893 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
3894 hi->root.type = bfd_link_hash_indirect;
3895 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
3896 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
3897 sym_hash = elf_sym_hashes (abfd);
3898 if (sym_hash)
3899 for (symidx = 0; symidx < extsymcount; ++symidx)
3900 if (sym_hash[symidx] == hi)
3902 sym_hash[symidx] = h;
3903 break;
3906 free (shortname);
3908 free (nondeflt_vers);
3909 nondeflt_vers = NULL;
3912 if (extversym != NULL)
3914 free (extversym);
3915 extversym = NULL;
3918 if (isymbuf != NULL)
3919 free (isymbuf);
3920 isymbuf = NULL;
3922 /* Now set the weakdefs field correctly for all the weak defined
3923 symbols we found. The only way to do this is to search all the
3924 symbols. Since we only need the information for non functions in
3925 dynamic objects, that's the only time we actually put anything on
3926 the list WEAKS. We need this information so that if a regular
3927 object refers to a symbol defined weakly in a dynamic object, the
3928 real symbol in the dynamic object is also put in the dynamic
3929 symbols; we also must arrange for both symbols to point to the
3930 same memory location. We could handle the general case of symbol
3931 aliasing, but a general symbol alias can only be generated in
3932 assembler code, handling it correctly would be very time
3933 consuming, and other ELF linkers don't handle general aliasing
3934 either. */
3935 if (weaks != NULL)
3937 struct elf_link_hash_entry **hpp;
3938 struct elf_link_hash_entry **hppend;
3939 struct elf_link_hash_entry **sorted_sym_hash;
3940 struct elf_link_hash_entry *h;
3941 size_t sym_count;
3943 /* Since we have to search the whole symbol list for each weak
3944 defined symbol, search time for N weak defined symbols will be
3945 O(N^2). Binary search will cut it down to O(NlogN). */
3946 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3947 sorted_sym_hash = bfd_malloc (amt);
3948 if (sorted_sym_hash == NULL)
3949 goto error_return;
3950 sym_hash = sorted_sym_hash;
3951 hpp = elf_sym_hashes (abfd);
3952 hppend = hpp + extsymcount;
3953 sym_count = 0;
3954 for (; hpp < hppend; hpp++)
3956 h = *hpp;
3957 if (h != NULL
3958 && h->root.type == bfd_link_hash_defined
3959 && h->type != STT_FUNC)
3961 *sym_hash = h;
3962 sym_hash++;
3963 sym_count++;
3967 qsort (sorted_sym_hash, sym_count,
3968 sizeof (struct elf_link_hash_entry *),
3969 elf_sort_symbol);
3971 while (weaks != NULL)
3973 struct elf_link_hash_entry *hlook;
3974 asection *slook;
3975 bfd_vma vlook;
3976 long ilook;
3977 size_t i, j, idx;
3979 hlook = weaks;
3980 weaks = hlook->weakdef;
3981 hlook->weakdef = NULL;
3983 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
3984 || hlook->root.type == bfd_link_hash_defweak
3985 || hlook->root.type == bfd_link_hash_common
3986 || hlook->root.type == bfd_link_hash_indirect);
3987 slook = hlook->root.u.def.section;
3988 vlook = hlook->root.u.def.value;
3990 ilook = -1;
3991 i = 0;
3992 j = sym_count;
3993 while (i < j)
3995 bfd_signed_vma vdiff;
3996 idx = (i + j) / 2;
3997 h = sorted_sym_hash [idx];
3998 vdiff = vlook - h->root.u.def.value;
3999 if (vdiff < 0)
4000 j = idx;
4001 else if (vdiff > 0)
4002 i = idx + 1;
4003 else
4005 long sdiff = slook->id - h->root.u.def.section->id;
4006 if (sdiff < 0)
4007 j = idx;
4008 else if (sdiff > 0)
4009 i = idx + 1;
4010 else
4012 ilook = idx;
4013 break;
4018 /* We didn't find a value/section match. */
4019 if (ilook == -1)
4020 continue;
4022 for (i = ilook; i < sym_count; i++)
4024 h = sorted_sym_hash [i];
4026 /* Stop if value or section doesn't match. */
4027 if (h->root.u.def.value != vlook
4028 || h->root.u.def.section != slook)
4029 break;
4030 else if (h != hlook)
4032 hlook->weakdef = h;
4034 /* If the weak definition is in the list of dynamic
4035 symbols, make sure the real definition is put
4036 there as well. */
4037 if (hlook->dynindx != -1 && h->dynindx == -1)
4039 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4040 goto error_return;
4043 /* If the real definition is in the list of dynamic
4044 symbols, make sure the weak definition is put
4045 there as well. If we don't do this, then the
4046 dynamic loader might not merge the entries for the
4047 real definition and the weak definition. */
4048 if (h->dynindx != -1 && hlook->dynindx == -1)
4050 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4051 goto error_return;
4053 break;
4058 free (sorted_sym_hash);
4061 check_directives = get_elf_backend_data (abfd)->check_directives;
4062 if (check_directives)
4063 check_directives (abfd, info);
4065 /* If this object is the same format as the output object, and it is
4066 not a shared library, then let the backend look through the
4067 relocs.
4069 This is required to build global offset table entries and to
4070 arrange for dynamic relocs. It is not required for the
4071 particular common case of linking non PIC code, even when linking
4072 against shared libraries, but unfortunately there is no way of
4073 knowing whether an object file has been compiled PIC or not.
4074 Looking through the relocs is not particularly time consuming.
4075 The problem is that we must either (1) keep the relocs in memory,
4076 which causes the linker to require additional runtime memory or
4077 (2) read the relocs twice from the input file, which wastes time.
4078 This would be a good case for using mmap.
4080 I have no idea how to handle linking PIC code into a file of a
4081 different format. It probably can't be done. */
4082 check_relocs = get_elf_backend_data (abfd)->check_relocs;
4083 if (! dynamic
4084 && is_elf_hash_table (hash_table)
4085 && hash_table->root.creator == abfd->xvec
4086 && check_relocs != NULL)
4088 asection *o;
4090 for (o = abfd->sections; o != NULL; o = o->next)
4092 Elf_Internal_Rela *internal_relocs;
4093 bfd_boolean ok;
4095 if ((o->flags & SEC_RELOC) == 0
4096 || o->reloc_count == 0
4097 || ((info->strip == strip_all || info->strip == strip_debugger)
4098 && (o->flags & SEC_DEBUGGING) != 0)
4099 || bfd_is_abs_section (o->output_section))
4100 continue;
4102 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4103 info->keep_memory);
4104 if (internal_relocs == NULL)
4105 goto error_return;
4107 ok = (*check_relocs) (abfd, info, o, internal_relocs);
4109 if (elf_section_data (o)->relocs != internal_relocs)
4110 free (internal_relocs);
4112 if (! ok)
4113 goto error_return;
4117 /* If this is a non-traditional link, try to optimize the handling
4118 of the .stab/.stabstr sections. */
4119 if (! dynamic
4120 && ! info->traditional_format
4121 && is_elf_hash_table (hash_table)
4122 && (info->strip != strip_all && info->strip != strip_debugger))
4124 asection *stabstr;
4126 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4127 if (stabstr != NULL)
4129 bfd_size_type string_offset = 0;
4130 asection *stab;
4132 for (stab = abfd->sections; stab; stab = stab->next)
4133 if (strncmp (".stab", stab->name, 5) == 0
4134 && (!stab->name[5] ||
4135 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4136 && (stab->flags & SEC_MERGE) == 0
4137 && !bfd_is_abs_section (stab->output_section))
4139 struct bfd_elf_section_data *secdata;
4141 secdata = elf_section_data (stab);
4142 if (! _bfd_link_section_stabs (abfd,
4143 &hash_table->stab_info,
4144 stab, stabstr,
4145 &secdata->sec_info,
4146 &string_offset))
4147 goto error_return;
4148 if (secdata->sec_info)
4149 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4154 if (is_elf_hash_table (hash_table))
4156 /* Add this bfd to the loaded list. */
4157 struct elf_link_loaded_list *n;
4159 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4160 if (n == NULL)
4161 goto error_return;
4162 n->abfd = abfd;
4163 n->next = hash_table->loaded;
4164 hash_table->loaded = n;
4167 return TRUE;
4169 error_free_vers:
4170 if (nondeflt_vers != NULL)
4171 free (nondeflt_vers);
4172 if (extversym != NULL)
4173 free (extversym);
4174 error_free_sym:
4175 if (isymbuf != NULL)
4176 free (isymbuf);
4177 error_return:
4178 return FALSE;
4181 /* Return the linker hash table entry of a symbol that might be
4182 satisfied by an archive symbol. Return -1 on error. */
4184 struct elf_link_hash_entry *
4185 _bfd_elf_archive_symbol_lookup (bfd *abfd,
4186 struct bfd_link_info *info,
4187 const char *name)
4189 struct elf_link_hash_entry *h;
4190 char *p, *copy;
4191 size_t len, first;
4193 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4194 if (h != NULL)
4195 return h;
4197 /* If this is a default version (the name contains @@), look up the
4198 symbol again with only one `@' as well as without the version.
4199 The effect is that references to the symbol with and without the
4200 version will be matched by the default symbol in the archive. */
4202 p = strchr (name, ELF_VER_CHR);
4203 if (p == NULL || p[1] != ELF_VER_CHR)
4204 return h;
4206 /* First check with only one `@'. */
4207 len = strlen (name);
4208 copy = bfd_alloc (abfd, len);
4209 if (copy == NULL)
4210 return (struct elf_link_hash_entry *) 0 - 1;
4212 first = p - name + 1;
4213 memcpy (copy, name, first);
4214 memcpy (copy + first, name + first + 1, len - first);
4216 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
4217 if (h == NULL)
4219 /* We also need to check references to the symbol without the
4220 version. */
4221 copy[first - 1] = '\0';
4222 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4223 FALSE, FALSE, FALSE);
4226 bfd_release (abfd, copy);
4227 return h;
4230 /* Add symbols from an ELF archive file to the linker hash table. We
4231 don't use _bfd_generic_link_add_archive_symbols because of a
4232 problem which arises on UnixWare. The UnixWare libc.so is an
4233 archive which includes an entry libc.so.1 which defines a bunch of
4234 symbols. The libc.so archive also includes a number of other
4235 object files, which also define symbols, some of which are the same
4236 as those defined in libc.so.1. Correct linking requires that we
4237 consider each object file in turn, and include it if it defines any
4238 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4239 this; it looks through the list of undefined symbols, and includes
4240 any object file which defines them. When this algorithm is used on
4241 UnixWare, it winds up pulling in libc.so.1 early and defining a
4242 bunch of symbols. This means that some of the other objects in the
4243 archive are not included in the link, which is incorrect since they
4244 precede libc.so.1 in the archive.
4246 Fortunately, ELF archive handling is simpler than that done by
4247 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4248 oddities. In ELF, if we find a symbol in the archive map, and the
4249 symbol is currently undefined, we know that we must pull in that
4250 object file.
4252 Unfortunately, we do have to make multiple passes over the symbol
4253 table until nothing further is resolved. */
4255 static bfd_boolean
4256 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
4258 symindex c;
4259 bfd_boolean *defined = NULL;
4260 bfd_boolean *included = NULL;
4261 carsym *symdefs;
4262 bfd_boolean loop;
4263 bfd_size_type amt;
4264 const struct elf_backend_data *bed;
4265 struct elf_link_hash_entry * (*archive_symbol_lookup)
4266 (bfd *, struct bfd_link_info *, const char *);
4268 if (! bfd_has_map (abfd))
4270 /* An empty archive is a special case. */
4271 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4272 return TRUE;
4273 bfd_set_error (bfd_error_no_armap);
4274 return FALSE;
4277 /* Keep track of all symbols we know to be already defined, and all
4278 files we know to be already included. This is to speed up the
4279 second and subsequent passes. */
4280 c = bfd_ardata (abfd)->symdef_count;
4281 if (c == 0)
4282 return TRUE;
4283 amt = c;
4284 amt *= sizeof (bfd_boolean);
4285 defined = bfd_zmalloc (amt);
4286 included = bfd_zmalloc (amt);
4287 if (defined == NULL || included == NULL)
4288 goto error_return;
4290 symdefs = bfd_ardata (abfd)->symdefs;
4291 bed = get_elf_backend_data (abfd);
4292 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
4296 file_ptr last;
4297 symindex i;
4298 carsym *symdef;
4299 carsym *symdefend;
4301 loop = FALSE;
4302 last = -1;
4304 symdef = symdefs;
4305 symdefend = symdef + c;
4306 for (i = 0; symdef < symdefend; symdef++, i++)
4308 struct elf_link_hash_entry *h;
4309 bfd *element;
4310 struct bfd_link_hash_entry *undefs_tail;
4311 symindex mark;
4313 if (defined[i] || included[i])
4314 continue;
4315 if (symdef->file_offset == last)
4317 included[i] = TRUE;
4318 continue;
4321 h = archive_symbol_lookup (abfd, info, symdef->name);
4322 if (h == (struct elf_link_hash_entry *) 0 - 1)
4323 goto error_return;
4325 if (h == NULL)
4326 continue;
4328 if (h->root.type == bfd_link_hash_common)
4330 /* We currently have a common symbol. The archive map contains
4331 a reference to this symbol, so we may want to include it. We
4332 only want to include it however, if this archive element
4333 contains a definition of the symbol, not just another common
4334 declaration of it.
4336 Unfortunately some archivers (including GNU ar) will put
4337 declarations of common symbols into their archive maps, as
4338 well as real definitions, so we cannot just go by the archive
4339 map alone. Instead we must read in the element's symbol
4340 table and check that to see what kind of symbol definition
4341 this is. */
4342 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
4343 continue;
4345 else if (h->root.type != bfd_link_hash_undefined)
4347 if (h->root.type != bfd_link_hash_undefweak)
4348 defined[i] = TRUE;
4349 continue;
4352 /* We need to include this archive member. */
4353 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4354 if (element == NULL)
4355 goto error_return;
4357 if (! bfd_check_format (element, bfd_object))
4358 goto error_return;
4360 /* Doublecheck that we have not included this object
4361 already--it should be impossible, but there may be
4362 something wrong with the archive. */
4363 if (element->archive_pass != 0)
4365 bfd_set_error (bfd_error_bad_value);
4366 goto error_return;
4368 element->archive_pass = 1;
4370 undefs_tail = info->hash->undefs_tail;
4372 if (! (*info->callbacks->add_archive_element) (info, element,
4373 symdef->name))
4374 goto error_return;
4375 if (! bfd_link_add_symbols (element, info))
4376 goto error_return;
4378 /* If there are any new undefined symbols, we need to make
4379 another pass through the archive in order to see whether
4380 they can be defined. FIXME: This isn't perfect, because
4381 common symbols wind up on undefs_tail and because an
4382 undefined symbol which is defined later on in this pass
4383 does not require another pass. This isn't a bug, but it
4384 does make the code less efficient than it could be. */
4385 if (undefs_tail != info->hash->undefs_tail)
4386 loop = TRUE;
4388 /* Look backward to mark all symbols from this object file
4389 which we have already seen in this pass. */
4390 mark = i;
4393 included[mark] = TRUE;
4394 if (mark == 0)
4395 break;
4396 --mark;
4398 while (symdefs[mark].file_offset == symdef->file_offset);
4400 /* We mark subsequent symbols from this object file as we go
4401 on through the loop. */
4402 last = symdef->file_offset;
4405 while (loop);
4407 free (defined);
4408 free (included);
4410 return TRUE;
4412 error_return:
4413 if (defined != NULL)
4414 free (defined);
4415 if (included != NULL)
4416 free (included);
4417 return FALSE;
4420 /* Given an ELF BFD, add symbols to the global hash table as
4421 appropriate. */
4423 bfd_boolean
4424 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
4426 switch (bfd_get_format (abfd))
4428 case bfd_object:
4429 return elf_link_add_object_symbols (abfd, info);
4430 case bfd_archive:
4431 return elf_link_add_archive_symbols (abfd, info);
4432 default:
4433 bfd_set_error (bfd_error_wrong_format);
4434 return FALSE;
4438 /* This function will be called though elf_link_hash_traverse to store
4439 all hash value of the exported symbols in an array. */
4441 static bfd_boolean
4442 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
4444 unsigned long **valuep = data;
4445 const char *name;
4446 char *p;
4447 unsigned long ha;
4448 char *alc = NULL;
4450 if (h->root.type == bfd_link_hash_warning)
4451 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4453 /* Ignore indirect symbols. These are added by the versioning code. */
4454 if (h->dynindx == -1)
4455 return TRUE;
4457 name = h->root.root.string;
4458 p = strchr (name, ELF_VER_CHR);
4459 if (p != NULL)
4461 alc = bfd_malloc (p - name + 1);
4462 memcpy (alc, name, p - name);
4463 alc[p - name] = '\0';
4464 name = alc;
4467 /* Compute the hash value. */
4468 ha = bfd_elf_hash (name);
4470 /* Store the found hash value in the array given as the argument. */
4471 *(*valuep)++ = ha;
4473 /* And store it in the struct so that we can put it in the hash table
4474 later. */
4475 h->elf_hash_value = ha;
4477 if (alc != NULL)
4478 free (alc);
4480 return TRUE;
4483 /* Array used to determine the number of hash table buckets to use
4484 based on the number of symbols there are. If there are fewer than
4485 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4486 fewer than 37 we use 17 buckets, and so forth. We never use more
4487 than 32771 buckets. */
4489 static const size_t elf_buckets[] =
4491 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4492 16411, 32771, 0
4495 /* Compute bucket count for hashing table. We do not use a static set
4496 of possible tables sizes anymore. Instead we determine for all
4497 possible reasonable sizes of the table the outcome (i.e., the
4498 number of collisions etc) and choose the best solution. The
4499 weighting functions are not too simple to allow the table to grow
4500 without bounds. Instead one of the weighting factors is the size.
4501 Therefore the result is always a good payoff between few collisions
4502 (= short chain lengths) and table size. */
4503 static size_t
4504 compute_bucket_count (struct bfd_link_info *info)
4506 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
4507 size_t best_size = 0;
4508 unsigned long int *hashcodes;
4509 unsigned long int *hashcodesp;
4510 unsigned long int i;
4511 bfd_size_type amt;
4513 /* Compute the hash values for all exported symbols. At the same
4514 time store the values in an array so that we could use them for
4515 optimizations. */
4516 amt = dynsymcount;
4517 amt *= sizeof (unsigned long int);
4518 hashcodes = bfd_malloc (amt);
4519 if (hashcodes == NULL)
4520 return 0;
4521 hashcodesp = hashcodes;
4523 /* Put all hash values in HASHCODES. */
4524 elf_link_hash_traverse (elf_hash_table (info),
4525 elf_collect_hash_codes, &hashcodesp);
4527 /* We have a problem here. The following code to optimize the table
4528 size requires an integer type with more the 32 bits. If
4529 BFD_HOST_U_64_BIT is set we know about such a type. */
4530 #ifdef BFD_HOST_U_64_BIT
4531 if (info->optimize)
4533 unsigned long int nsyms = hashcodesp - hashcodes;
4534 size_t minsize;
4535 size_t maxsize;
4536 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
4537 unsigned long int *counts ;
4538 bfd *dynobj = elf_hash_table (info)->dynobj;
4539 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
4541 /* Possible optimization parameters: if we have NSYMS symbols we say
4542 that the hashing table must at least have NSYMS/4 and at most
4543 2*NSYMS buckets. */
4544 minsize = nsyms / 4;
4545 if (minsize == 0)
4546 minsize = 1;
4547 best_size = maxsize = nsyms * 2;
4549 /* Create array where we count the collisions in. We must use bfd_malloc
4550 since the size could be large. */
4551 amt = maxsize;
4552 amt *= sizeof (unsigned long int);
4553 counts = bfd_malloc (amt);
4554 if (counts == NULL)
4556 free (hashcodes);
4557 return 0;
4560 /* Compute the "optimal" size for the hash table. The criteria is a
4561 minimal chain length. The minor criteria is (of course) the size
4562 of the table. */
4563 for (i = minsize; i < maxsize; ++i)
4565 /* Walk through the array of hashcodes and count the collisions. */
4566 BFD_HOST_U_64_BIT max;
4567 unsigned long int j;
4568 unsigned long int fact;
4570 memset (counts, '\0', i * sizeof (unsigned long int));
4572 /* Determine how often each hash bucket is used. */
4573 for (j = 0; j < nsyms; ++j)
4574 ++counts[hashcodes[j] % i];
4576 /* For the weight function we need some information about the
4577 pagesize on the target. This is information need not be 100%
4578 accurate. Since this information is not available (so far) we
4579 define it here to a reasonable default value. If it is crucial
4580 to have a better value some day simply define this value. */
4581 # ifndef BFD_TARGET_PAGESIZE
4582 # define BFD_TARGET_PAGESIZE (4096)
4583 # endif
4585 /* We in any case need 2 + NSYMS entries for the size values and
4586 the chains. */
4587 max = (2 + nsyms) * (bed->s->arch_size / 8);
4589 # if 1
4590 /* Variant 1: optimize for short chains. We add the squares
4591 of all the chain lengths (which favors many small chain
4592 over a few long chains). */
4593 for (j = 0; j < i; ++j)
4594 max += counts[j] * counts[j];
4596 /* This adds penalties for the overall size of the table. */
4597 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4598 max *= fact * fact;
4599 # else
4600 /* Variant 2: Optimize a lot more for small table. Here we
4601 also add squares of the size but we also add penalties for
4602 empty slots (the +1 term). */
4603 for (j = 0; j < i; ++j)
4604 max += (1 + counts[j]) * (1 + counts[j]);
4606 /* The overall size of the table is considered, but not as
4607 strong as in variant 1, where it is squared. */
4608 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4609 max *= fact;
4610 # endif
4612 /* Compare with current best results. */
4613 if (max < best_chlen)
4615 best_chlen = max;
4616 best_size = i;
4620 free (counts);
4622 else
4623 #endif /* defined (BFD_HOST_U_64_BIT) */
4625 /* This is the fallback solution if no 64bit type is available or if we
4626 are not supposed to spend much time on optimizations. We select the
4627 bucket count using a fixed set of numbers. */
4628 for (i = 0; elf_buckets[i] != 0; i++)
4630 best_size = elf_buckets[i];
4631 if (dynsymcount < elf_buckets[i + 1])
4632 break;
4636 /* Free the arrays we needed. */
4637 free (hashcodes);
4639 return best_size;
4642 /* Set up the sizes and contents of the ELF dynamic sections. This is
4643 called by the ELF linker emulation before_allocation routine. We
4644 must set the sizes of the sections before the linker sets the
4645 addresses of the various sections. */
4647 bfd_boolean
4648 bfd_elf_size_dynamic_sections (bfd *output_bfd,
4649 const char *soname,
4650 const char *rpath,
4651 const char *filter_shlib,
4652 const char * const *auxiliary_filters,
4653 struct bfd_link_info *info,
4654 asection **sinterpptr,
4655 struct bfd_elf_version_tree *verdefs)
4657 bfd_size_type soname_indx;
4658 bfd *dynobj;
4659 const struct elf_backend_data *bed;
4660 struct elf_assign_sym_version_info asvinfo;
4662 *sinterpptr = NULL;
4664 soname_indx = (bfd_size_type) -1;
4666 if (!is_elf_hash_table (info->hash))
4667 return TRUE;
4669 elf_tdata (output_bfd)->relro = info->relro;
4670 if (info->execstack)
4671 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
4672 else if (info->noexecstack)
4673 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
4674 else
4676 bfd *inputobj;
4677 asection *notesec = NULL;
4678 int exec = 0;
4680 for (inputobj = info->input_bfds;
4681 inputobj;
4682 inputobj = inputobj->link_next)
4684 asection *s;
4686 if (inputobj->flags & DYNAMIC)
4687 continue;
4688 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
4689 if (s)
4691 if (s->flags & SEC_CODE)
4692 exec = PF_X;
4693 notesec = s;
4695 else
4696 exec = PF_X;
4698 if (notesec)
4700 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
4701 if (exec && info->relocatable
4702 && notesec->output_section != bfd_abs_section_ptr)
4703 notesec->output_section->flags |= SEC_CODE;
4707 /* Any syms created from now on start with -1 in
4708 got.refcount/offset and plt.refcount/offset. */
4709 elf_hash_table (info)->init_refcount = elf_hash_table (info)->init_offset;
4711 /* The backend may have to create some sections regardless of whether
4712 we're dynamic or not. */
4713 bed = get_elf_backend_data (output_bfd);
4714 if (bed->elf_backend_always_size_sections
4715 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
4716 return FALSE;
4718 dynobj = elf_hash_table (info)->dynobj;
4720 /* If there were no dynamic objects in the link, there is nothing to
4721 do here. */
4722 if (dynobj == NULL)
4723 return TRUE;
4725 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
4726 return FALSE;
4728 if (elf_hash_table (info)->dynamic_sections_created)
4730 struct elf_info_failed eif;
4731 struct elf_link_hash_entry *h;
4732 asection *dynstr;
4733 struct bfd_elf_version_tree *t;
4734 struct bfd_elf_version_expr *d;
4735 bfd_boolean all_defined;
4737 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
4738 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
4740 if (soname != NULL)
4742 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
4743 soname, TRUE);
4744 if (soname_indx == (bfd_size_type) -1
4745 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
4746 return FALSE;
4749 if (info->symbolic)
4751 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
4752 return FALSE;
4753 info->flags |= DF_SYMBOLIC;
4756 if (rpath != NULL)
4758 bfd_size_type indx;
4760 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
4761 TRUE);
4762 if (indx == (bfd_size_type) -1
4763 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
4764 return FALSE;
4766 if (info->new_dtags)
4768 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
4769 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
4770 return FALSE;
4774 if (filter_shlib != NULL)
4776 bfd_size_type indx;
4778 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
4779 filter_shlib, TRUE);
4780 if (indx == (bfd_size_type) -1
4781 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
4782 return FALSE;
4785 if (auxiliary_filters != NULL)
4787 const char * const *p;
4789 for (p = auxiliary_filters; *p != NULL; p++)
4791 bfd_size_type indx;
4793 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
4794 *p, TRUE);
4795 if (indx == (bfd_size_type) -1
4796 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
4797 return FALSE;
4801 eif.info = info;
4802 eif.verdefs = verdefs;
4803 eif.failed = FALSE;
4805 /* If we are supposed to export all symbols into the dynamic symbol
4806 table (this is not the normal case), then do so. */
4807 if (info->export_dynamic)
4809 elf_link_hash_traverse (elf_hash_table (info),
4810 _bfd_elf_export_symbol,
4811 &eif);
4812 if (eif.failed)
4813 return FALSE;
4816 /* Make all global versions with definition. */
4817 for (t = verdefs; t != NULL; t = t->next)
4818 for (d = t->globals.list; d != NULL; d = d->next)
4819 if (!d->symver && d->symbol)
4821 const char *verstr, *name;
4822 size_t namelen, verlen, newlen;
4823 char *newname, *p;
4824 struct elf_link_hash_entry *newh;
4826 name = d->symbol;
4827 namelen = strlen (name);
4828 verstr = t->name;
4829 verlen = strlen (verstr);
4830 newlen = namelen + verlen + 3;
4832 newname = bfd_malloc (newlen);
4833 if (newname == NULL)
4834 return FALSE;
4835 memcpy (newname, name, namelen);
4837 /* Check the hidden versioned definition. */
4838 p = newname + namelen;
4839 *p++ = ELF_VER_CHR;
4840 memcpy (p, verstr, verlen + 1);
4841 newh = elf_link_hash_lookup (elf_hash_table (info),
4842 newname, FALSE, FALSE,
4843 FALSE);
4844 if (newh == NULL
4845 || (newh->root.type != bfd_link_hash_defined
4846 && newh->root.type != bfd_link_hash_defweak))
4848 /* Check the default versioned definition. */
4849 *p++ = ELF_VER_CHR;
4850 memcpy (p, verstr, verlen + 1);
4851 newh = elf_link_hash_lookup (elf_hash_table (info),
4852 newname, FALSE, FALSE,
4853 FALSE);
4855 free (newname);
4857 /* Mark this version if there is a definition and it is
4858 not defined in a shared object. */
4859 if (newh != NULL
4860 && !newh->def_dynamic
4861 && (newh->root.type == bfd_link_hash_defined
4862 || newh->root.type == bfd_link_hash_defweak))
4863 d->symver = 1;
4866 /* Attach all the symbols to their version information. */
4867 asvinfo.output_bfd = output_bfd;
4868 asvinfo.info = info;
4869 asvinfo.verdefs = verdefs;
4870 asvinfo.failed = FALSE;
4872 elf_link_hash_traverse (elf_hash_table (info),
4873 _bfd_elf_link_assign_sym_version,
4874 &asvinfo);
4875 if (asvinfo.failed)
4876 return FALSE;
4878 if (!info->allow_undefined_version)
4880 /* Check if all global versions have a definition. */
4881 all_defined = TRUE;
4882 for (t = verdefs; t != NULL; t = t->next)
4883 for (d = t->globals.list; d != NULL; d = d->next)
4884 if (!d->symver && !d->script)
4886 (*_bfd_error_handler)
4887 (_("%s: undefined version: %s"),
4888 d->pattern, t->name);
4889 all_defined = FALSE;
4892 if (!all_defined)
4894 bfd_set_error (bfd_error_bad_value);
4895 return FALSE;
4899 /* Find all symbols which were defined in a dynamic object and make
4900 the backend pick a reasonable value for them. */
4901 elf_link_hash_traverse (elf_hash_table (info),
4902 _bfd_elf_adjust_dynamic_symbol,
4903 &eif);
4904 if (eif.failed)
4905 return FALSE;
4907 /* Add some entries to the .dynamic section. We fill in some of the
4908 values later, in bfd_elf_final_link, but we must add the entries
4909 now so that we know the final size of the .dynamic section. */
4911 /* If there are initialization and/or finalization functions to
4912 call then add the corresponding DT_INIT/DT_FINI entries. */
4913 h = (info->init_function
4914 ? elf_link_hash_lookup (elf_hash_table (info),
4915 info->init_function, FALSE,
4916 FALSE, FALSE)
4917 : NULL);
4918 if (h != NULL
4919 && (h->ref_regular
4920 || h->def_regular))
4922 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
4923 return FALSE;
4925 h = (info->fini_function
4926 ? elf_link_hash_lookup (elf_hash_table (info),
4927 info->fini_function, FALSE,
4928 FALSE, FALSE)
4929 : NULL);
4930 if (h != NULL
4931 && (h->ref_regular
4932 || h->def_regular))
4934 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
4935 return FALSE;
4938 if (bfd_get_section_by_name (output_bfd, ".preinit_array") != NULL)
4940 /* DT_PREINIT_ARRAY is not allowed in shared library. */
4941 if (! info->executable)
4943 bfd *sub;
4944 asection *o;
4946 for (sub = info->input_bfds; sub != NULL;
4947 sub = sub->link_next)
4948 for (o = sub->sections; o != NULL; o = o->next)
4949 if (elf_section_data (o)->this_hdr.sh_type
4950 == SHT_PREINIT_ARRAY)
4952 (*_bfd_error_handler)
4953 (_("%B: .preinit_array section is not allowed in DSO"),
4954 sub);
4955 break;
4958 bfd_set_error (bfd_error_nonrepresentable_section);
4959 return FALSE;
4962 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
4963 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
4964 return FALSE;
4966 if (bfd_get_section_by_name (output_bfd, ".init_array") != NULL)
4968 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
4969 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
4970 return FALSE;
4972 if (bfd_get_section_by_name (output_bfd, ".fini_array") != NULL)
4974 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
4975 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
4976 return FALSE;
4979 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
4980 /* If .dynstr is excluded from the link, we don't want any of
4981 these tags. Strictly, we should be checking each section
4982 individually; This quick check covers for the case where
4983 someone does a /DISCARD/ : { *(*) }. */
4984 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
4986 bfd_size_type strsize;
4988 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
4989 if (!_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)
4990 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
4991 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
4992 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
4993 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
4994 bed->s->sizeof_sym))
4995 return FALSE;
4999 /* The backend must work out the sizes of all the other dynamic
5000 sections. */
5001 if (bed->elf_backend_size_dynamic_sections
5002 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5003 return FALSE;
5005 if (elf_hash_table (info)->dynamic_sections_created)
5007 bfd_size_type dynsymcount;
5008 asection *s;
5009 size_t bucketcount = 0;
5010 size_t hash_entry_size;
5011 unsigned int dtagcount;
5013 /* Set up the version definition section. */
5014 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5015 BFD_ASSERT (s != NULL);
5017 /* We may have created additional version definitions if we are
5018 just linking a regular application. */
5019 verdefs = asvinfo.verdefs;
5021 /* Skip anonymous version tag. */
5022 if (verdefs != NULL && verdefs->vernum == 0)
5023 verdefs = verdefs->next;
5025 if (verdefs == NULL)
5026 _bfd_strip_section_from_output (info, s);
5027 else
5029 unsigned int cdefs;
5030 bfd_size_type size;
5031 struct bfd_elf_version_tree *t;
5032 bfd_byte *p;
5033 Elf_Internal_Verdef def;
5034 Elf_Internal_Verdaux defaux;
5036 cdefs = 0;
5037 size = 0;
5039 /* Make space for the base version. */
5040 size += sizeof (Elf_External_Verdef);
5041 size += sizeof (Elf_External_Verdaux);
5042 ++cdefs;
5044 for (t = verdefs; t != NULL; t = t->next)
5046 struct bfd_elf_version_deps *n;
5048 size += sizeof (Elf_External_Verdef);
5049 size += sizeof (Elf_External_Verdaux);
5050 ++cdefs;
5052 for (n = t->deps; n != NULL; n = n->next)
5053 size += sizeof (Elf_External_Verdaux);
5056 s->size = size;
5057 s->contents = bfd_alloc (output_bfd, s->size);
5058 if (s->contents == NULL && s->size != 0)
5059 return FALSE;
5061 /* Fill in the version definition section. */
5063 p = s->contents;
5065 def.vd_version = VER_DEF_CURRENT;
5066 def.vd_flags = VER_FLG_BASE;
5067 def.vd_ndx = 1;
5068 def.vd_cnt = 1;
5069 def.vd_aux = sizeof (Elf_External_Verdef);
5070 def.vd_next = (sizeof (Elf_External_Verdef)
5071 + sizeof (Elf_External_Verdaux));
5073 if (soname_indx != (bfd_size_type) -1)
5075 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5076 soname_indx);
5077 def.vd_hash = bfd_elf_hash (soname);
5078 defaux.vda_name = soname_indx;
5080 else
5082 const char *name;
5083 bfd_size_type indx;
5085 name = basename (output_bfd->filename);
5086 def.vd_hash = bfd_elf_hash (name);
5087 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5088 name, FALSE);
5089 if (indx == (bfd_size_type) -1)
5090 return FALSE;
5091 defaux.vda_name = indx;
5093 defaux.vda_next = 0;
5095 _bfd_elf_swap_verdef_out (output_bfd, &def,
5096 (Elf_External_Verdef *) p);
5097 p += sizeof (Elf_External_Verdef);
5098 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5099 (Elf_External_Verdaux *) p);
5100 p += sizeof (Elf_External_Verdaux);
5102 for (t = verdefs; t != NULL; t = t->next)
5104 unsigned int cdeps;
5105 struct bfd_elf_version_deps *n;
5106 struct elf_link_hash_entry *h;
5107 struct bfd_link_hash_entry *bh;
5109 cdeps = 0;
5110 for (n = t->deps; n != NULL; n = n->next)
5111 ++cdeps;
5113 /* Add a symbol representing this version. */
5114 bh = NULL;
5115 if (! (_bfd_generic_link_add_one_symbol
5116 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5117 0, NULL, FALSE,
5118 get_elf_backend_data (dynobj)->collect, &bh)))
5119 return FALSE;
5120 h = (struct elf_link_hash_entry *) bh;
5121 h->non_elf = 0;
5122 h->def_regular = 1;
5123 h->type = STT_OBJECT;
5124 h->verinfo.vertree = t;
5126 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5127 return FALSE;
5129 def.vd_version = VER_DEF_CURRENT;
5130 def.vd_flags = 0;
5131 if (t->globals.list == NULL
5132 && t->locals.list == NULL
5133 && ! t->used)
5134 def.vd_flags |= VER_FLG_WEAK;
5135 def.vd_ndx = t->vernum + 1;
5136 def.vd_cnt = cdeps + 1;
5137 def.vd_hash = bfd_elf_hash (t->name);
5138 def.vd_aux = sizeof (Elf_External_Verdef);
5139 def.vd_next = 0;
5140 if (t->next != NULL)
5141 def.vd_next = (sizeof (Elf_External_Verdef)
5142 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5144 _bfd_elf_swap_verdef_out (output_bfd, &def,
5145 (Elf_External_Verdef *) p);
5146 p += sizeof (Elf_External_Verdef);
5148 defaux.vda_name = h->dynstr_index;
5149 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5150 h->dynstr_index);
5151 defaux.vda_next = 0;
5152 if (t->deps != NULL)
5153 defaux.vda_next = sizeof (Elf_External_Verdaux);
5154 t->name_indx = defaux.vda_name;
5156 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5157 (Elf_External_Verdaux *) p);
5158 p += sizeof (Elf_External_Verdaux);
5160 for (n = t->deps; n != NULL; n = n->next)
5162 if (n->version_needed == NULL)
5164 /* This can happen if there was an error in the
5165 version script. */
5166 defaux.vda_name = 0;
5168 else
5170 defaux.vda_name = n->version_needed->name_indx;
5171 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5172 defaux.vda_name);
5174 if (n->next == NULL)
5175 defaux.vda_next = 0;
5176 else
5177 defaux.vda_next = sizeof (Elf_External_Verdaux);
5179 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5180 (Elf_External_Verdaux *) p);
5181 p += sizeof (Elf_External_Verdaux);
5185 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
5186 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
5187 return FALSE;
5189 elf_tdata (output_bfd)->cverdefs = cdefs;
5192 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
5194 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
5195 return FALSE;
5197 else if (info->flags & DF_BIND_NOW)
5199 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
5200 return FALSE;
5203 if (info->flags_1)
5205 if (info->executable)
5206 info->flags_1 &= ~ (DF_1_INITFIRST
5207 | DF_1_NODELETE
5208 | DF_1_NOOPEN);
5209 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
5210 return FALSE;
5213 /* Work out the size of the version reference section. */
5215 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
5216 BFD_ASSERT (s != NULL);
5218 struct elf_find_verdep_info sinfo;
5220 sinfo.output_bfd = output_bfd;
5221 sinfo.info = info;
5222 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
5223 if (sinfo.vers == 0)
5224 sinfo.vers = 1;
5225 sinfo.failed = FALSE;
5227 elf_link_hash_traverse (elf_hash_table (info),
5228 _bfd_elf_link_find_version_dependencies,
5229 &sinfo);
5231 if (elf_tdata (output_bfd)->verref == NULL)
5232 _bfd_strip_section_from_output (info, s);
5233 else
5235 Elf_Internal_Verneed *t;
5236 unsigned int size;
5237 unsigned int crefs;
5238 bfd_byte *p;
5240 /* Build the version definition section. */
5241 size = 0;
5242 crefs = 0;
5243 for (t = elf_tdata (output_bfd)->verref;
5244 t != NULL;
5245 t = t->vn_nextref)
5247 Elf_Internal_Vernaux *a;
5249 size += sizeof (Elf_External_Verneed);
5250 ++crefs;
5251 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5252 size += sizeof (Elf_External_Vernaux);
5255 s->size = size;
5256 s->contents = bfd_alloc (output_bfd, s->size);
5257 if (s->contents == NULL)
5258 return FALSE;
5260 p = s->contents;
5261 for (t = elf_tdata (output_bfd)->verref;
5262 t != NULL;
5263 t = t->vn_nextref)
5265 unsigned int caux;
5266 Elf_Internal_Vernaux *a;
5267 bfd_size_type indx;
5269 caux = 0;
5270 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5271 ++caux;
5273 t->vn_version = VER_NEED_CURRENT;
5274 t->vn_cnt = caux;
5275 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5276 elf_dt_name (t->vn_bfd) != NULL
5277 ? elf_dt_name (t->vn_bfd)
5278 : basename (t->vn_bfd->filename),
5279 FALSE);
5280 if (indx == (bfd_size_type) -1)
5281 return FALSE;
5282 t->vn_file = indx;
5283 t->vn_aux = sizeof (Elf_External_Verneed);
5284 if (t->vn_nextref == NULL)
5285 t->vn_next = 0;
5286 else
5287 t->vn_next = (sizeof (Elf_External_Verneed)
5288 + caux * sizeof (Elf_External_Vernaux));
5290 _bfd_elf_swap_verneed_out (output_bfd, t,
5291 (Elf_External_Verneed *) p);
5292 p += sizeof (Elf_External_Verneed);
5294 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5296 a->vna_hash = bfd_elf_hash (a->vna_nodename);
5297 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5298 a->vna_nodename, FALSE);
5299 if (indx == (bfd_size_type) -1)
5300 return FALSE;
5301 a->vna_name = indx;
5302 if (a->vna_nextptr == NULL)
5303 a->vna_next = 0;
5304 else
5305 a->vna_next = sizeof (Elf_External_Vernaux);
5307 _bfd_elf_swap_vernaux_out (output_bfd, a,
5308 (Elf_External_Vernaux *) p);
5309 p += sizeof (Elf_External_Vernaux);
5313 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
5314 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
5315 return FALSE;
5317 elf_tdata (output_bfd)->cverrefs = crefs;
5321 /* Assign dynsym indicies. In a shared library we generate a
5322 section symbol for each output section, which come first.
5323 Next come all of the back-end allocated local dynamic syms,
5324 followed by the rest of the global symbols. */
5326 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
5328 /* Work out the size of the symbol version section. */
5329 s = bfd_get_section_by_name (dynobj, ".gnu.version");
5330 BFD_ASSERT (s != NULL);
5331 if (dynsymcount == 0
5332 || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL))
5334 _bfd_strip_section_from_output (info, s);
5335 /* The DYNSYMCOUNT might have changed if we were going to
5336 output a dynamic symbol table entry for S. */
5337 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
5339 else
5341 s->size = dynsymcount * sizeof (Elf_External_Versym);
5342 s->contents = bfd_zalloc (output_bfd, s->size);
5343 if (s->contents == NULL)
5344 return FALSE;
5346 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
5347 return FALSE;
5350 /* Set the size of the .dynsym and .hash sections. We counted
5351 the number of dynamic symbols in elf_link_add_object_symbols.
5352 We will build the contents of .dynsym and .hash when we build
5353 the final symbol table, because until then we do not know the
5354 correct value to give the symbols. We built the .dynstr
5355 section as we went along in elf_link_add_object_symbols. */
5356 s = bfd_get_section_by_name (dynobj, ".dynsym");
5357 BFD_ASSERT (s != NULL);
5358 s->size = dynsymcount * bed->s->sizeof_sym;
5359 s->contents = bfd_alloc (output_bfd, s->size);
5360 if (s->contents == NULL && s->size != 0)
5361 return FALSE;
5363 if (dynsymcount != 0)
5365 Elf_Internal_Sym isym;
5367 /* The first entry in .dynsym is a dummy symbol. */
5368 isym.st_value = 0;
5369 isym.st_size = 0;
5370 isym.st_name = 0;
5371 isym.st_info = 0;
5372 isym.st_other = 0;
5373 isym.st_shndx = 0;
5374 bed->s->swap_symbol_out (output_bfd, &isym, s->contents, 0);
5377 /* Compute the size of the hashing table. As a side effect this
5378 computes the hash values for all the names we export. */
5379 bucketcount = compute_bucket_count (info);
5381 s = bfd_get_section_by_name (dynobj, ".hash");
5382 BFD_ASSERT (s != NULL);
5383 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
5384 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
5385 s->contents = bfd_zalloc (output_bfd, s->size);
5386 if (s->contents == NULL)
5387 return FALSE;
5389 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
5390 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
5391 s->contents + hash_entry_size);
5393 elf_hash_table (info)->bucketcount = bucketcount;
5395 s = bfd_get_section_by_name (dynobj, ".dynstr");
5396 BFD_ASSERT (s != NULL);
5398 elf_finalize_dynstr (output_bfd, info);
5400 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5402 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
5403 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
5404 return FALSE;
5407 return TRUE;
5410 /* Final phase of ELF linker. */
5412 /* A structure we use to avoid passing large numbers of arguments. */
5414 struct elf_final_link_info
5416 /* General link information. */
5417 struct bfd_link_info *info;
5418 /* Output BFD. */
5419 bfd *output_bfd;
5420 /* Symbol string table. */
5421 struct bfd_strtab_hash *symstrtab;
5422 /* .dynsym section. */
5423 asection *dynsym_sec;
5424 /* .hash section. */
5425 asection *hash_sec;
5426 /* symbol version section (.gnu.version). */
5427 asection *symver_sec;
5428 /* Buffer large enough to hold contents of any section. */
5429 bfd_byte *contents;
5430 /* Buffer large enough to hold external relocs of any section. */
5431 void *external_relocs;
5432 /* Buffer large enough to hold internal relocs of any section. */
5433 Elf_Internal_Rela *internal_relocs;
5434 /* Buffer large enough to hold external local symbols of any input
5435 BFD. */
5436 bfd_byte *external_syms;
5437 /* And a buffer for symbol section indices. */
5438 Elf_External_Sym_Shndx *locsym_shndx;
5439 /* Buffer large enough to hold internal local symbols of any input
5440 BFD. */
5441 Elf_Internal_Sym *internal_syms;
5442 /* Array large enough to hold a symbol index for each local symbol
5443 of any input BFD. */
5444 long *indices;
5445 /* Array large enough to hold a section pointer for each local
5446 symbol of any input BFD. */
5447 asection **sections;
5448 /* Buffer to hold swapped out symbols. */
5449 bfd_byte *symbuf;
5450 /* And one for symbol section indices. */
5451 Elf_External_Sym_Shndx *symshndxbuf;
5452 /* Number of swapped out symbols in buffer. */
5453 size_t symbuf_count;
5454 /* Number of symbols which fit in symbuf. */
5455 size_t symbuf_size;
5456 /* And same for symshndxbuf. */
5457 size_t shndxbuf_size;
5460 /* This struct is used to pass information to elf_link_output_extsym. */
5462 struct elf_outext_info
5464 bfd_boolean failed;
5465 bfd_boolean localsyms;
5466 struct elf_final_link_info *finfo;
5469 /* When performing a relocatable link, the input relocations are
5470 preserved. But, if they reference global symbols, the indices
5471 referenced must be updated. Update all the relocations in
5472 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5474 static void
5475 elf_link_adjust_relocs (bfd *abfd,
5476 Elf_Internal_Shdr *rel_hdr,
5477 unsigned int count,
5478 struct elf_link_hash_entry **rel_hash)
5480 unsigned int i;
5481 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5482 bfd_byte *erela;
5483 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5484 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5485 bfd_vma r_type_mask;
5486 int r_sym_shift;
5488 if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
5490 swap_in = bed->s->swap_reloc_in;
5491 swap_out = bed->s->swap_reloc_out;
5493 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
5495 swap_in = bed->s->swap_reloca_in;
5496 swap_out = bed->s->swap_reloca_out;
5498 else
5499 abort ();
5501 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
5502 abort ();
5504 if (bed->s->arch_size == 32)
5506 r_type_mask = 0xff;
5507 r_sym_shift = 8;
5509 else
5511 r_type_mask = 0xffffffff;
5512 r_sym_shift = 32;
5515 erela = rel_hdr->contents;
5516 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
5518 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
5519 unsigned int j;
5521 if (*rel_hash == NULL)
5522 continue;
5524 BFD_ASSERT ((*rel_hash)->indx >= 0);
5526 (*swap_in) (abfd, erela, irela);
5527 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
5528 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
5529 | (irela[j].r_info & r_type_mask));
5530 (*swap_out) (abfd, irela, erela);
5534 struct elf_link_sort_rela
5536 union {
5537 bfd_vma offset;
5538 bfd_vma sym_mask;
5539 } u;
5540 enum elf_reloc_type_class type;
5541 /* We use this as an array of size int_rels_per_ext_rel. */
5542 Elf_Internal_Rela rela[1];
5545 static int
5546 elf_link_sort_cmp1 (const void *A, const void *B)
5548 const struct elf_link_sort_rela *a = A;
5549 const struct elf_link_sort_rela *b = B;
5550 int relativea, relativeb;
5552 relativea = a->type == reloc_class_relative;
5553 relativeb = b->type == reloc_class_relative;
5555 if (relativea < relativeb)
5556 return 1;
5557 if (relativea > relativeb)
5558 return -1;
5559 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
5560 return -1;
5561 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
5562 return 1;
5563 if (a->rela->r_offset < b->rela->r_offset)
5564 return -1;
5565 if (a->rela->r_offset > b->rela->r_offset)
5566 return 1;
5567 return 0;
5570 static int
5571 elf_link_sort_cmp2 (const void *A, const void *B)
5573 const struct elf_link_sort_rela *a = A;
5574 const struct elf_link_sort_rela *b = B;
5575 int copya, copyb;
5577 if (a->u.offset < b->u.offset)
5578 return -1;
5579 if (a->u.offset > b->u.offset)
5580 return 1;
5581 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
5582 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
5583 if (copya < copyb)
5584 return -1;
5585 if (copya > copyb)
5586 return 1;
5587 if (a->rela->r_offset < b->rela->r_offset)
5588 return -1;
5589 if (a->rela->r_offset > b->rela->r_offset)
5590 return 1;
5591 return 0;
5594 static size_t
5595 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
5597 asection *reldyn;
5598 bfd_size_type count, size;
5599 size_t i, ret, sort_elt, ext_size;
5600 bfd_byte *sort, *s_non_relative, *p;
5601 struct elf_link_sort_rela *sq;
5602 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5603 int i2e = bed->s->int_rels_per_ext_rel;
5604 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5605 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5606 struct bfd_link_order *lo;
5607 bfd_vma r_sym_mask;
5609 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
5610 if (reldyn == NULL || reldyn->size == 0)
5612 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
5613 if (reldyn == NULL || reldyn->size == 0)
5614 return 0;
5615 ext_size = bed->s->sizeof_rel;
5616 swap_in = bed->s->swap_reloc_in;
5617 swap_out = bed->s->swap_reloc_out;
5619 else
5621 ext_size = bed->s->sizeof_rela;
5622 swap_in = bed->s->swap_reloca_in;
5623 swap_out = bed->s->swap_reloca_out;
5625 count = reldyn->size / ext_size;
5627 size = 0;
5628 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
5629 if (lo->type == bfd_indirect_link_order)
5631 asection *o = lo->u.indirect.section;
5632 size += o->size;
5635 if (size != reldyn->size)
5636 return 0;
5638 sort_elt = (sizeof (struct elf_link_sort_rela)
5639 + (i2e - 1) * sizeof (Elf_Internal_Rela));
5640 sort = bfd_zmalloc (sort_elt * count);
5641 if (sort == NULL)
5643 (*info->callbacks->warning)
5644 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
5645 return 0;
5648 if (bed->s->arch_size == 32)
5649 r_sym_mask = ~(bfd_vma) 0xff;
5650 else
5651 r_sym_mask = ~(bfd_vma) 0xffffffff;
5653 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
5654 if (lo->type == bfd_indirect_link_order)
5656 bfd_byte *erel, *erelend;
5657 asection *o = lo->u.indirect.section;
5659 erel = o->contents;
5660 erelend = o->contents + o->size;
5661 p = sort + o->output_offset / ext_size * sort_elt;
5662 while (erel < erelend)
5664 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
5665 (*swap_in) (abfd, erel, s->rela);
5666 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
5667 s->u.sym_mask = r_sym_mask;
5668 p += sort_elt;
5669 erel += ext_size;
5673 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
5675 for (i = 0, p = sort; i < count; i++, p += sort_elt)
5677 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
5678 if (s->type != reloc_class_relative)
5679 break;
5681 ret = i;
5682 s_non_relative = p;
5684 sq = (struct elf_link_sort_rela *) s_non_relative;
5685 for (; i < count; i++, p += sort_elt)
5687 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
5688 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
5689 sq = sp;
5690 sp->u.offset = sq->rela->r_offset;
5693 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
5695 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
5696 if (lo->type == bfd_indirect_link_order)
5698 bfd_byte *erel, *erelend;
5699 asection *o = lo->u.indirect.section;
5701 erel = o->contents;
5702 erelend = o->contents + o->size;
5703 p = sort + o->output_offset / ext_size * sort_elt;
5704 while (erel < erelend)
5706 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
5707 (*swap_out) (abfd, s->rela, erel);
5708 p += sort_elt;
5709 erel += ext_size;
5713 free (sort);
5714 *psec = reldyn;
5715 return ret;
5718 /* Flush the output symbols to the file. */
5720 static bfd_boolean
5721 elf_link_flush_output_syms (struct elf_final_link_info *finfo,
5722 const struct elf_backend_data *bed)
5724 if (finfo->symbuf_count > 0)
5726 Elf_Internal_Shdr *hdr;
5727 file_ptr pos;
5728 bfd_size_type amt;
5730 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
5731 pos = hdr->sh_offset + hdr->sh_size;
5732 amt = finfo->symbuf_count * bed->s->sizeof_sym;
5733 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
5734 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
5735 return FALSE;
5737 hdr->sh_size += amt;
5738 finfo->symbuf_count = 0;
5741 return TRUE;
5744 /* Add a symbol to the output symbol table. */
5746 static bfd_boolean
5747 elf_link_output_sym (struct elf_final_link_info *finfo,
5748 const char *name,
5749 Elf_Internal_Sym *elfsym,
5750 asection *input_sec,
5751 struct elf_link_hash_entry *h)
5753 bfd_byte *dest;
5754 Elf_External_Sym_Shndx *destshndx;
5755 bfd_boolean (*output_symbol_hook)
5756 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
5757 struct elf_link_hash_entry *);
5758 const struct elf_backend_data *bed;
5760 bed = get_elf_backend_data (finfo->output_bfd);
5761 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
5762 if (output_symbol_hook != NULL)
5764 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
5765 return FALSE;
5768 if (name == NULL || *name == '\0')
5769 elfsym->st_name = 0;
5770 else if (input_sec->flags & SEC_EXCLUDE)
5771 elfsym->st_name = 0;
5772 else
5774 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
5775 name, TRUE, FALSE);
5776 if (elfsym->st_name == (unsigned long) -1)
5777 return FALSE;
5780 if (finfo->symbuf_count >= finfo->symbuf_size)
5782 if (! elf_link_flush_output_syms (finfo, bed))
5783 return FALSE;
5786 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
5787 destshndx = finfo->symshndxbuf;
5788 if (destshndx != NULL)
5790 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
5792 bfd_size_type amt;
5794 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
5795 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
5796 if (destshndx == NULL)
5797 return FALSE;
5798 memset ((char *) destshndx + amt, 0, amt);
5799 finfo->shndxbuf_size *= 2;
5801 destshndx += bfd_get_symcount (finfo->output_bfd);
5804 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
5805 finfo->symbuf_count += 1;
5806 bfd_get_symcount (finfo->output_bfd) += 1;
5808 return TRUE;
5811 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
5812 allowing an unsatisfied unversioned symbol in the DSO to match a
5813 versioned symbol that would normally require an explicit version.
5814 We also handle the case that a DSO references a hidden symbol
5815 which may be satisfied by a versioned symbol in another DSO. */
5817 static bfd_boolean
5818 elf_link_check_versioned_symbol (struct bfd_link_info *info,
5819 const struct elf_backend_data *bed,
5820 struct elf_link_hash_entry *h)
5822 bfd *abfd;
5823 struct elf_link_loaded_list *loaded;
5825 if (!is_elf_hash_table (info->hash))
5826 return FALSE;
5828 switch (h->root.type)
5830 default:
5831 abfd = NULL;
5832 break;
5834 case bfd_link_hash_undefined:
5835 case bfd_link_hash_undefweak:
5836 abfd = h->root.u.undef.abfd;
5837 if ((abfd->flags & DYNAMIC) == 0
5838 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
5839 return FALSE;
5840 break;
5842 case bfd_link_hash_defined:
5843 case bfd_link_hash_defweak:
5844 abfd = h->root.u.def.section->owner;
5845 break;
5847 case bfd_link_hash_common:
5848 abfd = h->root.u.c.p->section->owner;
5849 break;
5851 BFD_ASSERT (abfd != NULL);
5853 for (loaded = elf_hash_table (info)->loaded;
5854 loaded != NULL;
5855 loaded = loaded->next)
5857 bfd *input;
5858 Elf_Internal_Shdr *hdr;
5859 bfd_size_type symcount;
5860 bfd_size_type extsymcount;
5861 bfd_size_type extsymoff;
5862 Elf_Internal_Shdr *versymhdr;
5863 Elf_Internal_Sym *isym;
5864 Elf_Internal_Sym *isymend;
5865 Elf_Internal_Sym *isymbuf;
5866 Elf_External_Versym *ever;
5867 Elf_External_Versym *extversym;
5869 input = loaded->abfd;
5871 /* We check each DSO for a possible hidden versioned definition. */
5872 if (input == abfd
5873 || (input->flags & DYNAMIC) == 0
5874 || elf_dynversym (input) == 0)
5875 continue;
5877 hdr = &elf_tdata (input)->dynsymtab_hdr;
5879 symcount = hdr->sh_size / bed->s->sizeof_sym;
5880 if (elf_bad_symtab (input))
5882 extsymcount = symcount;
5883 extsymoff = 0;
5885 else
5887 extsymcount = symcount - hdr->sh_info;
5888 extsymoff = hdr->sh_info;
5891 if (extsymcount == 0)
5892 continue;
5894 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
5895 NULL, NULL, NULL);
5896 if (isymbuf == NULL)
5897 return FALSE;
5899 /* Read in any version definitions. */
5900 versymhdr = &elf_tdata (input)->dynversym_hdr;
5901 extversym = bfd_malloc (versymhdr->sh_size);
5902 if (extversym == NULL)
5903 goto error_ret;
5905 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
5906 || (bfd_bread (extversym, versymhdr->sh_size, input)
5907 != versymhdr->sh_size))
5909 free (extversym);
5910 error_ret:
5911 free (isymbuf);
5912 return FALSE;
5915 ever = extversym + extsymoff;
5916 isymend = isymbuf + extsymcount;
5917 for (isym = isymbuf; isym < isymend; isym++, ever++)
5919 const char *name;
5920 Elf_Internal_Versym iver;
5921 unsigned short version_index;
5923 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
5924 || isym->st_shndx == SHN_UNDEF)
5925 continue;
5927 name = bfd_elf_string_from_elf_section (input,
5928 hdr->sh_link,
5929 isym->st_name);
5930 if (strcmp (name, h->root.root.string) != 0)
5931 continue;
5933 _bfd_elf_swap_versym_in (input, ever, &iver);
5935 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
5937 /* If we have a non-hidden versioned sym, then it should
5938 have provided a definition for the undefined sym. */
5939 abort ();
5942 version_index = iver.vs_vers & VERSYM_VERSION;
5943 if (version_index == 1 || version_index == 2)
5945 /* This is the base or first version. We can use it. */
5946 free (extversym);
5947 free (isymbuf);
5948 return TRUE;
5952 free (extversym);
5953 free (isymbuf);
5956 return FALSE;
5959 /* Add an external symbol to the symbol table. This is called from
5960 the hash table traversal routine. When generating a shared object,
5961 we go through the symbol table twice. The first time we output
5962 anything that might have been forced to local scope in a version
5963 script. The second time we output the symbols that are still
5964 global symbols. */
5966 static bfd_boolean
5967 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
5969 struct elf_outext_info *eoinfo = data;
5970 struct elf_final_link_info *finfo = eoinfo->finfo;
5971 bfd_boolean strip;
5972 Elf_Internal_Sym sym;
5973 asection *input_sec;
5974 const struct elf_backend_data *bed;
5976 if (h->root.type == bfd_link_hash_warning)
5978 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5979 if (h->root.type == bfd_link_hash_new)
5980 return TRUE;
5983 /* Decide whether to output this symbol in this pass. */
5984 if (eoinfo->localsyms)
5986 if (!h->forced_local)
5987 return TRUE;
5989 else
5991 if (h->forced_local)
5992 return TRUE;
5995 bed = get_elf_backend_data (finfo->output_bfd);
5997 /* If we have an undefined symbol reference here then it must have
5998 come from a shared library that is being linked in. (Undefined
5999 references in regular files have already been handled). If we
6000 are reporting errors for this situation then do so now. */
6001 if (h->root.type == bfd_link_hash_undefined
6002 && h->ref_dynamic
6003 && !h->ref_regular
6004 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
6005 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
6007 if (! ((*finfo->info->callbacks->undefined_symbol)
6008 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
6009 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
6011 eoinfo->failed = TRUE;
6012 return FALSE;
6016 /* We should also warn if a forced local symbol is referenced from
6017 shared libraries. */
6018 if (! finfo->info->relocatable
6019 && (! finfo->info->shared)
6020 && h->forced_local
6021 && h->ref_dynamic
6022 && !h->dynamic_def
6023 && !h->dynamic_weak
6024 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
6026 (*_bfd_error_handler)
6027 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6028 finfo->output_bfd, h->root.u.def.section->owner,
6029 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
6030 ? "internal"
6031 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
6032 ? "hidden" : "local",
6033 h->root.root.string);
6034 eoinfo->failed = TRUE;
6035 return FALSE;
6038 /* We don't want to output symbols that have never been mentioned by
6039 a regular file, or that we have been told to strip. However, if
6040 h->indx is set to -2, the symbol is used by a reloc and we must
6041 output it. */
6042 if (h->indx == -2)
6043 strip = FALSE;
6044 else if ((h->def_dynamic
6045 || h->ref_dynamic)
6046 && !h->def_regular
6047 && !h->ref_regular)
6048 strip = TRUE;
6049 else if (finfo->info->strip == strip_all)
6050 strip = TRUE;
6051 else if (finfo->info->strip == strip_some
6052 && bfd_hash_lookup (finfo->info->keep_hash,
6053 h->root.root.string, FALSE, FALSE) == NULL)
6054 strip = TRUE;
6055 else if (finfo->info->strip_discarded
6056 && (h->root.type == bfd_link_hash_defined
6057 || h->root.type == bfd_link_hash_defweak)
6058 && elf_discarded_section (h->root.u.def.section))
6059 strip = TRUE;
6060 else
6061 strip = FALSE;
6063 /* If we're stripping it, and it's not a dynamic symbol, there's
6064 nothing else to do unless it is a forced local symbol. */
6065 if (strip
6066 && h->dynindx == -1
6067 && !h->forced_local)
6068 return TRUE;
6070 sym.st_value = 0;
6071 sym.st_size = h->size;
6072 sym.st_other = h->other;
6073 if (h->forced_local)
6074 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
6075 else if (h->root.type == bfd_link_hash_undefweak
6076 || h->root.type == bfd_link_hash_defweak)
6077 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
6078 else
6079 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
6081 switch (h->root.type)
6083 default:
6084 case bfd_link_hash_new:
6085 case bfd_link_hash_warning:
6086 abort ();
6087 return FALSE;
6089 case bfd_link_hash_undefined:
6090 case bfd_link_hash_undefweak:
6091 input_sec = bfd_und_section_ptr;
6092 sym.st_shndx = SHN_UNDEF;
6093 break;
6095 case bfd_link_hash_defined:
6096 case bfd_link_hash_defweak:
6098 input_sec = h->root.u.def.section;
6099 if (input_sec->output_section != NULL)
6101 sym.st_shndx =
6102 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
6103 input_sec->output_section);
6104 if (sym.st_shndx == SHN_BAD)
6106 (*_bfd_error_handler)
6107 (_("%B: could not find output section %A for input section %A"),
6108 finfo->output_bfd, input_sec->output_section, input_sec);
6109 eoinfo->failed = TRUE;
6110 return FALSE;
6113 /* ELF symbols in relocatable files are section relative,
6114 but in nonrelocatable files they are virtual
6115 addresses. */
6116 sym.st_value = h->root.u.def.value + input_sec->output_offset;
6117 if (! finfo->info->relocatable)
6119 sym.st_value += input_sec->output_section->vma;
6120 if (h->type == STT_TLS)
6122 /* STT_TLS symbols are relative to PT_TLS segment
6123 base. */
6124 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6125 sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6129 else
6131 BFD_ASSERT (input_sec->owner == NULL
6132 || (input_sec->owner->flags & DYNAMIC) != 0);
6133 sym.st_shndx = SHN_UNDEF;
6134 input_sec = bfd_und_section_ptr;
6137 break;
6139 case bfd_link_hash_common:
6140 input_sec = h->root.u.c.p->section;
6141 sym.st_shndx = SHN_COMMON;
6142 sym.st_value = 1 << h->root.u.c.p->alignment_power;
6143 break;
6145 case bfd_link_hash_indirect:
6146 /* These symbols are created by symbol versioning. They point
6147 to the decorated version of the name. For example, if the
6148 symbol foo@@GNU_1.2 is the default, which should be used when
6149 foo is used with no version, then we add an indirect symbol
6150 foo which points to foo@@GNU_1.2. We ignore these symbols,
6151 since the indirected symbol is already in the hash table. */
6152 return TRUE;
6155 /* Give the processor backend a chance to tweak the symbol value,
6156 and also to finish up anything that needs to be done for this
6157 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6158 forced local syms when non-shared is due to a historical quirk. */
6159 if ((h->dynindx != -1
6160 || h->forced_local)
6161 && ((finfo->info->shared
6162 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6163 || h->root.type != bfd_link_hash_undefweak))
6164 || !h->forced_local)
6165 && elf_hash_table (finfo->info)->dynamic_sections_created)
6167 if (! ((*bed->elf_backend_finish_dynamic_symbol)
6168 (finfo->output_bfd, finfo->info, h, &sym)))
6170 eoinfo->failed = TRUE;
6171 return FALSE;
6175 /* If we are marking the symbol as undefined, and there are no
6176 non-weak references to this symbol from a regular object, then
6177 mark the symbol as weak undefined; if there are non-weak
6178 references, mark the symbol as strong. We can't do this earlier,
6179 because it might not be marked as undefined until the
6180 finish_dynamic_symbol routine gets through with it. */
6181 if (sym.st_shndx == SHN_UNDEF
6182 && h->ref_regular
6183 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
6184 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
6186 int bindtype;
6188 if (h->ref_regular_nonweak)
6189 bindtype = STB_GLOBAL;
6190 else
6191 bindtype = STB_WEAK;
6192 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
6195 /* If a non-weak symbol with non-default visibility is not defined
6196 locally, it is a fatal error. */
6197 if (! finfo->info->relocatable
6198 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
6199 && ELF_ST_BIND (sym.st_info) != STB_WEAK
6200 && h->root.type == bfd_link_hash_undefined
6201 && !h->def_regular)
6203 (*_bfd_error_handler)
6204 (_("%B: %s symbol `%s' isn't defined"),
6205 finfo->output_bfd,
6206 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
6207 ? "protected"
6208 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
6209 ? "internal" : "hidden",
6210 h->root.root.string);
6211 eoinfo->failed = TRUE;
6212 return FALSE;
6215 /* If this symbol should be put in the .dynsym section, then put it
6216 there now. We already know the symbol index. We also fill in
6217 the entry in the .hash section. */
6218 if (h->dynindx != -1
6219 && elf_hash_table (finfo->info)->dynamic_sections_created)
6221 size_t bucketcount;
6222 size_t bucket;
6223 size_t hash_entry_size;
6224 bfd_byte *bucketpos;
6225 bfd_vma chain;
6226 bfd_byte *esym;
6228 sym.st_name = h->dynstr_index;
6229 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
6230 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
6232 bucketcount = elf_hash_table (finfo->info)->bucketcount;
6233 bucket = h->elf_hash_value % bucketcount;
6234 hash_entry_size
6235 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
6236 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
6237 + (bucket + 2) * hash_entry_size);
6238 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
6239 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
6240 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
6241 ((bfd_byte *) finfo->hash_sec->contents
6242 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
6244 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
6246 Elf_Internal_Versym iversym;
6247 Elf_External_Versym *eversym;
6249 if (!h->def_regular)
6251 if (h->verinfo.verdef == NULL)
6252 iversym.vs_vers = 0;
6253 else
6254 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
6256 else
6258 if (h->verinfo.vertree == NULL)
6259 iversym.vs_vers = 1;
6260 else
6261 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
6264 if (h->hidden)
6265 iversym.vs_vers |= VERSYM_HIDDEN;
6267 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
6268 eversym += h->dynindx;
6269 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
6273 /* If we're stripping it, then it was just a dynamic symbol, and
6274 there's nothing else to do. */
6275 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
6276 return TRUE;
6278 h->indx = bfd_get_symcount (finfo->output_bfd);
6280 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
6282 eoinfo->failed = TRUE;
6283 return FALSE;
6286 return TRUE;
6289 /* Return TRUE if special handling is done for relocs in SEC against
6290 symbols defined in discarded sections. */
6292 static bfd_boolean
6293 elf_section_ignore_discarded_relocs (asection *sec)
6295 const struct elf_backend_data *bed;
6297 switch (sec->sec_info_type)
6299 case ELF_INFO_TYPE_STABS:
6300 case ELF_INFO_TYPE_EH_FRAME:
6301 return TRUE;
6302 default:
6303 break;
6306 bed = get_elf_backend_data (sec->owner);
6307 if (bed->elf_backend_ignore_discarded_relocs != NULL
6308 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
6309 return TRUE;
6311 return FALSE;
6314 /* Return TRUE if we should complain about a reloc in SEC against a
6315 symbol defined in a discarded section. */
6317 static bfd_boolean
6318 elf_section_complain_discarded (asection *sec)
6320 if (strncmp (".stab", sec->name, 5) == 0
6321 && (!sec->name[5] ||
6322 (sec->name[5] == '.' && ISDIGIT (sec->name[6]))))
6323 return FALSE;
6325 if (strcmp (".eh_frame", sec->name) == 0)
6326 return FALSE;
6328 if (strcmp (".gcc_except_table", sec->name) == 0)
6329 return FALSE;
6331 if (strcmp (".PARISC.unwind", sec->name) == 0)
6332 return FALSE;
6334 if (strcmp (".fixup", sec->name) == 0)
6335 return FALSE;
6337 return TRUE;
6340 /* Find a match between a section and a member of a section group. */
6342 static asection *
6343 match_group_member (asection *sec, asection *group)
6345 asection *first = elf_next_in_group (group);
6346 asection *s = first;
6348 while (s != NULL)
6350 if (bfd_elf_match_symbols_in_sections (s, sec))
6351 return s;
6353 if (s == first)
6354 break;
6357 return NULL;
6360 /* Link an input file into the linker output file. This function
6361 handles all the sections and relocations of the input file at once.
6362 This is so that we only have to read the local symbols once, and
6363 don't have to keep them in memory. */
6365 static bfd_boolean
6366 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
6368 bfd_boolean (*relocate_section)
6369 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
6370 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
6371 bfd *output_bfd;
6372 Elf_Internal_Shdr *symtab_hdr;
6373 size_t locsymcount;
6374 size_t extsymoff;
6375 Elf_Internal_Sym *isymbuf;
6376 Elf_Internal_Sym *isym;
6377 Elf_Internal_Sym *isymend;
6378 long *pindex;
6379 asection **ppsection;
6380 asection *o;
6381 const struct elf_backend_data *bed;
6382 bfd_boolean emit_relocs;
6383 struct elf_link_hash_entry **sym_hashes;
6385 output_bfd = finfo->output_bfd;
6386 bed = get_elf_backend_data (output_bfd);
6387 relocate_section = bed->elf_backend_relocate_section;
6389 /* If this is a dynamic object, we don't want to do anything here:
6390 we don't want the local symbols, and we don't want the section
6391 contents. */
6392 if ((input_bfd->flags & DYNAMIC) != 0)
6393 return TRUE;
6395 emit_relocs = (finfo->info->relocatable
6396 || finfo->info->emitrelocations
6397 || bed->elf_backend_emit_relocs);
6399 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6400 if (elf_bad_symtab (input_bfd))
6402 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
6403 extsymoff = 0;
6405 else
6407 locsymcount = symtab_hdr->sh_info;
6408 extsymoff = symtab_hdr->sh_info;
6411 /* Read the local symbols. */
6412 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
6413 if (isymbuf == NULL && locsymcount != 0)
6415 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
6416 finfo->internal_syms,
6417 finfo->external_syms,
6418 finfo->locsym_shndx);
6419 if (isymbuf == NULL)
6420 return FALSE;
6423 /* Find local symbol sections and adjust values of symbols in
6424 SEC_MERGE sections. Write out those local symbols we know are
6425 going into the output file. */
6426 isymend = isymbuf + locsymcount;
6427 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
6428 isym < isymend;
6429 isym++, pindex++, ppsection++)
6431 asection *isec;
6432 const char *name;
6433 Elf_Internal_Sym osym;
6435 *pindex = -1;
6437 if (elf_bad_symtab (input_bfd))
6439 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
6441 *ppsection = NULL;
6442 continue;
6446 if (isym->st_shndx == SHN_UNDEF)
6447 isec = bfd_und_section_ptr;
6448 else if (isym->st_shndx < SHN_LORESERVE
6449 || isym->st_shndx > SHN_HIRESERVE)
6451 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
6452 if (isec
6453 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
6454 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
6455 isym->st_value =
6456 _bfd_merged_section_offset (output_bfd, &isec,
6457 elf_section_data (isec)->sec_info,
6458 isym->st_value);
6460 else if (isym->st_shndx == SHN_ABS)
6461 isec = bfd_abs_section_ptr;
6462 else if (isym->st_shndx == SHN_COMMON)
6463 isec = bfd_com_section_ptr;
6464 else
6466 /* Who knows? */
6467 isec = NULL;
6470 *ppsection = isec;
6472 /* Don't output the first, undefined, symbol. */
6473 if (ppsection == finfo->sections)
6474 continue;
6476 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6478 /* We never output section symbols. Instead, we use the
6479 section symbol of the corresponding section in the output
6480 file. */
6481 continue;
6484 /* If we are stripping all symbols, we don't want to output this
6485 one. */
6486 if (finfo->info->strip == strip_all)
6487 continue;
6489 /* If we are discarding all local symbols, we don't want to
6490 output this one. If we are generating a relocatable output
6491 file, then some of the local symbols may be required by
6492 relocs; we output them below as we discover that they are
6493 needed. */
6494 if (finfo->info->discard == discard_all)
6495 continue;
6497 /* If this symbol is defined in a section which we are
6498 discarding, we don't need to keep it, but note that
6499 linker_mark is only reliable for sections that have contents.
6500 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6501 as well as linker_mark. */
6502 if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
6503 && isec != NULL
6504 && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
6505 || (! finfo->info->relocatable
6506 && (isec->flags & SEC_EXCLUDE) != 0)))
6507 continue;
6509 /* Get the name of the symbol. */
6510 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
6511 isym->st_name);
6512 if (name == NULL)
6513 return FALSE;
6515 /* See if we are discarding symbols with this name. */
6516 if ((finfo->info->strip == strip_some
6517 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
6518 == NULL))
6519 || (((finfo->info->discard == discard_sec_merge
6520 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
6521 || finfo->info->discard == discard_l)
6522 && bfd_is_local_label_name (input_bfd, name)))
6523 continue;
6525 /* If we get here, we are going to output this symbol. */
6527 osym = *isym;
6529 /* Adjust the section index for the output file. */
6530 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
6531 isec->output_section);
6532 if (osym.st_shndx == SHN_BAD)
6533 return FALSE;
6535 *pindex = bfd_get_symcount (output_bfd);
6537 /* ELF symbols in relocatable files are section relative, but
6538 in executable files they are virtual addresses. Note that
6539 this code assumes that all ELF sections have an associated
6540 BFD section with a reasonable value for output_offset; below
6541 we assume that they also have a reasonable value for
6542 output_section. Any special sections must be set up to meet
6543 these requirements. */
6544 osym.st_value += isec->output_offset;
6545 if (! finfo->info->relocatable)
6547 osym.st_value += isec->output_section->vma;
6548 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
6550 /* STT_TLS symbols are relative to PT_TLS segment base. */
6551 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6552 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6556 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
6557 return FALSE;
6560 /* Relocate the contents of each section. */
6561 sym_hashes = elf_sym_hashes (input_bfd);
6562 for (o = input_bfd->sections; o != NULL; o = o->next)
6564 bfd_byte *contents;
6566 if (! o->linker_mark)
6568 /* This section was omitted from the link. */
6569 continue;
6572 if ((o->flags & SEC_HAS_CONTENTS) == 0
6573 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
6574 continue;
6576 if ((o->flags & SEC_LINKER_CREATED) != 0)
6578 /* Section was created by _bfd_elf_link_create_dynamic_sections
6579 or somesuch. */
6580 continue;
6583 /* Get the contents of the section. They have been cached by a
6584 relaxation routine. Note that o is a section in an input
6585 file, so the contents field will not have been set by any of
6586 the routines which work on output files. */
6587 if (elf_section_data (o)->this_hdr.contents != NULL)
6588 contents = elf_section_data (o)->this_hdr.contents;
6589 else
6591 bfd_size_type amt = o->rawsize ? o->rawsize : o->size;
6593 contents = finfo->contents;
6594 if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt))
6595 return FALSE;
6598 if ((o->flags & SEC_RELOC) != 0)
6600 Elf_Internal_Rela *internal_relocs;
6601 bfd_vma r_type_mask;
6602 int r_sym_shift;
6604 /* Get the swapped relocs. */
6605 internal_relocs
6606 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
6607 finfo->internal_relocs, FALSE);
6608 if (internal_relocs == NULL
6609 && o->reloc_count > 0)
6610 return FALSE;
6612 if (bed->s->arch_size == 32)
6614 r_type_mask = 0xff;
6615 r_sym_shift = 8;
6617 else
6619 r_type_mask = 0xffffffff;
6620 r_sym_shift = 32;
6623 /* Run through the relocs looking for any against symbols
6624 from discarded sections and section symbols from
6625 removed link-once sections. Complain about relocs
6626 against discarded sections. Zero relocs against removed
6627 link-once sections. Preserve debug information as much
6628 as we can. */
6629 if (!elf_section_ignore_discarded_relocs (o))
6631 Elf_Internal_Rela *rel, *relend;
6632 bfd_boolean complain = elf_section_complain_discarded (o);
6634 rel = internal_relocs;
6635 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
6636 for ( ; rel < relend; rel++)
6638 unsigned long r_symndx = rel->r_info >> r_sym_shift;
6639 asection **ps, *sec;
6640 struct elf_link_hash_entry *h = NULL;
6641 const char *sym_name;
6643 if (r_symndx == STN_UNDEF)
6644 continue;
6646 if (r_symndx >= locsymcount
6647 || (elf_bad_symtab (input_bfd)
6648 && finfo->sections[r_symndx] == NULL))
6650 h = sym_hashes[r_symndx - extsymoff];
6651 while (h->root.type == bfd_link_hash_indirect
6652 || h->root.type == bfd_link_hash_warning)
6653 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6655 if (h->root.type != bfd_link_hash_defined
6656 && h->root.type != bfd_link_hash_defweak)
6657 continue;
6659 ps = &h->root.u.def.section;
6660 sym_name = h->root.root.string;
6662 else
6664 Elf_Internal_Sym *sym = isymbuf + r_symndx;
6665 ps = &finfo->sections[r_symndx];
6666 sym_name = bfd_elf_local_sym_name (input_bfd, sym);
6669 /* Complain if the definition comes from a
6670 discarded section. */
6671 if ((sec = *ps) != NULL && elf_discarded_section (sec))
6673 if ((o->flags & SEC_DEBUGGING) != 0)
6675 BFD_ASSERT (r_symndx != 0);
6677 /* Try to preserve debug information.
6678 FIXME: This is quite broken. Modifying
6679 the symbol here means we will be changing
6680 all uses of the symbol, not just those in
6681 debug sections. The only thing that makes
6682 this half reasonable is that debug sections
6683 tend to come after other sections. Of
6684 course, that doesn't help with globals.
6685 ??? All link-once sections of the same name
6686 ought to define the same set of symbols, so
6687 it would seem that globals ought to always
6688 be defined in the kept section. */
6689 if (sec->kept_section != NULL)
6691 asection *member;
6693 /* Check if it is a linkonce section or
6694 member of a comdat group. */
6695 if (elf_sec_group (sec) == NULL
6696 && sec->size == sec->kept_section->size)
6698 *ps = sec->kept_section;
6699 continue;
6701 else if (elf_sec_group (sec) != NULL
6702 && (member = match_group_member (sec, sec->kept_section))
6703 && sec->size == member->size)
6705 *ps = member;
6706 continue;
6710 else if (complain)
6712 (*_bfd_error_handler)
6713 (_("`%s' referenced in section `%A' of %B: "
6714 "defined in discarded section `%A' of %B\n"),
6715 o, input_bfd, sec, sec->owner, sym_name);
6718 /* Remove the symbol reference from the reloc, but
6719 don't kill the reloc completely. This is so that
6720 a zero value will be written into the section,
6721 which may have non-zero contents put there by the
6722 assembler. Zero in things like an eh_frame fde
6723 pc_begin allows stack unwinders to recognize the
6724 fde as bogus. */
6725 rel->r_info &= r_type_mask;
6726 rel->r_addend = 0;
6731 /* Relocate the section by invoking a back end routine.
6733 The back end routine is responsible for adjusting the
6734 section contents as necessary, and (if using Rela relocs
6735 and generating a relocatable output file) adjusting the
6736 reloc addend as necessary.
6738 The back end routine does not have to worry about setting
6739 the reloc address or the reloc symbol index.
6741 The back end routine is given a pointer to the swapped in
6742 internal symbols, and can access the hash table entries
6743 for the external symbols via elf_sym_hashes (input_bfd).
6745 When generating relocatable output, the back end routine
6746 must handle STB_LOCAL/STT_SECTION symbols specially. The
6747 output symbol is going to be a section symbol
6748 corresponding to the output section, which will require
6749 the addend to be adjusted. */
6751 if (! (*relocate_section) (output_bfd, finfo->info,
6752 input_bfd, o, contents,
6753 internal_relocs,
6754 isymbuf,
6755 finfo->sections))
6756 return FALSE;
6758 if (emit_relocs)
6760 Elf_Internal_Rela *irela;
6761 Elf_Internal_Rela *irelaend;
6762 bfd_vma last_offset;
6763 struct elf_link_hash_entry **rel_hash;
6764 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
6765 unsigned int next_erel;
6766 bfd_boolean (*reloc_emitter)
6767 (bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *);
6768 bfd_boolean rela_normal;
6770 input_rel_hdr = &elf_section_data (o)->rel_hdr;
6771 rela_normal = (bed->rela_normal
6772 && (input_rel_hdr->sh_entsize
6773 == bed->s->sizeof_rela));
6775 /* Adjust the reloc addresses and symbol indices. */
6777 irela = internal_relocs;
6778 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
6779 rel_hash = (elf_section_data (o->output_section)->rel_hashes
6780 + elf_section_data (o->output_section)->rel_count
6781 + elf_section_data (o->output_section)->rel_count2);
6782 last_offset = o->output_offset;
6783 if (!finfo->info->relocatable)
6784 last_offset += o->output_section->vma;
6785 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
6787 unsigned long r_symndx;
6788 asection *sec;
6789 Elf_Internal_Sym sym;
6791 if (next_erel == bed->s->int_rels_per_ext_rel)
6793 rel_hash++;
6794 next_erel = 0;
6797 irela->r_offset = _bfd_elf_section_offset (output_bfd,
6798 finfo->info, o,
6799 irela->r_offset);
6800 if (irela->r_offset >= (bfd_vma) -2)
6802 /* This is a reloc for a deleted entry or somesuch.
6803 Turn it into an R_*_NONE reloc, at the same
6804 offset as the last reloc. elf_eh_frame.c and
6805 elf_bfd_discard_info rely on reloc offsets
6806 being ordered. */
6807 irela->r_offset = last_offset;
6808 irela->r_info = 0;
6809 irela->r_addend = 0;
6810 continue;
6813 irela->r_offset += o->output_offset;
6815 /* Relocs in an executable have to be virtual addresses. */
6816 if (!finfo->info->relocatable)
6817 irela->r_offset += o->output_section->vma;
6819 last_offset = irela->r_offset;
6821 r_symndx = irela->r_info >> r_sym_shift;
6822 if (r_symndx == STN_UNDEF)
6823 continue;
6825 if (r_symndx >= locsymcount
6826 || (elf_bad_symtab (input_bfd)
6827 && finfo->sections[r_symndx] == NULL))
6829 struct elf_link_hash_entry *rh;
6830 unsigned long indx;
6832 /* This is a reloc against a global symbol. We
6833 have not yet output all the local symbols, so
6834 we do not know the symbol index of any global
6835 symbol. We set the rel_hash entry for this
6836 reloc to point to the global hash table entry
6837 for this symbol. The symbol index is then
6838 set at the end of bfd_elf_final_link. */
6839 indx = r_symndx - extsymoff;
6840 rh = elf_sym_hashes (input_bfd)[indx];
6841 while (rh->root.type == bfd_link_hash_indirect
6842 || rh->root.type == bfd_link_hash_warning)
6843 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
6845 /* Setting the index to -2 tells
6846 elf_link_output_extsym that this symbol is
6847 used by a reloc. */
6848 BFD_ASSERT (rh->indx < 0);
6849 rh->indx = -2;
6851 *rel_hash = rh;
6853 continue;
6856 /* This is a reloc against a local symbol. */
6858 *rel_hash = NULL;
6859 sym = isymbuf[r_symndx];
6860 sec = finfo->sections[r_symndx];
6861 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
6863 /* I suppose the backend ought to fill in the
6864 section of any STT_SECTION symbol against a
6865 processor specific section. */
6866 r_symndx = 0;
6867 if (bfd_is_abs_section (sec))
6869 else if (sec == NULL || sec->owner == NULL)
6871 bfd_set_error (bfd_error_bad_value);
6872 return FALSE;
6874 else
6876 asection *osec = sec->output_section;
6878 /* If we have discarded a section, the output
6879 section will be the absolute section. In
6880 case of discarded link-once and discarded
6881 SEC_MERGE sections, use the kept section. */
6882 if (bfd_is_abs_section (osec)
6883 && sec->kept_section != NULL
6884 && sec->kept_section->output_section != NULL)
6886 osec = sec->kept_section->output_section;
6887 irela->r_addend -= osec->vma;
6890 if (!bfd_is_abs_section (osec))
6892 r_symndx = osec->target_index;
6893 BFD_ASSERT (r_symndx != 0);
6897 /* Adjust the addend according to where the
6898 section winds up in the output section. */
6899 if (rela_normal)
6900 irela->r_addend += sec->output_offset;
6902 else
6904 if (finfo->indices[r_symndx] == -1)
6906 unsigned long shlink;
6907 const char *name;
6908 asection *osec;
6910 if (finfo->info->strip == strip_all)
6912 /* You can't do ld -r -s. */
6913 bfd_set_error (bfd_error_invalid_operation);
6914 return FALSE;
6917 /* This symbol was skipped earlier, but
6918 since it is needed by a reloc, we
6919 must output it now. */
6920 shlink = symtab_hdr->sh_link;
6921 name = (bfd_elf_string_from_elf_section
6922 (input_bfd, shlink, sym.st_name));
6923 if (name == NULL)
6924 return FALSE;
6926 osec = sec->output_section;
6927 sym.st_shndx =
6928 _bfd_elf_section_from_bfd_section (output_bfd,
6929 osec);
6930 if (sym.st_shndx == SHN_BAD)
6931 return FALSE;
6933 sym.st_value += sec->output_offset;
6934 if (! finfo->info->relocatable)
6936 sym.st_value += osec->vma;
6937 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
6939 /* STT_TLS symbols are relative to PT_TLS
6940 segment base. */
6941 BFD_ASSERT (elf_hash_table (finfo->info)
6942 ->tls_sec != NULL);
6943 sym.st_value -= (elf_hash_table (finfo->info)
6944 ->tls_sec->vma);
6948 finfo->indices[r_symndx]
6949 = bfd_get_symcount (output_bfd);
6951 if (! elf_link_output_sym (finfo, name, &sym, sec,
6952 NULL))
6953 return FALSE;
6956 r_symndx = finfo->indices[r_symndx];
6959 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
6960 | (irela->r_info & r_type_mask));
6963 /* Swap out the relocs. */
6964 if (bed->elf_backend_emit_relocs
6965 && !(finfo->info->relocatable
6966 || finfo->info->emitrelocations))
6967 reloc_emitter = bed->elf_backend_emit_relocs;
6968 else
6969 reloc_emitter = _bfd_elf_link_output_relocs;
6971 if (input_rel_hdr->sh_size != 0
6972 && ! (*reloc_emitter) (output_bfd, o, input_rel_hdr,
6973 internal_relocs))
6974 return FALSE;
6976 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
6977 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
6979 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
6980 * bed->s->int_rels_per_ext_rel);
6981 if (! (*reloc_emitter) (output_bfd, o, input_rel_hdr2,
6982 internal_relocs))
6983 return FALSE;
6988 /* Write out the modified section contents. */
6989 if (bed->elf_backend_write_section
6990 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
6992 /* Section written out. */
6994 else switch (o->sec_info_type)
6996 case ELF_INFO_TYPE_STABS:
6997 if (! (_bfd_write_section_stabs
6998 (output_bfd,
6999 &elf_hash_table (finfo->info)->stab_info,
7000 o, &elf_section_data (o)->sec_info, contents)))
7001 return FALSE;
7002 break;
7003 case ELF_INFO_TYPE_MERGE:
7004 if (! _bfd_write_merged_section (output_bfd, o,
7005 elf_section_data (o)->sec_info))
7006 return FALSE;
7007 break;
7008 case ELF_INFO_TYPE_EH_FRAME:
7010 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
7011 o, contents))
7012 return FALSE;
7014 break;
7015 default:
7017 if (! (o->flags & SEC_EXCLUDE)
7018 && ! bfd_set_section_contents (output_bfd, o->output_section,
7019 contents,
7020 (file_ptr) o->output_offset,
7021 o->size))
7022 return FALSE;
7024 break;
7028 return TRUE;
7031 /* Generate a reloc when linking an ELF file. This is a reloc
7032 requested by the linker, and does come from any input file. This
7033 is used to build constructor and destructor tables when linking
7034 with -Ur. */
7036 static bfd_boolean
7037 elf_reloc_link_order (bfd *output_bfd,
7038 struct bfd_link_info *info,
7039 asection *output_section,
7040 struct bfd_link_order *link_order)
7042 reloc_howto_type *howto;
7043 long indx;
7044 bfd_vma offset;
7045 bfd_vma addend;
7046 struct elf_link_hash_entry **rel_hash_ptr;
7047 Elf_Internal_Shdr *rel_hdr;
7048 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7049 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
7050 bfd_byte *erel;
7051 unsigned int i;
7053 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
7054 if (howto == NULL)
7056 bfd_set_error (bfd_error_bad_value);
7057 return FALSE;
7060 addend = link_order->u.reloc.p->addend;
7062 /* Figure out the symbol index. */
7063 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
7064 + elf_section_data (output_section)->rel_count
7065 + elf_section_data (output_section)->rel_count2);
7066 if (link_order->type == bfd_section_reloc_link_order)
7068 indx = link_order->u.reloc.p->u.section->target_index;
7069 BFD_ASSERT (indx != 0);
7070 *rel_hash_ptr = NULL;
7072 else
7074 struct elf_link_hash_entry *h;
7076 /* Treat a reloc against a defined symbol as though it were
7077 actually against the section. */
7078 h = ((struct elf_link_hash_entry *)
7079 bfd_wrapped_link_hash_lookup (output_bfd, info,
7080 link_order->u.reloc.p->u.name,
7081 FALSE, FALSE, TRUE));
7082 if (h != NULL
7083 && (h->root.type == bfd_link_hash_defined
7084 || h->root.type == bfd_link_hash_defweak))
7086 asection *section;
7088 section = h->root.u.def.section;
7089 indx = section->output_section->target_index;
7090 *rel_hash_ptr = NULL;
7091 /* It seems that we ought to add the symbol value to the
7092 addend here, but in practice it has already been added
7093 because it was passed to constructor_callback. */
7094 addend += section->output_section->vma + section->output_offset;
7096 else if (h != NULL)
7098 /* Setting the index to -2 tells elf_link_output_extsym that
7099 this symbol is used by a reloc. */
7100 h->indx = -2;
7101 *rel_hash_ptr = h;
7102 indx = 0;
7104 else
7106 if (! ((*info->callbacks->unattached_reloc)
7107 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
7108 return FALSE;
7109 indx = 0;
7113 /* If this is an inplace reloc, we must write the addend into the
7114 object file. */
7115 if (howto->partial_inplace && addend != 0)
7117 bfd_size_type size;
7118 bfd_reloc_status_type rstat;
7119 bfd_byte *buf;
7120 bfd_boolean ok;
7121 const char *sym_name;
7123 size = bfd_get_reloc_size (howto);
7124 buf = bfd_zmalloc (size);
7125 if (buf == NULL)
7126 return FALSE;
7127 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
7128 switch (rstat)
7130 case bfd_reloc_ok:
7131 break;
7133 default:
7134 case bfd_reloc_outofrange:
7135 abort ();
7137 case bfd_reloc_overflow:
7138 if (link_order->type == bfd_section_reloc_link_order)
7139 sym_name = bfd_section_name (output_bfd,
7140 link_order->u.reloc.p->u.section);
7141 else
7142 sym_name = link_order->u.reloc.p->u.name;
7143 if (! ((*info->callbacks->reloc_overflow)
7144 (info, sym_name, howto->name, addend, NULL, NULL, 0)))
7146 free (buf);
7147 return FALSE;
7149 break;
7151 ok = bfd_set_section_contents (output_bfd, output_section, buf,
7152 link_order->offset, size);
7153 free (buf);
7154 if (! ok)
7155 return FALSE;
7158 /* The address of a reloc is relative to the section in a
7159 relocatable file, and is a virtual address in an executable
7160 file. */
7161 offset = link_order->offset;
7162 if (! info->relocatable)
7163 offset += output_section->vma;
7165 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7167 irel[i].r_offset = offset;
7168 irel[i].r_info = 0;
7169 irel[i].r_addend = 0;
7171 if (bed->s->arch_size == 32)
7172 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
7173 else
7174 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
7176 rel_hdr = &elf_section_data (output_section)->rel_hdr;
7177 erel = rel_hdr->contents;
7178 if (rel_hdr->sh_type == SHT_REL)
7180 erel += (elf_section_data (output_section)->rel_count
7181 * bed->s->sizeof_rel);
7182 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
7184 else
7186 irel[0].r_addend = addend;
7187 erel += (elf_section_data (output_section)->rel_count
7188 * bed->s->sizeof_rela);
7189 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
7192 ++elf_section_data (output_section)->rel_count;
7194 return TRUE;
7198 /* Get the output vma of the section pointed to by the sh_link field. */
7200 static bfd_vma
7201 elf_get_linked_section_vma (struct bfd_link_order *p)
7203 Elf_Internal_Shdr **elf_shdrp;
7204 asection *s;
7205 int elfsec;
7207 s = p->u.indirect.section;
7208 elf_shdrp = elf_elfsections (s->owner);
7209 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
7210 elfsec = elf_shdrp[elfsec]->sh_link;
7211 /* PR 290:
7212 The Intel C compiler generates SHT_IA_64_UNWIND with
7213 SHF_LINK_ORDER. But it doesn't set theh sh_link or
7214 sh_info fields. Hence we could get the situation
7215 where elfsec is 0. */
7216 if (elfsec == 0)
7218 const struct elf_backend_data *bed
7219 = get_elf_backend_data (s->owner);
7220 if (bed->link_order_error_handler)
7221 bed->link_order_error_handler
7222 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
7223 return 0;
7225 else
7227 s = elf_shdrp[elfsec]->bfd_section;
7228 return s->output_section->vma + s->output_offset;
7233 /* Compare two sections based on the locations of the sections they are
7234 linked to. Used by elf_fixup_link_order. */
7236 static int
7237 compare_link_order (const void * a, const void * b)
7239 bfd_vma apos;
7240 bfd_vma bpos;
7242 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
7243 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
7244 if (apos < bpos)
7245 return -1;
7246 return apos > bpos;
7250 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7251 order as their linked sections. Returns false if this could not be done
7252 because an output section includes both ordered and unordered
7253 sections. Ideally we'd do this in the linker proper. */
7255 static bfd_boolean
7256 elf_fixup_link_order (bfd *abfd, asection *o)
7258 int seen_linkorder;
7259 int seen_other;
7260 int n;
7261 struct bfd_link_order *p;
7262 bfd *sub;
7263 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7264 int elfsec;
7265 struct bfd_link_order **sections;
7266 asection *s;
7267 bfd_vma offset;
7269 seen_other = 0;
7270 seen_linkorder = 0;
7271 for (p = o->link_order_head; p != NULL; p = p->next)
7273 if (p->type == bfd_indirect_link_order
7274 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
7275 == bfd_target_elf_flavour)
7276 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
7278 s = p->u.indirect.section;
7279 elfsec = _bfd_elf_section_from_bfd_section (sub, s);
7280 if (elfsec != -1
7281 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER)
7282 seen_linkorder++;
7283 else
7284 seen_other++;
7286 else
7287 seen_other++;
7290 if (!seen_linkorder)
7291 return TRUE;
7293 if (seen_other && seen_linkorder)
7295 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
7297 bfd_set_error (bfd_error_bad_value);
7298 return FALSE;
7301 sections = (struct bfd_link_order **)
7302 xmalloc (seen_linkorder * sizeof (struct bfd_link_order *));
7303 seen_linkorder = 0;
7305 for (p = o->link_order_head; p != NULL; p = p->next)
7307 sections[seen_linkorder++] = p;
7309 /* Sort the input sections in the order of their linked section. */
7310 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
7311 compare_link_order);
7313 /* Change the offsets of the sections. */
7314 offset = 0;
7315 for (n = 0; n < seen_linkorder; n++)
7317 s = sections[n]->u.indirect.section;
7318 offset &= ~(bfd_vma)((1 << s->alignment_power) - 1);
7319 s->output_offset = offset;
7320 sections[n]->offset = offset;
7321 offset += sections[n]->size;
7324 return TRUE;
7328 /* Do the final step of an ELF link. */
7330 bfd_boolean
7331 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
7333 bfd_boolean dynamic;
7334 bfd_boolean emit_relocs;
7335 bfd *dynobj;
7336 struct elf_final_link_info finfo;
7337 register asection *o;
7338 register struct bfd_link_order *p;
7339 register bfd *sub;
7340 bfd_size_type max_contents_size;
7341 bfd_size_type max_external_reloc_size;
7342 bfd_size_type max_internal_reloc_count;
7343 bfd_size_type max_sym_count;
7344 bfd_size_type max_sym_shndx_count;
7345 file_ptr off;
7346 Elf_Internal_Sym elfsym;
7347 unsigned int i;
7348 Elf_Internal_Shdr *symtab_hdr;
7349 Elf_Internal_Shdr *symtab_shndx_hdr;
7350 Elf_Internal_Shdr *symstrtab_hdr;
7351 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7352 struct elf_outext_info eoinfo;
7353 bfd_boolean merged;
7354 size_t relativecount = 0;
7355 asection *reldyn = 0;
7356 bfd_size_type amt;
7358 if (! is_elf_hash_table (info->hash))
7359 return FALSE;
7361 if (info->shared)
7362 abfd->flags |= DYNAMIC;
7364 dynamic = elf_hash_table (info)->dynamic_sections_created;
7365 dynobj = elf_hash_table (info)->dynobj;
7367 emit_relocs = (info->relocatable
7368 || info->emitrelocations
7369 || bed->elf_backend_emit_relocs);
7371 finfo.info = info;
7372 finfo.output_bfd = abfd;
7373 finfo.symstrtab = _bfd_elf_stringtab_init ();
7374 if (finfo.symstrtab == NULL)
7375 return FALSE;
7377 if (! dynamic)
7379 finfo.dynsym_sec = NULL;
7380 finfo.hash_sec = NULL;
7381 finfo.symver_sec = NULL;
7383 else
7385 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
7386 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
7387 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
7388 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
7389 /* Note that it is OK if symver_sec is NULL. */
7392 finfo.contents = NULL;
7393 finfo.external_relocs = NULL;
7394 finfo.internal_relocs = NULL;
7395 finfo.external_syms = NULL;
7396 finfo.locsym_shndx = NULL;
7397 finfo.internal_syms = NULL;
7398 finfo.indices = NULL;
7399 finfo.sections = NULL;
7400 finfo.symbuf = NULL;
7401 finfo.symshndxbuf = NULL;
7402 finfo.symbuf_count = 0;
7403 finfo.shndxbuf_size = 0;
7405 /* Count up the number of relocations we will output for each output
7406 section, so that we know the sizes of the reloc sections. We
7407 also figure out some maximum sizes. */
7408 max_contents_size = 0;
7409 max_external_reloc_size = 0;
7410 max_internal_reloc_count = 0;
7411 max_sym_count = 0;
7412 max_sym_shndx_count = 0;
7413 merged = FALSE;
7414 for (o = abfd->sections; o != NULL; o = o->next)
7416 struct bfd_elf_section_data *esdo = elf_section_data (o);
7417 o->reloc_count = 0;
7419 for (p = o->link_order_head; p != NULL; p = p->next)
7421 unsigned int reloc_count = 0;
7422 struct bfd_elf_section_data *esdi = NULL;
7423 unsigned int *rel_count1;
7425 if (p->type == bfd_section_reloc_link_order
7426 || p->type == bfd_symbol_reloc_link_order)
7427 reloc_count = 1;
7428 else if (p->type == bfd_indirect_link_order)
7430 asection *sec;
7432 sec = p->u.indirect.section;
7433 esdi = elf_section_data (sec);
7435 /* Mark all sections which are to be included in the
7436 link. This will normally be every section. We need
7437 to do this so that we can identify any sections which
7438 the linker has decided to not include. */
7439 sec->linker_mark = TRUE;
7441 if (sec->flags & SEC_MERGE)
7442 merged = TRUE;
7444 if (info->relocatable || info->emitrelocations)
7445 reloc_count = sec->reloc_count;
7446 else if (bed->elf_backend_count_relocs)
7448 Elf_Internal_Rela * relocs;
7450 relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
7451 info->keep_memory);
7453 reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs);
7455 if (elf_section_data (o)->relocs != relocs)
7456 free (relocs);
7459 if (sec->rawsize > max_contents_size)
7460 max_contents_size = sec->rawsize;
7461 if (sec->size > max_contents_size)
7462 max_contents_size = sec->size;
7464 /* We are interested in just local symbols, not all
7465 symbols. */
7466 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
7467 && (sec->owner->flags & DYNAMIC) == 0)
7469 size_t sym_count;
7471 if (elf_bad_symtab (sec->owner))
7472 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
7473 / bed->s->sizeof_sym);
7474 else
7475 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
7477 if (sym_count > max_sym_count)
7478 max_sym_count = sym_count;
7480 if (sym_count > max_sym_shndx_count
7481 && elf_symtab_shndx (sec->owner) != 0)
7482 max_sym_shndx_count = sym_count;
7484 if ((sec->flags & SEC_RELOC) != 0)
7486 size_t ext_size;
7488 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
7489 if (ext_size > max_external_reloc_size)
7490 max_external_reloc_size = ext_size;
7491 if (sec->reloc_count > max_internal_reloc_count)
7492 max_internal_reloc_count = sec->reloc_count;
7497 if (reloc_count == 0)
7498 continue;
7500 o->reloc_count += reloc_count;
7502 /* MIPS may have a mix of REL and RELA relocs on sections.
7503 To support this curious ABI we keep reloc counts in
7504 elf_section_data too. We must be careful to add the
7505 relocations from the input section to the right output
7506 count. FIXME: Get rid of one count. We have
7507 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7508 rel_count1 = &esdo->rel_count;
7509 if (esdi != NULL)
7511 bfd_boolean same_size;
7512 bfd_size_type entsize1;
7514 entsize1 = esdi->rel_hdr.sh_entsize;
7515 BFD_ASSERT (entsize1 == bed->s->sizeof_rel
7516 || entsize1 == bed->s->sizeof_rela);
7517 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
7519 if (!same_size)
7520 rel_count1 = &esdo->rel_count2;
7522 if (esdi->rel_hdr2 != NULL)
7524 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
7525 unsigned int alt_count;
7526 unsigned int *rel_count2;
7528 BFD_ASSERT (entsize2 != entsize1
7529 && (entsize2 == bed->s->sizeof_rel
7530 || entsize2 == bed->s->sizeof_rela));
7532 rel_count2 = &esdo->rel_count2;
7533 if (!same_size)
7534 rel_count2 = &esdo->rel_count;
7536 /* The following is probably too simplistic if the
7537 backend counts output relocs unusually. */
7538 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
7539 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
7540 *rel_count2 += alt_count;
7541 reloc_count -= alt_count;
7544 *rel_count1 += reloc_count;
7547 if (o->reloc_count > 0)
7548 o->flags |= SEC_RELOC;
7549 else
7551 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7552 set it (this is probably a bug) and if it is set
7553 assign_section_numbers will create a reloc section. */
7554 o->flags &=~ SEC_RELOC;
7557 /* If the SEC_ALLOC flag is not set, force the section VMA to
7558 zero. This is done in elf_fake_sections as well, but forcing
7559 the VMA to 0 here will ensure that relocs against these
7560 sections are handled correctly. */
7561 if ((o->flags & SEC_ALLOC) == 0
7562 && ! o->user_set_vma)
7563 o->vma = 0;
7566 if (! info->relocatable && merged)
7567 elf_link_hash_traverse (elf_hash_table (info),
7568 _bfd_elf_link_sec_merge_syms, abfd);
7570 /* Figure out the file positions for everything but the symbol table
7571 and the relocs. We set symcount to force assign_section_numbers
7572 to create a symbol table. */
7573 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
7574 BFD_ASSERT (! abfd->output_has_begun);
7575 if (! _bfd_elf_compute_section_file_positions (abfd, info))
7576 goto error_return;
7578 /* Set sizes, and assign file positions for reloc sections. */
7579 for (o = abfd->sections; o != NULL; o = o->next)
7581 if ((o->flags & SEC_RELOC) != 0)
7583 if (!(_bfd_elf_link_size_reloc_section
7584 (abfd, &elf_section_data (o)->rel_hdr, o)))
7585 goto error_return;
7587 if (elf_section_data (o)->rel_hdr2
7588 && !(_bfd_elf_link_size_reloc_section
7589 (abfd, elf_section_data (o)->rel_hdr2, o)))
7590 goto error_return;
7593 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
7594 to count upwards while actually outputting the relocations. */
7595 elf_section_data (o)->rel_count = 0;
7596 elf_section_data (o)->rel_count2 = 0;
7599 _bfd_elf_assign_file_positions_for_relocs (abfd);
7601 /* We have now assigned file positions for all the sections except
7602 .symtab and .strtab. We start the .symtab section at the current
7603 file position, and write directly to it. We build the .strtab
7604 section in memory. */
7605 bfd_get_symcount (abfd) = 0;
7606 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7607 /* sh_name is set in prep_headers. */
7608 symtab_hdr->sh_type = SHT_SYMTAB;
7609 /* sh_flags, sh_addr and sh_size all start off zero. */
7610 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
7611 /* sh_link is set in assign_section_numbers. */
7612 /* sh_info is set below. */
7613 /* sh_offset is set just below. */
7614 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
7616 off = elf_tdata (abfd)->next_file_pos;
7617 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
7619 /* Note that at this point elf_tdata (abfd)->next_file_pos is
7620 incorrect. We do not yet know the size of the .symtab section.
7621 We correct next_file_pos below, after we do know the size. */
7623 /* Allocate a buffer to hold swapped out symbols. This is to avoid
7624 continuously seeking to the right position in the file. */
7625 if (! info->keep_memory || max_sym_count < 20)
7626 finfo.symbuf_size = 20;
7627 else
7628 finfo.symbuf_size = max_sym_count;
7629 amt = finfo.symbuf_size;
7630 amt *= bed->s->sizeof_sym;
7631 finfo.symbuf = bfd_malloc (amt);
7632 if (finfo.symbuf == NULL)
7633 goto error_return;
7634 if (elf_numsections (abfd) > SHN_LORESERVE)
7636 /* Wild guess at number of output symbols. realloc'd as needed. */
7637 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
7638 finfo.shndxbuf_size = amt;
7639 amt *= sizeof (Elf_External_Sym_Shndx);
7640 finfo.symshndxbuf = bfd_zmalloc (amt);
7641 if (finfo.symshndxbuf == NULL)
7642 goto error_return;
7645 /* Start writing out the symbol table. The first symbol is always a
7646 dummy symbol. */
7647 if (info->strip != strip_all
7648 || emit_relocs)
7650 elfsym.st_value = 0;
7651 elfsym.st_size = 0;
7652 elfsym.st_info = 0;
7653 elfsym.st_other = 0;
7654 elfsym.st_shndx = SHN_UNDEF;
7655 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
7656 NULL))
7657 goto error_return;
7660 #if 0
7661 /* Some standard ELF linkers do this, but we don't because it causes
7662 bootstrap comparison failures. */
7663 /* Output a file symbol for the output file as the second symbol.
7664 We output this even if we are discarding local symbols, although
7665 I'm not sure if this is correct. */
7666 elfsym.st_value = 0;
7667 elfsym.st_size = 0;
7668 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
7669 elfsym.st_other = 0;
7670 elfsym.st_shndx = SHN_ABS;
7671 if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd),
7672 &elfsym, bfd_abs_section_ptr, NULL))
7673 goto error_return;
7674 #endif
7676 /* Output a symbol for each section. We output these even if we are
7677 discarding local symbols, since they are used for relocs. These
7678 symbols have no names. We store the index of each one in the
7679 index field of the section, so that we can find it again when
7680 outputting relocs. */
7681 if (info->strip != strip_all
7682 || emit_relocs)
7684 elfsym.st_size = 0;
7685 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
7686 elfsym.st_other = 0;
7687 for (i = 1; i < elf_numsections (abfd); i++)
7689 o = bfd_section_from_elf_index (abfd, i);
7690 if (o != NULL)
7691 o->target_index = bfd_get_symcount (abfd);
7692 elfsym.st_shndx = i;
7693 if (info->relocatable || o == NULL)
7694 elfsym.st_value = 0;
7695 else
7696 elfsym.st_value = o->vma;
7697 if (! elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
7698 goto error_return;
7699 if (i == SHN_LORESERVE - 1)
7700 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
7704 /* Allocate some memory to hold information read in from the input
7705 files. */
7706 if (max_contents_size != 0)
7708 finfo.contents = bfd_malloc (max_contents_size);
7709 if (finfo.contents == NULL)
7710 goto error_return;
7713 if (max_external_reloc_size != 0)
7715 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
7716 if (finfo.external_relocs == NULL)
7717 goto error_return;
7720 if (max_internal_reloc_count != 0)
7722 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
7723 amt *= sizeof (Elf_Internal_Rela);
7724 finfo.internal_relocs = bfd_malloc (amt);
7725 if (finfo.internal_relocs == NULL)
7726 goto error_return;
7729 if (max_sym_count != 0)
7731 amt = max_sym_count * bed->s->sizeof_sym;
7732 finfo.external_syms = bfd_malloc (amt);
7733 if (finfo.external_syms == NULL)
7734 goto error_return;
7736 amt = max_sym_count * sizeof (Elf_Internal_Sym);
7737 finfo.internal_syms = bfd_malloc (amt);
7738 if (finfo.internal_syms == NULL)
7739 goto error_return;
7741 amt = max_sym_count * sizeof (long);
7742 finfo.indices = bfd_malloc (amt);
7743 if (finfo.indices == NULL)
7744 goto error_return;
7746 amt = max_sym_count * sizeof (asection *);
7747 finfo.sections = bfd_malloc (amt);
7748 if (finfo.sections == NULL)
7749 goto error_return;
7752 if (max_sym_shndx_count != 0)
7754 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
7755 finfo.locsym_shndx = bfd_malloc (amt);
7756 if (finfo.locsym_shndx == NULL)
7757 goto error_return;
7760 if (elf_hash_table (info)->tls_sec)
7762 bfd_vma base, end = 0;
7763 asection *sec;
7765 for (sec = elf_hash_table (info)->tls_sec;
7766 sec && (sec->flags & SEC_THREAD_LOCAL);
7767 sec = sec->next)
7769 bfd_vma size = sec->size;
7771 if (size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0)
7773 struct bfd_link_order *o;
7775 for (o = sec->link_order_head; o != NULL; o = o->next)
7776 if (size < o->offset + o->size)
7777 size = o->offset + o->size;
7779 end = sec->vma + size;
7781 base = elf_hash_table (info)->tls_sec->vma;
7782 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
7783 elf_hash_table (info)->tls_size = end - base;
7786 /* Reorder SHF_LINK_ORDER sections. */
7787 for (o = abfd->sections; o != NULL; o = o->next)
7789 if (!elf_fixup_link_order (abfd, o))
7790 return FALSE;
7793 /* Since ELF permits relocations to be against local symbols, we
7794 must have the local symbols available when we do the relocations.
7795 Since we would rather only read the local symbols once, and we
7796 would rather not keep them in memory, we handle all the
7797 relocations for a single input file at the same time.
7799 Unfortunately, there is no way to know the total number of local
7800 symbols until we have seen all of them, and the local symbol
7801 indices precede the global symbol indices. This means that when
7802 we are generating relocatable output, and we see a reloc against
7803 a global symbol, we can not know the symbol index until we have
7804 finished examining all the local symbols to see which ones we are
7805 going to output. To deal with this, we keep the relocations in
7806 memory, and don't output them until the end of the link. This is
7807 an unfortunate waste of memory, but I don't see a good way around
7808 it. Fortunately, it only happens when performing a relocatable
7809 link, which is not the common case. FIXME: If keep_memory is set
7810 we could write the relocs out and then read them again; I don't
7811 know how bad the memory loss will be. */
7813 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
7814 sub->output_has_begun = FALSE;
7815 for (o = abfd->sections; o != NULL; o = o->next)
7817 for (p = o->link_order_head; p != NULL; p = p->next)
7819 if (p->type == bfd_indirect_link_order
7820 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
7821 == bfd_target_elf_flavour)
7822 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
7824 if (! sub->output_has_begun)
7826 if (! elf_link_input_bfd (&finfo, sub))
7827 goto error_return;
7828 sub->output_has_begun = TRUE;
7831 else if (p->type == bfd_section_reloc_link_order
7832 || p->type == bfd_symbol_reloc_link_order)
7834 if (! elf_reloc_link_order (abfd, info, o, p))
7835 goto error_return;
7837 else
7839 if (! _bfd_default_link_order (abfd, info, o, p))
7840 goto error_return;
7845 /* Output any global symbols that got converted to local in a
7846 version script or due to symbol visibility. We do this in a
7847 separate step since ELF requires all local symbols to appear
7848 prior to any global symbols. FIXME: We should only do this if
7849 some global symbols were, in fact, converted to become local.
7850 FIXME: Will this work correctly with the Irix 5 linker? */
7851 eoinfo.failed = FALSE;
7852 eoinfo.finfo = &finfo;
7853 eoinfo.localsyms = TRUE;
7854 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
7855 &eoinfo);
7856 if (eoinfo.failed)
7857 return FALSE;
7859 /* That wrote out all the local symbols. Finish up the symbol table
7860 with the global symbols. Even if we want to strip everything we
7861 can, we still need to deal with those global symbols that got
7862 converted to local in a version script. */
7864 /* The sh_info field records the index of the first non local symbol. */
7865 symtab_hdr->sh_info = bfd_get_symcount (abfd);
7867 if (dynamic
7868 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
7870 Elf_Internal_Sym sym;
7871 bfd_byte *dynsym = finfo.dynsym_sec->contents;
7872 long last_local = 0;
7874 /* Write out the section symbols for the output sections. */
7875 if (info->shared)
7877 asection *s;
7879 sym.st_size = 0;
7880 sym.st_name = 0;
7881 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
7882 sym.st_other = 0;
7884 for (s = abfd->sections; s != NULL; s = s->next)
7886 int indx;
7887 bfd_byte *dest;
7888 long dynindx;
7890 dynindx = elf_section_data (s)->dynindx;
7891 if (dynindx <= 0)
7892 continue;
7893 indx = elf_section_data (s)->this_idx;
7894 BFD_ASSERT (indx > 0);
7895 sym.st_shndx = indx;
7896 sym.st_value = s->vma;
7897 dest = dynsym + dynindx * bed->s->sizeof_sym;
7898 if (last_local < dynindx)
7899 last_local = dynindx;
7900 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
7904 /* Write out the local dynsyms. */
7905 if (elf_hash_table (info)->dynlocal)
7907 struct elf_link_local_dynamic_entry *e;
7908 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
7910 asection *s;
7911 bfd_byte *dest;
7913 sym.st_size = e->isym.st_size;
7914 sym.st_other = e->isym.st_other;
7916 /* Copy the internal symbol as is.
7917 Note that we saved a word of storage and overwrote
7918 the original st_name with the dynstr_index. */
7919 sym = e->isym;
7921 if (e->isym.st_shndx != SHN_UNDEF
7922 && (e->isym.st_shndx < SHN_LORESERVE
7923 || e->isym.st_shndx > SHN_HIRESERVE))
7925 s = bfd_section_from_elf_index (e->input_bfd,
7926 e->isym.st_shndx);
7928 sym.st_shndx =
7929 elf_section_data (s->output_section)->this_idx;
7930 sym.st_value = (s->output_section->vma
7931 + s->output_offset
7932 + e->isym.st_value);
7935 if (last_local < e->dynindx)
7936 last_local = e->dynindx;
7938 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
7939 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
7943 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
7944 last_local + 1;
7947 /* We get the global symbols from the hash table. */
7948 eoinfo.failed = FALSE;
7949 eoinfo.localsyms = FALSE;
7950 eoinfo.finfo = &finfo;
7951 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
7952 &eoinfo);
7953 if (eoinfo.failed)
7954 return FALSE;
7956 /* If backend needs to output some symbols not present in the hash
7957 table, do it now. */
7958 if (bed->elf_backend_output_arch_syms)
7960 typedef bfd_boolean (*out_sym_func)
7961 (void *, const char *, Elf_Internal_Sym *, asection *,
7962 struct elf_link_hash_entry *);
7964 if (! ((*bed->elf_backend_output_arch_syms)
7965 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
7966 return FALSE;
7969 /* Flush all symbols to the file. */
7970 if (! elf_link_flush_output_syms (&finfo, bed))
7971 return FALSE;
7973 /* Now we know the size of the symtab section. */
7974 off += symtab_hdr->sh_size;
7976 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
7977 if (symtab_shndx_hdr->sh_name != 0)
7979 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
7980 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
7981 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
7982 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
7983 symtab_shndx_hdr->sh_size = amt;
7985 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
7986 off, TRUE);
7988 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
7989 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
7990 return FALSE;
7994 /* Finish up and write out the symbol string table (.strtab)
7995 section. */
7996 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
7997 /* sh_name was set in prep_headers. */
7998 symstrtab_hdr->sh_type = SHT_STRTAB;
7999 symstrtab_hdr->sh_flags = 0;
8000 symstrtab_hdr->sh_addr = 0;
8001 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
8002 symstrtab_hdr->sh_entsize = 0;
8003 symstrtab_hdr->sh_link = 0;
8004 symstrtab_hdr->sh_info = 0;
8005 /* sh_offset is set just below. */
8006 symstrtab_hdr->sh_addralign = 1;
8008 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
8009 elf_tdata (abfd)->next_file_pos = off;
8011 if (bfd_get_symcount (abfd) > 0)
8013 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
8014 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
8015 return FALSE;
8018 /* Adjust the relocs to have the correct symbol indices. */
8019 for (o = abfd->sections; o != NULL; o = o->next)
8021 if ((o->flags & SEC_RELOC) == 0)
8022 continue;
8024 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
8025 elf_section_data (o)->rel_count,
8026 elf_section_data (o)->rel_hashes);
8027 if (elf_section_data (o)->rel_hdr2 != NULL)
8028 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
8029 elf_section_data (o)->rel_count2,
8030 (elf_section_data (o)->rel_hashes
8031 + elf_section_data (o)->rel_count));
8033 /* Set the reloc_count field to 0 to prevent write_relocs from
8034 trying to swap the relocs out itself. */
8035 o->reloc_count = 0;
8038 if (dynamic && info->combreloc && dynobj != NULL)
8039 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
8041 /* If we are linking against a dynamic object, or generating a
8042 shared library, finish up the dynamic linking information. */
8043 if (dynamic)
8045 bfd_byte *dyncon, *dynconend;
8047 /* Fix up .dynamic entries. */
8048 o = bfd_get_section_by_name (dynobj, ".dynamic");
8049 BFD_ASSERT (o != NULL);
8051 dyncon = o->contents;
8052 dynconend = o->contents + o->size;
8053 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
8055 Elf_Internal_Dyn dyn;
8056 const char *name;
8057 unsigned int type;
8059 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
8061 switch (dyn.d_tag)
8063 default:
8064 continue;
8065 case DT_NULL:
8066 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
8068 switch (elf_section_data (reldyn)->this_hdr.sh_type)
8070 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
8071 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
8072 default: continue;
8074 dyn.d_un.d_val = relativecount;
8075 relativecount = 0;
8076 break;
8078 continue;
8080 case DT_INIT:
8081 name = info->init_function;
8082 goto get_sym;
8083 case DT_FINI:
8084 name = info->fini_function;
8085 get_sym:
8087 struct elf_link_hash_entry *h;
8089 h = elf_link_hash_lookup (elf_hash_table (info), name,
8090 FALSE, FALSE, TRUE);
8091 if (h != NULL
8092 && (h->root.type == bfd_link_hash_defined
8093 || h->root.type == bfd_link_hash_defweak))
8095 dyn.d_un.d_val = h->root.u.def.value;
8096 o = h->root.u.def.section;
8097 if (o->output_section != NULL)
8098 dyn.d_un.d_val += (o->output_section->vma
8099 + o->output_offset);
8100 else
8102 /* The symbol is imported from another shared
8103 library and does not apply to this one. */
8104 dyn.d_un.d_val = 0;
8106 break;
8109 continue;
8111 case DT_PREINIT_ARRAYSZ:
8112 name = ".preinit_array";
8113 goto get_size;
8114 case DT_INIT_ARRAYSZ:
8115 name = ".init_array";
8116 goto get_size;
8117 case DT_FINI_ARRAYSZ:
8118 name = ".fini_array";
8119 get_size:
8120 o = bfd_get_section_by_name (abfd, name);
8121 if (o == NULL)
8123 (*_bfd_error_handler)
8124 (_("%B: could not find output section %s"), abfd, name);
8125 goto error_return;
8127 if (o->size == 0)
8128 (*_bfd_error_handler)
8129 (_("warning: %s section has zero size"), name);
8130 dyn.d_un.d_val = o->size;
8131 break;
8133 case DT_PREINIT_ARRAY:
8134 name = ".preinit_array";
8135 goto get_vma;
8136 case DT_INIT_ARRAY:
8137 name = ".init_array";
8138 goto get_vma;
8139 case DT_FINI_ARRAY:
8140 name = ".fini_array";
8141 goto get_vma;
8143 case DT_HASH:
8144 name = ".hash";
8145 goto get_vma;
8146 case DT_STRTAB:
8147 name = ".dynstr";
8148 goto get_vma;
8149 case DT_SYMTAB:
8150 name = ".dynsym";
8151 goto get_vma;
8152 case DT_VERDEF:
8153 name = ".gnu.version_d";
8154 goto get_vma;
8155 case DT_VERNEED:
8156 name = ".gnu.version_r";
8157 goto get_vma;
8158 case DT_VERSYM:
8159 name = ".gnu.version";
8160 get_vma:
8161 o = bfd_get_section_by_name (abfd, name);
8162 if (o == NULL)
8164 (*_bfd_error_handler)
8165 (_("%B: could not find output section %s"), abfd, name);
8166 goto error_return;
8168 dyn.d_un.d_ptr = o->vma;
8169 break;
8171 case DT_REL:
8172 case DT_RELA:
8173 case DT_RELSZ:
8174 case DT_RELASZ:
8175 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
8176 type = SHT_REL;
8177 else
8178 type = SHT_RELA;
8179 dyn.d_un.d_val = 0;
8180 for (i = 1; i < elf_numsections (abfd); i++)
8182 Elf_Internal_Shdr *hdr;
8184 hdr = elf_elfsections (abfd)[i];
8185 if (hdr->sh_type == type
8186 && (hdr->sh_flags & SHF_ALLOC) != 0)
8188 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
8189 dyn.d_un.d_val += hdr->sh_size;
8190 else
8192 if (dyn.d_un.d_val == 0
8193 || hdr->sh_addr < dyn.d_un.d_val)
8194 dyn.d_un.d_val = hdr->sh_addr;
8198 break;
8200 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
8204 /* If we have created any dynamic sections, then output them. */
8205 if (dynobj != NULL)
8207 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
8208 goto error_return;
8210 for (o = dynobj->sections; o != NULL; o = o->next)
8212 if ((o->flags & SEC_HAS_CONTENTS) == 0
8213 || o->size == 0
8214 || o->output_section == bfd_abs_section_ptr)
8215 continue;
8216 if ((o->flags & SEC_LINKER_CREATED) == 0)
8218 /* At this point, we are only interested in sections
8219 created by _bfd_elf_link_create_dynamic_sections. */
8220 continue;
8222 if (elf_hash_table (info)->stab_info.stabstr == o)
8223 continue;
8224 if (elf_hash_table (info)->eh_info.hdr_sec == o)
8225 continue;
8226 if ((elf_section_data (o->output_section)->this_hdr.sh_type
8227 != SHT_STRTAB)
8228 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
8230 if (! bfd_set_section_contents (abfd, o->output_section,
8231 o->contents,
8232 (file_ptr) o->output_offset,
8233 o->size))
8234 goto error_return;
8236 else
8238 /* The contents of the .dynstr section are actually in a
8239 stringtab. */
8240 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
8241 if (bfd_seek (abfd, off, SEEK_SET) != 0
8242 || ! _bfd_elf_strtab_emit (abfd,
8243 elf_hash_table (info)->dynstr))
8244 goto error_return;
8249 if (info->relocatable)
8251 bfd_boolean failed = FALSE;
8253 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
8254 if (failed)
8255 goto error_return;
8258 /* If we have optimized stabs strings, output them. */
8259 if (elf_hash_table (info)->stab_info.stabstr != NULL)
8261 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
8262 goto error_return;
8265 if (info->eh_frame_hdr)
8267 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
8268 goto error_return;
8271 if (finfo.symstrtab != NULL)
8272 _bfd_stringtab_free (finfo.symstrtab);
8273 if (finfo.contents != NULL)
8274 free (finfo.contents);
8275 if (finfo.external_relocs != NULL)
8276 free (finfo.external_relocs);
8277 if (finfo.internal_relocs != NULL)
8278 free (finfo.internal_relocs);
8279 if (finfo.external_syms != NULL)
8280 free (finfo.external_syms);
8281 if (finfo.locsym_shndx != NULL)
8282 free (finfo.locsym_shndx);
8283 if (finfo.internal_syms != NULL)
8284 free (finfo.internal_syms);
8285 if (finfo.indices != NULL)
8286 free (finfo.indices);
8287 if (finfo.sections != NULL)
8288 free (finfo.sections);
8289 if (finfo.symbuf != NULL)
8290 free (finfo.symbuf);
8291 if (finfo.symshndxbuf != NULL)
8292 free (finfo.symshndxbuf);
8293 for (o = abfd->sections; o != NULL; o = o->next)
8295 if ((o->flags & SEC_RELOC) != 0
8296 && elf_section_data (o)->rel_hashes != NULL)
8297 free (elf_section_data (o)->rel_hashes);
8300 elf_tdata (abfd)->linker = TRUE;
8302 return TRUE;
8304 error_return:
8305 if (finfo.symstrtab != NULL)
8306 _bfd_stringtab_free (finfo.symstrtab);
8307 if (finfo.contents != NULL)
8308 free (finfo.contents);
8309 if (finfo.external_relocs != NULL)
8310 free (finfo.external_relocs);
8311 if (finfo.internal_relocs != NULL)
8312 free (finfo.internal_relocs);
8313 if (finfo.external_syms != NULL)
8314 free (finfo.external_syms);
8315 if (finfo.locsym_shndx != NULL)
8316 free (finfo.locsym_shndx);
8317 if (finfo.internal_syms != NULL)
8318 free (finfo.internal_syms);
8319 if (finfo.indices != NULL)
8320 free (finfo.indices);
8321 if (finfo.sections != NULL)
8322 free (finfo.sections);
8323 if (finfo.symbuf != NULL)
8324 free (finfo.symbuf);
8325 if (finfo.symshndxbuf != NULL)
8326 free (finfo.symshndxbuf);
8327 for (o = abfd->sections; o != NULL; o = o->next)
8329 if ((o->flags & SEC_RELOC) != 0
8330 && elf_section_data (o)->rel_hashes != NULL)
8331 free (elf_section_data (o)->rel_hashes);
8334 return FALSE;
8337 /* Garbage collect unused sections. */
8339 /* The mark phase of garbage collection. For a given section, mark
8340 it and any sections in this section's group, and all the sections
8341 which define symbols to which it refers. */
8343 typedef asection * (*gc_mark_hook_fn)
8344 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
8345 struct elf_link_hash_entry *, Elf_Internal_Sym *);
8347 bfd_boolean
8348 _bfd_elf_gc_mark (struct bfd_link_info *info,
8349 asection *sec,
8350 gc_mark_hook_fn gc_mark_hook)
8352 bfd_boolean ret;
8353 asection *group_sec;
8355 sec->gc_mark = 1;
8357 /* Mark all the sections in the group. */
8358 group_sec = elf_section_data (sec)->next_in_group;
8359 if (group_sec && !group_sec->gc_mark)
8360 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
8361 return FALSE;
8363 /* Look through the section relocs. */
8364 ret = TRUE;
8365 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
8367 Elf_Internal_Rela *relstart, *rel, *relend;
8368 Elf_Internal_Shdr *symtab_hdr;
8369 struct elf_link_hash_entry **sym_hashes;
8370 size_t nlocsyms;
8371 size_t extsymoff;
8372 bfd *input_bfd = sec->owner;
8373 const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
8374 Elf_Internal_Sym *isym = NULL;
8375 int r_sym_shift;
8377 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8378 sym_hashes = elf_sym_hashes (input_bfd);
8380 /* Read the local symbols. */
8381 if (elf_bad_symtab (input_bfd))
8383 nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym;
8384 extsymoff = 0;
8386 else
8387 extsymoff = nlocsyms = symtab_hdr->sh_info;
8389 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
8390 if (isym == NULL && nlocsyms != 0)
8392 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
8393 NULL, NULL, NULL);
8394 if (isym == NULL)
8395 return FALSE;
8398 /* Read the relocations. */
8399 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
8400 info->keep_memory);
8401 if (relstart == NULL)
8403 ret = FALSE;
8404 goto out1;
8406 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
8408 if (bed->s->arch_size == 32)
8409 r_sym_shift = 8;
8410 else
8411 r_sym_shift = 32;
8413 for (rel = relstart; rel < relend; rel++)
8415 unsigned long r_symndx;
8416 asection *rsec;
8417 struct elf_link_hash_entry *h;
8419 r_symndx = rel->r_info >> r_sym_shift;
8420 if (r_symndx == 0)
8421 continue;
8423 if (r_symndx >= nlocsyms
8424 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
8426 h = sym_hashes[r_symndx - extsymoff];
8427 while (h->root.type == bfd_link_hash_indirect
8428 || h->root.type == bfd_link_hash_warning)
8429 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8430 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
8432 else
8434 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
8437 if (rsec && !rsec->gc_mark)
8439 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
8440 rsec->gc_mark = 1;
8441 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
8443 ret = FALSE;
8444 goto out2;
8449 out2:
8450 if (elf_section_data (sec)->relocs != relstart)
8451 free (relstart);
8452 out1:
8453 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
8455 if (! info->keep_memory)
8456 free (isym);
8457 else
8458 symtab_hdr->contents = (unsigned char *) isym;
8462 return ret;
8465 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8467 static bfd_boolean
8468 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *idxptr)
8470 int *idx = idxptr;
8472 if (h->root.type == bfd_link_hash_warning)
8473 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8475 if (h->dynindx != -1
8476 && ((h->root.type != bfd_link_hash_defined
8477 && h->root.type != bfd_link_hash_defweak)
8478 || h->root.u.def.section->gc_mark))
8479 h->dynindx = (*idx)++;
8481 return TRUE;
8484 /* The sweep phase of garbage collection. Remove all garbage sections. */
8486 typedef bfd_boolean (*gc_sweep_hook_fn)
8487 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
8489 static bfd_boolean
8490 elf_gc_sweep (struct bfd_link_info *info, gc_sweep_hook_fn gc_sweep_hook)
8492 bfd *sub;
8494 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8496 asection *o;
8498 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
8499 continue;
8501 for (o = sub->sections; o != NULL; o = o->next)
8503 /* Keep debug and special sections. */
8504 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
8505 || (o->flags & (SEC_ALLOC | SEC_LOAD)) == 0)
8506 o->gc_mark = 1;
8508 if (o->gc_mark)
8509 continue;
8511 /* Skip sweeping sections already excluded. */
8512 if (o->flags & SEC_EXCLUDE)
8513 continue;
8515 /* Since this is early in the link process, it is simple
8516 to remove a section from the output. */
8517 o->flags |= SEC_EXCLUDE;
8519 /* But we also have to update some of the relocation
8520 info we collected before. */
8521 if (gc_sweep_hook
8522 && (o->flags & SEC_RELOC) && o->reloc_count > 0)
8524 Elf_Internal_Rela *internal_relocs;
8525 bfd_boolean r;
8527 internal_relocs
8528 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
8529 info->keep_memory);
8530 if (internal_relocs == NULL)
8531 return FALSE;
8533 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
8535 if (elf_section_data (o)->relocs != internal_relocs)
8536 free (internal_relocs);
8538 if (!r)
8539 return FALSE;
8544 /* Remove the symbols that were in the swept sections from the dynamic
8545 symbol table. GCFIXME: Anyone know how to get them out of the
8546 static symbol table as well? */
8548 int i = 0;
8550 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, &i);
8552 elf_hash_table (info)->dynsymcount = i;
8555 return TRUE;
8558 /* Propagate collected vtable information. This is called through
8559 elf_link_hash_traverse. */
8561 static bfd_boolean
8562 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
8564 if (h->root.type == bfd_link_hash_warning)
8565 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8567 /* Those that are not vtables. */
8568 if (h->vtable_parent == NULL)
8569 return TRUE;
8571 /* Those vtables that do not have parents, we cannot merge. */
8572 if (h->vtable_parent == (struct elf_link_hash_entry *) -1)
8573 return TRUE;
8575 /* If we've already been done, exit. */
8576 if (h->vtable_entries_used && h->vtable_entries_used[-1])
8577 return TRUE;
8579 /* Make sure the parent's table is up to date. */
8580 elf_gc_propagate_vtable_entries_used (h->vtable_parent, okp);
8582 if (h->vtable_entries_used == NULL)
8584 /* None of this table's entries were referenced. Re-use the
8585 parent's table. */
8586 h->vtable_entries_used = h->vtable_parent->vtable_entries_used;
8587 h->vtable_entries_size = h->vtable_parent->vtable_entries_size;
8589 else
8591 size_t n;
8592 bfd_boolean *cu, *pu;
8594 /* Or the parent's entries into ours. */
8595 cu = h->vtable_entries_used;
8596 cu[-1] = TRUE;
8597 pu = h->vtable_parent->vtable_entries_used;
8598 if (pu != NULL)
8600 const struct elf_backend_data *bed;
8601 unsigned int log_file_align;
8603 bed = get_elf_backend_data (h->root.u.def.section->owner);
8604 log_file_align = bed->s->log_file_align;
8605 n = h->vtable_parent->vtable_entries_size >> log_file_align;
8606 while (n--)
8608 if (*pu)
8609 *cu = TRUE;
8610 pu++;
8611 cu++;
8616 return TRUE;
8619 static bfd_boolean
8620 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
8622 asection *sec;
8623 bfd_vma hstart, hend;
8624 Elf_Internal_Rela *relstart, *relend, *rel;
8625 const struct elf_backend_data *bed;
8626 unsigned int log_file_align;
8628 if (h->root.type == bfd_link_hash_warning)
8629 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8631 /* Take care of both those symbols that do not describe vtables as
8632 well as those that are not loaded. */
8633 if (h->vtable_parent == NULL)
8634 return TRUE;
8636 BFD_ASSERT (h->root.type == bfd_link_hash_defined
8637 || h->root.type == bfd_link_hash_defweak);
8639 sec = h->root.u.def.section;
8640 hstart = h->root.u.def.value;
8641 hend = hstart + h->size;
8643 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
8644 if (!relstart)
8645 return *(bfd_boolean *) okp = FALSE;
8646 bed = get_elf_backend_data (sec->owner);
8647 log_file_align = bed->s->log_file_align;
8649 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
8651 for (rel = relstart; rel < relend; ++rel)
8652 if (rel->r_offset >= hstart && rel->r_offset < hend)
8654 /* If the entry is in use, do nothing. */
8655 if (h->vtable_entries_used
8656 && (rel->r_offset - hstart) < h->vtable_entries_size)
8658 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
8659 if (h->vtable_entries_used[entry])
8660 continue;
8662 /* Otherwise, kill it. */
8663 rel->r_offset = rel->r_info = rel->r_addend = 0;
8666 return TRUE;
8669 /* Mark sections containing dynamically referenced symbols. This is called
8670 through elf_link_hash_traverse. */
8672 static bfd_boolean
8673 elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h,
8674 void *okp ATTRIBUTE_UNUSED)
8676 if (h->root.type == bfd_link_hash_warning)
8677 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8679 if ((h->root.type == bfd_link_hash_defined
8680 || h->root.type == bfd_link_hash_defweak)
8681 && h->ref_dynamic)
8682 h->root.u.def.section->flags |= SEC_KEEP;
8684 return TRUE;
8687 /* Do mark and sweep of unused sections. */
8689 bfd_boolean
8690 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
8692 bfd_boolean ok = TRUE;
8693 bfd *sub;
8694 asection * (*gc_mark_hook)
8695 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
8696 struct elf_link_hash_entry *h, Elf_Internal_Sym *);
8698 if (!get_elf_backend_data (abfd)->can_gc_sections
8699 || info->relocatable
8700 || info->emitrelocations
8701 || info->shared
8702 || !is_elf_hash_table (info->hash))
8704 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
8705 return TRUE;
8708 /* Apply transitive closure to the vtable entry usage info. */
8709 elf_link_hash_traverse (elf_hash_table (info),
8710 elf_gc_propagate_vtable_entries_used,
8711 &ok);
8712 if (!ok)
8713 return FALSE;
8715 /* Kill the vtable relocations that were not used. */
8716 elf_link_hash_traverse (elf_hash_table (info),
8717 elf_gc_smash_unused_vtentry_relocs,
8718 &ok);
8719 if (!ok)
8720 return FALSE;
8722 /* Mark dynamically referenced symbols. */
8723 if (elf_hash_table (info)->dynamic_sections_created)
8724 elf_link_hash_traverse (elf_hash_table (info),
8725 elf_gc_mark_dynamic_ref_symbol,
8726 &ok);
8727 if (!ok)
8728 return FALSE;
8730 /* Grovel through relocs to find out who stays ... */
8731 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
8732 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8734 asection *o;
8736 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
8737 continue;
8739 for (o = sub->sections; o != NULL; o = o->next)
8741 if (o->flags & SEC_KEEP)
8743 /* _bfd_elf_discard_section_eh_frame knows how to discard
8744 orphaned FDEs so don't mark sections referenced by the
8745 EH frame section. */
8746 if (strcmp (o->name, ".eh_frame") == 0)
8747 o->gc_mark = 1;
8748 else if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
8749 return FALSE;
8754 /* ... and mark SEC_EXCLUDE for those that go. */
8755 if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook))
8756 return FALSE;
8758 return TRUE;
8761 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
8763 bfd_boolean
8764 bfd_elf_gc_record_vtinherit (bfd *abfd,
8765 asection *sec,
8766 struct elf_link_hash_entry *h,
8767 bfd_vma offset)
8769 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
8770 struct elf_link_hash_entry **search, *child;
8771 bfd_size_type extsymcount;
8772 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8774 /* The sh_info field of the symtab header tells us where the
8775 external symbols start. We don't care about the local symbols at
8776 this point. */
8777 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
8778 if (!elf_bad_symtab (abfd))
8779 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
8781 sym_hashes = elf_sym_hashes (abfd);
8782 sym_hashes_end = sym_hashes + extsymcount;
8784 /* Hunt down the child symbol, which is in this section at the same
8785 offset as the relocation. */
8786 for (search = sym_hashes; search != sym_hashes_end; ++search)
8788 if ((child = *search) != NULL
8789 && (child->root.type == bfd_link_hash_defined
8790 || child->root.type == bfd_link_hash_defweak)
8791 && child->root.u.def.section == sec
8792 && child->root.u.def.value == offset)
8793 goto win;
8796 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
8797 abfd, sec, (unsigned long) offset);
8798 bfd_set_error (bfd_error_invalid_operation);
8799 return FALSE;
8801 win:
8802 if (!h)
8804 /* This *should* only be the absolute section. It could potentially
8805 be that someone has defined a non-global vtable though, which
8806 would be bad. It isn't worth paging in the local symbols to be
8807 sure though; that case should simply be handled by the assembler. */
8809 child->vtable_parent = (struct elf_link_hash_entry *) -1;
8811 else
8812 child->vtable_parent = h;
8814 return TRUE;
8817 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
8819 bfd_boolean
8820 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
8821 asection *sec ATTRIBUTE_UNUSED,
8822 struct elf_link_hash_entry *h,
8823 bfd_vma addend)
8825 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8826 unsigned int log_file_align = bed->s->log_file_align;
8828 if (addend >= h->vtable_entries_size)
8830 size_t size, bytes, file_align;
8831 bfd_boolean *ptr = h->vtable_entries_used;
8833 /* While the symbol is undefined, we have to be prepared to handle
8834 a zero size. */
8835 file_align = 1 << log_file_align;
8836 if (h->root.type == bfd_link_hash_undefined)
8837 size = addend + file_align;
8838 else
8840 size = h->size;
8841 if (addend >= size)
8843 /* Oops! We've got a reference past the defined end of
8844 the table. This is probably a bug -- shall we warn? */
8845 size = addend + file_align;
8848 size = (size + file_align - 1) & -file_align;
8850 /* Allocate one extra entry for use as a "done" flag for the
8851 consolidation pass. */
8852 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
8854 if (ptr)
8856 ptr = bfd_realloc (ptr - 1, bytes);
8858 if (ptr != NULL)
8860 size_t oldbytes;
8862 oldbytes = (((h->vtable_entries_size >> log_file_align) + 1)
8863 * sizeof (bfd_boolean));
8864 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
8867 else
8868 ptr = bfd_zmalloc (bytes);
8870 if (ptr == NULL)
8871 return FALSE;
8873 /* And arrange for that done flag to be at index -1. */
8874 h->vtable_entries_used = ptr + 1;
8875 h->vtable_entries_size = size;
8878 h->vtable_entries_used[addend >> log_file_align] = TRUE;
8880 return TRUE;
8883 struct alloc_got_off_arg {
8884 bfd_vma gotoff;
8885 unsigned int got_elt_size;
8888 /* We need a special top-level link routine to convert got reference counts
8889 to real got offsets. */
8891 static bfd_boolean
8892 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
8894 struct alloc_got_off_arg *gofarg = arg;
8896 if (h->root.type == bfd_link_hash_warning)
8897 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8899 if (h->got.refcount > 0)
8901 h->got.offset = gofarg->gotoff;
8902 gofarg->gotoff += gofarg->got_elt_size;
8904 else
8905 h->got.offset = (bfd_vma) -1;
8907 return TRUE;
8910 /* And an accompanying bit to work out final got entry offsets once
8911 we're done. Should be called from final_link. */
8913 bfd_boolean
8914 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
8915 struct bfd_link_info *info)
8917 bfd *i;
8918 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8919 bfd_vma gotoff;
8920 unsigned int got_elt_size = bed->s->arch_size / 8;
8921 struct alloc_got_off_arg gofarg;
8923 if (! is_elf_hash_table (info->hash))
8924 return FALSE;
8926 /* The GOT offset is relative to the .got section, but the GOT header is
8927 put into the .got.plt section, if the backend uses it. */
8928 if (bed->want_got_plt)
8929 gotoff = 0;
8930 else
8931 gotoff = bed->got_header_size;
8933 /* Do the local .got entries first. */
8934 for (i = info->input_bfds; i; i = i->link_next)
8936 bfd_signed_vma *local_got;
8937 bfd_size_type j, locsymcount;
8938 Elf_Internal_Shdr *symtab_hdr;
8940 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
8941 continue;
8943 local_got = elf_local_got_refcounts (i);
8944 if (!local_got)
8945 continue;
8947 symtab_hdr = &elf_tdata (i)->symtab_hdr;
8948 if (elf_bad_symtab (i))
8949 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
8950 else
8951 locsymcount = symtab_hdr->sh_info;
8953 for (j = 0; j < locsymcount; ++j)
8955 if (local_got[j] > 0)
8957 local_got[j] = gotoff;
8958 gotoff += got_elt_size;
8960 else
8961 local_got[j] = (bfd_vma) -1;
8965 /* Then the global .got entries. .plt refcounts are handled by
8966 adjust_dynamic_symbol */
8967 gofarg.gotoff = gotoff;
8968 gofarg.got_elt_size = got_elt_size;
8969 elf_link_hash_traverse (elf_hash_table (info),
8970 elf_gc_allocate_got_offsets,
8971 &gofarg);
8972 return TRUE;
8975 /* Many folk need no more in the way of final link than this, once
8976 got entry reference counting is enabled. */
8978 bfd_boolean
8979 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
8981 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
8982 return FALSE;
8984 /* Invoke the regular ELF backend linker to do all the work. */
8985 return bfd_elf_final_link (abfd, info);
8988 bfd_boolean
8989 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
8991 struct elf_reloc_cookie *rcookie = cookie;
8993 if (rcookie->bad_symtab)
8994 rcookie->rel = rcookie->rels;
8996 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
8998 unsigned long r_symndx;
9000 if (! rcookie->bad_symtab)
9001 if (rcookie->rel->r_offset > offset)
9002 return FALSE;
9003 if (rcookie->rel->r_offset != offset)
9004 continue;
9006 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
9007 if (r_symndx == SHN_UNDEF)
9008 return TRUE;
9010 if (r_symndx >= rcookie->locsymcount
9011 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
9013 struct elf_link_hash_entry *h;
9015 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
9017 while (h->root.type == bfd_link_hash_indirect
9018 || h->root.type == bfd_link_hash_warning)
9019 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9021 if ((h->root.type == bfd_link_hash_defined
9022 || h->root.type == bfd_link_hash_defweak)
9023 && elf_discarded_section (h->root.u.def.section))
9024 return TRUE;
9025 else
9026 return FALSE;
9028 else
9030 /* It's not a relocation against a global symbol,
9031 but it could be a relocation against a local
9032 symbol for a discarded section. */
9033 asection *isec;
9034 Elf_Internal_Sym *isym;
9036 /* Need to: get the symbol; get the section. */
9037 isym = &rcookie->locsyms[r_symndx];
9038 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
9040 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
9041 if (isec != NULL && elf_discarded_section (isec))
9042 return TRUE;
9045 return FALSE;
9047 return FALSE;
9050 /* Discard unneeded references to discarded sections.
9051 Returns TRUE if any section's size was changed. */
9052 /* This function assumes that the relocations are in sorted order,
9053 which is true for all known assemblers. */
9055 bfd_boolean
9056 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
9058 struct elf_reloc_cookie cookie;
9059 asection *stab, *eh;
9060 Elf_Internal_Shdr *symtab_hdr;
9061 const struct elf_backend_data *bed;
9062 bfd *abfd;
9063 unsigned int count;
9064 bfd_boolean ret = FALSE;
9066 if (info->traditional_format
9067 || !is_elf_hash_table (info->hash))
9068 return FALSE;
9070 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
9072 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
9073 continue;
9075 bed = get_elf_backend_data (abfd);
9077 if ((abfd->flags & DYNAMIC) != 0)
9078 continue;
9080 eh = bfd_get_section_by_name (abfd, ".eh_frame");
9081 if (info->relocatable
9082 || (eh != NULL
9083 && (eh->size == 0
9084 || bfd_is_abs_section (eh->output_section))))
9085 eh = NULL;
9087 stab = bfd_get_section_by_name (abfd, ".stab");
9088 if (stab != NULL
9089 && (stab->size == 0
9090 || bfd_is_abs_section (stab->output_section)
9091 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
9092 stab = NULL;
9094 if (stab == NULL
9095 && eh == NULL
9096 && bed->elf_backend_discard_info == NULL)
9097 continue;
9099 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9100 cookie.abfd = abfd;
9101 cookie.sym_hashes = elf_sym_hashes (abfd);
9102 cookie.bad_symtab = elf_bad_symtab (abfd);
9103 if (cookie.bad_symtab)
9105 cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9106 cookie.extsymoff = 0;
9108 else
9110 cookie.locsymcount = symtab_hdr->sh_info;
9111 cookie.extsymoff = symtab_hdr->sh_info;
9114 if (bed->s->arch_size == 32)
9115 cookie.r_sym_shift = 8;
9116 else
9117 cookie.r_sym_shift = 32;
9119 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
9120 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
9122 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9123 cookie.locsymcount, 0,
9124 NULL, NULL, NULL);
9125 if (cookie.locsyms == NULL)
9126 return FALSE;
9129 if (stab != NULL)
9131 cookie.rels = NULL;
9132 count = stab->reloc_count;
9133 if (count != 0)
9134 cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
9135 info->keep_memory);
9136 if (cookie.rels != NULL)
9138 cookie.rel = cookie.rels;
9139 cookie.relend = cookie.rels;
9140 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9141 if (_bfd_discard_section_stabs (abfd, stab,
9142 elf_section_data (stab)->sec_info,
9143 bfd_elf_reloc_symbol_deleted_p,
9144 &cookie))
9145 ret = TRUE;
9146 if (elf_section_data (stab)->relocs != cookie.rels)
9147 free (cookie.rels);
9151 if (eh != NULL)
9153 cookie.rels = NULL;
9154 count = eh->reloc_count;
9155 if (count != 0)
9156 cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
9157 info->keep_memory);
9158 cookie.rel = cookie.rels;
9159 cookie.relend = cookie.rels;
9160 if (cookie.rels != NULL)
9161 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9163 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
9164 bfd_elf_reloc_symbol_deleted_p,
9165 &cookie))
9166 ret = TRUE;
9168 if (cookie.rels != NULL
9169 && elf_section_data (eh)->relocs != cookie.rels)
9170 free (cookie.rels);
9173 if (bed->elf_backend_discard_info != NULL
9174 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
9175 ret = TRUE;
9177 if (cookie.locsyms != NULL
9178 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
9180 if (! info->keep_memory)
9181 free (cookie.locsyms);
9182 else
9183 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
9187 if (info->eh_frame_hdr
9188 && !info->relocatable
9189 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
9190 ret = TRUE;
9192 return ret;
9195 struct already_linked_section
9197 asection *sec;
9198 asection *linked;
9201 /* Check if the member of a single member comdat group matches a
9202 linkonce section and vice versa. */
9203 static bfd_boolean
9204 try_match_symbols_in_sections
9205 (struct bfd_section_already_linked_hash_entry *h, void *info)
9207 struct bfd_section_already_linked *l;
9208 struct already_linked_section *s
9209 = (struct already_linked_section *) info;
9211 if (elf_sec_group (s->sec) == NULL)
9213 /* It is a linkonce section. Try to match it with the member of a
9214 single member comdat group. */
9215 for (l = h->entry; l != NULL; l = l->next)
9216 if ((l->sec->flags & SEC_GROUP))
9218 asection *first = elf_next_in_group (l->sec);
9220 if (first != NULL
9221 && elf_next_in_group (first) == first
9222 && bfd_elf_match_symbols_in_sections (first, s->sec))
9224 s->linked = first;
9225 return FALSE;
9229 else
9231 /* It is the member of a single member comdat group. Try to match
9232 it with a linkonce section. */
9233 for (l = h->entry; l != NULL; l = l->next)
9234 if ((l->sec->flags & SEC_GROUP) == 0
9235 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
9236 && bfd_elf_match_symbols_in_sections (l->sec, s->sec))
9238 s->linked = l->sec;
9239 return FALSE;
9243 return TRUE;
9246 static bfd_boolean
9247 already_linked (asection *sec, asection *group)
9249 struct already_linked_section result;
9251 result.sec = sec;
9252 result.linked = NULL;
9254 bfd_section_already_linked_table_traverse
9255 (try_match_symbols_in_sections, &result);
9257 if (result.linked)
9259 sec->output_section = bfd_abs_section_ptr;
9260 sec->kept_section = result.linked;
9262 /* Also discard the group section. */
9263 if (group)
9264 group->output_section = bfd_abs_section_ptr;
9266 return TRUE;
9269 return FALSE;
9272 void
9273 _bfd_elf_section_already_linked (bfd *abfd, struct bfd_section * sec)
9275 flagword flags;
9276 const char *name;
9277 struct bfd_section_already_linked *l;
9278 struct bfd_section_already_linked_hash_entry *already_linked_list;
9279 asection *group;
9281 /* A single member comdat group section may be discarded by a
9282 linkonce section. See below. */
9283 if (sec->output_section == bfd_abs_section_ptr)
9284 return;
9286 flags = sec->flags;
9288 /* Check if it belongs to a section group. */
9289 group = elf_sec_group (sec);
9291 /* Return if it isn't a linkonce section nor a member of a group. A
9292 comdat group section also has SEC_LINK_ONCE set. */
9293 if ((flags & SEC_LINK_ONCE) == 0 && group == NULL)
9294 return;
9296 if (group)
9298 /* If this is the member of a single member comdat group, check if
9299 the group should be discarded. */
9300 if (elf_next_in_group (sec) == sec
9301 && (group->flags & SEC_LINK_ONCE) != 0)
9302 sec = group;
9303 else
9304 return;
9307 /* FIXME: When doing a relocatable link, we may have trouble
9308 copying relocations in other sections that refer to local symbols
9309 in the section being discarded. Those relocations will have to
9310 be converted somehow; as of this writing I'm not sure that any of
9311 the backends handle that correctly.
9313 It is tempting to instead not discard link once sections when
9314 doing a relocatable link (technically, they should be discarded
9315 whenever we are building constructors). However, that fails,
9316 because the linker winds up combining all the link once sections
9317 into a single large link once section, which defeats the purpose
9318 of having link once sections in the first place.
9320 Also, not merging link once sections in a relocatable link
9321 causes trouble for MIPS ELF, which relies on link once semantics
9322 to handle the .reginfo section correctly. */
9324 name = bfd_get_section_name (abfd, sec);
9326 already_linked_list = bfd_section_already_linked_table_lookup (name);
9328 for (l = already_linked_list->entry; l != NULL; l = l->next)
9330 /* We may have 3 different sections on the list: group section,
9331 comdat section and linkonce section. SEC may be a linkonce or
9332 group section. We match a group section with a group section,
9333 a linkonce section with a linkonce section, and ignore comdat
9334 section. */
9335 if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
9336 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
9338 /* The section has already been linked. See if we should
9339 issue a warning. */
9340 switch (flags & SEC_LINK_DUPLICATES)
9342 default:
9343 abort ();
9345 case SEC_LINK_DUPLICATES_DISCARD:
9346 break;
9348 case SEC_LINK_DUPLICATES_ONE_ONLY:
9349 (*_bfd_error_handler)
9350 (_("%B: ignoring duplicate section `%A'\n"),
9351 abfd, sec);
9352 break;
9354 case SEC_LINK_DUPLICATES_SAME_SIZE:
9355 if (sec->size != l->sec->size)
9356 (*_bfd_error_handler)
9357 (_("%B: duplicate section `%A' has different size\n"),
9358 abfd, sec);
9359 break;
9361 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
9362 if (sec->size != l->sec->size)
9363 (*_bfd_error_handler)
9364 (_("%B: duplicate section `%A' has different size\n"),
9365 abfd, sec);
9366 else if (sec->size != 0)
9368 bfd_byte *sec_contents, *l_sec_contents;
9370 if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
9371 (*_bfd_error_handler)
9372 (_("%B: warning: could not read contents of section `%A'\n"),
9373 abfd, sec);
9374 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
9375 &l_sec_contents))
9376 (*_bfd_error_handler)
9377 (_("%B: warning: could not read contents of section `%A'\n"),
9378 l->sec->owner, l->sec);
9379 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
9380 (*_bfd_error_handler)
9381 (_("%B: warning: duplicate section `%A' has different contents\n"),
9382 abfd, sec);
9384 if (sec_contents)
9385 free (sec_contents);
9386 if (l_sec_contents)
9387 free (l_sec_contents);
9389 break;
9392 /* Set the output_section field so that lang_add_section
9393 does not create a lang_input_section structure for this
9394 section. Since there might be a symbol in the section
9395 being discarded, we must retain a pointer to the section
9396 which we are really going to use. */
9397 sec->output_section = bfd_abs_section_ptr;
9398 sec->kept_section = l->sec;
9400 if (flags & SEC_GROUP)
9402 asection *first = elf_next_in_group (sec);
9403 asection *s = first;
9405 while (s != NULL)
9407 s->output_section = bfd_abs_section_ptr;
9408 /* Record which group discards it. */
9409 s->kept_section = l->sec;
9410 s = elf_next_in_group (s);
9411 /* These lists are circular. */
9412 if (s == first)
9413 break;
9417 return;
9421 if (group)
9423 /* If this is the member of a single member comdat group and the
9424 group hasn't be discarded, we check if it matches a linkonce
9425 section. We only record the discarded comdat group. Otherwise
9426 the undiscarded group will be discarded incorrectly later since
9427 itself has been recorded. */
9428 if (! already_linked (elf_next_in_group (sec), group))
9429 return;
9431 else
9432 /* There is no direct match. But for linkonce section, we should
9433 check if there is a match with comdat group member. We always
9434 record the linkonce section, discarded or not. */
9435 already_linked (sec, group);
9437 /* This is the first section with this name. Record it. */
9438 bfd_section_already_linked_table_insert (already_linked_list, sec);