Automatic date update in version.in
[binutils-gdb.git] / bfd / elflink.c
1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2017 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
20
21 #include "sysdep.h"
22 #include "bfd.h"
23 #include "bfd_stdint.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 #if BFD_SUPPORTS_PLUGINS
32 #include "plugin-api.h"
33 #include "plugin.h"
34 #endif
35
36 /* This struct is used to pass information to routines called via
37 elf_link_hash_traverse which must return failure. */
38
39 struct elf_info_failed
40 {
41 struct bfd_link_info *info;
42 bfd_boolean failed;
43 };
44
45 /* This structure is used to pass information to
46 _bfd_elf_link_find_version_dependencies. */
47
48 struct elf_find_verdep_info
49 {
50 /* General link information. */
51 struct bfd_link_info *info;
52 /* The number of dependencies. */
53 unsigned int vers;
54 /* Whether we had a failure. */
55 bfd_boolean failed;
56 };
57
58 static bfd_boolean _bfd_elf_fix_symbol_flags
59 (struct elf_link_hash_entry *, struct elf_info_failed *);
60
61 asection *
62 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
63 unsigned long r_symndx,
64 bfd_boolean discard)
65 {
66 if (r_symndx >= cookie->locsymcount
67 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
68 {
69 struct elf_link_hash_entry *h;
70
71 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
72
73 while (h->root.type == bfd_link_hash_indirect
74 || h->root.type == bfd_link_hash_warning)
75 h = (struct elf_link_hash_entry *) h->root.u.i.link;
76
77 if ((h->root.type == bfd_link_hash_defined
78 || h->root.type == bfd_link_hash_defweak)
79 && discarded_section (h->root.u.def.section))
80 return h->root.u.def.section;
81 else
82 return NULL;
83 }
84 else
85 {
86 /* It's not a relocation against a global symbol,
87 but it could be a relocation against a local
88 symbol for a discarded section. */
89 asection *isec;
90 Elf_Internal_Sym *isym;
91
92 /* Need to: get the symbol; get the section. */
93 isym = &cookie->locsyms[r_symndx];
94 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
95 if (isec != NULL
96 && discard ? discarded_section (isec) : 1)
97 return isec;
98 }
99 return NULL;
100 }
101
102 /* Define a symbol in a dynamic linkage section. */
103
104 struct elf_link_hash_entry *
105 _bfd_elf_define_linkage_sym (bfd *abfd,
106 struct bfd_link_info *info,
107 asection *sec,
108 const char *name)
109 {
110 struct elf_link_hash_entry *h;
111 struct bfd_link_hash_entry *bh;
112 const struct elf_backend_data *bed;
113
114 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
115 if (h != NULL)
116 {
117 /* Zap symbol defined in an as-needed lib that wasn't linked.
118 This is a symptom of a larger problem: Absolute symbols
119 defined in shared libraries can't be overridden, because we
120 lose the link to the bfd which is via the symbol section. */
121 h->root.type = bfd_link_hash_new;
122 }
123
124 bh = &h->root;
125 bed = get_elf_backend_data (abfd);
126 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
127 sec, 0, NULL, FALSE, bed->collect,
128 &bh))
129 return NULL;
130 h = (struct elf_link_hash_entry *) bh;
131 h->def_regular = 1;
132 h->non_elf = 0;
133 h->root.linker_def = 1;
134 h->type = STT_OBJECT;
135 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
136 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
137
138 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
139 return h;
140 }
141
142 bfd_boolean
143 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
144 {
145 flagword flags;
146 asection *s;
147 struct elf_link_hash_entry *h;
148 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
149 struct elf_link_hash_table *htab = elf_hash_table (info);
150
151 /* This function may be called more than once. */
152 if (htab->sgot != NULL)
153 return TRUE;
154
155 flags = bed->dynamic_sec_flags;
156
157 s = bfd_make_section_anyway_with_flags (abfd,
158 (bed->rela_plts_and_copies_p
159 ? ".rela.got" : ".rel.got"),
160 (bed->dynamic_sec_flags
161 | SEC_READONLY));
162 if (s == NULL
163 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
164 return FALSE;
165 htab->srelgot = s;
166
167 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
168 if (s == NULL
169 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
170 return FALSE;
171 htab->sgot = s;
172
173 if (bed->want_got_plt)
174 {
175 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
176 if (s == NULL
177 || !bfd_set_section_alignment (abfd, s,
178 bed->s->log_file_align))
179 return FALSE;
180 htab->sgotplt = s;
181 }
182
183 /* The first bit of the global offset table is the header. */
184 s->size += bed->got_header_size;
185
186 if (bed->want_got_sym)
187 {
188 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
189 (or .got.plt) section. We don't do this in the linker script
190 because we don't want to define the symbol if we are not creating
191 a global offset table. */
192 h = _bfd_elf_define_linkage_sym (abfd, info, s,
193 "_GLOBAL_OFFSET_TABLE_");
194 elf_hash_table (info)->hgot = h;
195 if (h == NULL)
196 return FALSE;
197 }
198
199 return TRUE;
200 }
201 \f
202 /* Create a strtab to hold the dynamic symbol names. */
203 static bfd_boolean
204 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
205 {
206 struct elf_link_hash_table *hash_table;
207
208 hash_table = elf_hash_table (info);
209 if (hash_table->dynobj == NULL)
210 {
211 /* We may not set dynobj, an input file holding linker created
212 dynamic sections to abfd, which may be a dynamic object with
213 its own dynamic sections. We need to find a normal input file
214 to hold linker created sections if possible. */
215 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
216 {
217 bfd *ibfd;
218 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
219 if ((ibfd->flags
220 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0)
221 {
222 abfd = ibfd;
223 break;
224 }
225 }
226 hash_table->dynobj = abfd;
227 }
228
229 if (hash_table->dynstr == NULL)
230 {
231 hash_table->dynstr = _bfd_elf_strtab_init ();
232 if (hash_table->dynstr == NULL)
233 return FALSE;
234 }
235 return TRUE;
236 }
237
238 /* Create some sections which will be filled in with dynamic linking
239 information. ABFD is an input file which requires dynamic sections
240 to be created. The dynamic sections take up virtual memory space
241 when the final executable is run, so we need to create them before
242 addresses are assigned to the output sections. We work out the
243 actual contents and size of these sections later. */
244
245 bfd_boolean
246 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
247 {
248 flagword flags;
249 asection *s;
250 const struct elf_backend_data *bed;
251 struct elf_link_hash_entry *h;
252
253 if (! is_elf_hash_table (info->hash))
254 return FALSE;
255
256 if (elf_hash_table (info)->dynamic_sections_created)
257 return TRUE;
258
259 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
260 return FALSE;
261
262 abfd = elf_hash_table (info)->dynobj;
263 bed = get_elf_backend_data (abfd);
264
265 flags = bed->dynamic_sec_flags;
266
267 /* A dynamically linked executable has a .interp section, but a
268 shared library does not. */
269 if (bfd_link_executable (info) && !info->nointerp)
270 {
271 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
272 flags | SEC_READONLY);
273 if (s == NULL)
274 return FALSE;
275 }
276
277 /* Create sections to hold version informations. These are removed
278 if they are not needed. */
279 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
280 flags | SEC_READONLY);
281 if (s == NULL
282 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
283 return FALSE;
284
285 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
286 flags | SEC_READONLY);
287 if (s == NULL
288 || ! bfd_set_section_alignment (abfd, s, 1))
289 return FALSE;
290
291 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
292 flags | SEC_READONLY);
293 if (s == NULL
294 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
295 return FALSE;
296
297 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
298 flags | SEC_READONLY);
299 if (s == NULL
300 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
301 return FALSE;
302 elf_hash_table (info)->dynsym = s;
303
304 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
305 flags | SEC_READONLY);
306 if (s == NULL)
307 return FALSE;
308
309 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
310 if (s == NULL
311 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
312 return FALSE;
313
314 /* The special symbol _DYNAMIC is always set to the start of the
315 .dynamic section. We could set _DYNAMIC in a linker script, but we
316 only want to define it if we are, in fact, creating a .dynamic
317 section. We don't want to define it if there is no .dynamic
318 section, since on some ELF platforms the start up code examines it
319 to decide how to initialize the process. */
320 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
321 elf_hash_table (info)->hdynamic = h;
322 if (h == NULL)
323 return FALSE;
324
325 if (info->emit_hash)
326 {
327 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
328 flags | SEC_READONLY);
329 if (s == NULL
330 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
331 return FALSE;
332 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
333 }
334
335 if (info->emit_gnu_hash)
336 {
337 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
338 flags | SEC_READONLY);
339 if (s == NULL
340 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
341 return FALSE;
342 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
343 4 32-bit words followed by variable count of 64-bit words, then
344 variable count of 32-bit words. */
345 if (bed->s->arch_size == 64)
346 elf_section_data (s)->this_hdr.sh_entsize = 0;
347 else
348 elf_section_data (s)->this_hdr.sh_entsize = 4;
349 }
350
351 /* Let the backend create the rest of the sections. This lets the
352 backend set the right flags. The backend will normally create
353 the .got and .plt sections. */
354 if (bed->elf_backend_create_dynamic_sections == NULL
355 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
356 return FALSE;
357
358 elf_hash_table (info)->dynamic_sections_created = TRUE;
359
360 return TRUE;
361 }
362
363 /* Create dynamic sections when linking against a dynamic object. */
364
365 bfd_boolean
366 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
367 {
368 flagword flags, pltflags;
369 struct elf_link_hash_entry *h;
370 asection *s;
371 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
372 struct elf_link_hash_table *htab = elf_hash_table (info);
373
374 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
375 .rel[a].bss sections. */
376 flags = bed->dynamic_sec_flags;
377
378 pltflags = flags;
379 if (bed->plt_not_loaded)
380 /* We do not clear SEC_ALLOC here because we still want the OS to
381 allocate space for the section; it's just that there's nothing
382 to read in from the object file. */
383 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
384 else
385 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
386 if (bed->plt_readonly)
387 pltflags |= SEC_READONLY;
388
389 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
390 if (s == NULL
391 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
392 return FALSE;
393 htab->splt = s;
394
395 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
396 .plt section. */
397 if (bed->want_plt_sym)
398 {
399 h = _bfd_elf_define_linkage_sym (abfd, info, s,
400 "_PROCEDURE_LINKAGE_TABLE_");
401 elf_hash_table (info)->hplt = h;
402 if (h == NULL)
403 return FALSE;
404 }
405
406 s = bfd_make_section_anyway_with_flags (abfd,
407 (bed->rela_plts_and_copies_p
408 ? ".rela.plt" : ".rel.plt"),
409 flags | SEC_READONLY);
410 if (s == NULL
411 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
412 return FALSE;
413 htab->srelplt = s;
414
415 if (! _bfd_elf_create_got_section (abfd, info))
416 return FALSE;
417
418 if (bed->want_dynbss)
419 {
420 /* The .dynbss section is a place to put symbols which are defined
421 by dynamic objects, are referenced by regular objects, and are
422 not functions. We must allocate space for them in the process
423 image and use a R_*_COPY reloc to tell the dynamic linker to
424 initialize them at run time. The linker script puts the .dynbss
425 section into the .bss section of the final image. */
426 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
427 (SEC_ALLOC | SEC_LINKER_CREATED));
428 if (s == NULL)
429 return FALSE;
430 htab->sdynbss = s;
431
432 if (bed->want_dynrelro)
433 {
434 /* Similarly, but for symbols that were originally in read-only
435 sections. */
436 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
437 (SEC_ALLOC | SEC_READONLY
438 | SEC_HAS_CONTENTS
439 | SEC_LINKER_CREATED));
440 if (s == NULL)
441 return FALSE;
442 htab->sdynrelro = s;
443 }
444
445 /* The .rel[a].bss section holds copy relocs. This section is not
446 normally needed. We need to create it here, though, so that the
447 linker will map it to an output section. We can't just create it
448 only if we need it, because we will not know whether we need it
449 until we have seen all the input files, and the first time the
450 main linker code calls BFD after examining all the input files
451 (size_dynamic_sections) the input sections have already been
452 mapped to the output sections. If the section turns out not to
453 be needed, we can discard it later. We will never need this
454 section when generating a shared object, since they do not use
455 copy relocs. */
456 if (bfd_link_executable (info))
457 {
458 s = bfd_make_section_anyway_with_flags (abfd,
459 (bed->rela_plts_and_copies_p
460 ? ".rela.bss" : ".rel.bss"),
461 flags | SEC_READONLY);
462 if (s == NULL
463 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
464 return FALSE;
465 htab->srelbss = s;
466
467 if (bed->want_dynrelro)
468 {
469 s = (bfd_make_section_anyway_with_flags
470 (abfd, (bed->rela_plts_and_copies_p
471 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
472 flags | SEC_READONLY));
473 if (s == NULL
474 || ! bfd_set_section_alignment (abfd, s,
475 bed->s->log_file_align))
476 return FALSE;
477 htab->sreldynrelro = s;
478 }
479 }
480 }
481
482 return TRUE;
483 }
484 \f
485 /* Record a new dynamic symbol. We record the dynamic symbols as we
486 read the input files, since we need to have a list of all of them
487 before we can determine the final sizes of the output sections.
488 Note that we may actually call this function even though we are not
489 going to output any dynamic symbols; in some cases we know that a
490 symbol should be in the dynamic symbol table, but only if there is
491 one. */
492
493 bfd_boolean
494 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
495 struct elf_link_hash_entry *h)
496 {
497 if (h->dynindx == -1)
498 {
499 struct elf_strtab_hash *dynstr;
500 char *p;
501 const char *name;
502 size_t indx;
503
504 /* XXX: The ABI draft says the linker must turn hidden and
505 internal symbols into STB_LOCAL symbols when producing the
506 DSO. However, if ld.so honors st_other in the dynamic table,
507 this would not be necessary. */
508 switch (ELF_ST_VISIBILITY (h->other))
509 {
510 case STV_INTERNAL:
511 case STV_HIDDEN:
512 if (h->root.type != bfd_link_hash_undefined
513 && h->root.type != bfd_link_hash_undefweak)
514 {
515 h->forced_local = 1;
516 if (!elf_hash_table (info)->is_relocatable_executable)
517 return TRUE;
518 }
519
520 default:
521 break;
522 }
523
524 h->dynindx = elf_hash_table (info)->dynsymcount;
525 ++elf_hash_table (info)->dynsymcount;
526
527 dynstr = elf_hash_table (info)->dynstr;
528 if (dynstr == NULL)
529 {
530 /* Create a strtab to hold the dynamic symbol names. */
531 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
532 if (dynstr == NULL)
533 return FALSE;
534 }
535
536 /* We don't put any version information in the dynamic string
537 table. */
538 name = h->root.root.string;
539 p = strchr (name, ELF_VER_CHR);
540 if (p != NULL)
541 /* We know that the p points into writable memory. In fact,
542 there are only a few symbols that have read-only names, being
543 those like _GLOBAL_OFFSET_TABLE_ that are created specially
544 by the backends. Most symbols will have names pointing into
545 an ELF string table read from a file, or to objalloc memory. */
546 *p = 0;
547
548 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
549
550 if (p != NULL)
551 *p = ELF_VER_CHR;
552
553 if (indx == (size_t) -1)
554 return FALSE;
555 h->dynstr_index = indx;
556 }
557
558 return TRUE;
559 }
560 \f
561 /* Mark a symbol dynamic. */
562
563 static void
564 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
565 struct elf_link_hash_entry *h,
566 Elf_Internal_Sym *sym)
567 {
568 struct bfd_elf_dynamic_list *d = info->dynamic_list;
569
570 /* It may be called more than once on the same H. */
571 if(h->dynamic || bfd_link_relocatable (info))
572 return;
573
574 if ((info->dynamic_data
575 && (h->type == STT_OBJECT
576 || h->type == STT_COMMON
577 || (sym != NULL
578 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
579 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
580 || (d != NULL
581 && h->root.type == bfd_link_hash_new
582 && (*d->match) (&d->head, NULL, h->root.root.string)))
583 h->dynamic = 1;
584 }
585
586 /* Record an assignment to a symbol made by a linker script. We need
587 this in case some dynamic object refers to this symbol. */
588
589 bfd_boolean
590 bfd_elf_record_link_assignment (bfd *output_bfd,
591 struct bfd_link_info *info,
592 const char *name,
593 bfd_boolean provide,
594 bfd_boolean hidden)
595 {
596 struct elf_link_hash_entry *h, *hv;
597 struct elf_link_hash_table *htab;
598 const struct elf_backend_data *bed;
599
600 if (!is_elf_hash_table (info->hash))
601 return TRUE;
602
603 htab = elf_hash_table (info);
604 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
605 if (h == NULL)
606 return provide;
607
608 if (h->root.type == bfd_link_hash_warning)
609 h = (struct elf_link_hash_entry *) h->root.u.i.link;
610
611 if (h->versioned == unknown)
612 {
613 /* Set versioned if symbol version is unknown. */
614 char *version = strrchr (name, ELF_VER_CHR);
615 if (version)
616 {
617 if (version > name && version[-1] != ELF_VER_CHR)
618 h->versioned = versioned_hidden;
619 else
620 h->versioned = versioned;
621 }
622 }
623
624 switch (h->root.type)
625 {
626 case bfd_link_hash_defined:
627 case bfd_link_hash_defweak:
628 case bfd_link_hash_common:
629 break;
630 case bfd_link_hash_undefweak:
631 case bfd_link_hash_undefined:
632 /* Since we're defining the symbol, don't let it seem to have not
633 been defined. record_dynamic_symbol and size_dynamic_sections
634 may depend on this. */
635 h->root.type = bfd_link_hash_new;
636 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
637 bfd_link_repair_undef_list (&htab->root);
638 break;
639 case bfd_link_hash_new:
640 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
641 h->non_elf = 0;
642 break;
643 case bfd_link_hash_indirect:
644 /* We had a versioned symbol in a dynamic library. We make the
645 the versioned symbol point to this one. */
646 bed = get_elf_backend_data (output_bfd);
647 hv = h;
648 while (hv->root.type == bfd_link_hash_indirect
649 || hv->root.type == bfd_link_hash_warning)
650 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
651 /* We don't need to update h->root.u since linker will set them
652 later. */
653 h->root.type = bfd_link_hash_undefined;
654 hv->root.type = bfd_link_hash_indirect;
655 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
656 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
657 break;
658 default:
659 BFD_FAIL ();
660 return FALSE;
661 }
662
663 /* If this symbol is being provided by the linker script, and it is
664 currently defined by a dynamic object, but not by a regular
665 object, then mark it as undefined so that the generic linker will
666 force the correct value. */
667 if (provide
668 && h->def_dynamic
669 && !h->def_regular)
670 h->root.type = bfd_link_hash_undefined;
671
672 /* If this symbol is not being provided by the linker script, and it is
673 currently defined by a dynamic object, but not by a regular object,
674 then undo any forced local marking that may have been set by input
675 section garbage collection and clear out any version information
676 because the symbol will not be associated with the dynamic object
677 any more. */
678 if (!provide
679 && h->def_dynamic
680 && !h->def_regular)
681 {
682 h->forced_local = 0;
683 h->verinfo.verdef = NULL;
684 }
685
686 h->def_regular = 1;
687
688 if (hidden)
689 {
690 bed = get_elf_backend_data (output_bfd);
691 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
692 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
693 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
694 }
695
696 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
697 and executables. */
698 if (!bfd_link_relocatable (info)
699 && h->dynindx != -1
700 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
701 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
702 h->forced_local = 1;
703
704 if ((h->def_dynamic
705 || h->ref_dynamic
706 || bfd_link_dll (info)
707 || elf_hash_table (info)->is_relocatable_executable)
708 && h->dynindx == -1)
709 {
710 if (! bfd_elf_link_record_dynamic_symbol (info, h))
711 return FALSE;
712
713 /* If this is a weak defined symbol, and we know a corresponding
714 real symbol from the same dynamic object, make sure the real
715 symbol is also made into a dynamic symbol. */
716 if (h->u.weakdef != NULL
717 && h->u.weakdef->dynindx == -1)
718 {
719 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
720 return FALSE;
721 }
722 }
723
724 return TRUE;
725 }
726
727 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
728 success, and 2 on a failure caused by attempting to record a symbol
729 in a discarded section, eg. a discarded link-once section symbol. */
730
731 int
732 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
733 bfd *input_bfd,
734 long input_indx)
735 {
736 bfd_size_type amt;
737 struct elf_link_local_dynamic_entry *entry;
738 struct elf_link_hash_table *eht;
739 struct elf_strtab_hash *dynstr;
740 size_t dynstr_index;
741 char *name;
742 Elf_External_Sym_Shndx eshndx;
743 char esym[sizeof (Elf64_External_Sym)];
744
745 if (! is_elf_hash_table (info->hash))
746 return 0;
747
748 /* See if the entry exists already. */
749 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
750 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
751 return 1;
752
753 amt = sizeof (*entry);
754 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
755 if (entry == NULL)
756 return 0;
757
758 /* Go find the symbol, so that we can find it's name. */
759 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
760 1, input_indx, &entry->isym, esym, &eshndx))
761 {
762 bfd_release (input_bfd, entry);
763 return 0;
764 }
765
766 if (entry->isym.st_shndx != SHN_UNDEF
767 && entry->isym.st_shndx < SHN_LORESERVE)
768 {
769 asection *s;
770
771 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
772 if (s == NULL || bfd_is_abs_section (s->output_section))
773 {
774 /* We can still bfd_release here as nothing has done another
775 bfd_alloc. We can't do this later in this function. */
776 bfd_release (input_bfd, entry);
777 return 2;
778 }
779 }
780
781 name = (bfd_elf_string_from_elf_section
782 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
783 entry->isym.st_name));
784
785 dynstr = elf_hash_table (info)->dynstr;
786 if (dynstr == NULL)
787 {
788 /* Create a strtab to hold the dynamic symbol names. */
789 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
790 if (dynstr == NULL)
791 return 0;
792 }
793
794 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
795 if (dynstr_index == (size_t) -1)
796 return 0;
797 entry->isym.st_name = dynstr_index;
798
799 eht = elf_hash_table (info);
800
801 entry->next = eht->dynlocal;
802 eht->dynlocal = entry;
803 entry->input_bfd = input_bfd;
804 entry->input_indx = input_indx;
805 eht->dynsymcount++;
806
807 /* Whatever binding the symbol had before, it's now local. */
808 entry->isym.st_info
809 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
810
811 /* The dynindx will be set at the end of size_dynamic_sections. */
812
813 return 1;
814 }
815
816 /* Return the dynindex of a local dynamic symbol. */
817
818 long
819 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
820 bfd *input_bfd,
821 long input_indx)
822 {
823 struct elf_link_local_dynamic_entry *e;
824
825 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
826 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
827 return e->dynindx;
828 return -1;
829 }
830
831 /* This function is used to renumber the dynamic symbols, if some of
832 them are removed because they are marked as local. This is called
833 via elf_link_hash_traverse. */
834
835 static bfd_boolean
836 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
837 void *data)
838 {
839 size_t *count = (size_t *) data;
840
841 if (h->forced_local)
842 return TRUE;
843
844 if (h->dynindx != -1)
845 h->dynindx = ++(*count);
846
847 return TRUE;
848 }
849
850
851 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
852 STB_LOCAL binding. */
853
854 static bfd_boolean
855 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
856 void *data)
857 {
858 size_t *count = (size_t *) data;
859
860 if (!h->forced_local)
861 return TRUE;
862
863 if (h->dynindx != -1)
864 h->dynindx = ++(*count);
865
866 return TRUE;
867 }
868
869 /* Return true if the dynamic symbol for a given section should be
870 omitted when creating a shared library. */
871 bfd_boolean
872 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
873 struct bfd_link_info *info,
874 asection *p)
875 {
876 struct elf_link_hash_table *htab;
877 asection *ip;
878
879 switch (elf_section_data (p)->this_hdr.sh_type)
880 {
881 case SHT_PROGBITS:
882 case SHT_NOBITS:
883 /* If sh_type is yet undecided, assume it could be
884 SHT_PROGBITS/SHT_NOBITS. */
885 case SHT_NULL:
886 htab = elf_hash_table (info);
887 if (p == htab->tls_sec)
888 return FALSE;
889
890 if (htab->text_index_section != NULL)
891 return p != htab->text_index_section && p != htab->data_index_section;
892
893 return (htab->dynobj != NULL
894 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
895 && ip->output_section == p);
896
897 /* There shouldn't be section relative relocations
898 against any other section. */
899 default:
900 return TRUE;
901 }
902 }
903
904 /* Assign dynsym indices. In a shared library we generate a section
905 symbol for each output section, which come first. Next come symbols
906 which have been forced to local binding. Then all of the back-end
907 allocated local dynamic syms, followed by the rest of the global
908 symbols. */
909
910 static unsigned long
911 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
912 struct bfd_link_info *info,
913 unsigned long *section_sym_count)
914 {
915 unsigned long dynsymcount = 0;
916
917 if (bfd_link_pic (info)
918 || elf_hash_table (info)->is_relocatable_executable)
919 {
920 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
921 asection *p;
922 for (p = output_bfd->sections; p ; p = p->next)
923 if ((p->flags & SEC_EXCLUDE) == 0
924 && (p->flags & SEC_ALLOC) != 0
925 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
926 elf_section_data (p)->dynindx = ++dynsymcount;
927 else
928 elf_section_data (p)->dynindx = 0;
929 }
930 *section_sym_count = dynsymcount;
931
932 elf_link_hash_traverse (elf_hash_table (info),
933 elf_link_renumber_local_hash_table_dynsyms,
934 &dynsymcount);
935
936 if (elf_hash_table (info)->dynlocal)
937 {
938 struct elf_link_local_dynamic_entry *p;
939 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
940 p->dynindx = ++dynsymcount;
941 }
942 elf_hash_table (info)->local_dynsymcount = dynsymcount;
943
944 elf_link_hash_traverse (elf_hash_table (info),
945 elf_link_renumber_hash_table_dynsyms,
946 &dynsymcount);
947
948 /* There is an unused NULL entry at the head of the table which we
949 must account for in our count even if the table is empty since it
950 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
951 .dynamic section. */
952 dynsymcount++;
953
954 elf_hash_table (info)->dynsymcount = dynsymcount;
955 return dynsymcount;
956 }
957
958 /* Merge st_other field. */
959
960 static void
961 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
962 const Elf_Internal_Sym *isym, asection *sec,
963 bfd_boolean definition, bfd_boolean dynamic)
964 {
965 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
966
967 /* If st_other has a processor-specific meaning, specific
968 code might be needed here. */
969 if (bed->elf_backend_merge_symbol_attribute)
970 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
971 dynamic);
972
973 if (!dynamic)
974 {
975 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
976 unsigned hvis = ELF_ST_VISIBILITY (h->other);
977
978 /* Keep the most constraining visibility. Leave the remainder
979 of the st_other field to elf_backend_merge_symbol_attribute. */
980 if (symvis - 1 < hvis - 1)
981 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
982 }
983 else if (definition
984 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
985 && (sec->flags & SEC_READONLY) == 0)
986 h->protected_def = 1;
987 }
988
989 /* This function is called when we want to merge a new symbol with an
990 existing symbol. It handles the various cases which arise when we
991 find a definition in a dynamic object, or when there is already a
992 definition in a dynamic object. The new symbol is described by
993 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
994 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
995 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
996 of an old common symbol. We set OVERRIDE if the old symbol is
997 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
998 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
999 to change. By OK to change, we mean that we shouldn't warn if the
1000 type or size does change. */
1001
1002 static bfd_boolean
1003 _bfd_elf_merge_symbol (bfd *abfd,
1004 struct bfd_link_info *info,
1005 const char *name,
1006 Elf_Internal_Sym *sym,
1007 asection **psec,
1008 bfd_vma *pvalue,
1009 struct elf_link_hash_entry **sym_hash,
1010 bfd **poldbfd,
1011 bfd_boolean *pold_weak,
1012 unsigned int *pold_alignment,
1013 bfd_boolean *skip,
1014 bfd_boolean *override,
1015 bfd_boolean *type_change_ok,
1016 bfd_boolean *size_change_ok,
1017 bfd_boolean *matched)
1018 {
1019 asection *sec, *oldsec;
1020 struct elf_link_hash_entry *h;
1021 struct elf_link_hash_entry *hi;
1022 struct elf_link_hash_entry *flip;
1023 int bind;
1024 bfd *oldbfd;
1025 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1026 bfd_boolean newweak, oldweak, newfunc, oldfunc;
1027 const struct elf_backend_data *bed;
1028 char *new_version;
1029
1030 *skip = FALSE;
1031 *override = FALSE;
1032
1033 sec = *psec;
1034 bind = ELF_ST_BIND (sym->st_info);
1035
1036 if (! bfd_is_und_section (sec))
1037 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1038 else
1039 h = ((struct elf_link_hash_entry *)
1040 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1041 if (h == NULL)
1042 return FALSE;
1043 *sym_hash = h;
1044
1045 bed = get_elf_backend_data (abfd);
1046
1047 /* NEW_VERSION is the symbol version of the new symbol. */
1048 if (h->versioned != unversioned)
1049 {
1050 /* Symbol version is unknown or versioned. */
1051 new_version = strrchr (name, ELF_VER_CHR);
1052 if (new_version)
1053 {
1054 if (h->versioned == unknown)
1055 {
1056 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1057 h->versioned = versioned_hidden;
1058 else
1059 h->versioned = versioned;
1060 }
1061 new_version += 1;
1062 if (new_version[0] == '\0')
1063 new_version = NULL;
1064 }
1065 else
1066 h->versioned = unversioned;
1067 }
1068 else
1069 new_version = NULL;
1070
1071 /* For merging, we only care about real symbols. But we need to make
1072 sure that indirect symbol dynamic flags are updated. */
1073 hi = h;
1074 while (h->root.type == bfd_link_hash_indirect
1075 || h->root.type == bfd_link_hash_warning)
1076 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1077
1078 if (!*matched)
1079 {
1080 if (hi == h || h->root.type == bfd_link_hash_new)
1081 *matched = TRUE;
1082 else
1083 {
1084 /* OLD_HIDDEN is true if the existing symbol is only visible
1085 to the symbol with the same symbol version. NEW_HIDDEN is
1086 true if the new symbol is only visible to the symbol with
1087 the same symbol version. */
1088 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1089 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1090 if (!old_hidden && !new_hidden)
1091 /* The new symbol matches the existing symbol if both
1092 aren't hidden. */
1093 *matched = TRUE;
1094 else
1095 {
1096 /* OLD_VERSION is the symbol version of the existing
1097 symbol. */
1098 char *old_version;
1099
1100 if (h->versioned >= versioned)
1101 old_version = strrchr (h->root.root.string,
1102 ELF_VER_CHR) + 1;
1103 else
1104 old_version = NULL;
1105
1106 /* The new symbol matches the existing symbol if they
1107 have the same symbol version. */
1108 *matched = (old_version == new_version
1109 || (old_version != NULL
1110 && new_version != NULL
1111 && strcmp (old_version, new_version) == 0));
1112 }
1113 }
1114 }
1115
1116 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1117 existing symbol. */
1118
1119 oldbfd = NULL;
1120 oldsec = NULL;
1121 switch (h->root.type)
1122 {
1123 default:
1124 break;
1125
1126 case bfd_link_hash_undefined:
1127 case bfd_link_hash_undefweak:
1128 oldbfd = h->root.u.undef.abfd;
1129 break;
1130
1131 case bfd_link_hash_defined:
1132 case bfd_link_hash_defweak:
1133 oldbfd = h->root.u.def.section->owner;
1134 oldsec = h->root.u.def.section;
1135 break;
1136
1137 case bfd_link_hash_common:
1138 oldbfd = h->root.u.c.p->section->owner;
1139 oldsec = h->root.u.c.p->section;
1140 if (pold_alignment)
1141 *pold_alignment = h->root.u.c.p->alignment_power;
1142 break;
1143 }
1144 if (poldbfd && *poldbfd == NULL)
1145 *poldbfd = oldbfd;
1146
1147 /* Differentiate strong and weak symbols. */
1148 newweak = bind == STB_WEAK;
1149 oldweak = (h->root.type == bfd_link_hash_defweak
1150 || h->root.type == bfd_link_hash_undefweak);
1151 if (pold_weak)
1152 *pold_weak = oldweak;
1153
1154 /* This code is for coping with dynamic objects, and is only useful
1155 if we are doing an ELF link. */
1156 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1157 return TRUE;
1158
1159 /* We have to check it for every instance since the first few may be
1160 references and not all compilers emit symbol type for undefined
1161 symbols. */
1162 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1163
1164 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1165 respectively, is from a dynamic object. */
1166
1167 newdyn = (abfd->flags & DYNAMIC) != 0;
1168
1169 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1170 syms and defined syms in dynamic libraries respectively.
1171 ref_dynamic on the other hand can be set for a symbol defined in
1172 a dynamic library, and def_dynamic may not be set; When the
1173 definition in a dynamic lib is overridden by a definition in the
1174 executable use of the symbol in the dynamic lib becomes a
1175 reference to the executable symbol. */
1176 if (newdyn)
1177 {
1178 if (bfd_is_und_section (sec))
1179 {
1180 if (bind != STB_WEAK)
1181 {
1182 h->ref_dynamic_nonweak = 1;
1183 hi->ref_dynamic_nonweak = 1;
1184 }
1185 }
1186 else
1187 {
1188 /* Update the existing symbol only if they match. */
1189 if (*matched)
1190 h->dynamic_def = 1;
1191 hi->dynamic_def = 1;
1192 }
1193 }
1194
1195 /* If we just created the symbol, mark it as being an ELF symbol.
1196 Other than that, there is nothing to do--there is no merge issue
1197 with a newly defined symbol--so we just return. */
1198
1199 if (h->root.type == bfd_link_hash_new)
1200 {
1201 h->non_elf = 0;
1202 return TRUE;
1203 }
1204
1205 /* In cases involving weak versioned symbols, we may wind up trying
1206 to merge a symbol with itself. Catch that here, to avoid the
1207 confusion that results if we try to override a symbol with
1208 itself. The additional tests catch cases like
1209 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1210 dynamic object, which we do want to handle here. */
1211 if (abfd == oldbfd
1212 && (newweak || oldweak)
1213 && ((abfd->flags & DYNAMIC) == 0
1214 || !h->def_regular))
1215 return TRUE;
1216
1217 olddyn = FALSE;
1218 if (oldbfd != NULL)
1219 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1220 else if (oldsec != NULL)
1221 {
1222 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1223 indices used by MIPS ELF. */
1224 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1225 }
1226
1227 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1228 respectively, appear to be a definition rather than reference. */
1229
1230 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1231
1232 olddef = (h->root.type != bfd_link_hash_undefined
1233 && h->root.type != bfd_link_hash_undefweak
1234 && h->root.type != bfd_link_hash_common);
1235
1236 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1237 respectively, appear to be a function. */
1238
1239 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1240 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1241
1242 oldfunc = (h->type != STT_NOTYPE
1243 && bed->is_function_type (h->type));
1244
1245 /* If creating a default indirect symbol ("foo" or "foo@") from a
1246 dynamic versioned definition ("foo@@") skip doing so if there is
1247 an existing regular definition with a different type. We don't
1248 want, for example, a "time" variable in the executable overriding
1249 a "time" function in a shared library. */
1250 if (pold_alignment == NULL
1251 && newdyn
1252 && newdef
1253 && !olddyn
1254 && (olddef || h->root.type == bfd_link_hash_common)
1255 && ELF_ST_TYPE (sym->st_info) != h->type
1256 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1257 && h->type != STT_NOTYPE
1258 && !(newfunc && oldfunc))
1259 {
1260 *skip = TRUE;
1261 return TRUE;
1262 }
1263
1264 /* Check TLS symbols. We don't check undefined symbols introduced
1265 by "ld -u" which have no type (and oldbfd NULL), and we don't
1266 check symbols from plugins because they also have no type. */
1267 if (oldbfd != NULL
1268 && (oldbfd->flags & BFD_PLUGIN) == 0
1269 && (abfd->flags & BFD_PLUGIN) == 0
1270 && ELF_ST_TYPE (sym->st_info) != h->type
1271 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1272 {
1273 bfd *ntbfd, *tbfd;
1274 bfd_boolean ntdef, tdef;
1275 asection *ntsec, *tsec;
1276
1277 if (h->type == STT_TLS)
1278 {
1279 ntbfd = abfd;
1280 ntsec = sec;
1281 ntdef = newdef;
1282 tbfd = oldbfd;
1283 tsec = oldsec;
1284 tdef = olddef;
1285 }
1286 else
1287 {
1288 ntbfd = oldbfd;
1289 ntsec = oldsec;
1290 ntdef = olddef;
1291 tbfd = abfd;
1292 tsec = sec;
1293 tdef = newdef;
1294 }
1295
1296 if (tdef && ntdef)
1297 _bfd_error_handler
1298 /* xgettext:c-format */
1299 (_("%s: TLS definition in %B section %A "
1300 "mismatches non-TLS definition in %B section %A"),
1301 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1302 else if (!tdef && !ntdef)
1303 _bfd_error_handler
1304 /* xgettext:c-format */
1305 (_("%s: TLS reference in %B "
1306 "mismatches non-TLS reference in %B"),
1307 tbfd, ntbfd, h->root.root.string);
1308 else if (tdef)
1309 _bfd_error_handler
1310 /* xgettext:c-format */
1311 (_("%s: TLS definition in %B section %A "
1312 "mismatches non-TLS reference in %B"),
1313 tbfd, tsec, ntbfd, h->root.root.string);
1314 else
1315 _bfd_error_handler
1316 /* xgettext:c-format */
1317 (_("%s: TLS reference in %B "
1318 "mismatches non-TLS definition in %B section %A"),
1319 tbfd, ntbfd, ntsec, h->root.root.string);
1320
1321 bfd_set_error (bfd_error_bad_value);
1322 return FALSE;
1323 }
1324
1325 /* If the old symbol has non-default visibility, we ignore the new
1326 definition from a dynamic object. */
1327 if (newdyn
1328 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1329 && !bfd_is_und_section (sec))
1330 {
1331 *skip = TRUE;
1332 /* Make sure this symbol is dynamic. */
1333 h->ref_dynamic = 1;
1334 hi->ref_dynamic = 1;
1335 /* A protected symbol has external availability. Make sure it is
1336 recorded as dynamic.
1337
1338 FIXME: Should we check type and size for protected symbol? */
1339 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1340 return bfd_elf_link_record_dynamic_symbol (info, h);
1341 else
1342 return TRUE;
1343 }
1344 else if (!newdyn
1345 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1346 && h->def_dynamic)
1347 {
1348 /* If the new symbol with non-default visibility comes from a
1349 relocatable file and the old definition comes from a dynamic
1350 object, we remove the old definition. */
1351 if (hi->root.type == bfd_link_hash_indirect)
1352 {
1353 /* Handle the case where the old dynamic definition is
1354 default versioned. We need to copy the symbol info from
1355 the symbol with default version to the normal one if it
1356 was referenced before. */
1357 if (h->ref_regular)
1358 {
1359 hi->root.type = h->root.type;
1360 h->root.type = bfd_link_hash_indirect;
1361 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1362
1363 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1364 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1365 {
1366 /* If the new symbol is hidden or internal, completely undo
1367 any dynamic link state. */
1368 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1369 h->forced_local = 0;
1370 h->ref_dynamic = 0;
1371 }
1372 else
1373 h->ref_dynamic = 1;
1374
1375 h->def_dynamic = 0;
1376 /* FIXME: Should we check type and size for protected symbol? */
1377 h->size = 0;
1378 h->type = 0;
1379
1380 h = hi;
1381 }
1382 else
1383 h = hi;
1384 }
1385
1386 /* If the old symbol was undefined before, then it will still be
1387 on the undefs list. If the new symbol is undefined or
1388 common, we can't make it bfd_link_hash_new here, because new
1389 undefined or common symbols will be added to the undefs list
1390 by _bfd_generic_link_add_one_symbol. Symbols may not be
1391 added twice to the undefs list. Also, if the new symbol is
1392 undefweak then we don't want to lose the strong undef. */
1393 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1394 {
1395 h->root.type = bfd_link_hash_undefined;
1396 h->root.u.undef.abfd = abfd;
1397 }
1398 else
1399 {
1400 h->root.type = bfd_link_hash_new;
1401 h->root.u.undef.abfd = NULL;
1402 }
1403
1404 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1405 {
1406 /* If the new symbol is hidden or internal, completely undo
1407 any dynamic link state. */
1408 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1409 h->forced_local = 0;
1410 h->ref_dynamic = 0;
1411 }
1412 else
1413 h->ref_dynamic = 1;
1414 h->def_dynamic = 0;
1415 /* FIXME: Should we check type and size for protected symbol? */
1416 h->size = 0;
1417 h->type = 0;
1418 return TRUE;
1419 }
1420
1421 /* If a new weak symbol definition comes from a regular file and the
1422 old symbol comes from a dynamic library, we treat the new one as
1423 strong. Similarly, an old weak symbol definition from a regular
1424 file is treated as strong when the new symbol comes from a dynamic
1425 library. Further, an old weak symbol from a dynamic library is
1426 treated as strong if the new symbol is from a dynamic library.
1427 This reflects the way glibc's ld.so works.
1428
1429 Do this before setting *type_change_ok or *size_change_ok so that
1430 we warn properly when dynamic library symbols are overridden. */
1431
1432 if (newdef && !newdyn && olddyn)
1433 newweak = FALSE;
1434 if (olddef && newdyn)
1435 oldweak = FALSE;
1436
1437 /* Allow changes between different types of function symbol. */
1438 if (newfunc && oldfunc)
1439 *type_change_ok = TRUE;
1440
1441 /* It's OK to change the type if either the existing symbol or the
1442 new symbol is weak. A type change is also OK if the old symbol
1443 is undefined and the new symbol is defined. */
1444
1445 if (oldweak
1446 || newweak
1447 || (newdef
1448 && h->root.type == bfd_link_hash_undefined))
1449 *type_change_ok = TRUE;
1450
1451 /* It's OK to change the size if either the existing symbol or the
1452 new symbol is weak, or if the old symbol is undefined. */
1453
1454 if (*type_change_ok
1455 || h->root.type == bfd_link_hash_undefined)
1456 *size_change_ok = TRUE;
1457
1458 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1459 symbol, respectively, appears to be a common symbol in a dynamic
1460 object. If a symbol appears in an uninitialized section, and is
1461 not weak, and is not a function, then it may be a common symbol
1462 which was resolved when the dynamic object was created. We want
1463 to treat such symbols specially, because they raise special
1464 considerations when setting the symbol size: if the symbol
1465 appears as a common symbol in a regular object, and the size in
1466 the regular object is larger, we must make sure that we use the
1467 larger size. This problematic case can always be avoided in C,
1468 but it must be handled correctly when using Fortran shared
1469 libraries.
1470
1471 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1472 likewise for OLDDYNCOMMON and OLDDEF.
1473
1474 Note that this test is just a heuristic, and that it is quite
1475 possible to have an uninitialized symbol in a shared object which
1476 is really a definition, rather than a common symbol. This could
1477 lead to some minor confusion when the symbol really is a common
1478 symbol in some regular object. However, I think it will be
1479 harmless. */
1480
1481 if (newdyn
1482 && newdef
1483 && !newweak
1484 && (sec->flags & SEC_ALLOC) != 0
1485 && (sec->flags & SEC_LOAD) == 0
1486 && sym->st_size > 0
1487 && !newfunc)
1488 newdyncommon = TRUE;
1489 else
1490 newdyncommon = FALSE;
1491
1492 if (olddyn
1493 && olddef
1494 && h->root.type == bfd_link_hash_defined
1495 && h->def_dynamic
1496 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1497 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1498 && h->size > 0
1499 && !oldfunc)
1500 olddyncommon = TRUE;
1501 else
1502 olddyncommon = FALSE;
1503
1504 /* We now know everything about the old and new symbols. We ask the
1505 backend to check if we can merge them. */
1506 if (bed->merge_symbol != NULL)
1507 {
1508 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1509 return FALSE;
1510 sec = *psec;
1511 }
1512
1513 /* If both the old and the new symbols look like common symbols in a
1514 dynamic object, set the size of the symbol to the larger of the
1515 two. */
1516
1517 if (olddyncommon
1518 && newdyncommon
1519 && sym->st_size != h->size)
1520 {
1521 /* Since we think we have two common symbols, issue a multiple
1522 common warning if desired. Note that we only warn if the
1523 size is different. If the size is the same, we simply let
1524 the old symbol override the new one as normally happens with
1525 symbols defined in dynamic objects. */
1526
1527 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1528 bfd_link_hash_common, sym->st_size);
1529 if (sym->st_size > h->size)
1530 h->size = sym->st_size;
1531
1532 *size_change_ok = TRUE;
1533 }
1534
1535 /* If we are looking at a dynamic object, and we have found a
1536 definition, we need to see if the symbol was already defined by
1537 some other object. If so, we want to use the existing
1538 definition, and we do not want to report a multiple symbol
1539 definition error; we do this by clobbering *PSEC to be
1540 bfd_und_section_ptr.
1541
1542 We treat a common symbol as a definition if the symbol in the
1543 shared library is a function, since common symbols always
1544 represent variables; this can cause confusion in principle, but
1545 any such confusion would seem to indicate an erroneous program or
1546 shared library. We also permit a common symbol in a regular
1547 object to override a weak symbol in a shared object. A common
1548 symbol in executable also overrides a symbol in a shared object. */
1549
1550 if (newdyn
1551 && newdef
1552 && (olddef
1553 || (h->root.type == bfd_link_hash_common
1554 && (newweak
1555 || newfunc
1556 || (!olddyn && bfd_link_executable (info))))))
1557 {
1558 *override = TRUE;
1559 newdef = FALSE;
1560 newdyncommon = FALSE;
1561
1562 *psec = sec = bfd_und_section_ptr;
1563 *size_change_ok = TRUE;
1564
1565 /* If we get here when the old symbol is a common symbol, then
1566 we are explicitly letting it override a weak symbol or
1567 function in a dynamic object, and we don't want to warn about
1568 a type change. If the old symbol is a defined symbol, a type
1569 change warning may still be appropriate. */
1570
1571 if (h->root.type == bfd_link_hash_common)
1572 *type_change_ok = TRUE;
1573 }
1574
1575 /* Handle the special case of an old common symbol merging with a
1576 new symbol which looks like a common symbol in a shared object.
1577 We change *PSEC and *PVALUE to make the new symbol look like a
1578 common symbol, and let _bfd_generic_link_add_one_symbol do the
1579 right thing. */
1580
1581 if (newdyncommon
1582 && h->root.type == bfd_link_hash_common)
1583 {
1584 *override = TRUE;
1585 newdef = FALSE;
1586 newdyncommon = FALSE;
1587 *pvalue = sym->st_size;
1588 *psec = sec = bed->common_section (oldsec);
1589 *size_change_ok = TRUE;
1590 }
1591
1592 /* Skip weak definitions of symbols that are already defined. */
1593 if (newdef && olddef && newweak)
1594 {
1595 /* Don't skip new non-IR weak syms. */
1596 if (!(oldbfd != NULL
1597 && (oldbfd->flags & BFD_PLUGIN) != 0
1598 && (abfd->flags & BFD_PLUGIN) == 0))
1599 {
1600 newdef = FALSE;
1601 *skip = TRUE;
1602 }
1603
1604 /* Merge st_other. If the symbol already has a dynamic index,
1605 but visibility says it should not be visible, turn it into a
1606 local symbol. */
1607 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1608 if (h->dynindx != -1)
1609 switch (ELF_ST_VISIBILITY (h->other))
1610 {
1611 case STV_INTERNAL:
1612 case STV_HIDDEN:
1613 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1614 break;
1615 }
1616 }
1617
1618 /* If the old symbol is from a dynamic object, and the new symbol is
1619 a definition which is not from a dynamic object, then the new
1620 symbol overrides the old symbol. Symbols from regular files
1621 always take precedence over symbols from dynamic objects, even if
1622 they are defined after the dynamic object in the link.
1623
1624 As above, we again permit a common symbol in a regular object to
1625 override a definition in a shared object if the shared object
1626 symbol is a function or is weak. */
1627
1628 flip = NULL;
1629 if (!newdyn
1630 && (newdef
1631 || (bfd_is_com_section (sec)
1632 && (oldweak || oldfunc)))
1633 && olddyn
1634 && olddef
1635 && h->def_dynamic)
1636 {
1637 /* Change the hash table entry to undefined, and let
1638 _bfd_generic_link_add_one_symbol do the right thing with the
1639 new definition. */
1640
1641 h->root.type = bfd_link_hash_undefined;
1642 h->root.u.undef.abfd = h->root.u.def.section->owner;
1643 *size_change_ok = TRUE;
1644
1645 olddef = FALSE;
1646 olddyncommon = FALSE;
1647
1648 /* We again permit a type change when a common symbol may be
1649 overriding a function. */
1650
1651 if (bfd_is_com_section (sec))
1652 {
1653 if (oldfunc)
1654 {
1655 /* If a common symbol overrides a function, make sure
1656 that it isn't defined dynamically nor has type
1657 function. */
1658 h->def_dynamic = 0;
1659 h->type = STT_NOTYPE;
1660 }
1661 *type_change_ok = TRUE;
1662 }
1663
1664 if (hi->root.type == bfd_link_hash_indirect)
1665 flip = hi;
1666 else
1667 /* This union may have been set to be non-NULL when this symbol
1668 was seen in a dynamic object. We must force the union to be
1669 NULL, so that it is correct for a regular symbol. */
1670 h->verinfo.vertree = NULL;
1671 }
1672
1673 /* Handle the special case of a new common symbol merging with an
1674 old symbol that looks like it might be a common symbol defined in
1675 a shared object. Note that we have already handled the case in
1676 which a new common symbol should simply override the definition
1677 in the shared library. */
1678
1679 if (! newdyn
1680 && bfd_is_com_section (sec)
1681 && olddyncommon)
1682 {
1683 /* It would be best if we could set the hash table entry to a
1684 common symbol, but we don't know what to use for the section
1685 or the alignment. */
1686 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1687 bfd_link_hash_common, sym->st_size);
1688
1689 /* If the presumed common symbol in the dynamic object is
1690 larger, pretend that the new symbol has its size. */
1691
1692 if (h->size > *pvalue)
1693 *pvalue = h->size;
1694
1695 /* We need to remember the alignment required by the symbol
1696 in the dynamic object. */
1697 BFD_ASSERT (pold_alignment);
1698 *pold_alignment = h->root.u.def.section->alignment_power;
1699
1700 olddef = FALSE;
1701 olddyncommon = FALSE;
1702
1703 h->root.type = bfd_link_hash_undefined;
1704 h->root.u.undef.abfd = h->root.u.def.section->owner;
1705
1706 *size_change_ok = TRUE;
1707 *type_change_ok = TRUE;
1708
1709 if (hi->root.type == bfd_link_hash_indirect)
1710 flip = hi;
1711 else
1712 h->verinfo.vertree = NULL;
1713 }
1714
1715 if (flip != NULL)
1716 {
1717 /* Handle the case where we had a versioned symbol in a dynamic
1718 library and now find a definition in a normal object. In this
1719 case, we make the versioned symbol point to the normal one. */
1720 flip->root.type = h->root.type;
1721 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1722 h->root.type = bfd_link_hash_indirect;
1723 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1724 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1725 if (h->def_dynamic)
1726 {
1727 h->def_dynamic = 0;
1728 flip->ref_dynamic = 1;
1729 }
1730 }
1731
1732 return TRUE;
1733 }
1734
1735 /* This function is called to create an indirect symbol from the
1736 default for the symbol with the default version if needed. The
1737 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1738 set DYNSYM if the new indirect symbol is dynamic. */
1739
1740 static bfd_boolean
1741 _bfd_elf_add_default_symbol (bfd *abfd,
1742 struct bfd_link_info *info,
1743 struct elf_link_hash_entry *h,
1744 const char *name,
1745 Elf_Internal_Sym *sym,
1746 asection *sec,
1747 bfd_vma value,
1748 bfd **poldbfd,
1749 bfd_boolean *dynsym)
1750 {
1751 bfd_boolean type_change_ok;
1752 bfd_boolean size_change_ok;
1753 bfd_boolean skip;
1754 char *shortname;
1755 struct elf_link_hash_entry *hi;
1756 struct bfd_link_hash_entry *bh;
1757 const struct elf_backend_data *bed;
1758 bfd_boolean collect;
1759 bfd_boolean dynamic;
1760 bfd_boolean override;
1761 char *p;
1762 size_t len, shortlen;
1763 asection *tmp_sec;
1764 bfd_boolean matched;
1765
1766 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1767 return TRUE;
1768
1769 /* If this symbol has a version, and it is the default version, we
1770 create an indirect symbol from the default name to the fully
1771 decorated name. This will cause external references which do not
1772 specify a version to be bound to this version of the symbol. */
1773 p = strchr (name, ELF_VER_CHR);
1774 if (h->versioned == unknown)
1775 {
1776 if (p == NULL)
1777 {
1778 h->versioned = unversioned;
1779 return TRUE;
1780 }
1781 else
1782 {
1783 if (p[1] != ELF_VER_CHR)
1784 {
1785 h->versioned = versioned_hidden;
1786 return TRUE;
1787 }
1788 else
1789 h->versioned = versioned;
1790 }
1791 }
1792 else
1793 {
1794 /* PR ld/19073: We may see an unversioned definition after the
1795 default version. */
1796 if (p == NULL)
1797 return TRUE;
1798 }
1799
1800 bed = get_elf_backend_data (abfd);
1801 collect = bed->collect;
1802 dynamic = (abfd->flags & DYNAMIC) != 0;
1803
1804 shortlen = p - name;
1805 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1806 if (shortname == NULL)
1807 return FALSE;
1808 memcpy (shortname, name, shortlen);
1809 shortname[shortlen] = '\0';
1810
1811 /* We are going to create a new symbol. Merge it with any existing
1812 symbol with this name. For the purposes of the merge, act as
1813 though we were defining the symbol we just defined, although we
1814 actually going to define an indirect symbol. */
1815 type_change_ok = FALSE;
1816 size_change_ok = FALSE;
1817 matched = TRUE;
1818 tmp_sec = sec;
1819 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1820 &hi, poldbfd, NULL, NULL, &skip, &override,
1821 &type_change_ok, &size_change_ok, &matched))
1822 return FALSE;
1823
1824 if (skip)
1825 goto nondefault;
1826
1827 if (hi->def_regular)
1828 {
1829 /* If the undecorated symbol will have a version added by a
1830 script different to H, then don't indirect to/from the
1831 undecorated symbol. This isn't ideal because we may not yet
1832 have seen symbol versions, if given by a script on the
1833 command line rather than via --version-script. */
1834 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1835 {
1836 bfd_boolean hide;
1837
1838 hi->verinfo.vertree
1839 = bfd_find_version_for_sym (info->version_info,
1840 hi->root.root.string, &hide);
1841 if (hi->verinfo.vertree != NULL && hide)
1842 {
1843 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1844 goto nondefault;
1845 }
1846 }
1847 if (hi->verinfo.vertree != NULL
1848 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1849 goto nondefault;
1850 }
1851
1852 if (! override)
1853 {
1854 /* Add the default symbol if not performing a relocatable link. */
1855 if (! bfd_link_relocatable (info))
1856 {
1857 bh = &hi->root;
1858 if (! (_bfd_generic_link_add_one_symbol
1859 (info, abfd, shortname, BSF_INDIRECT,
1860 bfd_ind_section_ptr,
1861 0, name, FALSE, collect, &bh)))
1862 return FALSE;
1863 hi = (struct elf_link_hash_entry *) bh;
1864 }
1865 }
1866 else
1867 {
1868 /* In this case the symbol named SHORTNAME is overriding the
1869 indirect symbol we want to add. We were planning on making
1870 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1871 is the name without a version. NAME is the fully versioned
1872 name, and it is the default version.
1873
1874 Overriding means that we already saw a definition for the
1875 symbol SHORTNAME in a regular object, and it is overriding
1876 the symbol defined in the dynamic object.
1877
1878 When this happens, we actually want to change NAME, the
1879 symbol we just added, to refer to SHORTNAME. This will cause
1880 references to NAME in the shared object to become references
1881 to SHORTNAME in the regular object. This is what we expect
1882 when we override a function in a shared object: that the
1883 references in the shared object will be mapped to the
1884 definition in the regular object. */
1885
1886 while (hi->root.type == bfd_link_hash_indirect
1887 || hi->root.type == bfd_link_hash_warning)
1888 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1889
1890 h->root.type = bfd_link_hash_indirect;
1891 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1892 if (h->def_dynamic)
1893 {
1894 h->def_dynamic = 0;
1895 hi->ref_dynamic = 1;
1896 if (hi->ref_regular
1897 || hi->def_regular)
1898 {
1899 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1900 return FALSE;
1901 }
1902 }
1903
1904 /* Now set HI to H, so that the following code will set the
1905 other fields correctly. */
1906 hi = h;
1907 }
1908
1909 /* Check if HI is a warning symbol. */
1910 if (hi->root.type == bfd_link_hash_warning)
1911 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1912
1913 /* If there is a duplicate definition somewhere, then HI may not
1914 point to an indirect symbol. We will have reported an error to
1915 the user in that case. */
1916
1917 if (hi->root.type == bfd_link_hash_indirect)
1918 {
1919 struct elf_link_hash_entry *ht;
1920
1921 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1922 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1923
1924 /* A reference to the SHORTNAME symbol from a dynamic library
1925 will be satisfied by the versioned symbol at runtime. In
1926 effect, we have a reference to the versioned symbol. */
1927 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1928 hi->dynamic_def |= ht->dynamic_def;
1929
1930 /* See if the new flags lead us to realize that the symbol must
1931 be dynamic. */
1932 if (! *dynsym)
1933 {
1934 if (! dynamic)
1935 {
1936 if (! bfd_link_executable (info)
1937 || hi->def_dynamic
1938 || hi->ref_dynamic)
1939 *dynsym = TRUE;
1940 }
1941 else
1942 {
1943 if (hi->ref_regular)
1944 *dynsym = TRUE;
1945 }
1946 }
1947 }
1948
1949 /* We also need to define an indirection from the nondefault version
1950 of the symbol. */
1951
1952 nondefault:
1953 len = strlen (name);
1954 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
1955 if (shortname == NULL)
1956 return FALSE;
1957 memcpy (shortname, name, shortlen);
1958 memcpy (shortname + shortlen, p + 1, len - shortlen);
1959
1960 /* Once again, merge with any existing symbol. */
1961 type_change_ok = FALSE;
1962 size_change_ok = FALSE;
1963 tmp_sec = sec;
1964 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1965 &hi, poldbfd, NULL, NULL, &skip, &override,
1966 &type_change_ok, &size_change_ok, &matched))
1967 return FALSE;
1968
1969 if (skip)
1970 return TRUE;
1971
1972 if (override)
1973 {
1974 /* Here SHORTNAME is a versioned name, so we don't expect to see
1975 the type of override we do in the case above unless it is
1976 overridden by a versioned definition. */
1977 if (hi->root.type != bfd_link_hash_defined
1978 && hi->root.type != bfd_link_hash_defweak)
1979 _bfd_error_handler
1980 /* xgettext:c-format */
1981 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1982 abfd, shortname);
1983 }
1984 else
1985 {
1986 bh = &hi->root;
1987 if (! (_bfd_generic_link_add_one_symbol
1988 (info, abfd, shortname, BSF_INDIRECT,
1989 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1990 return FALSE;
1991 hi = (struct elf_link_hash_entry *) bh;
1992
1993 /* If there is a duplicate definition somewhere, then HI may not
1994 point to an indirect symbol. We will have reported an error
1995 to the user in that case. */
1996
1997 if (hi->root.type == bfd_link_hash_indirect)
1998 {
1999 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2000 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2001 hi->dynamic_def |= h->dynamic_def;
2002
2003 /* See if the new flags lead us to realize that the symbol
2004 must be dynamic. */
2005 if (! *dynsym)
2006 {
2007 if (! dynamic)
2008 {
2009 if (! bfd_link_executable (info)
2010 || hi->ref_dynamic)
2011 *dynsym = TRUE;
2012 }
2013 else
2014 {
2015 if (hi->ref_regular)
2016 *dynsym = TRUE;
2017 }
2018 }
2019 }
2020 }
2021
2022 return TRUE;
2023 }
2024 \f
2025 /* This routine is used to export all defined symbols into the dynamic
2026 symbol table. It is called via elf_link_hash_traverse. */
2027
2028 static bfd_boolean
2029 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2030 {
2031 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2032
2033 /* Ignore indirect symbols. These are added by the versioning code. */
2034 if (h->root.type == bfd_link_hash_indirect)
2035 return TRUE;
2036
2037 /* Ignore this if we won't export it. */
2038 if (!eif->info->export_dynamic && !h->dynamic)
2039 return TRUE;
2040
2041 if (h->dynindx == -1
2042 && (h->def_regular || h->ref_regular)
2043 && ! bfd_hide_sym_by_version (eif->info->version_info,
2044 h->root.root.string))
2045 {
2046 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2047 {
2048 eif->failed = TRUE;
2049 return FALSE;
2050 }
2051 }
2052
2053 return TRUE;
2054 }
2055 \f
2056 /* Look through the symbols which are defined in other shared
2057 libraries and referenced here. Update the list of version
2058 dependencies. This will be put into the .gnu.version_r section.
2059 This function is called via elf_link_hash_traverse. */
2060
2061 static bfd_boolean
2062 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2063 void *data)
2064 {
2065 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2066 Elf_Internal_Verneed *t;
2067 Elf_Internal_Vernaux *a;
2068 bfd_size_type amt;
2069
2070 /* We only care about symbols defined in shared objects with version
2071 information. */
2072 if (!h->def_dynamic
2073 || h->def_regular
2074 || h->dynindx == -1
2075 || h->verinfo.verdef == NULL
2076 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2077 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2078 return TRUE;
2079
2080 /* See if we already know about this version. */
2081 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2082 t != NULL;
2083 t = t->vn_nextref)
2084 {
2085 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2086 continue;
2087
2088 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2089 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2090 return TRUE;
2091
2092 break;
2093 }
2094
2095 /* This is a new version. Add it to tree we are building. */
2096
2097 if (t == NULL)
2098 {
2099 amt = sizeof *t;
2100 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2101 if (t == NULL)
2102 {
2103 rinfo->failed = TRUE;
2104 return FALSE;
2105 }
2106
2107 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2108 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2109 elf_tdata (rinfo->info->output_bfd)->verref = t;
2110 }
2111
2112 amt = sizeof *a;
2113 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2114 if (a == NULL)
2115 {
2116 rinfo->failed = TRUE;
2117 return FALSE;
2118 }
2119
2120 /* Note that we are copying a string pointer here, and testing it
2121 above. If bfd_elf_string_from_elf_section is ever changed to
2122 discard the string data when low in memory, this will have to be
2123 fixed. */
2124 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2125
2126 a->vna_flags = h->verinfo.verdef->vd_flags;
2127 a->vna_nextptr = t->vn_auxptr;
2128
2129 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2130 ++rinfo->vers;
2131
2132 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2133
2134 t->vn_auxptr = a;
2135
2136 return TRUE;
2137 }
2138
2139 /* Figure out appropriate versions for all the symbols. We may not
2140 have the version number script until we have read all of the input
2141 files, so until that point we don't know which symbols should be
2142 local. This function is called via elf_link_hash_traverse. */
2143
2144 static bfd_boolean
2145 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2146 {
2147 struct elf_info_failed *sinfo;
2148 struct bfd_link_info *info;
2149 const struct elf_backend_data *bed;
2150 struct elf_info_failed eif;
2151 char *p;
2152
2153 sinfo = (struct elf_info_failed *) data;
2154 info = sinfo->info;
2155
2156 /* Fix the symbol flags. */
2157 eif.failed = FALSE;
2158 eif.info = info;
2159 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2160 {
2161 if (eif.failed)
2162 sinfo->failed = TRUE;
2163 return FALSE;
2164 }
2165
2166 /* We only need version numbers for symbols defined in regular
2167 objects. */
2168 if (!h->def_regular)
2169 return TRUE;
2170
2171 bed = get_elf_backend_data (info->output_bfd);
2172 p = strchr (h->root.root.string, ELF_VER_CHR);
2173 if (p != NULL && h->verinfo.vertree == NULL)
2174 {
2175 struct bfd_elf_version_tree *t;
2176
2177 ++p;
2178 if (*p == ELF_VER_CHR)
2179 ++p;
2180
2181 /* If there is no version string, we can just return out. */
2182 if (*p == '\0')
2183 return TRUE;
2184
2185 /* Look for the version. If we find it, it is no longer weak. */
2186 for (t = sinfo->info->version_info; t != NULL; t = t->next)
2187 {
2188 if (strcmp (t->name, p) == 0)
2189 {
2190 size_t len;
2191 char *alc;
2192 struct bfd_elf_version_expr *d;
2193
2194 len = p - h->root.root.string;
2195 alc = (char *) bfd_malloc (len);
2196 if (alc == NULL)
2197 {
2198 sinfo->failed = TRUE;
2199 return FALSE;
2200 }
2201 memcpy (alc, h->root.root.string, len - 1);
2202 alc[len - 1] = '\0';
2203 if (alc[len - 2] == ELF_VER_CHR)
2204 alc[len - 2] = '\0';
2205
2206 h->verinfo.vertree = t;
2207 t->used = TRUE;
2208 d = NULL;
2209
2210 if (t->globals.list != NULL)
2211 d = (*t->match) (&t->globals, NULL, alc);
2212
2213 /* See if there is anything to force this symbol to
2214 local scope. */
2215 if (d == NULL && t->locals.list != NULL)
2216 {
2217 d = (*t->match) (&t->locals, NULL, alc);
2218 if (d != NULL
2219 && h->dynindx != -1
2220 && ! info->export_dynamic)
2221 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2222 }
2223
2224 free (alc);
2225 break;
2226 }
2227 }
2228
2229 /* If we are building an application, we need to create a
2230 version node for this version. */
2231 if (t == NULL && bfd_link_executable (info))
2232 {
2233 struct bfd_elf_version_tree **pp;
2234 int version_index;
2235
2236 /* If we aren't going to export this symbol, we don't need
2237 to worry about it. */
2238 if (h->dynindx == -1)
2239 return TRUE;
2240
2241 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2242 sizeof *t);
2243 if (t == NULL)
2244 {
2245 sinfo->failed = TRUE;
2246 return FALSE;
2247 }
2248
2249 t->name = p;
2250 t->name_indx = (unsigned int) -1;
2251 t->used = TRUE;
2252
2253 version_index = 1;
2254 /* Don't count anonymous version tag. */
2255 if (sinfo->info->version_info != NULL
2256 && sinfo->info->version_info->vernum == 0)
2257 version_index = 0;
2258 for (pp = &sinfo->info->version_info;
2259 *pp != NULL;
2260 pp = &(*pp)->next)
2261 ++version_index;
2262 t->vernum = version_index;
2263
2264 *pp = t;
2265
2266 h->verinfo.vertree = t;
2267 }
2268 else if (t == NULL)
2269 {
2270 /* We could not find the version for a symbol when
2271 generating a shared archive. Return an error. */
2272 _bfd_error_handler
2273 /* xgettext:c-format */
2274 (_("%B: version node not found for symbol %s"),
2275 info->output_bfd, h->root.root.string);
2276 bfd_set_error (bfd_error_bad_value);
2277 sinfo->failed = TRUE;
2278 return FALSE;
2279 }
2280 }
2281
2282 /* If we don't have a version for this symbol, see if we can find
2283 something. */
2284 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
2285 {
2286 bfd_boolean hide;
2287
2288 h->verinfo.vertree
2289 = bfd_find_version_for_sym (sinfo->info->version_info,
2290 h->root.root.string, &hide);
2291 if (h->verinfo.vertree != NULL && hide)
2292 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2293 }
2294
2295 return TRUE;
2296 }
2297 \f
2298 /* Read and swap the relocs from the section indicated by SHDR. This
2299 may be either a REL or a RELA section. The relocations are
2300 translated into RELA relocations and stored in INTERNAL_RELOCS,
2301 which should have already been allocated to contain enough space.
2302 The EXTERNAL_RELOCS are a buffer where the external form of the
2303 relocations should be stored.
2304
2305 Returns FALSE if something goes wrong. */
2306
2307 static bfd_boolean
2308 elf_link_read_relocs_from_section (bfd *abfd,
2309 asection *sec,
2310 Elf_Internal_Shdr *shdr,
2311 void *external_relocs,
2312 Elf_Internal_Rela *internal_relocs)
2313 {
2314 const struct elf_backend_data *bed;
2315 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2316 const bfd_byte *erela;
2317 const bfd_byte *erelaend;
2318 Elf_Internal_Rela *irela;
2319 Elf_Internal_Shdr *symtab_hdr;
2320 size_t nsyms;
2321
2322 /* Position ourselves at the start of the section. */
2323 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2324 return FALSE;
2325
2326 /* Read the relocations. */
2327 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2328 return FALSE;
2329
2330 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2331 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2332
2333 bed = get_elf_backend_data (abfd);
2334
2335 /* Convert the external relocations to the internal format. */
2336 if (shdr->sh_entsize == bed->s->sizeof_rel)
2337 swap_in = bed->s->swap_reloc_in;
2338 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2339 swap_in = bed->s->swap_reloca_in;
2340 else
2341 {
2342 bfd_set_error (bfd_error_wrong_format);
2343 return FALSE;
2344 }
2345
2346 erela = (const bfd_byte *) external_relocs;
2347 erelaend = erela + shdr->sh_size;
2348 irela = internal_relocs;
2349 while (erela < erelaend)
2350 {
2351 bfd_vma r_symndx;
2352
2353 (*swap_in) (abfd, erela, irela);
2354 r_symndx = ELF32_R_SYM (irela->r_info);
2355 if (bed->s->arch_size == 64)
2356 r_symndx >>= 24;
2357 if (nsyms > 0)
2358 {
2359 if ((size_t) r_symndx >= nsyms)
2360 {
2361 _bfd_error_handler
2362 /* xgettext:c-format */
2363 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2364 " for offset 0x%lx in section `%A'"),
2365 abfd, sec,
2366 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2367 bfd_set_error (bfd_error_bad_value);
2368 return FALSE;
2369 }
2370 }
2371 else if (r_symndx != STN_UNDEF)
2372 {
2373 _bfd_error_handler
2374 /* xgettext:c-format */
2375 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2376 " when the object file has no symbol table"),
2377 abfd, sec,
2378 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2379 bfd_set_error (bfd_error_bad_value);
2380 return FALSE;
2381 }
2382 irela += bed->s->int_rels_per_ext_rel;
2383 erela += shdr->sh_entsize;
2384 }
2385
2386 return TRUE;
2387 }
2388
2389 /* Read and swap the relocs for a section O. They may have been
2390 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2391 not NULL, they are used as buffers to read into. They are known to
2392 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2393 the return value is allocated using either malloc or bfd_alloc,
2394 according to the KEEP_MEMORY argument. If O has two relocation
2395 sections (both REL and RELA relocations), then the REL_HDR
2396 relocations will appear first in INTERNAL_RELOCS, followed by the
2397 RELA_HDR relocations. */
2398
2399 Elf_Internal_Rela *
2400 _bfd_elf_link_read_relocs (bfd *abfd,
2401 asection *o,
2402 void *external_relocs,
2403 Elf_Internal_Rela *internal_relocs,
2404 bfd_boolean keep_memory)
2405 {
2406 void *alloc1 = NULL;
2407 Elf_Internal_Rela *alloc2 = NULL;
2408 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2409 struct bfd_elf_section_data *esdo = elf_section_data (o);
2410 Elf_Internal_Rela *internal_rela_relocs;
2411
2412 if (esdo->relocs != NULL)
2413 return esdo->relocs;
2414
2415 if (o->reloc_count == 0)
2416 return NULL;
2417
2418 if (internal_relocs == NULL)
2419 {
2420 bfd_size_type size;
2421
2422 size = o->reloc_count;
2423 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2424 if (keep_memory)
2425 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2426 else
2427 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2428 if (internal_relocs == NULL)
2429 goto error_return;
2430 }
2431
2432 if (external_relocs == NULL)
2433 {
2434 bfd_size_type size = 0;
2435
2436 if (esdo->rel.hdr)
2437 size += esdo->rel.hdr->sh_size;
2438 if (esdo->rela.hdr)
2439 size += esdo->rela.hdr->sh_size;
2440
2441 alloc1 = bfd_malloc (size);
2442 if (alloc1 == NULL)
2443 goto error_return;
2444 external_relocs = alloc1;
2445 }
2446
2447 internal_rela_relocs = internal_relocs;
2448 if (esdo->rel.hdr)
2449 {
2450 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2451 external_relocs,
2452 internal_relocs))
2453 goto error_return;
2454 external_relocs = (((bfd_byte *) external_relocs)
2455 + esdo->rel.hdr->sh_size);
2456 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2457 * bed->s->int_rels_per_ext_rel);
2458 }
2459
2460 if (esdo->rela.hdr
2461 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2462 external_relocs,
2463 internal_rela_relocs)))
2464 goto error_return;
2465
2466 /* Cache the results for next time, if we can. */
2467 if (keep_memory)
2468 esdo->relocs = internal_relocs;
2469
2470 if (alloc1 != NULL)
2471 free (alloc1);
2472
2473 /* Don't free alloc2, since if it was allocated we are passing it
2474 back (under the name of internal_relocs). */
2475
2476 return internal_relocs;
2477
2478 error_return:
2479 if (alloc1 != NULL)
2480 free (alloc1);
2481 if (alloc2 != NULL)
2482 {
2483 if (keep_memory)
2484 bfd_release (abfd, alloc2);
2485 else
2486 free (alloc2);
2487 }
2488 return NULL;
2489 }
2490
2491 /* Compute the size of, and allocate space for, REL_HDR which is the
2492 section header for a section containing relocations for O. */
2493
2494 static bfd_boolean
2495 _bfd_elf_link_size_reloc_section (bfd *abfd,
2496 struct bfd_elf_section_reloc_data *reldata)
2497 {
2498 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2499
2500 /* That allows us to calculate the size of the section. */
2501 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2502
2503 /* The contents field must last into write_object_contents, so we
2504 allocate it with bfd_alloc rather than malloc. Also since we
2505 cannot be sure that the contents will actually be filled in,
2506 we zero the allocated space. */
2507 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2508 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2509 return FALSE;
2510
2511 if (reldata->hashes == NULL && reldata->count)
2512 {
2513 struct elf_link_hash_entry **p;
2514
2515 p = ((struct elf_link_hash_entry **)
2516 bfd_zmalloc (reldata->count * sizeof (*p)));
2517 if (p == NULL)
2518 return FALSE;
2519
2520 reldata->hashes = p;
2521 }
2522
2523 return TRUE;
2524 }
2525
2526 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2527 originated from the section given by INPUT_REL_HDR) to the
2528 OUTPUT_BFD. */
2529
2530 bfd_boolean
2531 _bfd_elf_link_output_relocs (bfd *output_bfd,
2532 asection *input_section,
2533 Elf_Internal_Shdr *input_rel_hdr,
2534 Elf_Internal_Rela *internal_relocs,
2535 struct elf_link_hash_entry **rel_hash
2536 ATTRIBUTE_UNUSED)
2537 {
2538 Elf_Internal_Rela *irela;
2539 Elf_Internal_Rela *irelaend;
2540 bfd_byte *erel;
2541 struct bfd_elf_section_reloc_data *output_reldata;
2542 asection *output_section;
2543 const struct elf_backend_data *bed;
2544 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2545 struct bfd_elf_section_data *esdo;
2546
2547 output_section = input_section->output_section;
2548
2549 bed = get_elf_backend_data (output_bfd);
2550 esdo = elf_section_data (output_section);
2551 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2552 {
2553 output_reldata = &esdo->rel;
2554 swap_out = bed->s->swap_reloc_out;
2555 }
2556 else if (esdo->rela.hdr
2557 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2558 {
2559 output_reldata = &esdo->rela;
2560 swap_out = bed->s->swap_reloca_out;
2561 }
2562 else
2563 {
2564 _bfd_error_handler
2565 /* xgettext:c-format */
2566 (_("%B: relocation size mismatch in %B section %A"),
2567 output_bfd, input_section->owner, input_section);
2568 bfd_set_error (bfd_error_wrong_format);
2569 return FALSE;
2570 }
2571
2572 erel = output_reldata->hdr->contents;
2573 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2574 irela = internal_relocs;
2575 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2576 * bed->s->int_rels_per_ext_rel);
2577 while (irela < irelaend)
2578 {
2579 (*swap_out) (output_bfd, irela, erel);
2580 irela += bed->s->int_rels_per_ext_rel;
2581 erel += input_rel_hdr->sh_entsize;
2582 }
2583
2584 /* Bump the counter, so that we know where to add the next set of
2585 relocations. */
2586 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2587
2588 return TRUE;
2589 }
2590 \f
2591 /* Make weak undefined symbols in PIE dynamic. */
2592
2593 bfd_boolean
2594 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2595 struct elf_link_hash_entry *h)
2596 {
2597 if (bfd_link_pie (info)
2598 && h->dynindx == -1
2599 && h->root.type == bfd_link_hash_undefweak)
2600 return bfd_elf_link_record_dynamic_symbol (info, h);
2601
2602 return TRUE;
2603 }
2604
2605 /* Fix up the flags for a symbol. This handles various cases which
2606 can only be fixed after all the input files are seen. This is
2607 currently called by both adjust_dynamic_symbol and
2608 assign_sym_version, which is unnecessary but perhaps more robust in
2609 the face of future changes. */
2610
2611 static bfd_boolean
2612 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2613 struct elf_info_failed *eif)
2614 {
2615 const struct elf_backend_data *bed;
2616
2617 /* If this symbol was mentioned in a non-ELF file, try to set
2618 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2619 permit a non-ELF file to correctly refer to a symbol defined in
2620 an ELF dynamic object. */
2621 if (h->non_elf)
2622 {
2623 while (h->root.type == bfd_link_hash_indirect)
2624 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2625
2626 if (h->root.type != bfd_link_hash_defined
2627 && h->root.type != bfd_link_hash_defweak)
2628 {
2629 h->ref_regular = 1;
2630 h->ref_regular_nonweak = 1;
2631 }
2632 else
2633 {
2634 if (h->root.u.def.section->owner != NULL
2635 && (bfd_get_flavour (h->root.u.def.section->owner)
2636 == bfd_target_elf_flavour))
2637 {
2638 h->ref_regular = 1;
2639 h->ref_regular_nonweak = 1;
2640 }
2641 else
2642 h->def_regular = 1;
2643 }
2644
2645 if (h->dynindx == -1
2646 && (h->def_dynamic
2647 || h->ref_dynamic))
2648 {
2649 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2650 {
2651 eif->failed = TRUE;
2652 return FALSE;
2653 }
2654 }
2655 }
2656 else
2657 {
2658 /* Unfortunately, NON_ELF is only correct if the symbol
2659 was first seen in a non-ELF file. Fortunately, if the symbol
2660 was first seen in an ELF file, we're probably OK unless the
2661 symbol was defined in a non-ELF file. Catch that case here.
2662 FIXME: We're still in trouble if the symbol was first seen in
2663 a dynamic object, and then later in a non-ELF regular object. */
2664 if ((h->root.type == bfd_link_hash_defined
2665 || h->root.type == bfd_link_hash_defweak)
2666 && !h->def_regular
2667 && (h->root.u.def.section->owner != NULL
2668 ? (bfd_get_flavour (h->root.u.def.section->owner)
2669 != bfd_target_elf_flavour)
2670 : (bfd_is_abs_section (h->root.u.def.section)
2671 && !h->def_dynamic)))
2672 h->def_regular = 1;
2673 }
2674
2675 /* Backend specific symbol fixup. */
2676 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2677 if (bed->elf_backend_fixup_symbol
2678 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2679 return FALSE;
2680
2681 /* If this is a final link, and the symbol was defined as a common
2682 symbol in a regular object file, and there was no definition in
2683 any dynamic object, then the linker will have allocated space for
2684 the symbol in a common section but the DEF_REGULAR
2685 flag will not have been set. */
2686 if (h->root.type == bfd_link_hash_defined
2687 && !h->def_regular
2688 && h->ref_regular
2689 && !h->def_dynamic
2690 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2691 h->def_regular = 1;
2692
2693 /* If a weak undefined symbol has non-default visibility, we also
2694 hide it from the dynamic linker. */
2695 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2696 && h->root.type == bfd_link_hash_undefweak)
2697 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2698
2699 /* A hidden versioned symbol in executable should be forced local if
2700 it is is locally defined, not referenced by shared library and not
2701 exported. */
2702 else if (bfd_link_executable (eif->info)
2703 && h->versioned == versioned_hidden
2704 && !eif->info->export_dynamic
2705 && !h->dynamic
2706 && !h->ref_dynamic
2707 && h->def_regular)
2708 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2709
2710 /* If -Bsymbolic was used (which means to bind references to global
2711 symbols to the definition within the shared object), and this
2712 symbol was defined in a regular object, then it actually doesn't
2713 need a PLT entry. Likewise, if the symbol has non-default
2714 visibility. If the symbol has hidden or internal visibility, we
2715 will force it local. */
2716 else if (h->needs_plt
2717 && bfd_link_pic (eif->info)
2718 && is_elf_hash_table (eif->info->hash)
2719 && (SYMBOLIC_BIND (eif->info, h)
2720 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2721 && h->def_regular)
2722 {
2723 bfd_boolean force_local;
2724
2725 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2726 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2727 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2728 }
2729
2730 /* If this is a weak defined symbol in a dynamic object, and we know
2731 the real definition in the dynamic object, copy interesting flags
2732 over to the real definition. */
2733 if (h->u.weakdef != NULL)
2734 {
2735 /* If the real definition is defined by a regular object file,
2736 don't do anything special. See the longer description in
2737 _bfd_elf_adjust_dynamic_symbol, below. */
2738 if (h->u.weakdef->def_regular)
2739 h->u.weakdef = NULL;
2740 else
2741 {
2742 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2743
2744 while (h->root.type == bfd_link_hash_indirect)
2745 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2746
2747 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2748 || h->root.type == bfd_link_hash_defweak);
2749 BFD_ASSERT (weakdef->def_dynamic);
2750 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2751 || weakdef->root.type == bfd_link_hash_defweak);
2752 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2753 }
2754 }
2755
2756 return TRUE;
2757 }
2758
2759 /* Make the backend pick a good value for a dynamic symbol. This is
2760 called via elf_link_hash_traverse, and also calls itself
2761 recursively. */
2762
2763 static bfd_boolean
2764 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2765 {
2766 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2767 bfd *dynobj;
2768 const struct elf_backend_data *bed;
2769
2770 if (! is_elf_hash_table (eif->info->hash))
2771 return FALSE;
2772
2773 /* Ignore indirect symbols. These are added by the versioning code. */
2774 if (h->root.type == bfd_link_hash_indirect)
2775 return TRUE;
2776
2777 /* Fix the symbol flags. */
2778 if (! _bfd_elf_fix_symbol_flags (h, eif))
2779 return FALSE;
2780
2781 /* If this symbol does not require a PLT entry, and it is not
2782 defined by a dynamic object, or is not referenced by a regular
2783 object, ignore it. We do have to handle a weak defined symbol,
2784 even if no regular object refers to it, if we decided to add it
2785 to the dynamic symbol table. FIXME: Do we normally need to worry
2786 about symbols which are defined by one dynamic object and
2787 referenced by another one? */
2788 if (!h->needs_plt
2789 && h->type != STT_GNU_IFUNC
2790 && (h->def_regular
2791 || !h->def_dynamic
2792 || (!h->ref_regular
2793 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2794 {
2795 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2796 return TRUE;
2797 }
2798
2799 /* If we've already adjusted this symbol, don't do it again. This
2800 can happen via a recursive call. */
2801 if (h->dynamic_adjusted)
2802 return TRUE;
2803
2804 /* Don't look at this symbol again. Note that we must set this
2805 after checking the above conditions, because we may look at a
2806 symbol once, decide not to do anything, and then get called
2807 recursively later after REF_REGULAR is set below. */
2808 h->dynamic_adjusted = 1;
2809
2810 /* If this is a weak definition, and we know a real definition, and
2811 the real symbol is not itself defined by a regular object file,
2812 then get a good value for the real definition. We handle the
2813 real symbol first, for the convenience of the backend routine.
2814
2815 Note that there is a confusing case here. If the real definition
2816 is defined by a regular object file, we don't get the real symbol
2817 from the dynamic object, but we do get the weak symbol. If the
2818 processor backend uses a COPY reloc, then if some routine in the
2819 dynamic object changes the real symbol, we will not see that
2820 change in the corresponding weak symbol. This is the way other
2821 ELF linkers work as well, and seems to be a result of the shared
2822 library model.
2823
2824 I will clarify this issue. Most SVR4 shared libraries define the
2825 variable _timezone and define timezone as a weak synonym. The
2826 tzset call changes _timezone. If you write
2827 extern int timezone;
2828 int _timezone = 5;
2829 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2830 you might expect that, since timezone is a synonym for _timezone,
2831 the same number will print both times. However, if the processor
2832 backend uses a COPY reloc, then actually timezone will be copied
2833 into your process image, and, since you define _timezone
2834 yourself, _timezone will not. Thus timezone and _timezone will
2835 wind up at different memory locations. The tzset call will set
2836 _timezone, leaving timezone unchanged. */
2837
2838 if (h->u.weakdef != NULL)
2839 {
2840 /* If we get to this point, there is an implicit reference to
2841 H->U.WEAKDEF by a regular object file via the weak symbol H. */
2842 h->u.weakdef->ref_regular = 1;
2843
2844 /* Ensure that the backend adjust_dynamic_symbol function sees
2845 H->U.WEAKDEF before H by recursively calling ourselves. */
2846 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2847 return FALSE;
2848 }
2849
2850 /* If a symbol has no type and no size and does not require a PLT
2851 entry, then we are probably about to do the wrong thing here: we
2852 are probably going to create a COPY reloc for an empty object.
2853 This case can arise when a shared object is built with assembly
2854 code, and the assembly code fails to set the symbol type. */
2855 if (h->size == 0
2856 && h->type == STT_NOTYPE
2857 && !h->needs_plt)
2858 _bfd_error_handler
2859 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2860 h->root.root.string);
2861
2862 dynobj = elf_hash_table (eif->info)->dynobj;
2863 bed = get_elf_backend_data (dynobj);
2864
2865 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2866 {
2867 eif->failed = TRUE;
2868 return FALSE;
2869 }
2870
2871 return TRUE;
2872 }
2873
2874 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2875 DYNBSS. */
2876
2877 bfd_boolean
2878 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2879 struct elf_link_hash_entry *h,
2880 asection *dynbss)
2881 {
2882 unsigned int power_of_two;
2883 bfd_vma mask;
2884 asection *sec = h->root.u.def.section;
2885
2886 /* The section aligment of definition is the maximum alignment
2887 requirement of symbols defined in the section. Since we don't
2888 know the symbol alignment requirement, we start with the
2889 maximum alignment and check low bits of the symbol address
2890 for the minimum alignment. */
2891 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2892 mask = ((bfd_vma) 1 << power_of_two) - 1;
2893 while ((h->root.u.def.value & mask) != 0)
2894 {
2895 mask >>= 1;
2896 --power_of_two;
2897 }
2898
2899 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2900 dynbss))
2901 {
2902 /* Adjust the section alignment if needed. */
2903 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2904 power_of_two))
2905 return FALSE;
2906 }
2907
2908 /* We make sure that the symbol will be aligned properly. */
2909 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2910
2911 /* Define the symbol as being at this point in DYNBSS. */
2912 h->root.u.def.section = dynbss;
2913 h->root.u.def.value = dynbss->size;
2914
2915 /* Increment the size of DYNBSS to make room for the symbol. */
2916 dynbss->size += h->size;
2917
2918 /* No error if extern_protected_data is true. */
2919 if (h->protected_def
2920 && (!info->extern_protected_data
2921 || (info->extern_protected_data < 0
2922 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
2923 info->callbacks->einfo
2924 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2925 h->root.root.string);
2926
2927 return TRUE;
2928 }
2929
2930 /* Adjust all external symbols pointing into SEC_MERGE sections
2931 to reflect the object merging within the sections. */
2932
2933 static bfd_boolean
2934 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2935 {
2936 asection *sec;
2937
2938 if ((h->root.type == bfd_link_hash_defined
2939 || h->root.type == bfd_link_hash_defweak)
2940 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2941 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
2942 {
2943 bfd *output_bfd = (bfd *) data;
2944
2945 h->root.u.def.value =
2946 _bfd_merged_section_offset (output_bfd,
2947 &h->root.u.def.section,
2948 elf_section_data (sec)->sec_info,
2949 h->root.u.def.value);
2950 }
2951
2952 return TRUE;
2953 }
2954
2955 /* Returns false if the symbol referred to by H should be considered
2956 to resolve local to the current module, and true if it should be
2957 considered to bind dynamically. */
2958
2959 bfd_boolean
2960 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2961 struct bfd_link_info *info,
2962 bfd_boolean not_local_protected)
2963 {
2964 bfd_boolean binding_stays_local_p;
2965 const struct elf_backend_data *bed;
2966 struct elf_link_hash_table *hash_table;
2967
2968 if (h == NULL)
2969 return FALSE;
2970
2971 while (h->root.type == bfd_link_hash_indirect
2972 || h->root.type == bfd_link_hash_warning)
2973 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2974
2975 /* If it was forced local, then clearly it's not dynamic. */
2976 if (h->dynindx == -1)
2977 return FALSE;
2978 if (h->forced_local)
2979 return FALSE;
2980
2981 /* Identify the cases where name binding rules say that a
2982 visible symbol resolves locally. */
2983 binding_stays_local_p = (bfd_link_executable (info)
2984 || SYMBOLIC_BIND (info, h));
2985
2986 switch (ELF_ST_VISIBILITY (h->other))
2987 {
2988 case STV_INTERNAL:
2989 case STV_HIDDEN:
2990 return FALSE;
2991
2992 case STV_PROTECTED:
2993 hash_table = elf_hash_table (info);
2994 if (!is_elf_hash_table (hash_table))
2995 return FALSE;
2996
2997 bed = get_elf_backend_data (hash_table->dynobj);
2998
2999 /* Proper resolution for function pointer equality may require
3000 that these symbols perhaps be resolved dynamically, even though
3001 we should be resolving them to the current module. */
3002 if (!not_local_protected || !bed->is_function_type (h->type))
3003 binding_stays_local_p = TRUE;
3004 break;
3005
3006 default:
3007 break;
3008 }
3009
3010 /* If it isn't defined locally, then clearly it's dynamic. */
3011 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3012 return TRUE;
3013
3014 /* Otherwise, the symbol is dynamic if binding rules don't tell
3015 us that it remains local. */
3016 return !binding_stays_local_p;
3017 }
3018
3019 /* Return true if the symbol referred to by H should be considered
3020 to resolve local to the current module, and false otherwise. Differs
3021 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3022 undefined symbols. The two functions are virtually identical except
3023 for the place where forced_local and dynindx == -1 are tested. If
3024 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
3025 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
3026 the symbol is local only for defined symbols.
3027 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3028 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3029 treatment of undefined weak symbols. For those that do not make
3030 undefined weak symbols dynamic, both functions may return false. */
3031
3032 bfd_boolean
3033 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3034 struct bfd_link_info *info,
3035 bfd_boolean local_protected)
3036 {
3037 const struct elf_backend_data *bed;
3038 struct elf_link_hash_table *hash_table;
3039
3040 /* If it's a local sym, of course we resolve locally. */
3041 if (h == NULL)
3042 return TRUE;
3043
3044 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3045 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3046 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3047 return TRUE;
3048
3049 /* Common symbols that become definitions don't get the DEF_REGULAR
3050 flag set, so test it first, and don't bail out. */
3051 if (ELF_COMMON_DEF_P (h))
3052 /* Do nothing. */;
3053 /* If we don't have a definition in a regular file, then we can't
3054 resolve locally. The sym is either undefined or dynamic. */
3055 else if (!h->def_regular)
3056 return FALSE;
3057
3058 /* Forced local symbols resolve locally. */
3059 if (h->forced_local)
3060 return TRUE;
3061
3062 /* As do non-dynamic symbols. */
3063 if (h->dynindx == -1)
3064 return TRUE;
3065
3066 /* At this point, we know the symbol is defined and dynamic. In an
3067 executable it must resolve locally, likewise when building symbolic
3068 shared libraries. */
3069 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3070 return TRUE;
3071
3072 /* Now deal with defined dynamic symbols in shared libraries. Ones
3073 with default visibility might not resolve locally. */
3074 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3075 return FALSE;
3076
3077 hash_table = elf_hash_table (info);
3078 if (!is_elf_hash_table (hash_table))
3079 return TRUE;
3080
3081 bed = get_elf_backend_data (hash_table->dynobj);
3082
3083 /* If extern_protected_data is false, STV_PROTECTED non-function
3084 symbols are local. */
3085 if ((!info->extern_protected_data
3086 || (info->extern_protected_data < 0
3087 && !bed->extern_protected_data))
3088 && !bed->is_function_type (h->type))
3089 return TRUE;
3090
3091 /* Function pointer equality tests may require that STV_PROTECTED
3092 symbols be treated as dynamic symbols. If the address of a
3093 function not defined in an executable is set to that function's
3094 plt entry in the executable, then the address of the function in
3095 a shared library must also be the plt entry in the executable. */
3096 return local_protected;
3097 }
3098
3099 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3100 aligned. Returns the first TLS output section. */
3101
3102 struct bfd_section *
3103 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3104 {
3105 struct bfd_section *sec, *tls;
3106 unsigned int align = 0;
3107
3108 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3109 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3110 break;
3111 tls = sec;
3112
3113 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3114 if (sec->alignment_power > align)
3115 align = sec->alignment_power;
3116
3117 elf_hash_table (info)->tls_sec = tls;
3118
3119 /* Ensure the alignment of the first section is the largest alignment,
3120 so that the tls segment starts aligned. */
3121 if (tls != NULL)
3122 tls->alignment_power = align;
3123
3124 return tls;
3125 }
3126
3127 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3128 static bfd_boolean
3129 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3130 Elf_Internal_Sym *sym)
3131 {
3132 const struct elf_backend_data *bed;
3133
3134 /* Local symbols do not count, but target specific ones might. */
3135 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3136 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3137 return FALSE;
3138
3139 bed = get_elf_backend_data (abfd);
3140 /* Function symbols do not count. */
3141 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3142 return FALSE;
3143
3144 /* If the section is undefined, then so is the symbol. */
3145 if (sym->st_shndx == SHN_UNDEF)
3146 return FALSE;
3147
3148 /* If the symbol is defined in the common section, then
3149 it is a common definition and so does not count. */
3150 if (bed->common_definition (sym))
3151 return FALSE;
3152
3153 /* If the symbol is in a target specific section then we
3154 must rely upon the backend to tell us what it is. */
3155 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3156 /* FIXME - this function is not coded yet:
3157
3158 return _bfd_is_global_symbol_definition (abfd, sym);
3159
3160 Instead for now assume that the definition is not global,
3161 Even if this is wrong, at least the linker will behave
3162 in the same way that it used to do. */
3163 return FALSE;
3164
3165 return TRUE;
3166 }
3167
3168 /* Search the symbol table of the archive element of the archive ABFD
3169 whose archive map contains a mention of SYMDEF, and determine if
3170 the symbol is defined in this element. */
3171 static bfd_boolean
3172 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3173 {
3174 Elf_Internal_Shdr * hdr;
3175 size_t symcount;
3176 size_t extsymcount;
3177 size_t extsymoff;
3178 Elf_Internal_Sym *isymbuf;
3179 Elf_Internal_Sym *isym;
3180 Elf_Internal_Sym *isymend;
3181 bfd_boolean result;
3182
3183 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3184 if (abfd == NULL)
3185 return FALSE;
3186
3187 if (! bfd_check_format (abfd, bfd_object))
3188 return FALSE;
3189
3190 /* Select the appropriate symbol table. If we don't know if the
3191 object file is an IR object, give linker LTO plugin a chance to
3192 get the correct symbol table. */
3193 if (abfd->plugin_format == bfd_plugin_yes
3194 #if BFD_SUPPORTS_PLUGINS
3195 || (abfd->plugin_format == bfd_plugin_unknown
3196 && bfd_link_plugin_object_p (abfd))
3197 #endif
3198 )
3199 {
3200 /* Use the IR symbol table if the object has been claimed by
3201 plugin. */
3202 abfd = abfd->plugin_dummy_bfd;
3203 hdr = &elf_tdata (abfd)->symtab_hdr;
3204 }
3205 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3206 hdr = &elf_tdata (abfd)->symtab_hdr;
3207 else
3208 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3209
3210 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3211
3212 /* The sh_info field of the symtab header tells us where the
3213 external symbols start. We don't care about the local symbols. */
3214 if (elf_bad_symtab (abfd))
3215 {
3216 extsymcount = symcount;
3217 extsymoff = 0;
3218 }
3219 else
3220 {
3221 extsymcount = symcount - hdr->sh_info;
3222 extsymoff = hdr->sh_info;
3223 }
3224
3225 if (extsymcount == 0)
3226 return FALSE;
3227
3228 /* Read in the symbol table. */
3229 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3230 NULL, NULL, NULL);
3231 if (isymbuf == NULL)
3232 return FALSE;
3233
3234 /* Scan the symbol table looking for SYMDEF. */
3235 result = FALSE;
3236 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3237 {
3238 const char *name;
3239
3240 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3241 isym->st_name);
3242 if (name == NULL)
3243 break;
3244
3245 if (strcmp (name, symdef->name) == 0)
3246 {
3247 result = is_global_data_symbol_definition (abfd, isym);
3248 break;
3249 }
3250 }
3251
3252 free (isymbuf);
3253
3254 return result;
3255 }
3256 \f
3257 /* Add an entry to the .dynamic table. */
3258
3259 bfd_boolean
3260 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3261 bfd_vma tag,
3262 bfd_vma val)
3263 {
3264 struct elf_link_hash_table *hash_table;
3265 const struct elf_backend_data *bed;
3266 asection *s;
3267 bfd_size_type newsize;
3268 bfd_byte *newcontents;
3269 Elf_Internal_Dyn dyn;
3270
3271 hash_table = elf_hash_table (info);
3272 if (! is_elf_hash_table (hash_table))
3273 return FALSE;
3274
3275 bed = get_elf_backend_data (hash_table->dynobj);
3276 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3277 BFD_ASSERT (s != NULL);
3278
3279 newsize = s->size + bed->s->sizeof_dyn;
3280 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3281 if (newcontents == NULL)
3282 return FALSE;
3283
3284 dyn.d_tag = tag;
3285 dyn.d_un.d_val = val;
3286 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3287
3288 s->size = newsize;
3289 s->contents = newcontents;
3290
3291 return TRUE;
3292 }
3293
3294 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3295 otherwise just check whether one already exists. Returns -1 on error,
3296 1 if a DT_NEEDED tag already exists, and 0 on success. */
3297
3298 static int
3299 elf_add_dt_needed_tag (bfd *abfd,
3300 struct bfd_link_info *info,
3301 const char *soname,
3302 bfd_boolean do_it)
3303 {
3304 struct elf_link_hash_table *hash_table;
3305 size_t strindex;
3306
3307 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3308 return -1;
3309
3310 hash_table = elf_hash_table (info);
3311 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3312 if (strindex == (size_t) -1)
3313 return -1;
3314
3315 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3316 {
3317 asection *sdyn;
3318 const struct elf_backend_data *bed;
3319 bfd_byte *extdyn;
3320
3321 bed = get_elf_backend_data (hash_table->dynobj);
3322 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3323 if (sdyn != NULL)
3324 for (extdyn = sdyn->contents;
3325 extdyn < sdyn->contents + sdyn->size;
3326 extdyn += bed->s->sizeof_dyn)
3327 {
3328 Elf_Internal_Dyn dyn;
3329
3330 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3331 if (dyn.d_tag == DT_NEEDED
3332 && dyn.d_un.d_val == strindex)
3333 {
3334 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3335 return 1;
3336 }
3337 }
3338 }
3339
3340 if (do_it)
3341 {
3342 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3343 return -1;
3344
3345 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3346 return -1;
3347 }
3348 else
3349 /* We were just checking for existence of the tag. */
3350 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3351
3352 return 0;
3353 }
3354
3355 /* Return true if SONAME is on the needed list between NEEDED and STOP
3356 (or the end of list if STOP is NULL), and needed by a library that
3357 will be loaded. */
3358
3359 static bfd_boolean
3360 on_needed_list (const char *soname,
3361 struct bfd_link_needed_list *needed,
3362 struct bfd_link_needed_list *stop)
3363 {
3364 struct bfd_link_needed_list *look;
3365 for (look = needed; look != stop; look = look->next)
3366 if (strcmp (soname, look->name) == 0
3367 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3368 /* If needed by a library that itself is not directly
3369 needed, recursively check whether that library is
3370 indirectly needed. Since we add DT_NEEDED entries to
3371 the end of the list, library dependencies appear after
3372 the library. Therefore search prior to the current
3373 LOOK, preventing possible infinite recursion. */
3374 || on_needed_list (elf_dt_name (look->by), needed, look)))
3375 return TRUE;
3376
3377 return FALSE;
3378 }
3379
3380 /* Sort symbol by value, section, and size. */
3381 static int
3382 elf_sort_symbol (const void *arg1, const void *arg2)
3383 {
3384 const struct elf_link_hash_entry *h1;
3385 const struct elf_link_hash_entry *h2;
3386 bfd_signed_vma vdiff;
3387
3388 h1 = *(const struct elf_link_hash_entry **) arg1;
3389 h2 = *(const struct elf_link_hash_entry **) arg2;
3390 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3391 if (vdiff != 0)
3392 return vdiff > 0 ? 1 : -1;
3393 else
3394 {
3395 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3396 if (sdiff != 0)
3397 return sdiff > 0 ? 1 : -1;
3398 }
3399 vdiff = h1->size - h2->size;
3400 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3401 }
3402
3403 /* This function is used to adjust offsets into .dynstr for
3404 dynamic symbols. This is called via elf_link_hash_traverse. */
3405
3406 static bfd_boolean
3407 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3408 {
3409 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3410
3411 if (h->dynindx != -1)
3412 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3413 return TRUE;
3414 }
3415
3416 /* Assign string offsets in .dynstr, update all structures referencing
3417 them. */
3418
3419 static bfd_boolean
3420 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3421 {
3422 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3423 struct elf_link_local_dynamic_entry *entry;
3424 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3425 bfd *dynobj = hash_table->dynobj;
3426 asection *sdyn;
3427 bfd_size_type size;
3428 const struct elf_backend_data *bed;
3429 bfd_byte *extdyn;
3430
3431 _bfd_elf_strtab_finalize (dynstr);
3432 size = _bfd_elf_strtab_size (dynstr);
3433
3434 bed = get_elf_backend_data (dynobj);
3435 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3436 BFD_ASSERT (sdyn != NULL);
3437
3438 /* Update all .dynamic entries referencing .dynstr strings. */
3439 for (extdyn = sdyn->contents;
3440 extdyn < sdyn->contents + sdyn->size;
3441 extdyn += bed->s->sizeof_dyn)
3442 {
3443 Elf_Internal_Dyn dyn;
3444
3445 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3446 switch (dyn.d_tag)
3447 {
3448 case DT_STRSZ:
3449 dyn.d_un.d_val = size;
3450 break;
3451 case DT_NEEDED:
3452 case DT_SONAME:
3453 case DT_RPATH:
3454 case DT_RUNPATH:
3455 case DT_FILTER:
3456 case DT_AUXILIARY:
3457 case DT_AUDIT:
3458 case DT_DEPAUDIT:
3459 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3460 break;
3461 default:
3462 continue;
3463 }
3464 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3465 }
3466
3467 /* Now update local dynamic symbols. */
3468 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3469 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3470 entry->isym.st_name);
3471
3472 /* And the rest of dynamic symbols. */
3473 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3474
3475 /* Adjust version definitions. */
3476 if (elf_tdata (output_bfd)->cverdefs)
3477 {
3478 asection *s;
3479 bfd_byte *p;
3480 size_t i;
3481 Elf_Internal_Verdef def;
3482 Elf_Internal_Verdaux defaux;
3483
3484 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3485 p = s->contents;
3486 do
3487 {
3488 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3489 &def);
3490 p += sizeof (Elf_External_Verdef);
3491 if (def.vd_aux != sizeof (Elf_External_Verdef))
3492 continue;
3493 for (i = 0; i < def.vd_cnt; ++i)
3494 {
3495 _bfd_elf_swap_verdaux_in (output_bfd,
3496 (Elf_External_Verdaux *) p, &defaux);
3497 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3498 defaux.vda_name);
3499 _bfd_elf_swap_verdaux_out (output_bfd,
3500 &defaux, (Elf_External_Verdaux *) p);
3501 p += sizeof (Elf_External_Verdaux);
3502 }
3503 }
3504 while (def.vd_next);
3505 }
3506
3507 /* Adjust version references. */
3508 if (elf_tdata (output_bfd)->verref)
3509 {
3510 asection *s;
3511 bfd_byte *p;
3512 size_t i;
3513 Elf_Internal_Verneed need;
3514 Elf_Internal_Vernaux needaux;
3515
3516 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3517 p = s->contents;
3518 do
3519 {
3520 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3521 &need);
3522 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3523 _bfd_elf_swap_verneed_out (output_bfd, &need,
3524 (Elf_External_Verneed *) p);
3525 p += sizeof (Elf_External_Verneed);
3526 for (i = 0; i < need.vn_cnt; ++i)
3527 {
3528 _bfd_elf_swap_vernaux_in (output_bfd,
3529 (Elf_External_Vernaux *) p, &needaux);
3530 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3531 needaux.vna_name);
3532 _bfd_elf_swap_vernaux_out (output_bfd,
3533 &needaux,
3534 (Elf_External_Vernaux *) p);
3535 p += sizeof (Elf_External_Vernaux);
3536 }
3537 }
3538 while (need.vn_next);
3539 }
3540
3541 return TRUE;
3542 }
3543 \f
3544 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3545 The default is to only match when the INPUT and OUTPUT are exactly
3546 the same target. */
3547
3548 bfd_boolean
3549 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3550 const bfd_target *output)
3551 {
3552 return input == output;
3553 }
3554
3555 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3556 This version is used when different targets for the same architecture
3557 are virtually identical. */
3558
3559 bfd_boolean
3560 _bfd_elf_relocs_compatible (const bfd_target *input,
3561 const bfd_target *output)
3562 {
3563 const struct elf_backend_data *obed, *ibed;
3564
3565 if (input == output)
3566 return TRUE;
3567
3568 ibed = xvec_get_elf_backend_data (input);
3569 obed = xvec_get_elf_backend_data (output);
3570
3571 if (ibed->arch != obed->arch)
3572 return FALSE;
3573
3574 /* If both backends are using this function, deem them compatible. */
3575 return ibed->relocs_compatible == obed->relocs_compatible;
3576 }
3577
3578 /* Make a special call to the linker "notice" function to tell it that
3579 we are about to handle an as-needed lib, or have finished
3580 processing the lib. */
3581
3582 bfd_boolean
3583 _bfd_elf_notice_as_needed (bfd *ibfd,
3584 struct bfd_link_info *info,
3585 enum notice_asneeded_action act)
3586 {
3587 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3588 }
3589
3590 /* Check relocations an ELF object file. */
3591
3592 bfd_boolean
3593 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3594 {
3595 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3596 struct elf_link_hash_table *htab = elf_hash_table (info);
3597
3598 /* If this object is the same format as the output object, and it is
3599 not a shared library, then let the backend look through the
3600 relocs.
3601
3602 This is required to build global offset table entries and to
3603 arrange for dynamic relocs. It is not required for the
3604 particular common case of linking non PIC code, even when linking
3605 against shared libraries, but unfortunately there is no way of
3606 knowing whether an object file has been compiled PIC or not.
3607 Looking through the relocs is not particularly time consuming.
3608 The problem is that we must either (1) keep the relocs in memory,
3609 which causes the linker to require additional runtime memory or
3610 (2) read the relocs twice from the input file, which wastes time.
3611 This would be a good case for using mmap.
3612
3613 I have no idea how to handle linking PIC code into a file of a
3614 different format. It probably can't be done. */
3615 if ((abfd->flags & DYNAMIC) == 0
3616 && is_elf_hash_table (htab)
3617 && bed->check_relocs != NULL
3618 && elf_object_id (abfd) == elf_hash_table_id (htab)
3619 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3620 {
3621 asection *o;
3622
3623 for (o = abfd->sections; o != NULL; o = o->next)
3624 {
3625 Elf_Internal_Rela *internal_relocs;
3626 bfd_boolean ok;
3627
3628 /* Don't check relocations in excluded sections. */
3629 if ((o->flags & SEC_RELOC) == 0
3630 || (o->flags & SEC_EXCLUDE) != 0
3631 || o->reloc_count == 0
3632 || ((info->strip == strip_all || info->strip == strip_debugger)
3633 && (o->flags & SEC_DEBUGGING) != 0)
3634 || bfd_is_abs_section (o->output_section))
3635 continue;
3636
3637 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3638 info->keep_memory);
3639 if (internal_relocs == NULL)
3640 return FALSE;
3641
3642 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3643
3644 if (elf_section_data (o)->relocs != internal_relocs)
3645 free (internal_relocs);
3646
3647 if (! ok)
3648 return FALSE;
3649 }
3650 }
3651
3652 return TRUE;
3653 }
3654
3655 /* Add symbols from an ELF object file to the linker hash table. */
3656
3657 static bfd_boolean
3658 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3659 {
3660 Elf_Internal_Ehdr *ehdr;
3661 Elf_Internal_Shdr *hdr;
3662 size_t symcount;
3663 size_t extsymcount;
3664 size_t extsymoff;
3665 struct elf_link_hash_entry **sym_hash;
3666 bfd_boolean dynamic;
3667 Elf_External_Versym *extversym = NULL;
3668 Elf_External_Versym *ever;
3669 struct elf_link_hash_entry *weaks;
3670 struct elf_link_hash_entry **nondeflt_vers = NULL;
3671 size_t nondeflt_vers_cnt = 0;
3672 Elf_Internal_Sym *isymbuf = NULL;
3673 Elf_Internal_Sym *isym;
3674 Elf_Internal_Sym *isymend;
3675 const struct elf_backend_data *bed;
3676 bfd_boolean add_needed;
3677 struct elf_link_hash_table *htab;
3678 bfd_size_type amt;
3679 void *alloc_mark = NULL;
3680 struct bfd_hash_entry **old_table = NULL;
3681 unsigned int old_size = 0;
3682 unsigned int old_count = 0;
3683 void *old_tab = NULL;
3684 void *old_ent;
3685 struct bfd_link_hash_entry *old_undefs = NULL;
3686 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3687 void *old_strtab = NULL;
3688 size_t tabsize = 0;
3689 asection *s;
3690 bfd_boolean just_syms;
3691
3692 htab = elf_hash_table (info);
3693 bed = get_elf_backend_data (abfd);
3694
3695 if ((abfd->flags & DYNAMIC) == 0)
3696 dynamic = FALSE;
3697 else
3698 {
3699 dynamic = TRUE;
3700
3701 /* You can't use -r against a dynamic object. Also, there's no
3702 hope of using a dynamic object which does not exactly match
3703 the format of the output file. */
3704 if (bfd_link_relocatable (info)
3705 || !is_elf_hash_table (htab)
3706 || info->output_bfd->xvec != abfd->xvec)
3707 {
3708 if (bfd_link_relocatable (info))
3709 bfd_set_error (bfd_error_invalid_operation);
3710 else
3711 bfd_set_error (bfd_error_wrong_format);
3712 goto error_return;
3713 }
3714 }
3715
3716 ehdr = elf_elfheader (abfd);
3717 if (info->warn_alternate_em
3718 && bed->elf_machine_code != ehdr->e_machine
3719 && ((bed->elf_machine_alt1 != 0
3720 && ehdr->e_machine == bed->elf_machine_alt1)
3721 || (bed->elf_machine_alt2 != 0
3722 && ehdr->e_machine == bed->elf_machine_alt2)))
3723 info->callbacks->einfo
3724 /* xgettext:c-format */
3725 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3726 ehdr->e_machine, abfd, bed->elf_machine_code);
3727
3728 /* As a GNU extension, any input sections which are named
3729 .gnu.warning.SYMBOL are treated as warning symbols for the given
3730 symbol. This differs from .gnu.warning sections, which generate
3731 warnings when they are included in an output file. */
3732 /* PR 12761: Also generate this warning when building shared libraries. */
3733 for (s = abfd->sections; s != NULL; s = s->next)
3734 {
3735 const char *name;
3736
3737 name = bfd_get_section_name (abfd, s);
3738 if (CONST_STRNEQ (name, ".gnu.warning."))
3739 {
3740 char *msg;
3741 bfd_size_type sz;
3742
3743 name += sizeof ".gnu.warning." - 1;
3744
3745 /* If this is a shared object, then look up the symbol
3746 in the hash table. If it is there, and it is already
3747 been defined, then we will not be using the entry
3748 from this shared object, so we don't need to warn.
3749 FIXME: If we see the definition in a regular object
3750 later on, we will warn, but we shouldn't. The only
3751 fix is to keep track of what warnings we are supposed
3752 to emit, and then handle them all at the end of the
3753 link. */
3754 if (dynamic)
3755 {
3756 struct elf_link_hash_entry *h;
3757
3758 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3759
3760 /* FIXME: What about bfd_link_hash_common? */
3761 if (h != NULL
3762 && (h->root.type == bfd_link_hash_defined
3763 || h->root.type == bfd_link_hash_defweak))
3764 continue;
3765 }
3766
3767 sz = s->size;
3768 msg = (char *) bfd_alloc (abfd, sz + 1);
3769 if (msg == NULL)
3770 goto error_return;
3771
3772 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3773 goto error_return;
3774
3775 msg[sz] = '\0';
3776
3777 if (! (_bfd_generic_link_add_one_symbol
3778 (info, abfd, name, BSF_WARNING, s, 0, msg,
3779 FALSE, bed->collect, NULL)))
3780 goto error_return;
3781
3782 if (bfd_link_executable (info))
3783 {
3784 /* Clobber the section size so that the warning does
3785 not get copied into the output file. */
3786 s->size = 0;
3787
3788 /* Also set SEC_EXCLUDE, so that symbols defined in
3789 the warning section don't get copied to the output. */
3790 s->flags |= SEC_EXCLUDE;
3791 }
3792 }
3793 }
3794
3795 just_syms = ((s = abfd->sections) != NULL
3796 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3797
3798 add_needed = TRUE;
3799 if (! dynamic)
3800 {
3801 /* If we are creating a shared library, create all the dynamic
3802 sections immediately. We need to attach them to something,
3803 so we attach them to this BFD, provided it is the right
3804 format and is not from ld --just-symbols. Always create the
3805 dynamic sections for -E/--dynamic-list. FIXME: If there
3806 are no input BFD's of the same format as the output, we can't
3807 make a shared library. */
3808 if (!just_syms
3809 && (bfd_link_pic (info)
3810 || (!bfd_link_relocatable (info)
3811 && (info->export_dynamic || info->dynamic)))
3812 && is_elf_hash_table (htab)
3813 && info->output_bfd->xvec == abfd->xvec
3814 && !htab->dynamic_sections_created)
3815 {
3816 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3817 goto error_return;
3818 }
3819 }
3820 else if (!is_elf_hash_table (htab))
3821 goto error_return;
3822 else
3823 {
3824 const char *soname = NULL;
3825 char *audit = NULL;
3826 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3827 const Elf_Internal_Phdr *phdr;
3828 int ret;
3829
3830 /* ld --just-symbols and dynamic objects don't mix very well.
3831 ld shouldn't allow it. */
3832 if (just_syms)
3833 abort ();
3834
3835 /* If this dynamic lib was specified on the command line with
3836 --as-needed in effect, then we don't want to add a DT_NEEDED
3837 tag unless the lib is actually used. Similary for libs brought
3838 in by another lib's DT_NEEDED. When --no-add-needed is used
3839 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3840 any dynamic library in DT_NEEDED tags in the dynamic lib at
3841 all. */
3842 add_needed = (elf_dyn_lib_class (abfd)
3843 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3844 | DYN_NO_NEEDED)) == 0;
3845
3846 s = bfd_get_section_by_name (abfd, ".dynamic");
3847 if (s != NULL)
3848 {
3849 bfd_byte *dynbuf;
3850 bfd_byte *extdyn;
3851 unsigned int elfsec;
3852 unsigned long shlink;
3853
3854 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3855 {
3856 error_free_dyn:
3857 free (dynbuf);
3858 goto error_return;
3859 }
3860
3861 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3862 if (elfsec == SHN_BAD)
3863 goto error_free_dyn;
3864 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3865
3866 for (extdyn = dynbuf;
3867 extdyn < dynbuf + s->size;
3868 extdyn += bed->s->sizeof_dyn)
3869 {
3870 Elf_Internal_Dyn dyn;
3871
3872 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3873 if (dyn.d_tag == DT_SONAME)
3874 {
3875 unsigned int tagv = dyn.d_un.d_val;
3876 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3877 if (soname == NULL)
3878 goto error_free_dyn;
3879 }
3880 if (dyn.d_tag == DT_NEEDED)
3881 {
3882 struct bfd_link_needed_list *n, **pn;
3883 char *fnm, *anm;
3884 unsigned int tagv = dyn.d_un.d_val;
3885
3886 amt = sizeof (struct bfd_link_needed_list);
3887 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3888 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3889 if (n == NULL || fnm == NULL)
3890 goto error_free_dyn;
3891 amt = strlen (fnm) + 1;
3892 anm = (char *) bfd_alloc (abfd, amt);
3893 if (anm == NULL)
3894 goto error_free_dyn;
3895 memcpy (anm, fnm, amt);
3896 n->name = anm;
3897 n->by = abfd;
3898 n->next = NULL;
3899 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3900 ;
3901 *pn = n;
3902 }
3903 if (dyn.d_tag == DT_RUNPATH)
3904 {
3905 struct bfd_link_needed_list *n, **pn;
3906 char *fnm, *anm;
3907 unsigned int tagv = dyn.d_un.d_val;
3908
3909 amt = sizeof (struct bfd_link_needed_list);
3910 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3911 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3912 if (n == NULL || fnm == NULL)
3913 goto error_free_dyn;
3914 amt = strlen (fnm) + 1;
3915 anm = (char *) bfd_alloc (abfd, amt);
3916 if (anm == NULL)
3917 goto error_free_dyn;
3918 memcpy (anm, fnm, amt);
3919 n->name = anm;
3920 n->by = abfd;
3921 n->next = NULL;
3922 for (pn = & runpath;
3923 *pn != NULL;
3924 pn = &(*pn)->next)
3925 ;
3926 *pn = n;
3927 }
3928 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3929 if (!runpath && dyn.d_tag == DT_RPATH)
3930 {
3931 struct bfd_link_needed_list *n, **pn;
3932 char *fnm, *anm;
3933 unsigned int tagv = dyn.d_un.d_val;
3934
3935 amt = sizeof (struct bfd_link_needed_list);
3936 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3937 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3938 if (n == NULL || fnm == NULL)
3939 goto error_free_dyn;
3940 amt = strlen (fnm) + 1;
3941 anm = (char *) bfd_alloc (abfd, amt);
3942 if (anm == NULL)
3943 goto error_free_dyn;
3944 memcpy (anm, fnm, amt);
3945 n->name = anm;
3946 n->by = abfd;
3947 n->next = NULL;
3948 for (pn = & rpath;
3949 *pn != NULL;
3950 pn = &(*pn)->next)
3951 ;
3952 *pn = n;
3953 }
3954 if (dyn.d_tag == DT_AUDIT)
3955 {
3956 unsigned int tagv = dyn.d_un.d_val;
3957 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3958 }
3959 }
3960
3961 free (dynbuf);
3962 }
3963
3964 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3965 frees all more recently bfd_alloc'd blocks as well. */
3966 if (runpath)
3967 rpath = runpath;
3968
3969 if (rpath)
3970 {
3971 struct bfd_link_needed_list **pn;
3972 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3973 ;
3974 *pn = rpath;
3975 }
3976
3977 /* If we have a PT_GNU_RELRO program header, mark as read-only
3978 all sections contained fully therein. This makes relro
3979 shared library sections appear as they will at run-time. */
3980 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
3981 while (--phdr >= elf_tdata (abfd)->phdr)
3982 if (phdr->p_type == PT_GNU_RELRO)
3983 {
3984 for (s = abfd->sections; s != NULL; s = s->next)
3985 if ((s->flags & SEC_ALLOC) != 0
3986 && s->vma >= phdr->p_vaddr
3987 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
3988 s->flags |= SEC_READONLY;
3989 break;
3990 }
3991
3992 /* We do not want to include any of the sections in a dynamic
3993 object in the output file. We hack by simply clobbering the
3994 list of sections in the BFD. This could be handled more
3995 cleanly by, say, a new section flag; the existing
3996 SEC_NEVER_LOAD flag is not the one we want, because that one
3997 still implies that the section takes up space in the output
3998 file. */
3999 bfd_section_list_clear (abfd);
4000
4001 /* Find the name to use in a DT_NEEDED entry that refers to this
4002 object. If the object has a DT_SONAME entry, we use it.
4003 Otherwise, if the generic linker stuck something in
4004 elf_dt_name, we use that. Otherwise, we just use the file
4005 name. */
4006 if (soname == NULL || *soname == '\0')
4007 {
4008 soname = elf_dt_name (abfd);
4009 if (soname == NULL || *soname == '\0')
4010 soname = bfd_get_filename (abfd);
4011 }
4012
4013 /* Save the SONAME because sometimes the linker emulation code
4014 will need to know it. */
4015 elf_dt_name (abfd) = soname;
4016
4017 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4018 if (ret < 0)
4019 goto error_return;
4020
4021 /* If we have already included this dynamic object in the
4022 link, just ignore it. There is no reason to include a
4023 particular dynamic object more than once. */
4024 if (ret > 0)
4025 return TRUE;
4026
4027 /* Save the DT_AUDIT entry for the linker emulation code. */
4028 elf_dt_audit (abfd) = audit;
4029 }
4030
4031 /* If this is a dynamic object, we always link against the .dynsym
4032 symbol table, not the .symtab symbol table. The dynamic linker
4033 will only see the .dynsym symbol table, so there is no reason to
4034 look at .symtab for a dynamic object. */
4035
4036 if (! dynamic || elf_dynsymtab (abfd) == 0)
4037 hdr = &elf_tdata (abfd)->symtab_hdr;
4038 else
4039 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4040
4041 symcount = hdr->sh_size / bed->s->sizeof_sym;
4042
4043 /* The sh_info field of the symtab header tells us where the
4044 external symbols start. We don't care about the local symbols at
4045 this point. */
4046 if (elf_bad_symtab (abfd))
4047 {
4048 extsymcount = symcount;
4049 extsymoff = 0;
4050 }
4051 else
4052 {
4053 extsymcount = symcount - hdr->sh_info;
4054 extsymoff = hdr->sh_info;
4055 }
4056
4057 sym_hash = elf_sym_hashes (abfd);
4058 if (extsymcount != 0)
4059 {
4060 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4061 NULL, NULL, NULL);
4062 if (isymbuf == NULL)
4063 goto error_return;
4064
4065 if (sym_hash == NULL)
4066 {
4067 /* We store a pointer to the hash table entry for each
4068 external symbol. */
4069 amt = extsymcount;
4070 amt *= sizeof (struct elf_link_hash_entry *);
4071 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4072 if (sym_hash == NULL)
4073 goto error_free_sym;
4074 elf_sym_hashes (abfd) = sym_hash;
4075 }
4076 }
4077
4078 if (dynamic)
4079 {
4080 /* Read in any version definitions. */
4081 if (!_bfd_elf_slurp_version_tables (abfd,
4082 info->default_imported_symver))
4083 goto error_free_sym;
4084
4085 /* Read in the symbol versions, but don't bother to convert them
4086 to internal format. */
4087 if (elf_dynversym (abfd) != 0)
4088 {
4089 Elf_Internal_Shdr *versymhdr;
4090
4091 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4092 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4093 if (extversym == NULL)
4094 goto error_free_sym;
4095 amt = versymhdr->sh_size;
4096 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4097 || bfd_bread (extversym, amt, abfd) != amt)
4098 goto error_free_vers;
4099 }
4100 }
4101
4102 /* If we are loading an as-needed shared lib, save the symbol table
4103 state before we start adding symbols. If the lib turns out
4104 to be unneeded, restore the state. */
4105 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4106 {
4107 unsigned int i;
4108 size_t entsize;
4109
4110 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4111 {
4112 struct bfd_hash_entry *p;
4113 struct elf_link_hash_entry *h;
4114
4115 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4116 {
4117 h = (struct elf_link_hash_entry *) p;
4118 entsize += htab->root.table.entsize;
4119 if (h->root.type == bfd_link_hash_warning)
4120 entsize += htab->root.table.entsize;
4121 }
4122 }
4123
4124 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4125 old_tab = bfd_malloc (tabsize + entsize);
4126 if (old_tab == NULL)
4127 goto error_free_vers;
4128
4129 /* Remember the current objalloc pointer, so that all mem for
4130 symbols added can later be reclaimed. */
4131 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4132 if (alloc_mark == NULL)
4133 goto error_free_vers;
4134
4135 /* Make a special call to the linker "notice" function to
4136 tell it that we are about to handle an as-needed lib. */
4137 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4138 goto error_free_vers;
4139
4140 /* Clone the symbol table. Remember some pointers into the
4141 symbol table, and dynamic symbol count. */
4142 old_ent = (char *) old_tab + tabsize;
4143 memcpy (old_tab, htab->root.table.table, tabsize);
4144 old_undefs = htab->root.undefs;
4145 old_undefs_tail = htab->root.undefs_tail;
4146 old_table = htab->root.table.table;
4147 old_size = htab->root.table.size;
4148 old_count = htab->root.table.count;
4149 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4150 if (old_strtab == NULL)
4151 goto error_free_vers;
4152
4153 for (i = 0; i < htab->root.table.size; i++)
4154 {
4155 struct bfd_hash_entry *p;
4156 struct elf_link_hash_entry *h;
4157
4158 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4159 {
4160 memcpy (old_ent, p, htab->root.table.entsize);
4161 old_ent = (char *) old_ent + htab->root.table.entsize;
4162 h = (struct elf_link_hash_entry *) p;
4163 if (h->root.type == bfd_link_hash_warning)
4164 {
4165 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4166 old_ent = (char *) old_ent + htab->root.table.entsize;
4167 }
4168 }
4169 }
4170 }
4171
4172 weaks = NULL;
4173 ever = extversym != NULL ? extversym + extsymoff : NULL;
4174 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4175 isym < isymend;
4176 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4177 {
4178 int bind;
4179 bfd_vma value;
4180 asection *sec, *new_sec;
4181 flagword flags;
4182 const char *name;
4183 struct elf_link_hash_entry *h;
4184 struct elf_link_hash_entry *hi;
4185 bfd_boolean definition;
4186 bfd_boolean size_change_ok;
4187 bfd_boolean type_change_ok;
4188 bfd_boolean new_weakdef;
4189 bfd_boolean new_weak;
4190 bfd_boolean old_weak;
4191 bfd_boolean override;
4192 bfd_boolean common;
4193 bfd_boolean discarded;
4194 unsigned int old_alignment;
4195 bfd *old_bfd;
4196 bfd_boolean matched;
4197
4198 override = FALSE;
4199
4200 flags = BSF_NO_FLAGS;
4201 sec = NULL;
4202 value = isym->st_value;
4203 common = bed->common_definition (isym);
4204 discarded = FALSE;
4205
4206 bind = ELF_ST_BIND (isym->st_info);
4207 switch (bind)
4208 {
4209 case STB_LOCAL:
4210 /* This should be impossible, since ELF requires that all
4211 global symbols follow all local symbols, and that sh_info
4212 point to the first global symbol. Unfortunately, Irix 5
4213 screws this up. */
4214 continue;
4215
4216 case STB_GLOBAL:
4217 if (isym->st_shndx != SHN_UNDEF && !common)
4218 flags = BSF_GLOBAL;
4219 break;
4220
4221 case STB_WEAK:
4222 flags = BSF_WEAK;
4223 break;
4224
4225 case STB_GNU_UNIQUE:
4226 flags = BSF_GNU_UNIQUE;
4227 break;
4228
4229 default:
4230 /* Leave it up to the processor backend. */
4231 break;
4232 }
4233
4234 if (isym->st_shndx == SHN_UNDEF)
4235 sec = bfd_und_section_ptr;
4236 else if (isym->st_shndx == SHN_ABS)
4237 sec = bfd_abs_section_ptr;
4238 else if (isym->st_shndx == SHN_COMMON)
4239 {
4240 sec = bfd_com_section_ptr;
4241 /* What ELF calls the size we call the value. What ELF
4242 calls the value we call the alignment. */
4243 value = isym->st_size;
4244 }
4245 else
4246 {
4247 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4248 if (sec == NULL)
4249 sec = bfd_abs_section_ptr;
4250 else if (discarded_section (sec))
4251 {
4252 /* Symbols from discarded section are undefined. We keep
4253 its visibility. */
4254 sec = bfd_und_section_ptr;
4255 discarded = TRUE;
4256 isym->st_shndx = SHN_UNDEF;
4257 }
4258 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4259 value -= sec->vma;
4260 }
4261
4262 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4263 isym->st_name);
4264 if (name == NULL)
4265 goto error_free_vers;
4266
4267 if (isym->st_shndx == SHN_COMMON
4268 && (abfd->flags & BFD_PLUGIN) != 0)
4269 {
4270 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4271
4272 if (xc == NULL)
4273 {
4274 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4275 | SEC_EXCLUDE);
4276 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4277 if (xc == NULL)
4278 goto error_free_vers;
4279 }
4280 sec = xc;
4281 }
4282 else if (isym->st_shndx == SHN_COMMON
4283 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4284 && !bfd_link_relocatable (info))
4285 {
4286 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4287
4288 if (tcomm == NULL)
4289 {
4290 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4291 | SEC_LINKER_CREATED);
4292 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4293 if (tcomm == NULL)
4294 goto error_free_vers;
4295 }
4296 sec = tcomm;
4297 }
4298 else if (bed->elf_add_symbol_hook)
4299 {
4300 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4301 &sec, &value))
4302 goto error_free_vers;
4303
4304 /* The hook function sets the name to NULL if this symbol
4305 should be skipped for some reason. */
4306 if (name == NULL)
4307 continue;
4308 }
4309
4310 /* Sanity check that all possibilities were handled. */
4311 if (sec == NULL)
4312 {
4313 bfd_set_error (bfd_error_bad_value);
4314 goto error_free_vers;
4315 }
4316
4317 /* Silently discard TLS symbols from --just-syms. There's
4318 no way to combine a static TLS block with a new TLS block
4319 for this executable. */
4320 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4321 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4322 continue;
4323
4324 if (bfd_is_und_section (sec)
4325 || bfd_is_com_section (sec))
4326 definition = FALSE;
4327 else
4328 definition = TRUE;
4329
4330 size_change_ok = FALSE;
4331 type_change_ok = bed->type_change_ok;
4332 old_weak = FALSE;
4333 matched = FALSE;
4334 old_alignment = 0;
4335 old_bfd = NULL;
4336 new_sec = sec;
4337
4338 if (is_elf_hash_table (htab))
4339 {
4340 Elf_Internal_Versym iver;
4341 unsigned int vernum = 0;
4342 bfd_boolean skip;
4343
4344 if (ever == NULL)
4345 {
4346 if (info->default_imported_symver)
4347 /* Use the default symbol version created earlier. */
4348 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4349 else
4350 iver.vs_vers = 0;
4351 }
4352 else
4353 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4354
4355 vernum = iver.vs_vers & VERSYM_VERSION;
4356
4357 /* If this is a hidden symbol, or if it is not version
4358 1, we append the version name to the symbol name.
4359 However, we do not modify a non-hidden absolute symbol
4360 if it is not a function, because it might be the version
4361 symbol itself. FIXME: What if it isn't? */
4362 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4363 || (vernum > 1
4364 && (!bfd_is_abs_section (sec)
4365 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4366 {
4367 const char *verstr;
4368 size_t namelen, verlen, newlen;
4369 char *newname, *p;
4370
4371 if (isym->st_shndx != SHN_UNDEF)
4372 {
4373 if (vernum > elf_tdata (abfd)->cverdefs)
4374 verstr = NULL;
4375 else if (vernum > 1)
4376 verstr =
4377 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4378 else
4379 verstr = "";
4380
4381 if (verstr == NULL)
4382 {
4383 _bfd_error_handler
4384 /* xgettext:c-format */
4385 (_("%B: %s: invalid version %u (max %d)"),
4386 abfd, name, vernum,
4387 elf_tdata (abfd)->cverdefs);
4388 bfd_set_error (bfd_error_bad_value);
4389 goto error_free_vers;
4390 }
4391 }
4392 else
4393 {
4394 /* We cannot simply test for the number of
4395 entries in the VERNEED section since the
4396 numbers for the needed versions do not start
4397 at 0. */
4398 Elf_Internal_Verneed *t;
4399
4400 verstr = NULL;
4401 for (t = elf_tdata (abfd)->verref;
4402 t != NULL;
4403 t = t->vn_nextref)
4404 {
4405 Elf_Internal_Vernaux *a;
4406
4407 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4408 {
4409 if (a->vna_other == vernum)
4410 {
4411 verstr = a->vna_nodename;
4412 break;
4413 }
4414 }
4415 if (a != NULL)
4416 break;
4417 }
4418 if (verstr == NULL)
4419 {
4420 _bfd_error_handler
4421 /* xgettext:c-format */
4422 (_("%B: %s: invalid needed version %d"),
4423 abfd, name, vernum);
4424 bfd_set_error (bfd_error_bad_value);
4425 goto error_free_vers;
4426 }
4427 }
4428
4429 namelen = strlen (name);
4430 verlen = strlen (verstr);
4431 newlen = namelen + verlen + 2;
4432 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4433 && isym->st_shndx != SHN_UNDEF)
4434 ++newlen;
4435
4436 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4437 if (newname == NULL)
4438 goto error_free_vers;
4439 memcpy (newname, name, namelen);
4440 p = newname + namelen;
4441 *p++ = ELF_VER_CHR;
4442 /* If this is a defined non-hidden version symbol,
4443 we add another @ to the name. This indicates the
4444 default version of the symbol. */
4445 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4446 && isym->st_shndx != SHN_UNDEF)
4447 *p++ = ELF_VER_CHR;
4448 memcpy (p, verstr, verlen + 1);
4449
4450 name = newname;
4451 }
4452
4453 /* If this symbol has default visibility and the user has
4454 requested we not re-export it, then mark it as hidden. */
4455 if (!bfd_is_und_section (sec)
4456 && !dynamic
4457 && abfd->no_export
4458 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4459 isym->st_other = (STV_HIDDEN
4460 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4461
4462 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4463 sym_hash, &old_bfd, &old_weak,
4464 &old_alignment, &skip, &override,
4465 &type_change_ok, &size_change_ok,
4466 &matched))
4467 goto error_free_vers;
4468
4469 if (skip)
4470 continue;
4471
4472 /* Override a definition only if the new symbol matches the
4473 existing one. */
4474 if (override && matched)
4475 definition = FALSE;
4476
4477 h = *sym_hash;
4478 while (h->root.type == bfd_link_hash_indirect
4479 || h->root.type == bfd_link_hash_warning)
4480 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4481
4482 if (elf_tdata (abfd)->verdef != NULL
4483 && vernum > 1
4484 && definition)
4485 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4486 }
4487
4488 if (! (_bfd_generic_link_add_one_symbol
4489 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4490 (struct bfd_link_hash_entry **) sym_hash)))
4491 goto error_free_vers;
4492
4493 if ((flags & BSF_GNU_UNIQUE)
4494 && (abfd->flags & DYNAMIC) == 0
4495 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4496 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique;
4497
4498 h = *sym_hash;
4499 /* We need to make sure that indirect symbol dynamic flags are
4500 updated. */
4501 hi = h;
4502 while (h->root.type == bfd_link_hash_indirect
4503 || h->root.type == bfd_link_hash_warning)
4504 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4505
4506 /* Setting the index to -3 tells elf_link_output_extsym that
4507 this symbol is defined in a discarded section. */
4508 if (discarded)
4509 h->indx = -3;
4510
4511 *sym_hash = h;
4512
4513 new_weak = (flags & BSF_WEAK) != 0;
4514 new_weakdef = FALSE;
4515 if (dynamic
4516 && definition
4517 && new_weak
4518 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4519 && is_elf_hash_table (htab)
4520 && h->u.weakdef == NULL)
4521 {
4522 /* Keep a list of all weak defined non function symbols from
4523 a dynamic object, using the weakdef field. Later in this
4524 function we will set the weakdef field to the correct
4525 value. We only put non-function symbols from dynamic
4526 objects on this list, because that happens to be the only
4527 time we need to know the normal symbol corresponding to a
4528 weak symbol, and the information is time consuming to
4529 figure out. If the weakdef field is not already NULL,
4530 then this symbol was already defined by some previous
4531 dynamic object, and we will be using that previous
4532 definition anyhow. */
4533
4534 h->u.weakdef = weaks;
4535 weaks = h;
4536 new_weakdef = TRUE;
4537 }
4538
4539 /* Set the alignment of a common symbol. */
4540 if ((common || bfd_is_com_section (sec))
4541 && h->root.type == bfd_link_hash_common)
4542 {
4543 unsigned int align;
4544
4545 if (common)
4546 align = bfd_log2 (isym->st_value);
4547 else
4548 {
4549 /* The new symbol is a common symbol in a shared object.
4550 We need to get the alignment from the section. */
4551 align = new_sec->alignment_power;
4552 }
4553 if (align > old_alignment)
4554 h->root.u.c.p->alignment_power = align;
4555 else
4556 h->root.u.c.p->alignment_power = old_alignment;
4557 }
4558
4559 if (is_elf_hash_table (htab))
4560 {
4561 /* Set a flag in the hash table entry indicating the type of
4562 reference or definition we just found. A dynamic symbol
4563 is one which is referenced or defined by both a regular
4564 object and a shared object. */
4565 bfd_boolean dynsym = FALSE;
4566
4567 /* Plugin symbols aren't normal. Don't set def_regular or
4568 ref_regular for them, or make them dynamic. */
4569 if ((abfd->flags & BFD_PLUGIN) != 0)
4570 ;
4571 else if (! dynamic)
4572 {
4573 if (! definition)
4574 {
4575 h->ref_regular = 1;
4576 if (bind != STB_WEAK)
4577 h->ref_regular_nonweak = 1;
4578 }
4579 else
4580 {
4581 h->def_regular = 1;
4582 if (h->def_dynamic)
4583 {
4584 h->def_dynamic = 0;
4585 h->ref_dynamic = 1;
4586 }
4587 }
4588
4589 /* If the indirect symbol has been forced local, don't
4590 make the real symbol dynamic. */
4591 if ((h == hi || !hi->forced_local)
4592 && (bfd_link_dll (info)
4593 || h->def_dynamic
4594 || h->ref_dynamic))
4595 dynsym = TRUE;
4596 }
4597 else
4598 {
4599 if (! definition)
4600 {
4601 h->ref_dynamic = 1;
4602 hi->ref_dynamic = 1;
4603 }
4604 else
4605 {
4606 h->def_dynamic = 1;
4607 hi->def_dynamic = 1;
4608 }
4609
4610 /* If the indirect symbol has been forced local, don't
4611 make the real symbol dynamic. */
4612 if ((h == hi || !hi->forced_local)
4613 && (h->def_regular
4614 || h->ref_regular
4615 || (h->u.weakdef != NULL
4616 && ! new_weakdef
4617 && h->u.weakdef->dynindx != -1)))
4618 dynsym = TRUE;
4619 }
4620
4621 /* Check to see if we need to add an indirect symbol for
4622 the default name. */
4623 if (definition
4624 || (!override && h->root.type == bfd_link_hash_common))
4625 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4626 sec, value, &old_bfd, &dynsym))
4627 goto error_free_vers;
4628
4629 /* Check the alignment when a common symbol is involved. This
4630 can change when a common symbol is overridden by a normal
4631 definition or a common symbol is ignored due to the old
4632 normal definition. We need to make sure the maximum
4633 alignment is maintained. */
4634 if ((old_alignment || common)
4635 && h->root.type != bfd_link_hash_common)
4636 {
4637 unsigned int common_align;
4638 unsigned int normal_align;
4639 unsigned int symbol_align;
4640 bfd *normal_bfd;
4641 bfd *common_bfd;
4642
4643 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4644 || h->root.type == bfd_link_hash_defweak);
4645
4646 symbol_align = ffs (h->root.u.def.value) - 1;
4647 if (h->root.u.def.section->owner != NULL
4648 && (h->root.u.def.section->owner->flags
4649 & (DYNAMIC | BFD_PLUGIN)) == 0)
4650 {
4651 normal_align = h->root.u.def.section->alignment_power;
4652 if (normal_align > symbol_align)
4653 normal_align = symbol_align;
4654 }
4655 else
4656 normal_align = symbol_align;
4657
4658 if (old_alignment)
4659 {
4660 common_align = old_alignment;
4661 common_bfd = old_bfd;
4662 normal_bfd = abfd;
4663 }
4664 else
4665 {
4666 common_align = bfd_log2 (isym->st_value);
4667 common_bfd = abfd;
4668 normal_bfd = old_bfd;
4669 }
4670
4671 if (normal_align < common_align)
4672 {
4673 /* PR binutils/2735 */
4674 if (normal_bfd == NULL)
4675 _bfd_error_handler
4676 /* xgettext:c-format */
4677 (_("Warning: alignment %u of common symbol `%s' in %B is"
4678 " greater than the alignment (%u) of its section %A"),
4679 common_bfd, h->root.u.def.section,
4680 1 << common_align, name, 1 << normal_align);
4681 else
4682 _bfd_error_handler
4683 /* xgettext:c-format */
4684 (_("Warning: alignment %u of symbol `%s' in %B"
4685 " is smaller than %u in %B"),
4686 normal_bfd, common_bfd,
4687 1 << normal_align, name, 1 << common_align);
4688 }
4689 }
4690
4691 /* Remember the symbol size if it isn't undefined. */
4692 if (isym->st_size != 0
4693 && isym->st_shndx != SHN_UNDEF
4694 && (definition || h->size == 0))
4695 {
4696 if (h->size != 0
4697 && h->size != isym->st_size
4698 && ! size_change_ok)
4699 _bfd_error_handler
4700 /* xgettext:c-format */
4701 (_("Warning: size of symbol `%s' changed"
4702 " from %lu in %B to %lu in %B"),
4703 old_bfd, abfd,
4704 name, (unsigned long) h->size,
4705 (unsigned long) isym->st_size);
4706
4707 h->size = isym->st_size;
4708 }
4709
4710 /* If this is a common symbol, then we always want H->SIZE
4711 to be the size of the common symbol. The code just above
4712 won't fix the size if a common symbol becomes larger. We
4713 don't warn about a size change here, because that is
4714 covered by --warn-common. Allow changes between different
4715 function types. */
4716 if (h->root.type == bfd_link_hash_common)
4717 h->size = h->root.u.c.size;
4718
4719 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4720 && ((definition && !new_weak)
4721 || (old_weak && h->root.type == bfd_link_hash_common)
4722 || h->type == STT_NOTYPE))
4723 {
4724 unsigned int type = ELF_ST_TYPE (isym->st_info);
4725
4726 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4727 symbol. */
4728 if (type == STT_GNU_IFUNC
4729 && (abfd->flags & DYNAMIC) != 0)
4730 type = STT_FUNC;
4731
4732 if (h->type != type)
4733 {
4734 if (h->type != STT_NOTYPE && ! type_change_ok)
4735 /* xgettext:c-format */
4736 _bfd_error_handler
4737 (_("Warning: type of symbol `%s' changed"
4738 " from %d to %d in %B"),
4739 abfd, name, h->type, type);
4740
4741 h->type = type;
4742 }
4743 }
4744
4745 /* Merge st_other field. */
4746 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4747
4748 /* We don't want to make debug symbol dynamic. */
4749 if (definition
4750 && (sec->flags & SEC_DEBUGGING)
4751 && !bfd_link_relocatable (info))
4752 dynsym = FALSE;
4753
4754 /* Nor should we make plugin symbols dynamic. */
4755 if ((abfd->flags & BFD_PLUGIN) != 0)
4756 dynsym = FALSE;
4757
4758 if (definition)
4759 {
4760 h->target_internal = isym->st_target_internal;
4761 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4762 }
4763
4764 if (definition && !dynamic)
4765 {
4766 char *p = strchr (name, ELF_VER_CHR);
4767 if (p != NULL && p[1] != ELF_VER_CHR)
4768 {
4769 /* Queue non-default versions so that .symver x, x@FOO
4770 aliases can be checked. */
4771 if (!nondeflt_vers)
4772 {
4773 amt = ((isymend - isym + 1)
4774 * sizeof (struct elf_link_hash_entry *));
4775 nondeflt_vers
4776 = (struct elf_link_hash_entry **) bfd_malloc (amt);
4777 if (!nondeflt_vers)
4778 goto error_free_vers;
4779 }
4780 nondeflt_vers[nondeflt_vers_cnt++] = h;
4781 }
4782 }
4783
4784 if (dynsym && h->dynindx == -1)
4785 {
4786 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4787 goto error_free_vers;
4788 if (h->u.weakdef != NULL
4789 && ! new_weakdef
4790 && h->u.weakdef->dynindx == -1)
4791 {
4792 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4793 goto error_free_vers;
4794 }
4795 }
4796 else if (h->dynindx != -1)
4797 /* If the symbol already has a dynamic index, but
4798 visibility says it should not be visible, turn it into
4799 a local symbol. */
4800 switch (ELF_ST_VISIBILITY (h->other))
4801 {
4802 case STV_INTERNAL:
4803 case STV_HIDDEN:
4804 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4805 dynsym = FALSE;
4806 break;
4807 }
4808
4809 /* Don't add DT_NEEDED for references from the dummy bfd nor
4810 for unmatched symbol. */
4811 if (!add_needed
4812 && matched
4813 && definition
4814 && ((dynsym
4815 && h->ref_regular_nonweak
4816 && (old_bfd == NULL
4817 || (old_bfd->flags & BFD_PLUGIN) == 0))
4818 || (h->ref_dynamic_nonweak
4819 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4820 && !on_needed_list (elf_dt_name (abfd),
4821 htab->needed, NULL))))
4822 {
4823 int ret;
4824 const char *soname = elf_dt_name (abfd);
4825
4826 info->callbacks->minfo ("%!", soname, old_bfd,
4827 h->root.root.string);
4828
4829 /* A symbol from a library loaded via DT_NEEDED of some
4830 other library is referenced by a regular object.
4831 Add a DT_NEEDED entry for it. Issue an error if
4832 --no-add-needed is used and the reference was not
4833 a weak one. */
4834 if (old_bfd != NULL
4835 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4836 {
4837 _bfd_error_handler
4838 /* xgettext:c-format */
4839 (_("%B: undefined reference to symbol '%s'"),
4840 old_bfd, name);
4841 bfd_set_error (bfd_error_missing_dso);
4842 goto error_free_vers;
4843 }
4844
4845 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4846 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
4847
4848 add_needed = TRUE;
4849 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4850 if (ret < 0)
4851 goto error_free_vers;
4852
4853 BFD_ASSERT (ret == 0);
4854 }
4855 }
4856 }
4857
4858 if (extversym != NULL)
4859 {
4860 free (extversym);
4861 extversym = NULL;
4862 }
4863
4864 if (isymbuf != NULL)
4865 {
4866 free (isymbuf);
4867 isymbuf = NULL;
4868 }
4869
4870 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4871 {
4872 unsigned int i;
4873
4874 /* Restore the symbol table. */
4875 old_ent = (char *) old_tab + tabsize;
4876 memset (elf_sym_hashes (abfd), 0,
4877 extsymcount * sizeof (struct elf_link_hash_entry *));
4878 htab->root.table.table = old_table;
4879 htab->root.table.size = old_size;
4880 htab->root.table.count = old_count;
4881 memcpy (htab->root.table.table, old_tab, tabsize);
4882 htab->root.undefs = old_undefs;
4883 htab->root.undefs_tail = old_undefs_tail;
4884 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
4885 free (old_strtab);
4886 old_strtab = NULL;
4887 for (i = 0; i < htab->root.table.size; i++)
4888 {
4889 struct bfd_hash_entry *p;
4890 struct elf_link_hash_entry *h;
4891 bfd_size_type size;
4892 unsigned int alignment_power;
4893
4894 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4895 {
4896 h = (struct elf_link_hash_entry *) p;
4897 if (h->root.type == bfd_link_hash_warning)
4898 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4899
4900 /* Preserve the maximum alignment and size for common
4901 symbols even if this dynamic lib isn't on DT_NEEDED
4902 since it can still be loaded at run time by another
4903 dynamic lib. */
4904 if (h->root.type == bfd_link_hash_common)
4905 {
4906 size = h->root.u.c.size;
4907 alignment_power = h->root.u.c.p->alignment_power;
4908 }
4909 else
4910 {
4911 size = 0;
4912 alignment_power = 0;
4913 }
4914 memcpy (p, old_ent, htab->root.table.entsize);
4915 old_ent = (char *) old_ent + htab->root.table.entsize;
4916 h = (struct elf_link_hash_entry *) p;
4917 if (h->root.type == bfd_link_hash_warning)
4918 {
4919 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4920 old_ent = (char *) old_ent + htab->root.table.entsize;
4921 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4922 }
4923 if (h->root.type == bfd_link_hash_common)
4924 {
4925 if (size > h->root.u.c.size)
4926 h->root.u.c.size = size;
4927 if (alignment_power > h->root.u.c.p->alignment_power)
4928 h->root.u.c.p->alignment_power = alignment_power;
4929 }
4930 }
4931 }
4932
4933 /* Make a special call to the linker "notice" function to
4934 tell it that symbols added for crefs may need to be removed. */
4935 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
4936 goto error_free_vers;
4937
4938 free (old_tab);
4939 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4940 alloc_mark);
4941 if (nondeflt_vers != NULL)
4942 free (nondeflt_vers);
4943 return TRUE;
4944 }
4945
4946 if (old_tab != NULL)
4947 {
4948 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
4949 goto error_free_vers;
4950 free (old_tab);
4951 old_tab = NULL;
4952 }
4953
4954 /* Now that all the symbols from this input file are created, if
4955 not performing a relocatable link, handle .symver foo, foo@BAR
4956 such that any relocs against foo become foo@BAR. */
4957 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4958 {
4959 size_t cnt, symidx;
4960
4961 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4962 {
4963 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4964 char *shortname, *p;
4965
4966 p = strchr (h->root.root.string, ELF_VER_CHR);
4967 if (p == NULL
4968 || (h->root.type != bfd_link_hash_defined
4969 && h->root.type != bfd_link_hash_defweak))
4970 continue;
4971
4972 amt = p - h->root.root.string;
4973 shortname = (char *) bfd_malloc (amt + 1);
4974 if (!shortname)
4975 goto error_free_vers;
4976 memcpy (shortname, h->root.root.string, amt);
4977 shortname[amt] = '\0';
4978
4979 hi = (struct elf_link_hash_entry *)
4980 bfd_link_hash_lookup (&htab->root, shortname,
4981 FALSE, FALSE, FALSE);
4982 if (hi != NULL
4983 && hi->root.type == h->root.type
4984 && hi->root.u.def.value == h->root.u.def.value
4985 && hi->root.u.def.section == h->root.u.def.section)
4986 {
4987 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4988 hi->root.type = bfd_link_hash_indirect;
4989 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4990 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4991 sym_hash = elf_sym_hashes (abfd);
4992 if (sym_hash)
4993 for (symidx = 0; symidx < extsymcount; ++symidx)
4994 if (sym_hash[symidx] == hi)
4995 {
4996 sym_hash[symidx] = h;
4997 break;
4998 }
4999 }
5000 free (shortname);
5001 }
5002 free (nondeflt_vers);
5003 nondeflt_vers = NULL;
5004 }
5005
5006 /* Now set the weakdefs field correctly for all the weak defined
5007 symbols we found. The only way to do this is to search all the
5008 symbols. Since we only need the information for non functions in
5009 dynamic objects, that's the only time we actually put anything on
5010 the list WEAKS. We need this information so that if a regular
5011 object refers to a symbol defined weakly in a dynamic object, the
5012 real symbol in the dynamic object is also put in the dynamic
5013 symbols; we also must arrange for both symbols to point to the
5014 same memory location. We could handle the general case of symbol
5015 aliasing, but a general symbol alias can only be generated in
5016 assembler code, handling it correctly would be very time
5017 consuming, and other ELF linkers don't handle general aliasing
5018 either. */
5019 if (weaks != NULL)
5020 {
5021 struct elf_link_hash_entry **hpp;
5022 struct elf_link_hash_entry **hppend;
5023 struct elf_link_hash_entry **sorted_sym_hash;
5024 struct elf_link_hash_entry *h;
5025 size_t sym_count;
5026
5027 /* Since we have to search the whole symbol list for each weak
5028 defined symbol, search time for N weak defined symbols will be
5029 O(N^2). Binary search will cut it down to O(NlogN). */
5030 amt = extsymcount;
5031 amt *= sizeof (struct elf_link_hash_entry *);
5032 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
5033 if (sorted_sym_hash == NULL)
5034 goto error_return;
5035 sym_hash = sorted_sym_hash;
5036 hpp = elf_sym_hashes (abfd);
5037 hppend = hpp + extsymcount;
5038 sym_count = 0;
5039 for (; hpp < hppend; hpp++)
5040 {
5041 h = *hpp;
5042 if (h != NULL
5043 && h->root.type == bfd_link_hash_defined
5044 && !bed->is_function_type (h->type))
5045 {
5046 *sym_hash = h;
5047 sym_hash++;
5048 sym_count++;
5049 }
5050 }
5051
5052 qsort (sorted_sym_hash, sym_count,
5053 sizeof (struct elf_link_hash_entry *),
5054 elf_sort_symbol);
5055
5056 while (weaks != NULL)
5057 {
5058 struct elf_link_hash_entry *hlook;
5059 asection *slook;
5060 bfd_vma vlook;
5061 size_t i, j, idx = 0;
5062
5063 hlook = weaks;
5064 weaks = hlook->u.weakdef;
5065 hlook->u.weakdef = NULL;
5066
5067 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
5068 || hlook->root.type == bfd_link_hash_defweak
5069 || hlook->root.type == bfd_link_hash_common
5070 || hlook->root.type == bfd_link_hash_indirect);
5071 slook = hlook->root.u.def.section;
5072 vlook = hlook->root.u.def.value;
5073
5074 i = 0;
5075 j = sym_count;
5076 while (i != j)
5077 {
5078 bfd_signed_vma vdiff;
5079 idx = (i + j) / 2;
5080 h = sorted_sym_hash[idx];
5081 vdiff = vlook - h->root.u.def.value;
5082 if (vdiff < 0)
5083 j = idx;
5084 else if (vdiff > 0)
5085 i = idx + 1;
5086 else
5087 {
5088 int sdiff = slook->id - h->root.u.def.section->id;
5089 if (sdiff < 0)
5090 j = idx;
5091 else if (sdiff > 0)
5092 i = idx + 1;
5093 else
5094 break;
5095 }
5096 }
5097
5098 /* We didn't find a value/section match. */
5099 if (i == j)
5100 continue;
5101
5102 /* With multiple aliases, or when the weak symbol is already
5103 strongly defined, we have multiple matching symbols and
5104 the binary search above may land on any of them. Step
5105 one past the matching symbol(s). */
5106 while (++idx != j)
5107 {
5108 h = sorted_sym_hash[idx];
5109 if (h->root.u.def.section != slook
5110 || h->root.u.def.value != vlook)
5111 break;
5112 }
5113
5114 /* Now look back over the aliases. Since we sorted by size
5115 as well as value and section, we'll choose the one with
5116 the largest size. */
5117 while (idx-- != i)
5118 {
5119 h = sorted_sym_hash[idx];
5120
5121 /* Stop if value or section doesn't match. */
5122 if (h->root.u.def.section != slook
5123 || h->root.u.def.value != vlook)
5124 break;
5125 else if (h != hlook)
5126 {
5127 hlook->u.weakdef = h;
5128
5129 /* If the weak definition is in the list of dynamic
5130 symbols, make sure the real definition is put
5131 there as well. */
5132 if (hlook->dynindx != -1 && h->dynindx == -1)
5133 {
5134 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5135 {
5136 err_free_sym_hash:
5137 free (sorted_sym_hash);
5138 goto error_return;
5139 }
5140 }
5141
5142 /* If the real definition is in the list of dynamic
5143 symbols, make sure the weak definition is put
5144 there as well. If we don't do this, then the
5145 dynamic loader might not merge the entries for the
5146 real definition and the weak definition. */
5147 if (h->dynindx != -1 && hlook->dynindx == -1)
5148 {
5149 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5150 goto err_free_sym_hash;
5151 }
5152 break;
5153 }
5154 }
5155 }
5156
5157 free (sorted_sym_hash);
5158 }
5159
5160 if (bed->check_directives
5161 && !(*bed->check_directives) (abfd, info))
5162 return FALSE;
5163
5164 if (!info->check_relocs_after_open_input
5165 && !_bfd_elf_link_check_relocs (abfd, info))
5166 return FALSE;
5167
5168 /* If this is a non-traditional link, try to optimize the handling
5169 of the .stab/.stabstr sections. */
5170 if (! dynamic
5171 && ! info->traditional_format
5172 && is_elf_hash_table (htab)
5173 && (info->strip != strip_all && info->strip != strip_debugger))
5174 {
5175 asection *stabstr;
5176
5177 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5178 if (stabstr != NULL)
5179 {
5180 bfd_size_type string_offset = 0;
5181 asection *stab;
5182
5183 for (stab = abfd->sections; stab; stab = stab->next)
5184 if (CONST_STRNEQ (stab->name, ".stab")
5185 && (!stab->name[5] ||
5186 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5187 && (stab->flags & SEC_MERGE) == 0
5188 && !bfd_is_abs_section (stab->output_section))
5189 {
5190 struct bfd_elf_section_data *secdata;
5191
5192 secdata = elf_section_data (stab);
5193 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5194 stabstr, &secdata->sec_info,
5195 &string_offset))
5196 goto error_return;
5197 if (secdata->sec_info)
5198 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5199 }
5200 }
5201 }
5202
5203 if (is_elf_hash_table (htab) && add_needed)
5204 {
5205 /* Add this bfd to the loaded list. */
5206 struct elf_link_loaded_list *n;
5207
5208 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5209 if (n == NULL)
5210 goto error_return;
5211 n->abfd = abfd;
5212 n->next = htab->loaded;
5213 htab->loaded = n;
5214 }
5215
5216 return TRUE;
5217
5218 error_free_vers:
5219 if (old_tab != NULL)
5220 free (old_tab);
5221 if (old_strtab != NULL)
5222 free (old_strtab);
5223 if (nondeflt_vers != NULL)
5224 free (nondeflt_vers);
5225 if (extversym != NULL)
5226 free (extversym);
5227 error_free_sym:
5228 if (isymbuf != NULL)
5229 free (isymbuf);
5230 error_return:
5231 return FALSE;
5232 }
5233
5234 /* Return the linker hash table entry of a symbol that might be
5235 satisfied by an archive symbol. Return -1 on error. */
5236
5237 struct elf_link_hash_entry *
5238 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5239 struct bfd_link_info *info,
5240 const char *name)
5241 {
5242 struct elf_link_hash_entry *h;
5243 char *p, *copy;
5244 size_t len, first;
5245
5246 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5247 if (h != NULL)
5248 return h;
5249
5250 /* If this is a default version (the name contains @@), look up the
5251 symbol again with only one `@' as well as without the version.
5252 The effect is that references to the symbol with and without the
5253 version will be matched by the default symbol in the archive. */
5254
5255 p = strchr (name, ELF_VER_CHR);
5256 if (p == NULL || p[1] != ELF_VER_CHR)
5257 return h;
5258
5259 /* First check with only one `@'. */
5260 len = strlen (name);
5261 copy = (char *) bfd_alloc (abfd, len);
5262 if (copy == NULL)
5263 return (struct elf_link_hash_entry *) 0 - 1;
5264
5265 first = p - name + 1;
5266 memcpy (copy, name, first);
5267 memcpy (copy + first, name + first + 1, len - first);
5268
5269 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5270 if (h == NULL)
5271 {
5272 /* We also need to check references to the symbol without the
5273 version. */
5274 copy[first - 1] = '\0';
5275 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5276 FALSE, FALSE, TRUE);
5277 }
5278
5279 bfd_release (abfd, copy);
5280 return h;
5281 }
5282
5283 /* Add symbols from an ELF archive file to the linker hash table. We
5284 don't use _bfd_generic_link_add_archive_symbols because we need to
5285 handle versioned symbols.
5286
5287 Fortunately, ELF archive handling is simpler than that done by
5288 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5289 oddities. In ELF, if we find a symbol in the archive map, and the
5290 symbol is currently undefined, we know that we must pull in that
5291 object file.
5292
5293 Unfortunately, we do have to make multiple passes over the symbol
5294 table until nothing further is resolved. */
5295
5296 static bfd_boolean
5297 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5298 {
5299 symindex c;
5300 unsigned char *included = NULL;
5301 carsym *symdefs;
5302 bfd_boolean loop;
5303 bfd_size_type amt;
5304 const struct elf_backend_data *bed;
5305 struct elf_link_hash_entry * (*archive_symbol_lookup)
5306 (bfd *, struct bfd_link_info *, const char *);
5307
5308 if (! bfd_has_map (abfd))
5309 {
5310 /* An empty archive is a special case. */
5311 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5312 return TRUE;
5313 bfd_set_error (bfd_error_no_armap);
5314 return FALSE;
5315 }
5316
5317 /* Keep track of all symbols we know to be already defined, and all
5318 files we know to be already included. This is to speed up the
5319 second and subsequent passes. */
5320 c = bfd_ardata (abfd)->symdef_count;
5321 if (c == 0)
5322 return TRUE;
5323 amt = c;
5324 amt *= sizeof (*included);
5325 included = (unsigned char *) bfd_zmalloc (amt);
5326 if (included == NULL)
5327 return FALSE;
5328
5329 symdefs = bfd_ardata (abfd)->symdefs;
5330 bed = get_elf_backend_data (abfd);
5331 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5332
5333 do
5334 {
5335 file_ptr last;
5336 symindex i;
5337 carsym *symdef;
5338 carsym *symdefend;
5339
5340 loop = FALSE;
5341 last = -1;
5342
5343 symdef = symdefs;
5344 symdefend = symdef + c;
5345 for (i = 0; symdef < symdefend; symdef++, i++)
5346 {
5347 struct elf_link_hash_entry *h;
5348 bfd *element;
5349 struct bfd_link_hash_entry *undefs_tail;
5350 symindex mark;
5351
5352 if (included[i])
5353 continue;
5354 if (symdef->file_offset == last)
5355 {
5356 included[i] = TRUE;
5357 continue;
5358 }
5359
5360 h = archive_symbol_lookup (abfd, info, symdef->name);
5361 if (h == (struct elf_link_hash_entry *) 0 - 1)
5362 goto error_return;
5363
5364 if (h == NULL)
5365 continue;
5366
5367 if (h->root.type == bfd_link_hash_common)
5368 {
5369 /* We currently have a common symbol. The archive map contains
5370 a reference to this symbol, so we may want to include it. We
5371 only want to include it however, if this archive element
5372 contains a definition of the symbol, not just another common
5373 declaration of it.
5374
5375 Unfortunately some archivers (including GNU ar) will put
5376 declarations of common symbols into their archive maps, as
5377 well as real definitions, so we cannot just go by the archive
5378 map alone. Instead we must read in the element's symbol
5379 table and check that to see what kind of symbol definition
5380 this is. */
5381 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5382 continue;
5383 }
5384 else if (h->root.type != bfd_link_hash_undefined)
5385 {
5386 if (h->root.type != bfd_link_hash_undefweak)
5387 /* Symbol must be defined. Don't check it again. */
5388 included[i] = TRUE;
5389 continue;
5390 }
5391
5392 /* We need to include this archive member. */
5393 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5394 if (element == NULL)
5395 goto error_return;
5396
5397 if (! bfd_check_format (element, bfd_object))
5398 goto error_return;
5399
5400 undefs_tail = info->hash->undefs_tail;
5401
5402 if (!(*info->callbacks
5403 ->add_archive_element) (info, element, symdef->name, &element))
5404 continue;
5405 if (!bfd_link_add_symbols (element, info))
5406 goto error_return;
5407
5408 /* If there are any new undefined symbols, we need to make
5409 another pass through the archive in order to see whether
5410 they can be defined. FIXME: This isn't perfect, because
5411 common symbols wind up on undefs_tail and because an
5412 undefined symbol which is defined later on in this pass
5413 does not require another pass. This isn't a bug, but it
5414 does make the code less efficient than it could be. */
5415 if (undefs_tail != info->hash->undefs_tail)
5416 loop = TRUE;
5417
5418 /* Look backward to mark all symbols from this object file
5419 which we have already seen in this pass. */
5420 mark = i;
5421 do
5422 {
5423 included[mark] = TRUE;
5424 if (mark == 0)
5425 break;
5426 --mark;
5427 }
5428 while (symdefs[mark].file_offset == symdef->file_offset);
5429
5430 /* We mark subsequent symbols from this object file as we go
5431 on through the loop. */
5432 last = symdef->file_offset;
5433 }
5434 }
5435 while (loop);
5436
5437 free (included);
5438
5439 return TRUE;
5440
5441 error_return:
5442 if (included != NULL)
5443 free (included);
5444 return FALSE;
5445 }
5446
5447 /* Given an ELF BFD, add symbols to the global hash table as
5448 appropriate. */
5449
5450 bfd_boolean
5451 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5452 {
5453 switch (bfd_get_format (abfd))
5454 {
5455 case bfd_object:
5456 return elf_link_add_object_symbols (abfd, info);
5457 case bfd_archive:
5458 return elf_link_add_archive_symbols (abfd, info);
5459 default:
5460 bfd_set_error (bfd_error_wrong_format);
5461 return FALSE;
5462 }
5463 }
5464 \f
5465 struct hash_codes_info
5466 {
5467 unsigned long *hashcodes;
5468 bfd_boolean error;
5469 };
5470
5471 /* This function will be called though elf_link_hash_traverse to store
5472 all hash value of the exported symbols in an array. */
5473
5474 static bfd_boolean
5475 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5476 {
5477 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5478 const char *name;
5479 unsigned long ha;
5480 char *alc = NULL;
5481
5482 /* Ignore indirect symbols. These are added by the versioning code. */
5483 if (h->dynindx == -1)
5484 return TRUE;
5485
5486 name = h->root.root.string;
5487 if (h->versioned >= versioned)
5488 {
5489 char *p = strchr (name, ELF_VER_CHR);
5490 if (p != NULL)
5491 {
5492 alc = (char *) bfd_malloc (p - name + 1);
5493 if (alc == NULL)
5494 {
5495 inf->error = TRUE;
5496 return FALSE;
5497 }
5498 memcpy (alc, name, p - name);
5499 alc[p - name] = '\0';
5500 name = alc;
5501 }
5502 }
5503
5504 /* Compute the hash value. */
5505 ha = bfd_elf_hash (name);
5506
5507 /* Store the found hash value in the array given as the argument. */
5508 *(inf->hashcodes)++ = ha;
5509
5510 /* And store it in the struct so that we can put it in the hash table
5511 later. */
5512 h->u.elf_hash_value = ha;
5513
5514 if (alc != NULL)
5515 free (alc);
5516
5517 return TRUE;
5518 }
5519
5520 struct collect_gnu_hash_codes
5521 {
5522 bfd *output_bfd;
5523 const struct elf_backend_data *bed;
5524 unsigned long int nsyms;
5525 unsigned long int maskbits;
5526 unsigned long int *hashcodes;
5527 unsigned long int *hashval;
5528 unsigned long int *indx;
5529 unsigned long int *counts;
5530 bfd_vma *bitmask;
5531 bfd_byte *contents;
5532 long int min_dynindx;
5533 unsigned long int bucketcount;
5534 unsigned long int symindx;
5535 long int local_indx;
5536 long int shift1, shift2;
5537 unsigned long int mask;
5538 bfd_boolean error;
5539 };
5540
5541 /* This function will be called though elf_link_hash_traverse to store
5542 all hash value of the exported symbols in an array. */
5543
5544 static bfd_boolean
5545 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5546 {
5547 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5548 const char *name;
5549 unsigned long ha;
5550 char *alc = NULL;
5551
5552 /* Ignore indirect symbols. These are added by the versioning code. */
5553 if (h->dynindx == -1)
5554 return TRUE;
5555
5556 /* Ignore also local symbols and undefined symbols. */
5557 if (! (*s->bed->elf_hash_symbol) (h))
5558 return TRUE;
5559
5560 name = h->root.root.string;
5561 if (h->versioned >= versioned)
5562 {
5563 char *p = strchr (name, ELF_VER_CHR);
5564 if (p != NULL)
5565 {
5566 alc = (char *) bfd_malloc (p - name + 1);
5567 if (alc == NULL)
5568 {
5569 s->error = TRUE;
5570 return FALSE;
5571 }
5572 memcpy (alc, name, p - name);
5573 alc[p - name] = '\0';
5574 name = alc;
5575 }
5576 }
5577
5578 /* Compute the hash value. */
5579 ha = bfd_elf_gnu_hash (name);
5580
5581 /* Store the found hash value in the array for compute_bucket_count,
5582 and also for .dynsym reordering purposes. */
5583 s->hashcodes[s->nsyms] = ha;
5584 s->hashval[h->dynindx] = ha;
5585 ++s->nsyms;
5586 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5587 s->min_dynindx = h->dynindx;
5588
5589 if (alc != NULL)
5590 free (alc);
5591
5592 return TRUE;
5593 }
5594
5595 /* This function will be called though elf_link_hash_traverse to do
5596 final dynaminc symbol renumbering. */
5597
5598 static bfd_boolean
5599 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5600 {
5601 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5602 unsigned long int bucket;
5603 unsigned long int val;
5604
5605 /* Ignore indirect symbols. */
5606 if (h->dynindx == -1)
5607 return TRUE;
5608
5609 /* Ignore also local symbols and undefined symbols. */
5610 if (! (*s->bed->elf_hash_symbol) (h))
5611 {
5612 if (h->dynindx >= s->min_dynindx)
5613 h->dynindx = s->local_indx++;
5614 return TRUE;
5615 }
5616
5617 bucket = s->hashval[h->dynindx] % s->bucketcount;
5618 val = (s->hashval[h->dynindx] >> s->shift1)
5619 & ((s->maskbits >> s->shift1) - 1);
5620 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5621 s->bitmask[val]
5622 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5623 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5624 if (s->counts[bucket] == 1)
5625 /* Last element terminates the chain. */
5626 val |= 1;
5627 bfd_put_32 (s->output_bfd, val,
5628 s->contents + (s->indx[bucket] - s->symindx) * 4);
5629 --s->counts[bucket];
5630 h->dynindx = s->indx[bucket]++;
5631 return TRUE;
5632 }
5633
5634 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5635
5636 bfd_boolean
5637 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5638 {
5639 return !(h->forced_local
5640 || h->root.type == bfd_link_hash_undefined
5641 || h->root.type == bfd_link_hash_undefweak
5642 || ((h->root.type == bfd_link_hash_defined
5643 || h->root.type == bfd_link_hash_defweak)
5644 && h->root.u.def.section->output_section == NULL));
5645 }
5646
5647 /* Array used to determine the number of hash table buckets to use
5648 based on the number of symbols there are. If there are fewer than
5649 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5650 fewer than 37 we use 17 buckets, and so forth. We never use more
5651 than 32771 buckets. */
5652
5653 static const size_t elf_buckets[] =
5654 {
5655 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5656 16411, 32771, 0
5657 };
5658
5659 /* Compute bucket count for hashing table. We do not use a static set
5660 of possible tables sizes anymore. Instead we determine for all
5661 possible reasonable sizes of the table the outcome (i.e., the
5662 number of collisions etc) and choose the best solution. The
5663 weighting functions are not too simple to allow the table to grow
5664 without bounds. Instead one of the weighting factors is the size.
5665 Therefore the result is always a good payoff between few collisions
5666 (= short chain lengths) and table size. */
5667 static size_t
5668 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5669 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5670 unsigned long int nsyms,
5671 int gnu_hash)
5672 {
5673 size_t best_size = 0;
5674 unsigned long int i;
5675
5676 /* We have a problem here. The following code to optimize the table
5677 size requires an integer type with more the 32 bits. If
5678 BFD_HOST_U_64_BIT is set we know about such a type. */
5679 #ifdef BFD_HOST_U_64_BIT
5680 if (info->optimize)
5681 {
5682 size_t minsize;
5683 size_t maxsize;
5684 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5685 bfd *dynobj = elf_hash_table (info)->dynobj;
5686 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5687 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5688 unsigned long int *counts;
5689 bfd_size_type amt;
5690 unsigned int no_improvement_count = 0;
5691
5692 /* Possible optimization parameters: if we have NSYMS symbols we say
5693 that the hashing table must at least have NSYMS/4 and at most
5694 2*NSYMS buckets. */
5695 minsize = nsyms / 4;
5696 if (minsize == 0)
5697 minsize = 1;
5698 best_size = maxsize = nsyms * 2;
5699 if (gnu_hash)
5700 {
5701 if (minsize < 2)
5702 minsize = 2;
5703 if ((best_size & 31) == 0)
5704 ++best_size;
5705 }
5706
5707 /* Create array where we count the collisions in. We must use bfd_malloc
5708 since the size could be large. */
5709 amt = maxsize;
5710 amt *= sizeof (unsigned long int);
5711 counts = (unsigned long int *) bfd_malloc (amt);
5712 if (counts == NULL)
5713 return 0;
5714
5715 /* Compute the "optimal" size for the hash table. The criteria is a
5716 minimal chain length. The minor criteria is (of course) the size
5717 of the table. */
5718 for (i = minsize; i < maxsize; ++i)
5719 {
5720 /* Walk through the array of hashcodes and count the collisions. */
5721 BFD_HOST_U_64_BIT max;
5722 unsigned long int j;
5723 unsigned long int fact;
5724
5725 if (gnu_hash && (i & 31) == 0)
5726 continue;
5727
5728 memset (counts, '\0', i * sizeof (unsigned long int));
5729
5730 /* Determine how often each hash bucket is used. */
5731 for (j = 0; j < nsyms; ++j)
5732 ++counts[hashcodes[j] % i];
5733
5734 /* For the weight function we need some information about the
5735 pagesize on the target. This is information need not be 100%
5736 accurate. Since this information is not available (so far) we
5737 define it here to a reasonable default value. If it is crucial
5738 to have a better value some day simply define this value. */
5739 # ifndef BFD_TARGET_PAGESIZE
5740 # define BFD_TARGET_PAGESIZE (4096)
5741 # endif
5742
5743 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5744 and the chains. */
5745 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5746
5747 # if 1
5748 /* Variant 1: optimize for short chains. We add the squares
5749 of all the chain lengths (which favors many small chain
5750 over a few long chains). */
5751 for (j = 0; j < i; ++j)
5752 max += counts[j] * counts[j];
5753
5754 /* This adds penalties for the overall size of the table. */
5755 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5756 max *= fact * fact;
5757 # else
5758 /* Variant 2: Optimize a lot more for small table. Here we
5759 also add squares of the size but we also add penalties for
5760 empty slots (the +1 term). */
5761 for (j = 0; j < i; ++j)
5762 max += (1 + counts[j]) * (1 + counts[j]);
5763
5764 /* The overall size of the table is considered, but not as
5765 strong as in variant 1, where it is squared. */
5766 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5767 max *= fact;
5768 # endif
5769
5770 /* Compare with current best results. */
5771 if (max < best_chlen)
5772 {
5773 best_chlen = max;
5774 best_size = i;
5775 no_improvement_count = 0;
5776 }
5777 /* PR 11843: Avoid futile long searches for the best bucket size
5778 when there are a large number of symbols. */
5779 else if (++no_improvement_count == 100)
5780 break;
5781 }
5782
5783 free (counts);
5784 }
5785 else
5786 #endif /* defined (BFD_HOST_U_64_BIT) */
5787 {
5788 /* This is the fallback solution if no 64bit type is available or if we
5789 are not supposed to spend much time on optimizations. We select the
5790 bucket count using a fixed set of numbers. */
5791 for (i = 0; elf_buckets[i] != 0; i++)
5792 {
5793 best_size = elf_buckets[i];
5794 if (nsyms < elf_buckets[i + 1])
5795 break;
5796 }
5797 if (gnu_hash && best_size < 2)
5798 best_size = 2;
5799 }
5800
5801 return best_size;
5802 }
5803
5804 /* Size any SHT_GROUP section for ld -r. */
5805
5806 bfd_boolean
5807 _bfd_elf_size_group_sections (struct bfd_link_info *info)
5808 {
5809 bfd *ibfd;
5810
5811 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
5812 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5813 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5814 return FALSE;
5815 return TRUE;
5816 }
5817
5818 /* Set a default stack segment size. The value in INFO wins. If it
5819 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5820 undefined it is initialized. */
5821
5822 bfd_boolean
5823 bfd_elf_stack_segment_size (bfd *output_bfd,
5824 struct bfd_link_info *info,
5825 const char *legacy_symbol,
5826 bfd_vma default_size)
5827 {
5828 struct elf_link_hash_entry *h = NULL;
5829
5830 /* Look for legacy symbol. */
5831 if (legacy_symbol)
5832 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5833 FALSE, FALSE, FALSE);
5834 if (h && (h->root.type == bfd_link_hash_defined
5835 || h->root.type == bfd_link_hash_defweak)
5836 && h->def_regular
5837 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5838 {
5839 /* The symbol has no type if specified on the command line. */
5840 h->type = STT_OBJECT;
5841 if (info->stacksize)
5842 /* xgettext:c-format */
5843 _bfd_error_handler (_("%B: stack size specified and %s set"),
5844 output_bfd, legacy_symbol);
5845 else if (h->root.u.def.section != bfd_abs_section_ptr)
5846 /* xgettext:c-format */
5847 _bfd_error_handler (_("%B: %s not absolute"),
5848 output_bfd, legacy_symbol);
5849 else
5850 info->stacksize = h->root.u.def.value;
5851 }
5852
5853 if (!info->stacksize)
5854 /* If the user didn't set a size, or explicitly inhibit the
5855 size, set it now. */
5856 info->stacksize = default_size;
5857
5858 /* Provide the legacy symbol, if it is referenced. */
5859 if (h && (h->root.type == bfd_link_hash_undefined
5860 || h->root.type == bfd_link_hash_undefweak))
5861 {
5862 struct bfd_link_hash_entry *bh = NULL;
5863
5864 if (!(_bfd_generic_link_add_one_symbol
5865 (info, output_bfd, legacy_symbol,
5866 BSF_GLOBAL, bfd_abs_section_ptr,
5867 info->stacksize >= 0 ? info->stacksize : 0,
5868 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5869 return FALSE;
5870
5871 h = (struct elf_link_hash_entry *) bh;
5872 h->def_regular = 1;
5873 h->type = STT_OBJECT;
5874 }
5875
5876 return TRUE;
5877 }
5878
5879 /* Set up the sizes and contents of the ELF dynamic sections. This is
5880 called by the ELF linker emulation before_allocation routine. We
5881 must set the sizes of the sections before the linker sets the
5882 addresses of the various sections. */
5883
5884 bfd_boolean
5885 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5886 const char *soname,
5887 const char *rpath,
5888 const char *filter_shlib,
5889 const char *audit,
5890 const char *depaudit,
5891 const char * const *auxiliary_filters,
5892 struct bfd_link_info *info,
5893 asection **sinterpptr)
5894 {
5895 size_t soname_indx;
5896 bfd *dynobj;
5897 const struct elf_backend_data *bed;
5898 struct elf_info_failed asvinfo;
5899
5900 *sinterpptr = NULL;
5901
5902 soname_indx = (size_t) -1;
5903
5904 if (!is_elf_hash_table (info->hash))
5905 return TRUE;
5906
5907 bed = get_elf_backend_data (output_bfd);
5908
5909 /* Any syms created from now on start with -1 in
5910 got.refcount/offset and plt.refcount/offset. */
5911 elf_hash_table (info)->init_got_refcount
5912 = elf_hash_table (info)->init_got_offset;
5913 elf_hash_table (info)->init_plt_refcount
5914 = elf_hash_table (info)->init_plt_offset;
5915
5916 if (bfd_link_relocatable (info)
5917 && !_bfd_elf_size_group_sections (info))
5918 return FALSE;
5919
5920 /* The backend may have to create some sections regardless of whether
5921 we're dynamic or not. */
5922 if (bed->elf_backend_always_size_sections
5923 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5924 return FALSE;
5925
5926 /* Determine any GNU_STACK segment requirements, after the backend
5927 has had a chance to set a default segment size. */
5928 if (info->execstack)
5929 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
5930 else if (info->noexecstack)
5931 elf_stack_flags (output_bfd) = PF_R | PF_W;
5932 else
5933 {
5934 bfd *inputobj;
5935 asection *notesec = NULL;
5936 int exec = 0;
5937
5938 for (inputobj = info->input_bfds;
5939 inputobj;
5940 inputobj = inputobj->link.next)
5941 {
5942 asection *s;
5943
5944 if (inputobj->flags
5945 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5946 continue;
5947 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5948 if (s)
5949 {
5950 if (s->flags & SEC_CODE)
5951 exec = PF_X;
5952 notesec = s;
5953 }
5954 else if (bed->default_execstack)
5955 exec = PF_X;
5956 }
5957 if (notesec || info->stacksize > 0)
5958 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
5959 if (notesec && exec && bfd_link_relocatable (info)
5960 && notesec->output_section != bfd_abs_section_ptr)
5961 notesec->output_section->flags |= SEC_CODE;
5962 }
5963
5964 dynobj = elf_hash_table (info)->dynobj;
5965
5966 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5967 {
5968 struct elf_info_failed eif;
5969 struct elf_link_hash_entry *h;
5970 asection *dynstr;
5971 struct bfd_elf_version_tree *t;
5972 struct bfd_elf_version_expr *d;
5973 asection *s;
5974 bfd_boolean all_defined;
5975
5976 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
5977 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
5978
5979 if (soname != NULL)
5980 {
5981 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5982 soname, TRUE);
5983 if (soname_indx == (size_t) -1
5984 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5985 return FALSE;
5986 }
5987
5988 if (info->symbolic)
5989 {
5990 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5991 return FALSE;
5992 info->flags |= DF_SYMBOLIC;
5993 }
5994
5995 if (rpath != NULL)
5996 {
5997 size_t indx;
5998 bfd_vma tag;
5999
6000 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6001 TRUE);
6002 if (indx == (size_t) -1)
6003 return FALSE;
6004
6005 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6006 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6007 return FALSE;
6008 }
6009
6010 if (filter_shlib != NULL)
6011 {
6012 size_t indx;
6013
6014 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6015 filter_shlib, TRUE);
6016 if (indx == (size_t) -1
6017 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6018 return FALSE;
6019 }
6020
6021 if (auxiliary_filters != NULL)
6022 {
6023 const char * const *p;
6024
6025 for (p = auxiliary_filters; *p != NULL; p++)
6026 {
6027 size_t indx;
6028
6029 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6030 *p, TRUE);
6031 if (indx == (size_t) -1
6032 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6033 return FALSE;
6034 }
6035 }
6036
6037 if (audit != NULL)
6038 {
6039 size_t indx;
6040
6041 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6042 TRUE);
6043 if (indx == (size_t) -1
6044 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6045 return FALSE;
6046 }
6047
6048 if (depaudit != NULL)
6049 {
6050 size_t indx;
6051
6052 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6053 TRUE);
6054 if (indx == (size_t) -1
6055 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6056 return FALSE;
6057 }
6058
6059 eif.info = info;
6060 eif.failed = FALSE;
6061
6062 /* If we are supposed to export all symbols into the dynamic symbol
6063 table (this is not the normal case), then do so. */
6064 if (info->export_dynamic
6065 || (bfd_link_executable (info) && info->dynamic))
6066 {
6067 elf_link_hash_traverse (elf_hash_table (info),
6068 _bfd_elf_export_symbol,
6069 &eif);
6070 if (eif.failed)
6071 return FALSE;
6072 }
6073
6074 /* Make all global versions with definition. */
6075 for (t = info->version_info; t != NULL; t = t->next)
6076 for (d = t->globals.list; d != NULL; d = d->next)
6077 if (!d->symver && d->literal)
6078 {
6079 const char *verstr, *name;
6080 size_t namelen, verlen, newlen;
6081 char *newname, *p, leading_char;
6082 struct elf_link_hash_entry *newh;
6083
6084 leading_char = bfd_get_symbol_leading_char (output_bfd);
6085 name = d->pattern;
6086 namelen = strlen (name) + (leading_char != '\0');
6087 verstr = t->name;
6088 verlen = strlen (verstr);
6089 newlen = namelen + verlen + 3;
6090
6091 newname = (char *) bfd_malloc (newlen);
6092 if (newname == NULL)
6093 return FALSE;
6094 newname[0] = leading_char;
6095 memcpy (newname + (leading_char != '\0'), name, namelen);
6096
6097 /* Check the hidden versioned definition. */
6098 p = newname + namelen;
6099 *p++ = ELF_VER_CHR;
6100 memcpy (p, verstr, verlen + 1);
6101 newh = elf_link_hash_lookup (elf_hash_table (info),
6102 newname, FALSE, FALSE,
6103 FALSE);
6104 if (newh == NULL
6105 || (newh->root.type != bfd_link_hash_defined
6106 && newh->root.type != bfd_link_hash_defweak))
6107 {
6108 /* Check the default versioned definition. */
6109 *p++ = ELF_VER_CHR;
6110 memcpy (p, verstr, verlen + 1);
6111 newh = elf_link_hash_lookup (elf_hash_table (info),
6112 newname, FALSE, FALSE,
6113 FALSE);
6114 }
6115 free (newname);
6116
6117 /* Mark this version if there is a definition and it is
6118 not defined in a shared object. */
6119 if (newh != NULL
6120 && !newh->def_dynamic
6121 && (newh->root.type == bfd_link_hash_defined
6122 || newh->root.type == bfd_link_hash_defweak))
6123 d->symver = 1;
6124 }
6125
6126 /* Attach all the symbols to their version information. */
6127 asvinfo.info = info;
6128 asvinfo.failed = FALSE;
6129
6130 elf_link_hash_traverse (elf_hash_table (info),
6131 _bfd_elf_link_assign_sym_version,
6132 &asvinfo);
6133 if (asvinfo.failed)
6134 return FALSE;
6135
6136 if (!info->allow_undefined_version)
6137 {
6138 /* Check if all global versions have a definition. */
6139 all_defined = TRUE;
6140 for (t = info->version_info; t != NULL; t = t->next)
6141 for (d = t->globals.list; d != NULL; d = d->next)
6142 if (d->literal && !d->symver && !d->script)
6143 {
6144 _bfd_error_handler
6145 (_("%s: undefined version: %s"),
6146 d->pattern, t->name);
6147 all_defined = FALSE;
6148 }
6149
6150 if (!all_defined)
6151 {
6152 bfd_set_error (bfd_error_bad_value);
6153 return FALSE;
6154 }
6155 }
6156
6157 /* Find all symbols which were defined in a dynamic object and make
6158 the backend pick a reasonable value for them. */
6159 elf_link_hash_traverse (elf_hash_table (info),
6160 _bfd_elf_adjust_dynamic_symbol,
6161 &eif);
6162 if (eif.failed)
6163 return FALSE;
6164
6165 /* Add some entries to the .dynamic section. We fill in some of the
6166 values later, in bfd_elf_final_link, but we must add the entries
6167 now so that we know the final size of the .dynamic section. */
6168
6169 /* If there are initialization and/or finalization functions to
6170 call then add the corresponding DT_INIT/DT_FINI entries. */
6171 h = (info->init_function
6172 ? elf_link_hash_lookup (elf_hash_table (info),
6173 info->init_function, FALSE,
6174 FALSE, FALSE)
6175 : NULL);
6176 if (h != NULL
6177 && (h->ref_regular
6178 || h->def_regular))
6179 {
6180 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6181 return FALSE;
6182 }
6183 h = (info->fini_function
6184 ? elf_link_hash_lookup (elf_hash_table (info),
6185 info->fini_function, FALSE,
6186 FALSE, FALSE)
6187 : NULL);
6188 if (h != NULL
6189 && (h->ref_regular
6190 || h->def_regular))
6191 {
6192 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6193 return FALSE;
6194 }
6195
6196 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6197 if (s != NULL && s->linker_has_input)
6198 {
6199 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6200 if (! bfd_link_executable (info))
6201 {
6202 bfd *sub;
6203 asection *o;
6204
6205 for (sub = info->input_bfds; sub != NULL;
6206 sub = sub->link.next)
6207 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
6208 for (o = sub->sections; o != NULL; o = o->next)
6209 if (elf_section_data (o)->this_hdr.sh_type
6210 == SHT_PREINIT_ARRAY)
6211 {
6212 _bfd_error_handler
6213 (_("%B: .preinit_array section is not allowed in DSO"),
6214 sub);
6215 break;
6216 }
6217
6218 bfd_set_error (bfd_error_nonrepresentable_section);
6219 return FALSE;
6220 }
6221
6222 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6223 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6224 return FALSE;
6225 }
6226 s = bfd_get_section_by_name (output_bfd, ".init_array");
6227 if (s != NULL && s->linker_has_input)
6228 {
6229 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6230 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6231 return FALSE;
6232 }
6233 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6234 if (s != NULL && s->linker_has_input)
6235 {
6236 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6237 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6238 return FALSE;
6239 }
6240
6241 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6242 /* If .dynstr is excluded from the link, we don't want any of
6243 these tags. Strictly, we should be checking each section
6244 individually; This quick check covers for the case where
6245 someone does a /DISCARD/ : { *(*) }. */
6246 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6247 {
6248 bfd_size_type strsize;
6249
6250 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6251 if ((info->emit_hash
6252 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6253 || (info->emit_gnu_hash
6254 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6255 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6256 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6257 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6258 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6259 bed->s->sizeof_sym))
6260 return FALSE;
6261 }
6262 }
6263
6264 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6265 return FALSE;
6266
6267 /* The backend must work out the sizes of all the other dynamic
6268 sections. */
6269 if (dynobj != NULL
6270 && bed->elf_backend_size_dynamic_sections != NULL
6271 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6272 return FALSE;
6273
6274 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6275 {
6276 unsigned long section_sym_count;
6277 struct bfd_elf_version_tree *verdefs;
6278 asection *s;
6279
6280 /* Set up the version definition section. */
6281 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6282 BFD_ASSERT (s != NULL);
6283
6284 /* We may have created additional version definitions if we are
6285 just linking a regular application. */
6286 verdefs = info->version_info;
6287
6288 /* Skip anonymous version tag. */
6289 if (verdefs != NULL && verdefs->vernum == 0)
6290 verdefs = verdefs->next;
6291
6292 if (verdefs == NULL && !info->create_default_symver)
6293 s->flags |= SEC_EXCLUDE;
6294 else
6295 {
6296 unsigned int cdefs;
6297 bfd_size_type size;
6298 struct bfd_elf_version_tree *t;
6299 bfd_byte *p;
6300 Elf_Internal_Verdef def;
6301 Elf_Internal_Verdaux defaux;
6302 struct bfd_link_hash_entry *bh;
6303 struct elf_link_hash_entry *h;
6304 const char *name;
6305
6306 cdefs = 0;
6307 size = 0;
6308
6309 /* Make space for the base version. */
6310 size += sizeof (Elf_External_Verdef);
6311 size += sizeof (Elf_External_Verdaux);
6312 ++cdefs;
6313
6314 /* Make space for the default version. */
6315 if (info->create_default_symver)
6316 {
6317 size += sizeof (Elf_External_Verdef);
6318 ++cdefs;
6319 }
6320
6321 for (t = verdefs; t != NULL; t = t->next)
6322 {
6323 struct bfd_elf_version_deps *n;
6324
6325 /* Don't emit base version twice. */
6326 if (t->vernum == 0)
6327 continue;
6328
6329 size += sizeof (Elf_External_Verdef);
6330 size += sizeof (Elf_External_Verdaux);
6331 ++cdefs;
6332
6333 for (n = t->deps; n != NULL; n = n->next)
6334 size += sizeof (Elf_External_Verdaux);
6335 }
6336
6337 s->size = size;
6338 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6339 if (s->contents == NULL && s->size != 0)
6340 return FALSE;
6341
6342 /* Fill in the version definition section. */
6343
6344 p = s->contents;
6345
6346 def.vd_version = VER_DEF_CURRENT;
6347 def.vd_flags = VER_FLG_BASE;
6348 def.vd_ndx = 1;
6349 def.vd_cnt = 1;
6350 if (info->create_default_symver)
6351 {
6352 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6353 def.vd_next = sizeof (Elf_External_Verdef);
6354 }
6355 else
6356 {
6357 def.vd_aux = sizeof (Elf_External_Verdef);
6358 def.vd_next = (sizeof (Elf_External_Verdef)
6359 + sizeof (Elf_External_Verdaux));
6360 }
6361
6362 if (soname_indx != (size_t) -1)
6363 {
6364 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6365 soname_indx);
6366 def.vd_hash = bfd_elf_hash (soname);
6367 defaux.vda_name = soname_indx;
6368 name = soname;
6369 }
6370 else
6371 {
6372 size_t indx;
6373
6374 name = lbasename (output_bfd->filename);
6375 def.vd_hash = bfd_elf_hash (name);
6376 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6377 name, FALSE);
6378 if (indx == (size_t) -1)
6379 return FALSE;
6380 defaux.vda_name = indx;
6381 }
6382 defaux.vda_next = 0;
6383
6384 _bfd_elf_swap_verdef_out (output_bfd, &def,
6385 (Elf_External_Verdef *) p);
6386 p += sizeof (Elf_External_Verdef);
6387 if (info->create_default_symver)
6388 {
6389 /* Add a symbol representing this version. */
6390 bh = NULL;
6391 if (! (_bfd_generic_link_add_one_symbol
6392 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6393 0, NULL, FALSE,
6394 get_elf_backend_data (dynobj)->collect, &bh)))
6395 return FALSE;
6396 h = (struct elf_link_hash_entry *) bh;
6397 h->non_elf = 0;
6398 h->def_regular = 1;
6399 h->type = STT_OBJECT;
6400 h->verinfo.vertree = NULL;
6401
6402 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6403 return FALSE;
6404
6405 /* Create a duplicate of the base version with the same
6406 aux block, but different flags. */
6407 def.vd_flags = 0;
6408 def.vd_ndx = 2;
6409 def.vd_aux = sizeof (Elf_External_Verdef);
6410 if (verdefs)
6411 def.vd_next = (sizeof (Elf_External_Verdef)
6412 + sizeof (Elf_External_Verdaux));
6413 else
6414 def.vd_next = 0;
6415 _bfd_elf_swap_verdef_out (output_bfd, &def,
6416 (Elf_External_Verdef *) p);
6417 p += sizeof (Elf_External_Verdef);
6418 }
6419 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6420 (Elf_External_Verdaux *) p);
6421 p += sizeof (Elf_External_Verdaux);
6422
6423 for (t = verdefs; t != NULL; t = t->next)
6424 {
6425 unsigned int cdeps;
6426 struct bfd_elf_version_deps *n;
6427
6428 /* Don't emit the base version twice. */
6429 if (t->vernum == 0)
6430 continue;
6431
6432 cdeps = 0;
6433 for (n = t->deps; n != NULL; n = n->next)
6434 ++cdeps;
6435
6436 /* Add a symbol representing this version. */
6437 bh = NULL;
6438 if (! (_bfd_generic_link_add_one_symbol
6439 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6440 0, NULL, FALSE,
6441 get_elf_backend_data (dynobj)->collect, &bh)))
6442 return FALSE;
6443 h = (struct elf_link_hash_entry *) bh;
6444 h->non_elf = 0;
6445 h->def_regular = 1;
6446 h->type = STT_OBJECT;
6447 h->verinfo.vertree = t;
6448
6449 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6450 return FALSE;
6451
6452 def.vd_version = VER_DEF_CURRENT;
6453 def.vd_flags = 0;
6454 if (t->globals.list == NULL
6455 && t->locals.list == NULL
6456 && ! t->used)
6457 def.vd_flags |= VER_FLG_WEAK;
6458 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6459 def.vd_cnt = cdeps + 1;
6460 def.vd_hash = bfd_elf_hash (t->name);
6461 def.vd_aux = sizeof (Elf_External_Verdef);
6462 def.vd_next = 0;
6463
6464 /* If a basever node is next, it *must* be the last node in
6465 the chain, otherwise Verdef construction breaks. */
6466 if (t->next != NULL && t->next->vernum == 0)
6467 BFD_ASSERT (t->next->next == NULL);
6468
6469 if (t->next != NULL && t->next->vernum != 0)
6470 def.vd_next = (sizeof (Elf_External_Verdef)
6471 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6472
6473 _bfd_elf_swap_verdef_out (output_bfd, &def,
6474 (Elf_External_Verdef *) p);
6475 p += sizeof (Elf_External_Verdef);
6476
6477 defaux.vda_name = h->dynstr_index;
6478 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6479 h->dynstr_index);
6480 defaux.vda_next = 0;
6481 if (t->deps != NULL)
6482 defaux.vda_next = sizeof (Elf_External_Verdaux);
6483 t->name_indx = defaux.vda_name;
6484
6485 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6486 (Elf_External_Verdaux *) p);
6487 p += sizeof (Elf_External_Verdaux);
6488
6489 for (n = t->deps; n != NULL; n = n->next)
6490 {
6491 if (n->version_needed == NULL)
6492 {
6493 /* This can happen if there was an error in the
6494 version script. */
6495 defaux.vda_name = 0;
6496 }
6497 else
6498 {
6499 defaux.vda_name = n->version_needed->name_indx;
6500 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6501 defaux.vda_name);
6502 }
6503 if (n->next == NULL)
6504 defaux.vda_next = 0;
6505 else
6506 defaux.vda_next = sizeof (Elf_External_Verdaux);
6507
6508 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6509 (Elf_External_Verdaux *) p);
6510 p += sizeof (Elf_External_Verdaux);
6511 }
6512 }
6513
6514 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6515 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6516 return FALSE;
6517
6518 elf_tdata (output_bfd)->cverdefs = cdefs;
6519 }
6520
6521 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6522 {
6523 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6524 return FALSE;
6525 }
6526 else if (info->flags & DF_BIND_NOW)
6527 {
6528 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6529 return FALSE;
6530 }
6531
6532 if (info->flags_1)
6533 {
6534 if (bfd_link_executable (info))
6535 info->flags_1 &= ~ (DF_1_INITFIRST
6536 | DF_1_NODELETE
6537 | DF_1_NOOPEN);
6538 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6539 return FALSE;
6540 }
6541
6542 /* Work out the size of the version reference section. */
6543
6544 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6545 BFD_ASSERT (s != NULL);
6546 {
6547 struct elf_find_verdep_info sinfo;
6548
6549 sinfo.info = info;
6550 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6551 if (sinfo.vers == 0)
6552 sinfo.vers = 1;
6553 sinfo.failed = FALSE;
6554
6555 elf_link_hash_traverse (elf_hash_table (info),
6556 _bfd_elf_link_find_version_dependencies,
6557 &sinfo);
6558 if (sinfo.failed)
6559 return FALSE;
6560
6561 if (elf_tdata (output_bfd)->verref == NULL)
6562 s->flags |= SEC_EXCLUDE;
6563 else
6564 {
6565 Elf_Internal_Verneed *t;
6566 unsigned int size;
6567 unsigned int crefs;
6568 bfd_byte *p;
6569
6570 /* Build the version dependency section. */
6571 size = 0;
6572 crefs = 0;
6573 for (t = elf_tdata (output_bfd)->verref;
6574 t != NULL;
6575 t = t->vn_nextref)
6576 {
6577 Elf_Internal_Vernaux *a;
6578
6579 size += sizeof (Elf_External_Verneed);
6580 ++crefs;
6581 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6582 size += sizeof (Elf_External_Vernaux);
6583 }
6584
6585 s->size = size;
6586 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6587 if (s->contents == NULL)
6588 return FALSE;
6589
6590 p = s->contents;
6591 for (t = elf_tdata (output_bfd)->verref;
6592 t != NULL;
6593 t = t->vn_nextref)
6594 {
6595 unsigned int caux;
6596 Elf_Internal_Vernaux *a;
6597 size_t indx;
6598
6599 caux = 0;
6600 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6601 ++caux;
6602
6603 t->vn_version = VER_NEED_CURRENT;
6604 t->vn_cnt = caux;
6605 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6606 elf_dt_name (t->vn_bfd) != NULL
6607 ? elf_dt_name (t->vn_bfd)
6608 : lbasename (t->vn_bfd->filename),
6609 FALSE);
6610 if (indx == (size_t) -1)
6611 return FALSE;
6612 t->vn_file = indx;
6613 t->vn_aux = sizeof (Elf_External_Verneed);
6614 if (t->vn_nextref == NULL)
6615 t->vn_next = 0;
6616 else
6617 t->vn_next = (sizeof (Elf_External_Verneed)
6618 + caux * sizeof (Elf_External_Vernaux));
6619
6620 _bfd_elf_swap_verneed_out (output_bfd, t,
6621 (Elf_External_Verneed *) p);
6622 p += sizeof (Elf_External_Verneed);
6623
6624 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6625 {
6626 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6627 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6628 a->vna_nodename, FALSE);
6629 if (indx == (size_t) -1)
6630 return FALSE;
6631 a->vna_name = indx;
6632 if (a->vna_nextptr == NULL)
6633 a->vna_next = 0;
6634 else
6635 a->vna_next = sizeof (Elf_External_Vernaux);
6636
6637 _bfd_elf_swap_vernaux_out (output_bfd, a,
6638 (Elf_External_Vernaux *) p);
6639 p += sizeof (Elf_External_Vernaux);
6640 }
6641 }
6642
6643 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6644 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6645 return FALSE;
6646
6647 elf_tdata (output_bfd)->cverrefs = crefs;
6648 }
6649 }
6650
6651 if ((elf_tdata (output_bfd)->cverrefs == 0
6652 && elf_tdata (output_bfd)->cverdefs == 0)
6653 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6654 &section_sym_count) == 0)
6655 {
6656 s = bfd_get_linker_section (dynobj, ".gnu.version");
6657 s->flags |= SEC_EXCLUDE;
6658 }
6659 }
6660 return TRUE;
6661 }
6662
6663 /* Find the first non-excluded output section. We'll use its
6664 section symbol for some emitted relocs. */
6665 void
6666 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6667 {
6668 asection *s;
6669
6670 for (s = output_bfd->sections; s != NULL; s = s->next)
6671 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6672 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6673 {
6674 elf_hash_table (info)->text_index_section = s;
6675 break;
6676 }
6677 }
6678
6679 /* Find two non-excluded output sections, one for code, one for data.
6680 We'll use their section symbols for some emitted relocs. */
6681 void
6682 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6683 {
6684 asection *s;
6685
6686 /* Data first, since setting text_index_section changes
6687 _bfd_elf_link_omit_section_dynsym. */
6688 for (s = output_bfd->sections; s != NULL; s = s->next)
6689 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6690 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6691 {
6692 elf_hash_table (info)->data_index_section = s;
6693 break;
6694 }
6695
6696 for (s = output_bfd->sections; s != NULL; s = s->next)
6697 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6698 == (SEC_ALLOC | SEC_READONLY))
6699 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6700 {
6701 elf_hash_table (info)->text_index_section = s;
6702 break;
6703 }
6704
6705 if (elf_hash_table (info)->text_index_section == NULL)
6706 elf_hash_table (info)->text_index_section
6707 = elf_hash_table (info)->data_index_section;
6708 }
6709
6710 bfd_boolean
6711 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6712 {
6713 const struct elf_backend_data *bed;
6714
6715 if (!is_elf_hash_table (info->hash))
6716 return TRUE;
6717
6718 bed = get_elf_backend_data (output_bfd);
6719 (*bed->elf_backend_init_index_section) (output_bfd, info);
6720
6721 if (elf_hash_table (info)->dynamic_sections_created)
6722 {
6723 bfd *dynobj;
6724 asection *s;
6725 bfd_size_type dynsymcount;
6726 unsigned long section_sym_count;
6727 unsigned int dtagcount;
6728
6729 dynobj = elf_hash_table (info)->dynobj;
6730
6731 /* Assign dynsym indicies. In a shared library we generate a
6732 section symbol for each output section, which come first.
6733 Next come all of the back-end allocated local dynamic syms,
6734 followed by the rest of the global symbols. */
6735
6736 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6737 &section_sym_count);
6738
6739 /* Work out the size of the symbol version section. */
6740 s = bfd_get_linker_section (dynobj, ".gnu.version");
6741 BFD_ASSERT (s != NULL);
6742 if ((s->flags & SEC_EXCLUDE) == 0)
6743 {
6744 s->size = dynsymcount * sizeof (Elf_External_Versym);
6745 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6746 if (s->contents == NULL)
6747 return FALSE;
6748
6749 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6750 return FALSE;
6751 }
6752
6753 /* Set the size of the .dynsym and .hash sections. We counted
6754 the number of dynamic symbols in elf_link_add_object_symbols.
6755 We will build the contents of .dynsym and .hash when we build
6756 the final symbol table, because until then we do not know the
6757 correct value to give the symbols. We built the .dynstr
6758 section as we went along in elf_link_add_object_symbols. */
6759 s = elf_hash_table (info)->dynsym;
6760 BFD_ASSERT (s != NULL);
6761 s->size = dynsymcount * bed->s->sizeof_sym;
6762
6763 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6764 if (s->contents == NULL)
6765 return FALSE;
6766
6767 /* The first entry in .dynsym is a dummy symbol. Clear all the
6768 section syms, in case we don't output them all. */
6769 ++section_sym_count;
6770 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6771
6772 elf_hash_table (info)->bucketcount = 0;
6773
6774 /* Compute the size of the hashing table. As a side effect this
6775 computes the hash values for all the names we export. */
6776 if (info->emit_hash)
6777 {
6778 unsigned long int *hashcodes;
6779 struct hash_codes_info hashinf;
6780 bfd_size_type amt;
6781 unsigned long int nsyms;
6782 size_t bucketcount;
6783 size_t hash_entry_size;
6784
6785 /* Compute the hash values for all exported symbols. At the same
6786 time store the values in an array so that we could use them for
6787 optimizations. */
6788 amt = dynsymcount * sizeof (unsigned long int);
6789 hashcodes = (unsigned long int *) bfd_malloc (amt);
6790 if (hashcodes == NULL)
6791 return FALSE;
6792 hashinf.hashcodes = hashcodes;
6793 hashinf.error = FALSE;
6794
6795 /* Put all hash values in HASHCODES. */
6796 elf_link_hash_traverse (elf_hash_table (info),
6797 elf_collect_hash_codes, &hashinf);
6798 if (hashinf.error)
6799 {
6800 free (hashcodes);
6801 return FALSE;
6802 }
6803
6804 nsyms = hashinf.hashcodes - hashcodes;
6805 bucketcount
6806 = compute_bucket_count (info, hashcodes, nsyms, 0);
6807 free (hashcodes);
6808
6809 if (bucketcount == 0)
6810 return FALSE;
6811
6812 elf_hash_table (info)->bucketcount = bucketcount;
6813
6814 s = bfd_get_linker_section (dynobj, ".hash");
6815 BFD_ASSERT (s != NULL);
6816 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6817 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6818 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6819 if (s->contents == NULL)
6820 return FALSE;
6821
6822 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6823 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6824 s->contents + hash_entry_size);
6825 }
6826
6827 if (info->emit_gnu_hash)
6828 {
6829 size_t i, cnt;
6830 unsigned char *contents;
6831 struct collect_gnu_hash_codes cinfo;
6832 bfd_size_type amt;
6833 size_t bucketcount;
6834
6835 memset (&cinfo, 0, sizeof (cinfo));
6836
6837 /* Compute the hash values for all exported symbols. At the same
6838 time store the values in an array so that we could use them for
6839 optimizations. */
6840 amt = dynsymcount * 2 * sizeof (unsigned long int);
6841 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
6842 if (cinfo.hashcodes == NULL)
6843 return FALSE;
6844
6845 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6846 cinfo.min_dynindx = -1;
6847 cinfo.output_bfd = output_bfd;
6848 cinfo.bed = bed;
6849
6850 /* Put all hash values in HASHCODES. */
6851 elf_link_hash_traverse (elf_hash_table (info),
6852 elf_collect_gnu_hash_codes, &cinfo);
6853 if (cinfo.error)
6854 {
6855 free (cinfo.hashcodes);
6856 return FALSE;
6857 }
6858
6859 bucketcount
6860 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6861
6862 if (bucketcount == 0)
6863 {
6864 free (cinfo.hashcodes);
6865 return FALSE;
6866 }
6867
6868 s = bfd_get_linker_section (dynobj, ".gnu.hash");
6869 BFD_ASSERT (s != NULL);
6870
6871 if (cinfo.nsyms == 0)
6872 {
6873 /* Empty .gnu.hash section is special. */
6874 BFD_ASSERT (cinfo.min_dynindx == -1);
6875 free (cinfo.hashcodes);
6876 s->size = 5 * 4 + bed->s->arch_size / 8;
6877 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6878 if (contents == NULL)
6879 return FALSE;
6880 s->contents = contents;
6881 /* 1 empty bucket. */
6882 bfd_put_32 (output_bfd, 1, contents);
6883 /* SYMIDX above the special symbol 0. */
6884 bfd_put_32 (output_bfd, 1, contents + 4);
6885 /* Just one word for bitmask. */
6886 bfd_put_32 (output_bfd, 1, contents + 8);
6887 /* Only hash fn bloom filter. */
6888 bfd_put_32 (output_bfd, 0, contents + 12);
6889 /* No hashes are valid - empty bitmask. */
6890 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6891 /* No hashes in the only bucket. */
6892 bfd_put_32 (output_bfd, 0,
6893 contents + 16 + bed->s->arch_size / 8);
6894 }
6895 else
6896 {
6897 unsigned long int maskwords, maskbitslog2, x;
6898 BFD_ASSERT (cinfo.min_dynindx != -1);
6899
6900 x = cinfo.nsyms;
6901 maskbitslog2 = 1;
6902 while ((x >>= 1) != 0)
6903 ++maskbitslog2;
6904 if (maskbitslog2 < 3)
6905 maskbitslog2 = 5;
6906 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6907 maskbitslog2 = maskbitslog2 + 3;
6908 else
6909 maskbitslog2 = maskbitslog2 + 2;
6910 if (bed->s->arch_size == 64)
6911 {
6912 if (maskbitslog2 == 5)
6913 maskbitslog2 = 6;
6914 cinfo.shift1 = 6;
6915 }
6916 else
6917 cinfo.shift1 = 5;
6918 cinfo.mask = (1 << cinfo.shift1) - 1;
6919 cinfo.shift2 = maskbitslog2;
6920 cinfo.maskbits = 1 << maskbitslog2;
6921 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6922 amt = bucketcount * sizeof (unsigned long int) * 2;
6923 amt += maskwords * sizeof (bfd_vma);
6924 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
6925 if (cinfo.bitmask == NULL)
6926 {
6927 free (cinfo.hashcodes);
6928 return FALSE;
6929 }
6930
6931 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
6932 cinfo.indx = cinfo.counts + bucketcount;
6933 cinfo.symindx = dynsymcount - cinfo.nsyms;
6934 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6935
6936 /* Determine how often each hash bucket is used. */
6937 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6938 for (i = 0; i < cinfo.nsyms; ++i)
6939 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6940
6941 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6942 if (cinfo.counts[i] != 0)
6943 {
6944 cinfo.indx[i] = cnt;
6945 cnt += cinfo.counts[i];
6946 }
6947 BFD_ASSERT (cnt == dynsymcount);
6948 cinfo.bucketcount = bucketcount;
6949 cinfo.local_indx = cinfo.min_dynindx;
6950
6951 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6952 s->size += cinfo.maskbits / 8;
6953 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6954 if (contents == NULL)
6955 {
6956 free (cinfo.bitmask);
6957 free (cinfo.hashcodes);
6958 return FALSE;
6959 }
6960
6961 s->contents = contents;
6962 bfd_put_32 (output_bfd, bucketcount, contents);
6963 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6964 bfd_put_32 (output_bfd, maskwords, contents + 8);
6965 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6966 contents += 16 + cinfo.maskbits / 8;
6967
6968 for (i = 0; i < bucketcount; ++i)
6969 {
6970 if (cinfo.counts[i] == 0)
6971 bfd_put_32 (output_bfd, 0, contents);
6972 else
6973 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6974 contents += 4;
6975 }
6976
6977 cinfo.contents = contents;
6978
6979 /* Renumber dynamic symbols, populate .gnu.hash section. */
6980 elf_link_hash_traverse (elf_hash_table (info),
6981 elf_renumber_gnu_hash_syms, &cinfo);
6982
6983 contents = s->contents + 16;
6984 for (i = 0; i < maskwords; ++i)
6985 {
6986 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6987 contents);
6988 contents += bed->s->arch_size / 8;
6989 }
6990
6991 free (cinfo.bitmask);
6992 free (cinfo.hashcodes);
6993 }
6994 }
6995
6996 s = bfd_get_linker_section (dynobj, ".dynstr");
6997 BFD_ASSERT (s != NULL);
6998
6999 elf_finalize_dynstr (output_bfd, info);
7000
7001 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7002
7003 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7004 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7005 return FALSE;
7006 }
7007
7008 return TRUE;
7009 }
7010 \f
7011 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7012
7013 static void
7014 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7015 asection *sec)
7016 {
7017 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7018 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7019 }
7020
7021 /* Finish SHF_MERGE section merging. */
7022
7023 bfd_boolean
7024 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7025 {
7026 bfd *ibfd;
7027 asection *sec;
7028
7029 if (!is_elf_hash_table (info->hash))
7030 return FALSE;
7031
7032 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7033 if ((ibfd->flags & DYNAMIC) == 0
7034 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7035 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7036 == get_elf_backend_data (obfd)->s->elfclass))
7037 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7038 if ((sec->flags & SEC_MERGE) != 0
7039 && !bfd_is_abs_section (sec->output_section))
7040 {
7041 struct bfd_elf_section_data *secdata;
7042
7043 secdata = elf_section_data (sec);
7044 if (! _bfd_add_merge_section (obfd,
7045 &elf_hash_table (info)->merge_info,
7046 sec, &secdata->sec_info))
7047 return FALSE;
7048 else if (secdata->sec_info)
7049 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7050 }
7051
7052 if (elf_hash_table (info)->merge_info != NULL)
7053 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7054 merge_sections_remove_hook);
7055 return TRUE;
7056 }
7057
7058 /* Create an entry in an ELF linker hash table. */
7059
7060 struct bfd_hash_entry *
7061 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7062 struct bfd_hash_table *table,
7063 const char *string)
7064 {
7065 /* Allocate the structure if it has not already been allocated by a
7066 subclass. */
7067 if (entry == NULL)
7068 {
7069 entry = (struct bfd_hash_entry *)
7070 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7071 if (entry == NULL)
7072 return entry;
7073 }
7074
7075 /* Call the allocation method of the superclass. */
7076 entry = _bfd_link_hash_newfunc (entry, table, string);
7077 if (entry != NULL)
7078 {
7079 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7080 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7081
7082 /* Set local fields. */
7083 ret->indx = -1;
7084 ret->dynindx = -1;
7085 ret->got = htab->init_got_refcount;
7086 ret->plt = htab->init_plt_refcount;
7087 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7088 - offsetof (struct elf_link_hash_entry, size)));
7089 /* Assume that we have been called by a non-ELF symbol reader.
7090 This flag is then reset by the code which reads an ELF input
7091 file. This ensures that a symbol created by a non-ELF symbol
7092 reader will have the flag set correctly. */
7093 ret->non_elf = 1;
7094 }
7095
7096 return entry;
7097 }
7098
7099 /* Copy data from an indirect symbol to its direct symbol, hiding the
7100 old indirect symbol. Also used for copying flags to a weakdef. */
7101
7102 void
7103 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7104 struct elf_link_hash_entry *dir,
7105 struct elf_link_hash_entry *ind)
7106 {
7107 struct elf_link_hash_table *htab;
7108
7109 /* Copy down any references that we may have already seen to the
7110 symbol which just became indirect. */
7111
7112 if (dir->versioned != versioned_hidden)
7113 dir->ref_dynamic |= ind->ref_dynamic;
7114 dir->ref_regular |= ind->ref_regular;
7115 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7116 dir->non_got_ref |= ind->non_got_ref;
7117 dir->needs_plt |= ind->needs_plt;
7118 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7119
7120 if (ind->root.type != bfd_link_hash_indirect)
7121 return;
7122
7123 /* Copy over the global and procedure linkage table refcount entries.
7124 These may have been already set up by a check_relocs routine. */
7125 htab = elf_hash_table (info);
7126 if (ind->got.refcount > htab->init_got_refcount.refcount)
7127 {
7128 if (dir->got.refcount < 0)
7129 dir->got.refcount = 0;
7130 dir->got.refcount += ind->got.refcount;
7131 ind->got.refcount = htab->init_got_refcount.refcount;
7132 }
7133
7134 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7135 {
7136 if (dir->plt.refcount < 0)
7137 dir->plt.refcount = 0;
7138 dir->plt.refcount += ind->plt.refcount;
7139 ind->plt.refcount = htab->init_plt_refcount.refcount;
7140 }
7141
7142 if (ind->dynindx != -1)
7143 {
7144 if (dir->dynindx != -1)
7145 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7146 dir->dynindx = ind->dynindx;
7147 dir->dynstr_index = ind->dynstr_index;
7148 ind->dynindx = -1;
7149 ind->dynstr_index = 0;
7150 }
7151 }
7152
7153 void
7154 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7155 struct elf_link_hash_entry *h,
7156 bfd_boolean force_local)
7157 {
7158 /* STT_GNU_IFUNC symbol must go through PLT. */
7159 if (h->type != STT_GNU_IFUNC)
7160 {
7161 h->plt = elf_hash_table (info)->init_plt_offset;
7162 h->needs_plt = 0;
7163 }
7164 if (force_local)
7165 {
7166 h->forced_local = 1;
7167 if (h->dynindx != -1)
7168 {
7169 h->dynindx = -1;
7170 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7171 h->dynstr_index);
7172 }
7173 }
7174 }
7175
7176 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7177 caller. */
7178
7179 bfd_boolean
7180 _bfd_elf_link_hash_table_init
7181 (struct elf_link_hash_table *table,
7182 bfd *abfd,
7183 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7184 struct bfd_hash_table *,
7185 const char *),
7186 unsigned int entsize,
7187 enum elf_target_id target_id)
7188 {
7189 bfd_boolean ret;
7190 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7191
7192 table->init_got_refcount.refcount = can_refcount - 1;
7193 table->init_plt_refcount.refcount = can_refcount - 1;
7194 table->init_got_offset.offset = -(bfd_vma) 1;
7195 table->init_plt_offset.offset = -(bfd_vma) 1;
7196 /* The first dynamic symbol is a dummy. */
7197 table->dynsymcount = 1;
7198
7199 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7200
7201 table->root.type = bfd_link_elf_hash_table;
7202 table->hash_table_id = target_id;
7203
7204 return ret;
7205 }
7206
7207 /* Create an ELF linker hash table. */
7208
7209 struct bfd_link_hash_table *
7210 _bfd_elf_link_hash_table_create (bfd *abfd)
7211 {
7212 struct elf_link_hash_table *ret;
7213 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7214
7215 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7216 if (ret == NULL)
7217 return NULL;
7218
7219 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7220 sizeof (struct elf_link_hash_entry),
7221 GENERIC_ELF_DATA))
7222 {
7223 free (ret);
7224 return NULL;
7225 }
7226 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7227
7228 return &ret->root;
7229 }
7230
7231 /* Destroy an ELF linker hash table. */
7232
7233 void
7234 _bfd_elf_link_hash_table_free (bfd *obfd)
7235 {
7236 struct elf_link_hash_table *htab;
7237
7238 htab = (struct elf_link_hash_table *) obfd->link.hash;
7239 if (htab->dynstr != NULL)
7240 _bfd_elf_strtab_free (htab->dynstr);
7241 _bfd_merge_sections_free (htab->merge_info);
7242 _bfd_generic_link_hash_table_free (obfd);
7243 }
7244
7245 /* This is a hook for the ELF emulation code in the generic linker to
7246 tell the backend linker what file name to use for the DT_NEEDED
7247 entry for a dynamic object. */
7248
7249 void
7250 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7251 {
7252 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7253 && bfd_get_format (abfd) == bfd_object)
7254 elf_dt_name (abfd) = name;
7255 }
7256
7257 int
7258 bfd_elf_get_dyn_lib_class (bfd *abfd)
7259 {
7260 int lib_class;
7261 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7262 && bfd_get_format (abfd) == bfd_object)
7263 lib_class = elf_dyn_lib_class (abfd);
7264 else
7265 lib_class = 0;
7266 return lib_class;
7267 }
7268
7269 void
7270 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7271 {
7272 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7273 && bfd_get_format (abfd) == bfd_object)
7274 elf_dyn_lib_class (abfd) = lib_class;
7275 }
7276
7277 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7278 the linker ELF emulation code. */
7279
7280 struct bfd_link_needed_list *
7281 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7282 struct bfd_link_info *info)
7283 {
7284 if (! is_elf_hash_table (info->hash))
7285 return NULL;
7286 return elf_hash_table (info)->needed;
7287 }
7288
7289 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7290 hook for the linker ELF emulation code. */
7291
7292 struct bfd_link_needed_list *
7293 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7294 struct bfd_link_info *info)
7295 {
7296 if (! is_elf_hash_table (info->hash))
7297 return NULL;
7298 return elf_hash_table (info)->runpath;
7299 }
7300
7301 /* Get the name actually used for a dynamic object for a link. This
7302 is the SONAME entry if there is one. Otherwise, it is the string
7303 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7304
7305 const char *
7306 bfd_elf_get_dt_soname (bfd *abfd)
7307 {
7308 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7309 && bfd_get_format (abfd) == bfd_object)
7310 return elf_dt_name (abfd);
7311 return NULL;
7312 }
7313
7314 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7315 the ELF linker emulation code. */
7316
7317 bfd_boolean
7318 bfd_elf_get_bfd_needed_list (bfd *abfd,
7319 struct bfd_link_needed_list **pneeded)
7320 {
7321 asection *s;
7322 bfd_byte *dynbuf = NULL;
7323 unsigned int elfsec;
7324 unsigned long shlink;
7325 bfd_byte *extdyn, *extdynend;
7326 size_t extdynsize;
7327 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7328
7329 *pneeded = NULL;
7330
7331 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7332 || bfd_get_format (abfd) != bfd_object)
7333 return TRUE;
7334
7335 s = bfd_get_section_by_name (abfd, ".dynamic");
7336 if (s == NULL || s->size == 0)
7337 return TRUE;
7338
7339 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7340 goto error_return;
7341
7342 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7343 if (elfsec == SHN_BAD)
7344 goto error_return;
7345
7346 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7347
7348 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7349 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7350
7351 extdyn = dynbuf;
7352 extdynend = extdyn + s->size;
7353 for (; extdyn < extdynend; extdyn += extdynsize)
7354 {
7355 Elf_Internal_Dyn dyn;
7356
7357 (*swap_dyn_in) (abfd, extdyn, &dyn);
7358
7359 if (dyn.d_tag == DT_NULL)
7360 break;
7361
7362 if (dyn.d_tag == DT_NEEDED)
7363 {
7364 const char *string;
7365 struct bfd_link_needed_list *l;
7366 unsigned int tagv = dyn.d_un.d_val;
7367 bfd_size_type amt;
7368
7369 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7370 if (string == NULL)
7371 goto error_return;
7372
7373 amt = sizeof *l;
7374 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7375 if (l == NULL)
7376 goto error_return;
7377
7378 l->by = abfd;
7379 l->name = string;
7380 l->next = *pneeded;
7381 *pneeded = l;
7382 }
7383 }
7384
7385 free (dynbuf);
7386
7387 return TRUE;
7388
7389 error_return:
7390 if (dynbuf != NULL)
7391 free (dynbuf);
7392 return FALSE;
7393 }
7394
7395 struct elf_symbuf_symbol
7396 {
7397 unsigned long st_name; /* Symbol name, index in string tbl */
7398 unsigned char st_info; /* Type and binding attributes */
7399 unsigned char st_other; /* Visibilty, and target specific */
7400 };
7401
7402 struct elf_symbuf_head
7403 {
7404 struct elf_symbuf_symbol *ssym;
7405 size_t count;
7406 unsigned int st_shndx;
7407 };
7408
7409 struct elf_symbol
7410 {
7411 union
7412 {
7413 Elf_Internal_Sym *isym;
7414 struct elf_symbuf_symbol *ssym;
7415 } u;
7416 const char *name;
7417 };
7418
7419 /* Sort references to symbols by ascending section number. */
7420
7421 static int
7422 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7423 {
7424 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7425 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7426
7427 return s1->st_shndx - s2->st_shndx;
7428 }
7429
7430 static int
7431 elf_sym_name_compare (const void *arg1, const void *arg2)
7432 {
7433 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7434 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7435 return strcmp (s1->name, s2->name);
7436 }
7437
7438 static struct elf_symbuf_head *
7439 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
7440 {
7441 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7442 struct elf_symbuf_symbol *ssym;
7443 struct elf_symbuf_head *ssymbuf, *ssymhead;
7444 size_t i, shndx_count, total_size;
7445
7446 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7447 if (indbuf == NULL)
7448 return NULL;
7449
7450 for (ind = indbuf, i = 0; i < symcount; i++)
7451 if (isymbuf[i].st_shndx != SHN_UNDEF)
7452 *ind++ = &isymbuf[i];
7453 indbufend = ind;
7454
7455 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7456 elf_sort_elf_symbol);
7457
7458 shndx_count = 0;
7459 if (indbufend > indbuf)
7460 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7461 if (ind[0]->st_shndx != ind[1]->st_shndx)
7462 shndx_count++;
7463
7464 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7465 + (indbufend - indbuf) * sizeof (*ssym));
7466 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7467 if (ssymbuf == NULL)
7468 {
7469 free (indbuf);
7470 return NULL;
7471 }
7472
7473 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7474 ssymbuf->ssym = NULL;
7475 ssymbuf->count = shndx_count;
7476 ssymbuf->st_shndx = 0;
7477 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7478 {
7479 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7480 {
7481 ssymhead++;
7482 ssymhead->ssym = ssym;
7483 ssymhead->count = 0;
7484 ssymhead->st_shndx = (*ind)->st_shndx;
7485 }
7486 ssym->st_name = (*ind)->st_name;
7487 ssym->st_info = (*ind)->st_info;
7488 ssym->st_other = (*ind)->st_other;
7489 ssymhead->count++;
7490 }
7491 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
7492 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7493 == total_size));
7494
7495 free (indbuf);
7496 return ssymbuf;
7497 }
7498
7499 /* Check if 2 sections define the same set of local and global
7500 symbols. */
7501
7502 static bfd_boolean
7503 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7504 struct bfd_link_info *info)
7505 {
7506 bfd *bfd1, *bfd2;
7507 const struct elf_backend_data *bed1, *bed2;
7508 Elf_Internal_Shdr *hdr1, *hdr2;
7509 size_t symcount1, symcount2;
7510 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7511 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7512 Elf_Internal_Sym *isym, *isymend;
7513 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7514 size_t count1, count2, i;
7515 unsigned int shndx1, shndx2;
7516 bfd_boolean result;
7517
7518 bfd1 = sec1->owner;
7519 bfd2 = sec2->owner;
7520
7521 /* Both sections have to be in ELF. */
7522 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7523 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7524 return FALSE;
7525
7526 if (elf_section_type (sec1) != elf_section_type (sec2))
7527 return FALSE;
7528
7529 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7530 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7531 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7532 return FALSE;
7533
7534 bed1 = get_elf_backend_data (bfd1);
7535 bed2 = get_elf_backend_data (bfd2);
7536 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7537 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7538 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7539 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7540
7541 if (symcount1 == 0 || symcount2 == 0)
7542 return FALSE;
7543
7544 result = FALSE;
7545 isymbuf1 = NULL;
7546 isymbuf2 = NULL;
7547 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7548 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7549
7550 if (ssymbuf1 == NULL)
7551 {
7552 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7553 NULL, NULL, NULL);
7554 if (isymbuf1 == NULL)
7555 goto done;
7556
7557 if (!info->reduce_memory_overheads)
7558 elf_tdata (bfd1)->symbuf = ssymbuf1
7559 = elf_create_symbuf (symcount1, isymbuf1);
7560 }
7561
7562 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7563 {
7564 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7565 NULL, NULL, NULL);
7566 if (isymbuf2 == NULL)
7567 goto done;
7568
7569 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7570 elf_tdata (bfd2)->symbuf = ssymbuf2
7571 = elf_create_symbuf (symcount2, isymbuf2);
7572 }
7573
7574 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7575 {
7576 /* Optimized faster version. */
7577 size_t lo, hi, mid;
7578 struct elf_symbol *symp;
7579 struct elf_symbuf_symbol *ssym, *ssymend;
7580
7581 lo = 0;
7582 hi = ssymbuf1->count;
7583 ssymbuf1++;
7584 count1 = 0;
7585 while (lo < hi)
7586 {
7587 mid = (lo + hi) / 2;
7588 if (shndx1 < ssymbuf1[mid].st_shndx)
7589 hi = mid;
7590 else if (shndx1 > ssymbuf1[mid].st_shndx)
7591 lo = mid + 1;
7592 else
7593 {
7594 count1 = ssymbuf1[mid].count;
7595 ssymbuf1 += mid;
7596 break;
7597 }
7598 }
7599
7600 lo = 0;
7601 hi = ssymbuf2->count;
7602 ssymbuf2++;
7603 count2 = 0;
7604 while (lo < hi)
7605 {
7606 mid = (lo + hi) / 2;
7607 if (shndx2 < ssymbuf2[mid].st_shndx)
7608 hi = mid;
7609 else if (shndx2 > ssymbuf2[mid].st_shndx)
7610 lo = mid + 1;
7611 else
7612 {
7613 count2 = ssymbuf2[mid].count;
7614 ssymbuf2 += mid;
7615 break;
7616 }
7617 }
7618
7619 if (count1 == 0 || count2 == 0 || count1 != count2)
7620 goto done;
7621
7622 symtable1
7623 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7624 symtable2
7625 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
7626 if (symtable1 == NULL || symtable2 == NULL)
7627 goto done;
7628
7629 symp = symtable1;
7630 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7631 ssym < ssymend; ssym++, symp++)
7632 {
7633 symp->u.ssym = ssym;
7634 symp->name = bfd_elf_string_from_elf_section (bfd1,
7635 hdr1->sh_link,
7636 ssym->st_name);
7637 }
7638
7639 symp = symtable2;
7640 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7641 ssym < ssymend; ssym++, symp++)
7642 {
7643 symp->u.ssym = ssym;
7644 symp->name = bfd_elf_string_from_elf_section (bfd2,
7645 hdr2->sh_link,
7646 ssym->st_name);
7647 }
7648
7649 /* Sort symbol by name. */
7650 qsort (symtable1, count1, sizeof (struct elf_symbol),
7651 elf_sym_name_compare);
7652 qsort (symtable2, count1, sizeof (struct elf_symbol),
7653 elf_sym_name_compare);
7654
7655 for (i = 0; i < count1; i++)
7656 /* Two symbols must have the same binding, type and name. */
7657 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7658 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7659 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7660 goto done;
7661
7662 result = TRUE;
7663 goto done;
7664 }
7665
7666 symtable1 = (struct elf_symbol *)
7667 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7668 symtable2 = (struct elf_symbol *)
7669 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7670 if (symtable1 == NULL || symtable2 == NULL)
7671 goto done;
7672
7673 /* Count definitions in the section. */
7674 count1 = 0;
7675 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7676 if (isym->st_shndx == shndx1)
7677 symtable1[count1++].u.isym = isym;
7678
7679 count2 = 0;
7680 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7681 if (isym->st_shndx == shndx2)
7682 symtable2[count2++].u.isym = isym;
7683
7684 if (count1 == 0 || count2 == 0 || count1 != count2)
7685 goto done;
7686
7687 for (i = 0; i < count1; i++)
7688 symtable1[i].name
7689 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7690 symtable1[i].u.isym->st_name);
7691
7692 for (i = 0; i < count2; i++)
7693 symtable2[i].name
7694 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7695 symtable2[i].u.isym->st_name);
7696
7697 /* Sort symbol by name. */
7698 qsort (symtable1, count1, sizeof (struct elf_symbol),
7699 elf_sym_name_compare);
7700 qsort (symtable2, count1, sizeof (struct elf_symbol),
7701 elf_sym_name_compare);
7702
7703 for (i = 0; i < count1; i++)
7704 /* Two symbols must have the same binding, type and name. */
7705 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7706 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7707 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7708 goto done;
7709
7710 result = TRUE;
7711
7712 done:
7713 if (symtable1)
7714 free (symtable1);
7715 if (symtable2)
7716 free (symtable2);
7717 if (isymbuf1)
7718 free (isymbuf1);
7719 if (isymbuf2)
7720 free (isymbuf2);
7721
7722 return result;
7723 }
7724
7725 /* Return TRUE if 2 section types are compatible. */
7726
7727 bfd_boolean
7728 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7729 bfd *bbfd, const asection *bsec)
7730 {
7731 if (asec == NULL
7732 || bsec == NULL
7733 || abfd->xvec->flavour != bfd_target_elf_flavour
7734 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7735 return TRUE;
7736
7737 return elf_section_type (asec) == elf_section_type (bsec);
7738 }
7739 \f
7740 /* Final phase of ELF linker. */
7741
7742 /* A structure we use to avoid passing large numbers of arguments. */
7743
7744 struct elf_final_link_info
7745 {
7746 /* General link information. */
7747 struct bfd_link_info *info;
7748 /* Output BFD. */
7749 bfd *output_bfd;
7750 /* Symbol string table. */
7751 struct elf_strtab_hash *symstrtab;
7752 /* .hash section. */
7753 asection *hash_sec;
7754 /* symbol version section (.gnu.version). */
7755 asection *symver_sec;
7756 /* Buffer large enough to hold contents of any section. */
7757 bfd_byte *contents;
7758 /* Buffer large enough to hold external relocs of any section. */
7759 void *external_relocs;
7760 /* Buffer large enough to hold internal relocs of any section. */
7761 Elf_Internal_Rela *internal_relocs;
7762 /* Buffer large enough to hold external local symbols of any input
7763 BFD. */
7764 bfd_byte *external_syms;
7765 /* And a buffer for symbol section indices. */
7766 Elf_External_Sym_Shndx *locsym_shndx;
7767 /* Buffer large enough to hold internal local symbols of any input
7768 BFD. */
7769 Elf_Internal_Sym *internal_syms;
7770 /* Array large enough to hold a symbol index for each local symbol
7771 of any input BFD. */
7772 long *indices;
7773 /* Array large enough to hold a section pointer for each local
7774 symbol of any input BFD. */
7775 asection **sections;
7776 /* Buffer for SHT_SYMTAB_SHNDX section. */
7777 Elf_External_Sym_Shndx *symshndxbuf;
7778 /* Number of STT_FILE syms seen. */
7779 size_t filesym_count;
7780 };
7781
7782 /* This struct is used to pass information to elf_link_output_extsym. */
7783
7784 struct elf_outext_info
7785 {
7786 bfd_boolean failed;
7787 bfd_boolean localsyms;
7788 bfd_boolean file_sym_done;
7789 struct elf_final_link_info *flinfo;
7790 };
7791
7792
7793 /* Support for evaluating a complex relocation.
7794
7795 Complex relocations are generalized, self-describing relocations. The
7796 implementation of them consists of two parts: complex symbols, and the
7797 relocations themselves.
7798
7799 The relocations are use a reserved elf-wide relocation type code (R_RELC
7800 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7801 information (start bit, end bit, word width, etc) into the addend. This
7802 information is extracted from CGEN-generated operand tables within gas.
7803
7804 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7805 internal) representing prefix-notation expressions, including but not
7806 limited to those sorts of expressions normally encoded as addends in the
7807 addend field. The symbol mangling format is:
7808
7809 <node> := <literal>
7810 | <unary-operator> ':' <node>
7811 | <binary-operator> ':' <node> ':' <node>
7812 ;
7813
7814 <literal> := 's' <digits=N> ':' <N character symbol name>
7815 | 'S' <digits=N> ':' <N character section name>
7816 | '#' <hexdigits>
7817 ;
7818
7819 <binary-operator> := as in C
7820 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7821
7822 static void
7823 set_symbol_value (bfd *bfd_with_globals,
7824 Elf_Internal_Sym *isymbuf,
7825 size_t locsymcount,
7826 size_t symidx,
7827 bfd_vma val)
7828 {
7829 struct elf_link_hash_entry **sym_hashes;
7830 struct elf_link_hash_entry *h;
7831 size_t extsymoff = locsymcount;
7832
7833 if (symidx < locsymcount)
7834 {
7835 Elf_Internal_Sym *sym;
7836
7837 sym = isymbuf + symidx;
7838 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7839 {
7840 /* It is a local symbol: move it to the
7841 "absolute" section and give it a value. */
7842 sym->st_shndx = SHN_ABS;
7843 sym->st_value = val;
7844 return;
7845 }
7846 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7847 extsymoff = 0;
7848 }
7849
7850 /* It is a global symbol: set its link type
7851 to "defined" and give it a value. */
7852
7853 sym_hashes = elf_sym_hashes (bfd_with_globals);
7854 h = sym_hashes [symidx - extsymoff];
7855 while (h->root.type == bfd_link_hash_indirect
7856 || h->root.type == bfd_link_hash_warning)
7857 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7858 h->root.type = bfd_link_hash_defined;
7859 h->root.u.def.value = val;
7860 h->root.u.def.section = bfd_abs_section_ptr;
7861 }
7862
7863 static bfd_boolean
7864 resolve_symbol (const char *name,
7865 bfd *input_bfd,
7866 struct elf_final_link_info *flinfo,
7867 bfd_vma *result,
7868 Elf_Internal_Sym *isymbuf,
7869 size_t locsymcount)
7870 {
7871 Elf_Internal_Sym *sym;
7872 struct bfd_link_hash_entry *global_entry;
7873 const char *candidate = NULL;
7874 Elf_Internal_Shdr *symtab_hdr;
7875 size_t i;
7876
7877 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7878
7879 for (i = 0; i < locsymcount; ++ i)
7880 {
7881 sym = isymbuf + i;
7882
7883 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7884 continue;
7885
7886 candidate = bfd_elf_string_from_elf_section (input_bfd,
7887 symtab_hdr->sh_link,
7888 sym->st_name);
7889 #ifdef DEBUG
7890 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7891 name, candidate, (unsigned long) sym->st_value);
7892 #endif
7893 if (candidate && strcmp (candidate, name) == 0)
7894 {
7895 asection *sec = flinfo->sections [i];
7896
7897 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7898 *result += sec->output_offset + sec->output_section->vma;
7899 #ifdef DEBUG
7900 printf ("Found symbol with value %8.8lx\n",
7901 (unsigned long) *result);
7902 #endif
7903 return TRUE;
7904 }
7905 }
7906
7907 /* Hmm, haven't found it yet. perhaps it is a global. */
7908 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
7909 FALSE, FALSE, TRUE);
7910 if (!global_entry)
7911 return FALSE;
7912
7913 if (global_entry->type == bfd_link_hash_defined
7914 || global_entry->type == bfd_link_hash_defweak)
7915 {
7916 *result = (global_entry->u.def.value
7917 + global_entry->u.def.section->output_section->vma
7918 + global_entry->u.def.section->output_offset);
7919 #ifdef DEBUG
7920 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7921 global_entry->root.string, (unsigned long) *result);
7922 #endif
7923 return TRUE;
7924 }
7925
7926 return FALSE;
7927 }
7928
7929 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
7930 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
7931 names like "foo.end" which is the end address of section "foo". */
7932
7933 static bfd_boolean
7934 resolve_section (const char *name,
7935 asection *sections,
7936 bfd_vma *result,
7937 bfd * abfd)
7938 {
7939 asection *curr;
7940 unsigned int len;
7941
7942 for (curr = sections; curr; curr = curr->next)
7943 if (strcmp (curr->name, name) == 0)
7944 {
7945 *result = curr->vma;
7946 return TRUE;
7947 }
7948
7949 /* Hmm. still haven't found it. try pseudo-section names. */
7950 /* FIXME: This could be coded more efficiently... */
7951 for (curr = sections; curr; curr = curr->next)
7952 {
7953 len = strlen (curr->name);
7954 if (len > strlen (name))
7955 continue;
7956
7957 if (strncmp (curr->name, name, len) == 0)
7958 {
7959 if (strncmp (".end", name + len, 4) == 0)
7960 {
7961 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
7962 return TRUE;
7963 }
7964
7965 /* Insert more pseudo-section names here, if you like. */
7966 }
7967 }
7968
7969 return FALSE;
7970 }
7971
7972 static void
7973 undefined_reference (const char *reftype, const char *name)
7974 {
7975 /* xgettext:c-format */
7976 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7977 reftype, name);
7978 }
7979
7980 static bfd_boolean
7981 eval_symbol (bfd_vma *result,
7982 const char **symp,
7983 bfd *input_bfd,
7984 struct elf_final_link_info *flinfo,
7985 bfd_vma dot,
7986 Elf_Internal_Sym *isymbuf,
7987 size_t locsymcount,
7988 int signed_p)
7989 {
7990 size_t len;
7991 size_t symlen;
7992 bfd_vma a;
7993 bfd_vma b;
7994 char symbuf[4096];
7995 const char *sym = *symp;
7996 const char *symend;
7997 bfd_boolean symbol_is_section = FALSE;
7998
7999 len = strlen (sym);
8000 symend = sym + len;
8001
8002 if (len < 1 || len > sizeof (symbuf))
8003 {
8004 bfd_set_error (bfd_error_invalid_operation);
8005 return FALSE;
8006 }
8007
8008 switch (* sym)
8009 {
8010 case '.':
8011 *result = dot;
8012 *symp = sym + 1;
8013 return TRUE;
8014
8015 case '#':
8016 ++sym;
8017 *result = strtoul (sym, (char **) symp, 16);
8018 return TRUE;
8019
8020 case 'S':
8021 symbol_is_section = TRUE;
8022 /* Fall through. */
8023 case 's':
8024 ++sym;
8025 symlen = strtol (sym, (char **) symp, 10);
8026 sym = *symp + 1; /* Skip the trailing ':'. */
8027
8028 if (symend < sym || symlen + 1 > sizeof (symbuf))
8029 {
8030 bfd_set_error (bfd_error_invalid_operation);
8031 return FALSE;
8032 }
8033
8034 memcpy (symbuf, sym, symlen);
8035 symbuf[symlen] = '\0';
8036 *symp = sym + symlen;
8037
8038 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8039 the symbol as a section, or vice-versa. so we're pretty liberal in our
8040 interpretation here; section means "try section first", not "must be a
8041 section", and likewise with symbol. */
8042
8043 if (symbol_is_section)
8044 {
8045 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8046 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8047 isymbuf, locsymcount))
8048 {
8049 undefined_reference ("section", symbuf);
8050 return FALSE;
8051 }
8052 }
8053 else
8054 {
8055 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8056 isymbuf, locsymcount)
8057 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8058 result, input_bfd))
8059 {
8060 undefined_reference ("symbol", symbuf);
8061 return FALSE;
8062 }
8063 }
8064
8065 return TRUE;
8066
8067 /* All that remains are operators. */
8068
8069 #define UNARY_OP(op) \
8070 if (strncmp (sym, #op, strlen (#op)) == 0) \
8071 { \
8072 sym += strlen (#op); \
8073 if (*sym == ':') \
8074 ++sym; \
8075 *symp = sym; \
8076 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8077 isymbuf, locsymcount, signed_p)) \
8078 return FALSE; \
8079 if (signed_p) \
8080 *result = op ((bfd_signed_vma) a); \
8081 else \
8082 *result = op a; \
8083 return TRUE; \
8084 }
8085
8086 #define BINARY_OP(op) \
8087 if (strncmp (sym, #op, strlen (#op)) == 0) \
8088 { \
8089 sym += strlen (#op); \
8090 if (*sym == ':') \
8091 ++sym; \
8092 *symp = sym; \
8093 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8094 isymbuf, locsymcount, signed_p)) \
8095 return FALSE; \
8096 ++*symp; \
8097 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8098 isymbuf, locsymcount, signed_p)) \
8099 return FALSE; \
8100 if (signed_p) \
8101 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8102 else \
8103 *result = a op b; \
8104 return TRUE; \
8105 }
8106
8107 default:
8108 UNARY_OP (0-);
8109 BINARY_OP (<<);
8110 BINARY_OP (>>);
8111 BINARY_OP (==);
8112 BINARY_OP (!=);
8113 BINARY_OP (<=);
8114 BINARY_OP (>=);
8115 BINARY_OP (&&);
8116 BINARY_OP (||);
8117 UNARY_OP (~);
8118 UNARY_OP (!);
8119 BINARY_OP (*);
8120 BINARY_OP (/);
8121 BINARY_OP (%);
8122 BINARY_OP (^);
8123 BINARY_OP (|);
8124 BINARY_OP (&);
8125 BINARY_OP (+);
8126 BINARY_OP (-);
8127 BINARY_OP (<);
8128 BINARY_OP (>);
8129 #undef UNARY_OP
8130 #undef BINARY_OP
8131 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8132 bfd_set_error (bfd_error_invalid_operation);
8133 return FALSE;
8134 }
8135 }
8136
8137 static void
8138 put_value (bfd_vma size,
8139 unsigned long chunksz,
8140 bfd *input_bfd,
8141 bfd_vma x,
8142 bfd_byte *location)
8143 {
8144 location += (size - chunksz);
8145
8146 for (; size; size -= chunksz, location -= chunksz)
8147 {
8148 switch (chunksz)
8149 {
8150 case 1:
8151 bfd_put_8 (input_bfd, x, location);
8152 x >>= 8;
8153 break;
8154 case 2:
8155 bfd_put_16 (input_bfd, x, location);
8156 x >>= 16;
8157 break;
8158 case 4:
8159 bfd_put_32 (input_bfd, x, location);
8160 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8161 x >>= 16;
8162 x >>= 16;
8163 break;
8164 #ifdef BFD64
8165 case 8:
8166 bfd_put_64 (input_bfd, x, location);
8167 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8168 x >>= 32;
8169 x >>= 32;
8170 break;
8171 #endif
8172 default:
8173 abort ();
8174 break;
8175 }
8176 }
8177 }
8178
8179 static bfd_vma
8180 get_value (bfd_vma size,
8181 unsigned long chunksz,
8182 bfd *input_bfd,
8183 bfd_byte *location)
8184 {
8185 int shift;
8186 bfd_vma x = 0;
8187
8188 /* Sanity checks. */
8189 BFD_ASSERT (chunksz <= sizeof (x)
8190 && size >= chunksz
8191 && chunksz != 0
8192 && (size % chunksz) == 0
8193 && input_bfd != NULL
8194 && location != NULL);
8195
8196 if (chunksz == sizeof (x))
8197 {
8198 BFD_ASSERT (size == chunksz);
8199
8200 /* Make sure that we do not perform an undefined shift operation.
8201 We know that size == chunksz so there will only be one iteration
8202 of the loop below. */
8203 shift = 0;
8204 }
8205 else
8206 shift = 8 * chunksz;
8207
8208 for (; size; size -= chunksz, location += chunksz)
8209 {
8210 switch (chunksz)
8211 {
8212 case 1:
8213 x = (x << shift) | bfd_get_8 (input_bfd, location);
8214 break;
8215 case 2:
8216 x = (x << shift) | bfd_get_16 (input_bfd, location);
8217 break;
8218 case 4:
8219 x = (x << shift) | bfd_get_32 (input_bfd, location);
8220 break;
8221 #ifdef BFD64
8222 case 8:
8223 x = (x << shift) | bfd_get_64 (input_bfd, location);
8224 break;
8225 #endif
8226 default:
8227 abort ();
8228 }
8229 }
8230 return x;
8231 }
8232
8233 static void
8234 decode_complex_addend (unsigned long *start, /* in bits */
8235 unsigned long *oplen, /* in bits */
8236 unsigned long *len, /* in bits */
8237 unsigned long *wordsz, /* in bytes */
8238 unsigned long *chunksz, /* in bytes */
8239 unsigned long *lsb0_p,
8240 unsigned long *signed_p,
8241 unsigned long *trunc_p,
8242 unsigned long encoded)
8243 {
8244 * start = encoded & 0x3F;
8245 * len = (encoded >> 6) & 0x3F;
8246 * oplen = (encoded >> 12) & 0x3F;
8247 * wordsz = (encoded >> 18) & 0xF;
8248 * chunksz = (encoded >> 22) & 0xF;
8249 * lsb0_p = (encoded >> 27) & 1;
8250 * signed_p = (encoded >> 28) & 1;
8251 * trunc_p = (encoded >> 29) & 1;
8252 }
8253
8254 bfd_reloc_status_type
8255 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8256 asection *input_section ATTRIBUTE_UNUSED,
8257 bfd_byte *contents,
8258 Elf_Internal_Rela *rel,
8259 bfd_vma relocation)
8260 {
8261 bfd_vma shift, x, mask;
8262 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8263 bfd_reloc_status_type r;
8264
8265 /* Perform this reloc, since it is complex.
8266 (this is not to say that it necessarily refers to a complex
8267 symbol; merely that it is a self-describing CGEN based reloc.
8268 i.e. the addend has the complete reloc information (bit start, end,
8269 word size, etc) encoded within it.). */
8270
8271 decode_complex_addend (&start, &oplen, &len, &wordsz,
8272 &chunksz, &lsb0_p, &signed_p,
8273 &trunc_p, rel->r_addend);
8274
8275 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8276
8277 if (lsb0_p)
8278 shift = (start + 1) - len;
8279 else
8280 shift = (8 * wordsz) - (start + len);
8281
8282 x = get_value (wordsz, chunksz, input_bfd,
8283 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8284
8285 #ifdef DEBUG
8286 printf ("Doing complex reloc: "
8287 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8288 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8289 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8290 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8291 oplen, (unsigned long) x, (unsigned long) mask,
8292 (unsigned long) relocation);
8293 #endif
8294
8295 r = bfd_reloc_ok;
8296 if (! trunc_p)
8297 /* Now do an overflow check. */
8298 r = bfd_check_overflow ((signed_p
8299 ? complain_overflow_signed
8300 : complain_overflow_unsigned),
8301 len, 0, (8 * wordsz),
8302 relocation);
8303
8304 /* Do the deed. */
8305 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8306
8307 #ifdef DEBUG
8308 printf (" relocation: %8.8lx\n"
8309 " shifted mask: %8.8lx\n"
8310 " shifted/masked reloc: %8.8lx\n"
8311 " result: %8.8lx\n",
8312 (unsigned long) relocation, (unsigned long) (mask << shift),
8313 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8314 #endif
8315 put_value (wordsz, chunksz, input_bfd, x,
8316 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8317 return r;
8318 }
8319
8320 /* Functions to read r_offset from external (target order) reloc
8321 entry. Faster than bfd_getl32 et al, because we let the compiler
8322 know the value is aligned. */
8323
8324 static bfd_vma
8325 ext32l_r_offset (const void *p)
8326 {
8327 union aligned32
8328 {
8329 uint32_t v;
8330 unsigned char c[4];
8331 };
8332 const union aligned32 *a
8333 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8334
8335 uint32_t aval = ( (uint32_t) a->c[0]
8336 | (uint32_t) a->c[1] << 8
8337 | (uint32_t) a->c[2] << 16
8338 | (uint32_t) a->c[3] << 24);
8339 return aval;
8340 }
8341
8342 static bfd_vma
8343 ext32b_r_offset (const void *p)
8344 {
8345 union aligned32
8346 {
8347 uint32_t v;
8348 unsigned char c[4];
8349 };
8350 const union aligned32 *a
8351 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8352
8353 uint32_t aval = ( (uint32_t) a->c[0] << 24
8354 | (uint32_t) a->c[1] << 16
8355 | (uint32_t) a->c[2] << 8
8356 | (uint32_t) a->c[3]);
8357 return aval;
8358 }
8359
8360 #ifdef BFD_HOST_64_BIT
8361 static bfd_vma
8362 ext64l_r_offset (const void *p)
8363 {
8364 union aligned64
8365 {
8366 uint64_t v;
8367 unsigned char c[8];
8368 };
8369 const union aligned64 *a
8370 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8371
8372 uint64_t aval = ( (uint64_t) a->c[0]
8373 | (uint64_t) a->c[1] << 8
8374 | (uint64_t) a->c[2] << 16
8375 | (uint64_t) a->c[3] << 24
8376 | (uint64_t) a->c[4] << 32
8377 | (uint64_t) a->c[5] << 40
8378 | (uint64_t) a->c[6] << 48
8379 | (uint64_t) a->c[7] << 56);
8380 return aval;
8381 }
8382
8383 static bfd_vma
8384 ext64b_r_offset (const void *p)
8385 {
8386 union aligned64
8387 {
8388 uint64_t v;
8389 unsigned char c[8];
8390 };
8391 const union aligned64 *a
8392 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8393
8394 uint64_t aval = ( (uint64_t) a->c[0] << 56
8395 | (uint64_t) a->c[1] << 48
8396 | (uint64_t) a->c[2] << 40
8397 | (uint64_t) a->c[3] << 32
8398 | (uint64_t) a->c[4] << 24
8399 | (uint64_t) a->c[5] << 16
8400 | (uint64_t) a->c[6] << 8
8401 | (uint64_t) a->c[7]);
8402 return aval;
8403 }
8404 #endif
8405
8406 /* When performing a relocatable link, the input relocations are
8407 preserved. But, if they reference global symbols, the indices
8408 referenced must be updated. Update all the relocations found in
8409 RELDATA. */
8410
8411 static bfd_boolean
8412 elf_link_adjust_relocs (bfd *abfd,
8413 asection *sec,
8414 struct bfd_elf_section_reloc_data *reldata,
8415 bfd_boolean sort)
8416 {
8417 unsigned int i;
8418 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8419 bfd_byte *erela;
8420 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8421 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8422 bfd_vma r_type_mask;
8423 int r_sym_shift;
8424 unsigned int count = reldata->count;
8425 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8426
8427 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8428 {
8429 swap_in = bed->s->swap_reloc_in;
8430 swap_out = bed->s->swap_reloc_out;
8431 }
8432 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8433 {
8434 swap_in = bed->s->swap_reloca_in;
8435 swap_out = bed->s->swap_reloca_out;
8436 }
8437 else
8438 abort ();
8439
8440 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8441 abort ();
8442
8443 if (bed->s->arch_size == 32)
8444 {
8445 r_type_mask = 0xff;
8446 r_sym_shift = 8;
8447 }
8448 else
8449 {
8450 r_type_mask = 0xffffffff;
8451 r_sym_shift = 32;
8452 }
8453
8454 erela = reldata->hdr->contents;
8455 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8456 {
8457 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8458 unsigned int j;
8459
8460 if (*rel_hash == NULL)
8461 continue;
8462
8463 BFD_ASSERT ((*rel_hash)->indx >= 0);
8464
8465 (*swap_in) (abfd, erela, irela);
8466 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8467 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8468 | (irela[j].r_info & r_type_mask));
8469 (*swap_out) (abfd, irela, erela);
8470 }
8471
8472 if (bed->elf_backend_update_relocs)
8473 (*bed->elf_backend_update_relocs) (sec, reldata);
8474
8475 if (sort && count != 0)
8476 {
8477 bfd_vma (*ext_r_off) (const void *);
8478 bfd_vma r_off;
8479 size_t elt_size;
8480 bfd_byte *base, *end, *p, *loc;
8481 bfd_byte *buf = NULL;
8482
8483 if (bed->s->arch_size == 32)
8484 {
8485 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8486 ext_r_off = ext32l_r_offset;
8487 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8488 ext_r_off = ext32b_r_offset;
8489 else
8490 abort ();
8491 }
8492 else
8493 {
8494 #ifdef BFD_HOST_64_BIT
8495 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8496 ext_r_off = ext64l_r_offset;
8497 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8498 ext_r_off = ext64b_r_offset;
8499 else
8500 #endif
8501 abort ();
8502 }
8503
8504 /* Must use a stable sort here. A modified insertion sort,
8505 since the relocs are mostly sorted already. */
8506 elt_size = reldata->hdr->sh_entsize;
8507 base = reldata->hdr->contents;
8508 end = base + count * elt_size;
8509 if (elt_size > sizeof (Elf64_External_Rela))
8510 abort ();
8511
8512 /* Ensure the first element is lowest. This acts as a sentinel,
8513 speeding the main loop below. */
8514 r_off = (*ext_r_off) (base);
8515 for (p = loc = base; (p += elt_size) < end; )
8516 {
8517 bfd_vma r_off2 = (*ext_r_off) (p);
8518 if (r_off > r_off2)
8519 {
8520 r_off = r_off2;
8521 loc = p;
8522 }
8523 }
8524 if (loc != base)
8525 {
8526 /* Don't just swap *base and *loc as that changes the order
8527 of the original base[0] and base[1] if they happen to
8528 have the same r_offset. */
8529 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8530 memcpy (onebuf, loc, elt_size);
8531 memmove (base + elt_size, base, loc - base);
8532 memcpy (base, onebuf, elt_size);
8533 }
8534
8535 for (p = base + elt_size; (p += elt_size) < end; )
8536 {
8537 /* base to p is sorted, *p is next to insert. */
8538 r_off = (*ext_r_off) (p);
8539 /* Search the sorted region for location to insert. */
8540 loc = p - elt_size;
8541 while (r_off < (*ext_r_off) (loc))
8542 loc -= elt_size;
8543 loc += elt_size;
8544 if (loc != p)
8545 {
8546 /* Chances are there is a run of relocs to insert here,
8547 from one of more input files. Files are not always
8548 linked in order due to the way elf_link_input_bfd is
8549 called. See pr17666. */
8550 size_t sortlen = p - loc;
8551 bfd_vma r_off2 = (*ext_r_off) (loc);
8552 size_t runlen = elt_size;
8553 size_t buf_size = 96 * 1024;
8554 while (p + runlen < end
8555 && (sortlen <= buf_size
8556 || runlen + elt_size <= buf_size)
8557 && r_off2 > (*ext_r_off) (p + runlen))
8558 runlen += elt_size;
8559 if (buf == NULL)
8560 {
8561 buf = bfd_malloc (buf_size);
8562 if (buf == NULL)
8563 return FALSE;
8564 }
8565 if (runlen < sortlen)
8566 {
8567 memcpy (buf, p, runlen);
8568 memmove (loc + runlen, loc, sortlen);
8569 memcpy (loc, buf, runlen);
8570 }
8571 else
8572 {
8573 memcpy (buf, loc, sortlen);
8574 memmove (loc, p, runlen);
8575 memcpy (loc + runlen, buf, sortlen);
8576 }
8577 p += runlen - elt_size;
8578 }
8579 }
8580 /* Hashes are no longer valid. */
8581 free (reldata->hashes);
8582 reldata->hashes = NULL;
8583 free (buf);
8584 }
8585 return TRUE;
8586 }
8587
8588 struct elf_link_sort_rela
8589 {
8590 union {
8591 bfd_vma offset;
8592 bfd_vma sym_mask;
8593 } u;
8594 enum elf_reloc_type_class type;
8595 /* We use this as an array of size int_rels_per_ext_rel. */
8596 Elf_Internal_Rela rela[1];
8597 };
8598
8599 static int
8600 elf_link_sort_cmp1 (const void *A, const void *B)
8601 {
8602 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8603 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8604 int relativea, relativeb;
8605
8606 relativea = a->type == reloc_class_relative;
8607 relativeb = b->type == reloc_class_relative;
8608
8609 if (relativea < relativeb)
8610 return 1;
8611 if (relativea > relativeb)
8612 return -1;
8613 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8614 return -1;
8615 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8616 return 1;
8617 if (a->rela->r_offset < b->rela->r_offset)
8618 return -1;
8619 if (a->rela->r_offset > b->rela->r_offset)
8620 return 1;
8621 return 0;
8622 }
8623
8624 static int
8625 elf_link_sort_cmp2 (const void *A, const void *B)
8626 {
8627 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8628 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8629
8630 if (a->type < b->type)
8631 return -1;
8632 if (a->type > b->type)
8633 return 1;
8634 if (a->u.offset < b->u.offset)
8635 return -1;
8636 if (a->u.offset > b->u.offset)
8637 return 1;
8638 if (a->rela->r_offset < b->rela->r_offset)
8639 return -1;
8640 if (a->rela->r_offset > b->rela->r_offset)
8641 return 1;
8642 return 0;
8643 }
8644
8645 static size_t
8646 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8647 {
8648 asection *dynamic_relocs;
8649 asection *rela_dyn;
8650 asection *rel_dyn;
8651 bfd_size_type count, size;
8652 size_t i, ret, sort_elt, ext_size;
8653 bfd_byte *sort, *s_non_relative, *p;
8654 struct elf_link_sort_rela *sq;
8655 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8656 int i2e = bed->s->int_rels_per_ext_rel;
8657 unsigned int opb = bfd_octets_per_byte (abfd);
8658 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8659 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8660 struct bfd_link_order *lo;
8661 bfd_vma r_sym_mask;
8662 bfd_boolean use_rela;
8663
8664 /* Find a dynamic reloc section. */
8665 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8666 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8667 if (rela_dyn != NULL && rela_dyn->size > 0
8668 && rel_dyn != NULL && rel_dyn->size > 0)
8669 {
8670 bfd_boolean use_rela_initialised = FALSE;
8671
8672 /* This is just here to stop gcc from complaining.
8673 Its initialization checking code is not perfect. */
8674 use_rela = TRUE;
8675
8676 /* Both sections are present. Examine the sizes
8677 of the indirect sections to help us choose. */
8678 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8679 if (lo->type == bfd_indirect_link_order)
8680 {
8681 asection *o = lo->u.indirect.section;
8682
8683 if ((o->size % bed->s->sizeof_rela) == 0)
8684 {
8685 if ((o->size % bed->s->sizeof_rel) == 0)
8686 /* Section size is divisible by both rel and rela sizes.
8687 It is of no help to us. */
8688 ;
8689 else
8690 {
8691 /* Section size is only divisible by rela. */
8692 if (use_rela_initialised && (use_rela == FALSE))
8693 {
8694 _bfd_error_handler (_("%B: Unable to sort relocs - "
8695 "they are in more than one size"),
8696 abfd);
8697 bfd_set_error (bfd_error_invalid_operation);
8698 return 0;
8699 }
8700 else
8701 {
8702 use_rela = TRUE;
8703 use_rela_initialised = TRUE;
8704 }
8705 }
8706 }
8707 else if ((o->size % bed->s->sizeof_rel) == 0)
8708 {
8709 /* Section size is only divisible by rel. */
8710 if (use_rela_initialised && (use_rela == TRUE))
8711 {
8712 _bfd_error_handler (_("%B: Unable to sort relocs - "
8713 "they are in more than one size"),
8714 abfd);
8715 bfd_set_error (bfd_error_invalid_operation);
8716 return 0;
8717 }
8718 else
8719 {
8720 use_rela = FALSE;
8721 use_rela_initialised = TRUE;
8722 }
8723 }
8724 else
8725 {
8726 /* The section size is not divisible by either -
8727 something is wrong. */
8728 _bfd_error_handler (_("%B: Unable to sort relocs - "
8729 "they are of an unknown size"), abfd);
8730 bfd_set_error (bfd_error_invalid_operation);
8731 return 0;
8732 }
8733 }
8734
8735 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8736 if (lo->type == bfd_indirect_link_order)
8737 {
8738 asection *o = lo->u.indirect.section;
8739
8740 if ((o->size % bed->s->sizeof_rela) == 0)
8741 {
8742 if ((o->size % bed->s->sizeof_rel) == 0)
8743 /* Section size is divisible by both rel and rela sizes.
8744 It is of no help to us. */
8745 ;
8746 else
8747 {
8748 /* Section size is only divisible by rela. */
8749 if (use_rela_initialised && (use_rela == FALSE))
8750 {
8751 _bfd_error_handler (_("%B: Unable to sort relocs - "
8752 "they are in more than one size"),
8753 abfd);
8754 bfd_set_error (bfd_error_invalid_operation);
8755 return 0;
8756 }
8757 else
8758 {
8759 use_rela = TRUE;
8760 use_rela_initialised = TRUE;
8761 }
8762 }
8763 }
8764 else if ((o->size % bed->s->sizeof_rel) == 0)
8765 {
8766 /* Section size is only divisible by rel. */
8767 if (use_rela_initialised && (use_rela == TRUE))
8768 {
8769 _bfd_error_handler (_("%B: Unable to sort relocs - "
8770 "they are in more than one size"),
8771 abfd);
8772 bfd_set_error (bfd_error_invalid_operation);
8773 return 0;
8774 }
8775 else
8776 {
8777 use_rela = FALSE;
8778 use_rela_initialised = TRUE;
8779 }
8780 }
8781 else
8782 {
8783 /* The section size is not divisible by either -
8784 something is wrong. */
8785 _bfd_error_handler (_("%B: Unable to sort relocs - "
8786 "they are of an unknown size"), abfd);
8787 bfd_set_error (bfd_error_invalid_operation);
8788 return 0;
8789 }
8790 }
8791
8792 if (! use_rela_initialised)
8793 /* Make a guess. */
8794 use_rela = TRUE;
8795 }
8796 else if (rela_dyn != NULL && rela_dyn->size > 0)
8797 use_rela = TRUE;
8798 else if (rel_dyn != NULL && rel_dyn->size > 0)
8799 use_rela = FALSE;
8800 else
8801 return 0;
8802
8803 if (use_rela)
8804 {
8805 dynamic_relocs = rela_dyn;
8806 ext_size = bed->s->sizeof_rela;
8807 swap_in = bed->s->swap_reloca_in;
8808 swap_out = bed->s->swap_reloca_out;
8809 }
8810 else
8811 {
8812 dynamic_relocs = rel_dyn;
8813 ext_size = bed->s->sizeof_rel;
8814 swap_in = bed->s->swap_reloc_in;
8815 swap_out = bed->s->swap_reloc_out;
8816 }
8817
8818 size = 0;
8819 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8820 if (lo->type == bfd_indirect_link_order)
8821 size += lo->u.indirect.section->size;
8822
8823 if (size != dynamic_relocs->size)
8824 return 0;
8825
8826 sort_elt = (sizeof (struct elf_link_sort_rela)
8827 + (i2e - 1) * sizeof (Elf_Internal_Rela));
8828
8829 count = dynamic_relocs->size / ext_size;
8830 if (count == 0)
8831 return 0;
8832 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
8833
8834 if (sort == NULL)
8835 {
8836 (*info->callbacks->warning)
8837 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8838 return 0;
8839 }
8840
8841 if (bed->s->arch_size == 32)
8842 r_sym_mask = ~(bfd_vma) 0xff;
8843 else
8844 r_sym_mask = ~(bfd_vma) 0xffffffff;
8845
8846 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8847 if (lo->type == bfd_indirect_link_order)
8848 {
8849 bfd_byte *erel, *erelend;
8850 asection *o = lo->u.indirect.section;
8851
8852 if (o->contents == NULL && o->size != 0)
8853 {
8854 /* This is a reloc section that is being handled as a normal
8855 section. See bfd_section_from_shdr. We can't combine
8856 relocs in this case. */
8857 free (sort);
8858 return 0;
8859 }
8860 erel = o->contents;
8861 erelend = o->contents + o->size;
8862 p = sort + o->output_offset * opb / ext_size * sort_elt;
8863
8864 while (erel < erelend)
8865 {
8866 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8867
8868 (*swap_in) (abfd, erel, s->rela);
8869 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
8870 s->u.sym_mask = r_sym_mask;
8871 p += sort_elt;
8872 erel += ext_size;
8873 }
8874 }
8875
8876 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8877
8878 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8879 {
8880 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8881 if (s->type != reloc_class_relative)
8882 break;
8883 }
8884 ret = i;
8885 s_non_relative = p;
8886
8887 sq = (struct elf_link_sort_rela *) s_non_relative;
8888 for (; i < count; i++, p += sort_elt)
8889 {
8890 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8891 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8892 sq = sp;
8893 sp->u.offset = sq->rela->r_offset;
8894 }
8895
8896 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8897
8898 struct elf_link_hash_table *htab = elf_hash_table (info);
8899 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
8900 {
8901 /* We have plt relocs in .rela.dyn. */
8902 sq = (struct elf_link_sort_rela *) sort;
8903 for (i = 0; i < count; i++)
8904 if (sq[count - i - 1].type != reloc_class_plt)
8905 break;
8906 if (i != 0 && htab->srelplt->size == i * ext_size)
8907 {
8908 struct bfd_link_order **plo;
8909 /* Put srelplt link_order last. This is so the output_offset
8910 set in the next loop is correct for DT_JMPREL. */
8911 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
8912 if ((*plo)->type == bfd_indirect_link_order
8913 && (*plo)->u.indirect.section == htab->srelplt)
8914 {
8915 lo = *plo;
8916 *plo = lo->next;
8917 }
8918 else
8919 plo = &(*plo)->next;
8920 *plo = lo;
8921 lo->next = NULL;
8922 dynamic_relocs->map_tail.link_order = lo;
8923 }
8924 }
8925
8926 p = sort;
8927 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8928 if (lo->type == bfd_indirect_link_order)
8929 {
8930 bfd_byte *erel, *erelend;
8931 asection *o = lo->u.indirect.section;
8932
8933 erel = o->contents;
8934 erelend = o->contents + o->size;
8935 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
8936 while (erel < erelend)
8937 {
8938 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8939 (*swap_out) (abfd, s->rela, erel);
8940 p += sort_elt;
8941 erel += ext_size;
8942 }
8943 }
8944
8945 free (sort);
8946 *psec = dynamic_relocs;
8947 return ret;
8948 }
8949
8950 /* Add a symbol to the output symbol string table. */
8951
8952 static int
8953 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
8954 const char *name,
8955 Elf_Internal_Sym *elfsym,
8956 asection *input_sec,
8957 struct elf_link_hash_entry *h)
8958 {
8959 int (*output_symbol_hook)
8960 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8961 struct elf_link_hash_entry *);
8962 struct elf_link_hash_table *hash_table;
8963 const struct elf_backend_data *bed;
8964 bfd_size_type strtabsize;
8965
8966 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8967
8968 bed = get_elf_backend_data (flinfo->output_bfd);
8969 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8970 if (output_symbol_hook != NULL)
8971 {
8972 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
8973 if (ret != 1)
8974 return ret;
8975 }
8976
8977 if (name == NULL
8978 || *name == '\0'
8979 || (input_sec->flags & SEC_EXCLUDE))
8980 elfsym->st_name = (unsigned long) -1;
8981 else
8982 {
8983 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8984 to get the final offset for st_name. */
8985 elfsym->st_name
8986 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
8987 name, FALSE);
8988 if (elfsym->st_name == (unsigned long) -1)
8989 return 0;
8990 }
8991
8992 hash_table = elf_hash_table (flinfo->info);
8993 strtabsize = hash_table->strtabsize;
8994 if (strtabsize <= hash_table->strtabcount)
8995 {
8996 strtabsize += strtabsize;
8997 hash_table->strtabsize = strtabsize;
8998 strtabsize *= sizeof (*hash_table->strtab);
8999 hash_table->strtab
9000 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9001 strtabsize);
9002 if (hash_table->strtab == NULL)
9003 return 0;
9004 }
9005 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9006 hash_table->strtab[hash_table->strtabcount].dest_index
9007 = hash_table->strtabcount;
9008 hash_table->strtab[hash_table->strtabcount].destshndx_index
9009 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9010
9011 bfd_get_symcount (flinfo->output_bfd) += 1;
9012 hash_table->strtabcount += 1;
9013
9014 return 1;
9015 }
9016
9017 /* Swap symbols out to the symbol table and flush the output symbols to
9018 the file. */
9019
9020 static bfd_boolean
9021 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9022 {
9023 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9024 bfd_size_type amt;
9025 size_t i;
9026 const struct elf_backend_data *bed;
9027 bfd_byte *symbuf;
9028 Elf_Internal_Shdr *hdr;
9029 file_ptr pos;
9030 bfd_boolean ret;
9031
9032 if (!hash_table->strtabcount)
9033 return TRUE;
9034
9035 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9036
9037 bed = get_elf_backend_data (flinfo->output_bfd);
9038
9039 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9040 symbuf = (bfd_byte *) bfd_malloc (amt);
9041 if (symbuf == NULL)
9042 return FALSE;
9043
9044 if (flinfo->symshndxbuf)
9045 {
9046 amt = sizeof (Elf_External_Sym_Shndx);
9047 amt *= bfd_get_symcount (flinfo->output_bfd);
9048 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9049 if (flinfo->symshndxbuf == NULL)
9050 {
9051 free (symbuf);
9052 return FALSE;
9053 }
9054 }
9055
9056 for (i = 0; i < hash_table->strtabcount; i++)
9057 {
9058 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9059 if (elfsym->sym.st_name == (unsigned long) -1)
9060 elfsym->sym.st_name = 0;
9061 else
9062 elfsym->sym.st_name
9063 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9064 elfsym->sym.st_name);
9065 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9066 ((bfd_byte *) symbuf
9067 + (elfsym->dest_index
9068 * bed->s->sizeof_sym)),
9069 (flinfo->symshndxbuf
9070 + elfsym->destshndx_index));
9071 }
9072
9073 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9074 pos = hdr->sh_offset + hdr->sh_size;
9075 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9076 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9077 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9078 {
9079 hdr->sh_size += amt;
9080 ret = TRUE;
9081 }
9082 else
9083 ret = FALSE;
9084
9085 free (symbuf);
9086
9087 free (hash_table->strtab);
9088 hash_table->strtab = NULL;
9089
9090 return ret;
9091 }
9092
9093 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9094
9095 static bfd_boolean
9096 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9097 {
9098 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9099 && sym->st_shndx < SHN_LORESERVE)
9100 {
9101 /* The gABI doesn't support dynamic symbols in output sections
9102 beyond 64k. */
9103 _bfd_error_handler
9104 /* xgettext:c-format */
9105 (_("%B: Too many sections: %d (>= %d)"),
9106 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9107 bfd_set_error (bfd_error_nonrepresentable_section);
9108 return FALSE;
9109 }
9110 return TRUE;
9111 }
9112
9113 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9114 allowing an unsatisfied unversioned symbol in the DSO to match a
9115 versioned symbol that would normally require an explicit version.
9116 We also handle the case that a DSO references a hidden symbol
9117 which may be satisfied by a versioned symbol in another DSO. */
9118
9119 static bfd_boolean
9120 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9121 const struct elf_backend_data *bed,
9122 struct elf_link_hash_entry *h)
9123 {
9124 bfd *abfd;
9125 struct elf_link_loaded_list *loaded;
9126
9127 if (!is_elf_hash_table (info->hash))
9128 return FALSE;
9129
9130 /* Check indirect symbol. */
9131 while (h->root.type == bfd_link_hash_indirect)
9132 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9133
9134 switch (h->root.type)
9135 {
9136 default:
9137 abfd = NULL;
9138 break;
9139
9140 case bfd_link_hash_undefined:
9141 case bfd_link_hash_undefweak:
9142 abfd = h->root.u.undef.abfd;
9143 if (abfd == NULL
9144 || (abfd->flags & DYNAMIC) == 0
9145 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9146 return FALSE;
9147 break;
9148
9149 case bfd_link_hash_defined:
9150 case bfd_link_hash_defweak:
9151 abfd = h->root.u.def.section->owner;
9152 break;
9153
9154 case bfd_link_hash_common:
9155 abfd = h->root.u.c.p->section->owner;
9156 break;
9157 }
9158 BFD_ASSERT (abfd != NULL);
9159
9160 for (loaded = elf_hash_table (info)->loaded;
9161 loaded != NULL;
9162 loaded = loaded->next)
9163 {
9164 bfd *input;
9165 Elf_Internal_Shdr *hdr;
9166 size_t symcount;
9167 size_t extsymcount;
9168 size_t extsymoff;
9169 Elf_Internal_Shdr *versymhdr;
9170 Elf_Internal_Sym *isym;
9171 Elf_Internal_Sym *isymend;
9172 Elf_Internal_Sym *isymbuf;
9173 Elf_External_Versym *ever;
9174 Elf_External_Versym *extversym;
9175
9176 input = loaded->abfd;
9177
9178 /* We check each DSO for a possible hidden versioned definition. */
9179 if (input == abfd
9180 || (input->flags & DYNAMIC) == 0
9181 || elf_dynversym (input) == 0)
9182 continue;
9183
9184 hdr = &elf_tdata (input)->dynsymtab_hdr;
9185
9186 symcount = hdr->sh_size / bed->s->sizeof_sym;
9187 if (elf_bad_symtab (input))
9188 {
9189 extsymcount = symcount;
9190 extsymoff = 0;
9191 }
9192 else
9193 {
9194 extsymcount = symcount - hdr->sh_info;
9195 extsymoff = hdr->sh_info;
9196 }
9197
9198 if (extsymcount == 0)
9199 continue;
9200
9201 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9202 NULL, NULL, NULL);
9203 if (isymbuf == NULL)
9204 return FALSE;
9205
9206 /* Read in any version definitions. */
9207 versymhdr = &elf_tdata (input)->dynversym_hdr;
9208 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
9209 if (extversym == NULL)
9210 goto error_ret;
9211
9212 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9213 || (bfd_bread (extversym, versymhdr->sh_size, input)
9214 != versymhdr->sh_size))
9215 {
9216 free (extversym);
9217 error_ret:
9218 free (isymbuf);
9219 return FALSE;
9220 }
9221
9222 ever = extversym + extsymoff;
9223 isymend = isymbuf + extsymcount;
9224 for (isym = isymbuf; isym < isymend; isym++, ever++)
9225 {
9226 const char *name;
9227 Elf_Internal_Versym iver;
9228 unsigned short version_index;
9229
9230 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9231 || isym->st_shndx == SHN_UNDEF)
9232 continue;
9233
9234 name = bfd_elf_string_from_elf_section (input,
9235 hdr->sh_link,
9236 isym->st_name);
9237 if (strcmp (name, h->root.root.string) != 0)
9238 continue;
9239
9240 _bfd_elf_swap_versym_in (input, ever, &iver);
9241
9242 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9243 && !(h->def_regular
9244 && h->forced_local))
9245 {
9246 /* If we have a non-hidden versioned sym, then it should
9247 have provided a definition for the undefined sym unless
9248 it is defined in a non-shared object and forced local.
9249 */
9250 abort ();
9251 }
9252
9253 version_index = iver.vs_vers & VERSYM_VERSION;
9254 if (version_index == 1 || version_index == 2)
9255 {
9256 /* This is the base or first version. We can use it. */
9257 free (extversym);
9258 free (isymbuf);
9259 return TRUE;
9260 }
9261 }
9262
9263 free (extversym);
9264 free (isymbuf);
9265 }
9266
9267 return FALSE;
9268 }
9269
9270 /* Convert ELF common symbol TYPE. */
9271
9272 static int
9273 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9274 {
9275 /* Commom symbol can only appear in relocatable link. */
9276 if (!bfd_link_relocatable (info))
9277 abort ();
9278 switch (info->elf_stt_common)
9279 {
9280 case unchanged:
9281 break;
9282 case elf_stt_common:
9283 type = STT_COMMON;
9284 break;
9285 case no_elf_stt_common:
9286 type = STT_OBJECT;
9287 break;
9288 }
9289 return type;
9290 }
9291
9292 /* Add an external symbol to the symbol table. This is called from
9293 the hash table traversal routine. When generating a shared object,
9294 we go through the symbol table twice. The first time we output
9295 anything that might have been forced to local scope in a version
9296 script. The second time we output the symbols that are still
9297 global symbols. */
9298
9299 static bfd_boolean
9300 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9301 {
9302 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9303 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9304 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9305 bfd_boolean strip;
9306 Elf_Internal_Sym sym;
9307 asection *input_sec;
9308 const struct elf_backend_data *bed;
9309 long indx;
9310 int ret;
9311 unsigned int type;
9312
9313 if (h->root.type == bfd_link_hash_warning)
9314 {
9315 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9316 if (h->root.type == bfd_link_hash_new)
9317 return TRUE;
9318 }
9319
9320 /* Decide whether to output this symbol in this pass. */
9321 if (eoinfo->localsyms)
9322 {
9323 if (!h->forced_local)
9324 return TRUE;
9325 }
9326 else
9327 {
9328 if (h->forced_local)
9329 return TRUE;
9330 }
9331
9332 bed = get_elf_backend_data (flinfo->output_bfd);
9333
9334 if (h->root.type == bfd_link_hash_undefined)
9335 {
9336 /* If we have an undefined symbol reference here then it must have
9337 come from a shared library that is being linked in. (Undefined
9338 references in regular files have already been handled unless
9339 they are in unreferenced sections which are removed by garbage
9340 collection). */
9341 bfd_boolean ignore_undef = FALSE;
9342
9343 /* Some symbols may be special in that the fact that they're
9344 undefined can be safely ignored - let backend determine that. */
9345 if (bed->elf_backend_ignore_undef_symbol)
9346 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9347
9348 /* If we are reporting errors for this situation then do so now. */
9349 if (!ignore_undef
9350 && h->ref_dynamic
9351 && (!h->ref_regular || flinfo->info->gc_sections)
9352 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9353 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9354 (*flinfo->info->callbacks->undefined_symbol)
9355 (flinfo->info, h->root.root.string,
9356 h->ref_regular ? NULL : h->root.u.undef.abfd,
9357 NULL, 0,
9358 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
9359
9360 /* Strip a global symbol defined in a discarded section. */
9361 if (h->indx == -3)
9362 return TRUE;
9363 }
9364
9365 /* We should also warn if a forced local symbol is referenced from
9366 shared libraries. */
9367 if (bfd_link_executable (flinfo->info)
9368 && h->forced_local
9369 && h->ref_dynamic
9370 && h->def_regular
9371 && !h->dynamic_def
9372 && h->ref_dynamic_nonweak
9373 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9374 {
9375 bfd *def_bfd;
9376 const char *msg;
9377 struct elf_link_hash_entry *hi = h;
9378
9379 /* Check indirect symbol. */
9380 while (hi->root.type == bfd_link_hash_indirect)
9381 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9382
9383 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9384 /* xgettext:c-format */
9385 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9386 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9387 /* xgettext:c-format */
9388 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9389 else
9390 /* xgettext:c-format */
9391 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
9392 def_bfd = flinfo->output_bfd;
9393 if (hi->root.u.def.section != bfd_abs_section_ptr)
9394 def_bfd = hi->root.u.def.section->owner;
9395 _bfd_error_handler (msg, flinfo->output_bfd, def_bfd,
9396 h->root.root.string);
9397 bfd_set_error (bfd_error_bad_value);
9398 eoinfo->failed = TRUE;
9399 return FALSE;
9400 }
9401
9402 /* We don't want to output symbols that have never been mentioned by
9403 a regular file, or that we have been told to strip. However, if
9404 h->indx is set to -2, the symbol is used by a reloc and we must
9405 output it. */
9406 strip = FALSE;
9407 if (h->indx == -2)
9408 ;
9409 else if ((h->def_dynamic
9410 || h->ref_dynamic
9411 || h->root.type == bfd_link_hash_new)
9412 && !h->def_regular
9413 && !h->ref_regular)
9414 strip = TRUE;
9415 else if (flinfo->info->strip == strip_all)
9416 strip = TRUE;
9417 else if (flinfo->info->strip == strip_some
9418 && bfd_hash_lookup (flinfo->info->keep_hash,
9419 h->root.root.string, FALSE, FALSE) == NULL)
9420 strip = TRUE;
9421 else if ((h->root.type == bfd_link_hash_defined
9422 || h->root.type == bfd_link_hash_defweak)
9423 && ((flinfo->info->strip_discarded
9424 && discarded_section (h->root.u.def.section))
9425 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9426 && h->root.u.def.section->owner != NULL
9427 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9428 strip = TRUE;
9429 else if ((h->root.type == bfd_link_hash_undefined
9430 || h->root.type == bfd_link_hash_undefweak)
9431 && h->root.u.undef.abfd != NULL
9432 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9433 strip = TRUE;
9434
9435 type = h->type;
9436
9437 /* If we're stripping it, and it's not a dynamic symbol, there's
9438 nothing else to do. However, if it is a forced local symbol or
9439 an ifunc symbol we need to give the backend finish_dynamic_symbol
9440 function a chance to make it dynamic. */
9441 if (strip
9442 && h->dynindx == -1
9443 && type != STT_GNU_IFUNC
9444 && !h->forced_local)
9445 return TRUE;
9446
9447 sym.st_value = 0;
9448 sym.st_size = h->size;
9449 sym.st_other = h->other;
9450 switch (h->root.type)
9451 {
9452 default:
9453 case bfd_link_hash_new:
9454 case bfd_link_hash_warning:
9455 abort ();
9456 return FALSE;
9457
9458 case bfd_link_hash_undefined:
9459 case bfd_link_hash_undefweak:
9460 input_sec = bfd_und_section_ptr;
9461 sym.st_shndx = SHN_UNDEF;
9462 break;
9463
9464 case bfd_link_hash_defined:
9465 case bfd_link_hash_defweak:
9466 {
9467 input_sec = h->root.u.def.section;
9468 if (input_sec->output_section != NULL)
9469 {
9470 sym.st_shndx =
9471 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9472 input_sec->output_section);
9473 if (sym.st_shndx == SHN_BAD)
9474 {
9475 _bfd_error_handler
9476 /* xgettext:c-format */
9477 (_("%B: could not find output section %A for input section %A"),
9478 flinfo->output_bfd, input_sec->output_section, input_sec);
9479 bfd_set_error (bfd_error_nonrepresentable_section);
9480 eoinfo->failed = TRUE;
9481 return FALSE;
9482 }
9483
9484 /* ELF symbols in relocatable files are section relative,
9485 but in nonrelocatable files they are virtual
9486 addresses. */
9487 sym.st_value = h->root.u.def.value + input_sec->output_offset;
9488 if (!bfd_link_relocatable (flinfo->info))
9489 {
9490 sym.st_value += input_sec->output_section->vma;
9491 if (h->type == STT_TLS)
9492 {
9493 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9494 if (tls_sec != NULL)
9495 sym.st_value -= tls_sec->vma;
9496 }
9497 }
9498 }
9499 else
9500 {
9501 BFD_ASSERT (input_sec->owner == NULL
9502 || (input_sec->owner->flags & DYNAMIC) != 0);
9503 sym.st_shndx = SHN_UNDEF;
9504 input_sec = bfd_und_section_ptr;
9505 }
9506 }
9507 break;
9508
9509 case bfd_link_hash_common:
9510 input_sec = h->root.u.c.p->section;
9511 sym.st_shndx = bed->common_section_index (input_sec);
9512 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9513 break;
9514
9515 case bfd_link_hash_indirect:
9516 /* These symbols are created by symbol versioning. They point
9517 to the decorated version of the name. For example, if the
9518 symbol foo@@GNU_1.2 is the default, which should be used when
9519 foo is used with no version, then we add an indirect symbol
9520 foo which points to foo@@GNU_1.2. We ignore these symbols,
9521 since the indirected symbol is already in the hash table. */
9522 return TRUE;
9523 }
9524
9525 if (type == STT_COMMON || type == STT_OBJECT)
9526 switch (h->root.type)
9527 {
9528 case bfd_link_hash_common:
9529 type = elf_link_convert_common_type (flinfo->info, type);
9530 break;
9531 case bfd_link_hash_defined:
9532 case bfd_link_hash_defweak:
9533 if (bed->common_definition (&sym))
9534 type = elf_link_convert_common_type (flinfo->info, type);
9535 else
9536 type = STT_OBJECT;
9537 break;
9538 case bfd_link_hash_undefined:
9539 case bfd_link_hash_undefweak:
9540 break;
9541 default:
9542 abort ();
9543 }
9544
9545 if (h->forced_local)
9546 {
9547 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9548 /* Turn off visibility on local symbol. */
9549 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9550 }
9551 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9552 else if (h->unique_global && h->def_regular)
9553 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9554 else if (h->root.type == bfd_link_hash_undefweak
9555 || h->root.type == bfd_link_hash_defweak)
9556 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9557 else
9558 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9559 sym.st_target_internal = h->target_internal;
9560
9561 /* Give the processor backend a chance to tweak the symbol value,
9562 and also to finish up anything that needs to be done for this
9563 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9564 forced local syms when non-shared is due to a historical quirk.
9565 STT_GNU_IFUNC symbol must go through PLT. */
9566 if ((h->type == STT_GNU_IFUNC
9567 && h->def_regular
9568 && !bfd_link_relocatable (flinfo->info))
9569 || ((h->dynindx != -1
9570 || h->forced_local)
9571 && ((bfd_link_pic (flinfo->info)
9572 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9573 || h->root.type != bfd_link_hash_undefweak))
9574 || !h->forced_local)
9575 && elf_hash_table (flinfo->info)->dynamic_sections_created))
9576 {
9577 if (! ((*bed->elf_backend_finish_dynamic_symbol)
9578 (flinfo->output_bfd, flinfo->info, h, &sym)))
9579 {
9580 eoinfo->failed = TRUE;
9581 return FALSE;
9582 }
9583 }
9584
9585 /* If we are marking the symbol as undefined, and there are no
9586 non-weak references to this symbol from a regular object, then
9587 mark the symbol as weak undefined; if there are non-weak
9588 references, mark the symbol as strong. We can't do this earlier,
9589 because it might not be marked as undefined until the
9590 finish_dynamic_symbol routine gets through with it. */
9591 if (sym.st_shndx == SHN_UNDEF
9592 && h->ref_regular
9593 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9594 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9595 {
9596 int bindtype;
9597 type = ELF_ST_TYPE (sym.st_info);
9598
9599 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9600 if (type == STT_GNU_IFUNC)
9601 type = STT_FUNC;
9602
9603 if (h->ref_regular_nonweak)
9604 bindtype = STB_GLOBAL;
9605 else
9606 bindtype = STB_WEAK;
9607 sym.st_info = ELF_ST_INFO (bindtype, type);
9608 }
9609
9610 /* If this is a symbol defined in a dynamic library, don't use the
9611 symbol size from the dynamic library. Relinking an executable
9612 against a new library may introduce gratuitous changes in the
9613 executable's symbols if we keep the size. */
9614 if (sym.st_shndx == SHN_UNDEF
9615 && !h->def_regular
9616 && h->def_dynamic)
9617 sym.st_size = 0;
9618
9619 /* If a non-weak symbol with non-default visibility is not defined
9620 locally, it is a fatal error. */
9621 if (!bfd_link_relocatable (flinfo->info)
9622 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9623 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9624 && h->root.type == bfd_link_hash_undefined
9625 && !h->def_regular)
9626 {
9627 const char *msg;
9628
9629 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9630 /* xgettext:c-format */
9631 msg = _("%B: protected symbol `%s' isn't defined");
9632 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9633 /* xgettext:c-format */
9634 msg = _("%B: internal symbol `%s' isn't defined");
9635 else
9636 /* xgettext:c-format */
9637 msg = _("%B: hidden symbol `%s' isn't defined");
9638 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
9639 bfd_set_error (bfd_error_bad_value);
9640 eoinfo->failed = TRUE;
9641 return FALSE;
9642 }
9643
9644 /* If this symbol should be put in the .dynsym section, then put it
9645 there now. We already know the symbol index. We also fill in
9646 the entry in the .hash section. */
9647 if (elf_hash_table (flinfo->info)->dynsym != NULL
9648 && h->dynindx != -1
9649 && elf_hash_table (flinfo->info)->dynamic_sections_created)
9650 {
9651 bfd_byte *esym;
9652
9653 /* Since there is no version information in the dynamic string,
9654 if there is no version info in symbol version section, we will
9655 have a run-time problem if not linking executable, referenced
9656 by shared library, or not bound locally. */
9657 if (h->verinfo.verdef == NULL
9658 && (!bfd_link_executable (flinfo->info)
9659 || h->ref_dynamic
9660 || !h->def_regular))
9661 {
9662 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9663
9664 if (p && p [1] != '\0')
9665 {
9666 _bfd_error_handler
9667 /* xgettext:c-format */
9668 (_("%B: No symbol version section for versioned symbol `%s'"),
9669 flinfo->output_bfd, h->root.root.string);
9670 eoinfo->failed = TRUE;
9671 return FALSE;
9672 }
9673 }
9674
9675 sym.st_name = h->dynstr_index;
9676 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9677 + h->dynindx * bed->s->sizeof_sym);
9678 if (!check_dynsym (flinfo->output_bfd, &sym))
9679 {
9680 eoinfo->failed = TRUE;
9681 return FALSE;
9682 }
9683 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
9684
9685 if (flinfo->hash_sec != NULL)
9686 {
9687 size_t hash_entry_size;
9688 bfd_byte *bucketpos;
9689 bfd_vma chain;
9690 size_t bucketcount;
9691 size_t bucket;
9692
9693 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
9694 bucket = h->u.elf_hash_value % bucketcount;
9695
9696 hash_entry_size
9697 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9698 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
9699 + (bucket + 2) * hash_entry_size);
9700 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9701 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9702 bucketpos);
9703 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9704 ((bfd_byte *) flinfo->hash_sec->contents
9705 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9706 }
9707
9708 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
9709 {
9710 Elf_Internal_Versym iversym;
9711 Elf_External_Versym *eversym;
9712
9713 if (!h->def_regular)
9714 {
9715 if (h->verinfo.verdef == NULL
9716 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9717 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
9718 iversym.vs_vers = 0;
9719 else
9720 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9721 }
9722 else
9723 {
9724 if (h->verinfo.vertree == NULL)
9725 iversym.vs_vers = 1;
9726 else
9727 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
9728 if (flinfo->info->create_default_symver)
9729 iversym.vs_vers++;
9730 }
9731
9732 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9733 defined locally. */
9734 if (h->versioned == versioned_hidden && h->def_regular)
9735 iversym.vs_vers |= VERSYM_HIDDEN;
9736
9737 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
9738 eversym += h->dynindx;
9739 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
9740 }
9741 }
9742
9743 /* If the symbol is undefined, and we didn't output it to .dynsym,
9744 strip it from .symtab too. Obviously we can't do this for
9745 relocatable output or when needed for --emit-relocs. */
9746 else if (input_sec == bfd_und_section_ptr
9747 && h->indx != -2
9748 && !bfd_link_relocatable (flinfo->info))
9749 return TRUE;
9750 /* Also strip others that we couldn't earlier due to dynamic symbol
9751 processing. */
9752 if (strip)
9753 return TRUE;
9754 if ((input_sec->flags & SEC_EXCLUDE) != 0)
9755 return TRUE;
9756
9757 /* Output a FILE symbol so that following locals are not associated
9758 with the wrong input file. We need one for forced local symbols
9759 if we've seen more than one FILE symbol or when we have exactly
9760 one FILE symbol but global symbols are present in a file other
9761 than the one with the FILE symbol. We also need one if linker
9762 defined symbols are present. In practice these conditions are
9763 always met, so just emit the FILE symbol unconditionally. */
9764 if (eoinfo->localsyms
9765 && !eoinfo->file_sym_done
9766 && eoinfo->flinfo->filesym_count != 0)
9767 {
9768 Elf_Internal_Sym fsym;
9769
9770 memset (&fsym, 0, sizeof (fsym));
9771 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9772 fsym.st_shndx = SHN_ABS;
9773 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9774 bfd_und_section_ptr, NULL))
9775 return FALSE;
9776
9777 eoinfo->file_sym_done = TRUE;
9778 }
9779
9780 indx = bfd_get_symcount (flinfo->output_bfd);
9781 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9782 input_sec, h);
9783 if (ret == 0)
9784 {
9785 eoinfo->failed = TRUE;
9786 return FALSE;
9787 }
9788 else if (ret == 1)
9789 h->indx = indx;
9790 else if (h->indx == -2)
9791 abort();
9792
9793 return TRUE;
9794 }
9795
9796 /* Return TRUE if special handling is done for relocs in SEC against
9797 symbols defined in discarded sections. */
9798
9799 static bfd_boolean
9800 elf_section_ignore_discarded_relocs (asection *sec)
9801 {
9802 const struct elf_backend_data *bed;
9803
9804 switch (sec->sec_info_type)
9805 {
9806 case SEC_INFO_TYPE_STABS:
9807 case SEC_INFO_TYPE_EH_FRAME:
9808 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
9809 return TRUE;
9810 default:
9811 break;
9812 }
9813
9814 bed = get_elf_backend_data (sec->owner);
9815 if (bed->elf_backend_ignore_discarded_relocs != NULL
9816 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9817 return TRUE;
9818
9819 return FALSE;
9820 }
9821
9822 /* Return a mask saying how ld should treat relocations in SEC against
9823 symbols defined in discarded sections. If this function returns
9824 COMPLAIN set, ld will issue a warning message. If this function
9825 returns PRETEND set, and the discarded section was link-once and the
9826 same size as the kept link-once section, ld will pretend that the
9827 symbol was actually defined in the kept section. Otherwise ld will
9828 zero the reloc (at least that is the intent, but some cooperation by
9829 the target dependent code is needed, particularly for REL targets). */
9830
9831 unsigned int
9832 _bfd_elf_default_action_discarded (asection *sec)
9833 {
9834 if (sec->flags & SEC_DEBUGGING)
9835 return PRETEND;
9836
9837 if (strcmp (".eh_frame", sec->name) == 0)
9838 return 0;
9839
9840 if (strcmp (".gcc_except_table", sec->name) == 0)
9841 return 0;
9842
9843 return COMPLAIN | PRETEND;
9844 }
9845
9846 /* Find a match between a section and a member of a section group. */
9847
9848 static asection *
9849 match_group_member (asection *sec, asection *group,
9850 struct bfd_link_info *info)
9851 {
9852 asection *first = elf_next_in_group (group);
9853 asection *s = first;
9854
9855 while (s != NULL)
9856 {
9857 if (bfd_elf_match_symbols_in_sections (s, sec, info))
9858 return s;
9859
9860 s = elf_next_in_group (s);
9861 if (s == first)
9862 break;
9863 }
9864
9865 return NULL;
9866 }
9867
9868 /* Check if the kept section of a discarded section SEC can be used
9869 to replace it. Return the replacement if it is OK. Otherwise return
9870 NULL. */
9871
9872 asection *
9873 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
9874 {
9875 asection *kept;
9876
9877 kept = sec->kept_section;
9878 if (kept != NULL)
9879 {
9880 if ((kept->flags & SEC_GROUP) != 0)
9881 kept = match_group_member (sec, kept, info);
9882 if (kept != NULL
9883 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9884 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
9885 kept = NULL;
9886 sec->kept_section = kept;
9887 }
9888 return kept;
9889 }
9890
9891 /* Link an input file into the linker output file. This function
9892 handles all the sections and relocations of the input file at once.
9893 This is so that we only have to read the local symbols once, and
9894 don't have to keep them in memory. */
9895
9896 static bfd_boolean
9897 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
9898 {
9899 int (*relocate_section)
9900 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9901 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9902 bfd *output_bfd;
9903 Elf_Internal_Shdr *symtab_hdr;
9904 size_t locsymcount;
9905 size_t extsymoff;
9906 Elf_Internal_Sym *isymbuf;
9907 Elf_Internal_Sym *isym;
9908 Elf_Internal_Sym *isymend;
9909 long *pindex;
9910 asection **ppsection;
9911 asection *o;
9912 const struct elf_backend_data *bed;
9913 struct elf_link_hash_entry **sym_hashes;
9914 bfd_size_type address_size;
9915 bfd_vma r_type_mask;
9916 int r_sym_shift;
9917 bfd_boolean have_file_sym = FALSE;
9918
9919 output_bfd = flinfo->output_bfd;
9920 bed = get_elf_backend_data (output_bfd);
9921 relocate_section = bed->elf_backend_relocate_section;
9922
9923 /* If this is a dynamic object, we don't want to do anything here:
9924 we don't want the local symbols, and we don't want the section
9925 contents. */
9926 if ((input_bfd->flags & DYNAMIC) != 0)
9927 return TRUE;
9928
9929 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9930 if (elf_bad_symtab (input_bfd))
9931 {
9932 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9933 extsymoff = 0;
9934 }
9935 else
9936 {
9937 locsymcount = symtab_hdr->sh_info;
9938 extsymoff = symtab_hdr->sh_info;
9939 }
9940
9941 /* Read the local symbols. */
9942 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9943 if (isymbuf == NULL && locsymcount != 0)
9944 {
9945 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
9946 flinfo->internal_syms,
9947 flinfo->external_syms,
9948 flinfo->locsym_shndx);
9949 if (isymbuf == NULL)
9950 return FALSE;
9951 }
9952
9953 /* Find local symbol sections and adjust values of symbols in
9954 SEC_MERGE sections. Write out those local symbols we know are
9955 going into the output file. */
9956 isymend = isymbuf + locsymcount;
9957 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
9958 isym < isymend;
9959 isym++, pindex++, ppsection++)
9960 {
9961 asection *isec;
9962 const char *name;
9963 Elf_Internal_Sym osym;
9964 long indx;
9965 int ret;
9966
9967 *pindex = -1;
9968
9969 if (elf_bad_symtab (input_bfd))
9970 {
9971 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9972 {
9973 *ppsection = NULL;
9974 continue;
9975 }
9976 }
9977
9978 if (isym->st_shndx == SHN_UNDEF)
9979 isec = bfd_und_section_ptr;
9980 else if (isym->st_shndx == SHN_ABS)
9981 isec = bfd_abs_section_ptr;
9982 else if (isym->st_shndx == SHN_COMMON)
9983 isec = bfd_com_section_ptr;
9984 else
9985 {
9986 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9987 if (isec == NULL)
9988 {
9989 /* Don't attempt to output symbols with st_shnx in the
9990 reserved range other than SHN_ABS and SHN_COMMON. */
9991 *ppsection = NULL;
9992 continue;
9993 }
9994 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
9995 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9996 isym->st_value =
9997 _bfd_merged_section_offset (output_bfd, &isec,
9998 elf_section_data (isec)->sec_info,
9999 isym->st_value);
10000 }
10001
10002 *ppsection = isec;
10003
10004 /* Don't output the first, undefined, symbol. In fact, don't
10005 output any undefined local symbol. */
10006 if (isec == bfd_und_section_ptr)
10007 continue;
10008
10009 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10010 {
10011 /* We never output section symbols. Instead, we use the
10012 section symbol of the corresponding section in the output
10013 file. */
10014 continue;
10015 }
10016
10017 /* If we are stripping all symbols, we don't want to output this
10018 one. */
10019 if (flinfo->info->strip == strip_all)
10020 continue;
10021
10022 /* If we are discarding all local symbols, we don't want to
10023 output this one. If we are generating a relocatable output
10024 file, then some of the local symbols may be required by
10025 relocs; we output them below as we discover that they are
10026 needed. */
10027 if (flinfo->info->discard == discard_all)
10028 continue;
10029
10030 /* If this symbol is defined in a section which we are
10031 discarding, we don't need to keep it. */
10032 if (isym->st_shndx != SHN_UNDEF
10033 && isym->st_shndx < SHN_LORESERVE
10034 && bfd_section_removed_from_list (output_bfd,
10035 isec->output_section))
10036 continue;
10037
10038 /* Get the name of the symbol. */
10039 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10040 isym->st_name);
10041 if (name == NULL)
10042 return FALSE;
10043
10044 /* See if we are discarding symbols with this name. */
10045 if ((flinfo->info->strip == strip_some
10046 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
10047 == NULL))
10048 || (((flinfo->info->discard == discard_sec_merge
10049 && (isec->flags & SEC_MERGE)
10050 && !bfd_link_relocatable (flinfo->info))
10051 || flinfo->info->discard == discard_l)
10052 && bfd_is_local_label_name (input_bfd, name)))
10053 continue;
10054
10055 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10056 {
10057 if (input_bfd->lto_output)
10058 /* -flto puts a temp file name here. This means builds
10059 are not reproducible. Discard the symbol. */
10060 continue;
10061 have_file_sym = TRUE;
10062 flinfo->filesym_count += 1;
10063 }
10064 if (!have_file_sym)
10065 {
10066 /* In the absence of debug info, bfd_find_nearest_line uses
10067 FILE symbols to determine the source file for local
10068 function symbols. Provide a FILE symbol here if input
10069 files lack such, so that their symbols won't be
10070 associated with a previous input file. It's not the
10071 source file, but the best we can do. */
10072 have_file_sym = TRUE;
10073 flinfo->filesym_count += 1;
10074 memset (&osym, 0, sizeof (osym));
10075 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10076 osym.st_shndx = SHN_ABS;
10077 if (!elf_link_output_symstrtab (flinfo,
10078 (input_bfd->lto_output ? NULL
10079 : input_bfd->filename),
10080 &osym, bfd_abs_section_ptr,
10081 NULL))
10082 return FALSE;
10083 }
10084
10085 osym = *isym;
10086
10087 /* Adjust the section index for the output file. */
10088 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10089 isec->output_section);
10090 if (osym.st_shndx == SHN_BAD)
10091 return FALSE;
10092
10093 /* ELF symbols in relocatable files are section relative, but
10094 in executable files they are virtual addresses. Note that
10095 this code assumes that all ELF sections have an associated
10096 BFD section with a reasonable value for output_offset; below
10097 we assume that they also have a reasonable value for
10098 output_section. Any special sections must be set up to meet
10099 these requirements. */
10100 osym.st_value += isec->output_offset;
10101 if (!bfd_link_relocatable (flinfo->info))
10102 {
10103 osym.st_value += isec->output_section->vma;
10104 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10105 {
10106 /* STT_TLS symbols are relative to PT_TLS segment base. */
10107 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10108 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10109 }
10110 }
10111
10112 indx = bfd_get_symcount (output_bfd);
10113 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10114 if (ret == 0)
10115 return FALSE;
10116 else if (ret == 1)
10117 *pindex = indx;
10118 }
10119
10120 if (bed->s->arch_size == 32)
10121 {
10122 r_type_mask = 0xff;
10123 r_sym_shift = 8;
10124 address_size = 4;
10125 }
10126 else
10127 {
10128 r_type_mask = 0xffffffff;
10129 r_sym_shift = 32;
10130 address_size = 8;
10131 }
10132
10133 /* Relocate the contents of each section. */
10134 sym_hashes = elf_sym_hashes (input_bfd);
10135 for (o = input_bfd->sections; o != NULL; o = o->next)
10136 {
10137 bfd_byte *contents;
10138
10139 if (! o->linker_mark)
10140 {
10141 /* This section was omitted from the link. */
10142 continue;
10143 }
10144
10145 if (bfd_link_relocatable (flinfo->info)
10146 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10147 {
10148 /* Deal with the group signature symbol. */
10149 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10150 unsigned long symndx = sec_data->this_hdr.sh_info;
10151 asection *osec = o->output_section;
10152
10153 if (symndx >= locsymcount
10154 || (elf_bad_symtab (input_bfd)
10155 && flinfo->sections[symndx] == NULL))
10156 {
10157 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10158 while (h->root.type == bfd_link_hash_indirect
10159 || h->root.type == bfd_link_hash_warning)
10160 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10161 /* Arrange for symbol to be output. */
10162 h->indx = -2;
10163 elf_section_data (osec)->this_hdr.sh_info = -2;
10164 }
10165 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10166 {
10167 /* We'll use the output section target_index. */
10168 asection *sec = flinfo->sections[symndx]->output_section;
10169 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10170 }
10171 else
10172 {
10173 if (flinfo->indices[symndx] == -1)
10174 {
10175 /* Otherwise output the local symbol now. */
10176 Elf_Internal_Sym sym = isymbuf[symndx];
10177 asection *sec = flinfo->sections[symndx]->output_section;
10178 const char *name;
10179 long indx;
10180 int ret;
10181
10182 name = bfd_elf_string_from_elf_section (input_bfd,
10183 symtab_hdr->sh_link,
10184 sym.st_name);
10185 if (name == NULL)
10186 return FALSE;
10187
10188 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10189 sec);
10190 if (sym.st_shndx == SHN_BAD)
10191 return FALSE;
10192
10193 sym.st_value += o->output_offset;
10194
10195 indx = bfd_get_symcount (output_bfd);
10196 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10197 NULL);
10198 if (ret == 0)
10199 return FALSE;
10200 else if (ret == 1)
10201 flinfo->indices[symndx] = indx;
10202 else
10203 abort ();
10204 }
10205 elf_section_data (osec)->this_hdr.sh_info
10206 = flinfo->indices[symndx];
10207 }
10208 }
10209
10210 if ((o->flags & SEC_HAS_CONTENTS) == 0
10211 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10212 continue;
10213
10214 if ((o->flags & SEC_LINKER_CREATED) != 0)
10215 {
10216 /* Section was created by _bfd_elf_link_create_dynamic_sections
10217 or somesuch. */
10218 continue;
10219 }
10220
10221 /* Get the contents of the section. They have been cached by a
10222 relaxation routine. Note that o is a section in an input
10223 file, so the contents field will not have been set by any of
10224 the routines which work on output files. */
10225 if (elf_section_data (o)->this_hdr.contents != NULL)
10226 {
10227 contents = elf_section_data (o)->this_hdr.contents;
10228 if (bed->caches_rawsize
10229 && o->rawsize != 0
10230 && o->rawsize < o->size)
10231 {
10232 memcpy (flinfo->contents, contents, o->rawsize);
10233 contents = flinfo->contents;
10234 }
10235 }
10236 else
10237 {
10238 contents = flinfo->contents;
10239 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10240 return FALSE;
10241 }
10242
10243 if ((o->flags & SEC_RELOC) != 0)
10244 {
10245 Elf_Internal_Rela *internal_relocs;
10246 Elf_Internal_Rela *rel, *relend;
10247 int action_discarded;
10248 int ret;
10249
10250 /* Get the swapped relocs. */
10251 internal_relocs
10252 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10253 flinfo->internal_relocs, FALSE);
10254 if (internal_relocs == NULL
10255 && o->reloc_count > 0)
10256 return FALSE;
10257
10258 /* We need to reverse-copy input .ctors/.dtors sections if
10259 they are placed in .init_array/.finit_array for output. */
10260 if (o->size > address_size
10261 && ((strncmp (o->name, ".ctors", 6) == 0
10262 && strcmp (o->output_section->name,
10263 ".init_array") == 0)
10264 || (strncmp (o->name, ".dtors", 6) == 0
10265 && strcmp (o->output_section->name,
10266 ".fini_array") == 0))
10267 && (o->name[6] == 0 || o->name[6] == '.'))
10268 {
10269 if (o->size != o->reloc_count * address_size)
10270 {
10271 _bfd_error_handler
10272 /* xgettext:c-format */
10273 (_("error: %B: size of section %A is not "
10274 "multiple of address size"),
10275 input_bfd, o);
10276 bfd_set_error (bfd_error_on_input);
10277 return FALSE;
10278 }
10279 o->flags |= SEC_ELF_REVERSE_COPY;
10280 }
10281
10282 action_discarded = -1;
10283 if (!elf_section_ignore_discarded_relocs (o))
10284 action_discarded = (*bed->action_discarded) (o);
10285
10286 /* Run through the relocs evaluating complex reloc symbols and
10287 looking for relocs against symbols from discarded sections
10288 or section symbols from removed link-once sections.
10289 Complain about relocs against discarded sections. Zero
10290 relocs against removed link-once sections. */
10291
10292 rel = internal_relocs;
10293 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
10294 for ( ; rel < relend; rel++)
10295 {
10296 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10297 unsigned int s_type;
10298 asection **ps, *sec;
10299 struct elf_link_hash_entry *h = NULL;
10300 const char *sym_name;
10301
10302 if (r_symndx == STN_UNDEF)
10303 continue;
10304
10305 if (r_symndx >= locsymcount
10306 || (elf_bad_symtab (input_bfd)
10307 && flinfo->sections[r_symndx] == NULL))
10308 {
10309 h = sym_hashes[r_symndx - extsymoff];
10310
10311 /* Badly formatted input files can contain relocs that
10312 reference non-existant symbols. Check here so that
10313 we do not seg fault. */
10314 if (h == NULL)
10315 {
10316 char buffer [32];
10317
10318 sprintf_vma (buffer, rel->r_info);
10319 _bfd_error_handler
10320 /* xgettext:c-format */
10321 (_("error: %B contains a reloc (0x%s) for section %A "
10322 "that references a non-existent global symbol"),
10323 input_bfd, o, buffer);
10324 bfd_set_error (bfd_error_bad_value);
10325 return FALSE;
10326 }
10327
10328 while (h->root.type == bfd_link_hash_indirect
10329 || h->root.type == bfd_link_hash_warning)
10330 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10331
10332 s_type = h->type;
10333
10334 /* If a plugin symbol is referenced from a non-IR file,
10335 mark the symbol as undefined. Note that the
10336 linker may attach linker created dynamic sections
10337 to the plugin bfd. Symbols defined in linker
10338 created sections are not plugin symbols. */
10339 if (h->root.non_ir_ref
10340 && (h->root.type == bfd_link_hash_defined
10341 || h->root.type == bfd_link_hash_defweak)
10342 && (h->root.u.def.section->flags
10343 & SEC_LINKER_CREATED) == 0
10344 && h->root.u.def.section->owner != NULL
10345 && (h->root.u.def.section->owner->flags
10346 & BFD_PLUGIN) != 0)
10347 {
10348 h->root.type = bfd_link_hash_undefined;
10349 h->root.u.undef.abfd = h->root.u.def.section->owner;
10350 }
10351
10352 ps = NULL;
10353 if (h->root.type == bfd_link_hash_defined
10354 || h->root.type == bfd_link_hash_defweak)
10355 ps = &h->root.u.def.section;
10356
10357 sym_name = h->root.root.string;
10358 }
10359 else
10360 {
10361 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10362
10363 s_type = ELF_ST_TYPE (sym->st_info);
10364 ps = &flinfo->sections[r_symndx];
10365 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10366 sym, *ps);
10367 }
10368
10369 if ((s_type == STT_RELC || s_type == STT_SRELC)
10370 && !bfd_link_relocatable (flinfo->info))
10371 {
10372 bfd_vma val;
10373 bfd_vma dot = (rel->r_offset
10374 + o->output_offset + o->output_section->vma);
10375 #ifdef DEBUG
10376 printf ("Encountered a complex symbol!");
10377 printf (" (input_bfd %s, section %s, reloc %ld\n",
10378 input_bfd->filename, o->name,
10379 (long) (rel - internal_relocs));
10380 printf (" symbol: idx %8.8lx, name %s\n",
10381 r_symndx, sym_name);
10382 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10383 (unsigned long) rel->r_info,
10384 (unsigned long) rel->r_offset);
10385 #endif
10386 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10387 isymbuf, locsymcount, s_type == STT_SRELC))
10388 return FALSE;
10389
10390 /* Symbol evaluated OK. Update to absolute value. */
10391 set_symbol_value (input_bfd, isymbuf, locsymcount,
10392 r_symndx, val);
10393 continue;
10394 }
10395
10396 if (action_discarded != -1 && ps != NULL)
10397 {
10398 /* Complain if the definition comes from a
10399 discarded section. */
10400 if ((sec = *ps) != NULL && discarded_section (sec))
10401 {
10402 BFD_ASSERT (r_symndx != STN_UNDEF);
10403 if (action_discarded & COMPLAIN)
10404 (*flinfo->info->callbacks->einfo)
10405 /* xgettext:c-format */
10406 (_("%X`%s' referenced in section `%A' of %B: "
10407 "defined in discarded section `%A' of %B\n"),
10408 sym_name, o, input_bfd, sec, sec->owner);
10409
10410 /* Try to do the best we can to support buggy old
10411 versions of gcc. Pretend that the symbol is
10412 really defined in the kept linkonce section.
10413 FIXME: This is quite broken. Modifying the
10414 symbol here means we will be changing all later
10415 uses of the symbol, not just in this section. */
10416 if (action_discarded & PRETEND)
10417 {
10418 asection *kept;
10419
10420 kept = _bfd_elf_check_kept_section (sec,
10421 flinfo->info);
10422 if (kept != NULL)
10423 {
10424 *ps = kept;
10425 continue;
10426 }
10427 }
10428 }
10429 }
10430 }
10431
10432 /* Relocate the section by invoking a back end routine.
10433
10434 The back end routine is responsible for adjusting the
10435 section contents as necessary, and (if using Rela relocs
10436 and generating a relocatable output file) adjusting the
10437 reloc addend as necessary.
10438
10439 The back end routine does not have to worry about setting
10440 the reloc address or the reloc symbol index.
10441
10442 The back end routine is given a pointer to the swapped in
10443 internal symbols, and can access the hash table entries
10444 for the external symbols via elf_sym_hashes (input_bfd).
10445
10446 When generating relocatable output, the back end routine
10447 must handle STB_LOCAL/STT_SECTION symbols specially. The
10448 output symbol is going to be a section symbol
10449 corresponding to the output section, which will require
10450 the addend to be adjusted. */
10451
10452 ret = (*relocate_section) (output_bfd, flinfo->info,
10453 input_bfd, o, contents,
10454 internal_relocs,
10455 isymbuf,
10456 flinfo->sections);
10457 if (!ret)
10458 return FALSE;
10459
10460 if (ret == 2
10461 || bfd_link_relocatable (flinfo->info)
10462 || flinfo->info->emitrelocations)
10463 {
10464 Elf_Internal_Rela *irela;
10465 Elf_Internal_Rela *irelaend, *irelamid;
10466 bfd_vma last_offset;
10467 struct elf_link_hash_entry **rel_hash;
10468 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10469 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10470 unsigned int next_erel;
10471 bfd_boolean rela_normal;
10472 struct bfd_elf_section_data *esdi, *esdo;
10473
10474 esdi = elf_section_data (o);
10475 esdo = elf_section_data (o->output_section);
10476 rela_normal = FALSE;
10477
10478 /* Adjust the reloc addresses and symbol indices. */
10479
10480 irela = internal_relocs;
10481 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
10482 rel_hash = esdo->rel.hashes + esdo->rel.count;
10483 /* We start processing the REL relocs, if any. When we reach
10484 IRELAMID in the loop, we switch to the RELA relocs. */
10485 irelamid = irela;
10486 if (esdi->rel.hdr != NULL)
10487 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10488 * bed->s->int_rels_per_ext_rel);
10489 rel_hash_list = rel_hash;
10490 rela_hash_list = NULL;
10491 last_offset = o->output_offset;
10492 if (!bfd_link_relocatable (flinfo->info))
10493 last_offset += o->output_section->vma;
10494 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10495 {
10496 unsigned long r_symndx;
10497 asection *sec;
10498 Elf_Internal_Sym sym;
10499
10500 if (next_erel == bed->s->int_rels_per_ext_rel)
10501 {
10502 rel_hash++;
10503 next_erel = 0;
10504 }
10505
10506 if (irela == irelamid)
10507 {
10508 rel_hash = esdo->rela.hashes + esdo->rela.count;
10509 rela_hash_list = rel_hash;
10510 rela_normal = bed->rela_normal;
10511 }
10512
10513 irela->r_offset = _bfd_elf_section_offset (output_bfd,
10514 flinfo->info, o,
10515 irela->r_offset);
10516 if (irela->r_offset >= (bfd_vma) -2)
10517 {
10518 /* This is a reloc for a deleted entry or somesuch.
10519 Turn it into an R_*_NONE reloc, at the same
10520 offset as the last reloc. elf_eh_frame.c and
10521 bfd_elf_discard_info rely on reloc offsets
10522 being ordered. */
10523 irela->r_offset = last_offset;
10524 irela->r_info = 0;
10525 irela->r_addend = 0;
10526 continue;
10527 }
10528
10529 irela->r_offset += o->output_offset;
10530
10531 /* Relocs in an executable have to be virtual addresses. */
10532 if (!bfd_link_relocatable (flinfo->info))
10533 irela->r_offset += o->output_section->vma;
10534
10535 last_offset = irela->r_offset;
10536
10537 r_symndx = irela->r_info >> r_sym_shift;
10538 if (r_symndx == STN_UNDEF)
10539 continue;
10540
10541 if (r_symndx >= locsymcount
10542 || (elf_bad_symtab (input_bfd)
10543 && flinfo->sections[r_symndx] == NULL))
10544 {
10545 struct elf_link_hash_entry *rh;
10546 unsigned long indx;
10547
10548 /* This is a reloc against a global symbol. We
10549 have not yet output all the local symbols, so
10550 we do not know the symbol index of any global
10551 symbol. We set the rel_hash entry for this
10552 reloc to point to the global hash table entry
10553 for this symbol. The symbol index is then
10554 set at the end of bfd_elf_final_link. */
10555 indx = r_symndx - extsymoff;
10556 rh = elf_sym_hashes (input_bfd)[indx];
10557 while (rh->root.type == bfd_link_hash_indirect
10558 || rh->root.type == bfd_link_hash_warning)
10559 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10560
10561 /* Setting the index to -2 tells
10562 elf_link_output_extsym that this symbol is
10563 used by a reloc. */
10564 BFD_ASSERT (rh->indx < 0);
10565 rh->indx = -2;
10566
10567 *rel_hash = rh;
10568
10569 continue;
10570 }
10571
10572 /* This is a reloc against a local symbol. */
10573
10574 *rel_hash = NULL;
10575 sym = isymbuf[r_symndx];
10576 sec = flinfo->sections[r_symndx];
10577 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10578 {
10579 /* I suppose the backend ought to fill in the
10580 section of any STT_SECTION symbol against a
10581 processor specific section. */
10582 r_symndx = STN_UNDEF;
10583 if (bfd_is_abs_section (sec))
10584 ;
10585 else if (sec == NULL || sec->owner == NULL)
10586 {
10587 bfd_set_error (bfd_error_bad_value);
10588 return FALSE;
10589 }
10590 else
10591 {
10592 asection *osec = sec->output_section;
10593
10594 /* If we have discarded a section, the output
10595 section will be the absolute section. In
10596 case of discarded SEC_MERGE sections, use
10597 the kept section. relocate_section should
10598 have already handled discarded linkonce
10599 sections. */
10600 if (bfd_is_abs_section (osec)
10601 && sec->kept_section != NULL
10602 && sec->kept_section->output_section != NULL)
10603 {
10604 osec = sec->kept_section->output_section;
10605 irela->r_addend -= osec->vma;
10606 }
10607
10608 if (!bfd_is_abs_section (osec))
10609 {
10610 r_symndx = osec->target_index;
10611 if (r_symndx == STN_UNDEF)
10612 {
10613 irela->r_addend += osec->vma;
10614 osec = _bfd_nearby_section (output_bfd, osec,
10615 osec->vma);
10616 irela->r_addend -= osec->vma;
10617 r_symndx = osec->target_index;
10618 }
10619 }
10620 }
10621
10622 /* Adjust the addend according to where the
10623 section winds up in the output section. */
10624 if (rela_normal)
10625 irela->r_addend += sec->output_offset;
10626 }
10627 else
10628 {
10629 if (flinfo->indices[r_symndx] == -1)
10630 {
10631 unsigned long shlink;
10632 const char *name;
10633 asection *osec;
10634 long indx;
10635
10636 if (flinfo->info->strip == strip_all)
10637 {
10638 /* You can't do ld -r -s. */
10639 bfd_set_error (bfd_error_invalid_operation);
10640 return FALSE;
10641 }
10642
10643 /* This symbol was skipped earlier, but
10644 since it is needed by a reloc, we
10645 must output it now. */
10646 shlink = symtab_hdr->sh_link;
10647 name = (bfd_elf_string_from_elf_section
10648 (input_bfd, shlink, sym.st_name));
10649 if (name == NULL)
10650 return FALSE;
10651
10652 osec = sec->output_section;
10653 sym.st_shndx =
10654 _bfd_elf_section_from_bfd_section (output_bfd,
10655 osec);
10656 if (sym.st_shndx == SHN_BAD)
10657 return FALSE;
10658
10659 sym.st_value += sec->output_offset;
10660 if (!bfd_link_relocatable (flinfo->info))
10661 {
10662 sym.st_value += osec->vma;
10663 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10664 {
10665 /* STT_TLS symbols are relative to PT_TLS
10666 segment base. */
10667 BFD_ASSERT (elf_hash_table (flinfo->info)
10668 ->tls_sec != NULL);
10669 sym.st_value -= (elf_hash_table (flinfo->info)
10670 ->tls_sec->vma);
10671 }
10672 }
10673
10674 indx = bfd_get_symcount (output_bfd);
10675 ret = elf_link_output_symstrtab (flinfo, name,
10676 &sym, sec,
10677 NULL);
10678 if (ret == 0)
10679 return FALSE;
10680 else if (ret == 1)
10681 flinfo->indices[r_symndx] = indx;
10682 else
10683 abort ();
10684 }
10685
10686 r_symndx = flinfo->indices[r_symndx];
10687 }
10688
10689 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10690 | (irela->r_info & r_type_mask));
10691 }
10692
10693 /* Swap out the relocs. */
10694 input_rel_hdr = esdi->rel.hdr;
10695 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
10696 {
10697 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10698 input_rel_hdr,
10699 internal_relocs,
10700 rel_hash_list))
10701 return FALSE;
10702 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10703 * bed->s->int_rels_per_ext_rel);
10704 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
10705 }
10706
10707 input_rela_hdr = esdi->rela.hdr;
10708 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10709 {
10710 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10711 input_rela_hdr,
10712 internal_relocs,
10713 rela_hash_list))
10714 return FALSE;
10715 }
10716 }
10717 }
10718
10719 /* Write out the modified section contents. */
10720 if (bed->elf_backend_write_section
10721 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
10722 contents))
10723 {
10724 /* Section written out. */
10725 }
10726 else switch (o->sec_info_type)
10727 {
10728 case SEC_INFO_TYPE_STABS:
10729 if (! (_bfd_write_section_stabs
10730 (output_bfd,
10731 &elf_hash_table (flinfo->info)->stab_info,
10732 o, &elf_section_data (o)->sec_info, contents)))
10733 return FALSE;
10734 break;
10735 case SEC_INFO_TYPE_MERGE:
10736 if (! _bfd_write_merged_section (output_bfd, o,
10737 elf_section_data (o)->sec_info))
10738 return FALSE;
10739 break;
10740 case SEC_INFO_TYPE_EH_FRAME:
10741 {
10742 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
10743 o, contents))
10744 return FALSE;
10745 }
10746 break;
10747 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10748 {
10749 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10750 flinfo->info,
10751 o, contents))
10752 return FALSE;
10753 }
10754 break;
10755 default:
10756 {
10757 if (! (o->flags & SEC_EXCLUDE))
10758 {
10759 file_ptr offset = (file_ptr) o->output_offset;
10760 bfd_size_type todo = o->size;
10761
10762 offset *= bfd_octets_per_byte (output_bfd);
10763
10764 if ((o->flags & SEC_ELF_REVERSE_COPY))
10765 {
10766 /* Reverse-copy input section to output. */
10767 do
10768 {
10769 todo -= address_size;
10770 if (! bfd_set_section_contents (output_bfd,
10771 o->output_section,
10772 contents + todo,
10773 offset,
10774 address_size))
10775 return FALSE;
10776 if (todo == 0)
10777 break;
10778 offset += address_size;
10779 }
10780 while (1);
10781 }
10782 else if (! bfd_set_section_contents (output_bfd,
10783 o->output_section,
10784 contents,
10785 offset, todo))
10786 return FALSE;
10787 }
10788 }
10789 break;
10790 }
10791 }
10792
10793 return TRUE;
10794 }
10795
10796 /* Generate a reloc when linking an ELF file. This is a reloc
10797 requested by the linker, and does not come from any input file. This
10798 is used to build constructor and destructor tables when linking
10799 with -Ur. */
10800
10801 static bfd_boolean
10802 elf_reloc_link_order (bfd *output_bfd,
10803 struct bfd_link_info *info,
10804 asection *output_section,
10805 struct bfd_link_order *link_order)
10806 {
10807 reloc_howto_type *howto;
10808 long indx;
10809 bfd_vma offset;
10810 bfd_vma addend;
10811 struct bfd_elf_section_reloc_data *reldata;
10812 struct elf_link_hash_entry **rel_hash_ptr;
10813 Elf_Internal_Shdr *rel_hdr;
10814 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10815 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10816 bfd_byte *erel;
10817 unsigned int i;
10818 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
10819
10820 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10821 if (howto == NULL)
10822 {
10823 bfd_set_error (bfd_error_bad_value);
10824 return FALSE;
10825 }
10826
10827 addend = link_order->u.reloc.p->addend;
10828
10829 if (esdo->rel.hdr)
10830 reldata = &esdo->rel;
10831 else if (esdo->rela.hdr)
10832 reldata = &esdo->rela;
10833 else
10834 {
10835 reldata = NULL;
10836 BFD_ASSERT (0);
10837 }
10838
10839 /* Figure out the symbol index. */
10840 rel_hash_ptr = reldata->hashes + reldata->count;
10841 if (link_order->type == bfd_section_reloc_link_order)
10842 {
10843 indx = link_order->u.reloc.p->u.section->target_index;
10844 BFD_ASSERT (indx != 0);
10845 *rel_hash_ptr = NULL;
10846 }
10847 else
10848 {
10849 struct elf_link_hash_entry *h;
10850
10851 /* Treat a reloc against a defined symbol as though it were
10852 actually against the section. */
10853 h = ((struct elf_link_hash_entry *)
10854 bfd_wrapped_link_hash_lookup (output_bfd, info,
10855 link_order->u.reloc.p->u.name,
10856 FALSE, FALSE, TRUE));
10857 if (h != NULL
10858 && (h->root.type == bfd_link_hash_defined
10859 || h->root.type == bfd_link_hash_defweak))
10860 {
10861 asection *section;
10862
10863 section = h->root.u.def.section;
10864 indx = section->output_section->target_index;
10865 *rel_hash_ptr = NULL;
10866 /* It seems that we ought to add the symbol value to the
10867 addend here, but in practice it has already been added
10868 because it was passed to constructor_callback. */
10869 addend += section->output_section->vma + section->output_offset;
10870 }
10871 else if (h != NULL)
10872 {
10873 /* Setting the index to -2 tells elf_link_output_extsym that
10874 this symbol is used by a reloc. */
10875 h->indx = -2;
10876 *rel_hash_ptr = h;
10877 indx = 0;
10878 }
10879 else
10880 {
10881 (*info->callbacks->unattached_reloc)
10882 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
10883 indx = 0;
10884 }
10885 }
10886
10887 /* If this is an inplace reloc, we must write the addend into the
10888 object file. */
10889 if (howto->partial_inplace && addend != 0)
10890 {
10891 bfd_size_type size;
10892 bfd_reloc_status_type rstat;
10893 bfd_byte *buf;
10894 bfd_boolean ok;
10895 const char *sym_name;
10896
10897 size = (bfd_size_type) bfd_get_reloc_size (howto);
10898 buf = (bfd_byte *) bfd_zmalloc (size);
10899 if (buf == NULL && size != 0)
10900 return FALSE;
10901 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10902 switch (rstat)
10903 {
10904 case bfd_reloc_ok:
10905 break;
10906
10907 default:
10908 case bfd_reloc_outofrange:
10909 abort ();
10910
10911 case bfd_reloc_overflow:
10912 if (link_order->type == bfd_section_reloc_link_order)
10913 sym_name = bfd_section_name (output_bfd,
10914 link_order->u.reloc.p->u.section);
10915 else
10916 sym_name = link_order->u.reloc.p->u.name;
10917 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
10918 howto->name, addend, NULL, NULL,
10919 (bfd_vma) 0);
10920 break;
10921 }
10922
10923 ok = bfd_set_section_contents (output_bfd, output_section, buf,
10924 link_order->offset
10925 * bfd_octets_per_byte (output_bfd),
10926 size);
10927 free (buf);
10928 if (! ok)
10929 return FALSE;
10930 }
10931
10932 /* The address of a reloc is relative to the section in a
10933 relocatable file, and is a virtual address in an executable
10934 file. */
10935 offset = link_order->offset;
10936 if (! bfd_link_relocatable (info))
10937 offset += output_section->vma;
10938
10939 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10940 {
10941 irel[i].r_offset = offset;
10942 irel[i].r_info = 0;
10943 irel[i].r_addend = 0;
10944 }
10945 if (bed->s->arch_size == 32)
10946 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10947 else
10948 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10949
10950 rel_hdr = reldata->hdr;
10951 erel = rel_hdr->contents;
10952 if (rel_hdr->sh_type == SHT_REL)
10953 {
10954 erel += reldata->count * bed->s->sizeof_rel;
10955 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10956 }
10957 else
10958 {
10959 irel[0].r_addend = addend;
10960 erel += reldata->count * bed->s->sizeof_rela;
10961 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10962 }
10963
10964 ++reldata->count;
10965
10966 return TRUE;
10967 }
10968
10969
10970 /* Get the output vma of the section pointed to by the sh_link field. */
10971
10972 static bfd_vma
10973 elf_get_linked_section_vma (struct bfd_link_order *p)
10974 {
10975 Elf_Internal_Shdr **elf_shdrp;
10976 asection *s;
10977 int elfsec;
10978
10979 s = p->u.indirect.section;
10980 elf_shdrp = elf_elfsections (s->owner);
10981 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10982 elfsec = elf_shdrp[elfsec]->sh_link;
10983 /* PR 290:
10984 The Intel C compiler generates SHT_IA_64_UNWIND with
10985 SHF_LINK_ORDER. But it doesn't set the sh_link or
10986 sh_info fields. Hence we could get the situation
10987 where elfsec is 0. */
10988 if (elfsec == 0)
10989 {
10990 const struct elf_backend_data *bed
10991 = get_elf_backend_data (s->owner);
10992 if (bed->link_order_error_handler)
10993 bed->link_order_error_handler
10994 /* xgettext:c-format */
10995 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
10996 return 0;
10997 }
10998 else
10999 {
11000 s = elf_shdrp[elfsec]->bfd_section;
11001 return s->output_section->vma + s->output_offset;
11002 }
11003 }
11004
11005
11006 /* Compare two sections based on the locations of the sections they are
11007 linked to. Used by elf_fixup_link_order. */
11008
11009 static int
11010 compare_link_order (const void * a, const void * b)
11011 {
11012 bfd_vma apos;
11013 bfd_vma bpos;
11014
11015 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11016 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11017 if (apos < bpos)
11018 return -1;
11019 return apos > bpos;
11020 }
11021
11022
11023 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11024 order as their linked sections. Returns false if this could not be done
11025 because an output section includes both ordered and unordered
11026 sections. Ideally we'd do this in the linker proper. */
11027
11028 static bfd_boolean
11029 elf_fixup_link_order (bfd *abfd, asection *o)
11030 {
11031 int seen_linkorder;
11032 int seen_other;
11033 int n;
11034 struct bfd_link_order *p;
11035 bfd *sub;
11036 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11037 unsigned elfsec;
11038 struct bfd_link_order **sections;
11039 asection *s, *other_sec, *linkorder_sec;
11040 bfd_vma offset;
11041
11042 other_sec = NULL;
11043 linkorder_sec = NULL;
11044 seen_other = 0;
11045 seen_linkorder = 0;
11046 for (p = o->map_head.link_order; p != NULL; p = p->next)
11047 {
11048 if (p->type == bfd_indirect_link_order)
11049 {
11050 s = p->u.indirect.section;
11051 sub = s->owner;
11052 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11053 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
11054 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11055 && elfsec < elf_numsections (sub)
11056 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11057 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
11058 {
11059 seen_linkorder++;
11060 linkorder_sec = s;
11061 }
11062 else
11063 {
11064 seen_other++;
11065 other_sec = s;
11066 }
11067 }
11068 else
11069 seen_other++;
11070
11071 if (seen_other && seen_linkorder)
11072 {
11073 if (other_sec && linkorder_sec)
11074 _bfd_error_handler
11075 /* xgettext:c-format */
11076 (_("%A has both ordered [`%A' in %B] "
11077 "and unordered [`%A' in %B] sections"),
11078 o, linkorder_sec,
11079 linkorder_sec->owner, other_sec,
11080 other_sec->owner);
11081 else
11082 _bfd_error_handler
11083 (_("%A has both ordered and unordered sections"), o);
11084 bfd_set_error (bfd_error_bad_value);
11085 return FALSE;
11086 }
11087 }
11088
11089 if (!seen_linkorder)
11090 return TRUE;
11091
11092 sections = (struct bfd_link_order **)
11093 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11094 if (sections == NULL)
11095 return FALSE;
11096 seen_linkorder = 0;
11097
11098 for (p = o->map_head.link_order; p != NULL; p = p->next)
11099 {
11100 sections[seen_linkorder++] = p;
11101 }
11102 /* Sort the input sections in the order of their linked section. */
11103 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11104 compare_link_order);
11105
11106 /* Change the offsets of the sections. */
11107 offset = 0;
11108 for (n = 0; n < seen_linkorder; n++)
11109 {
11110 s = sections[n]->u.indirect.section;
11111 offset &= ~(bfd_vma) 0 << s->alignment_power;
11112 s->output_offset = offset / bfd_octets_per_byte (abfd);
11113 sections[n]->offset = offset;
11114 offset += sections[n]->size;
11115 }
11116
11117 free (sections);
11118 return TRUE;
11119 }
11120
11121 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11122 Returns TRUE upon success, FALSE otherwise. */
11123
11124 static bfd_boolean
11125 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11126 {
11127 bfd_boolean ret = FALSE;
11128 bfd *implib_bfd;
11129 const struct elf_backend_data *bed;
11130 flagword flags;
11131 enum bfd_architecture arch;
11132 unsigned int mach;
11133 asymbol **sympp = NULL;
11134 long symsize;
11135 long symcount;
11136 long src_count;
11137 elf_symbol_type *osymbuf;
11138
11139 implib_bfd = info->out_implib_bfd;
11140 bed = get_elf_backend_data (abfd);
11141
11142 if (!bfd_set_format (implib_bfd, bfd_object))
11143 return FALSE;
11144
11145 flags = bfd_get_file_flags (abfd);
11146 flags &= ~HAS_RELOC;
11147 if (!bfd_set_start_address (implib_bfd, 0)
11148 || !bfd_set_file_flags (implib_bfd, flags))
11149 return FALSE;
11150
11151 /* Copy architecture of output file to import library file. */
11152 arch = bfd_get_arch (abfd);
11153 mach = bfd_get_mach (abfd);
11154 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11155 && (abfd->target_defaulted
11156 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11157 return FALSE;
11158
11159 /* Get symbol table size. */
11160 symsize = bfd_get_symtab_upper_bound (abfd);
11161 if (symsize < 0)
11162 return FALSE;
11163
11164 /* Read in the symbol table. */
11165 sympp = (asymbol **) xmalloc (symsize);
11166 symcount = bfd_canonicalize_symtab (abfd, sympp);
11167 if (symcount < 0)
11168 goto free_sym_buf;
11169
11170 /* Allow the BFD backend to copy any private header data it
11171 understands from the output BFD to the import library BFD. */
11172 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11173 goto free_sym_buf;
11174
11175 /* Filter symbols to appear in the import library. */
11176 if (bed->elf_backend_filter_implib_symbols)
11177 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11178 symcount);
11179 else
11180 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11181 if (symcount == 0)
11182 {
11183 bfd_set_error (bfd_error_no_symbols);
11184 _bfd_error_handler (_("%B: no symbol found for import library"),
11185 implib_bfd);
11186 goto free_sym_buf;
11187 }
11188
11189
11190 /* Make symbols absolute. */
11191 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11192 sizeof (*osymbuf));
11193 for (src_count = 0; src_count < symcount; src_count++)
11194 {
11195 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11196 sizeof (*osymbuf));
11197 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11198 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11199 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11200 osymbuf[src_count].internal_elf_sym.st_value =
11201 osymbuf[src_count].symbol.value;
11202 sympp[src_count] = &osymbuf[src_count].symbol;
11203 }
11204
11205 bfd_set_symtab (implib_bfd, sympp, symcount);
11206
11207 /* Allow the BFD backend to copy any private data it understands
11208 from the output BFD to the import library BFD. This is done last
11209 to permit the routine to look at the filtered symbol table. */
11210 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11211 goto free_sym_buf;
11212
11213 if (!bfd_close (implib_bfd))
11214 goto free_sym_buf;
11215
11216 ret = TRUE;
11217
11218 free_sym_buf:
11219 free (sympp);
11220 return ret;
11221 }
11222
11223 static void
11224 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11225 {
11226 asection *o;
11227
11228 if (flinfo->symstrtab != NULL)
11229 _bfd_elf_strtab_free (flinfo->symstrtab);
11230 if (flinfo->contents != NULL)
11231 free (flinfo->contents);
11232 if (flinfo->external_relocs != NULL)
11233 free (flinfo->external_relocs);
11234 if (flinfo->internal_relocs != NULL)
11235 free (flinfo->internal_relocs);
11236 if (flinfo->external_syms != NULL)
11237 free (flinfo->external_syms);
11238 if (flinfo->locsym_shndx != NULL)
11239 free (flinfo->locsym_shndx);
11240 if (flinfo->internal_syms != NULL)
11241 free (flinfo->internal_syms);
11242 if (flinfo->indices != NULL)
11243 free (flinfo->indices);
11244 if (flinfo->sections != NULL)
11245 free (flinfo->sections);
11246 if (flinfo->symshndxbuf != NULL)
11247 free (flinfo->symshndxbuf);
11248 for (o = obfd->sections; o != NULL; o = o->next)
11249 {
11250 struct bfd_elf_section_data *esdo = elf_section_data (o);
11251 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11252 free (esdo->rel.hashes);
11253 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11254 free (esdo->rela.hashes);
11255 }
11256 }
11257
11258 /* Do the final step of an ELF link. */
11259
11260 bfd_boolean
11261 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11262 {
11263 bfd_boolean dynamic;
11264 bfd_boolean emit_relocs;
11265 bfd *dynobj;
11266 struct elf_final_link_info flinfo;
11267 asection *o;
11268 struct bfd_link_order *p;
11269 bfd *sub;
11270 bfd_size_type max_contents_size;
11271 bfd_size_type max_external_reloc_size;
11272 bfd_size_type max_internal_reloc_count;
11273 bfd_size_type max_sym_count;
11274 bfd_size_type max_sym_shndx_count;
11275 Elf_Internal_Sym elfsym;
11276 unsigned int i;
11277 Elf_Internal_Shdr *symtab_hdr;
11278 Elf_Internal_Shdr *symtab_shndx_hdr;
11279 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11280 struct elf_outext_info eoinfo;
11281 bfd_boolean merged;
11282 size_t relativecount = 0;
11283 asection *reldyn = 0;
11284 bfd_size_type amt;
11285 asection *attr_section = NULL;
11286 bfd_vma attr_size = 0;
11287 const char *std_attrs_section;
11288 struct elf_link_hash_table *htab = elf_hash_table (info);
11289
11290 if (!is_elf_hash_table (htab))
11291 return FALSE;
11292
11293 if (bfd_link_pic (info))
11294 abfd->flags |= DYNAMIC;
11295
11296 dynamic = htab->dynamic_sections_created;
11297 dynobj = htab->dynobj;
11298
11299 emit_relocs = (bfd_link_relocatable (info)
11300 || info->emitrelocations);
11301
11302 flinfo.info = info;
11303 flinfo.output_bfd = abfd;
11304 flinfo.symstrtab = _bfd_elf_strtab_init ();
11305 if (flinfo.symstrtab == NULL)
11306 return FALSE;
11307
11308 if (! dynamic)
11309 {
11310 flinfo.hash_sec = NULL;
11311 flinfo.symver_sec = NULL;
11312 }
11313 else
11314 {
11315 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11316 /* Note that dynsym_sec can be NULL (on VMS). */
11317 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11318 /* Note that it is OK if symver_sec is NULL. */
11319 }
11320
11321 flinfo.contents = NULL;
11322 flinfo.external_relocs = NULL;
11323 flinfo.internal_relocs = NULL;
11324 flinfo.external_syms = NULL;
11325 flinfo.locsym_shndx = NULL;
11326 flinfo.internal_syms = NULL;
11327 flinfo.indices = NULL;
11328 flinfo.sections = NULL;
11329 flinfo.symshndxbuf = NULL;
11330 flinfo.filesym_count = 0;
11331
11332 /* The object attributes have been merged. Remove the input
11333 sections from the link, and set the contents of the output
11334 secton. */
11335 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11336 for (o = abfd->sections; o != NULL; o = o->next)
11337 {
11338 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11339 || strcmp (o->name, ".gnu.attributes") == 0)
11340 {
11341 for (p = o->map_head.link_order; p != NULL; p = p->next)
11342 {
11343 asection *input_section;
11344
11345 if (p->type != bfd_indirect_link_order)
11346 continue;
11347 input_section = p->u.indirect.section;
11348 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11349 elf_link_input_bfd ignores this section. */
11350 input_section->flags &= ~SEC_HAS_CONTENTS;
11351 }
11352
11353 attr_size = bfd_elf_obj_attr_size (abfd);
11354 if (attr_size)
11355 {
11356 bfd_set_section_size (abfd, o, attr_size);
11357 attr_section = o;
11358 /* Skip this section later on. */
11359 o->map_head.link_order = NULL;
11360 }
11361 else
11362 o->flags |= SEC_EXCLUDE;
11363 }
11364 }
11365
11366 /* Count up the number of relocations we will output for each output
11367 section, so that we know the sizes of the reloc sections. We
11368 also figure out some maximum sizes. */
11369 max_contents_size = 0;
11370 max_external_reloc_size = 0;
11371 max_internal_reloc_count = 0;
11372 max_sym_count = 0;
11373 max_sym_shndx_count = 0;
11374 merged = FALSE;
11375 for (o = abfd->sections; o != NULL; o = o->next)
11376 {
11377 struct bfd_elf_section_data *esdo = elf_section_data (o);
11378 o->reloc_count = 0;
11379
11380 for (p = o->map_head.link_order; p != NULL; p = p->next)
11381 {
11382 unsigned int reloc_count = 0;
11383 unsigned int additional_reloc_count = 0;
11384 struct bfd_elf_section_data *esdi = NULL;
11385
11386 if (p->type == bfd_section_reloc_link_order
11387 || p->type == bfd_symbol_reloc_link_order)
11388 reloc_count = 1;
11389 else if (p->type == bfd_indirect_link_order)
11390 {
11391 asection *sec;
11392
11393 sec = p->u.indirect.section;
11394
11395 /* Mark all sections which are to be included in the
11396 link. This will normally be every section. We need
11397 to do this so that we can identify any sections which
11398 the linker has decided to not include. */
11399 sec->linker_mark = TRUE;
11400
11401 if (sec->flags & SEC_MERGE)
11402 merged = TRUE;
11403
11404 if (sec->rawsize > max_contents_size)
11405 max_contents_size = sec->rawsize;
11406 if (sec->size > max_contents_size)
11407 max_contents_size = sec->size;
11408
11409 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11410 && (sec->owner->flags & DYNAMIC) == 0)
11411 {
11412 size_t sym_count;
11413
11414 /* We are interested in just local symbols, not all
11415 symbols. */
11416 if (elf_bad_symtab (sec->owner))
11417 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11418 / bed->s->sizeof_sym);
11419 else
11420 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11421
11422 if (sym_count > max_sym_count)
11423 max_sym_count = sym_count;
11424
11425 if (sym_count > max_sym_shndx_count
11426 && elf_symtab_shndx_list (sec->owner) != NULL)
11427 max_sym_shndx_count = sym_count;
11428
11429 if (esdo->this_hdr.sh_type == SHT_REL
11430 || esdo->this_hdr.sh_type == SHT_RELA)
11431 /* Some backends use reloc_count in relocation sections
11432 to count particular types of relocs. Of course,
11433 reloc sections themselves can't have relocations. */
11434 ;
11435 else if (emit_relocs)
11436 {
11437 reloc_count = sec->reloc_count;
11438 if (bed->elf_backend_count_additional_relocs)
11439 {
11440 int c;
11441 c = (*bed->elf_backend_count_additional_relocs) (sec);
11442 additional_reloc_count += c;
11443 }
11444 }
11445 else if (bed->elf_backend_count_relocs)
11446 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11447
11448 esdi = elf_section_data (sec);
11449
11450 if ((sec->flags & SEC_RELOC) != 0)
11451 {
11452 size_t ext_size = 0;
11453
11454 if (esdi->rel.hdr != NULL)
11455 ext_size = esdi->rel.hdr->sh_size;
11456 if (esdi->rela.hdr != NULL)
11457 ext_size += esdi->rela.hdr->sh_size;
11458
11459 if (ext_size > max_external_reloc_size)
11460 max_external_reloc_size = ext_size;
11461 if (sec->reloc_count > max_internal_reloc_count)
11462 max_internal_reloc_count = sec->reloc_count;
11463 }
11464 }
11465 }
11466
11467 if (reloc_count == 0)
11468 continue;
11469
11470 reloc_count += additional_reloc_count;
11471 o->reloc_count += reloc_count;
11472
11473 if (p->type == bfd_indirect_link_order && emit_relocs)
11474 {
11475 if (esdi->rel.hdr)
11476 {
11477 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11478 esdo->rel.count += additional_reloc_count;
11479 }
11480 if (esdi->rela.hdr)
11481 {
11482 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11483 esdo->rela.count += additional_reloc_count;
11484 }
11485 }
11486 else
11487 {
11488 if (o->use_rela_p)
11489 esdo->rela.count += reloc_count;
11490 else
11491 esdo->rel.count += reloc_count;
11492 }
11493 }
11494
11495 if (o->reloc_count > 0)
11496 o->flags |= SEC_RELOC;
11497 else
11498 {
11499 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11500 set it (this is probably a bug) and if it is set
11501 assign_section_numbers will create a reloc section. */
11502 o->flags &=~ SEC_RELOC;
11503 }
11504
11505 /* If the SEC_ALLOC flag is not set, force the section VMA to
11506 zero. This is done in elf_fake_sections as well, but forcing
11507 the VMA to 0 here will ensure that relocs against these
11508 sections are handled correctly. */
11509 if ((o->flags & SEC_ALLOC) == 0
11510 && ! o->user_set_vma)
11511 o->vma = 0;
11512 }
11513
11514 if (! bfd_link_relocatable (info) && merged)
11515 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
11516
11517 /* Figure out the file positions for everything but the symbol table
11518 and the relocs. We set symcount to force assign_section_numbers
11519 to create a symbol table. */
11520 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
11521 BFD_ASSERT (! abfd->output_has_begun);
11522 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11523 goto error_return;
11524
11525 /* Set sizes, and assign file positions for reloc sections. */
11526 for (o = abfd->sections; o != NULL; o = o->next)
11527 {
11528 struct bfd_elf_section_data *esdo = elf_section_data (o);
11529 if ((o->flags & SEC_RELOC) != 0)
11530 {
11531 if (esdo->rel.hdr
11532 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
11533 goto error_return;
11534
11535 if (esdo->rela.hdr
11536 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
11537 goto error_return;
11538 }
11539
11540 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11541 to count upwards while actually outputting the relocations. */
11542 esdo->rel.count = 0;
11543 esdo->rela.count = 0;
11544
11545 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11546 {
11547 /* Cache the section contents so that they can be compressed
11548 later. Use bfd_malloc since it will be freed by
11549 bfd_compress_section_contents. */
11550 unsigned char *contents = esdo->this_hdr.contents;
11551 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11552 abort ();
11553 contents
11554 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11555 if (contents == NULL)
11556 goto error_return;
11557 esdo->this_hdr.contents = contents;
11558 }
11559 }
11560
11561 /* We have now assigned file positions for all the sections except
11562 .symtab, .strtab, and non-loaded reloc sections. We start the
11563 .symtab section at the current file position, and write directly
11564 to it. We build the .strtab section in memory. */
11565 bfd_get_symcount (abfd) = 0;
11566 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11567 /* sh_name is set in prep_headers. */
11568 symtab_hdr->sh_type = SHT_SYMTAB;
11569 /* sh_flags, sh_addr and sh_size all start off zero. */
11570 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11571 /* sh_link is set in assign_section_numbers. */
11572 /* sh_info is set below. */
11573 /* sh_offset is set just below. */
11574 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
11575
11576 if (max_sym_count < 20)
11577 max_sym_count = 20;
11578 htab->strtabsize = max_sym_count;
11579 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11580 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
11581 if (htab->strtab == NULL)
11582 goto error_return;
11583 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11584 flinfo.symshndxbuf
11585 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11586 ? (Elf_External_Sym_Shndx *) -1 : NULL);
11587
11588 if (info->strip != strip_all || emit_relocs)
11589 {
11590 file_ptr off = elf_next_file_pos (abfd);
11591
11592 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11593
11594 /* Note that at this point elf_next_file_pos (abfd) is
11595 incorrect. We do not yet know the size of the .symtab section.
11596 We correct next_file_pos below, after we do know the size. */
11597
11598 /* Start writing out the symbol table. The first symbol is always a
11599 dummy symbol. */
11600 elfsym.st_value = 0;
11601 elfsym.st_size = 0;
11602 elfsym.st_info = 0;
11603 elfsym.st_other = 0;
11604 elfsym.st_shndx = SHN_UNDEF;
11605 elfsym.st_target_internal = 0;
11606 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11607 bfd_und_section_ptr, NULL) != 1)
11608 goto error_return;
11609
11610 /* Output a symbol for each section. We output these even if we are
11611 discarding local symbols, since they are used for relocs. These
11612 symbols have no names. We store the index of each one in the
11613 index field of the section, so that we can find it again when
11614 outputting relocs. */
11615
11616 elfsym.st_size = 0;
11617 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11618 elfsym.st_other = 0;
11619 elfsym.st_value = 0;
11620 elfsym.st_target_internal = 0;
11621 for (i = 1; i < elf_numsections (abfd); i++)
11622 {
11623 o = bfd_section_from_elf_index (abfd, i);
11624 if (o != NULL)
11625 {
11626 o->target_index = bfd_get_symcount (abfd);
11627 elfsym.st_shndx = i;
11628 if (!bfd_link_relocatable (info))
11629 elfsym.st_value = o->vma;
11630 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11631 NULL) != 1)
11632 goto error_return;
11633 }
11634 }
11635 }
11636
11637 /* Allocate some memory to hold information read in from the input
11638 files. */
11639 if (max_contents_size != 0)
11640 {
11641 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11642 if (flinfo.contents == NULL)
11643 goto error_return;
11644 }
11645
11646 if (max_external_reloc_size != 0)
11647 {
11648 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11649 if (flinfo.external_relocs == NULL)
11650 goto error_return;
11651 }
11652
11653 if (max_internal_reloc_count != 0)
11654 {
11655 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11656 amt *= sizeof (Elf_Internal_Rela);
11657 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11658 if (flinfo.internal_relocs == NULL)
11659 goto error_return;
11660 }
11661
11662 if (max_sym_count != 0)
11663 {
11664 amt = max_sym_count * bed->s->sizeof_sym;
11665 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11666 if (flinfo.external_syms == NULL)
11667 goto error_return;
11668
11669 amt = max_sym_count * sizeof (Elf_Internal_Sym);
11670 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11671 if (flinfo.internal_syms == NULL)
11672 goto error_return;
11673
11674 amt = max_sym_count * sizeof (long);
11675 flinfo.indices = (long int *) bfd_malloc (amt);
11676 if (flinfo.indices == NULL)
11677 goto error_return;
11678
11679 amt = max_sym_count * sizeof (asection *);
11680 flinfo.sections = (asection **) bfd_malloc (amt);
11681 if (flinfo.sections == NULL)
11682 goto error_return;
11683 }
11684
11685 if (max_sym_shndx_count != 0)
11686 {
11687 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
11688 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11689 if (flinfo.locsym_shndx == NULL)
11690 goto error_return;
11691 }
11692
11693 if (htab->tls_sec)
11694 {
11695 bfd_vma base, end = 0;
11696 asection *sec;
11697
11698 for (sec = htab->tls_sec;
11699 sec && (sec->flags & SEC_THREAD_LOCAL);
11700 sec = sec->next)
11701 {
11702 bfd_size_type size = sec->size;
11703
11704 if (size == 0
11705 && (sec->flags & SEC_HAS_CONTENTS) == 0)
11706 {
11707 struct bfd_link_order *ord = sec->map_tail.link_order;
11708
11709 if (ord != NULL)
11710 size = ord->offset + ord->size;
11711 }
11712 end = sec->vma + size;
11713 }
11714 base = htab->tls_sec->vma;
11715 /* Only align end of TLS section if static TLS doesn't have special
11716 alignment requirements. */
11717 if (bed->static_tls_alignment == 1)
11718 end = align_power (end, htab->tls_sec->alignment_power);
11719 htab->tls_size = end - base;
11720 }
11721
11722 /* Reorder SHF_LINK_ORDER sections. */
11723 for (o = abfd->sections; o != NULL; o = o->next)
11724 {
11725 if (!elf_fixup_link_order (abfd, o))
11726 return FALSE;
11727 }
11728
11729 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11730 return FALSE;
11731
11732 /* Since ELF permits relocations to be against local symbols, we
11733 must have the local symbols available when we do the relocations.
11734 Since we would rather only read the local symbols once, and we
11735 would rather not keep them in memory, we handle all the
11736 relocations for a single input file at the same time.
11737
11738 Unfortunately, there is no way to know the total number of local
11739 symbols until we have seen all of them, and the local symbol
11740 indices precede the global symbol indices. This means that when
11741 we are generating relocatable output, and we see a reloc against
11742 a global symbol, we can not know the symbol index until we have
11743 finished examining all the local symbols to see which ones we are
11744 going to output. To deal with this, we keep the relocations in
11745 memory, and don't output them until the end of the link. This is
11746 an unfortunate waste of memory, but I don't see a good way around
11747 it. Fortunately, it only happens when performing a relocatable
11748 link, which is not the common case. FIXME: If keep_memory is set
11749 we could write the relocs out and then read them again; I don't
11750 know how bad the memory loss will be. */
11751
11752 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11753 sub->output_has_begun = FALSE;
11754 for (o = abfd->sections; o != NULL; o = o->next)
11755 {
11756 for (p = o->map_head.link_order; p != NULL; p = p->next)
11757 {
11758 if (p->type == bfd_indirect_link_order
11759 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11760 == bfd_target_elf_flavour)
11761 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11762 {
11763 if (! sub->output_has_begun)
11764 {
11765 if (! elf_link_input_bfd (&flinfo, sub))
11766 goto error_return;
11767 sub->output_has_begun = TRUE;
11768 }
11769 }
11770 else if (p->type == bfd_section_reloc_link_order
11771 || p->type == bfd_symbol_reloc_link_order)
11772 {
11773 if (! elf_reloc_link_order (abfd, info, o, p))
11774 goto error_return;
11775 }
11776 else
11777 {
11778 if (! _bfd_default_link_order (abfd, info, o, p))
11779 {
11780 if (p->type == bfd_indirect_link_order
11781 && (bfd_get_flavour (sub)
11782 == bfd_target_elf_flavour)
11783 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11784 != bed->s->elfclass))
11785 {
11786 const char *iclass, *oclass;
11787
11788 switch (bed->s->elfclass)
11789 {
11790 case ELFCLASS64: oclass = "ELFCLASS64"; break;
11791 case ELFCLASS32: oclass = "ELFCLASS32"; break;
11792 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
11793 default: abort ();
11794 }
11795
11796 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
11797 {
11798 case ELFCLASS64: iclass = "ELFCLASS64"; break;
11799 case ELFCLASS32: iclass = "ELFCLASS32"; break;
11800 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
11801 default: abort ();
11802 }
11803
11804 bfd_set_error (bfd_error_wrong_format);
11805 _bfd_error_handler
11806 /* xgettext:c-format */
11807 (_("%B: file class %s incompatible with %s"),
11808 sub, iclass, oclass);
11809 }
11810
11811 goto error_return;
11812 }
11813 }
11814 }
11815 }
11816
11817 /* Free symbol buffer if needed. */
11818 if (!info->reduce_memory_overheads)
11819 {
11820 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11821 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11822 && elf_tdata (sub)->symbuf)
11823 {
11824 free (elf_tdata (sub)->symbuf);
11825 elf_tdata (sub)->symbuf = NULL;
11826 }
11827 }
11828
11829 /* Output any global symbols that got converted to local in a
11830 version script or due to symbol visibility. We do this in a
11831 separate step since ELF requires all local symbols to appear
11832 prior to any global symbols. FIXME: We should only do this if
11833 some global symbols were, in fact, converted to become local.
11834 FIXME: Will this work correctly with the Irix 5 linker? */
11835 eoinfo.failed = FALSE;
11836 eoinfo.flinfo = &flinfo;
11837 eoinfo.localsyms = TRUE;
11838 eoinfo.file_sym_done = FALSE;
11839 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11840 if (eoinfo.failed)
11841 return FALSE;
11842
11843 /* If backend needs to output some local symbols not present in the hash
11844 table, do it now. */
11845 if (bed->elf_backend_output_arch_local_syms
11846 && (info->strip != strip_all || emit_relocs))
11847 {
11848 typedef int (*out_sym_func)
11849 (void *, const char *, Elf_Internal_Sym *, asection *,
11850 struct elf_link_hash_entry *);
11851
11852 if (! ((*bed->elf_backend_output_arch_local_syms)
11853 (abfd, info, &flinfo,
11854 (out_sym_func) elf_link_output_symstrtab)))
11855 return FALSE;
11856 }
11857
11858 /* That wrote out all the local symbols. Finish up the symbol table
11859 with the global symbols. Even if we want to strip everything we
11860 can, we still need to deal with those global symbols that got
11861 converted to local in a version script. */
11862
11863 /* The sh_info field records the index of the first non local symbol. */
11864 symtab_hdr->sh_info = bfd_get_symcount (abfd);
11865
11866 if (dynamic
11867 && htab->dynsym != NULL
11868 && htab->dynsym->output_section != bfd_abs_section_ptr)
11869 {
11870 Elf_Internal_Sym sym;
11871 bfd_byte *dynsym = htab->dynsym->contents;
11872
11873 o = htab->dynsym->output_section;
11874 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
11875
11876 /* Write out the section symbols for the output sections. */
11877 if (bfd_link_pic (info)
11878 || htab->is_relocatable_executable)
11879 {
11880 asection *s;
11881
11882 sym.st_size = 0;
11883 sym.st_name = 0;
11884 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11885 sym.st_other = 0;
11886 sym.st_target_internal = 0;
11887
11888 for (s = abfd->sections; s != NULL; s = s->next)
11889 {
11890 int indx;
11891 bfd_byte *dest;
11892 long dynindx;
11893
11894 dynindx = elf_section_data (s)->dynindx;
11895 if (dynindx <= 0)
11896 continue;
11897 indx = elf_section_data (s)->this_idx;
11898 BFD_ASSERT (indx > 0);
11899 sym.st_shndx = indx;
11900 if (! check_dynsym (abfd, &sym))
11901 return FALSE;
11902 sym.st_value = s->vma;
11903 dest = dynsym + dynindx * bed->s->sizeof_sym;
11904 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11905 }
11906 }
11907
11908 /* Write out the local dynsyms. */
11909 if (htab->dynlocal)
11910 {
11911 struct elf_link_local_dynamic_entry *e;
11912 for (e = htab->dynlocal; e ; e = e->next)
11913 {
11914 asection *s;
11915 bfd_byte *dest;
11916
11917 /* Copy the internal symbol and turn off visibility.
11918 Note that we saved a word of storage and overwrote
11919 the original st_name with the dynstr_index. */
11920 sym = e->isym;
11921 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
11922
11923 s = bfd_section_from_elf_index (e->input_bfd,
11924 e->isym.st_shndx);
11925 if (s != NULL)
11926 {
11927 sym.st_shndx =
11928 elf_section_data (s->output_section)->this_idx;
11929 if (! check_dynsym (abfd, &sym))
11930 return FALSE;
11931 sym.st_value = (s->output_section->vma
11932 + s->output_offset
11933 + e->isym.st_value);
11934 }
11935
11936 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
11937 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11938 }
11939 }
11940 }
11941
11942 /* We get the global symbols from the hash table. */
11943 eoinfo.failed = FALSE;
11944 eoinfo.localsyms = FALSE;
11945 eoinfo.flinfo = &flinfo;
11946 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11947 if (eoinfo.failed)
11948 return FALSE;
11949
11950 /* If backend needs to output some symbols not present in the hash
11951 table, do it now. */
11952 if (bed->elf_backend_output_arch_syms
11953 && (info->strip != strip_all || emit_relocs))
11954 {
11955 typedef int (*out_sym_func)
11956 (void *, const char *, Elf_Internal_Sym *, asection *,
11957 struct elf_link_hash_entry *);
11958
11959 if (! ((*bed->elf_backend_output_arch_syms)
11960 (abfd, info, &flinfo,
11961 (out_sym_func) elf_link_output_symstrtab)))
11962 return FALSE;
11963 }
11964
11965 /* Finalize the .strtab section. */
11966 _bfd_elf_strtab_finalize (flinfo.symstrtab);
11967
11968 /* Swap out the .strtab section. */
11969 if (!elf_link_swap_symbols_out (&flinfo))
11970 return FALSE;
11971
11972 /* Now we know the size of the symtab section. */
11973 if (bfd_get_symcount (abfd) > 0)
11974 {
11975 /* Finish up and write out the symbol string table (.strtab)
11976 section. */
11977 Elf_Internal_Shdr *symstrtab_hdr;
11978 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
11979
11980 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
11981 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
11982 {
11983 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
11984 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
11985 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
11986 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
11987 symtab_shndx_hdr->sh_size = amt;
11988
11989 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
11990 off, TRUE);
11991
11992 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
11993 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
11994 return FALSE;
11995 }
11996
11997 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
11998 /* sh_name was set in prep_headers. */
11999 symstrtab_hdr->sh_type = SHT_STRTAB;
12000 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
12001 symstrtab_hdr->sh_addr = 0;
12002 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
12003 symstrtab_hdr->sh_entsize = 0;
12004 symstrtab_hdr->sh_link = 0;
12005 symstrtab_hdr->sh_info = 0;
12006 /* sh_offset is set just below. */
12007 symstrtab_hdr->sh_addralign = 1;
12008
12009 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12010 off, TRUE);
12011 elf_next_file_pos (abfd) = off;
12012
12013 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12014 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12015 return FALSE;
12016 }
12017
12018 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12019 {
12020 _bfd_error_handler (_("%B: failed to generate import library"),
12021 info->out_implib_bfd);
12022 return FALSE;
12023 }
12024
12025 /* Adjust the relocs to have the correct symbol indices. */
12026 for (o = abfd->sections; o != NULL; o = o->next)
12027 {
12028 struct bfd_elf_section_data *esdo = elf_section_data (o);
12029 bfd_boolean sort;
12030 if ((o->flags & SEC_RELOC) == 0)
12031 continue;
12032
12033 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12034 if (esdo->rel.hdr != NULL
12035 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort))
12036 return FALSE;
12037 if (esdo->rela.hdr != NULL
12038 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort))
12039 return FALSE;
12040
12041 /* Set the reloc_count field to 0 to prevent write_relocs from
12042 trying to swap the relocs out itself. */
12043 o->reloc_count = 0;
12044 }
12045
12046 if (dynamic && info->combreloc && dynobj != NULL)
12047 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12048
12049 /* If we are linking against a dynamic object, or generating a
12050 shared library, finish up the dynamic linking information. */
12051 if (dynamic)
12052 {
12053 bfd_byte *dyncon, *dynconend;
12054
12055 /* Fix up .dynamic entries. */
12056 o = bfd_get_linker_section (dynobj, ".dynamic");
12057 BFD_ASSERT (o != NULL);
12058
12059 dyncon = o->contents;
12060 dynconend = o->contents + o->size;
12061 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12062 {
12063 Elf_Internal_Dyn dyn;
12064 const char *name;
12065 unsigned int type;
12066
12067 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12068
12069 switch (dyn.d_tag)
12070 {
12071 default:
12072 continue;
12073 case DT_NULL:
12074 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12075 {
12076 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12077 {
12078 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12079 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12080 default: continue;
12081 }
12082 dyn.d_un.d_val = relativecount;
12083 relativecount = 0;
12084 break;
12085 }
12086 continue;
12087
12088 case DT_INIT:
12089 name = info->init_function;
12090 goto get_sym;
12091 case DT_FINI:
12092 name = info->fini_function;
12093 get_sym:
12094 {
12095 struct elf_link_hash_entry *h;
12096
12097 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
12098 if (h != NULL
12099 && (h->root.type == bfd_link_hash_defined
12100 || h->root.type == bfd_link_hash_defweak))
12101 {
12102 dyn.d_un.d_ptr = h->root.u.def.value;
12103 o = h->root.u.def.section;
12104 if (o->output_section != NULL)
12105 dyn.d_un.d_ptr += (o->output_section->vma
12106 + o->output_offset);
12107 else
12108 {
12109 /* The symbol is imported from another shared
12110 library and does not apply to this one. */
12111 dyn.d_un.d_ptr = 0;
12112 }
12113 break;
12114 }
12115 }
12116 continue;
12117
12118 case DT_PREINIT_ARRAYSZ:
12119 name = ".preinit_array";
12120 goto get_out_size;
12121 case DT_INIT_ARRAYSZ:
12122 name = ".init_array";
12123 goto get_out_size;
12124 case DT_FINI_ARRAYSZ:
12125 name = ".fini_array";
12126 get_out_size:
12127 o = bfd_get_section_by_name (abfd, name);
12128 if (o == NULL)
12129 {
12130 _bfd_error_handler
12131 (_("could not find section %s"), name);
12132 goto error_return;
12133 }
12134 if (o->size == 0)
12135 _bfd_error_handler
12136 (_("warning: %s section has zero size"), name);
12137 dyn.d_un.d_val = o->size;
12138 break;
12139
12140 case DT_PREINIT_ARRAY:
12141 name = ".preinit_array";
12142 goto get_out_vma;
12143 case DT_INIT_ARRAY:
12144 name = ".init_array";
12145 goto get_out_vma;
12146 case DT_FINI_ARRAY:
12147 name = ".fini_array";
12148 get_out_vma:
12149 o = bfd_get_section_by_name (abfd, name);
12150 goto do_vma;
12151
12152 case DT_HASH:
12153 name = ".hash";
12154 goto get_vma;
12155 case DT_GNU_HASH:
12156 name = ".gnu.hash";
12157 goto get_vma;
12158 case DT_STRTAB:
12159 name = ".dynstr";
12160 goto get_vma;
12161 case DT_SYMTAB:
12162 name = ".dynsym";
12163 goto get_vma;
12164 case DT_VERDEF:
12165 name = ".gnu.version_d";
12166 goto get_vma;
12167 case DT_VERNEED:
12168 name = ".gnu.version_r";
12169 goto get_vma;
12170 case DT_VERSYM:
12171 name = ".gnu.version";
12172 get_vma:
12173 o = bfd_get_linker_section (dynobj, name);
12174 do_vma:
12175 if (o == NULL)
12176 {
12177 _bfd_error_handler
12178 (_("could not find section %s"), name);
12179 goto error_return;
12180 }
12181 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12182 {
12183 _bfd_error_handler
12184 (_("warning: section '%s' is being made into a note"), name);
12185 bfd_set_error (bfd_error_nonrepresentable_section);
12186 goto error_return;
12187 }
12188 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
12189 break;
12190
12191 case DT_REL:
12192 case DT_RELA:
12193 case DT_RELSZ:
12194 case DT_RELASZ:
12195 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12196 type = SHT_REL;
12197 else
12198 type = SHT_RELA;
12199 dyn.d_un.d_val = 0;
12200 dyn.d_un.d_ptr = 0;
12201 for (i = 1; i < elf_numsections (abfd); i++)
12202 {
12203 Elf_Internal_Shdr *hdr;
12204
12205 hdr = elf_elfsections (abfd)[i];
12206 if (hdr->sh_type == type
12207 && (hdr->sh_flags & SHF_ALLOC) != 0)
12208 {
12209 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12210 dyn.d_un.d_val += hdr->sh_size;
12211 else
12212 {
12213 if (dyn.d_un.d_ptr == 0
12214 || hdr->sh_addr < dyn.d_un.d_ptr)
12215 dyn.d_un.d_ptr = hdr->sh_addr;
12216 }
12217 }
12218 }
12219 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12220 {
12221 /* Don't count procedure linkage table relocs in the
12222 overall reloc count. */
12223 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12224 dyn.d_un.d_val -= htab->srelplt->size;
12225 /* If .rela.plt is the first .rela section, exclude
12226 it from DT_RELA. */
12227 else if (dyn.d_un.d_ptr == (htab->srelplt->output_section->vma
12228 + htab->srelplt->output_offset))
12229 dyn.d_un.d_ptr += htab->srelplt->size;
12230 }
12231 break;
12232 }
12233 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12234 }
12235 }
12236
12237 /* If we have created any dynamic sections, then output them. */
12238 if (dynobj != NULL)
12239 {
12240 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12241 goto error_return;
12242
12243 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12244 if (((info->warn_shared_textrel && bfd_link_pic (info))
12245 || info->error_textrel)
12246 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
12247 {
12248 bfd_byte *dyncon, *dynconend;
12249
12250 dyncon = o->contents;
12251 dynconend = o->contents + o->size;
12252 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12253 {
12254 Elf_Internal_Dyn dyn;
12255
12256 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12257
12258 if (dyn.d_tag == DT_TEXTREL)
12259 {
12260 if (info->error_textrel)
12261 info->callbacks->einfo
12262 (_("%P%X: read-only segment has dynamic relocations.\n"));
12263 else
12264 info->callbacks->einfo
12265 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
12266 break;
12267 }
12268 }
12269 }
12270
12271 for (o = dynobj->sections; o != NULL; o = o->next)
12272 {
12273 if ((o->flags & SEC_HAS_CONTENTS) == 0
12274 || o->size == 0
12275 || o->output_section == bfd_abs_section_ptr)
12276 continue;
12277 if ((o->flags & SEC_LINKER_CREATED) == 0)
12278 {
12279 /* At this point, we are only interested in sections
12280 created by _bfd_elf_link_create_dynamic_sections. */
12281 continue;
12282 }
12283 if (htab->stab_info.stabstr == o)
12284 continue;
12285 if (htab->eh_info.hdr_sec == o)
12286 continue;
12287 if (strcmp (o->name, ".dynstr") != 0)
12288 {
12289 if (! bfd_set_section_contents (abfd, o->output_section,
12290 o->contents,
12291 (file_ptr) o->output_offset
12292 * bfd_octets_per_byte (abfd),
12293 o->size))
12294 goto error_return;
12295 }
12296 else
12297 {
12298 /* The contents of the .dynstr section are actually in a
12299 stringtab. */
12300 file_ptr off;
12301
12302 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12303 if (bfd_seek (abfd, off, SEEK_SET) != 0
12304 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
12305 goto error_return;
12306 }
12307 }
12308 }
12309
12310 if (bfd_link_relocatable (info))
12311 {
12312 bfd_boolean failed = FALSE;
12313
12314 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12315 if (failed)
12316 goto error_return;
12317 }
12318
12319 /* If we have optimized stabs strings, output them. */
12320 if (htab->stab_info.stabstr != NULL)
12321 {
12322 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
12323 goto error_return;
12324 }
12325
12326 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12327 goto error_return;
12328
12329 elf_final_link_free (abfd, &flinfo);
12330
12331 elf_linker (abfd) = TRUE;
12332
12333 if (attr_section)
12334 {
12335 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
12336 if (contents == NULL)
12337 return FALSE; /* Bail out and fail. */
12338 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12339 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12340 free (contents);
12341 }
12342
12343 return TRUE;
12344
12345 error_return:
12346 elf_final_link_free (abfd, &flinfo);
12347 return FALSE;
12348 }
12349 \f
12350 /* Initialize COOKIE for input bfd ABFD. */
12351
12352 static bfd_boolean
12353 init_reloc_cookie (struct elf_reloc_cookie *cookie,
12354 struct bfd_link_info *info, bfd *abfd)
12355 {
12356 Elf_Internal_Shdr *symtab_hdr;
12357 const struct elf_backend_data *bed;
12358
12359 bed = get_elf_backend_data (abfd);
12360 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12361
12362 cookie->abfd = abfd;
12363 cookie->sym_hashes = elf_sym_hashes (abfd);
12364 cookie->bad_symtab = elf_bad_symtab (abfd);
12365 if (cookie->bad_symtab)
12366 {
12367 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12368 cookie->extsymoff = 0;
12369 }
12370 else
12371 {
12372 cookie->locsymcount = symtab_hdr->sh_info;
12373 cookie->extsymoff = symtab_hdr->sh_info;
12374 }
12375
12376 if (bed->s->arch_size == 32)
12377 cookie->r_sym_shift = 8;
12378 else
12379 cookie->r_sym_shift = 32;
12380
12381 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12382 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12383 {
12384 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12385 cookie->locsymcount, 0,
12386 NULL, NULL, NULL);
12387 if (cookie->locsyms == NULL)
12388 {
12389 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12390 return FALSE;
12391 }
12392 if (info->keep_memory)
12393 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12394 }
12395 return TRUE;
12396 }
12397
12398 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12399
12400 static void
12401 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12402 {
12403 Elf_Internal_Shdr *symtab_hdr;
12404
12405 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12406 if (cookie->locsyms != NULL
12407 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12408 free (cookie->locsyms);
12409 }
12410
12411 /* Initialize the relocation information in COOKIE for input section SEC
12412 of input bfd ABFD. */
12413
12414 static bfd_boolean
12415 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12416 struct bfd_link_info *info, bfd *abfd,
12417 asection *sec)
12418 {
12419 const struct elf_backend_data *bed;
12420
12421 if (sec->reloc_count == 0)
12422 {
12423 cookie->rels = NULL;
12424 cookie->relend = NULL;
12425 }
12426 else
12427 {
12428 bed = get_elf_backend_data (abfd);
12429
12430 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12431 info->keep_memory);
12432 if (cookie->rels == NULL)
12433 return FALSE;
12434 cookie->rel = cookie->rels;
12435 cookie->relend = (cookie->rels
12436 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
12437 }
12438 cookie->rel = cookie->rels;
12439 return TRUE;
12440 }
12441
12442 /* Free the memory allocated by init_reloc_cookie_rels,
12443 if appropriate. */
12444
12445 static void
12446 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12447 asection *sec)
12448 {
12449 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12450 free (cookie->rels);
12451 }
12452
12453 /* Initialize the whole of COOKIE for input section SEC. */
12454
12455 static bfd_boolean
12456 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12457 struct bfd_link_info *info,
12458 asection *sec)
12459 {
12460 if (!init_reloc_cookie (cookie, info, sec->owner))
12461 goto error1;
12462 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12463 goto error2;
12464 return TRUE;
12465
12466 error2:
12467 fini_reloc_cookie (cookie, sec->owner);
12468 error1:
12469 return FALSE;
12470 }
12471
12472 /* Free the memory allocated by init_reloc_cookie_for_section,
12473 if appropriate. */
12474
12475 static void
12476 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12477 asection *sec)
12478 {
12479 fini_reloc_cookie_rels (cookie, sec);
12480 fini_reloc_cookie (cookie, sec->owner);
12481 }
12482 \f
12483 /* Garbage collect unused sections. */
12484
12485 /* Default gc_mark_hook. */
12486
12487 asection *
12488 _bfd_elf_gc_mark_hook (asection *sec,
12489 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12490 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12491 struct elf_link_hash_entry *h,
12492 Elf_Internal_Sym *sym)
12493 {
12494 if (h != NULL)
12495 {
12496 switch (h->root.type)
12497 {
12498 case bfd_link_hash_defined:
12499 case bfd_link_hash_defweak:
12500 return h->root.u.def.section;
12501
12502 case bfd_link_hash_common:
12503 return h->root.u.c.p->section;
12504
12505 default:
12506 break;
12507 }
12508 }
12509 else
12510 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12511
12512 return NULL;
12513 }
12514
12515 /* For undefined __start_<name> and __stop_<name> symbols, return the
12516 first input section matching <name>. Return NULL otherwise. */
12517
12518 asection *
12519 _bfd_elf_is_start_stop (const struct bfd_link_info *info,
12520 struct elf_link_hash_entry *h)
12521 {
12522 asection *s;
12523 const char *sec_name;
12524
12525 if (h->root.type != bfd_link_hash_undefined
12526 && h->root.type != bfd_link_hash_undefweak)
12527 return NULL;
12528
12529 s = h->root.u.undef.section;
12530 if (s != NULL)
12531 {
12532 if (s == (asection *) 0 - 1)
12533 return NULL;
12534 return s;
12535 }
12536
12537 sec_name = NULL;
12538 if (strncmp (h->root.root.string, "__start_", 8) == 0)
12539 sec_name = h->root.root.string + 8;
12540 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
12541 sec_name = h->root.root.string + 7;
12542
12543 if (sec_name != NULL && *sec_name != '\0')
12544 {
12545 bfd *i;
12546
12547 for (i = info->input_bfds; i != NULL; i = i->link.next)
12548 {
12549 s = bfd_get_section_by_name (i, sec_name);
12550 if (s != NULL)
12551 {
12552 h->root.u.undef.section = s;
12553 break;
12554 }
12555 }
12556 }
12557
12558 if (s == NULL)
12559 h->root.u.undef.section = (asection *) 0 - 1;
12560
12561 return s;
12562 }
12563
12564 /* COOKIE->rel describes a relocation against section SEC, which is
12565 a section we've decided to keep. Return the section that contains
12566 the relocation symbol, or NULL if no section contains it. */
12567
12568 asection *
12569 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12570 elf_gc_mark_hook_fn gc_mark_hook,
12571 struct elf_reloc_cookie *cookie,
12572 bfd_boolean *start_stop)
12573 {
12574 unsigned long r_symndx;
12575 struct elf_link_hash_entry *h;
12576
12577 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
12578 if (r_symndx == STN_UNDEF)
12579 return NULL;
12580
12581 if (r_symndx >= cookie->locsymcount
12582 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12583 {
12584 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
12585 if (h == NULL)
12586 {
12587 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12588 sec->owner);
12589 return NULL;
12590 }
12591 while (h->root.type == bfd_link_hash_indirect
12592 || h->root.type == bfd_link_hash_warning)
12593 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12594 h->mark = 1;
12595 /* If this symbol is weak and there is a non-weak definition, we
12596 keep the non-weak definition because many backends put
12597 dynamic reloc info on the non-weak definition for code
12598 handling copy relocs. */
12599 if (h->u.weakdef != NULL)
12600 h->u.weakdef->mark = 1;
12601
12602 if (start_stop != NULL)
12603 {
12604 /* To work around a glibc bug, mark all XXX input sections
12605 when there is an as yet undefined reference to __start_XXX
12606 or __stop_XXX symbols. The linker will later define such
12607 symbols for orphan input sections that have a name
12608 representable as a C identifier. */
12609 asection *s = _bfd_elf_is_start_stop (info, h);
12610
12611 if (s != NULL)
12612 {
12613 *start_stop = !s->gc_mark;
12614 return s;
12615 }
12616 }
12617
12618 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12619 }
12620
12621 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12622 &cookie->locsyms[r_symndx]);
12623 }
12624
12625 /* COOKIE->rel describes a relocation against section SEC, which is
12626 a section we've decided to keep. Mark the section that contains
12627 the relocation symbol. */
12628
12629 bfd_boolean
12630 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12631 asection *sec,
12632 elf_gc_mark_hook_fn gc_mark_hook,
12633 struct elf_reloc_cookie *cookie)
12634 {
12635 asection *rsec;
12636 bfd_boolean start_stop = FALSE;
12637
12638 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12639 while (rsec != NULL)
12640 {
12641 if (!rsec->gc_mark)
12642 {
12643 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12644 || (rsec->owner->flags & DYNAMIC) != 0)
12645 rsec->gc_mark = 1;
12646 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12647 return FALSE;
12648 }
12649 if (!start_stop)
12650 break;
12651 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
12652 }
12653 return TRUE;
12654 }
12655
12656 /* The mark phase of garbage collection. For a given section, mark
12657 it and any sections in this section's group, and all the sections
12658 which define symbols to which it refers. */
12659
12660 bfd_boolean
12661 _bfd_elf_gc_mark (struct bfd_link_info *info,
12662 asection *sec,
12663 elf_gc_mark_hook_fn gc_mark_hook)
12664 {
12665 bfd_boolean ret;
12666 asection *group_sec, *eh_frame;
12667
12668 sec->gc_mark = 1;
12669
12670 /* Mark all the sections in the group. */
12671 group_sec = elf_section_data (sec)->next_in_group;
12672 if (group_sec && !group_sec->gc_mark)
12673 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
12674 return FALSE;
12675
12676 /* Look through the section relocs. */
12677 ret = TRUE;
12678 eh_frame = elf_eh_frame_section (sec->owner);
12679 if ((sec->flags & SEC_RELOC) != 0
12680 && sec->reloc_count > 0
12681 && sec != eh_frame)
12682 {
12683 struct elf_reloc_cookie cookie;
12684
12685 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12686 ret = FALSE;
12687 else
12688 {
12689 for (; cookie.rel < cookie.relend; cookie.rel++)
12690 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
12691 {
12692 ret = FALSE;
12693 break;
12694 }
12695 fini_reloc_cookie_for_section (&cookie, sec);
12696 }
12697 }
12698
12699 if (ret && eh_frame && elf_fde_list (sec))
12700 {
12701 struct elf_reloc_cookie cookie;
12702
12703 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12704 ret = FALSE;
12705 else
12706 {
12707 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12708 gc_mark_hook, &cookie))
12709 ret = FALSE;
12710 fini_reloc_cookie_for_section (&cookie, eh_frame);
12711 }
12712 }
12713
12714 eh_frame = elf_section_eh_frame_entry (sec);
12715 if (ret && eh_frame && !eh_frame->gc_mark)
12716 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12717 ret = FALSE;
12718
12719 return ret;
12720 }
12721
12722 /* Scan and mark sections in a special or debug section group. */
12723
12724 static void
12725 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12726 {
12727 /* Point to first section of section group. */
12728 asection *ssec;
12729 /* Used to iterate the section group. */
12730 asection *msec;
12731
12732 bfd_boolean is_special_grp = TRUE;
12733 bfd_boolean is_debug_grp = TRUE;
12734
12735 /* First scan to see if group contains any section other than debug
12736 and special section. */
12737 ssec = msec = elf_next_in_group (grp);
12738 do
12739 {
12740 if ((msec->flags & SEC_DEBUGGING) == 0)
12741 is_debug_grp = FALSE;
12742
12743 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12744 is_special_grp = FALSE;
12745
12746 msec = elf_next_in_group (msec);
12747 }
12748 while (msec != ssec);
12749
12750 /* If this is a pure debug section group or pure special section group,
12751 keep all sections in this group. */
12752 if (is_debug_grp || is_special_grp)
12753 {
12754 do
12755 {
12756 msec->gc_mark = 1;
12757 msec = elf_next_in_group (msec);
12758 }
12759 while (msec != ssec);
12760 }
12761 }
12762
12763 /* Keep debug and special sections. */
12764
12765 bfd_boolean
12766 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12767 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12768 {
12769 bfd *ibfd;
12770
12771 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12772 {
12773 asection *isec;
12774 bfd_boolean some_kept;
12775 bfd_boolean debug_frag_seen;
12776
12777 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12778 continue;
12779
12780 /* Ensure all linker created sections are kept,
12781 see if any other section is already marked,
12782 and note if we have any fragmented debug sections. */
12783 debug_frag_seen = some_kept = FALSE;
12784 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12785 {
12786 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12787 isec->gc_mark = 1;
12788 else if (isec->gc_mark)
12789 some_kept = TRUE;
12790
12791 if (debug_frag_seen == FALSE
12792 && (isec->flags & SEC_DEBUGGING)
12793 && CONST_STRNEQ (isec->name, ".debug_line."))
12794 debug_frag_seen = TRUE;
12795 }
12796
12797 /* If no section in this file will be kept, then we can
12798 toss out the debug and special sections. */
12799 if (!some_kept)
12800 continue;
12801
12802 /* Keep debug and special sections like .comment when they are
12803 not part of a group. Also keep section groups that contain
12804 just debug sections or special sections. */
12805 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12806 {
12807 if ((isec->flags & SEC_GROUP) != 0)
12808 _bfd_elf_gc_mark_debug_special_section_group (isec);
12809 else if (((isec->flags & SEC_DEBUGGING) != 0
12810 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12811 && elf_next_in_group (isec) == NULL)
12812 isec->gc_mark = 1;
12813 }
12814
12815 if (! debug_frag_seen)
12816 continue;
12817
12818 /* Look for CODE sections which are going to be discarded,
12819 and find and discard any fragmented debug sections which
12820 are associated with that code section. */
12821 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12822 if ((isec->flags & SEC_CODE) != 0
12823 && isec->gc_mark == 0)
12824 {
12825 unsigned int ilen;
12826 asection *dsec;
12827
12828 ilen = strlen (isec->name);
12829
12830 /* Association is determined by the name of the debug section
12831 containing the name of the code section as a suffix. For
12832 example .debug_line.text.foo is a debug section associated
12833 with .text.foo. */
12834 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12835 {
12836 unsigned int dlen;
12837
12838 if (dsec->gc_mark == 0
12839 || (dsec->flags & SEC_DEBUGGING) == 0)
12840 continue;
12841
12842 dlen = strlen (dsec->name);
12843
12844 if (dlen > ilen
12845 && strncmp (dsec->name + (dlen - ilen),
12846 isec->name, ilen) == 0)
12847 {
12848 dsec->gc_mark = 0;
12849 }
12850 }
12851 }
12852 }
12853 return TRUE;
12854 }
12855
12856 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
12857
12858 struct elf_gc_sweep_symbol_info
12859 {
12860 struct bfd_link_info *info;
12861 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
12862 bfd_boolean);
12863 };
12864
12865 static bfd_boolean
12866 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
12867 {
12868 if (!h->mark
12869 && (((h->root.type == bfd_link_hash_defined
12870 || h->root.type == bfd_link_hash_defweak)
12871 && !((h->def_regular || ELF_COMMON_DEF_P (h))
12872 && h->root.u.def.section->gc_mark))
12873 || h->root.type == bfd_link_hash_undefined
12874 || h->root.type == bfd_link_hash_undefweak))
12875 {
12876 struct elf_gc_sweep_symbol_info *inf;
12877
12878 inf = (struct elf_gc_sweep_symbol_info *) data;
12879 (*inf->hide_symbol) (inf->info, h, TRUE);
12880 h->def_regular = 0;
12881 h->ref_regular = 0;
12882 h->ref_regular_nonweak = 0;
12883 }
12884
12885 return TRUE;
12886 }
12887
12888 /* The sweep phase of garbage collection. Remove all garbage sections. */
12889
12890 typedef bfd_boolean (*gc_sweep_hook_fn)
12891 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
12892
12893 static bfd_boolean
12894 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
12895 {
12896 bfd *sub;
12897 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12898 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
12899 unsigned long section_sym_count;
12900 struct elf_gc_sweep_symbol_info sweep_info;
12901
12902 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12903 {
12904 asection *o;
12905
12906 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12907 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
12908 continue;
12909
12910 for (o = sub->sections; o != NULL; o = o->next)
12911 {
12912 /* When any section in a section group is kept, we keep all
12913 sections in the section group. If the first member of
12914 the section group is excluded, we will also exclude the
12915 group section. */
12916 if (o->flags & SEC_GROUP)
12917 {
12918 asection *first = elf_next_in_group (o);
12919 o->gc_mark = first->gc_mark;
12920 }
12921
12922 if (o->gc_mark)
12923 continue;
12924
12925 /* Skip sweeping sections already excluded. */
12926 if (o->flags & SEC_EXCLUDE)
12927 continue;
12928
12929 /* Since this is early in the link process, it is simple
12930 to remove a section from the output. */
12931 o->flags |= SEC_EXCLUDE;
12932
12933 if (info->print_gc_sections && o->size != 0)
12934 /* xgettext:c-format */
12935 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
12936
12937 /* But we also have to update some of the relocation
12938 info we collected before. */
12939 if (gc_sweep_hook
12940 && (o->flags & SEC_RELOC) != 0
12941 && o->reloc_count != 0
12942 && !((info->strip == strip_all || info->strip == strip_debugger)
12943 && (o->flags & SEC_DEBUGGING) != 0)
12944 && !bfd_is_abs_section (o->output_section))
12945 {
12946 Elf_Internal_Rela *internal_relocs;
12947 bfd_boolean r;
12948
12949 internal_relocs
12950 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
12951 info->keep_memory);
12952 if (internal_relocs == NULL)
12953 return FALSE;
12954
12955 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
12956
12957 if (elf_section_data (o)->relocs != internal_relocs)
12958 free (internal_relocs);
12959
12960 if (!r)
12961 return FALSE;
12962 }
12963 }
12964 }
12965
12966 /* Remove the symbols that were in the swept sections from the dynamic
12967 symbol table. GCFIXME: Anyone know how to get them out of the
12968 static symbol table as well? */
12969 sweep_info.info = info;
12970 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
12971 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
12972 &sweep_info);
12973
12974 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
12975 return TRUE;
12976 }
12977
12978 /* Propagate collected vtable information. This is called through
12979 elf_link_hash_traverse. */
12980
12981 static bfd_boolean
12982 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
12983 {
12984 /* Those that are not vtables. */
12985 if (h->vtable == NULL || h->vtable->parent == NULL)
12986 return TRUE;
12987
12988 /* Those vtables that do not have parents, we cannot merge. */
12989 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
12990 return TRUE;
12991
12992 /* If we've already been done, exit. */
12993 if (h->vtable->used && h->vtable->used[-1])
12994 return TRUE;
12995
12996 /* Make sure the parent's table is up to date. */
12997 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
12998
12999 if (h->vtable->used == NULL)
13000 {
13001 /* None of this table's entries were referenced. Re-use the
13002 parent's table. */
13003 h->vtable->used = h->vtable->parent->vtable->used;
13004 h->vtable->size = h->vtable->parent->vtable->size;
13005 }
13006 else
13007 {
13008 size_t n;
13009 bfd_boolean *cu, *pu;
13010
13011 /* Or the parent's entries into ours. */
13012 cu = h->vtable->used;
13013 cu[-1] = TRUE;
13014 pu = h->vtable->parent->vtable->used;
13015 if (pu != NULL)
13016 {
13017 const struct elf_backend_data *bed;
13018 unsigned int log_file_align;
13019
13020 bed = get_elf_backend_data (h->root.u.def.section->owner);
13021 log_file_align = bed->s->log_file_align;
13022 n = h->vtable->parent->vtable->size >> log_file_align;
13023 while (n--)
13024 {
13025 if (*pu)
13026 *cu = TRUE;
13027 pu++;
13028 cu++;
13029 }
13030 }
13031 }
13032
13033 return TRUE;
13034 }
13035
13036 static bfd_boolean
13037 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13038 {
13039 asection *sec;
13040 bfd_vma hstart, hend;
13041 Elf_Internal_Rela *relstart, *relend, *rel;
13042 const struct elf_backend_data *bed;
13043 unsigned int log_file_align;
13044
13045 /* Take care of both those symbols that do not describe vtables as
13046 well as those that are not loaded. */
13047 if (h->vtable == NULL || h->vtable->parent == NULL)
13048 return TRUE;
13049
13050 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13051 || h->root.type == bfd_link_hash_defweak);
13052
13053 sec = h->root.u.def.section;
13054 hstart = h->root.u.def.value;
13055 hend = hstart + h->size;
13056
13057 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13058 if (!relstart)
13059 return *(bfd_boolean *) okp = FALSE;
13060 bed = get_elf_backend_data (sec->owner);
13061 log_file_align = bed->s->log_file_align;
13062
13063 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
13064
13065 for (rel = relstart; rel < relend; ++rel)
13066 if (rel->r_offset >= hstart && rel->r_offset < hend)
13067 {
13068 /* If the entry is in use, do nothing. */
13069 if (h->vtable->used
13070 && (rel->r_offset - hstart) < h->vtable->size)
13071 {
13072 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13073 if (h->vtable->used[entry])
13074 continue;
13075 }
13076 /* Otherwise, kill it. */
13077 rel->r_offset = rel->r_info = rel->r_addend = 0;
13078 }
13079
13080 return TRUE;
13081 }
13082
13083 /* Mark sections containing dynamically referenced symbols. When
13084 building shared libraries, we must assume that any visible symbol is
13085 referenced. */
13086
13087 bfd_boolean
13088 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
13089 {
13090 struct bfd_link_info *info = (struct bfd_link_info *) inf;
13091 struct bfd_elf_dynamic_list *d = info->dynamic_list;
13092
13093 if ((h->root.type == bfd_link_hash_defined
13094 || h->root.type == bfd_link_hash_defweak)
13095 && (h->ref_dynamic
13096 || ((h->def_regular || ELF_COMMON_DEF_P (h))
13097 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
13098 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
13099 && (!bfd_link_executable (info)
13100 || info->gc_keep_exported
13101 || info->export_dynamic
13102 || (h->dynamic
13103 && d != NULL
13104 && (*d->match) (&d->head, NULL, h->root.root.string)))
13105 && (h->versioned >= versioned
13106 || !bfd_hide_sym_by_version (info->version_info,
13107 h->root.root.string)))))
13108 h->root.u.def.section->flags |= SEC_KEEP;
13109
13110 return TRUE;
13111 }
13112
13113 /* Keep all sections containing symbols undefined on the command-line,
13114 and the section containing the entry symbol. */
13115
13116 void
13117 _bfd_elf_gc_keep (struct bfd_link_info *info)
13118 {
13119 struct bfd_sym_chain *sym;
13120
13121 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13122 {
13123 struct elf_link_hash_entry *h;
13124
13125 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13126 FALSE, FALSE, FALSE);
13127
13128 if (h != NULL
13129 && (h->root.type == bfd_link_hash_defined
13130 || h->root.type == bfd_link_hash_defweak)
13131 && !bfd_is_abs_section (h->root.u.def.section)
13132 && !bfd_is_und_section (h->root.u.def.section))
13133 h->root.u.def.section->flags |= SEC_KEEP;
13134 }
13135 }
13136
13137 bfd_boolean
13138 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13139 struct bfd_link_info *info)
13140 {
13141 bfd *ibfd = info->input_bfds;
13142
13143 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13144 {
13145 asection *sec;
13146 struct elf_reloc_cookie cookie;
13147
13148 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13149 continue;
13150
13151 if (!init_reloc_cookie (&cookie, info, ibfd))
13152 return FALSE;
13153
13154 for (sec = ibfd->sections; sec; sec = sec->next)
13155 {
13156 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13157 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13158 {
13159 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13160 fini_reloc_cookie_rels (&cookie, sec);
13161 }
13162 }
13163 }
13164 return TRUE;
13165 }
13166
13167 /* Do mark and sweep of unused sections. */
13168
13169 bfd_boolean
13170 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13171 {
13172 bfd_boolean ok = TRUE;
13173 bfd *sub;
13174 elf_gc_mark_hook_fn gc_mark_hook;
13175 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13176 struct elf_link_hash_table *htab;
13177
13178 if (!bed->can_gc_sections
13179 || !is_elf_hash_table (info->hash))
13180 {
13181 _bfd_error_handler(_("Warning: gc-sections option ignored"));
13182 return TRUE;
13183 }
13184
13185 bed->gc_keep (info);
13186 htab = elf_hash_table (info);
13187
13188 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13189 at the .eh_frame section if we can mark the FDEs individually. */
13190 for (sub = info->input_bfds;
13191 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13192 sub = sub->link.next)
13193 {
13194 asection *sec;
13195 struct elf_reloc_cookie cookie;
13196
13197 sec = bfd_get_section_by_name (sub, ".eh_frame");
13198 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
13199 {
13200 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
13201 if (elf_section_data (sec)->sec_info
13202 && (sec->flags & SEC_LINKER_CREATED) == 0)
13203 elf_eh_frame_section (sub) = sec;
13204 fini_reloc_cookie_for_section (&cookie, sec);
13205 sec = bfd_get_next_section_by_name (NULL, sec);
13206 }
13207 }
13208
13209 /* Apply transitive closure to the vtable entry usage info. */
13210 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
13211 if (!ok)
13212 return FALSE;
13213
13214 /* Kill the vtable relocations that were not used. */
13215 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
13216 if (!ok)
13217 return FALSE;
13218
13219 /* Mark dynamically referenced symbols. */
13220 if (htab->dynamic_sections_created || info->gc_keep_exported)
13221 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
13222
13223 /* Grovel through relocs to find out who stays ... */
13224 gc_mark_hook = bed->gc_mark_hook;
13225 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13226 {
13227 asection *o;
13228
13229 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13230 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13231 continue;
13232
13233 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13234 Also treat note sections as a root, if the section is not part
13235 of a group. */
13236 for (o = sub->sections; o != NULL; o = o->next)
13237 if (!o->gc_mark
13238 && (o->flags & SEC_EXCLUDE) == 0
13239 && ((o->flags & SEC_KEEP) != 0
13240 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13241 && elf_next_in_group (o) == NULL )))
13242 {
13243 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13244 return FALSE;
13245 }
13246 }
13247
13248 /* Allow the backend to mark additional target specific sections. */
13249 bed->gc_mark_extra_sections (info, gc_mark_hook);
13250
13251 /* ... and mark SEC_EXCLUDE for those that go. */
13252 return elf_gc_sweep (abfd, info);
13253 }
13254 \f
13255 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13256
13257 bfd_boolean
13258 bfd_elf_gc_record_vtinherit (bfd *abfd,
13259 asection *sec,
13260 struct elf_link_hash_entry *h,
13261 bfd_vma offset)
13262 {
13263 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13264 struct elf_link_hash_entry **search, *child;
13265 size_t extsymcount;
13266 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13267
13268 /* The sh_info field of the symtab header tells us where the
13269 external symbols start. We don't care about the local symbols at
13270 this point. */
13271 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13272 if (!elf_bad_symtab (abfd))
13273 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13274
13275 sym_hashes = elf_sym_hashes (abfd);
13276 sym_hashes_end = sym_hashes + extsymcount;
13277
13278 /* Hunt down the child symbol, which is in this section at the same
13279 offset as the relocation. */
13280 for (search = sym_hashes; search != sym_hashes_end; ++search)
13281 {
13282 if ((child = *search) != NULL
13283 && (child->root.type == bfd_link_hash_defined
13284 || child->root.type == bfd_link_hash_defweak)
13285 && child->root.u.def.section == sec
13286 && child->root.u.def.value == offset)
13287 goto win;
13288 }
13289
13290 /* xgettext:c-format */
13291 _bfd_error_handler (_("%B: %A+%lu: No symbol found for INHERIT"),
13292 abfd, sec, (unsigned long) offset);
13293 bfd_set_error (bfd_error_invalid_operation);
13294 return FALSE;
13295
13296 win:
13297 if (!child->vtable)
13298 {
13299 child->vtable = ((struct elf_link_virtual_table_entry *)
13300 bfd_zalloc (abfd, sizeof (*child->vtable)));
13301 if (!child->vtable)
13302 return FALSE;
13303 }
13304 if (!h)
13305 {
13306 /* This *should* only be the absolute section. It could potentially
13307 be that someone has defined a non-global vtable though, which
13308 would be bad. It isn't worth paging in the local symbols to be
13309 sure though; that case should simply be handled by the assembler. */
13310
13311 child->vtable->parent = (struct elf_link_hash_entry *) -1;
13312 }
13313 else
13314 child->vtable->parent = h;
13315
13316 return TRUE;
13317 }
13318
13319 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13320
13321 bfd_boolean
13322 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13323 asection *sec ATTRIBUTE_UNUSED,
13324 struct elf_link_hash_entry *h,
13325 bfd_vma addend)
13326 {
13327 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13328 unsigned int log_file_align = bed->s->log_file_align;
13329
13330 if (!h->vtable)
13331 {
13332 h->vtable = ((struct elf_link_virtual_table_entry *)
13333 bfd_zalloc (abfd, sizeof (*h->vtable)));
13334 if (!h->vtable)
13335 return FALSE;
13336 }
13337
13338 if (addend >= h->vtable->size)
13339 {
13340 size_t size, bytes, file_align;
13341 bfd_boolean *ptr = h->vtable->used;
13342
13343 /* While the symbol is undefined, we have to be prepared to handle
13344 a zero size. */
13345 file_align = 1 << log_file_align;
13346 if (h->root.type == bfd_link_hash_undefined)
13347 size = addend + file_align;
13348 else
13349 {
13350 size = h->size;
13351 if (addend >= size)
13352 {
13353 /* Oops! We've got a reference past the defined end of
13354 the table. This is probably a bug -- shall we warn? */
13355 size = addend + file_align;
13356 }
13357 }
13358 size = (size + file_align - 1) & -file_align;
13359
13360 /* Allocate one extra entry for use as a "done" flag for the
13361 consolidation pass. */
13362 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13363
13364 if (ptr)
13365 {
13366 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
13367
13368 if (ptr != NULL)
13369 {
13370 size_t oldbytes;
13371
13372 oldbytes = (((h->vtable->size >> log_file_align) + 1)
13373 * sizeof (bfd_boolean));
13374 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13375 }
13376 }
13377 else
13378 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
13379
13380 if (ptr == NULL)
13381 return FALSE;
13382
13383 /* And arrange for that done flag to be at index -1. */
13384 h->vtable->used = ptr + 1;
13385 h->vtable->size = size;
13386 }
13387
13388 h->vtable->used[addend >> log_file_align] = TRUE;
13389
13390 return TRUE;
13391 }
13392
13393 /* Map an ELF section header flag to its corresponding string. */
13394 typedef struct
13395 {
13396 char *flag_name;
13397 flagword flag_value;
13398 } elf_flags_to_name_table;
13399
13400 static elf_flags_to_name_table elf_flags_to_names [] =
13401 {
13402 { "SHF_WRITE", SHF_WRITE },
13403 { "SHF_ALLOC", SHF_ALLOC },
13404 { "SHF_EXECINSTR", SHF_EXECINSTR },
13405 { "SHF_MERGE", SHF_MERGE },
13406 { "SHF_STRINGS", SHF_STRINGS },
13407 { "SHF_INFO_LINK", SHF_INFO_LINK},
13408 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13409 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13410 { "SHF_GROUP", SHF_GROUP },
13411 { "SHF_TLS", SHF_TLS },
13412 { "SHF_MASKOS", SHF_MASKOS },
13413 { "SHF_EXCLUDE", SHF_EXCLUDE },
13414 };
13415
13416 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13417 bfd_boolean
13418 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
13419 struct flag_info *flaginfo,
13420 asection *section)
13421 {
13422 const bfd_vma sh_flags = elf_section_flags (section);
13423
13424 if (!flaginfo->flags_initialized)
13425 {
13426 bfd *obfd = info->output_bfd;
13427 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13428 struct flag_info_list *tf = flaginfo->flag_list;
13429 int with_hex = 0;
13430 int without_hex = 0;
13431
13432 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13433 {
13434 unsigned i;
13435 flagword (*lookup) (char *);
13436
13437 lookup = bed->elf_backend_lookup_section_flags_hook;
13438 if (lookup != NULL)
13439 {
13440 flagword hexval = (*lookup) ((char *) tf->name);
13441
13442 if (hexval != 0)
13443 {
13444 if (tf->with == with_flags)
13445 with_hex |= hexval;
13446 else if (tf->with == without_flags)
13447 without_hex |= hexval;
13448 tf->valid = TRUE;
13449 continue;
13450 }
13451 }
13452 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13453 {
13454 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
13455 {
13456 if (tf->with == with_flags)
13457 with_hex |= elf_flags_to_names[i].flag_value;
13458 else if (tf->with == without_flags)
13459 without_hex |= elf_flags_to_names[i].flag_value;
13460 tf->valid = TRUE;
13461 break;
13462 }
13463 }
13464 if (!tf->valid)
13465 {
13466 info->callbacks->einfo
13467 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
13468 return FALSE;
13469 }
13470 }
13471 flaginfo->flags_initialized = TRUE;
13472 flaginfo->only_with_flags |= with_hex;
13473 flaginfo->not_with_flags |= without_hex;
13474 }
13475
13476 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13477 return FALSE;
13478
13479 if ((flaginfo->not_with_flags & sh_flags) != 0)
13480 return FALSE;
13481
13482 return TRUE;
13483 }
13484
13485 struct alloc_got_off_arg {
13486 bfd_vma gotoff;
13487 struct bfd_link_info *info;
13488 };
13489
13490 /* We need a special top-level link routine to convert got reference counts
13491 to real got offsets. */
13492
13493 static bfd_boolean
13494 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13495 {
13496 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13497 bfd *obfd = gofarg->info->output_bfd;
13498 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13499
13500 if (h->got.refcount > 0)
13501 {
13502 h->got.offset = gofarg->gotoff;
13503 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13504 }
13505 else
13506 h->got.offset = (bfd_vma) -1;
13507
13508 return TRUE;
13509 }
13510
13511 /* And an accompanying bit to work out final got entry offsets once
13512 we're done. Should be called from final_link. */
13513
13514 bfd_boolean
13515 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13516 struct bfd_link_info *info)
13517 {
13518 bfd *i;
13519 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13520 bfd_vma gotoff;
13521 struct alloc_got_off_arg gofarg;
13522
13523 BFD_ASSERT (abfd == info->output_bfd);
13524
13525 if (! is_elf_hash_table (info->hash))
13526 return FALSE;
13527
13528 /* The GOT offset is relative to the .got section, but the GOT header is
13529 put into the .got.plt section, if the backend uses it. */
13530 if (bed->want_got_plt)
13531 gotoff = 0;
13532 else
13533 gotoff = bed->got_header_size;
13534
13535 /* Do the local .got entries first. */
13536 for (i = info->input_bfds; i; i = i->link.next)
13537 {
13538 bfd_signed_vma *local_got;
13539 size_t j, locsymcount;
13540 Elf_Internal_Shdr *symtab_hdr;
13541
13542 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13543 continue;
13544
13545 local_got = elf_local_got_refcounts (i);
13546 if (!local_got)
13547 continue;
13548
13549 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13550 if (elf_bad_symtab (i))
13551 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13552 else
13553 locsymcount = symtab_hdr->sh_info;
13554
13555 for (j = 0; j < locsymcount; ++j)
13556 {
13557 if (local_got[j] > 0)
13558 {
13559 local_got[j] = gotoff;
13560 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
13561 }
13562 else
13563 local_got[j] = (bfd_vma) -1;
13564 }
13565 }
13566
13567 /* Then the global .got entries. .plt refcounts are handled by
13568 adjust_dynamic_symbol */
13569 gofarg.gotoff = gotoff;
13570 gofarg.info = info;
13571 elf_link_hash_traverse (elf_hash_table (info),
13572 elf_gc_allocate_got_offsets,
13573 &gofarg);
13574 return TRUE;
13575 }
13576
13577 /* Many folk need no more in the way of final link than this, once
13578 got entry reference counting is enabled. */
13579
13580 bfd_boolean
13581 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13582 {
13583 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13584 return FALSE;
13585
13586 /* Invoke the regular ELF backend linker to do all the work. */
13587 return bfd_elf_final_link (abfd, info);
13588 }
13589
13590 bfd_boolean
13591 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13592 {
13593 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
13594
13595 if (rcookie->bad_symtab)
13596 rcookie->rel = rcookie->rels;
13597
13598 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13599 {
13600 unsigned long r_symndx;
13601
13602 if (! rcookie->bad_symtab)
13603 if (rcookie->rel->r_offset > offset)
13604 return FALSE;
13605 if (rcookie->rel->r_offset != offset)
13606 continue;
13607
13608 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
13609 if (r_symndx == STN_UNDEF)
13610 return TRUE;
13611
13612 if (r_symndx >= rcookie->locsymcount
13613 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13614 {
13615 struct elf_link_hash_entry *h;
13616
13617 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13618
13619 while (h->root.type == bfd_link_hash_indirect
13620 || h->root.type == bfd_link_hash_warning)
13621 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13622
13623 if ((h->root.type == bfd_link_hash_defined
13624 || h->root.type == bfd_link_hash_defweak)
13625 && (h->root.u.def.section->owner != rcookie->abfd
13626 || h->root.u.def.section->kept_section != NULL
13627 || discarded_section (h->root.u.def.section)))
13628 return TRUE;
13629 }
13630 else
13631 {
13632 /* It's not a relocation against a global symbol,
13633 but it could be a relocation against a local
13634 symbol for a discarded section. */
13635 asection *isec;
13636 Elf_Internal_Sym *isym;
13637
13638 /* Need to: get the symbol; get the section. */
13639 isym = &rcookie->locsyms[r_symndx];
13640 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
13641 if (isec != NULL
13642 && (isec->kept_section != NULL
13643 || discarded_section (isec)))
13644 return TRUE;
13645 }
13646 return FALSE;
13647 }
13648 return FALSE;
13649 }
13650
13651 /* Discard unneeded references to discarded sections.
13652 Returns -1 on error, 1 if any section's size was changed, 0 if
13653 nothing changed. This function assumes that the relocations are in
13654 sorted order, which is true for all known assemblers. */
13655
13656 int
13657 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13658 {
13659 struct elf_reloc_cookie cookie;
13660 asection *o;
13661 bfd *abfd;
13662 int changed = 0;
13663
13664 if (info->traditional_format
13665 || !is_elf_hash_table (info->hash))
13666 return 0;
13667
13668 o = bfd_get_section_by_name (output_bfd, ".stab");
13669 if (o != NULL)
13670 {
13671 asection *i;
13672
13673 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13674 {
13675 if (i->size == 0
13676 || i->reloc_count == 0
13677 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13678 continue;
13679
13680 abfd = i->owner;
13681 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13682 continue;
13683
13684 if (!init_reloc_cookie_for_section (&cookie, info, i))
13685 return -1;
13686
13687 if (_bfd_discard_section_stabs (abfd, i,
13688 elf_section_data (i)->sec_info,
13689 bfd_elf_reloc_symbol_deleted_p,
13690 &cookie))
13691 changed = 1;
13692
13693 fini_reloc_cookie_for_section (&cookie, i);
13694 }
13695 }
13696
13697 o = NULL;
13698 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13699 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
13700 if (o != NULL)
13701 {
13702 asection *i;
13703
13704 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13705 {
13706 if (i->size == 0)
13707 continue;
13708
13709 abfd = i->owner;
13710 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13711 continue;
13712
13713 if (!init_reloc_cookie_for_section (&cookie, info, i))
13714 return -1;
13715
13716 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13717 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
13718 bfd_elf_reloc_symbol_deleted_p,
13719 &cookie))
13720 changed = 1;
13721
13722 fini_reloc_cookie_for_section (&cookie, i);
13723 }
13724 }
13725
13726 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13727 {
13728 const struct elf_backend_data *bed;
13729
13730 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13731 continue;
13732
13733 bed = get_elf_backend_data (abfd);
13734
13735 if (bed->elf_backend_discard_info != NULL)
13736 {
13737 if (!init_reloc_cookie (&cookie, info, abfd))
13738 return -1;
13739
13740 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
13741 changed = 1;
13742
13743 fini_reloc_cookie (&cookie, abfd);
13744 }
13745 }
13746
13747 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13748 _bfd_elf_end_eh_frame_parsing (info);
13749
13750 if (info->eh_frame_hdr_type
13751 && !bfd_link_relocatable (info)
13752 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
13753 changed = 1;
13754
13755 return changed;
13756 }
13757
13758 bfd_boolean
13759 _bfd_elf_section_already_linked (bfd *abfd,
13760 asection *sec,
13761 struct bfd_link_info *info)
13762 {
13763 flagword flags;
13764 const char *name, *key;
13765 struct bfd_section_already_linked *l;
13766 struct bfd_section_already_linked_hash_entry *already_linked_list;
13767
13768 if (sec->output_section == bfd_abs_section_ptr)
13769 return FALSE;
13770
13771 flags = sec->flags;
13772
13773 /* Return if it isn't a linkonce section. A comdat group section
13774 also has SEC_LINK_ONCE set. */
13775 if ((flags & SEC_LINK_ONCE) == 0)
13776 return FALSE;
13777
13778 /* Don't put group member sections on our list of already linked
13779 sections. They are handled as a group via their group section. */
13780 if (elf_sec_group (sec) != NULL)
13781 return FALSE;
13782
13783 /* For a SHT_GROUP section, use the group signature as the key. */
13784 name = sec->name;
13785 if ((flags & SEC_GROUP) != 0
13786 && elf_next_in_group (sec) != NULL
13787 && elf_group_name (elf_next_in_group (sec)) != NULL)
13788 key = elf_group_name (elf_next_in_group (sec));
13789 else
13790 {
13791 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
13792 if (CONST_STRNEQ (name, ".gnu.linkonce.")
13793 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13794 key++;
13795 else
13796 /* Must be a user linkonce section that doesn't follow gcc's
13797 naming convention. In this case we won't be matching
13798 single member groups. */
13799 key = name;
13800 }
13801
13802 already_linked_list = bfd_section_already_linked_table_lookup (key);
13803
13804 for (l = already_linked_list->entry; l != NULL; l = l->next)
13805 {
13806 /* We may have 2 different types of sections on the list: group
13807 sections with a signature of <key> (<key> is some string),
13808 and linkonce sections named .gnu.linkonce.<type>.<key>.
13809 Match like sections. LTO plugin sections are an exception.
13810 They are always named .gnu.linkonce.t.<key> and match either
13811 type of section. */
13812 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13813 && ((flags & SEC_GROUP) != 0
13814 || strcmp (name, l->sec->name) == 0))
13815 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
13816 {
13817 /* The section has already been linked. See if we should
13818 issue a warning. */
13819 if (!_bfd_handle_already_linked (sec, l, info))
13820 return FALSE;
13821
13822 if (flags & SEC_GROUP)
13823 {
13824 asection *first = elf_next_in_group (sec);
13825 asection *s = first;
13826
13827 while (s != NULL)
13828 {
13829 s->output_section = bfd_abs_section_ptr;
13830 /* Record which group discards it. */
13831 s->kept_section = l->sec;
13832 s = elf_next_in_group (s);
13833 /* These lists are circular. */
13834 if (s == first)
13835 break;
13836 }
13837 }
13838
13839 return TRUE;
13840 }
13841 }
13842
13843 /* A single member comdat group section may be discarded by a
13844 linkonce section and vice versa. */
13845 if ((flags & SEC_GROUP) != 0)
13846 {
13847 asection *first = elf_next_in_group (sec);
13848
13849 if (first != NULL && elf_next_in_group (first) == first)
13850 /* Check this single member group against linkonce sections. */
13851 for (l = already_linked_list->entry; l != NULL; l = l->next)
13852 if ((l->sec->flags & SEC_GROUP) == 0
13853 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13854 {
13855 first->output_section = bfd_abs_section_ptr;
13856 first->kept_section = l->sec;
13857 sec->output_section = bfd_abs_section_ptr;
13858 break;
13859 }
13860 }
13861 else
13862 /* Check this linkonce section against single member groups. */
13863 for (l = already_linked_list->entry; l != NULL; l = l->next)
13864 if (l->sec->flags & SEC_GROUP)
13865 {
13866 asection *first = elf_next_in_group (l->sec);
13867
13868 if (first != NULL
13869 && elf_next_in_group (first) == first
13870 && bfd_elf_match_symbols_in_sections (first, sec, info))
13871 {
13872 sec->output_section = bfd_abs_section_ptr;
13873 sec->kept_section = first;
13874 break;
13875 }
13876 }
13877
13878 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13879 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13880 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13881 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13882 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13883 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13884 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13885 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13886 The reverse order cannot happen as there is never a bfd with only the
13887 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13888 matter as here were are looking only for cross-bfd sections. */
13889
13890 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
13891 for (l = already_linked_list->entry; l != NULL; l = l->next)
13892 if ((l->sec->flags & SEC_GROUP) == 0
13893 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
13894 {
13895 if (abfd != l->sec->owner)
13896 sec->output_section = bfd_abs_section_ptr;
13897 break;
13898 }
13899
13900 /* This is the first section with this name. Record it. */
13901 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
13902 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
13903 return sec->output_section == bfd_abs_section_ptr;
13904 }
13905
13906 bfd_boolean
13907 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
13908 {
13909 return sym->st_shndx == SHN_COMMON;
13910 }
13911
13912 unsigned int
13913 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
13914 {
13915 return SHN_COMMON;
13916 }
13917
13918 asection *
13919 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
13920 {
13921 return bfd_com_section_ptr;
13922 }
13923
13924 bfd_vma
13925 _bfd_elf_default_got_elt_size (bfd *abfd,
13926 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13927 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
13928 bfd *ibfd ATTRIBUTE_UNUSED,
13929 unsigned long symndx ATTRIBUTE_UNUSED)
13930 {
13931 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13932 return bed->s->arch_size / 8;
13933 }
13934
13935 /* Routines to support the creation of dynamic relocs. */
13936
13937 /* Returns the name of the dynamic reloc section associated with SEC. */
13938
13939 static const char *
13940 get_dynamic_reloc_section_name (bfd * abfd,
13941 asection * sec,
13942 bfd_boolean is_rela)
13943 {
13944 char *name;
13945 const char *old_name = bfd_get_section_name (NULL, sec);
13946 const char *prefix = is_rela ? ".rela" : ".rel";
13947
13948 if (old_name == NULL)
13949 return NULL;
13950
13951 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
13952 sprintf (name, "%s%s", prefix, old_name);
13953
13954 return name;
13955 }
13956
13957 /* Returns the dynamic reloc section associated with SEC.
13958 If necessary compute the name of the dynamic reloc section based
13959 on SEC's name (looked up in ABFD's string table) and the setting
13960 of IS_RELA. */
13961
13962 asection *
13963 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
13964 asection * sec,
13965 bfd_boolean is_rela)
13966 {
13967 asection * reloc_sec = elf_section_data (sec)->sreloc;
13968
13969 if (reloc_sec == NULL)
13970 {
13971 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13972
13973 if (name != NULL)
13974 {
13975 reloc_sec = bfd_get_linker_section (abfd, name);
13976
13977 if (reloc_sec != NULL)
13978 elf_section_data (sec)->sreloc = reloc_sec;
13979 }
13980 }
13981
13982 return reloc_sec;
13983 }
13984
13985 /* Returns the dynamic reloc section associated with SEC. If the
13986 section does not exist it is created and attached to the DYNOBJ
13987 bfd and stored in the SRELOC field of SEC's elf_section_data
13988 structure.
13989
13990 ALIGNMENT is the alignment for the newly created section and
13991 IS_RELA defines whether the name should be .rela.<SEC's name>
13992 or .rel.<SEC's name>. The section name is looked up in the
13993 string table associated with ABFD. */
13994
13995 asection *
13996 _bfd_elf_make_dynamic_reloc_section (asection *sec,
13997 bfd *dynobj,
13998 unsigned int alignment,
13999 bfd *abfd,
14000 bfd_boolean is_rela)
14001 {
14002 asection * reloc_sec = elf_section_data (sec)->sreloc;
14003
14004 if (reloc_sec == NULL)
14005 {
14006 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14007
14008 if (name == NULL)
14009 return NULL;
14010
14011 reloc_sec = bfd_get_linker_section (dynobj, name);
14012
14013 if (reloc_sec == NULL)
14014 {
14015 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14016 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
14017 if ((sec->flags & SEC_ALLOC) != 0)
14018 flags |= SEC_ALLOC | SEC_LOAD;
14019
14020 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
14021 if (reloc_sec != NULL)
14022 {
14023 /* _bfd_elf_get_sec_type_attr chooses a section type by
14024 name. Override as it may be wrong, eg. for a user
14025 section named "auto" we'll get ".relauto" which is
14026 seen to be a .rela section. */
14027 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
14028 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14029 reloc_sec = NULL;
14030 }
14031 }
14032
14033 elf_section_data (sec)->sreloc = reloc_sec;
14034 }
14035
14036 return reloc_sec;
14037 }
14038
14039 /* Copy the ELF symbol type and other attributes for a linker script
14040 assignment from HSRC to HDEST. Generally this should be treated as
14041 if we found a strong non-dynamic definition for HDEST (except that
14042 ld ignores multiple definition errors). */
14043 void
14044 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14045 struct bfd_link_hash_entry *hdest,
14046 struct bfd_link_hash_entry *hsrc)
14047 {
14048 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14049 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14050 Elf_Internal_Sym isym;
14051
14052 ehdest->type = ehsrc->type;
14053 ehdest->target_internal = ehsrc->target_internal;
14054
14055 isym.st_other = ehsrc->other;
14056 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
14057 }
14058
14059 /* Append a RELA relocation REL to section S in BFD. */
14060
14061 void
14062 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14063 {
14064 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14065 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14066 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14067 bed->s->swap_reloca_out (abfd, rel, loc);
14068 }
14069
14070 /* Append a REL relocation REL to section S in BFD. */
14071
14072 void
14073 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14074 {
14075 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14076 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14077 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
14078 bed->s->swap_reloc_out (abfd, rel, loc);
14079 }