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