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