]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - bfd/elflink.c
Don't use bfd_get_* macro to set bfd fields
[thirdparty/binutils-gdb.git] / bfd / elflink.c
1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2019 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 "bfdlink.h"
24 #include "libbfd.h"
25 #define ARCH_SIZE 0
26 #include "elf-bfd.h"
27 #include "safe-ctype.h"
28 #include "libiberty.h"
29 #include "objalloc.h"
30 #if BFD_SUPPORTS_PLUGINS
31 #include "plugin-api.h"
32 #include "plugin.h"
33 #endif
34
35 /* This struct is used to pass information to routines called via
36 elf_link_hash_traverse which must return failure. */
37
38 struct elf_info_failed
39 {
40 struct bfd_link_info *info;
41 bfd_boolean failed;
42 };
43
44 /* This structure is used to pass information to
45 _bfd_elf_link_find_version_dependencies. */
46
47 struct elf_find_verdep_info
48 {
49 /* General link information. */
50 struct bfd_link_info *info;
51 /* The number of dependencies. */
52 unsigned int vers;
53 /* Whether we had a failure. */
54 bfd_boolean failed;
55 };
56
57 static bfd_boolean _bfd_elf_fix_symbol_flags
58 (struct elf_link_hash_entry *, struct elf_info_failed *);
59
60 asection *
61 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
62 unsigned long r_symndx,
63 bfd_boolean discard)
64 {
65 if (r_symndx >= cookie->locsymcount
66 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
67 {
68 struct elf_link_hash_entry *h;
69
70 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
71
72 while (h->root.type == bfd_link_hash_indirect
73 || h->root.type == bfd_link_hash_warning)
74 h = (struct elf_link_hash_entry *) h->root.u.i.link;
75
76 if ((h->root.type == bfd_link_hash_defined
77 || h->root.type == bfd_link_hash_defweak)
78 && discarded_section (h->root.u.def.section))
79 return h->root.u.def.section;
80 else
81 return NULL;
82 }
83 else
84 {
85 /* It's not a relocation against a global symbol,
86 but it could be a relocation against a local
87 symbol for a discarded section. */
88 asection *isec;
89 Elf_Internal_Sym *isym;
90
91 /* Need to: get the symbol; get the section. */
92 isym = &cookie->locsyms[r_symndx];
93 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
94 if (isec != NULL
95 && discard ? discarded_section (isec) : 1)
96 return isec;
97 }
98 return NULL;
99 }
100
101 /* Define a symbol in a dynamic linkage section. */
102
103 struct elf_link_hash_entry *
104 _bfd_elf_define_linkage_sym (bfd *abfd,
105 struct bfd_link_info *info,
106 asection *sec,
107 const char *name)
108 {
109 struct elf_link_hash_entry *h;
110 struct bfd_link_hash_entry *bh;
111 const struct elf_backend_data *bed;
112
113 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
114 if (h != NULL)
115 {
116 /* Zap symbol defined in an as-needed lib that wasn't linked.
117 This is a symptom of a larger problem: Absolute symbols
118 defined in shared libraries can't be overridden, because we
119 lose the link to the bfd which is via the symbol section. */
120 h->root.type = bfd_link_hash_new;
121 bh = &h->root;
122 }
123 else
124 bh = NULL;
125
126 bed = get_elf_backend_data (abfd);
127 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
128 sec, 0, NULL, FALSE, bed->collect,
129 &bh))
130 return NULL;
131 h = (struct elf_link_hash_entry *) bh;
132 BFD_ASSERT (h != NULL);
133 h->def_regular = 1;
134 h->non_elf = 0;
135 h->root.linker_def = 1;
136 h->type = STT_OBJECT;
137 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
138 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
139
140 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
141 return h;
142 }
143
144 bfd_boolean
145 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
146 {
147 flagword flags;
148 asection *s;
149 struct elf_link_hash_entry *h;
150 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
151 struct elf_link_hash_table *htab = elf_hash_table (info);
152
153 /* This function may be called more than once. */
154 if (htab->sgot != NULL)
155 return TRUE;
156
157 flags = bed->dynamic_sec_flags;
158
159 s = bfd_make_section_anyway_with_flags (abfd,
160 (bed->rela_plts_and_copies_p
161 ? ".rela.got" : ".rel.got"),
162 (bed->dynamic_sec_flags
163 | SEC_READONLY));
164 if (s == NULL
165 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
166 return FALSE;
167 htab->srelgot = s;
168
169 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
170 if (s == NULL
171 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
172 return FALSE;
173 htab->sgot = s;
174
175 if (bed->want_got_plt)
176 {
177 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
178 if (s == NULL
179 || !bfd_set_section_alignment (abfd, s,
180 bed->s->log_file_align))
181 return FALSE;
182 htab->sgotplt = s;
183 }
184
185 /* The first bit of the global offset table is the header. */
186 s->size += bed->got_header_size;
187
188 if (bed->want_got_sym)
189 {
190 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
191 (or .got.plt) section. We don't do this in the linker script
192 because we don't want to define the symbol if we are not creating
193 a global offset table. */
194 h = _bfd_elf_define_linkage_sym (abfd, info, s,
195 "_GLOBAL_OFFSET_TABLE_");
196 elf_hash_table (info)->hgot = h;
197 if (h == NULL)
198 return FALSE;
199 }
200
201 return TRUE;
202 }
203 \f
204 /* Create a strtab to hold the dynamic symbol names. */
205 static bfd_boolean
206 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
207 {
208 struct elf_link_hash_table *hash_table;
209
210 hash_table = elf_hash_table (info);
211 if (hash_table->dynobj == NULL)
212 {
213 /* We may not set dynobj, an input file holding linker created
214 dynamic sections to abfd, which may be a dynamic object with
215 its own dynamic sections. We need to find a normal input file
216 to hold linker created sections if possible. */
217 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
218 {
219 bfd *ibfd;
220 asection *s;
221 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
222 if ((ibfd->flags
223 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
224 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
225 && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
226 && !((s = ibfd->sections) != NULL
227 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
228 {
229 abfd = ibfd;
230 break;
231 }
232 }
233 hash_table->dynobj = abfd;
234 }
235
236 if (hash_table->dynstr == NULL)
237 {
238 hash_table->dynstr = _bfd_elf_strtab_init ();
239 if (hash_table->dynstr == NULL)
240 return FALSE;
241 }
242 return TRUE;
243 }
244
245 /* Create some sections which will be filled in with dynamic linking
246 information. ABFD is an input file which requires dynamic sections
247 to be created. The dynamic sections take up virtual memory space
248 when the final executable is run, so we need to create them before
249 addresses are assigned to the output sections. We work out the
250 actual contents and size of these sections later. */
251
252 bfd_boolean
253 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
254 {
255 flagword flags;
256 asection *s;
257 const struct elf_backend_data *bed;
258 struct elf_link_hash_entry *h;
259
260 if (! is_elf_hash_table (info->hash))
261 return FALSE;
262
263 if (elf_hash_table (info)->dynamic_sections_created)
264 return TRUE;
265
266 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
267 return FALSE;
268
269 abfd = elf_hash_table (info)->dynobj;
270 bed = get_elf_backend_data (abfd);
271
272 flags = bed->dynamic_sec_flags;
273
274 /* A dynamically linked executable has a .interp section, but a
275 shared library does not. */
276 if (bfd_link_executable (info) && !info->nointerp)
277 {
278 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
279 flags | SEC_READONLY);
280 if (s == NULL)
281 return FALSE;
282 }
283
284 /* Create sections to hold version informations. These are removed
285 if they are not needed. */
286 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
287 flags | SEC_READONLY);
288 if (s == NULL
289 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
290 return FALSE;
291
292 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
293 flags | SEC_READONLY);
294 if (s == NULL
295 || ! bfd_set_section_alignment (abfd, s, 1))
296 return FALSE;
297
298 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
299 flags | SEC_READONLY);
300 if (s == NULL
301 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
302 return FALSE;
303
304 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
305 flags | SEC_READONLY);
306 if (s == NULL
307 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
308 return FALSE;
309 elf_hash_table (info)->dynsym = s;
310
311 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
312 flags | SEC_READONLY);
313 if (s == NULL)
314 return FALSE;
315
316 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
317 if (s == NULL
318 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
319 return FALSE;
320
321 /* The special symbol _DYNAMIC is always set to the start of the
322 .dynamic section. We could set _DYNAMIC in a linker script, but we
323 only want to define it if we are, in fact, creating a .dynamic
324 section. We don't want to define it if there is no .dynamic
325 section, since on some ELF platforms the start up code examines it
326 to decide how to initialize the process. */
327 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
328 elf_hash_table (info)->hdynamic = h;
329 if (h == NULL)
330 return FALSE;
331
332 if (info->emit_hash)
333 {
334 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
335 flags | SEC_READONLY);
336 if (s == NULL
337 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
338 return FALSE;
339 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
340 }
341
342 if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL)
343 {
344 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
345 flags | SEC_READONLY);
346 if (s == NULL
347 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
348 return FALSE;
349 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
350 4 32-bit words followed by variable count of 64-bit words, then
351 variable count of 32-bit words. */
352 if (bed->s->arch_size == 64)
353 elf_section_data (s)->this_hdr.sh_entsize = 0;
354 else
355 elf_section_data (s)->this_hdr.sh_entsize = 4;
356 }
357
358 /* Let the backend create the rest of the sections. This lets the
359 backend set the right flags. The backend will normally create
360 the .got and .plt sections. */
361 if (bed->elf_backend_create_dynamic_sections == NULL
362 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
363 return FALSE;
364
365 elf_hash_table (info)->dynamic_sections_created = TRUE;
366
367 return TRUE;
368 }
369
370 /* Create dynamic sections when linking against a dynamic object. */
371
372 bfd_boolean
373 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
374 {
375 flagword flags, pltflags;
376 struct elf_link_hash_entry *h;
377 asection *s;
378 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
379 struct elf_link_hash_table *htab = elf_hash_table (info);
380
381 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
382 .rel[a].bss sections. */
383 flags = bed->dynamic_sec_flags;
384
385 pltflags = flags;
386 if (bed->plt_not_loaded)
387 /* We do not clear SEC_ALLOC here because we still want the OS to
388 allocate space for the section; it's just that there's nothing
389 to read in from the object file. */
390 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
391 else
392 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
393 if (bed->plt_readonly)
394 pltflags |= SEC_READONLY;
395
396 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
397 if (s == NULL
398 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
399 return FALSE;
400 htab->splt = s;
401
402 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
403 .plt section. */
404 if (bed->want_plt_sym)
405 {
406 h = _bfd_elf_define_linkage_sym (abfd, info, s,
407 "_PROCEDURE_LINKAGE_TABLE_");
408 elf_hash_table (info)->hplt = h;
409 if (h == NULL)
410 return FALSE;
411 }
412
413 s = bfd_make_section_anyway_with_flags (abfd,
414 (bed->rela_plts_and_copies_p
415 ? ".rela.plt" : ".rel.plt"),
416 flags | SEC_READONLY);
417 if (s == NULL
418 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
419 return FALSE;
420 htab->srelplt = s;
421
422 if (! _bfd_elf_create_got_section (abfd, info))
423 return FALSE;
424
425 if (bed->want_dynbss)
426 {
427 /* The .dynbss section is a place to put symbols which are defined
428 by dynamic objects, are referenced by regular objects, and are
429 not functions. We must allocate space for them in the process
430 image and use a R_*_COPY reloc to tell the dynamic linker to
431 initialize them at run time. The linker script puts the .dynbss
432 section into the .bss section of the final image. */
433 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
434 SEC_ALLOC | SEC_LINKER_CREATED);
435 if (s == NULL)
436 return FALSE;
437 htab->sdynbss = s;
438
439 if (bed->want_dynrelro)
440 {
441 /* Similarly, but for symbols that were originally in read-only
442 sections. This section doesn't really need to have contents,
443 but make it like other .data.rel.ro sections. */
444 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
445 flags);
446 if (s == NULL)
447 return FALSE;
448 htab->sdynrelro = s;
449 }
450
451 /* The .rel[a].bss section holds copy relocs. This section is not
452 normally needed. We need to create it here, though, so that the
453 linker will map it to an output section. We can't just create it
454 only if we need it, because we will not know whether we need it
455 until we have seen all the input files, and the first time the
456 main linker code calls BFD after examining all the input files
457 (size_dynamic_sections) the input sections have already been
458 mapped to the output sections. If the section turns out not to
459 be needed, we can discard it later. We will never need this
460 section when generating a shared object, since they do not use
461 copy relocs. */
462 if (bfd_link_executable (info))
463 {
464 s = bfd_make_section_anyway_with_flags (abfd,
465 (bed->rela_plts_and_copies_p
466 ? ".rela.bss" : ".rel.bss"),
467 flags | SEC_READONLY);
468 if (s == NULL
469 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
470 return FALSE;
471 htab->srelbss = s;
472
473 if (bed->want_dynrelro)
474 {
475 s = (bfd_make_section_anyway_with_flags
476 (abfd, (bed->rela_plts_and_copies_p
477 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
478 flags | SEC_READONLY));
479 if (s == NULL
480 || ! bfd_set_section_alignment (abfd, s,
481 bed->s->log_file_align))
482 return FALSE;
483 htab->sreldynrelro = s;
484 }
485 }
486 }
487
488 return TRUE;
489 }
490 \f
491 /* Record a new dynamic symbol. We record the dynamic symbols as we
492 read the input files, since we need to have a list of all of them
493 before we can determine the final sizes of the output sections.
494 Note that we may actually call this function even though we are not
495 going to output any dynamic symbols; in some cases we know that a
496 symbol should be in the dynamic symbol table, but only if there is
497 one. */
498
499 bfd_boolean
500 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
501 struct elf_link_hash_entry *h)
502 {
503 if (h->dynindx == -1)
504 {
505 struct elf_strtab_hash *dynstr;
506 char *p;
507 const char *name;
508 size_t indx;
509
510 /* XXX: The ABI draft says the linker must turn hidden and
511 internal symbols into STB_LOCAL symbols when producing the
512 DSO. However, if ld.so honors st_other in the dynamic table,
513 this would not be necessary. */
514 switch (ELF_ST_VISIBILITY (h->other))
515 {
516 case STV_INTERNAL:
517 case STV_HIDDEN:
518 if (h->root.type != bfd_link_hash_undefined
519 && h->root.type != bfd_link_hash_undefweak)
520 {
521 h->forced_local = 1;
522 if (!elf_hash_table (info)->is_relocatable_executable)
523 return TRUE;
524 }
525
526 default:
527 break;
528 }
529
530 h->dynindx = elf_hash_table (info)->dynsymcount;
531 ++elf_hash_table (info)->dynsymcount;
532
533 dynstr = elf_hash_table (info)->dynstr;
534 if (dynstr == NULL)
535 {
536 /* Create a strtab to hold the dynamic symbol names. */
537 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
538 if (dynstr == NULL)
539 return FALSE;
540 }
541
542 /* We don't put any version information in the dynamic string
543 table. */
544 name = h->root.root.string;
545 p = strchr (name, ELF_VER_CHR);
546 if (p != NULL)
547 /* We know that the p points into writable memory. In fact,
548 there are only a few symbols that have read-only names, being
549 those like _GLOBAL_OFFSET_TABLE_ that are created specially
550 by the backends. Most symbols will have names pointing into
551 an ELF string table read from a file, or to objalloc memory. */
552 *p = 0;
553
554 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
555
556 if (p != NULL)
557 *p = ELF_VER_CHR;
558
559 if (indx == (size_t) -1)
560 return FALSE;
561 h->dynstr_index = indx;
562 }
563
564 return TRUE;
565 }
566 \f
567 /* Mark a symbol dynamic. */
568
569 static void
570 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
571 struct elf_link_hash_entry *h,
572 Elf_Internal_Sym *sym)
573 {
574 struct bfd_elf_dynamic_list *d = info->dynamic_list;
575
576 /* It may be called more than once on the same H. */
577 if(h->dynamic || bfd_link_relocatable (info))
578 return;
579
580 if ((info->dynamic_data
581 && (h->type == STT_OBJECT
582 || h->type == STT_COMMON
583 || (sym != NULL
584 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
585 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
586 || (d != NULL
587 && h->non_elf
588 && (*d->match) (&d->head, NULL, h->root.root.string)))
589 {
590 h->dynamic = 1;
591 /* NB: If a symbol is made dynamic by --dynamic-list, it has
592 non-IR reference. */
593 h->root.non_ir_ref_dynamic = 1;
594 }
595 }
596
597 /* Record an assignment to a symbol made by a linker script. We need
598 this in case some dynamic object refers to this symbol. */
599
600 bfd_boolean
601 bfd_elf_record_link_assignment (bfd *output_bfd,
602 struct bfd_link_info *info,
603 const char *name,
604 bfd_boolean provide,
605 bfd_boolean hidden)
606 {
607 struct elf_link_hash_entry *h, *hv;
608 struct elf_link_hash_table *htab;
609 const struct elf_backend_data *bed;
610
611 if (!is_elf_hash_table (info->hash))
612 return TRUE;
613
614 htab = elf_hash_table (info);
615 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
616 if (h == NULL)
617 return provide;
618
619 if (h->root.type == bfd_link_hash_warning)
620 h = (struct elf_link_hash_entry *) h->root.u.i.link;
621
622 if (h->versioned == unknown)
623 {
624 /* Set versioned if symbol version is unknown. */
625 char *version = strrchr (name, ELF_VER_CHR);
626 if (version)
627 {
628 if (version > name && version[-1] != ELF_VER_CHR)
629 h->versioned = versioned_hidden;
630 else
631 h->versioned = versioned;
632 }
633 }
634
635 /* Symbols defined in a linker script but not referenced anywhere
636 else will have non_elf set. */
637 if (h->non_elf)
638 {
639 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
640 h->non_elf = 0;
641 }
642
643 switch (h->root.type)
644 {
645 case bfd_link_hash_defined:
646 case bfd_link_hash_defweak:
647 case bfd_link_hash_common:
648 break;
649 case bfd_link_hash_undefweak:
650 case bfd_link_hash_undefined:
651 /* Since we're defining the symbol, don't let it seem to have not
652 been defined. record_dynamic_symbol and size_dynamic_sections
653 may depend on this. */
654 h->root.type = bfd_link_hash_new;
655 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
656 bfd_link_repair_undef_list (&htab->root);
657 break;
658 case bfd_link_hash_new:
659 break;
660 case bfd_link_hash_indirect:
661 /* We had a versioned symbol in a dynamic library. We make the
662 the versioned symbol point to this one. */
663 bed = get_elf_backend_data (output_bfd);
664 hv = h;
665 while (hv->root.type == bfd_link_hash_indirect
666 || hv->root.type == bfd_link_hash_warning)
667 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
668 /* We don't need to update h->root.u since linker will set them
669 later. */
670 h->root.type = bfd_link_hash_undefined;
671 hv->root.type = bfd_link_hash_indirect;
672 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
673 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
674 break;
675 default:
676 BFD_FAIL ();
677 return FALSE;
678 }
679
680 /* If this symbol is being provided by the linker script, and it is
681 currently defined by a dynamic object, but not by a regular
682 object, then mark it as undefined so that the generic linker will
683 force the correct value. */
684 if (provide
685 && h->def_dynamic
686 && !h->def_regular)
687 h->root.type = bfd_link_hash_undefined;
688
689 /* If this symbol is currently defined by a dynamic object, but not
690 by a regular object, then clear out any version information because
691 the symbol will not be associated with the dynamic object any
692 more. */
693 if (h->def_dynamic && !h->def_regular)
694 h->verinfo.verdef = NULL;
695
696 /* Make sure this symbol is not garbage collected. */
697 h->mark = 1;
698
699 h->def_regular = 1;
700
701 if (hidden)
702 {
703 bed = get_elf_backend_data (output_bfd);
704 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
705 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
706 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
707 }
708
709 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
710 and executables. */
711 if (!bfd_link_relocatable (info)
712 && h->dynindx != -1
713 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
714 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
715 h->forced_local = 1;
716
717 if ((h->def_dynamic
718 || h->ref_dynamic
719 || bfd_link_dll (info)
720 || elf_hash_table (info)->is_relocatable_executable)
721 && !h->forced_local
722 && h->dynindx == -1)
723 {
724 if (! bfd_elf_link_record_dynamic_symbol (info, h))
725 return FALSE;
726
727 /* If this is a weak defined symbol, and we know a corresponding
728 real symbol from the same dynamic object, make sure the real
729 symbol is also made into a dynamic symbol. */
730 if (h->is_weakalias)
731 {
732 struct elf_link_hash_entry *def = weakdef (h);
733
734 if (def->dynindx == -1
735 && !bfd_elf_link_record_dynamic_symbol (info, def))
736 return FALSE;
737 }
738 }
739
740 return TRUE;
741 }
742
743 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
744 success, and 2 on a failure caused by attempting to record a symbol
745 in a discarded section, eg. a discarded link-once section symbol. */
746
747 int
748 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
749 bfd *input_bfd,
750 long input_indx)
751 {
752 bfd_size_type amt;
753 struct elf_link_local_dynamic_entry *entry;
754 struct elf_link_hash_table *eht;
755 struct elf_strtab_hash *dynstr;
756 size_t dynstr_index;
757 char *name;
758 Elf_External_Sym_Shndx eshndx;
759 char esym[sizeof (Elf64_External_Sym)];
760
761 if (! is_elf_hash_table (info->hash))
762 return 0;
763
764 /* See if the entry exists already. */
765 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
766 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
767 return 1;
768
769 amt = sizeof (*entry);
770 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
771 if (entry == NULL)
772 return 0;
773
774 /* Go find the symbol, so that we can find it's name. */
775 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
776 1, input_indx, &entry->isym, esym, &eshndx))
777 {
778 bfd_release (input_bfd, entry);
779 return 0;
780 }
781
782 if (entry->isym.st_shndx != SHN_UNDEF
783 && entry->isym.st_shndx < SHN_LORESERVE)
784 {
785 asection *s;
786
787 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
788 if (s == NULL || bfd_is_abs_section (s->output_section))
789 {
790 /* We can still bfd_release here as nothing has done another
791 bfd_alloc. We can't do this later in this function. */
792 bfd_release (input_bfd, entry);
793 return 2;
794 }
795 }
796
797 name = (bfd_elf_string_from_elf_section
798 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
799 entry->isym.st_name));
800
801 dynstr = elf_hash_table (info)->dynstr;
802 if (dynstr == NULL)
803 {
804 /* Create a strtab to hold the dynamic symbol names. */
805 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
806 if (dynstr == NULL)
807 return 0;
808 }
809
810 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
811 if (dynstr_index == (size_t) -1)
812 return 0;
813 entry->isym.st_name = dynstr_index;
814
815 eht = elf_hash_table (info);
816
817 entry->next = eht->dynlocal;
818 eht->dynlocal = entry;
819 entry->input_bfd = input_bfd;
820 entry->input_indx = input_indx;
821 eht->dynsymcount++;
822
823 /* Whatever binding the symbol had before, it's now local. */
824 entry->isym.st_info
825 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
826
827 /* The dynindx will be set at the end of size_dynamic_sections. */
828
829 return 1;
830 }
831
832 /* Return the dynindex of a local dynamic symbol. */
833
834 long
835 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
836 bfd *input_bfd,
837 long input_indx)
838 {
839 struct elf_link_local_dynamic_entry *e;
840
841 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
842 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
843 return e->dynindx;
844 return -1;
845 }
846
847 /* This function is used to renumber the dynamic symbols, if some of
848 them are removed because they are marked as local. This is called
849 via elf_link_hash_traverse. */
850
851 static bfd_boolean
852 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
853 void *data)
854 {
855 size_t *count = (size_t *) data;
856
857 if (h->forced_local)
858 return TRUE;
859
860 if (h->dynindx != -1)
861 h->dynindx = ++(*count);
862
863 return TRUE;
864 }
865
866
867 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
868 STB_LOCAL binding. */
869
870 static bfd_boolean
871 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
872 void *data)
873 {
874 size_t *count = (size_t *) data;
875
876 if (!h->forced_local)
877 return TRUE;
878
879 if (h->dynindx != -1)
880 h->dynindx = ++(*count);
881
882 return TRUE;
883 }
884
885 /* Return true if the dynamic symbol for a given section should be
886 omitted when creating a shared library. */
887 bfd_boolean
888 _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
889 struct bfd_link_info *info,
890 asection *p)
891 {
892 struct elf_link_hash_table *htab;
893 asection *ip;
894
895 switch (elf_section_data (p)->this_hdr.sh_type)
896 {
897 case SHT_PROGBITS:
898 case SHT_NOBITS:
899 /* If sh_type is yet undecided, assume it could be
900 SHT_PROGBITS/SHT_NOBITS. */
901 case SHT_NULL:
902 htab = elf_hash_table (info);
903 if (htab->text_index_section != NULL)
904 return p != htab->text_index_section && p != htab->data_index_section;
905
906 return (htab->dynobj != NULL
907 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
908 && ip->output_section == p);
909
910 /* There shouldn't be section relative relocations
911 against any other section. */
912 default:
913 return TRUE;
914 }
915 }
916
917 bfd_boolean
918 _bfd_elf_omit_section_dynsym_all
919 (bfd *output_bfd ATTRIBUTE_UNUSED,
920 struct bfd_link_info *info ATTRIBUTE_UNUSED,
921 asection *p ATTRIBUTE_UNUSED)
922 {
923 return TRUE;
924 }
925
926 /* Assign dynsym indices. In a shared library we generate a section
927 symbol for each output section, which come first. Next come symbols
928 which have been forced to local binding. Then all of the back-end
929 allocated local dynamic syms, followed by the rest of the global
930 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
931 (This prevents the early call before elf_backend_init_index_section
932 and strip_excluded_output_sections setting dynindx for sections
933 that are stripped.) */
934
935 static unsigned long
936 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
937 struct bfd_link_info *info,
938 unsigned long *section_sym_count)
939 {
940 unsigned long dynsymcount = 0;
941 bfd_boolean do_sec = section_sym_count != NULL;
942
943 if (bfd_link_pic (info)
944 || elf_hash_table (info)->is_relocatable_executable)
945 {
946 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
947 asection *p;
948 for (p = output_bfd->sections; p ; p = p->next)
949 if ((p->flags & SEC_EXCLUDE) == 0
950 && (p->flags & SEC_ALLOC) != 0
951 && elf_hash_table (info)->dynamic_relocs
952 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
953 {
954 ++dynsymcount;
955 if (do_sec)
956 elf_section_data (p)->dynindx = dynsymcount;
957 }
958 else if (do_sec)
959 elf_section_data (p)->dynindx = 0;
960 }
961 if (do_sec)
962 *section_sym_count = dynsymcount;
963
964 elf_link_hash_traverse (elf_hash_table (info),
965 elf_link_renumber_local_hash_table_dynsyms,
966 &dynsymcount);
967
968 if (elf_hash_table (info)->dynlocal)
969 {
970 struct elf_link_local_dynamic_entry *p;
971 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
972 p->dynindx = ++dynsymcount;
973 }
974 elf_hash_table (info)->local_dynsymcount = dynsymcount;
975
976 elf_link_hash_traverse (elf_hash_table (info),
977 elf_link_renumber_hash_table_dynsyms,
978 &dynsymcount);
979
980 /* There is an unused NULL entry at the head of the table which we
981 must account for in our count even if the table is empty since it
982 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
983 .dynamic section. */
984 dynsymcount++;
985
986 elf_hash_table (info)->dynsymcount = dynsymcount;
987 return dynsymcount;
988 }
989
990 /* Merge st_other field. */
991
992 static void
993 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
994 const Elf_Internal_Sym *isym, asection *sec,
995 bfd_boolean definition, bfd_boolean dynamic)
996 {
997 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
998
999 /* If st_other has a processor-specific meaning, specific
1000 code might be needed here. */
1001 if (bed->elf_backend_merge_symbol_attribute)
1002 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
1003 dynamic);
1004
1005 if (!dynamic)
1006 {
1007 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
1008 unsigned hvis = ELF_ST_VISIBILITY (h->other);
1009
1010 /* Keep the most constraining visibility. Leave the remainder
1011 of the st_other field to elf_backend_merge_symbol_attribute. */
1012 if (symvis - 1 < hvis - 1)
1013 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1014 }
1015 else if (definition
1016 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
1017 && (sec->flags & SEC_READONLY) == 0)
1018 h->protected_def = 1;
1019 }
1020
1021 /* This function is called when we want to merge a new symbol with an
1022 existing symbol. It handles the various cases which arise when we
1023 find a definition in a dynamic object, or when there is already a
1024 definition in a dynamic object. The new symbol is described by
1025 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1026 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1027 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1028 of an old common symbol. We set OVERRIDE if the old symbol is
1029 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1030 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1031 to change. By OK to change, we mean that we shouldn't warn if the
1032 type or size does change. */
1033
1034 static bfd_boolean
1035 _bfd_elf_merge_symbol (bfd *abfd,
1036 struct bfd_link_info *info,
1037 const char *name,
1038 Elf_Internal_Sym *sym,
1039 asection **psec,
1040 bfd_vma *pvalue,
1041 struct elf_link_hash_entry **sym_hash,
1042 bfd **poldbfd,
1043 bfd_boolean *pold_weak,
1044 unsigned int *pold_alignment,
1045 bfd_boolean *skip,
1046 bfd_boolean *override,
1047 bfd_boolean *type_change_ok,
1048 bfd_boolean *size_change_ok,
1049 bfd_boolean *matched)
1050 {
1051 asection *sec, *oldsec;
1052 struct elf_link_hash_entry *h;
1053 struct elf_link_hash_entry *hi;
1054 struct elf_link_hash_entry *flip;
1055 int bind;
1056 bfd *oldbfd;
1057 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1058 bfd_boolean newweak, oldweak, newfunc, oldfunc;
1059 const struct elf_backend_data *bed;
1060 char *new_version;
1061 bfd_boolean default_sym = *matched;
1062
1063 *skip = FALSE;
1064 *override = FALSE;
1065
1066 sec = *psec;
1067 bind = ELF_ST_BIND (sym->st_info);
1068
1069 if (! bfd_is_und_section (sec))
1070 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1071 else
1072 h = ((struct elf_link_hash_entry *)
1073 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1074 if (h == NULL)
1075 return FALSE;
1076 *sym_hash = h;
1077
1078 bed = get_elf_backend_data (abfd);
1079
1080 /* NEW_VERSION is the symbol version of the new symbol. */
1081 if (h->versioned != unversioned)
1082 {
1083 /* Symbol version is unknown or versioned. */
1084 new_version = strrchr (name, ELF_VER_CHR);
1085 if (new_version)
1086 {
1087 if (h->versioned == unknown)
1088 {
1089 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1090 h->versioned = versioned_hidden;
1091 else
1092 h->versioned = versioned;
1093 }
1094 new_version += 1;
1095 if (new_version[0] == '\0')
1096 new_version = NULL;
1097 }
1098 else
1099 h->versioned = unversioned;
1100 }
1101 else
1102 new_version = NULL;
1103
1104 /* For merging, we only care about real symbols. But we need to make
1105 sure that indirect symbol dynamic flags are updated. */
1106 hi = h;
1107 while (h->root.type == bfd_link_hash_indirect
1108 || h->root.type == bfd_link_hash_warning)
1109 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1110
1111 if (!*matched)
1112 {
1113 if (hi == h || h->root.type == bfd_link_hash_new)
1114 *matched = TRUE;
1115 else
1116 {
1117 /* OLD_HIDDEN is true if the existing symbol is only visible
1118 to the symbol with the same symbol version. NEW_HIDDEN is
1119 true if the new symbol is only visible to the symbol with
1120 the same symbol version. */
1121 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1122 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1123 if (!old_hidden && !new_hidden)
1124 /* The new symbol matches the existing symbol if both
1125 aren't hidden. */
1126 *matched = TRUE;
1127 else
1128 {
1129 /* OLD_VERSION is the symbol version of the existing
1130 symbol. */
1131 char *old_version;
1132
1133 if (h->versioned >= versioned)
1134 old_version = strrchr (h->root.root.string,
1135 ELF_VER_CHR) + 1;
1136 else
1137 old_version = NULL;
1138
1139 /* The new symbol matches the existing symbol if they
1140 have the same symbol version. */
1141 *matched = (old_version == new_version
1142 || (old_version != NULL
1143 && new_version != NULL
1144 && strcmp (old_version, new_version) == 0));
1145 }
1146 }
1147 }
1148
1149 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1150 existing symbol. */
1151
1152 oldbfd = NULL;
1153 oldsec = NULL;
1154 switch (h->root.type)
1155 {
1156 default:
1157 break;
1158
1159 case bfd_link_hash_undefined:
1160 case bfd_link_hash_undefweak:
1161 oldbfd = h->root.u.undef.abfd;
1162 break;
1163
1164 case bfd_link_hash_defined:
1165 case bfd_link_hash_defweak:
1166 oldbfd = h->root.u.def.section->owner;
1167 oldsec = h->root.u.def.section;
1168 break;
1169
1170 case bfd_link_hash_common:
1171 oldbfd = h->root.u.c.p->section->owner;
1172 oldsec = h->root.u.c.p->section;
1173 if (pold_alignment)
1174 *pold_alignment = h->root.u.c.p->alignment_power;
1175 break;
1176 }
1177 if (poldbfd && *poldbfd == NULL)
1178 *poldbfd = oldbfd;
1179
1180 /* Differentiate strong and weak symbols. */
1181 newweak = bind == STB_WEAK;
1182 oldweak = (h->root.type == bfd_link_hash_defweak
1183 || h->root.type == bfd_link_hash_undefweak);
1184 if (pold_weak)
1185 *pold_weak = oldweak;
1186
1187 /* We have to check it for every instance since the first few may be
1188 references and not all compilers emit symbol type for undefined
1189 symbols. */
1190 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1191
1192 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1193 respectively, is from a dynamic object. */
1194
1195 newdyn = (abfd->flags & DYNAMIC) != 0;
1196
1197 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1198 syms and defined syms in dynamic libraries respectively.
1199 ref_dynamic on the other hand can be set for a symbol defined in
1200 a dynamic library, and def_dynamic may not be set; When the
1201 definition in a dynamic lib is overridden by a definition in the
1202 executable use of the symbol in the dynamic lib becomes a
1203 reference to the executable symbol. */
1204 if (newdyn)
1205 {
1206 if (bfd_is_und_section (sec))
1207 {
1208 if (bind != STB_WEAK)
1209 {
1210 h->ref_dynamic_nonweak = 1;
1211 hi->ref_dynamic_nonweak = 1;
1212 }
1213 }
1214 else
1215 {
1216 /* Update the existing symbol only if they match. */
1217 if (*matched)
1218 h->dynamic_def = 1;
1219 hi->dynamic_def = 1;
1220 }
1221 }
1222
1223 /* If we just created the symbol, mark it as being an ELF symbol.
1224 Other than that, there is nothing to do--there is no merge issue
1225 with a newly defined symbol--so we just return. */
1226
1227 if (h->root.type == bfd_link_hash_new)
1228 {
1229 h->non_elf = 0;
1230 return TRUE;
1231 }
1232
1233 /* In cases involving weak versioned symbols, we may wind up trying
1234 to merge a symbol with itself. Catch that here, to avoid the
1235 confusion that results if we try to override a symbol with
1236 itself. The additional tests catch cases like
1237 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1238 dynamic object, which we do want to handle here. */
1239 if (abfd == oldbfd
1240 && (newweak || oldweak)
1241 && ((abfd->flags & DYNAMIC) == 0
1242 || !h->def_regular))
1243 return TRUE;
1244
1245 olddyn = FALSE;
1246 if (oldbfd != NULL)
1247 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1248 else if (oldsec != NULL)
1249 {
1250 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1251 indices used by MIPS ELF. */
1252 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1253 }
1254
1255 /* Handle a case where plugin_notice won't be called and thus won't
1256 set the non_ir_ref flags on the first pass over symbols. */
1257 if (oldbfd != NULL
1258 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1259 && newdyn != olddyn)
1260 {
1261 h->root.non_ir_ref_dynamic = TRUE;
1262 hi->root.non_ir_ref_dynamic = TRUE;
1263 }
1264
1265 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1266 respectively, appear to be a definition rather than reference. */
1267
1268 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1269
1270 olddef = (h->root.type != bfd_link_hash_undefined
1271 && h->root.type != bfd_link_hash_undefweak
1272 && h->root.type != bfd_link_hash_common);
1273
1274 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1275 respectively, appear to be a function. */
1276
1277 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1278 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1279
1280 oldfunc = (h->type != STT_NOTYPE
1281 && bed->is_function_type (h->type));
1282
1283 if (!(newfunc && oldfunc)
1284 && ELF_ST_TYPE (sym->st_info) != h->type
1285 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1286 && h->type != STT_NOTYPE
1287 && (newdef || bfd_is_com_section (sec))
1288 && (olddef || h->root.type == bfd_link_hash_common))
1289 {
1290 /* If creating a default indirect symbol ("foo" or "foo@") from
1291 a dynamic versioned definition ("foo@@") skip doing so if
1292 there is an existing regular definition with a different
1293 type. We don't want, for example, a "time" variable in the
1294 executable overriding a "time" function in a shared library. */
1295 if (newdyn
1296 && !olddyn)
1297 {
1298 *skip = TRUE;
1299 return TRUE;
1300 }
1301
1302 /* When adding a symbol from a regular object file after we have
1303 created indirect symbols, undo the indirection and any
1304 dynamic state. */
1305 if (hi != h
1306 && !newdyn
1307 && olddyn)
1308 {
1309 h = hi;
1310 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1311 h->forced_local = 0;
1312 h->ref_dynamic = 0;
1313 h->def_dynamic = 0;
1314 h->dynamic_def = 0;
1315 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1316 {
1317 h->root.type = bfd_link_hash_undefined;
1318 h->root.u.undef.abfd = abfd;
1319 }
1320 else
1321 {
1322 h->root.type = bfd_link_hash_new;
1323 h->root.u.undef.abfd = NULL;
1324 }
1325 return TRUE;
1326 }
1327 }
1328
1329 /* Check TLS symbols. We don't check undefined symbols introduced
1330 by "ld -u" which have no type (and oldbfd NULL), and we don't
1331 check symbols from plugins because they also have no type. */
1332 if (oldbfd != NULL
1333 && (oldbfd->flags & BFD_PLUGIN) == 0
1334 && (abfd->flags & BFD_PLUGIN) == 0
1335 && ELF_ST_TYPE (sym->st_info) != h->type
1336 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1337 {
1338 bfd *ntbfd, *tbfd;
1339 bfd_boolean ntdef, tdef;
1340 asection *ntsec, *tsec;
1341
1342 if (h->type == STT_TLS)
1343 {
1344 ntbfd = abfd;
1345 ntsec = sec;
1346 ntdef = newdef;
1347 tbfd = oldbfd;
1348 tsec = oldsec;
1349 tdef = olddef;
1350 }
1351 else
1352 {
1353 ntbfd = oldbfd;
1354 ntsec = oldsec;
1355 ntdef = olddef;
1356 tbfd = abfd;
1357 tsec = sec;
1358 tdef = newdef;
1359 }
1360
1361 if (tdef && ntdef)
1362 _bfd_error_handler
1363 /* xgettext:c-format */
1364 (_("%s: TLS definition in %pB section %pA "
1365 "mismatches non-TLS definition in %pB section %pA"),
1366 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1367 else if (!tdef && !ntdef)
1368 _bfd_error_handler
1369 /* xgettext:c-format */
1370 (_("%s: TLS reference in %pB "
1371 "mismatches non-TLS reference in %pB"),
1372 h->root.root.string, tbfd, ntbfd);
1373 else if (tdef)
1374 _bfd_error_handler
1375 /* xgettext:c-format */
1376 (_("%s: TLS definition in %pB section %pA "
1377 "mismatches non-TLS reference in %pB"),
1378 h->root.root.string, tbfd, tsec, ntbfd);
1379 else
1380 _bfd_error_handler
1381 /* xgettext:c-format */
1382 (_("%s: TLS reference in %pB "
1383 "mismatches non-TLS definition in %pB section %pA"),
1384 h->root.root.string, tbfd, ntbfd, ntsec);
1385
1386 bfd_set_error (bfd_error_bad_value);
1387 return FALSE;
1388 }
1389
1390 /* If the old symbol has non-default visibility, we ignore the new
1391 definition from a dynamic object. */
1392 if (newdyn
1393 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1394 && !bfd_is_und_section (sec))
1395 {
1396 *skip = TRUE;
1397 /* Make sure this symbol is dynamic. */
1398 h->ref_dynamic = 1;
1399 hi->ref_dynamic = 1;
1400 /* A protected symbol has external availability. Make sure it is
1401 recorded as dynamic.
1402
1403 FIXME: Should we check type and size for protected symbol? */
1404 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1405 return bfd_elf_link_record_dynamic_symbol (info, h);
1406 else
1407 return TRUE;
1408 }
1409 else if (!newdyn
1410 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1411 && h->def_dynamic)
1412 {
1413 /* If the new symbol with non-default visibility comes from a
1414 relocatable file and the old definition comes from a dynamic
1415 object, we remove the old definition. */
1416 if (hi->root.type == bfd_link_hash_indirect)
1417 {
1418 /* Handle the case where the old dynamic definition is
1419 default versioned. We need to copy the symbol info from
1420 the symbol with default version to the normal one if it
1421 was referenced before. */
1422 if (h->ref_regular)
1423 {
1424 hi->root.type = h->root.type;
1425 h->root.type = bfd_link_hash_indirect;
1426 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1427
1428 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1429 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1430 {
1431 /* If the new symbol is hidden or internal, completely undo
1432 any dynamic link state. */
1433 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1434 h->forced_local = 0;
1435 h->ref_dynamic = 0;
1436 }
1437 else
1438 h->ref_dynamic = 1;
1439
1440 h->def_dynamic = 0;
1441 /* FIXME: Should we check type and size for protected symbol? */
1442 h->size = 0;
1443 h->type = 0;
1444
1445 h = hi;
1446 }
1447 else
1448 h = hi;
1449 }
1450
1451 /* If the old symbol was undefined before, then it will still be
1452 on the undefs list. If the new symbol is undefined or
1453 common, we can't make it bfd_link_hash_new here, because new
1454 undefined or common symbols will be added to the undefs list
1455 by _bfd_generic_link_add_one_symbol. Symbols may not be
1456 added twice to the undefs list. Also, if the new symbol is
1457 undefweak then we don't want to lose the strong undef. */
1458 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1459 {
1460 h->root.type = bfd_link_hash_undefined;
1461 h->root.u.undef.abfd = abfd;
1462 }
1463 else
1464 {
1465 h->root.type = bfd_link_hash_new;
1466 h->root.u.undef.abfd = NULL;
1467 }
1468
1469 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1470 {
1471 /* If the new symbol is hidden or internal, completely undo
1472 any dynamic link state. */
1473 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1474 h->forced_local = 0;
1475 h->ref_dynamic = 0;
1476 }
1477 else
1478 h->ref_dynamic = 1;
1479 h->def_dynamic = 0;
1480 /* FIXME: Should we check type and size for protected symbol? */
1481 h->size = 0;
1482 h->type = 0;
1483 return TRUE;
1484 }
1485
1486 /* If a new weak symbol definition comes from a regular file and the
1487 old symbol comes from a dynamic library, we treat the new one as
1488 strong. Similarly, an old weak symbol definition from a regular
1489 file is treated as strong when the new symbol comes from a dynamic
1490 library. Further, an old weak symbol from a dynamic library is
1491 treated as strong if the new symbol is from a dynamic library.
1492 This reflects the way glibc's ld.so works.
1493
1494 Also allow a weak symbol to override a linker script symbol
1495 defined by an early pass over the script. This is done so the
1496 linker knows the symbol is defined in an object file, for the
1497 DEFINED script function.
1498
1499 Do this before setting *type_change_ok or *size_change_ok so that
1500 we warn properly when dynamic library symbols are overridden. */
1501
1502 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1503 newweak = FALSE;
1504 if (olddef && newdyn)
1505 oldweak = FALSE;
1506
1507 /* Allow changes between different types of function symbol. */
1508 if (newfunc && oldfunc)
1509 *type_change_ok = TRUE;
1510
1511 /* It's OK to change the type if either the existing symbol or the
1512 new symbol is weak. A type change is also OK if the old symbol
1513 is undefined and the new symbol is defined. */
1514
1515 if (oldweak
1516 || newweak
1517 || (newdef
1518 && h->root.type == bfd_link_hash_undefined))
1519 *type_change_ok = TRUE;
1520
1521 /* It's OK to change the size if either the existing symbol or the
1522 new symbol is weak, or if the old symbol is undefined. */
1523
1524 if (*type_change_ok
1525 || h->root.type == bfd_link_hash_undefined)
1526 *size_change_ok = TRUE;
1527
1528 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1529 symbol, respectively, appears to be a common symbol in a dynamic
1530 object. If a symbol appears in an uninitialized section, and is
1531 not weak, and is not a function, then it may be a common symbol
1532 which was resolved when the dynamic object was created. We want
1533 to treat such symbols specially, because they raise special
1534 considerations when setting the symbol size: if the symbol
1535 appears as a common symbol in a regular object, and the size in
1536 the regular object is larger, we must make sure that we use the
1537 larger size. This problematic case can always be avoided in C,
1538 but it must be handled correctly when using Fortran shared
1539 libraries.
1540
1541 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1542 likewise for OLDDYNCOMMON and OLDDEF.
1543
1544 Note that this test is just a heuristic, and that it is quite
1545 possible to have an uninitialized symbol in a shared object which
1546 is really a definition, rather than a common symbol. This could
1547 lead to some minor confusion when the symbol really is a common
1548 symbol in some regular object. However, I think it will be
1549 harmless. */
1550
1551 if (newdyn
1552 && newdef
1553 && !newweak
1554 && (sec->flags & SEC_ALLOC) != 0
1555 && (sec->flags & SEC_LOAD) == 0
1556 && sym->st_size > 0
1557 && !newfunc)
1558 newdyncommon = TRUE;
1559 else
1560 newdyncommon = FALSE;
1561
1562 if (olddyn
1563 && olddef
1564 && h->root.type == bfd_link_hash_defined
1565 && h->def_dynamic
1566 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1567 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1568 && h->size > 0
1569 && !oldfunc)
1570 olddyncommon = TRUE;
1571 else
1572 olddyncommon = FALSE;
1573
1574 /* We now know everything about the old and new symbols. We ask the
1575 backend to check if we can merge them. */
1576 if (bed->merge_symbol != NULL)
1577 {
1578 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1579 return FALSE;
1580 sec = *psec;
1581 }
1582
1583 /* There are multiple definitions of a normal symbol. Skip the
1584 default symbol as well as definition from an IR object. */
1585 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1586 && !default_sym && h->def_regular
1587 && !(oldbfd != NULL
1588 && (oldbfd->flags & BFD_PLUGIN) != 0
1589 && (abfd->flags & BFD_PLUGIN) == 0))
1590 {
1591 /* Handle a multiple definition. */
1592 (*info->callbacks->multiple_definition) (info, &h->root,
1593 abfd, sec, *pvalue);
1594 *skip = TRUE;
1595 return TRUE;
1596 }
1597
1598 /* If both the old and the new symbols look like common symbols in a
1599 dynamic object, set the size of the symbol to the larger of the
1600 two. */
1601
1602 if (olddyncommon
1603 && newdyncommon
1604 && sym->st_size != h->size)
1605 {
1606 /* Since we think we have two common symbols, issue a multiple
1607 common warning if desired. Note that we only warn if the
1608 size is different. If the size is the same, we simply let
1609 the old symbol override the new one as normally happens with
1610 symbols defined in dynamic objects. */
1611
1612 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1613 bfd_link_hash_common, sym->st_size);
1614 if (sym->st_size > h->size)
1615 h->size = sym->st_size;
1616
1617 *size_change_ok = TRUE;
1618 }
1619
1620 /* If we are looking at a dynamic object, and we have found a
1621 definition, we need to see if the symbol was already defined by
1622 some other object. If so, we want to use the existing
1623 definition, and we do not want to report a multiple symbol
1624 definition error; we do this by clobbering *PSEC to be
1625 bfd_und_section_ptr.
1626
1627 We treat a common symbol as a definition if the symbol in the
1628 shared library is a function, since common symbols always
1629 represent variables; this can cause confusion in principle, but
1630 any such confusion would seem to indicate an erroneous program or
1631 shared library. We also permit a common symbol in a regular
1632 object to override a weak symbol in a shared object. */
1633
1634 if (newdyn
1635 && newdef
1636 && (olddef
1637 || (h->root.type == bfd_link_hash_common
1638 && (newweak || newfunc))))
1639 {
1640 *override = TRUE;
1641 newdef = FALSE;
1642 newdyncommon = FALSE;
1643
1644 *psec = sec = bfd_und_section_ptr;
1645 *size_change_ok = TRUE;
1646
1647 /* If we get here when the old symbol is a common symbol, then
1648 we are explicitly letting it override a weak symbol or
1649 function in a dynamic object, and we don't want to warn about
1650 a type change. If the old symbol is a defined symbol, a type
1651 change warning may still be appropriate. */
1652
1653 if (h->root.type == bfd_link_hash_common)
1654 *type_change_ok = TRUE;
1655 }
1656
1657 /* Handle the special case of an old common symbol merging with a
1658 new symbol which looks like a common symbol in a shared object.
1659 We change *PSEC and *PVALUE to make the new symbol look like a
1660 common symbol, and let _bfd_generic_link_add_one_symbol do the
1661 right thing. */
1662
1663 if (newdyncommon
1664 && h->root.type == bfd_link_hash_common)
1665 {
1666 *override = TRUE;
1667 newdef = FALSE;
1668 newdyncommon = FALSE;
1669 *pvalue = sym->st_size;
1670 *psec = sec = bed->common_section (oldsec);
1671 *size_change_ok = TRUE;
1672 }
1673
1674 /* Skip weak definitions of symbols that are already defined. */
1675 if (newdef && olddef && newweak)
1676 {
1677 /* Don't skip new non-IR weak syms. */
1678 if (!(oldbfd != NULL
1679 && (oldbfd->flags & BFD_PLUGIN) != 0
1680 && (abfd->flags & BFD_PLUGIN) == 0))
1681 {
1682 newdef = FALSE;
1683 *skip = TRUE;
1684 }
1685
1686 /* Merge st_other. If the symbol already has a dynamic index,
1687 but visibility says it should not be visible, turn it into a
1688 local symbol. */
1689 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1690 if (h->dynindx != -1)
1691 switch (ELF_ST_VISIBILITY (h->other))
1692 {
1693 case STV_INTERNAL:
1694 case STV_HIDDEN:
1695 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1696 break;
1697 }
1698 }
1699
1700 /* If the old symbol is from a dynamic object, and the new symbol is
1701 a definition which is not from a dynamic object, then the new
1702 symbol overrides the old symbol. Symbols from regular files
1703 always take precedence over symbols from dynamic objects, even if
1704 they are defined after the dynamic object in the link.
1705
1706 As above, we again permit a common symbol in a regular object to
1707 override a definition in a shared object if the shared object
1708 symbol is a function or is weak. */
1709
1710 flip = NULL;
1711 if (!newdyn
1712 && (newdef
1713 || (bfd_is_com_section (sec)
1714 && (oldweak || oldfunc)))
1715 && olddyn
1716 && olddef
1717 && h->def_dynamic)
1718 {
1719 /* Change the hash table entry to undefined, and let
1720 _bfd_generic_link_add_one_symbol do the right thing with the
1721 new definition. */
1722
1723 h->root.type = bfd_link_hash_undefined;
1724 h->root.u.undef.abfd = h->root.u.def.section->owner;
1725 *size_change_ok = TRUE;
1726
1727 olddef = FALSE;
1728 olddyncommon = FALSE;
1729
1730 /* We again permit a type change when a common symbol may be
1731 overriding a function. */
1732
1733 if (bfd_is_com_section (sec))
1734 {
1735 if (oldfunc)
1736 {
1737 /* If a common symbol overrides a function, make sure
1738 that it isn't defined dynamically nor has type
1739 function. */
1740 h->def_dynamic = 0;
1741 h->type = STT_NOTYPE;
1742 }
1743 *type_change_ok = TRUE;
1744 }
1745
1746 if (hi->root.type == bfd_link_hash_indirect)
1747 flip = hi;
1748 else
1749 /* This union may have been set to be non-NULL when this symbol
1750 was seen in a dynamic object. We must force the union to be
1751 NULL, so that it is correct for a regular symbol. */
1752 h->verinfo.vertree = NULL;
1753 }
1754
1755 /* Handle the special case of a new common symbol merging with an
1756 old symbol that looks like it might be a common symbol defined in
1757 a shared object. Note that we have already handled the case in
1758 which a new common symbol should simply override the definition
1759 in the shared library. */
1760
1761 if (! newdyn
1762 && bfd_is_com_section (sec)
1763 && olddyncommon)
1764 {
1765 /* It would be best if we could set the hash table entry to a
1766 common symbol, but we don't know what to use for the section
1767 or the alignment. */
1768 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1769 bfd_link_hash_common, sym->st_size);
1770
1771 /* If the presumed common symbol in the dynamic object is
1772 larger, pretend that the new symbol has its size. */
1773
1774 if (h->size > *pvalue)
1775 *pvalue = h->size;
1776
1777 /* We need to remember the alignment required by the symbol
1778 in the dynamic object. */
1779 BFD_ASSERT (pold_alignment);
1780 *pold_alignment = h->root.u.def.section->alignment_power;
1781
1782 olddef = FALSE;
1783 olddyncommon = FALSE;
1784
1785 h->root.type = bfd_link_hash_undefined;
1786 h->root.u.undef.abfd = h->root.u.def.section->owner;
1787
1788 *size_change_ok = TRUE;
1789 *type_change_ok = TRUE;
1790
1791 if (hi->root.type == bfd_link_hash_indirect)
1792 flip = hi;
1793 else
1794 h->verinfo.vertree = NULL;
1795 }
1796
1797 if (flip != NULL)
1798 {
1799 /* Handle the case where we had a versioned symbol in a dynamic
1800 library and now find a definition in a normal object. In this
1801 case, we make the versioned symbol point to the normal one. */
1802 flip->root.type = h->root.type;
1803 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1804 h->root.type = bfd_link_hash_indirect;
1805 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1806 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1807 if (h->def_dynamic)
1808 {
1809 h->def_dynamic = 0;
1810 flip->ref_dynamic = 1;
1811 }
1812 }
1813
1814 return TRUE;
1815 }
1816
1817 /* This function is called to create an indirect symbol from the
1818 default for the symbol with the default version if needed. The
1819 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1820 set DYNSYM if the new indirect symbol is dynamic. */
1821
1822 static bfd_boolean
1823 _bfd_elf_add_default_symbol (bfd *abfd,
1824 struct bfd_link_info *info,
1825 struct elf_link_hash_entry *h,
1826 const char *name,
1827 Elf_Internal_Sym *sym,
1828 asection *sec,
1829 bfd_vma value,
1830 bfd **poldbfd,
1831 bfd_boolean *dynsym)
1832 {
1833 bfd_boolean type_change_ok;
1834 bfd_boolean size_change_ok;
1835 bfd_boolean skip;
1836 char *shortname;
1837 struct elf_link_hash_entry *hi;
1838 struct bfd_link_hash_entry *bh;
1839 const struct elf_backend_data *bed;
1840 bfd_boolean collect;
1841 bfd_boolean dynamic;
1842 bfd_boolean override;
1843 char *p;
1844 size_t len, shortlen;
1845 asection *tmp_sec;
1846 bfd_boolean matched;
1847
1848 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1849 return TRUE;
1850
1851 /* If this symbol has a version, and it is the default version, we
1852 create an indirect symbol from the default name to the fully
1853 decorated name. This will cause external references which do not
1854 specify a version to be bound to this version of the symbol. */
1855 p = strchr (name, ELF_VER_CHR);
1856 if (h->versioned == unknown)
1857 {
1858 if (p == NULL)
1859 {
1860 h->versioned = unversioned;
1861 return TRUE;
1862 }
1863 else
1864 {
1865 if (p[1] != ELF_VER_CHR)
1866 {
1867 h->versioned = versioned_hidden;
1868 return TRUE;
1869 }
1870 else
1871 h->versioned = versioned;
1872 }
1873 }
1874 else
1875 {
1876 /* PR ld/19073: We may see an unversioned definition after the
1877 default version. */
1878 if (p == NULL)
1879 return TRUE;
1880 }
1881
1882 bed = get_elf_backend_data (abfd);
1883 collect = bed->collect;
1884 dynamic = (abfd->flags & DYNAMIC) != 0;
1885
1886 shortlen = p - name;
1887 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1888 if (shortname == NULL)
1889 return FALSE;
1890 memcpy (shortname, name, shortlen);
1891 shortname[shortlen] = '\0';
1892
1893 /* We are going to create a new symbol. Merge it with any existing
1894 symbol with this name. For the purposes of the merge, act as
1895 though we were defining the symbol we just defined, although we
1896 actually going to define an indirect symbol. */
1897 type_change_ok = FALSE;
1898 size_change_ok = FALSE;
1899 matched = TRUE;
1900 tmp_sec = sec;
1901 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1902 &hi, poldbfd, NULL, NULL, &skip, &override,
1903 &type_change_ok, &size_change_ok, &matched))
1904 return FALSE;
1905
1906 if (skip)
1907 goto nondefault;
1908
1909 if (hi->def_regular)
1910 {
1911 /* If the undecorated symbol will have a version added by a
1912 script different to H, then don't indirect to/from the
1913 undecorated symbol. This isn't ideal because we may not yet
1914 have seen symbol versions, if given by a script on the
1915 command line rather than via --version-script. */
1916 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1917 {
1918 bfd_boolean hide;
1919
1920 hi->verinfo.vertree
1921 = bfd_find_version_for_sym (info->version_info,
1922 hi->root.root.string, &hide);
1923 if (hi->verinfo.vertree != NULL && hide)
1924 {
1925 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1926 goto nondefault;
1927 }
1928 }
1929 if (hi->verinfo.vertree != NULL
1930 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1931 goto nondefault;
1932 }
1933
1934 if (! override)
1935 {
1936 /* Add the default symbol if not performing a relocatable link. */
1937 if (! bfd_link_relocatable (info))
1938 {
1939 bh = &hi->root;
1940 if (bh->type == bfd_link_hash_defined
1941 && bh->u.def.section->owner != NULL
1942 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
1943 {
1944 /* Mark the previous definition from IR object as
1945 undefined so that the generic linker will override
1946 it. */
1947 bh->type = bfd_link_hash_undefined;
1948 bh->u.undef.abfd = bh->u.def.section->owner;
1949 }
1950 if (! (_bfd_generic_link_add_one_symbol
1951 (info, abfd, shortname, BSF_INDIRECT,
1952 bfd_ind_section_ptr,
1953 0, name, FALSE, collect, &bh)))
1954 return FALSE;
1955 hi = (struct elf_link_hash_entry *) bh;
1956 }
1957 }
1958 else
1959 {
1960 /* In this case the symbol named SHORTNAME is overriding the
1961 indirect symbol we want to add. We were planning on making
1962 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1963 is the name without a version. NAME is the fully versioned
1964 name, and it is the default version.
1965
1966 Overriding means that we already saw a definition for the
1967 symbol SHORTNAME in a regular object, and it is overriding
1968 the symbol defined in the dynamic object.
1969
1970 When this happens, we actually want to change NAME, the
1971 symbol we just added, to refer to SHORTNAME. This will cause
1972 references to NAME in the shared object to become references
1973 to SHORTNAME in the regular object. This is what we expect
1974 when we override a function in a shared object: that the
1975 references in the shared object will be mapped to the
1976 definition in the regular object. */
1977
1978 while (hi->root.type == bfd_link_hash_indirect
1979 || hi->root.type == bfd_link_hash_warning)
1980 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1981
1982 h->root.type = bfd_link_hash_indirect;
1983 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1984 if (h->def_dynamic)
1985 {
1986 h->def_dynamic = 0;
1987 hi->ref_dynamic = 1;
1988 if (hi->ref_regular
1989 || hi->def_regular)
1990 {
1991 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1992 return FALSE;
1993 }
1994 }
1995
1996 /* Now set HI to H, so that the following code will set the
1997 other fields correctly. */
1998 hi = h;
1999 }
2000
2001 /* Check if HI is a warning symbol. */
2002 if (hi->root.type == bfd_link_hash_warning)
2003 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2004
2005 /* If there is a duplicate definition somewhere, then HI may not
2006 point to an indirect symbol. We will have reported an error to
2007 the user in that case. */
2008
2009 if (hi->root.type == bfd_link_hash_indirect)
2010 {
2011 struct elf_link_hash_entry *ht;
2012
2013 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2014 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2015
2016 /* A reference to the SHORTNAME symbol from a dynamic library
2017 will be satisfied by the versioned symbol at runtime. In
2018 effect, we have a reference to the versioned symbol. */
2019 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2020 hi->dynamic_def |= ht->dynamic_def;
2021
2022 /* See if the new flags lead us to realize that the symbol must
2023 be dynamic. */
2024 if (! *dynsym)
2025 {
2026 if (! dynamic)
2027 {
2028 if (! bfd_link_executable (info)
2029 || hi->def_dynamic
2030 || hi->ref_dynamic)
2031 *dynsym = TRUE;
2032 }
2033 else
2034 {
2035 if (hi->ref_regular)
2036 *dynsym = TRUE;
2037 }
2038 }
2039 }
2040
2041 /* We also need to define an indirection from the nondefault version
2042 of the symbol. */
2043
2044 nondefault:
2045 len = strlen (name);
2046 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2047 if (shortname == NULL)
2048 return FALSE;
2049 memcpy (shortname, name, shortlen);
2050 memcpy (shortname + shortlen, p + 1, len - shortlen);
2051
2052 /* Once again, merge with any existing symbol. */
2053 type_change_ok = FALSE;
2054 size_change_ok = FALSE;
2055 tmp_sec = sec;
2056 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2057 &hi, poldbfd, NULL, NULL, &skip, &override,
2058 &type_change_ok, &size_change_ok, &matched))
2059 return FALSE;
2060
2061 if (skip)
2062 return TRUE;
2063
2064 if (override)
2065 {
2066 /* Here SHORTNAME is a versioned name, so we don't expect to see
2067 the type of override we do in the case above unless it is
2068 overridden by a versioned definition. */
2069 if (hi->root.type != bfd_link_hash_defined
2070 && hi->root.type != bfd_link_hash_defweak)
2071 _bfd_error_handler
2072 /* xgettext:c-format */
2073 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2074 abfd, shortname);
2075 }
2076 else
2077 {
2078 bh = &hi->root;
2079 if (! (_bfd_generic_link_add_one_symbol
2080 (info, abfd, shortname, BSF_INDIRECT,
2081 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
2082 return FALSE;
2083 hi = (struct elf_link_hash_entry *) bh;
2084
2085 /* If there is a duplicate definition somewhere, then HI may not
2086 point to an indirect symbol. We will have reported an error
2087 to the user in that case. */
2088
2089 if (hi->root.type == bfd_link_hash_indirect)
2090 {
2091 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2092 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2093 hi->dynamic_def |= h->dynamic_def;
2094
2095 /* See if the new flags lead us to realize that the symbol
2096 must be dynamic. */
2097 if (! *dynsym)
2098 {
2099 if (! dynamic)
2100 {
2101 if (! bfd_link_executable (info)
2102 || hi->ref_dynamic)
2103 *dynsym = TRUE;
2104 }
2105 else
2106 {
2107 if (hi->ref_regular)
2108 *dynsym = TRUE;
2109 }
2110 }
2111 }
2112 }
2113
2114 return TRUE;
2115 }
2116 \f
2117 /* This routine is used to export all defined symbols into the dynamic
2118 symbol table. It is called via elf_link_hash_traverse. */
2119
2120 static bfd_boolean
2121 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2122 {
2123 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2124
2125 /* Ignore indirect symbols. These are added by the versioning code. */
2126 if (h->root.type == bfd_link_hash_indirect)
2127 return TRUE;
2128
2129 /* Ignore this if we won't export it. */
2130 if (!eif->info->export_dynamic && !h->dynamic)
2131 return TRUE;
2132
2133 if (h->dynindx == -1
2134 && (h->def_regular || h->ref_regular)
2135 && ! bfd_hide_sym_by_version (eif->info->version_info,
2136 h->root.root.string))
2137 {
2138 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2139 {
2140 eif->failed = TRUE;
2141 return FALSE;
2142 }
2143 }
2144
2145 return TRUE;
2146 }
2147 \f
2148 /* Look through the symbols which are defined in other shared
2149 libraries and referenced here. Update the list of version
2150 dependencies. This will be put into the .gnu.version_r section.
2151 This function is called via elf_link_hash_traverse. */
2152
2153 static bfd_boolean
2154 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2155 void *data)
2156 {
2157 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2158 Elf_Internal_Verneed *t;
2159 Elf_Internal_Vernaux *a;
2160 bfd_size_type amt;
2161
2162 /* We only care about symbols defined in shared objects with version
2163 information. */
2164 if (!h->def_dynamic
2165 || h->def_regular
2166 || h->dynindx == -1
2167 || h->verinfo.verdef == NULL
2168 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2169 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2170 return TRUE;
2171
2172 /* See if we already know about this version. */
2173 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2174 t != NULL;
2175 t = t->vn_nextref)
2176 {
2177 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2178 continue;
2179
2180 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2181 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2182 return TRUE;
2183
2184 break;
2185 }
2186
2187 /* This is a new version. Add it to tree we are building. */
2188
2189 if (t == NULL)
2190 {
2191 amt = sizeof *t;
2192 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2193 if (t == NULL)
2194 {
2195 rinfo->failed = TRUE;
2196 return FALSE;
2197 }
2198
2199 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2200 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2201 elf_tdata (rinfo->info->output_bfd)->verref = t;
2202 }
2203
2204 amt = sizeof *a;
2205 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2206 if (a == NULL)
2207 {
2208 rinfo->failed = TRUE;
2209 return FALSE;
2210 }
2211
2212 /* Note that we are copying a string pointer here, and testing it
2213 above. If bfd_elf_string_from_elf_section is ever changed to
2214 discard the string data when low in memory, this will have to be
2215 fixed. */
2216 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2217
2218 a->vna_flags = h->verinfo.verdef->vd_flags;
2219 a->vna_nextptr = t->vn_auxptr;
2220
2221 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2222 ++rinfo->vers;
2223
2224 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2225
2226 t->vn_auxptr = a;
2227
2228 return TRUE;
2229 }
2230
2231 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2232 hidden. Set *T_P to NULL if there is no match. */
2233
2234 static bfd_boolean
2235 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2236 struct elf_link_hash_entry *h,
2237 const char *version_p,
2238 struct bfd_elf_version_tree **t_p,
2239 bfd_boolean *hide)
2240 {
2241 struct bfd_elf_version_tree *t;
2242
2243 /* Look for the version. If we find it, it is no longer weak. */
2244 for (t = info->version_info; t != NULL; t = t->next)
2245 {
2246 if (strcmp (t->name, version_p) == 0)
2247 {
2248 size_t len;
2249 char *alc;
2250 struct bfd_elf_version_expr *d;
2251
2252 len = version_p - h->root.root.string;
2253 alc = (char *) bfd_malloc (len);
2254 if (alc == NULL)
2255 return FALSE;
2256 memcpy (alc, h->root.root.string, len - 1);
2257 alc[len - 1] = '\0';
2258 if (alc[len - 2] == ELF_VER_CHR)
2259 alc[len - 2] = '\0';
2260
2261 h->verinfo.vertree = t;
2262 t->used = TRUE;
2263 d = NULL;
2264
2265 if (t->globals.list != NULL)
2266 d = (*t->match) (&t->globals, NULL, alc);
2267
2268 /* See if there is anything to force this symbol to
2269 local scope. */
2270 if (d == NULL && t->locals.list != NULL)
2271 {
2272 d = (*t->match) (&t->locals, NULL, alc);
2273 if (d != NULL
2274 && h->dynindx != -1
2275 && ! info->export_dynamic)
2276 *hide = TRUE;
2277 }
2278
2279 free (alc);
2280 break;
2281 }
2282 }
2283
2284 *t_p = t;
2285
2286 return TRUE;
2287 }
2288
2289 /* Return TRUE if the symbol H is hidden by version script. */
2290
2291 bfd_boolean
2292 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2293 struct elf_link_hash_entry *h)
2294 {
2295 const char *p;
2296 bfd_boolean hide = FALSE;
2297 const struct elf_backend_data *bed
2298 = get_elf_backend_data (info->output_bfd);
2299
2300 /* Version script only hides symbols defined in regular objects. */
2301 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2302 return TRUE;
2303
2304 p = strchr (h->root.root.string, ELF_VER_CHR);
2305 if (p != NULL && h->verinfo.vertree == NULL)
2306 {
2307 struct bfd_elf_version_tree *t;
2308
2309 ++p;
2310 if (*p == ELF_VER_CHR)
2311 ++p;
2312
2313 if (*p != '\0'
2314 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2315 && hide)
2316 {
2317 if (hide)
2318 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2319 return TRUE;
2320 }
2321 }
2322
2323 /* If we don't have a version for this symbol, see if we can find
2324 something. */
2325 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2326 {
2327 h->verinfo.vertree
2328 = bfd_find_version_for_sym (info->version_info,
2329 h->root.root.string, &hide);
2330 if (h->verinfo.vertree != NULL && hide)
2331 {
2332 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2333 return TRUE;
2334 }
2335 }
2336
2337 return FALSE;
2338 }
2339
2340 /* Figure out appropriate versions for all the symbols. We may not
2341 have the version number script until we have read all of the input
2342 files, so until that point we don't know which symbols should be
2343 local. This function is called via elf_link_hash_traverse. */
2344
2345 static bfd_boolean
2346 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2347 {
2348 struct elf_info_failed *sinfo;
2349 struct bfd_link_info *info;
2350 const struct elf_backend_data *bed;
2351 struct elf_info_failed eif;
2352 char *p;
2353 bfd_boolean hide;
2354
2355 sinfo = (struct elf_info_failed *) data;
2356 info = sinfo->info;
2357
2358 /* Fix the symbol flags. */
2359 eif.failed = FALSE;
2360 eif.info = info;
2361 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2362 {
2363 if (eif.failed)
2364 sinfo->failed = TRUE;
2365 return FALSE;
2366 }
2367
2368 bed = get_elf_backend_data (info->output_bfd);
2369
2370 /* We only need version numbers for symbols defined in regular
2371 objects. */
2372 if (!h->def_regular)
2373 {
2374 /* Hide symbols defined in discarded input sections. */
2375 if ((h->root.type == bfd_link_hash_defined
2376 || h->root.type == bfd_link_hash_defweak)
2377 && discarded_section (h->root.u.def.section))
2378 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2379 return TRUE;
2380 }
2381
2382 hide = FALSE;
2383 p = strchr (h->root.root.string, ELF_VER_CHR);
2384 if (p != NULL && h->verinfo.vertree == NULL)
2385 {
2386 struct bfd_elf_version_tree *t;
2387
2388 ++p;
2389 if (*p == ELF_VER_CHR)
2390 ++p;
2391
2392 /* If there is no version string, we can just return out. */
2393 if (*p == '\0')
2394 return TRUE;
2395
2396 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2397 {
2398 sinfo->failed = TRUE;
2399 return FALSE;
2400 }
2401
2402 if (hide)
2403 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2404
2405 /* If we are building an application, we need to create a
2406 version node for this version. */
2407 if (t == NULL && bfd_link_executable (info))
2408 {
2409 struct bfd_elf_version_tree **pp;
2410 int version_index;
2411
2412 /* If we aren't going to export this symbol, we don't need
2413 to worry about it. */
2414 if (h->dynindx == -1)
2415 return TRUE;
2416
2417 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2418 sizeof *t);
2419 if (t == NULL)
2420 {
2421 sinfo->failed = TRUE;
2422 return FALSE;
2423 }
2424
2425 t->name = p;
2426 t->name_indx = (unsigned int) -1;
2427 t->used = TRUE;
2428
2429 version_index = 1;
2430 /* Don't count anonymous version tag. */
2431 if (sinfo->info->version_info != NULL
2432 && sinfo->info->version_info->vernum == 0)
2433 version_index = 0;
2434 for (pp = &sinfo->info->version_info;
2435 *pp != NULL;
2436 pp = &(*pp)->next)
2437 ++version_index;
2438 t->vernum = version_index;
2439
2440 *pp = t;
2441
2442 h->verinfo.vertree = t;
2443 }
2444 else if (t == NULL)
2445 {
2446 /* We could not find the version for a symbol when
2447 generating a shared archive. Return an error. */
2448 _bfd_error_handler
2449 /* xgettext:c-format */
2450 (_("%pB: version node not found for symbol %s"),
2451 info->output_bfd, h->root.root.string);
2452 bfd_set_error (bfd_error_bad_value);
2453 sinfo->failed = TRUE;
2454 return FALSE;
2455 }
2456 }
2457
2458 /* If we don't have a version for this symbol, see if we can find
2459 something. */
2460 if (!hide
2461 && h->verinfo.vertree == NULL
2462 && sinfo->info->version_info != NULL)
2463 {
2464 h->verinfo.vertree
2465 = bfd_find_version_for_sym (sinfo->info->version_info,
2466 h->root.root.string, &hide);
2467 if (h->verinfo.vertree != NULL && hide)
2468 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2469 }
2470
2471 return TRUE;
2472 }
2473 \f
2474 /* Read and swap the relocs from the section indicated by SHDR. This
2475 may be either a REL or a RELA section. The relocations are
2476 translated into RELA relocations and stored in INTERNAL_RELOCS,
2477 which should have already been allocated to contain enough space.
2478 The EXTERNAL_RELOCS are a buffer where the external form of the
2479 relocations should be stored.
2480
2481 Returns FALSE if something goes wrong. */
2482
2483 static bfd_boolean
2484 elf_link_read_relocs_from_section (bfd *abfd,
2485 asection *sec,
2486 Elf_Internal_Shdr *shdr,
2487 void *external_relocs,
2488 Elf_Internal_Rela *internal_relocs)
2489 {
2490 const struct elf_backend_data *bed;
2491 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2492 const bfd_byte *erela;
2493 const bfd_byte *erelaend;
2494 Elf_Internal_Rela *irela;
2495 Elf_Internal_Shdr *symtab_hdr;
2496 size_t nsyms;
2497
2498 /* Position ourselves at the start of the section. */
2499 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2500 return FALSE;
2501
2502 /* Read the relocations. */
2503 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2504 return FALSE;
2505
2506 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2507 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2508
2509 bed = get_elf_backend_data (abfd);
2510
2511 /* Convert the external relocations to the internal format. */
2512 if (shdr->sh_entsize == bed->s->sizeof_rel)
2513 swap_in = bed->s->swap_reloc_in;
2514 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2515 swap_in = bed->s->swap_reloca_in;
2516 else
2517 {
2518 bfd_set_error (bfd_error_wrong_format);
2519 return FALSE;
2520 }
2521
2522 erela = (const bfd_byte *) external_relocs;
2523 /* Setting erelaend like this and comparing with <= handles case of
2524 a fuzzed object with sh_size not a multiple of sh_entsize. */
2525 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
2526 irela = internal_relocs;
2527 while (erela <= erelaend)
2528 {
2529 bfd_vma r_symndx;
2530
2531 (*swap_in) (abfd, erela, irela);
2532 r_symndx = ELF32_R_SYM (irela->r_info);
2533 if (bed->s->arch_size == 64)
2534 r_symndx >>= 24;
2535 if (nsyms > 0)
2536 {
2537 if ((size_t) r_symndx >= nsyms)
2538 {
2539 _bfd_error_handler
2540 /* xgettext:c-format */
2541 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2542 " for offset %#" PRIx64 " in section `%pA'"),
2543 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2544 (uint64_t) irela->r_offset, sec);
2545 bfd_set_error (bfd_error_bad_value);
2546 return FALSE;
2547 }
2548 }
2549 else if (r_symndx != STN_UNDEF)
2550 {
2551 _bfd_error_handler
2552 /* xgettext:c-format */
2553 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2554 " for offset %#" PRIx64 " in section `%pA'"
2555 " when the object file has no symbol table"),
2556 abfd, (uint64_t) r_symndx,
2557 (uint64_t) irela->r_offset, sec);
2558 bfd_set_error (bfd_error_bad_value);
2559 return FALSE;
2560 }
2561 irela += bed->s->int_rels_per_ext_rel;
2562 erela += shdr->sh_entsize;
2563 }
2564
2565 return TRUE;
2566 }
2567
2568 /* Read and swap the relocs for a section O. They may have been
2569 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2570 not NULL, they are used as buffers to read into. They are known to
2571 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2572 the return value is allocated using either malloc or bfd_alloc,
2573 according to the KEEP_MEMORY argument. If O has two relocation
2574 sections (both REL and RELA relocations), then the REL_HDR
2575 relocations will appear first in INTERNAL_RELOCS, followed by the
2576 RELA_HDR relocations. */
2577
2578 Elf_Internal_Rela *
2579 _bfd_elf_link_read_relocs (bfd *abfd,
2580 asection *o,
2581 void *external_relocs,
2582 Elf_Internal_Rela *internal_relocs,
2583 bfd_boolean keep_memory)
2584 {
2585 void *alloc1 = NULL;
2586 Elf_Internal_Rela *alloc2 = NULL;
2587 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2588 struct bfd_elf_section_data *esdo = elf_section_data (o);
2589 Elf_Internal_Rela *internal_rela_relocs;
2590
2591 if (esdo->relocs != NULL)
2592 return esdo->relocs;
2593
2594 if (o->reloc_count == 0)
2595 return NULL;
2596
2597 if (internal_relocs == NULL)
2598 {
2599 bfd_size_type size;
2600
2601 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2602 if (keep_memory)
2603 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2604 else
2605 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2606 if (internal_relocs == NULL)
2607 goto error_return;
2608 }
2609
2610 if (external_relocs == NULL)
2611 {
2612 bfd_size_type size = 0;
2613
2614 if (esdo->rel.hdr)
2615 size += esdo->rel.hdr->sh_size;
2616 if (esdo->rela.hdr)
2617 size += esdo->rela.hdr->sh_size;
2618
2619 alloc1 = bfd_malloc (size);
2620 if (alloc1 == NULL)
2621 goto error_return;
2622 external_relocs = alloc1;
2623 }
2624
2625 internal_rela_relocs = internal_relocs;
2626 if (esdo->rel.hdr)
2627 {
2628 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2629 external_relocs,
2630 internal_relocs))
2631 goto error_return;
2632 external_relocs = (((bfd_byte *) external_relocs)
2633 + esdo->rel.hdr->sh_size);
2634 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2635 * bed->s->int_rels_per_ext_rel);
2636 }
2637
2638 if (esdo->rela.hdr
2639 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2640 external_relocs,
2641 internal_rela_relocs)))
2642 goto error_return;
2643
2644 /* Cache the results for next time, if we can. */
2645 if (keep_memory)
2646 esdo->relocs = internal_relocs;
2647
2648 if (alloc1 != NULL)
2649 free (alloc1);
2650
2651 /* Don't free alloc2, since if it was allocated we are passing it
2652 back (under the name of internal_relocs). */
2653
2654 return internal_relocs;
2655
2656 error_return:
2657 if (alloc1 != NULL)
2658 free (alloc1);
2659 if (alloc2 != NULL)
2660 {
2661 if (keep_memory)
2662 bfd_release (abfd, alloc2);
2663 else
2664 free (alloc2);
2665 }
2666 return NULL;
2667 }
2668
2669 /* Compute the size of, and allocate space for, REL_HDR which is the
2670 section header for a section containing relocations for O. */
2671
2672 static bfd_boolean
2673 _bfd_elf_link_size_reloc_section (bfd *abfd,
2674 struct bfd_elf_section_reloc_data *reldata)
2675 {
2676 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2677
2678 /* That allows us to calculate the size of the section. */
2679 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2680
2681 /* The contents field must last into write_object_contents, so we
2682 allocate it with bfd_alloc rather than malloc. Also since we
2683 cannot be sure that the contents will actually be filled in,
2684 we zero the allocated space. */
2685 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2686 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2687 return FALSE;
2688
2689 if (reldata->hashes == NULL && reldata->count)
2690 {
2691 struct elf_link_hash_entry **p;
2692
2693 p = ((struct elf_link_hash_entry **)
2694 bfd_zmalloc (reldata->count * sizeof (*p)));
2695 if (p == NULL)
2696 return FALSE;
2697
2698 reldata->hashes = p;
2699 }
2700
2701 return TRUE;
2702 }
2703
2704 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2705 originated from the section given by INPUT_REL_HDR) to the
2706 OUTPUT_BFD. */
2707
2708 bfd_boolean
2709 _bfd_elf_link_output_relocs (bfd *output_bfd,
2710 asection *input_section,
2711 Elf_Internal_Shdr *input_rel_hdr,
2712 Elf_Internal_Rela *internal_relocs,
2713 struct elf_link_hash_entry **rel_hash
2714 ATTRIBUTE_UNUSED)
2715 {
2716 Elf_Internal_Rela *irela;
2717 Elf_Internal_Rela *irelaend;
2718 bfd_byte *erel;
2719 struct bfd_elf_section_reloc_data *output_reldata;
2720 asection *output_section;
2721 const struct elf_backend_data *bed;
2722 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2723 struct bfd_elf_section_data *esdo;
2724
2725 output_section = input_section->output_section;
2726
2727 bed = get_elf_backend_data (output_bfd);
2728 esdo = elf_section_data (output_section);
2729 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2730 {
2731 output_reldata = &esdo->rel;
2732 swap_out = bed->s->swap_reloc_out;
2733 }
2734 else if (esdo->rela.hdr
2735 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2736 {
2737 output_reldata = &esdo->rela;
2738 swap_out = bed->s->swap_reloca_out;
2739 }
2740 else
2741 {
2742 _bfd_error_handler
2743 /* xgettext:c-format */
2744 (_("%pB: relocation size mismatch in %pB section %pA"),
2745 output_bfd, input_section->owner, input_section);
2746 bfd_set_error (bfd_error_wrong_format);
2747 return FALSE;
2748 }
2749
2750 erel = output_reldata->hdr->contents;
2751 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2752 irela = internal_relocs;
2753 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2754 * bed->s->int_rels_per_ext_rel);
2755 while (irela < irelaend)
2756 {
2757 (*swap_out) (output_bfd, irela, erel);
2758 irela += bed->s->int_rels_per_ext_rel;
2759 erel += input_rel_hdr->sh_entsize;
2760 }
2761
2762 /* Bump the counter, so that we know where to add the next set of
2763 relocations. */
2764 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2765
2766 return TRUE;
2767 }
2768 \f
2769 /* Make weak undefined symbols in PIE dynamic. */
2770
2771 bfd_boolean
2772 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2773 struct elf_link_hash_entry *h)
2774 {
2775 if (bfd_link_pie (info)
2776 && h->dynindx == -1
2777 && h->root.type == bfd_link_hash_undefweak)
2778 return bfd_elf_link_record_dynamic_symbol (info, h);
2779
2780 return TRUE;
2781 }
2782
2783 /* Fix up the flags for a symbol. This handles various cases which
2784 can only be fixed after all the input files are seen. This is
2785 currently called by both adjust_dynamic_symbol and
2786 assign_sym_version, which is unnecessary but perhaps more robust in
2787 the face of future changes. */
2788
2789 static bfd_boolean
2790 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2791 struct elf_info_failed *eif)
2792 {
2793 const struct elf_backend_data *bed;
2794
2795 /* If this symbol was mentioned in a non-ELF file, try to set
2796 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2797 permit a non-ELF file to correctly refer to a symbol defined in
2798 an ELF dynamic object. */
2799 if (h->non_elf)
2800 {
2801 while (h->root.type == bfd_link_hash_indirect)
2802 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2803
2804 if (h->root.type != bfd_link_hash_defined
2805 && h->root.type != bfd_link_hash_defweak)
2806 {
2807 h->ref_regular = 1;
2808 h->ref_regular_nonweak = 1;
2809 }
2810 else
2811 {
2812 if (h->root.u.def.section->owner != NULL
2813 && (bfd_get_flavour (h->root.u.def.section->owner)
2814 == bfd_target_elf_flavour))
2815 {
2816 h->ref_regular = 1;
2817 h->ref_regular_nonweak = 1;
2818 }
2819 else
2820 h->def_regular = 1;
2821 }
2822
2823 if (h->dynindx == -1
2824 && (h->def_dynamic
2825 || h->ref_dynamic))
2826 {
2827 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2828 {
2829 eif->failed = TRUE;
2830 return FALSE;
2831 }
2832 }
2833 }
2834 else
2835 {
2836 /* Unfortunately, NON_ELF is only correct if the symbol
2837 was first seen in a non-ELF file. Fortunately, if the symbol
2838 was first seen in an ELF file, we're probably OK unless the
2839 symbol was defined in a non-ELF file. Catch that case here.
2840 FIXME: We're still in trouble if the symbol was first seen in
2841 a dynamic object, and then later in a non-ELF regular object. */
2842 if ((h->root.type == bfd_link_hash_defined
2843 || h->root.type == bfd_link_hash_defweak)
2844 && !h->def_regular
2845 && (h->root.u.def.section->owner != NULL
2846 ? (bfd_get_flavour (h->root.u.def.section->owner)
2847 != bfd_target_elf_flavour)
2848 : (bfd_is_abs_section (h->root.u.def.section)
2849 && !h->def_dynamic)))
2850 h->def_regular = 1;
2851 }
2852
2853 /* Backend specific symbol fixup. */
2854 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2855 if (bed->elf_backend_fixup_symbol
2856 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2857 return FALSE;
2858
2859 /* If this is a final link, and the symbol was defined as a common
2860 symbol in a regular object file, and there was no definition in
2861 any dynamic object, then the linker will have allocated space for
2862 the symbol in a common section but the DEF_REGULAR
2863 flag will not have been set. */
2864 if (h->root.type == bfd_link_hash_defined
2865 && !h->def_regular
2866 && h->ref_regular
2867 && !h->def_dynamic
2868 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2869 h->def_regular = 1;
2870
2871 /* Symbols defined in discarded sections shouldn't be dynamic. */
2872 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
2873 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2874
2875 /* If a weak undefined symbol has non-default visibility, we also
2876 hide it from the dynamic linker. */
2877 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2878 && h->root.type == bfd_link_hash_undefweak)
2879 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2880
2881 /* A hidden versioned symbol in executable should be forced local if
2882 it is is locally defined, not referenced by shared library and not
2883 exported. */
2884 else if (bfd_link_executable (eif->info)
2885 && h->versioned == versioned_hidden
2886 && !eif->info->export_dynamic
2887 && !h->dynamic
2888 && !h->ref_dynamic
2889 && h->def_regular)
2890 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2891
2892 /* If -Bsymbolic was used (which means to bind references to global
2893 symbols to the definition within the shared object), and this
2894 symbol was defined in a regular object, then it actually doesn't
2895 need a PLT entry. Likewise, if the symbol has non-default
2896 visibility. If the symbol has hidden or internal visibility, we
2897 will force it local. */
2898 else if (h->needs_plt
2899 && bfd_link_pic (eif->info)
2900 && is_elf_hash_table (eif->info->hash)
2901 && (SYMBOLIC_BIND (eif->info, h)
2902 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2903 && h->def_regular)
2904 {
2905 bfd_boolean force_local;
2906
2907 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2908 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2909 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2910 }
2911
2912 /* If this is a weak defined symbol in a dynamic object, and we know
2913 the real definition in the dynamic object, copy interesting flags
2914 over to the real definition. */
2915 if (h->is_weakalias)
2916 {
2917 struct elf_link_hash_entry *def = weakdef (h);
2918
2919 /* If the real definition is defined by a regular object file,
2920 don't do anything special. See the longer description in
2921 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
2922 bfd_link_hash_defined as it was when put on the alias list
2923 then it must have originally been a versioned symbol (for
2924 which a non-versioned indirect symbol is created) and later
2925 a definition for the non-versioned symbol is found. In that
2926 case the indirection is flipped with the versioned symbol
2927 becoming an indirect pointing at the non-versioned symbol.
2928 Thus, not an alias any more. */
2929 if (def->def_regular
2930 || def->root.type != bfd_link_hash_defined)
2931 {
2932 h = def;
2933 while ((h = h->u.alias) != def)
2934 h->is_weakalias = 0;
2935 }
2936 else
2937 {
2938 while (h->root.type == bfd_link_hash_indirect)
2939 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2940 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2941 || h->root.type == bfd_link_hash_defweak);
2942 BFD_ASSERT (def->def_dynamic);
2943 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
2944 }
2945 }
2946
2947 return TRUE;
2948 }
2949
2950 /* Make the backend pick a good value for a dynamic symbol. This is
2951 called via elf_link_hash_traverse, and also calls itself
2952 recursively. */
2953
2954 static bfd_boolean
2955 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2956 {
2957 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2958 struct elf_link_hash_table *htab;
2959 const struct elf_backend_data *bed;
2960
2961 if (! is_elf_hash_table (eif->info->hash))
2962 return FALSE;
2963
2964 /* Ignore indirect symbols. These are added by the versioning code. */
2965 if (h->root.type == bfd_link_hash_indirect)
2966 return TRUE;
2967
2968 /* Fix the symbol flags. */
2969 if (! _bfd_elf_fix_symbol_flags (h, eif))
2970 return FALSE;
2971
2972 htab = elf_hash_table (eif->info);
2973 bed = get_elf_backend_data (htab->dynobj);
2974
2975 if (h->root.type == bfd_link_hash_undefweak)
2976 {
2977 if (eif->info->dynamic_undefined_weak == 0)
2978 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2979 else if (eif->info->dynamic_undefined_weak > 0
2980 && h->ref_regular
2981 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2982 && !bfd_hide_sym_by_version (eif->info->version_info,
2983 h->root.root.string))
2984 {
2985 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2986 {
2987 eif->failed = TRUE;
2988 return FALSE;
2989 }
2990 }
2991 }
2992
2993 /* If this symbol does not require a PLT entry, and it is not
2994 defined by a dynamic object, or is not referenced by a regular
2995 object, ignore it. We do have to handle a weak defined symbol,
2996 even if no regular object refers to it, if we decided to add it
2997 to the dynamic symbol table. FIXME: Do we normally need to worry
2998 about symbols which are defined by one dynamic object and
2999 referenced by another one? */
3000 if (!h->needs_plt
3001 && h->type != STT_GNU_IFUNC
3002 && (h->def_regular
3003 || !h->def_dynamic
3004 || (!h->ref_regular
3005 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3006 {
3007 h->plt = elf_hash_table (eif->info)->init_plt_offset;
3008 return TRUE;
3009 }
3010
3011 /* If we've already adjusted this symbol, don't do it again. This
3012 can happen via a recursive call. */
3013 if (h->dynamic_adjusted)
3014 return TRUE;
3015
3016 /* Don't look at this symbol again. Note that we must set this
3017 after checking the above conditions, because we may look at a
3018 symbol once, decide not to do anything, and then get called
3019 recursively later after REF_REGULAR is set below. */
3020 h->dynamic_adjusted = 1;
3021
3022 /* If this is a weak definition, and we know a real definition, and
3023 the real symbol is not itself defined by a regular object file,
3024 then get a good value for the real definition. We handle the
3025 real symbol first, for the convenience of the backend routine.
3026
3027 Note that there is a confusing case here. If the real definition
3028 is defined by a regular object file, we don't get the real symbol
3029 from the dynamic object, but we do get the weak symbol. If the
3030 processor backend uses a COPY reloc, then if some routine in the
3031 dynamic object changes the real symbol, we will not see that
3032 change in the corresponding weak symbol. This is the way other
3033 ELF linkers work as well, and seems to be a result of the shared
3034 library model.
3035
3036 I will clarify this issue. Most SVR4 shared libraries define the
3037 variable _timezone and define timezone as a weak synonym. The
3038 tzset call changes _timezone. If you write
3039 extern int timezone;
3040 int _timezone = 5;
3041 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3042 you might expect that, since timezone is a synonym for _timezone,
3043 the same number will print both times. However, if the processor
3044 backend uses a COPY reloc, then actually timezone will be copied
3045 into your process image, and, since you define _timezone
3046 yourself, _timezone will not. Thus timezone and _timezone will
3047 wind up at different memory locations. The tzset call will set
3048 _timezone, leaving timezone unchanged. */
3049
3050 if (h->is_weakalias)
3051 {
3052 struct elf_link_hash_entry *def = weakdef (h);
3053
3054 /* If we get to this point, there is an implicit reference to
3055 the alias by a regular object file via the weak symbol H. */
3056 def->ref_regular = 1;
3057
3058 /* Ensure that the backend adjust_dynamic_symbol function sees
3059 the strong alias before H by recursively calling ourselves. */
3060 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3061 return FALSE;
3062 }
3063
3064 /* If a symbol has no type and no size and does not require a PLT
3065 entry, then we are probably about to do the wrong thing here: we
3066 are probably going to create a COPY reloc for an empty object.
3067 This case can arise when a shared object is built with assembly
3068 code, and the assembly code fails to set the symbol type. */
3069 if (h->size == 0
3070 && h->type == STT_NOTYPE
3071 && !h->needs_plt)
3072 _bfd_error_handler
3073 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3074 h->root.root.string);
3075
3076 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3077 {
3078 eif->failed = TRUE;
3079 return FALSE;
3080 }
3081
3082 return TRUE;
3083 }
3084
3085 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3086 DYNBSS. */
3087
3088 bfd_boolean
3089 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3090 struct elf_link_hash_entry *h,
3091 asection *dynbss)
3092 {
3093 unsigned int power_of_two;
3094 bfd_vma mask;
3095 asection *sec = h->root.u.def.section;
3096
3097 /* The section alignment of the definition is the maximum alignment
3098 requirement of symbols defined in the section. Since we don't
3099 know the symbol alignment requirement, we start with the
3100 maximum alignment and check low bits of the symbol address
3101 for the minimum alignment. */
3102 power_of_two = bfd_get_section_alignment (sec->owner, sec);
3103 mask = ((bfd_vma) 1 << power_of_two) - 1;
3104 while ((h->root.u.def.value & mask) != 0)
3105 {
3106 mask >>= 1;
3107 --power_of_two;
3108 }
3109
3110 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
3111 dynbss))
3112 {
3113 /* Adjust the section alignment if needed. */
3114 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
3115 power_of_two))
3116 return FALSE;
3117 }
3118
3119 /* We make sure that the symbol will be aligned properly. */
3120 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3121
3122 /* Define the symbol as being at this point in DYNBSS. */
3123 h->root.u.def.section = dynbss;
3124 h->root.u.def.value = dynbss->size;
3125
3126 /* Increment the size of DYNBSS to make room for the symbol. */
3127 dynbss->size += h->size;
3128
3129 /* No error if extern_protected_data is true. */
3130 if (h->protected_def
3131 && (!info->extern_protected_data
3132 || (info->extern_protected_data < 0
3133 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3134 info->callbacks->einfo
3135 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3136 h->root.root.string);
3137
3138 return TRUE;
3139 }
3140
3141 /* Adjust all external symbols pointing into SEC_MERGE sections
3142 to reflect the object merging within the sections. */
3143
3144 static bfd_boolean
3145 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3146 {
3147 asection *sec;
3148
3149 if ((h->root.type == bfd_link_hash_defined
3150 || h->root.type == bfd_link_hash_defweak)
3151 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3152 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3153 {
3154 bfd *output_bfd = (bfd *) data;
3155
3156 h->root.u.def.value =
3157 _bfd_merged_section_offset (output_bfd,
3158 &h->root.u.def.section,
3159 elf_section_data (sec)->sec_info,
3160 h->root.u.def.value);
3161 }
3162
3163 return TRUE;
3164 }
3165
3166 /* Returns false if the symbol referred to by H should be considered
3167 to resolve local to the current module, and true if it should be
3168 considered to bind dynamically. */
3169
3170 bfd_boolean
3171 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3172 struct bfd_link_info *info,
3173 bfd_boolean not_local_protected)
3174 {
3175 bfd_boolean binding_stays_local_p;
3176 const struct elf_backend_data *bed;
3177 struct elf_link_hash_table *hash_table;
3178
3179 if (h == NULL)
3180 return FALSE;
3181
3182 while (h->root.type == bfd_link_hash_indirect
3183 || h->root.type == bfd_link_hash_warning)
3184 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3185
3186 /* If it was forced local, then clearly it's not dynamic. */
3187 if (h->dynindx == -1)
3188 return FALSE;
3189 if (h->forced_local)
3190 return FALSE;
3191
3192 /* Identify the cases where name binding rules say that a
3193 visible symbol resolves locally. */
3194 binding_stays_local_p = (bfd_link_executable (info)
3195 || SYMBOLIC_BIND (info, h));
3196
3197 switch (ELF_ST_VISIBILITY (h->other))
3198 {
3199 case STV_INTERNAL:
3200 case STV_HIDDEN:
3201 return FALSE;
3202
3203 case STV_PROTECTED:
3204 hash_table = elf_hash_table (info);
3205 if (!is_elf_hash_table (hash_table))
3206 return FALSE;
3207
3208 bed = get_elf_backend_data (hash_table->dynobj);
3209
3210 /* Proper resolution for function pointer equality may require
3211 that these symbols perhaps be resolved dynamically, even though
3212 we should be resolving them to the current module. */
3213 if (!not_local_protected || !bed->is_function_type (h->type))
3214 binding_stays_local_p = TRUE;
3215 break;
3216
3217 default:
3218 break;
3219 }
3220
3221 /* If it isn't defined locally, then clearly it's dynamic. */
3222 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3223 return TRUE;
3224
3225 /* Otherwise, the symbol is dynamic if binding rules don't tell
3226 us that it remains local. */
3227 return !binding_stays_local_p;
3228 }
3229
3230 /* Return true if the symbol referred to by H should be considered
3231 to resolve local to the current module, and false otherwise. Differs
3232 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3233 undefined symbols. The two functions are virtually identical except
3234 for the place where dynindx == -1 is tested. If that test is true,
3235 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3236 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3237 defined symbols.
3238 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3239 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3240 treatment of undefined weak symbols. For those that do not make
3241 undefined weak symbols dynamic, both functions may return false. */
3242
3243 bfd_boolean
3244 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3245 struct bfd_link_info *info,
3246 bfd_boolean local_protected)
3247 {
3248 const struct elf_backend_data *bed;
3249 struct elf_link_hash_table *hash_table;
3250
3251 /* If it's a local sym, of course we resolve locally. */
3252 if (h == NULL)
3253 return TRUE;
3254
3255 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3256 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3257 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3258 return TRUE;
3259
3260 /* Forced local symbols resolve locally. */
3261 if (h->forced_local)
3262 return TRUE;
3263
3264 /* Common symbols that become definitions don't get the DEF_REGULAR
3265 flag set, so test it first, and don't bail out. */
3266 if (ELF_COMMON_DEF_P (h))
3267 /* Do nothing. */;
3268 /* If we don't have a definition in a regular file, then we can't
3269 resolve locally. The sym is either undefined or dynamic. */
3270 else if (!h->def_regular)
3271 return FALSE;
3272
3273 /* Non-dynamic symbols resolve locally. */
3274 if (h->dynindx == -1)
3275 return TRUE;
3276
3277 /* At this point, we know the symbol is defined and dynamic. In an
3278 executable it must resolve locally, likewise when building symbolic
3279 shared libraries. */
3280 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3281 return TRUE;
3282
3283 /* Now deal with defined dynamic symbols in shared libraries. Ones
3284 with default visibility might not resolve locally. */
3285 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3286 return FALSE;
3287
3288 hash_table = elf_hash_table (info);
3289 if (!is_elf_hash_table (hash_table))
3290 return TRUE;
3291
3292 bed = get_elf_backend_data (hash_table->dynobj);
3293
3294 /* If extern_protected_data is false, STV_PROTECTED non-function
3295 symbols are local. */
3296 if ((!info->extern_protected_data
3297 || (info->extern_protected_data < 0
3298 && !bed->extern_protected_data))
3299 && !bed->is_function_type (h->type))
3300 return TRUE;
3301
3302 /* Function pointer equality tests may require that STV_PROTECTED
3303 symbols be treated as dynamic symbols. If the address of a
3304 function not defined in an executable is set to that function's
3305 plt entry in the executable, then the address of the function in
3306 a shared library must also be the plt entry in the executable. */
3307 return local_protected;
3308 }
3309
3310 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3311 aligned. Returns the first TLS output section. */
3312
3313 struct bfd_section *
3314 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3315 {
3316 struct bfd_section *sec, *tls;
3317 unsigned int align = 0;
3318
3319 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3320 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3321 break;
3322 tls = sec;
3323
3324 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3325 if (sec->alignment_power > align)
3326 align = sec->alignment_power;
3327
3328 elf_hash_table (info)->tls_sec = tls;
3329
3330 /* Ensure the alignment of the first section is the largest alignment,
3331 so that the tls segment starts aligned. */
3332 if (tls != NULL)
3333 tls->alignment_power = align;
3334
3335 return tls;
3336 }
3337
3338 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3339 static bfd_boolean
3340 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3341 Elf_Internal_Sym *sym)
3342 {
3343 const struct elf_backend_data *bed;
3344
3345 /* Local symbols do not count, but target specific ones might. */
3346 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3347 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3348 return FALSE;
3349
3350 bed = get_elf_backend_data (abfd);
3351 /* Function symbols do not count. */
3352 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3353 return FALSE;
3354
3355 /* If the section is undefined, then so is the symbol. */
3356 if (sym->st_shndx == SHN_UNDEF)
3357 return FALSE;
3358
3359 /* If the symbol is defined in the common section, then
3360 it is a common definition and so does not count. */
3361 if (bed->common_definition (sym))
3362 return FALSE;
3363
3364 /* If the symbol is in a target specific section then we
3365 must rely upon the backend to tell us what it is. */
3366 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3367 /* FIXME - this function is not coded yet:
3368
3369 return _bfd_is_global_symbol_definition (abfd, sym);
3370
3371 Instead for now assume that the definition is not global,
3372 Even if this is wrong, at least the linker will behave
3373 in the same way that it used to do. */
3374 return FALSE;
3375
3376 return TRUE;
3377 }
3378
3379 /* Search the symbol table of the archive element of the archive ABFD
3380 whose archive map contains a mention of SYMDEF, and determine if
3381 the symbol is defined in this element. */
3382 static bfd_boolean
3383 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3384 {
3385 Elf_Internal_Shdr * hdr;
3386 size_t symcount;
3387 size_t extsymcount;
3388 size_t extsymoff;
3389 Elf_Internal_Sym *isymbuf;
3390 Elf_Internal_Sym *isym;
3391 Elf_Internal_Sym *isymend;
3392 bfd_boolean result;
3393
3394 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3395 if (abfd == NULL)
3396 return FALSE;
3397
3398 if (! bfd_check_format (abfd, bfd_object))
3399 return FALSE;
3400
3401 /* Select the appropriate symbol table. If we don't know if the
3402 object file is an IR object, give linker LTO plugin a chance to
3403 get the correct symbol table. */
3404 if (abfd->plugin_format == bfd_plugin_yes
3405 #if BFD_SUPPORTS_PLUGINS
3406 || (abfd->plugin_format == bfd_plugin_unknown
3407 && bfd_link_plugin_object_p (abfd))
3408 #endif
3409 )
3410 {
3411 /* Use the IR symbol table if the object has been claimed by
3412 plugin. */
3413 abfd = abfd->plugin_dummy_bfd;
3414 hdr = &elf_tdata (abfd)->symtab_hdr;
3415 }
3416 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3417 hdr = &elf_tdata (abfd)->symtab_hdr;
3418 else
3419 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3420
3421 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3422
3423 /* The sh_info field of the symtab header tells us where the
3424 external symbols start. We don't care about the local symbols. */
3425 if (elf_bad_symtab (abfd))
3426 {
3427 extsymcount = symcount;
3428 extsymoff = 0;
3429 }
3430 else
3431 {
3432 extsymcount = symcount - hdr->sh_info;
3433 extsymoff = hdr->sh_info;
3434 }
3435
3436 if (extsymcount == 0)
3437 return FALSE;
3438
3439 /* Read in the symbol table. */
3440 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3441 NULL, NULL, NULL);
3442 if (isymbuf == NULL)
3443 return FALSE;
3444
3445 /* Scan the symbol table looking for SYMDEF. */
3446 result = FALSE;
3447 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3448 {
3449 const char *name;
3450
3451 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3452 isym->st_name);
3453 if (name == NULL)
3454 break;
3455
3456 if (strcmp (name, symdef->name) == 0)
3457 {
3458 result = is_global_data_symbol_definition (abfd, isym);
3459 break;
3460 }
3461 }
3462
3463 free (isymbuf);
3464
3465 return result;
3466 }
3467 \f
3468 /* Add an entry to the .dynamic table. */
3469
3470 bfd_boolean
3471 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3472 bfd_vma tag,
3473 bfd_vma val)
3474 {
3475 struct elf_link_hash_table *hash_table;
3476 const struct elf_backend_data *bed;
3477 asection *s;
3478 bfd_size_type newsize;
3479 bfd_byte *newcontents;
3480 Elf_Internal_Dyn dyn;
3481
3482 hash_table = elf_hash_table (info);
3483 if (! is_elf_hash_table (hash_table))
3484 return FALSE;
3485
3486 if (tag == DT_RELA || tag == DT_REL)
3487 hash_table->dynamic_relocs = TRUE;
3488
3489 bed = get_elf_backend_data (hash_table->dynobj);
3490 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3491 BFD_ASSERT (s != NULL);
3492
3493 newsize = s->size + bed->s->sizeof_dyn;
3494 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3495 if (newcontents == NULL)
3496 return FALSE;
3497
3498 dyn.d_tag = tag;
3499 dyn.d_un.d_val = val;
3500 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3501
3502 s->size = newsize;
3503 s->contents = newcontents;
3504
3505 return TRUE;
3506 }
3507
3508 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3509 otherwise just check whether one already exists. Returns -1 on error,
3510 1 if a DT_NEEDED tag already exists, and 0 on success. */
3511
3512 static int
3513 elf_add_dt_needed_tag (bfd *abfd,
3514 struct bfd_link_info *info,
3515 const char *soname,
3516 bfd_boolean do_it)
3517 {
3518 struct elf_link_hash_table *hash_table;
3519 size_t strindex;
3520
3521 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3522 return -1;
3523
3524 hash_table = elf_hash_table (info);
3525 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3526 if (strindex == (size_t) -1)
3527 return -1;
3528
3529 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3530 {
3531 asection *sdyn;
3532 const struct elf_backend_data *bed;
3533 bfd_byte *extdyn;
3534
3535 bed = get_elf_backend_data (hash_table->dynobj);
3536 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3537 if (sdyn != NULL)
3538 for (extdyn = sdyn->contents;
3539 extdyn < sdyn->contents + sdyn->size;
3540 extdyn += bed->s->sizeof_dyn)
3541 {
3542 Elf_Internal_Dyn dyn;
3543
3544 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3545 if (dyn.d_tag == DT_NEEDED
3546 && dyn.d_un.d_val == strindex)
3547 {
3548 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3549 return 1;
3550 }
3551 }
3552 }
3553
3554 if (do_it)
3555 {
3556 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3557 return -1;
3558
3559 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3560 return -1;
3561 }
3562 else
3563 /* We were just checking for existence of the tag. */
3564 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3565
3566 return 0;
3567 }
3568
3569 /* Return true if SONAME is on the needed list between NEEDED and STOP
3570 (or the end of list if STOP is NULL), and needed by a library that
3571 will be loaded. */
3572
3573 static bfd_boolean
3574 on_needed_list (const char *soname,
3575 struct bfd_link_needed_list *needed,
3576 struct bfd_link_needed_list *stop)
3577 {
3578 struct bfd_link_needed_list *look;
3579 for (look = needed; look != stop; look = look->next)
3580 if (strcmp (soname, look->name) == 0
3581 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3582 /* If needed by a library that itself is not directly
3583 needed, recursively check whether that library is
3584 indirectly needed. Since we add DT_NEEDED entries to
3585 the end of the list, library dependencies appear after
3586 the library. Therefore search prior to the current
3587 LOOK, preventing possible infinite recursion. */
3588 || on_needed_list (elf_dt_name (look->by), needed, look)))
3589 return TRUE;
3590
3591 return FALSE;
3592 }
3593
3594 /* Sort symbol by value, section, and size. */
3595 static int
3596 elf_sort_symbol (const void *arg1, const void *arg2)
3597 {
3598 const struct elf_link_hash_entry *h1;
3599 const struct elf_link_hash_entry *h2;
3600 bfd_signed_vma vdiff;
3601
3602 h1 = *(const struct elf_link_hash_entry **) arg1;
3603 h2 = *(const struct elf_link_hash_entry **) arg2;
3604 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3605 if (vdiff != 0)
3606 return vdiff > 0 ? 1 : -1;
3607 else
3608 {
3609 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3610 if (sdiff != 0)
3611 return sdiff > 0 ? 1 : -1;
3612 }
3613 vdiff = h1->size - h2->size;
3614 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3615 }
3616
3617 /* This function is used to adjust offsets into .dynstr for
3618 dynamic symbols. This is called via elf_link_hash_traverse. */
3619
3620 static bfd_boolean
3621 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3622 {
3623 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3624
3625 if (h->dynindx != -1)
3626 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3627 return TRUE;
3628 }
3629
3630 /* Assign string offsets in .dynstr, update all structures referencing
3631 them. */
3632
3633 static bfd_boolean
3634 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3635 {
3636 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3637 struct elf_link_local_dynamic_entry *entry;
3638 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3639 bfd *dynobj = hash_table->dynobj;
3640 asection *sdyn;
3641 bfd_size_type size;
3642 const struct elf_backend_data *bed;
3643 bfd_byte *extdyn;
3644
3645 _bfd_elf_strtab_finalize (dynstr);
3646 size = _bfd_elf_strtab_size (dynstr);
3647
3648 bed = get_elf_backend_data (dynobj);
3649 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3650 BFD_ASSERT (sdyn != NULL);
3651
3652 /* Update all .dynamic entries referencing .dynstr strings. */
3653 for (extdyn = sdyn->contents;
3654 extdyn < sdyn->contents + sdyn->size;
3655 extdyn += bed->s->sizeof_dyn)
3656 {
3657 Elf_Internal_Dyn dyn;
3658
3659 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3660 switch (dyn.d_tag)
3661 {
3662 case DT_STRSZ:
3663 dyn.d_un.d_val = size;
3664 break;
3665 case DT_NEEDED:
3666 case DT_SONAME:
3667 case DT_RPATH:
3668 case DT_RUNPATH:
3669 case DT_FILTER:
3670 case DT_AUXILIARY:
3671 case DT_AUDIT:
3672 case DT_DEPAUDIT:
3673 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3674 break;
3675 default:
3676 continue;
3677 }
3678 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3679 }
3680
3681 /* Now update local dynamic symbols. */
3682 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3683 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3684 entry->isym.st_name);
3685
3686 /* And the rest of dynamic symbols. */
3687 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3688
3689 /* Adjust version definitions. */
3690 if (elf_tdata (output_bfd)->cverdefs)
3691 {
3692 asection *s;
3693 bfd_byte *p;
3694 size_t i;
3695 Elf_Internal_Verdef def;
3696 Elf_Internal_Verdaux defaux;
3697
3698 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3699 p = s->contents;
3700 do
3701 {
3702 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3703 &def);
3704 p += sizeof (Elf_External_Verdef);
3705 if (def.vd_aux != sizeof (Elf_External_Verdef))
3706 continue;
3707 for (i = 0; i < def.vd_cnt; ++i)
3708 {
3709 _bfd_elf_swap_verdaux_in (output_bfd,
3710 (Elf_External_Verdaux *) p, &defaux);
3711 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3712 defaux.vda_name);
3713 _bfd_elf_swap_verdaux_out (output_bfd,
3714 &defaux, (Elf_External_Verdaux *) p);
3715 p += sizeof (Elf_External_Verdaux);
3716 }
3717 }
3718 while (def.vd_next);
3719 }
3720
3721 /* Adjust version references. */
3722 if (elf_tdata (output_bfd)->verref)
3723 {
3724 asection *s;
3725 bfd_byte *p;
3726 size_t i;
3727 Elf_Internal_Verneed need;
3728 Elf_Internal_Vernaux needaux;
3729
3730 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3731 p = s->contents;
3732 do
3733 {
3734 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3735 &need);
3736 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3737 _bfd_elf_swap_verneed_out (output_bfd, &need,
3738 (Elf_External_Verneed *) p);
3739 p += sizeof (Elf_External_Verneed);
3740 for (i = 0; i < need.vn_cnt; ++i)
3741 {
3742 _bfd_elf_swap_vernaux_in (output_bfd,
3743 (Elf_External_Vernaux *) p, &needaux);
3744 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3745 needaux.vna_name);
3746 _bfd_elf_swap_vernaux_out (output_bfd,
3747 &needaux,
3748 (Elf_External_Vernaux *) p);
3749 p += sizeof (Elf_External_Vernaux);
3750 }
3751 }
3752 while (need.vn_next);
3753 }
3754
3755 return TRUE;
3756 }
3757 \f
3758 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3759 The default is to only match when the INPUT and OUTPUT are exactly
3760 the same target. */
3761
3762 bfd_boolean
3763 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3764 const bfd_target *output)
3765 {
3766 return input == output;
3767 }
3768
3769 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3770 This version is used when different targets for the same architecture
3771 are virtually identical. */
3772
3773 bfd_boolean
3774 _bfd_elf_relocs_compatible (const bfd_target *input,
3775 const bfd_target *output)
3776 {
3777 const struct elf_backend_data *obed, *ibed;
3778
3779 if (input == output)
3780 return TRUE;
3781
3782 ibed = xvec_get_elf_backend_data (input);
3783 obed = xvec_get_elf_backend_data (output);
3784
3785 if (ibed->arch != obed->arch)
3786 return FALSE;
3787
3788 /* If both backends are using this function, deem them compatible. */
3789 return ibed->relocs_compatible == obed->relocs_compatible;
3790 }
3791
3792 /* Make a special call to the linker "notice" function to tell it that
3793 we are about to handle an as-needed lib, or have finished
3794 processing the lib. */
3795
3796 bfd_boolean
3797 _bfd_elf_notice_as_needed (bfd *ibfd,
3798 struct bfd_link_info *info,
3799 enum notice_asneeded_action act)
3800 {
3801 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3802 }
3803
3804 /* Check relocations an ELF object file. */
3805
3806 bfd_boolean
3807 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3808 {
3809 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3810 struct elf_link_hash_table *htab = elf_hash_table (info);
3811
3812 /* If this object is the same format as the output object, and it is
3813 not a shared library, then let the backend look through the
3814 relocs.
3815
3816 This is required to build global offset table entries and to
3817 arrange for dynamic relocs. It is not required for the
3818 particular common case of linking non PIC code, even when linking
3819 against shared libraries, but unfortunately there is no way of
3820 knowing whether an object file has been compiled PIC or not.
3821 Looking through the relocs is not particularly time consuming.
3822 The problem is that we must either (1) keep the relocs in memory,
3823 which causes the linker to require additional runtime memory or
3824 (2) read the relocs twice from the input file, which wastes time.
3825 This would be a good case for using mmap.
3826
3827 I have no idea how to handle linking PIC code into a file of a
3828 different format. It probably can't be done. */
3829 if ((abfd->flags & DYNAMIC) == 0
3830 && is_elf_hash_table (htab)
3831 && bed->check_relocs != NULL
3832 && elf_object_id (abfd) == elf_hash_table_id (htab)
3833 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3834 {
3835 asection *o;
3836
3837 for (o = abfd->sections; o != NULL; o = o->next)
3838 {
3839 Elf_Internal_Rela *internal_relocs;
3840 bfd_boolean ok;
3841
3842 /* Don't check relocations in excluded sections. */
3843 if ((o->flags & SEC_RELOC) == 0
3844 || (o->flags & SEC_EXCLUDE) != 0
3845 || o->reloc_count == 0
3846 || ((info->strip == strip_all || info->strip == strip_debugger)
3847 && (o->flags & SEC_DEBUGGING) != 0)
3848 || bfd_is_abs_section (o->output_section))
3849 continue;
3850
3851 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3852 info->keep_memory);
3853 if (internal_relocs == NULL)
3854 return FALSE;
3855
3856 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3857
3858 if (elf_section_data (o)->relocs != internal_relocs)
3859 free (internal_relocs);
3860
3861 if (! ok)
3862 return FALSE;
3863 }
3864 }
3865
3866 return TRUE;
3867 }
3868
3869 /* Add symbols from an ELF object file to the linker hash table. */
3870
3871 static bfd_boolean
3872 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3873 {
3874 Elf_Internal_Ehdr *ehdr;
3875 Elf_Internal_Shdr *hdr;
3876 size_t symcount;
3877 size_t extsymcount;
3878 size_t extsymoff;
3879 struct elf_link_hash_entry **sym_hash;
3880 bfd_boolean dynamic;
3881 Elf_External_Versym *extversym = NULL;
3882 Elf_External_Versym *extversym_end = NULL;
3883 Elf_External_Versym *ever;
3884 struct elf_link_hash_entry *weaks;
3885 struct elf_link_hash_entry **nondeflt_vers = NULL;
3886 size_t nondeflt_vers_cnt = 0;
3887 Elf_Internal_Sym *isymbuf = NULL;
3888 Elf_Internal_Sym *isym;
3889 Elf_Internal_Sym *isymend;
3890 const struct elf_backend_data *bed;
3891 bfd_boolean add_needed;
3892 struct elf_link_hash_table *htab;
3893 bfd_size_type amt;
3894 void *alloc_mark = NULL;
3895 struct bfd_hash_entry **old_table = NULL;
3896 unsigned int old_size = 0;
3897 unsigned int old_count = 0;
3898 void *old_tab = NULL;
3899 void *old_ent;
3900 struct bfd_link_hash_entry *old_undefs = NULL;
3901 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3902 void *old_strtab = NULL;
3903 size_t tabsize = 0;
3904 asection *s;
3905 bfd_boolean just_syms;
3906
3907 htab = elf_hash_table (info);
3908 bed = get_elf_backend_data (abfd);
3909
3910 if ((abfd->flags & DYNAMIC) == 0)
3911 dynamic = FALSE;
3912 else
3913 {
3914 dynamic = TRUE;
3915
3916 /* You can't use -r against a dynamic object. Also, there's no
3917 hope of using a dynamic object which does not exactly match
3918 the format of the output file. */
3919 if (bfd_link_relocatable (info)
3920 || !is_elf_hash_table (htab)
3921 || info->output_bfd->xvec != abfd->xvec)
3922 {
3923 if (bfd_link_relocatable (info))
3924 bfd_set_error (bfd_error_invalid_operation);
3925 else
3926 bfd_set_error (bfd_error_wrong_format);
3927 goto error_return;
3928 }
3929 }
3930
3931 ehdr = elf_elfheader (abfd);
3932 if (info->warn_alternate_em
3933 && bed->elf_machine_code != ehdr->e_machine
3934 && ((bed->elf_machine_alt1 != 0
3935 && ehdr->e_machine == bed->elf_machine_alt1)
3936 || (bed->elf_machine_alt2 != 0
3937 && ehdr->e_machine == bed->elf_machine_alt2)))
3938 _bfd_error_handler
3939 /* xgettext:c-format */
3940 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
3941 ehdr->e_machine, abfd, bed->elf_machine_code);
3942
3943 /* As a GNU extension, any input sections which are named
3944 .gnu.warning.SYMBOL are treated as warning symbols for the given
3945 symbol. This differs from .gnu.warning sections, which generate
3946 warnings when they are included in an output file. */
3947 /* PR 12761: Also generate this warning when building shared libraries. */
3948 for (s = abfd->sections; s != NULL; s = s->next)
3949 {
3950 const char *name;
3951
3952 name = bfd_get_section_name (abfd, s);
3953 if (CONST_STRNEQ (name, ".gnu.warning."))
3954 {
3955 char *msg;
3956 bfd_size_type sz;
3957
3958 name += sizeof ".gnu.warning." - 1;
3959
3960 /* If this is a shared object, then look up the symbol
3961 in the hash table. If it is there, and it is already
3962 been defined, then we will not be using the entry
3963 from this shared object, so we don't need to warn.
3964 FIXME: If we see the definition in a regular object
3965 later on, we will warn, but we shouldn't. The only
3966 fix is to keep track of what warnings we are supposed
3967 to emit, and then handle them all at the end of the
3968 link. */
3969 if (dynamic)
3970 {
3971 struct elf_link_hash_entry *h;
3972
3973 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3974
3975 /* FIXME: What about bfd_link_hash_common? */
3976 if (h != NULL
3977 && (h->root.type == bfd_link_hash_defined
3978 || h->root.type == bfd_link_hash_defweak))
3979 continue;
3980 }
3981
3982 sz = s->size;
3983 msg = (char *) bfd_alloc (abfd, sz + 1);
3984 if (msg == NULL)
3985 goto error_return;
3986
3987 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3988 goto error_return;
3989
3990 msg[sz] = '\0';
3991
3992 if (! (_bfd_generic_link_add_one_symbol
3993 (info, abfd, name, BSF_WARNING, s, 0, msg,
3994 FALSE, bed->collect, NULL)))
3995 goto error_return;
3996
3997 if (bfd_link_executable (info))
3998 {
3999 /* Clobber the section size so that the warning does
4000 not get copied into the output file. */
4001 s->size = 0;
4002
4003 /* Also set SEC_EXCLUDE, so that symbols defined in
4004 the warning section don't get copied to the output. */
4005 s->flags |= SEC_EXCLUDE;
4006 }
4007 }
4008 }
4009
4010 just_syms = ((s = abfd->sections) != NULL
4011 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4012
4013 add_needed = TRUE;
4014 if (! dynamic)
4015 {
4016 /* If we are creating a shared library, create all the dynamic
4017 sections immediately. We need to attach them to something,
4018 so we attach them to this BFD, provided it is the right
4019 format and is not from ld --just-symbols. Always create the
4020 dynamic sections for -E/--dynamic-list. FIXME: If there
4021 are no input BFD's of the same format as the output, we can't
4022 make a shared library. */
4023 if (!just_syms
4024 && (bfd_link_pic (info)
4025 || (!bfd_link_relocatable (info)
4026 && info->nointerp
4027 && (info->export_dynamic || info->dynamic)))
4028 && is_elf_hash_table (htab)
4029 && info->output_bfd->xvec == abfd->xvec
4030 && !htab->dynamic_sections_created)
4031 {
4032 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4033 goto error_return;
4034 }
4035 }
4036 else if (!is_elf_hash_table (htab))
4037 goto error_return;
4038 else
4039 {
4040 const char *soname = NULL;
4041 char *audit = NULL;
4042 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4043 const Elf_Internal_Phdr *phdr;
4044 int ret;
4045
4046 /* ld --just-symbols and dynamic objects don't mix very well.
4047 ld shouldn't allow it. */
4048 if (just_syms)
4049 abort ();
4050
4051 /* If this dynamic lib was specified on the command line with
4052 --as-needed in effect, then we don't want to add a DT_NEEDED
4053 tag unless the lib is actually used. Similary for libs brought
4054 in by another lib's DT_NEEDED. When --no-add-needed is used
4055 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4056 any dynamic library in DT_NEEDED tags in the dynamic lib at
4057 all. */
4058 add_needed = (elf_dyn_lib_class (abfd)
4059 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4060 | DYN_NO_NEEDED)) == 0;
4061
4062 s = bfd_get_section_by_name (abfd, ".dynamic");
4063 if (s != NULL)
4064 {
4065 bfd_byte *dynbuf;
4066 bfd_byte *extdyn;
4067 unsigned int elfsec;
4068 unsigned long shlink;
4069
4070 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4071 {
4072 error_free_dyn:
4073 free (dynbuf);
4074 goto error_return;
4075 }
4076
4077 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4078 if (elfsec == SHN_BAD)
4079 goto error_free_dyn;
4080 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4081
4082 for (extdyn = dynbuf;
4083 extdyn <= dynbuf + s->size - bed->s->sizeof_dyn;
4084 extdyn += bed->s->sizeof_dyn)
4085 {
4086 Elf_Internal_Dyn dyn;
4087
4088 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4089 if (dyn.d_tag == DT_SONAME)
4090 {
4091 unsigned int tagv = dyn.d_un.d_val;
4092 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4093 if (soname == NULL)
4094 goto error_free_dyn;
4095 }
4096 if (dyn.d_tag == DT_NEEDED)
4097 {
4098 struct bfd_link_needed_list *n, **pn;
4099 char *fnm, *anm;
4100 unsigned int tagv = dyn.d_un.d_val;
4101
4102 amt = sizeof (struct bfd_link_needed_list);
4103 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4104 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4105 if (n == NULL || fnm == NULL)
4106 goto error_free_dyn;
4107 amt = strlen (fnm) + 1;
4108 anm = (char *) bfd_alloc (abfd, amt);
4109 if (anm == NULL)
4110 goto error_free_dyn;
4111 memcpy (anm, fnm, amt);
4112 n->name = anm;
4113 n->by = abfd;
4114 n->next = NULL;
4115 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4116 ;
4117 *pn = n;
4118 }
4119 if (dyn.d_tag == DT_RUNPATH)
4120 {
4121 struct bfd_link_needed_list *n, **pn;
4122 char *fnm, *anm;
4123 unsigned int tagv = dyn.d_un.d_val;
4124
4125 amt = sizeof (struct bfd_link_needed_list);
4126 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4127 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4128 if (n == NULL || fnm == NULL)
4129 goto error_free_dyn;
4130 amt = strlen (fnm) + 1;
4131 anm = (char *) bfd_alloc (abfd, amt);
4132 if (anm == NULL)
4133 goto error_free_dyn;
4134 memcpy (anm, fnm, amt);
4135 n->name = anm;
4136 n->by = abfd;
4137 n->next = NULL;
4138 for (pn = & runpath;
4139 *pn != NULL;
4140 pn = &(*pn)->next)
4141 ;
4142 *pn = n;
4143 }
4144 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4145 if (!runpath && dyn.d_tag == DT_RPATH)
4146 {
4147 struct bfd_link_needed_list *n, **pn;
4148 char *fnm, *anm;
4149 unsigned int tagv = dyn.d_un.d_val;
4150
4151 amt = sizeof (struct bfd_link_needed_list);
4152 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4153 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4154 if (n == NULL || fnm == NULL)
4155 goto error_free_dyn;
4156 amt = strlen (fnm) + 1;
4157 anm = (char *) bfd_alloc (abfd, amt);
4158 if (anm == NULL)
4159 goto error_free_dyn;
4160 memcpy (anm, fnm, amt);
4161 n->name = anm;
4162 n->by = abfd;
4163 n->next = NULL;
4164 for (pn = & rpath;
4165 *pn != NULL;
4166 pn = &(*pn)->next)
4167 ;
4168 *pn = n;
4169 }
4170 if (dyn.d_tag == DT_AUDIT)
4171 {
4172 unsigned int tagv = dyn.d_un.d_val;
4173 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4174 }
4175 }
4176
4177 free (dynbuf);
4178 }
4179
4180 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4181 frees all more recently bfd_alloc'd blocks as well. */
4182 if (runpath)
4183 rpath = runpath;
4184
4185 if (rpath)
4186 {
4187 struct bfd_link_needed_list **pn;
4188 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4189 ;
4190 *pn = rpath;
4191 }
4192
4193 /* If we have a PT_GNU_RELRO program header, mark as read-only
4194 all sections contained fully therein. This makes relro
4195 shared library sections appear as they will at run-time. */
4196 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4197 while (phdr-- > elf_tdata (abfd)->phdr)
4198 if (phdr->p_type == PT_GNU_RELRO)
4199 {
4200 for (s = abfd->sections; s != NULL; s = s->next)
4201 if ((s->flags & SEC_ALLOC) != 0
4202 && s->vma >= phdr->p_vaddr
4203 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4204 s->flags |= SEC_READONLY;
4205 break;
4206 }
4207
4208 /* We do not want to include any of the sections in a dynamic
4209 object in the output file. We hack by simply clobbering the
4210 list of sections in the BFD. This could be handled more
4211 cleanly by, say, a new section flag; the existing
4212 SEC_NEVER_LOAD flag is not the one we want, because that one
4213 still implies that the section takes up space in the output
4214 file. */
4215 bfd_section_list_clear (abfd);
4216
4217 /* Find the name to use in a DT_NEEDED entry that refers to this
4218 object. If the object has a DT_SONAME entry, we use it.
4219 Otherwise, if the generic linker stuck something in
4220 elf_dt_name, we use that. Otherwise, we just use the file
4221 name. */
4222 if (soname == NULL || *soname == '\0')
4223 {
4224 soname = elf_dt_name (abfd);
4225 if (soname == NULL || *soname == '\0')
4226 soname = bfd_get_filename (abfd);
4227 }
4228
4229 /* Save the SONAME because sometimes the linker emulation code
4230 will need to know it. */
4231 elf_dt_name (abfd) = soname;
4232
4233 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4234 if (ret < 0)
4235 goto error_return;
4236
4237 /* If we have already included this dynamic object in the
4238 link, just ignore it. There is no reason to include a
4239 particular dynamic object more than once. */
4240 if (ret > 0)
4241 return TRUE;
4242
4243 /* Save the DT_AUDIT entry for the linker emulation code. */
4244 elf_dt_audit (abfd) = audit;
4245 }
4246
4247 /* If this is a dynamic object, we always link against the .dynsym
4248 symbol table, not the .symtab symbol table. The dynamic linker
4249 will only see the .dynsym symbol table, so there is no reason to
4250 look at .symtab for a dynamic object. */
4251
4252 if (! dynamic || elf_dynsymtab (abfd) == 0)
4253 hdr = &elf_tdata (abfd)->symtab_hdr;
4254 else
4255 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4256
4257 symcount = hdr->sh_size / bed->s->sizeof_sym;
4258
4259 /* The sh_info field of the symtab header tells us where the
4260 external symbols start. We don't care about the local symbols at
4261 this point. */
4262 if (elf_bad_symtab (abfd))
4263 {
4264 extsymcount = symcount;
4265 extsymoff = 0;
4266 }
4267 else
4268 {
4269 extsymcount = symcount - hdr->sh_info;
4270 extsymoff = hdr->sh_info;
4271 }
4272
4273 sym_hash = elf_sym_hashes (abfd);
4274 if (extsymcount != 0)
4275 {
4276 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4277 NULL, NULL, NULL);
4278 if (isymbuf == NULL)
4279 goto error_return;
4280
4281 if (sym_hash == NULL)
4282 {
4283 /* We store a pointer to the hash table entry for each
4284 external symbol. */
4285 amt = extsymcount;
4286 amt *= sizeof (struct elf_link_hash_entry *);
4287 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4288 if (sym_hash == NULL)
4289 goto error_free_sym;
4290 elf_sym_hashes (abfd) = sym_hash;
4291 }
4292 }
4293
4294 if (dynamic)
4295 {
4296 /* Read in any version definitions. */
4297 if (!_bfd_elf_slurp_version_tables (abfd,
4298 info->default_imported_symver))
4299 goto error_free_sym;
4300
4301 /* Read in the symbol versions, but don't bother to convert them
4302 to internal format. */
4303 if (elf_dynversym (abfd) != 0)
4304 {
4305 Elf_Internal_Shdr *versymhdr;
4306
4307 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4308 amt = versymhdr->sh_size;
4309 extversym = (Elf_External_Versym *) bfd_malloc (amt);
4310 if (extversym == NULL)
4311 goto error_free_sym;
4312 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4313 || bfd_bread (extversym, amt, abfd) != amt)
4314 goto error_free_vers;
4315 extversym_end = extversym + (amt / sizeof (* extversym));
4316 }
4317 }
4318
4319 /* If we are loading an as-needed shared lib, save the symbol table
4320 state before we start adding symbols. If the lib turns out
4321 to be unneeded, restore the state. */
4322 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4323 {
4324 unsigned int i;
4325 size_t entsize;
4326
4327 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4328 {
4329 struct bfd_hash_entry *p;
4330 struct elf_link_hash_entry *h;
4331
4332 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4333 {
4334 h = (struct elf_link_hash_entry *) p;
4335 entsize += htab->root.table.entsize;
4336 if (h->root.type == bfd_link_hash_warning)
4337 entsize += htab->root.table.entsize;
4338 }
4339 }
4340
4341 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4342 old_tab = bfd_malloc (tabsize + entsize);
4343 if (old_tab == NULL)
4344 goto error_free_vers;
4345
4346 /* Remember the current objalloc pointer, so that all mem for
4347 symbols added can later be reclaimed. */
4348 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4349 if (alloc_mark == NULL)
4350 goto error_free_vers;
4351
4352 /* Make a special call to the linker "notice" function to
4353 tell it that we are about to handle an as-needed lib. */
4354 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4355 goto error_free_vers;
4356
4357 /* Clone the symbol table. Remember some pointers into the
4358 symbol table, and dynamic symbol count. */
4359 old_ent = (char *) old_tab + tabsize;
4360 memcpy (old_tab, htab->root.table.table, tabsize);
4361 old_undefs = htab->root.undefs;
4362 old_undefs_tail = htab->root.undefs_tail;
4363 old_table = htab->root.table.table;
4364 old_size = htab->root.table.size;
4365 old_count = htab->root.table.count;
4366 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4367 if (old_strtab == NULL)
4368 goto error_free_vers;
4369
4370 for (i = 0; i < htab->root.table.size; i++)
4371 {
4372 struct bfd_hash_entry *p;
4373 struct elf_link_hash_entry *h;
4374
4375 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4376 {
4377 memcpy (old_ent, p, htab->root.table.entsize);
4378 old_ent = (char *) old_ent + htab->root.table.entsize;
4379 h = (struct elf_link_hash_entry *) p;
4380 if (h->root.type == bfd_link_hash_warning)
4381 {
4382 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4383 old_ent = (char *) old_ent + htab->root.table.entsize;
4384 }
4385 }
4386 }
4387 }
4388
4389 weaks = NULL;
4390 if (extversym == NULL)
4391 ever = NULL;
4392 else if (extversym + extsymoff < extversym_end)
4393 ever = extversym + extsymoff;
4394 else
4395 {
4396 /* xgettext:c-format */
4397 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4398 abfd, (long) extsymoff,
4399 (long) (extversym_end - extversym) / sizeof (* extversym));
4400 bfd_set_error (bfd_error_bad_value);
4401 goto error_free_vers;
4402 }
4403
4404 if (!bfd_link_relocatable (info)
4405 && abfd->lto_slim_object)
4406 {
4407 _bfd_error_handler
4408 (_("%pB: plugin needed to handle lto object"), abfd);
4409 }
4410
4411 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4412 isym < isymend;
4413 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4414 {
4415 int bind;
4416 bfd_vma value;
4417 asection *sec, *new_sec;
4418 flagword flags;
4419 const char *name;
4420 struct elf_link_hash_entry *h;
4421 struct elf_link_hash_entry *hi;
4422 bfd_boolean definition;
4423 bfd_boolean size_change_ok;
4424 bfd_boolean type_change_ok;
4425 bfd_boolean new_weak;
4426 bfd_boolean old_weak;
4427 bfd_boolean override;
4428 bfd_boolean common;
4429 bfd_boolean discarded;
4430 unsigned int old_alignment;
4431 unsigned int shindex;
4432 bfd *old_bfd;
4433 bfd_boolean matched;
4434
4435 override = FALSE;
4436
4437 flags = BSF_NO_FLAGS;
4438 sec = NULL;
4439 value = isym->st_value;
4440 common = bed->common_definition (isym);
4441 if (common && info->inhibit_common_definition)
4442 {
4443 /* Treat common symbol as undefined for --no-define-common. */
4444 isym->st_shndx = SHN_UNDEF;
4445 common = FALSE;
4446 }
4447 discarded = FALSE;
4448
4449 bind = ELF_ST_BIND (isym->st_info);
4450 switch (bind)
4451 {
4452 case STB_LOCAL:
4453 /* This should be impossible, since ELF requires that all
4454 global symbols follow all local symbols, and that sh_info
4455 point to the first global symbol. Unfortunately, Irix 5
4456 screws this up. */
4457 if (elf_bad_symtab (abfd))
4458 continue;
4459
4460 /* If we aren't prepared to handle locals within the globals
4461 then we'll likely segfault on a NULL symbol hash if the
4462 symbol is ever referenced in relocations. */
4463 shindex = elf_elfheader (abfd)->e_shstrndx;
4464 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4465 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4466 " (>= sh_info of %lu)"),
4467 abfd, name, (long) (isym - isymbuf + extsymoff),
4468 (long) extsymoff);
4469
4470 /* Dynamic object relocations are not processed by ld, so
4471 ld won't run into the problem mentioned above. */
4472 if (dynamic)
4473 continue;
4474 bfd_set_error (bfd_error_bad_value);
4475 goto error_free_vers;
4476
4477 case STB_GLOBAL:
4478 if (isym->st_shndx != SHN_UNDEF && !common)
4479 flags = BSF_GLOBAL;
4480 break;
4481
4482 case STB_WEAK:
4483 flags = BSF_WEAK;
4484 break;
4485
4486 case STB_GNU_UNIQUE:
4487 flags = BSF_GNU_UNIQUE;
4488 break;
4489
4490 default:
4491 /* Leave it up to the processor backend. */
4492 break;
4493 }
4494
4495 if (isym->st_shndx == SHN_UNDEF)
4496 sec = bfd_und_section_ptr;
4497 else if (isym->st_shndx == SHN_ABS)
4498 sec = bfd_abs_section_ptr;
4499 else if (isym->st_shndx == SHN_COMMON)
4500 {
4501 sec = bfd_com_section_ptr;
4502 /* What ELF calls the size we call the value. What ELF
4503 calls the value we call the alignment. */
4504 value = isym->st_size;
4505 }
4506 else
4507 {
4508 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4509 if (sec == NULL)
4510 sec = bfd_abs_section_ptr;
4511 else if (discarded_section (sec))
4512 {
4513 /* Symbols from discarded section are undefined. We keep
4514 its visibility. */
4515 sec = bfd_und_section_ptr;
4516 discarded = TRUE;
4517 isym->st_shndx = SHN_UNDEF;
4518 }
4519 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4520 value -= sec->vma;
4521 }
4522
4523 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4524 isym->st_name);
4525 if (name == NULL)
4526 goto error_free_vers;
4527
4528 if (isym->st_shndx == SHN_COMMON
4529 && (abfd->flags & BFD_PLUGIN) != 0)
4530 {
4531 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4532
4533 if (xc == NULL)
4534 {
4535 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4536 | SEC_EXCLUDE);
4537 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4538 if (xc == NULL)
4539 goto error_free_vers;
4540 }
4541 sec = xc;
4542 }
4543 else if (isym->st_shndx == SHN_COMMON
4544 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4545 && !bfd_link_relocatable (info))
4546 {
4547 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4548
4549 if (tcomm == NULL)
4550 {
4551 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4552 | SEC_LINKER_CREATED);
4553 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4554 if (tcomm == NULL)
4555 goto error_free_vers;
4556 }
4557 sec = tcomm;
4558 }
4559 else if (bed->elf_add_symbol_hook)
4560 {
4561 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4562 &sec, &value))
4563 goto error_free_vers;
4564
4565 /* The hook function sets the name to NULL if this symbol
4566 should be skipped for some reason. */
4567 if (name == NULL)
4568 continue;
4569 }
4570
4571 /* Sanity check that all possibilities were handled. */
4572 if (sec == NULL)
4573 abort ();
4574
4575 /* Silently discard TLS symbols from --just-syms. There's
4576 no way to combine a static TLS block with a new TLS block
4577 for this executable. */
4578 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4579 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4580 continue;
4581
4582 if (bfd_is_und_section (sec)
4583 || bfd_is_com_section (sec))
4584 definition = FALSE;
4585 else
4586 definition = TRUE;
4587
4588 size_change_ok = FALSE;
4589 type_change_ok = bed->type_change_ok;
4590 old_weak = FALSE;
4591 matched = FALSE;
4592 old_alignment = 0;
4593 old_bfd = NULL;
4594 new_sec = sec;
4595
4596 if (is_elf_hash_table (htab))
4597 {
4598 Elf_Internal_Versym iver;
4599 unsigned int vernum = 0;
4600 bfd_boolean skip;
4601
4602 if (ever == NULL)
4603 {
4604 if (info->default_imported_symver)
4605 /* Use the default symbol version created earlier. */
4606 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4607 else
4608 iver.vs_vers = 0;
4609 }
4610 else if (ever >= extversym_end)
4611 {
4612 /* xgettext:c-format */
4613 _bfd_error_handler (_("%pB: not enough version information"),
4614 abfd);
4615 bfd_set_error (bfd_error_bad_value);
4616 goto error_free_vers;
4617 }
4618 else
4619 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4620
4621 vernum = iver.vs_vers & VERSYM_VERSION;
4622
4623 /* If this is a hidden symbol, or if it is not version
4624 1, we append the version name to the symbol name.
4625 However, we do not modify a non-hidden absolute symbol
4626 if it is not a function, because it might be the version
4627 symbol itself. FIXME: What if it isn't? */
4628 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4629 || (vernum > 1
4630 && (!bfd_is_abs_section (sec)
4631 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4632 {
4633 const char *verstr;
4634 size_t namelen, verlen, newlen;
4635 char *newname, *p;
4636
4637 if (isym->st_shndx != SHN_UNDEF)
4638 {
4639 if (vernum > elf_tdata (abfd)->cverdefs)
4640 verstr = NULL;
4641 else if (vernum > 1)
4642 verstr =
4643 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4644 else
4645 verstr = "";
4646
4647 if (verstr == NULL)
4648 {
4649 _bfd_error_handler
4650 /* xgettext:c-format */
4651 (_("%pB: %s: invalid version %u (max %d)"),
4652 abfd, name, vernum,
4653 elf_tdata (abfd)->cverdefs);
4654 bfd_set_error (bfd_error_bad_value);
4655 goto error_free_vers;
4656 }
4657 }
4658 else
4659 {
4660 /* We cannot simply test for the number of
4661 entries in the VERNEED section since the
4662 numbers for the needed versions do not start
4663 at 0. */
4664 Elf_Internal_Verneed *t;
4665
4666 verstr = NULL;
4667 for (t = elf_tdata (abfd)->verref;
4668 t != NULL;
4669 t = t->vn_nextref)
4670 {
4671 Elf_Internal_Vernaux *a;
4672
4673 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4674 {
4675 if (a->vna_other == vernum)
4676 {
4677 verstr = a->vna_nodename;
4678 break;
4679 }
4680 }
4681 if (a != NULL)
4682 break;
4683 }
4684 if (verstr == NULL)
4685 {
4686 _bfd_error_handler
4687 /* xgettext:c-format */
4688 (_("%pB: %s: invalid needed version %d"),
4689 abfd, name, vernum);
4690 bfd_set_error (bfd_error_bad_value);
4691 goto error_free_vers;
4692 }
4693 }
4694
4695 namelen = strlen (name);
4696 verlen = strlen (verstr);
4697 newlen = namelen + verlen + 2;
4698 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4699 && isym->st_shndx != SHN_UNDEF)
4700 ++newlen;
4701
4702 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4703 if (newname == NULL)
4704 goto error_free_vers;
4705 memcpy (newname, name, namelen);
4706 p = newname + namelen;
4707 *p++ = ELF_VER_CHR;
4708 /* If this is a defined non-hidden version symbol,
4709 we add another @ to the name. This indicates the
4710 default version of the symbol. */
4711 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4712 && isym->st_shndx != SHN_UNDEF)
4713 *p++ = ELF_VER_CHR;
4714 memcpy (p, verstr, verlen + 1);
4715
4716 name = newname;
4717 }
4718
4719 /* If this symbol has default visibility and the user has
4720 requested we not re-export it, then mark it as hidden. */
4721 if (!bfd_is_und_section (sec)
4722 && !dynamic
4723 && abfd->no_export
4724 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4725 isym->st_other = (STV_HIDDEN
4726 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4727
4728 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4729 sym_hash, &old_bfd, &old_weak,
4730 &old_alignment, &skip, &override,
4731 &type_change_ok, &size_change_ok,
4732 &matched))
4733 goto error_free_vers;
4734
4735 if (skip)
4736 continue;
4737
4738 /* Override a definition only if the new symbol matches the
4739 existing one. */
4740 if (override && matched)
4741 definition = FALSE;
4742
4743 h = *sym_hash;
4744 while (h->root.type == bfd_link_hash_indirect
4745 || h->root.type == bfd_link_hash_warning)
4746 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4747
4748 if (elf_tdata (abfd)->verdef != NULL
4749 && vernum > 1
4750 && definition)
4751 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4752 }
4753
4754 if (! (_bfd_generic_link_add_one_symbol
4755 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4756 (struct bfd_link_hash_entry **) sym_hash)))
4757 goto error_free_vers;
4758
4759 h = *sym_hash;
4760 /* We need to make sure that indirect symbol dynamic flags are
4761 updated. */
4762 hi = h;
4763 while (h->root.type == bfd_link_hash_indirect
4764 || h->root.type == bfd_link_hash_warning)
4765 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4766
4767 /* Setting the index to -3 tells elf_link_output_extsym that
4768 this symbol is defined in a discarded section. */
4769 if (discarded)
4770 h->indx = -3;
4771
4772 *sym_hash = h;
4773
4774 new_weak = (flags & BSF_WEAK) != 0;
4775 if (dynamic
4776 && definition
4777 && new_weak
4778 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4779 && is_elf_hash_table (htab)
4780 && h->u.alias == NULL)
4781 {
4782 /* Keep a list of all weak defined non function symbols from
4783 a dynamic object, using the alias field. Later in this
4784 function we will set the alias field to the correct
4785 value. We only put non-function symbols from dynamic
4786 objects on this list, because that happens to be the only
4787 time we need to know the normal symbol corresponding to a
4788 weak symbol, and the information is time consuming to
4789 figure out. If the alias field is not already NULL,
4790 then this symbol was already defined by some previous
4791 dynamic object, and we will be using that previous
4792 definition anyhow. */
4793
4794 h->u.alias = weaks;
4795 weaks = h;
4796 }
4797
4798 /* Set the alignment of a common symbol. */
4799 if ((common || bfd_is_com_section (sec))
4800 && h->root.type == bfd_link_hash_common)
4801 {
4802 unsigned int align;
4803
4804 if (common)
4805 align = bfd_log2 (isym->st_value);
4806 else
4807 {
4808 /* The new symbol is a common symbol in a shared object.
4809 We need to get the alignment from the section. */
4810 align = new_sec->alignment_power;
4811 }
4812 if (align > old_alignment)
4813 h->root.u.c.p->alignment_power = align;
4814 else
4815 h->root.u.c.p->alignment_power = old_alignment;
4816 }
4817
4818 if (is_elf_hash_table (htab))
4819 {
4820 /* Set a flag in the hash table entry indicating the type of
4821 reference or definition we just found. A dynamic symbol
4822 is one which is referenced or defined by both a regular
4823 object and a shared object. */
4824 bfd_boolean dynsym = FALSE;
4825
4826 /* Plugin symbols aren't normal. Don't set def_regular or
4827 ref_regular for them, or make them dynamic. */
4828 if ((abfd->flags & BFD_PLUGIN) != 0)
4829 ;
4830 else if (! dynamic)
4831 {
4832 if (! definition)
4833 {
4834 h->ref_regular = 1;
4835 if (bind != STB_WEAK)
4836 h->ref_regular_nonweak = 1;
4837 }
4838 else
4839 {
4840 h->def_regular = 1;
4841 if (h->def_dynamic)
4842 {
4843 h->def_dynamic = 0;
4844 h->ref_dynamic = 1;
4845 }
4846 }
4847
4848 /* If the indirect symbol has been forced local, don't
4849 make the real symbol dynamic. */
4850 if ((h == hi || !hi->forced_local)
4851 && (bfd_link_dll (info)
4852 || h->def_dynamic
4853 || h->ref_dynamic))
4854 dynsym = TRUE;
4855 }
4856 else
4857 {
4858 if (! definition)
4859 {
4860 h->ref_dynamic = 1;
4861 hi->ref_dynamic = 1;
4862 }
4863 else
4864 {
4865 h->def_dynamic = 1;
4866 hi->def_dynamic = 1;
4867 }
4868
4869 /* If the indirect symbol has been forced local, don't
4870 make the real symbol dynamic. */
4871 if ((h == hi || !hi->forced_local)
4872 && (h->def_regular
4873 || h->ref_regular
4874 || (h->is_weakalias
4875 && weakdef (h)->dynindx != -1)))
4876 dynsym = TRUE;
4877 }
4878
4879 /* Check to see if we need to add an indirect symbol for
4880 the default name. */
4881 if (definition
4882 || (!override && h->root.type == bfd_link_hash_common))
4883 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4884 sec, value, &old_bfd, &dynsym))
4885 goto error_free_vers;
4886
4887 /* Check the alignment when a common symbol is involved. This
4888 can change when a common symbol is overridden by a normal
4889 definition or a common symbol is ignored due to the old
4890 normal definition. We need to make sure the maximum
4891 alignment is maintained. */
4892 if ((old_alignment || common)
4893 && h->root.type != bfd_link_hash_common)
4894 {
4895 unsigned int common_align;
4896 unsigned int normal_align;
4897 unsigned int symbol_align;
4898 bfd *normal_bfd;
4899 bfd *common_bfd;
4900
4901 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4902 || h->root.type == bfd_link_hash_defweak);
4903
4904 symbol_align = ffs (h->root.u.def.value) - 1;
4905 if (h->root.u.def.section->owner != NULL
4906 && (h->root.u.def.section->owner->flags
4907 & (DYNAMIC | BFD_PLUGIN)) == 0)
4908 {
4909 normal_align = h->root.u.def.section->alignment_power;
4910 if (normal_align > symbol_align)
4911 normal_align = symbol_align;
4912 }
4913 else
4914 normal_align = symbol_align;
4915
4916 if (old_alignment)
4917 {
4918 common_align = old_alignment;
4919 common_bfd = old_bfd;
4920 normal_bfd = abfd;
4921 }
4922 else
4923 {
4924 common_align = bfd_log2 (isym->st_value);
4925 common_bfd = abfd;
4926 normal_bfd = old_bfd;
4927 }
4928
4929 if (normal_align < common_align)
4930 {
4931 /* PR binutils/2735 */
4932 if (normal_bfd == NULL)
4933 _bfd_error_handler
4934 /* xgettext:c-format */
4935 (_("warning: alignment %u of common symbol `%s' in %pB is"
4936 " greater than the alignment (%u) of its section %pA"),
4937 1 << common_align, name, common_bfd,
4938 1 << normal_align, h->root.u.def.section);
4939 else
4940 _bfd_error_handler
4941 /* xgettext:c-format */
4942 (_("warning: alignment %u of symbol `%s' in %pB"
4943 " is smaller than %u in %pB"),
4944 1 << normal_align, name, normal_bfd,
4945 1 << common_align, common_bfd);
4946 }
4947 }
4948
4949 /* Remember the symbol size if it isn't undefined. */
4950 if (isym->st_size != 0
4951 && isym->st_shndx != SHN_UNDEF
4952 && (definition || h->size == 0))
4953 {
4954 if (h->size != 0
4955 && h->size != isym->st_size
4956 && ! size_change_ok)
4957 _bfd_error_handler
4958 /* xgettext:c-format */
4959 (_("warning: size of symbol `%s' changed"
4960 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
4961 name, (uint64_t) h->size, old_bfd,
4962 (uint64_t) isym->st_size, abfd);
4963
4964 h->size = isym->st_size;
4965 }
4966
4967 /* If this is a common symbol, then we always want H->SIZE
4968 to be the size of the common symbol. The code just above
4969 won't fix the size if a common symbol becomes larger. We
4970 don't warn about a size change here, because that is
4971 covered by --warn-common. Allow changes between different
4972 function types. */
4973 if (h->root.type == bfd_link_hash_common)
4974 h->size = h->root.u.c.size;
4975
4976 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4977 && ((definition && !new_weak)
4978 || (old_weak && h->root.type == bfd_link_hash_common)
4979 || h->type == STT_NOTYPE))
4980 {
4981 unsigned int type = ELF_ST_TYPE (isym->st_info);
4982
4983 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4984 symbol. */
4985 if (type == STT_GNU_IFUNC
4986 && (abfd->flags & DYNAMIC) != 0)
4987 type = STT_FUNC;
4988
4989 if (h->type != type)
4990 {
4991 if (h->type != STT_NOTYPE && ! type_change_ok)
4992 /* xgettext:c-format */
4993 _bfd_error_handler
4994 (_("warning: type of symbol `%s' changed"
4995 " from %d to %d in %pB"),
4996 name, h->type, type, abfd);
4997
4998 h->type = type;
4999 }
5000 }
5001
5002 /* Merge st_other field. */
5003 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
5004
5005 /* We don't want to make debug symbol dynamic. */
5006 if (definition
5007 && (sec->flags & SEC_DEBUGGING)
5008 && !bfd_link_relocatable (info))
5009 dynsym = FALSE;
5010
5011 /* Nor should we make plugin symbols dynamic. */
5012 if ((abfd->flags & BFD_PLUGIN) != 0)
5013 dynsym = FALSE;
5014
5015 if (definition)
5016 {
5017 h->target_internal = isym->st_target_internal;
5018 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5019 }
5020
5021 if (definition && !dynamic)
5022 {
5023 char *p = strchr (name, ELF_VER_CHR);
5024 if (p != NULL && p[1] != ELF_VER_CHR)
5025 {
5026 /* Queue non-default versions so that .symver x, x@FOO
5027 aliases can be checked. */
5028 if (!nondeflt_vers)
5029 {
5030 amt = ((isymend - isym + 1)
5031 * sizeof (struct elf_link_hash_entry *));
5032 nondeflt_vers
5033 = (struct elf_link_hash_entry **) bfd_malloc (amt);
5034 if (!nondeflt_vers)
5035 goto error_free_vers;
5036 }
5037 nondeflt_vers[nondeflt_vers_cnt++] = h;
5038 }
5039 }
5040
5041 if (dynsym && h->dynindx == -1)
5042 {
5043 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5044 goto error_free_vers;
5045 if (h->is_weakalias
5046 && weakdef (h)->dynindx == -1)
5047 {
5048 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5049 goto error_free_vers;
5050 }
5051 }
5052 else if (h->dynindx != -1)
5053 /* If the symbol already has a dynamic index, but
5054 visibility says it should not be visible, turn it into
5055 a local symbol. */
5056 switch (ELF_ST_VISIBILITY (h->other))
5057 {
5058 case STV_INTERNAL:
5059 case STV_HIDDEN:
5060 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
5061 dynsym = FALSE;
5062 break;
5063 }
5064
5065 /* Don't add DT_NEEDED for references from the dummy bfd nor
5066 for unmatched symbol. */
5067 if (!add_needed
5068 && matched
5069 && definition
5070 && ((dynsym
5071 && h->ref_regular_nonweak
5072 && (old_bfd == NULL
5073 || (old_bfd->flags & BFD_PLUGIN) == 0))
5074 || (h->ref_dynamic_nonweak
5075 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5076 && !on_needed_list (elf_dt_name (abfd),
5077 htab->needed, NULL))))
5078 {
5079 int ret;
5080 const char *soname = elf_dt_name (abfd);
5081
5082 info->callbacks->minfo ("%!", soname, old_bfd,
5083 h->root.root.string);
5084
5085 /* A symbol from a library loaded via DT_NEEDED of some
5086 other library is referenced by a regular object.
5087 Add a DT_NEEDED entry for it. Issue an error if
5088 --no-add-needed is used and the reference was not
5089 a weak one. */
5090 if (old_bfd != NULL
5091 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5092 {
5093 _bfd_error_handler
5094 /* xgettext:c-format */
5095 (_("%pB: undefined reference to symbol '%s'"),
5096 old_bfd, name);
5097 bfd_set_error (bfd_error_missing_dso);
5098 goto error_free_vers;
5099 }
5100
5101 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5102 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5103
5104 add_needed = TRUE;
5105 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
5106 if (ret < 0)
5107 goto error_free_vers;
5108
5109 BFD_ASSERT (ret == 0);
5110 }
5111 }
5112 }
5113
5114 if (info->lto_plugin_active
5115 && !bfd_link_relocatable (info)
5116 && (abfd->flags & BFD_PLUGIN) == 0
5117 && !just_syms
5118 && extsymcount)
5119 {
5120 int r_sym_shift;
5121
5122 if (bed->s->arch_size == 32)
5123 r_sym_shift = 8;
5124 else
5125 r_sym_shift = 32;
5126
5127 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5128 referenced in regular objects so that linker plugin will get
5129 the correct symbol resolution. */
5130
5131 sym_hash = elf_sym_hashes (abfd);
5132 for (s = abfd->sections; s != NULL; s = s->next)
5133 {
5134 Elf_Internal_Rela *internal_relocs;
5135 Elf_Internal_Rela *rel, *relend;
5136
5137 /* Don't check relocations in excluded sections. */
5138 if ((s->flags & SEC_RELOC) == 0
5139 || s->reloc_count == 0
5140 || (s->flags & SEC_EXCLUDE) != 0
5141 || ((info->strip == strip_all
5142 || info->strip == strip_debugger)
5143 && (s->flags & SEC_DEBUGGING) != 0))
5144 continue;
5145
5146 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5147 NULL,
5148 info->keep_memory);
5149 if (internal_relocs == NULL)
5150 goto error_free_vers;
5151
5152 rel = internal_relocs;
5153 relend = rel + s->reloc_count;
5154 for ( ; rel < relend; rel++)
5155 {
5156 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5157 struct elf_link_hash_entry *h;
5158
5159 /* Skip local symbols. */
5160 if (r_symndx < extsymoff)
5161 continue;
5162
5163 h = sym_hash[r_symndx - extsymoff];
5164 if (h != NULL)
5165 h->root.non_ir_ref_regular = 1;
5166 }
5167
5168 if (elf_section_data (s)->relocs != internal_relocs)
5169 free (internal_relocs);
5170 }
5171 }
5172
5173 if (extversym != NULL)
5174 {
5175 free (extversym);
5176 extversym = NULL;
5177 }
5178
5179 if (isymbuf != NULL)
5180 {
5181 free (isymbuf);
5182 isymbuf = NULL;
5183 }
5184
5185 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5186 {
5187 unsigned int i;
5188
5189 /* Restore the symbol table. */
5190 old_ent = (char *) old_tab + tabsize;
5191 memset (elf_sym_hashes (abfd), 0,
5192 extsymcount * sizeof (struct elf_link_hash_entry *));
5193 htab->root.table.table = old_table;
5194 htab->root.table.size = old_size;
5195 htab->root.table.count = old_count;
5196 memcpy (htab->root.table.table, old_tab, tabsize);
5197 htab->root.undefs = old_undefs;
5198 htab->root.undefs_tail = old_undefs_tail;
5199 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5200 free (old_strtab);
5201 old_strtab = NULL;
5202 for (i = 0; i < htab->root.table.size; i++)
5203 {
5204 struct bfd_hash_entry *p;
5205 struct elf_link_hash_entry *h;
5206 bfd_size_type size;
5207 unsigned int alignment_power;
5208 unsigned int non_ir_ref_dynamic;
5209
5210 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5211 {
5212 h = (struct elf_link_hash_entry *) p;
5213 if (h->root.type == bfd_link_hash_warning)
5214 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5215
5216 /* Preserve the maximum alignment and size for common
5217 symbols even if this dynamic lib isn't on DT_NEEDED
5218 since it can still be loaded at run time by another
5219 dynamic lib. */
5220 if (h->root.type == bfd_link_hash_common)
5221 {
5222 size = h->root.u.c.size;
5223 alignment_power = h->root.u.c.p->alignment_power;
5224 }
5225 else
5226 {
5227 size = 0;
5228 alignment_power = 0;
5229 }
5230 /* Preserve non_ir_ref_dynamic so that this symbol
5231 will be exported when the dynamic lib becomes needed
5232 in the second pass. */
5233 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5234 memcpy (p, old_ent, htab->root.table.entsize);
5235 old_ent = (char *) old_ent + htab->root.table.entsize;
5236 h = (struct elf_link_hash_entry *) p;
5237 if (h->root.type == bfd_link_hash_warning)
5238 {
5239 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5240 old_ent = (char *) old_ent + htab->root.table.entsize;
5241 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5242 }
5243 if (h->root.type == bfd_link_hash_common)
5244 {
5245 if (size > h->root.u.c.size)
5246 h->root.u.c.size = size;
5247 if (alignment_power > h->root.u.c.p->alignment_power)
5248 h->root.u.c.p->alignment_power = alignment_power;
5249 }
5250 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5251 }
5252 }
5253
5254 /* Make a special call to the linker "notice" function to
5255 tell it that symbols added for crefs may need to be removed. */
5256 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5257 goto error_free_vers;
5258
5259 free (old_tab);
5260 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5261 alloc_mark);
5262 if (nondeflt_vers != NULL)
5263 free (nondeflt_vers);
5264 return TRUE;
5265 }
5266
5267 if (old_tab != NULL)
5268 {
5269 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5270 goto error_free_vers;
5271 free (old_tab);
5272 old_tab = NULL;
5273 }
5274
5275 /* Now that all the symbols from this input file are created, if
5276 not performing a relocatable link, handle .symver foo, foo@BAR
5277 such that any relocs against foo become foo@BAR. */
5278 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5279 {
5280 size_t cnt, symidx;
5281
5282 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5283 {
5284 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5285 char *shortname, *p;
5286
5287 p = strchr (h->root.root.string, ELF_VER_CHR);
5288 if (p == NULL
5289 || (h->root.type != bfd_link_hash_defined
5290 && h->root.type != bfd_link_hash_defweak))
5291 continue;
5292
5293 amt = p - h->root.root.string;
5294 shortname = (char *) bfd_malloc (amt + 1);
5295 if (!shortname)
5296 goto error_free_vers;
5297 memcpy (shortname, h->root.root.string, amt);
5298 shortname[amt] = '\0';
5299
5300 hi = (struct elf_link_hash_entry *)
5301 bfd_link_hash_lookup (&htab->root, shortname,
5302 FALSE, FALSE, FALSE);
5303 if (hi != NULL
5304 && hi->root.type == h->root.type
5305 && hi->root.u.def.value == h->root.u.def.value
5306 && hi->root.u.def.section == h->root.u.def.section)
5307 {
5308 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5309 hi->root.type = bfd_link_hash_indirect;
5310 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5311 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5312 sym_hash = elf_sym_hashes (abfd);
5313 if (sym_hash)
5314 for (symidx = 0; symidx < extsymcount; ++symidx)
5315 if (sym_hash[symidx] == hi)
5316 {
5317 sym_hash[symidx] = h;
5318 break;
5319 }
5320 }
5321 free (shortname);
5322 }
5323 free (nondeflt_vers);
5324 nondeflt_vers = NULL;
5325 }
5326
5327 /* Now set the alias field correctly for all the weak defined
5328 symbols we found. The only way to do this is to search all the
5329 symbols. Since we only need the information for non functions in
5330 dynamic objects, that's the only time we actually put anything on
5331 the list WEAKS. We need this information so that if a regular
5332 object refers to a symbol defined weakly in a dynamic object, the
5333 real symbol in the dynamic object is also put in the dynamic
5334 symbols; we also must arrange for both symbols to point to the
5335 same memory location. We could handle the general case of symbol
5336 aliasing, but a general symbol alias can only be generated in
5337 assembler code, handling it correctly would be very time
5338 consuming, and other ELF linkers don't handle general aliasing
5339 either. */
5340 if (weaks != NULL)
5341 {
5342 struct elf_link_hash_entry **hpp;
5343 struct elf_link_hash_entry **hppend;
5344 struct elf_link_hash_entry **sorted_sym_hash;
5345 struct elf_link_hash_entry *h;
5346 size_t sym_count;
5347
5348 /* Since we have to search the whole symbol list for each weak
5349 defined symbol, search time for N weak defined symbols will be
5350 O(N^2). Binary search will cut it down to O(NlogN). */
5351 amt = extsymcount;
5352 amt *= sizeof (struct elf_link_hash_entry *);
5353 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
5354 if (sorted_sym_hash == NULL)
5355 goto error_return;
5356 sym_hash = sorted_sym_hash;
5357 hpp = elf_sym_hashes (abfd);
5358 hppend = hpp + extsymcount;
5359 sym_count = 0;
5360 for (; hpp < hppend; hpp++)
5361 {
5362 h = *hpp;
5363 if (h != NULL
5364 && h->root.type == bfd_link_hash_defined
5365 && !bed->is_function_type (h->type))
5366 {
5367 *sym_hash = h;
5368 sym_hash++;
5369 sym_count++;
5370 }
5371 }
5372
5373 qsort (sorted_sym_hash, sym_count,
5374 sizeof (struct elf_link_hash_entry *),
5375 elf_sort_symbol);
5376
5377 while (weaks != NULL)
5378 {
5379 struct elf_link_hash_entry *hlook;
5380 asection *slook;
5381 bfd_vma vlook;
5382 size_t i, j, idx = 0;
5383
5384 hlook = weaks;
5385 weaks = hlook->u.alias;
5386 hlook->u.alias = NULL;
5387
5388 if (hlook->root.type != bfd_link_hash_defined
5389 && hlook->root.type != bfd_link_hash_defweak)
5390 continue;
5391
5392 slook = hlook->root.u.def.section;
5393 vlook = hlook->root.u.def.value;
5394
5395 i = 0;
5396 j = sym_count;
5397 while (i != j)
5398 {
5399 bfd_signed_vma vdiff;
5400 idx = (i + j) / 2;
5401 h = sorted_sym_hash[idx];
5402 vdiff = vlook - h->root.u.def.value;
5403 if (vdiff < 0)
5404 j = idx;
5405 else if (vdiff > 0)
5406 i = idx + 1;
5407 else
5408 {
5409 int sdiff = slook->id - h->root.u.def.section->id;
5410 if (sdiff < 0)
5411 j = idx;
5412 else if (sdiff > 0)
5413 i = idx + 1;
5414 else
5415 break;
5416 }
5417 }
5418
5419 /* We didn't find a value/section match. */
5420 if (i == j)
5421 continue;
5422
5423 /* With multiple aliases, or when the weak symbol is already
5424 strongly defined, we have multiple matching symbols and
5425 the binary search above may land on any of them. Step
5426 one past the matching symbol(s). */
5427 while (++idx != j)
5428 {
5429 h = sorted_sym_hash[idx];
5430 if (h->root.u.def.section != slook
5431 || h->root.u.def.value != vlook)
5432 break;
5433 }
5434
5435 /* Now look back over the aliases. Since we sorted by size
5436 as well as value and section, we'll choose the one with
5437 the largest size. */
5438 while (idx-- != i)
5439 {
5440 h = sorted_sym_hash[idx];
5441
5442 /* Stop if value or section doesn't match. */
5443 if (h->root.u.def.section != slook
5444 || h->root.u.def.value != vlook)
5445 break;
5446 else if (h != hlook)
5447 {
5448 struct elf_link_hash_entry *t;
5449
5450 hlook->u.alias = h;
5451 hlook->is_weakalias = 1;
5452 t = h;
5453 if (t->u.alias != NULL)
5454 while (t->u.alias != h)
5455 t = t->u.alias;
5456 t->u.alias = hlook;
5457
5458 /* If the weak definition is in the list of dynamic
5459 symbols, make sure the real definition is put
5460 there as well. */
5461 if (hlook->dynindx != -1 && h->dynindx == -1)
5462 {
5463 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5464 {
5465 err_free_sym_hash:
5466 free (sorted_sym_hash);
5467 goto error_return;
5468 }
5469 }
5470
5471 /* If the real definition is in the list of dynamic
5472 symbols, make sure the weak definition is put
5473 there as well. If we don't do this, then the
5474 dynamic loader might not merge the entries for the
5475 real definition and the weak definition. */
5476 if (h->dynindx != -1 && hlook->dynindx == -1)
5477 {
5478 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5479 goto err_free_sym_hash;
5480 }
5481 break;
5482 }
5483 }
5484 }
5485
5486 free (sorted_sym_hash);
5487 }
5488
5489 if (bed->check_directives
5490 && !(*bed->check_directives) (abfd, info))
5491 return FALSE;
5492
5493 /* If this is a non-traditional link, try to optimize the handling
5494 of the .stab/.stabstr sections. */
5495 if (! dynamic
5496 && ! info->traditional_format
5497 && is_elf_hash_table (htab)
5498 && (info->strip != strip_all && info->strip != strip_debugger))
5499 {
5500 asection *stabstr;
5501
5502 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5503 if (stabstr != NULL)
5504 {
5505 bfd_size_type string_offset = 0;
5506 asection *stab;
5507
5508 for (stab = abfd->sections; stab; stab = stab->next)
5509 if (CONST_STRNEQ (stab->name, ".stab")
5510 && (!stab->name[5] ||
5511 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5512 && (stab->flags & SEC_MERGE) == 0
5513 && !bfd_is_abs_section (stab->output_section))
5514 {
5515 struct bfd_elf_section_data *secdata;
5516
5517 secdata = elf_section_data (stab);
5518 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5519 stabstr, &secdata->sec_info,
5520 &string_offset))
5521 goto error_return;
5522 if (secdata->sec_info)
5523 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5524 }
5525 }
5526 }
5527
5528 if (is_elf_hash_table (htab) && add_needed)
5529 {
5530 /* Add this bfd to the loaded list. */
5531 struct elf_link_loaded_list *n;
5532
5533 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5534 if (n == NULL)
5535 goto error_return;
5536 n->abfd = abfd;
5537 n->next = htab->loaded;
5538 htab->loaded = n;
5539 }
5540
5541 return TRUE;
5542
5543 error_free_vers:
5544 if (old_tab != NULL)
5545 free (old_tab);
5546 if (old_strtab != NULL)
5547 free (old_strtab);
5548 if (nondeflt_vers != NULL)
5549 free (nondeflt_vers);
5550 if (extversym != NULL)
5551 free (extversym);
5552 error_free_sym:
5553 if (isymbuf != NULL)
5554 free (isymbuf);
5555 error_return:
5556 return FALSE;
5557 }
5558
5559 /* Return the linker hash table entry of a symbol that might be
5560 satisfied by an archive symbol. Return -1 on error. */
5561
5562 struct elf_link_hash_entry *
5563 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5564 struct bfd_link_info *info,
5565 const char *name)
5566 {
5567 struct elf_link_hash_entry *h;
5568 char *p, *copy;
5569 size_t len, first;
5570
5571 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5572 if (h != NULL)
5573 return h;
5574
5575 /* If this is a default version (the name contains @@), look up the
5576 symbol again with only one `@' as well as without the version.
5577 The effect is that references to the symbol with and without the
5578 version will be matched by the default symbol in the archive. */
5579
5580 p = strchr (name, ELF_VER_CHR);
5581 if (p == NULL || p[1] != ELF_VER_CHR)
5582 return h;
5583
5584 /* First check with only one `@'. */
5585 len = strlen (name);
5586 copy = (char *) bfd_alloc (abfd, len);
5587 if (copy == NULL)
5588 return (struct elf_link_hash_entry *) -1;
5589
5590 first = p - name + 1;
5591 memcpy (copy, name, first);
5592 memcpy (copy + first, name + first + 1, len - first);
5593
5594 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5595 if (h == NULL)
5596 {
5597 /* We also need to check references to the symbol without the
5598 version. */
5599 copy[first - 1] = '\0';
5600 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5601 FALSE, FALSE, TRUE);
5602 }
5603
5604 bfd_release (abfd, copy);
5605 return h;
5606 }
5607
5608 /* Add symbols from an ELF archive file to the linker hash table. We
5609 don't use _bfd_generic_link_add_archive_symbols because we need to
5610 handle versioned symbols.
5611
5612 Fortunately, ELF archive handling is simpler than that done by
5613 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5614 oddities. In ELF, if we find a symbol in the archive map, and the
5615 symbol is currently undefined, we know that we must pull in that
5616 object file.
5617
5618 Unfortunately, we do have to make multiple passes over the symbol
5619 table until nothing further is resolved. */
5620
5621 static bfd_boolean
5622 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5623 {
5624 symindex c;
5625 unsigned char *included = NULL;
5626 carsym *symdefs;
5627 bfd_boolean loop;
5628 bfd_size_type amt;
5629 const struct elf_backend_data *bed;
5630 struct elf_link_hash_entry * (*archive_symbol_lookup)
5631 (bfd *, struct bfd_link_info *, const char *);
5632
5633 if (! bfd_has_map (abfd))
5634 {
5635 /* An empty archive is a special case. */
5636 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5637 return TRUE;
5638 bfd_set_error (bfd_error_no_armap);
5639 return FALSE;
5640 }
5641
5642 /* Keep track of all symbols we know to be already defined, and all
5643 files we know to be already included. This is to speed up the
5644 second and subsequent passes. */
5645 c = bfd_ardata (abfd)->symdef_count;
5646 if (c == 0)
5647 return TRUE;
5648 amt = c;
5649 amt *= sizeof (*included);
5650 included = (unsigned char *) bfd_zmalloc (amt);
5651 if (included == NULL)
5652 return FALSE;
5653
5654 symdefs = bfd_ardata (abfd)->symdefs;
5655 bed = get_elf_backend_data (abfd);
5656 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5657
5658 do
5659 {
5660 file_ptr last;
5661 symindex i;
5662 carsym *symdef;
5663 carsym *symdefend;
5664
5665 loop = FALSE;
5666 last = -1;
5667
5668 symdef = symdefs;
5669 symdefend = symdef + c;
5670 for (i = 0; symdef < symdefend; symdef++, i++)
5671 {
5672 struct elf_link_hash_entry *h;
5673 bfd *element;
5674 struct bfd_link_hash_entry *undefs_tail;
5675 symindex mark;
5676
5677 if (included[i])
5678 continue;
5679 if (symdef->file_offset == last)
5680 {
5681 included[i] = TRUE;
5682 continue;
5683 }
5684
5685 h = archive_symbol_lookup (abfd, info, symdef->name);
5686 if (h == (struct elf_link_hash_entry *) -1)
5687 goto error_return;
5688
5689 if (h == NULL)
5690 continue;
5691
5692 if (h->root.type == bfd_link_hash_common)
5693 {
5694 /* We currently have a common symbol. The archive map contains
5695 a reference to this symbol, so we may want to include it. We
5696 only want to include it however, if this archive element
5697 contains a definition of the symbol, not just another common
5698 declaration of it.
5699
5700 Unfortunately some archivers (including GNU ar) will put
5701 declarations of common symbols into their archive maps, as
5702 well as real definitions, so we cannot just go by the archive
5703 map alone. Instead we must read in the element's symbol
5704 table and check that to see what kind of symbol definition
5705 this is. */
5706 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5707 continue;
5708 }
5709 else if (h->root.type != bfd_link_hash_undefined)
5710 {
5711 if (h->root.type != bfd_link_hash_undefweak)
5712 /* Symbol must be defined. Don't check it again. */
5713 included[i] = TRUE;
5714 continue;
5715 }
5716
5717 /* We need to include this archive member. */
5718 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5719 if (element == NULL)
5720 goto error_return;
5721
5722 if (! bfd_check_format (element, bfd_object))
5723 goto error_return;
5724
5725 undefs_tail = info->hash->undefs_tail;
5726
5727 if (!(*info->callbacks
5728 ->add_archive_element) (info, element, symdef->name, &element))
5729 continue;
5730 if (!bfd_link_add_symbols (element, info))
5731 goto error_return;
5732
5733 /* If there are any new undefined symbols, we need to make
5734 another pass through the archive in order to see whether
5735 they can be defined. FIXME: This isn't perfect, because
5736 common symbols wind up on undefs_tail and because an
5737 undefined symbol which is defined later on in this pass
5738 does not require another pass. This isn't a bug, but it
5739 does make the code less efficient than it could be. */
5740 if (undefs_tail != info->hash->undefs_tail)
5741 loop = TRUE;
5742
5743 /* Look backward to mark all symbols from this object file
5744 which we have already seen in this pass. */
5745 mark = i;
5746 do
5747 {
5748 included[mark] = TRUE;
5749 if (mark == 0)
5750 break;
5751 --mark;
5752 }
5753 while (symdefs[mark].file_offset == symdef->file_offset);
5754
5755 /* We mark subsequent symbols from this object file as we go
5756 on through the loop. */
5757 last = symdef->file_offset;
5758 }
5759 }
5760 while (loop);
5761
5762 free (included);
5763
5764 return TRUE;
5765
5766 error_return:
5767 if (included != NULL)
5768 free (included);
5769 return FALSE;
5770 }
5771
5772 /* Given an ELF BFD, add symbols to the global hash table as
5773 appropriate. */
5774
5775 bfd_boolean
5776 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5777 {
5778 switch (bfd_get_format (abfd))
5779 {
5780 case bfd_object:
5781 return elf_link_add_object_symbols (abfd, info);
5782 case bfd_archive:
5783 return elf_link_add_archive_symbols (abfd, info);
5784 default:
5785 bfd_set_error (bfd_error_wrong_format);
5786 return FALSE;
5787 }
5788 }
5789 \f
5790 struct hash_codes_info
5791 {
5792 unsigned long *hashcodes;
5793 bfd_boolean error;
5794 };
5795
5796 /* This function will be called though elf_link_hash_traverse to store
5797 all hash value of the exported symbols in an array. */
5798
5799 static bfd_boolean
5800 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5801 {
5802 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5803 const char *name;
5804 unsigned long ha;
5805 char *alc = NULL;
5806
5807 /* Ignore indirect symbols. These are added by the versioning code. */
5808 if (h->dynindx == -1)
5809 return TRUE;
5810
5811 name = h->root.root.string;
5812 if (h->versioned >= versioned)
5813 {
5814 char *p = strchr (name, ELF_VER_CHR);
5815 if (p != NULL)
5816 {
5817 alc = (char *) bfd_malloc (p - name + 1);
5818 if (alc == NULL)
5819 {
5820 inf->error = TRUE;
5821 return FALSE;
5822 }
5823 memcpy (alc, name, p - name);
5824 alc[p - name] = '\0';
5825 name = alc;
5826 }
5827 }
5828
5829 /* Compute the hash value. */
5830 ha = bfd_elf_hash (name);
5831
5832 /* Store the found hash value in the array given as the argument. */
5833 *(inf->hashcodes)++ = ha;
5834
5835 /* And store it in the struct so that we can put it in the hash table
5836 later. */
5837 h->u.elf_hash_value = ha;
5838
5839 if (alc != NULL)
5840 free (alc);
5841
5842 return TRUE;
5843 }
5844
5845 struct collect_gnu_hash_codes
5846 {
5847 bfd *output_bfd;
5848 const struct elf_backend_data *bed;
5849 unsigned long int nsyms;
5850 unsigned long int maskbits;
5851 unsigned long int *hashcodes;
5852 unsigned long int *hashval;
5853 unsigned long int *indx;
5854 unsigned long int *counts;
5855 bfd_vma *bitmask;
5856 bfd_byte *contents;
5857 bfd_size_type xlat;
5858 long int min_dynindx;
5859 unsigned long int bucketcount;
5860 unsigned long int symindx;
5861 long int local_indx;
5862 long int shift1, shift2;
5863 unsigned long int mask;
5864 bfd_boolean error;
5865 };
5866
5867 /* This function will be called though elf_link_hash_traverse to store
5868 all hash value of the exported symbols in an array. */
5869
5870 static bfd_boolean
5871 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5872 {
5873 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5874 const char *name;
5875 unsigned long ha;
5876 char *alc = NULL;
5877
5878 /* Ignore indirect symbols. These are added by the versioning code. */
5879 if (h->dynindx == -1)
5880 return TRUE;
5881
5882 /* Ignore also local symbols and undefined symbols. */
5883 if (! (*s->bed->elf_hash_symbol) (h))
5884 return TRUE;
5885
5886 name = h->root.root.string;
5887 if (h->versioned >= versioned)
5888 {
5889 char *p = strchr (name, ELF_VER_CHR);
5890 if (p != NULL)
5891 {
5892 alc = (char *) bfd_malloc (p - name + 1);
5893 if (alc == NULL)
5894 {
5895 s->error = TRUE;
5896 return FALSE;
5897 }
5898 memcpy (alc, name, p - name);
5899 alc[p - name] = '\0';
5900 name = alc;
5901 }
5902 }
5903
5904 /* Compute the hash value. */
5905 ha = bfd_elf_gnu_hash (name);
5906
5907 /* Store the found hash value in the array for compute_bucket_count,
5908 and also for .dynsym reordering purposes. */
5909 s->hashcodes[s->nsyms] = ha;
5910 s->hashval[h->dynindx] = ha;
5911 ++s->nsyms;
5912 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5913 s->min_dynindx = h->dynindx;
5914
5915 if (alc != NULL)
5916 free (alc);
5917
5918 return TRUE;
5919 }
5920
5921 /* This function will be called though elf_link_hash_traverse to do
5922 final dynamic symbol renumbering in case of .gnu.hash.
5923 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
5924 to the translation table. */
5925
5926 static bfd_boolean
5927 elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
5928 {
5929 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5930 unsigned long int bucket;
5931 unsigned long int val;
5932
5933 /* Ignore indirect symbols. */
5934 if (h->dynindx == -1)
5935 return TRUE;
5936
5937 /* Ignore also local symbols and undefined symbols. */
5938 if (! (*s->bed->elf_hash_symbol) (h))
5939 {
5940 if (h->dynindx >= s->min_dynindx)
5941 {
5942 if (s->bed->record_xhash_symbol != NULL)
5943 {
5944 (*s->bed->record_xhash_symbol) (h, 0);
5945 s->local_indx++;
5946 }
5947 else
5948 h->dynindx = s->local_indx++;
5949 }
5950 return TRUE;
5951 }
5952
5953 bucket = s->hashval[h->dynindx] % s->bucketcount;
5954 val = (s->hashval[h->dynindx] >> s->shift1)
5955 & ((s->maskbits >> s->shift1) - 1);
5956 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5957 s->bitmask[val]
5958 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5959 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5960 if (s->counts[bucket] == 1)
5961 /* Last element terminates the chain. */
5962 val |= 1;
5963 bfd_put_32 (s->output_bfd, val,
5964 s->contents + (s->indx[bucket] - s->symindx) * 4);
5965 --s->counts[bucket];
5966 if (s->bed->record_xhash_symbol != NULL)
5967 {
5968 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
5969
5970 (*s->bed->record_xhash_symbol) (h, xlat_loc);
5971 }
5972 else
5973 h->dynindx = s->indx[bucket]++;
5974 return TRUE;
5975 }
5976
5977 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5978
5979 bfd_boolean
5980 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5981 {
5982 return !(h->forced_local
5983 || h->root.type == bfd_link_hash_undefined
5984 || h->root.type == bfd_link_hash_undefweak
5985 || ((h->root.type == bfd_link_hash_defined
5986 || h->root.type == bfd_link_hash_defweak)
5987 && h->root.u.def.section->output_section == NULL));
5988 }
5989
5990 /* Array used to determine the number of hash table buckets to use
5991 based on the number of symbols there are. If there are fewer than
5992 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5993 fewer than 37 we use 17 buckets, and so forth. We never use more
5994 than 32771 buckets. */
5995
5996 static const size_t elf_buckets[] =
5997 {
5998 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5999 16411, 32771, 0
6000 };
6001
6002 /* Compute bucket count for hashing table. We do not use a static set
6003 of possible tables sizes anymore. Instead we determine for all
6004 possible reasonable sizes of the table the outcome (i.e., the
6005 number of collisions etc) and choose the best solution. The
6006 weighting functions are not too simple to allow the table to grow
6007 without bounds. Instead one of the weighting factors is the size.
6008 Therefore the result is always a good payoff between few collisions
6009 (= short chain lengths) and table size. */
6010 static size_t
6011 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6012 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6013 unsigned long int nsyms,
6014 int gnu_hash)
6015 {
6016 size_t best_size = 0;
6017 unsigned long int i;
6018
6019 /* We have a problem here. The following code to optimize the table
6020 size requires an integer type with more the 32 bits. If
6021 BFD_HOST_U_64_BIT is set we know about such a type. */
6022 #ifdef BFD_HOST_U_64_BIT
6023 if (info->optimize)
6024 {
6025 size_t minsize;
6026 size_t maxsize;
6027 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
6028 bfd *dynobj = elf_hash_table (info)->dynobj;
6029 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6030 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
6031 unsigned long int *counts;
6032 bfd_size_type amt;
6033 unsigned int no_improvement_count = 0;
6034
6035 /* Possible optimization parameters: if we have NSYMS symbols we say
6036 that the hashing table must at least have NSYMS/4 and at most
6037 2*NSYMS buckets. */
6038 minsize = nsyms / 4;
6039 if (minsize == 0)
6040 minsize = 1;
6041 best_size = maxsize = nsyms * 2;
6042 if (gnu_hash)
6043 {
6044 if (minsize < 2)
6045 minsize = 2;
6046 if ((best_size & 31) == 0)
6047 ++best_size;
6048 }
6049
6050 /* Create array where we count the collisions in. We must use bfd_malloc
6051 since the size could be large. */
6052 amt = maxsize;
6053 amt *= sizeof (unsigned long int);
6054 counts = (unsigned long int *) bfd_malloc (amt);
6055 if (counts == NULL)
6056 return 0;
6057
6058 /* Compute the "optimal" size for the hash table. The criteria is a
6059 minimal chain length. The minor criteria is (of course) the size
6060 of the table. */
6061 for (i = minsize; i < maxsize; ++i)
6062 {
6063 /* Walk through the array of hashcodes and count the collisions. */
6064 BFD_HOST_U_64_BIT max;
6065 unsigned long int j;
6066 unsigned long int fact;
6067
6068 if (gnu_hash && (i & 31) == 0)
6069 continue;
6070
6071 memset (counts, '\0', i * sizeof (unsigned long int));
6072
6073 /* Determine how often each hash bucket is used. */
6074 for (j = 0; j < nsyms; ++j)
6075 ++counts[hashcodes[j] % i];
6076
6077 /* For the weight function we need some information about the
6078 pagesize on the target. This is information need not be 100%
6079 accurate. Since this information is not available (so far) we
6080 define it here to a reasonable default value. If it is crucial
6081 to have a better value some day simply define this value. */
6082 # ifndef BFD_TARGET_PAGESIZE
6083 # define BFD_TARGET_PAGESIZE (4096)
6084 # endif
6085
6086 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6087 and the chains. */
6088 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6089
6090 # if 1
6091 /* Variant 1: optimize for short chains. We add the squares
6092 of all the chain lengths (which favors many small chain
6093 over a few long chains). */
6094 for (j = 0; j < i; ++j)
6095 max += counts[j] * counts[j];
6096
6097 /* This adds penalties for the overall size of the table. */
6098 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6099 max *= fact * fact;
6100 # else
6101 /* Variant 2: Optimize a lot more for small table. Here we
6102 also add squares of the size but we also add penalties for
6103 empty slots (the +1 term). */
6104 for (j = 0; j < i; ++j)
6105 max += (1 + counts[j]) * (1 + counts[j]);
6106
6107 /* The overall size of the table is considered, but not as
6108 strong as in variant 1, where it is squared. */
6109 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6110 max *= fact;
6111 # endif
6112
6113 /* Compare with current best results. */
6114 if (max < best_chlen)
6115 {
6116 best_chlen = max;
6117 best_size = i;
6118 no_improvement_count = 0;
6119 }
6120 /* PR 11843: Avoid futile long searches for the best bucket size
6121 when there are a large number of symbols. */
6122 else if (++no_improvement_count == 100)
6123 break;
6124 }
6125
6126 free (counts);
6127 }
6128 else
6129 #endif /* defined (BFD_HOST_U_64_BIT) */
6130 {
6131 /* This is the fallback solution if no 64bit type is available or if we
6132 are not supposed to spend much time on optimizations. We select the
6133 bucket count using a fixed set of numbers. */
6134 for (i = 0; elf_buckets[i] != 0; i++)
6135 {
6136 best_size = elf_buckets[i];
6137 if (nsyms < elf_buckets[i + 1])
6138 break;
6139 }
6140 if (gnu_hash && best_size < 2)
6141 best_size = 2;
6142 }
6143
6144 return best_size;
6145 }
6146
6147 /* Size any SHT_GROUP section for ld -r. */
6148
6149 bfd_boolean
6150 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6151 {
6152 bfd *ibfd;
6153 asection *s;
6154
6155 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6156 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6157 && (s = ibfd->sections) != NULL
6158 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6159 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6160 return FALSE;
6161 return TRUE;
6162 }
6163
6164 /* Set a default stack segment size. The value in INFO wins. If it
6165 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6166 undefined it is initialized. */
6167
6168 bfd_boolean
6169 bfd_elf_stack_segment_size (bfd *output_bfd,
6170 struct bfd_link_info *info,
6171 const char *legacy_symbol,
6172 bfd_vma default_size)
6173 {
6174 struct elf_link_hash_entry *h = NULL;
6175
6176 /* Look for legacy symbol. */
6177 if (legacy_symbol)
6178 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6179 FALSE, FALSE, FALSE);
6180 if (h && (h->root.type == bfd_link_hash_defined
6181 || h->root.type == bfd_link_hash_defweak)
6182 && h->def_regular
6183 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6184 {
6185 /* The symbol has no type if specified on the command line. */
6186 h->type = STT_OBJECT;
6187 if (info->stacksize)
6188 /* xgettext:c-format */
6189 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6190 output_bfd, legacy_symbol);
6191 else if (h->root.u.def.section != bfd_abs_section_ptr)
6192 /* xgettext:c-format */
6193 _bfd_error_handler (_("%pB: %s not absolute"),
6194 output_bfd, legacy_symbol);
6195 else
6196 info->stacksize = h->root.u.def.value;
6197 }
6198
6199 if (!info->stacksize)
6200 /* If the user didn't set a size, or explicitly inhibit the
6201 size, set it now. */
6202 info->stacksize = default_size;
6203
6204 /* Provide the legacy symbol, if it is referenced. */
6205 if (h && (h->root.type == bfd_link_hash_undefined
6206 || h->root.type == bfd_link_hash_undefweak))
6207 {
6208 struct bfd_link_hash_entry *bh = NULL;
6209
6210 if (!(_bfd_generic_link_add_one_symbol
6211 (info, output_bfd, legacy_symbol,
6212 BSF_GLOBAL, bfd_abs_section_ptr,
6213 info->stacksize >= 0 ? info->stacksize : 0,
6214 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6215 return FALSE;
6216
6217 h = (struct elf_link_hash_entry *) bh;
6218 h->def_regular = 1;
6219 h->type = STT_OBJECT;
6220 }
6221
6222 return TRUE;
6223 }
6224
6225 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6226
6227 struct elf_gc_sweep_symbol_info
6228 {
6229 struct bfd_link_info *info;
6230 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6231 bfd_boolean);
6232 };
6233
6234 static bfd_boolean
6235 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6236 {
6237 if (!h->mark
6238 && (((h->root.type == bfd_link_hash_defined
6239 || h->root.type == bfd_link_hash_defweak)
6240 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6241 && h->root.u.def.section->gc_mark))
6242 || h->root.type == bfd_link_hash_undefined
6243 || h->root.type == bfd_link_hash_undefweak))
6244 {
6245 struct elf_gc_sweep_symbol_info *inf;
6246
6247 inf = (struct elf_gc_sweep_symbol_info *) data;
6248 (*inf->hide_symbol) (inf->info, h, TRUE);
6249 h->def_regular = 0;
6250 h->ref_regular = 0;
6251 h->ref_regular_nonweak = 0;
6252 }
6253
6254 return TRUE;
6255 }
6256
6257 /* Set up the sizes and contents of the ELF dynamic sections. This is
6258 called by the ELF linker emulation before_allocation routine. We
6259 must set the sizes of the sections before the linker sets the
6260 addresses of the various sections. */
6261
6262 bfd_boolean
6263 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6264 const char *soname,
6265 const char *rpath,
6266 const char *filter_shlib,
6267 const char *audit,
6268 const char *depaudit,
6269 const char * const *auxiliary_filters,
6270 struct bfd_link_info *info,
6271 asection **sinterpptr)
6272 {
6273 bfd *dynobj;
6274 const struct elf_backend_data *bed;
6275
6276 *sinterpptr = NULL;
6277
6278 if (!is_elf_hash_table (info->hash))
6279 return TRUE;
6280
6281 dynobj = elf_hash_table (info)->dynobj;
6282
6283 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6284 {
6285 struct bfd_elf_version_tree *verdefs;
6286 struct elf_info_failed asvinfo;
6287 struct bfd_elf_version_tree *t;
6288 struct bfd_elf_version_expr *d;
6289 asection *s;
6290 size_t soname_indx;
6291
6292 /* If we are supposed to export all symbols into the dynamic symbol
6293 table (this is not the normal case), then do so. */
6294 if (info->export_dynamic
6295 || (bfd_link_executable (info) && info->dynamic))
6296 {
6297 struct elf_info_failed eif;
6298
6299 eif.info = info;
6300 eif.failed = FALSE;
6301 elf_link_hash_traverse (elf_hash_table (info),
6302 _bfd_elf_export_symbol,
6303 &eif);
6304 if (eif.failed)
6305 return FALSE;
6306 }
6307
6308 if (soname != NULL)
6309 {
6310 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6311 soname, TRUE);
6312 if (soname_indx == (size_t) -1
6313 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6314 return FALSE;
6315 }
6316 else
6317 soname_indx = (size_t) -1;
6318
6319 /* Make all global versions with definition. */
6320 for (t = info->version_info; t != NULL; t = t->next)
6321 for (d = t->globals.list; d != NULL; d = d->next)
6322 if (!d->symver && d->literal)
6323 {
6324 const char *verstr, *name;
6325 size_t namelen, verlen, newlen;
6326 char *newname, *p, leading_char;
6327 struct elf_link_hash_entry *newh;
6328
6329 leading_char = bfd_get_symbol_leading_char (output_bfd);
6330 name = d->pattern;
6331 namelen = strlen (name) + (leading_char != '\0');
6332 verstr = t->name;
6333 verlen = strlen (verstr);
6334 newlen = namelen + verlen + 3;
6335
6336 newname = (char *) bfd_malloc (newlen);
6337 if (newname == NULL)
6338 return FALSE;
6339 newname[0] = leading_char;
6340 memcpy (newname + (leading_char != '\0'), name, namelen);
6341
6342 /* Check the hidden versioned definition. */
6343 p = newname + namelen;
6344 *p++ = ELF_VER_CHR;
6345 memcpy (p, verstr, verlen + 1);
6346 newh = elf_link_hash_lookup (elf_hash_table (info),
6347 newname, FALSE, FALSE,
6348 FALSE);
6349 if (newh == NULL
6350 || (newh->root.type != bfd_link_hash_defined
6351 && newh->root.type != bfd_link_hash_defweak))
6352 {
6353 /* Check the default versioned definition. */
6354 *p++ = ELF_VER_CHR;
6355 memcpy (p, verstr, verlen + 1);
6356 newh = elf_link_hash_lookup (elf_hash_table (info),
6357 newname, FALSE, FALSE,
6358 FALSE);
6359 }
6360 free (newname);
6361
6362 /* Mark this version if there is a definition and it is
6363 not defined in a shared object. */
6364 if (newh != NULL
6365 && !newh->def_dynamic
6366 && (newh->root.type == bfd_link_hash_defined
6367 || newh->root.type == bfd_link_hash_defweak))
6368 d->symver = 1;
6369 }
6370
6371 /* Attach all the symbols to their version information. */
6372 asvinfo.info = info;
6373 asvinfo.failed = FALSE;
6374
6375 elf_link_hash_traverse (elf_hash_table (info),
6376 _bfd_elf_link_assign_sym_version,
6377 &asvinfo);
6378 if (asvinfo.failed)
6379 return FALSE;
6380
6381 if (!info->allow_undefined_version)
6382 {
6383 /* Check if all global versions have a definition. */
6384 bfd_boolean all_defined = TRUE;
6385 for (t = info->version_info; t != NULL; t = t->next)
6386 for (d = t->globals.list; d != NULL; d = d->next)
6387 if (d->literal && !d->symver && !d->script)
6388 {
6389 _bfd_error_handler
6390 (_("%s: undefined version: %s"),
6391 d->pattern, t->name);
6392 all_defined = FALSE;
6393 }
6394
6395 if (!all_defined)
6396 {
6397 bfd_set_error (bfd_error_bad_value);
6398 return FALSE;
6399 }
6400 }
6401
6402 /* Set up the version definition section. */
6403 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6404 BFD_ASSERT (s != NULL);
6405
6406 /* We may have created additional version definitions if we are
6407 just linking a regular application. */
6408 verdefs = info->version_info;
6409
6410 /* Skip anonymous version tag. */
6411 if (verdefs != NULL && verdefs->vernum == 0)
6412 verdefs = verdefs->next;
6413
6414 if (verdefs == NULL && !info->create_default_symver)
6415 s->flags |= SEC_EXCLUDE;
6416 else
6417 {
6418 unsigned int cdefs;
6419 bfd_size_type size;
6420 bfd_byte *p;
6421 Elf_Internal_Verdef def;
6422 Elf_Internal_Verdaux defaux;
6423 struct bfd_link_hash_entry *bh;
6424 struct elf_link_hash_entry *h;
6425 const char *name;
6426
6427 cdefs = 0;
6428 size = 0;
6429
6430 /* Make space for the base version. */
6431 size += sizeof (Elf_External_Verdef);
6432 size += sizeof (Elf_External_Verdaux);
6433 ++cdefs;
6434
6435 /* Make space for the default version. */
6436 if (info->create_default_symver)
6437 {
6438 size += sizeof (Elf_External_Verdef);
6439 ++cdefs;
6440 }
6441
6442 for (t = verdefs; t != NULL; t = t->next)
6443 {
6444 struct bfd_elf_version_deps *n;
6445
6446 /* Don't emit base version twice. */
6447 if (t->vernum == 0)
6448 continue;
6449
6450 size += sizeof (Elf_External_Verdef);
6451 size += sizeof (Elf_External_Verdaux);
6452 ++cdefs;
6453
6454 for (n = t->deps; n != NULL; n = n->next)
6455 size += sizeof (Elf_External_Verdaux);
6456 }
6457
6458 s->size = size;
6459 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6460 if (s->contents == NULL && s->size != 0)
6461 return FALSE;
6462
6463 /* Fill in the version definition section. */
6464
6465 p = s->contents;
6466
6467 def.vd_version = VER_DEF_CURRENT;
6468 def.vd_flags = VER_FLG_BASE;
6469 def.vd_ndx = 1;
6470 def.vd_cnt = 1;
6471 if (info->create_default_symver)
6472 {
6473 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6474 def.vd_next = sizeof (Elf_External_Verdef);
6475 }
6476 else
6477 {
6478 def.vd_aux = sizeof (Elf_External_Verdef);
6479 def.vd_next = (sizeof (Elf_External_Verdef)
6480 + sizeof (Elf_External_Verdaux));
6481 }
6482
6483 if (soname_indx != (size_t) -1)
6484 {
6485 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6486 soname_indx);
6487 def.vd_hash = bfd_elf_hash (soname);
6488 defaux.vda_name = soname_indx;
6489 name = soname;
6490 }
6491 else
6492 {
6493 size_t indx;
6494
6495 name = lbasename (output_bfd->filename);
6496 def.vd_hash = bfd_elf_hash (name);
6497 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6498 name, FALSE);
6499 if (indx == (size_t) -1)
6500 return FALSE;
6501 defaux.vda_name = indx;
6502 }
6503 defaux.vda_next = 0;
6504
6505 _bfd_elf_swap_verdef_out (output_bfd, &def,
6506 (Elf_External_Verdef *) p);
6507 p += sizeof (Elf_External_Verdef);
6508 if (info->create_default_symver)
6509 {
6510 /* Add a symbol representing this version. */
6511 bh = NULL;
6512 if (! (_bfd_generic_link_add_one_symbol
6513 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6514 0, NULL, FALSE,
6515 get_elf_backend_data (dynobj)->collect, &bh)))
6516 return FALSE;
6517 h = (struct elf_link_hash_entry *) bh;
6518 h->non_elf = 0;
6519 h->def_regular = 1;
6520 h->type = STT_OBJECT;
6521 h->verinfo.vertree = NULL;
6522
6523 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6524 return FALSE;
6525
6526 /* Create a duplicate of the base version with the same
6527 aux block, but different flags. */
6528 def.vd_flags = 0;
6529 def.vd_ndx = 2;
6530 def.vd_aux = sizeof (Elf_External_Verdef);
6531 if (verdefs)
6532 def.vd_next = (sizeof (Elf_External_Verdef)
6533 + sizeof (Elf_External_Verdaux));
6534 else
6535 def.vd_next = 0;
6536 _bfd_elf_swap_verdef_out (output_bfd, &def,
6537 (Elf_External_Verdef *) p);
6538 p += sizeof (Elf_External_Verdef);
6539 }
6540 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6541 (Elf_External_Verdaux *) p);
6542 p += sizeof (Elf_External_Verdaux);
6543
6544 for (t = verdefs; t != NULL; t = t->next)
6545 {
6546 unsigned int cdeps;
6547 struct bfd_elf_version_deps *n;
6548
6549 /* Don't emit the base version twice. */
6550 if (t->vernum == 0)
6551 continue;
6552
6553 cdeps = 0;
6554 for (n = t->deps; n != NULL; n = n->next)
6555 ++cdeps;
6556
6557 /* Add a symbol representing this version. */
6558 bh = NULL;
6559 if (! (_bfd_generic_link_add_one_symbol
6560 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6561 0, NULL, FALSE,
6562 get_elf_backend_data (dynobj)->collect, &bh)))
6563 return FALSE;
6564 h = (struct elf_link_hash_entry *) bh;
6565 h->non_elf = 0;
6566 h->def_regular = 1;
6567 h->type = STT_OBJECT;
6568 h->verinfo.vertree = t;
6569
6570 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6571 return FALSE;
6572
6573 def.vd_version = VER_DEF_CURRENT;
6574 def.vd_flags = 0;
6575 if (t->globals.list == NULL
6576 && t->locals.list == NULL
6577 && ! t->used)
6578 def.vd_flags |= VER_FLG_WEAK;
6579 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6580 def.vd_cnt = cdeps + 1;
6581 def.vd_hash = bfd_elf_hash (t->name);
6582 def.vd_aux = sizeof (Elf_External_Verdef);
6583 def.vd_next = 0;
6584
6585 /* If a basever node is next, it *must* be the last node in
6586 the chain, otherwise Verdef construction breaks. */
6587 if (t->next != NULL && t->next->vernum == 0)
6588 BFD_ASSERT (t->next->next == NULL);
6589
6590 if (t->next != NULL && t->next->vernum != 0)
6591 def.vd_next = (sizeof (Elf_External_Verdef)
6592 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6593
6594 _bfd_elf_swap_verdef_out (output_bfd, &def,
6595 (Elf_External_Verdef *) p);
6596 p += sizeof (Elf_External_Verdef);
6597
6598 defaux.vda_name = h->dynstr_index;
6599 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6600 h->dynstr_index);
6601 defaux.vda_next = 0;
6602 if (t->deps != NULL)
6603 defaux.vda_next = sizeof (Elf_External_Verdaux);
6604 t->name_indx = defaux.vda_name;
6605
6606 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6607 (Elf_External_Verdaux *) p);
6608 p += sizeof (Elf_External_Verdaux);
6609
6610 for (n = t->deps; n != NULL; n = n->next)
6611 {
6612 if (n->version_needed == NULL)
6613 {
6614 /* This can happen if there was an error in the
6615 version script. */
6616 defaux.vda_name = 0;
6617 }
6618 else
6619 {
6620 defaux.vda_name = n->version_needed->name_indx;
6621 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6622 defaux.vda_name);
6623 }
6624 if (n->next == NULL)
6625 defaux.vda_next = 0;
6626 else
6627 defaux.vda_next = sizeof (Elf_External_Verdaux);
6628
6629 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6630 (Elf_External_Verdaux *) p);
6631 p += sizeof (Elf_External_Verdaux);
6632 }
6633 }
6634
6635 elf_tdata (output_bfd)->cverdefs = cdefs;
6636 }
6637 }
6638
6639 bed = get_elf_backend_data (output_bfd);
6640
6641 if (info->gc_sections && bed->can_gc_sections)
6642 {
6643 struct elf_gc_sweep_symbol_info sweep_info;
6644
6645 /* Remove the symbols that were in the swept sections from the
6646 dynamic symbol table. */
6647 sweep_info.info = info;
6648 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6649 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6650 &sweep_info);
6651 }
6652
6653 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6654 {
6655 asection *s;
6656 struct elf_find_verdep_info sinfo;
6657
6658 /* Work out the size of the version reference section. */
6659
6660 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6661 BFD_ASSERT (s != NULL);
6662
6663 sinfo.info = info;
6664 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6665 if (sinfo.vers == 0)
6666 sinfo.vers = 1;
6667 sinfo.failed = FALSE;
6668
6669 elf_link_hash_traverse (elf_hash_table (info),
6670 _bfd_elf_link_find_version_dependencies,
6671 &sinfo);
6672 if (sinfo.failed)
6673 return FALSE;
6674
6675 if (elf_tdata (output_bfd)->verref == NULL)
6676 s->flags |= SEC_EXCLUDE;
6677 else
6678 {
6679 Elf_Internal_Verneed *vn;
6680 unsigned int size;
6681 unsigned int crefs;
6682 bfd_byte *p;
6683
6684 /* Build the version dependency section. */
6685 size = 0;
6686 crefs = 0;
6687 for (vn = elf_tdata (output_bfd)->verref;
6688 vn != NULL;
6689 vn = vn->vn_nextref)
6690 {
6691 Elf_Internal_Vernaux *a;
6692
6693 size += sizeof (Elf_External_Verneed);
6694 ++crefs;
6695 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6696 size += sizeof (Elf_External_Vernaux);
6697 }
6698
6699 s->size = size;
6700 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6701 if (s->contents == NULL)
6702 return FALSE;
6703
6704 p = s->contents;
6705 for (vn = elf_tdata (output_bfd)->verref;
6706 vn != NULL;
6707 vn = vn->vn_nextref)
6708 {
6709 unsigned int caux;
6710 Elf_Internal_Vernaux *a;
6711 size_t indx;
6712
6713 caux = 0;
6714 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6715 ++caux;
6716
6717 vn->vn_version = VER_NEED_CURRENT;
6718 vn->vn_cnt = caux;
6719 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6720 elf_dt_name (vn->vn_bfd) != NULL
6721 ? elf_dt_name (vn->vn_bfd)
6722 : lbasename (vn->vn_bfd->filename),
6723 FALSE);
6724 if (indx == (size_t) -1)
6725 return FALSE;
6726 vn->vn_file = indx;
6727 vn->vn_aux = sizeof (Elf_External_Verneed);
6728 if (vn->vn_nextref == NULL)
6729 vn->vn_next = 0;
6730 else
6731 vn->vn_next = (sizeof (Elf_External_Verneed)
6732 + caux * sizeof (Elf_External_Vernaux));
6733
6734 _bfd_elf_swap_verneed_out (output_bfd, vn,
6735 (Elf_External_Verneed *) p);
6736 p += sizeof (Elf_External_Verneed);
6737
6738 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6739 {
6740 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6741 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6742 a->vna_nodename, FALSE);
6743 if (indx == (size_t) -1)
6744 return FALSE;
6745 a->vna_name = indx;
6746 if (a->vna_nextptr == NULL)
6747 a->vna_next = 0;
6748 else
6749 a->vna_next = sizeof (Elf_External_Vernaux);
6750
6751 _bfd_elf_swap_vernaux_out (output_bfd, a,
6752 (Elf_External_Vernaux *) p);
6753 p += sizeof (Elf_External_Vernaux);
6754 }
6755 }
6756
6757 elf_tdata (output_bfd)->cverrefs = crefs;
6758 }
6759 }
6760
6761 /* Any syms created from now on start with -1 in
6762 got.refcount/offset and plt.refcount/offset. */
6763 elf_hash_table (info)->init_got_refcount
6764 = elf_hash_table (info)->init_got_offset;
6765 elf_hash_table (info)->init_plt_refcount
6766 = elf_hash_table (info)->init_plt_offset;
6767
6768 if (bfd_link_relocatable (info)
6769 && !_bfd_elf_size_group_sections (info))
6770 return FALSE;
6771
6772 /* The backend may have to create some sections regardless of whether
6773 we're dynamic or not. */
6774 if (bed->elf_backend_always_size_sections
6775 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6776 return FALSE;
6777
6778 /* Determine any GNU_STACK segment requirements, after the backend
6779 has had a chance to set a default segment size. */
6780 if (info->execstack)
6781 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6782 else if (info->noexecstack)
6783 elf_stack_flags (output_bfd) = PF_R | PF_W;
6784 else
6785 {
6786 bfd *inputobj;
6787 asection *notesec = NULL;
6788 int exec = 0;
6789
6790 for (inputobj = info->input_bfds;
6791 inputobj;
6792 inputobj = inputobj->link.next)
6793 {
6794 asection *s;
6795
6796 if (inputobj->flags
6797 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6798 continue;
6799 s = inputobj->sections;
6800 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6801 continue;
6802
6803 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6804 if (s)
6805 {
6806 if (s->flags & SEC_CODE)
6807 exec = PF_X;
6808 notesec = s;
6809 }
6810 else if (bed->default_execstack)
6811 exec = PF_X;
6812 }
6813 if (notesec || info->stacksize > 0)
6814 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6815 if (notesec && exec && bfd_link_relocatable (info)
6816 && notesec->output_section != bfd_abs_section_ptr)
6817 notesec->output_section->flags |= SEC_CODE;
6818 }
6819
6820 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6821 {
6822 struct elf_info_failed eif;
6823 struct elf_link_hash_entry *h;
6824 asection *dynstr;
6825 asection *s;
6826
6827 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6828 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6829
6830 if (info->symbolic)
6831 {
6832 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6833 return FALSE;
6834 info->flags |= DF_SYMBOLIC;
6835 }
6836
6837 if (rpath != NULL)
6838 {
6839 size_t indx;
6840 bfd_vma tag;
6841
6842 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6843 TRUE);
6844 if (indx == (size_t) -1)
6845 return FALSE;
6846
6847 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6848 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6849 return FALSE;
6850 }
6851
6852 if (filter_shlib != NULL)
6853 {
6854 size_t indx;
6855
6856 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6857 filter_shlib, TRUE);
6858 if (indx == (size_t) -1
6859 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6860 return FALSE;
6861 }
6862
6863 if (auxiliary_filters != NULL)
6864 {
6865 const char * const *p;
6866
6867 for (p = auxiliary_filters; *p != NULL; p++)
6868 {
6869 size_t indx;
6870
6871 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6872 *p, TRUE);
6873 if (indx == (size_t) -1
6874 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6875 return FALSE;
6876 }
6877 }
6878
6879 if (audit != NULL)
6880 {
6881 size_t indx;
6882
6883 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6884 TRUE);
6885 if (indx == (size_t) -1
6886 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6887 return FALSE;
6888 }
6889
6890 if (depaudit != NULL)
6891 {
6892 size_t indx;
6893
6894 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6895 TRUE);
6896 if (indx == (size_t) -1
6897 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6898 return FALSE;
6899 }
6900
6901 eif.info = info;
6902 eif.failed = FALSE;
6903
6904 /* Find all symbols which were defined in a dynamic object and make
6905 the backend pick a reasonable value for them. */
6906 elf_link_hash_traverse (elf_hash_table (info),
6907 _bfd_elf_adjust_dynamic_symbol,
6908 &eif);
6909 if (eif.failed)
6910 return FALSE;
6911
6912 /* Add some entries to the .dynamic section. We fill in some of the
6913 values later, in bfd_elf_final_link, but we must add the entries
6914 now so that we know the final size of the .dynamic section. */
6915
6916 /* If there are initialization and/or finalization functions to
6917 call then add the corresponding DT_INIT/DT_FINI entries. */
6918 h = (info->init_function
6919 ? elf_link_hash_lookup (elf_hash_table (info),
6920 info->init_function, FALSE,
6921 FALSE, FALSE)
6922 : NULL);
6923 if (h != NULL
6924 && (h->ref_regular
6925 || h->def_regular))
6926 {
6927 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6928 return FALSE;
6929 }
6930 h = (info->fini_function
6931 ? elf_link_hash_lookup (elf_hash_table (info),
6932 info->fini_function, FALSE,
6933 FALSE, FALSE)
6934 : NULL);
6935 if (h != NULL
6936 && (h->ref_regular
6937 || h->def_regular))
6938 {
6939 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6940 return FALSE;
6941 }
6942
6943 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6944 if (s != NULL && s->linker_has_input)
6945 {
6946 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6947 if (! bfd_link_executable (info))
6948 {
6949 bfd *sub;
6950 asection *o;
6951
6952 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6953 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
6954 && (o = sub->sections) != NULL
6955 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
6956 for (o = sub->sections; o != NULL; o = o->next)
6957 if (elf_section_data (o)->this_hdr.sh_type
6958 == SHT_PREINIT_ARRAY)
6959 {
6960 _bfd_error_handler
6961 (_("%pB: .preinit_array section is not allowed in DSO"),
6962 sub);
6963 break;
6964 }
6965
6966 bfd_set_error (bfd_error_nonrepresentable_section);
6967 return FALSE;
6968 }
6969
6970 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6971 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6972 return FALSE;
6973 }
6974 s = bfd_get_section_by_name (output_bfd, ".init_array");
6975 if (s != NULL && s->linker_has_input)
6976 {
6977 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6978 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6979 return FALSE;
6980 }
6981 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6982 if (s != NULL && s->linker_has_input)
6983 {
6984 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6985 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6986 return FALSE;
6987 }
6988
6989 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6990 /* If .dynstr is excluded from the link, we don't want any of
6991 these tags. Strictly, we should be checking each section
6992 individually; This quick check covers for the case where
6993 someone does a /DISCARD/ : { *(*) }. */
6994 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6995 {
6996 bfd_size_type strsize;
6997
6998 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6999 if ((info->emit_hash
7000 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7001 || (info->emit_gnu_hash
7002 && (bed->record_xhash_symbol == NULL
7003 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7004 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7005 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7006 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7007 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7008 bed->s->sizeof_sym))
7009 return FALSE;
7010 }
7011 }
7012
7013 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7014 return FALSE;
7015
7016 /* The backend must work out the sizes of all the other dynamic
7017 sections. */
7018 if (dynobj != NULL
7019 && bed->elf_backend_size_dynamic_sections != NULL
7020 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
7021 return FALSE;
7022
7023 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7024 {
7025 if (elf_tdata (output_bfd)->cverdefs)
7026 {
7027 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7028
7029 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7030 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7031 return FALSE;
7032 }
7033
7034 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7035 {
7036 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7037 return FALSE;
7038 }
7039 else if (info->flags & DF_BIND_NOW)
7040 {
7041 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7042 return FALSE;
7043 }
7044
7045 if (info->flags_1)
7046 {
7047 if (bfd_link_executable (info))
7048 info->flags_1 &= ~ (DF_1_INITFIRST
7049 | DF_1_NODELETE
7050 | DF_1_NOOPEN);
7051 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7052 return FALSE;
7053 }
7054
7055 if (elf_tdata (output_bfd)->cverrefs)
7056 {
7057 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7058
7059 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7060 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7061 return FALSE;
7062 }
7063
7064 if ((elf_tdata (output_bfd)->cverrefs == 0
7065 && elf_tdata (output_bfd)->cverdefs == 0)
7066 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7067 {
7068 asection *s;
7069
7070 s = bfd_get_linker_section (dynobj, ".gnu.version");
7071 s->flags |= SEC_EXCLUDE;
7072 }
7073 }
7074 return TRUE;
7075 }
7076
7077 /* Find the first non-excluded output section. We'll use its
7078 section symbol for some emitted relocs. */
7079 void
7080 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7081 {
7082 asection *s;
7083 asection *found = NULL;
7084
7085 for (s = output_bfd->sections; s != NULL; s = s->next)
7086 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7087 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7088 {
7089 found = s;
7090 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7091 break;
7092 }
7093 elf_hash_table (info)->text_index_section = found;
7094 }
7095
7096 /* Find two non-excluded output sections, one for code, one for data.
7097 We'll use their section symbols for some emitted relocs. */
7098 void
7099 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7100 {
7101 asection *s;
7102 asection *found = NULL;
7103
7104 /* Data first, since setting text_index_section changes
7105 _bfd_elf_omit_section_dynsym_default. */
7106 for (s = output_bfd->sections; s != NULL; s = s->next)
7107 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7108 && !(s->flags & SEC_READONLY)
7109 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7110 {
7111 found = s;
7112 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7113 break;
7114 }
7115 elf_hash_table (info)->data_index_section = found;
7116
7117 for (s = output_bfd->sections; s != NULL; s = s->next)
7118 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7119 && (s->flags & SEC_READONLY)
7120 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7121 {
7122 found = s;
7123 break;
7124 }
7125 elf_hash_table (info)->text_index_section = found;
7126 }
7127
7128 #define GNU_HASH_SECTION_NAME(bed) \
7129 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7130
7131 bfd_boolean
7132 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7133 {
7134 const struct elf_backend_data *bed;
7135 unsigned long section_sym_count;
7136 bfd_size_type dynsymcount = 0;
7137
7138 if (!is_elf_hash_table (info->hash))
7139 return TRUE;
7140
7141 bed = get_elf_backend_data (output_bfd);
7142 (*bed->elf_backend_init_index_section) (output_bfd, info);
7143
7144 /* Assign dynsym indices. In a shared library we generate a section
7145 symbol for each output section, which come first. Next come all
7146 of the back-end allocated local dynamic syms, followed by the rest
7147 of the global symbols.
7148
7149 This is usually not needed for static binaries, however backends
7150 can request to always do it, e.g. the MIPS backend uses dynamic
7151 symbol counts to lay out GOT, which will be produced in the
7152 presence of GOT relocations even in static binaries (holding fixed
7153 data in that case, to satisfy those relocations). */
7154
7155 if (elf_hash_table (info)->dynamic_sections_created
7156 || bed->always_renumber_dynsyms)
7157 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7158 &section_sym_count);
7159
7160 if (elf_hash_table (info)->dynamic_sections_created)
7161 {
7162 bfd *dynobj;
7163 asection *s;
7164 unsigned int dtagcount;
7165
7166 dynobj = elf_hash_table (info)->dynobj;
7167
7168 /* Work out the size of the symbol version section. */
7169 s = bfd_get_linker_section (dynobj, ".gnu.version");
7170 BFD_ASSERT (s != NULL);
7171 if ((s->flags & SEC_EXCLUDE) == 0)
7172 {
7173 s->size = dynsymcount * sizeof (Elf_External_Versym);
7174 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7175 if (s->contents == NULL)
7176 return FALSE;
7177
7178 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7179 return FALSE;
7180 }
7181
7182 /* Set the size of the .dynsym and .hash sections. We counted
7183 the number of dynamic symbols in elf_link_add_object_symbols.
7184 We will build the contents of .dynsym and .hash when we build
7185 the final symbol table, because until then we do not know the
7186 correct value to give the symbols. We built the .dynstr
7187 section as we went along in elf_link_add_object_symbols. */
7188 s = elf_hash_table (info)->dynsym;
7189 BFD_ASSERT (s != NULL);
7190 s->size = dynsymcount * bed->s->sizeof_sym;
7191
7192 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7193 if (s->contents == NULL)
7194 return FALSE;
7195
7196 /* The first entry in .dynsym is a dummy symbol. Clear all the
7197 section syms, in case we don't output them all. */
7198 ++section_sym_count;
7199 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7200
7201 elf_hash_table (info)->bucketcount = 0;
7202
7203 /* Compute the size of the hashing table. As a side effect this
7204 computes the hash values for all the names we export. */
7205 if (info->emit_hash)
7206 {
7207 unsigned long int *hashcodes;
7208 struct hash_codes_info hashinf;
7209 bfd_size_type amt;
7210 unsigned long int nsyms;
7211 size_t bucketcount;
7212 size_t hash_entry_size;
7213
7214 /* Compute the hash values for all exported symbols. At the same
7215 time store the values in an array so that we could use them for
7216 optimizations. */
7217 amt = dynsymcount * sizeof (unsigned long int);
7218 hashcodes = (unsigned long int *) bfd_malloc (amt);
7219 if (hashcodes == NULL)
7220 return FALSE;
7221 hashinf.hashcodes = hashcodes;
7222 hashinf.error = FALSE;
7223
7224 /* Put all hash values in HASHCODES. */
7225 elf_link_hash_traverse (elf_hash_table (info),
7226 elf_collect_hash_codes, &hashinf);
7227 if (hashinf.error)
7228 {
7229 free (hashcodes);
7230 return FALSE;
7231 }
7232
7233 nsyms = hashinf.hashcodes - hashcodes;
7234 bucketcount
7235 = compute_bucket_count (info, hashcodes, nsyms, 0);
7236 free (hashcodes);
7237
7238 if (bucketcount == 0 && nsyms > 0)
7239 return FALSE;
7240
7241 elf_hash_table (info)->bucketcount = bucketcount;
7242
7243 s = bfd_get_linker_section (dynobj, ".hash");
7244 BFD_ASSERT (s != NULL);
7245 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7246 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7247 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7248 if (s->contents == NULL)
7249 return FALSE;
7250
7251 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7252 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7253 s->contents + hash_entry_size);
7254 }
7255
7256 if (info->emit_gnu_hash)
7257 {
7258 size_t i, cnt;
7259 unsigned char *contents;
7260 struct collect_gnu_hash_codes cinfo;
7261 bfd_size_type amt;
7262 size_t bucketcount;
7263
7264 memset (&cinfo, 0, sizeof (cinfo));
7265
7266 /* Compute the hash values for all exported symbols. At the same
7267 time store the values in an array so that we could use them for
7268 optimizations. */
7269 amt = dynsymcount * 2 * sizeof (unsigned long int);
7270 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7271 if (cinfo.hashcodes == NULL)
7272 return FALSE;
7273
7274 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7275 cinfo.min_dynindx = -1;
7276 cinfo.output_bfd = output_bfd;
7277 cinfo.bed = bed;
7278
7279 /* Put all hash values in HASHCODES. */
7280 elf_link_hash_traverse (elf_hash_table (info),
7281 elf_collect_gnu_hash_codes, &cinfo);
7282 if (cinfo.error)
7283 {
7284 free (cinfo.hashcodes);
7285 return FALSE;
7286 }
7287
7288 bucketcount
7289 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7290
7291 if (bucketcount == 0)
7292 {
7293 free (cinfo.hashcodes);
7294 return FALSE;
7295 }
7296
7297 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
7298 BFD_ASSERT (s != NULL);
7299
7300 if (cinfo.nsyms == 0)
7301 {
7302 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7303 BFD_ASSERT (cinfo.min_dynindx == -1);
7304 free (cinfo.hashcodes);
7305 s->size = 5 * 4 + bed->s->arch_size / 8;
7306 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7307 if (contents == NULL)
7308 return FALSE;
7309 s->contents = contents;
7310 /* 1 empty bucket. */
7311 bfd_put_32 (output_bfd, 1, contents);
7312 /* SYMIDX above the special symbol 0. */
7313 bfd_put_32 (output_bfd, 1, contents + 4);
7314 /* Just one word for bitmask. */
7315 bfd_put_32 (output_bfd, 1, contents + 8);
7316 /* Only hash fn bloom filter. */
7317 bfd_put_32 (output_bfd, 0, contents + 12);
7318 /* No hashes are valid - empty bitmask. */
7319 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7320 /* No hashes in the only bucket. */
7321 bfd_put_32 (output_bfd, 0,
7322 contents + 16 + bed->s->arch_size / 8);
7323 }
7324 else
7325 {
7326 unsigned long int maskwords, maskbitslog2, x;
7327 BFD_ASSERT (cinfo.min_dynindx != -1);
7328
7329 x = cinfo.nsyms;
7330 maskbitslog2 = 1;
7331 while ((x >>= 1) != 0)
7332 ++maskbitslog2;
7333 if (maskbitslog2 < 3)
7334 maskbitslog2 = 5;
7335 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7336 maskbitslog2 = maskbitslog2 + 3;
7337 else
7338 maskbitslog2 = maskbitslog2 + 2;
7339 if (bed->s->arch_size == 64)
7340 {
7341 if (maskbitslog2 == 5)
7342 maskbitslog2 = 6;
7343 cinfo.shift1 = 6;
7344 }
7345 else
7346 cinfo.shift1 = 5;
7347 cinfo.mask = (1 << cinfo.shift1) - 1;
7348 cinfo.shift2 = maskbitslog2;
7349 cinfo.maskbits = 1 << maskbitslog2;
7350 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7351 amt = bucketcount * sizeof (unsigned long int) * 2;
7352 amt += maskwords * sizeof (bfd_vma);
7353 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7354 if (cinfo.bitmask == NULL)
7355 {
7356 free (cinfo.hashcodes);
7357 return FALSE;
7358 }
7359
7360 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7361 cinfo.indx = cinfo.counts + bucketcount;
7362 cinfo.symindx = dynsymcount - cinfo.nsyms;
7363 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7364
7365 /* Determine how often each hash bucket is used. */
7366 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7367 for (i = 0; i < cinfo.nsyms; ++i)
7368 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7369
7370 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7371 if (cinfo.counts[i] != 0)
7372 {
7373 cinfo.indx[i] = cnt;
7374 cnt += cinfo.counts[i];
7375 }
7376 BFD_ASSERT (cnt == dynsymcount);
7377 cinfo.bucketcount = bucketcount;
7378 cinfo.local_indx = cinfo.min_dynindx;
7379
7380 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7381 s->size += cinfo.maskbits / 8;
7382 if (bed->record_xhash_symbol != NULL)
7383 s->size += cinfo.nsyms * 4;
7384 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7385 if (contents == NULL)
7386 {
7387 free (cinfo.bitmask);
7388 free (cinfo.hashcodes);
7389 return FALSE;
7390 }
7391
7392 s->contents = contents;
7393 bfd_put_32 (output_bfd, bucketcount, contents);
7394 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7395 bfd_put_32 (output_bfd, maskwords, contents + 8);
7396 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7397 contents += 16 + cinfo.maskbits / 8;
7398
7399 for (i = 0; i < bucketcount; ++i)
7400 {
7401 if (cinfo.counts[i] == 0)
7402 bfd_put_32 (output_bfd, 0, contents);
7403 else
7404 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7405 contents += 4;
7406 }
7407
7408 cinfo.contents = contents;
7409
7410 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7411 /* Renumber dynamic symbols, if populating .gnu.hash section.
7412 If using .MIPS.xhash, populate the translation table. */
7413 elf_link_hash_traverse (elf_hash_table (info),
7414 elf_gnu_hash_process_symidx, &cinfo);
7415
7416 contents = s->contents + 16;
7417 for (i = 0; i < maskwords; ++i)
7418 {
7419 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7420 contents);
7421 contents += bed->s->arch_size / 8;
7422 }
7423
7424 free (cinfo.bitmask);
7425 free (cinfo.hashcodes);
7426 }
7427 }
7428
7429 s = bfd_get_linker_section (dynobj, ".dynstr");
7430 BFD_ASSERT (s != NULL);
7431
7432 elf_finalize_dynstr (output_bfd, info);
7433
7434 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7435
7436 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7437 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7438 return FALSE;
7439 }
7440
7441 return TRUE;
7442 }
7443 \f
7444 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7445
7446 static void
7447 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7448 asection *sec)
7449 {
7450 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7451 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7452 }
7453
7454 /* Finish SHF_MERGE section merging. */
7455
7456 bfd_boolean
7457 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7458 {
7459 bfd *ibfd;
7460 asection *sec;
7461
7462 if (!is_elf_hash_table (info->hash))
7463 return FALSE;
7464
7465 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7466 if ((ibfd->flags & DYNAMIC) == 0
7467 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7468 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7469 == get_elf_backend_data (obfd)->s->elfclass))
7470 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7471 if ((sec->flags & SEC_MERGE) != 0
7472 && !bfd_is_abs_section (sec->output_section))
7473 {
7474 struct bfd_elf_section_data *secdata;
7475
7476 secdata = elf_section_data (sec);
7477 if (! _bfd_add_merge_section (obfd,
7478 &elf_hash_table (info)->merge_info,
7479 sec, &secdata->sec_info))
7480 return FALSE;
7481 else if (secdata->sec_info)
7482 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7483 }
7484
7485 if (elf_hash_table (info)->merge_info != NULL)
7486 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7487 merge_sections_remove_hook);
7488 return TRUE;
7489 }
7490
7491 /* Create an entry in an ELF linker hash table. */
7492
7493 struct bfd_hash_entry *
7494 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7495 struct bfd_hash_table *table,
7496 const char *string)
7497 {
7498 /* Allocate the structure if it has not already been allocated by a
7499 subclass. */
7500 if (entry == NULL)
7501 {
7502 entry = (struct bfd_hash_entry *)
7503 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7504 if (entry == NULL)
7505 return entry;
7506 }
7507
7508 /* Call the allocation method of the superclass. */
7509 entry = _bfd_link_hash_newfunc (entry, table, string);
7510 if (entry != NULL)
7511 {
7512 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7513 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7514
7515 /* Set local fields. */
7516 ret->indx = -1;
7517 ret->dynindx = -1;
7518 ret->got = htab->init_got_refcount;
7519 ret->plt = htab->init_plt_refcount;
7520 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7521 - offsetof (struct elf_link_hash_entry, size)));
7522 /* Assume that we have been called by a non-ELF symbol reader.
7523 This flag is then reset by the code which reads an ELF input
7524 file. This ensures that a symbol created by a non-ELF symbol
7525 reader will have the flag set correctly. */
7526 ret->non_elf = 1;
7527 }
7528
7529 return entry;
7530 }
7531
7532 /* Copy data from an indirect symbol to its direct symbol, hiding the
7533 old indirect symbol. Also used for copying flags to a weakdef. */
7534
7535 void
7536 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7537 struct elf_link_hash_entry *dir,
7538 struct elf_link_hash_entry *ind)
7539 {
7540 struct elf_link_hash_table *htab;
7541
7542 /* Copy down any references that we may have already seen to the
7543 symbol which just became indirect. */
7544
7545 if (dir->versioned != versioned_hidden)
7546 dir->ref_dynamic |= ind->ref_dynamic;
7547 dir->ref_regular |= ind->ref_regular;
7548 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7549 dir->non_got_ref |= ind->non_got_ref;
7550 dir->needs_plt |= ind->needs_plt;
7551 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7552
7553 if (ind->root.type != bfd_link_hash_indirect)
7554 return;
7555
7556 /* Copy over the global and procedure linkage table refcount entries.
7557 These may have been already set up by a check_relocs routine. */
7558 htab = elf_hash_table (info);
7559 if (ind->got.refcount > htab->init_got_refcount.refcount)
7560 {
7561 if (dir->got.refcount < 0)
7562 dir->got.refcount = 0;
7563 dir->got.refcount += ind->got.refcount;
7564 ind->got.refcount = htab->init_got_refcount.refcount;
7565 }
7566
7567 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7568 {
7569 if (dir->plt.refcount < 0)
7570 dir->plt.refcount = 0;
7571 dir->plt.refcount += ind->plt.refcount;
7572 ind->plt.refcount = htab->init_plt_refcount.refcount;
7573 }
7574
7575 if (ind->dynindx != -1)
7576 {
7577 if (dir->dynindx != -1)
7578 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7579 dir->dynindx = ind->dynindx;
7580 dir->dynstr_index = ind->dynstr_index;
7581 ind->dynindx = -1;
7582 ind->dynstr_index = 0;
7583 }
7584 }
7585
7586 void
7587 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7588 struct elf_link_hash_entry *h,
7589 bfd_boolean force_local)
7590 {
7591 /* STT_GNU_IFUNC symbol must go through PLT. */
7592 if (h->type != STT_GNU_IFUNC)
7593 {
7594 h->plt = elf_hash_table (info)->init_plt_offset;
7595 h->needs_plt = 0;
7596 }
7597 if (force_local)
7598 {
7599 h->forced_local = 1;
7600 if (h->dynindx != -1)
7601 {
7602 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7603 h->dynstr_index);
7604 h->dynindx = -1;
7605 h->dynstr_index = 0;
7606 }
7607 }
7608 }
7609
7610 /* Hide a symbol. */
7611
7612 void
7613 _bfd_elf_link_hide_symbol (bfd *output_bfd,
7614 struct bfd_link_info *info,
7615 struct bfd_link_hash_entry *h)
7616 {
7617 if (is_elf_hash_table (info->hash))
7618 {
7619 const struct elf_backend_data *bed
7620 = get_elf_backend_data (output_bfd);
7621 struct elf_link_hash_entry *eh
7622 = (struct elf_link_hash_entry *) h;
7623 bed->elf_backend_hide_symbol (info, eh, TRUE);
7624 eh->def_dynamic = 0;
7625 eh->ref_dynamic = 0;
7626 eh->dynamic_def = 0;
7627 }
7628 }
7629
7630 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7631 caller. */
7632
7633 bfd_boolean
7634 _bfd_elf_link_hash_table_init
7635 (struct elf_link_hash_table *table,
7636 bfd *abfd,
7637 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7638 struct bfd_hash_table *,
7639 const char *),
7640 unsigned int entsize,
7641 enum elf_target_id target_id)
7642 {
7643 bfd_boolean ret;
7644 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7645
7646 table->init_got_refcount.refcount = can_refcount - 1;
7647 table->init_plt_refcount.refcount = can_refcount - 1;
7648 table->init_got_offset.offset = -(bfd_vma) 1;
7649 table->init_plt_offset.offset = -(bfd_vma) 1;
7650 /* The first dynamic symbol is a dummy. */
7651 table->dynsymcount = 1;
7652
7653 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7654
7655 table->root.type = bfd_link_elf_hash_table;
7656 table->hash_table_id = target_id;
7657
7658 return ret;
7659 }
7660
7661 /* Create an ELF linker hash table. */
7662
7663 struct bfd_link_hash_table *
7664 _bfd_elf_link_hash_table_create (bfd *abfd)
7665 {
7666 struct elf_link_hash_table *ret;
7667 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7668
7669 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7670 if (ret == NULL)
7671 return NULL;
7672
7673 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7674 sizeof (struct elf_link_hash_entry),
7675 GENERIC_ELF_DATA))
7676 {
7677 free (ret);
7678 return NULL;
7679 }
7680 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7681
7682 return &ret->root;
7683 }
7684
7685 /* Destroy an ELF linker hash table. */
7686
7687 void
7688 _bfd_elf_link_hash_table_free (bfd *obfd)
7689 {
7690 struct elf_link_hash_table *htab;
7691
7692 htab = (struct elf_link_hash_table *) obfd->link.hash;
7693 if (htab->dynstr != NULL)
7694 _bfd_elf_strtab_free (htab->dynstr);
7695 _bfd_merge_sections_free (htab->merge_info);
7696 _bfd_generic_link_hash_table_free (obfd);
7697 }
7698
7699 /* This is a hook for the ELF emulation code in the generic linker to
7700 tell the backend linker what file name to use for the DT_NEEDED
7701 entry for a dynamic object. */
7702
7703 void
7704 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7705 {
7706 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7707 && bfd_get_format (abfd) == bfd_object)
7708 elf_dt_name (abfd) = name;
7709 }
7710
7711 int
7712 bfd_elf_get_dyn_lib_class (bfd *abfd)
7713 {
7714 int lib_class;
7715 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7716 && bfd_get_format (abfd) == bfd_object)
7717 lib_class = elf_dyn_lib_class (abfd);
7718 else
7719 lib_class = 0;
7720 return lib_class;
7721 }
7722
7723 void
7724 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7725 {
7726 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7727 && bfd_get_format (abfd) == bfd_object)
7728 elf_dyn_lib_class (abfd) = lib_class;
7729 }
7730
7731 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7732 the linker ELF emulation code. */
7733
7734 struct bfd_link_needed_list *
7735 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7736 struct bfd_link_info *info)
7737 {
7738 if (! is_elf_hash_table (info->hash))
7739 return NULL;
7740 return elf_hash_table (info)->needed;
7741 }
7742
7743 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7744 hook for the linker ELF emulation code. */
7745
7746 struct bfd_link_needed_list *
7747 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7748 struct bfd_link_info *info)
7749 {
7750 if (! is_elf_hash_table (info->hash))
7751 return NULL;
7752 return elf_hash_table (info)->runpath;
7753 }
7754
7755 /* Get the name actually used for a dynamic object for a link. This
7756 is the SONAME entry if there is one. Otherwise, it is the string
7757 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7758
7759 const char *
7760 bfd_elf_get_dt_soname (bfd *abfd)
7761 {
7762 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7763 && bfd_get_format (abfd) == bfd_object)
7764 return elf_dt_name (abfd);
7765 return NULL;
7766 }
7767
7768 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7769 the ELF linker emulation code. */
7770
7771 bfd_boolean
7772 bfd_elf_get_bfd_needed_list (bfd *abfd,
7773 struct bfd_link_needed_list **pneeded)
7774 {
7775 asection *s;
7776 bfd_byte *dynbuf = NULL;
7777 unsigned int elfsec;
7778 unsigned long shlink;
7779 bfd_byte *extdyn, *extdynend;
7780 size_t extdynsize;
7781 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7782
7783 *pneeded = NULL;
7784
7785 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7786 || bfd_get_format (abfd) != bfd_object)
7787 return TRUE;
7788
7789 s = bfd_get_section_by_name (abfd, ".dynamic");
7790 if (s == NULL || s->size == 0)
7791 return TRUE;
7792
7793 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7794 goto error_return;
7795
7796 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7797 if (elfsec == SHN_BAD)
7798 goto error_return;
7799
7800 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7801
7802 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7803 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7804
7805 extdyn = dynbuf;
7806 extdynend = extdyn + s->size;
7807 for (; extdyn < extdynend; extdyn += extdynsize)
7808 {
7809 Elf_Internal_Dyn dyn;
7810
7811 (*swap_dyn_in) (abfd, extdyn, &dyn);
7812
7813 if (dyn.d_tag == DT_NULL)
7814 break;
7815
7816 if (dyn.d_tag == DT_NEEDED)
7817 {
7818 const char *string;
7819 struct bfd_link_needed_list *l;
7820 unsigned int tagv = dyn.d_un.d_val;
7821 bfd_size_type amt;
7822
7823 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7824 if (string == NULL)
7825 goto error_return;
7826
7827 amt = sizeof *l;
7828 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7829 if (l == NULL)
7830 goto error_return;
7831
7832 l->by = abfd;
7833 l->name = string;
7834 l->next = *pneeded;
7835 *pneeded = l;
7836 }
7837 }
7838
7839 free (dynbuf);
7840
7841 return TRUE;
7842
7843 error_return:
7844 if (dynbuf != NULL)
7845 free (dynbuf);
7846 return FALSE;
7847 }
7848
7849 struct elf_symbuf_symbol
7850 {
7851 unsigned long st_name; /* Symbol name, index in string tbl */
7852 unsigned char st_info; /* Type and binding attributes */
7853 unsigned char st_other; /* Visibilty, and target specific */
7854 };
7855
7856 struct elf_symbuf_head
7857 {
7858 struct elf_symbuf_symbol *ssym;
7859 size_t count;
7860 unsigned int st_shndx;
7861 };
7862
7863 struct elf_symbol
7864 {
7865 union
7866 {
7867 Elf_Internal_Sym *isym;
7868 struct elf_symbuf_symbol *ssym;
7869 } u;
7870 const char *name;
7871 };
7872
7873 /* Sort references to symbols by ascending section number. */
7874
7875 static int
7876 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7877 {
7878 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7879 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7880
7881 return s1->st_shndx - s2->st_shndx;
7882 }
7883
7884 static int
7885 elf_sym_name_compare (const void *arg1, const void *arg2)
7886 {
7887 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7888 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7889 return strcmp (s1->name, s2->name);
7890 }
7891
7892 static struct elf_symbuf_head *
7893 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
7894 {
7895 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7896 struct elf_symbuf_symbol *ssym;
7897 struct elf_symbuf_head *ssymbuf, *ssymhead;
7898 size_t i, shndx_count, total_size;
7899
7900 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7901 if (indbuf == NULL)
7902 return NULL;
7903
7904 for (ind = indbuf, i = 0; i < symcount; i++)
7905 if (isymbuf[i].st_shndx != SHN_UNDEF)
7906 *ind++ = &isymbuf[i];
7907 indbufend = ind;
7908
7909 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7910 elf_sort_elf_symbol);
7911
7912 shndx_count = 0;
7913 if (indbufend > indbuf)
7914 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7915 if (ind[0]->st_shndx != ind[1]->st_shndx)
7916 shndx_count++;
7917
7918 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7919 + (indbufend - indbuf) * sizeof (*ssym));
7920 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7921 if (ssymbuf == NULL)
7922 {
7923 free (indbuf);
7924 return NULL;
7925 }
7926
7927 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7928 ssymbuf->ssym = NULL;
7929 ssymbuf->count = shndx_count;
7930 ssymbuf->st_shndx = 0;
7931 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7932 {
7933 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7934 {
7935 ssymhead++;
7936 ssymhead->ssym = ssym;
7937 ssymhead->count = 0;
7938 ssymhead->st_shndx = (*ind)->st_shndx;
7939 }
7940 ssym->st_name = (*ind)->st_name;
7941 ssym->st_info = (*ind)->st_info;
7942 ssym->st_other = (*ind)->st_other;
7943 ssymhead->count++;
7944 }
7945 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
7946 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7947 == total_size));
7948
7949 free (indbuf);
7950 return ssymbuf;
7951 }
7952
7953 /* Check if 2 sections define the same set of local and global
7954 symbols. */
7955
7956 static bfd_boolean
7957 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7958 struct bfd_link_info *info)
7959 {
7960 bfd *bfd1, *bfd2;
7961 const struct elf_backend_data *bed1, *bed2;
7962 Elf_Internal_Shdr *hdr1, *hdr2;
7963 size_t symcount1, symcount2;
7964 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7965 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7966 Elf_Internal_Sym *isym, *isymend;
7967 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7968 size_t count1, count2, i;
7969 unsigned int shndx1, shndx2;
7970 bfd_boolean result;
7971
7972 bfd1 = sec1->owner;
7973 bfd2 = sec2->owner;
7974
7975 /* Both sections have to be in ELF. */
7976 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7977 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7978 return FALSE;
7979
7980 if (elf_section_type (sec1) != elf_section_type (sec2))
7981 return FALSE;
7982
7983 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7984 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7985 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7986 return FALSE;
7987
7988 bed1 = get_elf_backend_data (bfd1);
7989 bed2 = get_elf_backend_data (bfd2);
7990 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7991 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7992 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7993 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7994
7995 if (symcount1 == 0 || symcount2 == 0)
7996 return FALSE;
7997
7998 result = FALSE;
7999 isymbuf1 = NULL;
8000 isymbuf2 = NULL;
8001 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8002 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8003
8004 if (ssymbuf1 == NULL)
8005 {
8006 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8007 NULL, NULL, NULL);
8008 if (isymbuf1 == NULL)
8009 goto done;
8010
8011 if (!info->reduce_memory_overheads)
8012 elf_tdata (bfd1)->symbuf = ssymbuf1
8013 = elf_create_symbuf (symcount1, isymbuf1);
8014 }
8015
8016 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8017 {
8018 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8019 NULL, NULL, NULL);
8020 if (isymbuf2 == NULL)
8021 goto done;
8022
8023 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
8024 elf_tdata (bfd2)->symbuf = ssymbuf2
8025 = elf_create_symbuf (symcount2, isymbuf2);
8026 }
8027
8028 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8029 {
8030 /* Optimized faster version. */
8031 size_t lo, hi, mid;
8032 struct elf_symbol *symp;
8033 struct elf_symbuf_symbol *ssym, *ssymend;
8034
8035 lo = 0;
8036 hi = ssymbuf1->count;
8037 ssymbuf1++;
8038 count1 = 0;
8039 while (lo < hi)
8040 {
8041 mid = (lo + hi) / 2;
8042 if (shndx1 < ssymbuf1[mid].st_shndx)
8043 hi = mid;
8044 else if (shndx1 > ssymbuf1[mid].st_shndx)
8045 lo = mid + 1;
8046 else
8047 {
8048 count1 = ssymbuf1[mid].count;
8049 ssymbuf1 += mid;
8050 break;
8051 }
8052 }
8053
8054 lo = 0;
8055 hi = ssymbuf2->count;
8056 ssymbuf2++;
8057 count2 = 0;
8058 while (lo < hi)
8059 {
8060 mid = (lo + hi) / 2;
8061 if (shndx2 < ssymbuf2[mid].st_shndx)
8062 hi = mid;
8063 else if (shndx2 > ssymbuf2[mid].st_shndx)
8064 lo = mid + 1;
8065 else
8066 {
8067 count2 = ssymbuf2[mid].count;
8068 ssymbuf2 += mid;
8069 break;
8070 }
8071 }
8072
8073 if (count1 == 0 || count2 == 0 || count1 != count2)
8074 goto done;
8075
8076 symtable1
8077 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8078 symtable2
8079 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8080 if (symtable1 == NULL || symtable2 == NULL)
8081 goto done;
8082
8083 symp = symtable1;
8084 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
8085 ssym < ssymend; ssym++, symp++)
8086 {
8087 symp->u.ssym = ssym;
8088 symp->name = bfd_elf_string_from_elf_section (bfd1,
8089 hdr1->sh_link,
8090 ssym->st_name);
8091 }
8092
8093 symp = symtable2;
8094 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8095 ssym < ssymend; ssym++, symp++)
8096 {
8097 symp->u.ssym = ssym;
8098 symp->name = bfd_elf_string_from_elf_section (bfd2,
8099 hdr2->sh_link,
8100 ssym->st_name);
8101 }
8102
8103 /* Sort symbol by name. */
8104 qsort (symtable1, count1, sizeof (struct elf_symbol),
8105 elf_sym_name_compare);
8106 qsort (symtable2, count1, sizeof (struct elf_symbol),
8107 elf_sym_name_compare);
8108
8109 for (i = 0; i < count1; i++)
8110 /* Two symbols must have the same binding, type and name. */
8111 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8112 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8113 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8114 goto done;
8115
8116 result = TRUE;
8117 goto done;
8118 }
8119
8120 symtable1 = (struct elf_symbol *)
8121 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8122 symtable2 = (struct elf_symbol *)
8123 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8124 if (symtable1 == NULL || symtable2 == NULL)
8125 goto done;
8126
8127 /* Count definitions in the section. */
8128 count1 = 0;
8129 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8130 if (isym->st_shndx == shndx1)
8131 symtable1[count1++].u.isym = isym;
8132
8133 count2 = 0;
8134 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8135 if (isym->st_shndx == shndx2)
8136 symtable2[count2++].u.isym = isym;
8137
8138 if (count1 == 0 || count2 == 0 || count1 != count2)
8139 goto done;
8140
8141 for (i = 0; i < count1; i++)
8142 symtable1[i].name
8143 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8144 symtable1[i].u.isym->st_name);
8145
8146 for (i = 0; i < count2; i++)
8147 symtable2[i].name
8148 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8149 symtable2[i].u.isym->st_name);
8150
8151 /* Sort symbol by name. */
8152 qsort (symtable1, count1, sizeof (struct elf_symbol),
8153 elf_sym_name_compare);
8154 qsort (symtable2, count1, sizeof (struct elf_symbol),
8155 elf_sym_name_compare);
8156
8157 for (i = 0; i < count1; i++)
8158 /* Two symbols must have the same binding, type and name. */
8159 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8160 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8161 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8162 goto done;
8163
8164 result = TRUE;
8165
8166 done:
8167 if (symtable1)
8168 free (symtable1);
8169 if (symtable2)
8170 free (symtable2);
8171 if (isymbuf1)
8172 free (isymbuf1);
8173 if (isymbuf2)
8174 free (isymbuf2);
8175
8176 return result;
8177 }
8178
8179 /* Return TRUE if 2 section types are compatible. */
8180
8181 bfd_boolean
8182 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8183 bfd *bbfd, const asection *bsec)
8184 {
8185 if (asec == NULL
8186 || bsec == NULL
8187 || abfd->xvec->flavour != bfd_target_elf_flavour
8188 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8189 return TRUE;
8190
8191 return elf_section_type (asec) == elf_section_type (bsec);
8192 }
8193 \f
8194 /* Final phase of ELF linker. */
8195
8196 /* A structure we use to avoid passing large numbers of arguments. */
8197
8198 struct elf_final_link_info
8199 {
8200 /* General link information. */
8201 struct bfd_link_info *info;
8202 /* Output BFD. */
8203 bfd *output_bfd;
8204 /* Symbol string table. */
8205 struct elf_strtab_hash *symstrtab;
8206 /* .hash section. */
8207 asection *hash_sec;
8208 /* symbol version section (.gnu.version). */
8209 asection *symver_sec;
8210 /* Buffer large enough to hold contents of any section. */
8211 bfd_byte *contents;
8212 /* Buffer large enough to hold external relocs of any section. */
8213 void *external_relocs;
8214 /* Buffer large enough to hold internal relocs of any section. */
8215 Elf_Internal_Rela *internal_relocs;
8216 /* Buffer large enough to hold external local symbols of any input
8217 BFD. */
8218 bfd_byte *external_syms;
8219 /* And a buffer for symbol section indices. */
8220 Elf_External_Sym_Shndx *locsym_shndx;
8221 /* Buffer large enough to hold internal local symbols of any input
8222 BFD. */
8223 Elf_Internal_Sym *internal_syms;
8224 /* Array large enough to hold a symbol index for each local symbol
8225 of any input BFD. */
8226 long *indices;
8227 /* Array large enough to hold a section pointer for each local
8228 symbol of any input BFD. */
8229 asection **sections;
8230 /* Buffer for SHT_SYMTAB_SHNDX section. */
8231 Elf_External_Sym_Shndx *symshndxbuf;
8232 /* Number of STT_FILE syms seen. */
8233 size_t filesym_count;
8234 };
8235
8236 /* This struct is used to pass information to elf_link_output_extsym. */
8237
8238 struct elf_outext_info
8239 {
8240 bfd_boolean failed;
8241 bfd_boolean localsyms;
8242 bfd_boolean file_sym_done;
8243 struct elf_final_link_info *flinfo;
8244 };
8245
8246
8247 /* Support for evaluating a complex relocation.
8248
8249 Complex relocations are generalized, self-describing relocations. The
8250 implementation of them consists of two parts: complex symbols, and the
8251 relocations themselves.
8252
8253 The relocations are use a reserved elf-wide relocation type code (R_RELC
8254 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8255 information (start bit, end bit, word width, etc) into the addend. This
8256 information is extracted from CGEN-generated operand tables within gas.
8257
8258 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8259 internal) representing prefix-notation expressions, including but not
8260 limited to those sorts of expressions normally encoded as addends in the
8261 addend field. The symbol mangling format is:
8262
8263 <node> := <literal>
8264 | <unary-operator> ':' <node>
8265 | <binary-operator> ':' <node> ':' <node>
8266 ;
8267
8268 <literal> := 's' <digits=N> ':' <N character symbol name>
8269 | 'S' <digits=N> ':' <N character section name>
8270 | '#' <hexdigits>
8271 ;
8272
8273 <binary-operator> := as in C
8274 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8275
8276 static void
8277 set_symbol_value (bfd *bfd_with_globals,
8278 Elf_Internal_Sym *isymbuf,
8279 size_t locsymcount,
8280 size_t symidx,
8281 bfd_vma val)
8282 {
8283 struct elf_link_hash_entry **sym_hashes;
8284 struct elf_link_hash_entry *h;
8285 size_t extsymoff = locsymcount;
8286
8287 if (symidx < locsymcount)
8288 {
8289 Elf_Internal_Sym *sym;
8290
8291 sym = isymbuf + symidx;
8292 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8293 {
8294 /* It is a local symbol: move it to the
8295 "absolute" section and give it a value. */
8296 sym->st_shndx = SHN_ABS;
8297 sym->st_value = val;
8298 return;
8299 }
8300 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8301 extsymoff = 0;
8302 }
8303
8304 /* It is a global symbol: set its link type
8305 to "defined" and give it a value. */
8306
8307 sym_hashes = elf_sym_hashes (bfd_with_globals);
8308 h = sym_hashes [symidx - extsymoff];
8309 while (h->root.type == bfd_link_hash_indirect
8310 || h->root.type == bfd_link_hash_warning)
8311 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8312 h->root.type = bfd_link_hash_defined;
8313 h->root.u.def.value = val;
8314 h->root.u.def.section = bfd_abs_section_ptr;
8315 }
8316
8317 static bfd_boolean
8318 resolve_symbol (const char *name,
8319 bfd *input_bfd,
8320 struct elf_final_link_info *flinfo,
8321 bfd_vma *result,
8322 Elf_Internal_Sym *isymbuf,
8323 size_t locsymcount)
8324 {
8325 Elf_Internal_Sym *sym;
8326 struct bfd_link_hash_entry *global_entry;
8327 const char *candidate = NULL;
8328 Elf_Internal_Shdr *symtab_hdr;
8329 size_t i;
8330
8331 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8332
8333 for (i = 0; i < locsymcount; ++ i)
8334 {
8335 sym = isymbuf + i;
8336
8337 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8338 continue;
8339
8340 candidate = bfd_elf_string_from_elf_section (input_bfd,
8341 symtab_hdr->sh_link,
8342 sym->st_name);
8343 #ifdef DEBUG
8344 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8345 name, candidate, (unsigned long) sym->st_value);
8346 #endif
8347 if (candidate && strcmp (candidate, name) == 0)
8348 {
8349 asection *sec = flinfo->sections [i];
8350
8351 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8352 *result += sec->output_offset + sec->output_section->vma;
8353 #ifdef DEBUG
8354 printf ("Found symbol with value %8.8lx\n",
8355 (unsigned long) *result);
8356 #endif
8357 return TRUE;
8358 }
8359 }
8360
8361 /* Hmm, haven't found it yet. perhaps it is a global. */
8362 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8363 FALSE, FALSE, TRUE);
8364 if (!global_entry)
8365 return FALSE;
8366
8367 if (global_entry->type == bfd_link_hash_defined
8368 || global_entry->type == bfd_link_hash_defweak)
8369 {
8370 *result = (global_entry->u.def.value
8371 + global_entry->u.def.section->output_section->vma
8372 + global_entry->u.def.section->output_offset);
8373 #ifdef DEBUG
8374 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8375 global_entry->root.string, (unsigned long) *result);
8376 #endif
8377 return TRUE;
8378 }
8379
8380 return FALSE;
8381 }
8382
8383 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8384 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8385 names like "foo.end" which is the end address of section "foo". */
8386
8387 static bfd_boolean
8388 resolve_section (const char *name,
8389 asection *sections,
8390 bfd_vma *result,
8391 bfd * abfd)
8392 {
8393 asection *curr;
8394 unsigned int len;
8395
8396 for (curr = sections; curr; curr = curr->next)
8397 if (strcmp (curr->name, name) == 0)
8398 {
8399 *result = curr->vma;
8400 return TRUE;
8401 }
8402
8403 /* Hmm. still haven't found it. try pseudo-section names. */
8404 /* FIXME: This could be coded more efficiently... */
8405 for (curr = sections; curr; curr = curr->next)
8406 {
8407 len = strlen (curr->name);
8408 if (len > strlen (name))
8409 continue;
8410
8411 if (strncmp (curr->name, name, len) == 0)
8412 {
8413 if (strncmp (".end", name + len, 4) == 0)
8414 {
8415 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
8416 return TRUE;
8417 }
8418
8419 /* Insert more pseudo-section names here, if you like. */
8420 }
8421 }
8422
8423 return FALSE;
8424 }
8425
8426 static void
8427 undefined_reference (const char *reftype, const char *name)
8428 {
8429 /* xgettext:c-format */
8430 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8431 reftype, name);
8432 }
8433
8434 static bfd_boolean
8435 eval_symbol (bfd_vma *result,
8436 const char **symp,
8437 bfd *input_bfd,
8438 struct elf_final_link_info *flinfo,
8439 bfd_vma dot,
8440 Elf_Internal_Sym *isymbuf,
8441 size_t locsymcount,
8442 int signed_p)
8443 {
8444 size_t len;
8445 size_t symlen;
8446 bfd_vma a;
8447 bfd_vma b;
8448 char symbuf[4096];
8449 const char *sym = *symp;
8450 const char *symend;
8451 bfd_boolean symbol_is_section = FALSE;
8452
8453 len = strlen (sym);
8454 symend = sym + len;
8455
8456 if (len < 1 || len > sizeof (symbuf))
8457 {
8458 bfd_set_error (bfd_error_invalid_operation);
8459 return FALSE;
8460 }
8461
8462 switch (* sym)
8463 {
8464 case '.':
8465 *result = dot;
8466 *symp = sym + 1;
8467 return TRUE;
8468
8469 case '#':
8470 ++sym;
8471 *result = strtoul (sym, (char **) symp, 16);
8472 return TRUE;
8473
8474 case 'S':
8475 symbol_is_section = TRUE;
8476 /* Fall through. */
8477 case 's':
8478 ++sym;
8479 symlen = strtol (sym, (char **) symp, 10);
8480 sym = *symp + 1; /* Skip the trailing ':'. */
8481
8482 if (symend < sym || symlen + 1 > sizeof (symbuf))
8483 {
8484 bfd_set_error (bfd_error_invalid_operation);
8485 return FALSE;
8486 }
8487
8488 memcpy (symbuf, sym, symlen);
8489 symbuf[symlen] = '\0';
8490 *symp = sym + symlen;
8491
8492 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8493 the symbol as a section, or vice-versa. so we're pretty liberal in our
8494 interpretation here; section means "try section first", not "must be a
8495 section", and likewise with symbol. */
8496
8497 if (symbol_is_section)
8498 {
8499 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8500 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8501 isymbuf, locsymcount))
8502 {
8503 undefined_reference ("section", symbuf);
8504 return FALSE;
8505 }
8506 }
8507 else
8508 {
8509 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8510 isymbuf, locsymcount)
8511 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8512 result, input_bfd))
8513 {
8514 undefined_reference ("symbol", symbuf);
8515 return FALSE;
8516 }
8517 }
8518
8519 return TRUE;
8520
8521 /* All that remains are operators. */
8522
8523 #define UNARY_OP(op) \
8524 if (strncmp (sym, #op, strlen (#op)) == 0) \
8525 { \
8526 sym += strlen (#op); \
8527 if (*sym == ':') \
8528 ++sym; \
8529 *symp = sym; \
8530 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8531 isymbuf, locsymcount, signed_p)) \
8532 return FALSE; \
8533 if (signed_p) \
8534 *result = op ((bfd_signed_vma) a); \
8535 else \
8536 *result = op a; \
8537 return TRUE; \
8538 }
8539
8540 #define BINARY_OP(op) \
8541 if (strncmp (sym, #op, strlen (#op)) == 0) \
8542 { \
8543 sym += strlen (#op); \
8544 if (*sym == ':') \
8545 ++sym; \
8546 *symp = sym; \
8547 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8548 isymbuf, locsymcount, signed_p)) \
8549 return FALSE; \
8550 ++*symp; \
8551 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8552 isymbuf, locsymcount, signed_p)) \
8553 return FALSE; \
8554 if (signed_p) \
8555 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8556 else \
8557 *result = a op b; \
8558 return TRUE; \
8559 }
8560
8561 default:
8562 UNARY_OP (0-);
8563 BINARY_OP (<<);
8564 BINARY_OP (>>);
8565 BINARY_OP (==);
8566 BINARY_OP (!=);
8567 BINARY_OP (<=);
8568 BINARY_OP (>=);
8569 BINARY_OP (&&);
8570 BINARY_OP (||);
8571 UNARY_OP (~);
8572 UNARY_OP (!);
8573 BINARY_OP (*);
8574 BINARY_OP (/);
8575 BINARY_OP (%);
8576 BINARY_OP (^);
8577 BINARY_OP (|);
8578 BINARY_OP (&);
8579 BINARY_OP (+);
8580 BINARY_OP (-);
8581 BINARY_OP (<);
8582 BINARY_OP (>);
8583 #undef UNARY_OP
8584 #undef BINARY_OP
8585 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8586 bfd_set_error (bfd_error_invalid_operation);
8587 return FALSE;
8588 }
8589 }
8590
8591 static void
8592 put_value (bfd_vma size,
8593 unsigned long chunksz,
8594 bfd *input_bfd,
8595 bfd_vma x,
8596 bfd_byte *location)
8597 {
8598 location += (size - chunksz);
8599
8600 for (; size; size -= chunksz, location -= chunksz)
8601 {
8602 switch (chunksz)
8603 {
8604 case 1:
8605 bfd_put_8 (input_bfd, x, location);
8606 x >>= 8;
8607 break;
8608 case 2:
8609 bfd_put_16 (input_bfd, x, location);
8610 x >>= 16;
8611 break;
8612 case 4:
8613 bfd_put_32 (input_bfd, x, location);
8614 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8615 x >>= 16;
8616 x >>= 16;
8617 break;
8618 #ifdef BFD64
8619 case 8:
8620 bfd_put_64 (input_bfd, x, location);
8621 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8622 x >>= 32;
8623 x >>= 32;
8624 break;
8625 #endif
8626 default:
8627 abort ();
8628 break;
8629 }
8630 }
8631 }
8632
8633 static bfd_vma
8634 get_value (bfd_vma size,
8635 unsigned long chunksz,
8636 bfd *input_bfd,
8637 bfd_byte *location)
8638 {
8639 int shift;
8640 bfd_vma x = 0;
8641
8642 /* Sanity checks. */
8643 BFD_ASSERT (chunksz <= sizeof (x)
8644 && size >= chunksz
8645 && chunksz != 0
8646 && (size % chunksz) == 0
8647 && input_bfd != NULL
8648 && location != NULL);
8649
8650 if (chunksz == sizeof (x))
8651 {
8652 BFD_ASSERT (size == chunksz);
8653
8654 /* Make sure that we do not perform an undefined shift operation.
8655 We know that size == chunksz so there will only be one iteration
8656 of the loop below. */
8657 shift = 0;
8658 }
8659 else
8660 shift = 8 * chunksz;
8661
8662 for (; size; size -= chunksz, location += chunksz)
8663 {
8664 switch (chunksz)
8665 {
8666 case 1:
8667 x = (x << shift) | bfd_get_8 (input_bfd, location);
8668 break;
8669 case 2:
8670 x = (x << shift) | bfd_get_16 (input_bfd, location);
8671 break;
8672 case 4:
8673 x = (x << shift) | bfd_get_32 (input_bfd, location);
8674 break;
8675 #ifdef BFD64
8676 case 8:
8677 x = (x << shift) | bfd_get_64 (input_bfd, location);
8678 break;
8679 #endif
8680 default:
8681 abort ();
8682 }
8683 }
8684 return x;
8685 }
8686
8687 static void
8688 decode_complex_addend (unsigned long *start, /* in bits */
8689 unsigned long *oplen, /* in bits */
8690 unsigned long *len, /* in bits */
8691 unsigned long *wordsz, /* in bytes */
8692 unsigned long *chunksz, /* in bytes */
8693 unsigned long *lsb0_p,
8694 unsigned long *signed_p,
8695 unsigned long *trunc_p,
8696 unsigned long encoded)
8697 {
8698 * start = encoded & 0x3F;
8699 * len = (encoded >> 6) & 0x3F;
8700 * oplen = (encoded >> 12) & 0x3F;
8701 * wordsz = (encoded >> 18) & 0xF;
8702 * chunksz = (encoded >> 22) & 0xF;
8703 * lsb0_p = (encoded >> 27) & 1;
8704 * signed_p = (encoded >> 28) & 1;
8705 * trunc_p = (encoded >> 29) & 1;
8706 }
8707
8708 bfd_reloc_status_type
8709 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8710 asection *input_section ATTRIBUTE_UNUSED,
8711 bfd_byte *contents,
8712 Elf_Internal_Rela *rel,
8713 bfd_vma relocation)
8714 {
8715 bfd_vma shift, x, mask;
8716 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8717 bfd_reloc_status_type r;
8718
8719 /* Perform this reloc, since it is complex.
8720 (this is not to say that it necessarily refers to a complex
8721 symbol; merely that it is a self-describing CGEN based reloc.
8722 i.e. the addend has the complete reloc information (bit start, end,
8723 word size, etc) encoded within it.). */
8724
8725 decode_complex_addend (&start, &oplen, &len, &wordsz,
8726 &chunksz, &lsb0_p, &signed_p,
8727 &trunc_p, rel->r_addend);
8728
8729 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8730
8731 if (lsb0_p)
8732 shift = (start + 1) - len;
8733 else
8734 shift = (8 * wordsz) - (start + len);
8735
8736 x = get_value (wordsz, chunksz, input_bfd,
8737 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8738
8739 #ifdef DEBUG
8740 printf ("Doing complex reloc: "
8741 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8742 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8743 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8744 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8745 oplen, (unsigned long) x, (unsigned long) mask,
8746 (unsigned long) relocation);
8747 #endif
8748
8749 r = bfd_reloc_ok;
8750 if (! trunc_p)
8751 /* Now do an overflow check. */
8752 r = bfd_check_overflow ((signed_p
8753 ? complain_overflow_signed
8754 : complain_overflow_unsigned),
8755 len, 0, (8 * wordsz),
8756 relocation);
8757
8758 /* Do the deed. */
8759 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8760
8761 #ifdef DEBUG
8762 printf (" relocation: %8.8lx\n"
8763 " shifted mask: %8.8lx\n"
8764 " shifted/masked reloc: %8.8lx\n"
8765 " result: %8.8lx\n",
8766 (unsigned long) relocation, (unsigned long) (mask << shift),
8767 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8768 #endif
8769 put_value (wordsz, chunksz, input_bfd, x,
8770 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8771 return r;
8772 }
8773
8774 /* Functions to read r_offset from external (target order) reloc
8775 entry. Faster than bfd_getl32 et al, because we let the compiler
8776 know the value is aligned. */
8777
8778 static bfd_vma
8779 ext32l_r_offset (const void *p)
8780 {
8781 union aligned32
8782 {
8783 uint32_t v;
8784 unsigned char c[4];
8785 };
8786 const union aligned32 *a
8787 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8788
8789 uint32_t aval = ( (uint32_t) a->c[0]
8790 | (uint32_t) a->c[1] << 8
8791 | (uint32_t) a->c[2] << 16
8792 | (uint32_t) a->c[3] << 24);
8793 return aval;
8794 }
8795
8796 static bfd_vma
8797 ext32b_r_offset (const void *p)
8798 {
8799 union aligned32
8800 {
8801 uint32_t v;
8802 unsigned char c[4];
8803 };
8804 const union aligned32 *a
8805 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8806
8807 uint32_t aval = ( (uint32_t) a->c[0] << 24
8808 | (uint32_t) a->c[1] << 16
8809 | (uint32_t) a->c[2] << 8
8810 | (uint32_t) a->c[3]);
8811 return aval;
8812 }
8813
8814 #ifdef BFD_HOST_64_BIT
8815 static bfd_vma
8816 ext64l_r_offset (const void *p)
8817 {
8818 union aligned64
8819 {
8820 uint64_t v;
8821 unsigned char c[8];
8822 };
8823 const union aligned64 *a
8824 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8825
8826 uint64_t aval = ( (uint64_t) a->c[0]
8827 | (uint64_t) a->c[1] << 8
8828 | (uint64_t) a->c[2] << 16
8829 | (uint64_t) a->c[3] << 24
8830 | (uint64_t) a->c[4] << 32
8831 | (uint64_t) a->c[5] << 40
8832 | (uint64_t) a->c[6] << 48
8833 | (uint64_t) a->c[7] << 56);
8834 return aval;
8835 }
8836
8837 static bfd_vma
8838 ext64b_r_offset (const void *p)
8839 {
8840 union aligned64
8841 {
8842 uint64_t v;
8843 unsigned char c[8];
8844 };
8845 const union aligned64 *a
8846 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8847
8848 uint64_t aval = ( (uint64_t) a->c[0] << 56
8849 | (uint64_t) a->c[1] << 48
8850 | (uint64_t) a->c[2] << 40
8851 | (uint64_t) a->c[3] << 32
8852 | (uint64_t) a->c[4] << 24
8853 | (uint64_t) a->c[5] << 16
8854 | (uint64_t) a->c[6] << 8
8855 | (uint64_t) a->c[7]);
8856 return aval;
8857 }
8858 #endif
8859
8860 /* When performing a relocatable link, the input relocations are
8861 preserved. But, if they reference global symbols, the indices
8862 referenced must be updated. Update all the relocations found in
8863 RELDATA. */
8864
8865 static bfd_boolean
8866 elf_link_adjust_relocs (bfd *abfd,
8867 asection *sec,
8868 struct bfd_elf_section_reloc_data *reldata,
8869 bfd_boolean sort,
8870 struct bfd_link_info *info)
8871 {
8872 unsigned int i;
8873 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8874 bfd_byte *erela;
8875 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8876 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8877 bfd_vma r_type_mask;
8878 int r_sym_shift;
8879 unsigned int count = reldata->count;
8880 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8881
8882 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8883 {
8884 swap_in = bed->s->swap_reloc_in;
8885 swap_out = bed->s->swap_reloc_out;
8886 }
8887 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8888 {
8889 swap_in = bed->s->swap_reloca_in;
8890 swap_out = bed->s->swap_reloca_out;
8891 }
8892 else
8893 abort ();
8894
8895 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8896 abort ();
8897
8898 if (bed->s->arch_size == 32)
8899 {
8900 r_type_mask = 0xff;
8901 r_sym_shift = 8;
8902 }
8903 else
8904 {
8905 r_type_mask = 0xffffffff;
8906 r_sym_shift = 32;
8907 }
8908
8909 erela = reldata->hdr->contents;
8910 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8911 {
8912 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8913 unsigned int j;
8914
8915 if (*rel_hash == NULL)
8916 continue;
8917
8918 if ((*rel_hash)->indx == -2
8919 && info->gc_sections
8920 && ! info->gc_keep_exported)
8921 {
8922 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
8923 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
8924 abfd, sec,
8925 (*rel_hash)->root.root.string);
8926 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
8927 abfd, sec);
8928 bfd_set_error (bfd_error_invalid_operation);
8929 return FALSE;
8930 }
8931 BFD_ASSERT ((*rel_hash)->indx >= 0);
8932
8933 (*swap_in) (abfd, erela, irela);
8934 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8935 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8936 | (irela[j].r_info & r_type_mask));
8937 (*swap_out) (abfd, irela, erela);
8938 }
8939
8940 if (bed->elf_backend_update_relocs)
8941 (*bed->elf_backend_update_relocs) (sec, reldata);
8942
8943 if (sort && count != 0)
8944 {
8945 bfd_vma (*ext_r_off) (const void *);
8946 bfd_vma r_off;
8947 size_t elt_size;
8948 bfd_byte *base, *end, *p, *loc;
8949 bfd_byte *buf = NULL;
8950
8951 if (bed->s->arch_size == 32)
8952 {
8953 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8954 ext_r_off = ext32l_r_offset;
8955 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8956 ext_r_off = ext32b_r_offset;
8957 else
8958 abort ();
8959 }
8960 else
8961 {
8962 #ifdef BFD_HOST_64_BIT
8963 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8964 ext_r_off = ext64l_r_offset;
8965 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8966 ext_r_off = ext64b_r_offset;
8967 else
8968 #endif
8969 abort ();
8970 }
8971
8972 /* Must use a stable sort here. A modified insertion sort,
8973 since the relocs are mostly sorted already. */
8974 elt_size = reldata->hdr->sh_entsize;
8975 base = reldata->hdr->contents;
8976 end = base + count * elt_size;
8977 if (elt_size > sizeof (Elf64_External_Rela))
8978 abort ();
8979
8980 /* Ensure the first element is lowest. This acts as a sentinel,
8981 speeding the main loop below. */
8982 r_off = (*ext_r_off) (base);
8983 for (p = loc = base; (p += elt_size) < end; )
8984 {
8985 bfd_vma r_off2 = (*ext_r_off) (p);
8986 if (r_off > r_off2)
8987 {
8988 r_off = r_off2;
8989 loc = p;
8990 }
8991 }
8992 if (loc != base)
8993 {
8994 /* Don't just swap *base and *loc as that changes the order
8995 of the original base[0] and base[1] if they happen to
8996 have the same r_offset. */
8997 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8998 memcpy (onebuf, loc, elt_size);
8999 memmove (base + elt_size, base, loc - base);
9000 memcpy (base, onebuf, elt_size);
9001 }
9002
9003 for (p = base + elt_size; (p += elt_size) < end; )
9004 {
9005 /* base to p is sorted, *p is next to insert. */
9006 r_off = (*ext_r_off) (p);
9007 /* Search the sorted region for location to insert. */
9008 loc = p - elt_size;
9009 while (r_off < (*ext_r_off) (loc))
9010 loc -= elt_size;
9011 loc += elt_size;
9012 if (loc != p)
9013 {
9014 /* Chances are there is a run of relocs to insert here,
9015 from one of more input files. Files are not always
9016 linked in order due to the way elf_link_input_bfd is
9017 called. See pr17666. */
9018 size_t sortlen = p - loc;
9019 bfd_vma r_off2 = (*ext_r_off) (loc);
9020 size_t runlen = elt_size;
9021 size_t buf_size = 96 * 1024;
9022 while (p + runlen < end
9023 && (sortlen <= buf_size
9024 || runlen + elt_size <= buf_size)
9025 && r_off2 > (*ext_r_off) (p + runlen))
9026 runlen += elt_size;
9027 if (buf == NULL)
9028 {
9029 buf = bfd_malloc (buf_size);
9030 if (buf == NULL)
9031 return FALSE;
9032 }
9033 if (runlen < sortlen)
9034 {
9035 memcpy (buf, p, runlen);
9036 memmove (loc + runlen, loc, sortlen);
9037 memcpy (loc, buf, runlen);
9038 }
9039 else
9040 {
9041 memcpy (buf, loc, sortlen);
9042 memmove (loc, p, runlen);
9043 memcpy (loc + runlen, buf, sortlen);
9044 }
9045 p += runlen - elt_size;
9046 }
9047 }
9048 /* Hashes are no longer valid. */
9049 free (reldata->hashes);
9050 reldata->hashes = NULL;
9051 free (buf);
9052 }
9053 return TRUE;
9054 }
9055
9056 struct elf_link_sort_rela
9057 {
9058 union {
9059 bfd_vma offset;
9060 bfd_vma sym_mask;
9061 } u;
9062 enum elf_reloc_type_class type;
9063 /* We use this as an array of size int_rels_per_ext_rel. */
9064 Elf_Internal_Rela rela[1];
9065 };
9066
9067 static int
9068 elf_link_sort_cmp1 (const void *A, const void *B)
9069 {
9070 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9071 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9072 int relativea, relativeb;
9073
9074 relativea = a->type == reloc_class_relative;
9075 relativeb = b->type == reloc_class_relative;
9076
9077 if (relativea < relativeb)
9078 return 1;
9079 if (relativea > relativeb)
9080 return -1;
9081 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9082 return -1;
9083 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9084 return 1;
9085 if (a->rela->r_offset < b->rela->r_offset)
9086 return -1;
9087 if (a->rela->r_offset > b->rela->r_offset)
9088 return 1;
9089 return 0;
9090 }
9091
9092 static int
9093 elf_link_sort_cmp2 (const void *A, const void *B)
9094 {
9095 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9096 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9097
9098 if (a->type < b->type)
9099 return -1;
9100 if (a->type > b->type)
9101 return 1;
9102 if (a->u.offset < b->u.offset)
9103 return -1;
9104 if (a->u.offset > b->u.offset)
9105 return 1;
9106 if (a->rela->r_offset < b->rela->r_offset)
9107 return -1;
9108 if (a->rela->r_offset > b->rela->r_offset)
9109 return 1;
9110 return 0;
9111 }
9112
9113 static size_t
9114 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9115 {
9116 asection *dynamic_relocs;
9117 asection *rela_dyn;
9118 asection *rel_dyn;
9119 bfd_size_type count, size;
9120 size_t i, ret, sort_elt, ext_size;
9121 bfd_byte *sort, *s_non_relative, *p;
9122 struct elf_link_sort_rela *sq;
9123 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9124 int i2e = bed->s->int_rels_per_ext_rel;
9125 unsigned int opb = bfd_octets_per_byte (abfd);
9126 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9127 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9128 struct bfd_link_order *lo;
9129 bfd_vma r_sym_mask;
9130 bfd_boolean use_rela;
9131
9132 /* Find a dynamic reloc section. */
9133 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9134 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9135 if (rela_dyn != NULL && rela_dyn->size > 0
9136 && rel_dyn != NULL && rel_dyn->size > 0)
9137 {
9138 bfd_boolean use_rela_initialised = FALSE;
9139
9140 /* This is just here to stop gcc from complaining.
9141 Its initialization checking code is not perfect. */
9142 use_rela = TRUE;
9143
9144 /* Both sections are present. Examine the sizes
9145 of the indirect sections to help us choose. */
9146 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9147 if (lo->type == bfd_indirect_link_order)
9148 {
9149 asection *o = lo->u.indirect.section;
9150
9151 if ((o->size % bed->s->sizeof_rela) == 0)
9152 {
9153 if ((o->size % bed->s->sizeof_rel) == 0)
9154 /* Section size is divisible by both rel and rela sizes.
9155 It is of no help to us. */
9156 ;
9157 else
9158 {
9159 /* Section size is only divisible by rela. */
9160 if (use_rela_initialised && !use_rela)
9161 {
9162 _bfd_error_handler (_("%pB: unable to sort relocs - "
9163 "they are in more than one size"),
9164 abfd);
9165 bfd_set_error (bfd_error_invalid_operation);
9166 return 0;
9167 }
9168 else
9169 {
9170 use_rela = TRUE;
9171 use_rela_initialised = TRUE;
9172 }
9173 }
9174 }
9175 else if ((o->size % bed->s->sizeof_rel) == 0)
9176 {
9177 /* Section size is only divisible by rel. */
9178 if (use_rela_initialised && use_rela)
9179 {
9180 _bfd_error_handler (_("%pB: unable to sort relocs - "
9181 "they are in more than one size"),
9182 abfd);
9183 bfd_set_error (bfd_error_invalid_operation);
9184 return 0;
9185 }
9186 else
9187 {
9188 use_rela = FALSE;
9189 use_rela_initialised = TRUE;
9190 }
9191 }
9192 else
9193 {
9194 /* The section size is not divisible by either -
9195 something is wrong. */
9196 _bfd_error_handler (_("%pB: unable to sort relocs - "
9197 "they are of an unknown size"), abfd);
9198 bfd_set_error (bfd_error_invalid_operation);
9199 return 0;
9200 }
9201 }
9202
9203 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9204 if (lo->type == bfd_indirect_link_order)
9205 {
9206 asection *o = lo->u.indirect.section;
9207
9208 if ((o->size % bed->s->sizeof_rela) == 0)
9209 {
9210 if ((o->size % bed->s->sizeof_rel) == 0)
9211 /* Section size is divisible by both rel and rela sizes.
9212 It is of no help to us. */
9213 ;
9214 else
9215 {
9216 /* Section size is only divisible by rela. */
9217 if (use_rela_initialised && !use_rela)
9218 {
9219 _bfd_error_handler (_("%pB: unable to sort relocs - "
9220 "they are in more than one size"),
9221 abfd);
9222 bfd_set_error (bfd_error_invalid_operation);
9223 return 0;
9224 }
9225 else
9226 {
9227 use_rela = TRUE;
9228 use_rela_initialised = TRUE;
9229 }
9230 }
9231 }
9232 else if ((o->size % bed->s->sizeof_rel) == 0)
9233 {
9234 /* Section size is only divisible by rel. */
9235 if (use_rela_initialised && use_rela)
9236 {
9237 _bfd_error_handler (_("%pB: unable to sort relocs - "
9238 "they are in more than one size"),
9239 abfd);
9240 bfd_set_error (bfd_error_invalid_operation);
9241 return 0;
9242 }
9243 else
9244 {
9245 use_rela = FALSE;
9246 use_rela_initialised = TRUE;
9247 }
9248 }
9249 else
9250 {
9251 /* The section size is not divisible by either -
9252 something is wrong. */
9253 _bfd_error_handler (_("%pB: unable to sort relocs - "
9254 "they are of an unknown size"), abfd);
9255 bfd_set_error (bfd_error_invalid_operation);
9256 return 0;
9257 }
9258 }
9259
9260 if (! use_rela_initialised)
9261 /* Make a guess. */
9262 use_rela = TRUE;
9263 }
9264 else if (rela_dyn != NULL && rela_dyn->size > 0)
9265 use_rela = TRUE;
9266 else if (rel_dyn != NULL && rel_dyn->size > 0)
9267 use_rela = FALSE;
9268 else
9269 return 0;
9270
9271 if (use_rela)
9272 {
9273 dynamic_relocs = rela_dyn;
9274 ext_size = bed->s->sizeof_rela;
9275 swap_in = bed->s->swap_reloca_in;
9276 swap_out = bed->s->swap_reloca_out;
9277 }
9278 else
9279 {
9280 dynamic_relocs = rel_dyn;
9281 ext_size = bed->s->sizeof_rel;
9282 swap_in = bed->s->swap_reloc_in;
9283 swap_out = bed->s->swap_reloc_out;
9284 }
9285
9286 size = 0;
9287 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9288 if (lo->type == bfd_indirect_link_order)
9289 size += lo->u.indirect.section->size;
9290
9291 if (size != dynamic_relocs->size)
9292 return 0;
9293
9294 sort_elt = (sizeof (struct elf_link_sort_rela)
9295 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9296
9297 count = dynamic_relocs->size / ext_size;
9298 if (count == 0)
9299 return 0;
9300 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9301
9302 if (sort == NULL)
9303 {
9304 (*info->callbacks->warning)
9305 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
9306 return 0;
9307 }
9308
9309 if (bed->s->arch_size == 32)
9310 r_sym_mask = ~(bfd_vma) 0xff;
9311 else
9312 r_sym_mask = ~(bfd_vma) 0xffffffff;
9313
9314 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9315 if (lo->type == bfd_indirect_link_order)
9316 {
9317 bfd_byte *erel, *erelend;
9318 asection *o = lo->u.indirect.section;
9319
9320 if (o->contents == NULL && o->size != 0)
9321 {
9322 /* This is a reloc section that is being handled as a normal
9323 section. See bfd_section_from_shdr. We can't combine
9324 relocs in this case. */
9325 free (sort);
9326 return 0;
9327 }
9328 erel = o->contents;
9329 erelend = o->contents + o->size;
9330 p = sort + o->output_offset * opb / ext_size * sort_elt;
9331
9332 while (erel < erelend)
9333 {
9334 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9335
9336 (*swap_in) (abfd, erel, s->rela);
9337 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9338 s->u.sym_mask = r_sym_mask;
9339 p += sort_elt;
9340 erel += ext_size;
9341 }
9342 }
9343
9344 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9345
9346 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9347 {
9348 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9349 if (s->type != reloc_class_relative)
9350 break;
9351 }
9352 ret = i;
9353 s_non_relative = p;
9354
9355 sq = (struct elf_link_sort_rela *) s_non_relative;
9356 for (; i < count; i++, p += sort_elt)
9357 {
9358 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9359 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9360 sq = sp;
9361 sp->u.offset = sq->rela->r_offset;
9362 }
9363
9364 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9365
9366 struct elf_link_hash_table *htab = elf_hash_table (info);
9367 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9368 {
9369 /* We have plt relocs in .rela.dyn. */
9370 sq = (struct elf_link_sort_rela *) sort;
9371 for (i = 0; i < count; i++)
9372 if (sq[count - i - 1].type != reloc_class_plt)
9373 break;
9374 if (i != 0 && htab->srelplt->size == i * ext_size)
9375 {
9376 struct bfd_link_order **plo;
9377 /* Put srelplt link_order last. This is so the output_offset
9378 set in the next loop is correct for DT_JMPREL. */
9379 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9380 if ((*plo)->type == bfd_indirect_link_order
9381 && (*plo)->u.indirect.section == htab->srelplt)
9382 {
9383 lo = *plo;
9384 *plo = lo->next;
9385 }
9386 else
9387 plo = &(*plo)->next;
9388 *plo = lo;
9389 lo->next = NULL;
9390 dynamic_relocs->map_tail.link_order = lo;
9391 }
9392 }
9393
9394 p = sort;
9395 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9396 if (lo->type == bfd_indirect_link_order)
9397 {
9398 bfd_byte *erel, *erelend;
9399 asection *o = lo->u.indirect.section;
9400
9401 erel = o->contents;
9402 erelend = o->contents + o->size;
9403 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9404 while (erel < erelend)
9405 {
9406 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9407 (*swap_out) (abfd, s->rela, erel);
9408 p += sort_elt;
9409 erel += ext_size;
9410 }
9411 }
9412
9413 free (sort);
9414 *psec = dynamic_relocs;
9415 return ret;
9416 }
9417
9418 /* Add a symbol to the output symbol string table. */
9419
9420 static int
9421 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9422 const char *name,
9423 Elf_Internal_Sym *elfsym,
9424 asection *input_sec,
9425 struct elf_link_hash_entry *h)
9426 {
9427 int (*output_symbol_hook)
9428 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9429 struct elf_link_hash_entry *);
9430 struct elf_link_hash_table *hash_table;
9431 const struct elf_backend_data *bed;
9432 bfd_size_type strtabsize;
9433
9434 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9435
9436 bed = get_elf_backend_data (flinfo->output_bfd);
9437 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9438 if (output_symbol_hook != NULL)
9439 {
9440 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9441 if (ret != 1)
9442 return ret;
9443 }
9444
9445 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
9446 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
9447 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
9448 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
9449
9450 if (name == NULL
9451 || *name == '\0'
9452 || (input_sec->flags & SEC_EXCLUDE))
9453 elfsym->st_name = (unsigned long) -1;
9454 else
9455 {
9456 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9457 to get the final offset for st_name. */
9458 elfsym->st_name
9459 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9460 name, FALSE);
9461 if (elfsym->st_name == (unsigned long) -1)
9462 return 0;
9463 }
9464
9465 hash_table = elf_hash_table (flinfo->info);
9466 strtabsize = hash_table->strtabsize;
9467 if (strtabsize <= hash_table->strtabcount)
9468 {
9469 strtabsize += strtabsize;
9470 hash_table->strtabsize = strtabsize;
9471 strtabsize *= sizeof (*hash_table->strtab);
9472 hash_table->strtab
9473 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9474 strtabsize);
9475 if (hash_table->strtab == NULL)
9476 return 0;
9477 }
9478 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9479 hash_table->strtab[hash_table->strtabcount].dest_index
9480 = hash_table->strtabcount;
9481 hash_table->strtab[hash_table->strtabcount].destshndx_index
9482 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9483
9484 flinfo->output_bfd->symcount += 1;
9485 hash_table->strtabcount += 1;
9486
9487 return 1;
9488 }
9489
9490 /* Swap symbols out to the symbol table and flush the output symbols to
9491 the file. */
9492
9493 static bfd_boolean
9494 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9495 {
9496 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9497 bfd_size_type amt;
9498 size_t i;
9499 const struct elf_backend_data *bed;
9500 bfd_byte *symbuf;
9501 Elf_Internal_Shdr *hdr;
9502 file_ptr pos;
9503 bfd_boolean ret;
9504
9505 if (!hash_table->strtabcount)
9506 return TRUE;
9507
9508 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9509
9510 bed = get_elf_backend_data (flinfo->output_bfd);
9511
9512 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9513 symbuf = (bfd_byte *) bfd_malloc (amt);
9514 if (symbuf == NULL)
9515 return FALSE;
9516
9517 if (flinfo->symshndxbuf)
9518 {
9519 amt = sizeof (Elf_External_Sym_Shndx);
9520 amt *= bfd_get_symcount (flinfo->output_bfd);
9521 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9522 if (flinfo->symshndxbuf == NULL)
9523 {
9524 free (symbuf);
9525 return FALSE;
9526 }
9527 }
9528
9529 for (i = 0; i < hash_table->strtabcount; i++)
9530 {
9531 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9532 if (elfsym->sym.st_name == (unsigned long) -1)
9533 elfsym->sym.st_name = 0;
9534 else
9535 elfsym->sym.st_name
9536 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9537 elfsym->sym.st_name);
9538 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9539 ((bfd_byte *) symbuf
9540 + (elfsym->dest_index
9541 * bed->s->sizeof_sym)),
9542 (flinfo->symshndxbuf
9543 + elfsym->destshndx_index));
9544 }
9545
9546 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9547 pos = hdr->sh_offset + hdr->sh_size;
9548 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9549 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9550 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9551 {
9552 hdr->sh_size += amt;
9553 ret = TRUE;
9554 }
9555 else
9556 ret = FALSE;
9557
9558 free (symbuf);
9559
9560 free (hash_table->strtab);
9561 hash_table->strtab = NULL;
9562
9563 return ret;
9564 }
9565
9566 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9567
9568 static bfd_boolean
9569 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9570 {
9571 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9572 && sym->st_shndx < SHN_LORESERVE)
9573 {
9574 /* The gABI doesn't support dynamic symbols in output sections
9575 beyond 64k. */
9576 _bfd_error_handler
9577 /* xgettext:c-format */
9578 (_("%pB: too many sections: %d (>= %d)"),
9579 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9580 bfd_set_error (bfd_error_nonrepresentable_section);
9581 return FALSE;
9582 }
9583 return TRUE;
9584 }
9585
9586 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9587 allowing an unsatisfied unversioned symbol in the DSO to match a
9588 versioned symbol that would normally require an explicit version.
9589 We also handle the case that a DSO references a hidden symbol
9590 which may be satisfied by a versioned symbol in another DSO. */
9591
9592 static bfd_boolean
9593 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9594 const struct elf_backend_data *bed,
9595 struct elf_link_hash_entry *h)
9596 {
9597 bfd *abfd;
9598 struct elf_link_loaded_list *loaded;
9599
9600 if (!is_elf_hash_table (info->hash))
9601 return FALSE;
9602
9603 /* Check indirect symbol. */
9604 while (h->root.type == bfd_link_hash_indirect)
9605 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9606
9607 switch (h->root.type)
9608 {
9609 default:
9610 abfd = NULL;
9611 break;
9612
9613 case bfd_link_hash_undefined:
9614 case bfd_link_hash_undefweak:
9615 abfd = h->root.u.undef.abfd;
9616 if (abfd == NULL
9617 || (abfd->flags & DYNAMIC) == 0
9618 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9619 return FALSE;
9620 break;
9621
9622 case bfd_link_hash_defined:
9623 case bfd_link_hash_defweak:
9624 abfd = h->root.u.def.section->owner;
9625 break;
9626
9627 case bfd_link_hash_common:
9628 abfd = h->root.u.c.p->section->owner;
9629 break;
9630 }
9631 BFD_ASSERT (abfd != NULL);
9632
9633 for (loaded = elf_hash_table (info)->loaded;
9634 loaded != NULL;
9635 loaded = loaded->next)
9636 {
9637 bfd *input;
9638 Elf_Internal_Shdr *hdr;
9639 size_t symcount;
9640 size_t extsymcount;
9641 size_t extsymoff;
9642 Elf_Internal_Shdr *versymhdr;
9643 Elf_Internal_Sym *isym;
9644 Elf_Internal_Sym *isymend;
9645 Elf_Internal_Sym *isymbuf;
9646 Elf_External_Versym *ever;
9647 Elf_External_Versym *extversym;
9648
9649 input = loaded->abfd;
9650
9651 /* We check each DSO for a possible hidden versioned definition. */
9652 if (input == abfd
9653 || (input->flags & DYNAMIC) == 0
9654 || elf_dynversym (input) == 0)
9655 continue;
9656
9657 hdr = &elf_tdata (input)->dynsymtab_hdr;
9658
9659 symcount = hdr->sh_size / bed->s->sizeof_sym;
9660 if (elf_bad_symtab (input))
9661 {
9662 extsymcount = symcount;
9663 extsymoff = 0;
9664 }
9665 else
9666 {
9667 extsymcount = symcount - hdr->sh_info;
9668 extsymoff = hdr->sh_info;
9669 }
9670
9671 if (extsymcount == 0)
9672 continue;
9673
9674 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9675 NULL, NULL, NULL);
9676 if (isymbuf == NULL)
9677 return FALSE;
9678
9679 /* Read in any version definitions. */
9680 versymhdr = &elf_tdata (input)->dynversym_hdr;
9681 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
9682 if (extversym == NULL)
9683 goto error_ret;
9684
9685 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9686 || (bfd_bread (extversym, versymhdr->sh_size, input)
9687 != versymhdr->sh_size))
9688 {
9689 free (extversym);
9690 error_ret:
9691 free (isymbuf);
9692 return FALSE;
9693 }
9694
9695 ever = extversym + extsymoff;
9696 isymend = isymbuf + extsymcount;
9697 for (isym = isymbuf; isym < isymend; isym++, ever++)
9698 {
9699 const char *name;
9700 Elf_Internal_Versym iver;
9701 unsigned short version_index;
9702
9703 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9704 || isym->st_shndx == SHN_UNDEF)
9705 continue;
9706
9707 name = bfd_elf_string_from_elf_section (input,
9708 hdr->sh_link,
9709 isym->st_name);
9710 if (strcmp (name, h->root.root.string) != 0)
9711 continue;
9712
9713 _bfd_elf_swap_versym_in (input, ever, &iver);
9714
9715 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9716 && !(h->def_regular
9717 && h->forced_local))
9718 {
9719 /* If we have a non-hidden versioned sym, then it should
9720 have provided a definition for the undefined sym unless
9721 it is defined in a non-shared object and forced local.
9722 */
9723 abort ();
9724 }
9725
9726 version_index = iver.vs_vers & VERSYM_VERSION;
9727 if (version_index == 1 || version_index == 2)
9728 {
9729 /* This is the base or first version. We can use it. */
9730 free (extversym);
9731 free (isymbuf);
9732 return TRUE;
9733 }
9734 }
9735
9736 free (extversym);
9737 free (isymbuf);
9738 }
9739
9740 return FALSE;
9741 }
9742
9743 /* Convert ELF common symbol TYPE. */
9744
9745 static int
9746 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9747 {
9748 /* Commom symbol can only appear in relocatable link. */
9749 if (!bfd_link_relocatable (info))
9750 abort ();
9751 switch (info->elf_stt_common)
9752 {
9753 case unchanged:
9754 break;
9755 case elf_stt_common:
9756 type = STT_COMMON;
9757 break;
9758 case no_elf_stt_common:
9759 type = STT_OBJECT;
9760 break;
9761 }
9762 return type;
9763 }
9764
9765 /* Add an external symbol to the symbol table. This is called from
9766 the hash table traversal routine. When generating a shared object,
9767 we go through the symbol table twice. The first time we output
9768 anything that might have been forced to local scope in a version
9769 script. The second time we output the symbols that are still
9770 global symbols. */
9771
9772 static bfd_boolean
9773 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9774 {
9775 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9776 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9777 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9778 bfd_boolean strip;
9779 Elf_Internal_Sym sym;
9780 asection *input_sec;
9781 const struct elf_backend_data *bed;
9782 long indx;
9783 int ret;
9784 unsigned int type;
9785
9786 if (h->root.type == bfd_link_hash_warning)
9787 {
9788 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9789 if (h->root.type == bfd_link_hash_new)
9790 return TRUE;
9791 }
9792
9793 /* Decide whether to output this symbol in this pass. */
9794 if (eoinfo->localsyms)
9795 {
9796 if (!h->forced_local)
9797 return TRUE;
9798 }
9799 else
9800 {
9801 if (h->forced_local)
9802 return TRUE;
9803 }
9804
9805 bed = get_elf_backend_data (flinfo->output_bfd);
9806
9807 if (h->root.type == bfd_link_hash_undefined)
9808 {
9809 /* If we have an undefined symbol reference here then it must have
9810 come from a shared library that is being linked in. (Undefined
9811 references in regular files have already been handled unless
9812 they are in unreferenced sections which are removed by garbage
9813 collection). */
9814 bfd_boolean ignore_undef = FALSE;
9815
9816 /* Some symbols may be special in that the fact that they're
9817 undefined can be safely ignored - let backend determine that. */
9818 if (bed->elf_backend_ignore_undef_symbol)
9819 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9820
9821 /* If we are reporting errors for this situation then do so now. */
9822 if (!ignore_undef
9823 && h->ref_dynamic_nonweak
9824 && (!h->ref_regular || flinfo->info->gc_sections)
9825 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9826 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9827 (*flinfo->info->callbacks->undefined_symbol)
9828 (flinfo->info, h->root.root.string,
9829 h->ref_regular ? NULL : h->root.u.undef.abfd,
9830 NULL, 0,
9831 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
9832
9833 /* Strip a global symbol defined in a discarded section. */
9834 if (h->indx == -3)
9835 return TRUE;
9836 }
9837
9838 /* We should also warn if a forced local symbol is referenced from
9839 shared libraries. */
9840 if (bfd_link_executable (flinfo->info)
9841 && h->forced_local
9842 && h->ref_dynamic
9843 && h->def_regular
9844 && !h->dynamic_def
9845 && h->ref_dynamic_nonweak
9846 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9847 {
9848 bfd *def_bfd;
9849 const char *msg;
9850 struct elf_link_hash_entry *hi = h;
9851
9852 /* Check indirect symbol. */
9853 while (hi->root.type == bfd_link_hash_indirect)
9854 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9855
9856 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9857 /* xgettext:c-format */
9858 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
9859 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9860 /* xgettext:c-format */
9861 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
9862 else
9863 /* xgettext:c-format */
9864 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
9865 def_bfd = flinfo->output_bfd;
9866 if (hi->root.u.def.section != bfd_abs_section_ptr)
9867 def_bfd = hi->root.u.def.section->owner;
9868 _bfd_error_handler (msg, flinfo->output_bfd,
9869 h->root.root.string, def_bfd);
9870 bfd_set_error (bfd_error_bad_value);
9871 eoinfo->failed = TRUE;
9872 return FALSE;
9873 }
9874
9875 /* We don't want to output symbols that have never been mentioned by
9876 a regular file, or that we have been told to strip. However, if
9877 h->indx is set to -2, the symbol is used by a reloc and we must
9878 output it. */
9879 strip = FALSE;
9880 if (h->indx == -2)
9881 ;
9882 else if ((h->def_dynamic
9883 || h->ref_dynamic
9884 || h->root.type == bfd_link_hash_new)
9885 && !h->def_regular
9886 && !h->ref_regular)
9887 strip = TRUE;
9888 else if (flinfo->info->strip == strip_all)
9889 strip = TRUE;
9890 else if (flinfo->info->strip == strip_some
9891 && bfd_hash_lookup (flinfo->info->keep_hash,
9892 h->root.root.string, FALSE, FALSE) == NULL)
9893 strip = TRUE;
9894 else if ((h->root.type == bfd_link_hash_defined
9895 || h->root.type == bfd_link_hash_defweak)
9896 && ((flinfo->info->strip_discarded
9897 && discarded_section (h->root.u.def.section))
9898 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9899 && h->root.u.def.section->owner != NULL
9900 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9901 strip = TRUE;
9902 else if ((h->root.type == bfd_link_hash_undefined
9903 || h->root.type == bfd_link_hash_undefweak)
9904 && h->root.u.undef.abfd != NULL
9905 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9906 strip = TRUE;
9907
9908 type = h->type;
9909
9910 /* If we're stripping it, and it's not a dynamic symbol, there's
9911 nothing else to do. However, if it is a forced local symbol or
9912 an ifunc symbol we need to give the backend finish_dynamic_symbol
9913 function a chance to make it dynamic. */
9914 if (strip
9915 && h->dynindx == -1
9916 && type != STT_GNU_IFUNC
9917 && !h->forced_local)
9918 return TRUE;
9919
9920 sym.st_value = 0;
9921 sym.st_size = h->size;
9922 sym.st_other = h->other;
9923 switch (h->root.type)
9924 {
9925 default:
9926 case bfd_link_hash_new:
9927 case bfd_link_hash_warning:
9928 abort ();
9929 return FALSE;
9930
9931 case bfd_link_hash_undefined:
9932 case bfd_link_hash_undefweak:
9933 input_sec = bfd_und_section_ptr;
9934 sym.st_shndx = SHN_UNDEF;
9935 break;
9936
9937 case bfd_link_hash_defined:
9938 case bfd_link_hash_defweak:
9939 {
9940 input_sec = h->root.u.def.section;
9941 if (input_sec->output_section != NULL)
9942 {
9943 sym.st_shndx =
9944 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9945 input_sec->output_section);
9946 if (sym.st_shndx == SHN_BAD)
9947 {
9948 _bfd_error_handler
9949 /* xgettext:c-format */
9950 (_("%pB: could not find output section %pA for input section %pA"),
9951 flinfo->output_bfd, input_sec->output_section, input_sec);
9952 bfd_set_error (bfd_error_nonrepresentable_section);
9953 eoinfo->failed = TRUE;
9954 return FALSE;
9955 }
9956
9957 /* ELF symbols in relocatable files are section relative,
9958 but in nonrelocatable files they are virtual
9959 addresses. */
9960 sym.st_value = h->root.u.def.value + input_sec->output_offset;
9961 if (!bfd_link_relocatable (flinfo->info))
9962 {
9963 sym.st_value += input_sec->output_section->vma;
9964 if (h->type == STT_TLS)
9965 {
9966 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9967 if (tls_sec != NULL)
9968 sym.st_value -= tls_sec->vma;
9969 }
9970 }
9971 }
9972 else
9973 {
9974 BFD_ASSERT (input_sec->owner == NULL
9975 || (input_sec->owner->flags & DYNAMIC) != 0);
9976 sym.st_shndx = SHN_UNDEF;
9977 input_sec = bfd_und_section_ptr;
9978 }
9979 }
9980 break;
9981
9982 case bfd_link_hash_common:
9983 input_sec = h->root.u.c.p->section;
9984 sym.st_shndx = bed->common_section_index (input_sec);
9985 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9986 break;
9987
9988 case bfd_link_hash_indirect:
9989 /* These symbols are created by symbol versioning. They point
9990 to the decorated version of the name. For example, if the
9991 symbol foo@@GNU_1.2 is the default, which should be used when
9992 foo is used with no version, then we add an indirect symbol
9993 foo which points to foo@@GNU_1.2. We ignore these symbols,
9994 since the indirected symbol is already in the hash table. */
9995 return TRUE;
9996 }
9997
9998 if (type == STT_COMMON || type == STT_OBJECT)
9999 switch (h->root.type)
10000 {
10001 case bfd_link_hash_common:
10002 type = elf_link_convert_common_type (flinfo->info, type);
10003 break;
10004 case bfd_link_hash_defined:
10005 case bfd_link_hash_defweak:
10006 if (bed->common_definition (&sym))
10007 type = elf_link_convert_common_type (flinfo->info, type);
10008 else
10009 type = STT_OBJECT;
10010 break;
10011 case bfd_link_hash_undefined:
10012 case bfd_link_hash_undefweak:
10013 break;
10014 default:
10015 abort ();
10016 }
10017
10018 if (h->forced_local)
10019 {
10020 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10021 /* Turn off visibility on local symbol. */
10022 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10023 }
10024 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10025 else if (h->unique_global && h->def_regular)
10026 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10027 else if (h->root.type == bfd_link_hash_undefweak
10028 || h->root.type == bfd_link_hash_defweak)
10029 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10030 else
10031 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10032 sym.st_target_internal = h->target_internal;
10033
10034 /* Give the processor backend a chance to tweak the symbol value,
10035 and also to finish up anything that needs to be done for this
10036 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10037 forced local syms when non-shared is due to a historical quirk.
10038 STT_GNU_IFUNC symbol must go through PLT. */
10039 if ((h->type == STT_GNU_IFUNC
10040 && h->def_regular
10041 && !bfd_link_relocatable (flinfo->info))
10042 || ((h->dynindx != -1
10043 || h->forced_local)
10044 && ((bfd_link_pic (flinfo->info)
10045 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10046 || h->root.type != bfd_link_hash_undefweak))
10047 || !h->forced_local)
10048 && elf_hash_table (flinfo->info)->dynamic_sections_created))
10049 {
10050 if (! ((*bed->elf_backend_finish_dynamic_symbol)
10051 (flinfo->output_bfd, flinfo->info, h, &sym)))
10052 {
10053 eoinfo->failed = TRUE;
10054 return FALSE;
10055 }
10056 }
10057
10058 /* If we are marking the symbol as undefined, and there are no
10059 non-weak references to this symbol from a regular object, then
10060 mark the symbol as weak undefined; if there are non-weak
10061 references, mark the symbol as strong. We can't do this earlier,
10062 because it might not be marked as undefined until the
10063 finish_dynamic_symbol routine gets through with it. */
10064 if (sym.st_shndx == SHN_UNDEF
10065 && h->ref_regular
10066 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10067 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10068 {
10069 int bindtype;
10070 type = ELF_ST_TYPE (sym.st_info);
10071
10072 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10073 if (type == STT_GNU_IFUNC)
10074 type = STT_FUNC;
10075
10076 if (h->ref_regular_nonweak)
10077 bindtype = STB_GLOBAL;
10078 else
10079 bindtype = STB_WEAK;
10080 sym.st_info = ELF_ST_INFO (bindtype, type);
10081 }
10082
10083 /* If this is a symbol defined in a dynamic library, don't use the
10084 symbol size from the dynamic library. Relinking an executable
10085 against a new library may introduce gratuitous changes in the
10086 executable's symbols if we keep the size. */
10087 if (sym.st_shndx == SHN_UNDEF
10088 && !h->def_regular
10089 && h->def_dynamic)
10090 sym.st_size = 0;
10091
10092 /* If a non-weak symbol with non-default visibility is not defined
10093 locally, it is a fatal error. */
10094 if (!bfd_link_relocatable (flinfo->info)
10095 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10096 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10097 && h->root.type == bfd_link_hash_undefined
10098 && !h->def_regular)
10099 {
10100 const char *msg;
10101
10102 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10103 /* xgettext:c-format */
10104 msg = _("%pB: protected symbol `%s' isn't defined");
10105 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10106 /* xgettext:c-format */
10107 msg = _("%pB: internal symbol `%s' isn't defined");
10108 else
10109 /* xgettext:c-format */
10110 msg = _("%pB: hidden symbol `%s' isn't defined");
10111 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10112 bfd_set_error (bfd_error_bad_value);
10113 eoinfo->failed = TRUE;
10114 return FALSE;
10115 }
10116
10117 /* If this symbol should be put in the .dynsym section, then put it
10118 there now. We already know the symbol index. We also fill in
10119 the entry in the .hash section. */
10120 if (h->dynindx != -1
10121 && elf_hash_table (flinfo->info)->dynamic_sections_created
10122 && elf_hash_table (flinfo->info)->dynsym != NULL
10123 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
10124 {
10125 bfd_byte *esym;
10126
10127 /* Since there is no version information in the dynamic string,
10128 if there is no version info in symbol version section, we will
10129 have a run-time problem if not linking executable, referenced
10130 by shared library, or not bound locally. */
10131 if (h->verinfo.verdef == NULL
10132 && (!bfd_link_executable (flinfo->info)
10133 || h->ref_dynamic
10134 || !h->def_regular))
10135 {
10136 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10137
10138 if (p && p [1] != '\0')
10139 {
10140 _bfd_error_handler
10141 /* xgettext:c-format */
10142 (_("%pB: no symbol version section for versioned symbol `%s'"),
10143 flinfo->output_bfd, h->root.root.string);
10144 eoinfo->failed = TRUE;
10145 return FALSE;
10146 }
10147 }
10148
10149 sym.st_name = h->dynstr_index;
10150 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10151 + h->dynindx * bed->s->sizeof_sym);
10152 if (!check_dynsym (flinfo->output_bfd, &sym))
10153 {
10154 eoinfo->failed = TRUE;
10155 return FALSE;
10156 }
10157 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
10158
10159 if (flinfo->hash_sec != NULL)
10160 {
10161 size_t hash_entry_size;
10162 bfd_byte *bucketpos;
10163 bfd_vma chain;
10164 size_t bucketcount;
10165 size_t bucket;
10166
10167 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
10168 bucket = h->u.elf_hash_value % bucketcount;
10169
10170 hash_entry_size
10171 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10172 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
10173 + (bucket + 2) * hash_entry_size);
10174 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10175 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10176 bucketpos);
10177 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10178 ((bfd_byte *) flinfo->hash_sec->contents
10179 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10180 }
10181
10182 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
10183 {
10184 Elf_Internal_Versym iversym;
10185 Elf_External_Versym *eversym;
10186
10187 if (!h->def_regular)
10188 {
10189 if (h->verinfo.verdef == NULL
10190 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10191 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
10192 iversym.vs_vers = 0;
10193 else
10194 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10195 }
10196 else
10197 {
10198 if (h->verinfo.vertree == NULL)
10199 iversym.vs_vers = 1;
10200 else
10201 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
10202 if (flinfo->info->create_default_symver)
10203 iversym.vs_vers++;
10204 }
10205
10206 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10207 defined locally. */
10208 if (h->versioned == versioned_hidden && h->def_regular)
10209 iversym.vs_vers |= VERSYM_HIDDEN;
10210
10211 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10212 eversym += h->dynindx;
10213 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10214 }
10215 }
10216
10217 /* If the symbol is undefined, and we didn't output it to .dynsym,
10218 strip it from .symtab too. Obviously we can't do this for
10219 relocatable output or when needed for --emit-relocs. */
10220 else if (input_sec == bfd_und_section_ptr
10221 && h->indx != -2
10222 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10223 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10224 && !bfd_link_relocatable (flinfo->info))
10225 return TRUE;
10226
10227 /* Also strip others that we couldn't earlier due to dynamic symbol
10228 processing. */
10229 if (strip)
10230 return TRUE;
10231 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10232 return TRUE;
10233
10234 /* Output a FILE symbol so that following locals are not associated
10235 with the wrong input file. We need one for forced local symbols
10236 if we've seen more than one FILE symbol or when we have exactly
10237 one FILE symbol but global symbols are present in a file other
10238 than the one with the FILE symbol. We also need one if linker
10239 defined symbols are present. In practice these conditions are
10240 always met, so just emit the FILE symbol unconditionally. */
10241 if (eoinfo->localsyms
10242 && !eoinfo->file_sym_done
10243 && eoinfo->flinfo->filesym_count != 0)
10244 {
10245 Elf_Internal_Sym fsym;
10246
10247 memset (&fsym, 0, sizeof (fsym));
10248 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10249 fsym.st_shndx = SHN_ABS;
10250 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10251 bfd_und_section_ptr, NULL))
10252 return FALSE;
10253
10254 eoinfo->file_sym_done = TRUE;
10255 }
10256
10257 indx = bfd_get_symcount (flinfo->output_bfd);
10258 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10259 input_sec, h);
10260 if (ret == 0)
10261 {
10262 eoinfo->failed = TRUE;
10263 return FALSE;
10264 }
10265 else if (ret == 1)
10266 h->indx = indx;
10267 else if (h->indx == -2)
10268 abort();
10269
10270 return TRUE;
10271 }
10272
10273 /* Return TRUE if special handling is done for relocs in SEC against
10274 symbols defined in discarded sections. */
10275
10276 static bfd_boolean
10277 elf_section_ignore_discarded_relocs (asection *sec)
10278 {
10279 const struct elf_backend_data *bed;
10280
10281 switch (sec->sec_info_type)
10282 {
10283 case SEC_INFO_TYPE_STABS:
10284 case SEC_INFO_TYPE_EH_FRAME:
10285 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10286 return TRUE;
10287 default:
10288 break;
10289 }
10290
10291 bed = get_elf_backend_data (sec->owner);
10292 if (bed->elf_backend_ignore_discarded_relocs != NULL
10293 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10294 return TRUE;
10295
10296 return FALSE;
10297 }
10298
10299 /* Return a mask saying how ld should treat relocations in SEC against
10300 symbols defined in discarded sections. If this function returns
10301 COMPLAIN set, ld will issue a warning message. If this function
10302 returns PRETEND set, and the discarded section was link-once and the
10303 same size as the kept link-once section, ld will pretend that the
10304 symbol was actually defined in the kept section. Otherwise ld will
10305 zero the reloc (at least that is the intent, but some cooperation by
10306 the target dependent code is needed, particularly for REL targets). */
10307
10308 unsigned int
10309 _bfd_elf_default_action_discarded (asection *sec)
10310 {
10311 if (sec->flags & SEC_DEBUGGING)
10312 return PRETEND;
10313
10314 if (strcmp (".eh_frame", sec->name) == 0)
10315 return 0;
10316
10317 if (strcmp (".gcc_except_table", sec->name) == 0)
10318 return 0;
10319
10320 return COMPLAIN | PRETEND;
10321 }
10322
10323 /* Find a match between a section and a member of a section group. */
10324
10325 static asection *
10326 match_group_member (asection *sec, asection *group,
10327 struct bfd_link_info *info)
10328 {
10329 asection *first = elf_next_in_group (group);
10330 asection *s = first;
10331
10332 while (s != NULL)
10333 {
10334 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10335 return s;
10336
10337 s = elf_next_in_group (s);
10338 if (s == first)
10339 break;
10340 }
10341
10342 return NULL;
10343 }
10344
10345 /* Check if the kept section of a discarded section SEC can be used
10346 to replace it. Return the replacement if it is OK. Otherwise return
10347 NULL. */
10348
10349 asection *
10350 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10351 {
10352 asection *kept;
10353
10354 kept = sec->kept_section;
10355 if (kept != NULL)
10356 {
10357 if ((kept->flags & SEC_GROUP) != 0)
10358 kept = match_group_member (sec, kept, info);
10359 if (kept != NULL
10360 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10361 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
10362 kept = NULL;
10363 sec->kept_section = kept;
10364 }
10365 return kept;
10366 }
10367
10368 /* Link an input file into the linker output file. This function
10369 handles all the sections and relocations of the input file at once.
10370 This is so that we only have to read the local symbols once, and
10371 don't have to keep them in memory. */
10372
10373 static bfd_boolean
10374 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
10375 {
10376 int (*relocate_section)
10377 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10378 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10379 bfd *output_bfd;
10380 Elf_Internal_Shdr *symtab_hdr;
10381 size_t locsymcount;
10382 size_t extsymoff;
10383 Elf_Internal_Sym *isymbuf;
10384 Elf_Internal_Sym *isym;
10385 Elf_Internal_Sym *isymend;
10386 long *pindex;
10387 asection **ppsection;
10388 asection *o;
10389 const struct elf_backend_data *bed;
10390 struct elf_link_hash_entry **sym_hashes;
10391 bfd_size_type address_size;
10392 bfd_vma r_type_mask;
10393 int r_sym_shift;
10394 bfd_boolean have_file_sym = FALSE;
10395
10396 output_bfd = flinfo->output_bfd;
10397 bed = get_elf_backend_data (output_bfd);
10398 relocate_section = bed->elf_backend_relocate_section;
10399
10400 /* If this is a dynamic object, we don't want to do anything here:
10401 we don't want the local symbols, and we don't want the section
10402 contents. */
10403 if ((input_bfd->flags & DYNAMIC) != 0)
10404 return TRUE;
10405
10406 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10407 if (elf_bad_symtab (input_bfd))
10408 {
10409 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10410 extsymoff = 0;
10411 }
10412 else
10413 {
10414 locsymcount = symtab_hdr->sh_info;
10415 extsymoff = symtab_hdr->sh_info;
10416 }
10417
10418 /* Read the local symbols. */
10419 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10420 if (isymbuf == NULL && locsymcount != 0)
10421 {
10422 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
10423 flinfo->internal_syms,
10424 flinfo->external_syms,
10425 flinfo->locsym_shndx);
10426 if (isymbuf == NULL)
10427 return FALSE;
10428 }
10429
10430 /* Find local symbol sections and adjust values of symbols in
10431 SEC_MERGE sections. Write out those local symbols we know are
10432 going into the output file. */
10433 isymend = isymbuf + locsymcount;
10434 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
10435 isym < isymend;
10436 isym++, pindex++, ppsection++)
10437 {
10438 asection *isec;
10439 const char *name;
10440 Elf_Internal_Sym osym;
10441 long indx;
10442 int ret;
10443
10444 *pindex = -1;
10445
10446 if (elf_bad_symtab (input_bfd))
10447 {
10448 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10449 {
10450 *ppsection = NULL;
10451 continue;
10452 }
10453 }
10454
10455 if (isym->st_shndx == SHN_UNDEF)
10456 isec = bfd_und_section_ptr;
10457 else if (isym->st_shndx == SHN_ABS)
10458 isec = bfd_abs_section_ptr;
10459 else if (isym->st_shndx == SHN_COMMON)
10460 isec = bfd_com_section_ptr;
10461 else
10462 {
10463 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10464 if (isec == NULL)
10465 {
10466 /* Don't attempt to output symbols with st_shnx in the
10467 reserved range other than SHN_ABS and SHN_COMMON. */
10468 isec = bfd_und_section_ptr;
10469 }
10470 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
10471 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10472 isym->st_value =
10473 _bfd_merged_section_offset (output_bfd, &isec,
10474 elf_section_data (isec)->sec_info,
10475 isym->st_value);
10476 }
10477
10478 *ppsection = isec;
10479
10480 /* Don't output the first, undefined, symbol. In fact, don't
10481 output any undefined local symbol. */
10482 if (isec == bfd_und_section_ptr)
10483 continue;
10484
10485 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10486 {
10487 /* We never output section symbols. Instead, we use the
10488 section symbol of the corresponding section in the output
10489 file. */
10490 continue;
10491 }
10492
10493 /* If we are stripping all symbols, we don't want to output this
10494 one. */
10495 if (flinfo->info->strip == strip_all)
10496 continue;
10497
10498 /* If we are discarding all local symbols, we don't want to
10499 output this one. If we are generating a relocatable output
10500 file, then some of the local symbols may be required by
10501 relocs; we output them below as we discover that they are
10502 needed. */
10503 if (flinfo->info->discard == discard_all)
10504 continue;
10505
10506 /* If this symbol is defined in a section which we are
10507 discarding, we don't need to keep it. */
10508 if (isym->st_shndx != SHN_UNDEF
10509 && isym->st_shndx < SHN_LORESERVE
10510 && bfd_section_removed_from_list (output_bfd,
10511 isec->output_section))
10512 continue;
10513
10514 /* Get the name of the symbol. */
10515 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10516 isym->st_name);
10517 if (name == NULL)
10518 return FALSE;
10519
10520 /* See if we are discarding symbols with this name. */
10521 if ((flinfo->info->strip == strip_some
10522 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
10523 == NULL))
10524 || (((flinfo->info->discard == discard_sec_merge
10525 && (isec->flags & SEC_MERGE)
10526 && !bfd_link_relocatable (flinfo->info))
10527 || flinfo->info->discard == discard_l)
10528 && bfd_is_local_label_name (input_bfd, name)))
10529 continue;
10530
10531 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10532 {
10533 if (input_bfd->lto_output)
10534 /* -flto puts a temp file name here. This means builds
10535 are not reproducible. Discard the symbol. */
10536 continue;
10537 have_file_sym = TRUE;
10538 flinfo->filesym_count += 1;
10539 }
10540 if (!have_file_sym)
10541 {
10542 /* In the absence of debug info, bfd_find_nearest_line uses
10543 FILE symbols to determine the source file for local
10544 function symbols. Provide a FILE symbol here if input
10545 files lack such, so that their symbols won't be
10546 associated with a previous input file. It's not the
10547 source file, but the best we can do. */
10548 have_file_sym = TRUE;
10549 flinfo->filesym_count += 1;
10550 memset (&osym, 0, sizeof (osym));
10551 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10552 osym.st_shndx = SHN_ABS;
10553 if (!elf_link_output_symstrtab (flinfo,
10554 (input_bfd->lto_output ? NULL
10555 : input_bfd->filename),
10556 &osym, bfd_abs_section_ptr,
10557 NULL))
10558 return FALSE;
10559 }
10560
10561 osym = *isym;
10562
10563 /* Adjust the section index for the output file. */
10564 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10565 isec->output_section);
10566 if (osym.st_shndx == SHN_BAD)
10567 return FALSE;
10568
10569 /* ELF symbols in relocatable files are section relative, but
10570 in executable files they are virtual addresses. Note that
10571 this code assumes that all ELF sections have an associated
10572 BFD section with a reasonable value for output_offset; below
10573 we assume that they also have a reasonable value for
10574 output_section. Any special sections must be set up to meet
10575 these requirements. */
10576 osym.st_value += isec->output_offset;
10577 if (!bfd_link_relocatable (flinfo->info))
10578 {
10579 osym.st_value += isec->output_section->vma;
10580 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10581 {
10582 /* STT_TLS symbols are relative to PT_TLS segment base. */
10583 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
10584 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10585 else
10586 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
10587 STT_NOTYPE);
10588 }
10589 }
10590
10591 indx = bfd_get_symcount (output_bfd);
10592 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10593 if (ret == 0)
10594 return FALSE;
10595 else if (ret == 1)
10596 *pindex = indx;
10597 }
10598
10599 if (bed->s->arch_size == 32)
10600 {
10601 r_type_mask = 0xff;
10602 r_sym_shift = 8;
10603 address_size = 4;
10604 }
10605 else
10606 {
10607 r_type_mask = 0xffffffff;
10608 r_sym_shift = 32;
10609 address_size = 8;
10610 }
10611
10612 /* Relocate the contents of each section. */
10613 sym_hashes = elf_sym_hashes (input_bfd);
10614 for (o = input_bfd->sections; o != NULL; o = o->next)
10615 {
10616 bfd_byte *contents;
10617
10618 if (! o->linker_mark)
10619 {
10620 /* This section was omitted from the link. */
10621 continue;
10622 }
10623
10624 if (!flinfo->info->resolve_section_groups
10625 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10626 {
10627 /* Deal with the group signature symbol. */
10628 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10629 unsigned long symndx = sec_data->this_hdr.sh_info;
10630 asection *osec = o->output_section;
10631
10632 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
10633 if (symndx >= locsymcount
10634 || (elf_bad_symtab (input_bfd)
10635 && flinfo->sections[symndx] == NULL))
10636 {
10637 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10638 while (h->root.type == bfd_link_hash_indirect
10639 || h->root.type == bfd_link_hash_warning)
10640 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10641 /* Arrange for symbol to be output. */
10642 h->indx = -2;
10643 elf_section_data (osec)->this_hdr.sh_info = -2;
10644 }
10645 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10646 {
10647 /* We'll use the output section target_index. */
10648 asection *sec = flinfo->sections[symndx]->output_section;
10649 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10650 }
10651 else
10652 {
10653 if (flinfo->indices[symndx] == -1)
10654 {
10655 /* Otherwise output the local symbol now. */
10656 Elf_Internal_Sym sym = isymbuf[symndx];
10657 asection *sec = flinfo->sections[symndx]->output_section;
10658 const char *name;
10659 long indx;
10660 int ret;
10661
10662 name = bfd_elf_string_from_elf_section (input_bfd,
10663 symtab_hdr->sh_link,
10664 sym.st_name);
10665 if (name == NULL)
10666 return FALSE;
10667
10668 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10669 sec);
10670 if (sym.st_shndx == SHN_BAD)
10671 return FALSE;
10672
10673 sym.st_value += o->output_offset;
10674
10675 indx = bfd_get_symcount (output_bfd);
10676 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10677 NULL);
10678 if (ret == 0)
10679 return FALSE;
10680 else if (ret == 1)
10681 flinfo->indices[symndx] = indx;
10682 else
10683 abort ();
10684 }
10685 elf_section_data (osec)->this_hdr.sh_info
10686 = flinfo->indices[symndx];
10687 }
10688 }
10689
10690 if ((o->flags & SEC_HAS_CONTENTS) == 0
10691 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10692 continue;
10693
10694 if ((o->flags & SEC_LINKER_CREATED) != 0)
10695 {
10696 /* Section was created by _bfd_elf_link_create_dynamic_sections
10697 or somesuch. */
10698 continue;
10699 }
10700
10701 /* Get the contents of the section. They have been cached by a
10702 relaxation routine. Note that o is a section in an input
10703 file, so the contents field will not have been set by any of
10704 the routines which work on output files. */
10705 if (elf_section_data (o)->this_hdr.contents != NULL)
10706 {
10707 contents = elf_section_data (o)->this_hdr.contents;
10708 if (bed->caches_rawsize
10709 && o->rawsize != 0
10710 && o->rawsize < o->size)
10711 {
10712 memcpy (flinfo->contents, contents, o->rawsize);
10713 contents = flinfo->contents;
10714 }
10715 }
10716 else
10717 {
10718 contents = flinfo->contents;
10719 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10720 return FALSE;
10721 }
10722
10723 if ((o->flags & SEC_RELOC) != 0)
10724 {
10725 Elf_Internal_Rela *internal_relocs;
10726 Elf_Internal_Rela *rel, *relend;
10727 int action_discarded;
10728 int ret;
10729
10730 /* Get the swapped relocs. */
10731 internal_relocs
10732 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10733 flinfo->internal_relocs, FALSE);
10734 if (internal_relocs == NULL
10735 && o->reloc_count > 0)
10736 return FALSE;
10737
10738 /* We need to reverse-copy input .ctors/.dtors sections if
10739 they are placed in .init_array/.finit_array for output. */
10740 if (o->size > address_size
10741 && ((strncmp (o->name, ".ctors", 6) == 0
10742 && strcmp (o->output_section->name,
10743 ".init_array") == 0)
10744 || (strncmp (o->name, ".dtors", 6) == 0
10745 && strcmp (o->output_section->name,
10746 ".fini_array") == 0))
10747 && (o->name[6] == 0 || o->name[6] == '.'))
10748 {
10749 if (o->size * bed->s->int_rels_per_ext_rel
10750 != o->reloc_count * address_size)
10751 {
10752 _bfd_error_handler
10753 /* xgettext:c-format */
10754 (_("error: %pB: size of section %pA is not "
10755 "multiple of address size"),
10756 input_bfd, o);
10757 bfd_set_error (bfd_error_bad_value);
10758 return FALSE;
10759 }
10760 o->flags |= SEC_ELF_REVERSE_COPY;
10761 }
10762
10763 action_discarded = -1;
10764 if (!elf_section_ignore_discarded_relocs (o))
10765 action_discarded = (*bed->action_discarded) (o);
10766
10767 /* Run through the relocs evaluating complex reloc symbols and
10768 looking for relocs against symbols from discarded sections
10769 or section symbols from removed link-once sections.
10770 Complain about relocs against discarded sections. Zero
10771 relocs against removed link-once sections. */
10772
10773 rel = internal_relocs;
10774 relend = rel + o->reloc_count;
10775 for ( ; rel < relend; rel++)
10776 {
10777 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10778 unsigned int s_type;
10779 asection **ps, *sec;
10780 struct elf_link_hash_entry *h = NULL;
10781 const char *sym_name;
10782
10783 if (r_symndx == STN_UNDEF)
10784 continue;
10785
10786 if (r_symndx >= locsymcount
10787 || (elf_bad_symtab (input_bfd)
10788 && flinfo->sections[r_symndx] == NULL))
10789 {
10790 h = sym_hashes[r_symndx - extsymoff];
10791
10792 /* Badly formatted input files can contain relocs that
10793 reference non-existant symbols. Check here so that
10794 we do not seg fault. */
10795 if (h == NULL)
10796 {
10797 _bfd_error_handler
10798 /* xgettext:c-format */
10799 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
10800 "that references a non-existent global symbol"),
10801 input_bfd, (uint64_t) rel->r_info, o);
10802 bfd_set_error (bfd_error_bad_value);
10803 return FALSE;
10804 }
10805
10806 while (h->root.type == bfd_link_hash_indirect
10807 || h->root.type == bfd_link_hash_warning)
10808 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10809
10810 s_type = h->type;
10811
10812 /* If a plugin symbol is referenced from a non-IR file,
10813 mark the symbol as undefined. Note that the
10814 linker may attach linker created dynamic sections
10815 to the plugin bfd. Symbols defined in linker
10816 created sections are not plugin symbols. */
10817 if ((h->root.non_ir_ref_regular
10818 || h->root.non_ir_ref_dynamic)
10819 && (h->root.type == bfd_link_hash_defined
10820 || h->root.type == bfd_link_hash_defweak)
10821 && (h->root.u.def.section->flags
10822 & SEC_LINKER_CREATED) == 0
10823 && h->root.u.def.section->owner != NULL
10824 && (h->root.u.def.section->owner->flags
10825 & BFD_PLUGIN) != 0)
10826 {
10827 h->root.type = bfd_link_hash_undefined;
10828 h->root.u.undef.abfd = h->root.u.def.section->owner;
10829 }
10830
10831 ps = NULL;
10832 if (h->root.type == bfd_link_hash_defined
10833 || h->root.type == bfd_link_hash_defweak)
10834 ps = &h->root.u.def.section;
10835
10836 sym_name = h->root.root.string;
10837 }
10838 else
10839 {
10840 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10841
10842 s_type = ELF_ST_TYPE (sym->st_info);
10843 ps = &flinfo->sections[r_symndx];
10844 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10845 sym, *ps);
10846 }
10847
10848 if ((s_type == STT_RELC || s_type == STT_SRELC)
10849 && !bfd_link_relocatable (flinfo->info))
10850 {
10851 bfd_vma val;
10852 bfd_vma dot = (rel->r_offset
10853 + o->output_offset + o->output_section->vma);
10854 #ifdef DEBUG
10855 printf ("Encountered a complex symbol!");
10856 printf (" (input_bfd %s, section %s, reloc %ld\n",
10857 input_bfd->filename, o->name,
10858 (long) (rel - internal_relocs));
10859 printf (" symbol: idx %8.8lx, name %s\n",
10860 r_symndx, sym_name);
10861 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10862 (unsigned long) rel->r_info,
10863 (unsigned long) rel->r_offset);
10864 #endif
10865 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10866 isymbuf, locsymcount, s_type == STT_SRELC))
10867 return FALSE;
10868
10869 /* Symbol evaluated OK. Update to absolute value. */
10870 set_symbol_value (input_bfd, isymbuf, locsymcount,
10871 r_symndx, val);
10872 continue;
10873 }
10874
10875 if (action_discarded != -1 && ps != NULL)
10876 {
10877 /* Complain if the definition comes from a
10878 discarded section. */
10879 if ((sec = *ps) != NULL && discarded_section (sec))
10880 {
10881 BFD_ASSERT (r_symndx != STN_UNDEF);
10882 if (action_discarded & COMPLAIN)
10883 (*flinfo->info->callbacks->einfo)
10884 /* xgettext:c-format */
10885 (_("%X`%s' referenced in section `%pA' of %pB: "
10886 "defined in discarded section `%pA' of %pB\n"),
10887 sym_name, o, input_bfd, sec, sec->owner);
10888
10889 /* Try to do the best we can to support buggy old
10890 versions of gcc. Pretend that the symbol is
10891 really defined in the kept linkonce section.
10892 FIXME: This is quite broken. Modifying the
10893 symbol here means we will be changing all later
10894 uses of the symbol, not just in this section. */
10895 if (action_discarded & PRETEND)
10896 {
10897 asection *kept;
10898
10899 kept = _bfd_elf_check_kept_section (sec,
10900 flinfo->info);
10901 if (kept != NULL)
10902 {
10903 *ps = kept;
10904 continue;
10905 }
10906 }
10907 }
10908 }
10909 }
10910
10911 /* Relocate the section by invoking a back end routine.
10912
10913 The back end routine is responsible for adjusting the
10914 section contents as necessary, and (if using Rela relocs
10915 and generating a relocatable output file) adjusting the
10916 reloc addend as necessary.
10917
10918 The back end routine does not have to worry about setting
10919 the reloc address or the reloc symbol index.
10920
10921 The back end routine is given a pointer to the swapped in
10922 internal symbols, and can access the hash table entries
10923 for the external symbols via elf_sym_hashes (input_bfd).
10924
10925 When generating relocatable output, the back end routine
10926 must handle STB_LOCAL/STT_SECTION symbols specially. The
10927 output symbol is going to be a section symbol
10928 corresponding to the output section, which will require
10929 the addend to be adjusted. */
10930
10931 ret = (*relocate_section) (output_bfd, flinfo->info,
10932 input_bfd, o, contents,
10933 internal_relocs,
10934 isymbuf,
10935 flinfo->sections);
10936 if (!ret)
10937 return FALSE;
10938
10939 if (ret == 2
10940 || bfd_link_relocatable (flinfo->info)
10941 || flinfo->info->emitrelocations)
10942 {
10943 Elf_Internal_Rela *irela;
10944 Elf_Internal_Rela *irelaend, *irelamid;
10945 bfd_vma last_offset;
10946 struct elf_link_hash_entry **rel_hash;
10947 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10948 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10949 unsigned int next_erel;
10950 bfd_boolean rela_normal;
10951 struct bfd_elf_section_data *esdi, *esdo;
10952
10953 esdi = elf_section_data (o);
10954 esdo = elf_section_data (o->output_section);
10955 rela_normal = FALSE;
10956
10957 /* Adjust the reloc addresses and symbol indices. */
10958
10959 irela = internal_relocs;
10960 irelaend = irela + o->reloc_count;
10961 rel_hash = esdo->rel.hashes + esdo->rel.count;
10962 /* We start processing the REL relocs, if any. When we reach
10963 IRELAMID in the loop, we switch to the RELA relocs. */
10964 irelamid = irela;
10965 if (esdi->rel.hdr != NULL)
10966 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10967 * bed->s->int_rels_per_ext_rel);
10968 rel_hash_list = rel_hash;
10969 rela_hash_list = NULL;
10970 last_offset = o->output_offset;
10971 if (!bfd_link_relocatable (flinfo->info))
10972 last_offset += o->output_section->vma;
10973 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10974 {
10975 unsigned long r_symndx;
10976 asection *sec;
10977 Elf_Internal_Sym sym;
10978
10979 if (next_erel == bed->s->int_rels_per_ext_rel)
10980 {
10981 rel_hash++;
10982 next_erel = 0;
10983 }
10984
10985 if (irela == irelamid)
10986 {
10987 rel_hash = esdo->rela.hashes + esdo->rela.count;
10988 rela_hash_list = rel_hash;
10989 rela_normal = bed->rela_normal;
10990 }
10991
10992 irela->r_offset = _bfd_elf_section_offset (output_bfd,
10993 flinfo->info, o,
10994 irela->r_offset);
10995 if (irela->r_offset >= (bfd_vma) -2)
10996 {
10997 /* This is a reloc for a deleted entry or somesuch.
10998 Turn it into an R_*_NONE reloc, at the same
10999 offset as the last reloc. elf_eh_frame.c and
11000 bfd_elf_discard_info rely on reloc offsets
11001 being ordered. */
11002 irela->r_offset = last_offset;
11003 irela->r_info = 0;
11004 irela->r_addend = 0;
11005 continue;
11006 }
11007
11008 irela->r_offset += o->output_offset;
11009
11010 /* Relocs in an executable have to be virtual addresses. */
11011 if (!bfd_link_relocatable (flinfo->info))
11012 irela->r_offset += o->output_section->vma;
11013
11014 last_offset = irela->r_offset;
11015
11016 r_symndx = irela->r_info >> r_sym_shift;
11017 if (r_symndx == STN_UNDEF)
11018 continue;
11019
11020 if (r_symndx >= locsymcount
11021 || (elf_bad_symtab (input_bfd)
11022 && flinfo->sections[r_symndx] == NULL))
11023 {
11024 struct elf_link_hash_entry *rh;
11025 unsigned long indx;
11026
11027 /* This is a reloc against a global symbol. We
11028 have not yet output all the local symbols, so
11029 we do not know the symbol index of any global
11030 symbol. We set the rel_hash entry for this
11031 reloc to point to the global hash table entry
11032 for this symbol. The symbol index is then
11033 set at the end of bfd_elf_final_link. */
11034 indx = r_symndx - extsymoff;
11035 rh = elf_sym_hashes (input_bfd)[indx];
11036 while (rh->root.type == bfd_link_hash_indirect
11037 || rh->root.type == bfd_link_hash_warning)
11038 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11039
11040 /* Setting the index to -2 tells
11041 elf_link_output_extsym that this symbol is
11042 used by a reloc. */
11043 BFD_ASSERT (rh->indx < 0);
11044 rh->indx = -2;
11045 *rel_hash = rh;
11046
11047 continue;
11048 }
11049
11050 /* This is a reloc against a local symbol. */
11051
11052 *rel_hash = NULL;
11053 sym = isymbuf[r_symndx];
11054 sec = flinfo->sections[r_symndx];
11055 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11056 {
11057 /* I suppose the backend ought to fill in the
11058 section of any STT_SECTION symbol against a
11059 processor specific section. */
11060 r_symndx = STN_UNDEF;
11061 if (bfd_is_abs_section (sec))
11062 ;
11063 else if (sec == NULL || sec->owner == NULL)
11064 {
11065 bfd_set_error (bfd_error_bad_value);
11066 return FALSE;
11067 }
11068 else
11069 {
11070 asection *osec = sec->output_section;
11071
11072 /* If we have discarded a section, the output
11073 section will be the absolute section. In
11074 case of discarded SEC_MERGE sections, use
11075 the kept section. relocate_section should
11076 have already handled discarded linkonce
11077 sections. */
11078 if (bfd_is_abs_section (osec)
11079 && sec->kept_section != NULL
11080 && sec->kept_section->output_section != NULL)
11081 {
11082 osec = sec->kept_section->output_section;
11083 irela->r_addend -= osec->vma;
11084 }
11085
11086 if (!bfd_is_abs_section (osec))
11087 {
11088 r_symndx = osec->target_index;
11089 if (r_symndx == STN_UNDEF)
11090 {
11091 irela->r_addend += osec->vma;
11092 osec = _bfd_nearby_section (output_bfd, osec,
11093 osec->vma);
11094 irela->r_addend -= osec->vma;
11095 r_symndx = osec->target_index;
11096 }
11097 }
11098 }
11099
11100 /* Adjust the addend according to where the
11101 section winds up in the output section. */
11102 if (rela_normal)
11103 irela->r_addend += sec->output_offset;
11104 }
11105 else
11106 {
11107 if (flinfo->indices[r_symndx] == -1)
11108 {
11109 unsigned long shlink;
11110 const char *name;
11111 asection *osec;
11112 long indx;
11113
11114 if (flinfo->info->strip == strip_all)
11115 {
11116 /* You can't do ld -r -s. */
11117 bfd_set_error (bfd_error_invalid_operation);
11118 return FALSE;
11119 }
11120
11121 /* This symbol was skipped earlier, but
11122 since it is needed by a reloc, we
11123 must output it now. */
11124 shlink = symtab_hdr->sh_link;
11125 name = (bfd_elf_string_from_elf_section
11126 (input_bfd, shlink, sym.st_name));
11127 if (name == NULL)
11128 return FALSE;
11129
11130 osec = sec->output_section;
11131 sym.st_shndx =
11132 _bfd_elf_section_from_bfd_section (output_bfd,
11133 osec);
11134 if (sym.st_shndx == SHN_BAD)
11135 return FALSE;
11136
11137 sym.st_value += sec->output_offset;
11138 if (!bfd_link_relocatable (flinfo->info))
11139 {
11140 sym.st_value += osec->vma;
11141 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11142 {
11143 struct elf_link_hash_table *htab
11144 = elf_hash_table (flinfo->info);
11145
11146 /* STT_TLS symbols are relative to PT_TLS
11147 segment base. */
11148 if (htab->tls_sec != NULL)
11149 sym.st_value -= htab->tls_sec->vma;
11150 else
11151 sym.st_info
11152 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11153 STT_NOTYPE);
11154 }
11155 }
11156
11157 indx = bfd_get_symcount (output_bfd);
11158 ret = elf_link_output_symstrtab (flinfo, name,
11159 &sym, sec,
11160 NULL);
11161 if (ret == 0)
11162 return FALSE;
11163 else if (ret == 1)
11164 flinfo->indices[r_symndx] = indx;
11165 else
11166 abort ();
11167 }
11168
11169 r_symndx = flinfo->indices[r_symndx];
11170 }
11171
11172 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11173 | (irela->r_info & r_type_mask));
11174 }
11175
11176 /* Swap out the relocs. */
11177 input_rel_hdr = esdi->rel.hdr;
11178 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
11179 {
11180 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11181 input_rel_hdr,
11182 internal_relocs,
11183 rel_hash_list))
11184 return FALSE;
11185 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11186 * bed->s->int_rels_per_ext_rel);
11187 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
11188 }
11189
11190 input_rela_hdr = esdi->rela.hdr;
11191 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11192 {
11193 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11194 input_rela_hdr,
11195 internal_relocs,
11196 rela_hash_list))
11197 return FALSE;
11198 }
11199 }
11200 }
11201
11202 /* Write out the modified section contents. */
11203 if (bed->elf_backend_write_section
11204 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
11205 contents))
11206 {
11207 /* Section written out. */
11208 }
11209 else switch (o->sec_info_type)
11210 {
11211 case SEC_INFO_TYPE_STABS:
11212 if (! (_bfd_write_section_stabs
11213 (output_bfd,
11214 &elf_hash_table (flinfo->info)->stab_info,
11215 o, &elf_section_data (o)->sec_info, contents)))
11216 return FALSE;
11217 break;
11218 case SEC_INFO_TYPE_MERGE:
11219 if (! _bfd_write_merged_section (output_bfd, o,
11220 elf_section_data (o)->sec_info))
11221 return FALSE;
11222 break;
11223 case SEC_INFO_TYPE_EH_FRAME:
11224 {
11225 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11226 o, contents))
11227 return FALSE;
11228 }
11229 break;
11230 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11231 {
11232 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11233 flinfo->info,
11234 o, contents))
11235 return FALSE;
11236 }
11237 break;
11238 default:
11239 {
11240 if (! (o->flags & SEC_EXCLUDE))
11241 {
11242 file_ptr offset = (file_ptr) o->output_offset;
11243 bfd_size_type todo = o->size;
11244
11245 offset *= bfd_octets_per_byte (output_bfd);
11246
11247 if ((o->flags & SEC_ELF_REVERSE_COPY))
11248 {
11249 /* Reverse-copy input section to output. */
11250 do
11251 {
11252 todo -= address_size;
11253 if (! bfd_set_section_contents (output_bfd,
11254 o->output_section,
11255 contents + todo,
11256 offset,
11257 address_size))
11258 return FALSE;
11259 if (todo == 0)
11260 break;
11261 offset += address_size;
11262 }
11263 while (1);
11264 }
11265 else if (! bfd_set_section_contents (output_bfd,
11266 o->output_section,
11267 contents,
11268 offset, todo))
11269 return FALSE;
11270 }
11271 }
11272 break;
11273 }
11274 }
11275
11276 return TRUE;
11277 }
11278
11279 /* Generate a reloc when linking an ELF file. This is a reloc
11280 requested by the linker, and does not come from any input file. This
11281 is used to build constructor and destructor tables when linking
11282 with -Ur. */
11283
11284 static bfd_boolean
11285 elf_reloc_link_order (bfd *output_bfd,
11286 struct bfd_link_info *info,
11287 asection *output_section,
11288 struct bfd_link_order *link_order)
11289 {
11290 reloc_howto_type *howto;
11291 long indx;
11292 bfd_vma offset;
11293 bfd_vma addend;
11294 struct bfd_elf_section_reloc_data *reldata;
11295 struct elf_link_hash_entry **rel_hash_ptr;
11296 Elf_Internal_Shdr *rel_hdr;
11297 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11298 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11299 bfd_byte *erel;
11300 unsigned int i;
11301 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11302
11303 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11304 if (howto == NULL)
11305 {
11306 bfd_set_error (bfd_error_bad_value);
11307 return FALSE;
11308 }
11309
11310 addend = link_order->u.reloc.p->addend;
11311
11312 if (esdo->rel.hdr)
11313 reldata = &esdo->rel;
11314 else if (esdo->rela.hdr)
11315 reldata = &esdo->rela;
11316 else
11317 {
11318 reldata = NULL;
11319 BFD_ASSERT (0);
11320 }
11321
11322 /* Figure out the symbol index. */
11323 rel_hash_ptr = reldata->hashes + reldata->count;
11324 if (link_order->type == bfd_section_reloc_link_order)
11325 {
11326 indx = link_order->u.reloc.p->u.section->target_index;
11327 BFD_ASSERT (indx != 0);
11328 *rel_hash_ptr = NULL;
11329 }
11330 else
11331 {
11332 struct elf_link_hash_entry *h;
11333
11334 /* Treat a reloc against a defined symbol as though it were
11335 actually against the section. */
11336 h = ((struct elf_link_hash_entry *)
11337 bfd_wrapped_link_hash_lookup (output_bfd, info,
11338 link_order->u.reloc.p->u.name,
11339 FALSE, FALSE, TRUE));
11340 if (h != NULL
11341 && (h->root.type == bfd_link_hash_defined
11342 || h->root.type == bfd_link_hash_defweak))
11343 {
11344 asection *section;
11345
11346 section = h->root.u.def.section;
11347 indx = section->output_section->target_index;
11348 *rel_hash_ptr = NULL;
11349 /* It seems that we ought to add the symbol value to the
11350 addend here, but in practice it has already been added
11351 because it was passed to constructor_callback. */
11352 addend += section->output_section->vma + section->output_offset;
11353 }
11354 else if (h != NULL)
11355 {
11356 /* Setting the index to -2 tells elf_link_output_extsym that
11357 this symbol is used by a reloc. */
11358 h->indx = -2;
11359 *rel_hash_ptr = h;
11360 indx = 0;
11361 }
11362 else
11363 {
11364 (*info->callbacks->unattached_reloc)
11365 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
11366 indx = 0;
11367 }
11368 }
11369
11370 /* If this is an inplace reloc, we must write the addend into the
11371 object file. */
11372 if (howto->partial_inplace && addend != 0)
11373 {
11374 bfd_size_type size;
11375 bfd_reloc_status_type rstat;
11376 bfd_byte *buf;
11377 bfd_boolean ok;
11378 const char *sym_name;
11379
11380 size = (bfd_size_type) bfd_get_reloc_size (howto);
11381 buf = (bfd_byte *) bfd_zmalloc (size);
11382 if (buf == NULL && size != 0)
11383 return FALSE;
11384 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11385 switch (rstat)
11386 {
11387 case bfd_reloc_ok:
11388 break;
11389
11390 default:
11391 case bfd_reloc_outofrange:
11392 abort ();
11393
11394 case bfd_reloc_overflow:
11395 if (link_order->type == bfd_section_reloc_link_order)
11396 sym_name = bfd_section_name (output_bfd,
11397 link_order->u.reloc.p->u.section);
11398 else
11399 sym_name = link_order->u.reloc.p->u.name;
11400 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11401 howto->name, addend, NULL, NULL,
11402 (bfd_vma) 0);
11403 break;
11404 }
11405
11406 ok = bfd_set_section_contents (output_bfd, output_section, buf,
11407 link_order->offset
11408 * bfd_octets_per_byte (output_bfd),
11409 size);
11410 free (buf);
11411 if (! ok)
11412 return FALSE;
11413 }
11414
11415 /* The address of a reloc is relative to the section in a
11416 relocatable file, and is a virtual address in an executable
11417 file. */
11418 offset = link_order->offset;
11419 if (! bfd_link_relocatable (info))
11420 offset += output_section->vma;
11421
11422 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11423 {
11424 irel[i].r_offset = offset;
11425 irel[i].r_info = 0;
11426 irel[i].r_addend = 0;
11427 }
11428 if (bed->s->arch_size == 32)
11429 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11430 else
11431 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11432
11433 rel_hdr = reldata->hdr;
11434 erel = rel_hdr->contents;
11435 if (rel_hdr->sh_type == SHT_REL)
11436 {
11437 erel += reldata->count * bed->s->sizeof_rel;
11438 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11439 }
11440 else
11441 {
11442 irel[0].r_addend = addend;
11443 erel += reldata->count * bed->s->sizeof_rela;
11444 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11445 }
11446
11447 ++reldata->count;
11448
11449 return TRUE;
11450 }
11451
11452
11453 /* Get the output vma of the section pointed to by the sh_link field. */
11454
11455 static bfd_vma
11456 elf_get_linked_section_vma (struct bfd_link_order *p)
11457 {
11458 Elf_Internal_Shdr **elf_shdrp;
11459 asection *s;
11460 int elfsec;
11461
11462 s = p->u.indirect.section;
11463 elf_shdrp = elf_elfsections (s->owner);
11464 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
11465 elfsec = elf_shdrp[elfsec]->sh_link;
11466 /* PR 290:
11467 The Intel C compiler generates SHT_IA_64_UNWIND with
11468 SHF_LINK_ORDER. But it doesn't set the sh_link or
11469 sh_info fields. Hence we could get the situation
11470 where elfsec is 0. */
11471 if (elfsec == 0)
11472 {
11473 const struct elf_backend_data *bed
11474 = get_elf_backend_data (s->owner);
11475 if (bed->link_order_error_handler)
11476 bed->link_order_error_handler
11477 /* xgettext:c-format */
11478 (_("%pB: warning: sh_link not set for section `%pA'"), s->owner, s);
11479 return 0;
11480 }
11481 else
11482 {
11483 s = elf_shdrp[elfsec]->bfd_section;
11484 return s->output_section->vma + s->output_offset;
11485 }
11486 }
11487
11488
11489 /* Compare two sections based on the locations of the sections they are
11490 linked to. Used by elf_fixup_link_order. */
11491
11492 static int
11493 compare_link_order (const void * a, const void * b)
11494 {
11495 bfd_vma apos;
11496 bfd_vma bpos;
11497
11498 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11499 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11500 if (apos < bpos)
11501 return -1;
11502 return apos > bpos;
11503 }
11504
11505
11506 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11507 order as their linked sections. Returns false if this could not be done
11508 because an output section includes both ordered and unordered
11509 sections. Ideally we'd do this in the linker proper. */
11510
11511 static bfd_boolean
11512 elf_fixup_link_order (bfd *abfd, asection *o)
11513 {
11514 int seen_linkorder;
11515 int seen_other;
11516 int n;
11517 struct bfd_link_order *p;
11518 bfd *sub;
11519 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11520 unsigned elfsec;
11521 struct bfd_link_order **sections;
11522 asection *s, *other_sec, *linkorder_sec;
11523 bfd_vma offset;
11524
11525 other_sec = NULL;
11526 linkorder_sec = NULL;
11527 seen_other = 0;
11528 seen_linkorder = 0;
11529 for (p = o->map_head.link_order; p != NULL; p = p->next)
11530 {
11531 if (p->type == bfd_indirect_link_order)
11532 {
11533 s = p->u.indirect.section;
11534 sub = s->owner;
11535 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11536 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
11537 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11538 && elfsec < elf_numsections (sub)
11539 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11540 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
11541 {
11542 seen_linkorder++;
11543 linkorder_sec = s;
11544 }
11545 else
11546 {
11547 seen_other++;
11548 other_sec = s;
11549 }
11550 }
11551 else
11552 seen_other++;
11553
11554 if (seen_other && seen_linkorder)
11555 {
11556 if (other_sec && linkorder_sec)
11557 _bfd_error_handler
11558 /* xgettext:c-format */
11559 (_("%pA has both ordered [`%pA' in %pB] "
11560 "and unordered [`%pA' in %pB] sections"),
11561 o, linkorder_sec, linkorder_sec->owner,
11562 other_sec, other_sec->owner);
11563 else
11564 _bfd_error_handler
11565 (_("%pA has both ordered and unordered sections"), o);
11566 bfd_set_error (bfd_error_bad_value);
11567 return FALSE;
11568 }
11569 }
11570
11571 if (!seen_linkorder)
11572 return TRUE;
11573
11574 sections = (struct bfd_link_order **)
11575 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11576 if (sections == NULL)
11577 return FALSE;
11578 seen_linkorder = 0;
11579
11580 for (p = o->map_head.link_order; p != NULL; p = p->next)
11581 {
11582 sections[seen_linkorder++] = p;
11583 }
11584 /* Sort the input sections in the order of their linked section. */
11585 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11586 compare_link_order);
11587
11588 /* Change the offsets of the sections. */
11589 offset = 0;
11590 for (n = 0; n < seen_linkorder; n++)
11591 {
11592 s = sections[n]->u.indirect.section;
11593 offset &= ~(bfd_vma) 0 << s->alignment_power;
11594 s->output_offset = offset / bfd_octets_per_byte (abfd);
11595 sections[n]->offset = offset;
11596 offset += sections[n]->size;
11597 }
11598
11599 free (sections);
11600 return TRUE;
11601 }
11602
11603 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11604 Returns TRUE upon success, FALSE otherwise. */
11605
11606 static bfd_boolean
11607 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11608 {
11609 bfd_boolean ret = FALSE;
11610 bfd *implib_bfd;
11611 const struct elf_backend_data *bed;
11612 flagword flags;
11613 enum bfd_architecture arch;
11614 unsigned int mach;
11615 asymbol **sympp = NULL;
11616 long symsize;
11617 long symcount;
11618 long src_count;
11619 elf_symbol_type *osymbuf;
11620
11621 implib_bfd = info->out_implib_bfd;
11622 bed = get_elf_backend_data (abfd);
11623
11624 if (!bfd_set_format (implib_bfd, bfd_object))
11625 return FALSE;
11626
11627 /* Use flag from executable but make it a relocatable object. */
11628 flags = bfd_get_file_flags (abfd);
11629 flags &= ~HAS_RELOC;
11630 if (!bfd_set_start_address (implib_bfd, 0)
11631 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
11632 return FALSE;
11633
11634 /* Copy architecture of output file to import library file. */
11635 arch = bfd_get_arch (abfd);
11636 mach = bfd_get_mach (abfd);
11637 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11638 && (abfd->target_defaulted
11639 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11640 return FALSE;
11641
11642 /* Get symbol table size. */
11643 symsize = bfd_get_symtab_upper_bound (abfd);
11644 if (symsize < 0)
11645 return FALSE;
11646
11647 /* Read in the symbol table. */
11648 sympp = (asymbol **) xmalloc (symsize);
11649 symcount = bfd_canonicalize_symtab (abfd, sympp);
11650 if (symcount < 0)
11651 goto free_sym_buf;
11652
11653 /* Allow the BFD backend to copy any private header data it
11654 understands from the output BFD to the import library BFD. */
11655 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11656 goto free_sym_buf;
11657
11658 /* Filter symbols to appear in the import library. */
11659 if (bed->elf_backend_filter_implib_symbols)
11660 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11661 symcount);
11662 else
11663 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11664 if (symcount == 0)
11665 {
11666 bfd_set_error (bfd_error_no_symbols);
11667 _bfd_error_handler (_("%pB: no symbol found for import library"),
11668 implib_bfd);
11669 goto free_sym_buf;
11670 }
11671
11672
11673 /* Make symbols absolute. */
11674 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11675 sizeof (*osymbuf));
11676 for (src_count = 0; src_count < symcount; src_count++)
11677 {
11678 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11679 sizeof (*osymbuf));
11680 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11681 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11682 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11683 osymbuf[src_count].internal_elf_sym.st_value =
11684 osymbuf[src_count].symbol.value;
11685 sympp[src_count] = &osymbuf[src_count].symbol;
11686 }
11687
11688 bfd_set_symtab (implib_bfd, sympp, symcount);
11689
11690 /* Allow the BFD backend to copy any private data it understands
11691 from the output BFD to the import library BFD. This is done last
11692 to permit the routine to look at the filtered symbol table. */
11693 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11694 goto free_sym_buf;
11695
11696 if (!bfd_close (implib_bfd))
11697 goto free_sym_buf;
11698
11699 ret = TRUE;
11700
11701 free_sym_buf:
11702 free (sympp);
11703 return ret;
11704 }
11705
11706 static void
11707 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11708 {
11709 asection *o;
11710
11711 if (flinfo->symstrtab != NULL)
11712 _bfd_elf_strtab_free (flinfo->symstrtab);
11713 if (flinfo->contents != NULL)
11714 free (flinfo->contents);
11715 if (flinfo->external_relocs != NULL)
11716 free (flinfo->external_relocs);
11717 if (flinfo->internal_relocs != NULL)
11718 free (flinfo->internal_relocs);
11719 if (flinfo->external_syms != NULL)
11720 free (flinfo->external_syms);
11721 if (flinfo->locsym_shndx != NULL)
11722 free (flinfo->locsym_shndx);
11723 if (flinfo->internal_syms != NULL)
11724 free (flinfo->internal_syms);
11725 if (flinfo->indices != NULL)
11726 free (flinfo->indices);
11727 if (flinfo->sections != NULL)
11728 free (flinfo->sections);
11729 if (flinfo->symshndxbuf != NULL
11730 && flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
11731 free (flinfo->symshndxbuf);
11732 for (o = obfd->sections; o != NULL; o = o->next)
11733 {
11734 struct bfd_elf_section_data *esdo = elf_section_data (o);
11735 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11736 free (esdo->rel.hashes);
11737 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11738 free (esdo->rela.hashes);
11739 }
11740 }
11741
11742 /* Do the final step of an ELF link. */
11743
11744 bfd_boolean
11745 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11746 {
11747 bfd_boolean dynamic;
11748 bfd_boolean emit_relocs;
11749 bfd *dynobj;
11750 struct elf_final_link_info flinfo;
11751 asection *o;
11752 struct bfd_link_order *p;
11753 bfd *sub;
11754 bfd_size_type max_contents_size;
11755 bfd_size_type max_external_reloc_size;
11756 bfd_size_type max_internal_reloc_count;
11757 bfd_size_type max_sym_count;
11758 bfd_size_type max_sym_shndx_count;
11759 Elf_Internal_Sym elfsym;
11760 unsigned int i;
11761 Elf_Internal_Shdr *symtab_hdr;
11762 Elf_Internal_Shdr *symtab_shndx_hdr;
11763 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11764 struct elf_outext_info eoinfo;
11765 bfd_boolean merged;
11766 size_t relativecount = 0;
11767 asection *reldyn = 0;
11768 bfd_size_type amt;
11769 asection *attr_section = NULL;
11770 bfd_vma attr_size = 0;
11771 const char *std_attrs_section;
11772 struct elf_link_hash_table *htab = elf_hash_table (info);
11773
11774 if (!is_elf_hash_table (htab))
11775 return FALSE;
11776
11777 if (bfd_link_pic (info))
11778 abfd->flags |= DYNAMIC;
11779
11780 dynamic = htab->dynamic_sections_created;
11781 dynobj = htab->dynobj;
11782
11783 emit_relocs = (bfd_link_relocatable (info)
11784 || info->emitrelocations);
11785
11786 flinfo.info = info;
11787 flinfo.output_bfd = abfd;
11788 flinfo.symstrtab = _bfd_elf_strtab_init ();
11789 if (flinfo.symstrtab == NULL)
11790 return FALSE;
11791
11792 if (! dynamic)
11793 {
11794 flinfo.hash_sec = NULL;
11795 flinfo.symver_sec = NULL;
11796 }
11797 else
11798 {
11799 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11800 /* Note that dynsym_sec can be NULL (on VMS). */
11801 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11802 /* Note that it is OK if symver_sec is NULL. */
11803 }
11804
11805 flinfo.contents = NULL;
11806 flinfo.external_relocs = NULL;
11807 flinfo.internal_relocs = NULL;
11808 flinfo.external_syms = NULL;
11809 flinfo.locsym_shndx = NULL;
11810 flinfo.internal_syms = NULL;
11811 flinfo.indices = NULL;
11812 flinfo.sections = NULL;
11813 flinfo.symshndxbuf = NULL;
11814 flinfo.filesym_count = 0;
11815
11816 /* The object attributes have been merged. Remove the input
11817 sections from the link, and set the contents of the output
11818 secton. */
11819 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11820 for (o = abfd->sections; o != NULL; o = o->next)
11821 {
11822 bfd_boolean remove_section = FALSE;
11823
11824 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11825 || strcmp (o->name, ".gnu.attributes") == 0)
11826 {
11827 for (p = o->map_head.link_order; p != NULL; p = p->next)
11828 {
11829 asection *input_section;
11830
11831 if (p->type != bfd_indirect_link_order)
11832 continue;
11833 input_section = p->u.indirect.section;
11834 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11835 elf_link_input_bfd ignores this section. */
11836 input_section->flags &= ~SEC_HAS_CONTENTS;
11837 }
11838
11839 attr_size = bfd_elf_obj_attr_size (abfd);
11840 bfd_set_section_size (abfd, o, attr_size);
11841 /* Skip this section later on. */
11842 o->map_head.link_order = NULL;
11843 if (attr_size)
11844 attr_section = o;
11845 else
11846 remove_section = TRUE;
11847 }
11848 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
11849 {
11850 /* Remove empty group section from linker output. */
11851 remove_section = TRUE;
11852 }
11853 if (remove_section)
11854 {
11855 o->flags |= SEC_EXCLUDE;
11856 bfd_section_list_remove (abfd, o);
11857 abfd->section_count--;
11858 }
11859 }
11860
11861 /* Count up the number of relocations we will output for each output
11862 section, so that we know the sizes of the reloc sections. We
11863 also figure out some maximum sizes. */
11864 max_contents_size = 0;
11865 max_external_reloc_size = 0;
11866 max_internal_reloc_count = 0;
11867 max_sym_count = 0;
11868 max_sym_shndx_count = 0;
11869 merged = FALSE;
11870 for (o = abfd->sections; o != NULL; o = o->next)
11871 {
11872 struct bfd_elf_section_data *esdo = elf_section_data (o);
11873 o->reloc_count = 0;
11874
11875 for (p = o->map_head.link_order; p != NULL; p = p->next)
11876 {
11877 unsigned int reloc_count = 0;
11878 unsigned int additional_reloc_count = 0;
11879 struct bfd_elf_section_data *esdi = NULL;
11880
11881 if (p->type == bfd_section_reloc_link_order
11882 || p->type == bfd_symbol_reloc_link_order)
11883 reloc_count = 1;
11884 else if (p->type == bfd_indirect_link_order)
11885 {
11886 asection *sec;
11887
11888 sec = p->u.indirect.section;
11889
11890 /* Mark all sections which are to be included in the
11891 link. This will normally be every section. We need
11892 to do this so that we can identify any sections which
11893 the linker has decided to not include. */
11894 sec->linker_mark = TRUE;
11895
11896 if (sec->flags & SEC_MERGE)
11897 merged = TRUE;
11898
11899 if (sec->rawsize > max_contents_size)
11900 max_contents_size = sec->rawsize;
11901 if (sec->size > max_contents_size)
11902 max_contents_size = sec->size;
11903
11904 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11905 && (sec->owner->flags & DYNAMIC) == 0)
11906 {
11907 size_t sym_count;
11908
11909 /* We are interested in just local symbols, not all
11910 symbols. */
11911 if (elf_bad_symtab (sec->owner))
11912 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11913 / bed->s->sizeof_sym);
11914 else
11915 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11916
11917 if (sym_count > max_sym_count)
11918 max_sym_count = sym_count;
11919
11920 if (sym_count > max_sym_shndx_count
11921 && elf_symtab_shndx_list (sec->owner) != NULL)
11922 max_sym_shndx_count = sym_count;
11923
11924 if (esdo->this_hdr.sh_type == SHT_REL
11925 || esdo->this_hdr.sh_type == SHT_RELA)
11926 /* Some backends use reloc_count in relocation sections
11927 to count particular types of relocs. Of course,
11928 reloc sections themselves can't have relocations. */
11929 ;
11930 else if (emit_relocs)
11931 {
11932 reloc_count = sec->reloc_count;
11933 if (bed->elf_backend_count_additional_relocs)
11934 {
11935 int c;
11936 c = (*bed->elf_backend_count_additional_relocs) (sec);
11937 additional_reloc_count += c;
11938 }
11939 }
11940 else if (bed->elf_backend_count_relocs)
11941 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11942
11943 esdi = elf_section_data (sec);
11944
11945 if ((sec->flags & SEC_RELOC) != 0)
11946 {
11947 size_t ext_size = 0;
11948
11949 if (esdi->rel.hdr != NULL)
11950 ext_size = esdi->rel.hdr->sh_size;
11951 if (esdi->rela.hdr != NULL)
11952 ext_size += esdi->rela.hdr->sh_size;
11953
11954 if (ext_size > max_external_reloc_size)
11955 max_external_reloc_size = ext_size;
11956 if (sec->reloc_count > max_internal_reloc_count)
11957 max_internal_reloc_count = sec->reloc_count;
11958 }
11959 }
11960 }
11961
11962 if (reloc_count == 0)
11963 continue;
11964
11965 reloc_count += additional_reloc_count;
11966 o->reloc_count += reloc_count;
11967
11968 if (p->type == bfd_indirect_link_order && emit_relocs)
11969 {
11970 if (esdi->rel.hdr)
11971 {
11972 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11973 esdo->rel.count += additional_reloc_count;
11974 }
11975 if (esdi->rela.hdr)
11976 {
11977 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11978 esdo->rela.count += additional_reloc_count;
11979 }
11980 }
11981 else
11982 {
11983 if (o->use_rela_p)
11984 esdo->rela.count += reloc_count;
11985 else
11986 esdo->rel.count += reloc_count;
11987 }
11988 }
11989
11990 if (o->reloc_count > 0)
11991 o->flags |= SEC_RELOC;
11992 else
11993 {
11994 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11995 set it (this is probably a bug) and if it is set
11996 assign_section_numbers will create a reloc section. */
11997 o->flags &=~ SEC_RELOC;
11998 }
11999
12000 /* If the SEC_ALLOC flag is not set, force the section VMA to
12001 zero. This is done in elf_fake_sections as well, but forcing
12002 the VMA to 0 here will ensure that relocs against these
12003 sections are handled correctly. */
12004 if ((o->flags & SEC_ALLOC) == 0
12005 && ! o->user_set_vma)
12006 o->vma = 0;
12007 }
12008
12009 if (! bfd_link_relocatable (info) && merged)
12010 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
12011
12012 /* Figure out the file positions for everything but the symbol table
12013 and the relocs. We set symcount to force assign_section_numbers
12014 to create a symbol table. */
12015 abfd->symcount = info->strip != strip_all || emit_relocs;
12016 BFD_ASSERT (! abfd->output_has_begun);
12017 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12018 goto error_return;
12019
12020 /* Set sizes, and assign file positions for reloc sections. */
12021 for (o = abfd->sections; o != NULL; o = o->next)
12022 {
12023 struct bfd_elf_section_data *esdo = elf_section_data (o);
12024 if ((o->flags & SEC_RELOC) != 0)
12025 {
12026 if (esdo->rel.hdr
12027 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
12028 goto error_return;
12029
12030 if (esdo->rela.hdr
12031 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
12032 goto error_return;
12033 }
12034
12035 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12036 to count upwards while actually outputting the relocations. */
12037 esdo->rel.count = 0;
12038 esdo->rela.count = 0;
12039
12040 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
12041 {
12042 /* Cache the section contents so that they can be compressed
12043 later. Use bfd_malloc since it will be freed by
12044 bfd_compress_section_contents. */
12045 unsigned char *contents = esdo->this_hdr.contents;
12046 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
12047 abort ();
12048 contents
12049 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12050 if (contents == NULL)
12051 goto error_return;
12052 esdo->this_hdr.contents = contents;
12053 }
12054 }
12055
12056 /* We have now assigned file positions for all the sections except
12057 .symtab, .strtab, and non-loaded reloc sections. We start the
12058 .symtab section at the current file position, and write directly
12059 to it. We build the .strtab section in memory. */
12060 abfd->symcount = 0;
12061 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12062 /* sh_name is set in prep_headers. */
12063 symtab_hdr->sh_type = SHT_SYMTAB;
12064 /* sh_flags, sh_addr and sh_size all start off zero. */
12065 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12066 /* sh_link is set in assign_section_numbers. */
12067 /* sh_info is set below. */
12068 /* sh_offset is set just below. */
12069 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
12070
12071 if (max_sym_count < 20)
12072 max_sym_count = 20;
12073 htab->strtabsize = max_sym_count;
12074 amt = max_sym_count * sizeof (struct elf_sym_strtab);
12075 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12076 if (htab->strtab == NULL)
12077 goto error_return;
12078 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12079 flinfo.symshndxbuf
12080 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12081 ? (Elf_External_Sym_Shndx *) -1 : NULL);
12082
12083 if (info->strip != strip_all || emit_relocs)
12084 {
12085 file_ptr off = elf_next_file_pos (abfd);
12086
12087 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
12088
12089 /* Note that at this point elf_next_file_pos (abfd) is
12090 incorrect. We do not yet know the size of the .symtab section.
12091 We correct next_file_pos below, after we do know the size. */
12092
12093 /* Start writing out the symbol table. The first symbol is always a
12094 dummy symbol. */
12095 elfsym.st_value = 0;
12096 elfsym.st_size = 0;
12097 elfsym.st_info = 0;
12098 elfsym.st_other = 0;
12099 elfsym.st_shndx = SHN_UNDEF;
12100 elfsym.st_target_internal = 0;
12101 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12102 bfd_und_section_ptr, NULL) != 1)
12103 goto error_return;
12104
12105 /* Output a symbol for each section. We output these even if we are
12106 discarding local symbols, since they are used for relocs. These
12107 symbols have no names. We store the index of each one in the
12108 index field of the section, so that we can find it again when
12109 outputting relocs. */
12110
12111 elfsym.st_size = 0;
12112 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12113 elfsym.st_other = 0;
12114 elfsym.st_value = 0;
12115 elfsym.st_target_internal = 0;
12116 for (i = 1; i < elf_numsections (abfd); i++)
12117 {
12118 o = bfd_section_from_elf_index (abfd, i);
12119 if (o != NULL)
12120 {
12121 o->target_index = bfd_get_symcount (abfd);
12122 elfsym.st_shndx = i;
12123 if (!bfd_link_relocatable (info))
12124 elfsym.st_value = o->vma;
12125 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
12126 NULL) != 1)
12127 goto error_return;
12128 }
12129 }
12130 }
12131
12132 /* Allocate some memory to hold information read in from the input
12133 files. */
12134 if (max_contents_size != 0)
12135 {
12136 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12137 if (flinfo.contents == NULL)
12138 goto error_return;
12139 }
12140
12141 if (max_external_reloc_size != 0)
12142 {
12143 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12144 if (flinfo.external_relocs == NULL)
12145 goto error_return;
12146 }
12147
12148 if (max_internal_reloc_count != 0)
12149 {
12150 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12151 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12152 if (flinfo.internal_relocs == NULL)
12153 goto error_return;
12154 }
12155
12156 if (max_sym_count != 0)
12157 {
12158 amt = max_sym_count * bed->s->sizeof_sym;
12159 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12160 if (flinfo.external_syms == NULL)
12161 goto error_return;
12162
12163 amt = max_sym_count * sizeof (Elf_Internal_Sym);
12164 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12165 if (flinfo.internal_syms == NULL)
12166 goto error_return;
12167
12168 amt = max_sym_count * sizeof (long);
12169 flinfo.indices = (long int *) bfd_malloc (amt);
12170 if (flinfo.indices == NULL)
12171 goto error_return;
12172
12173 amt = max_sym_count * sizeof (asection *);
12174 flinfo.sections = (asection **) bfd_malloc (amt);
12175 if (flinfo.sections == NULL)
12176 goto error_return;
12177 }
12178
12179 if (max_sym_shndx_count != 0)
12180 {
12181 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
12182 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12183 if (flinfo.locsym_shndx == NULL)
12184 goto error_return;
12185 }
12186
12187 if (htab->tls_sec)
12188 {
12189 bfd_vma base, end = 0;
12190 asection *sec;
12191
12192 for (sec = htab->tls_sec;
12193 sec && (sec->flags & SEC_THREAD_LOCAL);
12194 sec = sec->next)
12195 {
12196 bfd_size_type size = sec->size;
12197
12198 if (size == 0
12199 && (sec->flags & SEC_HAS_CONTENTS) == 0)
12200 {
12201 struct bfd_link_order *ord = sec->map_tail.link_order;
12202
12203 if (ord != NULL)
12204 size = ord->offset + ord->size;
12205 }
12206 end = sec->vma + size;
12207 }
12208 base = htab->tls_sec->vma;
12209 /* Only align end of TLS section if static TLS doesn't have special
12210 alignment requirements. */
12211 if (bed->static_tls_alignment == 1)
12212 end = align_power (end, htab->tls_sec->alignment_power);
12213 htab->tls_size = end - base;
12214 }
12215
12216 /* Reorder SHF_LINK_ORDER sections. */
12217 for (o = abfd->sections; o != NULL; o = o->next)
12218 {
12219 if (!elf_fixup_link_order (abfd, o))
12220 return FALSE;
12221 }
12222
12223 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12224 return FALSE;
12225
12226 /* Since ELF permits relocations to be against local symbols, we
12227 must have the local symbols available when we do the relocations.
12228 Since we would rather only read the local symbols once, and we
12229 would rather not keep them in memory, we handle all the
12230 relocations for a single input file at the same time.
12231
12232 Unfortunately, there is no way to know the total number of local
12233 symbols until we have seen all of them, and the local symbol
12234 indices precede the global symbol indices. This means that when
12235 we are generating relocatable output, and we see a reloc against
12236 a global symbol, we can not know the symbol index until we have
12237 finished examining all the local symbols to see which ones we are
12238 going to output. To deal with this, we keep the relocations in
12239 memory, and don't output them until the end of the link. This is
12240 an unfortunate waste of memory, but I don't see a good way around
12241 it. Fortunately, it only happens when performing a relocatable
12242 link, which is not the common case. FIXME: If keep_memory is set
12243 we could write the relocs out and then read them again; I don't
12244 know how bad the memory loss will be. */
12245
12246 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12247 sub->output_has_begun = FALSE;
12248 for (o = abfd->sections; o != NULL; o = o->next)
12249 {
12250 for (p = o->map_head.link_order; p != NULL; p = p->next)
12251 {
12252 if (p->type == bfd_indirect_link_order
12253 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12254 == bfd_target_elf_flavour)
12255 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12256 {
12257 if (! sub->output_has_begun)
12258 {
12259 if (! elf_link_input_bfd (&flinfo, sub))
12260 goto error_return;
12261 sub->output_has_begun = TRUE;
12262 }
12263 }
12264 else if (p->type == bfd_section_reloc_link_order
12265 || p->type == bfd_symbol_reloc_link_order)
12266 {
12267 if (! elf_reloc_link_order (abfd, info, o, p))
12268 goto error_return;
12269 }
12270 else
12271 {
12272 if (! _bfd_default_link_order (abfd, info, o, p))
12273 {
12274 if (p->type == bfd_indirect_link_order
12275 && (bfd_get_flavour (sub)
12276 == bfd_target_elf_flavour)
12277 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12278 != bed->s->elfclass))
12279 {
12280 const char *iclass, *oclass;
12281
12282 switch (bed->s->elfclass)
12283 {
12284 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12285 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12286 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12287 default: abort ();
12288 }
12289
12290 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12291 {
12292 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12293 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12294 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12295 default: abort ();
12296 }
12297
12298 bfd_set_error (bfd_error_wrong_format);
12299 _bfd_error_handler
12300 /* xgettext:c-format */
12301 (_("%pB: file class %s incompatible with %s"),
12302 sub, iclass, oclass);
12303 }
12304
12305 goto error_return;
12306 }
12307 }
12308 }
12309 }
12310
12311 /* Free symbol buffer if needed. */
12312 if (!info->reduce_memory_overheads)
12313 {
12314 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12315 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
12316 && elf_tdata (sub)->symbuf)
12317 {
12318 free (elf_tdata (sub)->symbuf);
12319 elf_tdata (sub)->symbuf = NULL;
12320 }
12321 }
12322
12323 /* Output any global symbols that got converted to local in a
12324 version script or due to symbol visibility. We do this in a
12325 separate step since ELF requires all local symbols to appear
12326 prior to any global symbols. FIXME: We should only do this if
12327 some global symbols were, in fact, converted to become local.
12328 FIXME: Will this work correctly with the Irix 5 linker? */
12329 eoinfo.failed = FALSE;
12330 eoinfo.flinfo = &flinfo;
12331 eoinfo.localsyms = TRUE;
12332 eoinfo.file_sym_done = FALSE;
12333 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12334 if (eoinfo.failed)
12335 return FALSE;
12336
12337 /* If backend needs to output some local symbols not present in the hash
12338 table, do it now. */
12339 if (bed->elf_backend_output_arch_local_syms
12340 && (info->strip != strip_all || emit_relocs))
12341 {
12342 typedef int (*out_sym_func)
12343 (void *, const char *, Elf_Internal_Sym *, asection *,
12344 struct elf_link_hash_entry *);
12345
12346 if (! ((*bed->elf_backend_output_arch_local_syms)
12347 (abfd, info, &flinfo,
12348 (out_sym_func) elf_link_output_symstrtab)))
12349 return FALSE;
12350 }
12351
12352 /* That wrote out all the local symbols. Finish up the symbol table
12353 with the global symbols. Even if we want to strip everything we
12354 can, we still need to deal with those global symbols that got
12355 converted to local in a version script. */
12356
12357 /* The sh_info field records the index of the first non local symbol. */
12358 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12359
12360 if (dynamic
12361 && htab->dynsym != NULL
12362 && htab->dynsym->output_section != bfd_abs_section_ptr)
12363 {
12364 Elf_Internal_Sym sym;
12365 bfd_byte *dynsym = htab->dynsym->contents;
12366
12367 o = htab->dynsym->output_section;
12368 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12369
12370 /* Write out the section symbols for the output sections. */
12371 if (bfd_link_pic (info)
12372 || htab->is_relocatable_executable)
12373 {
12374 asection *s;
12375
12376 sym.st_size = 0;
12377 sym.st_name = 0;
12378 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12379 sym.st_other = 0;
12380 sym.st_target_internal = 0;
12381
12382 for (s = abfd->sections; s != NULL; s = s->next)
12383 {
12384 int indx;
12385 bfd_byte *dest;
12386 long dynindx;
12387
12388 dynindx = elf_section_data (s)->dynindx;
12389 if (dynindx <= 0)
12390 continue;
12391 indx = elf_section_data (s)->this_idx;
12392 BFD_ASSERT (indx > 0);
12393 sym.st_shndx = indx;
12394 if (! check_dynsym (abfd, &sym))
12395 return FALSE;
12396 sym.st_value = s->vma;
12397 dest = dynsym + dynindx * bed->s->sizeof_sym;
12398 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12399 }
12400 }
12401
12402 /* Write out the local dynsyms. */
12403 if (htab->dynlocal)
12404 {
12405 struct elf_link_local_dynamic_entry *e;
12406 for (e = htab->dynlocal; e ; e = e->next)
12407 {
12408 asection *s;
12409 bfd_byte *dest;
12410
12411 /* Copy the internal symbol and turn off visibility.
12412 Note that we saved a word of storage and overwrote
12413 the original st_name with the dynstr_index. */
12414 sym = e->isym;
12415 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12416
12417 s = bfd_section_from_elf_index (e->input_bfd,
12418 e->isym.st_shndx);
12419 if (s != NULL)
12420 {
12421 sym.st_shndx =
12422 elf_section_data (s->output_section)->this_idx;
12423 if (! check_dynsym (abfd, &sym))
12424 return FALSE;
12425 sym.st_value = (s->output_section->vma
12426 + s->output_offset
12427 + e->isym.st_value);
12428 }
12429
12430 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12431 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12432 }
12433 }
12434 }
12435
12436 /* We get the global symbols from the hash table. */
12437 eoinfo.failed = FALSE;
12438 eoinfo.localsyms = FALSE;
12439 eoinfo.flinfo = &flinfo;
12440 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12441 if (eoinfo.failed)
12442 return FALSE;
12443
12444 /* If backend needs to output some symbols not present in the hash
12445 table, do it now. */
12446 if (bed->elf_backend_output_arch_syms
12447 && (info->strip != strip_all || emit_relocs))
12448 {
12449 typedef int (*out_sym_func)
12450 (void *, const char *, Elf_Internal_Sym *, asection *,
12451 struct elf_link_hash_entry *);
12452
12453 if (! ((*bed->elf_backend_output_arch_syms)
12454 (abfd, info, &flinfo,
12455 (out_sym_func) elf_link_output_symstrtab)))
12456 return FALSE;
12457 }
12458
12459 /* Finalize the .strtab section. */
12460 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12461
12462 /* Swap out the .strtab section. */
12463 if (!elf_link_swap_symbols_out (&flinfo))
12464 return FALSE;
12465
12466 /* Now we know the size of the symtab section. */
12467 if (bfd_get_symcount (abfd) > 0)
12468 {
12469 /* Finish up and write out the symbol string table (.strtab)
12470 section. */
12471 Elf_Internal_Shdr *symstrtab_hdr = NULL;
12472 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12473
12474 if (elf_symtab_shndx_list (abfd))
12475 {
12476 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12477
12478 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12479 {
12480 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12481 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12482 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12483 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12484 symtab_shndx_hdr->sh_size = amt;
12485
12486 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12487 off, TRUE);
12488
12489 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12490 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12491 return FALSE;
12492 }
12493 }
12494
12495 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12496 /* sh_name was set in prep_headers. */
12497 symstrtab_hdr->sh_type = SHT_STRTAB;
12498 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
12499 symstrtab_hdr->sh_addr = 0;
12500 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
12501 symstrtab_hdr->sh_entsize = 0;
12502 symstrtab_hdr->sh_link = 0;
12503 symstrtab_hdr->sh_info = 0;
12504 /* sh_offset is set just below. */
12505 symstrtab_hdr->sh_addralign = 1;
12506
12507 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12508 off, TRUE);
12509 elf_next_file_pos (abfd) = off;
12510
12511 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12512 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12513 return FALSE;
12514 }
12515
12516 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12517 {
12518 _bfd_error_handler (_("%pB: failed to generate import library"),
12519 info->out_implib_bfd);
12520 return FALSE;
12521 }
12522
12523 /* Adjust the relocs to have the correct symbol indices. */
12524 for (o = abfd->sections; o != NULL; o = o->next)
12525 {
12526 struct bfd_elf_section_data *esdo = elf_section_data (o);
12527 bfd_boolean sort;
12528
12529 if ((o->flags & SEC_RELOC) == 0)
12530 continue;
12531
12532 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12533 if (esdo->rel.hdr != NULL
12534 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
12535 return FALSE;
12536 if (esdo->rela.hdr != NULL
12537 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
12538 return FALSE;
12539
12540 /* Set the reloc_count field to 0 to prevent write_relocs from
12541 trying to swap the relocs out itself. */
12542 o->reloc_count = 0;
12543 }
12544
12545 if (dynamic && info->combreloc && dynobj != NULL)
12546 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12547
12548 /* If we are linking against a dynamic object, or generating a
12549 shared library, finish up the dynamic linking information. */
12550 if (dynamic)
12551 {
12552 bfd_byte *dyncon, *dynconend;
12553
12554 /* Fix up .dynamic entries. */
12555 o = bfd_get_linker_section (dynobj, ".dynamic");
12556 BFD_ASSERT (o != NULL);
12557
12558 dyncon = o->contents;
12559 dynconend = o->contents + o->size;
12560 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12561 {
12562 Elf_Internal_Dyn dyn;
12563 const char *name;
12564 unsigned int type;
12565 bfd_size_type sh_size;
12566 bfd_vma sh_addr;
12567
12568 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12569
12570 switch (dyn.d_tag)
12571 {
12572 default:
12573 continue;
12574 case DT_NULL:
12575 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12576 {
12577 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12578 {
12579 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12580 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12581 default: continue;
12582 }
12583 dyn.d_un.d_val = relativecount;
12584 relativecount = 0;
12585 break;
12586 }
12587 continue;
12588
12589 case DT_INIT:
12590 name = info->init_function;
12591 goto get_sym;
12592 case DT_FINI:
12593 name = info->fini_function;
12594 get_sym:
12595 {
12596 struct elf_link_hash_entry *h;
12597
12598 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
12599 if (h != NULL
12600 && (h->root.type == bfd_link_hash_defined
12601 || h->root.type == bfd_link_hash_defweak))
12602 {
12603 dyn.d_un.d_ptr = h->root.u.def.value;
12604 o = h->root.u.def.section;
12605 if (o->output_section != NULL)
12606 dyn.d_un.d_ptr += (o->output_section->vma
12607 + o->output_offset);
12608 else
12609 {
12610 /* The symbol is imported from another shared
12611 library and does not apply to this one. */
12612 dyn.d_un.d_ptr = 0;
12613 }
12614 break;
12615 }
12616 }
12617 continue;
12618
12619 case DT_PREINIT_ARRAYSZ:
12620 name = ".preinit_array";
12621 goto get_out_size;
12622 case DT_INIT_ARRAYSZ:
12623 name = ".init_array";
12624 goto get_out_size;
12625 case DT_FINI_ARRAYSZ:
12626 name = ".fini_array";
12627 get_out_size:
12628 o = bfd_get_section_by_name (abfd, name);
12629 if (o == NULL)
12630 {
12631 _bfd_error_handler
12632 (_("could not find section %s"), name);
12633 goto error_return;
12634 }
12635 if (o->size == 0)
12636 _bfd_error_handler
12637 (_("warning: %s section has zero size"), name);
12638 dyn.d_un.d_val = o->size;
12639 break;
12640
12641 case DT_PREINIT_ARRAY:
12642 name = ".preinit_array";
12643 goto get_out_vma;
12644 case DT_INIT_ARRAY:
12645 name = ".init_array";
12646 goto get_out_vma;
12647 case DT_FINI_ARRAY:
12648 name = ".fini_array";
12649 get_out_vma:
12650 o = bfd_get_section_by_name (abfd, name);
12651 goto do_vma;
12652
12653 case DT_HASH:
12654 name = ".hash";
12655 goto get_vma;
12656 case DT_GNU_HASH:
12657 name = ".gnu.hash";
12658 goto get_vma;
12659 case DT_STRTAB:
12660 name = ".dynstr";
12661 goto get_vma;
12662 case DT_SYMTAB:
12663 name = ".dynsym";
12664 goto get_vma;
12665 case DT_VERDEF:
12666 name = ".gnu.version_d";
12667 goto get_vma;
12668 case DT_VERNEED:
12669 name = ".gnu.version_r";
12670 goto get_vma;
12671 case DT_VERSYM:
12672 name = ".gnu.version";
12673 get_vma:
12674 o = bfd_get_linker_section (dynobj, name);
12675 do_vma:
12676 if (o == NULL || bfd_is_abs_section (o->output_section))
12677 {
12678 _bfd_error_handler
12679 (_("could not find section %s"), name);
12680 goto error_return;
12681 }
12682 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12683 {
12684 _bfd_error_handler
12685 (_("warning: section '%s' is being made into a note"), name);
12686 bfd_set_error (bfd_error_nonrepresentable_section);
12687 goto error_return;
12688 }
12689 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
12690 break;
12691
12692 case DT_REL:
12693 case DT_RELA:
12694 case DT_RELSZ:
12695 case DT_RELASZ:
12696 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12697 type = SHT_REL;
12698 else
12699 type = SHT_RELA;
12700 sh_size = 0;
12701 sh_addr = 0;
12702 for (i = 1; i < elf_numsections (abfd); i++)
12703 {
12704 Elf_Internal_Shdr *hdr;
12705
12706 hdr = elf_elfsections (abfd)[i];
12707 if (hdr->sh_type == type
12708 && (hdr->sh_flags & SHF_ALLOC) != 0)
12709 {
12710 sh_size += hdr->sh_size;
12711 if (sh_addr == 0
12712 || sh_addr > hdr->sh_addr)
12713 sh_addr = hdr->sh_addr;
12714 }
12715 }
12716
12717 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12718 {
12719 /* Don't count procedure linkage table relocs in the
12720 overall reloc count. */
12721 sh_size -= htab->srelplt->size;
12722 if (sh_size == 0)
12723 /* If the size is zero, make the address zero too.
12724 This is to avoid a glibc bug. If the backend
12725 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12726 zero, then we'll put DT_RELA at the end of
12727 DT_JMPREL. glibc will interpret the end of
12728 DT_RELA matching the end of DT_JMPREL as the
12729 case where DT_RELA includes DT_JMPREL, and for
12730 LD_BIND_NOW will decide that processing DT_RELA
12731 will process the PLT relocs too. Net result:
12732 No PLT relocs applied. */
12733 sh_addr = 0;
12734
12735 /* If .rela.plt is the first .rela section, exclude
12736 it from DT_RELA. */
12737 else if (sh_addr == (htab->srelplt->output_section->vma
12738 + htab->srelplt->output_offset))
12739 sh_addr += htab->srelplt->size;
12740 }
12741
12742 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12743 dyn.d_un.d_val = sh_size;
12744 else
12745 dyn.d_un.d_ptr = sh_addr;
12746 break;
12747 }
12748 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12749 }
12750 }
12751
12752 /* If we have created any dynamic sections, then output them. */
12753 if (dynobj != NULL)
12754 {
12755 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12756 goto error_return;
12757
12758 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12759 if (((info->warn_shared_textrel && bfd_link_pic (info))
12760 || info->error_textrel)
12761 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
12762 {
12763 bfd_byte *dyncon, *dynconend;
12764
12765 dyncon = o->contents;
12766 dynconend = o->contents + o->size;
12767 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12768 {
12769 Elf_Internal_Dyn dyn;
12770
12771 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12772
12773 if (dyn.d_tag == DT_TEXTREL)
12774 {
12775 if (info->error_textrel)
12776 info->callbacks->einfo
12777 (_("%P%X: read-only segment has dynamic relocations\n"));
12778 else
12779 info->callbacks->einfo
12780 (_("%P: warning: creating a DT_TEXTREL in a shared object\n"));
12781 break;
12782 }
12783 }
12784 }
12785
12786 for (o = dynobj->sections; o != NULL; o = o->next)
12787 {
12788 if ((o->flags & SEC_HAS_CONTENTS) == 0
12789 || o->size == 0
12790 || o->output_section == bfd_abs_section_ptr)
12791 continue;
12792 if ((o->flags & SEC_LINKER_CREATED) == 0)
12793 {
12794 /* At this point, we are only interested in sections
12795 created by _bfd_elf_link_create_dynamic_sections. */
12796 continue;
12797 }
12798 if (htab->stab_info.stabstr == o)
12799 continue;
12800 if (htab->eh_info.hdr_sec == o)
12801 continue;
12802 if (strcmp (o->name, ".dynstr") != 0)
12803 {
12804 if (! bfd_set_section_contents (abfd, o->output_section,
12805 o->contents,
12806 (file_ptr) o->output_offset
12807 * bfd_octets_per_byte (abfd),
12808 o->size))
12809 goto error_return;
12810 }
12811 else
12812 {
12813 /* The contents of the .dynstr section are actually in a
12814 stringtab. */
12815 file_ptr off;
12816
12817 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12818 if (bfd_seek (abfd, off, SEEK_SET) != 0
12819 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
12820 goto error_return;
12821 }
12822 }
12823 }
12824
12825 if (!info->resolve_section_groups)
12826 {
12827 bfd_boolean failed = FALSE;
12828
12829 BFD_ASSERT (bfd_link_relocatable (info));
12830 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12831 if (failed)
12832 goto error_return;
12833 }
12834
12835 /* If we have optimized stabs strings, output them. */
12836 if (htab->stab_info.stabstr != NULL)
12837 {
12838 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
12839 goto error_return;
12840 }
12841
12842 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12843 goto error_return;
12844
12845 elf_final_link_free (abfd, &flinfo);
12846
12847 if (attr_section)
12848 {
12849 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
12850 if (contents == NULL)
12851 return FALSE; /* Bail out and fail. */
12852 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12853 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12854 free (contents);
12855 }
12856
12857 return TRUE;
12858
12859 error_return:
12860 elf_final_link_free (abfd, &flinfo);
12861 return FALSE;
12862 }
12863 \f
12864 /* Initialize COOKIE for input bfd ABFD. */
12865
12866 static bfd_boolean
12867 init_reloc_cookie (struct elf_reloc_cookie *cookie,
12868 struct bfd_link_info *info, bfd *abfd)
12869 {
12870 Elf_Internal_Shdr *symtab_hdr;
12871 const struct elf_backend_data *bed;
12872
12873 bed = get_elf_backend_data (abfd);
12874 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12875
12876 cookie->abfd = abfd;
12877 cookie->sym_hashes = elf_sym_hashes (abfd);
12878 cookie->bad_symtab = elf_bad_symtab (abfd);
12879 if (cookie->bad_symtab)
12880 {
12881 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12882 cookie->extsymoff = 0;
12883 }
12884 else
12885 {
12886 cookie->locsymcount = symtab_hdr->sh_info;
12887 cookie->extsymoff = symtab_hdr->sh_info;
12888 }
12889
12890 if (bed->s->arch_size == 32)
12891 cookie->r_sym_shift = 8;
12892 else
12893 cookie->r_sym_shift = 32;
12894
12895 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12896 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12897 {
12898 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12899 cookie->locsymcount, 0,
12900 NULL, NULL, NULL);
12901 if (cookie->locsyms == NULL)
12902 {
12903 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12904 return FALSE;
12905 }
12906 if (info->keep_memory)
12907 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12908 }
12909 return TRUE;
12910 }
12911
12912 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12913
12914 static void
12915 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12916 {
12917 Elf_Internal_Shdr *symtab_hdr;
12918
12919 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12920 if (cookie->locsyms != NULL
12921 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12922 free (cookie->locsyms);
12923 }
12924
12925 /* Initialize the relocation information in COOKIE for input section SEC
12926 of input bfd ABFD. */
12927
12928 static bfd_boolean
12929 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12930 struct bfd_link_info *info, bfd *abfd,
12931 asection *sec)
12932 {
12933 if (sec->reloc_count == 0)
12934 {
12935 cookie->rels = NULL;
12936 cookie->relend = NULL;
12937 }
12938 else
12939 {
12940 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12941 info->keep_memory);
12942 if (cookie->rels == NULL)
12943 return FALSE;
12944 cookie->rel = cookie->rels;
12945 cookie->relend = cookie->rels + sec->reloc_count;
12946 }
12947 cookie->rel = cookie->rels;
12948 return TRUE;
12949 }
12950
12951 /* Free the memory allocated by init_reloc_cookie_rels,
12952 if appropriate. */
12953
12954 static void
12955 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12956 asection *sec)
12957 {
12958 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12959 free (cookie->rels);
12960 }
12961
12962 /* Initialize the whole of COOKIE for input section SEC. */
12963
12964 static bfd_boolean
12965 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12966 struct bfd_link_info *info,
12967 asection *sec)
12968 {
12969 if (!init_reloc_cookie (cookie, info, sec->owner))
12970 goto error1;
12971 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12972 goto error2;
12973 return TRUE;
12974
12975 error2:
12976 fini_reloc_cookie (cookie, sec->owner);
12977 error1:
12978 return FALSE;
12979 }
12980
12981 /* Free the memory allocated by init_reloc_cookie_for_section,
12982 if appropriate. */
12983
12984 static void
12985 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12986 asection *sec)
12987 {
12988 fini_reloc_cookie_rels (cookie, sec);
12989 fini_reloc_cookie (cookie, sec->owner);
12990 }
12991 \f
12992 /* Garbage collect unused sections. */
12993
12994 /* Default gc_mark_hook. */
12995
12996 asection *
12997 _bfd_elf_gc_mark_hook (asection *sec,
12998 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12999 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13000 struct elf_link_hash_entry *h,
13001 Elf_Internal_Sym *sym)
13002 {
13003 if (h != NULL)
13004 {
13005 switch (h->root.type)
13006 {
13007 case bfd_link_hash_defined:
13008 case bfd_link_hash_defweak:
13009 return h->root.u.def.section;
13010
13011 case bfd_link_hash_common:
13012 return h->root.u.c.p->section;
13013
13014 default:
13015 break;
13016 }
13017 }
13018 else
13019 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13020
13021 return NULL;
13022 }
13023
13024 /* Return the debug definition section. */
13025
13026 static asection *
13027 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13028 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13029 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13030 struct elf_link_hash_entry *h,
13031 Elf_Internal_Sym *sym)
13032 {
13033 if (h != NULL)
13034 {
13035 /* Return the global debug definition section. */
13036 if ((h->root.type == bfd_link_hash_defined
13037 || h->root.type == bfd_link_hash_defweak)
13038 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13039 return h->root.u.def.section;
13040 }
13041 else
13042 {
13043 /* Return the local debug definition section. */
13044 asection *isec = bfd_section_from_elf_index (sec->owner,
13045 sym->st_shndx);
13046 if ((isec->flags & SEC_DEBUGGING) != 0)
13047 return isec;
13048 }
13049
13050 return NULL;
13051 }
13052
13053 /* COOKIE->rel describes a relocation against section SEC, which is
13054 a section we've decided to keep. Return the section that contains
13055 the relocation symbol, or NULL if no section contains it. */
13056
13057 asection *
13058 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13059 elf_gc_mark_hook_fn gc_mark_hook,
13060 struct elf_reloc_cookie *cookie,
13061 bfd_boolean *start_stop)
13062 {
13063 unsigned long r_symndx;
13064 struct elf_link_hash_entry *h;
13065
13066 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
13067 if (r_symndx == STN_UNDEF)
13068 return NULL;
13069
13070 if (r_symndx >= cookie->locsymcount
13071 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13072 {
13073 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
13074 if (h == NULL)
13075 {
13076 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
13077 sec->owner);
13078 return NULL;
13079 }
13080 while (h->root.type == bfd_link_hash_indirect
13081 || h->root.type == bfd_link_hash_warning)
13082 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13083 h->mark = 1;
13084 /* If this symbol is weak and there is a non-weak definition, we
13085 keep the non-weak definition because many backends put
13086 dynamic reloc info on the non-weak definition for code
13087 handling copy relocs. */
13088 if (h->is_weakalias)
13089 weakdef (h)->mark = 1;
13090
13091 if (start_stop != NULL)
13092 {
13093 /* To work around a glibc bug, mark XXX input sections
13094 when there is a reference to __start_XXX or __stop_XXX
13095 symbols. */
13096 if (h->start_stop)
13097 {
13098 asection *s = h->u2.start_stop_section;
13099 *start_stop = !s->gc_mark;
13100 return s;
13101 }
13102 }
13103
13104 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13105 }
13106
13107 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13108 &cookie->locsyms[r_symndx]);
13109 }
13110
13111 /* COOKIE->rel describes a relocation against section SEC, which is
13112 a section we've decided to keep. Mark the section that contains
13113 the relocation symbol. */
13114
13115 bfd_boolean
13116 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13117 asection *sec,
13118 elf_gc_mark_hook_fn gc_mark_hook,
13119 struct elf_reloc_cookie *cookie)
13120 {
13121 asection *rsec;
13122 bfd_boolean start_stop = FALSE;
13123
13124 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13125 while (rsec != NULL)
13126 {
13127 if (!rsec->gc_mark)
13128 {
13129 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13130 || (rsec->owner->flags & DYNAMIC) != 0)
13131 rsec->gc_mark = 1;
13132 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13133 return FALSE;
13134 }
13135 if (!start_stop)
13136 break;
13137 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
13138 }
13139 return TRUE;
13140 }
13141
13142 /* The mark phase of garbage collection. For a given section, mark
13143 it and any sections in this section's group, and all the sections
13144 which define symbols to which it refers. */
13145
13146 bfd_boolean
13147 _bfd_elf_gc_mark (struct bfd_link_info *info,
13148 asection *sec,
13149 elf_gc_mark_hook_fn gc_mark_hook)
13150 {
13151 bfd_boolean ret;
13152 asection *group_sec, *eh_frame;
13153
13154 sec->gc_mark = 1;
13155
13156 /* Mark all the sections in the group. */
13157 group_sec = elf_section_data (sec)->next_in_group;
13158 if (group_sec && !group_sec->gc_mark)
13159 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
13160 return FALSE;
13161
13162 /* Look through the section relocs. */
13163 ret = TRUE;
13164 eh_frame = elf_eh_frame_section (sec->owner);
13165 if ((sec->flags & SEC_RELOC) != 0
13166 && sec->reloc_count > 0
13167 && sec != eh_frame)
13168 {
13169 struct elf_reloc_cookie cookie;
13170
13171 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13172 ret = FALSE;
13173 else
13174 {
13175 for (; cookie.rel < cookie.relend; cookie.rel++)
13176 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
13177 {
13178 ret = FALSE;
13179 break;
13180 }
13181 fini_reloc_cookie_for_section (&cookie, sec);
13182 }
13183 }
13184
13185 if (ret && eh_frame && elf_fde_list (sec))
13186 {
13187 struct elf_reloc_cookie cookie;
13188
13189 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13190 ret = FALSE;
13191 else
13192 {
13193 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13194 gc_mark_hook, &cookie))
13195 ret = FALSE;
13196 fini_reloc_cookie_for_section (&cookie, eh_frame);
13197 }
13198 }
13199
13200 eh_frame = elf_section_eh_frame_entry (sec);
13201 if (ret && eh_frame && !eh_frame->gc_mark)
13202 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13203 ret = FALSE;
13204
13205 return ret;
13206 }
13207
13208 /* Scan and mark sections in a special or debug section group. */
13209
13210 static void
13211 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13212 {
13213 /* Point to first section of section group. */
13214 asection *ssec;
13215 /* Used to iterate the section group. */
13216 asection *msec;
13217
13218 bfd_boolean is_special_grp = TRUE;
13219 bfd_boolean is_debug_grp = TRUE;
13220
13221 /* First scan to see if group contains any section other than debug
13222 and special section. */
13223 ssec = msec = elf_next_in_group (grp);
13224 do
13225 {
13226 if ((msec->flags & SEC_DEBUGGING) == 0)
13227 is_debug_grp = FALSE;
13228
13229 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13230 is_special_grp = FALSE;
13231
13232 msec = elf_next_in_group (msec);
13233 }
13234 while (msec != ssec);
13235
13236 /* If this is a pure debug section group or pure special section group,
13237 keep all sections in this group. */
13238 if (is_debug_grp || is_special_grp)
13239 {
13240 do
13241 {
13242 msec->gc_mark = 1;
13243 msec = elf_next_in_group (msec);
13244 }
13245 while (msec != ssec);
13246 }
13247 }
13248
13249 /* Keep debug and special sections. */
13250
13251 bfd_boolean
13252 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13253 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
13254 {
13255 bfd *ibfd;
13256
13257 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13258 {
13259 asection *isec;
13260 bfd_boolean some_kept;
13261 bfd_boolean debug_frag_seen;
13262 bfd_boolean has_kept_debug_info;
13263
13264 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13265 continue;
13266 isec = ibfd->sections;
13267 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13268 continue;
13269
13270 /* Ensure all linker created sections are kept,
13271 see if any other section is already marked,
13272 and note if we have any fragmented debug sections. */
13273 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
13274 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13275 {
13276 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13277 isec->gc_mark = 1;
13278 else if (isec->gc_mark
13279 && (isec->flags & SEC_ALLOC) != 0
13280 && elf_section_type (isec) != SHT_NOTE)
13281 some_kept = TRUE;
13282
13283 if (!debug_frag_seen
13284 && (isec->flags & SEC_DEBUGGING)
13285 && CONST_STRNEQ (isec->name, ".debug_line."))
13286 debug_frag_seen = TRUE;
13287 }
13288
13289 /* If no non-note alloc section in this file will be kept, then
13290 we can toss out the debug and special sections. */
13291 if (!some_kept)
13292 continue;
13293
13294 /* Keep debug and special sections like .comment when they are
13295 not part of a group. Also keep section groups that contain
13296 just debug sections or special sections. */
13297 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13298 {
13299 if ((isec->flags & SEC_GROUP) != 0)
13300 _bfd_elf_gc_mark_debug_special_section_group (isec);
13301 else if (((isec->flags & SEC_DEBUGGING) != 0
13302 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13303 && elf_next_in_group (isec) == NULL)
13304 isec->gc_mark = 1;
13305 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13306 has_kept_debug_info = TRUE;
13307 }
13308
13309 /* Look for CODE sections which are going to be discarded,
13310 and find and discard any fragmented debug sections which
13311 are associated with that code section. */
13312 if (debug_frag_seen)
13313 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13314 if ((isec->flags & SEC_CODE) != 0
13315 && isec->gc_mark == 0)
13316 {
13317 unsigned int ilen;
13318 asection *dsec;
13319
13320 ilen = strlen (isec->name);
13321
13322 /* Association is determined by the name of the debug
13323 section containing the name of the code section as
13324 a suffix. For example .debug_line.text.foo is a
13325 debug section associated with .text.foo. */
13326 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13327 {
13328 unsigned int dlen;
13329
13330 if (dsec->gc_mark == 0
13331 || (dsec->flags & SEC_DEBUGGING) == 0)
13332 continue;
13333
13334 dlen = strlen (dsec->name);
13335
13336 if (dlen > ilen
13337 && strncmp (dsec->name + (dlen - ilen),
13338 isec->name, ilen) == 0)
13339 dsec->gc_mark = 0;
13340 }
13341 }
13342
13343 /* Mark debug sections referenced by kept debug sections. */
13344 if (has_kept_debug_info)
13345 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13346 if (isec->gc_mark
13347 && (isec->flags & SEC_DEBUGGING) != 0)
13348 if (!_bfd_elf_gc_mark (info, isec,
13349 elf_gc_mark_debug_section))
13350 return FALSE;
13351 }
13352 return TRUE;
13353 }
13354
13355 static bfd_boolean
13356 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
13357 {
13358 bfd *sub;
13359 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13360
13361 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13362 {
13363 asection *o;
13364
13365 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13366 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
13367 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13368 continue;
13369 o = sub->sections;
13370 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13371 continue;
13372
13373 for (o = sub->sections; o != NULL; o = o->next)
13374 {
13375 /* When any section in a section group is kept, we keep all
13376 sections in the section group. If the first member of
13377 the section group is excluded, we will also exclude the
13378 group section. */
13379 if (o->flags & SEC_GROUP)
13380 {
13381 asection *first = elf_next_in_group (o);
13382 o->gc_mark = first->gc_mark;
13383 }
13384
13385 if (o->gc_mark)
13386 continue;
13387
13388 /* Skip sweeping sections already excluded. */
13389 if (o->flags & SEC_EXCLUDE)
13390 continue;
13391
13392 /* Since this is early in the link process, it is simple
13393 to remove a section from the output. */
13394 o->flags |= SEC_EXCLUDE;
13395
13396 if (info->print_gc_sections && o->size != 0)
13397 /* xgettext:c-format */
13398 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
13399 o, sub);
13400 }
13401 }
13402
13403 return TRUE;
13404 }
13405
13406 /* Propagate collected vtable information. This is called through
13407 elf_link_hash_traverse. */
13408
13409 static bfd_boolean
13410 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13411 {
13412 /* Those that are not vtables. */
13413 if (h->start_stop
13414 || h->u2.vtable == NULL
13415 || h->u2.vtable->parent == NULL)
13416 return TRUE;
13417
13418 /* Those vtables that do not have parents, we cannot merge. */
13419 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
13420 return TRUE;
13421
13422 /* If we've already been done, exit. */
13423 if (h->u2.vtable->used && h->u2.vtable->used[-1])
13424 return TRUE;
13425
13426 /* Make sure the parent's table is up to date. */
13427 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
13428
13429 if (h->u2.vtable->used == NULL)
13430 {
13431 /* None of this table's entries were referenced. Re-use the
13432 parent's table. */
13433 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13434 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
13435 }
13436 else
13437 {
13438 size_t n;
13439 bfd_boolean *cu, *pu;
13440
13441 /* Or the parent's entries into ours. */
13442 cu = h->u2.vtable->used;
13443 cu[-1] = TRUE;
13444 pu = h->u2.vtable->parent->u2.vtable->used;
13445 if (pu != NULL)
13446 {
13447 const struct elf_backend_data *bed;
13448 unsigned int log_file_align;
13449
13450 bed = get_elf_backend_data (h->root.u.def.section->owner);
13451 log_file_align = bed->s->log_file_align;
13452 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
13453 while (n--)
13454 {
13455 if (*pu)
13456 *cu = TRUE;
13457 pu++;
13458 cu++;
13459 }
13460 }
13461 }
13462
13463 return TRUE;
13464 }
13465
13466 static bfd_boolean
13467 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13468 {
13469 asection *sec;
13470 bfd_vma hstart, hend;
13471 Elf_Internal_Rela *relstart, *relend, *rel;
13472 const struct elf_backend_data *bed;
13473 unsigned int log_file_align;
13474
13475 /* Take care of both those symbols that do not describe vtables as
13476 well as those that are not loaded. */
13477 if (h->start_stop
13478 || h->u2.vtable == NULL
13479 || h->u2.vtable->parent == NULL)
13480 return TRUE;
13481
13482 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13483 || h->root.type == bfd_link_hash_defweak);
13484
13485 sec = h->root.u.def.section;
13486 hstart = h->root.u.def.value;
13487 hend = hstart + h->size;
13488
13489 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13490 if (!relstart)
13491 return *(bfd_boolean *) okp = FALSE;
13492 bed = get_elf_backend_data (sec->owner);
13493 log_file_align = bed->s->log_file_align;
13494
13495 relend = relstart + sec->reloc_count;
13496
13497 for (rel = relstart; rel < relend; ++rel)
13498 if (rel->r_offset >= hstart && rel->r_offset < hend)
13499 {
13500 /* If the entry is in use, do nothing. */
13501 if (h->u2.vtable->used
13502 && (rel->r_offset - hstart) < h->u2.vtable->size)
13503 {
13504 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13505 if (h->u2.vtable->used[entry])
13506 continue;
13507 }
13508 /* Otherwise, kill it. */
13509 rel->r_offset = rel->r_info = rel->r_addend = 0;
13510 }
13511
13512 return TRUE;
13513 }
13514
13515 /* Mark sections containing dynamically referenced symbols. When
13516 building shared libraries, we must assume that any visible symbol is
13517 referenced. */
13518
13519 bfd_boolean
13520 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
13521 {
13522 struct bfd_link_info *info = (struct bfd_link_info *) inf;
13523 struct bfd_elf_dynamic_list *d = info->dynamic_list;
13524
13525 if ((h->root.type == bfd_link_hash_defined
13526 || h->root.type == bfd_link_hash_defweak)
13527 && ((h->ref_dynamic && !h->forced_local)
13528 || ((h->def_regular || ELF_COMMON_DEF_P (h))
13529 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
13530 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
13531 && (!bfd_link_executable (info)
13532 || info->gc_keep_exported
13533 || info->export_dynamic
13534 || (h->dynamic
13535 && d != NULL
13536 && (*d->match) (&d->head, NULL, h->root.root.string)))
13537 && (h->versioned >= versioned
13538 || !bfd_hide_sym_by_version (info->version_info,
13539 h->root.root.string)))))
13540 h->root.u.def.section->flags |= SEC_KEEP;
13541
13542 return TRUE;
13543 }
13544
13545 /* Keep all sections containing symbols undefined on the command-line,
13546 and the section containing the entry symbol. */
13547
13548 void
13549 _bfd_elf_gc_keep (struct bfd_link_info *info)
13550 {
13551 struct bfd_sym_chain *sym;
13552
13553 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13554 {
13555 struct elf_link_hash_entry *h;
13556
13557 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13558 FALSE, FALSE, FALSE);
13559
13560 if (h != NULL
13561 && (h->root.type == bfd_link_hash_defined
13562 || h->root.type == bfd_link_hash_defweak)
13563 && !bfd_is_abs_section (h->root.u.def.section)
13564 && !bfd_is_und_section (h->root.u.def.section))
13565 h->root.u.def.section->flags |= SEC_KEEP;
13566 }
13567 }
13568
13569 bfd_boolean
13570 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13571 struct bfd_link_info *info)
13572 {
13573 bfd *ibfd = info->input_bfds;
13574
13575 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13576 {
13577 asection *sec;
13578 struct elf_reloc_cookie cookie;
13579
13580 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13581 continue;
13582 sec = ibfd->sections;
13583 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13584 continue;
13585
13586 if (!init_reloc_cookie (&cookie, info, ibfd))
13587 return FALSE;
13588
13589 for (sec = ibfd->sections; sec; sec = sec->next)
13590 {
13591 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13592 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13593 {
13594 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13595 fini_reloc_cookie_rels (&cookie, sec);
13596 }
13597 }
13598 }
13599 return TRUE;
13600 }
13601
13602 /* Do mark and sweep of unused sections. */
13603
13604 bfd_boolean
13605 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13606 {
13607 bfd_boolean ok = TRUE;
13608 bfd *sub;
13609 elf_gc_mark_hook_fn gc_mark_hook;
13610 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13611 struct elf_link_hash_table *htab;
13612
13613 if (!bed->can_gc_sections
13614 || !is_elf_hash_table (info->hash))
13615 {
13616 _bfd_error_handler(_("warning: gc-sections option ignored"));
13617 return TRUE;
13618 }
13619
13620 bed->gc_keep (info);
13621 htab = elf_hash_table (info);
13622
13623 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13624 at the .eh_frame section if we can mark the FDEs individually. */
13625 for (sub = info->input_bfds;
13626 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13627 sub = sub->link.next)
13628 {
13629 asection *sec;
13630 struct elf_reloc_cookie cookie;
13631
13632 sec = sub->sections;
13633 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13634 continue;
13635 sec = bfd_get_section_by_name (sub, ".eh_frame");
13636 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
13637 {
13638 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
13639 if (elf_section_data (sec)->sec_info
13640 && (sec->flags & SEC_LINKER_CREATED) == 0)
13641 elf_eh_frame_section (sub) = sec;
13642 fini_reloc_cookie_for_section (&cookie, sec);
13643 sec = bfd_get_next_section_by_name (NULL, sec);
13644 }
13645 }
13646
13647 /* Apply transitive closure to the vtable entry usage info. */
13648 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
13649 if (!ok)
13650 return FALSE;
13651
13652 /* Kill the vtable relocations that were not used. */
13653 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
13654 if (!ok)
13655 return FALSE;
13656
13657 /* Mark dynamically referenced symbols. */
13658 if (htab->dynamic_sections_created || info->gc_keep_exported)
13659 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
13660
13661 /* Grovel through relocs to find out who stays ... */
13662 gc_mark_hook = bed->gc_mark_hook;
13663 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13664 {
13665 asection *o;
13666
13667 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13668 || elf_object_id (sub) != elf_hash_table_id (htab)
13669 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13670 continue;
13671
13672 o = sub->sections;
13673 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13674 continue;
13675
13676 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13677 Also treat note sections as a root, if the section is not part
13678 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13679 well as FINI_ARRAY sections for ld -r. */
13680 for (o = sub->sections; o != NULL; o = o->next)
13681 if (!o->gc_mark
13682 && (o->flags & SEC_EXCLUDE) == 0
13683 && ((o->flags & SEC_KEEP) != 0
13684 || (bfd_link_relocatable (info)
13685 && ((elf_section_data (o)->this_hdr.sh_type
13686 == SHT_PREINIT_ARRAY)
13687 || (elf_section_data (o)->this_hdr.sh_type
13688 == SHT_INIT_ARRAY)
13689 || (elf_section_data (o)->this_hdr.sh_type
13690 == SHT_FINI_ARRAY)))
13691 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13692 && elf_next_in_group (o) == NULL )))
13693 {
13694 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13695 return FALSE;
13696 }
13697 }
13698
13699 /* Allow the backend to mark additional target specific sections. */
13700 bed->gc_mark_extra_sections (info, gc_mark_hook);
13701
13702 /* ... and mark SEC_EXCLUDE for those that go. */
13703 return elf_gc_sweep (abfd, info);
13704 }
13705 \f
13706 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13707
13708 bfd_boolean
13709 bfd_elf_gc_record_vtinherit (bfd *abfd,
13710 asection *sec,
13711 struct elf_link_hash_entry *h,
13712 bfd_vma offset)
13713 {
13714 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13715 struct elf_link_hash_entry **search, *child;
13716 size_t extsymcount;
13717 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13718
13719 /* The sh_info field of the symtab header tells us where the
13720 external symbols start. We don't care about the local symbols at
13721 this point. */
13722 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13723 if (!elf_bad_symtab (abfd))
13724 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13725
13726 sym_hashes = elf_sym_hashes (abfd);
13727 sym_hashes_end = sym_hashes + extsymcount;
13728
13729 /* Hunt down the child symbol, which is in this section at the same
13730 offset as the relocation. */
13731 for (search = sym_hashes; search != sym_hashes_end; ++search)
13732 {
13733 if ((child = *search) != NULL
13734 && (child->root.type == bfd_link_hash_defined
13735 || child->root.type == bfd_link_hash_defweak)
13736 && child->root.u.def.section == sec
13737 && child->root.u.def.value == offset)
13738 goto win;
13739 }
13740
13741 /* xgettext:c-format */
13742 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
13743 abfd, sec, (uint64_t) offset);
13744 bfd_set_error (bfd_error_invalid_operation);
13745 return FALSE;
13746
13747 win:
13748 if (!child->u2.vtable)
13749 {
13750 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13751 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13752 if (!child->u2.vtable)
13753 return FALSE;
13754 }
13755 if (!h)
13756 {
13757 /* This *should* only be the absolute section. It could potentially
13758 be that someone has defined a non-global vtable though, which
13759 would be bad. It isn't worth paging in the local symbols to be
13760 sure though; that case should simply be handled by the assembler. */
13761
13762 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
13763 }
13764 else
13765 child->u2.vtable->parent = h;
13766
13767 return TRUE;
13768 }
13769
13770 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13771
13772 bfd_boolean
13773 bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
13774 struct elf_link_hash_entry *h,
13775 bfd_vma addend)
13776 {
13777 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13778 unsigned int log_file_align = bed->s->log_file_align;
13779
13780 if (!h)
13781 {
13782 /* xgettext:c-format */
13783 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
13784 abfd, sec);
13785 bfd_set_error (bfd_error_bad_value);
13786 return FALSE;
13787 }
13788
13789 if (!h->u2.vtable)
13790 {
13791 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
13792 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
13793 if (!h->u2.vtable)
13794 return FALSE;
13795 }
13796
13797 if (addend >= h->u2.vtable->size)
13798 {
13799 size_t size, bytes, file_align;
13800 bfd_boolean *ptr = h->u2.vtable->used;
13801
13802 /* While the symbol is undefined, we have to be prepared to handle
13803 a zero size. */
13804 file_align = 1 << log_file_align;
13805 if (h->root.type == bfd_link_hash_undefined)
13806 size = addend + file_align;
13807 else
13808 {
13809 size = h->size;
13810 if (addend >= size)
13811 {
13812 /* Oops! We've got a reference past the defined end of
13813 the table. This is probably a bug -- shall we warn? */
13814 size = addend + file_align;
13815 }
13816 }
13817 size = (size + file_align - 1) & -file_align;
13818
13819 /* Allocate one extra entry for use as a "done" flag for the
13820 consolidation pass. */
13821 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13822
13823 if (ptr)
13824 {
13825 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
13826
13827 if (ptr != NULL)
13828 {
13829 size_t oldbytes;
13830
13831 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
13832 * sizeof (bfd_boolean));
13833 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13834 }
13835 }
13836 else
13837 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
13838
13839 if (ptr == NULL)
13840 return FALSE;
13841
13842 /* And arrange for that done flag to be at index -1. */
13843 h->u2.vtable->used = ptr + 1;
13844 h->u2.vtable->size = size;
13845 }
13846
13847 h->u2.vtable->used[addend >> log_file_align] = TRUE;
13848
13849 return TRUE;
13850 }
13851
13852 /* Map an ELF section header flag to its corresponding string. */
13853 typedef struct
13854 {
13855 char *flag_name;
13856 flagword flag_value;
13857 } elf_flags_to_name_table;
13858
13859 static elf_flags_to_name_table elf_flags_to_names [] =
13860 {
13861 { "SHF_WRITE", SHF_WRITE },
13862 { "SHF_ALLOC", SHF_ALLOC },
13863 { "SHF_EXECINSTR", SHF_EXECINSTR },
13864 { "SHF_MERGE", SHF_MERGE },
13865 { "SHF_STRINGS", SHF_STRINGS },
13866 { "SHF_INFO_LINK", SHF_INFO_LINK},
13867 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13868 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13869 { "SHF_GROUP", SHF_GROUP },
13870 { "SHF_TLS", SHF_TLS },
13871 { "SHF_MASKOS", SHF_MASKOS },
13872 { "SHF_EXCLUDE", SHF_EXCLUDE },
13873 };
13874
13875 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13876 bfd_boolean
13877 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
13878 struct flag_info *flaginfo,
13879 asection *section)
13880 {
13881 const bfd_vma sh_flags = elf_section_flags (section);
13882
13883 if (!flaginfo->flags_initialized)
13884 {
13885 bfd *obfd = info->output_bfd;
13886 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13887 struct flag_info_list *tf = flaginfo->flag_list;
13888 int with_hex = 0;
13889 int without_hex = 0;
13890
13891 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13892 {
13893 unsigned i;
13894 flagword (*lookup) (char *);
13895
13896 lookup = bed->elf_backend_lookup_section_flags_hook;
13897 if (lookup != NULL)
13898 {
13899 flagword hexval = (*lookup) ((char *) tf->name);
13900
13901 if (hexval != 0)
13902 {
13903 if (tf->with == with_flags)
13904 with_hex |= hexval;
13905 else if (tf->with == without_flags)
13906 without_hex |= hexval;
13907 tf->valid = TRUE;
13908 continue;
13909 }
13910 }
13911 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13912 {
13913 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
13914 {
13915 if (tf->with == with_flags)
13916 with_hex |= elf_flags_to_names[i].flag_value;
13917 else if (tf->with == without_flags)
13918 without_hex |= elf_flags_to_names[i].flag_value;
13919 tf->valid = TRUE;
13920 break;
13921 }
13922 }
13923 if (!tf->valid)
13924 {
13925 info->callbacks->einfo
13926 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
13927 return FALSE;
13928 }
13929 }
13930 flaginfo->flags_initialized = TRUE;
13931 flaginfo->only_with_flags |= with_hex;
13932 flaginfo->not_with_flags |= without_hex;
13933 }
13934
13935 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13936 return FALSE;
13937
13938 if ((flaginfo->not_with_flags & sh_flags) != 0)
13939 return FALSE;
13940
13941 return TRUE;
13942 }
13943
13944 struct alloc_got_off_arg {
13945 bfd_vma gotoff;
13946 struct bfd_link_info *info;
13947 };
13948
13949 /* We need a special top-level link routine to convert got reference counts
13950 to real got offsets. */
13951
13952 static bfd_boolean
13953 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13954 {
13955 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13956 bfd *obfd = gofarg->info->output_bfd;
13957 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13958
13959 if (h->got.refcount > 0)
13960 {
13961 h->got.offset = gofarg->gotoff;
13962 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13963 }
13964 else
13965 h->got.offset = (bfd_vma) -1;
13966
13967 return TRUE;
13968 }
13969
13970 /* And an accompanying bit to work out final got entry offsets once
13971 we're done. Should be called from final_link. */
13972
13973 bfd_boolean
13974 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13975 struct bfd_link_info *info)
13976 {
13977 bfd *i;
13978 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13979 bfd_vma gotoff;
13980 struct alloc_got_off_arg gofarg;
13981
13982 BFD_ASSERT (abfd == info->output_bfd);
13983
13984 if (! is_elf_hash_table (info->hash))
13985 return FALSE;
13986
13987 /* The GOT offset is relative to the .got section, but the GOT header is
13988 put into the .got.plt section, if the backend uses it. */
13989 if (bed->want_got_plt)
13990 gotoff = 0;
13991 else
13992 gotoff = bed->got_header_size;
13993
13994 /* Do the local .got entries first. */
13995 for (i = info->input_bfds; i; i = i->link.next)
13996 {
13997 bfd_signed_vma *local_got;
13998 size_t j, locsymcount;
13999 Elf_Internal_Shdr *symtab_hdr;
14000
14001 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14002 continue;
14003
14004 local_got = elf_local_got_refcounts (i);
14005 if (!local_got)
14006 continue;
14007
14008 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14009 if (elf_bad_symtab (i))
14010 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14011 else
14012 locsymcount = symtab_hdr->sh_info;
14013
14014 for (j = 0; j < locsymcount; ++j)
14015 {
14016 if (local_got[j] > 0)
14017 {
14018 local_got[j] = gotoff;
14019 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
14020 }
14021 else
14022 local_got[j] = (bfd_vma) -1;
14023 }
14024 }
14025
14026 /* Then the global .got entries. .plt refcounts are handled by
14027 adjust_dynamic_symbol */
14028 gofarg.gotoff = gotoff;
14029 gofarg.info = info;
14030 elf_link_hash_traverse (elf_hash_table (info),
14031 elf_gc_allocate_got_offsets,
14032 &gofarg);
14033 return TRUE;
14034 }
14035
14036 /* Many folk need no more in the way of final link than this, once
14037 got entry reference counting is enabled. */
14038
14039 bfd_boolean
14040 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14041 {
14042 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14043 return FALSE;
14044
14045 /* Invoke the regular ELF backend linker to do all the work. */
14046 return bfd_elf_final_link (abfd, info);
14047 }
14048
14049 bfd_boolean
14050 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14051 {
14052 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
14053
14054 if (rcookie->bad_symtab)
14055 rcookie->rel = rcookie->rels;
14056
14057 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14058 {
14059 unsigned long r_symndx;
14060
14061 if (! rcookie->bad_symtab)
14062 if (rcookie->rel->r_offset > offset)
14063 return FALSE;
14064 if (rcookie->rel->r_offset != offset)
14065 continue;
14066
14067 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
14068 if (r_symndx == STN_UNDEF)
14069 return TRUE;
14070
14071 if (r_symndx >= rcookie->locsymcount
14072 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14073 {
14074 struct elf_link_hash_entry *h;
14075
14076 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14077
14078 while (h->root.type == bfd_link_hash_indirect
14079 || h->root.type == bfd_link_hash_warning)
14080 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14081
14082 if ((h->root.type == bfd_link_hash_defined
14083 || h->root.type == bfd_link_hash_defweak)
14084 && (h->root.u.def.section->owner != rcookie->abfd
14085 || h->root.u.def.section->kept_section != NULL
14086 || discarded_section (h->root.u.def.section)))
14087 return TRUE;
14088 }
14089 else
14090 {
14091 /* It's not a relocation against a global symbol,
14092 but it could be a relocation against a local
14093 symbol for a discarded section. */
14094 asection *isec;
14095 Elf_Internal_Sym *isym;
14096
14097 /* Need to: get the symbol; get the section. */
14098 isym = &rcookie->locsyms[r_symndx];
14099 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
14100 if (isec != NULL
14101 && (isec->kept_section != NULL
14102 || discarded_section (isec)))
14103 return TRUE;
14104 }
14105 return FALSE;
14106 }
14107 return FALSE;
14108 }
14109
14110 /* Discard unneeded references to discarded sections.
14111 Returns -1 on error, 1 if any section's size was changed, 0 if
14112 nothing changed. This function assumes that the relocations are in
14113 sorted order, which is true for all known assemblers. */
14114
14115 int
14116 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14117 {
14118 struct elf_reloc_cookie cookie;
14119 asection *o;
14120 bfd *abfd;
14121 int changed = 0;
14122
14123 if (info->traditional_format
14124 || !is_elf_hash_table (info->hash))
14125 return 0;
14126
14127 o = bfd_get_section_by_name (output_bfd, ".stab");
14128 if (o != NULL)
14129 {
14130 asection *i;
14131
14132 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14133 {
14134 if (i->size == 0
14135 || i->reloc_count == 0
14136 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14137 continue;
14138
14139 abfd = i->owner;
14140 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14141 continue;
14142
14143 if (!init_reloc_cookie_for_section (&cookie, info, i))
14144 return -1;
14145
14146 if (_bfd_discard_section_stabs (abfd, i,
14147 elf_section_data (i)->sec_info,
14148 bfd_elf_reloc_symbol_deleted_p,
14149 &cookie))
14150 changed = 1;
14151
14152 fini_reloc_cookie_for_section (&cookie, i);
14153 }
14154 }
14155
14156 o = NULL;
14157 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14158 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
14159 if (o != NULL)
14160 {
14161 asection *i;
14162 int eh_changed = 0;
14163 unsigned int eh_alignment;
14164
14165 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14166 {
14167 if (i->size == 0)
14168 continue;
14169
14170 abfd = i->owner;
14171 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14172 continue;
14173
14174 if (!init_reloc_cookie_for_section (&cookie, info, i))
14175 return -1;
14176
14177 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14178 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
14179 bfd_elf_reloc_symbol_deleted_p,
14180 &cookie))
14181 {
14182 eh_changed = 1;
14183 if (i->size != i->rawsize)
14184 changed = 1;
14185 }
14186
14187 fini_reloc_cookie_for_section (&cookie, i);
14188 }
14189
14190 eh_alignment = 1 << o->alignment_power;
14191 /* Skip over zero terminator, and prevent empty sections from
14192 adding alignment padding at the end. */
14193 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14194 if (i->size == 0)
14195 i->flags |= SEC_EXCLUDE;
14196 else if (i->size > 4)
14197 break;
14198 /* The last non-empty eh_frame section doesn't need padding. */
14199 if (i != NULL)
14200 i = i->map_tail.s;
14201 /* Any prior sections must pad the last FDE out to the output
14202 section alignment. Otherwise we might have zero padding
14203 between sections, which would be seen as a terminator. */
14204 for (; i != NULL; i = i->map_tail.s)
14205 if (i->size == 4)
14206 /* All but the last zero terminator should have been removed. */
14207 BFD_FAIL ();
14208 else
14209 {
14210 bfd_size_type size
14211 = (i->size + eh_alignment - 1) & -eh_alignment;
14212 if (i->size != size)
14213 {
14214 i->size = size;
14215 changed = 1;
14216 eh_changed = 1;
14217 }
14218 }
14219 if (eh_changed)
14220 elf_link_hash_traverse (elf_hash_table (info),
14221 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
14222 }
14223
14224 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14225 {
14226 const struct elf_backend_data *bed;
14227 asection *s;
14228
14229 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14230 continue;
14231 s = abfd->sections;
14232 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14233 continue;
14234
14235 bed = get_elf_backend_data (abfd);
14236
14237 if (bed->elf_backend_discard_info != NULL)
14238 {
14239 if (!init_reloc_cookie (&cookie, info, abfd))
14240 return -1;
14241
14242 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
14243 changed = 1;
14244
14245 fini_reloc_cookie (&cookie, abfd);
14246 }
14247 }
14248
14249 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14250 _bfd_elf_end_eh_frame_parsing (info);
14251
14252 if (info->eh_frame_hdr_type
14253 && !bfd_link_relocatable (info)
14254 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
14255 changed = 1;
14256
14257 return changed;
14258 }
14259
14260 bfd_boolean
14261 _bfd_elf_section_already_linked (bfd *abfd,
14262 asection *sec,
14263 struct bfd_link_info *info)
14264 {
14265 flagword flags;
14266 const char *name, *key;
14267 struct bfd_section_already_linked *l;
14268 struct bfd_section_already_linked_hash_entry *already_linked_list;
14269
14270 if (sec->output_section == bfd_abs_section_ptr)
14271 return FALSE;
14272
14273 flags = sec->flags;
14274
14275 /* Return if it isn't a linkonce section. A comdat group section
14276 also has SEC_LINK_ONCE set. */
14277 if ((flags & SEC_LINK_ONCE) == 0)
14278 return FALSE;
14279
14280 /* Don't put group member sections on our list of already linked
14281 sections. They are handled as a group via their group section. */
14282 if (elf_sec_group (sec) != NULL)
14283 return FALSE;
14284
14285 /* For a SHT_GROUP section, use the group signature as the key. */
14286 name = sec->name;
14287 if ((flags & SEC_GROUP) != 0
14288 && elf_next_in_group (sec) != NULL
14289 && elf_group_name (elf_next_in_group (sec)) != NULL)
14290 key = elf_group_name (elf_next_in_group (sec));
14291 else
14292 {
14293 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14294 if (CONST_STRNEQ (name, ".gnu.linkonce.")
14295 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14296 key++;
14297 else
14298 /* Must be a user linkonce section that doesn't follow gcc's
14299 naming convention. In this case we won't be matching
14300 single member groups. */
14301 key = name;
14302 }
14303
14304 already_linked_list = bfd_section_already_linked_table_lookup (key);
14305
14306 for (l = already_linked_list->entry; l != NULL; l = l->next)
14307 {
14308 /* We may have 2 different types of sections on the list: group
14309 sections with a signature of <key> (<key> is some string),
14310 and linkonce sections named .gnu.linkonce.<type>.<key>.
14311 Match like sections. LTO plugin sections are an exception.
14312 They are always named .gnu.linkonce.t.<key> and match either
14313 type of section. */
14314 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14315 && ((flags & SEC_GROUP) != 0
14316 || strcmp (name, l->sec->name) == 0))
14317 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
14318 {
14319 /* The section has already been linked. See if we should
14320 issue a warning. */
14321 if (!_bfd_handle_already_linked (sec, l, info))
14322 return FALSE;
14323
14324 if (flags & SEC_GROUP)
14325 {
14326 asection *first = elf_next_in_group (sec);
14327 asection *s = first;
14328
14329 while (s != NULL)
14330 {
14331 s->output_section = bfd_abs_section_ptr;
14332 /* Record which group discards it. */
14333 s->kept_section = l->sec;
14334 s = elf_next_in_group (s);
14335 /* These lists are circular. */
14336 if (s == first)
14337 break;
14338 }
14339 }
14340
14341 return TRUE;
14342 }
14343 }
14344
14345 /* A single member comdat group section may be discarded by a
14346 linkonce section and vice versa. */
14347 if ((flags & SEC_GROUP) != 0)
14348 {
14349 asection *first = elf_next_in_group (sec);
14350
14351 if (first != NULL && elf_next_in_group (first) == first)
14352 /* Check this single member group against linkonce sections. */
14353 for (l = already_linked_list->entry; l != NULL; l = l->next)
14354 if ((l->sec->flags & SEC_GROUP) == 0
14355 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14356 {
14357 first->output_section = bfd_abs_section_ptr;
14358 first->kept_section = l->sec;
14359 sec->output_section = bfd_abs_section_ptr;
14360 break;
14361 }
14362 }
14363 else
14364 /* Check this linkonce section against single member groups. */
14365 for (l = already_linked_list->entry; l != NULL; l = l->next)
14366 if (l->sec->flags & SEC_GROUP)
14367 {
14368 asection *first = elf_next_in_group (l->sec);
14369
14370 if (first != NULL
14371 && elf_next_in_group (first) == first
14372 && bfd_elf_match_symbols_in_sections (first, sec, info))
14373 {
14374 sec->output_section = bfd_abs_section_ptr;
14375 sec->kept_section = first;
14376 break;
14377 }
14378 }
14379
14380 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14381 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14382 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14383 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14384 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14385 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14386 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14387 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14388 The reverse order cannot happen as there is never a bfd with only the
14389 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14390 matter as here were are looking only for cross-bfd sections. */
14391
14392 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14393 for (l = already_linked_list->entry; l != NULL; l = l->next)
14394 if ((l->sec->flags & SEC_GROUP) == 0
14395 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14396 {
14397 if (abfd != l->sec->owner)
14398 sec->output_section = bfd_abs_section_ptr;
14399 break;
14400 }
14401
14402 /* This is the first section with this name. Record it. */
14403 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
14404 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
14405 return sec->output_section == bfd_abs_section_ptr;
14406 }
14407
14408 bfd_boolean
14409 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
14410 {
14411 return sym->st_shndx == SHN_COMMON;
14412 }
14413
14414 unsigned int
14415 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14416 {
14417 return SHN_COMMON;
14418 }
14419
14420 asection *
14421 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14422 {
14423 return bfd_com_section_ptr;
14424 }
14425
14426 bfd_vma
14427 _bfd_elf_default_got_elt_size (bfd *abfd,
14428 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14429 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14430 bfd *ibfd ATTRIBUTE_UNUSED,
14431 unsigned long symndx ATTRIBUTE_UNUSED)
14432 {
14433 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14434 return bed->s->arch_size / 8;
14435 }
14436
14437 /* Routines to support the creation of dynamic relocs. */
14438
14439 /* Returns the name of the dynamic reloc section associated with SEC. */
14440
14441 static const char *
14442 get_dynamic_reloc_section_name (bfd * abfd,
14443 asection * sec,
14444 bfd_boolean is_rela)
14445 {
14446 char *name;
14447 const char *old_name = bfd_get_section_name (NULL, sec);
14448 const char *prefix = is_rela ? ".rela" : ".rel";
14449
14450 if (old_name == NULL)
14451 return NULL;
14452
14453 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
14454 sprintf (name, "%s%s", prefix, old_name);
14455
14456 return name;
14457 }
14458
14459 /* Returns the dynamic reloc section associated with SEC.
14460 If necessary compute the name of the dynamic reloc section based
14461 on SEC's name (looked up in ABFD's string table) and the setting
14462 of IS_RELA. */
14463
14464 asection *
14465 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14466 asection * sec,
14467 bfd_boolean is_rela)
14468 {
14469 asection * reloc_sec = elf_section_data (sec)->sreloc;
14470
14471 if (reloc_sec == NULL)
14472 {
14473 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14474
14475 if (name != NULL)
14476 {
14477 reloc_sec = bfd_get_linker_section (abfd, name);
14478
14479 if (reloc_sec != NULL)
14480 elf_section_data (sec)->sreloc = reloc_sec;
14481 }
14482 }
14483
14484 return reloc_sec;
14485 }
14486
14487 /* Returns the dynamic reloc section associated with SEC. If the
14488 section does not exist it is created and attached to the DYNOBJ
14489 bfd and stored in the SRELOC field of SEC's elf_section_data
14490 structure.
14491
14492 ALIGNMENT is the alignment for the newly created section and
14493 IS_RELA defines whether the name should be .rela.<SEC's name>
14494 or .rel.<SEC's name>. The section name is looked up in the
14495 string table associated with ABFD. */
14496
14497 asection *
14498 _bfd_elf_make_dynamic_reloc_section (asection *sec,
14499 bfd *dynobj,
14500 unsigned int alignment,
14501 bfd *abfd,
14502 bfd_boolean is_rela)
14503 {
14504 asection * reloc_sec = elf_section_data (sec)->sreloc;
14505
14506 if (reloc_sec == NULL)
14507 {
14508 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14509
14510 if (name == NULL)
14511 return NULL;
14512
14513 reloc_sec = bfd_get_linker_section (dynobj, name);
14514
14515 if (reloc_sec == NULL)
14516 {
14517 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14518 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
14519 if ((sec->flags & SEC_ALLOC) != 0)
14520 flags |= SEC_ALLOC | SEC_LOAD;
14521
14522 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
14523 if (reloc_sec != NULL)
14524 {
14525 /* _bfd_elf_get_sec_type_attr chooses a section type by
14526 name. Override as it may be wrong, eg. for a user
14527 section named "auto" we'll get ".relauto" which is
14528 seen to be a .rela section. */
14529 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
14530 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14531 reloc_sec = NULL;
14532 }
14533 }
14534
14535 elf_section_data (sec)->sreloc = reloc_sec;
14536 }
14537
14538 return reloc_sec;
14539 }
14540
14541 /* Copy the ELF symbol type and other attributes for a linker script
14542 assignment from HSRC to HDEST. Generally this should be treated as
14543 if we found a strong non-dynamic definition for HDEST (except that
14544 ld ignores multiple definition errors). */
14545 void
14546 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14547 struct bfd_link_hash_entry *hdest,
14548 struct bfd_link_hash_entry *hsrc)
14549 {
14550 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14551 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14552 Elf_Internal_Sym isym;
14553
14554 ehdest->type = ehsrc->type;
14555 ehdest->target_internal = ehsrc->target_internal;
14556
14557 isym.st_other = ehsrc->other;
14558 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
14559 }
14560
14561 /* Append a RELA relocation REL to section S in BFD. */
14562
14563 void
14564 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14565 {
14566 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14567 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14568 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14569 bed->s->swap_reloca_out (abfd, rel, loc);
14570 }
14571
14572 /* Append a REL relocation REL to section S in BFD. */
14573
14574 void
14575 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14576 {
14577 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14578 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14579 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
14580 bed->s->swap_reloc_out (abfd, rel, loc);
14581 }
14582
14583 /* Define __start, __stop, .startof. or .sizeof. symbol. */
14584
14585 struct bfd_link_hash_entry *
14586 bfd_elf_define_start_stop (struct bfd_link_info *info,
14587 const char *symbol, asection *sec)
14588 {
14589 struct elf_link_hash_entry *h;
14590
14591 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14592 FALSE, FALSE, TRUE);
14593 if (h != NULL
14594 && (h->root.type == bfd_link_hash_undefined
14595 || h->root.type == bfd_link_hash_undefweak
14596 || ((h->ref_regular || h->def_dynamic) && !h->def_regular)))
14597 {
14598 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
14599 h->root.type = bfd_link_hash_defined;
14600 h->root.u.def.section = sec;
14601 h->root.u.def.value = 0;
14602 h->def_regular = 1;
14603 h->def_dynamic = 0;
14604 h->start_stop = 1;
14605 h->u2.start_stop_section = sec;
14606 if (symbol[0] == '.')
14607 {
14608 /* .startof. and .sizeof. symbols are local. */
14609 const struct elf_backend_data *bed;
14610 bed = get_elf_backend_data (info->output_bfd);
14611 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
14612 }
14613 else
14614 {
14615 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14616 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
14617 if (was_dynamic)
14618 bfd_elf_link_record_dynamic_symbol (info, h);
14619 }
14620 return &h->root;
14621 }
14622 return NULL;
14623 }