PR20932, Internal error during record link assignment
[deliverable/binutils-gdb.git] / bfd / elflink.c
1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2016 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
431 /* The .rel[a].bss section holds copy relocs. This section is not
432 normally needed. We need to create it here, though, so that the
433 linker will map it to an output section. We can't just create it
434 only if we need it, because we will not know whether we need it
435 until we have seen all the input files, and the first time the
436 main linker code calls BFD after examining all the input files
437 (size_dynamic_sections) the input sections have already been
438 mapped to the output sections. If the section turns out not to
439 be needed, we can discard it later. We will never need this
440 section when generating a shared object, since they do not use
441 copy relocs. */
442 if (! bfd_link_pic (info))
443 {
444 s = bfd_make_section_anyway_with_flags (abfd,
445 (bed->rela_plts_and_copies_p
446 ? ".rela.bss" : ".rel.bss"),
447 flags | SEC_READONLY);
448 if (s == NULL
449 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
450 return FALSE;
451 }
452 }
453
454 return TRUE;
455 }
456 \f
457 /* Record a new dynamic symbol. We record the dynamic symbols as we
458 read the input files, since we need to have a list of all of them
459 before we can determine the final sizes of the output sections.
460 Note that we may actually call this function even though we are not
461 going to output any dynamic symbols; in some cases we know that a
462 symbol should be in the dynamic symbol table, but only if there is
463 one. */
464
465 bfd_boolean
466 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
467 struct elf_link_hash_entry *h)
468 {
469 if (h->dynindx == -1)
470 {
471 struct elf_strtab_hash *dynstr;
472 char *p;
473 const char *name;
474 size_t indx;
475
476 /* XXX: The ABI draft says the linker must turn hidden and
477 internal symbols into STB_LOCAL symbols when producing the
478 DSO. However, if ld.so honors st_other in the dynamic table,
479 this would not be necessary. */
480 switch (ELF_ST_VISIBILITY (h->other))
481 {
482 case STV_INTERNAL:
483 case STV_HIDDEN:
484 if (h->root.type != bfd_link_hash_undefined
485 && h->root.type != bfd_link_hash_undefweak)
486 {
487 h->forced_local = 1;
488 if (!elf_hash_table (info)->is_relocatable_executable)
489 return TRUE;
490 }
491
492 default:
493 break;
494 }
495
496 h->dynindx = elf_hash_table (info)->dynsymcount;
497 ++elf_hash_table (info)->dynsymcount;
498
499 dynstr = elf_hash_table (info)->dynstr;
500 if (dynstr == NULL)
501 {
502 /* Create a strtab to hold the dynamic symbol names. */
503 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
504 if (dynstr == NULL)
505 return FALSE;
506 }
507
508 /* We don't put any version information in the dynamic string
509 table. */
510 name = h->root.root.string;
511 p = strchr (name, ELF_VER_CHR);
512 if (p != NULL)
513 /* We know that the p points into writable memory. In fact,
514 there are only a few symbols that have read-only names, being
515 those like _GLOBAL_OFFSET_TABLE_ that are created specially
516 by the backends. Most symbols will have names pointing into
517 an ELF string table read from a file, or to objalloc memory. */
518 *p = 0;
519
520 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
521
522 if (p != NULL)
523 *p = ELF_VER_CHR;
524
525 if (indx == (size_t) -1)
526 return FALSE;
527 h->dynstr_index = indx;
528 }
529
530 return TRUE;
531 }
532 \f
533 /* Mark a symbol dynamic. */
534
535 static void
536 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
537 struct elf_link_hash_entry *h,
538 Elf_Internal_Sym *sym)
539 {
540 struct bfd_elf_dynamic_list *d = info->dynamic_list;
541
542 /* It may be called more than once on the same H. */
543 if(h->dynamic || bfd_link_relocatable (info))
544 return;
545
546 if ((info->dynamic_data
547 && (h->type == STT_OBJECT
548 || h->type == STT_COMMON
549 || (sym != NULL
550 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
551 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
552 || (d != NULL
553 && h->root.type == bfd_link_hash_new
554 && (*d->match) (&d->head, NULL, h->root.root.string)))
555 h->dynamic = 1;
556 }
557
558 /* Record an assignment to a symbol made by a linker script. We need
559 this in case some dynamic object refers to this symbol. */
560
561 bfd_boolean
562 bfd_elf_record_link_assignment (bfd *output_bfd,
563 struct bfd_link_info *info,
564 const char *name,
565 bfd_boolean provide,
566 bfd_boolean hidden)
567 {
568 struct elf_link_hash_entry *h, *hv;
569 struct elf_link_hash_table *htab;
570 const struct elf_backend_data *bed;
571
572 if (!is_elf_hash_table (info->hash))
573 return TRUE;
574
575 htab = elf_hash_table (info);
576 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
577 if (h == NULL)
578 return provide;
579
580 if (h->root.type == bfd_link_hash_warning)
581 h = (struct elf_link_hash_entry *) h->root.u.i.link;
582
583 if (h->versioned == unknown)
584 {
585 /* Set versioned if symbol version is unknown. */
586 char *version = strrchr (name, ELF_VER_CHR);
587 if (version)
588 {
589 if (version > name && version[-1] != ELF_VER_CHR)
590 h->versioned = versioned_hidden;
591 else
592 h->versioned = versioned;
593 }
594 }
595
596 switch (h->root.type)
597 {
598 case bfd_link_hash_defined:
599 case bfd_link_hash_defweak:
600 case bfd_link_hash_common:
601 break;
602 case bfd_link_hash_undefweak:
603 case bfd_link_hash_undefined:
604 /* Since we're defining the symbol, don't let it seem to have not
605 been defined. record_dynamic_symbol and size_dynamic_sections
606 may depend on this. */
607 h->root.type = bfd_link_hash_new;
608 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
609 bfd_link_repair_undef_list (&htab->root);
610 break;
611 case bfd_link_hash_new:
612 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
613 h->non_elf = 0;
614 break;
615 case bfd_link_hash_indirect:
616 /* We had a versioned symbol in a dynamic library. We make the
617 the versioned symbol point to this one. */
618 bed = get_elf_backend_data (output_bfd);
619 hv = h;
620 while (hv->root.type == bfd_link_hash_indirect
621 || hv->root.type == bfd_link_hash_warning)
622 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
623 /* We don't need to update h->root.u since linker will set them
624 later. */
625 h->root.type = bfd_link_hash_undefined;
626 hv->root.type = bfd_link_hash_indirect;
627 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
628 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
629 break;
630 default:
631 BFD_FAIL ();
632 return FALSE;
633 }
634
635 /* If this symbol is being provided by the linker script, and it is
636 currently defined by a dynamic object, but not by a regular
637 object, then mark it as undefined so that the generic linker will
638 force the correct value. */
639 if (provide
640 && h->def_dynamic
641 && !h->def_regular)
642 h->root.type = bfd_link_hash_undefined;
643
644 /* If this symbol is not being provided by the linker script, and it is
645 currently defined by a dynamic object, but not by a regular object,
646 then clear out any version information because the symbol will not be
647 associated with the dynamic object any more. */
648 if (!provide
649 && h->def_dynamic
650 && !h->def_regular)
651 h->verinfo.verdef = NULL;
652
653 h->def_regular = 1;
654
655 if (hidden)
656 {
657 bed = get_elf_backend_data (output_bfd);
658 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
659 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
660 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
661 }
662
663 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
664 and executables. */
665 if (!bfd_link_relocatable (info)
666 && h->dynindx != -1
667 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
668 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
669 h->forced_local = 1;
670
671 if ((h->def_dynamic
672 || h->ref_dynamic
673 || bfd_link_dll (info)
674 || elf_hash_table (info)->is_relocatable_executable)
675 && h->dynindx == -1)
676 {
677 if (! bfd_elf_link_record_dynamic_symbol (info, h))
678 return FALSE;
679
680 /* If this is a weak defined symbol, and we know a corresponding
681 real symbol from the same dynamic object, make sure the real
682 symbol is also made into a dynamic symbol. */
683 if (h->u.weakdef != NULL
684 && h->u.weakdef->dynindx == -1)
685 {
686 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
687 return FALSE;
688 }
689 }
690
691 return TRUE;
692 }
693
694 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
695 success, and 2 on a failure caused by attempting to record a symbol
696 in a discarded section, eg. a discarded link-once section symbol. */
697
698 int
699 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
700 bfd *input_bfd,
701 long input_indx)
702 {
703 bfd_size_type amt;
704 struct elf_link_local_dynamic_entry *entry;
705 struct elf_link_hash_table *eht;
706 struct elf_strtab_hash *dynstr;
707 size_t dynstr_index;
708 char *name;
709 Elf_External_Sym_Shndx eshndx;
710 char esym[sizeof (Elf64_External_Sym)];
711
712 if (! is_elf_hash_table (info->hash))
713 return 0;
714
715 /* See if the entry exists already. */
716 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
717 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
718 return 1;
719
720 amt = sizeof (*entry);
721 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
722 if (entry == NULL)
723 return 0;
724
725 /* Go find the symbol, so that we can find it's name. */
726 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
727 1, input_indx, &entry->isym, esym, &eshndx))
728 {
729 bfd_release (input_bfd, entry);
730 return 0;
731 }
732
733 if (entry->isym.st_shndx != SHN_UNDEF
734 && entry->isym.st_shndx < SHN_LORESERVE)
735 {
736 asection *s;
737
738 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
739 if (s == NULL || bfd_is_abs_section (s->output_section))
740 {
741 /* We can still bfd_release here as nothing has done another
742 bfd_alloc. We can't do this later in this function. */
743 bfd_release (input_bfd, entry);
744 return 2;
745 }
746 }
747
748 name = (bfd_elf_string_from_elf_section
749 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
750 entry->isym.st_name));
751
752 dynstr = elf_hash_table (info)->dynstr;
753 if (dynstr == NULL)
754 {
755 /* Create a strtab to hold the dynamic symbol names. */
756 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
757 if (dynstr == NULL)
758 return 0;
759 }
760
761 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
762 if (dynstr_index == (size_t) -1)
763 return 0;
764 entry->isym.st_name = dynstr_index;
765
766 eht = elf_hash_table (info);
767
768 entry->next = eht->dynlocal;
769 eht->dynlocal = entry;
770 entry->input_bfd = input_bfd;
771 entry->input_indx = input_indx;
772 eht->dynsymcount++;
773
774 /* Whatever binding the symbol had before, it's now local. */
775 entry->isym.st_info
776 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
777
778 /* The dynindx will be set at the end of size_dynamic_sections. */
779
780 return 1;
781 }
782
783 /* Return the dynindex of a local dynamic symbol. */
784
785 long
786 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
787 bfd *input_bfd,
788 long input_indx)
789 {
790 struct elf_link_local_dynamic_entry *e;
791
792 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
793 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
794 return e->dynindx;
795 return -1;
796 }
797
798 /* This function is used to renumber the dynamic symbols, if some of
799 them are removed because they are marked as local. This is called
800 via elf_link_hash_traverse. */
801
802 static bfd_boolean
803 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
804 void *data)
805 {
806 size_t *count = (size_t *) data;
807
808 if (h->forced_local)
809 return TRUE;
810
811 if (h->dynindx != -1)
812 h->dynindx = ++(*count);
813
814 return TRUE;
815 }
816
817
818 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
819 STB_LOCAL binding. */
820
821 static bfd_boolean
822 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
823 void *data)
824 {
825 size_t *count = (size_t *) data;
826
827 if (!h->forced_local)
828 return TRUE;
829
830 if (h->dynindx != -1)
831 h->dynindx = ++(*count);
832
833 return TRUE;
834 }
835
836 /* Return true if the dynamic symbol for a given section should be
837 omitted when creating a shared library. */
838 bfd_boolean
839 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
840 struct bfd_link_info *info,
841 asection *p)
842 {
843 struct elf_link_hash_table *htab;
844 asection *ip;
845
846 switch (elf_section_data (p)->this_hdr.sh_type)
847 {
848 case SHT_PROGBITS:
849 case SHT_NOBITS:
850 /* If sh_type is yet undecided, assume it could be
851 SHT_PROGBITS/SHT_NOBITS. */
852 case SHT_NULL:
853 htab = elf_hash_table (info);
854 if (p == htab->tls_sec)
855 return FALSE;
856
857 if (htab->text_index_section != NULL)
858 return p != htab->text_index_section && p != htab->data_index_section;
859
860 return (htab->dynobj != NULL
861 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
862 && ip->output_section == p);
863
864 /* There shouldn't be section relative relocations
865 against any other section. */
866 default:
867 return TRUE;
868 }
869 }
870
871 /* Assign dynsym indices. In a shared library we generate a section
872 symbol for each output section, which come first. Next come symbols
873 which have been forced to local binding. Then all of the back-end
874 allocated local dynamic syms, followed by the rest of the global
875 symbols. */
876
877 static unsigned long
878 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
879 struct bfd_link_info *info,
880 unsigned long *section_sym_count)
881 {
882 unsigned long dynsymcount = 0;
883
884 if (bfd_link_pic (info)
885 || elf_hash_table (info)->is_relocatable_executable)
886 {
887 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
888 asection *p;
889 for (p = output_bfd->sections; p ; p = p->next)
890 if ((p->flags & SEC_EXCLUDE) == 0
891 && (p->flags & SEC_ALLOC) != 0
892 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
893 elf_section_data (p)->dynindx = ++dynsymcount;
894 else
895 elf_section_data (p)->dynindx = 0;
896 }
897 *section_sym_count = dynsymcount;
898
899 elf_link_hash_traverse (elf_hash_table (info),
900 elf_link_renumber_local_hash_table_dynsyms,
901 &dynsymcount);
902
903 if (elf_hash_table (info)->dynlocal)
904 {
905 struct elf_link_local_dynamic_entry *p;
906 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
907 p->dynindx = ++dynsymcount;
908 }
909 elf_hash_table (info)->local_dynsymcount = dynsymcount;
910
911 elf_link_hash_traverse (elf_hash_table (info),
912 elf_link_renumber_hash_table_dynsyms,
913 &dynsymcount);
914
915 /* There is an unused NULL entry at the head of the table which we
916 must account for in our count even if the table is empty since it
917 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
918 .dynamic section. */
919 dynsymcount++;
920
921 elf_hash_table (info)->dynsymcount = dynsymcount;
922 return dynsymcount;
923 }
924
925 /* Merge st_other field. */
926
927 static void
928 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
929 const Elf_Internal_Sym *isym, asection *sec,
930 bfd_boolean definition, bfd_boolean dynamic)
931 {
932 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
933
934 /* If st_other has a processor-specific meaning, specific
935 code might be needed here. */
936 if (bed->elf_backend_merge_symbol_attribute)
937 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
938 dynamic);
939
940 if (!dynamic)
941 {
942 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
943 unsigned hvis = ELF_ST_VISIBILITY (h->other);
944
945 /* Keep the most constraining visibility. Leave the remainder
946 of the st_other field to elf_backend_merge_symbol_attribute. */
947 if (symvis - 1 < hvis - 1)
948 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
949 }
950 else if (definition
951 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
952 && (sec->flags & SEC_READONLY) == 0)
953 h->protected_def = 1;
954 }
955
956 /* This function is called when we want to merge a new symbol with an
957 existing symbol. It handles the various cases which arise when we
958 find a definition in a dynamic object, or when there is already a
959 definition in a dynamic object. The new symbol is described by
960 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
961 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
962 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
963 of an old common symbol. We set OVERRIDE if the old symbol is
964 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
965 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
966 to change. By OK to change, we mean that we shouldn't warn if the
967 type or size does change. */
968
969 static bfd_boolean
970 _bfd_elf_merge_symbol (bfd *abfd,
971 struct bfd_link_info *info,
972 const char *name,
973 Elf_Internal_Sym *sym,
974 asection **psec,
975 bfd_vma *pvalue,
976 struct elf_link_hash_entry **sym_hash,
977 bfd **poldbfd,
978 bfd_boolean *pold_weak,
979 unsigned int *pold_alignment,
980 bfd_boolean *skip,
981 bfd_boolean *override,
982 bfd_boolean *type_change_ok,
983 bfd_boolean *size_change_ok,
984 bfd_boolean *matched)
985 {
986 asection *sec, *oldsec;
987 struct elf_link_hash_entry *h;
988 struct elf_link_hash_entry *hi;
989 struct elf_link_hash_entry *flip;
990 int bind;
991 bfd *oldbfd;
992 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
993 bfd_boolean newweak, oldweak, newfunc, oldfunc;
994 const struct elf_backend_data *bed;
995 char *new_version;
996
997 *skip = FALSE;
998 *override = FALSE;
999
1000 sec = *psec;
1001 bind = ELF_ST_BIND (sym->st_info);
1002
1003 if (! bfd_is_und_section (sec))
1004 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1005 else
1006 h = ((struct elf_link_hash_entry *)
1007 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1008 if (h == NULL)
1009 return FALSE;
1010 *sym_hash = h;
1011
1012 bed = get_elf_backend_data (abfd);
1013
1014 /* NEW_VERSION is the symbol version of the new symbol. */
1015 if (h->versioned != unversioned)
1016 {
1017 /* Symbol version is unknown or versioned. */
1018 new_version = strrchr (name, ELF_VER_CHR);
1019 if (new_version)
1020 {
1021 if (h->versioned == unknown)
1022 {
1023 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1024 h->versioned = versioned_hidden;
1025 else
1026 h->versioned = versioned;
1027 }
1028 new_version += 1;
1029 if (new_version[0] == '\0')
1030 new_version = NULL;
1031 }
1032 else
1033 h->versioned = unversioned;
1034 }
1035 else
1036 new_version = NULL;
1037
1038 /* For merging, we only care about real symbols. But we need to make
1039 sure that indirect symbol dynamic flags are updated. */
1040 hi = h;
1041 while (h->root.type == bfd_link_hash_indirect
1042 || h->root.type == bfd_link_hash_warning)
1043 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1044
1045 if (!*matched)
1046 {
1047 if (hi == h || h->root.type == bfd_link_hash_new)
1048 *matched = TRUE;
1049 else
1050 {
1051 /* OLD_HIDDEN is true if the existing symbol is only visible
1052 to the symbol with the same symbol version. NEW_HIDDEN is
1053 true if the new symbol is only visible to the symbol with
1054 the same symbol version. */
1055 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1056 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1057 if (!old_hidden && !new_hidden)
1058 /* The new symbol matches the existing symbol if both
1059 aren't hidden. */
1060 *matched = TRUE;
1061 else
1062 {
1063 /* OLD_VERSION is the symbol version of the existing
1064 symbol. */
1065 char *old_version;
1066
1067 if (h->versioned >= versioned)
1068 old_version = strrchr (h->root.root.string,
1069 ELF_VER_CHR) + 1;
1070 else
1071 old_version = NULL;
1072
1073 /* The new symbol matches the existing symbol if they
1074 have the same symbol version. */
1075 *matched = (old_version == new_version
1076 || (old_version != NULL
1077 && new_version != NULL
1078 && strcmp (old_version, new_version) == 0));
1079 }
1080 }
1081 }
1082
1083 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1084 existing symbol. */
1085
1086 oldbfd = NULL;
1087 oldsec = NULL;
1088 switch (h->root.type)
1089 {
1090 default:
1091 break;
1092
1093 case bfd_link_hash_undefined:
1094 case bfd_link_hash_undefweak:
1095 oldbfd = h->root.u.undef.abfd;
1096 break;
1097
1098 case bfd_link_hash_defined:
1099 case bfd_link_hash_defweak:
1100 oldbfd = h->root.u.def.section->owner;
1101 oldsec = h->root.u.def.section;
1102 break;
1103
1104 case bfd_link_hash_common:
1105 oldbfd = h->root.u.c.p->section->owner;
1106 oldsec = h->root.u.c.p->section;
1107 if (pold_alignment)
1108 *pold_alignment = h->root.u.c.p->alignment_power;
1109 break;
1110 }
1111 if (poldbfd && *poldbfd == NULL)
1112 *poldbfd = oldbfd;
1113
1114 /* Differentiate strong and weak symbols. */
1115 newweak = bind == STB_WEAK;
1116 oldweak = (h->root.type == bfd_link_hash_defweak
1117 || h->root.type == bfd_link_hash_undefweak);
1118 if (pold_weak)
1119 *pold_weak = oldweak;
1120
1121 /* This code is for coping with dynamic objects, and is only useful
1122 if we are doing an ELF link. */
1123 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1124 return TRUE;
1125
1126 /* We have to check it for every instance since the first few may be
1127 references and not all compilers emit symbol type for undefined
1128 symbols. */
1129 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1130
1131 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1132 respectively, is from a dynamic object. */
1133
1134 newdyn = (abfd->flags & DYNAMIC) != 0;
1135
1136 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1137 syms and defined syms in dynamic libraries respectively.
1138 ref_dynamic on the other hand can be set for a symbol defined in
1139 a dynamic library, and def_dynamic may not be set; When the
1140 definition in a dynamic lib is overridden by a definition in the
1141 executable use of the symbol in the dynamic lib becomes a
1142 reference to the executable symbol. */
1143 if (newdyn)
1144 {
1145 if (bfd_is_und_section (sec))
1146 {
1147 if (bind != STB_WEAK)
1148 {
1149 h->ref_dynamic_nonweak = 1;
1150 hi->ref_dynamic_nonweak = 1;
1151 }
1152 }
1153 else
1154 {
1155 /* Update the existing symbol only if they match. */
1156 if (*matched)
1157 h->dynamic_def = 1;
1158 hi->dynamic_def = 1;
1159 }
1160 }
1161
1162 /* If we just created the symbol, mark it as being an ELF symbol.
1163 Other than that, there is nothing to do--there is no merge issue
1164 with a newly defined symbol--so we just return. */
1165
1166 if (h->root.type == bfd_link_hash_new)
1167 {
1168 h->non_elf = 0;
1169 return TRUE;
1170 }
1171
1172 /* In cases involving weak versioned symbols, we may wind up trying
1173 to merge a symbol with itself. Catch that here, to avoid the
1174 confusion that results if we try to override a symbol with
1175 itself. The additional tests catch cases like
1176 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1177 dynamic object, which we do want to handle here. */
1178 if (abfd == oldbfd
1179 && (newweak || oldweak)
1180 && ((abfd->flags & DYNAMIC) == 0
1181 || !h->def_regular))
1182 return TRUE;
1183
1184 olddyn = FALSE;
1185 if (oldbfd != NULL)
1186 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1187 else if (oldsec != NULL)
1188 {
1189 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1190 indices used by MIPS ELF. */
1191 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1192 }
1193
1194 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1195 respectively, appear to be a definition rather than reference. */
1196
1197 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1198
1199 olddef = (h->root.type != bfd_link_hash_undefined
1200 && h->root.type != bfd_link_hash_undefweak
1201 && h->root.type != bfd_link_hash_common);
1202
1203 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1204 respectively, appear to be a function. */
1205
1206 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1207 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1208
1209 oldfunc = (h->type != STT_NOTYPE
1210 && bed->is_function_type (h->type));
1211
1212 /* If creating a default indirect symbol ("foo" or "foo@") from a
1213 dynamic versioned definition ("foo@@") skip doing so if there is
1214 an existing regular definition with a different type. We don't
1215 want, for example, a "time" variable in the executable overriding
1216 a "time" function in a shared library. */
1217 if (pold_alignment == NULL
1218 && newdyn
1219 && newdef
1220 && !olddyn
1221 && (olddef || h->root.type == bfd_link_hash_common)
1222 && ELF_ST_TYPE (sym->st_info) != h->type
1223 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1224 && h->type != STT_NOTYPE
1225 && !(newfunc && oldfunc))
1226 {
1227 *skip = TRUE;
1228 return TRUE;
1229 }
1230
1231 /* Check TLS symbols. We don't check undefined symbols introduced
1232 by "ld -u" which have no type (and oldbfd NULL), and we don't
1233 check symbols from plugins because they also have no type. */
1234 if (oldbfd != NULL
1235 && (oldbfd->flags & BFD_PLUGIN) == 0
1236 && (abfd->flags & BFD_PLUGIN) == 0
1237 && ELF_ST_TYPE (sym->st_info) != h->type
1238 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1239 {
1240 bfd *ntbfd, *tbfd;
1241 bfd_boolean ntdef, tdef;
1242 asection *ntsec, *tsec;
1243
1244 if (h->type == STT_TLS)
1245 {
1246 ntbfd = abfd;
1247 ntsec = sec;
1248 ntdef = newdef;
1249 tbfd = oldbfd;
1250 tsec = oldsec;
1251 tdef = olddef;
1252 }
1253 else
1254 {
1255 ntbfd = oldbfd;
1256 ntsec = oldsec;
1257 ntdef = olddef;
1258 tbfd = abfd;
1259 tsec = sec;
1260 tdef = newdef;
1261 }
1262
1263 if (tdef && ntdef)
1264 _bfd_error_handler
1265 /* xgettext:c-format */
1266 (_("%s: TLS definition in %B section %A "
1267 "mismatches non-TLS definition in %B section %A"),
1268 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1269 else if (!tdef && !ntdef)
1270 _bfd_error_handler
1271 /* xgettext:c-format */
1272 (_("%s: TLS reference in %B "
1273 "mismatches non-TLS reference in %B"),
1274 tbfd, ntbfd, h->root.root.string);
1275 else if (tdef)
1276 _bfd_error_handler
1277 /* xgettext:c-format */
1278 (_("%s: TLS definition in %B section %A "
1279 "mismatches non-TLS reference in %B"),
1280 tbfd, tsec, ntbfd, h->root.root.string);
1281 else
1282 _bfd_error_handler
1283 /* xgettext:c-format */
1284 (_("%s: TLS reference in %B "
1285 "mismatches non-TLS definition in %B section %A"),
1286 tbfd, ntbfd, ntsec, h->root.root.string);
1287
1288 bfd_set_error (bfd_error_bad_value);
1289 return FALSE;
1290 }
1291
1292 /* If the old symbol has non-default visibility, we ignore the new
1293 definition from a dynamic object. */
1294 if (newdyn
1295 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1296 && !bfd_is_und_section (sec))
1297 {
1298 *skip = TRUE;
1299 /* Make sure this symbol is dynamic. */
1300 h->ref_dynamic = 1;
1301 hi->ref_dynamic = 1;
1302 /* A protected symbol has external availability. Make sure it is
1303 recorded as dynamic.
1304
1305 FIXME: Should we check type and size for protected symbol? */
1306 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1307 return bfd_elf_link_record_dynamic_symbol (info, h);
1308 else
1309 return TRUE;
1310 }
1311 else if (!newdyn
1312 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1313 && h->def_dynamic)
1314 {
1315 /* If the new symbol with non-default visibility comes from a
1316 relocatable file and the old definition comes from a dynamic
1317 object, we remove the old definition. */
1318 if (hi->root.type == bfd_link_hash_indirect)
1319 {
1320 /* Handle the case where the old dynamic definition is
1321 default versioned. We need to copy the symbol info from
1322 the symbol with default version to the normal one if it
1323 was referenced before. */
1324 if (h->ref_regular)
1325 {
1326 hi->root.type = h->root.type;
1327 h->root.type = bfd_link_hash_indirect;
1328 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1329
1330 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1331 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1332 {
1333 /* If the new symbol is hidden or internal, completely undo
1334 any dynamic link state. */
1335 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1336 h->forced_local = 0;
1337 h->ref_dynamic = 0;
1338 }
1339 else
1340 h->ref_dynamic = 1;
1341
1342 h->def_dynamic = 0;
1343 /* FIXME: Should we check type and size for protected symbol? */
1344 h->size = 0;
1345 h->type = 0;
1346
1347 h = hi;
1348 }
1349 else
1350 h = hi;
1351 }
1352
1353 /* If the old symbol was undefined before, then it will still be
1354 on the undefs list. If the new symbol is undefined or
1355 common, we can't make it bfd_link_hash_new here, because new
1356 undefined or common symbols will be added to the undefs list
1357 by _bfd_generic_link_add_one_symbol. Symbols may not be
1358 added twice to the undefs list. Also, if the new symbol is
1359 undefweak then we don't want to lose the strong undef. */
1360 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1361 {
1362 h->root.type = bfd_link_hash_undefined;
1363 h->root.u.undef.abfd = abfd;
1364 }
1365 else
1366 {
1367 h->root.type = bfd_link_hash_new;
1368 h->root.u.undef.abfd = NULL;
1369 }
1370
1371 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1372 {
1373 /* If the new symbol is hidden or internal, completely undo
1374 any dynamic link state. */
1375 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1376 h->forced_local = 0;
1377 h->ref_dynamic = 0;
1378 }
1379 else
1380 h->ref_dynamic = 1;
1381 h->def_dynamic = 0;
1382 /* FIXME: Should we check type and size for protected symbol? */
1383 h->size = 0;
1384 h->type = 0;
1385 return TRUE;
1386 }
1387
1388 /* If a new weak symbol definition comes from a regular file and the
1389 old symbol comes from a dynamic library, we treat the new one as
1390 strong. Similarly, an old weak symbol definition from a regular
1391 file is treated as strong when the new symbol comes from a dynamic
1392 library. Further, an old weak symbol from a dynamic library is
1393 treated as strong if the new symbol is from a dynamic library.
1394 This reflects the way glibc's ld.so works.
1395
1396 Do this before setting *type_change_ok or *size_change_ok so that
1397 we warn properly when dynamic library symbols are overridden. */
1398
1399 if (newdef && !newdyn && olddyn)
1400 newweak = FALSE;
1401 if (olddef && newdyn)
1402 oldweak = FALSE;
1403
1404 /* Allow changes between different types of function symbol. */
1405 if (newfunc && oldfunc)
1406 *type_change_ok = TRUE;
1407
1408 /* It's OK to change the type if either the existing symbol or the
1409 new symbol is weak. A type change is also OK if the old symbol
1410 is undefined and the new symbol is defined. */
1411
1412 if (oldweak
1413 || newweak
1414 || (newdef
1415 && h->root.type == bfd_link_hash_undefined))
1416 *type_change_ok = TRUE;
1417
1418 /* It's OK to change the size if either the existing symbol or the
1419 new symbol is weak, or if the old symbol is undefined. */
1420
1421 if (*type_change_ok
1422 || h->root.type == bfd_link_hash_undefined)
1423 *size_change_ok = TRUE;
1424
1425 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1426 symbol, respectively, appears to be a common symbol in a dynamic
1427 object. If a symbol appears in an uninitialized section, and is
1428 not weak, and is not a function, then it may be a common symbol
1429 which was resolved when the dynamic object was created. We want
1430 to treat such symbols specially, because they raise special
1431 considerations when setting the symbol size: if the symbol
1432 appears as a common symbol in a regular object, and the size in
1433 the regular object is larger, we must make sure that we use the
1434 larger size. This problematic case can always be avoided in C,
1435 but it must be handled correctly when using Fortran shared
1436 libraries.
1437
1438 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1439 likewise for OLDDYNCOMMON and OLDDEF.
1440
1441 Note that this test is just a heuristic, and that it is quite
1442 possible to have an uninitialized symbol in a shared object which
1443 is really a definition, rather than a common symbol. This could
1444 lead to some minor confusion when the symbol really is a common
1445 symbol in some regular object. However, I think it will be
1446 harmless. */
1447
1448 if (newdyn
1449 && newdef
1450 && !newweak
1451 && (sec->flags & SEC_ALLOC) != 0
1452 && (sec->flags & SEC_LOAD) == 0
1453 && sym->st_size > 0
1454 && !newfunc)
1455 newdyncommon = TRUE;
1456 else
1457 newdyncommon = FALSE;
1458
1459 if (olddyn
1460 && olddef
1461 && h->root.type == bfd_link_hash_defined
1462 && h->def_dynamic
1463 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1464 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1465 && h->size > 0
1466 && !oldfunc)
1467 olddyncommon = TRUE;
1468 else
1469 olddyncommon = FALSE;
1470
1471 /* We now know everything about the old and new symbols. We ask the
1472 backend to check if we can merge them. */
1473 if (bed->merge_symbol != NULL)
1474 {
1475 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1476 return FALSE;
1477 sec = *psec;
1478 }
1479
1480 /* If both the old and the new symbols look like common symbols in a
1481 dynamic object, set the size of the symbol to the larger of the
1482 two. */
1483
1484 if (olddyncommon
1485 && newdyncommon
1486 && sym->st_size != h->size)
1487 {
1488 /* Since we think we have two common symbols, issue a multiple
1489 common warning if desired. Note that we only warn if the
1490 size is different. If the size is the same, we simply let
1491 the old symbol override the new one as normally happens with
1492 symbols defined in dynamic objects. */
1493
1494 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1495 bfd_link_hash_common, sym->st_size);
1496 if (sym->st_size > h->size)
1497 h->size = sym->st_size;
1498
1499 *size_change_ok = TRUE;
1500 }
1501
1502 /* If we are looking at a dynamic object, and we have found a
1503 definition, we need to see if the symbol was already defined by
1504 some other object. If so, we want to use the existing
1505 definition, and we do not want to report a multiple symbol
1506 definition error; we do this by clobbering *PSEC to be
1507 bfd_und_section_ptr.
1508
1509 We treat a common symbol as a definition if the symbol in the
1510 shared library is a function, since common symbols always
1511 represent variables; this can cause confusion in principle, but
1512 any such confusion would seem to indicate an erroneous program or
1513 shared library. We also permit a common symbol in a regular
1514 object to override a weak symbol in a shared object. A common
1515 symbol in executable also overrides a symbol in a shared object. */
1516
1517 if (newdyn
1518 && newdef
1519 && (olddef
1520 || (h->root.type == bfd_link_hash_common
1521 && (newweak
1522 || newfunc
1523 || (!olddyn && bfd_link_executable (info))))))
1524 {
1525 *override = TRUE;
1526 newdef = FALSE;
1527 newdyncommon = FALSE;
1528
1529 *psec = sec = bfd_und_section_ptr;
1530 *size_change_ok = TRUE;
1531
1532 /* If we get here when the old symbol is a common symbol, then
1533 we are explicitly letting it override a weak symbol or
1534 function in a dynamic object, and we don't want to warn about
1535 a type change. If the old symbol is a defined symbol, a type
1536 change warning may still be appropriate. */
1537
1538 if (h->root.type == bfd_link_hash_common)
1539 *type_change_ok = TRUE;
1540 }
1541
1542 /* Handle the special case of an old common symbol merging with a
1543 new symbol which looks like a common symbol in a shared object.
1544 We change *PSEC and *PVALUE to make the new symbol look like a
1545 common symbol, and let _bfd_generic_link_add_one_symbol do the
1546 right thing. */
1547
1548 if (newdyncommon
1549 && h->root.type == bfd_link_hash_common)
1550 {
1551 *override = TRUE;
1552 newdef = FALSE;
1553 newdyncommon = FALSE;
1554 *pvalue = sym->st_size;
1555 *psec = sec = bed->common_section (oldsec);
1556 *size_change_ok = TRUE;
1557 }
1558
1559 /* Skip weak definitions of symbols that are already defined. */
1560 if (newdef && olddef && newweak)
1561 {
1562 /* Don't skip new non-IR weak syms. */
1563 if (!(oldbfd != NULL
1564 && (oldbfd->flags & BFD_PLUGIN) != 0
1565 && (abfd->flags & BFD_PLUGIN) == 0))
1566 {
1567 newdef = FALSE;
1568 *skip = TRUE;
1569 }
1570
1571 /* Merge st_other. If the symbol already has a dynamic index,
1572 but visibility says it should not be visible, turn it into a
1573 local symbol. */
1574 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1575 if (h->dynindx != -1)
1576 switch (ELF_ST_VISIBILITY (h->other))
1577 {
1578 case STV_INTERNAL:
1579 case STV_HIDDEN:
1580 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1581 break;
1582 }
1583 }
1584
1585 /* If the old symbol is from a dynamic object, and the new symbol is
1586 a definition which is not from a dynamic object, then the new
1587 symbol overrides the old symbol. Symbols from regular files
1588 always take precedence over symbols from dynamic objects, even if
1589 they are defined after the dynamic object in the link.
1590
1591 As above, we again permit a common symbol in a regular object to
1592 override a definition in a shared object if the shared object
1593 symbol is a function or is weak. */
1594
1595 flip = NULL;
1596 if (!newdyn
1597 && (newdef
1598 || (bfd_is_com_section (sec)
1599 && (oldweak || oldfunc)))
1600 && olddyn
1601 && olddef
1602 && h->def_dynamic)
1603 {
1604 /* Change the hash table entry to undefined, and let
1605 _bfd_generic_link_add_one_symbol do the right thing with the
1606 new definition. */
1607
1608 h->root.type = bfd_link_hash_undefined;
1609 h->root.u.undef.abfd = h->root.u.def.section->owner;
1610 *size_change_ok = TRUE;
1611
1612 olddef = FALSE;
1613 olddyncommon = FALSE;
1614
1615 /* We again permit a type change when a common symbol may be
1616 overriding a function. */
1617
1618 if (bfd_is_com_section (sec))
1619 {
1620 if (oldfunc)
1621 {
1622 /* If a common symbol overrides a function, make sure
1623 that it isn't defined dynamically nor has type
1624 function. */
1625 h->def_dynamic = 0;
1626 h->type = STT_NOTYPE;
1627 }
1628 *type_change_ok = TRUE;
1629 }
1630
1631 if (hi->root.type == bfd_link_hash_indirect)
1632 flip = hi;
1633 else
1634 /* This union may have been set to be non-NULL when this symbol
1635 was seen in a dynamic object. We must force the union to be
1636 NULL, so that it is correct for a regular symbol. */
1637 h->verinfo.vertree = NULL;
1638 }
1639
1640 /* Handle the special case of a new common symbol merging with an
1641 old symbol that looks like it might be a common symbol defined in
1642 a shared object. Note that we have already handled the case in
1643 which a new common symbol should simply override the definition
1644 in the shared library. */
1645
1646 if (! newdyn
1647 && bfd_is_com_section (sec)
1648 && olddyncommon)
1649 {
1650 /* It would be best if we could set the hash table entry to a
1651 common symbol, but we don't know what to use for the section
1652 or the alignment. */
1653 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1654 bfd_link_hash_common, sym->st_size);
1655
1656 /* If the presumed common symbol in the dynamic object is
1657 larger, pretend that the new symbol has its size. */
1658
1659 if (h->size > *pvalue)
1660 *pvalue = h->size;
1661
1662 /* We need to remember the alignment required by the symbol
1663 in the dynamic object. */
1664 BFD_ASSERT (pold_alignment);
1665 *pold_alignment = h->root.u.def.section->alignment_power;
1666
1667 olddef = FALSE;
1668 olddyncommon = FALSE;
1669
1670 h->root.type = bfd_link_hash_undefined;
1671 h->root.u.undef.abfd = h->root.u.def.section->owner;
1672
1673 *size_change_ok = TRUE;
1674 *type_change_ok = TRUE;
1675
1676 if (hi->root.type == bfd_link_hash_indirect)
1677 flip = hi;
1678 else
1679 h->verinfo.vertree = NULL;
1680 }
1681
1682 if (flip != NULL)
1683 {
1684 /* Handle the case where we had a versioned symbol in a dynamic
1685 library and now find a definition in a normal object. In this
1686 case, we make the versioned symbol point to the normal one. */
1687 flip->root.type = h->root.type;
1688 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1689 h->root.type = bfd_link_hash_indirect;
1690 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1691 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1692 if (h->def_dynamic)
1693 {
1694 h->def_dynamic = 0;
1695 flip->ref_dynamic = 1;
1696 }
1697 }
1698
1699 return TRUE;
1700 }
1701
1702 /* This function is called to create an indirect symbol from the
1703 default for the symbol with the default version if needed. The
1704 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1705 set DYNSYM if the new indirect symbol is dynamic. */
1706
1707 static bfd_boolean
1708 _bfd_elf_add_default_symbol (bfd *abfd,
1709 struct bfd_link_info *info,
1710 struct elf_link_hash_entry *h,
1711 const char *name,
1712 Elf_Internal_Sym *sym,
1713 asection *sec,
1714 bfd_vma value,
1715 bfd **poldbfd,
1716 bfd_boolean *dynsym)
1717 {
1718 bfd_boolean type_change_ok;
1719 bfd_boolean size_change_ok;
1720 bfd_boolean skip;
1721 char *shortname;
1722 struct elf_link_hash_entry *hi;
1723 struct bfd_link_hash_entry *bh;
1724 const struct elf_backend_data *bed;
1725 bfd_boolean collect;
1726 bfd_boolean dynamic;
1727 bfd_boolean override;
1728 char *p;
1729 size_t len, shortlen;
1730 asection *tmp_sec;
1731 bfd_boolean matched;
1732
1733 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1734 return TRUE;
1735
1736 /* If this symbol has a version, and it is the default version, we
1737 create an indirect symbol from the default name to the fully
1738 decorated name. This will cause external references which do not
1739 specify a version to be bound to this version of the symbol. */
1740 p = strchr (name, ELF_VER_CHR);
1741 if (h->versioned == unknown)
1742 {
1743 if (p == NULL)
1744 {
1745 h->versioned = unversioned;
1746 return TRUE;
1747 }
1748 else
1749 {
1750 if (p[1] != ELF_VER_CHR)
1751 {
1752 h->versioned = versioned_hidden;
1753 return TRUE;
1754 }
1755 else
1756 h->versioned = versioned;
1757 }
1758 }
1759 else
1760 {
1761 /* PR ld/19073: We may see an unversioned definition after the
1762 default version. */
1763 if (p == NULL)
1764 return TRUE;
1765 }
1766
1767 bed = get_elf_backend_data (abfd);
1768 collect = bed->collect;
1769 dynamic = (abfd->flags & DYNAMIC) != 0;
1770
1771 shortlen = p - name;
1772 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1773 if (shortname == NULL)
1774 return FALSE;
1775 memcpy (shortname, name, shortlen);
1776 shortname[shortlen] = '\0';
1777
1778 /* We are going to create a new symbol. Merge it with any existing
1779 symbol with this name. For the purposes of the merge, act as
1780 though we were defining the symbol we just defined, although we
1781 actually going to define an indirect symbol. */
1782 type_change_ok = FALSE;
1783 size_change_ok = FALSE;
1784 matched = TRUE;
1785 tmp_sec = sec;
1786 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1787 &hi, poldbfd, NULL, NULL, &skip, &override,
1788 &type_change_ok, &size_change_ok, &matched))
1789 return FALSE;
1790
1791 if (skip)
1792 goto nondefault;
1793
1794 if (hi->def_regular)
1795 {
1796 /* If the undecorated symbol will have a version added by a
1797 script different to H, then don't indirect to/from the
1798 undecorated symbol. This isn't ideal because we may not yet
1799 have seen symbol versions, if given by a script on the
1800 command line rather than via --version-script. */
1801 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1802 {
1803 bfd_boolean hide;
1804
1805 hi->verinfo.vertree
1806 = bfd_find_version_for_sym (info->version_info,
1807 hi->root.root.string, &hide);
1808 if (hi->verinfo.vertree != NULL && hide)
1809 {
1810 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1811 goto nondefault;
1812 }
1813 }
1814 if (hi->verinfo.vertree != NULL
1815 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1816 goto nondefault;
1817 }
1818
1819 if (! override)
1820 {
1821 /* Add the default symbol if not performing a relocatable link. */
1822 if (! bfd_link_relocatable (info))
1823 {
1824 bh = &hi->root;
1825 if (! (_bfd_generic_link_add_one_symbol
1826 (info, abfd, shortname, BSF_INDIRECT,
1827 bfd_ind_section_ptr,
1828 0, name, FALSE, collect, &bh)))
1829 return FALSE;
1830 hi = (struct elf_link_hash_entry *) bh;
1831 }
1832 }
1833 else
1834 {
1835 /* In this case the symbol named SHORTNAME is overriding the
1836 indirect symbol we want to add. We were planning on making
1837 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1838 is the name without a version. NAME is the fully versioned
1839 name, and it is the default version.
1840
1841 Overriding means that we already saw a definition for the
1842 symbol SHORTNAME in a regular object, and it is overriding
1843 the symbol defined in the dynamic object.
1844
1845 When this happens, we actually want to change NAME, the
1846 symbol we just added, to refer to SHORTNAME. This will cause
1847 references to NAME in the shared object to become references
1848 to SHORTNAME in the regular object. This is what we expect
1849 when we override a function in a shared object: that the
1850 references in the shared object will be mapped to the
1851 definition in the regular object. */
1852
1853 while (hi->root.type == bfd_link_hash_indirect
1854 || hi->root.type == bfd_link_hash_warning)
1855 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1856
1857 h->root.type = bfd_link_hash_indirect;
1858 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1859 if (h->def_dynamic)
1860 {
1861 h->def_dynamic = 0;
1862 hi->ref_dynamic = 1;
1863 if (hi->ref_regular
1864 || hi->def_regular)
1865 {
1866 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1867 return FALSE;
1868 }
1869 }
1870
1871 /* Now set HI to H, so that the following code will set the
1872 other fields correctly. */
1873 hi = h;
1874 }
1875
1876 /* Check if HI is a warning symbol. */
1877 if (hi->root.type == bfd_link_hash_warning)
1878 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1879
1880 /* If there is a duplicate definition somewhere, then HI may not
1881 point to an indirect symbol. We will have reported an error to
1882 the user in that case. */
1883
1884 if (hi->root.type == bfd_link_hash_indirect)
1885 {
1886 struct elf_link_hash_entry *ht;
1887
1888 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1889 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1890
1891 /* A reference to the SHORTNAME symbol from a dynamic library
1892 will be satisfied by the versioned symbol at runtime. In
1893 effect, we have a reference to the versioned symbol. */
1894 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1895 hi->dynamic_def |= ht->dynamic_def;
1896
1897 /* See if the new flags lead us to realize that the symbol must
1898 be dynamic. */
1899 if (! *dynsym)
1900 {
1901 if (! dynamic)
1902 {
1903 if (! bfd_link_executable (info)
1904 || hi->def_dynamic
1905 || hi->ref_dynamic)
1906 *dynsym = TRUE;
1907 }
1908 else
1909 {
1910 if (hi->ref_regular)
1911 *dynsym = TRUE;
1912 }
1913 }
1914 }
1915
1916 /* We also need to define an indirection from the nondefault version
1917 of the symbol. */
1918
1919 nondefault:
1920 len = strlen (name);
1921 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
1922 if (shortname == NULL)
1923 return FALSE;
1924 memcpy (shortname, name, shortlen);
1925 memcpy (shortname + shortlen, p + 1, len - shortlen);
1926
1927 /* Once again, merge with any existing symbol. */
1928 type_change_ok = FALSE;
1929 size_change_ok = FALSE;
1930 tmp_sec = sec;
1931 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1932 &hi, poldbfd, NULL, NULL, &skip, &override,
1933 &type_change_ok, &size_change_ok, &matched))
1934 return FALSE;
1935
1936 if (skip)
1937 return TRUE;
1938
1939 if (override)
1940 {
1941 /* Here SHORTNAME is a versioned name, so we don't expect to see
1942 the type of override we do in the case above unless it is
1943 overridden by a versioned definition. */
1944 if (hi->root.type != bfd_link_hash_defined
1945 && hi->root.type != bfd_link_hash_defweak)
1946 _bfd_error_handler
1947 /* xgettext:c-format */
1948 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1949 abfd, shortname);
1950 }
1951 else
1952 {
1953 bh = &hi->root;
1954 if (! (_bfd_generic_link_add_one_symbol
1955 (info, abfd, shortname, BSF_INDIRECT,
1956 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1957 return FALSE;
1958 hi = (struct elf_link_hash_entry *) bh;
1959
1960 /* If there is a duplicate definition somewhere, then HI may not
1961 point to an indirect symbol. We will have reported an error
1962 to the user in that case. */
1963
1964 if (hi->root.type == bfd_link_hash_indirect)
1965 {
1966 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1967 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1968 hi->dynamic_def |= h->dynamic_def;
1969
1970 /* See if the new flags lead us to realize that the symbol
1971 must be dynamic. */
1972 if (! *dynsym)
1973 {
1974 if (! dynamic)
1975 {
1976 if (! bfd_link_executable (info)
1977 || hi->ref_dynamic)
1978 *dynsym = TRUE;
1979 }
1980 else
1981 {
1982 if (hi->ref_regular)
1983 *dynsym = TRUE;
1984 }
1985 }
1986 }
1987 }
1988
1989 return TRUE;
1990 }
1991 \f
1992 /* This routine is used to export all defined symbols into the dynamic
1993 symbol table. It is called via elf_link_hash_traverse. */
1994
1995 static bfd_boolean
1996 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1997 {
1998 struct elf_info_failed *eif = (struct elf_info_failed *) data;
1999
2000 /* Ignore indirect symbols. These are added by the versioning code. */
2001 if (h->root.type == bfd_link_hash_indirect)
2002 return TRUE;
2003
2004 /* Ignore this if we won't export it. */
2005 if (!eif->info->export_dynamic && !h->dynamic)
2006 return TRUE;
2007
2008 if (h->dynindx == -1
2009 && (h->def_regular || h->ref_regular)
2010 && ! bfd_hide_sym_by_version (eif->info->version_info,
2011 h->root.root.string))
2012 {
2013 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2014 {
2015 eif->failed = TRUE;
2016 return FALSE;
2017 }
2018 }
2019
2020 return TRUE;
2021 }
2022 \f
2023 /* Look through the symbols which are defined in other shared
2024 libraries and referenced here. Update the list of version
2025 dependencies. This will be put into the .gnu.version_r section.
2026 This function is called via elf_link_hash_traverse. */
2027
2028 static bfd_boolean
2029 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2030 void *data)
2031 {
2032 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2033 Elf_Internal_Verneed *t;
2034 Elf_Internal_Vernaux *a;
2035 bfd_size_type amt;
2036
2037 /* We only care about symbols defined in shared objects with version
2038 information. */
2039 if (!h->def_dynamic
2040 || h->def_regular
2041 || h->dynindx == -1
2042 || h->verinfo.verdef == NULL
2043 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2044 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2045 return TRUE;
2046
2047 /* See if we already know about this version. */
2048 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2049 t != NULL;
2050 t = t->vn_nextref)
2051 {
2052 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2053 continue;
2054
2055 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2056 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2057 return TRUE;
2058
2059 break;
2060 }
2061
2062 /* This is a new version. Add it to tree we are building. */
2063
2064 if (t == NULL)
2065 {
2066 amt = sizeof *t;
2067 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2068 if (t == NULL)
2069 {
2070 rinfo->failed = TRUE;
2071 return FALSE;
2072 }
2073
2074 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2075 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2076 elf_tdata (rinfo->info->output_bfd)->verref = t;
2077 }
2078
2079 amt = sizeof *a;
2080 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2081 if (a == NULL)
2082 {
2083 rinfo->failed = TRUE;
2084 return FALSE;
2085 }
2086
2087 /* Note that we are copying a string pointer here, and testing it
2088 above. If bfd_elf_string_from_elf_section is ever changed to
2089 discard the string data when low in memory, this will have to be
2090 fixed. */
2091 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2092
2093 a->vna_flags = h->verinfo.verdef->vd_flags;
2094 a->vna_nextptr = t->vn_auxptr;
2095
2096 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2097 ++rinfo->vers;
2098
2099 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2100
2101 t->vn_auxptr = a;
2102
2103 return TRUE;
2104 }
2105
2106 /* Figure out appropriate versions for all the symbols. We may not
2107 have the version number script until we have read all of the input
2108 files, so until that point we don't know which symbols should be
2109 local. This function is called via elf_link_hash_traverse. */
2110
2111 static bfd_boolean
2112 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2113 {
2114 struct elf_info_failed *sinfo;
2115 struct bfd_link_info *info;
2116 const struct elf_backend_data *bed;
2117 struct elf_info_failed eif;
2118 char *p;
2119
2120 sinfo = (struct elf_info_failed *) data;
2121 info = sinfo->info;
2122
2123 /* Fix the symbol flags. */
2124 eif.failed = FALSE;
2125 eif.info = info;
2126 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2127 {
2128 if (eif.failed)
2129 sinfo->failed = TRUE;
2130 return FALSE;
2131 }
2132
2133 /* We only need version numbers for symbols defined in regular
2134 objects. */
2135 if (!h->def_regular)
2136 return TRUE;
2137
2138 bed = get_elf_backend_data (info->output_bfd);
2139 p = strchr (h->root.root.string, ELF_VER_CHR);
2140 if (p != NULL && h->verinfo.vertree == NULL)
2141 {
2142 struct bfd_elf_version_tree *t;
2143
2144 ++p;
2145 if (*p == ELF_VER_CHR)
2146 ++p;
2147
2148 /* If there is no version string, we can just return out. */
2149 if (*p == '\0')
2150 return TRUE;
2151
2152 /* Look for the version. If we find it, it is no longer weak. */
2153 for (t = sinfo->info->version_info; t != NULL; t = t->next)
2154 {
2155 if (strcmp (t->name, p) == 0)
2156 {
2157 size_t len;
2158 char *alc;
2159 struct bfd_elf_version_expr *d;
2160
2161 len = p - h->root.root.string;
2162 alc = (char *) bfd_malloc (len);
2163 if (alc == NULL)
2164 {
2165 sinfo->failed = TRUE;
2166 return FALSE;
2167 }
2168 memcpy (alc, h->root.root.string, len - 1);
2169 alc[len - 1] = '\0';
2170 if (alc[len - 2] == ELF_VER_CHR)
2171 alc[len - 2] = '\0';
2172
2173 h->verinfo.vertree = t;
2174 t->used = TRUE;
2175 d = NULL;
2176
2177 if (t->globals.list != NULL)
2178 d = (*t->match) (&t->globals, NULL, alc);
2179
2180 /* See if there is anything to force this symbol to
2181 local scope. */
2182 if (d == NULL && t->locals.list != NULL)
2183 {
2184 d = (*t->match) (&t->locals, NULL, alc);
2185 if (d != NULL
2186 && h->dynindx != -1
2187 && ! info->export_dynamic)
2188 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2189 }
2190
2191 free (alc);
2192 break;
2193 }
2194 }
2195
2196 /* If we are building an application, we need to create a
2197 version node for this version. */
2198 if (t == NULL && bfd_link_executable (info))
2199 {
2200 struct bfd_elf_version_tree **pp;
2201 int version_index;
2202
2203 /* If we aren't going to export this symbol, we don't need
2204 to worry about it. */
2205 if (h->dynindx == -1)
2206 return TRUE;
2207
2208 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2209 sizeof *t);
2210 if (t == NULL)
2211 {
2212 sinfo->failed = TRUE;
2213 return FALSE;
2214 }
2215
2216 t->name = p;
2217 t->name_indx = (unsigned int) -1;
2218 t->used = TRUE;
2219
2220 version_index = 1;
2221 /* Don't count anonymous version tag. */
2222 if (sinfo->info->version_info != NULL
2223 && sinfo->info->version_info->vernum == 0)
2224 version_index = 0;
2225 for (pp = &sinfo->info->version_info;
2226 *pp != NULL;
2227 pp = &(*pp)->next)
2228 ++version_index;
2229 t->vernum = version_index;
2230
2231 *pp = t;
2232
2233 h->verinfo.vertree = t;
2234 }
2235 else if (t == NULL)
2236 {
2237 /* We could not find the version for a symbol when
2238 generating a shared archive. Return an error. */
2239 _bfd_error_handler
2240 /* xgettext:c-format */
2241 (_("%B: version node not found for symbol %s"),
2242 info->output_bfd, h->root.root.string);
2243 bfd_set_error (bfd_error_bad_value);
2244 sinfo->failed = TRUE;
2245 return FALSE;
2246 }
2247 }
2248
2249 /* If we don't have a version for this symbol, see if we can find
2250 something. */
2251 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
2252 {
2253 bfd_boolean hide;
2254
2255 h->verinfo.vertree
2256 = bfd_find_version_for_sym (sinfo->info->version_info,
2257 h->root.root.string, &hide);
2258 if (h->verinfo.vertree != NULL && hide)
2259 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2260 }
2261
2262 return TRUE;
2263 }
2264 \f
2265 /* Read and swap the relocs from the section indicated by SHDR. This
2266 may be either a REL or a RELA section. The relocations are
2267 translated into RELA relocations and stored in INTERNAL_RELOCS,
2268 which should have already been allocated to contain enough space.
2269 The EXTERNAL_RELOCS are a buffer where the external form of the
2270 relocations should be stored.
2271
2272 Returns FALSE if something goes wrong. */
2273
2274 static bfd_boolean
2275 elf_link_read_relocs_from_section (bfd *abfd,
2276 asection *sec,
2277 Elf_Internal_Shdr *shdr,
2278 void *external_relocs,
2279 Elf_Internal_Rela *internal_relocs)
2280 {
2281 const struct elf_backend_data *bed;
2282 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2283 const bfd_byte *erela;
2284 const bfd_byte *erelaend;
2285 Elf_Internal_Rela *irela;
2286 Elf_Internal_Shdr *symtab_hdr;
2287 size_t nsyms;
2288
2289 /* Position ourselves at the start of the section. */
2290 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2291 return FALSE;
2292
2293 /* Read the relocations. */
2294 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2295 return FALSE;
2296
2297 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2298 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2299
2300 bed = get_elf_backend_data (abfd);
2301
2302 /* Convert the external relocations to the internal format. */
2303 if (shdr->sh_entsize == bed->s->sizeof_rel)
2304 swap_in = bed->s->swap_reloc_in;
2305 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2306 swap_in = bed->s->swap_reloca_in;
2307 else
2308 {
2309 bfd_set_error (bfd_error_wrong_format);
2310 return FALSE;
2311 }
2312
2313 erela = (const bfd_byte *) external_relocs;
2314 erelaend = erela + shdr->sh_size;
2315 irela = internal_relocs;
2316 while (erela < erelaend)
2317 {
2318 bfd_vma r_symndx;
2319
2320 (*swap_in) (abfd, erela, irela);
2321 r_symndx = ELF32_R_SYM (irela->r_info);
2322 if (bed->s->arch_size == 64)
2323 r_symndx >>= 24;
2324 if (nsyms > 0)
2325 {
2326 if ((size_t) r_symndx >= nsyms)
2327 {
2328 _bfd_error_handler
2329 /* xgettext:c-format */
2330 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2331 " for offset 0x%lx in section `%A'"),
2332 abfd, sec,
2333 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2334 bfd_set_error (bfd_error_bad_value);
2335 return FALSE;
2336 }
2337 }
2338 else if (r_symndx != STN_UNDEF)
2339 {
2340 _bfd_error_handler
2341 /* xgettext:c-format */
2342 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2343 " when the object file has no symbol table"),
2344 abfd, sec,
2345 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2346 bfd_set_error (bfd_error_bad_value);
2347 return FALSE;
2348 }
2349 irela += bed->s->int_rels_per_ext_rel;
2350 erela += shdr->sh_entsize;
2351 }
2352
2353 return TRUE;
2354 }
2355
2356 /* Read and swap the relocs for a section O. They may have been
2357 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2358 not NULL, they are used as buffers to read into. They are known to
2359 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2360 the return value is allocated using either malloc or bfd_alloc,
2361 according to the KEEP_MEMORY argument. If O has two relocation
2362 sections (both REL and RELA relocations), then the REL_HDR
2363 relocations will appear first in INTERNAL_RELOCS, followed by the
2364 RELA_HDR relocations. */
2365
2366 Elf_Internal_Rela *
2367 _bfd_elf_link_read_relocs (bfd *abfd,
2368 asection *o,
2369 void *external_relocs,
2370 Elf_Internal_Rela *internal_relocs,
2371 bfd_boolean keep_memory)
2372 {
2373 void *alloc1 = NULL;
2374 Elf_Internal_Rela *alloc2 = NULL;
2375 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2376 struct bfd_elf_section_data *esdo = elf_section_data (o);
2377 Elf_Internal_Rela *internal_rela_relocs;
2378
2379 if (esdo->relocs != NULL)
2380 return esdo->relocs;
2381
2382 if (o->reloc_count == 0)
2383 return NULL;
2384
2385 if (internal_relocs == NULL)
2386 {
2387 bfd_size_type size;
2388
2389 size = o->reloc_count;
2390 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2391 if (keep_memory)
2392 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2393 else
2394 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2395 if (internal_relocs == NULL)
2396 goto error_return;
2397 }
2398
2399 if (external_relocs == NULL)
2400 {
2401 bfd_size_type size = 0;
2402
2403 if (esdo->rel.hdr)
2404 size += esdo->rel.hdr->sh_size;
2405 if (esdo->rela.hdr)
2406 size += esdo->rela.hdr->sh_size;
2407
2408 alloc1 = bfd_malloc (size);
2409 if (alloc1 == NULL)
2410 goto error_return;
2411 external_relocs = alloc1;
2412 }
2413
2414 internal_rela_relocs = internal_relocs;
2415 if (esdo->rel.hdr)
2416 {
2417 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2418 external_relocs,
2419 internal_relocs))
2420 goto error_return;
2421 external_relocs = (((bfd_byte *) external_relocs)
2422 + esdo->rel.hdr->sh_size);
2423 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2424 * bed->s->int_rels_per_ext_rel);
2425 }
2426
2427 if (esdo->rela.hdr
2428 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2429 external_relocs,
2430 internal_rela_relocs)))
2431 goto error_return;
2432
2433 /* Cache the results for next time, if we can. */
2434 if (keep_memory)
2435 esdo->relocs = internal_relocs;
2436
2437 if (alloc1 != NULL)
2438 free (alloc1);
2439
2440 /* Don't free alloc2, since if it was allocated we are passing it
2441 back (under the name of internal_relocs). */
2442
2443 return internal_relocs;
2444
2445 error_return:
2446 if (alloc1 != NULL)
2447 free (alloc1);
2448 if (alloc2 != NULL)
2449 {
2450 if (keep_memory)
2451 bfd_release (abfd, alloc2);
2452 else
2453 free (alloc2);
2454 }
2455 return NULL;
2456 }
2457
2458 /* Compute the size of, and allocate space for, REL_HDR which is the
2459 section header for a section containing relocations for O. */
2460
2461 static bfd_boolean
2462 _bfd_elf_link_size_reloc_section (bfd *abfd,
2463 struct bfd_elf_section_reloc_data *reldata)
2464 {
2465 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2466
2467 /* That allows us to calculate the size of the section. */
2468 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2469
2470 /* The contents field must last into write_object_contents, so we
2471 allocate it with bfd_alloc rather than malloc. Also since we
2472 cannot be sure that the contents will actually be filled in,
2473 we zero the allocated space. */
2474 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2475 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2476 return FALSE;
2477
2478 if (reldata->hashes == NULL && reldata->count)
2479 {
2480 struct elf_link_hash_entry **p;
2481
2482 p = ((struct elf_link_hash_entry **)
2483 bfd_zmalloc (reldata->count * sizeof (*p)));
2484 if (p == NULL)
2485 return FALSE;
2486
2487 reldata->hashes = p;
2488 }
2489
2490 return TRUE;
2491 }
2492
2493 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2494 originated from the section given by INPUT_REL_HDR) to the
2495 OUTPUT_BFD. */
2496
2497 bfd_boolean
2498 _bfd_elf_link_output_relocs (bfd *output_bfd,
2499 asection *input_section,
2500 Elf_Internal_Shdr *input_rel_hdr,
2501 Elf_Internal_Rela *internal_relocs,
2502 struct elf_link_hash_entry **rel_hash
2503 ATTRIBUTE_UNUSED)
2504 {
2505 Elf_Internal_Rela *irela;
2506 Elf_Internal_Rela *irelaend;
2507 bfd_byte *erel;
2508 struct bfd_elf_section_reloc_data *output_reldata;
2509 asection *output_section;
2510 const struct elf_backend_data *bed;
2511 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2512 struct bfd_elf_section_data *esdo;
2513
2514 output_section = input_section->output_section;
2515
2516 bed = get_elf_backend_data (output_bfd);
2517 esdo = elf_section_data (output_section);
2518 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2519 {
2520 output_reldata = &esdo->rel;
2521 swap_out = bed->s->swap_reloc_out;
2522 }
2523 else if (esdo->rela.hdr
2524 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2525 {
2526 output_reldata = &esdo->rela;
2527 swap_out = bed->s->swap_reloca_out;
2528 }
2529 else
2530 {
2531 _bfd_error_handler
2532 /* xgettext:c-format */
2533 (_("%B: relocation size mismatch in %B section %A"),
2534 output_bfd, input_section->owner, input_section);
2535 bfd_set_error (bfd_error_wrong_format);
2536 return FALSE;
2537 }
2538
2539 erel = output_reldata->hdr->contents;
2540 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2541 irela = internal_relocs;
2542 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2543 * bed->s->int_rels_per_ext_rel);
2544 while (irela < irelaend)
2545 {
2546 (*swap_out) (output_bfd, irela, erel);
2547 irela += bed->s->int_rels_per_ext_rel;
2548 erel += input_rel_hdr->sh_entsize;
2549 }
2550
2551 /* Bump the counter, so that we know where to add the next set of
2552 relocations. */
2553 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2554
2555 return TRUE;
2556 }
2557 \f
2558 /* Make weak undefined symbols in PIE dynamic. */
2559
2560 bfd_boolean
2561 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2562 struct elf_link_hash_entry *h)
2563 {
2564 if (bfd_link_pie (info)
2565 && h->dynindx == -1
2566 && h->root.type == bfd_link_hash_undefweak)
2567 return bfd_elf_link_record_dynamic_symbol (info, h);
2568
2569 return TRUE;
2570 }
2571
2572 /* Fix up the flags for a symbol. This handles various cases which
2573 can only be fixed after all the input files are seen. This is
2574 currently called by both adjust_dynamic_symbol and
2575 assign_sym_version, which is unnecessary but perhaps more robust in
2576 the face of future changes. */
2577
2578 static bfd_boolean
2579 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2580 struct elf_info_failed *eif)
2581 {
2582 const struct elf_backend_data *bed;
2583
2584 /* If this symbol was mentioned in a non-ELF file, try to set
2585 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2586 permit a non-ELF file to correctly refer to a symbol defined in
2587 an ELF dynamic object. */
2588 if (h->non_elf)
2589 {
2590 while (h->root.type == bfd_link_hash_indirect)
2591 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2592
2593 if (h->root.type != bfd_link_hash_defined
2594 && h->root.type != bfd_link_hash_defweak)
2595 {
2596 h->ref_regular = 1;
2597 h->ref_regular_nonweak = 1;
2598 }
2599 else
2600 {
2601 if (h->root.u.def.section->owner != NULL
2602 && (bfd_get_flavour (h->root.u.def.section->owner)
2603 == bfd_target_elf_flavour))
2604 {
2605 h->ref_regular = 1;
2606 h->ref_regular_nonweak = 1;
2607 }
2608 else
2609 h->def_regular = 1;
2610 }
2611
2612 if (h->dynindx == -1
2613 && (h->def_dynamic
2614 || h->ref_dynamic))
2615 {
2616 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2617 {
2618 eif->failed = TRUE;
2619 return FALSE;
2620 }
2621 }
2622 }
2623 else
2624 {
2625 /* Unfortunately, NON_ELF is only correct if the symbol
2626 was first seen in a non-ELF file. Fortunately, if the symbol
2627 was first seen in an ELF file, we're probably OK unless the
2628 symbol was defined in a non-ELF file. Catch that case here.
2629 FIXME: We're still in trouble if the symbol was first seen in
2630 a dynamic object, and then later in a non-ELF regular object. */
2631 if ((h->root.type == bfd_link_hash_defined
2632 || h->root.type == bfd_link_hash_defweak)
2633 && !h->def_regular
2634 && (h->root.u.def.section->owner != NULL
2635 ? (bfd_get_flavour (h->root.u.def.section->owner)
2636 != bfd_target_elf_flavour)
2637 : (bfd_is_abs_section (h->root.u.def.section)
2638 && !h->def_dynamic)))
2639 h->def_regular = 1;
2640 }
2641
2642 /* Backend specific symbol fixup. */
2643 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2644 if (bed->elf_backend_fixup_symbol
2645 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2646 return FALSE;
2647
2648 /* If this is a final link, and the symbol was defined as a common
2649 symbol in a regular object file, and there was no definition in
2650 any dynamic object, then the linker will have allocated space for
2651 the symbol in a common section but the DEF_REGULAR
2652 flag will not have been set. */
2653 if (h->root.type == bfd_link_hash_defined
2654 && !h->def_regular
2655 && h->ref_regular
2656 && !h->def_dynamic
2657 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2658 h->def_regular = 1;
2659
2660 /* If a weak undefined symbol has non-default visibility, we also
2661 hide it from the dynamic linker. */
2662 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2663 && h->root.type == bfd_link_hash_undefweak)
2664 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2665
2666 /* A hidden versioned symbol in executable should be forced local if
2667 it is is locally defined, not referenced by shared library and not
2668 exported. */
2669 else if (bfd_link_executable (eif->info)
2670 && h->versioned == versioned_hidden
2671 && !eif->info->export_dynamic
2672 && !h->dynamic
2673 && !h->ref_dynamic
2674 && h->def_regular)
2675 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2676
2677 /* If -Bsymbolic was used (which means to bind references to global
2678 symbols to the definition within the shared object), and this
2679 symbol was defined in a regular object, then it actually doesn't
2680 need a PLT entry. Likewise, if the symbol has non-default
2681 visibility. If the symbol has hidden or internal visibility, we
2682 will force it local. */
2683 else if (h->needs_plt
2684 && bfd_link_pic (eif->info)
2685 && is_elf_hash_table (eif->info->hash)
2686 && (SYMBOLIC_BIND (eif->info, h)
2687 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2688 && h->def_regular)
2689 {
2690 bfd_boolean force_local;
2691
2692 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2693 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2694 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2695 }
2696
2697 /* If this is a weak defined symbol in a dynamic object, and we know
2698 the real definition in the dynamic object, copy interesting flags
2699 over to the real definition. */
2700 if (h->u.weakdef != NULL)
2701 {
2702 /* If the real definition is defined by a regular object file,
2703 don't do anything special. See the longer description in
2704 _bfd_elf_adjust_dynamic_symbol, below. */
2705 if (h->u.weakdef->def_regular)
2706 h->u.weakdef = NULL;
2707 else
2708 {
2709 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2710
2711 while (h->root.type == bfd_link_hash_indirect)
2712 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2713
2714 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2715 || h->root.type == bfd_link_hash_defweak);
2716 BFD_ASSERT (weakdef->def_dynamic);
2717 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2718 || weakdef->root.type == bfd_link_hash_defweak);
2719 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2720 }
2721 }
2722
2723 return TRUE;
2724 }
2725
2726 /* Make the backend pick a good value for a dynamic symbol. This is
2727 called via elf_link_hash_traverse, and also calls itself
2728 recursively. */
2729
2730 static bfd_boolean
2731 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2732 {
2733 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2734 bfd *dynobj;
2735 const struct elf_backend_data *bed;
2736
2737 if (! is_elf_hash_table (eif->info->hash))
2738 return FALSE;
2739
2740 /* Ignore indirect symbols. These are added by the versioning code. */
2741 if (h->root.type == bfd_link_hash_indirect)
2742 return TRUE;
2743
2744 /* Fix the symbol flags. */
2745 if (! _bfd_elf_fix_symbol_flags (h, eif))
2746 return FALSE;
2747
2748 /* If this symbol does not require a PLT entry, and it is not
2749 defined by a dynamic object, or is not referenced by a regular
2750 object, ignore it. We do have to handle a weak defined symbol,
2751 even if no regular object refers to it, if we decided to add it
2752 to the dynamic symbol table. FIXME: Do we normally need to worry
2753 about symbols which are defined by one dynamic object and
2754 referenced by another one? */
2755 if (!h->needs_plt
2756 && h->type != STT_GNU_IFUNC
2757 && (h->def_regular
2758 || !h->def_dynamic
2759 || (!h->ref_regular
2760 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2761 {
2762 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2763 return TRUE;
2764 }
2765
2766 /* If we've already adjusted this symbol, don't do it again. This
2767 can happen via a recursive call. */
2768 if (h->dynamic_adjusted)
2769 return TRUE;
2770
2771 /* Don't look at this symbol again. Note that we must set this
2772 after checking the above conditions, because we may look at a
2773 symbol once, decide not to do anything, and then get called
2774 recursively later after REF_REGULAR is set below. */
2775 h->dynamic_adjusted = 1;
2776
2777 /* If this is a weak definition, and we know a real definition, and
2778 the real symbol is not itself defined by a regular object file,
2779 then get a good value for the real definition. We handle the
2780 real symbol first, for the convenience of the backend routine.
2781
2782 Note that there is a confusing case here. If the real definition
2783 is defined by a regular object file, we don't get the real symbol
2784 from the dynamic object, but we do get the weak symbol. If the
2785 processor backend uses a COPY reloc, then if some routine in the
2786 dynamic object changes the real symbol, we will not see that
2787 change in the corresponding weak symbol. This is the way other
2788 ELF linkers work as well, and seems to be a result of the shared
2789 library model.
2790
2791 I will clarify this issue. Most SVR4 shared libraries define the
2792 variable _timezone and define timezone as a weak synonym. The
2793 tzset call changes _timezone. If you write
2794 extern int timezone;
2795 int _timezone = 5;
2796 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2797 you might expect that, since timezone is a synonym for _timezone,
2798 the same number will print both times. However, if the processor
2799 backend uses a COPY reloc, then actually timezone will be copied
2800 into your process image, and, since you define _timezone
2801 yourself, _timezone will not. Thus timezone and _timezone will
2802 wind up at different memory locations. The tzset call will set
2803 _timezone, leaving timezone unchanged. */
2804
2805 if (h->u.weakdef != NULL)
2806 {
2807 /* If we get to this point, there is an implicit reference to
2808 H->U.WEAKDEF by a regular object file via the weak symbol H. */
2809 h->u.weakdef->ref_regular = 1;
2810
2811 /* Ensure that the backend adjust_dynamic_symbol function sees
2812 H->U.WEAKDEF before H by recursively calling ourselves. */
2813 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2814 return FALSE;
2815 }
2816
2817 /* If a symbol has no type and no size and does not require a PLT
2818 entry, then we are probably about to do the wrong thing here: we
2819 are probably going to create a COPY reloc for an empty object.
2820 This case can arise when a shared object is built with assembly
2821 code, and the assembly code fails to set the symbol type. */
2822 if (h->size == 0
2823 && h->type == STT_NOTYPE
2824 && !h->needs_plt)
2825 _bfd_error_handler
2826 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2827 h->root.root.string);
2828
2829 dynobj = elf_hash_table (eif->info)->dynobj;
2830 bed = get_elf_backend_data (dynobj);
2831
2832 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2833 {
2834 eif->failed = TRUE;
2835 return FALSE;
2836 }
2837
2838 return TRUE;
2839 }
2840
2841 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2842 DYNBSS. */
2843
2844 bfd_boolean
2845 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2846 struct elf_link_hash_entry *h,
2847 asection *dynbss)
2848 {
2849 unsigned int power_of_two;
2850 bfd_vma mask;
2851 asection *sec = h->root.u.def.section;
2852
2853 /* The section aligment of definition is the maximum alignment
2854 requirement of symbols defined in the section. Since we don't
2855 know the symbol alignment requirement, we start with the
2856 maximum alignment and check low bits of the symbol address
2857 for the minimum alignment. */
2858 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2859 mask = ((bfd_vma) 1 << power_of_two) - 1;
2860 while ((h->root.u.def.value & mask) != 0)
2861 {
2862 mask >>= 1;
2863 --power_of_two;
2864 }
2865
2866 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2867 dynbss))
2868 {
2869 /* Adjust the section alignment if needed. */
2870 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2871 power_of_two))
2872 return FALSE;
2873 }
2874
2875 /* We make sure that the symbol will be aligned properly. */
2876 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2877
2878 /* Define the symbol as being at this point in DYNBSS. */
2879 h->root.u.def.section = dynbss;
2880 h->root.u.def.value = dynbss->size;
2881
2882 /* Increment the size of DYNBSS to make room for the symbol. */
2883 dynbss->size += h->size;
2884
2885 /* No error if extern_protected_data is true. */
2886 if (h->protected_def
2887 && (!info->extern_protected_data
2888 || (info->extern_protected_data < 0
2889 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
2890 info->callbacks->einfo
2891 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2892 h->root.root.string);
2893
2894 return TRUE;
2895 }
2896
2897 /* Adjust all external symbols pointing into SEC_MERGE sections
2898 to reflect the object merging within the sections. */
2899
2900 static bfd_boolean
2901 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2902 {
2903 asection *sec;
2904
2905 if ((h->root.type == bfd_link_hash_defined
2906 || h->root.type == bfd_link_hash_defweak)
2907 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2908 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
2909 {
2910 bfd *output_bfd = (bfd *) data;
2911
2912 h->root.u.def.value =
2913 _bfd_merged_section_offset (output_bfd,
2914 &h->root.u.def.section,
2915 elf_section_data (sec)->sec_info,
2916 h->root.u.def.value);
2917 }
2918
2919 return TRUE;
2920 }
2921
2922 /* Returns false if the symbol referred to by H should be considered
2923 to resolve local to the current module, and true if it should be
2924 considered to bind dynamically. */
2925
2926 bfd_boolean
2927 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2928 struct bfd_link_info *info,
2929 bfd_boolean not_local_protected)
2930 {
2931 bfd_boolean binding_stays_local_p;
2932 const struct elf_backend_data *bed;
2933 struct elf_link_hash_table *hash_table;
2934
2935 if (h == NULL)
2936 return FALSE;
2937
2938 while (h->root.type == bfd_link_hash_indirect
2939 || h->root.type == bfd_link_hash_warning)
2940 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2941
2942 /* If it was forced local, then clearly it's not dynamic. */
2943 if (h->dynindx == -1)
2944 return FALSE;
2945 if (h->forced_local)
2946 return FALSE;
2947
2948 /* Identify the cases where name binding rules say that a
2949 visible symbol resolves locally. */
2950 binding_stays_local_p = (bfd_link_executable (info)
2951 || SYMBOLIC_BIND (info, h));
2952
2953 switch (ELF_ST_VISIBILITY (h->other))
2954 {
2955 case STV_INTERNAL:
2956 case STV_HIDDEN:
2957 return FALSE;
2958
2959 case STV_PROTECTED:
2960 hash_table = elf_hash_table (info);
2961 if (!is_elf_hash_table (hash_table))
2962 return FALSE;
2963
2964 bed = get_elf_backend_data (hash_table->dynobj);
2965
2966 /* Proper resolution for function pointer equality may require
2967 that these symbols perhaps be resolved dynamically, even though
2968 we should be resolving them to the current module. */
2969 if (!not_local_protected || !bed->is_function_type (h->type))
2970 binding_stays_local_p = TRUE;
2971 break;
2972
2973 default:
2974 break;
2975 }
2976
2977 /* If it isn't defined locally, then clearly it's dynamic. */
2978 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2979 return TRUE;
2980
2981 /* Otherwise, the symbol is dynamic if binding rules don't tell
2982 us that it remains local. */
2983 return !binding_stays_local_p;
2984 }
2985
2986 /* Return true if the symbol referred to by H should be considered
2987 to resolve local to the current module, and false otherwise. Differs
2988 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2989 undefined symbols. The two functions are virtually identical except
2990 for the place where forced_local and dynindx == -1 are tested. If
2991 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2992 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2993 the symbol is local only for defined symbols.
2994 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2995 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2996 treatment of undefined weak symbols. For those that do not make
2997 undefined weak symbols dynamic, both functions may return false. */
2998
2999 bfd_boolean
3000 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3001 struct bfd_link_info *info,
3002 bfd_boolean local_protected)
3003 {
3004 const struct elf_backend_data *bed;
3005 struct elf_link_hash_table *hash_table;
3006
3007 /* If it's a local sym, of course we resolve locally. */
3008 if (h == NULL)
3009 return TRUE;
3010
3011 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3012 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3013 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3014 return TRUE;
3015
3016 /* Common symbols that become definitions don't get the DEF_REGULAR
3017 flag set, so test it first, and don't bail out. */
3018 if (ELF_COMMON_DEF_P (h))
3019 /* Do nothing. */;
3020 /* If we don't have a definition in a regular file, then we can't
3021 resolve locally. The sym is either undefined or dynamic. */
3022 else if (!h->def_regular)
3023 return FALSE;
3024
3025 /* Forced local symbols resolve locally. */
3026 if (h->forced_local)
3027 return TRUE;
3028
3029 /* As do non-dynamic symbols. */
3030 if (h->dynindx == -1)
3031 return TRUE;
3032
3033 /* At this point, we know the symbol is defined and dynamic. In an
3034 executable it must resolve locally, likewise when building symbolic
3035 shared libraries. */
3036 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3037 return TRUE;
3038
3039 /* Now deal with defined dynamic symbols in shared libraries. Ones
3040 with default visibility might not resolve locally. */
3041 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3042 return FALSE;
3043
3044 hash_table = elf_hash_table (info);
3045 if (!is_elf_hash_table (hash_table))
3046 return TRUE;
3047
3048 bed = get_elf_backend_data (hash_table->dynobj);
3049
3050 /* If extern_protected_data is false, STV_PROTECTED non-function
3051 symbols are local. */
3052 if ((!info->extern_protected_data
3053 || (info->extern_protected_data < 0
3054 && !bed->extern_protected_data))
3055 && !bed->is_function_type (h->type))
3056 return TRUE;
3057
3058 /* Function pointer equality tests may require that STV_PROTECTED
3059 symbols be treated as dynamic symbols. If the address of a
3060 function not defined in an executable is set to that function's
3061 plt entry in the executable, then the address of the function in
3062 a shared library must also be the plt entry in the executable. */
3063 return local_protected;
3064 }
3065
3066 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3067 aligned. Returns the first TLS output section. */
3068
3069 struct bfd_section *
3070 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3071 {
3072 struct bfd_section *sec, *tls;
3073 unsigned int align = 0;
3074
3075 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3076 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3077 break;
3078 tls = sec;
3079
3080 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3081 if (sec->alignment_power > align)
3082 align = sec->alignment_power;
3083
3084 elf_hash_table (info)->tls_sec = tls;
3085
3086 /* Ensure the alignment of the first section is the largest alignment,
3087 so that the tls segment starts aligned. */
3088 if (tls != NULL)
3089 tls->alignment_power = align;
3090
3091 return tls;
3092 }
3093
3094 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3095 static bfd_boolean
3096 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3097 Elf_Internal_Sym *sym)
3098 {
3099 const struct elf_backend_data *bed;
3100
3101 /* Local symbols do not count, but target specific ones might. */
3102 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3103 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3104 return FALSE;
3105
3106 bed = get_elf_backend_data (abfd);
3107 /* Function symbols do not count. */
3108 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3109 return FALSE;
3110
3111 /* If the section is undefined, then so is the symbol. */
3112 if (sym->st_shndx == SHN_UNDEF)
3113 return FALSE;
3114
3115 /* If the symbol is defined in the common section, then
3116 it is a common definition and so does not count. */
3117 if (bed->common_definition (sym))
3118 return FALSE;
3119
3120 /* If the symbol is in a target specific section then we
3121 must rely upon the backend to tell us what it is. */
3122 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3123 /* FIXME - this function is not coded yet:
3124
3125 return _bfd_is_global_symbol_definition (abfd, sym);
3126
3127 Instead for now assume that the definition is not global,
3128 Even if this is wrong, at least the linker will behave
3129 in the same way that it used to do. */
3130 return FALSE;
3131
3132 return TRUE;
3133 }
3134
3135 /* Search the symbol table of the archive element of the archive ABFD
3136 whose archive map contains a mention of SYMDEF, and determine if
3137 the symbol is defined in this element. */
3138 static bfd_boolean
3139 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3140 {
3141 Elf_Internal_Shdr * hdr;
3142 size_t symcount;
3143 size_t extsymcount;
3144 size_t extsymoff;
3145 Elf_Internal_Sym *isymbuf;
3146 Elf_Internal_Sym *isym;
3147 Elf_Internal_Sym *isymend;
3148 bfd_boolean result;
3149
3150 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3151 if (abfd == NULL)
3152 return FALSE;
3153
3154 if (! bfd_check_format (abfd, bfd_object))
3155 return FALSE;
3156
3157 /* Select the appropriate symbol table. If we don't know if the
3158 object file is an IR object, give linker LTO plugin a chance to
3159 get the correct symbol table. */
3160 if (abfd->plugin_format == bfd_plugin_yes
3161 #if BFD_SUPPORTS_PLUGINS
3162 || (abfd->plugin_format == bfd_plugin_unknown
3163 && bfd_link_plugin_object_p (abfd))
3164 #endif
3165 )
3166 {
3167 /* Use the IR symbol table if the object has been claimed by
3168 plugin. */
3169 abfd = abfd->plugin_dummy_bfd;
3170 hdr = &elf_tdata (abfd)->symtab_hdr;
3171 }
3172 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3173 hdr = &elf_tdata (abfd)->symtab_hdr;
3174 else
3175 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3176
3177 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3178
3179 /* The sh_info field of the symtab header tells us where the
3180 external symbols start. We don't care about the local symbols. */
3181 if (elf_bad_symtab (abfd))
3182 {
3183 extsymcount = symcount;
3184 extsymoff = 0;
3185 }
3186 else
3187 {
3188 extsymcount = symcount - hdr->sh_info;
3189 extsymoff = hdr->sh_info;
3190 }
3191
3192 if (extsymcount == 0)
3193 return FALSE;
3194
3195 /* Read in the symbol table. */
3196 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3197 NULL, NULL, NULL);
3198 if (isymbuf == NULL)
3199 return FALSE;
3200
3201 /* Scan the symbol table looking for SYMDEF. */
3202 result = FALSE;
3203 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3204 {
3205 const char *name;
3206
3207 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3208 isym->st_name);
3209 if (name == NULL)
3210 break;
3211
3212 if (strcmp (name, symdef->name) == 0)
3213 {
3214 result = is_global_data_symbol_definition (abfd, isym);
3215 break;
3216 }
3217 }
3218
3219 free (isymbuf);
3220
3221 return result;
3222 }
3223 \f
3224 /* Add an entry to the .dynamic table. */
3225
3226 bfd_boolean
3227 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3228 bfd_vma tag,
3229 bfd_vma val)
3230 {
3231 struct elf_link_hash_table *hash_table;
3232 const struct elf_backend_data *bed;
3233 asection *s;
3234 bfd_size_type newsize;
3235 bfd_byte *newcontents;
3236 Elf_Internal_Dyn dyn;
3237
3238 hash_table = elf_hash_table (info);
3239 if (! is_elf_hash_table (hash_table))
3240 return FALSE;
3241
3242 bed = get_elf_backend_data (hash_table->dynobj);
3243 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3244 BFD_ASSERT (s != NULL);
3245
3246 newsize = s->size + bed->s->sizeof_dyn;
3247 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3248 if (newcontents == NULL)
3249 return FALSE;
3250
3251 dyn.d_tag = tag;
3252 dyn.d_un.d_val = val;
3253 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3254
3255 s->size = newsize;
3256 s->contents = newcontents;
3257
3258 return TRUE;
3259 }
3260
3261 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3262 otherwise just check whether one already exists. Returns -1 on error,
3263 1 if a DT_NEEDED tag already exists, and 0 on success. */
3264
3265 static int
3266 elf_add_dt_needed_tag (bfd *abfd,
3267 struct bfd_link_info *info,
3268 const char *soname,
3269 bfd_boolean do_it)
3270 {
3271 struct elf_link_hash_table *hash_table;
3272 size_t strindex;
3273
3274 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3275 return -1;
3276
3277 hash_table = elf_hash_table (info);
3278 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3279 if (strindex == (size_t) -1)
3280 return -1;
3281
3282 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3283 {
3284 asection *sdyn;
3285 const struct elf_backend_data *bed;
3286 bfd_byte *extdyn;
3287
3288 bed = get_elf_backend_data (hash_table->dynobj);
3289 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3290 if (sdyn != NULL)
3291 for (extdyn = sdyn->contents;
3292 extdyn < sdyn->contents + sdyn->size;
3293 extdyn += bed->s->sizeof_dyn)
3294 {
3295 Elf_Internal_Dyn dyn;
3296
3297 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3298 if (dyn.d_tag == DT_NEEDED
3299 && dyn.d_un.d_val == strindex)
3300 {
3301 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3302 return 1;
3303 }
3304 }
3305 }
3306
3307 if (do_it)
3308 {
3309 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3310 return -1;
3311
3312 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3313 return -1;
3314 }
3315 else
3316 /* We were just checking for existence of the tag. */
3317 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3318
3319 return 0;
3320 }
3321
3322 /* Return true if SONAME is on the needed list between NEEDED and STOP
3323 (or the end of list if STOP is NULL), and needed by a library that
3324 will be loaded. */
3325
3326 static bfd_boolean
3327 on_needed_list (const char *soname,
3328 struct bfd_link_needed_list *needed,
3329 struct bfd_link_needed_list *stop)
3330 {
3331 struct bfd_link_needed_list *look;
3332 for (look = needed; look != stop; look = look->next)
3333 if (strcmp (soname, look->name) == 0
3334 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3335 /* If needed by a library that itself is not directly
3336 needed, recursively check whether that library is
3337 indirectly needed. Since we add DT_NEEDED entries to
3338 the end of the list, library dependencies appear after
3339 the library. Therefore search prior to the current
3340 LOOK, preventing possible infinite recursion. */
3341 || on_needed_list (elf_dt_name (look->by), needed, look)))
3342 return TRUE;
3343
3344 return FALSE;
3345 }
3346
3347 /* Sort symbol by value, section, and size. */
3348 static int
3349 elf_sort_symbol (const void *arg1, const void *arg2)
3350 {
3351 const struct elf_link_hash_entry *h1;
3352 const struct elf_link_hash_entry *h2;
3353 bfd_signed_vma vdiff;
3354
3355 h1 = *(const struct elf_link_hash_entry **) arg1;
3356 h2 = *(const struct elf_link_hash_entry **) arg2;
3357 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3358 if (vdiff != 0)
3359 return vdiff > 0 ? 1 : -1;
3360 else
3361 {
3362 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3363 if (sdiff != 0)
3364 return sdiff > 0 ? 1 : -1;
3365 }
3366 vdiff = h1->size - h2->size;
3367 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3368 }
3369
3370 /* This function is used to adjust offsets into .dynstr for
3371 dynamic symbols. This is called via elf_link_hash_traverse. */
3372
3373 static bfd_boolean
3374 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3375 {
3376 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3377
3378 if (h->dynindx != -1)
3379 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3380 return TRUE;
3381 }
3382
3383 /* Assign string offsets in .dynstr, update all structures referencing
3384 them. */
3385
3386 static bfd_boolean
3387 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3388 {
3389 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3390 struct elf_link_local_dynamic_entry *entry;
3391 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3392 bfd *dynobj = hash_table->dynobj;
3393 asection *sdyn;
3394 bfd_size_type size;
3395 const struct elf_backend_data *bed;
3396 bfd_byte *extdyn;
3397
3398 _bfd_elf_strtab_finalize (dynstr);
3399 size = _bfd_elf_strtab_size (dynstr);
3400
3401 bed = get_elf_backend_data (dynobj);
3402 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3403 BFD_ASSERT (sdyn != NULL);
3404
3405 /* Update all .dynamic entries referencing .dynstr strings. */
3406 for (extdyn = sdyn->contents;
3407 extdyn < sdyn->contents + sdyn->size;
3408 extdyn += bed->s->sizeof_dyn)
3409 {
3410 Elf_Internal_Dyn dyn;
3411
3412 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3413 switch (dyn.d_tag)
3414 {
3415 case DT_STRSZ:
3416 dyn.d_un.d_val = size;
3417 break;
3418 case DT_NEEDED:
3419 case DT_SONAME:
3420 case DT_RPATH:
3421 case DT_RUNPATH:
3422 case DT_FILTER:
3423 case DT_AUXILIARY:
3424 case DT_AUDIT:
3425 case DT_DEPAUDIT:
3426 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3427 break;
3428 default:
3429 continue;
3430 }
3431 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3432 }
3433
3434 /* Now update local dynamic symbols. */
3435 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3436 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3437 entry->isym.st_name);
3438
3439 /* And the rest of dynamic symbols. */
3440 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3441
3442 /* Adjust version definitions. */
3443 if (elf_tdata (output_bfd)->cverdefs)
3444 {
3445 asection *s;
3446 bfd_byte *p;
3447 size_t i;
3448 Elf_Internal_Verdef def;
3449 Elf_Internal_Verdaux defaux;
3450
3451 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3452 p = s->contents;
3453 do
3454 {
3455 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3456 &def);
3457 p += sizeof (Elf_External_Verdef);
3458 if (def.vd_aux != sizeof (Elf_External_Verdef))
3459 continue;
3460 for (i = 0; i < def.vd_cnt; ++i)
3461 {
3462 _bfd_elf_swap_verdaux_in (output_bfd,
3463 (Elf_External_Verdaux *) p, &defaux);
3464 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3465 defaux.vda_name);
3466 _bfd_elf_swap_verdaux_out (output_bfd,
3467 &defaux, (Elf_External_Verdaux *) p);
3468 p += sizeof (Elf_External_Verdaux);
3469 }
3470 }
3471 while (def.vd_next);
3472 }
3473
3474 /* Adjust version references. */
3475 if (elf_tdata (output_bfd)->verref)
3476 {
3477 asection *s;
3478 bfd_byte *p;
3479 size_t i;
3480 Elf_Internal_Verneed need;
3481 Elf_Internal_Vernaux needaux;
3482
3483 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3484 p = s->contents;
3485 do
3486 {
3487 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3488 &need);
3489 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3490 _bfd_elf_swap_verneed_out (output_bfd, &need,
3491 (Elf_External_Verneed *) p);
3492 p += sizeof (Elf_External_Verneed);
3493 for (i = 0; i < need.vn_cnt; ++i)
3494 {
3495 _bfd_elf_swap_vernaux_in (output_bfd,
3496 (Elf_External_Vernaux *) p, &needaux);
3497 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3498 needaux.vna_name);
3499 _bfd_elf_swap_vernaux_out (output_bfd,
3500 &needaux,
3501 (Elf_External_Vernaux *) p);
3502 p += sizeof (Elf_External_Vernaux);
3503 }
3504 }
3505 while (need.vn_next);
3506 }
3507
3508 return TRUE;
3509 }
3510 \f
3511 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3512 The default is to only match when the INPUT and OUTPUT are exactly
3513 the same target. */
3514
3515 bfd_boolean
3516 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3517 const bfd_target *output)
3518 {
3519 return input == output;
3520 }
3521
3522 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3523 This version is used when different targets for the same architecture
3524 are virtually identical. */
3525
3526 bfd_boolean
3527 _bfd_elf_relocs_compatible (const bfd_target *input,
3528 const bfd_target *output)
3529 {
3530 const struct elf_backend_data *obed, *ibed;
3531
3532 if (input == output)
3533 return TRUE;
3534
3535 ibed = xvec_get_elf_backend_data (input);
3536 obed = xvec_get_elf_backend_data (output);
3537
3538 if (ibed->arch != obed->arch)
3539 return FALSE;
3540
3541 /* If both backends are using this function, deem them compatible. */
3542 return ibed->relocs_compatible == obed->relocs_compatible;
3543 }
3544
3545 /* Make a special call to the linker "notice" function to tell it that
3546 we are about to handle an as-needed lib, or have finished
3547 processing the lib. */
3548
3549 bfd_boolean
3550 _bfd_elf_notice_as_needed (bfd *ibfd,
3551 struct bfd_link_info *info,
3552 enum notice_asneeded_action act)
3553 {
3554 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3555 }
3556
3557 /* Check relocations an ELF object file. */
3558
3559 bfd_boolean
3560 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3561 {
3562 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3563 struct elf_link_hash_table *htab = elf_hash_table (info);
3564
3565 /* If this object is the same format as the output object, and it is
3566 not a shared library, then let the backend look through the
3567 relocs.
3568
3569 This is required to build global offset table entries and to
3570 arrange for dynamic relocs. It is not required for the
3571 particular common case of linking non PIC code, even when linking
3572 against shared libraries, but unfortunately there is no way of
3573 knowing whether an object file has been compiled PIC or not.
3574 Looking through the relocs is not particularly time consuming.
3575 The problem is that we must either (1) keep the relocs in memory,
3576 which causes the linker to require additional runtime memory or
3577 (2) read the relocs twice from the input file, which wastes time.
3578 This would be a good case for using mmap.
3579
3580 I have no idea how to handle linking PIC code into a file of a
3581 different format. It probably can't be done. */
3582 if ((abfd->flags & DYNAMIC) == 0
3583 && is_elf_hash_table (htab)
3584 && bed->check_relocs != NULL
3585 && elf_object_id (abfd) == elf_hash_table_id (htab)
3586 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3587 {
3588 asection *o;
3589
3590 for (o = abfd->sections; o != NULL; o = o->next)
3591 {
3592 Elf_Internal_Rela *internal_relocs;
3593 bfd_boolean ok;
3594
3595 /* Don't check relocations in excluded sections. */
3596 if ((o->flags & SEC_RELOC) == 0
3597 || (o->flags & SEC_EXCLUDE) != 0
3598 || o->reloc_count == 0
3599 || ((info->strip == strip_all || info->strip == strip_debugger)
3600 && (o->flags & SEC_DEBUGGING) != 0)
3601 || bfd_is_abs_section (o->output_section))
3602 continue;
3603
3604 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3605 info->keep_memory);
3606 if (internal_relocs == NULL)
3607 return FALSE;
3608
3609 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3610
3611 if (elf_section_data (o)->relocs != internal_relocs)
3612 free (internal_relocs);
3613
3614 if (! ok)
3615 return FALSE;
3616 }
3617 }
3618
3619 return TRUE;
3620 }
3621
3622 /* Add symbols from an ELF object file to the linker hash table. */
3623
3624 static bfd_boolean
3625 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3626 {
3627 Elf_Internal_Ehdr *ehdr;
3628 Elf_Internal_Shdr *hdr;
3629 size_t symcount;
3630 size_t extsymcount;
3631 size_t extsymoff;
3632 struct elf_link_hash_entry **sym_hash;
3633 bfd_boolean dynamic;
3634 Elf_External_Versym *extversym = NULL;
3635 Elf_External_Versym *ever;
3636 struct elf_link_hash_entry *weaks;
3637 struct elf_link_hash_entry **nondeflt_vers = NULL;
3638 size_t nondeflt_vers_cnt = 0;
3639 Elf_Internal_Sym *isymbuf = NULL;
3640 Elf_Internal_Sym *isym;
3641 Elf_Internal_Sym *isymend;
3642 const struct elf_backend_data *bed;
3643 bfd_boolean add_needed;
3644 struct elf_link_hash_table *htab;
3645 bfd_size_type amt;
3646 void *alloc_mark = NULL;
3647 struct bfd_hash_entry **old_table = NULL;
3648 unsigned int old_size = 0;
3649 unsigned int old_count = 0;
3650 void *old_tab = NULL;
3651 void *old_ent;
3652 struct bfd_link_hash_entry *old_undefs = NULL;
3653 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3654 void *old_strtab = NULL;
3655 size_t tabsize = 0;
3656 asection *s;
3657 bfd_boolean just_syms;
3658
3659 htab = elf_hash_table (info);
3660 bed = get_elf_backend_data (abfd);
3661
3662 if ((abfd->flags & DYNAMIC) == 0)
3663 dynamic = FALSE;
3664 else
3665 {
3666 dynamic = TRUE;
3667
3668 /* You can't use -r against a dynamic object. Also, there's no
3669 hope of using a dynamic object which does not exactly match
3670 the format of the output file. */
3671 if (bfd_link_relocatable (info)
3672 || !is_elf_hash_table (htab)
3673 || info->output_bfd->xvec != abfd->xvec)
3674 {
3675 if (bfd_link_relocatable (info))
3676 bfd_set_error (bfd_error_invalid_operation);
3677 else
3678 bfd_set_error (bfd_error_wrong_format);
3679 goto error_return;
3680 }
3681 }
3682
3683 ehdr = elf_elfheader (abfd);
3684 if (info->warn_alternate_em
3685 && bed->elf_machine_code != ehdr->e_machine
3686 && ((bed->elf_machine_alt1 != 0
3687 && ehdr->e_machine == bed->elf_machine_alt1)
3688 || (bed->elf_machine_alt2 != 0
3689 && ehdr->e_machine == bed->elf_machine_alt2)))
3690 info->callbacks->einfo
3691 /* xgettext:c-format */
3692 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3693 ehdr->e_machine, abfd, bed->elf_machine_code);
3694
3695 /* As a GNU extension, any input sections which are named
3696 .gnu.warning.SYMBOL are treated as warning symbols for the given
3697 symbol. This differs from .gnu.warning sections, which generate
3698 warnings when they are included in an output file. */
3699 /* PR 12761: Also generate this warning when building shared libraries. */
3700 for (s = abfd->sections; s != NULL; s = s->next)
3701 {
3702 const char *name;
3703
3704 name = bfd_get_section_name (abfd, s);
3705 if (CONST_STRNEQ (name, ".gnu.warning."))
3706 {
3707 char *msg;
3708 bfd_size_type sz;
3709
3710 name += sizeof ".gnu.warning." - 1;
3711
3712 /* If this is a shared object, then look up the symbol
3713 in the hash table. If it is there, and it is already
3714 been defined, then we will not be using the entry
3715 from this shared object, so we don't need to warn.
3716 FIXME: If we see the definition in a regular object
3717 later on, we will warn, but we shouldn't. The only
3718 fix is to keep track of what warnings we are supposed
3719 to emit, and then handle them all at the end of the
3720 link. */
3721 if (dynamic)
3722 {
3723 struct elf_link_hash_entry *h;
3724
3725 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3726
3727 /* FIXME: What about bfd_link_hash_common? */
3728 if (h != NULL
3729 && (h->root.type == bfd_link_hash_defined
3730 || h->root.type == bfd_link_hash_defweak))
3731 continue;
3732 }
3733
3734 sz = s->size;
3735 msg = (char *) bfd_alloc (abfd, sz + 1);
3736 if (msg == NULL)
3737 goto error_return;
3738
3739 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3740 goto error_return;
3741
3742 msg[sz] = '\0';
3743
3744 if (! (_bfd_generic_link_add_one_symbol
3745 (info, abfd, name, BSF_WARNING, s, 0, msg,
3746 FALSE, bed->collect, NULL)))
3747 goto error_return;
3748
3749 if (bfd_link_executable (info))
3750 {
3751 /* Clobber the section size so that the warning does
3752 not get copied into the output file. */
3753 s->size = 0;
3754
3755 /* Also set SEC_EXCLUDE, so that symbols defined in
3756 the warning section don't get copied to the output. */
3757 s->flags |= SEC_EXCLUDE;
3758 }
3759 }
3760 }
3761
3762 just_syms = ((s = abfd->sections) != NULL
3763 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3764
3765 add_needed = TRUE;
3766 if (! dynamic)
3767 {
3768 /* If we are creating a shared library, create all the dynamic
3769 sections immediately. We need to attach them to something,
3770 so we attach them to this BFD, provided it is the right
3771 format and is not from ld --just-symbols. Always create the
3772 dynamic sections for -E/--dynamic-list. FIXME: If there
3773 are no input BFD's of the same format as the output, we can't
3774 make a shared library. */
3775 if (!just_syms
3776 && (bfd_link_pic (info)
3777 || (!bfd_link_relocatable (info)
3778 && (info->export_dynamic || info->dynamic)))
3779 && is_elf_hash_table (htab)
3780 && info->output_bfd->xvec == abfd->xvec
3781 && !htab->dynamic_sections_created)
3782 {
3783 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3784 goto error_return;
3785 }
3786 }
3787 else if (!is_elf_hash_table (htab))
3788 goto error_return;
3789 else
3790 {
3791 const char *soname = NULL;
3792 char *audit = NULL;
3793 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3794 int ret;
3795
3796 /* ld --just-symbols and dynamic objects don't mix very well.
3797 ld shouldn't allow it. */
3798 if (just_syms)
3799 abort ();
3800
3801 /* If this dynamic lib was specified on the command line with
3802 --as-needed in effect, then we don't want to add a DT_NEEDED
3803 tag unless the lib is actually used. Similary for libs brought
3804 in by another lib's DT_NEEDED. When --no-add-needed is used
3805 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3806 any dynamic library in DT_NEEDED tags in the dynamic lib at
3807 all. */
3808 add_needed = (elf_dyn_lib_class (abfd)
3809 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3810 | DYN_NO_NEEDED)) == 0;
3811
3812 s = bfd_get_section_by_name (abfd, ".dynamic");
3813 if (s != NULL)
3814 {
3815 bfd_byte *dynbuf;
3816 bfd_byte *extdyn;
3817 unsigned int elfsec;
3818 unsigned long shlink;
3819
3820 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3821 {
3822 error_free_dyn:
3823 free (dynbuf);
3824 goto error_return;
3825 }
3826
3827 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3828 if (elfsec == SHN_BAD)
3829 goto error_free_dyn;
3830 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3831
3832 for (extdyn = dynbuf;
3833 extdyn < dynbuf + s->size;
3834 extdyn += bed->s->sizeof_dyn)
3835 {
3836 Elf_Internal_Dyn dyn;
3837
3838 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3839 if (dyn.d_tag == DT_SONAME)
3840 {
3841 unsigned int tagv = dyn.d_un.d_val;
3842 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3843 if (soname == NULL)
3844 goto error_free_dyn;
3845 }
3846 if (dyn.d_tag == DT_NEEDED)
3847 {
3848 struct bfd_link_needed_list *n, **pn;
3849 char *fnm, *anm;
3850 unsigned int tagv = dyn.d_un.d_val;
3851
3852 amt = sizeof (struct bfd_link_needed_list);
3853 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3854 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3855 if (n == NULL || fnm == NULL)
3856 goto error_free_dyn;
3857 amt = strlen (fnm) + 1;
3858 anm = (char *) bfd_alloc (abfd, amt);
3859 if (anm == NULL)
3860 goto error_free_dyn;
3861 memcpy (anm, fnm, amt);
3862 n->name = anm;
3863 n->by = abfd;
3864 n->next = NULL;
3865 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3866 ;
3867 *pn = n;
3868 }
3869 if (dyn.d_tag == DT_RUNPATH)
3870 {
3871 struct bfd_link_needed_list *n, **pn;
3872 char *fnm, *anm;
3873 unsigned int tagv = dyn.d_un.d_val;
3874
3875 amt = sizeof (struct bfd_link_needed_list);
3876 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3877 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3878 if (n == NULL || fnm == NULL)
3879 goto error_free_dyn;
3880 amt = strlen (fnm) + 1;
3881 anm = (char *) bfd_alloc (abfd, amt);
3882 if (anm == NULL)
3883 goto error_free_dyn;
3884 memcpy (anm, fnm, amt);
3885 n->name = anm;
3886 n->by = abfd;
3887 n->next = NULL;
3888 for (pn = & runpath;
3889 *pn != NULL;
3890 pn = &(*pn)->next)
3891 ;
3892 *pn = n;
3893 }
3894 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3895 if (!runpath && dyn.d_tag == DT_RPATH)
3896 {
3897 struct bfd_link_needed_list *n, **pn;
3898 char *fnm, *anm;
3899 unsigned int tagv = dyn.d_un.d_val;
3900
3901 amt = sizeof (struct bfd_link_needed_list);
3902 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3903 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3904 if (n == NULL || fnm == NULL)
3905 goto error_free_dyn;
3906 amt = strlen (fnm) + 1;
3907 anm = (char *) bfd_alloc (abfd, amt);
3908 if (anm == NULL)
3909 goto error_free_dyn;
3910 memcpy (anm, fnm, amt);
3911 n->name = anm;
3912 n->by = abfd;
3913 n->next = NULL;
3914 for (pn = & rpath;
3915 *pn != NULL;
3916 pn = &(*pn)->next)
3917 ;
3918 *pn = n;
3919 }
3920 if (dyn.d_tag == DT_AUDIT)
3921 {
3922 unsigned int tagv = dyn.d_un.d_val;
3923 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3924 }
3925 }
3926
3927 free (dynbuf);
3928 }
3929
3930 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3931 frees all more recently bfd_alloc'd blocks as well. */
3932 if (runpath)
3933 rpath = runpath;
3934
3935 if (rpath)
3936 {
3937 struct bfd_link_needed_list **pn;
3938 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3939 ;
3940 *pn = rpath;
3941 }
3942
3943 /* We do not want to include any of the sections in a dynamic
3944 object in the output file. We hack by simply clobbering the
3945 list of sections in the BFD. This could be handled more
3946 cleanly by, say, a new section flag; the existing
3947 SEC_NEVER_LOAD flag is not the one we want, because that one
3948 still implies that the section takes up space in the output
3949 file. */
3950 bfd_section_list_clear (abfd);
3951
3952 /* Find the name to use in a DT_NEEDED entry that refers to this
3953 object. If the object has a DT_SONAME entry, we use it.
3954 Otherwise, if the generic linker stuck something in
3955 elf_dt_name, we use that. Otherwise, we just use the file
3956 name. */
3957 if (soname == NULL || *soname == '\0')
3958 {
3959 soname = elf_dt_name (abfd);
3960 if (soname == NULL || *soname == '\0')
3961 soname = bfd_get_filename (abfd);
3962 }
3963
3964 /* Save the SONAME because sometimes the linker emulation code
3965 will need to know it. */
3966 elf_dt_name (abfd) = soname;
3967
3968 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3969 if (ret < 0)
3970 goto error_return;
3971
3972 /* If we have already included this dynamic object in the
3973 link, just ignore it. There is no reason to include a
3974 particular dynamic object more than once. */
3975 if (ret > 0)
3976 return TRUE;
3977
3978 /* Save the DT_AUDIT entry for the linker emulation code. */
3979 elf_dt_audit (abfd) = audit;
3980 }
3981
3982 /* If this is a dynamic object, we always link against the .dynsym
3983 symbol table, not the .symtab symbol table. The dynamic linker
3984 will only see the .dynsym symbol table, so there is no reason to
3985 look at .symtab for a dynamic object. */
3986
3987 if (! dynamic || elf_dynsymtab (abfd) == 0)
3988 hdr = &elf_tdata (abfd)->symtab_hdr;
3989 else
3990 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3991
3992 symcount = hdr->sh_size / bed->s->sizeof_sym;
3993
3994 /* The sh_info field of the symtab header tells us where the
3995 external symbols start. We don't care about the local symbols at
3996 this point. */
3997 if (elf_bad_symtab (abfd))
3998 {
3999 extsymcount = symcount;
4000 extsymoff = 0;
4001 }
4002 else
4003 {
4004 extsymcount = symcount - hdr->sh_info;
4005 extsymoff = hdr->sh_info;
4006 }
4007
4008 sym_hash = elf_sym_hashes (abfd);
4009 if (extsymcount != 0)
4010 {
4011 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4012 NULL, NULL, NULL);
4013 if (isymbuf == NULL)
4014 goto error_return;
4015
4016 if (sym_hash == NULL)
4017 {
4018 /* We store a pointer to the hash table entry for each
4019 external symbol. */
4020 amt = extsymcount;
4021 amt *= sizeof (struct elf_link_hash_entry *);
4022 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4023 if (sym_hash == NULL)
4024 goto error_free_sym;
4025 elf_sym_hashes (abfd) = sym_hash;
4026 }
4027 }
4028
4029 if (dynamic)
4030 {
4031 /* Read in any version definitions. */
4032 if (!_bfd_elf_slurp_version_tables (abfd,
4033 info->default_imported_symver))
4034 goto error_free_sym;
4035
4036 /* Read in the symbol versions, but don't bother to convert them
4037 to internal format. */
4038 if (elf_dynversym (abfd) != 0)
4039 {
4040 Elf_Internal_Shdr *versymhdr;
4041
4042 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4043 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4044 if (extversym == NULL)
4045 goto error_free_sym;
4046 amt = versymhdr->sh_size;
4047 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4048 || bfd_bread (extversym, amt, abfd) != amt)
4049 goto error_free_vers;
4050 }
4051 }
4052
4053 /* If we are loading an as-needed shared lib, save the symbol table
4054 state before we start adding symbols. If the lib turns out
4055 to be unneeded, restore the state. */
4056 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4057 {
4058 unsigned int i;
4059 size_t entsize;
4060
4061 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4062 {
4063 struct bfd_hash_entry *p;
4064 struct elf_link_hash_entry *h;
4065
4066 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4067 {
4068 h = (struct elf_link_hash_entry *) p;
4069 entsize += htab->root.table.entsize;
4070 if (h->root.type == bfd_link_hash_warning)
4071 entsize += htab->root.table.entsize;
4072 }
4073 }
4074
4075 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4076 old_tab = bfd_malloc (tabsize + entsize);
4077 if (old_tab == NULL)
4078 goto error_free_vers;
4079
4080 /* Remember the current objalloc pointer, so that all mem for
4081 symbols added can later be reclaimed. */
4082 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4083 if (alloc_mark == NULL)
4084 goto error_free_vers;
4085
4086 /* Make a special call to the linker "notice" function to
4087 tell it that we are about to handle an as-needed lib. */
4088 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4089 goto error_free_vers;
4090
4091 /* Clone the symbol table. Remember some pointers into the
4092 symbol table, and dynamic symbol count. */
4093 old_ent = (char *) old_tab + tabsize;
4094 memcpy (old_tab, htab->root.table.table, tabsize);
4095 old_undefs = htab->root.undefs;
4096 old_undefs_tail = htab->root.undefs_tail;
4097 old_table = htab->root.table.table;
4098 old_size = htab->root.table.size;
4099 old_count = htab->root.table.count;
4100 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4101 if (old_strtab == NULL)
4102 goto error_free_vers;
4103
4104 for (i = 0; i < htab->root.table.size; i++)
4105 {
4106 struct bfd_hash_entry *p;
4107 struct elf_link_hash_entry *h;
4108
4109 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4110 {
4111 memcpy (old_ent, p, htab->root.table.entsize);
4112 old_ent = (char *) old_ent + htab->root.table.entsize;
4113 h = (struct elf_link_hash_entry *) p;
4114 if (h->root.type == bfd_link_hash_warning)
4115 {
4116 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4117 old_ent = (char *) old_ent + htab->root.table.entsize;
4118 }
4119 }
4120 }
4121 }
4122
4123 weaks = NULL;
4124 ever = extversym != NULL ? extversym + extsymoff : NULL;
4125 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4126 isym < isymend;
4127 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4128 {
4129 int bind;
4130 bfd_vma value;
4131 asection *sec, *new_sec;
4132 flagword flags;
4133 const char *name;
4134 struct elf_link_hash_entry *h;
4135 struct elf_link_hash_entry *hi;
4136 bfd_boolean definition;
4137 bfd_boolean size_change_ok;
4138 bfd_boolean type_change_ok;
4139 bfd_boolean new_weakdef;
4140 bfd_boolean new_weak;
4141 bfd_boolean old_weak;
4142 bfd_boolean override;
4143 bfd_boolean common;
4144 bfd_boolean discarded;
4145 unsigned int old_alignment;
4146 bfd *old_bfd;
4147 bfd_boolean matched;
4148
4149 override = FALSE;
4150
4151 flags = BSF_NO_FLAGS;
4152 sec = NULL;
4153 value = isym->st_value;
4154 common = bed->common_definition (isym);
4155 discarded = FALSE;
4156
4157 bind = ELF_ST_BIND (isym->st_info);
4158 switch (bind)
4159 {
4160 case STB_LOCAL:
4161 /* This should be impossible, since ELF requires that all
4162 global symbols follow all local symbols, and that sh_info
4163 point to the first global symbol. Unfortunately, Irix 5
4164 screws this up. */
4165 continue;
4166
4167 case STB_GLOBAL:
4168 if (isym->st_shndx != SHN_UNDEF && !common)
4169 flags = BSF_GLOBAL;
4170 break;
4171
4172 case STB_WEAK:
4173 flags = BSF_WEAK;
4174 break;
4175
4176 case STB_GNU_UNIQUE:
4177 flags = BSF_GNU_UNIQUE;
4178 break;
4179
4180 default:
4181 /* Leave it up to the processor backend. */
4182 break;
4183 }
4184
4185 if (isym->st_shndx == SHN_UNDEF)
4186 sec = bfd_und_section_ptr;
4187 else if (isym->st_shndx == SHN_ABS)
4188 sec = bfd_abs_section_ptr;
4189 else if (isym->st_shndx == SHN_COMMON)
4190 {
4191 sec = bfd_com_section_ptr;
4192 /* What ELF calls the size we call the value. What ELF
4193 calls the value we call the alignment. */
4194 value = isym->st_size;
4195 }
4196 else
4197 {
4198 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4199 if (sec == NULL)
4200 sec = bfd_abs_section_ptr;
4201 else if (discarded_section (sec))
4202 {
4203 /* Symbols from discarded section are undefined. We keep
4204 its visibility. */
4205 sec = bfd_und_section_ptr;
4206 discarded = TRUE;
4207 isym->st_shndx = SHN_UNDEF;
4208 }
4209 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4210 value -= sec->vma;
4211 }
4212
4213 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4214 isym->st_name);
4215 if (name == NULL)
4216 goto error_free_vers;
4217
4218 if (isym->st_shndx == SHN_COMMON
4219 && (abfd->flags & BFD_PLUGIN) != 0)
4220 {
4221 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4222
4223 if (xc == NULL)
4224 {
4225 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4226 | SEC_EXCLUDE);
4227 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4228 if (xc == NULL)
4229 goto error_free_vers;
4230 }
4231 sec = xc;
4232 }
4233 else if (isym->st_shndx == SHN_COMMON
4234 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4235 && !bfd_link_relocatable (info))
4236 {
4237 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4238
4239 if (tcomm == NULL)
4240 {
4241 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4242 | SEC_LINKER_CREATED);
4243 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4244 if (tcomm == NULL)
4245 goto error_free_vers;
4246 }
4247 sec = tcomm;
4248 }
4249 else if (bed->elf_add_symbol_hook)
4250 {
4251 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4252 &sec, &value))
4253 goto error_free_vers;
4254
4255 /* The hook function sets the name to NULL if this symbol
4256 should be skipped for some reason. */
4257 if (name == NULL)
4258 continue;
4259 }
4260
4261 /* Sanity check that all possibilities were handled. */
4262 if (sec == NULL)
4263 {
4264 bfd_set_error (bfd_error_bad_value);
4265 goto error_free_vers;
4266 }
4267
4268 /* Silently discard TLS symbols from --just-syms. There's
4269 no way to combine a static TLS block with a new TLS block
4270 for this executable. */
4271 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4272 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4273 continue;
4274
4275 if (bfd_is_und_section (sec)
4276 || bfd_is_com_section (sec))
4277 definition = FALSE;
4278 else
4279 definition = TRUE;
4280
4281 size_change_ok = FALSE;
4282 type_change_ok = bed->type_change_ok;
4283 old_weak = FALSE;
4284 matched = FALSE;
4285 old_alignment = 0;
4286 old_bfd = NULL;
4287 new_sec = sec;
4288
4289 if (is_elf_hash_table (htab))
4290 {
4291 Elf_Internal_Versym iver;
4292 unsigned int vernum = 0;
4293 bfd_boolean skip;
4294
4295 if (ever == NULL)
4296 {
4297 if (info->default_imported_symver)
4298 /* Use the default symbol version created earlier. */
4299 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4300 else
4301 iver.vs_vers = 0;
4302 }
4303 else
4304 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4305
4306 vernum = iver.vs_vers & VERSYM_VERSION;
4307
4308 /* If this is a hidden symbol, or if it is not version
4309 1, we append the version name to the symbol name.
4310 However, we do not modify a non-hidden absolute symbol
4311 if it is not a function, because it might be the version
4312 symbol itself. FIXME: What if it isn't? */
4313 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4314 || (vernum > 1
4315 && (!bfd_is_abs_section (sec)
4316 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4317 {
4318 const char *verstr;
4319 size_t namelen, verlen, newlen;
4320 char *newname, *p;
4321
4322 if (isym->st_shndx != SHN_UNDEF)
4323 {
4324 if (vernum > elf_tdata (abfd)->cverdefs)
4325 verstr = NULL;
4326 else if (vernum > 1)
4327 verstr =
4328 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4329 else
4330 verstr = "";
4331
4332 if (verstr == NULL)
4333 {
4334 _bfd_error_handler
4335 /* xgettext:c-format */
4336 (_("%B: %s: invalid version %u (max %d)"),
4337 abfd, name, vernum,
4338 elf_tdata (abfd)->cverdefs);
4339 bfd_set_error (bfd_error_bad_value);
4340 goto error_free_vers;
4341 }
4342 }
4343 else
4344 {
4345 /* We cannot simply test for the number of
4346 entries in the VERNEED section since the
4347 numbers for the needed versions do not start
4348 at 0. */
4349 Elf_Internal_Verneed *t;
4350
4351 verstr = NULL;
4352 for (t = elf_tdata (abfd)->verref;
4353 t != NULL;
4354 t = t->vn_nextref)
4355 {
4356 Elf_Internal_Vernaux *a;
4357
4358 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4359 {
4360 if (a->vna_other == vernum)
4361 {
4362 verstr = a->vna_nodename;
4363 break;
4364 }
4365 }
4366 if (a != NULL)
4367 break;
4368 }
4369 if (verstr == NULL)
4370 {
4371 _bfd_error_handler
4372 /* xgettext:c-format */
4373 (_("%B: %s: invalid needed version %d"),
4374 abfd, name, vernum);
4375 bfd_set_error (bfd_error_bad_value);
4376 goto error_free_vers;
4377 }
4378 }
4379
4380 namelen = strlen (name);
4381 verlen = strlen (verstr);
4382 newlen = namelen + verlen + 2;
4383 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4384 && isym->st_shndx != SHN_UNDEF)
4385 ++newlen;
4386
4387 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4388 if (newname == NULL)
4389 goto error_free_vers;
4390 memcpy (newname, name, namelen);
4391 p = newname + namelen;
4392 *p++ = ELF_VER_CHR;
4393 /* If this is a defined non-hidden version symbol,
4394 we add another @ to the name. This indicates the
4395 default version of the symbol. */
4396 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4397 && isym->st_shndx != SHN_UNDEF)
4398 *p++ = ELF_VER_CHR;
4399 memcpy (p, verstr, verlen + 1);
4400
4401 name = newname;
4402 }
4403
4404 /* If this symbol has default visibility and the user has
4405 requested we not re-export it, then mark it as hidden. */
4406 if (!bfd_is_und_section (sec)
4407 && !dynamic
4408 && abfd->no_export
4409 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4410 isym->st_other = (STV_HIDDEN
4411 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4412
4413 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4414 sym_hash, &old_bfd, &old_weak,
4415 &old_alignment, &skip, &override,
4416 &type_change_ok, &size_change_ok,
4417 &matched))
4418 goto error_free_vers;
4419
4420 if (skip)
4421 continue;
4422
4423 /* Override a definition only if the new symbol matches the
4424 existing one. */
4425 if (override && matched)
4426 definition = FALSE;
4427
4428 h = *sym_hash;
4429 while (h->root.type == bfd_link_hash_indirect
4430 || h->root.type == bfd_link_hash_warning)
4431 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4432
4433 if (elf_tdata (abfd)->verdef != NULL
4434 && vernum > 1
4435 && definition)
4436 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4437 }
4438
4439 if (! (_bfd_generic_link_add_one_symbol
4440 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4441 (struct bfd_link_hash_entry **) sym_hash)))
4442 goto error_free_vers;
4443
4444 if ((flags & BSF_GNU_UNIQUE)
4445 && (abfd->flags & DYNAMIC) == 0
4446 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4447 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique;
4448
4449 h = *sym_hash;
4450 /* We need to make sure that indirect symbol dynamic flags are
4451 updated. */
4452 hi = h;
4453 while (h->root.type == bfd_link_hash_indirect
4454 || h->root.type == bfd_link_hash_warning)
4455 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4456
4457 /* Setting the index to -3 tells elf_link_output_extsym that
4458 this symbol is defined in a discarded section. */
4459 if (discarded)
4460 h->indx = -3;
4461
4462 *sym_hash = h;
4463
4464 new_weak = (flags & BSF_WEAK) != 0;
4465 new_weakdef = FALSE;
4466 if (dynamic
4467 && definition
4468 && new_weak
4469 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4470 && is_elf_hash_table (htab)
4471 && h->u.weakdef == NULL)
4472 {
4473 /* Keep a list of all weak defined non function symbols from
4474 a dynamic object, using the weakdef field. Later in this
4475 function we will set the weakdef field to the correct
4476 value. We only put non-function symbols from dynamic
4477 objects on this list, because that happens to be the only
4478 time we need to know the normal symbol corresponding to a
4479 weak symbol, and the information is time consuming to
4480 figure out. If the weakdef field is not already NULL,
4481 then this symbol was already defined by some previous
4482 dynamic object, and we will be using that previous
4483 definition anyhow. */
4484
4485 h->u.weakdef = weaks;
4486 weaks = h;
4487 new_weakdef = TRUE;
4488 }
4489
4490 /* Set the alignment of a common symbol. */
4491 if ((common || bfd_is_com_section (sec))
4492 && h->root.type == bfd_link_hash_common)
4493 {
4494 unsigned int align;
4495
4496 if (common)
4497 align = bfd_log2 (isym->st_value);
4498 else
4499 {
4500 /* The new symbol is a common symbol in a shared object.
4501 We need to get the alignment from the section. */
4502 align = new_sec->alignment_power;
4503 }
4504 if (align > old_alignment)
4505 h->root.u.c.p->alignment_power = align;
4506 else
4507 h->root.u.c.p->alignment_power = old_alignment;
4508 }
4509
4510 if (is_elf_hash_table (htab))
4511 {
4512 /* Set a flag in the hash table entry indicating the type of
4513 reference or definition we just found. A dynamic symbol
4514 is one which is referenced or defined by both a regular
4515 object and a shared object. */
4516 bfd_boolean dynsym = FALSE;
4517
4518 /* Plugin symbols aren't normal. Don't set def_regular or
4519 ref_regular for them, or make them dynamic. */
4520 if ((abfd->flags & BFD_PLUGIN) != 0)
4521 ;
4522 else if (! dynamic)
4523 {
4524 if (! definition)
4525 {
4526 h->ref_regular = 1;
4527 if (bind != STB_WEAK)
4528 h->ref_regular_nonweak = 1;
4529 }
4530 else
4531 {
4532 h->def_regular = 1;
4533 if (h->def_dynamic)
4534 {
4535 h->def_dynamic = 0;
4536 h->ref_dynamic = 1;
4537 }
4538 }
4539
4540 /* If the indirect symbol has been forced local, don't
4541 make the real symbol dynamic. */
4542 if ((h == hi || !hi->forced_local)
4543 && (bfd_link_dll (info)
4544 || h->def_dynamic
4545 || h->ref_dynamic))
4546 dynsym = TRUE;
4547 }
4548 else
4549 {
4550 if (! definition)
4551 {
4552 h->ref_dynamic = 1;
4553 hi->ref_dynamic = 1;
4554 }
4555 else
4556 {
4557 h->def_dynamic = 1;
4558 hi->def_dynamic = 1;
4559 }
4560
4561 /* If the indirect symbol has been forced local, don't
4562 make the real symbol dynamic. */
4563 if ((h == hi || !hi->forced_local)
4564 && (h->def_regular
4565 || h->ref_regular
4566 || (h->u.weakdef != NULL
4567 && ! new_weakdef
4568 && h->u.weakdef->dynindx != -1)))
4569 dynsym = TRUE;
4570 }
4571
4572 /* Check to see if we need to add an indirect symbol for
4573 the default name. */
4574 if (definition
4575 || (!override && h->root.type == bfd_link_hash_common))
4576 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4577 sec, value, &old_bfd, &dynsym))
4578 goto error_free_vers;
4579
4580 /* Check the alignment when a common symbol is involved. This
4581 can change when a common symbol is overridden by a normal
4582 definition or a common symbol is ignored due to the old
4583 normal definition. We need to make sure the maximum
4584 alignment is maintained. */
4585 if ((old_alignment || common)
4586 && h->root.type != bfd_link_hash_common)
4587 {
4588 unsigned int common_align;
4589 unsigned int normal_align;
4590 unsigned int symbol_align;
4591 bfd *normal_bfd;
4592 bfd *common_bfd;
4593
4594 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4595 || h->root.type == bfd_link_hash_defweak);
4596
4597 symbol_align = ffs (h->root.u.def.value) - 1;
4598 if (h->root.u.def.section->owner != NULL
4599 && (h->root.u.def.section->owner->flags
4600 & (DYNAMIC | BFD_PLUGIN)) == 0)
4601 {
4602 normal_align = h->root.u.def.section->alignment_power;
4603 if (normal_align > symbol_align)
4604 normal_align = symbol_align;
4605 }
4606 else
4607 normal_align = symbol_align;
4608
4609 if (old_alignment)
4610 {
4611 common_align = old_alignment;
4612 common_bfd = old_bfd;
4613 normal_bfd = abfd;
4614 }
4615 else
4616 {
4617 common_align = bfd_log2 (isym->st_value);
4618 common_bfd = abfd;
4619 normal_bfd = old_bfd;
4620 }
4621
4622 if (normal_align < common_align)
4623 {
4624 /* PR binutils/2735 */
4625 if (normal_bfd == NULL)
4626 _bfd_error_handler
4627 /* xgettext:c-format */
4628 (_("Warning: alignment %u of common symbol `%s' in %B is"
4629 " greater than the alignment (%u) of its section %A"),
4630 common_bfd, h->root.u.def.section,
4631 1 << common_align, name, 1 << normal_align);
4632 else
4633 _bfd_error_handler
4634 /* xgettext:c-format */
4635 (_("Warning: alignment %u of symbol `%s' in %B"
4636 " is smaller than %u in %B"),
4637 normal_bfd, common_bfd,
4638 1 << normal_align, name, 1 << common_align);
4639 }
4640 }
4641
4642 /* Remember the symbol size if it isn't undefined. */
4643 if (isym->st_size != 0
4644 && isym->st_shndx != SHN_UNDEF
4645 && (definition || h->size == 0))
4646 {
4647 if (h->size != 0
4648 && h->size != isym->st_size
4649 && ! size_change_ok)
4650 _bfd_error_handler
4651 /* xgettext:c-format */
4652 (_("Warning: size of symbol `%s' changed"
4653 " from %lu in %B to %lu in %B"),
4654 old_bfd, abfd,
4655 name, (unsigned long) h->size,
4656 (unsigned long) isym->st_size);
4657
4658 h->size = isym->st_size;
4659 }
4660
4661 /* If this is a common symbol, then we always want H->SIZE
4662 to be the size of the common symbol. The code just above
4663 won't fix the size if a common symbol becomes larger. We
4664 don't warn about a size change here, because that is
4665 covered by --warn-common. Allow changes between different
4666 function types. */
4667 if (h->root.type == bfd_link_hash_common)
4668 h->size = h->root.u.c.size;
4669
4670 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4671 && ((definition && !new_weak)
4672 || (old_weak && h->root.type == bfd_link_hash_common)
4673 || h->type == STT_NOTYPE))
4674 {
4675 unsigned int type = ELF_ST_TYPE (isym->st_info);
4676
4677 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4678 symbol. */
4679 if (type == STT_GNU_IFUNC
4680 && (abfd->flags & DYNAMIC) != 0)
4681 type = STT_FUNC;
4682
4683 if (h->type != type)
4684 {
4685 if (h->type != STT_NOTYPE && ! type_change_ok)
4686 /* xgettext:c-format */
4687 _bfd_error_handler
4688 (_("Warning: type of symbol `%s' changed"
4689 " from %d to %d in %B"),
4690 abfd, name, h->type, type);
4691
4692 h->type = type;
4693 }
4694 }
4695
4696 /* Merge st_other field. */
4697 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4698
4699 /* We don't want to make debug symbol dynamic. */
4700 if (definition
4701 && (sec->flags & SEC_DEBUGGING)
4702 && !bfd_link_relocatable (info))
4703 dynsym = FALSE;
4704
4705 /* Nor should we make plugin symbols dynamic. */
4706 if ((abfd->flags & BFD_PLUGIN) != 0)
4707 dynsym = FALSE;
4708
4709 if (definition)
4710 {
4711 h->target_internal = isym->st_target_internal;
4712 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4713 }
4714
4715 if (definition && !dynamic)
4716 {
4717 char *p = strchr (name, ELF_VER_CHR);
4718 if (p != NULL && p[1] != ELF_VER_CHR)
4719 {
4720 /* Queue non-default versions so that .symver x, x@FOO
4721 aliases can be checked. */
4722 if (!nondeflt_vers)
4723 {
4724 amt = ((isymend - isym + 1)
4725 * sizeof (struct elf_link_hash_entry *));
4726 nondeflt_vers
4727 = (struct elf_link_hash_entry **) bfd_malloc (amt);
4728 if (!nondeflt_vers)
4729 goto error_free_vers;
4730 }
4731 nondeflt_vers[nondeflt_vers_cnt++] = h;
4732 }
4733 }
4734
4735 if (dynsym && h->dynindx == -1)
4736 {
4737 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4738 goto error_free_vers;
4739 if (h->u.weakdef != NULL
4740 && ! new_weakdef
4741 && h->u.weakdef->dynindx == -1)
4742 {
4743 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4744 goto error_free_vers;
4745 }
4746 }
4747 else if (h->dynindx != -1)
4748 /* If the symbol already has a dynamic index, but
4749 visibility says it should not be visible, turn it into
4750 a local symbol. */
4751 switch (ELF_ST_VISIBILITY (h->other))
4752 {
4753 case STV_INTERNAL:
4754 case STV_HIDDEN:
4755 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4756 dynsym = FALSE;
4757 break;
4758 }
4759
4760 /* Don't add DT_NEEDED for references from the dummy bfd nor
4761 for unmatched symbol. */
4762 if (!add_needed
4763 && matched
4764 && definition
4765 && ((dynsym
4766 && h->ref_regular_nonweak
4767 && (old_bfd == NULL
4768 || (old_bfd->flags & BFD_PLUGIN) == 0))
4769 || (h->ref_dynamic_nonweak
4770 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4771 && !on_needed_list (elf_dt_name (abfd),
4772 htab->needed, NULL))))
4773 {
4774 int ret;
4775 const char *soname = elf_dt_name (abfd);
4776
4777 info->callbacks->minfo ("%!", soname, old_bfd,
4778 h->root.root.string);
4779
4780 /* A symbol from a library loaded via DT_NEEDED of some
4781 other library is referenced by a regular object.
4782 Add a DT_NEEDED entry for it. Issue an error if
4783 --no-add-needed is used and the reference was not
4784 a weak one. */
4785 if (old_bfd != NULL
4786 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4787 {
4788 _bfd_error_handler
4789 /* xgettext:c-format */
4790 (_("%B: undefined reference to symbol '%s'"),
4791 old_bfd, name);
4792 bfd_set_error (bfd_error_missing_dso);
4793 goto error_free_vers;
4794 }
4795
4796 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4797 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
4798
4799 add_needed = TRUE;
4800 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4801 if (ret < 0)
4802 goto error_free_vers;
4803
4804 BFD_ASSERT (ret == 0);
4805 }
4806 }
4807 }
4808
4809 if (extversym != NULL)
4810 {
4811 free (extversym);
4812 extversym = NULL;
4813 }
4814
4815 if (isymbuf != NULL)
4816 {
4817 free (isymbuf);
4818 isymbuf = NULL;
4819 }
4820
4821 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4822 {
4823 unsigned int i;
4824
4825 /* Restore the symbol table. */
4826 old_ent = (char *) old_tab + tabsize;
4827 memset (elf_sym_hashes (abfd), 0,
4828 extsymcount * sizeof (struct elf_link_hash_entry *));
4829 htab->root.table.table = old_table;
4830 htab->root.table.size = old_size;
4831 htab->root.table.count = old_count;
4832 memcpy (htab->root.table.table, old_tab, tabsize);
4833 htab->root.undefs = old_undefs;
4834 htab->root.undefs_tail = old_undefs_tail;
4835 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
4836 free (old_strtab);
4837 old_strtab = NULL;
4838 for (i = 0; i < htab->root.table.size; i++)
4839 {
4840 struct bfd_hash_entry *p;
4841 struct elf_link_hash_entry *h;
4842 bfd_size_type size;
4843 unsigned int alignment_power;
4844
4845 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4846 {
4847 h = (struct elf_link_hash_entry *) p;
4848 if (h->root.type == bfd_link_hash_warning)
4849 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4850
4851 /* Preserve the maximum alignment and size for common
4852 symbols even if this dynamic lib isn't on DT_NEEDED
4853 since it can still be loaded at run time by another
4854 dynamic lib. */
4855 if (h->root.type == bfd_link_hash_common)
4856 {
4857 size = h->root.u.c.size;
4858 alignment_power = h->root.u.c.p->alignment_power;
4859 }
4860 else
4861 {
4862 size = 0;
4863 alignment_power = 0;
4864 }
4865 memcpy (p, old_ent, htab->root.table.entsize);
4866 old_ent = (char *) old_ent + htab->root.table.entsize;
4867 h = (struct elf_link_hash_entry *) p;
4868 if (h->root.type == bfd_link_hash_warning)
4869 {
4870 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4871 old_ent = (char *) old_ent + htab->root.table.entsize;
4872 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4873 }
4874 if (h->root.type == bfd_link_hash_common)
4875 {
4876 if (size > h->root.u.c.size)
4877 h->root.u.c.size = size;
4878 if (alignment_power > h->root.u.c.p->alignment_power)
4879 h->root.u.c.p->alignment_power = alignment_power;
4880 }
4881 }
4882 }
4883
4884 /* Make a special call to the linker "notice" function to
4885 tell it that symbols added for crefs may need to be removed. */
4886 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
4887 goto error_free_vers;
4888
4889 free (old_tab);
4890 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4891 alloc_mark);
4892 if (nondeflt_vers != NULL)
4893 free (nondeflt_vers);
4894 return TRUE;
4895 }
4896
4897 if (old_tab != NULL)
4898 {
4899 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
4900 goto error_free_vers;
4901 free (old_tab);
4902 old_tab = NULL;
4903 }
4904
4905 /* Now that all the symbols from this input file are created, if
4906 not performing a relocatable link, handle .symver foo, foo@BAR
4907 such that any relocs against foo become foo@BAR. */
4908 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4909 {
4910 size_t cnt, symidx;
4911
4912 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4913 {
4914 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4915 char *shortname, *p;
4916
4917 p = strchr (h->root.root.string, ELF_VER_CHR);
4918 if (p == NULL
4919 || (h->root.type != bfd_link_hash_defined
4920 && h->root.type != bfd_link_hash_defweak))
4921 continue;
4922
4923 amt = p - h->root.root.string;
4924 shortname = (char *) bfd_malloc (amt + 1);
4925 if (!shortname)
4926 goto error_free_vers;
4927 memcpy (shortname, h->root.root.string, amt);
4928 shortname[amt] = '\0';
4929
4930 hi = (struct elf_link_hash_entry *)
4931 bfd_link_hash_lookup (&htab->root, shortname,
4932 FALSE, FALSE, FALSE);
4933 if (hi != NULL
4934 && hi->root.type == h->root.type
4935 && hi->root.u.def.value == h->root.u.def.value
4936 && hi->root.u.def.section == h->root.u.def.section)
4937 {
4938 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4939 hi->root.type = bfd_link_hash_indirect;
4940 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4941 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4942 sym_hash = elf_sym_hashes (abfd);
4943 if (sym_hash)
4944 for (symidx = 0; symidx < extsymcount; ++symidx)
4945 if (sym_hash[symidx] == hi)
4946 {
4947 sym_hash[symidx] = h;
4948 break;
4949 }
4950 }
4951 free (shortname);
4952 }
4953 free (nondeflt_vers);
4954 nondeflt_vers = NULL;
4955 }
4956
4957 /* Now set the weakdefs field correctly for all the weak defined
4958 symbols we found. The only way to do this is to search all the
4959 symbols. Since we only need the information for non functions in
4960 dynamic objects, that's the only time we actually put anything on
4961 the list WEAKS. We need this information so that if a regular
4962 object refers to a symbol defined weakly in a dynamic object, the
4963 real symbol in the dynamic object is also put in the dynamic
4964 symbols; we also must arrange for both symbols to point to the
4965 same memory location. We could handle the general case of symbol
4966 aliasing, but a general symbol alias can only be generated in
4967 assembler code, handling it correctly would be very time
4968 consuming, and other ELF linkers don't handle general aliasing
4969 either. */
4970 if (weaks != NULL)
4971 {
4972 struct elf_link_hash_entry **hpp;
4973 struct elf_link_hash_entry **hppend;
4974 struct elf_link_hash_entry **sorted_sym_hash;
4975 struct elf_link_hash_entry *h;
4976 size_t sym_count;
4977
4978 /* Since we have to search the whole symbol list for each weak
4979 defined symbol, search time for N weak defined symbols will be
4980 O(N^2). Binary search will cut it down to O(NlogN). */
4981 amt = extsymcount;
4982 amt *= sizeof (struct elf_link_hash_entry *);
4983 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4984 if (sorted_sym_hash == NULL)
4985 goto error_return;
4986 sym_hash = sorted_sym_hash;
4987 hpp = elf_sym_hashes (abfd);
4988 hppend = hpp + extsymcount;
4989 sym_count = 0;
4990 for (; hpp < hppend; hpp++)
4991 {
4992 h = *hpp;
4993 if (h != NULL
4994 && h->root.type == bfd_link_hash_defined
4995 && !bed->is_function_type (h->type))
4996 {
4997 *sym_hash = h;
4998 sym_hash++;
4999 sym_count++;
5000 }
5001 }
5002
5003 qsort (sorted_sym_hash, sym_count,
5004 sizeof (struct elf_link_hash_entry *),
5005 elf_sort_symbol);
5006
5007 while (weaks != NULL)
5008 {
5009 struct elf_link_hash_entry *hlook;
5010 asection *slook;
5011 bfd_vma vlook;
5012 size_t i, j, idx = 0;
5013
5014 hlook = weaks;
5015 weaks = hlook->u.weakdef;
5016 hlook->u.weakdef = NULL;
5017
5018 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
5019 || hlook->root.type == bfd_link_hash_defweak
5020 || hlook->root.type == bfd_link_hash_common
5021 || hlook->root.type == bfd_link_hash_indirect);
5022 slook = hlook->root.u.def.section;
5023 vlook = hlook->root.u.def.value;
5024
5025 i = 0;
5026 j = sym_count;
5027 while (i != j)
5028 {
5029 bfd_signed_vma vdiff;
5030 idx = (i + j) / 2;
5031 h = sorted_sym_hash[idx];
5032 vdiff = vlook - h->root.u.def.value;
5033 if (vdiff < 0)
5034 j = idx;
5035 else if (vdiff > 0)
5036 i = idx + 1;
5037 else
5038 {
5039 int sdiff = slook->id - h->root.u.def.section->id;
5040 if (sdiff < 0)
5041 j = idx;
5042 else if (sdiff > 0)
5043 i = idx + 1;
5044 else
5045 break;
5046 }
5047 }
5048
5049 /* We didn't find a value/section match. */
5050 if (i == j)
5051 continue;
5052
5053 /* With multiple aliases, or when the weak symbol is already
5054 strongly defined, we have multiple matching symbols and
5055 the binary search above may land on any of them. Step
5056 one past the matching symbol(s). */
5057 while (++idx != j)
5058 {
5059 h = sorted_sym_hash[idx];
5060 if (h->root.u.def.section != slook
5061 || h->root.u.def.value != vlook)
5062 break;
5063 }
5064
5065 /* Now look back over the aliases. Since we sorted by size
5066 as well as value and section, we'll choose the one with
5067 the largest size. */
5068 while (idx-- != i)
5069 {
5070 h = sorted_sym_hash[idx];
5071
5072 /* Stop if value or section doesn't match. */
5073 if (h->root.u.def.section != slook
5074 || h->root.u.def.value != vlook)
5075 break;
5076 else if (h != hlook)
5077 {
5078 hlook->u.weakdef = h;
5079
5080 /* If the weak definition is in the list of dynamic
5081 symbols, make sure the real definition is put
5082 there as well. */
5083 if (hlook->dynindx != -1 && h->dynindx == -1)
5084 {
5085 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5086 {
5087 err_free_sym_hash:
5088 free (sorted_sym_hash);
5089 goto error_return;
5090 }
5091 }
5092
5093 /* If the real definition is in the list of dynamic
5094 symbols, make sure the weak definition is put
5095 there as well. If we don't do this, then the
5096 dynamic loader might not merge the entries for the
5097 real definition and the weak definition. */
5098 if (h->dynindx != -1 && hlook->dynindx == -1)
5099 {
5100 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5101 goto err_free_sym_hash;
5102 }
5103 break;
5104 }
5105 }
5106 }
5107
5108 free (sorted_sym_hash);
5109 }
5110
5111 if (bed->check_directives
5112 && !(*bed->check_directives) (abfd, info))
5113 return FALSE;
5114
5115 if (!info->check_relocs_after_open_input
5116 && !_bfd_elf_link_check_relocs (abfd, info))
5117 return FALSE;
5118
5119 /* If this is a non-traditional link, try to optimize the handling
5120 of the .stab/.stabstr sections. */
5121 if (! dynamic
5122 && ! info->traditional_format
5123 && is_elf_hash_table (htab)
5124 && (info->strip != strip_all && info->strip != strip_debugger))
5125 {
5126 asection *stabstr;
5127
5128 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5129 if (stabstr != NULL)
5130 {
5131 bfd_size_type string_offset = 0;
5132 asection *stab;
5133
5134 for (stab = abfd->sections; stab; stab = stab->next)
5135 if (CONST_STRNEQ (stab->name, ".stab")
5136 && (!stab->name[5] ||
5137 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5138 && (stab->flags & SEC_MERGE) == 0
5139 && !bfd_is_abs_section (stab->output_section))
5140 {
5141 struct bfd_elf_section_data *secdata;
5142
5143 secdata = elf_section_data (stab);
5144 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5145 stabstr, &secdata->sec_info,
5146 &string_offset))
5147 goto error_return;
5148 if (secdata->sec_info)
5149 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5150 }
5151 }
5152 }
5153
5154 if (is_elf_hash_table (htab) && add_needed)
5155 {
5156 /* Add this bfd to the loaded list. */
5157 struct elf_link_loaded_list *n;
5158
5159 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5160 if (n == NULL)
5161 goto error_return;
5162 n->abfd = abfd;
5163 n->next = htab->loaded;
5164 htab->loaded = n;
5165 }
5166
5167 return TRUE;
5168
5169 error_free_vers:
5170 if (old_tab != NULL)
5171 free (old_tab);
5172 if (old_strtab != NULL)
5173 free (old_strtab);
5174 if (nondeflt_vers != NULL)
5175 free (nondeflt_vers);
5176 if (extversym != NULL)
5177 free (extversym);
5178 error_free_sym:
5179 if (isymbuf != NULL)
5180 free (isymbuf);
5181 error_return:
5182 return FALSE;
5183 }
5184
5185 /* Return the linker hash table entry of a symbol that might be
5186 satisfied by an archive symbol. Return -1 on error. */
5187
5188 struct elf_link_hash_entry *
5189 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5190 struct bfd_link_info *info,
5191 const char *name)
5192 {
5193 struct elf_link_hash_entry *h;
5194 char *p, *copy;
5195 size_t len, first;
5196
5197 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5198 if (h != NULL)
5199 return h;
5200
5201 /* If this is a default version (the name contains @@), look up the
5202 symbol again with only one `@' as well as without the version.
5203 The effect is that references to the symbol with and without the
5204 version will be matched by the default symbol in the archive. */
5205
5206 p = strchr (name, ELF_VER_CHR);
5207 if (p == NULL || p[1] != ELF_VER_CHR)
5208 return h;
5209
5210 /* First check with only one `@'. */
5211 len = strlen (name);
5212 copy = (char *) bfd_alloc (abfd, len);
5213 if (copy == NULL)
5214 return (struct elf_link_hash_entry *) 0 - 1;
5215
5216 first = p - name + 1;
5217 memcpy (copy, name, first);
5218 memcpy (copy + first, name + first + 1, len - first);
5219
5220 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5221 if (h == NULL)
5222 {
5223 /* We also need to check references to the symbol without the
5224 version. */
5225 copy[first - 1] = '\0';
5226 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5227 FALSE, FALSE, TRUE);
5228 }
5229
5230 bfd_release (abfd, copy);
5231 return h;
5232 }
5233
5234 /* Add symbols from an ELF archive file to the linker hash table. We
5235 don't use _bfd_generic_link_add_archive_symbols because we need to
5236 handle versioned symbols.
5237
5238 Fortunately, ELF archive handling is simpler than that done by
5239 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5240 oddities. In ELF, if we find a symbol in the archive map, and the
5241 symbol is currently undefined, we know that we must pull in that
5242 object file.
5243
5244 Unfortunately, we do have to make multiple passes over the symbol
5245 table until nothing further is resolved. */
5246
5247 static bfd_boolean
5248 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5249 {
5250 symindex c;
5251 unsigned char *included = NULL;
5252 carsym *symdefs;
5253 bfd_boolean loop;
5254 bfd_size_type amt;
5255 const struct elf_backend_data *bed;
5256 struct elf_link_hash_entry * (*archive_symbol_lookup)
5257 (bfd *, struct bfd_link_info *, const char *);
5258
5259 if (! bfd_has_map (abfd))
5260 {
5261 /* An empty archive is a special case. */
5262 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5263 return TRUE;
5264 bfd_set_error (bfd_error_no_armap);
5265 return FALSE;
5266 }
5267
5268 /* Keep track of all symbols we know to be already defined, and all
5269 files we know to be already included. This is to speed up the
5270 second and subsequent passes. */
5271 c = bfd_ardata (abfd)->symdef_count;
5272 if (c == 0)
5273 return TRUE;
5274 amt = c;
5275 amt *= sizeof (*included);
5276 included = (unsigned char *) bfd_zmalloc (amt);
5277 if (included == NULL)
5278 return FALSE;
5279
5280 symdefs = bfd_ardata (abfd)->symdefs;
5281 bed = get_elf_backend_data (abfd);
5282 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5283
5284 do
5285 {
5286 file_ptr last;
5287 symindex i;
5288 carsym *symdef;
5289 carsym *symdefend;
5290
5291 loop = FALSE;
5292 last = -1;
5293
5294 symdef = symdefs;
5295 symdefend = symdef + c;
5296 for (i = 0; symdef < symdefend; symdef++, i++)
5297 {
5298 struct elf_link_hash_entry *h;
5299 bfd *element;
5300 struct bfd_link_hash_entry *undefs_tail;
5301 symindex mark;
5302
5303 if (included[i])
5304 continue;
5305 if (symdef->file_offset == last)
5306 {
5307 included[i] = TRUE;
5308 continue;
5309 }
5310
5311 h = archive_symbol_lookup (abfd, info, symdef->name);
5312 if (h == (struct elf_link_hash_entry *) 0 - 1)
5313 goto error_return;
5314
5315 if (h == NULL)
5316 continue;
5317
5318 if (h->root.type == bfd_link_hash_common)
5319 {
5320 /* We currently have a common symbol. The archive map contains
5321 a reference to this symbol, so we may want to include it. We
5322 only want to include it however, if this archive element
5323 contains a definition of the symbol, not just another common
5324 declaration of it.
5325
5326 Unfortunately some archivers (including GNU ar) will put
5327 declarations of common symbols into their archive maps, as
5328 well as real definitions, so we cannot just go by the archive
5329 map alone. Instead we must read in the element's symbol
5330 table and check that to see what kind of symbol definition
5331 this is. */
5332 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5333 continue;
5334 }
5335 else if (h->root.type != bfd_link_hash_undefined)
5336 {
5337 if (h->root.type != bfd_link_hash_undefweak)
5338 /* Symbol must be defined. Don't check it again. */
5339 included[i] = TRUE;
5340 continue;
5341 }
5342
5343 /* We need to include this archive member. */
5344 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5345 if (element == NULL)
5346 goto error_return;
5347
5348 if (! bfd_check_format (element, bfd_object))
5349 goto error_return;
5350
5351 undefs_tail = info->hash->undefs_tail;
5352
5353 if (!(*info->callbacks
5354 ->add_archive_element) (info, element, symdef->name, &element))
5355 continue;
5356 if (!bfd_link_add_symbols (element, info))
5357 goto error_return;
5358
5359 /* If there are any new undefined symbols, we need to make
5360 another pass through the archive in order to see whether
5361 they can be defined. FIXME: This isn't perfect, because
5362 common symbols wind up on undefs_tail and because an
5363 undefined symbol which is defined later on in this pass
5364 does not require another pass. This isn't a bug, but it
5365 does make the code less efficient than it could be. */
5366 if (undefs_tail != info->hash->undefs_tail)
5367 loop = TRUE;
5368
5369 /* Look backward to mark all symbols from this object file
5370 which we have already seen in this pass. */
5371 mark = i;
5372 do
5373 {
5374 included[mark] = TRUE;
5375 if (mark == 0)
5376 break;
5377 --mark;
5378 }
5379 while (symdefs[mark].file_offset == symdef->file_offset);
5380
5381 /* We mark subsequent symbols from this object file as we go
5382 on through the loop. */
5383 last = symdef->file_offset;
5384 }
5385 }
5386 while (loop);
5387
5388 free (included);
5389
5390 return TRUE;
5391
5392 error_return:
5393 if (included != NULL)
5394 free (included);
5395 return FALSE;
5396 }
5397
5398 /* Given an ELF BFD, add symbols to the global hash table as
5399 appropriate. */
5400
5401 bfd_boolean
5402 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5403 {
5404 switch (bfd_get_format (abfd))
5405 {
5406 case bfd_object:
5407 return elf_link_add_object_symbols (abfd, info);
5408 case bfd_archive:
5409 return elf_link_add_archive_symbols (abfd, info);
5410 default:
5411 bfd_set_error (bfd_error_wrong_format);
5412 return FALSE;
5413 }
5414 }
5415 \f
5416 struct hash_codes_info
5417 {
5418 unsigned long *hashcodes;
5419 bfd_boolean error;
5420 };
5421
5422 /* This function will be called though elf_link_hash_traverse to store
5423 all hash value of the exported symbols in an array. */
5424
5425 static bfd_boolean
5426 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5427 {
5428 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5429 const char *name;
5430 unsigned long ha;
5431 char *alc = NULL;
5432
5433 /* Ignore indirect symbols. These are added by the versioning code. */
5434 if (h->dynindx == -1)
5435 return TRUE;
5436
5437 name = h->root.root.string;
5438 if (h->versioned >= versioned)
5439 {
5440 char *p = strchr (name, ELF_VER_CHR);
5441 if (p != NULL)
5442 {
5443 alc = (char *) bfd_malloc (p - name + 1);
5444 if (alc == NULL)
5445 {
5446 inf->error = TRUE;
5447 return FALSE;
5448 }
5449 memcpy (alc, name, p - name);
5450 alc[p - name] = '\0';
5451 name = alc;
5452 }
5453 }
5454
5455 /* Compute the hash value. */
5456 ha = bfd_elf_hash (name);
5457
5458 /* Store the found hash value in the array given as the argument. */
5459 *(inf->hashcodes)++ = ha;
5460
5461 /* And store it in the struct so that we can put it in the hash table
5462 later. */
5463 h->u.elf_hash_value = ha;
5464
5465 if (alc != NULL)
5466 free (alc);
5467
5468 return TRUE;
5469 }
5470
5471 struct collect_gnu_hash_codes
5472 {
5473 bfd *output_bfd;
5474 const struct elf_backend_data *bed;
5475 unsigned long int nsyms;
5476 unsigned long int maskbits;
5477 unsigned long int *hashcodes;
5478 unsigned long int *hashval;
5479 unsigned long int *indx;
5480 unsigned long int *counts;
5481 bfd_vma *bitmask;
5482 bfd_byte *contents;
5483 long int min_dynindx;
5484 unsigned long int bucketcount;
5485 unsigned long int symindx;
5486 long int local_indx;
5487 long int shift1, shift2;
5488 unsigned long int mask;
5489 bfd_boolean error;
5490 };
5491
5492 /* This function will be called though elf_link_hash_traverse to store
5493 all hash value of the exported symbols in an array. */
5494
5495 static bfd_boolean
5496 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5497 {
5498 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5499 const char *name;
5500 unsigned long ha;
5501 char *alc = NULL;
5502
5503 /* Ignore indirect symbols. These are added by the versioning code. */
5504 if (h->dynindx == -1)
5505 return TRUE;
5506
5507 /* Ignore also local symbols and undefined symbols. */
5508 if (! (*s->bed->elf_hash_symbol) (h))
5509 return TRUE;
5510
5511 name = h->root.root.string;
5512 if (h->versioned >= versioned)
5513 {
5514 char *p = strchr (name, ELF_VER_CHR);
5515 if (p != NULL)
5516 {
5517 alc = (char *) bfd_malloc (p - name + 1);
5518 if (alc == NULL)
5519 {
5520 s->error = TRUE;
5521 return FALSE;
5522 }
5523 memcpy (alc, name, p - name);
5524 alc[p - name] = '\0';
5525 name = alc;
5526 }
5527 }
5528
5529 /* Compute the hash value. */
5530 ha = bfd_elf_gnu_hash (name);
5531
5532 /* Store the found hash value in the array for compute_bucket_count,
5533 and also for .dynsym reordering purposes. */
5534 s->hashcodes[s->nsyms] = ha;
5535 s->hashval[h->dynindx] = ha;
5536 ++s->nsyms;
5537 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5538 s->min_dynindx = h->dynindx;
5539
5540 if (alc != NULL)
5541 free (alc);
5542
5543 return TRUE;
5544 }
5545
5546 /* This function will be called though elf_link_hash_traverse to do
5547 final dynaminc symbol renumbering. */
5548
5549 static bfd_boolean
5550 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5551 {
5552 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5553 unsigned long int bucket;
5554 unsigned long int val;
5555
5556 /* Ignore indirect symbols. */
5557 if (h->dynindx == -1)
5558 return TRUE;
5559
5560 /* Ignore also local symbols and undefined symbols. */
5561 if (! (*s->bed->elf_hash_symbol) (h))
5562 {
5563 if (h->dynindx >= s->min_dynindx)
5564 h->dynindx = s->local_indx++;
5565 return TRUE;
5566 }
5567
5568 bucket = s->hashval[h->dynindx] % s->bucketcount;
5569 val = (s->hashval[h->dynindx] >> s->shift1)
5570 & ((s->maskbits >> s->shift1) - 1);
5571 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5572 s->bitmask[val]
5573 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5574 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5575 if (s->counts[bucket] == 1)
5576 /* Last element terminates the chain. */
5577 val |= 1;
5578 bfd_put_32 (s->output_bfd, val,
5579 s->contents + (s->indx[bucket] - s->symindx) * 4);
5580 --s->counts[bucket];
5581 h->dynindx = s->indx[bucket]++;
5582 return TRUE;
5583 }
5584
5585 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5586
5587 bfd_boolean
5588 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5589 {
5590 return !(h->forced_local
5591 || h->root.type == bfd_link_hash_undefined
5592 || h->root.type == bfd_link_hash_undefweak
5593 || ((h->root.type == bfd_link_hash_defined
5594 || h->root.type == bfd_link_hash_defweak)
5595 && h->root.u.def.section->output_section == NULL));
5596 }
5597
5598 /* Array used to determine the number of hash table buckets to use
5599 based on the number of symbols there are. If there are fewer than
5600 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5601 fewer than 37 we use 17 buckets, and so forth. We never use more
5602 than 32771 buckets. */
5603
5604 static const size_t elf_buckets[] =
5605 {
5606 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5607 16411, 32771, 0
5608 };
5609
5610 /* Compute bucket count for hashing table. We do not use a static set
5611 of possible tables sizes anymore. Instead we determine for all
5612 possible reasonable sizes of the table the outcome (i.e., the
5613 number of collisions etc) and choose the best solution. The
5614 weighting functions are not too simple to allow the table to grow
5615 without bounds. Instead one of the weighting factors is the size.
5616 Therefore the result is always a good payoff between few collisions
5617 (= short chain lengths) and table size. */
5618 static size_t
5619 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5620 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5621 unsigned long int nsyms,
5622 int gnu_hash)
5623 {
5624 size_t best_size = 0;
5625 unsigned long int i;
5626
5627 /* We have a problem here. The following code to optimize the table
5628 size requires an integer type with more the 32 bits. If
5629 BFD_HOST_U_64_BIT is set we know about such a type. */
5630 #ifdef BFD_HOST_U_64_BIT
5631 if (info->optimize)
5632 {
5633 size_t minsize;
5634 size_t maxsize;
5635 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5636 bfd *dynobj = elf_hash_table (info)->dynobj;
5637 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5638 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5639 unsigned long int *counts;
5640 bfd_size_type amt;
5641 unsigned int no_improvement_count = 0;
5642
5643 /* Possible optimization parameters: if we have NSYMS symbols we say
5644 that the hashing table must at least have NSYMS/4 and at most
5645 2*NSYMS buckets. */
5646 minsize = nsyms / 4;
5647 if (minsize == 0)
5648 minsize = 1;
5649 best_size = maxsize = nsyms * 2;
5650 if (gnu_hash)
5651 {
5652 if (minsize < 2)
5653 minsize = 2;
5654 if ((best_size & 31) == 0)
5655 ++best_size;
5656 }
5657
5658 /* Create array where we count the collisions in. We must use bfd_malloc
5659 since the size could be large. */
5660 amt = maxsize;
5661 amt *= sizeof (unsigned long int);
5662 counts = (unsigned long int *) bfd_malloc (amt);
5663 if (counts == NULL)
5664 return 0;
5665
5666 /* Compute the "optimal" size for the hash table. The criteria is a
5667 minimal chain length. The minor criteria is (of course) the size
5668 of the table. */
5669 for (i = minsize; i < maxsize; ++i)
5670 {
5671 /* Walk through the array of hashcodes and count the collisions. */
5672 BFD_HOST_U_64_BIT max;
5673 unsigned long int j;
5674 unsigned long int fact;
5675
5676 if (gnu_hash && (i & 31) == 0)
5677 continue;
5678
5679 memset (counts, '\0', i * sizeof (unsigned long int));
5680
5681 /* Determine how often each hash bucket is used. */
5682 for (j = 0; j < nsyms; ++j)
5683 ++counts[hashcodes[j] % i];
5684
5685 /* For the weight function we need some information about the
5686 pagesize on the target. This is information need not be 100%
5687 accurate. Since this information is not available (so far) we
5688 define it here to a reasonable default value. If it is crucial
5689 to have a better value some day simply define this value. */
5690 # ifndef BFD_TARGET_PAGESIZE
5691 # define BFD_TARGET_PAGESIZE (4096)
5692 # endif
5693
5694 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5695 and the chains. */
5696 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5697
5698 # if 1
5699 /* Variant 1: optimize for short chains. We add the squares
5700 of all the chain lengths (which favors many small chain
5701 over a few long chains). */
5702 for (j = 0; j < i; ++j)
5703 max += counts[j] * counts[j];
5704
5705 /* This adds penalties for the overall size of the table. */
5706 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5707 max *= fact * fact;
5708 # else
5709 /* Variant 2: Optimize a lot more for small table. Here we
5710 also add squares of the size but we also add penalties for
5711 empty slots (the +1 term). */
5712 for (j = 0; j < i; ++j)
5713 max += (1 + counts[j]) * (1 + counts[j]);
5714
5715 /* The overall size of the table is considered, but not as
5716 strong as in variant 1, where it is squared. */
5717 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5718 max *= fact;
5719 # endif
5720
5721 /* Compare with current best results. */
5722 if (max < best_chlen)
5723 {
5724 best_chlen = max;
5725 best_size = i;
5726 no_improvement_count = 0;
5727 }
5728 /* PR 11843: Avoid futile long searches for the best bucket size
5729 when there are a large number of symbols. */
5730 else if (++no_improvement_count == 100)
5731 break;
5732 }
5733
5734 free (counts);
5735 }
5736 else
5737 #endif /* defined (BFD_HOST_U_64_BIT) */
5738 {
5739 /* This is the fallback solution if no 64bit type is available or if we
5740 are not supposed to spend much time on optimizations. We select the
5741 bucket count using a fixed set of numbers. */
5742 for (i = 0; elf_buckets[i] != 0; i++)
5743 {
5744 best_size = elf_buckets[i];
5745 if (nsyms < elf_buckets[i + 1])
5746 break;
5747 }
5748 if (gnu_hash && best_size < 2)
5749 best_size = 2;
5750 }
5751
5752 return best_size;
5753 }
5754
5755 /* Size any SHT_GROUP section for ld -r. */
5756
5757 bfd_boolean
5758 _bfd_elf_size_group_sections (struct bfd_link_info *info)
5759 {
5760 bfd *ibfd;
5761
5762 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
5763 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5764 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5765 return FALSE;
5766 return TRUE;
5767 }
5768
5769 /* Set a default stack segment size. The value in INFO wins. If it
5770 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5771 undefined it is initialized. */
5772
5773 bfd_boolean
5774 bfd_elf_stack_segment_size (bfd *output_bfd,
5775 struct bfd_link_info *info,
5776 const char *legacy_symbol,
5777 bfd_vma default_size)
5778 {
5779 struct elf_link_hash_entry *h = NULL;
5780
5781 /* Look for legacy symbol. */
5782 if (legacy_symbol)
5783 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5784 FALSE, FALSE, FALSE);
5785 if (h && (h->root.type == bfd_link_hash_defined
5786 || h->root.type == bfd_link_hash_defweak)
5787 && h->def_regular
5788 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5789 {
5790 /* The symbol has no type if specified on the command line. */
5791 h->type = STT_OBJECT;
5792 if (info->stacksize)
5793 /* xgettext:c-format */
5794 _bfd_error_handler (_("%B: stack size specified and %s set"),
5795 output_bfd, legacy_symbol);
5796 else if (h->root.u.def.section != bfd_abs_section_ptr)
5797 /* xgettext:c-format */
5798 _bfd_error_handler (_("%B: %s not absolute"),
5799 output_bfd, legacy_symbol);
5800 else
5801 info->stacksize = h->root.u.def.value;
5802 }
5803
5804 if (!info->stacksize)
5805 /* If the user didn't set a size, or explicitly inhibit the
5806 size, set it now. */
5807 info->stacksize = default_size;
5808
5809 /* Provide the legacy symbol, if it is referenced. */
5810 if (h && (h->root.type == bfd_link_hash_undefined
5811 || h->root.type == bfd_link_hash_undefweak))
5812 {
5813 struct bfd_link_hash_entry *bh = NULL;
5814
5815 if (!(_bfd_generic_link_add_one_symbol
5816 (info, output_bfd, legacy_symbol,
5817 BSF_GLOBAL, bfd_abs_section_ptr,
5818 info->stacksize >= 0 ? info->stacksize : 0,
5819 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5820 return FALSE;
5821
5822 h = (struct elf_link_hash_entry *) bh;
5823 h->def_regular = 1;
5824 h->type = STT_OBJECT;
5825 }
5826
5827 return TRUE;
5828 }
5829
5830 /* Set up the sizes and contents of the ELF dynamic sections. This is
5831 called by the ELF linker emulation before_allocation routine. We
5832 must set the sizes of the sections before the linker sets the
5833 addresses of the various sections. */
5834
5835 bfd_boolean
5836 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5837 const char *soname,
5838 const char *rpath,
5839 const char *filter_shlib,
5840 const char *audit,
5841 const char *depaudit,
5842 const char * const *auxiliary_filters,
5843 struct bfd_link_info *info,
5844 asection **sinterpptr)
5845 {
5846 size_t soname_indx;
5847 bfd *dynobj;
5848 const struct elf_backend_data *bed;
5849 struct elf_info_failed asvinfo;
5850
5851 *sinterpptr = NULL;
5852
5853 soname_indx = (size_t) -1;
5854
5855 if (!is_elf_hash_table (info->hash))
5856 return TRUE;
5857
5858 bed = get_elf_backend_data (output_bfd);
5859
5860 /* Any syms created from now on start with -1 in
5861 got.refcount/offset and plt.refcount/offset. */
5862 elf_hash_table (info)->init_got_refcount
5863 = elf_hash_table (info)->init_got_offset;
5864 elf_hash_table (info)->init_plt_refcount
5865 = elf_hash_table (info)->init_plt_offset;
5866
5867 if (bfd_link_relocatable (info)
5868 && !_bfd_elf_size_group_sections (info))
5869 return FALSE;
5870
5871 /* The backend may have to create some sections regardless of whether
5872 we're dynamic or not. */
5873 if (bed->elf_backend_always_size_sections
5874 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5875 return FALSE;
5876
5877 /* Determine any GNU_STACK segment requirements, after the backend
5878 has had a chance to set a default segment size. */
5879 if (info->execstack)
5880 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
5881 else if (info->noexecstack)
5882 elf_stack_flags (output_bfd) = PF_R | PF_W;
5883 else
5884 {
5885 bfd *inputobj;
5886 asection *notesec = NULL;
5887 int exec = 0;
5888
5889 for (inputobj = info->input_bfds;
5890 inputobj;
5891 inputobj = inputobj->link.next)
5892 {
5893 asection *s;
5894
5895 if (inputobj->flags
5896 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5897 continue;
5898 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5899 if (s)
5900 {
5901 if (s->flags & SEC_CODE)
5902 exec = PF_X;
5903 notesec = s;
5904 }
5905 else if (bed->default_execstack)
5906 exec = PF_X;
5907 }
5908 if (notesec || info->stacksize > 0)
5909 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
5910 if (notesec && exec && bfd_link_relocatable (info)
5911 && notesec->output_section != bfd_abs_section_ptr)
5912 notesec->output_section->flags |= SEC_CODE;
5913 }
5914
5915 dynobj = elf_hash_table (info)->dynobj;
5916
5917 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5918 {
5919 struct elf_info_failed eif;
5920 struct elf_link_hash_entry *h;
5921 asection *dynstr;
5922 struct bfd_elf_version_tree *t;
5923 struct bfd_elf_version_expr *d;
5924 asection *s;
5925 bfd_boolean all_defined;
5926
5927 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
5928 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
5929
5930 if (soname != NULL)
5931 {
5932 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5933 soname, TRUE);
5934 if (soname_indx == (size_t) -1
5935 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5936 return FALSE;
5937 }
5938
5939 if (info->symbolic)
5940 {
5941 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5942 return FALSE;
5943 info->flags |= DF_SYMBOLIC;
5944 }
5945
5946 if (rpath != NULL)
5947 {
5948 size_t indx;
5949 bfd_vma tag;
5950
5951 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5952 TRUE);
5953 if (indx == (size_t) -1)
5954 return FALSE;
5955
5956 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
5957 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
5958 return FALSE;
5959 }
5960
5961 if (filter_shlib != NULL)
5962 {
5963 size_t indx;
5964
5965 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5966 filter_shlib, TRUE);
5967 if (indx == (size_t) -1
5968 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5969 return FALSE;
5970 }
5971
5972 if (auxiliary_filters != NULL)
5973 {
5974 const char * const *p;
5975
5976 for (p = auxiliary_filters; *p != NULL; p++)
5977 {
5978 size_t indx;
5979
5980 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5981 *p, TRUE);
5982 if (indx == (size_t) -1
5983 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5984 return FALSE;
5985 }
5986 }
5987
5988 if (audit != NULL)
5989 {
5990 size_t indx;
5991
5992 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5993 TRUE);
5994 if (indx == (size_t) -1
5995 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5996 return FALSE;
5997 }
5998
5999 if (depaudit != NULL)
6000 {
6001 size_t indx;
6002
6003 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6004 TRUE);
6005 if (indx == (size_t) -1
6006 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6007 return FALSE;
6008 }
6009
6010 eif.info = info;
6011 eif.failed = FALSE;
6012
6013 /* If we are supposed to export all symbols into the dynamic symbol
6014 table (this is not the normal case), then do so. */
6015 if (info->export_dynamic
6016 || (bfd_link_executable (info) && info->dynamic))
6017 {
6018 elf_link_hash_traverse (elf_hash_table (info),
6019 _bfd_elf_export_symbol,
6020 &eif);
6021 if (eif.failed)
6022 return FALSE;
6023 }
6024
6025 /* Make all global versions with definition. */
6026 for (t = info->version_info; t != NULL; t = t->next)
6027 for (d = t->globals.list; d != NULL; d = d->next)
6028 if (!d->symver && d->literal)
6029 {
6030 const char *verstr, *name;
6031 size_t namelen, verlen, newlen;
6032 char *newname, *p, leading_char;
6033 struct elf_link_hash_entry *newh;
6034
6035 leading_char = bfd_get_symbol_leading_char (output_bfd);
6036 name = d->pattern;
6037 namelen = strlen (name) + (leading_char != '\0');
6038 verstr = t->name;
6039 verlen = strlen (verstr);
6040 newlen = namelen + verlen + 3;
6041
6042 newname = (char *) bfd_malloc (newlen);
6043 if (newname == NULL)
6044 return FALSE;
6045 newname[0] = leading_char;
6046 memcpy (newname + (leading_char != '\0'), name, namelen);
6047
6048 /* Check the hidden versioned definition. */
6049 p = newname + namelen;
6050 *p++ = ELF_VER_CHR;
6051 memcpy (p, verstr, verlen + 1);
6052 newh = elf_link_hash_lookup (elf_hash_table (info),
6053 newname, FALSE, FALSE,
6054 FALSE);
6055 if (newh == NULL
6056 || (newh->root.type != bfd_link_hash_defined
6057 && newh->root.type != bfd_link_hash_defweak))
6058 {
6059 /* Check the default versioned definition. */
6060 *p++ = ELF_VER_CHR;
6061 memcpy (p, verstr, verlen + 1);
6062 newh = elf_link_hash_lookup (elf_hash_table (info),
6063 newname, FALSE, FALSE,
6064 FALSE);
6065 }
6066 free (newname);
6067
6068 /* Mark this version if there is a definition and it is
6069 not defined in a shared object. */
6070 if (newh != NULL
6071 && !newh->def_dynamic
6072 && (newh->root.type == bfd_link_hash_defined
6073 || newh->root.type == bfd_link_hash_defweak))
6074 d->symver = 1;
6075 }
6076
6077 /* Attach all the symbols to their version information. */
6078 asvinfo.info = info;
6079 asvinfo.failed = FALSE;
6080
6081 elf_link_hash_traverse (elf_hash_table (info),
6082 _bfd_elf_link_assign_sym_version,
6083 &asvinfo);
6084 if (asvinfo.failed)
6085 return FALSE;
6086
6087 if (!info->allow_undefined_version)
6088 {
6089 /* Check if all global versions have a definition. */
6090 all_defined = TRUE;
6091 for (t = info->version_info; t != NULL; t = t->next)
6092 for (d = t->globals.list; d != NULL; d = d->next)
6093 if (d->literal && !d->symver && !d->script)
6094 {
6095 _bfd_error_handler
6096 (_("%s: undefined version: %s"),
6097 d->pattern, t->name);
6098 all_defined = FALSE;
6099 }
6100
6101 if (!all_defined)
6102 {
6103 bfd_set_error (bfd_error_bad_value);
6104 return FALSE;
6105 }
6106 }
6107
6108 /* Find all symbols which were defined in a dynamic object and make
6109 the backend pick a reasonable value for them. */
6110 elf_link_hash_traverse (elf_hash_table (info),
6111 _bfd_elf_adjust_dynamic_symbol,
6112 &eif);
6113 if (eif.failed)
6114 return FALSE;
6115
6116 /* Add some entries to the .dynamic section. We fill in some of the
6117 values later, in bfd_elf_final_link, but we must add the entries
6118 now so that we know the final size of the .dynamic section. */
6119
6120 /* If there are initialization and/or finalization functions to
6121 call then add the corresponding DT_INIT/DT_FINI entries. */
6122 h = (info->init_function
6123 ? elf_link_hash_lookup (elf_hash_table (info),
6124 info->init_function, FALSE,
6125 FALSE, FALSE)
6126 : NULL);
6127 if (h != NULL
6128 && (h->ref_regular
6129 || h->def_regular))
6130 {
6131 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6132 return FALSE;
6133 }
6134 h = (info->fini_function
6135 ? elf_link_hash_lookup (elf_hash_table (info),
6136 info->fini_function, FALSE,
6137 FALSE, FALSE)
6138 : NULL);
6139 if (h != NULL
6140 && (h->ref_regular
6141 || h->def_regular))
6142 {
6143 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6144 return FALSE;
6145 }
6146
6147 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6148 if (s != NULL && s->linker_has_input)
6149 {
6150 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6151 if (! bfd_link_executable (info))
6152 {
6153 bfd *sub;
6154 asection *o;
6155
6156 for (sub = info->input_bfds; sub != NULL;
6157 sub = sub->link.next)
6158 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
6159 for (o = sub->sections; o != NULL; o = o->next)
6160 if (elf_section_data (o)->this_hdr.sh_type
6161 == SHT_PREINIT_ARRAY)
6162 {
6163 _bfd_error_handler
6164 (_("%B: .preinit_array section is not allowed in DSO"),
6165 sub);
6166 break;
6167 }
6168
6169 bfd_set_error (bfd_error_nonrepresentable_section);
6170 return FALSE;
6171 }
6172
6173 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6174 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6175 return FALSE;
6176 }
6177 s = bfd_get_section_by_name (output_bfd, ".init_array");
6178 if (s != NULL && s->linker_has_input)
6179 {
6180 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6181 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6182 return FALSE;
6183 }
6184 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6185 if (s != NULL && s->linker_has_input)
6186 {
6187 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6188 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6189 return FALSE;
6190 }
6191
6192 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6193 /* If .dynstr is excluded from the link, we don't want any of
6194 these tags. Strictly, we should be checking each section
6195 individually; This quick check covers for the case where
6196 someone does a /DISCARD/ : { *(*) }. */
6197 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6198 {
6199 bfd_size_type strsize;
6200
6201 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6202 if ((info->emit_hash
6203 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6204 || (info->emit_gnu_hash
6205 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6206 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6207 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6208 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6209 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6210 bed->s->sizeof_sym))
6211 return FALSE;
6212 }
6213 }
6214
6215 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6216 return FALSE;
6217
6218 /* The backend must work out the sizes of all the other dynamic
6219 sections. */
6220 if (dynobj != NULL
6221 && bed->elf_backend_size_dynamic_sections != NULL
6222 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6223 return FALSE;
6224
6225 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6226 {
6227 unsigned long section_sym_count;
6228 struct bfd_elf_version_tree *verdefs;
6229 asection *s;
6230
6231 /* Set up the version definition section. */
6232 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6233 BFD_ASSERT (s != NULL);
6234
6235 /* We may have created additional version definitions if we are
6236 just linking a regular application. */
6237 verdefs = info->version_info;
6238
6239 /* Skip anonymous version tag. */
6240 if (verdefs != NULL && verdefs->vernum == 0)
6241 verdefs = verdefs->next;
6242
6243 if (verdefs == NULL && !info->create_default_symver)
6244 s->flags |= SEC_EXCLUDE;
6245 else
6246 {
6247 unsigned int cdefs;
6248 bfd_size_type size;
6249 struct bfd_elf_version_tree *t;
6250 bfd_byte *p;
6251 Elf_Internal_Verdef def;
6252 Elf_Internal_Verdaux defaux;
6253 struct bfd_link_hash_entry *bh;
6254 struct elf_link_hash_entry *h;
6255 const char *name;
6256
6257 cdefs = 0;
6258 size = 0;
6259
6260 /* Make space for the base version. */
6261 size += sizeof (Elf_External_Verdef);
6262 size += sizeof (Elf_External_Verdaux);
6263 ++cdefs;
6264
6265 /* Make space for the default version. */
6266 if (info->create_default_symver)
6267 {
6268 size += sizeof (Elf_External_Verdef);
6269 ++cdefs;
6270 }
6271
6272 for (t = verdefs; t != NULL; t = t->next)
6273 {
6274 struct bfd_elf_version_deps *n;
6275
6276 /* Don't emit base version twice. */
6277 if (t->vernum == 0)
6278 continue;
6279
6280 size += sizeof (Elf_External_Verdef);
6281 size += sizeof (Elf_External_Verdaux);
6282 ++cdefs;
6283
6284 for (n = t->deps; n != NULL; n = n->next)
6285 size += sizeof (Elf_External_Verdaux);
6286 }
6287
6288 s->size = size;
6289 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6290 if (s->contents == NULL && s->size != 0)
6291 return FALSE;
6292
6293 /* Fill in the version definition section. */
6294
6295 p = s->contents;
6296
6297 def.vd_version = VER_DEF_CURRENT;
6298 def.vd_flags = VER_FLG_BASE;
6299 def.vd_ndx = 1;
6300 def.vd_cnt = 1;
6301 if (info->create_default_symver)
6302 {
6303 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6304 def.vd_next = sizeof (Elf_External_Verdef);
6305 }
6306 else
6307 {
6308 def.vd_aux = sizeof (Elf_External_Verdef);
6309 def.vd_next = (sizeof (Elf_External_Verdef)
6310 + sizeof (Elf_External_Verdaux));
6311 }
6312
6313 if (soname_indx != (size_t) -1)
6314 {
6315 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6316 soname_indx);
6317 def.vd_hash = bfd_elf_hash (soname);
6318 defaux.vda_name = soname_indx;
6319 name = soname;
6320 }
6321 else
6322 {
6323 size_t indx;
6324
6325 name = lbasename (output_bfd->filename);
6326 def.vd_hash = bfd_elf_hash (name);
6327 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6328 name, FALSE);
6329 if (indx == (size_t) -1)
6330 return FALSE;
6331 defaux.vda_name = indx;
6332 }
6333 defaux.vda_next = 0;
6334
6335 _bfd_elf_swap_verdef_out (output_bfd, &def,
6336 (Elf_External_Verdef *) p);
6337 p += sizeof (Elf_External_Verdef);
6338 if (info->create_default_symver)
6339 {
6340 /* Add a symbol representing this version. */
6341 bh = NULL;
6342 if (! (_bfd_generic_link_add_one_symbol
6343 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6344 0, NULL, FALSE,
6345 get_elf_backend_data (dynobj)->collect, &bh)))
6346 return FALSE;
6347 h = (struct elf_link_hash_entry *) bh;
6348 h->non_elf = 0;
6349 h->def_regular = 1;
6350 h->type = STT_OBJECT;
6351 h->verinfo.vertree = NULL;
6352
6353 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6354 return FALSE;
6355
6356 /* Create a duplicate of the base version with the same
6357 aux block, but different flags. */
6358 def.vd_flags = 0;
6359 def.vd_ndx = 2;
6360 def.vd_aux = sizeof (Elf_External_Verdef);
6361 if (verdefs)
6362 def.vd_next = (sizeof (Elf_External_Verdef)
6363 + sizeof (Elf_External_Verdaux));
6364 else
6365 def.vd_next = 0;
6366 _bfd_elf_swap_verdef_out (output_bfd, &def,
6367 (Elf_External_Verdef *) p);
6368 p += sizeof (Elf_External_Verdef);
6369 }
6370 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6371 (Elf_External_Verdaux *) p);
6372 p += sizeof (Elf_External_Verdaux);
6373
6374 for (t = verdefs; t != NULL; t = t->next)
6375 {
6376 unsigned int cdeps;
6377 struct bfd_elf_version_deps *n;
6378
6379 /* Don't emit the base version twice. */
6380 if (t->vernum == 0)
6381 continue;
6382
6383 cdeps = 0;
6384 for (n = t->deps; n != NULL; n = n->next)
6385 ++cdeps;
6386
6387 /* Add a symbol representing this version. */
6388 bh = NULL;
6389 if (! (_bfd_generic_link_add_one_symbol
6390 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6391 0, NULL, FALSE,
6392 get_elf_backend_data (dynobj)->collect, &bh)))
6393 return FALSE;
6394 h = (struct elf_link_hash_entry *) bh;
6395 h->non_elf = 0;
6396 h->def_regular = 1;
6397 h->type = STT_OBJECT;
6398 h->verinfo.vertree = t;
6399
6400 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6401 return FALSE;
6402
6403 def.vd_version = VER_DEF_CURRENT;
6404 def.vd_flags = 0;
6405 if (t->globals.list == NULL
6406 && t->locals.list == NULL
6407 && ! t->used)
6408 def.vd_flags |= VER_FLG_WEAK;
6409 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6410 def.vd_cnt = cdeps + 1;
6411 def.vd_hash = bfd_elf_hash (t->name);
6412 def.vd_aux = sizeof (Elf_External_Verdef);
6413 def.vd_next = 0;
6414
6415 /* If a basever node is next, it *must* be the last node in
6416 the chain, otherwise Verdef construction breaks. */
6417 if (t->next != NULL && t->next->vernum == 0)
6418 BFD_ASSERT (t->next->next == NULL);
6419
6420 if (t->next != NULL && t->next->vernum != 0)
6421 def.vd_next = (sizeof (Elf_External_Verdef)
6422 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6423
6424 _bfd_elf_swap_verdef_out (output_bfd, &def,
6425 (Elf_External_Verdef *) p);
6426 p += sizeof (Elf_External_Verdef);
6427
6428 defaux.vda_name = h->dynstr_index;
6429 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6430 h->dynstr_index);
6431 defaux.vda_next = 0;
6432 if (t->deps != NULL)
6433 defaux.vda_next = sizeof (Elf_External_Verdaux);
6434 t->name_indx = defaux.vda_name;
6435
6436 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6437 (Elf_External_Verdaux *) p);
6438 p += sizeof (Elf_External_Verdaux);
6439
6440 for (n = t->deps; n != NULL; n = n->next)
6441 {
6442 if (n->version_needed == NULL)
6443 {
6444 /* This can happen if there was an error in the
6445 version script. */
6446 defaux.vda_name = 0;
6447 }
6448 else
6449 {
6450 defaux.vda_name = n->version_needed->name_indx;
6451 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6452 defaux.vda_name);
6453 }
6454 if (n->next == NULL)
6455 defaux.vda_next = 0;
6456 else
6457 defaux.vda_next = sizeof (Elf_External_Verdaux);
6458
6459 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6460 (Elf_External_Verdaux *) p);
6461 p += sizeof (Elf_External_Verdaux);
6462 }
6463 }
6464
6465 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6466 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6467 return FALSE;
6468
6469 elf_tdata (output_bfd)->cverdefs = cdefs;
6470 }
6471
6472 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6473 {
6474 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6475 return FALSE;
6476 }
6477 else if (info->flags & DF_BIND_NOW)
6478 {
6479 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6480 return FALSE;
6481 }
6482
6483 if (info->flags_1)
6484 {
6485 if (bfd_link_executable (info))
6486 info->flags_1 &= ~ (DF_1_INITFIRST
6487 | DF_1_NODELETE
6488 | DF_1_NOOPEN);
6489 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6490 return FALSE;
6491 }
6492
6493 /* Work out the size of the version reference section. */
6494
6495 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6496 BFD_ASSERT (s != NULL);
6497 {
6498 struct elf_find_verdep_info sinfo;
6499
6500 sinfo.info = info;
6501 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6502 if (sinfo.vers == 0)
6503 sinfo.vers = 1;
6504 sinfo.failed = FALSE;
6505
6506 elf_link_hash_traverse (elf_hash_table (info),
6507 _bfd_elf_link_find_version_dependencies,
6508 &sinfo);
6509 if (sinfo.failed)
6510 return FALSE;
6511
6512 if (elf_tdata (output_bfd)->verref == NULL)
6513 s->flags |= SEC_EXCLUDE;
6514 else
6515 {
6516 Elf_Internal_Verneed *t;
6517 unsigned int size;
6518 unsigned int crefs;
6519 bfd_byte *p;
6520
6521 /* Build the version dependency section. */
6522 size = 0;
6523 crefs = 0;
6524 for (t = elf_tdata (output_bfd)->verref;
6525 t != NULL;
6526 t = t->vn_nextref)
6527 {
6528 Elf_Internal_Vernaux *a;
6529
6530 size += sizeof (Elf_External_Verneed);
6531 ++crefs;
6532 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6533 size += sizeof (Elf_External_Vernaux);
6534 }
6535
6536 s->size = size;
6537 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6538 if (s->contents == NULL)
6539 return FALSE;
6540
6541 p = s->contents;
6542 for (t = elf_tdata (output_bfd)->verref;
6543 t != NULL;
6544 t = t->vn_nextref)
6545 {
6546 unsigned int caux;
6547 Elf_Internal_Vernaux *a;
6548 size_t indx;
6549
6550 caux = 0;
6551 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6552 ++caux;
6553
6554 t->vn_version = VER_NEED_CURRENT;
6555 t->vn_cnt = caux;
6556 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6557 elf_dt_name (t->vn_bfd) != NULL
6558 ? elf_dt_name (t->vn_bfd)
6559 : lbasename (t->vn_bfd->filename),
6560 FALSE);
6561 if (indx == (size_t) -1)
6562 return FALSE;
6563 t->vn_file = indx;
6564 t->vn_aux = sizeof (Elf_External_Verneed);
6565 if (t->vn_nextref == NULL)
6566 t->vn_next = 0;
6567 else
6568 t->vn_next = (sizeof (Elf_External_Verneed)
6569 + caux * sizeof (Elf_External_Vernaux));
6570
6571 _bfd_elf_swap_verneed_out (output_bfd, t,
6572 (Elf_External_Verneed *) p);
6573 p += sizeof (Elf_External_Verneed);
6574
6575 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6576 {
6577 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6578 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6579 a->vna_nodename, FALSE);
6580 if (indx == (size_t) -1)
6581 return FALSE;
6582 a->vna_name = indx;
6583 if (a->vna_nextptr == NULL)
6584 a->vna_next = 0;
6585 else
6586 a->vna_next = sizeof (Elf_External_Vernaux);
6587
6588 _bfd_elf_swap_vernaux_out (output_bfd, a,
6589 (Elf_External_Vernaux *) p);
6590 p += sizeof (Elf_External_Vernaux);
6591 }
6592 }
6593
6594 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6595 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6596 return FALSE;
6597
6598 elf_tdata (output_bfd)->cverrefs = crefs;
6599 }
6600 }
6601
6602 if ((elf_tdata (output_bfd)->cverrefs == 0
6603 && elf_tdata (output_bfd)->cverdefs == 0)
6604 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6605 &section_sym_count) == 0)
6606 {
6607 s = bfd_get_linker_section (dynobj, ".gnu.version");
6608 s->flags |= SEC_EXCLUDE;
6609 }
6610 }
6611 return TRUE;
6612 }
6613
6614 /* Find the first non-excluded output section. We'll use its
6615 section symbol for some emitted relocs. */
6616 void
6617 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6618 {
6619 asection *s;
6620
6621 for (s = output_bfd->sections; s != NULL; s = s->next)
6622 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6623 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6624 {
6625 elf_hash_table (info)->text_index_section = s;
6626 break;
6627 }
6628 }
6629
6630 /* Find two non-excluded output sections, one for code, one for data.
6631 We'll use their section symbols for some emitted relocs. */
6632 void
6633 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6634 {
6635 asection *s;
6636
6637 /* Data first, since setting text_index_section changes
6638 _bfd_elf_link_omit_section_dynsym. */
6639 for (s = output_bfd->sections; s != NULL; s = s->next)
6640 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6641 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6642 {
6643 elf_hash_table (info)->data_index_section = s;
6644 break;
6645 }
6646
6647 for (s = output_bfd->sections; s != NULL; s = s->next)
6648 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6649 == (SEC_ALLOC | SEC_READONLY))
6650 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6651 {
6652 elf_hash_table (info)->text_index_section = s;
6653 break;
6654 }
6655
6656 if (elf_hash_table (info)->text_index_section == NULL)
6657 elf_hash_table (info)->text_index_section
6658 = elf_hash_table (info)->data_index_section;
6659 }
6660
6661 bfd_boolean
6662 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6663 {
6664 const struct elf_backend_data *bed;
6665
6666 if (!is_elf_hash_table (info->hash))
6667 return TRUE;
6668
6669 bed = get_elf_backend_data (output_bfd);
6670 (*bed->elf_backend_init_index_section) (output_bfd, info);
6671
6672 if (elf_hash_table (info)->dynamic_sections_created)
6673 {
6674 bfd *dynobj;
6675 asection *s;
6676 bfd_size_type dynsymcount;
6677 unsigned long section_sym_count;
6678 unsigned int dtagcount;
6679
6680 dynobj = elf_hash_table (info)->dynobj;
6681
6682 /* Assign dynsym indicies. In a shared library we generate a
6683 section symbol for each output section, which come first.
6684 Next come all of the back-end allocated local dynamic syms,
6685 followed by the rest of the global symbols. */
6686
6687 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6688 &section_sym_count);
6689
6690 /* Work out the size of the symbol version section. */
6691 s = bfd_get_linker_section (dynobj, ".gnu.version");
6692 BFD_ASSERT (s != NULL);
6693 if ((s->flags & SEC_EXCLUDE) == 0)
6694 {
6695 s->size = dynsymcount * sizeof (Elf_External_Versym);
6696 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6697 if (s->contents == NULL)
6698 return FALSE;
6699
6700 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6701 return FALSE;
6702 }
6703
6704 /* Set the size of the .dynsym and .hash sections. We counted
6705 the number of dynamic symbols in elf_link_add_object_symbols.
6706 We will build the contents of .dynsym and .hash when we build
6707 the final symbol table, because until then we do not know the
6708 correct value to give the symbols. We built the .dynstr
6709 section as we went along in elf_link_add_object_symbols. */
6710 s = elf_hash_table (info)->dynsym;
6711 BFD_ASSERT (s != NULL);
6712 s->size = dynsymcount * bed->s->sizeof_sym;
6713
6714 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6715 if (s->contents == NULL)
6716 return FALSE;
6717
6718 /* The first entry in .dynsym is a dummy symbol. Clear all the
6719 section syms, in case we don't output them all. */
6720 ++section_sym_count;
6721 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6722
6723 elf_hash_table (info)->bucketcount = 0;
6724
6725 /* Compute the size of the hashing table. As a side effect this
6726 computes the hash values for all the names we export. */
6727 if (info->emit_hash)
6728 {
6729 unsigned long int *hashcodes;
6730 struct hash_codes_info hashinf;
6731 bfd_size_type amt;
6732 unsigned long int nsyms;
6733 size_t bucketcount;
6734 size_t hash_entry_size;
6735
6736 /* Compute the hash values for all exported symbols. At the same
6737 time store the values in an array so that we could use them for
6738 optimizations. */
6739 amt = dynsymcount * sizeof (unsigned long int);
6740 hashcodes = (unsigned long int *) bfd_malloc (amt);
6741 if (hashcodes == NULL)
6742 return FALSE;
6743 hashinf.hashcodes = hashcodes;
6744 hashinf.error = FALSE;
6745
6746 /* Put all hash values in HASHCODES. */
6747 elf_link_hash_traverse (elf_hash_table (info),
6748 elf_collect_hash_codes, &hashinf);
6749 if (hashinf.error)
6750 {
6751 free (hashcodes);
6752 return FALSE;
6753 }
6754
6755 nsyms = hashinf.hashcodes - hashcodes;
6756 bucketcount
6757 = compute_bucket_count (info, hashcodes, nsyms, 0);
6758 free (hashcodes);
6759
6760 if (bucketcount == 0)
6761 return FALSE;
6762
6763 elf_hash_table (info)->bucketcount = bucketcount;
6764
6765 s = bfd_get_linker_section (dynobj, ".hash");
6766 BFD_ASSERT (s != NULL);
6767 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6768 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6769 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6770 if (s->contents == NULL)
6771 return FALSE;
6772
6773 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6774 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6775 s->contents + hash_entry_size);
6776 }
6777
6778 if (info->emit_gnu_hash)
6779 {
6780 size_t i, cnt;
6781 unsigned char *contents;
6782 struct collect_gnu_hash_codes cinfo;
6783 bfd_size_type amt;
6784 size_t bucketcount;
6785
6786 memset (&cinfo, 0, sizeof (cinfo));
6787
6788 /* Compute the hash values for all exported symbols. At the same
6789 time store the values in an array so that we could use them for
6790 optimizations. */
6791 amt = dynsymcount * 2 * sizeof (unsigned long int);
6792 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
6793 if (cinfo.hashcodes == NULL)
6794 return FALSE;
6795
6796 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6797 cinfo.min_dynindx = -1;
6798 cinfo.output_bfd = output_bfd;
6799 cinfo.bed = bed;
6800
6801 /* Put all hash values in HASHCODES. */
6802 elf_link_hash_traverse (elf_hash_table (info),
6803 elf_collect_gnu_hash_codes, &cinfo);
6804 if (cinfo.error)
6805 {
6806 free (cinfo.hashcodes);
6807 return FALSE;
6808 }
6809
6810 bucketcount
6811 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6812
6813 if (bucketcount == 0)
6814 {
6815 free (cinfo.hashcodes);
6816 return FALSE;
6817 }
6818
6819 s = bfd_get_linker_section (dynobj, ".gnu.hash");
6820 BFD_ASSERT (s != NULL);
6821
6822 if (cinfo.nsyms == 0)
6823 {
6824 /* Empty .gnu.hash section is special. */
6825 BFD_ASSERT (cinfo.min_dynindx == -1);
6826 free (cinfo.hashcodes);
6827 s->size = 5 * 4 + bed->s->arch_size / 8;
6828 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6829 if (contents == NULL)
6830 return FALSE;
6831 s->contents = contents;
6832 /* 1 empty bucket. */
6833 bfd_put_32 (output_bfd, 1, contents);
6834 /* SYMIDX above the special symbol 0. */
6835 bfd_put_32 (output_bfd, 1, contents + 4);
6836 /* Just one word for bitmask. */
6837 bfd_put_32 (output_bfd, 1, contents + 8);
6838 /* Only hash fn bloom filter. */
6839 bfd_put_32 (output_bfd, 0, contents + 12);
6840 /* No hashes are valid - empty bitmask. */
6841 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6842 /* No hashes in the only bucket. */
6843 bfd_put_32 (output_bfd, 0,
6844 contents + 16 + bed->s->arch_size / 8);
6845 }
6846 else
6847 {
6848 unsigned long int maskwords, maskbitslog2, x;
6849 BFD_ASSERT (cinfo.min_dynindx != -1);
6850
6851 x = cinfo.nsyms;
6852 maskbitslog2 = 1;
6853 while ((x >>= 1) != 0)
6854 ++maskbitslog2;
6855 if (maskbitslog2 < 3)
6856 maskbitslog2 = 5;
6857 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6858 maskbitslog2 = maskbitslog2 + 3;
6859 else
6860 maskbitslog2 = maskbitslog2 + 2;
6861 if (bed->s->arch_size == 64)
6862 {
6863 if (maskbitslog2 == 5)
6864 maskbitslog2 = 6;
6865 cinfo.shift1 = 6;
6866 }
6867 else
6868 cinfo.shift1 = 5;
6869 cinfo.mask = (1 << cinfo.shift1) - 1;
6870 cinfo.shift2 = maskbitslog2;
6871 cinfo.maskbits = 1 << maskbitslog2;
6872 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6873 amt = bucketcount * sizeof (unsigned long int) * 2;
6874 amt += maskwords * sizeof (bfd_vma);
6875 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
6876 if (cinfo.bitmask == NULL)
6877 {
6878 free (cinfo.hashcodes);
6879 return FALSE;
6880 }
6881
6882 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
6883 cinfo.indx = cinfo.counts + bucketcount;
6884 cinfo.symindx = dynsymcount - cinfo.nsyms;
6885 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6886
6887 /* Determine how often each hash bucket is used. */
6888 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6889 for (i = 0; i < cinfo.nsyms; ++i)
6890 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6891
6892 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6893 if (cinfo.counts[i] != 0)
6894 {
6895 cinfo.indx[i] = cnt;
6896 cnt += cinfo.counts[i];
6897 }
6898 BFD_ASSERT (cnt == dynsymcount);
6899 cinfo.bucketcount = bucketcount;
6900 cinfo.local_indx = cinfo.min_dynindx;
6901
6902 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6903 s->size += cinfo.maskbits / 8;
6904 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6905 if (contents == NULL)
6906 {
6907 free (cinfo.bitmask);
6908 free (cinfo.hashcodes);
6909 return FALSE;
6910 }
6911
6912 s->contents = contents;
6913 bfd_put_32 (output_bfd, bucketcount, contents);
6914 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6915 bfd_put_32 (output_bfd, maskwords, contents + 8);
6916 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6917 contents += 16 + cinfo.maskbits / 8;
6918
6919 for (i = 0; i < bucketcount; ++i)
6920 {
6921 if (cinfo.counts[i] == 0)
6922 bfd_put_32 (output_bfd, 0, contents);
6923 else
6924 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6925 contents += 4;
6926 }
6927
6928 cinfo.contents = contents;
6929
6930 /* Renumber dynamic symbols, populate .gnu.hash section. */
6931 elf_link_hash_traverse (elf_hash_table (info),
6932 elf_renumber_gnu_hash_syms, &cinfo);
6933
6934 contents = s->contents + 16;
6935 for (i = 0; i < maskwords; ++i)
6936 {
6937 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6938 contents);
6939 contents += bed->s->arch_size / 8;
6940 }
6941
6942 free (cinfo.bitmask);
6943 free (cinfo.hashcodes);
6944 }
6945 }
6946
6947 s = bfd_get_linker_section (dynobj, ".dynstr");
6948 BFD_ASSERT (s != NULL);
6949
6950 elf_finalize_dynstr (output_bfd, info);
6951
6952 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6953
6954 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6955 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6956 return FALSE;
6957 }
6958
6959 return TRUE;
6960 }
6961 \f
6962 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
6963
6964 static void
6965 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6966 asection *sec)
6967 {
6968 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
6969 sec->sec_info_type = SEC_INFO_TYPE_NONE;
6970 }
6971
6972 /* Finish SHF_MERGE section merging. */
6973
6974 bfd_boolean
6975 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
6976 {
6977 bfd *ibfd;
6978 asection *sec;
6979
6980 if (!is_elf_hash_table (info->hash))
6981 return FALSE;
6982
6983 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6984 if ((ibfd->flags & DYNAMIC) == 0
6985 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6986 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
6987 == get_elf_backend_data (obfd)->s->elfclass))
6988 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6989 if ((sec->flags & SEC_MERGE) != 0
6990 && !bfd_is_abs_section (sec->output_section))
6991 {
6992 struct bfd_elf_section_data *secdata;
6993
6994 secdata = elf_section_data (sec);
6995 if (! _bfd_add_merge_section (obfd,
6996 &elf_hash_table (info)->merge_info,
6997 sec, &secdata->sec_info))
6998 return FALSE;
6999 else if (secdata->sec_info)
7000 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7001 }
7002
7003 if (elf_hash_table (info)->merge_info != NULL)
7004 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7005 merge_sections_remove_hook);
7006 return TRUE;
7007 }
7008
7009 /* Create an entry in an ELF linker hash table. */
7010
7011 struct bfd_hash_entry *
7012 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7013 struct bfd_hash_table *table,
7014 const char *string)
7015 {
7016 /* Allocate the structure if it has not already been allocated by a
7017 subclass. */
7018 if (entry == NULL)
7019 {
7020 entry = (struct bfd_hash_entry *)
7021 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7022 if (entry == NULL)
7023 return entry;
7024 }
7025
7026 /* Call the allocation method of the superclass. */
7027 entry = _bfd_link_hash_newfunc (entry, table, string);
7028 if (entry != NULL)
7029 {
7030 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7031 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7032
7033 /* Set local fields. */
7034 ret->indx = -1;
7035 ret->dynindx = -1;
7036 ret->got = htab->init_got_refcount;
7037 ret->plt = htab->init_plt_refcount;
7038 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7039 - offsetof (struct elf_link_hash_entry, size)));
7040 /* Assume that we have been called by a non-ELF symbol reader.
7041 This flag is then reset by the code which reads an ELF input
7042 file. This ensures that a symbol created by a non-ELF symbol
7043 reader will have the flag set correctly. */
7044 ret->non_elf = 1;
7045 }
7046
7047 return entry;
7048 }
7049
7050 /* Copy data from an indirect symbol to its direct symbol, hiding the
7051 old indirect symbol. Also used for copying flags to a weakdef. */
7052
7053 void
7054 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7055 struct elf_link_hash_entry *dir,
7056 struct elf_link_hash_entry *ind)
7057 {
7058 struct elf_link_hash_table *htab;
7059
7060 /* Copy down any references that we may have already seen to the
7061 symbol which just became indirect if DIR isn't a hidden versioned
7062 symbol. */
7063
7064 if (dir->versioned != versioned_hidden)
7065 {
7066 dir->ref_dynamic |= ind->ref_dynamic;
7067 dir->ref_regular |= ind->ref_regular;
7068 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7069 dir->non_got_ref |= ind->non_got_ref;
7070 dir->needs_plt |= ind->needs_plt;
7071 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7072 }
7073
7074 if (ind->root.type != bfd_link_hash_indirect)
7075 return;
7076
7077 /* Copy over the global and procedure linkage table refcount entries.
7078 These may have been already set up by a check_relocs routine. */
7079 htab = elf_hash_table (info);
7080 if (ind->got.refcount > htab->init_got_refcount.refcount)
7081 {
7082 if (dir->got.refcount < 0)
7083 dir->got.refcount = 0;
7084 dir->got.refcount += ind->got.refcount;
7085 ind->got.refcount = htab->init_got_refcount.refcount;
7086 }
7087
7088 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7089 {
7090 if (dir->plt.refcount < 0)
7091 dir->plt.refcount = 0;
7092 dir->plt.refcount += ind->plt.refcount;
7093 ind->plt.refcount = htab->init_plt_refcount.refcount;
7094 }
7095
7096 if (ind->dynindx != -1)
7097 {
7098 if (dir->dynindx != -1)
7099 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7100 dir->dynindx = ind->dynindx;
7101 dir->dynstr_index = ind->dynstr_index;
7102 ind->dynindx = -1;
7103 ind->dynstr_index = 0;
7104 }
7105 }
7106
7107 void
7108 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7109 struct elf_link_hash_entry *h,
7110 bfd_boolean force_local)
7111 {
7112 /* STT_GNU_IFUNC symbol must go through PLT. */
7113 if (h->type != STT_GNU_IFUNC)
7114 {
7115 h->plt = elf_hash_table (info)->init_plt_offset;
7116 h->needs_plt = 0;
7117 }
7118 if (force_local)
7119 {
7120 h->forced_local = 1;
7121 if (h->dynindx != -1)
7122 {
7123 h->dynindx = -1;
7124 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7125 h->dynstr_index);
7126 }
7127 }
7128 }
7129
7130 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7131 caller. */
7132
7133 bfd_boolean
7134 _bfd_elf_link_hash_table_init
7135 (struct elf_link_hash_table *table,
7136 bfd *abfd,
7137 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7138 struct bfd_hash_table *,
7139 const char *),
7140 unsigned int entsize,
7141 enum elf_target_id target_id)
7142 {
7143 bfd_boolean ret;
7144 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7145
7146 table->init_got_refcount.refcount = can_refcount - 1;
7147 table->init_plt_refcount.refcount = can_refcount - 1;
7148 table->init_got_offset.offset = -(bfd_vma) 1;
7149 table->init_plt_offset.offset = -(bfd_vma) 1;
7150 /* The first dynamic symbol is a dummy. */
7151 table->dynsymcount = 1;
7152
7153 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7154
7155 table->root.type = bfd_link_elf_hash_table;
7156 table->hash_table_id = target_id;
7157
7158 return ret;
7159 }
7160
7161 /* Create an ELF linker hash table. */
7162
7163 struct bfd_link_hash_table *
7164 _bfd_elf_link_hash_table_create (bfd *abfd)
7165 {
7166 struct elf_link_hash_table *ret;
7167 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7168
7169 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7170 if (ret == NULL)
7171 return NULL;
7172
7173 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7174 sizeof (struct elf_link_hash_entry),
7175 GENERIC_ELF_DATA))
7176 {
7177 free (ret);
7178 return NULL;
7179 }
7180 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7181
7182 return &ret->root;
7183 }
7184
7185 /* Destroy an ELF linker hash table. */
7186
7187 void
7188 _bfd_elf_link_hash_table_free (bfd *obfd)
7189 {
7190 struct elf_link_hash_table *htab;
7191
7192 htab = (struct elf_link_hash_table *) obfd->link.hash;
7193 if (htab->dynstr != NULL)
7194 _bfd_elf_strtab_free (htab->dynstr);
7195 _bfd_merge_sections_free (htab->merge_info);
7196 _bfd_generic_link_hash_table_free (obfd);
7197 }
7198
7199 /* This is a hook for the ELF emulation code in the generic linker to
7200 tell the backend linker what file name to use for the DT_NEEDED
7201 entry for a dynamic object. */
7202
7203 void
7204 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7205 {
7206 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7207 && bfd_get_format (abfd) == bfd_object)
7208 elf_dt_name (abfd) = name;
7209 }
7210
7211 int
7212 bfd_elf_get_dyn_lib_class (bfd *abfd)
7213 {
7214 int lib_class;
7215 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7216 && bfd_get_format (abfd) == bfd_object)
7217 lib_class = elf_dyn_lib_class (abfd);
7218 else
7219 lib_class = 0;
7220 return lib_class;
7221 }
7222
7223 void
7224 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7225 {
7226 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7227 && bfd_get_format (abfd) == bfd_object)
7228 elf_dyn_lib_class (abfd) = lib_class;
7229 }
7230
7231 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7232 the linker ELF emulation code. */
7233
7234 struct bfd_link_needed_list *
7235 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7236 struct bfd_link_info *info)
7237 {
7238 if (! is_elf_hash_table (info->hash))
7239 return NULL;
7240 return elf_hash_table (info)->needed;
7241 }
7242
7243 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7244 hook for the linker ELF emulation code. */
7245
7246 struct bfd_link_needed_list *
7247 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7248 struct bfd_link_info *info)
7249 {
7250 if (! is_elf_hash_table (info->hash))
7251 return NULL;
7252 return elf_hash_table (info)->runpath;
7253 }
7254
7255 /* Get the name actually used for a dynamic object for a link. This
7256 is the SONAME entry if there is one. Otherwise, it is the string
7257 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7258
7259 const char *
7260 bfd_elf_get_dt_soname (bfd *abfd)
7261 {
7262 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7263 && bfd_get_format (abfd) == bfd_object)
7264 return elf_dt_name (abfd);
7265 return NULL;
7266 }
7267
7268 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7269 the ELF linker emulation code. */
7270
7271 bfd_boolean
7272 bfd_elf_get_bfd_needed_list (bfd *abfd,
7273 struct bfd_link_needed_list **pneeded)
7274 {
7275 asection *s;
7276 bfd_byte *dynbuf = NULL;
7277 unsigned int elfsec;
7278 unsigned long shlink;
7279 bfd_byte *extdyn, *extdynend;
7280 size_t extdynsize;
7281 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7282
7283 *pneeded = NULL;
7284
7285 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7286 || bfd_get_format (abfd) != bfd_object)
7287 return TRUE;
7288
7289 s = bfd_get_section_by_name (abfd, ".dynamic");
7290 if (s == NULL || s->size == 0)
7291 return TRUE;
7292
7293 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7294 goto error_return;
7295
7296 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7297 if (elfsec == SHN_BAD)
7298 goto error_return;
7299
7300 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7301
7302 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7303 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7304
7305 extdyn = dynbuf;
7306 extdynend = extdyn + s->size;
7307 for (; extdyn < extdynend; extdyn += extdynsize)
7308 {
7309 Elf_Internal_Dyn dyn;
7310
7311 (*swap_dyn_in) (abfd, extdyn, &dyn);
7312
7313 if (dyn.d_tag == DT_NULL)
7314 break;
7315
7316 if (dyn.d_tag == DT_NEEDED)
7317 {
7318 const char *string;
7319 struct bfd_link_needed_list *l;
7320 unsigned int tagv = dyn.d_un.d_val;
7321 bfd_size_type amt;
7322
7323 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7324 if (string == NULL)
7325 goto error_return;
7326
7327 amt = sizeof *l;
7328 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7329 if (l == NULL)
7330 goto error_return;
7331
7332 l->by = abfd;
7333 l->name = string;
7334 l->next = *pneeded;
7335 *pneeded = l;
7336 }
7337 }
7338
7339 free (dynbuf);
7340
7341 return TRUE;
7342
7343 error_return:
7344 if (dynbuf != NULL)
7345 free (dynbuf);
7346 return FALSE;
7347 }
7348
7349 struct elf_symbuf_symbol
7350 {
7351 unsigned long st_name; /* Symbol name, index in string tbl */
7352 unsigned char st_info; /* Type and binding attributes */
7353 unsigned char st_other; /* Visibilty, and target specific */
7354 };
7355
7356 struct elf_symbuf_head
7357 {
7358 struct elf_symbuf_symbol *ssym;
7359 size_t count;
7360 unsigned int st_shndx;
7361 };
7362
7363 struct elf_symbol
7364 {
7365 union
7366 {
7367 Elf_Internal_Sym *isym;
7368 struct elf_symbuf_symbol *ssym;
7369 } u;
7370 const char *name;
7371 };
7372
7373 /* Sort references to symbols by ascending section number. */
7374
7375 static int
7376 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7377 {
7378 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7379 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7380
7381 return s1->st_shndx - s2->st_shndx;
7382 }
7383
7384 static int
7385 elf_sym_name_compare (const void *arg1, const void *arg2)
7386 {
7387 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7388 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7389 return strcmp (s1->name, s2->name);
7390 }
7391
7392 static struct elf_symbuf_head *
7393 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
7394 {
7395 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7396 struct elf_symbuf_symbol *ssym;
7397 struct elf_symbuf_head *ssymbuf, *ssymhead;
7398 size_t i, shndx_count, total_size;
7399
7400 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7401 if (indbuf == NULL)
7402 return NULL;
7403
7404 for (ind = indbuf, i = 0; i < symcount; i++)
7405 if (isymbuf[i].st_shndx != SHN_UNDEF)
7406 *ind++ = &isymbuf[i];
7407 indbufend = ind;
7408
7409 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7410 elf_sort_elf_symbol);
7411
7412 shndx_count = 0;
7413 if (indbufend > indbuf)
7414 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7415 if (ind[0]->st_shndx != ind[1]->st_shndx)
7416 shndx_count++;
7417
7418 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7419 + (indbufend - indbuf) * sizeof (*ssym));
7420 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7421 if (ssymbuf == NULL)
7422 {
7423 free (indbuf);
7424 return NULL;
7425 }
7426
7427 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7428 ssymbuf->ssym = NULL;
7429 ssymbuf->count = shndx_count;
7430 ssymbuf->st_shndx = 0;
7431 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7432 {
7433 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7434 {
7435 ssymhead++;
7436 ssymhead->ssym = ssym;
7437 ssymhead->count = 0;
7438 ssymhead->st_shndx = (*ind)->st_shndx;
7439 }
7440 ssym->st_name = (*ind)->st_name;
7441 ssym->st_info = (*ind)->st_info;
7442 ssym->st_other = (*ind)->st_other;
7443 ssymhead->count++;
7444 }
7445 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
7446 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7447 == total_size));
7448
7449 free (indbuf);
7450 return ssymbuf;
7451 }
7452
7453 /* Check if 2 sections define the same set of local and global
7454 symbols. */
7455
7456 static bfd_boolean
7457 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7458 struct bfd_link_info *info)
7459 {
7460 bfd *bfd1, *bfd2;
7461 const struct elf_backend_data *bed1, *bed2;
7462 Elf_Internal_Shdr *hdr1, *hdr2;
7463 size_t symcount1, symcount2;
7464 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7465 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7466 Elf_Internal_Sym *isym, *isymend;
7467 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7468 size_t count1, count2, i;
7469 unsigned int shndx1, shndx2;
7470 bfd_boolean result;
7471
7472 bfd1 = sec1->owner;
7473 bfd2 = sec2->owner;
7474
7475 /* Both sections have to be in ELF. */
7476 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7477 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7478 return FALSE;
7479
7480 if (elf_section_type (sec1) != elf_section_type (sec2))
7481 return FALSE;
7482
7483 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7484 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7485 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7486 return FALSE;
7487
7488 bed1 = get_elf_backend_data (bfd1);
7489 bed2 = get_elf_backend_data (bfd2);
7490 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7491 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7492 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7493 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7494
7495 if (symcount1 == 0 || symcount2 == 0)
7496 return FALSE;
7497
7498 result = FALSE;
7499 isymbuf1 = NULL;
7500 isymbuf2 = NULL;
7501 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7502 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7503
7504 if (ssymbuf1 == NULL)
7505 {
7506 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7507 NULL, NULL, NULL);
7508 if (isymbuf1 == NULL)
7509 goto done;
7510
7511 if (!info->reduce_memory_overheads)
7512 elf_tdata (bfd1)->symbuf = ssymbuf1
7513 = elf_create_symbuf (symcount1, isymbuf1);
7514 }
7515
7516 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7517 {
7518 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7519 NULL, NULL, NULL);
7520 if (isymbuf2 == NULL)
7521 goto done;
7522
7523 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7524 elf_tdata (bfd2)->symbuf = ssymbuf2
7525 = elf_create_symbuf (symcount2, isymbuf2);
7526 }
7527
7528 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7529 {
7530 /* Optimized faster version. */
7531 size_t lo, hi, mid;
7532 struct elf_symbol *symp;
7533 struct elf_symbuf_symbol *ssym, *ssymend;
7534
7535 lo = 0;
7536 hi = ssymbuf1->count;
7537 ssymbuf1++;
7538 count1 = 0;
7539 while (lo < hi)
7540 {
7541 mid = (lo + hi) / 2;
7542 if (shndx1 < ssymbuf1[mid].st_shndx)
7543 hi = mid;
7544 else if (shndx1 > ssymbuf1[mid].st_shndx)
7545 lo = mid + 1;
7546 else
7547 {
7548 count1 = ssymbuf1[mid].count;
7549 ssymbuf1 += mid;
7550 break;
7551 }
7552 }
7553
7554 lo = 0;
7555 hi = ssymbuf2->count;
7556 ssymbuf2++;
7557 count2 = 0;
7558 while (lo < hi)
7559 {
7560 mid = (lo + hi) / 2;
7561 if (shndx2 < ssymbuf2[mid].st_shndx)
7562 hi = mid;
7563 else if (shndx2 > ssymbuf2[mid].st_shndx)
7564 lo = mid + 1;
7565 else
7566 {
7567 count2 = ssymbuf2[mid].count;
7568 ssymbuf2 += mid;
7569 break;
7570 }
7571 }
7572
7573 if (count1 == 0 || count2 == 0 || count1 != count2)
7574 goto done;
7575
7576 symtable1
7577 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7578 symtable2
7579 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
7580 if (symtable1 == NULL || symtable2 == NULL)
7581 goto done;
7582
7583 symp = symtable1;
7584 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7585 ssym < ssymend; ssym++, symp++)
7586 {
7587 symp->u.ssym = ssym;
7588 symp->name = bfd_elf_string_from_elf_section (bfd1,
7589 hdr1->sh_link,
7590 ssym->st_name);
7591 }
7592
7593 symp = symtable2;
7594 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7595 ssym < ssymend; ssym++, symp++)
7596 {
7597 symp->u.ssym = ssym;
7598 symp->name = bfd_elf_string_from_elf_section (bfd2,
7599 hdr2->sh_link,
7600 ssym->st_name);
7601 }
7602
7603 /* Sort symbol by name. */
7604 qsort (symtable1, count1, sizeof (struct elf_symbol),
7605 elf_sym_name_compare);
7606 qsort (symtable2, count1, sizeof (struct elf_symbol),
7607 elf_sym_name_compare);
7608
7609 for (i = 0; i < count1; i++)
7610 /* Two symbols must have the same binding, type and name. */
7611 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7612 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7613 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7614 goto done;
7615
7616 result = TRUE;
7617 goto done;
7618 }
7619
7620 symtable1 = (struct elf_symbol *)
7621 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7622 symtable2 = (struct elf_symbol *)
7623 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7624 if (symtable1 == NULL || symtable2 == NULL)
7625 goto done;
7626
7627 /* Count definitions in the section. */
7628 count1 = 0;
7629 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7630 if (isym->st_shndx == shndx1)
7631 symtable1[count1++].u.isym = isym;
7632
7633 count2 = 0;
7634 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7635 if (isym->st_shndx == shndx2)
7636 symtable2[count2++].u.isym = isym;
7637
7638 if (count1 == 0 || count2 == 0 || count1 != count2)
7639 goto done;
7640
7641 for (i = 0; i < count1; i++)
7642 symtable1[i].name
7643 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7644 symtable1[i].u.isym->st_name);
7645
7646 for (i = 0; i < count2; i++)
7647 symtable2[i].name
7648 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7649 symtable2[i].u.isym->st_name);
7650
7651 /* Sort symbol by name. */
7652 qsort (symtable1, count1, sizeof (struct elf_symbol),
7653 elf_sym_name_compare);
7654 qsort (symtable2, count1, sizeof (struct elf_symbol),
7655 elf_sym_name_compare);
7656
7657 for (i = 0; i < count1; i++)
7658 /* Two symbols must have the same binding, type and name. */
7659 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7660 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7661 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7662 goto done;
7663
7664 result = TRUE;
7665
7666 done:
7667 if (symtable1)
7668 free (symtable1);
7669 if (symtable2)
7670 free (symtable2);
7671 if (isymbuf1)
7672 free (isymbuf1);
7673 if (isymbuf2)
7674 free (isymbuf2);
7675
7676 return result;
7677 }
7678
7679 /* Return TRUE if 2 section types are compatible. */
7680
7681 bfd_boolean
7682 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7683 bfd *bbfd, const asection *bsec)
7684 {
7685 if (asec == NULL
7686 || bsec == NULL
7687 || abfd->xvec->flavour != bfd_target_elf_flavour
7688 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7689 return TRUE;
7690
7691 return elf_section_type (asec) == elf_section_type (bsec);
7692 }
7693 \f
7694 /* Final phase of ELF linker. */
7695
7696 /* A structure we use to avoid passing large numbers of arguments. */
7697
7698 struct elf_final_link_info
7699 {
7700 /* General link information. */
7701 struct bfd_link_info *info;
7702 /* Output BFD. */
7703 bfd *output_bfd;
7704 /* Symbol string table. */
7705 struct elf_strtab_hash *symstrtab;
7706 /* .hash section. */
7707 asection *hash_sec;
7708 /* symbol version section (.gnu.version). */
7709 asection *symver_sec;
7710 /* Buffer large enough to hold contents of any section. */
7711 bfd_byte *contents;
7712 /* Buffer large enough to hold external relocs of any section. */
7713 void *external_relocs;
7714 /* Buffer large enough to hold internal relocs of any section. */
7715 Elf_Internal_Rela *internal_relocs;
7716 /* Buffer large enough to hold external local symbols of any input
7717 BFD. */
7718 bfd_byte *external_syms;
7719 /* And a buffer for symbol section indices. */
7720 Elf_External_Sym_Shndx *locsym_shndx;
7721 /* Buffer large enough to hold internal local symbols of any input
7722 BFD. */
7723 Elf_Internal_Sym *internal_syms;
7724 /* Array large enough to hold a symbol index for each local symbol
7725 of any input BFD. */
7726 long *indices;
7727 /* Array large enough to hold a section pointer for each local
7728 symbol of any input BFD. */
7729 asection **sections;
7730 /* Buffer for SHT_SYMTAB_SHNDX section. */
7731 Elf_External_Sym_Shndx *symshndxbuf;
7732 /* Number of STT_FILE syms seen. */
7733 size_t filesym_count;
7734 };
7735
7736 /* This struct is used to pass information to elf_link_output_extsym. */
7737
7738 struct elf_outext_info
7739 {
7740 bfd_boolean failed;
7741 bfd_boolean localsyms;
7742 bfd_boolean file_sym_done;
7743 struct elf_final_link_info *flinfo;
7744 };
7745
7746
7747 /* Support for evaluating a complex relocation.
7748
7749 Complex relocations are generalized, self-describing relocations. The
7750 implementation of them consists of two parts: complex symbols, and the
7751 relocations themselves.
7752
7753 The relocations are use a reserved elf-wide relocation type code (R_RELC
7754 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7755 information (start bit, end bit, word width, etc) into the addend. This
7756 information is extracted from CGEN-generated operand tables within gas.
7757
7758 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7759 internal) representing prefix-notation expressions, including but not
7760 limited to those sorts of expressions normally encoded as addends in the
7761 addend field. The symbol mangling format is:
7762
7763 <node> := <literal>
7764 | <unary-operator> ':' <node>
7765 | <binary-operator> ':' <node> ':' <node>
7766 ;
7767
7768 <literal> := 's' <digits=N> ':' <N character symbol name>
7769 | 'S' <digits=N> ':' <N character section name>
7770 | '#' <hexdigits>
7771 ;
7772
7773 <binary-operator> := as in C
7774 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7775
7776 static void
7777 set_symbol_value (bfd *bfd_with_globals,
7778 Elf_Internal_Sym *isymbuf,
7779 size_t locsymcount,
7780 size_t symidx,
7781 bfd_vma val)
7782 {
7783 struct elf_link_hash_entry **sym_hashes;
7784 struct elf_link_hash_entry *h;
7785 size_t extsymoff = locsymcount;
7786
7787 if (symidx < locsymcount)
7788 {
7789 Elf_Internal_Sym *sym;
7790
7791 sym = isymbuf + symidx;
7792 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7793 {
7794 /* It is a local symbol: move it to the
7795 "absolute" section and give it a value. */
7796 sym->st_shndx = SHN_ABS;
7797 sym->st_value = val;
7798 return;
7799 }
7800 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7801 extsymoff = 0;
7802 }
7803
7804 /* It is a global symbol: set its link type
7805 to "defined" and give it a value. */
7806
7807 sym_hashes = elf_sym_hashes (bfd_with_globals);
7808 h = sym_hashes [symidx - extsymoff];
7809 while (h->root.type == bfd_link_hash_indirect
7810 || h->root.type == bfd_link_hash_warning)
7811 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7812 h->root.type = bfd_link_hash_defined;
7813 h->root.u.def.value = val;
7814 h->root.u.def.section = bfd_abs_section_ptr;
7815 }
7816
7817 static bfd_boolean
7818 resolve_symbol (const char *name,
7819 bfd *input_bfd,
7820 struct elf_final_link_info *flinfo,
7821 bfd_vma *result,
7822 Elf_Internal_Sym *isymbuf,
7823 size_t locsymcount)
7824 {
7825 Elf_Internal_Sym *sym;
7826 struct bfd_link_hash_entry *global_entry;
7827 const char *candidate = NULL;
7828 Elf_Internal_Shdr *symtab_hdr;
7829 size_t i;
7830
7831 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7832
7833 for (i = 0; i < locsymcount; ++ i)
7834 {
7835 sym = isymbuf + i;
7836
7837 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7838 continue;
7839
7840 candidate = bfd_elf_string_from_elf_section (input_bfd,
7841 symtab_hdr->sh_link,
7842 sym->st_name);
7843 #ifdef DEBUG
7844 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7845 name, candidate, (unsigned long) sym->st_value);
7846 #endif
7847 if (candidate && strcmp (candidate, name) == 0)
7848 {
7849 asection *sec = flinfo->sections [i];
7850
7851 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7852 *result += sec->output_offset + sec->output_section->vma;
7853 #ifdef DEBUG
7854 printf ("Found symbol with value %8.8lx\n",
7855 (unsigned long) *result);
7856 #endif
7857 return TRUE;
7858 }
7859 }
7860
7861 /* Hmm, haven't found it yet. perhaps it is a global. */
7862 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
7863 FALSE, FALSE, TRUE);
7864 if (!global_entry)
7865 return FALSE;
7866
7867 if (global_entry->type == bfd_link_hash_defined
7868 || global_entry->type == bfd_link_hash_defweak)
7869 {
7870 *result = (global_entry->u.def.value
7871 + global_entry->u.def.section->output_section->vma
7872 + global_entry->u.def.section->output_offset);
7873 #ifdef DEBUG
7874 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7875 global_entry->root.string, (unsigned long) *result);
7876 #endif
7877 return TRUE;
7878 }
7879
7880 return FALSE;
7881 }
7882
7883 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
7884 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
7885 names like "foo.end" which is the end address of section "foo". */
7886
7887 static bfd_boolean
7888 resolve_section (const char *name,
7889 asection *sections,
7890 bfd_vma *result,
7891 bfd * abfd)
7892 {
7893 asection *curr;
7894 unsigned int len;
7895
7896 for (curr = sections; curr; curr = curr->next)
7897 if (strcmp (curr->name, name) == 0)
7898 {
7899 *result = curr->vma;
7900 return TRUE;
7901 }
7902
7903 /* Hmm. still haven't found it. try pseudo-section names. */
7904 /* FIXME: This could be coded more efficiently... */
7905 for (curr = sections; curr; curr = curr->next)
7906 {
7907 len = strlen (curr->name);
7908 if (len > strlen (name))
7909 continue;
7910
7911 if (strncmp (curr->name, name, len) == 0)
7912 {
7913 if (strncmp (".end", name + len, 4) == 0)
7914 {
7915 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
7916 return TRUE;
7917 }
7918
7919 /* Insert more pseudo-section names here, if you like. */
7920 }
7921 }
7922
7923 return FALSE;
7924 }
7925
7926 static void
7927 undefined_reference (const char *reftype, const char *name)
7928 {
7929 /* xgettext:c-format */
7930 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7931 reftype, name);
7932 }
7933
7934 static bfd_boolean
7935 eval_symbol (bfd_vma *result,
7936 const char **symp,
7937 bfd *input_bfd,
7938 struct elf_final_link_info *flinfo,
7939 bfd_vma dot,
7940 Elf_Internal_Sym *isymbuf,
7941 size_t locsymcount,
7942 int signed_p)
7943 {
7944 size_t len;
7945 size_t symlen;
7946 bfd_vma a;
7947 bfd_vma b;
7948 char symbuf[4096];
7949 const char *sym = *symp;
7950 const char *symend;
7951 bfd_boolean symbol_is_section = FALSE;
7952
7953 len = strlen (sym);
7954 symend = sym + len;
7955
7956 if (len < 1 || len > sizeof (symbuf))
7957 {
7958 bfd_set_error (bfd_error_invalid_operation);
7959 return FALSE;
7960 }
7961
7962 switch (* sym)
7963 {
7964 case '.':
7965 *result = dot;
7966 *symp = sym + 1;
7967 return TRUE;
7968
7969 case '#':
7970 ++sym;
7971 *result = strtoul (sym, (char **) symp, 16);
7972 return TRUE;
7973
7974 case 'S':
7975 symbol_is_section = TRUE;
7976 /* Fall through. */
7977 case 's':
7978 ++sym;
7979 symlen = strtol (sym, (char **) symp, 10);
7980 sym = *symp + 1; /* Skip the trailing ':'. */
7981
7982 if (symend < sym || symlen + 1 > sizeof (symbuf))
7983 {
7984 bfd_set_error (bfd_error_invalid_operation);
7985 return FALSE;
7986 }
7987
7988 memcpy (symbuf, sym, symlen);
7989 symbuf[symlen] = '\0';
7990 *symp = sym + symlen;
7991
7992 /* Is it always possible, with complex symbols, that gas "mis-guessed"
7993 the symbol as a section, or vice-versa. so we're pretty liberal in our
7994 interpretation here; section means "try section first", not "must be a
7995 section", and likewise with symbol. */
7996
7997 if (symbol_is_section)
7998 {
7999 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8000 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8001 isymbuf, locsymcount))
8002 {
8003 undefined_reference ("section", symbuf);
8004 return FALSE;
8005 }
8006 }
8007 else
8008 {
8009 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8010 isymbuf, locsymcount)
8011 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8012 result, input_bfd))
8013 {
8014 undefined_reference ("symbol", symbuf);
8015 return FALSE;
8016 }
8017 }
8018
8019 return TRUE;
8020
8021 /* All that remains are operators. */
8022
8023 #define UNARY_OP(op) \
8024 if (strncmp (sym, #op, strlen (#op)) == 0) \
8025 { \
8026 sym += strlen (#op); \
8027 if (*sym == ':') \
8028 ++sym; \
8029 *symp = sym; \
8030 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8031 isymbuf, locsymcount, signed_p)) \
8032 return FALSE; \
8033 if (signed_p) \
8034 *result = op ((bfd_signed_vma) a); \
8035 else \
8036 *result = op a; \
8037 return TRUE; \
8038 }
8039
8040 #define BINARY_OP(op) \
8041 if (strncmp (sym, #op, strlen (#op)) == 0) \
8042 { \
8043 sym += strlen (#op); \
8044 if (*sym == ':') \
8045 ++sym; \
8046 *symp = sym; \
8047 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8048 isymbuf, locsymcount, signed_p)) \
8049 return FALSE; \
8050 ++*symp; \
8051 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8052 isymbuf, locsymcount, signed_p)) \
8053 return FALSE; \
8054 if (signed_p) \
8055 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8056 else \
8057 *result = a op b; \
8058 return TRUE; \
8059 }
8060
8061 default:
8062 UNARY_OP (0-);
8063 BINARY_OP (<<);
8064 BINARY_OP (>>);
8065 BINARY_OP (==);
8066 BINARY_OP (!=);
8067 BINARY_OP (<=);
8068 BINARY_OP (>=);
8069 BINARY_OP (&&);
8070 BINARY_OP (||);
8071 UNARY_OP (~);
8072 UNARY_OP (!);
8073 BINARY_OP (*);
8074 BINARY_OP (/);
8075 BINARY_OP (%);
8076 BINARY_OP (^);
8077 BINARY_OP (|);
8078 BINARY_OP (&);
8079 BINARY_OP (+);
8080 BINARY_OP (-);
8081 BINARY_OP (<);
8082 BINARY_OP (>);
8083 #undef UNARY_OP
8084 #undef BINARY_OP
8085 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8086 bfd_set_error (bfd_error_invalid_operation);
8087 return FALSE;
8088 }
8089 }
8090
8091 static void
8092 put_value (bfd_vma size,
8093 unsigned long chunksz,
8094 bfd *input_bfd,
8095 bfd_vma x,
8096 bfd_byte *location)
8097 {
8098 location += (size - chunksz);
8099
8100 for (; size; size -= chunksz, location -= chunksz)
8101 {
8102 switch (chunksz)
8103 {
8104 case 1:
8105 bfd_put_8 (input_bfd, x, location);
8106 x >>= 8;
8107 break;
8108 case 2:
8109 bfd_put_16 (input_bfd, x, location);
8110 x >>= 16;
8111 break;
8112 case 4:
8113 bfd_put_32 (input_bfd, x, location);
8114 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8115 x >>= 16;
8116 x >>= 16;
8117 break;
8118 #ifdef BFD64
8119 case 8:
8120 bfd_put_64 (input_bfd, x, location);
8121 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8122 x >>= 32;
8123 x >>= 32;
8124 break;
8125 #endif
8126 default:
8127 abort ();
8128 break;
8129 }
8130 }
8131 }
8132
8133 static bfd_vma
8134 get_value (bfd_vma size,
8135 unsigned long chunksz,
8136 bfd *input_bfd,
8137 bfd_byte *location)
8138 {
8139 int shift;
8140 bfd_vma x = 0;
8141
8142 /* Sanity checks. */
8143 BFD_ASSERT (chunksz <= sizeof (x)
8144 && size >= chunksz
8145 && chunksz != 0
8146 && (size % chunksz) == 0
8147 && input_bfd != NULL
8148 && location != NULL);
8149
8150 if (chunksz == sizeof (x))
8151 {
8152 BFD_ASSERT (size == chunksz);
8153
8154 /* Make sure that we do not perform an undefined shift operation.
8155 We know that size == chunksz so there will only be one iteration
8156 of the loop below. */
8157 shift = 0;
8158 }
8159 else
8160 shift = 8 * chunksz;
8161
8162 for (; size; size -= chunksz, location += chunksz)
8163 {
8164 switch (chunksz)
8165 {
8166 case 1:
8167 x = (x << shift) | bfd_get_8 (input_bfd, location);
8168 break;
8169 case 2:
8170 x = (x << shift) | bfd_get_16 (input_bfd, location);
8171 break;
8172 case 4:
8173 x = (x << shift) | bfd_get_32 (input_bfd, location);
8174 break;
8175 #ifdef BFD64
8176 case 8:
8177 x = (x << shift) | bfd_get_64 (input_bfd, location);
8178 break;
8179 #endif
8180 default:
8181 abort ();
8182 }
8183 }
8184 return x;
8185 }
8186
8187 static void
8188 decode_complex_addend (unsigned long *start, /* in bits */
8189 unsigned long *oplen, /* in bits */
8190 unsigned long *len, /* in bits */
8191 unsigned long *wordsz, /* in bytes */
8192 unsigned long *chunksz, /* in bytes */
8193 unsigned long *lsb0_p,
8194 unsigned long *signed_p,
8195 unsigned long *trunc_p,
8196 unsigned long encoded)
8197 {
8198 * start = encoded & 0x3F;
8199 * len = (encoded >> 6) & 0x3F;
8200 * oplen = (encoded >> 12) & 0x3F;
8201 * wordsz = (encoded >> 18) & 0xF;
8202 * chunksz = (encoded >> 22) & 0xF;
8203 * lsb0_p = (encoded >> 27) & 1;
8204 * signed_p = (encoded >> 28) & 1;
8205 * trunc_p = (encoded >> 29) & 1;
8206 }
8207
8208 bfd_reloc_status_type
8209 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8210 asection *input_section ATTRIBUTE_UNUSED,
8211 bfd_byte *contents,
8212 Elf_Internal_Rela *rel,
8213 bfd_vma relocation)
8214 {
8215 bfd_vma shift, x, mask;
8216 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8217 bfd_reloc_status_type r;
8218
8219 /* Perform this reloc, since it is complex.
8220 (this is not to say that it necessarily refers to a complex
8221 symbol; merely that it is a self-describing CGEN based reloc.
8222 i.e. the addend has the complete reloc information (bit start, end,
8223 word size, etc) encoded within it.). */
8224
8225 decode_complex_addend (&start, &oplen, &len, &wordsz,
8226 &chunksz, &lsb0_p, &signed_p,
8227 &trunc_p, rel->r_addend);
8228
8229 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8230
8231 if (lsb0_p)
8232 shift = (start + 1) - len;
8233 else
8234 shift = (8 * wordsz) - (start + len);
8235
8236 x = get_value (wordsz, chunksz, input_bfd,
8237 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8238
8239 #ifdef DEBUG
8240 printf ("Doing complex reloc: "
8241 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8242 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8243 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8244 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8245 oplen, (unsigned long) x, (unsigned long) mask,
8246 (unsigned long) relocation);
8247 #endif
8248
8249 r = bfd_reloc_ok;
8250 if (! trunc_p)
8251 /* Now do an overflow check. */
8252 r = bfd_check_overflow ((signed_p
8253 ? complain_overflow_signed
8254 : complain_overflow_unsigned),
8255 len, 0, (8 * wordsz),
8256 relocation);
8257
8258 /* Do the deed. */
8259 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8260
8261 #ifdef DEBUG
8262 printf (" relocation: %8.8lx\n"
8263 " shifted mask: %8.8lx\n"
8264 " shifted/masked reloc: %8.8lx\n"
8265 " result: %8.8lx\n",
8266 (unsigned long) relocation, (unsigned long) (mask << shift),
8267 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8268 #endif
8269 put_value (wordsz, chunksz, input_bfd, x,
8270 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8271 return r;
8272 }
8273
8274 /* Functions to read r_offset from external (target order) reloc
8275 entry. Faster than bfd_getl32 et al, because we let the compiler
8276 know the value is aligned. */
8277
8278 static bfd_vma
8279 ext32l_r_offset (const void *p)
8280 {
8281 union aligned32
8282 {
8283 uint32_t v;
8284 unsigned char c[4];
8285 };
8286 const union aligned32 *a
8287 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8288
8289 uint32_t aval = ( (uint32_t) a->c[0]
8290 | (uint32_t) a->c[1] << 8
8291 | (uint32_t) a->c[2] << 16
8292 | (uint32_t) a->c[3] << 24);
8293 return aval;
8294 }
8295
8296 static bfd_vma
8297 ext32b_r_offset (const void *p)
8298 {
8299 union aligned32
8300 {
8301 uint32_t v;
8302 unsigned char c[4];
8303 };
8304 const union aligned32 *a
8305 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8306
8307 uint32_t aval = ( (uint32_t) a->c[0] << 24
8308 | (uint32_t) a->c[1] << 16
8309 | (uint32_t) a->c[2] << 8
8310 | (uint32_t) a->c[3]);
8311 return aval;
8312 }
8313
8314 #ifdef BFD_HOST_64_BIT
8315 static bfd_vma
8316 ext64l_r_offset (const void *p)
8317 {
8318 union aligned64
8319 {
8320 uint64_t v;
8321 unsigned char c[8];
8322 };
8323 const union aligned64 *a
8324 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8325
8326 uint64_t aval = ( (uint64_t) a->c[0]
8327 | (uint64_t) a->c[1] << 8
8328 | (uint64_t) a->c[2] << 16
8329 | (uint64_t) a->c[3] << 24
8330 | (uint64_t) a->c[4] << 32
8331 | (uint64_t) a->c[5] << 40
8332 | (uint64_t) a->c[6] << 48
8333 | (uint64_t) a->c[7] << 56);
8334 return aval;
8335 }
8336
8337 static bfd_vma
8338 ext64b_r_offset (const void *p)
8339 {
8340 union aligned64
8341 {
8342 uint64_t v;
8343 unsigned char c[8];
8344 };
8345 const union aligned64 *a
8346 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8347
8348 uint64_t aval = ( (uint64_t) a->c[0] << 56
8349 | (uint64_t) a->c[1] << 48
8350 | (uint64_t) a->c[2] << 40
8351 | (uint64_t) a->c[3] << 32
8352 | (uint64_t) a->c[4] << 24
8353 | (uint64_t) a->c[5] << 16
8354 | (uint64_t) a->c[6] << 8
8355 | (uint64_t) a->c[7]);
8356 return aval;
8357 }
8358 #endif
8359
8360 /* When performing a relocatable link, the input relocations are
8361 preserved. But, if they reference global symbols, the indices
8362 referenced must be updated. Update all the relocations found in
8363 RELDATA. */
8364
8365 static bfd_boolean
8366 elf_link_adjust_relocs (bfd *abfd,
8367 asection *sec,
8368 struct bfd_elf_section_reloc_data *reldata,
8369 bfd_boolean sort)
8370 {
8371 unsigned int i;
8372 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8373 bfd_byte *erela;
8374 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8375 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8376 bfd_vma r_type_mask;
8377 int r_sym_shift;
8378 unsigned int count = reldata->count;
8379 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8380
8381 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8382 {
8383 swap_in = bed->s->swap_reloc_in;
8384 swap_out = bed->s->swap_reloc_out;
8385 }
8386 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8387 {
8388 swap_in = bed->s->swap_reloca_in;
8389 swap_out = bed->s->swap_reloca_out;
8390 }
8391 else
8392 abort ();
8393
8394 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8395 abort ();
8396
8397 if (bed->s->arch_size == 32)
8398 {
8399 r_type_mask = 0xff;
8400 r_sym_shift = 8;
8401 }
8402 else
8403 {
8404 r_type_mask = 0xffffffff;
8405 r_sym_shift = 32;
8406 }
8407
8408 erela = reldata->hdr->contents;
8409 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8410 {
8411 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8412 unsigned int j;
8413
8414 if (*rel_hash == NULL)
8415 continue;
8416
8417 BFD_ASSERT ((*rel_hash)->indx >= 0);
8418
8419 (*swap_in) (abfd, erela, irela);
8420 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8421 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8422 | (irela[j].r_info & r_type_mask));
8423 (*swap_out) (abfd, irela, erela);
8424 }
8425
8426 if (bed->elf_backend_update_relocs)
8427 (*bed->elf_backend_update_relocs) (sec, reldata);
8428
8429 if (sort && count != 0)
8430 {
8431 bfd_vma (*ext_r_off) (const void *);
8432 bfd_vma r_off;
8433 size_t elt_size;
8434 bfd_byte *base, *end, *p, *loc;
8435 bfd_byte *buf = NULL;
8436
8437 if (bed->s->arch_size == 32)
8438 {
8439 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8440 ext_r_off = ext32l_r_offset;
8441 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8442 ext_r_off = ext32b_r_offset;
8443 else
8444 abort ();
8445 }
8446 else
8447 {
8448 #ifdef BFD_HOST_64_BIT
8449 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8450 ext_r_off = ext64l_r_offset;
8451 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8452 ext_r_off = ext64b_r_offset;
8453 else
8454 #endif
8455 abort ();
8456 }
8457
8458 /* Must use a stable sort here. A modified insertion sort,
8459 since the relocs are mostly sorted already. */
8460 elt_size = reldata->hdr->sh_entsize;
8461 base = reldata->hdr->contents;
8462 end = base + count * elt_size;
8463 if (elt_size > sizeof (Elf64_External_Rela))
8464 abort ();
8465
8466 /* Ensure the first element is lowest. This acts as a sentinel,
8467 speeding the main loop below. */
8468 r_off = (*ext_r_off) (base);
8469 for (p = loc = base; (p += elt_size) < end; )
8470 {
8471 bfd_vma r_off2 = (*ext_r_off) (p);
8472 if (r_off > r_off2)
8473 {
8474 r_off = r_off2;
8475 loc = p;
8476 }
8477 }
8478 if (loc != base)
8479 {
8480 /* Don't just swap *base and *loc as that changes the order
8481 of the original base[0] and base[1] if they happen to
8482 have the same r_offset. */
8483 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8484 memcpy (onebuf, loc, elt_size);
8485 memmove (base + elt_size, base, loc - base);
8486 memcpy (base, onebuf, elt_size);
8487 }
8488
8489 for (p = base + elt_size; (p += elt_size) < end; )
8490 {
8491 /* base to p is sorted, *p is next to insert. */
8492 r_off = (*ext_r_off) (p);
8493 /* Search the sorted region for location to insert. */
8494 loc = p - elt_size;
8495 while (r_off < (*ext_r_off) (loc))
8496 loc -= elt_size;
8497 loc += elt_size;
8498 if (loc != p)
8499 {
8500 /* Chances are there is a run of relocs to insert here,
8501 from one of more input files. Files are not always
8502 linked in order due to the way elf_link_input_bfd is
8503 called. See pr17666. */
8504 size_t sortlen = p - loc;
8505 bfd_vma r_off2 = (*ext_r_off) (loc);
8506 size_t runlen = elt_size;
8507 size_t buf_size = 96 * 1024;
8508 while (p + runlen < end
8509 && (sortlen <= buf_size
8510 || runlen + elt_size <= buf_size)
8511 && r_off2 > (*ext_r_off) (p + runlen))
8512 runlen += elt_size;
8513 if (buf == NULL)
8514 {
8515 buf = bfd_malloc (buf_size);
8516 if (buf == NULL)
8517 return FALSE;
8518 }
8519 if (runlen < sortlen)
8520 {
8521 memcpy (buf, p, runlen);
8522 memmove (loc + runlen, loc, sortlen);
8523 memcpy (loc, buf, runlen);
8524 }
8525 else
8526 {
8527 memcpy (buf, loc, sortlen);
8528 memmove (loc, p, runlen);
8529 memcpy (loc + runlen, buf, sortlen);
8530 }
8531 p += runlen - elt_size;
8532 }
8533 }
8534 /* Hashes are no longer valid. */
8535 free (reldata->hashes);
8536 reldata->hashes = NULL;
8537 free (buf);
8538 }
8539 return TRUE;
8540 }
8541
8542 struct elf_link_sort_rela
8543 {
8544 union {
8545 bfd_vma offset;
8546 bfd_vma sym_mask;
8547 } u;
8548 enum elf_reloc_type_class type;
8549 /* We use this as an array of size int_rels_per_ext_rel. */
8550 Elf_Internal_Rela rela[1];
8551 };
8552
8553 static int
8554 elf_link_sort_cmp1 (const void *A, const void *B)
8555 {
8556 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8557 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8558 int relativea, relativeb;
8559
8560 relativea = a->type == reloc_class_relative;
8561 relativeb = b->type == reloc_class_relative;
8562
8563 if (relativea < relativeb)
8564 return 1;
8565 if (relativea > relativeb)
8566 return -1;
8567 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8568 return -1;
8569 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8570 return 1;
8571 if (a->rela->r_offset < b->rela->r_offset)
8572 return -1;
8573 if (a->rela->r_offset > b->rela->r_offset)
8574 return 1;
8575 return 0;
8576 }
8577
8578 static int
8579 elf_link_sort_cmp2 (const void *A, const void *B)
8580 {
8581 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8582 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8583
8584 if (a->type < b->type)
8585 return -1;
8586 if (a->type > b->type)
8587 return 1;
8588 if (a->u.offset < b->u.offset)
8589 return -1;
8590 if (a->u.offset > b->u.offset)
8591 return 1;
8592 if (a->rela->r_offset < b->rela->r_offset)
8593 return -1;
8594 if (a->rela->r_offset > b->rela->r_offset)
8595 return 1;
8596 return 0;
8597 }
8598
8599 static size_t
8600 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8601 {
8602 asection *dynamic_relocs;
8603 asection *rela_dyn;
8604 asection *rel_dyn;
8605 bfd_size_type count, size;
8606 size_t i, ret, sort_elt, ext_size;
8607 bfd_byte *sort, *s_non_relative, *p;
8608 struct elf_link_sort_rela *sq;
8609 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8610 int i2e = bed->s->int_rels_per_ext_rel;
8611 unsigned int opb = bfd_octets_per_byte (abfd);
8612 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8613 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8614 struct bfd_link_order *lo;
8615 bfd_vma r_sym_mask;
8616 bfd_boolean use_rela;
8617
8618 /* Find a dynamic reloc section. */
8619 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8620 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8621 if (rela_dyn != NULL && rela_dyn->size > 0
8622 && rel_dyn != NULL && rel_dyn->size > 0)
8623 {
8624 bfd_boolean use_rela_initialised = FALSE;
8625
8626 /* This is just here to stop gcc from complaining.
8627 Its initialization checking code is not perfect. */
8628 use_rela = TRUE;
8629
8630 /* Both sections are present. Examine the sizes
8631 of the indirect sections to help us choose. */
8632 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8633 if (lo->type == bfd_indirect_link_order)
8634 {
8635 asection *o = lo->u.indirect.section;
8636
8637 if ((o->size % bed->s->sizeof_rela) == 0)
8638 {
8639 if ((o->size % bed->s->sizeof_rel) == 0)
8640 /* Section size is divisible by both rel and rela sizes.
8641 It is of no help to us. */
8642 ;
8643 else
8644 {
8645 /* Section size is only divisible by rela. */
8646 if (use_rela_initialised && (use_rela == FALSE))
8647 {
8648 _bfd_error_handler (_("%B: Unable to sort relocs - "
8649 "they are in more than one size"),
8650 abfd);
8651 bfd_set_error (bfd_error_invalid_operation);
8652 return 0;
8653 }
8654 else
8655 {
8656 use_rela = TRUE;
8657 use_rela_initialised = TRUE;
8658 }
8659 }
8660 }
8661 else if ((o->size % bed->s->sizeof_rel) == 0)
8662 {
8663 /* Section size is only divisible by rel. */
8664 if (use_rela_initialised && (use_rela == TRUE))
8665 {
8666 _bfd_error_handler (_("%B: Unable to sort relocs - "
8667 "they are in more than one size"),
8668 abfd);
8669 bfd_set_error (bfd_error_invalid_operation);
8670 return 0;
8671 }
8672 else
8673 {
8674 use_rela = FALSE;
8675 use_rela_initialised = TRUE;
8676 }
8677 }
8678 else
8679 {
8680 /* The section size is not divisible by either -
8681 something is wrong. */
8682 _bfd_error_handler (_("%B: Unable to sort relocs - "
8683 "they are of an unknown size"), abfd);
8684 bfd_set_error (bfd_error_invalid_operation);
8685 return 0;
8686 }
8687 }
8688
8689 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8690 if (lo->type == bfd_indirect_link_order)
8691 {
8692 asection *o = lo->u.indirect.section;
8693
8694 if ((o->size % bed->s->sizeof_rela) == 0)
8695 {
8696 if ((o->size % bed->s->sizeof_rel) == 0)
8697 /* Section size is divisible by both rel and rela sizes.
8698 It is of no help to us. */
8699 ;
8700 else
8701 {
8702 /* Section size is only divisible by rela. */
8703 if (use_rela_initialised && (use_rela == FALSE))
8704 {
8705 _bfd_error_handler (_("%B: Unable to sort relocs - "
8706 "they are in more than one size"),
8707 abfd);
8708 bfd_set_error (bfd_error_invalid_operation);
8709 return 0;
8710 }
8711 else
8712 {
8713 use_rela = TRUE;
8714 use_rela_initialised = TRUE;
8715 }
8716 }
8717 }
8718 else if ((o->size % bed->s->sizeof_rel) == 0)
8719 {
8720 /* Section size is only divisible by rel. */
8721 if (use_rela_initialised && (use_rela == TRUE))
8722 {
8723 _bfd_error_handler (_("%B: Unable to sort relocs - "
8724 "they are in more than one size"),
8725 abfd);
8726 bfd_set_error (bfd_error_invalid_operation);
8727 return 0;
8728 }
8729 else
8730 {
8731 use_rela = FALSE;
8732 use_rela_initialised = TRUE;
8733 }
8734 }
8735 else
8736 {
8737 /* The section size is not divisible by either -
8738 something is wrong. */
8739 _bfd_error_handler (_("%B: Unable to sort relocs - "
8740 "they are of an unknown size"), abfd);
8741 bfd_set_error (bfd_error_invalid_operation);
8742 return 0;
8743 }
8744 }
8745
8746 if (! use_rela_initialised)
8747 /* Make a guess. */
8748 use_rela = TRUE;
8749 }
8750 else if (rela_dyn != NULL && rela_dyn->size > 0)
8751 use_rela = TRUE;
8752 else if (rel_dyn != NULL && rel_dyn->size > 0)
8753 use_rela = FALSE;
8754 else
8755 return 0;
8756
8757 if (use_rela)
8758 {
8759 dynamic_relocs = rela_dyn;
8760 ext_size = bed->s->sizeof_rela;
8761 swap_in = bed->s->swap_reloca_in;
8762 swap_out = bed->s->swap_reloca_out;
8763 }
8764 else
8765 {
8766 dynamic_relocs = rel_dyn;
8767 ext_size = bed->s->sizeof_rel;
8768 swap_in = bed->s->swap_reloc_in;
8769 swap_out = bed->s->swap_reloc_out;
8770 }
8771
8772 size = 0;
8773 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8774 if (lo->type == bfd_indirect_link_order)
8775 size += lo->u.indirect.section->size;
8776
8777 if (size != dynamic_relocs->size)
8778 return 0;
8779
8780 sort_elt = (sizeof (struct elf_link_sort_rela)
8781 + (i2e - 1) * sizeof (Elf_Internal_Rela));
8782
8783 count = dynamic_relocs->size / ext_size;
8784 if (count == 0)
8785 return 0;
8786 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
8787
8788 if (sort == NULL)
8789 {
8790 (*info->callbacks->warning)
8791 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8792 return 0;
8793 }
8794
8795 if (bed->s->arch_size == 32)
8796 r_sym_mask = ~(bfd_vma) 0xff;
8797 else
8798 r_sym_mask = ~(bfd_vma) 0xffffffff;
8799
8800 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8801 if (lo->type == bfd_indirect_link_order)
8802 {
8803 bfd_byte *erel, *erelend;
8804 asection *o = lo->u.indirect.section;
8805
8806 if (o->contents == NULL && o->size != 0)
8807 {
8808 /* This is a reloc section that is being handled as a normal
8809 section. See bfd_section_from_shdr. We can't combine
8810 relocs in this case. */
8811 free (sort);
8812 return 0;
8813 }
8814 erel = o->contents;
8815 erelend = o->contents + o->size;
8816 p = sort + o->output_offset * opb / ext_size * sort_elt;
8817
8818 while (erel < erelend)
8819 {
8820 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8821
8822 (*swap_in) (abfd, erel, s->rela);
8823 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
8824 s->u.sym_mask = r_sym_mask;
8825 p += sort_elt;
8826 erel += ext_size;
8827 }
8828 }
8829
8830 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8831
8832 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8833 {
8834 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8835 if (s->type != reloc_class_relative)
8836 break;
8837 }
8838 ret = i;
8839 s_non_relative = p;
8840
8841 sq = (struct elf_link_sort_rela *) s_non_relative;
8842 for (; i < count; i++, p += sort_elt)
8843 {
8844 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8845 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8846 sq = sp;
8847 sp->u.offset = sq->rela->r_offset;
8848 }
8849
8850 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8851
8852 struct elf_link_hash_table *htab = elf_hash_table (info);
8853 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
8854 {
8855 /* We have plt relocs in .rela.dyn. */
8856 sq = (struct elf_link_sort_rela *) sort;
8857 for (i = 0; i < count; i++)
8858 if (sq[count - i - 1].type != reloc_class_plt)
8859 break;
8860 if (i != 0 && htab->srelplt->size == i * ext_size)
8861 {
8862 struct bfd_link_order **plo;
8863 /* Put srelplt link_order last. This is so the output_offset
8864 set in the next loop is correct for DT_JMPREL. */
8865 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
8866 if ((*plo)->type == bfd_indirect_link_order
8867 && (*plo)->u.indirect.section == htab->srelplt)
8868 {
8869 lo = *plo;
8870 *plo = lo->next;
8871 }
8872 else
8873 plo = &(*plo)->next;
8874 *plo = lo;
8875 lo->next = NULL;
8876 dynamic_relocs->map_tail.link_order = lo;
8877 }
8878 }
8879
8880 p = sort;
8881 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8882 if (lo->type == bfd_indirect_link_order)
8883 {
8884 bfd_byte *erel, *erelend;
8885 asection *o = lo->u.indirect.section;
8886
8887 erel = o->contents;
8888 erelend = o->contents + o->size;
8889 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
8890 while (erel < erelend)
8891 {
8892 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8893 (*swap_out) (abfd, s->rela, erel);
8894 p += sort_elt;
8895 erel += ext_size;
8896 }
8897 }
8898
8899 free (sort);
8900 *psec = dynamic_relocs;
8901 return ret;
8902 }
8903
8904 /* Add a symbol to the output symbol string table. */
8905
8906 static int
8907 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
8908 const char *name,
8909 Elf_Internal_Sym *elfsym,
8910 asection *input_sec,
8911 struct elf_link_hash_entry *h)
8912 {
8913 int (*output_symbol_hook)
8914 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8915 struct elf_link_hash_entry *);
8916 struct elf_link_hash_table *hash_table;
8917 const struct elf_backend_data *bed;
8918 bfd_size_type strtabsize;
8919
8920 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8921
8922 bed = get_elf_backend_data (flinfo->output_bfd);
8923 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8924 if (output_symbol_hook != NULL)
8925 {
8926 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
8927 if (ret != 1)
8928 return ret;
8929 }
8930
8931 if (name == NULL
8932 || *name == '\0'
8933 || (input_sec->flags & SEC_EXCLUDE))
8934 elfsym->st_name = (unsigned long) -1;
8935 else
8936 {
8937 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8938 to get the final offset for st_name. */
8939 elfsym->st_name
8940 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
8941 name, FALSE);
8942 if (elfsym->st_name == (unsigned long) -1)
8943 return 0;
8944 }
8945
8946 hash_table = elf_hash_table (flinfo->info);
8947 strtabsize = hash_table->strtabsize;
8948 if (strtabsize <= hash_table->strtabcount)
8949 {
8950 strtabsize += strtabsize;
8951 hash_table->strtabsize = strtabsize;
8952 strtabsize *= sizeof (*hash_table->strtab);
8953 hash_table->strtab
8954 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
8955 strtabsize);
8956 if (hash_table->strtab == NULL)
8957 return 0;
8958 }
8959 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
8960 hash_table->strtab[hash_table->strtabcount].dest_index
8961 = hash_table->strtabcount;
8962 hash_table->strtab[hash_table->strtabcount].destshndx_index
8963 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
8964
8965 bfd_get_symcount (flinfo->output_bfd) += 1;
8966 hash_table->strtabcount += 1;
8967
8968 return 1;
8969 }
8970
8971 /* Swap symbols out to the symbol table and flush the output symbols to
8972 the file. */
8973
8974 static bfd_boolean
8975 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
8976 {
8977 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
8978 bfd_size_type amt;
8979 size_t i;
8980 const struct elf_backend_data *bed;
8981 bfd_byte *symbuf;
8982 Elf_Internal_Shdr *hdr;
8983 file_ptr pos;
8984 bfd_boolean ret;
8985
8986 if (!hash_table->strtabcount)
8987 return TRUE;
8988
8989 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8990
8991 bed = get_elf_backend_data (flinfo->output_bfd);
8992
8993 amt = bed->s->sizeof_sym * hash_table->strtabcount;
8994 symbuf = (bfd_byte *) bfd_malloc (amt);
8995 if (symbuf == NULL)
8996 return FALSE;
8997
8998 if (flinfo->symshndxbuf)
8999 {
9000 amt = sizeof (Elf_External_Sym_Shndx);
9001 amt *= bfd_get_symcount (flinfo->output_bfd);
9002 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9003 if (flinfo->symshndxbuf == NULL)
9004 {
9005 free (symbuf);
9006 return FALSE;
9007 }
9008 }
9009
9010 for (i = 0; i < hash_table->strtabcount; i++)
9011 {
9012 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9013 if (elfsym->sym.st_name == (unsigned long) -1)
9014 elfsym->sym.st_name = 0;
9015 else
9016 elfsym->sym.st_name
9017 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9018 elfsym->sym.st_name);
9019 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9020 ((bfd_byte *) symbuf
9021 + (elfsym->dest_index
9022 * bed->s->sizeof_sym)),
9023 (flinfo->symshndxbuf
9024 + elfsym->destshndx_index));
9025 }
9026
9027 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9028 pos = hdr->sh_offset + hdr->sh_size;
9029 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9030 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9031 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9032 {
9033 hdr->sh_size += amt;
9034 ret = TRUE;
9035 }
9036 else
9037 ret = FALSE;
9038
9039 free (symbuf);
9040
9041 free (hash_table->strtab);
9042 hash_table->strtab = NULL;
9043
9044 return ret;
9045 }
9046
9047 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9048
9049 static bfd_boolean
9050 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9051 {
9052 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9053 && sym->st_shndx < SHN_LORESERVE)
9054 {
9055 /* The gABI doesn't support dynamic symbols in output sections
9056 beyond 64k. */
9057 _bfd_error_handler
9058 /* xgettext:c-format */
9059 (_("%B: Too many sections: %d (>= %d)"),
9060 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9061 bfd_set_error (bfd_error_nonrepresentable_section);
9062 return FALSE;
9063 }
9064 return TRUE;
9065 }
9066
9067 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9068 allowing an unsatisfied unversioned symbol in the DSO to match a
9069 versioned symbol that would normally require an explicit version.
9070 We also handle the case that a DSO references a hidden symbol
9071 which may be satisfied by a versioned symbol in another DSO. */
9072
9073 static bfd_boolean
9074 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9075 const struct elf_backend_data *bed,
9076 struct elf_link_hash_entry *h)
9077 {
9078 bfd *abfd;
9079 struct elf_link_loaded_list *loaded;
9080
9081 if (!is_elf_hash_table (info->hash))
9082 return FALSE;
9083
9084 /* Check indirect symbol. */
9085 while (h->root.type == bfd_link_hash_indirect)
9086 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9087
9088 switch (h->root.type)
9089 {
9090 default:
9091 abfd = NULL;
9092 break;
9093
9094 case bfd_link_hash_undefined:
9095 case bfd_link_hash_undefweak:
9096 abfd = h->root.u.undef.abfd;
9097 if (abfd == NULL
9098 || (abfd->flags & DYNAMIC) == 0
9099 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9100 return FALSE;
9101 break;
9102
9103 case bfd_link_hash_defined:
9104 case bfd_link_hash_defweak:
9105 abfd = h->root.u.def.section->owner;
9106 break;
9107
9108 case bfd_link_hash_common:
9109 abfd = h->root.u.c.p->section->owner;
9110 break;
9111 }
9112 BFD_ASSERT (abfd != NULL);
9113
9114 for (loaded = elf_hash_table (info)->loaded;
9115 loaded != NULL;
9116 loaded = loaded->next)
9117 {
9118 bfd *input;
9119 Elf_Internal_Shdr *hdr;
9120 size_t symcount;
9121 size_t extsymcount;
9122 size_t extsymoff;
9123 Elf_Internal_Shdr *versymhdr;
9124 Elf_Internal_Sym *isym;
9125 Elf_Internal_Sym *isymend;
9126 Elf_Internal_Sym *isymbuf;
9127 Elf_External_Versym *ever;
9128 Elf_External_Versym *extversym;
9129
9130 input = loaded->abfd;
9131
9132 /* We check each DSO for a possible hidden versioned definition. */
9133 if (input == abfd
9134 || (input->flags & DYNAMIC) == 0
9135 || elf_dynversym (input) == 0)
9136 continue;
9137
9138 hdr = &elf_tdata (input)->dynsymtab_hdr;
9139
9140 symcount = hdr->sh_size / bed->s->sizeof_sym;
9141 if (elf_bad_symtab (input))
9142 {
9143 extsymcount = symcount;
9144 extsymoff = 0;
9145 }
9146 else
9147 {
9148 extsymcount = symcount - hdr->sh_info;
9149 extsymoff = hdr->sh_info;
9150 }
9151
9152 if (extsymcount == 0)
9153 continue;
9154
9155 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9156 NULL, NULL, NULL);
9157 if (isymbuf == NULL)
9158 return FALSE;
9159
9160 /* Read in any version definitions. */
9161 versymhdr = &elf_tdata (input)->dynversym_hdr;
9162 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
9163 if (extversym == NULL)
9164 goto error_ret;
9165
9166 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9167 || (bfd_bread (extversym, versymhdr->sh_size, input)
9168 != versymhdr->sh_size))
9169 {
9170 free (extversym);
9171 error_ret:
9172 free (isymbuf);
9173 return FALSE;
9174 }
9175
9176 ever = extversym + extsymoff;
9177 isymend = isymbuf + extsymcount;
9178 for (isym = isymbuf; isym < isymend; isym++, ever++)
9179 {
9180 const char *name;
9181 Elf_Internal_Versym iver;
9182 unsigned short version_index;
9183
9184 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9185 || isym->st_shndx == SHN_UNDEF)
9186 continue;
9187
9188 name = bfd_elf_string_from_elf_section (input,
9189 hdr->sh_link,
9190 isym->st_name);
9191 if (strcmp (name, h->root.root.string) != 0)
9192 continue;
9193
9194 _bfd_elf_swap_versym_in (input, ever, &iver);
9195
9196 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9197 && !(h->def_regular
9198 && h->forced_local))
9199 {
9200 /* If we have a non-hidden versioned sym, then it should
9201 have provided a definition for the undefined sym unless
9202 it is defined in a non-shared object and forced local.
9203 */
9204 abort ();
9205 }
9206
9207 version_index = iver.vs_vers & VERSYM_VERSION;
9208 if (version_index == 1 || version_index == 2)
9209 {
9210 /* This is the base or first version. We can use it. */
9211 free (extversym);
9212 free (isymbuf);
9213 return TRUE;
9214 }
9215 }
9216
9217 free (extversym);
9218 free (isymbuf);
9219 }
9220
9221 return FALSE;
9222 }
9223
9224 /* Convert ELF common symbol TYPE. */
9225
9226 static int
9227 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9228 {
9229 /* Commom symbol can only appear in relocatable link. */
9230 if (!bfd_link_relocatable (info))
9231 abort ();
9232 switch (info->elf_stt_common)
9233 {
9234 case unchanged:
9235 break;
9236 case elf_stt_common:
9237 type = STT_COMMON;
9238 break;
9239 case no_elf_stt_common:
9240 type = STT_OBJECT;
9241 break;
9242 }
9243 return type;
9244 }
9245
9246 /* Add an external symbol to the symbol table. This is called from
9247 the hash table traversal routine. When generating a shared object,
9248 we go through the symbol table twice. The first time we output
9249 anything that might have been forced to local scope in a version
9250 script. The second time we output the symbols that are still
9251 global symbols. */
9252
9253 static bfd_boolean
9254 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9255 {
9256 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9257 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9258 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9259 bfd_boolean strip;
9260 Elf_Internal_Sym sym;
9261 asection *input_sec;
9262 const struct elf_backend_data *bed;
9263 long indx;
9264 int ret;
9265 unsigned int type;
9266
9267 if (h->root.type == bfd_link_hash_warning)
9268 {
9269 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9270 if (h->root.type == bfd_link_hash_new)
9271 return TRUE;
9272 }
9273
9274 /* Decide whether to output this symbol in this pass. */
9275 if (eoinfo->localsyms)
9276 {
9277 if (!h->forced_local)
9278 return TRUE;
9279 }
9280 else
9281 {
9282 if (h->forced_local)
9283 return TRUE;
9284 }
9285
9286 bed = get_elf_backend_data (flinfo->output_bfd);
9287
9288 if (h->root.type == bfd_link_hash_undefined)
9289 {
9290 /* If we have an undefined symbol reference here then it must have
9291 come from a shared library that is being linked in. (Undefined
9292 references in regular files have already been handled unless
9293 they are in unreferenced sections which are removed by garbage
9294 collection). */
9295 bfd_boolean ignore_undef = FALSE;
9296
9297 /* Some symbols may be special in that the fact that they're
9298 undefined can be safely ignored - let backend determine that. */
9299 if (bed->elf_backend_ignore_undef_symbol)
9300 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9301
9302 /* If we are reporting errors for this situation then do so now. */
9303 if (!ignore_undef
9304 && h->ref_dynamic
9305 && (!h->ref_regular || flinfo->info->gc_sections)
9306 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9307 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9308 (*flinfo->info->callbacks->undefined_symbol)
9309 (flinfo->info, h->root.root.string,
9310 h->ref_regular ? NULL : h->root.u.undef.abfd,
9311 NULL, 0,
9312 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
9313
9314 /* Strip a global symbol defined in a discarded section. */
9315 if (h->indx == -3)
9316 return TRUE;
9317 }
9318
9319 /* We should also warn if a forced local symbol is referenced from
9320 shared libraries. */
9321 if (bfd_link_executable (flinfo->info)
9322 && h->forced_local
9323 && h->ref_dynamic
9324 && h->def_regular
9325 && !h->dynamic_def
9326 && h->ref_dynamic_nonweak
9327 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9328 {
9329 bfd *def_bfd;
9330 const char *msg;
9331 struct elf_link_hash_entry *hi = h;
9332
9333 /* Check indirect symbol. */
9334 while (hi->root.type == bfd_link_hash_indirect)
9335 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9336
9337 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9338 /* xgettext:c-format */
9339 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9340 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9341 /* xgettext:c-format */
9342 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9343 else
9344 /* xgettext:c-format */
9345 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
9346 def_bfd = flinfo->output_bfd;
9347 if (hi->root.u.def.section != bfd_abs_section_ptr)
9348 def_bfd = hi->root.u.def.section->owner;
9349 _bfd_error_handler (msg, flinfo->output_bfd, def_bfd,
9350 h->root.root.string);
9351 bfd_set_error (bfd_error_bad_value);
9352 eoinfo->failed = TRUE;
9353 return FALSE;
9354 }
9355
9356 /* We don't want to output symbols that have never been mentioned by
9357 a regular file, or that we have been told to strip. However, if
9358 h->indx is set to -2, the symbol is used by a reloc and we must
9359 output it. */
9360 strip = FALSE;
9361 if (h->indx == -2)
9362 ;
9363 else if ((h->def_dynamic
9364 || h->ref_dynamic
9365 || h->root.type == bfd_link_hash_new)
9366 && !h->def_regular
9367 && !h->ref_regular)
9368 strip = TRUE;
9369 else if (flinfo->info->strip == strip_all)
9370 strip = TRUE;
9371 else if (flinfo->info->strip == strip_some
9372 && bfd_hash_lookup (flinfo->info->keep_hash,
9373 h->root.root.string, FALSE, FALSE) == NULL)
9374 strip = TRUE;
9375 else if ((h->root.type == bfd_link_hash_defined
9376 || h->root.type == bfd_link_hash_defweak)
9377 && ((flinfo->info->strip_discarded
9378 && discarded_section (h->root.u.def.section))
9379 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9380 && h->root.u.def.section->owner != NULL
9381 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9382 strip = TRUE;
9383 else if ((h->root.type == bfd_link_hash_undefined
9384 || h->root.type == bfd_link_hash_undefweak)
9385 && h->root.u.undef.abfd != NULL
9386 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9387 strip = TRUE;
9388
9389 type = h->type;
9390
9391 /* If we're stripping it, and it's not a dynamic symbol, there's
9392 nothing else to do. However, if it is a forced local symbol or
9393 an ifunc symbol we need to give the backend finish_dynamic_symbol
9394 function a chance to make it dynamic. */
9395 if (strip
9396 && h->dynindx == -1
9397 && type != STT_GNU_IFUNC
9398 && !h->forced_local)
9399 return TRUE;
9400
9401 sym.st_value = 0;
9402 sym.st_size = h->size;
9403 sym.st_other = h->other;
9404 switch (h->root.type)
9405 {
9406 default:
9407 case bfd_link_hash_new:
9408 case bfd_link_hash_warning:
9409 abort ();
9410 return FALSE;
9411
9412 case bfd_link_hash_undefined:
9413 case bfd_link_hash_undefweak:
9414 input_sec = bfd_und_section_ptr;
9415 sym.st_shndx = SHN_UNDEF;
9416 break;
9417
9418 case bfd_link_hash_defined:
9419 case bfd_link_hash_defweak:
9420 {
9421 input_sec = h->root.u.def.section;
9422 if (input_sec->output_section != NULL)
9423 {
9424 sym.st_shndx =
9425 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9426 input_sec->output_section);
9427 if (sym.st_shndx == SHN_BAD)
9428 {
9429 _bfd_error_handler
9430 /* xgettext:c-format */
9431 (_("%B: could not find output section %A for input section %A"),
9432 flinfo->output_bfd, input_sec->output_section, input_sec);
9433 bfd_set_error (bfd_error_nonrepresentable_section);
9434 eoinfo->failed = TRUE;
9435 return FALSE;
9436 }
9437
9438 /* ELF symbols in relocatable files are section relative,
9439 but in nonrelocatable files they are virtual
9440 addresses. */
9441 sym.st_value = h->root.u.def.value + input_sec->output_offset;
9442 if (!bfd_link_relocatable (flinfo->info))
9443 {
9444 sym.st_value += input_sec->output_section->vma;
9445 if (h->type == STT_TLS)
9446 {
9447 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9448 if (tls_sec != NULL)
9449 sym.st_value -= tls_sec->vma;
9450 }
9451 }
9452 }
9453 else
9454 {
9455 BFD_ASSERT (input_sec->owner == NULL
9456 || (input_sec->owner->flags & DYNAMIC) != 0);
9457 sym.st_shndx = SHN_UNDEF;
9458 input_sec = bfd_und_section_ptr;
9459 }
9460 }
9461 break;
9462
9463 case bfd_link_hash_common:
9464 input_sec = h->root.u.c.p->section;
9465 sym.st_shndx = bed->common_section_index (input_sec);
9466 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9467 break;
9468
9469 case bfd_link_hash_indirect:
9470 /* These symbols are created by symbol versioning. They point
9471 to the decorated version of the name. For example, if the
9472 symbol foo@@GNU_1.2 is the default, which should be used when
9473 foo is used with no version, then we add an indirect symbol
9474 foo which points to foo@@GNU_1.2. We ignore these symbols,
9475 since the indirected symbol is already in the hash table. */
9476 return TRUE;
9477 }
9478
9479 if (type == STT_COMMON || type == STT_OBJECT)
9480 switch (h->root.type)
9481 {
9482 case bfd_link_hash_common:
9483 type = elf_link_convert_common_type (flinfo->info, type);
9484 break;
9485 case bfd_link_hash_defined:
9486 case bfd_link_hash_defweak:
9487 if (bed->common_definition (&sym))
9488 type = elf_link_convert_common_type (flinfo->info, type);
9489 else
9490 type = STT_OBJECT;
9491 break;
9492 case bfd_link_hash_undefined:
9493 case bfd_link_hash_undefweak:
9494 break;
9495 default:
9496 abort ();
9497 }
9498
9499 if (h->forced_local)
9500 {
9501 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9502 /* Turn off visibility on local symbol. */
9503 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9504 }
9505 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9506 else if (h->unique_global && h->def_regular)
9507 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9508 else if (h->root.type == bfd_link_hash_undefweak
9509 || h->root.type == bfd_link_hash_defweak)
9510 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9511 else
9512 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9513 sym.st_target_internal = h->target_internal;
9514
9515 /* Give the processor backend a chance to tweak the symbol value,
9516 and also to finish up anything that needs to be done for this
9517 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9518 forced local syms when non-shared is due to a historical quirk.
9519 STT_GNU_IFUNC symbol must go through PLT. */
9520 if ((h->type == STT_GNU_IFUNC
9521 && h->def_regular
9522 && !bfd_link_relocatable (flinfo->info))
9523 || ((h->dynindx != -1
9524 || h->forced_local)
9525 && ((bfd_link_pic (flinfo->info)
9526 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9527 || h->root.type != bfd_link_hash_undefweak))
9528 || !h->forced_local)
9529 && elf_hash_table (flinfo->info)->dynamic_sections_created))
9530 {
9531 if (! ((*bed->elf_backend_finish_dynamic_symbol)
9532 (flinfo->output_bfd, flinfo->info, h, &sym)))
9533 {
9534 eoinfo->failed = TRUE;
9535 return FALSE;
9536 }
9537 }
9538
9539 /* If we are marking the symbol as undefined, and there are no
9540 non-weak references to this symbol from a regular object, then
9541 mark the symbol as weak undefined; if there are non-weak
9542 references, mark the symbol as strong. We can't do this earlier,
9543 because it might not be marked as undefined until the
9544 finish_dynamic_symbol routine gets through with it. */
9545 if (sym.st_shndx == SHN_UNDEF
9546 && h->ref_regular
9547 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9548 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9549 {
9550 int bindtype;
9551 type = ELF_ST_TYPE (sym.st_info);
9552
9553 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9554 if (type == STT_GNU_IFUNC)
9555 type = STT_FUNC;
9556
9557 if (h->ref_regular_nonweak)
9558 bindtype = STB_GLOBAL;
9559 else
9560 bindtype = STB_WEAK;
9561 sym.st_info = ELF_ST_INFO (bindtype, type);
9562 }
9563
9564 /* If this is a symbol defined in a dynamic library, don't use the
9565 symbol size from the dynamic library. Relinking an executable
9566 against a new library may introduce gratuitous changes in the
9567 executable's symbols if we keep the size. */
9568 if (sym.st_shndx == SHN_UNDEF
9569 && !h->def_regular
9570 && h->def_dynamic)
9571 sym.st_size = 0;
9572
9573 /* If a non-weak symbol with non-default visibility is not defined
9574 locally, it is a fatal error. */
9575 if (!bfd_link_relocatable (flinfo->info)
9576 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9577 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9578 && h->root.type == bfd_link_hash_undefined
9579 && !h->def_regular)
9580 {
9581 const char *msg;
9582
9583 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9584 /* xgettext:c-format */
9585 msg = _("%B: protected symbol `%s' isn't defined");
9586 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9587 /* xgettext:c-format */
9588 msg = _("%B: internal symbol `%s' isn't defined");
9589 else
9590 /* xgettext:c-format */
9591 msg = _("%B: hidden symbol `%s' isn't defined");
9592 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
9593 bfd_set_error (bfd_error_bad_value);
9594 eoinfo->failed = TRUE;
9595 return FALSE;
9596 }
9597
9598 /* If this symbol should be put in the .dynsym section, then put it
9599 there now. We already know the symbol index. We also fill in
9600 the entry in the .hash section. */
9601 if (elf_hash_table (flinfo->info)->dynsym != NULL
9602 && h->dynindx != -1
9603 && elf_hash_table (flinfo->info)->dynamic_sections_created)
9604 {
9605 bfd_byte *esym;
9606
9607 /* Since there is no version information in the dynamic string,
9608 if there is no version info in symbol version section, we will
9609 have a run-time problem if not linking executable, referenced
9610 by shared library, or not bound locally. */
9611 if (h->verinfo.verdef == NULL
9612 && (!bfd_link_executable (flinfo->info)
9613 || h->ref_dynamic
9614 || !h->def_regular))
9615 {
9616 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9617
9618 if (p && p [1] != '\0')
9619 {
9620 _bfd_error_handler
9621 /* xgettext:c-format */
9622 (_("%B: No symbol version section for versioned symbol `%s'"),
9623 flinfo->output_bfd, h->root.root.string);
9624 eoinfo->failed = TRUE;
9625 return FALSE;
9626 }
9627 }
9628
9629 sym.st_name = h->dynstr_index;
9630 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9631 + h->dynindx * bed->s->sizeof_sym);
9632 if (!check_dynsym (flinfo->output_bfd, &sym))
9633 {
9634 eoinfo->failed = TRUE;
9635 return FALSE;
9636 }
9637 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
9638
9639 if (flinfo->hash_sec != NULL)
9640 {
9641 size_t hash_entry_size;
9642 bfd_byte *bucketpos;
9643 bfd_vma chain;
9644 size_t bucketcount;
9645 size_t bucket;
9646
9647 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
9648 bucket = h->u.elf_hash_value % bucketcount;
9649
9650 hash_entry_size
9651 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9652 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
9653 + (bucket + 2) * hash_entry_size);
9654 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9655 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9656 bucketpos);
9657 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9658 ((bfd_byte *) flinfo->hash_sec->contents
9659 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9660 }
9661
9662 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
9663 {
9664 Elf_Internal_Versym iversym;
9665 Elf_External_Versym *eversym;
9666
9667 if (!h->def_regular)
9668 {
9669 if (h->verinfo.verdef == NULL
9670 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9671 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
9672 iversym.vs_vers = 0;
9673 else
9674 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9675 }
9676 else
9677 {
9678 if (h->verinfo.vertree == NULL)
9679 iversym.vs_vers = 1;
9680 else
9681 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
9682 if (flinfo->info->create_default_symver)
9683 iversym.vs_vers++;
9684 }
9685
9686 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9687 defined locally. */
9688 if (h->versioned == versioned_hidden && h->def_regular)
9689 iversym.vs_vers |= VERSYM_HIDDEN;
9690
9691 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
9692 eversym += h->dynindx;
9693 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
9694 }
9695 }
9696
9697 /* If the symbol is undefined, and we didn't output it to .dynsym,
9698 strip it from .symtab too. Obviously we can't do this for
9699 relocatable output or when needed for --emit-relocs. */
9700 else if (input_sec == bfd_und_section_ptr
9701 && h->indx != -2
9702 && !bfd_link_relocatable (flinfo->info))
9703 return TRUE;
9704 /* Also strip others that we couldn't earlier due to dynamic symbol
9705 processing. */
9706 if (strip)
9707 return TRUE;
9708 if ((input_sec->flags & SEC_EXCLUDE) != 0)
9709 return TRUE;
9710
9711 /* Output a FILE symbol so that following locals are not associated
9712 with the wrong input file. We need one for forced local symbols
9713 if we've seen more than one FILE symbol or when we have exactly
9714 one FILE symbol but global symbols are present in a file other
9715 than the one with the FILE symbol. We also need one if linker
9716 defined symbols are present. In practice these conditions are
9717 always met, so just emit the FILE symbol unconditionally. */
9718 if (eoinfo->localsyms
9719 && !eoinfo->file_sym_done
9720 && eoinfo->flinfo->filesym_count != 0)
9721 {
9722 Elf_Internal_Sym fsym;
9723
9724 memset (&fsym, 0, sizeof (fsym));
9725 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9726 fsym.st_shndx = SHN_ABS;
9727 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9728 bfd_und_section_ptr, NULL))
9729 return FALSE;
9730
9731 eoinfo->file_sym_done = TRUE;
9732 }
9733
9734 indx = bfd_get_symcount (flinfo->output_bfd);
9735 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9736 input_sec, h);
9737 if (ret == 0)
9738 {
9739 eoinfo->failed = TRUE;
9740 return FALSE;
9741 }
9742 else if (ret == 1)
9743 h->indx = indx;
9744 else if (h->indx == -2)
9745 abort();
9746
9747 return TRUE;
9748 }
9749
9750 /* Return TRUE if special handling is done for relocs in SEC against
9751 symbols defined in discarded sections. */
9752
9753 static bfd_boolean
9754 elf_section_ignore_discarded_relocs (asection *sec)
9755 {
9756 const struct elf_backend_data *bed;
9757
9758 switch (sec->sec_info_type)
9759 {
9760 case SEC_INFO_TYPE_STABS:
9761 case SEC_INFO_TYPE_EH_FRAME:
9762 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
9763 return TRUE;
9764 default:
9765 break;
9766 }
9767
9768 bed = get_elf_backend_data (sec->owner);
9769 if (bed->elf_backend_ignore_discarded_relocs != NULL
9770 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9771 return TRUE;
9772
9773 return FALSE;
9774 }
9775
9776 /* Return a mask saying how ld should treat relocations in SEC against
9777 symbols defined in discarded sections. If this function returns
9778 COMPLAIN set, ld will issue a warning message. If this function
9779 returns PRETEND set, and the discarded section was link-once and the
9780 same size as the kept link-once section, ld will pretend that the
9781 symbol was actually defined in the kept section. Otherwise ld will
9782 zero the reloc (at least that is the intent, but some cooperation by
9783 the target dependent code is needed, particularly for REL targets). */
9784
9785 unsigned int
9786 _bfd_elf_default_action_discarded (asection *sec)
9787 {
9788 if (sec->flags & SEC_DEBUGGING)
9789 return PRETEND;
9790
9791 if (strcmp (".eh_frame", sec->name) == 0)
9792 return 0;
9793
9794 if (strcmp (".gcc_except_table", sec->name) == 0)
9795 return 0;
9796
9797 return COMPLAIN | PRETEND;
9798 }
9799
9800 /* Find a match between a section and a member of a section group. */
9801
9802 static asection *
9803 match_group_member (asection *sec, asection *group,
9804 struct bfd_link_info *info)
9805 {
9806 asection *first = elf_next_in_group (group);
9807 asection *s = first;
9808
9809 while (s != NULL)
9810 {
9811 if (bfd_elf_match_symbols_in_sections (s, sec, info))
9812 return s;
9813
9814 s = elf_next_in_group (s);
9815 if (s == first)
9816 break;
9817 }
9818
9819 return NULL;
9820 }
9821
9822 /* Check if the kept section of a discarded section SEC can be used
9823 to replace it. Return the replacement if it is OK. Otherwise return
9824 NULL. */
9825
9826 asection *
9827 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
9828 {
9829 asection *kept;
9830
9831 kept = sec->kept_section;
9832 if (kept != NULL)
9833 {
9834 if ((kept->flags & SEC_GROUP) != 0)
9835 kept = match_group_member (sec, kept, info);
9836 if (kept != NULL
9837 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9838 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
9839 kept = NULL;
9840 sec->kept_section = kept;
9841 }
9842 return kept;
9843 }
9844
9845 /* Link an input file into the linker output file. This function
9846 handles all the sections and relocations of the input file at once.
9847 This is so that we only have to read the local symbols once, and
9848 don't have to keep them in memory. */
9849
9850 static bfd_boolean
9851 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
9852 {
9853 int (*relocate_section)
9854 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9855 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9856 bfd *output_bfd;
9857 Elf_Internal_Shdr *symtab_hdr;
9858 size_t locsymcount;
9859 size_t extsymoff;
9860 Elf_Internal_Sym *isymbuf;
9861 Elf_Internal_Sym *isym;
9862 Elf_Internal_Sym *isymend;
9863 long *pindex;
9864 asection **ppsection;
9865 asection *o;
9866 const struct elf_backend_data *bed;
9867 struct elf_link_hash_entry **sym_hashes;
9868 bfd_size_type address_size;
9869 bfd_vma r_type_mask;
9870 int r_sym_shift;
9871 bfd_boolean have_file_sym = FALSE;
9872
9873 output_bfd = flinfo->output_bfd;
9874 bed = get_elf_backend_data (output_bfd);
9875 relocate_section = bed->elf_backend_relocate_section;
9876
9877 /* If this is a dynamic object, we don't want to do anything here:
9878 we don't want the local symbols, and we don't want the section
9879 contents. */
9880 if ((input_bfd->flags & DYNAMIC) != 0)
9881 return TRUE;
9882
9883 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9884 if (elf_bad_symtab (input_bfd))
9885 {
9886 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9887 extsymoff = 0;
9888 }
9889 else
9890 {
9891 locsymcount = symtab_hdr->sh_info;
9892 extsymoff = symtab_hdr->sh_info;
9893 }
9894
9895 /* Read the local symbols. */
9896 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9897 if (isymbuf == NULL && locsymcount != 0)
9898 {
9899 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
9900 flinfo->internal_syms,
9901 flinfo->external_syms,
9902 flinfo->locsym_shndx);
9903 if (isymbuf == NULL)
9904 return FALSE;
9905 }
9906
9907 /* Find local symbol sections and adjust values of symbols in
9908 SEC_MERGE sections. Write out those local symbols we know are
9909 going into the output file. */
9910 isymend = isymbuf + locsymcount;
9911 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
9912 isym < isymend;
9913 isym++, pindex++, ppsection++)
9914 {
9915 asection *isec;
9916 const char *name;
9917 Elf_Internal_Sym osym;
9918 long indx;
9919 int ret;
9920
9921 *pindex = -1;
9922
9923 if (elf_bad_symtab (input_bfd))
9924 {
9925 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9926 {
9927 *ppsection = NULL;
9928 continue;
9929 }
9930 }
9931
9932 if (isym->st_shndx == SHN_UNDEF)
9933 isec = bfd_und_section_ptr;
9934 else if (isym->st_shndx == SHN_ABS)
9935 isec = bfd_abs_section_ptr;
9936 else if (isym->st_shndx == SHN_COMMON)
9937 isec = bfd_com_section_ptr;
9938 else
9939 {
9940 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9941 if (isec == NULL)
9942 {
9943 /* Don't attempt to output symbols with st_shnx in the
9944 reserved range other than SHN_ABS and SHN_COMMON. */
9945 *ppsection = NULL;
9946 continue;
9947 }
9948 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
9949 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9950 isym->st_value =
9951 _bfd_merged_section_offset (output_bfd, &isec,
9952 elf_section_data (isec)->sec_info,
9953 isym->st_value);
9954 }
9955
9956 *ppsection = isec;
9957
9958 /* Don't output the first, undefined, symbol. In fact, don't
9959 output any undefined local symbol. */
9960 if (isec == bfd_und_section_ptr)
9961 continue;
9962
9963 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9964 {
9965 /* We never output section symbols. Instead, we use the
9966 section symbol of the corresponding section in the output
9967 file. */
9968 continue;
9969 }
9970
9971 /* If we are stripping all symbols, we don't want to output this
9972 one. */
9973 if (flinfo->info->strip == strip_all)
9974 continue;
9975
9976 /* If we are discarding all local symbols, we don't want to
9977 output this one. If we are generating a relocatable output
9978 file, then some of the local symbols may be required by
9979 relocs; we output them below as we discover that they are
9980 needed. */
9981 if (flinfo->info->discard == discard_all)
9982 continue;
9983
9984 /* If this symbol is defined in a section which we are
9985 discarding, we don't need to keep it. */
9986 if (isym->st_shndx != SHN_UNDEF
9987 && isym->st_shndx < SHN_LORESERVE
9988 && bfd_section_removed_from_list (output_bfd,
9989 isec->output_section))
9990 continue;
9991
9992 /* Get the name of the symbol. */
9993 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9994 isym->st_name);
9995 if (name == NULL)
9996 return FALSE;
9997
9998 /* See if we are discarding symbols with this name. */
9999 if ((flinfo->info->strip == strip_some
10000 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
10001 == NULL))
10002 || (((flinfo->info->discard == discard_sec_merge
10003 && (isec->flags & SEC_MERGE)
10004 && !bfd_link_relocatable (flinfo->info))
10005 || flinfo->info->discard == discard_l)
10006 && bfd_is_local_label_name (input_bfd, name)))
10007 continue;
10008
10009 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10010 {
10011 if (input_bfd->lto_output)
10012 /* -flto puts a temp file name here. This means builds
10013 are not reproducible. Discard the symbol. */
10014 continue;
10015 have_file_sym = TRUE;
10016 flinfo->filesym_count += 1;
10017 }
10018 if (!have_file_sym)
10019 {
10020 /* In the absence of debug info, bfd_find_nearest_line uses
10021 FILE symbols to determine the source file for local
10022 function symbols. Provide a FILE symbol here if input
10023 files lack such, so that their symbols won't be
10024 associated with a previous input file. It's not the
10025 source file, but the best we can do. */
10026 have_file_sym = TRUE;
10027 flinfo->filesym_count += 1;
10028 memset (&osym, 0, sizeof (osym));
10029 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10030 osym.st_shndx = SHN_ABS;
10031 if (!elf_link_output_symstrtab (flinfo,
10032 (input_bfd->lto_output ? NULL
10033 : input_bfd->filename),
10034 &osym, bfd_abs_section_ptr,
10035 NULL))
10036 return FALSE;
10037 }
10038
10039 osym = *isym;
10040
10041 /* Adjust the section index for the output file. */
10042 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10043 isec->output_section);
10044 if (osym.st_shndx == SHN_BAD)
10045 return FALSE;
10046
10047 /* ELF symbols in relocatable files are section relative, but
10048 in executable files they are virtual addresses. Note that
10049 this code assumes that all ELF sections have an associated
10050 BFD section with a reasonable value for output_offset; below
10051 we assume that they also have a reasonable value for
10052 output_section. Any special sections must be set up to meet
10053 these requirements. */
10054 osym.st_value += isec->output_offset;
10055 if (!bfd_link_relocatable (flinfo->info))
10056 {
10057 osym.st_value += isec->output_section->vma;
10058 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10059 {
10060 /* STT_TLS symbols are relative to PT_TLS segment base. */
10061 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10062 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10063 }
10064 }
10065
10066 indx = bfd_get_symcount (output_bfd);
10067 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10068 if (ret == 0)
10069 return FALSE;
10070 else if (ret == 1)
10071 *pindex = indx;
10072 }
10073
10074 if (bed->s->arch_size == 32)
10075 {
10076 r_type_mask = 0xff;
10077 r_sym_shift = 8;
10078 address_size = 4;
10079 }
10080 else
10081 {
10082 r_type_mask = 0xffffffff;
10083 r_sym_shift = 32;
10084 address_size = 8;
10085 }
10086
10087 /* Relocate the contents of each section. */
10088 sym_hashes = elf_sym_hashes (input_bfd);
10089 for (o = input_bfd->sections; o != NULL; o = o->next)
10090 {
10091 bfd_byte *contents;
10092
10093 if (! o->linker_mark)
10094 {
10095 /* This section was omitted from the link. */
10096 continue;
10097 }
10098
10099 if (bfd_link_relocatable (flinfo->info)
10100 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10101 {
10102 /* Deal with the group signature symbol. */
10103 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10104 unsigned long symndx = sec_data->this_hdr.sh_info;
10105 asection *osec = o->output_section;
10106
10107 if (symndx >= locsymcount
10108 || (elf_bad_symtab (input_bfd)
10109 && flinfo->sections[symndx] == NULL))
10110 {
10111 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10112 while (h->root.type == bfd_link_hash_indirect
10113 || h->root.type == bfd_link_hash_warning)
10114 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10115 /* Arrange for symbol to be output. */
10116 h->indx = -2;
10117 elf_section_data (osec)->this_hdr.sh_info = -2;
10118 }
10119 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10120 {
10121 /* We'll use the output section target_index. */
10122 asection *sec = flinfo->sections[symndx]->output_section;
10123 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10124 }
10125 else
10126 {
10127 if (flinfo->indices[symndx] == -1)
10128 {
10129 /* Otherwise output the local symbol now. */
10130 Elf_Internal_Sym sym = isymbuf[symndx];
10131 asection *sec = flinfo->sections[symndx]->output_section;
10132 const char *name;
10133 long indx;
10134 int ret;
10135
10136 name = bfd_elf_string_from_elf_section (input_bfd,
10137 symtab_hdr->sh_link,
10138 sym.st_name);
10139 if (name == NULL)
10140 return FALSE;
10141
10142 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10143 sec);
10144 if (sym.st_shndx == SHN_BAD)
10145 return FALSE;
10146
10147 sym.st_value += o->output_offset;
10148
10149 indx = bfd_get_symcount (output_bfd);
10150 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10151 NULL);
10152 if (ret == 0)
10153 return FALSE;
10154 else if (ret == 1)
10155 flinfo->indices[symndx] = indx;
10156 else
10157 abort ();
10158 }
10159 elf_section_data (osec)->this_hdr.sh_info
10160 = flinfo->indices[symndx];
10161 }
10162 }
10163
10164 if ((o->flags & SEC_HAS_CONTENTS) == 0
10165 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10166 continue;
10167
10168 if ((o->flags & SEC_LINKER_CREATED) != 0)
10169 {
10170 /* Section was created by _bfd_elf_link_create_dynamic_sections
10171 or somesuch. */
10172 continue;
10173 }
10174
10175 /* Get the contents of the section. They have been cached by a
10176 relaxation routine. Note that o is a section in an input
10177 file, so the contents field will not have been set by any of
10178 the routines which work on output files. */
10179 if (elf_section_data (o)->this_hdr.contents != NULL)
10180 {
10181 contents = elf_section_data (o)->this_hdr.contents;
10182 if (bed->caches_rawsize
10183 && o->rawsize != 0
10184 && o->rawsize < o->size)
10185 {
10186 memcpy (flinfo->contents, contents, o->rawsize);
10187 contents = flinfo->contents;
10188 }
10189 }
10190 else
10191 {
10192 contents = flinfo->contents;
10193 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10194 return FALSE;
10195 }
10196
10197 if ((o->flags & SEC_RELOC) != 0)
10198 {
10199 Elf_Internal_Rela *internal_relocs;
10200 Elf_Internal_Rela *rel, *relend;
10201 int action_discarded;
10202 int ret;
10203
10204 /* Get the swapped relocs. */
10205 internal_relocs
10206 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10207 flinfo->internal_relocs, FALSE);
10208 if (internal_relocs == NULL
10209 && o->reloc_count > 0)
10210 return FALSE;
10211
10212 /* We need to reverse-copy input .ctors/.dtors sections if
10213 they are placed in .init_array/.finit_array for output. */
10214 if (o->size > address_size
10215 && ((strncmp (o->name, ".ctors", 6) == 0
10216 && strcmp (o->output_section->name,
10217 ".init_array") == 0)
10218 || (strncmp (o->name, ".dtors", 6) == 0
10219 && strcmp (o->output_section->name,
10220 ".fini_array") == 0))
10221 && (o->name[6] == 0 || o->name[6] == '.'))
10222 {
10223 if (o->size != o->reloc_count * address_size)
10224 {
10225 _bfd_error_handler
10226 /* xgettext:c-format */
10227 (_("error: %B: size of section %A is not "
10228 "multiple of address size"),
10229 input_bfd, o);
10230 bfd_set_error (bfd_error_on_input);
10231 return FALSE;
10232 }
10233 o->flags |= SEC_ELF_REVERSE_COPY;
10234 }
10235
10236 action_discarded = -1;
10237 if (!elf_section_ignore_discarded_relocs (o))
10238 action_discarded = (*bed->action_discarded) (o);
10239
10240 /* Run through the relocs evaluating complex reloc symbols and
10241 looking for relocs against symbols from discarded sections
10242 or section symbols from removed link-once sections.
10243 Complain about relocs against discarded sections. Zero
10244 relocs against removed link-once sections. */
10245
10246 rel = internal_relocs;
10247 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
10248 for ( ; rel < relend; rel++)
10249 {
10250 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10251 unsigned int s_type;
10252 asection **ps, *sec;
10253 struct elf_link_hash_entry *h = NULL;
10254 const char *sym_name;
10255
10256 if (r_symndx == STN_UNDEF)
10257 continue;
10258
10259 if (r_symndx >= locsymcount
10260 || (elf_bad_symtab (input_bfd)
10261 && flinfo->sections[r_symndx] == NULL))
10262 {
10263 h = sym_hashes[r_symndx - extsymoff];
10264
10265 /* Badly formatted input files can contain relocs that
10266 reference non-existant symbols. Check here so that
10267 we do not seg fault. */
10268 if (h == NULL)
10269 {
10270 char buffer [32];
10271
10272 sprintf_vma (buffer, rel->r_info);
10273 _bfd_error_handler
10274 /* xgettext:c-format */
10275 (_("error: %B contains a reloc (0x%s) for section %A "
10276 "that references a non-existent global symbol"),
10277 input_bfd, o, buffer);
10278 bfd_set_error (bfd_error_bad_value);
10279 return FALSE;
10280 }
10281
10282 while (h->root.type == bfd_link_hash_indirect
10283 || h->root.type == bfd_link_hash_warning)
10284 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10285
10286 s_type = h->type;
10287
10288 /* If a plugin symbol is referenced from a non-IR file,
10289 mark the symbol as undefined. Note that the
10290 linker may attach linker created dynamic sections
10291 to the plugin bfd. Symbols defined in linker
10292 created sections are not plugin symbols. */
10293 if (h->root.non_ir_ref
10294 && (h->root.type == bfd_link_hash_defined
10295 || h->root.type == bfd_link_hash_defweak)
10296 && (h->root.u.def.section->flags
10297 & SEC_LINKER_CREATED) == 0
10298 && h->root.u.def.section->owner != NULL
10299 && (h->root.u.def.section->owner->flags
10300 & BFD_PLUGIN) != 0)
10301 {
10302 h->root.type = bfd_link_hash_undefined;
10303 h->root.u.undef.abfd = h->root.u.def.section->owner;
10304 }
10305
10306 ps = NULL;
10307 if (h->root.type == bfd_link_hash_defined
10308 || h->root.type == bfd_link_hash_defweak)
10309 ps = &h->root.u.def.section;
10310
10311 sym_name = h->root.root.string;
10312 }
10313 else
10314 {
10315 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10316
10317 s_type = ELF_ST_TYPE (sym->st_info);
10318 ps = &flinfo->sections[r_symndx];
10319 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10320 sym, *ps);
10321 }
10322
10323 if ((s_type == STT_RELC || s_type == STT_SRELC)
10324 && !bfd_link_relocatable (flinfo->info))
10325 {
10326 bfd_vma val;
10327 bfd_vma dot = (rel->r_offset
10328 + o->output_offset + o->output_section->vma);
10329 #ifdef DEBUG
10330 printf ("Encountered a complex symbol!");
10331 printf (" (input_bfd %s, section %s, reloc %ld\n",
10332 input_bfd->filename, o->name,
10333 (long) (rel - internal_relocs));
10334 printf (" symbol: idx %8.8lx, name %s\n",
10335 r_symndx, sym_name);
10336 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10337 (unsigned long) rel->r_info,
10338 (unsigned long) rel->r_offset);
10339 #endif
10340 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10341 isymbuf, locsymcount, s_type == STT_SRELC))
10342 return FALSE;
10343
10344 /* Symbol evaluated OK. Update to absolute value. */
10345 set_symbol_value (input_bfd, isymbuf, locsymcount,
10346 r_symndx, val);
10347 continue;
10348 }
10349
10350 if (action_discarded != -1 && ps != NULL)
10351 {
10352 /* Complain if the definition comes from a
10353 discarded section. */
10354 if ((sec = *ps) != NULL && discarded_section (sec))
10355 {
10356 BFD_ASSERT (r_symndx != STN_UNDEF);
10357 if (action_discarded & COMPLAIN)
10358 (*flinfo->info->callbacks->einfo)
10359 /* xgettext:c-format */
10360 (_("%X`%s' referenced in section `%A' of %B: "
10361 "defined in discarded section `%A' of %B\n"),
10362 sym_name, o, input_bfd, sec, sec->owner);
10363
10364 /* Try to do the best we can to support buggy old
10365 versions of gcc. Pretend that the symbol is
10366 really defined in the kept linkonce section.
10367 FIXME: This is quite broken. Modifying the
10368 symbol here means we will be changing all later
10369 uses of the symbol, not just in this section. */
10370 if (action_discarded & PRETEND)
10371 {
10372 asection *kept;
10373
10374 kept = _bfd_elf_check_kept_section (sec,
10375 flinfo->info);
10376 if (kept != NULL)
10377 {
10378 *ps = kept;
10379 continue;
10380 }
10381 }
10382 }
10383 }
10384 }
10385
10386 /* Relocate the section by invoking a back end routine.
10387
10388 The back end routine is responsible for adjusting the
10389 section contents as necessary, and (if using Rela relocs
10390 and generating a relocatable output file) adjusting the
10391 reloc addend as necessary.
10392
10393 The back end routine does not have to worry about setting
10394 the reloc address or the reloc symbol index.
10395
10396 The back end routine is given a pointer to the swapped in
10397 internal symbols, and can access the hash table entries
10398 for the external symbols via elf_sym_hashes (input_bfd).
10399
10400 When generating relocatable output, the back end routine
10401 must handle STB_LOCAL/STT_SECTION symbols specially. The
10402 output symbol is going to be a section symbol
10403 corresponding to the output section, which will require
10404 the addend to be adjusted. */
10405
10406 ret = (*relocate_section) (output_bfd, flinfo->info,
10407 input_bfd, o, contents,
10408 internal_relocs,
10409 isymbuf,
10410 flinfo->sections);
10411 if (!ret)
10412 return FALSE;
10413
10414 if (ret == 2
10415 || bfd_link_relocatable (flinfo->info)
10416 || flinfo->info->emitrelocations)
10417 {
10418 Elf_Internal_Rela *irela;
10419 Elf_Internal_Rela *irelaend, *irelamid;
10420 bfd_vma last_offset;
10421 struct elf_link_hash_entry **rel_hash;
10422 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10423 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10424 unsigned int next_erel;
10425 bfd_boolean rela_normal;
10426 struct bfd_elf_section_data *esdi, *esdo;
10427
10428 esdi = elf_section_data (o);
10429 esdo = elf_section_data (o->output_section);
10430 rela_normal = FALSE;
10431
10432 /* Adjust the reloc addresses and symbol indices. */
10433
10434 irela = internal_relocs;
10435 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
10436 rel_hash = esdo->rel.hashes + esdo->rel.count;
10437 /* We start processing the REL relocs, if any. When we reach
10438 IRELAMID in the loop, we switch to the RELA relocs. */
10439 irelamid = irela;
10440 if (esdi->rel.hdr != NULL)
10441 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10442 * bed->s->int_rels_per_ext_rel);
10443 rel_hash_list = rel_hash;
10444 rela_hash_list = NULL;
10445 last_offset = o->output_offset;
10446 if (!bfd_link_relocatable (flinfo->info))
10447 last_offset += o->output_section->vma;
10448 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10449 {
10450 unsigned long r_symndx;
10451 asection *sec;
10452 Elf_Internal_Sym sym;
10453
10454 if (next_erel == bed->s->int_rels_per_ext_rel)
10455 {
10456 rel_hash++;
10457 next_erel = 0;
10458 }
10459
10460 if (irela == irelamid)
10461 {
10462 rel_hash = esdo->rela.hashes + esdo->rela.count;
10463 rela_hash_list = rel_hash;
10464 rela_normal = bed->rela_normal;
10465 }
10466
10467 irela->r_offset = _bfd_elf_section_offset (output_bfd,
10468 flinfo->info, o,
10469 irela->r_offset);
10470 if (irela->r_offset >= (bfd_vma) -2)
10471 {
10472 /* This is a reloc for a deleted entry or somesuch.
10473 Turn it into an R_*_NONE reloc, at the same
10474 offset as the last reloc. elf_eh_frame.c and
10475 bfd_elf_discard_info rely on reloc offsets
10476 being ordered. */
10477 irela->r_offset = last_offset;
10478 irela->r_info = 0;
10479 irela->r_addend = 0;
10480 continue;
10481 }
10482
10483 irela->r_offset += o->output_offset;
10484
10485 /* Relocs in an executable have to be virtual addresses. */
10486 if (!bfd_link_relocatable (flinfo->info))
10487 irela->r_offset += o->output_section->vma;
10488
10489 last_offset = irela->r_offset;
10490
10491 r_symndx = irela->r_info >> r_sym_shift;
10492 if (r_symndx == STN_UNDEF)
10493 continue;
10494
10495 if (r_symndx >= locsymcount
10496 || (elf_bad_symtab (input_bfd)
10497 && flinfo->sections[r_symndx] == NULL))
10498 {
10499 struct elf_link_hash_entry *rh;
10500 unsigned long indx;
10501
10502 /* This is a reloc against a global symbol. We
10503 have not yet output all the local symbols, so
10504 we do not know the symbol index of any global
10505 symbol. We set the rel_hash entry for this
10506 reloc to point to the global hash table entry
10507 for this symbol. The symbol index is then
10508 set at the end of bfd_elf_final_link. */
10509 indx = r_symndx - extsymoff;
10510 rh = elf_sym_hashes (input_bfd)[indx];
10511 while (rh->root.type == bfd_link_hash_indirect
10512 || rh->root.type == bfd_link_hash_warning)
10513 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10514
10515 /* Setting the index to -2 tells
10516 elf_link_output_extsym that this symbol is
10517 used by a reloc. */
10518 BFD_ASSERT (rh->indx < 0);
10519 rh->indx = -2;
10520
10521 *rel_hash = rh;
10522
10523 continue;
10524 }
10525
10526 /* This is a reloc against a local symbol. */
10527
10528 *rel_hash = NULL;
10529 sym = isymbuf[r_symndx];
10530 sec = flinfo->sections[r_symndx];
10531 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10532 {
10533 /* I suppose the backend ought to fill in the
10534 section of any STT_SECTION symbol against a
10535 processor specific section. */
10536 r_symndx = STN_UNDEF;
10537 if (bfd_is_abs_section (sec))
10538 ;
10539 else if (sec == NULL || sec->owner == NULL)
10540 {
10541 bfd_set_error (bfd_error_bad_value);
10542 return FALSE;
10543 }
10544 else
10545 {
10546 asection *osec = sec->output_section;
10547
10548 /* If we have discarded a section, the output
10549 section will be the absolute section. In
10550 case of discarded SEC_MERGE sections, use
10551 the kept section. relocate_section should
10552 have already handled discarded linkonce
10553 sections. */
10554 if (bfd_is_abs_section (osec)
10555 && sec->kept_section != NULL
10556 && sec->kept_section->output_section != NULL)
10557 {
10558 osec = sec->kept_section->output_section;
10559 irela->r_addend -= osec->vma;
10560 }
10561
10562 if (!bfd_is_abs_section (osec))
10563 {
10564 r_symndx = osec->target_index;
10565 if (r_symndx == STN_UNDEF)
10566 {
10567 irela->r_addend += osec->vma;
10568 osec = _bfd_nearby_section (output_bfd, osec,
10569 osec->vma);
10570 irela->r_addend -= osec->vma;
10571 r_symndx = osec->target_index;
10572 }
10573 }
10574 }
10575
10576 /* Adjust the addend according to where the
10577 section winds up in the output section. */
10578 if (rela_normal)
10579 irela->r_addend += sec->output_offset;
10580 }
10581 else
10582 {
10583 if (flinfo->indices[r_symndx] == -1)
10584 {
10585 unsigned long shlink;
10586 const char *name;
10587 asection *osec;
10588 long indx;
10589
10590 if (flinfo->info->strip == strip_all)
10591 {
10592 /* You can't do ld -r -s. */
10593 bfd_set_error (bfd_error_invalid_operation);
10594 return FALSE;
10595 }
10596
10597 /* This symbol was skipped earlier, but
10598 since it is needed by a reloc, we
10599 must output it now. */
10600 shlink = symtab_hdr->sh_link;
10601 name = (bfd_elf_string_from_elf_section
10602 (input_bfd, shlink, sym.st_name));
10603 if (name == NULL)
10604 return FALSE;
10605
10606 osec = sec->output_section;
10607 sym.st_shndx =
10608 _bfd_elf_section_from_bfd_section (output_bfd,
10609 osec);
10610 if (sym.st_shndx == SHN_BAD)
10611 return FALSE;
10612
10613 sym.st_value += sec->output_offset;
10614 if (!bfd_link_relocatable (flinfo->info))
10615 {
10616 sym.st_value += osec->vma;
10617 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10618 {
10619 /* STT_TLS symbols are relative to PT_TLS
10620 segment base. */
10621 BFD_ASSERT (elf_hash_table (flinfo->info)
10622 ->tls_sec != NULL);
10623 sym.st_value -= (elf_hash_table (flinfo->info)
10624 ->tls_sec->vma);
10625 }
10626 }
10627
10628 indx = bfd_get_symcount (output_bfd);
10629 ret = elf_link_output_symstrtab (flinfo, name,
10630 &sym, sec,
10631 NULL);
10632 if (ret == 0)
10633 return FALSE;
10634 else if (ret == 1)
10635 flinfo->indices[r_symndx] = indx;
10636 else
10637 abort ();
10638 }
10639
10640 r_symndx = flinfo->indices[r_symndx];
10641 }
10642
10643 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10644 | (irela->r_info & r_type_mask));
10645 }
10646
10647 /* Swap out the relocs. */
10648 input_rel_hdr = esdi->rel.hdr;
10649 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
10650 {
10651 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10652 input_rel_hdr,
10653 internal_relocs,
10654 rel_hash_list))
10655 return FALSE;
10656 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10657 * bed->s->int_rels_per_ext_rel);
10658 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
10659 }
10660
10661 input_rela_hdr = esdi->rela.hdr;
10662 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10663 {
10664 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10665 input_rela_hdr,
10666 internal_relocs,
10667 rela_hash_list))
10668 return FALSE;
10669 }
10670 }
10671 }
10672
10673 /* Write out the modified section contents. */
10674 if (bed->elf_backend_write_section
10675 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
10676 contents))
10677 {
10678 /* Section written out. */
10679 }
10680 else switch (o->sec_info_type)
10681 {
10682 case SEC_INFO_TYPE_STABS:
10683 if (! (_bfd_write_section_stabs
10684 (output_bfd,
10685 &elf_hash_table (flinfo->info)->stab_info,
10686 o, &elf_section_data (o)->sec_info, contents)))
10687 return FALSE;
10688 break;
10689 case SEC_INFO_TYPE_MERGE:
10690 if (! _bfd_write_merged_section (output_bfd, o,
10691 elf_section_data (o)->sec_info))
10692 return FALSE;
10693 break;
10694 case SEC_INFO_TYPE_EH_FRAME:
10695 {
10696 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
10697 o, contents))
10698 return FALSE;
10699 }
10700 break;
10701 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10702 {
10703 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10704 flinfo->info,
10705 o, contents))
10706 return FALSE;
10707 }
10708 break;
10709 default:
10710 {
10711 if (! (o->flags & SEC_EXCLUDE))
10712 {
10713 file_ptr offset = (file_ptr) o->output_offset;
10714 bfd_size_type todo = o->size;
10715
10716 offset *= bfd_octets_per_byte (output_bfd);
10717
10718 if ((o->flags & SEC_ELF_REVERSE_COPY))
10719 {
10720 /* Reverse-copy input section to output. */
10721 do
10722 {
10723 todo -= address_size;
10724 if (! bfd_set_section_contents (output_bfd,
10725 o->output_section,
10726 contents + todo,
10727 offset,
10728 address_size))
10729 return FALSE;
10730 if (todo == 0)
10731 break;
10732 offset += address_size;
10733 }
10734 while (1);
10735 }
10736 else if (! bfd_set_section_contents (output_bfd,
10737 o->output_section,
10738 contents,
10739 offset, todo))
10740 return FALSE;
10741 }
10742 }
10743 break;
10744 }
10745 }
10746
10747 return TRUE;
10748 }
10749
10750 /* Generate a reloc when linking an ELF file. This is a reloc
10751 requested by the linker, and does not come from any input file. This
10752 is used to build constructor and destructor tables when linking
10753 with -Ur. */
10754
10755 static bfd_boolean
10756 elf_reloc_link_order (bfd *output_bfd,
10757 struct bfd_link_info *info,
10758 asection *output_section,
10759 struct bfd_link_order *link_order)
10760 {
10761 reloc_howto_type *howto;
10762 long indx;
10763 bfd_vma offset;
10764 bfd_vma addend;
10765 struct bfd_elf_section_reloc_data *reldata;
10766 struct elf_link_hash_entry **rel_hash_ptr;
10767 Elf_Internal_Shdr *rel_hdr;
10768 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10769 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10770 bfd_byte *erel;
10771 unsigned int i;
10772 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
10773
10774 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10775 if (howto == NULL)
10776 {
10777 bfd_set_error (bfd_error_bad_value);
10778 return FALSE;
10779 }
10780
10781 addend = link_order->u.reloc.p->addend;
10782
10783 if (esdo->rel.hdr)
10784 reldata = &esdo->rel;
10785 else if (esdo->rela.hdr)
10786 reldata = &esdo->rela;
10787 else
10788 {
10789 reldata = NULL;
10790 BFD_ASSERT (0);
10791 }
10792
10793 /* Figure out the symbol index. */
10794 rel_hash_ptr = reldata->hashes + reldata->count;
10795 if (link_order->type == bfd_section_reloc_link_order)
10796 {
10797 indx = link_order->u.reloc.p->u.section->target_index;
10798 BFD_ASSERT (indx != 0);
10799 *rel_hash_ptr = NULL;
10800 }
10801 else
10802 {
10803 struct elf_link_hash_entry *h;
10804
10805 /* Treat a reloc against a defined symbol as though it were
10806 actually against the section. */
10807 h = ((struct elf_link_hash_entry *)
10808 bfd_wrapped_link_hash_lookup (output_bfd, info,
10809 link_order->u.reloc.p->u.name,
10810 FALSE, FALSE, TRUE));
10811 if (h != NULL
10812 && (h->root.type == bfd_link_hash_defined
10813 || h->root.type == bfd_link_hash_defweak))
10814 {
10815 asection *section;
10816
10817 section = h->root.u.def.section;
10818 indx = section->output_section->target_index;
10819 *rel_hash_ptr = NULL;
10820 /* It seems that we ought to add the symbol value to the
10821 addend here, but in practice it has already been added
10822 because it was passed to constructor_callback. */
10823 addend += section->output_section->vma + section->output_offset;
10824 }
10825 else if (h != NULL)
10826 {
10827 /* Setting the index to -2 tells elf_link_output_extsym that
10828 this symbol is used by a reloc. */
10829 h->indx = -2;
10830 *rel_hash_ptr = h;
10831 indx = 0;
10832 }
10833 else
10834 {
10835 (*info->callbacks->unattached_reloc)
10836 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
10837 indx = 0;
10838 }
10839 }
10840
10841 /* If this is an inplace reloc, we must write the addend into the
10842 object file. */
10843 if (howto->partial_inplace && addend != 0)
10844 {
10845 bfd_size_type size;
10846 bfd_reloc_status_type rstat;
10847 bfd_byte *buf;
10848 bfd_boolean ok;
10849 const char *sym_name;
10850
10851 size = (bfd_size_type) bfd_get_reloc_size (howto);
10852 buf = (bfd_byte *) bfd_zmalloc (size);
10853 if (buf == NULL && size != 0)
10854 return FALSE;
10855 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10856 switch (rstat)
10857 {
10858 case bfd_reloc_ok:
10859 break;
10860
10861 default:
10862 case bfd_reloc_outofrange:
10863 abort ();
10864
10865 case bfd_reloc_overflow:
10866 if (link_order->type == bfd_section_reloc_link_order)
10867 sym_name = bfd_section_name (output_bfd,
10868 link_order->u.reloc.p->u.section);
10869 else
10870 sym_name = link_order->u.reloc.p->u.name;
10871 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
10872 howto->name, addend, NULL, NULL,
10873 (bfd_vma) 0);
10874 break;
10875 }
10876
10877 ok = bfd_set_section_contents (output_bfd, output_section, buf,
10878 link_order->offset
10879 * bfd_octets_per_byte (output_bfd),
10880 size);
10881 free (buf);
10882 if (! ok)
10883 return FALSE;
10884 }
10885
10886 /* The address of a reloc is relative to the section in a
10887 relocatable file, and is a virtual address in an executable
10888 file. */
10889 offset = link_order->offset;
10890 if (! bfd_link_relocatable (info))
10891 offset += output_section->vma;
10892
10893 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10894 {
10895 irel[i].r_offset = offset;
10896 irel[i].r_info = 0;
10897 irel[i].r_addend = 0;
10898 }
10899 if (bed->s->arch_size == 32)
10900 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10901 else
10902 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10903
10904 rel_hdr = reldata->hdr;
10905 erel = rel_hdr->contents;
10906 if (rel_hdr->sh_type == SHT_REL)
10907 {
10908 erel += reldata->count * bed->s->sizeof_rel;
10909 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10910 }
10911 else
10912 {
10913 irel[0].r_addend = addend;
10914 erel += reldata->count * bed->s->sizeof_rela;
10915 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10916 }
10917
10918 ++reldata->count;
10919
10920 return TRUE;
10921 }
10922
10923
10924 /* Get the output vma of the section pointed to by the sh_link field. */
10925
10926 static bfd_vma
10927 elf_get_linked_section_vma (struct bfd_link_order *p)
10928 {
10929 Elf_Internal_Shdr **elf_shdrp;
10930 asection *s;
10931 int elfsec;
10932
10933 s = p->u.indirect.section;
10934 elf_shdrp = elf_elfsections (s->owner);
10935 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10936 elfsec = elf_shdrp[elfsec]->sh_link;
10937 /* PR 290:
10938 The Intel C compiler generates SHT_IA_64_UNWIND with
10939 SHF_LINK_ORDER. But it doesn't set the sh_link or
10940 sh_info fields. Hence we could get the situation
10941 where elfsec is 0. */
10942 if (elfsec == 0)
10943 {
10944 const struct elf_backend_data *bed
10945 = get_elf_backend_data (s->owner);
10946 if (bed->link_order_error_handler)
10947 bed->link_order_error_handler
10948 /* xgettext:c-format */
10949 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
10950 return 0;
10951 }
10952 else
10953 {
10954 s = elf_shdrp[elfsec]->bfd_section;
10955 return s->output_section->vma + s->output_offset;
10956 }
10957 }
10958
10959
10960 /* Compare two sections based on the locations of the sections they are
10961 linked to. Used by elf_fixup_link_order. */
10962
10963 static int
10964 compare_link_order (const void * a, const void * b)
10965 {
10966 bfd_vma apos;
10967 bfd_vma bpos;
10968
10969 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10970 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10971 if (apos < bpos)
10972 return -1;
10973 return apos > bpos;
10974 }
10975
10976
10977 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10978 order as their linked sections. Returns false if this could not be done
10979 because an output section includes both ordered and unordered
10980 sections. Ideally we'd do this in the linker proper. */
10981
10982 static bfd_boolean
10983 elf_fixup_link_order (bfd *abfd, asection *o)
10984 {
10985 int seen_linkorder;
10986 int seen_other;
10987 int n;
10988 struct bfd_link_order *p;
10989 bfd *sub;
10990 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10991 unsigned elfsec;
10992 struct bfd_link_order **sections;
10993 asection *s, *other_sec, *linkorder_sec;
10994 bfd_vma offset;
10995
10996 other_sec = NULL;
10997 linkorder_sec = NULL;
10998 seen_other = 0;
10999 seen_linkorder = 0;
11000 for (p = o->map_head.link_order; p != NULL; p = p->next)
11001 {
11002 if (p->type == bfd_indirect_link_order)
11003 {
11004 s = p->u.indirect.section;
11005 sub = s->owner;
11006 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11007 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
11008 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11009 && elfsec < elf_numsections (sub)
11010 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11011 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
11012 {
11013 seen_linkorder++;
11014 linkorder_sec = s;
11015 }
11016 else
11017 {
11018 seen_other++;
11019 other_sec = s;
11020 }
11021 }
11022 else
11023 seen_other++;
11024
11025 if (seen_other && seen_linkorder)
11026 {
11027 if (other_sec && linkorder_sec)
11028 _bfd_error_handler
11029 /* xgettext:c-format */
11030 (_("%A has both ordered [`%A' in %B] "
11031 "and unordered [`%A' in %B] sections"),
11032 o, linkorder_sec,
11033 linkorder_sec->owner, other_sec,
11034 other_sec->owner);
11035 else
11036 _bfd_error_handler
11037 (_("%A has both ordered and unordered sections"), o);
11038 bfd_set_error (bfd_error_bad_value);
11039 return FALSE;
11040 }
11041 }
11042
11043 if (!seen_linkorder)
11044 return TRUE;
11045
11046 sections = (struct bfd_link_order **)
11047 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11048 if (sections == NULL)
11049 return FALSE;
11050 seen_linkorder = 0;
11051
11052 for (p = o->map_head.link_order; p != NULL; p = p->next)
11053 {
11054 sections[seen_linkorder++] = p;
11055 }
11056 /* Sort the input sections in the order of their linked section. */
11057 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11058 compare_link_order);
11059
11060 /* Change the offsets of the sections. */
11061 offset = 0;
11062 for (n = 0; n < seen_linkorder; n++)
11063 {
11064 s = sections[n]->u.indirect.section;
11065 offset &= ~(bfd_vma) 0 << s->alignment_power;
11066 s->output_offset = offset / bfd_octets_per_byte (abfd);
11067 sections[n]->offset = offset;
11068 offset += sections[n]->size;
11069 }
11070
11071 free (sections);
11072 return TRUE;
11073 }
11074
11075 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11076 Returns TRUE upon success, FALSE otherwise. */
11077
11078 static bfd_boolean
11079 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11080 {
11081 bfd_boolean ret = FALSE;
11082 bfd *implib_bfd;
11083 const struct elf_backend_data *bed;
11084 flagword flags;
11085 enum bfd_architecture arch;
11086 unsigned int mach;
11087 asymbol **sympp = NULL;
11088 long symsize;
11089 long symcount;
11090 long src_count;
11091 elf_symbol_type *osymbuf;
11092
11093 implib_bfd = info->out_implib_bfd;
11094 bed = get_elf_backend_data (abfd);
11095
11096 if (!bfd_set_format (implib_bfd, bfd_object))
11097 return FALSE;
11098
11099 flags = bfd_get_file_flags (abfd);
11100 flags &= ~HAS_RELOC;
11101 if (!bfd_set_start_address (implib_bfd, 0)
11102 || !bfd_set_file_flags (implib_bfd, flags))
11103 return FALSE;
11104
11105 /* Copy architecture of output file to import library file. */
11106 arch = bfd_get_arch (abfd);
11107 mach = bfd_get_mach (abfd);
11108 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11109 && (abfd->target_defaulted
11110 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11111 return FALSE;
11112
11113 /* Get symbol table size. */
11114 symsize = bfd_get_symtab_upper_bound (abfd);
11115 if (symsize < 0)
11116 return FALSE;
11117
11118 /* Read in the symbol table. */
11119 sympp = (asymbol **) xmalloc (symsize);
11120 symcount = bfd_canonicalize_symtab (abfd, sympp);
11121 if (symcount < 0)
11122 goto free_sym_buf;
11123
11124 /* Allow the BFD backend to copy any private header data it
11125 understands from the output BFD to the import library BFD. */
11126 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11127 goto free_sym_buf;
11128
11129 /* Filter symbols to appear in the import library. */
11130 if (bed->elf_backend_filter_implib_symbols)
11131 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11132 symcount);
11133 else
11134 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11135 if (symcount == 0)
11136 {
11137 bfd_set_error (bfd_error_no_symbols);
11138 _bfd_error_handler (_("%B: no symbol found for import library"),
11139 implib_bfd);
11140 goto free_sym_buf;
11141 }
11142
11143
11144 /* Make symbols absolute. */
11145 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11146 sizeof (*osymbuf));
11147 for (src_count = 0; src_count < symcount; src_count++)
11148 {
11149 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11150 sizeof (*osymbuf));
11151 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11152 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11153 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11154 osymbuf[src_count].internal_elf_sym.st_value =
11155 osymbuf[src_count].symbol.value;
11156 sympp[src_count] = &osymbuf[src_count].symbol;
11157 }
11158
11159 bfd_set_symtab (implib_bfd, sympp, symcount);
11160
11161 /* Allow the BFD backend to copy any private data it understands
11162 from the output BFD to the import library BFD. This is done last
11163 to permit the routine to look at the filtered symbol table. */
11164 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11165 goto free_sym_buf;
11166
11167 if (!bfd_close (implib_bfd))
11168 goto free_sym_buf;
11169
11170 ret = TRUE;
11171
11172 free_sym_buf:
11173 free (sympp);
11174 return ret;
11175 }
11176
11177 static void
11178 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11179 {
11180 asection *o;
11181
11182 if (flinfo->symstrtab != NULL)
11183 _bfd_elf_strtab_free (flinfo->symstrtab);
11184 if (flinfo->contents != NULL)
11185 free (flinfo->contents);
11186 if (flinfo->external_relocs != NULL)
11187 free (flinfo->external_relocs);
11188 if (flinfo->internal_relocs != NULL)
11189 free (flinfo->internal_relocs);
11190 if (flinfo->external_syms != NULL)
11191 free (flinfo->external_syms);
11192 if (flinfo->locsym_shndx != NULL)
11193 free (flinfo->locsym_shndx);
11194 if (flinfo->internal_syms != NULL)
11195 free (flinfo->internal_syms);
11196 if (flinfo->indices != NULL)
11197 free (flinfo->indices);
11198 if (flinfo->sections != NULL)
11199 free (flinfo->sections);
11200 if (flinfo->symshndxbuf != NULL)
11201 free (flinfo->symshndxbuf);
11202 for (o = obfd->sections; o != NULL; o = o->next)
11203 {
11204 struct bfd_elf_section_data *esdo = elf_section_data (o);
11205 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11206 free (esdo->rel.hashes);
11207 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11208 free (esdo->rela.hashes);
11209 }
11210 }
11211
11212 /* Do the final step of an ELF link. */
11213
11214 bfd_boolean
11215 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11216 {
11217 bfd_boolean dynamic;
11218 bfd_boolean emit_relocs;
11219 bfd *dynobj;
11220 struct elf_final_link_info flinfo;
11221 asection *o;
11222 struct bfd_link_order *p;
11223 bfd *sub;
11224 bfd_size_type max_contents_size;
11225 bfd_size_type max_external_reloc_size;
11226 bfd_size_type max_internal_reloc_count;
11227 bfd_size_type max_sym_count;
11228 bfd_size_type max_sym_shndx_count;
11229 Elf_Internal_Sym elfsym;
11230 unsigned int i;
11231 Elf_Internal_Shdr *symtab_hdr;
11232 Elf_Internal_Shdr *symtab_shndx_hdr;
11233 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11234 struct elf_outext_info eoinfo;
11235 bfd_boolean merged;
11236 size_t relativecount = 0;
11237 asection *reldyn = 0;
11238 bfd_size_type amt;
11239 asection *attr_section = NULL;
11240 bfd_vma attr_size = 0;
11241 const char *std_attrs_section;
11242 struct elf_link_hash_table *htab = elf_hash_table (info);
11243
11244 if (!is_elf_hash_table (htab))
11245 return FALSE;
11246
11247 if (bfd_link_pic (info))
11248 abfd->flags |= DYNAMIC;
11249
11250 dynamic = htab->dynamic_sections_created;
11251 dynobj = htab->dynobj;
11252
11253 emit_relocs = (bfd_link_relocatable (info)
11254 || info->emitrelocations);
11255
11256 flinfo.info = info;
11257 flinfo.output_bfd = abfd;
11258 flinfo.symstrtab = _bfd_elf_strtab_init ();
11259 if (flinfo.symstrtab == NULL)
11260 return FALSE;
11261
11262 if (! dynamic)
11263 {
11264 flinfo.hash_sec = NULL;
11265 flinfo.symver_sec = NULL;
11266 }
11267 else
11268 {
11269 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11270 /* Note that dynsym_sec can be NULL (on VMS). */
11271 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11272 /* Note that it is OK if symver_sec is NULL. */
11273 }
11274
11275 flinfo.contents = NULL;
11276 flinfo.external_relocs = NULL;
11277 flinfo.internal_relocs = NULL;
11278 flinfo.external_syms = NULL;
11279 flinfo.locsym_shndx = NULL;
11280 flinfo.internal_syms = NULL;
11281 flinfo.indices = NULL;
11282 flinfo.sections = NULL;
11283 flinfo.symshndxbuf = NULL;
11284 flinfo.filesym_count = 0;
11285
11286 /* The object attributes have been merged. Remove the input
11287 sections from the link, and set the contents of the output
11288 secton. */
11289 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11290 for (o = abfd->sections; o != NULL; o = o->next)
11291 {
11292 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11293 || strcmp (o->name, ".gnu.attributes") == 0)
11294 {
11295 for (p = o->map_head.link_order; p != NULL; p = p->next)
11296 {
11297 asection *input_section;
11298
11299 if (p->type != bfd_indirect_link_order)
11300 continue;
11301 input_section = p->u.indirect.section;
11302 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11303 elf_link_input_bfd ignores this section. */
11304 input_section->flags &= ~SEC_HAS_CONTENTS;
11305 }
11306
11307 attr_size = bfd_elf_obj_attr_size (abfd);
11308 if (attr_size)
11309 {
11310 bfd_set_section_size (abfd, o, attr_size);
11311 attr_section = o;
11312 /* Skip this section later on. */
11313 o->map_head.link_order = NULL;
11314 }
11315 else
11316 o->flags |= SEC_EXCLUDE;
11317 }
11318 }
11319
11320 /* Count up the number of relocations we will output for each output
11321 section, so that we know the sizes of the reloc sections. We
11322 also figure out some maximum sizes. */
11323 max_contents_size = 0;
11324 max_external_reloc_size = 0;
11325 max_internal_reloc_count = 0;
11326 max_sym_count = 0;
11327 max_sym_shndx_count = 0;
11328 merged = FALSE;
11329 for (o = abfd->sections; o != NULL; o = o->next)
11330 {
11331 struct bfd_elf_section_data *esdo = elf_section_data (o);
11332 o->reloc_count = 0;
11333
11334 for (p = o->map_head.link_order; p != NULL; p = p->next)
11335 {
11336 unsigned int reloc_count = 0;
11337 unsigned int additional_reloc_count = 0;
11338 struct bfd_elf_section_data *esdi = NULL;
11339
11340 if (p->type == bfd_section_reloc_link_order
11341 || p->type == bfd_symbol_reloc_link_order)
11342 reloc_count = 1;
11343 else if (p->type == bfd_indirect_link_order)
11344 {
11345 asection *sec;
11346
11347 sec = p->u.indirect.section;
11348 /* See PR 20908 for a reproducer. */
11349 if (bfd_get_flavour (sec->owner) != bfd_target_elf_flavour)
11350 {
11351 _bfd_error_handler (_("%B: not in ELF format"), sec->owner);
11352 goto error_return;
11353 }
11354 esdi = elf_section_data (sec);
11355
11356 /* Mark all sections which are to be included in the
11357 link. This will normally be every section. We need
11358 to do this so that we can identify any sections which
11359 the linker has decided to not include. */
11360 sec->linker_mark = TRUE;
11361
11362 if (sec->flags & SEC_MERGE)
11363 merged = TRUE;
11364
11365 if (esdo->this_hdr.sh_type == SHT_REL
11366 || esdo->this_hdr.sh_type == SHT_RELA)
11367 /* Some backends use reloc_count in relocation sections
11368 to count particular types of relocs. Of course,
11369 reloc sections themselves can't have relocations. */
11370 reloc_count = 0;
11371 else if (emit_relocs)
11372 {
11373 reloc_count = sec->reloc_count;
11374 if (bed->elf_backend_count_additional_relocs)
11375 {
11376 int c;
11377 c = (*bed->elf_backend_count_additional_relocs) (sec);
11378 additional_reloc_count += c;
11379 }
11380 }
11381 else if (bed->elf_backend_count_relocs)
11382 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11383
11384 if (sec->rawsize > max_contents_size)
11385 max_contents_size = sec->rawsize;
11386 if (sec->size > max_contents_size)
11387 max_contents_size = sec->size;
11388
11389 /* We are interested in just local symbols, not all
11390 symbols. */
11391 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11392 && (sec->owner->flags & DYNAMIC) == 0)
11393 {
11394 size_t sym_count;
11395
11396 if (elf_bad_symtab (sec->owner))
11397 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11398 / bed->s->sizeof_sym);
11399 else
11400 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11401
11402 if (sym_count > max_sym_count)
11403 max_sym_count = sym_count;
11404
11405 if (sym_count > max_sym_shndx_count
11406 && elf_symtab_shndx_list (sec->owner) != NULL)
11407 max_sym_shndx_count = sym_count;
11408
11409 if ((sec->flags & SEC_RELOC) != 0)
11410 {
11411 size_t ext_size = 0;
11412
11413 if (esdi->rel.hdr != NULL)
11414 ext_size = esdi->rel.hdr->sh_size;
11415 if (esdi->rela.hdr != NULL)
11416 ext_size += esdi->rela.hdr->sh_size;
11417
11418 if (ext_size > max_external_reloc_size)
11419 max_external_reloc_size = ext_size;
11420 if (sec->reloc_count > max_internal_reloc_count)
11421 max_internal_reloc_count = sec->reloc_count;
11422 }
11423 }
11424 }
11425
11426 if (reloc_count == 0)
11427 continue;
11428
11429 reloc_count += additional_reloc_count;
11430 o->reloc_count += reloc_count;
11431
11432 if (p->type == bfd_indirect_link_order && emit_relocs)
11433 {
11434 if (esdi->rel.hdr)
11435 {
11436 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11437 esdo->rel.count += additional_reloc_count;
11438 }
11439 if (esdi->rela.hdr)
11440 {
11441 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11442 esdo->rela.count += additional_reloc_count;
11443 }
11444 }
11445 else
11446 {
11447 if (o->use_rela_p)
11448 esdo->rela.count += reloc_count;
11449 else
11450 esdo->rel.count += reloc_count;
11451 }
11452 }
11453
11454 if (o->reloc_count > 0)
11455 o->flags |= SEC_RELOC;
11456 else
11457 {
11458 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11459 set it (this is probably a bug) and if it is set
11460 assign_section_numbers will create a reloc section. */
11461 o->flags &=~ SEC_RELOC;
11462 }
11463
11464 /* If the SEC_ALLOC flag is not set, force the section VMA to
11465 zero. This is done in elf_fake_sections as well, but forcing
11466 the VMA to 0 here will ensure that relocs against these
11467 sections are handled correctly. */
11468 if ((o->flags & SEC_ALLOC) == 0
11469 && ! o->user_set_vma)
11470 o->vma = 0;
11471 }
11472
11473 if (! bfd_link_relocatable (info) && merged)
11474 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
11475
11476 /* Figure out the file positions for everything but the symbol table
11477 and the relocs. We set symcount to force assign_section_numbers
11478 to create a symbol table. */
11479 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
11480 BFD_ASSERT (! abfd->output_has_begun);
11481 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11482 goto error_return;
11483
11484 /* Set sizes, and assign file positions for reloc sections. */
11485 for (o = abfd->sections; o != NULL; o = o->next)
11486 {
11487 struct bfd_elf_section_data *esdo = elf_section_data (o);
11488 if ((o->flags & SEC_RELOC) != 0)
11489 {
11490 if (esdo->rel.hdr
11491 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
11492 goto error_return;
11493
11494 if (esdo->rela.hdr
11495 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
11496 goto error_return;
11497 }
11498
11499 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11500 to count upwards while actually outputting the relocations. */
11501 esdo->rel.count = 0;
11502 esdo->rela.count = 0;
11503
11504 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11505 {
11506 /* Cache the section contents so that they can be compressed
11507 later. Use bfd_malloc since it will be freed by
11508 bfd_compress_section_contents. */
11509 unsigned char *contents = esdo->this_hdr.contents;
11510 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11511 abort ();
11512 contents
11513 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11514 if (contents == NULL)
11515 goto error_return;
11516 esdo->this_hdr.contents = contents;
11517 }
11518 }
11519
11520 /* We have now assigned file positions for all the sections except
11521 .symtab, .strtab, and non-loaded reloc sections. We start the
11522 .symtab section at the current file position, and write directly
11523 to it. We build the .strtab section in memory. */
11524 bfd_get_symcount (abfd) = 0;
11525 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11526 /* sh_name is set in prep_headers. */
11527 symtab_hdr->sh_type = SHT_SYMTAB;
11528 /* sh_flags, sh_addr and sh_size all start off zero. */
11529 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11530 /* sh_link is set in assign_section_numbers. */
11531 /* sh_info is set below. */
11532 /* sh_offset is set just below. */
11533 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
11534
11535 if (max_sym_count < 20)
11536 max_sym_count = 20;
11537 htab->strtabsize = max_sym_count;
11538 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11539 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
11540 if (htab->strtab == NULL)
11541 goto error_return;
11542 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11543 flinfo.symshndxbuf
11544 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11545 ? (Elf_External_Sym_Shndx *) -1 : NULL);
11546
11547 if (info->strip != strip_all || emit_relocs)
11548 {
11549 file_ptr off = elf_next_file_pos (abfd);
11550
11551 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11552
11553 /* Note that at this point elf_next_file_pos (abfd) is
11554 incorrect. We do not yet know the size of the .symtab section.
11555 We correct next_file_pos below, after we do know the size. */
11556
11557 /* Start writing out the symbol table. The first symbol is always a
11558 dummy symbol. */
11559 elfsym.st_value = 0;
11560 elfsym.st_size = 0;
11561 elfsym.st_info = 0;
11562 elfsym.st_other = 0;
11563 elfsym.st_shndx = SHN_UNDEF;
11564 elfsym.st_target_internal = 0;
11565 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11566 bfd_und_section_ptr, NULL) != 1)
11567 goto error_return;
11568
11569 /* Output a symbol for each section. We output these even if we are
11570 discarding local symbols, since they are used for relocs. These
11571 symbols have no names. We store the index of each one in the
11572 index field of the section, so that we can find it again when
11573 outputting relocs. */
11574
11575 elfsym.st_size = 0;
11576 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11577 elfsym.st_other = 0;
11578 elfsym.st_value = 0;
11579 elfsym.st_target_internal = 0;
11580 for (i = 1; i < elf_numsections (abfd); i++)
11581 {
11582 o = bfd_section_from_elf_index (abfd, i);
11583 if (o != NULL)
11584 {
11585 o->target_index = bfd_get_symcount (abfd);
11586 elfsym.st_shndx = i;
11587 if (!bfd_link_relocatable (info))
11588 elfsym.st_value = o->vma;
11589 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11590 NULL) != 1)
11591 goto error_return;
11592 }
11593 }
11594 }
11595
11596 /* Allocate some memory to hold information read in from the input
11597 files. */
11598 if (max_contents_size != 0)
11599 {
11600 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11601 if (flinfo.contents == NULL)
11602 goto error_return;
11603 }
11604
11605 if (max_external_reloc_size != 0)
11606 {
11607 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11608 if (flinfo.external_relocs == NULL)
11609 goto error_return;
11610 }
11611
11612 if (max_internal_reloc_count != 0)
11613 {
11614 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11615 amt *= sizeof (Elf_Internal_Rela);
11616 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11617 if (flinfo.internal_relocs == NULL)
11618 goto error_return;
11619 }
11620
11621 if (max_sym_count != 0)
11622 {
11623 amt = max_sym_count * bed->s->sizeof_sym;
11624 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11625 if (flinfo.external_syms == NULL)
11626 goto error_return;
11627
11628 amt = max_sym_count * sizeof (Elf_Internal_Sym);
11629 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11630 if (flinfo.internal_syms == NULL)
11631 goto error_return;
11632
11633 amt = max_sym_count * sizeof (long);
11634 flinfo.indices = (long int *) bfd_malloc (amt);
11635 if (flinfo.indices == NULL)
11636 goto error_return;
11637
11638 amt = max_sym_count * sizeof (asection *);
11639 flinfo.sections = (asection **) bfd_malloc (amt);
11640 if (flinfo.sections == NULL)
11641 goto error_return;
11642 }
11643
11644 if (max_sym_shndx_count != 0)
11645 {
11646 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
11647 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11648 if (flinfo.locsym_shndx == NULL)
11649 goto error_return;
11650 }
11651
11652 if (htab->tls_sec)
11653 {
11654 bfd_vma base, end = 0;
11655 asection *sec;
11656
11657 for (sec = htab->tls_sec;
11658 sec && (sec->flags & SEC_THREAD_LOCAL);
11659 sec = sec->next)
11660 {
11661 bfd_size_type size = sec->size;
11662
11663 if (size == 0
11664 && (sec->flags & SEC_HAS_CONTENTS) == 0)
11665 {
11666 struct bfd_link_order *ord = sec->map_tail.link_order;
11667
11668 if (ord != NULL)
11669 size = ord->offset + ord->size;
11670 }
11671 end = sec->vma + size;
11672 }
11673 base = htab->tls_sec->vma;
11674 /* Only align end of TLS section if static TLS doesn't have special
11675 alignment requirements. */
11676 if (bed->static_tls_alignment == 1)
11677 end = align_power (end, htab->tls_sec->alignment_power);
11678 htab->tls_size = end - base;
11679 }
11680
11681 /* Reorder SHF_LINK_ORDER sections. */
11682 for (o = abfd->sections; o != NULL; o = o->next)
11683 {
11684 if (!elf_fixup_link_order (abfd, o))
11685 return FALSE;
11686 }
11687
11688 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11689 return FALSE;
11690
11691 /* Since ELF permits relocations to be against local symbols, we
11692 must have the local symbols available when we do the relocations.
11693 Since we would rather only read the local symbols once, and we
11694 would rather not keep them in memory, we handle all the
11695 relocations for a single input file at the same time.
11696
11697 Unfortunately, there is no way to know the total number of local
11698 symbols until we have seen all of them, and the local symbol
11699 indices precede the global symbol indices. This means that when
11700 we are generating relocatable output, and we see a reloc against
11701 a global symbol, we can not know the symbol index until we have
11702 finished examining all the local symbols to see which ones we are
11703 going to output. To deal with this, we keep the relocations in
11704 memory, and don't output them until the end of the link. This is
11705 an unfortunate waste of memory, but I don't see a good way around
11706 it. Fortunately, it only happens when performing a relocatable
11707 link, which is not the common case. FIXME: If keep_memory is set
11708 we could write the relocs out and then read them again; I don't
11709 know how bad the memory loss will be. */
11710
11711 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11712 sub->output_has_begun = FALSE;
11713 for (o = abfd->sections; o != NULL; o = o->next)
11714 {
11715 for (p = o->map_head.link_order; p != NULL; p = p->next)
11716 {
11717 if (p->type == bfd_indirect_link_order
11718 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11719 == bfd_target_elf_flavour)
11720 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11721 {
11722 if (! sub->output_has_begun)
11723 {
11724 if (! elf_link_input_bfd (&flinfo, sub))
11725 goto error_return;
11726 sub->output_has_begun = TRUE;
11727 }
11728 }
11729 else if (p->type == bfd_section_reloc_link_order
11730 || p->type == bfd_symbol_reloc_link_order)
11731 {
11732 if (! elf_reloc_link_order (abfd, info, o, p))
11733 goto error_return;
11734 }
11735 else
11736 {
11737 if (! _bfd_default_link_order (abfd, info, o, p))
11738 {
11739 if (p->type == bfd_indirect_link_order
11740 && (bfd_get_flavour (sub)
11741 == bfd_target_elf_flavour)
11742 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11743 != bed->s->elfclass))
11744 {
11745 const char *iclass, *oclass;
11746
11747 switch (bed->s->elfclass)
11748 {
11749 case ELFCLASS64: oclass = "ELFCLASS64"; break;
11750 case ELFCLASS32: oclass = "ELFCLASS32"; break;
11751 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
11752 default: abort ();
11753 }
11754
11755 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
11756 {
11757 case ELFCLASS64: iclass = "ELFCLASS64"; break;
11758 case ELFCLASS32: iclass = "ELFCLASS32"; break;
11759 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
11760 default: abort ();
11761 }
11762
11763 bfd_set_error (bfd_error_wrong_format);
11764 _bfd_error_handler
11765 /* xgettext:c-format */
11766 (_("%B: file class %s incompatible with %s"),
11767 sub, iclass, oclass);
11768 }
11769
11770 goto error_return;
11771 }
11772 }
11773 }
11774 }
11775
11776 /* Free symbol buffer if needed. */
11777 if (!info->reduce_memory_overheads)
11778 {
11779 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11780 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11781 && elf_tdata (sub)->symbuf)
11782 {
11783 free (elf_tdata (sub)->symbuf);
11784 elf_tdata (sub)->symbuf = NULL;
11785 }
11786 }
11787
11788 /* Output any global symbols that got converted to local in a
11789 version script or due to symbol visibility. We do this in a
11790 separate step since ELF requires all local symbols to appear
11791 prior to any global symbols. FIXME: We should only do this if
11792 some global symbols were, in fact, converted to become local.
11793 FIXME: Will this work correctly with the Irix 5 linker? */
11794 eoinfo.failed = FALSE;
11795 eoinfo.flinfo = &flinfo;
11796 eoinfo.localsyms = TRUE;
11797 eoinfo.file_sym_done = FALSE;
11798 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11799 if (eoinfo.failed)
11800 return FALSE;
11801
11802 /* If backend needs to output some local symbols not present in the hash
11803 table, do it now. */
11804 if (bed->elf_backend_output_arch_local_syms
11805 && (info->strip != strip_all || emit_relocs))
11806 {
11807 typedef int (*out_sym_func)
11808 (void *, const char *, Elf_Internal_Sym *, asection *,
11809 struct elf_link_hash_entry *);
11810
11811 if (! ((*bed->elf_backend_output_arch_local_syms)
11812 (abfd, info, &flinfo,
11813 (out_sym_func) elf_link_output_symstrtab)))
11814 return FALSE;
11815 }
11816
11817 /* That wrote out all the local symbols. Finish up the symbol table
11818 with the global symbols. Even if we want to strip everything we
11819 can, we still need to deal with those global symbols that got
11820 converted to local in a version script. */
11821
11822 /* The sh_info field records the index of the first non local symbol. */
11823 symtab_hdr->sh_info = bfd_get_symcount (abfd);
11824
11825 if (dynamic
11826 && htab->dynsym != NULL
11827 && htab->dynsym->output_section != bfd_abs_section_ptr)
11828 {
11829 Elf_Internal_Sym sym;
11830 bfd_byte *dynsym = htab->dynsym->contents;
11831
11832 o = htab->dynsym->output_section;
11833 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
11834
11835 /* Write out the section symbols for the output sections. */
11836 if (bfd_link_pic (info)
11837 || htab->is_relocatable_executable)
11838 {
11839 asection *s;
11840
11841 sym.st_size = 0;
11842 sym.st_name = 0;
11843 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11844 sym.st_other = 0;
11845 sym.st_target_internal = 0;
11846
11847 for (s = abfd->sections; s != NULL; s = s->next)
11848 {
11849 int indx;
11850 bfd_byte *dest;
11851 long dynindx;
11852
11853 dynindx = elf_section_data (s)->dynindx;
11854 if (dynindx <= 0)
11855 continue;
11856 indx = elf_section_data (s)->this_idx;
11857 BFD_ASSERT (indx > 0);
11858 sym.st_shndx = indx;
11859 if (! check_dynsym (abfd, &sym))
11860 return FALSE;
11861 sym.st_value = s->vma;
11862 dest = dynsym + dynindx * bed->s->sizeof_sym;
11863 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11864 }
11865 }
11866
11867 /* Write out the local dynsyms. */
11868 if (htab->dynlocal)
11869 {
11870 struct elf_link_local_dynamic_entry *e;
11871 for (e = htab->dynlocal; e ; e = e->next)
11872 {
11873 asection *s;
11874 bfd_byte *dest;
11875
11876 /* Copy the internal symbol and turn off visibility.
11877 Note that we saved a word of storage and overwrote
11878 the original st_name with the dynstr_index. */
11879 sym = e->isym;
11880 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
11881
11882 s = bfd_section_from_elf_index (e->input_bfd,
11883 e->isym.st_shndx);
11884 if (s != NULL)
11885 {
11886 sym.st_shndx =
11887 elf_section_data (s->output_section)->this_idx;
11888 if (! check_dynsym (abfd, &sym))
11889 return FALSE;
11890 sym.st_value = (s->output_section->vma
11891 + s->output_offset
11892 + e->isym.st_value);
11893 }
11894
11895 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
11896 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11897 }
11898 }
11899 }
11900
11901 /* We get the global symbols from the hash table. */
11902 eoinfo.failed = FALSE;
11903 eoinfo.localsyms = FALSE;
11904 eoinfo.flinfo = &flinfo;
11905 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11906 if (eoinfo.failed)
11907 return FALSE;
11908
11909 /* If backend needs to output some symbols not present in the hash
11910 table, do it now. */
11911 if (bed->elf_backend_output_arch_syms
11912 && (info->strip != strip_all || emit_relocs))
11913 {
11914 typedef int (*out_sym_func)
11915 (void *, const char *, Elf_Internal_Sym *, asection *,
11916 struct elf_link_hash_entry *);
11917
11918 if (! ((*bed->elf_backend_output_arch_syms)
11919 (abfd, info, &flinfo,
11920 (out_sym_func) elf_link_output_symstrtab)))
11921 return FALSE;
11922 }
11923
11924 /* Finalize the .strtab section. */
11925 _bfd_elf_strtab_finalize (flinfo.symstrtab);
11926
11927 /* Swap out the .strtab section. */
11928 if (!elf_link_swap_symbols_out (&flinfo))
11929 return FALSE;
11930
11931 /* Now we know the size of the symtab section. */
11932 if (bfd_get_symcount (abfd) > 0)
11933 {
11934 /* Finish up and write out the symbol string table (.strtab)
11935 section. */
11936 Elf_Internal_Shdr *symstrtab_hdr;
11937 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
11938
11939 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
11940 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
11941 {
11942 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
11943 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
11944 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
11945 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
11946 symtab_shndx_hdr->sh_size = amt;
11947
11948 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
11949 off, TRUE);
11950
11951 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
11952 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
11953 return FALSE;
11954 }
11955
11956 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
11957 /* sh_name was set in prep_headers. */
11958 symstrtab_hdr->sh_type = SHT_STRTAB;
11959 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
11960 symstrtab_hdr->sh_addr = 0;
11961 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
11962 symstrtab_hdr->sh_entsize = 0;
11963 symstrtab_hdr->sh_link = 0;
11964 symstrtab_hdr->sh_info = 0;
11965 /* sh_offset is set just below. */
11966 symstrtab_hdr->sh_addralign = 1;
11967
11968 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
11969 off, TRUE);
11970 elf_next_file_pos (abfd) = off;
11971
11972 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
11973 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
11974 return FALSE;
11975 }
11976
11977 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
11978 {
11979 _bfd_error_handler (_("%B: failed to generate import library"),
11980 info->out_implib_bfd);
11981 return FALSE;
11982 }
11983
11984 /* Adjust the relocs to have the correct symbol indices. */
11985 for (o = abfd->sections; o != NULL; o = o->next)
11986 {
11987 struct bfd_elf_section_data *esdo = elf_section_data (o);
11988 bfd_boolean sort;
11989 if ((o->flags & SEC_RELOC) == 0)
11990 continue;
11991
11992 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
11993 if (esdo->rel.hdr != NULL
11994 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort))
11995 return FALSE;
11996 if (esdo->rela.hdr != NULL
11997 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort))
11998 return FALSE;
11999
12000 /* Set the reloc_count field to 0 to prevent write_relocs from
12001 trying to swap the relocs out itself. */
12002 o->reloc_count = 0;
12003 }
12004
12005 if (dynamic && info->combreloc && dynobj != NULL)
12006 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12007
12008 /* If we are linking against a dynamic object, or generating a
12009 shared library, finish up the dynamic linking information. */
12010 if (dynamic)
12011 {
12012 bfd_byte *dyncon, *dynconend;
12013
12014 /* Fix up .dynamic entries. */
12015 o = bfd_get_linker_section (dynobj, ".dynamic");
12016 BFD_ASSERT (o != NULL);
12017
12018 dyncon = o->contents;
12019 dynconend = o->contents + o->size;
12020 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12021 {
12022 Elf_Internal_Dyn dyn;
12023 const char *name;
12024 unsigned int type;
12025
12026 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12027
12028 switch (dyn.d_tag)
12029 {
12030 default:
12031 continue;
12032 case DT_NULL:
12033 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12034 {
12035 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12036 {
12037 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12038 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12039 default: continue;
12040 }
12041 dyn.d_un.d_val = relativecount;
12042 relativecount = 0;
12043 break;
12044 }
12045 continue;
12046
12047 case DT_INIT:
12048 name = info->init_function;
12049 goto get_sym;
12050 case DT_FINI:
12051 name = info->fini_function;
12052 get_sym:
12053 {
12054 struct elf_link_hash_entry *h;
12055
12056 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
12057 if (h != NULL
12058 && (h->root.type == bfd_link_hash_defined
12059 || h->root.type == bfd_link_hash_defweak))
12060 {
12061 dyn.d_un.d_ptr = h->root.u.def.value;
12062 o = h->root.u.def.section;
12063 if (o->output_section != NULL)
12064 dyn.d_un.d_ptr += (o->output_section->vma
12065 + o->output_offset);
12066 else
12067 {
12068 /* The symbol is imported from another shared
12069 library and does not apply to this one. */
12070 dyn.d_un.d_ptr = 0;
12071 }
12072 break;
12073 }
12074 }
12075 continue;
12076
12077 case DT_PREINIT_ARRAYSZ:
12078 name = ".preinit_array";
12079 goto get_out_size;
12080 case DT_INIT_ARRAYSZ:
12081 name = ".init_array";
12082 goto get_out_size;
12083 case DT_FINI_ARRAYSZ:
12084 name = ".fini_array";
12085 get_out_size:
12086 o = bfd_get_section_by_name (abfd, name);
12087 if (o == NULL)
12088 {
12089 _bfd_error_handler
12090 (_("could not find section %s"), name);
12091 goto error_return;
12092 }
12093 if (o->size == 0)
12094 _bfd_error_handler
12095 (_("warning: %s section has zero size"), name);
12096 dyn.d_un.d_val = o->size;
12097 break;
12098
12099 case DT_PREINIT_ARRAY:
12100 name = ".preinit_array";
12101 goto get_out_vma;
12102 case DT_INIT_ARRAY:
12103 name = ".init_array";
12104 goto get_out_vma;
12105 case DT_FINI_ARRAY:
12106 name = ".fini_array";
12107 get_out_vma:
12108 o = bfd_get_section_by_name (abfd, name);
12109 goto do_vma;
12110
12111 case DT_HASH:
12112 name = ".hash";
12113 goto get_vma;
12114 case DT_GNU_HASH:
12115 name = ".gnu.hash";
12116 goto get_vma;
12117 case DT_STRTAB:
12118 name = ".dynstr";
12119 goto get_vma;
12120 case DT_SYMTAB:
12121 name = ".dynsym";
12122 goto get_vma;
12123 case DT_VERDEF:
12124 name = ".gnu.version_d";
12125 goto get_vma;
12126 case DT_VERNEED:
12127 name = ".gnu.version_r";
12128 goto get_vma;
12129 case DT_VERSYM:
12130 name = ".gnu.version";
12131 get_vma:
12132 o = bfd_get_linker_section (dynobj, name);
12133 do_vma:
12134 if (o == NULL)
12135 {
12136 _bfd_error_handler
12137 (_("could not find section %s"), name);
12138 goto error_return;
12139 }
12140 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12141 {
12142 _bfd_error_handler
12143 (_("warning: section '%s' is being made into a note"), name);
12144 bfd_set_error (bfd_error_nonrepresentable_section);
12145 goto error_return;
12146 }
12147 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
12148 break;
12149
12150 case DT_REL:
12151 case DT_RELA:
12152 case DT_RELSZ:
12153 case DT_RELASZ:
12154 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12155 type = SHT_REL;
12156 else
12157 type = SHT_RELA;
12158 dyn.d_un.d_val = 0;
12159 dyn.d_un.d_ptr = 0;
12160 for (i = 1; i < elf_numsections (abfd); i++)
12161 {
12162 Elf_Internal_Shdr *hdr;
12163
12164 hdr = elf_elfsections (abfd)[i];
12165 if (hdr->sh_type == type
12166 && (hdr->sh_flags & SHF_ALLOC) != 0)
12167 {
12168 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12169 dyn.d_un.d_val += hdr->sh_size;
12170 else
12171 {
12172 if (dyn.d_un.d_ptr == 0
12173 || hdr->sh_addr < dyn.d_un.d_ptr)
12174 dyn.d_un.d_ptr = hdr->sh_addr;
12175 }
12176 }
12177 }
12178 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12179 {
12180 /* Don't count procedure linkage table relocs in the
12181 overall reloc count. */
12182 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12183 dyn.d_un.d_val -= htab->srelplt->size;
12184 /* If .rela.plt is the first .rela section, exclude
12185 it from DT_RELA. */
12186 else if (dyn.d_un.d_ptr == (htab->srelplt->output_section->vma
12187 + htab->srelplt->output_offset))
12188 dyn.d_un.d_ptr += htab->srelplt->size;
12189 }
12190 break;
12191 }
12192 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12193 }
12194 }
12195
12196 /* If we have created any dynamic sections, then output them. */
12197 if (dynobj != NULL)
12198 {
12199 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12200 goto error_return;
12201
12202 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12203 if (((info->warn_shared_textrel && bfd_link_pic (info))
12204 || info->error_textrel)
12205 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
12206 {
12207 bfd_byte *dyncon, *dynconend;
12208
12209 dyncon = o->contents;
12210 dynconend = o->contents + o->size;
12211 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12212 {
12213 Elf_Internal_Dyn dyn;
12214
12215 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12216
12217 if (dyn.d_tag == DT_TEXTREL)
12218 {
12219 if (info->error_textrel)
12220 info->callbacks->einfo
12221 (_("%P%X: read-only segment has dynamic relocations.\n"));
12222 else
12223 info->callbacks->einfo
12224 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
12225 break;
12226 }
12227 }
12228 }
12229
12230 for (o = dynobj->sections; o != NULL; o = o->next)
12231 {
12232 if ((o->flags & SEC_HAS_CONTENTS) == 0
12233 || o->size == 0
12234 || o->output_section == bfd_abs_section_ptr)
12235 continue;
12236 if ((o->flags & SEC_LINKER_CREATED) == 0)
12237 {
12238 /* At this point, we are only interested in sections
12239 created by _bfd_elf_link_create_dynamic_sections. */
12240 continue;
12241 }
12242 if (htab->stab_info.stabstr == o)
12243 continue;
12244 if (htab->eh_info.hdr_sec == o)
12245 continue;
12246 if (strcmp (o->name, ".dynstr") != 0)
12247 {
12248 if (! bfd_set_section_contents (abfd, o->output_section,
12249 o->contents,
12250 (file_ptr) o->output_offset
12251 * bfd_octets_per_byte (abfd),
12252 o->size))
12253 goto error_return;
12254 }
12255 else
12256 {
12257 /* The contents of the .dynstr section are actually in a
12258 stringtab. */
12259 file_ptr off;
12260
12261 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12262 if (bfd_seek (abfd, off, SEEK_SET) != 0
12263 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
12264 goto error_return;
12265 }
12266 }
12267 }
12268
12269 if (bfd_link_relocatable (info))
12270 {
12271 bfd_boolean failed = FALSE;
12272
12273 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12274 if (failed)
12275 goto error_return;
12276 }
12277
12278 /* If we have optimized stabs strings, output them. */
12279 if (htab->stab_info.stabstr != NULL)
12280 {
12281 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
12282 goto error_return;
12283 }
12284
12285 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12286 goto error_return;
12287
12288 elf_final_link_free (abfd, &flinfo);
12289
12290 elf_linker (abfd) = TRUE;
12291
12292 if (attr_section)
12293 {
12294 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
12295 if (contents == NULL)
12296 return FALSE; /* Bail out and fail. */
12297 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12298 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12299 free (contents);
12300 }
12301
12302 return TRUE;
12303
12304 error_return:
12305 elf_final_link_free (abfd, &flinfo);
12306 return FALSE;
12307 }
12308 \f
12309 /* Initialize COOKIE for input bfd ABFD. */
12310
12311 static bfd_boolean
12312 init_reloc_cookie (struct elf_reloc_cookie *cookie,
12313 struct bfd_link_info *info, bfd *abfd)
12314 {
12315 Elf_Internal_Shdr *symtab_hdr;
12316 const struct elf_backend_data *bed;
12317
12318 bed = get_elf_backend_data (abfd);
12319 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12320
12321 cookie->abfd = abfd;
12322 cookie->sym_hashes = elf_sym_hashes (abfd);
12323 cookie->bad_symtab = elf_bad_symtab (abfd);
12324 if (cookie->bad_symtab)
12325 {
12326 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12327 cookie->extsymoff = 0;
12328 }
12329 else
12330 {
12331 cookie->locsymcount = symtab_hdr->sh_info;
12332 cookie->extsymoff = symtab_hdr->sh_info;
12333 }
12334
12335 if (bed->s->arch_size == 32)
12336 cookie->r_sym_shift = 8;
12337 else
12338 cookie->r_sym_shift = 32;
12339
12340 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12341 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12342 {
12343 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12344 cookie->locsymcount, 0,
12345 NULL, NULL, NULL);
12346 if (cookie->locsyms == NULL)
12347 {
12348 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12349 return FALSE;
12350 }
12351 if (info->keep_memory)
12352 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12353 }
12354 return TRUE;
12355 }
12356
12357 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12358
12359 static void
12360 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12361 {
12362 Elf_Internal_Shdr *symtab_hdr;
12363
12364 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12365 if (cookie->locsyms != NULL
12366 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12367 free (cookie->locsyms);
12368 }
12369
12370 /* Initialize the relocation information in COOKIE for input section SEC
12371 of input bfd ABFD. */
12372
12373 static bfd_boolean
12374 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12375 struct bfd_link_info *info, bfd *abfd,
12376 asection *sec)
12377 {
12378 const struct elf_backend_data *bed;
12379
12380 if (sec->reloc_count == 0)
12381 {
12382 cookie->rels = NULL;
12383 cookie->relend = NULL;
12384 }
12385 else
12386 {
12387 bed = get_elf_backend_data (abfd);
12388
12389 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12390 info->keep_memory);
12391 if (cookie->rels == NULL)
12392 return FALSE;
12393 cookie->rel = cookie->rels;
12394 cookie->relend = (cookie->rels
12395 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
12396 }
12397 cookie->rel = cookie->rels;
12398 return TRUE;
12399 }
12400
12401 /* Free the memory allocated by init_reloc_cookie_rels,
12402 if appropriate. */
12403
12404 static void
12405 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12406 asection *sec)
12407 {
12408 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12409 free (cookie->rels);
12410 }
12411
12412 /* Initialize the whole of COOKIE for input section SEC. */
12413
12414 static bfd_boolean
12415 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12416 struct bfd_link_info *info,
12417 asection *sec)
12418 {
12419 if (!init_reloc_cookie (cookie, info, sec->owner))
12420 goto error1;
12421 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12422 goto error2;
12423 return TRUE;
12424
12425 error2:
12426 fini_reloc_cookie (cookie, sec->owner);
12427 error1:
12428 return FALSE;
12429 }
12430
12431 /* Free the memory allocated by init_reloc_cookie_for_section,
12432 if appropriate. */
12433
12434 static void
12435 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12436 asection *sec)
12437 {
12438 fini_reloc_cookie_rels (cookie, sec);
12439 fini_reloc_cookie (cookie, sec->owner);
12440 }
12441 \f
12442 /* Garbage collect unused sections. */
12443
12444 /* Default gc_mark_hook. */
12445
12446 asection *
12447 _bfd_elf_gc_mark_hook (asection *sec,
12448 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12449 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12450 struct elf_link_hash_entry *h,
12451 Elf_Internal_Sym *sym)
12452 {
12453 if (h != NULL)
12454 {
12455 switch (h->root.type)
12456 {
12457 case bfd_link_hash_defined:
12458 case bfd_link_hash_defweak:
12459 return h->root.u.def.section;
12460
12461 case bfd_link_hash_common:
12462 return h->root.u.c.p->section;
12463
12464 default:
12465 break;
12466 }
12467 }
12468 else
12469 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12470
12471 return NULL;
12472 }
12473
12474 /* For undefined __start_<name> and __stop_<name> symbols, return the
12475 first input section matching <name>. Return NULL otherwise. */
12476
12477 asection *
12478 _bfd_elf_is_start_stop (const struct bfd_link_info *info,
12479 struct elf_link_hash_entry *h)
12480 {
12481 asection *s;
12482 const char *sec_name;
12483
12484 if (h->root.type != bfd_link_hash_undefined
12485 && h->root.type != bfd_link_hash_undefweak)
12486 return NULL;
12487
12488 s = h->root.u.undef.section;
12489 if (s != NULL)
12490 {
12491 if (s == (asection *) 0 - 1)
12492 return NULL;
12493 return s;
12494 }
12495
12496 sec_name = NULL;
12497 if (strncmp (h->root.root.string, "__start_", 8) == 0)
12498 sec_name = h->root.root.string + 8;
12499 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
12500 sec_name = h->root.root.string + 7;
12501
12502 if (sec_name != NULL && *sec_name != '\0')
12503 {
12504 bfd *i;
12505
12506 for (i = info->input_bfds; i != NULL; i = i->link.next)
12507 {
12508 s = bfd_get_section_by_name (i, sec_name);
12509 if (s != NULL)
12510 {
12511 h->root.u.undef.section = s;
12512 break;
12513 }
12514 }
12515 }
12516
12517 if (s == NULL)
12518 h->root.u.undef.section = (asection *) 0 - 1;
12519
12520 return s;
12521 }
12522
12523 /* COOKIE->rel describes a relocation against section SEC, which is
12524 a section we've decided to keep. Return the section that contains
12525 the relocation symbol, or NULL if no section contains it. */
12526
12527 asection *
12528 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12529 elf_gc_mark_hook_fn gc_mark_hook,
12530 struct elf_reloc_cookie *cookie,
12531 bfd_boolean *start_stop)
12532 {
12533 unsigned long r_symndx;
12534 struct elf_link_hash_entry *h;
12535
12536 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
12537 if (r_symndx == STN_UNDEF)
12538 return NULL;
12539
12540 if (r_symndx >= cookie->locsymcount
12541 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12542 {
12543 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
12544 if (h == NULL)
12545 {
12546 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12547 sec->owner);
12548 return NULL;
12549 }
12550 while (h->root.type == bfd_link_hash_indirect
12551 || h->root.type == bfd_link_hash_warning)
12552 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12553 h->mark = 1;
12554 /* If this symbol is weak and there is a non-weak definition, we
12555 keep the non-weak definition because many backends put
12556 dynamic reloc info on the non-weak definition for code
12557 handling copy relocs. */
12558 if (h->u.weakdef != NULL)
12559 h->u.weakdef->mark = 1;
12560
12561 if (start_stop != NULL)
12562 {
12563 /* To work around a glibc bug, mark all XXX input sections
12564 when there is an as yet undefined reference to __start_XXX
12565 or __stop_XXX symbols. The linker will later define such
12566 symbols for orphan input sections that have a name
12567 representable as a C identifier. */
12568 asection *s = _bfd_elf_is_start_stop (info, h);
12569
12570 if (s != NULL)
12571 {
12572 *start_stop = !s->gc_mark;
12573 return s;
12574 }
12575 }
12576
12577 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12578 }
12579
12580 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12581 &cookie->locsyms[r_symndx]);
12582 }
12583
12584 /* COOKIE->rel describes a relocation against section SEC, which is
12585 a section we've decided to keep. Mark the section that contains
12586 the relocation symbol. */
12587
12588 bfd_boolean
12589 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12590 asection *sec,
12591 elf_gc_mark_hook_fn gc_mark_hook,
12592 struct elf_reloc_cookie *cookie)
12593 {
12594 asection *rsec;
12595 bfd_boolean start_stop = FALSE;
12596
12597 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12598 while (rsec != NULL)
12599 {
12600 if (!rsec->gc_mark)
12601 {
12602 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12603 || (rsec->owner->flags & DYNAMIC) != 0)
12604 rsec->gc_mark = 1;
12605 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12606 return FALSE;
12607 }
12608 if (!start_stop)
12609 break;
12610 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
12611 }
12612 return TRUE;
12613 }
12614
12615 /* The mark phase of garbage collection. For a given section, mark
12616 it and any sections in this section's group, and all the sections
12617 which define symbols to which it refers. */
12618
12619 bfd_boolean
12620 _bfd_elf_gc_mark (struct bfd_link_info *info,
12621 asection *sec,
12622 elf_gc_mark_hook_fn gc_mark_hook)
12623 {
12624 bfd_boolean ret;
12625 asection *group_sec, *eh_frame;
12626
12627 sec->gc_mark = 1;
12628
12629 /* Mark all the sections in the group. */
12630 group_sec = elf_section_data (sec)->next_in_group;
12631 if (group_sec && !group_sec->gc_mark)
12632 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
12633 return FALSE;
12634
12635 /* Look through the section relocs. */
12636 ret = TRUE;
12637 eh_frame = elf_eh_frame_section (sec->owner);
12638 if ((sec->flags & SEC_RELOC) != 0
12639 && sec->reloc_count > 0
12640 && sec != eh_frame)
12641 {
12642 struct elf_reloc_cookie cookie;
12643
12644 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12645 ret = FALSE;
12646 else
12647 {
12648 for (; cookie.rel < cookie.relend; cookie.rel++)
12649 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
12650 {
12651 ret = FALSE;
12652 break;
12653 }
12654 fini_reloc_cookie_for_section (&cookie, sec);
12655 }
12656 }
12657
12658 if (ret && eh_frame && elf_fde_list (sec))
12659 {
12660 struct elf_reloc_cookie cookie;
12661
12662 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12663 ret = FALSE;
12664 else
12665 {
12666 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12667 gc_mark_hook, &cookie))
12668 ret = FALSE;
12669 fini_reloc_cookie_for_section (&cookie, eh_frame);
12670 }
12671 }
12672
12673 eh_frame = elf_section_eh_frame_entry (sec);
12674 if (ret && eh_frame && !eh_frame->gc_mark)
12675 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12676 ret = FALSE;
12677
12678 return ret;
12679 }
12680
12681 /* Scan and mark sections in a special or debug section group. */
12682
12683 static void
12684 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12685 {
12686 /* Point to first section of section group. */
12687 asection *ssec;
12688 /* Used to iterate the section group. */
12689 asection *msec;
12690
12691 bfd_boolean is_special_grp = TRUE;
12692 bfd_boolean is_debug_grp = TRUE;
12693
12694 /* First scan to see if group contains any section other than debug
12695 and special section. */
12696 ssec = msec = elf_next_in_group (grp);
12697 do
12698 {
12699 if ((msec->flags & SEC_DEBUGGING) == 0)
12700 is_debug_grp = FALSE;
12701
12702 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12703 is_special_grp = FALSE;
12704
12705 msec = elf_next_in_group (msec);
12706 }
12707 while (msec != ssec);
12708
12709 /* If this is a pure debug section group or pure special section group,
12710 keep all sections in this group. */
12711 if (is_debug_grp || is_special_grp)
12712 {
12713 do
12714 {
12715 msec->gc_mark = 1;
12716 msec = elf_next_in_group (msec);
12717 }
12718 while (msec != ssec);
12719 }
12720 }
12721
12722 /* Keep debug and special sections. */
12723
12724 bfd_boolean
12725 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12726 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12727 {
12728 bfd *ibfd;
12729
12730 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12731 {
12732 asection *isec;
12733 bfd_boolean some_kept;
12734 bfd_boolean debug_frag_seen;
12735
12736 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12737 continue;
12738
12739 /* Ensure all linker created sections are kept,
12740 see if any other section is already marked,
12741 and note if we have any fragmented debug sections. */
12742 debug_frag_seen = some_kept = FALSE;
12743 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12744 {
12745 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12746 isec->gc_mark = 1;
12747 else if (isec->gc_mark)
12748 some_kept = TRUE;
12749
12750 if (debug_frag_seen == FALSE
12751 && (isec->flags & SEC_DEBUGGING)
12752 && CONST_STRNEQ (isec->name, ".debug_line."))
12753 debug_frag_seen = TRUE;
12754 }
12755
12756 /* If no section in this file will be kept, then we can
12757 toss out the debug and special sections. */
12758 if (!some_kept)
12759 continue;
12760
12761 /* Keep debug and special sections like .comment when they are
12762 not part of a group. Also keep section groups that contain
12763 just debug sections or special sections. */
12764 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12765 {
12766 if ((isec->flags & SEC_GROUP) != 0)
12767 _bfd_elf_gc_mark_debug_special_section_group (isec);
12768 else if (((isec->flags & SEC_DEBUGGING) != 0
12769 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12770 && elf_next_in_group (isec) == NULL)
12771 isec->gc_mark = 1;
12772 }
12773
12774 if (! debug_frag_seen)
12775 continue;
12776
12777 /* Look for CODE sections which are going to be discarded,
12778 and find and discard any fragmented debug sections which
12779 are associated with that code section. */
12780 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12781 if ((isec->flags & SEC_CODE) != 0
12782 && isec->gc_mark == 0)
12783 {
12784 unsigned int ilen;
12785 asection *dsec;
12786
12787 ilen = strlen (isec->name);
12788
12789 /* Association is determined by the name of the debug section
12790 containing the name of the code section as a suffix. For
12791 example .debug_line.text.foo is a debug section associated
12792 with .text.foo. */
12793 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12794 {
12795 unsigned int dlen;
12796
12797 if (dsec->gc_mark == 0
12798 || (dsec->flags & SEC_DEBUGGING) == 0)
12799 continue;
12800
12801 dlen = strlen (dsec->name);
12802
12803 if (dlen > ilen
12804 && strncmp (dsec->name + (dlen - ilen),
12805 isec->name, ilen) == 0)
12806 {
12807 dsec->gc_mark = 0;
12808 }
12809 }
12810 }
12811 }
12812 return TRUE;
12813 }
12814
12815 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
12816
12817 struct elf_gc_sweep_symbol_info
12818 {
12819 struct bfd_link_info *info;
12820 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
12821 bfd_boolean);
12822 };
12823
12824 static bfd_boolean
12825 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
12826 {
12827 if (!h->mark
12828 && (((h->root.type == bfd_link_hash_defined
12829 || h->root.type == bfd_link_hash_defweak)
12830 && !((h->def_regular || ELF_COMMON_DEF_P (h))
12831 && h->root.u.def.section->gc_mark))
12832 || h->root.type == bfd_link_hash_undefined
12833 || h->root.type == bfd_link_hash_undefweak))
12834 {
12835 struct elf_gc_sweep_symbol_info *inf;
12836
12837 inf = (struct elf_gc_sweep_symbol_info *) data;
12838 (*inf->hide_symbol) (inf->info, h, TRUE);
12839 h->def_regular = 0;
12840 h->ref_regular = 0;
12841 h->ref_regular_nonweak = 0;
12842 }
12843
12844 return TRUE;
12845 }
12846
12847 /* The sweep phase of garbage collection. Remove all garbage sections. */
12848
12849 typedef bfd_boolean (*gc_sweep_hook_fn)
12850 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
12851
12852 static bfd_boolean
12853 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
12854 {
12855 bfd *sub;
12856 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12857 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
12858 unsigned long section_sym_count;
12859 struct elf_gc_sweep_symbol_info sweep_info;
12860
12861 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12862 {
12863 asection *o;
12864
12865 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12866 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
12867 continue;
12868
12869 for (o = sub->sections; o != NULL; o = o->next)
12870 {
12871 /* When any section in a section group is kept, we keep all
12872 sections in the section group. If the first member of
12873 the section group is excluded, we will also exclude the
12874 group section. */
12875 if (o->flags & SEC_GROUP)
12876 {
12877 asection *first = elf_next_in_group (o);
12878 o->gc_mark = first->gc_mark;
12879 }
12880
12881 if (o->gc_mark)
12882 continue;
12883
12884 /* Skip sweeping sections already excluded. */
12885 if (o->flags & SEC_EXCLUDE)
12886 continue;
12887
12888 /* Since this is early in the link process, it is simple
12889 to remove a section from the output. */
12890 o->flags |= SEC_EXCLUDE;
12891
12892 if (info->print_gc_sections && o->size != 0)
12893 /* xgettext:c-format */
12894 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
12895
12896 /* But we also have to update some of the relocation
12897 info we collected before. */
12898 if (gc_sweep_hook
12899 && (o->flags & SEC_RELOC) != 0
12900 && o->reloc_count != 0
12901 && !((info->strip == strip_all || info->strip == strip_debugger)
12902 && (o->flags & SEC_DEBUGGING) != 0)
12903 && !bfd_is_abs_section (o->output_section))
12904 {
12905 Elf_Internal_Rela *internal_relocs;
12906 bfd_boolean r;
12907
12908 internal_relocs
12909 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
12910 info->keep_memory);
12911 if (internal_relocs == NULL)
12912 return FALSE;
12913
12914 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
12915
12916 if (elf_section_data (o)->relocs != internal_relocs)
12917 free (internal_relocs);
12918
12919 if (!r)
12920 return FALSE;
12921 }
12922 }
12923 }
12924
12925 /* Remove the symbols that were in the swept sections from the dynamic
12926 symbol table. GCFIXME: Anyone know how to get them out of the
12927 static symbol table as well? */
12928 sweep_info.info = info;
12929 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
12930 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
12931 &sweep_info);
12932
12933 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
12934 return TRUE;
12935 }
12936
12937 /* Propagate collected vtable information. This is called through
12938 elf_link_hash_traverse. */
12939
12940 static bfd_boolean
12941 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
12942 {
12943 /* Those that are not vtables. */
12944 if (h->vtable == NULL || h->vtable->parent == NULL)
12945 return TRUE;
12946
12947 /* Those vtables that do not have parents, we cannot merge. */
12948 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
12949 return TRUE;
12950
12951 /* If we've already been done, exit. */
12952 if (h->vtable->used && h->vtable->used[-1])
12953 return TRUE;
12954
12955 /* Make sure the parent's table is up to date. */
12956 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
12957
12958 if (h->vtable->used == NULL)
12959 {
12960 /* None of this table's entries were referenced. Re-use the
12961 parent's table. */
12962 h->vtable->used = h->vtable->parent->vtable->used;
12963 h->vtable->size = h->vtable->parent->vtable->size;
12964 }
12965 else
12966 {
12967 size_t n;
12968 bfd_boolean *cu, *pu;
12969
12970 /* Or the parent's entries into ours. */
12971 cu = h->vtable->used;
12972 cu[-1] = TRUE;
12973 pu = h->vtable->parent->vtable->used;
12974 if (pu != NULL)
12975 {
12976 const struct elf_backend_data *bed;
12977 unsigned int log_file_align;
12978
12979 bed = get_elf_backend_data (h->root.u.def.section->owner);
12980 log_file_align = bed->s->log_file_align;
12981 n = h->vtable->parent->vtable->size >> log_file_align;
12982 while (n--)
12983 {
12984 if (*pu)
12985 *cu = TRUE;
12986 pu++;
12987 cu++;
12988 }
12989 }
12990 }
12991
12992 return TRUE;
12993 }
12994
12995 static bfd_boolean
12996 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
12997 {
12998 asection *sec;
12999 bfd_vma hstart, hend;
13000 Elf_Internal_Rela *relstart, *relend, *rel;
13001 const struct elf_backend_data *bed;
13002 unsigned int log_file_align;
13003
13004 /* Take care of both those symbols that do not describe vtables as
13005 well as those that are not loaded. */
13006 if (h->vtable == NULL || h->vtable->parent == NULL)
13007 return TRUE;
13008
13009 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13010 || h->root.type == bfd_link_hash_defweak);
13011
13012 sec = h->root.u.def.section;
13013 hstart = h->root.u.def.value;
13014 hend = hstart + h->size;
13015
13016 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13017 if (!relstart)
13018 return *(bfd_boolean *) okp = FALSE;
13019 bed = get_elf_backend_data (sec->owner);
13020 log_file_align = bed->s->log_file_align;
13021
13022 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
13023
13024 for (rel = relstart; rel < relend; ++rel)
13025 if (rel->r_offset >= hstart && rel->r_offset < hend)
13026 {
13027 /* If the entry is in use, do nothing. */
13028 if (h->vtable->used
13029 && (rel->r_offset - hstart) < h->vtable->size)
13030 {
13031 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13032 if (h->vtable->used[entry])
13033 continue;
13034 }
13035 /* Otherwise, kill it. */
13036 rel->r_offset = rel->r_info = rel->r_addend = 0;
13037 }
13038
13039 return TRUE;
13040 }
13041
13042 /* Mark sections containing dynamically referenced symbols. When
13043 building shared libraries, we must assume that any visible symbol is
13044 referenced. */
13045
13046 bfd_boolean
13047 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
13048 {
13049 struct bfd_link_info *info = (struct bfd_link_info *) inf;
13050 struct bfd_elf_dynamic_list *d = info->dynamic_list;
13051
13052 if ((h->root.type == bfd_link_hash_defined
13053 || h->root.type == bfd_link_hash_defweak)
13054 && (h->ref_dynamic
13055 || ((h->def_regular || ELF_COMMON_DEF_P (h))
13056 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
13057 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
13058 && (!bfd_link_executable (info)
13059 || info->export_dynamic
13060 || (h->dynamic
13061 && d != NULL
13062 && (*d->match) (&d->head, NULL, h->root.root.string)))
13063 && (h->versioned >= versioned
13064 || !bfd_hide_sym_by_version (info->version_info,
13065 h->root.root.string)))))
13066 h->root.u.def.section->flags |= SEC_KEEP;
13067
13068 return TRUE;
13069 }
13070
13071 /* Keep all sections containing symbols undefined on the command-line,
13072 and the section containing the entry symbol. */
13073
13074 void
13075 _bfd_elf_gc_keep (struct bfd_link_info *info)
13076 {
13077 struct bfd_sym_chain *sym;
13078
13079 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13080 {
13081 struct elf_link_hash_entry *h;
13082
13083 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13084 FALSE, FALSE, FALSE);
13085
13086 if (h != NULL
13087 && (h->root.type == bfd_link_hash_defined
13088 || h->root.type == bfd_link_hash_defweak)
13089 && !bfd_is_abs_section (h->root.u.def.section)
13090 && !bfd_is_und_section (h->root.u.def.section))
13091 h->root.u.def.section->flags |= SEC_KEEP;
13092 }
13093 }
13094
13095 bfd_boolean
13096 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13097 struct bfd_link_info *info)
13098 {
13099 bfd *ibfd = info->input_bfds;
13100
13101 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13102 {
13103 asection *sec;
13104 struct elf_reloc_cookie cookie;
13105
13106 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13107 continue;
13108
13109 if (!init_reloc_cookie (&cookie, info, ibfd))
13110 return FALSE;
13111
13112 for (sec = ibfd->sections; sec; sec = sec->next)
13113 {
13114 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13115 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13116 {
13117 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13118 fini_reloc_cookie_rels (&cookie, sec);
13119 }
13120 }
13121 }
13122 return TRUE;
13123 }
13124
13125 /* Do mark and sweep of unused sections. */
13126
13127 bfd_boolean
13128 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13129 {
13130 bfd_boolean ok = TRUE;
13131 bfd *sub;
13132 elf_gc_mark_hook_fn gc_mark_hook;
13133 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13134 struct elf_link_hash_table *htab;
13135
13136 if (!bed->can_gc_sections
13137 || !is_elf_hash_table (info->hash))
13138 {
13139 _bfd_error_handler(_("Warning: gc-sections option ignored"));
13140 return TRUE;
13141 }
13142
13143 bed->gc_keep (info);
13144 htab = elf_hash_table (info);
13145
13146 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13147 at the .eh_frame section if we can mark the FDEs individually. */
13148 for (sub = info->input_bfds;
13149 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13150 sub = sub->link.next)
13151 {
13152 asection *sec;
13153 struct elf_reloc_cookie cookie;
13154
13155 sec = bfd_get_section_by_name (sub, ".eh_frame");
13156 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
13157 {
13158 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
13159 if (elf_section_data (sec)->sec_info
13160 && (sec->flags & SEC_LINKER_CREATED) == 0)
13161 elf_eh_frame_section (sub) = sec;
13162 fini_reloc_cookie_for_section (&cookie, sec);
13163 sec = bfd_get_next_section_by_name (NULL, sec);
13164 }
13165 }
13166
13167 /* Apply transitive closure to the vtable entry usage info. */
13168 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
13169 if (!ok)
13170 return FALSE;
13171
13172 /* Kill the vtable relocations that were not used. */
13173 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
13174 if (!ok)
13175 return FALSE;
13176
13177 /* Mark dynamically referenced symbols. */
13178 if (htab->dynamic_sections_created)
13179 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
13180
13181 /* Grovel through relocs to find out who stays ... */
13182 gc_mark_hook = bed->gc_mark_hook;
13183 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13184 {
13185 asection *o;
13186
13187 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13188 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13189 continue;
13190
13191 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13192 Also treat note sections as a root, if the section is not part
13193 of a group. */
13194 for (o = sub->sections; o != NULL; o = o->next)
13195 if (!o->gc_mark
13196 && (o->flags & SEC_EXCLUDE) == 0
13197 && ((o->flags & SEC_KEEP) != 0
13198 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13199 && elf_next_in_group (o) == NULL )))
13200 {
13201 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13202 return FALSE;
13203 }
13204 }
13205
13206 /* Allow the backend to mark additional target specific sections. */
13207 bed->gc_mark_extra_sections (info, gc_mark_hook);
13208
13209 /* ... and mark SEC_EXCLUDE for those that go. */
13210 return elf_gc_sweep (abfd, info);
13211 }
13212 \f
13213 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13214
13215 bfd_boolean
13216 bfd_elf_gc_record_vtinherit (bfd *abfd,
13217 asection *sec,
13218 struct elf_link_hash_entry *h,
13219 bfd_vma offset)
13220 {
13221 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13222 struct elf_link_hash_entry **search, *child;
13223 size_t extsymcount;
13224 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13225
13226 /* The sh_info field of the symtab header tells us where the
13227 external symbols start. We don't care about the local symbols at
13228 this point. */
13229 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13230 if (!elf_bad_symtab (abfd))
13231 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13232
13233 sym_hashes = elf_sym_hashes (abfd);
13234 sym_hashes_end = sym_hashes + extsymcount;
13235
13236 /* Hunt down the child symbol, which is in this section at the same
13237 offset as the relocation. */
13238 for (search = sym_hashes; search != sym_hashes_end; ++search)
13239 {
13240 if ((child = *search) != NULL
13241 && (child->root.type == bfd_link_hash_defined
13242 || child->root.type == bfd_link_hash_defweak)
13243 && child->root.u.def.section == sec
13244 && child->root.u.def.value == offset)
13245 goto win;
13246 }
13247
13248 /* xgettext:c-format */
13249 _bfd_error_handler (_("%B: %A+%lu: No symbol found for INHERIT"),
13250 abfd, sec, (unsigned long) offset);
13251 bfd_set_error (bfd_error_invalid_operation);
13252 return FALSE;
13253
13254 win:
13255 if (!child->vtable)
13256 {
13257 child->vtable = ((struct elf_link_virtual_table_entry *)
13258 bfd_zalloc (abfd, sizeof (*child->vtable)));
13259 if (!child->vtable)
13260 return FALSE;
13261 }
13262 if (!h)
13263 {
13264 /* This *should* only be the absolute section. It could potentially
13265 be that someone has defined a non-global vtable though, which
13266 would be bad. It isn't worth paging in the local symbols to be
13267 sure though; that case should simply be handled by the assembler. */
13268
13269 child->vtable->parent = (struct elf_link_hash_entry *) -1;
13270 }
13271 else
13272 child->vtable->parent = h;
13273
13274 return TRUE;
13275 }
13276
13277 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13278
13279 bfd_boolean
13280 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13281 asection *sec ATTRIBUTE_UNUSED,
13282 struct elf_link_hash_entry *h,
13283 bfd_vma addend)
13284 {
13285 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13286 unsigned int log_file_align = bed->s->log_file_align;
13287
13288 if (!h->vtable)
13289 {
13290 h->vtable = ((struct elf_link_virtual_table_entry *)
13291 bfd_zalloc (abfd, sizeof (*h->vtable)));
13292 if (!h->vtable)
13293 return FALSE;
13294 }
13295
13296 if (addend >= h->vtable->size)
13297 {
13298 size_t size, bytes, file_align;
13299 bfd_boolean *ptr = h->vtable->used;
13300
13301 /* While the symbol is undefined, we have to be prepared to handle
13302 a zero size. */
13303 file_align = 1 << log_file_align;
13304 if (h->root.type == bfd_link_hash_undefined)
13305 size = addend + file_align;
13306 else
13307 {
13308 size = h->size;
13309 if (addend >= size)
13310 {
13311 /* Oops! We've got a reference past the defined end of
13312 the table. This is probably a bug -- shall we warn? */
13313 size = addend + file_align;
13314 }
13315 }
13316 size = (size + file_align - 1) & -file_align;
13317
13318 /* Allocate one extra entry for use as a "done" flag for the
13319 consolidation pass. */
13320 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13321
13322 if (ptr)
13323 {
13324 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
13325
13326 if (ptr != NULL)
13327 {
13328 size_t oldbytes;
13329
13330 oldbytes = (((h->vtable->size >> log_file_align) + 1)
13331 * sizeof (bfd_boolean));
13332 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13333 }
13334 }
13335 else
13336 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
13337
13338 if (ptr == NULL)
13339 return FALSE;
13340
13341 /* And arrange for that done flag to be at index -1. */
13342 h->vtable->used = ptr + 1;
13343 h->vtable->size = size;
13344 }
13345
13346 h->vtable->used[addend >> log_file_align] = TRUE;
13347
13348 return TRUE;
13349 }
13350
13351 /* Map an ELF section header flag to its corresponding string. */
13352 typedef struct
13353 {
13354 char *flag_name;
13355 flagword flag_value;
13356 } elf_flags_to_name_table;
13357
13358 static elf_flags_to_name_table elf_flags_to_names [] =
13359 {
13360 { "SHF_WRITE", SHF_WRITE },
13361 { "SHF_ALLOC", SHF_ALLOC },
13362 { "SHF_EXECINSTR", SHF_EXECINSTR },
13363 { "SHF_MERGE", SHF_MERGE },
13364 { "SHF_STRINGS", SHF_STRINGS },
13365 { "SHF_INFO_LINK", SHF_INFO_LINK},
13366 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13367 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13368 { "SHF_GROUP", SHF_GROUP },
13369 { "SHF_TLS", SHF_TLS },
13370 { "SHF_MASKOS", SHF_MASKOS },
13371 { "SHF_EXCLUDE", SHF_EXCLUDE },
13372 };
13373
13374 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13375 bfd_boolean
13376 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
13377 struct flag_info *flaginfo,
13378 asection *section)
13379 {
13380 const bfd_vma sh_flags = elf_section_flags (section);
13381
13382 if (!flaginfo->flags_initialized)
13383 {
13384 bfd *obfd = info->output_bfd;
13385 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13386 struct flag_info_list *tf = flaginfo->flag_list;
13387 int with_hex = 0;
13388 int without_hex = 0;
13389
13390 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13391 {
13392 unsigned i;
13393 flagword (*lookup) (char *);
13394
13395 lookup = bed->elf_backend_lookup_section_flags_hook;
13396 if (lookup != NULL)
13397 {
13398 flagword hexval = (*lookup) ((char *) tf->name);
13399
13400 if (hexval != 0)
13401 {
13402 if (tf->with == with_flags)
13403 with_hex |= hexval;
13404 else if (tf->with == without_flags)
13405 without_hex |= hexval;
13406 tf->valid = TRUE;
13407 continue;
13408 }
13409 }
13410 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13411 {
13412 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
13413 {
13414 if (tf->with == with_flags)
13415 with_hex |= elf_flags_to_names[i].flag_value;
13416 else if (tf->with == without_flags)
13417 without_hex |= elf_flags_to_names[i].flag_value;
13418 tf->valid = TRUE;
13419 break;
13420 }
13421 }
13422 if (!tf->valid)
13423 {
13424 info->callbacks->einfo
13425 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
13426 return FALSE;
13427 }
13428 }
13429 flaginfo->flags_initialized = TRUE;
13430 flaginfo->only_with_flags |= with_hex;
13431 flaginfo->not_with_flags |= without_hex;
13432 }
13433
13434 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13435 return FALSE;
13436
13437 if ((flaginfo->not_with_flags & sh_flags) != 0)
13438 return FALSE;
13439
13440 return TRUE;
13441 }
13442
13443 struct alloc_got_off_arg {
13444 bfd_vma gotoff;
13445 struct bfd_link_info *info;
13446 };
13447
13448 /* We need a special top-level link routine to convert got reference counts
13449 to real got offsets. */
13450
13451 static bfd_boolean
13452 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13453 {
13454 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13455 bfd *obfd = gofarg->info->output_bfd;
13456 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13457
13458 if (h->got.refcount > 0)
13459 {
13460 h->got.offset = gofarg->gotoff;
13461 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13462 }
13463 else
13464 h->got.offset = (bfd_vma) -1;
13465
13466 return TRUE;
13467 }
13468
13469 /* And an accompanying bit to work out final got entry offsets once
13470 we're done. Should be called from final_link. */
13471
13472 bfd_boolean
13473 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13474 struct bfd_link_info *info)
13475 {
13476 bfd *i;
13477 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13478 bfd_vma gotoff;
13479 struct alloc_got_off_arg gofarg;
13480
13481 BFD_ASSERT (abfd == info->output_bfd);
13482
13483 if (! is_elf_hash_table (info->hash))
13484 return FALSE;
13485
13486 /* The GOT offset is relative to the .got section, but the GOT header is
13487 put into the .got.plt section, if the backend uses it. */
13488 if (bed->want_got_plt)
13489 gotoff = 0;
13490 else
13491 gotoff = bed->got_header_size;
13492
13493 /* Do the local .got entries first. */
13494 for (i = info->input_bfds; i; i = i->link.next)
13495 {
13496 bfd_signed_vma *local_got;
13497 size_t j, locsymcount;
13498 Elf_Internal_Shdr *symtab_hdr;
13499
13500 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13501 continue;
13502
13503 local_got = elf_local_got_refcounts (i);
13504 if (!local_got)
13505 continue;
13506
13507 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13508 if (elf_bad_symtab (i))
13509 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13510 else
13511 locsymcount = symtab_hdr->sh_info;
13512
13513 for (j = 0; j < locsymcount; ++j)
13514 {
13515 if (local_got[j] > 0)
13516 {
13517 local_got[j] = gotoff;
13518 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
13519 }
13520 else
13521 local_got[j] = (bfd_vma) -1;
13522 }
13523 }
13524
13525 /* Then the global .got entries. .plt refcounts are handled by
13526 adjust_dynamic_symbol */
13527 gofarg.gotoff = gotoff;
13528 gofarg.info = info;
13529 elf_link_hash_traverse (elf_hash_table (info),
13530 elf_gc_allocate_got_offsets,
13531 &gofarg);
13532 return TRUE;
13533 }
13534
13535 /* Many folk need no more in the way of final link than this, once
13536 got entry reference counting is enabled. */
13537
13538 bfd_boolean
13539 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13540 {
13541 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13542 return FALSE;
13543
13544 /* Invoke the regular ELF backend linker to do all the work. */
13545 return bfd_elf_final_link (abfd, info);
13546 }
13547
13548 bfd_boolean
13549 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13550 {
13551 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
13552
13553 if (rcookie->bad_symtab)
13554 rcookie->rel = rcookie->rels;
13555
13556 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13557 {
13558 unsigned long r_symndx;
13559
13560 if (! rcookie->bad_symtab)
13561 if (rcookie->rel->r_offset > offset)
13562 return FALSE;
13563 if (rcookie->rel->r_offset != offset)
13564 continue;
13565
13566 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
13567 if (r_symndx == STN_UNDEF)
13568 return TRUE;
13569
13570 if (r_symndx >= rcookie->locsymcount
13571 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13572 {
13573 struct elf_link_hash_entry *h;
13574
13575 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13576
13577 while (h->root.type == bfd_link_hash_indirect
13578 || h->root.type == bfd_link_hash_warning)
13579 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13580
13581 if ((h->root.type == bfd_link_hash_defined
13582 || h->root.type == bfd_link_hash_defweak)
13583 && (h->root.u.def.section->owner != rcookie->abfd
13584 || h->root.u.def.section->kept_section != NULL
13585 || discarded_section (h->root.u.def.section)))
13586 return TRUE;
13587 }
13588 else
13589 {
13590 /* It's not a relocation against a global symbol,
13591 but it could be a relocation against a local
13592 symbol for a discarded section. */
13593 asection *isec;
13594 Elf_Internal_Sym *isym;
13595
13596 /* Need to: get the symbol; get the section. */
13597 isym = &rcookie->locsyms[r_symndx];
13598 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
13599 if (isec != NULL
13600 && (isec->kept_section != NULL
13601 || discarded_section (isec)))
13602 return TRUE;
13603 }
13604 return FALSE;
13605 }
13606 return FALSE;
13607 }
13608
13609 /* Discard unneeded references to discarded sections.
13610 Returns -1 on error, 1 if any section's size was changed, 0 if
13611 nothing changed. This function assumes that the relocations are in
13612 sorted order, which is true for all known assemblers. */
13613
13614 int
13615 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13616 {
13617 struct elf_reloc_cookie cookie;
13618 asection *o;
13619 bfd *abfd;
13620 int changed = 0;
13621
13622 if (info->traditional_format
13623 || !is_elf_hash_table (info->hash))
13624 return 0;
13625
13626 o = bfd_get_section_by_name (output_bfd, ".stab");
13627 if (o != NULL)
13628 {
13629 asection *i;
13630
13631 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13632 {
13633 if (i->size == 0
13634 || i->reloc_count == 0
13635 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13636 continue;
13637
13638 abfd = i->owner;
13639 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13640 continue;
13641
13642 if (!init_reloc_cookie_for_section (&cookie, info, i))
13643 return -1;
13644
13645 if (_bfd_discard_section_stabs (abfd, i,
13646 elf_section_data (i)->sec_info,
13647 bfd_elf_reloc_symbol_deleted_p,
13648 &cookie))
13649 changed = 1;
13650
13651 fini_reloc_cookie_for_section (&cookie, i);
13652 }
13653 }
13654
13655 o = NULL;
13656 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13657 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
13658 if (o != NULL)
13659 {
13660 asection *i;
13661
13662 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13663 {
13664 if (i->size == 0)
13665 continue;
13666
13667 abfd = i->owner;
13668 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13669 continue;
13670
13671 if (!init_reloc_cookie_for_section (&cookie, info, i))
13672 return -1;
13673
13674 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13675 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
13676 bfd_elf_reloc_symbol_deleted_p,
13677 &cookie))
13678 changed = 1;
13679
13680 fini_reloc_cookie_for_section (&cookie, i);
13681 }
13682 }
13683
13684 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13685 {
13686 const struct elf_backend_data *bed;
13687
13688 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13689 continue;
13690
13691 bed = get_elf_backend_data (abfd);
13692
13693 if (bed->elf_backend_discard_info != NULL)
13694 {
13695 if (!init_reloc_cookie (&cookie, info, abfd))
13696 return -1;
13697
13698 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
13699 changed = 1;
13700
13701 fini_reloc_cookie (&cookie, abfd);
13702 }
13703 }
13704
13705 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13706 _bfd_elf_end_eh_frame_parsing (info);
13707
13708 if (info->eh_frame_hdr_type
13709 && !bfd_link_relocatable (info)
13710 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
13711 changed = 1;
13712
13713 return changed;
13714 }
13715
13716 bfd_boolean
13717 _bfd_elf_section_already_linked (bfd *abfd,
13718 asection *sec,
13719 struct bfd_link_info *info)
13720 {
13721 flagword flags;
13722 const char *name, *key;
13723 struct bfd_section_already_linked *l;
13724 struct bfd_section_already_linked_hash_entry *already_linked_list;
13725
13726 if (sec->output_section == bfd_abs_section_ptr)
13727 return FALSE;
13728
13729 flags = sec->flags;
13730
13731 /* Return if it isn't a linkonce section. A comdat group section
13732 also has SEC_LINK_ONCE set. */
13733 if ((flags & SEC_LINK_ONCE) == 0)
13734 return FALSE;
13735
13736 /* Don't put group member sections on our list of already linked
13737 sections. They are handled as a group via their group section. */
13738 if (elf_sec_group (sec) != NULL)
13739 return FALSE;
13740
13741 /* For a SHT_GROUP section, use the group signature as the key. */
13742 name = sec->name;
13743 if ((flags & SEC_GROUP) != 0
13744 && elf_next_in_group (sec) != NULL
13745 && elf_group_name (elf_next_in_group (sec)) != NULL)
13746 key = elf_group_name (elf_next_in_group (sec));
13747 else
13748 {
13749 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
13750 if (CONST_STRNEQ (name, ".gnu.linkonce.")
13751 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13752 key++;
13753 else
13754 /* Must be a user linkonce section that doesn't follow gcc's
13755 naming convention. In this case we won't be matching
13756 single member groups. */
13757 key = name;
13758 }
13759
13760 already_linked_list = bfd_section_already_linked_table_lookup (key);
13761
13762 for (l = already_linked_list->entry; l != NULL; l = l->next)
13763 {
13764 /* We may have 2 different types of sections on the list: group
13765 sections with a signature of <key> (<key> is some string),
13766 and linkonce sections named .gnu.linkonce.<type>.<key>.
13767 Match like sections. LTO plugin sections are an exception.
13768 They are always named .gnu.linkonce.t.<key> and match either
13769 type of section. */
13770 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13771 && ((flags & SEC_GROUP) != 0
13772 || strcmp (name, l->sec->name) == 0))
13773 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
13774 {
13775 /* The section has already been linked. See if we should
13776 issue a warning. */
13777 if (!_bfd_handle_already_linked (sec, l, info))
13778 return FALSE;
13779
13780 if (flags & SEC_GROUP)
13781 {
13782 asection *first = elf_next_in_group (sec);
13783 asection *s = first;
13784
13785 while (s != NULL)
13786 {
13787 s->output_section = bfd_abs_section_ptr;
13788 /* Record which group discards it. */
13789 s->kept_section = l->sec;
13790 s = elf_next_in_group (s);
13791 /* These lists are circular. */
13792 if (s == first)
13793 break;
13794 }
13795 }
13796
13797 return TRUE;
13798 }
13799 }
13800
13801 /* A single member comdat group section may be discarded by a
13802 linkonce section and vice versa. */
13803 if ((flags & SEC_GROUP) != 0)
13804 {
13805 asection *first = elf_next_in_group (sec);
13806
13807 if (first != NULL && elf_next_in_group (first) == first)
13808 /* Check this single member group against linkonce sections. */
13809 for (l = already_linked_list->entry; l != NULL; l = l->next)
13810 if ((l->sec->flags & SEC_GROUP) == 0
13811 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13812 {
13813 first->output_section = bfd_abs_section_ptr;
13814 first->kept_section = l->sec;
13815 sec->output_section = bfd_abs_section_ptr;
13816 break;
13817 }
13818 }
13819 else
13820 /* Check this linkonce section against single member groups. */
13821 for (l = already_linked_list->entry; l != NULL; l = l->next)
13822 if (l->sec->flags & SEC_GROUP)
13823 {
13824 asection *first = elf_next_in_group (l->sec);
13825
13826 if (first != NULL
13827 && elf_next_in_group (first) == first
13828 && bfd_elf_match_symbols_in_sections (first, sec, info))
13829 {
13830 sec->output_section = bfd_abs_section_ptr;
13831 sec->kept_section = first;
13832 break;
13833 }
13834 }
13835
13836 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13837 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13838 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13839 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13840 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13841 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13842 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13843 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13844 The reverse order cannot happen as there is never a bfd with only the
13845 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13846 matter as here were are looking only for cross-bfd sections. */
13847
13848 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
13849 for (l = already_linked_list->entry; l != NULL; l = l->next)
13850 if ((l->sec->flags & SEC_GROUP) == 0
13851 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
13852 {
13853 if (abfd != l->sec->owner)
13854 sec->output_section = bfd_abs_section_ptr;
13855 break;
13856 }
13857
13858 /* This is the first section with this name. Record it. */
13859 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
13860 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
13861 return sec->output_section == bfd_abs_section_ptr;
13862 }
13863
13864 bfd_boolean
13865 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
13866 {
13867 return sym->st_shndx == SHN_COMMON;
13868 }
13869
13870 unsigned int
13871 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
13872 {
13873 return SHN_COMMON;
13874 }
13875
13876 asection *
13877 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
13878 {
13879 return bfd_com_section_ptr;
13880 }
13881
13882 bfd_vma
13883 _bfd_elf_default_got_elt_size (bfd *abfd,
13884 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13885 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
13886 bfd *ibfd ATTRIBUTE_UNUSED,
13887 unsigned long symndx ATTRIBUTE_UNUSED)
13888 {
13889 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13890 return bed->s->arch_size / 8;
13891 }
13892
13893 /* Routines to support the creation of dynamic relocs. */
13894
13895 /* Returns the name of the dynamic reloc section associated with SEC. */
13896
13897 static const char *
13898 get_dynamic_reloc_section_name (bfd * abfd,
13899 asection * sec,
13900 bfd_boolean is_rela)
13901 {
13902 char *name;
13903 const char *old_name = bfd_get_section_name (NULL, sec);
13904 const char *prefix = is_rela ? ".rela" : ".rel";
13905
13906 if (old_name == NULL)
13907 return NULL;
13908
13909 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
13910 sprintf (name, "%s%s", prefix, old_name);
13911
13912 return name;
13913 }
13914
13915 /* Returns the dynamic reloc section associated with SEC.
13916 If necessary compute the name of the dynamic reloc section based
13917 on SEC's name (looked up in ABFD's string table) and the setting
13918 of IS_RELA. */
13919
13920 asection *
13921 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
13922 asection * sec,
13923 bfd_boolean is_rela)
13924 {
13925 asection * reloc_sec = elf_section_data (sec)->sreloc;
13926
13927 if (reloc_sec == NULL)
13928 {
13929 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13930
13931 if (name != NULL)
13932 {
13933 reloc_sec = bfd_get_linker_section (abfd, name);
13934
13935 if (reloc_sec != NULL)
13936 elf_section_data (sec)->sreloc = reloc_sec;
13937 }
13938 }
13939
13940 return reloc_sec;
13941 }
13942
13943 /* Returns the dynamic reloc section associated with SEC. If the
13944 section does not exist it is created and attached to the DYNOBJ
13945 bfd and stored in the SRELOC field of SEC's elf_section_data
13946 structure.
13947
13948 ALIGNMENT is the alignment for the newly created section and
13949 IS_RELA defines whether the name should be .rela.<SEC's name>
13950 or .rel.<SEC's name>. The section name is looked up in the
13951 string table associated with ABFD. */
13952
13953 asection *
13954 _bfd_elf_make_dynamic_reloc_section (asection *sec,
13955 bfd *dynobj,
13956 unsigned int alignment,
13957 bfd *abfd,
13958 bfd_boolean is_rela)
13959 {
13960 asection * reloc_sec = elf_section_data (sec)->sreloc;
13961
13962 if (reloc_sec == NULL)
13963 {
13964 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13965
13966 if (name == NULL)
13967 return NULL;
13968
13969 reloc_sec = bfd_get_linker_section (dynobj, name);
13970
13971 if (reloc_sec == NULL)
13972 {
13973 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
13974 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
13975 if ((sec->flags & SEC_ALLOC) != 0)
13976 flags |= SEC_ALLOC | SEC_LOAD;
13977
13978 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
13979 if (reloc_sec != NULL)
13980 {
13981 /* _bfd_elf_get_sec_type_attr chooses a section type by
13982 name. Override as it may be wrong, eg. for a user
13983 section named "auto" we'll get ".relauto" which is
13984 seen to be a .rela section. */
13985 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
13986 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
13987 reloc_sec = NULL;
13988 }
13989 }
13990
13991 elf_section_data (sec)->sreloc = reloc_sec;
13992 }
13993
13994 return reloc_sec;
13995 }
13996
13997 /* Copy the ELF symbol type and other attributes for a linker script
13998 assignment from HSRC to HDEST. Generally this should be treated as
13999 if we found a strong non-dynamic definition for HDEST (except that
14000 ld ignores multiple definition errors). */
14001 void
14002 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14003 struct bfd_link_hash_entry *hdest,
14004 struct bfd_link_hash_entry *hsrc)
14005 {
14006 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14007 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14008 Elf_Internal_Sym isym;
14009
14010 ehdest->type = ehsrc->type;
14011 ehdest->target_internal = ehsrc->target_internal;
14012
14013 isym.st_other = ehsrc->other;
14014 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
14015 }
14016
14017 /* Append a RELA relocation REL to section S in BFD. */
14018
14019 void
14020 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14021 {
14022 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14023 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14024 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14025 bed->s->swap_reloca_out (abfd, rel, loc);
14026 }
14027
14028 /* Append a REL relocation REL to section S in BFD. */
14029
14030 void
14031 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14032 {
14033 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14034 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14035 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
14036 bed->s->swap_reloc_out (abfd, rel, loc);
14037 }
This page took 0.330488 seconds and 4 git commands to generate.