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