bfd/
[binutils-gdb.git] / bfd / elflink.c
1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008 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 3 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,
20 MA 02110-1301, USA. */
21
22 #include "sysdep.h"
23 #include "bfd.h"
24 #include "bfdlink.h"
25 #include "libbfd.h"
26 #define ARCH_SIZE 0
27 #include "elf-bfd.h"
28 #include "safe-ctype.h"
29 #include "libiberty.h"
30 #include "objalloc.h"
31
32 /* Define a symbol in a dynamic linkage section. */
33
34 struct elf_link_hash_entry *
35 _bfd_elf_define_linkage_sym (bfd *abfd,
36 struct bfd_link_info *info,
37 asection *sec,
38 const char *name)
39 {
40 struct elf_link_hash_entry *h;
41 struct bfd_link_hash_entry *bh;
42 const struct elf_backend_data *bed;
43
44 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
45 if (h != NULL)
46 {
47 /* Zap symbol defined in an as-needed lib that wasn't linked.
48 This is a symptom of a larger problem: Absolute symbols
49 defined in shared libraries can't be overridden, because we
50 lose the link to the bfd which is via the symbol section. */
51 h->root.type = bfd_link_hash_new;
52 }
53
54 bh = &h->root;
55 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
56 sec, 0, NULL, FALSE,
57 get_elf_backend_data (abfd)->collect,
58 &bh))
59 return NULL;
60 h = (struct elf_link_hash_entry *) bh;
61 h->def_regular = 1;
62 h->type = STT_OBJECT;
63 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
64
65 bed = get_elf_backend_data (abfd);
66 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
67 return h;
68 }
69
70 bfd_boolean
71 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
72 {
73 flagword flags;
74 asection *s;
75 struct elf_link_hash_entry *h;
76 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
77 int ptralign;
78
79 /* This function may be called more than once. */
80 s = bfd_get_section_by_name (abfd, ".got");
81 if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
82 return TRUE;
83
84 switch (bed->s->arch_size)
85 {
86 case 32:
87 ptralign = 2;
88 break;
89
90 case 64:
91 ptralign = 3;
92 break;
93
94 default:
95 bfd_set_error (bfd_error_bad_value);
96 return FALSE;
97 }
98
99 flags = bed->dynamic_sec_flags;
100
101 s = bfd_make_section_with_flags (abfd, ".got", flags);
102 if (s == NULL
103 || !bfd_set_section_alignment (abfd, s, ptralign))
104 return FALSE;
105
106 if (bed->want_got_plt)
107 {
108 s = bfd_make_section_with_flags (abfd, ".got.plt", flags);
109 if (s == NULL
110 || !bfd_set_section_alignment (abfd, s, ptralign))
111 return FALSE;
112 }
113
114 if (bed->want_got_sym)
115 {
116 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
117 (or .got.plt) section. We don't do this in the linker script
118 because we don't want to define the symbol if we are not creating
119 a global offset table. */
120 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_GLOBAL_OFFSET_TABLE_");
121 elf_hash_table (info)->hgot = h;
122 if (h == NULL)
123 return FALSE;
124 }
125
126 /* The first bit of the global offset table is the header. */
127 s->size += bed->got_header_size;
128
129 return TRUE;
130 }
131 \f
132 /* Create a strtab to hold the dynamic symbol names. */
133 static bfd_boolean
134 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
135 {
136 struct elf_link_hash_table *hash_table;
137
138 hash_table = elf_hash_table (info);
139 if (hash_table->dynobj == NULL)
140 hash_table->dynobj = abfd;
141
142 if (hash_table->dynstr == NULL)
143 {
144 hash_table->dynstr = _bfd_elf_strtab_init ();
145 if (hash_table->dynstr == NULL)
146 return FALSE;
147 }
148 return TRUE;
149 }
150
151 /* Create some sections which will be filled in with dynamic linking
152 information. ABFD is an input file which requires dynamic sections
153 to be created. The dynamic sections take up virtual memory space
154 when the final executable is run, so we need to create them before
155 addresses are assigned to the output sections. We work out the
156 actual contents and size of these sections later. */
157
158 bfd_boolean
159 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
160 {
161 flagword flags;
162 register asection *s;
163 const struct elf_backend_data *bed;
164
165 if (! is_elf_hash_table (info->hash))
166 return FALSE;
167
168 if (elf_hash_table (info)->dynamic_sections_created)
169 return TRUE;
170
171 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
172 return FALSE;
173
174 abfd = elf_hash_table (info)->dynobj;
175 bed = get_elf_backend_data (abfd);
176
177 flags = bed->dynamic_sec_flags;
178
179 /* A dynamically linked executable has a .interp section, but a
180 shared library does not. */
181 if (info->executable)
182 {
183 s = bfd_make_section_with_flags (abfd, ".interp",
184 flags | SEC_READONLY);
185 if (s == NULL)
186 return FALSE;
187 }
188
189 /* Create sections to hold version informations. These are removed
190 if they are not needed. */
191 s = bfd_make_section_with_flags (abfd, ".gnu.version_d",
192 flags | SEC_READONLY);
193 if (s == NULL
194 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
195 return FALSE;
196
197 s = bfd_make_section_with_flags (abfd, ".gnu.version",
198 flags | SEC_READONLY);
199 if (s == NULL
200 || ! bfd_set_section_alignment (abfd, s, 1))
201 return FALSE;
202
203 s = bfd_make_section_with_flags (abfd, ".gnu.version_r",
204 flags | SEC_READONLY);
205 if (s == NULL
206 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
207 return FALSE;
208
209 s = bfd_make_section_with_flags (abfd, ".dynsym",
210 flags | SEC_READONLY);
211 if (s == NULL
212 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
213 return FALSE;
214
215 s = bfd_make_section_with_flags (abfd, ".dynstr",
216 flags | SEC_READONLY);
217 if (s == NULL)
218 return FALSE;
219
220 s = bfd_make_section_with_flags (abfd, ".dynamic", flags);
221 if (s == NULL
222 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
223 return FALSE;
224
225 /* The special symbol _DYNAMIC is always set to the start of the
226 .dynamic section. We could set _DYNAMIC in a linker script, but we
227 only want to define it if we are, in fact, creating a .dynamic
228 section. We don't want to define it if there is no .dynamic
229 section, since on some ELF platforms the start up code examines it
230 to decide how to initialize the process. */
231 if (!_bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC"))
232 return FALSE;
233
234 if (info->emit_hash)
235 {
236 s = bfd_make_section_with_flags (abfd, ".hash", flags | SEC_READONLY);
237 if (s == NULL
238 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
239 return FALSE;
240 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
241 }
242
243 if (info->emit_gnu_hash)
244 {
245 s = bfd_make_section_with_flags (abfd, ".gnu.hash",
246 flags | SEC_READONLY);
247 if (s == NULL
248 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
249 return FALSE;
250 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
251 4 32-bit words followed by variable count of 64-bit words, then
252 variable count of 32-bit words. */
253 if (bed->s->arch_size == 64)
254 elf_section_data (s)->this_hdr.sh_entsize = 0;
255 else
256 elf_section_data (s)->this_hdr.sh_entsize = 4;
257 }
258
259 /* Let the backend create the rest of the sections. This lets the
260 backend set the right flags. The backend will normally create
261 the .got and .plt sections. */
262 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
263 return FALSE;
264
265 elf_hash_table (info)->dynamic_sections_created = TRUE;
266
267 return TRUE;
268 }
269
270 /* Create dynamic sections when linking against a dynamic object. */
271
272 bfd_boolean
273 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
274 {
275 flagword flags, pltflags;
276 struct elf_link_hash_entry *h;
277 asection *s;
278 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
279
280 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
281 .rel[a].bss sections. */
282 flags = bed->dynamic_sec_flags;
283
284 pltflags = flags;
285 if (bed->plt_not_loaded)
286 /* We do not clear SEC_ALLOC here because we still want the OS to
287 allocate space for the section; it's just that there's nothing
288 to read in from the object file. */
289 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
290 else
291 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
292 if (bed->plt_readonly)
293 pltflags |= SEC_READONLY;
294
295 s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
296 if (s == NULL
297 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
298 return FALSE;
299
300 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
301 .plt section. */
302 if (bed->want_plt_sym)
303 {
304 h = _bfd_elf_define_linkage_sym (abfd, info, s,
305 "_PROCEDURE_LINKAGE_TABLE_");
306 elf_hash_table (info)->hplt = h;
307 if (h == NULL)
308 return FALSE;
309 }
310
311 s = bfd_make_section_with_flags (abfd,
312 (bed->rela_plts_and_copies_p
313 ? ".rela.plt" : ".rel.plt"),
314 flags | SEC_READONLY);
315 if (s == NULL
316 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
317 return FALSE;
318
319 if (! _bfd_elf_create_got_section (abfd, info))
320 return FALSE;
321
322 if (bed->want_dynbss)
323 {
324 /* The .dynbss section is a place to put symbols which are defined
325 by dynamic objects, are referenced by regular objects, and are
326 not functions. We must allocate space for them in the process
327 image and use a R_*_COPY reloc to tell the dynamic linker to
328 initialize them at run time. The linker script puts the .dynbss
329 section into the .bss section of the final image. */
330 s = bfd_make_section_with_flags (abfd, ".dynbss",
331 (SEC_ALLOC
332 | SEC_LINKER_CREATED));
333 if (s == NULL)
334 return FALSE;
335
336 /* The .rel[a].bss section holds copy relocs. This section is not
337 normally needed. We need to create it here, though, so that the
338 linker will map it to an output section. We can't just create it
339 only if we need it, because we will not know whether we need it
340 until we have seen all the input files, and the first time the
341 main linker code calls BFD after examining all the input files
342 (size_dynamic_sections) the input sections have already been
343 mapped to the output sections. If the section turns out not to
344 be needed, we can discard it later. We will never need this
345 section when generating a shared object, since they do not use
346 copy relocs. */
347 if (! info->shared)
348 {
349 s = bfd_make_section_with_flags (abfd,
350 (bed->rela_plts_and_copies_p
351 ? ".rela.bss" : ".rel.bss"),
352 flags | SEC_READONLY);
353 if (s == NULL
354 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
355 return FALSE;
356 }
357 }
358
359 return TRUE;
360 }
361 \f
362 /* Record a new dynamic symbol. We record the dynamic symbols as we
363 read the input files, since we need to have a list of all of them
364 before we can determine the final sizes of the output sections.
365 Note that we may actually call this function even though we are not
366 going to output any dynamic symbols; in some cases we know that a
367 symbol should be in the dynamic symbol table, but only if there is
368 one. */
369
370 bfd_boolean
371 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
372 struct elf_link_hash_entry *h)
373 {
374 if (h->dynindx == -1)
375 {
376 struct elf_strtab_hash *dynstr;
377 char *p;
378 const char *name;
379 bfd_size_type indx;
380
381 /* XXX: The ABI draft says the linker must turn hidden and
382 internal symbols into STB_LOCAL symbols when producing the
383 DSO. However, if ld.so honors st_other in the dynamic table,
384 this would not be necessary. */
385 switch (ELF_ST_VISIBILITY (h->other))
386 {
387 case STV_INTERNAL:
388 case STV_HIDDEN:
389 if (h->root.type != bfd_link_hash_undefined
390 && h->root.type != bfd_link_hash_undefweak)
391 {
392 h->forced_local = 1;
393 if (!elf_hash_table (info)->is_relocatable_executable)
394 return TRUE;
395 }
396
397 default:
398 break;
399 }
400
401 h->dynindx = elf_hash_table (info)->dynsymcount;
402 ++elf_hash_table (info)->dynsymcount;
403
404 dynstr = elf_hash_table (info)->dynstr;
405 if (dynstr == NULL)
406 {
407 /* Create a strtab to hold the dynamic symbol names. */
408 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
409 if (dynstr == NULL)
410 return FALSE;
411 }
412
413 /* We don't put any version information in the dynamic string
414 table. */
415 name = h->root.root.string;
416 p = strchr (name, ELF_VER_CHR);
417 if (p != NULL)
418 /* We know that the p points into writable memory. In fact,
419 there are only a few symbols that have read-only names, being
420 those like _GLOBAL_OFFSET_TABLE_ that are created specially
421 by the backends. Most symbols will have names pointing into
422 an ELF string table read from a file, or to objalloc memory. */
423 *p = 0;
424
425 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
426
427 if (p != NULL)
428 *p = ELF_VER_CHR;
429
430 if (indx == (bfd_size_type) -1)
431 return FALSE;
432 h->dynstr_index = indx;
433 }
434
435 return TRUE;
436 }
437 \f
438 /* Mark a symbol dynamic. */
439
440 void
441 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
442 struct elf_link_hash_entry *h,
443 Elf_Internal_Sym *sym)
444 {
445 struct bfd_elf_dynamic_list *d = info->dynamic_list;
446
447 /* It may be called more than once on the same H. */
448 if(h->dynamic || info->relocatable)
449 return;
450
451 if ((info->dynamic_data
452 && (h->type == STT_OBJECT
453 || (sym != NULL
454 && ELF_ST_TYPE (sym->st_info) == STT_OBJECT)))
455 || (d != NULL
456 && h->root.type == bfd_link_hash_new
457 && (*d->match) (&d->head, NULL, h->root.root.string)))
458 h->dynamic = 1;
459 }
460
461 /* Record an assignment to a symbol made by a linker script. We need
462 this in case some dynamic object refers to this symbol. */
463
464 bfd_boolean
465 bfd_elf_record_link_assignment (bfd *output_bfd,
466 struct bfd_link_info *info,
467 const char *name,
468 bfd_boolean provide,
469 bfd_boolean hidden)
470 {
471 struct elf_link_hash_entry *h, *hv;
472 struct elf_link_hash_table *htab;
473 const struct elf_backend_data *bed;
474
475 if (!is_elf_hash_table (info->hash))
476 return TRUE;
477
478 htab = elf_hash_table (info);
479 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
480 if (h == NULL)
481 return provide;
482
483 switch (h->root.type)
484 {
485 case bfd_link_hash_defined:
486 case bfd_link_hash_defweak:
487 case bfd_link_hash_common:
488 break;
489 case bfd_link_hash_undefweak:
490 case bfd_link_hash_undefined:
491 /* Since we're defining the symbol, don't let it seem to have not
492 been defined. record_dynamic_symbol and size_dynamic_sections
493 may depend on this. */
494 h->root.type = bfd_link_hash_new;
495 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
496 bfd_link_repair_undef_list (&htab->root);
497 break;
498 case bfd_link_hash_new:
499 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
500 h->non_elf = 0;
501 break;
502 case bfd_link_hash_indirect:
503 /* We had a versioned symbol in a dynamic library. We make the
504 the versioned symbol point to this one. */
505 bed = get_elf_backend_data (output_bfd);
506 hv = h;
507 while (hv->root.type == bfd_link_hash_indirect
508 || hv->root.type == bfd_link_hash_warning)
509 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
510 /* We don't need to update h->root.u since linker will set them
511 later. */
512 h->root.type = bfd_link_hash_undefined;
513 hv->root.type = bfd_link_hash_indirect;
514 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
515 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
516 break;
517 case bfd_link_hash_warning:
518 abort ();
519 break;
520 }
521
522 /* If this symbol is being provided by the linker script, and it is
523 currently defined by a dynamic object, but not by a regular
524 object, then mark it as undefined so that the generic linker will
525 force the correct value. */
526 if (provide
527 && h->def_dynamic
528 && !h->def_regular)
529 h->root.type = bfd_link_hash_undefined;
530
531 /* If this symbol is not being provided by the linker script, and it is
532 currently defined by a dynamic object, but not by a regular object,
533 then clear out any version information because the symbol will not be
534 associated with the dynamic object any more. */
535 if (!provide
536 && h->def_dynamic
537 && !h->def_regular)
538 h->verinfo.verdef = NULL;
539
540 h->def_regular = 1;
541
542 if (provide && hidden)
543 {
544 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
545
546 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
547 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
548 }
549
550 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
551 and executables. */
552 if (!info->relocatable
553 && h->dynindx != -1
554 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
555 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
556 h->forced_local = 1;
557
558 if ((h->def_dynamic
559 || h->ref_dynamic
560 || info->shared
561 || (info->executable && elf_hash_table (info)->is_relocatable_executable))
562 && h->dynindx == -1)
563 {
564 if (! bfd_elf_link_record_dynamic_symbol (info, h))
565 return FALSE;
566
567 /* If this is a weak defined symbol, and we know a corresponding
568 real symbol from the same dynamic object, make sure the real
569 symbol is also made into a dynamic symbol. */
570 if (h->u.weakdef != NULL
571 && h->u.weakdef->dynindx == -1)
572 {
573 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
574 return FALSE;
575 }
576 }
577
578 return TRUE;
579 }
580
581 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
582 success, and 2 on a failure caused by attempting to record a symbol
583 in a discarded section, eg. a discarded link-once section symbol. */
584
585 int
586 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
587 bfd *input_bfd,
588 long input_indx)
589 {
590 bfd_size_type amt;
591 struct elf_link_local_dynamic_entry *entry;
592 struct elf_link_hash_table *eht;
593 struct elf_strtab_hash *dynstr;
594 unsigned long dynstr_index;
595 char *name;
596 Elf_External_Sym_Shndx eshndx;
597 char esym[sizeof (Elf64_External_Sym)];
598
599 if (! is_elf_hash_table (info->hash))
600 return 0;
601
602 /* See if the entry exists already. */
603 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
604 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
605 return 1;
606
607 amt = sizeof (*entry);
608 entry = bfd_alloc (input_bfd, amt);
609 if (entry == NULL)
610 return 0;
611
612 /* Go find the symbol, so that we can find it's name. */
613 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
614 1, input_indx, &entry->isym, esym, &eshndx))
615 {
616 bfd_release (input_bfd, entry);
617 return 0;
618 }
619
620 if (entry->isym.st_shndx != SHN_UNDEF
621 && entry->isym.st_shndx < SHN_LORESERVE)
622 {
623 asection *s;
624
625 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
626 if (s == NULL || bfd_is_abs_section (s->output_section))
627 {
628 /* We can still bfd_release here as nothing has done another
629 bfd_alloc. We can't do this later in this function. */
630 bfd_release (input_bfd, entry);
631 return 2;
632 }
633 }
634
635 name = (bfd_elf_string_from_elf_section
636 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
637 entry->isym.st_name));
638
639 dynstr = elf_hash_table (info)->dynstr;
640 if (dynstr == NULL)
641 {
642 /* Create a strtab to hold the dynamic symbol names. */
643 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
644 if (dynstr == NULL)
645 return 0;
646 }
647
648 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
649 if (dynstr_index == (unsigned long) -1)
650 return 0;
651 entry->isym.st_name = dynstr_index;
652
653 eht = elf_hash_table (info);
654
655 entry->next = eht->dynlocal;
656 eht->dynlocal = entry;
657 entry->input_bfd = input_bfd;
658 entry->input_indx = input_indx;
659 eht->dynsymcount++;
660
661 /* Whatever binding the symbol had before, it's now local. */
662 entry->isym.st_info
663 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
664
665 /* The dynindx will be set at the end of size_dynamic_sections. */
666
667 return 1;
668 }
669
670 /* Return the dynindex of a local dynamic symbol. */
671
672 long
673 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
674 bfd *input_bfd,
675 long input_indx)
676 {
677 struct elf_link_local_dynamic_entry *e;
678
679 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
680 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
681 return e->dynindx;
682 return -1;
683 }
684
685 /* This function is used to renumber the dynamic symbols, if some of
686 them are removed because they are marked as local. This is called
687 via elf_link_hash_traverse. */
688
689 static bfd_boolean
690 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
691 void *data)
692 {
693 size_t *count = data;
694
695 if (h->root.type == bfd_link_hash_warning)
696 h = (struct elf_link_hash_entry *) h->root.u.i.link;
697
698 if (h->forced_local)
699 return TRUE;
700
701 if (h->dynindx != -1)
702 h->dynindx = ++(*count);
703
704 return TRUE;
705 }
706
707
708 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
709 STB_LOCAL binding. */
710
711 static bfd_boolean
712 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
713 void *data)
714 {
715 size_t *count = data;
716
717 if (h->root.type == bfd_link_hash_warning)
718 h = (struct elf_link_hash_entry *) h->root.u.i.link;
719
720 if (!h->forced_local)
721 return TRUE;
722
723 if (h->dynindx != -1)
724 h->dynindx = ++(*count);
725
726 return TRUE;
727 }
728
729 /* Return true if the dynamic symbol for a given section should be
730 omitted when creating a shared library. */
731 bfd_boolean
732 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
733 struct bfd_link_info *info,
734 asection *p)
735 {
736 struct elf_link_hash_table *htab;
737
738 switch (elf_section_data (p)->this_hdr.sh_type)
739 {
740 case SHT_PROGBITS:
741 case SHT_NOBITS:
742 /* If sh_type is yet undecided, assume it could be
743 SHT_PROGBITS/SHT_NOBITS. */
744 case SHT_NULL:
745 htab = elf_hash_table (info);
746 if (p == htab->tls_sec)
747 return FALSE;
748
749 if (htab->text_index_section != NULL)
750 return p != htab->text_index_section && p != htab->data_index_section;
751
752 if (strcmp (p->name, ".got") == 0
753 || strcmp (p->name, ".got.plt") == 0
754 || strcmp (p->name, ".plt") == 0)
755 {
756 asection *ip;
757
758 if (htab->dynobj != NULL
759 && (ip = bfd_get_section_by_name (htab->dynobj, p->name)) != NULL
760 && (ip->flags & SEC_LINKER_CREATED)
761 && ip->output_section == p)
762 return TRUE;
763 }
764 return FALSE;
765
766 /* There shouldn't be section relative relocations
767 against any other section. */
768 default:
769 return TRUE;
770 }
771 }
772
773 /* Assign dynsym indices. In a shared library we generate a section
774 symbol for each output section, which come first. Next come symbols
775 which have been forced to local binding. Then all of the back-end
776 allocated local dynamic syms, followed by the rest of the global
777 symbols. */
778
779 static unsigned long
780 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
781 struct bfd_link_info *info,
782 unsigned long *section_sym_count)
783 {
784 unsigned long dynsymcount = 0;
785
786 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
787 {
788 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
789 asection *p;
790 for (p = output_bfd->sections; p ; p = p->next)
791 if ((p->flags & SEC_EXCLUDE) == 0
792 && (p->flags & SEC_ALLOC) != 0
793 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
794 elf_section_data (p)->dynindx = ++dynsymcount;
795 else
796 elf_section_data (p)->dynindx = 0;
797 }
798 *section_sym_count = dynsymcount;
799
800 elf_link_hash_traverse (elf_hash_table (info),
801 elf_link_renumber_local_hash_table_dynsyms,
802 &dynsymcount);
803
804 if (elf_hash_table (info)->dynlocal)
805 {
806 struct elf_link_local_dynamic_entry *p;
807 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
808 p->dynindx = ++dynsymcount;
809 }
810
811 elf_link_hash_traverse (elf_hash_table (info),
812 elf_link_renumber_hash_table_dynsyms,
813 &dynsymcount);
814
815 /* There is an unused NULL entry at the head of the table which
816 we must account for in our count. Unless there weren't any
817 symbols, which means we'll have no table at all. */
818 if (dynsymcount != 0)
819 ++dynsymcount;
820
821 elf_hash_table (info)->dynsymcount = dynsymcount;
822 return dynsymcount;
823 }
824
825 /* Merge st_other field. */
826
827 static void
828 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
829 Elf_Internal_Sym *isym, bfd_boolean definition,
830 bfd_boolean dynamic)
831 {
832 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
833
834 /* If st_other has a processor-specific meaning, specific
835 code might be needed here. We never merge the visibility
836 attribute with the one from a dynamic object. */
837 if (bed->elf_backend_merge_symbol_attribute)
838 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
839 dynamic);
840
841 /* If this symbol has default visibility and the user has requested
842 we not re-export it, then mark it as hidden. */
843 if (definition
844 && !dynamic
845 && (abfd->no_export
846 || (abfd->my_archive && abfd->my_archive->no_export))
847 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
848 isym->st_other = (STV_HIDDEN
849 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
850
851 if (!dynamic && ELF_ST_VISIBILITY (isym->st_other) != 0)
852 {
853 unsigned char hvis, symvis, other, nvis;
854
855 /* Only merge the visibility. Leave the remainder of the
856 st_other field to elf_backend_merge_symbol_attribute. */
857 other = h->other & ~ELF_ST_VISIBILITY (-1);
858
859 /* Combine visibilities, using the most constraining one. */
860 hvis = ELF_ST_VISIBILITY (h->other);
861 symvis = ELF_ST_VISIBILITY (isym->st_other);
862 if (! hvis)
863 nvis = symvis;
864 else if (! symvis)
865 nvis = hvis;
866 else
867 nvis = hvis < symvis ? hvis : symvis;
868
869 h->other = other | nvis;
870 }
871 }
872
873 /* This function is called when we want to define a new symbol. It
874 handles the various cases which arise when we find a definition in
875 a dynamic object, or when there is already a definition in a
876 dynamic object. The new symbol is described by NAME, SYM, PSEC,
877 and PVALUE. We set SYM_HASH to the hash table entry. We set
878 OVERRIDE if the old symbol is overriding a new definition. We set
879 TYPE_CHANGE_OK if it is OK for the type to change. We set
880 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
881 change, we mean that we shouldn't warn if the type or size does
882 change. We set POLD_ALIGNMENT if an old common symbol in a dynamic
883 object is overridden by a regular object. */
884
885 bfd_boolean
886 _bfd_elf_merge_symbol (bfd *abfd,
887 struct bfd_link_info *info,
888 const char *name,
889 Elf_Internal_Sym *sym,
890 asection **psec,
891 bfd_vma *pvalue,
892 unsigned int *pold_alignment,
893 struct elf_link_hash_entry **sym_hash,
894 bfd_boolean *skip,
895 bfd_boolean *override,
896 bfd_boolean *type_change_ok,
897 bfd_boolean *size_change_ok)
898 {
899 asection *sec, *oldsec;
900 struct elf_link_hash_entry *h;
901 struct elf_link_hash_entry *flip;
902 int bind;
903 bfd *oldbfd;
904 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
905 bfd_boolean newweak, oldweak, newfunc, oldfunc;
906 const struct elf_backend_data *bed;
907
908 *skip = FALSE;
909 *override = FALSE;
910
911 sec = *psec;
912 bind = ELF_ST_BIND (sym->st_info);
913
914 /* Silently discard TLS symbols from --just-syms. There's no way to
915 combine a static TLS block with a new TLS block for this executable. */
916 if (ELF_ST_TYPE (sym->st_info) == STT_TLS
917 && sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
918 {
919 *skip = TRUE;
920 return TRUE;
921 }
922
923 if (! bfd_is_und_section (sec))
924 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
925 else
926 h = ((struct elf_link_hash_entry *)
927 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
928 if (h == NULL)
929 return FALSE;
930 *sym_hash = h;
931
932 bed = get_elf_backend_data (abfd);
933
934 /* This code is for coping with dynamic objects, and is only useful
935 if we are doing an ELF link. */
936 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
937 return TRUE;
938
939 /* For merging, we only care about real symbols. */
940
941 while (h->root.type == bfd_link_hash_indirect
942 || h->root.type == bfd_link_hash_warning)
943 h = (struct elf_link_hash_entry *) h->root.u.i.link;
944
945 /* We have to check it for every instance since the first few may be
946 refereences and not all compilers emit symbol type for undefined
947 symbols. */
948 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
949
950 /* If we just created the symbol, mark it as being an ELF symbol.
951 Other than that, there is nothing to do--there is no merge issue
952 with a newly defined symbol--so we just return. */
953
954 if (h->root.type == bfd_link_hash_new)
955 {
956 h->non_elf = 0;
957 return TRUE;
958 }
959
960 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
961 existing symbol. */
962
963 switch (h->root.type)
964 {
965 default:
966 oldbfd = NULL;
967 oldsec = NULL;
968 break;
969
970 case bfd_link_hash_undefined:
971 case bfd_link_hash_undefweak:
972 oldbfd = h->root.u.undef.abfd;
973 oldsec = NULL;
974 break;
975
976 case bfd_link_hash_defined:
977 case bfd_link_hash_defweak:
978 oldbfd = h->root.u.def.section->owner;
979 oldsec = h->root.u.def.section;
980 break;
981
982 case bfd_link_hash_common:
983 oldbfd = h->root.u.c.p->section->owner;
984 oldsec = h->root.u.c.p->section;
985 break;
986 }
987
988 /* In cases involving weak versioned symbols, we may wind up trying
989 to merge a symbol with itself. Catch that here, to avoid the
990 confusion that results if we try to override a symbol with
991 itself. The additional tests catch cases like
992 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
993 dynamic object, which we do want to handle here. */
994 if (abfd == oldbfd
995 && ((abfd->flags & DYNAMIC) == 0
996 || !h->def_regular))
997 return TRUE;
998
999 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1000 respectively, is from a dynamic object. */
1001
1002 newdyn = (abfd->flags & DYNAMIC) != 0;
1003
1004 olddyn = FALSE;
1005 if (oldbfd != NULL)
1006 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1007 else if (oldsec != NULL)
1008 {
1009 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1010 indices used by MIPS ELF. */
1011 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1012 }
1013
1014 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1015 respectively, appear to be a definition rather than reference. */
1016
1017 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1018
1019 olddef = (h->root.type != bfd_link_hash_undefined
1020 && h->root.type != bfd_link_hash_undefweak
1021 && h->root.type != bfd_link_hash_common);
1022
1023 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1024 respectively, appear to be a function. */
1025
1026 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1027 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1028
1029 oldfunc = (h->type != STT_NOTYPE
1030 && bed->is_function_type (h->type));
1031
1032 /* When we try to create a default indirect symbol from the dynamic
1033 definition with the default version, we skip it if its type and
1034 the type of existing regular definition mismatch. We only do it
1035 if the existing regular definition won't be dynamic. */
1036 if (pold_alignment == NULL
1037 && !info->shared
1038 && !info->export_dynamic
1039 && !h->ref_dynamic
1040 && newdyn
1041 && newdef
1042 && !olddyn
1043 && (olddef || h->root.type == bfd_link_hash_common)
1044 && ELF_ST_TYPE (sym->st_info) != h->type
1045 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1046 && h->type != STT_NOTYPE
1047 && !(newfunc && oldfunc))
1048 {
1049 *skip = TRUE;
1050 return TRUE;
1051 }
1052
1053 /* Check TLS symbol. We don't check undefined symbol introduced by
1054 "ld -u". */
1055 if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)
1056 && ELF_ST_TYPE (sym->st_info) != h->type
1057 && oldbfd != NULL)
1058 {
1059 bfd *ntbfd, *tbfd;
1060 bfd_boolean ntdef, tdef;
1061 asection *ntsec, *tsec;
1062
1063 if (h->type == STT_TLS)
1064 {
1065 ntbfd = abfd;
1066 ntsec = sec;
1067 ntdef = newdef;
1068 tbfd = oldbfd;
1069 tsec = oldsec;
1070 tdef = olddef;
1071 }
1072 else
1073 {
1074 ntbfd = oldbfd;
1075 ntsec = oldsec;
1076 ntdef = olddef;
1077 tbfd = abfd;
1078 tsec = sec;
1079 tdef = newdef;
1080 }
1081
1082 if (tdef && ntdef)
1083 (*_bfd_error_handler)
1084 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
1085 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1086 else if (!tdef && !ntdef)
1087 (*_bfd_error_handler)
1088 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
1089 tbfd, ntbfd, h->root.root.string);
1090 else if (tdef)
1091 (*_bfd_error_handler)
1092 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
1093 tbfd, tsec, ntbfd, h->root.root.string);
1094 else
1095 (*_bfd_error_handler)
1096 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
1097 tbfd, ntbfd, ntsec, h->root.root.string);
1098
1099 bfd_set_error (bfd_error_bad_value);
1100 return FALSE;
1101 }
1102
1103 /* We need to remember if a symbol has a definition in a dynamic
1104 object or is weak in all dynamic objects. Internal and hidden
1105 visibility will make it unavailable to dynamic objects. */
1106 if (newdyn && !h->dynamic_def)
1107 {
1108 if (!bfd_is_und_section (sec))
1109 h->dynamic_def = 1;
1110 else
1111 {
1112 /* Check if this symbol is weak in all dynamic objects. If it
1113 is the first time we see it in a dynamic object, we mark
1114 if it is weak. Otherwise, we clear it. */
1115 if (!h->ref_dynamic)
1116 {
1117 if (bind == STB_WEAK)
1118 h->dynamic_weak = 1;
1119 }
1120 else if (bind != STB_WEAK)
1121 h->dynamic_weak = 0;
1122 }
1123 }
1124
1125 /* If the old symbol has non-default visibility, we ignore the new
1126 definition from a dynamic object. */
1127 if (newdyn
1128 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1129 && !bfd_is_und_section (sec))
1130 {
1131 *skip = TRUE;
1132 /* Make sure this symbol is dynamic. */
1133 h->ref_dynamic = 1;
1134 /* A protected symbol has external availability. Make sure it is
1135 recorded as dynamic.
1136
1137 FIXME: Should we check type and size for protected symbol? */
1138 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1139 return bfd_elf_link_record_dynamic_symbol (info, h);
1140 else
1141 return TRUE;
1142 }
1143 else if (!newdyn
1144 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1145 && h->def_dynamic)
1146 {
1147 /* If the new symbol with non-default visibility comes from a
1148 relocatable file and the old definition comes from a dynamic
1149 object, we remove the old definition. */
1150 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1151 {
1152 /* Handle the case where the old dynamic definition is
1153 default versioned. We need to copy the symbol info from
1154 the symbol with default version to the normal one if it
1155 was referenced before. */
1156 if (h->ref_regular)
1157 {
1158 const struct elf_backend_data *bed
1159 = get_elf_backend_data (abfd);
1160 struct elf_link_hash_entry *vh = *sym_hash;
1161 vh->root.type = h->root.type;
1162 h->root.type = bfd_link_hash_indirect;
1163 (*bed->elf_backend_copy_indirect_symbol) (info, vh, h);
1164 /* Protected symbols will override the dynamic definition
1165 with default version. */
1166 if (ELF_ST_VISIBILITY (sym->st_other) == STV_PROTECTED)
1167 {
1168 h->root.u.i.link = (struct bfd_link_hash_entry *) vh;
1169 vh->dynamic_def = 1;
1170 vh->ref_dynamic = 1;
1171 }
1172 else
1173 {
1174 h->root.type = vh->root.type;
1175 vh->ref_dynamic = 0;
1176 /* We have to hide it here since it was made dynamic
1177 global with extra bits when the symbol info was
1178 copied from the old dynamic definition. */
1179 (*bed->elf_backend_hide_symbol) (info, vh, TRUE);
1180 }
1181 h = vh;
1182 }
1183 else
1184 h = *sym_hash;
1185 }
1186
1187 if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1188 && bfd_is_und_section (sec))
1189 {
1190 /* If the new symbol is undefined and the old symbol was
1191 also undefined before, we need to make sure
1192 _bfd_generic_link_add_one_symbol doesn't mess
1193 up the linker hash table undefs list. Since the old
1194 definition came from a dynamic object, it is still on the
1195 undefs list. */
1196 h->root.type = bfd_link_hash_undefined;
1197 h->root.u.undef.abfd = abfd;
1198 }
1199 else
1200 {
1201 h->root.type = bfd_link_hash_new;
1202 h->root.u.undef.abfd = NULL;
1203 }
1204
1205 if (h->def_dynamic)
1206 {
1207 h->def_dynamic = 0;
1208 h->ref_dynamic = 1;
1209 h->dynamic_def = 1;
1210 }
1211 /* FIXME: Should we check type and size for protected symbol? */
1212 h->size = 0;
1213 h->type = 0;
1214 return TRUE;
1215 }
1216
1217 /* Differentiate strong and weak symbols. */
1218 newweak = bind == STB_WEAK;
1219 oldweak = (h->root.type == bfd_link_hash_defweak
1220 || h->root.type == bfd_link_hash_undefweak);
1221
1222 /* If a new weak symbol definition comes from a regular file and the
1223 old symbol comes from a dynamic library, we treat the new one as
1224 strong. Similarly, an old weak symbol definition from a regular
1225 file is treated as strong when the new symbol comes from a dynamic
1226 library. Further, an old weak symbol from a dynamic library is
1227 treated as strong if the new symbol is from a dynamic library.
1228 This reflects the way glibc's ld.so works.
1229
1230 Do this before setting *type_change_ok or *size_change_ok so that
1231 we warn properly when dynamic library symbols are overridden. */
1232
1233 if (newdef && !newdyn && olddyn)
1234 newweak = FALSE;
1235 if (olddef && newdyn)
1236 oldweak = FALSE;
1237
1238 /* Allow changes between different types of funciton symbol. */
1239 if (newfunc && oldfunc)
1240 *type_change_ok = TRUE;
1241
1242 /* It's OK to change the type if either the existing symbol or the
1243 new symbol is weak. A type change is also OK if the old symbol
1244 is undefined and the new symbol is defined. */
1245
1246 if (oldweak
1247 || newweak
1248 || (newdef
1249 && h->root.type == bfd_link_hash_undefined))
1250 *type_change_ok = TRUE;
1251
1252 /* It's OK to change the size if either the existing symbol or the
1253 new symbol is weak, or if the old symbol is undefined. */
1254
1255 if (*type_change_ok
1256 || h->root.type == bfd_link_hash_undefined)
1257 *size_change_ok = TRUE;
1258
1259 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1260 symbol, respectively, appears to be a common symbol in a dynamic
1261 object. If a symbol appears in an uninitialized section, and is
1262 not weak, and is not a function, then it may be a common symbol
1263 which was resolved when the dynamic object was created. We want
1264 to treat such symbols specially, because they raise special
1265 considerations when setting the symbol size: if the symbol
1266 appears as a common symbol in a regular object, and the size in
1267 the regular object is larger, we must make sure that we use the
1268 larger size. This problematic case can always be avoided in C,
1269 but it must be handled correctly when using Fortran shared
1270 libraries.
1271
1272 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1273 likewise for OLDDYNCOMMON and OLDDEF.
1274
1275 Note that this test is just a heuristic, and that it is quite
1276 possible to have an uninitialized symbol in a shared object which
1277 is really a definition, rather than a common symbol. This could
1278 lead to some minor confusion when the symbol really is a common
1279 symbol in some regular object. However, I think it will be
1280 harmless. */
1281
1282 if (newdyn
1283 && newdef
1284 && !newweak
1285 && (sec->flags & SEC_ALLOC) != 0
1286 && (sec->flags & SEC_LOAD) == 0
1287 && sym->st_size > 0
1288 && !newfunc)
1289 newdyncommon = TRUE;
1290 else
1291 newdyncommon = FALSE;
1292
1293 if (olddyn
1294 && olddef
1295 && h->root.type == bfd_link_hash_defined
1296 && h->def_dynamic
1297 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1298 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1299 && h->size > 0
1300 && !oldfunc)
1301 olddyncommon = TRUE;
1302 else
1303 olddyncommon = FALSE;
1304
1305 /* We now know everything about the old and new symbols. We ask the
1306 backend to check if we can merge them. */
1307 if (bed->merge_symbol
1308 && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue,
1309 pold_alignment, skip, override,
1310 type_change_ok, size_change_ok,
1311 &newdyn, &newdef, &newdyncommon, &newweak,
1312 abfd, &sec,
1313 &olddyn, &olddef, &olddyncommon, &oldweak,
1314 oldbfd, &oldsec))
1315 return FALSE;
1316
1317 /* If both the old and the new symbols look like common symbols in a
1318 dynamic object, set the size of the symbol to the larger of the
1319 two. */
1320
1321 if (olddyncommon
1322 && newdyncommon
1323 && sym->st_size != h->size)
1324 {
1325 /* Since we think we have two common symbols, issue a multiple
1326 common warning if desired. Note that we only warn if the
1327 size is different. If the size is the same, we simply let
1328 the old symbol override the new one as normally happens with
1329 symbols defined in dynamic objects. */
1330
1331 if (! ((*info->callbacks->multiple_common)
1332 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1333 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1334 return FALSE;
1335
1336 if (sym->st_size > h->size)
1337 h->size = sym->st_size;
1338
1339 *size_change_ok = TRUE;
1340 }
1341
1342 /* If we are looking at a dynamic object, and we have found a
1343 definition, we need to see if the symbol was already defined by
1344 some other object. If so, we want to use the existing
1345 definition, and we do not want to report a multiple symbol
1346 definition error; we do this by clobbering *PSEC to be
1347 bfd_und_section_ptr.
1348
1349 We treat a common symbol as a definition if the symbol in the
1350 shared library is a function, since common symbols always
1351 represent variables; this can cause confusion in principle, but
1352 any such confusion would seem to indicate an erroneous program or
1353 shared library. We also permit a common symbol in a regular
1354 object to override a weak symbol in a shared object. */
1355
1356 if (newdyn
1357 && newdef
1358 && (olddef
1359 || (h->root.type == bfd_link_hash_common
1360 && (newweak || newfunc))))
1361 {
1362 *override = TRUE;
1363 newdef = FALSE;
1364 newdyncommon = FALSE;
1365
1366 *psec = sec = bfd_und_section_ptr;
1367 *size_change_ok = TRUE;
1368
1369 /* If we get here when the old symbol is a common symbol, then
1370 we are explicitly letting it override a weak symbol or
1371 function in a dynamic object, and we don't want to warn about
1372 a type change. If the old symbol is a defined symbol, a type
1373 change warning may still be appropriate. */
1374
1375 if (h->root.type == bfd_link_hash_common)
1376 *type_change_ok = TRUE;
1377 }
1378
1379 /* Handle the special case of an old common symbol merging with a
1380 new symbol which looks like a common symbol in a shared object.
1381 We change *PSEC and *PVALUE to make the new symbol look like a
1382 common symbol, and let _bfd_generic_link_add_one_symbol do the
1383 right thing. */
1384
1385 if (newdyncommon
1386 && h->root.type == bfd_link_hash_common)
1387 {
1388 *override = TRUE;
1389 newdef = FALSE;
1390 newdyncommon = FALSE;
1391 *pvalue = sym->st_size;
1392 *psec = sec = bed->common_section (oldsec);
1393 *size_change_ok = TRUE;
1394 }
1395
1396 /* Skip weak definitions of symbols that are already defined. */
1397 if (newdef && olddef && newweak)
1398 {
1399 *skip = TRUE;
1400
1401 /* Merge st_other. If the symbol already has a dynamic index,
1402 but visibility says it should not be visible, turn it into a
1403 local symbol. */
1404 elf_merge_st_other (abfd, h, sym, newdef, newdyn);
1405 if (h->dynindx != -1)
1406 switch (ELF_ST_VISIBILITY (h->other))
1407 {
1408 case STV_INTERNAL:
1409 case STV_HIDDEN:
1410 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1411 break;
1412 }
1413 }
1414
1415 /* If the old symbol is from a dynamic object, and the new symbol is
1416 a definition which is not from a dynamic object, then the new
1417 symbol overrides the old symbol. Symbols from regular files
1418 always take precedence over symbols from dynamic objects, even if
1419 they are defined after the dynamic object in the link.
1420
1421 As above, we again permit a common symbol in a regular object to
1422 override a definition in a shared object if the shared object
1423 symbol is a function or is weak. */
1424
1425 flip = NULL;
1426 if (!newdyn
1427 && (newdef
1428 || (bfd_is_com_section (sec)
1429 && (oldweak || oldfunc)))
1430 && olddyn
1431 && olddef
1432 && h->def_dynamic)
1433 {
1434 /* Change the hash table entry to undefined, and let
1435 _bfd_generic_link_add_one_symbol do the right thing with the
1436 new definition. */
1437
1438 h->root.type = bfd_link_hash_undefined;
1439 h->root.u.undef.abfd = h->root.u.def.section->owner;
1440 *size_change_ok = TRUE;
1441
1442 olddef = FALSE;
1443 olddyncommon = FALSE;
1444
1445 /* We again permit a type change when a common symbol may be
1446 overriding a function. */
1447
1448 if (bfd_is_com_section (sec))
1449 {
1450 if (oldfunc)
1451 {
1452 /* If a common symbol overrides a function, make sure
1453 that it isn't defined dynamically nor has type
1454 function. */
1455 h->def_dynamic = 0;
1456 h->type = STT_NOTYPE;
1457 }
1458 *type_change_ok = TRUE;
1459 }
1460
1461 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1462 flip = *sym_hash;
1463 else
1464 /* This union may have been set to be non-NULL when this symbol
1465 was seen in a dynamic object. We must force the union to be
1466 NULL, so that it is correct for a regular symbol. */
1467 h->verinfo.vertree = NULL;
1468 }
1469
1470 /* Handle the special case of a new common symbol merging with an
1471 old symbol that looks like it might be a common symbol defined in
1472 a shared object. Note that we have already handled the case in
1473 which a new common symbol should simply override the definition
1474 in the shared library. */
1475
1476 if (! newdyn
1477 && bfd_is_com_section (sec)
1478 && olddyncommon)
1479 {
1480 /* It would be best if we could set the hash table entry to a
1481 common symbol, but we don't know what to use for the section
1482 or the alignment. */
1483 if (! ((*info->callbacks->multiple_common)
1484 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1485 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1486 return FALSE;
1487
1488 /* If the presumed common symbol in the dynamic object is
1489 larger, pretend that the new symbol has its size. */
1490
1491 if (h->size > *pvalue)
1492 *pvalue = h->size;
1493
1494 /* We need to remember the alignment required by the symbol
1495 in the dynamic object. */
1496 BFD_ASSERT (pold_alignment);
1497 *pold_alignment = h->root.u.def.section->alignment_power;
1498
1499 olddef = FALSE;
1500 olddyncommon = FALSE;
1501
1502 h->root.type = bfd_link_hash_undefined;
1503 h->root.u.undef.abfd = h->root.u.def.section->owner;
1504
1505 *size_change_ok = TRUE;
1506 *type_change_ok = TRUE;
1507
1508 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1509 flip = *sym_hash;
1510 else
1511 h->verinfo.vertree = NULL;
1512 }
1513
1514 if (flip != NULL)
1515 {
1516 /* Handle the case where we had a versioned symbol in a dynamic
1517 library and now find a definition in a normal object. In this
1518 case, we make the versioned symbol point to the normal one. */
1519 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1520 flip->root.type = h->root.type;
1521 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1522 h->root.type = bfd_link_hash_indirect;
1523 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1524 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1525 if (h->def_dynamic)
1526 {
1527 h->def_dynamic = 0;
1528 flip->ref_dynamic = 1;
1529 }
1530 }
1531
1532 return TRUE;
1533 }
1534
1535 /* This function is called to create an indirect symbol from the
1536 default for the symbol with the default version if needed. The
1537 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1538 set DYNSYM if the new indirect symbol is dynamic. */
1539
1540 bfd_boolean
1541 _bfd_elf_add_default_symbol (bfd *abfd,
1542 struct bfd_link_info *info,
1543 struct elf_link_hash_entry *h,
1544 const char *name,
1545 Elf_Internal_Sym *sym,
1546 asection **psec,
1547 bfd_vma *value,
1548 bfd_boolean *dynsym,
1549 bfd_boolean override)
1550 {
1551 bfd_boolean type_change_ok;
1552 bfd_boolean size_change_ok;
1553 bfd_boolean skip;
1554 char *shortname;
1555 struct elf_link_hash_entry *hi;
1556 struct bfd_link_hash_entry *bh;
1557 const struct elf_backend_data *bed;
1558 bfd_boolean collect;
1559 bfd_boolean dynamic;
1560 char *p;
1561 size_t len, shortlen;
1562 asection *sec;
1563
1564 /* If this symbol has a version, and it is the default version, we
1565 create an indirect symbol from the default name to the fully
1566 decorated name. This will cause external references which do not
1567 specify a version to be bound to this version of the symbol. */
1568 p = strchr (name, ELF_VER_CHR);
1569 if (p == NULL || p[1] != ELF_VER_CHR)
1570 return TRUE;
1571
1572 if (override)
1573 {
1574 /* We are overridden by an old definition. We need to check if we
1575 need to create the indirect symbol from the default name. */
1576 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1577 FALSE, FALSE);
1578 BFD_ASSERT (hi != NULL);
1579 if (hi == h)
1580 return TRUE;
1581 while (hi->root.type == bfd_link_hash_indirect
1582 || hi->root.type == bfd_link_hash_warning)
1583 {
1584 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1585 if (hi == h)
1586 return TRUE;
1587 }
1588 }
1589
1590 bed = get_elf_backend_data (abfd);
1591 collect = bed->collect;
1592 dynamic = (abfd->flags & DYNAMIC) != 0;
1593
1594 shortlen = p - name;
1595 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1596 if (shortname == NULL)
1597 return FALSE;
1598 memcpy (shortname, name, shortlen);
1599 shortname[shortlen] = '\0';
1600
1601 /* We are going to create a new symbol. Merge it with any existing
1602 symbol with this name. For the purposes of the merge, act as
1603 though we were defining the symbol we just defined, although we
1604 actually going to define an indirect symbol. */
1605 type_change_ok = FALSE;
1606 size_change_ok = FALSE;
1607 sec = *psec;
1608 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1609 NULL, &hi, &skip, &override,
1610 &type_change_ok, &size_change_ok))
1611 return FALSE;
1612
1613 if (skip)
1614 goto nondefault;
1615
1616 if (! override)
1617 {
1618 bh = &hi->root;
1619 if (! (_bfd_generic_link_add_one_symbol
1620 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1621 0, name, FALSE, collect, &bh)))
1622 return FALSE;
1623 hi = (struct elf_link_hash_entry *) bh;
1624 }
1625 else
1626 {
1627 /* In this case the symbol named SHORTNAME is overriding the
1628 indirect symbol we want to add. We were planning on making
1629 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1630 is the name without a version. NAME is the fully versioned
1631 name, and it is the default version.
1632
1633 Overriding means that we already saw a definition for the
1634 symbol SHORTNAME in a regular object, and it is overriding
1635 the symbol defined in the dynamic object.
1636
1637 When this happens, we actually want to change NAME, the
1638 symbol we just added, to refer to SHORTNAME. This will cause
1639 references to NAME in the shared object to become references
1640 to SHORTNAME in the regular object. This is what we expect
1641 when we override a function in a shared object: that the
1642 references in the shared object will be mapped to the
1643 definition in the regular object. */
1644
1645 while (hi->root.type == bfd_link_hash_indirect
1646 || hi->root.type == bfd_link_hash_warning)
1647 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1648
1649 h->root.type = bfd_link_hash_indirect;
1650 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1651 if (h->def_dynamic)
1652 {
1653 h->def_dynamic = 0;
1654 hi->ref_dynamic = 1;
1655 if (hi->ref_regular
1656 || hi->def_regular)
1657 {
1658 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1659 return FALSE;
1660 }
1661 }
1662
1663 /* Now set HI to H, so that the following code will set the
1664 other fields correctly. */
1665 hi = h;
1666 }
1667
1668 /* Check if HI is a warning symbol. */
1669 if (hi->root.type == bfd_link_hash_warning)
1670 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1671
1672 /* If there is a duplicate definition somewhere, then HI may not
1673 point to an indirect symbol. We will have reported an error to
1674 the user in that case. */
1675
1676 if (hi->root.type == bfd_link_hash_indirect)
1677 {
1678 struct elf_link_hash_entry *ht;
1679
1680 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1681 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1682
1683 /* See if the new flags lead us to realize that the symbol must
1684 be dynamic. */
1685 if (! *dynsym)
1686 {
1687 if (! dynamic)
1688 {
1689 if (info->shared
1690 || hi->ref_dynamic)
1691 *dynsym = TRUE;
1692 }
1693 else
1694 {
1695 if (hi->ref_regular)
1696 *dynsym = TRUE;
1697 }
1698 }
1699 }
1700
1701 /* We also need to define an indirection from the nondefault version
1702 of the symbol. */
1703
1704 nondefault:
1705 len = strlen (name);
1706 shortname = bfd_hash_allocate (&info->hash->table, len);
1707 if (shortname == NULL)
1708 return FALSE;
1709 memcpy (shortname, name, shortlen);
1710 memcpy (shortname + shortlen, p + 1, len - shortlen);
1711
1712 /* Once again, merge with any existing symbol. */
1713 type_change_ok = FALSE;
1714 size_change_ok = FALSE;
1715 sec = *psec;
1716 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1717 NULL, &hi, &skip, &override,
1718 &type_change_ok, &size_change_ok))
1719 return FALSE;
1720
1721 if (skip)
1722 return TRUE;
1723
1724 if (override)
1725 {
1726 /* Here SHORTNAME is a versioned name, so we don't expect to see
1727 the type of override we do in the case above unless it is
1728 overridden by a versioned definition. */
1729 if (hi->root.type != bfd_link_hash_defined
1730 && hi->root.type != bfd_link_hash_defweak)
1731 (*_bfd_error_handler)
1732 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1733 abfd, shortname);
1734 }
1735 else
1736 {
1737 bh = &hi->root;
1738 if (! (_bfd_generic_link_add_one_symbol
1739 (info, abfd, shortname, BSF_INDIRECT,
1740 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1741 return FALSE;
1742 hi = (struct elf_link_hash_entry *) bh;
1743
1744 /* If there is a duplicate definition somewhere, then HI may not
1745 point to an indirect symbol. We will have reported an error
1746 to the user in that case. */
1747
1748 if (hi->root.type == bfd_link_hash_indirect)
1749 {
1750 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1751
1752 /* See if the new flags lead us to realize that the symbol
1753 must be dynamic. */
1754 if (! *dynsym)
1755 {
1756 if (! dynamic)
1757 {
1758 if (info->shared
1759 || hi->ref_dynamic)
1760 *dynsym = TRUE;
1761 }
1762 else
1763 {
1764 if (hi->ref_regular)
1765 *dynsym = TRUE;
1766 }
1767 }
1768 }
1769 }
1770
1771 return TRUE;
1772 }
1773 \f
1774 /* This routine is used to export all defined symbols into the dynamic
1775 symbol table. It is called via elf_link_hash_traverse. */
1776
1777 bfd_boolean
1778 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1779 {
1780 struct elf_info_failed *eif = data;
1781
1782 /* Ignore this if we won't export it. */
1783 if (!eif->info->export_dynamic && !h->dynamic)
1784 return TRUE;
1785
1786 /* Ignore indirect symbols. These are added by the versioning code. */
1787 if (h->root.type == bfd_link_hash_indirect)
1788 return TRUE;
1789
1790 if (h->root.type == bfd_link_hash_warning)
1791 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1792
1793 if (h->dynindx == -1
1794 && (h->def_regular
1795 || h->ref_regular))
1796 {
1797 struct bfd_elf_version_tree *t;
1798 struct bfd_elf_version_expr *d;
1799
1800 for (t = eif->verdefs; t != NULL; t = t->next)
1801 {
1802 if (t->globals.list != NULL)
1803 {
1804 d = (*t->match) (&t->globals, NULL, h->root.root.string);
1805 if (d != NULL)
1806 goto doit;
1807 }
1808
1809 if (t->locals.list != NULL)
1810 {
1811 d = (*t->match) (&t->locals, NULL, h->root.root.string);
1812 if (d != NULL)
1813 return TRUE;
1814 }
1815 }
1816
1817 if (!eif->verdefs)
1818 {
1819 doit:
1820 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1821 {
1822 eif->failed = TRUE;
1823 return FALSE;
1824 }
1825 }
1826 }
1827
1828 return TRUE;
1829 }
1830 \f
1831 /* Look through the symbols which are defined in other shared
1832 libraries and referenced here. Update the list of version
1833 dependencies. This will be put into the .gnu.version_r section.
1834 This function is called via elf_link_hash_traverse. */
1835
1836 bfd_boolean
1837 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1838 void *data)
1839 {
1840 struct elf_find_verdep_info *rinfo = data;
1841 Elf_Internal_Verneed *t;
1842 Elf_Internal_Vernaux *a;
1843 bfd_size_type amt;
1844
1845 if (h->root.type == bfd_link_hash_warning)
1846 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1847
1848 /* We only care about symbols defined in shared objects with version
1849 information. */
1850 if (!h->def_dynamic
1851 || h->def_regular
1852 || h->dynindx == -1
1853 || h->verinfo.verdef == NULL)
1854 return TRUE;
1855
1856 /* See if we already know about this version. */
1857 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1858 {
1859 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1860 continue;
1861
1862 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1863 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1864 return TRUE;
1865
1866 break;
1867 }
1868
1869 /* This is a new version. Add it to tree we are building. */
1870
1871 if (t == NULL)
1872 {
1873 amt = sizeof *t;
1874 t = bfd_zalloc (rinfo->output_bfd, amt);
1875 if (t == NULL)
1876 {
1877 rinfo->failed = TRUE;
1878 return FALSE;
1879 }
1880
1881 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1882 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1883 elf_tdata (rinfo->output_bfd)->verref = t;
1884 }
1885
1886 amt = sizeof *a;
1887 a = bfd_zalloc (rinfo->output_bfd, amt);
1888 if (a == NULL)
1889 {
1890 rinfo->failed = TRUE;
1891 return FALSE;
1892 }
1893
1894 /* Note that we are copying a string pointer here, and testing it
1895 above. If bfd_elf_string_from_elf_section is ever changed to
1896 discard the string data when low in memory, this will have to be
1897 fixed. */
1898 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1899
1900 a->vna_flags = h->verinfo.verdef->vd_flags;
1901 a->vna_nextptr = t->vn_auxptr;
1902
1903 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1904 ++rinfo->vers;
1905
1906 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1907
1908 t->vn_auxptr = a;
1909
1910 return TRUE;
1911 }
1912
1913 /* Figure out appropriate versions for all the symbols. We may not
1914 have the version number script until we have read all of the input
1915 files, so until that point we don't know which symbols should be
1916 local. This function is called via elf_link_hash_traverse. */
1917
1918 bfd_boolean
1919 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1920 {
1921 struct elf_assign_sym_version_info *sinfo;
1922 struct bfd_link_info *info;
1923 const struct elf_backend_data *bed;
1924 struct elf_info_failed eif;
1925 char *p;
1926 bfd_size_type amt;
1927
1928 sinfo = data;
1929 info = sinfo->info;
1930
1931 if (h->root.type == bfd_link_hash_warning)
1932 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1933
1934 /* Fix the symbol flags. */
1935 eif.failed = FALSE;
1936 eif.info = info;
1937 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1938 {
1939 if (eif.failed)
1940 sinfo->failed = TRUE;
1941 return FALSE;
1942 }
1943
1944 /* We only need version numbers for symbols defined in regular
1945 objects. */
1946 if (!h->def_regular)
1947 return TRUE;
1948
1949 bed = get_elf_backend_data (sinfo->output_bfd);
1950 p = strchr (h->root.root.string, ELF_VER_CHR);
1951 if (p != NULL && h->verinfo.vertree == NULL)
1952 {
1953 struct bfd_elf_version_tree *t;
1954 bfd_boolean hidden;
1955
1956 hidden = TRUE;
1957
1958 /* There are two consecutive ELF_VER_CHR characters if this is
1959 not a hidden symbol. */
1960 ++p;
1961 if (*p == ELF_VER_CHR)
1962 {
1963 hidden = FALSE;
1964 ++p;
1965 }
1966
1967 /* If there is no version string, we can just return out. */
1968 if (*p == '\0')
1969 {
1970 if (hidden)
1971 h->hidden = 1;
1972 return TRUE;
1973 }
1974
1975 /* Look for the version. If we find it, it is no longer weak. */
1976 for (t = sinfo->verdefs; t != NULL; t = t->next)
1977 {
1978 if (strcmp (t->name, p) == 0)
1979 {
1980 size_t len;
1981 char *alc;
1982 struct bfd_elf_version_expr *d;
1983
1984 len = p - h->root.root.string;
1985 alc = bfd_malloc (len);
1986 if (alc == NULL)
1987 {
1988 sinfo->failed = TRUE;
1989 return FALSE;
1990 }
1991 memcpy (alc, h->root.root.string, len - 1);
1992 alc[len - 1] = '\0';
1993 if (alc[len - 2] == ELF_VER_CHR)
1994 alc[len - 2] = '\0';
1995
1996 h->verinfo.vertree = t;
1997 t->used = TRUE;
1998 d = NULL;
1999
2000 if (t->globals.list != NULL)
2001 d = (*t->match) (&t->globals, NULL, alc);
2002
2003 /* See if there is anything to force this symbol to
2004 local scope. */
2005 if (d == NULL && t->locals.list != NULL)
2006 {
2007 d = (*t->match) (&t->locals, NULL, alc);
2008 if (d != NULL
2009 && h->dynindx != -1
2010 && ! info->export_dynamic)
2011 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2012 }
2013
2014 free (alc);
2015 break;
2016 }
2017 }
2018
2019 /* If we are building an application, we need to create a
2020 version node for this version. */
2021 if (t == NULL && info->executable)
2022 {
2023 struct bfd_elf_version_tree **pp;
2024 int version_index;
2025
2026 /* If we aren't going to export this symbol, we don't need
2027 to worry about it. */
2028 if (h->dynindx == -1)
2029 return TRUE;
2030
2031 amt = sizeof *t;
2032 t = bfd_zalloc (sinfo->output_bfd, amt);
2033 if (t == NULL)
2034 {
2035 sinfo->failed = TRUE;
2036 return FALSE;
2037 }
2038
2039 t->name = p;
2040 t->name_indx = (unsigned int) -1;
2041 t->used = TRUE;
2042
2043 version_index = 1;
2044 /* Don't count anonymous version tag. */
2045 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
2046 version_index = 0;
2047 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
2048 ++version_index;
2049 t->vernum = version_index;
2050
2051 *pp = t;
2052
2053 h->verinfo.vertree = t;
2054 }
2055 else if (t == NULL)
2056 {
2057 /* We could not find the version for a symbol when
2058 generating a shared archive. Return an error. */
2059 (*_bfd_error_handler)
2060 (_("%B: version node not found for symbol %s"),
2061 sinfo->output_bfd, h->root.root.string);
2062 bfd_set_error (bfd_error_bad_value);
2063 sinfo->failed = TRUE;
2064 return FALSE;
2065 }
2066
2067 if (hidden)
2068 h->hidden = 1;
2069 }
2070
2071 /* If we don't have a version for this symbol, see if we can find
2072 something. */
2073 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
2074 {
2075 struct bfd_elf_version_tree *t;
2076 struct bfd_elf_version_tree *local_ver, *global_ver, *exist_ver;
2077 struct bfd_elf_version_expr *d;
2078
2079 /* See if can find what version this symbol is in. If the
2080 symbol is supposed to be local, then don't actually register
2081 it. */
2082 local_ver = NULL;
2083 global_ver = NULL;
2084 exist_ver = NULL;
2085 for (t = sinfo->verdefs; t != NULL; t = t->next)
2086 {
2087 if (t->globals.list != NULL)
2088 {
2089 d = NULL;
2090 while ((d = (*t->match) (&t->globals, d,
2091 h->root.root.string)) != NULL)
2092 {
2093 global_ver = t;
2094 local_ver = NULL;
2095 if (d->symver)
2096 exist_ver = t;
2097 d->script = 1;
2098 /* If the match is a wildcard pattern, keep looking for
2099 a more explicit, perhaps even local, match. */
2100 if (d->literal)
2101 break;
2102 }
2103
2104 if (d != NULL)
2105 break;
2106 }
2107
2108 if (t->locals.list != NULL)
2109 {
2110 d = NULL;
2111 while ((d = (*t->match) (&t->locals, d,
2112 h->root.root.string)) != NULL)
2113 {
2114 local_ver = t;
2115 /* If the match is a wildcard pattern, keep looking for
2116 a more explicit, perhaps even global, match. */
2117 if (d->literal)
2118 {
2119 /* An exact match overrides a global wildcard. */
2120 global_ver = NULL;
2121 break;
2122 }
2123 }
2124
2125 if (d != NULL)
2126 break;
2127 }
2128 }
2129
2130 if (global_ver != NULL)
2131 {
2132 h->verinfo.vertree = global_ver;
2133 /* If we already have a versioned symbol that matches the
2134 node for this symbol, then we don't want to create a
2135 duplicate from the unversioned symbol. Instead hide the
2136 unversioned symbol. */
2137 if (exist_ver == global_ver)
2138 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2139 }
2140 else if (local_ver != NULL)
2141 {
2142 h->verinfo.vertree = local_ver;
2143 if (!info->export_dynamic
2144 || exist_ver == local_ver)
2145 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2146 }
2147 }
2148
2149 return TRUE;
2150 }
2151 \f
2152 /* Read and swap the relocs from the section indicated by SHDR. This
2153 may be either a REL or a RELA section. The relocations are
2154 translated into RELA relocations and stored in INTERNAL_RELOCS,
2155 which should have already been allocated to contain enough space.
2156 The EXTERNAL_RELOCS are a buffer where the external form of the
2157 relocations should be stored.
2158
2159 Returns FALSE if something goes wrong. */
2160
2161 static bfd_boolean
2162 elf_link_read_relocs_from_section (bfd *abfd,
2163 asection *sec,
2164 Elf_Internal_Shdr *shdr,
2165 void *external_relocs,
2166 Elf_Internal_Rela *internal_relocs)
2167 {
2168 const struct elf_backend_data *bed;
2169 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2170 const bfd_byte *erela;
2171 const bfd_byte *erelaend;
2172 Elf_Internal_Rela *irela;
2173 Elf_Internal_Shdr *symtab_hdr;
2174 size_t nsyms;
2175
2176 /* Position ourselves at the start of the section. */
2177 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2178 return FALSE;
2179
2180 /* Read the relocations. */
2181 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2182 return FALSE;
2183
2184 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2185 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
2186
2187 bed = get_elf_backend_data (abfd);
2188
2189 /* Convert the external relocations to the internal format. */
2190 if (shdr->sh_entsize == bed->s->sizeof_rel)
2191 swap_in = bed->s->swap_reloc_in;
2192 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2193 swap_in = bed->s->swap_reloca_in;
2194 else
2195 {
2196 bfd_set_error (bfd_error_wrong_format);
2197 return FALSE;
2198 }
2199
2200 erela = external_relocs;
2201 erelaend = erela + shdr->sh_size;
2202 irela = internal_relocs;
2203 while (erela < erelaend)
2204 {
2205 bfd_vma r_symndx;
2206
2207 (*swap_in) (abfd, erela, irela);
2208 r_symndx = ELF32_R_SYM (irela->r_info);
2209 if (bed->s->arch_size == 64)
2210 r_symndx >>= 24;
2211 if ((size_t) r_symndx >= nsyms)
2212 {
2213 (*_bfd_error_handler)
2214 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2215 " for offset 0x%lx in section `%A'"),
2216 abfd, sec,
2217 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2218 bfd_set_error (bfd_error_bad_value);
2219 return FALSE;
2220 }
2221 irela += bed->s->int_rels_per_ext_rel;
2222 erela += shdr->sh_entsize;
2223 }
2224
2225 return TRUE;
2226 }
2227
2228 /* Read and swap the relocs for a section O. They may have been
2229 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2230 not NULL, they are used as buffers to read into. They are known to
2231 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2232 the return value is allocated using either malloc or bfd_alloc,
2233 according to the KEEP_MEMORY argument. If O has two relocation
2234 sections (both REL and RELA relocations), then the REL_HDR
2235 relocations will appear first in INTERNAL_RELOCS, followed by the
2236 REL_HDR2 relocations. */
2237
2238 Elf_Internal_Rela *
2239 _bfd_elf_link_read_relocs (bfd *abfd,
2240 asection *o,
2241 void *external_relocs,
2242 Elf_Internal_Rela *internal_relocs,
2243 bfd_boolean keep_memory)
2244 {
2245 Elf_Internal_Shdr *rel_hdr;
2246 void *alloc1 = NULL;
2247 Elf_Internal_Rela *alloc2 = NULL;
2248 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2249
2250 if (elf_section_data (o)->relocs != NULL)
2251 return elf_section_data (o)->relocs;
2252
2253 if (o->reloc_count == 0)
2254 return NULL;
2255
2256 rel_hdr = &elf_section_data (o)->rel_hdr;
2257
2258 if (internal_relocs == NULL)
2259 {
2260 bfd_size_type size;
2261
2262 size = o->reloc_count;
2263 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2264 if (keep_memory)
2265 internal_relocs = alloc2 = bfd_alloc (abfd, size);
2266 else
2267 internal_relocs = alloc2 = bfd_malloc (size);
2268 if (internal_relocs == NULL)
2269 goto error_return;
2270 }
2271
2272 if (external_relocs == NULL)
2273 {
2274 bfd_size_type size = rel_hdr->sh_size;
2275
2276 if (elf_section_data (o)->rel_hdr2)
2277 size += elf_section_data (o)->rel_hdr2->sh_size;
2278 alloc1 = bfd_malloc (size);
2279 if (alloc1 == NULL)
2280 goto error_return;
2281 external_relocs = alloc1;
2282 }
2283
2284 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
2285 external_relocs,
2286 internal_relocs))
2287 goto error_return;
2288 if (elf_section_data (o)->rel_hdr2
2289 && (!elf_link_read_relocs_from_section
2290 (abfd, o,
2291 elf_section_data (o)->rel_hdr2,
2292 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2293 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2294 * bed->s->int_rels_per_ext_rel))))
2295 goto error_return;
2296
2297 /* Cache the results for next time, if we can. */
2298 if (keep_memory)
2299 elf_section_data (o)->relocs = internal_relocs;
2300
2301 if (alloc1 != NULL)
2302 free (alloc1);
2303
2304 /* Don't free alloc2, since if it was allocated we are passing it
2305 back (under the name of internal_relocs). */
2306
2307 return internal_relocs;
2308
2309 error_return:
2310 if (alloc1 != NULL)
2311 free (alloc1);
2312 if (alloc2 != NULL)
2313 {
2314 if (keep_memory)
2315 bfd_release (abfd, alloc2);
2316 else
2317 free (alloc2);
2318 }
2319 return NULL;
2320 }
2321
2322 /* Compute the size of, and allocate space for, REL_HDR which is the
2323 section header for a section containing relocations for O. */
2324
2325 bfd_boolean
2326 _bfd_elf_link_size_reloc_section (bfd *abfd,
2327 Elf_Internal_Shdr *rel_hdr,
2328 asection *o)
2329 {
2330 bfd_size_type reloc_count;
2331 bfd_size_type num_rel_hashes;
2332
2333 /* Figure out how many relocations there will be. */
2334 if (rel_hdr == &elf_section_data (o)->rel_hdr)
2335 reloc_count = elf_section_data (o)->rel_count;
2336 else
2337 reloc_count = elf_section_data (o)->rel_count2;
2338
2339 num_rel_hashes = o->reloc_count;
2340 if (num_rel_hashes < reloc_count)
2341 num_rel_hashes = reloc_count;
2342
2343 /* That allows us to calculate the size of the section. */
2344 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
2345
2346 /* The contents field must last into write_object_contents, so we
2347 allocate it with bfd_alloc rather than malloc. Also since we
2348 cannot be sure that the contents will actually be filled in,
2349 we zero the allocated space. */
2350 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
2351 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2352 return FALSE;
2353
2354 /* We only allocate one set of hash entries, so we only do it the
2355 first time we are called. */
2356 if (elf_section_data (o)->rel_hashes == NULL
2357 && num_rel_hashes)
2358 {
2359 struct elf_link_hash_entry **p;
2360
2361 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
2362 if (p == NULL)
2363 return FALSE;
2364
2365 elf_section_data (o)->rel_hashes = p;
2366 }
2367
2368 return TRUE;
2369 }
2370
2371 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2372 originated from the section given by INPUT_REL_HDR) to the
2373 OUTPUT_BFD. */
2374
2375 bfd_boolean
2376 _bfd_elf_link_output_relocs (bfd *output_bfd,
2377 asection *input_section,
2378 Elf_Internal_Shdr *input_rel_hdr,
2379 Elf_Internal_Rela *internal_relocs,
2380 struct elf_link_hash_entry **rel_hash
2381 ATTRIBUTE_UNUSED)
2382 {
2383 Elf_Internal_Rela *irela;
2384 Elf_Internal_Rela *irelaend;
2385 bfd_byte *erel;
2386 Elf_Internal_Shdr *output_rel_hdr;
2387 asection *output_section;
2388 unsigned int *rel_countp = NULL;
2389 const struct elf_backend_data *bed;
2390 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2391
2392 output_section = input_section->output_section;
2393 output_rel_hdr = NULL;
2394
2395 if (elf_section_data (output_section)->rel_hdr.sh_entsize
2396 == input_rel_hdr->sh_entsize)
2397 {
2398 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2399 rel_countp = &elf_section_data (output_section)->rel_count;
2400 }
2401 else if (elf_section_data (output_section)->rel_hdr2
2402 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2403 == input_rel_hdr->sh_entsize))
2404 {
2405 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2406 rel_countp = &elf_section_data (output_section)->rel_count2;
2407 }
2408 else
2409 {
2410 (*_bfd_error_handler)
2411 (_("%B: relocation size mismatch in %B section %A"),
2412 output_bfd, input_section->owner, input_section);
2413 bfd_set_error (bfd_error_wrong_format);
2414 return FALSE;
2415 }
2416
2417 bed = get_elf_backend_data (output_bfd);
2418 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2419 swap_out = bed->s->swap_reloc_out;
2420 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2421 swap_out = bed->s->swap_reloca_out;
2422 else
2423 abort ();
2424
2425 erel = output_rel_hdr->contents;
2426 erel += *rel_countp * input_rel_hdr->sh_entsize;
2427 irela = internal_relocs;
2428 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2429 * bed->s->int_rels_per_ext_rel);
2430 while (irela < irelaend)
2431 {
2432 (*swap_out) (output_bfd, irela, erel);
2433 irela += bed->s->int_rels_per_ext_rel;
2434 erel += input_rel_hdr->sh_entsize;
2435 }
2436
2437 /* Bump the counter, so that we know where to add the next set of
2438 relocations. */
2439 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2440
2441 return TRUE;
2442 }
2443 \f
2444 /* Make weak undefined symbols in PIE dynamic. */
2445
2446 bfd_boolean
2447 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2448 struct elf_link_hash_entry *h)
2449 {
2450 if (info->pie
2451 && h->dynindx == -1
2452 && h->root.type == bfd_link_hash_undefweak)
2453 return bfd_elf_link_record_dynamic_symbol (info, h);
2454
2455 return TRUE;
2456 }
2457
2458 /* Fix up the flags for a symbol. This handles various cases which
2459 can only be fixed after all the input files are seen. This is
2460 currently called by both adjust_dynamic_symbol and
2461 assign_sym_version, which is unnecessary but perhaps more robust in
2462 the face of future changes. */
2463
2464 bfd_boolean
2465 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2466 struct elf_info_failed *eif)
2467 {
2468 const struct elf_backend_data *bed;
2469
2470 /* If this symbol was mentioned in a non-ELF file, try to set
2471 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2472 permit a non-ELF file to correctly refer to a symbol defined in
2473 an ELF dynamic object. */
2474 if (h->non_elf)
2475 {
2476 while (h->root.type == bfd_link_hash_indirect)
2477 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2478
2479 if (h->root.type != bfd_link_hash_defined
2480 && h->root.type != bfd_link_hash_defweak)
2481 {
2482 h->ref_regular = 1;
2483 h->ref_regular_nonweak = 1;
2484 }
2485 else
2486 {
2487 if (h->root.u.def.section->owner != NULL
2488 && (bfd_get_flavour (h->root.u.def.section->owner)
2489 == bfd_target_elf_flavour))
2490 {
2491 h->ref_regular = 1;
2492 h->ref_regular_nonweak = 1;
2493 }
2494 else
2495 h->def_regular = 1;
2496 }
2497
2498 if (h->dynindx == -1
2499 && (h->def_dynamic
2500 || h->ref_dynamic))
2501 {
2502 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2503 {
2504 eif->failed = TRUE;
2505 return FALSE;
2506 }
2507 }
2508 }
2509 else
2510 {
2511 /* Unfortunately, NON_ELF is only correct if the symbol
2512 was first seen in a non-ELF file. Fortunately, if the symbol
2513 was first seen in an ELF file, we're probably OK unless the
2514 symbol was defined in a non-ELF file. Catch that case here.
2515 FIXME: We're still in trouble if the symbol was first seen in
2516 a dynamic object, and then later in a non-ELF regular object. */
2517 if ((h->root.type == bfd_link_hash_defined
2518 || h->root.type == bfd_link_hash_defweak)
2519 && !h->def_regular
2520 && (h->root.u.def.section->owner != NULL
2521 ? (bfd_get_flavour (h->root.u.def.section->owner)
2522 != bfd_target_elf_flavour)
2523 : (bfd_is_abs_section (h->root.u.def.section)
2524 && !h->def_dynamic)))
2525 h->def_regular = 1;
2526 }
2527
2528 /* Backend specific symbol fixup. */
2529 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2530 if (bed->elf_backend_fixup_symbol
2531 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2532 return FALSE;
2533
2534 /* If this is a final link, and the symbol was defined as a common
2535 symbol in a regular object file, and there was no definition in
2536 any dynamic object, then the linker will have allocated space for
2537 the symbol in a common section but the DEF_REGULAR
2538 flag will not have been set. */
2539 if (h->root.type == bfd_link_hash_defined
2540 && !h->def_regular
2541 && h->ref_regular
2542 && !h->def_dynamic
2543 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2544 h->def_regular = 1;
2545
2546 /* If -Bsymbolic was used (which means to bind references to global
2547 symbols to the definition within the shared object), and this
2548 symbol was defined in a regular object, then it actually doesn't
2549 need a PLT entry. Likewise, if the symbol has non-default
2550 visibility. If the symbol has hidden or internal visibility, we
2551 will force it local. */
2552 if (h->needs_plt
2553 && eif->info->shared
2554 && is_elf_hash_table (eif->info->hash)
2555 && (SYMBOLIC_BIND (eif->info, h)
2556 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2557 && h->def_regular)
2558 {
2559 bfd_boolean force_local;
2560
2561 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2562 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2563 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2564 }
2565
2566 /* If a weak undefined symbol has non-default visibility, we also
2567 hide it from the dynamic linker. */
2568 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2569 && h->root.type == bfd_link_hash_undefweak)
2570 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2571
2572 /* If this is a weak defined symbol in a dynamic object, and we know
2573 the real definition in the dynamic object, copy interesting flags
2574 over to the real definition. */
2575 if (h->u.weakdef != NULL)
2576 {
2577 struct elf_link_hash_entry *weakdef;
2578
2579 weakdef = h->u.weakdef;
2580 if (h->root.type == bfd_link_hash_indirect)
2581 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2582
2583 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2584 || h->root.type == bfd_link_hash_defweak);
2585 BFD_ASSERT (weakdef->def_dynamic);
2586
2587 /* If the real definition is defined by a regular object file,
2588 don't do anything special. See the longer description in
2589 _bfd_elf_adjust_dynamic_symbol, below. */
2590 if (weakdef->def_regular)
2591 h->u.weakdef = NULL;
2592 else
2593 {
2594 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2595 || weakdef->root.type == bfd_link_hash_defweak);
2596 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2597 }
2598 }
2599
2600 return TRUE;
2601 }
2602
2603 /* Make the backend pick a good value for a dynamic symbol. This is
2604 called via elf_link_hash_traverse, and also calls itself
2605 recursively. */
2606
2607 bfd_boolean
2608 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2609 {
2610 struct elf_info_failed *eif = data;
2611 bfd *dynobj;
2612 const struct elf_backend_data *bed;
2613
2614 if (! is_elf_hash_table (eif->info->hash))
2615 return FALSE;
2616
2617 if (h->root.type == bfd_link_hash_warning)
2618 {
2619 h->got = elf_hash_table (eif->info)->init_got_offset;
2620 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2621
2622 /* When warning symbols are created, they **replace** the "real"
2623 entry in the hash table, thus we never get to see the real
2624 symbol in a hash traversal. So look at it now. */
2625 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2626 }
2627
2628 /* Ignore indirect symbols. These are added by the versioning code. */
2629 if (h->root.type == bfd_link_hash_indirect)
2630 return TRUE;
2631
2632 /* Fix the symbol flags. */
2633 if (! _bfd_elf_fix_symbol_flags (h, eif))
2634 return FALSE;
2635
2636 /* If this symbol does not require a PLT entry, and it is not
2637 defined by a dynamic object, or is not referenced by a regular
2638 object, ignore it. We do have to handle a weak defined symbol,
2639 even if no regular object refers to it, if we decided to add it
2640 to the dynamic symbol table. FIXME: Do we normally need to worry
2641 about symbols which are defined by one dynamic object and
2642 referenced by another one? */
2643 if (!h->needs_plt
2644 && (h->def_regular
2645 || !h->def_dynamic
2646 || (!h->ref_regular
2647 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2648 {
2649 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2650 return TRUE;
2651 }
2652
2653 /* If we've already adjusted this symbol, don't do it again. This
2654 can happen via a recursive call. */
2655 if (h->dynamic_adjusted)
2656 return TRUE;
2657
2658 /* Don't look at this symbol again. Note that we must set this
2659 after checking the above conditions, because we may look at a
2660 symbol once, decide not to do anything, and then get called
2661 recursively later after REF_REGULAR is set below. */
2662 h->dynamic_adjusted = 1;
2663
2664 /* If this is a weak definition, and we know a real definition, and
2665 the real symbol is not itself defined by a regular object file,
2666 then get a good value for the real definition. We handle the
2667 real symbol first, for the convenience of the backend routine.
2668
2669 Note that there is a confusing case here. If the real definition
2670 is defined by a regular object file, we don't get the real symbol
2671 from the dynamic object, but we do get the weak symbol. If the
2672 processor backend uses a COPY reloc, then if some routine in the
2673 dynamic object changes the real symbol, we will not see that
2674 change in the corresponding weak symbol. This is the way other
2675 ELF linkers work as well, and seems to be a result of the shared
2676 library model.
2677
2678 I will clarify this issue. Most SVR4 shared libraries define the
2679 variable _timezone and define timezone as a weak synonym. The
2680 tzset call changes _timezone. If you write
2681 extern int timezone;
2682 int _timezone = 5;
2683 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2684 you might expect that, since timezone is a synonym for _timezone,
2685 the same number will print both times. However, if the processor
2686 backend uses a COPY reloc, then actually timezone will be copied
2687 into your process image, and, since you define _timezone
2688 yourself, _timezone will not. Thus timezone and _timezone will
2689 wind up at different memory locations. The tzset call will set
2690 _timezone, leaving timezone unchanged. */
2691
2692 if (h->u.weakdef != NULL)
2693 {
2694 /* If we get to this point, we know there is an implicit
2695 reference by a regular object file via the weak symbol H.
2696 FIXME: Is this really true? What if the traversal finds
2697 H->U.WEAKDEF before it finds H? */
2698 h->u.weakdef->ref_regular = 1;
2699
2700 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2701 return FALSE;
2702 }
2703
2704 /* If a symbol has no type and no size and does not require a PLT
2705 entry, then we are probably about to do the wrong thing here: we
2706 are probably going to create a COPY reloc for an empty object.
2707 This case can arise when a shared object is built with assembly
2708 code, and the assembly code fails to set the symbol type. */
2709 if (h->size == 0
2710 && h->type == STT_NOTYPE
2711 && !h->needs_plt)
2712 (*_bfd_error_handler)
2713 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2714 h->root.root.string);
2715
2716 dynobj = elf_hash_table (eif->info)->dynobj;
2717 bed = get_elf_backend_data (dynobj);
2718
2719 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2720 {
2721 eif->failed = TRUE;
2722 return FALSE;
2723 }
2724
2725 return TRUE;
2726 }
2727
2728 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2729 DYNBSS. */
2730
2731 bfd_boolean
2732 _bfd_elf_adjust_dynamic_copy (struct elf_link_hash_entry *h,
2733 asection *dynbss)
2734 {
2735 unsigned int power_of_two;
2736 bfd_vma mask;
2737 asection *sec = h->root.u.def.section;
2738
2739 /* The section aligment of definition is the maximum alignment
2740 requirement of symbols defined in the section. Since we don't
2741 know the symbol alignment requirement, we start with the
2742 maximum alignment and check low bits of the symbol address
2743 for the minimum alignment. */
2744 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2745 mask = ((bfd_vma) 1 << power_of_two) - 1;
2746 while ((h->root.u.def.value & mask) != 0)
2747 {
2748 mask >>= 1;
2749 --power_of_two;
2750 }
2751
2752 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2753 dynbss))
2754 {
2755 /* Adjust the section alignment if needed. */
2756 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2757 power_of_two))
2758 return FALSE;
2759 }
2760
2761 /* We make sure that the symbol will be aligned properly. */
2762 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2763
2764 /* Define the symbol as being at this point in DYNBSS. */
2765 h->root.u.def.section = dynbss;
2766 h->root.u.def.value = dynbss->size;
2767
2768 /* Increment the size of DYNBSS to make room for the symbol. */
2769 dynbss->size += h->size;
2770
2771 return TRUE;
2772 }
2773
2774 /* Adjust all external symbols pointing into SEC_MERGE sections
2775 to reflect the object merging within the sections. */
2776
2777 bfd_boolean
2778 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2779 {
2780 asection *sec;
2781
2782 if (h->root.type == bfd_link_hash_warning)
2783 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2784
2785 if ((h->root.type == bfd_link_hash_defined
2786 || h->root.type == bfd_link_hash_defweak)
2787 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2788 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2789 {
2790 bfd *output_bfd = data;
2791
2792 h->root.u.def.value =
2793 _bfd_merged_section_offset (output_bfd,
2794 &h->root.u.def.section,
2795 elf_section_data (sec)->sec_info,
2796 h->root.u.def.value);
2797 }
2798
2799 return TRUE;
2800 }
2801
2802 /* Returns false if the symbol referred to by H should be considered
2803 to resolve local to the current module, and true if it should be
2804 considered to bind dynamically. */
2805
2806 bfd_boolean
2807 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2808 struct bfd_link_info *info,
2809 bfd_boolean ignore_protected)
2810 {
2811 bfd_boolean binding_stays_local_p;
2812 const struct elf_backend_data *bed;
2813 struct elf_link_hash_table *hash_table;
2814
2815 if (h == NULL)
2816 return FALSE;
2817
2818 while (h->root.type == bfd_link_hash_indirect
2819 || h->root.type == bfd_link_hash_warning)
2820 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2821
2822 /* If it was forced local, then clearly it's not dynamic. */
2823 if (h->dynindx == -1)
2824 return FALSE;
2825 if (h->forced_local)
2826 return FALSE;
2827
2828 /* Identify the cases where name binding rules say that a
2829 visible symbol resolves locally. */
2830 binding_stays_local_p = info->executable || SYMBOLIC_BIND (info, h);
2831
2832 switch (ELF_ST_VISIBILITY (h->other))
2833 {
2834 case STV_INTERNAL:
2835 case STV_HIDDEN:
2836 return FALSE;
2837
2838 case STV_PROTECTED:
2839 hash_table = elf_hash_table (info);
2840 if (!is_elf_hash_table (hash_table))
2841 return FALSE;
2842
2843 bed = get_elf_backend_data (hash_table->dynobj);
2844
2845 /* Proper resolution for function pointer equality may require
2846 that these symbols perhaps be resolved dynamically, even though
2847 we should be resolving them to the current module. */
2848 if (!ignore_protected || !bed->is_function_type (h->type))
2849 binding_stays_local_p = TRUE;
2850 break;
2851
2852 default:
2853 break;
2854 }
2855
2856 /* If it isn't defined locally, then clearly it's dynamic. */
2857 if (!h->def_regular)
2858 return TRUE;
2859
2860 /* Otherwise, the symbol is dynamic if binding rules don't tell
2861 us that it remains local. */
2862 return !binding_stays_local_p;
2863 }
2864
2865 /* Return true if the symbol referred to by H should be considered
2866 to resolve local to the current module, and false otherwise. Differs
2867 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2868 undefined symbols and weak symbols. */
2869
2870 bfd_boolean
2871 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2872 struct bfd_link_info *info,
2873 bfd_boolean local_protected)
2874 {
2875 const struct elf_backend_data *bed;
2876 struct elf_link_hash_table *hash_table;
2877
2878 /* If it's a local sym, of course we resolve locally. */
2879 if (h == NULL)
2880 return TRUE;
2881
2882 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2883 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2884 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2885 return TRUE;
2886
2887 /* Common symbols that become definitions don't get the DEF_REGULAR
2888 flag set, so test it first, and don't bail out. */
2889 if (ELF_COMMON_DEF_P (h))
2890 /* Do nothing. */;
2891 /* If we don't have a definition in a regular file, then we can't
2892 resolve locally. The sym is either undefined or dynamic. */
2893 else if (!h->def_regular)
2894 return FALSE;
2895
2896 /* Forced local symbols resolve locally. */
2897 if (h->forced_local)
2898 return TRUE;
2899
2900 /* As do non-dynamic symbols. */
2901 if (h->dynindx == -1)
2902 return TRUE;
2903
2904 /* At this point, we know the symbol is defined and dynamic. In an
2905 executable it must resolve locally, likewise when building symbolic
2906 shared libraries. */
2907 if (info->executable || SYMBOLIC_BIND (info, h))
2908 return TRUE;
2909
2910 /* Now deal with defined dynamic symbols in shared libraries. Ones
2911 with default visibility might not resolve locally. */
2912 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2913 return FALSE;
2914
2915 hash_table = elf_hash_table (info);
2916 if (!is_elf_hash_table (hash_table))
2917 return TRUE;
2918
2919 bed = get_elf_backend_data (hash_table->dynobj);
2920
2921 /* STV_PROTECTED non-function symbols are local. */
2922 if (!bed->is_function_type (h->type))
2923 return TRUE;
2924
2925 /* Function pointer equality tests may require that STV_PROTECTED
2926 symbols be treated as dynamic symbols, even when we know that the
2927 dynamic linker will resolve them locally. */
2928 return local_protected;
2929 }
2930
2931 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2932 aligned. Returns the first TLS output section. */
2933
2934 struct bfd_section *
2935 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2936 {
2937 struct bfd_section *sec, *tls;
2938 unsigned int align = 0;
2939
2940 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2941 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2942 break;
2943 tls = sec;
2944
2945 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2946 if (sec->alignment_power > align)
2947 align = sec->alignment_power;
2948
2949 elf_hash_table (info)->tls_sec = tls;
2950
2951 /* Ensure the alignment of the first section is the largest alignment,
2952 so that the tls segment starts aligned. */
2953 if (tls != NULL)
2954 tls->alignment_power = align;
2955
2956 return tls;
2957 }
2958
2959 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2960 static bfd_boolean
2961 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2962 Elf_Internal_Sym *sym)
2963 {
2964 const struct elf_backend_data *bed;
2965
2966 /* Local symbols do not count, but target specific ones might. */
2967 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2968 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2969 return FALSE;
2970
2971 bed = get_elf_backend_data (abfd);
2972 /* Function symbols do not count. */
2973 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
2974 return FALSE;
2975
2976 /* If the section is undefined, then so is the symbol. */
2977 if (sym->st_shndx == SHN_UNDEF)
2978 return FALSE;
2979
2980 /* If the symbol is defined in the common section, then
2981 it is a common definition and so does not count. */
2982 if (bed->common_definition (sym))
2983 return FALSE;
2984
2985 /* If the symbol is in a target specific section then we
2986 must rely upon the backend to tell us what it is. */
2987 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2988 /* FIXME - this function is not coded yet:
2989
2990 return _bfd_is_global_symbol_definition (abfd, sym);
2991
2992 Instead for now assume that the definition is not global,
2993 Even if this is wrong, at least the linker will behave
2994 in the same way that it used to do. */
2995 return FALSE;
2996
2997 return TRUE;
2998 }
2999
3000 /* Search the symbol table of the archive element of the archive ABFD
3001 whose archive map contains a mention of SYMDEF, and determine if
3002 the symbol is defined in this element. */
3003 static bfd_boolean
3004 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3005 {
3006 Elf_Internal_Shdr * hdr;
3007 bfd_size_type symcount;
3008 bfd_size_type extsymcount;
3009 bfd_size_type extsymoff;
3010 Elf_Internal_Sym *isymbuf;
3011 Elf_Internal_Sym *isym;
3012 Elf_Internal_Sym *isymend;
3013 bfd_boolean result;
3014
3015 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3016 if (abfd == NULL)
3017 return FALSE;
3018
3019 if (! bfd_check_format (abfd, bfd_object))
3020 return FALSE;
3021
3022 /* If we have already included the element containing this symbol in the
3023 link then we do not need to include it again. Just claim that any symbol
3024 it contains is not a definition, so that our caller will not decide to
3025 (re)include this element. */
3026 if (abfd->archive_pass)
3027 return FALSE;
3028
3029 /* Select the appropriate symbol table. */
3030 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3031 hdr = &elf_tdata (abfd)->symtab_hdr;
3032 else
3033 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3034
3035 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3036
3037 /* The sh_info field of the symtab header tells us where the
3038 external symbols start. We don't care about the local symbols. */
3039 if (elf_bad_symtab (abfd))
3040 {
3041 extsymcount = symcount;
3042 extsymoff = 0;
3043 }
3044 else
3045 {
3046 extsymcount = symcount - hdr->sh_info;
3047 extsymoff = hdr->sh_info;
3048 }
3049
3050 if (extsymcount == 0)
3051 return FALSE;
3052
3053 /* Read in the symbol table. */
3054 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3055 NULL, NULL, NULL);
3056 if (isymbuf == NULL)
3057 return FALSE;
3058
3059 /* Scan the symbol table looking for SYMDEF. */
3060 result = FALSE;
3061 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3062 {
3063 const char *name;
3064
3065 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3066 isym->st_name);
3067 if (name == NULL)
3068 break;
3069
3070 if (strcmp (name, symdef->name) == 0)
3071 {
3072 result = is_global_data_symbol_definition (abfd, isym);
3073 break;
3074 }
3075 }
3076
3077 free (isymbuf);
3078
3079 return result;
3080 }
3081 \f
3082 /* Add an entry to the .dynamic table. */
3083
3084 bfd_boolean
3085 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3086 bfd_vma tag,
3087 bfd_vma val)
3088 {
3089 struct elf_link_hash_table *hash_table;
3090 const struct elf_backend_data *bed;
3091 asection *s;
3092 bfd_size_type newsize;
3093 bfd_byte *newcontents;
3094 Elf_Internal_Dyn dyn;
3095
3096 hash_table = elf_hash_table (info);
3097 if (! is_elf_hash_table (hash_table))
3098 return FALSE;
3099
3100 bed = get_elf_backend_data (hash_table->dynobj);
3101 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
3102 BFD_ASSERT (s != NULL);
3103
3104 newsize = s->size + bed->s->sizeof_dyn;
3105 newcontents = bfd_realloc (s->contents, newsize);
3106 if (newcontents == NULL)
3107 return FALSE;
3108
3109 dyn.d_tag = tag;
3110 dyn.d_un.d_val = val;
3111 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3112
3113 s->size = newsize;
3114 s->contents = newcontents;
3115
3116 return TRUE;
3117 }
3118
3119 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3120 otherwise just check whether one already exists. Returns -1 on error,
3121 1 if a DT_NEEDED tag already exists, and 0 on success. */
3122
3123 static int
3124 elf_add_dt_needed_tag (bfd *abfd,
3125 struct bfd_link_info *info,
3126 const char *soname,
3127 bfd_boolean do_it)
3128 {
3129 struct elf_link_hash_table *hash_table;
3130 bfd_size_type oldsize;
3131 bfd_size_type strindex;
3132
3133 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3134 return -1;
3135
3136 hash_table = elf_hash_table (info);
3137 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
3138 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3139 if (strindex == (bfd_size_type) -1)
3140 return -1;
3141
3142 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
3143 {
3144 asection *sdyn;
3145 const struct elf_backend_data *bed;
3146 bfd_byte *extdyn;
3147
3148 bed = get_elf_backend_data (hash_table->dynobj);
3149 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
3150 if (sdyn != NULL)
3151 for (extdyn = sdyn->contents;
3152 extdyn < sdyn->contents + sdyn->size;
3153 extdyn += bed->s->sizeof_dyn)
3154 {
3155 Elf_Internal_Dyn dyn;
3156
3157 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3158 if (dyn.d_tag == DT_NEEDED
3159 && dyn.d_un.d_val == strindex)
3160 {
3161 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3162 return 1;
3163 }
3164 }
3165 }
3166
3167 if (do_it)
3168 {
3169 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3170 return -1;
3171
3172 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3173 return -1;
3174 }
3175 else
3176 /* We were just checking for existence of the tag. */
3177 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3178
3179 return 0;
3180 }
3181
3182 /* Sort symbol by value and section. */
3183 static int
3184 elf_sort_symbol (const void *arg1, const void *arg2)
3185 {
3186 const struct elf_link_hash_entry *h1;
3187 const struct elf_link_hash_entry *h2;
3188 bfd_signed_vma vdiff;
3189
3190 h1 = *(const struct elf_link_hash_entry **) arg1;
3191 h2 = *(const struct elf_link_hash_entry **) arg2;
3192 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3193 if (vdiff != 0)
3194 return vdiff > 0 ? 1 : -1;
3195 else
3196 {
3197 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3198 if (sdiff != 0)
3199 return sdiff > 0 ? 1 : -1;
3200 }
3201 return 0;
3202 }
3203
3204 /* This function is used to adjust offsets into .dynstr for
3205 dynamic symbols. This is called via elf_link_hash_traverse. */
3206
3207 static bfd_boolean
3208 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3209 {
3210 struct elf_strtab_hash *dynstr = data;
3211
3212 if (h->root.type == bfd_link_hash_warning)
3213 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3214
3215 if (h->dynindx != -1)
3216 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3217 return TRUE;
3218 }
3219
3220 /* Assign string offsets in .dynstr, update all structures referencing
3221 them. */
3222
3223 static bfd_boolean
3224 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3225 {
3226 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3227 struct elf_link_local_dynamic_entry *entry;
3228 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3229 bfd *dynobj = hash_table->dynobj;
3230 asection *sdyn;
3231 bfd_size_type size;
3232 const struct elf_backend_data *bed;
3233 bfd_byte *extdyn;
3234
3235 _bfd_elf_strtab_finalize (dynstr);
3236 size = _bfd_elf_strtab_size (dynstr);
3237
3238 bed = get_elf_backend_data (dynobj);
3239 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3240 BFD_ASSERT (sdyn != NULL);
3241
3242 /* Update all .dynamic entries referencing .dynstr strings. */
3243 for (extdyn = sdyn->contents;
3244 extdyn < sdyn->contents + sdyn->size;
3245 extdyn += bed->s->sizeof_dyn)
3246 {
3247 Elf_Internal_Dyn dyn;
3248
3249 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3250 switch (dyn.d_tag)
3251 {
3252 case DT_STRSZ:
3253 dyn.d_un.d_val = size;
3254 break;
3255 case DT_NEEDED:
3256 case DT_SONAME:
3257 case DT_RPATH:
3258 case DT_RUNPATH:
3259 case DT_FILTER:
3260 case DT_AUXILIARY:
3261 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3262 break;
3263 default:
3264 continue;
3265 }
3266 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3267 }
3268
3269 /* Now update local dynamic symbols. */
3270 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3271 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3272 entry->isym.st_name);
3273
3274 /* And the rest of dynamic symbols. */
3275 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3276
3277 /* Adjust version definitions. */
3278 if (elf_tdata (output_bfd)->cverdefs)
3279 {
3280 asection *s;
3281 bfd_byte *p;
3282 bfd_size_type i;
3283 Elf_Internal_Verdef def;
3284 Elf_Internal_Verdaux defaux;
3285
3286 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3287 p = s->contents;
3288 do
3289 {
3290 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3291 &def);
3292 p += sizeof (Elf_External_Verdef);
3293 if (def.vd_aux != sizeof (Elf_External_Verdef))
3294 continue;
3295 for (i = 0; i < def.vd_cnt; ++i)
3296 {
3297 _bfd_elf_swap_verdaux_in (output_bfd,
3298 (Elf_External_Verdaux *) p, &defaux);
3299 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3300 defaux.vda_name);
3301 _bfd_elf_swap_verdaux_out (output_bfd,
3302 &defaux, (Elf_External_Verdaux *) p);
3303 p += sizeof (Elf_External_Verdaux);
3304 }
3305 }
3306 while (def.vd_next);
3307 }
3308
3309 /* Adjust version references. */
3310 if (elf_tdata (output_bfd)->verref)
3311 {
3312 asection *s;
3313 bfd_byte *p;
3314 bfd_size_type i;
3315 Elf_Internal_Verneed need;
3316 Elf_Internal_Vernaux needaux;
3317
3318 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3319 p = s->contents;
3320 do
3321 {
3322 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3323 &need);
3324 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3325 _bfd_elf_swap_verneed_out (output_bfd, &need,
3326 (Elf_External_Verneed *) p);
3327 p += sizeof (Elf_External_Verneed);
3328 for (i = 0; i < need.vn_cnt; ++i)
3329 {
3330 _bfd_elf_swap_vernaux_in (output_bfd,
3331 (Elf_External_Vernaux *) p, &needaux);
3332 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3333 needaux.vna_name);
3334 _bfd_elf_swap_vernaux_out (output_bfd,
3335 &needaux,
3336 (Elf_External_Vernaux *) p);
3337 p += sizeof (Elf_External_Vernaux);
3338 }
3339 }
3340 while (need.vn_next);
3341 }
3342
3343 return TRUE;
3344 }
3345 \f
3346 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3347 The default is to only match when the INPUT and OUTPUT are exactly
3348 the same target. */
3349
3350 bfd_boolean
3351 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3352 const bfd_target *output)
3353 {
3354 return input == output;
3355 }
3356
3357 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3358 This version is used when different targets for the same architecture
3359 are virtually identical. */
3360
3361 bfd_boolean
3362 _bfd_elf_relocs_compatible (const bfd_target *input,
3363 const bfd_target *output)
3364 {
3365 const struct elf_backend_data *obed, *ibed;
3366
3367 if (input == output)
3368 return TRUE;
3369
3370 ibed = xvec_get_elf_backend_data (input);
3371 obed = xvec_get_elf_backend_data (output);
3372
3373 if (ibed->arch != obed->arch)
3374 return FALSE;
3375
3376 /* If both backends are using this function, deem them compatible. */
3377 return ibed->relocs_compatible == obed->relocs_compatible;
3378 }
3379
3380 /* Add symbols from an ELF object file to the linker hash table. */
3381
3382 static bfd_boolean
3383 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3384 {
3385 Elf_Internal_Shdr *hdr;
3386 bfd_size_type symcount;
3387 bfd_size_type extsymcount;
3388 bfd_size_type extsymoff;
3389 struct elf_link_hash_entry **sym_hash;
3390 bfd_boolean dynamic;
3391 Elf_External_Versym *extversym = NULL;
3392 Elf_External_Versym *ever;
3393 struct elf_link_hash_entry *weaks;
3394 struct elf_link_hash_entry **nondeflt_vers = NULL;
3395 bfd_size_type nondeflt_vers_cnt = 0;
3396 Elf_Internal_Sym *isymbuf = NULL;
3397 Elf_Internal_Sym *isym;
3398 Elf_Internal_Sym *isymend;
3399 const struct elf_backend_data *bed;
3400 bfd_boolean add_needed;
3401 struct elf_link_hash_table *htab;
3402 bfd_size_type amt;
3403 void *alloc_mark = NULL;
3404 struct bfd_hash_entry **old_table = NULL;
3405 unsigned int old_size = 0;
3406 unsigned int old_count = 0;
3407 void *old_tab = NULL;
3408 void *old_hash;
3409 void *old_ent;
3410 struct bfd_link_hash_entry *old_undefs = NULL;
3411 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3412 long old_dynsymcount = 0;
3413 size_t tabsize = 0;
3414 size_t hashsize = 0;
3415
3416 htab = elf_hash_table (info);
3417 bed = get_elf_backend_data (abfd);
3418
3419 if ((abfd->flags & DYNAMIC) == 0)
3420 dynamic = FALSE;
3421 else
3422 {
3423 dynamic = TRUE;
3424
3425 /* You can't use -r against a dynamic object. Also, there's no
3426 hope of using a dynamic object which does not exactly match
3427 the format of the output file. */
3428 if (info->relocatable
3429 || !is_elf_hash_table (htab)
3430 || info->output_bfd->xvec != abfd->xvec)
3431 {
3432 if (info->relocatable)
3433 bfd_set_error (bfd_error_invalid_operation);
3434 else
3435 bfd_set_error (bfd_error_wrong_format);
3436 goto error_return;
3437 }
3438 }
3439
3440 /* As a GNU extension, any input sections which are named
3441 .gnu.warning.SYMBOL are treated as warning symbols for the given
3442 symbol. This differs from .gnu.warning sections, which generate
3443 warnings when they are included in an output file. */
3444 if (info->executable)
3445 {
3446 asection *s;
3447
3448 for (s = abfd->sections; s != NULL; s = s->next)
3449 {
3450 const char *name;
3451
3452 name = bfd_get_section_name (abfd, s);
3453 if (CONST_STRNEQ (name, ".gnu.warning."))
3454 {
3455 char *msg;
3456 bfd_size_type sz;
3457
3458 name += sizeof ".gnu.warning." - 1;
3459
3460 /* If this is a shared object, then look up the symbol
3461 in the hash table. If it is there, and it is already
3462 been defined, then we will not be using the entry
3463 from this shared object, so we don't need to warn.
3464 FIXME: If we see the definition in a regular object
3465 later on, we will warn, but we shouldn't. The only
3466 fix is to keep track of what warnings we are supposed
3467 to emit, and then handle them all at the end of the
3468 link. */
3469 if (dynamic)
3470 {
3471 struct elf_link_hash_entry *h;
3472
3473 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3474
3475 /* FIXME: What about bfd_link_hash_common? */
3476 if (h != NULL
3477 && (h->root.type == bfd_link_hash_defined
3478 || h->root.type == bfd_link_hash_defweak))
3479 {
3480 /* We don't want to issue this warning. Clobber
3481 the section size so that the warning does not
3482 get copied into the output file. */
3483 s->size = 0;
3484 continue;
3485 }
3486 }
3487
3488 sz = s->size;
3489 msg = bfd_alloc (abfd, sz + 1);
3490 if (msg == NULL)
3491 goto error_return;
3492
3493 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3494 goto error_return;
3495
3496 msg[sz] = '\0';
3497
3498 if (! (_bfd_generic_link_add_one_symbol
3499 (info, abfd, name, BSF_WARNING, s, 0, msg,
3500 FALSE, bed->collect, NULL)))
3501 goto error_return;
3502
3503 if (! info->relocatable)
3504 {
3505 /* Clobber the section size so that the warning does
3506 not get copied into the output file. */
3507 s->size = 0;
3508
3509 /* Also set SEC_EXCLUDE, so that symbols defined in
3510 the warning section don't get copied to the output. */
3511 s->flags |= SEC_EXCLUDE;
3512 }
3513 }
3514 }
3515 }
3516
3517 add_needed = TRUE;
3518 if (! dynamic)
3519 {
3520 /* If we are creating a shared library, create all the dynamic
3521 sections immediately. We need to attach them to something,
3522 so we attach them to this BFD, provided it is the right
3523 format. FIXME: If there are no input BFD's of the same
3524 format as the output, we can't make a shared library. */
3525 if (info->shared
3526 && is_elf_hash_table (htab)
3527 && info->output_bfd->xvec == abfd->xvec
3528 && !htab->dynamic_sections_created)
3529 {
3530 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3531 goto error_return;
3532 }
3533 }
3534 else if (!is_elf_hash_table (htab))
3535 goto error_return;
3536 else
3537 {
3538 asection *s;
3539 const char *soname = NULL;
3540 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3541 int ret;
3542
3543 /* ld --just-symbols and dynamic objects don't mix very well.
3544 ld shouldn't allow it. */
3545 if ((s = abfd->sections) != NULL
3546 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3547 abort ();
3548
3549 /* If this dynamic lib was specified on the command line with
3550 --as-needed in effect, then we don't want to add a DT_NEEDED
3551 tag unless the lib is actually used. Similary for libs brought
3552 in by another lib's DT_NEEDED. When --no-add-needed is used
3553 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3554 any dynamic library in DT_NEEDED tags in the dynamic lib at
3555 all. */
3556 add_needed = (elf_dyn_lib_class (abfd)
3557 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3558 | DYN_NO_NEEDED)) == 0;
3559
3560 s = bfd_get_section_by_name (abfd, ".dynamic");
3561 if (s != NULL)
3562 {
3563 bfd_byte *dynbuf;
3564 bfd_byte *extdyn;
3565 unsigned int elfsec;
3566 unsigned long shlink;
3567
3568 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3569 goto error_free_dyn;
3570
3571 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3572 if (elfsec == SHN_BAD)
3573 goto error_free_dyn;
3574 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3575
3576 for (extdyn = dynbuf;
3577 extdyn < dynbuf + s->size;
3578 extdyn += bed->s->sizeof_dyn)
3579 {
3580 Elf_Internal_Dyn dyn;
3581
3582 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3583 if (dyn.d_tag == DT_SONAME)
3584 {
3585 unsigned int tagv = dyn.d_un.d_val;
3586 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3587 if (soname == NULL)
3588 goto error_free_dyn;
3589 }
3590 if (dyn.d_tag == DT_NEEDED)
3591 {
3592 struct bfd_link_needed_list *n, **pn;
3593 char *fnm, *anm;
3594 unsigned int tagv = dyn.d_un.d_val;
3595
3596 amt = sizeof (struct bfd_link_needed_list);
3597 n = bfd_alloc (abfd, amt);
3598 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3599 if (n == NULL || fnm == NULL)
3600 goto error_free_dyn;
3601 amt = strlen (fnm) + 1;
3602 anm = bfd_alloc (abfd, amt);
3603 if (anm == NULL)
3604 goto error_free_dyn;
3605 memcpy (anm, fnm, amt);
3606 n->name = anm;
3607 n->by = abfd;
3608 n->next = NULL;
3609 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3610 ;
3611 *pn = n;
3612 }
3613 if (dyn.d_tag == DT_RUNPATH)
3614 {
3615 struct bfd_link_needed_list *n, **pn;
3616 char *fnm, *anm;
3617 unsigned int tagv = dyn.d_un.d_val;
3618
3619 amt = sizeof (struct bfd_link_needed_list);
3620 n = bfd_alloc (abfd, amt);
3621 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3622 if (n == NULL || fnm == NULL)
3623 goto error_free_dyn;
3624 amt = strlen (fnm) + 1;
3625 anm = bfd_alloc (abfd, amt);
3626 if (anm == NULL)
3627 goto error_free_dyn;
3628 memcpy (anm, fnm, amt);
3629 n->name = anm;
3630 n->by = abfd;
3631 n->next = NULL;
3632 for (pn = & runpath;
3633 *pn != NULL;
3634 pn = &(*pn)->next)
3635 ;
3636 *pn = n;
3637 }
3638 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3639 if (!runpath && dyn.d_tag == DT_RPATH)
3640 {
3641 struct bfd_link_needed_list *n, **pn;
3642 char *fnm, *anm;
3643 unsigned int tagv = dyn.d_un.d_val;
3644
3645 amt = sizeof (struct bfd_link_needed_list);
3646 n = bfd_alloc (abfd, amt);
3647 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3648 if (n == NULL || fnm == NULL)
3649 goto error_free_dyn;
3650 amt = strlen (fnm) + 1;
3651 anm = bfd_alloc (abfd, amt);
3652 if (anm == NULL)
3653 {
3654 error_free_dyn:
3655 free (dynbuf);
3656 goto error_return;
3657 }
3658 memcpy (anm, fnm, amt);
3659 n->name = anm;
3660 n->by = abfd;
3661 n->next = NULL;
3662 for (pn = & rpath;
3663 *pn != NULL;
3664 pn = &(*pn)->next)
3665 ;
3666 *pn = n;
3667 }
3668 }
3669
3670 free (dynbuf);
3671 }
3672
3673 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3674 frees all more recently bfd_alloc'd blocks as well. */
3675 if (runpath)
3676 rpath = runpath;
3677
3678 if (rpath)
3679 {
3680 struct bfd_link_needed_list **pn;
3681 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3682 ;
3683 *pn = rpath;
3684 }
3685
3686 /* We do not want to include any of the sections in a dynamic
3687 object in the output file. We hack by simply clobbering the
3688 list of sections in the BFD. This could be handled more
3689 cleanly by, say, a new section flag; the existing
3690 SEC_NEVER_LOAD flag is not the one we want, because that one
3691 still implies that the section takes up space in the output
3692 file. */
3693 bfd_section_list_clear (abfd);
3694
3695 /* Find the name to use in a DT_NEEDED entry that refers to this
3696 object. If the object has a DT_SONAME entry, we use it.
3697 Otherwise, if the generic linker stuck something in
3698 elf_dt_name, we use that. Otherwise, we just use the file
3699 name. */
3700 if (soname == NULL || *soname == '\0')
3701 {
3702 soname = elf_dt_name (abfd);
3703 if (soname == NULL || *soname == '\0')
3704 soname = bfd_get_filename (abfd);
3705 }
3706
3707 /* Save the SONAME because sometimes the linker emulation code
3708 will need to know it. */
3709 elf_dt_name (abfd) = soname;
3710
3711 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3712 if (ret < 0)
3713 goto error_return;
3714
3715 /* If we have already included this dynamic object in the
3716 link, just ignore it. There is no reason to include a
3717 particular dynamic object more than once. */
3718 if (ret > 0)
3719 return TRUE;
3720 }
3721
3722 /* If this is a dynamic object, we always link against the .dynsym
3723 symbol table, not the .symtab symbol table. The dynamic linker
3724 will only see the .dynsym symbol table, so there is no reason to
3725 look at .symtab for a dynamic object. */
3726
3727 if (! dynamic || elf_dynsymtab (abfd) == 0)
3728 hdr = &elf_tdata (abfd)->symtab_hdr;
3729 else
3730 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3731
3732 symcount = hdr->sh_size / bed->s->sizeof_sym;
3733
3734 /* The sh_info field of the symtab header tells us where the
3735 external symbols start. We don't care about the local symbols at
3736 this point. */
3737 if (elf_bad_symtab (abfd))
3738 {
3739 extsymcount = symcount;
3740 extsymoff = 0;
3741 }
3742 else
3743 {
3744 extsymcount = symcount - hdr->sh_info;
3745 extsymoff = hdr->sh_info;
3746 }
3747
3748 sym_hash = NULL;
3749 if (extsymcount != 0)
3750 {
3751 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3752 NULL, NULL, NULL);
3753 if (isymbuf == NULL)
3754 goto error_return;
3755
3756 /* We store a pointer to the hash table entry for each external
3757 symbol. */
3758 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3759 sym_hash = bfd_alloc (abfd, amt);
3760 if (sym_hash == NULL)
3761 goto error_free_sym;
3762 elf_sym_hashes (abfd) = sym_hash;
3763 }
3764
3765 if (dynamic)
3766 {
3767 /* Read in any version definitions. */
3768 if (!_bfd_elf_slurp_version_tables (abfd,
3769 info->default_imported_symver))
3770 goto error_free_sym;
3771
3772 /* Read in the symbol versions, but don't bother to convert them
3773 to internal format. */
3774 if (elf_dynversym (abfd) != 0)
3775 {
3776 Elf_Internal_Shdr *versymhdr;
3777
3778 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3779 extversym = bfd_malloc (versymhdr->sh_size);
3780 if (extversym == NULL)
3781 goto error_free_sym;
3782 amt = versymhdr->sh_size;
3783 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3784 || bfd_bread (extversym, amt, abfd) != amt)
3785 goto error_free_vers;
3786 }
3787 }
3788
3789 /* If we are loading an as-needed shared lib, save the symbol table
3790 state before we start adding symbols. If the lib turns out
3791 to be unneeded, restore the state. */
3792 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3793 {
3794 unsigned int i;
3795 size_t entsize;
3796
3797 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3798 {
3799 struct bfd_hash_entry *p;
3800 struct elf_link_hash_entry *h;
3801
3802 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3803 {
3804 h = (struct elf_link_hash_entry *) p;
3805 entsize += htab->root.table.entsize;
3806 if (h->root.type == bfd_link_hash_warning)
3807 entsize += htab->root.table.entsize;
3808 }
3809 }
3810
3811 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
3812 hashsize = extsymcount * sizeof (struct elf_link_hash_entry *);
3813 old_tab = bfd_malloc (tabsize + entsize + hashsize);
3814 if (old_tab == NULL)
3815 goto error_free_vers;
3816
3817 /* Remember the current objalloc pointer, so that all mem for
3818 symbols added can later be reclaimed. */
3819 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3820 if (alloc_mark == NULL)
3821 goto error_free_vers;
3822
3823 /* Make a special call to the linker "notice" function to
3824 tell it that we are about to handle an as-needed lib. */
3825 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
3826 notice_as_needed))
3827 goto error_free_vers;
3828
3829 /* Clone the symbol table and sym hashes. Remember some
3830 pointers into the symbol table, and dynamic symbol count. */
3831 old_hash = (char *) old_tab + tabsize;
3832 old_ent = (char *) old_hash + hashsize;
3833 memcpy (old_tab, htab->root.table.table, tabsize);
3834 memcpy (old_hash, sym_hash, hashsize);
3835 old_undefs = htab->root.undefs;
3836 old_undefs_tail = htab->root.undefs_tail;
3837 old_table = htab->root.table.table;
3838 old_size = htab->root.table.size;
3839 old_count = htab->root.table.count;
3840 old_dynsymcount = htab->dynsymcount;
3841
3842 for (i = 0; i < htab->root.table.size; i++)
3843 {
3844 struct bfd_hash_entry *p;
3845 struct elf_link_hash_entry *h;
3846
3847 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3848 {
3849 memcpy (old_ent, p, htab->root.table.entsize);
3850 old_ent = (char *) old_ent + htab->root.table.entsize;
3851 h = (struct elf_link_hash_entry *) p;
3852 if (h->root.type == bfd_link_hash_warning)
3853 {
3854 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3855 old_ent = (char *) old_ent + htab->root.table.entsize;
3856 }
3857 }
3858 }
3859 }
3860
3861 weaks = NULL;
3862 ever = extversym != NULL ? extversym + extsymoff : NULL;
3863 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3864 isym < isymend;
3865 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3866 {
3867 int bind;
3868 bfd_vma value;
3869 asection *sec, *new_sec;
3870 flagword flags;
3871 const char *name;
3872 struct elf_link_hash_entry *h;
3873 bfd_boolean definition;
3874 bfd_boolean size_change_ok;
3875 bfd_boolean type_change_ok;
3876 bfd_boolean new_weakdef;
3877 bfd_boolean override;
3878 bfd_boolean common;
3879 unsigned int old_alignment;
3880 bfd *old_bfd;
3881
3882 override = FALSE;
3883
3884 flags = BSF_NO_FLAGS;
3885 sec = NULL;
3886 value = isym->st_value;
3887 *sym_hash = NULL;
3888 common = bed->common_definition (isym);
3889
3890 bind = ELF_ST_BIND (isym->st_info);
3891 if (bind == STB_LOCAL)
3892 {
3893 /* This should be impossible, since ELF requires that all
3894 global symbols follow all local symbols, and that sh_info
3895 point to the first global symbol. Unfortunately, Irix 5
3896 screws this up. */
3897 continue;
3898 }
3899 else if (bind == STB_GLOBAL)
3900 {
3901 if (isym->st_shndx != SHN_UNDEF && !common)
3902 flags = BSF_GLOBAL;
3903 }
3904 else if (bind == STB_WEAK)
3905 flags = BSF_WEAK;
3906 else
3907 {
3908 /* Leave it up to the processor backend. */
3909 }
3910
3911 if (isym->st_shndx == SHN_UNDEF)
3912 sec = bfd_und_section_ptr;
3913 else if (isym->st_shndx == SHN_ABS)
3914 sec = bfd_abs_section_ptr;
3915 else if (isym->st_shndx == SHN_COMMON)
3916 {
3917 sec = bfd_com_section_ptr;
3918 /* What ELF calls the size we call the value. What ELF
3919 calls the value we call the alignment. */
3920 value = isym->st_size;
3921 }
3922 else
3923 {
3924 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3925 if (sec == NULL)
3926 sec = bfd_abs_section_ptr;
3927 else if (sec->kept_section)
3928 {
3929 /* Symbols from discarded section are undefined. We keep
3930 its visibility. */
3931 sec = bfd_und_section_ptr;
3932 isym->st_shndx = SHN_UNDEF;
3933 }
3934 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3935 value -= sec->vma;
3936 }
3937
3938 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3939 isym->st_name);
3940 if (name == NULL)
3941 goto error_free_vers;
3942
3943 if (isym->st_shndx == SHN_COMMON
3944 && ELF_ST_TYPE (isym->st_info) == STT_TLS
3945 && !info->relocatable)
3946 {
3947 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3948
3949 if (tcomm == NULL)
3950 {
3951 tcomm = bfd_make_section_with_flags (abfd, ".tcommon",
3952 (SEC_ALLOC
3953 | SEC_IS_COMMON
3954 | SEC_LINKER_CREATED
3955 | SEC_THREAD_LOCAL));
3956 if (tcomm == NULL)
3957 goto error_free_vers;
3958 }
3959 sec = tcomm;
3960 }
3961 else if (bed->elf_add_symbol_hook)
3962 {
3963 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
3964 &sec, &value))
3965 goto error_free_vers;
3966
3967 /* The hook function sets the name to NULL if this symbol
3968 should be skipped for some reason. */
3969 if (name == NULL)
3970 continue;
3971 }
3972
3973 /* Sanity check that all possibilities were handled. */
3974 if (sec == NULL)
3975 {
3976 bfd_set_error (bfd_error_bad_value);
3977 goto error_free_vers;
3978 }
3979
3980 if (bfd_is_und_section (sec)
3981 || bfd_is_com_section (sec))
3982 definition = FALSE;
3983 else
3984 definition = TRUE;
3985
3986 size_change_ok = FALSE;
3987 type_change_ok = bed->type_change_ok;
3988 old_alignment = 0;
3989 old_bfd = NULL;
3990 new_sec = sec;
3991
3992 if (is_elf_hash_table (htab))
3993 {
3994 Elf_Internal_Versym iver;
3995 unsigned int vernum = 0;
3996 bfd_boolean skip;
3997
3998 if (ever == NULL)
3999 {
4000 if (info->default_imported_symver)
4001 /* Use the default symbol version created earlier. */
4002 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4003 else
4004 iver.vs_vers = 0;
4005 }
4006 else
4007 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4008
4009 vernum = iver.vs_vers & VERSYM_VERSION;
4010
4011 /* If this is a hidden symbol, or if it is not version
4012 1, we append the version name to the symbol name.
4013 However, we do not modify a non-hidden absolute symbol
4014 if it is not a function, because it might be the version
4015 symbol itself. FIXME: What if it isn't? */
4016 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4017 || (vernum > 1
4018 && (!bfd_is_abs_section (sec)
4019 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4020 {
4021 const char *verstr;
4022 size_t namelen, verlen, newlen;
4023 char *newname, *p;
4024
4025 if (isym->st_shndx != SHN_UNDEF)
4026 {
4027 if (vernum > elf_tdata (abfd)->cverdefs)
4028 verstr = NULL;
4029 else if (vernum > 1)
4030 verstr =
4031 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4032 else
4033 verstr = "";
4034
4035 if (verstr == NULL)
4036 {
4037 (*_bfd_error_handler)
4038 (_("%B: %s: invalid version %u (max %d)"),
4039 abfd, name, vernum,
4040 elf_tdata (abfd)->cverdefs);
4041 bfd_set_error (bfd_error_bad_value);
4042 goto error_free_vers;
4043 }
4044 }
4045 else
4046 {
4047 /* We cannot simply test for the number of
4048 entries in the VERNEED section since the
4049 numbers for the needed versions do not start
4050 at 0. */
4051 Elf_Internal_Verneed *t;
4052
4053 verstr = NULL;
4054 for (t = elf_tdata (abfd)->verref;
4055 t != NULL;
4056 t = t->vn_nextref)
4057 {
4058 Elf_Internal_Vernaux *a;
4059
4060 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4061 {
4062 if (a->vna_other == vernum)
4063 {
4064 verstr = a->vna_nodename;
4065 break;
4066 }
4067 }
4068 if (a != NULL)
4069 break;
4070 }
4071 if (verstr == NULL)
4072 {
4073 (*_bfd_error_handler)
4074 (_("%B: %s: invalid needed version %d"),
4075 abfd, name, vernum);
4076 bfd_set_error (bfd_error_bad_value);
4077 goto error_free_vers;
4078 }
4079 }
4080
4081 namelen = strlen (name);
4082 verlen = strlen (verstr);
4083 newlen = namelen + verlen + 2;
4084 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4085 && isym->st_shndx != SHN_UNDEF)
4086 ++newlen;
4087
4088 newname = bfd_hash_allocate (&htab->root.table, newlen);
4089 if (newname == NULL)
4090 goto error_free_vers;
4091 memcpy (newname, name, namelen);
4092 p = newname + namelen;
4093 *p++ = ELF_VER_CHR;
4094 /* If this is a defined non-hidden version symbol,
4095 we add another @ to the name. This indicates the
4096 default version of the symbol. */
4097 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4098 && isym->st_shndx != SHN_UNDEF)
4099 *p++ = ELF_VER_CHR;
4100 memcpy (p, verstr, verlen + 1);
4101
4102 name = newname;
4103 }
4104
4105 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec,
4106 &value, &old_alignment,
4107 sym_hash, &skip, &override,
4108 &type_change_ok, &size_change_ok))
4109 goto error_free_vers;
4110
4111 if (skip)
4112 continue;
4113
4114 if (override)
4115 definition = FALSE;
4116
4117 h = *sym_hash;
4118 while (h->root.type == bfd_link_hash_indirect
4119 || h->root.type == bfd_link_hash_warning)
4120 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4121
4122 /* Remember the old alignment if this is a common symbol, so
4123 that we don't reduce the alignment later on. We can't
4124 check later, because _bfd_generic_link_add_one_symbol
4125 will set a default for the alignment which we want to
4126 override. We also remember the old bfd where the existing
4127 definition comes from. */
4128 switch (h->root.type)
4129 {
4130 default:
4131 break;
4132
4133 case bfd_link_hash_defined:
4134 case bfd_link_hash_defweak:
4135 old_bfd = h->root.u.def.section->owner;
4136 break;
4137
4138 case bfd_link_hash_common:
4139 old_bfd = h->root.u.c.p->section->owner;
4140 old_alignment = h->root.u.c.p->alignment_power;
4141 break;
4142 }
4143
4144 if (elf_tdata (abfd)->verdef != NULL
4145 && ! override
4146 && vernum > 1
4147 && definition)
4148 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4149 }
4150
4151 if (! (_bfd_generic_link_add_one_symbol
4152 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4153 (struct bfd_link_hash_entry **) sym_hash)))
4154 goto error_free_vers;
4155
4156 h = *sym_hash;
4157 while (h->root.type == bfd_link_hash_indirect
4158 || h->root.type == bfd_link_hash_warning)
4159 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4160 *sym_hash = h;
4161
4162 new_weakdef = FALSE;
4163 if (dynamic
4164 && definition
4165 && (flags & BSF_WEAK) != 0
4166 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4167 && is_elf_hash_table (htab)
4168 && h->u.weakdef == NULL)
4169 {
4170 /* Keep a list of all weak defined non function symbols from
4171 a dynamic object, using the weakdef field. Later in this
4172 function we will set the weakdef field to the correct
4173 value. We only put non-function symbols from dynamic
4174 objects on this list, because that happens to be the only
4175 time we need to know the normal symbol corresponding to a
4176 weak symbol, and the information is time consuming to
4177 figure out. If the weakdef field is not already NULL,
4178 then this symbol was already defined by some previous
4179 dynamic object, and we will be using that previous
4180 definition anyhow. */
4181
4182 h->u.weakdef = weaks;
4183 weaks = h;
4184 new_weakdef = TRUE;
4185 }
4186
4187 /* Set the alignment of a common symbol. */
4188 if ((common || bfd_is_com_section (sec))
4189 && h->root.type == bfd_link_hash_common)
4190 {
4191 unsigned int align;
4192
4193 if (common)
4194 align = bfd_log2 (isym->st_value);
4195 else
4196 {
4197 /* The new symbol is a common symbol in a shared object.
4198 We need to get the alignment from the section. */
4199 align = new_sec->alignment_power;
4200 }
4201 if (align > old_alignment
4202 /* Permit an alignment power of zero if an alignment of one
4203 is specified and no other alignments have been specified. */
4204 || (isym->st_value == 1 && old_alignment == 0))
4205 h->root.u.c.p->alignment_power = align;
4206 else
4207 h->root.u.c.p->alignment_power = old_alignment;
4208 }
4209
4210 if (is_elf_hash_table (htab))
4211 {
4212 bfd_boolean dynsym;
4213
4214 /* Check the alignment when a common symbol is involved. This
4215 can change when a common symbol is overridden by a normal
4216 definition or a common symbol is ignored due to the old
4217 normal definition. We need to make sure the maximum
4218 alignment is maintained. */
4219 if ((old_alignment || common)
4220 && h->root.type != bfd_link_hash_common)
4221 {
4222 unsigned int common_align;
4223 unsigned int normal_align;
4224 unsigned int symbol_align;
4225 bfd *normal_bfd;
4226 bfd *common_bfd;
4227
4228 symbol_align = ffs (h->root.u.def.value) - 1;
4229 if (h->root.u.def.section->owner != NULL
4230 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4231 {
4232 normal_align = h->root.u.def.section->alignment_power;
4233 if (normal_align > symbol_align)
4234 normal_align = symbol_align;
4235 }
4236 else
4237 normal_align = symbol_align;
4238
4239 if (old_alignment)
4240 {
4241 common_align = old_alignment;
4242 common_bfd = old_bfd;
4243 normal_bfd = abfd;
4244 }
4245 else
4246 {
4247 common_align = bfd_log2 (isym->st_value);
4248 common_bfd = abfd;
4249 normal_bfd = old_bfd;
4250 }
4251
4252 if (normal_align < common_align)
4253 {
4254 /* PR binutils/2735 */
4255 if (normal_bfd == NULL)
4256 (*_bfd_error_handler)
4257 (_("Warning: alignment %u of common symbol `%s' in %B"
4258 " is greater than the alignment (%u) of its section %A"),
4259 common_bfd, h->root.u.def.section,
4260 1 << common_align, name, 1 << normal_align);
4261 else
4262 (*_bfd_error_handler)
4263 (_("Warning: alignment %u of symbol `%s' in %B"
4264 " is smaller than %u in %B"),
4265 normal_bfd, common_bfd,
4266 1 << normal_align, name, 1 << common_align);
4267 }
4268 }
4269
4270 /* Remember the symbol size if it isn't undefined. */
4271 if ((isym->st_size != 0 && isym->st_shndx != SHN_UNDEF)
4272 && (definition || h->size == 0))
4273 {
4274 if (h->size != 0
4275 && h->size != isym->st_size
4276 && ! size_change_ok)
4277 (*_bfd_error_handler)
4278 (_("Warning: size of symbol `%s' changed"
4279 " from %lu in %B to %lu in %B"),
4280 old_bfd, abfd,
4281 name, (unsigned long) h->size,
4282 (unsigned long) isym->st_size);
4283
4284 h->size = isym->st_size;
4285 }
4286
4287 /* If this is a common symbol, then we always want H->SIZE
4288 to be the size of the common symbol. The code just above
4289 won't fix the size if a common symbol becomes larger. We
4290 don't warn about a size change here, because that is
4291 covered by --warn-common. Allow changed between different
4292 function types. */
4293 if (h->root.type == bfd_link_hash_common)
4294 h->size = h->root.u.c.size;
4295
4296 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4297 && (definition || h->type == STT_NOTYPE))
4298 {
4299 if (h->type != STT_NOTYPE
4300 && h->type != ELF_ST_TYPE (isym->st_info)
4301 && ! type_change_ok)
4302 (*_bfd_error_handler)
4303 (_("Warning: type of symbol `%s' changed"
4304 " from %d to %d in %B"),
4305 abfd, name, h->type, ELF_ST_TYPE (isym->st_info));
4306
4307 h->type = ELF_ST_TYPE (isym->st_info);
4308 }
4309
4310 /* Merge st_other field. */
4311 elf_merge_st_other (abfd, h, isym, definition, dynamic);
4312
4313 /* Set a flag in the hash table entry indicating the type of
4314 reference or definition we just found. Keep a count of
4315 the number of dynamic symbols we find. A dynamic symbol
4316 is one which is referenced or defined by both a regular
4317 object and a shared object. */
4318 dynsym = FALSE;
4319 if (! dynamic)
4320 {
4321 if (! definition)
4322 {
4323 h->ref_regular = 1;
4324 if (bind != STB_WEAK)
4325 h->ref_regular_nonweak = 1;
4326 }
4327 else
4328 {
4329 h->def_regular = 1;
4330 if (h->def_dynamic)
4331 {
4332 h->def_dynamic = 0;
4333 h->ref_dynamic = 1;
4334 h->dynamic_def = 1;
4335 }
4336 }
4337 if (! info->executable
4338 || h->def_dynamic
4339 || h->ref_dynamic)
4340 dynsym = TRUE;
4341 }
4342 else
4343 {
4344 if (! definition)
4345 h->ref_dynamic = 1;
4346 else
4347 h->def_dynamic = 1;
4348 if (h->def_regular
4349 || h->ref_regular
4350 || (h->u.weakdef != NULL
4351 && ! new_weakdef
4352 && h->u.weakdef->dynindx != -1))
4353 dynsym = TRUE;
4354 }
4355
4356 if (definition && (sec->flags & SEC_DEBUGGING) && !info->relocatable)
4357 {
4358 /* We don't want to make debug symbol dynamic. */
4359 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4360 dynsym = FALSE;
4361 }
4362
4363 /* Check to see if we need to add an indirect symbol for
4364 the default name. */
4365 if (definition || h->root.type == bfd_link_hash_common)
4366 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4367 &sec, &value, &dynsym,
4368 override))
4369 goto error_free_vers;
4370
4371 if (definition && !dynamic)
4372 {
4373 char *p = strchr (name, ELF_VER_CHR);
4374 if (p != NULL && p[1] != ELF_VER_CHR)
4375 {
4376 /* Queue non-default versions so that .symver x, x@FOO
4377 aliases can be checked. */
4378 if (!nondeflt_vers)
4379 {
4380 amt = ((isymend - isym + 1)
4381 * sizeof (struct elf_link_hash_entry *));
4382 nondeflt_vers = bfd_malloc (amt);
4383 if (!nondeflt_vers)
4384 goto error_free_vers;
4385 }
4386 nondeflt_vers[nondeflt_vers_cnt++] = h;
4387 }
4388 }
4389
4390 if (dynsym && h->dynindx == -1)
4391 {
4392 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4393 goto error_free_vers;
4394 if (h->u.weakdef != NULL
4395 && ! new_weakdef
4396 && h->u.weakdef->dynindx == -1)
4397 {
4398 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4399 goto error_free_vers;
4400 }
4401 }
4402 else if (dynsym && h->dynindx != -1)
4403 /* If the symbol already has a dynamic index, but
4404 visibility says it should not be visible, turn it into
4405 a local symbol. */
4406 switch (ELF_ST_VISIBILITY (h->other))
4407 {
4408 case STV_INTERNAL:
4409 case STV_HIDDEN:
4410 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4411 dynsym = FALSE;
4412 break;
4413 }
4414
4415 if (!add_needed
4416 && definition
4417 && dynsym
4418 && h->ref_regular)
4419 {
4420 int ret;
4421 const char *soname = elf_dt_name (abfd);
4422
4423 /* A symbol from a library loaded via DT_NEEDED of some
4424 other library is referenced by a regular object.
4425 Add a DT_NEEDED entry for it. Issue an error if
4426 --no-add-needed is used. */
4427 if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4428 {
4429 (*_bfd_error_handler)
4430 (_("%s: invalid DSO for symbol `%s' definition"),
4431 abfd, name);
4432 bfd_set_error (bfd_error_bad_value);
4433 goto error_free_vers;
4434 }
4435
4436 elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED;
4437
4438 add_needed = TRUE;
4439 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4440 if (ret < 0)
4441 goto error_free_vers;
4442
4443 BFD_ASSERT (ret == 0);
4444 }
4445 }
4446 }
4447
4448 if (extversym != NULL)
4449 {
4450 free (extversym);
4451 extversym = NULL;
4452 }
4453
4454 if (isymbuf != NULL)
4455 {
4456 free (isymbuf);
4457 isymbuf = NULL;
4458 }
4459
4460 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4461 {
4462 unsigned int i;
4463
4464 /* Restore the symbol table. */
4465 if (bed->as_needed_cleanup)
4466 (*bed->as_needed_cleanup) (abfd, info);
4467 old_hash = (char *) old_tab + tabsize;
4468 old_ent = (char *) old_hash + hashsize;
4469 sym_hash = elf_sym_hashes (abfd);
4470 htab->root.table.table = old_table;
4471 htab->root.table.size = old_size;
4472 htab->root.table.count = old_count;
4473 memcpy (htab->root.table.table, old_tab, tabsize);
4474 memcpy (sym_hash, old_hash, hashsize);
4475 htab->root.undefs = old_undefs;
4476 htab->root.undefs_tail = old_undefs_tail;
4477 for (i = 0; i < htab->root.table.size; i++)
4478 {
4479 struct bfd_hash_entry *p;
4480 struct elf_link_hash_entry *h;
4481
4482 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4483 {
4484 h = (struct elf_link_hash_entry *) p;
4485 if (h->root.type == bfd_link_hash_warning)
4486 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4487 if (h->dynindx >= old_dynsymcount)
4488 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
4489
4490 memcpy (p, old_ent, htab->root.table.entsize);
4491 old_ent = (char *) old_ent + htab->root.table.entsize;
4492 h = (struct elf_link_hash_entry *) p;
4493 if (h->root.type == bfd_link_hash_warning)
4494 {
4495 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4496 old_ent = (char *) old_ent + htab->root.table.entsize;
4497 }
4498 }
4499 }
4500
4501 /* Make a special call to the linker "notice" function to
4502 tell it that symbols added for crefs may need to be removed. */
4503 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
4504 notice_not_needed))
4505 goto error_free_vers;
4506
4507 free (old_tab);
4508 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4509 alloc_mark);
4510 if (nondeflt_vers != NULL)
4511 free (nondeflt_vers);
4512 return TRUE;
4513 }
4514
4515 if (old_tab != NULL)
4516 {
4517 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
4518 notice_needed))
4519 goto error_free_vers;
4520 free (old_tab);
4521 old_tab = NULL;
4522 }
4523
4524 /* Now that all the symbols from this input file are created, handle
4525 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4526 if (nondeflt_vers != NULL)
4527 {
4528 bfd_size_type cnt, symidx;
4529
4530 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4531 {
4532 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4533 char *shortname, *p;
4534
4535 p = strchr (h->root.root.string, ELF_VER_CHR);
4536 if (p == NULL
4537 || (h->root.type != bfd_link_hash_defined
4538 && h->root.type != bfd_link_hash_defweak))
4539 continue;
4540
4541 amt = p - h->root.root.string;
4542 shortname = bfd_malloc (amt + 1);
4543 if (!shortname)
4544 goto error_free_vers;
4545 memcpy (shortname, h->root.root.string, amt);
4546 shortname[amt] = '\0';
4547
4548 hi = (struct elf_link_hash_entry *)
4549 bfd_link_hash_lookup (&htab->root, shortname,
4550 FALSE, FALSE, FALSE);
4551 if (hi != NULL
4552 && hi->root.type == h->root.type
4553 && hi->root.u.def.value == h->root.u.def.value
4554 && hi->root.u.def.section == h->root.u.def.section)
4555 {
4556 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4557 hi->root.type = bfd_link_hash_indirect;
4558 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4559 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4560 sym_hash = elf_sym_hashes (abfd);
4561 if (sym_hash)
4562 for (symidx = 0; symidx < extsymcount; ++symidx)
4563 if (sym_hash[symidx] == hi)
4564 {
4565 sym_hash[symidx] = h;
4566 break;
4567 }
4568 }
4569 free (shortname);
4570 }
4571 free (nondeflt_vers);
4572 nondeflt_vers = NULL;
4573 }
4574
4575 /* Now set the weakdefs field correctly for all the weak defined
4576 symbols we found. The only way to do this is to search all the
4577 symbols. Since we only need the information for non functions in
4578 dynamic objects, that's the only time we actually put anything on
4579 the list WEAKS. We need this information so that if a regular
4580 object refers to a symbol defined weakly in a dynamic object, the
4581 real symbol in the dynamic object is also put in the dynamic
4582 symbols; we also must arrange for both symbols to point to the
4583 same memory location. We could handle the general case of symbol
4584 aliasing, but a general symbol alias can only be generated in
4585 assembler code, handling it correctly would be very time
4586 consuming, and other ELF linkers don't handle general aliasing
4587 either. */
4588 if (weaks != NULL)
4589 {
4590 struct elf_link_hash_entry **hpp;
4591 struct elf_link_hash_entry **hppend;
4592 struct elf_link_hash_entry **sorted_sym_hash;
4593 struct elf_link_hash_entry *h;
4594 size_t sym_count;
4595
4596 /* Since we have to search the whole symbol list for each weak
4597 defined symbol, search time for N weak defined symbols will be
4598 O(N^2). Binary search will cut it down to O(NlogN). */
4599 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4600 sorted_sym_hash = bfd_malloc (amt);
4601 if (sorted_sym_hash == NULL)
4602 goto error_return;
4603 sym_hash = sorted_sym_hash;
4604 hpp = elf_sym_hashes (abfd);
4605 hppend = hpp + extsymcount;
4606 sym_count = 0;
4607 for (; hpp < hppend; hpp++)
4608 {
4609 h = *hpp;
4610 if (h != NULL
4611 && h->root.type == bfd_link_hash_defined
4612 && !bed->is_function_type (h->type))
4613 {
4614 *sym_hash = h;
4615 sym_hash++;
4616 sym_count++;
4617 }
4618 }
4619
4620 qsort (sorted_sym_hash, sym_count,
4621 sizeof (struct elf_link_hash_entry *),
4622 elf_sort_symbol);
4623
4624 while (weaks != NULL)
4625 {
4626 struct elf_link_hash_entry *hlook;
4627 asection *slook;
4628 bfd_vma vlook;
4629 long ilook;
4630 size_t i, j, idx;
4631
4632 hlook = weaks;
4633 weaks = hlook->u.weakdef;
4634 hlook->u.weakdef = NULL;
4635
4636 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4637 || hlook->root.type == bfd_link_hash_defweak
4638 || hlook->root.type == bfd_link_hash_common
4639 || hlook->root.type == bfd_link_hash_indirect);
4640 slook = hlook->root.u.def.section;
4641 vlook = hlook->root.u.def.value;
4642
4643 ilook = -1;
4644 i = 0;
4645 j = sym_count;
4646 while (i < j)
4647 {
4648 bfd_signed_vma vdiff;
4649 idx = (i + j) / 2;
4650 h = sorted_sym_hash [idx];
4651 vdiff = vlook - h->root.u.def.value;
4652 if (vdiff < 0)
4653 j = idx;
4654 else if (vdiff > 0)
4655 i = idx + 1;
4656 else
4657 {
4658 long sdiff = slook->id - h->root.u.def.section->id;
4659 if (sdiff < 0)
4660 j = idx;
4661 else if (sdiff > 0)
4662 i = idx + 1;
4663 else
4664 {
4665 ilook = idx;
4666 break;
4667 }
4668 }
4669 }
4670
4671 /* We didn't find a value/section match. */
4672 if (ilook == -1)
4673 continue;
4674
4675 for (i = ilook; i < sym_count; i++)
4676 {
4677 h = sorted_sym_hash [i];
4678
4679 /* Stop if value or section doesn't match. */
4680 if (h->root.u.def.value != vlook
4681 || h->root.u.def.section != slook)
4682 break;
4683 else if (h != hlook)
4684 {
4685 hlook->u.weakdef = h;
4686
4687 /* If the weak definition is in the list of dynamic
4688 symbols, make sure the real definition is put
4689 there as well. */
4690 if (hlook->dynindx != -1 && h->dynindx == -1)
4691 {
4692 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4693 {
4694 err_free_sym_hash:
4695 free (sorted_sym_hash);
4696 goto error_return;
4697 }
4698 }
4699
4700 /* If the real definition is in the list of dynamic
4701 symbols, make sure the weak definition is put
4702 there as well. If we don't do this, then the
4703 dynamic loader might not merge the entries for the
4704 real definition and the weak definition. */
4705 if (h->dynindx != -1 && hlook->dynindx == -1)
4706 {
4707 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4708 goto err_free_sym_hash;
4709 }
4710 break;
4711 }
4712 }
4713 }
4714
4715 free (sorted_sym_hash);
4716 }
4717
4718 if (bed->check_directives
4719 && !(*bed->check_directives) (abfd, info))
4720 return FALSE;
4721
4722 /* If this object is the same format as the output object, and it is
4723 not a shared library, then let the backend look through the
4724 relocs.
4725
4726 This is required to build global offset table entries and to
4727 arrange for dynamic relocs. It is not required for the
4728 particular common case of linking non PIC code, even when linking
4729 against shared libraries, but unfortunately there is no way of
4730 knowing whether an object file has been compiled PIC or not.
4731 Looking through the relocs is not particularly time consuming.
4732 The problem is that we must either (1) keep the relocs in memory,
4733 which causes the linker to require additional runtime memory or
4734 (2) read the relocs twice from the input file, which wastes time.
4735 This would be a good case for using mmap.
4736
4737 I have no idea how to handle linking PIC code into a file of a
4738 different format. It probably can't be done. */
4739 if (! dynamic
4740 && is_elf_hash_table (htab)
4741 && bed->check_relocs != NULL
4742 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4743 {
4744 asection *o;
4745
4746 for (o = abfd->sections; o != NULL; o = o->next)
4747 {
4748 Elf_Internal_Rela *internal_relocs;
4749 bfd_boolean ok;
4750
4751 if ((o->flags & SEC_RELOC) == 0
4752 || o->reloc_count == 0
4753 || ((info->strip == strip_all || info->strip == strip_debugger)
4754 && (o->flags & SEC_DEBUGGING) != 0)
4755 || bfd_is_abs_section (o->output_section))
4756 continue;
4757
4758 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4759 info->keep_memory);
4760 if (internal_relocs == NULL)
4761 goto error_return;
4762
4763 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4764
4765 if (elf_section_data (o)->relocs != internal_relocs)
4766 free (internal_relocs);
4767
4768 if (! ok)
4769 goto error_return;
4770 }
4771 }
4772
4773 /* If this is a non-traditional link, try to optimize the handling
4774 of the .stab/.stabstr sections. */
4775 if (! dynamic
4776 && ! info->traditional_format
4777 && is_elf_hash_table (htab)
4778 && (info->strip != strip_all && info->strip != strip_debugger))
4779 {
4780 asection *stabstr;
4781
4782 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4783 if (stabstr != NULL)
4784 {
4785 bfd_size_type string_offset = 0;
4786 asection *stab;
4787
4788 for (stab = abfd->sections; stab; stab = stab->next)
4789 if (CONST_STRNEQ (stab->name, ".stab")
4790 && (!stab->name[5] ||
4791 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4792 && (stab->flags & SEC_MERGE) == 0
4793 && !bfd_is_abs_section (stab->output_section))
4794 {
4795 struct bfd_elf_section_data *secdata;
4796
4797 secdata = elf_section_data (stab);
4798 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
4799 stabstr, &secdata->sec_info,
4800 &string_offset))
4801 goto error_return;
4802 if (secdata->sec_info)
4803 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4804 }
4805 }
4806 }
4807
4808 if (is_elf_hash_table (htab) && add_needed)
4809 {
4810 /* Add this bfd to the loaded list. */
4811 struct elf_link_loaded_list *n;
4812
4813 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4814 if (n == NULL)
4815 goto error_return;
4816 n->abfd = abfd;
4817 n->next = htab->loaded;
4818 htab->loaded = n;
4819 }
4820
4821 return TRUE;
4822
4823 error_free_vers:
4824 if (old_tab != NULL)
4825 free (old_tab);
4826 if (nondeflt_vers != NULL)
4827 free (nondeflt_vers);
4828 if (extversym != NULL)
4829 free (extversym);
4830 error_free_sym:
4831 if (isymbuf != NULL)
4832 free (isymbuf);
4833 error_return:
4834 return FALSE;
4835 }
4836
4837 /* Return the linker hash table entry of a symbol that might be
4838 satisfied by an archive symbol. Return -1 on error. */
4839
4840 struct elf_link_hash_entry *
4841 _bfd_elf_archive_symbol_lookup (bfd *abfd,
4842 struct bfd_link_info *info,
4843 const char *name)
4844 {
4845 struct elf_link_hash_entry *h;
4846 char *p, *copy;
4847 size_t len, first;
4848
4849 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4850 if (h != NULL)
4851 return h;
4852
4853 /* If this is a default version (the name contains @@), look up the
4854 symbol again with only one `@' as well as without the version.
4855 The effect is that references to the symbol with and without the
4856 version will be matched by the default symbol in the archive. */
4857
4858 p = strchr (name, ELF_VER_CHR);
4859 if (p == NULL || p[1] != ELF_VER_CHR)
4860 return h;
4861
4862 /* First check with only one `@'. */
4863 len = strlen (name);
4864 copy = bfd_alloc (abfd, len);
4865 if (copy == NULL)
4866 return (struct elf_link_hash_entry *) 0 - 1;
4867
4868 first = p - name + 1;
4869 memcpy (copy, name, first);
4870 memcpy (copy + first, name + first + 1, len - first);
4871
4872 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
4873 if (h == NULL)
4874 {
4875 /* We also need to check references to the symbol without the
4876 version. */
4877 copy[first - 1] = '\0';
4878 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4879 FALSE, FALSE, FALSE);
4880 }
4881
4882 bfd_release (abfd, copy);
4883 return h;
4884 }
4885
4886 /* Add symbols from an ELF archive file to the linker hash table. We
4887 don't use _bfd_generic_link_add_archive_symbols because of a
4888 problem which arises on UnixWare. The UnixWare libc.so is an
4889 archive which includes an entry libc.so.1 which defines a bunch of
4890 symbols. The libc.so archive also includes a number of other
4891 object files, which also define symbols, some of which are the same
4892 as those defined in libc.so.1. Correct linking requires that we
4893 consider each object file in turn, and include it if it defines any
4894 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4895 this; it looks through the list of undefined symbols, and includes
4896 any object file which defines them. When this algorithm is used on
4897 UnixWare, it winds up pulling in libc.so.1 early and defining a
4898 bunch of symbols. This means that some of the other objects in the
4899 archive are not included in the link, which is incorrect since they
4900 precede libc.so.1 in the archive.
4901
4902 Fortunately, ELF archive handling is simpler than that done by
4903 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4904 oddities. In ELF, if we find a symbol in the archive map, and the
4905 symbol is currently undefined, we know that we must pull in that
4906 object file.
4907
4908 Unfortunately, we do have to make multiple passes over the symbol
4909 table until nothing further is resolved. */
4910
4911 static bfd_boolean
4912 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
4913 {
4914 symindex c;
4915 bfd_boolean *defined = NULL;
4916 bfd_boolean *included = NULL;
4917 carsym *symdefs;
4918 bfd_boolean loop;
4919 bfd_size_type amt;
4920 const struct elf_backend_data *bed;
4921 struct elf_link_hash_entry * (*archive_symbol_lookup)
4922 (bfd *, struct bfd_link_info *, const char *);
4923
4924 if (! bfd_has_map (abfd))
4925 {
4926 /* An empty archive is a special case. */
4927 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4928 return TRUE;
4929 bfd_set_error (bfd_error_no_armap);
4930 return FALSE;
4931 }
4932
4933 /* Keep track of all symbols we know to be already defined, and all
4934 files we know to be already included. This is to speed up the
4935 second and subsequent passes. */
4936 c = bfd_ardata (abfd)->symdef_count;
4937 if (c == 0)
4938 return TRUE;
4939 amt = c;
4940 amt *= sizeof (bfd_boolean);
4941 defined = bfd_zmalloc (amt);
4942 included = bfd_zmalloc (amt);
4943 if (defined == NULL || included == NULL)
4944 goto error_return;
4945
4946 symdefs = bfd_ardata (abfd)->symdefs;
4947 bed = get_elf_backend_data (abfd);
4948 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
4949
4950 do
4951 {
4952 file_ptr last;
4953 symindex i;
4954 carsym *symdef;
4955 carsym *symdefend;
4956
4957 loop = FALSE;
4958 last = -1;
4959
4960 symdef = symdefs;
4961 symdefend = symdef + c;
4962 for (i = 0; symdef < symdefend; symdef++, i++)
4963 {
4964 struct elf_link_hash_entry *h;
4965 bfd *element;
4966 struct bfd_link_hash_entry *undefs_tail;
4967 symindex mark;
4968
4969 if (defined[i] || included[i])
4970 continue;
4971 if (symdef->file_offset == last)
4972 {
4973 included[i] = TRUE;
4974 continue;
4975 }
4976
4977 h = archive_symbol_lookup (abfd, info, symdef->name);
4978 if (h == (struct elf_link_hash_entry *) 0 - 1)
4979 goto error_return;
4980
4981 if (h == NULL)
4982 continue;
4983
4984 if (h->root.type == bfd_link_hash_common)
4985 {
4986 /* We currently have a common symbol. The archive map contains
4987 a reference to this symbol, so we may want to include it. We
4988 only want to include it however, if this archive element
4989 contains a definition of the symbol, not just another common
4990 declaration of it.
4991
4992 Unfortunately some archivers (including GNU ar) will put
4993 declarations of common symbols into their archive maps, as
4994 well as real definitions, so we cannot just go by the archive
4995 map alone. Instead we must read in the element's symbol
4996 table and check that to see what kind of symbol definition
4997 this is. */
4998 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
4999 continue;
5000 }
5001 else if (h->root.type != bfd_link_hash_undefined)
5002 {
5003 if (h->root.type != bfd_link_hash_undefweak)
5004 defined[i] = TRUE;
5005 continue;
5006 }
5007
5008 /* We need to include this archive member. */
5009 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5010 if (element == NULL)
5011 goto error_return;
5012
5013 if (! bfd_check_format (element, bfd_object))
5014 goto error_return;
5015
5016 /* Doublecheck that we have not included this object
5017 already--it should be impossible, but there may be
5018 something wrong with the archive. */
5019 if (element->archive_pass != 0)
5020 {
5021 bfd_set_error (bfd_error_bad_value);
5022 goto error_return;
5023 }
5024 element->archive_pass = 1;
5025
5026 undefs_tail = info->hash->undefs_tail;
5027
5028 if (! (*info->callbacks->add_archive_element) (info, element,
5029 symdef->name))
5030 goto error_return;
5031 if (! bfd_link_add_symbols (element, info))
5032 goto error_return;
5033
5034 /* If there are any new undefined symbols, we need to make
5035 another pass through the archive in order to see whether
5036 they can be defined. FIXME: This isn't perfect, because
5037 common symbols wind up on undefs_tail and because an
5038 undefined symbol which is defined later on in this pass
5039 does not require another pass. This isn't a bug, but it
5040 does make the code less efficient than it could be. */
5041 if (undefs_tail != info->hash->undefs_tail)
5042 loop = TRUE;
5043
5044 /* Look backward to mark all symbols from this object file
5045 which we have already seen in this pass. */
5046 mark = i;
5047 do
5048 {
5049 included[mark] = TRUE;
5050 if (mark == 0)
5051 break;
5052 --mark;
5053 }
5054 while (symdefs[mark].file_offset == symdef->file_offset);
5055
5056 /* We mark subsequent symbols from this object file as we go
5057 on through the loop. */
5058 last = symdef->file_offset;
5059 }
5060 }
5061 while (loop);
5062
5063 free (defined);
5064 free (included);
5065
5066 return TRUE;
5067
5068 error_return:
5069 if (defined != NULL)
5070 free (defined);
5071 if (included != NULL)
5072 free (included);
5073 return FALSE;
5074 }
5075
5076 /* Given an ELF BFD, add symbols to the global hash table as
5077 appropriate. */
5078
5079 bfd_boolean
5080 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5081 {
5082 switch (bfd_get_format (abfd))
5083 {
5084 case bfd_object:
5085 return elf_link_add_object_symbols (abfd, info);
5086 case bfd_archive:
5087 return elf_link_add_archive_symbols (abfd, info);
5088 default:
5089 bfd_set_error (bfd_error_wrong_format);
5090 return FALSE;
5091 }
5092 }
5093 \f
5094 struct hash_codes_info
5095 {
5096 unsigned long *hashcodes;
5097 bfd_boolean error;
5098 };
5099
5100 /* This function will be called though elf_link_hash_traverse to store
5101 all hash value of the exported symbols in an array. */
5102
5103 static bfd_boolean
5104 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5105 {
5106 struct hash_codes_info *inf = data;
5107 const char *name;
5108 char *p;
5109 unsigned long ha;
5110 char *alc = NULL;
5111
5112 if (h->root.type == bfd_link_hash_warning)
5113 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5114
5115 /* Ignore indirect symbols. These are added by the versioning code. */
5116 if (h->dynindx == -1)
5117 return TRUE;
5118
5119 name = h->root.root.string;
5120 p = strchr (name, ELF_VER_CHR);
5121 if (p != NULL)
5122 {
5123 alc = bfd_malloc (p - name + 1);
5124 if (alc == NULL)
5125 {
5126 inf->error = TRUE;
5127 return FALSE;
5128 }
5129 memcpy (alc, name, p - name);
5130 alc[p - name] = '\0';
5131 name = alc;
5132 }
5133
5134 /* Compute the hash value. */
5135 ha = bfd_elf_hash (name);
5136
5137 /* Store the found hash value in the array given as the argument. */
5138 *(inf->hashcodes)++ = ha;
5139
5140 /* And store it in the struct so that we can put it in the hash table
5141 later. */
5142 h->u.elf_hash_value = ha;
5143
5144 if (alc != NULL)
5145 free (alc);
5146
5147 return TRUE;
5148 }
5149
5150 struct collect_gnu_hash_codes
5151 {
5152 bfd *output_bfd;
5153 const struct elf_backend_data *bed;
5154 unsigned long int nsyms;
5155 unsigned long int maskbits;
5156 unsigned long int *hashcodes;
5157 unsigned long int *hashval;
5158 unsigned long int *indx;
5159 unsigned long int *counts;
5160 bfd_vma *bitmask;
5161 bfd_byte *contents;
5162 long int min_dynindx;
5163 unsigned long int bucketcount;
5164 unsigned long int symindx;
5165 long int local_indx;
5166 long int shift1, shift2;
5167 unsigned long int mask;
5168 bfd_boolean error;
5169 };
5170
5171 /* This function will be called though elf_link_hash_traverse to store
5172 all hash value of the exported symbols in an array. */
5173
5174 static bfd_boolean
5175 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5176 {
5177 struct collect_gnu_hash_codes *s = data;
5178 const char *name;
5179 char *p;
5180 unsigned long ha;
5181 char *alc = NULL;
5182
5183 if (h->root.type == bfd_link_hash_warning)
5184 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5185
5186 /* Ignore indirect symbols. These are added by the versioning code. */
5187 if (h->dynindx == -1)
5188 return TRUE;
5189
5190 /* Ignore also local symbols and undefined symbols. */
5191 if (! (*s->bed->elf_hash_symbol) (h))
5192 return TRUE;
5193
5194 name = h->root.root.string;
5195 p = strchr (name, ELF_VER_CHR);
5196 if (p != NULL)
5197 {
5198 alc = bfd_malloc (p - name + 1);
5199 if (alc == NULL)
5200 {
5201 s->error = TRUE;
5202 return FALSE;
5203 }
5204 memcpy (alc, name, p - name);
5205 alc[p - name] = '\0';
5206 name = alc;
5207 }
5208
5209 /* Compute the hash value. */
5210 ha = bfd_elf_gnu_hash (name);
5211
5212 /* Store the found hash value in the array for compute_bucket_count,
5213 and also for .dynsym reordering purposes. */
5214 s->hashcodes[s->nsyms] = ha;
5215 s->hashval[h->dynindx] = ha;
5216 ++s->nsyms;
5217 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5218 s->min_dynindx = h->dynindx;
5219
5220 if (alc != NULL)
5221 free (alc);
5222
5223 return TRUE;
5224 }
5225
5226 /* This function will be called though elf_link_hash_traverse to do
5227 final dynaminc symbol renumbering. */
5228
5229 static bfd_boolean
5230 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5231 {
5232 struct collect_gnu_hash_codes *s = data;
5233 unsigned long int bucket;
5234 unsigned long int val;
5235
5236 if (h->root.type == bfd_link_hash_warning)
5237 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5238
5239 /* Ignore indirect symbols. */
5240 if (h->dynindx == -1)
5241 return TRUE;
5242
5243 /* Ignore also local symbols and undefined symbols. */
5244 if (! (*s->bed->elf_hash_symbol) (h))
5245 {
5246 if (h->dynindx >= s->min_dynindx)
5247 h->dynindx = s->local_indx++;
5248 return TRUE;
5249 }
5250
5251 bucket = s->hashval[h->dynindx] % s->bucketcount;
5252 val = (s->hashval[h->dynindx] >> s->shift1)
5253 & ((s->maskbits >> s->shift1) - 1);
5254 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5255 s->bitmask[val]
5256 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5257 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5258 if (s->counts[bucket] == 1)
5259 /* Last element terminates the chain. */
5260 val |= 1;
5261 bfd_put_32 (s->output_bfd, val,
5262 s->contents + (s->indx[bucket] - s->symindx) * 4);
5263 --s->counts[bucket];
5264 h->dynindx = s->indx[bucket]++;
5265 return TRUE;
5266 }
5267
5268 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5269
5270 bfd_boolean
5271 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5272 {
5273 return !(h->forced_local
5274 || h->root.type == bfd_link_hash_undefined
5275 || h->root.type == bfd_link_hash_undefweak
5276 || ((h->root.type == bfd_link_hash_defined
5277 || h->root.type == bfd_link_hash_defweak)
5278 && h->root.u.def.section->output_section == NULL));
5279 }
5280
5281 /* Array used to determine the number of hash table buckets to use
5282 based on the number of symbols there are. If there are fewer than
5283 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5284 fewer than 37 we use 17 buckets, and so forth. We never use more
5285 than 32771 buckets. */
5286
5287 static const size_t elf_buckets[] =
5288 {
5289 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5290 16411, 32771, 0
5291 };
5292
5293 /* Compute bucket count for hashing table. We do not use a static set
5294 of possible tables sizes anymore. Instead we determine for all
5295 possible reasonable sizes of the table the outcome (i.e., the
5296 number of collisions etc) and choose the best solution. The
5297 weighting functions are not too simple to allow the table to grow
5298 without bounds. Instead one of the weighting factors is the size.
5299 Therefore the result is always a good payoff between few collisions
5300 (= short chain lengths) and table size. */
5301 static size_t
5302 compute_bucket_count (struct bfd_link_info *info,
5303 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5304 unsigned long int nsyms,
5305 int gnu_hash)
5306 {
5307 size_t best_size = 0;
5308 unsigned long int i;
5309
5310 /* We have a problem here. The following code to optimize the table
5311 size requires an integer type with more the 32 bits. If
5312 BFD_HOST_U_64_BIT is set we know about such a type. */
5313 #ifdef BFD_HOST_U_64_BIT
5314 if (info->optimize)
5315 {
5316 size_t minsize;
5317 size_t maxsize;
5318 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5319 bfd *dynobj = elf_hash_table (info)->dynobj;
5320 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5321 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5322 unsigned long int *counts;
5323 bfd_size_type amt;
5324
5325 /* Possible optimization parameters: if we have NSYMS symbols we say
5326 that the hashing table must at least have NSYMS/4 and at most
5327 2*NSYMS buckets. */
5328 minsize = nsyms / 4;
5329 if (minsize == 0)
5330 minsize = 1;
5331 best_size = maxsize = nsyms * 2;
5332 if (gnu_hash)
5333 {
5334 if (minsize < 2)
5335 minsize = 2;
5336 if ((best_size & 31) == 0)
5337 ++best_size;
5338 }
5339
5340 /* Create array where we count the collisions in. We must use bfd_malloc
5341 since the size could be large. */
5342 amt = maxsize;
5343 amt *= sizeof (unsigned long int);
5344 counts = bfd_malloc (amt);
5345 if (counts == NULL)
5346 return 0;
5347
5348 /* Compute the "optimal" size for the hash table. The criteria is a
5349 minimal chain length. The minor criteria is (of course) the size
5350 of the table. */
5351 for (i = minsize; i < maxsize; ++i)
5352 {
5353 /* Walk through the array of hashcodes and count the collisions. */
5354 BFD_HOST_U_64_BIT max;
5355 unsigned long int j;
5356 unsigned long int fact;
5357
5358 if (gnu_hash && (i & 31) == 0)
5359 continue;
5360
5361 memset (counts, '\0', i * sizeof (unsigned long int));
5362
5363 /* Determine how often each hash bucket is used. */
5364 for (j = 0; j < nsyms; ++j)
5365 ++counts[hashcodes[j] % i];
5366
5367 /* For the weight function we need some information about the
5368 pagesize on the target. This is information need not be 100%
5369 accurate. Since this information is not available (so far) we
5370 define it here to a reasonable default value. If it is crucial
5371 to have a better value some day simply define this value. */
5372 # ifndef BFD_TARGET_PAGESIZE
5373 # define BFD_TARGET_PAGESIZE (4096)
5374 # endif
5375
5376 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5377 and the chains. */
5378 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5379
5380 # if 1
5381 /* Variant 1: optimize for short chains. We add the squares
5382 of all the chain lengths (which favors many small chain
5383 over a few long chains). */
5384 for (j = 0; j < i; ++j)
5385 max += counts[j] * counts[j];
5386
5387 /* This adds penalties for the overall size of the table. */
5388 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5389 max *= fact * fact;
5390 # else
5391 /* Variant 2: Optimize a lot more for small table. Here we
5392 also add squares of the size but we also add penalties for
5393 empty slots (the +1 term). */
5394 for (j = 0; j < i; ++j)
5395 max += (1 + counts[j]) * (1 + counts[j]);
5396
5397 /* The overall size of the table is considered, but not as
5398 strong as in variant 1, where it is squared. */
5399 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5400 max *= fact;
5401 # endif
5402
5403 /* Compare with current best results. */
5404 if (max < best_chlen)
5405 {
5406 best_chlen = max;
5407 best_size = i;
5408 }
5409 }
5410
5411 free (counts);
5412 }
5413 else
5414 #endif /* defined (BFD_HOST_U_64_BIT) */
5415 {
5416 /* This is the fallback solution if no 64bit type is available or if we
5417 are not supposed to spend much time on optimizations. We select the
5418 bucket count using a fixed set of numbers. */
5419 for (i = 0; elf_buckets[i] != 0; i++)
5420 {
5421 best_size = elf_buckets[i];
5422 if (nsyms < elf_buckets[i + 1])
5423 break;
5424 }
5425 if (gnu_hash && best_size < 2)
5426 best_size = 2;
5427 }
5428
5429 return best_size;
5430 }
5431
5432 /* Set up the sizes and contents of the ELF dynamic sections. This is
5433 called by the ELF linker emulation before_allocation routine. We
5434 must set the sizes of the sections before the linker sets the
5435 addresses of the various sections. */
5436
5437 bfd_boolean
5438 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5439 const char *soname,
5440 const char *rpath,
5441 const char *filter_shlib,
5442 const char * const *auxiliary_filters,
5443 struct bfd_link_info *info,
5444 asection **sinterpptr,
5445 struct bfd_elf_version_tree *verdefs)
5446 {
5447 bfd_size_type soname_indx;
5448 bfd *dynobj;
5449 const struct elf_backend_data *bed;
5450 struct elf_assign_sym_version_info asvinfo;
5451
5452 *sinterpptr = NULL;
5453
5454 soname_indx = (bfd_size_type) -1;
5455
5456 if (!is_elf_hash_table (info->hash))
5457 return TRUE;
5458
5459 bed = get_elf_backend_data (output_bfd);
5460 if (info->execstack)
5461 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
5462 else if (info->noexecstack)
5463 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
5464 else
5465 {
5466 bfd *inputobj;
5467 asection *notesec = NULL;
5468 int exec = 0;
5469
5470 for (inputobj = info->input_bfds;
5471 inputobj;
5472 inputobj = inputobj->link_next)
5473 {
5474 asection *s;
5475
5476 if (inputobj->flags & (DYNAMIC | EXEC_P | BFD_LINKER_CREATED))
5477 continue;
5478 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5479 if (s)
5480 {
5481 if (s->flags & SEC_CODE)
5482 exec = PF_X;
5483 notesec = s;
5484 }
5485 else if (bed->default_execstack)
5486 exec = PF_X;
5487 }
5488 if (notesec)
5489 {
5490 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
5491 if (exec && info->relocatable
5492 && notesec->output_section != bfd_abs_section_ptr)
5493 notesec->output_section->flags |= SEC_CODE;
5494 }
5495 }
5496
5497 /* Any syms created from now on start with -1 in
5498 got.refcount/offset and plt.refcount/offset. */
5499 elf_hash_table (info)->init_got_refcount
5500 = elf_hash_table (info)->init_got_offset;
5501 elf_hash_table (info)->init_plt_refcount
5502 = elf_hash_table (info)->init_plt_offset;
5503
5504 /* The backend may have to create some sections regardless of whether
5505 we're dynamic or not. */
5506 if (bed->elf_backend_always_size_sections
5507 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5508 return FALSE;
5509
5510 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
5511 return FALSE;
5512
5513 dynobj = elf_hash_table (info)->dynobj;
5514
5515 /* If there were no dynamic objects in the link, there is nothing to
5516 do here. */
5517 if (dynobj == NULL)
5518 return TRUE;
5519
5520 if (elf_hash_table (info)->dynamic_sections_created)
5521 {
5522 struct elf_info_failed eif;
5523 struct elf_link_hash_entry *h;
5524 asection *dynstr;
5525 struct bfd_elf_version_tree *t;
5526 struct bfd_elf_version_expr *d;
5527 asection *s;
5528 bfd_boolean all_defined;
5529
5530 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
5531 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
5532
5533 if (soname != NULL)
5534 {
5535 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5536 soname, TRUE);
5537 if (soname_indx == (bfd_size_type) -1
5538 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5539 return FALSE;
5540 }
5541
5542 if (info->symbolic)
5543 {
5544 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5545 return FALSE;
5546 info->flags |= DF_SYMBOLIC;
5547 }
5548
5549 if (rpath != NULL)
5550 {
5551 bfd_size_type indx;
5552
5553 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5554 TRUE);
5555 if (indx == (bfd_size_type) -1
5556 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
5557 return FALSE;
5558
5559 if (info->new_dtags)
5560 {
5561 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
5562 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
5563 return FALSE;
5564 }
5565 }
5566
5567 if (filter_shlib != NULL)
5568 {
5569 bfd_size_type indx;
5570
5571 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5572 filter_shlib, TRUE);
5573 if (indx == (bfd_size_type) -1
5574 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5575 return FALSE;
5576 }
5577
5578 if (auxiliary_filters != NULL)
5579 {
5580 const char * const *p;
5581
5582 for (p = auxiliary_filters; *p != NULL; p++)
5583 {
5584 bfd_size_type indx;
5585
5586 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5587 *p, TRUE);
5588 if (indx == (bfd_size_type) -1
5589 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5590 return FALSE;
5591 }
5592 }
5593
5594 eif.info = info;
5595 eif.verdefs = verdefs;
5596 eif.failed = FALSE;
5597
5598 /* If we are supposed to export all symbols into the dynamic symbol
5599 table (this is not the normal case), then do so. */
5600 if (info->export_dynamic
5601 || (info->executable && info->dynamic))
5602 {
5603 elf_link_hash_traverse (elf_hash_table (info),
5604 _bfd_elf_export_symbol,
5605 &eif);
5606 if (eif.failed)
5607 return FALSE;
5608 }
5609
5610 /* Make all global versions with definition. */
5611 for (t = verdefs; t != NULL; t = t->next)
5612 for (d = t->globals.list; d != NULL; d = d->next)
5613 if (!d->symver && d->literal)
5614 {
5615 const char *verstr, *name;
5616 size_t namelen, verlen, newlen;
5617 char *newname, *p;
5618 struct elf_link_hash_entry *newh;
5619
5620 name = d->pattern;
5621 namelen = strlen (name);
5622 verstr = t->name;
5623 verlen = strlen (verstr);
5624 newlen = namelen + verlen + 3;
5625
5626 newname = bfd_malloc (newlen);
5627 if (newname == NULL)
5628 return FALSE;
5629 memcpy (newname, name, namelen);
5630
5631 /* Check the hidden versioned definition. */
5632 p = newname + namelen;
5633 *p++ = ELF_VER_CHR;
5634 memcpy (p, verstr, verlen + 1);
5635 newh = elf_link_hash_lookup (elf_hash_table (info),
5636 newname, FALSE, FALSE,
5637 FALSE);
5638 if (newh == NULL
5639 || (newh->root.type != bfd_link_hash_defined
5640 && newh->root.type != bfd_link_hash_defweak))
5641 {
5642 /* Check the default versioned definition. */
5643 *p++ = ELF_VER_CHR;
5644 memcpy (p, verstr, verlen + 1);
5645 newh = elf_link_hash_lookup (elf_hash_table (info),
5646 newname, FALSE, FALSE,
5647 FALSE);
5648 }
5649 free (newname);
5650
5651 /* Mark this version if there is a definition and it is
5652 not defined in a shared object. */
5653 if (newh != NULL
5654 && !newh->def_dynamic
5655 && (newh->root.type == bfd_link_hash_defined
5656 || newh->root.type == bfd_link_hash_defweak))
5657 d->symver = 1;
5658 }
5659
5660 /* Attach all the symbols to their version information. */
5661 asvinfo.output_bfd = output_bfd;
5662 asvinfo.info = info;
5663 asvinfo.verdefs = verdefs;
5664 asvinfo.failed = FALSE;
5665
5666 elf_link_hash_traverse (elf_hash_table (info),
5667 _bfd_elf_link_assign_sym_version,
5668 &asvinfo);
5669 if (asvinfo.failed)
5670 return FALSE;
5671
5672 if (!info->allow_undefined_version)
5673 {
5674 /* Check if all global versions have a definition. */
5675 all_defined = TRUE;
5676 for (t = verdefs; t != NULL; t = t->next)
5677 for (d = t->globals.list; d != NULL; d = d->next)
5678 if (d->literal && !d->symver && !d->script)
5679 {
5680 (*_bfd_error_handler)
5681 (_("%s: undefined version: %s"),
5682 d->pattern, t->name);
5683 all_defined = FALSE;
5684 }
5685
5686 if (!all_defined)
5687 {
5688 bfd_set_error (bfd_error_bad_value);
5689 return FALSE;
5690 }
5691 }
5692
5693 /* Find all symbols which were defined in a dynamic object and make
5694 the backend pick a reasonable value for them. */
5695 elf_link_hash_traverse (elf_hash_table (info),
5696 _bfd_elf_adjust_dynamic_symbol,
5697 &eif);
5698 if (eif.failed)
5699 return FALSE;
5700
5701 /* Add some entries to the .dynamic section. We fill in some of the
5702 values later, in bfd_elf_final_link, but we must add the entries
5703 now so that we know the final size of the .dynamic section. */
5704
5705 /* If there are initialization and/or finalization functions to
5706 call then add the corresponding DT_INIT/DT_FINI entries. */
5707 h = (info->init_function
5708 ? elf_link_hash_lookup (elf_hash_table (info),
5709 info->init_function, FALSE,
5710 FALSE, FALSE)
5711 : NULL);
5712 if (h != NULL
5713 && (h->ref_regular
5714 || h->def_regular))
5715 {
5716 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5717 return FALSE;
5718 }
5719 h = (info->fini_function
5720 ? elf_link_hash_lookup (elf_hash_table (info),
5721 info->fini_function, FALSE,
5722 FALSE, FALSE)
5723 : NULL);
5724 if (h != NULL
5725 && (h->ref_regular
5726 || h->def_regular))
5727 {
5728 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5729 return FALSE;
5730 }
5731
5732 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5733 if (s != NULL && s->linker_has_input)
5734 {
5735 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5736 if (! info->executable)
5737 {
5738 bfd *sub;
5739 asection *o;
5740
5741 for (sub = info->input_bfds; sub != NULL;
5742 sub = sub->link_next)
5743 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
5744 for (o = sub->sections; o != NULL; o = o->next)
5745 if (elf_section_data (o)->this_hdr.sh_type
5746 == SHT_PREINIT_ARRAY)
5747 {
5748 (*_bfd_error_handler)
5749 (_("%B: .preinit_array section is not allowed in DSO"),
5750 sub);
5751 break;
5752 }
5753
5754 bfd_set_error (bfd_error_nonrepresentable_section);
5755 return FALSE;
5756 }
5757
5758 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5759 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5760 return FALSE;
5761 }
5762 s = bfd_get_section_by_name (output_bfd, ".init_array");
5763 if (s != NULL && s->linker_has_input)
5764 {
5765 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5766 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5767 return FALSE;
5768 }
5769 s = bfd_get_section_by_name (output_bfd, ".fini_array");
5770 if (s != NULL && s->linker_has_input)
5771 {
5772 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5773 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5774 return FALSE;
5775 }
5776
5777 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
5778 /* If .dynstr is excluded from the link, we don't want any of
5779 these tags. Strictly, we should be checking each section
5780 individually; This quick check covers for the case where
5781 someone does a /DISCARD/ : { *(*) }. */
5782 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5783 {
5784 bfd_size_type strsize;
5785
5786 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5787 if ((info->emit_hash
5788 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
5789 || (info->emit_gnu_hash
5790 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5791 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5792 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5793 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5794 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5795 bed->s->sizeof_sym))
5796 return FALSE;
5797 }
5798 }
5799
5800 /* The backend must work out the sizes of all the other dynamic
5801 sections. */
5802 if (bed->elf_backend_size_dynamic_sections
5803 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5804 return FALSE;
5805
5806 if (elf_hash_table (info)->dynamic_sections_created)
5807 {
5808 unsigned long section_sym_count;
5809 asection *s;
5810
5811 /* Set up the version definition section. */
5812 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5813 BFD_ASSERT (s != NULL);
5814
5815 /* We may have created additional version definitions if we are
5816 just linking a regular application. */
5817 verdefs = asvinfo.verdefs;
5818
5819 /* Skip anonymous version tag. */
5820 if (verdefs != NULL && verdefs->vernum == 0)
5821 verdefs = verdefs->next;
5822
5823 if (verdefs == NULL && !info->create_default_symver)
5824 s->flags |= SEC_EXCLUDE;
5825 else
5826 {
5827 unsigned int cdefs;
5828 bfd_size_type size;
5829 struct bfd_elf_version_tree *t;
5830 bfd_byte *p;
5831 Elf_Internal_Verdef def;
5832 Elf_Internal_Verdaux defaux;
5833 struct bfd_link_hash_entry *bh;
5834 struct elf_link_hash_entry *h;
5835 const char *name;
5836
5837 cdefs = 0;
5838 size = 0;
5839
5840 /* Make space for the base version. */
5841 size += sizeof (Elf_External_Verdef);
5842 size += sizeof (Elf_External_Verdaux);
5843 ++cdefs;
5844
5845 /* Make space for the default version. */
5846 if (info->create_default_symver)
5847 {
5848 size += sizeof (Elf_External_Verdef);
5849 ++cdefs;
5850 }
5851
5852 for (t = verdefs; t != NULL; t = t->next)
5853 {
5854 struct bfd_elf_version_deps *n;
5855
5856 size += sizeof (Elf_External_Verdef);
5857 size += sizeof (Elf_External_Verdaux);
5858 ++cdefs;
5859
5860 for (n = t->deps; n != NULL; n = n->next)
5861 size += sizeof (Elf_External_Verdaux);
5862 }
5863
5864 s->size = size;
5865 s->contents = bfd_alloc (output_bfd, s->size);
5866 if (s->contents == NULL && s->size != 0)
5867 return FALSE;
5868
5869 /* Fill in the version definition section. */
5870
5871 p = s->contents;
5872
5873 def.vd_version = VER_DEF_CURRENT;
5874 def.vd_flags = VER_FLG_BASE;
5875 def.vd_ndx = 1;
5876 def.vd_cnt = 1;
5877 if (info->create_default_symver)
5878 {
5879 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
5880 def.vd_next = sizeof (Elf_External_Verdef);
5881 }
5882 else
5883 {
5884 def.vd_aux = sizeof (Elf_External_Verdef);
5885 def.vd_next = (sizeof (Elf_External_Verdef)
5886 + sizeof (Elf_External_Verdaux));
5887 }
5888
5889 if (soname_indx != (bfd_size_type) -1)
5890 {
5891 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5892 soname_indx);
5893 def.vd_hash = bfd_elf_hash (soname);
5894 defaux.vda_name = soname_indx;
5895 name = soname;
5896 }
5897 else
5898 {
5899 bfd_size_type indx;
5900
5901 name = lbasename (output_bfd->filename);
5902 def.vd_hash = bfd_elf_hash (name);
5903 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5904 name, FALSE);
5905 if (indx == (bfd_size_type) -1)
5906 return FALSE;
5907 defaux.vda_name = indx;
5908 }
5909 defaux.vda_next = 0;
5910
5911 _bfd_elf_swap_verdef_out (output_bfd, &def,
5912 (Elf_External_Verdef *) p);
5913 p += sizeof (Elf_External_Verdef);
5914 if (info->create_default_symver)
5915 {
5916 /* Add a symbol representing this version. */
5917 bh = NULL;
5918 if (! (_bfd_generic_link_add_one_symbol
5919 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
5920 0, NULL, FALSE,
5921 get_elf_backend_data (dynobj)->collect, &bh)))
5922 return FALSE;
5923 h = (struct elf_link_hash_entry *) bh;
5924 h->non_elf = 0;
5925 h->def_regular = 1;
5926 h->type = STT_OBJECT;
5927 h->verinfo.vertree = NULL;
5928
5929 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5930 return FALSE;
5931
5932 /* Create a duplicate of the base version with the same
5933 aux block, but different flags. */
5934 def.vd_flags = 0;
5935 def.vd_ndx = 2;
5936 def.vd_aux = sizeof (Elf_External_Verdef);
5937 if (verdefs)
5938 def.vd_next = (sizeof (Elf_External_Verdef)
5939 + sizeof (Elf_External_Verdaux));
5940 else
5941 def.vd_next = 0;
5942 _bfd_elf_swap_verdef_out (output_bfd, &def,
5943 (Elf_External_Verdef *) p);
5944 p += sizeof (Elf_External_Verdef);
5945 }
5946 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5947 (Elf_External_Verdaux *) p);
5948 p += sizeof (Elf_External_Verdaux);
5949
5950 for (t = verdefs; t != NULL; t = t->next)
5951 {
5952 unsigned int cdeps;
5953 struct bfd_elf_version_deps *n;
5954
5955 cdeps = 0;
5956 for (n = t->deps; n != NULL; n = n->next)
5957 ++cdeps;
5958
5959 /* Add a symbol representing this version. */
5960 bh = NULL;
5961 if (! (_bfd_generic_link_add_one_symbol
5962 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5963 0, NULL, FALSE,
5964 get_elf_backend_data (dynobj)->collect, &bh)))
5965 return FALSE;
5966 h = (struct elf_link_hash_entry *) bh;
5967 h->non_elf = 0;
5968 h->def_regular = 1;
5969 h->type = STT_OBJECT;
5970 h->verinfo.vertree = t;
5971
5972 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5973 return FALSE;
5974
5975 def.vd_version = VER_DEF_CURRENT;
5976 def.vd_flags = 0;
5977 if (t->globals.list == NULL
5978 && t->locals.list == NULL
5979 && ! t->used)
5980 def.vd_flags |= VER_FLG_WEAK;
5981 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5982 def.vd_cnt = cdeps + 1;
5983 def.vd_hash = bfd_elf_hash (t->name);
5984 def.vd_aux = sizeof (Elf_External_Verdef);
5985 def.vd_next = 0;
5986 if (t->next != NULL)
5987 def.vd_next = (sizeof (Elf_External_Verdef)
5988 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5989
5990 _bfd_elf_swap_verdef_out (output_bfd, &def,
5991 (Elf_External_Verdef *) p);
5992 p += sizeof (Elf_External_Verdef);
5993
5994 defaux.vda_name = h->dynstr_index;
5995 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5996 h->dynstr_index);
5997 defaux.vda_next = 0;
5998 if (t->deps != NULL)
5999 defaux.vda_next = sizeof (Elf_External_Verdaux);
6000 t->name_indx = defaux.vda_name;
6001
6002 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6003 (Elf_External_Verdaux *) p);
6004 p += sizeof (Elf_External_Verdaux);
6005
6006 for (n = t->deps; n != NULL; n = n->next)
6007 {
6008 if (n->version_needed == NULL)
6009 {
6010 /* This can happen if there was an error in the
6011 version script. */
6012 defaux.vda_name = 0;
6013 }
6014 else
6015 {
6016 defaux.vda_name = n->version_needed->name_indx;
6017 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6018 defaux.vda_name);
6019 }
6020 if (n->next == NULL)
6021 defaux.vda_next = 0;
6022 else
6023 defaux.vda_next = sizeof (Elf_External_Verdaux);
6024
6025 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6026 (Elf_External_Verdaux *) p);
6027 p += sizeof (Elf_External_Verdaux);
6028 }
6029 }
6030
6031 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6032 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6033 return FALSE;
6034
6035 elf_tdata (output_bfd)->cverdefs = cdefs;
6036 }
6037
6038 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6039 {
6040 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6041 return FALSE;
6042 }
6043 else if (info->flags & DF_BIND_NOW)
6044 {
6045 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6046 return FALSE;
6047 }
6048
6049 if (info->flags_1)
6050 {
6051 if (info->executable)
6052 info->flags_1 &= ~ (DF_1_INITFIRST
6053 | DF_1_NODELETE
6054 | DF_1_NOOPEN);
6055 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6056 return FALSE;
6057 }
6058
6059 /* Work out the size of the version reference section. */
6060
6061 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
6062 BFD_ASSERT (s != NULL);
6063 {
6064 struct elf_find_verdep_info sinfo;
6065
6066 sinfo.output_bfd = output_bfd;
6067 sinfo.info = info;
6068 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6069 if (sinfo.vers == 0)
6070 sinfo.vers = 1;
6071 sinfo.failed = FALSE;
6072
6073 elf_link_hash_traverse (elf_hash_table (info),
6074 _bfd_elf_link_find_version_dependencies,
6075 &sinfo);
6076 if (sinfo.failed)
6077 return FALSE;
6078
6079 if (elf_tdata (output_bfd)->verref == NULL)
6080 s->flags |= SEC_EXCLUDE;
6081 else
6082 {
6083 Elf_Internal_Verneed *t;
6084 unsigned int size;
6085 unsigned int crefs;
6086 bfd_byte *p;
6087
6088 /* Build the version definition section. */
6089 size = 0;
6090 crefs = 0;
6091 for (t = elf_tdata (output_bfd)->verref;
6092 t != NULL;
6093 t = t->vn_nextref)
6094 {
6095 Elf_Internal_Vernaux *a;
6096
6097 size += sizeof (Elf_External_Verneed);
6098 ++crefs;
6099 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6100 size += sizeof (Elf_External_Vernaux);
6101 }
6102
6103 s->size = size;
6104 s->contents = bfd_alloc (output_bfd, s->size);
6105 if (s->contents == NULL)
6106 return FALSE;
6107
6108 p = s->contents;
6109 for (t = elf_tdata (output_bfd)->verref;
6110 t != NULL;
6111 t = t->vn_nextref)
6112 {
6113 unsigned int caux;
6114 Elf_Internal_Vernaux *a;
6115 bfd_size_type indx;
6116
6117 caux = 0;
6118 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6119 ++caux;
6120
6121 t->vn_version = VER_NEED_CURRENT;
6122 t->vn_cnt = caux;
6123 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6124 elf_dt_name (t->vn_bfd) != NULL
6125 ? elf_dt_name (t->vn_bfd)
6126 : lbasename (t->vn_bfd->filename),
6127 FALSE);
6128 if (indx == (bfd_size_type) -1)
6129 return FALSE;
6130 t->vn_file = indx;
6131 t->vn_aux = sizeof (Elf_External_Verneed);
6132 if (t->vn_nextref == NULL)
6133 t->vn_next = 0;
6134 else
6135 t->vn_next = (sizeof (Elf_External_Verneed)
6136 + caux * sizeof (Elf_External_Vernaux));
6137
6138 _bfd_elf_swap_verneed_out (output_bfd, t,
6139 (Elf_External_Verneed *) p);
6140 p += sizeof (Elf_External_Verneed);
6141
6142 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6143 {
6144 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6145 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6146 a->vna_nodename, FALSE);
6147 if (indx == (bfd_size_type) -1)
6148 return FALSE;
6149 a->vna_name = indx;
6150 if (a->vna_nextptr == NULL)
6151 a->vna_next = 0;
6152 else
6153 a->vna_next = sizeof (Elf_External_Vernaux);
6154
6155 _bfd_elf_swap_vernaux_out (output_bfd, a,
6156 (Elf_External_Vernaux *) p);
6157 p += sizeof (Elf_External_Vernaux);
6158 }
6159 }
6160
6161 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6162 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6163 return FALSE;
6164
6165 elf_tdata (output_bfd)->cverrefs = crefs;
6166 }
6167 }
6168
6169 if ((elf_tdata (output_bfd)->cverrefs == 0
6170 && elf_tdata (output_bfd)->cverdefs == 0)
6171 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6172 &section_sym_count) == 0)
6173 {
6174 s = bfd_get_section_by_name (dynobj, ".gnu.version");
6175 s->flags |= SEC_EXCLUDE;
6176 }
6177 }
6178 return TRUE;
6179 }
6180
6181 /* Find the first non-excluded output section. We'll use its
6182 section symbol for some emitted relocs. */
6183 void
6184 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6185 {
6186 asection *s;
6187
6188 for (s = output_bfd->sections; s != NULL; s = s->next)
6189 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6190 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6191 {
6192 elf_hash_table (info)->text_index_section = s;
6193 break;
6194 }
6195 }
6196
6197 /* Find two non-excluded output sections, one for code, one for data.
6198 We'll use their section symbols for some emitted relocs. */
6199 void
6200 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6201 {
6202 asection *s;
6203
6204 /* Data first, since setting text_index_section changes
6205 _bfd_elf_link_omit_section_dynsym. */
6206 for (s = output_bfd->sections; s != NULL; s = s->next)
6207 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6208 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6209 {
6210 elf_hash_table (info)->data_index_section = s;
6211 break;
6212 }
6213
6214 for (s = output_bfd->sections; s != NULL; s = s->next)
6215 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6216 == (SEC_ALLOC | SEC_READONLY))
6217 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6218 {
6219 elf_hash_table (info)->text_index_section = s;
6220 break;
6221 }
6222
6223 if (elf_hash_table (info)->text_index_section == NULL)
6224 elf_hash_table (info)->text_index_section
6225 = elf_hash_table (info)->data_index_section;
6226 }
6227
6228 bfd_boolean
6229 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6230 {
6231 const struct elf_backend_data *bed;
6232
6233 if (!is_elf_hash_table (info->hash))
6234 return TRUE;
6235
6236 bed = get_elf_backend_data (output_bfd);
6237 (*bed->elf_backend_init_index_section) (output_bfd, info);
6238
6239 if (elf_hash_table (info)->dynamic_sections_created)
6240 {
6241 bfd *dynobj;
6242 asection *s;
6243 bfd_size_type dynsymcount;
6244 unsigned long section_sym_count;
6245 unsigned int dtagcount;
6246
6247 dynobj = elf_hash_table (info)->dynobj;
6248
6249 /* Assign dynsym indicies. In a shared library we generate a
6250 section symbol for each output section, which come first.
6251 Next come all of the back-end allocated local dynamic syms,
6252 followed by the rest of the global symbols. */
6253
6254 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6255 &section_sym_count);
6256
6257 /* Work out the size of the symbol version section. */
6258 s = bfd_get_section_by_name (dynobj, ".gnu.version");
6259 BFD_ASSERT (s != NULL);
6260 if (dynsymcount != 0
6261 && (s->flags & SEC_EXCLUDE) == 0)
6262 {
6263 s->size = dynsymcount * sizeof (Elf_External_Versym);
6264 s->contents = bfd_zalloc (output_bfd, s->size);
6265 if (s->contents == NULL)
6266 return FALSE;
6267
6268 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6269 return FALSE;
6270 }
6271
6272 /* Set the size of the .dynsym and .hash sections. We counted
6273 the number of dynamic symbols in elf_link_add_object_symbols.
6274 We will build the contents of .dynsym and .hash when we build
6275 the final symbol table, because until then we do not know the
6276 correct value to give the symbols. We built the .dynstr
6277 section as we went along in elf_link_add_object_symbols. */
6278 s = bfd_get_section_by_name (dynobj, ".dynsym");
6279 BFD_ASSERT (s != NULL);
6280 s->size = dynsymcount * bed->s->sizeof_sym;
6281
6282 if (dynsymcount != 0)
6283 {
6284 s->contents = bfd_alloc (output_bfd, s->size);
6285 if (s->contents == NULL)
6286 return FALSE;
6287
6288 /* The first entry in .dynsym is a dummy symbol.
6289 Clear all the section syms, in case we don't output them all. */
6290 ++section_sym_count;
6291 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6292 }
6293
6294 elf_hash_table (info)->bucketcount = 0;
6295
6296 /* Compute the size of the hashing table. As a side effect this
6297 computes the hash values for all the names we export. */
6298 if (info->emit_hash)
6299 {
6300 unsigned long int *hashcodes;
6301 struct hash_codes_info hashinf;
6302 bfd_size_type amt;
6303 unsigned long int nsyms;
6304 size_t bucketcount;
6305 size_t hash_entry_size;
6306
6307 /* Compute the hash values for all exported symbols. At the same
6308 time store the values in an array so that we could use them for
6309 optimizations. */
6310 amt = dynsymcount * sizeof (unsigned long int);
6311 hashcodes = bfd_malloc (amt);
6312 if (hashcodes == NULL)
6313 return FALSE;
6314 hashinf.hashcodes = hashcodes;
6315 hashinf.error = FALSE;
6316
6317 /* Put all hash values in HASHCODES. */
6318 elf_link_hash_traverse (elf_hash_table (info),
6319 elf_collect_hash_codes, &hashinf);
6320 if (hashinf.error)
6321 {
6322 free (hashcodes);
6323 return FALSE;
6324 }
6325
6326 nsyms = hashinf.hashcodes - hashcodes;
6327 bucketcount
6328 = compute_bucket_count (info, hashcodes, nsyms, 0);
6329 free (hashcodes);
6330
6331 if (bucketcount == 0)
6332 return FALSE;
6333
6334 elf_hash_table (info)->bucketcount = bucketcount;
6335
6336 s = bfd_get_section_by_name (dynobj, ".hash");
6337 BFD_ASSERT (s != NULL);
6338 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6339 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6340 s->contents = bfd_zalloc (output_bfd, s->size);
6341 if (s->contents == NULL)
6342 return FALSE;
6343
6344 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6345 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6346 s->contents + hash_entry_size);
6347 }
6348
6349 if (info->emit_gnu_hash)
6350 {
6351 size_t i, cnt;
6352 unsigned char *contents;
6353 struct collect_gnu_hash_codes cinfo;
6354 bfd_size_type amt;
6355 size_t bucketcount;
6356
6357 memset (&cinfo, 0, sizeof (cinfo));
6358
6359 /* Compute the hash values for all exported symbols. At the same
6360 time store the values in an array so that we could use them for
6361 optimizations. */
6362 amt = dynsymcount * 2 * sizeof (unsigned long int);
6363 cinfo.hashcodes = bfd_malloc (amt);
6364 if (cinfo.hashcodes == NULL)
6365 return FALSE;
6366
6367 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6368 cinfo.min_dynindx = -1;
6369 cinfo.output_bfd = output_bfd;
6370 cinfo.bed = bed;
6371
6372 /* Put all hash values in HASHCODES. */
6373 elf_link_hash_traverse (elf_hash_table (info),
6374 elf_collect_gnu_hash_codes, &cinfo);
6375 if (cinfo.error)
6376 {
6377 free (cinfo.hashcodes);
6378 return FALSE;
6379 }
6380
6381 bucketcount
6382 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6383
6384 if (bucketcount == 0)
6385 {
6386 free (cinfo.hashcodes);
6387 return FALSE;
6388 }
6389
6390 s = bfd_get_section_by_name (dynobj, ".gnu.hash");
6391 BFD_ASSERT (s != NULL);
6392
6393 if (cinfo.nsyms == 0)
6394 {
6395 /* Empty .gnu.hash section is special. */
6396 BFD_ASSERT (cinfo.min_dynindx == -1);
6397 free (cinfo.hashcodes);
6398 s->size = 5 * 4 + bed->s->arch_size / 8;
6399 contents = bfd_zalloc (output_bfd, s->size);
6400 if (contents == NULL)
6401 return FALSE;
6402 s->contents = contents;
6403 /* 1 empty bucket. */
6404 bfd_put_32 (output_bfd, 1, contents);
6405 /* SYMIDX above the special symbol 0. */
6406 bfd_put_32 (output_bfd, 1, contents + 4);
6407 /* Just one word for bitmask. */
6408 bfd_put_32 (output_bfd, 1, contents + 8);
6409 /* Only hash fn bloom filter. */
6410 bfd_put_32 (output_bfd, 0, contents + 12);
6411 /* No hashes are valid - empty bitmask. */
6412 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6413 /* No hashes in the only bucket. */
6414 bfd_put_32 (output_bfd, 0,
6415 contents + 16 + bed->s->arch_size / 8);
6416 }
6417 else
6418 {
6419 unsigned long int maskwords, maskbitslog2;
6420 BFD_ASSERT (cinfo.min_dynindx != -1);
6421
6422 maskbitslog2 = bfd_log2 (cinfo.nsyms) + 1;
6423 if (maskbitslog2 < 3)
6424 maskbitslog2 = 5;
6425 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6426 maskbitslog2 = maskbitslog2 + 3;
6427 else
6428 maskbitslog2 = maskbitslog2 + 2;
6429 if (bed->s->arch_size == 64)
6430 {
6431 if (maskbitslog2 == 5)
6432 maskbitslog2 = 6;
6433 cinfo.shift1 = 6;
6434 }
6435 else
6436 cinfo.shift1 = 5;
6437 cinfo.mask = (1 << cinfo.shift1) - 1;
6438 cinfo.shift2 = maskbitslog2;
6439 cinfo.maskbits = 1 << maskbitslog2;
6440 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6441 amt = bucketcount * sizeof (unsigned long int) * 2;
6442 amt += maskwords * sizeof (bfd_vma);
6443 cinfo.bitmask = bfd_malloc (amt);
6444 if (cinfo.bitmask == NULL)
6445 {
6446 free (cinfo.hashcodes);
6447 return FALSE;
6448 }
6449
6450 cinfo.counts = (void *) (cinfo.bitmask + maskwords);
6451 cinfo.indx = cinfo.counts + bucketcount;
6452 cinfo.symindx = dynsymcount - cinfo.nsyms;
6453 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6454
6455 /* Determine how often each hash bucket is used. */
6456 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6457 for (i = 0; i < cinfo.nsyms; ++i)
6458 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6459
6460 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6461 if (cinfo.counts[i] != 0)
6462 {
6463 cinfo.indx[i] = cnt;
6464 cnt += cinfo.counts[i];
6465 }
6466 BFD_ASSERT (cnt == dynsymcount);
6467 cinfo.bucketcount = bucketcount;
6468 cinfo.local_indx = cinfo.min_dynindx;
6469
6470 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6471 s->size += cinfo.maskbits / 8;
6472 contents = bfd_zalloc (output_bfd, s->size);
6473 if (contents == NULL)
6474 {
6475 free (cinfo.bitmask);
6476 free (cinfo.hashcodes);
6477 return FALSE;
6478 }
6479
6480 s->contents = contents;
6481 bfd_put_32 (output_bfd, bucketcount, contents);
6482 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6483 bfd_put_32 (output_bfd, maskwords, contents + 8);
6484 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6485 contents += 16 + cinfo.maskbits / 8;
6486
6487 for (i = 0; i < bucketcount; ++i)
6488 {
6489 if (cinfo.counts[i] == 0)
6490 bfd_put_32 (output_bfd, 0, contents);
6491 else
6492 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6493 contents += 4;
6494 }
6495
6496 cinfo.contents = contents;
6497
6498 /* Renumber dynamic symbols, populate .gnu.hash section. */
6499 elf_link_hash_traverse (elf_hash_table (info),
6500 elf_renumber_gnu_hash_syms, &cinfo);
6501
6502 contents = s->contents + 16;
6503 for (i = 0; i < maskwords; ++i)
6504 {
6505 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6506 contents);
6507 contents += bed->s->arch_size / 8;
6508 }
6509
6510 free (cinfo.bitmask);
6511 free (cinfo.hashcodes);
6512 }
6513 }
6514
6515 s = bfd_get_section_by_name (dynobj, ".dynstr");
6516 BFD_ASSERT (s != NULL);
6517
6518 elf_finalize_dynstr (output_bfd, info);
6519
6520 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6521
6522 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6523 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6524 return FALSE;
6525 }
6526
6527 return TRUE;
6528 }
6529 \f
6530 /* Indicate that we are only retrieving symbol values from this
6531 section. */
6532
6533 void
6534 _bfd_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
6535 {
6536 if (is_elf_hash_table (info->hash))
6537 sec->sec_info_type = ELF_INFO_TYPE_JUST_SYMS;
6538 _bfd_generic_link_just_syms (sec, info);
6539 }
6540
6541 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
6542
6543 static void
6544 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6545 asection *sec)
6546 {
6547 BFD_ASSERT (sec->sec_info_type == ELF_INFO_TYPE_MERGE);
6548 sec->sec_info_type = ELF_INFO_TYPE_NONE;
6549 }
6550
6551 /* Finish SHF_MERGE section merging. */
6552
6553 bfd_boolean
6554 _bfd_elf_merge_sections (bfd *abfd, struct bfd_link_info *info)
6555 {
6556 bfd *ibfd;
6557 asection *sec;
6558
6559 if (!is_elf_hash_table (info->hash))
6560 return FALSE;
6561
6562 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
6563 if ((ibfd->flags & DYNAMIC) == 0)
6564 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6565 if ((sec->flags & SEC_MERGE) != 0
6566 && !bfd_is_abs_section (sec->output_section))
6567 {
6568 struct bfd_elf_section_data *secdata;
6569
6570 secdata = elf_section_data (sec);
6571 if (! _bfd_add_merge_section (abfd,
6572 &elf_hash_table (info)->merge_info,
6573 sec, &secdata->sec_info))
6574 return FALSE;
6575 else if (secdata->sec_info)
6576 sec->sec_info_type = ELF_INFO_TYPE_MERGE;
6577 }
6578
6579 if (elf_hash_table (info)->merge_info != NULL)
6580 _bfd_merge_sections (abfd, info, elf_hash_table (info)->merge_info,
6581 merge_sections_remove_hook);
6582 return TRUE;
6583 }
6584
6585 /* Create an entry in an ELF linker hash table. */
6586
6587 struct bfd_hash_entry *
6588 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6589 struct bfd_hash_table *table,
6590 const char *string)
6591 {
6592 /* Allocate the structure if it has not already been allocated by a
6593 subclass. */
6594 if (entry == NULL)
6595 {
6596 entry = bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
6597 if (entry == NULL)
6598 return entry;
6599 }
6600
6601 /* Call the allocation method of the superclass. */
6602 entry = _bfd_link_hash_newfunc (entry, table, string);
6603 if (entry != NULL)
6604 {
6605 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6606 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6607
6608 /* Set local fields. */
6609 ret->indx = -1;
6610 ret->dynindx = -1;
6611 ret->got = htab->init_got_refcount;
6612 ret->plt = htab->init_plt_refcount;
6613 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
6614 - offsetof (struct elf_link_hash_entry, size)));
6615 /* Assume that we have been called by a non-ELF symbol reader.
6616 This flag is then reset by the code which reads an ELF input
6617 file. This ensures that a symbol created by a non-ELF symbol
6618 reader will have the flag set correctly. */
6619 ret->non_elf = 1;
6620 }
6621
6622 return entry;
6623 }
6624
6625 /* Copy data from an indirect symbol to its direct symbol, hiding the
6626 old indirect symbol. Also used for copying flags to a weakdef. */
6627
6628 void
6629 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
6630 struct elf_link_hash_entry *dir,
6631 struct elf_link_hash_entry *ind)
6632 {
6633 struct elf_link_hash_table *htab;
6634
6635 /* Copy down any references that we may have already seen to the
6636 symbol which just became indirect. */
6637
6638 dir->ref_dynamic |= ind->ref_dynamic;
6639 dir->ref_regular |= ind->ref_regular;
6640 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
6641 dir->non_got_ref |= ind->non_got_ref;
6642 dir->needs_plt |= ind->needs_plt;
6643 dir->pointer_equality_needed |= ind->pointer_equality_needed;
6644
6645 if (ind->root.type != bfd_link_hash_indirect)
6646 return;
6647
6648 /* Copy over the global and procedure linkage table refcount entries.
6649 These may have been already set up by a check_relocs routine. */
6650 htab = elf_hash_table (info);
6651 if (ind->got.refcount > htab->init_got_refcount.refcount)
6652 {
6653 if (dir->got.refcount < 0)
6654 dir->got.refcount = 0;
6655 dir->got.refcount += ind->got.refcount;
6656 ind->got.refcount = htab->init_got_refcount.refcount;
6657 }
6658
6659 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
6660 {
6661 if (dir->plt.refcount < 0)
6662 dir->plt.refcount = 0;
6663 dir->plt.refcount += ind->plt.refcount;
6664 ind->plt.refcount = htab->init_plt_refcount.refcount;
6665 }
6666
6667 if (ind->dynindx != -1)
6668 {
6669 if (dir->dynindx != -1)
6670 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
6671 dir->dynindx = ind->dynindx;
6672 dir->dynstr_index = ind->dynstr_index;
6673 ind->dynindx = -1;
6674 ind->dynstr_index = 0;
6675 }
6676 }
6677
6678 void
6679 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
6680 struct elf_link_hash_entry *h,
6681 bfd_boolean force_local)
6682 {
6683 h->plt = elf_hash_table (info)->init_plt_offset;
6684 h->needs_plt = 0;
6685 if (force_local)
6686 {
6687 h->forced_local = 1;
6688 if (h->dynindx != -1)
6689 {
6690 h->dynindx = -1;
6691 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
6692 h->dynstr_index);
6693 }
6694 }
6695 }
6696
6697 /* Initialize an ELF linker hash table. */
6698
6699 bfd_boolean
6700 _bfd_elf_link_hash_table_init
6701 (struct elf_link_hash_table *table,
6702 bfd *abfd,
6703 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
6704 struct bfd_hash_table *,
6705 const char *),
6706 unsigned int entsize)
6707 {
6708 bfd_boolean ret;
6709 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
6710
6711 memset (table, 0, sizeof * table);
6712 table->init_got_refcount.refcount = can_refcount - 1;
6713 table->init_plt_refcount.refcount = can_refcount - 1;
6714 table->init_got_offset.offset = -(bfd_vma) 1;
6715 table->init_plt_offset.offset = -(bfd_vma) 1;
6716 /* The first dynamic symbol is a dummy. */
6717 table->dynsymcount = 1;
6718
6719 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
6720 table->root.type = bfd_link_elf_hash_table;
6721
6722 return ret;
6723 }
6724
6725 /* Create an ELF linker hash table. */
6726
6727 struct bfd_link_hash_table *
6728 _bfd_elf_link_hash_table_create (bfd *abfd)
6729 {
6730 struct elf_link_hash_table *ret;
6731 bfd_size_type amt = sizeof (struct elf_link_hash_table);
6732
6733 ret = bfd_malloc (amt);
6734 if (ret == NULL)
6735 return NULL;
6736
6737 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
6738 sizeof (struct elf_link_hash_entry)))
6739 {
6740 free (ret);
6741 return NULL;
6742 }
6743
6744 return &ret->root;
6745 }
6746
6747 /* This is a hook for the ELF emulation code in the generic linker to
6748 tell the backend linker what file name to use for the DT_NEEDED
6749 entry for a dynamic object. */
6750
6751 void
6752 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
6753 {
6754 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6755 && bfd_get_format (abfd) == bfd_object)
6756 elf_dt_name (abfd) = name;
6757 }
6758
6759 int
6760 bfd_elf_get_dyn_lib_class (bfd *abfd)
6761 {
6762 int lib_class;
6763 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6764 && bfd_get_format (abfd) == bfd_object)
6765 lib_class = elf_dyn_lib_class (abfd);
6766 else
6767 lib_class = 0;
6768 return lib_class;
6769 }
6770
6771 void
6772 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
6773 {
6774 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6775 && bfd_get_format (abfd) == bfd_object)
6776 elf_dyn_lib_class (abfd) = lib_class;
6777 }
6778
6779 /* Get the list of DT_NEEDED entries for a link. This is a hook for
6780 the linker ELF emulation code. */
6781
6782 struct bfd_link_needed_list *
6783 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
6784 struct bfd_link_info *info)
6785 {
6786 if (! is_elf_hash_table (info->hash))
6787 return NULL;
6788 return elf_hash_table (info)->needed;
6789 }
6790
6791 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
6792 hook for the linker ELF emulation code. */
6793
6794 struct bfd_link_needed_list *
6795 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
6796 struct bfd_link_info *info)
6797 {
6798 if (! is_elf_hash_table (info->hash))
6799 return NULL;
6800 return elf_hash_table (info)->runpath;
6801 }
6802
6803 /* Get the name actually used for a dynamic object for a link. This
6804 is the SONAME entry if there is one. Otherwise, it is the string
6805 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
6806
6807 const char *
6808 bfd_elf_get_dt_soname (bfd *abfd)
6809 {
6810 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6811 && bfd_get_format (abfd) == bfd_object)
6812 return elf_dt_name (abfd);
6813 return NULL;
6814 }
6815
6816 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
6817 the ELF linker emulation code. */
6818
6819 bfd_boolean
6820 bfd_elf_get_bfd_needed_list (bfd *abfd,
6821 struct bfd_link_needed_list **pneeded)
6822 {
6823 asection *s;
6824 bfd_byte *dynbuf = NULL;
6825 unsigned int elfsec;
6826 unsigned long shlink;
6827 bfd_byte *extdyn, *extdynend;
6828 size_t extdynsize;
6829 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
6830
6831 *pneeded = NULL;
6832
6833 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
6834 || bfd_get_format (abfd) != bfd_object)
6835 return TRUE;
6836
6837 s = bfd_get_section_by_name (abfd, ".dynamic");
6838 if (s == NULL || s->size == 0)
6839 return TRUE;
6840
6841 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
6842 goto error_return;
6843
6844 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
6845 if (elfsec == SHN_BAD)
6846 goto error_return;
6847
6848 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
6849
6850 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
6851 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
6852
6853 extdyn = dynbuf;
6854 extdynend = extdyn + s->size;
6855 for (; extdyn < extdynend; extdyn += extdynsize)
6856 {
6857 Elf_Internal_Dyn dyn;
6858
6859 (*swap_dyn_in) (abfd, extdyn, &dyn);
6860
6861 if (dyn.d_tag == DT_NULL)
6862 break;
6863
6864 if (dyn.d_tag == DT_NEEDED)
6865 {
6866 const char *string;
6867 struct bfd_link_needed_list *l;
6868 unsigned int tagv = dyn.d_un.d_val;
6869 bfd_size_type amt;
6870
6871 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
6872 if (string == NULL)
6873 goto error_return;
6874
6875 amt = sizeof *l;
6876 l = bfd_alloc (abfd, amt);
6877 if (l == NULL)
6878 goto error_return;
6879
6880 l->by = abfd;
6881 l->name = string;
6882 l->next = *pneeded;
6883 *pneeded = l;
6884 }
6885 }
6886
6887 free (dynbuf);
6888
6889 return TRUE;
6890
6891 error_return:
6892 if (dynbuf != NULL)
6893 free (dynbuf);
6894 return FALSE;
6895 }
6896
6897 struct elf_symbuf_symbol
6898 {
6899 unsigned long st_name; /* Symbol name, index in string tbl */
6900 unsigned char st_info; /* Type and binding attributes */
6901 unsigned char st_other; /* Visibilty, and target specific */
6902 };
6903
6904 struct elf_symbuf_head
6905 {
6906 struct elf_symbuf_symbol *ssym;
6907 bfd_size_type count;
6908 unsigned int st_shndx;
6909 };
6910
6911 struct elf_symbol
6912 {
6913 union
6914 {
6915 Elf_Internal_Sym *isym;
6916 struct elf_symbuf_symbol *ssym;
6917 } u;
6918 const char *name;
6919 };
6920
6921 /* Sort references to symbols by ascending section number. */
6922
6923 static int
6924 elf_sort_elf_symbol (const void *arg1, const void *arg2)
6925 {
6926 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
6927 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
6928
6929 return s1->st_shndx - s2->st_shndx;
6930 }
6931
6932 static int
6933 elf_sym_name_compare (const void *arg1, const void *arg2)
6934 {
6935 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
6936 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
6937 return strcmp (s1->name, s2->name);
6938 }
6939
6940 static struct elf_symbuf_head *
6941 elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
6942 {
6943 Elf_Internal_Sym **ind, **indbufend, **indbuf;
6944 struct elf_symbuf_symbol *ssym;
6945 struct elf_symbuf_head *ssymbuf, *ssymhead;
6946 bfd_size_type i, shndx_count, total_size;
6947
6948 indbuf = bfd_malloc2 (symcount, sizeof (*indbuf));
6949 if (indbuf == NULL)
6950 return NULL;
6951
6952 for (ind = indbuf, i = 0; i < symcount; i++)
6953 if (isymbuf[i].st_shndx != SHN_UNDEF)
6954 *ind++ = &isymbuf[i];
6955 indbufend = ind;
6956
6957 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
6958 elf_sort_elf_symbol);
6959
6960 shndx_count = 0;
6961 if (indbufend > indbuf)
6962 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
6963 if (ind[0]->st_shndx != ind[1]->st_shndx)
6964 shndx_count++;
6965
6966 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
6967 + (indbufend - indbuf) * sizeof (*ssym));
6968 ssymbuf = bfd_malloc (total_size);
6969 if (ssymbuf == NULL)
6970 {
6971 free (indbuf);
6972 return NULL;
6973 }
6974
6975 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
6976 ssymbuf->ssym = NULL;
6977 ssymbuf->count = shndx_count;
6978 ssymbuf->st_shndx = 0;
6979 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
6980 {
6981 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
6982 {
6983 ssymhead++;
6984 ssymhead->ssym = ssym;
6985 ssymhead->count = 0;
6986 ssymhead->st_shndx = (*ind)->st_shndx;
6987 }
6988 ssym->st_name = (*ind)->st_name;
6989 ssym->st_info = (*ind)->st_info;
6990 ssym->st_other = (*ind)->st_other;
6991 ssymhead->count++;
6992 }
6993 BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
6994 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
6995 == total_size));
6996
6997 free (indbuf);
6998 return ssymbuf;
6999 }
7000
7001 /* Check if 2 sections define the same set of local and global
7002 symbols. */
7003
7004 static bfd_boolean
7005 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7006 struct bfd_link_info *info)
7007 {
7008 bfd *bfd1, *bfd2;
7009 const struct elf_backend_data *bed1, *bed2;
7010 Elf_Internal_Shdr *hdr1, *hdr2;
7011 bfd_size_type symcount1, symcount2;
7012 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7013 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7014 Elf_Internal_Sym *isym, *isymend;
7015 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7016 bfd_size_type count1, count2, i;
7017 unsigned int shndx1, shndx2;
7018 bfd_boolean result;
7019
7020 bfd1 = sec1->owner;
7021 bfd2 = sec2->owner;
7022
7023 /* Both sections have to be in ELF. */
7024 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7025 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7026 return FALSE;
7027
7028 if (elf_section_type (sec1) != elf_section_type (sec2))
7029 return FALSE;
7030
7031 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7032 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7033 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7034 return FALSE;
7035
7036 bed1 = get_elf_backend_data (bfd1);
7037 bed2 = get_elf_backend_data (bfd2);
7038 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7039 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7040 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7041 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7042
7043 if (symcount1 == 0 || symcount2 == 0)
7044 return FALSE;
7045
7046 result = FALSE;
7047 isymbuf1 = NULL;
7048 isymbuf2 = NULL;
7049 ssymbuf1 = elf_tdata (bfd1)->symbuf;
7050 ssymbuf2 = elf_tdata (bfd2)->symbuf;
7051
7052 if (ssymbuf1 == NULL)
7053 {
7054 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7055 NULL, NULL, NULL);
7056 if (isymbuf1 == NULL)
7057 goto done;
7058
7059 if (!info->reduce_memory_overheads)
7060 elf_tdata (bfd1)->symbuf = ssymbuf1
7061 = elf_create_symbuf (symcount1, isymbuf1);
7062 }
7063
7064 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7065 {
7066 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7067 NULL, NULL, NULL);
7068 if (isymbuf2 == NULL)
7069 goto done;
7070
7071 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7072 elf_tdata (bfd2)->symbuf = ssymbuf2
7073 = elf_create_symbuf (symcount2, isymbuf2);
7074 }
7075
7076 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7077 {
7078 /* Optimized faster version. */
7079 bfd_size_type lo, hi, mid;
7080 struct elf_symbol *symp;
7081 struct elf_symbuf_symbol *ssym, *ssymend;
7082
7083 lo = 0;
7084 hi = ssymbuf1->count;
7085 ssymbuf1++;
7086 count1 = 0;
7087 while (lo < hi)
7088 {
7089 mid = (lo + hi) / 2;
7090 if (shndx1 < ssymbuf1[mid].st_shndx)
7091 hi = mid;
7092 else if (shndx1 > ssymbuf1[mid].st_shndx)
7093 lo = mid + 1;
7094 else
7095 {
7096 count1 = ssymbuf1[mid].count;
7097 ssymbuf1 += mid;
7098 break;
7099 }
7100 }
7101
7102 lo = 0;
7103 hi = ssymbuf2->count;
7104 ssymbuf2++;
7105 count2 = 0;
7106 while (lo < hi)
7107 {
7108 mid = (lo + hi) / 2;
7109 if (shndx2 < ssymbuf2[mid].st_shndx)
7110 hi = mid;
7111 else if (shndx2 > ssymbuf2[mid].st_shndx)
7112 lo = mid + 1;
7113 else
7114 {
7115 count2 = ssymbuf2[mid].count;
7116 ssymbuf2 += mid;
7117 break;
7118 }
7119 }
7120
7121 if (count1 == 0 || count2 == 0 || count1 != count2)
7122 goto done;
7123
7124 symtable1 = bfd_malloc (count1 * sizeof (struct elf_symbol));
7125 symtable2 = bfd_malloc (count2 * sizeof (struct elf_symbol));
7126 if (symtable1 == NULL || symtable2 == NULL)
7127 goto done;
7128
7129 symp = symtable1;
7130 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7131 ssym < ssymend; ssym++, symp++)
7132 {
7133 symp->u.ssym = ssym;
7134 symp->name = bfd_elf_string_from_elf_section (bfd1,
7135 hdr1->sh_link,
7136 ssym->st_name);
7137 }
7138
7139 symp = symtable2;
7140 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7141 ssym < ssymend; ssym++, symp++)
7142 {
7143 symp->u.ssym = ssym;
7144 symp->name = bfd_elf_string_from_elf_section (bfd2,
7145 hdr2->sh_link,
7146 ssym->st_name);
7147 }
7148
7149 /* Sort symbol by name. */
7150 qsort (symtable1, count1, sizeof (struct elf_symbol),
7151 elf_sym_name_compare);
7152 qsort (symtable2, count1, sizeof (struct elf_symbol),
7153 elf_sym_name_compare);
7154
7155 for (i = 0; i < count1; i++)
7156 /* Two symbols must have the same binding, type and name. */
7157 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7158 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7159 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7160 goto done;
7161
7162 result = TRUE;
7163 goto done;
7164 }
7165
7166 symtable1 = bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7167 symtable2 = bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7168 if (symtable1 == NULL || symtable2 == NULL)
7169 goto done;
7170
7171 /* Count definitions in the section. */
7172 count1 = 0;
7173 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7174 if (isym->st_shndx == shndx1)
7175 symtable1[count1++].u.isym = isym;
7176
7177 count2 = 0;
7178 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7179 if (isym->st_shndx == shndx2)
7180 symtable2[count2++].u.isym = isym;
7181
7182 if (count1 == 0 || count2 == 0 || count1 != count2)
7183 goto done;
7184
7185 for (i = 0; i < count1; i++)
7186 symtable1[i].name
7187 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7188 symtable1[i].u.isym->st_name);
7189
7190 for (i = 0; i < count2; i++)
7191 symtable2[i].name
7192 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7193 symtable2[i].u.isym->st_name);
7194
7195 /* Sort symbol by name. */
7196 qsort (symtable1, count1, sizeof (struct elf_symbol),
7197 elf_sym_name_compare);
7198 qsort (symtable2, count1, sizeof (struct elf_symbol),
7199 elf_sym_name_compare);
7200
7201 for (i = 0; i < count1; i++)
7202 /* Two symbols must have the same binding, type and name. */
7203 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7204 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7205 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7206 goto done;
7207
7208 result = TRUE;
7209
7210 done:
7211 if (symtable1)
7212 free (symtable1);
7213 if (symtable2)
7214 free (symtable2);
7215 if (isymbuf1)
7216 free (isymbuf1);
7217 if (isymbuf2)
7218 free (isymbuf2);
7219
7220 return result;
7221 }
7222
7223 /* Return TRUE if 2 section types are compatible. */
7224
7225 bfd_boolean
7226 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7227 bfd *bbfd, const asection *bsec)
7228 {
7229 if (asec == NULL
7230 || bsec == NULL
7231 || abfd->xvec->flavour != bfd_target_elf_flavour
7232 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7233 return TRUE;
7234
7235 return elf_section_type (asec) == elf_section_type (bsec);
7236 }
7237 \f
7238 /* Final phase of ELF linker. */
7239
7240 /* A structure we use to avoid passing large numbers of arguments. */
7241
7242 struct elf_final_link_info
7243 {
7244 /* General link information. */
7245 struct bfd_link_info *info;
7246 /* Output BFD. */
7247 bfd *output_bfd;
7248 /* Symbol string table. */
7249 struct bfd_strtab_hash *symstrtab;
7250 /* .dynsym section. */
7251 asection *dynsym_sec;
7252 /* .hash section. */
7253 asection *hash_sec;
7254 /* symbol version section (.gnu.version). */
7255 asection *symver_sec;
7256 /* Buffer large enough to hold contents of any section. */
7257 bfd_byte *contents;
7258 /* Buffer large enough to hold external relocs of any section. */
7259 void *external_relocs;
7260 /* Buffer large enough to hold internal relocs of any section. */
7261 Elf_Internal_Rela *internal_relocs;
7262 /* Buffer large enough to hold external local symbols of any input
7263 BFD. */
7264 bfd_byte *external_syms;
7265 /* And a buffer for symbol section indices. */
7266 Elf_External_Sym_Shndx *locsym_shndx;
7267 /* Buffer large enough to hold internal local symbols of any input
7268 BFD. */
7269 Elf_Internal_Sym *internal_syms;
7270 /* Array large enough to hold a symbol index for each local symbol
7271 of any input BFD. */
7272 long *indices;
7273 /* Array large enough to hold a section pointer for each local
7274 symbol of any input BFD. */
7275 asection **sections;
7276 /* Buffer to hold swapped out symbols. */
7277 bfd_byte *symbuf;
7278 /* And one for symbol section indices. */
7279 Elf_External_Sym_Shndx *symshndxbuf;
7280 /* Number of swapped out symbols in buffer. */
7281 size_t symbuf_count;
7282 /* Number of symbols which fit in symbuf. */
7283 size_t symbuf_size;
7284 /* And same for symshndxbuf. */
7285 size_t shndxbuf_size;
7286 };
7287
7288 /* This struct is used to pass information to elf_link_output_extsym. */
7289
7290 struct elf_outext_info
7291 {
7292 bfd_boolean failed;
7293 bfd_boolean localsyms;
7294 struct elf_final_link_info *finfo;
7295 };
7296
7297
7298 /* Support for evaluating a complex relocation.
7299
7300 Complex relocations are generalized, self-describing relocations. The
7301 implementation of them consists of two parts: complex symbols, and the
7302 relocations themselves.
7303
7304 The relocations are use a reserved elf-wide relocation type code (R_RELC
7305 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7306 information (start bit, end bit, word width, etc) into the addend. This
7307 information is extracted from CGEN-generated operand tables within gas.
7308
7309 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7310 internal) representing prefix-notation expressions, including but not
7311 limited to those sorts of expressions normally encoded as addends in the
7312 addend field. The symbol mangling format is:
7313
7314 <node> := <literal>
7315 | <unary-operator> ':' <node>
7316 | <binary-operator> ':' <node> ':' <node>
7317 ;
7318
7319 <literal> := 's' <digits=N> ':' <N character symbol name>
7320 | 'S' <digits=N> ':' <N character section name>
7321 | '#' <hexdigits>
7322 ;
7323
7324 <binary-operator> := as in C
7325 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7326
7327 static void
7328 set_symbol_value (bfd *bfd_with_globals,
7329 Elf_Internal_Sym *isymbuf,
7330 size_t locsymcount,
7331 size_t symidx,
7332 bfd_vma val)
7333 {
7334 struct elf_link_hash_entry **sym_hashes;
7335 struct elf_link_hash_entry *h;
7336 size_t extsymoff = locsymcount;
7337
7338 if (symidx < locsymcount)
7339 {
7340 Elf_Internal_Sym *sym;
7341
7342 sym = isymbuf + symidx;
7343 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7344 {
7345 /* It is a local symbol: move it to the
7346 "absolute" section and give it a value. */
7347 sym->st_shndx = SHN_ABS;
7348 sym->st_value = val;
7349 return;
7350 }
7351 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7352 extsymoff = 0;
7353 }
7354
7355 /* It is a global symbol: set its link type
7356 to "defined" and give it a value. */
7357
7358 sym_hashes = elf_sym_hashes (bfd_with_globals);
7359 h = sym_hashes [symidx - extsymoff];
7360 while (h->root.type == bfd_link_hash_indirect
7361 || h->root.type == bfd_link_hash_warning)
7362 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7363 h->root.type = bfd_link_hash_defined;
7364 h->root.u.def.value = val;
7365 h->root.u.def.section = bfd_abs_section_ptr;
7366 }
7367
7368 static bfd_boolean
7369 resolve_symbol (const char *name,
7370 bfd *input_bfd,
7371 struct elf_final_link_info *finfo,
7372 bfd_vma *result,
7373 Elf_Internal_Sym *isymbuf,
7374 size_t locsymcount)
7375 {
7376 Elf_Internal_Sym *sym;
7377 struct bfd_link_hash_entry *global_entry;
7378 const char *candidate = NULL;
7379 Elf_Internal_Shdr *symtab_hdr;
7380 size_t i;
7381
7382 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7383
7384 for (i = 0; i < locsymcount; ++ i)
7385 {
7386 sym = isymbuf + i;
7387
7388 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7389 continue;
7390
7391 candidate = bfd_elf_string_from_elf_section (input_bfd,
7392 symtab_hdr->sh_link,
7393 sym->st_name);
7394 #ifdef DEBUG
7395 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7396 name, candidate, (unsigned long) sym->st_value);
7397 #endif
7398 if (candidate && strcmp (candidate, name) == 0)
7399 {
7400 asection *sec = finfo->sections [i];
7401
7402 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7403 *result += sec->output_offset + sec->output_section->vma;
7404 #ifdef DEBUG
7405 printf ("Found symbol with value %8.8lx\n",
7406 (unsigned long) *result);
7407 #endif
7408 return TRUE;
7409 }
7410 }
7411
7412 /* Hmm, haven't found it yet. perhaps it is a global. */
7413 global_entry = bfd_link_hash_lookup (finfo->info->hash, name,
7414 FALSE, FALSE, TRUE);
7415 if (!global_entry)
7416 return FALSE;
7417
7418 if (global_entry->type == bfd_link_hash_defined
7419 || global_entry->type == bfd_link_hash_defweak)
7420 {
7421 *result = (global_entry->u.def.value
7422 + global_entry->u.def.section->output_section->vma
7423 + global_entry->u.def.section->output_offset);
7424 #ifdef DEBUG
7425 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7426 global_entry->root.string, (unsigned long) *result);
7427 #endif
7428 return TRUE;
7429 }
7430
7431 return FALSE;
7432 }
7433
7434 static bfd_boolean
7435 resolve_section (const char *name,
7436 asection *sections,
7437 bfd_vma *result)
7438 {
7439 asection *curr;
7440 unsigned int len;
7441
7442 for (curr = sections; curr; curr = curr->next)
7443 if (strcmp (curr->name, name) == 0)
7444 {
7445 *result = curr->vma;
7446 return TRUE;
7447 }
7448
7449 /* Hmm. still haven't found it. try pseudo-section names. */
7450 for (curr = sections; curr; curr = curr->next)
7451 {
7452 len = strlen (curr->name);
7453 if (len > strlen (name))
7454 continue;
7455
7456 if (strncmp (curr->name, name, len) == 0)
7457 {
7458 if (strncmp (".end", name + len, 4) == 0)
7459 {
7460 *result = curr->vma + curr->size;
7461 return TRUE;
7462 }
7463
7464 /* Insert more pseudo-section names here, if you like. */
7465 }
7466 }
7467
7468 return FALSE;
7469 }
7470
7471 static void
7472 undefined_reference (const char *reftype, const char *name)
7473 {
7474 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7475 reftype, name);
7476 }
7477
7478 static bfd_boolean
7479 eval_symbol (bfd_vma *result,
7480 const char **symp,
7481 bfd *input_bfd,
7482 struct elf_final_link_info *finfo,
7483 bfd_vma dot,
7484 Elf_Internal_Sym *isymbuf,
7485 size_t locsymcount,
7486 int signed_p)
7487 {
7488 size_t len;
7489 size_t symlen;
7490 bfd_vma a;
7491 bfd_vma b;
7492 char symbuf[4096];
7493 const char *sym = *symp;
7494 const char *symend;
7495 bfd_boolean symbol_is_section = FALSE;
7496
7497 len = strlen (sym);
7498 symend = sym + len;
7499
7500 if (len < 1 || len > sizeof (symbuf))
7501 {
7502 bfd_set_error (bfd_error_invalid_operation);
7503 return FALSE;
7504 }
7505
7506 switch (* sym)
7507 {
7508 case '.':
7509 *result = dot;
7510 *symp = sym + 1;
7511 return TRUE;
7512
7513 case '#':
7514 ++sym;
7515 *result = strtoul (sym, (char **) symp, 16);
7516 return TRUE;
7517
7518 case 'S':
7519 symbol_is_section = TRUE;
7520 case 's':
7521 ++sym;
7522 symlen = strtol (sym, (char **) symp, 10);
7523 sym = *symp + 1; /* Skip the trailing ':'. */
7524
7525 if (symend < sym || symlen + 1 > sizeof (symbuf))
7526 {
7527 bfd_set_error (bfd_error_invalid_operation);
7528 return FALSE;
7529 }
7530
7531 memcpy (symbuf, sym, symlen);
7532 symbuf[symlen] = '\0';
7533 *symp = sym + symlen;
7534
7535 /* Is it always possible, with complex symbols, that gas "mis-guessed"
7536 the symbol as a section, or vice-versa. so we're pretty liberal in our
7537 interpretation here; section means "try section first", not "must be a
7538 section", and likewise with symbol. */
7539
7540 if (symbol_is_section)
7541 {
7542 if (!resolve_section (symbuf, finfo->output_bfd->sections, result)
7543 && !resolve_symbol (symbuf, input_bfd, finfo, result,
7544 isymbuf, locsymcount))
7545 {
7546 undefined_reference ("section", symbuf);
7547 return FALSE;
7548 }
7549 }
7550 else
7551 {
7552 if (!resolve_symbol (symbuf, input_bfd, finfo, result,
7553 isymbuf, locsymcount)
7554 && !resolve_section (symbuf, finfo->output_bfd->sections,
7555 result))
7556 {
7557 undefined_reference ("symbol", symbuf);
7558 return FALSE;
7559 }
7560 }
7561
7562 return TRUE;
7563
7564 /* All that remains are operators. */
7565
7566 #define UNARY_OP(op) \
7567 if (strncmp (sym, #op, strlen (#op)) == 0) \
7568 { \
7569 sym += strlen (#op); \
7570 if (*sym == ':') \
7571 ++sym; \
7572 *symp = sym; \
7573 if (!eval_symbol (&a, symp, input_bfd, finfo, dot, \
7574 isymbuf, locsymcount, signed_p)) \
7575 return FALSE; \
7576 if (signed_p) \
7577 *result = op ((bfd_signed_vma) a); \
7578 else \
7579 *result = op a; \
7580 return TRUE; \
7581 }
7582
7583 #define BINARY_OP(op) \
7584 if (strncmp (sym, #op, strlen (#op)) == 0) \
7585 { \
7586 sym += strlen (#op); \
7587 if (*sym == ':') \
7588 ++sym; \
7589 *symp = sym; \
7590 if (!eval_symbol (&a, symp, input_bfd, finfo, dot, \
7591 isymbuf, locsymcount, signed_p)) \
7592 return FALSE; \
7593 ++*symp; \
7594 if (!eval_symbol (&b, symp, input_bfd, finfo, dot, \
7595 isymbuf, locsymcount, signed_p)) \
7596 return FALSE; \
7597 if (signed_p) \
7598 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
7599 else \
7600 *result = a op b; \
7601 return TRUE; \
7602 }
7603
7604 default:
7605 UNARY_OP (0-);
7606 BINARY_OP (<<);
7607 BINARY_OP (>>);
7608 BINARY_OP (==);
7609 BINARY_OP (!=);
7610 BINARY_OP (<=);
7611 BINARY_OP (>=);
7612 BINARY_OP (&&);
7613 BINARY_OP (||);
7614 UNARY_OP (~);
7615 UNARY_OP (!);
7616 BINARY_OP (*);
7617 BINARY_OP (/);
7618 BINARY_OP (%);
7619 BINARY_OP (^);
7620 BINARY_OP (|);
7621 BINARY_OP (&);
7622 BINARY_OP (+);
7623 BINARY_OP (-);
7624 BINARY_OP (<);
7625 BINARY_OP (>);
7626 #undef UNARY_OP
7627 #undef BINARY_OP
7628 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
7629 bfd_set_error (bfd_error_invalid_operation);
7630 return FALSE;
7631 }
7632 }
7633
7634 static void
7635 put_value (bfd_vma size,
7636 unsigned long chunksz,
7637 bfd *input_bfd,
7638 bfd_vma x,
7639 bfd_byte *location)
7640 {
7641 location += (size - chunksz);
7642
7643 for (; size; size -= chunksz, location -= chunksz, x >>= (chunksz * 8))
7644 {
7645 switch (chunksz)
7646 {
7647 default:
7648 case 0:
7649 abort ();
7650 case 1:
7651 bfd_put_8 (input_bfd, x, location);
7652 break;
7653 case 2:
7654 bfd_put_16 (input_bfd, x, location);
7655 break;
7656 case 4:
7657 bfd_put_32 (input_bfd, x, location);
7658 break;
7659 case 8:
7660 #ifdef BFD64
7661 bfd_put_64 (input_bfd, x, location);
7662 #else
7663 abort ();
7664 #endif
7665 break;
7666 }
7667 }
7668 }
7669
7670 static bfd_vma
7671 get_value (bfd_vma size,
7672 unsigned long chunksz,
7673 bfd *input_bfd,
7674 bfd_byte *location)
7675 {
7676 bfd_vma x = 0;
7677
7678 for (; size; size -= chunksz, location += chunksz)
7679 {
7680 switch (chunksz)
7681 {
7682 default:
7683 case 0:
7684 abort ();
7685 case 1:
7686 x = (x << (8 * chunksz)) | bfd_get_8 (input_bfd, location);
7687 break;
7688 case 2:
7689 x = (x << (8 * chunksz)) | bfd_get_16 (input_bfd, location);
7690 break;
7691 case 4:
7692 x = (x << (8 * chunksz)) | bfd_get_32 (input_bfd, location);
7693 break;
7694 case 8:
7695 #ifdef BFD64
7696 x = (x << (8 * chunksz)) | bfd_get_64 (input_bfd, location);
7697 #else
7698 abort ();
7699 #endif
7700 break;
7701 }
7702 }
7703 return x;
7704 }
7705
7706 static void
7707 decode_complex_addend (unsigned long *start, /* in bits */
7708 unsigned long *oplen, /* in bits */
7709 unsigned long *len, /* in bits */
7710 unsigned long *wordsz, /* in bytes */
7711 unsigned long *chunksz, /* in bytes */
7712 unsigned long *lsb0_p,
7713 unsigned long *signed_p,
7714 unsigned long *trunc_p,
7715 unsigned long encoded)
7716 {
7717 * start = encoded & 0x3F;
7718 * len = (encoded >> 6) & 0x3F;
7719 * oplen = (encoded >> 12) & 0x3F;
7720 * wordsz = (encoded >> 18) & 0xF;
7721 * chunksz = (encoded >> 22) & 0xF;
7722 * lsb0_p = (encoded >> 27) & 1;
7723 * signed_p = (encoded >> 28) & 1;
7724 * trunc_p = (encoded >> 29) & 1;
7725 }
7726
7727 bfd_reloc_status_type
7728 bfd_elf_perform_complex_relocation (bfd *input_bfd,
7729 asection *input_section ATTRIBUTE_UNUSED,
7730 bfd_byte *contents,
7731 Elf_Internal_Rela *rel,
7732 bfd_vma relocation)
7733 {
7734 bfd_vma shift, x, mask;
7735 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
7736 bfd_reloc_status_type r;
7737
7738 /* Perform this reloc, since it is complex.
7739 (this is not to say that it necessarily refers to a complex
7740 symbol; merely that it is a self-describing CGEN based reloc.
7741 i.e. the addend has the complete reloc information (bit start, end,
7742 word size, etc) encoded within it.). */
7743
7744 decode_complex_addend (&start, &oplen, &len, &wordsz,
7745 &chunksz, &lsb0_p, &signed_p,
7746 &trunc_p, rel->r_addend);
7747
7748 mask = (((1L << (len - 1)) - 1) << 1) | 1;
7749
7750 if (lsb0_p)
7751 shift = (start + 1) - len;
7752 else
7753 shift = (8 * wordsz) - (start + len);
7754
7755 x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
7756
7757 #ifdef DEBUG
7758 printf ("Doing complex reloc: "
7759 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
7760 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
7761 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
7762 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
7763 oplen, x, mask, relocation);
7764 #endif
7765
7766 r = bfd_reloc_ok;
7767 if (! trunc_p)
7768 /* Now do an overflow check. */
7769 r = bfd_check_overflow ((signed_p
7770 ? complain_overflow_signed
7771 : complain_overflow_unsigned),
7772 len, 0, (8 * wordsz),
7773 relocation);
7774
7775 /* Do the deed. */
7776 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
7777
7778 #ifdef DEBUG
7779 printf (" relocation: %8.8lx\n"
7780 " shifted mask: %8.8lx\n"
7781 " shifted/masked reloc: %8.8lx\n"
7782 " result: %8.8lx\n",
7783 relocation, (mask << shift),
7784 ((relocation & mask) << shift), x);
7785 #endif
7786 put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
7787 return r;
7788 }
7789
7790 /* When performing a relocatable link, the input relocations are
7791 preserved. But, if they reference global symbols, the indices
7792 referenced must be updated. Update all the relocations in
7793 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
7794
7795 static void
7796 elf_link_adjust_relocs (bfd *abfd,
7797 Elf_Internal_Shdr *rel_hdr,
7798 unsigned int count,
7799 struct elf_link_hash_entry **rel_hash)
7800 {
7801 unsigned int i;
7802 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7803 bfd_byte *erela;
7804 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
7805 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
7806 bfd_vma r_type_mask;
7807 int r_sym_shift;
7808
7809 if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
7810 {
7811 swap_in = bed->s->swap_reloc_in;
7812 swap_out = bed->s->swap_reloc_out;
7813 }
7814 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
7815 {
7816 swap_in = bed->s->swap_reloca_in;
7817 swap_out = bed->s->swap_reloca_out;
7818 }
7819 else
7820 abort ();
7821
7822 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
7823 abort ();
7824
7825 if (bed->s->arch_size == 32)
7826 {
7827 r_type_mask = 0xff;
7828 r_sym_shift = 8;
7829 }
7830 else
7831 {
7832 r_type_mask = 0xffffffff;
7833 r_sym_shift = 32;
7834 }
7835
7836 erela = rel_hdr->contents;
7837 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
7838 {
7839 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
7840 unsigned int j;
7841
7842 if (*rel_hash == NULL)
7843 continue;
7844
7845 BFD_ASSERT ((*rel_hash)->indx >= 0);
7846
7847 (*swap_in) (abfd, erela, irela);
7848 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
7849 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
7850 | (irela[j].r_info & r_type_mask));
7851 (*swap_out) (abfd, irela, erela);
7852 }
7853 }
7854
7855 struct elf_link_sort_rela
7856 {
7857 union {
7858 bfd_vma offset;
7859 bfd_vma sym_mask;
7860 } u;
7861 enum elf_reloc_type_class type;
7862 /* We use this as an array of size int_rels_per_ext_rel. */
7863 Elf_Internal_Rela rela[1];
7864 };
7865
7866 static int
7867 elf_link_sort_cmp1 (const void *A, const void *B)
7868 {
7869 const struct elf_link_sort_rela *a = A;
7870 const struct elf_link_sort_rela *b = B;
7871 int relativea, relativeb;
7872
7873 relativea = a->type == reloc_class_relative;
7874 relativeb = b->type == reloc_class_relative;
7875
7876 if (relativea < relativeb)
7877 return 1;
7878 if (relativea > relativeb)
7879 return -1;
7880 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
7881 return -1;
7882 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
7883 return 1;
7884 if (a->rela->r_offset < b->rela->r_offset)
7885 return -1;
7886 if (a->rela->r_offset > b->rela->r_offset)
7887 return 1;
7888 return 0;
7889 }
7890
7891 static int
7892 elf_link_sort_cmp2 (const void *A, const void *B)
7893 {
7894 const struct elf_link_sort_rela *a = A;
7895 const struct elf_link_sort_rela *b = B;
7896 int copya, copyb;
7897
7898 if (a->u.offset < b->u.offset)
7899 return -1;
7900 if (a->u.offset > b->u.offset)
7901 return 1;
7902 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
7903 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
7904 if (copya < copyb)
7905 return -1;
7906 if (copya > copyb)
7907 return 1;
7908 if (a->rela->r_offset < b->rela->r_offset)
7909 return -1;
7910 if (a->rela->r_offset > b->rela->r_offset)
7911 return 1;
7912 return 0;
7913 }
7914
7915 static size_t
7916 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
7917 {
7918 asection *dynamic_relocs;
7919 asection *rela_dyn;
7920 asection *rel_dyn;
7921 bfd_size_type count, size;
7922 size_t i, ret, sort_elt, ext_size;
7923 bfd_byte *sort, *s_non_relative, *p;
7924 struct elf_link_sort_rela *sq;
7925 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7926 int i2e = bed->s->int_rels_per_ext_rel;
7927 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
7928 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
7929 struct bfd_link_order *lo;
7930 bfd_vma r_sym_mask;
7931 bfd_boolean use_rela;
7932
7933 /* Find a dynamic reloc section. */
7934 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
7935 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
7936 if (rela_dyn != NULL && rela_dyn->size > 0
7937 && rel_dyn != NULL && rel_dyn->size > 0)
7938 {
7939 bfd_boolean use_rela_initialised = FALSE;
7940
7941 /* This is just here to stop gcc from complaining.
7942 It's initialization checking code is not perfect. */
7943 use_rela = TRUE;
7944
7945 /* Both sections are present. Examine the sizes
7946 of the indirect sections to help us choose. */
7947 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
7948 if (lo->type == bfd_indirect_link_order)
7949 {
7950 asection *o = lo->u.indirect.section;
7951
7952 if ((o->size % bed->s->sizeof_rela) == 0)
7953 {
7954 if ((o->size % bed->s->sizeof_rel) == 0)
7955 /* Section size is divisible by both rel and rela sizes.
7956 It is of no help to us. */
7957 ;
7958 else
7959 {
7960 /* Section size is only divisible by rela. */
7961 if (use_rela_initialised && (use_rela == FALSE))
7962 {
7963 _bfd_error_handler
7964 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
7965 bfd_set_error (bfd_error_invalid_operation);
7966 return 0;
7967 }
7968 else
7969 {
7970 use_rela = TRUE;
7971 use_rela_initialised = TRUE;
7972 }
7973 }
7974 }
7975 else if ((o->size % bed->s->sizeof_rel) == 0)
7976 {
7977 /* Section size is only divisible by rel. */
7978 if (use_rela_initialised && (use_rela == TRUE))
7979 {
7980 _bfd_error_handler
7981 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
7982 bfd_set_error (bfd_error_invalid_operation);
7983 return 0;
7984 }
7985 else
7986 {
7987 use_rela = FALSE;
7988 use_rela_initialised = TRUE;
7989 }
7990 }
7991 else
7992 {
7993 /* The section size is not divisible by either - something is wrong. */
7994 _bfd_error_handler
7995 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
7996 bfd_set_error (bfd_error_invalid_operation);
7997 return 0;
7998 }
7999 }
8000
8001 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8002 if (lo->type == bfd_indirect_link_order)
8003 {
8004 asection *o = lo->u.indirect.section;
8005
8006 if ((o->size % bed->s->sizeof_rela) == 0)
8007 {
8008 if ((o->size % bed->s->sizeof_rel) == 0)
8009 /* Section size is divisible by both rel and rela sizes.
8010 It is of no help to us. */
8011 ;
8012 else
8013 {
8014 /* Section size is only divisible by rela. */
8015 if (use_rela_initialised && (use_rela == FALSE))
8016 {
8017 _bfd_error_handler
8018 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8019 bfd_set_error (bfd_error_invalid_operation);
8020 return 0;
8021 }
8022 else
8023 {
8024 use_rela = TRUE;
8025 use_rela_initialised = TRUE;
8026 }
8027 }
8028 }
8029 else if ((o->size % bed->s->sizeof_rel) == 0)
8030 {
8031 /* Section size is only divisible by rel. */
8032 if (use_rela_initialised && (use_rela == TRUE))
8033 {
8034 _bfd_error_handler
8035 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8036 bfd_set_error (bfd_error_invalid_operation);
8037 return 0;
8038 }
8039 else
8040 {
8041 use_rela = FALSE;
8042 use_rela_initialised = TRUE;
8043 }
8044 }
8045 else
8046 {
8047 /* The section size is not divisible by either - something is wrong. */
8048 _bfd_error_handler
8049 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8050 bfd_set_error (bfd_error_invalid_operation);
8051 return 0;
8052 }
8053 }
8054
8055 if (! use_rela_initialised)
8056 /* Make a guess. */
8057 use_rela = TRUE;
8058 }
8059 else if (rela_dyn != NULL && rela_dyn->size > 0)
8060 use_rela = TRUE;
8061 else if (rel_dyn != NULL && rel_dyn->size > 0)
8062 use_rela = FALSE;
8063 else
8064 return 0;
8065
8066 if (use_rela)
8067 {
8068 dynamic_relocs = rela_dyn;
8069 ext_size = bed->s->sizeof_rela;
8070 swap_in = bed->s->swap_reloca_in;
8071 swap_out = bed->s->swap_reloca_out;
8072 }
8073 else
8074 {
8075 dynamic_relocs = rel_dyn;
8076 ext_size = bed->s->sizeof_rel;
8077 swap_in = bed->s->swap_reloc_in;
8078 swap_out = bed->s->swap_reloc_out;
8079 }
8080
8081 size = 0;
8082 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8083 if (lo->type == bfd_indirect_link_order)
8084 size += lo->u.indirect.section->size;
8085
8086 if (size != dynamic_relocs->size)
8087 return 0;
8088
8089 sort_elt = (sizeof (struct elf_link_sort_rela)
8090 + (i2e - 1) * sizeof (Elf_Internal_Rela));
8091
8092 count = dynamic_relocs->size / ext_size;
8093 sort = bfd_zmalloc (sort_elt * count);
8094
8095 if (sort == NULL)
8096 {
8097 (*info->callbacks->warning)
8098 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8099 return 0;
8100 }
8101
8102 if (bed->s->arch_size == 32)
8103 r_sym_mask = ~(bfd_vma) 0xff;
8104 else
8105 r_sym_mask = ~(bfd_vma) 0xffffffff;
8106
8107 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8108 if (lo->type == bfd_indirect_link_order)
8109 {
8110 bfd_byte *erel, *erelend;
8111 asection *o = lo->u.indirect.section;
8112
8113 if (o->contents == NULL && o->size != 0)
8114 {
8115 /* This is a reloc section that is being handled as a normal
8116 section. See bfd_section_from_shdr. We can't combine
8117 relocs in this case. */
8118 free (sort);
8119 return 0;
8120 }
8121 erel = o->contents;
8122 erelend = o->contents + o->size;
8123 p = sort + o->output_offset / ext_size * sort_elt;
8124
8125 while (erel < erelend)
8126 {
8127 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8128
8129 (*swap_in) (abfd, erel, s->rela);
8130 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
8131 s->u.sym_mask = r_sym_mask;
8132 p += sort_elt;
8133 erel += ext_size;
8134 }
8135 }
8136
8137 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8138
8139 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8140 {
8141 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8142 if (s->type != reloc_class_relative)
8143 break;
8144 }
8145 ret = i;
8146 s_non_relative = p;
8147
8148 sq = (struct elf_link_sort_rela *) s_non_relative;
8149 for (; i < count; i++, p += sort_elt)
8150 {
8151 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8152 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8153 sq = sp;
8154 sp->u.offset = sq->rela->r_offset;
8155 }
8156
8157 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8158
8159 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8160 if (lo->type == bfd_indirect_link_order)
8161 {
8162 bfd_byte *erel, *erelend;
8163 asection *o = lo->u.indirect.section;
8164
8165 erel = o->contents;
8166 erelend = o->contents + o->size;
8167 p = sort + o->output_offset / ext_size * sort_elt;
8168 while (erel < erelend)
8169 {
8170 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8171 (*swap_out) (abfd, s->rela, erel);
8172 p += sort_elt;
8173 erel += ext_size;
8174 }
8175 }
8176
8177 free (sort);
8178 *psec = dynamic_relocs;
8179 return ret;
8180 }
8181
8182 /* Flush the output symbols to the file. */
8183
8184 static bfd_boolean
8185 elf_link_flush_output_syms (struct elf_final_link_info *finfo,
8186 const struct elf_backend_data *bed)
8187 {
8188 if (finfo->symbuf_count > 0)
8189 {
8190 Elf_Internal_Shdr *hdr;
8191 file_ptr pos;
8192 bfd_size_type amt;
8193
8194 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
8195 pos = hdr->sh_offset + hdr->sh_size;
8196 amt = finfo->symbuf_count * bed->s->sizeof_sym;
8197 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
8198 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
8199 return FALSE;
8200
8201 hdr->sh_size += amt;
8202 finfo->symbuf_count = 0;
8203 }
8204
8205 return TRUE;
8206 }
8207
8208 /* Add a symbol to the output symbol table. */
8209
8210 static bfd_boolean
8211 elf_link_output_sym (struct elf_final_link_info *finfo,
8212 const char *name,
8213 Elf_Internal_Sym *elfsym,
8214 asection *input_sec,
8215 struct elf_link_hash_entry *h)
8216 {
8217 bfd_byte *dest;
8218 Elf_External_Sym_Shndx *destshndx;
8219 bfd_boolean (*output_symbol_hook)
8220 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8221 struct elf_link_hash_entry *);
8222 const struct elf_backend_data *bed;
8223
8224 bed = get_elf_backend_data (finfo->output_bfd);
8225 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8226 if (output_symbol_hook != NULL)
8227 {
8228 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
8229 return FALSE;
8230 }
8231
8232 if (name == NULL || *name == '\0')
8233 elfsym->st_name = 0;
8234 else if (input_sec->flags & SEC_EXCLUDE)
8235 elfsym->st_name = 0;
8236 else
8237 {
8238 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
8239 name, TRUE, FALSE);
8240 if (elfsym->st_name == (unsigned long) -1)
8241 return FALSE;
8242 }
8243
8244 if (finfo->symbuf_count >= finfo->symbuf_size)
8245 {
8246 if (! elf_link_flush_output_syms (finfo, bed))
8247 return FALSE;
8248 }
8249
8250 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
8251 destshndx = finfo->symshndxbuf;
8252 if (destshndx != NULL)
8253 {
8254 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
8255 {
8256 bfd_size_type amt;
8257
8258 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
8259 destshndx = bfd_realloc (destshndx, amt * 2);
8260 if (destshndx == NULL)
8261 return FALSE;
8262 finfo->symshndxbuf = destshndx;
8263 memset ((char *) destshndx + amt, 0, amt);
8264 finfo->shndxbuf_size *= 2;
8265 }
8266 destshndx += bfd_get_symcount (finfo->output_bfd);
8267 }
8268
8269 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
8270 finfo->symbuf_count += 1;
8271 bfd_get_symcount (finfo->output_bfd) += 1;
8272
8273 return TRUE;
8274 }
8275
8276 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8277
8278 static bfd_boolean
8279 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
8280 {
8281 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
8282 && sym->st_shndx < SHN_LORESERVE)
8283 {
8284 /* The gABI doesn't support dynamic symbols in output sections
8285 beyond 64k. */
8286 (*_bfd_error_handler)
8287 (_("%B: Too many sections: %d (>= %d)"),
8288 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
8289 bfd_set_error (bfd_error_nonrepresentable_section);
8290 return FALSE;
8291 }
8292 return TRUE;
8293 }
8294
8295 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8296 allowing an unsatisfied unversioned symbol in the DSO to match a
8297 versioned symbol that would normally require an explicit version.
8298 We also handle the case that a DSO references a hidden symbol
8299 which may be satisfied by a versioned symbol in another DSO. */
8300
8301 static bfd_boolean
8302 elf_link_check_versioned_symbol (struct bfd_link_info *info,
8303 const struct elf_backend_data *bed,
8304 struct elf_link_hash_entry *h)
8305 {
8306 bfd *abfd;
8307 struct elf_link_loaded_list *loaded;
8308
8309 if (!is_elf_hash_table (info->hash))
8310 return FALSE;
8311
8312 switch (h->root.type)
8313 {
8314 default:
8315 abfd = NULL;
8316 break;
8317
8318 case bfd_link_hash_undefined:
8319 case bfd_link_hash_undefweak:
8320 abfd = h->root.u.undef.abfd;
8321 if ((abfd->flags & DYNAMIC) == 0
8322 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
8323 return FALSE;
8324 break;
8325
8326 case bfd_link_hash_defined:
8327 case bfd_link_hash_defweak:
8328 abfd = h->root.u.def.section->owner;
8329 break;
8330
8331 case bfd_link_hash_common:
8332 abfd = h->root.u.c.p->section->owner;
8333 break;
8334 }
8335 BFD_ASSERT (abfd != NULL);
8336
8337 for (loaded = elf_hash_table (info)->loaded;
8338 loaded != NULL;
8339 loaded = loaded->next)
8340 {
8341 bfd *input;
8342 Elf_Internal_Shdr *hdr;
8343 bfd_size_type symcount;
8344 bfd_size_type extsymcount;
8345 bfd_size_type extsymoff;
8346 Elf_Internal_Shdr *versymhdr;
8347 Elf_Internal_Sym *isym;
8348 Elf_Internal_Sym *isymend;
8349 Elf_Internal_Sym *isymbuf;
8350 Elf_External_Versym *ever;
8351 Elf_External_Versym *extversym;
8352
8353 input = loaded->abfd;
8354
8355 /* We check each DSO for a possible hidden versioned definition. */
8356 if (input == abfd
8357 || (input->flags & DYNAMIC) == 0
8358 || elf_dynversym (input) == 0)
8359 continue;
8360
8361 hdr = &elf_tdata (input)->dynsymtab_hdr;
8362
8363 symcount = hdr->sh_size / bed->s->sizeof_sym;
8364 if (elf_bad_symtab (input))
8365 {
8366 extsymcount = symcount;
8367 extsymoff = 0;
8368 }
8369 else
8370 {
8371 extsymcount = symcount - hdr->sh_info;
8372 extsymoff = hdr->sh_info;
8373 }
8374
8375 if (extsymcount == 0)
8376 continue;
8377
8378 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
8379 NULL, NULL, NULL);
8380 if (isymbuf == NULL)
8381 return FALSE;
8382
8383 /* Read in any version definitions. */
8384 versymhdr = &elf_tdata (input)->dynversym_hdr;
8385 extversym = bfd_malloc (versymhdr->sh_size);
8386 if (extversym == NULL)
8387 goto error_ret;
8388
8389 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
8390 || (bfd_bread (extversym, versymhdr->sh_size, input)
8391 != versymhdr->sh_size))
8392 {
8393 free (extversym);
8394 error_ret:
8395 free (isymbuf);
8396 return FALSE;
8397 }
8398
8399 ever = extversym + extsymoff;
8400 isymend = isymbuf + extsymcount;
8401 for (isym = isymbuf; isym < isymend; isym++, ever++)
8402 {
8403 const char *name;
8404 Elf_Internal_Versym iver;
8405 unsigned short version_index;
8406
8407 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
8408 || isym->st_shndx == SHN_UNDEF)
8409 continue;
8410
8411 name = bfd_elf_string_from_elf_section (input,
8412 hdr->sh_link,
8413 isym->st_name);
8414 if (strcmp (name, h->root.root.string) != 0)
8415 continue;
8416
8417 _bfd_elf_swap_versym_in (input, ever, &iver);
8418
8419 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
8420 {
8421 /* If we have a non-hidden versioned sym, then it should
8422 have provided a definition for the undefined sym. */
8423 abort ();
8424 }
8425
8426 version_index = iver.vs_vers & VERSYM_VERSION;
8427 if (version_index == 1 || version_index == 2)
8428 {
8429 /* This is the base or first version. We can use it. */
8430 free (extversym);
8431 free (isymbuf);
8432 return TRUE;
8433 }
8434 }
8435
8436 free (extversym);
8437 free (isymbuf);
8438 }
8439
8440 return FALSE;
8441 }
8442
8443 /* Add an external symbol to the symbol table. This is called from
8444 the hash table traversal routine. When generating a shared object,
8445 we go through the symbol table twice. The first time we output
8446 anything that might have been forced to local scope in a version
8447 script. The second time we output the symbols that are still
8448 global symbols. */
8449
8450 static bfd_boolean
8451 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
8452 {
8453 struct elf_outext_info *eoinfo = data;
8454 struct elf_final_link_info *finfo = eoinfo->finfo;
8455 bfd_boolean strip;
8456 Elf_Internal_Sym sym;
8457 asection *input_sec;
8458 const struct elf_backend_data *bed;
8459
8460 if (h->root.type == bfd_link_hash_warning)
8461 {
8462 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8463 if (h->root.type == bfd_link_hash_new)
8464 return TRUE;
8465 }
8466
8467 /* Decide whether to output this symbol in this pass. */
8468 if (eoinfo->localsyms)
8469 {
8470 if (!h->forced_local)
8471 return TRUE;
8472 }
8473 else
8474 {
8475 if (h->forced_local)
8476 return TRUE;
8477 }
8478
8479 bed = get_elf_backend_data (finfo->output_bfd);
8480
8481 if (h->root.type == bfd_link_hash_undefined)
8482 {
8483 /* If we have an undefined symbol reference here then it must have
8484 come from a shared library that is being linked in. (Undefined
8485 references in regular files have already been handled). */
8486 bfd_boolean ignore_undef = FALSE;
8487
8488 /* Some symbols may be special in that the fact that they're
8489 undefined can be safely ignored - let backend determine that. */
8490 if (bed->elf_backend_ignore_undef_symbol)
8491 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
8492
8493 /* If we are reporting errors for this situation then do so now. */
8494 if (ignore_undef == FALSE
8495 && h->ref_dynamic
8496 && ! h->ref_regular
8497 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
8498 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
8499 {
8500 if (! (finfo->info->callbacks->undefined_symbol
8501 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
8502 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
8503 {
8504 eoinfo->failed = TRUE;
8505 return FALSE;
8506 }
8507 }
8508 }
8509
8510 /* We should also warn if a forced local symbol is referenced from
8511 shared libraries. */
8512 if (! finfo->info->relocatable
8513 && (! finfo->info->shared)
8514 && h->forced_local
8515 && h->ref_dynamic
8516 && !h->dynamic_def
8517 && !h->dynamic_weak
8518 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
8519 {
8520 (*_bfd_error_handler)
8521 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
8522 finfo->output_bfd,
8523 h->root.u.def.section == bfd_abs_section_ptr
8524 ? finfo->output_bfd : h->root.u.def.section->owner,
8525 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
8526 ? "internal"
8527 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
8528 ? "hidden" : "local",
8529 h->root.root.string);
8530 eoinfo->failed = TRUE;
8531 return FALSE;
8532 }
8533
8534 /* We don't want to output symbols that have never been mentioned by
8535 a regular file, or that we have been told to strip. However, if
8536 h->indx is set to -2, the symbol is used by a reloc and we must
8537 output it. */
8538 if (h->indx == -2)
8539 strip = FALSE;
8540 else if ((h->def_dynamic
8541 || h->ref_dynamic
8542 || h->root.type == bfd_link_hash_new)
8543 && !h->def_regular
8544 && !h->ref_regular)
8545 strip = TRUE;
8546 else if (finfo->info->strip == strip_all)
8547 strip = TRUE;
8548 else if (finfo->info->strip == strip_some
8549 && bfd_hash_lookup (finfo->info->keep_hash,
8550 h->root.root.string, FALSE, FALSE) == NULL)
8551 strip = TRUE;
8552 else if (finfo->info->strip_discarded
8553 && (h->root.type == bfd_link_hash_defined
8554 || h->root.type == bfd_link_hash_defweak)
8555 && elf_discarded_section (h->root.u.def.section))
8556 strip = TRUE;
8557 else
8558 strip = FALSE;
8559
8560 /* If we're stripping it, and it's not a dynamic symbol, there's
8561 nothing else to do unless it is a forced local symbol. */
8562 if (strip
8563 && h->dynindx == -1
8564 && !h->forced_local)
8565 return TRUE;
8566
8567 sym.st_value = 0;
8568 sym.st_size = h->size;
8569 sym.st_other = h->other;
8570 if (h->forced_local)
8571 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
8572 else if (h->root.type == bfd_link_hash_undefweak
8573 || h->root.type == bfd_link_hash_defweak)
8574 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
8575 else
8576 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
8577
8578 switch (h->root.type)
8579 {
8580 default:
8581 case bfd_link_hash_new:
8582 case bfd_link_hash_warning:
8583 abort ();
8584 return FALSE;
8585
8586 case bfd_link_hash_undefined:
8587 case bfd_link_hash_undefweak:
8588 input_sec = bfd_und_section_ptr;
8589 sym.st_shndx = SHN_UNDEF;
8590 break;
8591
8592 case bfd_link_hash_defined:
8593 case bfd_link_hash_defweak:
8594 {
8595 input_sec = h->root.u.def.section;
8596 if (input_sec->output_section != NULL)
8597 {
8598 sym.st_shndx =
8599 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
8600 input_sec->output_section);
8601 if (sym.st_shndx == SHN_BAD)
8602 {
8603 (*_bfd_error_handler)
8604 (_("%B: could not find output section %A for input section %A"),
8605 finfo->output_bfd, input_sec->output_section, input_sec);
8606 eoinfo->failed = TRUE;
8607 return FALSE;
8608 }
8609
8610 /* ELF symbols in relocatable files are section relative,
8611 but in nonrelocatable files they are virtual
8612 addresses. */
8613 sym.st_value = h->root.u.def.value + input_sec->output_offset;
8614 if (! finfo->info->relocatable)
8615 {
8616 sym.st_value += input_sec->output_section->vma;
8617 if (h->type == STT_TLS)
8618 {
8619 asection *tls_sec = elf_hash_table (finfo->info)->tls_sec;
8620 if (tls_sec != NULL)
8621 sym.st_value -= tls_sec->vma;
8622 else
8623 {
8624 /* The TLS section may have been garbage collected. */
8625 BFD_ASSERT (finfo->info->gc_sections
8626 && !input_sec->gc_mark);
8627 }
8628 }
8629 }
8630 }
8631 else
8632 {
8633 BFD_ASSERT (input_sec->owner == NULL
8634 || (input_sec->owner->flags & DYNAMIC) != 0);
8635 sym.st_shndx = SHN_UNDEF;
8636 input_sec = bfd_und_section_ptr;
8637 }
8638 }
8639 break;
8640
8641 case bfd_link_hash_common:
8642 input_sec = h->root.u.c.p->section;
8643 sym.st_shndx = bed->common_section_index (input_sec);
8644 sym.st_value = 1 << h->root.u.c.p->alignment_power;
8645 break;
8646
8647 case bfd_link_hash_indirect:
8648 /* These symbols are created by symbol versioning. They point
8649 to the decorated version of the name. For example, if the
8650 symbol foo@@GNU_1.2 is the default, which should be used when
8651 foo is used with no version, then we add an indirect symbol
8652 foo which points to foo@@GNU_1.2. We ignore these symbols,
8653 since the indirected symbol is already in the hash table. */
8654 return TRUE;
8655 }
8656
8657 /* Give the processor backend a chance to tweak the symbol value,
8658 and also to finish up anything that needs to be done for this
8659 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
8660 forced local syms when non-shared is due to a historical quirk. */
8661 if ((h->dynindx != -1
8662 || h->forced_local)
8663 && ((finfo->info->shared
8664 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8665 || h->root.type != bfd_link_hash_undefweak))
8666 || !h->forced_local)
8667 && elf_hash_table (finfo->info)->dynamic_sections_created)
8668 {
8669 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8670 (finfo->output_bfd, finfo->info, h, &sym)))
8671 {
8672 eoinfo->failed = TRUE;
8673 return FALSE;
8674 }
8675 }
8676
8677 /* If we are marking the symbol as undefined, and there are no
8678 non-weak references to this symbol from a regular object, then
8679 mark the symbol as weak undefined; if there are non-weak
8680 references, mark the symbol as strong. We can't do this earlier,
8681 because it might not be marked as undefined until the
8682 finish_dynamic_symbol routine gets through with it. */
8683 if (sym.st_shndx == SHN_UNDEF
8684 && h->ref_regular
8685 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
8686 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
8687 {
8688 int bindtype;
8689
8690 if (h->ref_regular_nonweak)
8691 bindtype = STB_GLOBAL;
8692 else
8693 bindtype = STB_WEAK;
8694 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
8695 }
8696
8697 /* If this is a symbol defined in a dynamic library, don't use the
8698 symbol size from the dynamic library. Relinking an executable
8699 against a new library may introduce gratuitous changes in the
8700 executable's symbols if we keep the size. */
8701 if (sym.st_shndx == SHN_UNDEF
8702 && !h->def_regular
8703 && h->def_dynamic)
8704 sym.st_size = 0;
8705
8706 /* If a non-weak symbol with non-default visibility is not defined
8707 locally, it is a fatal error. */
8708 if (! finfo->info->relocatable
8709 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
8710 && ELF_ST_BIND (sym.st_info) != STB_WEAK
8711 && h->root.type == bfd_link_hash_undefined
8712 && !h->def_regular)
8713 {
8714 (*_bfd_error_handler)
8715 (_("%B: %s symbol `%s' isn't defined"),
8716 finfo->output_bfd,
8717 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
8718 ? "protected"
8719 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
8720 ? "internal" : "hidden",
8721 h->root.root.string);
8722 eoinfo->failed = TRUE;
8723 return FALSE;
8724 }
8725
8726 /* If this symbol should be put in the .dynsym section, then put it
8727 there now. We already know the symbol index. We also fill in
8728 the entry in the .hash section. */
8729 if (h->dynindx != -1
8730 && elf_hash_table (finfo->info)->dynamic_sections_created)
8731 {
8732 bfd_byte *esym;
8733
8734 sym.st_name = h->dynstr_index;
8735 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
8736 if (! check_dynsym (finfo->output_bfd, &sym))
8737 {
8738 eoinfo->failed = TRUE;
8739 return FALSE;
8740 }
8741 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
8742
8743 if (finfo->hash_sec != NULL)
8744 {
8745 size_t hash_entry_size;
8746 bfd_byte *bucketpos;
8747 bfd_vma chain;
8748 size_t bucketcount;
8749 size_t bucket;
8750
8751 bucketcount = elf_hash_table (finfo->info)->bucketcount;
8752 bucket = h->u.elf_hash_value % bucketcount;
8753
8754 hash_entry_size
8755 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
8756 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
8757 + (bucket + 2) * hash_entry_size);
8758 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
8759 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
8760 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
8761 ((bfd_byte *) finfo->hash_sec->contents
8762 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
8763 }
8764
8765 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
8766 {
8767 Elf_Internal_Versym iversym;
8768 Elf_External_Versym *eversym;
8769
8770 if (!h->def_regular)
8771 {
8772 if (h->verinfo.verdef == NULL)
8773 iversym.vs_vers = 0;
8774 else
8775 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
8776 }
8777 else
8778 {
8779 if (h->verinfo.vertree == NULL)
8780 iversym.vs_vers = 1;
8781 else
8782 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8783 if (finfo->info->create_default_symver)
8784 iversym.vs_vers++;
8785 }
8786
8787 if (h->hidden)
8788 iversym.vs_vers |= VERSYM_HIDDEN;
8789
8790 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
8791 eversym += h->dynindx;
8792 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
8793 }
8794 }
8795
8796 /* If we're stripping it, then it was just a dynamic symbol, and
8797 there's nothing else to do. */
8798 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
8799 return TRUE;
8800
8801 h->indx = bfd_get_symcount (finfo->output_bfd);
8802
8803 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
8804 {
8805 eoinfo->failed = TRUE;
8806 return FALSE;
8807 }
8808
8809 return TRUE;
8810 }
8811
8812 /* Return TRUE if special handling is done for relocs in SEC against
8813 symbols defined in discarded sections. */
8814
8815 static bfd_boolean
8816 elf_section_ignore_discarded_relocs (asection *sec)
8817 {
8818 const struct elf_backend_data *bed;
8819
8820 switch (sec->sec_info_type)
8821 {
8822 case ELF_INFO_TYPE_STABS:
8823 case ELF_INFO_TYPE_EH_FRAME:
8824 return TRUE;
8825 default:
8826 break;
8827 }
8828
8829 bed = get_elf_backend_data (sec->owner);
8830 if (bed->elf_backend_ignore_discarded_relocs != NULL
8831 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
8832 return TRUE;
8833
8834 return FALSE;
8835 }
8836
8837 /* Return a mask saying how ld should treat relocations in SEC against
8838 symbols defined in discarded sections. If this function returns
8839 COMPLAIN set, ld will issue a warning message. If this function
8840 returns PRETEND set, and the discarded section was link-once and the
8841 same size as the kept link-once section, ld will pretend that the
8842 symbol was actually defined in the kept section. Otherwise ld will
8843 zero the reloc (at least that is the intent, but some cooperation by
8844 the target dependent code is needed, particularly for REL targets). */
8845
8846 unsigned int
8847 _bfd_elf_default_action_discarded (asection *sec)
8848 {
8849 if (sec->flags & SEC_DEBUGGING)
8850 return PRETEND;
8851
8852 if (strcmp (".eh_frame", sec->name) == 0)
8853 return 0;
8854
8855 if (strcmp (".gcc_except_table", sec->name) == 0)
8856 return 0;
8857
8858 return COMPLAIN | PRETEND;
8859 }
8860
8861 /* Find a match between a section and a member of a section group. */
8862
8863 static asection *
8864 match_group_member (asection *sec, asection *group,
8865 struct bfd_link_info *info)
8866 {
8867 asection *first = elf_next_in_group (group);
8868 asection *s = first;
8869
8870 while (s != NULL)
8871 {
8872 if (bfd_elf_match_symbols_in_sections (s, sec, info))
8873 return s;
8874
8875 s = elf_next_in_group (s);
8876 if (s == first)
8877 break;
8878 }
8879
8880 return NULL;
8881 }
8882
8883 /* Check if the kept section of a discarded section SEC can be used
8884 to replace it. Return the replacement if it is OK. Otherwise return
8885 NULL. */
8886
8887 asection *
8888 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
8889 {
8890 asection *kept;
8891
8892 kept = sec->kept_section;
8893 if (kept != NULL)
8894 {
8895 if ((kept->flags & SEC_GROUP) != 0)
8896 kept = match_group_member (sec, kept, info);
8897 if (kept != NULL
8898 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
8899 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
8900 kept = NULL;
8901 sec->kept_section = kept;
8902 }
8903 return kept;
8904 }
8905
8906 /* Link an input file into the linker output file. This function
8907 handles all the sections and relocations of the input file at once.
8908 This is so that we only have to read the local symbols once, and
8909 don't have to keep them in memory. */
8910
8911 static bfd_boolean
8912 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
8913 {
8914 int (*relocate_section)
8915 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
8916 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
8917 bfd *output_bfd;
8918 Elf_Internal_Shdr *symtab_hdr;
8919 size_t locsymcount;
8920 size_t extsymoff;
8921 Elf_Internal_Sym *isymbuf;
8922 Elf_Internal_Sym *isym;
8923 Elf_Internal_Sym *isymend;
8924 long *pindex;
8925 asection **ppsection;
8926 asection *o;
8927 const struct elf_backend_data *bed;
8928 struct elf_link_hash_entry **sym_hashes;
8929
8930 output_bfd = finfo->output_bfd;
8931 bed = get_elf_backend_data (output_bfd);
8932 relocate_section = bed->elf_backend_relocate_section;
8933
8934 /* If this is a dynamic object, we don't want to do anything here:
8935 we don't want the local symbols, and we don't want the section
8936 contents. */
8937 if ((input_bfd->flags & DYNAMIC) != 0)
8938 return TRUE;
8939
8940 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8941 if (elf_bad_symtab (input_bfd))
8942 {
8943 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
8944 extsymoff = 0;
8945 }
8946 else
8947 {
8948 locsymcount = symtab_hdr->sh_info;
8949 extsymoff = symtab_hdr->sh_info;
8950 }
8951
8952 /* Read the local symbols. */
8953 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
8954 if (isymbuf == NULL && locsymcount != 0)
8955 {
8956 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8957 finfo->internal_syms,
8958 finfo->external_syms,
8959 finfo->locsym_shndx);
8960 if (isymbuf == NULL)
8961 return FALSE;
8962 }
8963
8964 /* Find local symbol sections and adjust values of symbols in
8965 SEC_MERGE sections. Write out those local symbols we know are
8966 going into the output file. */
8967 isymend = isymbuf + locsymcount;
8968 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
8969 isym < isymend;
8970 isym++, pindex++, ppsection++)
8971 {
8972 asection *isec;
8973 const char *name;
8974 Elf_Internal_Sym osym;
8975
8976 *pindex = -1;
8977
8978 if (elf_bad_symtab (input_bfd))
8979 {
8980 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
8981 {
8982 *ppsection = NULL;
8983 continue;
8984 }
8985 }
8986
8987 if (isym->st_shndx == SHN_UNDEF)
8988 isec = bfd_und_section_ptr;
8989 else if (isym->st_shndx == SHN_ABS)
8990 isec = bfd_abs_section_ptr;
8991 else if (isym->st_shndx == SHN_COMMON)
8992 isec = bfd_com_section_ptr;
8993 else
8994 {
8995 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
8996 if (isec == NULL)
8997 {
8998 /* Don't attempt to output symbols with st_shnx in the
8999 reserved range other than SHN_ABS and SHN_COMMON. */
9000 *ppsection = NULL;
9001 continue;
9002 }
9003 else if (isec->sec_info_type == ELF_INFO_TYPE_MERGE
9004 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9005 isym->st_value =
9006 _bfd_merged_section_offset (output_bfd, &isec,
9007 elf_section_data (isec)->sec_info,
9008 isym->st_value);
9009 }
9010
9011 *ppsection = isec;
9012
9013 /* Don't output the first, undefined, symbol. */
9014 if (ppsection == finfo->sections)
9015 continue;
9016
9017 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9018 {
9019 /* We never output section symbols. Instead, we use the
9020 section symbol of the corresponding section in the output
9021 file. */
9022 continue;
9023 }
9024
9025 /* If we are stripping all symbols, we don't want to output this
9026 one. */
9027 if (finfo->info->strip == strip_all)
9028 continue;
9029
9030 /* If we are discarding all local symbols, we don't want to
9031 output this one. If we are generating a relocatable output
9032 file, then some of the local symbols may be required by
9033 relocs; we output them below as we discover that they are
9034 needed. */
9035 if (finfo->info->discard == discard_all)
9036 continue;
9037
9038 /* If this symbol is defined in a section which we are
9039 discarding, we don't need to keep it. */
9040 if (isym->st_shndx != SHN_UNDEF
9041 && isym->st_shndx < SHN_LORESERVE
9042 && bfd_section_removed_from_list (output_bfd,
9043 isec->output_section))
9044 continue;
9045
9046 /* Get the name of the symbol. */
9047 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9048 isym->st_name);
9049 if (name == NULL)
9050 return FALSE;
9051
9052 /* See if we are discarding symbols with this name. */
9053 if ((finfo->info->strip == strip_some
9054 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
9055 == NULL))
9056 || (((finfo->info->discard == discard_sec_merge
9057 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
9058 || finfo->info->discard == discard_l)
9059 && bfd_is_local_label_name (input_bfd, name)))
9060 continue;
9061
9062 /* If we get here, we are going to output this symbol. */
9063
9064 osym = *isym;
9065
9066 /* Adjust the section index for the output file. */
9067 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9068 isec->output_section);
9069 if (osym.st_shndx == SHN_BAD)
9070 return FALSE;
9071
9072 *pindex = bfd_get_symcount (output_bfd);
9073
9074 /* ELF symbols in relocatable files are section relative, but
9075 in executable files they are virtual addresses. Note that
9076 this code assumes that all ELF sections have an associated
9077 BFD section with a reasonable value for output_offset; below
9078 we assume that they also have a reasonable value for
9079 output_section. Any special sections must be set up to meet
9080 these requirements. */
9081 osym.st_value += isec->output_offset;
9082 if (! finfo->info->relocatable)
9083 {
9084 osym.st_value += isec->output_section->vma;
9085 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
9086 {
9087 /* STT_TLS symbols are relative to PT_TLS segment base. */
9088 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
9089 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
9090 }
9091 }
9092
9093 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
9094 return FALSE;
9095 }
9096
9097 /* Relocate the contents of each section. */
9098 sym_hashes = elf_sym_hashes (input_bfd);
9099 for (o = input_bfd->sections; o != NULL; o = o->next)
9100 {
9101 bfd_byte *contents;
9102
9103 if (! o->linker_mark)
9104 {
9105 /* This section was omitted from the link. */
9106 continue;
9107 }
9108
9109 if (finfo->info->relocatable
9110 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
9111 {
9112 /* Deal with the group signature symbol. */
9113 struct bfd_elf_section_data *sec_data = elf_section_data (o);
9114 unsigned long symndx = sec_data->this_hdr.sh_info;
9115 asection *osec = o->output_section;
9116
9117 if (symndx >= locsymcount
9118 || (elf_bad_symtab (input_bfd)
9119 && finfo->sections[symndx] == NULL))
9120 {
9121 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
9122 while (h->root.type == bfd_link_hash_indirect
9123 || h->root.type == bfd_link_hash_warning)
9124 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9125 /* Arrange for symbol to be output. */
9126 h->indx = -2;
9127 elf_section_data (osec)->this_hdr.sh_info = -2;
9128 }
9129 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
9130 {
9131 /* We'll use the output section target_index. */
9132 asection *sec = finfo->sections[symndx]->output_section;
9133 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
9134 }
9135 else
9136 {
9137 if (finfo->indices[symndx] == -1)
9138 {
9139 /* Otherwise output the local symbol now. */
9140 Elf_Internal_Sym sym = isymbuf[symndx];
9141 asection *sec = finfo->sections[symndx]->output_section;
9142 const char *name;
9143
9144 name = bfd_elf_string_from_elf_section (input_bfd,
9145 symtab_hdr->sh_link,
9146 sym.st_name);
9147 if (name == NULL)
9148 return FALSE;
9149
9150 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9151 sec);
9152 if (sym.st_shndx == SHN_BAD)
9153 return FALSE;
9154
9155 sym.st_value += o->output_offset;
9156
9157 finfo->indices[symndx] = bfd_get_symcount (output_bfd);
9158 if (! elf_link_output_sym (finfo, name, &sym, o, NULL))
9159 return FALSE;
9160 }
9161 elf_section_data (osec)->this_hdr.sh_info
9162 = finfo->indices[symndx];
9163 }
9164 }
9165
9166 if ((o->flags & SEC_HAS_CONTENTS) == 0
9167 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
9168 continue;
9169
9170 if ((o->flags & SEC_LINKER_CREATED) != 0)
9171 {
9172 /* Section was created by _bfd_elf_link_create_dynamic_sections
9173 or somesuch. */
9174 continue;
9175 }
9176
9177 /* Get the contents of the section. They have been cached by a
9178 relaxation routine. Note that o is a section in an input
9179 file, so the contents field will not have been set by any of
9180 the routines which work on output files. */
9181 if (elf_section_data (o)->this_hdr.contents != NULL)
9182 contents = elf_section_data (o)->this_hdr.contents;
9183 else
9184 {
9185 bfd_size_type amt = o->rawsize ? o->rawsize : o->size;
9186
9187 contents = finfo->contents;
9188 if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt))
9189 return FALSE;
9190 }
9191
9192 if ((o->flags & SEC_RELOC) != 0)
9193 {
9194 Elf_Internal_Rela *internal_relocs;
9195 Elf_Internal_Rela *rel, *relend;
9196 bfd_vma r_type_mask;
9197 int r_sym_shift;
9198 int action_discarded;
9199 int ret;
9200
9201 /* Get the swapped relocs. */
9202 internal_relocs
9203 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
9204 finfo->internal_relocs, FALSE);
9205 if (internal_relocs == NULL
9206 && o->reloc_count > 0)
9207 return FALSE;
9208
9209 if (bed->s->arch_size == 32)
9210 {
9211 r_type_mask = 0xff;
9212 r_sym_shift = 8;
9213 }
9214 else
9215 {
9216 r_type_mask = 0xffffffff;
9217 r_sym_shift = 32;
9218 }
9219
9220 action_discarded = -1;
9221 if (!elf_section_ignore_discarded_relocs (o))
9222 action_discarded = (*bed->action_discarded) (o);
9223
9224 /* Run through the relocs evaluating complex reloc symbols and
9225 looking for relocs against symbols from discarded sections
9226 or section symbols from removed link-once sections.
9227 Complain about relocs against discarded sections. Zero
9228 relocs against removed link-once sections. */
9229
9230 rel = internal_relocs;
9231 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
9232 for ( ; rel < relend; rel++)
9233 {
9234 unsigned long r_symndx = rel->r_info >> r_sym_shift;
9235 unsigned int s_type;
9236 asection **ps, *sec;
9237 struct elf_link_hash_entry *h = NULL;
9238 const char *sym_name;
9239
9240 if (r_symndx == STN_UNDEF)
9241 continue;
9242
9243 if (r_symndx >= locsymcount
9244 || (elf_bad_symtab (input_bfd)
9245 && finfo->sections[r_symndx] == NULL))
9246 {
9247 h = sym_hashes[r_symndx - extsymoff];
9248
9249 /* Badly formatted input files can contain relocs that
9250 reference non-existant symbols. Check here so that
9251 we do not seg fault. */
9252 if (h == NULL)
9253 {
9254 char buffer [32];
9255
9256 sprintf_vma (buffer, rel->r_info);
9257 (*_bfd_error_handler)
9258 (_("error: %B contains a reloc (0x%s) for section %A "
9259 "that references a non-existent global symbol"),
9260 input_bfd, o, buffer);
9261 bfd_set_error (bfd_error_bad_value);
9262 return FALSE;
9263 }
9264
9265 while (h->root.type == bfd_link_hash_indirect
9266 || h->root.type == bfd_link_hash_warning)
9267 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9268
9269 s_type = h->type;
9270
9271 ps = NULL;
9272 if (h->root.type == bfd_link_hash_defined
9273 || h->root.type == bfd_link_hash_defweak)
9274 ps = &h->root.u.def.section;
9275
9276 sym_name = h->root.root.string;
9277 }
9278 else
9279 {
9280 Elf_Internal_Sym *sym = isymbuf + r_symndx;
9281
9282 s_type = ELF_ST_TYPE (sym->st_info);
9283 ps = &finfo->sections[r_symndx];
9284 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
9285 sym, *ps);
9286 }
9287
9288 if (s_type == STT_RELC || s_type == STT_SRELC)
9289 {
9290 bfd_vma val;
9291 bfd_vma dot = (rel->r_offset
9292 + o->output_offset + o->output_section->vma);
9293 #ifdef DEBUG
9294 printf ("Encountered a complex symbol!");
9295 printf (" (input_bfd %s, section %s, reloc %ld\n",
9296 input_bfd->filename, o->name, rel - internal_relocs);
9297 printf (" symbol: idx %8.8lx, name %s\n",
9298 r_symndx, sym_name);
9299 printf (" reloc : info %8.8lx, addr %8.8lx\n",
9300 (unsigned long) rel->r_info,
9301 (unsigned long) rel->r_offset);
9302 #endif
9303 if (!eval_symbol (&val, &sym_name, input_bfd, finfo, dot,
9304 isymbuf, locsymcount, s_type == STT_SRELC))
9305 return FALSE;
9306
9307 /* Symbol evaluated OK. Update to absolute value. */
9308 set_symbol_value (input_bfd, isymbuf, locsymcount,
9309 r_symndx, val);
9310 continue;
9311 }
9312
9313 if (action_discarded != -1 && ps != NULL)
9314 {
9315 /* Complain if the definition comes from a
9316 discarded section. */
9317 if ((sec = *ps) != NULL && elf_discarded_section (sec))
9318 {
9319 BFD_ASSERT (r_symndx != 0);
9320 if (action_discarded & COMPLAIN)
9321 (*finfo->info->callbacks->einfo)
9322 (_("%X`%s' referenced in section `%A' of %B: "
9323 "defined in discarded section `%A' of %B\n"),
9324 sym_name, o, input_bfd, sec, sec->owner);
9325
9326 /* Try to do the best we can to support buggy old
9327 versions of gcc. Pretend that the symbol is
9328 really defined in the kept linkonce section.
9329 FIXME: This is quite broken. Modifying the
9330 symbol here means we will be changing all later
9331 uses of the symbol, not just in this section. */
9332 if (action_discarded & PRETEND)
9333 {
9334 asection *kept;
9335
9336 kept = _bfd_elf_check_kept_section (sec,
9337 finfo->info);
9338 if (kept != NULL)
9339 {
9340 *ps = kept;
9341 continue;
9342 }
9343 }
9344 }
9345 }
9346 }
9347
9348 /* Relocate the section by invoking a back end routine.
9349
9350 The back end routine is responsible for adjusting the
9351 section contents as necessary, and (if using Rela relocs
9352 and generating a relocatable output file) adjusting the
9353 reloc addend as necessary.
9354
9355 The back end routine does not have to worry about setting
9356 the reloc address or the reloc symbol index.
9357
9358 The back end routine is given a pointer to the swapped in
9359 internal symbols, and can access the hash table entries
9360 for the external symbols via elf_sym_hashes (input_bfd).
9361
9362 When generating relocatable output, the back end routine
9363 must handle STB_LOCAL/STT_SECTION symbols specially. The
9364 output symbol is going to be a section symbol
9365 corresponding to the output section, which will require
9366 the addend to be adjusted. */
9367
9368 ret = (*relocate_section) (output_bfd, finfo->info,
9369 input_bfd, o, contents,
9370 internal_relocs,
9371 isymbuf,
9372 finfo->sections);
9373 if (!ret)
9374 return FALSE;
9375
9376 if (ret == 2
9377 || finfo->info->relocatable
9378 || finfo->info->emitrelocations)
9379 {
9380 Elf_Internal_Rela *irela;
9381 Elf_Internal_Rela *irelaend;
9382 bfd_vma last_offset;
9383 struct elf_link_hash_entry **rel_hash;
9384 struct elf_link_hash_entry **rel_hash_list;
9385 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
9386 unsigned int next_erel;
9387 bfd_boolean rela_normal;
9388
9389 input_rel_hdr = &elf_section_data (o)->rel_hdr;
9390 rela_normal = (bed->rela_normal
9391 && (input_rel_hdr->sh_entsize
9392 == bed->s->sizeof_rela));
9393
9394 /* Adjust the reloc addresses and symbol indices. */
9395
9396 irela = internal_relocs;
9397 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
9398 rel_hash = (elf_section_data (o->output_section)->rel_hashes
9399 + elf_section_data (o->output_section)->rel_count
9400 + elf_section_data (o->output_section)->rel_count2);
9401 rel_hash_list = rel_hash;
9402 last_offset = o->output_offset;
9403 if (!finfo->info->relocatable)
9404 last_offset += o->output_section->vma;
9405 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
9406 {
9407 unsigned long r_symndx;
9408 asection *sec;
9409 Elf_Internal_Sym sym;
9410
9411 if (next_erel == bed->s->int_rels_per_ext_rel)
9412 {
9413 rel_hash++;
9414 next_erel = 0;
9415 }
9416
9417 irela->r_offset = _bfd_elf_section_offset (output_bfd,
9418 finfo->info, o,
9419 irela->r_offset);
9420 if (irela->r_offset >= (bfd_vma) -2)
9421 {
9422 /* This is a reloc for a deleted entry or somesuch.
9423 Turn it into an R_*_NONE reloc, at the same
9424 offset as the last reloc. elf_eh_frame.c and
9425 bfd_elf_discard_info rely on reloc offsets
9426 being ordered. */
9427 irela->r_offset = last_offset;
9428 irela->r_info = 0;
9429 irela->r_addend = 0;
9430 continue;
9431 }
9432
9433 irela->r_offset += o->output_offset;
9434
9435 /* Relocs in an executable have to be virtual addresses. */
9436 if (!finfo->info->relocatable)
9437 irela->r_offset += o->output_section->vma;
9438
9439 last_offset = irela->r_offset;
9440
9441 r_symndx = irela->r_info >> r_sym_shift;
9442 if (r_symndx == STN_UNDEF)
9443 continue;
9444
9445 if (r_symndx >= locsymcount
9446 || (elf_bad_symtab (input_bfd)
9447 && finfo->sections[r_symndx] == NULL))
9448 {
9449 struct elf_link_hash_entry *rh;
9450 unsigned long indx;
9451
9452 /* This is a reloc against a global symbol. We
9453 have not yet output all the local symbols, so
9454 we do not know the symbol index of any global
9455 symbol. We set the rel_hash entry for this
9456 reloc to point to the global hash table entry
9457 for this symbol. The symbol index is then
9458 set at the end of bfd_elf_final_link. */
9459 indx = r_symndx - extsymoff;
9460 rh = elf_sym_hashes (input_bfd)[indx];
9461 while (rh->root.type == bfd_link_hash_indirect
9462 || rh->root.type == bfd_link_hash_warning)
9463 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
9464
9465 /* Setting the index to -2 tells
9466 elf_link_output_extsym that this symbol is
9467 used by a reloc. */
9468 BFD_ASSERT (rh->indx < 0);
9469 rh->indx = -2;
9470
9471 *rel_hash = rh;
9472
9473 continue;
9474 }
9475
9476 /* This is a reloc against a local symbol. */
9477
9478 *rel_hash = NULL;
9479 sym = isymbuf[r_symndx];
9480 sec = finfo->sections[r_symndx];
9481 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
9482 {
9483 /* I suppose the backend ought to fill in the
9484 section of any STT_SECTION symbol against a
9485 processor specific section. */
9486 r_symndx = 0;
9487 if (bfd_is_abs_section (sec))
9488 ;
9489 else if (sec == NULL || sec->owner == NULL)
9490 {
9491 bfd_set_error (bfd_error_bad_value);
9492 return FALSE;
9493 }
9494 else
9495 {
9496 asection *osec = sec->output_section;
9497
9498 /* If we have discarded a section, the output
9499 section will be the absolute section. In
9500 case of discarded SEC_MERGE sections, use
9501 the kept section. relocate_section should
9502 have already handled discarded linkonce
9503 sections. */
9504 if (bfd_is_abs_section (osec)
9505 && sec->kept_section != NULL
9506 && sec->kept_section->output_section != NULL)
9507 {
9508 osec = sec->kept_section->output_section;
9509 irela->r_addend -= osec->vma;
9510 }
9511
9512 if (!bfd_is_abs_section (osec))
9513 {
9514 r_symndx = osec->target_index;
9515 if (r_symndx == 0)
9516 {
9517 struct elf_link_hash_table *htab;
9518 asection *oi;
9519
9520 htab = elf_hash_table (finfo->info);
9521 oi = htab->text_index_section;
9522 if ((osec->flags & SEC_READONLY) == 0
9523 && htab->data_index_section != NULL)
9524 oi = htab->data_index_section;
9525
9526 if (oi != NULL)
9527 {
9528 irela->r_addend += osec->vma - oi->vma;
9529 r_symndx = oi->target_index;
9530 }
9531 }
9532
9533 BFD_ASSERT (r_symndx != 0);
9534 }
9535 }
9536
9537 /* Adjust the addend according to where the
9538 section winds up in the output section. */
9539 if (rela_normal)
9540 irela->r_addend += sec->output_offset;
9541 }
9542 else
9543 {
9544 if (finfo->indices[r_symndx] == -1)
9545 {
9546 unsigned long shlink;
9547 const char *name;
9548 asection *osec;
9549
9550 if (finfo->info->strip == strip_all)
9551 {
9552 /* You can't do ld -r -s. */
9553 bfd_set_error (bfd_error_invalid_operation);
9554 return FALSE;
9555 }
9556
9557 /* This symbol was skipped earlier, but
9558 since it is needed by a reloc, we
9559 must output it now. */
9560 shlink = symtab_hdr->sh_link;
9561 name = (bfd_elf_string_from_elf_section
9562 (input_bfd, shlink, sym.st_name));
9563 if (name == NULL)
9564 return FALSE;
9565
9566 osec = sec->output_section;
9567 sym.st_shndx =
9568 _bfd_elf_section_from_bfd_section (output_bfd,
9569 osec);
9570 if (sym.st_shndx == SHN_BAD)
9571 return FALSE;
9572
9573 sym.st_value += sec->output_offset;
9574 if (! finfo->info->relocatable)
9575 {
9576 sym.st_value += osec->vma;
9577 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
9578 {
9579 /* STT_TLS symbols are relative to PT_TLS
9580 segment base. */
9581 BFD_ASSERT (elf_hash_table (finfo->info)
9582 ->tls_sec != NULL);
9583 sym.st_value -= (elf_hash_table (finfo->info)
9584 ->tls_sec->vma);
9585 }
9586 }
9587
9588 finfo->indices[r_symndx]
9589 = bfd_get_symcount (output_bfd);
9590
9591 if (! elf_link_output_sym (finfo, name, &sym, sec,
9592 NULL))
9593 return FALSE;
9594 }
9595
9596 r_symndx = finfo->indices[r_symndx];
9597 }
9598
9599 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
9600 | (irela->r_info & r_type_mask));
9601 }
9602
9603 /* Swap out the relocs. */
9604 if (input_rel_hdr->sh_size != 0
9605 && !bed->elf_backend_emit_relocs (output_bfd, o,
9606 input_rel_hdr,
9607 internal_relocs,
9608 rel_hash_list))
9609 return FALSE;
9610
9611 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
9612 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
9613 {
9614 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
9615 * bed->s->int_rels_per_ext_rel);
9616 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
9617 if (!bed->elf_backend_emit_relocs (output_bfd, o,
9618 input_rel_hdr2,
9619 internal_relocs,
9620 rel_hash_list))
9621 return FALSE;
9622 }
9623 }
9624 }
9625
9626 /* Write out the modified section contents. */
9627 if (bed->elf_backend_write_section
9628 && (*bed->elf_backend_write_section) (output_bfd, finfo->info, o,
9629 contents))
9630 {
9631 /* Section written out. */
9632 }
9633 else switch (o->sec_info_type)
9634 {
9635 case ELF_INFO_TYPE_STABS:
9636 if (! (_bfd_write_section_stabs
9637 (output_bfd,
9638 &elf_hash_table (finfo->info)->stab_info,
9639 o, &elf_section_data (o)->sec_info, contents)))
9640 return FALSE;
9641 break;
9642 case ELF_INFO_TYPE_MERGE:
9643 if (! _bfd_write_merged_section (output_bfd, o,
9644 elf_section_data (o)->sec_info))
9645 return FALSE;
9646 break;
9647 case ELF_INFO_TYPE_EH_FRAME:
9648 {
9649 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
9650 o, contents))
9651 return FALSE;
9652 }
9653 break;
9654 default:
9655 {
9656 if (! (o->flags & SEC_EXCLUDE)
9657 && ! (o->output_section->flags & SEC_NEVER_LOAD)
9658 && ! bfd_set_section_contents (output_bfd, o->output_section,
9659 contents,
9660 (file_ptr) o->output_offset,
9661 o->size))
9662 return FALSE;
9663 }
9664 break;
9665 }
9666 }
9667
9668 return TRUE;
9669 }
9670
9671 /* Generate a reloc when linking an ELF file. This is a reloc
9672 requested by the linker, and does not come from any input file. This
9673 is used to build constructor and destructor tables when linking
9674 with -Ur. */
9675
9676 static bfd_boolean
9677 elf_reloc_link_order (bfd *output_bfd,
9678 struct bfd_link_info *info,
9679 asection *output_section,
9680 struct bfd_link_order *link_order)
9681 {
9682 reloc_howto_type *howto;
9683 long indx;
9684 bfd_vma offset;
9685 bfd_vma addend;
9686 struct elf_link_hash_entry **rel_hash_ptr;
9687 Elf_Internal_Shdr *rel_hdr;
9688 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
9689 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
9690 bfd_byte *erel;
9691 unsigned int i;
9692
9693 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
9694 if (howto == NULL)
9695 {
9696 bfd_set_error (bfd_error_bad_value);
9697 return FALSE;
9698 }
9699
9700 addend = link_order->u.reloc.p->addend;
9701
9702 /* Figure out the symbol index. */
9703 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
9704 + elf_section_data (output_section)->rel_count
9705 + elf_section_data (output_section)->rel_count2);
9706 if (link_order->type == bfd_section_reloc_link_order)
9707 {
9708 indx = link_order->u.reloc.p->u.section->target_index;
9709 BFD_ASSERT (indx != 0);
9710 *rel_hash_ptr = NULL;
9711 }
9712 else
9713 {
9714 struct elf_link_hash_entry *h;
9715
9716 /* Treat a reloc against a defined symbol as though it were
9717 actually against the section. */
9718 h = ((struct elf_link_hash_entry *)
9719 bfd_wrapped_link_hash_lookup (output_bfd, info,
9720 link_order->u.reloc.p->u.name,
9721 FALSE, FALSE, TRUE));
9722 if (h != NULL
9723 && (h->root.type == bfd_link_hash_defined
9724 || h->root.type == bfd_link_hash_defweak))
9725 {
9726 asection *section;
9727
9728 section = h->root.u.def.section;
9729 indx = section->output_section->target_index;
9730 *rel_hash_ptr = NULL;
9731 /* It seems that we ought to add the symbol value to the
9732 addend here, but in practice it has already been added
9733 because it was passed to constructor_callback. */
9734 addend += section->output_section->vma + section->output_offset;
9735 }
9736 else if (h != NULL)
9737 {
9738 /* Setting the index to -2 tells elf_link_output_extsym that
9739 this symbol is used by a reloc. */
9740 h->indx = -2;
9741 *rel_hash_ptr = h;
9742 indx = 0;
9743 }
9744 else
9745 {
9746 if (! ((*info->callbacks->unattached_reloc)
9747 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
9748 return FALSE;
9749 indx = 0;
9750 }
9751 }
9752
9753 /* If this is an inplace reloc, we must write the addend into the
9754 object file. */
9755 if (howto->partial_inplace && addend != 0)
9756 {
9757 bfd_size_type size;
9758 bfd_reloc_status_type rstat;
9759 bfd_byte *buf;
9760 bfd_boolean ok;
9761 const char *sym_name;
9762
9763 size = bfd_get_reloc_size (howto);
9764 buf = bfd_zmalloc (size);
9765 if (buf == NULL)
9766 return FALSE;
9767 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
9768 switch (rstat)
9769 {
9770 case bfd_reloc_ok:
9771 break;
9772
9773 default:
9774 case bfd_reloc_outofrange:
9775 abort ();
9776
9777 case bfd_reloc_overflow:
9778 if (link_order->type == bfd_section_reloc_link_order)
9779 sym_name = bfd_section_name (output_bfd,
9780 link_order->u.reloc.p->u.section);
9781 else
9782 sym_name = link_order->u.reloc.p->u.name;
9783 if (! ((*info->callbacks->reloc_overflow)
9784 (info, NULL, sym_name, howto->name, addend, NULL,
9785 NULL, (bfd_vma) 0)))
9786 {
9787 free (buf);
9788 return FALSE;
9789 }
9790 break;
9791 }
9792 ok = bfd_set_section_contents (output_bfd, output_section, buf,
9793 link_order->offset, size);
9794 free (buf);
9795 if (! ok)
9796 return FALSE;
9797 }
9798
9799 /* The address of a reloc is relative to the section in a
9800 relocatable file, and is a virtual address in an executable
9801 file. */
9802 offset = link_order->offset;
9803 if (! info->relocatable)
9804 offset += output_section->vma;
9805
9806 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
9807 {
9808 irel[i].r_offset = offset;
9809 irel[i].r_info = 0;
9810 irel[i].r_addend = 0;
9811 }
9812 if (bed->s->arch_size == 32)
9813 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
9814 else
9815 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
9816
9817 rel_hdr = &elf_section_data (output_section)->rel_hdr;
9818 erel = rel_hdr->contents;
9819 if (rel_hdr->sh_type == SHT_REL)
9820 {
9821 erel += (elf_section_data (output_section)->rel_count
9822 * bed->s->sizeof_rel);
9823 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
9824 }
9825 else
9826 {
9827 irel[0].r_addend = addend;
9828 erel += (elf_section_data (output_section)->rel_count
9829 * bed->s->sizeof_rela);
9830 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
9831 }
9832
9833 ++elf_section_data (output_section)->rel_count;
9834
9835 return TRUE;
9836 }
9837
9838
9839 /* Get the output vma of the section pointed to by the sh_link field. */
9840
9841 static bfd_vma
9842 elf_get_linked_section_vma (struct bfd_link_order *p)
9843 {
9844 Elf_Internal_Shdr **elf_shdrp;
9845 asection *s;
9846 int elfsec;
9847
9848 s = p->u.indirect.section;
9849 elf_shdrp = elf_elfsections (s->owner);
9850 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
9851 elfsec = elf_shdrp[elfsec]->sh_link;
9852 /* PR 290:
9853 The Intel C compiler generates SHT_IA_64_UNWIND with
9854 SHF_LINK_ORDER. But it doesn't set the sh_link or
9855 sh_info fields. Hence we could get the situation
9856 where elfsec is 0. */
9857 if (elfsec == 0)
9858 {
9859 const struct elf_backend_data *bed
9860 = get_elf_backend_data (s->owner);
9861 if (bed->link_order_error_handler)
9862 bed->link_order_error_handler
9863 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
9864 return 0;
9865 }
9866 else
9867 {
9868 s = elf_shdrp[elfsec]->bfd_section;
9869 return s->output_section->vma + s->output_offset;
9870 }
9871 }
9872
9873
9874 /* Compare two sections based on the locations of the sections they are
9875 linked to. Used by elf_fixup_link_order. */
9876
9877 static int
9878 compare_link_order (const void * a, const void * b)
9879 {
9880 bfd_vma apos;
9881 bfd_vma bpos;
9882
9883 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
9884 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
9885 if (apos < bpos)
9886 return -1;
9887 return apos > bpos;
9888 }
9889
9890
9891 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
9892 order as their linked sections. Returns false if this could not be done
9893 because an output section includes both ordered and unordered
9894 sections. Ideally we'd do this in the linker proper. */
9895
9896 static bfd_boolean
9897 elf_fixup_link_order (bfd *abfd, asection *o)
9898 {
9899 int seen_linkorder;
9900 int seen_other;
9901 int n;
9902 struct bfd_link_order *p;
9903 bfd *sub;
9904 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9905 unsigned elfsec;
9906 struct bfd_link_order **sections;
9907 asection *s, *other_sec, *linkorder_sec;
9908 bfd_vma offset;
9909
9910 other_sec = NULL;
9911 linkorder_sec = NULL;
9912 seen_other = 0;
9913 seen_linkorder = 0;
9914 for (p = o->map_head.link_order; p != NULL; p = p->next)
9915 {
9916 if (p->type == bfd_indirect_link_order)
9917 {
9918 s = p->u.indirect.section;
9919 sub = s->owner;
9920 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
9921 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
9922 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
9923 && elfsec < elf_numsections (sub)
9924 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
9925 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
9926 {
9927 seen_linkorder++;
9928 linkorder_sec = s;
9929 }
9930 else
9931 {
9932 seen_other++;
9933 other_sec = s;
9934 }
9935 }
9936 else
9937 seen_other++;
9938
9939 if (seen_other && seen_linkorder)
9940 {
9941 if (other_sec && linkorder_sec)
9942 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
9943 o, linkorder_sec,
9944 linkorder_sec->owner, other_sec,
9945 other_sec->owner);
9946 else
9947 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
9948 o);
9949 bfd_set_error (bfd_error_bad_value);
9950 return FALSE;
9951 }
9952 }
9953
9954 if (!seen_linkorder)
9955 return TRUE;
9956
9957 sections = (struct bfd_link_order **)
9958 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
9959 if (sections == NULL)
9960 return FALSE;
9961 seen_linkorder = 0;
9962
9963 for (p = o->map_head.link_order; p != NULL; p = p->next)
9964 {
9965 sections[seen_linkorder++] = p;
9966 }
9967 /* Sort the input sections in the order of their linked section. */
9968 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
9969 compare_link_order);
9970
9971 /* Change the offsets of the sections. */
9972 offset = 0;
9973 for (n = 0; n < seen_linkorder; n++)
9974 {
9975 s = sections[n]->u.indirect.section;
9976 offset &= ~(bfd_vma) 0 << s->alignment_power;
9977 s->output_offset = offset;
9978 sections[n]->offset = offset;
9979 offset += sections[n]->size;
9980 }
9981
9982 free (sections);
9983 return TRUE;
9984 }
9985
9986
9987 /* Do the final step of an ELF link. */
9988
9989 bfd_boolean
9990 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
9991 {
9992 bfd_boolean dynamic;
9993 bfd_boolean emit_relocs;
9994 bfd *dynobj;
9995 struct elf_final_link_info finfo;
9996 register asection *o;
9997 register struct bfd_link_order *p;
9998 register bfd *sub;
9999 bfd_size_type max_contents_size;
10000 bfd_size_type max_external_reloc_size;
10001 bfd_size_type max_internal_reloc_count;
10002 bfd_size_type max_sym_count;
10003 bfd_size_type max_sym_shndx_count;
10004 file_ptr off;
10005 Elf_Internal_Sym elfsym;
10006 unsigned int i;
10007 Elf_Internal_Shdr *symtab_hdr;
10008 Elf_Internal_Shdr *symtab_shndx_hdr;
10009 Elf_Internal_Shdr *symstrtab_hdr;
10010 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10011 struct elf_outext_info eoinfo;
10012 bfd_boolean merged;
10013 size_t relativecount = 0;
10014 asection *reldyn = 0;
10015 bfd_size_type amt;
10016 asection *attr_section = NULL;
10017 bfd_vma attr_size = 0;
10018 const char *std_attrs_section;
10019
10020 if (! is_elf_hash_table (info->hash))
10021 return FALSE;
10022
10023 if (info->shared)
10024 abfd->flags |= DYNAMIC;
10025
10026 dynamic = elf_hash_table (info)->dynamic_sections_created;
10027 dynobj = elf_hash_table (info)->dynobj;
10028
10029 emit_relocs = (info->relocatable
10030 || info->emitrelocations);
10031
10032 finfo.info = info;
10033 finfo.output_bfd = abfd;
10034 finfo.symstrtab = _bfd_elf_stringtab_init ();
10035 if (finfo.symstrtab == NULL)
10036 return FALSE;
10037
10038 if (! dynamic)
10039 {
10040 finfo.dynsym_sec = NULL;
10041 finfo.hash_sec = NULL;
10042 finfo.symver_sec = NULL;
10043 }
10044 else
10045 {
10046 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
10047 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
10048 BFD_ASSERT (finfo.dynsym_sec != NULL);
10049 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
10050 /* Note that it is OK if symver_sec is NULL. */
10051 }
10052
10053 finfo.contents = NULL;
10054 finfo.external_relocs = NULL;
10055 finfo.internal_relocs = NULL;
10056 finfo.external_syms = NULL;
10057 finfo.locsym_shndx = NULL;
10058 finfo.internal_syms = NULL;
10059 finfo.indices = NULL;
10060 finfo.sections = NULL;
10061 finfo.symbuf = NULL;
10062 finfo.symshndxbuf = NULL;
10063 finfo.symbuf_count = 0;
10064 finfo.shndxbuf_size = 0;
10065
10066 /* The object attributes have been merged. Remove the input
10067 sections from the link, and set the contents of the output
10068 secton. */
10069 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
10070 for (o = abfd->sections; o != NULL; o = o->next)
10071 {
10072 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
10073 || strcmp (o->name, ".gnu.attributes") == 0)
10074 {
10075 for (p = o->map_head.link_order; p != NULL; p = p->next)
10076 {
10077 asection *input_section;
10078
10079 if (p->type != bfd_indirect_link_order)
10080 continue;
10081 input_section = p->u.indirect.section;
10082 /* Hack: reset the SEC_HAS_CONTENTS flag so that
10083 elf_link_input_bfd ignores this section. */
10084 input_section->flags &= ~SEC_HAS_CONTENTS;
10085 }
10086
10087 attr_size = bfd_elf_obj_attr_size (abfd);
10088 if (attr_size)
10089 {
10090 bfd_set_section_size (abfd, o, attr_size);
10091 attr_section = o;
10092 /* Skip this section later on. */
10093 o->map_head.link_order = NULL;
10094 }
10095 else
10096 o->flags |= SEC_EXCLUDE;
10097 }
10098 }
10099
10100 /* Count up the number of relocations we will output for each output
10101 section, so that we know the sizes of the reloc sections. We
10102 also figure out some maximum sizes. */
10103 max_contents_size = 0;
10104 max_external_reloc_size = 0;
10105 max_internal_reloc_count = 0;
10106 max_sym_count = 0;
10107 max_sym_shndx_count = 0;
10108 merged = FALSE;
10109 for (o = abfd->sections; o != NULL; o = o->next)
10110 {
10111 struct bfd_elf_section_data *esdo = elf_section_data (o);
10112 o->reloc_count = 0;
10113
10114 for (p = o->map_head.link_order; p != NULL; p = p->next)
10115 {
10116 unsigned int reloc_count = 0;
10117 struct bfd_elf_section_data *esdi = NULL;
10118 unsigned int *rel_count1;
10119
10120 if (p->type == bfd_section_reloc_link_order
10121 || p->type == bfd_symbol_reloc_link_order)
10122 reloc_count = 1;
10123 else if (p->type == bfd_indirect_link_order)
10124 {
10125 asection *sec;
10126
10127 sec = p->u.indirect.section;
10128 esdi = elf_section_data (sec);
10129
10130 /* Mark all sections which are to be included in the
10131 link. This will normally be every section. We need
10132 to do this so that we can identify any sections which
10133 the linker has decided to not include. */
10134 sec->linker_mark = TRUE;
10135
10136 if (sec->flags & SEC_MERGE)
10137 merged = TRUE;
10138
10139 if (info->relocatable || info->emitrelocations)
10140 reloc_count = sec->reloc_count;
10141 else if (bed->elf_backend_count_relocs)
10142 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
10143
10144 if (sec->rawsize > max_contents_size)
10145 max_contents_size = sec->rawsize;
10146 if (sec->size > max_contents_size)
10147 max_contents_size = sec->size;
10148
10149 /* We are interested in just local symbols, not all
10150 symbols. */
10151 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
10152 && (sec->owner->flags & DYNAMIC) == 0)
10153 {
10154 size_t sym_count;
10155
10156 if (elf_bad_symtab (sec->owner))
10157 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
10158 / bed->s->sizeof_sym);
10159 else
10160 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
10161
10162 if (sym_count > max_sym_count)
10163 max_sym_count = sym_count;
10164
10165 if (sym_count > max_sym_shndx_count
10166 && elf_symtab_shndx (sec->owner) != 0)
10167 max_sym_shndx_count = sym_count;
10168
10169 if ((sec->flags & SEC_RELOC) != 0)
10170 {
10171 size_t ext_size;
10172
10173 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
10174 if (ext_size > max_external_reloc_size)
10175 max_external_reloc_size = ext_size;
10176 if (sec->reloc_count > max_internal_reloc_count)
10177 max_internal_reloc_count = sec->reloc_count;
10178 }
10179 }
10180 }
10181
10182 if (reloc_count == 0)
10183 continue;
10184
10185 o->reloc_count += reloc_count;
10186
10187 /* MIPS may have a mix of REL and RELA relocs on sections.
10188 To support this curious ABI we keep reloc counts in
10189 elf_section_data too. We must be careful to add the
10190 relocations from the input section to the right output
10191 count. FIXME: Get rid of one count. We have
10192 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
10193 rel_count1 = &esdo->rel_count;
10194 if (esdi != NULL)
10195 {
10196 bfd_boolean same_size;
10197 bfd_size_type entsize1;
10198
10199 entsize1 = esdi->rel_hdr.sh_entsize;
10200 BFD_ASSERT (entsize1 == bed->s->sizeof_rel
10201 || entsize1 == bed->s->sizeof_rela);
10202 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
10203
10204 if (!same_size)
10205 rel_count1 = &esdo->rel_count2;
10206
10207 if (esdi->rel_hdr2 != NULL)
10208 {
10209 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
10210 unsigned int alt_count;
10211 unsigned int *rel_count2;
10212
10213 BFD_ASSERT (entsize2 != entsize1
10214 && (entsize2 == bed->s->sizeof_rel
10215 || entsize2 == bed->s->sizeof_rela));
10216
10217 rel_count2 = &esdo->rel_count2;
10218 if (!same_size)
10219 rel_count2 = &esdo->rel_count;
10220
10221 /* The following is probably too simplistic if the
10222 backend counts output relocs unusually. */
10223 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
10224 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
10225 *rel_count2 += alt_count;
10226 reloc_count -= alt_count;
10227 }
10228 }
10229 *rel_count1 += reloc_count;
10230 }
10231
10232 if (o->reloc_count > 0)
10233 o->flags |= SEC_RELOC;
10234 else
10235 {
10236 /* Explicitly clear the SEC_RELOC flag. The linker tends to
10237 set it (this is probably a bug) and if it is set
10238 assign_section_numbers will create a reloc section. */
10239 o->flags &=~ SEC_RELOC;
10240 }
10241
10242 /* If the SEC_ALLOC flag is not set, force the section VMA to
10243 zero. This is done in elf_fake_sections as well, but forcing
10244 the VMA to 0 here will ensure that relocs against these
10245 sections are handled correctly. */
10246 if ((o->flags & SEC_ALLOC) == 0
10247 && ! o->user_set_vma)
10248 o->vma = 0;
10249 }
10250
10251 if (! info->relocatable && merged)
10252 elf_link_hash_traverse (elf_hash_table (info),
10253 _bfd_elf_link_sec_merge_syms, abfd);
10254
10255 /* Figure out the file positions for everything but the symbol table
10256 and the relocs. We set symcount to force assign_section_numbers
10257 to create a symbol table. */
10258 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
10259 BFD_ASSERT (! abfd->output_has_begun);
10260 if (! _bfd_elf_compute_section_file_positions (abfd, info))
10261 goto error_return;
10262
10263 /* Set sizes, and assign file positions for reloc sections. */
10264 for (o = abfd->sections; o != NULL; o = o->next)
10265 {
10266 if ((o->flags & SEC_RELOC) != 0)
10267 {
10268 if (!(_bfd_elf_link_size_reloc_section
10269 (abfd, &elf_section_data (o)->rel_hdr, o)))
10270 goto error_return;
10271
10272 if (elf_section_data (o)->rel_hdr2
10273 && !(_bfd_elf_link_size_reloc_section
10274 (abfd, elf_section_data (o)->rel_hdr2, o)))
10275 goto error_return;
10276 }
10277
10278 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
10279 to count upwards while actually outputting the relocations. */
10280 elf_section_data (o)->rel_count = 0;
10281 elf_section_data (o)->rel_count2 = 0;
10282 }
10283
10284 _bfd_elf_assign_file_positions_for_relocs (abfd);
10285
10286 /* We have now assigned file positions for all the sections except
10287 .symtab and .strtab. We start the .symtab section at the current
10288 file position, and write directly to it. We build the .strtab
10289 section in memory. */
10290 bfd_get_symcount (abfd) = 0;
10291 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10292 /* sh_name is set in prep_headers. */
10293 symtab_hdr->sh_type = SHT_SYMTAB;
10294 /* sh_flags, sh_addr and sh_size all start off zero. */
10295 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
10296 /* sh_link is set in assign_section_numbers. */
10297 /* sh_info is set below. */
10298 /* sh_offset is set just below. */
10299 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
10300
10301 off = elf_tdata (abfd)->next_file_pos;
10302 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
10303
10304 /* Note that at this point elf_tdata (abfd)->next_file_pos is
10305 incorrect. We do not yet know the size of the .symtab section.
10306 We correct next_file_pos below, after we do know the size. */
10307
10308 /* Allocate a buffer to hold swapped out symbols. This is to avoid
10309 continuously seeking to the right position in the file. */
10310 if (! info->keep_memory || max_sym_count < 20)
10311 finfo.symbuf_size = 20;
10312 else
10313 finfo.symbuf_size = max_sym_count;
10314 amt = finfo.symbuf_size;
10315 amt *= bed->s->sizeof_sym;
10316 finfo.symbuf = bfd_malloc (amt);
10317 if (finfo.symbuf == NULL)
10318 goto error_return;
10319 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
10320 {
10321 /* Wild guess at number of output symbols. realloc'd as needed. */
10322 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
10323 finfo.shndxbuf_size = amt;
10324 amt *= sizeof (Elf_External_Sym_Shndx);
10325 finfo.symshndxbuf = bfd_zmalloc (amt);
10326 if (finfo.symshndxbuf == NULL)
10327 goto error_return;
10328 }
10329
10330 /* Start writing out the symbol table. The first symbol is always a
10331 dummy symbol. */
10332 if (info->strip != strip_all
10333 || emit_relocs)
10334 {
10335 elfsym.st_value = 0;
10336 elfsym.st_size = 0;
10337 elfsym.st_info = 0;
10338 elfsym.st_other = 0;
10339 elfsym.st_shndx = SHN_UNDEF;
10340 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
10341 NULL))
10342 goto error_return;
10343 }
10344
10345 /* Output a symbol for each section. We output these even if we are
10346 discarding local symbols, since they are used for relocs. These
10347 symbols have no names. We store the index of each one in the
10348 index field of the section, so that we can find it again when
10349 outputting relocs. */
10350 if (info->strip != strip_all
10351 || emit_relocs)
10352 {
10353 elfsym.st_size = 0;
10354 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
10355 elfsym.st_other = 0;
10356 elfsym.st_value = 0;
10357 for (i = 1; i < elf_numsections (abfd); i++)
10358 {
10359 o = bfd_section_from_elf_index (abfd, i);
10360 if (o != NULL)
10361 {
10362 o->target_index = bfd_get_symcount (abfd);
10363 elfsym.st_shndx = i;
10364 if (!info->relocatable)
10365 elfsym.st_value = o->vma;
10366 if (!elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
10367 goto error_return;
10368 }
10369 }
10370 }
10371
10372 /* Allocate some memory to hold information read in from the input
10373 files. */
10374 if (max_contents_size != 0)
10375 {
10376 finfo.contents = bfd_malloc (max_contents_size);
10377 if (finfo.contents == NULL)
10378 goto error_return;
10379 }
10380
10381 if (max_external_reloc_size != 0)
10382 {
10383 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
10384 if (finfo.external_relocs == NULL)
10385 goto error_return;
10386 }
10387
10388 if (max_internal_reloc_count != 0)
10389 {
10390 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
10391 amt *= sizeof (Elf_Internal_Rela);
10392 finfo.internal_relocs = bfd_malloc (amt);
10393 if (finfo.internal_relocs == NULL)
10394 goto error_return;
10395 }
10396
10397 if (max_sym_count != 0)
10398 {
10399 amt = max_sym_count * bed->s->sizeof_sym;
10400 finfo.external_syms = bfd_malloc (amt);
10401 if (finfo.external_syms == NULL)
10402 goto error_return;
10403
10404 amt = max_sym_count * sizeof (Elf_Internal_Sym);
10405 finfo.internal_syms = bfd_malloc (amt);
10406 if (finfo.internal_syms == NULL)
10407 goto error_return;
10408
10409 amt = max_sym_count * sizeof (long);
10410 finfo.indices = bfd_malloc (amt);
10411 if (finfo.indices == NULL)
10412 goto error_return;
10413
10414 amt = max_sym_count * sizeof (asection *);
10415 finfo.sections = bfd_malloc (amt);
10416 if (finfo.sections == NULL)
10417 goto error_return;
10418 }
10419
10420 if (max_sym_shndx_count != 0)
10421 {
10422 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
10423 finfo.locsym_shndx = bfd_malloc (amt);
10424 if (finfo.locsym_shndx == NULL)
10425 goto error_return;
10426 }
10427
10428 if (elf_hash_table (info)->tls_sec)
10429 {
10430 bfd_vma base, end = 0;
10431 asection *sec;
10432
10433 for (sec = elf_hash_table (info)->tls_sec;
10434 sec && (sec->flags & SEC_THREAD_LOCAL);
10435 sec = sec->next)
10436 {
10437 bfd_size_type size = sec->size;
10438
10439 if (size == 0
10440 && (sec->flags & SEC_HAS_CONTENTS) == 0)
10441 {
10442 struct bfd_link_order *o = sec->map_tail.link_order;
10443 if (o != NULL)
10444 size = o->offset + o->size;
10445 }
10446 end = sec->vma + size;
10447 }
10448 base = elf_hash_table (info)->tls_sec->vma;
10449 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
10450 elf_hash_table (info)->tls_size = end - base;
10451 }
10452
10453 /* Reorder SHF_LINK_ORDER sections. */
10454 for (o = abfd->sections; o != NULL; o = o->next)
10455 {
10456 if (!elf_fixup_link_order (abfd, o))
10457 return FALSE;
10458 }
10459
10460 /* Since ELF permits relocations to be against local symbols, we
10461 must have the local symbols available when we do the relocations.
10462 Since we would rather only read the local symbols once, and we
10463 would rather not keep them in memory, we handle all the
10464 relocations for a single input file at the same time.
10465
10466 Unfortunately, there is no way to know the total number of local
10467 symbols until we have seen all of them, and the local symbol
10468 indices precede the global symbol indices. This means that when
10469 we are generating relocatable output, and we see a reloc against
10470 a global symbol, we can not know the symbol index until we have
10471 finished examining all the local symbols to see which ones we are
10472 going to output. To deal with this, we keep the relocations in
10473 memory, and don't output them until the end of the link. This is
10474 an unfortunate waste of memory, but I don't see a good way around
10475 it. Fortunately, it only happens when performing a relocatable
10476 link, which is not the common case. FIXME: If keep_memory is set
10477 we could write the relocs out and then read them again; I don't
10478 know how bad the memory loss will be. */
10479
10480 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10481 sub->output_has_begun = FALSE;
10482 for (o = abfd->sections; o != NULL; o = o->next)
10483 {
10484 for (p = o->map_head.link_order; p != NULL; p = p->next)
10485 {
10486 if (p->type == bfd_indirect_link_order
10487 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
10488 == bfd_target_elf_flavour)
10489 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
10490 {
10491 if (! sub->output_has_begun)
10492 {
10493 if (! elf_link_input_bfd (&finfo, sub))
10494 goto error_return;
10495 sub->output_has_begun = TRUE;
10496 }
10497 }
10498 else if (p->type == bfd_section_reloc_link_order
10499 || p->type == bfd_symbol_reloc_link_order)
10500 {
10501 if (! elf_reloc_link_order (abfd, info, o, p))
10502 goto error_return;
10503 }
10504 else
10505 {
10506 if (! _bfd_default_link_order (abfd, info, o, p))
10507 goto error_return;
10508 }
10509 }
10510 }
10511
10512 /* Free symbol buffer if needed. */
10513 if (!info->reduce_memory_overheads)
10514 {
10515 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10516 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10517 && elf_tdata (sub)->symbuf)
10518 {
10519 free (elf_tdata (sub)->symbuf);
10520 elf_tdata (sub)->symbuf = NULL;
10521 }
10522 }
10523
10524 /* Output any global symbols that got converted to local in a
10525 version script or due to symbol visibility. We do this in a
10526 separate step since ELF requires all local symbols to appear
10527 prior to any global symbols. FIXME: We should only do this if
10528 some global symbols were, in fact, converted to become local.
10529 FIXME: Will this work correctly with the Irix 5 linker? */
10530 eoinfo.failed = FALSE;
10531 eoinfo.finfo = &finfo;
10532 eoinfo.localsyms = TRUE;
10533 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
10534 &eoinfo);
10535 if (eoinfo.failed)
10536 return FALSE;
10537
10538 /* If backend needs to output some local symbols not present in the hash
10539 table, do it now. */
10540 if (bed->elf_backend_output_arch_local_syms)
10541 {
10542 typedef bfd_boolean (*out_sym_func)
10543 (void *, const char *, Elf_Internal_Sym *, asection *,
10544 struct elf_link_hash_entry *);
10545
10546 if (! ((*bed->elf_backend_output_arch_local_syms)
10547 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
10548 return FALSE;
10549 }
10550
10551 /* That wrote out all the local symbols. Finish up the symbol table
10552 with the global symbols. Even if we want to strip everything we
10553 can, we still need to deal with those global symbols that got
10554 converted to local in a version script. */
10555
10556 /* The sh_info field records the index of the first non local symbol. */
10557 symtab_hdr->sh_info = bfd_get_symcount (abfd);
10558
10559 if (dynamic
10560 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
10561 {
10562 Elf_Internal_Sym sym;
10563 bfd_byte *dynsym = finfo.dynsym_sec->contents;
10564 long last_local = 0;
10565
10566 /* Write out the section symbols for the output sections. */
10567 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
10568 {
10569 asection *s;
10570
10571 sym.st_size = 0;
10572 sym.st_name = 0;
10573 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
10574 sym.st_other = 0;
10575
10576 for (s = abfd->sections; s != NULL; s = s->next)
10577 {
10578 int indx;
10579 bfd_byte *dest;
10580 long dynindx;
10581
10582 dynindx = elf_section_data (s)->dynindx;
10583 if (dynindx <= 0)
10584 continue;
10585 indx = elf_section_data (s)->this_idx;
10586 BFD_ASSERT (indx > 0);
10587 sym.st_shndx = indx;
10588 if (! check_dynsym (abfd, &sym))
10589 return FALSE;
10590 sym.st_value = s->vma;
10591 dest = dynsym + dynindx * bed->s->sizeof_sym;
10592 if (last_local < dynindx)
10593 last_local = dynindx;
10594 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
10595 }
10596 }
10597
10598 /* Write out the local dynsyms. */
10599 if (elf_hash_table (info)->dynlocal)
10600 {
10601 struct elf_link_local_dynamic_entry *e;
10602 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
10603 {
10604 asection *s;
10605 bfd_byte *dest;
10606
10607 sym.st_size = e->isym.st_size;
10608 sym.st_other = e->isym.st_other;
10609
10610 /* Copy the internal symbol as is.
10611 Note that we saved a word of storage and overwrote
10612 the original st_name with the dynstr_index. */
10613 sym = e->isym;
10614
10615 s = bfd_section_from_elf_index (e->input_bfd,
10616 e->isym.st_shndx);
10617 if (s != NULL)
10618 {
10619 sym.st_shndx =
10620 elf_section_data (s->output_section)->this_idx;
10621 if (! check_dynsym (abfd, &sym))
10622 return FALSE;
10623 sym.st_value = (s->output_section->vma
10624 + s->output_offset
10625 + e->isym.st_value);
10626 }
10627
10628 if (last_local < e->dynindx)
10629 last_local = e->dynindx;
10630
10631 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
10632 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
10633 }
10634 }
10635
10636 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
10637 last_local + 1;
10638 }
10639
10640 /* We get the global symbols from the hash table. */
10641 eoinfo.failed = FALSE;
10642 eoinfo.localsyms = FALSE;
10643 eoinfo.finfo = &finfo;
10644 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
10645 &eoinfo);
10646 if (eoinfo.failed)
10647 return FALSE;
10648
10649 /* If backend needs to output some symbols not present in the hash
10650 table, do it now. */
10651 if (bed->elf_backend_output_arch_syms)
10652 {
10653 typedef bfd_boolean (*out_sym_func)
10654 (void *, const char *, Elf_Internal_Sym *, asection *,
10655 struct elf_link_hash_entry *);
10656
10657 if (! ((*bed->elf_backend_output_arch_syms)
10658 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
10659 return FALSE;
10660 }
10661
10662 /* Flush all symbols to the file. */
10663 if (! elf_link_flush_output_syms (&finfo, bed))
10664 return FALSE;
10665
10666 /* Now we know the size of the symtab section. */
10667 off += symtab_hdr->sh_size;
10668
10669 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
10670 if (symtab_shndx_hdr->sh_name != 0)
10671 {
10672 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
10673 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
10674 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
10675 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
10676 symtab_shndx_hdr->sh_size = amt;
10677
10678 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
10679 off, TRUE);
10680
10681 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
10682 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
10683 return FALSE;
10684 }
10685
10686
10687 /* Finish up and write out the symbol string table (.strtab)
10688 section. */
10689 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
10690 /* sh_name was set in prep_headers. */
10691 symstrtab_hdr->sh_type = SHT_STRTAB;
10692 symstrtab_hdr->sh_flags = 0;
10693 symstrtab_hdr->sh_addr = 0;
10694 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
10695 symstrtab_hdr->sh_entsize = 0;
10696 symstrtab_hdr->sh_link = 0;
10697 symstrtab_hdr->sh_info = 0;
10698 /* sh_offset is set just below. */
10699 symstrtab_hdr->sh_addralign = 1;
10700
10701 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
10702 elf_tdata (abfd)->next_file_pos = off;
10703
10704 if (bfd_get_symcount (abfd) > 0)
10705 {
10706 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
10707 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
10708 return FALSE;
10709 }
10710
10711 /* Adjust the relocs to have the correct symbol indices. */
10712 for (o = abfd->sections; o != NULL; o = o->next)
10713 {
10714 if ((o->flags & SEC_RELOC) == 0)
10715 continue;
10716
10717 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
10718 elf_section_data (o)->rel_count,
10719 elf_section_data (o)->rel_hashes);
10720 if (elf_section_data (o)->rel_hdr2 != NULL)
10721 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
10722 elf_section_data (o)->rel_count2,
10723 (elf_section_data (o)->rel_hashes
10724 + elf_section_data (o)->rel_count));
10725
10726 /* Set the reloc_count field to 0 to prevent write_relocs from
10727 trying to swap the relocs out itself. */
10728 o->reloc_count = 0;
10729 }
10730
10731 if (dynamic && info->combreloc && dynobj != NULL)
10732 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
10733
10734 /* If we are linking against a dynamic object, or generating a
10735 shared library, finish up the dynamic linking information. */
10736 if (dynamic)
10737 {
10738 bfd_byte *dyncon, *dynconend;
10739
10740 /* Fix up .dynamic entries. */
10741 o = bfd_get_section_by_name (dynobj, ".dynamic");
10742 BFD_ASSERT (o != NULL);
10743
10744 dyncon = o->contents;
10745 dynconend = o->contents + o->size;
10746 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
10747 {
10748 Elf_Internal_Dyn dyn;
10749 const char *name;
10750 unsigned int type;
10751
10752 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
10753
10754 switch (dyn.d_tag)
10755 {
10756 default:
10757 continue;
10758 case DT_NULL:
10759 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
10760 {
10761 switch (elf_section_data (reldyn)->this_hdr.sh_type)
10762 {
10763 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
10764 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
10765 default: continue;
10766 }
10767 dyn.d_un.d_val = relativecount;
10768 relativecount = 0;
10769 break;
10770 }
10771 continue;
10772
10773 case DT_INIT:
10774 name = info->init_function;
10775 goto get_sym;
10776 case DT_FINI:
10777 name = info->fini_function;
10778 get_sym:
10779 {
10780 struct elf_link_hash_entry *h;
10781
10782 h = elf_link_hash_lookup (elf_hash_table (info), name,
10783 FALSE, FALSE, TRUE);
10784 if (h != NULL
10785 && (h->root.type == bfd_link_hash_defined
10786 || h->root.type == bfd_link_hash_defweak))
10787 {
10788 dyn.d_un.d_ptr = h->root.u.def.value;
10789 o = h->root.u.def.section;
10790 if (o->output_section != NULL)
10791 dyn.d_un.d_ptr += (o->output_section->vma
10792 + o->output_offset);
10793 else
10794 {
10795 /* The symbol is imported from another shared
10796 library and does not apply to this one. */
10797 dyn.d_un.d_ptr = 0;
10798 }
10799 break;
10800 }
10801 }
10802 continue;
10803
10804 case DT_PREINIT_ARRAYSZ:
10805 name = ".preinit_array";
10806 goto get_size;
10807 case DT_INIT_ARRAYSZ:
10808 name = ".init_array";
10809 goto get_size;
10810 case DT_FINI_ARRAYSZ:
10811 name = ".fini_array";
10812 get_size:
10813 o = bfd_get_section_by_name (abfd, name);
10814 if (o == NULL)
10815 {
10816 (*_bfd_error_handler)
10817 (_("%B: could not find output section %s"), abfd, name);
10818 goto error_return;
10819 }
10820 if (o->size == 0)
10821 (*_bfd_error_handler)
10822 (_("warning: %s section has zero size"), name);
10823 dyn.d_un.d_val = o->size;
10824 break;
10825
10826 case DT_PREINIT_ARRAY:
10827 name = ".preinit_array";
10828 goto get_vma;
10829 case DT_INIT_ARRAY:
10830 name = ".init_array";
10831 goto get_vma;
10832 case DT_FINI_ARRAY:
10833 name = ".fini_array";
10834 goto get_vma;
10835
10836 case DT_HASH:
10837 name = ".hash";
10838 goto get_vma;
10839 case DT_GNU_HASH:
10840 name = ".gnu.hash";
10841 goto get_vma;
10842 case DT_STRTAB:
10843 name = ".dynstr";
10844 goto get_vma;
10845 case DT_SYMTAB:
10846 name = ".dynsym";
10847 goto get_vma;
10848 case DT_VERDEF:
10849 name = ".gnu.version_d";
10850 goto get_vma;
10851 case DT_VERNEED:
10852 name = ".gnu.version_r";
10853 goto get_vma;
10854 case DT_VERSYM:
10855 name = ".gnu.version";
10856 get_vma:
10857 o = bfd_get_section_by_name (abfd, name);
10858 if (o == NULL)
10859 {
10860 (*_bfd_error_handler)
10861 (_("%B: could not find output section %s"), abfd, name);
10862 goto error_return;
10863 }
10864 dyn.d_un.d_ptr = o->vma;
10865 break;
10866
10867 case DT_REL:
10868 case DT_RELA:
10869 case DT_RELSZ:
10870 case DT_RELASZ:
10871 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
10872 type = SHT_REL;
10873 else
10874 type = SHT_RELA;
10875 dyn.d_un.d_val = 0;
10876 dyn.d_un.d_ptr = 0;
10877 for (i = 1; i < elf_numsections (abfd); i++)
10878 {
10879 Elf_Internal_Shdr *hdr;
10880
10881 hdr = elf_elfsections (abfd)[i];
10882 if (hdr->sh_type == type
10883 && (hdr->sh_flags & SHF_ALLOC) != 0)
10884 {
10885 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
10886 dyn.d_un.d_val += hdr->sh_size;
10887 else
10888 {
10889 if (dyn.d_un.d_ptr == 0
10890 || hdr->sh_addr < dyn.d_un.d_ptr)
10891 dyn.d_un.d_ptr = hdr->sh_addr;
10892 }
10893 }
10894 }
10895 break;
10896 }
10897 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
10898 }
10899 }
10900
10901 /* If we have created any dynamic sections, then output them. */
10902 if (dynobj != NULL)
10903 {
10904 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
10905 goto error_return;
10906
10907 /* Check for DT_TEXTREL (late, in case the backend removes it). */
10908 if (info->warn_shared_textrel && info->shared)
10909 {
10910 bfd_byte *dyncon, *dynconend;
10911
10912 /* Fix up .dynamic entries. */
10913 o = bfd_get_section_by_name (dynobj, ".dynamic");
10914 BFD_ASSERT (o != NULL);
10915
10916 dyncon = o->contents;
10917 dynconend = o->contents + o->size;
10918 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
10919 {
10920 Elf_Internal_Dyn dyn;
10921
10922 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
10923
10924 if (dyn.d_tag == DT_TEXTREL)
10925 {
10926 info->callbacks->einfo
10927 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
10928 break;
10929 }
10930 }
10931 }
10932
10933 for (o = dynobj->sections; o != NULL; o = o->next)
10934 {
10935 if ((o->flags & SEC_HAS_CONTENTS) == 0
10936 || o->size == 0
10937 || o->output_section == bfd_abs_section_ptr)
10938 continue;
10939 if ((o->flags & SEC_LINKER_CREATED) == 0)
10940 {
10941 /* At this point, we are only interested in sections
10942 created by _bfd_elf_link_create_dynamic_sections. */
10943 continue;
10944 }
10945 if (elf_hash_table (info)->stab_info.stabstr == o)
10946 continue;
10947 if (elf_hash_table (info)->eh_info.hdr_sec == o)
10948 continue;
10949 if ((elf_section_data (o->output_section)->this_hdr.sh_type
10950 != SHT_STRTAB)
10951 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
10952 {
10953 if (! bfd_set_section_contents (abfd, o->output_section,
10954 o->contents,
10955 (file_ptr) o->output_offset,
10956 o->size))
10957 goto error_return;
10958 }
10959 else
10960 {
10961 /* The contents of the .dynstr section are actually in a
10962 stringtab. */
10963 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
10964 if (bfd_seek (abfd, off, SEEK_SET) != 0
10965 || ! _bfd_elf_strtab_emit (abfd,
10966 elf_hash_table (info)->dynstr))
10967 goto error_return;
10968 }
10969 }
10970 }
10971
10972 if (info->relocatable)
10973 {
10974 bfd_boolean failed = FALSE;
10975
10976 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
10977 if (failed)
10978 goto error_return;
10979 }
10980
10981 /* If we have optimized stabs strings, output them. */
10982 if (elf_hash_table (info)->stab_info.stabstr != NULL)
10983 {
10984 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
10985 goto error_return;
10986 }
10987
10988 if (info->eh_frame_hdr)
10989 {
10990 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
10991 goto error_return;
10992 }
10993
10994 if (finfo.symstrtab != NULL)
10995 _bfd_stringtab_free (finfo.symstrtab);
10996 if (finfo.contents != NULL)
10997 free (finfo.contents);
10998 if (finfo.external_relocs != NULL)
10999 free (finfo.external_relocs);
11000 if (finfo.internal_relocs != NULL)
11001 free (finfo.internal_relocs);
11002 if (finfo.external_syms != NULL)
11003 free (finfo.external_syms);
11004 if (finfo.locsym_shndx != NULL)
11005 free (finfo.locsym_shndx);
11006 if (finfo.internal_syms != NULL)
11007 free (finfo.internal_syms);
11008 if (finfo.indices != NULL)
11009 free (finfo.indices);
11010 if (finfo.sections != NULL)
11011 free (finfo.sections);
11012 if (finfo.symbuf != NULL)
11013 free (finfo.symbuf);
11014 if (finfo.symshndxbuf != NULL)
11015 free (finfo.symshndxbuf);
11016 for (o = abfd->sections; o != NULL; o = o->next)
11017 {
11018 if ((o->flags & SEC_RELOC) != 0
11019 && elf_section_data (o)->rel_hashes != NULL)
11020 free (elf_section_data (o)->rel_hashes);
11021 }
11022
11023 elf_tdata (abfd)->linker = TRUE;
11024
11025 if (attr_section)
11026 {
11027 bfd_byte *contents = bfd_malloc (attr_size);
11028 if (contents == NULL)
11029 return FALSE; /* Bail out and fail. */
11030 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
11031 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
11032 free (contents);
11033 }
11034
11035 return TRUE;
11036
11037 error_return:
11038 if (finfo.symstrtab != NULL)
11039 _bfd_stringtab_free (finfo.symstrtab);
11040 if (finfo.contents != NULL)
11041 free (finfo.contents);
11042 if (finfo.external_relocs != NULL)
11043 free (finfo.external_relocs);
11044 if (finfo.internal_relocs != NULL)
11045 free (finfo.internal_relocs);
11046 if (finfo.external_syms != NULL)
11047 free (finfo.external_syms);
11048 if (finfo.locsym_shndx != NULL)
11049 free (finfo.locsym_shndx);
11050 if (finfo.internal_syms != NULL)
11051 free (finfo.internal_syms);
11052 if (finfo.indices != NULL)
11053 free (finfo.indices);
11054 if (finfo.sections != NULL)
11055 free (finfo.sections);
11056 if (finfo.symbuf != NULL)
11057 free (finfo.symbuf);
11058 if (finfo.symshndxbuf != NULL)
11059 free (finfo.symshndxbuf);
11060 for (o = abfd->sections; o != NULL; o = o->next)
11061 {
11062 if ((o->flags & SEC_RELOC) != 0
11063 && elf_section_data (o)->rel_hashes != NULL)
11064 free (elf_section_data (o)->rel_hashes);
11065 }
11066
11067 return FALSE;
11068 }
11069 \f
11070 /* Initialize COOKIE for input bfd ABFD. */
11071
11072 static bfd_boolean
11073 init_reloc_cookie (struct elf_reloc_cookie *cookie,
11074 struct bfd_link_info *info, bfd *abfd)
11075 {
11076 Elf_Internal_Shdr *symtab_hdr;
11077 const struct elf_backend_data *bed;
11078
11079 bed = get_elf_backend_data (abfd);
11080 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11081
11082 cookie->abfd = abfd;
11083 cookie->sym_hashes = elf_sym_hashes (abfd);
11084 cookie->bad_symtab = elf_bad_symtab (abfd);
11085 if (cookie->bad_symtab)
11086 {
11087 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11088 cookie->extsymoff = 0;
11089 }
11090 else
11091 {
11092 cookie->locsymcount = symtab_hdr->sh_info;
11093 cookie->extsymoff = symtab_hdr->sh_info;
11094 }
11095
11096 if (bed->s->arch_size == 32)
11097 cookie->r_sym_shift = 8;
11098 else
11099 cookie->r_sym_shift = 32;
11100
11101 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
11102 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
11103 {
11104 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
11105 cookie->locsymcount, 0,
11106 NULL, NULL, NULL);
11107 if (cookie->locsyms == NULL)
11108 {
11109 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
11110 return FALSE;
11111 }
11112 if (info->keep_memory)
11113 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
11114 }
11115 return TRUE;
11116 }
11117
11118 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
11119
11120 static void
11121 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
11122 {
11123 Elf_Internal_Shdr *symtab_hdr;
11124
11125 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11126 if (cookie->locsyms != NULL
11127 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
11128 free (cookie->locsyms);
11129 }
11130
11131 /* Initialize the relocation information in COOKIE for input section SEC
11132 of input bfd ABFD. */
11133
11134 static bfd_boolean
11135 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11136 struct bfd_link_info *info, bfd *abfd,
11137 asection *sec)
11138 {
11139 const struct elf_backend_data *bed;
11140
11141 if (sec->reloc_count == 0)
11142 {
11143 cookie->rels = NULL;
11144 cookie->relend = NULL;
11145 }
11146 else
11147 {
11148 bed = get_elf_backend_data (abfd);
11149
11150 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
11151 info->keep_memory);
11152 if (cookie->rels == NULL)
11153 return FALSE;
11154 cookie->rel = cookie->rels;
11155 cookie->relend = (cookie->rels
11156 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
11157 }
11158 cookie->rel = cookie->rels;
11159 return TRUE;
11160 }
11161
11162 /* Free the memory allocated by init_reloc_cookie_rels,
11163 if appropriate. */
11164
11165 static void
11166 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11167 asection *sec)
11168 {
11169 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
11170 free (cookie->rels);
11171 }
11172
11173 /* Initialize the whole of COOKIE for input section SEC. */
11174
11175 static bfd_boolean
11176 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11177 struct bfd_link_info *info,
11178 asection *sec)
11179 {
11180 if (!init_reloc_cookie (cookie, info, sec->owner))
11181 goto error1;
11182 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
11183 goto error2;
11184 return TRUE;
11185
11186 error2:
11187 fini_reloc_cookie (cookie, sec->owner);
11188 error1:
11189 return FALSE;
11190 }
11191
11192 /* Free the memory allocated by init_reloc_cookie_for_section,
11193 if appropriate. */
11194
11195 static void
11196 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11197 asection *sec)
11198 {
11199 fini_reloc_cookie_rels (cookie, sec);
11200 fini_reloc_cookie (cookie, sec->owner);
11201 }
11202 \f
11203 /* Garbage collect unused sections. */
11204
11205 /* Default gc_mark_hook. */
11206
11207 asection *
11208 _bfd_elf_gc_mark_hook (asection *sec,
11209 struct bfd_link_info *info ATTRIBUTE_UNUSED,
11210 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
11211 struct elf_link_hash_entry *h,
11212 Elf_Internal_Sym *sym)
11213 {
11214 if (h != NULL)
11215 {
11216 switch (h->root.type)
11217 {
11218 case bfd_link_hash_defined:
11219 case bfd_link_hash_defweak:
11220 return h->root.u.def.section;
11221
11222 case bfd_link_hash_common:
11223 return h->root.u.c.p->section;
11224
11225 default:
11226 break;
11227 }
11228 }
11229 else
11230 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
11231
11232 return NULL;
11233 }
11234
11235 /* COOKIE->rel describes a relocation against section SEC, which is
11236 a section we've decided to keep. Return the section that contains
11237 the relocation symbol, or NULL if no section contains it. */
11238
11239 asection *
11240 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
11241 elf_gc_mark_hook_fn gc_mark_hook,
11242 struct elf_reloc_cookie *cookie)
11243 {
11244 unsigned long r_symndx;
11245 struct elf_link_hash_entry *h;
11246
11247 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
11248 if (r_symndx == 0)
11249 return NULL;
11250
11251 if (r_symndx >= cookie->locsymcount
11252 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
11253 {
11254 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
11255 while (h->root.type == bfd_link_hash_indirect
11256 || h->root.type == bfd_link_hash_warning)
11257 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11258 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
11259 }
11260
11261 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
11262 &cookie->locsyms[r_symndx]);
11263 }
11264
11265 /* COOKIE->rel describes a relocation against section SEC, which is
11266 a section we've decided to keep. Mark the section that contains
11267 the relocation symbol. */
11268
11269 bfd_boolean
11270 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
11271 asection *sec,
11272 elf_gc_mark_hook_fn gc_mark_hook,
11273 struct elf_reloc_cookie *cookie)
11274 {
11275 asection *rsec;
11276
11277 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie);
11278 if (rsec && !rsec->gc_mark)
11279 {
11280 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
11281 rsec->gc_mark = 1;
11282 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
11283 return FALSE;
11284 }
11285 return TRUE;
11286 }
11287
11288 /* The mark phase of garbage collection. For a given section, mark
11289 it and any sections in this section's group, and all the sections
11290 which define symbols to which it refers. */
11291
11292 bfd_boolean
11293 _bfd_elf_gc_mark (struct bfd_link_info *info,
11294 asection *sec,
11295 elf_gc_mark_hook_fn gc_mark_hook)
11296 {
11297 bfd_boolean ret;
11298 asection *group_sec, *eh_frame;
11299
11300 sec->gc_mark = 1;
11301
11302 /* Mark all the sections in the group. */
11303 group_sec = elf_section_data (sec)->next_in_group;
11304 if (group_sec && !group_sec->gc_mark)
11305 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
11306 return FALSE;
11307
11308 /* Look through the section relocs. */
11309 ret = TRUE;
11310 eh_frame = elf_eh_frame_section (sec->owner);
11311 if ((sec->flags & SEC_RELOC) != 0
11312 && sec->reloc_count > 0
11313 && sec != eh_frame)
11314 {
11315 struct elf_reloc_cookie cookie;
11316
11317 if (!init_reloc_cookie_for_section (&cookie, info, sec))
11318 ret = FALSE;
11319 else
11320 {
11321 for (; cookie.rel < cookie.relend; cookie.rel++)
11322 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
11323 {
11324 ret = FALSE;
11325 break;
11326 }
11327 fini_reloc_cookie_for_section (&cookie, sec);
11328 }
11329 }
11330
11331 if (ret && eh_frame && elf_fde_list (sec))
11332 {
11333 struct elf_reloc_cookie cookie;
11334
11335 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
11336 ret = FALSE;
11337 else
11338 {
11339 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
11340 gc_mark_hook, &cookie))
11341 ret = FALSE;
11342 fini_reloc_cookie_for_section (&cookie, eh_frame);
11343 }
11344 }
11345
11346 return ret;
11347 }
11348
11349 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
11350
11351 struct elf_gc_sweep_symbol_info
11352 {
11353 struct bfd_link_info *info;
11354 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
11355 bfd_boolean);
11356 };
11357
11358 static bfd_boolean
11359 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
11360 {
11361 if (h->root.type == bfd_link_hash_warning)
11362 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11363
11364 if ((h->root.type == bfd_link_hash_defined
11365 || h->root.type == bfd_link_hash_defweak)
11366 && !h->root.u.def.section->gc_mark
11367 && !(h->root.u.def.section->owner->flags & DYNAMIC))
11368 {
11369 struct elf_gc_sweep_symbol_info *inf = data;
11370 (*inf->hide_symbol) (inf->info, h, TRUE);
11371 }
11372
11373 return TRUE;
11374 }
11375
11376 /* The sweep phase of garbage collection. Remove all garbage sections. */
11377
11378 typedef bfd_boolean (*gc_sweep_hook_fn)
11379 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
11380
11381 static bfd_boolean
11382 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
11383 {
11384 bfd *sub;
11385 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11386 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
11387 unsigned long section_sym_count;
11388 struct elf_gc_sweep_symbol_info sweep_info;
11389
11390 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11391 {
11392 asection *o;
11393
11394 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
11395 continue;
11396
11397 for (o = sub->sections; o != NULL; o = o->next)
11398 {
11399 /* Keep debug and special sections. */
11400 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
11401 || (o->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
11402 o->gc_mark = 1;
11403
11404 if (o->gc_mark)
11405 continue;
11406
11407 /* Skip sweeping sections already excluded. */
11408 if (o->flags & SEC_EXCLUDE)
11409 continue;
11410
11411 /* Since this is early in the link process, it is simple
11412 to remove a section from the output. */
11413 o->flags |= SEC_EXCLUDE;
11414
11415 if (info->print_gc_sections && o->size != 0)
11416 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
11417
11418 /* But we also have to update some of the relocation
11419 info we collected before. */
11420 if (gc_sweep_hook
11421 && (o->flags & SEC_RELOC) != 0
11422 && o->reloc_count > 0
11423 && !bfd_is_abs_section (o->output_section))
11424 {
11425 Elf_Internal_Rela *internal_relocs;
11426 bfd_boolean r;
11427
11428 internal_relocs
11429 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
11430 info->keep_memory);
11431 if (internal_relocs == NULL)
11432 return FALSE;
11433
11434 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
11435
11436 if (elf_section_data (o)->relocs != internal_relocs)
11437 free (internal_relocs);
11438
11439 if (!r)
11440 return FALSE;
11441 }
11442 }
11443 }
11444
11445 /* Remove the symbols that were in the swept sections from the dynamic
11446 symbol table. GCFIXME: Anyone know how to get them out of the
11447 static symbol table as well? */
11448 sweep_info.info = info;
11449 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
11450 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
11451 &sweep_info);
11452
11453 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
11454 return TRUE;
11455 }
11456
11457 /* Propagate collected vtable information. This is called through
11458 elf_link_hash_traverse. */
11459
11460 static bfd_boolean
11461 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
11462 {
11463 if (h->root.type == bfd_link_hash_warning)
11464 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11465
11466 /* Those that are not vtables. */
11467 if (h->vtable == NULL || h->vtable->parent == NULL)
11468 return TRUE;
11469
11470 /* Those vtables that do not have parents, we cannot merge. */
11471 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
11472 return TRUE;
11473
11474 /* If we've already been done, exit. */
11475 if (h->vtable->used && h->vtable->used[-1])
11476 return TRUE;
11477
11478 /* Make sure the parent's table is up to date. */
11479 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
11480
11481 if (h->vtable->used == NULL)
11482 {
11483 /* None of this table's entries were referenced. Re-use the
11484 parent's table. */
11485 h->vtable->used = h->vtable->parent->vtable->used;
11486 h->vtable->size = h->vtable->parent->vtable->size;
11487 }
11488 else
11489 {
11490 size_t n;
11491 bfd_boolean *cu, *pu;
11492
11493 /* Or the parent's entries into ours. */
11494 cu = h->vtable->used;
11495 cu[-1] = TRUE;
11496 pu = h->vtable->parent->vtable->used;
11497 if (pu != NULL)
11498 {
11499 const struct elf_backend_data *bed;
11500 unsigned int log_file_align;
11501
11502 bed = get_elf_backend_data (h->root.u.def.section->owner);
11503 log_file_align = bed->s->log_file_align;
11504 n = h->vtable->parent->vtable->size >> log_file_align;
11505 while (n--)
11506 {
11507 if (*pu)
11508 *cu = TRUE;
11509 pu++;
11510 cu++;
11511 }
11512 }
11513 }
11514
11515 return TRUE;
11516 }
11517
11518 static bfd_boolean
11519 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
11520 {
11521 asection *sec;
11522 bfd_vma hstart, hend;
11523 Elf_Internal_Rela *relstart, *relend, *rel;
11524 const struct elf_backend_data *bed;
11525 unsigned int log_file_align;
11526
11527 if (h->root.type == bfd_link_hash_warning)
11528 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11529
11530 /* Take care of both those symbols that do not describe vtables as
11531 well as those that are not loaded. */
11532 if (h->vtable == NULL || h->vtable->parent == NULL)
11533 return TRUE;
11534
11535 BFD_ASSERT (h->root.type == bfd_link_hash_defined
11536 || h->root.type == bfd_link_hash_defweak);
11537
11538 sec = h->root.u.def.section;
11539 hstart = h->root.u.def.value;
11540 hend = hstart + h->size;
11541
11542 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
11543 if (!relstart)
11544 return *(bfd_boolean *) okp = FALSE;
11545 bed = get_elf_backend_data (sec->owner);
11546 log_file_align = bed->s->log_file_align;
11547
11548 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
11549
11550 for (rel = relstart; rel < relend; ++rel)
11551 if (rel->r_offset >= hstart && rel->r_offset < hend)
11552 {
11553 /* If the entry is in use, do nothing. */
11554 if (h->vtable->used
11555 && (rel->r_offset - hstart) < h->vtable->size)
11556 {
11557 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
11558 if (h->vtable->used[entry])
11559 continue;
11560 }
11561 /* Otherwise, kill it. */
11562 rel->r_offset = rel->r_info = rel->r_addend = 0;
11563 }
11564
11565 return TRUE;
11566 }
11567
11568 /* Mark sections containing dynamically referenced symbols. When
11569 building shared libraries, we must assume that any visible symbol is
11570 referenced. */
11571
11572 bfd_boolean
11573 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
11574 {
11575 struct bfd_link_info *info = (struct bfd_link_info *) inf;
11576
11577 if (h->root.type == bfd_link_hash_warning)
11578 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11579
11580 if ((h->root.type == bfd_link_hash_defined
11581 || h->root.type == bfd_link_hash_defweak)
11582 && (h->ref_dynamic
11583 || (!info->executable
11584 && h->def_regular
11585 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
11586 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN)))
11587 h->root.u.def.section->flags |= SEC_KEEP;
11588
11589 return TRUE;
11590 }
11591
11592 /* Keep all sections containing symbols undefined on the command-line,
11593 and the section containing the entry symbol. */
11594
11595 void
11596 _bfd_elf_gc_keep (struct bfd_link_info *info)
11597 {
11598 struct bfd_sym_chain *sym;
11599
11600 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
11601 {
11602 struct elf_link_hash_entry *h;
11603
11604 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
11605 FALSE, FALSE, FALSE);
11606
11607 if (h != NULL
11608 && (h->root.type == bfd_link_hash_defined
11609 || h->root.type == bfd_link_hash_defweak)
11610 && !bfd_is_abs_section (h->root.u.def.section))
11611 h->root.u.def.section->flags |= SEC_KEEP;
11612 }
11613 }
11614
11615 /* Do mark and sweep of unused sections. */
11616
11617 bfd_boolean
11618 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
11619 {
11620 bfd_boolean ok = TRUE;
11621 bfd *sub;
11622 elf_gc_mark_hook_fn gc_mark_hook;
11623 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11624
11625 if (!bed->can_gc_sections
11626 || !is_elf_hash_table (info->hash))
11627 {
11628 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
11629 return TRUE;
11630 }
11631
11632 bed->gc_keep (info);
11633
11634 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
11635 at the .eh_frame section if we can mark the FDEs individually. */
11636 _bfd_elf_begin_eh_frame_parsing (info);
11637 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11638 {
11639 asection *sec;
11640 struct elf_reloc_cookie cookie;
11641
11642 sec = bfd_get_section_by_name (sub, ".eh_frame");
11643 if (sec && init_reloc_cookie_for_section (&cookie, info, sec))
11644 {
11645 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
11646 if (elf_section_data (sec)->sec_info)
11647 elf_eh_frame_section (sub) = sec;
11648 fini_reloc_cookie_for_section (&cookie, sec);
11649 }
11650 }
11651 _bfd_elf_end_eh_frame_parsing (info);
11652
11653 /* Apply transitive closure to the vtable entry usage info. */
11654 elf_link_hash_traverse (elf_hash_table (info),
11655 elf_gc_propagate_vtable_entries_used,
11656 &ok);
11657 if (!ok)
11658 return FALSE;
11659
11660 /* Kill the vtable relocations that were not used. */
11661 elf_link_hash_traverse (elf_hash_table (info),
11662 elf_gc_smash_unused_vtentry_relocs,
11663 &ok);
11664 if (!ok)
11665 return FALSE;
11666
11667 /* Mark dynamically referenced symbols. */
11668 if (elf_hash_table (info)->dynamic_sections_created)
11669 elf_link_hash_traverse (elf_hash_table (info),
11670 bed->gc_mark_dynamic_ref,
11671 info);
11672
11673 /* Grovel through relocs to find out who stays ... */
11674 gc_mark_hook = bed->gc_mark_hook;
11675 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11676 {
11677 asection *o;
11678
11679 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
11680 continue;
11681
11682 for (o = sub->sections; o != NULL; o = o->next)
11683 if ((o->flags & (SEC_EXCLUDE | SEC_KEEP)) == SEC_KEEP && !o->gc_mark)
11684 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
11685 return FALSE;
11686 }
11687
11688 /* Allow the backend to mark additional target specific sections. */
11689 if (bed->gc_mark_extra_sections)
11690 bed->gc_mark_extra_sections (info, gc_mark_hook);
11691
11692 /* ... and mark SEC_EXCLUDE for those that go. */
11693 return elf_gc_sweep (abfd, info);
11694 }
11695 \f
11696 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
11697
11698 bfd_boolean
11699 bfd_elf_gc_record_vtinherit (bfd *abfd,
11700 asection *sec,
11701 struct elf_link_hash_entry *h,
11702 bfd_vma offset)
11703 {
11704 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
11705 struct elf_link_hash_entry **search, *child;
11706 bfd_size_type extsymcount;
11707 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11708
11709 /* The sh_info field of the symtab header tells us where the
11710 external symbols start. We don't care about the local symbols at
11711 this point. */
11712 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
11713 if (!elf_bad_symtab (abfd))
11714 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
11715
11716 sym_hashes = elf_sym_hashes (abfd);
11717 sym_hashes_end = sym_hashes + extsymcount;
11718
11719 /* Hunt down the child symbol, which is in this section at the same
11720 offset as the relocation. */
11721 for (search = sym_hashes; search != sym_hashes_end; ++search)
11722 {
11723 if ((child = *search) != NULL
11724 && (child->root.type == bfd_link_hash_defined
11725 || child->root.type == bfd_link_hash_defweak)
11726 && child->root.u.def.section == sec
11727 && child->root.u.def.value == offset)
11728 goto win;
11729 }
11730
11731 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
11732 abfd, sec, (unsigned long) offset);
11733 bfd_set_error (bfd_error_invalid_operation);
11734 return FALSE;
11735
11736 win:
11737 if (!child->vtable)
11738 {
11739 child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable));
11740 if (!child->vtable)
11741 return FALSE;
11742 }
11743 if (!h)
11744 {
11745 /* This *should* only be the absolute section. It could potentially
11746 be that someone has defined a non-global vtable though, which
11747 would be bad. It isn't worth paging in the local symbols to be
11748 sure though; that case should simply be handled by the assembler. */
11749
11750 child->vtable->parent = (struct elf_link_hash_entry *) -1;
11751 }
11752 else
11753 child->vtable->parent = h;
11754
11755 return TRUE;
11756 }
11757
11758 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
11759
11760 bfd_boolean
11761 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
11762 asection *sec ATTRIBUTE_UNUSED,
11763 struct elf_link_hash_entry *h,
11764 bfd_vma addend)
11765 {
11766 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11767 unsigned int log_file_align = bed->s->log_file_align;
11768
11769 if (!h->vtable)
11770 {
11771 h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable));
11772 if (!h->vtable)
11773 return FALSE;
11774 }
11775
11776 if (addend >= h->vtable->size)
11777 {
11778 size_t size, bytes, file_align;
11779 bfd_boolean *ptr = h->vtable->used;
11780
11781 /* While the symbol is undefined, we have to be prepared to handle
11782 a zero size. */
11783 file_align = 1 << log_file_align;
11784 if (h->root.type == bfd_link_hash_undefined)
11785 size = addend + file_align;
11786 else
11787 {
11788 size = h->size;
11789 if (addend >= size)
11790 {
11791 /* Oops! We've got a reference past the defined end of
11792 the table. This is probably a bug -- shall we warn? */
11793 size = addend + file_align;
11794 }
11795 }
11796 size = (size + file_align - 1) & -file_align;
11797
11798 /* Allocate one extra entry for use as a "done" flag for the
11799 consolidation pass. */
11800 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
11801
11802 if (ptr)
11803 {
11804 ptr = bfd_realloc (ptr - 1, bytes);
11805
11806 if (ptr != NULL)
11807 {
11808 size_t oldbytes;
11809
11810 oldbytes = (((h->vtable->size >> log_file_align) + 1)
11811 * sizeof (bfd_boolean));
11812 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
11813 }
11814 }
11815 else
11816 ptr = bfd_zmalloc (bytes);
11817
11818 if (ptr == NULL)
11819 return FALSE;
11820
11821 /* And arrange for that done flag to be at index -1. */
11822 h->vtable->used = ptr + 1;
11823 h->vtable->size = size;
11824 }
11825
11826 h->vtable->used[addend >> log_file_align] = TRUE;
11827
11828 return TRUE;
11829 }
11830
11831 struct alloc_got_off_arg {
11832 bfd_vma gotoff;
11833 struct bfd_link_info *info;
11834 };
11835
11836 /* We need a special top-level link routine to convert got reference counts
11837 to real got offsets. */
11838
11839 static bfd_boolean
11840 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
11841 {
11842 struct alloc_got_off_arg *gofarg = arg;
11843 bfd *obfd = gofarg->info->output_bfd;
11844 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
11845
11846 if (h->root.type == bfd_link_hash_warning)
11847 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11848
11849 if (h->got.refcount > 0)
11850 {
11851 h->got.offset = gofarg->gotoff;
11852 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
11853 }
11854 else
11855 h->got.offset = (bfd_vma) -1;
11856
11857 return TRUE;
11858 }
11859
11860 /* And an accompanying bit to work out final got entry offsets once
11861 we're done. Should be called from final_link. */
11862
11863 bfd_boolean
11864 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
11865 struct bfd_link_info *info)
11866 {
11867 bfd *i;
11868 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11869 bfd_vma gotoff;
11870 struct alloc_got_off_arg gofarg;
11871
11872 BFD_ASSERT (abfd == info->output_bfd);
11873
11874 if (! is_elf_hash_table (info->hash))
11875 return FALSE;
11876
11877 /* The GOT offset is relative to the .got section, but the GOT header is
11878 put into the .got.plt section, if the backend uses it. */
11879 if (bed->want_got_plt)
11880 gotoff = 0;
11881 else
11882 gotoff = bed->got_header_size;
11883
11884 /* Do the local .got entries first. */
11885 for (i = info->input_bfds; i; i = i->link_next)
11886 {
11887 bfd_signed_vma *local_got;
11888 bfd_size_type j, locsymcount;
11889 Elf_Internal_Shdr *symtab_hdr;
11890
11891 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
11892 continue;
11893
11894 local_got = elf_local_got_refcounts (i);
11895 if (!local_got)
11896 continue;
11897
11898 symtab_hdr = &elf_tdata (i)->symtab_hdr;
11899 if (elf_bad_symtab (i))
11900 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11901 else
11902 locsymcount = symtab_hdr->sh_info;
11903
11904 for (j = 0; j < locsymcount; ++j)
11905 {
11906 if (local_got[j] > 0)
11907 {
11908 local_got[j] = gotoff;
11909 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
11910 }
11911 else
11912 local_got[j] = (bfd_vma) -1;
11913 }
11914 }
11915
11916 /* Then the global .got entries. .plt refcounts are handled by
11917 adjust_dynamic_symbol */
11918 gofarg.gotoff = gotoff;
11919 gofarg.info = info;
11920 elf_link_hash_traverse (elf_hash_table (info),
11921 elf_gc_allocate_got_offsets,
11922 &gofarg);
11923 return TRUE;
11924 }
11925
11926 /* Many folk need no more in the way of final link than this, once
11927 got entry reference counting is enabled. */
11928
11929 bfd_boolean
11930 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
11931 {
11932 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
11933 return FALSE;
11934
11935 /* Invoke the regular ELF backend linker to do all the work. */
11936 return bfd_elf_final_link (abfd, info);
11937 }
11938
11939 bfd_boolean
11940 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
11941 {
11942 struct elf_reloc_cookie *rcookie = cookie;
11943
11944 if (rcookie->bad_symtab)
11945 rcookie->rel = rcookie->rels;
11946
11947 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
11948 {
11949 unsigned long r_symndx;
11950
11951 if (! rcookie->bad_symtab)
11952 if (rcookie->rel->r_offset > offset)
11953 return FALSE;
11954 if (rcookie->rel->r_offset != offset)
11955 continue;
11956
11957 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
11958 if (r_symndx == SHN_UNDEF)
11959 return TRUE;
11960
11961 if (r_symndx >= rcookie->locsymcount
11962 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
11963 {
11964 struct elf_link_hash_entry *h;
11965
11966 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
11967
11968 while (h->root.type == bfd_link_hash_indirect
11969 || h->root.type == bfd_link_hash_warning)
11970 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11971
11972 if ((h->root.type == bfd_link_hash_defined
11973 || h->root.type == bfd_link_hash_defweak)
11974 && elf_discarded_section (h->root.u.def.section))
11975 return TRUE;
11976 else
11977 return FALSE;
11978 }
11979 else
11980 {
11981 /* It's not a relocation against a global symbol,
11982 but it could be a relocation against a local
11983 symbol for a discarded section. */
11984 asection *isec;
11985 Elf_Internal_Sym *isym;
11986
11987 /* Need to: get the symbol; get the section. */
11988 isym = &rcookie->locsyms[r_symndx];
11989 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
11990 if (isec != NULL && elf_discarded_section (isec))
11991 return TRUE;
11992 }
11993 return FALSE;
11994 }
11995 return FALSE;
11996 }
11997
11998 /* Discard unneeded references to discarded sections.
11999 Returns TRUE if any section's size was changed. */
12000 /* This function assumes that the relocations are in sorted order,
12001 which is true for all known assemblers. */
12002
12003 bfd_boolean
12004 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
12005 {
12006 struct elf_reloc_cookie cookie;
12007 asection *stab, *eh;
12008 const struct elf_backend_data *bed;
12009 bfd *abfd;
12010 bfd_boolean ret = FALSE;
12011
12012 if (info->traditional_format
12013 || !is_elf_hash_table (info->hash))
12014 return FALSE;
12015
12016 _bfd_elf_begin_eh_frame_parsing (info);
12017 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
12018 {
12019 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
12020 continue;
12021
12022 bed = get_elf_backend_data (abfd);
12023
12024 if ((abfd->flags & DYNAMIC) != 0)
12025 continue;
12026
12027 eh = NULL;
12028 if (!info->relocatable)
12029 {
12030 eh = bfd_get_section_by_name (abfd, ".eh_frame");
12031 if (eh != NULL
12032 && (eh->size == 0
12033 || bfd_is_abs_section (eh->output_section)))
12034 eh = NULL;
12035 }
12036
12037 stab = bfd_get_section_by_name (abfd, ".stab");
12038 if (stab != NULL
12039 && (stab->size == 0
12040 || bfd_is_abs_section (stab->output_section)
12041 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
12042 stab = NULL;
12043
12044 if (stab == NULL
12045 && eh == NULL
12046 && bed->elf_backend_discard_info == NULL)
12047 continue;
12048
12049 if (!init_reloc_cookie (&cookie, info, abfd))
12050 return FALSE;
12051
12052 if (stab != NULL
12053 && stab->reloc_count > 0
12054 && init_reloc_cookie_rels (&cookie, info, abfd, stab))
12055 {
12056 if (_bfd_discard_section_stabs (abfd, stab,
12057 elf_section_data (stab)->sec_info,
12058 bfd_elf_reloc_symbol_deleted_p,
12059 &cookie))
12060 ret = TRUE;
12061 fini_reloc_cookie_rels (&cookie, stab);
12062 }
12063
12064 if (eh != NULL
12065 && init_reloc_cookie_rels (&cookie, info, abfd, eh))
12066 {
12067 _bfd_elf_parse_eh_frame (abfd, info, eh, &cookie);
12068 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
12069 bfd_elf_reloc_symbol_deleted_p,
12070 &cookie))
12071 ret = TRUE;
12072 fini_reloc_cookie_rels (&cookie, eh);
12073 }
12074
12075 if (bed->elf_backend_discard_info != NULL
12076 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
12077 ret = TRUE;
12078
12079 fini_reloc_cookie (&cookie, abfd);
12080 }
12081 _bfd_elf_end_eh_frame_parsing (info);
12082
12083 if (info->eh_frame_hdr
12084 && !info->relocatable
12085 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
12086 ret = TRUE;
12087
12088 return ret;
12089 }
12090
12091 /* For a SHT_GROUP section, return the group signature. For other
12092 sections, return the normal section name. */
12093
12094 static const char *
12095 section_signature (asection *sec)
12096 {
12097 if ((sec->flags & SEC_GROUP) != 0
12098 && elf_next_in_group (sec) != NULL
12099 && elf_group_name (elf_next_in_group (sec)) != NULL)
12100 return elf_group_name (elf_next_in_group (sec));
12101 return sec->name;
12102 }
12103
12104 void
12105 _bfd_elf_section_already_linked (bfd *abfd, asection *sec,
12106 struct bfd_link_info *info)
12107 {
12108 flagword flags;
12109 const char *name, *p;
12110 struct bfd_section_already_linked *l;
12111 struct bfd_section_already_linked_hash_entry *already_linked_list;
12112
12113 if (sec->output_section == bfd_abs_section_ptr)
12114 return;
12115
12116 flags = sec->flags;
12117
12118 /* Return if it isn't a linkonce section. A comdat group section
12119 also has SEC_LINK_ONCE set. */
12120 if ((flags & SEC_LINK_ONCE) == 0)
12121 return;
12122
12123 /* Don't put group member sections on our list of already linked
12124 sections. They are handled as a group via their group section. */
12125 if (elf_sec_group (sec) != NULL)
12126 return;
12127
12128 /* FIXME: When doing a relocatable link, we may have trouble
12129 copying relocations in other sections that refer to local symbols
12130 in the section being discarded. Those relocations will have to
12131 be converted somehow; as of this writing I'm not sure that any of
12132 the backends handle that correctly.
12133
12134 It is tempting to instead not discard link once sections when
12135 doing a relocatable link (technically, they should be discarded
12136 whenever we are building constructors). However, that fails,
12137 because the linker winds up combining all the link once sections
12138 into a single large link once section, which defeats the purpose
12139 of having link once sections in the first place.
12140
12141 Also, not merging link once sections in a relocatable link
12142 causes trouble for MIPS ELF, which relies on link once semantics
12143 to handle the .reginfo section correctly. */
12144
12145 name = section_signature (sec);
12146
12147 if (CONST_STRNEQ (name, ".gnu.linkonce.")
12148 && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
12149 p++;
12150 else
12151 p = name;
12152
12153 already_linked_list = bfd_section_already_linked_table_lookup (p);
12154
12155 for (l = already_linked_list->entry; l != NULL; l = l->next)
12156 {
12157 /* We may have 2 different types of sections on the list: group
12158 sections and linkonce sections. Match like sections. */
12159 if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
12160 && strcmp (name, section_signature (l->sec)) == 0
12161 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
12162 {
12163 /* The section has already been linked. See if we should
12164 issue a warning. */
12165 switch (flags & SEC_LINK_DUPLICATES)
12166 {
12167 default:
12168 abort ();
12169
12170 case SEC_LINK_DUPLICATES_DISCARD:
12171 break;
12172
12173 case SEC_LINK_DUPLICATES_ONE_ONLY:
12174 (*_bfd_error_handler)
12175 (_("%B: ignoring duplicate section `%A'"),
12176 abfd, sec);
12177 break;
12178
12179 case SEC_LINK_DUPLICATES_SAME_SIZE:
12180 if (sec->size != l->sec->size)
12181 (*_bfd_error_handler)
12182 (_("%B: duplicate section `%A' has different size"),
12183 abfd, sec);
12184 break;
12185
12186 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
12187 if (sec->size != l->sec->size)
12188 (*_bfd_error_handler)
12189 (_("%B: duplicate section `%A' has different size"),
12190 abfd, sec);
12191 else if (sec->size != 0)
12192 {
12193 bfd_byte *sec_contents, *l_sec_contents;
12194
12195 if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
12196 (*_bfd_error_handler)
12197 (_("%B: warning: could not read contents of section `%A'"),
12198 abfd, sec);
12199 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
12200 &l_sec_contents))
12201 (*_bfd_error_handler)
12202 (_("%B: warning: could not read contents of section `%A'"),
12203 l->sec->owner, l->sec);
12204 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
12205 (*_bfd_error_handler)
12206 (_("%B: warning: duplicate section `%A' has different contents"),
12207 abfd, sec);
12208
12209 if (sec_contents)
12210 free (sec_contents);
12211 if (l_sec_contents)
12212 free (l_sec_contents);
12213 }
12214 break;
12215 }
12216
12217 /* Set the output_section field so that lang_add_section
12218 does not create a lang_input_section structure for this
12219 section. Since there might be a symbol in the section
12220 being discarded, we must retain a pointer to the section
12221 which we are really going to use. */
12222 sec->output_section = bfd_abs_section_ptr;
12223 sec->kept_section = l->sec;
12224
12225 if (flags & SEC_GROUP)
12226 {
12227 asection *first = elf_next_in_group (sec);
12228 asection *s = first;
12229
12230 while (s != NULL)
12231 {
12232 s->output_section = bfd_abs_section_ptr;
12233 /* Record which group discards it. */
12234 s->kept_section = l->sec;
12235 s = elf_next_in_group (s);
12236 /* These lists are circular. */
12237 if (s == first)
12238 break;
12239 }
12240 }
12241
12242 return;
12243 }
12244 }
12245
12246 /* A single member comdat group section may be discarded by a
12247 linkonce section and vice versa. */
12248
12249 if ((flags & SEC_GROUP) != 0)
12250 {
12251 asection *first = elf_next_in_group (sec);
12252
12253 if (first != NULL && elf_next_in_group (first) == first)
12254 /* Check this single member group against linkonce sections. */
12255 for (l = already_linked_list->entry; l != NULL; l = l->next)
12256 if ((l->sec->flags & SEC_GROUP) == 0
12257 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
12258 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
12259 {
12260 first->output_section = bfd_abs_section_ptr;
12261 first->kept_section = l->sec;
12262 sec->output_section = bfd_abs_section_ptr;
12263 break;
12264 }
12265 }
12266 else
12267 /* Check this linkonce section against single member groups. */
12268 for (l = already_linked_list->entry; l != NULL; l = l->next)
12269 if (l->sec->flags & SEC_GROUP)
12270 {
12271 asection *first = elf_next_in_group (l->sec);
12272
12273 if (first != NULL
12274 && elf_next_in_group (first) == first
12275 && bfd_elf_match_symbols_in_sections (first, sec, info))
12276 {
12277 sec->output_section = bfd_abs_section_ptr;
12278 sec->kept_section = first;
12279 break;
12280 }
12281 }
12282
12283 /* This is the first section with this name. Record it. */
12284 if (! bfd_section_already_linked_table_insert (already_linked_list, sec))
12285 info->callbacks->einfo (_("%F%P: already_linked_table: %E"));
12286 }
12287
12288 bfd_boolean
12289 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
12290 {
12291 return sym->st_shndx == SHN_COMMON;
12292 }
12293
12294 unsigned int
12295 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
12296 {
12297 return SHN_COMMON;
12298 }
12299
12300 asection *
12301 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
12302 {
12303 return bfd_com_section_ptr;
12304 }
12305
12306 bfd_vma
12307 _bfd_elf_default_got_elt_size (bfd *abfd,
12308 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12309 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
12310 bfd *ibfd ATTRIBUTE_UNUSED,
12311 unsigned long symndx ATTRIBUTE_UNUSED)
12312 {
12313 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12314 return bed->s->arch_size / 8;
12315 }
12316
12317 /* Routines to support the creation of dynamic relocs. */
12318
12319 /* Return true if NAME is a name of a relocation
12320 section associated with section S. */
12321
12322 static bfd_boolean
12323 is_reloc_section (bfd_boolean rela, const char * name, asection * s)
12324 {
12325 if (rela)
12326 return CONST_STRNEQ (name, ".rela")
12327 && strcmp (bfd_get_section_name (NULL, s), name + 5) == 0;
12328
12329 return CONST_STRNEQ (name, ".rel")
12330 && strcmp (bfd_get_section_name (NULL, s), name + 4) == 0;
12331 }
12332
12333 /* Returns the name of the dynamic reloc section associated with SEC. */
12334
12335 static const char *
12336 get_dynamic_reloc_section_name (bfd * abfd,
12337 asection * sec,
12338 bfd_boolean is_rela)
12339 {
12340 const char * name;
12341 unsigned int strndx = elf_elfheader (abfd)->e_shstrndx;
12342 unsigned int shnam = elf_section_data (sec)->rel_hdr.sh_name;
12343
12344 name = bfd_elf_string_from_elf_section (abfd, strndx, shnam);
12345 if (name == NULL)
12346 return NULL;
12347
12348 if (! is_reloc_section (is_rela, name, sec))
12349 {
12350 static bfd_boolean complained = FALSE;
12351
12352 if (! complained)
12353 {
12354 (*_bfd_error_handler)
12355 (_("%B: bad relocation section name `%s\'"), abfd, name);
12356 complained = TRUE;
12357 }
12358 name = NULL;
12359 }
12360
12361 return name;
12362 }
12363
12364 /* Returns the dynamic reloc section associated with SEC.
12365 If necessary compute the name of the dynamic reloc section based
12366 on SEC's name (looked up in ABFD's string table) and the setting
12367 of IS_RELA. */
12368
12369 asection *
12370 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
12371 asection * sec,
12372 bfd_boolean is_rela)
12373 {
12374 asection * reloc_sec = elf_section_data (sec)->sreloc;
12375
12376 if (reloc_sec == NULL)
12377 {
12378 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
12379
12380 if (name != NULL)
12381 {
12382 reloc_sec = bfd_get_section_by_name (abfd, name);
12383
12384 if (reloc_sec != NULL)
12385 elf_section_data (sec)->sreloc = reloc_sec;
12386 }
12387 }
12388
12389 return reloc_sec;
12390 }
12391
12392 /* Returns the dynamic reloc section associated with SEC. If the
12393 section does not exist it is created and attached to the DYNOBJ
12394 bfd and stored in the SRELOC field of SEC's elf_section_data
12395 structure.
12396
12397 ALIGNMENT is the alignment for the newly created section and
12398 IS_RELA defines whether the name should be .rela.<SEC's name>
12399 or .rel.<SEC's name>. The section name is looked up in the
12400 string table associated with ABFD. */
12401
12402 asection *
12403 _bfd_elf_make_dynamic_reloc_section (asection * sec,
12404 bfd * dynobj,
12405 unsigned int alignment,
12406 bfd * abfd,
12407 bfd_boolean is_rela)
12408 {
12409 asection * reloc_sec = elf_section_data (sec)->sreloc;
12410
12411 if (reloc_sec == NULL)
12412 {
12413 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
12414
12415 if (name == NULL)
12416 return NULL;
12417
12418 reloc_sec = bfd_get_section_by_name (dynobj, name);
12419
12420 if (reloc_sec == NULL)
12421 {
12422 flagword flags;
12423
12424 flags = (SEC_HAS_CONTENTS | SEC_READONLY | SEC_IN_MEMORY | SEC_LINKER_CREATED);
12425 if ((sec->flags & SEC_ALLOC) != 0)
12426 flags |= SEC_ALLOC | SEC_LOAD;
12427
12428 reloc_sec = bfd_make_section_with_flags (dynobj, name, flags);
12429 if (reloc_sec != NULL)
12430 {
12431 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
12432 reloc_sec = NULL;
12433 }
12434 }
12435
12436 elf_section_data (sec)->sreloc = reloc_sec;
12437 }
12438
12439 return reloc_sec;
12440 }