* config.bfd (arm*-*-symbianelf*): Use OS-specific target vectors.
[binutils.git] / bfd / elflink.c
blob6ccc08c751bf89d321f3234b9505af3404ccf7be
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->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
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->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
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->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
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->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
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->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
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->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
450 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
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->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
459 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
460 h->verinfo.verdef = NULL;
462 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
464 if (((h->elf_link_hash_flags & (ELF_LINK_HASH_DEF_DYNAMIC
465 | ELF_LINK_HASH_REF_DYNAMIC)) != 0
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->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
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->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0))
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->elf_link_hash_flags & ELF_LINK_DYNAMIC_DEF) == 0)
848 if (!bfd_is_und_section (sec))
849 h->elf_link_hash_flags |= ELF_LINK_DYNAMIC_DEF;
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->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0)
857 if (bind == STB_WEAK)
858 h->elf_link_hash_flags |= ELF_LINK_DYNAMIC_WEAK;
860 else if (bind != STB_WEAK)
861 h->elf_link_hash_flags &= ~ELF_LINK_DYNAMIC_WEAK;
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->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
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->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0)
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->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
914 h->elf_link_hash_flags &= ~ELF_LINK_HASH_DEF_DYNAMIC;
915 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_DYNAMIC
916 | ELF_LINK_DYNAMIC_DEF);
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->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
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->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0)
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->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
1190 h->elf_link_hash_flags &= ~ELF_LINK_HASH_DEF_DYNAMIC;
1191 flip->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
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->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
1316 h->elf_link_hash_flags &=~ ELF_LINK_HASH_DEF_DYNAMIC;
1317 hi->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
1318 if (hi->elf_link_hash_flags
1319 & (ELF_LINK_HASH_REF_REGULAR
1320 | ELF_LINK_HASH_DEF_REGULAR))
1322 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1323 return FALSE;
1327 /* Now set HI to H, so that the following code will set the
1328 other fields correctly. */
1329 hi = h;
1332 /* If there is a duplicate definition somewhere, then HI may not
1333 point to an indirect symbol. We will have reported an error to
1334 the user in that case. */
1336 if (hi->root.type == bfd_link_hash_indirect)
1338 struct elf_link_hash_entry *ht;
1340 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1341 (*bed->elf_backend_copy_indirect_symbol) (bed, ht, hi);
1343 /* See if the new flags lead us to realize that the symbol must
1344 be dynamic. */
1345 if (! *dynsym)
1347 if (! dynamic)
1349 if (info->shared
1350 || ((hi->elf_link_hash_flags
1351 & ELF_LINK_HASH_REF_DYNAMIC) != 0))
1352 *dynsym = TRUE;
1354 else
1356 if ((hi->elf_link_hash_flags
1357 & ELF_LINK_HASH_REF_REGULAR) != 0)
1358 *dynsym = TRUE;
1363 /* We also need to define an indirection from the nondefault version
1364 of the symbol. */
1366 nondefault:
1367 len = strlen (name);
1368 shortname = bfd_hash_allocate (&info->hash->table, len);
1369 if (shortname == NULL)
1370 return FALSE;
1371 memcpy (shortname, name, shortlen);
1372 memcpy (shortname + shortlen, p + 1, len - shortlen);
1374 /* Once again, merge with any existing symbol. */
1375 type_change_ok = FALSE;
1376 size_change_ok = FALSE;
1377 sec = *psec;
1378 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1379 &hi, &skip, &override, &type_change_ok,
1380 &size_change_ok))
1381 return FALSE;
1383 if (skip)
1384 return TRUE;
1386 if (override)
1388 /* Here SHORTNAME is a versioned name, so we don't expect to see
1389 the type of override we do in the case above unless it is
1390 overridden by a versioned definition. */
1391 if (hi->root.type != bfd_link_hash_defined
1392 && hi->root.type != bfd_link_hash_defweak)
1393 (*_bfd_error_handler)
1394 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1395 abfd, shortname);
1397 else
1399 bh = &hi->root;
1400 if (! (_bfd_generic_link_add_one_symbol
1401 (info, abfd, shortname, BSF_INDIRECT,
1402 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1403 return FALSE;
1404 hi = (struct elf_link_hash_entry *) bh;
1406 /* If there is a duplicate definition somewhere, then HI may not
1407 point to an indirect symbol. We will have reported an error
1408 to the user in that case. */
1410 if (hi->root.type == bfd_link_hash_indirect)
1412 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
1414 /* See if the new flags lead us to realize that the symbol
1415 must be dynamic. */
1416 if (! *dynsym)
1418 if (! dynamic)
1420 if (info->shared
1421 || ((hi->elf_link_hash_flags
1422 & ELF_LINK_HASH_REF_DYNAMIC) != 0))
1423 *dynsym = TRUE;
1425 else
1427 if ((hi->elf_link_hash_flags
1428 & ELF_LINK_HASH_REF_REGULAR) != 0)
1429 *dynsym = TRUE;
1435 return TRUE;
1438 /* This routine is used to export all defined symbols into the dynamic
1439 symbol table. It is called via elf_link_hash_traverse. */
1441 bfd_boolean
1442 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1444 struct elf_info_failed *eif = data;
1446 /* Ignore indirect symbols. These are added by the versioning code. */
1447 if (h->root.type == bfd_link_hash_indirect)
1448 return TRUE;
1450 if (h->root.type == bfd_link_hash_warning)
1451 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1453 if (h->dynindx == -1
1454 && (h->elf_link_hash_flags
1455 & (ELF_LINK_HASH_DEF_REGULAR | ELF_LINK_HASH_REF_REGULAR)) != 0)
1457 struct bfd_elf_version_tree *t;
1458 struct bfd_elf_version_expr *d;
1460 for (t = eif->verdefs; t != NULL; t = t->next)
1462 if (t->globals.list != NULL)
1464 d = (*t->match) (&t->globals, NULL, h->root.root.string);
1465 if (d != NULL)
1466 goto doit;
1469 if (t->locals.list != NULL)
1471 d = (*t->match) (&t->locals, NULL, h->root.root.string);
1472 if (d != NULL)
1473 return TRUE;
1477 if (!eif->verdefs)
1479 doit:
1480 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1482 eif->failed = TRUE;
1483 return FALSE;
1488 return TRUE;
1491 /* Look through the symbols which are defined in other shared
1492 libraries and referenced here. Update the list of version
1493 dependencies. This will be put into the .gnu.version_r section.
1494 This function is called via elf_link_hash_traverse. */
1496 bfd_boolean
1497 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1498 void *data)
1500 struct elf_find_verdep_info *rinfo = data;
1501 Elf_Internal_Verneed *t;
1502 Elf_Internal_Vernaux *a;
1503 bfd_size_type amt;
1505 if (h->root.type == bfd_link_hash_warning)
1506 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1508 /* We only care about symbols defined in shared objects with version
1509 information. */
1510 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
1511 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
1512 || h->dynindx == -1
1513 || h->verinfo.verdef == NULL)
1514 return TRUE;
1516 /* See if we already know about this version. */
1517 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1519 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1520 continue;
1522 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1523 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1524 return TRUE;
1526 break;
1529 /* This is a new version. Add it to tree we are building. */
1531 if (t == NULL)
1533 amt = sizeof *t;
1534 t = bfd_zalloc (rinfo->output_bfd, amt);
1535 if (t == NULL)
1537 rinfo->failed = TRUE;
1538 return FALSE;
1541 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1542 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1543 elf_tdata (rinfo->output_bfd)->verref = t;
1546 amt = sizeof *a;
1547 a = bfd_zalloc (rinfo->output_bfd, amt);
1549 /* Note that we are copying a string pointer here, and testing it
1550 above. If bfd_elf_string_from_elf_section is ever changed to
1551 discard the string data when low in memory, this will have to be
1552 fixed. */
1553 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1555 a->vna_flags = h->verinfo.verdef->vd_flags;
1556 a->vna_nextptr = t->vn_auxptr;
1558 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1559 ++rinfo->vers;
1561 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1563 t->vn_auxptr = a;
1565 return TRUE;
1568 /* Figure out appropriate versions for all the symbols. We may not
1569 have the version number script until we have read all of the input
1570 files, so until that point we don't know which symbols should be
1571 local. This function is called via elf_link_hash_traverse. */
1573 bfd_boolean
1574 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1576 struct elf_assign_sym_version_info *sinfo;
1577 struct bfd_link_info *info;
1578 const struct elf_backend_data *bed;
1579 struct elf_info_failed eif;
1580 char *p;
1581 bfd_size_type amt;
1583 sinfo = data;
1584 info = sinfo->info;
1586 if (h->root.type == bfd_link_hash_warning)
1587 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1589 /* Fix the symbol flags. */
1590 eif.failed = FALSE;
1591 eif.info = info;
1592 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1594 if (eif.failed)
1595 sinfo->failed = TRUE;
1596 return FALSE;
1599 /* We only need version numbers for symbols defined in regular
1600 objects. */
1601 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1602 return TRUE;
1604 bed = get_elf_backend_data (sinfo->output_bfd);
1605 p = strchr (h->root.root.string, ELF_VER_CHR);
1606 if (p != NULL && h->verinfo.vertree == NULL)
1608 struct bfd_elf_version_tree *t;
1609 bfd_boolean hidden;
1611 hidden = TRUE;
1613 /* There are two consecutive ELF_VER_CHR characters if this is
1614 not a hidden symbol. */
1615 ++p;
1616 if (*p == ELF_VER_CHR)
1618 hidden = FALSE;
1619 ++p;
1622 /* If there is no version string, we can just return out. */
1623 if (*p == '\0')
1625 if (hidden)
1626 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
1627 return TRUE;
1630 /* Look for the version. If we find it, it is no longer weak. */
1631 for (t = sinfo->verdefs; t != NULL; t = t->next)
1633 if (strcmp (t->name, p) == 0)
1635 size_t len;
1636 char *alc;
1637 struct bfd_elf_version_expr *d;
1639 len = p - h->root.root.string;
1640 alc = bfd_malloc (len);
1641 if (alc == NULL)
1642 return FALSE;
1643 memcpy (alc, h->root.root.string, len - 1);
1644 alc[len - 1] = '\0';
1645 if (alc[len - 2] == ELF_VER_CHR)
1646 alc[len - 2] = '\0';
1648 h->verinfo.vertree = t;
1649 t->used = TRUE;
1650 d = NULL;
1652 if (t->globals.list != NULL)
1653 d = (*t->match) (&t->globals, NULL, alc);
1655 /* See if there is anything to force this symbol to
1656 local scope. */
1657 if (d == NULL && t->locals.list != NULL)
1659 d = (*t->match) (&t->locals, NULL, alc);
1660 if (d != NULL
1661 && h->dynindx != -1
1662 && info->shared
1663 && ! info->export_dynamic)
1664 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1667 free (alc);
1668 break;
1672 /* If we are building an application, we need to create a
1673 version node for this version. */
1674 if (t == NULL && info->executable)
1676 struct bfd_elf_version_tree **pp;
1677 int version_index;
1679 /* If we aren't going to export this symbol, we don't need
1680 to worry about it. */
1681 if (h->dynindx == -1)
1682 return TRUE;
1684 amt = sizeof *t;
1685 t = bfd_zalloc (sinfo->output_bfd, amt);
1686 if (t == NULL)
1688 sinfo->failed = TRUE;
1689 return FALSE;
1692 t->name = p;
1693 t->name_indx = (unsigned int) -1;
1694 t->used = TRUE;
1696 version_index = 1;
1697 /* Don't count anonymous version tag. */
1698 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1699 version_index = 0;
1700 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1701 ++version_index;
1702 t->vernum = version_index;
1704 *pp = t;
1706 h->verinfo.vertree = t;
1708 else if (t == NULL)
1710 /* We could not find the version for a symbol when
1711 generating a shared archive. Return an error. */
1712 (*_bfd_error_handler)
1713 (_("%B: undefined versioned symbol name %s"),
1714 sinfo->output_bfd, h->root.root.string);
1715 bfd_set_error (bfd_error_bad_value);
1716 sinfo->failed = TRUE;
1717 return FALSE;
1720 if (hidden)
1721 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
1724 /* If we don't have a version for this symbol, see if we can find
1725 something. */
1726 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
1728 struct bfd_elf_version_tree *t;
1729 struct bfd_elf_version_tree *local_ver;
1730 struct bfd_elf_version_expr *d;
1732 /* See if can find what version this symbol is in. If the
1733 symbol is supposed to be local, then don't actually register
1734 it. */
1735 local_ver = NULL;
1736 for (t = sinfo->verdefs; t != NULL; t = t->next)
1738 if (t->globals.list != NULL)
1740 bfd_boolean matched;
1742 matched = FALSE;
1743 d = NULL;
1744 while ((d = (*t->match) (&t->globals, d,
1745 h->root.root.string)) != NULL)
1746 if (d->symver)
1747 matched = TRUE;
1748 else
1750 /* There is a version without definition. Make
1751 the symbol the default definition for this
1752 version. */
1753 h->verinfo.vertree = t;
1754 local_ver = NULL;
1755 d->script = 1;
1756 break;
1758 if (d != NULL)
1759 break;
1760 else if (matched)
1761 /* There is no undefined version for this symbol. Hide the
1762 default one. */
1763 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1766 if (t->locals.list != NULL)
1768 d = NULL;
1769 while ((d = (*t->match) (&t->locals, d,
1770 h->root.root.string)) != NULL)
1772 local_ver = t;
1773 /* If the match is "*", keep looking for a more
1774 explicit, perhaps even global, match.
1775 XXX: Shouldn't this be !d->wildcard instead? */
1776 if (d->pattern[0] != '*' || d->pattern[1] != '\0')
1777 break;
1780 if (d != NULL)
1781 break;
1785 if (local_ver != NULL)
1787 h->verinfo.vertree = local_ver;
1788 if (h->dynindx != -1
1789 && info->shared
1790 && ! info->export_dynamic)
1792 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1797 return TRUE;
1800 /* Read and swap the relocs from the section indicated by SHDR. This
1801 may be either a REL or a RELA section. The relocations are
1802 translated into RELA relocations and stored in INTERNAL_RELOCS,
1803 which should have already been allocated to contain enough space.
1804 The EXTERNAL_RELOCS are a buffer where the external form of the
1805 relocations should be stored.
1807 Returns FALSE if something goes wrong. */
1809 static bfd_boolean
1810 elf_link_read_relocs_from_section (bfd *abfd,
1811 asection *sec,
1812 Elf_Internal_Shdr *shdr,
1813 void *external_relocs,
1814 Elf_Internal_Rela *internal_relocs)
1816 const struct elf_backend_data *bed;
1817 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
1818 const bfd_byte *erela;
1819 const bfd_byte *erelaend;
1820 Elf_Internal_Rela *irela;
1821 Elf_Internal_Shdr *symtab_hdr;
1822 size_t nsyms;
1824 /* Position ourselves at the start of the section. */
1825 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
1826 return FALSE;
1828 /* Read the relocations. */
1829 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
1830 return FALSE;
1832 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1833 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
1835 bed = get_elf_backend_data (abfd);
1837 /* Convert the external relocations to the internal format. */
1838 if (shdr->sh_entsize == bed->s->sizeof_rel)
1839 swap_in = bed->s->swap_reloc_in;
1840 else if (shdr->sh_entsize == bed->s->sizeof_rela)
1841 swap_in = bed->s->swap_reloca_in;
1842 else
1844 bfd_set_error (bfd_error_wrong_format);
1845 return FALSE;
1848 erela = external_relocs;
1849 erelaend = erela + shdr->sh_size;
1850 irela = internal_relocs;
1851 while (erela < erelaend)
1853 bfd_vma r_symndx;
1855 (*swap_in) (abfd, erela, irela);
1856 r_symndx = ELF32_R_SYM (irela->r_info);
1857 if (bed->s->arch_size == 64)
1858 r_symndx >>= 24;
1859 if ((size_t) r_symndx >= nsyms)
1861 (*_bfd_error_handler)
1862 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
1863 " for offset 0x%lx in section `%A'"),
1864 abfd, sec,
1865 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
1866 bfd_set_error (bfd_error_bad_value);
1867 return FALSE;
1869 irela += bed->s->int_rels_per_ext_rel;
1870 erela += shdr->sh_entsize;
1873 return TRUE;
1876 /* Read and swap the relocs for a section O. They may have been
1877 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1878 not NULL, they are used as buffers to read into. They are known to
1879 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
1880 the return value is allocated using either malloc or bfd_alloc,
1881 according to the KEEP_MEMORY argument. If O has two relocation
1882 sections (both REL and RELA relocations), then the REL_HDR
1883 relocations will appear first in INTERNAL_RELOCS, followed by the
1884 REL_HDR2 relocations. */
1886 Elf_Internal_Rela *
1887 _bfd_elf_link_read_relocs (bfd *abfd,
1888 asection *o,
1889 void *external_relocs,
1890 Elf_Internal_Rela *internal_relocs,
1891 bfd_boolean keep_memory)
1893 Elf_Internal_Shdr *rel_hdr;
1894 void *alloc1 = NULL;
1895 Elf_Internal_Rela *alloc2 = NULL;
1896 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1898 if (elf_section_data (o)->relocs != NULL)
1899 return elf_section_data (o)->relocs;
1901 if (o->reloc_count == 0)
1902 return NULL;
1904 rel_hdr = &elf_section_data (o)->rel_hdr;
1906 if (internal_relocs == NULL)
1908 bfd_size_type size;
1910 size = o->reloc_count;
1911 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
1912 if (keep_memory)
1913 internal_relocs = bfd_alloc (abfd, size);
1914 else
1915 internal_relocs = alloc2 = bfd_malloc (size);
1916 if (internal_relocs == NULL)
1917 goto error_return;
1920 if (external_relocs == NULL)
1922 bfd_size_type size = rel_hdr->sh_size;
1924 if (elf_section_data (o)->rel_hdr2)
1925 size += elf_section_data (o)->rel_hdr2->sh_size;
1926 alloc1 = bfd_malloc (size);
1927 if (alloc1 == NULL)
1928 goto error_return;
1929 external_relocs = alloc1;
1932 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
1933 external_relocs,
1934 internal_relocs))
1935 goto error_return;
1936 if (elf_section_data (o)->rel_hdr2
1937 && (!elf_link_read_relocs_from_section
1938 (abfd, o,
1939 elf_section_data (o)->rel_hdr2,
1940 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
1941 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
1942 * bed->s->int_rels_per_ext_rel))))
1943 goto error_return;
1945 /* Cache the results for next time, if we can. */
1946 if (keep_memory)
1947 elf_section_data (o)->relocs = internal_relocs;
1949 if (alloc1 != NULL)
1950 free (alloc1);
1952 /* Don't free alloc2, since if it was allocated we are passing it
1953 back (under the name of internal_relocs). */
1955 return internal_relocs;
1957 error_return:
1958 if (alloc1 != NULL)
1959 free (alloc1);
1960 if (alloc2 != NULL)
1961 free (alloc2);
1962 return NULL;
1965 /* Compute the size of, and allocate space for, REL_HDR which is the
1966 section header for a section containing relocations for O. */
1968 bfd_boolean
1969 _bfd_elf_link_size_reloc_section (bfd *abfd,
1970 Elf_Internal_Shdr *rel_hdr,
1971 asection *o)
1973 bfd_size_type reloc_count;
1974 bfd_size_type num_rel_hashes;
1976 /* Figure out how many relocations there will be. */
1977 if (rel_hdr == &elf_section_data (o)->rel_hdr)
1978 reloc_count = elf_section_data (o)->rel_count;
1979 else
1980 reloc_count = elf_section_data (o)->rel_count2;
1982 num_rel_hashes = o->reloc_count;
1983 if (num_rel_hashes < reloc_count)
1984 num_rel_hashes = reloc_count;
1986 /* That allows us to calculate the size of the section. */
1987 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
1989 /* The contents field must last into write_object_contents, so we
1990 allocate it with bfd_alloc rather than malloc. Also since we
1991 cannot be sure that the contents will actually be filled in,
1992 we zero the allocated space. */
1993 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
1994 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
1995 return FALSE;
1997 /* We only allocate one set of hash entries, so we only do it the
1998 first time we are called. */
1999 if (elf_section_data (o)->rel_hashes == NULL
2000 && num_rel_hashes)
2002 struct elf_link_hash_entry **p;
2004 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
2005 if (p == NULL)
2006 return FALSE;
2008 elf_section_data (o)->rel_hashes = p;
2011 return TRUE;
2014 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2015 originated from the section given by INPUT_REL_HDR) to the
2016 OUTPUT_BFD. */
2018 bfd_boolean
2019 _bfd_elf_link_output_relocs (bfd *output_bfd,
2020 asection *input_section,
2021 Elf_Internal_Shdr *input_rel_hdr,
2022 Elf_Internal_Rela *internal_relocs)
2024 Elf_Internal_Rela *irela;
2025 Elf_Internal_Rela *irelaend;
2026 bfd_byte *erel;
2027 Elf_Internal_Shdr *output_rel_hdr;
2028 asection *output_section;
2029 unsigned int *rel_countp = NULL;
2030 const struct elf_backend_data *bed;
2031 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2033 output_section = input_section->output_section;
2034 output_rel_hdr = NULL;
2036 if (elf_section_data (output_section)->rel_hdr.sh_entsize
2037 == input_rel_hdr->sh_entsize)
2039 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2040 rel_countp = &elf_section_data (output_section)->rel_count;
2042 else if (elf_section_data (output_section)->rel_hdr2
2043 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2044 == input_rel_hdr->sh_entsize))
2046 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2047 rel_countp = &elf_section_data (output_section)->rel_count2;
2049 else
2051 (*_bfd_error_handler)
2052 (_("%B: relocation size mismatch in %B section %A"),
2053 output_bfd, input_section->owner, input_section);
2054 bfd_set_error (bfd_error_wrong_object_format);
2055 return FALSE;
2058 bed = get_elf_backend_data (output_bfd);
2059 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2060 swap_out = bed->s->swap_reloc_out;
2061 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2062 swap_out = bed->s->swap_reloca_out;
2063 else
2064 abort ();
2066 erel = output_rel_hdr->contents;
2067 erel += *rel_countp * input_rel_hdr->sh_entsize;
2068 irela = internal_relocs;
2069 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2070 * bed->s->int_rels_per_ext_rel);
2071 while (irela < irelaend)
2073 (*swap_out) (output_bfd, irela, erel);
2074 irela += bed->s->int_rels_per_ext_rel;
2075 erel += input_rel_hdr->sh_entsize;
2078 /* Bump the counter, so that we know where to add the next set of
2079 relocations. */
2080 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2082 return TRUE;
2085 /* Fix up the flags for a symbol. This handles various cases which
2086 can only be fixed after all the input files are seen. This is
2087 currently called by both adjust_dynamic_symbol and
2088 assign_sym_version, which is unnecessary but perhaps more robust in
2089 the face of future changes. */
2091 bfd_boolean
2092 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2093 struct elf_info_failed *eif)
2095 /* If this symbol was mentioned in a non-ELF file, try to set
2096 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2097 permit a non-ELF file to correctly refer to a symbol defined in
2098 an ELF dynamic object. */
2099 if ((h->elf_link_hash_flags & ELF_LINK_NON_ELF) != 0)
2101 while (h->root.type == bfd_link_hash_indirect)
2102 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2104 if (h->root.type != bfd_link_hash_defined
2105 && h->root.type != bfd_link_hash_defweak)
2106 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
2107 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
2108 else
2110 if (h->root.u.def.section->owner != NULL
2111 && (bfd_get_flavour (h->root.u.def.section->owner)
2112 == bfd_target_elf_flavour))
2113 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
2114 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
2115 else
2116 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2119 if (h->dynindx == -1
2120 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2121 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0))
2123 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2125 eif->failed = TRUE;
2126 return FALSE;
2130 else
2132 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
2133 was first seen in a non-ELF file. Fortunately, if the symbol
2134 was first seen in an ELF file, we're probably OK unless the
2135 symbol was defined in a non-ELF file. Catch that case here.
2136 FIXME: We're still in trouble if the symbol was first seen in
2137 a dynamic object, and then later in a non-ELF regular object. */
2138 if ((h->root.type == bfd_link_hash_defined
2139 || h->root.type == bfd_link_hash_defweak)
2140 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
2141 && (h->root.u.def.section->owner != NULL
2142 ? (bfd_get_flavour (h->root.u.def.section->owner)
2143 != bfd_target_elf_flavour)
2144 : (bfd_is_abs_section (h->root.u.def.section)
2145 && (h->elf_link_hash_flags
2146 & ELF_LINK_HASH_DEF_DYNAMIC) == 0)))
2147 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2150 /* If this is a final link, and the symbol was defined as a common
2151 symbol in a regular object file, and there was no definition in
2152 any dynamic object, then the linker will have allocated space for
2153 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
2154 flag will not have been set. */
2155 if (h->root.type == bfd_link_hash_defined
2156 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
2157 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
2158 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
2159 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2160 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2162 /* If -Bsymbolic was used (which means to bind references to global
2163 symbols to the definition within the shared object), and this
2164 symbol was defined in a regular object, then it actually doesn't
2165 need a PLT entry. Likewise, if the symbol has non-default
2166 visibility. If the symbol has hidden or internal visibility, we
2167 will force it local. */
2168 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
2169 && eif->info->shared
2170 && is_elf_hash_table (eif->info->hash)
2171 && (eif->info->symbolic
2172 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2173 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
2175 const struct elf_backend_data *bed;
2176 bfd_boolean force_local;
2178 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2180 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2181 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2182 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2185 /* If a weak undefined symbol has non-default visibility, we also
2186 hide it from the dynamic linker. */
2187 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2188 && h->root.type == bfd_link_hash_undefweak)
2190 const struct elf_backend_data *bed;
2191 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2192 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2195 /* If this is a weak defined symbol in a dynamic object, and we know
2196 the real definition in the dynamic object, copy interesting flags
2197 over to the real definition. */
2198 if (h->weakdef != NULL)
2200 struct elf_link_hash_entry *weakdef;
2202 weakdef = h->weakdef;
2203 if (h->root.type == bfd_link_hash_indirect)
2204 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2206 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2207 || h->root.type == bfd_link_hash_defweak);
2208 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2209 || weakdef->root.type == bfd_link_hash_defweak);
2210 BFD_ASSERT (weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC);
2212 /* If the real definition is defined by a regular object file,
2213 don't do anything special. See the longer description in
2214 _bfd_elf_adjust_dynamic_symbol, below. */
2215 if ((weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
2216 h->weakdef = NULL;
2217 else
2219 const struct elf_backend_data *bed;
2221 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2222 (*bed->elf_backend_copy_indirect_symbol) (bed, weakdef, h);
2226 return TRUE;
2229 /* Make the backend pick a good value for a dynamic symbol. This is
2230 called via elf_link_hash_traverse, and also calls itself
2231 recursively. */
2233 bfd_boolean
2234 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2236 struct elf_info_failed *eif = data;
2237 bfd *dynobj;
2238 const struct elf_backend_data *bed;
2240 if (! is_elf_hash_table (eif->info->hash))
2241 return FALSE;
2243 if (h->root.type == bfd_link_hash_warning)
2245 h->plt = elf_hash_table (eif->info)->init_offset;
2246 h->got = elf_hash_table (eif->info)->init_offset;
2248 /* When warning symbols are created, they **replace** the "real"
2249 entry in the hash table, thus we never get to see the real
2250 symbol in a hash traversal. So look at it now. */
2251 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2254 /* Ignore indirect symbols. These are added by the versioning code. */
2255 if (h->root.type == bfd_link_hash_indirect)
2256 return TRUE;
2258 /* Fix the symbol flags. */
2259 if (! _bfd_elf_fix_symbol_flags (h, eif))
2260 return FALSE;
2262 /* If this symbol does not require a PLT entry, and it is not
2263 defined by a dynamic object, or is not referenced by a regular
2264 object, ignore it. We do have to handle a weak defined symbol,
2265 even if no regular object refers to it, if we decided to add it
2266 to the dynamic symbol table. FIXME: Do we normally need to worry
2267 about symbols which are defined by one dynamic object and
2268 referenced by another one? */
2269 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0
2270 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
2271 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
2272 || ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
2273 && (h->weakdef == NULL || h->weakdef->dynindx == -1))))
2275 h->plt = elf_hash_table (eif->info)->init_offset;
2276 return TRUE;
2279 /* If we've already adjusted this symbol, don't do it again. This
2280 can happen via a recursive call. */
2281 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
2282 return TRUE;
2284 /* Don't look at this symbol again. Note that we must set this
2285 after checking the above conditions, because we may look at a
2286 symbol once, decide not to do anything, and then get called
2287 recursively later after REF_REGULAR is set below. */
2288 h->elf_link_hash_flags |= ELF_LINK_HASH_DYNAMIC_ADJUSTED;
2290 /* If this is a weak definition, and we know a real definition, and
2291 the real symbol is not itself defined by a regular object file,
2292 then get a good value for the real definition. We handle the
2293 real symbol first, for the convenience of the backend routine.
2295 Note that there is a confusing case here. If the real definition
2296 is defined by a regular object file, we don't get the real symbol
2297 from the dynamic object, but we do get the weak symbol. If the
2298 processor backend uses a COPY reloc, then if some routine in the
2299 dynamic object changes the real symbol, we will not see that
2300 change in the corresponding weak symbol. This is the way other
2301 ELF linkers work as well, and seems to be a result of the shared
2302 library model.
2304 I will clarify this issue. Most SVR4 shared libraries define the
2305 variable _timezone and define timezone as a weak synonym. The
2306 tzset call changes _timezone. If you write
2307 extern int timezone;
2308 int _timezone = 5;
2309 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2310 you might expect that, since timezone is a synonym for _timezone,
2311 the same number will print both times. However, if the processor
2312 backend uses a COPY reloc, then actually timezone will be copied
2313 into your process image, and, since you define _timezone
2314 yourself, _timezone will not. Thus timezone and _timezone will
2315 wind up at different memory locations. The tzset call will set
2316 _timezone, leaving timezone unchanged. */
2318 if (h->weakdef != NULL)
2320 /* If we get to this point, we know there is an implicit
2321 reference by a regular object file via the weak symbol H.
2322 FIXME: Is this really true? What if the traversal finds
2323 H->WEAKDEF before it finds H? */
2324 h->weakdef->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
2326 if (! _bfd_elf_adjust_dynamic_symbol (h->weakdef, eif))
2327 return FALSE;
2330 /* If a symbol has no type and no size and does not require a PLT
2331 entry, then we are probably about to do the wrong thing here: we
2332 are probably going to create a COPY reloc for an empty object.
2333 This case can arise when a shared object is built with assembly
2334 code, and the assembly code fails to set the symbol type. */
2335 if (h->size == 0
2336 && h->type == STT_NOTYPE
2337 && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0)
2338 (*_bfd_error_handler)
2339 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2340 h->root.root.string);
2342 dynobj = elf_hash_table (eif->info)->dynobj;
2343 bed = get_elf_backend_data (dynobj);
2344 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2346 eif->failed = TRUE;
2347 return FALSE;
2350 return TRUE;
2353 /* Adjust all external symbols pointing into SEC_MERGE sections
2354 to reflect the object merging within the sections. */
2356 bfd_boolean
2357 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2359 asection *sec;
2361 if (h->root.type == bfd_link_hash_warning)
2362 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2364 if ((h->root.type == bfd_link_hash_defined
2365 || h->root.type == bfd_link_hash_defweak)
2366 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2367 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2369 bfd *output_bfd = data;
2371 h->root.u.def.value =
2372 _bfd_merged_section_offset (output_bfd,
2373 &h->root.u.def.section,
2374 elf_section_data (sec)->sec_info,
2375 h->root.u.def.value);
2378 return TRUE;
2381 /* Returns false if the symbol referred to by H should be considered
2382 to resolve local to the current module, and true if it should be
2383 considered to bind dynamically. */
2385 bfd_boolean
2386 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2387 struct bfd_link_info *info,
2388 bfd_boolean ignore_protected)
2390 bfd_boolean binding_stays_local_p;
2392 if (h == NULL)
2393 return FALSE;
2395 while (h->root.type == bfd_link_hash_indirect
2396 || h->root.type == bfd_link_hash_warning)
2397 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2399 /* If it was forced local, then clearly it's not dynamic. */
2400 if (h->dynindx == -1)
2401 return FALSE;
2402 if (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
2403 return FALSE;
2405 /* Identify the cases where name binding rules say that a
2406 visible symbol resolves locally. */
2407 binding_stays_local_p = info->executable || info->symbolic;
2409 switch (ELF_ST_VISIBILITY (h->other))
2411 case STV_INTERNAL:
2412 case STV_HIDDEN:
2413 return FALSE;
2415 case STV_PROTECTED:
2416 /* Proper resolution for function pointer equality may require
2417 that these symbols perhaps be resolved dynamically, even though
2418 we should be resolving them to the current module. */
2419 if (!ignore_protected)
2420 binding_stays_local_p = TRUE;
2421 break;
2423 default:
2424 break;
2427 /* If it isn't defined locally, then clearly it's dynamic. */
2428 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2429 return TRUE;
2431 /* Otherwise, the symbol is dynamic if binding rules don't tell
2432 us that it remains local. */
2433 return !binding_stays_local_p;
2436 /* Return true if the symbol referred to by H should be considered
2437 to resolve local to the current module, and false otherwise. Differs
2438 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2439 undefined symbols and weak symbols. */
2441 bfd_boolean
2442 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2443 struct bfd_link_info *info,
2444 bfd_boolean local_protected)
2446 /* If it's a local sym, of course we resolve locally. */
2447 if (h == NULL)
2448 return TRUE;
2450 /* Common symbols that become definitions don't get the DEF_REGULAR
2451 flag set, so test it first, and don't bail out. */
2452 if (ELF_COMMON_DEF_P (h))
2453 /* Do nothing. */;
2454 /* If we don't have a definition in a regular file, then we can't
2455 resolve locally. The sym is either undefined or dynamic. */
2456 else if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2457 return FALSE;
2459 /* Forced local symbols resolve locally. */
2460 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
2461 return TRUE;
2463 /* As do non-dynamic symbols. */
2464 if (h->dynindx == -1)
2465 return TRUE;
2467 /* At this point, we know the symbol is defined and dynamic. In an
2468 executable it must resolve locally, likewise when building symbolic
2469 shared libraries. */
2470 if (info->executable || info->symbolic)
2471 return TRUE;
2473 /* Now deal with defined dynamic symbols in shared libraries. Ones
2474 with default visibility might not resolve locally. */
2475 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2476 return FALSE;
2478 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2479 if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED)
2480 return TRUE;
2482 /* Function pointer equality tests may require that STV_PROTECTED
2483 symbols be treated as dynamic symbols, even when we know that the
2484 dynamic linker will resolve them locally. */
2485 return local_protected;
2488 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2489 aligned. Returns the first TLS output section. */
2491 struct bfd_section *
2492 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2494 struct bfd_section *sec, *tls;
2495 unsigned int align = 0;
2497 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2498 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2499 break;
2500 tls = sec;
2502 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2503 if (sec->alignment_power > align)
2504 align = sec->alignment_power;
2506 elf_hash_table (info)->tls_sec = tls;
2508 /* Ensure the alignment of the first section is the largest alignment,
2509 so that the tls segment starts aligned. */
2510 if (tls != NULL)
2511 tls->alignment_power = align;
2513 return tls;
2516 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2517 static bfd_boolean
2518 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2519 Elf_Internal_Sym *sym)
2521 /* Local symbols do not count, but target specific ones might. */
2522 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2523 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2524 return FALSE;
2526 /* Function symbols do not count. */
2527 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
2528 return FALSE;
2530 /* If the section is undefined, then so is the symbol. */
2531 if (sym->st_shndx == SHN_UNDEF)
2532 return FALSE;
2534 /* If the symbol is defined in the common section, then
2535 it is a common definition and so does not count. */
2536 if (sym->st_shndx == SHN_COMMON)
2537 return FALSE;
2539 /* If the symbol is in a target specific section then we
2540 must rely upon the backend to tell us what it is. */
2541 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2542 /* FIXME - this function is not coded yet:
2544 return _bfd_is_global_symbol_definition (abfd, sym);
2546 Instead for now assume that the definition is not global,
2547 Even if this is wrong, at least the linker will behave
2548 in the same way that it used to do. */
2549 return FALSE;
2551 return TRUE;
2554 /* Search the symbol table of the archive element of the archive ABFD
2555 whose archive map contains a mention of SYMDEF, and determine if
2556 the symbol is defined in this element. */
2557 static bfd_boolean
2558 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2560 Elf_Internal_Shdr * hdr;
2561 bfd_size_type symcount;
2562 bfd_size_type extsymcount;
2563 bfd_size_type extsymoff;
2564 Elf_Internal_Sym *isymbuf;
2565 Elf_Internal_Sym *isym;
2566 Elf_Internal_Sym *isymend;
2567 bfd_boolean result;
2569 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2570 if (abfd == NULL)
2571 return FALSE;
2573 if (! bfd_check_format (abfd, bfd_object))
2574 return FALSE;
2576 /* If we have already included the element containing this symbol in the
2577 link then we do not need to include it again. Just claim that any symbol
2578 it contains is not a definition, so that our caller will not decide to
2579 (re)include this element. */
2580 if (abfd->archive_pass)
2581 return FALSE;
2583 /* Select the appropriate symbol table. */
2584 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2585 hdr = &elf_tdata (abfd)->symtab_hdr;
2586 else
2587 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2589 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2591 /* The sh_info field of the symtab header tells us where the
2592 external symbols start. We don't care about the local symbols. */
2593 if (elf_bad_symtab (abfd))
2595 extsymcount = symcount;
2596 extsymoff = 0;
2598 else
2600 extsymcount = symcount - hdr->sh_info;
2601 extsymoff = hdr->sh_info;
2604 if (extsymcount == 0)
2605 return FALSE;
2607 /* Read in the symbol table. */
2608 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2609 NULL, NULL, NULL);
2610 if (isymbuf == NULL)
2611 return FALSE;
2613 /* Scan the symbol table looking for SYMDEF. */
2614 result = FALSE;
2615 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2617 const char *name;
2619 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
2620 isym->st_name);
2621 if (name == NULL)
2622 break;
2624 if (strcmp (name, symdef->name) == 0)
2626 result = is_global_data_symbol_definition (abfd, isym);
2627 break;
2631 free (isymbuf);
2633 return result;
2636 /* Add an entry to the .dynamic table. */
2638 bfd_boolean
2639 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
2640 bfd_vma tag,
2641 bfd_vma val)
2643 struct elf_link_hash_table *hash_table;
2644 const struct elf_backend_data *bed;
2645 asection *s;
2646 bfd_size_type newsize;
2647 bfd_byte *newcontents;
2648 Elf_Internal_Dyn dyn;
2650 hash_table = elf_hash_table (info);
2651 if (! is_elf_hash_table (hash_table))
2652 return FALSE;
2654 bed = get_elf_backend_data (hash_table->dynobj);
2655 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2656 BFD_ASSERT (s != NULL);
2658 newsize = s->size + bed->s->sizeof_dyn;
2659 newcontents = bfd_realloc (s->contents, newsize);
2660 if (newcontents == NULL)
2661 return FALSE;
2663 dyn.d_tag = tag;
2664 dyn.d_un.d_val = val;
2665 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
2667 s->size = newsize;
2668 s->contents = newcontents;
2670 return TRUE;
2673 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2674 otherwise just check whether one already exists. Returns -1 on error,
2675 1 if a DT_NEEDED tag already exists, and 0 on success. */
2677 static int
2678 elf_add_dt_needed_tag (struct bfd_link_info *info,
2679 const char *soname,
2680 bfd_boolean do_it)
2682 struct elf_link_hash_table *hash_table;
2683 bfd_size_type oldsize;
2684 bfd_size_type strindex;
2686 hash_table = elf_hash_table (info);
2687 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
2688 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
2689 if (strindex == (bfd_size_type) -1)
2690 return -1;
2692 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
2694 asection *sdyn;
2695 const struct elf_backend_data *bed;
2696 bfd_byte *extdyn;
2698 bed = get_elf_backend_data (hash_table->dynobj);
2699 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2700 BFD_ASSERT (sdyn != NULL);
2702 for (extdyn = sdyn->contents;
2703 extdyn < sdyn->contents + sdyn->size;
2704 extdyn += bed->s->sizeof_dyn)
2706 Elf_Internal_Dyn dyn;
2708 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
2709 if (dyn.d_tag == DT_NEEDED
2710 && dyn.d_un.d_val == strindex)
2712 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2713 return 1;
2718 if (do_it)
2720 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
2721 return -1;
2723 else
2724 /* We were just checking for existence of the tag. */
2725 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2727 return 0;
2730 /* Sort symbol by value and section. */
2731 static int
2732 elf_sort_symbol (const void *arg1, const void *arg2)
2734 const struct elf_link_hash_entry *h1;
2735 const struct elf_link_hash_entry *h2;
2736 bfd_signed_vma vdiff;
2738 h1 = *(const struct elf_link_hash_entry **) arg1;
2739 h2 = *(const struct elf_link_hash_entry **) arg2;
2740 vdiff = h1->root.u.def.value - h2->root.u.def.value;
2741 if (vdiff != 0)
2742 return vdiff > 0 ? 1 : -1;
2743 else
2745 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
2746 if (sdiff != 0)
2747 return sdiff > 0 ? 1 : -1;
2749 return 0;
2752 /* This function is used to adjust offsets into .dynstr for
2753 dynamic symbols. This is called via elf_link_hash_traverse. */
2755 static bfd_boolean
2756 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
2758 struct elf_strtab_hash *dynstr = data;
2760 if (h->root.type == bfd_link_hash_warning)
2761 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2763 if (h->dynindx != -1)
2764 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
2765 return TRUE;
2768 /* Assign string offsets in .dynstr, update all structures referencing
2769 them. */
2771 static bfd_boolean
2772 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
2774 struct elf_link_hash_table *hash_table = elf_hash_table (info);
2775 struct elf_link_local_dynamic_entry *entry;
2776 struct elf_strtab_hash *dynstr = hash_table->dynstr;
2777 bfd *dynobj = hash_table->dynobj;
2778 asection *sdyn;
2779 bfd_size_type size;
2780 const struct elf_backend_data *bed;
2781 bfd_byte *extdyn;
2783 _bfd_elf_strtab_finalize (dynstr);
2784 size = _bfd_elf_strtab_size (dynstr);
2786 bed = get_elf_backend_data (dynobj);
2787 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2788 BFD_ASSERT (sdyn != NULL);
2790 /* Update all .dynamic entries referencing .dynstr strings. */
2791 for (extdyn = sdyn->contents;
2792 extdyn < sdyn->contents + sdyn->size;
2793 extdyn += bed->s->sizeof_dyn)
2795 Elf_Internal_Dyn dyn;
2797 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
2798 switch (dyn.d_tag)
2800 case DT_STRSZ:
2801 dyn.d_un.d_val = size;
2802 break;
2803 case DT_NEEDED:
2804 case DT_SONAME:
2805 case DT_RPATH:
2806 case DT_RUNPATH:
2807 case DT_FILTER:
2808 case DT_AUXILIARY:
2809 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
2810 break;
2811 default:
2812 continue;
2814 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
2817 /* Now update local dynamic symbols. */
2818 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
2819 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
2820 entry->isym.st_name);
2822 /* And the rest of dynamic symbols. */
2823 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
2825 /* Adjust version definitions. */
2826 if (elf_tdata (output_bfd)->cverdefs)
2828 asection *s;
2829 bfd_byte *p;
2830 bfd_size_type i;
2831 Elf_Internal_Verdef def;
2832 Elf_Internal_Verdaux defaux;
2834 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
2835 p = s->contents;
2838 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
2839 &def);
2840 p += sizeof (Elf_External_Verdef);
2841 for (i = 0; i < def.vd_cnt; ++i)
2843 _bfd_elf_swap_verdaux_in (output_bfd,
2844 (Elf_External_Verdaux *) p, &defaux);
2845 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
2846 defaux.vda_name);
2847 _bfd_elf_swap_verdaux_out (output_bfd,
2848 &defaux, (Elf_External_Verdaux *) p);
2849 p += sizeof (Elf_External_Verdaux);
2852 while (def.vd_next);
2855 /* Adjust version references. */
2856 if (elf_tdata (output_bfd)->verref)
2858 asection *s;
2859 bfd_byte *p;
2860 bfd_size_type i;
2861 Elf_Internal_Verneed need;
2862 Elf_Internal_Vernaux needaux;
2864 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
2865 p = s->contents;
2868 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
2869 &need);
2870 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
2871 _bfd_elf_swap_verneed_out (output_bfd, &need,
2872 (Elf_External_Verneed *) p);
2873 p += sizeof (Elf_External_Verneed);
2874 for (i = 0; i < need.vn_cnt; ++i)
2876 _bfd_elf_swap_vernaux_in (output_bfd,
2877 (Elf_External_Vernaux *) p, &needaux);
2878 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
2879 needaux.vna_name);
2880 _bfd_elf_swap_vernaux_out (output_bfd,
2881 &needaux,
2882 (Elf_External_Vernaux *) p);
2883 p += sizeof (Elf_External_Vernaux);
2886 while (need.vn_next);
2889 return TRUE;
2892 /* Add symbols from an ELF object file to the linker hash table. */
2894 static bfd_boolean
2895 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
2897 bfd_boolean (*add_symbol_hook)
2898 (bfd *, struct bfd_link_info *, Elf_Internal_Sym *,
2899 const char **, flagword *, asection **, bfd_vma *);
2900 bfd_boolean (*check_relocs)
2901 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
2902 bfd_boolean (*check_directives)
2903 (bfd *, struct bfd_link_info *);
2904 bfd_boolean collect;
2905 Elf_Internal_Shdr *hdr;
2906 bfd_size_type symcount;
2907 bfd_size_type extsymcount;
2908 bfd_size_type extsymoff;
2909 struct elf_link_hash_entry **sym_hash;
2910 bfd_boolean dynamic;
2911 Elf_External_Versym *extversym = NULL;
2912 Elf_External_Versym *ever;
2913 struct elf_link_hash_entry *weaks;
2914 struct elf_link_hash_entry **nondeflt_vers = NULL;
2915 bfd_size_type nondeflt_vers_cnt = 0;
2916 Elf_Internal_Sym *isymbuf = NULL;
2917 Elf_Internal_Sym *isym;
2918 Elf_Internal_Sym *isymend;
2919 const struct elf_backend_data *bed;
2920 bfd_boolean add_needed;
2921 struct elf_link_hash_table * hash_table;
2922 bfd_size_type amt;
2924 hash_table = elf_hash_table (info);
2926 bed = get_elf_backend_data (abfd);
2927 add_symbol_hook = bed->elf_add_symbol_hook;
2928 collect = bed->collect;
2930 if ((abfd->flags & DYNAMIC) == 0)
2931 dynamic = FALSE;
2932 else
2934 dynamic = TRUE;
2936 /* You can't use -r against a dynamic object. Also, there's no
2937 hope of using a dynamic object which does not exactly match
2938 the format of the output file. */
2939 if (info->relocatable
2940 || !is_elf_hash_table (hash_table)
2941 || hash_table->root.creator != abfd->xvec)
2943 bfd_set_error (bfd_error_invalid_operation);
2944 goto error_return;
2948 /* As a GNU extension, any input sections which are named
2949 .gnu.warning.SYMBOL are treated as warning symbols for the given
2950 symbol. This differs from .gnu.warning sections, which generate
2951 warnings when they are included in an output file. */
2952 if (info->executable)
2954 asection *s;
2956 for (s = abfd->sections; s != NULL; s = s->next)
2958 const char *name;
2960 name = bfd_get_section_name (abfd, s);
2961 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
2963 char *msg;
2964 bfd_size_type sz;
2965 bfd_size_type prefix_len;
2966 const char * gnu_warning_prefix = _("warning: ");
2968 name += sizeof ".gnu.warning." - 1;
2970 /* If this is a shared object, then look up the symbol
2971 in the hash table. If it is there, and it is already
2972 been defined, then we will not be using the entry
2973 from this shared object, so we don't need to warn.
2974 FIXME: If we see the definition in a regular object
2975 later on, we will warn, but we shouldn't. The only
2976 fix is to keep track of what warnings we are supposed
2977 to emit, and then handle them all at the end of the
2978 link. */
2979 if (dynamic)
2981 struct elf_link_hash_entry *h;
2983 h = elf_link_hash_lookup (hash_table, name,
2984 FALSE, FALSE, TRUE);
2986 /* FIXME: What about bfd_link_hash_common? */
2987 if (h != NULL
2988 && (h->root.type == bfd_link_hash_defined
2989 || h->root.type == bfd_link_hash_defweak))
2991 /* We don't want to issue this warning. Clobber
2992 the section size so that the warning does not
2993 get copied into the output file. */
2994 s->size = 0;
2995 continue;
2999 sz = s->size;
3000 prefix_len = strlen (gnu_warning_prefix);
3001 msg = bfd_alloc (abfd, prefix_len + sz + 1);
3002 if (msg == NULL)
3003 goto error_return;
3005 strcpy (msg, gnu_warning_prefix);
3006 if (! bfd_get_section_contents (abfd, s, msg + prefix_len, 0, sz))
3007 goto error_return;
3009 msg[prefix_len + sz] = '\0';
3011 if (! (_bfd_generic_link_add_one_symbol
3012 (info, abfd, name, BSF_WARNING, s, 0, msg,
3013 FALSE, collect, NULL)))
3014 goto error_return;
3016 if (! info->relocatable)
3018 /* Clobber the section size so that the warning does
3019 not get copied into the output file. */
3020 s->size = 0;
3026 add_needed = TRUE;
3027 if (! dynamic)
3029 /* If we are creating a shared library, create all the dynamic
3030 sections immediately. We need to attach them to something,
3031 so we attach them to this BFD, provided it is the right
3032 format. FIXME: If there are no input BFD's of the same
3033 format as the output, we can't make a shared library. */
3034 if (info->shared
3035 && is_elf_hash_table (hash_table)
3036 && hash_table->root.creator == abfd->xvec
3037 && ! hash_table->dynamic_sections_created)
3039 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3040 goto error_return;
3043 else if (!is_elf_hash_table (hash_table))
3044 goto error_return;
3045 else
3047 asection *s;
3048 const char *soname = NULL;
3049 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3050 int ret;
3052 /* ld --just-symbols and dynamic objects don't mix very well.
3053 Test for --just-symbols by looking at info set up by
3054 _bfd_elf_link_just_syms. */
3055 if ((s = abfd->sections) != NULL
3056 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3057 goto error_return;
3059 /* If this dynamic lib was specified on the command line with
3060 --as-needed in effect, then we don't want to add a DT_NEEDED
3061 tag unless the lib is actually used. Similary for libs brought
3062 in by another lib's DT_NEEDED. When --no-add-needed is used
3063 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3064 any dynamic library in DT_NEEDED tags in the dynamic lib at
3065 all. */
3066 add_needed = (elf_dyn_lib_class (abfd)
3067 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3068 | DYN_NO_NEEDED)) == 0;
3070 s = bfd_get_section_by_name (abfd, ".dynamic");
3071 if (s != NULL)
3073 bfd_byte *dynbuf;
3074 bfd_byte *extdyn;
3075 int elfsec;
3076 unsigned long shlink;
3078 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3079 goto error_free_dyn;
3081 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3082 if (elfsec == -1)
3083 goto error_free_dyn;
3084 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3086 for (extdyn = dynbuf;
3087 extdyn < dynbuf + s->size;
3088 extdyn += bed->s->sizeof_dyn)
3090 Elf_Internal_Dyn dyn;
3092 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3093 if (dyn.d_tag == DT_SONAME)
3095 unsigned int tagv = dyn.d_un.d_val;
3096 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3097 if (soname == NULL)
3098 goto error_free_dyn;
3100 if (dyn.d_tag == DT_NEEDED)
3102 struct bfd_link_needed_list *n, **pn;
3103 char *fnm, *anm;
3104 unsigned int tagv = dyn.d_un.d_val;
3106 amt = sizeof (struct bfd_link_needed_list);
3107 n = bfd_alloc (abfd, amt);
3108 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3109 if (n == NULL || fnm == NULL)
3110 goto error_free_dyn;
3111 amt = strlen (fnm) + 1;
3112 anm = bfd_alloc (abfd, amt);
3113 if (anm == NULL)
3114 goto error_free_dyn;
3115 memcpy (anm, fnm, amt);
3116 n->name = anm;
3117 n->by = abfd;
3118 n->next = NULL;
3119 for (pn = & hash_table->needed;
3120 *pn != NULL;
3121 pn = &(*pn)->next)
3123 *pn = n;
3125 if (dyn.d_tag == DT_RUNPATH)
3127 struct bfd_link_needed_list *n, **pn;
3128 char *fnm, *anm;
3129 unsigned int tagv = dyn.d_un.d_val;
3131 amt = sizeof (struct bfd_link_needed_list);
3132 n = bfd_alloc (abfd, amt);
3133 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3134 if (n == NULL || fnm == NULL)
3135 goto error_free_dyn;
3136 amt = strlen (fnm) + 1;
3137 anm = bfd_alloc (abfd, amt);
3138 if (anm == NULL)
3139 goto error_free_dyn;
3140 memcpy (anm, fnm, amt);
3141 n->name = anm;
3142 n->by = abfd;
3143 n->next = NULL;
3144 for (pn = & runpath;
3145 *pn != NULL;
3146 pn = &(*pn)->next)
3148 *pn = n;
3150 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3151 if (!runpath && dyn.d_tag == DT_RPATH)
3153 struct bfd_link_needed_list *n, **pn;
3154 char *fnm, *anm;
3155 unsigned int tagv = dyn.d_un.d_val;
3157 amt = sizeof (struct bfd_link_needed_list);
3158 n = bfd_alloc (abfd, amt);
3159 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3160 if (n == NULL || fnm == NULL)
3161 goto error_free_dyn;
3162 amt = strlen (fnm) + 1;
3163 anm = bfd_alloc (abfd, amt);
3164 if (anm == NULL)
3166 error_free_dyn:
3167 free (dynbuf);
3168 goto error_return;
3170 memcpy (anm, fnm, amt);
3171 n->name = anm;
3172 n->by = abfd;
3173 n->next = NULL;
3174 for (pn = & rpath;
3175 *pn != NULL;
3176 pn = &(*pn)->next)
3178 *pn = n;
3182 free (dynbuf);
3185 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3186 frees all more recently bfd_alloc'd blocks as well. */
3187 if (runpath)
3188 rpath = runpath;
3190 if (rpath)
3192 struct bfd_link_needed_list **pn;
3193 for (pn = & hash_table->runpath;
3194 *pn != NULL;
3195 pn = &(*pn)->next)
3197 *pn = rpath;
3200 /* We do not want to include any of the sections in a dynamic
3201 object in the output file. We hack by simply clobbering the
3202 list of sections in the BFD. This could be handled more
3203 cleanly by, say, a new section flag; the existing
3204 SEC_NEVER_LOAD flag is not the one we want, because that one
3205 still implies that the section takes up space in the output
3206 file. */
3207 bfd_section_list_clear (abfd);
3209 /* If this is the first dynamic object found in the link, create
3210 the special sections required for dynamic linking. */
3211 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3212 goto error_return;
3214 /* Find the name to use in a DT_NEEDED entry that refers to this
3215 object. If the object has a DT_SONAME entry, we use it.
3216 Otherwise, if the generic linker stuck something in
3217 elf_dt_name, we use that. Otherwise, we just use the file
3218 name. */
3219 if (soname == NULL || *soname == '\0')
3221 soname = elf_dt_name (abfd);
3222 if (soname == NULL || *soname == '\0')
3223 soname = bfd_get_filename (abfd);
3226 /* Save the SONAME because sometimes the linker emulation code
3227 will need to know it. */
3228 elf_dt_name (abfd) = soname;
3230 ret = elf_add_dt_needed_tag (info, soname, add_needed);
3231 if (ret < 0)
3232 goto error_return;
3234 /* If we have already included this dynamic object in the
3235 link, just ignore it. There is no reason to include a
3236 particular dynamic object more than once. */
3237 if (ret > 0)
3238 return TRUE;
3241 /* If this is a dynamic object, we always link against the .dynsym
3242 symbol table, not the .symtab symbol table. The dynamic linker
3243 will only see the .dynsym symbol table, so there is no reason to
3244 look at .symtab for a dynamic object. */
3246 if (! dynamic || elf_dynsymtab (abfd) == 0)
3247 hdr = &elf_tdata (abfd)->symtab_hdr;
3248 else
3249 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3251 symcount = hdr->sh_size / bed->s->sizeof_sym;
3253 /* The sh_info field of the symtab header tells us where the
3254 external symbols start. We don't care about the local symbols at
3255 this point. */
3256 if (elf_bad_symtab (abfd))
3258 extsymcount = symcount;
3259 extsymoff = 0;
3261 else
3263 extsymcount = symcount - hdr->sh_info;
3264 extsymoff = hdr->sh_info;
3267 sym_hash = NULL;
3268 if (extsymcount != 0)
3270 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3271 NULL, NULL, NULL);
3272 if (isymbuf == NULL)
3273 goto error_return;
3275 /* We store a pointer to the hash table entry for each external
3276 symbol. */
3277 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3278 sym_hash = bfd_alloc (abfd, amt);
3279 if (sym_hash == NULL)
3280 goto error_free_sym;
3281 elf_sym_hashes (abfd) = sym_hash;
3284 if (dynamic)
3286 /* Read in any version definitions. */
3287 if (! _bfd_elf_slurp_version_tables (abfd))
3288 goto error_free_sym;
3290 /* Read in the symbol versions, but don't bother to convert them
3291 to internal format. */
3292 if (elf_dynversym (abfd) != 0)
3294 Elf_Internal_Shdr *versymhdr;
3296 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3297 extversym = bfd_malloc (versymhdr->sh_size);
3298 if (extversym == NULL)
3299 goto error_free_sym;
3300 amt = versymhdr->sh_size;
3301 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3302 || bfd_bread (extversym, amt, abfd) != amt)
3303 goto error_free_vers;
3307 weaks = NULL;
3309 ever = extversym != NULL ? extversym + extsymoff : NULL;
3310 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3311 isym < isymend;
3312 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3314 int bind;
3315 bfd_vma value;
3316 asection *sec;
3317 flagword flags;
3318 const char *name;
3319 struct elf_link_hash_entry *h;
3320 bfd_boolean definition;
3321 bfd_boolean size_change_ok;
3322 bfd_boolean type_change_ok;
3323 bfd_boolean new_weakdef;
3324 bfd_boolean override;
3325 unsigned int old_alignment;
3326 bfd *old_bfd;
3328 override = FALSE;
3330 flags = BSF_NO_FLAGS;
3331 sec = NULL;
3332 value = isym->st_value;
3333 *sym_hash = NULL;
3335 bind = ELF_ST_BIND (isym->st_info);
3336 if (bind == STB_LOCAL)
3338 /* This should be impossible, since ELF requires that all
3339 global symbols follow all local symbols, and that sh_info
3340 point to the first global symbol. Unfortunately, Irix 5
3341 screws this up. */
3342 continue;
3344 else if (bind == STB_GLOBAL)
3346 if (isym->st_shndx != SHN_UNDEF
3347 && isym->st_shndx != SHN_COMMON)
3348 flags = BSF_GLOBAL;
3350 else if (bind == STB_WEAK)
3351 flags = BSF_WEAK;
3352 else
3354 /* Leave it up to the processor backend. */
3357 if (isym->st_shndx == SHN_UNDEF)
3358 sec = bfd_und_section_ptr;
3359 else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
3361 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3362 if (sec == NULL)
3363 sec = bfd_abs_section_ptr;
3364 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3365 value -= sec->vma;
3367 else if (isym->st_shndx == SHN_ABS)
3368 sec = bfd_abs_section_ptr;
3369 else if (isym->st_shndx == SHN_COMMON)
3371 sec = bfd_com_section_ptr;
3372 /* What ELF calls the size we call the value. What ELF
3373 calls the value we call the alignment. */
3374 value = isym->st_size;
3376 else
3378 /* Leave it up to the processor backend. */
3381 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3382 isym->st_name);
3383 if (name == NULL)
3384 goto error_free_vers;
3386 if (isym->st_shndx == SHN_COMMON
3387 && ELF_ST_TYPE (isym->st_info) == STT_TLS)
3389 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3391 if (tcomm == NULL)
3393 tcomm = bfd_make_section (abfd, ".tcommon");
3394 if (tcomm == NULL
3395 || !bfd_set_section_flags (abfd, tcomm, (SEC_ALLOC
3396 | SEC_IS_COMMON
3397 | SEC_LINKER_CREATED
3398 | SEC_THREAD_LOCAL)))
3399 goto error_free_vers;
3401 sec = tcomm;
3403 else if (add_symbol_hook)
3405 if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec,
3406 &value))
3407 goto error_free_vers;
3409 /* The hook function sets the name to NULL if this symbol
3410 should be skipped for some reason. */
3411 if (name == NULL)
3412 continue;
3415 /* Sanity check that all possibilities were handled. */
3416 if (sec == NULL)
3418 bfd_set_error (bfd_error_bad_value);
3419 goto error_free_vers;
3422 if (bfd_is_und_section (sec)
3423 || bfd_is_com_section (sec))
3424 definition = FALSE;
3425 else
3426 definition = TRUE;
3428 size_change_ok = FALSE;
3429 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
3430 old_alignment = 0;
3431 old_bfd = NULL;
3433 if (is_elf_hash_table (hash_table))
3435 Elf_Internal_Versym iver;
3436 unsigned int vernum = 0;
3437 bfd_boolean skip;
3439 if (ever != NULL)
3441 _bfd_elf_swap_versym_in (abfd, ever, &iver);
3442 vernum = iver.vs_vers & VERSYM_VERSION;
3444 /* If this is a hidden symbol, or if it is not version
3445 1, we append the version name to the symbol name.
3446 However, we do not modify a non-hidden absolute
3447 symbol, because it might be the version symbol
3448 itself. FIXME: What if it isn't? */
3449 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
3450 || (vernum > 1 && ! bfd_is_abs_section (sec)))
3452 const char *verstr;
3453 size_t namelen, verlen, newlen;
3454 char *newname, *p;
3456 if (isym->st_shndx != SHN_UNDEF)
3458 if (vernum > elf_tdata (abfd)->dynverdef_hdr.sh_info)
3460 (*_bfd_error_handler)
3461 (_("%B: %s: invalid version %u (max %d)"),
3462 abfd, name, vernum,
3463 elf_tdata (abfd)->dynverdef_hdr.sh_info);
3464 bfd_set_error (bfd_error_bad_value);
3465 goto error_free_vers;
3467 else if (vernum > 1)
3468 verstr =
3469 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
3470 else
3471 verstr = "";
3473 else
3475 /* We cannot simply test for the number of
3476 entries in the VERNEED section since the
3477 numbers for the needed versions do not start
3478 at 0. */
3479 Elf_Internal_Verneed *t;
3481 verstr = NULL;
3482 for (t = elf_tdata (abfd)->verref;
3483 t != NULL;
3484 t = t->vn_nextref)
3486 Elf_Internal_Vernaux *a;
3488 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3490 if (a->vna_other == vernum)
3492 verstr = a->vna_nodename;
3493 break;
3496 if (a != NULL)
3497 break;
3499 if (verstr == NULL)
3501 (*_bfd_error_handler)
3502 (_("%B: %s: invalid needed version %d"),
3503 abfd, name, vernum);
3504 bfd_set_error (bfd_error_bad_value);
3505 goto error_free_vers;
3509 namelen = strlen (name);
3510 verlen = strlen (verstr);
3511 newlen = namelen + verlen + 2;
3512 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3513 && isym->st_shndx != SHN_UNDEF)
3514 ++newlen;
3516 newname = bfd_alloc (abfd, newlen);
3517 if (newname == NULL)
3518 goto error_free_vers;
3519 memcpy (newname, name, namelen);
3520 p = newname + namelen;
3521 *p++ = ELF_VER_CHR;
3522 /* If this is a defined non-hidden version symbol,
3523 we add another @ to the name. This indicates the
3524 default version of the symbol. */
3525 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3526 && isym->st_shndx != SHN_UNDEF)
3527 *p++ = ELF_VER_CHR;
3528 memcpy (p, verstr, verlen + 1);
3530 name = newname;
3534 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
3535 sym_hash, &skip, &override,
3536 &type_change_ok, &size_change_ok))
3537 goto error_free_vers;
3539 if (skip)
3540 continue;
3542 if (override)
3543 definition = FALSE;
3545 h = *sym_hash;
3546 while (h->root.type == bfd_link_hash_indirect
3547 || h->root.type == bfd_link_hash_warning)
3548 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3550 /* Remember the old alignment if this is a common symbol, so
3551 that we don't reduce the alignment later on. We can't
3552 check later, because _bfd_generic_link_add_one_symbol
3553 will set a default for the alignment which we want to
3554 override. We also remember the old bfd where the existing
3555 definition comes from. */
3556 switch (h->root.type)
3558 default:
3559 break;
3561 case bfd_link_hash_defined:
3562 case bfd_link_hash_defweak:
3563 old_bfd = h->root.u.def.section->owner;
3564 break;
3566 case bfd_link_hash_common:
3567 old_bfd = h->root.u.c.p->section->owner;
3568 old_alignment = h->root.u.c.p->alignment_power;
3569 break;
3572 if (elf_tdata (abfd)->verdef != NULL
3573 && ! override
3574 && vernum > 1
3575 && definition)
3576 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
3579 if (! (_bfd_generic_link_add_one_symbol
3580 (info, abfd, name, flags, sec, value, NULL, FALSE, collect,
3581 (struct bfd_link_hash_entry **) sym_hash)))
3582 goto error_free_vers;
3584 h = *sym_hash;
3585 while (h->root.type == bfd_link_hash_indirect
3586 || h->root.type == bfd_link_hash_warning)
3587 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3588 *sym_hash = h;
3590 new_weakdef = FALSE;
3591 if (dynamic
3592 && definition
3593 && (flags & BSF_WEAK) != 0
3594 && ELF_ST_TYPE (isym->st_info) != STT_FUNC
3595 && is_elf_hash_table (hash_table)
3596 && h->weakdef == NULL)
3598 /* Keep a list of all weak defined non function symbols from
3599 a dynamic object, using the weakdef field. Later in this
3600 function we will set the weakdef field to the correct
3601 value. We only put non-function symbols from dynamic
3602 objects on this list, because that happens to be the only
3603 time we need to know the normal symbol corresponding to a
3604 weak symbol, and the information is time consuming to
3605 figure out. If the weakdef field is not already NULL,
3606 then this symbol was already defined by some previous
3607 dynamic object, and we will be using that previous
3608 definition anyhow. */
3610 h->weakdef = weaks;
3611 weaks = h;
3612 new_weakdef = TRUE;
3615 /* Set the alignment of a common symbol. */
3616 if (isym->st_shndx == SHN_COMMON
3617 && h->root.type == bfd_link_hash_common)
3619 unsigned int align;
3621 align = bfd_log2 (isym->st_value);
3622 if (align > old_alignment
3623 /* Permit an alignment power of zero if an alignment of one
3624 is specified and no other alignments have been specified. */
3625 || (isym->st_value == 1 && old_alignment == 0))
3626 h->root.u.c.p->alignment_power = align;
3627 else
3628 h->root.u.c.p->alignment_power = old_alignment;
3631 if (is_elf_hash_table (hash_table))
3633 int old_flags;
3634 bfd_boolean dynsym;
3635 int new_flag;
3637 /* Check the alignment when a common symbol is involved. This
3638 can change when a common symbol is overridden by a normal
3639 definition or a common symbol is ignored due to the old
3640 normal definition. We need to make sure the maximum
3641 alignment is maintained. */
3642 if ((old_alignment || isym->st_shndx == SHN_COMMON)
3643 && h->root.type != bfd_link_hash_common)
3645 unsigned int common_align;
3646 unsigned int normal_align;
3647 unsigned int symbol_align;
3648 bfd *normal_bfd;
3649 bfd *common_bfd;
3651 symbol_align = ffs (h->root.u.def.value) - 1;
3652 if (h->root.u.def.section->owner != NULL
3653 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3655 normal_align = h->root.u.def.section->alignment_power;
3656 if (normal_align > symbol_align)
3657 normal_align = symbol_align;
3659 else
3660 normal_align = symbol_align;
3662 if (old_alignment)
3664 common_align = old_alignment;
3665 common_bfd = old_bfd;
3666 normal_bfd = abfd;
3668 else
3670 common_align = bfd_log2 (isym->st_value);
3671 common_bfd = abfd;
3672 normal_bfd = old_bfd;
3675 if (normal_align < common_align)
3676 (*_bfd_error_handler)
3677 (_("Warning: alignment %u of symbol `%s' in %B"
3678 " is smaller than %u in %B"),
3679 normal_bfd, common_bfd,
3680 1 << normal_align, name, 1 << common_align);
3683 /* Remember the symbol size and type. */
3684 if (isym->st_size != 0
3685 && (definition || h->size == 0))
3687 if (h->size != 0 && h->size != isym->st_size && ! size_change_ok)
3688 (*_bfd_error_handler)
3689 (_("Warning: size of symbol `%s' changed"
3690 " from %lu in %B to %lu in %B"),
3691 old_bfd, abfd,
3692 name, (unsigned long) h->size,
3693 (unsigned long) isym->st_size);
3695 h->size = isym->st_size;
3698 /* If this is a common symbol, then we always want H->SIZE
3699 to be the size of the common symbol. The code just above
3700 won't fix the size if a common symbol becomes larger. We
3701 don't warn about a size change here, because that is
3702 covered by --warn-common. */
3703 if (h->root.type == bfd_link_hash_common)
3704 h->size = h->root.u.c.size;
3706 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
3707 && (definition || h->type == STT_NOTYPE))
3709 if (h->type != STT_NOTYPE
3710 && h->type != ELF_ST_TYPE (isym->st_info)
3711 && ! type_change_ok)
3712 (*_bfd_error_handler)
3713 (_("Warning: type of symbol `%s' changed"
3714 " from %d to %d in %B"),
3715 abfd, name, h->type, ELF_ST_TYPE (isym->st_info));
3717 h->type = ELF_ST_TYPE (isym->st_info);
3720 /* If st_other has a processor-specific meaning, specific
3721 code might be needed here. We never merge the visibility
3722 attribute with the one from a dynamic object. */
3723 if (bed->elf_backend_merge_symbol_attribute)
3724 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
3725 dynamic);
3727 if (isym->st_other != 0 && !dynamic)
3729 unsigned char hvis, symvis, other, nvis;
3731 /* Take the balance of OTHER from the definition. */
3732 other = (definition ? isym->st_other : h->other);
3733 other &= ~ ELF_ST_VISIBILITY (-1);
3735 /* Combine visibilities, using the most constraining one. */
3736 hvis = ELF_ST_VISIBILITY (h->other);
3737 symvis = ELF_ST_VISIBILITY (isym->st_other);
3738 if (! hvis)
3739 nvis = symvis;
3740 else if (! symvis)
3741 nvis = hvis;
3742 else
3743 nvis = hvis < symvis ? hvis : symvis;
3745 h->other = other | nvis;
3748 /* Set a flag in the hash table entry indicating the type of
3749 reference or definition we just found. Keep a count of
3750 the number of dynamic symbols we find. A dynamic symbol
3751 is one which is referenced or defined by both a regular
3752 object and a shared object. */
3753 old_flags = h->elf_link_hash_flags;
3754 dynsym = FALSE;
3755 if (! dynamic)
3757 if (! definition)
3759 new_flag = ELF_LINK_HASH_REF_REGULAR;
3760 if (bind != STB_WEAK)
3761 new_flag |= ELF_LINK_HASH_REF_REGULAR_NONWEAK;
3763 else
3764 new_flag = ELF_LINK_HASH_DEF_REGULAR;
3765 if (! info->executable
3766 || (old_flags & (ELF_LINK_HASH_DEF_DYNAMIC
3767 | ELF_LINK_HASH_REF_DYNAMIC)) != 0)
3768 dynsym = TRUE;
3770 else
3772 if (! definition)
3773 new_flag = ELF_LINK_HASH_REF_DYNAMIC;
3774 else
3775 new_flag = ELF_LINK_HASH_DEF_DYNAMIC;
3776 if ((old_flags & (ELF_LINK_HASH_DEF_REGULAR
3777 | ELF_LINK_HASH_REF_REGULAR)) != 0
3778 || (h->weakdef != NULL
3779 && ! new_weakdef
3780 && h->weakdef->dynindx != -1))
3781 dynsym = TRUE;
3784 h->elf_link_hash_flags |= new_flag;
3786 /* Check to see if we need to add an indirect symbol for
3787 the default name. */
3788 if (definition || h->root.type == bfd_link_hash_common)
3789 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
3790 &sec, &value, &dynsym,
3791 override))
3792 goto error_free_vers;
3794 if (definition && !dynamic)
3796 char *p = strchr (name, ELF_VER_CHR);
3797 if (p != NULL && p[1] != ELF_VER_CHR)
3799 /* Queue non-default versions so that .symver x, x@FOO
3800 aliases can be checked. */
3801 if (! nondeflt_vers)
3803 amt = (isymend - isym + 1)
3804 * sizeof (struct elf_link_hash_entry *);
3805 nondeflt_vers = bfd_malloc (amt);
3807 nondeflt_vers [nondeflt_vers_cnt++] = h;
3811 if (dynsym && h->dynindx == -1)
3813 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3814 goto error_free_vers;
3815 if (h->weakdef != NULL
3816 && ! new_weakdef
3817 && h->weakdef->dynindx == -1)
3819 if (! bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
3820 goto error_free_vers;
3823 else if (dynsym && h->dynindx != -1)
3824 /* If the symbol already has a dynamic index, but
3825 visibility says it should not be visible, turn it into
3826 a local symbol. */
3827 switch (ELF_ST_VISIBILITY (h->other))
3829 case STV_INTERNAL:
3830 case STV_HIDDEN:
3831 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
3832 dynsym = FALSE;
3833 break;
3836 if (!add_needed
3837 && definition
3838 && dynsym
3839 && (h->elf_link_hash_flags
3840 & ELF_LINK_HASH_REF_REGULAR) != 0)
3842 int ret;
3843 const char *soname = elf_dt_name (abfd);
3845 /* A symbol from a library loaded via DT_NEEDED of some
3846 other library is referenced by a regular object.
3847 Add a DT_NEEDED entry for it. Issue an error if
3848 --no-add-needed is used. */
3849 if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
3851 (*_bfd_error_handler)
3852 (_("%s: invalid DSO for symbol `%s' definition"),
3853 abfd, name);
3854 bfd_set_error (bfd_error_bad_value);
3855 goto error_free_vers;
3858 add_needed = TRUE;
3859 ret = elf_add_dt_needed_tag (info, soname, add_needed);
3860 if (ret < 0)
3861 goto error_free_vers;
3863 BFD_ASSERT (ret == 0);
3868 /* Now that all the symbols from this input file are created, handle
3869 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
3870 if (nondeflt_vers != NULL)
3872 bfd_size_type cnt, symidx;
3874 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
3876 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
3877 char *shortname, *p;
3879 p = strchr (h->root.root.string, ELF_VER_CHR);
3880 if (p == NULL
3881 || (h->root.type != bfd_link_hash_defined
3882 && h->root.type != bfd_link_hash_defweak))
3883 continue;
3885 amt = p - h->root.root.string;
3886 shortname = bfd_malloc (amt + 1);
3887 memcpy (shortname, h->root.root.string, amt);
3888 shortname[amt] = '\0';
3890 hi = (struct elf_link_hash_entry *)
3891 bfd_link_hash_lookup (&hash_table->root, shortname,
3892 FALSE, FALSE, FALSE);
3893 if (hi != NULL
3894 && hi->root.type == h->root.type
3895 && hi->root.u.def.value == h->root.u.def.value
3896 && hi->root.u.def.section == h->root.u.def.section)
3898 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
3899 hi->root.type = bfd_link_hash_indirect;
3900 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
3901 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
3902 sym_hash = elf_sym_hashes (abfd);
3903 if (sym_hash)
3904 for (symidx = 0; symidx < extsymcount; ++symidx)
3905 if (sym_hash[symidx] == hi)
3907 sym_hash[symidx] = h;
3908 break;
3911 free (shortname);
3913 free (nondeflt_vers);
3914 nondeflt_vers = NULL;
3917 if (extversym != NULL)
3919 free (extversym);
3920 extversym = NULL;
3923 if (isymbuf != NULL)
3924 free (isymbuf);
3925 isymbuf = NULL;
3927 /* Now set the weakdefs field correctly for all the weak defined
3928 symbols we found. The only way to do this is to search all the
3929 symbols. Since we only need the information for non functions in
3930 dynamic objects, that's the only time we actually put anything on
3931 the list WEAKS. We need this information so that if a regular
3932 object refers to a symbol defined weakly in a dynamic object, the
3933 real symbol in the dynamic object is also put in the dynamic
3934 symbols; we also must arrange for both symbols to point to the
3935 same memory location. We could handle the general case of symbol
3936 aliasing, but a general symbol alias can only be generated in
3937 assembler code, handling it correctly would be very time
3938 consuming, and other ELF linkers don't handle general aliasing
3939 either. */
3940 if (weaks != NULL)
3942 struct elf_link_hash_entry **hpp;
3943 struct elf_link_hash_entry **hppend;
3944 struct elf_link_hash_entry **sorted_sym_hash;
3945 struct elf_link_hash_entry *h;
3946 size_t sym_count;
3948 /* Since we have to search the whole symbol list for each weak
3949 defined symbol, search time for N weak defined symbols will be
3950 O(N^2). Binary search will cut it down to O(NlogN). */
3951 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3952 sorted_sym_hash = bfd_malloc (amt);
3953 if (sorted_sym_hash == NULL)
3954 goto error_return;
3955 sym_hash = sorted_sym_hash;
3956 hpp = elf_sym_hashes (abfd);
3957 hppend = hpp + extsymcount;
3958 sym_count = 0;
3959 for (; hpp < hppend; hpp++)
3961 h = *hpp;
3962 if (h != NULL
3963 && h->root.type == bfd_link_hash_defined
3964 && h->type != STT_FUNC)
3966 *sym_hash = h;
3967 sym_hash++;
3968 sym_count++;
3972 qsort (sorted_sym_hash, sym_count,
3973 sizeof (struct elf_link_hash_entry *),
3974 elf_sort_symbol);
3976 while (weaks != NULL)
3978 struct elf_link_hash_entry *hlook;
3979 asection *slook;
3980 bfd_vma vlook;
3981 long ilook;
3982 size_t i, j, idx;
3984 hlook = weaks;
3985 weaks = hlook->weakdef;
3986 hlook->weakdef = NULL;
3988 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
3989 || hlook->root.type == bfd_link_hash_defweak
3990 || hlook->root.type == bfd_link_hash_common
3991 || hlook->root.type == bfd_link_hash_indirect);
3992 slook = hlook->root.u.def.section;
3993 vlook = hlook->root.u.def.value;
3995 ilook = -1;
3996 i = 0;
3997 j = sym_count;
3998 while (i < j)
4000 bfd_signed_vma vdiff;
4001 idx = (i + j) / 2;
4002 h = sorted_sym_hash [idx];
4003 vdiff = vlook - h->root.u.def.value;
4004 if (vdiff < 0)
4005 j = idx;
4006 else if (vdiff > 0)
4007 i = idx + 1;
4008 else
4010 long sdiff = slook->id - h->root.u.def.section->id;
4011 if (sdiff < 0)
4012 j = idx;
4013 else if (sdiff > 0)
4014 i = idx + 1;
4015 else
4017 ilook = idx;
4018 break;
4023 /* We didn't find a value/section match. */
4024 if (ilook == -1)
4025 continue;
4027 for (i = ilook; i < sym_count; i++)
4029 h = sorted_sym_hash [i];
4031 /* Stop if value or section doesn't match. */
4032 if (h->root.u.def.value != vlook
4033 || h->root.u.def.section != slook)
4034 break;
4035 else if (h != hlook)
4037 hlook->weakdef = h;
4039 /* If the weak definition is in the list of dynamic
4040 symbols, make sure the real definition is put
4041 there as well. */
4042 if (hlook->dynindx != -1 && h->dynindx == -1)
4044 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4045 goto error_return;
4048 /* If the real definition is in the list of dynamic
4049 symbols, make sure the weak definition is put
4050 there as well. If we don't do this, then the
4051 dynamic loader might not merge the entries for the
4052 real definition and the weak definition. */
4053 if (h->dynindx != -1 && hlook->dynindx == -1)
4055 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4056 goto error_return;
4058 break;
4063 free (sorted_sym_hash);
4066 check_directives = get_elf_backend_data (abfd)->check_directives;
4067 if (check_directives)
4068 check_directives (abfd, info);
4070 /* If this object is the same format as the output object, and it is
4071 not a shared library, then let the backend look through the
4072 relocs.
4074 This is required to build global offset table entries and to
4075 arrange for dynamic relocs. It is not required for the
4076 particular common case of linking non PIC code, even when linking
4077 against shared libraries, but unfortunately there is no way of
4078 knowing whether an object file has been compiled PIC or not.
4079 Looking through the relocs is not particularly time consuming.
4080 The problem is that we must either (1) keep the relocs in memory,
4081 which causes the linker to require additional runtime memory or
4082 (2) read the relocs twice from the input file, which wastes time.
4083 This would be a good case for using mmap.
4085 I have no idea how to handle linking PIC code into a file of a
4086 different format. It probably can't be done. */
4087 check_relocs = get_elf_backend_data (abfd)->check_relocs;
4088 if (! dynamic
4089 && is_elf_hash_table (hash_table)
4090 && hash_table->root.creator == abfd->xvec
4091 && check_relocs != NULL)
4093 asection *o;
4095 for (o = abfd->sections; o != NULL; o = o->next)
4097 Elf_Internal_Rela *internal_relocs;
4098 bfd_boolean ok;
4100 if ((o->flags & SEC_RELOC) == 0
4101 || o->reloc_count == 0
4102 || ((info->strip == strip_all || info->strip == strip_debugger)
4103 && (o->flags & SEC_DEBUGGING) != 0)
4104 || bfd_is_abs_section (o->output_section))
4105 continue;
4107 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4108 info->keep_memory);
4109 if (internal_relocs == NULL)
4110 goto error_return;
4112 ok = (*check_relocs) (abfd, info, o, internal_relocs);
4114 if (elf_section_data (o)->relocs != internal_relocs)
4115 free (internal_relocs);
4117 if (! ok)
4118 goto error_return;
4122 /* If this is a non-traditional link, try to optimize the handling
4123 of the .stab/.stabstr sections. */
4124 if (! dynamic
4125 && ! info->traditional_format
4126 && is_elf_hash_table (hash_table)
4127 && (info->strip != strip_all && info->strip != strip_debugger))
4129 asection *stabstr;
4131 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4132 if (stabstr != NULL)
4134 bfd_size_type string_offset = 0;
4135 asection *stab;
4137 for (stab = abfd->sections; stab; stab = stab->next)
4138 if (strncmp (".stab", stab->name, 5) == 0
4139 && (!stab->name[5] ||
4140 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4141 && (stab->flags & SEC_MERGE) == 0
4142 && !bfd_is_abs_section (stab->output_section))
4144 struct bfd_elf_section_data *secdata;
4146 secdata = elf_section_data (stab);
4147 if (! _bfd_link_section_stabs (abfd,
4148 &hash_table->stab_info,
4149 stab, stabstr,
4150 &secdata->sec_info,
4151 &string_offset))
4152 goto error_return;
4153 if (secdata->sec_info)
4154 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4159 if (is_elf_hash_table (hash_table))
4161 /* Add this bfd to the loaded list. */
4162 struct elf_link_loaded_list *n;
4164 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4165 if (n == NULL)
4166 goto error_return;
4167 n->abfd = abfd;
4168 n->next = hash_table->loaded;
4169 hash_table->loaded = n;
4172 return TRUE;
4174 error_free_vers:
4175 if (nondeflt_vers != NULL)
4176 free (nondeflt_vers);
4177 if (extversym != NULL)
4178 free (extversym);
4179 error_free_sym:
4180 if (isymbuf != NULL)
4181 free (isymbuf);
4182 error_return:
4183 return FALSE;
4186 /* Return the linker hash table entry of a symbol that might be
4187 satisfied by an archive symbol. Return -1 on error. */
4189 struct elf_link_hash_entry *
4190 _bfd_elf_archive_symbol_lookup (bfd *abfd,
4191 struct bfd_link_info *info,
4192 const char *name)
4194 struct elf_link_hash_entry *h;
4195 char *p, *copy;
4196 size_t len, first;
4198 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4199 if (h != NULL)
4200 return h;
4202 /* If this is a default version (the name contains @@), look up the
4203 symbol again with only one `@' as well as without the version.
4204 The effect is that references to the symbol with and without the
4205 version will be matched by the default symbol in the archive. */
4207 p = strchr (name, ELF_VER_CHR);
4208 if (p == NULL || p[1] != ELF_VER_CHR)
4209 return h;
4211 /* First check with only one `@'. */
4212 len = strlen (name);
4213 copy = bfd_alloc (abfd, len);
4214 if (copy == NULL)
4215 return (struct elf_link_hash_entry *) 0 - 1;
4217 first = p - name + 1;
4218 memcpy (copy, name, first);
4219 memcpy (copy + first, name + first + 1, len - first);
4221 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
4222 if (h == NULL)
4224 /* We also need to check references to the symbol without the
4225 version. */
4226 copy[first - 1] = '\0';
4227 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4228 FALSE, FALSE, FALSE);
4231 bfd_release (abfd, copy);
4232 return h;
4235 /* Add symbols from an ELF archive file to the linker hash table. We
4236 don't use _bfd_generic_link_add_archive_symbols because of a
4237 problem which arises on UnixWare. The UnixWare libc.so is an
4238 archive which includes an entry libc.so.1 which defines a bunch of
4239 symbols. The libc.so archive also includes a number of other
4240 object files, which also define symbols, some of which are the same
4241 as those defined in libc.so.1. Correct linking requires that we
4242 consider each object file in turn, and include it if it defines any
4243 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4244 this; it looks through the list of undefined symbols, and includes
4245 any object file which defines them. When this algorithm is used on
4246 UnixWare, it winds up pulling in libc.so.1 early and defining a
4247 bunch of symbols. This means that some of the other objects in the
4248 archive are not included in the link, which is incorrect since they
4249 precede libc.so.1 in the archive.
4251 Fortunately, ELF archive handling is simpler than that done by
4252 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4253 oddities. In ELF, if we find a symbol in the archive map, and the
4254 symbol is currently undefined, we know that we must pull in that
4255 object file.
4257 Unfortunately, we do have to make multiple passes over the symbol
4258 table until nothing further is resolved. */
4260 static bfd_boolean
4261 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
4263 symindex c;
4264 bfd_boolean *defined = NULL;
4265 bfd_boolean *included = NULL;
4266 carsym *symdefs;
4267 bfd_boolean loop;
4268 bfd_size_type amt;
4269 const struct elf_backend_data *bed;
4270 struct elf_link_hash_entry * (*archive_symbol_lookup)
4271 (bfd *, struct bfd_link_info *, const char *);
4273 if (! bfd_has_map (abfd))
4275 /* An empty archive is a special case. */
4276 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4277 return TRUE;
4278 bfd_set_error (bfd_error_no_armap);
4279 return FALSE;
4282 /* Keep track of all symbols we know to be already defined, and all
4283 files we know to be already included. This is to speed up the
4284 second and subsequent passes. */
4285 c = bfd_ardata (abfd)->symdef_count;
4286 if (c == 0)
4287 return TRUE;
4288 amt = c;
4289 amt *= sizeof (bfd_boolean);
4290 defined = bfd_zmalloc (amt);
4291 included = bfd_zmalloc (amt);
4292 if (defined == NULL || included == NULL)
4293 goto error_return;
4295 symdefs = bfd_ardata (abfd)->symdefs;
4296 bed = get_elf_backend_data (abfd);
4297 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
4301 file_ptr last;
4302 symindex i;
4303 carsym *symdef;
4304 carsym *symdefend;
4306 loop = FALSE;
4307 last = -1;
4309 symdef = symdefs;
4310 symdefend = symdef + c;
4311 for (i = 0; symdef < symdefend; symdef++, i++)
4313 struct elf_link_hash_entry *h;
4314 bfd *element;
4315 struct bfd_link_hash_entry *undefs_tail;
4316 symindex mark;
4318 if (defined[i] || included[i])
4319 continue;
4320 if (symdef->file_offset == last)
4322 included[i] = TRUE;
4323 continue;
4326 h = archive_symbol_lookup (abfd, info, symdef->name);
4327 if (h == (struct elf_link_hash_entry *) 0 - 1)
4328 goto error_return;
4330 if (h == NULL)
4331 continue;
4333 if (h->root.type == bfd_link_hash_common)
4335 /* We currently have a common symbol. The archive map contains
4336 a reference to this symbol, so we may want to include it. We
4337 only want to include it however, if this archive element
4338 contains a definition of the symbol, not just another common
4339 declaration of it.
4341 Unfortunately some archivers (including GNU ar) will put
4342 declarations of common symbols into their archive maps, as
4343 well as real definitions, so we cannot just go by the archive
4344 map alone. Instead we must read in the element's symbol
4345 table and check that to see what kind of symbol definition
4346 this is. */
4347 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
4348 continue;
4350 else if (h->root.type != bfd_link_hash_undefined)
4352 if (h->root.type != bfd_link_hash_undefweak)
4353 defined[i] = TRUE;
4354 continue;
4357 /* We need to include this archive member. */
4358 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4359 if (element == NULL)
4360 goto error_return;
4362 if (! bfd_check_format (element, bfd_object))
4363 goto error_return;
4365 /* Doublecheck that we have not included this object
4366 already--it should be impossible, but there may be
4367 something wrong with the archive. */
4368 if (element->archive_pass != 0)
4370 bfd_set_error (bfd_error_bad_value);
4371 goto error_return;
4373 element->archive_pass = 1;
4375 undefs_tail = info->hash->undefs_tail;
4377 if (! (*info->callbacks->add_archive_element) (info, element,
4378 symdef->name))
4379 goto error_return;
4380 if (! bfd_link_add_symbols (element, info))
4381 goto error_return;
4383 /* If there are any new undefined symbols, we need to make
4384 another pass through the archive in order to see whether
4385 they can be defined. FIXME: This isn't perfect, because
4386 common symbols wind up on undefs_tail and because an
4387 undefined symbol which is defined later on in this pass
4388 does not require another pass. This isn't a bug, but it
4389 does make the code less efficient than it could be. */
4390 if (undefs_tail != info->hash->undefs_tail)
4391 loop = TRUE;
4393 /* Look backward to mark all symbols from this object file
4394 which we have already seen in this pass. */
4395 mark = i;
4398 included[mark] = TRUE;
4399 if (mark == 0)
4400 break;
4401 --mark;
4403 while (symdefs[mark].file_offset == symdef->file_offset);
4405 /* We mark subsequent symbols from this object file as we go
4406 on through the loop. */
4407 last = symdef->file_offset;
4410 while (loop);
4412 free (defined);
4413 free (included);
4415 return TRUE;
4417 error_return:
4418 if (defined != NULL)
4419 free (defined);
4420 if (included != NULL)
4421 free (included);
4422 return FALSE;
4425 /* Given an ELF BFD, add symbols to the global hash table as
4426 appropriate. */
4428 bfd_boolean
4429 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
4431 switch (bfd_get_format (abfd))
4433 case bfd_object:
4434 return elf_link_add_object_symbols (abfd, info);
4435 case bfd_archive:
4436 return elf_link_add_archive_symbols (abfd, info);
4437 default:
4438 bfd_set_error (bfd_error_wrong_format);
4439 return FALSE;
4443 /* This function will be called though elf_link_hash_traverse to store
4444 all hash value of the exported symbols in an array. */
4446 static bfd_boolean
4447 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
4449 unsigned long **valuep = data;
4450 const char *name;
4451 char *p;
4452 unsigned long ha;
4453 char *alc = NULL;
4455 if (h->root.type == bfd_link_hash_warning)
4456 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4458 /* Ignore indirect symbols. These are added by the versioning code. */
4459 if (h->dynindx == -1)
4460 return TRUE;
4462 name = h->root.root.string;
4463 p = strchr (name, ELF_VER_CHR);
4464 if (p != NULL)
4466 alc = bfd_malloc (p - name + 1);
4467 memcpy (alc, name, p - name);
4468 alc[p - name] = '\0';
4469 name = alc;
4472 /* Compute the hash value. */
4473 ha = bfd_elf_hash (name);
4475 /* Store the found hash value in the array given as the argument. */
4476 *(*valuep)++ = ha;
4478 /* And store it in the struct so that we can put it in the hash table
4479 later. */
4480 h->elf_hash_value = ha;
4482 if (alc != NULL)
4483 free (alc);
4485 return TRUE;
4488 /* Array used to determine the number of hash table buckets to use
4489 based on the number of symbols there are. If there are fewer than
4490 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4491 fewer than 37 we use 17 buckets, and so forth. We never use more
4492 than 32771 buckets. */
4494 static const size_t elf_buckets[] =
4496 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4497 16411, 32771, 0
4500 /* Compute bucket count for hashing table. We do not use a static set
4501 of possible tables sizes anymore. Instead we determine for all
4502 possible reasonable sizes of the table the outcome (i.e., the
4503 number of collisions etc) and choose the best solution. The
4504 weighting functions are not too simple to allow the table to grow
4505 without bounds. Instead one of the weighting factors is the size.
4506 Therefore the result is always a good payoff between few collisions
4507 (= short chain lengths) and table size. */
4508 static size_t
4509 compute_bucket_count (struct bfd_link_info *info)
4511 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
4512 size_t best_size = 0;
4513 unsigned long int *hashcodes;
4514 unsigned long int *hashcodesp;
4515 unsigned long int i;
4516 bfd_size_type amt;
4518 /* Compute the hash values for all exported symbols. At the same
4519 time store the values in an array so that we could use them for
4520 optimizations. */
4521 amt = dynsymcount;
4522 amt *= sizeof (unsigned long int);
4523 hashcodes = bfd_malloc (amt);
4524 if (hashcodes == NULL)
4525 return 0;
4526 hashcodesp = hashcodes;
4528 /* Put all hash values in HASHCODES. */
4529 elf_link_hash_traverse (elf_hash_table (info),
4530 elf_collect_hash_codes, &hashcodesp);
4532 /* We have a problem here. The following code to optimize the table
4533 size requires an integer type with more the 32 bits. If
4534 BFD_HOST_U_64_BIT is set we know about such a type. */
4535 #ifdef BFD_HOST_U_64_BIT
4536 if (info->optimize)
4538 unsigned long int nsyms = hashcodesp - hashcodes;
4539 size_t minsize;
4540 size_t maxsize;
4541 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
4542 unsigned long int *counts ;
4543 bfd *dynobj = elf_hash_table (info)->dynobj;
4544 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
4546 /* Possible optimization parameters: if we have NSYMS symbols we say
4547 that the hashing table must at least have NSYMS/4 and at most
4548 2*NSYMS buckets. */
4549 minsize = nsyms / 4;
4550 if (minsize == 0)
4551 minsize = 1;
4552 best_size = maxsize = nsyms * 2;
4554 /* Create array where we count the collisions in. We must use bfd_malloc
4555 since the size could be large. */
4556 amt = maxsize;
4557 amt *= sizeof (unsigned long int);
4558 counts = bfd_malloc (amt);
4559 if (counts == NULL)
4561 free (hashcodes);
4562 return 0;
4565 /* Compute the "optimal" size for the hash table. The criteria is a
4566 minimal chain length. The minor criteria is (of course) the size
4567 of the table. */
4568 for (i = minsize; i < maxsize; ++i)
4570 /* Walk through the array of hashcodes and count the collisions. */
4571 BFD_HOST_U_64_BIT max;
4572 unsigned long int j;
4573 unsigned long int fact;
4575 memset (counts, '\0', i * sizeof (unsigned long int));
4577 /* Determine how often each hash bucket is used. */
4578 for (j = 0; j < nsyms; ++j)
4579 ++counts[hashcodes[j] % i];
4581 /* For the weight function we need some information about the
4582 pagesize on the target. This is information need not be 100%
4583 accurate. Since this information is not available (so far) we
4584 define it here to a reasonable default value. If it is crucial
4585 to have a better value some day simply define this value. */
4586 # ifndef BFD_TARGET_PAGESIZE
4587 # define BFD_TARGET_PAGESIZE (4096)
4588 # endif
4590 /* We in any case need 2 + NSYMS entries for the size values and
4591 the chains. */
4592 max = (2 + nsyms) * (bed->s->arch_size / 8);
4594 # if 1
4595 /* Variant 1: optimize for short chains. We add the squares
4596 of all the chain lengths (which favors many small chain
4597 over a few long chains). */
4598 for (j = 0; j < i; ++j)
4599 max += counts[j] * counts[j];
4601 /* This adds penalties for the overall size of the table. */
4602 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4603 max *= fact * fact;
4604 # else
4605 /* Variant 2: Optimize a lot more for small table. Here we
4606 also add squares of the size but we also add penalties for
4607 empty slots (the +1 term). */
4608 for (j = 0; j < i; ++j)
4609 max += (1 + counts[j]) * (1 + counts[j]);
4611 /* The overall size of the table is considered, but not as
4612 strong as in variant 1, where it is squared. */
4613 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4614 max *= fact;
4615 # endif
4617 /* Compare with current best results. */
4618 if (max < best_chlen)
4620 best_chlen = max;
4621 best_size = i;
4625 free (counts);
4627 else
4628 #endif /* defined (BFD_HOST_U_64_BIT) */
4630 /* This is the fallback solution if no 64bit type is available or if we
4631 are not supposed to spend much time on optimizations. We select the
4632 bucket count using a fixed set of numbers. */
4633 for (i = 0; elf_buckets[i] != 0; i++)
4635 best_size = elf_buckets[i];
4636 if (dynsymcount < elf_buckets[i + 1])
4637 break;
4641 /* Free the arrays we needed. */
4642 free (hashcodes);
4644 return best_size;
4647 /* Set up the sizes and contents of the ELF dynamic sections. This is
4648 called by the ELF linker emulation before_allocation routine. We
4649 must set the sizes of the sections before the linker sets the
4650 addresses of the various sections. */
4652 bfd_boolean
4653 bfd_elf_size_dynamic_sections (bfd *output_bfd,
4654 const char *soname,
4655 const char *rpath,
4656 const char *filter_shlib,
4657 const char * const *auxiliary_filters,
4658 struct bfd_link_info *info,
4659 asection **sinterpptr,
4660 struct bfd_elf_version_tree *verdefs)
4662 bfd_size_type soname_indx;
4663 bfd *dynobj;
4664 const struct elf_backend_data *bed;
4665 struct elf_assign_sym_version_info asvinfo;
4667 *sinterpptr = NULL;
4669 soname_indx = (bfd_size_type) -1;
4671 if (!is_elf_hash_table (info->hash))
4672 return TRUE;
4674 elf_tdata (output_bfd)->relro = info->relro;
4675 if (info->execstack)
4676 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
4677 else if (info->noexecstack)
4678 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
4679 else
4681 bfd *inputobj;
4682 asection *notesec = NULL;
4683 int exec = 0;
4685 for (inputobj = info->input_bfds;
4686 inputobj;
4687 inputobj = inputobj->link_next)
4689 asection *s;
4691 if (inputobj->flags & DYNAMIC)
4692 continue;
4693 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
4694 if (s)
4696 if (s->flags & SEC_CODE)
4697 exec = PF_X;
4698 notesec = s;
4700 else
4701 exec = PF_X;
4703 if (notesec)
4705 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
4706 if (exec && info->relocatable
4707 && notesec->output_section != bfd_abs_section_ptr)
4708 notesec->output_section->flags |= SEC_CODE;
4712 /* Any syms created from now on start with -1 in
4713 got.refcount/offset and plt.refcount/offset. */
4714 elf_hash_table (info)->init_refcount = elf_hash_table (info)->init_offset;
4716 /* The backend may have to create some sections regardless of whether
4717 we're dynamic or not. */
4718 bed = get_elf_backend_data (output_bfd);
4719 if (bed->elf_backend_always_size_sections
4720 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
4721 return FALSE;
4723 dynobj = elf_hash_table (info)->dynobj;
4725 /* If there were no dynamic objects in the link, there is nothing to
4726 do here. */
4727 if (dynobj == NULL)
4728 return TRUE;
4730 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
4731 return FALSE;
4733 if (elf_hash_table (info)->dynamic_sections_created)
4735 struct elf_info_failed eif;
4736 struct elf_link_hash_entry *h;
4737 asection *dynstr;
4738 struct bfd_elf_version_tree *t;
4739 struct bfd_elf_version_expr *d;
4740 bfd_boolean all_defined;
4742 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
4743 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
4745 if (soname != NULL)
4747 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
4748 soname, TRUE);
4749 if (soname_indx == (bfd_size_type) -1
4750 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
4751 return FALSE;
4754 if (info->symbolic)
4756 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
4757 return FALSE;
4758 info->flags |= DF_SYMBOLIC;
4761 if (rpath != NULL)
4763 bfd_size_type indx;
4765 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
4766 TRUE);
4767 if (indx == (bfd_size_type) -1
4768 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
4769 return FALSE;
4771 if (info->new_dtags)
4773 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
4774 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
4775 return FALSE;
4779 if (filter_shlib != NULL)
4781 bfd_size_type indx;
4783 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
4784 filter_shlib, TRUE);
4785 if (indx == (bfd_size_type) -1
4786 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
4787 return FALSE;
4790 if (auxiliary_filters != NULL)
4792 const char * const *p;
4794 for (p = auxiliary_filters; *p != NULL; p++)
4796 bfd_size_type indx;
4798 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
4799 *p, TRUE);
4800 if (indx == (bfd_size_type) -1
4801 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
4802 return FALSE;
4806 eif.info = info;
4807 eif.verdefs = verdefs;
4808 eif.failed = FALSE;
4810 /* If we are supposed to export all symbols into the dynamic symbol
4811 table (this is not the normal case), then do so. */
4812 if (info->export_dynamic)
4814 elf_link_hash_traverse (elf_hash_table (info),
4815 _bfd_elf_export_symbol,
4816 &eif);
4817 if (eif.failed)
4818 return FALSE;
4821 /* Make all global versions with definition. */
4822 for (t = verdefs; t != NULL; t = t->next)
4823 for (d = t->globals.list; d != NULL; d = d->next)
4824 if (!d->symver && d->symbol)
4826 const char *verstr, *name;
4827 size_t namelen, verlen, newlen;
4828 char *newname, *p;
4829 struct elf_link_hash_entry *newh;
4831 name = d->symbol;
4832 namelen = strlen (name);
4833 verstr = t->name;
4834 verlen = strlen (verstr);
4835 newlen = namelen + verlen + 3;
4837 newname = bfd_malloc (newlen);
4838 if (newname == NULL)
4839 return FALSE;
4840 memcpy (newname, name, namelen);
4842 /* Check the hidden versioned definition. */
4843 p = newname + namelen;
4844 *p++ = ELF_VER_CHR;
4845 memcpy (p, verstr, verlen + 1);
4846 newh = elf_link_hash_lookup (elf_hash_table (info),
4847 newname, FALSE, FALSE,
4848 FALSE);
4849 if (newh == NULL
4850 || (newh->root.type != bfd_link_hash_defined
4851 && newh->root.type != bfd_link_hash_defweak))
4853 /* Check the default versioned definition. */
4854 *p++ = ELF_VER_CHR;
4855 memcpy (p, verstr, verlen + 1);
4856 newh = elf_link_hash_lookup (elf_hash_table (info),
4857 newname, FALSE, FALSE,
4858 FALSE);
4860 free (newname);
4862 /* Mark this version if there is a definition and it is
4863 not defined in a shared object. */
4864 if (newh != NULL
4865 && ((newh->elf_link_hash_flags
4866 & ELF_LINK_HASH_DEF_DYNAMIC) == 0)
4867 && (newh->root.type == bfd_link_hash_defined
4868 || newh->root.type == bfd_link_hash_defweak))
4869 d->symver = 1;
4872 /* Attach all the symbols to their version information. */
4873 asvinfo.output_bfd = output_bfd;
4874 asvinfo.info = info;
4875 asvinfo.verdefs = verdefs;
4876 asvinfo.failed = FALSE;
4878 elf_link_hash_traverse (elf_hash_table (info),
4879 _bfd_elf_link_assign_sym_version,
4880 &asvinfo);
4881 if (asvinfo.failed)
4882 return FALSE;
4884 if (!info->allow_undefined_version)
4886 /* Check if all global versions have a definition. */
4887 all_defined = TRUE;
4888 for (t = verdefs; t != NULL; t = t->next)
4889 for (d = t->globals.list; d != NULL; d = d->next)
4890 if (!d->symver && !d->script)
4892 (*_bfd_error_handler)
4893 (_("%s: undefined version: %s"),
4894 d->pattern, t->name);
4895 all_defined = FALSE;
4898 if (!all_defined)
4900 bfd_set_error (bfd_error_bad_value);
4901 return FALSE;
4905 /* Find all symbols which were defined in a dynamic object and make
4906 the backend pick a reasonable value for them. */
4907 elf_link_hash_traverse (elf_hash_table (info),
4908 _bfd_elf_adjust_dynamic_symbol,
4909 &eif);
4910 if (eif.failed)
4911 return FALSE;
4913 /* Add some entries to the .dynamic section. We fill in some of the
4914 values later, in elf_bfd_final_link, but we must add the entries
4915 now so that we know the final size of the .dynamic section. */
4917 /* If there are initialization and/or finalization functions to
4918 call then add the corresponding DT_INIT/DT_FINI entries. */
4919 h = (info->init_function
4920 ? elf_link_hash_lookup (elf_hash_table (info),
4921 info->init_function, FALSE,
4922 FALSE, FALSE)
4923 : NULL);
4924 if (h != NULL
4925 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
4926 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
4928 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
4929 return FALSE;
4931 h = (info->fini_function
4932 ? elf_link_hash_lookup (elf_hash_table (info),
4933 info->fini_function, FALSE,
4934 FALSE, FALSE)
4935 : NULL);
4936 if (h != NULL
4937 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
4938 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
4940 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
4941 return FALSE;
4944 if (bfd_get_section_by_name (output_bfd, ".preinit_array") != NULL)
4946 /* DT_PREINIT_ARRAY is not allowed in shared library. */
4947 if (! info->executable)
4949 bfd *sub;
4950 asection *o;
4952 for (sub = info->input_bfds; sub != NULL;
4953 sub = sub->link_next)
4954 for (o = sub->sections; o != NULL; o = o->next)
4955 if (elf_section_data (o)->this_hdr.sh_type
4956 == SHT_PREINIT_ARRAY)
4958 (*_bfd_error_handler)
4959 (_("%B: .preinit_array section is not allowed in DSO"),
4960 sub);
4961 break;
4964 bfd_set_error (bfd_error_nonrepresentable_section);
4965 return FALSE;
4968 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
4969 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
4970 return FALSE;
4972 if (bfd_get_section_by_name (output_bfd, ".init_array") != NULL)
4974 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
4975 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
4976 return FALSE;
4978 if (bfd_get_section_by_name (output_bfd, ".fini_array") != NULL)
4980 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
4981 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
4982 return FALSE;
4985 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
4986 /* If .dynstr is excluded from the link, we don't want any of
4987 these tags. Strictly, we should be checking each section
4988 individually; This quick check covers for the case where
4989 someone does a /DISCARD/ : { *(*) }. */
4990 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
4992 bfd_size_type strsize;
4994 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
4995 if (!_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)
4996 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
4997 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
4998 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
4999 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5000 bed->s->sizeof_sym))
5001 return FALSE;
5005 /* The backend must work out the sizes of all the other dynamic
5006 sections. */
5007 if (bed->elf_backend_size_dynamic_sections
5008 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5009 return FALSE;
5011 if (elf_hash_table (info)->dynamic_sections_created)
5013 bfd_size_type dynsymcount;
5014 asection *s;
5015 size_t bucketcount = 0;
5016 size_t hash_entry_size;
5017 unsigned int dtagcount;
5019 /* Set up the version definition section. */
5020 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5021 BFD_ASSERT (s != NULL);
5023 /* We may have created additional version definitions if we are
5024 just linking a regular application. */
5025 verdefs = asvinfo.verdefs;
5027 /* Skip anonymous version tag. */
5028 if (verdefs != NULL && verdefs->vernum == 0)
5029 verdefs = verdefs->next;
5031 if (verdefs == NULL)
5032 _bfd_strip_section_from_output (info, s);
5033 else
5035 unsigned int cdefs;
5036 bfd_size_type size;
5037 struct bfd_elf_version_tree *t;
5038 bfd_byte *p;
5039 Elf_Internal_Verdef def;
5040 Elf_Internal_Verdaux defaux;
5042 cdefs = 0;
5043 size = 0;
5045 /* Make space for the base version. */
5046 size += sizeof (Elf_External_Verdef);
5047 size += sizeof (Elf_External_Verdaux);
5048 ++cdefs;
5050 for (t = verdefs; t != NULL; t = t->next)
5052 struct bfd_elf_version_deps *n;
5054 size += sizeof (Elf_External_Verdef);
5055 size += sizeof (Elf_External_Verdaux);
5056 ++cdefs;
5058 for (n = t->deps; n != NULL; n = n->next)
5059 size += sizeof (Elf_External_Verdaux);
5062 s->size = size;
5063 s->contents = bfd_alloc (output_bfd, s->size);
5064 if (s->contents == NULL && s->size != 0)
5065 return FALSE;
5067 /* Fill in the version definition section. */
5069 p = s->contents;
5071 def.vd_version = VER_DEF_CURRENT;
5072 def.vd_flags = VER_FLG_BASE;
5073 def.vd_ndx = 1;
5074 def.vd_cnt = 1;
5075 def.vd_aux = sizeof (Elf_External_Verdef);
5076 def.vd_next = (sizeof (Elf_External_Verdef)
5077 + sizeof (Elf_External_Verdaux));
5079 if (soname_indx != (bfd_size_type) -1)
5081 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5082 soname_indx);
5083 def.vd_hash = bfd_elf_hash (soname);
5084 defaux.vda_name = soname_indx;
5086 else
5088 const char *name;
5089 bfd_size_type indx;
5091 name = basename (output_bfd->filename);
5092 def.vd_hash = bfd_elf_hash (name);
5093 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5094 name, FALSE);
5095 if (indx == (bfd_size_type) -1)
5096 return FALSE;
5097 defaux.vda_name = indx;
5099 defaux.vda_next = 0;
5101 _bfd_elf_swap_verdef_out (output_bfd, &def,
5102 (Elf_External_Verdef *) p);
5103 p += sizeof (Elf_External_Verdef);
5104 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5105 (Elf_External_Verdaux *) p);
5106 p += sizeof (Elf_External_Verdaux);
5108 for (t = verdefs; t != NULL; t = t->next)
5110 unsigned int cdeps;
5111 struct bfd_elf_version_deps *n;
5112 struct elf_link_hash_entry *h;
5113 struct bfd_link_hash_entry *bh;
5115 cdeps = 0;
5116 for (n = t->deps; n != NULL; n = n->next)
5117 ++cdeps;
5119 /* Add a symbol representing this version. */
5120 bh = NULL;
5121 if (! (_bfd_generic_link_add_one_symbol
5122 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5123 0, NULL, FALSE,
5124 get_elf_backend_data (dynobj)->collect, &bh)))
5125 return FALSE;
5126 h = (struct elf_link_hash_entry *) bh;
5127 h->elf_link_hash_flags &= ~ ELF_LINK_NON_ELF;
5128 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5129 h->type = STT_OBJECT;
5130 h->verinfo.vertree = t;
5132 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5133 return FALSE;
5135 def.vd_version = VER_DEF_CURRENT;
5136 def.vd_flags = 0;
5137 if (t->globals.list == NULL
5138 && t->locals.list == NULL
5139 && ! t->used)
5140 def.vd_flags |= VER_FLG_WEAK;
5141 def.vd_ndx = t->vernum + 1;
5142 def.vd_cnt = cdeps + 1;
5143 def.vd_hash = bfd_elf_hash (t->name);
5144 def.vd_aux = sizeof (Elf_External_Verdef);
5145 def.vd_next = 0;
5146 if (t->next != NULL)
5147 def.vd_next = (sizeof (Elf_External_Verdef)
5148 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5150 _bfd_elf_swap_verdef_out (output_bfd, &def,
5151 (Elf_External_Verdef *) p);
5152 p += sizeof (Elf_External_Verdef);
5154 defaux.vda_name = h->dynstr_index;
5155 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5156 h->dynstr_index);
5157 defaux.vda_next = 0;
5158 if (t->deps != NULL)
5159 defaux.vda_next = sizeof (Elf_External_Verdaux);
5160 t->name_indx = defaux.vda_name;
5162 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5163 (Elf_External_Verdaux *) p);
5164 p += sizeof (Elf_External_Verdaux);
5166 for (n = t->deps; n != NULL; n = n->next)
5168 if (n->version_needed == NULL)
5170 /* This can happen if there was an error in the
5171 version script. */
5172 defaux.vda_name = 0;
5174 else
5176 defaux.vda_name = n->version_needed->name_indx;
5177 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5178 defaux.vda_name);
5180 if (n->next == NULL)
5181 defaux.vda_next = 0;
5182 else
5183 defaux.vda_next = sizeof (Elf_External_Verdaux);
5185 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5186 (Elf_External_Verdaux *) p);
5187 p += sizeof (Elf_External_Verdaux);
5191 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
5192 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
5193 return FALSE;
5195 elf_tdata (output_bfd)->cverdefs = cdefs;
5198 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
5200 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
5201 return FALSE;
5203 else if (info->flags & DF_BIND_NOW)
5205 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
5206 return FALSE;
5209 if (info->flags_1)
5211 if (info->executable)
5212 info->flags_1 &= ~ (DF_1_INITFIRST
5213 | DF_1_NODELETE
5214 | DF_1_NOOPEN);
5215 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
5216 return FALSE;
5219 /* Work out the size of the version reference section. */
5221 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
5222 BFD_ASSERT (s != NULL);
5224 struct elf_find_verdep_info sinfo;
5226 sinfo.output_bfd = output_bfd;
5227 sinfo.info = info;
5228 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
5229 if (sinfo.vers == 0)
5230 sinfo.vers = 1;
5231 sinfo.failed = FALSE;
5233 elf_link_hash_traverse (elf_hash_table (info),
5234 _bfd_elf_link_find_version_dependencies,
5235 &sinfo);
5237 if (elf_tdata (output_bfd)->verref == NULL)
5238 _bfd_strip_section_from_output (info, s);
5239 else
5241 Elf_Internal_Verneed *t;
5242 unsigned int size;
5243 unsigned int crefs;
5244 bfd_byte *p;
5246 /* Build the version definition section. */
5247 size = 0;
5248 crefs = 0;
5249 for (t = elf_tdata (output_bfd)->verref;
5250 t != NULL;
5251 t = t->vn_nextref)
5253 Elf_Internal_Vernaux *a;
5255 size += sizeof (Elf_External_Verneed);
5256 ++crefs;
5257 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5258 size += sizeof (Elf_External_Vernaux);
5261 s->size = size;
5262 s->contents = bfd_alloc (output_bfd, s->size);
5263 if (s->contents == NULL)
5264 return FALSE;
5266 p = s->contents;
5267 for (t = elf_tdata (output_bfd)->verref;
5268 t != NULL;
5269 t = t->vn_nextref)
5271 unsigned int caux;
5272 Elf_Internal_Vernaux *a;
5273 bfd_size_type indx;
5275 caux = 0;
5276 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5277 ++caux;
5279 t->vn_version = VER_NEED_CURRENT;
5280 t->vn_cnt = caux;
5281 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5282 elf_dt_name (t->vn_bfd) != NULL
5283 ? elf_dt_name (t->vn_bfd)
5284 : basename (t->vn_bfd->filename),
5285 FALSE);
5286 if (indx == (bfd_size_type) -1)
5287 return FALSE;
5288 t->vn_file = indx;
5289 t->vn_aux = sizeof (Elf_External_Verneed);
5290 if (t->vn_nextref == NULL)
5291 t->vn_next = 0;
5292 else
5293 t->vn_next = (sizeof (Elf_External_Verneed)
5294 + caux * sizeof (Elf_External_Vernaux));
5296 _bfd_elf_swap_verneed_out (output_bfd, t,
5297 (Elf_External_Verneed *) p);
5298 p += sizeof (Elf_External_Verneed);
5300 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5302 a->vna_hash = bfd_elf_hash (a->vna_nodename);
5303 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5304 a->vna_nodename, FALSE);
5305 if (indx == (bfd_size_type) -1)
5306 return FALSE;
5307 a->vna_name = indx;
5308 if (a->vna_nextptr == NULL)
5309 a->vna_next = 0;
5310 else
5311 a->vna_next = sizeof (Elf_External_Vernaux);
5313 _bfd_elf_swap_vernaux_out (output_bfd, a,
5314 (Elf_External_Vernaux *) p);
5315 p += sizeof (Elf_External_Vernaux);
5319 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
5320 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
5321 return FALSE;
5323 elf_tdata (output_bfd)->cverrefs = crefs;
5327 /* Assign dynsym indicies. In a shared library we generate a
5328 section symbol for each output section, which come first.
5329 Next come all of the back-end allocated local dynamic syms,
5330 followed by the rest of the global symbols. */
5332 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
5334 /* Work out the size of the symbol version section. */
5335 s = bfd_get_section_by_name (dynobj, ".gnu.version");
5336 BFD_ASSERT (s != NULL);
5337 if (dynsymcount == 0
5338 || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL))
5340 _bfd_strip_section_from_output (info, s);
5341 /* The DYNSYMCOUNT might have changed if we were going to
5342 output a dynamic symbol table entry for S. */
5343 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
5345 else
5347 s->size = dynsymcount * sizeof (Elf_External_Versym);
5348 s->contents = bfd_zalloc (output_bfd, s->size);
5349 if (s->contents == NULL)
5350 return FALSE;
5352 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
5353 return FALSE;
5356 /* Set the size of the .dynsym and .hash sections. We counted
5357 the number of dynamic symbols in elf_link_add_object_symbols.
5358 We will build the contents of .dynsym and .hash when we build
5359 the final symbol table, because until then we do not know the
5360 correct value to give the symbols. We built the .dynstr
5361 section as we went along in elf_link_add_object_symbols. */
5362 s = bfd_get_section_by_name (dynobj, ".dynsym");
5363 BFD_ASSERT (s != NULL);
5364 s->size = dynsymcount * bed->s->sizeof_sym;
5365 s->contents = bfd_alloc (output_bfd, s->size);
5366 if (s->contents == NULL && s->size != 0)
5367 return FALSE;
5369 if (dynsymcount != 0)
5371 Elf_Internal_Sym isym;
5373 /* The first entry in .dynsym is a dummy symbol. */
5374 isym.st_value = 0;
5375 isym.st_size = 0;
5376 isym.st_name = 0;
5377 isym.st_info = 0;
5378 isym.st_other = 0;
5379 isym.st_shndx = 0;
5380 bed->s->swap_symbol_out (output_bfd, &isym, s->contents, 0);
5383 /* Compute the size of the hashing table. As a side effect this
5384 computes the hash values for all the names we export. */
5385 bucketcount = compute_bucket_count (info);
5387 s = bfd_get_section_by_name (dynobj, ".hash");
5388 BFD_ASSERT (s != NULL);
5389 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
5390 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
5391 s->contents = bfd_zalloc (output_bfd, s->size);
5392 if (s->contents == NULL)
5393 return FALSE;
5395 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
5396 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
5397 s->contents + hash_entry_size);
5399 elf_hash_table (info)->bucketcount = bucketcount;
5401 s = bfd_get_section_by_name (dynobj, ".dynstr");
5402 BFD_ASSERT (s != NULL);
5404 elf_finalize_dynstr (output_bfd, info);
5406 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5408 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
5409 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
5410 return FALSE;
5413 return TRUE;
5416 /* Final phase of ELF linker. */
5418 /* A structure we use to avoid passing large numbers of arguments. */
5420 struct elf_final_link_info
5422 /* General link information. */
5423 struct bfd_link_info *info;
5424 /* Output BFD. */
5425 bfd *output_bfd;
5426 /* Symbol string table. */
5427 struct bfd_strtab_hash *symstrtab;
5428 /* .dynsym section. */
5429 asection *dynsym_sec;
5430 /* .hash section. */
5431 asection *hash_sec;
5432 /* symbol version section (.gnu.version). */
5433 asection *symver_sec;
5434 /* Buffer large enough to hold contents of any section. */
5435 bfd_byte *contents;
5436 /* Buffer large enough to hold external relocs of any section. */
5437 void *external_relocs;
5438 /* Buffer large enough to hold internal relocs of any section. */
5439 Elf_Internal_Rela *internal_relocs;
5440 /* Buffer large enough to hold external local symbols of any input
5441 BFD. */
5442 bfd_byte *external_syms;
5443 /* And a buffer for symbol section indices. */
5444 Elf_External_Sym_Shndx *locsym_shndx;
5445 /* Buffer large enough to hold internal local symbols of any input
5446 BFD. */
5447 Elf_Internal_Sym *internal_syms;
5448 /* Array large enough to hold a symbol index for each local symbol
5449 of any input BFD. */
5450 long *indices;
5451 /* Array large enough to hold a section pointer for each local
5452 symbol of any input BFD. */
5453 asection **sections;
5454 /* Buffer to hold swapped out symbols. */
5455 bfd_byte *symbuf;
5456 /* And one for symbol section indices. */
5457 Elf_External_Sym_Shndx *symshndxbuf;
5458 /* Number of swapped out symbols in buffer. */
5459 size_t symbuf_count;
5460 /* Number of symbols which fit in symbuf. */
5461 size_t symbuf_size;
5462 /* And same for symshndxbuf. */
5463 size_t shndxbuf_size;
5466 /* This struct is used to pass information to elf_link_output_extsym. */
5468 struct elf_outext_info
5470 bfd_boolean failed;
5471 bfd_boolean localsyms;
5472 struct elf_final_link_info *finfo;
5475 /* When performing a relocatable link, the input relocations are
5476 preserved. But, if they reference global symbols, the indices
5477 referenced must be updated. Update all the relocations in
5478 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5480 static void
5481 elf_link_adjust_relocs (bfd *abfd,
5482 Elf_Internal_Shdr *rel_hdr,
5483 unsigned int count,
5484 struct elf_link_hash_entry **rel_hash)
5486 unsigned int i;
5487 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5488 bfd_byte *erela;
5489 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5490 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5491 bfd_vma r_type_mask;
5492 int r_sym_shift;
5494 if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
5496 swap_in = bed->s->swap_reloc_in;
5497 swap_out = bed->s->swap_reloc_out;
5499 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
5501 swap_in = bed->s->swap_reloca_in;
5502 swap_out = bed->s->swap_reloca_out;
5504 else
5505 abort ();
5507 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
5508 abort ();
5510 if (bed->s->arch_size == 32)
5512 r_type_mask = 0xff;
5513 r_sym_shift = 8;
5515 else
5517 r_type_mask = 0xffffffff;
5518 r_sym_shift = 32;
5521 erela = rel_hdr->contents;
5522 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
5524 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
5525 unsigned int j;
5527 if (*rel_hash == NULL)
5528 continue;
5530 BFD_ASSERT ((*rel_hash)->indx >= 0);
5532 (*swap_in) (abfd, erela, irela);
5533 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
5534 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
5535 | (irela[j].r_info & r_type_mask));
5536 (*swap_out) (abfd, irela, erela);
5540 struct elf_link_sort_rela
5542 union {
5543 bfd_vma offset;
5544 bfd_vma sym_mask;
5545 } u;
5546 enum elf_reloc_type_class type;
5547 /* We use this as an array of size int_rels_per_ext_rel. */
5548 Elf_Internal_Rela rela[1];
5551 static int
5552 elf_link_sort_cmp1 (const void *A, const void *B)
5554 const struct elf_link_sort_rela *a = A;
5555 const struct elf_link_sort_rela *b = B;
5556 int relativea, relativeb;
5558 relativea = a->type == reloc_class_relative;
5559 relativeb = b->type == reloc_class_relative;
5561 if (relativea < relativeb)
5562 return 1;
5563 if (relativea > relativeb)
5564 return -1;
5565 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
5566 return -1;
5567 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
5568 return 1;
5569 if (a->rela->r_offset < b->rela->r_offset)
5570 return -1;
5571 if (a->rela->r_offset > b->rela->r_offset)
5572 return 1;
5573 return 0;
5576 static int
5577 elf_link_sort_cmp2 (const void *A, const void *B)
5579 const struct elf_link_sort_rela *a = A;
5580 const struct elf_link_sort_rela *b = B;
5581 int copya, copyb;
5583 if (a->u.offset < b->u.offset)
5584 return -1;
5585 if (a->u.offset > b->u.offset)
5586 return 1;
5587 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
5588 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
5589 if (copya < copyb)
5590 return -1;
5591 if (copya > copyb)
5592 return 1;
5593 if (a->rela->r_offset < b->rela->r_offset)
5594 return -1;
5595 if (a->rela->r_offset > b->rela->r_offset)
5596 return 1;
5597 return 0;
5600 static size_t
5601 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
5603 asection *reldyn;
5604 bfd_size_type count, size;
5605 size_t i, ret, sort_elt, ext_size;
5606 bfd_byte *sort, *s_non_relative, *p;
5607 struct elf_link_sort_rela *sq;
5608 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5609 int i2e = bed->s->int_rels_per_ext_rel;
5610 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5611 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5612 struct bfd_link_order *lo;
5613 bfd_vma r_sym_mask;
5615 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
5616 if (reldyn == NULL || reldyn->size == 0)
5618 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
5619 if (reldyn == NULL || reldyn->size == 0)
5620 return 0;
5621 ext_size = bed->s->sizeof_rel;
5622 swap_in = bed->s->swap_reloc_in;
5623 swap_out = bed->s->swap_reloc_out;
5625 else
5627 ext_size = bed->s->sizeof_rela;
5628 swap_in = bed->s->swap_reloca_in;
5629 swap_out = bed->s->swap_reloca_out;
5631 count = reldyn->size / ext_size;
5633 size = 0;
5634 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
5635 if (lo->type == bfd_indirect_link_order)
5637 asection *o = lo->u.indirect.section;
5638 size += o->size;
5641 if (size != reldyn->size)
5642 return 0;
5644 sort_elt = (sizeof (struct elf_link_sort_rela)
5645 + (i2e - 1) * sizeof (Elf_Internal_Rela));
5646 sort = bfd_zmalloc (sort_elt * count);
5647 if (sort == NULL)
5649 (*info->callbacks->warning)
5650 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
5651 return 0;
5654 if (bed->s->arch_size == 32)
5655 r_sym_mask = ~(bfd_vma) 0xff;
5656 else
5657 r_sym_mask = ~(bfd_vma) 0xffffffff;
5659 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
5660 if (lo->type == bfd_indirect_link_order)
5662 bfd_byte *erel, *erelend;
5663 asection *o = lo->u.indirect.section;
5665 erel = o->contents;
5666 erelend = o->contents + o->size;
5667 p = sort + o->output_offset / ext_size * sort_elt;
5668 while (erel < erelend)
5670 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
5671 (*swap_in) (abfd, erel, s->rela);
5672 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
5673 s->u.sym_mask = r_sym_mask;
5674 p += sort_elt;
5675 erel += ext_size;
5679 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
5681 for (i = 0, p = sort; i < count; i++, p += sort_elt)
5683 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
5684 if (s->type != reloc_class_relative)
5685 break;
5687 ret = i;
5688 s_non_relative = p;
5690 sq = (struct elf_link_sort_rela *) s_non_relative;
5691 for (; i < count; i++, p += sort_elt)
5693 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
5694 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
5695 sq = sp;
5696 sp->u.offset = sq->rela->r_offset;
5699 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
5701 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
5702 if (lo->type == bfd_indirect_link_order)
5704 bfd_byte *erel, *erelend;
5705 asection *o = lo->u.indirect.section;
5707 erel = o->contents;
5708 erelend = o->contents + o->size;
5709 p = sort + o->output_offset / ext_size * sort_elt;
5710 while (erel < erelend)
5712 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
5713 (*swap_out) (abfd, s->rela, erel);
5714 p += sort_elt;
5715 erel += ext_size;
5719 free (sort);
5720 *psec = reldyn;
5721 return ret;
5724 /* Flush the output symbols to the file. */
5726 static bfd_boolean
5727 elf_link_flush_output_syms (struct elf_final_link_info *finfo,
5728 const struct elf_backend_data *bed)
5730 if (finfo->symbuf_count > 0)
5732 Elf_Internal_Shdr *hdr;
5733 file_ptr pos;
5734 bfd_size_type amt;
5736 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
5737 pos = hdr->sh_offset + hdr->sh_size;
5738 amt = finfo->symbuf_count * bed->s->sizeof_sym;
5739 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
5740 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
5741 return FALSE;
5743 hdr->sh_size += amt;
5744 finfo->symbuf_count = 0;
5747 return TRUE;
5750 /* Add a symbol to the output symbol table. */
5752 static bfd_boolean
5753 elf_link_output_sym (struct elf_final_link_info *finfo,
5754 const char *name,
5755 Elf_Internal_Sym *elfsym,
5756 asection *input_sec,
5757 struct elf_link_hash_entry *h)
5759 bfd_byte *dest;
5760 Elf_External_Sym_Shndx *destshndx;
5761 bfd_boolean (*output_symbol_hook)
5762 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
5763 struct elf_link_hash_entry *);
5764 const struct elf_backend_data *bed;
5766 bed = get_elf_backend_data (finfo->output_bfd);
5767 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
5768 if (output_symbol_hook != NULL)
5770 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
5771 return FALSE;
5774 if (name == NULL || *name == '\0')
5775 elfsym->st_name = 0;
5776 else if (input_sec->flags & SEC_EXCLUDE)
5777 elfsym->st_name = 0;
5778 else
5780 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
5781 name, TRUE, FALSE);
5782 if (elfsym->st_name == (unsigned long) -1)
5783 return FALSE;
5786 if (finfo->symbuf_count >= finfo->symbuf_size)
5788 if (! elf_link_flush_output_syms (finfo, bed))
5789 return FALSE;
5792 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
5793 destshndx = finfo->symshndxbuf;
5794 if (destshndx != NULL)
5796 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
5798 bfd_size_type amt;
5800 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
5801 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
5802 if (destshndx == NULL)
5803 return FALSE;
5804 memset ((char *) destshndx + amt, 0, amt);
5805 finfo->shndxbuf_size *= 2;
5807 destshndx += bfd_get_symcount (finfo->output_bfd);
5810 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
5811 finfo->symbuf_count += 1;
5812 bfd_get_symcount (finfo->output_bfd) += 1;
5814 return TRUE;
5817 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
5818 allowing an unsatisfied unversioned symbol in the DSO to match a
5819 versioned symbol that would normally require an explicit version.
5820 We also handle the case that a DSO references a hidden symbol
5821 which may be satisfied by a versioned symbol in another DSO. */
5823 static bfd_boolean
5824 elf_link_check_versioned_symbol (struct bfd_link_info *info,
5825 const struct elf_backend_data *bed,
5826 struct elf_link_hash_entry *h)
5828 bfd *abfd;
5829 struct elf_link_loaded_list *loaded;
5831 if (!is_elf_hash_table (info->hash))
5832 return FALSE;
5834 switch (h->root.type)
5836 default:
5837 abfd = NULL;
5838 break;
5840 case bfd_link_hash_undefined:
5841 case bfd_link_hash_undefweak:
5842 abfd = h->root.u.undef.abfd;
5843 if ((abfd->flags & DYNAMIC) == 0
5844 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
5845 return FALSE;
5846 break;
5848 case bfd_link_hash_defined:
5849 case bfd_link_hash_defweak:
5850 abfd = h->root.u.def.section->owner;
5851 break;
5853 case bfd_link_hash_common:
5854 abfd = h->root.u.c.p->section->owner;
5855 break;
5857 BFD_ASSERT (abfd != NULL);
5859 for (loaded = elf_hash_table (info)->loaded;
5860 loaded != NULL;
5861 loaded = loaded->next)
5863 bfd *input;
5864 Elf_Internal_Shdr *hdr;
5865 bfd_size_type symcount;
5866 bfd_size_type extsymcount;
5867 bfd_size_type extsymoff;
5868 Elf_Internal_Shdr *versymhdr;
5869 Elf_Internal_Sym *isym;
5870 Elf_Internal_Sym *isymend;
5871 Elf_Internal_Sym *isymbuf;
5872 Elf_External_Versym *ever;
5873 Elf_External_Versym *extversym;
5875 input = loaded->abfd;
5877 /* We check each DSO for a possible hidden versioned definition. */
5878 if (input == abfd
5879 || (input->flags & DYNAMIC) == 0
5880 || elf_dynversym (input) == 0)
5881 continue;
5883 hdr = &elf_tdata (input)->dynsymtab_hdr;
5885 symcount = hdr->sh_size / bed->s->sizeof_sym;
5886 if (elf_bad_symtab (input))
5888 extsymcount = symcount;
5889 extsymoff = 0;
5891 else
5893 extsymcount = symcount - hdr->sh_info;
5894 extsymoff = hdr->sh_info;
5897 if (extsymcount == 0)
5898 continue;
5900 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
5901 NULL, NULL, NULL);
5902 if (isymbuf == NULL)
5903 return FALSE;
5905 /* Read in any version definitions. */
5906 versymhdr = &elf_tdata (input)->dynversym_hdr;
5907 extversym = bfd_malloc (versymhdr->sh_size);
5908 if (extversym == NULL)
5909 goto error_ret;
5911 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
5912 || (bfd_bread (extversym, versymhdr->sh_size, input)
5913 != versymhdr->sh_size))
5915 free (extversym);
5916 error_ret:
5917 free (isymbuf);
5918 return FALSE;
5921 ever = extversym + extsymoff;
5922 isymend = isymbuf + extsymcount;
5923 for (isym = isymbuf; isym < isymend; isym++, ever++)
5925 const char *name;
5926 Elf_Internal_Versym iver;
5927 unsigned short version_index;
5929 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
5930 || isym->st_shndx == SHN_UNDEF)
5931 continue;
5933 name = bfd_elf_string_from_elf_section (input,
5934 hdr->sh_link,
5935 isym->st_name);
5936 if (strcmp (name, h->root.root.string) != 0)
5937 continue;
5939 _bfd_elf_swap_versym_in (input, ever, &iver);
5941 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
5943 /* If we have a non-hidden versioned sym, then it should
5944 have provided a definition for the undefined sym. */
5945 abort ();
5948 version_index = iver.vs_vers & VERSYM_VERSION;
5949 if (version_index == 1 || version_index == 2)
5951 /* This is the base or first version. We can use it. */
5952 free (extversym);
5953 free (isymbuf);
5954 return TRUE;
5958 free (extversym);
5959 free (isymbuf);
5962 return FALSE;
5965 /* Add an external symbol to the symbol table. This is called from
5966 the hash table traversal routine. When generating a shared object,
5967 we go through the symbol table twice. The first time we output
5968 anything that might have been forced to local scope in a version
5969 script. The second time we output the symbols that are still
5970 global symbols. */
5972 static bfd_boolean
5973 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
5975 struct elf_outext_info *eoinfo = data;
5976 struct elf_final_link_info *finfo = eoinfo->finfo;
5977 bfd_boolean strip;
5978 Elf_Internal_Sym sym;
5979 asection *input_sec;
5980 const struct elf_backend_data *bed;
5982 if (h->root.type == bfd_link_hash_warning)
5984 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5985 if (h->root.type == bfd_link_hash_new)
5986 return TRUE;
5989 /* Decide whether to output this symbol in this pass. */
5990 if (eoinfo->localsyms)
5992 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
5993 return TRUE;
5995 else
5997 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
5998 return TRUE;
6001 bed = get_elf_backend_data (finfo->output_bfd);
6003 /* If we have an undefined symbol reference here then it must have
6004 come from a shared library that is being linked in. (Undefined
6005 references in regular files have already been handled). If we
6006 are reporting errors for this situation then do so now. */
6007 if (h->root.type == bfd_link_hash_undefined
6008 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0
6009 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
6010 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
6011 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
6013 if (! ((*finfo->info->callbacks->undefined_symbol)
6014 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
6015 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
6017 eoinfo->failed = TRUE;
6018 return FALSE;
6022 /* We should also warn if a forced local symbol is referenced from
6023 shared libraries. */
6024 if (! finfo->info->relocatable
6025 && (! finfo->info->shared)
6026 && (h->elf_link_hash_flags
6027 & (ELF_LINK_FORCED_LOCAL | ELF_LINK_HASH_REF_DYNAMIC | ELF_LINK_DYNAMIC_DEF | ELF_LINK_DYNAMIC_WEAK))
6028 == (ELF_LINK_FORCED_LOCAL | ELF_LINK_HASH_REF_DYNAMIC)
6029 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
6031 (*_bfd_error_handler)
6032 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6033 finfo->output_bfd, h->root.u.def.section->owner,
6034 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
6035 ? "internal"
6036 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
6037 ? "hidden" : "local",
6038 h->root.root.string);
6039 eoinfo->failed = TRUE;
6040 return FALSE;
6043 /* We don't want to output symbols that have never been mentioned by
6044 a regular file, or that we have been told to strip. However, if
6045 h->indx is set to -2, the symbol is used by a reloc and we must
6046 output it. */
6047 if (h->indx == -2)
6048 strip = FALSE;
6049 else if (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
6050 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
6051 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
6052 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
6053 strip = TRUE;
6054 else if (finfo->info->strip == strip_all)
6055 strip = TRUE;
6056 else if (finfo->info->strip == strip_some
6057 && bfd_hash_lookup (finfo->info->keep_hash,
6058 h->root.root.string, FALSE, FALSE) == NULL)
6059 strip = TRUE;
6060 else if (finfo->info->strip_discarded
6061 && (h->root.type == bfd_link_hash_defined
6062 || h->root.type == bfd_link_hash_defweak)
6063 && elf_discarded_section (h->root.u.def.section))
6064 strip = TRUE;
6065 else
6066 strip = FALSE;
6068 /* If we're stripping it, and it's not a dynamic symbol, there's
6069 nothing else to do unless it is a forced local symbol. */
6070 if (strip
6071 && h->dynindx == -1
6072 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
6073 return TRUE;
6075 sym.st_value = 0;
6076 sym.st_size = h->size;
6077 sym.st_other = h->other;
6078 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
6079 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
6080 else if (h->root.type == bfd_link_hash_undefweak
6081 || h->root.type == bfd_link_hash_defweak)
6082 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
6083 else
6084 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
6086 switch (h->root.type)
6088 default:
6089 case bfd_link_hash_new:
6090 case bfd_link_hash_warning:
6091 abort ();
6092 return FALSE;
6094 case bfd_link_hash_undefined:
6095 case bfd_link_hash_undefweak:
6096 input_sec = bfd_und_section_ptr;
6097 sym.st_shndx = SHN_UNDEF;
6098 break;
6100 case bfd_link_hash_defined:
6101 case bfd_link_hash_defweak:
6103 input_sec = h->root.u.def.section;
6104 if (input_sec->output_section != NULL)
6106 sym.st_shndx =
6107 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
6108 input_sec->output_section);
6109 if (sym.st_shndx == SHN_BAD)
6111 (*_bfd_error_handler)
6112 (_("%B: could not find output section %A for input section %A"),
6113 finfo->output_bfd, input_sec->output_section, input_sec);
6114 eoinfo->failed = TRUE;
6115 return FALSE;
6118 /* ELF symbols in relocatable files are section relative,
6119 but in nonrelocatable files they are virtual
6120 addresses. */
6121 sym.st_value = h->root.u.def.value + input_sec->output_offset;
6122 if (! finfo->info->relocatable)
6124 sym.st_value += input_sec->output_section->vma;
6125 if (h->type == STT_TLS)
6127 /* STT_TLS symbols are relative to PT_TLS segment
6128 base. */
6129 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6130 sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6134 else
6136 BFD_ASSERT (input_sec->owner == NULL
6137 || (input_sec->owner->flags & DYNAMIC) != 0);
6138 sym.st_shndx = SHN_UNDEF;
6139 input_sec = bfd_und_section_ptr;
6142 break;
6144 case bfd_link_hash_common:
6145 input_sec = h->root.u.c.p->section;
6146 sym.st_shndx = SHN_COMMON;
6147 sym.st_value = 1 << h->root.u.c.p->alignment_power;
6148 break;
6150 case bfd_link_hash_indirect:
6151 /* These symbols are created by symbol versioning. They point
6152 to the decorated version of the name. For example, if the
6153 symbol foo@@GNU_1.2 is the default, which should be used when
6154 foo is used with no version, then we add an indirect symbol
6155 foo which points to foo@@GNU_1.2. We ignore these symbols,
6156 since the indirected symbol is already in the hash table. */
6157 return TRUE;
6160 /* Give the processor backend a chance to tweak the symbol value,
6161 and also to finish up anything that needs to be done for this
6162 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6163 forced local syms when non-shared is due to a historical quirk. */
6164 if ((h->dynindx != -1
6165 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
6166 && ((finfo->info->shared
6167 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6168 || h->root.type != bfd_link_hash_undefweak))
6169 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
6170 && elf_hash_table (finfo->info)->dynamic_sections_created)
6172 if (! ((*bed->elf_backend_finish_dynamic_symbol)
6173 (finfo->output_bfd, finfo->info, h, &sym)))
6175 eoinfo->failed = TRUE;
6176 return FALSE;
6180 /* If we are marking the symbol as undefined, and there are no
6181 non-weak references to this symbol from a regular object, then
6182 mark the symbol as weak undefined; if there are non-weak
6183 references, mark the symbol as strong. We can't do this earlier,
6184 because it might not be marked as undefined until the
6185 finish_dynamic_symbol routine gets through with it. */
6186 if (sym.st_shndx == SHN_UNDEF
6187 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
6188 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
6189 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
6191 int bindtype;
6193 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK) != 0)
6194 bindtype = STB_GLOBAL;
6195 else
6196 bindtype = STB_WEAK;
6197 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
6200 /* If a non-weak symbol with non-default visibility is not defined
6201 locally, it is a fatal error. */
6202 if (! finfo->info->relocatable
6203 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
6204 && ELF_ST_BIND (sym.st_info) != STB_WEAK
6205 && h->root.type == bfd_link_hash_undefined
6206 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6208 (*_bfd_error_handler)
6209 (_("%B: %s symbol `%s' isn't defined"),
6210 finfo->output_bfd,
6211 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
6212 ? "protected"
6213 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
6214 ? "internal" : "hidden",
6215 h->root.root.string);
6216 eoinfo->failed = TRUE;
6217 return FALSE;
6220 /* If this symbol should be put in the .dynsym section, then put it
6221 there now. We already know the symbol index. We also fill in
6222 the entry in the .hash section. */
6223 if (h->dynindx != -1
6224 && elf_hash_table (finfo->info)->dynamic_sections_created)
6226 size_t bucketcount;
6227 size_t bucket;
6228 size_t hash_entry_size;
6229 bfd_byte *bucketpos;
6230 bfd_vma chain;
6231 bfd_byte *esym;
6233 sym.st_name = h->dynstr_index;
6234 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
6235 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
6237 bucketcount = elf_hash_table (finfo->info)->bucketcount;
6238 bucket = h->elf_hash_value % bucketcount;
6239 hash_entry_size
6240 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
6241 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
6242 + (bucket + 2) * hash_entry_size);
6243 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
6244 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
6245 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
6246 ((bfd_byte *) finfo->hash_sec->contents
6247 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
6249 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
6251 Elf_Internal_Versym iversym;
6252 Elf_External_Versym *eversym;
6254 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6256 if (h->verinfo.verdef == NULL)
6257 iversym.vs_vers = 0;
6258 else
6259 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
6261 else
6263 if (h->verinfo.vertree == NULL)
6264 iversym.vs_vers = 1;
6265 else
6266 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
6269 if ((h->elf_link_hash_flags & ELF_LINK_HIDDEN) != 0)
6270 iversym.vs_vers |= VERSYM_HIDDEN;
6272 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
6273 eversym += h->dynindx;
6274 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
6278 /* If we're stripping it, then it was just a dynamic symbol, and
6279 there's nothing else to do. */
6280 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
6281 return TRUE;
6283 h->indx = bfd_get_symcount (finfo->output_bfd);
6285 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
6287 eoinfo->failed = TRUE;
6288 return FALSE;
6291 return TRUE;
6294 /* Return TRUE if special handling is done for relocs in SEC against
6295 symbols defined in discarded sections. */
6297 static bfd_boolean
6298 elf_section_ignore_discarded_relocs (asection *sec)
6300 const struct elf_backend_data *bed;
6302 switch (sec->sec_info_type)
6304 case ELF_INFO_TYPE_STABS:
6305 case ELF_INFO_TYPE_EH_FRAME:
6306 return TRUE;
6307 default:
6308 break;
6311 bed = get_elf_backend_data (sec->owner);
6312 if (bed->elf_backend_ignore_discarded_relocs != NULL
6313 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
6314 return TRUE;
6316 return FALSE;
6319 /* Return TRUE if we should complain about a reloc in SEC against a
6320 symbol defined in a discarded section. */
6322 static bfd_boolean
6323 elf_section_complain_discarded (asection *sec)
6325 if (strncmp (".stab", sec->name, 5) == 0
6326 && (!sec->name[5] ||
6327 (sec->name[5] == '.' && ISDIGIT (sec->name[6]))))
6328 return FALSE;
6330 if (strcmp (".eh_frame", sec->name) == 0)
6331 return FALSE;
6333 if (strcmp (".gcc_except_table", sec->name) == 0)
6334 return FALSE;
6336 if (strcmp (".PARISC.unwind", sec->name) == 0)
6337 return FALSE;
6339 if (strcmp (".fixup", sec->name) == 0)
6340 return FALSE;
6342 return TRUE;
6345 /* Find a match between a section and a member of a section group. */
6347 static asection *
6348 match_group_member (asection *sec, asection *group)
6350 asection *first = elf_next_in_group (group);
6351 asection *s = first;
6353 while (s != NULL)
6355 if (bfd_elf_match_symbols_in_sections (s, sec))
6356 return s;
6358 if (s == first)
6359 break;
6362 return NULL;
6365 /* Link an input file into the linker output file. This function
6366 handles all the sections and relocations of the input file at once.
6367 This is so that we only have to read the local symbols once, and
6368 don't have to keep them in memory. */
6370 static bfd_boolean
6371 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
6373 bfd_boolean (*relocate_section)
6374 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
6375 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
6376 bfd *output_bfd;
6377 Elf_Internal_Shdr *symtab_hdr;
6378 size_t locsymcount;
6379 size_t extsymoff;
6380 Elf_Internal_Sym *isymbuf;
6381 Elf_Internal_Sym *isym;
6382 Elf_Internal_Sym *isymend;
6383 long *pindex;
6384 asection **ppsection;
6385 asection *o;
6386 const struct elf_backend_data *bed;
6387 bfd_boolean emit_relocs;
6388 struct elf_link_hash_entry **sym_hashes;
6390 output_bfd = finfo->output_bfd;
6391 bed = get_elf_backend_data (output_bfd);
6392 relocate_section = bed->elf_backend_relocate_section;
6394 /* If this is a dynamic object, we don't want to do anything here:
6395 we don't want the local symbols, and we don't want the section
6396 contents. */
6397 if ((input_bfd->flags & DYNAMIC) != 0)
6398 return TRUE;
6400 emit_relocs = (finfo->info->relocatable
6401 || finfo->info->emitrelocations
6402 || bed->elf_backend_emit_relocs);
6404 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6405 if (elf_bad_symtab (input_bfd))
6407 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
6408 extsymoff = 0;
6410 else
6412 locsymcount = symtab_hdr->sh_info;
6413 extsymoff = symtab_hdr->sh_info;
6416 /* Read the local symbols. */
6417 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
6418 if (isymbuf == NULL && locsymcount != 0)
6420 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
6421 finfo->internal_syms,
6422 finfo->external_syms,
6423 finfo->locsym_shndx);
6424 if (isymbuf == NULL)
6425 return FALSE;
6428 /* Find local symbol sections and adjust values of symbols in
6429 SEC_MERGE sections. Write out those local symbols we know are
6430 going into the output file. */
6431 isymend = isymbuf + locsymcount;
6432 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
6433 isym < isymend;
6434 isym++, pindex++, ppsection++)
6436 asection *isec;
6437 const char *name;
6438 Elf_Internal_Sym osym;
6440 *pindex = -1;
6442 if (elf_bad_symtab (input_bfd))
6444 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
6446 *ppsection = NULL;
6447 continue;
6451 if (isym->st_shndx == SHN_UNDEF)
6452 isec = bfd_und_section_ptr;
6453 else if (isym->st_shndx < SHN_LORESERVE
6454 || isym->st_shndx > SHN_HIRESERVE)
6456 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
6457 if (isec
6458 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
6459 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
6460 isym->st_value =
6461 _bfd_merged_section_offset (output_bfd, &isec,
6462 elf_section_data (isec)->sec_info,
6463 isym->st_value);
6465 else if (isym->st_shndx == SHN_ABS)
6466 isec = bfd_abs_section_ptr;
6467 else if (isym->st_shndx == SHN_COMMON)
6468 isec = bfd_com_section_ptr;
6469 else
6471 /* Who knows? */
6472 isec = NULL;
6475 *ppsection = isec;
6477 /* Don't output the first, undefined, symbol. */
6478 if (ppsection == finfo->sections)
6479 continue;
6481 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6483 /* We never output section symbols. Instead, we use the
6484 section symbol of the corresponding section in the output
6485 file. */
6486 continue;
6489 /* If we are stripping all symbols, we don't want to output this
6490 one. */
6491 if (finfo->info->strip == strip_all)
6492 continue;
6494 /* If we are discarding all local symbols, we don't want to
6495 output this one. If we are generating a relocatable output
6496 file, then some of the local symbols may be required by
6497 relocs; we output them below as we discover that they are
6498 needed. */
6499 if (finfo->info->discard == discard_all)
6500 continue;
6502 /* If this symbol is defined in a section which we are
6503 discarding, we don't need to keep it, but note that
6504 linker_mark is only reliable for sections that have contents.
6505 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6506 as well as linker_mark. */
6507 if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
6508 && isec != NULL
6509 && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
6510 || (! finfo->info->relocatable
6511 && (isec->flags & SEC_EXCLUDE) != 0)))
6512 continue;
6514 /* Get the name of the symbol. */
6515 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
6516 isym->st_name);
6517 if (name == NULL)
6518 return FALSE;
6520 /* See if we are discarding symbols with this name. */
6521 if ((finfo->info->strip == strip_some
6522 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
6523 == NULL))
6524 || (((finfo->info->discard == discard_sec_merge
6525 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
6526 || finfo->info->discard == discard_l)
6527 && bfd_is_local_label_name (input_bfd, name)))
6528 continue;
6530 /* If we get here, we are going to output this symbol. */
6532 osym = *isym;
6534 /* Adjust the section index for the output file. */
6535 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
6536 isec->output_section);
6537 if (osym.st_shndx == SHN_BAD)
6538 return FALSE;
6540 *pindex = bfd_get_symcount (output_bfd);
6542 /* ELF symbols in relocatable files are section relative, but
6543 in executable files they are virtual addresses. Note that
6544 this code assumes that all ELF sections have an associated
6545 BFD section with a reasonable value for output_offset; below
6546 we assume that they also have a reasonable value for
6547 output_section. Any special sections must be set up to meet
6548 these requirements. */
6549 osym.st_value += isec->output_offset;
6550 if (! finfo->info->relocatable)
6552 osym.st_value += isec->output_section->vma;
6553 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
6555 /* STT_TLS symbols are relative to PT_TLS segment base. */
6556 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6557 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6561 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
6562 return FALSE;
6565 /* Relocate the contents of each section. */
6566 sym_hashes = elf_sym_hashes (input_bfd);
6567 for (o = input_bfd->sections; o != NULL; o = o->next)
6569 bfd_byte *contents;
6571 if (! o->linker_mark)
6573 /* This section was omitted from the link. */
6574 continue;
6577 if ((o->flags & SEC_HAS_CONTENTS) == 0
6578 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
6579 continue;
6581 if ((o->flags & SEC_LINKER_CREATED) != 0)
6583 /* Section was created by _bfd_elf_link_create_dynamic_sections
6584 or somesuch. */
6585 continue;
6588 /* Get the contents of the section. They have been cached by a
6589 relaxation routine. Note that o is a section in an input
6590 file, so the contents field will not have been set by any of
6591 the routines which work on output files. */
6592 if (elf_section_data (o)->this_hdr.contents != NULL)
6593 contents = elf_section_data (o)->this_hdr.contents;
6594 else
6596 bfd_size_type amt = o->rawsize ? o->rawsize : o->size;
6598 contents = finfo->contents;
6599 if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt))
6600 return FALSE;
6603 if ((o->flags & SEC_RELOC) != 0)
6605 Elf_Internal_Rela *internal_relocs;
6606 bfd_vma r_type_mask;
6607 int r_sym_shift;
6609 /* Get the swapped relocs. */
6610 internal_relocs
6611 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
6612 finfo->internal_relocs, FALSE);
6613 if (internal_relocs == NULL
6614 && o->reloc_count > 0)
6615 return FALSE;
6617 if (bed->s->arch_size == 32)
6619 r_type_mask = 0xff;
6620 r_sym_shift = 8;
6622 else
6624 r_type_mask = 0xffffffff;
6625 r_sym_shift = 32;
6628 /* Run through the relocs looking for any against symbols
6629 from discarded sections and section symbols from
6630 removed link-once sections. Complain about relocs
6631 against discarded sections. Zero relocs against removed
6632 link-once sections. Preserve debug information as much
6633 as we can. */
6634 if (!elf_section_ignore_discarded_relocs (o))
6636 Elf_Internal_Rela *rel, *relend;
6637 bfd_boolean complain = elf_section_complain_discarded (o);
6639 rel = internal_relocs;
6640 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
6641 for ( ; rel < relend; rel++)
6643 unsigned long r_symndx = rel->r_info >> r_sym_shift;
6644 asection **ps, *sec;
6645 struct elf_link_hash_entry *h = NULL;
6646 const char *sym_name;
6648 if (r_symndx >= locsymcount
6649 || (elf_bad_symtab (input_bfd)
6650 && finfo->sections[r_symndx] == NULL))
6652 h = sym_hashes[r_symndx - extsymoff];
6653 while (h->root.type == bfd_link_hash_indirect
6654 || h->root.type == bfd_link_hash_warning)
6655 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6657 if (h->root.type != bfd_link_hash_defined
6658 && h->root.type != bfd_link_hash_defweak)
6659 continue;
6661 ps = &h->root.u.def.section;
6662 sym_name = h->root.root.string;
6664 else
6666 Elf_Internal_Sym *sym = isymbuf + r_symndx;
6667 ps = &finfo->sections[r_symndx];
6668 sym_name = bfd_elf_local_sym_name (input_bfd, sym);
6671 /* Complain if the definition comes from a
6672 discarded section. */
6673 if ((sec = *ps) != NULL && elf_discarded_section (sec))
6675 if ((o->flags & SEC_DEBUGGING) != 0)
6677 BFD_ASSERT (r_symndx != 0);
6679 /* Try to preserve debug information.
6680 FIXME: This is quite broken. Modifying
6681 the symbol here means we will be changing
6682 all uses of the symbol, not just those in
6683 debug sections. The only thing that makes
6684 this half reasonable is that debug sections
6685 tend to come after other sections. Of
6686 course, that doesn't help with globals.
6687 ??? All link-once sections of the same name
6688 ought to define the same set of symbols, so
6689 it would seem that globals ought to always
6690 be defined in the kept section. */
6691 if (sec->kept_section != NULL)
6693 asection *member;
6695 /* Check if it is a linkonce section or
6696 member of a comdat group. */
6697 if (elf_sec_group (sec) == NULL
6698 && sec->size == sec->kept_section->size)
6700 *ps = sec->kept_section;
6701 continue;
6703 else if (elf_sec_group (sec) != NULL
6704 && (member = match_group_member (sec, sec->kept_section))
6705 && sec->size == member->size)
6707 *ps = member;
6708 continue;
6712 else if (complain)
6714 (*_bfd_error_handler)
6715 (_("`%s' referenced in section `%A' of %B: "
6716 "defined in discarded section `%A' of %B\n"),
6717 o, input_bfd, sec, sec->owner, sym_name);
6720 /* Remove the symbol reference from the reloc, but
6721 don't kill the reloc completely. This is so that
6722 a zero value will be written into the section,
6723 which may have non-zero contents put there by the
6724 assembler. Zero in things like an eh_frame fde
6725 pc_begin allows stack unwinders to recognize the
6726 fde as bogus. */
6727 rel->r_info &= r_type_mask;
6728 rel->r_addend = 0;
6733 /* Relocate the section by invoking a back end routine.
6735 The back end routine is responsible for adjusting the
6736 section contents as necessary, and (if using Rela relocs
6737 and generating a relocatable output file) adjusting the
6738 reloc addend as necessary.
6740 The back end routine does not have to worry about setting
6741 the reloc address or the reloc symbol index.
6743 The back end routine is given a pointer to the swapped in
6744 internal symbols, and can access the hash table entries
6745 for the external symbols via elf_sym_hashes (input_bfd).
6747 When generating relocatable output, the back end routine
6748 must handle STB_LOCAL/STT_SECTION symbols specially. The
6749 output symbol is going to be a section symbol
6750 corresponding to the output section, which will require
6751 the addend to be adjusted. */
6753 if (! (*relocate_section) (output_bfd, finfo->info,
6754 input_bfd, o, contents,
6755 internal_relocs,
6756 isymbuf,
6757 finfo->sections))
6758 return FALSE;
6760 if (emit_relocs)
6762 Elf_Internal_Rela *irela;
6763 Elf_Internal_Rela *irelaend;
6764 bfd_vma last_offset;
6765 struct elf_link_hash_entry **rel_hash;
6766 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
6767 unsigned int next_erel;
6768 bfd_boolean (*reloc_emitter)
6769 (bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *);
6770 bfd_boolean rela_normal;
6772 input_rel_hdr = &elf_section_data (o)->rel_hdr;
6773 rela_normal = (bed->rela_normal
6774 && (input_rel_hdr->sh_entsize
6775 == bed->s->sizeof_rela));
6777 /* Adjust the reloc addresses and symbol indices. */
6779 irela = internal_relocs;
6780 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
6781 rel_hash = (elf_section_data (o->output_section)->rel_hashes
6782 + elf_section_data (o->output_section)->rel_count
6783 + elf_section_data (o->output_section)->rel_count2);
6784 last_offset = o->output_offset;
6785 if (!finfo->info->relocatable)
6786 last_offset += o->output_section->vma;
6787 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
6789 unsigned long r_symndx;
6790 asection *sec;
6791 Elf_Internal_Sym sym;
6793 if (next_erel == bed->s->int_rels_per_ext_rel)
6795 rel_hash++;
6796 next_erel = 0;
6799 irela->r_offset = _bfd_elf_section_offset (output_bfd,
6800 finfo->info, o,
6801 irela->r_offset);
6802 if (irela->r_offset >= (bfd_vma) -2)
6804 /* This is a reloc for a deleted entry or somesuch.
6805 Turn it into an R_*_NONE reloc, at the same
6806 offset as the last reloc. elf_eh_frame.c and
6807 elf_bfd_discard_info rely on reloc offsets
6808 being ordered. */
6809 irela->r_offset = last_offset;
6810 irela->r_info = 0;
6811 irela->r_addend = 0;
6812 continue;
6815 irela->r_offset += o->output_offset;
6817 /* Relocs in an executable have to be virtual addresses. */
6818 if (!finfo->info->relocatable)
6819 irela->r_offset += o->output_section->vma;
6821 last_offset = irela->r_offset;
6823 r_symndx = irela->r_info >> r_sym_shift;
6824 if (r_symndx == STN_UNDEF)
6825 continue;
6827 if (r_symndx >= locsymcount
6828 || (elf_bad_symtab (input_bfd)
6829 && finfo->sections[r_symndx] == NULL))
6831 struct elf_link_hash_entry *rh;
6832 unsigned long indx;
6834 /* This is a reloc against a global symbol. We
6835 have not yet output all the local symbols, so
6836 we do not know the symbol index of any global
6837 symbol. We set the rel_hash entry for this
6838 reloc to point to the global hash table entry
6839 for this symbol. The symbol index is then
6840 set at the end of elf_bfd_final_link. */
6841 indx = r_symndx - extsymoff;
6842 rh = elf_sym_hashes (input_bfd)[indx];
6843 while (rh->root.type == bfd_link_hash_indirect
6844 || rh->root.type == bfd_link_hash_warning)
6845 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
6847 /* Setting the index to -2 tells
6848 elf_link_output_extsym that this symbol is
6849 used by a reloc. */
6850 BFD_ASSERT (rh->indx < 0);
6851 rh->indx = -2;
6853 *rel_hash = rh;
6855 continue;
6858 /* This is a reloc against a local symbol. */
6860 *rel_hash = NULL;
6861 sym = isymbuf[r_symndx];
6862 sec = finfo->sections[r_symndx];
6863 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
6865 /* I suppose the backend ought to fill in the
6866 section of any STT_SECTION symbol against a
6867 processor specific section. */
6868 r_symndx = 0;
6869 if (bfd_is_abs_section (sec))
6871 else if (sec == NULL || sec->owner == NULL)
6873 bfd_set_error (bfd_error_bad_value);
6874 return FALSE;
6876 else
6878 asection *osec = sec->output_section;
6880 /* If we have discarded a section, the output
6881 section will be the absolute section. In
6882 case of discarded link-once and discarded
6883 SEC_MERGE sections, use the kept section. */
6884 if (bfd_is_abs_section (osec)
6885 && sec->kept_section != NULL
6886 && sec->kept_section->output_section != NULL)
6888 osec = sec->kept_section->output_section;
6889 irela->r_addend -= osec->vma;
6892 if (!bfd_is_abs_section (osec))
6894 r_symndx = osec->target_index;
6895 BFD_ASSERT (r_symndx != 0);
6899 /* Adjust the addend according to where the
6900 section winds up in the output section. */
6901 if (rela_normal)
6902 irela->r_addend += sec->output_offset;
6904 else
6906 if (finfo->indices[r_symndx] == -1)
6908 unsigned long shlink;
6909 const char *name;
6910 asection *osec;
6912 if (finfo->info->strip == strip_all)
6914 /* You can't do ld -r -s. */
6915 bfd_set_error (bfd_error_invalid_operation);
6916 return FALSE;
6919 /* This symbol was skipped earlier, but
6920 since it is needed by a reloc, we
6921 must output it now. */
6922 shlink = symtab_hdr->sh_link;
6923 name = (bfd_elf_string_from_elf_section
6924 (input_bfd, shlink, sym.st_name));
6925 if (name == NULL)
6926 return FALSE;
6928 osec = sec->output_section;
6929 sym.st_shndx =
6930 _bfd_elf_section_from_bfd_section (output_bfd,
6931 osec);
6932 if (sym.st_shndx == SHN_BAD)
6933 return FALSE;
6935 sym.st_value += sec->output_offset;
6936 if (! finfo->info->relocatable)
6938 sym.st_value += osec->vma;
6939 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
6941 /* STT_TLS symbols are relative to PT_TLS
6942 segment base. */
6943 BFD_ASSERT (elf_hash_table (finfo->info)
6944 ->tls_sec != NULL);
6945 sym.st_value -= (elf_hash_table (finfo->info)
6946 ->tls_sec->vma);
6950 finfo->indices[r_symndx]
6951 = bfd_get_symcount (output_bfd);
6953 if (! elf_link_output_sym (finfo, name, &sym, sec,
6954 NULL))
6955 return FALSE;
6958 r_symndx = finfo->indices[r_symndx];
6961 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
6962 | (irela->r_info & r_type_mask));
6965 /* Swap out the relocs. */
6966 if (bed->elf_backend_emit_relocs
6967 && !(finfo->info->relocatable
6968 || finfo->info->emitrelocations))
6969 reloc_emitter = bed->elf_backend_emit_relocs;
6970 else
6971 reloc_emitter = _bfd_elf_link_output_relocs;
6973 if (input_rel_hdr->sh_size != 0
6974 && ! (*reloc_emitter) (output_bfd, o, input_rel_hdr,
6975 internal_relocs))
6976 return FALSE;
6978 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
6979 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
6981 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
6982 * bed->s->int_rels_per_ext_rel);
6983 if (! (*reloc_emitter) (output_bfd, o, input_rel_hdr2,
6984 internal_relocs))
6985 return FALSE;
6990 /* Write out the modified section contents. */
6991 if (bed->elf_backend_write_section
6992 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
6994 /* Section written out. */
6996 else switch (o->sec_info_type)
6998 case ELF_INFO_TYPE_STABS:
6999 if (! (_bfd_write_section_stabs
7000 (output_bfd,
7001 &elf_hash_table (finfo->info)->stab_info,
7002 o, &elf_section_data (o)->sec_info, contents)))
7003 return FALSE;
7004 break;
7005 case ELF_INFO_TYPE_MERGE:
7006 if (! _bfd_write_merged_section (output_bfd, o,
7007 elf_section_data (o)->sec_info))
7008 return FALSE;
7009 break;
7010 case ELF_INFO_TYPE_EH_FRAME:
7012 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
7013 o, contents))
7014 return FALSE;
7016 break;
7017 default:
7019 if (! (o->flags & SEC_EXCLUDE)
7020 && ! bfd_set_section_contents (output_bfd, o->output_section,
7021 contents,
7022 (file_ptr) o->output_offset,
7023 o->size))
7024 return FALSE;
7026 break;
7030 return TRUE;
7033 /* Generate a reloc when linking an ELF file. This is a reloc
7034 requested by the linker, and does come from any input file. This
7035 is used to build constructor and destructor tables when linking
7036 with -Ur. */
7038 static bfd_boolean
7039 elf_reloc_link_order (bfd *output_bfd,
7040 struct bfd_link_info *info,
7041 asection *output_section,
7042 struct bfd_link_order *link_order)
7044 reloc_howto_type *howto;
7045 long indx;
7046 bfd_vma offset;
7047 bfd_vma addend;
7048 struct elf_link_hash_entry **rel_hash_ptr;
7049 Elf_Internal_Shdr *rel_hdr;
7050 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7051 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
7052 bfd_byte *erel;
7053 unsigned int i;
7055 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
7056 if (howto == NULL)
7058 bfd_set_error (bfd_error_bad_value);
7059 return FALSE;
7062 addend = link_order->u.reloc.p->addend;
7064 /* Figure out the symbol index. */
7065 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
7066 + elf_section_data (output_section)->rel_count
7067 + elf_section_data (output_section)->rel_count2);
7068 if (link_order->type == bfd_section_reloc_link_order)
7070 indx = link_order->u.reloc.p->u.section->target_index;
7071 BFD_ASSERT (indx != 0);
7072 *rel_hash_ptr = NULL;
7074 else
7076 struct elf_link_hash_entry *h;
7078 /* Treat a reloc against a defined symbol as though it were
7079 actually against the section. */
7080 h = ((struct elf_link_hash_entry *)
7081 bfd_wrapped_link_hash_lookup (output_bfd, info,
7082 link_order->u.reloc.p->u.name,
7083 FALSE, FALSE, TRUE));
7084 if (h != NULL
7085 && (h->root.type == bfd_link_hash_defined
7086 || h->root.type == bfd_link_hash_defweak))
7088 asection *section;
7090 section = h->root.u.def.section;
7091 indx = section->output_section->target_index;
7092 *rel_hash_ptr = NULL;
7093 /* It seems that we ought to add the symbol value to the
7094 addend here, but in practice it has already been added
7095 because it was passed to constructor_callback. */
7096 addend += section->output_section->vma + section->output_offset;
7098 else if (h != NULL)
7100 /* Setting the index to -2 tells elf_link_output_extsym that
7101 this symbol is used by a reloc. */
7102 h->indx = -2;
7103 *rel_hash_ptr = h;
7104 indx = 0;
7106 else
7108 if (! ((*info->callbacks->unattached_reloc)
7109 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
7110 return FALSE;
7111 indx = 0;
7115 /* If this is an inplace reloc, we must write the addend into the
7116 object file. */
7117 if (howto->partial_inplace && addend != 0)
7119 bfd_size_type size;
7120 bfd_reloc_status_type rstat;
7121 bfd_byte *buf;
7122 bfd_boolean ok;
7123 const char *sym_name;
7125 size = bfd_get_reloc_size (howto);
7126 buf = bfd_zmalloc (size);
7127 if (buf == NULL)
7128 return FALSE;
7129 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
7130 switch (rstat)
7132 case bfd_reloc_ok:
7133 break;
7135 default:
7136 case bfd_reloc_outofrange:
7137 abort ();
7139 case bfd_reloc_overflow:
7140 if (link_order->type == bfd_section_reloc_link_order)
7141 sym_name = bfd_section_name (output_bfd,
7142 link_order->u.reloc.p->u.section);
7143 else
7144 sym_name = link_order->u.reloc.p->u.name;
7145 if (! ((*info->callbacks->reloc_overflow)
7146 (info, sym_name, howto->name, addend, NULL, NULL, 0)))
7148 free (buf);
7149 return FALSE;
7151 break;
7153 ok = bfd_set_section_contents (output_bfd, output_section, buf,
7154 link_order->offset, size);
7155 free (buf);
7156 if (! ok)
7157 return FALSE;
7160 /* The address of a reloc is relative to the section in a
7161 relocatable file, and is a virtual address in an executable
7162 file. */
7163 offset = link_order->offset;
7164 if (! info->relocatable)
7165 offset += output_section->vma;
7167 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7169 irel[i].r_offset = offset;
7170 irel[i].r_info = 0;
7171 irel[i].r_addend = 0;
7173 if (bed->s->arch_size == 32)
7174 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
7175 else
7176 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
7178 rel_hdr = &elf_section_data (output_section)->rel_hdr;
7179 erel = rel_hdr->contents;
7180 if (rel_hdr->sh_type == SHT_REL)
7182 erel += (elf_section_data (output_section)->rel_count
7183 * bed->s->sizeof_rel);
7184 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
7186 else
7188 irel[0].r_addend = addend;
7189 erel += (elf_section_data (output_section)->rel_count
7190 * bed->s->sizeof_rela);
7191 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
7194 ++elf_section_data (output_section)->rel_count;
7196 return TRUE;
7200 /* Get the output vma of the section pointed to by the sh_link field. */
7202 static bfd_vma
7203 elf_get_linked_section_vma (struct bfd_link_order *p)
7205 Elf_Internal_Shdr **elf_shdrp;
7206 asection *s;
7207 int elfsec;
7209 s = p->u.indirect.section;
7210 elf_shdrp = elf_elfsections (s->owner);
7211 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
7212 elfsec = elf_shdrp[elfsec]->sh_link;
7213 /* PR 290:
7214 The Intel C compiler generates SHT_IA_64_UNWIND with
7215 SHF_LINK_ORDER. But it doesn't set theh sh_link or
7216 sh_info fields. Hence we could get the situation
7217 where elfsec is 0. */
7218 if (elfsec == 0)
7220 const struct elf_backend_data *bed
7221 = get_elf_backend_data (s->owner);
7222 if (bed->link_order_error_handler)
7223 bed->link_order_error_handler
7224 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
7225 return 0;
7227 else
7229 s = elf_shdrp[elfsec]->bfd_section;
7230 return s->output_section->vma + s->output_offset;
7235 /* Compare two sections based on the locations of the sections they are
7236 linked to. Used by elf_fixup_link_order. */
7238 static int
7239 compare_link_order (const void * a, const void * b)
7241 bfd_vma apos;
7242 bfd_vma bpos;
7244 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
7245 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
7246 if (apos < bpos)
7247 return -1;
7248 return apos > bpos;
7252 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7253 order as their linked sections. Returns false if this could not be done
7254 because an output section includes both ordered and unordered
7255 sections. Ideally we'd do this in the linker proper. */
7257 static bfd_boolean
7258 elf_fixup_link_order (bfd *abfd, asection *o)
7260 int seen_linkorder;
7261 int seen_other;
7262 int n;
7263 struct bfd_link_order *p;
7264 bfd *sub;
7265 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7266 int elfsec;
7267 struct bfd_link_order **sections;
7268 asection *s;
7269 bfd_vma offset;
7271 seen_other = 0;
7272 seen_linkorder = 0;
7273 for (p = o->link_order_head; p != NULL; p = p->next)
7275 if (p->type == bfd_indirect_link_order
7276 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
7277 == bfd_target_elf_flavour)
7278 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
7280 s = p->u.indirect.section;
7281 elfsec = _bfd_elf_section_from_bfd_section (sub, s);
7282 if (elfsec != -1
7283 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER)
7284 seen_linkorder++;
7285 else
7286 seen_other++;
7288 else
7289 seen_other++;
7292 if (!seen_linkorder)
7293 return TRUE;
7295 if (seen_other && seen_linkorder)
7297 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
7299 bfd_set_error (bfd_error_bad_value);
7300 return FALSE;
7303 sections = (struct bfd_link_order **)
7304 xmalloc (seen_linkorder * sizeof (struct bfd_link_order *));
7305 seen_linkorder = 0;
7307 for (p = o->link_order_head; p != NULL; p = p->next)
7309 sections[seen_linkorder++] = p;
7311 /* Sort the input sections in the order of their linked section. */
7312 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
7313 compare_link_order);
7315 /* Change the offsets of the sections. */
7316 offset = 0;
7317 for (n = 0; n < seen_linkorder; n++)
7319 s = sections[n]->u.indirect.section;
7320 offset &= ~(bfd_vma)((1 << s->alignment_power) - 1);
7321 s->output_offset = offset;
7322 sections[n]->offset = offset;
7323 offset += sections[n]->size;
7326 return TRUE;
7330 /* Do the final step of an ELF link. */
7332 bfd_boolean
7333 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
7335 bfd_boolean dynamic;
7336 bfd_boolean emit_relocs;
7337 bfd *dynobj;
7338 struct elf_final_link_info finfo;
7339 register asection *o;
7340 register struct bfd_link_order *p;
7341 register bfd *sub;
7342 bfd_size_type max_contents_size;
7343 bfd_size_type max_external_reloc_size;
7344 bfd_size_type max_internal_reloc_count;
7345 bfd_size_type max_sym_count;
7346 bfd_size_type max_sym_shndx_count;
7347 file_ptr off;
7348 Elf_Internal_Sym elfsym;
7349 unsigned int i;
7350 Elf_Internal_Shdr *symtab_hdr;
7351 Elf_Internal_Shdr *symtab_shndx_hdr;
7352 Elf_Internal_Shdr *symstrtab_hdr;
7353 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7354 struct elf_outext_info eoinfo;
7355 bfd_boolean merged;
7356 size_t relativecount = 0;
7357 asection *reldyn = 0;
7358 bfd_size_type amt;
7360 if (! is_elf_hash_table (info->hash))
7361 return FALSE;
7363 if (info->shared)
7364 abfd->flags |= DYNAMIC;
7366 dynamic = elf_hash_table (info)->dynamic_sections_created;
7367 dynobj = elf_hash_table (info)->dynobj;
7369 emit_relocs = (info->relocatable
7370 || info->emitrelocations
7371 || bed->elf_backend_emit_relocs);
7373 finfo.info = info;
7374 finfo.output_bfd = abfd;
7375 finfo.symstrtab = _bfd_elf_stringtab_init ();
7376 if (finfo.symstrtab == NULL)
7377 return FALSE;
7379 if (! dynamic)
7381 finfo.dynsym_sec = NULL;
7382 finfo.hash_sec = NULL;
7383 finfo.symver_sec = NULL;
7385 else
7387 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
7388 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
7389 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
7390 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
7391 /* Note that it is OK if symver_sec is NULL. */
7394 finfo.contents = NULL;
7395 finfo.external_relocs = NULL;
7396 finfo.internal_relocs = NULL;
7397 finfo.external_syms = NULL;
7398 finfo.locsym_shndx = NULL;
7399 finfo.internal_syms = NULL;
7400 finfo.indices = NULL;
7401 finfo.sections = NULL;
7402 finfo.symbuf = NULL;
7403 finfo.symshndxbuf = NULL;
7404 finfo.symbuf_count = 0;
7405 finfo.shndxbuf_size = 0;
7407 /* Count up the number of relocations we will output for each output
7408 section, so that we know the sizes of the reloc sections. We
7409 also figure out some maximum sizes. */
7410 max_contents_size = 0;
7411 max_external_reloc_size = 0;
7412 max_internal_reloc_count = 0;
7413 max_sym_count = 0;
7414 max_sym_shndx_count = 0;
7415 merged = FALSE;
7416 for (o = abfd->sections; o != NULL; o = o->next)
7418 struct bfd_elf_section_data *esdo = elf_section_data (o);
7419 o->reloc_count = 0;
7421 for (p = o->link_order_head; p != NULL; p = p->next)
7423 unsigned int reloc_count = 0;
7424 struct bfd_elf_section_data *esdi = NULL;
7425 unsigned int *rel_count1;
7427 if (p->type == bfd_section_reloc_link_order
7428 || p->type == bfd_symbol_reloc_link_order)
7429 reloc_count = 1;
7430 else if (p->type == bfd_indirect_link_order)
7432 asection *sec;
7434 sec = p->u.indirect.section;
7435 esdi = elf_section_data (sec);
7437 /* Mark all sections which are to be included in the
7438 link. This will normally be every section. We need
7439 to do this so that we can identify any sections which
7440 the linker has decided to not include. */
7441 sec->linker_mark = TRUE;
7443 if (sec->flags & SEC_MERGE)
7444 merged = TRUE;
7446 if (info->relocatable || info->emitrelocations)
7447 reloc_count = sec->reloc_count;
7448 else if (bed->elf_backend_count_relocs)
7450 Elf_Internal_Rela * relocs;
7452 relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
7453 info->keep_memory);
7455 reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs);
7457 if (elf_section_data (o)->relocs != relocs)
7458 free (relocs);
7461 if (sec->rawsize > max_contents_size)
7462 max_contents_size = sec->rawsize;
7463 if (sec->size > max_contents_size)
7464 max_contents_size = sec->size;
7466 /* We are interested in just local symbols, not all
7467 symbols. */
7468 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
7469 && (sec->owner->flags & DYNAMIC) == 0)
7471 size_t sym_count;
7473 if (elf_bad_symtab (sec->owner))
7474 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
7475 / bed->s->sizeof_sym);
7476 else
7477 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
7479 if (sym_count > max_sym_count)
7480 max_sym_count = sym_count;
7482 if (sym_count > max_sym_shndx_count
7483 && elf_symtab_shndx (sec->owner) != 0)
7484 max_sym_shndx_count = sym_count;
7486 if ((sec->flags & SEC_RELOC) != 0)
7488 size_t ext_size;
7490 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
7491 if (ext_size > max_external_reloc_size)
7492 max_external_reloc_size = ext_size;
7493 if (sec->reloc_count > max_internal_reloc_count)
7494 max_internal_reloc_count = sec->reloc_count;
7499 if (reloc_count == 0)
7500 continue;
7502 o->reloc_count += reloc_count;
7504 /* MIPS may have a mix of REL and RELA relocs on sections.
7505 To support this curious ABI we keep reloc counts in
7506 elf_section_data too. We must be careful to add the
7507 relocations from the input section to the right output
7508 count. FIXME: Get rid of one count. We have
7509 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7510 rel_count1 = &esdo->rel_count;
7511 if (esdi != NULL)
7513 bfd_boolean same_size;
7514 bfd_size_type entsize1;
7516 entsize1 = esdi->rel_hdr.sh_entsize;
7517 BFD_ASSERT (entsize1 == bed->s->sizeof_rel
7518 || entsize1 == bed->s->sizeof_rela);
7519 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
7521 if (!same_size)
7522 rel_count1 = &esdo->rel_count2;
7524 if (esdi->rel_hdr2 != NULL)
7526 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
7527 unsigned int alt_count;
7528 unsigned int *rel_count2;
7530 BFD_ASSERT (entsize2 != entsize1
7531 && (entsize2 == bed->s->sizeof_rel
7532 || entsize2 == bed->s->sizeof_rela));
7534 rel_count2 = &esdo->rel_count2;
7535 if (!same_size)
7536 rel_count2 = &esdo->rel_count;
7538 /* The following is probably too simplistic if the
7539 backend counts output relocs unusually. */
7540 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
7541 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
7542 *rel_count2 += alt_count;
7543 reloc_count -= alt_count;
7546 *rel_count1 += reloc_count;
7549 if (o->reloc_count > 0)
7550 o->flags |= SEC_RELOC;
7551 else
7553 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7554 set it (this is probably a bug) and if it is set
7555 assign_section_numbers will create a reloc section. */
7556 o->flags &=~ SEC_RELOC;
7559 /* If the SEC_ALLOC flag is not set, force the section VMA to
7560 zero. This is done in elf_fake_sections as well, but forcing
7561 the VMA to 0 here will ensure that relocs against these
7562 sections are handled correctly. */
7563 if ((o->flags & SEC_ALLOC) == 0
7564 && ! o->user_set_vma)
7565 o->vma = 0;
7568 if (! info->relocatable && merged)
7569 elf_link_hash_traverse (elf_hash_table (info),
7570 _bfd_elf_link_sec_merge_syms, abfd);
7572 /* Figure out the file positions for everything but the symbol table
7573 and the relocs. We set symcount to force assign_section_numbers
7574 to create a symbol table. */
7575 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
7576 BFD_ASSERT (! abfd->output_has_begun);
7577 if (! _bfd_elf_compute_section_file_positions (abfd, info))
7578 goto error_return;
7580 /* That created the reloc sections. Set their sizes, and assign
7581 them file positions, and allocate some buffers. */
7582 for (o = abfd->sections; o != NULL; o = o->next)
7584 if ((o->flags & SEC_RELOC) != 0)
7586 if (!(_bfd_elf_link_size_reloc_section
7587 (abfd, &elf_section_data (o)->rel_hdr, o)))
7588 goto error_return;
7590 if (elf_section_data (o)->rel_hdr2
7591 && !(_bfd_elf_link_size_reloc_section
7592 (abfd, elf_section_data (o)->rel_hdr2, o)))
7593 goto error_return;
7596 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
7597 to count upwards while actually outputting the relocations. */
7598 elf_section_data (o)->rel_count = 0;
7599 elf_section_data (o)->rel_count2 = 0;
7602 _bfd_elf_assign_file_positions_for_relocs (abfd);
7604 /* We have now assigned file positions for all the sections except
7605 .symtab and .strtab. We start the .symtab section at the current
7606 file position, and write directly to it. We build the .strtab
7607 section in memory. */
7608 bfd_get_symcount (abfd) = 0;
7609 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7610 /* sh_name is set in prep_headers. */
7611 symtab_hdr->sh_type = SHT_SYMTAB;
7612 /* sh_flags, sh_addr and sh_size all start off zero. */
7613 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
7614 /* sh_link is set in assign_section_numbers. */
7615 /* sh_info is set below. */
7616 /* sh_offset is set just below. */
7617 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
7619 off = elf_tdata (abfd)->next_file_pos;
7620 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
7622 /* Note that at this point elf_tdata (abfd)->next_file_pos is
7623 incorrect. We do not yet know the size of the .symtab section.
7624 We correct next_file_pos below, after we do know the size. */
7626 /* Allocate a buffer to hold swapped out symbols. This is to avoid
7627 continuously seeking to the right position in the file. */
7628 if (! info->keep_memory || max_sym_count < 20)
7629 finfo.symbuf_size = 20;
7630 else
7631 finfo.symbuf_size = max_sym_count;
7632 amt = finfo.symbuf_size;
7633 amt *= bed->s->sizeof_sym;
7634 finfo.symbuf = bfd_malloc (amt);
7635 if (finfo.symbuf == NULL)
7636 goto error_return;
7637 if (elf_numsections (abfd) > SHN_LORESERVE)
7639 /* Wild guess at number of output symbols. realloc'd as needed. */
7640 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
7641 finfo.shndxbuf_size = amt;
7642 amt *= sizeof (Elf_External_Sym_Shndx);
7643 finfo.symshndxbuf = bfd_zmalloc (amt);
7644 if (finfo.symshndxbuf == NULL)
7645 goto error_return;
7648 /* Start writing out the symbol table. The first symbol is always a
7649 dummy symbol. */
7650 if (info->strip != strip_all
7651 || emit_relocs)
7653 elfsym.st_value = 0;
7654 elfsym.st_size = 0;
7655 elfsym.st_info = 0;
7656 elfsym.st_other = 0;
7657 elfsym.st_shndx = SHN_UNDEF;
7658 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
7659 NULL))
7660 goto error_return;
7663 #if 0
7664 /* Some standard ELF linkers do this, but we don't because it causes
7665 bootstrap comparison failures. */
7666 /* Output a file symbol for the output file as the second symbol.
7667 We output this even if we are discarding local symbols, although
7668 I'm not sure if this is correct. */
7669 elfsym.st_value = 0;
7670 elfsym.st_size = 0;
7671 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
7672 elfsym.st_other = 0;
7673 elfsym.st_shndx = SHN_ABS;
7674 if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd),
7675 &elfsym, bfd_abs_section_ptr, NULL))
7676 goto error_return;
7677 #endif
7679 /* Output a symbol for each section. We output these even if we are
7680 discarding local symbols, since they are used for relocs. These
7681 symbols have no names. We store the index of each one in the
7682 index field of the section, so that we can find it again when
7683 outputting relocs. */
7684 if (info->strip != strip_all
7685 || emit_relocs)
7687 elfsym.st_size = 0;
7688 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
7689 elfsym.st_other = 0;
7690 for (i = 1; i < elf_numsections (abfd); i++)
7692 o = bfd_section_from_elf_index (abfd, i);
7693 if (o != NULL)
7694 o->target_index = bfd_get_symcount (abfd);
7695 elfsym.st_shndx = i;
7696 if (info->relocatable || o == NULL)
7697 elfsym.st_value = 0;
7698 else
7699 elfsym.st_value = o->vma;
7700 if (! elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
7701 goto error_return;
7702 if (i == SHN_LORESERVE - 1)
7703 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
7707 /* Allocate some memory to hold information read in from the input
7708 files. */
7709 if (max_contents_size != 0)
7711 finfo.contents = bfd_malloc (max_contents_size);
7712 if (finfo.contents == NULL)
7713 goto error_return;
7716 if (max_external_reloc_size != 0)
7718 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
7719 if (finfo.external_relocs == NULL)
7720 goto error_return;
7723 if (max_internal_reloc_count != 0)
7725 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
7726 amt *= sizeof (Elf_Internal_Rela);
7727 finfo.internal_relocs = bfd_malloc (amt);
7728 if (finfo.internal_relocs == NULL)
7729 goto error_return;
7732 if (max_sym_count != 0)
7734 amt = max_sym_count * bed->s->sizeof_sym;
7735 finfo.external_syms = bfd_malloc (amt);
7736 if (finfo.external_syms == NULL)
7737 goto error_return;
7739 amt = max_sym_count * sizeof (Elf_Internal_Sym);
7740 finfo.internal_syms = bfd_malloc (amt);
7741 if (finfo.internal_syms == NULL)
7742 goto error_return;
7744 amt = max_sym_count * sizeof (long);
7745 finfo.indices = bfd_malloc (amt);
7746 if (finfo.indices == NULL)
7747 goto error_return;
7749 amt = max_sym_count * sizeof (asection *);
7750 finfo.sections = bfd_malloc (amt);
7751 if (finfo.sections == NULL)
7752 goto error_return;
7755 if (max_sym_shndx_count != 0)
7757 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
7758 finfo.locsym_shndx = bfd_malloc (amt);
7759 if (finfo.locsym_shndx == NULL)
7760 goto error_return;
7763 if (elf_hash_table (info)->tls_sec)
7765 bfd_vma base, end = 0;
7766 asection *sec;
7768 for (sec = elf_hash_table (info)->tls_sec;
7769 sec && (sec->flags & SEC_THREAD_LOCAL);
7770 sec = sec->next)
7772 bfd_vma size = sec->size;
7774 if (size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0)
7776 struct bfd_link_order *o;
7778 for (o = sec->link_order_head; o != NULL; o = o->next)
7779 if (size < o->offset + o->size)
7780 size = o->offset + o->size;
7782 end = sec->vma + size;
7784 base = elf_hash_table (info)->tls_sec->vma;
7785 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
7786 elf_hash_table (info)->tls_size = end - base;
7789 /* Reorder SHF_LINK_ORDER sections. */
7790 for (o = abfd->sections; o != NULL; o = o->next)
7792 if (!elf_fixup_link_order (abfd, o))
7793 return FALSE;
7796 /* Since ELF permits relocations to be against local symbols, we
7797 must have the local symbols available when we do the relocations.
7798 Since we would rather only read the local symbols once, and we
7799 would rather not keep them in memory, we handle all the
7800 relocations for a single input file at the same time.
7802 Unfortunately, there is no way to know the total number of local
7803 symbols until we have seen all of them, and the local symbol
7804 indices precede the global symbol indices. This means that when
7805 we are generating relocatable output, and we see a reloc against
7806 a global symbol, we can not know the symbol index until we have
7807 finished examining all the local symbols to see which ones we are
7808 going to output. To deal with this, we keep the relocations in
7809 memory, and don't output them until the end of the link. This is
7810 an unfortunate waste of memory, but I don't see a good way around
7811 it. Fortunately, it only happens when performing a relocatable
7812 link, which is not the common case. FIXME: If keep_memory is set
7813 we could write the relocs out and then read them again; I don't
7814 know how bad the memory loss will be. */
7816 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
7817 sub->output_has_begun = FALSE;
7818 for (o = abfd->sections; o != NULL; o = o->next)
7820 for (p = o->link_order_head; p != NULL; p = p->next)
7822 if (p->type == bfd_indirect_link_order
7823 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
7824 == bfd_target_elf_flavour)
7825 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
7827 if (! sub->output_has_begun)
7829 if (! elf_link_input_bfd (&finfo, sub))
7830 goto error_return;
7831 sub->output_has_begun = TRUE;
7834 else if (p->type == bfd_section_reloc_link_order
7835 || p->type == bfd_symbol_reloc_link_order)
7837 if (! elf_reloc_link_order (abfd, info, o, p))
7838 goto error_return;
7840 else
7842 if (! _bfd_default_link_order (abfd, info, o, p))
7843 goto error_return;
7848 /* Output any global symbols that got converted to local in a
7849 version script or due to symbol visibility. We do this in a
7850 separate step since ELF requires all local symbols to appear
7851 prior to any global symbols. FIXME: We should only do this if
7852 some global symbols were, in fact, converted to become local.
7853 FIXME: Will this work correctly with the Irix 5 linker? */
7854 eoinfo.failed = FALSE;
7855 eoinfo.finfo = &finfo;
7856 eoinfo.localsyms = TRUE;
7857 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
7858 &eoinfo);
7859 if (eoinfo.failed)
7860 return FALSE;
7862 /* That wrote out all the local symbols. Finish up the symbol table
7863 with the global symbols. Even if we want to strip everything we
7864 can, we still need to deal with those global symbols that got
7865 converted to local in a version script. */
7867 /* The sh_info field records the index of the first non local symbol. */
7868 symtab_hdr->sh_info = bfd_get_symcount (abfd);
7870 if (dynamic
7871 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
7873 Elf_Internal_Sym sym;
7874 bfd_byte *dynsym = finfo.dynsym_sec->contents;
7875 long last_local = 0;
7877 /* Write out the section symbols for the output sections. */
7878 if (info->shared)
7880 asection *s;
7882 sym.st_size = 0;
7883 sym.st_name = 0;
7884 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
7885 sym.st_other = 0;
7887 for (s = abfd->sections; s != NULL; s = s->next)
7889 int indx;
7890 bfd_byte *dest;
7891 long dynindx;
7893 dynindx = elf_section_data (s)->dynindx;
7894 if (dynindx <= 0)
7895 continue;
7896 indx = elf_section_data (s)->this_idx;
7897 BFD_ASSERT (indx > 0);
7898 sym.st_shndx = indx;
7899 sym.st_value = s->vma;
7900 dest = dynsym + dynindx * bed->s->sizeof_sym;
7901 if (last_local < dynindx)
7902 last_local = dynindx;
7903 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
7907 /* Write out the local dynsyms. */
7908 if (elf_hash_table (info)->dynlocal)
7910 struct elf_link_local_dynamic_entry *e;
7911 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
7913 asection *s;
7914 bfd_byte *dest;
7916 sym.st_size = e->isym.st_size;
7917 sym.st_other = e->isym.st_other;
7919 /* Copy the internal symbol as is.
7920 Note that we saved a word of storage and overwrote
7921 the original st_name with the dynstr_index. */
7922 sym = e->isym;
7924 if (e->isym.st_shndx != SHN_UNDEF
7925 && (e->isym.st_shndx < SHN_LORESERVE
7926 || e->isym.st_shndx > SHN_HIRESERVE))
7928 s = bfd_section_from_elf_index (e->input_bfd,
7929 e->isym.st_shndx);
7931 sym.st_shndx =
7932 elf_section_data (s->output_section)->this_idx;
7933 sym.st_value = (s->output_section->vma
7934 + s->output_offset
7935 + e->isym.st_value);
7938 if (last_local < e->dynindx)
7939 last_local = e->dynindx;
7941 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
7942 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
7946 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
7947 last_local + 1;
7950 /* We get the global symbols from the hash table. */
7951 eoinfo.failed = FALSE;
7952 eoinfo.localsyms = FALSE;
7953 eoinfo.finfo = &finfo;
7954 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
7955 &eoinfo);
7956 if (eoinfo.failed)
7957 return FALSE;
7959 /* If backend needs to output some symbols not present in the hash
7960 table, do it now. */
7961 if (bed->elf_backend_output_arch_syms)
7963 typedef bfd_boolean (*out_sym_func)
7964 (void *, const char *, Elf_Internal_Sym *, asection *,
7965 struct elf_link_hash_entry *);
7967 if (! ((*bed->elf_backend_output_arch_syms)
7968 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
7969 return FALSE;
7972 /* Flush all symbols to the file. */
7973 if (! elf_link_flush_output_syms (&finfo, bed))
7974 return FALSE;
7976 /* Now we know the size of the symtab section. */
7977 off += symtab_hdr->sh_size;
7979 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
7980 if (symtab_shndx_hdr->sh_name != 0)
7982 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
7983 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
7984 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
7985 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
7986 symtab_shndx_hdr->sh_size = amt;
7988 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
7989 off, TRUE);
7991 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
7992 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
7993 return FALSE;
7997 /* Finish up and write out the symbol string table (.strtab)
7998 section. */
7999 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8000 /* sh_name was set in prep_headers. */
8001 symstrtab_hdr->sh_type = SHT_STRTAB;
8002 symstrtab_hdr->sh_flags = 0;
8003 symstrtab_hdr->sh_addr = 0;
8004 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
8005 symstrtab_hdr->sh_entsize = 0;
8006 symstrtab_hdr->sh_link = 0;
8007 symstrtab_hdr->sh_info = 0;
8008 /* sh_offset is set just below. */
8009 symstrtab_hdr->sh_addralign = 1;
8011 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
8012 elf_tdata (abfd)->next_file_pos = off;
8014 if (bfd_get_symcount (abfd) > 0)
8016 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
8017 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
8018 return FALSE;
8021 /* Adjust the relocs to have the correct symbol indices. */
8022 for (o = abfd->sections; o != NULL; o = o->next)
8024 if ((o->flags & SEC_RELOC) == 0)
8025 continue;
8027 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
8028 elf_section_data (o)->rel_count,
8029 elf_section_data (o)->rel_hashes);
8030 if (elf_section_data (o)->rel_hdr2 != NULL)
8031 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
8032 elf_section_data (o)->rel_count2,
8033 (elf_section_data (o)->rel_hashes
8034 + elf_section_data (o)->rel_count));
8036 /* Set the reloc_count field to 0 to prevent write_relocs from
8037 trying to swap the relocs out itself. */
8038 o->reloc_count = 0;
8041 if (dynamic && info->combreloc && dynobj != NULL)
8042 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
8044 /* If we are linking against a dynamic object, or generating a
8045 shared library, finish up the dynamic linking information. */
8046 if (dynamic)
8048 bfd_byte *dyncon, *dynconend;
8050 /* Fix up .dynamic entries. */
8051 o = bfd_get_section_by_name (dynobj, ".dynamic");
8052 BFD_ASSERT (o != NULL);
8054 dyncon = o->contents;
8055 dynconend = o->contents + o->size;
8056 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
8058 Elf_Internal_Dyn dyn;
8059 const char *name;
8060 unsigned int type;
8062 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
8064 switch (dyn.d_tag)
8066 default:
8067 continue;
8068 case DT_NULL:
8069 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
8071 switch (elf_section_data (reldyn)->this_hdr.sh_type)
8073 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
8074 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
8075 default: continue;
8077 dyn.d_un.d_val = relativecount;
8078 relativecount = 0;
8079 break;
8081 continue;
8083 case DT_INIT:
8084 name = info->init_function;
8085 goto get_sym;
8086 case DT_FINI:
8087 name = info->fini_function;
8088 get_sym:
8090 struct elf_link_hash_entry *h;
8092 h = elf_link_hash_lookup (elf_hash_table (info), name,
8093 FALSE, FALSE, TRUE);
8094 if (h != NULL
8095 && (h->root.type == bfd_link_hash_defined
8096 || h->root.type == bfd_link_hash_defweak))
8098 dyn.d_un.d_val = h->root.u.def.value;
8099 o = h->root.u.def.section;
8100 if (o->output_section != NULL)
8101 dyn.d_un.d_val += (o->output_section->vma
8102 + o->output_offset);
8103 else
8105 /* The symbol is imported from another shared
8106 library and does not apply to this one. */
8107 dyn.d_un.d_val = 0;
8109 break;
8112 continue;
8114 case DT_PREINIT_ARRAYSZ:
8115 name = ".preinit_array";
8116 goto get_size;
8117 case DT_INIT_ARRAYSZ:
8118 name = ".init_array";
8119 goto get_size;
8120 case DT_FINI_ARRAYSZ:
8121 name = ".fini_array";
8122 get_size:
8123 o = bfd_get_section_by_name (abfd, name);
8124 if (o == NULL)
8126 (*_bfd_error_handler)
8127 (_("%B: could not find output section %s"), abfd, name);
8128 goto error_return;
8130 if (o->size == 0)
8131 (*_bfd_error_handler)
8132 (_("warning: %s section has zero size"), name);
8133 dyn.d_un.d_val = o->size;
8134 break;
8136 case DT_PREINIT_ARRAY:
8137 name = ".preinit_array";
8138 goto get_vma;
8139 case DT_INIT_ARRAY:
8140 name = ".init_array";
8141 goto get_vma;
8142 case DT_FINI_ARRAY:
8143 name = ".fini_array";
8144 goto get_vma;
8146 case DT_HASH:
8147 name = ".hash";
8148 goto get_vma;
8149 case DT_STRTAB:
8150 name = ".dynstr";
8151 goto get_vma;
8152 case DT_SYMTAB:
8153 name = ".dynsym";
8154 goto get_vma;
8155 case DT_VERDEF:
8156 name = ".gnu.version_d";
8157 goto get_vma;
8158 case DT_VERNEED:
8159 name = ".gnu.version_r";
8160 goto get_vma;
8161 case DT_VERSYM:
8162 name = ".gnu.version";
8163 get_vma:
8164 o = bfd_get_section_by_name (abfd, name);
8165 if (o == NULL)
8167 (*_bfd_error_handler)
8168 (_("%B: could not find output section %s"), abfd, name);
8169 goto error_return;
8171 dyn.d_un.d_ptr = o->vma;
8172 break;
8174 case DT_REL:
8175 case DT_RELA:
8176 case DT_RELSZ:
8177 case DT_RELASZ:
8178 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
8179 type = SHT_REL;
8180 else
8181 type = SHT_RELA;
8182 dyn.d_un.d_val = 0;
8183 for (i = 1; i < elf_numsections (abfd); i++)
8185 Elf_Internal_Shdr *hdr;
8187 hdr = elf_elfsections (abfd)[i];
8188 if (hdr->sh_type == type
8189 && (hdr->sh_flags & SHF_ALLOC) != 0)
8191 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
8192 dyn.d_un.d_val += hdr->sh_size;
8193 else
8195 if (dyn.d_un.d_val == 0
8196 || hdr->sh_addr < dyn.d_un.d_val)
8197 dyn.d_un.d_val = hdr->sh_addr;
8201 break;
8203 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
8207 /* If we have created any dynamic sections, then output them. */
8208 if (dynobj != NULL)
8210 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
8211 goto error_return;
8213 for (o = dynobj->sections; o != NULL; o = o->next)
8215 if ((o->flags & SEC_HAS_CONTENTS) == 0
8216 || o->size == 0
8217 || o->output_section == bfd_abs_section_ptr)
8218 continue;
8219 if ((o->flags & SEC_LINKER_CREATED) == 0)
8221 /* At this point, we are only interested in sections
8222 created by _bfd_elf_link_create_dynamic_sections. */
8223 continue;
8225 if (elf_hash_table (info)->stab_info.stabstr == o)
8226 continue;
8227 if (elf_hash_table (info)->eh_info.hdr_sec == o)
8228 continue;
8229 if ((elf_section_data (o->output_section)->this_hdr.sh_type
8230 != SHT_STRTAB)
8231 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
8233 if (! bfd_set_section_contents (abfd, o->output_section,
8234 o->contents,
8235 (file_ptr) o->output_offset,
8236 o->size))
8237 goto error_return;
8239 else
8241 /* The contents of the .dynstr section are actually in a
8242 stringtab. */
8243 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
8244 if (bfd_seek (abfd, off, SEEK_SET) != 0
8245 || ! _bfd_elf_strtab_emit (abfd,
8246 elf_hash_table (info)->dynstr))
8247 goto error_return;
8252 if (info->relocatable)
8254 bfd_boolean failed = FALSE;
8256 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
8257 if (failed)
8258 goto error_return;
8261 /* If we have optimized stabs strings, output them. */
8262 if (elf_hash_table (info)->stab_info.stabstr != NULL)
8264 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
8265 goto error_return;
8268 if (info->eh_frame_hdr)
8270 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
8271 goto error_return;
8274 if (finfo.symstrtab != NULL)
8275 _bfd_stringtab_free (finfo.symstrtab);
8276 if (finfo.contents != NULL)
8277 free (finfo.contents);
8278 if (finfo.external_relocs != NULL)
8279 free (finfo.external_relocs);
8280 if (finfo.internal_relocs != NULL)
8281 free (finfo.internal_relocs);
8282 if (finfo.external_syms != NULL)
8283 free (finfo.external_syms);
8284 if (finfo.locsym_shndx != NULL)
8285 free (finfo.locsym_shndx);
8286 if (finfo.internal_syms != NULL)
8287 free (finfo.internal_syms);
8288 if (finfo.indices != NULL)
8289 free (finfo.indices);
8290 if (finfo.sections != NULL)
8291 free (finfo.sections);
8292 if (finfo.symbuf != NULL)
8293 free (finfo.symbuf);
8294 if (finfo.symshndxbuf != NULL)
8295 free (finfo.symshndxbuf);
8296 for (o = abfd->sections; o != NULL; o = o->next)
8298 if ((o->flags & SEC_RELOC) != 0
8299 && elf_section_data (o)->rel_hashes != NULL)
8300 free (elf_section_data (o)->rel_hashes);
8303 elf_tdata (abfd)->linker = TRUE;
8305 return TRUE;
8307 error_return:
8308 if (finfo.symstrtab != NULL)
8309 _bfd_stringtab_free (finfo.symstrtab);
8310 if (finfo.contents != NULL)
8311 free (finfo.contents);
8312 if (finfo.external_relocs != NULL)
8313 free (finfo.external_relocs);
8314 if (finfo.internal_relocs != NULL)
8315 free (finfo.internal_relocs);
8316 if (finfo.external_syms != NULL)
8317 free (finfo.external_syms);
8318 if (finfo.locsym_shndx != NULL)
8319 free (finfo.locsym_shndx);
8320 if (finfo.internal_syms != NULL)
8321 free (finfo.internal_syms);
8322 if (finfo.indices != NULL)
8323 free (finfo.indices);
8324 if (finfo.sections != NULL)
8325 free (finfo.sections);
8326 if (finfo.symbuf != NULL)
8327 free (finfo.symbuf);
8328 if (finfo.symshndxbuf != NULL)
8329 free (finfo.symshndxbuf);
8330 for (o = abfd->sections; o != NULL; o = o->next)
8332 if ((o->flags & SEC_RELOC) != 0
8333 && elf_section_data (o)->rel_hashes != NULL)
8334 free (elf_section_data (o)->rel_hashes);
8337 return FALSE;
8340 /* Garbage collect unused sections. */
8342 /* The mark phase of garbage collection. For a given section, mark
8343 it and any sections in this section's group, and all the sections
8344 which define symbols to which it refers. */
8346 typedef asection * (*gc_mark_hook_fn)
8347 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
8348 struct elf_link_hash_entry *, Elf_Internal_Sym *);
8350 bfd_boolean
8351 _bfd_elf_gc_mark (struct bfd_link_info *info,
8352 asection *sec,
8353 gc_mark_hook_fn gc_mark_hook)
8355 bfd_boolean ret;
8356 asection *group_sec;
8358 sec->gc_mark = 1;
8360 /* Mark all the sections in the group. */
8361 group_sec = elf_section_data (sec)->next_in_group;
8362 if (group_sec && !group_sec->gc_mark)
8363 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
8364 return FALSE;
8366 /* Look through the section relocs. */
8367 ret = TRUE;
8368 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
8370 Elf_Internal_Rela *relstart, *rel, *relend;
8371 Elf_Internal_Shdr *symtab_hdr;
8372 struct elf_link_hash_entry **sym_hashes;
8373 size_t nlocsyms;
8374 size_t extsymoff;
8375 bfd *input_bfd = sec->owner;
8376 const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
8377 Elf_Internal_Sym *isym = NULL;
8378 int r_sym_shift;
8380 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8381 sym_hashes = elf_sym_hashes (input_bfd);
8383 /* Read the local symbols. */
8384 if (elf_bad_symtab (input_bfd))
8386 nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym;
8387 extsymoff = 0;
8389 else
8390 extsymoff = nlocsyms = symtab_hdr->sh_info;
8392 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
8393 if (isym == NULL && nlocsyms != 0)
8395 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
8396 NULL, NULL, NULL);
8397 if (isym == NULL)
8398 return FALSE;
8401 /* Read the relocations. */
8402 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
8403 info->keep_memory);
8404 if (relstart == NULL)
8406 ret = FALSE;
8407 goto out1;
8409 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
8411 if (bed->s->arch_size == 32)
8412 r_sym_shift = 8;
8413 else
8414 r_sym_shift = 32;
8416 for (rel = relstart; rel < relend; rel++)
8418 unsigned long r_symndx;
8419 asection *rsec;
8420 struct elf_link_hash_entry *h;
8422 r_symndx = rel->r_info >> r_sym_shift;
8423 if (r_symndx == 0)
8424 continue;
8426 if (r_symndx >= nlocsyms
8427 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
8429 h = sym_hashes[r_symndx - extsymoff];
8430 while (h->root.type == bfd_link_hash_indirect
8431 || h->root.type == bfd_link_hash_warning)
8432 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8433 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
8435 else
8437 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
8440 if (rsec && !rsec->gc_mark)
8442 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
8443 rsec->gc_mark = 1;
8444 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
8446 ret = FALSE;
8447 goto out2;
8452 out2:
8453 if (elf_section_data (sec)->relocs != relstart)
8454 free (relstart);
8455 out1:
8456 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
8458 if (! info->keep_memory)
8459 free (isym);
8460 else
8461 symtab_hdr->contents = (unsigned char *) isym;
8465 return ret;
8468 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8470 static bfd_boolean
8471 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *idxptr)
8473 int *idx = idxptr;
8475 if (h->root.type == bfd_link_hash_warning)
8476 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8478 if (h->dynindx != -1
8479 && ((h->root.type != bfd_link_hash_defined
8480 && h->root.type != bfd_link_hash_defweak)
8481 || h->root.u.def.section->gc_mark))
8482 h->dynindx = (*idx)++;
8484 return TRUE;
8487 /* The sweep phase of garbage collection. Remove all garbage sections. */
8489 typedef bfd_boolean (*gc_sweep_hook_fn)
8490 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
8492 static bfd_boolean
8493 elf_gc_sweep (struct bfd_link_info *info, gc_sweep_hook_fn gc_sweep_hook)
8495 bfd *sub;
8497 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8499 asection *o;
8501 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
8502 continue;
8504 for (o = sub->sections; o != NULL; o = o->next)
8506 /* Keep debug and special sections. */
8507 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
8508 || (o->flags & (SEC_ALLOC | SEC_LOAD)) == 0)
8509 o->gc_mark = 1;
8511 if (o->gc_mark)
8512 continue;
8514 /* Skip sweeping sections already excluded. */
8515 if (o->flags & SEC_EXCLUDE)
8516 continue;
8518 /* Since this is early in the link process, it is simple
8519 to remove a section from the output. */
8520 o->flags |= SEC_EXCLUDE;
8522 /* But we also have to update some of the relocation
8523 info we collected before. */
8524 if (gc_sweep_hook
8525 && (o->flags & SEC_RELOC) && o->reloc_count > 0)
8527 Elf_Internal_Rela *internal_relocs;
8528 bfd_boolean r;
8530 internal_relocs
8531 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
8532 info->keep_memory);
8533 if (internal_relocs == NULL)
8534 return FALSE;
8536 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
8538 if (elf_section_data (o)->relocs != internal_relocs)
8539 free (internal_relocs);
8541 if (!r)
8542 return FALSE;
8547 /* Remove the symbols that were in the swept sections from the dynamic
8548 symbol table. GCFIXME: Anyone know how to get them out of the
8549 static symbol table as well? */
8551 int i = 0;
8553 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, &i);
8555 elf_hash_table (info)->dynsymcount = i;
8558 return TRUE;
8561 /* Propagate collected vtable information. This is called through
8562 elf_link_hash_traverse. */
8564 static bfd_boolean
8565 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
8567 if (h->root.type == bfd_link_hash_warning)
8568 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8570 /* Those that are not vtables. */
8571 if (h->vtable_parent == NULL)
8572 return TRUE;
8574 /* Those vtables that do not have parents, we cannot merge. */
8575 if (h->vtable_parent == (struct elf_link_hash_entry *) -1)
8576 return TRUE;
8578 /* If we've already been done, exit. */
8579 if (h->vtable_entries_used && h->vtable_entries_used[-1])
8580 return TRUE;
8582 /* Make sure the parent's table is up to date. */
8583 elf_gc_propagate_vtable_entries_used (h->vtable_parent, okp);
8585 if (h->vtable_entries_used == NULL)
8587 /* None of this table's entries were referenced. Re-use the
8588 parent's table. */
8589 h->vtable_entries_used = h->vtable_parent->vtable_entries_used;
8590 h->vtable_entries_size = h->vtable_parent->vtable_entries_size;
8592 else
8594 size_t n;
8595 bfd_boolean *cu, *pu;
8597 /* Or the parent's entries into ours. */
8598 cu = h->vtable_entries_used;
8599 cu[-1] = TRUE;
8600 pu = h->vtable_parent->vtable_entries_used;
8601 if (pu != NULL)
8603 const struct elf_backend_data *bed;
8604 unsigned int log_file_align;
8606 bed = get_elf_backend_data (h->root.u.def.section->owner);
8607 log_file_align = bed->s->log_file_align;
8608 n = h->vtable_parent->vtable_entries_size >> log_file_align;
8609 while (n--)
8611 if (*pu)
8612 *cu = TRUE;
8613 pu++;
8614 cu++;
8619 return TRUE;
8622 static bfd_boolean
8623 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
8625 asection *sec;
8626 bfd_vma hstart, hend;
8627 Elf_Internal_Rela *relstart, *relend, *rel;
8628 const struct elf_backend_data *bed;
8629 unsigned int log_file_align;
8631 if (h->root.type == bfd_link_hash_warning)
8632 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8634 /* Take care of both those symbols that do not describe vtables as
8635 well as those that are not loaded. */
8636 if (h->vtable_parent == NULL)
8637 return TRUE;
8639 BFD_ASSERT (h->root.type == bfd_link_hash_defined
8640 || h->root.type == bfd_link_hash_defweak);
8642 sec = h->root.u.def.section;
8643 hstart = h->root.u.def.value;
8644 hend = hstart + h->size;
8646 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
8647 if (!relstart)
8648 return *(bfd_boolean *) okp = FALSE;
8649 bed = get_elf_backend_data (sec->owner);
8650 log_file_align = bed->s->log_file_align;
8652 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
8654 for (rel = relstart; rel < relend; ++rel)
8655 if (rel->r_offset >= hstart && rel->r_offset < hend)
8657 /* If the entry is in use, do nothing. */
8658 if (h->vtable_entries_used
8659 && (rel->r_offset - hstart) < h->vtable_entries_size)
8661 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
8662 if (h->vtable_entries_used[entry])
8663 continue;
8665 /* Otherwise, kill it. */
8666 rel->r_offset = rel->r_info = rel->r_addend = 0;
8669 return TRUE;
8672 /* Mark sections containing dynamically referenced symbols. This is called
8673 through elf_link_hash_traverse. */
8675 static bfd_boolean
8676 elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h,
8677 void *okp ATTRIBUTE_UNUSED)
8679 if (h->root.type == bfd_link_hash_warning)
8680 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8682 if ((h->root.type == bfd_link_hash_defined
8683 || h->root.type == bfd_link_hash_defweak)
8684 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC))
8685 h->root.u.def.section->flags |= SEC_KEEP;
8687 return TRUE;
8690 /* Do mark and sweep of unused sections. */
8692 bfd_boolean
8693 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
8695 bfd_boolean ok = TRUE;
8696 bfd *sub;
8697 asection * (*gc_mark_hook)
8698 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
8699 struct elf_link_hash_entry *h, Elf_Internal_Sym *);
8701 if (!get_elf_backend_data (abfd)->can_gc_sections
8702 || info->relocatable
8703 || info->emitrelocations
8704 || info->shared
8705 || !is_elf_hash_table (info->hash))
8707 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
8708 return TRUE;
8711 /* Apply transitive closure to the vtable entry usage info. */
8712 elf_link_hash_traverse (elf_hash_table (info),
8713 elf_gc_propagate_vtable_entries_used,
8714 &ok);
8715 if (!ok)
8716 return FALSE;
8718 /* Kill the vtable relocations that were not used. */
8719 elf_link_hash_traverse (elf_hash_table (info),
8720 elf_gc_smash_unused_vtentry_relocs,
8721 &ok);
8722 if (!ok)
8723 return FALSE;
8725 /* Mark dynamically referenced symbols. */
8726 if (elf_hash_table (info)->dynamic_sections_created)
8727 elf_link_hash_traverse (elf_hash_table (info),
8728 elf_gc_mark_dynamic_ref_symbol,
8729 &ok);
8730 if (!ok)
8731 return FALSE;
8733 /* Grovel through relocs to find out who stays ... */
8734 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
8735 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8737 asection *o;
8739 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
8740 continue;
8742 for (o = sub->sections; o != NULL; o = o->next)
8744 if (o->flags & SEC_KEEP)
8746 /* _bfd_elf_discard_section_eh_frame knows how to discard
8747 orphaned FDEs so don't mark sections referenced by the
8748 EH frame section. */
8749 if (strcmp (o->name, ".eh_frame") == 0)
8750 o->gc_mark = 1;
8751 else if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
8752 return FALSE;
8757 /* ... and mark SEC_EXCLUDE for those that go. */
8758 if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook))
8759 return FALSE;
8761 return TRUE;
8764 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
8766 bfd_boolean
8767 bfd_elf_gc_record_vtinherit (bfd *abfd,
8768 asection *sec,
8769 struct elf_link_hash_entry *h,
8770 bfd_vma offset)
8772 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
8773 struct elf_link_hash_entry **search, *child;
8774 bfd_size_type extsymcount;
8775 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8777 /* The sh_info field of the symtab header tells us where the
8778 external symbols start. We don't care about the local symbols at
8779 this point. */
8780 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
8781 if (!elf_bad_symtab (abfd))
8782 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
8784 sym_hashes = elf_sym_hashes (abfd);
8785 sym_hashes_end = sym_hashes + extsymcount;
8787 /* Hunt down the child symbol, which is in this section at the same
8788 offset as the relocation. */
8789 for (search = sym_hashes; search != sym_hashes_end; ++search)
8791 if ((child = *search) != NULL
8792 && (child->root.type == bfd_link_hash_defined
8793 || child->root.type == bfd_link_hash_defweak)
8794 && child->root.u.def.section == sec
8795 && child->root.u.def.value == offset)
8796 goto win;
8799 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
8800 abfd, sec, (unsigned long) offset);
8801 bfd_set_error (bfd_error_invalid_operation);
8802 return FALSE;
8804 win:
8805 if (!h)
8807 /* This *should* only be the absolute section. It could potentially
8808 be that someone has defined a non-global vtable though, which
8809 would be bad. It isn't worth paging in the local symbols to be
8810 sure though; that case should simply be handled by the assembler. */
8812 child->vtable_parent = (struct elf_link_hash_entry *) -1;
8814 else
8815 child->vtable_parent = h;
8817 return TRUE;
8820 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
8822 bfd_boolean
8823 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
8824 asection *sec ATTRIBUTE_UNUSED,
8825 struct elf_link_hash_entry *h,
8826 bfd_vma addend)
8828 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8829 unsigned int log_file_align = bed->s->log_file_align;
8831 if (addend >= h->vtable_entries_size)
8833 size_t size, bytes, file_align;
8834 bfd_boolean *ptr = h->vtable_entries_used;
8836 /* While the symbol is undefined, we have to be prepared to handle
8837 a zero size. */
8838 file_align = 1 << log_file_align;
8839 if (h->root.type == bfd_link_hash_undefined)
8840 size = addend + file_align;
8841 else
8843 size = h->size;
8844 if (addend >= size)
8846 /* Oops! We've got a reference past the defined end of
8847 the table. This is probably a bug -- shall we warn? */
8848 size = addend + file_align;
8851 size = (size + file_align - 1) & -file_align;
8853 /* Allocate one extra entry for use as a "done" flag for the
8854 consolidation pass. */
8855 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
8857 if (ptr)
8859 ptr = bfd_realloc (ptr - 1, bytes);
8861 if (ptr != NULL)
8863 size_t oldbytes;
8865 oldbytes = (((h->vtable_entries_size >> log_file_align) + 1)
8866 * sizeof (bfd_boolean));
8867 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
8870 else
8871 ptr = bfd_zmalloc (bytes);
8873 if (ptr == NULL)
8874 return FALSE;
8876 /* And arrange for that done flag to be at index -1. */
8877 h->vtable_entries_used = ptr + 1;
8878 h->vtable_entries_size = size;
8881 h->vtable_entries_used[addend >> log_file_align] = TRUE;
8883 return TRUE;
8886 struct alloc_got_off_arg {
8887 bfd_vma gotoff;
8888 unsigned int got_elt_size;
8891 /* We need a special top-level link routine to convert got reference counts
8892 to real got offsets. */
8894 static bfd_boolean
8895 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
8897 struct alloc_got_off_arg *gofarg = arg;
8899 if (h->root.type == bfd_link_hash_warning)
8900 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8902 if (h->got.refcount > 0)
8904 h->got.offset = gofarg->gotoff;
8905 gofarg->gotoff += gofarg->got_elt_size;
8907 else
8908 h->got.offset = (bfd_vma) -1;
8910 return TRUE;
8913 /* And an accompanying bit to work out final got entry offsets once
8914 we're done. Should be called from final_link. */
8916 bfd_boolean
8917 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
8918 struct bfd_link_info *info)
8920 bfd *i;
8921 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8922 bfd_vma gotoff;
8923 unsigned int got_elt_size = bed->s->arch_size / 8;
8924 struct alloc_got_off_arg gofarg;
8926 if (! is_elf_hash_table (info->hash))
8927 return FALSE;
8929 /* The GOT offset is relative to the .got section, but the GOT header is
8930 put into the .got.plt section, if the backend uses it. */
8931 if (bed->want_got_plt)
8932 gotoff = 0;
8933 else
8934 gotoff = bed->got_header_size;
8936 /* Do the local .got entries first. */
8937 for (i = info->input_bfds; i; i = i->link_next)
8939 bfd_signed_vma *local_got;
8940 bfd_size_type j, locsymcount;
8941 Elf_Internal_Shdr *symtab_hdr;
8943 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
8944 continue;
8946 local_got = elf_local_got_refcounts (i);
8947 if (!local_got)
8948 continue;
8950 symtab_hdr = &elf_tdata (i)->symtab_hdr;
8951 if (elf_bad_symtab (i))
8952 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
8953 else
8954 locsymcount = symtab_hdr->sh_info;
8956 for (j = 0; j < locsymcount; ++j)
8958 if (local_got[j] > 0)
8960 local_got[j] = gotoff;
8961 gotoff += got_elt_size;
8963 else
8964 local_got[j] = (bfd_vma) -1;
8968 /* Then the global .got entries. .plt refcounts are handled by
8969 adjust_dynamic_symbol */
8970 gofarg.gotoff = gotoff;
8971 gofarg.got_elt_size = got_elt_size;
8972 elf_link_hash_traverse (elf_hash_table (info),
8973 elf_gc_allocate_got_offsets,
8974 &gofarg);
8975 return TRUE;
8978 /* Many folk need no more in the way of final link than this, once
8979 got entry reference counting is enabled. */
8981 bfd_boolean
8982 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
8984 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
8985 return FALSE;
8987 /* Invoke the regular ELF backend linker to do all the work. */
8988 return bfd_elf_final_link (abfd, info);
8991 bfd_boolean
8992 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
8994 struct elf_reloc_cookie *rcookie = cookie;
8996 if (rcookie->bad_symtab)
8997 rcookie->rel = rcookie->rels;
8999 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
9001 unsigned long r_symndx;
9003 if (! rcookie->bad_symtab)
9004 if (rcookie->rel->r_offset > offset)
9005 return FALSE;
9006 if (rcookie->rel->r_offset != offset)
9007 continue;
9009 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
9010 if (r_symndx == SHN_UNDEF)
9011 return TRUE;
9013 if (r_symndx >= rcookie->locsymcount
9014 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
9016 struct elf_link_hash_entry *h;
9018 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
9020 while (h->root.type == bfd_link_hash_indirect
9021 || h->root.type == bfd_link_hash_warning)
9022 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9024 if ((h->root.type == bfd_link_hash_defined
9025 || h->root.type == bfd_link_hash_defweak)
9026 && elf_discarded_section (h->root.u.def.section))
9027 return TRUE;
9028 else
9029 return FALSE;
9031 else
9033 /* It's not a relocation against a global symbol,
9034 but it could be a relocation against a local
9035 symbol for a discarded section. */
9036 asection *isec;
9037 Elf_Internal_Sym *isym;
9039 /* Need to: get the symbol; get the section. */
9040 isym = &rcookie->locsyms[r_symndx];
9041 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
9043 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
9044 if (isec != NULL && elf_discarded_section (isec))
9045 return TRUE;
9048 return FALSE;
9050 return FALSE;
9053 /* Discard unneeded references to discarded sections.
9054 Returns TRUE if any section's size was changed. */
9055 /* This function assumes that the relocations are in sorted order,
9056 which is true for all known assemblers. */
9058 bfd_boolean
9059 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
9061 struct elf_reloc_cookie cookie;
9062 asection *stab, *eh;
9063 Elf_Internal_Shdr *symtab_hdr;
9064 const struct elf_backend_data *bed;
9065 bfd *abfd;
9066 unsigned int count;
9067 bfd_boolean ret = FALSE;
9069 if (info->traditional_format
9070 || !is_elf_hash_table (info->hash))
9071 return FALSE;
9073 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
9075 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
9076 continue;
9078 bed = get_elf_backend_data (abfd);
9080 if ((abfd->flags & DYNAMIC) != 0)
9081 continue;
9083 eh = bfd_get_section_by_name (abfd, ".eh_frame");
9084 if (info->relocatable
9085 || (eh != NULL
9086 && (eh->size == 0
9087 || bfd_is_abs_section (eh->output_section))))
9088 eh = NULL;
9090 stab = bfd_get_section_by_name (abfd, ".stab");
9091 if (stab != NULL
9092 && (stab->size == 0
9093 || bfd_is_abs_section (stab->output_section)
9094 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
9095 stab = NULL;
9097 if (stab == NULL
9098 && eh == NULL
9099 && bed->elf_backend_discard_info == NULL)
9100 continue;
9102 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9103 cookie.abfd = abfd;
9104 cookie.sym_hashes = elf_sym_hashes (abfd);
9105 cookie.bad_symtab = elf_bad_symtab (abfd);
9106 if (cookie.bad_symtab)
9108 cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9109 cookie.extsymoff = 0;
9111 else
9113 cookie.locsymcount = symtab_hdr->sh_info;
9114 cookie.extsymoff = symtab_hdr->sh_info;
9117 if (bed->s->arch_size == 32)
9118 cookie.r_sym_shift = 8;
9119 else
9120 cookie.r_sym_shift = 32;
9122 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
9123 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
9125 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9126 cookie.locsymcount, 0,
9127 NULL, NULL, NULL);
9128 if (cookie.locsyms == NULL)
9129 return FALSE;
9132 if (stab != NULL)
9134 cookie.rels = NULL;
9135 count = stab->reloc_count;
9136 if (count != 0)
9137 cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
9138 info->keep_memory);
9139 if (cookie.rels != NULL)
9141 cookie.rel = cookie.rels;
9142 cookie.relend = cookie.rels;
9143 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9144 if (_bfd_discard_section_stabs (abfd, stab,
9145 elf_section_data (stab)->sec_info,
9146 bfd_elf_reloc_symbol_deleted_p,
9147 &cookie))
9148 ret = TRUE;
9149 if (elf_section_data (stab)->relocs != cookie.rels)
9150 free (cookie.rels);
9154 if (eh != NULL)
9156 cookie.rels = NULL;
9157 count = eh->reloc_count;
9158 if (count != 0)
9159 cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
9160 info->keep_memory);
9161 cookie.rel = cookie.rels;
9162 cookie.relend = cookie.rels;
9163 if (cookie.rels != NULL)
9164 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9166 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
9167 bfd_elf_reloc_symbol_deleted_p,
9168 &cookie))
9169 ret = TRUE;
9171 if (cookie.rels != NULL
9172 && elf_section_data (eh)->relocs != cookie.rels)
9173 free (cookie.rels);
9176 if (bed->elf_backend_discard_info != NULL
9177 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
9178 ret = TRUE;
9180 if (cookie.locsyms != NULL
9181 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
9183 if (! info->keep_memory)
9184 free (cookie.locsyms);
9185 else
9186 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
9190 if (info->eh_frame_hdr
9191 && !info->relocatable
9192 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
9193 ret = TRUE;
9195 return ret;
9198 struct already_linked_section
9200 asection *sec;
9201 asection *linked;
9204 /* Check if the member of a single member comdat group matches a
9205 linkonce section and vice versa. */
9206 static bfd_boolean
9207 try_match_symbols_in_sections
9208 (struct bfd_section_already_linked_hash_entry *h, void *info)
9210 struct bfd_section_already_linked *l;
9211 struct already_linked_section *s
9212 = (struct already_linked_section *) info;
9214 if (elf_sec_group (s->sec) == NULL)
9216 /* It is a linkonce section. Try to match it with the member of a
9217 single member comdat group. */
9218 for (l = h->entry; l != NULL; l = l->next)
9219 if ((l->sec->flags & SEC_GROUP))
9221 asection *first = elf_next_in_group (l->sec);
9223 if (first != NULL
9224 && elf_next_in_group (first) == first
9225 && bfd_elf_match_symbols_in_sections (first, s->sec))
9227 s->linked = first;
9228 return FALSE;
9232 else
9234 /* It is the member of a single member comdat group. Try to match
9235 it with a linkonce section. */
9236 for (l = h->entry; l != NULL; l = l->next)
9237 if ((l->sec->flags & SEC_GROUP) == 0
9238 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
9239 && bfd_elf_match_symbols_in_sections (l->sec, s->sec))
9241 s->linked = l->sec;
9242 return FALSE;
9246 return TRUE;
9249 static bfd_boolean
9250 already_linked (asection *sec, asection *group)
9252 struct already_linked_section result;
9254 result.sec = sec;
9255 result.linked = NULL;
9257 bfd_section_already_linked_table_traverse
9258 (try_match_symbols_in_sections, &result);
9260 if (result.linked)
9262 sec->output_section = bfd_abs_section_ptr;
9263 sec->kept_section = result.linked;
9265 /* Also discard the group section. */
9266 if (group)
9267 group->output_section = bfd_abs_section_ptr;
9269 return TRUE;
9272 return FALSE;
9275 void
9276 _bfd_elf_section_already_linked (bfd *abfd, struct bfd_section * sec)
9278 flagword flags;
9279 const char *name;
9280 struct bfd_section_already_linked *l;
9281 struct bfd_section_already_linked_hash_entry *already_linked_list;
9282 asection *group;
9284 /* A single member comdat group section may be discarded by a
9285 linkonce section. See below. */
9286 if (sec->output_section == bfd_abs_section_ptr)
9287 return;
9289 flags = sec->flags;
9291 /* Check if it belongs to a section group. */
9292 group = elf_sec_group (sec);
9294 /* Return if it isn't a linkonce section nor a member of a group. A
9295 comdat group section also has SEC_LINK_ONCE set. */
9296 if ((flags & SEC_LINK_ONCE) == 0 && group == NULL)
9297 return;
9299 if (group)
9301 /* If this is the member of a single member comdat group, check if
9302 the group should be discarded. */
9303 if (elf_next_in_group (sec) == sec
9304 && (group->flags & SEC_LINK_ONCE) != 0)
9305 sec = group;
9306 else
9307 return;
9310 /* FIXME: When doing a relocatable link, we may have trouble
9311 copying relocations in other sections that refer to local symbols
9312 in the section being discarded. Those relocations will have to
9313 be converted somehow; as of this writing I'm not sure that any of
9314 the backends handle that correctly.
9316 It is tempting to instead not discard link once sections when
9317 doing a relocatable link (technically, they should be discarded
9318 whenever we are building constructors). However, that fails,
9319 because the linker winds up combining all the link once sections
9320 into a single large link once section, which defeats the purpose
9321 of having link once sections in the first place.
9323 Also, not merging link once sections in a relocatable link
9324 causes trouble for MIPS ELF, which relies on link once semantics
9325 to handle the .reginfo section correctly. */
9327 name = bfd_get_section_name (abfd, sec);
9329 already_linked_list = bfd_section_already_linked_table_lookup (name);
9331 for (l = already_linked_list->entry; l != NULL; l = l->next)
9333 /* We may have 3 different sections on the list: group section,
9334 comdat section and linkonce section. SEC may be a linkonce or
9335 group section. We match a group section with a group section,
9336 a linkonce section with a linkonce section, and ignore comdat
9337 section. */
9338 if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
9339 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
9341 /* The section has already been linked. See if we should
9342 issue a warning. */
9343 switch (flags & SEC_LINK_DUPLICATES)
9345 default:
9346 abort ();
9348 case SEC_LINK_DUPLICATES_DISCARD:
9349 break;
9351 case SEC_LINK_DUPLICATES_ONE_ONLY:
9352 (*_bfd_error_handler)
9353 (_("%B: ignoring duplicate section `%A'\n"),
9354 abfd, sec);
9355 break;
9357 case SEC_LINK_DUPLICATES_SAME_SIZE:
9358 if (sec->size != l->sec->size)
9359 (*_bfd_error_handler)
9360 (_("%B: duplicate section `%A' has different size\n"),
9361 abfd, sec);
9362 break;
9364 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
9365 if (sec->size != l->sec->size)
9366 (*_bfd_error_handler)
9367 (_("%B: duplicate section `%A' has different size\n"),
9368 abfd, sec);
9369 else if (sec->size != 0)
9371 bfd_byte *sec_contents, *l_sec_contents;
9373 if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
9374 (*_bfd_error_handler)
9375 (_("%B: warning: could not read contents of section `%A'\n"),
9376 abfd, sec);
9377 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
9378 &l_sec_contents))
9379 (*_bfd_error_handler)
9380 (_("%B: warning: could not read contents of section `%A'\n"),
9381 l->sec->owner, l->sec);
9382 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
9383 (*_bfd_error_handler)
9384 (_("%B: warning: duplicate section `%A' has different contents\n"),
9385 abfd, sec);
9387 if (sec_contents)
9388 free (sec_contents);
9389 if (l_sec_contents)
9390 free (l_sec_contents);
9392 break;
9395 /* Set the output_section field so that lang_add_section
9396 does not create a lang_input_section structure for this
9397 section. Since there might be a symbol in the section
9398 being discarded, we must retain a pointer to the section
9399 which we are really going to use. */
9400 sec->output_section = bfd_abs_section_ptr;
9401 sec->kept_section = l->sec;
9403 if (flags & SEC_GROUP)
9405 asection *first = elf_next_in_group (sec);
9406 asection *s = first;
9408 while (s != NULL)
9410 s->output_section = bfd_abs_section_ptr;
9411 /* Record which group discards it. */
9412 s->kept_section = l->sec;
9413 s = elf_next_in_group (s);
9414 /* These lists are circular. */
9415 if (s == first)
9416 break;
9420 return;
9424 if (group)
9426 /* If this is the member of a single member comdat group and the
9427 group hasn't be discarded, we check if it matches a linkonce
9428 section. We only record the discarded comdat group. Otherwise
9429 the undiscarded group will be discarded incorrectly later since
9430 itself has been recorded. */
9431 if (! already_linked (elf_next_in_group (sec), group))
9432 return;
9434 else
9435 /* There is no direct match. But for linkonce section, we should
9436 check if there is a match with comdat group member. We always
9437 record the linkonce section, discarded or not. */
9438 already_linked (sec, group);
9440 /* This is the first section with this name. Record it. */
9441 bfd_section_already_linked_table_insert (already_linked_list, sec);