Fix an illegal memory access when parsing a corrupt ELF file.
[deliverable/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)
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 (p == htab->tls_sec)
904 return FALSE;
905
906 if (htab->text_index_section != NULL)
907 return p != htab->text_index_section && p != htab->data_index_section;
908
909 return (htab->dynobj != NULL
910 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
911 && ip->output_section == p);
912
913 /* There shouldn't be section relative relocations
914 against any other section. */
915 default:
916 return TRUE;
917 }
918 }
919
920 bfd_boolean
921 _bfd_elf_omit_section_dynsym_all
922 (bfd *output_bfd ATTRIBUTE_UNUSED,
923 struct bfd_link_info *info ATTRIBUTE_UNUSED,
924 asection *p ATTRIBUTE_UNUSED)
925 {
926 return TRUE;
927 }
928
929 /* Assign dynsym indices. In a shared library we generate a section
930 symbol for each output section, which come first. Next come symbols
931 which have been forced to local binding. Then all of the back-end
932 allocated local dynamic syms, followed by the rest of the global
933 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
934 (This prevents the early call before elf_backend_init_index_section
935 and strip_excluded_output_sections setting dynindx for sections
936 that are stripped.) */
937
938 static unsigned long
939 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
940 struct bfd_link_info *info,
941 unsigned long *section_sym_count)
942 {
943 unsigned long dynsymcount = 0;
944 bfd_boolean do_sec = section_sym_count != NULL;
945
946 if (bfd_link_pic (info)
947 || elf_hash_table (info)->is_relocatable_executable)
948 {
949 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
950 asection *p;
951 for (p = output_bfd->sections; p ; p = p->next)
952 if ((p->flags & SEC_EXCLUDE) == 0
953 && (p->flags & SEC_ALLOC) != 0
954 && elf_hash_table (info)->dynamic_relocs
955 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
956 {
957 ++dynsymcount;
958 if (do_sec)
959 elf_section_data (p)->dynindx = dynsymcount;
960 }
961 else if (do_sec)
962 elf_section_data (p)->dynindx = 0;
963 }
964 if (do_sec)
965 *section_sym_count = dynsymcount;
966
967 elf_link_hash_traverse (elf_hash_table (info),
968 elf_link_renumber_local_hash_table_dynsyms,
969 &dynsymcount);
970
971 if (elf_hash_table (info)->dynlocal)
972 {
973 struct elf_link_local_dynamic_entry *p;
974 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
975 p->dynindx = ++dynsymcount;
976 }
977 elf_hash_table (info)->local_dynsymcount = dynsymcount;
978
979 elf_link_hash_traverse (elf_hash_table (info),
980 elf_link_renumber_hash_table_dynsyms,
981 &dynsymcount);
982
983 /* There is an unused NULL entry at the head of the table which we
984 must account for in our count even if the table is empty since it
985 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
986 .dynamic section. */
987 dynsymcount++;
988
989 elf_hash_table (info)->dynsymcount = dynsymcount;
990 return dynsymcount;
991 }
992
993 /* Merge st_other field. */
994
995 static void
996 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
997 const Elf_Internal_Sym *isym, asection *sec,
998 bfd_boolean definition, bfd_boolean dynamic)
999 {
1000 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1001
1002 /* If st_other has a processor-specific meaning, specific
1003 code might be needed here. */
1004 if (bed->elf_backend_merge_symbol_attribute)
1005 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
1006 dynamic);
1007
1008 if (!dynamic)
1009 {
1010 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
1011 unsigned hvis = ELF_ST_VISIBILITY (h->other);
1012
1013 /* Keep the most constraining visibility. Leave the remainder
1014 of the st_other field to elf_backend_merge_symbol_attribute. */
1015 if (symvis - 1 < hvis - 1)
1016 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1017 }
1018 else if (definition
1019 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
1020 && (sec->flags & SEC_READONLY) == 0)
1021 h->protected_def = 1;
1022 }
1023
1024 /* This function is called when we want to merge a new symbol with an
1025 existing symbol. It handles the various cases which arise when we
1026 find a definition in a dynamic object, or when there is already a
1027 definition in a dynamic object. The new symbol is described by
1028 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1029 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1030 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1031 of an old common symbol. We set OVERRIDE if the old symbol is
1032 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1033 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1034 to change. By OK to change, we mean that we shouldn't warn if the
1035 type or size does change. */
1036
1037 static bfd_boolean
1038 _bfd_elf_merge_symbol (bfd *abfd,
1039 struct bfd_link_info *info,
1040 const char *name,
1041 Elf_Internal_Sym *sym,
1042 asection **psec,
1043 bfd_vma *pvalue,
1044 struct elf_link_hash_entry **sym_hash,
1045 bfd **poldbfd,
1046 bfd_boolean *pold_weak,
1047 unsigned int *pold_alignment,
1048 bfd_boolean *skip,
1049 bfd_boolean *override,
1050 bfd_boolean *type_change_ok,
1051 bfd_boolean *size_change_ok,
1052 bfd_boolean *matched)
1053 {
1054 asection *sec, *oldsec;
1055 struct elf_link_hash_entry *h;
1056 struct elf_link_hash_entry *hi;
1057 struct elf_link_hash_entry *flip;
1058 int bind;
1059 bfd *oldbfd;
1060 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1061 bfd_boolean newweak, oldweak, newfunc, oldfunc;
1062 const struct elf_backend_data *bed;
1063 char *new_version;
1064 bfd_boolean default_sym = *matched;
1065
1066 *skip = FALSE;
1067 *override = FALSE;
1068
1069 sec = *psec;
1070 bind = ELF_ST_BIND (sym->st_info);
1071
1072 if (! bfd_is_und_section (sec))
1073 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1074 else
1075 h = ((struct elf_link_hash_entry *)
1076 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1077 if (h == NULL)
1078 return FALSE;
1079 *sym_hash = h;
1080
1081 bed = get_elf_backend_data (abfd);
1082
1083 /* NEW_VERSION is the symbol version of the new symbol. */
1084 if (h->versioned != unversioned)
1085 {
1086 /* Symbol version is unknown or versioned. */
1087 new_version = strrchr (name, ELF_VER_CHR);
1088 if (new_version)
1089 {
1090 if (h->versioned == unknown)
1091 {
1092 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1093 h->versioned = versioned_hidden;
1094 else
1095 h->versioned = versioned;
1096 }
1097 new_version += 1;
1098 if (new_version[0] == '\0')
1099 new_version = NULL;
1100 }
1101 else
1102 h->versioned = unversioned;
1103 }
1104 else
1105 new_version = NULL;
1106
1107 /* For merging, we only care about real symbols. But we need to make
1108 sure that indirect symbol dynamic flags are updated. */
1109 hi = h;
1110 while (h->root.type == bfd_link_hash_indirect
1111 || h->root.type == bfd_link_hash_warning)
1112 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1113
1114 if (!*matched)
1115 {
1116 if (hi == h || h->root.type == bfd_link_hash_new)
1117 *matched = TRUE;
1118 else
1119 {
1120 /* OLD_HIDDEN is true if the existing symbol is only visible
1121 to the symbol with the same symbol version. NEW_HIDDEN is
1122 true if the new symbol is only visible to the symbol with
1123 the same symbol version. */
1124 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1125 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1126 if (!old_hidden && !new_hidden)
1127 /* The new symbol matches the existing symbol if both
1128 aren't hidden. */
1129 *matched = TRUE;
1130 else
1131 {
1132 /* OLD_VERSION is the symbol version of the existing
1133 symbol. */
1134 char *old_version;
1135
1136 if (h->versioned >= versioned)
1137 old_version = strrchr (h->root.root.string,
1138 ELF_VER_CHR) + 1;
1139 else
1140 old_version = NULL;
1141
1142 /* The new symbol matches the existing symbol if they
1143 have the same symbol version. */
1144 *matched = (old_version == new_version
1145 || (old_version != NULL
1146 && new_version != NULL
1147 && strcmp (old_version, new_version) == 0));
1148 }
1149 }
1150 }
1151
1152 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1153 existing symbol. */
1154
1155 oldbfd = NULL;
1156 oldsec = NULL;
1157 switch (h->root.type)
1158 {
1159 default:
1160 break;
1161
1162 case bfd_link_hash_undefined:
1163 case bfd_link_hash_undefweak:
1164 oldbfd = h->root.u.undef.abfd;
1165 break;
1166
1167 case bfd_link_hash_defined:
1168 case bfd_link_hash_defweak:
1169 oldbfd = h->root.u.def.section->owner;
1170 oldsec = h->root.u.def.section;
1171 break;
1172
1173 case bfd_link_hash_common:
1174 oldbfd = h->root.u.c.p->section->owner;
1175 oldsec = h->root.u.c.p->section;
1176 if (pold_alignment)
1177 *pold_alignment = h->root.u.c.p->alignment_power;
1178 break;
1179 }
1180 if (poldbfd && *poldbfd == NULL)
1181 *poldbfd = oldbfd;
1182
1183 /* Differentiate strong and weak symbols. */
1184 newweak = bind == STB_WEAK;
1185 oldweak = (h->root.type == bfd_link_hash_defweak
1186 || h->root.type == bfd_link_hash_undefweak);
1187 if (pold_weak)
1188 *pold_weak = oldweak;
1189
1190 /* We have to check it for every instance since the first few may be
1191 references and not all compilers emit symbol type for undefined
1192 symbols. */
1193 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1194
1195 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1196 respectively, is from a dynamic object. */
1197
1198 newdyn = (abfd->flags & DYNAMIC) != 0;
1199
1200 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1201 syms and defined syms in dynamic libraries respectively.
1202 ref_dynamic on the other hand can be set for a symbol defined in
1203 a dynamic library, and def_dynamic may not be set; When the
1204 definition in a dynamic lib is overridden by a definition in the
1205 executable use of the symbol in the dynamic lib becomes a
1206 reference to the executable symbol. */
1207 if (newdyn)
1208 {
1209 if (bfd_is_und_section (sec))
1210 {
1211 if (bind != STB_WEAK)
1212 {
1213 h->ref_dynamic_nonweak = 1;
1214 hi->ref_dynamic_nonweak = 1;
1215 }
1216 }
1217 else
1218 {
1219 /* Update the existing symbol only if they match. */
1220 if (*matched)
1221 h->dynamic_def = 1;
1222 hi->dynamic_def = 1;
1223 }
1224 }
1225
1226 /* If we just created the symbol, mark it as being an ELF symbol.
1227 Other than that, there is nothing to do--there is no merge issue
1228 with a newly defined symbol--so we just return. */
1229
1230 if (h->root.type == bfd_link_hash_new)
1231 {
1232 h->non_elf = 0;
1233 return TRUE;
1234 }
1235
1236 /* In cases involving weak versioned symbols, we may wind up trying
1237 to merge a symbol with itself. Catch that here, to avoid the
1238 confusion that results if we try to override a symbol with
1239 itself. The additional tests catch cases like
1240 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1241 dynamic object, which we do want to handle here. */
1242 if (abfd == oldbfd
1243 && (newweak || oldweak)
1244 && ((abfd->flags & DYNAMIC) == 0
1245 || !h->def_regular))
1246 return TRUE;
1247
1248 olddyn = FALSE;
1249 if (oldbfd != NULL)
1250 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1251 else if (oldsec != NULL)
1252 {
1253 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1254 indices used by MIPS ELF. */
1255 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1256 }
1257
1258 /* Handle a case where plugin_notice won't be called and thus won't
1259 set the non_ir_ref flags on the first pass over symbols. */
1260 if (oldbfd != NULL
1261 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1262 && newdyn != olddyn)
1263 {
1264 h->root.non_ir_ref_dynamic = TRUE;
1265 hi->root.non_ir_ref_dynamic = TRUE;
1266 }
1267
1268 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1269 respectively, appear to be a definition rather than reference. */
1270
1271 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1272
1273 olddef = (h->root.type != bfd_link_hash_undefined
1274 && h->root.type != bfd_link_hash_undefweak
1275 && h->root.type != bfd_link_hash_common);
1276
1277 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1278 respectively, appear to be a function. */
1279
1280 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1281 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1282
1283 oldfunc = (h->type != STT_NOTYPE
1284 && bed->is_function_type (h->type));
1285
1286 if (!(newfunc && oldfunc)
1287 && ELF_ST_TYPE (sym->st_info) != h->type
1288 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1289 && h->type != STT_NOTYPE
1290 && (newdef || bfd_is_com_section (sec))
1291 && (olddef || h->root.type == bfd_link_hash_common))
1292 {
1293 /* If creating a default indirect symbol ("foo" or "foo@") from
1294 a dynamic versioned definition ("foo@@") skip doing so if
1295 there is an existing regular definition with a different
1296 type. We don't want, for example, a "time" variable in the
1297 executable overriding a "time" function in a shared library. */
1298 if (newdyn
1299 && !olddyn)
1300 {
1301 *skip = TRUE;
1302 return TRUE;
1303 }
1304
1305 /* When adding a symbol from a regular object file after we have
1306 created indirect symbols, undo the indirection and any
1307 dynamic state. */
1308 if (hi != h
1309 && !newdyn
1310 && olddyn)
1311 {
1312 h = hi;
1313 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1314 h->forced_local = 0;
1315 h->ref_dynamic = 0;
1316 h->def_dynamic = 0;
1317 h->dynamic_def = 0;
1318 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1319 {
1320 h->root.type = bfd_link_hash_undefined;
1321 h->root.u.undef.abfd = abfd;
1322 }
1323 else
1324 {
1325 h->root.type = bfd_link_hash_new;
1326 h->root.u.undef.abfd = NULL;
1327 }
1328 return TRUE;
1329 }
1330 }
1331
1332 /* Check TLS symbols. We don't check undefined symbols introduced
1333 by "ld -u" which have no type (and oldbfd NULL), and we don't
1334 check symbols from plugins because they also have no type. */
1335 if (oldbfd != NULL
1336 && (oldbfd->flags & BFD_PLUGIN) == 0
1337 && (abfd->flags & BFD_PLUGIN) == 0
1338 && ELF_ST_TYPE (sym->st_info) != h->type
1339 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1340 {
1341 bfd *ntbfd, *tbfd;
1342 bfd_boolean ntdef, tdef;
1343 asection *ntsec, *tsec;
1344
1345 if (h->type == STT_TLS)
1346 {
1347 ntbfd = abfd;
1348 ntsec = sec;
1349 ntdef = newdef;
1350 tbfd = oldbfd;
1351 tsec = oldsec;
1352 tdef = olddef;
1353 }
1354 else
1355 {
1356 ntbfd = oldbfd;
1357 ntsec = oldsec;
1358 ntdef = olddef;
1359 tbfd = abfd;
1360 tsec = sec;
1361 tdef = newdef;
1362 }
1363
1364 if (tdef && ntdef)
1365 _bfd_error_handler
1366 /* xgettext:c-format */
1367 (_("%s: TLS definition in %pB section %pA "
1368 "mismatches non-TLS definition in %pB section %pA"),
1369 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1370 else if (!tdef && !ntdef)
1371 _bfd_error_handler
1372 /* xgettext:c-format */
1373 (_("%s: TLS reference in %pB "
1374 "mismatches non-TLS reference in %pB"),
1375 h->root.root.string, tbfd, ntbfd);
1376 else if (tdef)
1377 _bfd_error_handler
1378 /* xgettext:c-format */
1379 (_("%s: TLS definition in %pB section %pA "
1380 "mismatches non-TLS reference in %pB"),
1381 h->root.root.string, tbfd, tsec, ntbfd);
1382 else
1383 _bfd_error_handler
1384 /* xgettext:c-format */
1385 (_("%s: TLS reference in %pB "
1386 "mismatches non-TLS definition in %pB section %pA"),
1387 h->root.root.string, tbfd, ntbfd, ntsec);
1388
1389 bfd_set_error (bfd_error_bad_value);
1390 return FALSE;
1391 }
1392
1393 /* If the old symbol has non-default visibility, we ignore the new
1394 definition from a dynamic object. */
1395 if (newdyn
1396 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1397 && !bfd_is_und_section (sec))
1398 {
1399 *skip = TRUE;
1400 /* Make sure this symbol is dynamic. */
1401 h->ref_dynamic = 1;
1402 hi->ref_dynamic = 1;
1403 /* A protected symbol has external availability. Make sure it is
1404 recorded as dynamic.
1405
1406 FIXME: Should we check type and size for protected symbol? */
1407 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1408 return bfd_elf_link_record_dynamic_symbol (info, h);
1409 else
1410 return TRUE;
1411 }
1412 else if (!newdyn
1413 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1414 && h->def_dynamic)
1415 {
1416 /* If the new symbol with non-default visibility comes from a
1417 relocatable file and the old definition comes from a dynamic
1418 object, we remove the old definition. */
1419 if (hi->root.type == bfd_link_hash_indirect)
1420 {
1421 /* Handle the case where the old dynamic definition is
1422 default versioned. We need to copy the symbol info from
1423 the symbol with default version to the normal one if it
1424 was referenced before. */
1425 if (h->ref_regular)
1426 {
1427 hi->root.type = h->root.type;
1428 h->root.type = bfd_link_hash_indirect;
1429 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1430
1431 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1432 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1433 {
1434 /* If the new symbol is hidden or internal, completely undo
1435 any dynamic link state. */
1436 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1437 h->forced_local = 0;
1438 h->ref_dynamic = 0;
1439 }
1440 else
1441 h->ref_dynamic = 1;
1442
1443 h->def_dynamic = 0;
1444 /* FIXME: Should we check type and size for protected symbol? */
1445 h->size = 0;
1446 h->type = 0;
1447
1448 h = hi;
1449 }
1450 else
1451 h = hi;
1452 }
1453
1454 /* If the old symbol was undefined before, then it will still be
1455 on the undefs list. If the new symbol is undefined or
1456 common, we can't make it bfd_link_hash_new here, because new
1457 undefined or common symbols will be added to the undefs list
1458 by _bfd_generic_link_add_one_symbol. Symbols may not be
1459 added twice to the undefs list. Also, if the new symbol is
1460 undefweak then we don't want to lose the strong undef. */
1461 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1462 {
1463 h->root.type = bfd_link_hash_undefined;
1464 h->root.u.undef.abfd = abfd;
1465 }
1466 else
1467 {
1468 h->root.type = bfd_link_hash_new;
1469 h->root.u.undef.abfd = NULL;
1470 }
1471
1472 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1473 {
1474 /* If the new symbol is hidden or internal, completely undo
1475 any dynamic link state. */
1476 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1477 h->forced_local = 0;
1478 h->ref_dynamic = 0;
1479 }
1480 else
1481 h->ref_dynamic = 1;
1482 h->def_dynamic = 0;
1483 /* FIXME: Should we check type and size for protected symbol? */
1484 h->size = 0;
1485 h->type = 0;
1486 return TRUE;
1487 }
1488
1489 /* If a new weak symbol definition comes from a regular file and the
1490 old symbol comes from a dynamic library, we treat the new one as
1491 strong. Similarly, an old weak symbol definition from a regular
1492 file is treated as strong when the new symbol comes from a dynamic
1493 library. Further, an old weak symbol from a dynamic library is
1494 treated as strong if the new symbol is from a dynamic library.
1495 This reflects the way glibc's ld.so works.
1496
1497 Also allow a weak symbol to override a linker script symbol
1498 defined by an early pass over the script. This is done so the
1499 linker knows the symbol is defined in an object file, for the
1500 DEFINED script function.
1501
1502 Do this before setting *type_change_ok or *size_change_ok so that
1503 we warn properly when dynamic library symbols are overridden. */
1504
1505 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1506 newweak = FALSE;
1507 if (olddef && newdyn)
1508 oldweak = FALSE;
1509
1510 /* Allow changes between different types of function symbol. */
1511 if (newfunc && oldfunc)
1512 *type_change_ok = TRUE;
1513
1514 /* It's OK to change the type if either the existing symbol or the
1515 new symbol is weak. A type change is also OK if the old symbol
1516 is undefined and the new symbol is defined. */
1517
1518 if (oldweak
1519 || newweak
1520 || (newdef
1521 && h->root.type == bfd_link_hash_undefined))
1522 *type_change_ok = TRUE;
1523
1524 /* It's OK to change the size if either the existing symbol or the
1525 new symbol is weak, or if the old symbol is undefined. */
1526
1527 if (*type_change_ok
1528 || h->root.type == bfd_link_hash_undefined)
1529 *size_change_ok = TRUE;
1530
1531 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1532 symbol, respectively, appears to be a common symbol in a dynamic
1533 object. If a symbol appears in an uninitialized section, and is
1534 not weak, and is not a function, then it may be a common symbol
1535 which was resolved when the dynamic object was created. We want
1536 to treat such symbols specially, because they raise special
1537 considerations when setting the symbol size: if the symbol
1538 appears as a common symbol in a regular object, and the size in
1539 the regular object is larger, we must make sure that we use the
1540 larger size. This problematic case can always be avoided in C,
1541 but it must be handled correctly when using Fortran shared
1542 libraries.
1543
1544 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1545 likewise for OLDDYNCOMMON and OLDDEF.
1546
1547 Note that this test is just a heuristic, and that it is quite
1548 possible to have an uninitialized symbol in a shared object which
1549 is really a definition, rather than a common symbol. This could
1550 lead to some minor confusion when the symbol really is a common
1551 symbol in some regular object. However, I think it will be
1552 harmless. */
1553
1554 if (newdyn
1555 && newdef
1556 && !newweak
1557 && (sec->flags & SEC_ALLOC) != 0
1558 && (sec->flags & SEC_LOAD) == 0
1559 && sym->st_size > 0
1560 && !newfunc)
1561 newdyncommon = TRUE;
1562 else
1563 newdyncommon = FALSE;
1564
1565 if (olddyn
1566 && olddef
1567 && h->root.type == bfd_link_hash_defined
1568 && h->def_dynamic
1569 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1570 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1571 && h->size > 0
1572 && !oldfunc)
1573 olddyncommon = TRUE;
1574 else
1575 olddyncommon = FALSE;
1576
1577 /* We now know everything about the old and new symbols. We ask the
1578 backend to check if we can merge them. */
1579 if (bed->merge_symbol != NULL)
1580 {
1581 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1582 return FALSE;
1583 sec = *psec;
1584 }
1585
1586 /* There are multiple definitions of a normal symbol. Skip the
1587 default symbol as well as definition from an IR object. */
1588 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1589 && !default_sym && h->def_regular
1590 && !(oldbfd != NULL
1591 && (oldbfd->flags & BFD_PLUGIN) != 0
1592 && (abfd->flags & BFD_PLUGIN) == 0))
1593 {
1594 /* Handle a multiple definition. */
1595 (*info->callbacks->multiple_definition) (info, &h->root,
1596 abfd, sec, *pvalue);
1597 *skip = TRUE;
1598 return TRUE;
1599 }
1600
1601 /* If both the old and the new symbols look like common symbols in a
1602 dynamic object, set the size of the symbol to the larger of the
1603 two. */
1604
1605 if (olddyncommon
1606 && newdyncommon
1607 && sym->st_size != h->size)
1608 {
1609 /* Since we think we have two common symbols, issue a multiple
1610 common warning if desired. Note that we only warn if the
1611 size is different. If the size is the same, we simply let
1612 the old symbol override the new one as normally happens with
1613 symbols defined in dynamic objects. */
1614
1615 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1616 bfd_link_hash_common, sym->st_size);
1617 if (sym->st_size > h->size)
1618 h->size = sym->st_size;
1619
1620 *size_change_ok = TRUE;
1621 }
1622
1623 /* If we are looking at a dynamic object, and we have found a
1624 definition, we need to see if the symbol was already defined by
1625 some other object. If so, we want to use the existing
1626 definition, and we do not want to report a multiple symbol
1627 definition error; we do this by clobbering *PSEC to be
1628 bfd_und_section_ptr.
1629
1630 We treat a common symbol as a definition if the symbol in the
1631 shared library is a function, since common symbols always
1632 represent variables; this can cause confusion in principle, but
1633 any such confusion would seem to indicate an erroneous program or
1634 shared library. We also permit a common symbol in a regular
1635 object to override a weak symbol in a shared object. */
1636
1637 if (newdyn
1638 && newdef
1639 && (olddef
1640 || (h->root.type == bfd_link_hash_common
1641 && (newweak || newfunc))))
1642 {
1643 *override = TRUE;
1644 newdef = FALSE;
1645 newdyncommon = FALSE;
1646
1647 *psec = sec = bfd_und_section_ptr;
1648 *size_change_ok = TRUE;
1649
1650 /* If we get here when the old symbol is a common symbol, then
1651 we are explicitly letting it override a weak symbol or
1652 function in a dynamic object, and we don't want to warn about
1653 a type change. If the old symbol is a defined symbol, a type
1654 change warning may still be appropriate. */
1655
1656 if (h->root.type == bfd_link_hash_common)
1657 *type_change_ok = TRUE;
1658 }
1659
1660 /* Handle the special case of an old common symbol merging with a
1661 new symbol which looks like a common symbol in a shared object.
1662 We change *PSEC and *PVALUE to make the new symbol look like a
1663 common symbol, and let _bfd_generic_link_add_one_symbol do the
1664 right thing. */
1665
1666 if (newdyncommon
1667 && h->root.type == bfd_link_hash_common)
1668 {
1669 *override = TRUE;
1670 newdef = FALSE;
1671 newdyncommon = FALSE;
1672 *pvalue = sym->st_size;
1673 *psec = sec = bed->common_section (oldsec);
1674 *size_change_ok = TRUE;
1675 }
1676
1677 /* Skip weak definitions of symbols that are already defined. */
1678 if (newdef && olddef && newweak)
1679 {
1680 /* Don't skip new non-IR weak syms. */
1681 if (!(oldbfd != NULL
1682 && (oldbfd->flags & BFD_PLUGIN) != 0
1683 && (abfd->flags & BFD_PLUGIN) == 0))
1684 {
1685 newdef = FALSE;
1686 *skip = TRUE;
1687 }
1688
1689 /* Merge st_other. If the symbol already has a dynamic index,
1690 but visibility says it should not be visible, turn it into a
1691 local symbol. */
1692 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1693 if (h->dynindx != -1)
1694 switch (ELF_ST_VISIBILITY (h->other))
1695 {
1696 case STV_INTERNAL:
1697 case STV_HIDDEN:
1698 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1699 break;
1700 }
1701 }
1702
1703 /* If the old symbol is from a dynamic object, and the new symbol is
1704 a definition which is not from a dynamic object, then the new
1705 symbol overrides the old symbol. Symbols from regular files
1706 always take precedence over symbols from dynamic objects, even if
1707 they are defined after the dynamic object in the link.
1708
1709 As above, we again permit a common symbol in a regular object to
1710 override a definition in a shared object if the shared object
1711 symbol is a function or is weak. */
1712
1713 flip = NULL;
1714 if (!newdyn
1715 && (newdef
1716 || (bfd_is_com_section (sec)
1717 && (oldweak || oldfunc)))
1718 && olddyn
1719 && olddef
1720 && h->def_dynamic)
1721 {
1722 /* Change the hash table entry to undefined, and let
1723 _bfd_generic_link_add_one_symbol do the right thing with the
1724 new definition. */
1725
1726 h->root.type = bfd_link_hash_undefined;
1727 h->root.u.undef.abfd = h->root.u.def.section->owner;
1728 *size_change_ok = TRUE;
1729
1730 olddef = FALSE;
1731 olddyncommon = FALSE;
1732
1733 /* We again permit a type change when a common symbol may be
1734 overriding a function. */
1735
1736 if (bfd_is_com_section (sec))
1737 {
1738 if (oldfunc)
1739 {
1740 /* If a common symbol overrides a function, make sure
1741 that it isn't defined dynamically nor has type
1742 function. */
1743 h->def_dynamic = 0;
1744 h->type = STT_NOTYPE;
1745 }
1746 *type_change_ok = TRUE;
1747 }
1748
1749 if (hi->root.type == bfd_link_hash_indirect)
1750 flip = hi;
1751 else
1752 /* This union may have been set to be non-NULL when this symbol
1753 was seen in a dynamic object. We must force the union to be
1754 NULL, so that it is correct for a regular symbol. */
1755 h->verinfo.vertree = NULL;
1756 }
1757
1758 /* Handle the special case of a new common symbol merging with an
1759 old symbol that looks like it might be a common symbol defined in
1760 a shared object. Note that we have already handled the case in
1761 which a new common symbol should simply override the definition
1762 in the shared library. */
1763
1764 if (! newdyn
1765 && bfd_is_com_section (sec)
1766 && olddyncommon)
1767 {
1768 /* It would be best if we could set the hash table entry to a
1769 common symbol, but we don't know what to use for the section
1770 or the alignment. */
1771 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1772 bfd_link_hash_common, sym->st_size);
1773
1774 /* If the presumed common symbol in the dynamic object is
1775 larger, pretend that the new symbol has its size. */
1776
1777 if (h->size > *pvalue)
1778 *pvalue = h->size;
1779
1780 /* We need to remember the alignment required by the symbol
1781 in the dynamic object. */
1782 BFD_ASSERT (pold_alignment);
1783 *pold_alignment = h->root.u.def.section->alignment_power;
1784
1785 olddef = FALSE;
1786 olddyncommon = FALSE;
1787
1788 h->root.type = bfd_link_hash_undefined;
1789 h->root.u.undef.abfd = h->root.u.def.section->owner;
1790
1791 *size_change_ok = TRUE;
1792 *type_change_ok = TRUE;
1793
1794 if (hi->root.type == bfd_link_hash_indirect)
1795 flip = hi;
1796 else
1797 h->verinfo.vertree = NULL;
1798 }
1799
1800 if (flip != NULL)
1801 {
1802 /* Handle the case where we had a versioned symbol in a dynamic
1803 library and now find a definition in a normal object. In this
1804 case, we make the versioned symbol point to the normal one. */
1805 flip->root.type = h->root.type;
1806 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1807 h->root.type = bfd_link_hash_indirect;
1808 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1809 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1810 if (h->def_dynamic)
1811 {
1812 h->def_dynamic = 0;
1813 flip->ref_dynamic = 1;
1814 }
1815 }
1816
1817 return TRUE;
1818 }
1819
1820 /* This function is called to create an indirect symbol from the
1821 default for the symbol with the default version if needed. The
1822 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1823 set DYNSYM if the new indirect symbol is dynamic. */
1824
1825 static bfd_boolean
1826 _bfd_elf_add_default_symbol (bfd *abfd,
1827 struct bfd_link_info *info,
1828 struct elf_link_hash_entry *h,
1829 const char *name,
1830 Elf_Internal_Sym *sym,
1831 asection *sec,
1832 bfd_vma value,
1833 bfd **poldbfd,
1834 bfd_boolean *dynsym)
1835 {
1836 bfd_boolean type_change_ok;
1837 bfd_boolean size_change_ok;
1838 bfd_boolean skip;
1839 char *shortname;
1840 struct elf_link_hash_entry *hi;
1841 struct bfd_link_hash_entry *bh;
1842 const struct elf_backend_data *bed;
1843 bfd_boolean collect;
1844 bfd_boolean dynamic;
1845 bfd_boolean override;
1846 char *p;
1847 size_t len, shortlen;
1848 asection *tmp_sec;
1849 bfd_boolean matched;
1850
1851 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1852 return TRUE;
1853
1854 /* If this symbol has a version, and it is the default version, we
1855 create an indirect symbol from the default name to the fully
1856 decorated name. This will cause external references which do not
1857 specify a version to be bound to this version of the symbol. */
1858 p = strchr (name, ELF_VER_CHR);
1859 if (h->versioned == unknown)
1860 {
1861 if (p == NULL)
1862 {
1863 h->versioned = unversioned;
1864 return TRUE;
1865 }
1866 else
1867 {
1868 if (p[1] != ELF_VER_CHR)
1869 {
1870 h->versioned = versioned_hidden;
1871 return TRUE;
1872 }
1873 else
1874 h->versioned = versioned;
1875 }
1876 }
1877 else
1878 {
1879 /* PR ld/19073: We may see an unversioned definition after the
1880 default version. */
1881 if (p == NULL)
1882 return TRUE;
1883 }
1884
1885 bed = get_elf_backend_data (abfd);
1886 collect = bed->collect;
1887 dynamic = (abfd->flags & DYNAMIC) != 0;
1888
1889 shortlen = p - name;
1890 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1891 if (shortname == NULL)
1892 return FALSE;
1893 memcpy (shortname, name, shortlen);
1894 shortname[shortlen] = '\0';
1895
1896 /* We are going to create a new symbol. Merge it with any existing
1897 symbol with this name. For the purposes of the merge, act as
1898 though we were defining the symbol we just defined, although we
1899 actually going to define an indirect symbol. */
1900 type_change_ok = FALSE;
1901 size_change_ok = FALSE;
1902 matched = TRUE;
1903 tmp_sec = sec;
1904 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1905 &hi, poldbfd, NULL, NULL, &skip, &override,
1906 &type_change_ok, &size_change_ok, &matched))
1907 return FALSE;
1908
1909 if (skip)
1910 goto nondefault;
1911
1912 if (hi->def_regular)
1913 {
1914 /* If the undecorated symbol will have a version added by a
1915 script different to H, then don't indirect to/from the
1916 undecorated symbol. This isn't ideal because we may not yet
1917 have seen symbol versions, if given by a script on the
1918 command line rather than via --version-script. */
1919 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1920 {
1921 bfd_boolean hide;
1922
1923 hi->verinfo.vertree
1924 = bfd_find_version_for_sym (info->version_info,
1925 hi->root.root.string, &hide);
1926 if (hi->verinfo.vertree != NULL && hide)
1927 {
1928 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1929 goto nondefault;
1930 }
1931 }
1932 if (hi->verinfo.vertree != NULL
1933 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1934 goto nondefault;
1935 }
1936
1937 if (! override)
1938 {
1939 /* Add the default symbol if not performing a relocatable link. */
1940 if (! bfd_link_relocatable (info))
1941 {
1942 bh = &hi->root;
1943 if (bh->type == bfd_link_hash_defined
1944 && bh->u.def.section->owner != NULL
1945 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
1946 {
1947 /* Mark the previous definition from IR object as
1948 undefined so that the generic linker will override
1949 it. */
1950 bh->type = bfd_link_hash_undefined;
1951 bh->u.undef.abfd = bh->u.def.section->owner;
1952 }
1953 if (! (_bfd_generic_link_add_one_symbol
1954 (info, abfd, shortname, BSF_INDIRECT,
1955 bfd_ind_section_ptr,
1956 0, name, FALSE, collect, &bh)))
1957 return FALSE;
1958 hi = (struct elf_link_hash_entry *) bh;
1959 }
1960 }
1961 else
1962 {
1963 /* In this case the symbol named SHORTNAME is overriding the
1964 indirect symbol we want to add. We were planning on making
1965 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1966 is the name without a version. NAME is the fully versioned
1967 name, and it is the default version.
1968
1969 Overriding means that we already saw a definition for the
1970 symbol SHORTNAME in a regular object, and it is overriding
1971 the symbol defined in the dynamic object.
1972
1973 When this happens, we actually want to change NAME, the
1974 symbol we just added, to refer to SHORTNAME. This will cause
1975 references to NAME in the shared object to become references
1976 to SHORTNAME in the regular object. This is what we expect
1977 when we override a function in a shared object: that the
1978 references in the shared object will be mapped to the
1979 definition in the regular object. */
1980
1981 while (hi->root.type == bfd_link_hash_indirect
1982 || hi->root.type == bfd_link_hash_warning)
1983 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1984
1985 h->root.type = bfd_link_hash_indirect;
1986 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1987 if (h->def_dynamic)
1988 {
1989 h->def_dynamic = 0;
1990 hi->ref_dynamic = 1;
1991 if (hi->ref_regular
1992 || hi->def_regular)
1993 {
1994 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1995 return FALSE;
1996 }
1997 }
1998
1999 /* Now set HI to H, so that the following code will set the
2000 other fields correctly. */
2001 hi = h;
2002 }
2003
2004 /* Check if HI is a warning symbol. */
2005 if (hi->root.type == bfd_link_hash_warning)
2006 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2007
2008 /* If there is a duplicate definition somewhere, then HI may not
2009 point to an indirect symbol. We will have reported an error to
2010 the user in that case. */
2011
2012 if (hi->root.type == bfd_link_hash_indirect)
2013 {
2014 struct elf_link_hash_entry *ht;
2015
2016 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2017 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2018
2019 /* A reference to the SHORTNAME symbol from a dynamic library
2020 will be satisfied by the versioned symbol at runtime. In
2021 effect, we have a reference to the versioned symbol. */
2022 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2023 hi->dynamic_def |= ht->dynamic_def;
2024
2025 /* See if the new flags lead us to realize that the symbol must
2026 be dynamic. */
2027 if (! *dynsym)
2028 {
2029 if (! dynamic)
2030 {
2031 if (! bfd_link_executable (info)
2032 || hi->def_dynamic
2033 || hi->ref_dynamic)
2034 *dynsym = TRUE;
2035 }
2036 else
2037 {
2038 if (hi->ref_regular)
2039 *dynsym = TRUE;
2040 }
2041 }
2042 }
2043
2044 /* We also need to define an indirection from the nondefault version
2045 of the symbol. */
2046
2047 nondefault:
2048 len = strlen (name);
2049 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2050 if (shortname == NULL)
2051 return FALSE;
2052 memcpy (shortname, name, shortlen);
2053 memcpy (shortname + shortlen, p + 1, len - shortlen);
2054
2055 /* Once again, merge with any existing symbol. */
2056 type_change_ok = FALSE;
2057 size_change_ok = FALSE;
2058 tmp_sec = sec;
2059 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2060 &hi, poldbfd, NULL, NULL, &skip, &override,
2061 &type_change_ok, &size_change_ok, &matched))
2062 return FALSE;
2063
2064 if (skip)
2065 return TRUE;
2066
2067 if (override)
2068 {
2069 /* Here SHORTNAME is a versioned name, so we don't expect to see
2070 the type of override we do in the case above unless it is
2071 overridden by a versioned definition. */
2072 if (hi->root.type != bfd_link_hash_defined
2073 && hi->root.type != bfd_link_hash_defweak)
2074 _bfd_error_handler
2075 /* xgettext:c-format */
2076 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2077 abfd, shortname);
2078 }
2079 else
2080 {
2081 bh = &hi->root;
2082 if (! (_bfd_generic_link_add_one_symbol
2083 (info, abfd, shortname, BSF_INDIRECT,
2084 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
2085 return FALSE;
2086 hi = (struct elf_link_hash_entry *) bh;
2087
2088 /* If there is a duplicate definition somewhere, then HI may not
2089 point to an indirect symbol. We will have reported an error
2090 to the user in that case. */
2091
2092 if (hi->root.type == bfd_link_hash_indirect)
2093 {
2094 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2095 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2096 hi->dynamic_def |= h->dynamic_def;
2097
2098 /* See if the new flags lead us to realize that the symbol
2099 must be dynamic. */
2100 if (! *dynsym)
2101 {
2102 if (! dynamic)
2103 {
2104 if (! bfd_link_executable (info)
2105 || hi->ref_dynamic)
2106 *dynsym = TRUE;
2107 }
2108 else
2109 {
2110 if (hi->ref_regular)
2111 *dynsym = TRUE;
2112 }
2113 }
2114 }
2115 }
2116
2117 return TRUE;
2118 }
2119 \f
2120 /* This routine is used to export all defined symbols into the dynamic
2121 symbol table. It is called via elf_link_hash_traverse. */
2122
2123 static bfd_boolean
2124 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2125 {
2126 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2127
2128 /* Ignore indirect symbols. These are added by the versioning code. */
2129 if (h->root.type == bfd_link_hash_indirect)
2130 return TRUE;
2131
2132 /* Ignore this if we won't export it. */
2133 if (!eif->info->export_dynamic && !h->dynamic)
2134 return TRUE;
2135
2136 if (h->dynindx == -1
2137 && (h->def_regular || h->ref_regular)
2138 && ! bfd_hide_sym_by_version (eif->info->version_info,
2139 h->root.root.string))
2140 {
2141 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2142 {
2143 eif->failed = TRUE;
2144 return FALSE;
2145 }
2146 }
2147
2148 return TRUE;
2149 }
2150 \f
2151 /* Look through the symbols which are defined in other shared
2152 libraries and referenced here. Update the list of version
2153 dependencies. This will be put into the .gnu.version_r section.
2154 This function is called via elf_link_hash_traverse. */
2155
2156 static bfd_boolean
2157 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2158 void *data)
2159 {
2160 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2161 Elf_Internal_Verneed *t;
2162 Elf_Internal_Vernaux *a;
2163 bfd_size_type amt;
2164
2165 /* We only care about symbols defined in shared objects with version
2166 information. */
2167 if (!h->def_dynamic
2168 || h->def_regular
2169 || h->dynindx == -1
2170 || h->verinfo.verdef == NULL
2171 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2172 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2173 return TRUE;
2174
2175 /* See if we already know about this version. */
2176 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2177 t != NULL;
2178 t = t->vn_nextref)
2179 {
2180 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2181 continue;
2182
2183 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2184 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2185 return TRUE;
2186
2187 break;
2188 }
2189
2190 /* This is a new version. Add it to tree we are building. */
2191
2192 if (t == NULL)
2193 {
2194 amt = sizeof *t;
2195 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2196 if (t == NULL)
2197 {
2198 rinfo->failed = TRUE;
2199 return FALSE;
2200 }
2201
2202 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2203 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2204 elf_tdata (rinfo->info->output_bfd)->verref = t;
2205 }
2206
2207 amt = sizeof *a;
2208 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2209 if (a == NULL)
2210 {
2211 rinfo->failed = TRUE;
2212 return FALSE;
2213 }
2214
2215 /* Note that we are copying a string pointer here, and testing it
2216 above. If bfd_elf_string_from_elf_section is ever changed to
2217 discard the string data when low in memory, this will have to be
2218 fixed. */
2219 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2220
2221 a->vna_flags = h->verinfo.verdef->vd_flags;
2222 a->vna_nextptr = t->vn_auxptr;
2223
2224 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2225 ++rinfo->vers;
2226
2227 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2228
2229 t->vn_auxptr = a;
2230
2231 return TRUE;
2232 }
2233
2234 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2235 hidden. Set *T_P to NULL if there is no match. */
2236
2237 static bfd_boolean
2238 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2239 struct elf_link_hash_entry *h,
2240 const char *version_p,
2241 struct bfd_elf_version_tree **t_p,
2242 bfd_boolean *hide)
2243 {
2244 struct bfd_elf_version_tree *t;
2245
2246 /* Look for the version. If we find it, it is no longer weak. */
2247 for (t = info->version_info; t != NULL; t = t->next)
2248 {
2249 if (strcmp (t->name, version_p) == 0)
2250 {
2251 size_t len;
2252 char *alc;
2253 struct bfd_elf_version_expr *d;
2254
2255 len = version_p - h->root.root.string;
2256 alc = (char *) bfd_malloc (len);
2257 if (alc == NULL)
2258 return FALSE;
2259 memcpy (alc, h->root.root.string, len - 1);
2260 alc[len - 1] = '\0';
2261 if (alc[len - 2] == ELF_VER_CHR)
2262 alc[len - 2] = '\0';
2263
2264 h->verinfo.vertree = t;
2265 t->used = TRUE;
2266 d = NULL;
2267
2268 if (t->globals.list != NULL)
2269 d = (*t->match) (&t->globals, NULL, alc);
2270
2271 /* See if there is anything to force this symbol to
2272 local scope. */
2273 if (d == NULL && t->locals.list != NULL)
2274 {
2275 d = (*t->match) (&t->locals, NULL, alc);
2276 if (d != NULL
2277 && h->dynindx != -1
2278 && ! info->export_dynamic)
2279 *hide = TRUE;
2280 }
2281
2282 free (alc);
2283 break;
2284 }
2285 }
2286
2287 *t_p = t;
2288
2289 return TRUE;
2290 }
2291
2292 /* Return TRUE if the symbol H is hidden by version script. */
2293
2294 bfd_boolean
2295 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2296 struct elf_link_hash_entry *h)
2297 {
2298 const char *p;
2299 bfd_boolean hide = FALSE;
2300 const struct elf_backend_data *bed
2301 = get_elf_backend_data (info->output_bfd);
2302
2303 /* Version script only hides symbols defined in regular objects. */
2304 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2305 return TRUE;
2306
2307 p = strchr (h->root.root.string, ELF_VER_CHR);
2308 if (p != NULL && h->verinfo.vertree == NULL)
2309 {
2310 struct bfd_elf_version_tree *t;
2311
2312 ++p;
2313 if (*p == ELF_VER_CHR)
2314 ++p;
2315
2316 if (*p != '\0'
2317 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2318 && hide)
2319 {
2320 if (hide)
2321 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2322 return TRUE;
2323 }
2324 }
2325
2326 /* If we don't have a version for this symbol, see if we can find
2327 something. */
2328 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2329 {
2330 h->verinfo.vertree
2331 = bfd_find_version_for_sym (info->version_info,
2332 h->root.root.string, &hide);
2333 if (h->verinfo.vertree != NULL && hide)
2334 {
2335 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2336 return TRUE;
2337 }
2338 }
2339
2340 return FALSE;
2341 }
2342
2343 /* Figure out appropriate versions for all the symbols. We may not
2344 have the version number script until we have read all of the input
2345 files, so until that point we don't know which symbols should be
2346 local. This function is called via elf_link_hash_traverse. */
2347
2348 static bfd_boolean
2349 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2350 {
2351 struct elf_info_failed *sinfo;
2352 struct bfd_link_info *info;
2353 const struct elf_backend_data *bed;
2354 struct elf_info_failed eif;
2355 char *p;
2356 bfd_boolean hide;
2357
2358 sinfo = (struct elf_info_failed *) data;
2359 info = sinfo->info;
2360
2361 /* Fix the symbol flags. */
2362 eif.failed = FALSE;
2363 eif.info = info;
2364 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2365 {
2366 if (eif.failed)
2367 sinfo->failed = TRUE;
2368 return FALSE;
2369 }
2370
2371 bed = get_elf_backend_data (info->output_bfd);
2372
2373 /* We only need version numbers for symbols defined in regular
2374 objects. */
2375 if (!h->def_regular)
2376 {
2377 /* Hide symbols defined in discarded input sections. */
2378 if ((h->root.type == bfd_link_hash_defined
2379 || h->root.type == bfd_link_hash_defweak)
2380 && discarded_section (h->root.u.def.section))
2381 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2382 return TRUE;
2383 }
2384
2385 hide = FALSE;
2386 p = strchr (h->root.root.string, ELF_VER_CHR);
2387 if (p != NULL && h->verinfo.vertree == NULL)
2388 {
2389 struct bfd_elf_version_tree *t;
2390
2391 ++p;
2392 if (*p == ELF_VER_CHR)
2393 ++p;
2394
2395 /* If there is no version string, we can just return out. */
2396 if (*p == '\0')
2397 return TRUE;
2398
2399 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2400 {
2401 sinfo->failed = TRUE;
2402 return FALSE;
2403 }
2404
2405 if (hide)
2406 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2407
2408 /* If we are building an application, we need to create a
2409 version node for this version. */
2410 if (t == NULL && bfd_link_executable (info))
2411 {
2412 struct bfd_elf_version_tree **pp;
2413 int version_index;
2414
2415 /* If we aren't going to export this symbol, we don't need
2416 to worry about it. */
2417 if (h->dynindx == -1)
2418 return TRUE;
2419
2420 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2421 sizeof *t);
2422 if (t == NULL)
2423 {
2424 sinfo->failed = TRUE;
2425 return FALSE;
2426 }
2427
2428 t->name = p;
2429 t->name_indx = (unsigned int) -1;
2430 t->used = TRUE;
2431
2432 version_index = 1;
2433 /* Don't count anonymous version tag. */
2434 if (sinfo->info->version_info != NULL
2435 && sinfo->info->version_info->vernum == 0)
2436 version_index = 0;
2437 for (pp = &sinfo->info->version_info;
2438 *pp != NULL;
2439 pp = &(*pp)->next)
2440 ++version_index;
2441 t->vernum = version_index;
2442
2443 *pp = t;
2444
2445 h->verinfo.vertree = t;
2446 }
2447 else if (t == NULL)
2448 {
2449 /* We could not find the version for a symbol when
2450 generating a shared archive. Return an error. */
2451 _bfd_error_handler
2452 /* xgettext:c-format */
2453 (_("%pB: version node not found for symbol %s"),
2454 info->output_bfd, h->root.root.string);
2455 bfd_set_error (bfd_error_bad_value);
2456 sinfo->failed = TRUE;
2457 return FALSE;
2458 }
2459 }
2460
2461 /* If we don't have a version for this symbol, see if we can find
2462 something. */
2463 if (!hide
2464 && h->verinfo.vertree == NULL
2465 && sinfo->info->version_info != NULL)
2466 {
2467 h->verinfo.vertree
2468 = bfd_find_version_for_sym (sinfo->info->version_info,
2469 h->root.root.string, &hide);
2470 if (h->verinfo.vertree != NULL && hide)
2471 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2472 }
2473
2474 return TRUE;
2475 }
2476 \f
2477 /* Read and swap the relocs from the section indicated by SHDR. This
2478 may be either a REL or a RELA section. The relocations are
2479 translated into RELA relocations and stored in INTERNAL_RELOCS,
2480 which should have already been allocated to contain enough space.
2481 The EXTERNAL_RELOCS are a buffer where the external form of the
2482 relocations should be stored.
2483
2484 Returns FALSE if something goes wrong. */
2485
2486 static bfd_boolean
2487 elf_link_read_relocs_from_section (bfd *abfd,
2488 asection *sec,
2489 Elf_Internal_Shdr *shdr,
2490 void *external_relocs,
2491 Elf_Internal_Rela *internal_relocs)
2492 {
2493 const struct elf_backend_data *bed;
2494 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2495 const bfd_byte *erela;
2496 const bfd_byte *erelaend;
2497 Elf_Internal_Rela *irela;
2498 Elf_Internal_Shdr *symtab_hdr;
2499 size_t nsyms;
2500
2501 /* Position ourselves at the start of the section. */
2502 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2503 return FALSE;
2504
2505 /* Read the relocations. */
2506 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2507 return FALSE;
2508
2509 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2510 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2511
2512 bed = get_elf_backend_data (abfd);
2513
2514 /* Convert the external relocations to the internal format. */
2515 if (shdr->sh_entsize == bed->s->sizeof_rel)
2516 swap_in = bed->s->swap_reloc_in;
2517 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2518 swap_in = bed->s->swap_reloca_in;
2519 else
2520 {
2521 bfd_set_error (bfd_error_wrong_format);
2522 return FALSE;
2523 }
2524
2525 erela = (const bfd_byte *) external_relocs;
2526 erelaend = erela + shdr->sh_size;
2527 irela = internal_relocs;
2528 while (erela < erelaend)
2529 {
2530 bfd_vma r_symndx;
2531
2532 (*swap_in) (abfd, erela, irela);
2533 r_symndx = ELF32_R_SYM (irela->r_info);
2534 if (bed->s->arch_size == 64)
2535 r_symndx >>= 24;
2536 if (nsyms > 0)
2537 {
2538 if ((size_t) r_symndx >= nsyms)
2539 {
2540 _bfd_error_handler
2541 /* xgettext:c-format */
2542 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2543 " for offset %#" PRIx64 " in section `%pA'"),
2544 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2545 (uint64_t) irela->r_offset, sec);
2546 bfd_set_error (bfd_error_bad_value);
2547 return FALSE;
2548 }
2549 }
2550 else if (r_symndx != STN_UNDEF)
2551 {
2552 _bfd_error_handler
2553 /* xgettext:c-format */
2554 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2555 " for offset %#" PRIx64 " in section `%pA'"
2556 " when the object file has no symbol table"),
2557 abfd, (uint64_t) r_symndx,
2558 (uint64_t) irela->r_offset, sec);
2559 bfd_set_error (bfd_error_bad_value);
2560 return FALSE;
2561 }
2562 irela += bed->s->int_rels_per_ext_rel;
2563 erela += shdr->sh_entsize;
2564 }
2565
2566 return TRUE;
2567 }
2568
2569 /* Read and swap the relocs for a section O. They may have been
2570 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2571 not NULL, they are used as buffers to read into. They are known to
2572 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2573 the return value is allocated using either malloc or bfd_alloc,
2574 according to the KEEP_MEMORY argument. If O has two relocation
2575 sections (both REL and RELA relocations), then the REL_HDR
2576 relocations will appear first in INTERNAL_RELOCS, followed by the
2577 RELA_HDR relocations. */
2578
2579 Elf_Internal_Rela *
2580 _bfd_elf_link_read_relocs (bfd *abfd,
2581 asection *o,
2582 void *external_relocs,
2583 Elf_Internal_Rela *internal_relocs,
2584 bfd_boolean keep_memory)
2585 {
2586 void *alloc1 = NULL;
2587 Elf_Internal_Rela *alloc2 = NULL;
2588 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2589 struct bfd_elf_section_data *esdo = elf_section_data (o);
2590 Elf_Internal_Rela *internal_rela_relocs;
2591
2592 if (esdo->relocs != NULL)
2593 return esdo->relocs;
2594
2595 if (o->reloc_count == 0)
2596 return NULL;
2597
2598 if (internal_relocs == NULL)
2599 {
2600 bfd_size_type size;
2601
2602 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2603 if (keep_memory)
2604 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2605 else
2606 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2607 if (internal_relocs == NULL)
2608 goto error_return;
2609 }
2610
2611 if (external_relocs == NULL)
2612 {
2613 bfd_size_type size = 0;
2614
2615 if (esdo->rel.hdr)
2616 size += esdo->rel.hdr->sh_size;
2617 if (esdo->rela.hdr)
2618 size += esdo->rela.hdr->sh_size;
2619
2620 alloc1 = bfd_malloc (size);
2621 if (alloc1 == NULL)
2622 goto error_return;
2623 external_relocs = alloc1;
2624 }
2625
2626 internal_rela_relocs = internal_relocs;
2627 if (esdo->rel.hdr)
2628 {
2629 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2630 external_relocs,
2631 internal_relocs))
2632 goto error_return;
2633 external_relocs = (((bfd_byte *) external_relocs)
2634 + esdo->rel.hdr->sh_size);
2635 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2636 * bed->s->int_rels_per_ext_rel);
2637 }
2638
2639 if (esdo->rela.hdr
2640 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2641 external_relocs,
2642 internal_rela_relocs)))
2643 goto error_return;
2644
2645 /* Cache the results for next time, if we can. */
2646 if (keep_memory)
2647 esdo->relocs = internal_relocs;
2648
2649 if (alloc1 != NULL)
2650 free (alloc1);
2651
2652 /* Don't free alloc2, since if it was allocated we are passing it
2653 back (under the name of internal_relocs). */
2654
2655 return internal_relocs;
2656
2657 error_return:
2658 if (alloc1 != NULL)
2659 free (alloc1);
2660 if (alloc2 != NULL)
2661 {
2662 if (keep_memory)
2663 bfd_release (abfd, alloc2);
2664 else
2665 free (alloc2);
2666 }
2667 return NULL;
2668 }
2669
2670 /* Compute the size of, and allocate space for, REL_HDR which is the
2671 section header for a section containing relocations for O. */
2672
2673 static bfd_boolean
2674 _bfd_elf_link_size_reloc_section (bfd *abfd,
2675 struct bfd_elf_section_reloc_data *reldata)
2676 {
2677 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2678
2679 /* That allows us to calculate the size of the section. */
2680 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2681
2682 /* The contents field must last into write_object_contents, so we
2683 allocate it with bfd_alloc rather than malloc. Also since we
2684 cannot be sure that the contents will actually be filled in,
2685 we zero the allocated space. */
2686 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2687 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2688 return FALSE;
2689
2690 if (reldata->hashes == NULL && reldata->count)
2691 {
2692 struct elf_link_hash_entry **p;
2693
2694 p = ((struct elf_link_hash_entry **)
2695 bfd_zmalloc (reldata->count * sizeof (*p)));
2696 if (p == NULL)
2697 return FALSE;
2698
2699 reldata->hashes = p;
2700 }
2701
2702 return TRUE;
2703 }
2704
2705 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2706 originated from the section given by INPUT_REL_HDR) to the
2707 OUTPUT_BFD. */
2708
2709 bfd_boolean
2710 _bfd_elf_link_output_relocs (bfd *output_bfd,
2711 asection *input_section,
2712 Elf_Internal_Shdr *input_rel_hdr,
2713 Elf_Internal_Rela *internal_relocs,
2714 struct elf_link_hash_entry **rel_hash
2715 ATTRIBUTE_UNUSED)
2716 {
2717 Elf_Internal_Rela *irela;
2718 Elf_Internal_Rela *irelaend;
2719 bfd_byte *erel;
2720 struct bfd_elf_section_reloc_data *output_reldata;
2721 asection *output_section;
2722 const struct elf_backend_data *bed;
2723 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2724 struct bfd_elf_section_data *esdo;
2725
2726 output_section = input_section->output_section;
2727
2728 bed = get_elf_backend_data (output_bfd);
2729 esdo = elf_section_data (output_section);
2730 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2731 {
2732 output_reldata = &esdo->rel;
2733 swap_out = bed->s->swap_reloc_out;
2734 }
2735 else if (esdo->rela.hdr
2736 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2737 {
2738 output_reldata = &esdo->rela;
2739 swap_out = bed->s->swap_reloca_out;
2740 }
2741 else
2742 {
2743 _bfd_error_handler
2744 /* xgettext:c-format */
2745 (_("%pB: relocation size mismatch in %pB section %pA"),
2746 output_bfd, input_section->owner, input_section);
2747 bfd_set_error (bfd_error_wrong_format);
2748 return FALSE;
2749 }
2750
2751 erel = output_reldata->hdr->contents;
2752 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2753 irela = internal_relocs;
2754 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2755 * bed->s->int_rels_per_ext_rel);
2756 while (irela < irelaend)
2757 {
2758 (*swap_out) (output_bfd, irela, erel);
2759 irela += bed->s->int_rels_per_ext_rel;
2760 erel += input_rel_hdr->sh_entsize;
2761 }
2762
2763 /* Bump the counter, so that we know where to add the next set of
2764 relocations. */
2765 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2766
2767 return TRUE;
2768 }
2769 \f
2770 /* Make weak undefined symbols in PIE dynamic. */
2771
2772 bfd_boolean
2773 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2774 struct elf_link_hash_entry *h)
2775 {
2776 if (bfd_link_pie (info)
2777 && h->dynindx == -1
2778 && h->root.type == bfd_link_hash_undefweak)
2779 return bfd_elf_link_record_dynamic_symbol (info, h);
2780
2781 return TRUE;
2782 }
2783
2784 /* Fix up the flags for a symbol. This handles various cases which
2785 can only be fixed after all the input files are seen. This is
2786 currently called by both adjust_dynamic_symbol and
2787 assign_sym_version, which is unnecessary but perhaps more robust in
2788 the face of future changes. */
2789
2790 static bfd_boolean
2791 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2792 struct elf_info_failed *eif)
2793 {
2794 const struct elf_backend_data *bed;
2795
2796 /* If this symbol was mentioned in a non-ELF file, try to set
2797 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2798 permit a non-ELF file to correctly refer to a symbol defined in
2799 an ELF dynamic object. */
2800 if (h->non_elf)
2801 {
2802 while (h->root.type == bfd_link_hash_indirect)
2803 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2804
2805 if (h->root.type != bfd_link_hash_defined
2806 && h->root.type != bfd_link_hash_defweak)
2807 {
2808 h->ref_regular = 1;
2809 h->ref_regular_nonweak = 1;
2810 }
2811 else
2812 {
2813 if (h->root.u.def.section->owner != NULL
2814 && (bfd_get_flavour (h->root.u.def.section->owner)
2815 == bfd_target_elf_flavour))
2816 {
2817 h->ref_regular = 1;
2818 h->ref_regular_nonweak = 1;
2819 }
2820 else
2821 h->def_regular = 1;
2822 }
2823
2824 if (h->dynindx == -1
2825 && (h->def_dynamic
2826 || h->ref_dynamic))
2827 {
2828 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2829 {
2830 eif->failed = TRUE;
2831 return FALSE;
2832 }
2833 }
2834 }
2835 else
2836 {
2837 /* Unfortunately, NON_ELF is only correct if the symbol
2838 was first seen in a non-ELF file. Fortunately, if the symbol
2839 was first seen in an ELF file, we're probably OK unless the
2840 symbol was defined in a non-ELF file. Catch that case here.
2841 FIXME: We're still in trouble if the symbol was first seen in
2842 a dynamic object, and then later in a non-ELF regular object. */
2843 if ((h->root.type == bfd_link_hash_defined
2844 || h->root.type == bfd_link_hash_defweak)
2845 && !h->def_regular
2846 && (h->root.u.def.section->owner != NULL
2847 ? (bfd_get_flavour (h->root.u.def.section->owner)
2848 != bfd_target_elf_flavour)
2849 : (bfd_is_abs_section (h->root.u.def.section)
2850 && !h->def_dynamic)))
2851 h->def_regular = 1;
2852 }
2853
2854 /* Backend specific symbol fixup. */
2855 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2856 if (bed->elf_backend_fixup_symbol
2857 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2858 return FALSE;
2859
2860 /* If this is a final link, and the symbol was defined as a common
2861 symbol in a regular object file, and there was no definition in
2862 any dynamic object, then the linker will have allocated space for
2863 the symbol in a common section but the DEF_REGULAR
2864 flag will not have been set. */
2865 if (h->root.type == bfd_link_hash_defined
2866 && !h->def_regular
2867 && h->ref_regular
2868 && !h->def_dynamic
2869 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2870 h->def_regular = 1;
2871
2872 /* Symbols defined in discarded sections shouldn't be dynamic. */
2873 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
2874 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2875
2876 /* If a weak undefined symbol has non-default visibility, we also
2877 hide it from the dynamic linker. */
2878 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2879 && h->root.type == bfd_link_hash_undefweak)
2880 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2881
2882 /* A hidden versioned symbol in executable should be forced local if
2883 it is is locally defined, not referenced by shared library and not
2884 exported. */
2885 else if (bfd_link_executable (eif->info)
2886 && h->versioned == versioned_hidden
2887 && !eif->info->export_dynamic
2888 && !h->dynamic
2889 && !h->ref_dynamic
2890 && h->def_regular)
2891 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2892
2893 /* If -Bsymbolic was used (which means to bind references to global
2894 symbols to the definition within the shared object), and this
2895 symbol was defined in a regular object, then it actually doesn't
2896 need a PLT entry. Likewise, if the symbol has non-default
2897 visibility. If the symbol has hidden or internal visibility, we
2898 will force it local. */
2899 else if (h->needs_plt
2900 && bfd_link_pic (eif->info)
2901 && is_elf_hash_table (eif->info->hash)
2902 && (SYMBOLIC_BIND (eif->info, h)
2903 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2904 && h->def_regular)
2905 {
2906 bfd_boolean force_local;
2907
2908 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2909 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2910 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2911 }
2912
2913 /* If this is a weak defined symbol in a dynamic object, and we know
2914 the real definition in the dynamic object, copy interesting flags
2915 over to the real definition. */
2916 if (h->is_weakalias)
2917 {
2918 struct elf_link_hash_entry *def = weakdef (h);
2919
2920 /* If the real definition is defined by a regular object file,
2921 don't do anything special. See the longer description in
2922 _bfd_elf_adjust_dynamic_symbol, below. */
2923 if (def->def_regular)
2924 {
2925 h = def;
2926 while ((h = h->u.alias) != def)
2927 h->is_weakalias = 0;
2928 }
2929 else
2930 {
2931 while (h->root.type == bfd_link_hash_indirect)
2932 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2933 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2934 || h->root.type == bfd_link_hash_defweak);
2935 BFD_ASSERT (def->def_dynamic);
2936 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
2937 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
2938 }
2939 }
2940
2941 return TRUE;
2942 }
2943
2944 /* Make the backend pick a good value for a dynamic symbol. This is
2945 called via elf_link_hash_traverse, and also calls itself
2946 recursively. */
2947
2948 static bfd_boolean
2949 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2950 {
2951 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2952 struct elf_link_hash_table *htab;
2953 const struct elf_backend_data *bed;
2954
2955 if (! is_elf_hash_table (eif->info->hash))
2956 return FALSE;
2957
2958 /* Ignore indirect symbols. These are added by the versioning code. */
2959 if (h->root.type == bfd_link_hash_indirect)
2960 return TRUE;
2961
2962 /* Fix the symbol flags. */
2963 if (! _bfd_elf_fix_symbol_flags (h, eif))
2964 return FALSE;
2965
2966 htab = elf_hash_table (eif->info);
2967 bed = get_elf_backend_data (htab->dynobj);
2968
2969 if (h->root.type == bfd_link_hash_undefweak)
2970 {
2971 if (eif->info->dynamic_undefined_weak == 0)
2972 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2973 else if (eif->info->dynamic_undefined_weak > 0
2974 && h->ref_regular
2975 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2976 && !bfd_hide_sym_by_version (eif->info->version_info,
2977 h->root.root.string))
2978 {
2979 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2980 {
2981 eif->failed = TRUE;
2982 return FALSE;
2983 }
2984 }
2985 }
2986
2987 /* If this symbol does not require a PLT entry, and it is not
2988 defined by a dynamic object, or is not referenced by a regular
2989 object, ignore it. We do have to handle a weak defined symbol,
2990 even if no regular object refers to it, if we decided to add it
2991 to the dynamic symbol table. FIXME: Do we normally need to worry
2992 about symbols which are defined by one dynamic object and
2993 referenced by another one? */
2994 if (!h->needs_plt
2995 && h->type != STT_GNU_IFUNC
2996 && (h->def_regular
2997 || !h->def_dynamic
2998 || (!h->ref_regular
2999 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3000 {
3001 h->plt = elf_hash_table (eif->info)->init_plt_offset;
3002 return TRUE;
3003 }
3004
3005 /* If we've already adjusted this symbol, don't do it again. This
3006 can happen via a recursive call. */
3007 if (h->dynamic_adjusted)
3008 return TRUE;
3009
3010 /* Don't look at this symbol again. Note that we must set this
3011 after checking the above conditions, because we may look at a
3012 symbol once, decide not to do anything, and then get called
3013 recursively later after REF_REGULAR is set below. */
3014 h->dynamic_adjusted = 1;
3015
3016 /* If this is a weak definition, and we know a real definition, and
3017 the real symbol is not itself defined by a regular object file,
3018 then get a good value for the real definition. We handle the
3019 real symbol first, for the convenience of the backend routine.
3020
3021 Note that there is a confusing case here. If the real definition
3022 is defined by a regular object file, we don't get the real symbol
3023 from the dynamic object, but we do get the weak symbol. If the
3024 processor backend uses a COPY reloc, then if some routine in the
3025 dynamic object changes the real symbol, we will not see that
3026 change in the corresponding weak symbol. This is the way other
3027 ELF linkers work as well, and seems to be a result of the shared
3028 library model.
3029
3030 I will clarify this issue. Most SVR4 shared libraries define the
3031 variable _timezone and define timezone as a weak synonym. The
3032 tzset call changes _timezone. If you write
3033 extern int timezone;
3034 int _timezone = 5;
3035 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3036 you might expect that, since timezone is a synonym for _timezone,
3037 the same number will print both times. However, if the processor
3038 backend uses a COPY reloc, then actually timezone will be copied
3039 into your process image, and, since you define _timezone
3040 yourself, _timezone will not. Thus timezone and _timezone will
3041 wind up at different memory locations. The tzset call will set
3042 _timezone, leaving timezone unchanged. */
3043
3044 if (h->is_weakalias)
3045 {
3046 struct elf_link_hash_entry *def = weakdef (h);
3047
3048 /* If we get to this point, there is an implicit reference to
3049 the alias by a regular object file via the weak symbol H. */
3050 def->ref_regular = 1;
3051
3052 /* Ensure that the backend adjust_dynamic_symbol function sees
3053 the strong alias before H by recursively calling ourselves. */
3054 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3055 return FALSE;
3056 }
3057
3058 /* If a symbol has no type and no size and does not require a PLT
3059 entry, then we are probably about to do the wrong thing here: we
3060 are probably going to create a COPY reloc for an empty object.
3061 This case can arise when a shared object is built with assembly
3062 code, and the assembly code fails to set the symbol type. */
3063 if (h->size == 0
3064 && h->type == STT_NOTYPE
3065 && !h->needs_plt)
3066 _bfd_error_handler
3067 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3068 h->root.root.string);
3069
3070 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3071 {
3072 eif->failed = TRUE;
3073 return FALSE;
3074 }
3075
3076 return TRUE;
3077 }
3078
3079 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3080 DYNBSS. */
3081
3082 bfd_boolean
3083 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3084 struct elf_link_hash_entry *h,
3085 asection *dynbss)
3086 {
3087 unsigned int power_of_two;
3088 bfd_vma mask;
3089 asection *sec = h->root.u.def.section;
3090
3091 /* The section alignment of the definition is the maximum alignment
3092 requirement of symbols defined in the section. Since we don't
3093 know the symbol alignment requirement, we start with the
3094 maximum alignment and check low bits of the symbol address
3095 for the minimum alignment. */
3096 power_of_two = bfd_get_section_alignment (sec->owner, sec);
3097 mask = ((bfd_vma) 1 << power_of_two) - 1;
3098 while ((h->root.u.def.value & mask) != 0)
3099 {
3100 mask >>= 1;
3101 --power_of_two;
3102 }
3103
3104 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
3105 dynbss))
3106 {
3107 /* Adjust the section alignment if needed. */
3108 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
3109 power_of_two))
3110 return FALSE;
3111 }
3112
3113 /* We make sure that the symbol will be aligned properly. */
3114 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3115
3116 /* Define the symbol as being at this point in DYNBSS. */
3117 h->root.u.def.section = dynbss;
3118 h->root.u.def.value = dynbss->size;
3119
3120 /* Increment the size of DYNBSS to make room for the symbol. */
3121 dynbss->size += h->size;
3122
3123 /* No error if extern_protected_data is true. */
3124 if (h->protected_def
3125 && (!info->extern_protected_data
3126 || (info->extern_protected_data < 0
3127 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3128 info->callbacks->einfo
3129 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3130 h->root.root.string);
3131
3132 return TRUE;
3133 }
3134
3135 /* Adjust all external symbols pointing into SEC_MERGE sections
3136 to reflect the object merging within the sections. */
3137
3138 static bfd_boolean
3139 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3140 {
3141 asection *sec;
3142
3143 if ((h->root.type == bfd_link_hash_defined
3144 || h->root.type == bfd_link_hash_defweak)
3145 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3146 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3147 {
3148 bfd *output_bfd = (bfd *) data;
3149
3150 h->root.u.def.value =
3151 _bfd_merged_section_offset (output_bfd,
3152 &h->root.u.def.section,
3153 elf_section_data (sec)->sec_info,
3154 h->root.u.def.value);
3155 }
3156
3157 return TRUE;
3158 }
3159
3160 /* Returns false if the symbol referred to by H should be considered
3161 to resolve local to the current module, and true if it should be
3162 considered to bind dynamically. */
3163
3164 bfd_boolean
3165 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3166 struct bfd_link_info *info,
3167 bfd_boolean not_local_protected)
3168 {
3169 bfd_boolean binding_stays_local_p;
3170 const struct elf_backend_data *bed;
3171 struct elf_link_hash_table *hash_table;
3172
3173 if (h == NULL)
3174 return FALSE;
3175
3176 while (h->root.type == bfd_link_hash_indirect
3177 || h->root.type == bfd_link_hash_warning)
3178 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3179
3180 /* If it was forced local, then clearly it's not dynamic. */
3181 if (h->dynindx == -1)
3182 return FALSE;
3183 if (h->forced_local)
3184 return FALSE;
3185
3186 /* Identify the cases where name binding rules say that a
3187 visible symbol resolves locally. */
3188 binding_stays_local_p = (bfd_link_executable (info)
3189 || SYMBOLIC_BIND (info, h));
3190
3191 switch (ELF_ST_VISIBILITY (h->other))
3192 {
3193 case STV_INTERNAL:
3194 case STV_HIDDEN:
3195 return FALSE;
3196
3197 case STV_PROTECTED:
3198 hash_table = elf_hash_table (info);
3199 if (!is_elf_hash_table (hash_table))
3200 return FALSE;
3201
3202 bed = get_elf_backend_data (hash_table->dynobj);
3203
3204 /* Proper resolution for function pointer equality may require
3205 that these symbols perhaps be resolved dynamically, even though
3206 we should be resolving them to the current module. */
3207 if (!not_local_protected || !bed->is_function_type (h->type))
3208 binding_stays_local_p = TRUE;
3209 break;
3210
3211 default:
3212 break;
3213 }
3214
3215 /* If it isn't defined locally, then clearly it's dynamic. */
3216 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3217 return TRUE;
3218
3219 /* Otherwise, the symbol is dynamic if binding rules don't tell
3220 us that it remains local. */
3221 return !binding_stays_local_p;
3222 }
3223
3224 /* Return true if the symbol referred to by H should be considered
3225 to resolve local to the current module, and false otherwise. Differs
3226 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3227 undefined symbols. The two functions are virtually identical except
3228 for the place where dynindx == -1 is tested. If that test is true,
3229 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3230 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3231 defined symbols.
3232 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3233 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3234 treatment of undefined weak symbols. For those that do not make
3235 undefined weak symbols dynamic, both functions may return false. */
3236
3237 bfd_boolean
3238 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3239 struct bfd_link_info *info,
3240 bfd_boolean local_protected)
3241 {
3242 const struct elf_backend_data *bed;
3243 struct elf_link_hash_table *hash_table;
3244
3245 /* If it's a local sym, of course we resolve locally. */
3246 if (h == NULL)
3247 return TRUE;
3248
3249 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3250 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3251 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3252 return TRUE;
3253
3254 /* Forced local symbols resolve locally. */
3255 if (h->forced_local)
3256 return TRUE;
3257
3258 /* Common symbols that become definitions don't get the DEF_REGULAR
3259 flag set, so test it first, and don't bail out. */
3260 if (ELF_COMMON_DEF_P (h))
3261 /* Do nothing. */;
3262 /* If we don't have a definition in a regular file, then we can't
3263 resolve locally. The sym is either undefined or dynamic. */
3264 else if (!h->def_regular)
3265 return FALSE;
3266
3267 /* Non-dynamic symbols resolve locally. */
3268 if (h->dynindx == -1)
3269 return TRUE;
3270
3271 /* At this point, we know the symbol is defined and dynamic. In an
3272 executable it must resolve locally, likewise when building symbolic
3273 shared libraries. */
3274 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3275 return TRUE;
3276
3277 /* Now deal with defined dynamic symbols in shared libraries. Ones
3278 with default visibility might not resolve locally. */
3279 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3280 return FALSE;
3281
3282 hash_table = elf_hash_table (info);
3283 if (!is_elf_hash_table (hash_table))
3284 return TRUE;
3285
3286 bed = get_elf_backend_data (hash_table->dynobj);
3287
3288 /* If extern_protected_data is false, STV_PROTECTED non-function
3289 symbols are local. */
3290 if ((!info->extern_protected_data
3291 || (info->extern_protected_data < 0
3292 && !bed->extern_protected_data))
3293 && !bed->is_function_type (h->type))
3294 return TRUE;
3295
3296 /* Function pointer equality tests may require that STV_PROTECTED
3297 symbols be treated as dynamic symbols. If the address of a
3298 function not defined in an executable is set to that function's
3299 plt entry in the executable, then the address of the function in
3300 a shared library must also be the plt entry in the executable. */
3301 return local_protected;
3302 }
3303
3304 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3305 aligned. Returns the first TLS output section. */
3306
3307 struct bfd_section *
3308 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3309 {
3310 struct bfd_section *sec, *tls;
3311 unsigned int align = 0;
3312
3313 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3314 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3315 break;
3316 tls = sec;
3317
3318 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3319 if (sec->alignment_power > align)
3320 align = sec->alignment_power;
3321
3322 elf_hash_table (info)->tls_sec = tls;
3323
3324 /* Ensure the alignment of the first section is the largest alignment,
3325 so that the tls segment starts aligned. */
3326 if (tls != NULL)
3327 tls->alignment_power = align;
3328
3329 return tls;
3330 }
3331
3332 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3333 static bfd_boolean
3334 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3335 Elf_Internal_Sym *sym)
3336 {
3337 const struct elf_backend_data *bed;
3338
3339 /* Local symbols do not count, but target specific ones might. */
3340 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3341 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3342 return FALSE;
3343
3344 bed = get_elf_backend_data (abfd);
3345 /* Function symbols do not count. */
3346 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3347 return FALSE;
3348
3349 /* If the section is undefined, then so is the symbol. */
3350 if (sym->st_shndx == SHN_UNDEF)
3351 return FALSE;
3352
3353 /* If the symbol is defined in the common section, then
3354 it is a common definition and so does not count. */
3355 if (bed->common_definition (sym))
3356 return FALSE;
3357
3358 /* If the symbol is in a target specific section then we
3359 must rely upon the backend to tell us what it is. */
3360 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3361 /* FIXME - this function is not coded yet:
3362
3363 return _bfd_is_global_symbol_definition (abfd, sym);
3364
3365 Instead for now assume that the definition is not global,
3366 Even if this is wrong, at least the linker will behave
3367 in the same way that it used to do. */
3368 return FALSE;
3369
3370 return TRUE;
3371 }
3372
3373 /* Search the symbol table of the archive element of the archive ABFD
3374 whose archive map contains a mention of SYMDEF, and determine if
3375 the symbol is defined in this element. */
3376 static bfd_boolean
3377 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3378 {
3379 Elf_Internal_Shdr * hdr;
3380 size_t symcount;
3381 size_t extsymcount;
3382 size_t extsymoff;
3383 Elf_Internal_Sym *isymbuf;
3384 Elf_Internal_Sym *isym;
3385 Elf_Internal_Sym *isymend;
3386 bfd_boolean result;
3387
3388 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3389 if (abfd == NULL)
3390 return FALSE;
3391
3392 if (! bfd_check_format (abfd, bfd_object))
3393 return FALSE;
3394
3395 /* Select the appropriate symbol table. If we don't know if the
3396 object file is an IR object, give linker LTO plugin a chance to
3397 get the correct symbol table. */
3398 if (abfd->plugin_format == bfd_plugin_yes
3399 #if BFD_SUPPORTS_PLUGINS
3400 || (abfd->plugin_format == bfd_plugin_unknown
3401 && bfd_link_plugin_object_p (abfd))
3402 #endif
3403 )
3404 {
3405 /* Use the IR symbol table if the object has been claimed by
3406 plugin. */
3407 abfd = abfd->plugin_dummy_bfd;
3408 hdr = &elf_tdata (abfd)->symtab_hdr;
3409 }
3410 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3411 hdr = &elf_tdata (abfd)->symtab_hdr;
3412 else
3413 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3414
3415 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3416
3417 /* The sh_info field of the symtab header tells us where the
3418 external symbols start. We don't care about the local symbols. */
3419 if (elf_bad_symtab (abfd))
3420 {
3421 extsymcount = symcount;
3422 extsymoff = 0;
3423 }
3424 else
3425 {
3426 extsymcount = symcount - hdr->sh_info;
3427 extsymoff = hdr->sh_info;
3428 }
3429
3430 if (extsymcount == 0)
3431 return FALSE;
3432
3433 /* Read in the symbol table. */
3434 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3435 NULL, NULL, NULL);
3436 if (isymbuf == NULL)
3437 return FALSE;
3438
3439 /* Scan the symbol table looking for SYMDEF. */
3440 result = FALSE;
3441 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3442 {
3443 const char *name;
3444
3445 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3446 isym->st_name);
3447 if (name == NULL)
3448 break;
3449
3450 if (strcmp (name, symdef->name) == 0)
3451 {
3452 result = is_global_data_symbol_definition (abfd, isym);
3453 break;
3454 }
3455 }
3456
3457 free (isymbuf);
3458
3459 return result;
3460 }
3461 \f
3462 /* Add an entry to the .dynamic table. */
3463
3464 bfd_boolean
3465 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3466 bfd_vma tag,
3467 bfd_vma val)
3468 {
3469 struct elf_link_hash_table *hash_table;
3470 const struct elf_backend_data *bed;
3471 asection *s;
3472 bfd_size_type newsize;
3473 bfd_byte *newcontents;
3474 Elf_Internal_Dyn dyn;
3475
3476 hash_table = elf_hash_table (info);
3477 if (! is_elf_hash_table (hash_table))
3478 return FALSE;
3479
3480 if (tag == DT_RELA || tag == DT_REL)
3481 hash_table->dynamic_relocs = TRUE;
3482
3483 bed = get_elf_backend_data (hash_table->dynobj);
3484 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3485 BFD_ASSERT (s != NULL);
3486
3487 newsize = s->size + bed->s->sizeof_dyn;
3488 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3489 if (newcontents == NULL)
3490 return FALSE;
3491
3492 dyn.d_tag = tag;
3493 dyn.d_un.d_val = val;
3494 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3495
3496 s->size = newsize;
3497 s->contents = newcontents;
3498
3499 return TRUE;
3500 }
3501
3502 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3503 otherwise just check whether one already exists. Returns -1 on error,
3504 1 if a DT_NEEDED tag already exists, and 0 on success. */
3505
3506 static int
3507 elf_add_dt_needed_tag (bfd *abfd,
3508 struct bfd_link_info *info,
3509 const char *soname,
3510 bfd_boolean do_it)
3511 {
3512 struct elf_link_hash_table *hash_table;
3513 size_t strindex;
3514
3515 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3516 return -1;
3517
3518 hash_table = elf_hash_table (info);
3519 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3520 if (strindex == (size_t) -1)
3521 return -1;
3522
3523 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3524 {
3525 asection *sdyn;
3526 const struct elf_backend_data *bed;
3527 bfd_byte *extdyn;
3528
3529 bed = get_elf_backend_data (hash_table->dynobj);
3530 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3531 if (sdyn != NULL)
3532 for (extdyn = sdyn->contents;
3533 extdyn < sdyn->contents + sdyn->size;
3534 extdyn += bed->s->sizeof_dyn)
3535 {
3536 Elf_Internal_Dyn dyn;
3537
3538 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3539 if (dyn.d_tag == DT_NEEDED
3540 && dyn.d_un.d_val == strindex)
3541 {
3542 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3543 return 1;
3544 }
3545 }
3546 }
3547
3548 if (do_it)
3549 {
3550 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3551 return -1;
3552
3553 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3554 return -1;
3555 }
3556 else
3557 /* We were just checking for existence of the tag. */
3558 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3559
3560 return 0;
3561 }
3562
3563 /* Return true if SONAME is on the needed list between NEEDED and STOP
3564 (or the end of list if STOP is NULL), and needed by a library that
3565 will be loaded. */
3566
3567 static bfd_boolean
3568 on_needed_list (const char *soname,
3569 struct bfd_link_needed_list *needed,
3570 struct bfd_link_needed_list *stop)
3571 {
3572 struct bfd_link_needed_list *look;
3573 for (look = needed; look != stop; look = look->next)
3574 if (strcmp (soname, look->name) == 0
3575 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3576 /* If needed by a library that itself is not directly
3577 needed, recursively check whether that library is
3578 indirectly needed. Since we add DT_NEEDED entries to
3579 the end of the list, library dependencies appear after
3580 the library. Therefore search prior to the current
3581 LOOK, preventing possible infinite recursion. */
3582 || on_needed_list (elf_dt_name (look->by), needed, look)))
3583 return TRUE;
3584
3585 return FALSE;
3586 }
3587
3588 /* Sort symbol by value, section, and size. */
3589 static int
3590 elf_sort_symbol (const void *arg1, const void *arg2)
3591 {
3592 const struct elf_link_hash_entry *h1;
3593 const struct elf_link_hash_entry *h2;
3594 bfd_signed_vma vdiff;
3595
3596 h1 = *(const struct elf_link_hash_entry **) arg1;
3597 h2 = *(const struct elf_link_hash_entry **) arg2;
3598 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3599 if (vdiff != 0)
3600 return vdiff > 0 ? 1 : -1;
3601 else
3602 {
3603 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3604 if (sdiff != 0)
3605 return sdiff > 0 ? 1 : -1;
3606 }
3607 vdiff = h1->size - h2->size;
3608 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3609 }
3610
3611 /* This function is used to adjust offsets into .dynstr for
3612 dynamic symbols. This is called via elf_link_hash_traverse. */
3613
3614 static bfd_boolean
3615 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3616 {
3617 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3618
3619 if (h->dynindx != -1)
3620 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3621 return TRUE;
3622 }
3623
3624 /* Assign string offsets in .dynstr, update all structures referencing
3625 them. */
3626
3627 static bfd_boolean
3628 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3629 {
3630 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3631 struct elf_link_local_dynamic_entry *entry;
3632 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3633 bfd *dynobj = hash_table->dynobj;
3634 asection *sdyn;
3635 bfd_size_type size;
3636 const struct elf_backend_data *bed;
3637 bfd_byte *extdyn;
3638
3639 _bfd_elf_strtab_finalize (dynstr);
3640 size = _bfd_elf_strtab_size (dynstr);
3641
3642 bed = get_elf_backend_data (dynobj);
3643 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3644 BFD_ASSERT (sdyn != NULL);
3645
3646 /* Update all .dynamic entries referencing .dynstr strings. */
3647 for (extdyn = sdyn->contents;
3648 extdyn < sdyn->contents + sdyn->size;
3649 extdyn += bed->s->sizeof_dyn)
3650 {
3651 Elf_Internal_Dyn dyn;
3652
3653 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3654 switch (dyn.d_tag)
3655 {
3656 case DT_STRSZ:
3657 dyn.d_un.d_val = size;
3658 break;
3659 case DT_NEEDED:
3660 case DT_SONAME:
3661 case DT_RPATH:
3662 case DT_RUNPATH:
3663 case DT_FILTER:
3664 case DT_AUXILIARY:
3665 case DT_AUDIT:
3666 case DT_DEPAUDIT:
3667 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3668 break;
3669 default:
3670 continue;
3671 }
3672 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3673 }
3674
3675 /* Now update local dynamic symbols. */
3676 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3677 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3678 entry->isym.st_name);
3679
3680 /* And the rest of dynamic symbols. */
3681 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3682
3683 /* Adjust version definitions. */
3684 if (elf_tdata (output_bfd)->cverdefs)
3685 {
3686 asection *s;
3687 bfd_byte *p;
3688 size_t i;
3689 Elf_Internal_Verdef def;
3690 Elf_Internal_Verdaux defaux;
3691
3692 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3693 p = s->contents;
3694 do
3695 {
3696 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3697 &def);
3698 p += sizeof (Elf_External_Verdef);
3699 if (def.vd_aux != sizeof (Elf_External_Verdef))
3700 continue;
3701 for (i = 0; i < def.vd_cnt; ++i)
3702 {
3703 _bfd_elf_swap_verdaux_in (output_bfd,
3704 (Elf_External_Verdaux *) p, &defaux);
3705 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3706 defaux.vda_name);
3707 _bfd_elf_swap_verdaux_out (output_bfd,
3708 &defaux, (Elf_External_Verdaux *) p);
3709 p += sizeof (Elf_External_Verdaux);
3710 }
3711 }
3712 while (def.vd_next);
3713 }
3714
3715 /* Adjust version references. */
3716 if (elf_tdata (output_bfd)->verref)
3717 {
3718 asection *s;
3719 bfd_byte *p;
3720 size_t i;
3721 Elf_Internal_Verneed need;
3722 Elf_Internal_Vernaux needaux;
3723
3724 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3725 p = s->contents;
3726 do
3727 {
3728 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3729 &need);
3730 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3731 _bfd_elf_swap_verneed_out (output_bfd, &need,
3732 (Elf_External_Verneed *) p);
3733 p += sizeof (Elf_External_Verneed);
3734 for (i = 0; i < need.vn_cnt; ++i)
3735 {
3736 _bfd_elf_swap_vernaux_in (output_bfd,
3737 (Elf_External_Vernaux *) p, &needaux);
3738 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3739 needaux.vna_name);
3740 _bfd_elf_swap_vernaux_out (output_bfd,
3741 &needaux,
3742 (Elf_External_Vernaux *) p);
3743 p += sizeof (Elf_External_Vernaux);
3744 }
3745 }
3746 while (need.vn_next);
3747 }
3748
3749 return TRUE;
3750 }
3751 \f
3752 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3753 The default is to only match when the INPUT and OUTPUT are exactly
3754 the same target. */
3755
3756 bfd_boolean
3757 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3758 const bfd_target *output)
3759 {
3760 return input == output;
3761 }
3762
3763 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3764 This version is used when different targets for the same architecture
3765 are virtually identical. */
3766
3767 bfd_boolean
3768 _bfd_elf_relocs_compatible (const bfd_target *input,
3769 const bfd_target *output)
3770 {
3771 const struct elf_backend_data *obed, *ibed;
3772
3773 if (input == output)
3774 return TRUE;
3775
3776 ibed = xvec_get_elf_backend_data (input);
3777 obed = xvec_get_elf_backend_data (output);
3778
3779 if (ibed->arch != obed->arch)
3780 return FALSE;
3781
3782 /* If both backends are using this function, deem them compatible. */
3783 return ibed->relocs_compatible == obed->relocs_compatible;
3784 }
3785
3786 /* Make a special call to the linker "notice" function to tell it that
3787 we are about to handle an as-needed lib, or have finished
3788 processing the lib. */
3789
3790 bfd_boolean
3791 _bfd_elf_notice_as_needed (bfd *ibfd,
3792 struct bfd_link_info *info,
3793 enum notice_asneeded_action act)
3794 {
3795 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3796 }
3797
3798 /* Check relocations an ELF object file. */
3799
3800 bfd_boolean
3801 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3802 {
3803 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3804 struct elf_link_hash_table *htab = elf_hash_table (info);
3805
3806 /* If this object is the same format as the output object, and it is
3807 not a shared library, then let the backend look through the
3808 relocs.
3809
3810 This is required to build global offset table entries and to
3811 arrange for dynamic relocs. It is not required for the
3812 particular common case of linking non PIC code, even when linking
3813 against shared libraries, but unfortunately there is no way of
3814 knowing whether an object file has been compiled PIC or not.
3815 Looking through the relocs is not particularly time consuming.
3816 The problem is that we must either (1) keep the relocs in memory,
3817 which causes the linker to require additional runtime memory or
3818 (2) read the relocs twice from the input file, which wastes time.
3819 This would be a good case for using mmap.
3820
3821 I have no idea how to handle linking PIC code into a file of a
3822 different format. It probably can't be done. */
3823 if ((abfd->flags & DYNAMIC) == 0
3824 && is_elf_hash_table (htab)
3825 && bed->check_relocs != NULL
3826 && elf_object_id (abfd) == elf_hash_table_id (htab)
3827 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3828 {
3829 asection *o;
3830
3831 for (o = abfd->sections; o != NULL; o = o->next)
3832 {
3833 Elf_Internal_Rela *internal_relocs;
3834 bfd_boolean ok;
3835
3836 /* Don't check relocations in excluded sections. */
3837 if ((o->flags & SEC_RELOC) == 0
3838 || (o->flags & SEC_EXCLUDE) != 0
3839 || o->reloc_count == 0
3840 || ((info->strip == strip_all || info->strip == strip_debugger)
3841 && (o->flags & SEC_DEBUGGING) != 0)
3842 || bfd_is_abs_section (o->output_section))
3843 continue;
3844
3845 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3846 info->keep_memory);
3847 if (internal_relocs == NULL)
3848 return FALSE;
3849
3850 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3851
3852 if (elf_section_data (o)->relocs != internal_relocs)
3853 free (internal_relocs);
3854
3855 if (! ok)
3856 return FALSE;
3857 }
3858 }
3859
3860 return TRUE;
3861 }
3862
3863 /* Add symbols from an ELF object file to the linker hash table. */
3864
3865 static bfd_boolean
3866 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3867 {
3868 Elf_Internal_Ehdr *ehdr;
3869 Elf_Internal_Shdr *hdr;
3870 size_t symcount;
3871 size_t extsymcount;
3872 size_t extsymoff;
3873 struct elf_link_hash_entry **sym_hash;
3874 bfd_boolean dynamic;
3875 Elf_External_Versym *extversym = NULL;
3876 Elf_External_Versym *extversym_end = NULL;
3877 Elf_External_Versym *ever;
3878 struct elf_link_hash_entry *weaks;
3879 struct elf_link_hash_entry **nondeflt_vers = NULL;
3880 size_t nondeflt_vers_cnt = 0;
3881 Elf_Internal_Sym *isymbuf = NULL;
3882 Elf_Internal_Sym *isym;
3883 Elf_Internal_Sym *isymend;
3884 const struct elf_backend_data *bed;
3885 bfd_boolean add_needed;
3886 struct elf_link_hash_table *htab;
3887 bfd_size_type amt;
3888 void *alloc_mark = NULL;
3889 struct bfd_hash_entry **old_table = NULL;
3890 unsigned int old_size = 0;
3891 unsigned int old_count = 0;
3892 void *old_tab = NULL;
3893 void *old_ent;
3894 struct bfd_link_hash_entry *old_undefs = NULL;
3895 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3896 void *old_strtab = NULL;
3897 size_t tabsize = 0;
3898 asection *s;
3899 bfd_boolean just_syms;
3900
3901 htab = elf_hash_table (info);
3902 bed = get_elf_backend_data (abfd);
3903
3904 if ((abfd->flags & DYNAMIC) == 0)
3905 dynamic = FALSE;
3906 else
3907 {
3908 dynamic = TRUE;
3909
3910 /* You can't use -r against a dynamic object. Also, there's no
3911 hope of using a dynamic object which does not exactly match
3912 the format of the output file. */
3913 if (bfd_link_relocatable (info)
3914 || !is_elf_hash_table (htab)
3915 || info->output_bfd->xvec != abfd->xvec)
3916 {
3917 if (bfd_link_relocatable (info))
3918 bfd_set_error (bfd_error_invalid_operation);
3919 else
3920 bfd_set_error (bfd_error_wrong_format);
3921 goto error_return;
3922 }
3923 }
3924
3925 ehdr = elf_elfheader (abfd);
3926 if (info->warn_alternate_em
3927 && bed->elf_machine_code != ehdr->e_machine
3928 && ((bed->elf_machine_alt1 != 0
3929 && ehdr->e_machine == bed->elf_machine_alt1)
3930 || (bed->elf_machine_alt2 != 0
3931 && ehdr->e_machine == bed->elf_machine_alt2)))
3932 _bfd_error_handler
3933 /* xgettext:c-format */
3934 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
3935 ehdr->e_machine, abfd, bed->elf_machine_code);
3936
3937 /* As a GNU extension, any input sections which are named
3938 .gnu.warning.SYMBOL are treated as warning symbols for the given
3939 symbol. This differs from .gnu.warning sections, which generate
3940 warnings when they are included in an output file. */
3941 /* PR 12761: Also generate this warning when building shared libraries. */
3942 for (s = abfd->sections; s != NULL; s = s->next)
3943 {
3944 const char *name;
3945
3946 name = bfd_get_section_name (abfd, s);
3947 if (CONST_STRNEQ (name, ".gnu.warning."))
3948 {
3949 char *msg;
3950 bfd_size_type sz;
3951
3952 name += sizeof ".gnu.warning." - 1;
3953
3954 /* If this is a shared object, then look up the symbol
3955 in the hash table. If it is there, and it is already
3956 been defined, then we will not be using the entry
3957 from this shared object, so we don't need to warn.
3958 FIXME: If we see the definition in a regular object
3959 later on, we will warn, but we shouldn't. The only
3960 fix is to keep track of what warnings we are supposed
3961 to emit, and then handle them all at the end of the
3962 link. */
3963 if (dynamic)
3964 {
3965 struct elf_link_hash_entry *h;
3966
3967 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3968
3969 /* FIXME: What about bfd_link_hash_common? */
3970 if (h != NULL
3971 && (h->root.type == bfd_link_hash_defined
3972 || h->root.type == bfd_link_hash_defweak))
3973 continue;
3974 }
3975
3976 sz = s->size;
3977 msg = (char *) bfd_alloc (abfd, sz + 1);
3978 if (msg == NULL)
3979 goto error_return;
3980
3981 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3982 goto error_return;
3983
3984 msg[sz] = '\0';
3985
3986 if (! (_bfd_generic_link_add_one_symbol
3987 (info, abfd, name, BSF_WARNING, s, 0, msg,
3988 FALSE, bed->collect, NULL)))
3989 goto error_return;
3990
3991 if (bfd_link_executable (info))
3992 {
3993 /* Clobber the section size so that the warning does
3994 not get copied into the output file. */
3995 s->size = 0;
3996
3997 /* Also set SEC_EXCLUDE, so that symbols defined in
3998 the warning section don't get copied to the output. */
3999 s->flags |= SEC_EXCLUDE;
4000 }
4001 }
4002 }
4003
4004 just_syms = ((s = abfd->sections) != NULL
4005 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4006
4007 add_needed = TRUE;
4008 if (! dynamic)
4009 {
4010 /* If we are creating a shared library, create all the dynamic
4011 sections immediately. We need to attach them to something,
4012 so we attach them to this BFD, provided it is the right
4013 format and is not from ld --just-symbols. Always create the
4014 dynamic sections for -E/--dynamic-list. FIXME: If there
4015 are no input BFD's of the same format as the output, we can't
4016 make a shared library. */
4017 if (!just_syms
4018 && (bfd_link_pic (info)
4019 || (!bfd_link_relocatable (info)
4020 && info->nointerp
4021 && (info->export_dynamic || info->dynamic)))
4022 && is_elf_hash_table (htab)
4023 && info->output_bfd->xvec == abfd->xvec
4024 && !htab->dynamic_sections_created)
4025 {
4026 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4027 goto error_return;
4028 }
4029 }
4030 else if (!is_elf_hash_table (htab))
4031 goto error_return;
4032 else
4033 {
4034 const char *soname = NULL;
4035 char *audit = NULL;
4036 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4037 const Elf_Internal_Phdr *phdr;
4038 int ret;
4039
4040 /* ld --just-symbols and dynamic objects don't mix very well.
4041 ld shouldn't allow it. */
4042 if (just_syms)
4043 abort ();
4044
4045 /* If this dynamic lib was specified on the command line with
4046 --as-needed in effect, then we don't want to add a DT_NEEDED
4047 tag unless the lib is actually used. Similary for libs brought
4048 in by another lib's DT_NEEDED. When --no-add-needed is used
4049 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4050 any dynamic library in DT_NEEDED tags in the dynamic lib at
4051 all. */
4052 add_needed = (elf_dyn_lib_class (abfd)
4053 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4054 | DYN_NO_NEEDED)) == 0;
4055
4056 s = bfd_get_section_by_name (abfd, ".dynamic");
4057 if (s != NULL)
4058 {
4059 bfd_byte *dynbuf;
4060 bfd_byte *extdyn;
4061 unsigned int elfsec;
4062 unsigned long shlink;
4063
4064 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4065 {
4066 error_free_dyn:
4067 free (dynbuf);
4068 goto error_return;
4069 }
4070
4071 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4072 if (elfsec == SHN_BAD)
4073 goto error_free_dyn;
4074 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4075
4076 for (extdyn = dynbuf;
4077 extdyn < dynbuf + s->size;
4078 extdyn += bed->s->sizeof_dyn)
4079 {
4080 Elf_Internal_Dyn dyn;
4081
4082 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4083 if (dyn.d_tag == DT_SONAME)
4084 {
4085 unsigned int tagv = dyn.d_un.d_val;
4086 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4087 if (soname == NULL)
4088 goto error_free_dyn;
4089 }
4090 if (dyn.d_tag == DT_NEEDED)
4091 {
4092 struct bfd_link_needed_list *n, **pn;
4093 char *fnm, *anm;
4094 unsigned int tagv = dyn.d_un.d_val;
4095
4096 amt = sizeof (struct bfd_link_needed_list);
4097 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4098 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4099 if (n == NULL || fnm == NULL)
4100 goto error_free_dyn;
4101 amt = strlen (fnm) + 1;
4102 anm = (char *) bfd_alloc (abfd, amt);
4103 if (anm == NULL)
4104 goto error_free_dyn;
4105 memcpy (anm, fnm, amt);
4106 n->name = anm;
4107 n->by = abfd;
4108 n->next = NULL;
4109 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4110 ;
4111 *pn = n;
4112 }
4113 if (dyn.d_tag == DT_RUNPATH)
4114 {
4115 struct bfd_link_needed_list *n, **pn;
4116 char *fnm, *anm;
4117 unsigned int tagv = dyn.d_un.d_val;
4118
4119 amt = sizeof (struct bfd_link_needed_list);
4120 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4121 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4122 if (n == NULL || fnm == NULL)
4123 goto error_free_dyn;
4124 amt = strlen (fnm) + 1;
4125 anm = (char *) bfd_alloc (abfd, amt);
4126 if (anm == NULL)
4127 goto error_free_dyn;
4128 memcpy (anm, fnm, amt);
4129 n->name = anm;
4130 n->by = abfd;
4131 n->next = NULL;
4132 for (pn = & runpath;
4133 *pn != NULL;
4134 pn = &(*pn)->next)
4135 ;
4136 *pn = n;
4137 }
4138 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4139 if (!runpath && dyn.d_tag == DT_RPATH)
4140 {
4141 struct bfd_link_needed_list *n, **pn;
4142 char *fnm, *anm;
4143 unsigned int tagv = dyn.d_un.d_val;
4144
4145 amt = sizeof (struct bfd_link_needed_list);
4146 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4147 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4148 if (n == NULL || fnm == NULL)
4149 goto error_free_dyn;
4150 amt = strlen (fnm) + 1;
4151 anm = (char *) bfd_alloc (abfd, amt);
4152 if (anm == NULL)
4153 goto error_free_dyn;
4154 memcpy (anm, fnm, amt);
4155 n->name = anm;
4156 n->by = abfd;
4157 n->next = NULL;
4158 for (pn = & rpath;
4159 *pn != NULL;
4160 pn = &(*pn)->next)
4161 ;
4162 *pn = n;
4163 }
4164 if (dyn.d_tag == DT_AUDIT)
4165 {
4166 unsigned int tagv = dyn.d_un.d_val;
4167 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4168 }
4169 }
4170
4171 free (dynbuf);
4172 }
4173
4174 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4175 frees all more recently bfd_alloc'd blocks as well. */
4176 if (runpath)
4177 rpath = runpath;
4178
4179 if (rpath)
4180 {
4181 struct bfd_link_needed_list **pn;
4182 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4183 ;
4184 *pn = rpath;
4185 }
4186
4187 /* If we have a PT_GNU_RELRO program header, mark as read-only
4188 all sections contained fully therein. This makes relro
4189 shared library sections appear as they will at run-time. */
4190 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4191 while (phdr-- > elf_tdata (abfd)->phdr)
4192 if (phdr->p_type == PT_GNU_RELRO)
4193 {
4194 for (s = abfd->sections; s != NULL; s = s->next)
4195 if ((s->flags & SEC_ALLOC) != 0
4196 && s->vma >= phdr->p_vaddr
4197 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4198 s->flags |= SEC_READONLY;
4199 break;
4200 }
4201
4202 /* We do not want to include any of the sections in a dynamic
4203 object in the output file. We hack by simply clobbering the
4204 list of sections in the BFD. This could be handled more
4205 cleanly by, say, a new section flag; the existing
4206 SEC_NEVER_LOAD flag is not the one we want, because that one
4207 still implies that the section takes up space in the output
4208 file. */
4209 bfd_section_list_clear (abfd);
4210
4211 /* Find the name to use in a DT_NEEDED entry that refers to this
4212 object. If the object has a DT_SONAME entry, we use it.
4213 Otherwise, if the generic linker stuck something in
4214 elf_dt_name, we use that. Otherwise, we just use the file
4215 name. */
4216 if (soname == NULL || *soname == '\0')
4217 {
4218 soname = elf_dt_name (abfd);
4219 if (soname == NULL || *soname == '\0')
4220 soname = bfd_get_filename (abfd);
4221 }
4222
4223 /* Save the SONAME because sometimes the linker emulation code
4224 will need to know it. */
4225 elf_dt_name (abfd) = soname;
4226
4227 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4228 if (ret < 0)
4229 goto error_return;
4230
4231 /* If we have already included this dynamic object in the
4232 link, just ignore it. There is no reason to include a
4233 particular dynamic object more than once. */
4234 if (ret > 0)
4235 return TRUE;
4236
4237 /* Save the DT_AUDIT entry for the linker emulation code. */
4238 elf_dt_audit (abfd) = audit;
4239 }
4240
4241 /* If this is a dynamic object, we always link against the .dynsym
4242 symbol table, not the .symtab symbol table. The dynamic linker
4243 will only see the .dynsym symbol table, so there is no reason to
4244 look at .symtab for a dynamic object. */
4245
4246 if (! dynamic || elf_dynsymtab (abfd) == 0)
4247 hdr = &elf_tdata (abfd)->symtab_hdr;
4248 else
4249 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4250
4251 symcount = hdr->sh_size / bed->s->sizeof_sym;
4252
4253 /* The sh_info field of the symtab header tells us where the
4254 external symbols start. We don't care about the local symbols at
4255 this point. */
4256 if (elf_bad_symtab (abfd))
4257 {
4258 extsymcount = symcount;
4259 extsymoff = 0;
4260 }
4261 else
4262 {
4263 extsymcount = symcount - hdr->sh_info;
4264 extsymoff = hdr->sh_info;
4265 }
4266
4267 sym_hash = elf_sym_hashes (abfd);
4268 if (extsymcount != 0)
4269 {
4270 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4271 NULL, NULL, NULL);
4272 if (isymbuf == NULL)
4273 goto error_return;
4274
4275 if (sym_hash == NULL)
4276 {
4277 /* We store a pointer to the hash table entry for each
4278 external symbol. */
4279 amt = extsymcount;
4280 amt *= sizeof (struct elf_link_hash_entry *);
4281 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4282 if (sym_hash == NULL)
4283 goto error_free_sym;
4284 elf_sym_hashes (abfd) = sym_hash;
4285 }
4286 }
4287
4288 if (dynamic)
4289 {
4290 /* Read in any version definitions. */
4291 if (!_bfd_elf_slurp_version_tables (abfd,
4292 info->default_imported_symver))
4293 goto error_free_sym;
4294
4295 /* Read in the symbol versions, but don't bother to convert them
4296 to internal format. */
4297 if (elf_dynversym (abfd) != 0)
4298 {
4299 Elf_Internal_Shdr *versymhdr;
4300
4301 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4302 amt = versymhdr->sh_size;
4303 extversym = (Elf_External_Versym *) bfd_malloc (amt);
4304 if (extversym == NULL)
4305 goto error_free_sym;
4306 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4307 || bfd_bread (extversym, amt, abfd) != amt)
4308 goto error_free_vers;
4309 extversym_end = extversym + (amt / sizeof (* extversym));
4310 }
4311 }
4312
4313 /* If we are loading an as-needed shared lib, save the symbol table
4314 state before we start adding symbols. If the lib turns out
4315 to be unneeded, restore the state. */
4316 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4317 {
4318 unsigned int i;
4319 size_t entsize;
4320
4321 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4322 {
4323 struct bfd_hash_entry *p;
4324 struct elf_link_hash_entry *h;
4325
4326 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4327 {
4328 h = (struct elf_link_hash_entry *) p;
4329 entsize += htab->root.table.entsize;
4330 if (h->root.type == bfd_link_hash_warning)
4331 entsize += htab->root.table.entsize;
4332 }
4333 }
4334
4335 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4336 old_tab = bfd_malloc (tabsize + entsize);
4337 if (old_tab == NULL)
4338 goto error_free_vers;
4339
4340 /* Remember the current objalloc pointer, so that all mem for
4341 symbols added can later be reclaimed. */
4342 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4343 if (alloc_mark == NULL)
4344 goto error_free_vers;
4345
4346 /* Make a special call to the linker "notice" function to
4347 tell it that we are about to handle an as-needed lib. */
4348 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4349 goto error_free_vers;
4350
4351 /* Clone the symbol table. Remember some pointers into the
4352 symbol table, and dynamic symbol count. */
4353 old_ent = (char *) old_tab + tabsize;
4354 memcpy (old_tab, htab->root.table.table, tabsize);
4355 old_undefs = htab->root.undefs;
4356 old_undefs_tail = htab->root.undefs_tail;
4357 old_table = htab->root.table.table;
4358 old_size = htab->root.table.size;
4359 old_count = htab->root.table.count;
4360 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4361 if (old_strtab == NULL)
4362 goto error_free_vers;
4363
4364 for (i = 0; i < htab->root.table.size; i++)
4365 {
4366 struct bfd_hash_entry *p;
4367 struct elf_link_hash_entry *h;
4368
4369 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4370 {
4371 memcpy (old_ent, p, htab->root.table.entsize);
4372 old_ent = (char *) old_ent + htab->root.table.entsize;
4373 h = (struct elf_link_hash_entry *) p;
4374 if (h->root.type == bfd_link_hash_warning)
4375 {
4376 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4377 old_ent = (char *) old_ent + htab->root.table.entsize;
4378 }
4379 }
4380 }
4381 }
4382
4383 weaks = NULL;
4384 if (extversym == NULL)
4385 ever = NULL;
4386 else if (extversym + extsymoff < extversym_end)
4387 ever = extversym + extsymoff;
4388 else
4389 {
4390 /* xgettext:c-format */
4391 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4392 abfd, (long) extsymoff,
4393 (long) (extversym_end - extversym) / sizeof (* extversym));
4394 bfd_set_error (bfd_error_bad_value);
4395 goto error_free_vers;
4396 }
4397
4398 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4399 isym < isymend;
4400 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4401 {
4402 int bind;
4403 bfd_vma value;
4404 asection *sec, *new_sec;
4405 flagword flags;
4406 const char *name;
4407 struct elf_link_hash_entry *h;
4408 struct elf_link_hash_entry *hi;
4409 bfd_boolean definition;
4410 bfd_boolean size_change_ok;
4411 bfd_boolean type_change_ok;
4412 bfd_boolean new_weak;
4413 bfd_boolean old_weak;
4414 bfd_boolean override;
4415 bfd_boolean common;
4416 bfd_boolean discarded;
4417 unsigned int old_alignment;
4418 bfd *old_bfd;
4419 bfd_boolean matched;
4420
4421 override = FALSE;
4422
4423 flags = BSF_NO_FLAGS;
4424 sec = NULL;
4425 value = isym->st_value;
4426 common = bed->common_definition (isym);
4427 if (common && info->inhibit_common_definition)
4428 {
4429 /* Treat common symbol as undefined for --no-define-common. */
4430 isym->st_shndx = SHN_UNDEF;
4431 common = FALSE;
4432 }
4433 discarded = FALSE;
4434
4435 bind = ELF_ST_BIND (isym->st_info);
4436 switch (bind)
4437 {
4438 case STB_LOCAL:
4439 /* This should be impossible, since ELF requires that all
4440 global symbols follow all local symbols, and that sh_info
4441 point to the first global symbol. Unfortunately, Irix 5
4442 screws this up. */
4443 continue;
4444
4445 case STB_GLOBAL:
4446 if (isym->st_shndx != SHN_UNDEF && !common)
4447 flags = BSF_GLOBAL;
4448 break;
4449
4450 case STB_WEAK:
4451 flags = BSF_WEAK;
4452 break;
4453
4454 case STB_GNU_UNIQUE:
4455 flags = BSF_GNU_UNIQUE;
4456 break;
4457
4458 default:
4459 /* Leave it up to the processor backend. */
4460 break;
4461 }
4462
4463 if (isym->st_shndx == SHN_UNDEF)
4464 sec = bfd_und_section_ptr;
4465 else if (isym->st_shndx == SHN_ABS)
4466 sec = bfd_abs_section_ptr;
4467 else if (isym->st_shndx == SHN_COMMON)
4468 {
4469 sec = bfd_com_section_ptr;
4470 /* What ELF calls the size we call the value. What ELF
4471 calls the value we call the alignment. */
4472 value = isym->st_size;
4473 }
4474 else
4475 {
4476 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4477 if (sec == NULL)
4478 sec = bfd_abs_section_ptr;
4479 else if (discarded_section (sec))
4480 {
4481 /* Symbols from discarded section are undefined. We keep
4482 its visibility. */
4483 sec = bfd_und_section_ptr;
4484 discarded = TRUE;
4485 isym->st_shndx = SHN_UNDEF;
4486 }
4487 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4488 value -= sec->vma;
4489 }
4490
4491 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4492 isym->st_name);
4493 if (name == NULL)
4494 goto error_free_vers;
4495
4496 if (isym->st_shndx == SHN_COMMON
4497 && (abfd->flags & BFD_PLUGIN) != 0)
4498 {
4499 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4500
4501 if (xc == NULL)
4502 {
4503 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4504 | SEC_EXCLUDE);
4505 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4506 if (xc == NULL)
4507 goto error_free_vers;
4508 }
4509 sec = xc;
4510 }
4511 else if (isym->st_shndx == SHN_COMMON
4512 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4513 && !bfd_link_relocatable (info))
4514 {
4515 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4516
4517 if (tcomm == NULL)
4518 {
4519 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4520 | SEC_LINKER_CREATED);
4521 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4522 if (tcomm == NULL)
4523 goto error_free_vers;
4524 }
4525 sec = tcomm;
4526 }
4527 else if (bed->elf_add_symbol_hook)
4528 {
4529 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4530 &sec, &value))
4531 goto error_free_vers;
4532
4533 /* The hook function sets the name to NULL if this symbol
4534 should be skipped for some reason. */
4535 if (name == NULL)
4536 continue;
4537 }
4538
4539 /* Sanity check that all possibilities were handled. */
4540 if (sec == NULL)
4541 {
4542 bfd_set_error (bfd_error_bad_value);
4543 goto error_free_vers;
4544 }
4545
4546 /* Silently discard TLS symbols from --just-syms. There's
4547 no way to combine a static TLS block with a new TLS block
4548 for this executable. */
4549 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4550 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4551 continue;
4552
4553 if (bfd_is_und_section (sec)
4554 || bfd_is_com_section (sec))
4555 definition = FALSE;
4556 else
4557 definition = TRUE;
4558
4559 size_change_ok = FALSE;
4560 type_change_ok = bed->type_change_ok;
4561 old_weak = FALSE;
4562 matched = FALSE;
4563 old_alignment = 0;
4564 old_bfd = NULL;
4565 new_sec = sec;
4566
4567 if (is_elf_hash_table (htab))
4568 {
4569 Elf_Internal_Versym iver;
4570 unsigned int vernum = 0;
4571 bfd_boolean skip;
4572
4573 if (ever == NULL)
4574 {
4575 if (info->default_imported_symver)
4576 /* Use the default symbol version created earlier. */
4577 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4578 else
4579 iver.vs_vers = 0;
4580 }
4581 else if (ever >= extversym_end)
4582 {
4583 /* xgettext:c-format */
4584 _bfd_error_handler (_("%pB: not enough version information"),
4585 abfd);
4586 bfd_set_error (bfd_error_bad_value);
4587 goto error_free_vers;
4588 }
4589 else
4590 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4591
4592 vernum = iver.vs_vers & VERSYM_VERSION;
4593
4594 /* If this is a hidden symbol, or if it is not version
4595 1, we append the version name to the symbol name.
4596 However, we do not modify a non-hidden absolute symbol
4597 if it is not a function, because it might be the version
4598 symbol itself. FIXME: What if it isn't? */
4599 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4600 || (vernum > 1
4601 && (!bfd_is_abs_section (sec)
4602 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4603 {
4604 const char *verstr;
4605 size_t namelen, verlen, newlen;
4606 char *newname, *p;
4607
4608 if (isym->st_shndx != SHN_UNDEF)
4609 {
4610 if (vernum > elf_tdata (abfd)->cverdefs)
4611 verstr = NULL;
4612 else if (vernum > 1)
4613 verstr =
4614 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4615 else
4616 verstr = "";
4617
4618 if (verstr == NULL)
4619 {
4620 _bfd_error_handler
4621 /* xgettext:c-format */
4622 (_("%pB: %s: invalid version %u (max %d)"),
4623 abfd, name, vernum,
4624 elf_tdata (abfd)->cverdefs);
4625 bfd_set_error (bfd_error_bad_value);
4626 goto error_free_vers;
4627 }
4628 }
4629 else
4630 {
4631 /* We cannot simply test for the number of
4632 entries in the VERNEED section since the
4633 numbers for the needed versions do not start
4634 at 0. */
4635 Elf_Internal_Verneed *t;
4636
4637 verstr = NULL;
4638 for (t = elf_tdata (abfd)->verref;
4639 t != NULL;
4640 t = t->vn_nextref)
4641 {
4642 Elf_Internal_Vernaux *a;
4643
4644 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4645 {
4646 if (a->vna_other == vernum)
4647 {
4648 verstr = a->vna_nodename;
4649 break;
4650 }
4651 }
4652 if (a != NULL)
4653 break;
4654 }
4655 if (verstr == NULL)
4656 {
4657 _bfd_error_handler
4658 /* xgettext:c-format */
4659 (_("%pB: %s: invalid needed version %d"),
4660 abfd, name, vernum);
4661 bfd_set_error (bfd_error_bad_value);
4662 goto error_free_vers;
4663 }
4664 }
4665
4666 namelen = strlen (name);
4667 verlen = strlen (verstr);
4668 newlen = namelen + verlen + 2;
4669 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4670 && isym->st_shndx != SHN_UNDEF)
4671 ++newlen;
4672
4673 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4674 if (newname == NULL)
4675 goto error_free_vers;
4676 memcpy (newname, name, namelen);
4677 p = newname + namelen;
4678 *p++ = ELF_VER_CHR;
4679 /* If this is a defined non-hidden version symbol,
4680 we add another @ to the name. This indicates the
4681 default version of the symbol. */
4682 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4683 && isym->st_shndx != SHN_UNDEF)
4684 *p++ = ELF_VER_CHR;
4685 memcpy (p, verstr, verlen + 1);
4686
4687 name = newname;
4688 }
4689
4690 /* If this symbol has default visibility and the user has
4691 requested we not re-export it, then mark it as hidden. */
4692 if (!bfd_is_und_section (sec)
4693 && !dynamic
4694 && abfd->no_export
4695 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4696 isym->st_other = (STV_HIDDEN
4697 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4698
4699 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4700 sym_hash, &old_bfd, &old_weak,
4701 &old_alignment, &skip, &override,
4702 &type_change_ok, &size_change_ok,
4703 &matched))
4704 goto error_free_vers;
4705
4706 if (skip)
4707 continue;
4708
4709 /* Override a definition only if the new symbol matches the
4710 existing one. */
4711 if (override && matched)
4712 definition = FALSE;
4713
4714 h = *sym_hash;
4715 while (h->root.type == bfd_link_hash_indirect
4716 || h->root.type == bfd_link_hash_warning)
4717 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4718
4719 if (elf_tdata (abfd)->verdef != NULL
4720 && vernum > 1
4721 && definition)
4722 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4723 }
4724
4725 if (! (_bfd_generic_link_add_one_symbol
4726 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4727 (struct bfd_link_hash_entry **) sym_hash)))
4728 goto error_free_vers;
4729
4730 if ((abfd->flags & DYNAMIC) == 0
4731 && (bfd_get_flavour (info->output_bfd)
4732 == bfd_target_elf_flavour))
4733 {
4734 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4735 elf_tdata (info->output_bfd)->has_gnu_symbols
4736 |= elf_gnu_symbol_ifunc;
4737 if ((flags & BSF_GNU_UNIQUE))
4738 elf_tdata (info->output_bfd)->has_gnu_symbols
4739 |= elf_gnu_symbol_unique;
4740 }
4741
4742 h = *sym_hash;
4743 /* We need to make sure that indirect symbol dynamic flags are
4744 updated. */
4745 hi = h;
4746 while (h->root.type == bfd_link_hash_indirect
4747 || h->root.type == bfd_link_hash_warning)
4748 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4749
4750 /* Setting the index to -3 tells elf_link_output_extsym that
4751 this symbol is defined in a discarded section. */
4752 if (discarded)
4753 h->indx = -3;
4754
4755 *sym_hash = h;
4756
4757 new_weak = (flags & BSF_WEAK) != 0;
4758 if (dynamic
4759 && definition
4760 && new_weak
4761 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4762 && is_elf_hash_table (htab)
4763 && h->u.alias == NULL)
4764 {
4765 /* Keep a list of all weak defined non function symbols from
4766 a dynamic object, using the alias field. Later in this
4767 function we will set the alias field to the correct
4768 value. We only put non-function symbols from dynamic
4769 objects on this list, because that happens to be the only
4770 time we need to know the normal symbol corresponding to a
4771 weak symbol, and the information is time consuming to
4772 figure out. If the alias field is not already NULL,
4773 then this symbol was already defined by some previous
4774 dynamic object, and we will be using that previous
4775 definition anyhow. */
4776
4777 h->u.alias = weaks;
4778 weaks = h;
4779 }
4780
4781 /* Set the alignment of a common symbol. */
4782 if ((common || bfd_is_com_section (sec))
4783 && h->root.type == bfd_link_hash_common)
4784 {
4785 unsigned int align;
4786
4787 if (common)
4788 align = bfd_log2 (isym->st_value);
4789 else
4790 {
4791 /* The new symbol is a common symbol in a shared object.
4792 We need to get the alignment from the section. */
4793 align = new_sec->alignment_power;
4794 }
4795 if (align > old_alignment)
4796 h->root.u.c.p->alignment_power = align;
4797 else
4798 h->root.u.c.p->alignment_power = old_alignment;
4799 }
4800
4801 if (is_elf_hash_table (htab))
4802 {
4803 /* Set a flag in the hash table entry indicating the type of
4804 reference or definition we just found. A dynamic symbol
4805 is one which is referenced or defined by both a regular
4806 object and a shared object. */
4807 bfd_boolean dynsym = FALSE;
4808
4809 /* Plugin symbols aren't normal. Don't set def_regular or
4810 ref_regular for them, or make them dynamic. */
4811 if ((abfd->flags & BFD_PLUGIN) != 0)
4812 ;
4813 else if (! dynamic)
4814 {
4815 if (! definition)
4816 {
4817 h->ref_regular = 1;
4818 if (bind != STB_WEAK)
4819 h->ref_regular_nonweak = 1;
4820 }
4821 else
4822 {
4823 h->def_regular = 1;
4824 if (h->def_dynamic)
4825 {
4826 h->def_dynamic = 0;
4827 h->ref_dynamic = 1;
4828 }
4829 }
4830
4831 /* If the indirect symbol has been forced local, don't
4832 make the real symbol dynamic. */
4833 if ((h == hi || !hi->forced_local)
4834 && (bfd_link_dll (info)
4835 || h->def_dynamic
4836 || h->ref_dynamic))
4837 dynsym = TRUE;
4838 }
4839 else
4840 {
4841 if (! definition)
4842 {
4843 h->ref_dynamic = 1;
4844 hi->ref_dynamic = 1;
4845 }
4846 else
4847 {
4848 h->def_dynamic = 1;
4849 hi->def_dynamic = 1;
4850 }
4851
4852 /* If the indirect symbol has been forced local, don't
4853 make the real symbol dynamic. */
4854 if ((h == hi || !hi->forced_local)
4855 && (h->def_regular
4856 || h->ref_regular
4857 || (h->is_weakalias
4858 && weakdef (h)->dynindx != -1)))
4859 dynsym = TRUE;
4860 }
4861
4862 /* Check to see if we need to add an indirect symbol for
4863 the default name. */
4864 if (definition
4865 || (!override && h->root.type == bfd_link_hash_common))
4866 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4867 sec, value, &old_bfd, &dynsym))
4868 goto error_free_vers;
4869
4870 /* Check the alignment when a common symbol is involved. This
4871 can change when a common symbol is overridden by a normal
4872 definition or a common symbol is ignored due to the old
4873 normal definition. We need to make sure the maximum
4874 alignment is maintained. */
4875 if ((old_alignment || common)
4876 && h->root.type != bfd_link_hash_common)
4877 {
4878 unsigned int common_align;
4879 unsigned int normal_align;
4880 unsigned int symbol_align;
4881 bfd *normal_bfd;
4882 bfd *common_bfd;
4883
4884 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4885 || h->root.type == bfd_link_hash_defweak);
4886
4887 symbol_align = ffs (h->root.u.def.value) - 1;
4888 if (h->root.u.def.section->owner != NULL
4889 && (h->root.u.def.section->owner->flags
4890 & (DYNAMIC | BFD_PLUGIN)) == 0)
4891 {
4892 normal_align = h->root.u.def.section->alignment_power;
4893 if (normal_align > symbol_align)
4894 normal_align = symbol_align;
4895 }
4896 else
4897 normal_align = symbol_align;
4898
4899 if (old_alignment)
4900 {
4901 common_align = old_alignment;
4902 common_bfd = old_bfd;
4903 normal_bfd = abfd;
4904 }
4905 else
4906 {
4907 common_align = bfd_log2 (isym->st_value);
4908 common_bfd = abfd;
4909 normal_bfd = old_bfd;
4910 }
4911
4912 if (normal_align < common_align)
4913 {
4914 /* PR binutils/2735 */
4915 if (normal_bfd == NULL)
4916 _bfd_error_handler
4917 /* xgettext:c-format */
4918 (_("warning: alignment %u of common symbol `%s' in %pB is"
4919 " greater than the alignment (%u) of its section %pA"),
4920 1 << common_align, name, common_bfd,
4921 1 << normal_align, h->root.u.def.section);
4922 else
4923 _bfd_error_handler
4924 /* xgettext:c-format */
4925 (_("warning: alignment %u of symbol `%s' in %pB"
4926 " is smaller than %u in %pB"),
4927 1 << normal_align, name, normal_bfd,
4928 1 << common_align, common_bfd);
4929 }
4930 }
4931
4932 /* Remember the symbol size if it isn't undefined. */
4933 if (isym->st_size != 0
4934 && isym->st_shndx != SHN_UNDEF
4935 && (definition || h->size == 0))
4936 {
4937 if (h->size != 0
4938 && h->size != isym->st_size
4939 && ! size_change_ok)
4940 _bfd_error_handler
4941 /* xgettext:c-format */
4942 (_("warning: size of symbol `%s' changed"
4943 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
4944 name, (uint64_t) h->size, old_bfd,
4945 (uint64_t) isym->st_size, abfd);
4946
4947 h->size = isym->st_size;
4948 }
4949
4950 /* If this is a common symbol, then we always want H->SIZE
4951 to be the size of the common symbol. The code just above
4952 won't fix the size if a common symbol becomes larger. We
4953 don't warn about a size change here, because that is
4954 covered by --warn-common. Allow changes between different
4955 function types. */
4956 if (h->root.type == bfd_link_hash_common)
4957 h->size = h->root.u.c.size;
4958
4959 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4960 && ((definition && !new_weak)
4961 || (old_weak && h->root.type == bfd_link_hash_common)
4962 || h->type == STT_NOTYPE))
4963 {
4964 unsigned int type = ELF_ST_TYPE (isym->st_info);
4965
4966 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4967 symbol. */
4968 if (type == STT_GNU_IFUNC
4969 && (abfd->flags & DYNAMIC) != 0)
4970 type = STT_FUNC;
4971
4972 if (h->type != type)
4973 {
4974 if (h->type != STT_NOTYPE && ! type_change_ok)
4975 /* xgettext:c-format */
4976 _bfd_error_handler
4977 (_("warning: type of symbol `%s' changed"
4978 " from %d to %d in %pB"),
4979 name, h->type, type, abfd);
4980
4981 h->type = type;
4982 }
4983 }
4984
4985 /* Merge st_other field. */
4986 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4987
4988 /* We don't want to make debug symbol dynamic. */
4989 if (definition
4990 && (sec->flags & SEC_DEBUGGING)
4991 && !bfd_link_relocatable (info))
4992 dynsym = FALSE;
4993
4994 /* Nor should we make plugin symbols dynamic. */
4995 if ((abfd->flags & BFD_PLUGIN) != 0)
4996 dynsym = FALSE;
4997
4998 if (definition)
4999 {
5000 h->target_internal = isym->st_target_internal;
5001 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5002 }
5003
5004 if (definition && !dynamic)
5005 {
5006 char *p = strchr (name, ELF_VER_CHR);
5007 if (p != NULL && p[1] != ELF_VER_CHR)
5008 {
5009 /* Queue non-default versions so that .symver x, x@FOO
5010 aliases can be checked. */
5011 if (!nondeflt_vers)
5012 {
5013 amt = ((isymend - isym + 1)
5014 * sizeof (struct elf_link_hash_entry *));
5015 nondeflt_vers
5016 = (struct elf_link_hash_entry **) bfd_malloc (amt);
5017 if (!nondeflt_vers)
5018 goto error_free_vers;
5019 }
5020 nondeflt_vers[nondeflt_vers_cnt++] = h;
5021 }
5022 }
5023
5024 if (dynsym && h->dynindx == -1)
5025 {
5026 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5027 goto error_free_vers;
5028 if (h->is_weakalias
5029 && weakdef (h)->dynindx == -1)
5030 {
5031 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5032 goto error_free_vers;
5033 }
5034 }
5035 else if (h->dynindx != -1)
5036 /* If the symbol already has a dynamic index, but
5037 visibility says it should not be visible, turn it into
5038 a local symbol. */
5039 switch (ELF_ST_VISIBILITY (h->other))
5040 {
5041 case STV_INTERNAL:
5042 case STV_HIDDEN:
5043 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
5044 dynsym = FALSE;
5045 break;
5046 }
5047
5048 /* Don't add DT_NEEDED for references from the dummy bfd nor
5049 for unmatched symbol. */
5050 if (!add_needed
5051 && matched
5052 && definition
5053 && ((dynsym
5054 && h->ref_regular_nonweak
5055 && (old_bfd == NULL
5056 || (old_bfd->flags & BFD_PLUGIN) == 0))
5057 || (h->ref_dynamic_nonweak
5058 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5059 && !on_needed_list (elf_dt_name (abfd),
5060 htab->needed, NULL))))
5061 {
5062 int ret;
5063 const char *soname = elf_dt_name (abfd);
5064
5065 info->callbacks->minfo ("%!", soname, old_bfd,
5066 h->root.root.string);
5067
5068 /* A symbol from a library loaded via DT_NEEDED of some
5069 other library is referenced by a regular object.
5070 Add a DT_NEEDED entry for it. Issue an error if
5071 --no-add-needed is used and the reference was not
5072 a weak one. */
5073 if (old_bfd != NULL
5074 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5075 {
5076 _bfd_error_handler
5077 /* xgettext:c-format */
5078 (_("%pB: undefined reference to symbol '%s'"),
5079 old_bfd, name);
5080 bfd_set_error (bfd_error_missing_dso);
5081 goto error_free_vers;
5082 }
5083
5084 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5085 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5086
5087 add_needed = TRUE;
5088 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
5089 if (ret < 0)
5090 goto error_free_vers;
5091
5092 BFD_ASSERT (ret == 0);
5093 }
5094 }
5095 }
5096
5097 if (info->lto_plugin_active
5098 && !bfd_link_relocatable (info)
5099 && (abfd->flags & BFD_PLUGIN) == 0
5100 && !just_syms
5101 && extsymcount)
5102 {
5103 int r_sym_shift;
5104
5105 if (bed->s->arch_size == 32)
5106 r_sym_shift = 8;
5107 else
5108 r_sym_shift = 32;
5109
5110 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5111 referenced in regular objects so that linker plugin will get
5112 the correct symbol resolution. */
5113
5114 sym_hash = elf_sym_hashes (abfd);
5115 for (s = abfd->sections; s != NULL; s = s->next)
5116 {
5117 Elf_Internal_Rela *internal_relocs;
5118 Elf_Internal_Rela *rel, *relend;
5119
5120 /* Don't check relocations in excluded sections. */
5121 if ((s->flags & SEC_RELOC) == 0
5122 || s->reloc_count == 0
5123 || (s->flags & SEC_EXCLUDE) != 0
5124 || ((info->strip == strip_all
5125 || info->strip == strip_debugger)
5126 && (s->flags & SEC_DEBUGGING) != 0))
5127 continue;
5128
5129 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5130 NULL,
5131 info->keep_memory);
5132 if (internal_relocs == NULL)
5133 goto error_free_vers;
5134
5135 rel = internal_relocs;
5136 relend = rel + s->reloc_count;
5137 for ( ; rel < relend; rel++)
5138 {
5139 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5140 struct elf_link_hash_entry *h;
5141
5142 /* Skip local symbols. */
5143 if (r_symndx < extsymoff)
5144 continue;
5145
5146 h = sym_hash[r_symndx - extsymoff];
5147 if (h != NULL)
5148 h->root.non_ir_ref_regular = 1;
5149 }
5150
5151 if (elf_section_data (s)->relocs != internal_relocs)
5152 free (internal_relocs);
5153 }
5154 }
5155
5156 if (extversym != NULL)
5157 {
5158 free (extversym);
5159 extversym = NULL;
5160 }
5161
5162 if (isymbuf != NULL)
5163 {
5164 free (isymbuf);
5165 isymbuf = NULL;
5166 }
5167
5168 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5169 {
5170 unsigned int i;
5171
5172 /* Restore the symbol table. */
5173 old_ent = (char *) old_tab + tabsize;
5174 memset (elf_sym_hashes (abfd), 0,
5175 extsymcount * sizeof (struct elf_link_hash_entry *));
5176 htab->root.table.table = old_table;
5177 htab->root.table.size = old_size;
5178 htab->root.table.count = old_count;
5179 memcpy (htab->root.table.table, old_tab, tabsize);
5180 htab->root.undefs = old_undefs;
5181 htab->root.undefs_tail = old_undefs_tail;
5182 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5183 free (old_strtab);
5184 old_strtab = NULL;
5185 for (i = 0; i < htab->root.table.size; i++)
5186 {
5187 struct bfd_hash_entry *p;
5188 struct elf_link_hash_entry *h;
5189 bfd_size_type size;
5190 unsigned int alignment_power;
5191 unsigned int non_ir_ref_dynamic;
5192
5193 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5194 {
5195 h = (struct elf_link_hash_entry *) p;
5196 if (h->root.type == bfd_link_hash_warning)
5197 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5198
5199 /* Preserve the maximum alignment and size for common
5200 symbols even if this dynamic lib isn't on DT_NEEDED
5201 since it can still be loaded at run time by another
5202 dynamic lib. */
5203 if (h->root.type == bfd_link_hash_common)
5204 {
5205 size = h->root.u.c.size;
5206 alignment_power = h->root.u.c.p->alignment_power;
5207 }
5208 else
5209 {
5210 size = 0;
5211 alignment_power = 0;
5212 }
5213 /* Preserve non_ir_ref_dynamic so that this symbol
5214 will be exported when the dynamic lib becomes needed
5215 in the second pass. */
5216 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5217 memcpy (p, old_ent, htab->root.table.entsize);
5218 old_ent = (char *) old_ent + htab->root.table.entsize;
5219 h = (struct elf_link_hash_entry *) p;
5220 if (h->root.type == bfd_link_hash_warning)
5221 {
5222 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5223 old_ent = (char *) old_ent + htab->root.table.entsize;
5224 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5225 }
5226 if (h->root.type == bfd_link_hash_common)
5227 {
5228 if (size > h->root.u.c.size)
5229 h->root.u.c.size = size;
5230 if (alignment_power > h->root.u.c.p->alignment_power)
5231 h->root.u.c.p->alignment_power = alignment_power;
5232 }
5233 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5234 }
5235 }
5236
5237 /* Make a special call to the linker "notice" function to
5238 tell it that symbols added for crefs may need to be removed. */
5239 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5240 goto error_free_vers;
5241
5242 free (old_tab);
5243 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5244 alloc_mark);
5245 if (nondeflt_vers != NULL)
5246 free (nondeflt_vers);
5247 return TRUE;
5248 }
5249
5250 if (old_tab != NULL)
5251 {
5252 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5253 goto error_free_vers;
5254 free (old_tab);
5255 old_tab = NULL;
5256 }
5257
5258 /* Now that all the symbols from this input file are created, if
5259 not performing a relocatable link, handle .symver foo, foo@BAR
5260 such that any relocs against foo become foo@BAR. */
5261 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5262 {
5263 size_t cnt, symidx;
5264
5265 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5266 {
5267 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5268 char *shortname, *p;
5269
5270 p = strchr (h->root.root.string, ELF_VER_CHR);
5271 if (p == NULL
5272 || (h->root.type != bfd_link_hash_defined
5273 && h->root.type != bfd_link_hash_defweak))
5274 continue;
5275
5276 amt = p - h->root.root.string;
5277 shortname = (char *) bfd_malloc (amt + 1);
5278 if (!shortname)
5279 goto error_free_vers;
5280 memcpy (shortname, h->root.root.string, amt);
5281 shortname[amt] = '\0';
5282
5283 hi = (struct elf_link_hash_entry *)
5284 bfd_link_hash_lookup (&htab->root, shortname,
5285 FALSE, FALSE, FALSE);
5286 if (hi != NULL
5287 && hi->root.type == h->root.type
5288 && hi->root.u.def.value == h->root.u.def.value
5289 && hi->root.u.def.section == h->root.u.def.section)
5290 {
5291 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5292 hi->root.type = bfd_link_hash_indirect;
5293 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5294 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5295 sym_hash = elf_sym_hashes (abfd);
5296 if (sym_hash)
5297 for (symidx = 0; symidx < extsymcount; ++symidx)
5298 if (sym_hash[symidx] == hi)
5299 {
5300 sym_hash[symidx] = h;
5301 break;
5302 }
5303 }
5304 free (shortname);
5305 }
5306 free (nondeflt_vers);
5307 nondeflt_vers = NULL;
5308 }
5309
5310 /* Now set the alias field correctly for all the weak defined
5311 symbols we found. The only way to do this is to search all the
5312 symbols. Since we only need the information for non functions in
5313 dynamic objects, that's the only time we actually put anything on
5314 the list WEAKS. We need this information so that if a regular
5315 object refers to a symbol defined weakly in a dynamic object, the
5316 real symbol in the dynamic object is also put in the dynamic
5317 symbols; we also must arrange for both symbols to point to the
5318 same memory location. We could handle the general case of symbol
5319 aliasing, but a general symbol alias can only be generated in
5320 assembler code, handling it correctly would be very time
5321 consuming, and other ELF linkers don't handle general aliasing
5322 either. */
5323 if (weaks != NULL)
5324 {
5325 struct elf_link_hash_entry **hpp;
5326 struct elf_link_hash_entry **hppend;
5327 struct elf_link_hash_entry **sorted_sym_hash;
5328 struct elf_link_hash_entry *h;
5329 size_t sym_count;
5330
5331 /* Since we have to search the whole symbol list for each weak
5332 defined symbol, search time for N weak defined symbols will be
5333 O(N^2). Binary search will cut it down to O(NlogN). */
5334 amt = extsymcount;
5335 amt *= sizeof (struct elf_link_hash_entry *);
5336 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
5337 if (sorted_sym_hash == NULL)
5338 goto error_return;
5339 sym_hash = sorted_sym_hash;
5340 hpp = elf_sym_hashes (abfd);
5341 hppend = hpp + extsymcount;
5342 sym_count = 0;
5343 for (; hpp < hppend; hpp++)
5344 {
5345 h = *hpp;
5346 if (h != NULL
5347 && h->root.type == bfd_link_hash_defined
5348 && !bed->is_function_type (h->type))
5349 {
5350 *sym_hash = h;
5351 sym_hash++;
5352 sym_count++;
5353 }
5354 }
5355
5356 qsort (sorted_sym_hash, sym_count,
5357 sizeof (struct elf_link_hash_entry *),
5358 elf_sort_symbol);
5359
5360 while (weaks != NULL)
5361 {
5362 struct elf_link_hash_entry *hlook;
5363 asection *slook;
5364 bfd_vma vlook;
5365 size_t i, j, idx = 0;
5366
5367 hlook = weaks;
5368 weaks = hlook->u.alias;
5369 hlook->u.alias = NULL;
5370
5371 if (hlook->root.type != bfd_link_hash_defined
5372 && hlook->root.type != bfd_link_hash_defweak)
5373 continue;
5374
5375 slook = hlook->root.u.def.section;
5376 vlook = hlook->root.u.def.value;
5377
5378 i = 0;
5379 j = sym_count;
5380 while (i != j)
5381 {
5382 bfd_signed_vma vdiff;
5383 idx = (i + j) / 2;
5384 h = sorted_sym_hash[idx];
5385 vdiff = vlook - h->root.u.def.value;
5386 if (vdiff < 0)
5387 j = idx;
5388 else if (vdiff > 0)
5389 i = idx + 1;
5390 else
5391 {
5392 int sdiff = slook->id - h->root.u.def.section->id;
5393 if (sdiff < 0)
5394 j = idx;
5395 else if (sdiff > 0)
5396 i = idx + 1;
5397 else
5398 break;
5399 }
5400 }
5401
5402 /* We didn't find a value/section match. */
5403 if (i == j)
5404 continue;
5405
5406 /* With multiple aliases, or when the weak symbol is already
5407 strongly defined, we have multiple matching symbols and
5408 the binary search above may land on any of them. Step
5409 one past the matching symbol(s). */
5410 while (++idx != j)
5411 {
5412 h = sorted_sym_hash[idx];
5413 if (h->root.u.def.section != slook
5414 || h->root.u.def.value != vlook)
5415 break;
5416 }
5417
5418 /* Now look back over the aliases. Since we sorted by size
5419 as well as value and section, we'll choose the one with
5420 the largest size. */
5421 while (idx-- != i)
5422 {
5423 h = sorted_sym_hash[idx];
5424
5425 /* Stop if value or section doesn't match. */
5426 if (h->root.u.def.section != slook
5427 || h->root.u.def.value != vlook)
5428 break;
5429 else if (h != hlook)
5430 {
5431 struct elf_link_hash_entry *t;
5432
5433 hlook->u.alias = h;
5434 hlook->is_weakalias = 1;
5435 t = h;
5436 if (t->u.alias != NULL)
5437 while (t->u.alias != h)
5438 t = t->u.alias;
5439 t->u.alias = hlook;
5440
5441 /* If the weak definition is in the list of dynamic
5442 symbols, make sure the real definition is put
5443 there as well. */
5444 if (hlook->dynindx != -1 && h->dynindx == -1)
5445 {
5446 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5447 {
5448 err_free_sym_hash:
5449 free (sorted_sym_hash);
5450 goto error_return;
5451 }
5452 }
5453
5454 /* If the real definition is in the list of dynamic
5455 symbols, make sure the weak definition is put
5456 there as well. If we don't do this, then the
5457 dynamic loader might not merge the entries for the
5458 real definition and the weak definition. */
5459 if (h->dynindx != -1 && hlook->dynindx == -1)
5460 {
5461 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5462 goto err_free_sym_hash;
5463 }
5464 break;
5465 }
5466 }
5467 }
5468
5469 free (sorted_sym_hash);
5470 }
5471
5472 if (bed->check_directives
5473 && !(*bed->check_directives) (abfd, info))
5474 return FALSE;
5475
5476 /* If this is a non-traditional link, try to optimize the handling
5477 of the .stab/.stabstr sections. */
5478 if (! dynamic
5479 && ! info->traditional_format
5480 && is_elf_hash_table (htab)
5481 && (info->strip != strip_all && info->strip != strip_debugger))
5482 {
5483 asection *stabstr;
5484
5485 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5486 if (stabstr != NULL)
5487 {
5488 bfd_size_type string_offset = 0;
5489 asection *stab;
5490
5491 for (stab = abfd->sections; stab; stab = stab->next)
5492 if (CONST_STRNEQ (stab->name, ".stab")
5493 && (!stab->name[5] ||
5494 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5495 && (stab->flags & SEC_MERGE) == 0
5496 && !bfd_is_abs_section (stab->output_section))
5497 {
5498 struct bfd_elf_section_data *secdata;
5499
5500 secdata = elf_section_data (stab);
5501 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5502 stabstr, &secdata->sec_info,
5503 &string_offset))
5504 goto error_return;
5505 if (secdata->sec_info)
5506 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5507 }
5508 }
5509 }
5510
5511 if (is_elf_hash_table (htab) && add_needed)
5512 {
5513 /* Add this bfd to the loaded list. */
5514 struct elf_link_loaded_list *n;
5515
5516 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5517 if (n == NULL)
5518 goto error_return;
5519 n->abfd = abfd;
5520 n->next = htab->loaded;
5521 htab->loaded = n;
5522 }
5523
5524 return TRUE;
5525
5526 error_free_vers:
5527 if (old_tab != NULL)
5528 free (old_tab);
5529 if (old_strtab != NULL)
5530 free (old_strtab);
5531 if (nondeflt_vers != NULL)
5532 free (nondeflt_vers);
5533 if (extversym != NULL)
5534 free (extversym);
5535 error_free_sym:
5536 if (isymbuf != NULL)
5537 free (isymbuf);
5538 error_return:
5539 return FALSE;
5540 }
5541
5542 /* Return the linker hash table entry of a symbol that might be
5543 satisfied by an archive symbol. Return -1 on error. */
5544
5545 struct elf_link_hash_entry *
5546 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5547 struct bfd_link_info *info,
5548 const char *name)
5549 {
5550 struct elf_link_hash_entry *h;
5551 char *p, *copy;
5552 size_t len, first;
5553
5554 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5555 if (h != NULL)
5556 return h;
5557
5558 /* If this is a default version (the name contains @@), look up the
5559 symbol again with only one `@' as well as without the version.
5560 The effect is that references to the symbol with and without the
5561 version will be matched by the default symbol in the archive. */
5562
5563 p = strchr (name, ELF_VER_CHR);
5564 if (p == NULL || p[1] != ELF_VER_CHR)
5565 return h;
5566
5567 /* First check with only one `@'. */
5568 len = strlen (name);
5569 copy = (char *) bfd_alloc (abfd, len);
5570 if (copy == NULL)
5571 return (struct elf_link_hash_entry *) -1;
5572
5573 first = p - name + 1;
5574 memcpy (copy, name, first);
5575 memcpy (copy + first, name + first + 1, len - first);
5576
5577 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5578 if (h == NULL)
5579 {
5580 /* We also need to check references to the symbol without the
5581 version. */
5582 copy[first - 1] = '\0';
5583 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5584 FALSE, FALSE, TRUE);
5585 }
5586
5587 bfd_release (abfd, copy);
5588 return h;
5589 }
5590
5591 /* Add symbols from an ELF archive file to the linker hash table. We
5592 don't use _bfd_generic_link_add_archive_symbols because we need to
5593 handle versioned symbols.
5594
5595 Fortunately, ELF archive handling is simpler than that done by
5596 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5597 oddities. In ELF, if we find a symbol in the archive map, and the
5598 symbol is currently undefined, we know that we must pull in that
5599 object file.
5600
5601 Unfortunately, we do have to make multiple passes over the symbol
5602 table until nothing further is resolved. */
5603
5604 static bfd_boolean
5605 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5606 {
5607 symindex c;
5608 unsigned char *included = NULL;
5609 carsym *symdefs;
5610 bfd_boolean loop;
5611 bfd_size_type amt;
5612 const struct elf_backend_data *bed;
5613 struct elf_link_hash_entry * (*archive_symbol_lookup)
5614 (bfd *, struct bfd_link_info *, const char *);
5615
5616 if (! bfd_has_map (abfd))
5617 {
5618 /* An empty archive is a special case. */
5619 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5620 return TRUE;
5621 bfd_set_error (bfd_error_no_armap);
5622 return FALSE;
5623 }
5624
5625 /* Keep track of all symbols we know to be already defined, and all
5626 files we know to be already included. This is to speed up the
5627 second and subsequent passes. */
5628 c = bfd_ardata (abfd)->symdef_count;
5629 if (c == 0)
5630 return TRUE;
5631 amt = c;
5632 amt *= sizeof (*included);
5633 included = (unsigned char *) bfd_zmalloc (amt);
5634 if (included == NULL)
5635 return FALSE;
5636
5637 symdefs = bfd_ardata (abfd)->symdefs;
5638 bed = get_elf_backend_data (abfd);
5639 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5640
5641 do
5642 {
5643 file_ptr last;
5644 symindex i;
5645 carsym *symdef;
5646 carsym *symdefend;
5647
5648 loop = FALSE;
5649 last = -1;
5650
5651 symdef = symdefs;
5652 symdefend = symdef + c;
5653 for (i = 0; symdef < symdefend; symdef++, i++)
5654 {
5655 struct elf_link_hash_entry *h;
5656 bfd *element;
5657 struct bfd_link_hash_entry *undefs_tail;
5658 symindex mark;
5659
5660 if (included[i])
5661 continue;
5662 if (symdef->file_offset == last)
5663 {
5664 included[i] = TRUE;
5665 continue;
5666 }
5667
5668 h = archive_symbol_lookup (abfd, info, symdef->name);
5669 if (h == (struct elf_link_hash_entry *) -1)
5670 goto error_return;
5671
5672 if (h == NULL)
5673 continue;
5674
5675 if (h->root.type == bfd_link_hash_common)
5676 {
5677 /* We currently have a common symbol. The archive map contains
5678 a reference to this symbol, so we may want to include it. We
5679 only want to include it however, if this archive element
5680 contains a definition of the symbol, not just another common
5681 declaration of it.
5682
5683 Unfortunately some archivers (including GNU ar) will put
5684 declarations of common symbols into their archive maps, as
5685 well as real definitions, so we cannot just go by the archive
5686 map alone. Instead we must read in the element's symbol
5687 table and check that to see what kind of symbol definition
5688 this is. */
5689 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5690 continue;
5691 }
5692 else if (h->root.type != bfd_link_hash_undefined)
5693 {
5694 if (h->root.type != bfd_link_hash_undefweak)
5695 /* Symbol must be defined. Don't check it again. */
5696 included[i] = TRUE;
5697 continue;
5698 }
5699
5700 /* We need to include this archive member. */
5701 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5702 if (element == NULL)
5703 goto error_return;
5704
5705 if (! bfd_check_format (element, bfd_object))
5706 goto error_return;
5707
5708 undefs_tail = info->hash->undefs_tail;
5709
5710 if (!(*info->callbacks
5711 ->add_archive_element) (info, element, symdef->name, &element))
5712 continue;
5713 if (!bfd_link_add_symbols (element, info))
5714 goto error_return;
5715
5716 /* If there are any new undefined symbols, we need to make
5717 another pass through the archive in order to see whether
5718 they can be defined. FIXME: This isn't perfect, because
5719 common symbols wind up on undefs_tail and because an
5720 undefined symbol which is defined later on in this pass
5721 does not require another pass. This isn't a bug, but it
5722 does make the code less efficient than it could be. */
5723 if (undefs_tail != info->hash->undefs_tail)
5724 loop = TRUE;
5725
5726 /* Look backward to mark all symbols from this object file
5727 which we have already seen in this pass. */
5728 mark = i;
5729 do
5730 {
5731 included[mark] = TRUE;
5732 if (mark == 0)
5733 break;
5734 --mark;
5735 }
5736 while (symdefs[mark].file_offset == symdef->file_offset);
5737
5738 /* We mark subsequent symbols from this object file as we go
5739 on through the loop. */
5740 last = symdef->file_offset;
5741 }
5742 }
5743 while (loop);
5744
5745 free (included);
5746
5747 return TRUE;
5748
5749 error_return:
5750 if (included != NULL)
5751 free (included);
5752 return FALSE;
5753 }
5754
5755 /* Given an ELF BFD, add symbols to the global hash table as
5756 appropriate. */
5757
5758 bfd_boolean
5759 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5760 {
5761 switch (bfd_get_format (abfd))
5762 {
5763 case bfd_object:
5764 return elf_link_add_object_symbols (abfd, info);
5765 case bfd_archive:
5766 return elf_link_add_archive_symbols (abfd, info);
5767 default:
5768 bfd_set_error (bfd_error_wrong_format);
5769 return FALSE;
5770 }
5771 }
5772 \f
5773 struct hash_codes_info
5774 {
5775 unsigned long *hashcodes;
5776 bfd_boolean error;
5777 };
5778
5779 /* This function will be called though elf_link_hash_traverse to store
5780 all hash value of the exported symbols in an array. */
5781
5782 static bfd_boolean
5783 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5784 {
5785 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5786 const char *name;
5787 unsigned long ha;
5788 char *alc = NULL;
5789
5790 /* Ignore indirect symbols. These are added by the versioning code. */
5791 if (h->dynindx == -1)
5792 return TRUE;
5793
5794 name = h->root.root.string;
5795 if (h->versioned >= versioned)
5796 {
5797 char *p = strchr (name, ELF_VER_CHR);
5798 if (p != NULL)
5799 {
5800 alc = (char *) bfd_malloc (p - name + 1);
5801 if (alc == NULL)
5802 {
5803 inf->error = TRUE;
5804 return FALSE;
5805 }
5806 memcpy (alc, name, p - name);
5807 alc[p - name] = '\0';
5808 name = alc;
5809 }
5810 }
5811
5812 /* Compute the hash value. */
5813 ha = bfd_elf_hash (name);
5814
5815 /* Store the found hash value in the array given as the argument. */
5816 *(inf->hashcodes)++ = ha;
5817
5818 /* And store it in the struct so that we can put it in the hash table
5819 later. */
5820 h->u.elf_hash_value = ha;
5821
5822 if (alc != NULL)
5823 free (alc);
5824
5825 return TRUE;
5826 }
5827
5828 struct collect_gnu_hash_codes
5829 {
5830 bfd *output_bfd;
5831 const struct elf_backend_data *bed;
5832 unsigned long int nsyms;
5833 unsigned long int maskbits;
5834 unsigned long int *hashcodes;
5835 unsigned long int *hashval;
5836 unsigned long int *indx;
5837 unsigned long int *counts;
5838 bfd_vma *bitmask;
5839 bfd_byte *contents;
5840 long int min_dynindx;
5841 unsigned long int bucketcount;
5842 unsigned long int symindx;
5843 long int local_indx;
5844 long int shift1, shift2;
5845 unsigned long int mask;
5846 bfd_boolean error;
5847 };
5848
5849 /* This function will be called though elf_link_hash_traverse to store
5850 all hash value of the exported symbols in an array. */
5851
5852 static bfd_boolean
5853 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5854 {
5855 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5856 const char *name;
5857 unsigned long ha;
5858 char *alc = NULL;
5859
5860 /* Ignore indirect symbols. These are added by the versioning code. */
5861 if (h->dynindx == -1)
5862 return TRUE;
5863
5864 /* Ignore also local symbols and undefined symbols. */
5865 if (! (*s->bed->elf_hash_symbol) (h))
5866 return TRUE;
5867
5868 name = h->root.root.string;
5869 if (h->versioned >= versioned)
5870 {
5871 char *p = strchr (name, ELF_VER_CHR);
5872 if (p != NULL)
5873 {
5874 alc = (char *) bfd_malloc (p - name + 1);
5875 if (alc == NULL)
5876 {
5877 s->error = TRUE;
5878 return FALSE;
5879 }
5880 memcpy (alc, name, p - name);
5881 alc[p - name] = '\0';
5882 name = alc;
5883 }
5884 }
5885
5886 /* Compute the hash value. */
5887 ha = bfd_elf_gnu_hash (name);
5888
5889 /* Store the found hash value in the array for compute_bucket_count,
5890 and also for .dynsym reordering purposes. */
5891 s->hashcodes[s->nsyms] = ha;
5892 s->hashval[h->dynindx] = ha;
5893 ++s->nsyms;
5894 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5895 s->min_dynindx = h->dynindx;
5896
5897 if (alc != NULL)
5898 free (alc);
5899
5900 return TRUE;
5901 }
5902
5903 /* This function will be called though elf_link_hash_traverse to do
5904 final dynaminc symbol renumbering. */
5905
5906 static bfd_boolean
5907 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5908 {
5909 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5910 unsigned long int bucket;
5911 unsigned long int val;
5912
5913 /* Ignore indirect symbols. */
5914 if (h->dynindx == -1)
5915 return TRUE;
5916
5917 /* Ignore also local symbols and undefined symbols. */
5918 if (! (*s->bed->elf_hash_symbol) (h))
5919 {
5920 if (h->dynindx >= s->min_dynindx)
5921 h->dynindx = s->local_indx++;
5922 return TRUE;
5923 }
5924
5925 bucket = s->hashval[h->dynindx] % s->bucketcount;
5926 val = (s->hashval[h->dynindx] >> s->shift1)
5927 & ((s->maskbits >> s->shift1) - 1);
5928 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5929 s->bitmask[val]
5930 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5931 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5932 if (s->counts[bucket] == 1)
5933 /* Last element terminates the chain. */
5934 val |= 1;
5935 bfd_put_32 (s->output_bfd, val,
5936 s->contents + (s->indx[bucket] - s->symindx) * 4);
5937 --s->counts[bucket];
5938 h->dynindx = s->indx[bucket]++;
5939 return TRUE;
5940 }
5941
5942 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5943
5944 bfd_boolean
5945 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5946 {
5947 return !(h->forced_local
5948 || h->root.type == bfd_link_hash_undefined
5949 || h->root.type == bfd_link_hash_undefweak
5950 || ((h->root.type == bfd_link_hash_defined
5951 || h->root.type == bfd_link_hash_defweak)
5952 && h->root.u.def.section->output_section == NULL));
5953 }
5954
5955 /* Array used to determine the number of hash table buckets to use
5956 based on the number of symbols there are. If there are fewer than
5957 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5958 fewer than 37 we use 17 buckets, and so forth. We never use more
5959 than 32771 buckets. */
5960
5961 static const size_t elf_buckets[] =
5962 {
5963 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5964 16411, 32771, 0
5965 };
5966
5967 /* Compute bucket count for hashing table. We do not use a static set
5968 of possible tables sizes anymore. Instead we determine for all
5969 possible reasonable sizes of the table the outcome (i.e., the
5970 number of collisions etc) and choose the best solution. The
5971 weighting functions are not too simple to allow the table to grow
5972 without bounds. Instead one of the weighting factors is the size.
5973 Therefore the result is always a good payoff between few collisions
5974 (= short chain lengths) and table size. */
5975 static size_t
5976 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5977 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5978 unsigned long int nsyms,
5979 int gnu_hash)
5980 {
5981 size_t best_size = 0;
5982 unsigned long int i;
5983
5984 /* We have a problem here. The following code to optimize the table
5985 size requires an integer type with more the 32 bits. If
5986 BFD_HOST_U_64_BIT is set we know about such a type. */
5987 #ifdef BFD_HOST_U_64_BIT
5988 if (info->optimize)
5989 {
5990 size_t minsize;
5991 size_t maxsize;
5992 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5993 bfd *dynobj = elf_hash_table (info)->dynobj;
5994 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5995 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5996 unsigned long int *counts;
5997 bfd_size_type amt;
5998 unsigned int no_improvement_count = 0;
5999
6000 /* Possible optimization parameters: if we have NSYMS symbols we say
6001 that the hashing table must at least have NSYMS/4 and at most
6002 2*NSYMS buckets. */
6003 minsize = nsyms / 4;
6004 if (minsize == 0)
6005 minsize = 1;
6006 best_size = maxsize = nsyms * 2;
6007 if (gnu_hash)
6008 {
6009 if (minsize < 2)
6010 minsize = 2;
6011 if ((best_size & 31) == 0)
6012 ++best_size;
6013 }
6014
6015 /* Create array where we count the collisions in. We must use bfd_malloc
6016 since the size could be large. */
6017 amt = maxsize;
6018 amt *= sizeof (unsigned long int);
6019 counts = (unsigned long int *) bfd_malloc (amt);
6020 if (counts == NULL)
6021 return 0;
6022
6023 /* Compute the "optimal" size for the hash table. The criteria is a
6024 minimal chain length. The minor criteria is (of course) the size
6025 of the table. */
6026 for (i = minsize; i < maxsize; ++i)
6027 {
6028 /* Walk through the array of hashcodes and count the collisions. */
6029 BFD_HOST_U_64_BIT max;
6030 unsigned long int j;
6031 unsigned long int fact;
6032
6033 if (gnu_hash && (i & 31) == 0)
6034 continue;
6035
6036 memset (counts, '\0', i * sizeof (unsigned long int));
6037
6038 /* Determine how often each hash bucket is used. */
6039 for (j = 0; j < nsyms; ++j)
6040 ++counts[hashcodes[j] % i];
6041
6042 /* For the weight function we need some information about the
6043 pagesize on the target. This is information need not be 100%
6044 accurate. Since this information is not available (so far) we
6045 define it here to a reasonable default value. If it is crucial
6046 to have a better value some day simply define this value. */
6047 # ifndef BFD_TARGET_PAGESIZE
6048 # define BFD_TARGET_PAGESIZE (4096)
6049 # endif
6050
6051 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6052 and the chains. */
6053 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6054
6055 # if 1
6056 /* Variant 1: optimize for short chains. We add the squares
6057 of all the chain lengths (which favors many small chain
6058 over a few long chains). */
6059 for (j = 0; j < i; ++j)
6060 max += counts[j] * counts[j];
6061
6062 /* This adds penalties for the overall size of the table. */
6063 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6064 max *= fact * fact;
6065 # else
6066 /* Variant 2: Optimize a lot more for small table. Here we
6067 also add squares of the size but we also add penalties for
6068 empty slots (the +1 term). */
6069 for (j = 0; j < i; ++j)
6070 max += (1 + counts[j]) * (1 + counts[j]);
6071
6072 /* The overall size of the table is considered, but not as
6073 strong as in variant 1, where it is squared. */
6074 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6075 max *= fact;
6076 # endif
6077
6078 /* Compare with current best results. */
6079 if (max < best_chlen)
6080 {
6081 best_chlen = max;
6082 best_size = i;
6083 no_improvement_count = 0;
6084 }
6085 /* PR 11843: Avoid futile long searches for the best bucket size
6086 when there are a large number of symbols. */
6087 else if (++no_improvement_count == 100)
6088 break;
6089 }
6090
6091 free (counts);
6092 }
6093 else
6094 #endif /* defined (BFD_HOST_U_64_BIT) */
6095 {
6096 /* This is the fallback solution if no 64bit type is available or if we
6097 are not supposed to spend much time on optimizations. We select the
6098 bucket count using a fixed set of numbers. */
6099 for (i = 0; elf_buckets[i] != 0; i++)
6100 {
6101 best_size = elf_buckets[i];
6102 if (nsyms < elf_buckets[i + 1])
6103 break;
6104 }
6105 if (gnu_hash && best_size < 2)
6106 best_size = 2;
6107 }
6108
6109 return best_size;
6110 }
6111
6112 /* Size any SHT_GROUP section for ld -r. */
6113
6114 bfd_boolean
6115 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6116 {
6117 bfd *ibfd;
6118 asection *s;
6119
6120 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6121 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6122 && (s = ibfd->sections) != NULL
6123 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6124 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6125 return FALSE;
6126 return TRUE;
6127 }
6128
6129 /* Set a default stack segment size. The value in INFO wins. If it
6130 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6131 undefined it is initialized. */
6132
6133 bfd_boolean
6134 bfd_elf_stack_segment_size (bfd *output_bfd,
6135 struct bfd_link_info *info,
6136 const char *legacy_symbol,
6137 bfd_vma default_size)
6138 {
6139 struct elf_link_hash_entry *h = NULL;
6140
6141 /* Look for legacy symbol. */
6142 if (legacy_symbol)
6143 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6144 FALSE, FALSE, FALSE);
6145 if (h && (h->root.type == bfd_link_hash_defined
6146 || h->root.type == bfd_link_hash_defweak)
6147 && h->def_regular
6148 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6149 {
6150 /* The symbol has no type if specified on the command line. */
6151 h->type = STT_OBJECT;
6152 if (info->stacksize)
6153 /* xgettext:c-format */
6154 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6155 output_bfd, legacy_symbol);
6156 else if (h->root.u.def.section != bfd_abs_section_ptr)
6157 /* xgettext:c-format */
6158 _bfd_error_handler (_("%pB: %s not absolute"),
6159 output_bfd, legacy_symbol);
6160 else
6161 info->stacksize = h->root.u.def.value;
6162 }
6163
6164 if (!info->stacksize)
6165 /* If the user didn't set a size, or explicitly inhibit the
6166 size, set it now. */
6167 info->stacksize = default_size;
6168
6169 /* Provide the legacy symbol, if it is referenced. */
6170 if (h && (h->root.type == bfd_link_hash_undefined
6171 || h->root.type == bfd_link_hash_undefweak))
6172 {
6173 struct bfd_link_hash_entry *bh = NULL;
6174
6175 if (!(_bfd_generic_link_add_one_symbol
6176 (info, output_bfd, legacy_symbol,
6177 BSF_GLOBAL, bfd_abs_section_ptr,
6178 info->stacksize >= 0 ? info->stacksize : 0,
6179 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6180 return FALSE;
6181
6182 h = (struct elf_link_hash_entry *) bh;
6183 h->def_regular = 1;
6184 h->type = STT_OBJECT;
6185 }
6186
6187 return TRUE;
6188 }
6189
6190 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6191
6192 struct elf_gc_sweep_symbol_info
6193 {
6194 struct bfd_link_info *info;
6195 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6196 bfd_boolean);
6197 };
6198
6199 static bfd_boolean
6200 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6201 {
6202 if (!h->mark
6203 && (((h->root.type == bfd_link_hash_defined
6204 || h->root.type == bfd_link_hash_defweak)
6205 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6206 && h->root.u.def.section->gc_mark))
6207 || h->root.type == bfd_link_hash_undefined
6208 || h->root.type == bfd_link_hash_undefweak))
6209 {
6210 struct elf_gc_sweep_symbol_info *inf;
6211
6212 inf = (struct elf_gc_sweep_symbol_info *) data;
6213 (*inf->hide_symbol) (inf->info, h, TRUE);
6214 h->def_regular = 0;
6215 h->ref_regular = 0;
6216 h->ref_regular_nonweak = 0;
6217 }
6218
6219 return TRUE;
6220 }
6221
6222 /* Set up the sizes and contents of the ELF dynamic sections. This is
6223 called by the ELF linker emulation before_allocation routine. We
6224 must set the sizes of the sections before the linker sets the
6225 addresses of the various sections. */
6226
6227 bfd_boolean
6228 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6229 const char *soname,
6230 const char *rpath,
6231 const char *filter_shlib,
6232 const char *audit,
6233 const char *depaudit,
6234 const char * const *auxiliary_filters,
6235 struct bfd_link_info *info,
6236 asection **sinterpptr)
6237 {
6238 bfd *dynobj;
6239 const struct elf_backend_data *bed;
6240
6241 *sinterpptr = NULL;
6242
6243 if (!is_elf_hash_table (info->hash))
6244 return TRUE;
6245
6246 dynobj = elf_hash_table (info)->dynobj;
6247
6248 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6249 {
6250 struct bfd_elf_version_tree *verdefs;
6251 struct elf_info_failed asvinfo;
6252 struct bfd_elf_version_tree *t;
6253 struct bfd_elf_version_expr *d;
6254 asection *s;
6255 size_t soname_indx;
6256
6257 /* If we are supposed to export all symbols into the dynamic symbol
6258 table (this is not the normal case), then do so. */
6259 if (info->export_dynamic
6260 || (bfd_link_executable (info) && info->dynamic))
6261 {
6262 struct elf_info_failed eif;
6263
6264 eif.info = info;
6265 eif.failed = FALSE;
6266 elf_link_hash_traverse (elf_hash_table (info),
6267 _bfd_elf_export_symbol,
6268 &eif);
6269 if (eif.failed)
6270 return FALSE;
6271 }
6272
6273 if (soname != NULL)
6274 {
6275 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6276 soname, TRUE);
6277 if (soname_indx == (size_t) -1
6278 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6279 return FALSE;
6280 }
6281 else
6282 soname_indx = (size_t) -1;
6283
6284 /* Make all global versions with definition. */
6285 for (t = info->version_info; t != NULL; t = t->next)
6286 for (d = t->globals.list; d != NULL; d = d->next)
6287 if (!d->symver && d->literal)
6288 {
6289 const char *verstr, *name;
6290 size_t namelen, verlen, newlen;
6291 char *newname, *p, leading_char;
6292 struct elf_link_hash_entry *newh;
6293
6294 leading_char = bfd_get_symbol_leading_char (output_bfd);
6295 name = d->pattern;
6296 namelen = strlen (name) + (leading_char != '\0');
6297 verstr = t->name;
6298 verlen = strlen (verstr);
6299 newlen = namelen + verlen + 3;
6300
6301 newname = (char *) bfd_malloc (newlen);
6302 if (newname == NULL)
6303 return FALSE;
6304 newname[0] = leading_char;
6305 memcpy (newname + (leading_char != '\0'), name, namelen);
6306
6307 /* Check the hidden versioned definition. */
6308 p = newname + namelen;
6309 *p++ = ELF_VER_CHR;
6310 memcpy (p, verstr, verlen + 1);
6311 newh = elf_link_hash_lookup (elf_hash_table (info),
6312 newname, FALSE, FALSE,
6313 FALSE);
6314 if (newh == NULL
6315 || (newh->root.type != bfd_link_hash_defined
6316 && newh->root.type != bfd_link_hash_defweak))
6317 {
6318 /* Check the default versioned definition. */
6319 *p++ = ELF_VER_CHR;
6320 memcpy (p, verstr, verlen + 1);
6321 newh = elf_link_hash_lookup (elf_hash_table (info),
6322 newname, FALSE, FALSE,
6323 FALSE);
6324 }
6325 free (newname);
6326
6327 /* Mark this version if there is a definition and it is
6328 not defined in a shared object. */
6329 if (newh != NULL
6330 && !newh->def_dynamic
6331 && (newh->root.type == bfd_link_hash_defined
6332 || newh->root.type == bfd_link_hash_defweak))
6333 d->symver = 1;
6334 }
6335
6336 /* Attach all the symbols to their version information. */
6337 asvinfo.info = info;
6338 asvinfo.failed = FALSE;
6339
6340 elf_link_hash_traverse (elf_hash_table (info),
6341 _bfd_elf_link_assign_sym_version,
6342 &asvinfo);
6343 if (asvinfo.failed)
6344 return FALSE;
6345
6346 if (!info->allow_undefined_version)
6347 {
6348 /* Check if all global versions have a definition. */
6349 bfd_boolean all_defined = TRUE;
6350 for (t = info->version_info; t != NULL; t = t->next)
6351 for (d = t->globals.list; d != NULL; d = d->next)
6352 if (d->literal && !d->symver && !d->script)
6353 {
6354 _bfd_error_handler
6355 (_("%s: undefined version: %s"),
6356 d->pattern, t->name);
6357 all_defined = FALSE;
6358 }
6359
6360 if (!all_defined)
6361 {
6362 bfd_set_error (bfd_error_bad_value);
6363 return FALSE;
6364 }
6365 }
6366
6367 /* Set up the version definition section. */
6368 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6369 BFD_ASSERT (s != NULL);
6370
6371 /* We may have created additional version definitions if we are
6372 just linking a regular application. */
6373 verdefs = info->version_info;
6374
6375 /* Skip anonymous version tag. */
6376 if (verdefs != NULL && verdefs->vernum == 0)
6377 verdefs = verdefs->next;
6378
6379 if (verdefs == NULL && !info->create_default_symver)
6380 s->flags |= SEC_EXCLUDE;
6381 else
6382 {
6383 unsigned int cdefs;
6384 bfd_size_type size;
6385 bfd_byte *p;
6386 Elf_Internal_Verdef def;
6387 Elf_Internal_Verdaux defaux;
6388 struct bfd_link_hash_entry *bh;
6389 struct elf_link_hash_entry *h;
6390 const char *name;
6391
6392 cdefs = 0;
6393 size = 0;
6394
6395 /* Make space for the base version. */
6396 size += sizeof (Elf_External_Verdef);
6397 size += sizeof (Elf_External_Verdaux);
6398 ++cdefs;
6399
6400 /* Make space for the default version. */
6401 if (info->create_default_symver)
6402 {
6403 size += sizeof (Elf_External_Verdef);
6404 ++cdefs;
6405 }
6406
6407 for (t = verdefs; t != NULL; t = t->next)
6408 {
6409 struct bfd_elf_version_deps *n;
6410
6411 /* Don't emit base version twice. */
6412 if (t->vernum == 0)
6413 continue;
6414
6415 size += sizeof (Elf_External_Verdef);
6416 size += sizeof (Elf_External_Verdaux);
6417 ++cdefs;
6418
6419 for (n = t->deps; n != NULL; n = n->next)
6420 size += sizeof (Elf_External_Verdaux);
6421 }
6422
6423 s->size = size;
6424 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6425 if (s->contents == NULL && s->size != 0)
6426 return FALSE;
6427
6428 /* Fill in the version definition section. */
6429
6430 p = s->contents;
6431
6432 def.vd_version = VER_DEF_CURRENT;
6433 def.vd_flags = VER_FLG_BASE;
6434 def.vd_ndx = 1;
6435 def.vd_cnt = 1;
6436 if (info->create_default_symver)
6437 {
6438 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6439 def.vd_next = sizeof (Elf_External_Verdef);
6440 }
6441 else
6442 {
6443 def.vd_aux = sizeof (Elf_External_Verdef);
6444 def.vd_next = (sizeof (Elf_External_Verdef)
6445 + sizeof (Elf_External_Verdaux));
6446 }
6447
6448 if (soname_indx != (size_t) -1)
6449 {
6450 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6451 soname_indx);
6452 def.vd_hash = bfd_elf_hash (soname);
6453 defaux.vda_name = soname_indx;
6454 name = soname;
6455 }
6456 else
6457 {
6458 size_t indx;
6459
6460 name = lbasename (output_bfd->filename);
6461 def.vd_hash = bfd_elf_hash (name);
6462 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6463 name, FALSE);
6464 if (indx == (size_t) -1)
6465 return FALSE;
6466 defaux.vda_name = indx;
6467 }
6468 defaux.vda_next = 0;
6469
6470 _bfd_elf_swap_verdef_out (output_bfd, &def,
6471 (Elf_External_Verdef *) p);
6472 p += sizeof (Elf_External_Verdef);
6473 if (info->create_default_symver)
6474 {
6475 /* Add a symbol representing this version. */
6476 bh = NULL;
6477 if (! (_bfd_generic_link_add_one_symbol
6478 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6479 0, NULL, FALSE,
6480 get_elf_backend_data (dynobj)->collect, &bh)))
6481 return FALSE;
6482 h = (struct elf_link_hash_entry *) bh;
6483 h->non_elf = 0;
6484 h->def_regular = 1;
6485 h->type = STT_OBJECT;
6486 h->verinfo.vertree = NULL;
6487
6488 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6489 return FALSE;
6490
6491 /* Create a duplicate of the base version with the same
6492 aux block, but different flags. */
6493 def.vd_flags = 0;
6494 def.vd_ndx = 2;
6495 def.vd_aux = sizeof (Elf_External_Verdef);
6496 if (verdefs)
6497 def.vd_next = (sizeof (Elf_External_Verdef)
6498 + sizeof (Elf_External_Verdaux));
6499 else
6500 def.vd_next = 0;
6501 _bfd_elf_swap_verdef_out (output_bfd, &def,
6502 (Elf_External_Verdef *) p);
6503 p += sizeof (Elf_External_Verdef);
6504 }
6505 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6506 (Elf_External_Verdaux *) p);
6507 p += sizeof (Elf_External_Verdaux);
6508
6509 for (t = verdefs; t != NULL; t = t->next)
6510 {
6511 unsigned int cdeps;
6512 struct bfd_elf_version_deps *n;
6513
6514 /* Don't emit the base version twice. */
6515 if (t->vernum == 0)
6516 continue;
6517
6518 cdeps = 0;
6519 for (n = t->deps; n != NULL; n = n->next)
6520 ++cdeps;
6521
6522 /* Add a symbol representing this version. */
6523 bh = NULL;
6524 if (! (_bfd_generic_link_add_one_symbol
6525 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6526 0, NULL, FALSE,
6527 get_elf_backend_data (dynobj)->collect, &bh)))
6528 return FALSE;
6529 h = (struct elf_link_hash_entry *) bh;
6530 h->non_elf = 0;
6531 h->def_regular = 1;
6532 h->type = STT_OBJECT;
6533 h->verinfo.vertree = t;
6534
6535 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6536 return FALSE;
6537
6538 def.vd_version = VER_DEF_CURRENT;
6539 def.vd_flags = 0;
6540 if (t->globals.list == NULL
6541 && t->locals.list == NULL
6542 && ! t->used)
6543 def.vd_flags |= VER_FLG_WEAK;
6544 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6545 def.vd_cnt = cdeps + 1;
6546 def.vd_hash = bfd_elf_hash (t->name);
6547 def.vd_aux = sizeof (Elf_External_Verdef);
6548 def.vd_next = 0;
6549
6550 /* If a basever node is next, it *must* be the last node in
6551 the chain, otherwise Verdef construction breaks. */
6552 if (t->next != NULL && t->next->vernum == 0)
6553 BFD_ASSERT (t->next->next == NULL);
6554
6555 if (t->next != NULL && t->next->vernum != 0)
6556 def.vd_next = (sizeof (Elf_External_Verdef)
6557 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6558
6559 _bfd_elf_swap_verdef_out (output_bfd, &def,
6560 (Elf_External_Verdef *) p);
6561 p += sizeof (Elf_External_Verdef);
6562
6563 defaux.vda_name = h->dynstr_index;
6564 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6565 h->dynstr_index);
6566 defaux.vda_next = 0;
6567 if (t->deps != NULL)
6568 defaux.vda_next = sizeof (Elf_External_Verdaux);
6569 t->name_indx = defaux.vda_name;
6570
6571 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6572 (Elf_External_Verdaux *) p);
6573 p += sizeof (Elf_External_Verdaux);
6574
6575 for (n = t->deps; n != NULL; n = n->next)
6576 {
6577 if (n->version_needed == NULL)
6578 {
6579 /* This can happen if there was an error in the
6580 version script. */
6581 defaux.vda_name = 0;
6582 }
6583 else
6584 {
6585 defaux.vda_name = n->version_needed->name_indx;
6586 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6587 defaux.vda_name);
6588 }
6589 if (n->next == NULL)
6590 defaux.vda_next = 0;
6591 else
6592 defaux.vda_next = sizeof (Elf_External_Verdaux);
6593
6594 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6595 (Elf_External_Verdaux *) p);
6596 p += sizeof (Elf_External_Verdaux);
6597 }
6598 }
6599
6600 elf_tdata (output_bfd)->cverdefs = cdefs;
6601 }
6602 }
6603
6604 bed = get_elf_backend_data (output_bfd);
6605
6606 if (info->gc_sections && bed->can_gc_sections)
6607 {
6608 struct elf_gc_sweep_symbol_info sweep_info;
6609
6610 /* Remove the symbols that were in the swept sections from the
6611 dynamic symbol table. */
6612 sweep_info.info = info;
6613 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6614 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6615 &sweep_info);
6616 }
6617
6618 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6619 {
6620 asection *s;
6621 struct elf_find_verdep_info sinfo;
6622
6623 /* Work out the size of the version reference section. */
6624
6625 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6626 BFD_ASSERT (s != NULL);
6627
6628 sinfo.info = info;
6629 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6630 if (sinfo.vers == 0)
6631 sinfo.vers = 1;
6632 sinfo.failed = FALSE;
6633
6634 elf_link_hash_traverse (elf_hash_table (info),
6635 _bfd_elf_link_find_version_dependencies,
6636 &sinfo);
6637 if (sinfo.failed)
6638 return FALSE;
6639
6640 if (elf_tdata (output_bfd)->verref == NULL)
6641 s->flags |= SEC_EXCLUDE;
6642 else
6643 {
6644 Elf_Internal_Verneed *vn;
6645 unsigned int size;
6646 unsigned int crefs;
6647 bfd_byte *p;
6648
6649 /* Build the version dependency section. */
6650 size = 0;
6651 crefs = 0;
6652 for (vn = elf_tdata (output_bfd)->verref;
6653 vn != NULL;
6654 vn = vn->vn_nextref)
6655 {
6656 Elf_Internal_Vernaux *a;
6657
6658 size += sizeof (Elf_External_Verneed);
6659 ++crefs;
6660 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6661 size += sizeof (Elf_External_Vernaux);
6662 }
6663
6664 s->size = size;
6665 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6666 if (s->contents == NULL)
6667 return FALSE;
6668
6669 p = s->contents;
6670 for (vn = elf_tdata (output_bfd)->verref;
6671 vn != NULL;
6672 vn = vn->vn_nextref)
6673 {
6674 unsigned int caux;
6675 Elf_Internal_Vernaux *a;
6676 size_t indx;
6677
6678 caux = 0;
6679 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6680 ++caux;
6681
6682 vn->vn_version = VER_NEED_CURRENT;
6683 vn->vn_cnt = caux;
6684 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6685 elf_dt_name (vn->vn_bfd) != NULL
6686 ? elf_dt_name (vn->vn_bfd)
6687 : lbasename (vn->vn_bfd->filename),
6688 FALSE);
6689 if (indx == (size_t) -1)
6690 return FALSE;
6691 vn->vn_file = indx;
6692 vn->vn_aux = sizeof (Elf_External_Verneed);
6693 if (vn->vn_nextref == NULL)
6694 vn->vn_next = 0;
6695 else
6696 vn->vn_next = (sizeof (Elf_External_Verneed)
6697 + caux * sizeof (Elf_External_Vernaux));
6698
6699 _bfd_elf_swap_verneed_out (output_bfd, vn,
6700 (Elf_External_Verneed *) p);
6701 p += sizeof (Elf_External_Verneed);
6702
6703 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6704 {
6705 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6706 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6707 a->vna_nodename, FALSE);
6708 if (indx == (size_t) -1)
6709 return FALSE;
6710 a->vna_name = indx;
6711 if (a->vna_nextptr == NULL)
6712 a->vna_next = 0;
6713 else
6714 a->vna_next = sizeof (Elf_External_Vernaux);
6715
6716 _bfd_elf_swap_vernaux_out (output_bfd, a,
6717 (Elf_External_Vernaux *) p);
6718 p += sizeof (Elf_External_Vernaux);
6719 }
6720 }
6721
6722 elf_tdata (output_bfd)->cverrefs = crefs;
6723 }
6724 }
6725
6726 /* Any syms created from now on start with -1 in
6727 got.refcount/offset and plt.refcount/offset. */
6728 elf_hash_table (info)->init_got_refcount
6729 = elf_hash_table (info)->init_got_offset;
6730 elf_hash_table (info)->init_plt_refcount
6731 = elf_hash_table (info)->init_plt_offset;
6732
6733 if (bfd_link_relocatable (info)
6734 && !_bfd_elf_size_group_sections (info))
6735 return FALSE;
6736
6737 /* The backend may have to create some sections regardless of whether
6738 we're dynamic or not. */
6739 if (bed->elf_backend_always_size_sections
6740 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6741 return FALSE;
6742
6743 /* Determine any GNU_STACK segment requirements, after the backend
6744 has had a chance to set a default segment size. */
6745 if (info->execstack)
6746 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6747 else if (info->noexecstack)
6748 elf_stack_flags (output_bfd) = PF_R | PF_W;
6749 else
6750 {
6751 bfd *inputobj;
6752 asection *notesec = NULL;
6753 int exec = 0;
6754
6755 for (inputobj = info->input_bfds;
6756 inputobj;
6757 inputobj = inputobj->link.next)
6758 {
6759 asection *s;
6760
6761 if (inputobj->flags
6762 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6763 continue;
6764 s = inputobj->sections;
6765 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6766 continue;
6767
6768 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6769 if (s)
6770 {
6771 if (s->flags & SEC_CODE)
6772 exec = PF_X;
6773 notesec = s;
6774 }
6775 else if (bed->default_execstack)
6776 exec = PF_X;
6777 }
6778 if (notesec || info->stacksize > 0)
6779 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6780 if (notesec && exec && bfd_link_relocatable (info)
6781 && notesec->output_section != bfd_abs_section_ptr)
6782 notesec->output_section->flags |= SEC_CODE;
6783 }
6784
6785 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6786 {
6787 struct elf_info_failed eif;
6788 struct elf_link_hash_entry *h;
6789 asection *dynstr;
6790 asection *s;
6791
6792 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6793 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6794
6795 if (info->symbolic)
6796 {
6797 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6798 return FALSE;
6799 info->flags |= DF_SYMBOLIC;
6800 }
6801
6802 if (rpath != NULL)
6803 {
6804 size_t indx;
6805 bfd_vma tag;
6806
6807 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6808 TRUE);
6809 if (indx == (size_t) -1)
6810 return FALSE;
6811
6812 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6813 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6814 return FALSE;
6815 }
6816
6817 if (filter_shlib != NULL)
6818 {
6819 size_t indx;
6820
6821 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6822 filter_shlib, TRUE);
6823 if (indx == (size_t) -1
6824 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6825 return FALSE;
6826 }
6827
6828 if (auxiliary_filters != NULL)
6829 {
6830 const char * const *p;
6831
6832 for (p = auxiliary_filters; *p != NULL; p++)
6833 {
6834 size_t indx;
6835
6836 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6837 *p, TRUE);
6838 if (indx == (size_t) -1
6839 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6840 return FALSE;
6841 }
6842 }
6843
6844 if (audit != NULL)
6845 {
6846 size_t indx;
6847
6848 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6849 TRUE);
6850 if (indx == (size_t) -1
6851 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6852 return FALSE;
6853 }
6854
6855 if (depaudit != NULL)
6856 {
6857 size_t indx;
6858
6859 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6860 TRUE);
6861 if (indx == (size_t) -1
6862 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6863 return FALSE;
6864 }
6865
6866 eif.info = info;
6867 eif.failed = FALSE;
6868
6869 /* Find all symbols which were defined in a dynamic object and make
6870 the backend pick a reasonable value for them. */
6871 elf_link_hash_traverse (elf_hash_table (info),
6872 _bfd_elf_adjust_dynamic_symbol,
6873 &eif);
6874 if (eif.failed)
6875 return FALSE;
6876
6877 /* Add some entries to the .dynamic section. We fill in some of the
6878 values later, in bfd_elf_final_link, but we must add the entries
6879 now so that we know the final size of the .dynamic section. */
6880
6881 /* If there are initialization and/or finalization functions to
6882 call then add the corresponding DT_INIT/DT_FINI entries. */
6883 h = (info->init_function
6884 ? elf_link_hash_lookup (elf_hash_table (info),
6885 info->init_function, FALSE,
6886 FALSE, FALSE)
6887 : NULL);
6888 if (h != NULL
6889 && (h->ref_regular
6890 || h->def_regular))
6891 {
6892 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6893 return FALSE;
6894 }
6895 h = (info->fini_function
6896 ? elf_link_hash_lookup (elf_hash_table (info),
6897 info->fini_function, FALSE,
6898 FALSE, FALSE)
6899 : NULL);
6900 if (h != NULL
6901 && (h->ref_regular
6902 || h->def_regular))
6903 {
6904 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6905 return FALSE;
6906 }
6907
6908 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6909 if (s != NULL && s->linker_has_input)
6910 {
6911 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6912 if (! bfd_link_executable (info))
6913 {
6914 bfd *sub;
6915 asection *o;
6916
6917 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6918 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
6919 && (o = sub->sections) != NULL
6920 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
6921 for (o = sub->sections; o != NULL; o = o->next)
6922 if (elf_section_data (o)->this_hdr.sh_type
6923 == SHT_PREINIT_ARRAY)
6924 {
6925 _bfd_error_handler
6926 (_("%pB: .preinit_array section is not allowed in DSO"),
6927 sub);
6928 break;
6929 }
6930
6931 bfd_set_error (bfd_error_nonrepresentable_section);
6932 return FALSE;
6933 }
6934
6935 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6936 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6937 return FALSE;
6938 }
6939 s = bfd_get_section_by_name (output_bfd, ".init_array");
6940 if (s != NULL && s->linker_has_input)
6941 {
6942 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6943 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6944 return FALSE;
6945 }
6946 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6947 if (s != NULL && s->linker_has_input)
6948 {
6949 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6950 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6951 return FALSE;
6952 }
6953
6954 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6955 /* If .dynstr is excluded from the link, we don't want any of
6956 these tags. Strictly, we should be checking each section
6957 individually; This quick check covers for the case where
6958 someone does a /DISCARD/ : { *(*) }. */
6959 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6960 {
6961 bfd_size_type strsize;
6962
6963 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6964 if ((info->emit_hash
6965 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6966 || (info->emit_gnu_hash
6967 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6968 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6969 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6970 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6971 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6972 bed->s->sizeof_sym))
6973 return FALSE;
6974 }
6975 }
6976
6977 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6978 return FALSE;
6979
6980 /* The backend must work out the sizes of all the other dynamic
6981 sections. */
6982 if (dynobj != NULL
6983 && bed->elf_backend_size_dynamic_sections != NULL
6984 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6985 return FALSE;
6986
6987 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6988 {
6989 if (elf_tdata (output_bfd)->cverdefs)
6990 {
6991 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
6992
6993 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6994 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
6995 return FALSE;
6996 }
6997
6998 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6999 {
7000 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7001 return FALSE;
7002 }
7003 else if (info->flags & DF_BIND_NOW)
7004 {
7005 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7006 return FALSE;
7007 }
7008
7009 if (info->flags_1)
7010 {
7011 if (bfd_link_executable (info))
7012 info->flags_1 &= ~ (DF_1_INITFIRST
7013 | DF_1_NODELETE
7014 | DF_1_NOOPEN);
7015 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7016 return FALSE;
7017 }
7018
7019 if (elf_tdata (output_bfd)->cverrefs)
7020 {
7021 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7022
7023 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7024 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7025 return FALSE;
7026 }
7027
7028 if ((elf_tdata (output_bfd)->cverrefs == 0
7029 && elf_tdata (output_bfd)->cverdefs == 0)
7030 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7031 {
7032 asection *s;
7033
7034 s = bfd_get_linker_section (dynobj, ".gnu.version");
7035 s->flags |= SEC_EXCLUDE;
7036 }
7037 }
7038 return TRUE;
7039 }
7040
7041 /* Find the first non-excluded output section. We'll use its
7042 section symbol for some emitted relocs. */
7043 void
7044 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7045 {
7046 asection *s;
7047
7048 for (s = output_bfd->sections; s != NULL; s = s->next)
7049 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7050 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7051 {
7052 elf_hash_table (info)->text_index_section = s;
7053 break;
7054 }
7055 }
7056
7057 /* Find two non-excluded output sections, one for code, one for data.
7058 We'll use their section symbols for some emitted relocs. */
7059 void
7060 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7061 {
7062 asection *s;
7063
7064 /* Data first, since setting text_index_section changes
7065 _bfd_elf_omit_section_dynsym_default. */
7066 for (s = output_bfd->sections; s != NULL; s = s->next)
7067 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
7068 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7069 {
7070 elf_hash_table (info)->data_index_section = s;
7071 break;
7072 }
7073
7074 for (s = output_bfd->sections; s != NULL; s = s->next)
7075 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
7076 == (SEC_ALLOC | SEC_READONLY))
7077 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7078 {
7079 elf_hash_table (info)->text_index_section = s;
7080 break;
7081 }
7082
7083 if (elf_hash_table (info)->text_index_section == NULL)
7084 elf_hash_table (info)->text_index_section
7085 = elf_hash_table (info)->data_index_section;
7086 }
7087
7088 bfd_boolean
7089 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7090 {
7091 const struct elf_backend_data *bed;
7092 unsigned long section_sym_count;
7093 bfd_size_type dynsymcount = 0;
7094
7095 if (!is_elf_hash_table (info->hash))
7096 return TRUE;
7097
7098 bed = get_elf_backend_data (output_bfd);
7099 (*bed->elf_backend_init_index_section) (output_bfd, info);
7100
7101 /* Assign dynsym indices. In a shared library we generate a section
7102 symbol for each output section, which come first. Next come all
7103 of the back-end allocated local dynamic syms, followed by the rest
7104 of the global symbols.
7105
7106 This is usually not needed for static binaries, however backends
7107 can request to always do it, e.g. the MIPS backend uses dynamic
7108 symbol counts to lay out GOT, which will be produced in the
7109 presence of GOT relocations even in static binaries (holding fixed
7110 data in that case, to satisfy those relocations). */
7111
7112 if (elf_hash_table (info)->dynamic_sections_created
7113 || bed->always_renumber_dynsyms)
7114 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7115 &section_sym_count);
7116
7117 if (elf_hash_table (info)->dynamic_sections_created)
7118 {
7119 bfd *dynobj;
7120 asection *s;
7121 unsigned int dtagcount;
7122
7123 dynobj = elf_hash_table (info)->dynobj;
7124
7125 /* Work out the size of the symbol version section. */
7126 s = bfd_get_linker_section (dynobj, ".gnu.version");
7127 BFD_ASSERT (s != NULL);
7128 if ((s->flags & SEC_EXCLUDE) == 0)
7129 {
7130 s->size = dynsymcount * sizeof (Elf_External_Versym);
7131 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7132 if (s->contents == NULL)
7133 return FALSE;
7134
7135 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7136 return FALSE;
7137 }
7138
7139 /* Set the size of the .dynsym and .hash sections. We counted
7140 the number of dynamic symbols in elf_link_add_object_symbols.
7141 We will build the contents of .dynsym and .hash when we build
7142 the final symbol table, because until then we do not know the
7143 correct value to give the symbols. We built the .dynstr
7144 section as we went along in elf_link_add_object_symbols. */
7145 s = elf_hash_table (info)->dynsym;
7146 BFD_ASSERT (s != NULL);
7147 s->size = dynsymcount * bed->s->sizeof_sym;
7148
7149 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7150 if (s->contents == NULL)
7151 return FALSE;
7152
7153 /* The first entry in .dynsym is a dummy symbol. Clear all the
7154 section syms, in case we don't output them all. */
7155 ++section_sym_count;
7156 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7157
7158 elf_hash_table (info)->bucketcount = 0;
7159
7160 /* Compute the size of the hashing table. As a side effect this
7161 computes the hash values for all the names we export. */
7162 if (info->emit_hash)
7163 {
7164 unsigned long int *hashcodes;
7165 struct hash_codes_info hashinf;
7166 bfd_size_type amt;
7167 unsigned long int nsyms;
7168 size_t bucketcount;
7169 size_t hash_entry_size;
7170
7171 /* Compute the hash values for all exported symbols. At the same
7172 time store the values in an array so that we could use them for
7173 optimizations. */
7174 amt = dynsymcount * sizeof (unsigned long int);
7175 hashcodes = (unsigned long int *) bfd_malloc (amt);
7176 if (hashcodes == NULL)
7177 return FALSE;
7178 hashinf.hashcodes = hashcodes;
7179 hashinf.error = FALSE;
7180
7181 /* Put all hash values in HASHCODES. */
7182 elf_link_hash_traverse (elf_hash_table (info),
7183 elf_collect_hash_codes, &hashinf);
7184 if (hashinf.error)
7185 {
7186 free (hashcodes);
7187 return FALSE;
7188 }
7189
7190 nsyms = hashinf.hashcodes - hashcodes;
7191 bucketcount
7192 = compute_bucket_count (info, hashcodes, nsyms, 0);
7193 free (hashcodes);
7194
7195 if (bucketcount == 0 && nsyms > 0)
7196 return FALSE;
7197
7198 elf_hash_table (info)->bucketcount = bucketcount;
7199
7200 s = bfd_get_linker_section (dynobj, ".hash");
7201 BFD_ASSERT (s != NULL);
7202 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7203 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7204 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7205 if (s->contents == NULL)
7206 return FALSE;
7207
7208 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7209 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7210 s->contents + hash_entry_size);
7211 }
7212
7213 if (info->emit_gnu_hash)
7214 {
7215 size_t i, cnt;
7216 unsigned char *contents;
7217 struct collect_gnu_hash_codes cinfo;
7218 bfd_size_type amt;
7219 size_t bucketcount;
7220
7221 memset (&cinfo, 0, sizeof (cinfo));
7222
7223 /* Compute the hash values for all exported symbols. At the same
7224 time store the values in an array so that we could use them for
7225 optimizations. */
7226 amt = dynsymcount * 2 * sizeof (unsigned long int);
7227 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7228 if (cinfo.hashcodes == NULL)
7229 return FALSE;
7230
7231 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7232 cinfo.min_dynindx = -1;
7233 cinfo.output_bfd = output_bfd;
7234 cinfo.bed = bed;
7235
7236 /* Put all hash values in HASHCODES. */
7237 elf_link_hash_traverse (elf_hash_table (info),
7238 elf_collect_gnu_hash_codes, &cinfo);
7239 if (cinfo.error)
7240 {
7241 free (cinfo.hashcodes);
7242 return FALSE;
7243 }
7244
7245 bucketcount
7246 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7247
7248 if (bucketcount == 0)
7249 {
7250 free (cinfo.hashcodes);
7251 return FALSE;
7252 }
7253
7254 s = bfd_get_linker_section (dynobj, ".gnu.hash");
7255 BFD_ASSERT (s != NULL);
7256
7257 if (cinfo.nsyms == 0)
7258 {
7259 /* Empty .gnu.hash section is special. */
7260 BFD_ASSERT (cinfo.min_dynindx == -1);
7261 free (cinfo.hashcodes);
7262 s->size = 5 * 4 + bed->s->arch_size / 8;
7263 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7264 if (contents == NULL)
7265 return FALSE;
7266 s->contents = contents;
7267 /* 1 empty bucket. */
7268 bfd_put_32 (output_bfd, 1, contents);
7269 /* SYMIDX above the special symbol 0. */
7270 bfd_put_32 (output_bfd, 1, contents + 4);
7271 /* Just one word for bitmask. */
7272 bfd_put_32 (output_bfd, 1, contents + 8);
7273 /* Only hash fn bloom filter. */
7274 bfd_put_32 (output_bfd, 0, contents + 12);
7275 /* No hashes are valid - empty bitmask. */
7276 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7277 /* No hashes in the only bucket. */
7278 bfd_put_32 (output_bfd, 0,
7279 contents + 16 + bed->s->arch_size / 8);
7280 }
7281 else
7282 {
7283 unsigned long int maskwords, maskbitslog2, x;
7284 BFD_ASSERT (cinfo.min_dynindx != -1);
7285
7286 x = cinfo.nsyms;
7287 maskbitslog2 = 1;
7288 while ((x >>= 1) != 0)
7289 ++maskbitslog2;
7290 if (maskbitslog2 < 3)
7291 maskbitslog2 = 5;
7292 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7293 maskbitslog2 = maskbitslog2 + 3;
7294 else
7295 maskbitslog2 = maskbitslog2 + 2;
7296 if (bed->s->arch_size == 64)
7297 {
7298 if (maskbitslog2 == 5)
7299 maskbitslog2 = 6;
7300 cinfo.shift1 = 6;
7301 }
7302 else
7303 cinfo.shift1 = 5;
7304 cinfo.mask = (1 << cinfo.shift1) - 1;
7305 cinfo.shift2 = maskbitslog2;
7306 cinfo.maskbits = 1 << maskbitslog2;
7307 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7308 amt = bucketcount * sizeof (unsigned long int) * 2;
7309 amt += maskwords * sizeof (bfd_vma);
7310 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7311 if (cinfo.bitmask == NULL)
7312 {
7313 free (cinfo.hashcodes);
7314 return FALSE;
7315 }
7316
7317 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7318 cinfo.indx = cinfo.counts + bucketcount;
7319 cinfo.symindx = dynsymcount - cinfo.nsyms;
7320 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7321
7322 /* Determine how often each hash bucket is used. */
7323 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7324 for (i = 0; i < cinfo.nsyms; ++i)
7325 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7326
7327 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7328 if (cinfo.counts[i] != 0)
7329 {
7330 cinfo.indx[i] = cnt;
7331 cnt += cinfo.counts[i];
7332 }
7333 BFD_ASSERT (cnt == dynsymcount);
7334 cinfo.bucketcount = bucketcount;
7335 cinfo.local_indx = cinfo.min_dynindx;
7336
7337 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7338 s->size += cinfo.maskbits / 8;
7339 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7340 if (contents == NULL)
7341 {
7342 free (cinfo.bitmask);
7343 free (cinfo.hashcodes);
7344 return FALSE;
7345 }
7346
7347 s->contents = contents;
7348 bfd_put_32 (output_bfd, bucketcount, contents);
7349 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7350 bfd_put_32 (output_bfd, maskwords, contents + 8);
7351 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7352 contents += 16 + cinfo.maskbits / 8;
7353
7354 for (i = 0; i < bucketcount; ++i)
7355 {
7356 if (cinfo.counts[i] == 0)
7357 bfd_put_32 (output_bfd, 0, contents);
7358 else
7359 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7360 contents += 4;
7361 }
7362
7363 cinfo.contents = contents;
7364
7365 /* Renumber dynamic symbols, populate .gnu.hash section. */
7366 elf_link_hash_traverse (elf_hash_table (info),
7367 elf_renumber_gnu_hash_syms, &cinfo);
7368
7369 contents = s->contents + 16;
7370 for (i = 0; i < maskwords; ++i)
7371 {
7372 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7373 contents);
7374 contents += bed->s->arch_size / 8;
7375 }
7376
7377 free (cinfo.bitmask);
7378 free (cinfo.hashcodes);
7379 }
7380 }
7381
7382 s = bfd_get_linker_section (dynobj, ".dynstr");
7383 BFD_ASSERT (s != NULL);
7384
7385 elf_finalize_dynstr (output_bfd, info);
7386
7387 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7388
7389 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7390 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7391 return FALSE;
7392 }
7393
7394 return TRUE;
7395 }
7396 \f
7397 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7398
7399 static void
7400 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7401 asection *sec)
7402 {
7403 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7404 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7405 }
7406
7407 /* Finish SHF_MERGE section merging. */
7408
7409 bfd_boolean
7410 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7411 {
7412 bfd *ibfd;
7413 asection *sec;
7414
7415 if (!is_elf_hash_table (info->hash))
7416 return FALSE;
7417
7418 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7419 if ((ibfd->flags & DYNAMIC) == 0
7420 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7421 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7422 == get_elf_backend_data (obfd)->s->elfclass))
7423 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7424 if ((sec->flags & SEC_MERGE) != 0
7425 && !bfd_is_abs_section (sec->output_section))
7426 {
7427 struct bfd_elf_section_data *secdata;
7428
7429 secdata = elf_section_data (sec);
7430 if (! _bfd_add_merge_section (obfd,
7431 &elf_hash_table (info)->merge_info,
7432 sec, &secdata->sec_info))
7433 return FALSE;
7434 else if (secdata->sec_info)
7435 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7436 }
7437
7438 if (elf_hash_table (info)->merge_info != NULL)
7439 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7440 merge_sections_remove_hook);
7441 return TRUE;
7442 }
7443
7444 /* Create an entry in an ELF linker hash table. */
7445
7446 struct bfd_hash_entry *
7447 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7448 struct bfd_hash_table *table,
7449 const char *string)
7450 {
7451 /* Allocate the structure if it has not already been allocated by a
7452 subclass. */
7453 if (entry == NULL)
7454 {
7455 entry = (struct bfd_hash_entry *)
7456 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7457 if (entry == NULL)
7458 return entry;
7459 }
7460
7461 /* Call the allocation method of the superclass. */
7462 entry = _bfd_link_hash_newfunc (entry, table, string);
7463 if (entry != NULL)
7464 {
7465 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7466 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7467
7468 /* Set local fields. */
7469 ret->indx = -1;
7470 ret->dynindx = -1;
7471 ret->got = htab->init_got_refcount;
7472 ret->plt = htab->init_plt_refcount;
7473 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7474 - offsetof (struct elf_link_hash_entry, size)));
7475 /* Assume that we have been called by a non-ELF symbol reader.
7476 This flag is then reset by the code which reads an ELF input
7477 file. This ensures that a symbol created by a non-ELF symbol
7478 reader will have the flag set correctly. */
7479 ret->non_elf = 1;
7480 }
7481
7482 return entry;
7483 }
7484
7485 /* Copy data from an indirect symbol to its direct symbol, hiding the
7486 old indirect symbol. Also used for copying flags to a weakdef. */
7487
7488 void
7489 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7490 struct elf_link_hash_entry *dir,
7491 struct elf_link_hash_entry *ind)
7492 {
7493 struct elf_link_hash_table *htab;
7494
7495 /* Copy down any references that we may have already seen to the
7496 symbol which just became indirect. */
7497
7498 if (dir->versioned != versioned_hidden)
7499 dir->ref_dynamic |= ind->ref_dynamic;
7500 dir->ref_regular |= ind->ref_regular;
7501 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7502 dir->non_got_ref |= ind->non_got_ref;
7503 dir->needs_plt |= ind->needs_plt;
7504 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7505
7506 if (ind->root.type != bfd_link_hash_indirect)
7507 return;
7508
7509 /* Copy over the global and procedure linkage table refcount entries.
7510 These may have been already set up by a check_relocs routine. */
7511 htab = elf_hash_table (info);
7512 if (ind->got.refcount > htab->init_got_refcount.refcount)
7513 {
7514 if (dir->got.refcount < 0)
7515 dir->got.refcount = 0;
7516 dir->got.refcount += ind->got.refcount;
7517 ind->got.refcount = htab->init_got_refcount.refcount;
7518 }
7519
7520 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7521 {
7522 if (dir->plt.refcount < 0)
7523 dir->plt.refcount = 0;
7524 dir->plt.refcount += ind->plt.refcount;
7525 ind->plt.refcount = htab->init_plt_refcount.refcount;
7526 }
7527
7528 if (ind->dynindx != -1)
7529 {
7530 if (dir->dynindx != -1)
7531 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7532 dir->dynindx = ind->dynindx;
7533 dir->dynstr_index = ind->dynstr_index;
7534 ind->dynindx = -1;
7535 ind->dynstr_index = 0;
7536 }
7537 }
7538
7539 void
7540 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7541 struct elf_link_hash_entry *h,
7542 bfd_boolean force_local)
7543 {
7544 /* STT_GNU_IFUNC symbol must go through PLT. */
7545 if (h->type != STT_GNU_IFUNC)
7546 {
7547 h->plt = elf_hash_table (info)->init_plt_offset;
7548 h->needs_plt = 0;
7549 }
7550 if (force_local)
7551 {
7552 h->forced_local = 1;
7553 if (h->dynindx != -1)
7554 {
7555 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7556 h->dynstr_index);
7557 h->dynindx = -1;
7558 h->dynstr_index = 0;
7559 }
7560 }
7561 }
7562
7563 /* Hide a symbol. */
7564
7565 void
7566 _bfd_elf_link_hide_symbol (bfd *output_bfd,
7567 struct bfd_link_info *info,
7568 struct bfd_link_hash_entry *h)
7569 {
7570 if (is_elf_hash_table (info->hash))
7571 {
7572 const struct elf_backend_data *bed
7573 = get_elf_backend_data (output_bfd);
7574 struct elf_link_hash_entry *eh
7575 = (struct elf_link_hash_entry *) h;
7576 bed->elf_backend_hide_symbol (info, eh, TRUE);
7577 eh->def_dynamic = 0;
7578 eh->ref_dynamic = 0;
7579 eh->dynamic_def = 0;
7580 }
7581 }
7582
7583 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7584 caller. */
7585
7586 bfd_boolean
7587 _bfd_elf_link_hash_table_init
7588 (struct elf_link_hash_table *table,
7589 bfd *abfd,
7590 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7591 struct bfd_hash_table *,
7592 const char *),
7593 unsigned int entsize,
7594 enum elf_target_id target_id)
7595 {
7596 bfd_boolean ret;
7597 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7598
7599 table->init_got_refcount.refcount = can_refcount - 1;
7600 table->init_plt_refcount.refcount = can_refcount - 1;
7601 table->init_got_offset.offset = -(bfd_vma) 1;
7602 table->init_plt_offset.offset = -(bfd_vma) 1;
7603 /* The first dynamic symbol is a dummy. */
7604 table->dynsymcount = 1;
7605
7606 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7607
7608 table->root.type = bfd_link_elf_hash_table;
7609 table->hash_table_id = target_id;
7610
7611 return ret;
7612 }
7613
7614 /* Create an ELF linker hash table. */
7615
7616 struct bfd_link_hash_table *
7617 _bfd_elf_link_hash_table_create (bfd *abfd)
7618 {
7619 struct elf_link_hash_table *ret;
7620 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7621
7622 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7623 if (ret == NULL)
7624 return NULL;
7625
7626 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7627 sizeof (struct elf_link_hash_entry),
7628 GENERIC_ELF_DATA))
7629 {
7630 free (ret);
7631 return NULL;
7632 }
7633 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7634
7635 return &ret->root;
7636 }
7637
7638 /* Destroy an ELF linker hash table. */
7639
7640 void
7641 _bfd_elf_link_hash_table_free (bfd *obfd)
7642 {
7643 struct elf_link_hash_table *htab;
7644
7645 htab = (struct elf_link_hash_table *) obfd->link.hash;
7646 if (htab->dynstr != NULL)
7647 _bfd_elf_strtab_free (htab->dynstr);
7648 _bfd_merge_sections_free (htab->merge_info);
7649 _bfd_generic_link_hash_table_free (obfd);
7650 }
7651
7652 /* This is a hook for the ELF emulation code in the generic linker to
7653 tell the backend linker what file name to use for the DT_NEEDED
7654 entry for a dynamic object. */
7655
7656 void
7657 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7658 {
7659 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7660 && bfd_get_format (abfd) == bfd_object)
7661 elf_dt_name (abfd) = name;
7662 }
7663
7664 int
7665 bfd_elf_get_dyn_lib_class (bfd *abfd)
7666 {
7667 int lib_class;
7668 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7669 && bfd_get_format (abfd) == bfd_object)
7670 lib_class = elf_dyn_lib_class (abfd);
7671 else
7672 lib_class = 0;
7673 return lib_class;
7674 }
7675
7676 void
7677 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7678 {
7679 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7680 && bfd_get_format (abfd) == bfd_object)
7681 elf_dyn_lib_class (abfd) = lib_class;
7682 }
7683
7684 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7685 the linker ELF emulation code. */
7686
7687 struct bfd_link_needed_list *
7688 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7689 struct bfd_link_info *info)
7690 {
7691 if (! is_elf_hash_table (info->hash))
7692 return NULL;
7693 return elf_hash_table (info)->needed;
7694 }
7695
7696 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7697 hook for the linker ELF emulation code. */
7698
7699 struct bfd_link_needed_list *
7700 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7701 struct bfd_link_info *info)
7702 {
7703 if (! is_elf_hash_table (info->hash))
7704 return NULL;
7705 return elf_hash_table (info)->runpath;
7706 }
7707
7708 /* Get the name actually used for a dynamic object for a link. This
7709 is the SONAME entry if there is one. Otherwise, it is the string
7710 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7711
7712 const char *
7713 bfd_elf_get_dt_soname (bfd *abfd)
7714 {
7715 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7716 && bfd_get_format (abfd) == bfd_object)
7717 return elf_dt_name (abfd);
7718 return NULL;
7719 }
7720
7721 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7722 the ELF linker emulation code. */
7723
7724 bfd_boolean
7725 bfd_elf_get_bfd_needed_list (bfd *abfd,
7726 struct bfd_link_needed_list **pneeded)
7727 {
7728 asection *s;
7729 bfd_byte *dynbuf = NULL;
7730 unsigned int elfsec;
7731 unsigned long shlink;
7732 bfd_byte *extdyn, *extdynend;
7733 size_t extdynsize;
7734 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7735
7736 *pneeded = NULL;
7737
7738 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7739 || bfd_get_format (abfd) != bfd_object)
7740 return TRUE;
7741
7742 s = bfd_get_section_by_name (abfd, ".dynamic");
7743 if (s == NULL || s->size == 0)
7744 return TRUE;
7745
7746 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7747 goto error_return;
7748
7749 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7750 if (elfsec == SHN_BAD)
7751 goto error_return;
7752
7753 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7754
7755 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7756 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7757
7758 extdyn = dynbuf;
7759 extdynend = extdyn + s->size;
7760 for (; extdyn < extdynend; extdyn += extdynsize)
7761 {
7762 Elf_Internal_Dyn dyn;
7763
7764 (*swap_dyn_in) (abfd, extdyn, &dyn);
7765
7766 if (dyn.d_tag == DT_NULL)
7767 break;
7768
7769 if (dyn.d_tag == DT_NEEDED)
7770 {
7771 const char *string;
7772 struct bfd_link_needed_list *l;
7773 unsigned int tagv = dyn.d_un.d_val;
7774 bfd_size_type amt;
7775
7776 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7777 if (string == NULL)
7778 goto error_return;
7779
7780 amt = sizeof *l;
7781 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7782 if (l == NULL)
7783 goto error_return;
7784
7785 l->by = abfd;
7786 l->name = string;
7787 l->next = *pneeded;
7788 *pneeded = l;
7789 }
7790 }
7791
7792 free (dynbuf);
7793
7794 return TRUE;
7795
7796 error_return:
7797 if (dynbuf != NULL)
7798 free (dynbuf);
7799 return FALSE;
7800 }
7801
7802 struct elf_symbuf_symbol
7803 {
7804 unsigned long st_name; /* Symbol name, index in string tbl */
7805 unsigned char st_info; /* Type and binding attributes */
7806 unsigned char st_other; /* Visibilty, and target specific */
7807 };
7808
7809 struct elf_symbuf_head
7810 {
7811 struct elf_symbuf_symbol *ssym;
7812 size_t count;
7813 unsigned int st_shndx;
7814 };
7815
7816 struct elf_symbol
7817 {
7818 union
7819 {
7820 Elf_Internal_Sym *isym;
7821 struct elf_symbuf_symbol *ssym;
7822 } u;
7823 const char *name;
7824 };
7825
7826 /* Sort references to symbols by ascending section number. */
7827
7828 static int
7829 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7830 {
7831 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7832 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7833
7834 return s1->st_shndx - s2->st_shndx;
7835 }
7836
7837 static int
7838 elf_sym_name_compare (const void *arg1, const void *arg2)
7839 {
7840 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7841 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7842 return strcmp (s1->name, s2->name);
7843 }
7844
7845 static struct elf_symbuf_head *
7846 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
7847 {
7848 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7849 struct elf_symbuf_symbol *ssym;
7850 struct elf_symbuf_head *ssymbuf, *ssymhead;
7851 size_t i, shndx_count, total_size;
7852
7853 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7854 if (indbuf == NULL)
7855 return NULL;
7856
7857 for (ind = indbuf, i = 0; i < symcount; i++)
7858 if (isymbuf[i].st_shndx != SHN_UNDEF)
7859 *ind++ = &isymbuf[i];
7860 indbufend = ind;
7861
7862 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7863 elf_sort_elf_symbol);
7864
7865 shndx_count = 0;
7866 if (indbufend > indbuf)
7867 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7868 if (ind[0]->st_shndx != ind[1]->st_shndx)
7869 shndx_count++;
7870
7871 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7872 + (indbufend - indbuf) * sizeof (*ssym));
7873 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7874 if (ssymbuf == NULL)
7875 {
7876 free (indbuf);
7877 return NULL;
7878 }
7879
7880 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7881 ssymbuf->ssym = NULL;
7882 ssymbuf->count = shndx_count;
7883 ssymbuf->st_shndx = 0;
7884 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7885 {
7886 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7887 {
7888 ssymhead++;
7889 ssymhead->ssym = ssym;
7890 ssymhead->count = 0;
7891 ssymhead->st_shndx = (*ind)->st_shndx;
7892 }
7893 ssym->st_name = (*ind)->st_name;
7894 ssym->st_info = (*ind)->st_info;
7895 ssym->st_other = (*ind)->st_other;
7896 ssymhead->count++;
7897 }
7898 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
7899 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7900 == total_size));
7901
7902 free (indbuf);
7903 return ssymbuf;
7904 }
7905
7906 /* Check if 2 sections define the same set of local and global
7907 symbols. */
7908
7909 static bfd_boolean
7910 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7911 struct bfd_link_info *info)
7912 {
7913 bfd *bfd1, *bfd2;
7914 const struct elf_backend_data *bed1, *bed2;
7915 Elf_Internal_Shdr *hdr1, *hdr2;
7916 size_t symcount1, symcount2;
7917 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7918 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7919 Elf_Internal_Sym *isym, *isymend;
7920 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7921 size_t count1, count2, i;
7922 unsigned int shndx1, shndx2;
7923 bfd_boolean result;
7924
7925 bfd1 = sec1->owner;
7926 bfd2 = sec2->owner;
7927
7928 /* Both sections have to be in ELF. */
7929 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7930 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7931 return FALSE;
7932
7933 if (elf_section_type (sec1) != elf_section_type (sec2))
7934 return FALSE;
7935
7936 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7937 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7938 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7939 return FALSE;
7940
7941 bed1 = get_elf_backend_data (bfd1);
7942 bed2 = get_elf_backend_data (bfd2);
7943 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7944 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7945 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7946 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7947
7948 if (symcount1 == 0 || symcount2 == 0)
7949 return FALSE;
7950
7951 result = FALSE;
7952 isymbuf1 = NULL;
7953 isymbuf2 = NULL;
7954 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7955 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7956
7957 if (ssymbuf1 == NULL)
7958 {
7959 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7960 NULL, NULL, NULL);
7961 if (isymbuf1 == NULL)
7962 goto done;
7963
7964 if (!info->reduce_memory_overheads)
7965 elf_tdata (bfd1)->symbuf = ssymbuf1
7966 = elf_create_symbuf (symcount1, isymbuf1);
7967 }
7968
7969 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7970 {
7971 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7972 NULL, NULL, NULL);
7973 if (isymbuf2 == NULL)
7974 goto done;
7975
7976 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7977 elf_tdata (bfd2)->symbuf = ssymbuf2
7978 = elf_create_symbuf (symcount2, isymbuf2);
7979 }
7980
7981 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7982 {
7983 /* Optimized faster version. */
7984 size_t lo, hi, mid;
7985 struct elf_symbol *symp;
7986 struct elf_symbuf_symbol *ssym, *ssymend;
7987
7988 lo = 0;
7989 hi = ssymbuf1->count;
7990 ssymbuf1++;
7991 count1 = 0;
7992 while (lo < hi)
7993 {
7994 mid = (lo + hi) / 2;
7995 if (shndx1 < ssymbuf1[mid].st_shndx)
7996 hi = mid;
7997 else if (shndx1 > ssymbuf1[mid].st_shndx)
7998 lo = mid + 1;
7999 else
8000 {
8001 count1 = ssymbuf1[mid].count;
8002 ssymbuf1 += mid;
8003 break;
8004 }
8005 }
8006
8007 lo = 0;
8008 hi = ssymbuf2->count;
8009 ssymbuf2++;
8010 count2 = 0;
8011 while (lo < hi)
8012 {
8013 mid = (lo + hi) / 2;
8014 if (shndx2 < ssymbuf2[mid].st_shndx)
8015 hi = mid;
8016 else if (shndx2 > ssymbuf2[mid].st_shndx)
8017 lo = mid + 1;
8018 else
8019 {
8020 count2 = ssymbuf2[mid].count;
8021 ssymbuf2 += mid;
8022 break;
8023 }
8024 }
8025
8026 if (count1 == 0 || count2 == 0 || count1 != count2)
8027 goto done;
8028
8029 symtable1
8030 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8031 symtable2
8032 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8033 if (symtable1 == NULL || symtable2 == NULL)
8034 goto done;
8035
8036 symp = symtable1;
8037 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
8038 ssym < ssymend; ssym++, symp++)
8039 {
8040 symp->u.ssym = ssym;
8041 symp->name = bfd_elf_string_from_elf_section (bfd1,
8042 hdr1->sh_link,
8043 ssym->st_name);
8044 }
8045
8046 symp = symtable2;
8047 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8048 ssym < ssymend; ssym++, symp++)
8049 {
8050 symp->u.ssym = ssym;
8051 symp->name = bfd_elf_string_from_elf_section (bfd2,
8052 hdr2->sh_link,
8053 ssym->st_name);
8054 }
8055
8056 /* Sort symbol by name. */
8057 qsort (symtable1, count1, sizeof (struct elf_symbol),
8058 elf_sym_name_compare);
8059 qsort (symtable2, count1, sizeof (struct elf_symbol),
8060 elf_sym_name_compare);
8061
8062 for (i = 0; i < count1; i++)
8063 /* Two symbols must have the same binding, type and name. */
8064 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8065 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8066 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8067 goto done;
8068
8069 result = TRUE;
8070 goto done;
8071 }
8072
8073 symtable1 = (struct elf_symbol *)
8074 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8075 symtable2 = (struct elf_symbol *)
8076 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8077 if (symtable1 == NULL || symtable2 == NULL)
8078 goto done;
8079
8080 /* Count definitions in the section. */
8081 count1 = 0;
8082 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8083 if (isym->st_shndx == shndx1)
8084 symtable1[count1++].u.isym = isym;
8085
8086 count2 = 0;
8087 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8088 if (isym->st_shndx == shndx2)
8089 symtable2[count2++].u.isym = isym;
8090
8091 if (count1 == 0 || count2 == 0 || count1 != count2)
8092 goto done;
8093
8094 for (i = 0; i < count1; i++)
8095 symtable1[i].name
8096 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8097 symtable1[i].u.isym->st_name);
8098
8099 for (i = 0; i < count2; i++)
8100 symtable2[i].name
8101 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8102 symtable2[i].u.isym->st_name);
8103
8104 /* Sort symbol by name. */
8105 qsort (symtable1, count1, sizeof (struct elf_symbol),
8106 elf_sym_name_compare);
8107 qsort (symtable2, count1, sizeof (struct elf_symbol),
8108 elf_sym_name_compare);
8109
8110 for (i = 0; i < count1; i++)
8111 /* Two symbols must have the same binding, type and name. */
8112 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8113 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8114 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8115 goto done;
8116
8117 result = TRUE;
8118
8119 done:
8120 if (symtable1)
8121 free (symtable1);
8122 if (symtable2)
8123 free (symtable2);
8124 if (isymbuf1)
8125 free (isymbuf1);
8126 if (isymbuf2)
8127 free (isymbuf2);
8128
8129 return result;
8130 }
8131
8132 /* Return TRUE if 2 section types are compatible. */
8133
8134 bfd_boolean
8135 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8136 bfd *bbfd, const asection *bsec)
8137 {
8138 if (asec == NULL
8139 || bsec == NULL
8140 || abfd->xvec->flavour != bfd_target_elf_flavour
8141 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8142 return TRUE;
8143
8144 return elf_section_type (asec) == elf_section_type (bsec);
8145 }
8146 \f
8147 /* Final phase of ELF linker. */
8148
8149 /* A structure we use to avoid passing large numbers of arguments. */
8150
8151 struct elf_final_link_info
8152 {
8153 /* General link information. */
8154 struct bfd_link_info *info;
8155 /* Output BFD. */
8156 bfd *output_bfd;
8157 /* Symbol string table. */
8158 struct elf_strtab_hash *symstrtab;
8159 /* .hash section. */
8160 asection *hash_sec;
8161 /* symbol version section (.gnu.version). */
8162 asection *symver_sec;
8163 /* Buffer large enough to hold contents of any section. */
8164 bfd_byte *contents;
8165 /* Buffer large enough to hold external relocs of any section. */
8166 void *external_relocs;
8167 /* Buffer large enough to hold internal relocs of any section. */
8168 Elf_Internal_Rela *internal_relocs;
8169 /* Buffer large enough to hold external local symbols of any input
8170 BFD. */
8171 bfd_byte *external_syms;
8172 /* And a buffer for symbol section indices. */
8173 Elf_External_Sym_Shndx *locsym_shndx;
8174 /* Buffer large enough to hold internal local symbols of any input
8175 BFD. */
8176 Elf_Internal_Sym *internal_syms;
8177 /* Array large enough to hold a symbol index for each local symbol
8178 of any input BFD. */
8179 long *indices;
8180 /* Array large enough to hold a section pointer for each local
8181 symbol of any input BFD. */
8182 asection **sections;
8183 /* Buffer for SHT_SYMTAB_SHNDX section. */
8184 Elf_External_Sym_Shndx *symshndxbuf;
8185 /* Number of STT_FILE syms seen. */
8186 size_t filesym_count;
8187 };
8188
8189 /* This struct is used to pass information to elf_link_output_extsym. */
8190
8191 struct elf_outext_info
8192 {
8193 bfd_boolean failed;
8194 bfd_boolean localsyms;
8195 bfd_boolean file_sym_done;
8196 struct elf_final_link_info *flinfo;
8197 };
8198
8199
8200 /* Support for evaluating a complex relocation.
8201
8202 Complex relocations are generalized, self-describing relocations. The
8203 implementation of them consists of two parts: complex symbols, and the
8204 relocations themselves.
8205
8206 The relocations are use a reserved elf-wide relocation type code (R_RELC
8207 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8208 information (start bit, end bit, word width, etc) into the addend. This
8209 information is extracted from CGEN-generated operand tables within gas.
8210
8211 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8212 internal) representing prefix-notation expressions, including but not
8213 limited to those sorts of expressions normally encoded as addends in the
8214 addend field. The symbol mangling format is:
8215
8216 <node> := <literal>
8217 | <unary-operator> ':' <node>
8218 | <binary-operator> ':' <node> ':' <node>
8219 ;
8220
8221 <literal> := 's' <digits=N> ':' <N character symbol name>
8222 | 'S' <digits=N> ':' <N character section name>
8223 | '#' <hexdigits>
8224 ;
8225
8226 <binary-operator> := as in C
8227 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8228
8229 static void
8230 set_symbol_value (bfd *bfd_with_globals,
8231 Elf_Internal_Sym *isymbuf,
8232 size_t locsymcount,
8233 size_t symidx,
8234 bfd_vma val)
8235 {
8236 struct elf_link_hash_entry **sym_hashes;
8237 struct elf_link_hash_entry *h;
8238 size_t extsymoff = locsymcount;
8239
8240 if (symidx < locsymcount)
8241 {
8242 Elf_Internal_Sym *sym;
8243
8244 sym = isymbuf + symidx;
8245 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8246 {
8247 /* It is a local symbol: move it to the
8248 "absolute" section and give it a value. */
8249 sym->st_shndx = SHN_ABS;
8250 sym->st_value = val;
8251 return;
8252 }
8253 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8254 extsymoff = 0;
8255 }
8256
8257 /* It is a global symbol: set its link type
8258 to "defined" and give it a value. */
8259
8260 sym_hashes = elf_sym_hashes (bfd_with_globals);
8261 h = sym_hashes [symidx - extsymoff];
8262 while (h->root.type == bfd_link_hash_indirect
8263 || h->root.type == bfd_link_hash_warning)
8264 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8265 h->root.type = bfd_link_hash_defined;
8266 h->root.u.def.value = val;
8267 h->root.u.def.section = bfd_abs_section_ptr;
8268 }
8269
8270 static bfd_boolean
8271 resolve_symbol (const char *name,
8272 bfd *input_bfd,
8273 struct elf_final_link_info *flinfo,
8274 bfd_vma *result,
8275 Elf_Internal_Sym *isymbuf,
8276 size_t locsymcount)
8277 {
8278 Elf_Internal_Sym *sym;
8279 struct bfd_link_hash_entry *global_entry;
8280 const char *candidate = NULL;
8281 Elf_Internal_Shdr *symtab_hdr;
8282 size_t i;
8283
8284 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8285
8286 for (i = 0; i < locsymcount; ++ i)
8287 {
8288 sym = isymbuf + i;
8289
8290 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8291 continue;
8292
8293 candidate = bfd_elf_string_from_elf_section (input_bfd,
8294 symtab_hdr->sh_link,
8295 sym->st_name);
8296 #ifdef DEBUG
8297 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8298 name, candidate, (unsigned long) sym->st_value);
8299 #endif
8300 if (candidate && strcmp (candidate, name) == 0)
8301 {
8302 asection *sec = flinfo->sections [i];
8303
8304 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8305 *result += sec->output_offset + sec->output_section->vma;
8306 #ifdef DEBUG
8307 printf ("Found symbol with value %8.8lx\n",
8308 (unsigned long) *result);
8309 #endif
8310 return TRUE;
8311 }
8312 }
8313
8314 /* Hmm, haven't found it yet. perhaps it is a global. */
8315 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8316 FALSE, FALSE, TRUE);
8317 if (!global_entry)
8318 return FALSE;
8319
8320 if (global_entry->type == bfd_link_hash_defined
8321 || global_entry->type == bfd_link_hash_defweak)
8322 {
8323 *result = (global_entry->u.def.value
8324 + global_entry->u.def.section->output_section->vma
8325 + global_entry->u.def.section->output_offset);
8326 #ifdef DEBUG
8327 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8328 global_entry->root.string, (unsigned long) *result);
8329 #endif
8330 return TRUE;
8331 }
8332
8333 return FALSE;
8334 }
8335
8336 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8337 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8338 names like "foo.end" which is the end address of section "foo". */
8339
8340 static bfd_boolean
8341 resolve_section (const char *name,
8342 asection *sections,
8343 bfd_vma *result,
8344 bfd * abfd)
8345 {
8346 asection *curr;
8347 unsigned int len;
8348
8349 for (curr = sections; curr; curr = curr->next)
8350 if (strcmp (curr->name, name) == 0)
8351 {
8352 *result = curr->vma;
8353 return TRUE;
8354 }
8355
8356 /* Hmm. still haven't found it. try pseudo-section names. */
8357 /* FIXME: This could be coded more efficiently... */
8358 for (curr = sections; curr; curr = curr->next)
8359 {
8360 len = strlen (curr->name);
8361 if (len > strlen (name))
8362 continue;
8363
8364 if (strncmp (curr->name, name, len) == 0)
8365 {
8366 if (strncmp (".end", name + len, 4) == 0)
8367 {
8368 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
8369 return TRUE;
8370 }
8371
8372 /* Insert more pseudo-section names here, if you like. */
8373 }
8374 }
8375
8376 return FALSE;
8377 }
8378
8379 static void
8380 undefined_reference (const char *reftype, const char *name)
8381 {
8382 /* xgettext:c-format */
8383 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8384 reftype, name);
8385 }
8386
8387 static bfd_boolean
8388 eval_symbol (bfd_vma *result,
8389 const char **symp,
8390 bfd *input_bfd,
8391 struct elf_final_link_info *flinfo,
8392 bfd_vma dot,
8393 Elf_Internal_Sym *isymbuf,
8394 size_t locsymcount,
8395 int signed_p)
8396 {
8397 size_t len;
8398 size_t symlen;
8399 bfd_vma a;
8400 bfd_vma b;
8401 char symbuf[4096];
8402 const char *sym = *symp;
8403 const char *symend;
8404 bfd_boolean symbol_is_section = FALSE;
8405
8406 len = strlen (sym);
8407 symend = sym + len;
8408
8409 if (len < 1 || len > sizeof (symbuf))
8410 {
8411 bfd_set_error (bfd_error_invalid_operation);
8412 return FALSE;
8413 }
8414
8415 switch (* sym)
8416 {
8417 case '.':
8418 *result = dot;
8419 *symp = sym + 1;
8420 return TRUE;
8421
8422 case '#':
8423 ++sym;
8424 *result = strtoul (sym, (char **) symp, 16);
8425 return TRUE;
8426
8427 case 'S':
8428 symbol_is_section = TRUE;
8429 /* Fall through. */
8430 case 's':
8431 ++sym;
8432 symlen = strtol (sym, (char **) symp, 10);
8433 sym = *symp + 1; /* Skip the trailing ':'. */
8434
8435 if (symend < sym || symlen + 1 > sizeof (symbuf))
8436 {
8437 bfd_set_error (bfd_error_invalid_operation);
8438 return FALSE;
8439 }
8440
8441 memcpy (symbuf, sym, symlen);
8442 symbuf[symlen] = '\0';
8443 *symp = sym + symlen;
8444
8445 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8446 the symbol as a section, or vice-versa. so we're pretty liberal in our
8447 interpretation here; section means "try section first", not "must be a
8448 section", and likewise with symbol. */
8449
8450 if (symbol_is_section)
8451 {
8452 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8453 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8454 isymbuf, locsymcount))
8455 {
8456 undefined_reference ("section", symbuf);
8457 return FALSE;
8458 }
8459 }
8460 else
8461 {
8462 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8463 isymbuf, locsymcount)
8464 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8465 result, input_bfd))
8466 {
8467 undefined_reference ("symbol", symbuf);
8468 return FALSE;
8469 }
8470 }
8471
8472 return TRUE;
8473
8474 /* All that remains are operators. */
8475
8476 #define UNARY_OP(op) \
8477 if (strncmp (sym, #op, strlen (#op)) == 0) \
8478 { \
8479 sym += strlen (#op); \
8480 if (*sym == ':') \
8481 ++sym; \
8482 *symp = sym; \
8483 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8484 isymbuf, locsymcount, signed_p)) \
8485 return FALSE; \
8486 if (signed_p) \
8487 *result = op ((bfd_signed_vma) a); \
8488 else \
8489 *result = op a; \
8490 return TRUE; \
8491 }
8492
8493 #define BINARY_OP(op) \
8494 if (strncmp (sym, #op, strlen (#op)) == 0) \
8495 { \
8496 sym += strlen (#op); \
8497 if (*sym == ':') \
8498 ++sym; \
8499 *symp = sym; \
8500 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8501 isymbuf, locsymcount, signed_p)) \
8502 return FALSE; \
8503 ++*symp; \
8504 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8505 isymbuf, locsymcount, signed_p)) \
8506 return FALSE; \
8507 if (signed_p) \
8508 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8509 else \
8510 *result = a op b; \
8511 return TRUE; \
8512 }
8513
8514 default:
8515 UNARY_OP (0-);
8516 BINARY_OP (<<);
8517 BINARY_OP (>>);
8518 BINARY_OP (==);
8519 BINARY_OP (!=);
8520 BINARY_OP (<=);
8521 BINARY_OP (>=);
8522 BINARY_OP (&&);
8523 BINARY_OP (||);
8524 UNARY_OP (~);
8525 UNARY_OP (!);
8526 BINARY_OP (*);
8527 BINARY_OP (/);
8528 BINARY_OP (%);
8529 BINARY_OP (^);
8530 BINARY_OP (|);
8531 BINARY_OP (&);
8532 BINARY_OP (+);
8533 BINARY_OP (-);
8534 BINARY_OP (<);
8535 BINARY_OP (>);
8536 #undef UNARY_OP
8537 #undef BINARY_OP
8538 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8539 bfd_set_error (bfd_error_invalid_operation);
8540 return FALSE;
8541 }
8542 }
8543
8544 static void
8545 put_value (bfd_vma size,
8546 unsigned long chunksz,
8547 bfd *input_bfd,
8548 bfd_vma x,
8549 bfd_byte *location)
8550 {
8551 location += (size - chunksz);
8552
8553 for (; size; size -= chunksz, location -= chunksz)
8554 {
8555 switch (chunksz)
8556 {
8557 case 1:
8558 bfd_put_8 (input_bfd, x, location);
8559 x >>= 8;
8560 break;
8561 case 2:
8562 bfd_put_16 (input_bfd, x, location);
8563 x >>= 16;
8564 break;
8565 case 4:
8566 bfd_put_32 (input_bfd, x, location);
8567 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8568 x >>= 16;
8569 x >>= 16;
8570 break;
8571 #ifdef BFD64
8572 case 8:
8573 bfd_put_64 (input_bfd, x, location);
8574 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8575 x >>= 32;
8576 x >>= 32;
8577 break;
8578 #endif
8579 default:
8580 abort ();
8581 break;
8582 }
8583 }
8584 }
8585
8586 static bfd_vma
8587 get_value (bfd_vma size,
8588 unsigned long chunksz,
8589 bfd *input_bfd,
8590 bfd_byte *location)
8591 {
8592 int shift;
8593 bfd_vma x = 0;
8594
8595 /* Sanity checks. */
8596 BFD_ASSERT (chunksz <= sizeof (x)
8597 && size >= chunksz
8598 && chunksz != 0
8599 && (size % chunksz) == 0
8600 && input_bfd != NULL
8601 && location != NULL);
8602
8603 if (chunksz == sizeof (x))
8604 {
8605 BFD_ASSERT (size == chunksz);
8606
8607 /* Make sure that we do not perform an undefined shift operation.
8608 We know that size == chunksz so there will only be one iteration
8609 of the loop below. */
8610 shift = 0;
8611 }
8612 else
8613 shift = 8 * chunksz;
8614
8615 for (; size; size -= chunksz, location += chunksz)
8616 {
8617 switch (chunksz)
8618 {
8619 case 1:
8620 x = (x << shift) | bfd_get_8 (input_bfd, location);
8621 break;
8622 case 2:
8623 x = (x << shift) | bfd_get_16 (input_bfd, location);
8624 break;
8625 case 4:
8626 x = (x << shift) | bfd_get_32 (input_bfd, location);
8627 break;
8628 #ifdef BFD64
8629 case 8:
8630 x = (x << shift) | bfd_get_64 (input_bfd, location);
8631 break;
8632 #endif
8633 default:
8634 abort ();
8635 }
8636 }
8637 return x;
8638 }
8639
8640 static void
8641 decode_complex_addend (unsigned long *start, /* in bits */
8642 unsigned long *oplen, /* in bits */
8643 unsigned long *len, /* in bits */
8644 unsigned long *wordsz, /* in bytes */
8645 unsigned long *chunksz, /* in bytes */
8646 unsigned long *lsb0_p,
8647 unsigned long *signed_p,
8648 unsigned long *trunc_p,
8649 unsigned long encoded)
8650 {
8651 * start = encoded & 0x3F;
8652 * len = (encoded >> 6) & 0x3F;
8653 * oplen = (encoded >> 12) & 0x3F;
8654 * wordsz = (encoded >> 18) & 0xF;
8655 * chunksz = (encoded >> 22) & 0xF;
8656 * lsb0_p = (encoded >> 27) & 1;
8657 * signed_p = (encoded >> 28) & 1;
8658 * trunc_p = (encoded >> 29) & 1;
8659 }
8660
8661 bfd_reloc_status_type
8662 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8663 asection *input_section ATTRIBUTE_UNUSED,
8664 bfd_byte *contents,
8665 Elf_Internal_Rela *rel,
8666 bfd_vma relocation)
8667 {
8668 bfd_vma shift, x, mask;
8669 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8670 bfd_reloc_status_type r;
8671
8672 /* Perform this reloc, since it is complex.
8673 (this is not to say that it necessarily refers to a complex
8674 symbol; merely that it is a self-describing CGEN based reloc.
8675 i.e. the addend has the complete reloc information (bit start, end,
8676 word size, etc) encoded within it.). */
8677
8678 decode_complex_addend (&start, &oplen, &len, &wordsz,
8679 &chunksz, &lsb0_p, &signed_p,
8680 &trunc_p, rel->r_addend);
8681
8682 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8683
8684 if (lsb0_p)
8685 shift = (start + 1) - len;
8686 else
8687 shift = (8 * wordsz) - (start + len);
8688
8689 x = get_value (wordsz, chunksz, input_bfd,
8690 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8691
8692 #ifdef DEBUG
8693 printf ("Doing complex reloc: "
8694 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8695 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8696 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8697 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8698 oplen, (unsigned long) x, (unsigned long) mask,
8699 (unsigned long) relocation);
8700 #endif
8701
8702 r = bfd_reloc_ok;
8703 if (! trunc_p)
8704 /* Now do an overflow check. */
8705 r = bfd_check_overflow ((signed_p
8706 ? complain_overflow_signed
8707 : complain_overflow_unsigned),
8708 len, 0, (8 * wordsz),
8709 relocation);
8710
8711 /* Do the deed. */
8712 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8713
8714 #ifdef DEBUG
8715 printf (" relocation: %8.8lx\n"
8716 " shifted mask: %8.8lx\n"
8717 " shifted/masked reloc: %8.8lx\n"
8718 " result: %8.8lx\n",
8719 (unsigned long) relocation, (unsigned long) (mask << shift),
8720 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8721 #endif
8722 put_value (wordsz, chunksz, input_bfd, x,
8723 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8724 return r;
8725 }
8726
8727 /* Functions to read r_offset from external (target order) reloc
8728 entry. Faster than bfd_getl32 et al, because we let the compiler
8729 know the value is aligned. */
8730
8731 static bfd_vma
8732 ext32l_r_offset (const void *p)
8733 {
8734 union aligned32
8735 {
8736 uint32_t v;
8737 unsigned char c[4];
8738 };
8739 const union aligned32 *a
8740 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8741
8742 uint32_t aval = ( (uint32_t) a->c[0]
8743 | (uint32_t) a->c[1] << 8
8744 | (uint32_t) a->c[2] << 16
8745 | (uint32_t) a->c[3] << 24);
8746 return aval;
8747 }
8748
8749 static bfd_vma
8750 ext32b_r_offset (const void *p)
8751 {
8752 union aligned32
8753 {
8754 uint32_t v;
8755 unsigned char c[4];
8756 };
8757 const union aligned32 *a
8758 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8759
8760 uint32_t aval = ( (uint32_t) a->c[0] << 24
8761 | (uint32_t) a->c[1] << 16
8762 | (uint32_t) a->c[2] << 8
8763 | (uint32_t) a->c[3]);
8764 return aval;
8765 }
8766
8767 #ifdef BFD_HOST_64_BIT
8768 static bfd_vma
8769 ext64l_r_offset (const void *p)
8770 {
8771 union aligned64
8772 {
8773 uint64_t v;
8774 unsigned char c[8];
8775 };
8776 const union aligned64 *a
8777 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8778
8779 uint64_t aval = ( (uint64_t) a->c[0]
8780 | (uint64_t) a->c[1] << 8
8781 | (uint64_t) a->c[2] << 16
8782 | (uint64_t) a->c[3] << 24
8783 | (uint64_t) a->c[4] << 32
8784 | (uint64_t) a->c[5] << 40
8785 | (uint64_t) a->c[6] << 48
8786 | (uint64_t) a->c[7] << 56);
8787 return aval;
8788 }
8789
8790 static bfd_vma
8791 ext64b_r_offset (const void *p)
8792 {
8793 union aligned64
8794 {
8795 uint64_t v;
8796 unsigned char c[8];
8797 };
8798 const union aligned64 *a
8799 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8800
8801 uint64_t aval = ( (uint64_t) a->c[0] << 56
8802 | (uint64_t) a->c[1] << 48
8803 | (uint64_t) a->c[2] << 40
8804 | (uint64_t) a->c[3] << 32
8805 | (uint64_t) a->c[4] << 24
8806 | (uint64_t) a->c[5] << 16
8807 | (uint64_t) a->c[6] << 8
8808 | (uint64_t) a->c[7]);
8809 return aval;
8810 }
8811 #endif
8812
8813 /* When performing a relocatable link, the input relocations are
8814 preserved. But, if they reference global symbols, the indices
8815 referenced must be updated. Update all the relocations found in
8816 RELDATA. */
8817
8818 static bfd_boolean
8819 elf_link_adjust_relocs (bfd *abfd,
8820 asection *sec,
8821 struct bfd_elf_section_reloc_data *reldata,
8822 bfd_boolean sort,
8823 struct bfd_link_info *info)
8824 {
8825 unsigned int i;
8826 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8827 bfd_byte *erela;
8828 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8829 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8830 bfd_vma r_type_mask;
8831 int r_sym_shift;
8832 unsigned int count = reldata->count;
8833 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8834
8835 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8836 {
8837 swap_in = bed->s->swap_reloc_in;
8838 swap_out = bed->s->swap_reloc_out;
8839 }
8840 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8841 {
8842 swap_in = bed->s->swap_reloca_in;
8843 swap_out = bed->s->swap_reloca_out;
8844 }
8845 else
8846 abort ();
8847
8848 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8849 abort ();
8850
8851 if (bed->s->arch_size == 32)
8852 {
8853 r_type_mask = 0xff;
8854 r_sym_shift = 8;
8855 }
8856 else
8857 {
8858 r_type_mask = 0xffffffff;
8859 r_sym_shift = 32;
8860 }
8861
8862 erela = reldata->hdr->contents;
8863 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8864 {
8865 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8866 unsigned int j;
8867
8868 if (*rel_hash == NULL)
8869 continue;
8870
8871 if ((*rel_hash)->indx == -2
8872 && info->gc_sections
8873 && ! info->gc_keep_exported)
8874 {
8875 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
8876 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
8877 abfd, sec,
8878 (*rel_hash)->root.root.string);
8879 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
8880 abfd, sec);
8881 bfd_set_error (bfd_error_invalid_operation);
8882 return FALSE;
8883 }
8884 BFD_ASSERT ((*rel_hash)->indx >= 0);
8885
8886 (*swap_in) (abfd, erela, irela);
8887 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8888 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8889 | (irela[j].r_info & r_type_mask));
8890 (*swap_out) (abfd, irela, erela);
8891 }
8892
8893 if (bed->elf_backend_update_relocs)
8894 (*bed->elf_backend_update_relocs) (sec, reldata);
8895
8896 if (sort && count != 0)
8897 {
8898 bfd_vma (*ext_r_off) (const void *);
8899 bfd_vma r_off;
8900 size_t elt_size;
8901 bfd_byte *base, *end, *p, *loc;
8902 bfd_byte *buf = NULL;
8903
8904 if (bed->s->arch_size == 32)
8905 {
8906 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8907 ext_r_off = ext32l_r_offset;
8908 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8909 ext_r_off = ext32b_r_offset;
8910 else
8911 abort ();
8912 }
8913 else
8914 {
8915 #ifdef BFD_HOST_64_BIT
8916 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8917 ext_r_off = ext64l_r_offset;
8918 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8919 ext_r_off = ext64b_r_offset;
8920 else
8921 #endif
8922 abort ();
8923 }
8924
8925 /* Must use a stable sort here. A modified insertion sort,
8926 since the relocs are mostly sorted already. */
8927 elt_size = reldata->hdr->sh_entsize;
8928 base = reldata->hdr->contents;
8929 end = base + count * elt_size;
8930 if (elt_size > sizeof (Elf64_External_Rela))
8931 abort ();
8932
8933 /* Ensure the first element is lowest. This acts as a sentinel,
8934 speeding the main loop below. */
8935 r_off = (*ext_r_off) (base);
8936 for (p = loc = base; (p += elt_size) < end; )
8937 {
8938 bfd_vma r_off2 = (*ext_r_off) (p);
8939 if (r_off > r_off2)
8940 {
8941 r_off = r_off2;
8942 loc = p;
8943 }
8944 }
8945 if (loc != base)
8946 {
8947 /* Don't just swap *base and *loc as that changes the order
8948 of the original base[0] and base[1] if they happen to
8949 have the same r_offset. */
8950 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8951 memcpy (onebuf, loc, elt_size);
8952 memmove (base + elt_size, base, loc - base);
8953 memcpy (base, onebuf, elt_size);
8954 }
8955
8956 for (p = base + elt_size; (p += elt_size) < end; )
8957 {
8958 /* base to p is sorted, *p is next to insert. */
8959 r_off = (*ext_r_off) (p);
8960 /* Search the sorted region for location to insert. */
8961 loc = p - elt_size;
8962 while (r_off < (*ext_r_off) (loc))
8963 loc -= elt_size;
8964 loc += elt_size;
8965 if (loc != p)
8966 {
8967 /* Chances are there is a run of relocs to insert here,
8968 from one of more input files. Files are not always
8969 linked in order due to the way elf_link_input_bfd is
8970 called. See pr17666. */
8971 size_t sortlen = p - loc;
8972 bfd_vma r_off2 = (*ext_r_off) (loc);
8973 size_t runlen = elt_size;
8974 size_t buf_size = 96 * 1024;
8975 while (p + runlen < end
8976 && (sortlen <= buf_size
8977 || runlen + elt_size <= buf_size)
8978 && r_off2 > (*ext_r_off) (p + runlen))
8979 runlen += elt_size;
8980 if (buf == NULL)
8981 {
8982 buf = bfd_malloc (buf_size);
8983 if (buf == NULL)
8984 return FALSE;
8985 }
8986 if (runlen < sortlen)
8987 {
8988 memcpy (buf, p, runlen);
8989 memmove (loc + runlen, loc, sortlen);
8990 memcpy (loc, buf, runlen);
8991 }
8992 else
8993 {
8994 memcpy (buf, loc, sortlen);
8995 memmove (loc, p, runlen);
8996 memcpy (loc + runlen, buf, sortlen);
8997 }
8998 p += runlen - elt_size;
8999 }
9000 }
9001 /* Hashes are no longer valid. */
9002 free (reldata->hashes);
9003 reldata->hashes = NULL;
9004 free (buf);
9005 }
9006 return TRUE;
9007 }
9008
9009 struct elf_link_sort_rela
9010 {
9011 union {
9012 bfd_vma offset;
9013 bfd_vma sym_mask;
9014 } u;
9015 enum elf_reloc_type_class type;
9016 /* We use this as an array of size int_rels_per_ext_rel. */
9017 Elf_Internal_Rela rela[1];
9018 };
9019
9020 static int
9021 elf_link_sort_cmp1 (const void *A, const void *B)
9022 {
9023 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9024 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9025 int relativea, relativeb;
9026
9027 relativea = a->type == reloc_class_relative;
9028 relativeb = b->type == reloc_class_relative;
9029
9030 if (relativea < relativeb)
9031 return 1;
9032 if (relativea > relativeb)
9033 return -1;
9034 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9035 return -1;
9036 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9037 return 1;
9038 if (a->rela->r_offset < b->rela->r_offset)
9039 return -1;
9040 if (a->rela->r_offset > b->rela->r_offset)
9041 return 1;
9042 return 0;
9043 }
9044
9045 static int
9046 elf_link_sort_cmp2 (const void *A, const void *B)
9047 {
9048 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9049 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9050
9051 if (a->type < b->type)
9052 return -1;
9053 if (a->type > b->type)
9054 return 1;
9055 if (a->u.offset < b->u.offset)
9056 return -1;
9057 if (a->u.offset > b->u.offset)
9058 return 1;
9059 if (a->rela->r_offset < b->rela->r_offset)
9060 return -1;
9061 if (a->rela->r_offset > b->rela->r_offset)
9062 return 1;
9063 return 0;
9064 }
9065
9066 static size_t
9067 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9068 {
9069 asection *dynamic_relocs;
9070 asection *rela_dyn;
9071 asection *rel_dyn;
9072 bfd_size_type count, size;
9073 size_t i, ret, sort_elt, ext_size;
9074 bfd_byte *sort, *s_non_relative, *p;
9075 struct elf_link_sort_rela *sq;
9076 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9077 int i2e = bed->s->int_rels_per_ext_rel;
9078 unsigned int opb = bfd_octets_per_byte (abfd);
9079 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9080 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9081 struct bfd_link_order *lo;
9082 bfd_vma r_sym_mask;
9083 bfd_boolean use_rela;
9084
9085 /* Find a dynamic reloc section. */
9086 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9087 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9088 if (rela_dyn != NULL && rela_dyn->size > 0
9089 && rel_dyn != NULL && rel_dyn->size > 0)
9090 {
9091 bfd_boolean use_rela_initialised = FALSE;
9092
9093 /* This is just here to stop gcc from complaining.
9094 Its initialization checking code is not perfect. */
9095 use_rela = TRUE;
9096
9097 /* Both sections are present. Examine the sizes
9098 of the indirect sections to help us choose. */
9099 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9100 if (lo->type == bfd_indirect_link_order)
9101 {
9102 asection *o = lo->u.indirect.section;
9103
9104 if ((o->size % bed->s->sizeof_rela) == 0)
9105 {
9106 if ((o->size % bed->s->sizeof_rel) == 0)
9107 /* Section size is divisible by both rel and rela sizes.
9108 It is of no help to us. */
9109 ;
9110 else
9111 {
9112 /* Section size is only divisible by rela. */
9113 if (use_rela_initialised && !use_rela)
9114 {
9115 _bfd_error_handler (_("%pB: unable to sort relocs - "
9116 "they are in more than one size"),
9117 abfd);
9118 bfd_set_error (bfd_error_invalid_operation);
9119 return 0;
9120 }
9121 else
9122 {
9123 use_rela = TRUE;
9124 use_rela_initialised = TRUE;
9125 }
9126 }
9127 }
9128 else if ((o->size % bed->s->sizeof_rel) == 0)
9129 {
9130 /* Section size is only divisible by rel. */
9131 if (use_rela_initialised && use_rela)
9132 {
9133 _bfd_error_handler (_("%pB: unable to sort relocs - "
9134 "they are in more than one size"),
9135 abfd);
9136 bfd_set_error (bfd_error_invalid_operation);
9137 return 0;
9138 }
9139 else
9140 {
9141 use_rela = FALSE;
9142 use_rela_initialised = TRUE;
9143 }
9144 }
9145 else
9146 {
9147 /* The section size is not divisible by either -
9148 something is wrong. */
9149 _bfd_error_handler (_("%pB: unable to sort relocs - "
9150 "they are of an unknown size"), abfd);
9151 bfd_set_error (bfd_error_invalid_operation);
9152 return 0;
9153 }
9154 }
9155
9156 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9157 if (lo->type == bfd_indirect_link_order)
9158 {
9159 asection *o = lo->u.indirect.section;
9160
9161 if ((o->size % bed->s->sizeof_rela) == 0)
9162 {
9163 if ((o->size % bed->s->sizeof_rel) == 0)
9164 /* Section size is divisible by both rel and rela sizes.
9165 It is of no help to us. */
9166 ;
9167 else
9168 {
9169 /* Section size is only divisible by rela. */
9170 if (use_rela_initialised && !use_rela)
9171 {
9172 _bfd_error_handler (_("%pB: unable to sort relocs - "
9173 "they are in more than one size"),
9174 abfd);
9175 bfd_set_error (bfd_error_invalid_operation);
9176 return 0;
9177 }
9178 else
9179 {
9180 use_rela = TRUE;
9181 use_rela_initialised = TRUE;
9182 }
9183 }
9184 }
9185 else if ((o->size % bed->s->sizeof_rel) == 0)
9186 {
9187 /* Section size is only divisible by rel. */
9188 if (use_rela_initialised && use_rela)
9189 {
9190 _bfd_error_handler (_("%pB: unable to sort relocs - "
9191 "they are in more than one size"),
9192 abfd);
9193 bfd_set_error (bfd_error_invalid_operation);
9194 return 0;
9195 }
9196 else
9197 {
9198 use_rela = FALSE;
9199 use_rela_initialised = TRUE;
9200 }
9201 }
9202 else
9203 {
9204 /* The section size is not divisible by either -
9205 something is wrong. */
9206 _bfd_error_handler (_("%pB: unable to sort relocs - "
9207 "they are of an unknown size"), abfd);
9208 bfd_set_error (bfd_error_invalid_operation);
9209 return 0;
9210 }
9211 }
9212
9213 if (! use_rela_initialised)
9214 /* Make a guess. */
9215 use_rela = TRUE;
9216 }
9217 else if (rela_dyn != NULL && rela_dyn->size > 0)
9218 use_rela = TRUE;
9219 else if (rel_dyn != NULL && rel_dyn->size > 0)
9220 use_rela = FALSE;
9221 else
9222 return 0;
9223
9224 if (use_rela)
9225 {
9226 dynamic_relocs = rela_dyn;
9227 ext_size = bed->s->sizeof_rela;
9228 swap_in = bed->s->swap_reloca_in;
9229 swap_out = bed->s->swap_reloca_out;
9230 }
9231 else
9232 {
9233 dynamic_relocs = rel_dyn;
9234 ext_size = bed->s->sizeof_rel;
9235 swap_in = bed->s->swap_reloc_in;
9236 swap_out = bed->s->swap_reloc_out;
9237 }
9238
9239 size = 0;
9240 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9241 if (lo->type == bfd_indirect_link_order)
9242 size += lo->u.indirect.section->size;
9243
9244 if (size != dynamic_relocs->size)
9245 return 0;
9246
9247 sort_elt = (sizeof (struct elf_link_sort_rela)
9248 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9249
9250 count = dynamic_relocs->size / ext_size;
9251 if (count == 0)
9252 return 0;
9253 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9254
9255 if (sort == NULL)
9256 {
9257 (*info->callbacks->warning)
9258 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
9259 return 0;
9260 }
9261
9262 if (bed->s->arch_size == 32)
9263 r_sym_mask = ~(bfd_vma) 0xff;
9264 else
9265 r_sym_mask = ~(bfd_vma) 0xffffffff;
9266
9267 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9268 if (lo->type == bfd_indirect_link_order)
9269 {
9270 bfd_byte *erel, *erelend;
9271 asection *o = lo->u.indirect.section;
9272
9273 if (o->contents == NULL && o->size != 0)
9274 {
9275 /* This is a reloc section that is being handled as a normal
9276 section. See bfd_section_from_shdr. We can't combine
9277 relocs in this case. */
9278 free (sort);
9279 return 0;
9280 }
9281 erel = o->contents;
9282 erelend = o->contents + o->size;
9283 p = sort + o->output_offset * opb / ext_size * sort_elt;
9284
9285 while (erel < erelend)
9286 {
9287 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9288
9289 (*swap_in) (abfd, erel, s->rela);
9290 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9291 s->u.sym_mask = r_sym_mask;
9292 p += sort_elt;
9293 erel += ext_size;
9294 }
9295 }
9296
9297 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9298
9299 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9300 {
9301 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9302 if (s->type != reloc_class_relative)
9303 break;
9304 }
9305 ret = i;
9306 s_non_relative = p;
9307
9308 sq = (struct elf_link_sort_rela *) s_non_relative;
9309 for (; i < count; i++, p += sort_elt)
9310 {
9311 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9312 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9313 sq = sp;
9314 sp->u.offset = sq->rela->r_offset;
9315 }
9316
9317 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9318
9319 struct elf_link_hash_table *htab = elf_hash_table (info);
9320 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9321 {
9322 /* We have plt relocs in .rela.dyn. */
9323 sq = (struct elf_link_sort_rela *) sort;
9324 for (i = 0; i < count; i++)
9325 if (sq[count - i - 1].type != reloc_class_plt)
9326 break;
9327 if (i != 0 && htab->srelplt->size == i * ext_size)
9328 {
9329 struct bfd_link_order **plo;
9330 /* Put srelplt link_order last. This is so the output_offset
9331 set in the next loop is correct for DT_JMPREL. */
9332 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9333 if ((*plo)->type == bfd_indirect_link_order
9334 && (*plo)->u.indirect.section == htab->srelplt)
9335 {
9336 lo = *plo;
9337 *plo = lo->next;
9338 }
9339 else
9340 plo = &(*plo)->next;
9341 *plo = lo;
9342 lo->next = NULL;
9343 dynamic_relocs->map_tail.link_order = lo;
9344 }
9345 }
9346
9347 p = sort;
9348 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9349 if (lo->type == bfd_indirect_link_order)
9350 {
9351 bfd_byte *erel, *erelend;
9352 asection *o = lo->u.indirect.section;
9353
9354 erel = o->contents;
9355 erelend = o->contents + o->size;
9356 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9357 while (erel < erelend)
9358 {
9359 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9360 (*swap_out) (abfd, s->rela, erel);
9361 p += sort_elt;
9362 erel += ext_size;
9363 }
9364 }
9365
9366 free (sort);
9367 *psec = dynamic_relocs;
9368 return ret;
9369 }
9370
9371 /* Add a symbol to the output symbol string table. */
9372
9373 static int
9374 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9375 const char *name,
9376 Elf_Internal_Sym *elfsym,
9377 asection *input_sec,
9378 struct elf_link_hash_entry *h)
9379 {
9380 int (*output_symbol_hook)
9381 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9382 struct elf_link_hash_entry *);
9383 struct elf_link_hash_table *hash_table;
9384 const struct elf_backend_data *bed;
9385 bfd_size_type strtabsize;
9386
9387 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9388
9389 bed = get_elf_backend_data (flinfo->output_bfd);
9390 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9391 if (output_symbol_hook != NULL)
9392 {
9393 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9394 if (ret != 1)
9395 return ret;
9396 }
9397
9398 if (name == NULL
9399 || *name == '\0'
9400 || (input_sec->flags & SEC_EXCLUDE))
9401 elfsym->st_name = (unsigned long) -1;
9402 else
9403 {
9404 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9405 to get the final offset for st_name. */
9406 elfsym->st_name
9407 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9408 name, FALSE);
9409 if (elfsym->st_name == (unsigned long) -1)
9410 return 0;
9411 }
9412
9413 hash_table = elf_hash_table (flinfo->info);
9414 strtabsize = hash_table->strtabsize;
9415 if (strtabsize <= hash_table->strtabcount)
9416 {
9417 strtabsize += strtabsize;
9418 hash_table->strtabsize = strtabsize;
9419 strtabsize *= sizeof (*hash_table->strtab);
9420 hash_table->strtab
9421 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9422 strtabsize);
9423 if (hash_table->strtab == NULL)
9424 return 0;
9425 }
9426 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9427 hash_table->strtab[hash_table->strtabcount].dest_index
9428 = hash_table->strtabcount;
9429 hash_table->strtab[hash_table->strtabcount].destshndx_index
9430 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9431
9432 bfd_get_symcount (flinfo->output_bfd) += 1;
9433 hash_table->strtabcount += 1;
9434
9435 return 1;
9436 }
9437
9438 /* Swap symbols out to the symbol table and flush the output symbols to
9439 the file. */
9440
9441 static bfd_boolean
9442 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9443 {
9444 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9445 bfd_size_type amt;
9446 size_t i;
9447 const struct elf_backend_data *bed;
9448 bfd_byte *symbuf;
9449 Elf_Internal_Shdr *hdr;
9450 file_ptr pos;
9451 bfd_boolean ret;
9452
9453 if (!hash_table->strtabcount)
9454 return TRUE;
9455
9456 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9457
9458 bed = get_elf_backend_data (flinfo->output_bfd);
9459
9460 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9461 symbuf = (bfd_byte *) bfd_malloc (amt);
9462 if (symbuf == NULL)
9463 return FALSE;
9464
9465 if (flinfo->symshndxbuf)
9466 {
9467 amt = sizeof (Elf_External_Sym_Shndx);
9468 amt *= bfd_get_symcount (flinfo->output_bfd);
9469 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9470 if (flinfo->symshndxbuf == NULL)
9471 {
9472 free (symbuf);
9473 return FALSE;
9474 }
9475 }
9476
9477 for (i = 0; i < hash_table->strtabcount; i++)
9478 {
9479 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9480 if (elfsym->sym.st_name == (unsigned long) -1)
9481 elfsym->sym.st_name = 0;
9482 else
9483 elfsym->sym.st_name
9484 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9485 elfsym->sym.st_name);
9486 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9487 ((bfd_byte *) symbuf
9488 + (elfsym->dest_index
9489 * bed->s->sizeof_sym)),
9490 (flinfo->symshndxbuf
9491 + elfsym->destshndx_index));
9492 }
9493
9494 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9495 pos = hdr->sh_offset + hdr->sh_size;
9496 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9497 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9498 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9499 {
9500 hdr->sh_size += amt;
9501 ret = TRUE;
9502 }
9503 else
9504 ret = FALSE;
9505
9506 free (symbuf);
9507
9508 free (hash_table->strtab);
9509 hash_table->strtab = NULL;
9510
9511 return ret;
9512 }
9513
9514 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9515
9516 static bfd_boolean
9517 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9518 {
9519 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9520 && sym->st_shndx < SHN_LORESERVE)
9521 {
9522 /* The gABI doesn't support dynamic symbols in output sections
9523 beyond 64k. */
9524 _bfd_error_handler
9525 /* xgettext:c-format */
9526 (_("%pB: too many sections: %d (>= %d)"),
9527 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9528 bfd_set_error (bfd_error_nonrepresentable_section);
9529 return FALSE;
9530 }
9531 return TRUE;
9532 }
9533
9534 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9535 allowing an unsatisfied unversioned symbol in the DSO to match a
9536 versioned symbol that would normally require an explicit version.
9537 We also handle the case that a DSO references a hidden symbol
9538 which may be satisfied by a versioned symbol in another DSO. */
9539
9540 static bfd_boolean
9541 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9542 const struct elf_backend_data *bed,
9543 struct elf_link_hash_entry *h)
9544 {
9545 bfd *abfd;
9546 struct elf_link_loaded_list *loaded;
9547
9548 if (!is_elf_hash_table (info->hash))
9549 return FALSE;
9550
9551 /* Check indirect symbol. */
9552 while (h->root.type == bfd_link_hash_indirect)
9553 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9554
9555 switch (h->root.type)
9556 {
9557 default:
9558 abfd = NULL;
9559 break;
9560
9561 case bfd_link_hash_undefined:
9562 case bfd_link_hash_undefweak:
9563 abfd = h->root.u.undef.abfd;
9564 if (abfd == NULL
9565 || (abfd->flags & DYNAMIC) == 0
9566 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9567 return FALSE;
9568 break;
9569
9570 case bfd_link_hash_defined:
9571 case bfd_link_hash_defweak:
9572 abfd = h->root.u.def.section->owner;
9573 break;
9574
9575 case bfd_link_hash_common:
9576 abfd = h->root.u.c.p->section->owner;
9577 break;
9578 }
9579 BFD_ASSERT (abfd != NULL);
9580
9581 for (loaded = elf_hash_table (info)->loaded;
9582 loaded != NULL;
9583 loaded = loaded->next)
9584 {
9585 bfd *input;
9586 Elf_Internal_Shdr *hdr;
9587 size_t symcount;
9588 size_t extsymcount;
9589 size_t extsymoff;
9590 Elf_Internal_Shdr *versymhdr;
9591 Elf_Internal_Sym *isym;
9592 Elf_Internal_Sym *isymend;
9593 Elf_Internal_Sym *isymbuf;
9594 Elf_External_Versym *ever;
9595 Elf_External_Versym *extversym;
9596
9597 input = loaded->abfd;
9598
9599 /* We check each DSO for a possible hidden versioned definition. */
9600 if (input == abfd
9601 || (input->flags & DYNAMIC) == 0
9602 || elf_dynversym (input) == 0)
9603 continue;
9604
9605 hdr = &elf_tdata (input)->dynsymtab_hdr;
9606
9607 symcount = hdr->sh_size / bed->s->sizeof_sym;
9608 if (elf_bad_symtab (input))
9609 {
9610 extsymcount = symcount;
9611 extsymoff = 0;
9612 }
9613 else
9614 {
9615 extsymcount = symcount - hdr->sh_info;
9616 extsymoff = hdr->sh_info;
9617 }
9618
9619 if (extsymcount == 0)
9620 continue;
9621
9622 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9623 NULL, NULL, NULL);
9624 if (isymbuf == NULL)
9625 return FALSE;
9626
9627 /* Read in any version definitions. */
9628 versymhdr = &elf_tdata (input)->dynversym_hdr;
9629 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
9630 if (extversym == NULL)
9631 goto error_ret;
9632
9633 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9634 || (bfd_bread (extversym, versymhdr->sh_size, input)
9635 != versymhdr->sh_size))
9636 {
9637 free (extversym);
9638 error_ret:
9639 free (isymbuf);
9640 return FALSE;
9641 }
9642
9643 ever = extversym + extsymoff;
9644 isymend = isymbuf + extsymcount;
9645 for (isym = isymbuf; isym < isymend; isym++, ever++)
9646 {
9647 const char *name;
9648 Elf_Internal_Versym iver;
9649 unsigned short version_index;
9650
9651 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9652 || isym->st_shndx == SHN_UNDEF)
9653 continue;
9654
9655 name = bfd_elf_string_from_elf_section (input,
9656 hdr->sh_link,
9657 isym->st_name);
9658 if (strcmp (name, h->root.root.string) != 0)
9659 continue;
9660
9661 _bfd_elf_swap_versym_in (input, ever, &iver);
9662
9663 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9664 && !(h->def_regular
9665 && h->forced_local))
9666 {
9667 /* If we have a non-hidden versioned sym, then it should
9668 have provided a definition for the undefined sym unless
9669 it is defined in a non-shared object and forced local.
9670 */
9671 abort ();
9672 }
9673
9674 version_index = iver.vs_vers & VERSYM_VERSION;
9675 if (version_index == 1 || version_index == 2)
9676 {
9677 /* This is the base or first version. We can use it. */
9678 free (extversym);
9679 free (isymbuf);
9680 return TRUE;
9681 }
9682 }
9683
9684 free (extversym);
9685 free (isymbuf);
9686 }
9687
9688 return FALSE;
9689 }
9690
9691 /* Convert ELF common symbol TYPE. */
9692
9693 static int
9694 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9695 {
9696 /* Commom symbol can only appear in relocatable link. */
9697 if (!bfd_link_relocatable (info))
9698 abort ();
9699 switch (info->elf_stt_common)
9700 {
9701 case unchanged:
9702 break;
9703 case elf_stt_common:
9704 type = STT_COMMON;
9705 break;
9706 case no_elf_stt_common:
9707 type = STT_OBJECT;
9708 break;
9709 }
9710 return type;
9711 }
9712
9713 /* Add an external symbol to the symbol table. This is called from
9714 the hash table traversal routine. When generating a shared object,
9715 we go through the symbol table twice. The first time we output
9716 anything that might have been forced to local scope in a version
9717 script. The second time we output the symbols that are still
9718 global symbols. */
9719
9720 static bfd_boolean
9721 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9722 {
9723 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9724 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9725 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9726 bfd_boolean strip;
9727 Elf_Internal_Sym sym;
9728 asection *input_sec;
9729 const struct elf_backend_data *bed;
9730 long indx;
9731 int ret;
9732 unsigned int type;
9733
9734 if (h->root.type == bfd_link_hash_warning)
9735 {
9736 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9737 if (h->root.type == bfd_link_hash_new)
9738 return TRUE;
9739 }
9740
9741 /* Decide whether to output this symbol in this pass. */
9742 if (eoinfo->localsyms)
9743 {
9744 if (!h->forced_local)
9745 return TRUE;
9746 }
9747 else
9748 {
9749 if (h->forced_local)
9750 return TRUE;
9751 }
9752
9753 bed = get_elf_backend_data (flinfo->output_bfd);
9754
9755 if (h->root.type == bfd_link_hash_undefined)
9756 {
9757 /* If we have an undefined symbol reference here then it must have
9758 come from a shared library that is being linked in. (Undefined
9759 references in regular files have already been handled unless
9760 they are in unreferenced sections which are removed by garbage
9761 collection). */
9762 bfd_boolean ignore_undef = FALSE;
9763
9764 /* Some symbols may be special in that the fact that they're
9765 undefined can be safely ignored - let backend determine that. */
9766 if (bed->elf_backend_ignore_undef_symbol)
9767 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9768
9769 /* If we are reporting errors for this situation then do so now. */
9770 if (!ignore_undef
9771 && h->ref_dynamic
9772 && (!h->ref_regular || flinfo->info->gc_sections)
9773 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9774 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9775 (*flinfo->info->callbacks->undefined_symbol)
9776 (flinfo->info, h->root.root.string,
9777 h->ref_regular ? NULL : h->root.u.undef.abfd,
9778 NULL, 0,
9779 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
9780
9781 /* Strip a global symbol defined in a discarded section. */
9782 if (h->indx == -3)
9783 return TRUE;
9784 }
9785
9786 /* We should also warn if a forced local symbol is referenced from
9787 shared libraries. */
9788 if (bfd_link_executable (flinfo->info)
9789 && h->forced_local
9790 && h->ref_dynamic
9791 && h->def_regular
9792 && !h->dynamic_def
9793 && h->ref_dynamic_nonweak
9794 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9795 {
9796 bfd *def_bfd;
9797 const char *msg;
9798 struct elf_link_hash_entry *hi = h;
9799
9800 /* Check indirect symbol. */
9801 while (hi->root.type == bfd_link_hash_indirect)
9802 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9803
9804 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9805 /* xgettext:c-format */
9806 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
9807 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9808 /* xgettext:c-format */
9809 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
9810 else
9811 /* xgettext:c-format */
9812 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
9813 def_bfd = flinfo->output_bfd;
9814 if (hi->root.u.def.section != bfd_abs_section_ptr)
9815 def_bfd = hi->root.u.def.section->owner;
9816 _bfd_error_handler (msg, flinfo->output_bfd,
9817 h->root.root.string, def_bfd);
9818 bfd_set_error (bfd_error_bad_value);
9819 eoinfo->failed = TRUE;
9820 return FALSE;
9821 }
9822
9823 /* We don't want to output symbols that have never been mentioned by
9824 a regular file, or that we have been told to strip. However, if
9825 h->indx is set to -2, the symbol is used by a reloc and we must
9826 output it. */
9827 strip = FALSE;
9828 if (h->indx == -2)
9829 ;
9830 else if ((h->def_dynamic
9831 || h->ref_dynamic
9832 || h->root.type == bfd_link_hash_new)
9833 && !h->def_regular
9834 && !h->ref_regular)
9835 strip = TRUE;
9836 else if (flinfo->info->strip == strip_all)
9837 strip = TRUE;
9838 else if (flinfo->info->strip == strip_some
9839 && bfd_hash_lookup (flinfo->info->keep_hash,
9840 h->root.root.string, FALSE, FALSE) == NULL)
9841 strip = TRUE;
9842 else if ((h->root.type == bfd_link_hash_defined
9843 || h->root.type == bfd_link_hash_defweak)
9844 && ((flinfo->info->strip_discarded
9845 && discarded_section (h->root.u.def.section))
9846 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9847 && h->root.u.def.section->owner != NULL
9848 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9849 strip = TRUE;
9850 else if ((h->root.type == bfd_link_hash_undefined
9851 || h->root.type == bfd_link_hash_undefweak)
9852 && h->root.u.undef.abfd != NULL
9853 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9854 strip = TRUE;
9855
9856 type = h->type;
9857
9858 /* If we're stripping it, and it's not a dynamic symbol, there's
9859 nothing else to do. However, if it is a forced local symbol or
9860 an ifunc symbol we need to give the backend finish_dynamic_symbol
9861 function a chance to make it dynamic. */
9862 if (strip
9863 && h->dynindx == -1
9864 && type != STT_GNU_IFUNC
9865 && !h->forced_local)
9866 return TRUE;
9867
9868 sym.st_value = 0;
9869 sym.st_size = h->size;
9870 sym.st_other = h->other;
9871 switch (h->root.type)
9872 {
9873 default:
9874 case bfd_link_hash_new:
9875 case bfd_link_hash_warning:
9876 abort ();
9877 return FALSE;
9878
9879 case bfd_link_hash_undefined:
9880 case bfd_link_hash_undefweak:
9881 input_sec = bfd_und_section_ptr;
9882 sym.st_shndx = SHN_UNDEF;
9883 break;
9884
9885 case bfd_link_hash_defined:
9886 case bfd_link_hash_defweak:
9887 {
9888 input_sec = h->root.u.def.section;
9889 if (input_sec->output_section != NULL)
9890 {
9891 sym.st_shndx =
9892 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9893 input_sec->output_section);
9894 if (sym.st_shndx == SHN_BAD)
9895 {
9896 _bfd_error_handler
9897 /* xgettext:c-format */
9898 (_("%pB: could not find output section %pA for input section %pA"),
9899 flinfo->output_bfd, input_sec->output_section, input_sec);
9900 bfd_set_error (bfd_error_nonrepresentable_section);
9901 eoinfo->failed = TRUE;
9902 return FALSE;
9903 }
9904
9905 /* ELF symbols in relocatable files are section relative,
9906 but in nonrelocatable files they are virtual
9907 addresses. */
9908 sym.st_value = h->root.u.def.value + input_sec->output_offset;
9909 if (!bfd_link_relocatable (flinfo->info))
9910 {
9911 sym.st_value += input_sec->output_section->vma;
9912 if (h->type == STT_TLS)
9913 {
9914 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9915 if (tls_sec != NULL)
9916 sym.st_value -= tls_sec->vma;
9917 }
9918 }
9919 }
9920 else
9921 {
9922 BFD_ASSERT (input_sec->owner == NULL
9923 || (input_sec->owner->flags & DYNAMIC) != 0);
9924 sym.st_shndx = SHN_UNDEF;
9925 input_sec = bfd_und_section_ptr;
9926 }
9927 }
9928 break;
9929
9930 case bfd_link_hash_common:
9931 input_sec = h->root.u.c.p->section;
9932 sym.st_shndx = bed->common_section_index (input_sec);
9933 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9934 break;
9935
9936 case bfd_link_hash_indirect:
9937 /* These symbols are created by symbol versioning. They point
9938 to the decorated version of the name. For example, if the
9939 symbol foo@@GNU_1.2 is the default, which should be used when
9940 foo is used with no version, then we add an indirect symbol
9941 foo which points to foo@@GNU_1.2. We ignore these symbols,
9942 since the indirected symbol is already in the hash table. */
9943 return TRUE;
9944 }
9945
9946 if (type == STT_COMMON || type == STT_OBJECT)
9947 switch (h->root.type)
9948 {
9949 case bfd_link_hash_common:
9950 type = elf_link_convert_common_type (flinfo->info, type);
9951 break;
9952 case bfd_link_hash_defined:
9953 case bfd_link_hash_defweak:
9954 if (bed->common_definition (&sym))
9955 type = elf_link_convert_common_type (flinfo->info, type);
9956 else
9957 type = STT_OBJECT;
9958 break;
9959 case bfd_link_hash_undefined:
9960 case bfd_link_hash_undefweak:
9961 break;
9962 default:
9963 abort ();
9964 }
9965
9966 if (h->forced_local)
9967 {
9968 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9969 /* Turn off visibility on local symbol. */
9970 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9971 }
9972 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9973 else if (h->unique_global && h->def_regular)
9974 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9975 else if (h->root.type == bfd_link_hash_undefweak
9976 || h->root.type == bfd_link_hash_defweak)
9977 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9978 else
9979 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9980 sym.st_target_internal = h->target_internal;
9981
9982 /* Give the processor backend a chance to tweak the symbol value,
9983 and also to finish up anything that needs to be done for this
9984 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9985 forced local syms when non-shared is due to a historical quirk.
9986 STT_GNU_IFUNC symbol must go through PLT. */
9987 if ((h->type == STT_GNU_IFUNC
9988 && h->def_regular
9989 && !bfd_link_relocatable (flinfo->info))
9990 || ((h->dynindx != -1
9991 || h->forced_local)
9992 && ((bfd_link_pic (flinfo->info)
9993 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9994 || h->root.type != bfd_link_hash_undefweak))
9995 || !h->forced_local)
9996 && elf_hash_table (flinfo->info)->dynamic_sections_created))
9997 {
9998 if (! ((*bed->elf_backend_finish_dynamic_symbol)
9999 (flinfo->output_bfd, flinfo->info, h, &sym)))
10000 {
10001 eoinfo->failed = TRUE;
10002 return FALSE;
10003 }
10004 }
10005
10006 /* If we are marking the symbol as undefined, and there are no
10007 non-weak references to this symbol from a regular object, then
10008 mark the symbol as weak undefined; if there are non-weak
10009 references, mark the symbol as strong. We can't do this earlier,
10010 because it might not be marked as undefined until the
10011 finish_dynamic_symbol routine gets through with it. */
10012 if (sym.st_shndx == SHN_UNDEF
10013 && h->ref_regular
10014 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10015 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10016 {
10017 int bindtype;
10018 type = ELF_ST_TYPE (sym.st_info);
10019
10020 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10021 if (type == STT_GNU_IFUNC)
10022 type = STT_FUNC;
10023
10024 if (h->ref_regular_nonweak)
10025 bindtype = STB_GLOBAL;
10026 else
10027 bindtype = STB_WEAK;
10028 sym.st_info = ELF_ST_INFO (bindtype, type);
10029 }
10030
10031 /* If this is a symbol defined in a dynamic library, don't use the
10032 symbol size from the dynamic library. Relinking an executable
10033 against a new library may introduce gratuitous changes in the
10034 executable's symbols if we keep the size. */
10035 if (sym.st_shndx == SHN_UNDEF
10036 && !h->def_regular
10037 && h->def_dynamic)
10038 sym.st_size = 0;
10039
10040 /* If a non-weak symbol with non-default visibility is not defined
10041 locally, it is a fatal error. */
10042 if (!bfd_link_relocatable (flinfo->info)
10043 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10044 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10045 && h->root.type == bfd_link_hash_undefined
10046 && !h->def_regular)
10047 {
10048 const char *msg;
10049
10050 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10051 /* xgettext:c-format */
10052 msg = _("%pB: protected symbol `%s' isn't defined");
10053 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10054 /* xgettext:c-format */
10055 msg = _("%pB: internal symbol `%s' isn't defined");
10056 else
10057 /* xgettext:c-format */
10058 msg = _("%pB: hidden symbol `%s' isn't defined");
10059 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10060 bfd_set_error (bfd_error_bad_value);
10061 eoinfo->failed = TRUE;
10062 return FALSE;
10063 }
10064
10065 /* If this symbol should be put in the .dynsym section, then put it
10066 there now. We already know the symbol index. We also fill in
10067 the entry in the .hash section. */
10068 if (h->dynindx != -1
10069 && elf_hash_table (flinfo->info)->dynamic_sections_created
10070 && elf_hash_table (flinfo->info)->dynsym != NULL
10071 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
10072 {
10073 bfd_byte *esym;
10074
10075 /* Since there is no version information in the dynamic string,
10076 if there is no version info in symbol version section, we will
10077 have a run-time problem if not linking executable, referenced
10078 by shared library, or not bound locally. */
10079 if (h->verinfo.verdef == NULL
10080 && (!bfd_link_executable (flinfo->info)
10081 || h->ref_dynamic
10082 || !h->def_regular))
10083 {
10084 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10085
10086 if (p && p [1] != '\0')
10087 {
10088 _bfd_error_handler
10089 /* xgettext:c-format */
10090 (_("%pB: no symbol version section for versioned symbol `%s'"),
10091 flinfo->output_bfd, h->root.root.string);
10092 eoinfo->failed = TRUE;
10093 return FALSE;
10094 }
10095 }
10096
10097 sym.st_name = h->dynstr_index;
10098 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10099 + h->dynindx * bed->s->sizeof_sym);
10100 if (!check_dynsym (flinfo->output_bfd, &sym))
10101 {
10102 eoinfo->failed = TRUE;
10103 return FALSE;
10104 }
10105 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
10106
10107 if (flinfo->hash_sec != NULL)
10108 {
10109 size_t hash_entry_size;
10110 bfd_byte *bucketpos;
10111 bfd_vma chain;
10112 size_t bucketcount;
10113 size_t bucket;
10114
10115 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
10116 bucket = h->u.elf_hash_value % bucketcount;
10117
10118 hash_entry_size
10119 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10120 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
10121 + (bucket + 2) * hash_entry_size);
10122 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10123 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10124 bucketpos);
10125 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10126 ((bfd_byte *) flinfo->hash_sec->contents
10127 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10128 }
10129
10130 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
10131 {
10132 Elf_Internal_Versym iversym;
10133 Elf_External_Versym *eversym;
10134
10135 if (!h->def_regular)
10136 {
10137 if (h->verinfo.verdef == NULL
10138 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10139 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
10140 iversym.vs_vers = 0;
10141 else
10142 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10143 }
10144 else
10145 {
10146 if (h->verinfo.vertree == NULL)
10147 iversym.vs_vers = 1;
10148 else
10149 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
10150 if (flinfo->info->create_default_symver)
10151 iversym.vs_vers++;
10152 }
10153
10154 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10155 defined locally. */
10156 if (h->versioned == versioned_hidden && h->def_regular)
10157 iversym.vs_vers |= VERSYM_HIDDEN;
10158
10159 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10160 eversym += h->dynindx;
10161 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10162 }
10163 }
10164
10165 /* If the symbol is undefined, and we didn't output it to .dynsym,
10166 strip it from .symtab too. Obviously we can't do this for
10167 relocatable output or when needed for --emit-relocs. */
10168 else if (input_sec == bfd_und_section_ptr
10169 && h->indx != -2
10170 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10171 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10172 && !bfd_link_relocatable (flinfo->info))
10173 return TRUE;
10174
10175 /* Also strip others that we couldn't earlier due to dynamic symbol
10176 processing. */
10177 if (strip)
10178 return TRUE;
10179 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10180 return TRUE;
10181
10182 /* Output a FILE symbol so that following locals are not associated
10183 with the wrong input file. We need one for forced local symbols
10184 if we've seen more than one FILE symbol or when we have exactly
10185 one FILE symbol but global symbols are present in a file other
10186 than the one with the FILE symbol. We also need one if linker
10187 defined symbols are present. In practice these conditions are
10188 always met, so just emit the FILE symbol unconditionally. */
10189 if (eoinfo->localsyms
10190 && !eoinfo->file_sym_done
10191 && eoinfo->flinfo->filesym_count != 0)
10192 {
10193 Elf_Internal_Sym fsym;
10194
10195 memset (&fsym, 0, sizeof (fsym));
10196 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10197 fsym.st_shndx = SHN_ABS;
10198 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10199 bfd_und_section_ptr, NULL))
10200 return FALSE;
10201
10202 eoinfo->file_sym_done = TRUE;
10203 }
10204
10205 indx = bfd_get_symcount (flinfo->output_bfd);
10206 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10207 input_sec, h);
10208 if (ret == 0)
10209 {
10210 eoinfo->failed = TRUE;
10211 return FALSE;
10212 }
10213 else if (ret == 1)
10214 h->indx = indx;
10215 else if (h->indx == -2)
10216 abort();
10217
10218 return TRUE;
10219 }
10220
10221 /* Return TRUE if special handling is done for relocs in SEC against
10222 symbols defined in discarded sections. */
10223
10224 static bfd_boolean
10225 elf_section_ignore_discarded_relocs (asection *sec)
10226 {
10227 const struct elf_backend_data *bed;
10228
10229 switch (sec->sec_info_type)
10230 {
10231 case SEC_INFO_TYPE_STABS:
10232 case SEC_INFO_TYPE_EH_FRAME:
10233 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10234 return TRUE;
10235 default:
10236 break;
10237 }
10238
10239 bed = get_elf_backend_data (sec->owner);
10240 if (bed->elf_backend_ignore_discarded_relocs != NULL
10241 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10242 return TRUE;
10243
10244 return FALSE;
10245 }
10246
10247 /* Return a mask saying how ld should treat relocations in SEC against
10248 symbols defined in discarded sections. If this function returns
10249 COMPLAIN set, ld will issue a warning message. If this function
10250 returns PRETEND set, and the discarded section was link-once and the
10251 same size as the kept link-once section, ld will pretend that the
10252 symbol was actually defined in the kept section. Otherwise ld will
10253 zero the reloc (at least that is the intent, but some cooperation by
10254 the target dependent code is needed, particularly for REL targets). */
10255
10256 unsigned int
10257 _bfd_elf_default_action_discarded (asection *sec)
10258 {
10259 if (sec->flags & SEC_DEBUGGING)
10260 return PRETEND;
10261
10262 if (strcmp (".eh_frame", sec->name) == 0)
10263 return 0;
10264
10265 if (strcmp (".gcc_except_table", sec->name) == 0)
10266 return 0;
10267
10268 return COMPLAIN | PRETEND;
10269 }
10270
10271 /* Find a match between a section and a member of a section group. */
10272
10273 static asection *
10274 match_group_member (asection *sec, asection *group,
10275 struct bfd_link_info *info)
10276 {
10277 asection *first = elf_next_in_group (group);
10278 asection *s = first;
10279
10280 while (s != NULL)
10281 {
10282 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10283 return s;
10284
10285 s = elf_next_in_group (s);
10286 if (s == first)
10287 break;
10288 }
10289
10290 return NULL;
10291 }
10292
10293 /* Check if the kept section of a discarded section SEC can be used
10294 to replace it. Return the replacement if it is OK. Otherwise return
10295 NULL. */
10296
10297 asection *
10298 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10299 {
10300 asection *kept;
10301
10302 kept = sec->kept_section;
10303 if (kept != NULL)
10304 {
10305 if ((kept->flags & SEC_GROUP) != 0)
10306 kept = match_group_member (sec, kept, info);
10307 if (kept != NULL
10308 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10309 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
10310 kept = NULL;
10311 sec->kept_section = kept;
10312 }
10313 return kept;
10314 }
10315
10316 /* Link an input file into the linker output file. This function
10317 handles all the sections and relocations of the input file at once.
10318 This is so that we only have to read the local symbols once, and
10319 don't have to keep them in memory. */
10320
10321 static bfd_boolean
10322 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
10323 {
10324 int (*relocate_section)
10325 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10326 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10327 bfd *output_bfd;
10328 Elf_Internal_Shdr *symtab_hdr;
10329 size_t locsymcount;
10330 size_t extsymoff;
10331 Elf_Internal_Sym *isymbuf;
10332 Elf_Internal_Sym *isym;
10333 Elf_Internal_Sym *isymend;
10334 long *pindex;
10335 asection **ppsection;
10336 asection *o;
10337 const struct elf_backend_data *bed;
10338 struct elf_link_hash_entry **sym_hashes;
10339 bfd_size_type address_size;
10340 bfd_vma r_type_mask;
10341 int r_sym_shift;
10342 bfd_boolean have_file_sym = FALSE;
10343
10344 output_bfd = flinfo->output_bfd;
10345 bed = get_elf_backend_data (output_bfd);
10346 relocate_section = bed->elf_backend_relocate_section;
10347
10348 /* If this is a dynamic object, we don't want to do anything here:
10349 we don't want the local symbols, and we don't want the section
10350 contents. */
10351 if ((input_bfd->flags & DYNAMIC) != 0)
10352 return TRUE;
10353
10354 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10355 if (elf_bad_symtab (input_bfd))
10356 {
10357 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10358 extsymoff = 0;
10359 }
10360 else
10361 {
10362 locsymcount = symtab_hdr->sh_info;
10363 extsymoff = symtab_hdr->sh_info;
10364 }
10365
10366 /* Read the local symbols. */
10367 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10368 if (isymbuf == NULL && locsymcount != 0)
10369 {
10370 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
10371 flinfo->internal_syms,
10372 flinfo->external_syms,
10373 flinfo->locsym_shndx);
10374 if (isymbuf == NULL)
10375 return FALSE;
10376 }
10377
10378 /* Find local symbol sections and adjust values of symbols in
10379 SEC_MERGE sections. Write out those local symbols we know are
10380 going into the output file. */
10381 isymend = isymbuf + locsymcount;
10382 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
10383 isym < isymend;
10384 isym++, pindex++, ppsection++)
10385 {
10386 asection *isec;
10387 const char *name;
10388 Elf_Internal_Sym osym;
10389 long indx;
10390 int ret;
10391
10392 *pindex = -1;
10393
10394 if (elf_bad_symtab (input_bfd))
10395 {
10396 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10397 {
10398 *ppsection = NULL;
10399 continue;
10400 }
10401 }
10402
10403 if (isym->st_shndx == SHN_UNDEF)
10404 isec = bfd_und_section_ptr;
10405 else if (isym->st_shndx == SHN_ABS)
10406 isec = bfd_abs_section_ptr;
10407 else if (isym->st_shndx == SHN_COMMON)
10408 isec = bfd_com_section_ptr;
10409 else
10410 {
10411 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10412 if (isec == NULL)
10413 {
10414 /* Don't attempt to output symbols with st_shnx in the
10415 reserved range other than SHN_ABS and SHN_COMMON. */
10416 *ppsection = NULL;
10417 continue;
10418 }
10419 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
10420 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10421 isym->st_value =
10422 _bfd_merged_section_offset (output_bfd, &isec,
10423 elf_section_data (isec)->sec_info,
10424 isym->st_value);
10425 }
10426
10427 *ppsection = isec;
10428
10429 /* Don't output the first, undefined, symbol. In fact, don't
10430 output any undefined local symbol. */
10431 if (isec == bfd_und_section_ptr)
10432 continue;
10433
10434 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10435 {
10436 /* We never output section symbols. Instead, we use the
10437 section symbol of the corresponding section in the output
10438 file. */
10439 continue;
10440 }
10441
10442 /* If we are stripping all symbols, we don't want to output this
10443 one. */
10444 if (flinfo->info->strip == strip_all)
10445 continue;
10446
10447 /* If we are discarding all local symbols, we don't want to
10448 output this one. If we are generating a relocatable output
10449 file, then some of the local symbols may be required by
10450 relocs; we output them below as we discover that they are
10451 needed. */
10452 if (flinfo->info->discard == discard_all)
10453 continue;
10454
10455 /* If this symbol is defined in a section which we are
10456 discarding, we don't need to keep it. */
10457 if (isym->st_shndx != SHN_UNDEF
10458 && isym->st_shndx < SHN_LORESERVE
10459 && bfd_section_removed_from_list (output_bfd,
10460 isec->output_section))
10461 continue;
10462
10463 /* Get the name of the symbol. */
10464 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10465 isym->st_name);
10466 if (name == NULL)
10467 return FALSE;
10468
10469 /* See if we are discarding symbols with this name. */
10470 if ((flinfo->info->strip == strip_some
10471 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
10472 == NULL))
10473 || (((flinfo->info->discard == discard_sec_merge
10474 && (isec->flags & SEC_MERGE)
10475 && !bfd_link_relocatable (flinfo->info))
10476 || flinfo->info->discard == discard_l)
10477 && bfd_is_local_label_name (input_bfd, name)))
10478 continue;
10479
10480 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10481 {
10482 if (input_bfd->lto_output)
10483 /* -flto puts a temp file name here. This means builds
10484 are not reproducible. Discard the symbol. */
10485 continue;
10486 have_file_sym = TRUE;
10487 flinfo->filesym_count += 1;
10488 }
10489 if (!have_file_sym)
10490 {
10491 /* In the absence of debug info, bfd_find_nearest_line uses
10492 FILE symbols to determine the source file for local
10493 function symbols. Provide a FILE symbol here if input
10494 files lack such, so that their symbols won't be
10495 associated with a previous input file. It's not the
10496 source file, but the best we can do. */
10497 have_file_sym = TRUE;
10498 flinfo->filesym_count += 1;
10499 memset (&osym, 0, sizeof (osym));
10500 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10501 osym.st_shndx = SHN_ABS;
10502 if (!elf_link_output_symstrtab (flinfo,
10503 (input_bfd->lto_output ? NULL
10504 : input_bfd->filename),
10505 &osym, bfd_abs_section_ptr,
10506 NULL))
10507 return FALSE;
10508 }
10509
10510 osym = *isym;
10511
10512 /* Adjust the section index for the output file. */
10513 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10514 isec->output_section);
10515 if (osym.st_shndx == SHN_BAD)
10516 return FALSE;
10517
10518 /* ELF symbols in relocatable files are section relative, but
10519 in executable files they are virtual addresses. Note that
10520 this code assumes that all ELF sections have an associated
10521 BFD section with a reasonable value for output_offset; below
10522 we assume that they also have a reasonable value for
10523 output_section. Any special sections must be set up to meet
10524 these requirements. */
10525 osym.st_value += isec->output_offset;
10526 if (!bfd_link_relocatable (flinfo->info))
10527 {
10528 osym.st_value += isec->output_section->vma;
10529 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10530 {
10531 /* STT_TLS symbols are relative to PT_TLS segment base. */
10532 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
10533 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10534 else
10535 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
10536 STT_NOTYPE);
10537 }
10538 }
10539
10540 indx = bfd_get_symcount (output_bfd);
10541 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10542 if (ret == 0)
10543 return FALSE;
10544 else if (ret == 1)
10545 *pindex = indx;
10546 }
10547
10548 if (bed->s->arch_size == 32)
10549 {
10550 r_type_mask = 0xff;
10551 r_sym_shift = 8;
10552 address_size = 4;
10553 }
10554 else
10555 {
10556 r_type_mask = 0xffffffff;
10557 r_sym_shift = 32;
10558 address_size = 8;
10559 }
10560
10561 /* Relocate the contents of each section. */
10562 sym_hashes = elf_sym_hashes (input_bfd);
10563 for (o = input_bfd->sections; o != NULL; o = o->next)
10564 {
10565 bfd_byte *contents;
10566
10567 if (! o->linker_mark)
10568 {
10569 /* This section was omitted from the link. */
10570 continue;
10571 }
10572
10573 if (!flinfo->info->resolve_section_groups
10574 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10575 {
10576 /* Deal with the group signature symbol. */
10577 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10578 unsigned long symndx = sec_data->this_hdr.sh_info;
10579 asection *osec = o->output_section;
10580
10581 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
10582 if (symndx >= locsymcount
10583 || (elf_bad_symtab (input_bfd)
10584 && flinfo->sections[symndx] == NULL))
10585 {
10586 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10587 while (h->root.type == bfd_link_hash_indirect
10588 || h->root.type == bfd_link_hash_warning)
10589 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10590 /* Arrange for symbol to be output. */
10591 h->indx = -2;
10592 elf_section_data (osec)->this_hdr.sh_info = -2;
10593 }
10594 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10595 {
10596 /* We'll use the output section target_index. */
10597 asection *sec = flinfo->sections[symndx]->output_section;
10598 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10599 }
10600 else
10601 {
10602 if (flinfo->indices[symndx] == -1)
10603 {
10604 /* Otherwise output the local symbol now. */
10605 Elf_Internal_Sym sym = isymbuf[symndx];
10606 asection *sec = flinfo->sections[symndx]->output_section;
10607 const char *name;
10608 long indx;
10609 int ret;
10610
10611 name = bfd_elf_string_from_elf_section (input_bfd,
10612 symtab_hdr->sh_link,
10613 sym.st_name);
10614 if (name == NULL)
10615 return FALSE;
10616
10617 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10618 sec);
10619 if (sym.st_shndx == SHN_BAD)
10620 return FALSE;
10621
10622 sym.st_value += o->output_offset;
10623
10624 indx = bfd_get_symcount (output_bfd);
10625 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10626 NULL);
10627 if (ret == 0)
10628 return FALSE;
10629 else if (ret == 1)
10630 flinfo->indices[symndx] = indx;
10631 else
10632 abort ();
10633 }
10634 elf_section_data (osec)->this_hdr.sh_info
10635 = flinfo->indices[symndx];
10636 }
10637 }
10638
10639 if ((o->flags & SEC_HAS_CONTENTS) == 0
10640 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10641 continue;
10642
10643 if ((o->flags & SEC_LINKER_CREATED) != 0)
10644 {
10645 /* Section was created by _bfd_elf_link_create_dynamic_sections
10646 or somesuch. */
10647 continue;
10648 }
10649
10650 /* Get the contents of the section. They have been cached by a
10651 relaxation routine. Note that o is a section in an input
10652 file, so the contents field will not have been set by any of
10653 the routines which work on output files. */
10654 if (elf_section_data (o)->this_hdr.contents != NULL)
10655 {
10656 contents = elf_section_data (o)->this_hdr.contents;
10657 if (bed->caches_rawsize
10658 && o->rawsize != 0
10659 && o->rawsize < o->size)
10660 {
10661 memcpy (flinfo->contents, contents, o->rawsize);
10662 contents = flinfo->contents;
10663 }
10664 }
10665 else
10666 {
10667 contents = flinfo->contents;
10668 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10669 return FALSE;
10670 }
10671
10672 if ((o->flags & SEC_RELOC) != 0)
10673 {
10674 Elf_Internal_Rela *internal_relocs;
10675 Elf_Internal_Rela *rel, *relend;
10676 int action_discarded;
10677 int ret;
10678
10679 /* Get the swapped relocs. */
10680 internal_relocs
10681 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10682 flinfo->internal_relocs, FALSE);
10683 if (internal_relocs == NULL
10684 && o->reloc_count > 0)
10685 return FALSE;
10686
10687 /* We need to reverse-copy input .ctors/.dtors sections if
10688 they are placed in .init_array/.finit_array for output. */
10689 if (o->size > address_size
10690 && ((strncmp (o->name, ".ctors", 6) == 0
10691 && strcmp (o->output_section->name,
10692 ".init_array") == 0)
10693 || (strncmp (o->name, ".dtors", 6) == 0
10694 && strcmp (o->output_section->name,
10695 ".fini_array") == 0))
10696 && (o->name[6] == 0 || o->name[6] == '.'))
10697 {
10698 if (o->size * bed->s->int_rels_per_ext_rel
10699 != o->reloc_count * address_size)
10700 {
10701 _bfd_error_handler
10702 /* xgettext:c-format */
10703 (_("error: %pB: size of section %pA is not "
10704 "multiple of address size"),
10705 input_bfd, o);
10706 bfd_set_error (bfd_error_bad_value);
10707 return FALSE;
10708 }
10709 o->flags |= SEC_ELF_REVERSE_COPY;
10710 }
10711
10712 action_discarded = -1;
10713 if (!elf_section_ignore_discarded_relocs (o))
10714 action_discarded = (*bed->action_discarded) (o);
10715
10716 /* Run through the relocs evaluating complex reloc symbols and
10717 looking for relocs against symbols from discarded sections
10718 or section symbols from removed link-once sections.
10719 Complain about relocs against discarded sections. Zero
10720 relocs against removed link-once sections. */
10721
10722 rel = internal_relocs;
10723 relend = rel + o->reloc_count;
10724 for ( ; rel < relend; rel++)
10725 {
10726 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10727 unsigned int s_type;
10728 asection **ps, *sec;
10729 struct elf_link_hash_entry *h = NULL;
10730 const char *sym_name;
10731
10732 if (r_symndx == STN_UNDEF)
10733 continue;
10734
10735 if (r_symndx >= locsymcount
10736 || (elf_bad_symtab (input_bfd)
10737 && flinfo->sections[r_symndx] == NULL))
10738 {
10739 h = sym_hashes[r_symndx - extsymoff];
10740
10741 /* Badly formatted input files can contain relocs that
10742 reference non-existant symbols. Check here so that
10743 we do not seg fault. */
10744 if (h == NULL)
10745 {
10746 _bfd_error_handler
10747 /* xgettext:c-format */
10748 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
10749 "that references a non-existent global symbol"),
10750 input_bfd, (uint64_t) rel->r_info, o);
10751 bfd_set_error (bfd_error_bad_value);
10752 return FALSE;
10753 }
10754
10755 while (h->root.type == bfd_link_hash_indirect
10756 || h->root.type == bfd_link_hash_warning)
10757 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10758
10759 s_type = h->type;
10760
10761 /* If a plugin symbol is referenced from a non-IR file,
10762 mark the symbol as undefined. Note that the
10763 linker may attach linker created dynamic sections
10764 to the plugin bfd. Symbols defined in linker
10765 created sections are not plugin symbols. */
10766 if ((h->root.non_ir_ref_regular
10767 || h->root.non_ir_ref_dynamic)
10768 && (h->root.type == bfd_link_hash_defined
10769 || h->root.type == bfd_link_hash_defweak)
10770 && (h->root.u.def.section->flags
10771 & SEC_LINKER_CREATED) == 0
10772 && h->root.u.def.section->owner != NULL
10773 && (h->root.u.def.section->owner->flags
10774 & BFD_PLUGIN) != 0)
10775 {
10776 h->root.type = bfd_link_hash_undefined;
10777 h->root.u.undef.abfd = h->root.u.def.section->owner;
10778 }
10779
10780 ps = NULL;
10781 if (h->root.type == bfd_link_hash_defined
10782 || h->root.type == bfd_link_hash_defweak)
10783 ps = &h->root.u.def.section;
10784
10785 sym_name = h->root.root.string;
10786 }
10787 else
10788 {
10789 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10790
10791 s_type = ELF_ST_TYPE (sym->st_info);
10792 ps = &flinfo->sections[r_symndx];
10793 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10794 sym, *ps);
10795 }
10796
10797 if ((s_type == STT_RELC || s_type == STT_SRELC)
10798 && !bfd_link_relocatable (flinfo->info))
10799 {
10800 bfd_vma val;
10801 bfd_vma dot = (rel->r_offset
10802 + o->output_offset + o->output_section->vma);
10803 #ifdef DEBUG
10804 printf ("Encountered a complex symbol!");
10805 printf (" (input_bfd %s, section %s, reloc %ld\n",
10806 input_bfd->filename, o->name,
10807 (long) (rel - internal_relocs));
10808 printf (" symbol: idx %8.8lx, name %s\n",
10809 r_symndx, sym_name);
10810 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10811 (unsigned long) rel->r_info,
10812 (unsigned long) rel->r_offset);
10813 #endif
10814 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10815 isymbuf, locsymcount, s_type == STT_SRELC))
10816 return FALSE;
10817
10818 /* Symbol evaluated OK. Update to absolute value. */
10819 set_symbol_value (input_bfd, isymbuf, locsymcount,
10820 r_symndx, val);
10821 continue;
10822 }
10823
10824 if (action_discarded != -1 && ps != NULL)
10825 {
10826 /* Complain if the definition comes from a
10827 discarded section. */
10828 if ((sec = *ps) != NULL && discarded_section (sec))
10829 {
10830 BFD_ASSERT (r_symndx != STN_UNDEF);
10831 if (action_discarded & COMPLAIN)
10832 (*flinfo->info->callbacks->einfo)
10833 /* xgettext:c-format */
10834 (_("%X`%s' referenced in section `%pA' of %pB: "
10835 "defined in discarded section `%pA' of %pB\n"),
10836 sym_name, o, input_bfd, sec, sec->owner);
10837
10838 /* Try to do the best we can to support buggy old
10839 versions of gcc. Pretend that the symbol is
10840 really defined in the kept linkonce section.
10841 FIXME: This is quite broken. Modifying the
10842 symbol here means we will be changing all later
10843 uses of the symbol, not just in this section. */
10844 if (action_discarded & PRETEND)
10845 {
10846 asection *kept;
10847
10848 kept = _bfd_elf_check_kept_section (sec,
10849 flinfo->info);
10850 if (kept != NULL)
10851 {
10852 *ps = kept;
10853 continue;
10854 }
10855 }
10856 }
10857 }
10858 }
10859
10860 /* Relocate the section by invoking a back end routine.
10861
10862 The back end routine is responsible for adjusting the
10863 section contents as necessary, and (if using Rela relocs
10864 and generating a relocatable output file) adjusting the
10865 reloc addend as necessary.
10866
10867 The back end routine does not have to worry about setting
10868 the reloc address or the reloc symbol index.
10869
10870 The back end routine is given a pointer to the swapped in
10871 internal symbols, and can access the hash table entries
10872 for the external symbols via elf_sym_hashes (input_bfd).
10873
10874 When generating relocatable output, the back end routine
10875 must handle STB_LOCAL/STT_SECTION symbols specially. The
10876 output symbol is going to be a section symbol
10877 corresponding to the output section, which will require
10878 the addend to be adjusted. */
10879
10880 ret = (*relocate_section) (output_bfd, flinfo->info,
10881 input_bfd, o, contents,
10882 internal_relocs,
10883 isymbuf,
10884 flinfo->sections);
10885 if (!ret)
10886 return FALSE;
10887
10888 if (ret == 2
10889 || bfd_link_relocatable (flinfo->info)
10890 || flinfo->info->emitrelocations)
10891 {
10892 Elf_Internal_Rela *irela;
10893 Elf_Internal_Rela *irelaend, *irelamid;
10894 bfd_vma last_offset;
10895 struct elf_link_hash_entry **rel_hash;
10896 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10897 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10898 unsigned int next_erel;
10899 bfd_boolean rela_normal;
10900 struct bfd_elf_section_data *esdi, *esdo;
10901
10902 esdi = elf_section_data (o);
10903 esdo = elf_section_data (o->output_section);
10904 rela_normal = FALSE;
10905
10906 /* Adjust the reloc addresses and symbol indices. */
10907
10908 irela = internal_relocs;
10909 irelaend = irela + o->reloc_count;
10910 rel_hash = esdo->rel.hashes + esdo->rel.count;
10911 /* We start processing the REL relocs, if any. When we reach
10912 IRELAMID in the loop, we switch to the RELA relocs. */
10913 irelamid = irela;
10914 if (esdi->rel.hdr != NULL)
10915 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10916 * bed->s->int_rels_per_ext_rel);
10917 rel_hash_list = rel_hash;
10918 rela_hash_list = NULL;
10919 last_offset = o->output_offset;
10920 if (!bfd_link_relocatable (flinfo->info))
10921 last_offset += o->output_section->vma;
10922 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10923 {
10924 unsigned long r_symndx;
10925 asection *sec;
10926 Elf_Internal_Sym sym;
10927
10928 if (next_erel == bed->s->int_rels_per_ext_rel)
10929 {
10930 rel_hash++;
10931 next_erel = 0;
10932 }
10933
10934 if (irela == irelamid)
10935 {
10936 rel_hash = esdo->rela.hashes + esdo->rela.count;
10937 rela_hash_list = rel_hash;
10938 rela_normal = bed->rela_normal;
10939 }
10940
10941 irela->r_offset = _bfd_elf_section_offset (output_bfd,
10942 flinfo->info, o,
10943 irela->r_offset);
10944 if (irela->r_offset >= (bfd_vma) -2)
10945 {
10946 /* This is a reloc for a deleted entry or somesuch.
10947 Turn it into an R_*_NONE reloc, at the same
10948 offset as the last reloc. elf_eh_frame.c and
10949 bfd_elf_discard_info rely on reloc offsets
10950 being ordered. */
10951 irela->r_offset = last_offset;
10952 irela->r_info = 0;
10953 irela->r_addend = 0;
10954 continue;
10955 }
10956
10957 irela->r_offset += o->output_offset;
10958
10959 /* Relocs in an executable have to be virtual addresses. */
10960 if (!bfd_link_relocatable (flinfo->info))
10961 irela->r_offset += o->output_section->vma;
10962
10963 last_offset = irela->r_offset;
10964
10965 r_symndx = irela->r_info >> r_sym_shift;
10966 if (r_symndx == STN_UNDEF)
10967 continue;
10968
10969 if (r_symndx >= locsymcount
10970 || (elf_bad_symtab (input_bfd)
10971 && flinfo->sections[r_symndx] == NULL))
10972 {
10973 struct elf_link_hash_entry *rh;
10974 unsigned long indx;
10975
10976 /* This is a reloc against a global symbol. We
10977 have not yet output all the local symbols, so
10978 we do not know the symbol index of any global
10979 symbol. We set the rel_hash entry for this
10980 reloc to point to the global hash table entry
10981 for this symbol. The symbol index is then
10982 set at the end of bfd_elf_final_link. */
10983 indx = r_symndx - extsymoff;
10984 rh = elf_sym_hashes (input_bfd)[indx];
10985 while (rh->root.type == bfd_link_hash_indirect
10986 || rh->root.type == bfd_link_hash_warning)
10987 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10988
10989 /* Setting the index to -2 tells
10990 elf_link_output_extsym that this symbol is
10991 used by a reloc. */
10992 BFD_ASSERT (rh->indx < 0);
10993 rh->indx = -2;
10994 *rel_hash = rh;
10995
10996 continue;
10997 }
10998
10999 /* This is a reloc against a local symbol. */
11000
11001 *rel_hash = NULL;
11002 sym = isymbuf[r_symndx];
11003 sec = flinfo->sections[r_symndx];
11004 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11005 {
11006 /* I suppose the backend ought to fill in the
11007 section of any STT_SECTION symbol against a
11008 processor specific section. */
11009 r_symndx = STN_UNDEF;
11010 if (bfd_is_abs_section (sec))
11011 ;
11012 else if (sec == NULL || sec->owner == NULL)
11013 {
11014 bfd_set_error (bfd_error_bad_value);
11015 return FALSE;
11016 }
11017 else
11018 {
11019 asection *osec = sec->output_section;
11020
11021 /* If we have discarded a section, the output
11022 section will be the absolute section. In
11023 case of discarded SEC_MERGE sections, use
11024 the kept section. relocate_section should
11025 have already handled discarded linkonce
11026 sections. */
11027 if (bfd_is_abs_section (osec)
11028 && sec->kept_section != NULL
11029 && sec->kept_section->output_section != NULL)
11030 {
11031 osec = sec->kept_section->output_section;
11032 irela->r_addend -= osec->vma;
11033 }
11034
11035 if (!bfd_is_abs_section (osec))
11036 {
11037 r_symndx = osec->target_index;
11038 if (r_symndx == STN_UNDEF)
11039 {
11040 irela->r_addend += osec->vma;
11041 osec = _bfd_nearby_section (output_bfd, osec,
11042 osec->vma);
11043 irela->r_addend -= osec->vma;
11044 r_symndx = osec->target_index;
11045 }
11046 }
11047 }
11048
11049 /* Adjust the addend according to where the
11050 section winds up in the output section. */
11051 if (rela_normal)
11052 irela->r_addend += sec->output_offset;
11053 }
11054 else
11055 {
11056 if (flinfo->indices[r_symndx] == -1)
11057 {
11058 unsigned long shlink;
11059 const char *name;
11060 asection *osec;
11061 long indx;
11062
11063 if (flinfo->info->strip == strip_all)
11064 {
11065 /* You can't do ld -r -s. */
11066 bfd_set_error (bfd_error_invalid_operation);
11067 return FALSE;
11068 }
11069
11070 /* This symbol was skipped earlier, but
11071 since it is needed by a reloc, we
11072 must output it now. */
11073 shlink = symtab_hdr->sh_link;
11074 name = (bfd_elf_string_from_elf_section
11075 (input_bfd, shlink, sym.st_name));
11076 if (name == NULL)
11077 return FALSE;
11078
11079 osec = sec->output_section;
11080 sym.st_shndx =
11081 _bfd_elf_section_from_bfd_section (output_bfd,
11082 osec);
11083 if (sym.st_shndx == SHN_BAD)
11084 return FALSE;
11085
11086 sym.st_value += sec->output_offset;
11087 if (!bfd_link_relocatable (flinfo->info))
11088 {
11089 sym.st_value += osec->vma;
11090 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11091 {
11092 struct elf_link_hash_table *htab
11093 = elf_hash_table (flinfo->info);
11094
11095 /* STT_TLS symbols are relative to PT_TLS
11096 segment base. */
11097 if (htab->tls_sec != NULL)
11098 sym.st_value -= htab->tls_sec->vma;
11099 else
11100 sym.st_info
11101 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11102 STT_NOTYPE);
11103 }
11104 }
11105
11106 indx = bfd_get_symcount (output_bfd);
11107 ret = elf_link_output_symstrtab (flinfo, name,
11108 &sym, sec,
11109 NULL);
11110 if (ret == 0)
11111 return FALSE;
11112 else if (ret == 1)
11113 flinfo->indices[r_symndx] = indx;
11114 else
11115 abort ();
11116 }
11117
11118 r_symndx = flinfo->indices[r_symndx];
11119 }
11120
11121 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11122 | (irela->r_info & r_type_mask));
11123 }
11124
11125 /* Swap out the relocs. */
11126 input_rel_hdr = esdi->rel.hdr;
11127 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
11128 {
11129 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11130 input_rel_hdr,
11131 internal_relocs,
11132 rel_hash_list))
11133 return FALSE;
11134 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11135 * bed->s->int_rels_per_ext_rel);
11136 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
11137 }
11138
11139 input_rela_hdr = esdi->rela.hdr;
11140 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11141 {
11142 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11143 input_rela_hdr,
11144 internal_relocs,
11145 rela_hash_list))
11146 return FALSE;
11147 }
11148 }
11149 }
11150
11151 /* Write out the modified section contents. */
11152 if (bed->elf_backend_write_section
11153 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
11154 contents))
11155 {
11156 /* Section written out. */
11157 }
11158 else switch (o->sec_info_type)
11159 {
11160 case SEC_INFO_TYPE_STABS:
11161 if (! (_bfd_write_section_stabs
11162 (output_bfd,
11163 &elf_hash_table (flinfo->info)->stab_info,
11164 o, &elf_section_data (o)->sec_info, contents)))
11165 return FALSE;
11166 break;
11167 case SEC_INFO_TYPE_MERGE:
11168 if (! _bfd_write_merged_section (output_bfd, o,
11169 elf_section_data (o)->sec_info))
11170 return FALSE;
11171 break;
11172 case SEC_INFO_TYPE_EH_FRAME:
11173 {
11174 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11175 o, contents))
11176 return FALSE;
11177 }
11178 break;
11179 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11180 {
11181 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11182 flinfo->info,
11183 o, contents))
11184 return FALSE;
11185 }
11186 break;
11187 default:
11188 {
11189 if (! (o->flags & SEC_EXCLUDE))
11190 {
11191 file_ptr offset = (file_ptr) o->output_offset;
11192 bfd_size_type todo = o->size;
11193
11194 offset *= bfd_octets_per_byte (output_bfd);
11195
11196 if ((o->flags & SEC_ELF_REVERSE_COPY))
11197 {
11198 /* Reverse-copy input section to output. */
11199 do
11200 {
11201 todo -= address_size;
11202 if (! bfd_set_section_contents (output_bfd,
11203 o->output_section,
11204 contents + todo,
11205 offset,
11206 address_size))
11207 return FALSE;
11208 if (todo == 0)
11209 break;
11210 offset += address_size;
11211 }
11212 while (1);
11213 }
11214 else if (! bfd_set_section_contents (output_bfd,
11215 o->output_section,
11216 contents,
11217 offset, todo))
11218 return FALSE;
11219 }
11220 }
11221 break;
11222 }
11223 }
11224
11225 return TRUE;
11226 }
11227
11228 /* Generate a reloc when linking an ELF file. This is a reloc
11229 requested by the linker, and does not come from any input file. This
11230 is used to build constructor and destructor tables when linking
11231 with -Ur. */
11232
11233 static bfd_boolean
11234 elf_reloc_link_order (bfd *output_bfd,
11235 struct bfd_link_info *info,
11236 asection *output_section,
11237 struct bfd_link_order *link_order)
11238 {
11239 reloc_howto_type *howto;
11240 long indx;
11241 bfd_vma offset;
11242 bfd_vma addend;
11243 struct bfd_elf_section_reloc_data *reldata;
11244 struct elf_link_hash_entry **rel_hash_ptr;
11245 Elf_Internal_Shdr *rel_hdr;
11246 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11247 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11248 bfd_byte *erel;
11249 unsigned int i;
11250 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11251
11252 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11253 if (howto == NULL)
11254 {
11255 bfd_set_error (bfd_error_bad_value);
11256 return FALSE;
11257 }
11258
11259 addend = link_order->u.reloc.p->addend;
11260
11261 if (esdo->rel.hdr)
11262 reldata = &esdo->rel;
11263 else if (esdo->rela.hdr)
11264 reldata = &esdo->rela;
11265 else
11266 {
11267 reldata = NULL;
11268 BFD_ASSERT (0);
11269 }
11270
11271 /* Figure out the symbol index. */
11272 rel_hash_ptr = reldata->hashes + reldata->count;
11273 if (link_order->type == bfd_section_reloc_link_order)
11274 {
11275 indx = link_order->u.reloc.p->u.section->target_index;
11276 BFD_ASSERT (indx != 0);
11277 *rel_hash_ptr = NULL;
11278 }
11279 else
11280 {
11281 struct elf_link_hash_entry *h;
11282
11283 /* Treat a reloc against a defined symbol as though it were
11284 actually against the section. */
11285 h = ((struct elf_link_hash_entry *)
11286 bfd_wrapped_link_hash_lookup (output_bfd, info,
11287 link_order->u.reloc.p->u.name,
11288 FALSE, FALSE, TRUE));
11289 if (h != NULL
11290 && (h->root.type == bfd_link_hash_defined
11291 || h->root.type == bfd_link_hash_defweak))
11292 {
11293 asection *section;
11294
11295 section = h->root.u.def.section;
11296 indx = section->output_section->target_index;
11297 *rel_hash_ptr = NULL;
11298 /* It seems that we ought to add the symbol value to the
11299 addend here, but in practice it has already been added
11300 because it was passed to constructor_callback. */
11301 addend += section->output_section->vma + section->output_offset;
11302 }
11303 else if (h != NULL)
11304 {
11305 /* Setting the index to -2 tells elf_link_output_extsym that
11306 this symbol is used by a reloc. */
11307 h->indx = -2;
11308 *rel_hash_ptr = h;
11309 indx = 0;
11310 }
11311 else
11312 {
11313 (*info->callbacks->unattached_reloc)
11314 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
11315 indx = 0;
11316 }
11317 }
11318
11319 /* If this is an inplace reloc, we must write the addend into the
11320 object file. */
11321 if (howto->partial_inplace && addend != 0)
11322 {
11323 bfd_size_type size;
11324 bfd_reloc_status_type rstat;
11325 bfd_byte *buf;
11326 bfd_boolean ok;
11327 const char *sym_name;
11328
11329 size = (bfd_size_type) bfd_get_reloc_size (howto);
11330 buf = (bfd_byte *) bfd_zmalloc (size);
11331 if (buf == NULL && size != 0)
11332 return FALSE;
11333 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11334 switch (rstat)
11335 {
11336 case bfd_reloc_ok:
11337 break;
11338
11339 default:
11340 case bfd_reloc_outofrange:
11341 abort ();
11342
11343 case bfd_reloc_overflow:
11344 if (link_order->type == bfd_section_reloc_link_order)
11345 sym_name = bfd_section_name (output_bfd,
11346 link_order->u.reloc.p->u.section);
11347 else
11348 sym_name = link_order->u.reloc.p->u.name;
11349 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11350 howto->name, addend, NULL, NULL,
11351 (bfd_vma) 0);
11352 break;
11353 }
11354
11355 ok = bfd_set_section_contents (output_bfd, output_section, buf,
11356 link_order->offset
11357 * bfd_octets_per_byte (output_bfd),
11358 size);
11359 free (buf);
11360 if (! ok)
11361 return FALSE;
11362 }
11363
11364 /* The address of a reloc is relative to the section in a
11365 relocatable file, and is a virtual address in an executable
11366 file. */
11367 offset = link_order->offset;
11368 if (! bfd_link_relocatable (info))
11369 offset += output_section->vma;
11370
11371 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11372 {
11373 irel[i].r_offset = offset;
11374 irel[i].r_info = 0;
11375 irel[i].r_addend = 0;
11376 }
11377 if (bed->s->arch_size == 32)
11378 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11379 else
11380 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11381
11382 rel_hdr = reldata->hdr;
11383 erel = rel_hdr->contents;
11384 if (rel_hdr->sh_type == SHT_REL)
11385 {
11386 erel += reldata->count * bed->s->sizeof_rel;
11387 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11388 }
11389 else
11390 {
11391 irel[0].r_addend = addend;
11392 erel += reldata->count * bed->s->sizeof_rela;
11393 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11394 }
11395
11396 ++reldata->count;
11397
11398 return TRUE;
11399 }
11400
11401
11402 /* Get the output vma of the section pointed to by the sh_link field. */
11403
11404 static bfd_vma
11405 elf_get_linked_section_vma (struct bfd_link_order *p)
11406 {
11407 Elf_Internal_Shdr **elf_shdrp;
11408 asection *s;
11409 int elfsec;
11410
11411 s = p->u.indirect.section;
11412 elf_shdrp = elf_elfsections (s->owner);
11413 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
11414 elfsec = elf_shdrp[elfsec]->sh_link;
11415 /* PR 290:
11416 The Intel C compiler generates SHT_IA_64_UNWIND with
11417 SHF_LINK_ORDER. But it doesn't set the sh_link or
11418 sh_info fields. Hence we could get the situation
11419 where elfsec is 0. */
11420 if (elfsec == 0)
11421 {
11422 const struct elf_backend_data *bed
11423 = get_elf_backend_data (s->owner);
11424 if (bed->link_order_error_handler)
11425 bed->link_order_error_handler
11426 /* xgettext:c-format */
11427 (_("%pB: warning: sh_link not set for section `%pA'"), s->owner, s);
11428 return 0;
11429 }
11430 else
11431 {
11432 s = elf_shdrp[elfsec]->bfd_section;
11433 return s->output_section->vma + s->output_offset;
11434 }
11435 }
11436
11437
11438 /* Compare two sections based on the locations of the sections they are
11439 linked to. Used by elf_fixup_link_order. */
11440
11441 static int
11442 compare_link_order (const void * a, const void * b)
11443 {
11444 bfd_vma apos;
11445 bfd_vma bpos;
11446
11447 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11448 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11449 if (apos < bpos)
11450 return -1;
11451 return apos > bpos;
11452 }
11453
11454
11455 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11456 order as their linked sections. Returns false if this could not be done
11457 because an output section includes both ordered and unordered
11458 sections. Ideally we'd do this in the linker proper. */
11459
11460 static bfd_boolean
11461 elf_fixup_link_order (bfd *abfd, asection *o)
11462 {
11463 int seen_linkorder;
11464 int seen_other;
11465 int n;
11466 struct bfd_link_order *p;
11467 bfd *sub;
11468 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11469 unsigned elfsec;
11470 struct bfd_link_order **sections;
11471 asection *s, *other_sec, *linkorder_sec;
11472 bfd_vma offset;
11473
11474 other_sec = NULL;
11475 linkorder_sec = NULL;
11476 seen_other = 0;
11477 seen_linkorder = 0;
11478 for (p = o->map_head.link_order; p != NULL; p = p->next)
11479 {
11480 if (p->type == bfd_indirect_link_order)
11481 {
11482 s = p->u.indirect.section;
11483 sub = s->owner;
11484 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11485 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
11486 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11487 && elfsec < elf_numsections (sub)
11488 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11489 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
11490 {
11491 seen_linkorder++;
11492 linkorder_sec = s;
11493 }
11494 else
11495 {
11496 seen_other++;
11497 other_sec = s;
11498 }
11499 }
11500 else
11501 seen_other++;
11502
11503 if (seen_other && seen_linkorder)
11504 {
11505 if (other_sec && linkorder_sec)
11506 _bfd_error_handler
11507 /* xgettext:c-format */
11508 (_("%pA has both ordered [`%pA' in %pB] "
11509 "and unordered [`%pA' in %pB] sections"),
11510 o, linkorder_sec, linkorder_sec->owner,
11511 other_sec, other_sec->owner);
11512 else
11513 _bfd_error_handler
11514 (_("%pA has both ordered and unordered sections"), o);
11515 bfd_set_error (bfd_error_bad_value);
11516 return FALSE;
11517 }
11518 }
11519
11520 if (!seen_linkorder)
11521 return TRUE;
11522
11523 sections = (struct bfd_link_order **)
11524 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11525 if (sections == NULL)
11526 return FALSE;
11527 seen_linkorder = 0;
11528
11529 for (p = o->map_head.link_order; p != NULL; p = p->next)
11530 {
11531 sections[seen_linkorder++] = p;
11532 }
11533 /* Sort the input sections in the order of their linked section. */
11534 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11535 compare_link_order);
11536
11537 /* Change the offsets of the sections. */
11538 offset = 0;
11539 for (n = 0; n < seen_linkorder; n++)
11540 {
11541 s = sections[n]->u.indirect.section;
11542 offset &= ~(bfd_vma) 0 << s->alignment_power;
11543 s->output_offset = offset / bfd_octets_per_byte (abfd);
11544 sections[n]->offset = offset;
11545 offset += sections[n]->size;
11546 }
11547
11548 free (sections);
11549 return TRUE;
11550 }
11551
11552 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11553 Returns TRUE upon success, FALSE otherwise. */
11554
11555 static bfd_boolean
11556 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11557 {
11558 bfd_boolean ret = FALSE;
11559 bfd *implib_bfd;
11560 const struct elf_backend_data *bed;
11561 flagword flags;
11562 enum bfd_architecture arch;
11563 unsigned int mach;
11564 asymbol **sympp = NULL;
11565 long symsize;
11566 long symcount;
11567 long src_count;
11568 elf_symbol_type *osymbuf;
11569
11570 implib_bfd = info->out_implib_bfd;
11571 bed = get_elf_backend_data (abfd);
11572
11573 if (!bfd_set_format (implib_bfd, bfd_object))
11574 return FALSE;
11575
11576 /* Use flag from executable but make it a relocatable object. */
11577 flags = bfd_get_file_flags (abfd);
11578 flags &= ~HAS_RELOC;
11579 if (!bfd_set_start_address (implib_bfd, 0)
11580 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
11581 return FALSE;
11582
11583 /* Copy architecture of output file to import library file. */
11584 arch = bfd_get_arch (abfd);
11585 mach = bfd_get_mach (abfd);
11586 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11587 && (abfd->target_defaulted
11588 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11589 return FALSE;
11590
11591 /* Get symbol table size. */
11592 symsize = bfd_get_symtab_upper_bound (abfd);
11593 if (symsize < 0)
11594 return FALSE;
11595
11596 /* Read in the symbol table. */
11597 sympp = (asymbol **) xmalloc (symsize);
11598 symcount = bfd_canonicalize_symtab (abfd, sympp);
11599 if (symcount < 0)
11600 goto free_sym_buf;
11601
11602 /* Allow the BFD backend to copy any private header data it
11603 understands from the output BFD to the import library BFD. */
11604 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11605 goto free_sym_buf;
11606
11607 /* Filter symbols to appear in the import library. */
11608 if (bed->elf_backend_filter_implib_symbols)
11609 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11610 symcount);
11611 else
11612 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11613 if (symcount == 0)
11614 {
11615 bfd_set_error (bfd_error_no_symbols);
11616 _bfd_error_handler (_("%pB: no symbol found for import library"),
11617 implib_bfd);
11618 goto free_sym_buf;
11619 }
11620
11621
11622 /* Make symbols absolute. */
11623 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11624 sizeof (*osymbuf));
11625 for (src_count = 0; src_count < symcount; src_count++)
11626 {
11627 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11628 sizeof (*osymbuf));
11629 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11630 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11631 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11632 osymbuf[src_count].internal_elf_sym.st_value =
11633 osymbuf[src_count].symbol.value;
11634 sympp[src_count] = &osymbuf[src_count].symbol;
11635 }
11636
11637 bfd_set_symtab (implib_bfd, sympp, symcount);
11638
11639 /* Allow the BFD backend to copy any private data it understands
11640 from the output BFD to the import library BFD. This is done last
11641 to permit the routine to look at the filtered symbol table. */
11642 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11643 goto free_sym_buf;
11644
11645 if (!bfd_close (implib_bfd))
11646 goto free_sym_buf;
11647
11648 ret = TRUE;
11649
11650 free_sym_buf:
11651 free (sympp);
11652 return ret;
11653 }
11654
11655 static void
11656 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11657 {
11658 asection *o;
11659
11660 if (flinfo->symstrtab != NULL)
11661 _bfd_elf_strtab_free (flinfo->symstrtab);
11662 if (flinfo->contents != NULL)
11663 free (flinfo->contents);
11664 if (flinfo->external_relocs != NULL)
11665 free (flinfo->external_relocs);
11666 if (flinfo->internal_relocs != NULL)
11667 free (flinfo->internal_relocs);
11668 if (flinfo->external_syms != NULL)
11669 free (flinfo->external_syms);
11670 if (flinfo->locsym_shndx != NULL)
11671 free (flinfo->locsym_shndx);
11672 if (flinfo->internal_syms != NULL)
11673 free (flinfo->internal_syms);
11674 if (flinfo->indices != NULL)
11675 free (flinfo->indices);
11676 if (flinfo->sections != NULL)
11677 free (flinfo->sections);
11678 if (flinfo->symshndxbuf != NULL)
11679 free (flinfo->symshndxbuf);
11680 for (o = obfd->sections; o != NULL; o = o->next)
11681 {
11682 struct bfd_elf_section_data *esdo = elf_section_data (o);
11683 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11684 free (esdo->rel.hashes);
11685 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11686 free (esdo->rela.hashes);
11687 }
11688 }
11689
11690 /* Do the final step of an ELF link. */
11691
11692 bfd_boolean
11693 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11694 {
11695 bfd_boolean dynamic;
11696 bfd_boolean emit_relocs;
11697 bfd *dynobj;
11698 struct elf_final_link_info flinfo;
11699 asection *o;
11700 struct bfd_link_order *p;
11701 bfd *sub;
11702 bfd_size_type max_contents_size;
11703 bfd_size_type max_external_reloc_size;
11704 bfd_size_type max_internal_reloc_count;
11705 bfd_size_type max_sym_count;
11706 bfd_size_type max_sym_shndx_count;
11707 Elf_Internal_Sym elfsym;
11708 unsigned int i;
11709 Elf_Internal_Shdr *symtab_hdr;
11710 Elf_Internal_Shdr *symtab_shndx_hdr;
11711 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11712 struct elf_outext_info eoinfo;
11713 bfd_boolean merged;
11714 size_t relativecount = 0;
11715 asection *reldyn = 0;
11716 bfd_size_type amt;
11717 asection *attr_section = NULL;
11718 bfd_vma attr_size = 0;
11719 const char *std_attrs_section;
11720 struct elf_link_hash_table *htab = elf_hash_table (info);
11721
11722 if (!is_elf_hash_table (htab))
11723 return FALSE;
11724
11725 if (bfd_link_pic (info))
11726 abfd->flags |= DYNAMIC;
11727
11728 dynamic = htab->dynamic_sections_created;
11729 dynobj = htab->dynobj;
11730
11731 emit_relocs = (bfd_link_relocatable (info)
11732 || info->emitrelocations);
11733
11734 flinfo.info = info;
11735 flinfo.output_bfd = abfd;
11736 flinfo.symstrtab = _bfd_elf_strtab_init ();
11737 if (flinfo.symstrtab == NULL)
11738 return FALSE;
11739
11740 if (! dynamic)
11741 {
11742 flinfo.hash_sec = NULL;
11743 flinfo.symver_sec = NULL;
11744 }
11745 else
11746 {
11747 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11748 /* Note that dynsym_sec can be NULL (on VMS). */
11749 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11750 /* Note that it is OK if symver_sec is NULL. */
11751 }
11752
11753 flinfo.contents = NULL;
11754 flinfo.external_relocs = NULL;
11755 flinfo.internal_relocs = NULL;
11756 flinfo.external_syms = NULL;
11757 flinfo.locsym_shndx = NULL;
11758 flinfo.internal_syms = NULL;
11759 flinfo.indices = NULL;
11760 flinfo.sections = NULL;
11761 flinfo.symshndxbuf = NULL;
11762 flinfo.filesym_count = 0;
11763
11764 /* The object attributes have been merged. Remove the input
11765 sections from the link, and set the contents of the output
11766 secton. */
11767 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11768 for (o = abfd->sections; o != NULL; o = o->next)
11769 {
11770 bfd_boolean remove_section = FALSE;
11771
11772 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11773 || strcmp (o->name, ".gnu.attributes") == 0)
11774 {
11775 for (p = o->map_head.link_order; p != NULL; p = p->next)
11776 {
11777 asection *input_section;
11778
11779 if (p->type != bfd_indirect_link_order)
11780 continue;
11781 input_section = p->u.indirect.section;
11782 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11783 elf_link_input_bfd ignores this section. */
11784 input_section->flags &= ~SEC_HAS_CONTENTS;
11785 }
11786
11787 attr_size = bfd_elf_obj_attr_size (abfd);
11788 bfd_set_section_size (abfd, o, attr_size);
11789 /* Skip this section later on. */
11790 o->map_head.link_order = NULL;
11791 if (attr_size)
11792 attr_section = o;
11793 else
11794 remove_section = TRUE;
11795 }
11796 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
11797 {
11798 /* Remove empty group section from linker output. */
11799 remove_section = TRUE;
11800 }
11801 if (remove_section)
11802 {
11803 o->flags |= SEC_EXCLUDE;
11804 bfd_section_list_remove (abfd, o);
11805 abfd->section_count--;
11806 }
11807 }
11808
11809 /* Count up the number of relocations we will output for each output
11810 section, so that we know the sizes of the reloc sections. We
11811 also figure out some maximum sizes. */
11812 max_contents_size = 0;
11813 max_external_reloc_size = 0;
11814 max_internal_reloc_count = 0;
11815 max_sym_count = 0;
11816 max_sym_shndx_count = 0;
11817 merged = FALSE;
11818 for (o = abfd->sections; o != NULL; o = o->next)
11819 {
11820 struct bfd_elf_section_data *esdo = elf_section_data (o);
11821 o->reloc_count = 0;
11822
11823 for (p = o->map_head.link_order; p != NULL; p = p->next)
11824 {
11825 unsigned int reloc_count = 0;
11826 unsigned int additional_reloc_count = 0;
11827 struct bfd_elf_section_data *esdi = NULL;
11828
11829 if (p->type == bfd_section_reloc_link_order
11830 || p->type == bfd_symbol_reloc_link_order)
11831 reloc_count = 1;
11832 else if (p->type == bfd_indirect_link_order)
11833 {
11834 asection *sec;
11835
11836 sec = p->u.indirect.section;
11837
11838 /* Mark all sections which are to be included in the
11839 link. This will normally be every section. We need
11840 to do this so that we can identify any sections which
11841 the linker has decided to not include. */
11842 sec->linker_mark = TRUE;
11843
11844 if (sec->flags & SEC_MERGE)
11845 merged = TRUE;
11846
11847 if (sec->rawsize > max_contents_size)
11848 max_contents_size = sec->rawsize;
11849 if (sec->size > max_contents_size)
11850 max_contents_size = sec->size;
11851
11852 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11853 && (sec->owner->flags & DYNAMIC) == 0)
11854 {
11855 size_t sym_count;
11856
11857 /* We are interested in just local symbols, not all
11858 symbols. */
11859 if (elf_bad_symtab (sec->owner))
11860 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11861 / bed->s->sizeof_sym);
11862 else
11863 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11864
11865 if (sym_count > max_sym_count)
11866 max_sym_count = sym_count;
11867
11868 if (sym_count > max_sym_shndx_count
11869 && elf_symtab_shndx_list (sec->owner) != NULL)
11870 max_sym_shndx_count = sym_count;
11871
11872 if (esdo->this_hdr.sh_type == SHT_REL
11873 || esdo->this_hdr.sh_type == SHT_RELA)
11874 /* Some backends use reloc_count in relocation sections
11875 to count particular types of relocs. Of course,
11876 reloc sections themselves can't have relocations. */
11877 ;
11878 else if (emit_relocs)
11879 {
11880 reloc_count = sec->reloc_count;
11881 if (bed->elf_backend_count_additional_relocs)
11882 {
11883 int c;
11884 c = (*bed->elf_backend_count_additional_relocs) (sec);
11885 additional_reloc_count += c;
11886 }
11887 }
11888 else if (bed->elf_backend_count_relocs)
11889 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11890
11891 esdi = elf_section_data (sec);
11892
11893 if ((sec->flags & SEC_RELOC) != 0)
11894 {
11895 size_t ext_size = 0;
11896
11897 if (esdi->rel.hdr != NULL)
11898 ext_size = esdi->rel.hdr->sh_size;
11899 if (esdi->rela.hdr != NULL)
11900 ext_size += esdi->rela.hdr->sh_size;
11901
11902 if (ext_size > max_external_reloc_size)
11903 max_external_reloc_size = ext_size;
11904 if (sec->reloc_count > max_internal_reloc_count)
11905 max_internal_reloc_count = sec->reloc_count;
11906 }
11907 }
11908 }
11909
11910 if (reloc_count == 0)
11911 continue;
11912
11913 reloc_count += additional_reloc_count;
11914 o->reloc_count += reloc_count;
11915
11916 if (p->type == bfd_indirect_link_order && emit_relocs)
11917 {
11918 if (esdi->rel.hdr)
11919 {
11920 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11921 esdo->rel.count += additional_reloc_count;
11922 }
11923 if (esdi->rela.hdr)
11924 {
11925 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11926 esdo->rela.count += additional_reloc_count;
11927 }
11928 }
11929 else
11930 {
11931 if (o->use_rela_p)
11932 esdo->rela.count += reloc_count;
11933 else
11934 esdo->rel.count += reloc_count;
11935 }
11936 }
11937
11938 if (o->reloc_count > 0)
11939 o->flags |= SEC_RELOC;
11940 else
11941 {
11942 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11943 set it (this is probably a bug) and if it is set
11944 assign_section_numbers will create a reloc section. */
11945 o->flags &=~ SEC_RELOC;
11946 }
11947
11948 /* If the SEC_ALLOC flag is not set, force the section VMA to
11949 zero. This is done in elf_fake_sections as well, but forcing
11950 the VMA to 0 here will ensure that relocs against these
11951 sections are handled correctly. */
11952 if ((o->flags & SEC_ALLOC) == 0
11953 && ! o->user_set_vma)
11954 o->vma = 0;
11955 }
11956
11957 if (! bfd_link_relocatable (info) && merged)
11958 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
11959
11960 /* Figure out the file positions for everything but the symbol table
11961 and the relocs. We set symcount to force assign_section_numbers
11962 to create a symbol table. */
11963 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
11964 BFD_ASSERT (! abfd->output_has_begun);
11965 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11966 goto error_return;
11967
11968 /* Set sizes, and assign file positions for reloc sections. */
11969 for (o = abfd->sections; o != NULL; o = o->next)
11970 {
11971 struct bfd_elf_section_data *esdo = elf_section_data (o);
11972 if ((o->flags & SEC_RELOC) != 0)
11973 {
11974 if (esdo->rel.hdr
11975 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
11976 goto error_return;
11977
11978 if (esdo->rela.hdr
11979 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
11980 goto error_return;
11981 }
11982
11983 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11984 to count upwards while actually outputting the relocations. */
11985 esdo->rel.count = 0;
11986 esdo->rela.count = 0;
11987
11988 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11989 {
11990 /* Cache the section contents so that they can be compressed
11991 later. Use bfd_malloc since it will be freed by
11992 bfd_compress_section_contents. */
11993 unsigned char *contents = esdo->this_hdr.contents;
11994 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11995 abort ();
11996 contents
11997 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11998 if (contents == NULL)
11999 goto error_return;
12000 esdo->this_hdr.contents = contents;
12001 }
12002 }
12003
12004 /* We have now assigned file positions for all the sections except
12005 .symtab, .strtab, and non-loaded reloc sections. We start the
12006 .symtab section at the current file position, and write directly
12007 to it. We build the .strtab section in memory. */
12008 bfd_get_symcount (abfd) = 0;
12009 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12010 /* sh_name is set in prep_headers. */
12011 symtab_hdr->sh_type = SHT_SYMTAB;
12012 /* sh_flags, sh_addr and sh_size all start off zero. */
12013 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12014 /* sh_link is set in assign_section_numbers. */
12015 /* sh_info is set below. */
12016 /* sh_offset is set just below. */
12017 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
12018
12019 if (max_sym_count < 20)
12020 max_sym_count = 20;
12021 htab->strtabsize = max_sym_count;
12022 amt = max_sym_count * sizeof (struct elf_sym_strtab);
12023 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12024 if (htab->strtab == NULL)
12025 goto error_return;
12026 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12027 flinfo.symshndxbuf
12028 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12029 ? (Elf_External_Sym_Shndx *) -1 : NULL);
12030
12031 if (info->strip != strip_all || emit_relocs)
12032 {
12033 file_ptr off = elf_next_file_pos (abfd);
12034
12035 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
12036
12037 /* Note that at this point elf_next_file_pos (abfd) is
12038 incorrect. We do not yet know the size of the .symtab section.
12039 We correct next_file_pos below, after we do know the size. */
12040
12041 /* Start writing out the symbol table. The first symbol is always a
12042 dummy symbol. */
12043 elfsym.st_value = 0;
12044 elfsym.st_size = 0;
12045 elfsym.st_info = 0;
12046 elfsym.st_other = 0;
12047 elfsym.st_shndx = SHN_UNDEF;
12048 elfsym.st_target_internal = 0;
12049 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12050 bfd_und_section_ptr, NULL) != 1)
12051 goto error_return;
12052
12053 /* Output a symbol for each section. We output these even if we are
12054 discarding local symbols, since they are used for relocs. These
12055 symbols have no names. We store the index of each one in the
12056 index field of the section, so that we can find it again when
12057 outputting relocs. */
12058
12059 elfsym.st_size = 0;
12060 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12061 elfsym.st_other = 0;
12062 elfsym.st_value = 0;
12063 elfsym.st_target_internal = 0;
12064 for (i = 1; i < elf_numsections (abfd); i++)
12065 {
12066 o = bfd_section_from_elf_index (abfd, i);
12067 if (o != NULL)
12068 {
12069 o->target_index = bfd_get_symcount (abfd);
12070 elfsym.st_shndx = i;
12071 if (!bfd_link_relocatable (info))
12072 elfsym.st_value = o->vma;
12073 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
12074 NULL) != 1)
12075 goto error_return;
12076 }
12077 }
12078 }
12079
12080 /* Allocate some memory to hold information read in from the input
12081 files. */
12082 if (max_contents_size != 0)
12083 {
12084 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12085 if (flinfo.contents == NULL)
12086 goto error_return;
12087 }
12088
12089 if (max_external_reloc_size != 0)
12090 {
12091 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12092 if (flinfo.external_relocs == NULL)
12093 goto error_return;
12094 }
12095
12096 if (max_internal_reloc_count != 0)
12097 {
12098 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12099 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12100 if (flinfo.internal_relocs == NULL)
12101 goto error_return;
12102 }
12103
12104 if (max_sym_count != 0)
12105 {
12106 amt = max_sym_count * bed->s->sizeof_sym;
12107 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12108 if (flinfo.external_syms == NULL)
12109 goto error_return;
12110
12111 amt = max_sym_count * sizeof (Elf_Internal_Sym);
12112 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12113 if (flinfo.internal_syms == NULL)
12114 goto error_return;
12115
12116 amt = max_sym_count * sizeof (long);
12117 flinfo.indices = (long int *) bfd_malloc (amt);
12118 if (flinfo.indices == NULL)
12119 goto error_return;
12120
12121 amt = max_sym_count * sizeof (asection *);
12122 flinfo.sections = (asection **) bfd_malloc (amt);
12123 if (flinfo.sections == NULL)
12124 goto error_return;
12125 }
12126
12127 if (max_sym_shndx_count != 0)
12128 {
12129 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
12130 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12131 if (flinfo.locsym_shndx == NULL)
12132 goto error_return;
12133 }
12134
12135 if (htab->tls_sec)
12136 {
12137 bfd_vma base, end = 0;
12138 asection *sec;
12139
12140 for (sec = htab->tls_sec;
12141 sec && (sec->flags & SEC_THREAD_LOCAL);
12142 sec = sec->next)
12143 {
12144 bfd_size_type size = sec->size;
12145
12146 if (size == 0
12147 && (sec->flags & SEC_HAS_CONTENTS) == 0)
12148 {
12149 struct bfd_link_order *ord = sec->map_tail.link_order;
12150
12151 if (ord != NULL)
12152 size = ord->offset + ord->size;
12153 }
12154 end = sec->vma + size;
12155 }
12156 base = htab->tls_sec->vma;
12157 /* Only align end of TLS section if static TLS doesn't have special
12158 alignment requirements. */
12159 if (bed->static_tls_alignment == 1)
12160 end = align_power (end, htab->tls_sec->alignment_power);
12161 htab->tls_size = end - base;
12162 }
12163
12164 /* Reorder SHF_LINK_ORDER sections. */
12165 for (o = abfd->sections; o != NULL; o = o->next)
12166 {
12167 if (!elf_fixup_link_order (abfd, o))
12168 return FALSE;
12169 }
12170
12171 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12172 return FALSE;
12173
12174 /* Since ELF permits relocations to be against local symbols, we
12175 must have the local symbols available when we do the relocations.
12176 Since we would rather only read the local symbols once, and we
12177 would rather not keep them in memory, we handle all the
12178 relocations for a single input file at the same time.
12179
12180 Unfortunately, there is no way to know the total number of local
12181 symbols until we have seen all of them, and the local symbol
12182 indices precede the global symbol indices. This means that when
12183 we are generating relocatable output, and we see a reloc against
12184 a global symbol, we can not know the symbol index until we have
12185 finished examining all the local symbols to see which ones we are
12186 going to output. To deal with this, we keep the relocations in
12187 memory, and don't output them until the end of the link. This is
12188 an unfortunate waste of memory, but I don't see a good way around
12189 it. Fortunately, it only happens when performing a relocatable
12190 link, which is not the common case. FIXME: If keep_memory is set
12191 we could write the relocs out and then read them again; I don't
12192 know how bad the memory loss will be. */
12193
12194 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12195 sub->output_has_begun = FALSE;
12196 for (o = abfd->sections; o != NULL; o = o->next)
12197 {
12198 for (p = o->map_head.link_order; p != NULL; p = p->next)
12199 {
12200 if (p->type == bfd_indirect_link_order
12201 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12202 == bfd_target_elf_flavour)
12203 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12204 {
12205 if (! sub->output_has_begun)
12206 {
12207 if (! elf_link_input_bfd (&flinfo, sub))
12208 goto error_return;
12209 sub->output_has_begun = TRUE;
12210 }
12211 }
12212 else if (p->type == bfd_section_reloc_link_order
12213 || p->type == bfd_symbol_reloc_link_order)
12214 {
12215 if (! elf_reloc_link_order (abfd, info, o, p))
12216 goto error_return;
12217 }
12218 else
12219 {
12220 if (! _bfd_default_link_order (abfd, info, o, p))
12221 {
12222 if (p->type == bfd_indirect_link_order
12223 && (bfd_get_flavour (sub)
12224 == bfd_target_elf_flavour)
12225 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12226 != bed->s->elfclass))
12227 {
12228 const char *iclass, *oclass;
12229
12230 switch (bed->s->elfclass)
12231 {
12232 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12233 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12234 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12235 default: abort ();
12236 }
12237
12238 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12239 {
12240 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12241 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12242 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12243 default: abort ();
12244 }
12245
12246 bfd_set_error (bfd_error_wrong_format);
12247 _bfd_error_handler
12248 /* xgettext:c-format */
12249 (_("%pB: file class %s incompatible with %s"),
12250 sub, iclass, oclass);
12251 }
12252
12253 goto error_return;
12254 }
12255 }
12256 }
12257 }
12258
12259 /* Free symbol buffer if needed. */
12260 if (!info->reduce_memory_overheads)
12261 {
12262 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12263 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
12264 && elf_tdata (sub)->symbuf)
12265 {
12266 free (elf_tdata (sub)->symbuf);
12267 elf_tdata (sub)->symbuf = NULL;
12268 }
12269 }
12270
12271 /* Output any global symbols that got converted to local in a
12272 version script or due to symbol visibility. We do this in a
12273 separate step since ELF requires all local symbols to appear
12274 prior to any global symbols. FIXME: We should only do this if
12275 some global symbols were, in fact, converted to become local.
12276 FIXME: Will this work correctly with the Irix 5 linker? */
12277 eoinfo.failed = FALSE;
12278 eoinfo.flinfo = &flinfo;
12279 eoinfo.localsyms = TRUE;
12280 eoinfo.file_sym_done = FALSE;
12281 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12282 if (eoinfo.failed)
12283 return FALSE;
12284
12285 /* If backend needs to output some local symbols not present in the hash
12286 table, do it now. */
12287 if (bed->elf_backend_output_arch_local_syms
12288 && (info->strip != strip_all || emit_relocs))
12289 {
12290 typedef int (*out_sym_func)
12291 (void *, const char *, Elf_Internal_Sym *, asection *,
12292 struct elf_link_hash_entry *);
12293
12294 if (! ((*bed->elf_backend_output_arch_local_syms)
12295 (abfd, info, &flinfo,
12296 (out_sym_func) elf_link_output_symstrtab)))
12297 return FALSE;
12298 }
12299
12300 /* That wrote out all the local symbols. Finish up the symbol table
12301 with the global symbols. Even if we want to strip everything we
12302 can, we still need to deal with those global symbols that got
12303 converted to local in a version script. */
12304
12305 /* The sh_info field records the index of the first non local symbol. */
12306 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12307
12308 if (dynamic
12309 && htab->dynsym != NULL
12310 && htab->dynsym->output_section != bfd_abs_section_ptr)
12311 {
12312 Elf_Internal_Sym sym;
12313 bfd_byte *dynsym = htab->dynsym->contents;
12314
12315 o = htab->dynsym->output_section;
12316 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12317
12318 /* Write out the section symbols for the output sections. */
12319 if (bfd_link_pic (info)
12320 || htab->is_relocatable_executable)
12321 {
12322 asection *s;
12323
12324 sym.st_size = 0;
12325 sym.st_name = 0;
12326 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12327 sym.st_other = 0;
12328 sym.st_target_internal = 0;
12329
12330 for (s = abfd->sections; s != NULL; s = s->next)
12331 {
12332 int indx;
12333 bfd_byte *dest;
12334 long dynindx;
12335
12336 dynindx = elf_section_data (s)->dynindx;
12337 if (dynindx <= 0)
12338 continue;
12339 indx = elf_section_data (s)->this_idx;
12340 BFD_ASSERT (indx > 0);
12341 sym.st_shndx = indx;
12342 if (! check_dynsym (abfd, &sym))
12343 return FALSE;
12344 sym.st_value = s->vma;
12345 dest = dynsym + dynindx * bed->s->sizeof_sym;
12346 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12347 }
12348 }
12349
12350 /* Write out the local dynsyms. */
12351 if (htab->dynlocal)
12352 {
12353 struct elf_link_local_dynamic_entry *e;
12354 for (e = htab->dynlocal; e ; e = e->next)
12355 {
12356 asection *s;
12357 bfd_byte *dest;
12358
12359 /* Copy the internal symbol and turn off visibility.
12360 Note that we saved a word of storage and overwrote
12361 the original st_name with the dynstr_index. */
12362 sym = e->isym;
12363 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12364
12365 s = bfd_section_from_elf_index (e->input_bfd,
12366 e->isym.st_shndx);
12367 if (s != NULL)
12368 {
12369 sym.st_shndx =
12370 elf_section_data (s->output_section)->this_idx;
12371 if (! check_dynsym (abfd, &sym))
12372 return FALSE;
12373 sym.st_value = (s->output_section->vma
12374 + s->output_offset
12375 + e->isym.st_value);
12376 }
12377
12378 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12379 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12380 }
12381 }
12382 }
12383
12384 /* We get the global symbols from the hash table. */
12385 eoinfo.failed = FALSE;
12386 eoinfo.localsyms = FALSE;
12387 eoinfo.flinfo = &flinfo;
12388 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12389 if (eoinfo.failed)
12390 return FALSE;
12391
12392 /* If backend needs to output some symbols not present in the hash
12393 table, do it now. */
12394 if (bed->elf_backend_output_arch_syms
12395 && (info->strip != strip_all || emit_relocs))
12396 {
12397 typedef int (*out_sym_func)
12398 (void *, const char *, Elf_Internal_Sym *, asection *,
12399 struct elf_link_hash_entry *);
12400
12401 if (! ((*bed->elf_backend_output_arch_syms)
12402 (abfd, info, &flinfo,
12403 (out_sym_func) elf_link_output_symstrtab)))
12404 return FALSE;
12405 }
12406
12407 /* Finalize the .strtab section. */
12408 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12409
12410 /* Swap out the .strtab section. */
12411 if (!elf_link_swap_symbols_out (&flinfo))
12412 return FALSE;
12413
12414 /* Now we know the size of the symtab section. */
12415 if (bfd_get_symcount (abfd) > 0)
12416 {
12417 /* Finish up and write out the symbol string table (.strtab)
12418 section. */
12419 Elf_Internal_Shdr *symstrtab_hdr = NULL;
12420 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12421
12422 if (elf_symtab_shndx_list (abfd))
12423 {
12424 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12425
12426 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12427 {
12428 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12429 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12430 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12431 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12432 symtab_shndx_hdr->sh_size = amt;
12433
12434 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12435 off, TRUE);
12436
12437 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12438 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12439 return FALSE;
12440 }
12441 }
12442
12443 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12444 /* sh_name was set in prep_headers. */
12445 symstrtab_hdr->sh_type = SHT_STRTAB;
12446 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
12447 symstrtab_hdr->sh_addr = 0;
12448 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
12449 symstrtab_hdr->sh_entsize = 0;
12450 symstrtab_hdr->sh_link = 0;
12451 symstrtab_hdr->sh_info = 0;
12452 /* sh_offset is set just below. */
12453 symstrtab_hdr->sh_addralign = 1;
12454
12455 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12456 off, TRUE);
12457 elf_next_file_pos (abfd) = off;
12458
12459 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12460 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12461 return FALSE;
12462 }
12463
12464 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12465 {
12466 _bfd_error_handler (_("%pB: failed to generate import library"),
12467 info->out_implib_bfd);
12468 return FALSE;
12469 }
12470
12471 /* Adjust the relocs to have the correct symbol indices. */
12472 for (o = abfd->sections; o != NULL; o = o->next)
12473 {
12474 struct bfd_elf_section_data *esdo = elf_section_data (o);
12475 bfd_boolean sort;
12476
12477 if ((o->flags & SEC_RELOC) == 0)
12478 continue;
12479
12480 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12481 if (esdo->rel.hdr != NULL
12482 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
12483 return FALSE;
12484 if (esdo->rela.hdr != NULL
12485 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
12486 return FALSE;
12487
12488 /* Set the reloc_count field to 0 to prevent write_relocs from
12489 trying to swap the relocs out itself. */
12490 o->reloc_count = 0;
12491 }
12492
12493 if (dynamic && info->combreloc && dynobj != NULL)
12494 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12495
12496 /* If we are linking against a dynamic object, or generating a
12497 shared library, finish up the dynamic linking information. */
12498 if (dynamic)
12499 {
12500 bfd_byte *dyncon, *dynconend;
12501
12502 /* Fix up .dynamic entries. */
12503 o = bfd_get_linker_section (dynobj, ".dynamic");
12504 BFD_ASSERT (o != NULL);
12505
12506 dyncon = o->contents;
12507 dynconend = o->contents + o->size;
12508 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12509 {
12510 Elf_Internal_Dyn dyn;
12511 const char *name;
12512 unsigned int type;
12513 bfd_size_type sh_size;
12514 bfd_vma sh_addr;
12515
12516 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12517
12518 switch (dyn.d_tag)
12519 {
12520 default:
12521 continue;
12522 case DT_NULL:
12523 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12524 {
12525 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12526 {
12527 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12528 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12529 default: continue;
12530 }
12531 dyn.d_un.d_val = relativecount;
12532 relativecount = 0;
12533 break;
12534 }
12535 continue;
12536
12537 case DT_INIT:
12538 name = info->init_function;
12539 goto get_sym;
12540 case DT_FINI:
12541 name = info->fini_function;
12542 get_sym:
12543 {
12544 struct elf_link_hash_entry *h;
12545
12546 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
12547 if (h != NULL
12548 && (h->root.type == bfd_link_hash_defined
12549 || h->root.type == bfd_link_hash_defweak))
12550 {
12551 dyn.d_un.d_ptr = h->root.u.def.value;
12552 o = h->root.u.def.section;
12553 if (o->output_section != NULL)
12554 dyn.d_un.d_ptr += (o->output_section->vma
12555 + o->output_offset);
12556 else
12557 {
12558 /* The symbol is imported from another shared
12559 library and does not apply to this one. */
12560 dyn.d_un.d_ptr = 0;
12561 }
12562 break;
12563 }
12564 }
12565 continue;
12566
12567 case DT_PREINIT_ARRAYSZ:
12568 name = ".preinit_array";
12569 goto get_out_size;
12570 case DT_INIT_ARRAYSZ:
12571 name = ".init_array";
12572 goto get_out_size;
12573 case DT_FINI_ARRAYSZ:
12574 name = ".fini_array";
12575 get_out_size:
12576 o = bfd_get_section_by_name (abfd, name);
12577 if (o == NULL)
12578 {
12579 _bfd_error_handler
12580 (_("could not find section %s"), name);
12581 goto error_return;
12582 }
12583 if (o->size == 0)
12584 _bfd_error_handler
12585 (_("warning: %s section has zero size"), name);
12586 dyn.d_un.d_val = o->size;
12587 break;
12588
12589 case DT_PREINIT_ARRAY:
12590 name = ".preinit_array";
12591 goto get_out_vma;
12592 case DT_INIT_ARRAY:
12593 name = ".init_array";
12594 goto get_out_vma;
12595 case DT_FINI_ARRAY:
12596 name = ".fini_array";
12597 get_out_vma:
12598 o = bfd_get_section_by_name (abfd, name);
12599 goto do_vma;
12600
12601 case DT_HASH:
12602 name = ".hash";
12603 goto get_vma;
12604 case DT_GNU_HASH:
12605 name = ".gnu.hash";
12606 goto get_vma;
12607 case DT_STRTAB:
12608 name = ".dynstr";
12609 goto get_vma;
12610 case DT_SYMTAB:
12611 name = ".dynsym";
12612 goto get_vma;
12613 case DT_VERDEF:
12614 name = ".gnu.version_d";
12615 goto get_vma;
12616 case DT_VERNEED:
12617 name = ".gnu.version_r";
12618 goto get_vma;
12619 case DT_VERSYM:
12620 name = ".gnu.version";
12621 get_vma:
12622 o = bfd_get_linker_section (dynobj, name);
12623 do_vma:
12624 if (o == NULL || bfd_is_abs_section (o->output_section))
12625 {
12626 _bfd_error_handler
12627 (_("could not find section %s"), name);
12628 goto error_return;
12629 }
12630 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12631 {
12632 _bfd_error_handler
12633 (_("warning: section '%s' is being made into a note"), name);
12634 bfd_set_error (bfd_error_nonrepresentable_section);
12635 goto error_return;
12636 }
12637 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
12638 break;
12639
12640 case DT_REL:
12641 case DT_RELA:
12642 case DT_RELSZ:
12643 case DT_RELASZ:
12644 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12645 type = SHT_REL;
12646 else
12647 type = SHT_RELA;
12648 sh_size = 0;
12649 sh_addr = 0;
12650 for (i = 1; i < elf_numsections (abfd); i++)
12651 {
12652 Elf_Internal_Shdr *hdr;
12653
12654 hdr = elf_elfsections (abfd)[i];
12655 if (hdr->sh_type == type
12656 && (hdr->sh_flags & SHF_ALLOC) != 0)
12657 {
12658 sh_size += hdr->sh_size;
12659 if (sh_addr == 0
12660 || sh_addr > hdr->sh_addr)
12661 sh_addr = hdr->sh_addr;
12662 }
12663 }
12664
12665 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12666 {
12667 /* Don't count procedure linkage table relocs in the
12668 overall reloc count. */
12669 sh_size -= htab->srelplt->size;
12670 if (sh_size == 0)
12671 /* If the size is zero, make the address zero too.
12672 This is to avoid a glibc bug. If the backend
12673 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12674 zero, then we'll put DT_RELA at the end of
12675 DT_JMPREL. glibc will interpret the end of
12676 DT_RELA matching the end of DT_JMPREL as the
12677 case where DT_RELA includes DT_JMPREL, and for
12678 LD_BIND_NOW will decide that processing DT_RELA
12679 will process the PLT relocs too. Net result:
12680 No PLT relocs applied. */
12681 sh_addr = 0;
12682
12683 /* If .rela.plt is the first .rela section, exclude
12684 it from DT_RELA. */
12685 else if (sh_addr == (htab->srelplt->output_section->vma
12686 + htab->srelplt->output_offset))
12687 sh_addr += htab->srelplt->size;
12688 }
12689
12690 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12691 dyn.d_un.d_val = sh_size;
12692 else
12693 dyn.d_un.d_ptr = sh_addr;
12694 break;
12695 }
12696 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12697 }
12698 }
12699
12700 /* If we have created any dynamic sections, then output them. */
12701 if (dynobj != NULL)
12702 {
12703 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12704 goto error_return;
12705
12706 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12707 if (((info->warn_shared_textrel && bfd_link_pic (info))
12708 || info->error_textrel)
12709 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
12710 {
12711 bfd_byte *dyncon, *dynconend;
12712
12713 dyncon = o->contents;
12714 dynconend = o->contents + o->size;
12715 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12716 {
12717 Elf_Internal_Dyn dyn;
12718
12719 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12720
12721 if (dyn.d_tag == DT_TEXTREL)
12722 {
12723 if (info->error_textrel)
12724 info->callbacks->einfo
12725 (_("%P%X: read-only segment has dynamic relocations\n"));
12726 else
12727 info->callbacks->einfo
12728 (_("%P: warning: creating a DT_TEXTREL in a shared object\n"));
12729 break;
12730 }
12731 }
12732 }
12733
12734 for (o = dynobj->sections; o != NULL; o = o->next)
12735 {
12736 if ((o->flags & SEC_HAS_CONTENTS) == 0
12737 || o->size == 0
12738 || o->output_section == bfd_abs_section_ptr)
12739 continue;
12740 if ((o->flags & SEC_LINKER_CREATED) == 0)
12741 {
12742 /* At this point, we are only interested in sections
12743 created by _bfd_elf_link_create_dynamic_sections. */
12744 continue;
12745 }
12746 if (htab->stab_info.stabstr == o)
12747 continue;
12748 if (htab->eh_info.hdr_sec == o)
12749 continue;
12750 if (strcmp (o->name, ".dynstr") != 0)
12751 {
12752 if (! bfd_set_section_contents (abfd, o->output_section,
12753 o->contents,
12754 (file_ptr) o->output_offset
12755 * bfd_octets_per_byte (abfd),
12756 o->size))
12757 goto error_return;
12758 }
12759 else
12760 {
12761 /* The contents of the .dynstr section are actually in a
12762 stringtab. */
12763 file_ptr off;
12764
12765 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12766 if (bfd_seek (abfd, off, SEEK_SET) != 0
12767 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
12768 goto error_return;
12769 }
12770 }
12771 }
12772
12773 if (!info->resolve_section_groups)
12774 {
12775 bfd_boolean failed = FALSE;
12776
12777 BFD_ASSERT (bfd_link_relocatable (info));
12778 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12779 if (failed)
12780 goto error_return;
12781 }
12782
12783 /* If we have optimized stabs strings, output them. */
12784 if (htab->stab_info.stabstr != NULL)
12785 {
12786 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
12787 goto error_return;
12788 }
12789
12790 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12791 goto error_return;
12792
12793 elf_final_link_free (abfd, &flinfo);
12794
12795 elf_linker (abfd) = TRUE;
12796
12797 if (attr_section)
12798 {
12799 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
12800 if (contents == NULL)
12801 return FALSE; /* Bail out and fail. */
12802 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12803 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12804 free (contents);
12805 }
12806
12807 return TRUE;
12808
12809 error_return:
12810 elf_final_link_free (abfd, &flinfo);
12811 return FALSE;
12812 }
12813 \f
12814 /* Initialize COOKIE for input bfd ABFD. */
12815
12816 static bfd_boolean
12817 init_reloc_cookie (struct elf_reloc_cookie *cookie,
12818 struct bfd_link_info *info, bfd *abfd)
12819 {
12820 Elf_Internal_Shdr *symtab_hdr;
12821 const struct elf_backend_data *bed;
12822
12823 bed = get_elf_backend_data (abfd);
12824 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12825
12826 cookie->abfd = abfd;
12827 cookie->sym_hashes = elf_sym_hashes (abfd);
12828 cookie->bad_symtab = elf_bad_symtab (abfd);
12829 if (cookie->bad_symtab)
12830 {
12831 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12832 cookie->extsymoff = 0;
12833 }
12834 else
12835 {
12836 cookie->locsymcount = symtab_hdr->sh_info;
12837 cookie->extsymoff = symtab_hdr->sh_info;
12838 }
12839
12840 if (bed->s->arch_size == 32)
12841 cookie->r_sym_shift = 8;
12842 else
12843 cookie->r_sym_shift = 32;
12844
12845 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12846 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12847 {
12848 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12849 cookie->locsymcount, 0,
12850 NULL, NULL, NULL);
12851 if (cookie->locsyms == NULL)
12852 {
12853 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12854 return FALSE;
12855 }
12856 if (info->keep_memory)
12857 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12858 }
12859 return TRUE;
12860 }
12861
12862 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12863
12864 static void
12865 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12866 {
12867 Elf_Internal_Shdr *symtab_hdr;
12868
12869 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12870 if (cookie->locsyms != NULL
12871 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12872 free (cookie->locsyms);
12873 }
12874
12875 /* Initialize the relocation information in COOKIE for input section SEC
12876 of input bfd ABFD. */
12877
12878 static bfd_boolean
12879 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12880 struct bfd_link_info *info, bfd *abfd,
12881 asection *sec)
12882 {
12883 if (sec->reloc_count == 0)
12884 {
12885 cookie->rels = NULL;
12886 cookie->relend = NULL;
12887 }
12888 else
12889 {
12890 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12891 info->keep_memory);
12892 if (cookie->rels == NULL)
12893 return FALSE;
12894 cookie->rel = cookie->rels;
12895 cookie->relend = cookie->rels + sec->reloc_count;
12896 }
12897 cookie->rel = cookie->rels;
12898 return TRUE;
12899 }
12900
12901 /* Free the memory allocated by init_reloc_cookie_rels,
12902 if appropriate. */
12903
12904 static void
12905 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12906 asection *sec)
12907 {
12908 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12909 free (cookie->rels);
12910 }
12911
12912 /* Initialize the whole of COOKIE for input section SEC. */
12913
12914 static bfd_boolean
12915 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12916 struct bfd_link_info *info,
12917 asection *sec)
12918 {
12919 if (!init_reloc_cookie (cookie, info, sec->owner))
12920 goto error1;
12921 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12922 goto error2;
12923 return TRUE;
12924
12925 error2:
12926 fini_reloc_cookie (cookie, sec->owner);
12927 error1:
12928 return FALSE;
12929 }
12930
12931 /* Free the memory allocated by init_reloc_cookie_for_section,
12932 if appropriate. */
12933
12934 static void
12935 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12936 asection *sec)
12937 {
12938 fini_reloc_cookie_rels (cookie, sec);
12939 fini_reloc_cookie (cookie, sec->owner);
12940 }
12941 \f
12942 /* Garbage collect unused sections. */
12943
12944 /* Default gc_mark_hook. */
12945
12946 asection *
12947 _bfd_elf_gc_mark_hook (asection *sec,
12948 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12949 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12950 struct elf_link_hash_entry *h,
12951 Elf_Internal_Sym *sym)
12952 {
12953 if (h != NULL)
12954 {
12955 switch (h->root.type)
12956 {
12957 case bfd_link_hash_defined:
12958 case bfd_link_hash_defweak:
12959 return h->root.u.def.section;
12960
12961 case bfd_link_hash_common:
12962 return h->root.u.c.p->section;
12963
12964 default:
12965 break;
12966 }
12967 }
12968 else
12969 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12970
12971 return NULL;
12972 }
12973
12974 /* Return the debug definition section. */
12975
12976 static asection *
12977 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
12978 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12979 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12980 struct elf_link_hash_entry *h,
12981 Elf_Internal_Sym *sym)
12982 {
12983 if (h != NULL)
12984 {
12985 /* Return the global debug definition section. */
12986 if ((h->root.type == bfd_link_hash_defined
12987 || h->root.type == bfd_link_hash_defweak)
12988 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
12989 return h->root.u.def.section;
12990 }
12991 else
12992 {
12993 /* Return the local debug definition section. */
12994 asection *isec = bfd_section_from_elf_index (sec->owner,
12995 sym->st_shndx);
12996 if ((isec->flags & SEC_DEBUGGING) != 0)
12997 return isec;
12998 }
12999
13000 return NULL;
13001 }
13002
13003 /* COOKIE->rel describes a relocation against section SEC, which is
13004 a section we've decided to keep. Return the section that contains
13005 the relocation symbol, or NULL if no section contains it. */
13006
13007 asection *
13008 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13009 elf_gc_mark_hook_fn gc_mark_hook,
13010 struct elf_reloc_cookie *cookie,
13011 bfd_boolean *start_stop)
13012 {
13013 unsigned long r_symndx;
13014 struct elf_link_hash_entry *h;
13015
13016 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
13017 if (r_symndx == STN_UNDEF)
13018 return NULL;
13019
13020 if (r_symndx >= cookie->locsymcount
13021 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13022 {
13023 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
13024 if (h == NULL)
13025 {
13026 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
13027 sec->owner);
13028 return NULL;
13029 }
13030 while (h->root.type == bfd_link_hash_indirect
13031 || h->root.type == bfd_link_hash_warning)
13032 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13033 h->mark = 1;
13034 /* If this symbol is weak and there is a non-weak definition, we
13035 keep the non-weak definition because many backends put
13036 dynamic reloc info on the non-weak definition for code
13037 handling copy relocs. */
13038 if (h->is_weakalias)
13039 weakdef (h)->mark = 1;
13040
13041 if (start_stop != NULL)
13042 {
13043 /* To work around a glibc bug, mark XXX input sections
13044 when there is a reference to __start_XXX or __stop_XXX
13045 symbols. */
13046 if (h->start_stop)
13047 {
13048 asection *s = h->u2.start_stop_section;
13049 *start_stop = !s->gc_mark;
13050 return s;
13051 }
13052 }
13053
13054 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13055 }
13056
13057 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13058 &cookie->locsyms[r_symndx]);
13059 }
13060
13061 /* COOKIE->rel describes a relocation against section SEC, which is
13062 a section we've decided to keep. Mark the section that contains
13063 the relocation symbol. */
13064
13065 bfd_boolean
13066 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13067 asection *sec,
13068 elf_gc_mark_hook_fn gc_mark_hook,
13069 struct elf_reloc_cookie *cookie)
13070 {
13071 asection *rsec;
13072 bfd_boolean start_stop = FALSE;
13073
13074 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13075 while (rsec != NULL)
13076 {
13077 if (!rsec->gc_mark)
13078 {
13079 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13080 || (rsec->owner->flags & DYNAMIC) != 0)
13081 rsec->gc_mark = 1;
13082 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13083 return FALSE;
13084 }
13085 if (!start_stop)
13086 break;
13087 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
13088 }
13089 return TRUE;
13090 }
13091
13092 /* The mark phase of garbage collection. For a given section, mark
13093 it and any sections in this section's group, and all the sections
13094 which define symbols to which it refers. */
13095
13096 bfd_boolean
13097 _bfd_elf_gc_mark (struct bfd_link_info *info,
13098 asection *sec,
13099 elf_gc_mark_hook_fn gc_mark_hook)
13100 {
13101 bfd_boolean ret;
13102 asection *group_sec, *eh_frame;
13103
13104 sec->gc_mark = 1;
13105
13106 /* Mark all the sections in the group. */
13107 group_sec = elf_section_data (sec)->next_in_group;
13108 if (group_sec && !group_sec->gc_mark)
13109 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
13110 return FALSE;
13111
13112 /* Look through the section relocs. */
13113 ret = TRUE;
13114 eh_frame = elf_eh_frame_section (sec->owner);
13115 if ((sec->flags & SEC_RELOC) != 0
13116 && sec->reloc_count > 0
13117 && sec != eh_frame)
13118 {
13119 struct elf_reloc_cookie cookie;
13120
13121 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13122 ret = FALSE;
13123 else
13124 {
13125 for (; cookie.rel < cookie.relend; cookie.rel++)
13126 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
13127 {
13128 ret = FALSE;
13129 break;
13130 }
13131 fini_reloc_cookie_for_section (&cookie, sec);
13132 }
13133 }
13134
13135 if (ret && eh_frame && elf_fde_list (sec))
13136 {
13137 struct elf_reloc_cookie cookie;
13138
13139 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13140 ret = FALSE;
13141 else
13142 {
13143 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13144 gc_mark_hook, &cookie))
13145 ret = FALSE;
13146 fini_reloc_cookie_for_section (&cookie, eh_frame);
13147 }
13148 }
13149
13150 eh_frame = elf_section_eh_frame_entry (sec);
13151 if (ret && eh_frame && !eh_frame->gc_mark)
13152 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13153 ret = FALSE;
13154
13155 return ret;
13156 }
13157
13158 /* Scan and mark sections in a special or debug section group. */
13159
13160 static void
13161 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13162 {
13163 /* Point to first section of section group. */
13164 asection *ssec;
13165 /* Used to iterate the section group. */
13166 asection *msec;
13167
13168 bfd_boolean is_special_grp = TRUE;
13169 bfd_boolean is_debug_grp = TRUE;
13170
13171 /* First scan to see if group contains any section other than debug
13172 and special section. */
13173 ssec = msec = elf_next_in_group (grp);
13174 do
13175 {
13176 if ((msec->flags & SEC_DEBUGGING) == 0)
13177 is_debug_grp = FALSE;
13178
13179 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13180 is_special_grp = FALSE;
13181
13182 msec = elf_next_in_group (msec);
13183 }
13184 while (msec != ssec);
13185
13186 /* If this is a pure debug section group or pure special section group,
13187 keep all sections in this group. */
13188 if (is_debug_grp || is_special_grp)
13189 {
13190 do
13191 {
13192 msec->gc_mark = 1;
13193 msec = elf_next_in_group (msec);
13194 }
13195 while (msec != ssec);
13196 }
13197 }
13198
13199 /* Keep debug and special sections. */
13200
13201 bfd_boolean
13202 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13203 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
13204 {
13205 bfd *ibfd;
13206
13207 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13208 {
13209 asection *isec;
13210 bfd_boolean some_kept;
13211 bfd_boolean debug_frag_seen;
13212 bfd_boolean has_kept_debug_info;
13213
13214 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13215 continue;
13216 isec = ibfd->sections;
13217 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13218 continue;
13219
13220 /* Ensure all linker created sections are kept,
13221 see if any other section is already marked,
13222 and note if we have any fragmented debug sections. */
13223 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
13224 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13225 {
13226 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13227 isec->gc_mark = 1;
13228 else if (isec->gc_mark
13229 && (isec->flags & SEC_ALLOC) != 0
13230 && elf_section_type (isec) != SHT_NOTE)
13231 some_kept = TRUE;
13232
13233 if (!debug_frag_seen
13234 && (isec->flags & SEC_DEBUGGING)
13235 && CONST_STRNEQ (isec->name, ".debug_line."))
13236 debug_frag_seen = TRUE;
13237 }
13238
13239 /* If no non-note alloc section in this file will be kept, then
13240 we can toss out the debug and special sections. */
13241 if (!some_kept)
13242 continue;
13243
13244 /* Keep debug and special sections like .comment when they are
13245 not part of a group. Also keep section groups that contain
13246 just debug sections or special sections. */
13247 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13248 {
13249 if ((isec->flags & SEC_GROUP) != 0)
13250 _bfd_elf_gc_mark_debug_special_section_group (isec);
13251 else if (((isec->flags & SEC_DEBUGGING) != 0
13252 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13253 && elf_next_in_group (isec) == NULL)
13254 isec->gc_mark = 1;
13255 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13256 has_kept_debug_info = TRUE;
13257 }
13258
13259 /* Look for CODE sections which are going to be discarded,
13260 and find and discard any fragmented debug sections which
13261 are associated with that code section. */
13262 if (debug_frag_seen)
13263 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13264 if ((isec->flags & SEC_CODE) != 0
13265 && isec->gc_mark == 0)
13266 {
13267 unsigned int ilen;
13268 asection *dsec;
13269
13270 ilen = strlen (isec->name);
13271
13272 /* Association is determined by the name of the debug
13273 section containing the name of the code section as
13274 a suffix. For example .debug_line.text.foo is a
13275 debug section associated with .text.foo. */
13276 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13277 {
13278 unsigned int dlen;
13279
13280 if (dsec->gc_mark == 0
13281 || (dsec->flags & SEC_DEBUGGING) == 0)
13282 continue;
13283
13284 dlen = strlen (dsec->name);
13285
13286 if (dlen > ilen
13287 && strncmp (dsec->name + (dlen - ilen),
13288 isec->name, ilen) == 0)
13289 dsec->gc_mark = 0;
13290 }
13291 }
13292
13293 /* Mark debug sections referenced by kept debug sections. */
13294 if (has_kept_debug_info)
13295 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13296 if (isec->gc_mark
13297 && (isec->flags & SEC_DEBUGGING) != 0)
13298 if (!_bfd_elf_gc_mark (info, isec,
13299 elf_gc_mark_debug_section))
13300 return FALSE;
13301 }
13302 return TRUE;
13303 }
13304
13305 static bfd_boolean
13306 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
13307 {
13308 bfd *sub;
13309 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13310
13311 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13312 {
13313 asection *o;
13314
13315 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13316 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
13317 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13318 continue;
13319 o = sub->sections;
13320 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13321 continue;
13322
13323 for (o = sub->sections; o != NULL; o = o->next)
13324 {
13325 /* When any section in a section group is kept, we keep all
13326 sections in the section group. If the first member of
13327 the section group is excluded, we will also exclude the
13328 group section. */
13329 if (o->flags & SEC_GROUP)
13330 {
13331 asection *first = elf_next_in_group (o);
13332 o->gc_mark = first->gc_mark;
13333 }
13334
13335 if (o->gc_mark)
13336 continue;
13337
13338 /* Skip sweeping sections already excluded. */
13339 if (o->flags & SEC_EXCLUDE)
13340 continue;
13341
13342 /* Since this is early in the link process, it is simple
13343 to remove a section from the output. */
13344 o->flags |= SEC_EXCLUDE;
13345
13346 if (info->print_gc_sections && o->size != 0)
13347 /* xgettext:c-format */
13348 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
13349 o, sub);
13350 }
13351 }
13352
13353 return TRUE;
13354 }
13355
13356 /* Propagate collected vtable information. This is called through
13357 elf_link_hash_traverse. */
13358
13359 static bfd_boolean
13360 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13361 {
13362 /* Those that are not vtables. */
13363 if (h->start_stop
13364 || h->u2.vtable == NULL
13365 || h->u2.vtable->parent == NULL)
13366 return TRUE;
13367
13368 /* Those vtables that do not have parents, we cannot merge. */
13369 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
13370 return TRUE;
13371
13372 /* If we've already been done, exit. */
13373 if (h->u2.vtable->used && h->u2.vtable->used[-1])
13374 return TRUE;
13375
13376 /* Make sure the parent's table is up to date. */
13377 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
13378
13379 if (h->u2.vtable->used == NULL)
13380 {
13381 /* None of this table's entries were referenced. Re-use the
13382 parent's table. */
13383 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13384 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
13385 }
13386 else
13387 {
13388 size_t n;
13389 bfd_boolean *cu, *pu;
13390
13391 /* Or the parent's entries into ours. */
13392 cu = h->u2.vtable->used;
13393 cu[-1] = TRUE;
13394 pu = h->u2.vtable->parent->u2.vtable->used;
13395 if (pu != NULL)
13396 {
13397 const struct elf_backend_data *bed;
13398 unsigned int log_file_align;
13399
13400 bed = get_elf_backend_data (h->root.u.def.section->owner);
13401 log_file_align = bed->s->log_file_align;
13402 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
13403 while (n--)
13404 {
13405 if (*pu)
13406 *cu = TRUE;
13407 pu++;
13408 cu++;
13409 }
13410 }
13411 }
13412
13413 return TRUE;
13414 }
13415
13416 static bfd_boolean
13417 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13418 {
13419 asection *sec;
13420 bfd_vma hstart, hend;
13421 Elf_Internal_Rela *relstart, *relend, *rel;
13422 const struct elf_backend_data *bed;
13423 unsigned int log_file_align;
13424
13425 /* Take care of both those symbols that do not describe vtables as
13426 well as those that are not loaded. */
13427 if (h->start_stop
13428 || h->u2.vtable == NULL
13429 || h->u2.vtable->parent == NULL)
13430 return TRUE;
13431
13432 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13433 || h->root.type == bfd_link_hash_defweak);
13434
13435 sec = h->root.u.def.section;
13436 hstart = h->root.u.def.value;
13437 hend = hstart + h->size;
13438
13439 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13440 if (!relstart)
13441 return *(bfd_boolean *) okp = FALSE;
13442 bed = get_elf_backend_data (sec->owner);
13443 log_file_align = bed->s->log_file_align;
13444
13445 relend = relstart + sec->reloc_count;
13446
13447 for (rel = relstart; rel < relend; ++rel)
13448 if (rel->r_offset >= hstart && rel->r_offset < hend)
13449 {
13450 /* If the entry is in use, do nothing. */
13451 if (h->u2.vtable->used
13452 && (rel->r_offset - hstart) < h->u2.vtable->size)
13453 {
13454 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13455 if (h->u2.vtable->used[entry])
13456 continue;
13457 }
13458 /* Otherwise, kill it. */
13459 rel->r_offset = rel->r_info = rel->r_addend = 0;
13460 }
13461
13462 return TRUE;
13463 }
13464
13465 /* Mark sections containing dynamically referenced symbols. When
13466 building shared libraries, we must assume that any visible symbol is
13467 referenced. */
13468
13469 bfd_boolean
13470 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
13471 {
13472 struct bfd_link_info *info = (struct bfd_link_info *) inf;
13473 struct bfd_elf_dynamic_list *d = info->dynamic_list;
13474
13475 if ((h->root.type == bfd_link_hash_defined
13476 || h->root.type == bfd_link_hash_defweak)
13477 && ((h->ref_dynamic && !h->forced_local)
13478 || ((h->def_regular || ELF_COMMON_DEF_P (h))
13479 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
13480 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
13481 && (!bfd_link_executable (info)
13482 || info->gc_keep_exported
13483 || info->export_dynamic
13484 || (h->dynamic
13485 && d != NULL
13486 && (*d->match) (&d->head, NULL, h->root.root.string)))
13487 && (h->versioned >= versioned
13488 || !bfd_hide_sym_by_version (info->version_info,
13489 h->root.root.string)))))
13490 h->root.u.def.section->flags |= SEC_KEEP;
13491
13492 return TRUE;
13493 }
13494
13495 /* Keep all sections containing symbols undefined on the command-line,
13496 and the section containing the entry symbol. */
13497
13498 void
13499 _bfd_elf_gc_keep (struct bfd_link_info *info)
13500 {
13501 struct bfd_sym_chain *sym;
13502
13503 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13504 {
13505 struct elf_link_hash_entry *h;
13506
13507 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13508 FALSE, FALSE, FALSE);
13509
13510 if (h != NULL
13511 && (h->root.type == bfd_link_hash_defined
13512 || h->root.type == bfd_link_hash_defweak)
13513 && !bfd_is_abs_section (h->root.u.def.section)
13514 && !bfd_is_und_section (h->root.u.def.section))
13515 h->root.u.def.section->flags |= SEC_KEEP;
13516 }
13517 }
13518
13519 bfd_boolean
13520 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13521 struct bfd_link_info *info)
13522 {
13523 bfd *ibfd = info->input_bfds;
13524
13525 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13526 {
13527 asection *sec;
13528 struct elf_reloc_cookie cookie;
13529
13530 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13531 continue;
13532 sec = ibfd->sections;
13533 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13534 continue;
13535
13536 if (!init_reloc_cookie (&cookie, info, ibfd))
13537 return FALSE;
13538
13539 for (sec = ibfd->sections; sec; sec = sec->next)
13540 {
13541 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13542 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13543 {
13544 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13545 fini_reloc_cookie_rels (&cookie, sec);
13546 }
13547 }
13548 }
13549 return TRUE;
13550 }
13551
13552 /* Do mark and sweep of unused sections. */
13553
13554 bfd_boolean
13555 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13556 {
13557 bfd_boolean ok = TRUE;
13558 bfd *sub;
13559 elf_gc_mark_hook_fn gc_mark_hook;
13560 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13561 struct elf_link_hash_table *htab;
13562
13563 if (!bed->can_gc_sections
13564 || !is_elf_hash_table (info->hash))
13565 {
13566 _bfd_error_handler(_("warning: gc-sections option ignored"));
13567 return TRUE;
13568 }
13569
13570 bed->gc_keep (info);
13571 htab = elf_hash_table (info);
13572
13573 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13574 at the .eh_frame section if we can mark the FDEs individually. */
13575 for (sub = info->input_bfds;
13576 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13577 sub = sub->link.next)
13578 {
13579 asection *sec;
13580 struct elf_reloc_cookie cookie;
13581
13582 sec = sub->sections;
13583 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13584 continue;
13585 sec = bfd_get_section_by_name (sub, ".eh_frame");
13586 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
13587 {
13588 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
13589 if (elf_section_data (sec)->sec_info
13590 && (sec->flags & SEC_LINKER_CREATED) == 0)
13591 elf_eh_frame_section (sub) = sec;
13592 fini_reloc_cookie_for_section (&cookie, sec);
13593 sec = bfd_get_next_section_by_name (NULL, sec);
13594 }
13595 }
13596
13597 /* Apply transitive closure to the vtable entry usage info. */
13598 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
13599 if (!ok)
13600 return FALSE;
13601
13602 /* Kill the vtable relocations that were not used. */
13603 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
13604 if (!ok)
13605 return FALSE;
13606
13607 /* Mark dynamically referenced symbols. */
13608 if (htab->dynamic_sections_created || info->gc_keep_exported)
13609 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
13610
13611 /* Grovel through relocs to find out who stays ... */
13612 gc_mark_hook = bed->gc_mark_hook;
13613 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13614 {
13615 asection *o;
13616
13617 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13618 || elf_object_id (sub) != elf_hash_table_id (htab)
13619 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13620 continue;
13621
13622 o = sub->sections;
13623 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13624 continue;
13625
13626 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13627 Also treat note sections as a root, if the section is not part
13628 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13629 well as FINI_ARRAY sections for ld -r. */
13630 for (o = sub->sections; o != NULL; o = o->next)
13631 if (!o->gc_mark
13632 && (o->flags & SEC_EXCLUDE) == 0
13633 && ((o->flags & SEC_KEEP) != 0
13634 || (bfd_link_relocatable (info)
13635 && ((elf_section_data (o)->this_hdr.sh_type
13636 == SHT_PREINIT_ARRAY)
13637 || (elf_section_data (o)->this_hdr.sh_type
13638 == SHT_INIT_ARRAY)
13639 || (elf_section_data (o)->this_hdr.sh_type
13640 == SHT_FINI_ARRAY)))
13641 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13642 && elf_next_in_group (o) == NULL )))
13643 {
13644 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13645 return FALSE;
13646 }
13647 }
13648
13649 /* Allow the backend to mark additional target specific sections. */
13650 bed->gc_mark_extra_sections (info, gc_mark_hook);
13651
13652 /* ... and mark SEC_EXCLUDE for those that go. */
13653 return elf_gc_sweep (abfd, info);
13654 }
13655 \f
13656 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13657
13658 bfd_boolean
13659 bfd_elf_gc_record_vtinherit (bfd *abfd,
13660 asection *sec,
13661 struct elf_link_hash_entry *h,
13662 bfd_vma offset)
13663 {
13664 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13665 struct elf_link_hash_entry **search, *child;
13666 size_t extsymcount;
13667 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13668
13669 /* The sh_info field of the symtab header tells us where the
13670 external symbols start. We don't care about the local symbols at
13671 this point. */
13672 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13673 if (!elf_bad_symtab (abfd))
13674 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13675
13676 sym_hashes = elf_sym_hashes (abfd);
13677 sym_hashes_end = sym_hashes + extsymcount;
13678
13679 /* Hunt down the child symbol, which is in this section at the same
13680 offset as the relocation. */
13681 for (search = sym_hashes; search != sym_hashes_end; ++search)
13682 {
13683 if ((child = *search) != NULL
13684 && (child->root.type == bfd_link_hash_defined
13685 || child->root.type == bfd_link_hash_defweak)
13686 && child->root.u.def.section == sec
13687 && child->root.u.def.value == offset)
13688 goto win;
13689 }
13690
13691 /* xgettext:c-format */
13692 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
13693 abfd, sec, (uint64_t) offset);
13694 bfd_set_error (bfd_error_invalid_operation);
13695 return FALSE;
13696
13697 win:
13698 if (!child->u2.vtable)
13699 {
13700 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13701 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13702 if (!child->u2.vtable)
13703 return FALSE;
13704 }
13705 if (!h)
13706 {
13707 /* This *should* only be the absolute section. It could potentially
13708 be that someone has defined a non-global vtable though, which
13709 would be bad. It isn't worth paging in the local symbols to be
13710 sure though; that case should simply be handled by the assembler. */
13711
13712 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
13713 }
13714 else
13715 child->u2.vtable->parent = h;
13716
13717 return TRUE;
13718 }
13719
13720 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13721
13722 bfd_boolean
13723 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13724 asection *sec ATTRIBUTE_UNUSED,
13725 struct elf_link_hash_entry *h,
13726 bfd_vma addend)
13727 {
13728 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13729 unsigned int log_file_align = bed->s->log_file_align;
13730
13731 if (!h->u2.vtable)
13732 {
13733 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
13734 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
13735 if (!h->u2.vtable)
13736 return FALSE;
13737 }
13738
13739 if (addend >= h->u2.vtable->size)
13740 {
13741 size_t size, bytes, file_align;
13742 bfd_boolean *ptr = h->u2.vtable->used;
13743
13744 /* While the symbol is undefined, we have to be prepared to handle
13745 a zero size. */
13746 file_align = 1 << log_file_align;
13747 if (h->root.type == bfd_link_hash_undefined)
13748 size = addend + file_align;
13749 else
13750 {
13751 size = h->size;
13752 if (addend >= size)
13753 {
13754 /* Oops! We've got a reference past the defined end of
13755 the table. This is probably a bug -- shall we warn? */
13756 size = addend + file_align;
13757 }
13758 }
13759 size = (size + file_align - 1) & -file_align;
13760
13761 /* Allocate one extra entry for use as a "done" flag for the
13762 consolidation pass. */
13763 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13764
13765 if (ptr)
13766 {
13767 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
13768
13769 if (ptr != NULL)
13770 {
13771 size_t oldbytes;
13772
13773 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
13774 * sizeof (bfd_boolean));
13775 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13776 }
13777 }
13778 else
13779 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
13780
13781 if (ptr == NULL)
13782 return FALSE;
13783
13784 /* And arrange for that done flag to be at index -1. */
13785 h->u2.vtable->used = ptr + 1;
13786 h->u2.vtable->size = size;
13787 }
13788
13789 h->u2.vtable->used[addend >> log_file_align] = TRUE;
13790
13791 return TRUE;
13792 }
13793
13794 /* Map an ELF section header flag to its corresponding string. */
13795 typedef struct
13796 {
13797 char *flag_name;
13798 flagword flag_value;
13799 } elf_flags_to_name_table;
13800
13801 static elf_flags_to_name_table elf_flags_to_names [] =
13802 {
13803 { "SHF_WRITE", SHF_WRITE },
13804 { "SHF_ALLOC", SHF_ALLOC },
13805 { "SHF_EXECINSTR", SHF_EXECINSTR },
13806 { "SHF_MERGE", SHF_MERGE },
13807 { "SHF_STRINGS", SHF_STRINGS },
13808 { "SHF_INFO_LINK", SHF_INFO_LINK},
13809 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13810 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13811 { "SHF_GROUP", SHF_GROUP },
13812 { "SHF_TLS", SHF_TLS },
13813 { "SHF_MASKOS", SHF_MASKOS },
13814 { "SHF_EXCLUDE", SHF_EXCLUDE },
13815 };
13816
13817 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13818 bfd_boolean
13819 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
13820 struct flag_info *flaginfo,
13821 asection *section)
13822 {
13823 const bfd_vma sh_flags = elf_section_flags (section);
13824
13825 if (!flaginfo->flags_initialized)
13826 {
13827 bfd *obfd = info->output_bfd;
13828 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13829 struct flag_info_list *tf = flaginfo->flag_list;
13830 int with_hex = 0;
13831 int without_hex = 0;
13832
13833 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13834 {
13835 unsigned i;
13836 flagword (*lookup) (char *);
13837
13838 lookup = bed->elf_backend_lookup_section_flags_hook;
13839 if (lookup != NULL)
13840 {
13841 flagword hexval = (*lookup) ((char *) tf->name);
13842
13843 if (hexval != 0)
13844 {
13845 if (tf->with == with_flags)
13846 with_hex |= hexval;
13847 else if (tf->with == without_flags)
13848 without_hex |= hexval;
13849 tf->valid = TRUE;
13850 continue;
13851 }
13852 }
13853 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13854 {
13855 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
13856 {
13857 if (tf->with == with_flags)
13858 with_hex |= elf_flags_to_names[i].flag_value;
13859 else if (tf->with == without_flags)
13860 without_hex |= elf_flags_to_names[i].flag_value;
13861 tf->valid = TRUE;
13862 break;
13863 }
13864 }
13865 if (!tf->valid)
13866 {
13867 info->callbacks->einfo
13868 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
13869 return FALSE;
13870 }
13871 }
13872 flaginfo->flags_initialized = TRUE;
13873 flaginfo->only_with_flags |= with_hex;
13874 flaginfo->not_with_flags |= without_hex;
13875 }
13876
13877 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13878 return FALSE;
13879
13880 if ((flaginfo->not_with_flags & sh_flags) != 0)
13881 return FALSE;
13882
13883 return TRUE;
13884 }
13885
13886 struct alloc_got_off_arg {
13887 bfd_vma gotoff;
13888 struct bfd_link_info *info;
13889 };
13890
13891 /* We need a special top-level link routine to convert got reference counts
13892 to real got offsets. */
13893
13894 static bfd_boolean
13895 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13896 {
13897 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13898 bfd *obfd = gofarg->info->output_bfd;
13899 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13900
13901 if (h->got.refcount > 0)
13902 {
13903 h->got.offset = gofarg->gotoff;
13904 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13905 }
13906 else
13907 h->got.offset = (bfd_vma) -1;
13908
13909 return TRUE;
13910 }
13911
13912 /* And an accompanying bit to work out final got entry offsets once
13913 we're done. Should be called from final_link. */
13914
13915 bfd_boolean
13916 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13917 struct bfd_link_info *info)
13918 {
13919 bfd *i;
13920 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13921 bfd_vma gotoff;
13922 struct alloc_got_off_arg gofarg;
13923
13924 BFD_ASSERT (abfd == info->output_bfd);
13925
13926 if (! is_elf_hash_table (info->hash))
13927 return FALSE;
13928
13929 /* The GOT offset is relative to the .got section, but the GOT header is
13930 put into the .got.plt section, if the backend uses it. */
13931 if (bed->want_got_plt)
13932 gotoff = 0;
13933 else
13934 gotoff = bed->got_header_size;
13935
13936 /* Do the local .got entries first. */
13937 for (i = info->input_bfds; i; i = i->link.next)
13938 {
13939 bfd_signed_vma *local_got;
13940 size_t j, locsymcount;
13941 Elf_Internal_Shdr *symtab_hdr;
13942
13943 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13944 continue;
13945
13946 local_got = elf_local_got_refcounts (i);
13947 if (!local_got)
13948 continue;
13949
13950 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13951 if (elf_bad_symtab (i))
13952 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13953 else
13954 locsymcount = symtab_hdr->sh_info;
13955
13956 for (j = 0; j < locsymcount; ++j)
13957 {
13958 if (local_got[j] > 0)
13959 {
13960 local_got[j] = gotoff;
13961 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
13962 }
13963 else
13964 local_got[j] = (bfd_vma) -1;
13965 }
13966 }
13967
13968 /* Then the global .got entries. .plt refcounts are handled by
13969 adjust_dynamic_symbol */
13970 gofarg.gotoff = gotoff;
13971 gofarg.info = info;
13972 elf_link_hash_traverse (elf_hash_table (info),
13973 elf_gc_allocate_got_offsets,
13974 &gofarg);
13975 return TRUE;
13976 }
13977
13978 /* Many folk need no more in the way of final link than this, once
13979 got entry reference counting is enabled. */
13980
13981 bfd_boolean
13982 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13983 {
13984 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13985 return FALSE;
13986
13987 /* Invoke the regular ELF backend linker to do all the work. */
13988 return bfd_elf_final_link (abfd, info);
13989 }
13990
13991 bfd_boolean
13992 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13993 {
13994 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
13995
13996 if (rcookie->bad_symtab)
13997 rcookie->rel = rcookie->rels;
13998
13999 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14000 {
14001 unsigned long r_symndx;
14002
14003 if (! rcookie->bad_symtab)
14004 if (rcookie->rel->r_offset > offset)
14005 return FALSE;
14006 if (rcookie->rel->r_offset != offset)
14007 continue;
14008
14009 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
14010 if (r_symndx == STN_UNDEF)
14011 return TRUE;
14012
14013 if (r_symndx >= rcookie->locsymcount
14014 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14015 {
14016 struct elf_link_hash_entry *h;
14017
14018 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14019
14020 while (h->root.type == bfd_link_hash_indirect
14021 || h->root.type == bfd_link_hash_warning)
14022 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14023
14024 if ((h->root.type == bfd_link_hash_defined
14025 || h->root.type == bfd_link_hash_defweak)
14026 && (h->root.u.def.section->owner != rcookie->abfd
14027 || h->root.u.def.section->kept_section != NULL
14028 || discarded_section (h->root.u.def.section)))
14029 return TRUE;
14030 }
14031 else
14032 {
14033 /* It's not a relocation against a global symbol,
14034 but it could be a relocation against a local
14035 symbol for a discarded section. */
14036 asection *isec;
14037 Elf_Internal_Sym *isym;
14038
14039 /* Need to: get the symbol; get the section. */
14040 isym = &rcookie->locsyms[r_symndx];
14041 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
14042 if (isec != NULL
14043 && (isec->kept_section != NULL
14044 || discarded_section (isec)))
14045 return TRUE;
14046 }
14047 return FALSE;
14048 }
14049 return FALSE;
14050 }
14051
14052 /* Discard unneeded references to discarded sections.
14053 Returns -1 on error, 1 if any section's size was changed, 0 if
14054 nothing changed. This function assumes that the relocations are in
14055 sorted order, which is true for all known assemblers. */
14056
14057 int
14058 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14059 {
14060 struct elf_reloc_cookie cookie;
14061 asection *o;
14062 bfd *abfd;
14063 int changed = 0;
14064
14065 if (info->traditional_format
14066 || !is_elf_hash_table (info->hash))
14067 return 0;
14068
14069 o = bfd_get_section_by_name (output_bfd, ".stab");
14070 if (o != NULL)
14071 {
14072 asection *i;
14073
14074 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14075 {
14076 if (i->size == 0
14077 || i->reloc_count == 0
14078 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14079 continue;
14080
14081 abfd = i->owner;
14082 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14083 continue;
14084
14085 if (!init_reloc_cookie_for_section (&cookie, info, i))
14086 return -1;
14087
14088 if (_bfd_discard_section_stabs (abfd, i,
14089 elf_section_data (i)->sec_info,
14090 bfd_elf_reloc_symbol_deleted_p,
14091 &cookie))
14092 changed = 1;
14093
14094 fini_reloc_cookie_for_section (&cookie, i);
14095 }
14096 }
14097
14098 o = NULL;
14099 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14100 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
14101 if (o != NULL)
14102 {
14103 asection *i;
14104 int eh_changed = 0;
14105 unsigned int eh_alignment;
14106
14107 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14108 {
14109 if (i->size == 0)
14110 continue;
14111
14112 abfd = i->owner;
14113 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14114 continue;
14115
14116 if (!init_reloc_cookie_for_section (&cookie, info, i))
14117 return -1;
14118
14119 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14120 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
14121 bfd_elf_reloc_symbol_deleted_p,
14122 &cookie))
14123 {
14124 eh_changed = 1;
14125 if (i->size != i->rawsize)
14126 changed = 1;
14127 }
14128
14129 fini_reloc_cookie_for_section (&cookie, i);
14130 }
14131
14132 eh_alignment = 1 << o->alignment_power;
14133 /* Skip over zero terminator, and prevent empty sections from
14134 adding alignment padding at the end. */
14135 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14136 if (i->size == 0)
14137 i->flags |= SEC_EXCLUDE;
14138 else if (i->size > 4)
14139 break;
14140 /* The last non-empty eh_frame section doesn't need padding. */
14141 if (i != NULL)
14142 i = i->map_tail.s;
14143 /* Any prior sections must pad the last FDE out to the output
14144 section alignment. Otherwise we might have zero padding
14145 between sections, which would be seen as a terminator. */
14146 for (; i != NULL; i = i->map_tail.s)
14147 if (i->size == 4)
14148 /* All but the last zero terminator should have been removed. */
14149 BFD_FAIL ();
14150 else
14151 {
14152 bfd_size_type size
14153 = (i->size + eh_alignment - 1) & -eh_alignment;
14154 if (i->size != size)
14155 {
14156 i->size = size;
14157 changed = 1;
14158 eh_changed = 1;
14159 }
14160 }
14161 if (eh_changed)
14162 elf_link_hash_traverse (elf_hash_table (info),
14163 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
14164 }
14165
14166 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14167 {
14168 const struct elf_backend_data *bed;
14169 asection *s;
14170
14171 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14172 continue;
14173 s = abfd->sections;
14174 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14175 continue;
14176
14177 bed = get_elf_backend_data (abfd);
14178
14179 if (bed->elf_backend_discard_info != NULL)
14180 {
14181 if (!init_reloc_cookie (&cookie, info, abfd))
14182 return -1;
14183
14184 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
14185 changed = 1;
14186
14187 fini_reloc_cookie (&cookie, abfd);
14188 }
14189 }
14190
14191 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14192 _bfd_elf_end_eh_frame_parsing (info);
14193
14194 if (info->eh_frame_hdr_type
14195 && !bfd_link_relocatable (info)
14196 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
14197 changed = 1;
14198
14199 return changed;
14200 }
14201
14202 bfd_boolean
14203 _bfd_elf_section_already_linked (bfd *abfd,
14204 asection *sec,
14205 struct bfd_link_info *info)
14206 {
14207 flagword flags;
14208 const char *name, *key;
14209 struct bfd_section_already_linked *l;
14210 struct bfd_section_already_linked_hash_entry *already_linked_list;
14211
14212 if (sec->output_section == bfd_abs_section_ptr)
14213 return FALSE;
14214
14215 flags = sec->flags;
14216
14217 /* Return if it isn't a linkonce section. A comdat group section
14218 also has SEC_LINK_ONCE set. */
14219 if ((flags & SEC_LINK_ONCE) == 0)
14220 return FALSE;
14221
14222 /* Don't put group member sections on our list of already linked
14223 sections. They are handled as a group via their group section. */
14224 if (elf_sec_group (sec) != NULL)
14225 return FALSE;
14226
14227 /* For a SHT_GROUP section, use the group signature as the key. */
14228 name = sec->name;
14229 if ((flags & SEC_GROUP) != 0
14230 && elf_next_in_group (sec) != NULL
14231 && elf_group_name (elf_next_in_group (sec)) != NULL)
14232 key = elf_group_name (elf_next_in_group (sec));
14233 else
14234 {
14235 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14236 if (CONST_STRNEQ (name, ".gnu.linkonce.")
14237 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14238 key++;
14239 else
14240 /* Must be a user linkonce section that doesn't follow gcc's
14241 naming convention. In this case we won't be matching
14242 single member groups. */
14243 key = name;
14244 }
14245
14246 already_linked_list = bfd_section_already_linked_table_lookup (key);
14247
14248 for (l = already_linked_list->entry; l != NULL; l = l->next)
14249 {
14250 /* We may have 2 different types of sections on the list: group
14251 sections with a signature of <key> (<key> is some string),
14252 and linkonce sections named .gnu.linkonce.<type>.<key>.
14253 Match like sections. LTO plugin sections are an exception.
14254 They are always named .gnu.linkonce.t.<key> and match either
14255 type of section. */
14256 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14257 && ((flags & SEC_GROUP) != 0
14258 || strcmp (name, l->sec->name) == 0))
14259 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
14260 {
14261 /* The section has already been linked. See if we should
14262 issue a warning. */
14263 if (!_bfd_handle_already_linked (sec, l, info))
14264 return FALSE;
14265
14266 if (flags & SEC_GROUP)
14267 {
14268 asection *first = elf_next_in_group (sec);
14269 asection *s = first;
14270
14271 while (s != NULL)
14272 {
14273 s->output_section = bfd_abs_section_ptr;
14274 /* Record which group discards it. */
14275 s->kept_section = l->sec;
14276 s = elf_next_in_group (s);
14277 /* These lists are circular. */
14278 if (s == first)
14279 break;
14280 }
14281 }
14282
14283 return TRUE;
14284 }
14285 }
14286
14287 /* A single member comdat group section may be discarded by a
14288 linkonce section and vice versa. */
14289 if ((flags & SEC_GROUP) != 0)
14290 {
14291 asection *first = elf_next_in_group (sec);
14292
14293 if (first != NULL && elf_next_in_group (first) == first)
14294 /* Check this single member group against linkonce sections. */
14295 for (l = already_linked_list->entry; l != NULL; l = l->next)
14296 if ((l->sec->flags & SEC_GROUP) == 0
14297 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14298 {
14299 first->output_section = bfd_abs_section_ptr;
14300 first->kept_section = l->sec;
14301 sec->output_section = bfd_abs_section_ptr;
14302 break;
14303 }
14304 }
14305 else
14306 /* Check this linkonce section against single member groups. */
14307 for (l = already_linked_list->entry; l != NULL; l = l->next)
14308 if (l->sec->flags & SEC_GROUP)
14309 {
14310 asection *first = elf_next_in_group (l->sec);
14311
14312 if (first != NULL
14313 && elf_next_in_group (first) == first
14314 && bfd_elf_match_symbols_in_sections (first, sec, info))
14315 {
14316 sec->output_section = bfd_abs_section_ptr;
14317 sec->kept_section = first;
14318 break;
14319 }
14320 }
14321
14322 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14323 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14324 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14325 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14326 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14327 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14328 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14329 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14330 The reverse order cannot happen as there is never a bfd with only the
14331 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14332 matter as here were are looking only for cross-bfd sections. */
14333
14334 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14335 for (l = already_linked_list->entry; l != NULL; l = l->next)
14336 if ((l->sec->flags & SEC_GROUP) == 0
14337 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14338 {
14339 if (abfd != l->sec->owner)
14340 sec->output_section = bfd_abs_section_ptr;
14341 break;
14342 }
14343
14344 /* This is the first section with this name. Record it. */
14345 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
14346 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
14347 return sec->output_section == bfd_abs_section_ptr;
14348 }
14349
14350 bfd_boolean
14351 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
14352 {
14353 return sym->st_shndx == SHN_COMMON;
14354 }
14355
14356 unsigned int
14357 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14358 {
14359 return SHN_COMMON;
14360 }
14361
14362 asection *
14363 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14364 {
14365 return bfd_com_section_ptr;
14366 }
14367
14368 bfd_vma
14369 _bfd_elf_default_got_elt_size (bfd *abfd,
14370 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14371 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14372 bfd *ibfd ATTRIBUTE_UNUSED,
14373 unsigned long symndx ATTRIBUTE_UNUSED)
14374 {
14375 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14376 return bed->s->arch_size / 8;
14377 }
14378
14379 /* Routines to support the creation of dynamic relocs. */
14380
14381 /* Returns the name of the dynamic reloc section associated with SEC. */
14382
14383 static const char *
14384 get_dynamic_reloc_section_name (bfd * abfd,
14385 asection * sec,
14386 bfd_boolean is_rela)
14387 {
14388 char *name;
14389 const char *old_name = bfd_get_section_name (NULL, sec);
14390 const char *prefix = is_rela ? ".rela" : ".rel";
14391
14392 if (old_name == NULL)
14393 return NULL;
14394
14395 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
14396 sprintf (name, "%s%s", prefix, old_name);
14397
14398 return name;
14399 }
14400
14401 /* Returns the dynamic reloc section associated with SEC.
14402 If necessary compute the name of the dynamic reloc section based
14403 on SEC's name (looked up in ABFD's string table) and the setting
14404 of IS_RELA. */
14405
14406 asection *
14407 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14408 asection * sec,
14409 bfd_boolean is_rela)
14410 {
14411 asection * reloc_sec = elf_section_data (sec)->sreloc;
14412
14413 if (reloc_sec == NULL)
14414 {
14415 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14416
14417 if (name != NULL)
14418 {
14419 reloc_sec = bfd_get_linker_section (abfd, name);
14420
14421 if (reloc_sec != NULL)
14422 elf_section_data (sec)->sreloc = reloc_sec;
14423 }
14424 }
14425
14426 return reloc_sec;
14427 }
14428
14429 /* Returns the dynamic reloc section associated with SEC. If the
14430 section does not exist it is created and attached to the DYNOBJ
14431 bfd and stored in the SRELOC field of SEC's elf_section_data
14432 structure.
14433
14434 ALIGNMENT is the alignment for the newly created section and
14435 IS_RELA defines whether the name should be .rela.<SEC's name>
14436 or .rel.<SEC's name>. The section name is looked up in the
14437 string table associated with ABFD. */
14438
14439 asection *
14440 _bfd_elf_make_dynamic_reloc_section (asection *sec,
14441 bfd *dynobj,
14442 unsigned int alignment,
14443 bfd *abfd,
14444 bfd_boolean is_rela)
14445 {
14446 asection * reloc_sec = elf_section_data (sec)->sreloc;
14447
14448 if (reloc_sec == NULL)
14449 {
14450 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14451
14452 if (name == NULL)
14453 return NULL;
14454
14455 reloc_sec = bfd_get_linker_section (dynobj, name);
14456
14457 if (reloc_sec == NULL)
14458 {
14459 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14460 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
14461 if ((sec->flags & SEC_ALLOC) != 0)
14462 flags |= SEC_ALLOC | SEC_LOAD;
14463
14464 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
14465 if (reloc_sec != NULL)
14466 {
14467 /* _bfd_elf_get_sec_type_attr chooses a section type by
14468 name. Override as it may be wrong, eg. for a user
14469 section named "auto" we'll get ".relauto" which is
14470 seen to be a .rela section. */
14471 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
14472 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14473 reloc_sec = NULL;
14474 }
14475 }
14476
14477 elf_section_data (sec)->sreloc = reloc_sec;
14478 }
14479
14480 return reloc_sec;
14481 }
14482
14483 /* Copy the ELF symbol type and other attributes for a linker script
14484 assignment from HSRC to HDEST. Generally this should be treated as
14485 if we found a strong non-dynamic definition for HDEST (except that
14486 ld ignores multiple definition errors). */
14487 void
14488 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14489 struct bfd_link_hash_entry *hdest,
14490 struct bfd_link_hash_entry *hsrc)
14491 {
14492 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14493 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14494 Elf_Internal_Sym isym;
14495
14496 ehdest->type = ehsrc->type;
14497 ehdest->target_internal = ehsrc->target_internal;
14498
14499 isym.st_other = ehsrc->other;
14500 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
14501 }
14502
14503 /* Append a RELA relocation REL to section S in BFD. */
14504
14505 void
14506 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14507 {
14508 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14509 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14510 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14511 bed->s->swap_reloca_out (abfd, rel, loc);
14512 }
14513
14514 /* Append a REL relocation REL to section S in BFD. */
14515
14516 void
14517 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14518 {
14519 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14520 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14521 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
14522 bed->s->swap_reloc_out (abfd, rel, loc);
14523 }
14524
14525 /* Define __start, __stop, .startof. or .sizeof. symbol. */
14526
14527 struct bfd_link_hash_entry *
14528 bfd_elf_define_start_stop (struct bfd_link_info *info,
14529 const char *symbol, asection *sec)
14530 {
14531 struct elf_link_hash_entry *h;
14532
14533 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14534 FALSE, FALSE, TRUE);
14535 if (h != NULL
14536 && (h->root.type == bfd_link_hash_undefined
14537 || h->root.type == bfd_link_hash_undefweak
14538 || ((h->ref_regular || h->def_dynamic) && !h->def_regular)))
14539 {
14540 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
14541 h->root.type = bfd_link_hash_defined;
14542 h->root.u.def.section = sec;
14543 h->root.u.def.value = 0;
14544 h->def_regular = 1;
14545 h->def_dynamic = 0;
14546 h->start_stop = 1;
14547 h->u2.start_stop_section = sec;
14548 if (symbol[0] == '.')
14549 {
14550 /* .startof. and .sizeof. symbols are local. */
14551 const struct elf_backend_data *bed;
14552 bed = get_elf_backend_data (info->output_bfd);
14553 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
14554 }
14555 else
14556 {
14557 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14558 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
14559 if (was_dynamic)
14560 bfd_elf_link_record_dynamic_symbol (info, h);
14561 }
14562 return &h->root;
14563 }
14564 return NULL;
14565 }
This page took 0.385486 seconds and 5 git commands to generate.