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