daily update
[binutils.git] / bfd / elflink.c
blobe4e8dcc721206dfd2cdfa02f2b570d62ab39e969
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 if (info->relocatable)
2944 bfd_set_error (bfd_error_invalid_operation);
2945 else
2946 bfd_set_error (bfd_error_wrong_format);
2947 goto error_return;
2951 /* As a GNU extension, any input sections which are named
2952 .gnu.warning.SYMBOL are treated as warning symbols for the given
2953 symbol. This differs from .gnu.warning sections, which generate
2954 warnings when they are included in an output file. */
2955 if (info->executable)
2957 asection *s;
2959 for (s = abfd->sections; s != NULL; s = s->next)
2961 const char *name;
2963 name = bfd_get_section_name (abfd, s);
2964 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
2966 char *msg;
2967 bfd_size_type sz;
2968 bfd_size_type prefix_len;
2969 const char * gnu_warning_prefix = _("warning: ");
2971 name += sizeof ".gnu.warning." - 1;
2973 /* If this is a shared object, then look up the symbol
2974 in the hash table. If it is there, and it is already
2975 been defined, then we will not be using the entry
2976 from this shared object, so we don't need to warn.
2977 FIXME: If we see the definition in a regular object
2978 later on, we will warn, but we shouldn't. The only
2979 fix is to keep track of what warnings we are supposed
2980 to emit, and then handle them all at the end of the
2981 link. */
2982 if (dynamic)
2984 struct elf_link_hash_entry *h;
2986 h = elf_link_hash_lookup (hash_table, name,
2987 FALSE, FALSE, TRUE);
2989 /* FIXME: What about bfd_link_hash_common? */
2990 if (h != NULL
2991 && (h->root.type == bfd_link_hash_defined
2992 || h->root.type == bfd_link_hash_defweak))
2994 /* We don't want to issue this warning. Clobber
2995 the section size so that the warning does not
2996 get copied into the output file. */
2997 s->size = 0;
2998 continue;
3002 sz = s->size;
3003 prefix_len = strlen (gnu_warning_prefix);
3004 msg = bfd_alloc (abfd, prefix_len + sz + 1);
3005 if (msg == NULL)
3006 goto error_return;
3008 strcpy (msg, gnu_warning_prefix);
3009 if (! bfd_get_section_contents (abfd, s, msg + prefix_len, 0, sz))
3010 goto error_return;
3012 msg[prefix_len + sz] = '\0';
3014 if (! (_bfd_generic_link_add_one_symbol
3015 (info, abfd, name, BSF_WARNING, s, 0, msg,
3016 FALSE, collect, NULL)))
3017 goto error_return;
3019 if (! info->relocatable)
3021 /* Clobber the section size so that the warning does
3022 not get copied into the output file. */
3023 s->size = 0;
3029 add_needed = TRUE;
3030 if (! dynamic)
3032 /* If we are creating a shared library, create all the dynamic
3033 sections immediately. We need to attach them to something,
3034 so we attach them to this BFD, provided it is the right
3035 format. FIXME: If there are no input BFD's of the same
3036 format as the output, we can't make a shared library. */
3037 if (info->shared
3038 && is_elf_hash_table (hash_table)
3039 && hash_table->root.creator == abfd->xvec
3040 && ! hash_table->dynamic_sections_created)
3042 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3043 goto error_return;
3046 else if (!is_elf_hash_table (hash_table))
3047 goto error_return;
3048 else
3050 asection *s;
3051 const char *soname = NULL;
3052 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3053 int ret;
3055 /* ld --just-symbols and dynamic objects don't mix very well.
3056 Test for --just-symbols by looking at info set up by
3057 _bfd_elf_link_just_syms. */
3058 if ((s = abfd->sections) != NULL
3059 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3060 goto error_return;
3062 /* If this dynamic lib was specified on the command line with
3063 --as-needed in effect, then we don't want to add a DT_NEEDED
3064 tag unless the lib is actually used. Similary for libs brought
3065 in by another lib's DT_NEEDED. When --no-add-needed is used
3066 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3067 any dynamic library in DT_NEEDED tags in the dynamic lib at
3068 all. */
3069 add_needed = (elf_dyn_lib_class (abfd)
3070 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3071 | DYN_NO_NEEDED)) == 0;
3073 s = bfd_get_section_by_name (abfd, ".dynamic");
3074 if (s != NULL)
3076 bfd_byte *dynbuf;
3077 bfd_byte *extdyn;
3078 int elfsec;
3079 unsigned long shlink;
3081 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3082 goto error_free_dyn;
3084 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3085 if (elfsec == -1)
3086 goto error_free_dyn;
3087 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3089 for (extdyn = dynbuf;
3090 extdyn < dynbuf + s->size;
3091 extdyn += bed->s->sizeof_dyn)
3093 Elf_Internal_Dyn dyn;
3095 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3096 if (dyn.d_tag == DT_SONAME)
3098 unsigned int tagv = dyn.d_un.d_val;
3099 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3100 if (soname == NULL)
3101 goto error_free_dyn;
3103 if (dyn.d_tag == DT_NEEDED)
3105 struct bfd_link_needed_list *n, **pn;
3106 char *fnm, *anm;
3107 unsigned int tagv = dyn.d_un.d_val;
3109 amt = sizeof (struct bfd_link_needed_list);
3110 n = bfd_alloc (abfd, amt);
3111 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3112 if (n == NULL || fnm == NULL)
3113 goto error_free_dyn;
3114 amt = strlen (fnm) + 1;
3115 anm = bfd_alloc (abfd, amt);
3116 if (anm == NULL)
3117 goto error_free_dyn;
3118 memcpy (anm, fnm, amt);
3119 n->name = anm;
3120 n->by = abfd;
3121 n->next = NULL;
3122 for (pn = & hash_table->needed;
3123 *pn != NULL;
3124 pn = &(*pn)->next)
3126 *pn = n;
3128 if (dyn.d_tag == DT_RUNPATH)
3130 struct bfd_link_needed_list *n, **pn;
3131 char *fnm, *anm;
3132 unsigned int tagv = dyn.d_un.d_val;
3134 amt = sizeof (struct bfd_link_needed_list);
3135 n = bfd_alloc (abfd, amt);
3136 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3137 if (n == NULL || fnm == NULL)
3138 goto error_free_dyn;
3139 amt = strlen (fnm) + 1;
3140 anm = bfd_alloc (abfd, amt);
3141 if (anm == NULL)
3142 goto error_free_dyn;
3143 memcpy (anm, fnm, amt);
3144 n->name = anm;
3145 n->by = abfd;
3146 n->next = NULL;
3147 for (pn = & runpath;
3148 *pn != NULL;
3149 pn = &(*pn)->next)
3151 *pn = n;
3153 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3154 if (!runpath && dyn.d_tag == DT_RPATH)
3156 struct bfd_link_needed_list *n, **pn;
3157 char *fnm, *anm;
3158 unsigned int tagv = dyn.d_un.d_val;
3160 amt = sizeof (struct bfd_link_needed_list);
3161 n = bfd_alloc (abfd, amt);
3162 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3163 if (n == NULL || fnm == NULL)
3164 goto error_free_dyn;
3165 amt = strlen (fnm) + 1;
3166 anm = bfd_alloc (abfd, amt);
3167 if (anm == NULL)
3169 error_free_dyn:
3170 free (dynbuf);
3171 goto error_return;
3173 memcpy (anm, fnm, amt);
3174 n->name = anm;
3175 n->by = abfd;
3176 n->next = NULL;
3177 for (pn = & rpath;
3178 *pn != NULL;
3179 pn = &(*pn)->next)
3181 *pn = n;
3185 free (dynbuf);
3188 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3189 frees all more recently bfd_alloc'd blocks as well. */
3190 if (runpath)
3191 rpath = runpath;
3193 if (rpath)
3195 struct bfd_link_needed_list **pn;
3196 for (pn = & hash_table->runpath;
3197 *pn != NULL;
3198 pn = &(*pn)->next)
3200 *pn = rpath;
3203 /* We do not want to include any of the sections in a dynamic
3204 object in the output file. We hack by simply clobbering the
3205 list of sections in the BFD. This could be handled more
3206 cleanly by, say, a new section flag; the existing
3207 SEC_NEVER_LOAD flag is not the one we want, because that one
3208 still implies that the section takes up space in the output
3209 file. */
3210 bfd_section_list_clear (abfd);
3212 /* If this is the first dynamic object found in the link, create
3213 the special sections required for dynamic linking. */
3214 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3215 goto error_return;
3217 /* Find the name to use in a DT_NEEDED entry that refers to this
3218 object. If the object has a DT_SONAME entry, we use it.
3219 Otherwise, if the generic linker stuck something in
3220 elf_dt_name, we use that. Otherwise, we just use the file
3221 name. */
3222 if (soname == NULL || *soname == '\0')
3224 soname = elf_dt_name (abfd);
3225 if (soname == NULL || *soname == '\0')
3226 soname = bfd_get_filename (abfd);
3229 /* Save the SONAME because sometimes the linker emulation code
3230 will need to know it. */
3231 elf_dt_name (abfd) = soname;
3233 ret = elf_add_dt_needed_tag (info, soname, add_needed);
3234 if (ret < 0)
3235 goto error_return;
3237 /* If we have already included this dynamic object in the
3238 link, just ignore it. There is no reason to include a
3239 particular dynamic object more than once. */
3240 if (ret > 0)
3241 return TRUE;
3244 /* If this is a dynamic object, we always link against the .dynsym
3245 symbol table, not the .symtab symbol table. The dynamic linker
3246 will only see the .dynsym symbol table, so there is no reason to
3247 look at .symtab for a dynamic object. */
3249 if (! dynamic || elf_dynsymtab (abfd) == 0)
3250 hdr = &elf_tdata (abfd)->symtab_hdr;
3251 else
3252 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3254 symcount = hdr->sh_size / bed->s->sizeof_sym;
3256 /* The sh_info field of the symtab header tells us where the
3257 external symbols start. We don't care about the local symbols at
3258 this point. */
3259 if (elf_bad_symtab (abfd))
3261 extsymcount = symcount;
3262 extsymoff = 0;
3264 else
3266 extsymcount = symcount - hdr->sh_info;
3267 extsymoff = hdr->sh_info;
3270 sym_hash = NULL;
3271 if (extsymcount != 0)
3273 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3274 NULL, NULL, NULL);
3275 if (isymbuf == NULL)
3276 goto error_return;
3278 /* We store a pointer to the hash table entry for each external
3279 symbol. */
3280 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3281 sym_hash = bfd_alloc (abfd, amt);
3282 if (sym_hash == NULL)
3283 goto error_free_sym;
3284 elf_sym_hashes (abfd) = sym_hash;
3287 if (dynamic)
3289 /* Read in any version definitions. */
3290 if (! _bfd_elf_slurp_version_tables (abfd))
3291 goto error_free_sym;
3293 /* Read in the symbol versions, but don't bother to convert them
3294 to internal format. */
3295 if (elf_dynversym (abfd) != 0)
3297 Elf_Internal_Shdr *versymhdr;
3299 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3300 extversym = bfd_malloc (versymhdr->sh_size);
3301 if (extversym == NULL)
3302 goto error_free_sym;
3303 amt = versymhdr->sh_size;
3304 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3305 || bfd_bread (extversym, amt, abfd) != amt)
3306 goto error_free_vers;
3310 weaks = NULL;
3312 ever = extversym != NULL ? extversym + extsymoff : NULL;
3313 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3314 isym < isymend;
3315 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3317 int bind;
3318 bfd_vma value;
3319 asection *sec;
3320 flagword flags;
3321 const char *name;
3322 struct elf_link_hash_entry *h;
3323 bfd_boolean definition;
3324 bfd_boolean size_change_ok;
3325 bfd_boolean type_change_ok;
3326 bfd_boolean new_weakdef;
3327 bfd_boolean override;
3328 unsigned int old_alignment;
3329 bfd *old_bfd;
3331 override = FALSE;
3333 flags = BSF_NO_FLAGS;
3334 sec = NULL;
3335 value = isym->st_value;
3336 *sym_hash = NULL;
3338 bind = ELF_ST_BIND (isym->st_info);
3339 if (bind == STB_LOCAL)
3341 /* This should be impossible, since ELF requires that all
3342 global symbols follow all local symbols, and that sh_info
3343 point to the first global symbol. Unfortunately, Irix 5
3344 screws this up. */
3345 continue;
3347 else if (bind == STB_GLOBAL)
3349 if (isym->st_shndx != SHN_UNDEF
3350 && isym->st_shndx != SHN_COMMON)
3351 flags = BSF_GLOBAL;
3353 else if (bind == STB_WEAK)
3354 flags = BSF_WEAK;
3355 else
3357 /* Leave it up to the processor backend. */
3360 if (isym->st_shndx == SHN_UNDEF)
3361 sec = bfd_und_section_ptr;
3362 else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
3364 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3365 if (sec == NULL)
3366 sec = bfd_abs_section_ptr;
3367 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3368 value -= sec->vma;
3370 else if (isym->st_shndx == SHN_ABS)
3371 sec = bfd_abs_section_ptr;
3372 else if (isym->st_shndx == SHN_COMMON)
3374 sec = bfd_com_section_ptr;
3375 /* What ELF calls the size we call the value. What ELF
3376 calls the value we call the alignment. */
3377 value = isym->st_size;
3379 else
3381 /* Leave it up to the processor backend. */
3384 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3385 isym->st_name);
3386 if (name == NULL)
3387 goto error_free_vers;
3389 if (isym->st_shndx == SHN_COMMON
3390 && ELF_ST_TYPE (isym->st_info) == STT_TLS)
3392 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3394 if (tcomm == NULL)
3396 tcomm = bfd_make_section (abfd, ".tcommon");
3397 if (tcomm == NULL
3398 || !bfd_set_section_flags (abfd, tcomm, (SEC_ALLOC
3399 | SEC_IS_COMMON
3400 | SEC_LINKER_CREATED
3401 | SEC_THREAD_LOCAL)))
3402 goto error_free_vers;
3404 sec = tcomm;
3406 else if (add_symbol_hook)
3408 if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec,
3409 &value))
3410 goto error_free_vers;
3412 /* The hook function sets the name to NULL if this symbol
3413 should be skipped for some reason. */
3414 if (name == NULL)
3415 continue;
3418 /* Sanity check that all possibilities were handled. */
3419 if (sec == NULL)
3421 bfd_set_error (bfd_error_bad_value);
3422 goto error_free_vers;
3425 if (bfd_is_und_section (sec)
3426 || bfd_is_com_section (sec))
3427 definition = FALSE;
3428 else
3429 definition = TRUE;
3431 size_change_ok = FALSE;
3432 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
3433 old_alignment = 0;
3434 old_bfd = NULL;
3436 if (is_elf_hash_table (hash_table))
3438 Elf_Internal_Versym iver;
3439 unsigned int vernum = 0;
3440 bfd_boolean skip;
3442 if (ever != NULL)
3444 _bfd_elf_swap_versym_in (abfd, ever, &iver);
3445 vernum = iver.vs_vers & VERSYM_VERSION;
3447 /* If this is a hidden symbol, or if it is not version
3448 1, we append the version name to the symbol name.
3449 However, we do not modify a non-hidden absolute
3450 symbol, because it might be the version symbol
3451 itself. FIXME: What if it isn't? */
3452 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
3453 || (vernum > 1 && ! bfd_is_abs_section (sec)))
3455 const char *verstr;
3456 size_t namelen, verlen, newlen;
3457 char *newname, *p;
3459 if (isym->st_shndx != SHN_UNDEF)
3461 if (vernum > elf_tdata (abfd)->dynverdef_hdr.sh_info)
3463 (*_bfd_error_handler)
3464 (_("%B: %s: invalid version %u (max %d)"),
3465 abfd, name, vernum,
3466 elf_tdata (abfd)->dynverdef_hdr.sh_info);
3467 bfd_set_error (bfd_error_bad_value);
3468 goto error_free_vers;
3470 else if (vernum > 1)
3471 verstr =
3472 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
3473 else
3474 verstr = "";
3476 else
3478 /* We cannot simply test for the number of
3479 entries in the VERNEED section since the
3480 numbers for the needed versions do not start
3481 at 0. */
3482 Elf_Internal_Verneed *t;
3484 verstr = NULL;
3485 for (t = elf_tdata (abfd)->verref;
3486 t != NULL;
3487 t = t->vn_nextref)
3489 Elf_Internal_Vernaux *a;
3491 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3493 if (a->vna_other == vernum)
3495 verstr = a->vna_nodename;
3496 break;
3499 if (a != NULL)
3500 break;
3502 if (verstr == NULL)
3504 (*_bfd_error_handler)
3505 (_("%B: %s: invalid needed version %d"),
3506 abfd, name, vernum);
3507 bfd_set_error (bfd_error_bad_value);
3508 goto error_free_vers;
3512 namelen = strlen (name);
3513 verlen = strlen (verstr);
3514 newlen = namelen + verlen + 2;
3515 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3516 && isym->st_shndx != SHN_UNDEF)
3517 ++newlen;
3519 newname = bfd_alloc (abfd, newlen);
3520 if (newname == NULL)
3521 goto error_free_vers;
3522 memcpy (newname, name, namelen);
3523 p = newname + namelen;
3524 *p++ = ELF_VER_CHR;
3525 /* If this is a defined non-hidden version symbol,
3526 we add another @ to the name. This indicates the
3527 default version of the symbol. */
3528 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3529 && isym->st_shndx != SHN_UNDEF)
3530 *p++ = ELF_VER_CHR;
3531 memcpy (p, verstr, verlen + 1);
3533 name = newname;
3537 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
3538 sym_hash, &skip, &override,
3539 &type_change_ok, &size_change_ok))
3540 goto error_free_vers;
3542 if (skip)
3543 continue;
3545 if (override)
3546 definition = FALSE;
3548 h = *sym_hash;
3549 while (h->root.type == bfd_link_hash_indirect
3550 || h->root.type == bfd_link_hash_warning)
3551 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3553 /* Remember the old alignment if this is a common symbol, so
3554 that we don't reduce the alignment later on. We can't
3555 check later, because _bfd_generic_link_add_one_symbol
3556 will set a default for the alignment which we want to
3557 override. We also remember the old bfd where the existing
3558 definition comes from. */
3559 switch (h->root.type)
3561 default:
3562 break;
3564 case bfd_link_hash_defined:
3565 case bfd_link_hash_defweak:
3566 old_bfd = h->root.u.def.section->owner;
3567 break;
3569 case bfd_link_hash_common:
3570 old_bfd = h->root.u.c.p->section->owner;
3571 old_alignment = h->root.u.c.p->alignment_power;
3572 break;
3575 if (elf_tdata (abfd)->verdef != NULL
3576 && ! override
3577 && vernum > 1
3578 && definition)
3579 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
3582 if (! (_bfd_generic_link_add_one_symbol
3583 (info, abfd, name, flags, sec, value, NULL, FALSE, collect,
3584 (struct bfd_link_hash_entry **) sym_hash)))
3585 goto error_free_vers;
3587 h = *sym_hash;
3588 while (h->root.type == bfd_link_hash_indirect
3589 || h->root.type == bfd_link_hash_warning)
3590 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3591 *sym_hash = h;
3593 new_weakdef = FALSE;
3594 if (dynamic
3595 && definition
3596 && (flags & BSF_WEAK) != 0
3597 && ELF_ST_TYPE (isym->st_info) != STT_FUNC
3598 && is_elf_hash_table (hash_table)
3599 && h->weakdef == NULL)
3601 /* Keep a list of all weak defined non function symbols from
3602 a dynamic object, using the weakdef field. Later in this
3603 function we will set the weakdef field to the correct
3604 value. We only put non-function symbols from dynamic
3605 objects on this list, because that happens to be the only
3606 time we need to know the normal symbol corresponding to a
3607 weak symbol, and the information is time consuming to
3608 figure out. If the weakdef field is not already NULL,
3609 then this symbol was already defined by some previous
3610 dynamic object, and we will be using that previous
3611 definition anyhow. */
3613 h->weakdef = weaks;
3614 weaks = h;
3615 new_weakdef = TRUE;
3618 /* Set the alignment of a common symbol. */
3619 if (isym->st_shndx == SHN_COMMON
3620 && h->root.type == bfd_link_hash_common)
3622 unsigned int align;
3624 align = bfd_log2 (isym->st_value);
3625 if (align > old_alignment
3626 /* Permit an alignment power of zero if an alignment of one
3627 is specified and no other alignments have been specified. */
3628 || (isym->st_value == 1 && old_alignment == 0))
3629 h->root.u.c.p->alignment_power = align;
3630 else
3631 h->root.u.c.p->alignment_power = old_alignment;
3634 if (is_elf_hash_table (hash_table))
3636 int old_flags;
3637 bfd_boolean dynsym;
3638 int new_flag;
3640 /* Check the alignment when a common symbol is involved. This
3641 can change when a common symbol is overridden by a normal
3642 definition or a common symbol is ignored due to the old
3643 normal definition. We need to make sure the maximum
3644 alignment is maintained. */
3645 if ((old_alignment || isym->st_shndx == SHN_COMMON)
3646 && h->root.type != bfd_link_hash_common)
3648 unsigned int common_align;
3649 unsigned int normal_align;
3650 unsigned int symbol_align;
3651 bfd *normal_bfd;
3652 bfd *common_bfd;
3654 symbol_align = ffs (h->root.u.def.value) - 1;
3655 if (h->root.u.def.section->owner != NULL
3656 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3658 normal_align = h->root.u.def.section->alignment_power;
3659 if (normal_align > symbol_align)
3660 normal_align = symbol_align;
3662 else
3663 normal_align = symbol_align;
3665 if (old_alignment)
3667 common_align = old_alignment;
3668 common_bfd = old_bfd;
3669 normal_bfd = abfd;
3671 else
3673 common_align = bfd_log2 (isym->st_value);
3674 common_bfd = abfd;
3675 normal_bfd = old_bfd;
3678 if (normal_align < common_align)
3679 (*_bfd_error_handler)
3680 (_("Warning: alignment %u of symbol `%s' in %B"
3681 " is smaller than %u in %B"),
3682 normal_bfd, common_bfd,
3683 1 << normal_align, name, 1 << common_align);
3686 /* Remember the symbol size and type. */
3687 if (isym->st_size != 0
3688 && (definition || h->size == 0))
3690 if (h->size != 0 && h->size != isym->st_size && ! size_change_ok)
3691 (*_bfd_error_handler)
3692 (_("Warning: size of symbol `%s' changed"
3693 " from %lu in %B to %lu in %B"),
3694 old_bfd, abfd,
3695 name, (unsigned long) h->size,
3696 (unsigned long) isym->st_size);
3698 h->size = isym->st_size;
3701 /* If this is a common symbol, then we always want H->SIZE
3702 to be the size of the common symbol. The code just above
3703 won't fix the size if a common symbol becomes larger. We
3704 don't warn about a size change here, because that is
3705 covered by --warn-common. */
3706 if (h->root.type == bfd_link_hash_common)
3707 h->size = h->root.u.c.size;
3709 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
3710 && (definition || h->type == STT_NOTYPE))
3712 if (h->type != STT_NOTYPE
3713 && h->type != ELF_ST_TYPE (isym->st_info)
3714 && ! type_change_ok)
3715 (*_bfd_error_handler)
3716 (_("Warning: type of symbol `%s' changed"
3717 " from %d to %d in %B"),
3718 abfd, name, h->type, ELF_ST_TYPE (isym->st_info));
3720 h->type = ELF_ST_TYPE (isym->st_info);
3723 /* If st_other has a processor-specific meaning, specific
3724 code might be needed here. We never merge the visibility
3725 attribute with the one from a dynamic object. */
3726 if (bed->elf_backend_merge_symbol_attribute)
3727 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
3728 dynamic);
3730 if (isym->st_other != 0 && !dynamic)
3732 unsigned char hvis, symvis, other, nvis;
3734 /* Take the balance of OTHER from the definition. */
3735 other = (definition ? isym->st_other : h->other);
3736 other &= ~ ELF_ST_VISIBILITY (-1);
3738 /* Combine visibilities, using the most constraining one. */
3739 hvis = ELF_ST_VISIBILITY (h->other);
3740 symvis = ELF_ST_VISIBILITY (isym->st_other);
3741 if (! hvis)
3742 nvis = symvis;
3743 else if (! symvis)
3744 nvis = hvis;
3745 else
3746 nvis = hvis < symvis ? hvis : symvis;
3748 h->other = other | nvis;
3751 /* Set a flag in the hash table entry indicating the type of
3752 reference or definition we just found. Keep a count of
3753 the number of dynamic symbols we find. A dynamic symbol
3754 is one which is referenced or defined by both a regular
3755 object and a shared object. */
3756 old_flags = h->elf_link_hash_flags;
3757 dynsym = FALSE;
3758 if (! dynamic)
3760 if (! definition)
3762 new_flag = ELF_LINK_HASH_REF_REGULAR;
3763 if (bind != STB_WEAK)
3764 new_flag |= ELF_LINK_HASH_REF_REGULAR_NONWEAK;
3766 else
3767 new_flag = ELF_LINK_HASH_DEF_REGULAR;
3768 if (! info->executable
3769 || (old_flags & (ELF_LINK_HASH_DEF_DYNAMIC
3770 | ELF_LINK_HASH_REF_DYNAMIC)) != 0)
3771 dynsym = TRUE;
3773 else
3775 if (! definition)
3776 new_flag = ELF_LINK_HASH_REF_DYNAMIC;
3777 else
3778 new_flag = ELF_LINK_HASH_DEF_DYNAMIC;
3779 if ((old_flags & (ELF_LINK_HASH_DEF_REGULAR
3780 | ELF_LINK_HASH_REF_REGULAR)) != 0
3781 || (h->weakdef != NULL
3782 && ! new_weakdef
3783 && h->weakdef->dynindx != -1))
3784 dynsym = TRUE;
3787 h->elf_link_hash_flags |= new_flag;
3789 /* Check to see if we need to add an indirect symbol for
3790 the default name. */
3791 if (definition || h->root.type == bfd_link_hash_common)
3792 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
3793 &sec, &value, &dynsym,
3794 override))
3795 goto error_free_vers;
3797 if (definition && !dynamic)
3799 char *p = strchr (name, ELF_VER_CHR);
3800 if (p != NULL && p[1] != ELF_VER_CHR)
3802 /* Queue non-default versions so that .symver x, x@FOO
3803 aliases can be checked. */
3804 if (! nondeflt_vers)
3806 amt = (isymend - isym + 1)
3807 * sizeof (struct elf_link_hash_entry *);
3808 nondeflt_vers = bfd_malloc (amt);
3810 nondeflt_vers [nondeflt_vers_cnt++] = h;
3814 if (dynsym && h->dynindx == -1)
3816 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3817 goto error_free_vers;
3818 if (h->weakdef != NULL
3819 && ! new_weakdef
3820 && h->weakdef->dynindx == -1)
3822 if (! bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
3823 goto error_free_vers;
3826 else if (dynsym && h->dynindx != -1)
3827 /* If the symbol already has a dynamic index, but
3828 visibility says it should not be visible, turn it into
3829 a local symbol. */
3830 switch (ELF_ST_VISIBILITY (h->other))
3832 case STV_INTERNAL:
3833 case STV_HIDDEN:
3834 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
3835 dynsym = FALSE;
3836 break;
3839 if (!add_needed
3840 && definition
3841 && dynsym
3842 && (h->elf_link_hash_flags
3843 & ELF_LINK_HASH_REF_REGULAR) != 0)
3845 int ret;
3846 const char *soname = elf_dt_name (abfd);
3848 /* A symbol from a library loaded via DT_NEEDED of some
3849 other library is referenced by a regular object.
3850 Add a DT_NEEDED entry for it. Issue an error if
3851 --no-add-needed is used. */
3852 if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
3854 (*_bfd_error_handler)
3855 (_("%s: invalid DSO for symbol `%s' definition"),
3856 abfd, name);
3857 bfd_set_error (bfd_error_bad_value);
3858 goto error_free_vers;
3861 add_needed = TRUE;
3862 ret = elf_add_dt_needed_tag (info, soname, add_needed);
3863 if (ret < 0)
3864 goto error_free_vers;
3866 BFD_ASSERT (ret == 0);
3871 /* Now that all the symbols from this input file are created, handle
3872 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
3873 if (nondeflt_vers != NULL)
3875 bfd_size_type cnt, symidx;
3877 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
3879 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
3880 char *shortname, *p;
3882 p = strchr (h->root.root.string, ELF_VER_CHR);
3883 if (p == NULL
3884 || (h->root.type != bfd_link_hash_defined
3885 && h->root.type != bfd_link_hash_defweak))
3886 continue;
3888 amt = p - h->root.root.string;
3889 shortname = bfd_malloc (amt + 1);
3890 memcpy (shortname, h->root.root.string, amt);
3891 shortname[amt] = '\0';
3893 hi = (struct elf_link_hash_entry *)
3894 bfd_link_hash_lookup (&hash_table->root, shortname,
3895 FALSE, FALSE, FALSE);
3896 if (hi != NULL
3897 && hi->root.type == h->root.type
3898 && hi->root.u.def.value == h->root.u.def.value
3899 && hi->root.u.def.section == h->root.u.def.section)
3901 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
3902 hi->root.type = bfd_link_hash_indirect;
3903 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
3904 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
3905 sym_hash = elf_sym_hashes (abfd);
3906 if (sym_hash)
3907 for (symidx = 0; symidx < extsymcount; ++symidx)
3908 if (sym_hash[symidx] == hi)
3910 sym_hash[symidx] = h;
3911 break;
3914 free (shortname);
3916 free (nondeflt_vers);
3917 nondeflt_vers = NULL;
3920 if (extversym != NULL)
3922 free (extversym);
3923 extversym = NULL;
3926 if (isymbuf != NULL)
3927 free (isymbuf);
3928 isymbuf = NULL;
3930 /* Now set the weakdefs field correctly for all the weak defined
3931 symbols we found. The only way to do this is to search all the
3932 symbols. Since we only need the information for non functions in
3933 dynamic objects, that's the only time we actually put anything on
3934 the list WEAKS. We need this information so that if a regular
3935 object refers to a symbol defined weakly in a dynamic object, the
3936 real symbol in the dynamic object is also put in the dynamic
3937 symbols; we also must arrange for both symbols to point to the
3938 same memory location. We could handle the general case of symbol
3939 aliasing, but a general symbol alias can only be generated in
3940 assembler code, handling it correctly would be very time
3941 consuming, and other ELF linkers don't handle general aliasing
3942 either. */
3943 if (weaks != NULL)
3945 struct elf_link_hash_entry **hpp;
3946 struct elf_link_hash_entry **hppend;
3947 struct elf_link_hash_entry **sorted_sym_hash;
3948 struct elf_link_hash_entry *h;
3949 size_t sym_count;
3951 /* Since we have to search the whole symbol list for each weak
3952 defined symbol, search time for N weak defined symbols will be
3953 O(N^2). Binary search will cut it down to O(NlogN). */
3954 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3955 sorted_sym_hash = bfd_malloc (amt);
3956 if (sorted_sym_hash == NULL)
3957 goto error_return;
3958 sym_hash = sorted_sym_hash;
3959 hpp = elf_sym_hashes (abfd);
3960 hppend = hpp + extsymcount;
3961 sym_count = 0;
3962 for (; hpp < hppend; hpp++)
3964 h = *hpp;
3965 if (h != NULL
3966 && h->root.type == bfd_link_hash_defined
3967 && h->type != STT_FUNC)
3969 *sym_hash = h;
3970 sym_hash++;
3971 sym_count++;
3975 qsort (sorted_sym_hash, sym_count,
3976 sizeof (struct elf_link_hash_entry *),
3977 elf_sort_symbol);
3979 while (weaks != NULL)
3981 struct elf_link_hash_entry *hlook;
3982 asection *slook;
3983 bfd_vma vlook;
3984 long ilook;
3985 size_t i, j, idx;
3987 hlook = weaks;
3988 weaks = hlook->weakdef;
3989 hlook->weakdef = NULL;
3991 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
3992 || hlook->root.type == bfd_link_hash_defweak
3993 || hlook->root.type == bfd_link_hash_common
3994 || hlook->root.type == bfd_link_hash_indirect);
3995 slook = hlook->root.u.def.section;
3996 vlook = hlook->root.u.def.value;
3998 ilook = -1;
3999 i = 0;
4000 j = sym_count;
4001 while (i < j)
4003 bfd_signed_vma vdiff;
4004 idx = (i + j) / 2;
4005 h = sorted_sym_hash [idx];
4006 vdiff = vlook - h->root.u.def.value;
4007 if (vdiff < 0)
4008 j = idx;
4009 else if (vdiff > 0)
4010 i = idx + 1;
4011 else
4013 long sdiff = slook->id - h->root.u.def.section->id;
4014 if (sdiff < 0)
4015 j = idx;
4016 else if (sdiff > 0)
4017 i = idx + 1;
4018 else
4020 ilook = idx;
4021 break;
4026 /* We didn't find a value/section match. */
4027 if (ilook == -1)
4028 continue;
4030 for (i = ilook; i < sym_count; i++)
4032 h = sorted_sym_hash [i];
4034 /* Stop if value or section doesn't match. */
4035 if (h->root.u.def.value != vlook
4036 || h->root.u.def.section != slook)
4037 break;
4038 else if (h != hlook)
4040 hlook->weakdef = h;
4042 /* If the weak definition is in the list of dynamic
4043 symbols, make sure the real definition is put
4044 there as well. */
4045 if (hlook->dynindx != -1 && h->dynindx == -1)
4047 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4048 goto error_return;
4051 /* If the real definition is in the list of dynamic
4052 symbols, make sure the weak definition is put
4053 there as well. If we don't do this, then the
4054 dynamic loader might not merge the entries for the
4055 real definition and the weak definition. */
4056 if (h->dynindx != -1 && hlook->dynindx == -1)
4058 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4059 goto error_return;
4061 break;
4066 free (sorted_sym_hash);
4069 check_directives = get_elf_backend_data (abfd)->check_directives;
4070 if (check_directives)
4071 check_directives (abfd, info);
4073 /* If this object is the same format as the output object, and it is
4074 not a shared library, then let the backend look through the
4075 relocs.
4077 This is required to build global offset table entries and to
4078 arrange for dynamic relocs. It is not required for the
4079 particular common case of linking non PIC code, even when linking
4080 against shared libraries, but unfortunately there is no way of
4081 knowing whether an object file has been compiled PIC or not.
4082 Looking through the relocs is not particularly time consuming.
4083 The problem is that we must either (1) keep the relocs in memory,
4084 which causes the linker to require additional runtime memory or
4085 (2) read the relocs twice from the input file, which wastes time.
4086 This would be a good case for using mmap.
4088 I have no idea how to handle linking PIC code into a file of a
4089 different format. It probably can't be done. */
4090 check_relocs = get_elf_backend_data (abfd)->check_relocs;
4091 if (! dynamic
4092 && is_elf_hash_table (hash_table)
4093 && hash_table->root.creator == abfd->xvec
4094 && check_relocs != NULL)
4096 asection *o;
4098 for (o = abfd->sections; o != NULL; o = o->next)
4100 Elf_Internal_Rela *internal_relocs;
4101 bfd_boolean ok;
4103 if ((o->flags & SEC_RELOC) == 0
4104 || o->reloc_count == 0
4105 || ((info->strip == strip_all || info->strip == strip_debugger)
4106 && (o->flags & SEC_DEBUGGING) != 0)
4107 || bfd_is_abs_section (o->output_section))
4108 continue;
4110 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4111 info->keep_memory);
4112 if (internal_relocs == NULL)
4113 goto error_return;
4115 ok = (*check_relocs) (abfd, info, o, internal_relocs);
4117 if (elf_section_data (o)->relocs != internal_relocs)
4118 free (internal_relocs);
4120 if (! ok)
4121 goto error_return;
4125 /* If this is a non-traditional link, try to optimize the handling
4126 of the .stab/.stabstr sections. */
4127 if (! dynamic
4128 && ! info->traditional_format
4129 && is_elf_hash_table (hash_table)
4130 && (info->strip != strip_all && info->strip != strip_debugger))
4132 asection *stabstr;
4134 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4135 if (stabstr != NULL)
4137 bfd_size_type string_offset = 0;
4138 asection *stab;
4140 for (stab = abfd->sections; stab; stab = stab->next)
4141 if (strncmp (".stab", stab->name, 5) == 0
4142 && (!stab->name[5] ||
4143 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4144 && (stab->flags & SEC_MERGE) == 0
4145 && !bfd_is_abs_section (stab->output_section))
4147 struct bfd_elf_section_data *secdata;
4149 secdata = elf_section_data (stab);
4150 if (! _bfd_link_section_stabs (abfd,
4151 &hash_table->stab_info,
4152 stab, stabstr,
4153 &secdata->sec_info,
4154 &string_offset))
4155 goto error_return;
4156 if (secdata->sec_info)
4157 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4162 if (is_elf_hash_table (hash_table))
4164 /* Add this bfd to the loaded list. */
4165 struct elf_link_loaded_list *n;
4167 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4168 if (n == NULL)
4169 goto error_return;
4170 n->abfd = abfd;
4171 n->next = hash_table->loaded;
4172 hash_table->loaded = n;
4175 return TRUE;
4177 error_free_vers:
4178 if (nondeflt_vers != NULL)
4179 free (nondeflt_vers);
4180 if (extversym != NULL)
4181 free (extversym);
4182 error_free_sym:
4183 if (isymbuf != NULL)
4184 free (isymbuf);
4185 error_return:
4186 return FALSE;
4189 /* Return the linker hash table entry of a symbol that might be
4190 satisfied by an archive symbol. Return -1 on error. */
4192 struct elf_link_hash_entry *
4193 _bfd_elf_archive_symbol_lookup (bfd *abfd,
4194 struct bfd_link_info *info,
4195 const char *name)
4197 struct elf_link_hash_entry *h;
4198 char *p, *copy;
4199 size_t len, first;
4201 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4202 if (h != NULL)
4203 return h;
4205 /* If this is a default version (the name contains @@), look up the
4206 symbol again with only one `@' as well as without the version.
4207 The effect is that references to the symbol with and without the
4208 version will be matched by the default symbol in the archive. */
4210 p = strchr (name, ELF_VER_CHR);
4211 if (p == NULL || p[1] != ELF_VER_CHR)
4212 return h;
4214 /* First check with only one `@'. */
4215 len = strlen (name);
4216 copy = bfd_alloc (abfd, len);
4217 if (copy == NULL)
4218 return (struct elf_link_hash_entry *) 0 - 1;
4220 first = p - name + 1;
4221 memcpy (copy, name, first);
4222 memcpy (copy + first, name + first + 1, len - first);
4224 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
4225 if (h == NULL)
4227 /* We also need to check references to the symbol without the
4228 version. */
4229 copy[first - 1] = '\0';
4230 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4231 FALSE, FALSE, FALSE);
4234 bfd_release (abfd, copy);
4235 return h;
4238 /* Add symbols from an ELF archive file to the linker hash table. We
4239 don't use _bfd_generic_link_add_archive_symbols because of a
4240 problem which arises on UnixWare. The UnixWare libc.so is an
4241 archive which includes an entry libc.so.1 which defines a bunch of
4242 symbols. The libc.so archive also includes a number of other
4243 object files, which also define symbols, some of which are the same
4244 as those defined in libc.so.1. Correct linking requires that we
4245 consider each object file in turn, and include it if it defines any
4246 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4247 this; it looks through the list of undefined symbols, and includes
4248 any object file which defines them. When this algorithm is used on
4249 UnixWare, it winds up pulling in libc.so.1 early and defining a
4250 bunch of symbols. This means that some of the other objects in the
4251 archive are not included in the link, which is incorrect since they
4252 precede libc.so.1 in the archive.
4254 Fortunately, ELF archive handling is simpler than that done by
4255 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4256 oddities. In ELF, if we find a symbol in the archive map, and the
4257 symbol is currently undefined, we know that we must pull in that
4258 object file.
4260 Unfortunately, we do have to make multiple passes over the symbol
4261 table until nothing further is resolved. */
4263 static bfd_boolean
4264 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
4266 symindex c;
4267 bfd_boolean *defined = NULL;
4268 bfd_boolean *included = NULL;
4269 carsym *symdefs;
4270 bfd_boolean loop;
4271 bfd_size_type amt;
4272 const struct elf_backend_data *bed;
4273 struct elf_link_hash_entry * (*archive_symbol_lookup)
4274 (bfd *, struct bfd_link_info *, const char *);
4276 if (! bfd_has_map (abfd))
4278 /* An empty archive is a special case. */
4279 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4280 return TRUE;
4281 bfd_set_error (bfd_error_no_armap);
4282 return FALSE;
4285 /* Keep track of all symbols we know to be already defined, and all
4286 files we know to be already included. This is to speed up the
4287 second and subsequent passes. */
4288 c = bfd_ardata (abfd)->symdef_count;
4289 if (c == 0)
4290 return TRUE;
4291 amt = c;
4292 amt *= sizeof (bfd_boolean);
4293 defined = bfd_zmalloc (amt);
4294 included = bfd_zmalloc (amt);
4295 if (defined == NULL || included == NULL)
4296 goto error_return;
4298 symdefs = bfd_ardata (abfd)->symdefs;
4299 bed = get_elf_backend_data (abfd);
4300 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
4304 file_ptr last;
4305 symindex i;
4306 carsym *symdef;
4307 carsym *symdefend;
4309 loop = FALSE;
4310 last = -1;
4312 symdef = symdefs;
4313 symdefend = symdef + c;
4314 for (i = 0; symdef < symdefend; symdef++, i++)
4316 struct elf_link_hash_entry *h;
4317 bfd *element;
4318 struct bfd_link_hash_entry *undefs_tail;
4319 symindex mark;
4321 if (defined[i] || included[i])
4322 continue;
4323 if (symdef->file_offset == last)
4325 included[i] = TRUE;
4326 continue;
4329 h = archive_symbol_lookup (abfd, info, symdef->name);
4330 if (h == (struct elf_link_hash_entry *) 0 - 1)
4331 goto error_return;
4333 if (h == NULL)
4334 continue;
4336 if (h->root.type == bfd_link_hash_common)
4338 /* We currently have a common symbol. The archive map contains
4339 a reference to this symbol, so we may want to include it. We
4340 only want to include it however, if this archive element
4341 contains a definition of the symbol, not just another common
4342 declaration of it.
4344 Unfortunately some archivers (including GNU ar) will put
4345 declarations of common symbols into their archive maps, as
4346 well as real definitions, so we cannot just go by the archive
4347 map alone. Instead we must read in the element's symbol
4348 table and check that to see what kind of symbol definition
4349 this is. */
4350 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
4351 continue;
4353 else if (h->root.type != bfd_link_hash_undefined)
4355 if (h->root.type != bfd_link_hash_undefweak)
4356 defined[i] = TRUE;
4357 continue;
4360 /* We need to include this archive member. */
4361 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4362 if (element == NULL)
4363 goto error_return;
4365 if (! bfd_check_format (element, bfd_object))
4366 goto error_return;
4368 /* Doublecheck that we have not included this object
4369 already--it should be impossible, but there may be
4370 something wrong with the archive. */
4371 if (element->archive_pass != 0)
4373 bfd_set_error (bfd_error_bad_value);
4374 goto error_return;
4376 element->archive_pass = 1;
4378 undefs_tail = info->hash->undefs_tail;
4380 if (! (*info->callbacks->add_archive_element) (info, element,
4381 symdef->name))
4382 goto error_return;
4383 if (! bfd_link_add_symbols (element, info))
4384 goto error_return;
4386 /* If there are any new undefined symbols, we need to make
4387 another pass through the archive in order to see whether
4388 they can be defined. FIXME: This isn't perfect, because
4389 common symbols wind up on undefs_tail and because an
4390 undefined symbol which is defined later on in this pass
4391 does not require another pass. This isn't a bug, but it
4392 does make the code less efficient than it could be. */
4393 if (undefs_tail != info->hash->undefs_tail)
4394 loop = TRUE;
4396 /* Look backward to mark all symbols from this object file
4397 which we have already seen in this pass. */
4398 mark = i;
4401 included[mark] = TRUE;
4402 if (mark == 0)
4403 break;
4404 --mark;
4406 while (symdefs[mark].file_offset == symdef->file_offset);
4408 /* We mark subsequent symbols from this object file as we go
4409 on through the loop. */
4410 last = symdef->file_offset;
4413 while (loop);
4415 free (defined);
4416 free (included);
4418 return TRUE;
4420 error_return:
4421 if (defined != NULL)
4422 free (defined);
4423 if (included != NULL)
4424 free (included);
4425 return FALSE;
4428 /* Given an ELF BFD, add symbols to the global hash table as
4429 appropriate. */
4431 bfd_boolean
4432 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
4434 switch (bfd_get_format (abfd))
4436 case bfd_object:
4437 return elf_link_add_object_symbols (abfd, info);
4438 case bfd_archive:
4439 return elf_link_add_archive_symbols (abfd, info);
4440 default:
4441 bfd_set_error (bfd_error_wrong_format);
4442 return FALSE;
4446 /* This function will be called though elf_link_hash_traverse to store
4447 all hash value of the exported symbols in an array. */
4449 static bfd_boolean
4450 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
4452 unsigned long **valuep = data;
4453 const char *name;
4454 char *p;
4455 unsigned long ha;
4456 char *alc = NULL;
4458 if (h->root.type == bfd_link_hash_warning)
4459 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4461 /* Ignore indirect symbols. These are added by the versioning code. */
4462 if (h->dynindx == -1)
4463 return TRUE;
4465 name = h->root.root.string;
4466 p = strchr (name, ELF_VER_CHR);
4467 if (p != NULL)
4469 alc = bfd_malloc (p - name + 1);
4470 memcpy (alc, name, p - name);
4471 alc[p - name] = '\0';
4472 name = alc;
4475 /* Compute the hash value. */
4476 ha = bfd_elf_hash (name);
4478 /* Store the found hash value in the array given as the argument. */
4479 *(*valuep)++ = ha;
4481 /* And store it in the struct so that we can put it in the hash table
4482 later. */
4483 h->elf_hash_value = ha;
4485 if (alc != NULL)
4486 free (alc);
4488 return TRUE;
4491 /* Array used to determine the number of hash table buckets to use
4492 based on the number of symbols there are. If there are fewer than
4493 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4494 fewer than 37 we use 17 buckets, and so forth. We never use more
4495 than 32771 buckets. */
4497 static const size_t elf_buckets[] =
4499 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4500 16411, 32771, 0
4503 /* Compute bucket count for hashing table. We do not use a static set
4504 of possible tables sizes anymore. Instead we determine for all
4505 possible reasonable sizes of the table the outcome (i.e., the
4506 number of collisions etc) and choose the best solution. The
4507 weighting functions are not too simple to allow the table to grow
4508 without bounds. Instead one of the weighting factors is the size.
4509 Therefore the result is always a good payoff between few collisions
4510 (= short chain lengths) and table size. */
4511 static size_t
4512 compute_bucket_count (struct bfd_link_info *info)
4514 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
4515 size_t best_size = 0;
4516 unsigned long int *hashcodes;
4517 unsigned long int *hashcodesp;
4518 unsigned long int i;
4519 bfd_size_type amt;
4521 /* Compute the hash values for all exported symbols. At the same
4522 time store the values in an array so that we could use them for
4523 optimizations. */
4524 amt = dynsymcount;
4525 amt *= sizeof (unsigned long int);
4526 hashcodes = bfd_malloc (amt);
4527 if (hashcodes == NULL)
4528 return 0;
4529 hashcodesp = hashcodes;
4531 /* Put all hash values in HASHCODES. */
4532 elf_link_hash_traverse (elf_hash_table (info),
4533 elf_collect_hash_codes, &hashcodesp);
4535 /* We have a problem here. The following code to optimize the table
4536 size requires an integer type with more the 32 bits. If
4537 BFD_HOST_U_64_BIT is set we know about such a type. */
4538 #ifdef BFD_HOST_U_64_BIT
4539 if (info->optimize)
4541 unsigned long int nsyms = hashcodesp - hashcodes;
4542 size_t minsize;
4543 size_t maxsize;
4544 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
4545 unsigned long int *counts ;
4546 bfd *dynobj = elf_hash_table (info)->dynobj;
4547 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
4549 /* Possible optimization parameters: if we have NSYMS symbols we say
4550 that the hashing table must at least have NSYMS/4 and at most
4551 2*NSYMS buckets. */
4552 minsize = nsyms / 4;
4553 if (minsize == 0)
4554 minsize = 1;
4555 best_size = maxsize = nsyms * 2;
4557 /* Create array where we count the collisions in. We must use bfd_malloc
4558 since the size could be large. */
4559 amt = maxsize;
4560 amt *= sizeof (unsigned long int);
4561 counts = bfd_malloc (amt);
4562 if (counts == NULL)
4564 free (hashcodes);
4565 return 0;
4568 /* Compute the "optimal" size for the hash table. The criteria is a
4569 minimal chain length. The minor criteria is (of course) the size
4570 of the table. */
4571 for (i = minsize; i < maxsize; ++i)
4573 /* Walk through the array of hashcodes and count the collisions. */
4574 BFD_HOST_U_64_BIT max;
4575 unsigned long int j;
4576 unsigned long int fact;
4578 memset (counts, '\0', i * sizeof (unsigned long int));
4580 /* Determine how often each hash bucket is used. */
4581 for (j = 0; j < nsyms; ++j)
4582 ++counts[hashcodes[j] % i];
4584 /* For the weight function we need some information about the
4585 pagesize on the target. This is information need not be 100%
4586 accurate. Since this information is not available (so far) we
4587 define it here to a reasonable default value. If it is crucial
4588 to have a better value some day simply define this value. */
4589 # ifndef BFD_TARGET_PAGESIZE
4590 # define BFD_TARGET_PAGESIZE (4096)
4591 # endif
4593 /* We in any case need 2 + NSYMS entries for the size values and
4594 the chains. */
4595 max = (2 + nsyms) * (bed->s->arch_size / 8);
4597 # if 1
4598 /* Variant 1: optimize for short chains. We add the squares
4599 of all the chain lengths (which favors many small chain
4600 over a few long chains). */
4601 for (j = 0; j < i; ++j)
4602 max += counts[j] * counts[j];
4604 /* This adds penalties for the overall size of the table. */
4605 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4606 max *= fact * fact;
4607 # else
4608 /* Variant 2: Optimize a lot more for small table. Here we
4609 also add squares of the size but we also add penalties for
4610 empty slots (the +1 term). */
4611 for (j = 0; j < i; ++j)
4612 max += (1 + counts[j]) * (1 + counts[j]);
4614 /* The overall size of the table is considered, but not as
4615 strong as in variant 1, where it is squared. */
4616 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4617 max *= fact;
4618 # endif
4620 /* Compare with current best results. */
4621 if (max < best_chlen)
4623 best_chlen = max;
4624 best_size = i;
4628 free (counts);
4630 else
4631 #endif /* defined (BFD_HOST_U_64_BIT) */
4633 /* This is the fallback solution if no 64bit type is available or if we
4634 are not supposed to spend much time on optimizations. We select the
4635 bucket count using a fixed set of numbers. */
4636 for (i = 0; elf_buckets[i] != 0; i++)
4638 best_size = elf_buckets[i];
4639 if (dynsymcount < elf_buckets[i + 1])
4640 break;
4644 /* Free the arrays we needed. */
4645 free (hashcodes);
4647 return best_size;
4650 /* Set up the sizes and contents of the ELF dynamic sections. This is
4651 called by the ELF linker emulation before_allocation routine. We
4652 must set the sizes of the sections before the linker sets the
4653 addresses of the various sections. */
4655 bfd_boolean
4656 bfd_elf_size_dynamic_sections (bfd *output_bfd,
4657 const char *soname,
4658 const char *rpath,
4659 const char *filter_shlib,
4660 const char * const *auxiliary_filters,
4661 struct bfd_link_info *info,
4662 asection **sinterpptr,
4663 struct bfd_elf_version_tree *verdefs)
4665 bfd_size_type soname_indx;
4666 bfd *dynobj;
4667 const struct elf_backend_data *bed;
4668 struct elf_assign_sym_version_info asvinfo;
4670 *sinterpptr = NULL;
4672 soname_indx = (bfd_size_type) -1;
4674 if (!is_elf_hash_table (info->hash))
4675 return TRUE;
4677 elf_tdata (output_bfd)->relro = info->relro;
4678 if (info->execstack)
4679 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
4680 else if (info->noexecstack)
4681 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
4682 else
4684 bfd *inputobj;
4685 asection *notesec = NULL;
4686 int exec = 0;
4688 for (inputobj = info->input_bfds;
4689 inputobj;
4690 inputobj = inputobj->link_next)
4692 asection *s;
4694 if (inputobj->flags & DYNAMIC)
4695 continue;
4696 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
4697 if (s)
4699 if (s->flags & SEC_CODE)
4700 exec = PF_X;
4701 notesec = s;
4703 else
4704 exec = PF_X;
4706 if (notesec)
4708 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
4709 if (exec && info->relocatable
4710 && notesec->output_section != bfd_abs_section_ptr)
4711 notesec->output_section->flags |= SEC_CODE;
4715 /* Any syms created from now on start with -1 in
4716 got.refcount/offset and plt.refcount/offset. */
4717 elf_hash_table (info)->init_refcount = elf_hash_table (info)->init_offset;
4719 /* The backend may have to create some sections regardless of whether
4720 we're dynamic or not. */
4721 bed = get_elf_backend_data (output_bfd);
4722 if (bed->elf_backend_always_size_sections
4723 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
4724 return FALSE;
4726 dynobj = elf_hash_table (info)->dynobj;
4728 /* If there were no dynamic objects in the link, there is nothing to
4729 do here. */
4730 if (dynobj == NULL)
4731 return TRUE;
4733 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
4734 return FALSE;
4736 if (elf_hash_table (info)->dynamic_sections_created)
4738 struct elf_info_failed eif;
4739 struct elf_link_hash_entry *h;
4740 asection *dynstr;
4741 struct bfd_elf_version_tree *t;
4742 struct bfd_elf_version_expr *d;
4743 bfd_boolean all_defined;
4745 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
4746 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
4748 if (soname != NULL)
4750 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
4751 soname, TRUE);
4752 if (soname_indx == (bfd_size_type) -1
4753 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
4754 return FALSE;
4757 if (info->symbolic)
4759 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
4760 return FALSE;
4761 info->flags |= DF_SYMBOLIC;
4764 if (rpath != NULL)
4766 bfd_size_type indx;
4768 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
4769 TRUE);
4770 if (indx == (bfd_size_type) -1
4771 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
4772 return FALSE;
4774 if (info->new_dtags)
4776 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
4777 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
4778 return FALSE;
4782 if (filter_shlib != NULL)
4784 bfd_size_type indx;
4786 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
4787 filter_shlib, TRUE);
4788 if (indx == (bfd_size_type) -1
4789 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
4790 return FALSE;
4793 if (auxiliary_filters != NULL)
4795 const char * const *p;
4797 for (p = auxiliary_filters; *p != NULL; p++)
4799 bfd_size_type indx;
4801 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
4802 *p, TRUE);
4803 if (indx == (bfd_size_type) -1
4804 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
4805 return FALSE;
4809 eif.info = info;
4810 eif.verdefs = verdefs;
4811 eif.failed = FALSE;
4813 /* If we are supposed to export all symbols into the dynamic symbol
4814 table (this is not the normal case), then do so. */
4815 if (info->export_dynamic)
4817 elf_link_hash_traverse (elf_hash_table (info),
4818 _bfd_elf_export_symbol,
4819 &eif);
4820 if (eif.failed)
4821 return FALSE;
4824 /* Make all global versions with definition. */
4825 for (t = verdefs; t != NULL; t = t->next)
4826 for (d = t->globals.list; d != NULL; d = d->next)
4827 if (!d->symver && d->symbol)
4829 const char *verstr, *name;
4830 size_t namelen, verlen, newlen;
4831 char *newname, *p;
4832 struct elf_link_hash_entry *newh;
4834 name = d->symbol;
4835 namelen = strlen (name);
4836 verstr = t->name;
4837 verlen = strlen (verstr);
4838 newlen = namelen + verlen + 3;
4840 newname = bfd_malloc (newlen);
4841 if (newname == NULL)
4842 return FALSE;
4843 memcpy (newname, name, namelen);
4845 /* Check the hidden versioned definition. */
4846 p = newname + namelen;
4847 *p++ = ELF_VER_CHR;
4848 memcpy (p, verstr, verlen + 1);
4849 newh = elf_link_hash_lookup (elf_hash_table (info),
4850 newname, FALSE, FALSE,
4851 FALSE);
4852 if (newh == NULL
4853 || (newh->root.type != bfd_link_hash_defined
4854 && newh->root.type != bfd_link_hash_defweak))
4856 /* Check the default versioned definition. */
4857 *p++ = ELF_VER_CHR;
4858 memcpy (p, verstr, verlen + 1);
4859 newh = elf_link_hash_lookup (elf_hash_table (info),
4860 newname, FALSE, FALSE,
4861 FALSE);
4863 free (newname);
4865 /* Mark this version if there is a definition and it is
4866 not defined in a shared object. */
4867 if (newh != NULL
4868 && ((newh->elf_link_hash_flags
4869 & ELF_LINK_HASH_DEF_DYNAMIC) == 0)
4870 && (newh->root.type == bfd_link_hash_defined
4871 || newh->root.type == bfd_link_hash_defweak))
4872 d->symver = 1;
4875 /* Attach all the symbols to their version information. */
4876 asvinfo.output_bfd = output_bfd;
4877 asvinfo.info = info;
4878 asvinfo.verdefs = verdefs;
4879 asvinfo.failed = FALSE;
4881 elf_link_hash_traverse (elf_hash_table (info),
4882 _bfd_elf_link_assign_sym_version,
4883 &asvinfo);
4884 if (asvinfo.failed)
4885 return FALSE;
4887 if (!info->allow_undefined_version)
4889 /* Check if all global versions have a definition. */
4890 all_defined = TRUE;
4891 for (t = verdefs; t != NULL; t = t->next)
4892 for (d = t->globals.list; d != NULL; d = d->next)
4893 if (!d->symver && !d->script)
4895 (*_bfd_error_handler)
4896 (_("%s: undefined version: %s"),
4897 d->pattern, t->name);
4898 all_defined = FALSE;
4901 if (!all_defined)
4903 bfd_set_error (bfd_error_bad_value);
4904 return FALSE;
4908 /* Find all symbols which were defined in a dynamic object and make
4909 the backend pick a reasonable value for them. */
4910 elf_link_hash_traverse (elf_hash_table (info),
4911 _bfd_elf_adjust_dynamic_symbol,
4912 &eif);
4913 if (eif.failed)
4914 return FALSE;
4916 /* Add some entries to the .dynamic section. We fill in some of the
4917 values later, in bfd_elf_final_link, but we must add the entries
4918 now so that we know the final size of the .dynamic section. */
4920 /* If there are initialization and/or finalization functions to
4921 call then add the corresponding DT_INIT/DT_FINI entries. */
4922 h = (info->init_function
4923 ? elf_link_hash_lookup (elf_hash_table (info),
4924 info->init_function, FALSE,
4925 FALSE, FALSE)
4926 : NULL);
4927 if (h != NULL
4928 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
4929 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
4931 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
4932 return FALSE;
4934 h = (info->fini_function
4935 ? elf_link_hash_lookup (elf_hash_table (info),
4936 info->fini_function, FALSE,
4937 FALSE, FALSE)
4938 : NULL);
4939 if (h != NULL
4940 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
4941 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
4943 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
4944 return FALSE;
4947 if (bfd_get_section_by_name (output_bfd, ".preinit_array") != NULL)
4949 /* DT_PREINIT_ARRAY is not allowed in shared library. */
4950 if (! info->executable)
4952 bfd *sub;
4953 asection *o;
4955 for (sub = info->input_bfds; sub != NULL;
4956 sub = sub->link_next)
4957 for (o = sub->sections; o != NULL; o = o->next)
4958 if (elf_section_data (o)->this_hdr.sh_type
4959 == SHT_PREINIT_ARRAY)
4961 (*_bfd_error_handler)
4962 (_("%B: .preinit_array section is not allowed in DSO"),
4963 sub);
4964 break;
4967 bfd_set_error (bfd_error_nonrepresentable_section);
4968 return FALSE;
4971 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
4972 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
4973 return FALSE;
4975 if (bfd_get_section_by_name (output_bfd, ".init_array") != NULL)
4977 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
4978 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
4979 return FALSE;
4981 if (bfd_get_section_by_name (output_bfd, ".fini_array") != NULL)
4983 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
4984 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
4985 return FALSE;
4988 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
4989 /* If .dynstr is excluded from the link, we don't want any of
4990 these tags. Strictly, we should be checking each section
4991 individually; This quick check covers for the case where
4992 someone does a /DISCARD/ : { *(*) }. */
4993 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
4995 bfd_size_type strsize;
4997 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
4998 if (!_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)
4999 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5000 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5001 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5002 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5003 bed->s->sizeof_sym))
5004 return FALSE;
5008 /* The backend must work out the sizes of all the other dynamic
5009 sections. */
5010 if (bed->elf_backend_size_dynamic_sections
5011 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5012 return FALSE;
5014 if (elf_hash_table (info)->dynamic_sections_created)
5016 bfd_size_type dynsymcount;
5017 asection *s;
5018 size_t bucketcount = 0;
5019 size_t hash_entry_size;
5020 unsigned int dtagcount;
5022 /* Set up the version definition section. */
5023 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5024 BFD_ASSERT (s != NULL);
5026 /* We may have created additional version definitions if we are
5027 just linking a regular application. */
5028 verdefs = asvinfo.verdefs;
5030 /* Skip anonymous version tag. */
5031 if (verdefs != NULL && verdefs->vernum == 0)
5032 verdefs = verdefs->next;
5034 if (verdefs == NULL)
5035 _bfd_strip_section_from_output (info, s);
5036 else
5038 unsigned int cdefs;
5039 bfd_size_type size;
5040 struct bfd_elf_version_tree *t;
5041 bfd_byte *p;
5042 Elf_Internal_Verdef def;
5043 Elf_Internal_Verdaux defaux;
5045 cdefs = 0;
5046 size = 0;
5048 /* Make space for the base version. */
5049 size += sizeof (Elf_External_Verdef);
5050 size += sizeof (Elf_External_Verdaux);
5051 ++cdefs;
5053 for (t = verdefs; t != NULL; t = t->next)
5055 struct bfd_elf_version_deps *n;
5057 size += sizeof (Elf_External_Verdef);
5058 size += sizeof (Elf_External_Verdaux);
5059 ++cdefs;
5061 for (n = t->deps; n != NULL; n = n->next)
5062 size += sizeof (Elf_External_Verdaux);
5065 s->size = size;
5066 s->contents = bfd_alloc (output_bfd, s->size);
5067 if (s->contents == NULL && s->size != 0)
5068 return FALSE;
5070 /* Fill in the version definition section. */
5072 p = s->contents;
5074 def.vd_version = VER_DEF_CURRENT;
5075 def.vd_flags = VER_FLG_BASE;
5076 def.vd_ndx = 1;
5077 def.vd_cnt = 1;
5078 def.vd_aux = sizeof (Elf_External_Verdef);
5079 def.vd_next = (sizeof (Elf_External_Verdef)
5080 + sizeof (Elf_External_Verdaux));
5082 if (soname_indx != (bfd_size_type) -1)
5084 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5085 soname_indx);
5086 def.vd_hash = bfd_elf_hash (soname);
5087 defaux.vda_name = soname_indx;
5089 else
5091 const char *name;
5092 bfd_size_type indx;
5094 name = basename (output_bfd->filename);
5095 def.vd_hash = bfd_elf_hash (name);
5096 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5097 name, FALSE);
5098 if (indx == (bfd_size_type) -1)
5099 return FALSE;
5100 defaux.vda_name = indx;
5102 defaux.vda_next = 0;
5104 _bfd_elf_swap_verdef_out (output_bfd, &def,
5105 (Elf_External_Verdef *) p);
5106 p += sizeof (Elf_External_Verdef);
5107 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5108 (Elf_External_Verdaux *) p);
5109 p += sizeof (Elf_External_Verdaux);
5111 for (t = verdefs; t != NULL; t = t->next)
5113 unsigned int cdeps;
5114 struct bfd_elf_version_deps *n;
5115 struct elf_link_hash_entry *h;
5116 struct bfd_link_hash_entry *bh;
5118 cdeps = 0;
5119 for (n = t->deps; n != NULL; n = n->next)
5120 ++cdeps;
5122 /* Add a symbol representing this version. */
5123 bh = NULL;
5124 if (! (_bfd_generic_link_add_one_symbol
5125 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5126 0, NULL, FALSE,
5127 get_elf_backend_data (dynobj)->collect, &bh)))
5128 return FALSE;
5129 h = (struct elf_link_hash_entry *) bh;
5130 h->elf_link_hash_flags &= ~ ELF_LINK_NON_ELF;
5131 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5132 h->type = STT_OBJECT;
5133 h->verinfo.vertree = t;
5135 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5136 return FALSE;
5138 def.vd_version = VER_DEF_CURRENT;
5139 def.vd_flags = 0;
5140 if (t->globals.list == NULL
5141 && t->locals.list == NULL
5142 && ! t->used)
5143 def.vd_flags |= VER_FLG_WEAK;
5144 def.vd_ndx = t->vernum + 1;
5145 def.vd_cnt = cdeps + 1;
5146 def.vd_hash = bfd_elf_hash (t->name);
5147 def.vd_aux = sizeof (Elf_External_Verdef);
5148 def.vd_next = 0;
5149 if (t->next != NULL)
5150 def.vd_next = (sizeof (Elf_External_Verdef)
5151 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5153 _bfd_elf_swap_verdef_out (output_bfd, &def,
5154 (Elf_External_Verdef *) p);
5155 p += sizeof (Elf_External_Verdef);
5157 defaux.vda_name = h->dynstr_index;
5158 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5159 h->dynstr_index);
5160 defaux.vda_next = 0;
5161 if (t->deps != NULL)
5162 defaux.vda_next = sizeof (Elf_External_Verdaux);
5163 t->name_indx = defaux.vda_name;
5165 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5166 (Elf_External_Verdaux *) p);
5167 p += sizeof (Elf_External_Verdaux);
5169 for (n = t->deps; n != NULL; n = n->next)
5171 if (n->version_needed == NULL)
5173 /* This can happen if there was an error in the
5174 version script. */
5175 defaux.vda_name = 0;
5177 else
5179 defaux.vda_name = n->version_needed->name_indx;
5180 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5181 defaux.vda_name);
5183 if (n->next == NULL)
5184 defaux.vda_next = 0;
5185 else
5186 defaux.vda_next = sizeof (Elf_External_Verdaux);
5188 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5189 (Elf_External_Verdaux *) p);
5190 p += sizeof (Elf_External_Verdaux);
5194 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
5195 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
5196 return FALSE;
5198 elf_tdata (output_bfd)->cverdefs = cdefs;
5201 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
5203 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
5204 return FALSE;
5206 else if (info->flags & DF_BIND_NOW)
5208 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
5209 return FALSE;
5212 if (info->flags_1)
5214 if (info->executable)
5215 info->flags_1 &= ~ (DF_1_INITFIRST
5216 | DF_1_NODELETE
5217 | DF_1_NOOPEN);
5218 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
5219 return FALSE;
5222 /* Work out the size of the version reference section. */
5224 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
5225 BFD_ASSERT (s != NULL);
5227 struct elf_find_verdep_info sinfo;
5229 sinfo.output_bfd = output_bfd;
5230 sinfo.info = info;
5231 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
5232 if (sinfo.vers == 0)
5233 sinfo.vers = 1;
5234 sinfo.failed = FALSE;
5236 elf_link_hash_traverse (elf_hash_table (info),
5237 _bfd_elf_link_find_version_dependencies,
5238 &sinfo);
5240 if (elf_tdata (output_bfd)->verref == NULL)
5241 _bfd_strip_section_from_output (info, s);
5242 else
5244 Elf_Internal_Verneed *t;
5245 unsigned int size;
5246 unsigned int crefs;
5247 bfd_byte *p;
5249 /* Build the version definition section. */
5250 size = 0;
5251 crefs = 0;
5252 for (t = elf_tdata (output_bfd)->verref;
5253 t != NULL;
5254 t = t->vn_nextref)
5256 Elf_Internal_Vernaux *a;
5258 size += sizeof (Elf_External_Verneed);
5259 ++crefs;
5260 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5261 size += sizeof (Elf_External_Vernaux);
5264 s->size = size;
5265 s->contents = bfd_alloc (output_bfd, s->size);
5266 if (s->contents == NULL)
5267 return FALSE;
5269 p = s->contents;
5270 for (t = elf_tdata (output_bfd)->verref;
5271 t != NULL;
5272 t = t->vn_nextref)
5274 unsigned int caux;
5275 Elf_Internal_Vernaux *a;
5276 bfd_size_type indx;
5278 caux = 0;
5279 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5280 ++caux;
5282 t->vn_version = VER_NEED_CURRENT;
5283 t->vn_cnt = caux;
5284 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5285 elf_dt_name (t->vn_bfd) != NULL
5286 ? elf_dt_name (t->vn_bfd)
5287 : basename (t->vn_bfd->filename),
5288 FALSE);
5289 if (indx == (bfd_size_type) -1)
5290 return FALSE;
5291 t->vn_file = indx;
5292 t->vn_aux = sizeof (Elf_External_Verneed);
5293 if (t->vn_nextref == NULL)
5294 t->vn_next = 0;
5295 else
5296 t->vn_next = (sizeof (Elf_External_Verneed)
5297 + caux * sizeof (Elf_External_Vernaux));
5299 _bfd_elf_swap_verneed_out (output_bfd, t,
5300 (Elf_External_Verneed *) p);
5301 p += sizeof (Elf_External_Verneed);
5303 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5305 a->vna_hash = bfd_elf_hash (a->vna_nodename);
5306 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5307 a->vna_nodename, FALSE);
5308 if (indx == (bfd_size_type) -1)
5309 return FALSE;
5310 a->vna_name = indx;
5311 if (a->vna_nextptr == NULL)
5312 a->vna_next = 0;
5313 else
5314 a->vna_next = sizeof (Elf_External_Vernaux);
5316 _bfd_elf_swap_vernaux_out (output_bfd, a,
5317 (Elf_External_Vernaux *) p);
5318 p += sizeof (Elf_External_Vernaux);
5322 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
5323 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
5324 return FALSE;
5326 elf_tdata (output_bfd)->cverrefs = crefs;
5330 /* Assign dynsym indicies. In a shared library we generate a
5331 section symbol for each output section, which come first.
5332 Next come all of the back-end allocated local dynamic syms,
5333 followed by the rest of the global symbols. */
5335 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
5337 /* Work out the size of the symbol version section. */
5338 s = bfd_get_section_by_name (dynobj, ".gnu.version");
5339 BFD_ASSERT (s != NULL);
5340 if (dynsymcount == 0
5341 || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL))
5343 _bfd_strip_section_from_output (info, s);
5344 /* The DYNSYMCOUNT might have changed if we were going to
5345 output a dynamic symbol table entry for S. */
5346 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
5348 else
5350 s->size = dynsymcount * sizeof (Elf_External_Versym);
5351 s->contents = bfd_zalloc (output_bfd, s->size);
5352 if (s->contents == NULL)
5353 return FALSE;
5355 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
5356 return FALSE;
5359 /* Set the size of the .dynsym and .hash sections. We counted
5360 the number of dynamic symbols in elf_link_add_object_symbols.
5361 We will build the contents of .dynsym and .hash when we build
5362 the final symbol table, because until then we do not know the
5363 correct value to give the symbols. We built the .dynstr
5364 section as we went along in elf_link_add_object_symbols. */
5365 s = bfd_get_section_by_name (dynobj, ".dynsym");
5366 BFD_ASSERT (s != NULL);
5367 s->size = dynsymcount * bed->s->sizeof_sym;
5368 s->contents = bfd_alloc (output_bfd, s->size);
5369 if (s->contents == NULL && s->size != 0)
5370 return FALSE;
5372 if (dynsymcount != 0)
5374 Elf_Internal_Sym isym;
5376 /* The first entry in .dynsym is a dummy symbol. */
5377 isym.st_value = 0;
5378 isym.st_size = 0;
5379 isym.st_name = 0;
5380 isym.st_info = 0;
5381 isym.st_other = 0;
5382 isym.st_shndx = 0;
5383 bed->s->swap_symbol_out (output_bfd, &isym, s->contents, 0);
5386 /* Compute the size of the hashing table. As a side effect this
5387 computes the hash values for all the names we export. */
5388 bucketcount = compute_bucket_count (info);
5390 s = bfd_get_section_by_name (dynobj, ".hash");
5391 BFD_ASSERT (s != NULL);
5392 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
5393 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
5394 s->contents = bfd_zalloc (output_bfd, s->size);
5395 if (s->contents == NULL)
5396 return FALSE;
5398 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
5399 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
5400 s->contents + hash_entry_size);
5402 elf_hash_table (info)->bucketcount = bucketcount;
5404 s = bfd_get_section_by_name (dynobj, ".dynstr");
5405 BFD_ASSERT (s != NULL);
5407 elf_finalize_dynstr (output_bfd, info);
5409 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5411 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
5412 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
5413 return FALSE;
5416 return TRUE;
5419 /* Final phase of ELF linker. */
5421 /* A structure we use to avoid passing large numbers of arguments. */
5423 struct elf_final_link_info
5425 /* General link information. */
5426 struct bfd_link_info *info;
5427 /* Output BFD. */
5428 bfd *output_bfd;
5429 /* Symbol string table. */
5430 struct bfd_strtab_hash *symstrtab;
5431 /* .dynsym section. */
5432 asection *dynsym_sec;
5433 /* .hash section. */
5434 asection *hash_sec;
5435 /* symbol version section (.gnu.version). */
5436 asection *symver_sec;
5437 /* Buffer large enough to hold contents of any section. */
5438 bfd_byte *contents;
5439 /* Buffer large enough to hold external relocs of any section. */
5440 void *external_relocs;
5441 /* Buffer large enough to hold internal relocs of any section. */
5442 Elf_Internal_Rela *internal_relocs;
5443 /* Buffer large enough to hold external local symbols of any input
5444 BFD. */
5445 bfd_byte *external_syms;
5446 /* And a buffer for symbol section indices. */
5447 Elf_External_Sym_Shndx *locsym_shndx;
5448 /* Buffer large enough to hold internal local symbols of any input
5449 BFD. */
5450 Elf_Internal_Sym *internal_syms;
5451 /* Array large enough to hold a symbol index for each local symbol
5452 of any input BFD. */
5453 long *indices;
5454 /* Array large enough to hold a section pointer for each local
5455 symbol of any input BFD. */
5456 asection **sections;
5457 /* Buffer to hold swapped out symbols. */
5458 bfd_byte *symbuf;
5459 /* And one for symbol section indices. */
5460 Elf_External_Sym_Shndx *symshndxbuf;
5461 /* Number of swapped out symbols in buffer. */
5462 size_t symbuf_count;
5463 /* Number of symbols which fit in symbuf. */
5464 size_t symbuf_size;
5465 /* And same for symshndxbuf. */
5466 size_t shndxbuf_size;
5469 /* This struct is used to pass information to elf_link_output_extsym. */
5471 struct elf_outext_info
5473 bfd_boolean failed;
5474 bfd_boolean localsyms;
5475 struct elf_final_link_info *finfo;
5478 /* When performing a relocatable link, the input relocations are
5479 preserved. But, if they reference global symbols, the indices
5480 referenced must be updated. Update all the relocations in
5481 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5483 static void
5484 elf_link_adjust_relocs (bfd *abfd,
5485 Elf_Internal_Shdr *rel_hdr,
5486 unsigned int count,
5487 struct elf_link_hash_entry **rel_hash)
5489 unsigned int i;
5490 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5491 bfd_byte *erela;
5492 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5493 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5494 bfd_vma r_type_mask;
5495 int r_sym_shift;
5497 if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
5499 swap_in = bed->s->swap_reloc_in;
5500 swap_out = bed->s->swap_reloc_out;
5502 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
5504 swap_in = bed->s->swap_reloca_in;
5505 swap_out = bed->s->swap_reloca_out;
5507 else
5508 abort ();
5510 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
5511 abort ();
5513 if (bed->s->arch_size == 32)
5515 r_type_mask = 0xff;
5516 r_sym_shift = 8;
5518 else
5520 r_type_mask = 0xffffffff;
5521 r_sym_shift = 32;
5524 erela = rel_hdr->contents;
5525 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
5527 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
5528 unsigned int j;
5530 if (*rel_hash == NULL)
5531 continue;
5533 BFD_ASSERT ((*rel_hash)->indx >= 0);
5535 (*swap_in) (abfd, erela, irela);
5536 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
5537 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
5538 | (irela[j].r_info & r_type_mask));
5539 (*swap_out) (abfd, irela, erela);
5543 struct elf_link_sort_rela
5545 union {
5546 bfd_vma offset;
5547 bfd_vma sym_mask;
5548 } u;
5549 enum elf_reloc_type_class type;
5550 /* We use this as an array of size int_rels_per_ext_rel. */
5551 Elf_Internal_Rela rela[1];
5554 static int
5555 elf_link_sort_cmp1 (const void *A, const void *B)
5557 const struct elf_link_sort_rela *a = A;
5558 const struct elf_link_sort_rela *b = B;
5559 int relativea, relativeb;
5561 relativea = a->type == reloc_class_relative;
5562 relativeb = b->type == reloc_class_relative;
5564 if (relativea < relativeb)
5565 return 1;
5566 if (relativea > relativeb)
5567 return -1;
5568 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
5569 return -1;
5570 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
5571 return 1;
5572 if (a->rela->r_offset < b->rela->r_offset)
5573 return -1;
5574 if (a->rela->r_offset > b->rela->r_offset)
5575 return 1;
5576 return 0;
5579 static int
5580 elf_link_sort_cmp2 (const void *A, const void *B)
5582 const struct elf_link_sort_rela *a = A;
5583 const struct elf_link_sort_rela *b = B;
5584 int copya, copyb;
5586 if (a->u.offset < b->u.offset)
5587 return -1;
5588 if (a->u.offset > b->u.offset)
5589 return 1;
5590 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
5591 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
5592 if (copya < copyb)
5593 return -1;
5594 if (copya > copyb)
5595 return 1;
5596 if (a->rela->r_offset < b->rela->r_offset)
5597 return -1;
5598 if (a->rela->r_offset > b->rela->r_offset)
5599 return 1;
5600 return 0;
5603 static size_t
5604 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
5606 asection *reldyn;
5607 bfd_size_type count, size;
5608 size_t i, ret, sort_elt, ext_size;
5609 bfd_byte *sort, *s_non_relative, *p;
5610 struct elf_link_sort_rela *sq;
5611 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5612 int i2e = bed->s->int_rels_per_ext_rel;
5613 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5614 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5615 struct bfd_link_order *lo;
5616 bfd_vma r_sym_mask;
5618 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
5619 if (reldyn == NULL || reldyn->size == 0)
5621 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
5622 if (reldyn == NULL || reldyn->size == 0)
5623 return 0;
5624 ext_size = bed->s->sizeof_rel;
5625 swap_in = bed->s->swap_reloc_in;
5626 swap_out = bed->s->swap_reloc_out;
5628 else
5630 ext_size = bed->s->sizeof_rela;
5631 swap_in = bed->s->swap_reloca_in;
5632 swap_out = bed->s->swap_reloca_out;
5634 count = reldyn->size / ext_size;
5636 size = 0;
5637 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
5638 if (lo->type == bfd_indirect_link_order)
5640 asection *o = lo->u.indirect.section;
5641 size += o->size;
5644 if (size != reldyn->size)
5645 return 0;
5647 sort_elt = (sizeof (struct elf_link_sort_rela)
5648 + (i2e - 1) * sizeof (Elf_Internal_Rela));
5649 sort = bfd_zmalloc (sort_elt * count);
5650 if (sort == NULL)
5652 (*info->callbacks->warning)
5653 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
5654 return 0;
5657 if (bed->s->arch_size == 32)
5658 r_sym_mask = ~(bfd_vma) 0xff;
5659 else
5660 r_sym_mask = ~(bfd_vma) 0xffffffff;
5662 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
5663 if (lo->type == bfd_indirect_link_order)
5665 bfd_byte *erel, *erelend;
5666 asection *o = lo->u.indirect.section;
5668 erel = o->contents;
5669 erelend = o->contents + o->size;
5670 p = sort + o->output_offset / ext_size * sort_elt;
5671 while (erel < erelend)
5673 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
5674 (*swap_in) (abfd, erel, s->rela);
5675 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
5676 s->u.sym_mask = r_sym_mask;
5677 p += sort_elt;
5678 erel += ext_size;
5682 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
5684 for (i = 0, p = sort; i < count; i++, p += sort_elt)
5686 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
5687 if (s->type != reloc_class_relative)
5688 break;
5690 ret = i;
5691 s_non_relative = p;
5693 sq = (struct elf_link_sort_rela *) s_non_relative;
5694 for (; i < count; i++, p += sort_elt)
5696 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
5697 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
5698 sq = sp;
5699 sp->u.offset = sq->rela->r_offset;
5702 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
5704 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
5705 if (lo->type == bfd_indirect_link_order)
5707 bfd_byte *erel, *erelend;
5708 asection *o = lo->u.indirect.section;
5710 erel = o->contents;
5711 erelend = o->contents + o->size;
5712 p = sort + o->output_offset / ext_size * sort_elt;
5713 while (erel < erelend)
5715 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
5716 (*swap_out) (abfd, s->rela, erel);
5717 p += sort_elt;
5718 erel += ext_size;
5722 free (sort);
5723 *psec = reldyn;
5724 return ret;
5727 /* Flush the output symbols to the file. */
5729 static bfd_boolean
5730 elf_link_flush_output_syms (struct elf_final_link_info *finfo,
5731 const struct elf_backend_data *bed)
5733 if (finfo->symbuf_count > 0)
5735 Elf_Internal_Shdr *hdr;
5736 file_ptr pos;
5737 bfd_size_type amt;
5739 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
5740 pos = hdr->sh_offset + hdr->sh_size;
5741 amt = finfo->symbuf_count * bed->s->sizeof_sym;
5742 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
5743 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
5744 return FALSE;
5746 hdr->sh_size += amt;
5747 finfo->symbuf_count = 0;
5750 return TRUE;
5753 /* Add a symbol to the output symbol table. */
5755 static bfd_boolean
5756 elf_link_output_sym (struct elf_final_link_info *finfo,
5757 const char *name,
5758 Elf_Internal_Sym *elfsym,
5759 asection *input_sec,
5760 struct elf_link_hash_entry *h)
5762 bfd_byte *dest;
5763 Elf_External_Sym_Shndx *destshndx;
5764 bfd_boolean (*output_symbol_hook)
5765 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
5766 struct elf_link_hash_entry *);
5767 const struct elf_backend_data *bed;
5769 bed = get_elf_backend_data (finfo->output_bfd);
5770 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
5771 if (output_symbol_hook != NULL)
5773 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
5774 return FALSE;
5777 if (name == NULL || *name == '\0')
5778 elfsym->st_name = 0;
5779 else if (input_sec->flags & SEC_EXCLUDE)
5780 elfsym->st_name = 0;
5781 else
5783 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
5784 name, TRUE, FALSE);
5785 if (elfsym->st_name == (unsigned long) -1)
5786 return FALSE;
5789 if (finfo->symbuf_count >= finfo->symbuf_size)
5791 if (! elf_link_flush_output_syms (finfo, bed))
5792 return FALSE;
5795 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
5796 destshndx = finfo->symshndxbuf;
5797 if (destshndx != NULL)
5799 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
5801 bfd_size_type amt;
5803 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
5804 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
5805 if (destshndx == NULL)
5806 return FALSE;
5807 memset ((char *) destshndx + amt, 0, amt);
5808 finfo->shndxbuf_size *= 2;
5810 destshndx += bfd_get_symcount (finfo->output_bfd);
5813 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
5814 finfo->symbuf_count += 1;
5815 bfd_get_symcount (finfo->output_bfd) += 1;
5817 return TRUE;
5820 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
5821 allowing an unsatisfied unversioned symbol in the DSO to match a
5822 versioned symbol that would normally require an explicit version.
5823 We also handle the case that a DSO references a hidden symbol
5824 which may be satisfied by a versioned symbol in another DSO. */
5826 static bfd_boolean
5827 elf_link_check_versioned_symbol (struct bfd_link_info *info,
5828 const struct elf_backend_data *bed,
5829 struct elf_link_hash_entry *h)
5831 bfd *abfd;
5832 struct elf_link_loaded_list *loaded;
5834 if (!is_elf_hash_table (info->hash))
5835 return FALSE;
5837 switch (h->root.type)
5839 default:
5840 abfd = NULL;
5841 break;
5843 case bfd_link_hash_undefined:
5844 case bfd_link_hash_undefweak:
5845 abfd = h->root.u.undef.abfd;
5846 if ((abfd->flags & DYNAMIC) == 0
5847 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
5848 return FALSE;
5849 break;
5851 case bfd_link_hash_defined:
5852 case bfd_link_hash_defweak:
5853 abfd = h->root.u.def.section->owner;
5854 break;
5856 case bfd_link_hash_common:
5857 abfd = h->root.u.c.p->section->owner;
5858 break;
5860 BFD_ASSERT (abfd != NULL);
5862 for (loaded = elf_hash_table (info)->loaded;
5863 loaded != NULL;
5864 loaded = loaded->next)
5866 bfd *input;
5867 Elf_Internal_Shdr *hdr;
5868 bfd_size_type symcount;
5869 bfd_size_type extsymcount;
5870 bfd_size_type extsymoff;
5871 Elf_Internal_Shdr *versymhdr;
5872 Elf_Internal_Sym *isym;
5873 Elf_Internal_Sym *isymend;
5874 Elf_Internal_Sym *isymbuf;
5875 Elf_External_Versym *ever;
5876 Elf_External_Versym *extversym;
5878 input = loaded->abfd;
5880 /* We check each DSO for a possible hidden versioned definition. */
5881 if (input == abfd
5882 || (input->flags & DYNAMIC) == 0
5883 || elf_dynversym (input) == 0)
5884 continue;
5886 hdr = &elf_tdata (input)->dynsymtab_hdr;
5888 symcount = hdr->sh_size / bed->s->sizeof_sym;
5889 if (elf_bad_symtab (input))
5891 extsymcount = symcount;
5892 extsymoff = 0;
5894 else
5896 extsymcount = symcount - hdr->sh_info;
5897 extsymoff = hdr->sh_info;
5900 if (extsymcount == 0)
5901 continue;
5903 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
5904 NULL, NULL, NULL);
5905 if (isymbuf == NULL)
5906 return FALSE;
5908 /* Read in any version definitions. */
5909 versymhdr = &elf_tdata (input)->dynversym_hdr;
5910 extversym = bfd_malloc (versymhdr->sh_size);
5911 if (extversym == NULL)
5912 goto error_ret;
5914 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
5915 || (bfd_bread (extversym, versymhdr->sh_size, input)
5916 != versymhdr->sh_size))
5918 free (extversym);
5919 error_ret:
5920 free (isymbuf);
5921 return FALSE;
5924 ever = extversym + extsymoff;
5925 isymend = isymbuf + extsymcount;
5926 for (isym = isymbuf; isym < isymend; isym++, ever++)
5928 const char *name;
5929 Elf_Internal_Versym iver;
5930 unsigned short version_index;
5932 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
5933 || isym->st_shndx == SHN_UNDEF)
5934 continue;
5936 name = bfd_elf_string_from_elf_section (input,
5937 hdr->sh_link,
5938 isym->st_name);
5939 if (strcmp (name, h->root.root.string) != 0)
5940 continue;
5942 _bfd_elf_swap_versym_in (input, ever, &iver);
5944 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
5946 /* If we have a non-hidden versioned sym, then it should
5947 have provided a definition for the undefined sym. */
5948 abort ();
5951 version_index = iver.vs_vers & VERSYM_VERSION;
5952 if (version_index == 1 || version_index == 2)
5954 /* This is the base or first version. We can use it. */
5955 free (extversym);
5956 free (isymbuf);
5957 return TRUE;
5961 free (extversym);
5962 free (isymbuf);
5965 return FALSE;
5968 /* Add an external symbol to the symbol table. This is called from
5969 the hash table traversal routine. When generating a shared object,
5970 we go through the symbol table twice. The first time we output
5971 anything that might have been forced to local scope in a version
5972 script. The second time we output the symbols that are still
5973 global symbols. */
5975 static bfd_boolean
5976 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
5978 struct elf_outext_info *eoinfo = data;
5979 struct elf_final_link_info *finfo = eoinfo->finfo;
5980 bfd_boolean strip;
5981 Elf_Internal_Sym sym;
5982 asection *input_sec;
5983 const struct elf_backend_data *bed;
5985 if (h->root.type == bfd_link_hash_warning)
5987 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5988 if (h->root.type == bfd_link_hash_new)
5989 return TRUE;
5992 /* Decide whether to output this symbol in this pass. */
5993 if (eoinfo->localsyms)
5995 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
5996 return TRUE;
5998 else
6000 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
6001 return TRUE;
6004 bed = get_elf_backend_data (finfo->output_bfd);
6006 /* If we have an undefined symbol reference here then it must have
6007 come from a shared library that is being linked in. (Undefined
6008 references in regular files have already been handled). If we
6009 are reporting errors for this situation then do so now. */
6010 if (h->root.type == bfd_link_hash_undefined
6011 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0
6012 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
6013 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
6014 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
6016 if (! ((*finfo->info->callbacks->undefined_symbol)
6017 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
6018 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
6020 eoinfo->failed = TRUE;
6021 return FALSE;
6025 /* We should also warn if a forced local symbol is referenced from
6026 shared libraries. */
6027 if (! finfo->info->relocatable
6028 && (! finfo->info->shared)
6029 && (h->elf_link_hash_flags
6030 & (ELF_LINK_FORCED_LOCAL | ELF_LINK_HASH_REF_DYNAMIC | ELF_LINK_DYNAMIC_DEF | ELF_LINK_DYNAMIC_WEAK))
6031 == (ELF_LINK_FORCED_LOCAL | ELF_LINK_HASH_REF_DYNAMIC)
6032 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
6034 (*_bfd_error_handler)
6035 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6036 finfo->output_bfd, h->root.u.def.section->owner,
6037 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
6038 ? "internal"
6039 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
6040 ? "hidden" : "local",
6041 h->root.root.string);
6042 eoinfo->failed = TRUE;
6043 return FALSE;
6046 /* We don't want to output symbols that have never been mentioned by
6047 a regular file, or that we have been told to strip. However, if
6048 h->indx is set to -2, the symbol is used by a reloc and we must
6049 output it. */
6050 if (h->indx == -2)
6051 strip = FALSE;
6052 else if (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
6053 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
6054 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
6055 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
6056 strip = TRUE;
6057 else if (finfo->info->strip == strip_all)
6058 strip = TRUE;
6059 else if (finfo->info->strip == strip_some
6060 && bfd_hash_lookup (finfo->info->keep_hash,
6061 h->root.root.string, FALSE, FALSE) == NULL)
6062 strip = TRUE;
6063 else if (finfo->info->strip_discarded
6064 && (h->root.type == bfd_link_hash_defined
6065 || h->root.type == bfd_link_hash_defweak)
6066 && elf_discarded_section (h->root.u.def.section))
6067 strip = TRUE;
6068 else
6069 strip = FALSE;
6071 /* If we're stripping it, and it's not a dynamic symbol, there's
6072 nothing else to do unless it is a forced local symbol. */
6073 if (strip
6074 && h->dynindx == -1
6075 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
6076 return TRUE;
6078 sym.st_value = 0;
6079 sym.st_size = h->size;
6080 sym.st_other = h->other;
6081 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
6082 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
6083 else if (h->root.type == bfd_link_hash_undefweak
6084 || h->root.type == bfd_link_hash_defweak)
6085 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
6086 else
6087 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
6089 switch (h->root.type)
6091 default:
6092 case bfd_link_hash_new:
6093 case bfd_link_hash_warning:
6094 abort ();
6095 return FALSE;
6097 case bfd_link_hash_undefined:
6098 case bfd_link_hash_undefweak:
6099 input_sec = bfd_und_section_ptr;
6100 sym.st_shndx = SHN_UNDEF;
6101 break;
6103 case bfd_link_hash_defined:
6104 case bfd_link_hash_defweak:
6106 input_sec = h->root.u.def.section;
6107 if (input_sec->output_section != NULL)
6109 sym.st_shndx =
6110 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
6111 input_sec->output_section);
6112 if (sym.st_shndx == SHN_BAD)
6114 (*_bfd_error_handler)
6115 (_("%B: could not find output section %A for input section %A"),
6116 finfo->output_bfd, input_sec->output_section, input_sec);
6117 eoinfo->failed = TRUE;
6118 return FALSE;
6121 /* ELF symbols in relocatable files are section relative,
6122 but in nonrelocatable files they are virtual
6123 addresses. */
6124 sym.st_value = h->root.u.def.value + input_sec->output_offset;
6125 if (! finfo->info->relocatable)
6127 sym.st_value += input_sec->output_section->vma;
6128 if (h->type == STT_TLS)
6130 /* STT_TLS symbols are relative to PT_TLS segment
6131 base. */
6132 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6133 sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6137 else
6139 BFD_ASSERT (input_sec->owner == NULL
6140 || (input_sec->owner->flags & DYNAMIC) != 0);
6141 sym.st_shndx = SHN_UNDEF;
6142 input_sec = bfd_und_section_ptr;
6145 break;
6147 case bfd_link_hash_common:
6148 input_sec = h->root.u.c.p->section;
6149 sym.st_shndx = SHN_COMMON;
6150 sym.st_value = 1 << h->root.u.c.p->alignment_power;
6151 break;
6153 case bfd_link_hash_indirect:
6154 /* These symbols are created by symbol versioning. They point
6155 to the decorated version of the name. For example, if the
6156 symbol foo@@GNU_1.2 is the default, which should be used when
6157 foo is used with no version, then we add an indirect symbol
6158 foo which points to foo@@GNU_1.2. We ignore these symbols,
6159 since the indirected symbol is already in the hash table. */
6160 return TRUE;
6163 /* Give the processor backend a chance to tweak the symbol value,
6164 and also to finish up anything that needs to be done for this
6165 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6166 forced local syms when non-shared is due to a historical quirk. */
6167 if ((h->dynindx != -1
6168 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
6169 && ((finfo->info->shared
6170 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6171 || h->root.type != bfd_link_hash_undefweak))
6172 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
6173 && elf_hash_table (finfo->info)->dynamic_sections_created)
6175 if (! ((*bed->elf_backend_finish_dynamic_symbol)
6176 (finfo->output_bfd, finfo->info, h, &sym)))
6178 eoinfo->failed = TRUE;
6179 return FALSE;
6183 /* If we are marking the symbol as undefined, and there are no
6184 non-weak references to this symbol from a regular object, then
6185 mark the symbol as weak undefined; if there are non-weak
6186 references, mark the symbol as strong. We can't do this earlier,
6187 because it might not be marked as undefined until the
6188 finish_dynamic_symbol routine gets through with it. */
6189 if (sym.st_shndx == SHN_UNDEF
6190 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
6191 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
6192 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
6194 int bindtype;
6196 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK) != 0)
6197 bindtype = STB_GLOBAL;
6198 else
6199 bindtype = STB_WEAK;
6200 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
6203 /* If a non-weak symbol with non-default visibility is not defined
6204 locally, it is a fatal error. */
6205 if (! finfo->info->relocatable
6206 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
6207 && ELF_ST_BIND (sym.st_info) != STB_WEAK
6208 && h->root.type == bfd_link_hash_undefined
6209 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6211 (*_bfd_error_handler)
6212 (_("%B: %s symbol `%s' isn't defined"),
6213 finfo->output_bfd,
6214 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
6215 ? "protected"
6216 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
6217 ? "internal" : "hidden",
6218 h->root.root.string);
6219 eoinfo->failed = TRUE;
6220 return FALSE;
6223 /* If this symbol should be put in the .dynsym section, then put it
6224 there now. We already know the symbol index. We also fill in
6225 the entry in the .hash section. */
6226 if (h->dynindx != -1
6227 && elf_hash_table (finfo->info)->dynamic_sections_created)
6229 size_t bucketcount;
6230 size_t bucket;
6231 size_t hash_entry_size;
6232 bfd_byte *bucketpos;
6233 bfd_vma chain;
6234 bfd_byte *esym;
6236 sym.st_name = h->dynstr_index;
6237 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
6238 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
6240 bucketcount = elf_hash_table (finfo->info)->bucketcount;
6241 bucket = h->elf_hash_value % bucketcount;
6242 hash_entry_size
6243 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
6244 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
6245 + (bucket + 2) * hash_entry_size);
6246 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
6247 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
6248 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
6249 ((bfd_byte *) finfo->hash_sec->contents
6250 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
6252 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
6254 Elf_Internal_Versym iversym;
6255 Elf_External_Versym *eversym;
6257 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6259 if (h->verinfo.verdef == NULL)
6260 iversym.vs_vers = 0;
6261 else
6262 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
6264 else
6266 if (h->verinfo.vertree == NULL)
6267 iversym.vs_vers = 1;
6268 else
6269 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
6272 if ((h->elf_link_hash_flags & ELF_LINK_HIDDEN) != 0)
6273 iversym.vs_vers |= VERSYM_HIDDEN;
6275 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
6276 eversym += h->dynindx;
6277 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
6281 /* If we're stripping it, then it was just a dynamic symbol, and
6282 there's nothing else to do. */
6283 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
6284 return TRUE;
6286 h->indx = bfd_get_symcount (finfo->output_bfd);
6288 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
6290 eoinfo->failed = TRUE;
6291 return FALSE;
6294 return TRUE;
6297 /* Return TRUE if special handling is done for relocs in SEC against
6298 symbols defined in discarded sections. */
6300 static bfd_boolean
6301 elf_section_ignore_discarded_relocs (asection *sec)
6303 const struct elf_backend_data *bed;
6305 switch (sec->sec_info_type)
6307 case ELF_INFO_TYPE_STABS:
6308 case ELF_INFO_TYPE_EH_FRAME:
6309 return TRUE;
6310 default:
6311 break;
6314 bed = get_elf_backend_data (sec->owner);
6315 if (bed->elf_backend_ignore_discarded_relocs != NULL
6316 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
6317 return TRUE;
6319 return FALSE;
6322 /* Return TRUE if we should complain about a reloc in SEC against a
6323 symbol defined in a discarded section. */
6325 static bfd_boolean
6326 elf_section_complain_discarded (asection *sec)
6328 if (strncmp (".stab", sec->name, 5) == 0
6329 && (!sec->name[5] ||
6330 (sec->name[5] == '.' && ISDIGIT (sec->name[6]))))
6331 return FALSE;
6333 if (strcmp (".eh_frame", sec->name) == 0)
6334 return FALSE;
6336 if (strcmp (".gcc_except_table", sec->name) == 0)
6337 return FALSE;
6339 if (strcmp (".PARISC.unwind", sec->name) == 0)
6340 return FALSE;
6342 if (strcmp (".fixup", sec->name) == 0)
6343 return FALSE;
6345 return TRUE;
6348 /* Find a match between a section and a member of a section group. */
6350 static asection *
6351 match_group_member (asection *sec, asection *group)
6353 asection *first = elf_next_in_group (group);
6354 asection *s = first;
6356 while (s != NULL)
6358 if (bfd_elf_match_symbols_in_sections (s, sec))
6359 return s;
6361 if (s == first)
6362 break;
6365 return NULL;
6368 /* Link an input file into the linker output file. This function
6369 handles all the sections and relocations of the input file at once.
6370 This is so that we only have to read the local symbols once, and
6371 don't have to keep them in memory. */
6373 static bfd_boolean
6374 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
6376 bfd_boolean (*relocate_section)
6377 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
6378 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
6379 bfd *output_bfd;
6380 Elf_Internal_Shdr *symtab_hdr;
6381 size_t locsymcount;
6382 size_t extsymoff;
6383 Elf_Internal_Sym *isymbuf;
6384 Elf_Internal_Sym *isym;
6385 Elf_Internal_Sym *isymend;
6386 long *pindex;
6387 asection **ppsection;
6388 asection *o;
6389 const struct elf_backend_data *bed;
6390 bfd_boolean emit_relocs;
6391 struct elf_link_hash_entry **sym_hashes;
6393 output_bfd = finfo->output_bfd;
6394 bed = get_elf_backend_data (output_bfd);
6395 relocate_section = bed->elf_backend_relocate_section;
6397 /* If this is a dynamic object, we don't want to do anything here:
6398 we don't want the local symbols, and we don't want the section
6399 contents. */
6400 if ((input_bfd->flags & DYNAMIC) != 0)
6401 return TRUE;
6403 emit_relocs = (finfo->info->relocatable
6404 || finfo->info->emitrelocations
6405 || bed->elf_backend_emit_relocs);
6407 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6408 if (elf_bad_symtab (input_bfd))
6410 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
6411 extsymoff = 0;
6413 else
6415 locsymcount = symtab_hdr->sh_info;
6416 extsymoff = symtab_hdr->sh_info;
6419 /* Read the local symbols. */
6420 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
6421 if (isymbuf == NULL && locsymcount != 0)
6423 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
6424 finfo->internal_syms,
6425 finfo->external_syms,
6426 finfo->locsym_shndx);
6427 if (isymbuf == NULL)
6428 return FALSE;
6431 /* Find local symbol sections and adjust values of symbols in
6432 SEC_MERGE sections. Write out those local symbols we know are
6433 going into the output file. */
6434 isymend = isymbuf + locsymcount;
6435 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
6436 isym < isymend;
6437 isym++, pindex++, ppsection++)
6439 asection *isec;
6440 const char *name;
6441 Elf_Internal_Sym osym;
6443 *pindex = -1;
6445 if (elf_bad_symtab (input_bfd))
6447 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
6449 *ppsection = NULL;
6450 continue;
6454 if (isym->st_shndx == SHN_UNDEF)
6455 isec = bfd_und_section_ptr;
6456 else if (isym->st_shndx < SHN_LORESERVE
6457 || isym->st_shndx > SHN_HIRESERVE)
6459 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
6460 if (isec
6461 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
6462 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
6463 isym->st_value =
6464 _bfd_merged_section_offset (output_bfd, &isec,
6465 elf_section_data (isec)->sec_info,
6466 isym->st_value);
6468 else if (isym->st_shndx == SHN_ABS)
6469 isec = bfd_abs_section_ptr;
6470 else if (isym->st_shndx == SHN_COMMON)
6471 isec = bfd_com_section_ptr;
6472 else
6474 /* Who knows? */
6475 isec = NULL;
6478 *ppsection = isec;
6480 /* Don't output the first, undefined, symbol. */
6481 if (ppsection == finfo->sections)
6482 continue;
6484 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6486 /* We never output section symbols. Instead, we use the
6487 section symbol of the corresponding section in the output
6488 file. */
6489 continue;
6492 /* If we are stripping all symbols, we don't want to output this
6493 one. */
6494 if (finfo->info->strip == strip_all)
6495 continue;
6497 /* If we are discarding all local symbols, we don't want to
6498 output this one. If we are generating a relocatable output
6499 file, then some of the local symbols may be required by
6500 relocs; we output them below as we discover that they are
6501 needed. */
6502 if (finfo->info->discard == discard_all)
6503 continue;
6505 /* If this symbol is defined in a section which we are
6506 discarding, we don't need to keep it, but note that
6507 linker_mark is only reliable for sections that have contents.
6508 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6509 as well as linker_mark. */
6510 if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
6511 && isec != NULL
6512 && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
6513 || (! finfo->info->relocatable
6514 && (isec->flags & SEC_EXCLUDE) != 0)))
6515 continue;
6517 /* Get the name of the symbol. */
6518 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
6519 isym->st_name);
6520 if (name == NULL)
6521 return FALSE;
6523 /* See if we are discarding symbols with this name. */
6524 if ((finfo->info->strip == strip_some
6525 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
6526 == NULL))
6527 || (((finfo->info->discard == discard_sec_merge
6528 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
6529 || finfo->info->discard == discard_l)
6530 && bfd_is_local_label_name (input_bfd, name)))
6531 continue;
6533 /* If we get here, we are going to output this symbol. */
6535 osym = *isym;
6537 /* Adjust the section index for the output file. */
6538 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
6539 isec->output_section);
6540 if (osym.st_shndx == SHN_BAD)
6541 return FALSE;
6543 *pindex = bfd_get_symcount (output_bfd);
6545 /* ELF symbols in relocatable files are section relative, but
6546 in executable files they are virtual addresses. Note that
6547 this code assumes that all ELF sections have an associated
6548 BFD section with a reasonable value for output_offset; below
6549 we assume that they also have a reasonable value for
6550 output_section. Any special sections must be set up to meet
6551 these requirements. */
6552 osym.st_value += isec->output_offset;
6553 if (! finfo->info->relocatable)
6555 osym.st_value += isec->output_section->vma;
6556 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
6558 /* STT_TLS symbols are relative to PT_TLS segment base. */
6559 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6560 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6564 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
6565 return FALSE;
6568 /* Relocate the contents of each section. */
6569 sym_hashes = elf_sym_hashes (input_bfd);
6570 for (o = input_bfd->sections; o != NULL; o = o->next)
6572 bfd_byte *contents;
6574 if (! o->linker_mark)
6576 /* This section was omitted from the link. */
6577 continue;
6580 if ((o->flags & SEC_HAS_CONTENTS) == 0
6581 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
6582 continue;
6584 if ((o->flags & SEC_LINKER_CREATED) != 0)
6586 /* Section was created by _bfd_elf_link_create_dynamic_sections
6587 or somesuch. */
6588 continue;
6591 /* Get the contents of the section. They have been cached by a
6592 relaxation routine. Note that o is a section in an input
6593 file, so the contents field will not have been set by any of
6594 the routines which work on output files. */
6595 if (elf_section_data (o)->this_hdr.contents != NULL)
6596 contents = elf_section_data (o)->this_hdr.contents;
6597 else
6599 bfd_size_type amt = o->rawsize ? o->rawsize : o->size;
6601 contents = finfo->contents;
6602 if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt))
6603 return FALSE;
6606 if ((o->flags & SEC_RELOC) != 0)
6608 Elf_Internal_Rela *internal_relocs;
6609 bfd_vma r_type_mask;
6610 int r_sym_shift;
6612 /* Get the swapped relocs. */
6613 internal_relocs
6614 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
6615 finfo->internal_relocs, FALSE);
6616 if (internal_relocs == NULL
6617 && o->reloc_count > 0)
6618 return FALSE;
6620 if (bed->s->arch_size == 32)
6622 r_type_mask = 0xff;
6623 r_sym_shift = 8;
6625 else
6627 r_type_mask = 0xffffffff;
6628 r_sym_shift = 32;
6631 /* Run through the relocs looking for any against symbols
6632 from discarded sections and section symbols from
6633 removed link-once sections. Complain about relocs
6634 against discarded sections. Zero relocs against removed
6635 link-once sections. Preserve debug information as much
6636 as we can. */
6637 if (!elf_section_ignore_discarded_relocs (o))
6639 Elf_Internal_Rela *rel, *relend;
6640 bfd_boolean complain = elf_section_complain_discarded (o);
6642 rel = internal_relocs;
6643 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
6644 for ( ; rel < relend; rel++)
6646 unsigned long r_symndx = rel->r_info >> r_sym_shift;
6647 asection **ps, *sec;
6648 struct elf_link_hash_entry *h = NULL;
6649 const char *sym_name;
6651 if (r_symndx == STN_UNDEF)
6652 continue;
6654 if (r_symndx >= locsymcount
6655 || (elf_bad_symtab (input_bfd)
6656 && finfo->sections[r_symndx] == NULL))
6658 h = sym_hashes[r_symndx - extsymoff];
6659 while (h->root.type == bfd_link_hash_indirect
6660 || h->root.type == bfd_link_hash_warning)
6661 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6663 if (h->root.type != bfd_link_hash_defined
6664 && h->root.type != bfd_link_hash_defweak)
6665 continue;
6667 ps = &h->root.u.def.section;
6668 sym_name = h->root.root.string;
6670 else
6672 Elf_Internal_Sym *sym = isymbuf + r_symndx;
6673 ps = &finfo->sections[r_symndx];
6674 sym_name = bfd_elf_local_sym_name (input_bfd, sym);
6677 /* Complain if the definition comes from a
6678 discarded section. */
6679 if ((sec = *ps) != NULL && elf_discarded_section (sec))
6681 if ((o->flags & SEC_DEBUGGING) != 0)
6683 BFD_ASSERT (r_symndx != 0);
6685 /* Try to preserve debug information.
6686 FIXME: This is quite broken. Modifying
6687 the symbol here means we will be changing
6688 all uses of the symbol, not just those in
6689 debug sections. The only thing that makes
6690 this half reasonable is that debug sections
6691 tend to come after other sections. Of
6692 course, that doesn't help with globals.
6693 ??? All link-once sections of the same name
6694 ought to define the same set of symbols, so
6695 it would seem that globals ought to always
6696 be defined in the kept section. */
6697 if (sec->kept_section != NULL)
6699 asection *member;
6701 /* Check if it is a linkonce section or
6702 member of a comdat group. */
6703 if (elf_sec_group (sec) == NULL
6704 && sec->size == sec->kept_section->size)
6706 *ps = sec->kept_section;
6707 continue;
6709 else if (elf_sec_group (sec) != NULL
6710 && (member = match_group_member (sec, sec->kept_section))
6711 && sec->size == member->size)
6713 *ps = member;
6714 continue;
6718 else if (complain)
6720 (*_bfd_error_handler)
6721 (_("`%s' referenced in section `%A' of %B: "
6722 "defined in discarded section `%A' of %B\n"),
6723 o, input_bfd, sec, sec->owner, sym_name);
6726 /* Remove the symbol reference from the reloc, but
6727 don't kill the reloc completely. This is so that
6728 a zero value will be written into the section,
6729 which may have non-zero contents put there by the
6730 assembler. Zero in things like an eh_frame fde
6731 pc_begin allows stack unwinders to recognize the
6732 fde as bogus. */
6733 rel->r_info &= r_type_mask;
6734 rel->r_addend = 0;
6739 /* Relocate the section by invoking a back end routine.
6741 The back end routine is responsible for adjusting the
6742 section contents as necessary, and (if using Rela relocs
6743 and generating a relocatable output file) adjusting the
6744 reloc addend as necessary.
6746 The back end routine does not have to worry about setting
6747 the reloc address or the reloc symbol index.
6749 The back end routine is given a pointer to the swapped in
6750 internal symbols, and can access the hash table entries
6751 for the external symbols via elf_sym_hashes (input_bfd).
6753 When generating relocatable output, the back end routine
6754 must handle STB_LOCAL/STT_SECTION symbols specially. The
6755 output symbol is going to be a section symbol
6756 corresponding to the output section, which will require
6757 the addend to be adjusted. */
6759 if (! (*relocate_section) (output_bfd, finfo->info,
6760 input_bfd, o, contents,
6761 internal_relocs,
6762 isymbuf,
6763 finfo->sections))
6764 return FALSE;
6766 if (emit_relocs)
6768 Elf_Internal_Rela *irela;
6769 Elf_Internal_Rela *irelaend;
6770 bfd_vma last_offset;
6771 struct elf_link_hash_entry **rel_hash;
6772 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
6773 unsigned int next_erel;
6774 bfd_boolean (*reloc_emitter)
6775 (bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *);
6776 bfd_boolean rela_normal;
6778 input_rel_hdr = &elf_section_data (o)->rel_hdr;
6779 rela_normal = (bed->rela_normal
6780 && (input_rel_hdr->sh_entsize
6781 == bed->s->sizeof_rela));
6783 /* Adjust the reloc addresses and symbol indices. */
6785 irela = internal_relocs;
6786 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
6787 rel_hash = (elf_section_data (o->output_section)->rel_hashes
6788 + elf_section_data (o->output_section)->rel_count
6789 + elf_section_data (o->output_section)->rel_count2);
6790 last_offset = o->output_offset;
6791 if (!finfo->info->relocatable)
6792 last_offset += o->output_section->vma;
6793 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
6795 unsigned long r_symndx;
6796 asection *sec;
6797 Elf_Internal_Sym sym;
6799 if (next_erel == bed->s->int_rels_per_ext_rel)
6801 rel_hash++;
6802 next_erel = 0;
6805 irela->r_offset = _bfd_elf_section_offset (output_bfd,
6806 finfo->info, o,
6807 irela->r_offset);
6808 if (irela->r_offset >= (bfd_vma) -2)
6810 /* This is a reloc for a deleted entry or somesuch.
6811 Turn it into an R_*_NONE reloc, at the same
6812 offset as the last reloc. elf_eh_frame.c and
6813 elf_bfd_discard_info rely on reloc offsets
6814 being ordered. */
6815 irela->r_offset = last_offset;
6816 irela->r_info = 0;
6817 irela->r_addend = 0;
6818 continue;
6821 irela->r_offset += o->output_offset;
6823 /* Relocs in an executable have to be virtual addresses. */
6824 if (!finfo->info->relocatable)
6825 irela->r_offset += o->output_section->vma;
6827 last_offset = irela->r_offset;
6829 r_symndx = irela->r_info >> r_sym_shift;
6830 if (r_symndx == STN_UNDEF)
6831 continue;
6833 if (r_symndx >= locsymcount
6834 || (elf_bad_symtab (input_bfd)
6835 && finfo->sections[r_symndx] == NULL))
6837 struct elf_link_hash_entry *rh;
6838 unsigned long indx;
6840 /* This is a reloc against a global symbol. We
6841 have not yet output all the local symbols, so
6842 we do not know the symbol index of any global
6843 symbol. We set the rel_hash entry for this
6844 reloc to point to the global hash table entry
6845 for this symbol. The symbol index is then
6846 set at the end of bfd_elf_final_link. */
6847 indx = r_symndx - extsymoff;
6848 rh = elf_sym_hashes (input_bfd)[indx];
6849 while (rh->root.type == bfd_link_hash_indirect
6850 || rh->root.type == bfd_link_hash_warning)
6851 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
6853 /* Setting the index to -2 tells
6854 elf_link_output_extsym that this symbol is
6855 used by a reloc. */
6856 BFD_ASSERT (rh->indx < 0);
6857 rh->indx = -2;
6859 *rel_hash = rh;
6861 continue;
6864 /* This is a reloc against a local symbol. */
6866 *rel_hash = NULL;
6867 sym = isymbuf[r_symndx];
6868 sec = finfo->sections[r_symndx];
6869 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
6871 /* I suppose the backend ought to fill in the
6872 section of any STT_SECTION symbol against a
6873 processor specific section. */
6874 r_symndx = 0;
6875 if (bfd_is_abs_section (sec))
6877 else if (sec == NULL || sec->owner == NULL)
6879 bfd_set_error (bfd_error_bad_value);
6880 return FALSE;
6882 else
6884 asection *osec = sec->output_section;
6886 /* If we have discarded a section, the output
6887 section will be the absolute section. In
6888 case of discarded link-once and discarded
6889 SEC_MERGE sections, use the kept section. */
6890 if (bfd_is_abs_section (osec)
6891 && sec->kept_section != NULL
6892 && sec->kept_section->output_section != NULL)
6894 osec = sec->kept_section->output_section;
6895 irela->r_addend -= osec->vma;
6898 if (!bfd_is_abs_section (osec))
6900 r_symndx = osec->target_index;
6901 BFD_ASSERT (r_symndx != 0);
6905 /* Adjust the addend according to where the
6906 section winds up in the output section. */
6907 if (rela_normal)
6908 irela->r_addend += sec->output_offset;
6910 else
6912 if (finfo->indices[r_symndx] == -1)
6914 unsigned long shlink;
6915 const char *name;
6916 asection *osec;
6918 if (finfo->info->strip == strip_all)
6920 /* You can't do ld -r -s. */
6921 bfd_set_error (bfd_error_invalid_operation);
6922 return FALSE;
6925 /* This symbol was skipped earlier, but
6926 since it is needed by a reloc, we
6927 must output it now. */
6928 shlink = symtab_hdr->sh_link;
6929 name = (bfd_elf_string_from_elf_section
6930 (input_bfd, shlink, sym.st_name));
6931 if (name == NULL)
6932 return FALSE;
6934 osec = sec->output_section;
6935 sym.st_shndx =
6936 _bfd_elf_section_from_bfd_section (output_bfd,
6937 osec);
6938 if (sym.st_shndx == SHN_BAD)
6939 return FALSE;
6941 sym.st_value += sec->output_offset;
6942 if (! finfo->info->relocatable)
6944 sym.st_value += osec->vma;
6945 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
6947 /* STT_TLS symbols are relative to PT_TLS
6948 segment base. */
6949 BFD_ASSERT (elf_hash_table (finfo->info)
6950 ->tls_sec != NULL);
6951 sym.st_value -= (elf_hash_table (finfo->info)
6952 ->tls_sec->vma);
6956 finfo->indices[r_symndx]
6957 = bfd_get_symcount (output_bfd);
6959 if (! elf_link_output_sym (finfo, name, &sym, sec,
6960 NULL))
6961 return FALSE;
6964 r_symndx = finfo->indices[r_symndx];
6967 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
6968 | (irela->r_info & r_type_mask));
6971 /* Swap out the relocs. */
6972 if (bed->elf_backend_emit_relocs
6973 && !(finfo->info->relocatable
6974 || finfo->info->emitrelocations))
6975 reloc_emitter = bed->elf_backend_emit_relocs;
6976 else
6977 reloc_emitter = _bfd_elf_link_output_relocs;
6979 if (input_rel_hdr->sh_size != 0
6980 && ! (*reloc_emitter) (output_bfd, o, input_rel_hdr,
6981 internal_relocs))
6982 return FALSE;
6984 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
6985 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
6987 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
6988 * bed->s->int_rels_per_ext_rel);
6989 if (! (*reloc_emitter) (output_bfd, o, input_rel_hdr2,
6990 internal_relocs))
6991 return FALSE;
6996 /* Write out the modified section contents. */
6997 if (bed->elf_backend_write_section
6998 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
7000 /* Section written out. */
7002 else switch (o->sec_info_type)
7004 case ELF_INFO_TYPE_STABS:
7005 if (! (_bfd_write_section_stabs
7006 (output_bfd,
7007 &elf_hash_table (finfo->info)->stab_info,
7008 o, &elf_section_data (o)->sec_info, contents)))
7009 return FALSE;
7010 break;
7011 case ELF_INFO_TYPE_MERGE:
7012 if (! _bfd_write_merged_section (output_bfd, o,
7013 elf_section_data (o)->sec_info))
7014 return FALSE;
7015 break;
7016 case ELF_INFO_TYPE_EH_FRAME:
7018 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
7019 o, contents))
7020 return FALSE;
7022 break;
7023 default:
7025 if (! (o->flags & SEC_EXCLUDE)
7026 && ! bfd_set_section_contents (output_bfd, o->output_section,
7027 contents,
7028 (file_ptr) o->output_offset,
7029 o->size))
7030 return FALSE;
7032 break;
7036 return TRUE;
7039 /* Generate a reloc when linking an ELF file. This is a reloc
7040 requested by the linker, and does come from any input file. This
7041 is used to build constructor and destructor tables when linking
7042 with -Ur. */
7044 static bfd_boolean
7045 elf_reloc_link_order (bfd *output_bfd,
7046 struct bfd_link_info *info,
7047 asection *output_section,
7048 struct bfd_link_order *link_order)
7050 reloc_howto_type *howto;
7051 long indx;
7052 bfd_vma offset;
7053 bfd_vma addend;
7054 struct elf_link_hash_entry **rel_hash_ptr;
7055 Elf_Internal_Shdr *rel_hdr;
7056 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7057 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
7058 bfd_byte *erel;
7059 unsigned int i;
7061 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
7062 if (howto == NULL)
7064 bfd_set_error (bfd_error_bad_value);
7065 return FALSE;
7068 addend = link_order->u.reloc.p->addend;
7070 /* Figure out the symbol index. */
7071 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
7072 + elf_section_data (output_section)->rel_count
7073 + elf_section_data (output_section)->rel_count2);
7074 if (link_order->type == bfd_section_reloc_link_order)
7076 indx = link_order->u.reloc.p->u.section->target_index;
7077 BFD_ASSERT (indx != 0);
7078 *rel_hash_ptr = NULL;
7080 else
7082 struct elf_link_hash_entry *h;
7084 /* Treat a reloc against a defined symbol as though it were
7085 actually against the section. */
7086 h = ((struct elf_link_hash_entry *)
7087 bfd_wrapped_link_hash_lookup (output_bfd, info,
7088 link_order->u.reloc.p->u.name,
7089 FALSE, FALSE, TRUE));
7090 if (h != NULL
7091 && (h->root.type == bfd_link_hash_defined
7092 || h->root.type == bfd_link_hash_defweak))
7094 asection *section;
7096 section = h->root.u.def.section;
7097 indx = section->output_section->target_index;
7098 *rel_hash_ptr = NULL;
7099 /* It seems that we ought to add the symbol value to the
7100 addend here, but in practice it has already been added
7101 because it was passed to constructor_callback. */
7102 addend += section->output_section->vma + section->output_offset;
7104 else if (h != NULL)
7106 /* Setting the index to -2 tells elf_link_output_extsym that
7107 this symbol is used by a reloc. */
7108 h->indx = -2;
7109 *rel_hash_ptr = h;
7110 indx = 0;
7112 else
7114 if (! ((*info->callbacks->unattached_reloc)
7115 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
7116 return FALSE;
7117 indx = 0;
7121 /* If this is an inplace reloc, we must write the addend into the
7122 object file. */
7123 if (howto->partial_inplace && addend != 0)
7125 bfd_size_type size;
7126 bfd_reloc_status_type rstat;
7127 bfd_byte *buf;
7128 bfd_boolean ok;
7129 const char *sym_name;
7131 size = bfd_get_reloc_size (howto);
7132 buf = bfd_zmalloc (size);
7133 if (buf == NULL)
7134 return FALSE;
7135 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
7136 switch (rstat)
7138 case bfd_reloc_ok:
7139 break;
7141 default:
7142 case bfd_reloc_outofrange:
7143 abort ();
7145 case bfd_reloc_overflow:
7146 if (link_order->type == bfd_section_reloc_link_order)
7147 sym_name = bfd_section_name (output_bfd,
7148 link_order->u.reloc.p->u.section);
7149 else
7150 sym_name = link_order->u.reloc.p->u.name;
7151 if (! ((*info->callbacks->reloc_overflow)
7152 (info, sym_name, howto->name, addend, NULL, NULL, 0)))
7154 free (buf);
7155 return FALSE;
7157 break;
7159 ok = bfd_set_section_contents (output_bfd, output_section, buf,
7160 link_order->offset, size);
7161 free (buf);
7162 if (! ok)
7163 return FALSE;
7166 /* The address of a reloc is relative to the section in a
7167 relocatable file, and is a virtual address in an executable
7168 file. */
7169 offset = link_order->offset;
7170 if (! info->relocatable)
7171 offset += output_section->vma;
7173 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7175 irel[i].r_offset = offset;
7176 irel[i].r_info = 0;
7177 irel[i].r_addend = 0;
7179 if (bed->s->arch_size == 32)
7180 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
7181 else
7182 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
7184 rel_hdr = &elf_section_data (output_section)->rel_hdr;
7185 erel = rel_hdr->contents;
7186 if (rel_hdr->sh_type == SHT_REL)
7188 erel += (elf_section_data (output_section)->rel_count
7189 * bed->s->sizeof_rel);
7190 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
7192 else
7194 irel[0].r_addend = addend;
7195 erel += (elf_section_data (output_section)->rel_count
7196 * bed->s->sizeof_rela);
7197 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
7200 ++elf_section_data (output_section)->rel_count;
7202 return TRUE;
7206 /* Get the output vma of the section pointed to by the sh_link field. */
7208 static bfd_vma
7209 elf_get_linked_section_vma (struct bfd_link_order *p)
7211 Elf_Internal_Shdr **elf_shdrp;
7212 asection *s;
7213 int elfsec;
7215 s = p->u.indirect.section;
7216 elf_shdrp = elf_elfsections (s->owner);
7217 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
7218 elfsec = elf_shdrp[elfsec]->sh_link;
7219 /* PR 290:
7220 The Intel C compiler generates SHT_IA_64_UNWIND with
7221 SHF_LINK_ORDER. But it doesn't set theh sh_link or
7222 sh_info fields. Hence we could get the situation
7223 where elfsec is 0. */
7224 if (elfsec == 0)
7226 const struct elf_backend_data *bed
7227 = get_elf_backend_data (s->owner);
7228 if (bed->link_order_error_handler)
7229 bed->link_order_error_handler
7230 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
7231 return 0;
7233 else
7235 s = elf_shdrp[elfsec]->bfd_section;
7236 return s->output_section->vma + s->output_offset;
7241 /* Compare two sections based on the locations of the sections they are
7242 linked to. Used by elf_fixup_link_order. */
7244 static int
7245 compare_link_order (const void * a, const void * b)
7247 bfd_vma apos;
7248 bfd_vma bpos;
7250 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
7251 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
7252 if (apos < bpos)
7253 return -1;
7254 return apos > bpos;
7258 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7259 order as their linked sections. Returns false if this could not be done
7260 because an output section includes both ordered and unordered
7261 sections. Ideally we'd do this in the linker proper. */
7263 static bfd_boolean
7264 elf_fixup_link_order (bfd *abfd, asection *o)
7266 int seen_linkorder;
7267 int seen_other;
7268 int n;
7269 struct bfd_link_order *p;
7270 bfd *sub;
7271 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7272 int elfsec;
7273 struct bfd_link_order **sections;
7274 asection *s;
7275 bfd_vma offset;
7277 seen_other = 0;
7278 seen_linkorder = 0;
7279 for (p = o->link_order_head; p != NULL; p = p->next)
7281 if (p->type == bfd_indirect_link_order
7282 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
7283 == bfd_target_elf_flavour)
7284 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
7286 s = p->u.indirect.section;
7287 elfsec = _bfd_elf_section_from_bfd_section (sub, s);
7288 if (elfsec != -1
7289 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER)
7290 seen_linkorder++;
7291 else
7292 seen_other++;
7294 else
7295 seen_other++;
7298 if (!seen_linkorder)
7299 return TRUE;
7301 if (seen_other && seen_linkorder)
7303 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
7305 bfd_set_error (bfd_error_bad_value);
7306 return FALSE;
7309 sections = (struct bfd_link_order **)
7310 xmalloc (seen_linkorder * sizeof (struct bfd_link_order *));
7311 seen_linkorder = 0;
7313 for (p = o->link_order_head; p != NULL; p = p->next)
7315 sections[seen_linkorder++] = p;
7317 /* Sort the input sections in the order of their linked section. */
7318 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
7319 compare_link_order);
7321 /* Change the offsets of the sections. */
7322 offset = 0;
7323 for (n = 0; n < seen_linkorder; n++)
7325 s = sections[n]->u.indirect.section;
7326 offset &= ~(bfd_vma)((1 << s->alignment_power) - 1);
7327 s->output_offset = offset;
7328 sections[n]->offset = offset;
7329 offset += sections[n]->size;
7332 return TRUE;
7336 /* Do the final step of an ELF link. */
7338 bfd_boolean
7339 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
7341 bfd_boolean dynamic;
7342 bfd_boolean emit_relocs;
7343 bfd *dynobj;
7344 struct elf_final_link_info finfo;
7345 register asection *o;
7346 register struct bfd_link_order *p;
7347 register bfd *sub;
7348 bfd_size_type max_contents_size;
7349 bfd_size_type max_external_reloc_size;
7350 bfd_size_type max_internal_reloc_count;
7351 bfd_size_type max_sym_count;
7352 bfd_size_type max_sym_shndx_count;
7353 file_ptr off;
7354 Elf_Internal_Sym elfsym;
7355 unsigned int i;
7356 Elf_Internal_Shdr *symtab_hdr;
7357 Elf_Internal_Shdr *symtab_shndx_hdr;
7358 Elf_Internal_Shdr *symstrtab_hdr;
7359 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7360 struct elf_outext_info eoinfo;
7361 bfd_boolean merged;
7362 size_t relativecount = 0;
7363 asection *reldyn = 0;
7364 bfd_size_type amt;
7366 if (! is_elf_hash_table (info->hash))
7367 return FALSE;
7369 if (info->shared)
7370 abfd->flags |= DYNAMIC;
7372 dynamic = elf_hash_table (info)->dynamic_sections_created;
7373 dynobj = elf_hash_table (info)->dynobj;
7375 emit_relocs = (info->relocatable
7376 || info->emitrelocations
7377 || bed->elf_backend_emit_relocs);
7379 finfo.info = info;
7380 finfo.output_bfd = abfd;
7381 finfo.symstrtab = _bfd_elf_stringtab_init ();
7382 if (finfo.symstrtab == NULL)
7383 return FALSE;
7385 if (! dynamic)
7387 finfo.dynsym_sec = NULL;
7388 finfo.hash_sec = NULL;
7389 finfo.symver_sec = NULL;
7391 else
7393 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
7394 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
7395 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
7396 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
7397 /* Note that it is OK if symver_sec is NULL. */
7400 finfo.contents = NULL;
7401 finfo.external_relocs = NULL;
7402 finfo.internal_relocs = NULL;
7403 finfo.external_syms = NULL;
7404 finfo.locsym_shndx = NULL;
7405 finfo.internal_syms = NULL;
7406 finfo.indices = NULL;
7407 finfo.sections = NULL;
7408 finfo.symbuf = NULL;
7409 finfo.symshndxbuf = NULL;
7410 finfo.symbuf_count = 0;
7411 finfo.shndxbuf_size = 0;
7413 /* Count up the number of relocations we will output for each output
7414 section, so that we know the sizes of the reloc sections. We
7415 also figure out some maximum sizes. */
7416 max_contents_size = 0;
7417 max_external_reloc_size = 0;
7418 max_internal_reloc_count = 0;
7419 max_sym_count = 0;
7420 max_sym_shndx_count = 0;
7421 merged = FALSE;
7422 for (o = abfd->sections; o != NULL; o = o->next)
7424 struct bfd_elf_section_data *esdo = elf_section_data (o);
7425 o->reloc_count = 0;
7427 for (p = o->link_order_head; p != NULL; p = p->next)
7429 unsigned int reloc_count = 0;
7430 struct bfd_elf_section_data *esdi = NULL;
7431 unsigned int *rel_count1;
7433 if (p->type == bfd_section_reloc_link_order
7434 || p->type == bfd_symbol_reloc_link_order)
7435 reloc_count = 1;
7436 else if (p->type == bfd_indirect_link_order)
7438 asection *sec;
7440 sec = p->u.indirect.section;
7441 esdi = elf_section_data (sec);
7443 /* Mark all sections which are to be included in the
7444 link. This will normally be every section. We need
7445 to do this so that we can identify any sections which
7446 the linker has decided to not include. */
7447 sec->linker_mark = TRUE;
7449 if (sec->flags & SEC_MERGE)
7450 merged = TRUE;
7452 if (info->relocatable || info->emitrelocations)
7453 reloc_count = sec->reloc_count;
7454 else if (bed->elf_backend_count_relocs)
7456 Elf_Internal_Rela * relocs;
7458 relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
7459 info->keep_memory);
7461 reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs);
7463 if (elf_section_data (o)->relocs != relocs)
7464 free (relocs);
7467 if (sec->rawsize > max_contents_size)
7468 max_contents_size = sec->rawsize;
7469 if (sec->size > max_contents_size)
7470 max_contents_size = sec->size;
7472 /* We are interested in just local symbols, not all
7473 symbols. */
7474 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
7475 && (sec->owner->flags & DYNAMIC) == 0)
7477 size_t sym_count;
7479 if (elf_bad_symtab (sec->owner))
7480 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
7481 / bed->s->sizeof_sym);
7482 else
7483 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
7485 if (sym_count > max_sym_count)
7486 max_sym_count = sym_count;
7488 if (sym_count > max_sym_shndx_count
7489 && elf_symtab_shndx (sec->owner) != 0)
7490 max_sym_shndx_count = sym_count;
7492 if ((sec->flags & SEC_RELOC) != 0)
7494 size_t ext_size;
7496 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
7497 if (ext_size > max_external_reloc_size)
7498 max_external_reloc_size = ext_size;
7499 if (sec->reloc_count > max_internal_reloc_count)
7500 max_internal_reloc_count = sec->reloc_count;
7505 if (reloc_count == 0)
7506 continue;
7508 o->reloc_count += reloc_count;
7510 /* MIPS may have a mix of REL and RELA relocs on sections.
7511 To support this curious ABI we keep reloc counts in
7512 elf_section_data too. We must be careful to add the
7513 relocations from the input section to the right output
7514 count. FIXME: Get rid of one count. We have
7515 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7516 rel_count1 = &esdo->rel_count;
7517 if (esdi != NULL)
7519 bfd_boolean same_size;
7520 bfd_size_type entsize1;
7522 entsize1 = esdi->rel_hdr.sh_entsize;
7523 BFD_ASSERT (entsize1 == bed->s->sizeof_rel
7524 || entsize1 == bed->s->sizeof_rela);
7525 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
7527 if (!same_size)
7528 rel_count1 = &esdo->rel_count2;
7530 if (esdi->rel_hdr2 != NULL)
7532 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
7533 unsigned int alt_count;
7534 unsigned int *rel_count2;
7536 BFD_ASSERT (entsize2 != entsize1
7537 && (entsize2 == bed->s->sizeof_rel
7538 || entsize2 == bed->s->sizeof_rela));
7540 rel_count2 = &esdo->rel_count2;
7541 if (!same_size)
7542 rel_count2 = &esdo->rel_count;
7544 /* The following is probably too simplistic if the
7545 backend counts output relocs unusually. */
7546 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
7547 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
7548 *rel_count2 += alt_count;
7549 reloc_count -= alt_count;
7552 *rel_count1 += reloc_count;
7555 if (o->reloc_count > 0)
7556 o->flags |= SEC_RELOC;
7557 else
7559 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7560 set it (this is probably a bug) and if it is set
7561 assign_section_numbers will create a reloc section. */
7562 o->flags &=~ SEC_RELOC;
7565 /* If the SEC_ALLOC flag is not set, force the section VMA to
7566 zero. This is done in elf_fake_sections as well, but forcing
7567 the VMA to 0 here will ensure that relocs against these
7568 sections are handled correctly. */
7569 if ((o->flags & SEC_ALLOC) == 0
7570 && ! o->user_set_vma)
7571 o->vma = 0;
7574 if (! info->relocatable && merged)
7575 elf_link_hash_traverse (elf_hash_table (info),
7576 _bfd_elf_link_sec_merge_syms, abfd);
7578 /* Figure out the file positions for everything but the symbol table
7579 and the relocs. We set symcount to force assign_section_numbers
7580 to create a symbol table. */
7581 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
7582 BFD_ASSERT (! abfd->output_has_begun);
7583 if (! _bfd_elf_compute_section_file_positions (abfd, info))
7584 goto error_return;
7586 /* Set sizes, and assign file positions for reloc sections. */
7587 for (o = abfd->sections; o != NULL; o = o->next)
7589 if ((o->flags & SEC_RELOC) != 0)
7591 if (!(_bfd_elf_link_size_reloc_section
7592 (abfd, &elf_section_data (o)->rel_hdr, o)))
7593 goto error_return;
7595 if (elf_section_data (o)->rel_hdr2
7596 && !(_bfd_elf_link_size_reloc_section
7597 (abfd, elf_section_data (o)->rel_hdr2, o)))
7598 goto error_return;
7601 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
7602 to count upwards while actually outputting the relocations. */
7603 elf_section_data (o)->rel_count = 0;
7604 elf_section_data (o)->rel_count2 = 0;
7607 _bfd_elf_assign_file_positions_for_relocs (abfd);
7609 /* We have now assigned file positions for all the sections except
7610 .symtab and .strtab. We start the .symtab section at the current
7611 file position, and write directly to it. We build the .strtab
7612 section in memory. */
7613 bfd_get_symcount (abfd) = 0;
7614 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7615 /* sh_name is set in prep_headers. */
7616 symtab_hdr->sh_type = SHT_SYMTAB;
7617 /* sh_flags, sh_addr and sh_size all start off zero. */
7618 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
7619 /* sh_link is set in assign_section_numbers. */
7620 /* sh_info is set below. */
7621 /* sh_offset is set just below. */
7622 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
7624 off = elf_tdata (abfd)->next_file_pos;
7625 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
7627 /* Note that at this point elf_tdata (abfd)->next_file_pos is
7628 incorrect. We do not yet know the size of the .symtab section.
7629 We correct next_file_pos below, after we do know the size. */
7631 /* Allocate a buffer to hold swapped out symbols. This is to avoid
7632 continuously seeking to the right position in the file. */
7633 if (! info->keep_memory || max_sym_count < 20)
7634 finfo.symbuf_size = 20;
7635 else
7636 finfo.symbuf_size = max_sym_count;
7637 amt = finfo.symbuf_size;
7638 amt *= bed->s->sizeof_sym;
7639 finfo.symbuf = bfd_malloc (amt);
7640 if (finfo.symbuf == NULL)
7641 goto error_return;
7642 if (elf_numsections (abfd) > SHN_LORESERVE)
7644 /* Wild guess at number of output symbols. realloc'd as needed. */
7645 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
7646 finfo.shndxbuf_size = amt;
7647 amt *= sizeof (Elf_External_Sym_Shndx);
7648 finfo.symshndxbuf = bfd_zmalloc (amt);
7649 if (finfo.symshndxbuf == NULL)
7650 goto error_return;
7653 /* Start writing out the symbol table. The first symbol is always a
7654 dummy symbol. */
7655 if (info->strip != strip_all
7656 || emit_relocs)
7658 elfsym.st_value = 0;
7659 elfsym.st_size = 0;
7660 elfsym.st_info = 0;
7661 elfsym.st_other = 0;
7662 elfsym.st_shndx = SHN_UNDEF;
7663 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
7664 NULL))
7665 goto error_return;
7668 #if 0
7669 /* Some standard ELF linkers do this, but we don't because it causes
7670 bootstrap comparison failures. */
7671 /* Output a file symbol for the output file as the second symbol.
7672 We output this even if we are discarding local symbols, although
7673 I'm not sure if this is correct. */
7674 elfsym.st_value = 0;
7675 elfsym.st_size = 0;
7676 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
7677 elfsym.st_other = 0;
7678 elfsym.st_shndx = SHN_ABS;
7679 if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd),
7680 &elfsym, bfd_abs_section_ptr, NULL))
7681 goto error_return;
7682 #endif
7684 /* Output a symbol for each section. We output these even if we are
7685 discarding local symbols, since they are used for relocs. These
7686 symbols have no names. We store the index of each one in the
7687 index field of the section, so that we can find it again when
7688 outputting relocs. */
7689 if (info->strip != strip_all
7690 || emit_relocs)
7692 elfsym.st_size = 0;
7693 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
7694 elfsym.st_other = 0;
7695 for (i = 1; i < elf_numsections (abfd); i++)
7697 o = bfd_section_from_elf_index (abfd, i);
7698 if (o != NULL)
7699 o->target_index = bfd_get_symcount (abfd);
7700 elfsym.st_shndx = i;
7701 if (info->relocatable || o == NULL)
7702 elfsym.st_value = 0;
7703 else
7704 elfsym.st_value = o->vma;
7705 if (! elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
7706 goto error_return;
7707 if (i == SHN_LORESERVE - 1)
7708 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
7712 /* Allocate some memory to hold information read in from the input
7713 files. */
7714 if (max_contents_size != 0)
7716 finfo.contents = bfd_malloc (max_contents_size);
7717 if (finfo.contents == NULL)
7718 goto error_return;
7721 if (max_external_reloc_size != 0)
7723 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
7724 if (finfo.external_relocs == NULL)
7725 goto error_return;
7728 if (max_internal_reloc_count != 0)
7730 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
7731 amt *= sizeof (Elf_Internal_Rela);
7732 finfo.internal_relocs = bfd_malloc (amt);
7733 if (finfo.internal_relocs == NULL)
7734 goto error_return;
7737 if (max_sym_count != 0)
7739 amt = max_sym_count * bed->s->sizeof_sym;
7740 finfo.external_syms = bfd_malloc (amt);
7741 if (finfo.external_syms == NULL)
7742 goto error_return;
7744 amt = max_sym_count * sizeof (Elf_Internal_Sym);
7745 finfo.internal_syms = bfd_malloc (amt);
7746 if (finfo.internal_syms == NULL)
7747 goto error_return;
7749 amt = max_sym_count * sizeof (long);
7750 finfo.indices = bfd_malloc (amt);
7751 if (finfo.indices == NULL)
7752 goto error_return;
7754 amt = max_sym_count * sizeof (asection *);
7755 finfo.sections = bfd_malloc (amt);
7756 if (finfo.sections == NULL)
7757 goto error_return;
7760 if (max_sym_shndx_count != 0)
7762 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
7763 finfo.locsym_shndx = bfd_malloc (amt);
7764 if (finfo.locsym_shndx == NULL)
7765 goto error_return;
7768 if (elf_hash_table (info)->tls_sec)
7770 bfd_vma base, end = 0;
7771 asection *sec;
7773 for (sec = elf_hash_table (info)->tls_sec;
7774 sec && (sec->flags & SEC_THREAD_LOCAL);
7775 sec = sec->next)
7777 bfd_vma size = sec->size;
7779 if (size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0)
7781 struct bfd_link_order *o;
7783 for (o = sec->link_order_head; o != NULL; o = o->next)
7784 if (size < o->offset + o->size)
7785 size = o->offset + o->size;
7787 end = sec->vma + size;
7789 base = elf_hash_table (info)->tls_sec->vma;
7790 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
7791 elf_hash_table (info)->tls_size = end - base;
7794 /* Reorder SHF_LINK_ORDER sections. */
7795 for (o = abfd->sections; o != NULL; o = o->next)
7797 if (!elf_fixup_link_order (abfd, o))
7798 return FALSE;
7801 /* Since ELF permits relocations to be against local symbols, we
7802 must have the local symbols available when we do the relocations.
7803 Since we would rather only read the local symbols once, and we
7804 would rather not keep them in memory, we handle all the
7805 relocations for a single input file at the same time.
7807 Unfortunately, there is no way to know the total number of local
7808 symbols until we have seen all of them, and the local symbol
7809 indices precede the global symbol indices. This means that when
7810 we are generating relocatable output, and we see a reloc against
7811 a global symbol, we can not know the symbol index until we have
7812 finished examining all the local symbols to see which ones we are
7813 going to output. To deal with this, we keep the relocations in
7814 memory, and don't output them until the end of the link. This is
7815 an unfortunate waste of memory, but I don't see a good way around
7816 it. Fortunately, it only happens when performing a relocatable
7817 link, which is not the common case. FIXME: If keep_memory is set
7818 we could write the relocs out and then read them again; I don't
7819 know how bad the memory loss will be. */
7821 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
7822 sub->output_has_begun = FALSE;
7823 for (o = abfd->sections; o != NULL; o = o->next)
7825 for (p = o->link_order_head; p != NULL; p = p->next)
7827 if (p->type == bfd_indirect_link_order
7828 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
7829 == bfd_target_elf_flavour)
7830 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
7832 if (! sub->output_has_begun)
7834 if (! elf_link_input_bfd (&finfo, sub))
7835 goto error_return;
7836 sub->output_has_begun = TRUE;
7839 else if (p->type == bfd_section_reloc_link_order
7840 || p->type == bfd_symbol_reloc_link_order)
7842 if (! elf_reloc_link_order (abfd, info, o, p))
7843 goto error_return;
7845 else
7847 if (! _bfd_default_link_order (abfd, info, o, p))
7848 goto error_return;
7853 /* Output any global symbols that got converted to local in a
7854 version script or due to symbol visibility. We do this in a
7855 separate step since ELF requires all local symbols to appear
7856 prior to any global symbols. FIXME: We should only do this if
7857 some global symbols were, in fact, converted to become local.
7858 FIXME: Will this work correctly with the Irix 5 linker? */
7859 eoinfo.failed = FALSE;
7860 eoinfo.finfo = &finfo;
7861 eoinfo.localsyms = TRUE;
7862 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
7863 &eoinfo);
7864 if (eoinfo.failed)
7865 return FALSE;
7867 /* That wrote out all the local symbols. Finish up the symbol table
7868 with the global symbols. Even if we want to strip everything we
7869 can, we still need to deal with those global symbols that got
7870 converted to local in a version script. */
7872 /* The sh_info field records the index of the first non local symbol. */
7873 symtab_hdr->sh_info = bfd_get_symcount (abfd);
7875 if (dynamic
7876 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
7878 Elf_Internal_Sym sym;
7879 bfd_byte *dynsym = finfo.dynsym_sec->contents;
7880 long last_local = 0;
7882 /* Write out the section symbols for the output sections. */
7883 if (info->shared)
7885 asection *s;
7887 sym.st_size = 0;
7888 sym.st_name = 0;
7889 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
7890 sym.st_other = 0;
7892 for (s = abfd->sections; s != NULL; s = s->next)
7894 int indx;
7895 bfd_byte *dest;
7896 long dynindx;
7898 dynindx = elf_section_data (s)->dynindx;
7899 if (dynindx <= 0)
7900 continue;
7901 indx = elf_section_data (s)->this_idx;
7902 BFD_ASSERT (indx > 0);
7903 sym.st_shndx = indx;
7904 sym.st_value = s->vma;
7905 dest = dynsym + dynindx * bed->s->sizeof_sym;
7906 if (last_local < dynindx)
7907 last_local = dynindx;
7908 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
7912 /* Write out the local dynsyms. */
7913 if (elf_hash_table (info)->dynlocal)
7915 struct elf_link_local_dynamic_entry *e;
7916 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
7918 asection *s;
7919 bfd_byte *dest;
7921 sym.st_size = e->isym.st_size;
7922 sym.st_other = e->isym.st_other;
7924 /* Copy the internal symbol as is.
7925 Note that we saved a word of storage and overwrote
7926 the original st_name with the dynstr_index. */
7927 sym = e->isym;
7929 if (e->isym.st_shndx != SHN_UNDEF
7930 && (e->isym.st_shndx < SHN_LORESERVE
7931 || e->isym.st_shndx > SHN_HIRESERVE))
7933 s = bfd_section_from_elf_index (e->input_bfd,
7934 e->isym.st_shndx);
7936 sym.st_shndx =
7937 elf_section_data (s->output_section)->this_idx;
7938 sym.st_value = (s->output_section->vma
7939 + s->output_offset
7940 + e->isym.st_value);
7943 if (last_local < e->dynindx)
7944 last_local = e->dynindx;
7946 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
7947 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
7951 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
7952 last_local + 1;
7955 /* We get the global symbols from the hash table. */
7956 eoinfo.failed = FALSE;
7957 eoinfo.localsyms = FALSE;
7958 eoinfo.finfo = &finfo;
7959 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
7960 &eoinfo);
7961 if (eoinfo.failed)
7962 return FALSE;
7964 /* If backend needs to output some symbols not present in the hash
7965 table, do it now. */
7966 if (bed->elf_backend_output_arch_syms)
7968 typedef bfd_boolean (*out_sym_func)
7969 (void *, const char *, Elf_Internal_Sym *, asection *,
7970 struct elf_link_hash_entry *);
7972 if (! ((*bed->elf_backend_output_arch_syms)
7973 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
7974 return FALSE;
7977 /* Flush all symbols to the file. */
7978 if (! elf_link_flush_output_syms (&finfo, bed))
7979 return FALSE;
7981 /* Now we know the size of the symtab section. */
7982 off += symtab_hdr->sh_size;
7984 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
7985 if (symtab_shndx_hdr->sh_name != 0)
7987 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
7988 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
7989 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
7990 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
7991 symtab_shndx_hdr->sh_size = amt;
7993 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
7994 off, TRUE);
7996 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
7997 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
7998 return FALSE;
8002 /* Finish up and write out the symbol string table (.strtab)
8003 section. */
8004 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8005 /* sh_name was set in prep_headers. */
8006 symstrtab_hdr->sh_type = SHT_STRTAB;
8007 symstrtab_hdr->sh_flags = 0;
8008 symstrtab_hdr->sh_addr = 0;
8009 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
8010 symstrtab_hdr->sh_entsize = 0;
8011 symstrtab_hdr->sh_link = 0;
8012 symstrtab_hdr->sh_info = 0;
8013 /* sh_offset is set just below. */
8014 symstrtab_hdr->sh_addralign = 1;
8016 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
8017 elf_tdata (abfd)->next_file_pos = off;
8019 if (bfd_get_symcount (abfd) > 0)
8021 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
8022 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
8023 return FALSE;
8026 /* Adjust the relocs to have the correct symbol indices. */
8027 for (o = abfd->sections; o != NULL; o = o->next)
8029 if ((o->flags & SEC_RELOC) == 0)
8030 continue;
8032 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
8033 elf_section_data (o)->rel_count,
8034 elf_section_data (o)->rel_hashes);
8035 if (elf_section_data (o)->rel_hdr2 != NULL)
8036 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
8037 elf_section_data (o)->rel_count2,
8038 (elf_section_data (o)->rel_hashes
8039 + elf_section_data (o)->rel_count));
8041 /* Set the reloc_count field to 0 to prevent write_relocs from
8042 trying to swap the relocs out itself. */
8043 o->reloc_count = 0;
8046 if (dynamic && info->combreloc && dynobj != NULL)
8047 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
8049 /* If we are linking against a dynamic object, or generating a
8050 shared library, finish up the dynamic linking information. */
8051 if (dynamic)
8053 bfd_byte *dyncon, *dynconend;
8055 /* Fix up .dynamic entries. */
8056 o = bfd_get_section_by_name (dynobj, ".dynamic");
8057 BFD_ASSERT (o != NULL);
8059 dyncon = o->contents;
8060 dynconend = o->contents + o->size;
8061 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
8063 Elf_Internal_Dyn dyn;
8064 const char *name;
8065 unsigned int type;
8067 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
8069 switch (dyn.d_tag)
8071 default:
8072 continue;
8073 case DT_NULL:
8074 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
8076 switch (elf_section_data (reldyn)->this_hdr.sh_type)
8078 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
8079 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
8080 default: continue;
8082 dyn.d_un.d_val = relativecount;
8083 relativecount = 0;
8084 break;
8086 continue;
8088 case DT_INIT:
8089 name = info->init_function;
8090 goto get_sym;
8091 case DT_FINI:
8092 name = info->fini_function;
8093 get_sym:
8095 struct elf_link_hash_entry *h;
8097 h = elf_link_hash_lookup (elf_hash_table (info), name,
8098 FALSE, FALSE, TRUE);
8099 if (h != NULL
8100 && (h->root.type == bfd_link_hash_defined
8101 || h->root.type == bfd_link_hash_defweak))
8103 dyn.d_un.d_val = h->root.u.def.value;
8104 o = h->root.u.def.section;
8105 if (o->output_section != NULL)
8106 dyn.d_un.d_val += (o->output_section->vma
8107 + o->output_offset);
8108 else
8110 /* The symbol is imported from another shared
8111 library and does not apply to this one. */
8112 dyn.d_un.d_val = 0;
8114 break;
8117 continue;
8119 case DT_PREINIT_ARRAYSZ:
8120 name = ".preinit_array";
8121 goto get_size;
8122 case DT_INIT_ARRAYSZ:
8123 name = ".init_array";
8124 goto get_size;
8125 case DT_FINI_ARRAYSZ:
8126 name = ".fini_array";
8127 get_size:
8128 o = bfd_get_section_by_name (abfd, name);
8129 if (o == NULL)
8131 (*_bfd_error_handler)
8132 (_("%B: could not find output section %s"), abfd, name);
8133 goto error_return;
8135 if (o->size == 0)
8136 (*_bfd_error_handler)
8137 (_("warning: %s section has zero size"), name);
8138 dyn.d_un.d_val = o->size;
8139 break;
8141 case DT_PREINIT_ARRAY:
8142 name = ".preinit_array";
8143 goto get_vma;
8144 case DT_INIT_ARRAY:
8145 name = ".init_array";
8146 goto get_vma;
8147 case DT_FINI_ARRAY:
8148 name = ".fini_array";
8149 goto get_vma;
8151 case DT_HASH:
8152 name = ".hash";
8153 goto get_vma;
8154 case DT_STRTAB:
8155 name = ".dynstr";
8156 goto get_vma;
8157 case DT_SYMTAB:
8158 name = ".dynsym";
8159 goto get_vma;
8160 case DT_VERDEF:
8161 name = ".gnu.version_d";
8162 goto get_vma;
8163 case DT_VERNEED:
8164 name = ".gnu.version_r";
8165 goto get_vma;
8166 case DT_VERSYM:
8167 name = ".gnu.version";
8168 get_vma:
8169 o = bfd_get_section_by_name (abfd, name);
8170 if (o == NULL)
8172 (*_bfd_error_handler)
8173 (_("%B: could not find output section %s"), abfd, name);
8174 goto error_return;
8176 dyn.d_un.d_ptr = o->vma;
8177 break;
8179 case DT_REL:
8180 case DT_RELA:
8181 case DT_RELSZ:
8182 case DT_RELASZ:
8183 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
8184 type = SHT_REL;
8185 else
8186 type = SHT_RELA;
8187 dyn.d_un.d_val = 0;
8188 for (i = 1; i < elf_numsections (abfd); i++)
8190 Elf_Internal_Shdr *hdr;
8192 hdr = elf_elfsections (abfd)[i];
8193 if (hdr->sh_type == type
8194 && (hdr->sh_flags & SHF_ALLOC) != 0)
8196 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
8197 dyn.d_un.d_val += hdr->sh_size;
8198 else
8200 if (dyn.d_un.d_val == 0
8201 || hdr->sh_addr < dyn.d_un.d_val)
8202 dyn.d_un.d_val = hdr->sh_addr;
8206 break;
8208 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
8212 /* If we have created any dynamic sections, then output them. */
8213 if (dynobj != NULL)
8215 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
8216 goto error_return;
8218 for (o = dynobj->sections; o != NULL; o = o->next)
8220 if ((o->flags & SEC_HAS_CONTENTS) == 0
8221 || o->size == 0
8222 || o->output_section == bfd_abs_section_ptr)
8223 continue;
8224 if ((o->flags & SEC_LINKER_CREATED) == 0)
8226 /* At this point, we are only interested in sections
8227 created by _bfd_elf_link_create_dynamic_sections. */
8228 continue;
8230 if (elf_hash_table (info)->stab_info.stabstr == o)
8231 continue;
8232 if (elf_hash_table (info)->eh_info.hdr_sec == o)
8233 continue;
8234 if ((elf_section_data (o->output_section)->this_hdr.sh_type
8235 != SHT_STRTAB)
8236 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
8238 if (! bfd_set_section_contents (abfd, o->output_section,
8239 o->contents,
8240 (file_ptr) o->output_offset,
8241 o->size))
8242 goto error_return;
8244 else
8246 /* The contents of the .dynstr section are actually in a
8247 stringtab. */
8248 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
8249 if (bfd_seek (abfd, off, SEEK_SET) != 0
8250 || ! _bfd_elf_strtab_emit (abfd,
8251 elf_hash_table (info)->dynstr))
8252 goto error_return;
8257 if (info->relocatable)
8259 bfd_boolean failed = FALSE;
8261 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
8262 if (failed)
8263 goto error_return;
8266 /* If we have optimized stabs strings, output them. */
8267 if (elf_hash_table (info)->stab_info.stabstr != NULL)
8269 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
8270 goto error_return;
8273 if (info->eh_frame_hdr)
8275 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
8276 goto error_return;
8279 if (finfo.symstrtab != NULL)
8280 _bfd_stringtab_free (finfo.symstrtab);
8281 if (finfo.contents != NULL)
8282 free (finfo.contents);
8283 if (finfo.external_relocs != NULL)
8284 free (finfo.external_relocs);
8285 if (finfo.internal_relocs != NULL)
8286 free (finfo.internal_relocs);
8287 if (finfo.external_syms != NULL)
8288 free (finfo.external_syms);
8289 if (finfo.locsym_shndx != NULL)
8290 free (finfo.locsym_shndx);
8291 if (finfo.internal_syms != NULL)
8292 free (finfo.internal_syms);
8293 if (finfo.indices != NULL)
8294 free (finfo.indices);
8295 if (finfo.sections != NULL)
8296 free (finfo.sections);
8297 if (finfo.symbuf != NULL)
8298 free (finfo.symbuf);
8299 if (finfo.symshndxbuf != NULL)
8300 free (finfo.symshndxbuf);
8301 for (o = abfd->sections; o != NULL; o = o->next)
8303 if ((o->flags & SEC_RELOC) != 0
8304 && elf_section_data (o)->rel_hashes != NULL)
8305 free (elf_section_data (o)->rel_hashes);
8308 elf_tdata (abfd)->linker = TRUE;
8310 return TRUE;
8312 error_return:
8313 if (finfo.symstrtab != NULL)
8314 _bfd_stringtab_free (finfo.symstrtab);
8315 if (finfo.contents != NULL)
8316 free (finfo.contents);
8317 if (finfo.external_relocs != NULL)
8318 free (finfo.external_relocs);
8319 if (finfo.internal_relocs != NULL)
8320 free (finfo.internal_relocs);
8321 if (finfo.external_syms != NULL)
8322 free (finfo.external_syms);
8323 if (finfo.locsym_shndx != NULL)
8324 free (finfo.locsym_shndx);
8325 if (finfo.internal_syms != NULL)
8326 free (finfo.internal_syms);
8327 if (finfo.indices != NULL)
8328 free (finfo.indices);
8329 if (finfo.sections != NULL)
8330 free (finfo.sections);
8331 if (finfo.symbuf != NULL)
8332 free (finfo.symbuf);
8333 if (finfo.symshndxbuf != NULL)
8334 free (finfo.symshndxbuf);
8335 for (o = abfd->sections; o != NULL; o = o->next)
8337 if ((o->flags & SEC_RELOC) != 0
8338 && elf_section_data (o)->rel_hashes != NULL)
8339 free (elf_section_data (o)->rel_hashes);
8342 return FALSE;
8345 /* Garbage collect unused sections. */
8347 /* The mark phase of garbage collection. For a given section, mark
8348 it and any sections in this section's group, and all the sections
8349 which define symbols to which it refers. */
8351 typedef asection * (*gc_mark_hook_fn)
8352 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
8353 struct elf_link_hash_entry *, Elf_Internal_Sym *);
8355 bfd_boolean
8356 _bfd_elf_gc_mark (struct bfd_link_info *info,
8357 asection *sec,
8358 gc_mark_hook_fn gc_mark_hook)
8360 bfd_boolean ret;
8361 asection *group_sec;
8363 sec->gc_mark = 1;
8365 /* Mark all the sections in the group. */
8366 group_sec = elf_section_data (sec)->next_in_group;
8367 if (group_sec && !group_sec->gc_mark)
8368 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
8369 return FALSE;
8371 /* Look through the section relocs. */
8372 ret = TRUE;
8373 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
8375 Elf_Internal_Rela *relstart, *rel, *relend;
8376 Elf_Internal_Shdr *symtab_hdr;
8377 struct elf_link_hash_entry **sym_hashes;
8378 size_t nlocsyms;
8379 size_t extsymoff;
8380 bfd *input_bfd = sec->owner;
8381 const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
8382 Elf_Internal_Sym *isym = NULL;
8383 int r_sym_shift;
8385 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8386 sym_hashes = elf_sym_hashes (input_bfd);
8388 /* Read the local symbols. */
8389 if (elf_bad_symtab (input_bfd))
8391 nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym;
8392 extsymoff = 0;
8394 else
8395 extsymoff = nlocsyms = symtab_hdr->sh_info;
8397 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
8398 if (isym == NULL && nlocsyms != 0)
8400 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
8401 NULL, NULL, NULL);
8402 if (isym == NULL)
8403 return FALSE;
8406 /* Read the relocations. */
8407 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
8408 info->keep_memory);
8409 if (relstart == NULL)
8411 ret = FALSE;
8412 goto out1;
8414 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
8416 if (bed->s->arch_size == 32)
8417 r_sym_shift = 8;
8418 else
8419 r_sym_shift = 32;
8421 for (rel = relstart; rel < relend; rel++)
8423 unsigned long r_symndx;
8424 asection *rsec;
8425 struct elf_link_hash_entry *h;
8427 r_symndx = rel->r_info >> r_sym_shift;
8428 if (r_symndx == 0)
8429 continue;
8431 if (r_symndx >= nlocsyms
8432 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
8434 h = sym_hashes[r_symndx - extsymoff];
8435 while (h->root.type == bfd_link_hash_indirect
8436 || h->root.type == bfd_link_hash_warning)
8437 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8438 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
8440 else
8442 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
8445 if (rsec && !rsec->gc_mark)
8447 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
8448 rsec->gc_mark = 1;
8449 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
8451 ret = FALSE;
8452 goto out2;
8457 out2:
8458 if (elf_section_data (sec)->relocs != relstart)
8459 free (relstart);
8460 out1:
8461 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
8463 if (! info->keep_memory)
8464 free (isym);
8465 else
8466 symtab_hdr->contents = (unsigned char *) isym;
8470 return ret;
8473 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8475 static bfd_boolean
8476 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *idxptr)
8478 int *idx = idxptr;
8480 if (h->root.type == bfd_link_hash_warning)
8481 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8483 if (h->dynindx != -1
8484 && ((h->root.type != bfd_link_hash_defined
8485 && h->root.type != bfd_link_hash_defweak)
8486 || h->root.u.def.section->gc_mark))
8487 h->dynindx = (*idx)++;
8489 return TRUE;
8492 /* The sweep phase of garbage collection. Remove all garbage sections. */
8494 typedef bfd_boolean (*gc_sweep_hook_fn)
8495 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
8497 static bfd_boolean
8498 elf_gc_sweep (struct bfd_link_info *info, gc_sweep_hook_fn gc_sweep_hook)
8500 bfd *sub;
8502 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8504 asection *o;
8506 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
8507 continue;
8509 for (o = sub->sections; o != NULL; o = o->next)
8511 /* Keep debug and special sections. */
8512 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
8513 || (o->flags & (SEC_ALLOC | SEC_LOAD)) == 0)
8514 o->gc_mark = 1;
8516 if (o->gc_mark)
8517 continue;
8519 /* Skip sweeping sections already excluded. */
8520 if (o->flags & SEC_EXCLUDE)
8521 continue;
8523 /* Since this is early in the link process, it is simple
8524 to remove a section from the output. */
8525 o->flags |= SEC_EXCLUDE;
8527 /* But we also have to update some of the relocation
8528 info we collected before. */
8529 if (gc_sweep_hook
8530 && (o->flags & SEC_RELOC) && o->reloc_count > 0)
8532 Elf_Internal_Rela *internal_relocs;
8533 bfd_boolean r;
8535 internal_relocs
8536 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
8537 info->keep_memory);
8538 if (internal_relocs == NULL)
8539 return FALSE;
8541 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
8543 if (elf_section_data (o)->relocs != internal_relocs)
8544 free (internal_relocs);
8546 if (!r)
8547 return FALSE;
8552 /* Remove the symbols that were in the swept sections from the dynamic
8553 symbol table. GCFIXME: Anyone know how to get them out of the
8554 static symbol table as well? */
8556 int i = 0;
8558 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, &i);
8560 elf_hash_table (info)->dynsymcount = i;
8563 return TRUE;
8566 /* Propagate collected vtable information. This is called through
8567 elf_link_hash_traverse. */
8569 static bfd_boolean
8570 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
8572 if (h->root.type == bfd_link_hash_warning)
8573 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8575 /* Those that are not vtables. */
8576 if (h->vtable_parent == NULL)
8577 return TRUE;
8579 /* Those vtables that do not have parents, we cannot merge. */
8580 if (h->vtable_parent == (struct elf_link_hash_entry *) -1)
8581 return TRUE;
8583 /* If we've already been done, exit. */
8584 if (h->vtable_entries_used && h->vtable_entries_used[-1])
8585 return TRUE;
8587 /* Make sure the parent's table is up to date. */
8588 elf_gc_propagate_vtable_entries_used (h->vtable_parent, okp);
8590 if (h->vtable_entries_used == NULL)
8592 /* None of this table's entries were referenced. Re-use the
8593 parent's table. */
8594 h->vtable_entries_used = h->vtable_parent->vtable_entries_used;
8595 h->vtable_entries_size = h->vtable_parent->vtable_entries_size;
8597 else
8599 size_t n;
8600 bfd_boolean *cu, *pu;
8602 /* Or the parent's entries into ours. */
8603 cu = h->vtable_entries_used;
8604 cu[-1] = TRUE;
8605 pu = h->vtable_parent->vtable_entries_used;
8606 if (pu != NULL)
8608 const struct elf_backend_data *bed;
8609 unsigned int log_file_align;
8611 bed = get_elf_backend_data (h->root.u.def.section->owner);
8612 log_file_align = bed->s->log_file_align;
8613 n = h->vtable_parent->vtable_entries_size >> log_file_align;
8614 while (n--)
8616 if (*pu)
8617 *cu = TRUE;
8618 pu++;
8619 cu++;
8624 return TRUE;
8627 static bfd_boolean
8628 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
8630 asection *sec;
8631 bfd_vma hstart, hend;
8632 Elf_Internal_Rela *relstart, *relend, *rel;
8633 const struct elf_backend_data *bed;
8634 unsigned int log_file_align;
8636 if (h->root.type == bfd_link_hash_warning)
8637 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8639 /* Take care of both those symbols that do not describe vtables as
8640 well as those that are not loaded. */
8641 if (h->vtable_parent == NULL)
8642 return TRUE;
8644 BFD_ASSERT (h->root.type == bfd_link_hash_defined
8645 || h->root.type == bfd_link_hash_defweak);
8647 sec = h->root.u.def.section;
8648 hstart = h->root.u.def.value;
8649 hend = hstart + h->size;
8651 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
8652 if (!relstart)
8653 return *(bfd_boolean *) okp = FALSE;
8654 bed = get_elf_backend_data (sec->owner);
8655 log_file_align = bed->s->log_file_align;
8657 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
8659 for (rel = relstart; rel < relend; ++rel)
8660 if (rel->r_offset >= hstart && rel->r_offset < hend)
8662 /* If the entry is in use, do nothing. */
8663 if (h->vtable_entries_used
8664 && (rel->r_offset - hstart) < h->vtable_entries_size)
8666 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
8667 if (h->vtable_entries_used[entry])
8668 continue;
8670 /* Otherwise, kill it. */
8671 rel->r_offset = rel->r_info = rel->r_addend = 0;
8674 return TRUE;
8677 /* Mark sections containing dynamically referenced symbols. This is called
8678 through elf_link_hash_traverse. */
8680 static bfd_boolean
8681 elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h,
8682 void *okp ATTRIBUTE_UNUSED)
8684 if (h->root.type == bfd_link_hash_warning)
8685 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8687 if ((h->root.type == bfd_link_hash_defined
8688 || h->root.type == bfd_link_hash_defweak)
8689 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC))
8690 h->root.u.def.section->flags |= SEC_KEEP;
8692 return TRUE;
8695 /* Do mark and sweep of unused sections. */
8697 bfd_boolean
8698 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
8700 bfd_boolean ok = TRUE;
8701 bfd *sub;
8702 asection * (*gc_mark_hook)
8703 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
8704 struct elf_link_hash_entry *h, Elf_Internal_Sym *);
8706 if (!get_elf_backend_data (abfd)->can_gc_sections
8707 || info->relocatable
8708 || info->emitrelocations
8709 || info->shared
8710 || !is_elf_hash_table (info->hash))
8712 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
8713 return TRUE;
8716 /* Apply transitive closure to the vtable entry usage info. */
8717 elf_link_hash_traverse (elf_hash_table (info),
8718 elf_gc_propagate_vtable_entries_used,
8719 &ok);
8720 if (!ok)
8721 return FALSE;
8723 /* Kill the vtable relocations that were not used. */
8724 elf_link_hash_traverse (elf_hash_table (info),
8725 elf_gc_smash_unused_vtentry_relocs,
8726 &ok);
8727 if (!ok)
8728 return FALSE;
8730 /* Mark dynamically referenced symbols. */
8731 if (elf_hash_table (info)->dynamic_sections_created)
8732 elf_link_hash_traverse (elf_hash_table (info),
8733 elf_gc_mark_dynamic_ref_symbol,
8734 &ok);
8735 if (!ok)
8736 return FALSE;
8738 /* Grovel through relocs to find out who stays ... */
8739 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
8740 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8742 asection *o;
8744 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
8745 continue;
8747 for (o = sub->sections; o != NULL; o = o->next)
8749 if (o->flags & SEC_KEEP)
8751 /* _bfd_elf_discard_section_eh_frame knows how to discard
8752 orphaned FDEs so don't mark sections referenced by the
8753 EH frame section. */
8754 if (strcmp (o->name, ".eh_frame") == 0)
8755 o->gc_mark = 1;
8756 else if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
8757 return FALSE;
8762 /* ... and mark SEC_EXCLUDE for those that go. */
8763 if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook))
8764 return FALSE;
8766 return TRUE;
8769 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
8771 bfd_boolean
8772 bfd_elf_gc_record_vtinherit (bfd *abfd,
8773 asection *sec,
8774 struct elf_link_hash_entry *h,
8775 bfd_vma offset)
8777 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
8778 struct elf_link_hash_entry **search, *child;
8779 bfd_size_type extsymcount;
8780 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8782 /* The sh_info field of the symtab header tells us where the
8783 external symbols start. We don't care about the local symbols at
8784 this point. */
8785 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
8786 if (!elf_bad_symtab (abfd))
8787 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
8789 sym_hashes = elf_sym_hashes (abfd);
8790 sym_hashes_end = sym_hashes + extsymcount;
8792 /* Hunt down the child symbol, which is in this section at the same
8793 offset as the relocation. */
8794 for (search = sym_hashes; search != sym_hashes_end; ++search)
8796 if ((child = *search) != NULL
8797 && (child->root.type == bfd_link_hash_defined
8798 || child->root.type == bfd_link_hash_defweak)
8799 && child->root.u.def.section == sec
8800 && child->root.u.def.value == offset)
8801 goto win;
8804 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
8805 abfd, sec, (unsigned long) offset);
8806 bfd_set_error (bfd_error_invalid_operation);
8807 return FALSE;
8809 win:
8810 if (!h)
8812 /* This *should* only be the absolute section. It could potentially
8813 be that someone has defined a non-global vtable though, which
8814 would be bad. It isn't worth paging in the local symbols to be
8815 sure though; that case should simply be handled by the assembler. */
8817 child->vtable_parent = (struct elf_link_hash_entry *) -1;
8819 else
8820 child->vtable_parent = h;
8822 return TRUE;
8825 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
8827 bfd_boolean
8828 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
8829 asection *sec ATTRIBUTE_UNUSED,
8830 struct elf_link_hash_entry *h,
8831 bfd_vma addend)
8833 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8834 unsigned int log_file_align = bed->s->log_file_align;
8836 if (addend >= h->vtable_entries_size)
8838 size_t size, bytes, file_align;
8839 bfd_boolean *ptr = h->vtable_entries_used;
8841 /* While the symbol is undefined, we have to be prepared to handle
8842 a zero size. */
8843 file_align = 1 << log_file_align;
8844 if (h->root.type == bfd_link_hash_undefined)
8845 size = addend + file_align;
8846 else
8848 size = h->size;
8849 if (addend >= size)
8851 /* Oops! We've got a reference past the defined end of
8852 the table. This is probably a bug -- shall we warn? */
8853 size = addend + file_align;
8856 size = (size + file_align - 1) & -file_align;
8858 /* Allocate one extra entry for use as a "done" flag for the
8859 consolidation pass. */
8860 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
8862 if (ptr)
8864 ptr = bfd_realloc (ptr - 1, bytes);
8866 if (ptr != NULL)
8868 size_t oldbytes;
8870 oldbytes = (((h->vtable_entries_size >> log_file_align) + 1)
8871 * sizeof (bfd_boolean));
8872 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
8875 else
8876 ptr = bfd_zmalloc (bytes);
8878 if (ptr == NULL)
8879 return FALSE;
8881 /* And arrange for that done flag to be at index -1. */
8882 h->vtable_entries_used = ptr + 1;
8883 h->vtable_entries_size = size;
8886 h->vtable_entries_used[addend >> log_file_align] = TRUE;
8888 return TRUE;
8891 struct alloc_got_off_arg {
8892 bfd_vma gotoff;
8893 unsigned int got_elt_size;
8896 /* We need a special top-level link routine to convert got reference counts
8897 to real got offsets. */
8899 static bfd_boolean
8900 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
8902 struct alloc_got_off_arg *gofarg = arg;
8904 if (h->root.type == bfd_link_hash_warning)
8905 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8907 if (h->got.refcount > 0)
8909 h->got.offset = gofarg->gotoff;
8910 gofarg->gotoff += gofarg->got_elt_size;
8912 else
8913 h->got.offset = (bfd_vma) -1;
8915 return TRUE;
8918 /* And an accompanying bit to work out final got entry offsets once
8919 we're done. Should be called from final_link. */
8921 bfd_boolean
8922 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
8923 struct bfd_link_info *info)
8925 bfd *i;
8926 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8927 bfd_vma gotoff;
8928 unsigned int got_elt_size = bed->s->arch_size / 8;
8929 struct alloc_got_off_arg gofarg;
8931 if (! is_elf_hash_table (info->hash))
8932 return FALSE;
8934 /* The GOT offset is relative to the .got section, but the GOT header is
8935 put into the .got.plt section, if the backend uses it. */
8936 if (bed->want_got_plt)
8937 gotoff = 0;
8938 else
8939 gotoff = bed->got_header_size;
8941 /* Do the local .got entries first. */
8942 for (i = info->input_bfds; i; i = i->link_next)
8944 bfd_signed_vma *local_got;
8945 bfd_size_type j, locsymcount;
8946 Elf_Internal_Shdr *symtab_hdr;
8948 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
8949 continue;
8951 local_got = elf_local_got_refcounts (i);
8952 if (!local_got)
8953 continue;
8955 symtab_hdr = &elf_tdata (i)->symtab_hdr;
8956 if (elf_bad_symtab (i))
8957 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
8958 else
8959 locsymcount = symtab_hdr->sh_info;
8961 for (j = 0; j < locsymcount; ++j)
8963 if (local_got[j] > 0)
8965 local_got[j] = gotoff;
8966 gotoff += got_elt_size;
8968 else
8969 local_got[j] = (bfd_vma) -1;
8973 /* Then the global .got entries. .plt refcounts are handled by
8974 adjust_dynamic_symbol */
8975 gofarg.gotoff = gotoff;
8976 gofarg.got_elt_size = got_elt_size;
8977 elf_link_hash_traverse (elf_hash_table (info),
8978 elf_gc_allocate_got_offsets,
8979 &gofarg);
8980 return TRUE;
8983 /* Many folk need no more in the way of final link than this, once
8984 got entry reference counting is enabled. */
8986 bfd_boolean
8987 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
8989 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
8990 return FALSE;
8992 /* Invoke the regular ELF backend linker to do all the work. */
8993 return bfd_elf_final_link (abfd, info);
8996 bfd_boolean
8997 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
8999 struct elf_reloc_cookie *rcookie = cookie;
9001 if (rcookie->bad_symtab)
9002 rcookie->rel = rcookie->rels;
9004 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
9006 unsigned long r_symndx;
9008 if (! rcookie->bad_symtab)
9009 if (rcookie->rel->r_offset > offset)
9010 return FALSE;
9011 if (rcookie->rel->r_offset != offset)
9012 continue;
9014 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
9015 if (r_symndx == SHN_UNDEF)
9016 return TRUE;
9018 if (r_symndx >= rcookie->locsymcount
9019 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
9021 struct elf_link_hash_entry *h;
9023 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
9025 while (h->root.type == bfd_link_hash_indirect
9026 || h->root.type == bfd_link_hash_warning)
9027 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9029 if ((h->root.type == bfd_link_hash_defined
9030 || h->root.type == bfd_link_hash_defweak)
9031 && elf_discarded_section (h->root.u.def.section))
9032 return TRUE;
9033 else
9034 return FALSE;
9036 else
9038 /* It's not a relocation against a global symbol,
9039 but it could be a relocation against a local
9040 symbol for a discarded section. */
9041 asection *isec;
9042 Elf_Internal_Sym *isym;
9044 /* Need to: get the symbol; get the section. */
9045 isym = &rcookie->locsyms[r_symndx];
9046 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
9048 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
9049 if (isec != NULL && elf_discarded_section (isec))
9050 return TRUE;
9053 return FALSE;
9055 return FALSE;
9058 /* Discard unneeded references to discarded sections.
9059 Returns TRUE if any section's size was changed. */
9060 /* This function assumes that the relocations are in sorted order,
9061 which is true for all known assemblers. */
9063 bfd_boolean
9064 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
9066 struct elf_reloc_cookie cookie;
9067 asection *stab, *eh;
9068 Elf_Internal_Shdr *symtab_hdr;
9069 const struct elf_backend_data *bed;
9070 bfd *abfd;
9071 unsigned int count;
9072 bfd_boolean ret = FALSE;
9074 if (info->traditional_format
9075 || !is_elf_hash_table (info->hash))
9076 return FALSE;
9078 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
9080 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
9081 continue;
9083 bed = get_elf_backend_data (abfd);
9085 if ((abfd->flags & DYNAMIC) != 0)
9086 continue;
9088 eh = bfd_get_section_by_name (abfd, ".eh_frame");
9089 if (info->relocatable
9090 || (eh != NULL
9091 && (eh->size == 0
9092 || bfd_is_abs_section (eh->output_section))))
9093 eh = NULL;
9095 stab = bfd_get_section_by_name (abfd, ".stab");
9096 if (stab != NULL
9097 && (stab->size == 0
9098 || bfd_is_abs_section (stab->output_section)
9099 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
9100 stab = NULL;
9102 if (stab == NULL
9103 && eh == NULL
9104 && bed->elf_backend_discard_info == NULL)
9105 continue;
9107 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9108 cookie.abfd = abfd;
9109 cookie.sym_hashes = elf_sym_hashes (abfd);
9110 cookie.bad_symtab = elf_bad_symtab (abfd);
9111 if (cookie.bad_symtab)
9113 cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9114 cookie.extsymoff = 0;
9116 else
9118 cookie.locsymcount = symtab_hdr->sh_info;
9119 cookie.extsymoff = symtab_hdr->sh_info;
9122 if (bed->s->arch_size == 32)
9123 cookie.r_sym_shift = 8;
9124 else
9125 cookie.r_sym_shift = 32;
9127 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
9128 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
9130 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9131 cookie.locsymcount, 0,
9132 NULL, NULL, NULL);
9133 if (cookie.locsyms == NULL)
9134 return FALSE;
9137 if (stab != NULL)
9139 cookie.rels = NULL;
9140 count = stab->reloc_count;
9141 if (count != 0)
9142 cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
9143 info->keep_memory);
9144 if (cookie.rels != NULL)
9146 cookie.rel = cookie.rels;
9147 cookie.relend = cookie.rels;
9148 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9149 if (_bfd_discard_section_stabs (abfd, stab,
9150 elf_section_data (stab)->sec_info,
9151 bfd_elf_reloc_symbol_deleted_p,
9152 &cookie))
9153 ret = TRUE;
9154 if (elf_section_data (stab)->relocs != cookie.rels)
9155 free (cookie.rels);
9159 if (eh != NULL)
9161 cookie.rels = NULL;
9162 count = eh->reloc_count;
9163 if (count != 0)
9164 cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
9165 info->keep_memory);
9166 cookie.rel = cookie.rels;
9167 cookie.relend = cookie.rels;
9168 if (cookie.rels != NULL)
9169 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9171 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
9172 bfd_elf_reloc_symbol_deleted_p,
9173 &cookie))
9174 ret = TRUE;
9176 if (cookie.rels != NULL
9177 && elf_section_data (eh)->relocs != cookie.rels)
9178 free (cookie.rels);
9181 if (bed->elf_backend_discard_info != NULL
9182 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
9183 ret = TRUE;
9185 if (cookie.locsyms != NULL
9186 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
9188 if (! info->keep_memory)
9189 free (cookie.locsyms);
9190 else
9191 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
9195 if (info->eh_frame_hdr
9196 && !info->relocatable
9197 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
9198 ret = TRUE;
9200 return ret;
9203 struct already_linked_section
9205 asection *sec;
9206 asection *linked;
9209 /* Check if the member of a single member comdat group matches a
9210 linkonce section and vice versa. */
9211 static bfd_boolean
9212 try_match_symbols_in_sections
9213 (struct bfd_section_already_linked_hash_entry *h, void *info)
9215 struct bfd_section_already_linked *l;
9216 struct already_linked_section *s
9217 = (struct already_linked_section *) info;
9219 if (elf_sec_group (s->sec) == NULL)
9221 /* It is a linkonce section. Try to match it with the member of a
9222 single member comdat group. */
9223 for (l = h->entry; l != NULL; l = l->next)
9224 if ((l->sec->flags & SEC_GROUP))
9226 asection *first = elf_next_in_group (l->sec);
9228 if (first != NULL
9229 && elf_next_in_group (first) == first
9230 && bfd_elf_match_symbols_in_sections (first, s->sec))
9232 s->linked = first;
9233 return FALSE;
9237 else
9239 /* It is the member of a single member comdat group. Try to match
9240 it with a linkonce section. */
9241 for (l = h->entry; l != NULL; l = l->next)
9242 if ((l->sec->flags & SEC_GROUP) == 0
9243 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
9244 && bfd_elf_match_symbols_in_sections (l->sec, s->sec))
9246 s->linked = l->sec;
9247 return FALSE;
9251 return TRUE;
9254 static bfd_boolean
9255 already_linked (asection *sec, asection *group)
9257 struct already_linked_section result;
9259 result.sec = sec;
9260 result.linked = NULL;
9262 bfd_section_already_linked_table_traverse
9263 (try_match_symbols_in_sections, &result);
9265 if (result.linked)
9267 sec->output_section = bfd_abs_section_ptr;
9268 sec->kept_section = result.linked;
9270 /* Also discard the group section. */
9271 if (group)
9272 group->output_section = bfd_abs_section_ptr;
9274 return TRUE;
9277 return FALSE;
9280 void
9281 _bfd_elf_section_already_linked (bfd *abfd, struct bfd_section * sec)
9283 flagword flags;
9284 const char *name;
9285 struct bfd_section_already_linked *l;
9286 struct bfd_section_already_linked_hash_entry *already_linked_list;
9287 asection *group;
9289 /* A single member comdat group section may be discarded by a
9290 linkonce section. See below. */
9291 if (sec->output_section == bfd_abs_section_ptr)
9292 return;
9294 flags = sec->flags;
9296 /* Check if it belongs to a section group. */
9297 group = elf_sec_group (sec);
9299 /* Return if it isn't a linkonce section nor a member of a group. A
9300 comdat group section also has SEC_LINK_ONCE set. */
9301 if ((flags & SEC_LINK_ONCE) == 0 && group == NULL)
9302 return;
9304 if (group)
9306 /* If this is the member of a single member comdat group, check if
9307 the group should be discarded. */
9308 if (elf_next_in_group (sec) == sec
9309 && (group->flags & SEC_LINK_ONCE) != 0)
9310 sec = group;
9311 else
9312 return;
9315 /* FIXME: When doing a relocatable link, we may have trouble
9316 copying relocations in other sections that refer to local symbols
9317 in the section being discarded. Those relocations will have to
9318 be converted somehow; as of this writing I'm not sure that any of
9319 the backends handle that correctly.
9321 It is tempting to instead not discard link once sections when
9322 doing a relocatable link (technically, they should be discarded
9323 whenever we are building constructors). However, that fails,
9324 because the linker winds up combining all the link once sections
9325 into a single large link once section, which defeats the purpose
9326 of having link once sections in the first place.
9328 Also, not merging link once sections in a relocatable link
9329 causes trouble for MIPS ELF, which relies on link once semantics
9330 to handle the .reginfo section correctly. */
9332 name = bfd_get_section_name (abfd, sec);
9334 already_linked_list = bfd_section_already_linked_table_lookup (name);
9336 for (l = already_linked_list->entry; l != NULL; l = l->next)
9338 /* We may have 3 different sections on the list: group section,
9339 comdat section and linkonce section. SEC may be a linkonce or
9340 group section. We match a group section with a group section,
9341 a linkonce section with a linkonce section, and ignore comdat
9342 section. */
9343 if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
9344 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
9346 /* The section has already been linked. See if we should
9347 issue a warning. */
9348 switch (flags & SEC_LINK_DUPLICATES)
9350 default:
9351 abort ();
9353 case SEC_LINK_DUPLICATES_DISCARD:
9354 break;
9356 case SEC_LINK_DUPLICATES_ONE_ONLY:
9357 (*_bfd_error_handler)
9358 (_("%B: ignoring duplicate section `%A'\n"),
9359 abfd, sec);
9360 break;
9362 case SEC_LINK_DUPLICATES_SAME_SIZE:
9363 if (sec->size != l->sec->size)
9364 (*_bfd_error_handler)
9365 (_("%B: duplicate section `%A' has different size\n"),
9366 abfd, sec);
9367 break;
9369 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
9370 if (sec->size != l->sec->size)
9371 (*_bfd_error_handler)
9372 (_("%B: duplicate section `%A' has different size\n"),
9373 abfd, sec);
9374 else if (sec->size != 0)
9376 bfd_byte *sec_contents, *l_sec_contents;
9378 if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
9379 (*_bfd_error_handler)
9380 (_("%B: warning: could not read contents of section `%A'\n"),
9381 abfd, sec);
9382 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
9383 &l_sec_contents))
9384 (*_bfd_error_handler)
9385 (_("%B: warning: could not read contents of section `%A'\n"),
9386 l->sec->owner, l->sec);
9387 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
9388 (*_bfd_error_handler)
9389 (_("%B: warning: duplicate section `%A' has different contents\n"),
9390 abfd, sec);
9392 if (sec_contents)
9393 free (sec_contents);
9394 if (l_sec_contents)
9395 free (l_sec_contents);
9397 break;
9400 /* Set the output_section field so that lang_add_section
9401 does not create a lang_input_section structure for this
9402 section. Since there might be a symbol in the section
9403 being discarded, we must retain a pointer to the section
9404 which we are really going to use. */
9405 sec->output_section = bfd_abs_section_ptr;
9406 sec->kept_section = l->sec;
9408 if (flags & SEC_GROUP)
9410 asection *first = elf_next_in_group (sec);
9411 asection *s = first;
9413 while (s != NULL)
9415 s->output_section = bfd_abs_section_ptr;
9416 /* Record which group discards it. */
9417 s->kept_section = l->sec;
9418 s = elf_next_in_group (s);
9419 /* These lists are circular. */
9420 if (s == first)
9421 break;
9425 return;
9429 if (group)
9431 /* If this is the member of a single member comdat group and the
9432 group hasn't be discarded, we check if it matches a linkonce
9433 section. We only record the discarded comdat group. Otherwise
9434 the undiscarded group will be discarded incorrectly later since
9435 itself has been recorded. */
9436 if (! already_linked (elf_next_in_group (sec), group))
9437 return;
9439 else
9440 /* There is no direct match. But for linkonce section, we should
9441 check if there is a match with comdat group member. We always
9442 record the linkonce section, discarded or not. */
9443 already_linked (sec, group);
9445 /* This is the first section with this name. Record it. */
9446 bfd_section_already_linked_table_insert (already_linked_list, sec);