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