2005-09-08 Paul Brook <paul@codesourcery.com>
[deliverable/binutils-gdb.git] / bfd / elflink.c
1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
3 Free Software Foundation, Inc.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
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.
11
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.
16
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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
20
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"
29
30 /* Define a symbol in a dynamic linkage section. */
31
32 struct elf_link_hash_entry *
33 _bfd_elf_define_linkage_sym (bfd *abfd,
34 struct bfd_link_info *info,
35 asection *sec,
36 const char *name)
37 {
38 struct elf_link_hash_entry *h;
39 struct bfd_link_hash_entry *bh;
40 const struct elf_backend_data *bed;
41
42 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
43 if (h != NULL)
44 {
45 /* Zap symbol defined in an as-needed lib that wasn't linked.
46 This is a symptom of a larger problem: Absolute symbols
47 defined in shared libraries can't be overridden, because we
48 lose the link to the bfd which is via the symbol section. */
49 h->root.type = bfd_link_hash_new;
50 }
51
52 bh = &h->root;
53 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
54 sec, 0, NULL, FALSE,
55 get_elf_backend_data (abfd)->collect,
56 &bh))
57 return NULL;
58 h = (struct elf_link_hash_entry *) bh;
59 h->def_regular = 1;
60 h->type = STT_OBJECT;
61 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
62
63 bed = get_elf_backend_data (abfd);
64 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
65 return h;
66 }
67
68 bfd_boolean
69 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
70 {
71 flagword flags;
72 asection *s;
73 struct elf_link_hash_entry *h;
74 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
75 int ptralign;
76
77 /* This function may be called more than once. */
78 s = bfd_get_section_by_name (abfd, ".got");
79 if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
80 return TRUE;
81
82 switch (bed->s->arch_size)
83 {
84 case 32:
85 ptralign = 2;
86 break;
87
88 case 64:
89 ptralign = 3;
90 break;
91
92 default:
93 bfd_set_error (bfd_error_bad_value);
94 return FALSE;
95 }
96
97 flags = bed->dynamic_sec_flags;
98
99 s = bfd_make_section_with_flags (abfd, ".got", flags);
100 if (s == NULL
101 || !bfd_set_section_alignment (abfd, s, ptralign))
102 return FALSE;
103
104 if (bed->want_got_plt)
105 {
106 s = bfd_make_section_with_flags (abfd, ".got.plt", flags);
107 if (s == NULL
108 || !bfd_set_section_alignment (abfd, s, ptralign))
109 return FALSE;
110 }
111
112 if (bed->want_got_sym)
113 {
114 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
115 (or .got.plt) section. We don't do this in the linker script
116 because we don't want to define the symbol if we are not creating
117 a global offset table. */
118 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_GLOBAL_OFFSET_TABLE_");
119 elf_hash_table (info)->hgot = h;
120 if (h == NULL)
121 return FALSE;
122 }
123
124 /* The first bit of the global offset table is the header. */
125 s->size += bed->got_header_size;
126
127 return TRUE;
128 }
129 \f
130 /* Create a strtab to hold the dynamic symbol names. */
131 static bfd_boolean
132 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
133 {
134 struct elf_link_hash_table *hash_table;
135
136 hash_table = elf_hash_table (info);
137 if (hash_table->dynobj == NULL)
138 hash_table->dynobj = abfd;
139
140 if (hash_table->dynstr == NULL)
141 {
142 hash_table->dynstr = _bfd_elf_strtab_init ();
143 if (hash_table->dynstr == NULL)
144 return FALSE;
145 }
146 return TRUE;
147 }
148
149 /* Create some sections which will be filled in with dynamic linking
150 information. ABFD is an input file which requires dynamic sections
151 to be created. The dynamic sections take up virtual memory space
152 when the final executable is run, so we need to create them before
153 addresses are assigned to the output sections. We work out the
154 actual contents and size of these sections later. */
155
156 bfd_boolean
157 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
158 {
159 flagword flags;
160 register asection *s;
161 const struct elf_backend_data *bed;
162
163 if (! is_elf_hash_table (info->hash))
164 return FALSE;
165
166 if (elf_hash_table (info)->dynamic_sections_created)
167 return TRUE;
168
169 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
170 return FALSE;
171
172 abfd = elf_hash_table (info)->dynobj;
173 bed = get_elf_backend_data (abfd);
174
175 flags = bed->dynamic_sec_flags;
176
177 /* A dynamically linked executable has a .interp section, but a
178 shared library does not. */
179 if (info->executable)
180 {
181 s = bfd_make_section_with_flags (abfd, ".interp",
182 flags | SEC_READONLY);
183 if (s == NULL)
184 return FALSE;
185 }
186
187 if (! info->traditional_format)
188 {
189 s = bfd_make_section_with_flags (abfd, ".eh_frame_hdr",
190 flags | SEC_READONLY);
191 if (s == NULL
192 || ! bfd_set_section_alignment (abfd, s, 2))
193 return FALSE;
194 elf_hash_table (info)->eh_info.hdr_sec = s;
195 }
196
197 /* Create sections to hold version informations. These are removed
198 if they are not needed. */
199 s = bfd_make_section_with_flags (abfd, ".gnu.version_d",
200 flags | SEC_READONLY);
201 if (s == NULL
202 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
203 return FALSE;
204
205 s = bfd_make_section_with_flags (abfd, ".gnu.version",
206 flags | SEC_READONLY);
207 if (s == NULL
208 || ! bfd_set_section_alignment (abfd, s, 1))
209 return FALSE;
210
211 s = bfd_make_section_with_flags (abfd, ".gnu.version_r",
212 flags | SEC_READONLY);
213 if (s == NULL
214 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
215 return FALSE;
216
217 s = bfd_make_section_with_flags (abfd, ".dynsym",
218 flags | SEC_READONLY);
219 if (s == NULL
220 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
221 return FALSE;
222
223 s = bfd_make_section_with_flags (abfd, ".dynstr",
224 flags | SEC_READONLY);
225 if (s == NULL)
226 return FALSE;
227
228 s = bfd_make_section_with_flags (abfd, ".dynamic", flags);
229 if (s == NULL
230 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
231 return FALSE;
232
233 /* The special symbol _DYNAMIC is always set to the start of the
234 .dynamic section. We could set _DYNAMIC in a linker script, but we
235 only want to define it if we are, in fact, creating a .dynamic
236 section. We don't want to define it if there is no .dynamic
237 section, since on some ELF platforms the start up code examines it
238 to decide how to initialize the process. */
239 if (!_bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC"))
240 return FALSE;
241
242 s = bfd_make_section_with_flags (abfd, ".hash",
243 flags | SEC_READONLY);
244 if (s == NULL
245 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
246 return FALSE;
247 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
248
249 /* Let the backend create the rest of the sections. This lets the
250 backend set the right flags. The backend will normally create
251 the .got and .plt sections. */
252 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
253 return FALSE;
254
255 elf_hash_table (info)->dynamic_sections_created = TRUE;
256
257 return TRUE;
258 }
259
260 /* Create dynamic sections when linking against a dynamic object. */
261
262 bfd_boolean
263 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
264 {
265 flagword flags, pltflags;
266 asection *s;
267 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
268
269 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
270 .rel[a].bss sections. */
271 flags = bed->dynamic_sec_flags;
272
273 pltflags = flags;
274 if (bed->plt_not_loaded)
275 /* We do not clear SEC_ALLOC here because we still want the OS to
276 allocate space for the section; it's just that there's nothing
277 to read in from the object file. */
278 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
279 else
280 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
281 if (bed->plt_readonly)
282 pltflags |= SEC_READONLY;
283
284 s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
285 if (s == NULL
286 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
287 return FALSE;
288
289 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
290 .plt section. */
291 if (bed->want_plt_sym
292 && !_bfd_elf_define_linkage_sym (abfd, info, s,
293 "_PROCEDURE_LINKAGE_TABLE_"))
294 return FALSE;
295
296 s = bfd_make_section_with_flags (abfd,
297 (bed->default_use_rela_p
298 ? ".rela.plt" : ".rel.plt"),
299 flags | SEC_READONLY);
300 if (s == NULL
301 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
302 return FALSE;
303
304 if (! _bfd_elf_create_got_section (abfd, info))
305 return FALSE;
306
307 if (bed->want_dynbss)
308 {
309 /* The .dynbss section is a place to put symbols which are defined
310 by dynamic objects, are referenced by regular objects, and are
311 not functions. We must allocate space for them in the process
312 image and use a R_*_COPY reloc to tell the dynamic linker to
313 initialize them at run time. The linker script puts the .dynbss
314 section into the .bss section of the final image. */
315 s = bfd_make_section_with_flags (abfd, ".dynbss",
316 (SEC_ALLOC
317 | SEC_LINKER_CREATED));
318 if (s == NULL)
319 return FALSE;
320
321 /* The .rel[a].bss section holds copy relocs. This section is not
322 normally needed. We need to create it here, though, so that the
323 linker will map it to an output section. We can't just create it
324 only if we need it, because we will not know whether we need it
325 until we have seen all the input files, and the first time the
326 main linker code calls BFD after examining all the input files
327 (size_dynamic_sections) the input sections have already been
328 mapped to the output sections. If the section turns out not to
329 be needed, we can discard it later. We will never need this
330 section when generating a shared object, since they do not use
331 copy relocs. */
332 if (! info->shared)
333 {
334 s = bfd_make_section_with_flags (abfd,
335 (bed->default_use_rela_p
336 ? ".rela.bss" : ".rel.bss"),
337 flags | SEC_READONLY);
338 if (s == NULL
339 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
340 return FALSE;
341 }
342 }
343
344 return TRUE;
345 }
346 \f
347 /* Record a new dynamic symbol. We record the dynamic symbols as we
348 read the input files, since we need to have a list of all of them
349 before we can determine the final sizes of the output sections.
350 Note that we may actually call this function even though we are not
351 going to output any dynamic symbols; in some cases we know that a
352 symbol should be in the dynamic symbol table, but only if there is
353 one. */
354
355 bfd_boolean
356 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
357 struct elf_link_hash_entry *h)
358 {
359 if (h->dynindx == -1)
360 {
361 struct elf_strtab_hash *dynstr;
362 char *p;
363 const char *name;
364 bfd_size_type indx;
365
366 /* XXX: The ABI draft says the linker must turn hidden and
367 internal symbols into STB_LOCAL symbols when producing the
368 DSO. However, if ld.so honors st_other in the dynamic table,
369 this would not be necessary. */
370 switch (ELF_ST_VISIBILITY (h->other))
371 {
372 case STV_INTERNAL:
373 case STV_HIDDEN:
374 if (h->root.type != bfd_link_hash_undefined
375 && h->root.type != bfd_link_hash_undefweak)
376 {
377 h->forced_local = 1;
378 if (!elf_hash_table (info)->is_relocatable_executable)
379 return TRUE;
380 }
381
382 default:
383 break;
384 }
385
386 h->dynindx = elf_hash_table (info)->dynsymcount;
387 ++elf_hash_table (info)->dynsymcount;
388
389 dynstr = elf_hash_table (info)->dynstr;
390 if (dynstr == NULL)
391 {
392 /* Create a strtab to hold the dynamic symbol names. */
393 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
394 if (dynstr == NULL)
395 return FALSE;
396 }
397
398 /* We don't put any version information in the dynamic string
399 table. */
400 name = h->root.root.string;
401 p = strchr (name, ELF_VER_CHR);
402 if (p != NULL)
403 /* We know that the p points into writable memory. In fact,
404 there are only a few symbols that have read-only names, being
405 those like _GLOBAL_OFFSET_TABLE_ that are created specially
406 by the backends. Most symbols will have names pointing into
407 an ELF string table read from a file, or to objalloc memory. */
408 *p = 0;
409
410 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
411
412 if (p != NULL)
413 *p = ELF_VER_CHR;
414
415 if (indx == (bfd_size_type) -1)
416 return FALSE;
417 h->dynstr_index = indx;
418 }
419
420 return TRUE;
421 }
422 \f
423 /* Record an assignment to a symbol made by a linker script. We need
424 this in case some dynamic object refers to this symbol. */
425
426 bfd_boolean
427 bfd_elf_record_link_assignment (struct bfd_link_info *info,
428 const char *name,
429 bfd_boolean provide)
430 {
431 struct elf_link_hash_entry *h;
432 struct elf_link_hash_table *htab;
433
434 if (!is_elf_hash_table (info->hash))
435 return TRUE;
436
437 htab = elf_hash_table (info);
438 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
439 if (h == NULL)
440 return provide;
441
442 /* Since we're defining the symbol, don't let it seem to have not
443 been defined. record_dynamic_symbol and size_dynamic_sections
444 may depend on this. */
445 if (h->root.type == bfd_link_hash_undefweak
446 || h->root.type == bfd_link_hash_undefined)
447 {
448 h->root.type = bfd_link_hash_new;
449 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
450 bfd_link_repair_undef_list (&htab->root);
451 }
452
453 if (h->root.type == bfd_link_hash_new)
454 h->non_elf = 0;
455
456 /* If this symbol is being provided by the linker script, and it is
457 currently defined by a dynamic object, but not by a regular
458 object, then mark it as undefined so that the generic linker will
459 force the correct value. */
460 if (provide
461 && h->def_dynamic
462 && !h->def_regular)
463 h->root.type = bfd_link_hash_undefined;
464
465 /* If this symbol is not being provided by the linker script, and it is
466 currently defined by a dynamic object, but not by a regular object,
467 then clear out any version information because the symbol will not be
468 associated with the dynamic object any more. */
469 if (!provide
470 && h->def_dynamic
471 && !h->def_regular)
472 h->verinfo.verdef = NULL;
473
474 h->def_regular = 1;
475
476 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
477 and executables. */
478 if (!info->relocatable
479 && h->dynindx != -1
480 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
481 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
482 h->forced_local = 1;
483
484 if ((h->def_dynamic
485 || h->ref_dynamic
486 || info->shared
487 || (info->executable && elf_hash_table (info)->is_relocatable_executable))
488 && h->dynindx == -1)
489 {
490 if (! bfd_elf_link_record_dynamic_symbol (info, h))
491 return FALSE;
492
493 /* If this is a weak defined symbol, and we know a corresponding
494 real symbol from the same dynamic object, make sure the real
495 symbol is also made into a dynamic symbol. */
496 if (h->u.weakdef != NULL
497 && h->u.weakdef->dynindx == -1)
498 {
499 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
500 return FALSE;
501 }
502 }
503
504 return TRUE;
505 }
506
507 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
508 success, and 2 on a failure caused by attempting to record a symbol
509 in a discarded section, eg. a discarded link-once section symbol. */
510
511 int
512 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
513 bfd *input_bfd,
514 long input_indx)
515 {
516 bfd_size_type amt;
517 struct elf_link_local_dynamic_entry *entry;
518 struct elf_link_hash_table *eht;
519 struct elf_strtab_hash *dynstr;
520 unsigned long dynstr_index;
521 char *name;
522 Elf_External_Sym_Shndx eshndx;
523 char esym[sizeof (Elf64_External_Sym)];
524
525 if (! is_elf_hash_table (info->hash))
526 return 0;
527
528 /* See if the entry exists already. */
529 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
530 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
531 return 1;
532
533 amt = sizeof (*entry);
534 entry = bfd_alloc (input_bfd, amt);
535 if (entry == NULL)
536 return 0;
537
538 /* Go find the symbol, so that we can find it's name. */
539 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
540 1, input_indx, &entry->isym, esym, &eshndx))
541 {
542 bfd_release (input_bfd, entry);
543 return 0;
544 }
545
546 if (entry->isym.st_shndx != SHN_UNDEF
547 && (entry->isym.st_shndx < SHN_LORESERVE
548 || entry->isym.st_shndx > SHN_HIRESERVE))
549 {
550 asection *s;
551
552 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
553 if (s == NULL || bfd_is_abs_section (s->output_section))
554 {
555 /* We can still bfd_release here as nothing has done another
556 bfd_alloc. We can't do this later in this function. */
557 bfd_release (input_bfd, entry);
558 return 2;
559 }
560 }
561
562 name = (bfd_elf_string_from_elf_section
563 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
564 entry->isym.st_name));
565
566 dynstr = elf_hash_table (info)->dynstr;
567 if (dynstr == NULL)
568 {
569 /* Create a strtab to hold the dynamic symbol names. */
570 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
571 if (dynstr == NULL)
572 return 0;
573 }
574
575 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
576 if (dynstr_index == (unsigned long) -1)
577 return 0;
578 entry->isym.st_name = dynstr_index;
579
580 eht = elf_hash_table (info);
581
582 entry->next = eht->dynlocal;
583 eht->dynlocal = entry;
584 entry->input_bfd = input_bfd;
585 entry->input_indx = input_indx;
586 eht->dynsymcount++;
587
588 /* Whatever binding the symbol had before, it's now local. */
589 entry->isym.st_info
590 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
591
592 /* The dynindx will be set at the end of size_dynamic_sections. */
593
594 return 1;
595 }
596
597 /* Return the dynindex of a local dynamic symbol. */
598
599 long
600 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
601 bfd *input_bfd,
602 long input_indx)
603 {
604 struct elf_link_local_dynamic_entry *e;
605
606 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
607 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
608 return e->dynindx;
609 return -1;
610 }
611
612 /* This function is used to renumber the dynamic symbols, if some of
613 them are removed because they are marked as local. This is called
614 via elf_link_hash_traverse. */
615
616 static bfd_boolean
617 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
618 void *data)
619 {
620 size_t *count = data;
621
622 if (h->root.type == bfd_link_hash_warning)
623 h = (struct elf_link_hash_entry *) h->root.u.i.link;
624
625 if (h->forced_local)
626 return TRUE;
627
628 if (h->dynindx != -1)
629 h->dynindx = ++(*count);
630
631 return TRUE;
632 }
633
634
635 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
636 STB_LOCAL binding. */
637
638 static bfd_boolean
639 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
640 void *data)
641 {
642 size_t *count = data;
643
644 if (h->root.type == bfd_link_hash_warning)
645 h = (struct elf_link_hash_entry *) h->root.u.i.link;
646
647 if (!h->forced_local)
648 return TRUE;
649
650 if (h->dynindx != -1)
651 h->dynindx = ++(*count);
652
653 return TRUE;
654 }
655
656 /* Return true if the dynamic symbol for a given section should be
657 omitted when creating a shared library. */
658 bfd_boolean
659 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
660 struct bfd_link_info *info,
661 asection *p)
662 {
663 switch (elf_section_data (p)->this_hdr.sh_type)
664 {
665 case SHT_PROGBITS:
666 case SHT_NOBITS:
667 /* If sh_type is yet undecided, assume it could be
668 SHT_PROGBITS/SHT_NOBITS. */
669 case SHT_NULL:
670 if (strcmp (p->name, ".got") == 0
671 || strcmp (p->name, ".got.plt") == 0
672 || strcmp (p->name, ".plt") == 0)
673 {
674 asection *ip;
675 bfd *dynobj = elf_hash_table (info)->dynobj;
676
677 if (dynobj != NULL
678 && (ip = bfd_get_section_by_name (dynobj, p->name)) != NULL
679 && (ip->flags & SEC_LINKER_CREATED)
680 && ip->output_section == p)
681 return TRUE;
682 }
683 return FALSE;
684
685 /* There shouldn't be section relative relocations
686 against any other section. */
687 default:
688 return TRUE;
689 }
690 }
691
692 /* Assign dynsym indices. In a shared library we generate a section
693 symbol for each output section, which come first. Next come symbols
694 which have been forced to local binding. Then all of the back-end
695 allocated local dynamic syms, followed by the rest of the global
696 symbols. */
697
698 static unsigned long
699 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
700 struct bfd_link_info *info,
701 unsigned long *section_sym_count)
702 {
703 unsigned long dynsymcount = 0;
704
705 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
706 {
707 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
708 asection *p;
709 for (p = output_bfd->sections; p ; p = p->next)
710 if ((p->flags & SEC_EXCLUDE) == 0
711 && (p->flags & SEC_ALLOC) != 0
712 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
713 elf_section_data (p)->dynindx = ++dynsymcount;
714 }
715 *section_sym_count = dynsymcount;
716
717 elf_link_hash_traverse (elf_hash_table (info),
718 elf_link_renumber_local_hash_table_dynsyms,
719 &dynsymcount);
720
721 if (elf_hash_table (info)->dynlocal)
722 {
723 struct elf_link_local_dynamic_entry *p;
724 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
725 p->dynindx = ++dynsymcount;
726 }
727
728 elf_link_hash_traverse (elf_hash_table (info),
729 elf_link_renumber_hash_table_dynsyms,
730 &dynsymcount);
731
732 /* There is an unused NULL entry at the head of the table which
733 we must account for in our count. Unless there weren't any
734 symbols, which means we'll have no table at all. */
735 if (dynsymcount != 0)
736 ++dynsymcount;
737
738 elf_hash_table (info)->dynsymcount = dynsymcount;
739 return dynsymcount;
740 }
741
742 /* This function is called when we want to define a new symbol. It
743 handles the various cases which arise when we find a definition in
744 a dynamic object, or when there is already a definition in a
745 dynamic object. The new symbol is described by NAME, SYM, PSEC,
746 and PVALUE. We set SYM_HASH to the hash table entry. We set
747 OVERRIDE if the old symbol is overriding a new definition. We set
748 TYPE_CHANGE_OK if it is OK for the type to change. We set
749 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
750 change, we mean that we shouldn't warn if the type or size does
751 change. We set POLD_ALIGNMENT if an old common symbol in a dynamic
752 object is overridden by a regular object. */
753
754 bfd_boolean
755 _bfd_elf_merge_symbol (bfd *abfd,
756 struct bfd_link_info *info,
757 const char *name,
758 Elf_Internal_Sym *sym,
759 asection **psec,
760 bfd_vma *pvalue,
761 unsigned int *pold_alignment,
762 struct elf_link_hash_entry **sym_hash,
763 bfd_boolean *skip,
764 bfd_boolean *override,
765 bfd_boolean *type_change_ok,
766 bfd_boolean *size_change_ok)
767 {
768 asection *sec, *oldsec;
769 struct elf_link_hash_entry *h;
770 struct elf_link_hash_entry *flip;
771 int bind;
772 bfd *oldbfd;
773 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
774 bfd_boolean newweak, oldweak;
775 const struct elf_backend_data *bed;
776
777 *skip = FALSE;
778 *override = FALSE;
779
780 sec = *psec;
781 bind = ELF_ST_BIND (sym->st_info);
782
783 if (! bfd_is_und_section (sec))
784 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
785 else
786 h = ((struct elf_link_hash_entry *)
787 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
788 if (h == NULL)
789 return FALSE;
790 *sym_hash = h;
791
792 /* This code is for coping with dynamic objects, and is only useful
793 if we are doing an ELF link. */
794 if (info->hash->creator != abfd->xvec)
795 return TRUE;
796
797 /* For merging, we only care about real symbols. */
798
799 while (h->root.type == bfd_link_hash_indirect
800 || h->root.type == bfd_link_hash_warning)
801 h = (struct elf_link_hash_entry *) h->root.u.i.link;
802
803 /* If we just created the symbol, mark it as being an ELF symbol.
804 Other than that, there is nothing to do--there is no merge issue
805 with a newly defined symbol--so we just return. */
806
807 if (h->root.type == bfd_link_hash_new)
808 {
809 h->non_elf = 0;
810 return TRUE;
811 }
812
813 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
814 existing symbol. */
815
816 switch (h->root.type)
817 {
818 default:
819 oldbfd = NULL;
820 oldsec = NULL;
821 break;
822
823 case bfd_link_hash_undefined:
824 case bfd_link_hash_undefweak:
825 oldbfd = h->root.u.undef.abfd;
826 oldsec = NULL;
827 break;
828
829 case bfd_link_hash_defined:
830 case bfd_link_hash_defweak:
831 oldbfd = h->root.u.def.section->owner;
832 oldsec = h->root.u.def.section;
833 break;
834
835 case bfd_link_hash_common:
836 oldbfd = h->root.u.c.p->section->owner;
837 oldsec = h->root.u.c.p->section;
838 break;
839 }
840
841 /* In cases involving weak versioned symbols, we may wind up trying
842 to merge a symbol with itself. Catch that here, to avoid the
843 confusion that results if we try to override a symbol with
844 itself. The additional tests catch cases like
845 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
846 dynamic object, which we do want to handle here. */
847 if (abfd == oldbfd
848 && ((abfd->flags & DYNAMIC) == 0
849 || !h->def_regular))
850 return TRUE;
851
852 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
853 respectively, is from a dynamic object. */
854
855 if ((abfd->flags & DYNAMIC) != 0)
856 newdyn = TRUE;
857 else
858 newdyn = FALSE;
859
860 if (oldbfd != NULL)
861 olddyn = (oldbfd->flags & DYNAMIC) != 0;
862 else
863 {
864 asection *hsec;
865
866 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
867 indices used by MIPS ELF. */
868 switch (h->root.type)
869 {
870 default:
871 hsec = NULL;
872 break;
873
874 case bfd_link_hash_defined:
875 case bfd_link_hash_defweak:
876 hsec = h->root.u.def.section;
877 break;
878
879 case bfd_link_hash_common:
880 hsec = h->root.u.c.p->section;
881 break;
882 }
883
884 if (hsec == NULL)
885 olddyn = FALSE;
886 else
887 olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0;
888 }
889
890 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
891 respectively, appear to be a definition rather than reference. */
892
893 if (bfd_is_und_section (sec) || bfd_is_com_section (sec))
894 newdef = FALSE;
895 else
896 newdef = TRUE;
897
898 if (h->root.type == bfd_link_hash_undefined
899 || h->root.type == bfd_link_hash_undefweak
900 || h->root.type == bfd_link_hash_common)
901 olddef = FALSE;
902 else
903 olddef = TRUE;
904
905 /* Check TLS symbol. We don't check undefined symbol introduced by
906 "ld -u". */
907 if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)
908 && ELF_ST_TYPE (sym->st_info) != h->type
909 && oldbfd != NULL)
910 {
911 bfd *ntbfd, *tbfd;
912 bfd_boolean ntdef, tdef;
913 asection *ntsec, *tsec;
914
915 if (h->type == STT_TLS)
916 {
917 ntbfd = abfd;
918 ntsec = sec;
919 ntdef = newdef;
920 tbfd = oldbfd;
921 tsec = oldsec;
922 tdef = olddef;
923 }
924 else
925 {
926 ntbfd = oldbfd;
927 ntsec = oldsec;
928 ntdef = olddef;
929 tbfd = abfd;
930 tsec = sec;
931 tdef = newdef;
932 }
933
934 if (tdef && ntdef)
935 (*_bfd_error_handler)
936 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
937 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
938 else if (!tdef && !ntdef)
939 (*_bfd_error_handler)
940 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
941 tbfd, ntbfd, h->root.root.string);
942 else if (tdef)
943 (*_bfd_error_handler)
944 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
945 tbfd, tsec, ntbfd, h->root.root.string);
946 else
947 (*_bfd_error_handler)
948 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
949 tbfd, ntbfd, ntsec, h->root.root.string);
950
951 bfd_set_error (bfd_error_bad_value);
952 return FALSE;
953 }
954
955 /* We need to remember if a symbol has a definition in a dynamic
956 object or is weak in all dynamic objects. Internal and hidden
957 visibility will make it unavailable to dynamic objects. */
958 if (newdyn && !h->dynamic_def)
959 {
960 if (!bfd_is_und_section (sec))
961 h->dynamic_def = 1;
962 else
963 {
964 /* Check if this symbol is weak in all dynamic objects. If it
965 is the first time we see it in a dynamic object, we mark
966 if it is weak. Otherwise, we clear it. */
967 if (!h->ref_dynamic)
968 {
969 if (bind == STB_WEAK)
970 h->dynamic_weak = 1;
971 }
972 else if (bind != STB_WEAK)
973 h->dynamic_weak = 0;
974 }
975 }
976
977 /* If the old symbol has non-default visibility, we ignore the new
978 definition from a dynamic object. */
979 if (newdyn
980 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
981 && !bfd_is_und_section (sec))
982 {
983 *skip = TRUE;
984 /* Make sure this symbol is dynamic. */
985 h->ref_dynamic = 1;
986 /* A protected symbol has external availability. Make sure it is
987 recorded as dynamic.
988
989 FIXME: Should we check type and size for protected symbol? */
990 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
991 return bfd_elf_link_record_dynamic_symbol (info, h);
992 else
993 return TRUE;
994 }
995 else if (!newdyn
996 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
997 && h->def_dynamic)
998 {
999 /* If the new symbol with non-default visibility comes from a
1000 relocatable file and the old definition comes from a dynamic
1001 object, we remove the old definition. */
1002 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1003 h = *sym_hash;
1004
1005 if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1006 && bfd_is_und_section (sec))
1007 {
1008 /* If the new symbol is undefined and the old symbol was
1009 also undefined before, we need to make sure
1010 _bfd_generic_link_add_one_symbol doesn't mess
1011 up the linker hash table undefs list. Since the old
1012 definition came from a dynamic object, it is still on the
1013 undefs list. */
1014 h->root.type = bfd_link_hash_undefined;
1015 h->root.u.undef.abfd = abfd;
1016 }
1017 else
1018 {
1019 h->root.type = bfd_link_hash_new;
1020 h->root.u.undef.abfd = NULL;
1021 }
1022
1023 if (h->def_dynamic)
1024 {
1025 h->def_dynamic = 0;
1026 h->ref_dynamic = 1;
1027 h->dynamic_def = 1;
1028 }
1029 /* FIXME: Should we check type and size for protected symbol? */
1030 h->size = 0;
1031 h->type = 0;
1032 return TRUE;
1033 }
1034
1035 /* Differentiate strong and weak symbols. */
1036 newweak = bind == STB_WEAK;
1037 oldweak = (h->root.type == bfd_link_hash_defweak
1038 || h->root.type == bfd_link_hash_undefweak);
1039
1040 /* If a new weak symbol definition comes from a regular file and the
1041 old symbol comes from a dynamic library, we treat the new one as
1042 strong. Similarly, an old weak symbol definition from a regular
1043 file is treated as strong when the new symbol comes from a dynamic
1044 library. Further, an old weak symbol from a dynamic library is
1045 treated as strong if the new symbol is from a dynamic library.
1046 This reflects the way glibc's ld.so works.
1047
1048 Do this before setting *type_change_ok or *size_change_ok so that
1049 we warn properly when dynamic library symbols are overridden. */
1050
1051 if (newdef && !newdyn && olddyn)
1052 newweak = FALSE;
1053 if (olddef && newdyn)
1054 oldweak = FALSE;
1055
1056 /* It's OK to change the type if either the existing symbol or the
1057 new symbol is weak. A type change is also OK if the old symbol
1058 is undefined and the new symbol is defined. */
1059
1060 if (oldweak
1061 || newweak
1062 || (newdef
1063 && h->root.type == bfd_link_hash_undefined))
1064 *type_change_ok = TRUE;
1065
1066 /* It's OK to change the size if either the existing symbol or the
1067 new symbol is weak, or if the old symbol is undefined. */
1068
1069 if (*type_change_ok
1070 || h->root.type == bfd_link_hash_undefined)
1071 *size_change_ok = TRUE;
1072
1073 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1074 symbol, respectively, appears to be a common symbol in a dynamic
1075 object. If a symbol appears in an uninitialized section, and is
1076 not weak, and is not a function, then it may be a common symbol
1077 which was resolved when the dynamic object was created. We want
1078 to treat such symbols specially, because they raise special
1079 considerations when setting the symbol size: if the symbol
1080 appears as a common symbol in a regular object, and the size in
1081 the regular object is larger, we must make sure that we use the
1082 larger size. This problematic case can always be avoided in C,
1083 but it must be handled correctly when using Fortran shared
1084 libraries.
1085
1086 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1087 likewise for OLDDYNCOMMON and OLDDEF.
1088
1089 Note that this test is just a heuristic, and that it is quite
1090 possible to have an uninitialized symbol in a shared object which
1091 is really a definition, rather than a common symbol. This could
1092 lead to some minor confusion when the symbol really is a common
1093 symbol in some regular object. However, I think it will be
1094 harmless. */
1095
1096 if (newdyn
1097 && newdef
1098 && !newweak
1099 && (sec->flags & SEC_ALLOC) != 0
1100 && (sec->flags & SEC_LOAD) == 0
1101 && sym->st_size > 0
1102 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
1103 newdyncommon = TRUE;
1104 else
1105 newdyncommon = FALSE;
1106
1107 if (olddyn
1108 && olddef
1109 && h->root.type == bfd_link_hash_defined
1110 && h->def_dynamic
1111 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1112 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1113 && h->size > 0
1114 && h->type != STT_FUNC)
1115 olddyncommon = TRUE;
1116 else
1117 olddyncommon = FALSE;
1118
1119 /* We now know everything about the old and new symbols. We ask the
1120 backend to check if we can merge them. */
1121 bed = get_elf_backend_data (abfd);
1122 if (bed->merge_symbol
1123 && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue,
1124 pold_alignment, skip, override,
1125 type_change_ok, size_change_ok,
1126 &newdyn, &newdef, &newdyncommon, &newweak,
1127 abfd, &sec,
1128 &olddyn, &olddef, &olddyncommon, &oldweak,
1129 oldbfd, &oldsec))
1130 return FALSE;
1131
1132 /* If both the old and the new symbols look like common symbols in a
1133 dynamic object, set the size of the symbol to the larger of the
1134 two. */
1135
1136 if (olddyncommon
1137 && newdyncommon
1138 && sym->st_size != h->size)
1139 {
1140 /* Since we think we have two common symbols, issue a multiple
1141 common warning if desired. Note that we only warn if the
1142 size is different. If the size is the same, we simply let
1143 the old symbol override the new one as normally happens with
1144 symbols defined in dynamic objects. */
1145
1146 if (! ((*info->callbacks->multiple_common)
1147 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1148 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1149 return FALSE;
1150
1151 if (sym->st_size > h->size)
1152 h->size = sym->st_size;
1153
1154 *size_change_ok = TRUE;
1155 }
1156
1157 /* If we are looking at a dynamic object, and we have found a
1158 definition, we need to see if the symbol was already defined by
1159 some other object. If so, we want to use the existing
1160 definition, and we do not want to report a multiple symbol
1161 definition error; we do this by clobbering *PSEC to be
1162 bfd_und_section_ptr.
1163
1164 We treat a common symbol as a definition if the symbol in the
1165 shared library is a function, since common symbols always
1166 represent variables; this can cause confusion in principle, but
1167 any such confusion would seem to indicate an erroneous program or
1168 shared library. We also permit a common symbol in a regular
1169 object to override a weak symbol in a shared object. */
1170
1171 if (newdyn
1172 && newdef
1173 && (olddef
1174 || (h->root.type == bfd_link_hash_common
1175 && (newweak
1176 || ELF_ST_TYPE (sym->st_info) == STT_FUNC))))
1177 {
1178 *override = TRUE;
1179 newdef = FALSE;
1180 newdyncommon = FALSE;
1181
1182 *psec = sec = bfd_und_section_ptr;
1183 *size_change_ok = TRUE;
1184
1185 /* If we get here when the old symbol is a common symbol, then
1186 we are explicitly letting it override a weak symbol or
1187 function in a dynamic object, and we don't want to warn about
1188 a type change. If the old symbol is a defined symbol, a type
1189 change warning may still be appropriate. */
1190
1191 if (h->root.type == bfd_link_hash_common)
1192 *type_change_ok = TRUE;
1193 }
1194
1195 /* Handle the special case of an old common symbol merging with a
1196 new symbol which looks like a common symbol in a shared object.
1197 We change *PSEC and *PVALUE to make the new symbol look like a
1198 common symbol, and let _bfd_generic_link_add_one_symbol do the
1199 right thing. */
1200
1201 if (newdyncommon
1202 && h->root.type == bfd_link_hash_common)
1203 {
1204 *override = TRUE;
1205 newdef = FALSE;
1206 newdyncommon = FALSE;
1207 *pvalue = sym->st_size;
1208 *psec = sec = bed->common_section (oldsec);
1209 *size_change_ok = TRUE;
1210 }
1211
1212 /* Skip weak definitions of symbols that are already defined. */
1213 if (newdef && olddef && newweak)
1214 *skip = TRUE;
1215
1216 /* If the old symbol is from a dynamic object, and the new symbol is
1217 a definition which is not from a dynamic object, then the new
1218 symbol overrides the old symbol. Symbols from regular files
1219 always take precedence over symbols from dynamic objects, even if
1220 they are defined after the dynamic object in the link.
1221
1222 As above, we again permit a common symbol in a regular object to
1223 override a definition in a shared object if the shared object
1224 symbol is a function or is weak. */
1225
1226 flip = NULL;
1227 if (!newdyn
1228 && (newdef
1229 || (bfd_is_com_section (sec)
1230 && (oldweak
1231 || h->type == STT_FUNC)))
1232 && olddyn
1233 && olddef
1234 && h->def_dynamic)
1235 {
1236 /* Change the hash table entry to undefined, and let
1237 _bfd_generic_link_add_one_symbol do the right thing with the
1238 new definition. */
1239
1240 h->root.type = bfd_link_hash_undefined;
1241 h->root.u.undef.abfd = h->root.u.def.section->owner;
1242 *size_change_ok = TRUE;
1243
1244 olddef = FALSE;
1245 olddyncommon = FALSE;
1246
1247 /* We again permit a type change when a common symbol may be
1248 overriding a function. */
1249
1250 if (bfd_is_com_section (sec))
1251 *type_change_ok = TRUE;
1252
1253 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1254 flip = *sym_hash;
1255 else
1256 /* This union may have been set to be non-NULL when this symbol
1257 was seen in a dynamic object. We must force the union to be
1258 NULL, so that it is correct for a regular symbol. */
1259 h->verinfo.vertree = NULL;
1260 }
1261
1262 /* Handle the special case of a new common symbol merging with an
1263 old symbol that looks like it might be a common symbol defined in
1264 a shared object. Note that we have already handled the case in
1265 which a new common symbol should simply override the definition
1266 in the shared library. */
1267
1268 if (! newdyn
1269 && bfd_is_com_section (sec)
1270 && olddyncommon)
1271 {
1272 /* It would be best if we could set the hash table entry to a
1273 common symbol, but we don't know what to use for the section
1274 or the alignment. */
1275 if (! ((*info->callbacks->multiple_common)
1276 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1277 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1278 return FALSE;
1279
1280 /* If the presumed common symbol in the dynamic object is
1281 larger, pretend that the new symbol has its size. */
1282
1283 if (h->size > *pvalue)
1284 *pvalue = h->size;
1285
1286 /* We need to remember the alignment required by the symbol
1287 in the dynamic object. */
1288 BFD_ASSERT (pold_alignment);
1289 *pold_alignment = h->root.u.def.section->alignment_power;
1290
1291 olddef = FALSE;
1292 olddyncommon = FALSE;
1293
1294 h->root.type = bfd_link_hash_undefined;
1295 h->root.u.undef.abfd = h->root.u.def.section->owner;
1296
1297 *size_change_ok = TRUE;
1298 *type_change_ok = TRUE;
1299
1300 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1301 flip = *sym_hash;
1302 else
1303 h->verinfo.vertree = NULL;
1304 }
1305
1306 if (flip != NULL)
1307 {
1308 /* Handle the case where we had a versioned symbol in a dynamic
1309 library and now find a definition in a normal object. In this
1310 case, we make the versioned symbol point to the normal one. */
1311 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1312 flip->root.type = h->root.type;
1313 h->root.type = bfd_link_hash_indirect;
1314 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1315 (*bed->elf_backend_copy_indirect_symbol) (bed, flip, h);
1316 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1317 if (h->def_dynamic)
1318 {
1319 h->def_dynamic = 0;
1320 flip->ref_dynamic = 1;
1321 }
1322 }
1323
1324 return TRUE;
1325 }
1326
1327 /* This function is called to create an indirect symbol from the
1328 default for the symbol with the default version if needed. The
1329 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1330 set DYNSYM if the new indirect symbol is dynamic. */
1331
1332 bfd_boolean
1333 _bfd_elf_add_default_symbol (bfd *abfd,
1334 struct bfd_link_info *info,
1335 struct elf_link_hash_entry *h,
1336 const char *name,
1337 Elf_Internal_Sym *sym,
1338 asection **psec,
1339 bfd_vma *value,
1340 bfd_boolean *dynsym,
1341 bfd_boolean override)
1342 {
1343 bfd_boolean type_change_ok;
1344 bfd_boolean size_change_ok;
1345 bfd_boolean skip;
1346 char *shortname;
1347 struct elf_link_hash_entry *hi;
1348 struct bfd_link_hash_entry *bh;
1349 const struct elf_backend_data *bed;
1350 bfd_boolean collect;
1351 bfd_boolean dynamic;
1352 char *p;
1353 size_t len, shortlen;
1354 asection *sec;
1355
1356 /* If this symbol has a version, and it is the default version, we
1357 create an indirect symbol from the default name to the fully
1358 decorated name. This will cause external references which do not
1359 specify a version to be bound to this version of the symbol. */
1360 p = strchr (name, ELF_VER_CHR);
1361 if (p == NULL || p[1] != ELF_VER_CHR)
1362 return TRUE;
1363
1364 if (override)
1365 {
1366 /* We are overridden by an old definition. We need to check if we
1367 need to create the indirect symbol from the default name. */
1368 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1369 FALSE, FALSE);
1370 BFD_ASSERT (hi != NULL);
1371 if (hi == h)
1372 return TRUE;
1373 while (hi->root.type == bfd_link_hash_indirect
1374 || hi->root.type == bfd_link_hash_warning)
1375 {
1376 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1377 if (hi == h)
1378 return TRUE;
1379 }
1380 }
1381
1382 bed = get_elf_backend_data (abfd);
1383 collect = bed->collect;
1384 dynamic = (abfd->flags & DYNAMIC) != 0;
1385
1386 shortlen = p - name;
1387 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1388 if (shortname == NULL)
1389 return FALSE;
1390 memcpy (shortname, name, shortlen);
1391 shortname[shortlen] = '\0';
1392
1393 /* We are going to create a new symbol. Merge it with any existing
1394 symbol with this name. For the purposes of the merge, act as
1395 though we were defining the symbol we just defined, although we
1396 actually going to define an indirect symbol. */
1397 type_change_ok = FALSE;
1398 size_change_ok = FALSE;
1399 sec = *psec;
1400 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1401 NULL, &hi, &skip, &override,
1402 &type_change_ok, &size_change_ok))
1403 return FALSE;
1404
1405 if (skip)
1406 goto nondefault;
1407
1408 if (! override)
1409 {
1410 bh = &hi->root;
1411 if (! (_bfd_generic_link_add_one_symbol
1412 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1413 0, name, FALSE, collect, &bh)))
1414 return FALSE;
1415 hi = (struct elf_link_hash_entry *) bh;
1416 }
1417 else
1418 {
1419 /* In this case the symbol named SHORTNAME is overriding the
1420 indirect symbol we want to add. We were planning on making
1421 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1422 is the name without a version. NAME is the fully versioned
1423 name, and it is the default version.
1424
1425 Overriding means that we already saw a definition for the
1426 symbol SHORTNAME in a regular object, and it is overriding
1427 the symbol defined in the dynamic object.
1428
1429 When this happens, we actually want to change NAME, the
1430 symbol we just added, to refer to SHORTNAME. This will cause
1431 references to NAME in the shared object to become references
1432 to SHORTNAME in the regular object. This is what we expect
1433 when we override a function in a shared object: that the
1434 references in the shared object will be mapped to the
1435 definition in the regular object. */
1436
1437 while (hi->root.type == bfd_link_hash_indirect
1438 || hi->root.type == bfd_link_hash_warning)
1439 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1440
1441 h->root.type = bfd_link_hash_indirect;
1442 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1443 if (h->def_dynamic)
1444 {
1445 h->def_dynamic = 0;
1446 hi->ref_dynamic = 1;
1447 if (hi->ref_regular
1448 || hi->def_regular)
1449 {
1450 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1451 return FALSE;
1452 }
1453 }
1454
1455 /* Now set HI to H, so that the following code will set the
1456 other fields correctly. */
1457 hi = h;
1458 }
1459
1460 /* If there is a duplicate definition somewhere, then HI may not
1461 point to an indirect symbol. We will have reported an error to
1462 the user in that case. */
1463
1464 if (hi->root.type == bfd_link_hash_indirect)
1465 {
1466 struct elf_link_hash_entry *ht;
1467
1468 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1469 (*bed->elf_backend_copy_indirect_symbol) (bed, ht, hi);
1470
1471 /* See if the new flags lead us to realize that the symbol must
1472 be dynamic. */
1473 if (! *dynsym)
1474 {
1475 if (! dynamic)
1476 {
1477 if (info->shared
1478 || hi->ref_dynamic)
1479 *dynsym = TRUE;
1480 }
1481 else
1482 {
1483 if (hi->ref_regular)
1484 *dynsym = TRUE;
1485 }
1486 }
1487 }
1488
1489 /* We also need to define an indirection from the nondefault version
1490 of the symbol. */
1491
1492 nondefault:
1493 len = strlen (name);
1494 shortname = bfd_hash_allocate (&info->hash->table, len);
1495 if (shortname == NULL)
1496 return FALSE;
1497 memcpy (shortname, name, shortlen);
1498 memcpy (shortname + shortlen, p + 1, len - shortlen);
1499
1500 /* Once again, merge with any existing symbol. */
1501 type_change_ok = FALSE;
1502 size_change_ok = FALSE;
1503 sec = *psec;
1504 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1505 NULL, &hi, &skip, &override,
1506 &type_change_ok, &size_change_ok))
1507 return FALSE;
1508
1509 if (skip)
1510 return TRUE;
1511
1512 if (override)
1513 {
1514 /* Here SHORTNAME is a versioned name, so we don't expect to see
1515 the type of override we do in the case above unless it is
1516 overridden by a versioned definition. */
1517 if (hi->root.type != bfd_link_hash_defined
1518 && hi->root.type != bfd_link_hash_defweak)
1519 (*_bfd_error_handler)
1520 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1521 abfd, shortname);
1522 }
1523 else
1524 {
1525 bh = &hi->root;
1526 if (! (_bfd_generic_link_add_one_symbol
1527 (info, abfd, shortname, BSF_INDIRECT,
1528 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1529 return FALSE;
1530 hi = (struct elf_link_hash_entry *) bh;
1531
1532 /* If there is a duplicate definition somewhere, then HI may not
1533 point to an indirect symbol. We will have reported an error
1534 to the user in that case. */
1535
1536 if (hi->root.type == bfd_link_hash_indirect)
1537 {
1538 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
1539
1540 /* See if the new flags lead us to realize that the symbol
1541 must be dynamic. */
1542 if (! *dynsym)
1543 {
1544 if (! dynamic)
1545 {
1546 if (info->shared
1547 || hi->ref_dynamic)
1548 *dynsym = TRUE;
1549 }
1550 else
1551 {
1552 if (hi->ref_regular)
1553 *dynsym = TRUE;
1554 }
1555 }
1556 }
1557 }
1558
1559 return TRUE;
1560 }
1561 \f
1562 /* This routine is used to export all defined symbols into the dynamic
1563 symbol table. It is called via elf_link_hash_traverse. */
1564
1565 bfd_boolean
1566 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1567 {
1568 struct elf_info_failed *eif = data;
1569
1570 /* Ignore indirect symbols. These are added by the versioning code. */
1571 if (h->root.type == bfd_link_hash_indirect)
1572 return TRUE;
1573
1574 if (h->root.type == bfd_link_hash_warning)
1575 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1576
1577 if (h->dynindx == -1
1578 && (h->def_regular
1579 || h->ref_regular))
1580 {
1581 struct bfd_elf_version_tree *t;
1582 struct bfd_elf_version_expr *d;
1583
1584 for (t = eif->verdefs; t != NULL; t = t->next)
1585 {
1586 if (t->globals.list != NULL)
1587 {
1588 d = (*t->match) (&t->globals, NULL, h->root.root.string);
1589 if (d != NULL)
1590 goto doit;
1591 }
1592
1593 if (t->locals.list != NULL)
1594 {
1595 d = (*t->match) (&t->locals, NULL, h->root.root.string);
1596 if (d != NULL)
1597 return TRUE;
1598 }
1599 }
1600
1601 if (!eif->verdefs)
1602 {
1603 doit:
1604 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1605 {
1606 eif->failed = TRUE;
1607 return FALSE;
1608 }
1609 }
1610 }
1611
1612 return TRUE;
1613 }
1614 \f
1615 /* Look through the symbols which are defined in other shared
1616 libraries and referenced here. Update the list of version
1617 dependencies. This will be put into the .gnu.version_r section.
1618 This function is called via elf_link_hash_traverse. */
1619
1620 bfd_boolean
1621 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1622 void *data)
1623 {
1624 struct elf_find_verdep_info *rinfo = data;
1625 Elf_Internal_Verneed *t;
1626 Elf_Internal_Vernaux *a;
1627 bfd_size_type amt;
1628
1629 if (h->root.type == bfd_link_hash_warning)
1630 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1631
1632 /* We only care about symbols defined in shared objects with version
1633 information. */
1634 if (!h->def_dynamic
1635 || h->def_regular
1636 || h->dynindx == -1
1637 || h->verinfo.verdef == NULL)
1638 return TRUE;
1639
1640 /* See if we already know about this version. */
1641 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1642 {
1643 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1644 continue;
1645
1646 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1647 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1648 return TRUE;
1649
1650 break;
1651 }
1652
1653 /* This is a new version. Add it to tree we are building. */
1654
1655 if (t == NULL)
1656 {
1657 amt = sizeof *t;
1658 t = bfd_zalloc (rinfo->output_bfd, amt);
1659 if (t == NULL)
1660 {
1661 rinfo->failed = TRUE;
1662 return FALSE;
1663 }
1664
1665 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1666 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1667 elf_tdata (rinfo->output_bfd)->verref = t;
1668 }
1669
1670 amt = sizeof *a;
1671 a = bfd_zalloc (rinfo->output_bfd, amt);
1672
1673 /* Note that we are copying a string pointer here, and testing it
1674 above. If bfd_elf_string_from_elf_section is ever changed to
1675 discard the string data when low in memory, this will have to be
1676 fixed. */
1677 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1678
1679 a->vna_flags = h->verinfo.verdef->vd_flags;
1680 a->vna_nextptr = t->vn_auxptr;
1681
1682 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1683 ++rinfo->vers;
1684
1685 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1686
1687 t->vn_auxptr = a;
1688
1689 return TRUE;
1690 }
1691
1692 /* Figure out appropriate versions for all the symbols. We may not
1693 have the version number script until we have read all of the input
1694 files, so until that point we don't know which symbols should be
1695 local. This function is called via elf_link_hash_traverse. */
1696
1697 bfd_boolean
1698 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1699 {
1700 struct elf_assign_sym_version_info *sinfo;
1701 struct bfd_link_info *info;
1702 const struct elf_backend_data *bed;
1703 struct elf_info_failed eif;
1704 char *p;
1705 bfd_size_type amt;
1706
1707 sinfo = data;
1708 info = sinfo->info;
1709
1710 if (h->root.type == bfd_link_hash_warning)
1711 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1712
1713 /* Fix the symbol flags. */
1714 eif.failed = FALSE;
1715 eif.info = info;
1716 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1717 {
1718 if (eif.failed)
1719 sinfo->failed = TRUE;
1720 return FALSE;
1721 }
1722
1723 /* We only need version numbers for symbols defined in regular
1724 objects. */
1725 if (!h->def_regular)
1726 return TRUE;
1727
1728 bed = get_elf_backend_data (sinfo->output_bfd);
1729 p = strchr (h->root.root.string, ELF_VER_CHR);
1730 if (p != NULL && h->verinfo.vertree == NULL)
1731 {
1732 struct bfd_elf_version_tree *t;
1733 bfd_boolean hidden;
1734
1735 hidden = TRUE;
1736
1737 /* There are two consecutive ELF_VER_CHR characters if this is
1738 not a hidden symbol. */
1739 ++p;
1740 if (*p == ELF_VER_CHR)
1741 {
1742 hidden = FALSE;
1743 ++p;
1744 }
1745
1746 /* If there is no version string, we can just return out. */
1747 if (*p == '\0')
1748 {
1749 if (hidden)
1750 h->hidden = 1;
1751 return TRUE;
1752 }
1753
1754 /* Look for the version. If we find it, it is no longer weak. */
1755 for (t = sinfo->verdefs; t != NULL; t = t->next)
1756 {
1757 if (strcmp (t->name, p) == 0)
1758 {
1759 size_t len;
1760 char *alc;
1761 struct bfd_elf_version_expr *d;
1762
1763 len = p - h->root.root.string;
1764 alc = bfd_malloc (len);
1765 if (alc == NULL)
1766 return FALSE;
1767 memcpy (alc, h->root.root.string, len - 1);
1768 alc[len - 1] = '\0';
1769 if (alc[len - 2] == ELF_VER_CHR)
1770 alc[len - 2] = '\0';
1771
1772 h->verinfo.vertree = t;
1773 t->used = TRUE;
1774 d = NULL;
1775
1776 if (t->globals.list != NULL)
1777 d = (*t->match) (&t->globals, NULL, alc);
1778
1779 /* See if there is anything to force this symbol to
1780 local scope. */
1781 if (d == NULL && t->locals.list != NULL)
1782 {
1783 d = (*t->match) (&t->locals, NULL, alc);
1784 if (d != NULL
1785 && h->dynindx != -1
1786 && ! info->export_dynamic)
1787 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1788 }
1789
1790 free (alc);
1791 break;
1792 }
1793 }
1794
1795 /* If we are building an application, we need to create a
1796 version node for this version. */
1797 if (t == NULL && info->executable)
1798 {
1799 struct bfd_elf_version_tree **pp;
1800 int version_index;
1801
1802 /* If we aren't going to export this symbol, we don't need
1803 to worry about it. */
1804 if (h->dynindx == -1)
1805 return TRUE;
1806
1807 amt = sizeof *t;
1808 t = bfd_zalloc (sinfo->output_bfd, amt);
1809 if (t == NULL)
1810 {
1811 sinfo->failed = TRUE;
1812 return FALSE;
1813 }
1814
1815 t->name = p;
1816 t->name_indx = (unsigned int) -1;
1817 t->used = TRUE;
1818
1819 version_index = 1;
1820 /* Don't count anonymous version tag. */
1821 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1822 version_index = 0;
1823 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1824 ++version_index;
1825 t->vernum = version_index;
1826
1827 *pp = t;
1828
1829 h->verinfo.vertree = t;
1830 }
1831 else if (t == NULL)
1832 {
1833 /* We could not find the version for a symbol when
1834 generating a shared archive. Return an error. */
1835 (*_bfd_error_handler)
1836 (_("%B: undefined versioned symbol name %s"),
1837 sinfo->output_bfd, h->root.root.string);
1838 bfd_set_error (bfd_error_bad_value);
1839 sinfo->failed = TRUE;
1840 return FALSE;
1841 }
1842
1843 if (hidden)
1844 h->hidden = 1;
1845 }
1846
1847 /* If we don't have a version for this symbol, see if we can find
1848 something. */
1849 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
1850 {
1851 struct bfd_elf_version_tree *t;
1852 struct bfd_elf_version_tree *local_ver;
1853 struct bfd_elf_version_expr *d;
1854
1855 /* See if can find what version this symbol is in. If the
1856 symbol is supposed to be local, then don't actually register
1857 it. */
1858 local_ver = NULL;
1859 for (t = sinfo->verdefs; t != NULL; t = t->next)
1860 {
1861 if (t->globals.list != NULL)
1862 {
1863 bfd_boolean matched;
1864
1865 matched = FALSE;
1866 d = NULL;
1867 while ((d = (*t->match) (&t->globals, d,
1868 h->root.root.string)) != NULL)
1869 if (d->symver)
1870 matched = TRUE;
1871 else
1872 {
1873 /* There is a version without definition. Make
1874 the symbol the default definition for this
1875 version. */
1876 h->verinfo.vertree = t;
1877 local_ver = NULL;
1878 d->script = 1;
1879 break;
1880 }
1881 if (d != NULL)
1882 break;
1883 else if (matched)
1884 /* There is no undefined version for this symbol. Hide the
1885 default one. */
1886 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1887 }
1888
1889 if (t->locals.list != NULL)
1890 {
1891 d = NULL;
1892 while ((d = (*t->match) (&t->locals, d,
1893 h->root.root.string)) != NULL)
1894 {
1895 local_ver = t;
1896 /* If the match is "*", keep looking for a more
1897 explicit, perhaps even global, match.
1898 XXX: Shouldn't this be !d->wildcard instead? */
1899 if (d->pattern[0] != '*' || d->pattern[1] != '\0')
1900 break;
1901 }
1902
1903 if (d != NULL)
1904 break;
1905 }
1906 }
1907
1908 if (local_ver != NULL)
1909 {
1910 h->verinfo.vertree = local_ver;
1911 if (h->dynindx != -1
1912 && ! info->export_dynamic)
1913 {
1914 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1915 }
1916 }
1917 }
1918
1919 return TRUE;
1920 }
1921 \f
1922 /* Read and swap the relocs from the section indicated by SHDR. This
1923 may be either a REL or a RELA section. The relocations are
1924 translated into RELA relocations and stored in INTERNAL_RELOCS,
1925 which should have already been allocated to contain enough space.
1926 The EXTERNAL_RELOCS are a buffer where the external form of the
1927 relocations should be stored.
1928
1929 Returns FALSE if something goes wrong. */
1930
1931 static bfd_boolean
1932 elf_link_read_relocs_from_section (bfd *abfd,
1933 asection *sec,
1934 Elf_Internal_Shdr *shdr,
1935 void *external_relocs,
1936 Elf_Internal_Rela *internal_relocs)
1937 {
1938 const struct elf_backend_data *bed;
1939 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
1940 const bfd_byte *erela;
1941 const bfd_byte *erelaend;
1942 Elf_Internal_Rela *irela;
1943 Elf_Internal_Shdr *symtab_hdr;
1944 size_t nsyms;
1945
1946 /* Position ourselves at the start of the section. */
1947 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
1948 return FALSE;
1949
1950 /* Read the relocations. */
1951 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
1952 return FALSE;
1953
1954 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1955 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
1956
1957 bed = get_elf_backend_data (abfd);
1958
1959 /* Convert the external relocations to the internal format. */
1960 if (shdr->sh_entsize == bed->s->sizeof_rel)
1961 swap_in = bed->s->swap_reloc_in;
1962 else if (shdr->sh_entsize == bed->s->sizeof_rela)
1963 swap_in = bed->s->swap_reloca_in;
1964 else
1965 {
1966 bfd_set_error (bfd_error_wrong_format);
1967 return FALSE;
1968 }
1969
1970 erela = external_relocs;
1971 erelaend = erela + shdr->sh_size;
1972 irela = internal_relocs;
1973 while (erela < erelaend)
1974 {
1975 bfd_vma r_symndx;
1976
1977 (*swap_in) (abfd, erela, irela);
1978 r_symndx = ELF32_R_SYM (irela->r_info);
1979 if (bed->s->arch_size == 64)
1980 r_symndx >>= 24;
1981 if ((size_t) r_symndx >= nsyms)
1982 {
1983 (*_bfd_error_handler)
1984 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
1985 " for offset 0x%lx in section `%A'"),
1986 abfd, sec,
1987 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
1988 bfd_set_error (bfd_error_bad_value);
1989 return FALSE;
1990 }
1991 irela += bed->s->int_rels_per_ext_rel;
1992 erela += shdr->sh_entsize;
1993 }
1994
1995 return TRUE;
1996 }
1997
1998 /* Read and swap the relocs for a section O. They may have been
1999 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2000 not NULL, they are used as buffers to read into. They are known to
2001 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2002 the return value is allocated using either malloc or bfd_alloc,
2003 according to the KEEP_MEMORY argument. If O has two relocation
2004 sections (both REL and RELA relocations), then the REL_HDR
2005 relocations will appear first in INTERNAL_RELOCS, followed by the
2006 REL_HDR2 relocations. */
2007
2008 Elf_Internal_Rela *
2009 _bfd_elf_link_read_relocs (bfd *abfd,
2010 asection *o,
2011 void *external_relocs,
2012 Elf_Internal_Rela *internal_relocs,
2013 bfd_boolean keep_memory)
2014 {
2015 Elf_Internal_Shdr *rel_hdr;
2016 void *alloc1 = NULL;
2017 Elf_Internal_Rela *alloc2 = NULL;
2018 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2019
2020 if (elf_section_data (o)->relocs != NULL)
2021 return elf_section_data (o)->relocs;
2022
2023 if (o->reloc_count == 0)
2024 return NULL;
2025
2026 rel_hdr = &elf_section_data (o)->rel_hdr;
2027
2028 if (internal_relocs == NULL)
2029 {
2030 bfd_size_type size;
2031
2032 size = o->reloc_count;
2033 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2034 if (keep_memory)
2035 internal_relocs = bfd_alloc (abfd, size);
2036 else
2037 internal_relocs = alloc2 = bfd_malloc (size);
2038 if (internal_relocs == NULL)
2039 goto error_return;
2040 }
2041
2042 if (external_relocs == NULL)
2043 {
2044 bfd_size_type size = rel_hdr->sh_size;
2045
2046 if (elf_section_data (o)->rel_hdr2)
2047 size += elf_section_data (o)->rel_hdr2->sh_size;
2048 alloc1 = bfd_malloc (size);
2049 if (alloc1 == NULL)
2050 goto error_return;
2051 external_relocs = alloc1;
2052 }
2053
2054 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
2055 external_relocs,
2056 internal_relocs))
2057 goto error_return;
2058 if (elf_section_data (o)->rel_hdr2
2059 && (!elf_link_read_relocs_from_section
2060 (abfd, o,
2061 elf_section_data (o)->rel_hdr2,
2062 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2063 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2064 * bed->s->int_rels_per_ext_rel))))
2065 goto error_return;
2066
2067 /* Cache the results for next time, if we can. */
2068 if (keep_memory)
2069 elf_section_data (o)->relocs = internal_relocs;
2070
2071 if (alloc1 != NULL)
2072 free (alloc1);
2073
2074 /* Don't free alloc2, since if it was allocated we are passing it
2075 back (under the name of internal_relocs). */
2076
2077 return internal_relocs;
2078
2079 error_return:
2080 if (alloc1 != NULL)
2081 free (alloc1);
2082 if (alloc2 != NULL)
2083 free (alloc2);
2084 return NULL;
2085 }
2086
2087 /* Compute the size of, and allocate space for, REL_HDR which is the
2088 section header for a section containing relocations for O. */
2089
2090 bfd_boolean
2091 _bfd_elf_link_size_reloc_section (bfd *abfd,
2092 Elf_Internal_Shdr *rel_hdr,
2093 asection *o)
2094 {
2095 bfd_size_type reloc_count;
2096 bfd_size_type num_rel_hashes;
2097
2098 /* Figure out how many relocations there will be. */
2099 if (rel_hdr == &elf_section_data (o)->rel_hdr)
2100 reloc_count = elf_section_data (o)->rel_count;
2101 else
2102 reloc_count = elf_section_data (o)->rel_count2;
2103
2104 num_rel_hashes = o->reloc_count;
2105 if (num_rel_hashes < reloc_count)
2106 num_rel_hashes = reloc_count;
2107
2108 /* That allows us to calculate the size of the section. */
2109 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
2110
2111 /* The contents field must last into write_object_contents, so we
2112 allocate it with bfd_alloc rather than malloc. Also since we
2113 cannot be sure that the contents will actually be filled in,
2114 we zero the allocated space. */
2115 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
2116 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2117 return FALSE;
2118
2119 /* We only allocate one set of hash entries, so we only do it the
2120 first time we are called. */
2121 if (elf_section_data (o)->rel_hashes == NULL
2122 && num_rel_hashes)
2123 {
2124 struct elf_link_hash_entry **p;
2125
2126 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
2127 if (p == NULL)
2128 return FALSE;
2129
2130 elf_section_data (o)->rel_hashes = p;
2131 }
2132
2133 return TRUE;
2134 }
2135
2136 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2137 originated from the section given by INPUT_REL_HDR) to the
2138 OUTPUT_BFD. */
2139
2140 bfd_boolean
2141 _bfd_elf_link_output_relocs (bfd *output_bfd,
2142 asection *input_section,
2143 Elf_Internal_Shdr *input_rel_hdr,
2144 Elf_Internal_Rela *internal_relocs,
2145 struct elf_link_hash_entry **rel_hash
2146 ATTRIBUTE_UNUSED)
2147 {
2148 Elf_Internal_Rela *irela;
2149 Elf_Internal_Rela *irelaend;
2150 bfd_byte *erel;
2151 Elf_Internal_Shdr *output_rel_hdr;
2152 asection *output_section;
2153 unsigned int *rel_countp = NULL;
2154 const struct elf_backend_data *bed;
2155 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2156
2157 output_section = input_section->output_section;
2158 output_rel_hdr = NULL;
2159
2160 if (elf_section_data (output_section)->rel_hdr.sh_entsize
2161 == input_rel_hdr->sh_entsize)
2162 {
2163 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2164 rel_countp = &elf_section_data (output_section)->rel_count;
2165 }
2166 else if (elf_section_data (output_section)->rel_hdr2
2167 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2168 == input_rel_hdr->sh_entsize))
2169 {
2170 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2171 rel_countp = &elf_section_data (output_section)->rel_count2;
2172 }
2173 else
2174 {
2175 (*_bfd_error_handler)
2176 (_("%B: relocation size mismatch in %B section %A"),
2177 output_bfd, input_section->owner, input_section);
2178 bfd_set_error (bfd_error_wrong_object_format);
2179 return FALSE;
2180 }
2181
2182 bed = get_elf_backend_data (output_bfd);
2183 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2184 swap_out = bed->s->swap_reloc_out;
2185 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2186 swap_out = bed->s->swap_reloca_out;
2187 else
2188 abort ();
2189
2190 erel = output_rel_hdr->contents;
2191 erel += *rel_countp * input_rel_hdr->sh_entsize;
2192 irela = internal_relocs;
2193 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2194 * bed->s->int_rels_per_ext_rel);
2195 while (irela < irelaend)
2196 {
2197 (*swap_out) (output_bfd, irela, erel);
2198 irela += bed->s->int_rels_per_ext_rel;
2199 erel += input_rel_hdr->sh_entsize;
2200 }
2201
2202 /* Bump the counter, so that we know where to add the next set of
2203 relocations. */
2204 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2205
2206 return TRUE;
2207 }
2208 \f
2209 /* Fix up the flags for a symbol. This handles various cases which
2210 can only be fixed after all the input files are seen. This is
2211 currently called by both adjust_dynamic_symbol and
2212 assign_sym_version, which is unnecessary but perhaps more robust in
2213 the face of future changes. */
2214
2215 bfd_boolean
2216 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2217 struct elf_info_failed *eif)
2218 {
2219 /* If this symbol was mentioned in a non-ELF file, try to set
2220 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2221 permit a non-ELF file to correctly refer to a symbol defined in
2222 an ELF dynamic object. */
2223 if (h->non_elf)
2224 {
2225 while (h->root.type == bfd_link_hash_indirect)
2226 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2227
2228 if (h->root.type != bfd_link_hash_defined
2229 && h->root.type != bfd_link_hash_defweak)
2230 {
2231 h->ref_regular = 1;
2232 h->ref_regular_nonweak = 1;
2233 }
2234 else
2235 {
2236 if (h->root.u.def.section->owner != NULL
2237 && (bfd_get_flavour (h->root.u.def.section->owner)
2238 == bfd_target_elf_flavour))
2239 {
2240 h->ref_regular = 1;
2241 h->ref_regular_nonweak = 1;
2242 }
2243 else
2244 h->def_regular = 1;
2245 }
2246
2247 if (h->dynindx == -1
2248 && (h->def_dynamic
2249 || h->ref_dynamic))
2250 {
2251 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2252 {
2253 eif->failed = TRUE;
2254 return FALSE;
2255 }
2256 }
2257 }
2258 else
2259 {
2260 /* Unfortunately, NON_ELF is only correct if the symbol
2261 was first seen in a non-ELF file. Fortunately, if the symbol
2262 was first seen in an ELF file, we're probably OK unless the
2263 symbol was defined in a non-ELF file. Catch that case here.
2264 FIXME: We're still in trouble if the symbol was first seen in
2265 a dynamic object, and then later in a non-ELF regular object. */
2266 if ((h->root.type == bfd_link_hash_defined
2267 || h->root.type == bfd_link_hash_defweak)
2268 && !h->def_regular
2269 && (h->root.u.def.section->owner != NULL
2270 ? (bfd_get_flavour (h->root.u.def.section->owner)
2271 != bfd_target_elf_flavour)
2272 : (bfd_is_abs_section (h->root.u.def.section)
2273 && !h->def_dynamic)))
2274 h->def_regular = 1;
2275 }
2276
2277 /* If this is a final link, and the symbol was defined as a common
2278 symbol in a regular object file, and there was no definition in
2279 any dynamic object, then the linker will have allocated space for
2280 the symbol in a common section but the DEF_REGULAR
2281 flag will not have been set. */
2282 if (h->root.type == bfd_link_hash_defined
2283 && !h->def_regular
2284 && h->ref_regular
2285 && !h->def_dynamic
2286 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2287 h->def_regular = 1;
2288
2289 /* If -Bsymbolic was used (which means to bind references to global
2290 symbols to the definition within the shared object), and this
2291 symbol was defined in a regular object, then it actually doesn't
2292 need a PLT entry. Likewise, if the symbol has non-default
2293 visibility. If the symbol has hidden or internal visibility, we
2294 will force it local. */
2295 if (h->needs_plt
2296 && eif->info->shared
2297 && is_elf_hash_table (eif->info->hash)
2298 && (eif->info->symbolic
2299 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2300 && h->def_regular)
2301 {
2302 const struct elf_backend_data *bed;
2303 bfd_boolean force_local;
2304
2305 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2306
2307 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2308 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2309 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2310 }
2311
2312 /* If a weak undefined symbol has non-default visibility, we also
2313 hide it from the dynamic linker. */
2314 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2315 && h->root.type == bfd_link_hash_undefweak)
2316 {
2317 const struct elf_backend_data *bed;
2318 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2319 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2320 }
2321
2322 /* If this is a weak defined symbol in a dynamic object, and we know
2323 the real definition in the dynamic object, copy interesting flags
2324 over to the real definition. */
2325 if (h->u.weakdef != NULL)
2326 {
2327 struct elf_link_hash_entry *weakdef;
2328
2329 weakdef = h->u.weakdef;
2330 if (h->root.type == bfd_link_hash_indirect)
2331 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2332
2333 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2334 || h->root.type == bfd_link_hash_defweak);
2335 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2336 || weakdef->root.type == bfd_link_hash_defweak);
2337 BFD_ASSERT (weakdef->def_dynamic);
2338
2339 /* If the real definition is defined by a regular object file,
2340 don't do anything special. See the longer description in
2341 _bfd_elf_adjust_dynamic_symbol, below. */
2342 if (weakdef->def_regular)
2343 h->u.weakdef = NULL;
2344 else
2345 {
2346 const struct elf_backend_data *bed;
2347
2348 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2349 (*bed->elf_backend_copy_indirect_symbol) (bed, weakdef, h);
2350 }
2351 }
2352
2353 return TRUE;
2354 }
2355
2356 /* Make the backend pick a good value for a dynamic symbol. This is
2357 called via elf_link_hash_traverse, and also calls itself
2358 recursively. */
2359
2360 bfd_boolean
2361 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2362 {
2363 struct elf_info_failed *eif = data;
2364 bfd *dynobj;
2365 const struct elf_backend_data *bed;
2366
2367 if (! is_elf_hash_table (eif->info->hash))
2368 return FALSE;
2369
2370 if (h->root.type == bfd_link_hash_warning)
2371 {
2372 h->got = elf_hash_table (eif->info)->init_got_offset;
2373 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2374
2375 /* When warning symbols are created, they **replace** the "real"
2376 entry in the hash table, thus we never get to see the real
2377 symbol in a hash traversal. So look at it now. */
2378 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2379 }
2380
2381 /* Ignore indirect symbols. These are added by the versioning code. */
2382 if (h->root.type == bfd_link_hash_indirect)
2383 return TRUE;
2384
2385 /* Fix the symbol flags. */
2386 if (! _bfd_elf_fix_symbol_flags (h, eif))
2387 return FALSE;
2388
2389 /* If this symbol does not require a PLT entry, and it is not
2390 defined by a dynamic object, or is not referenced by a regular
2391 object, ignore it. We do have to handle a weak defined symbol,
2392 even if no regular object refers to it, if we decided to add it
2393 to the dynamic symbol table. FIXME: Do we normally need to worry
2394 about symbols which are defined by one dynamic object and
2395 referenced by another one? */
2396 if (!h->needs_plt
2397 && (h->def_regular
2398 || !h->def_dynamic
2399 || (!h->ref_regular
2400 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2401 {
2402 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2403 return TRUE;
2404 }
2405
2406 /* If we've already adjusted this symbol, don't do it again. This
2407 can happen via a recursive call. */
2408 if (h->dynamic_adjusted)
2409 return TRUE;
2410
2411 /* Don't look at this symbol again. Note that we must set this
2412 after checking the above conditions, because we may look at a
2413 symbol once, decide not to do anything, and then get called
2414 recursively later after REF_REGULAR is set below. */
2415 h->dynamic_adjusted = 1;
2416
2417 /* If this is a weak definition, and we know a real definition, and
2418 the real symbol is not itself defined by a regular object file,
2419 then get a good value for the real definition. We handle the
2420 real symbol first, for the convenience of the backend routine.
2421
2422 Note that there is a confusing case here. If the real definition
2423 is defined by a regular object file, we don't get the real symbol
2424 from the dynamic object, but we do get the weak symbol. If the
2425 processor backend uses a COPY reloc, then if some routine in the
2426 dynamic object changes the real symbol, we will not see that
2427 change in the corresponding weak symbol. This is the way other
2428 ELF linkers work as well, and seems to be a result of the shared
2429 library model.
2430
2431 I will clarify this issue. Most SVR4 shared libraries define the
2432 variable _timezone and define timezone as a weak synonym. The
2433 tzset call changes _timezone. If you write
2434 extern int timezone;
2435 int _timezone = 5;
2436 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2437 you might expect that, since timezone is a synonym for _timezone,
2438 the same number will print both times. However, if the processor
2439 backend uses a COPY reloc, then actually timezone will be copied
2440 into your process image, and, since you define _timezone
2441 yourself, _timezone will not. Thus timezone and _timezone will
2442 wind up at different memory locations. The tzset call will set
2443 _timezone, leaving timezone unchanged. */
2444
2445 if (h->u.weakdef != NULL)
2446 {
2447 /* If we get to this point, we know there is an implicit
2448 reference by a regular object file via the weak symbol H.
2449 FIXME: Is this really true? What if the traversal finds
2450 H->U.WEAKDEF before it finds H? */
2451 h->u.weakdef->ref_regular = 1;
2452
2453 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2454 return FALSE;
2455 }
2456
2457 /* If a symbol has no type and no size and does not require a PLT
2458 entry, then we are probably about to do the wrong thing here: we
2459 are probably going to create a COPY reloc for an empty object.
2460 This case can arise when a shared object is built with assembly
2461 code, and the assembly code fails to set the symbol type. */
2462 if (h->size == 0
2463 && h->type == STT_NOTYPE
2464 && !h->needs_plt)
2465 (*_bfd_error_handler)
2466 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2467 h->root.root.string);
2468
2469 dynobj = elf_hash_table (eif->info)->dynobj;
2470 bed = get_elf_backend_data (dynobj);
2471 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2472 {
2473 eif->failed = TRUE;
2474 return FALSE;
2475 }
2476
2477 return TRUE;
2478 }
2479
2480 /* Adjust all external symbols pointing into SEC_MERGE sections
2481 to reflect the object merging within the sections. */
2482
2483 bfd_boolean
2484 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2485 {
2486 asection *sec;
2487
2488 if (h->root.type == bfd_link_hash_warning)
2489 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2490
2491 if ((h->root.type == bfd_link_hash_defined
2492 || h->root.type == bfd_link_hash_defweak)
2493 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2494 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2495 {
2496 bfd *output_bfd = data;
2497
2498 h->root.u.def.value =
2499 _bfd_merged_section_offset (output_bfd,
2500 &h->root.u.def.section,
2501 elf_section_data (sec)->sec_info,
2502 h->root.u.def.value);
2503 }
2504
2505 return TRUE;
2506 }
2507
2508 /* Returns false if the symbol referred to by H should be considered
2509 to resolve local to the current module, and true if it should be
2510 considered to bind dynamically. */
2511
2512 bfd_boolean
2513 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2514 struct bfd_link_info *info,
2515 bfd_boolean ignore_protected)
2516 {
2517 bfd_boolean binding_stays_local_p;
2518
2519 if (h == NULL)
2520 return FALSE;
2521
2522 while (h->root.type == bfd_link_hash_indirect
2523 || h->root.type == bfd_link_hash_warning)
2524 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2525
2526 /* If it was forced local, then clearly it's not dynamic. */
2527 if (h->dynindx == -1)
2528 return FALSE;
2529 if (h->forced_local)
2530 return FALSE;
2531
2532 /* Identify the cases where name binding rules say that a
2533 visible symbol resolves locally. */
2534 binding_stays_local_p = info->executable || info->symbolic;
2535
2536 switch (ELF_ST_VISIBILITY (h->other))
2537 {
2538 case STV_INTERNAL:
2539 case STV_HIDDEN:
2540 return FALSE;
2541
2542 case STV_PROTECTED:
2543 /* Proper resolution for function pointer equality may require
2544 that these symbols perhaps be resolved dynamically, even though
2545 we should be resolving them to the current module. */
2546 if (!ignore_protected || h->type != STT_FUNC)
2547 binding_stays_local_p = TRUE;
2548 break;
2549
2550 default:
2551 break;
2552 }
2553
2554 /* If it isn't defined locally, then clearly it's dynamic. */
2555 if (!h->def_regular)
2556 return TRUE;
2557
2558 /* Otherwise, the symbol is dynamic if binding rules don't tell
2559 us that it remains local. */
2560 return !binding_stays_local_p;
2561 }
2562
2563 /* Return true if the symbol referred to by H should be considered
2564 to resolve local to the current module, and false otherwise. Differs
2565 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2566 undefined symbols and weak symbols. */
2567
2568 bfd_boolean
2569 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2570 struct bfd_link_info *info,
2571 bfd_boolean local_protected)
2572 {
2573 /* If it's a local sym, of course we resolve locally. */
2574 if (h == NULL)
2575 return TRUE;
2576
2577 /* Common symbols that become definitions don't get the DEF_REGULAR
2578 flag set, so test it first, and don't bail out. */
2579 if (ELF_COMMON_DEF_P (h))
2580 /* Do nothing. */;
2581 /* If we don't have a definition in a regular file, then we can't
2582 resolve locally. The sym is either undefined or dynamic. */
2583 else if (!h->def_regular)
2584 return FALSE;
2585
2586 /* Forced local symbols resolve locally. */
2587 if (h->forced_local)
2588 return TRUE;
2589
2590 /* As do non-dynamic symbols. */
2591 if (h->dynindx == -1)
2592 return TRUE;
2593
2594 /* At this point, we know the symbol is defined and dynamic. In an
2595 executable it must resolve locally, likewise when building symbolic
2596 shared libraries. */
2597 if (info->executable || info->symbolic)
2598 return TRUE;
2599
2600 /* Now deal with defined dynamic symbols in shared libraries. Ones
2601 with default visibility might not resolve locally. */
2602 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2603 return FALSE;
2604
2605 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2606 if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED)
2607 return TRUE;
2608
2609 /* STV_PROTECTED non-function symbols are local. */
2610 if (h->type != STT_FUNC)
2611 return TRUE;
2612
2613 /* Function pointer equality tests may require that STV_PROTECTED
2614 symbols be treated as dynamic symbols, even when we know that the
2615 dynamic linker will resolve them locally. */
2616 return local_protected;
2617 }
2618
2619 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2620 aligned. Returns the first TLS output section. */
2621
2622 struct bfd_section *
2623 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2624 {
2625 struct bfd_section *sec, *tls;
2626 unsigned int align = 0;
2627
2628 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2629 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2630 break;
2631 tls = sec;
2632
2633 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2634 if (sec->alignment_power > align)
2635 align = sec->alignment_power;
2636
2637 elf_hash_table (info)->tls_sec = tls;
2638
2639 /* Ensure the alignment of the first section is the largest alignment,
2640 so that the tls segment starts aligned. */
2641 if (tls != NULL)
2642 tls->alignment_power = align;
2643
2644 return tls;
2645 }
2646
2647 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2648 static bfd_boolean
2649 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2650 Elf_Internal_Sym *sym)
2651 {
2652 const struct elf_backend_data *bed;
2653
2654 /* Local symbols do not count, but target specific ones might. */
2655 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2656 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2657 return FALSE;
2658
2659 /* Function symbols do not count. */
2660 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
2661 return FALSE;
2662
2663 /* If the section is undefined, then so is the symbol. */
2664 if (sym->st_shndx == SHN_UNDEF)
2665 return FALSE;
2666
2667 /* If the symbol is defined in the common section, then
2668 it is a common definition and so does not count. */
2669 bed = get_elf_backend_data (abfd);
2670 if (bed->common_definition (sym))
2671 return FALSE;
2672
2673 /* If the symbol is in a target specific section then we
2674 must rely upon the backend to tell us what it is. */
2675 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2676 /* FIXME - this function is not coded yet:
2677
2678 return _bfd_is_global_symbol_definition (abfd, sym);
2679
2680 Instead for now assume that the definition is not global,
2681 Even if this is wrong, at least the linker will behave
2682 in the same way that it used to do. */
2683 return FALSE;
2684
2685 return TRUE;
2686 }
2687
2688 /* Search the symbol table of the archive element of the archive ABFD
2689 whose archive map contains a mention of SYMDEF, and determine if
2690 the symbol is defined in this element. */
2691 static bfd_boolean
2692 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2693 {
2694 Elf_Internal_Shdr * hdr;
2695 bfd_size_type symcount;
2696 bfd_size_type extsymcount;
2697 bfd_size_type extsymoff;
2698 Elf_Internal_Sym *isymbuf;
2699 Elf_Internal_Sym *isym;
2700 Elf_Internal_Sym *isymend;
2701 bfd_boolean result;
2702
2703 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2704 if (abfd == NULL)
2705 return FALSE;
2706
2707 if (! bfd_check_format (abfd, bfd_object))
2708 return FALSE;
2709
2710 /* If we have already included the element containing this symbol in the
2711 link then we do not need to include it again. Just claim that any symbol
2712 it contains is not a definition, so that our caller will not decide to
2713 (re)include this element. */
2714 if (abfd->archive_pass)
2715 return FALSE;
2716
2717 /* Select the appropriate symbol table. */
2718 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2719 hdr = &elf_tdata (abfd)->symtab_hdr;
2720 else
2721 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2722
2723 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2724
2725 /* The sh_info field of the symtab header tells us where the
2726 external symbols start. We don't care about the local symbols. */
2727 if (elf_bad_symtab (abfd))
2728 {
2729 extsymcount = symcount;
2730 extsymoff = 0;
2731 }
2732 else
2733 {
2734 extsymcount = symcount - hdr->sh_info;
2735 extsymoff = hdr->sh_info;
2736 }
2737
2738 if (extsymcount == 0)
2739 return FALSE;
2740
2741 /* Read in the symbol table. */
2742 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2743 NULL, NULL, NULL);
2744 if (isymbuf == NULL)
2745 return FALSE;
2746
2747 /* Scan the symbol table looking for SYMDEF. */
2748 result = FALSE;
2749 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2750 {
2751 const char *name;
2752
2753 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
2754 isym->st_name);
2755 if (name == NULL)
2756 break;
2757
2758 if (strcmp (name, symdef->name) == 0)
2759 {
2760 result = is_global_data_symbol_definition (abfd, isym);
2761 break;
2762 }
2763 }
2764
2765 free (isymbuf);
2766
2767 return result;
2768 }
2769 \f
2770 /* Add an entry to the .dynamic table. */
2771
2772 bfd_boolean
2773 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
2774 bfd_vma tag,
2775 bfd_vma val)
2776 {
2777 struct elf_link_hash_table *hash_table;
2778 const struct elf_backend_data *bed;
2779 asection *s;
2780 bfd_size_type newsize;
2781 bfd_byte *newcontents;
2782 Elf_Internal_Dyn dyn;
2783
2784 hash_table = elf_hash_table (info);
2785 if (! is_elf_hash_table (hash_table))
2786 return FALSE;
2787
2788 if (info->warn_shared_textrel && info->shared && tag == DT_TEXTREL)
2789 _bfd_error_handler
2790 (_("warning: creating a DT_TEXTREL in a shared object."));
2791
2792 bed = get_elf_backend_data (hash_table->dynobj);
2793 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2794 BFD_ASSERT (s != NULL);
2795
2796 newsize = s->size + bed->s->sizeof_dyn;
2797 newcontents = bfd_realloc (s->contents, newsize);
2798 if (newcontents == NULL)
2799 return FALSE;
2800
2801 dyn.d_tag = tag;
2802 dyn.d_un.d_val = val;
2803 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
2804
2805 s->size = newsize;
2806 s->contents = newcontents;
2807
2808 return TRUE;
2809 }
2810
2811 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2812 otherwise just check whether one already exists. Returns -1 on error,
2813 1 if a DT_NEEDED tag already exists, and 0 on success. */
2814
2815 static int
2816 elf_add_dt_needed_tag (bfd *abfd,
2817 struct bfd_link_info *info,
2818 const char *soname,
2819 bfd_boolean do_it)
2820 {
2821 struct elf_link_hash_table *hash_table;
2822 bfd_size_type oldsize;
2823 bfd_size_type strindex;
2824
2825 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
2826 return -1;
2827
2828 hash_table = elf_hash_table (info);
2829 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
2830 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
2831 if (strindex == (bfd_size_type) -1)
2832 return -1;
2833
2834 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
2835 {
2836 asection *sdyn;
2837 const struct elf_backend_data *bed;
2838 bfd_byte *extdyn;
2839
2840 bed = get_elf_backend_data (hash_table->dynobj);
2841 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2842 if (sdyn != NULL)
2843 for (extdyn = sdyn->contents;
2844 extdyn < sdyn->contents + sdyn->size;
2845 extdyn += bed->s->sizeof_dyn)
2846 {
2847 Elf_Internal_Dyn dyn;
2848
2849 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
2850 if (dyn.d_tag == DT_NEEDED
2851 && dyn.d_un.d_val == strindex)
2852 {
2853 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2854 return 1;
2855 }
2856 }
2857 }
2858
2859 if (do_it)
2860 {
2861 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
2862 return -1;
2863
2864 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
2865 return -1;
2866 }
2867 else
2868 /* We were just checking for existence of the tag. */
2869 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2870
2871 return 0;
2872 }
2873
2874 /* Called via elf_link_hash_traverse, elf_smash_syms sets all symbols
2875 belonging to NOT_NEEDED to bfd_link_hash_new. We know there are no
2876 references from regular objects to these symbols.
2877
2878 ??? Should we do something about references from other dynamic
2879 obects? If not, we potentially lose some warnings about undefined
2880 symbols. But how can we recover the initial undefined / undefweak
2881 state? */
2882
2883 struct elf_smash_syms_data
2884 {
2885 bfd *not_needed;
2886 struct elf_link_hash_table *htab;
2887 bfd_boolean twiddled;
2888 };
2889
2890 static bfd_boolean
2891 elf_smash_syms (struct elf_link_hash_entry *h, void *data)
2892 {
2893 struct elf_smash_syms_data *inf = (struct elf_smash_syms_data *) data;
2894 struct bfd_link_hash_entry *bh;
2895
2896 switch (h->root.type)
2897 {
2898 default:
2899 case bfd_link_hash_new:
2900 return TRUE;
2901
2902 case bfd_link_hash_undefined:
2903 if (h->root.u.undef.abfd != inf->not_needed)
2904 return TRUE;
2905 if (h->root.u.undef.weak != NULL
2906 && h->root.u.undef.weak != inf->not_needed)
2907 {
2908 /* Symbol was undefweak in u.undef.weak bfd, and has become
2909 undefined in as-needed lib. Restore weak. */
2910 h->root.type = bfd_link_hash_undefweak;
2911 h->root.u.undef.abfd = h->root.u.undef.weak;
2912 if (h->root.u.undef.next != NULL
2913 || inf->htab->root.undefs_tail == &h->root)
2914 inf->twiddled = TRUE;
2915 return TRUE;
2916 }
2917 break;
2918
2919 case bfd_link_hash_undefweak:
2920 if (h->root.u.undef.abfd != inf->not_needed)
2921 return TRUE;
2922 break;
2923
2924 case bfd_link_hash_defined:
2925 case bfd_link_hash_defweak:
2926 if (h->root.u.def.section->owner != inf->not_needed)
2927 return TRUE;
2928 break;
2929
2930 case bfd_link_hash_common:
2931 if (h->root.u.c.p->section->owner != inf->not_needed)
2932 return TRUE;
2933 break;
2934
2935 case bfd_link_hash_warning:
2936 case bfd_link_hash_indirect:
2937 elf_smash_syms ((struct elf_link_hash_entry *) h->root.u.i.link, data);
2938 if (h->root.u.i.link->type != bfd_link_hash_new)
2939 return TRUE;
2940 if (h->root.u.i.link->u.undef.abfd != inf->not_needed)
2941 return TRUE;
2942 break;
2943 }
2944
2945 /* There is no way we can undo symbol table state from defined or
2946 defweak back to undefined. */
2947 if (h->ref_regular)
2948 abort ();
2949
2950 /* Set sym back to newly created state, but keep undef.next if it is
2951 being used as a list pointer. */
2952 bh = h->root.u.undef.next;
2953 if (bh == &h->root)
2954 bh = NULL;
2955 if (bh != NULL || inf->htab->root.undefs_tail == &h->root)
2956 inf->twiddled = TRUE;
2957 (*inf->htab->root.table.newfunc) (&h->root.root,
2958 &inf->htab->root.table,
2959 h->root.root.string);
2960 h->root.u.undef.next = bh;
2961 h->root.u.undef.abfd = inf->not_needed;
2962 h->non_elf = 0;
2963 return TRUE;
2964 }
2965
2966 /* Sort symbol by value and section. */
2967 static int
2968 elf_sort_symbol (const void *arg1, const void *arg2)
2969 {
2970 const struct elf_link_hash_entry *h1;
2971 const struct elf_link_hash_entry *h2;
2972 bfd_signed_vma vdiff;
2973
2974 h1 = *(const struct elf_link_hash_entry **) arg1;
2975 h2 = *(const struct elf_link_hash_entry **) arg2;
2976 vdiff = h1->root.u.def.value - h2->root.u.def.value;
2977 if (vdiff != 0)
2978 return vdiff > 0 ? 1 : -1;
2979 else
2980 {
2981 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
2982 if (sdiff != 0)
2983 return sdiff > 0 ? 1 : -1;
2984 }
2985 return 0;
2986 }
2987
2988 /* This function is used to adjust offsets into .dynstr for
2989 dynamic symbols. This is called via elf_link_hash_traverse. */
2990
2991 static bfd_boolean
2992 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
2993 {
2994 struct elf_strtab_hash *dynstr = data;
2995
2996 if (h->root.type == bfd_link_hash_warning)
2997 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2998
2999 if (h->dynindx != -1)
3000 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3001 return TRUE;
3002 }
3003
3004 /* Assign string offsets in .dynstr, update all structures referencing
3005 them. */
3006
3007 static bfd_boolean
3008 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3009 {
3010 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3011 struct elf_link_local_dynamic_entry *entry;
3012 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3013 bfd *dynobj = hash_table->dynobj;
3014 asection *sdyn;
3015 bfd_size_type size;
3016 const struct elf_backend_data *bed;
3017 bfd_byte *extdyn;
3018
3019 _bfd_elf_strtab_finalize (dynstr);
3020 size = _bfd_elf_strtab_size (dynstr);
3021
3022 bed = get_elf_backend_data (dynobj);
3023 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3024 BFD_ASSERT (sdyn != NULL);
3025
3026 /* Update all .dynamic entries referencing .dynstr strings. */
3027 for (extdyn = sdyn->contents;
3028 extdyn < sdyn->contents + sdyn->size;
3029 extdyn += bed->s->sizeof_dyn)
3030 {
3031 Elf_Internal_Dyn dyn;
3032
3033 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3034 switch (dyn.d_tag)
3035 {
3036 case DT_STRSZ:
3037 dyn.d_un.d_val = size;
3038 break;
3039 case DT_NEEDED:
3040 case DT_SONAME:
3041 case DT_RPATH:
3042 case DT_RUNPATH:
3043 case DT_FILTER:
3044 case DT_AUXILIARY:
3045 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3046 break;
3047 default:
3048 continue;
3049 }
3050 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3051 }
3052
3053 /* Now update local dynamic symbols. */
3054 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3055 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3056 entry->isym.st_name);
3057
3058 /* And the rest of dynamic symbols. */
3059 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3060
3061 /* Adjust version definitions. */
3062 if (elf_tdata (output_bfd)->cverdefs)
3063 {
3064 asection *s;
3065 bfd_byte *p;
3066 bfd_size_type i;
3067 Elf_Internal_Verdef def;
3068 Elf_Internal_Verdaux defaux;
3069
3070 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3071 p = s->contents;
3072 do
3073 {
3074 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3075 &def);
3076 p += sizeof (Elf_External_Verdef);
3077 if (def.vd_aux != sizeof (Elf_External_Verdef))
3078 continue;
3079 for (i = 0; i < def.vd_cnt; ++i)
3080 {
3081 _bfd_elf_swap_verdaux_in (output_bfd,
3082 (Elf_External_Verdaux *) p, &defaux);
3083 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3084 defaux.vda_name);
3085 _bfd_elf_swap_verdaux_out (output_bfd,
3086 &defaux, (Elf_External_Verdaux *) p);
3087 p += sizeof (Elf_External_Verdaux);
3088 }
3089 }
3090 while (def.vd_next);
3091 }
3092
3093 /* Adjust version references. */
3094 if (elf_tdata (output_bfd)->verref)
3095 {
3096 asection *s;
3097 bfd_byte *p;
3098 bfd_size_type i;
3099 Elf_Internal_Verneed need;
3100 Elf_Internal_Vernaux needaux;
3101
3102 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3103 p = s->contents;
3104 do
3105 {
3106 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3107 &need);
3108 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3109 _bfd_elf_swap_verneed_out (output_bfd, &need,
3110 (Elf_External_Verneed *) p);
3111 p += sizeof (Elf_External_Verneed);
3112 for (i = 0; i < need.vn_cnt; ++i)
3113 {
3114 _bfd_elf_swap_vernaux_in (output_bfd,
3115 (Elf_External_Vernaux *) p, &needaux);
3116 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3117 needaux.vna_name);
3118 _bfd_elf_swap_vernaux_out (output_bfd,
3119 &needaux,
3120 (Elf_External_Vernaux *) p);
3121 p += sizeof (Elf_External_Vernaux);
3122 }
3123 }
3124 while (need.vn_next);
3125 }
3126
3127 return TRUE;
3128 }
3129 \f
3130 /* Add symbols from an ELF object file to the linker hash table. */
3131
3132 static bfd_boolean
3133 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3134 {
3135 bfd_boolean (*add_symbol_hook)
3136 (bfd *, struct bfd_link_info *, Elf_Internal_Sym *,
3137 const char **, flagword *, asection **, bfd_vma *);
3138 bfd_boolean (*check_relocs)
3139 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
3140 bfd_boolean (*check_directives)
3141 (bfd *, struct bfd_link_info *);
3142 bfd_boolean collect;
3143 Elf_Internal_Shdr *hdr;
3144 bfd_size_type symcount;
3145 bfd_size_type extsymcount;
3146 bfd_size_type extsymoff;
3147 struct elf_link_hash_entry **sym_hash;
3148 bfd_boolean dynamic;
3149 Elf_External_Versym *extversym = NULL;
3150 Elf_External_Versym *ever;
3151 struct elf_link_hash_entry *weaks;
3152 struct elf_link_hash_entry **nondeflt_vers = NULL;
3153 bfd_size_type nondeflt_vers_cnt = 0;
3154 Elf_Internal_Sym *isymbuf = NULL;
3155 Elf_Internal_Sym *isym;
3156 Elf_Internal_Sym *isymend;
3157 const struct elf_backend_data *bed;
3158 bfd_boolean add_needed;
3159 struct elf_link_hash_table * hash_table;
3160 bfd_size_type amt;
3161
3162 hash_table = elf_hash_table (info);
3163
3164 bed = get_elf_backend_data (abfd);
3165 add_symbol_hook = bed->elf_add_symbol_hook;
3166 collect = bed->collect;
3167
3168 if ((abfd->flags & DYNAMIC) == 0)
3169 dynamic = FALSE;
3170 else
3171 {
3172 dynamic = TRUE;
3173
3174 /* You can't use -r against a dynamic object. Also, there's no
3175 hope of using a dynamic object which does not exactly match
3176 the format of the output file. */
3177 if (info->relocatable
3178 || !is_elf_hash_table (hash_table)
3179 || hash_table->root.creator != abfd->xvec)
3180 {
3181 if (info->relocatable)
3182 bfd_set_error (bfd_error_invalid_operation);
3183 else
3184 bfd_set_error (bfd_error_wrong_format);
3185 goto error_return;
3186 }
3187 }
3188
3189 /* As a GNU extension, any input sections which are named
3190 .gnu.warning.SYMBOL are treated as warning symbols for the given
3191 symbol. This differs from .gnu.warning sections, which generate
3192 warnings when they are included in an output file. */
3193 if (info->executable)
3194 {
3195 asection *s;
3196
3197 for (s = abfd->sections; s != NULL; s = s->next)
3198 {
3199 const char *name;
3200
3201 name = bfd_get_section_name (abfd, s);
3202 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
3203 {
3204 char *msg;
3205 bfd_size_type sz;
3206
3207 name += sizeof ".gnu.warning." - 1;
3208
3209 /* If this is a shared object, then look up the symbol
3210 in the hash table. If it is there, and it is already
3211 been defined, then we will not be using the entry
3212 from this shared object, so we don't need to warn.
3213 FIXME: If we see the definition in a regular object
3214 later on, we will warn, but we shouldn't. The only
3215 fix is to keep track of what warnings we are supposed
3216 to emit, and then handle them all at the end of the
3217 link. */
3218 if (dynamic)
3219 {
3220 struct elf_link_hash_entry *h;
3221
3222 h = elf_link_hash_lookup (hash_table, name,
3223 FALSE, FALSE, TRUE);
3224
3225 /* FIXME: What about bfd_link_hash_common? */
3226 if (h != NULL
3227 && (h->root.type == bfd_link_hash_defined
3228 || h->root.type == bfd_link_hash_defweak))
3229 {
3230 /* We don't want to issue this warning. Clobber
3231 the section size so that the warning does not
3232 get copied into the output file. */
3233 s->size = 0;
3234 continue;
3235 }
3236 }
3237
3238 sz = s->size;
3239 msg = bfd_alloc (abfd, sz + 1);
3240 if (msg == NULL)
3241 goto error_return;
3242
3243 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3244 goto error_return;
3245
3246 msg[sz] = '\0';
3247
3248 if (! (_bfd_generic_link_add_one_symbol
3249 (info, abfd, name, BSF_WARNING, s, 0, msg,
3250 FALSE, collect, NULL)))
3251 goto error_return;
3252
3253 if (! info->relocatable)
3254 {
3255 /* Clobber the section size so that the warning does
3256 not get copied into the output file. */
3257 s->size = 0;
3258
3259 /* Also set SEC_EXCLUDE, so that symbols defined in
3260 the warning section don't get copied to the output. */
3261 s->flags |= SEC_EXCLUDE;
3262 }
3263 }
3264 }
3265 }
3266
3267 add_needed = TRUE;
3268 if (! dynamic)
3269 {
3270 /* If we are creating a shared library, create all the dynamic
3271 sections immediately. We need to attach them to something,
3272 so we attach them to this BFD, provided it is the right
3273 format. FIXME: If there are no input BFD's of the same
3274 format as the output, we can't make a shared library. */
3275 if (info->shared
3276 && is_elf_hash_table (hash_table)
3277 && hash_table->root.creator == abfd->xvec
3278 && ! hash_table->dynamic_sections_created)
3279 {
3280 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3281 goto error_return;
3282 }
3283 }
3284 else if (!is_elf_hash_table (hash_table))
3285 goto error_return;
3286 else
3287 {
3288 asection *s;
3289 const char *soname = NULL;
3290 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3291 int ret;
3292
3293 /* ld --just-symbols and dynamic objects don't mix very well.
3294 Test for --just-symbols by looking at info set up by
3295 _bfd_elf_link_just_syms. */
3296 if ((s = abfd->sections) != NULL
3297 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3298 goto error_return;
3299
3300 /* If this dynamic lib was specified on the command line with
3301 --as-needed in effect, then we don't want to add a DT_NEEDED
3302 tag unless the lib is actually used. Similary for libs brought
3303 in by another lib's DT_NEEDED. When --no-add-needed is used
3304 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3305 any dynamic library in DT_NEEDED tags in the dynamic lib at
3306 all. */
3307 add_needed = (elf_dyn_lib_class (abfd)
3308 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3309 | DYN_NO_NEEDED)) == 0;
3310
3311 s = bfd_get_section_by_name (abfd, ".dynamic");
3312 if (s != NULL)
3313 {
3314 bfd_byte *dynbuf;
3315 bfd_byte *extdyn;
3316 int elfsec;
3317 unsigned long shlink;
3318
3319 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3320 goto error_free_dyn;
3321
3322 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3323 if (elfsec == -1)
3324 goto error_free_dyn;
3325 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3326
3327 for (extdyn = dynbuf;
3328 extdyn < dynbuf + s->size;
3329 extdyn += bed->s->sizeof_dyn)
3330 {
3331 Elf_Internal_Dyn dyn;
3332
3333 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3334 if (dyn.d_tag == DT_SONAME)
3335 {
3336 unsigned int tagv = dyn.d_un.d_val;
3337 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3338 if (soname == NULL)
3339 goto error_free_dyn;
3340 }
3341 if (dyn.d_tag == DT_NEEDED)
3342 {
3343 struct bfd_link_needed_list *n, **pn;
3344 char *fnm, *anm;
3345 unsigned int tagv = dyn.d_un.d_val;
3346
3347 amt = sizeof (struct bfd_link_needed_list);
3348 n = bfd_alloc (abfd, amt);
3349 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3350 if (n == NULL || fnm == NULL)
3351 goto error_free_dyn;
3352 amt = strlen (fnm) + 1;
3353 anm = bfd_alloc (abfd, amt);
3354 if (anm == NULL)
3355 goto error_free_dyn;
3356 memcpy (anm, fnm, amt);
3357 n->name = anm;
3358 n->by = abfd;
3359 n->next = NULL;
3360 for (pn = & hash_table->needed;
3361 *pn != NULL;
3362 pn = &(*pn)->next)
3363 ;
3364 *pn = n;
3365 }
3366 if (dyn.d_tag == DT_RUNPATH)
3367 {
3368 struct bfd_link_needed_list *n, **pn;
3369 char *fnm, *anm;
3370 unsigned int tagv = dyn.d_un.d_val;
3371
3372 amt = sizeof (struct bfd_link_needed_list);
3373 n = bfd_alloc (abfd, amt);
3374 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3375 if (n == NULL || fnm == NULL)
3376 goto error_free_dyn;
3377 amt = strlen (fnm) + 1;
3378 anm = bfd_alloc (abfd, amt);
3379 if (anm == NULL)
3380 goto error_free_dyn;
3381 memcpy (anm, fnm, amt);
3382 n->name = anm;
3383 n->by = abfd;
3384 n->next = NULL;
3385 for (pn = & runpath;
3386 *pn != NULL;
3387 pn = &(*pn)->next)
3388 ;
3389 *pn = n;
3390 }
3391 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3392 if (!runpath && dyn.d_tag == DT_RPATH)
3393 {
3394 struct bfd_link_needed_list *n, **pn;
3395 char *fnm, *anm;
3396 unsigned int tagv = dyn.d_un.d_val;
3397
3398 amt = sizeof (struct bfd_link_needed_list);
3399 n = bfd_alloc (abfd, amt);
3400 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3401 if (n == NULL || fnm == NULL)
3402 goto error_free_dyn;
3403 amt = strlen (fnm) + 1;
3404 anm = bfd_alloc (abfd, amt);
3405 if (anm == NULL)
3406 {
3407 error_free_dyn:
3408 free (dynbuf);
3409 goto error_return;
3410 }
3411 memcpy (anm, fnm, amt);
3412 n->name = anm;
3413 n->by = abfd;
3414 n->next = NULL;
3415 for (pn = & rpath;
3416 *pn != NULL;
3417 pn = &(*pn)->next)
3418 ;
3419 *pn = n;
3420 }
3421 }
3422
3423 free (dynbuf);
3424 }
3425
3426 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3427 frees all more recently bfd_alloc'd blocks as well. */
3428 if (runpath)
3429 rpath = runpath;
3430
3431 if (rpath)
3432 {
3433 struct bfd_link_needed_list **pn;
3434 for (pn = & hash_table->runpath;
3435 *pn != NULL;
3436 pn = &(*pn)->next)
3437 ;
3438 *pn = rpath;
3439 }
3440
3441 /* We do not want to include any of the sections in a dynamic
3442 object in the output file. We hack by simply clobbering the
3443 list of sections in the BFD. This could be handled more
3444 cleanly by, say, a new section flag; the existing
3445 SEC_NEVER_LOAD flag is not the one we want, because that one
3446 still implies that the section takes up space in the output
3447 file. */
3448 bfd_section_list_clear (abfd);
3449
3450 /* Find the name to use in a DT_NEEDED entry that refers to this
3451 object. If the object has a DT_SONAME entry, we use it.
3452 Otherwise, if the generic linker stuck something in
3453 elf_dt_name, we use that. Otherwise, we just use the file
3454 name. */
3455 if (soname == NULL || *soname == '\0')
3456 {
3457 soname = elf_dt_name (abfd);
3458 if (soname == NULL || *soname == '\0')
3459 soname = bfd_get_filename (abfd);
3460 }
3461
3462 /* Save the SONAME because sometimes the linker emulation code
3463 will need to know it. */
3464 elf_dt_name (abfd) = soname;
3465
3466 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3467 if (ret < 0)
3468 goto error_return;
3469
3470 /* If we have already included this dynamic object in the
3471 link, just ignore it. There is no reason to include a
3472 particular dynamic object more than once. */
3473 if (ret > 0)
3474 return TRUE;
3475 }
3476
3477 /* If this is a dynamic object, we always link against the .dynsym
3478 symbol table, not the .symtab symbol table. The dynamic linker
3479 will only see the .dynsym symbol table, so there is no reason to
3480 look at .symtab for a dynamic object. */
3481
3482 if (! dynamic || elf_dynsymtab (abfd) == 0)
3483 hdr = &elf_tdata (abfd)->symtab_hdr;
3484 else
3485 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3486
3487 symcount = hdr->sh_size / bed->s->sizeof_sym;
3488
3489 /* The sh_info field of the symtab header tells us where the
3490 external symbols start. We don't care about the local symbols at
3491 this point. */
3492 if (elf_bad_symtab (abfd))
3493 {
3494 extsymcount = symcount;
3495 extsymoff = 0;
3496 }
3497 else
3498 {
3499 extsymcount = symcount - hdr->sh_info;
3500 extsymoff = hdr->sh_info;
3501 }
3502
3503 sym_hash = NULL;
3504 if (extsymcount != 0)
3505 {
3506 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3507 NULL, NULL, NULL);
3508 if (isymbuf == NULL)
3509 goto error_return;
3510
3511 /* We store a pointer to the hash table entry for each external
3512 symbol. */
3513 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3514 sym_hash = bfd_alloc (abfd, amt);
3515 if (sym_hash == NULL)
3516 goto error_free_sym;
3517 elf_sym_hashes (abfd) = sym_hash;
3518 }
3519
3520 if (dynamic)
3521 {
3522 /* Read in any version definitions. */
3523 if (!_bfd_elf_slurp_version_tables (abfd,
3524 info->default_imported_symver))
3525 goto error_free_sym;
3526
3527 /* Read in the symbol versions, but don't bother to convert them
3528 to internal format. */
3529 if (elf_dynversym (abfd) != 0)
3530 {
3531 Elf_Internal_Shdr *versymhdr;
3532
3533 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3534 extversym = bfd_malloc (versymhdr->sh_size);
3535 if (extversym == NULL)
3536 goto error_free_sym;
3537 amt = versymhdr->sh_size;
3538 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3539 || bfd_bread (extversym, amt, abfd) != amt)
3540 goto error_free_vers;
3541 }
3542 }
3543
3544 weaks = NULL;
3545
3546 ever = extversym != NULL ? extversym + extsymoff : NULL;
3547 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3548 isym < isymend;
3549 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3550 {
3551 int bind;
3552 bfd_vma value;
3553 asection *sec, *new_sec;
3554 flagword flags;
3555 const char *name;
3556 struct elf_link_hash_entry *h;
3557 bfd_boolean definition;
3558 bfd_boolean size_change_ok;
3559 bfd_boolean type_change_ok;
3560 bfd_boolean new_weakdef;
3561 bfd_boolean override;
3562 bfd_boolean common;
3563 unsigned int old_alignment;
3564 bfd *old_bfd;
3565
3566 override = FALSE;
3567
3568 flags = BSF_NO_FLAGS;
3569 sec = NULL;
3570 value = isym->st_value;
3571 *sym_hash = NULL;
3572 common = bed->common_definition (isym);
3573
3574 bind = ELF_ST_BIND (isym->st_info);
3575 if (bind == STB_LOCAL)
3576 {
3577 /* This should be impossible, since ELF requires that all
3578 global symbols follow all local symbols, and that sh_info
3579 point to the first global symbol. Unfortunately, Irix 5
3580 screws this up. */
3581 continue;
3582 }
3583 else if (bind == STB_GLOBAL)
3584 {
3585 if (isym->st_shndx != SHN_UNDEF && !common)
3586 flags = BSF_GLOBAL;
3587 }
3588 else if (bind == STB_WEAK)
3589 flags = BSF_WEAK;
3590 else
3591 {
3592 /* Leave it up to the processor backend. */
3593 }
3594
3595 if (isym->st_shndx == SHN_UNDEF)
3596 sec = bfd_und_section_ptr;
3597 else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
3598 {
3599 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3600 if (sec == NULL)
3601 sec = bfd_abs_section_ptr;
3602 else if (sec->kept_section)
3603 {
3604 /* Symbols from discarded section are undefined, and have
3605 default visibility. */
3606 sec = bfd_und_section_ptr;
3607 isym->st_shndx = SHN_UNDEF;
3608 isym->st_other = STV_DEFAULT
3609 | (isym->st_other & ~ ELF_ST_VISIBILITY(-1));
3610 }
3611 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3612 value -= sec->vma;
3613 }
3614 else if (isym->st_shndx == SHN_ABS)
3615 sec = bfd_abs_section_ptr;
3616 else if (isym->st_shndx == SHN_COMMON)
3617 {
3618 sec = bfd_com_section_ptr;
3619 /* What ELF calls the size we call the value. What ELF
3620 calls the value we call the alignment. */
3621 value = isym->st_size;
3622 }
3623 else
3624 {
3625 /* Leave it up to the processor backend. */
3626 }
3627
3628 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3629 isym->st_name);
3630 if (name == NULL)
3631 goto error_free_vers;
3632
3633 if (isym->st_shndx == SHN_COMMON
3634 && ELF_ST_TYPE (isym->st_info) == STT_TLS)
3635 {
3636 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3637
3638 if (tcomm == NULL)
3639 {
3640 tcomm = bfd_make_section_with_flags (abfd, ".tcommon",
3641 (SEC_ALLOC
3642 | SEC_IS_COMMON
3643 | SEC_LINKER_CREATED
3644 | SEC_THREAD_LOCAL));
3645 if (tcomm == NULL)
3646 goto error_free_vers;
3647 }
3648 sec = tcomm;
3649 }
3650 else if (add_symbol_hook)
3651 {
3652 if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec,
3653 &value))
3654 goto error_free_vers;
3655
3656 /* The hook function sets the name to NULL if this symbol
3657 should be skipped for some reason. */
3658 if (name == NULL)
3659 continue;
3660 }
3661
3662 /* Sanity check that all possibilities were handled. */
3663 if (sec == NULL)
3664 {
3665 bfd_set_error (bfd_error_bad_value);
3666 goto error_free_vers;
3667 }
3668
3669 if (bfd_is_und_section (sec)
3670 || bfd_is_com_section (sec))
3671 definition = FALSE;
3672 else
3673 definition = TRUE;
3674
3675 size_change_ok = FALSE;
3676 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
3677 old_alignment = 0;
3678 old_bfd = NULL;
3679 new_sec = sec;
3680
3681 if (is_elf_hash_table (hash_table))
3682 {
3683 Elf_Internal_Versym iver;
3684 unsigned int vernum = 0;
3685 bfd_boolean skip;
3686
3687 if (ever == NULL)
3688 {
3689 if (info->default_imported_symver)
3690 /* Use the default symbol version created earlier. */
3691 iver.vs_vers = elf_tdata (abfd)->cverdefs;
3692 else
3693 iver.vs_vers = 0;
3694 }
3695 else
3696 _bfd_elf_swap_versym_in (abfd, ever, &iver);
3697
3698 vernum = iver.vs_vers & VERSYM_VERSION;
3699
3700 /* If this is a hidden symbol, or if it is not version
3701 1, we append the version name to the symbol name.
3702 However, we do not modify a non-hidden absolute symbol
3703 if it is not a function, because it might be the version
3704 symbol itself. FIXME: What if it isn't? */
3705 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
3706 || (vernum > 1 && (! bfd_is_abs_section (sec)
3707 || ELF_ST_TYPE (isym->st_info) == STT_FUNC)))
3708 {
3709 const char *verstr;
3710 size_t namelen, verlen, newlen;
3711 char *newname, *p;
3712
3713 if (isym->st_shndx != SHN_UNDEF)
3714 {
3715 if (vernum > elf_tdata (abfd)->cverdefs)
3716 verstr = NULL;
3717 else if (vernum > 1)
3718 verstr =
3719 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
3720 else
3721 verstr = "";
3722
3723 if (verstr == NULL)
3724 {
3725 (*_bfd_error_handler)
3726 (_("%B: %s: invalid version %u (max %d)"),
3727 abfd, name, vernum,
3728 elf_tdata (abfd)->cverdefs);
3729 bfd_set_error (bfd_error_bad_value);
3730 goto error_free_vers;
3731 }
3732 }
3733 else
3734 {
3735 /* We cannot simply test for the number of
3736 entries in the VERNEED section since the
3737 numbers for the needed versions do not start
3738 at 0. */
3739 Elf_Internal_Verneed *t;
3740
3741 verstr = NULL;
3742 for (t = elf_tdata (abfd)->verref;
3743 t != NULL;
3744 t = t->vn_nextref)
3745 {
3746 Elf_Internal_Vernaux *a;
3747
3748 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3749 {
3750 if (a->vna_other == vernum)
3751 {
3752 verstr = a->vna_nodename;
3753 break;
3754 }
3755 }
3756 if (a != NULL)
3757 break;
3758 }
3759 if (verstr == NULL)
3760 {
3761 (*_bfd_error_handler)
3762 (_("%B: %s: invalid needed version %d"),
3763 abfd, name, vernum);
3764 bfd_set_error (bfd_error_bad_value);
3765 goto error_free_vers;
3766 }
3767 }
3768
3769 namelen = strlen (name);
3770 verlen = strlen (verstr);
3771 newlen = namelen + verlen + 2;
3772 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3773 && isym->st_shndx != SHN_UNDEF)
3774 ++newlen;
3775
3776 newname = bfd_alloc (abfd, newlen);
3777 if (newname == NULL)
3778 goto error_free_vers;
3779 memcpy (newname, name, namelen);
3780 p = newname + namelen;
3781 *p++ = ELF_VER_CHR;
3782 /* If this is a defined non-hidden version symbol,
3783 we add another @ to the name. This indicates the
3784 default version of the symbol. */
3785 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3786 && isym->st_shndx != SHN_UNDEF)
3787 *p++ = ELF_VER_CHR;
3788 memcpy (p, verstr, verlen + 1);
3789
3790 name = newname;
3791 }
3792
3793 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec,
3794 &value, &old_alignment,
3795 sym_hash, &skip, &override,
3796 &type_change_ok, &size_change_ok))
3797 goto error_free_vers;
3798
3799 if (skip)
3800 continue;
3801
3802 if (override)
3803 definition = FALSE;
3804
3805 h = *sym_hash;
3806 while (h->root.type == bfd_link_hash_indirect
3807 || h->root.type == bfd_link_hash_warning)
3808 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3809
3810 /* Remember the old alignment if this is a common symbol, so
3811 that we don't reduce the alignment later on. We can't
3812 check later, because _bfd_generic_link_add_one_symbol
3813 will set a default for the alignment which we want to
3814 override. We also remember the old bfd where the existing
3815 definition comes from. */
3816 switch (h->root.type)
3817 {
3818 default:
3819 break;
3820
3821 case bfd_link_hash_defined:
3822 case bfd_link_hash_defweak:
3823 old_bfd = h->root.u.def.section->owner;
3824 break;
3825
3826 case bfd_link_hash_common:
3827 old_bfd = h->root.u.c.p->section->owner;
3828 old_alignment = h->root.u.c.p->alignment_power;
3829 break;
3830 }
3831
3832 if (elf_tdata (abfd)->verdef != NULL
3833 && ! override
3834 && vernum > 1
3835 && definition)
3836 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
3837 }
3838
3839 if (! (_bfd_generic_link_add_one_symbol
3840 (info, abfd, name, flags, sec, value, NULL, FALSE, collect,
3841 (struct bfd_link_hash_entry **) sym_hash)))
3842 goto error_free_vers;
3843
3844 h = *sym_hash;
3845 while (h->root.type == bfd_link_hash_indirect
3846 || h->root.type == bfd_link_hash_warning)
3847 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3848 *sym_hash = h;
3849
3850 new_weakdef = FALSE;
3851 if (dynamic
3852 && definition
3853 && (flags & BSF_WEAK) != 0
3854 && ELF_ST_TYPE (isym->st_info) != STT_FUNC
3855 && is_elf_hash_table (hash_table)
3856 && h->u.weakdef == NULL)
3857 {
3858 /* Keep a list of all weak defined non function symbols from
3859 a dynamic object, using the weakdef field. Later in this
3860 function we will set the weakdef field to the correct
3861 value. We only put non-function symbols from dynamic
3862 objects on this list, because that happens to be the only
3863 time we need to know the normal symbol corresponding to a
3864 weak symbol, and the information is time consuming to
3865 figure out. If the weakdef field is not already NULL,
3866 then this symbol was already defined by some previous
3867 dynamic object, and we will be using that previous
3868 definition anyhow. */
3869
3870 h->u.weakdef = weaks;
3871 weaks = h;
3872 new_weakdef = TRUE;
3873 }
3874
3875 /* Set the alignment of a common symbol. */
3876 if ((common || bfd_is_com_section (sec))
3877 && h->root.type == bfd_link_hash_common)
3878 {
3879 unsigned int align;
3880
3881 if (common)
3882 align = bfd_log2 (isym->st_value);
3883 else
3884 {
3885 /* The new symbol is a common symbol in a shared object.
3886 We need to get the alignment from the section. */
3887 align = new_sec->alignment_power;
3888 }
3889 if (align > old_alignment
3890 /* Permit an alignment power of zero if an alignment of one
3891 is specified and no other alignments have been specified. */
3892 || (isym->st_value == 1 && old_alignment == 0))
3893 h->root.u.c.p->alignment_power = align;
3894 else
3895 h->root.u.c.p->alignment_power = old_alignment;
3896 }
3897
3898 if (is_elf_hash_table (hash_table))
3899 {
3900 bfd_boolean dynsym;
3901
3902 /* Check the alignment when a common symbol is involved. This
3903 can change when a common symbol is overridden by a normal
3904 definition or a common symbol is ignored due to the old
3905 normal definition. We need to make sure the maximum
3906 alignment is maintained. */
3907 if ((old_alignment || common)
3908 && h->root.type != bfd_link_hash_common)
3909 {
3910 unsigned int common_align;
3911 unsigned int normal_align;
3912 unsigned int symbol_align;
3913 bfd *normal_bfd;
3914 bfd *common_bfd;
3915
3916 symbol_align = ffs (h->root.u.def.value) - 1;
3917 if (h->root.u.def.section->owner != NULL
3918 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3919 {
3920 normal_align = h->root.u.def.section->alignment_power;
3921 if (normal_align > symbol_align)
3922 normal_align = symbol_align;
3923 }
3924 else
3925 normal_align = symbol_align;
3926
3927 if (old_alignment)
3928 {
3929 common_align = old_alignment;
3930 common_bfd = old_bfd;
3931 normal_bfd = abfd;
3932 }
3933 else
3934 {
3935 common_align = bfd_log2 (isym->st_value);
3936 common_bfd = abfd;
3937 normal_bfd = old_bfd;
3938 }
3939
3940 if (normal_align < common_align)
3941 (*_bfd_error_handler)
3942 (_("Warning: alignment %u of symbol `%s' in %B"
3943 " is smaller than %u in %B"),
3944 normal_bfd, common_bfd,
3945 1 << normal_align, name, 1 << common_align);
3946 }
3947
3948 /* Remember the symbol size and type. */
3949 if (isym->st_size != 0
3950 && (definition || h->size == 0))
3951 {
3952 if (h->size != 0 && h->size != isym->st_size && ! size_change_ok)
3953 (*_bfd_error_handler)
3954 (_("Warning: size of symbol `%s' changed"
3955 " from %lu in %B to %lu in %B"),
3956 old_bfd, abfd,
3957 name, (unsigned long) h->size,
3958 (unsigned long) isym->st_size);
3959
3960 h->size = isym->st_size;
3961 }
3962
3963 /* If this is a common symbol, then we always want H->SIZE
3964 to be the size of the common symbol. The code just above
3965 won't fix the size if a common symbol becomes larger. We
3966 don't warn about a size change here, because that is
3967 covered by --warn-common. */
3968 if (h->root.type == bfd_link_hash_common)
3969 h->size = h->root.u.c.size;
3970
3971 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
3972 && (definition || h->type == STT_NOTYPE))
3973 {
3974 if (h->type != STT_NOTYPE
3975 && h->type != ELF_ST_TYPE (isym->st_info)
3976 && ! type_change_ok)
3977 (*_bfd_error_handler)
3978 (_("Warning: type of symbol `%s' changed"
3979 " from %d to %d in %B"),
3980 abfd, name, h->type, ELF_ST_TYPE (isym->st_info));
3981
3982 h->type = ELF_ST_TYPE (isym->st_info);
3983 }
3984
3985 /* If st_other has a processor-specific meaning, specific
3986 code might be needed here. We never merge the visibility
3987 attribute with the one from a dynamic object. */
3988 if (bed->elf_backend_merge_symbol_attribute)
3989 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
3990 dynamic);
3991
3992 /* If this symbol has default visibility and the user has requested
3993 we not re-export it, then mark it as hidden. */
3994 if (definition && !dynamic
3995 && (abfd->no_export
3996 || (abfd->my_archive && abfd->my_archive->no_export))
3997 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
3998 isym->st_other = STV_HIDDEN | (isym->st_other & ~ ELF_ST_VISIBILITY (-1));
3999
4000 if (isym->st_other != 0 && !dynamic)
4001 {
4002 unsigned char hvis, symvis, other, nvis;
4003
4004 /* Take the balance of OTHER from the definition. */
4005 other = (definition ? isym->st_other : h->other);
4006 other &= ~ ELF_ST_VISIBILITY (-1);
4007
4008 /* Combine visibilities, using the most constraining one. */
4009 hvis = ELF_ST_VISIBILITY (h->other);
4010 symvis = ELF_ST_VISIBILITY (isym->st_other);
4011 if (! hvis)
4012 nvis = symvis;
4013 else if (! symvis)
4014 nvis = hvis;
4015 else
4016 nvis = hvis < symvis ? hvis : symvis;
4017
4018 h->other = other | nvis;
4019 }
4020
4021 /* Set a flag in the hash table entry indicating the type of
4022 reference or definition we just found. Keep a count of
4023 the number of dynamic symbols we find. A dynamic symbol
4024 is one which is referenced or defined by both a regular
4025 object and a shared object. */
4026 dynsym = FALSE;
4027 if (! dynamic)
4028 {
4029 if (! definition)
4030 {
4031 h->ref_regular = 1;
4032 if (bind != STB_WEAK)
4033 h->ref_regular_nonweak = 1;
4034 }
4035 else
4036 h->def_regular = 1;
4037 if (! info->executable
4038 || h->def_dynamic
4039 || h->ref_dynamic)
4040 dynsym = TRUE;
4041 }
4042 else
4043 {
4044 if (! definition)
4045 h->ref_dynamic = 1;
4046 else
4047 h->def_dynamic = 1;
4048 if (h->def_regular
4049 || h->ref_regular
4050 || (h->u.weakdef != NULL
4051 && ! new_weakdef
4052 && h->u.weakdef->dynindx != -1))
4053 dynsym = TRUE;
4054 }
4055
4056 /* Check to see if we need to add an indirect symbol for
4057 the default name. */
4058 if (definition || h->root.type == bfd_link_hash_common)
4059 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4060 &sec, &value, &dynsym,
4061 override))
4062 goto error_free_vers;
4063
4064 if (definition && !dynamic)
4065 {
4066 char *p = strchr (name, ELF_VER_CHR);
4067 if (p != NULL && p[1] != ELF_VER_CHR)
4068 {
4069 /* Queue non-default versions so that .symver x, x@FOO
4070 aliases can be checked. */
4071 if (! nondeflt_vers)
4072 {
4073 amt = (isymend - isym + 1)
4074 * sizeof (struct elf_link_hash_entry *);
4075 nondeflt_vers = bfd_malloc (amt);
4076 }
4077 nondeflt_vers [nondeflt_vers_cnt++] = h;
4078 }
4079 }
4080
4081 if (dynsym && h->dynindx == -1)
4082 {
4083 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4084 goto error_free_vers;
4085 if (h->u.weakdef != NULL
4086 && ! new_weakdef
4087 && h->u.weakdef->dynindx == -1)
4088 {
4089 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4090 goto error_free_vers;
4091 }
4092 }
4093 else if (dynsym && h->dynindx != -1)
4094 /* If the symbol already has a dynamic index, but
4095 visibility says it should not be visible, turn it into
4096 a local symbol. */
4097 switch (ELF_ST_VISIBILITY (h->other))
4098 {
4099 case STV_INTERNAL:
4100 case STV_HIDDEN:
4101 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4102 dynsym = FALSE;
4103 break;
4104 }
4105
4106 if (!add_needed
4107 && definition
4108 && dynsym
4109 && h->ref_regular)
4110 {
4111 int ret;
4112 const char *soname = elf_dt_name (abfd);
4113
4114 /* A symbol from a library loaded via DT_NEEDED of some
4115 other library is referenced by a regular object.
4116 Add a DT_NEEDED entry for it. Issue an error if
4117 --no-add-needed is used. */
4118 if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4119 {
4120 (*_bfd_error_handler)
4121 (_("%s: invalid DSO for symbol `%s' definition"),
4122 abfd, name);
4123 bfd_set_error (bfd_error_bad_value);
4124 goto error_free_vers;
4125 }
4126
4127 elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED;
4128
4129 add_needed = TRUE;
4130 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4131 if (ret < 0)
4132 goto error_free_vers;
4133
4134 BFD_ASSERT (ret == 0);
4135 }
4136 }
4137 }
4138
4139 /* Now that all the symbols from this input file are created, handle
4140 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4141 if (nondeflt_vers != NULL)
4142 {
4143 bfd_size_type cnt, symidx;
4144
4145 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4146 {
4147 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4148 char *shortname, *p;
4149
4150 p = strchr (h->root.root.string, ELF_VER_CHR);
4151 if (p == NULL
4152 || (h->root.type != bfd_link_hash_defined
4153 && h->root.type != bfd_link_hash_defweak))
4154 continue;
4155
4156 amt = p - h->root.root.string;
4157 shortname = bfd_malloc (amt + 1);
4158 memcpy (shortname, h->root.root.string, amt);
4159 shortname[amt] = '\0';
4160
4161 hi = (struct elf_link_hash_entry *)
4162 bfd_link_hash_lookup (&hash_table->root, shortname,
4163 FALSE, FALSE, FALSE);
4164 if (hi != NULL
4165 && hi->root.type == h->root.type
4166 && hi->root.u.def.value == h->root.u.def.value
4167 && hi->root.u.def.section == h->root.u.def.section)
4168 {
4169 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4170 hi->root.type = bfd_link_hash_indirect;
4171 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4172 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
4173 sym_hash = elf_sym_hashes (abfd);
4174 if (sym_hash)
4175 for (symidx = 0; symidx < extsymcount; ++symidx)
4176 if (sym_hash[symidx] == hi)
4177 {
4178 sym_hash[symidx] = h;
4179 break;
4180 }
4181 }
4182 free (shortname);
4183 }
4184 free (nondeflt_vers);
4185 nondeflt_vers = NULL;
4186 }
4187
4188 if (extversym != NULL)
4189 {
4190 free (extversym);
4191 extversym = NULL;
4192 }
4193
4194 if (isymbuf != NULL)
4195 free (isymbuf);
4196 isymbuf = NULL;
4197
4198 if (!add_needed
4199 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4200 {
4201 /* Remove symbols defined in an as-needed shared lib that wasn't
4202 needed. */
4203 struct elf_smash_syms_data inf;
4204 inf.not_needed = abfd;
4205 inf.htab = hash_table;
4206 inf.twiddled = FALSE;
4207 elf_link_hash_traverse (hash_table, elf_smash_syms, &inf);
4208 if (inf.twiddled)
4209 bfd_link_repair_undef_list (&hash_table->root);
4210 weaks = NULL;
4211 }
4212
4213 /* Now set the weakdefs field correctly for all the weak defined
4214 symbols we found. The only way to do this is to search all the
4215 symbols. Since we only need the information for non functions in
4216 dynamic objects, that's the only time we actually put anything on
4217 the list WEAKS. We need this information so that if a regular
4218 object refers to a symbol defined weakly in a dynamic object, the
4219 real symbol in the dynamic object is also put in the dynamic
4220 symbols; we also must arrange for both symbols to point to the
4221 same memory location. We could handle the general case of symbol
4222 aliasing, but a general symbol alias can only be generated in
4223 assembler code, handling it correctly would be very time
4224 consuming, and other ELF linkers don't handle general aliasing
4225 either. */
4226 if (weaks != NULL)
4227 {
4228 struct elf_link_hash_entry **hpp;
4229 struct elf_link_hash_entry **hppend;
4230 struct elf_link_hash_entry **sorted_sym_hash;
4231 struct elf_link_hash_entry *h;
4232 size_t sym_count;
4233
4234 /* Since we have to search the whole symbol list for each weak
4235 defined symbol, search time for N weak defined symbols will be
4236 O(N^2). Binary search will cut it down to O(NlogN). */
4237 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4238 sorted_sym_hash = bfd_malloc (amt);
4239 if (sorted_sym_hash == NULL)
4240 goto error_return;
4241 sym_hash = sorted_sym_hash;
4242 hpp = elf_sym_hashes (abfd);
4243 hppend = hpp + extsymcount;
4244 sym_count = 0;
4245 for (; hpp < hppend; hpp++)
4246 {
4247 h = *hpp;
4248 if (h != NULL
4249 && h->root.type == bfd_link_hash_defined
4250 && h->type != STT_FUNC)
4251 {
4252 *sym_hash = h;
4253 sym_hash++;
4254 sym_count++;
4255 }
4256 }
4257
4258 qsort (sorted_sym_hash, sym_count,
4259 sizeof (struct elf_link_hash_entry *),
4260 elf_sort_symbol);
4261
4262 while (weaks != NULL)
4263 {
4264 struct elf_link_hash_entry *hlook;
4265 asection *slook;
4266 bfd_vma vlook;
4267 long ilook;
4268 size_t i, j, idx;
4269
4270 hlook = weaks;
4271 weaks = hlook->u.weakdef;
4272 hlook->u.weakdef = NULL;
4273
4274 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4275 || hlook->root.type == bfd_link_hash_defweak
4276 || hlook->root.type == bfd_link_hash_common
4277 || hlook->root.type == bfd_link_hash_indirect);
4278 slook = hlook->root.u.def.section;
4279 vlook = hlook->root.u.def.value;
4280
4281 ilook = -1;
4282 i = 0;
4283 j = sym_count;
4284 while (i < j)
4285 {
4286 bfd_signed_vma vdiff;
4287 idx = (i + j) / 2;
4288 h = sorted_sym_hash [idx];
4289 vdiff = vlook - h->root.u.def.value;
4290 if (vdiff < 0)
4291 j = idx;
4292 else if (vdiff > 0)
4293 i = idx + 1;
4294 else
4295 {
4296 long sdiff = slook->id - h->root.u.def.section->id;
4297 if (sdiff < 0)
4298 j = idx;
4299 else if (sdiff > 0)
4300 i = idx + 1;
4301 else
4302 {
4303 ilook = idx;
4304 break;
4305 }
4306 }
4307 }
4308
4309 /* We didn't find a value/section match. */
4310 if (ilook == -1)
4311 continue;
4312
4313 for (i = ilook; i < sym_count; i++)
4314 {
4315 h = sorted_sym_hash [i];
4316
4317 /* Stop if value or section doesn't match. */
4318 if (h->root.u.def.value != vlook
4319 || h->root.u.def.section != slook)
4320 break;
4321 else if (h != hlook)
4322 {
4323 hlook->u.weakdef = h;
4324
4325 /* If the weak definition is in the list of dynamic
4326 symbols, make sure the real definition is put
4327 there as well. */
4328 if (hlook->dynindx != -1 && h->dynindx == -1)
4329 {
4330 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4331 goto error_return;
4332 }
4333
4334 /* If the real definition is in the list of dynamic
4335 symbols, make sure the weak definition is put
4336 there as well. If we don't do this, then the
4337 dynamic loader might not merge the entries for the
4338 real definition and the weak definition. */
4339 if (h->dynindx != -1 && hlook->dynindx == -1)
4340 {
4341 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4342 goto error_return;
4343 }
4344 break;
4345 }
4346 }
4347 }
4348
4349 free (sorted_sym_hash);
4350 }
4351
4352 check_directives = get_elf_backend_data (abfd)->check_directives;
4353 if (check_directives)
4354 check_directives (abfd, info);
4355
4356 /* If this object is the same format as the output object, and it is
4357 not a shared library, then let the backend look through the
4358 relocs.
4359
4360 This is required to build global offset table entries and to
4361 arrange for dynamic relocs. It is not required for the
4362 particular common case of linking non PIC code, even when linking
4363 against shared libraries, but unfortunately there is no way of
4364 knowing whether an object file has been compiled PIC or not.
4365 Looking through the relocs is not particularly time consuming.
4366 The problem is that we must either (1) keep the relocs in memory,
4367 which causes the linker to require additional runtime memory or
4368 (2) read the relocs twice from the input file, which wastes time.
4369 This would be a good case for using mmap.
4370
4371 I have no idea how to handle linking PIC code into a file of a
4372 different format. It probably can't be done. */
4373 check_relocs = get_elf_backend_data (abfd)->check_relocs;
4374 if (! dynamic
4375 && is_elf_hash_table (hash_table)
4376 && hash_table->root.creator == abfd->xvec
4377 && check_relocs != NULL)
4378 {
4379 asection *o;
4380
4381 for (o = abfd->sections; o != NULL; o = o->next)
4382 {
4383 Elf_Internal_Rela *internal_relocs;
4384 bfd_boolean ok;
4385
4386 if ((o->flags & SEC_RELOC) == 0
4387 || o->reloc_count == 0
4388 || ((info->strip == strip_all || info->strip == strip_debugger)
4389 && (o->flags & SEC_DEBUGGING) != 0)
4390 || bfd_is_abs_section (o->output_section))
4391 continue;
4392
4393 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4394 info->keep_memory);
4395 if (internal_relocs == NULL)
4396 goto error_return;
4397
4398 ok = (*check_relocs) (abfd, info, o, internal_relocs);
4399
4400 if (elf_section_data (o)->relocs != internal_relocs)
4401 free (internal_relocs);
4402
4403 if (! ok)
4404 goto error_return;
4405 }
4406 }
4407
4408 /* If this is a non-traditional link, try to optimize the handling
4409 of the .stab/.stabstr sections. */
4410 if (! dynamic
4411 && ! info->traditional_format
4412 && is_elf_hash_table (hash_table)
4413 && (info->strip != strip_all && info->strip != strip_debugger))
4414 {
4415 asection *stabstr;
4416
4417 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4418 if (stabstr != NULL)
4419 {
4420 bfd_size_type string_offset = 0;
4421 asection *stab;
4422
4423 for (stab = abfd->sections; stab; stab = stab->next)
4424 if (strncmp (".stab", stab->name, 5) == 0
4425 && (!stab->name[5] ||
4426 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4427 && (stab->flags & SEC_MERGE) == 0
4428 && !bfd_is_abs_section (stab->output_section))
4429 {
4430 struct bfd_elf_section_data *secdata;
4431
4432 secdata = elf_section_data (stab);
4433 if (! _bfd_link_section_stabs (abfd,
4434 &hash_table->stab_info,
4435 stab, stabstr,
4436 &secdata->sec_info,
4437 &string_offset))
4438 goto error_return;
4439 if (secdata->sec_info)
4440 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4441 }
4442 }
4443 }
4444
4445 if (is_elf_hash_table (hash_table) && add_needed)
4446 {
4447 /* Add this bfd to the loaded list. */
4448 struct elf_link_loaded_list *n;
4449
4450 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4451 if (n == NULL)
4452 goto error_return;
4453 n->abfd = abfd;
4454 n->next = hash_table->loaded;
4455 hash_table->loaded = n;
4456 }
4457
4458 return TRUE;
4459
4460 error_free_vers:
4461 if (nondeflt_vers != NULL)
4462 free (nondeflt_vers);
4463 if (extversym != NULL)
4464 free (extversym);
4465 error_free_sym:
4466 if (isymbuf != NULL)
4467 free (isymbuf);
4468 error_return:
4469 return FALSE;
4470 }
4471
4472 /* Return the linker hash table entry of a symbol that might be
4473 satisfied by an archive symbol. Return -1 on error. */
4474
4475 struct elf_link_hash_entry *
4476 _bfd_elf_archive_symbol_lookup (bfd *abfd,
4477 struct bfd_link_info *info,
4478 const char *name)
4479 {
4480 struct elf_link_hash_entry *h;
4481 char *p, *copy;
4482 size_t len, first;
4483
4484 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4485 if (h != NULL)
4486 return h;
4487
4488 /* If this is a default version (the name contains @@), look up the
4489 symbol again with only one `@' as well as without the version.
4490 The effect is that references to the symbol with and without the
4491 version will be matched by the default symbol in the archive. */
4492
4493 p = strchr (name, ELF_VER_CHR);
4494 if (p == NULL || p[1] != ELF_VER_CHR)
4495 return h;
4496
4497 /* First check with only one `@'. */
4498 len = strlen (name);
4499 copy = bfd_alloc (abfd, len);
4500 if (copy == NULL)
4501 return (struct elf_link_hash_entry *) 0 - 1;
4502
4503 first = p - name + 1;
4504 memcpy (copy, name, first);
4505 memcpy (copy + first, name + first + 1, len - first);
4506
4507 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
4508 if (h == NULL)
4509 {
4510 /* We also need to check references to the symbol without the
4511 version. */
4512 copy[first - 1] = '\0';
4513 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4514 FALSE, FALSE, FALSE);
4515 }
4516
4517 bfd_release (abfd, copy);
4518 return h;
4519 }
4520
4521 /* Add symbols from an ELF archive file to the linker hash table. We
4522 don't use _bfd_generic_link_add_archive_symbols because of a
4523 problem which arises on UnixWare. The UnixWare libc.so is an
4524 archive which includes an entry libc.so.1 which defines a bunch of
4525 symbols. The libc.so archive also includes a number of other
4526 object files, which also define symbols, some of which are the same
4527 as those defined in libc.so.1. Correct linking requires that we
4528 consider each object file in turn, and include it if it defines any
4529 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4530 this; it looks through the list of undefined symbols, and includes
4531 any object file which defines them. When this algorithm is used on
4532 UnixWare, it winds up pulling in libc.so.1 early and defining a
4533 bunch of symbols. This means that some of the other objects in the
4534 archive are not included in the link, which is incorrect since they
4535 precede libc.so.1 in the archive.
4536
4537 Fortunately, ELF archive handling is simpler than that done by
4538 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4539 oddities. In ELF, if we find a symbol in the archive map, and the
4540 symbol is currently undefined, we know that we must pull in that
4541 object file.
4542
4543 Unfortunately, we do have to make multiple passes over the symbol
4544 table until nothing further is resolved. */
4545
4546 static bfd_boolean
4547 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
4548 {
4549 symindex c;
4550 bfd_boolean *defined = NULL;
4551 bfd_boolean *included = NULL;
4552 carsym *symdefs;
4553 bfd_boolean loop;
4554 bfd_size_type amt;
4555 const struct elf_backend_data *bed;
4556 struct elf_link_hash_entry * (*archive_symbol_lookup)
4557 (bfd *, struct bfd_link_info *, const char *);
4558
4559 if (! bfd_has_map (abfd))
4560 {
4561 /* An empty archive is a special case. */
4562 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4563 return TRUE;
4564 bfd_set_error (bfd_error_no_armap);
4565 return FALSE;
4566 }
4567
4568 /* Keep track of all symbols we know to be already defined, and all
4569 files we know to be already included. This is to speed up the
4570 second and subsequent passes. */
4571 c = bfd_ardata (abfd)->symdef_count;
4572 if (c == 0)
4573 return TRUE;
4574 amt = c;
4575 amt *= sizeof (bfd_boolean);
4576 defined = bfd_zmalloc (amt);
4577 included = bfd_zmalloc (amt);
4578 if (defined == NULL || included == NULL)
4579 goto error_return;
4580
4581 symdefs = bfd_ardata (abfd)->symdefs;
4582 bed = get_elf_backend_data (abfd);
4583 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
4584
4585 do
4586 {
4587 file_ptr last;
4588 symindex i;
4589 carsym *symdef;
4590 carsym *symdefend;
4591
4592 loop = FALSE;
4593 last = -1;
4594
4595 symdef = symdefs;
4596 symdefend = symdef + c;
4597 for (i = 0; symdef < symdefend; symdef++, i++)
4598 {
4599 struct elf_link_hash_entry *h;
4600 bfd *element;
4601 struct bfd_link_hash_entry *undefs_tail;
4602 symindex mark;
4603
4604 if (defined[i] || included[i])
4605 continue;
4606 if (symdef->file_offset == last)
4607 {
4608 included[i] = TRUE;
4609 continue;
4610 }
4611
4612 h = archive_symbol_lookup (abfd, info, symdef->name);
4613 if (h == (struct elf_link_hash_entry *) 0 - 1)
4614 goto error_return;
4615
4616 if (h == NULL)
4617 continue;
4618
4619 if (h->root.type == bfd_link_hash_common)
4620 {
4621 /* We currently have a common symbol. The archive map contains
4622 a reference to this symbol, so we may want to include it. We
4623 only want to include it however, if this archive element
4624 contains a definition of the symbol, not just another common
4625 declaration of it.
4626
4627 Unfortunately some archivers (including GNU ar) will put
4628 declarations of common symbols into their archive maps, as
4629 well as real definitions, so we cannot just go by the archive
4630 map alone. Instead we must read in the element's symbol
4631 table and check that to see what kind of symbol definition
4632 this is. */
4633 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
4634 continue;
4635 }
4636 else if (h->root.type != bfd_link_hash_undefined)
4637 {
4638 if (h->root.type != bfd_link_hash_undefweak)
4639 defined[i] = TRUE;
4640 continue;
4641 }
4642
4643 /* We need to include this archive member. */
4644 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4645 if (element == NULL)
4646 goto error_return;
4647
4648 if (! bfd_check_format (element, bfd_object))
4649 goto error_return;
4650
4651 /* Doublecheck that we have not included this object
4652 already--it should be impossible, but there may be
4653 something wrong with the archive. */
4654 if (element->archive_pass != 0)
4655 {
4656 bfd_set_error (bfd_error_bad_value);
4657 goto error_return;
4658 }
4659 element->archive_pass = 1;
4660
4661 undefs_tail = info->hash->undefs_tail;
4662
4663 if (! (*info->callbacks->add_archive_element) (info, element,
4664 symdef->name))
4665 goto error_return;
4666 if (! bfd_link_add_symbols (element, info))
4667 goto error_return;
4668
4669 /* If there are any new undefined symbols, we need to make
4670 another pass through the archive in order to see whether
4671 they can be defined. FIXME: This isn't perfect, because
4672 common symbols wind up on undefs_tail and because an
4673 undefined symbol which is defined later on in this pass
4674 does not require another pass. This isn't a bug, but it
4675 does make the code less efficient than it could be. */
4676 if (undefs_tail != info->hash->undefs_tail)
4677 loop = TRUE;
4678
4679 /* Look backward to mark all symbols from this object file
4680 which we have already seen in this pass. */
4681 mark = i;
4682 do
4683 {
4684 included[mark] = TRUE;
4685 if (mark == 0)
4686 break;
4687 --mark;
4688 }
4689 while (symdefs[mark].file_offset == symdef->file_offset);
4690
4691 /* We mark subsequent symbols from this object file as we go
4692 on through the loop. */
4693 last = symdef->file_offset;
4694 }
4695 }
4696 while (loop);
4697
4698 free (defined);
4699 free (included);
4700
4701 return TRUE;
4702
4703 error_return:
4704 if (defined != NULL)
4705 free (defined);
4706 if (included != NULL)
4707 free (included);
4708 return FALSE;
4709 }
4710
4711 /* Given an ELF BFD, add symbols to the global hash table as
4712 appropriate. */
4713
4714 bfd_boolean
4715 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
4716 {
4717 switch (bfd_get_format (abfd))
4718 {
4719 case bfd_object:
4720 return elf_link_add_object_symbols (abfd, info);
4721 case bfd_archive:
4722 return elf_link_add_archive_symbols (abfd, info);
4723 default:
4724 bfd_set_error (bfd_error_wrong_format);
4725 return FALSE;
4726 }
4727 }
4728 \f
4729 /* This function will be called though elf_link_hash_traverse to store
4730 all hash value of the exported symbols in an array. */
4731
4732 static bfd_boolean
4733 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
4734 {
4735 unsigned long **valuep = data;
4736 const char *name;
4737 char *p;
4738 unsigned long ha;
4739 char *alc = NULL;
4740
4741 if (h->root.type == bfd_link_hash_warning)
4742 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4743
4744 /* Ignore indirect symbols. These are added by the versioning code. */
4745 if (h->dynindx == -1)
4746 return TRUE;
4747
4748 name = h->root.root.string;
4749 p = strchr (name, ELF_VER_CHR);
4750 if (p != NULL)
4751 {
4752 alc = bfd_malloc (p - name + 1);
4753 memcpy (alc, name, p - name);
4754 alc[p - name] = '\0';
4755 name = alc;
4756 }
4757
4758 /* Compute the hash value. */
4759 ha = bfd_elf_hash (name);
4760
4761 /* Store the found hash value in the array given as the argument. */
4762 *(*valuep)++ = ha;
4763
4764 /* And store it in the struct so that we can put it in the hash table
4765 later. */
4766 h->u.elf_hash_value = ha;
4767
4768 if (alc != NULL)
4769 free (alc);
4770
4771 return TRUE;
4772 }
4773
4774 /* Array used to determine the number of hash table buckets to use
4775 based on the number of symbols there are. If there are fewer than
4776 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4777 fewer than 37 we use 17 buckets, and so forth. We never use more
4778 than 32771 buckets. */
4779
4780 static const size_t elf_buckets[] =
4781 {
4782 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4783 16411, 32771, 0
4784 };
4785
4786 /* Compute bucket count for hashing table. We do not use a static set
4787 of possible tables sizes anymore. Instead we determine for all
4788 possible reasonable sizes of the table the outcome (i.e., the
4789 number of collisions etc) and choose the best solution. The
4790 weighting functions are not too simple to allow the table to grow
4791 without bounds. Instead one of the weighting factors is the size.
4792 Therefore the result is always a good payoff between few collisions
4793 (= short chain lengths) and table size. */
4794 static size_t
4795 compute_bucket_count (struct bfd_link_info *info)
4796 {
4797 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
4798 size_t best_size = 0;
4799 unsigned long int *hashcodes;
4800 unsigned long int *hashcodesp;
4801 unsigned long int i;
4802 bfd_size_type amt;
4803
4804 /* Compute the hash values for all exported symbols. At the same
4805 time store the values in an array so that we could use them for
4806 optimizations. */
4807 amt = dynsymcount;
4808 amt *= sizeof (unsigned long int);
4809 hashcodes = bfd_malloc (amt);
4810 if (hashcodes == NULL)
4811 return 0;
4812 hashcodesp = hashcodes;
4813
4814 /* Put all hash values in HASHCODES. */
4815 elf_link_hash_traverse (elf_hash_table (info),
4816 elf_collect_hash_codes, &hashcodesp);
4817
4818 /* We have a problem here. The following code to optimize the table
4819 size requires an integer type with more the 32 bits. If
4820 BFD_HOST_U_64_BIT is set we know about such a type. */
4821 #ifdef BFD_HOST_U_64_BIT
4822 if (info->optimize)
4823 {
4824 unsigned long int nsyms = hashcodesp - hashcodes;
4825 size_t minsize;
4826 size_t maxsize;
4827 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
4828 unsigned long int *counts ;
4829 bfd *dynobj = elf_hash_table (info)->dynobj;
4830 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
4831
4832 /* Possible optimization parameters: if we have NSYMS symbols we say
4833 that the hashing table must at least have NSYMS/4 and at most
4834 2*NSYMS buckets. */
4835 minsize = nsyms / 4;
4836 if (minsize == 0)
4837 minsize = 1;
4838 best_size = maxsize = nsyms * 2;
4839
4840 /* Create array where we count the collisions in. We must use bfd_malloc
4841 since the size could be large. */
4842 amt = maxsize;
4843 amt *= sizeof (unsigned long int);
4844 counts = bfd_malloc (amt);
4845 if (counts == NULL)
4846 {
4847 free (hashcodes);
4848 return 0;
4849 }
4850
4851 /* Compute the "optimal" size for the hash table. The criteria is a
4852 minimal chain length. The minor criteria is (of course) the size
4853 of the table. */
4854 for (i = minsize; i < maxsize; ++i)
4855 {
4856 /* Walk through the array of hashcodes and count the collisions. */
4857 BFD_HOST_U_64_BIT max;
4858 unsigned long int j;
4859 unsigned long int fact;
4860
4861 memset (counts, '\0', i * sizeof (unsigned long int));
4862
4863 /* Determine how often each hash bucket is used. */
4864 for (j = 0; j < nsyms; ++j)
4865 ++counts[hashcodes[j] % i];
4866
4867 /* For the weight function we need some information about the
4868 pagesize on the target. This is information need not be 100%
4869 accurate. Since this information is not available (so far) we
4870 define it here to a reasonable default value. If it is crucial
4871 to have a better value some day simply define this value. */
4872 # ifndef BFD_TARGET_PAGESIZE
4873 # define BFD_TARGET_PAGESIZE (4096)
4874 # endif
4875
4876 /* We in any case need 2 + NSYMS entries for the size values and
4877 the chains. */
4878 max = (2 + nsyms) * (bed->s->arch_size / 8);
4879
4880 # if 1
4881 /* Variant 1: optimize for short chains. We add the squares
4882 of all the chain lengths (which favors many small chain
4883 over a few long chains). */
4884 for (j = 0; j < i; ++j)
4885 max += counts[j] * counts[j];
4886
4887 /* This adds penalties for the overall size of the table. */
4888 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4889 max *= fact * fact;
4890 # else
4891 /* Variant 2: Optimize a lot more for small table. Here we
4892 also add squares of the size but we also add penalties for
4893 empty slots (the +1 term). */
4894 for (j = 0; j < i; ++j)
4895 max += (1 + counts[j]) * (1 + counts[j]);
4896
4897 /* The overall size of the table is considered, but not as
4898 strong as in variant 1, where it is squared. */
4899 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4900 max *= fact;
4901 # endif
4902
4903 /* Compare with current best results. */
4904 if (max < best_chlen)
4905 {
4906 best_chlen = max;
4907 best_size = i;
4908 }
4909 }
4910
4911 free (counts);
4912 }
4913 else
4914 #endif /* defined (BFD_HOST_U_64_BIT) */
4915 {
4916 /* This is the fallback solution if no 64bit type is available or if we
4917 are not supposed to spend much time on optimizations. We select the
4918 bucket count using a fixed set of numbers. */
4919 for (i = 0; elf_buckets[i] != 0; i++)
4920 {
4921 best_size = elf_buckets[i];
4922 if (dynsymcount < elf_buckets[i + 1])
4923 break;
4924 }
4925 }
4926
4927 /* Free the arrays we needed. */
4928 free (hashcodes);
4929
4930 return best_size;
4931 }
4932
4933 /* Set up the sizes and contents of the ELF dynamic sections. This is
4934 called by the ELF linker emulation before_allocation routine. We
4935 must set the sizes of the sections before the linker sets the
4936 addresses of the various sections. */
4937
4938 bfd_boolean
4939 bfd_elf_size_dynamic_sections (bfd *output_bfd,
4940 const char *soname,
4941 const char *rpath,
4942 const char *filter_shlib,
4943 const char * const *auxiliary_filters,
4944 struct bfd_link_info *info,
4945 asection **sinterpptr,
4946 struct bfd_elf_version_tree *verdefs)
4947 {
4948 bfd_size_type soname_indx;
4949 bfd *dynobj;
4950 const struct elf_backend_data *bed;
4951 struct elf_assign_sym_version_info asvinfo;
4952
4953 *sinterpptr = NULL;
4954
4955 soname_indx = (bfd_size_type) -1;
4956
4957 if (!is_elf_hash_table (info->hash))
4958 return TRUE;
4959
4960 elf_tdata (output_bfd)->relro = info->relro;
4961 if (info->execstack)
4962 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
4963 else if (info->noexecstack)
4964 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
4965 else
4966 {
4967 bfd *inputobj;
4968 asection *notesec = NULL;
4969 int exec = 0;
4970
4971 for (inputobj = info->input_bfds;
4972 inputobj;
4973 inputobj = inputobj->link_next)
4974 {
4975 asection *s;
4976
4977 if (inputobj->flags & (DYNAMIC | BFD_LINKER_CREATED))
4978 continue;
4979 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
4980 if (s)
4981 {
4982 if (s->flags & SEC_CODE)
4983 exec = PF_X;
4984 notesec = s;
4985 }
4986 else
4987 exec = PF_X;
4988 }
4989 if (notesec)
4990 {
4991 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
4992 if (exec && info->relocatable
4993 && notesec->output_section != bfd_abs_section_ptr)
4994 notesec->output_section->flags |= SEC_CODE;
4995 }
4996 }
4997
4998 /* Any syms created from now on start with -1 in
4999 got.refcount/offset and plt.refcount/offset. */
5000 elf_hash_table (info)->init_got_refcount
5001 = elf_hash_table (info)->init_got_offset;
5002 elf_hash_table (info)->init_plt_refcount
5003 = elf_hash_table (info)->init_plt_offset;
5004
5005 /* The backend may have to create some sections regardless of whether
5006 we're dynamic or not. */
5007 bed = get_elf_backend_data (output_bfd);
5008 if (bed->elf_backend_always_size_sections
5009 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5010 return FALSE;
5011
5012 dynobj = elf_hash_table (info)->dynobj;
5013
5014 /* If there were no dynamic objects in the link, there is nothing to
5015 do here. */
5016 if (dynobj == NULL)
5017 return TRUE;
5018
5019 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
5020 return FALSE;
5021
5022 if (elf_hash_table (info)->dynamic_sections_created)
5023 {
5024 struct elf_info_failed eif;
5025 struct elf_link_hash_entry *h;
5026 asection *dynstr;
5027 struct bfd_elf_version_tree *t;
5028 struct bfd_elf_version_expr *d;
5029 asection *s;
5030 bfd_boolean all_defined;
5031
5032 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
5033 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
5034
5035 if (soname != NULL)
5036 {
5037 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5038 soname, TRUE);
5039 if (soname_indx == (bfd_size_type) -1
5040 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5041 return FALSE;
5042 }
5043
5044 if (info->symbolic)
5045 {
5046 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5047 return FALSE;
5048 info->flags |= DF_SYMBOLIC;
5049 }
5050
5051 if (rpath != NULL)
5052 {
5053 bfd_size_type indx;
5054
5055 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5056 TRUE);
5057 if (indx == (bfd_size_type) -1
5058 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
5059 return FALSE;
5060
5061 if (info->new_dtags)
5062 {
5063 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
5064 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
5065 return FALSE;
5066 }
5067 }
5068
5069 if (filter_shlib != NULL)
5070 {
5071 bfd_size_type indx;
5072
5073 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5074 filter_shlib, TRUE);
5075 if (indx == (bfd_size_type) -1
5076 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5077 return FALSE;
5078 }
5079
5080 if (auxiliary_filters != NULL)
5081 {
5082 const char * const *p;
5083
5084 for (p = auxiliary_filters; *p != NULL; p++)
5085 {
5086 bfd_size_type indx;
5087
5088 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5089 *p, TRUE);
5090 if (indx == (bfd_size_type) -1
5091 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5092 return FALSE;
5093 }
5094 }
5095
5096 eif.info = info;
5097 eif.verdefs = verdefs;
5098 eif.failed = FALSE;
5099
5100 /* If we are supposed to export all symbols into the dynamic symbol
5101 table (this is not the normal case), then do so. */
5102 if (info->export_dynamic)
5103 {
5104 elf_link_hash_traverse (elf_hash_table (info),
5105 _bfd_elf_export_symbol,
5106 &eif);
5107 if (eif.failed)
5108 return FALSE;
5109 }
5110
5111 /* Make all global versions with definition. */
5112 for (t = verdefs; t != NULL; t = t->next)
5113 for (d = t->globals.list; d != NULL; d = d->next)
5114 if (!d->symver && d->symbol)
5115 {
5116 const char *verstr, *name;
5117 size_t namelen, verlen, newlen;
5118 char *newname, *p;
5119 struct elf_link_hash_entry *newh;
5120
5121 name = d->symbol;
5122 namelen = strlen (name);
5123 verstr = t->name;
5124 verlen = strlen (verstr);
5125 newlen = namelen + verlen + 3;
5126
5127 newname = bfd_malloc (newlen);
5128 if (newname == NULL)
5129 return FALSE;
5130 memcpy (newname, name, namelen);
5131
5132 /* Check the hidden versioned definition. */
5133 p = newname + namelen;
5134 *p++ = ELF_VER_CHR;
5135 memcpy (p, verstr, verlen + 1);
5136 newh = elf_link_hash_lookup (elf_hash_table (info),
5137 newname, FALSE, FALSE,
5138 FALSE);
5139 if (newh == NULL
5140 || (newh->root.type != bfd_link_hash_defined
5141 && newh->root.type != bfd_link_hash_defweak))
5142 {
5143 /* Check the default versioned definition. */
5144 *p++ = ELF_VER_CHR;
5145 memcpy (p, verstr, verlen + 1);
5146 newh = elf_link_hash_lookup (elf_hash_table (info),
5147 newname, FALSE, FALSE,
5148 FALSE);
5149 }
5150 free (newname);
5151
5152 /* Mark this version if there is a definition and it is
5153 not defined in a shared object. */
5154 if (newh != NULL
5155 && !newh->def_dynamic
5156 && (newh->root.type == bfd_link_hash_defined
5157 || newh->root.type == bfd_link_hash_defweak))
5158 d->symver = 1;
5159 }
5160
5161 /* Attach all the symbols to their version information. */
5162 asvinfo.output_bfd = output_bfd;
5163 asvinfo.info = info;
5164 asvinfo.verdefs = verdefs;
5165 asvinfo.failed = FALSE;
5166
5167 elf_link_hash_traverse (elf_hash_table (info),
5168 _bfd_elf_link_assign_sym_version,
5169 &asvinfo);
5170 if (asvinfo.failed)
5171 return FALSE;
5172
5173 if (!info->allow_undefined_version)
5174 {
5175 /* Check if all global versions have a definition. */
5176 all_defined = TRUE;
5177 for (t = verdefs; t != NULL; t = t->next)
5178 for (d = t->globals.list; d != NULL; d = d->next)
5179 if (!d->symver && !d->script)
5180 {
5181 (*_bfd_error_handler)
5182 (_("%s: undefined version: %s"),
5183 d->pattern, t->name);
5184 all_defined = FALSE;
5185 }
5186
5187 if (!all_defined)
5188 {
5189 bfd_set_error (bfd_error_bad_value);
5190 return FALSE;
5191 }
5192 }
5193
5194 /* Find all symbols which were defined in a dynamic object and make
5195 the backend pick a reasonable value for them. */
5196 elf_link_hash_traverse (elf_hash_table (info),
5197 _bfd_elf_adjust_dynamic_symbol,
5198 &eif);
5199 if (eif.failed)
5200 return FALSE;
5201
5202 /* Add some entries to the .dynamic section. We fill in some of the
5203 values later, in bfd_elf_final_link, but we must add the entries
5204 now so that we know the final size of the .dynamic section. */
5205
5206 /* If there are initialization and/or finalization functions to
5207 call then add the corresponding DT_INIT/DT_FINI entries. */
5208 h = (info->init_function
5209 ? elf_link_hash_lookup (elf_hash_table (info),
5210 info->init_function, FALSE,
5211 FALSE, FALSE)
5212 : NULL);
5213 if (h != NULL
5214 && (h->ref_regular
5215 || h->def_regular))
5216 {
5217 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5218 return FALSE;
5219 }
5220 h = (info->fini_function
5221 ? elf_link_hash_lookup (elf_hash_table (info),
5222 info->fini_function, FALSE,
5223 FALSE, FALSE)
5224 : NULL);
5225 if (h != NULL
5226 && (h->ref_regular
5227 || h->def_regular))
5228 {
5229 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5230 return FALSE;
5231 }
5232
5233 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5234 if (s != NULL && s->linker_has_input)
5235 {
5236 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5237 if (! info->executable)
5238 {
5239 bfd *sub;
5240 asection *o;
5241
5242 for (sub = info->input_bfds; sub != NULL;
5243 sub = sub->link_next)
5244 for (o = sub->sections; o != NULL; o = o->next)
5245 if (elf_section_data (o)->this_hdr.sh_type
5246 == SHT_PREINIT_ARRAY)
5247 {
5248 (*_bfd_error_handler)
5249 (_("%B: .preinit_array section is not allowed in DSO"),
5250 sub);
5251 break;
5252 }
5253
5254 bfd_set_error (bfd_error_nonrepresentable_section);
5255 return FALSE;
5256 }
5257
5258 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5259 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5260 return FALSE;
5261 }
5262 s = bfd_get_section_by_name (output_bfd, ".init_array");
5263 if (s != NULL && s->linker_has_input)
5264 {
5265 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5266 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5267 return FALSE;
5268 }
5269 s = bfd_get_section_by_name (output_bfd, ".fini_array");
5270 if (s != NULL && s->linker_has_input)
5271 {
5272 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5273 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5274 return FALSE;
5275 }
5276
5277 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
5278 /* If .dynstr is excluded from the link, we don't want any of
5279 these tags. Strictly, we should be checking each section
5280 individually; This quick check covers for the case where
5281 someone does a /DISCARD/ : { *(*) }. */
5282 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5283 {
5284 bfd_size_type strsize;
5285
5286 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5287 if (!_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)
5288 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5289 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5290 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5291 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5292 bed->s->sizeof_sym))
5293 return FALSE;
5294 }
5295 }
5296
5297 /* The backend must work out the sizes of all the other dynamic
5298 sections. */
5299 if (bed->elf_backend_size_dynamic_sections
5300 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5301 return FALSE;
5302
5303 if (elf_hash_table (info)->dynamic_sections_created)
5304 {
5305 unsigned long section_sym_count;
5306 asection *s;
5307
5308 /* Set up the version definition section. */
5309 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5310 BFD_ASSERT (s != NULL);
5311
5312 /* We may have created additional version definitions if we are
5313 just linking a regular application. */
5314 verdefs = asvinfo.verdefs;
5315
5316 /* Skip anonymous version tag. */
5317 if (verdefs != NULL && verdefs->vernum == 0)
5318 verdefs = verdefs->next;
5319
5320 if (verdefs == NULL && !info->create_default_symver)
5321 s->flags |= SEC_EXCLUDE;
5322 else
5323 {
5324 unsigned int cdefs;
5325 bfd_size_type size;
5326 struct bfd_elf_version_tree *t;
5327 bfd_byte *p;
5328 Elf_Internal_Verdef def;
5329 Elf_Internal_Verdaux defaux;
5330 struct bfd_link_hash_entry *bh;
5331 struct elf_link_hash_entry *h;
5332 const char *name;
5333
5334 cdefs = 0;
5335 size = 0;
5336
5337 /* Make space for the base version. */
5338 size += sizeof (Elf_External_Verdef);
5339 size += sizeof (Elf_External_Verdaux);
5340 ++cdefs;
5341
5342 /* Make space for the default version. */
5343 if (info->create_default_symver)
5344 {
5345 size += sizeof (Elf_External_Verdef);
5346 ++cdefs;
5347 }
5348
5349 for (t = verdefs; t != NULL; t = t->next)
5350 {
5351 struct bfd_elf_version_deps *n;
5352
5353 size += sizeof (Elf_External_Verdef);
5354 size += sizeof (Elf_External_Verdaux);
5355 ++cdefs;
5356
5357 for (n = t->deps; n != NULL; n = n->next)
5358 size += sizeof (Elf_External_Verdaux);
5359 }
5360
5361 s->size = size;
5362 s->contents = bfd_alloc (output_bfd, s->size);
5363 if (s->contents == NULL && s->size != 0)
5364 return FALSE;
5365
5366 /* Fill in the version definition section. */
5367
5368 p = s->contents;
5369
5370 def.vd_version = VER_DEF_CURRENT;
5371 def.vd_flags = VER_FLG_BASE;
5372 def.vd_ndx = 1;
5373 def.vd_cnt = 1;
5374 if (info->create_default_symver)
5375 {
5376 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
5377 def.vd_next = sizeof (Elf_External_Verdef);
5378 }
5379 else
5380 {
5381 def.vd_aux = sizeof (Elf_External_Verdef);
5382 def.vd_next = (sizeof (Elf_External_Verdef)
5383 + sizeof (Elf_External_Verdaux));
5384 }
5385
5386 if (soname_indx != (bfd_size_type) -1)
5387 {
5388 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5389 soname_indx);
5390 def.vd_hash = bfd_elf_hash (soname);
5391 defaux.vda_name = soname_indx;
5392 name = soname;
5393 }
5394 else
5395 {
5396 bfd_size_type indx;
5397
5398 name = lbasename (output_bfd->filename);
5399 def.vd_hash = bfd_elf_hash (name);
5400 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5401 name, FALSE);
5402 if (indx == (bfd_size_type) -1)
5403 return FALSE;
5404 defaux.vda_name = indx;
5405 }
5406 defaux.vda_next = 0;
5407
5408 _bfd_elf_swap_verdef_out (output_bfd, &def,
5409 (Elf_External_Verdef *) p);
5410 p += sizeof (Elf_External_Verdef);
5411 if (info->create_default_symver)
5412 {
5413 /* Add a symbol representing this version. */
5414 bh = NULL;
5415 if (! (_bfd_generic_link_add_one_symbol
5416 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
5417 0, NULL, FALSE,
5418 get_elf_backend_data (dynobj)->collect, &bh)))
5419 return FALSE;
5420 h = (struct elf_link_hash_entry *) bh;
5421 h->non_elf = 0;
5422 h->def_regular = 1;
5423 h->type = STT_OBJECT;
5424 h->verinfo.vertree = NULL;
5425
5426 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5427 return FALSE;
5428
5429 /* Create a duplicate of the base version with the same
5430 aux block, but different flags. */
5431 def.vd_flags = 0;
5432 def.vd_ndx = 2;
5433 def.vd_aux = sizeof (Elf_External_Verdef);
5434 if (verdefs)
5435 def.vd_next = (sizeof (Elf_External_Verdef)
5436 + sizeof (Elf_External_Verdaux));
5437 else
5438 def.vd_next = 0;
5439 _bfd_elf_swap_verdef_out (output_bfd, &def,
5440 (Elf_External_Verdef *) p);
5441 p += sizeof (Elf_External_Verdef);
5442 }
5443 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5444 (Elf_External_Verdaux *) p);
5445 p += sizeof (Elf_External_Verdaux);
5446
5447 for (t = verdefs; t != NULL; t = t->next)
5448 {
5449 unsigned int cdeps;
5450 struct bfd_elf_version_deps *n;
5451
5452 cdeps = 0;
5453 for (n = t->deps; n != NULL; n = n->next)
5454 ++cdeps;
5455
5456 /* Add a symbol representing this version. */
5457 bh = NULL;
5458 if (! (_bfd_generic_link_add_one_symbol
5459 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5460 0, NULL, FALSE,
5461 get_elf_backend_data (dynobj)->collect, &bh)))
5462 return FALSE;
5463 h = (struct elf_link_hash_entry *) bh;
5464 h->non_elf = 0;
5465 h->def_regular = 1;
5466 h->type = STT_OBJECT;
5467 h->verinfo.vertree = t;
5468
5469 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5470 return FALSE;
5471
5472 def.vd_version = VER_DEF_CURRENT;
5473 def.vd_flags = 0;
5474 if (t->globals.list == NULL
5475 && t->locals.list == NULL
5476 && ! t->used)
5477 def.vd_flags |= VER_FLG_WEAK;
5478 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5479 def.vd_cnt = cdeps + 1;
5480 def.vd_hash = bfd_elf_hash (t->name);
5481 def.vd_aux = sizeof (Elf_External_Verdef);
5482 def.vd_next = 0;
5483 if (t->next != NULL)
5484 def.vd_next = (sizeof (Elf_External_Verdef)
5485 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5486
5487 _bfd_elf_swap_verdef_out (output_bfd, &def,
5488 (Elf_External_Verdef *) p);
5489 p += sizeof (Elf_External_Verdef);
5490
5491 defaux.vda_name = h->dynstr_index;
5492 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5493 h->dynstr_index);
5494 defaux.vda_next = 0;
5495 if (t->deps != NULL)
5496 defaux.vda_next = sizeof (Elf_External_Verdaux);
5497 t->name_indx = defaux.vda_name;
5498
5499 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5500 (Elf_External_Verdaux *) p);
5501 p += sizeof (Elf_External_Verdaux);
5502
5503 for (n = t->deps; n != NULL; n = n->next)
5504 {
5505 if (n->version_needed == NULL)
5506 {
5507 /* This can happen if there was an error in the
5508 version script. */
5509 defaux.vda_name = 0;
5510 }
5511 else
5512 {
5513 defaux.vda_name = n->version_needed->name_indx;
5514 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5515 defaux.vda_name);
5516 }
5517 if (n->next == NULL)
5518 defaux.vda_next = 0;
5519 else
5520 defaux.vda_next = sizeof (Elf_External_Verdaux);
5521
5522 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5523 (Elf_External_Verdaux *) p);
5524 p += sizeof (Elf_External_Verdaux);
5525 }
5526 }
5527
5528 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
5529 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
5530 return FALSE;
5531
5532 elf_tdata (output_bfd)->cverdefs = cdefs;
5533 }
5534
5535 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
5536 {
5537 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
5538 return FALSE;
5539 }
5540 else if (info->flags & DF_BIND_NOW)
5541 {
5542 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
5543 return FALSE;
5544 }
5545
5546 if (info->flags_1)
5547 {
5548 if (info->executable)
5549 info->flags_1 &= ~ (DF_1_INITFIRST
5550 | DF_1_NODELETE
5551 | DF_1_NOOPEN);
5552 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
5553 return FALSE;
5554 }
5555
5556 /* Work out the size of the version reference section. */
5557
5558 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
5559 BFD_ASSERT (s != NULL);
5560 {
5561 struct elf_find_verdep_info sinfo;
5562
5563 sinfo.output_bfd = output_bfd;
5564 sinfo.info = info;
5565 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
5566 if (sinfo.vers == 0)
5567 sinfo.vers = 1;
5568 sinfo.failed = FALSE;
5569
5570 elf_link_hash_traverse (elf_hash_table (info),
5571 _bfd_elf_link_find_version_dependencies,
5572 &sinfo);
5573
5574 if (elf_tdata (output_bfd)->verref == NULL)
5575 s->flags |= SEC_EXCLUDE;
5576 else
5577 {
5578 Elf_Internal_Verneed *t;
5579 unsigned int size;
5580 unsigned int crefs;
5581 bfd_byte *p;
5582
5583 /* Build the version definition section. */
5584 size = 0;
5585 crefs = 0;
5586 for (t = elf_tdata (output_bfd)->verref;
5587 t != NULL;
5588 t = t->vn_nextref)
5589 {
5590 Elf_Internal_Vernaux *a;
5591
5592 size += sizeof (Elf_External_Verneed);
5593 ++crefs;
5594 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5595 size += sizeof (Elf_External_Vernaux);
5596 }
5597
5598 s->size = size;
5599 s->contents = bfd_alloc (output_bfd, s->size);
5600 if (s->contents == NULL)
5601 return FALSE;
5602
5603 p = s->contents;
5604 for (t = elf_tdata (output_bfd)->verref;
5605 t != NULL;
5606 t = t->vn_nextref)
5607 {
5608 unsigned int caux;
5609 Elf_Internal_Vernaux *a;
5610 bfd_size_type indx;
5611
5612 caux = 0;
5613 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5614 ++caux;
5615
5616 t->vn_version = VER_NEED_CURRENT;
5617 t->vn_cnt = caux;
5618 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5619 elf_dt_name (t->vn_bfd) != NULL
5620 ? elf_dt_name (t->vn_bfd)
5621 : lbasename (t->vn_bfd->filename),
5622 FALSE);
5623 if (indx == (bfd_size_type) -1)
5624 return FALSE;
5625 t->vn_file = indx;
5626 t->vn_aux = sizeof (Elf_External_Verneed);
5627 if (t->vn_nextref == NULL)
5628 t->vn_next = 0;
5629 else
5630 t->vn_next = (sizeof (Elf_External_Verneed)
5631 + caux * sizeof (Elf_External_Vernaux));
5632
5633 _bfd_elf_swap_verneed_out (output_bfd, t,
5634 (Elf_External_Verneed *) p);
5635 p += sizeof (Elf_External_Verneed);
5636
5637 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5638 {
5639 a->vna_hash = bfd_elf_hash (a->vna_nodename);
5640 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5641 a->vna_nodename, FALSE);
5642 if (indx == (bfd_size_type) -1)
5643 return FALSE;
5644 a->vna_name = indx;
5645 if (a->vna_nextptr == NULL)
5646 a->vna_next = 0;
5647 else
5648 a->vna_next = sizeof (Elf_External_Vernaux);
5649
5650 _bfd_elf_swap_vernaux_out (output_bfd, a,
5651 (Elf_External_Vernaux *) p);
5652 p += sizeof (Elf_External_Vernaux);
5653 }
5654 }
5655
5656 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
5657 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
5658 return FALSE;
5659
5660 elf_tdata (output_bfd)->cverrefs = crefs;
5661 }
5662 }
5663
5664 if ((elf_tdata (output_bfd)->cverrefs == 0
5665 && elf_tdata (output_bfd)->cverdefs == 0)
5666 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
5667 &section_sym_count) == 0)
5668 {
5669 s = bfd_get_section_by_name (dynobj, ".gnu.version");
5670 s->flags |= SEC_EXCLUDE;
5671 }
5672 }
5673 return TRUE;
5674 }
5675
5676 bfd_boolean
5677 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5678 {
5679 if (!is_elf_hash_table (info->hash))
5680 return TRUE;
5681
5682 if (elf_hash_table (info)->dynamic_sections_created)
5683 {
5684 bfd *dynobj;
5685 const struct elf_backend_data *bed;
5686 asection *s;
5687 bfd_size_type dynsymcount;
5688 unsigned long section_sym_count;
5689 size_t bucketcount = 0;
5690 size_t hash_entry_size;
5691 unsigned int dtagcount;
5692
5693 dynobj = elf_hash_table (info)->dynobj;
5694
5695 /* Assign dynsym indicies. In a shared library we generate a
5696 section symbol for each output section, which come first.
5697 Next come all of the back-end allocated local dynamic syms,
5698 followed by the rest of the global symbols. */
5699
5700 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
5701 &section_sym_count);
5702
5703 /* Work out the size of the symbol version section. */
5704 s = bfd_get_section_by_name (dynobj, ".gnu.version");
5705 BFD_ASSERT (s != NULL);
5706 if (dynsymcount != 0
5707 && (s->flags & SEC_EXCLUDE) == 0)
5708 {
5709 s->size = dynsymcount * sizeof (Elf_External_Versym);
5710 s->contents = bfd_zalloc (output_bfd, s->size);
5711 if (s->contents == NULL)
5712 return FALSE;
5713
5714 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
5715 return FALSE;
5716 }
5717
5718 /* Set the size of the .dynsym and .hash sections. We counted
5719 the number of dynamic symbols in elf_link_add_object_symbols.
5720 We will build the contents of .dynsym and .hash when we build
5721 the final symbol table, because until then we do not know the
5722 correct value to give the symbols. We built the .dynstr
5723 section as we went along in elf_link_add_object_symbols. */
5724 s = bfd_get_section_by_name (dynobj, ".dynsym");
5725 BFD_ASSERT (s != NULL);
5726 bed = get_elf_backend_data (output_bfd);
5727 s->size = dynsymcount * bed->s->sizeof_sym;
5728
5729 if (dynsymcount != 0)
5730 {
5731 s->contents = bfd_alloc (output_bfd, s->size);
5732 if (s->contents == NULL)
5733 return FALSE;
5734
5735 /* The first entry in .dynsym is a dummy symbol.
5736 Clear all the section syms, in case we don't output them all. */
5737 ++section_sym_count;
5738 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5739 }
5740
5741 /* Compute the size of the hashing table. As a side effect this
5742 computes the hash values for all the names we export. */
5743 bucketcount = compute_bucket_count (info);
5744
5745 s = bfd_get_section_by_name (dynobj, ".hash");
5746 BFD_ASSERT (s != NULL);
5747 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
5748 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
5749 s->contents = bfd_zalloc (output_bfd, s->size);
5750 if (s->contents == NULL)
5751 return FALSE;
5752
5753 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
5754 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
5755 s->contents + hash_entry_size);
5756
5757 elf_hash_table (info)->bucketcount = bucketcount;
5758
5759 s = bfd_get_section_by_name (dynobj, ".dynstr");
5760 BFD_ASSERT (s != NULL);
5761
5762 elf_finalize_dynstr (output_bfd, info);
5763
5764 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5765
5766 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
5767 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
5768 return FALSE;
5769 }
5770
5771 return TRUE;
5772 }
5773
5774 /* Final phase of ELF linker. */
5775
5776 /* A structure we use to avoid passing large numbers of arguments. */
5777
5778 struct elf_final_link_info
5779 {
5780 /* General link information. */
5781 struct bfd_link_info *info;
5782 /* Output BFD. */
5783 bfd *output_bfd;
5784 /* Symbol string table. */
5785 struct bfd_strtab_hash *symstrtab;
5786 /* .dynsym section. */
5787 asection *dynsym_sec;
5788 /* .hash section. */
5789 asection *hash_sec;
5790 /* symbol version section (.gnu.version). */
5791 asection *symver_sec;
5792 /* Buffer large enough to hold contents of any section. */
5793 bfd_byte *contents;
5794 /* Buffer large enough to hold external relocs of any section. */
5795 void *external_relocs;
5796 /* Buffer large enough to hold internal relocs of any section. */
5797 Elf_Internal_Rela *internal_relocs;
5798 /* Buffer large enough to hold external local symbols of any input
5799 BFD. */
5800 bfd_byte *external_syms;
5801 /* And a buffer for symbol section indices. */
5802 Elf_External_Sym_Shndx *locsym_shndx;
5803 /* Buffer large enough to hold internal local symbols of any input
5804 BFD. */
5805 Elf_Internal_Sym *internal_syms;
5806 /* Array large enough to hold a symbol index for each local symbol
5807 of any input BFD. */
5808 long *indices;
5809 /* Array large enough to hold a section pointer for each local
5810 symbol of any input BFD. */
5811 asection **sections;
5812 /* Buffer to hold swapped out symbols. */
5813 bfd_byte *symbuf;
5814 /* And one for symbol section indices. */
5815 Elf_External_Sym_Shndx *symshndxbuf;
5816 /* Number of swapped out symbols in buffer. */
5817 size_t symbuf_count;
5818 /* Number of symbols which fit in symbuf. */
5819 size_t symbuf_size;
5820 /* And same for symshndxbuf. */
5821 size_t shndxbuf_size;
5822 };
5823
5824 /* This struct is used to pass information to elf_link_output_extsym. */
5825
5826 struct elf_outext_info
5827 {
5828 bfd_boolean failed;
5829 bfd_boolean localsyms;
5830 struct elf_final_link_info *finfo;
5831 };
5832
5833 /* When performing a relocatable link, the input relocations are
5834 preserved. But, if they reference global symbols, the indices
5835 referenced must be updated. Update all the relocations in
5836 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5837
5838 static void
5839 elf_link_adjust_relocs (bfd *abfd,
5840 Elf_Internal_Shdr *rel_hdr,
5841 unsigned int count,
5842 struct elf_link_hash_entry **rel_hash)
5843 {
5844 unsigned int i;
5845 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5846 bfd_byte *erela;
5847 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5848 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5849 bfd_vma r_type_mask;
5850 int r_sym_shift;
5851
5852 if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
5853 {
5854 swap_in = bed->s->swap_reloc_in;
5855 swap_out = bed->s->swap_reloc_out;
5856 }
5857 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
5858 {
5859 swap_in = bed->s->swap_reloca_in;
5860 swap_out = bed->s->swap_reloca_out;
5861 }
5862 else
5863 abort ();
5864
5865 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
5866 abort ();
5867
5868 if (bed->s->arch_size == 32)
5869 {
5870 r_type_mask = 0xff;
5871 r_sym_shift = 8;
5872 }
5873 else
5874 {
5875 r_type_mask = 0xffffffff;
5876 r_sym_shift = 32;
5877 }
5878
5879 erela = rel_hdr->contents;
5880 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
5881 {
5882 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
5883 unsigned int j;
5884
5885 if (*rel_hash == NULL)
5886 continue;
5887
5888 BFD_ASSERT ((*rel_hash)->indx >= 0);
5889
5890 (*swap_in) (abfd, erela, irela);
5891 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
5892 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
5893 | (irela[j].r_info & r_type_mask));
5894 (*swap_out) (abfd, irela, erela);
5895 }
5896 }
5897
5898 struct elf_link_sort_rela
5899 {
5900 union {
5901 bfd_vma offset;
5902 bfd_vma sym_mask;
5903 } u;
5904 enum elf_reloc_type_class type;
5905 /* We use this as an array of size int_rels_per_ext_rel. */
5906 Elf_Internal_Rela rela[1];
5907 };
5908
5909 static int
5910 elf_link_sort_cmp1 (const void *A, const void *B)
5911 {
5912 const struct elf_link_sort_rela *a = A;
5913 const struct elf_link_sort_rela *b = B;
5914 int relativea, relativeb;
5915
5916 relativea = a->type == reloc_class_relative;
5917 relativeb = b->type == reloc_class_relative;
5918
5919 if (relativea < relativeb)
5920 return 1;
5921 if (relativea > relativeb)
5922 return -1;
5923 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
5924 return -1;
5925 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
5926 return 1;
5927 if (a->rela->r_offset < b->rela->r_offset)
5928 return -1;
5929 if (a->rela->r_offset > b->rela->r_offset)
5930 return 1;
5931 return 0;
5932 }
5933
5934 static int
5935 elf_link_sort_cmp2 (const void *A, const void *B)
5936 {
5937 const struct elf_link_sort_rela *a = A;
5938 const struct elf_link_sort_rela *b = B;
5939 int copya, copyb;
5940
5941 if (a->u.offset < b->u.offset)
5942 return -1;
5943 if (a->u.offset > b->u.offset)
5944 return 1;
5945 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
5946 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
5947 if (copya < copyb)
5948 return -1;
5949 if (copya > copyb)
5950 return 1;
5951 if (a->rela->r_offset < b->rela->r_offset)
5952 return -1;
5953 if (a->rela->r_offset > b->rela->r_offset)
5954 return 1;
5955 return 0;
5956 }
5957
5958 static size_t
5959 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
5960 {
5961 asection *reldyn;
5962 bfd_size_type count, size;
5963 size_t i, ret, sort_elt, ext_size;
5964 bfd_byte *sort, *s_non_relative, *p;
5965 struct elf_link_sort_rela *sq;
5966 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5967 int i2e = bed->s->int_rels_per_ext_rel;
5968 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5969 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5970 struct bfd_link_order *lo;
5971 bfd_vma r_sym_mask;
5972
5973 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
5974 if (reldyn == NULL || reldyn->size == 0)
5975 {
5976 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
5977 if (reldyn == NULL || reldyn->size == 0)
5978 return 0;
5979 ext_size = bed->s->sizeof_rel;
5980 swap_in = bed->s->swap_reloc_in;
5981 swap_out = bed->s->swap_reloc_out;
5982 }
5983 else
5984 {
5985 ext_size = bed->s->sizeof_rela;
5986 swap_in = bed->s->swap_reloca_in;
5987 swap_out = bed->s->swap_reloca_out;
5988 }
5989 count = reldyn->size / ext_size;
5990
5991 size = 0;
5992 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
5993 if (lo->type == bfd_indirect_link_order)
5994 {
5995 asection *o = lo->u.indirect.section;
5996 size += o->size;
5997 }
5998
5999 if (size != reldyn->size)
6000 return 0;
6001
6002 sort_elt = (sizeof (struct elf_link_sort_rela)
6003 + (i2e - 1) * sizeof (Elf_Internal_Rela));
6004 sort = bfd_zmalloc (sort_elt * count);
6005 if (sort == NULL)
6006 {
6007 (*info->callbacks->warning)
6008 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
6009 return 0;
6010 }
6011
6012 if (bed->s->arch_size == 32)
6013 r_sym_mask = ~(bfd_vma) 0xff;
6014 else
6015 r_sym_mask = ~(bfd_vma) 0xffffffff;
6016
6017 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
6018 if (lo->type == bfd_indirect_link_order)
6019 {
6020 bfd_byte *erel, *erelend;
6021 asection *o = lo->u.indirect.section;
6022
6023 if (o->contents == NULL && o->size != 0)
6024 {
6025 /* This is a reloc section that is being handled as a normal
6026 section. See bfd_section_from_shdr. We can't combine
6027 relocs in this case. */
6028 free (sort);
6029 return 0;
6030 }
6031 erel = o->contents;
6032 erelend = o->contents + o->size;
6033 p = sort + o->output_offset / ext_size * sort_elt;
6034 while (erel < erelend)
6035 {
6036 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6037 (*swap_in) (abfd, erel, s->rela);
6038 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
6039 s->u.sym_mask = r_sym_mask;
6040 p += sort_elt;
6041 erel += ext_size;
6042 }
6043 }
6044
6045 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
6046
6047 for (i = 0, p = sort; i < count; i++, p += sort_elt)
6048 {
6049 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6050 if (s->type != reloc_class_relative)
6051 break;
6052 }
6053 ret = i;
6054 s_non_relative = p;
6055
6056 sq = (struct elf_link_sort_rela *) s_non_relative;
6057 for (; i < count; i++, p += sort_elt)
6058 {
6059 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
6060 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
6061 sq = sp;
6062 sp->u.offset = sq->rela->r_offset;
6063 }
6064
6065 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
6066
6067 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
6068 if (lo->type == bfd_indirect_link_order)
6069 {
6070 bfd_byte *erel, *erelend;
6071 asection *o = lo->u.indirect.section;
6072
6073 erel = o->contents;
6074 erelend = o->contents + o->size;
6075 p = sort + o->output_offset / ext_size * sort_elt;
6076 while (erel < erelend)
6077 {
6078 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6079 (*swap_out) (abfd, s->rela, erel);
6080 p += sort_elt;
6081 erel += ext_size;
6082 }
6083 }
6084
6085 free (sort);
6086 *psec = reldyn;
6087 return ret;
6088 }
6089
6090 /* Flush the output symbols to the file. */
6091
6092 static bfd_boolean
6093 elf_link_flush_output_syms (struct elf_final_link_info *finfo,
6094 const struct elf_backend_data *bed)
6095 {
6096 if (finfo->symbuf_count > 0)
6097 {
6098 Elf_Internal_Shdr *hdr;
6099 file_ptr pos;
6100 bfd_size_type amt;
6101
6102 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
6103 pos = hdr->sh_offset + hdr->sh_size;
6104 amt = finfo->symbuf_count * bed->s->sizeof_sym;
6105 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
6106 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
6107 return FALSE;
6108
6109 hdr->sh_size += amt;
6110 finfo->symbuf_count = 0;
6111 }
6112
6113 return TRUE;
6114 }
6115
6116 /* Add a symbol to the output symbol table. */
6117
6118 static bfd_boolean
6119 elf_link_output_sym (struct elf_final_link_info *finfo,
6120 const char *name,
6121 Elf_Internal_Sym *elfsym,
6122 asection *input_sec,
6123 struct elf_link_hash_entry *h)
6124 {
6125 bfd_byte *dest;
6126 Elf_External_Sym_Shndx *destshndx;
6127 bfd_boolean (*output_symbol_hook)
6128 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
6129 struct elf_link_hash_entry *);
6130 const struct elf_backend_data *bed;
6131
6132 bed = get_elf_backend_data (finfo->output_bfd);
6133 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
6134 if (output_symbol_hook != NULL)
6135 {
6136 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
6137 return FALSE;
6138 }
6139
6140 if (name == NULL || *name == '\0')
6141 elfsym->st_name = 0;
6142 else if (input_sec->flags & SEC_EXCLUDE)
6143 elfsym->st_name = 0;
6144 else
6145 {
6146 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
6147 name, TRUE, FALSE);
6148 if (elfsym->st_name == (unsigned long) -1)
6149 return FALSE;
6150 }
6151
6152 if (finfo->symbuf_count >= finfo->symbuf_size)
6153 {
6154 if (! elf_link_flush_output_syms (finfo, bed))
6155 return FALSE;
6156 }
6157
6158 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
6159 destshndx = finfo->symshndxbuf;
6160 if (destshndx != NULL)
6161 {
6162 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
6163 {
6164 bfd_size_type amt;
6165
6166 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
6167 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
6168 if (destshndx == NULL)
6169 return FALSE;
6170 memset ((char *) destshndx + amt, 0, amt);
6171 finfo->shndxbuf_size *= 2;
6172 }
6173 destshndx += bfd_get_symcount (finfo->output_bfd);
6174 }
6175
6176 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
6177 finfo->symbuf_count += 1;
6178 bfd_get_symcount (finfo->output_bfd) += 1;
6179
6180 return TRUE;
6181 }
6182
6183 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
6184 allowing an unsatisfied unversioned symbol in the DSO to match a
6185 versioned symbol that would normally require an explicit version.
6186 We also handle the case that a DSO references a hidden symbol
6187 which may be satisfied by a versioned symbol in another DSO. */
6188
6189 static bfd_boolean
6190 elf_link_check_versioned_symbol (struct bfd_link_info *info,
6191 const struct elf_backend_data *bed,
6192 struct elf_link_hash_entry *h)
6193 {
6194 bfd *abfd;
6195 struct elf_link_loaded_list *loaded;
6196
6197 if (!is_elf_hash_table (info->hash))
6198 return FALSE;
6199
6200 switch (h->root.type)
6201 {
6202 default:
6203 abfd = NULL;
6204 break;
6205
6206 case bfd_link_hash_undefined:
6207 case bfd_link_hash_undefweak:
6208 abfd = h->root.u.undef.abfd;
6209 if ((abfd->flags & DYNAMIC) == 0
6210 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
6211 return FALSE;
6212 break;
6213
6214 case bfd_link_hash_defined:
6215 case bfd_link_hash_defweak:
6216 abfd = h->root.u.def.section->owner;
6217 break;
6218
6219 case bfd_link_hash_common:
6220 abfd = h->root.u.c.p->section->owner;
6221 break;
6222 }
6223 BFD_ASSERT (abfd != NULL);
6224
6225 for (loaded = elf_hash_table (info)->loaded;
6226 loaded != NULL;
6227 loaded = loaded->next)
6228 {
6229 bfd *input;
6230 Elf_Internal_Shdr *hdr;
6231 bfd_size_type symcount;
6232 bfd_size_type extsymcount;
6233 bfd_size_type extsymoff;
6234 Elf_Internal_Shdr *versymhdr;
6235 Elf_Internal_Sym *isym;
6236 Elf_Internal_Sym *isymend;
6237 Elf_Internal_Sym *isymbuf;
6238 Elf_External_Versym *ever;
6239 Elf_External_Versym *extversym;
6240
6241 input = loaded->abfd;
6242
6243 /* We check each DSO for a possible hidden versioned definition. */
6244 if (input == abfd
6245 || (input->flags & DYNAMIC) == 0
6246 || elf_dynversym (input) == 0)
6247 continue;
6248
6249 hdr = &elf_tdata (input)->dynsymtab_hdr;
6250
6251 symcount = hdr->sh_size / bed->s->sizeof_sym;
6252 if (elf_bad_symtab (input))
6253 {
6254 extsymcount = symcount;
6255 extsymoff = 0;
6256 }
6257 else
6258 {
6259 extsymcount = symcount - hdr->sh_info;
6260 extsymoff = hdr->sh_info;
6261 }
6262
6263 if (extsymcount == 0)
6264 continue;
6265
6266 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
6267 NULL, NULL, NULL);
6268 if (isymbuf == NULL)
6269 return FALSE;
6270
6271 /* Read in any version definitions. */
6272 versymhdr = &elf_tdata (input)->dynversym_hdr;
6273 extversym = bfd_malloc (versymhdr->sh_size);
6274 if (extversym == NULL)
6275 goto error_ret;
6276
6277 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
6278 || (bfd_bread (extversym, versymhdr->sh_size, input)
6279 != versymhdr->sh_size))
6280 {
6281 free (extversym);
6282 error_ret:
6283 free (isymbuf);
6284 return FALSE;
6285 }
6286
6287 ever = extversym + extsymoff;
6288 isymend = isymbuf + extsymcount;
6289 for (isym = isymbuf; isym < isymend; isym++, ever++)
6290 {
6291 const char *name;
6292 Elf_Internal_Versym iver;
6293 unsigned short version_index;
6294
6295 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
6296 || isym->st_shndx == SHN_UNDEF)
6297 continue;
6298
6299 name = bfd_elf_string_from_elf_section (input,
6300 hdr->sh_link,
6301 isym->st_name);
6302 if (strcmp (name, h->root.root.string) != 0)
6303 continue;
6304
6305 _bfd_elf_swap_versym_in (input, ever, &iver);
6306
6307 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
6308 {
6309 /* If we have a non-hidden versioned sym, then it should
6310 have provided a definition for the undefined sym. */
6311 abort ();
6312 }
6313
6314 version_index = iver.vs_vers & VERSYM_VERSION;
6315 if (version_index == 1 || version_index == 2)
6316 {
6317 /* This is the base or first version. We can use it. */
6318 free (extversym);
6319 free (isymbuf);
6320 return TRUE;
6321 }
6322 }
6323
6324 free (extversym);
6325 free (isymbuf);
6326 }
6327
6328 return FALSE;
6329 }
6330
6331 /* Add an external symbol to the symbol table. This is called from
6332 the hash table traversal routine. When generating a shared object,
6333 we go through the symbol table twice. The first time we output
6334 anything that might have been forced to local scope in a version
6335 script. The second time we output the symbols that are still
6336 global symbols. */
6337
6338 static bfd_boolean
6339 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
6340 {
6341 struct elf_outext_info *eoinfo = data;
6342 struct elf_final_link_info *finfo = eoinfo->finfo;
6343 bfd_boolean strip;
6344 Elf_Internal_Sym sym;
6345 asection *input_sec;
6346 const struct elf_backend_data *bed;
6347
6348 if (h->root.type == bfd_link_hash_warning)
6349 {
6350 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6351 if (h->root.type == bfd_link_hash_new)
6352 return TRUE;
6353 }
6354
6355 /* Decide whether to output this symbol in this pass. */
6356 if (eoinfo->localsyms)
6357 {
6358 if (!h->forced_local)
6359 return TRUE;
6360 }
6361 else
6362 {
6363 if (h->forced_local)
6364 return TRUE;
6365 }
6366
6367 bed = get_elf_backend_data (finfo->output_bfd);
6368
6369 /* If we have an undefined symbol reference here then it must have
6370 come from a shared library that is being linked in. (Undefined
6371 references in regular files have already been handled). If we
6372 are reporting errors for this situation then do so now. */
6373 if (h->root.type == bfd_link_hash_undefined
6374 && h->ref_dynamic
6375 && !h->ref_regular
6376 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
6377 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
6378 {
6379 if (! ((*finfo->info->callbacks->undefined_symbol)
6380 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
6381 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
6382 {
6383 eoinfo->failed = TRUE;
6384 return FALSE;
6385 }
6386 }
6387
6388 /* We should also warn if a forced local symbol is referenced from
6389 shared libraries. */
6390 if (! finfo->info->relocatable
6391 && (! finfo->info->shared)
6392 && h->forced_local
6393 && h->ref_dynamic
6394 && !h->dynamic_def
6395 && !h->dynamic_weak
6396 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
6397 {
6398 (*_bfd_error_handler)
6399 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6400 finfo->output_bfd,
6401 h->root.u.def.section == bfd_abs_section_ptr
6402 ? finfo->output_bfd : h->root.u.def.section->owner,
6403 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
6404 ? "internal"
6405 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
6406 ? "hidden" : "local",
6407 h->root.root.string);
6408 eoinfo->failed = TRUE;
6409 return FALSE;
6410 }
6411
6412 /* We don't want to output symbols that have never been mentioned by
6413 a regular file, or that we have been told to strip. However, if
6414 h->indx is set to -2, the symbol is used by a reloc and we must
6415 output it. */
6416 if (h->indx == -2)
6417 strip = FALSE;
6418 else if ((h->def_dynamic
6419 || h->ref_dynamic
6420 || h->root.type == bfd_link_hash_new)
6421 && !h->def_regular
6422 && !h->ref_regular)
6423 strip = TRUE;
6424 else if (finfo->info->strip == strip_all)
6425 strip = TRUE;
6426 else if (finfo->info->strip == strip_some
6427 && bfd_hash_lookup (finfo->info->keep_hash,
6428 h->root.root.string, FALSE, FALSE) == NULL)
6429 strip = TRUE;
6430 else if (finfo->info->strip_discarded
6431 && (h->root.type == bfd_link_hash_defined
6432 || h->root.type == bfd_link_hash_defweak)
6433 && elf_discarded_section (h->root.u.def.section))
6434 strip = TRUE;
6435 else
6436 strip = FALSE;
6437
6438 /* If we're stripping it, and it's not a dynamic symbol, there's
6439 nothing else to do unless it is a forced local symbol. */
6440 if (strip
6441 && h->dynindx == -1
6442 && !h->forced_local)
6443 return TRUE;
6444
6445 sym.st_value = 0;
6446 sym.st_size = h->size;
6447 sym.st_other = h->other;
6448 if (h->forced_local)
6449 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
6450 else if (h->root.type == bfd_link_hash_undefweak
6451 || h->root.type == bfd_link_hash_defweak)
6452 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
6453 else
6454 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
6455
6456 switch (h->root.type)
6457 {
6458 default:
6459 case bfd_link_hash_new:
6460 case bfd_link_hash_warning:
6461 abort ();
6462 return FALSE;
6463
6464 case bfd_link_hash_undefined:
6465 case bfd_link_hash_undefweak:
6466 input_sec = bfd_und_section_ptr;
6467 sym.st_shndx = SHN_UNDEF;
6468 break;
6469
6470 case bfd_link_hash_defined:
6471 case bfd_link_hash_defweak:
6472 {
6473 input_sec = h->root.u.def.section;
6474 if (input_sec->output_section != NULL)
6475 {
6476 sym.st_shndx =
6477 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
6478 input_sec->output_section);
6479 if (sym.st_shndx == SHN_BAD)
6480 {
6481 (*_bfd_error_handler)
6482 (_("%B: could not find output section %A for input section %A"),
6483 finfo->output_bfd, input_sec->output_section, input_sec);
6484 eoinfo->failed = TRUE;
6485 return FALSE;
6486 }
6487
6488 /* ELF symbols in relocatable files are section relative,
6489 but in nonrelocatable files they are virtual
6490 addresses. */
6491 sym.st_value = h->root.u.def.value + input_sec->output_offset;
6492 if (! finfo->info->relocatable)
6493 {
6494 sym.st_value += input_sec->output_section->vma;
6495 if (h->type == STT_TLS)
6496 {
6497 /* STT_TLS symbols are relative to PT_TLS segment
6498 base. */
6499 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6500 sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6501 }
6502 }
6503 }
6504 else
6505 {
6506 BFD_ASSERT (input_sec->owner == NULL
6507 || (input_sec->owner->flags & DYNAMIC) != 0);
6508 sym.st_shndx = SHN_UNDEF;
6509 input_sec = bfd_und_section_ptr;
6510 }
6511 }
6512 break;
6513
6514 case bfd_link_hash_common:
6515 input_sec = h->root.u.c.p->section;
6516 sym.st_shndx = bed->common_section_index (input_sec);
6517 sym.st_value = 1 << h->root.u.c.p->alignment_power;
6518 break;
6519
6520 case bfd_link_hash_indirect:
6521 /* These symbols are created by symbol versioning. They point
6522 to the decorated version of the name. For example, if the
6523 symbol foo@@GNU_1.2 is the default, which should be used when
6524 foo is used with no version, then we add an indirect symbol
6525 foo which points to foo@@GNU_1.2. We ignore these symbols,
6526 since the indirected symbol is already in the hash table. */
6527 return TRUE;
6528 }
6529
6530 /* Give the processor backend a chance to tweak the symbol value,
6531 and also to finish up anything that needs to be done for this
6532 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6533 forced local syms when non-shared is due to a historical quirk. */
6534 if ((h->dynindx != -1
6535 || h->forced_local)
6536 && ((finfo->info->shared
6537 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6538 || h->root.type != bfd_link_hash_undefweak))
6539 || !h->forced_local)
6540 && elf_hash_table (finfo->info)->dynamic_sections_created)
6541 {
6542 if (! ((*bed->elf_backend_finish_dynamic_symbol)
6543 (finfo->output_bfd, finfo->info, h, &sym)))
6544 {
6545 eoinfo->failed = TRUE;
6546 return FALSE;
6547 }
6548 }
6549
6550 /* If we are marking the symbol as undefined, and there are no
6551 non-weak references to this symbol from a regular object, then
6552 mark the symbol as weak undefined; if there are non-weak
6553 references, mark the symbol as strong. We can't do this earlier,
6554 because it might not be marked as undefined until the
6555 finish_dynamic_symbol routine gets through with it. */
6556 if (sym.st_shndx == SHN_UNDEF
6557 && h->ref_regular
6558 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
6559 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
6560 {
6561 int bindtype;
6562
6563 if (h->ref_regular_nonweak)
6564 bindtype = STB_GLOBAL;
6565 else
6566 bindtype = STB_WEAK;
6567 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
6568 }
6569
6570 /* If a non-weak symbol with non-default visibility is not defined
6571 locally, it is a fatal error. */
6572 if (! finfo->info->relocatable
6573 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
6574 && ELF_ST_BIND (sym.st_info) != STB_WEAK
6575 && h->root.type == bfd_link_hash_undefined
6576 && !h->def_regular)
6577 {
6578 (*_bfd_error_handler)
6579 (_("%B: %s symbol `%s' isn't defined"),
6580 finfo->output_bfd,
6581 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
6582 ? "protected"
6583 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
6584 ? "internal" : "hidden",
6585 h->root.root.string);
6586 eoinfo->failed = TRUE;
6587 return FALSE;
6588 }
6589
6590 /* If this symbol should be put in the .dynsym section, then put it
6591 there now. We already know the symbol index. We also fill in
6592 the entry in the .hash section. */
6593 if (h->dynindx != -1
6594 && elf_hash_table (finfo->info)->dynamic_sections_created)
6595 {
6596 size_t bucketcount;
6597 size_t bucket;
6598 size_t hash_entry_size;
6599 bfd_byte *bucketpos;
6600 bfd_vma chain;
6601 bfd_byte *esym;
6602
6603 sym.st_name = h->dynstr_index;
6604 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
6605 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
6606
6607 bucketcount = elf_hash_table (finfo->info)->bucketcount;
6608 bucket = h->u.elf_hash_value % bucketcount;
6609 hash_entry_size
6610 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
6611 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
6612 + (bucket + 2) * hash_entry_size);
6613 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
6614 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
6615 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
6616 ((bfd_byte *) finfo->hash_sec->contents
6617 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
6618
6619 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
6620 {
6621 Elf_Internal_Versym iversym;
6622 Elf_External_Versym *eversym;
6623
6624 if (!h->def_regular)
6625 {
6626 if (h->verinfo.verdef == NULL)
6627 iversym.vs_vers = 0;
6628 else
6629 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
6630 }
6631 else
6632 {
6633 if (h->verinfo.vertree == NULL)
6634 iversym.vs_vers = 1;
6635 else
6636 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
6637 if (finfo->info->create_default_symver)
6638 iversym.vs_vers++;
6639 }
6640
6641 if (h->hidden)
6642 iversym.vs_vers |= VERSYM_HIDDEN;
6643
6644 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
6645 eversym += h->dynindx;
6646 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
6647 }
6648 }
6649
6650 /* If we're stripping it, then it was just a dynamic symbol, and
6651 there's nothing else to do. */
6652 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
6653 return TRUE;
6654
6655 h->indx = bfd_get_symcount (finfo->output_bfd);
6656
6657 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
6658 {
6659 eoinfo->failed = TRUE;
6660 return FALSE;
6661 }
6662
6663 return TRUE;
6664 }
6665
6666 /* Return TRUE if special handling is done for relocs in SEC against
6667 symbols defined in discarded sections. */
6668
6669 static bfd_boolean
6670 elf_section_ignore_discarded_relocs (asection *sec)
6671 {
6672 const struct elf_backend_data *bed;
6673
6674 switch (sec->sec_info_type)
6675 {
6676 case ELF_INFO_TYPE_STABS:
6677 case ELF_INFO_TYPE_EH_FRAME:
6678 return TRUE;
6679 default:
6680 break;
6681 }
6682
6683 bed = get_elf_backend_data (sec->owner);
6684 if (bed->elf_backend_ignore_discarded_relocs != NULL
6685 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
6686 return TRUE;
6687
6688 return FALSE;
6689 }
6690
6691 /* Return a mask saying how ld should treat relocations in SEC against
6692 symbols defined in discarded sections. If this function returns
6693 COMPLAIN set, ld will issue a warning message. If this function
6694 returns PRETEND set, and the discarded section was link-once and the
6695 same size as the kept link-once section, ld will pretend that the
6696 symbol was actually defined in the kept section. Otherwise ld will
6697 zero the reloc (at least that is the intent, but some cooperation by
6698 the target dependent code is needed, particularly for REL targets). */
6699
6700 unsigned int
6701 _bfd_elf_default_action_discarded (asection *sec)
6702 {
6703 if (sec->flags & SEC_DEBUGGING)
6704 return PRETEND;
6705
6706 if (strcmp (".eh_frame", sec->name) == 0)
6707 return 0;
6708
6709 if (strcmp (".gcc_except_table", sec->name) == 0)
6710 return 0;
6711
6712 return COMPLAIN | PRETEND;
6713 }
6714
6715 /* Find a match between a section and a member of a section group. */
6716
6717 static asection *
6718 match_group_member (asection *sec, asection *group)
6719 {
6720 asection *first = elf_next_in_group (group);
6721 asection *s = first;
6722
6723 while (s != NULL)
6724 {
6725 if (bfd_elf_match_symbols_in_sections (s, sec))
6726 return s;
6727
6728 if (s == first)
6729 break;
6730 }
6731
6732 return NULL;
6733 }
6734
6735 /* Check if the kept section of a discarded section SEC can be used
6736 to replace it. Return the replacement if it is OK. Otherwise return
6737 NULL. */
6738
6739 asection *
6740 _bfd_elf_check_kept_section (asection *sec)
6741 {
6742 asection *kept;
6743
6744 kept = sec->kept_section;
6745 if (kept != NULL)
6746 {
6747 if (elf_sec_group (sec) != NULL)
6748 kept = match_group_member (sec, kept);
6749 if (kept != NULL && sec->size != kept->size)
6750 kept = NULL;
6751 }
6752 return kept;
6753 }
6754
6755 /* Link an input file into the linker output file. This function
6756 handles all the sections and relocations of the input file at once.
6757 This is so that we only have to read the local symbols once, and
6758 don't have to keep them in memory. */
6759
6760 static bfd_boolean
6761 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
6762 {
6763 bfd_boolean (*relocate_section)
6764 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
6765 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
6766 bfd *output_bfd;
6767 Elf_Internal_Shdr *symtab_hdr;
6768 size_t locsymcount;
6769 size_t extsymoff;
6770 Elf_Internal_Sym *isymbuf;
6771 Elf_Internal_Sym *isym;
6772 Elf_Internal_Sym *isymend;
6773 long *pindex;
6774 asection **ppsection;
6775 asection *o;
6776 const struct elf_backend_data *bed;
6777 bfd_boolean emit_relocs;
6778 struct elf_link_hash_entry **sym_hashes;
6779
6780 output_bfd = finfo->output_bfd;
6781 bed = get_elf_backend_data (output_bfd);
6782 relocate_section = bed->elf_backend_relocate_section;
6783
6784 /* If this is a dynamic object, we don't want to do anything here:
6785 we don't want the local symbols, and we don't want the section
6786 contents. */
6787 if ((input_bfd->flags & DYNAMIC) != 0)
6788 return TRUE;
6789
6790 emit_relocs = (finfo->info->relocatable
6791 || finfo->info->emitrelocations);
6792
6793 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6794 if (elf_bad_symtab (input_bfd))
6795 {
6796 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
6797 extsymoff = 0;
6798 }
6799 else
6800 {
6801 locsymcount = symtab_hdr->sh_info;
6802 extsymoff = symtab_hdr->sh_info;
6803 }
6804
6805 /* Read the local symbols. */
6806 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
6807 if (isymbuf == NULL && locsymcount != 0)
6808 {
6809 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
6810 finfo->internal_syms,
6811 finfo->external_syms,
6812 finfo->locsym_shndx);
6813 if (isymbuf == NULL)
6814 return FALSE;
6815 }
6816
6817 /* Find local symbol sections and adjust values of symbols in
6818 SEC_MERGE sections. Write out those local symbols we know are
6819 going into the output file. */
6820 isymend = isymbuf + locsymcount;
6821 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
6822 isym < isymend;
6823 isym++, pindex++, ppsection++)
6824 {
6825 asection *isec;
6826 const char *name;
6827 Elf_Internal_Sym osym;
6828
6829 *pindex = -1;
6830
6831 if (elf_bad_symtab (input_bfd))
6832 {
6833 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
6834 {
6835 *ppsection = NULL;
6836 continue;
6837 }
6838 }
6839
6840 if (isym->st_shndx == SHN_UNDEF)
6841 isec = bfd_und_section_ptr;
6842 else if (isym->st_shndx < SHN_LORESERVE
6843 || isym->st_shndx > SHN_HIRESERVE)
6844 {
6845 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
6846 if (isec
6847 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
6848 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
6849 isym->st_value =
6850 _bfd_merged_section_offset (output_bfd, &isec,
6851 elf_section_data (isec)->sec_info,
6852 isym->st_value);
6853 }
6854 else if (isym->st_shndx == SHN_ABS)
6855 isec = bfd_abs_section_ptr;
6856 else if (isym->st_shndx == SHN_COMMON)
6857 isec = bfd_com_section_ptr;
6858 else
6859 {
6860 /* Who knows? */
6861 isec = NULL;
6862 }
6863
6864 *ppsection = isec;
6865
6866 /* Don't output the first, undefined, symbol. */
6867 if (ppsection == finfo->sections)
6868 continue;
6869
6870 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6871 {
6872 /* We never output section symbols. Instead, we use the
6873 section symbol of the corresponding section in the output
6874 file. */
6875 continue;
6876 }
6877
6878 /* If we are stripping all symbols, we don't want to output this
6879 one. */
6880 if (finfo->info->strip == strip_all)
6881 continue;
6882
6883 /* If we are discarding all local symbols, we don't want to
6884 output this one. If we are generating a relocatable output
6885 file, then some of the local symbols may be required by
6886 relocs; we output them below as we discover that they are
6887 needed. */
6888 if (finfo->info->discard == discard_all)
6889 continue;
6890
6891 /* If this symbol is defined in a section which we are
6892 discarding, we don't need to keep it, but note that
6893 linker_mark is only reliable for sections that have contents.
6894 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6895 as well as linker_mark. */
6896 if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
6897 && (isec == NULL
6898 || (! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
6899 || (! finfo->info->relocatable
6900 && (isec->flags & SEC_EXCLUDE) != 0)))
6901 continue;
6902
6903 /* If the section is not in the output BFD's section list, it is not
6904 being output. */
6905 if (bfd_section_removed_from_list (output_bfd, isec->output_section))
6906 continue;
6907
6908 /* Get the name of the symbol. */
6909 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
6910 isym->st_name);
6911 if (name == NULL)
6912 return FALSE;
6913
6914 /* See if we are discarding symbols with this name. */
6915 if ((finfo->info->strip == strip_some
6916 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
6917 == NULL))
6918 || (((finfo->info->discard == discard_sec_merge
6919 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
6920 || finfo->info->discard == discard_l)
6921 && bfd_is_local_label_name (input_bfd, name)))
6922 continue;
6923
6924 /* If we get here, we are going to output this symbol. */
6925
6926 osym = *isym;
6927
6928 /* Adjust the section index for the output file. */
6929 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
6930 isec->output_section);
6931 if (osym.st_shndx == SHN_BAD)
6932 return FALSE;
6933
6934 *pindex = bfd_get_symcount (output_bfd);
6935
6936 /* ELF symbols in relocatable files are section relative, but
6937 in executable files they are virtual addresses. Note that
6938 this code assumes that all ELF sections have an associated
6939 BFD section with a reasonable value for output_offset; below
6940 we assume that they also have a reasonable value for
6941 output_section. Any special sections must be set up to meet
6942 these requirements. */
6943 osym.st_value += isec->output_offset;
6944 if (! finfo->info->relocatable)
6945 {
6946 osym.st_value += isec->output_section->vma;
6947 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
6948 {
6949 /* STT_TLS symbols are relative to PT_TLS segment base. */
6950 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6951 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6952 }
6953 }
6954
6955 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
6956 return FALSE;
6957 }
6958
6959 /* Relocate the contents of each section. */
6960 sym_hashes = elf_sym_hashes (input_bfd);
6961 for (o = input_bfd->sections; o != NULL; o = o->next)
6962 {
6963 bfd_byte *contents;
6964
6965 if (! o->linker_mark)
6966 {
6967 /* This section was omitted from the link. */
6968 continue;
6969 }
6970
6971 if ((o->flags & SEC_HAS_CONTENTS) == 0
6972 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
6973 continue;
6974
6975 if ((o->flags & SEC_LINKER_CREATED) != 0)
6976 {
6977 /* Section was created by _bfd_elf_link_create_dynamic_sections
6978 or somesuch. */
6979 continue;
6980 }
6981
6982 /* Get the contents of the section. They have been cached by a
6983 relaxation routine. Note that o is a section in an input
6984 file, so the contents field will not have been set by any of
6985 the routines which work on output files. */
6986 if (elf_section_data (o)->this_hdr.contents != NULL)
6987 contents = elf_section_data (o)->this_hdr.contents;
6988 else
6989 {
6990 bfd_size_type amt = o->rawsize ? o->rawsize : o->size;
6991
6992 contents = finfo->contents;
6993 if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt))
6994 return FALSE;
6995 }
6996
6997 if ((o->flags & SEC_RELOC) != 0)
6998 {
6999 Elf_Internal_Rela *internal_relocs;
7000 bfd_vma r_type_mask;
7001 int r_sym_shift;
7002
7003 /* Get the swapped relocs. */
7004 internal_relocs
7005 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
7006 finfo->internal_relocs, FALSE);
7007 if (internal_relocs == NULL
7008 && o->reloc_count > 0)
7009 return FALSE;
7010
7011 if (bed->s->arch_size == 32)
7012 {
7013 r_type_mask = 0xff;
7014 r_sym_shift = 8;
7015 }
7016 else
7017 {
7018 r_type_mask = 0xffffffff;
7019 r_sym_shift = 32;
7020 }
7021
7022 /* Run through the relocs looking for any against symbols
7023 from discarded sections and section symbols from
7024 removed link-once sections. Complain about relocs
7025 against discarded sections. Zero relocs against removed
7026 link-once sections. Preserve debug information as much
7027 as we can. */
7028 if (!elf_section_ignore_discarded_relocs (o))
7029 {
7030 Elf_Internal_Rela *rel, *relend;
7031 unsigned int action = (*bed->action_discarded) (o);
7032
7033 rel = internal_relocs;
7034 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
7035 for ( ; rel < relend; rel++)
7036 {
7037 unsigned long r_symndx = rel->r_info >> r_sym_shift;
7038 asection **ps, *sec;
7039 struct elf_link_hash_entry *h = NULL;
7040 const char *sym_name;
7041
7042 if (r_symndx == STN_UNDEF)
7043 continue;
7044
7045 if (r_symndx >= locsymcount
7046 || (elf_bad_symtab (input_bfd)
7047 && finfo->sections[r_symndx] == NULL))
7048 {
7049 h = sym_hashes[r_symndx - extsymoff];
7050
7051 /* Badly formatted input files can contain relocs that
7052 reference non-existant symbols. Check here so that
7053 we do not seg fault. */
7054 if (h == NULL)
7055 {
7056 char buffer [32];
7057
7058 sprintf_vma (buffer, rel->r_info);
7059 (*_bfd_error_handler)
7060 (_("error: %B contains a reloc (0x%s) for section %A "
7061 "that references a non-existent global symbol"),
7062 input_bfd, o, buffer);
7063 bfd_set_error (bfd_error_bad_value);
7064 return FALSE;
7065 }
7066
7067 while (h->root.type == bfd_link_hash_indirect
7068 || h->root.type == bfd_link_hash_warning)
7069 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7070
7071 if (h->root.type != bfd_link_hash_defined
7072 && h->root.type != bfd_link_hash_defweak)
7073 continue;
7074
7075 ps = &h->root.u.def.section;
7076 sym_name = h->root.root.string;
7077 }
7078 else
7079 {
7080 Elf_Internal_Sym *sym = isymbuf + r_symndx;
7081 ps = &finfo->sections[r_symndx];
7082 sym_name = bfd_elf_sym_name (input_bfd,
7083 symtab_hdr,
7084 sym, *ps);
7085 }
7086
7087 /* Complain if the definition comes from a
7088 discarded section. */
7089 if ((sec = *ps) != NULL && elf_discarded_section (sec))
7090 {
7091 BFD_ASSERT (r_symndx != 0);
7092 if (action & COMPLAIN)
7093 (*finfo->info->callbacks->einfo)
7094 (_("%X`%s' referenced in section `%A' of %B: "
7095 "defined in discarded section `%A' of %B\n"),
7096 sym_name, o, input_bfd, sec, sec->owner);
7097
7098 /* Try to do the best we can to support buggy old
7099 versions of gcc. If we've warned, or this is
7100 debugging info, pretend that the symbol is
7101 really defined in the kept linkonce section.
7102 FIXME: This is quite broken. Modifying the
7103 symbol here means we will be changing all later
7104 uses of the symbol, not just in this section.
7105 The only thing that makes this half reasonable
7106 is that we warn in non-debug sections, and
7107 debug sections tend to come after other
7108 sections. */
7109 if (action & PRETEND)
7110 {
7111 asection *kept;
7112
7113 kept = _bfd_elf_check_kept_section (sec);
7114 if (kept != NULL)
7115 {
7116 *ps = kept;
7117 continue;
7118 }
7119 }
7120
7121 /* Remove the symbol reference from the reloc, but
7122 don't kill the reloc completely. This is so that
7123 a zero value will be written into the section,
7124 which may have non-zero contents put there by the
7125 assembler. Zero in things like an eh_frame fde
7126 pc_begin allows stack unwinders to recognize the
7127 fde as bogus. */
7128 rel->r_info &= r_type_mask;
7129 rel->r_addend = 0;
7130 }
7131 }
7132 }
7133
7134 /* Relocate the section by invoking a back end routine.
7135
7136 The back end routine is responsible for adjusting the
7137 section contents as necessary, and (if using Rela relocs
7138 and generating a relocatable output file) adjusting the
7139 reloc addend as necessary.
7140
7141 The back end routine does not have to worry about setting
7142 the reloc address or the reloc symbol index.
7143
7144 The back end routine is given a pointer to the swapped in
7145 internal symbols, and can access the hash table entries
7146 for the external symbols via elf_sym_hashes (input_bfd).
7147
7148 When generating relocatable output, the back end routine
7149 must handle STB_LOCAL/STT_SECTION symbols specially. The
7150 output symbol is going to be a section symbol
7151 corresponding to the output section, which will require
7152 the addend to be adjusted. */
7153
7154 if (! (*relocate_section) (output_bfd, finfo->info,
7155 input_bfd, o, contents,
7156 internal_relocs,
7157 isymbuf,
7158 finfo->sections))
7159 return FALSE;
7160
7161 if (emit_relocs)
7162 {
7163 Elf_Internal_Rela *irela;
7164 Elf_Internal_Rela *irelaend;
7165 bfd_vma last_offset;
7166 struct elf_link_hash_entry **rel_hash;
7167 struct elf_link_hash_entry **rel_hash_list;
7168 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
7169 unsigned int next_erel;
7170 bfd_boolean rela_normal;
7171
7172 input_rel_hdr = &elf_section_data (o)->rel_hdr;
7173 rela_normal = (bed->rela_normal
7174 && (input_rel_hdr->sh_entsize
7175 == bed->s->sizeof_rela));
7176
7177 /* Adjust the reloc addresses and symbol indices. */
7178
7179 irela = internal_relocs;
7180 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
7181 rel_hash = (elf_section_data (o->output_section)->rel_hashes
7182 + elf_section_data (o->output_section)->rel_count
7183 + elf_section_data (o->output_section)->rel_count2);
7184 rel_hash_list = rel_hash;
7185 last_offset = o->output_offset;
7186 if (!finfo->info->relocatable)
7187 last_offset += o->output_section->vma;
7188 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
7189 {
7190 unsigned long r_symndx;
7191 asection *sec;
7192 Elf_Internal_Sym sym;
7193
7194 if (next_erel == bed->s->int_rels_per_ext_rel)
7195 {
7196 rel_hash++;
7197 next_erel = 0;
7198 }
7199
7200 irela->r_offset = _bfd_elf_section_offset (output_bfd,
7201 finfo->info, o,
7202 irela->r_offset);
7203 if (irela->r_offset >= (bfd_vma) -2)
7204 {
7205 /* This is a reloc for a deleted entry or somesuch.
7206 Turn it into an R_*_NONE reloc, at the same
7207 offset as the last reloc. elf_eh_frame.c and
7208 elf_bfd_discard_info rely on reloc offsets
7209 being ordered. */
7210 irela->r_offset = last_offset;
7211 irela->r_info = 0;
7212 irela->r_addend = 0;
7213 continue;
7214 }
7215
7216 irela->r_offset += o->output_offset;
7217
7218 /* Relocs in an executable have to be virtual addresses. */
7219 if (!finfo->info->relocatable)
7220 irela->r_offset += o->output_section->vma;
7221
7222 last_offset = irela->r_offset;
7223
7224 r_symndx = irela->r_info >> r_sym_shift;
7225 if (r_symndx == STN_UNDEF)
7226 continue;
7227
7228 if (r_symndx >= locsymcount
7229 || (elf_bad_symtab (input_bfd)
7230 && finfo->sections[r_symndx] == NULL))
7231 {
7232 struct elf_link_hash_entry *rh;
7233 unsigned long indx;
7234
7235 /* This is a reloc against a global symbol. We
7236 have not yet output all the local symbols, so
7237 we do not know the symbol index of any global
7238 symbol. We set the rel_hash entry for this
7239 reloc to point to the global hash table entry
7240 for this symbol. The symbol index is then
7241 set at the end of bfd_elf_final_link. */
7242 indx = r_symndx - extsymoff;
7243 rh = elf_sym_hashes (input_bfd)[indx];
7244 while (rh->root.type == bfd_link_hash_indirect
7245 || rh->root.type == bfd_link_hash_warning)
7246 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
7247
7248 /* Setting the index to -2 tells
7249 elf_link_output_extsym that this symbol is
7250 used by a reloc. */
7251 BFD_ASSERT (rh->indx < 0);
7252 rh->indx = -2;
7253
7254 *rel_hash = rh;
7255
7256 continue;
7257 }
7258
7259 /* This is a reloc against a local symbol. */
7260
7261 *rel_hash = NULL;
7262 sym = isymbuf[r_symndx];
7263 sec = finfo->sections[r_symndx];
7264 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
7265 {
7266 /* I suppose the backend ought to fill in the
7267 section of any STT_SECTION symbol against a
7268 processor specific section. */
7269 r_symndx = 0;
7270 if (bfd_is_abs_section (sec))
7271 ;
7272 else if (sec == NULL || sec->owner == NULL)
7273 {
7274 bfd_set_error (bfd_error_bad_value);
7275 return FALSE;
7276 }
7277 else
7278 {
7279 asection *osec = sec->output_section;
7280
7281 /* If we have discarded a section, the output
7282 section will be the absolute section. In
7283 case of discarded link-once and discarded
7284 SEC_MERGE sections, use the kept section. */
7285 if (bfd_is_abs_section (osec)
7286 && sec->kept_section != NULL
7287 && sec->kept_section->output_section != NULL)
7288 {
7289 osec = sec->kept_section->output_section;
7290 irela->r_addend -= osec->vma;
7291 }
7292
7293 if (!bfd_is_abs_section (osec))
7294 {
7295 r_symndx = osec->target_index;
7296 BFD_ASSERT (r_symndx != 0);
7297 }
7298 }
7299
7300 /* Adjust the addend according to where the
7301 section winds up in the output section. */
7302 if (rela_normal)
7303 irela->r_addend += sec->output_offset;
7304 }
7305 else
7306 {
7307 if (finfo->indices[r_symndx] == -1)
7308 {
7309 unsigned long shlink;
7310 const char *name;
7311 asection *osec;
7312
7313 if (finfo->info->strip == strip_all)
7314 {
7315 /* You can't do ld -r -s. */
7316 bfd_set_error (bfd_error_invalid_operation);
7317 return FALSE;
7318 }
7319
7320 /* This symbol was skipped earlier, but
7321 since it is needed by a reloc, we
7322 must output it now. */
7323 shlink = symtab_hdr->sh_link;
7324 name = (bfd_elf_string_from_elf_section
7325 (input_bfd, shlink, sym.st_name));
7326 if (name == NULL)
7327 return FALSE;
7328
7329 osec = sec->output_section;
7330 sym.st_shndx =
7331 _bfd_elf_section_from_bfd_section (output_bfd,
7332 osec);
7333 if (sym.st_shndx == SHN_BAD)
7334 return FALSE;
7335
7336 sym.st_value += sec->output_offset;
7337 if (! finfo->info->relocatable)
7338 {
7339 sym.st_value += osec->vma;
7340 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
7341 {
7342 /* STT_TLS symbols are relative to PT_TLS
7343 segment base. */
7344 BFD_ASSERT (elf_hash_table (finfo->info)
7345 ->tls_sec != NULL);
7346 sym.st_value -= (elf_hash_table (finfo->info)
7347 ->tls_sec->vma);
7348 }
7349 }
7350
7351 finfo->indices[r_symndx]
7352 = bfd_get_symcount (output_bfd);
7353
7354 if (! elf_link_output_sym (finfo, name, &sym, sec,
7355 NULL))
7356 return FALSE;
7357 }
7358
7359 r_symndx = finfo->indices[r_symndx];
7360 }
7361
7362 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
7363 | (irela->r_info & r_type_mask));
7364 }
7365
7366 /* Swap out the relocs. */
7367 if (input_rel_hdr->sh_size != 0
7368 && !bed->elf_backend_emit_relocs (output_bfd, o,
7369 input_rel_hdr,
7370 internal_relocs,
7371 rel_hash_list))
7372 return FALSE;
7373
7374 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
7375 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
7376 {
7377 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
7378 * bed->s->int_rels_per_ext_rel);
7379 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
7380 if (!bed->elf_backend_emit_relocs (output_bfd, o,
7381 input_rel_hdr2,
7382 internal_relocs,
7383 rel_hash_list))
7384 return FALSE;
7385 }
7386 }
7387 }
7388
7389 /* Write out the modified section contents. */
7390 if (bed->elf_backend_write_section
7391 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
7392 {
7393 /* Section written out. */
7394 }
7395 else switch (o->sec_info_type)
7396 {
7397 case ELF_INFO_TYPE_STABS:
7398 if (! (_bfd_write_section_stabs
7399 (output_bfd,
7400 &elf_hash_table (finfo->info)->stab_info,
7401 o, &elf_section_data (o)->sec_info, contents)))
7402 return FALSE;
7403 break;
7404 case ELF_INFO_TYPE_MERGE:
7405 if (! _bfd_write_merged_section (output_bfd, o,
7406 elf_section_data (o)->sec_info))
7407 return FALSE;
7408 break;
7409 case ELF_INFO_TYPE_EH_FRAME:
7410 {
7411 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
7412 o, contents))
7413 return FALSE;
7414 }
7415 break;
7416 default:
7417 {
7418 if (! (o->flags & SEC_EXCLUDE)
7419 && ! bfd_set_section_contents (output_bfd, o->output_section,
7420 contents,
7421 (file_ptr) o->output_offset,
7422 o->size))
7423 return FALSE;
7424 }
7425 break;
7426 }
7427 }
7428
7429 return TRUE;
7430 }
7431
7432 /* Generate a reloc when linking an ELF file. This is a reloc
7433 requested by the linker, and does come from any input file. This
7434 is used to build constructor and destructor tables when linking
7435 with -Ur. */
7436
7437 static bfd_boolean
7438 elf_reloc_link_order (bfd *output_bfd,
7439 struct bfd_link_info *info,
7440 asection *output_section,
7441 struct bfd_link_order *link_order)
7442 {
7443 reloc_howto_type *howto;
7444 long indx;
7445 bfd_vma offset;
7446 bfd_vma addend;
7447 struct elf_link_hash_entry **rel_hash_ptr;
7448 Elf_Internal_Shdr *rel_hdr;
7449 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7450 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
7451 bfd_byte *erel;
7452 unsigned int i;
7453
7454 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
7455 if (howto == NULL)
7456 {
7457 bfd_set_error (bfd_error_bad_value);
7458 return FALSE;
7459 }
7460
7461 addend = link_order->u.reloc.p->addend;
7462
7463 /* Figure out the symbol index. */
7464 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
7465 + elf_section_data (output_section)->rel_count
7466 + elf_section_data (output_section)->rel_count2);
7467 if (link_order->type == bfd_section_reloc_link_order)
7468 {
7469 indx = link_order->u.reloc.p->u.section->target_index;
7470 BFD_ASSERT (indx != 0);
7471 *rel_hash_ptr = NULL;
7472 }
7473 else
7474 {
7475 struct elf_link_hash_entry *h;
7476
7477 /* Treat a reloc against a defined symbol as though it were
7478 actually against the section. */
7479 h = ((struct elf_link_hash_entry *)
7480 bfd_wrapped_link_hash_lookup (output_bfd, info,
7481 link_order->u.reloc.p->u.name,
7482 FALSE, FALSE, TRUE));
7483 if (h != NULL
7484 && (h->root.type == bfd_link_hash_defined
7485 || h->root.type == bfd_link_hash_defweak))
7486 {
7487 asection *section;
7488
7489 section = h->root.u.def.section;
7490 indx = section->output_section->target_index;
7491 *rel_hash_ptr = NULL;
7492 /* It seems that we ought to add the symbol value to the
7493 addend here, but in practice it has already been added
7494 because it was passed to constructor_callback. */
7495 addend += section->output_section->vma + section->output_offset;
7496 }
7497 else if (h != NULL)
7498 {
7499 /* Setting the index to -2 tells elf_link_output_extsym that
7500 this symbol is used by a reloc. */
7501 h->indx = -2;
7502 *rel_hash_ptr = h;
7503 indx = 0;
7504 }
7505 else
7506 {
7507 if (! ((*info->callbacks->unattached_reloc)
7508 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
7509 return FALSE;
7510 indx = 0;
7511 }
7512 }
7513
7514 /* If this is an inplace reloc, we must write the addend into the
7515 object file. */
7516 if (howto->partial_inplace && addend != 0)
7517 {
7518 bfd_size_type size;
7519 bfd_reloc_status_type rstat;
7520 bfd_byte *buf;
7521 bfd_boolean ok;
7522 const char *sym_name;
7523
7524 size = bfd_get_reloc_size (howto);
7525 buf = bfd_zmalloc (size);
7526 if (buf == NULL)
7527 return FALSE;
7528 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
7529 switch (rstat)
7530 {
7531 case bfd_reloc_ok:
7532 break;
7533
7534 default:
7535 case bfd_reloc_outofrange:
7536 abort ();
7537
7538 case bfd_reloc_overflow:
7539 if (link_order->type == bfd_section_reloc_link_order)
7540 sym_name = bfd_section_name (output_bfd,
7541 link_order->u.reloc.p->u.section);
7542 else
7543 sym_name = link_order->u.reloc.p->u.name;
7544 if (! ((*info->callbacks->reloc_overflow)
7545 (info, NULL, sym_name, howto->name, addend, NULL,
7546 NULL, (bfd_vma) 0)))
7547 {
7548 free (buf);
7549 return FALSE;
7550 }
7551 break;
7552 }
7553 ok = bfd_set_section_contents (output_bfd, output_section, buf,
7554 link_order->offset, size);
7555 free (buf);
7556 if (! ok)
7557 return FALSE;
7558 }
7559
7560 /* The address of a reloc is relative to the section in a
7561 relocatable file, and is a virtual address in an executable
7562 file. */
7563 offset = link_order->offset;
7564 if (! info->relocatable)
7565 offset += output_section->vma;
7566
7567 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7568 {
7569 irel[i].r_offset = offset;
7570 irel[i].r_info = 0;
7571 irel[i].r_addend = 0;
7572 }
7573 if (bed->s->arch_size == 32)
7574 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
7575 else
7576 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
7577
7578 rel_hdr = &elf_section_data (output_section)->rel_hdr;
7579 erel = rel_hdr->contents;
7580 if (rel_hdr->sh_type == SHT_REL)
7581 {
7582 erel += (elf_section_data (output_section)->rel_count
7583 * bed->s->sizeof_rel);
7584 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
7585 }
7586 else
7587 {
7588 irel[0].r_addend = addend;
7589 erel += (elf_section_data (output_section)->rel_count
7590 * bed->s->sizeof_rela);
7591 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
7592 }
7593
7594 ++elf_section_data (output_section)->rel_count;
7595
7596 return TRUE;
7597 }
7598
7599
7600 /* Get the output vma of the section pointed to by the sh_link field. */
7601
7602 static bfd_vma
7603 elf_get_linked_section_vma (struct bfd_link_order *p)
7604 {
7605 Elf_Internal_Shdr **elf_shdrp;
7606 asection *s;
7607 int elfsec;
7608
7609 s = p->u.indirect.section;
7610 elf_shdrp = elf_elfsections (s->owner);
7611 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
7612 elfsec = elf_shdrp[elfsec]->sh_link;
7613 /* PR 290:
7614 The Intel C compiler generates SHT_IA_64_UNWIND with
7615 SHF_LINK_ORDER. But it doesn't set theh sh_link or
7616 sh_info fields. Hence we could get the situation
7617 where elfsec is 0. */
7618 if (elfsec == 0)
7619 {
7620 const struct elf_backend_data *bed
7621 = get_elf_backend_data (s->owner);
7622 if (bed->link_order_error_handler)
7623 bed->link_order_error_handler
7624 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
7625 return 0;
7626 }
7627 else
7628 {
7629 s = elf_shdrp[elfsec]->bfd_section;
7630 return s->output_section->vma + s->output_offset;
7631 }
7632 }
7633
7634
7635 /* Compare two sections based on the locations of the sections they are
7636 linked to. Used by elf_fixup_link_order. */
7637
7638 static int
7639 compare_link_order (const void * a, const void * b)
7640 {
7641 bfd_vma apos;
7642 bfd_vma bpos;
7643
7644 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
7645 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
7646 if (apos < bpos)
7647 return -1;
7648 return apos > bpos;
7649 }
7650
7651
7652 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7653 order as their linked sections. Returns false if this could not be done
7654 because an output section includes both ordered and unordered
7655 sections. Ideally we'd do this in the linker proper. */
7656
7657 static bfd_boolean
7658 elf_fixup_link_order (bfd *abfd, asection *o)
7659 {
7660 int seen_linkorder;
7661 int seen_other;
7662 int n;
7663 struct bfd_link_order *p;
7664 bfd *sub;
7665 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7666 int elfsec;
7667 struct bfd_link_order **sections;
7668 asection *s;
7669 bfd_vma offset;
7670
7671 seen_other = 0;
7672 seen_linkorder = 0;
7673 for (p = o->map_head.link_order; p != NULL; p = p->next)
7674 {
7675 if (p->type == bfd_indirect_link_order
7676 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
7677 == bfd_target_elf_flavour)
7678 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
7679 {
7680 s = p->u.indirect.section;
7681 elfsec = _bfd_elf_section_from_bfd_section (sub, s);
7682 if (elfsec != -1
7683 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER)
7684 seen_linkorder++;
7685 else
7686 seen_other++;
7687 }
7688 else
7689 seen_other++;
7690 }
7691
7692 if (!seen_linkorder)
7693 return TRUE;
7694
7695 if (seen_other && seen_linkorder)
7696 {
7697 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
7698 o);
7699 bfd_set_error (bfd_error_bad_value);
7700 return FALSE;
7701 }
7702
7703 sections = (struct bfd_link_order **)
7704 xmalloc (seen_linkorder * sizeof (struct bfd_link_order *));
7705 seen_linkorder = 0;
7706
7707 for (p = o->map_head.link_order; p != NULL; p = p->next)
7708 {
7709 sections[seen_linkorder++] = p;
7710 }
7711 /* Sort the input sections in the order of their linked section. */
7712 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
7713 compare_link_order);
7714
7715 /* Change the offsets of the sections. */
7716 offset = 0;
7717 for (n = 0; n < seen_linkorder; n++)
7718 {
7719 s = sections[n]->u.indirect.section;
7720 offset &= ~(bfd_vma)((1 << s->alignment_power) - 1);
7721 s->output_offset = offset;
7722 sections[n]->offset = offset;
7723 offset += sections[n]->size;
7724 }
7725
7726 return TRUE;
7727 }
7728
7729
7730 /* Do the final step of an ELF link. */
7731
7732 bfd_boolean
7733 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
7734 {
7735 bfd_boolean dynamic;
7736 bfd_boolean emit_relocs;
7737 bfd *dynobj;
7738 struct elf_final_link_info finfo;
7739 register asection *o;
7740 register struct bfd_link_order *p;
7741 register bfd *sub;
7742 bfd_size_type max_contents_size;
7743 bfd_size_type max_external_reloc_size;
7744 bfd_size_type max_internal_reloc_count;
7745 bfd_size_type max_sym_count;
7746 bfd_size_type max_sym_shndx_count;
7747 file_ptr off;
7748 Elf_Internal_Sym elfsym;
7749 unsigned int i;
7750 Elf_Internal_Shdr *symtab_hdr;
7751 Elf_Internal_Shdr *symtab_shndx_hdr;
7752 Elf_Internal_Shdr *symstrtab_hdr;
7753 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7754 struct elf_outext_info eoinfo;
7755 bfd_boolean merged;
7756 size_t relativecount = 0;
7757 asection *reldyn = 0;
7758 bfd_size_type amt;
7759
7760 if (! is_elf_hash_table (info->hash))
7761 return FALSE;
7762
7763 if (info->shared)
7764 abfd->flags |= DYNAMIC;
7765
7766 dynamic = elf_hash_table (info)->dynamic_sections_created;
7767 dynobj = elf_hash_table (info)->dynobj;
7768
7769 emit_relocs = (info->relocatable
7770 || info->emitrelocations
7771 || bed->elf_backend_emit_relocs);
7772
7773 finfo.info = info;
7774 finfo.output_bfd = abfd;
7775 finfo.symstrtab = _bfd_elf_stringtab_init ();
7776 if (finfo.symstrtab == NULL)
7777 return FALSE;
7778
7779 if (! dynamic)
7780 {
7781 finfo.dynsym_sec = NULL;
7782 finfo.hash_sec = NULL;
7783 finfo.symver_sec = NULL;
7784 }
7785 else
7786 {
7787 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
7788 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
7789 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
7790 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
7791 /* Note that it is OK if symver_sec is NULL. */
7792 }
7793
7794 finfo.contents = NULL;
7795 finfo.external_relocs = NULL;
7796 finfo.internal_relocs = NULL;
7797 finfo.external_syms = NULL;
7798 finfo.locsym_shndx = NULL;
7799 finfo.internal_syms = NULL;
7800 finfo.indices = NULL;
7801 finfo.sections = NULL;
7802 finfo.symbuf = NULL;
7803 finfo.symshndxbuf = NULL;
7804 finfo.symbuf_count = 0;
7805 finfo.shndxbuf_size = 0;
7806
7807 /* Count up the number of relocations we will output for each output
7808 section, so that we know the sizes of the reloc sections. We
7809 also figure out some maximum sizes. */
7810 max_contents_size = 0;
7811 max_external_reloc_size = 0;
7812 max_internal_reloc_count = 0;
7813 max_sym_count = 0;
7814 max_sym_shndx_count = 0;
7815 merged = FALSE;
7816 for (o = abfd->sections; o != NULL; o = o->next)
7817 {
7818 struct bfd_elf_section_data *esdo = elf_section_data (o);
7819 o->reloc_count = 0;
7820
7821 for (p = o->map_head.link_order; p != NULL; p = p->next)
7822 {
7823 unsigned int reloc_count = 0;
7824 struct bfd_elf_section_data *esdi = NULL;
7825 unsigned int *rel_count1;
7826
7827 if (p->type == bfd_section_reloc_link_order
7828 || p->type == bfd_symbol_reloc_link_order)
7829 reloc_count = 1;
7830 else if (p->type == bfd_indirect_link_order)
7831 {
7832 asection *sec;
7833
7834 sec = p->u.indirect.section;
7835 esdi = elf_section_data (sec);
7836
7837 /* Mark all sections which are to be included in the
7838 link. This will normally be every section. We need
7839 to do this so that we can identify any sections which
7840 the linker has decided to not include. */
7841 sec->linker_mark = TRUE;
7842
7843 if (sec->flags & SEC_MERGE)
7844 merged = TRUE;
7845
7846 if (info->relocatable || info->emitrelocations)
7847 reloc_count = sec->reloc_count;
7848 else if (bed->elf_backend_count_relocs)
7849 {
7850 Elf_Internal_Rela * relocs;
7851
7852 relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
7853 info->keep_memory);
7854
7855 reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs);
7856
7857 if (elf_section_data (o)->relocs != relocs)
7858 free (relocs);
7859 }
7860
7861 if (sec->rawsize > max_contents_size)
7862 max_contents_size = sec->rawsize;
7863 if (sec->size > max_contents_size)
7864 max_contents_size = sec->size;
7865
7866 /* We are interested in just local symbols, not all
7867 symbols. */
7868 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
7869 && (sec->owner->flags & DYNAMIC) == 0)
7870 {
7871 size_t sym_count;
7872
7873 if (elf_bad_symtab (sec->owner))
7874 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
7875 / bed->s->sizeof_sym);
7876 else
7877 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
7878
7879 if (sym_count > max_sym_count)
7880 max_sym_count = sym_count;
7881
7882 if (sym_count > max_sym_shndx_count
7883 && elf_symtab_shndx (sec->owner) != 0)
7884 max_sym_shndx_count = sym_count;
7885
7886 if ((sec->flags & SEC_RELOC) != 0)
7887 {
7888 size_t ext_size;
7889
7890 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
7891 if (ext_size > max_external_reloc_size)
7892 max_external_reloc_size = ext_size;
7893 if (sec->reloc_count > max_internal_reloc_count)
7894 max_internal_reloc_count = sec->reloc_count;
7895 }
7896 }
7897 }
7898
7899 if (reloc_count == 0)
7900 continue;
7901
7902 o->reloc_count += reloc_count;
7903
7904 /* MIPS may have a mix of REL and RELA relocs on sections.
7905 To support this curious ABI we keep reloc counts in
7906 elf_section_data too. We must be careful to add the
7907 relocations from the input section to the right output
7908 count. FIXME: Get rid of one count. We have
7909 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7910 rel_count1 = &esdo->rel_count;
7911 if (esdi != NULL)
7912 {
7913 bfd_boolean same_size;
7914 bfd_size_type entsize1;
7915
7916 entsize1 = esdi->rel_hdr.sh_entsize;
7917 BFD_ASSERT (entsize1 == bed->s->sizeof_rel
7918 || entsize1 == bed->s->sizeof_rela);
7919 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
7920
7921 if (!same_size)
7922 rel_count1 = &esdo->rel_count2;
7923
7924 if (esdi->rel_hdr2 != NULL)
7925 {
7926 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
7927 unsigned int alt_count;
7928 unsigned int *rel_count2;
7929
7930 BFD_ASSERT (entsize2 != entsize1
7931 && (entsize2 == bed->s->sizeof_rel
7932 || entsize2 == bed->s->sizeof_rela));
7933
7934 rel_count2 = &esdo->rel_count2;
7935 if (!same_size)
7936 rel_count2 = &esdo->rel_count;
7937
7938 /* The following is probably too simplistic if the
7939 backend counts output relocs unusually. */
7940 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
7941 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
7942 *rel_count2 += alt_count;
7943 reloc_count -= alt_count;
7944 }
7945 }
7946 *rel_count1 += reloc_count;
7947 }
7948
7949 if (o->reloc_count > 0)
7950 o->flags |= SEC_RELOC;
7951 else
7952 {
7953 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7954 set it (this is probably a bug) and if it is set
7955 assign_section_numbers will create a reloc section. */
7956 o->flags &=~ SEC_RELOC;
7957 }
7958
7959 /* If the SEC_ALLOC flag is not set, force the section VMA to
7960 zero. This is done in elf_fake_sections as well, but forcing
7961 the VMA to 0 here will ensure that relocs against these
7962 sections are handled correctly. */
7963 if ((o->flags & SEC_ALLOC) == 0
7964 && ! o->user_set_vma)
7965 o->vma = 0;
7966 }
7967
7968 if (! info->relocatable && merged)
7969 elf_link_hash_traverse (elf_hash_table (info),
7970 _bfd_elf_link_sec_merge_syms, abfd);
7971
7972 /* Figure out the file positions for everything but the symbol table
7973 and the relocs. We set symcount to force assign_section_numbers
7974 to create a symbol table. */
7975 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
7976 BFD_ASSERT (! abfd->output_has_begun);
7977 if (! _bfd_elf_compute_section_file_positions (abfd, info))
7978 goto error_return;
7979
7980 /* Set sizes, and assign file positions for reloc sections. */
7981 for (o = abfd->sections; o != NULL; o = o->next)
7982 {
7983 if ((o->flags & SEC_RELOC) != 0)
7984 {
7985 if (!(_bfd_elf_link_size_reloc_section
7986 (abfd, &elf_section_data (o)->rel_hdr, o)))
7987 goto error_return;
7988
7989 if (elf_section_data (o)->rel_hdr2
7990 && !(_bfd_elf_link_size_reloc_section
7991 (abfd, elf_section_data (o)->rel_hdr2, o)))
7992 goto error_return;
7993 }
7994
7995 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
7996 to count upwards while actually outputting the relocations. */
7997 elf_section_data (o)->rel_count = 0;
7998 elf_section_data (o)->rel_count2 = 0;
7999 }
8000
8001 _bfd_elf_assign_file_positions_for_relocs (abfd);
8002
8003 /* We have now assigned file positions for all the sections except
8004 .symtab and .strtab. We start the .symtab section at the current
8005 file position, and write directly to it. We build the .strtab
8006 section in memory. */
8007 bfd_get_symcount (abfd) = 0;
8008 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8009 /* sh_name is set in prep_headers. */
8010 symtab_hdr->sh_type = SHT_SYMTAB;
8011 /* sh_flags, sh_addr and sh_size all start off zero. */
8012 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8013 /* sh_link is set in assign_section_numbers. */
8014 /* sh_info is set below. */
8015 /* sh_offset is set just below. */
8016 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
8017
8018 off = elf_tdata (abfd)->next_file_pos;
8019 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
8020
8021 /* Note that at this point elf_tdata (abfd)->next_file_pos is
8022 incorrect. We do not yet know the size of the .symtab section.
8023 We correct next_file_pos below, after we do know the size. */
8024
8025 /* Allocate a buffer to hold swapped out symbols. This is to avoid
8026 continuously seeking to the right position in the file. */
8027 if (! info->keep_memory || max_sym_count < 20)
8028 finfo.symbuf_size = 20;
8029 else
8030 finfo.symbuf_size = max_sym_count;
8031 amt = finfo.symbuf_size;
8032 amt *= bed->s->sizeof_sym;
8033 finfo.symbuf = bfd_malloc (amt);
8034 if (finfo.symbuf == NULL)
8035 goto error_return;
8036 if (elf_numsections (abfd) > SHN_LORESERVE)
8037 {
8038 /* Wild guess at number of output symbols. realloc'd as needed. */
8039 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
8040 finfo.shndxbuf_size = amt;
8041 amt *= sizeof (Elf_External_Sym_Shndx);
8042 finfo.symshndxbuf = bfd_zmalloc (amt);
8043 if (finfo.symshndxbuf == NULL)
8044 goto error_return;
8045 }
8046
8047 /* Start writing out the symbol table. The first symbol is always a
8048 dummy symbol. */
8049 if (info->strip != strip_all
8050 || emit_relocs)
8051 {
8052 elfsym.st_value = 0;
8053 elfsym.st_size = 0;
8054 elfsym.st_info = 0;
8055 elfsym.st_other = 0;
8056 elfsym.st_shndx = SHN_UNDEF;
8057 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
8058 NULL))
8059 goto error_return;
8060 }
8061
8062 /* Output a symbol for each section. We output these even if we are
8063 discarding local symbols, since they are used for relocs. These
8064 symbols have no names. We store the index of each one in the
8065 index field of the section, so that we can find it again when
8066 outputting relocs. */
8067 if (info->strip != strip_all
8068 || emit_relocs)
8069 {
8070 elfsym.st_size = 0;
8071 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8072 elfsym.st_other = 0;
8073 for (i = 1; i < elf_numsections (abfd); i++)
8074 {
8075 o = bfd_section_from_elf_index (abfd, i);
8076 if (o != NULL)
8077 o->target_index = bfd_get_symcount (abfd);
8078 elfsym.st_shndx = i;
8079 if (info->relocatable || o == NULL)
8080 elfsym.st_value = 0;
8081 else
8082 elfsym.st_value = o->vma;
8083 if (! elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
8084 goto error_return;
8085 if (i == SHN_LORESERVE - 1)
8086 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
8087 }
8088 }
8089
8090 /* Allocate some memory to hold information read in from the input
8091 files. */
8092 if (max_contents_size != 0)
8093 {
8094 finfo.contents = bfd_malloc (max_contents_size);
8095 if (finfo.contents == NULL)
8096 goto error_return;
8097 }
8098
8099 if (max_external_reloc_size != 0)
8100 {
8101 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
8102 if (finfo.external_relocs == NULL)
8103 goto error_return;
8104 }
8105
8106 if (max_internal_reloc_count != 0)
8107 {
8108 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
8109 amt *= sizeof (Elf_Internal_Rela);
8110 finfo.internal_relocs = bfd_malloc (amt);
8111 if (finfo.internal_relocs == NULL)
8112 goto error_return;
8113 }
8114
8115 if (max_sym_count != 0)
8116 {
8117 amt = max_sym_count * bed->s->sizeof_sym;
8118 finfo.external_syms = bfd_malloc (amt);
8119 if (finfo.external_syms == NULL)
8120 goto error_return;
8121
8122 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8123 finfo.internal_syms = bfd_malloc (amt);
8124 if (finfo.internal_syms == NULL)
8125 goto error_return;
8126
8127 amt = max_sym_count * sizeof (long);
8128 finfo.indices = bfd_malloc (amt);
8129 if (finfo.indices == NULL)
8130 goto error_return;
8131
8132 amt = max_sym_count * sizeof (asection *);
8133 finfo.sections = bfd_malloc (amt);
8134 if (finfo.sections == NULL)
8135 goto error_return;
8136 }
8137
8138 if (max_sym_shndx_count != 0)
8139 {
8140 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8141 finfo.locsym_shndx = bfd_malloc (amt);
8142 if (finfo.locsym_shndx == NULL)
8143 goto error_return;
8144 }
8145
8146 if (elf_hash_table (info)->tls_sec)
8147 {
8148 bfd_vma base, end = 0;
8149 asection *sec;
8150
8151 for (sec = elf_hash_table (info)->tls_sec;
8152 sec && (sec->flags & SEC_THREAD_LOCAL);
8153 sec = sec->next)
8154 {
8155 bfd_vma size = sec->size;
8156
8157 if (size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0)
8158 {
8159 struct bfd_link_order *o;
8160
8161 for (o = sec->map_head.link_order; o != NULL; o = o->next)
8162 if (size < o->offset + o->size)
8163 size = o->offset + o->size;
8164 }
8165 end = sec->vma + size;
8166 }
8167 base = elf_hash_table (info)->tls_sec->vma;
8168 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
8169 elf_hash_table (info)->tls_size = end - base;
8170 }
8171
8172 /* Reorder SHF_LINK_ORDER sections. */
8173 for (o = abfd->sections; o != NULL; o = o->next)
8174 {
8175 if (!elf_fixup_link_order (abfd, o))
8176 return FALSE;
8177 }
8178
8179 /* Since ELF permits relocations to be against local symbols, we
8180 must have the local symbols available when we do the relocations.
8181 Since we would rather only read the local symbols once, and we
8182 would rather not keep them in memory, we handle all the
8183 relocations for a single input file at the same time.
8184
8185 Unfortunately, there is no way to know the total number of local
8186 symbols until we have seen all of them, and the local symbol
8187 indices precede the global symbol indices. This means that when
8188 we are generating relocatable output, and we see a reloc against
8189 a global symbol, we can not know the symbol index until we have
8190 finished examining all the local symbols to see which ones we are
8191 going to output. To deal with this, we keep the relocations in
8192 memory, and don't output them until the end of the link. This is
8193 an unfortunate waste of memory, but I don't see a good way around
8194 it. Fortunately, it only happens when performing a relocatable
8195 link, which is not the common case. FIXME: If keep_memory is set
8196 we could write the relocs out and then read them again; I don't
8197 know how bad the memory loss will be. */
8198
8199 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8200 sub->output_has_begun = FALSE;
8201 for (o = abfd->sections; o != NULL; o = o->next)
8202 {
8203 for (p = o->map_head.link_order; p != NULL; p = p->next)
8204 {
8205 if (p->type == bfd_indirect_link_order
8206 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
8207 == bfd_target_elf_flavour)
8208 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
8209 {
8210 if (! sub->output_has_begun)
8211 {
8212 if (! elf_link_input_bfd (&finfo, sub))
8213 goto error_return;
8214 sub->output_has_begun = TRUE;
8215 }
8216 }
8217 else if (p->type == bfd_section_reloc_link_order
8218 || p->type == bfd_symbol_reloc_link_order)
8219 {
8220 if (! elf_reloc_link_order (abfd, info, o, p))
8221 goto error_return;
8222 }
8223 else
8224 {
8225 if (! _bfd_default_link_order (abfd, info, o, p))
8226 goto error_return;
8227 }
8228 }
8229 }
8230
8231 /* Output any global symbols that got converted to local in a
8232 version script or due to symbol visibility. We do this in a
8233 separate step since ELF requires all local symbols to appear
8234 prior to any global symbols. FIXME: We should only do this if
8235 some global symbols were, in fact, converted to become local.
8236 FIXME: Will this work correctly with the Irix 5 linker? */
8237 eoinfo.failed = FALSE;
8238 eoinfo.finfo = &finfo;
8239 eoinfo.localsyms = TRUE;
8240 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
8241 &eoinfo);
8242 if (eoinfo.failed)
8243 return FALSE;
8244
8245 /* That wrote out all the local symbols. Finish up the symbol table
8246 with the global symbols. Even if we want to strip everything we
8247 can, we still need to deal with those global symbols that got
8248 converted to local in a version script. */
8249
8250 /* The sh_info field records the index of the first non local symbol. */
8251 symtab_hdr->sh_info = bfd_get_symcount (abfd);
8252
8253 if (dynamic
8254 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
8255 {
8256 Elf_Internal_Sym sym;
8257 bfd_byte *dynsym = finfo.dynsym_sec->contents;
8258 long last_local = 0;
8259
8260 /* Write out the section symbols for the output sections. */
8261 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
8262 {
8263 asection *s;
8264
8265 sym.st_size = 0;
8266 sym.st_name = 0;
8267 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8268 sym.st_other = 0;
8269
8270 for (s = abfd->sections; s != NULL; s = s->next)
8271 {
8272 int indx;
8273 bfd_byte *dest;
8274 long dynindx;
8275
8276 dynindx = elf_section_data (s)->dynindx;
8277 if (dynindx <= 0)
8278 continue;
8279 indx = elf_section_data (s)->this_idx;
8280 BFD_ASSERT (indx > 0);
8281 sym.st_shndx = indx;
8282 sym.st_value = s->vma;
8283 dest = dynsym + dynindx * bed->s->sizeof_sym;
8284 if (last_local < dynindx)
8285 last_local = dynindx;
8286 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
8287 }
8288 }
8289
8290 /* Write out the local dynsyms. */
8291 if (elf_hash_table (info)->dynlocal)
8292 {
8293 struct elf_link_local_dynamic_entry *e;
8294 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
8295 {
8296 asection *s;
8297 bfd_byte *dest;
8298
8299 sym.st_size = e->isym.st_size;
8300 sym.st_other = e->isym.st_other;
8301
8302 /* Copy the internal symbol as is.
8303 Note that we saved a word of storage and overwrote
8304 the original st_name with the dynstr_index. */
8305 sym = e->isym;
8306
8307 if (e->isym.st_shndx != SHN_UNDEF
8308 && (e->isym.st_shndx < SHN_LORESERVE
8309 || e->isym.st_shndx > SHN_HIRESERVE))
8310 {
8311 s = bfd_section_from_elf_index (e->input_bfd,
8312 e->isym.st_shndx);
8313
8314 sym.st_shndx =
8315 elf_section_data (s->output_section)->this_idx;
8316 sym.st_value = (s->output_section->vma
8317 + s->output_offset
8318 + e->isym.st_value);
8319 }
8320
8321 if (last_local < e->dynindx)
8322 last_local = e->dynindx;
8323
8324 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
8325 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
8326 }
8327 }
8328
8329 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
8330 last_local + 1;
8331 }
8332
8333 /* We get the global symbols from the hash table. */
8334 eoinfo.failed = FALSE;
8335 eoinfo.localsyms = FALSE;
8336 eoinfo.finfo = &finfo;
8337 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
8338 &eoinfo);
8339 if (eoinfo.failed)
8340 return FALSE;
8341
8342 /* If backend needs to output some symbols not present in the hash
8343 table, do it now. */
8344 if (bed->elf_backend_output_arch_syms)
8345 {
8346 typedef bfd_boolean (*out_sym_func)
8347 (void *, const char *, Elf_Internal_Sym *, asection *,
8348 struct elf_link_hash_entry *);
8349
8350 if (! ((*bed->elf_backend_output_arch_syms)
8351 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
8352 return FALSE;
8353 }
8354
8355 /* Flush all symbols to the file. */
8356 if (! elf_link_flush_output_syms (&finfo, bed))
8357 return FALSE;
8358
8359 /* Now we know the size of the symtab section. */
8360 off += symtab_hdr->sh_size;
8361
8362 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
8363 if (symtab_shndx_hdr->sh_name != 0)
8364 {
8365 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8366 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8367 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8368 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
8369 symtab_shndx_hdr->sh_size = amt;
8370
8371 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
8372 off, TRUE);
8373
8374 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
8375 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
8376 return FALSE;
8377 }
8378
8379
8380 /* Finish up and write out the symbol string table (.strtab)
8381 section. */
8382 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8383 /* sh_name was set in prep_headers. */
8384 symstrtab_hdr->sh_type = SHT_STRTAB;
8385 symstrtab_hdr->sh_flags = 0;
8386 symstrtab_hdr->sh_addr = 0;
8387 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
8388 symstrtab_hdr->sh_entsize = 0;
8389 symstrtab_hdr->sh_link = 0;
8390 symstrtab_hdr->sh_info = 0;
8391 /* sh_offset is set just below. */
8392 symstrtab_hdr->sh_addralign = 1;
8393
8394 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
8395 elf_tdata (abfd)->next_file_pos = off;
8396
8397 if (bfd_get_symcount (abfd) > 0)
8398 {
8399 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
8400 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
8401 return FALSE;
8402 }
8403
8404 /* Adjust the relocs to have the correct symbol indices. */
8405 for (o = abfd->sections; o != NULL; o = o->next)
8406 {
8407 if ((o->flags & SEC_RELOC) == 0)
8408 continue;
8409
8410 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
8411 elf_section_data (o)->rel_count,
8412 elf_section_data (o)->rel_hashes);
8413 if (elf_section_data (o)->rel_hdr2 != NULL)
8414 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
8415 elf_section_data (o)->rel_count2,
8416 (elf_section_data (o)->rel_hashes
8417 + elf_section_data (o)->rel_count));
8418
8419 /* Set the reloc_count field to 0 to prevent write_relocs from
8420 trying to swap the relocs out itself. */
8421 o->reloc_count = 0;
8422 }
8423
8424 if (dynamic && info->combreloc && dynobj != NULL)
8425 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
8426
8427 /* If we are linking against a dynamic object, or generating a
8428 shared library, finish up the dynamic linking information. */
8429 if (dynamic)
8430 {
8431 bfd_byte *dyncon, *dynconend;
8432
8433 /* Fix up .dynamic entries. */
8434 o = bfd_get_section_by_name (dynobj, ".dynamic");
8435 BFD_ASSERT (o != NULL);
8436
8437 dyncon = o->contents;
8438 dynconend = o->contents + o->size;
8439 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
8440 {
8441 Elf_Internal_Dyn dyn;
8442 const char *name;
8443 unsigned int type;
8444
8445 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
8446
8447 switch (dyn.d_tag)
8448 {
8449 default:
8450 continue;
8451 case DT_NULL:
8452 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
8453 {
8454 switch (elf_section_data (reldyn)->this_hdr.sh_type)
8455 {
8456 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
8457 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
8458 default: continue;
8459 }
8460 dyn.d_un.d_val = relativecount;
8461 relativecount = 0;
8462 break;
8463 }
8464 continue;
8465
8466 case DT_INIT:
8467 name = info->init_function;
8468 goto get_sym;
8469 case DT_FINI:
8470 name = info->fini_function;
8471 get_sym:
8472 {
8473 struct elf_link_hash_entry *h;
8474
8475 h = elf_link_hash_lookup (elf_hash_table (info), name,
8476 FALSE, FALSE, TRUE);
8477 if (h != NULL
8478 && (h->root.type == bfd_link_hash_defined
8479 || h->root.type == bfd_link_hash_defweak))
8480 {
8481 dyn.d_un.d_val = h->root.u.def.value;
8482 o = h->root.u.def.section;
8483 if (o->output_section != NULL)
8484 dyn.d_un.d_val += (o->output_section->vma
8485 + o->output_offset);
8486 else
8487 {
8488 /* The symbol is imported from another shared
8489 library and does not apply to this one. */
8490 dyn.d_un.d_val = 0;
8491 }
8492 break;
8493 }
8494 }
8495 continue;
8496
8497 case DT_PREINIT_ARRAYSZ:
8498 name = ".preinit_array";
8499 goto get_size;
8500 case DT_INIT_ARRAYSZ:
8501 name = ".init_array";
8502 goto get_size;
8503 case DT_FINI_ARRAYSZ:
8504 name = ".fini_array";
8505 get_size:
8506 o = bfd_get_section_by_name (abfd, name);
8507 if (o == NULL)
8508 {
8509 (*_bfd_error_handler)
8510 (_("%B: could not find output section %s"), abfd, name);
8511 goto error_return;
8512 }
8513 if (o->size == 0)
8514 (*_bfd_error_handler)
8515 (_("warning: %s section has zero size"), name);
8516 dyn.d_un.d_val = o->size;
8517 break;
8518
8519 case DT_PREINIT_ARRAY:
8520 name = ".preinit_array";
8521 goto get_vma;
8522 case DT_INIT_ARRAY:
8523 name = ".init_array";
8524 goto get_vma;
8525 case DT_FINI_ARRAY:
8526 name = ".fini_array";
8527 goto get_vma;
8528
8529 case DT_HASH:
8530 name = ".hash";
8531 goto get_vma;
8532 case DT_STRTAB:
8533 name = ".dynstr";
8534 goto get_vma;
8535 case DT_SYMTAB:
8536 name = ".dynsym";
8537 goto get_vma;
8538 case DT_VERDEF:
8539 name = ".gnu.version_d";
8540 goto get_vma;
8541 case DT_VERNEED:
8542 name = ".gnu.version_r";
8543 goto get_vma;
8544 case DT_VERSYM:
8545 name = ".gnu.version";
8546 get_vma:
8547 o = bfd_get_section_by_name (abfd, name);
8548 if (o == NULL)
8549 {
8550 (*_bfd_error_handler)
8551 (_("%B: could not find output section %s"), abfd, name);
8552 goto error_return;
8553 }
8554 dyn.d_un.d_ptr = o->vma;
8555 break;
8556
8557 case DT_REL:
8558 case DT_RELA:
8559 case DT_RELSZ:
8560 case DT_RELASZ:
8561 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
8562 type = SHT_REL;
8563 else
8564 type = SHT_RELA;
8565 dyn.d_un.d_val = 0;
8566 for (i = 1; i < elf_numsections (abfd); i++)
8567 {
8568 Elf_Internal_Shdr *hdr;
8569
8570 hdr = elf_elfsections (abfd)[i];
8571 if (hdr->sh_type == type
8572 && (hdr->sh_flags & SHF_ALLOC) != 0)
8573 {
8574 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
8575 dyn.d_un.d_val += hdr->sh_size;
8576 else
8577 {
8578 if (dyn.d_un.d_val == 0
8579 || hdr->sh_addr < dyn.d_un.d_val)
8580 dyn.d_un.d_val = hdr->sh_addr;
8581 }
8582 }
8583 }
8584 break;
8585 }
8586 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
8587 }
8588 }
8589
8590 /* If we have created any dynamic sections, then output them. */
8591 if (dynobj != NULL)
8592 {
8593 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
8594 goto error_return;
8595
8596 for (o = dynobj->sections; o != NULL; o = o->next)
8597 {
8598 if ((o->flags & SEC_HAS_CONTENTS) == 0
8599 || o->size == 0
8600 || o->output_section == bfd_abs_section_ptr)
8601 continue;
8602 if ((o->flags & SEC_LINKER_CREATED) == 0)
8603 {
8604 /* At this point, we are only interested in sections
8605 created by _bfd_elf_link_create_dynamic_sections. */
8606 continue;
8607 }
8608 if (elf_hash_table (info)->stab_info.stabstr == o)
8609 continue;
8610 if (elf_hash_table (info)->eh_info.hdr_sec == o)
8611 continue;
8612 if ((elf_section_data (o->output_section)->this_hdr.sh_type
8613 != SHT_STRTAB)
8614 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
8615 {
8616 if (! bfd_set_section_contents (abfd, o->output_section,
8617 o->contents,
8618 (file_ptr) o->output_offset,
8619 o->size))
8620 goto error_return;
8621 }
8622 else
8623 {
8624 /* The contents of the .dynstr section are actually in a
8625 stringtab. */
8626 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
8627 if (bfd_seek (abfd, off, SEEK_SET) != 0
8628 || ! _bfd_elf_strtab_emit (abfd,
8629 elf_hash_table (info)->dynstr))
8630 goto error_return;
8631 }
8632 }
8633 }
8634
8635 if (info->relocatable)
8636 {
8637 bfd_boolean failed = FALSE;
8638
8639 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
8640 if (failed)
8641 goto error_return;
8642 }
8643
8644 /* If we have optimized stabs strings, output them. */
8645 if (elf_hash_table (info)->stab_info.stabstr != NULL)
8646 {
8647 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
8648 goto error_return;
8649 }
8650
8651 if (info->eh_frame_hdr)
8652 {
8653 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
8654 goto error_return;
8655 }
8656
8657 if (finfo.symstrtab != NULL)
8658 _bfd_stringtab_free (finfo.symstrtab);
8659 if (finfo.contents != NULL)
8660 free (finfo.contents);
8661 if (finfo.external_relocs != NULL)
8662 free (finfo.external_relocs);
8663 if (finfo.internal_relocs != NULL)
8664 free (finfo.internal_relocs);
8665 if (finfo.external_syms != NULL)
8666 free (finfo.external_syms);
8667 if (finfo.locsym_shndx != NULL)
8668 free (finfo.locsym_shndx);
8669 if (finfo.internal_syms != NULL)
8670 free (finfo.internal_syms);
8671 if (finfo.indices != NULL)
8672 free (finfo.indices);
8673 if (finfo.sections != NULL)
8674 free (finfo.sections);
8675 if (finfo.symbuf != NULL)
8676 free (finfo.symbuf);
8677 if (finfo.symshndxbuf != NULL)
8678 free (finfo.symshndxbuf);
8679 for (o = abfd->sections; o != NULL; o = o->next)
8680 {
8681 if ((o->flags & SEC_RELOC) != 0
8682 && elf_section_data (o)->rel_hashes != NULL)
8683 free (elf_section_data (o)->rel_hashes);
8684 }
8685
8686 elf_tdata (abfd)->linker = TRUE;
8687
8688 return TRUE;
8689
8690 error_return:
8691 if (finfo.symstrtab != NULL)
8692 _bfd_stringtab_free (finfo.symstrtab);
8693 if (finfo.contents != NULL)
8694 free (finfo.contents);
8695 if (finfo.external_relocs != NULL)
8696 free (finfo.external_relocs);
8697 if (finfo.internal_relocs != NULL)
8698 free (finfo.internal_relocs);
8699 if (finfo.external_syms != NULL)
8700 free (finfo.external_syms);
8701 if (finfo.locsym_shndx != NULL)
8702 free (finfo.locsym_shndx);
8703 if (finfo.internal_syms != NULL)
8704 free (finfo.internal_syms);
8705 if (finfo.indices != NULL)
8706 free (finfo.indices);
8707 if (finfo.sections != NULL)
8708 free (finfo.sections);
8709 if (finfo.symbuf != NULL)
8710 free (finfo.symbuf);
8711 if (finfo.symshndxbuf != NULL)
8712 free (finfo.symshndxbuf);
8713 for (o = abfd->sections; o != NULL; o = o->next)
8714 {
8715 if ((o->flags & SEC_RELOC) != 0
8716 && elf_section_data (o)->rel_hashes != NULL)
8717 free (elf_section_data (o)->rel_hashes);
8718 }
8719
8720 return FALSE;
8721 }
8722 \f
8723 /* Garbage collect unused sections. */
8724
8725 /* The mark phase of garbage collection. For a given section, mark
8726 it and any sections in this section's group, and all the sections
8727 which define symbols to which it refers. */
8728
8729 typedef asection * (*gc_mark_hook_fn)
8730 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
8731 struct elf_link_hash_entry *, Elf_Internal_Sym *);
8732
8733 bfd_boolean
8734 _bfd_elf_gc_mark (struct bfd_link_info *info,
8735 asection *sec,
8736 gc_mark_hook_fn gc_mark_hook)
8737 {
8738 bfd_boolean ret;
8739 bfd_boolean is_eh;
8740 asection *group_sec;
8741
8742 sec->gc_mark = 1;
8743
8744 /* Mark all the sections in the group. */
8745 group_sec = elf_section_data (sec)->next_in_group;
8746 if (group_sec && !group_sec->gc_mark)
8747 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
8748 return FALSE;
8749
8750 /* Look through the section relocs. */
8751 ret = TRUE;
8752 is_eh = strcmp (sec->name, ".eh_frame") == 0;
8753 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
8754 {
8755 Elf_Internal_Rela *relstart, *rel, *relend;
8756 Elf_Internal_Shdr *symtab_hdr;
8757 struct elf_link_hash_entry **sym_hashes;
8758 size_t nlocsyms;
8759 size_t extsymoff;
8760 bfd *input_bfd = sec->owner;
8761 const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
8762 Elf_Internal_Sym *isym = NULL;
8763 int r_sym_shift;
8764
8765 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8766 sym_hashes = elf_sym_hashes (input_bfd);
8767
8768 /* Read the local symbols. */
8769 if (elf_bad_symtab (input_bfd))
8770 {
8771 nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym;
8772 extsymoff = 0;
8773 }
8774 else
8775 extsymoff = nlocsyms = symtab_hdr->sh_info;
8776
8777 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
8778 if (isym == NULL && nlocsyms != 0)
8779 {
8780 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
8781 NULL, NULL, NULL);
8782 if (isym == NULL)
8783 return FALSE;
8784 }
8785
8786 /* Read the relocations. */
8787 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
8788 info->keep_memory);
8789 if (relstart == NULL)
8790 {
8791 ret = FALSE;
8792 goto out1;
8793 }
8794 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
8795
8796 if (bed->s->arch_size == 32)
8797 r_sym_shift = 8;
8798 else
8799 r_sym_shift = 32;
8800
8801 for (rel = relstart; rel < relend; rel++)
8802 {
8803 unsigned long r_symndx;
8804 asection *rsec;
8805 struct elf_link_hash_entry *h;
8806
8807 r_symndx = rel->r_info >> r_sym_shift;
8808 if (r_symndx == 0)
8809 continue;
8810
8811 if (r_symndx >= nlocsyms
8812 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
8813 {
8814 h = sym_hashes[r_symndx - extsymoff];
8815 while (h->root.type == bfd_link_hash_indirect
8816 || h->root.type == bfd_link_hash_warning)
8817 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8818 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
8819 }
8820 else
8821 {
8822 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
8823 }
8824
8825 if (rsec && !rsec->gc_mark)
8826 {
8827 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
8828 rsec->gc_mark = 1;
8829 else if (is_eh)
8830 rsec->gc_mark_from_eh = 1;
8831 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
8832 {
8833 ret = FALSE;
8834 goto out2;
8835 }
8836 }
8837 }
8838
8839 out2:
8840 if (elf_section_data (sec)->relocs != relstart)
8841 free (relstart);
8842 out1:
8843 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
8844 {
8845 if (! info->keep_memory)
8846 free (isym);
8847 else
8848 symtab_hdr->contents = (unsigned char *) isym;
8849 }
8850 }
8851
8852 return ret;
8853 }
8854
8855 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8856
8857 struct elf_gc_sweep_symbol_info {
8858 struct bfd_link_info *info;
8859 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
8860 bfd_boolean);
8861 };
8862
8863 static bfd_boolean
8864 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
8865 {
8866 if (h->root.type == bfd_link_hash_warning)
8867 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8868
8869 if ((h->root.type == bfd_link_hash_defined
8870 || h->root.type == bfd_link_hash_defweak)
8871 && !h->root.u.def.section->gc_mark
8872 && !(h->root.u.def.section->owner->flags & DYNAMIC))
8873 {
8874 struct elf_gc_sweep_symbol_info *inf = data;
8875 (*inf->hide_symbol) (inf->info, h, TRUE);
8876 }
8877
8878 return TRUE;
8879 }
8880
8881 /* The sweep phase of garbage collection. Remove all garbage sections. */
8882
8883 typedef bfd_boolean (*gc_sweep_hook_fn)
8884 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
8885
8886 static bfd_boolean
8887 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
8888 {
8889 bfd *sub;
8890 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8891 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
8892 unsigned long section_sym_count;
8893 struct elf_gc_sweep_symbol_info sweep_info;
8894
8895 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8896 {
8897 asection *o;
8898
8899 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
8900 continue;
8901
8902 for (o = sub->sections; o != NULL; o = o->next)
8903 {
8904 /* Keep debug and special sections. */
8905 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
8906 || (o->flags & (SEC_ALLOC | SEC_LOAD)) == 0)
8907 o->gc_mark = 1;
8908
8909 if (o->gc_mark)
8910 continue;
8911
8912 /* Skip sweeping sections already excluded. */
8913 if (o->flags & SEC_EXCLUDE)
8914 continue;
8915
8916 /* Since this is early in the link process, it is simple
8917 to remove a section from the output. */
8918 o->flags |= SEC_EXCLUDE;
8919
8920 /* But we also have to update some of the relocation
8921 info we collected before. */
8922 if (gc_sweep_hook
8923 && (o->flags & SEC_RELOC) != 0
8924 && o->reloc_count > 0
8925 && !bfd_is_abs_section (o->output_section))
8926 {
8927 Elf_Internal_Rela *internal_relocs;
8928 bfd_boolean r;
8929
8930 internal_relocs
8931 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
8932 info->keep_memory);
8933 if (internal_relocs == NULL)
8934 return FALSE;
8935
8936 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
8937
8938 if (elf_section_data (o)->relocs != internal_relocs)
8939 free (internal_relocs);
8940
8941 if (!r)
8942 return FALSE;
8943 }
8944 }
8945 }
8946
8947 /* Remove the symbols that were in the swept sections from the dynamic
8948 symbol table. GCFIXME: Anyone know how to get them out of the
8949 static symbol table as well? */
8950 sweep_info.info = info;
8951 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
8952 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
8953 &sweep_info);
8954
8955 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
8956 return TRUE;
8957 }
8958
8959 /* Propagate collected vtable information. This is called through
8960 elf_link_hash_traverse. */
8961
8962 static bfd_boolean
8963 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
8964 {
8965 if (h->root.type == bfd_link_hash_warning)
8966 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8967
8968 /* Those that are not vtables. */
8969 if (h->vtable == NULL || h->vtable->parent == NULL)
8970 return TRUE;
8971
8972 /* Those vtables that do not have parents, we cannot merge. */
8973 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
8974 return TRUE;
8975
8976 /* If we've already been done, exit. */
8977 if (h->vtable->used && h->vtable->used[-1])
8978 return TRUE;
8979
8980 /* Make sure the parent's table is up to date. */
8981 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
8982
8983 if (h->vtable->used == NULL)
8984 {
8985 /* None of this table's entries were referenced. Re-use the
8986 parent's table. */
8987 h->vtable->used = h->vtable->parent->vtable->used;
8988 h->vtable->size = h->vtable->parent->vtable->size;
8989 }
8990 else
8991 {
8992 size_t n;
8993 bfd_boolean *cu, *pu;
8994
8995 /* Or the parent's entries into ours. */
8996 cu = h->vtable->used;
8997 cu[-1] = TRUE;
8998 pu = h->vtable->parent->vtable->used;
8999 if (pu != NULL)
9000 {
9001 const struct elf_backend_data *bed;
9002 unsigned int log_file_align;
9003
9004 bed = get_elf_backend_data (h->root.u.def.section->owner);
9005 log_file_align = bed->s->log_file_align;
9006 n = h->vtable->parent->vtable->size >> log_file_align;
9007 while (n--)
9008 {
9009 if (*pu)
9010 *cu = TRUE;
9011 pu++;
9012 cu++;
9013 }
9014 }
9015 }
9016
9017 return TRUE;
9018 }
9019
9020 static bfd_boolean
9021 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
9022 {
9023 asection *sec;
9024 bfd_vma hstart, hend;
9025 Elf_Internal_Rela *relstart, *relend, *rel;
9026 const struct elf_backend_data *bed;
9027 unsigned int log_file_align;
9028
9029 if (h->root.type == bfd_link_hash_warning)
9030 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9031
9032 /* Take care of both those symbols that do not describe vtables as
9033 well as those that are not loaded. */
9034 if (h->vtable == NULL || h->vtable->parent == NULL)
9035 return TRUE;
9036
9037 BFD_ASSERT (h->root.type == bfd_link_hash_defined
9038 || h->root.type == bfd_link_hash_defweak);
9039
9040 sec = h->root.u.def.section;
9041 hstart = h->root.u.def.value;
9042 hend = hstart + h->size;
9043
9044 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
9045 if (!relstart)
9046 return *(bfd_boolean *) okp = FALSE;
9047 bed = get_elf_backend_data (sec->owner);
9048 log_file_align = bed->s->log_file_align;
9049
9050 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
9051
9052 for (rel = relstart; rel < relend; ++rel)
9053 if (rel->r_offset >= hstart && rel->r_offset < hend)
9054 {
9055 /* If the entry is in use, do nothing. */
9056 if (h->vtable->used
9057 && (rel->r_offset - hstart) < h->vtable->size)
9058 {
9059 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
9060 if (h->vtable->used[entry])
9061 continue;
9062 }
9063 /* Otherwise, kill it. */
9064 rel->r_offset = rel->r_info = rel->r_addend = 0;
9065 }
9066
9067 return TRUE;
9068 }
9069
9070 /* Mark sections containing dynamically referenced symbols. When
9071 building shared libraries, we must assume that any visible symbol is
9072 referenced. */
9073
9074 static bfd_boolean
9075 elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
9076 {
9077 struct bfd_link_info *info = (struct bfd_link_info *) inf;
9078
9079 if (h->root.type == bfd_link_hash_warning)
9080 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9081
9082 if ((h->root.type == bfd_link_hash_defined
9083 || h->root.type == bfd_link_hash_defweak)
9084 && (h->ref_dynamic
9085 || (info->shared
9086 && h->def_regular
9087 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
9088 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN)))
9089 h->root.u.def.section->flags |= SEC_KEEP;
9090
9091 return TRUE;
9092 }
9093
9094 /* Do mark and sweep of unused sections. */
9095
9096 bfd_boolean
9097 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
9098 {
9099 bfd_boolean ok = TRUE;
9100 bfd *sub;
9101 asection * (*gc_mark_hook)
9102 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
9103 struct elf_link_hash_entry *h, Elf_Internal_Sym *);
9104
9105 if (!get_elf_backend_data (abfd)->can_gc_sections
9106 || info->relocatable
9107 || info->emitrelocations
9108 || !is_elf_hash_table (info->hash))
9109 {
9110 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
9111 return TRUE;
9112 }
9113
9114 /* Apply transitive closure to the vtable entry usage info. */
9115 elf_link_hash_traverse (elf_hash_table (info),
9116 elf_gc_propagate_vtable_entries_used,
9117 &ok);
9118 if (!ok)
9119 return FALSE;
9120
9121 /* Kill the vtable relocations that were not used. */
9122 elf_link_hash_traverse (elf_hash_table (info),
9123 elf_gc_smash_unused_vtentry_relocs,
9124 &ok);
9125 if (!ok)
9126 return FALSE;
9127
9128 /* Mark dynamically referenced symbols. */
9129 if (elf_hash_table (info)->dynamic_sections_created)
9130 elf_link_hash_traverse (elf_hash_table (info),
9131 elf_gc_mark_dynamic_ref_symbol,
9132 info);
9133
9134 /* Grovel through relocs to find out who stays ... */
9135 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
9136 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
9137 {
9138 asection *o;
9139
9140 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
9141 continue;
9142
9143 for (o = sub->sections; o != NULL; o = o->next)
9144 if ((o->flags & SEC_KEEP) != 0 && !o->gc_mark)
9145 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
9146 return FALSE;
9147 }
9148
9149 /* ... again for sections marked from eh_frame. */
9150 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
9151 {
9152 asection *o;
9153
9154 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
9155 continue;
9156
9157 /* Keep .gcc_except_table.* if the associated .text.* is
9158 marked. This isn't very nice, but the proper solution,
9159 splitting .eh_frame up and using comdat doesn't pan out
9160 easily due to needing special relocs to handle the
9161 difference of two symbols in separate sections.
9162 Don't keep code sections referenced by .eh_frame. */
9163 for (o = sub->sections; o != NULL; o = o->next)
9164 if (!o->gc_mark && o->gc_mark_from_eh && (o->flags & SEC_CODE) == 0)
9165 {
9166 if (strncmp (o->name, ".gcc_except_table.", 18) == 0)
9167 {
9168 unsigned long len;
9169 char *fn_name;
9170 asection *fn_text;
9171
9172 len = strlen (o->name + 18) + 1;
9173 fn_name = bfd_malloc (len + 6);
9174 if (fn_name == NULL)
9175 return FALSE;
9176 memcpy (fn_name, ".text.", 6);
9177 memcpy (fn_name + 6, o->name + 18, len);
9178 fn_text = bfd_get_section_by_name (sub, fn_name);
9179 free (fn_name);
9180 if (fn_text == NULL || !fn_text->gc_mark)
9181 continue;
9182 }
9183
9184 /* If not using specially named exception table section,
9185 then keep whatever we are using. */
9186 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
9187 return FALSE;
9188 }
9189 }
9190
9191 /* ... and mark SEC_EXCLUDE for those that go. */
9192 return elf_gc_sweep (abfd, info);
9193 }
9194 \f
9195 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
9196
9197 bfd_boolean
9198 bfd_elf_gc_record_vtinherit (bfd *abfd,
9199 asection *sec,
9200 struct elf_link_hash_entry *h,
9201 bfd_vma offset)
9202 {
9203 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
9204 struct elf_link_hash_entry **search, *child;
9205 bfd_size_type extsymcount;
9206 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9207
9208 /* The sh_info field of the symtab header tells us where the
9209 external symbols start. We don't care about the local symbols at
9210 this point. */
9211 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
9212 if (!elf_bad_symtab (abfd))
9213 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
9214
9215 sym_hashes = elf_sym_hashes (abfd);
9216 sym_hashes_end = sym_hashes + extsymcount;
9217
9218 /* Hunt down the child symbol, which is in this section at the same
9219 offset as the relocation. */
9220 for (search = sym_hashes; search != sym_hashes_end; ++search)
9221 {
9222 if ((child = *search) != NULL
9223 && (child->root.type == bfd_link_hash_defined
9224 || child->root.type == bfd_link_hash_defweak)
9225 && child->root.u.def.section == sec
9226 && child->root.u.def.value == offset)
9227 goto win;
9228 }
9229
9230 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
9231 abfd, sec, (unsigned long) offset);
9232 bfd_set_error (bfd_error_invalid_operation);
9233 return FALSE;
9234
9235 win:
9236 if (!child->vtable)
9237 {
9238 child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable));
9239 if (!child->vtable)
9240 return FALSE;
9241 }
9242 if (!h)
9243 {
9244 /* This *should* only be the absolute section. It could potentially
9245 be that someone has defined a non-global vtable though, which
9246 would be bad. It isn't worth paging in the local symbols to be
9247 sure though; that case should simply be handled by the assembler. */
9248
9249 child->vtable->parent = (struct elf_link_hash_entry *) -1;
9250 }
9251 else
9252 child->vtable->parent = h;
9253
9254 return TRUE;
9255 }
9256
9257 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
9258
9259 bfd_boolean
9260 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
9261 asection *sec ATTRIBUTE_UNUSED,
9262 struct elf_link_hash_entry *h,
9263 bfd_vma addend)
9264 {
9265 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9266 unsigned int log_file_align = bed->s->log_file_align;
9267
9268 if (!h->vtable)
9269 {
9270 h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable));
9271 if (!h->vtable)
9272 return FALSE;
9273 }
9274
9275 if (addend >= h->vtable->size)
9276 {
9277 size_t size, bytes, file_align;
9278 bfd_boolean *ptr = h->vtable->used;
9279
9280 /* While the symbol is undefined, we have to be prepared to handle
9281 a zero size. */
9282 file_align = 1 << log_file_align;
9283 if (h->root.type == bfd_link_hash_undefined)
9284 size = addend + file_align;
9285 else
9286 {
9287 size = h->size;
9288 if (addend >= size)
9289 {
9290 /* Oops! We've got a reference past the defined end of
9291 the table. This is probably a bug -- shall we warn? */
9292 size = addend + file_align;
9293 }
9294 }
9295 size = (size + file_align - 1) & -file_align;
9296
9297 /* Allocate one extra entry for use as a "done" flag for the
9298 consolidation pass. */
9299 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
9300
9301 if (ptr)
9302 {
9303 ptr = bfd_realloc (ptr - 1, bytes);
9304
9305 if (ptr != NULL)
9306 {
9307 size_t oldbytes;
9308
9309 oldbytes = (((h->vtable->size >> log_file_align) + 1)
9310 * sizeof (bfd_boolean));
9311 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
9312 }
9313 }
9314 else
9315 ptr = bfd_zmalloc (bytes);
9316
9317 if (ptr == NULL)
9318 return FALSE;
9319
9320 /* And arrange for that done flag to be at index -1. */
9321 h->vtable->used = ptr + 1;
9322 h->vtable->size = size;
9323 }
9324
9325 h->vtable->used[addend >> log_file_align] = TRUE;
9326
9327 return TRUE;
9328 }
9329
9330 struct alloc_got_off_arg {
9331 bfd_vma gotoff;
9332 unsigned int got_elt_size;
9333 };
9334
9335 /* We need a special top-level link routine to convert got reference counts
9336 to real got offsets. */
9337
9338 static bfd_boolean
9339 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
9340 {
9341 struct alloc_got_off_arg *gofarg = arg;
9342
9343 if (h->root.type == bfd_link_hash_warning)
9344 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9345
9346 if (h->got.refcount > 0)
9347 {
9348 h->got.offset = gofarg->gotoff;
9349 gofarg->gotoff += gofarg->got_elt_size;
9350 }
9351 else
9352 h->got.offset = (bfd_vma) -1;
9353
9354 return TRUE;
9355 }
9356
9357 /* And an accompanying bit to work out final got entry offsets once
9358 we're done. Should be called from final_link. */
9359
9360 bfd_boolean
9361 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
9362 struct bfd_link_info *info)
9363 {
9364 bfd *i;
9365 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9366 bfd_vma gotoff;
9367 unsigned int got_elt_size = bed->s->arch_size / 8;
9368 struct alloc_got_off_arg gofarg;
9369
9370 if (! is_elf_hash_table (info->hash))
9371 return FALSE;
9372
9373 /* The GOT offset is relative to the .got section, but the GOT header is
9374 put into the .got.plt section, if the backend uses it. */
9375 if (bed->want_got_plt)
9376 gotoff = 0;
9377 else
9378 gotoff = bed->got_header_size;
9379
9380 /* Do the local .got entries first. */
9381 for (i = info->input_bfds; i; i = i->link_next)
9382 {
9383 bfd_signed_vma *local_got;
9384 bfd_size_type j, locsymcount;
9385 Elf_Internal_Shdr *symtab_hdr;
9386
9387 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
9388 continue;
9389
9390 local_got = elf_local_got_refcounts (i);
9391 if (!local_got)
9392 continue;
9393
9394 symtab_hdr = &elf_tdata (i)->symtab_hdr;
9395 if (elf_bad_symtab (i))
9396 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9397 else
9398 locsymcount = symtab_hdr->sh_info;
9399
9400 for (j = 0; j < locsymcount; ++j)
9401 {
9402 if (local_got[j] > 0)
9403 {
9404 local_got[j] = gotoff;
9405 gotoff += got_elt_size;
9406 }
9407 else
9408 local_got[j] = (bfd_vma) -1;
9409 }
9410 }
9411
9412 /* Then the global .got entries. .plt refcounts are handled by
9413 adjust_dynamic_symbol */
9414 gofarg.gotoff = gotoff;
9415 gofarg.got_elt_size = got_elt_size;
9416 elf_link_hash_traverse (elf_hash_table (info),
9417 elf_gc_allocate_got_offsets,
9418 &gofarg);
9419 return TRUE;
9420 }
9421
9422 /* Many folk need no more in the way of final link than this, once
9423 got entry reference counting is enabled. */
9424
9425 bfd_boolean
9426 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
9427 {
9428 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
9429 return FALSE;
9430
9431 /* Invoke the regular ELF backend linker to do all the work. */
9432 return bfd_elf_final_link (abfd, info);
9433 }
9434
9435 bfd_boolean
9436 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
9437 {
9438 struct elf_reloc_cookie *rcookie = cookie;
9439
9440 if (rcookie->bad_symtab)
9441 rcookie->rel = rcookie->rels;
9442
9443 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
9444 {
9445 unsigned long r_symndx;
9446
9447 if (! rcookie->bad_symtab)
9448 if (rcookie->rel->r_offset > offset)
9449 return FALSE;
9450 if (rcookie->rel->r_offset != offset)
9451 continue;
9452
9453 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
9454 if (r_symndx == SHN_UNDEF)
9455 return TRUE;
9456
9457 if (r_symndx >= rcookie->locsymcount
9458 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
9459 {
9460 struct elf_link_hash_entry *h;
9461
9462 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
9463
9464 while (h->root.type == bfd_link_hash_indirect
9465 || h->root.type == bfd_link_hash_warning)
9466 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9467
9468 if ((h->root.type == bfd_link_hash_defined
9469 || h->root.type == bfd_link_hash_defweak)
9470 && elf_discarded_section (h->root.u.def.section))
9471 return TRUE;
9472 else
9473 return FALSE;
9474 }
9475 else
9476 {
9477 /* It's not a relocation against a global symbol,
9478 but it could be a relocation against a local
9479 symbol for a discarded section. */
9480 asection *isec;
9481 Elf_Internal_Sym *isym;
9482
9483 /* Need to: get the symbol; get the section. */
9484 isym = &rcookie->locsyms[r_symndx];
9485 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
9486 {
9487 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
9488 if (isec != NULL && elf_discarded_section (isec))
9489 return TRUE;
9490 }
9491 }
9492 return FALSE;
9493 }
9494 return FALSE;
9495 }
9496
9497 /* Discard unneeded references to discarded sections.
9498 Returns TRUE if any section's size was changed. */
9499 /* This function assumes that the relocations are in sorted order,
9500 which is true for all known assemblers. */
9501
9502 bfd_boolean
9503 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
9504 {
9505 struct elf_reloc_cookie cookie;
9506 asection *stab, *eh;
9507 Elf_Internal_Shdr *symtab_hdr;
9508 const struct elf_backend_data *bed;
9509 bfd *abfd;
9510 unsigned int count;
9511 bfd_boolean ret = FALSE;
9512
9513 if (info->traditional_format
9514 || !is_elf_hash_table (info->hash))
9515 return FALSE;
9516
9517 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
9518 {
9519 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
9520 continue;
9521
9522 bed = get_elf_backend_data (abfd);
9523
9524 if ((abfd->flags & DYNAMIC) != 0)
9525 continue;
9526
9527 eh = bfd_get_section_by_name (abfd, ".eh_frame");
9528 if (info->relocatable
9529 || (eh != NULL
9530 && (eh->size == 0
9531 || bfd_is_abs_section (eh->output_section))))
9532 eh = NULL;
9533
9534 stab = bfd_get_section_by_name (abfd, ".stab");
9535 if (stab != NULL
9536 && (stab->size == 0
9537 || bfd_is_abs_section (stab->output_section)
9538 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
9539 stab = NULL;
9540
9541 if (stab == NULL
9542 && eh == NULL
9543 && bed->elf_backend_discard_info == NULL)
9544 continue;
9545
9546 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9547 cookie.abfd = abfd;
9548 cookie.sym_hashes = elf_sym_hashes (abfd);
9549 cookie.bad_symtab = elf_bad_symtab (abfd);
9550 if (cookie.bad_symtab)
9551 {
9552 cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9553 cookie.extsymoff = 0;
9554 }
9555 else
9556 {
9557 cookie.locsymcount = symtab_hdr->sh_info;
9558 cookie.extsymoff = symtab_hdr->sh_info;
9559 }
9560
9561 if (bed->s->arch_size == 32)
9562 cookie.r_sym_shift = 8;
9563 else
9564 cookie.r_sym_shift = 32;
9565
9566 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
9567 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
9568 {
9569 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9570 cookie.locsymcount, 0,
9571 NULL, NULL, NULL);
9572 if (cookie.locsyms == NULL)
9573 return FALSE;
9574 }
9575
9576 if (stab != NULL)
9577 {
9578 cookie.rels = NULL;
9579 count = stab->reloc_count;
9580 if (count != 0)
9581 cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
9582 info->keep_memory);
9583 if (cookie.rels != NULL)
9584 {
9585 cookie.rel = cookie.rels;
9586 cookie.relend = cookie.rels;
9587 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9588 if (_bfd_discard_section_stabs (abfd, stab,
9589 elf_section_data (stab)->sec_info,
9590 bfd_elf_reloc_symbol_deleted_p,
9591 &cookie))
9592 ret = TRUE;
9593 if (elf_section_data (stab)->relocs != cookie.rels)
9594 free (cookie.rels);
9595 }
9596 }
9597
9598 if (eh != NULL)
9599 {
9600 cookie.rels = NULL;
9601 count = eh->reloc_count;
9602 if (count != 0)
9603 cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
9604 info->keep_memory);
9605 cookie.rel = cookie.rels;
9606 cookie.relend = cookie.rels;
9607 if (cookie.rels != NULL)
9608 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9609
9610 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
9611 bfd_elf_reloc_symbol_deleted_p,
9612 &cookie))
9613 ret = TRUE;
9614
9615 if (cookie.rels != NULL
9616 && elf_section_data (eh)->relocs != cookie.rels)
9617 free (cookie.rels);
9618 }
9619
9620 if (bed->elf_backend_discard_info != NULL
9621 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
9622 ret = TRUE;
9623
9624 if (cookie.locsyms != NULL
9625 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
9626 {
9627 if (! info->keep_memory)
9628 free (cookie.locsyms);
9629 else
9630 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
9631 }
9632 }
9633
9634 if (info->eh_frame_hdr
9635 && !info->relocatable
9636 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
9637 ret = TRUE;
9638
9639 return ret;
9640 }
9641
9642 void
9643 _bfd_elf_section_already_linked (bfd *abfd, struct bfd_section * sec)
9644 {
9645 flagword flags;
9646 const char *name, *p;
9647 struct bfd_section_already_linked *l;
9648 struct bfd_section_already_linked_hash_entry *already_linked_list;
9649 asection *group;
9650
9651 /* A single member comdat group section may be discarded by a
9652 linkonce section. See below. */
9653 if (sec->output_section == bfd_abs_section_ptr)
9654 return;
9655
9656 flags = sec->flags;
9657
9658 /* Check if it belongs to a section group. */
9659 group = elf_sec_group (sec);
9660
9661 /* Return if it isn't a linkonce section nor a member of a group. A
9662 comdat group section also has SEC_LINK_ONCE set. */
9663 if ((flags & SEC_LINK_ONCE) == 0 && group == NULL)
9664 return;
9665
9666 if (group)
9667 {
9668 /* If this is the member of a single member comdat group, check if
9669 the group should be discarded. */
9670 if (elf_next_in_group (sec) == sec
9671 && (group->flags & SEC_LINK_ONCE) != 0)
9672 sec = group;
9673 else
9674 return;
9675 }
9676
9677 /* FIXME: When doing a relocatable link, we may have trouble
9678 copying relocations in other sections that refer to local symbols
9679 in the section being discarded. Those relocations will have to
9680 be converted somehow; as of this writing I'm not sure that any of
9681 the backends handle that correctly.
9682
9683 It is tempting to instead not discard link once sections when
9684 doing a relocatable link (technically, they should be discarded
9685 whenever we are building constructors). However, that fails,
9686 because the linker winds up combining all the link once sections
9687 into a single large link once section, which defeats the purpose
9688 of having link once sections in the first place.
9689
9690 Also, not merging link once sections in a relocatable link
9691 causes trouble for MIPS ELF, which relies on link once semantics
9692 to handle the .reginfo section correctly. */
9693
9694 name = bfd_get_section_name (abfd, sec);
9695
9696 if (strncmp (name, ".gnu.linkonce.", sizeof (".gnu.linkonce.") - 1) == 0
9697 && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
9698 p++;
9699 else
9700 p = name;
9701
9702 already_linked_list = bfd_section_already_linked_table_lookup (p);
9703
9704 for (l = already_linked_list->entry; l != NULL; l = l->next)
9705 {
9706 /* We may have 3 different sections on the list: group section,
9707 comdat section and linkonce section. SEC may be a linkonce or
9708 group section. We match a group section with a group section,
9709 a linkonce section with a linkonce section, and ignore comdat
9710 section. */
9711 if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
9712 && strcmp (name, l->sec->name) == 0
9713 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
9714 {
9715 /* The section has already been linked. See if we should
9716 issue a warning. */
9717 switch (flags & SEC_LINK_DUPLICATES)
9718 {
9719 default:
9720 abort ();
9721
9722 case SEC_LINK_DUPLICATES_DISCARD:
9723 break;
9724
9725 case SEC_LINK_DUPLICATES_ONE_ONLY:
9726 (*_bfd_error_handler)
9727 (_("%B: ignoring duplicate section `%A'"),
9728 abfd, sec);
9729 break;
9730
9731 case SEC_LINK_DUPLICATES_SAME_SIZE:
9732 if (sec->size != l->sec->size)
9733 (*_bfd_error_handler)
9734 (_("%B: duplicate section `%A' has different size"),
9735 abfd, sec);
9736 break;
9737
9738 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
9739 if (sec->size != l->sec->size)
9740 (*_bfd_error_handler)
9741 (_("%B: duplicate section `%A' has different size"),
9742 abfd, sec);
9743 else if (sec->size != 0)
9744 {
9745 bfd_byte *sec_contents, *l_sec_contents;
9746
9747 if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
9748 (*_bfd_error_handler)
9749 (_("%B: warning: could not read contents of section `%A'"),
9750 abfd, sec);
9751 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
9752 &l_sec_contents))
9753 (*_bfd_error_handler)
9754 (_("%B: warning: could not read contents of section `%A'"),
9755 l->sec->owner, l->sec);
9756 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
9757 (*_bfd_error_handler)
9758 (_("%B: warning: duplicate section `%A' has different contents"),
9759 abfd, sec);
9760
9761 if (sec_contents)
9762 free (sec_contents);
9763 if (l_sec_contents)
9764 free (l_sec_contents);
9765 }
9766 break;
9767 }
9768
9769 /* Set the output_section field so that lang_add_section
9770 does not create a lang_input_section structure for this
9771 section. Since there might be a symbol in the section
9772 being discarded, we must retain a pointer to the section
9773 which we are really going to use. */
9774 sec->output_section = bfd_abs_section_ptr;
9775 sec->kept_section = l->sec;
9776
9777 if (flags & SEC_GROUP)
9778 {
9779 asection *first = elf_next_in_group (sec);
9780 asection *s = first;
9781
9782 while (s != NULL)
9783 {
9784 s->output_section = bfd_abs_section_ptr;
9785 /* Record which group discards it. */
9786 s->kept_section = l->sec;
9787 s = elf_next_in_group (s);
9788 /* These lists are circular. */
9789 if (s == first)
9790 break;
9791 }
9792 }
9793
9794 return;
9795 }
9796 }
9797
9798 if (group)
9799 {
9800 /* If this is the member of a single member comdat group and the
9801 group hasn't be discarded, we check if it matches a linkonce
9802 section. We only record the discarded comdat group. Otherwise
9803 the undiscarded group will be discarded incorrectly later since
9804 itself has been recorded. */
9805 for (l = already_linked_list->entry; l != NULL; l = l->next)
9806 if ((l->sec->flags & SEC_GROUP) == 0
9807 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
9808 && bfd_elf_match_symbols_in_sections (l->sec,
9809 elf_next_in_group (sec)))
9810 {
9811 elf_next_in_group (sec)->output_section = bfd_abs_section_ptr;
9812 elf_next_in_group (sec)->kept_section = l->sec;
9813 group->output_section = bfd_abs_section_ptr;
9814 break;
9815 }
9816 if (l == NULL)
9817 return;
9818 }
9819 else
9820 /* There is no direct match. But for linkonce section, we should
9821 check if there is a match with comdat group member. We always
9822 record the linkonce section, discarded or not. */
9823 for (l = already_linked_list->entry; l != NULL; l = l->next)
9824 if (l->sec->flags & SEC_GROUP)
9825 {
9826 asection *first = elf_next_in_group (l->sec);
9827
9828 if (first != NULL
9829 && elf_next_in_group (first) == first
9830 && bfd_elf_match_symbols_in_sections (first, sec))
9831 {
9832 sec->output_section = bfd_abs_section_ptr;
9833 sec->kept_section = l->sec;
9834 break;
9835 }
9836 }
9837
9838 /* This is the first section with this name. Record it. */
9839 bfd_section_already_linked_table_insert (already_linked_list, sec);
9840 }
9841
9842 bfd_boolean
9843 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
9844 {
9845 return sym->st_shndx == SHN_COMMON;
9846 }
9847
9848 unsigned int
9849 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
9850 {
9851 return SHN_COMMON;
9852 }
9853
9854 asection *
9855 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
9856 {
9857 return bfd_com_section_ptr;
9858 }
This page took 0.24785 seconds and 4 git commands to generate.