Put .dynbss and .rel.bss shortcuts in main elf hash table
[deliverable/binutils-gdb.git] / bfd / elflink.c
1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2016 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
20
21 #include "sysdep.h"
22 #include "bfd.h"
23 #include "bfd_stdint.h"
24 #include "bfdlink.h"
25 #include "libbfd.h"
26 #define ARCH_SIZE 0
27 #include "elf-bfd.h"
28 #include "safe-ctype.h"
29 #include "libiberty.h"
30 #include "objalloc.h"
31 #if BFD_SUPPORTS_PLUGINS
32 #include "plugin-api.h"
33 #include "plugin.h"
34 #endif
35
36 /* This struct is used to pass information to routines called via
37 elf_link_hash_traverse which must return failure. */
38
39 struct elf_info_failed
40 {
41 struct bfd_link_info *info;
42 bfd_boolean failed;
43 };
44
45 /* This structure is used to pass information to
46 _bfd_elf_link_find_version_dependencies. */
47
48 struct elf_find_verdep_info
49 {
50 /* General link information. */
51 struct bfd_link_info *info;
52 /* The number of dependencies. */
53 unsigned int vers;
54 /* Whether we had a failure. */
55 bfd_boolean failed;
56 };
57
58 static bfd_boolean _bfd_elf_fix_symbol_flags
59 (struct elf_link_hash_entry *, struct elf_info_failed *);
60
61 asection *
62 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
63 unsigned long r_symndx,
64 bfd_boolean discard)
65 {
66 if (r_symndx >= cookie->locsymcount
67 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
68 {
69 struct elf_link_hash_entry *h;
70
71 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
72
73 while (h->root.type == bfd_link_hash_indirect
74 || h->root.type == bfd_link_hash_warning)
75 h = (struct elf_link_hash_entry *) h->root.u.i.link;
76
77 if ((h->root.type == bfd_link_hash_defined
78 || h->root.type == bfd_link_hash_defweak)
79 && discarded_section (h->root.u.def.section))
80 return h->root.u.def.section;
81 else
82 return NULL;
83 }
84 else
85 {
86 /* It's not a relocation against a global symbol,
87 but it could be a relocation against a local
88 symbol for a discarded section. */
89 asection *isec;
90 Elf_Internal_Sym *isym;
91
92 /* Need to: get the symbol; get the section. */
93 isym = &cookie->locsyms[r_symndx];
94 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
95 if (isec != NULL
96 && discard ? discarded_section (isec) : 1)
97 return isec;
98 }
99 return NULL;
100 }
101
102 /* Define a symbol in a dynamic linkage section. */
103
104 struct elf_link_hash_entry *
105 _bfd_elf_define_linkage_sym (bfd *abfd,
106 struct bfd_link_info *info,
107 asection *sec,
108 const char *name)
109 {
110 struct elf_link_hash_entry *h;
111 struct bfd_link_hash_entry *bh;
112 const struct elf_backend_data *bed;
113
114 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
115 if (h != NULL)
116 {
117 /* Zap symbol defined in an as-needed lib that wasn't linked.
118 This is a symptom of a larger problem: Absolute symbols
119 defined in shared libraries can't be overridden, because we
120 lose the link to the bfd which is via the symbol section. */
121 h->root.type = bfd_link_hash_new;
122 }
123
124 bh = &h->root;
125 bed = get_elf_backend_data (abfd);
126 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
127 sec, 0, NULL, FALSE, bed->collect,
128 &bh))
129 return NULL;
130 h = (struct elf_link_hash_entry *) bh;
131 h->def_regular = 1;
132 h->non_elf = 0;
133 h->root.linker_def = 1;
134 h->type = STT_OBJECT;
135 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
136 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
137
138 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
139 return h;
140 }
141
142 bfd_boolean
143 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
144 {
145 flagword flags;
146 asection *s;
147 struct elf_link_hash_entry *h;
148 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
149 struct elf_link_hash_table *htab = elf_hash_table (info);
150
151 /* This function may be called more than once. */
152 if (htab->sgot != NULL)
153 return TRUE;
154
155 flags = bed->dynamic_sec_flags;
156
157 s = bfd_make_section_anyway_with_flags (abfd,
158 (bed->rela_plts_and_copies_p
159 ? ".rela.got" : ".rel.got"),
160 (bed->dynamic_sec_flags
161 | SEC_READONLY));
162 if (s == NULL
163 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
164 return FALSE;
165 htab->srelgot = s;
166
167 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
168 if (s == NULL
169 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
170 return FALSE;
171 htab->sgot = s;
172
173 if (bed->want_got_plt)
174 {
175 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
176 if (s == NULL
177 || !bfd_set_section_alignment (abfd, s,
178 bed->s->log_file_align))
179 return FALSE;
180 htab->sgotplt = s;
181 }
182
183 /* The first bit of the global offset table is the header. */
184 s->size += bed->got_header_size;
185
186 if (bed->want_got_sym)
187 {
188 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
189 (or .got.plt) section. We don't do this in the linker script
190 because we don't want to define the symbol if we are not creating
191 a global offset table. */
192 h = _bfd_elf_define_linkage_sym (abfd, info, s,
193 "_GLOBAL_OFFSET_TABLE_");
194 elf_hash_table (info)->hgot = h;
195 if (h == NULL)
196 return FALSE;
197 }
198
199 return TRUE;
200 }
201 \f
202 /* Create a strtab to hold the dynamic symbol names. */
203 static bfd_boolean
204 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
205 {
206 struct elf_link_hash_table *hash_table;
207
208 hash_table = elf_hash_table (info);
209 if (hash_table->dynobj == NULL)
210 {
211 /* We may not set dynobj, an input file holding linker created
212 dynamic sections to abfd, which may be a dynamic object with
213 its own dynamic sections. We need to find a normal input file
214 to hold linker created sections if possible. */
215 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
216 {
217 bfd *ibfd;
218 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
219 if ((ibfd->flags
220 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0)
221 {
222 abfd = ibfd;
223 break;
224 }
225 }
226 hash_table->dynobj = abfd;
227 }
228
229 if (hash_table->dynstr == NULL)
230 {
231 hash_table->dynstr = _bfd_elf_strtab_init ();
232 if (hash_table->dynstr == NULL)
233 return FALSE;
234 }
235 return TRUE;
236 }
237
238 /* Create some sections which will be filled in with dynamic linking
239 information. ABFD is an input file which requires dynamic sections
240 to be created. The dynamic sections take up virtual memory space
241 when the final executable is run, so we need to create them before
242 addresses are assigned to the output sections. We work out the
243 actual contents and size of these sections later. */
244
245 bfd_boolean
246 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
247 {
248 flagword flags;
249 asection *s;
250 const struct elf_backend_data *bed;
251 struct elf_link_hash_entry *h;
252
253 if (! is_elf_hash_table (info->hash))
254 return FALSE;
255
256 if (elf_hash_table (info)->dynamic_sections_created)
257 return TRUE;
258
259 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
260 return FALSE;
261
262 abfd = elf_hash_table (info)->dynobj;
263 bed = get_elf_backend_data (abfd);
264
265 flags = bed->dynamic_sec_flags;
266
267 /* A dynamically linked executable has a .interp section, but a
268 shared library does not. */
269 if (bfd_link_executable (info) && !info->nointerp)
270 {
271 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
272 flags | SEC_READONLY);
273 if (s == NULL)
274 return FALSE;
275 }
276
277 /* Create sections to hold version informations. These are removed
278 if they are not needed. */
279 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
280 flags | SEC_READONLY);
281 if (s == NULL
282 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
283 return FALSE;
284
285 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
286 flags | SEC_READONLY);
287 if (s == NULL
288 || ! bfd_set_section_alignment (abfd, s, 1))
289 return FALSE;
290
291 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
292 flags | SEC_READONLY);
293 if (s == NULL
294 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
295 return FALSE;
296
297 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
298 flags | SEC_READONLY);
299 if (s == NULL
300 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
301 return FALSE;
302 elf_hash_table (info)->dynsym = s;
303
304 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
305 flags | SEC_READONLY);
306 if (s == NULL)
307 return FALSE;
308
309 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
310 if (s == NULL
311 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
312 return FALSE;
313
314 /* The special symbol _DYNAMIC is always set to the start of the
315 .dynamic section. We could set _DYNAMIC in a linker script, but we
316 only want to define it if we are, in fact, creating a .dynamic
317 section. We don't want to define it if there is no .dynamic
318 section, since on some ELF platforms the start up code examines it
319 to decide how to initialize the process. */
320 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
321 elf_hash_table (info)->hdynamic = h;
322 if (h == NULL)
323 return FALSE;
324
325 if (info->emit_hash)
326 {
327 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
328 flags | SEC_READONLY);
329 if (s == NULL
330 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
331 return FALSE;
332 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
333 }
334
335 if (info->emit_gnu_hash)
336 {
337 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
338 flags | SEC_READONLY);
339 if (s == NULL
340 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
341 return FALSE;
342 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
343 4 32-bit words followed by variable count of 64-bit words, then
344 variable count of 32-bit words. */
345 if (bed->s->arch_size == 64)
346 elf_section_data (s)->this_hdr.sh_entsize = 0;
347 else
348 elf_section_data (s)->this_hdr.sh_entsize = 4;
349 }
350
351 /* Let the backend create the rest of the sections. This lets the
352 backend set the right flags. The backend will normally create
353 the .got and .plt sections. */
354 if (bed->elf_backend_create_dynamic_sections == NULL
355 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
356 return FALSE;
357
358 elf_hash_table (info)->dynamic_sections_created = TRUE;
359
360 return TRUE;
361 }
362
363 /* Create dynamic sections when linking against a dynamic object. */
364
365 bfd_boolean
366 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
367 {
368 flagword flags, pltflags;
369 struct elf_link_hash_entry *h;
370 asection *s;
371 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
372 struct elf_link_hash_table *htab = elf_hash_table (info);
373
374 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
375 .rel[a].bss sections. */
376 flags = bed->dynamic_sec_flags;
377
378 pltflags = flags;
379 if (bed->plt_not_loaded)
380 /* We do not clear SEC_ALLOC here because we still want the OS to
381 allocate space for the section; it's just that there's nothing
382 to read in from the object file. */
383 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
384 else
385 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
386 if (bed->plt_readonly)
387 pltflags |= SEC_READONLY;
388
389 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
390 if (s == NULL
391 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
392 return FALSE;
393 htab->splt = s;
394
395 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
396 .plt section. */
397 if (bed->want_plt_sym)
398 {
399 h = _bfd_elf_define_linkage_sym (abfd, info, s,
400 "_PROCEDURE_LINKAGE_TABLE_");
401 elf_hash_table (info)->hplt = h;
402 if (h == NULL)
403 return FALSE;
404 }
405
406 s = bfd_make_section_anyway_with_flags (abfd,
407 (bed->rela_plts_and_copies_p
408 ? ".rela.plt" : ".rel.plt"),
409 flags | SEC_READONLY);
410 if (s == NULL
411 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
412 return FALSE;
413 htab->srelplt = s;
414
415 if (! _bfd_elf_create_got_section (abfd, info))
416 return FALSE;
417
418 if (bed->want_dynbss)
419 {
420 /* The .dynbss section is a place to put symbols which are defined
421 by dynamic objects, are referenced by regular objects, and are
422 not functions. We must allocate space for them in the process
423 image and use a R_*_COPY reloc to tell the dynamic linker to
424 initialize them at run time. The linker script puts the .dynbss
425 section into the .bss section of the final image. */
426 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
427 (SEC_ALLOC | SEC_LINKER_CREATED));
428 if (s == NULL)
429 return FALSE;
430 htab->sdynbss = s;
431
432 /* The .rel[a].bss section holds copy relocs. This section is not
433 normally needed. We need to create it here, though, so that the
434 linker will map it to an output section. We can't just create it
435 only if we need it, because we will not know whether we need it
436 until we have seen all the input files, and the first time the
437 main linker code calls BFD after examining all the input files
438 (size_dynamic_sections) the input sections have already been
439 mapped to the output sections. If the section turns out not to
440 be needed, we can discard it later. We will never need this
441 section when generating a shared object, since they do not use
442 copy relocs. */
443 if (bfd_link_executable (info))
444 {
445 s = bfd_make_section_anyway_with_flags (abfd,
446 (bed->rela_plts_and_copies_p
447 ? ".rela.bss" : ".rel.bss"),
448 flags | SEC_READONLY);
449 if (s == NULL
450 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
451 return FALSE;
452 htab->srelbss = s;
453 }
454 }
455
456 return TRUE;
457 }
458 \f
459 /* Record a new dynamic symbol. We record the dynamic symbols as we
460 read the input files, since we need to have a list of all of them
461 before we can determine the final sizes of the output sections.
462 Note that we may actually call this function even though we are not
463 going to output any dynamic symbols; in some cases we know that a
464 symbol should be in the dynamic symbol table, but only if there is
465 one. */
466
467 bfd_boolean
468 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
469 struct elf_link_hash_entry *h)
470 {
471 if (h->dynindx == -1)
472 {
473 struct elf_strtab_hash *dynstr;
474 char *p;
475 const char *name;
476 size_t indx;
477
478 /* XXX: The ABI draft says the linker must turn hidden and
479 internal symbols into STB_LOCAL symbols when producing the
480 DSO. However, if ld.so honors st_other in the dynamic table,
481 this would not be necessary. */
482 switch (ELF_ST_VISIBILITY (h->other))
483 {
484 case STV_INTERNAL:
485 case STV_HIDDEN:
486 if (h->root.type != bfd_link_hash_undefined
487 && h->root.type != bfd_link_hash_undefweak)
488 {
489 h->forced_local = 1;
490 if (!elf_hash_table (info)->is_relocatable_executable)
491 return TRUE;
492 }
493
494 default:
495 break;
496 }
497
498 h->dynindx = elf_hash_table (info)->dynsymcount;
499 ++elf_hash_table (info)->dynsymcount;
500
501 dynstr = elf_hash_table (info)->dynstr;
502 if (dynstr == NULL)
503 {
504 /* Create a strtab to hold the dynamic symbol names. */
505 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
506 if (dynstr == NULL)
507 return FALSE;
508 }
509
510 /* We don't put any version information in the dynamic string
511 table. */
512 name = h->root.root.string;
513 p = strchr (name, ELF_VER_CHR);
514 if (p != NULL)
515 /* We know that the p points into writable memory. In fact,
516 there are only a few symbols that have read-only names, being
517 those like _GLOBAL_OFFSET_TABLE_ that are created specially
518 by the backends. Most symbols will have names pointing into
519 an ELF string table read from a file, or to objalloc memory. */
520 *p = 0;
521
522 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
523
524 if (p != NULL)
525 *p = ELF_VER_CHR;
526
527 if (indx == (size_t) -1)
528 return FALSE;
529 h->dynstr_index = indx;
530 }
531
532 return TRUE;
533 }
534 \f
535 /* Mark a symbol dynamic. */
536
537 static void
538 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
539 struct elf_link_hash_entry *h,
540 Elf_Internal_Sym *sym)
541 {
542 struct bfd_elf_dynamic_list *d = info->dynamic_list;
543
544 /* It may be called more than once on the same H. */
545 if(h->dynamic || bfd_link_relocatable (info))
546 return;
547
548 if ((info->dynamic_data
549 && (h->type == STT_OBJECT
550 || h->type == STT_COMMON
551 || (sym != NULL
552 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
553 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
554 || (d != NULL
555 && h->root.type == bfd_link_hash_new
556 && (*d->match) (&d->head, NULL, h->root.root.string)))
557 h->dynamic = 1;
558 }
559
560 /* Record an assignment to a symbol made by a linker script. We need
561 this in case some dynamic object refers to this symbol. */
562
563 bfd_boolean
564 bfd_elf_record_link_assignment (bfd *output_bfd,
565 struct bfd_link_info *info,
566 const char *name,
567 bfd_boolean provide,
568 bfd_boolean hidden)
569 {
570 struct elf_link_hash_entry *h, *hv;
571 struct elf_link_hash_table *htab;
572 const struct elf_backend_data *bed;
573
574 if (!is_elf_hash_table (info->hash))
575 return TRUE;
576
577 htab = elf_hash_table (info);
578 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
579 if (h == NULL)
580 return provide;
581
582 if (h->root.type == bfd_link_hash_warning)
583 h = (struct elf_link_hash_entry *) h->root.u.i.link;
584
585 if (h->versioned == unknown)
586 {
587 /* Set versioned if symbol version is unknown. */
588 char *version = strrchr (name, ELF_VER_CHR);
589 if (version)
590 {
591 if (version > name && version[-1] != ELF_VER_CHR)
592 h->versioned = versioned_hidden;
593 else
594 h->versioned = versioned;
595 }
596 }
597
598 switch (h->root.type)
599 {
600 case bfd_link_hash_defined:
601 case bfd_link_hash_defweak:
602 case bfd_link_hash_common:
603 break;
604 case bfd_link_hash_undefweak:
605 case bfd_link_hash_undefined:
606 /* Since we're defining the symbol, don't let it seem to have not
607 been defined. record_dynamic_symbol and size_dynamic_sections
608 may depend on this. */
609 h->root.type = bfd_link_hash_new;
610 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
611 bfd_link_repair_undef_list (&htab->root);
612 break;
613 case bfd_link_hash_new:
614 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
615 h->non_elf = 0;
616 break;
617 case bfd_link_hash_indirect:
618 /* We had a versioned symbol in a dynamic library. We make the
619 the versioned symbol point to this one. */
620 bed = get_elf_backend_data (output_bfd);
621 hv = h;
622 while (hv->root.type == bfd_link_hash_indirect
623 || hv->root.type == bfd_link_hash_warning)
624 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
625 /* We don't need to update h->root.u since linker will set them
626 later. */
627 h->root.type = bfd_link_hash_undefined;
628 hv->root.type = bfd_link_hash_indirect;
629 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
630 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
631 break;
632 default:
633 BFD_FAIL ();
634 return FALSE;
635 }
636
637 /* If this symbol is being provided by the linker script, and it is
638 currently defined by a dynamic object, but not by a regular
639 object, then mark it as undefined so that the generic linker will
640 force the correct value. */
641 if (provide
642 && h->def_dynamic
643 && !h->def_regular)
644 h->root.type = bfd_link_hash_undefined;
645
646 /* If this symbol is not being provided by the linker script, and it is
647 currently defined by a dynamic object, but not by a regular object,
648 then clear out any version information because the symbol will not be
649 associated with the dynamic object any more. */
650 if (!provide
651 && h->def_dynamic
652 && !h->def_regular)
653 h->verinfo.verdef = NULL;
654
655 h->def_regular = 1;
656
657 if (hidden)
658 {
659 bed = get_elf_backend_data (output_bfd);
660 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
661 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
662 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
663 }
664
665 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
666 and executables. */
667 if (!bfd_link_relocatable (info)
668 && h->dynindx != -1
669 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
670 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
671 h->forced_local = 1;
672
673 if ((h->def_dynamic
674 || h->ref_dynamic
675 || bfd_link_dll (info)
676 || elf_hash_table (info)->is_relocatable_executable)
677 && h->dynindx == -1)
678 {
679 if (! bfd_elf_link_record_dynamic_symbol (info, h))
680 return FALSE;
681
682 /* If this is a weak defined symbol, and we know a corresponding
683 real symbol from the same dynamic object, make sure the real
684 symbol is also made into a dynamic symbol. */
685 if (h->u.weakdef != NULL
686 && h->u.weakdef->dynindx == -1)
687 {
688 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
689 return FALSE;
690 }
691 }
692
693 return TRUE;
694 }
695
696 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
697 success, and 2 on a failure caused by attempting to record a symbol
698 in a discarded section, eg. a discarded link-once section symbol. */
699
700 int
701 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
702 bfd *input_bfd,
703 long input_indx)
704 {
705 bfd_size_type amt;
706 struct elf_link_local_dynamic_entry *entry;
707 struct elf_link_hash_table *eht;
708 struct elf_strtab_hash *dynstr;
709 size_t dynstr_index;
710 char *name;
711 Elf_External_Sym_Shndx eshndx;
712 char esym[sizeof (Elf64_External_Sym)];
713
714 if (! is_elf_hash_table (info->hash))
715 return 0;
716
717 /* See if the entry exists already. */
718 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
719 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
720 return 1;
721
722 amt = sizeof (*entry);
723 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
724 if (entry == NULL)
725 return 0;
726
727 /* Go find the symbol, so that we can find it's name. */
728 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
729 1, input_indx, &entry->isym, esym, &eshndx))
730 {
731 bfd_release (input_bfd, entry);
732 return 0;
733 }
734
735 if (entry->isym.st_shndx != SHN_UNDEF
736 && entry->isym.st_shndx < SHN_LORESERVE)
737 {
738 asection *s;
739
740 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
741 if (s == NULL || bfd_is_abs_section (s->output_section))
742 {
743 /* We can still bfd_release here as nothing has done another
744 bfd_alloc. We can't do this later in this function. */
745 bfd_release (input_bfd, entry);
746 return 2;
747 }
748 }
749
750 name = (bfd_elf_string_from_elf_section
751 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
752 entry->isym.st_name));
753
754 dynstr = elf_hash_table (info)->dynstr;
755 if (dynstr == NULL)
756 {
757 /* Create a strtab to hold the dynamic symbol names. */
758 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
759 if (dynstr == NULL)
760 return 0;
761 }
762
763 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
764 if (dynstr_index == (size_t) -1)
765 return 0;
766 entry->isym.st_name = dynstr_index;
767
768 eht = elf_hash_table (info);
769
770 entry->next = eht->dynlocal;
771 eht->dynlocal = entry;
772 entry->input_bfd = input_bfd;
773 entry->input_indx = input_indx;
774 eht->dynsymcount++;
775
776 /* Whatever binding the symbol had before, it's now local. */
777 entry->isym.st_info
778 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
779
780 /* The dynindx will be set at the end of size_dynamic_sections. */
781
782 return 1;
783 }
784
785 /* Return the dynindex of a local dynamic symbol. */
786
787 long
788 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
789 bfd *input_bfd,
790 long input_indx)
791 {
792 struct elf_link_local_dynamic_entry *e;
793
794 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
795 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
796 return e->dynindx;
797 return -1;
798 }
799
800 /* This function is used to renumber the dynamic symbols, if some of
801 them are removed because they are marked as local. This is called
802 via elf_link_hash_traverse. */
803
804 static bfd_boolean
805 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
806 void *data)
807 {
808 size_t *count = (size_t *) data;
809
810 if (h->forced_local)
811 return TRUE;
812
813 if (h->dynindx != -1)
814 h->dynindx = ++(*count);
815
816 return TRUE;
817 }
818
819
820 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
821 STB_LOCAL binding. */
822
823 static bfd_boolean
824 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
825 void *data)
826 {
827 size_t *count = (size_t *) data;
828
829 if (!h->forced_local)
830 return TRUE;
831
832 if (h->dynindx != -1)
833 h->dynindx = ++(*count);
834
835 return TRUE;
836 }
837
838 /* Return true if the dynamic symbol for a given section should be
839 omitted when creating a shared library. */
840 bfd_boolean
841 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
842 struct bfd_link_info *info,
843 asection *p)
844 {
845 struct elf_link_hash_table *htab;
846 asection *ip;
847
848 switch (elf_section_data (p)->this_hdr.sh_type)
849 {
850 case SHT_PROGBITS:
851 case SHT_NOBITS:
852 /* If sh_type is yet undecided, assume it could be
853 SHT_PROGBITS/SHT_NOBITS. */
854 case SHT_NULL:
855 htab = elf_hash_table (info);
856 if (p == htab->tls_sec)
857 return FALSE;
858
859 if (htab->text_index_section != NULL)
860 return p != htab->text_index_section && p != htab->data_index_section;
861
862 return (htab->dynobj != NULL
863 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
864 && ip->output_section == p);
865
866 /* There shouldn't be section relative relocations
867 against any other section. */
868 default:
869 return TRUE;
870 }
871 }
872
873 /* Assign dynsym indices. In a shared library we generate a section
874 symbol for each output section, which come first. Next come symbols
875 which have been forced to local binding. Then all of the back-end
876 allocated local dynamic syms, followed by the rest of the global
877 symbols. */
878
879 static unsigned long
880 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
881 struct bfd_link_info *info,
882 unsigned long *section_sym_count)
883 {
884 unsigned long dynsymcount = 0;
885
886 if (bfd_link_pic (info)
887 || elf_hash_table (info)->is_relocatable_executable)
888 {
889 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
890 asection *p;
891 for (p = output_bfd->sections; p ; p = p->next)
892 if ((p->flags & SEC_EXCLUDE) == 0
893 && (p->flags & SEC_ALLOC) != 0
894 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
895 elf_section_data (p)->dynindx = ++dynsymcount;
896 else
897 elf_section_data (p)->dynindx = 0;
898 }
899 *section_sym_count = dynsymcount;
900
901 elf_link_hash_traverse (elf_hash_table (info),
902 elf_link_renumber_local_hash_table_dynsyms,
903 &dynsymcount);
904
905 if (elf_hash_table (info)->dynlocal)
906 {
907 struct elf_link_local_dynamic_entry *p;
908 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
909 p->dynindx = ++dynsymcount;
910 }
911 elf_hash_table (info)->local_dynsymcount = dynsymcount;
912
913 elf_link_hash_traverse (elf_hash_table (info),
914 elf_link_renumber_hash_table_dynsyms,
915 &dynsymcount);
916
917 /* There is an unused NULL entry at the head of the table which we
918 must account for in our count even if the table is empty since it
919 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
920 .dynamic section. */
921 dynsymcount++;
922
923 elf_hash_table (info)->dynsymcount = dynsymcount;
924 return dynsymcount;
925 }
926
927 /* Merge st_other field. */
928
929 static void
930 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
931 const Elf_Internal_Sym *isym, asection *sec,
932 bfd_boolean definition, bfd_boolean dynamic)
933 {
934 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
935
936 /* If st_other has a processor-specific meaning, specific
937 code might be needed here. */
938 if (bed->elf_backend_merge_symbol_attribute)
939 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
940 dynamic);
941
942 if (!dynamic)
943 {
944 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
945 unsigned hvis = ELF_ST_VISIBILITY (h->other);
946
947 /* Keep the most constraining visibility. Leave the remainder
948 of the st_other field to elf_backend_merge_symbol_attribute. */
949 if (symvis - 1 < hvis - 1)
950 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
951 }
952 else if (definition
953 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
954 && (sec->flags & SEC_READONLY) == 0)
955 h->protected_def = 1;
956 }
957
958 /* This function is called when we want to merge a new symbol with an
959 existing symbol. It handles the various cases which arise when we
960 find a definition in a dynamic object, or when there is already a
961 definition in a dynamic object. The new symbol is described by
962 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
963 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
964 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
965 of an old common symbol. We set OVERRIDE if the old symbol is
966 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
967 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
968 to change. By OK to change, we mean that we shouldn't warn if the
969 type or size does change. */
970
971 static bfd_boolean
972 _bfd_elf_merge_symbol (bfd *abfd,
973 struct bfd_link_info *info,
974 const char *name,
975 Elf_Internal_Sym *sym,
976 asection **psec,
977 bfd_vma *pvalue,
978 struct elf_link_hash_entry **sym_hash,
979 bfd **poldbfd,
980 bfd_boolean *pold_weak,
981 unsigned int *pold_alignment,
982 bfd_boolean *skip,
983 bfd_boolean *override,
984 bfd_boolean *type_change_ok,
985 bfd_boolean *size_change_ok,
986 bfd_boolean *matched)
987 {
988 asection *sec, *oldsec;
989 struct elf_link_hash_entry *h;
990 struct elf_link_hash_entry *hi;
991 struct elf_link_hash_entry *flip;
992 int bind;
993 bfd *oldbfd;
994 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
995 bfd_boolean newweak, oldweak, newfunc, oldfunc;
996 const struct elf_backend_data *bed;
997 char *new_version;
998
999 *skip = FALSE;
1000 *override = FALSE;
1001
1002 sec = *psec;
1003 bind = ELF_ST_BIND (sym->st_info);
1004
1005 if (! bfd_is_und_section (sec))
1006 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1007 else
1008 h = ((struct elf_link_hash_entry *)
1009 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1010 if (h == NULL)
1011 return FALSE;
1012 *sym_hash = h;
1013
1014 bed = get_elf_backend_data (abfd);
1015
1016 /* NEW_VERSION is the symbol version of the new symbol. */
1017 if (h->versioned != unversioned)
1018 {
1019 /* Symbol version is unknown or versioned. */
1020 new_version = strrchr (name, ELF_VER_CHR);
1021 if (new_version)
1022 {
1023 if (h->versioned == unknown)
1024 {
1025 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1026 h->versioned = versioned_hidden;
1027 else
1028 h->versioned = versioned;
1029 }
1030 new_version += 1;
1031 if (new_version[0] == '\0')
1032 new_version = NULL;
1033 }
1034 else
1035 h->versioned = unversioned;
1036 }
1037 else
1038 new_version = NULL;
1039
1040 /* For merging, we only care about real symbols. But we need to make
1041 sure that indirect symbol dynamic flags are updated. */
1042 hi = h;
1043 while (h->root.type == bfd_link_hash_indirect
1044 || h->root.type == bfd_link_hash_warning)
1045 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1046
1047 if (!*matched)
1048 {
1049 if (hi == h || h->root.type == bfd_link_hash_new)
1050 *matched = TRUE;
1051 else
1052 {
1053 /* OLD_HIDDEN is true if the existing symbol is only visible
1054 to the symbol with the same symbol version. NEW_HIDDEN is
1055 true if the new symbol is only visible to the symbol with
1056 the same symbol version. */
1057 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1058 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1059 if (!old_hidden && !new_hidden)
1060 /* The new symbol matches the existing symbol if both
1061 aren't hidden. */
1062 *matched = TRUE;
1063 else
1064 {
1065 /* OLD_VERSION is the symbol version of the existing
1066 symbol. */
1067 char *old_version;
1068
1069 if (h->versioned >= versioned)
1070 old_version = strrchr (h->root.root.string,
1071 ELF_VER_CHR) + 1;
1072 else
1073 old_version = NULL;
1074
1075 /* The new symbol matches the existing symbol if they
1076 have the same symbol version. */
1077 *matched = (old_version == new_version
1078 || (old_version != NULL
1079 && new_version != NULL
1080 && strcmp (old_version, new_version) == 0));
1081 }
1082 }
1083 }
1084
1085 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1086 existing symbol. */
1087
1088 oldbfd = NULL;
1089 oldsec = NULL;
1090 switch (h->root.type)
1091 {
1092 default:
1093 break;
1094
1095 case bfd_link_hash_undefined:
1096 case bfd_link_hash_undefweak:
1097 oldbfd = h->root.u.undef.abfd;
1098 break;
1099
1100 case bfd_link_hash_defined:
1101 case bfd_link_hash_defweak:
1102 oldbfd = h->root.u.def.section->owner;
1103 oldsec = h->root.u.def.section;
1104 break;
1105
1106 case bfd_link_hash_common:
1107 oldbfd = h->root.u.c.p->section->owner;
1108 oldsec = h->root.u.c.p->section;
1109 if (pold_alignment)
1110 *pold_alignment = h->root.u.c.p->alignment_power;
1111 break;
1112 }
1113 if (poldbfd && *poldbfd == NULL)
1114 *poldbfd = oldbfd;
1115
1116 /* Differentiate strong and weak symbols. */
1117 newweak = bind == STB_WEAK;
1118 oldweak = (h->root.type == bfd_link_hash_defweak
1119 || h->root.type == bfd_link_hash_undefweak);
1120 if (pold_weak)
1121 *pold_weak = oldweak;
1122
1123 /* This code is for coping with dynamic objects, and is only useful
1124 if we are doing an ELF link. */
1125 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1126 return TRUE;
1127
1128 /* We have to check it for every instance since the first few may be
1129 references and not all compilers emit symbol type for undefined
1130 symbols. */
1131 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1132
1133 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1134 respectively, is from a dynamic object. */
1135
1136 newdyn = (abfd->flags & DYNAMIC) != 0;
1137
1138 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1139 syms and defined syms in dynamic libraries respectively.
1140 ref_dynamic on the other hand can be set for a symbol defined in
1141 a dynamic library, and def_dynamic may not be set; When the
1142 definition in a dynamic lib is overridden by a definition in the
1143 executable use of the symbol in the dynamic lib becomes a
1144 reference to the executable symbol. */
1145 if (newdyn)
1146 {
1147 if (bfd_is_und_section (sec))
1148 {
1149 if (bind != STB_WEAK)
1150 {
1151 h->ref_dynamic_nonweak = 1;
1152 hi->ref_dynamic_nonweak = 1;
1153 }
1154 }
1155 else
1156 {
1157 /* Update the existing symbol only if they match. */
1158 if (*matched)
1159 h->dynamic_def = 1;
1160 hi->dynamic_def = 1;
1161 }
1162 }
1163
1164 /* If we just created the symbol, mark it as being an ELF symbol.
1165 Other than that, there is nothing to do--there is no merge issue
1166 with a newly defined symbol--so we just return. */
1167
1168 if (h->root.type == bfd_link_hash_new)
1169 {
1170 h->non_elf = 0;
1171 return TRUE;
1172 }
1173
1174 /* In cases involving weak versioned symbols, we may wind up trying
1175 to merge a symbol with itself. Catch that here, to avoid the
1176 confusion that results if we try to override a symbol with
1177 itself. The additional tests catch cases like
1178 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1179 dynamic object, which we do want to handle here. */
1180 if (abfd == oldbfd
1181 && (newweak || oldweak)
1182 && ((abfd->flags & DYNAMIC) == 0
1183 || !h->def_regular))
1184 return TRUE;
1185
1186 olddyn = FALSE;
1187 if (oldbfd != NULL)
1188 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1189 else if (oldsec != NULL)
1190 {
1191 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1192 indices used by MIPS ELF. */
1193 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1194 }
1195
1196 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1197 respectively, appear to be a definition rather than reference. */
1198
1199 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1200
1201 olddef = (h->root.type != bfd_link_hash_undefined
1202 && h->root.type != bfd_link_hash_undefweak
1203 && h->root.type != bfd_link_hash_common);
1204
1205 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1206 respectively, appear to be a function. */
1207
1208 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1209 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1210
1211 oldfunc = (h->type != STT_NOTYPE
1212 && bed->is_function_type (h->type));
1213
1214 /* If creating a default indirect symbol ("foo" or "foo@") from a
1215 dynamic versioned definition ("foo@@") skip doing so if there is
1216 an existing regular definition with a different type. We don't
1217 want, for example, a "time" variable in the executable overriding
1218 a "time" function in a shared library. */
1219 if (pold_alignment == NULL
1220 && newdyn
1221 && newdef
1222 && !olddyn
1223 && (olddef || h->root.type == bfd_link_hash_common)
1224 && ELF_ST_TYPE (sym->st_info) != h->type
1225 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1226 && h->type != STT_NOTYPE
1227 && !(newfunc && oldfunc))
1228 {
1229 *skip = TRUE;
1230 return TRUE;
1231 }
1232
1233 /* Check TLS symbols. We don't check undefined symbols introduced
1234 by "ld -u" which have no type (and oldbfd NULL), and we don't
1235 check symbols from plugins because they also have no type. */
1236 if (oldbfd != NULL
1237 && (oldbfd->flags & BFD_PLUGIN) == 0
1238 && (abfd->flags & BFD_PLUGIN) == 0
1239 && ELF_ST_TYPE (sym->st_info) != h->type
1240 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1241 {
1242 bfd *ntbfd, *tbfd;
1243 bfd_boolean ntdef, tdef;
1244 asection *ntsec, *tsec;
1245
1246 if (h->type == STT_TLS)
1247 {
1248 ntbfd = abfd;
1249 ntsec = sec;
1250 ntdef = newdef;
1251 tbfd = oldbfd;
1252 tsec = oldsec;
1253 tdef = olddef;
1254 }
1255 else
1256 {
1257 ntbfd = oldbfd;
1258 ntsec = oldsec;
1259 ntdef = olddef;
1260 tbfd = abfd;
1261 tsec = sec;
1262 tdef = newdef;
1263 }
1264
1265 if (tdef && ntdef)
1266 _bfd_error_handler
1267 /* xgettext:c-format */
1268 (_("%s: TLS definition in %B section %A "
1269 "mismatches non-TLS definition in %B section %A"),
1270 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1271 else if (!tdef && !ntdef)
1272 _bfd_error_handler
1273 /* xgettext:c-format */
1274 (_("%s: TLS reference in %B "
1275 "mismatches non-TLS reference in %B"),
1276 tbfd, ntbfd, h->root.root.string);
1277 else if (tdef)
1278 _bfd_error_handler
1279 /* xgettext:c-format */
1280 (_("%s: TLS definition in %B section %A "
1281 "mismatches non-TLS reference in %B"),
1282 tbfd, tsec, ntbfd, h->root.root.string);
1283 else
1284 _bfd_error_handler
1285 /* xgettext:c-format */
1286 (_("%s: TLS reference in %B "
1287 "mismatches non-TLS definition in %B section %A"),
1288 tbfd, ntbfd, ntsec, h->root.root.string);
1289
1290 bfd_set_error (bfd_error_bad_value);
1291 return FALSE;
1292 }
1293
1294 /* If the old symbol has non-default visibility, we ignore the new
1295 definition from a dynamic object. */
1296 if (newdyn
1297 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1298 && !bfd_is_und_section (sec))
1299 {
1300 *skip = TRUE;
1301 /* Make sure this symbol is dynamic. */
1302 h->ref_dynamic = 1;
1303 hi->ref_dynamic = 1;
1304 /* A protected symbol has external availability. Make sure it is
1305 recorded as dynamic.
1306
1307 FIXME: Should we check type and size for protected symbol? */
1308 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1309 return bfd_elf_link_record_dynamic_symbol (info, h);
1310 else
1311 return TRUE;
1312 }
1313 else if (!newdyn
1314 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1315 && h->def_dynamic)
1316 {
1317 /* If the new symbol with non-default visibility comes from a
1318 relocatable file and the old definition comes from a dynamic
1319 object, we remove the old definition. */
1320 if (hi->root.type == bfd_link_hash_indirect)
1321 {
1322 /* Handle the case where the old dynamic definition is
1323 default versioned. We need to copy the symbol info from
1324 the symbol with default version to the normal one if it
1325 was referenced before. */
1326 if (h->ref_regular)
1327 {
1328 hi->root.type = h->root.type;
1329 h->root.type = bfd_link_hash_indirect;
1330 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1331
1332 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1333 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1334 {
1335 /* If the new symbol is hidden or internal, completely undo
1336 any dynamic link state. */
1337 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1338 h->forced_local = 0;
1339 h->ref_dynamic = 0;
1340 }
1341 else
1342 h->ref_dynamic = 1;
1343
1344 h->def_dynamic = 0;
1345 /* FIXME: Should we check type and size for protected symbol? */
1346 h->size = 0;
1347 h->type = 0;
1348
1349 h = hi;
1350 }
1351 else
1352 h = hi;
1353 }
1354
1355 /* If the old symbol was undefined before, then it will still be
1356 on the undefs list. If the new symbol is undefined or
1357 common, we can't make it bfd_link_hash_new here, because new
1358 undefined or common symbols will be added to the undefs list
1359 by _bfd_generic_link_add_one_symbol. Symbols may not be
1360 added twice to the undefs list. Also, if the new symbol is
1361 undefweak then we don't want to lose the strong undef. */
1362 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1363 {
1364 h->root.type = bfd_link_hash_undefined;
1365 h->root.u.undef.abfd = abfd;
1366 }
1367 else
1368 {
1369 h->root.type = bfd_link_hash_new;
1370 h->root.u.undef.abfd = NULL;
1371 }
1372
1373 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1374 {
1375 /* If the new symbol is hidden or internal, completely undo
1376 any dynamic link state. */
1377 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1378 h->forced_local = 0;
1379 h->ref_dynamic = 0;
1380 }
1381 else
1382 h->ref_dynamic = 1;
1383 h->def_dynamic = 0;
1384 /* FIXME: Should we check type and size for protected symbol? */
1385 h->size = 0;
1386 h->type = 0;
1387 return TRUE;
1388 }
1389
1390 /* If a new weak symbol definition comes from a regular file and the
1391 old symbol comes from a dynamic library, we treat the new one as
1392 strong. Similarly, an old weak symbol definition from a regular
1393 file is treated as strong when the new symbol comes from a dynamic
1394 library. Further, an old weak symbol from a dynamic library is
1395 treated as strong if the new symbol is from a dynamic library.
1396 This reflects the way glibc's ld.so works.
1397
1398 Do this before setting *type_change_ok or *size_change_ok so that
1399 we warn properly when dynamic library symbols are overridden. */
1400
1401 if (newdef && !newdyn && olddyn)
1402 newweak = FALSE;
1403 if (olddef && newdyn)
1404 oldweak = FALSE;
1405
1406 /* Allow changes between different types of function symbol. */
1407 if (newfunc && oldfunc)
1408 *type_change_ok = TRUE;
1409
1410 /* It's OK to change the type if either the existing symbol or the
1411 new symbol is weak. A type change is also OK if the old symbol
1412 is undefined and the new symbol is defined. */
1413
1414 if (oldweak
1415 || newweak
1416 || (newdef
1417 && h->root.type == bfd_link_hash_undefined))
1418 *type_change_ok = TRUE;
1419
1420 /* It's OK to change the size if either the existing symbol or the
1421 new symbol is weak, or if the old symbol is undefined. */
1422
1423 if (*type_change_ok
1424 || h->root.type == bfd_link_hash_undefined)
1425 *size_change_ok = TRUE;
1426
1427 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1428 symbol, respectively, appears to be a common symbol in a dynamic
1429 object. If a symbol appears in an uninitialized section, and is
1430 not weak, and is not a function, then it may be a common symbol
1431 which was resolved when the dynamic object was created. We want
1432 to treat such symbols specially, because they raise special
1433 considerations when setting the symbol size: if the symbol
1434 appears as a common symbol in a regular object, and the size in
1435 the regular object is larger, we must make sure that we use the
1436 larger size. This problematic case can always be avoided in C,
1437 but it must be handled correctly when using Fortran shared
1438 libraries.
1439
1440 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1441 likewise for OLDDYNCOMMON and OLDDEF.
1442
1443 Note that this test is just a heuristic, and that it is quite
1444 possible to have an uninitialized symbol in a shared object which
1445 is really a definition, rather than a common symbol. This could
1446 lead to some minor confusion when the symbol really is a common
1447 symbol in some regular object. However, I think it will be
1448 harmless. */
1449
1450 if (newdyn
1451 && newdef
1452 && !newweak
1453 && (sec->flags & SEC_ALLOC) != 0
1454 && (sec->flags & SEC_LOAD) == 0
1455 && sym->st_size > 0
1456 && !newfunc)
1457 newdyncommon = TRUE;
1458 else
1459 newdyncommon = FALSE;
1460
1461 if (olddyn
1462 && olddef
1463 && h->root.type == bfd_link_hash_defined
1464 && h->def_dynamic
1465 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1466 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1467 && h->size > 0
1468 && !oldfunc)
1469 olddyncommon = TRUE;
1470 else
1471 olddyncommon = FALSE;
1472
1473 /* We now know everything about the old and new symbols. We ask the
1474 backend to check if we can merge them. */
1475 if (bed->merge_symbol != NULL)
1476 {
1477 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1478 return FALSE;
1479 sec = *psec;
1480 }
1481
1482 /* If both the old and the new symbols look like common symbols in a
1483 dynamic object, set the size of the symbol to the larger of the
1484 two. */
1485
1486 if (olddyncommon
1487 && newdyncommon
1488 && sym->st_size != h->size)
1489 {
1490 /* Since we think we have two common symbols, issue a multiple
1491 common warning if desired. Note that we only warn if the
1492 size is different. If the size is the same, we simply let
1493 the old symbol override the new one as normally happens with
1494 symbols defined in dynamic objects. */
1495
1496 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1497 bfd_link_hash_common, sym->st_size);
1498 if (sym->st_size > h->size)
1499 h->size = sym->st_size;
1500
1501 *size_change_ok = TRUE;
1502 }
1503
1504 /* If we are looking at a dynamic object, and we have found a
1505 definition, we need to see if the symbol was already defined by
1506 some other object. If so, we want to use the existing
1507 definition, and we do not want to report a multiple symbol
1508 definition error; we do this by clobbering *PSEC to be
1509 bfd_und_section_ptr.
1510
1511 We treat a common symbol as a definition if the symbol in the
1512 shared library is a function, since common symbols always
1513 represent variables; this can cause confusion in principle, but
1514 any such confusion would seem to indicate an erroneous program or
1515 shared library. We also permit a common symbol in a regular
1516 object to override a weak symbol in a shared object. A common
1517 symbol in executable also overrides a symbol in a shared object. */
1518
1519 if (newdyn
1520 && newdef
1521 && (olddef
1522 || (h->root.type == bfd_link_hash_common
1523 && (newweak
1524 || newfunc
1525 || (!olddyn && bfd_link_executable (info))))))
1526 {
1527 *override = TRUE;
1528 newdef = FALSE;
1529 newdyncommon = FALSE;
1530
1531 *psec = sec = bfd_und_section_ptr;
1532 *size_change_ok = TRUE;
1533
1534 /* If we get here when the old symbol is a common symbol, then
1535 we are explicitly letting it override a weak symbol or
1536 function in a dynamic object, and we don't want to warn about
1537 a type change. If the old symbol is a defined symbol, a type
1538 change warning may still be appropriate. */
1539
1540 if (h->root.type == bfd_link_hash_common)
1541 *type_change_ok = TRUE;
1542 }
1543
1544 /* Handle the special case of an old common symbol merging with a
1545 new symbol which looks like a common symbol in a shared object.
1546 We change *PSEC and *PVALUE to make the new symbol look like a
1547 common symbol, and let _bfd_generic_link_add_one_symbol do the
1548 right thing. */
1549
1550 if (newdyncommon
1551 && h->root.type == bfd_link_hash_common)
1552 {
1553 *override = TRUE;
1554 newdef = FALSE;
1555 newdyncommon = FALSE;
1556 *pvalue = sym->st_size;
1557 *psec = sec = bed->common_section (oldsec);
1558 *size_change_ok = TRUE;
1559 }
1560
1561 /* Skip weak definitions of symbols that are already defined. */
1562 if (newdef && olddef && newweak)
1563 {
1564 /* Don't skip new non-IR weak syms. */
1565 if (!(oldbfd != NULL
1566 && (oldbfd->flags & BFD_PLUGIN) != 0
1567 && (abfd->flags & BFD_PLUGIN) == 0))
1568 {
1569 newdef = FALSE;
1570 *skip = TRUE;
1571 }
1572
1573 /* Merge st_other. If the symbol already has a dynamic index,
1574 but visibility says it should not be visible, turn it into a
1575 local symbol. */
1576 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1577 if (h->dynindx != -1)
1578 switch (ELF_ST_VISIBILITY (h->other))
1579 {
1580 case STV_INTERNAL:
1581 case STV_HIDDEN:
1582 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1583 break;
1584 }
1585 }
1586
1587 /* If the old symbol is from a dynamic object, and the new symbol is
1588 a definition which is not from a dynamic object, then the new
1589 symbol overrides the old symbol. Symbols from regular files
1590 always take precedence over symbols from dynamic objects, even if
1591 they are defined after the dynamic object in the link.
1592
1593 As above, we again permit a common symbol in a regular object to
1594 override a definition in a shared object if the shared object
1595 symbol is a function or is weak. */
1596
1597 flip = NULL;
1598 if (!newdyn
1599 && (newdef
1600 || (bfd_is_com_section (sec)
1601 && (oldweak || oldfunc)))
1602 && olddyn
1603 && olddef
1604 && h->def_dynamic)
1605 {
1606 /* Change the hash table entry to undefined, and let
1607 _bfd_generic_link_add_one_symbol do the right thing with the
1608 new definition. */
1609
1610 h->root.type = bfd_link_hash_undefined;
1611 h->root.u.undef.abfd = h->root.u.def.section->owner;
1612 *size_change_ok = TRUE;
1613
1614 olddef = FALSE;
1615 olddyncommon = FALSE;
1616
1617 /* We again permit a type change when a common symbol may be
1618 overriding a function. */
1619
1620 if (bfd_is_com_section (sec))
1621 {
1622 if (oldfunc)
1623 {
1624 /* If a common symbol overrides a function, make sure
1625 that it isn't defined dynamically nor has type
1626 function. */
1627 h->def_dynamic = 0;
1628 h->type = STT_NOTYPE;
1629 }
1630 *type_change_ok = TRUE;
1631 }
1632
1633 if (hi->root.type == bfd_link_hash_indirect)
1634 flip = hi;
1635 else
1636 /* This union may have been set to be non-NULL when this symbol
1637 was seen in a dynamic object. We must force the union to be
1638 NULL, so that it is correct for a regular symbol. */
1639 h->verinfo.vertree = NULL;
1640 }
1641
1642 /* Handle the special case of a new common symbol merging with an
1643 old symbol that looks like it might be a common symbol defined in
1644 a shared object. Note that we have already handled the case in
1645 which a new common symbol should simply override the definition
1646 in the shared library. */
1647
1648 if (! newdyn
1649 && bfd_is_com_section (sec)
1650 && olddyncommon)
1651 {
1652 /* It would be best if we could set the hash table entry to a
1653 common symbol, but we don't know what to use for the section
1654 or the alignment. */
1655 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1656 bfd_link_hash_common, sym->st_size);
1657
1658 /* If the presumed common symbol in the dynamic object is
1659 larger, pretend that the new symbol has its size. */
1660
1661 if (h->size > *pvalue)
1662 *pvalue = h->size;
1663
1664 /* We need to remember the alignment required by the symbol
1665 in the dynamic object. */
1666 BFD_ASSERT (pold_alignment);
1667 *pold_alignment = h->root.u.def.section->alignment_power;
1668
1669 olddef = FALSE;
1670 olddyncommon = FALSE;
1671
1672 h->root.type = bfd_link_hash_undefined;
1673 h->root.u.undef.abfd = h->root.u.def.section->owner;
1674
1675 *size_change_ok = TRUE;
1676 *type_change_ok = TRUE;
1677
1678 if (hi->root.type == bfd_link_hash_indirect)
1679 flip = hi;
1680 else
1681 h->verinfo.vertree = NULL;
1682 }
1683
1684 if (flip != NULL)
1685 {
1686 /* Handle the case where we had a versioned symbol in a dynamic
1687 library and now find a definition in a normal object. In this
1688 case, we make the versioned symbol point to the normal one. */
1689 flip->root.type = h->root.type;
1690 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1691 h->root.type = bfd_link_hash_indirect;
1692 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1693 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1694 if (h->def_dynamic)
1695 {
1696 h->def_dynamic = 0;
1697 flip->ref_dynamic = 1;
1698 }
1699 }
1700
1701 return TRUE;
1702 }
1703
1704 /* This function is called to create an indirect symbol from the
1705 default for the symbol with the default version if needed. The
1706 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1707 set DYNSYM if the new indirect symbol is dynamic. */
1708
1709 static bfd_boolean
1710 _bfd_elf_add_default_symbol (bfd *abfd,
1711 struct bfd_link_info *info,
1712 struct elf_link_hash_entry *h,
1713 const char *name,
1714 Elf_Internal_Sym *sym,
1715 asection *sec,
1716 bfd_vma value,
1717 bfd **poldbfd,
1718 bfd_boolean *dynsym)
1719 {
1720 bfd_boolean type_change_ok;
1721 bfd_boolean size_change_ok;
1722 bfd_boolean skip;
1723 char *shortname;
1724 struct elf_link_hash_entry *hi;
1725 struct bfd_link_hash_entry *bh;
1726 const struct elf_backend_data *bed;
1727 bfd_boolean collect;
1728 bfd_boolean dynamic;
1729 bfd_boolean override;
1730 char *p;
1731 size_t len, shortlen;
1732 asection *tmp_sec;
1733 bfd_boolean matched;
1734
1735 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1736 return TRUE;
1737
1738 /* If this symbol has a version, and it is the default version, we
1739 create an indirect symbol from the default name to the fully
1740 decorated name. This will cause external references which do not
1741 specify a version to be bound to this version of the symbol. */
1742 p = strchr (name, ELF_VER_CHR);
1743 if (h->versioned == unknown)
1744 {
1745 if (p == NULL)
1746 {
1747 h->versioned = unversioned;
1748 return TRUE;
1749 }
1750 else
1751 {
1752 if (p[1] != ELF_VER_CHR)
1753 {
1754 h->versioned = versioned_hidden;
1755 return TRUE;
1756 }
1757 else
1758 h->versioned = versioned;
1759 }
1760 }
1761 else
1762 {
1763 /* PR ld/19073: We may see an unversioned definition after the
1764 default version. */
1765 if (p == NULL)
1766 return TRUE;
1767 }
1768
1769 bed = get_elf_backend_data (abfd);
1770 collect = bed->collect;
1771 dynamic = (abfd->flags & DYNAMIC) != 0;
1772
1773 shortlen = p - name;
1774 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1775 if (shortname == NULL)
1776 return FALSE;
1777 memcpy (shortname, name, shortlen);
1778 shortname[shortlen] = '\0';
1779
1780 /* We are going to create a new symbol. Merge it with any existing
1781 symbol with this name. For the purposes of the merge, act as
1782 though we were defining the symbol we just defined, although we
1783 actually going to define an indirect symbol. */
1784 type_change_ok = FALSE;
1785 size_change_ok = FALSE;
1786 matched = TRUE;
1787 tmp_sec = sec;
1788 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1789 &hi, poldbfd, NULL, NULL, &skip, &override,
1790 &type_change_ok, &size_change_ok, &matched))
1791 return FALSE;
1792
1793 if (skip)
1794 goto nondefault;
1795
1796 if (hi->def_regular)
1797 {
1798 /* If the undecorated symbol will have a version added by a
1799 script different to H, then don't indirect to/from the
1800 undecorated symbol. This isn't ideal because we may not yet
1801 have seen symbol versions, if given by a script on the
1802 command line rather than via --version-script. */
1803 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1804 {
1805 bfd_boolean hide;
1806
1807 hi->verinfo.vertree
1808 = bfd_find_version_for_sym (info->version_info,
1809 hi->root.root.string, &hide);
1810 if (hi->verinfo.vertree != NULL && hide)
1811 {
1812 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1813 goto nondefault;
1814 }
1815 }
1816 if (hi->verinfo.vertree != NULL
1817 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1818 goto nondefault;
1819 }
1820
1821 if (! override)
1822 {
1823 /* Add the default symbol if not performing a relocatable link. */
1824 if (! bfd_link_relocatable (info))
1825 {
1826 bh = &hi->root;
1827 if (! (_bfd_generic_link_add_one_symbol
1828 (info, abfd, shortname, BSF_INDIRECT,
1829 bfd_ind_section_ptr,
1830 0, name, FALSE, collect, &bh)))
1831 return FALSE;
1832 hi = (struct elf_link_hash_entry *) bh;
1833 }
1834 }
1835 else
1836 {
1837 /* In this case the symbol named SHORTNAME is overriding the
1838 indirect symbol we want to add. We were planning on making
1839 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1840 is the name without a version. NAME is the fully versioned
1841 name, and it is the default version.
1842
1843 Overriding means that we already saw a definition for the
1844 symbol SHORTNAME in a regular object, and it is overriding
1845 the symbol defined in the dynamic object.
1846
1847 When this happens, we actually want to change NAME, the
1848 symbol we just added, to refer to SHORTNAME. This will cause
1849 references to NAME in the shared object to become references
1850 to SHORTNAME in the regular object. This is what we expect
1851 when we override a function in a shared object: that the
1852 references in the shared object will be mapped to the
1853 definition in the regular object. */
1854
1855 while (hi->root.type == bfd_link_hash_indirect
1856 || hi->root.type == bfd_link_hash_warning)
1857 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1858
1859 h->root.type = bfd_link_hash_indirect;
1860 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1861 if (h->def_dynamic)
1862 {
1863 h->def_dynamic = 0;
1864 hi->ref_dynamic = 1;
1865 if (hi->ref_regular
1866 || hi->def_regular)
1867 {
1868 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1869 return FALSE;
1870 }
1871 }
1872
1873 /* Now set HI to H, so that the following code will set the
1874 other fields correctly. */
1875 hi = h;
1876 }
1877
1878 /* Check if HI is a warning symbol. */
1879 if (hi->root.type == bfd_link_hash_warning)
1880 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1881
1882 /* If there is a duplicate definition somewhere, then HI may not
1883 point to an indirect symbol. We will have reported an error to
1884 the user in that case. */
1885
1886 if (hi->root.type == bfd_link_hash_indirect)
1887 {
1888 struct elf_link_hash_entry *ht;
1889
1890 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1891 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1892
1893 /* A reference to the SHORTNAME symbol from a dynamic library
1894 will be satisfied by the versioned symbol at runtime. In
1895 effect, we have a reference to the versioned symbol. */
1896 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1897 hi->dynamic_def |= ht->dynamic_def;
1898
1899 /* See if the new flags lead us to realize that the symbol must
1900 be dynamic. */
1901 if (! *dynsym)
1902 {
1903 if (! dynamic)
1904 {
1905 if (! bfd_link_executable (info)
1906 || hi->def_dynamic
1907 || hi->ref_dynamic)
1908 *dynsym = TRUE;
1909 }
1910 else
1911 {
1912 if (hi->ref_regular)
1913 *dynsym = TRUE;
1914 }
1915 }
1916 }
1917
1918 /* We also need to define an indirection from the nondefault version
1919 of the symbol. */
1920
1921 nondefault:
1922 len = strlen (name);
1923 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
1924 if (shortname == NULL)
1925 return FALSE;
1926 memcpy (shortname, name, shortlen);
1927 memcpy (shortname + shortlen, p + 1, len - shortlen);
1928
1929 /* Once again, merge with any existing symbol. */
1930 type_change_ok = FALSE;
1931 size_change_ok = FALSE;
1932 tmp_sec = sec;
1933 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1934 &hi, poldbfd, NULL, NULL, &skip, &override,
1935 &type_change_ok, &size_change_ok, &matched))
1936 return FALSE;
1937
1938 if (skip)
1939 return TRUE;
1940
1941 if (override)
1942 {
1943 /* Here SHORTNAME is a versioned name, so we don't expect to see
1944 the type of override we do in the case above unless it is
1945 overridden by a versioned definition. */
1946 if (hi->root.type != bfd_link_hash_defined
1947 && hi->root.type != bfd_link_hash_defweak)
1948 _bfd_error_handler
1949 /* xgettext:c-format */
1950 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1951 abfd, shortname);
1952 }
1953 else
1954 {
1955 bh = &hi->root;
1956 if (! (_bfd_generic_link_add_one_symbol
1957 (info, abfd, shortname, BSF_INDIRECT,
1958 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1959 return FALSE;
1960 hi = (struct elf_link_hash_entry *) bh;
1961
1962 /* If there is a duplicate definition somewhere, then HI may not
1963 point to an indirect symbol. We will have reported an error
1964 to the user in that case. */
1965
1966 if (hi->root.type == bfd_link_hash_indirect)
1967 {
1968 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1969 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1970 hi->dynamic_def |= h->dynamic_def;
1971
1972 /* See if the new flags lead us to realize that the symbol
1973 must be dynamic. */
1974 if (! *dynsym)
1975 {
1976 if (! dynamic)
1977 {
1978 if (! bfd_link_executable (info)
1979 || hi->ref_dynamic)
1980 *dynsym = TRUE;
1981 }
1982 else
1983 {
1984 if (hi->ref_regular)
1985 *dynsym = TRUE;
1986 }
1987 }
1988 }
1989 }
1990
1991 return TRUE;
1992 }
1993 \f
1994 /* This routine is used to export all defined symbols into the dynamic
1995 symbol table. It is called via elf_link_hash_traverse. */
1996
1997 static bfd_boolean
1998 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1999 {
2000 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2001
2002 /* Ignore indirect symbols. These are added by the versioning code. */
2003 if (h->root.type == bfd_link_hash_indirect)
2004 return TRUE;
2005
2006 /* Ignore this if we won't export it. */
2007 if (!eif->info->export_dynamic && !h->dynamic)
2008 return TRUE;
2009
2010 if (h->dynindx == -1
2011 && (h->def_regular || h->ref_regular)
2012 && ! bfd_hide_sym_by_version (eif->info->version_info,
2013 h->root.root.string))
2014 {
2015 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2016 {
2017 eif->failed = TRUE;
2018 return FALSE;
2019 }
2020 }
2021
2022 return TRUE;
2023 }
2024 \f
2025 /* Look through the symbols which are defined in other shared
2026 libraries and referenced here. Update the list of version
2027 dependencies. This will be put into the .gnu.version_r section.
2028 This function is called via elf_link_hash_traverse. */
2029
2030 static bfd_boolean
2031 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2032 void *data)
2033 {
2034 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2035 Elf_Internal_Verneed *t;
2036 Elf_Internal_Vernaux *a;
2037 bfd_size_type amt;
2038
2039 /* We only care about symbols defined in shared objects with version
2040 information. */
2041 if (!h->def_dynamic
2042 || h->def_regular
2043 || h->dynindx == -1
2044 || h->verinfo.verdef == NULL
2045 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2046 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2047 return TRUE;
2048
2049 /* See if we already know about this version. */
2050 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2051 t != NULL;
2052 t = t->vn_nextref)
2053 {
2054 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2055 continue;
2056
2057 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2058 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2059 return TRUE;
2060
2061 break;
2062 }
2063
2064 /* This is a new version. Add it to tree we are building. */
2065
2066 if (t == NULL)
2067 {
2068 amt = sizeof *t;
2069 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2070 if (t == NULL)
2071 {
2072 rinfo->failed = TRUE;
2073 return FALSE;
2074 }
2075
2076 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2077 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2078 elf_tdata (rinfo->info->output_bfd)->verref = t;
2079 }
2080
2081 amt = sizeof *a;
2082 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2083 if (a == NULL)
2084 {
2085 rinfo->failed = TRUE;
2086 return FALSE;
2087 }
2088
2089 /* Note that we are copying a string pointer here, and testing it
2090 above. If bfd_elf_string_from_elf_section is ever changed to
2091 discard the string data when low in memory, this will have to be
2092 fixed. */
2093 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2094
2095 a->vna_flags = h->verinfo.verdef->vd_flags;
2096 a->vna_nextptr = t->vn_auxptr;
2097
2098 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2099 ++rinfo->vers;
2100
2101 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2102
2103 t->vn_auxptr = a;
2104
2105 return TRUE;
2106 }
2107
2108 /* Figure out appropriate versions for all the symbols. We may not
2109 have the version number script until we have read all of the input
2110 files, so until that point we don't know which symbols should be
2111 local. This function is called via elf_link_hash_traverse. */
2112
2113 static bfd_boolean
2114 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2115 {
2116 struct elf_info_failed *sinfo;
2117 struct bfd_link_info *info;
2118 const struct elf_backend_data *bed;
2119 struct elf_info_failed eif;
2120 char *p;
2121
2122 sinfo = (struct elf_info_failed *) data;
2123 info = sinfo->info;
2124
2125 /* Fix the symbol flags. */
2126 eif.failed = FALSE;
2127 eif.info = info;
2128 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2129 {
2130 if (eif.failed)
2131 sinfo->failed = TRUE;
2132 return FALSE;
2133 }
2134
2135 /* We only need version numbers for symbols defined in regular
2136 objects. */
2137 if (!h->def_regular)
2138 return TRUE;
2139
2140 bed = get_elf_backend_data (info->output_bfd);
2141 p = strchr (h->root.root.string, ELF_VER_CHR);
2142 if (p != NULL && h->verinfo.vertree == NULL)
2143 {
2144 struct bfd_elf_version_tree *t;
2145
2146 ++p;
2147 if (*p == ELF_VER_CHR)
2148 ++p;
2149
2150 /* If there is no version string, we can just return out. */
2151 if (*p == '\0')
2152 return TRUE;
2153
2154 /* Look for the version. If we find it, it is no longer weak. */
2155 for (t = sinfo->info->version_info; t != NULL; t = t->next)
2156 {
2157 if (strcmp (t->name, p) == 0)
2158 {
2159 size_t len;
2160 char *alc;
2161 struct bfd_elf_version_expr *d;
2162
2163 len = p - h->root.root.string;
2164 alc = (char *) bfd_malloc (len);
2165 if (alc == NULL)
2166 {
2167 sinfo->failed = TRUE;
2168 return FALSE;
2169 }
2170 memcpy (alc, h->root.root.string, len - 1);
2171 alc[len - 1] = '\0';
2172 if (alc[len - 2] == ELF_VER_CHR)
2173 alc[len - 2] = '\0';
2174
2175 h->verinfo.vertree = t;
2176 t->used = TRUE;
2177 d = NULL;
2178
2179 if (t->globals.list != NULL)
2180 d = (*t->match) (&t->globals, NULL, alc);
2181
2182 /* See if there is anything to force this symbol to
2183 local scope. */
2184 if (d == NULL && t->locals.list != NULL)
2185 {
2186 d = (*t->match) (&t->locals, NULL, alc);
2187 if (d != NULL
2188 && h->dynindx != -1
2189 && ! info->export_dynamic)
2190 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2191 }
2192
2193 free (alc);
2194 break;
2195 }
2196 }
2197
2198 /* If we are building an application, we need to create a
2199 version node for this version. */
2200 if (t == NULL && bfd_link_executable (info))
2201 {
2202 struct bfd_elf_version_tree **pp;
2203 int version_index;
2204
2205 /* If we aren't going to export this symbol, we don't need
2206 to worry about it. */
2207 if (h->dynindx == -1)
2208 return TRUE;
2209
2210 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2211 sizeof *t);
2212 if (t == NULL)
2213 {
2214 sinfo->failed = TRUE;
2215 return FALSE;
2216 }
2217
2218 t->name = p;
2219 t->name_indx = (unsigned int) -1;
2220 t->used = TRUE;
2221
2222 version_index = 1;
2223 /* Don't count anonymous version tag. */
2224 if (sinfo->info->version_info != NULL
2225 && sinfo->info->version_info->vernum == 0)
2226 version_index = 0;
2227 for (pp = &sinfo->info->version_info;
2228 *pp != NULL;
2229 pp = &(*pp)->next)
2230 ++version_index;
2231 t->vernum = version_index;
2232
2233 *pp = t;
2234
2235 h->verinfo.vertree = t;
2236 }
2237 else if (t == NULL)
2238 {
2239 /* We could not find the version for a symbol when
2240 generating a shared archive. Return an error. */
2241 _bfd_error_handler
2242 /* xgettext:c-format */
2243 (_("%B: version node not found for symbol %s"),
2244 info->output_bfd, h->root.root.string);
2245 bfd_set_error (bfd_error_bad_value);
2246 sinfo->failed = TRUE;
2247 return FALSE;
2248 }
2249 }
2250
2251 /* If we don't have a version for this symbol, see if we can find
2252 something. */
2253 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
2254 {
2255 bfd_boolean hide;
2256
2257 h->verinfo.vertree
2258 = bfd_find_version_for_sym (sinfo->info->version_info,
2259 h->root.root.string, &hide);
2260 if (h->verinfo.vertree != NULL && hide)
2261 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2262 }
2263
2264 return TRUE;
2265 }
2266 \f
2267 /* Read and swap the relocs from the section indicated by SHDR. This
2268 may be either a REL or a RELA section. The relocations are
2269 translated into RELA relocations and stored in INTERNAL_RELOCS,
2270 which should have already been allocated to contain enough space.
2271 The EXTERNAL_RELOCS are a buffer where the external form of the
2272 relocations should be stored.
2273
2274 Returns FALSE if something goes wrong. */
2275
2276 static bfd_boolean
2277 elf_link_read_relocs_from_section (bfd *abfd,
2278 asection *sec,
2279 Elf_Internal_Shdr *shdr,
2280 void *external_relocs,
2281 Elf_Internal_Rela *internal_relocs)
2282 {
2283 const struct elf_backend_data *bed;
2284 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2285 const bfd_byte *erela;
2286 const bfd_byte *erelaend;
2287 Elf_Internal_Rela *irela;
2288 Elf_Internal_Shdr *symtab_hdr;
2289 size_t nsyms;
2290
2291 /* Position ourselves at the start of the section. */
2292 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2293 return FALSE;
2294
2295 /* Read the relocations. */
2296 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2297 return FALSE;
2298
2299 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2300 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2301
2302 bed = get_elf_backend_data (abfd);
2303
2304 /* Convert the external relocations to the internal format. */
2305 if (shdr->sh_entsize == bed->s->sizeof_rel)
2306 swap_in = bed->s->swap_reloc_in;
2307 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2308 swap_in = bed->s->swap_reloca_in;
2309 else
2310 {
2311 bfd_set_error (bfd_error_wrong_format);
2312 return FALSE;
2313 }
2314
2315 erela = (const bfd_byte *) external_relocs;
2316 erelaend = erela + shdr->sh_size;
2317 irela = internal_relocs;
2318 while (erela < erelaend)
2319 {
2320 bfd_vma r_symndx;
2321
2322 (*swap_in) (abfd, erela, irela);
2323 r_symndx = ELF32_R_SYM (irela->r_info);
2324 if (bed->s->arch_size == 64)
2325 r_symndx >>= 24;
2326 if (nsyms > 0)
2327 {
2328 if ((size_t) r_symndx >= nsyms)
2329 {
2330 _bfd_error_handler
2331 /* xgettext:c-format */
2332 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2333 " for offset 0x%lx in section `%A'"),
2334 abfd, sec,
2335 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2336 bfd_set_error (bfd_error_bad_value);
2337 return FALSE;
2338 }
2339 }
2340 else if (r_symndx != STN_UNDEF)
2341 {
2342 _bfd_error_handler
2343 /* xgettext:c-format */
2344 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2345 " when the object file has no symbol table"),
2346 abfd, sec,
2347 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2348 bfd_set_error (bfd_error_bad_value);
2349 return FALSE;
2350 }
2351 irela += bed->s->int_rels_per_ext_rel;
2352 erela += shdr->sh_entsize;
2353 }
2354
2355 return TRUE;
2356 }
2357
2358 /* Read and swap the relocs for a section O. They may have been
2359 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2360 not NULL, they are used as buffers to read into. They are known to
2361 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2362 the return value is allocated using either malloc or bfd_alloc,
2363 according to the KEEP_MEMORY argument. If O has two relocation
2364 sections (both REL and RELA relocations), then the REL_HDR
2365 relocations will appear first in INTERNAL_RELOCS, followed by the
2366 RELA_HDR relocations. */
2367
2368 Elf_Internal_Rela *
2369 _bfd_elf_link_read_relocs (bfd *abfd,
2370 asection *o,
2371 void *external_relocs,
2372 Elf_Internal_Rela *internal_relocs,
2373 bfd_boolean keep_memory)
2374 {
2375 void *alloc1 = NULL;
2376 Elf_Internal_Rela *alloc2 = NULL;
2377 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2378 struct bfd_elf_section_data *esdo = elf_section_data (o);
2379 Elf_Internal_Rela *internal_rela_relocs;
2380
2381 if (esdo->relocs != NULL)
2382 return esdo->relocs;
2383
2384 if (o->reloc_count == 0)
2385 return NULL;
2386
2387 if (internal_relocs == NULL)
2388 {
2389 bfd_size_type size;
2390
2391 size = o->reloc_count;
2392 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2393 if (keep_memory)
2394 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2395 else
2396 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2397 if (internal_relocs == NULL)
2398 goto error_return;
2399 }
2400
2401 if (external_relocs == NULL)
2402 {
2403 bfd_size_type size = 0;
2404
2405 if (esdo->rel.hdr)
2406 size += esdo->rel.hdr->sh_size;
2407 if (esdo->rela.hdr)
2408 size += esdo->rela.hdr->sh_size;
2409
2410 alloc1 = bfd_malloc (size);
2411 if (alloc1 == NULL)
2412 goto error_return;
2413 external_relocs = alloc1;
2414 }
2415
2416 internal_rela_relocs = internal_relocs;
2417 if (esdo->rel.hdr)
2418 {
2419 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2420 external_relocs,
2421 internal_relocs))
2422 goto error_return;
2423 external_relocs = (((bfd_byte *) external_relocs)
2424 + esdo->rel.hdr->sh_size);
2425 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2426 * bed->s->int_rels_per_ext_rel);
2427 }
2428
2429 if (esdo->rela.hdr
2430 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2431 external_relocs,
2432 internal_rela_relocs)))
2433 goto error_return;
2434
2435 /* Cache the results for next time, if we can. */
2436 if (keep_memory)
2437 esdo->relocs = internal_relocs;
2438
2439 if (alloc1 != NULL)
2440 free (alloc1);
2441
2442 /* Don't free alloc2, since if it was allocated we are passing it
2443 back (under the name of internal_relocs). */
2444
2445 return internal_relocs;
2446
2447 error_return:
2448 if (alloc1 != NULL)
2449 free (alloc1);
2450 if (alloc2 != NULL)
2451 {
2452 if (keep_memory)
2453 bfd_release (abfd, alloc2);
2454 else
2455 free (alloc2);
2456 }
2457 return NULL;
2458 }
2459
2460 /* Compute the size of, and allocate space for, REL_HDR which is the
2461 section header for a section containing relocations for O. */
2462
2463 static bfd_boolean
2464 _bfd_elf_link_size_reloc_section (bfd *abfd,
2465 struct bfd_elf_section_reloc_data *reldata)
2466 {
2467 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2468
2469 /* That allows us to calculate the size of the section. */
2470 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2471
2472 /* The contents field must last into write_object_contents, so we
2473 allocate it with bfd_alloc rather than malloc. Also since we
2474 cannot be sure that the contents will actually be filled in,
2475 we zero the allocated space. */
2476 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2477 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2478 return FALSE;
2479
2480 if (reldata->hashes == NULL && reldata->count)
2481 {
2482 struct elf_link_hash_entry **p;
2483
2484 p = ((struct elf_link_hash_entry **)
2485 bfd_zmalloc (reldata->count * sizeof (*p)));
2486 if (p == NULL)
2487 return FALSE;
2488
2489 reldata->hashes = p;
2490 }
2491
2492 return TRUE;
2493 }
2494
2495 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2496 originated from the section given by INPUT_REL_HDR) to the
2497 OUTPUT_BFD. */
2498
2499 bfd_boolean
2500 _bfd_elf_link_output_relocs (bfd *output_bfd,
2501 asection *input_section,
2502 Elf_Internal_Shdr *input_rel_hdr,
2503 Elf_Internal_Rela *internal_relocs,
2504 struct elf_link_hash_entry **rel_hash
2505 ATTRIBUTE_UNUSED)
2506 {
2507 Elf_Internal_Rela *irela;
2508 Elf_Internal_Rela *irelaend;
2509 bfd_byte *erel;
2510 struct bfd_elf_section_reloc_data *output_reldata;
2511 asection *output_section;
2512 const struct elf_backend_data *bed;
2513 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2514 struct bfd_elf_section_data *esdo;
2515
2516 output_section = input_section->output_section;
2517
2518 bed = get_elf_backend_data (output_bfd);
2519 esdo = elf_section_data (output_section);
2520 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2521 {
2522 output_reldata = &esdo->rel;
2523 swap_out = bed->s->swap_reloc_out;
2524 }
2525 else if (esdo->rela.hdr
2526 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2527 {
2528 output_reldata = &esdo->rela;
2529 swap_out = bed->s->swap_reloca_out;
2530 }
2531 else
2532 {
2533 _bfd_error_handler
2534 /* xgettext:c-format */
2535 (_("%B: relocation size mismatch in %B section %A"),
2536 output_bfd, input_section->owner, input_section);
2537 bfd_set_error (bfd_error_wrong_format);
2538 return FALSE;
2539 }
2540
2541 erel = output_reldata->hdr->contents;
2542 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2543 irela = internal_relocs;
2544 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2545 * bed->s->int_rels_per_ext_rel);
2546 while (irela < irelaend)
2547 {
2548 (*swap_out) (output_bfd, irela, erel);
2549 irela += bed->s->int_rels_per_ext_rel;
2550 erel += input_rel_hdr->sh_entsize;
2551 }
2552
2553 /* Bump the counter, so that we know where to add the next set of
2554 relocations. */
2555 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2556
2557 return TRUE;
2558 }
2559 \f
2560 /* Make weak undefined symbols in PIE dynamic. */
2561
2562 bfd_boolean
2563 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2564 struct elf_link_hash_entry *h)
2565 {
2566 if (bfd_link_pie (info)
2567 && h->dynindx == -1
2568 && h->root.type == bfd_link_hash_undefweak)
2569 return bfd_elf_link_record_dynamic_symbol (info, h);
2570
2571 return TRUE;
2572 }
2573
2574 /* Fix up the flags for a symbol. This handles various cases which
2575 can only be fixed after all the input files are seen. This is
2576 currently called by both adjust_dynamic_symbol and
2577 assign_sym_version, which is unnecessary but perhaps more robust in
2578 the face of future changes. */
2579
2580 static bfd_boolean
2581 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2582 struct elf_info_failed *eif)
2583 {
2584 const struct elf_backend_data *bed;
2585
2586 /* If this symbol was mentioned in a non-ELF file, try to set
2587 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2588 permit a non-ELF file to correctly refer to a symbol defined in
2589 an ELF dynamic object. */
2590 if (h->non_elf)
2591 {
2592 while (h->root.type == bfd_link_hash_indirect)
2593 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2594
2595 if (h->root.type != bfd_link_hash_defined
2596 && h->root.type != bfd_link_hash_defweak)
2597 {
2598 h->ref_regular = 1;
2599 h->ref_regular_nonweak = 1;
2600 }
2601 else
2602 {
2603 if (h->root.u.def.section->owner != NULL
2604 && (bfd_get_flavour (h->root.u.def.section->owner)
2605 == bfd_target_elf_flavour))
2606 {
2607 h->ref_regular = 1;
2608 h->ref_regular_nonweak = 1;
2609 }
2610 else
2611 h->def_regular = 1;
2612 }
2613
2614 if (h->dynindx == -1
2615 && (h->def_dynamic
2616 || h->ref_dynamic))
2617 {
2618 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2619 {
2620 eif->failed = TRUE;
2621 return FALSE;
2622 }
2623 }
2624 }
2625 else
2626 {
2627 /* Unfortunately, NON_ELF is only correct if the symbol
2628 was first seen in a non-ELF file. Fortunately, if the symbol
2629 was first seen in an ELF file, we're probably OK unless the
2630 symbol was defined in a non-ELF file. Catch that case here.
2631 FIXME: We're still in trouble if the symbol was first seen in
2632 a dynamic object, and then later in a non-ELF regular object. */
2633 if ((h->root.type == bfd_link_hash_defined
2634 || h->root.type == bfd_link_hash_defweak)
2635 && !h->def_regular
2636 && (h->root.u.def.section->owner != NULL
2637 ? (bfd_get_flavour (h->root.u.def.section->owner)
2638 != bfd_target_elf_flavour)
2639 : (bfd_is_abs_section (h->root.u.def.section)
2640 && !h->def_dynamic)))
2641 h->def_regular = 1;
2642 }
2643
2644 /* Backend specific symbol fixup. */
2645 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2646 if (bed->elf_backend_fixup_symbol
2647 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2648 return FALSE;
2649
2650 /* If this is a final link, and the symbol was defined as a common
2651 symbol in a regular object file, and there was no definition in
2652 any dynamic object, then the linker will have allocated space for
2653 the symbol in a common section but the DEF_REGULAR
2654 flag will not have been set. */
2655 if (h->root.type == bfd_link_hash_defined
2656 && !h->def_regular
2657 && h->ref_regular
2658 && !h->def_dynamic
2659 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2660 h->def_regular = 1;
2661
2662 /* If a weak undefined symbol has non-default visibility, we also
2663 hide it from the dynamic linker. */
2664 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2665 && h->root.type == bfd_link_hash_undefweak)
2666 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2667
2668 /* A hidden versioned symbol in executable should be forced local if
2669 it is is locally defined, not referenced by shared library and not
2670 exported. */
2671 else if (bfd_link_executable (eif->info)
2672 && h->versioned == versioned_hidden
2673 && !eif->info->export_dynamic
2674 && !h->dynamic
2675 && !h->ref_dynamic
2676 && h->def_regular)
2677 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2678
2679 /* If -Bsymbolic was used (which means to bind references to global
2680 symbols to the definition within the shared object), and this
2681 symbol was defined in a regular object, then it actually doesn't
2682 need a PLT entry. Likewise, if the symbol has non-default
2683 visibility. If the symbol has hidden or internal visibility, we
2684 will force it local. */
2685 else if (h->needs_plt
2686 && bfd_link_pic (eif->info)
2687 && is_elf_hash_table (eif->info->hash)
2688 && (SYMBOLIC_BIND (eif->info, h)
2689 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2690 && h->def_regular)
2691 {
2692 bfd_boolean force_local;
2693
2694 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2695 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2696 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2697 }
2698
2699 /* If this is a weak defined symbol in a dynamic object, and we know
2700 the real definition in the dynamic object, copy interesting flags
2701 over to the real definition. */
2702 if (h->u.weakdef != NULL)
2703 {
2704 /* If the real definition is defined by a regular object file,
2705 don't do anything special. See the longer description in
2706 _bfd_elf_adjust_dynamic_symbol, below. */
2707 if (h->u.weakdef->def_regular)
2708 h->u.weakdef = NULL;
2709 else
2710 {
2711 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2712
2713 while (h->root.type == bfd_link_hash_indirect)
2714 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2715
2716 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2717 || h->root.type == bfd_link_hash_defweak);
2718 BFD_ASSERT (weakdef->def_dynamic);
2719 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2720 || weakdef->root.type == bfd_link_hash_defweak);
2721 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2722 }
2723 }
2724
2725 return TRUE;
2726 }
2727
2728 /* Make the backend pick a good value for a dynamic symbol. This is
2729 called via elf_link_hash_traverse, and also calls itself
2730 recursively. */
2731
2732 static bfd_boolean
2733 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2734 {
2735 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2736 bfd *dynobj;
2737 const struct elf_backend_data *bed;
2738
2739 if (! is_elf_hash_table (eif->info->hash))
2740 return FALSE;
2741
2742 /* Ignore indirect symbols. These are added by the versioning code. */
2743 if (h->root.type == bfd_link_hash_indirect)
2744 return TRUE;
2745
2746 /* Fix the symbol flags. */
2747 if (! _bfd_elf_fix_symbol_flags (h, eif))
2748 return FALSE;
2749
2750 /* If this symbol does not require a PLT entry, and it is not
2751 defined by a dynamic object, or is not referenced by a regular
2752 object, ignore it. We do have to handle a weak defined symbol,
2753 even if no regular object refers to it, if we decided to add it
2754 to the dynamic symbol table. FIXME: Do we normally need to worry
2755 about symbols which are defined by one dynamic object and
2756 referenced by another one? */
2757 if (!h->needs_plt
2758 && h->type != STT_GNU_IFUNC
2759 && (h->def_regular
2760 || !h->def_dynamic
2761 || (!h->ref_regular
2762 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2763 {
2764 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2765 return TRUE;
2766 }
2767
2768 /* If we've already adjusted this symbol, don't do it again. This
2769 can happen via a recursive call. */
2770 if (h->dynamic_adjusted)
2771 return TRUE;
2772
2773 /* Don't look at this symbol again. Note that we must set this
2774 after checking the above conditions, because we may look at a
2775 symbol once, decide not to do anything, and then get called
2776 recursively later after REF_REGULAR is set below. */
2777 h->dynamic_adjusted = 1;
2778
2779 /* If this is a weak definition, and we know a real definition, and
2780 the real symbol is not itself defined by a regular object file,
2781 then get a good value for the real definition. We handle the
2782 real symbol first, for the convenience of the backend routine.
2783
2784 Note that there is a confusing case here. If the real definition
2785 is defined by a regular object file, we don't get the real symbol
2786 from the dynamic object, but we do get the weak symbol. If the
2787 processor backend uses a COPY reloc, then if some routine in the
2788 dynamic object changes the real symbol, we will not see that
2789 change in the corresponding weak symbol. This is the way other
2790 ELF linkers work as well, and seems to be a result of the shared
2791 library model.
2792
2793 I will clarify this issue. Most SVR4 shared libraries define the
2794 variable _timezone and define timezone as a weak synonym. The
2795 tzset call changes _timezone. If you write
2796 extern int timezone;
2797 int _timezone = 5;
2798 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2799 you might expect that, since timezone is a synonym for _timezone,
2800 the same number will print both times. However, if the processor
2801 backend uses a COPY reloc, then actually timezone will be copied
2802 into your process image, and, since you define _timezone
2803 yourself, _timezone will not. Thus timezone and _timezone will
2804 wind up at different memory locations. The tzset call will set
2805 _timezone, leaving timezone unchanged. */
2806
2807 if (h->u.weakdef != NULL)
2808 {
2809 /* If we get to this point, there is an implicit reference to
2810 H->U.WEAKDEF by a regular object file via the weak symbol H. */
2811 h->u.weakdef->ref_regular = 1;
2812
2813 /* Ensure that the backend adjust_dynamic_symbol function sees
2814 H->U.WEAKDEF before H by recursively calling ourselves. */
2815 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2816 return FALSE;
2817 }
2818
2819 /* If a symbol has no type and no size and does not require a PLT
2820 entry, then we are probably about to do the wrong thing here: we
2821 are probably going to create a COPY reloc for an empty object.
2822 This case can arise when a shared object is built with assembly
2823 code, and the assembly code fails to set the symbol type. */
2824 if (h->size == 0
2825 && h->type == STT_NOTYPE
2826 && !h->needs_plt)
2827 _bfd_error_handler
2828 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2829 h->root.root.string);
2830
2831 dynobj = elf_hash_table (eif->info)->dynobj;
2832 bed = get_elf_backend_data (dynobj);
2833
2834 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2835 {
2836 eif->failed = TRUE;
2837 return FALSE;
2838 }
2839
2840 return TRUE;
2841 }
2842
2843 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2844 DYNBSS. */
2845
2846 bfd_boolean
2847 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2848 struct elf_link_hash_entry *h,
2849 asection *dynbss)
2850 {
2851 unsigned int power_of_two;
2852 bfd_vma mask;
2853 asection *sec = h->root.u.def.section;
2854
2855 /* The section aligment of definition is the maximum alignment
2856 requirement of symbols defined in the section. Since we don't
2857 know the symbol alignment requirement, we start with the
2858 maximum alignment and check low bits of the symbol address
2859 for the minimum alignment. */
2860 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2861 mask = ((bfd_vma) 1 << power_of_two) - 1;
2862 while ((h->root.u.def.value & mask) != 0)
2863 {
2864 mask >>= 1;
2865 --power_of_two;
2866 }
2867
2868 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2869 dynbss))
2870 {
2871 /* Adjust the section alignment if needed. */
2872 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2873 power_of_two))
2874 return FALSE;
2875 }
2876
2877 /* We make sure that the symbol will be aligned properly. */
2878 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2879
2880 /* Define the symbol as being at this point in DYNBSS. */
2881 h->root.u.def.section = dynbss;
2882 h->root.u.def.value = dynbss->size;
2883
2884 /* Increment the size of DYNBSS to make room for the symbol. */
2885 dynbss->size += h->size;
2886
2887 /* No error if extern_protected_data is true. */
2888 if (h->protected_def
2889 && (!info->extern_protected_data
2890 || (info->extern_protected_data < 0
2891 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
2892 info->callbacks->einfo
2893 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2894 h->root.root.string);
2895
2896 return TRUE;
2897 }
2898
2899 /* Adjust all external symbols pointing into SEC_MERGE sections
2900 to reflect the object merging within the sections. */
2901
2902 static bfd_boolean
2903 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2904 {
2905 asection *sec;
2906
2907 if ((h->root.type == bfd_link_hash_defined
2908 || h->root.type == bfd_link_hash_defweak)
2909 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2910 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
2911 {
2912 bfd *output_bfd = (bfd *) data;
2913
2914 h->root.u.def.value =
2915 _bfd_merged_section_offset (output_bfd,
2916 &h->root.u.def.section,
2917 elf_section_data (sec)->sec_info,
2918 h->root.u.def.value);
2919 }
2920
2921 return TRUE;
2922 }
2923
2924 /* Returns false if the symbol referred to by H should be considered
2925 to resolve local to the current module, and true if it should be
2926 considered to bind dynamically. */
2927
2928 bfd_boolean
2929 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2930 struct bfd_link_info *info,
2931 bfd_boolean not_local_protected)
2932 {
2933 bfd_boolean binding_stays_local_p;
2934 const struct elf_backend_data *bed;
2935 struct elf_link_hash_table *hash_table;
2936
2937 if (h == NULL)
2938 return FALSE;
2939
2940 while (h->root.type == bfd_link_hash_indirect
2941 || h->root.type == bfd_link_hash_warning)
2942 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2943
2944 /* If it was forced local, then clearly it's not dynamic. */
2945 if (h->dynindx == -1)
2946 return FALSE;
2947 if (h->forced_local)
2948 return FALSE;
2949
2950 /* Identify the cases where name binding rules say that a
2951 visible symbol resolves locally. */
2952 binding_stays_local_p = (bfd_link_executable (info)
2953 || SYMBOLIC_BIND (info, h));
2954
2955 switch (ELF_ST_VISIBILITY (h->other))
2956 {
2957 case STV_INTERNAL:
2958 case STV_HIDDEN:
2959 return FALSE;
2960
2961 case STV_PROTECTED:
2962 hash_table = elf_hash_table (info);
2963 if (!is_elf_hash_table (hash_table))
2964 return FALSE;
2965
2966 bed = get_elf_backend_data (hash_table->dynobj);
2967
2968 /* Proper resolution for function pointer equality may require
2969 that these symbols perhaps be resolved dynamically, even though
2970 we should be resolving them to the current module. */
2971 if (!not_local_protected || !bed->is_function_type (h->type))
2972 binding_stays_local_p = TRUE;
2973 break;
2974
2975 default:
2976 break;
2977 }
2978
2979 /* If it isn't defined locally, then clearly it's dynamic. */
2980 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2981 return TRUE;
2982
2983 /* Otherwise, the symbol is dynamic if binding rules don't tell
2984 us that it remains local. */
2985 return !binding_stays_local_p;
2986 }
2987
2988 /* Return true if the symbol referred to by H should be considered
2989 to resolve local to the current module, and false otherwise. Differs
2990 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2991 undefined symbols. The two functions are virtually identical except
2992 for the place where forced_local and dynindx == -1 are tested. If
2993 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2994 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2995 the symbol is local only for defined symbols.
2996 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2997 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2998 treatment of undefined weak symbols. For those that do not make
2999 undefined weak symbols dynamic, both functions may return false. */
3000
3001 bfd_boolean
3002 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3003 struct bfd_link_info *info,
3004 bfd_boolean local_protected)
3005 {
3006 const struct elf_backend_data *bed;
3007 struct elf_link_hash_table *hash_table;
3008
3009 /* If it's a local sym, of course we resolve locally. */
3010 if (h == NULL)
3011 return TRUE;
3012
3013 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3014 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3015 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3016 return TRUE;
3017
3018 /* Common symbols that become definitions don't get the DEF_REGULAR
3019 flag set, so test it first, and don't bail out. */
3020 if (ELF_COMMON_DEF_P (h))
3021 /* Do nothing. */;
3022 /* If we don't have a definition in a regular file, then we can't
3023 resolve locally. The sym is either undefined or dynamic. */
3024 else if (!h->def_regular)
3025 return FALSE;
3026
3027 /* Forced local symbols resolve locally. */
3028 if (h->forced_local)
3029 return TRUE;
3030
3031 /* As do non-dynamic symbols. */
3032 if (h->dynindx == -1)
3033 return TRUE;
3034
3035 /* At this point, we know the symbol is defined and dynamic. In an
3036 executable it must resolve locally, likewise when building symbolic
3037 shared libraries. */
3038 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3039 return TRUE;
3040
3041 /* Now deal with defined dynamic symbols in shared libraries. Ones
3042 with default visibility might not resolve locally. */
3043 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3044 return FALSE;
3045
3046 hash_table = elf_hash_table (info);
3047 if (!is_elf_hash_table (hash_table))
3048 return TRUE;
3049
3050 bed = get_elf_backend_data (hash_table->dynobj);
3051
3052 /* If extern_protected_data is false, STV_PROTECTED non-function
3053 symbols are local. */
3054 if ((!info->extern_protected_data
3055 || (info->extern_protected_data < 0
3056 && !bed->extern_protected_data))
3057 && !bed->is_function_type (h->type))
3058 return TRUE;
3059
3060 /* Function pointer equality tests may require that STV_PROTECTED
3061 symbols be treated as dynamic symbols. If the address of a
3062 function not defined in an executable is set to that function's
3063 plt entry in the executable, then the address of the function in
3064 a shared library must also be the plt entry in the executable. */
3065 return local_protected;
3066 }
3067
3068 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3069 aligned. Returns the first TLS output section. */
3070
3071 struct bfd_section *
3072 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3073 {
3074 struct bfd_section *sec, *tls;
3075 unsigned int align = 0;
3076
3077 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3078 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3079 break;
3080 tls = sec;
3081
3082 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3083 if (sec->alignment_power > align)
3084 align = sec->alignment_power;
3085
3086 elf_hash_table (info)->tls_sec = tls;
3087
3088 /* Ensure the alignment of the first section is the largest alignment,
3089 so that the tls segment starts aligned. */
3090 if (tls != NULL)
3091 tls->alignment_power = align;
3092
3093 return tls;
3094 }
3095
3096 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3097 static bfd_boolean
3098 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3099 Elf_Internal_Sym *sym)
3100 {
3101 const struct elf_backend_data *bed;
3102
3103 /* Local symbols do not count, but target specific ones might. */
3104 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3105 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3106 return FALSE;
3107
3108 bed = get_elf_backend_data (abfd);
3109 /* Function symbols do not count. */
3110 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3111 return FALSE;
3112
3113 /* If the section is undefined, then so is the symbol. */
3114 if (sym->st_shndx == SHN_UNDEF)
3115 return FALSE;
3116
3117 /* If the symbol is defined in the common section, then
3118 it is a common definition and so does not count. */
3119 if (bed->common_definition (sym))
3120 return FALSE;
3121
3122 /* If the symbol is in a target specific section then we
3123 must rely upon the backend to tell us what it is. */
3124 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3125 /* FIXME - this function is not coded yet:
3126
3127 return _bfd_is_global_symbol_definition (abfd, sym);
3128
3129 Instead for now assume that the definition is not global,
3130 Even if this is wrong, at least the linker will behave
3131 in the same way that it used to do. */
3132 return FALSE;
3133
3134 return TRUE;
3135 }
3136
3137 /* Search the symbol table of the archive element of the archive ABFD
3138 whose archive map contains a mention of SYMDEF, and determine if
3139 the symbol is defined in this element. */
3140 static bfd_boolean
3141 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3142 {
3143 Elf_Internal_Shdr * hdr;
3144 size_t symcount;
3145 size_t extsymcount;
3146 size_t extsymoff;
3147 Elf_Internal_Sym *isymbuf;
3148 Elf_Internal_Sym *isym;
3149 Elf_Internal_Sym *isymend;
3150 bfd_boolean result;
3151
3152 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3153 if (abfd == NULL)
3154 return FALSE;
3155
3156 if (! bfd_check_format (abfd, bfd_object))
3157 return FALSE;
3158
3159 /* Select the appropriate symbol table. If we don't know if the
3160 object file is an IR object, give linker LTO plugin a chance to
3161 get the correct symbol table. */
3162 if (abfd->plugin_format == bfd_plugin_yes
3163 #if BFD_SUPPORTS_PLUGINS
3164 || (abfd->plugin_format == bfd_plugin_unknown
3165 && bfd_link_plugin_object_p (abfd))
3166 #endif
3167 )
3168 {
3169 /* Use the IR symbol table if the object has been claimed by
3170 plugin. */
3171 abfd = abfd->plugin_dummy_bfd;
3172 hdr = &elf_tdata (abfd)->symtab_hdr;
3173 }
3174 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3175 hdr = &elf_tdata (abfd)->symtab_hdr;
3176 else
3177 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3178
3179 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3180
3181 /* The sh_info field of the symtab header tells us where the
3182 external symbols start. We don't care about the local symbols. */
3183 if (elf_bad_symtab (abfd))
3184 {
3185 extsymcount = symcount;
3186 extsymoff = 0;
3187 }
3188 else
3189 {
3190 extsymcount = symcount - hdr->sh_info;
3191 extsymoff = hdr->sh_info;
3192 }
3193
3194 if (extsymcount == 0)
3195 return FALSE;
3196
3197 /* Read in the symbol table. */
3198 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3199 NULL, NULL, NULL);
3200 if (isymbuf == NULL)
3201 return FALSE;
3202
3203 /* Scan the symbol table looking for SYMDEF. */
3204 result = FALSE;
3205 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3206 {
3207 const char *name;
3208
3209 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3210 isym->st_name);
3211 if (name == NULL)
3212 break;
3213
3214 if (strcmp (name, symdef->name) == 0)
3215 {
3216 result = is_global_data_symbol_definition (abfd, isym);
3217 break;
3218 }
3219 }
3220
3221 free (isymbuf);
3222
3223 return result;
3224 }
3225 \f
3226 /* Add an entry to the .dynamic table. */
3227
3228 bfd_boolean
3229 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3230 bfd_vma tag,
3231 bfd_vma val)
3232 {
3233 struct elf_link_hash_table *hash_table;
3234 const struct elf_backend_data *bed;
3235 asection *s;
3236 bfd_size_type newsize;
3237 bfd_byte *newcontents;
3238 Elf_Internal_Dyn dyn;
3239
3240 hash_table = elf_hash_table (info);
3241 if (! is_elf_hash_table (hash_table))
3242 return FALSE;
3243
3244 bed = get_elf_backend_data (hash_table->dynobj);
3245 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3246 BFD_ASSERT (s != NULL);
3247
3248 newsize = s->size + bed->s->sizeof_dyn;
3249 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3250 if (newcontents == NULL)
3251 return FALSE;
3252
3253 dyn.d_tag = tag;
3254 dyn.d_un.d_val = val;
3255 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3256
3257 s->size = newsize;
3258 s->contents = newcontents;
3259
3260 return TRUE;
3261 }
3262
3263 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3264 otherwise just check whether one already exists. Returns -1 on error,
3265 1 if a DT_NEEDED tag already exists, and 0 on success. */
3266
3267 static int
3268 elf_add_dt_needed_tag (bfd *abfd,
3269 struct bfd_link_info *info,
3270 const char *soname,
3271 bfd_boolean do_it)
3272 {
3273 struct elf_link_hash_table *hash_table;
3274 size_t strindex;
3275
3276 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3277 return -1;
3278
3279 hash_table = elf_hash_table (info);
3280 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3281 if (strindex == (size_t) -1)
3282 return -1;
3283
3284 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3285 {
3286 asection *sdyn;
3287 const struct elf_backend_data *bed;
3288 bfd_byte *extdyn;
3289
3290 bed = get_elf_backend_data (hash_table->dynobj);
3291 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3292 if (sdyn != NULL)
3293 for (extdyn = sdyn->contents;
3294 extdyn < sdyn->contents + sdyn->size;
3295 extdyn += bed->s->sizeof_dyn)
3296 {
3297 Elf_Internal_Dyn dyn;
3298
3299 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3300 if (dyn.d_tag == DT_NEEDED
3301 && dyn.d_un.d_val == strindex)
3302 {
3303 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3304 return 1;
3305 }
3306 }
3307 }
3308
3309 if (do_it)
3310 {
3311 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3312 return -1;
3313
3314 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3315 return -1;
3316 }
3317 else
3318 /* We were just checking for existence of the tag. */
3319 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3320
3321 return 0;
3322 }
3323
3324 /* Return true if SONAME is on the needed list between NEEDED and STOP
3325 (or the end of list if STOP is NULL), and needed by a library that
3326 will be loaded. */
3327
3328 static bfd_boolean
3329 on_needed_list (const char *soname,
3330 struct bfd_link_needed_list *needed,
3331 struct bfd_link_needed_list *stop)
3332 {
3333 struct bfd_link_needed_list *look;
3334 for (look = needed; look != stop; look = look->next)
3335 if (strcmp (soname, look->name) == 0
3336 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3337 /* If needed by a library that itself is not directly
3338 needed, recursively check whether that library is
3339 indirectly needed. Since we add DT_NEEDED entries to
3340 the end of the list, library dependencies appear after
3341 the library. Therefore search prior to the current
3342 LOOK, preventing possible infinite recursion. */
3343 || on_needed_list (elf_dt_name (look->by), needed, look)))
3344 return TRUE;
3345
3346 return FALSE;
3347 }
3348
3349 /* Sort symbol by value, section, and size. */
3350 static int
3351 elf_sort_symbol (const void *arg1, const void *arg2)
3352 {
3353 const struct elf_link_hash_entry *h1;
3354 const struct elf_link_hash_entry *h2;
3355 bfd_signed_vma vdiff;
3356
3357 h1 = *(const struct elf_link_hash_entry **) arg1;
3358 h2 = *(const struct elf_link_hash_entry **) arg2;
3359 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3360 if (vdiff != 0)
3361 return vdiff > 0 ? 1 : -1;
3362 else
3363 {
3364 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3365 if (sdiff != 0)
3366 return sdiff > 0 ? 1 : -1;
3367 }
3368 vdiff = h1->size - h2->size;
3369 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3370 }
3371
3372 /* This function is used to adjust offsets into .dynstr for
3373 dynamic symbols. This is called via elf_link_hash_traverse. */
3374
3375 static bfd_boolean
3376 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3377 {
3378 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3379
3380 if (h->dynindx != -1)
3381 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3382 return TRUE;
3383 }
3384
3385 /* Assign string offsets in .dynstr, update all structures referencing
3386 them. */
3387
3388 static bfd_boolean
3389 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3390 {
3391 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3392 struct elf_link_local_dynamic_entry *entry;
3393 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3394 bfd *dynobj = hash_table->dynobj;
3395 asection *sdyn;
3396 bfd_size_type size;
3397 const struct elf_backend_data *bed;
3398 bfd_byte *extdyn;
3399
3400 _bfd_elf_strtab_finalize (dynstr);
3401 size = _bfd_elf_strtab_size (dynstr);
3402
3403 bed = get_elf_backend_data (dynobj);
3404 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3405 BFD_ASSERT (sdyn != NULL);
3406
3407 /* Update all .dynamic entries referencing .dynstr strings. */
3408 for (extdyn = sdyn->contents;
3409 extdyn < sdyn->contents + sdyn->size;
3410 extdyn += bed->s->sizeof_dyn)
3411 {
3412 Elf_Internal_Dyn dyn;
3413
3414 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3415 switch (dyn.d_tag)
3416 {
3417 case DT_STRSZ:
3418 dyn.d_un.d_val = size;
3419 break;
3420 case DT_NEEDED:
3421 case DT_SONAME:
3422 case DT_RPATH:
3423 case DT_RUNPATH:
3424 case DT_FILTER:
3425 case DT_AUXILIARY:
3426 case DT_AUDIT:
3427 case DT_DEPAUDIT:
3428 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3429 break;
3430 default:
3431 continue;
3432 }
3433 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3434 }
3435
3436 /* Now update local dynamic symbols. */
3437 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3438 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3439 entry->isym.st_name);
3440
3441 /* And the rest of dynamic symbols. */
3442 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3443
3444 /* Adjust version definitions. */
3445 if (elf_tdata (output_bfd)->cverdefs)
3446 {
3447 asection *s;
3448 bfd_byte *p;
3449 size_t i;
3450 Elf_Internal_Verdef def;
3451 Elf_Internal_Verdaux defaux;
3452
3453 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3454 p = s->contents;
3455 do
3456 {
3457 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3458 &def);
3459 p += sizeof (Elf_External_Verdef);
3460 if (def.vd_aux != sizeof (Elf_External_Verdef))
3461 continue;
3462 for (i = 0; i < def.vd_cnt; ++i)
3463 {
3464 _bfd_elf_swap_verdaux_in (output_bfd,
3465 (Elf_External_Verdaux *) p, &defaux);
3466 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3467 defaux.vda_name);
3468 _bfd_elf_swap_verdaux_out (output_bfd,
3469 &defaux, (Elf_External_Verdaux *) p);
3470 p += sizeof (Elf_External_Verdaux);
3471 }
3472 }
3473 while (def.vd_next);
3474 }
3475
3476 /* Adjust version references. */
3477 if (elf_tdata (output_bfd)->verref)
3478 {
3479 asection *s;
3480 bfd_byte *p;
3481 size_t i;
3482 Elf_Internal_Verneed need;
3483 Elf_Internal_Vernaux needaux;
3484
3485 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3486 p = s->contents;
3487 do
3488 {
3489 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3490 &need);
3491 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3492 _bfd_elf_swap_verneed_out (output_bfd, &need,
3493 (Elf_External_Verneed *) p);
3494 p += sizeof (Elf_External_Verneed);
3495 for (i = 0; i < need.vn_cnt; ++i)
3496 {
3497 _bfd_elf_swap_vernaux_in (output_bfd,
3498 (Elf_External_Vernaux *) p, &needaux);
3499 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3500 needaux.vna_name);
3501 _bfd_elf_swap_vernaux_out (output_bfd,
3502 &needaux,
3503 (Elf_External_Vernaux *) p);
3504 p += sizeof (Elf_External_Vernaux);
3505 }
3506 }
3507 while (need.vn_next);
3508 }
3509
3510 return TRUE;
3511 }
3512 \f
3513 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3514 The default is to only match when the INPUT and OUTPUT are exactly
3515 the same target. */
3516
3517 bfd_boolean
3518 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3519 const bfd_target *output)
3520 {
3521 return input == output;
3522 }
3523
3524 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3525 This version is used when different targets for the same architecture
3526 are virtually identical. */
3527
3528 bfd_boolean
3529 _bfd_elf_relocs_compatible (const bfd_target *input,
3530 const bfd_target *output)
3531 {
3532 const struct elf_backend_data *obed, *ibed;
3533
3534 if (input == output)
3535 return TRUE;
3536
3537 ibed = xvec_get_elf_backend_data (input);
3538 obed = xvec_get_elf_backend_data (output);
3539
3540 if (ibed->arch != obed->arch)
3541 return FALSE;
3542
3543 /* If both backends are using this function, deem them compatible. */
3544 return ibed->relocs_compatible == obed->relocs_compatible;
3545 }
3546
3547 /* Make a special call to the linker "notice" function to tell it that
3548 we are about to handle an as-needed lib, or have finished
3549 processing the lib. */
3550
3551 bfd_boolean
3552 _bfd_elf_notice_as_needed (bfd *ibfd,
3553 struct bfd_link_info *info,
3554 enum notice_asneeded_action act)
3555 {
3556 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3557 }
3558
3559 /* Check relocations an ELF object file. */
3560
3561 bfd_boolean
3562 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3563 {
3564 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3565 struct elf_link_hash_table *htab = elf_hash_table (info);
3566
3567 /* If this object is the same format as the output object, and it is
3568 not a shared library, then let the backend look through the
3569 relocs.
3570
3571 This is required to build global offset table entries and to
3572 arrange for dynamic relocs. It is not required for the
3573 particular common case of linking non PIC code, even when linking
3574 against shared libraries, but unfortunately there is no way of
3575 knowing whether an object file has been compiled PIC or not.
3576 Looking through the relocs is not particularly time consuming.
3577 The problem is that we must either (1) keep the relocs in memory,
3578 which causes the linker to require additional runtime memory or
3579 (2) read the relocs twice from the input file, which wastes time.
3580 This would be a good case for using mmap.
3581
3582 I have no idea how to handle linking PIC code into a file of a
3583 different format. It probably can't be done. */
3584 if ((abfd->flags & DYNAMIC) == 0
3585 && is_elf_hash_table (htab)
3586 && bed->check_relocs != NULL
3587 && elf_object_id (abfd) == elf_hash_table_id (htab)
3588 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3589 {
3590 asection *o;
3591
3592 for (o = abfd->sections; o != NULL; o = o->next)
3593 {
3594 Elf_Internal_Rela *internal_relocs;
3595 bfd_boolean ok;
3596
3597 /* Don't check relocations in excluded sections. */
3598 if ((o->flags & SEC_RELOC) == 0
3599 || (o->flags & SEC_EXCLUDE) != 0
3600 || o->reloc_count == 0
3601 || ((info->strip == strip_all || info->strip == strip_debugger)
3602 && (o->flags & SEC_DEBUGGING) != 0)
3603 || bfd_is_abs_section (o->output_section))
3604 continue;
3605
3606 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3607 info->keep_memory);
3608 if (internal_relocs == NULL)
3609 return FALSE;
3610
3611 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3612
3613 if (elf_section_data (o)->relocs != internal_relocs)
3614 free (internal_relocs);
3615
3616 if (! ok)
3617 return FALSE;
3618 }
3619 }
3620
3621 return TRUE;
3622 }
3623
3624 /* Add symbols from an ELF object file to the linker hash table. */
3625
3626 static bfd_boolean
3627 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3628 {
3629 Elf_Internal_Ehdr *ehdr;
3630 Elf_Internal_Shdr *hdr;
3631 size_t symcount;
3632 size_t extsymcount;
3633 size_t extsymoff;
3634 struct elf_link_hash_entry **sym_hash;
3635 bfd_boolean dynamic;
3636 Elf_External_Versym *extversym = NULL;
3637 Elf_External_Versym *ever;
3638 struct elf_link_hash_entry *weaks;
3639 struct elf_link_hash_entry **nondeflt_vers = NULL;
3640 size_t nondeflt_vers_cnt = 0;
3641 Elf_Internal_Sym *isymbuf = NULL;
3642 Elf_Internal_Sym *isym;
3643 Elf_Internal_Sym *isymend;
3644 const struct elf_backend_data *bed;
3645 bfd_boolean add_needed;
3646 struct elf_link_hash_table *htab;
3647 bfd_size_type amt;
3648 void *alloc_mark = NULL;
3649 struct bfd_hash_entry **old_table = NULL;
3650 unsigned int old_size = 0;
3651 unsigned int old_count = 0;
3652 void *old_tab = NULL;
3653 void *old_ent;
3654 struct bfd_link_hash_entry *old_undefs = NULL;
3655 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3656 void *old_strtab = NULL;
3657 size_t tabsize = 0;
3658 asection *s;
3659 bfd_boolean just_syms;
3660
3661 htab = elf_hash_table (info);
3662 bed = get_elf_backend_data (abfd);
3663
3664 if ((abfd->flags & DYNAMIC) == 0)
3665 dynamic = FALSE;
3666 else
3667 {
3668 dynamic = TRUE;
3669
3670 /* You can't use -r against a dynamic object. Also, there's no
3671 hope of using a dynamic object which does not exactly match
3672 the format of the output file. */
3673 if (bfd_link_relocatable (info)
3674 || !is_elf_hash_table (htab)
3675 || info->output_bfd->xvec != abfd->xvec)
3676 {
3677 if (bfd_link_relocatable (info))
3678 bfd_set_error (bfd_error_invalid_operation);
3679 else
3680 bfd_set_error (bfd_error_wrong_format);
3681 goto error_return;
3682 }
3683 }
3684
3685 ehdr = elf_elfheader (abfd);
3686 if (info->warn_alternate_em
3687 && bed->elf_machine_code != ehdr->e_machine
3688 && ((bed->elf_machine_alt1 != 0
3689 && ehdr->e_machine == bed->elf_machine_alt1)
3690 || (bed->elf_machine_alt2 != 0
3691 && ehdr->e_machine == bed->elf_machine_alt2)))
3692 info->callbacks->einfo
3693 /* xgettext:c-format */
3694 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3695 ehdr->e_machine, abfd, bed->elf_machine_code);
3696
3697 /* As a GNU extension, any input sections which are named
3698 .gnu.warning.SYMBOL are treated as warning symbols for the given
3699 symbol. This differs from .gnu.warning sections, which generate
3700 warnings when they are included in an output file. */
3701 /* PR 12761: Also generate this warning when building shared libraries. */
3702 for (s = abfd->sections; s != NULL; s = s->next)
3703 {
3704 const char *name;
3705
3706 name = bfd_get_section_name (abfd, s);
3707 if (CONST_STRNEQ (name, ".gnu.warning."))
3708 {
3709 char *msg;
3710 bfd_size_type sz;
3711
3712 name += sizeof ".gnu.warning." - 1;
3713
3714 /* If this is a shared object, then look up the symbol
3715 in the hash table. If it is there, and it is already
3716 been defined, then we will not be using the entry
3717 from this shared object, so we don't need to warn.
3718 FIXME: If we see the definition in a regular object
3719 later on, we will warn, but we shouldn't. The only
3720 fix is to keep track of what warnings we are supposed
3721 to emit, and then handle them all at the end of the
3722 link. */
3723 if (dynamic)
3724 {
3725 struct elf_link_hash_entry *h;
3726
3727 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3728
3729 /* FIXME: What about bfd_link_hash_common? */
3730 if (h != NULL
3731 && (h->root.type == bfd_link_hash_defined
3732 || h->root.type == bfd_link_hash_defweak))
3733 continue;
3734 }
3735
3736 sz = s->size;
3737 msg = (char *) bfd_alloc (abfd, sz + 1);
3738 if (msg == NULL)
3739 goto error_return;
3740
3741 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3742 goto error_return;
3743
3744 msg[sz] = '\0';
3745
3746 if (! (_bfd_generic_link_add_one_symbol
3747 (info, abfd, name, BSF_WARNING, s, 0, msg,
3748 FALSE, bed->collect, NULL)))
3749 goto error_return;
3750
3751 if (bfd_link_executable (info))
3752 {
3753 /* Clobber the section size so that the warning does
3754 not get copied into the output file. */
3755 s->size = 0;
3756
3757 /* Also set SEC_EXCLUDE, so that symbols defined in
3758 the warning section don't get copied to the output. */
3759 s->flags |= SEC_EXCLUDE;
3760 }
3761 }
3762 }
3763
3764 just_syms = ((s = abfd->sections) != NULL
3765 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3766
3767 add_needed = TRUE;
3768 if (! dynamic)
3769 {
3770 /* If we are creating a shared library, create all the dynamic
3771 sections immediately. We need to attach them to something,
3772 so we attach them to this BFD, provided it is the right
3773 format and is not from ld --just-symbols. Always create the
3774 dynamic sections for -E/--dynamic-list. FIXME: If there
3775 are no input BFD's of the same format as the output, we can't
3776 make a shared library. */
3777 if (!just_syms
3778 && (bfd_link_pic (info)
3779 || (!bfd_link_relocatable (info)
3780 && (info->export_dynamic || info->dynamic)))
3781 && is_elf_hash_table (htab)
3782 && info->output_bfd->xvec == abfd->xvec
3783 && !htab->dynamic_sections_created)
3784 {
3785 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3786 goto error_return;
3787 }
3788 }
3789 else if (!is_elf_hash_table (htab))
3790 goto error_return;
3791 else
3792 {
3793 const char *soname = NULL;
3794 char *audit = NULL;
3795 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3796 int ret;
3797
3798 /* ld --just-symbols and dynamic objects don't mix very well.
3799 ld shouldn't allow it. */
3800 if (just_syms)
3801 abort ();
3802
3803 /* If this dynamic lib was specified on the command line with
3804 --as-needed in effect, then we don't want to add a DT_NEEDED
3805 tag unless the lib is actually used. Similary for libs brought
3806 in by another lib's DT_NEEDED. When --no-add-needed is used
3807 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3808 any dynamic library in DT_NEEDED tags in the dynamic lib at
3809 all. */
3810 add_needed = (elf_dyn_lib_class (abfd)
3811 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3812 | DYN_NO_NEEDED)) == 0;
3813
3814 s = bfd_get_section_by_name (abfd, ".dynamic");
3815 if (s != NULL)
3816 {
3817 bfd_byte *dynbuf;
3818 bfd_byte *extdyn;
3819 unsigned int elfsec;
3820 unsigned long shlink;
3821
3822 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3823 {
3824 error_free_dyn:
3825 free (dynbuf);
3826 goto error_return;
3827 }
3828
3829 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3830 if (elfsec == SHN_BAD)
3831 goto error_free_dyn;
3832 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3833
3834 for (extdyn = dynbuf;
3835 extdyn < dynbuf + s->size;
3836 extdyn += bed->s->sizeof_dyn)
3837 {
3838 Elf_Internal_Dyn dyn;
3839
3840 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3841 if (dyn.d_tag == DT_SONAME)
3842 {
3843 unsigned int tagv = dyn.d_un.d_val;
3844 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3845 if (soname == NULL)
3846 goto error_free_dyn;
3847 }
3848 if (dyn.d_tag == DT_NEEDED)
3849 {
3850 struct bfd_link_needed_list *n, **pn;
3851 char *fnm, *anm;
3852 unsigned int tagv = dyn.d_un.d_val;
3853
3854 amt = sizeof (struct bfd_link_needed_list);
3855 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3856 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3857 if (n == NULL || fnm == NULL)
3858 goto error_free_dyn;
3859 amt = strlen (fnm) + 1;
3860 anm = (char *) bfd_alloc (abfd, amt);
3861 if (anm == NULL)
3862 goto error_free_dyn;
3863 memcpy (anm, fnm, amt);
3864 n->name = anm;
3865 n->by = abfd;
3866 n->next = NULL;
3867 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3868 ;
3869 *pn = n;
3870 }
3871 if (dyn.d_tag == DT_RUNPATH)
3872 {
3873 struct bfd_link_needed_list *n, **pn;
3874 char *fnm, *anm;
3875 unsigned int tagv = dyn.d_un.d_val;
3876
3877 amt = sizeof (struct bfd_link_needed_list);
3878 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3879 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3880 if (n == NULL || fnm == NULL)
3881 goto error_free_dyn;
3882 amt = strlen (fnm) + 1;
3883 anm = (char *) bfd_alloc (abfd, amt);
3884 if (anm == NULL)
3885 goto error_free_dyn;
3886 memcpy (anm, fnm, amt);
3887 n->name = anm;
3888 n->by = abfd;
3889 n->next = NULL;
3890 for (pn = & runpath;
3891 *pn != NULL;
3892 pn = &(*pn)->next)
3893 ;
3894 *pn = n;
3895 }
3896 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3897 if (!runpath && dyn.d_tag == DT_RPATH)
3898 {
3899 struct bfd_link_needed_list *n, **pn;
3900 char *fnm, *anm;
3901 unsigned int tagv = dyn.d_un.d_val;
3902
3903 amt = sizeof (struct bfd_link_needed_list);
3904 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3905 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3906 if (n == NULL || fnm == NULL)
3907 goto error_free_dyn;
3908 amt = strlen (fnm) + 1;
3909 anm = (char *) bfd_alloc (abfd, amt);
3910 if (anm == NULL)
3911 goto error_free_dyn;
3912 memcpy (anm, fnm, amt);
3913 n->name = anm;
3914 n->by = abfd;
3915 n->next = NULL;
3916 for (pn = & rpath;
3917 *pn != NULL;
3918 pn = &(*pn)->next)
3919 ;
3920 *pn = n;
3921 }
3922 if (dyn.d_tag == DT_AUDIT)
3923 {
3924 unsigned int tagv = dyn.d_un.d_val;
3925 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3926 }
3927 }
3928
3929 free (dynbuf);
3930 }
3931
3932 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3933 frees all more recently bfd_alloc'd blocks as well. */
3934 if (runpath)
3935 rpath = runpath;
3936
3937 if (rpath)
3938 {
3939 struct bfd_link_needed_list **pn;
3940 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3941 ;
3942 *pn = rpath;
3943 }
3944
3945 /* We do not want to include any of the sections in a dynamic
3946 object in the output file. We hack by simply clobbering the
3947 list of sections in the BFD. This could be handled more
3948 cleanly by, say, a new section flag; the existing
3949 SEC_NEVER_LOAD flag is not the one we want, because that one
3950 still implies that the section takes up space in the output
3951 file. */
3952 bfd_section_list_clear (abfd);
3953
3954 /* Find the name to use in a DT_NEEDED entry that refers to this
3955 object. If the object has a DT_SONAME entry, we use it.
3956 Otherwise, if the generic linker stuck something in
3957 elf_dt_name, we use that. Otherwise, we just use the file
3958 name. */
3959 if (soname == NULL || *soname == '\0')
3960 {
3961 soname = elf_dt_name (abfd);
3962 if (soname == NULL || *soname == '\0')
3963 soname = bfd_get_filename (abfd);
3964 }
3965
3966 /* Save the SONAME because sometimes the linker emulation code
3967 will need to know it. */
3968 elf_dt_name (abfd) = soname;
3969
3970 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3971 if (ret < 0)
3972 goto error_return;
3973
3974 /* If we have already included this dynamic object in the
3975 link, just ignore it. There is no reason to include a
3976 particular dynamic object more than once. */
3977 if (ret > 0)
3978 return TRUE;
3979
3980 /* Save the DT_AUDIT entry for the linker emulation code. */
3981 elf_dt_audit (abfd) = audit;
3982 }
3983
3984 /* If this is a dynamic object, we always link against the .dynsym
3985 symbol table, not the .symtab symbol table. The dynamic linker
3986 will only see the .dynsym symbol table, so there is no reason to
3987 look at .symtab for a dynamic object. */
3988
3989 if (! dynamic || elf_dynsymtab (abfd) == 0)
3990 hdr = &elf_tdata (abfd)->symtab_hdr;
3991 else
3992 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3993
3994 symcount = hdr->sh_size / bed->s->sizeof_sym;
3995
3996 /* The sh_info field of the symtab header tells us where the
3997 external symbols start. We don't care about the local symbols at
3998 this point. */
3999 if (elf_bad_symtab (abfd))
4000 {
4001 extsymcount = symcount;
4002 extsymoff = 0;
4003 }
4004 else
4005 {
4006 extsymcount = symcount - hdr->sh_info;
4007 extsymoff = hdr->sh_info;
4008 }
4009
4010 sym_hash = elf_sym_hashes (abfd);
4011 if (extsymcount != 0)
4012 {
4013 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4014 NULL, NULL, NULL);
4015 if (isymbuf == NULL)
4016 goto error_return;
4017
4018 if (sym_hash == NULL)
4019 {
4020 /* We store a pointer to the hash table entry for each
4021 external symbol. */
4022 amt = extsymcount;
4023 amt *= sizeof (struct elf_link_hash_entry *);
4024 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4025 if (sym_hash == NULL)
4026 goto error_free_sym;
4027 elf_sym_hashes (abfd) = sym_hash;
4028 }
4029 }
4030
4031 if (dynamic)
4032 {
4033 /* Read in any version definitions. */
4034 if (!_bfd_elf_slurp_version_tables (abfd,
4035 info->default_imported_symver))
4036 goto error_free_sym;
4037
4038 /* Read in the symbol versions, but don't bother to convert them
4039 to internal format. */
4040 if (elf_dynversym (abfd) != 0)
4041 {
4042 Elf_Internal_Shdr *versymhdr;
4043
4044 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4045 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4046 if (extversym == NULL)
4047 goto error_free_sym;
4048 amt = versymhdr->sh_size;
4049 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4050 || bfd_bread (extversym, amt, abfd) != amt)
4051 goto error_free_vers;
4052 }
4053 }
4054
4055 /* If we are loading an as-needed shared lib, save the symbol table
4056 state before we start adding symbols. If the lib turns out
4057 to be unneeded, restore the state. */
4058 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4059 {
4060 unsigned int i;
4061 size_t entsize;
4062
4063 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4064 {
4065 struct bfd_hash_entry *p;
4066 struct elf_link_hash_entry *h;
4067
4068 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4069 {
4070 h = (struct elf_link_hash_entry *) p;
4071 entsize += htab->root.table.entsize;
4072 if (h->root.type == bfd_link_hash_warning)
4073 entsize += htab->root.table.entsize;
4074 }
4075 }
4076
4077 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4078 old_tab = bfd_malloc (tabsize + entsize);
4079 if (old_tab == NULL)
4080 goto error_free_vers;
4081
4082 /* Remember the current objalloc pointer, so that all mem for
4083 symbols added can later be reclaimed. */
4084 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4085 if (alloc_mark == NULL)
4086 goto error_free_vers;
4087
4088 /* Make a special call to the linker "notice" function to
4089 tell it that we are about to handle an as-needed lib. */
4090 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4091 goto error_free_vers;
4092
4093 /* Clone the symbol table. Remember some pointers into the
4094 symbol table, and dynamic symbol count. */
4095 old_ent = (char *) old_tab + tabsize;
4096 memcpy (old_tab, htab->root.table.table, tabsize);
4097 old_undefs = htab->root.undefs;
4098 old_undefs_tail = htab->root.undefs_tail;
4099 old_table = htab->root.table.table;
4100 old_size = htab->root.table.size;
4101 old_count = htab->root.table.count;
4102 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4103 if (old_strtab == NULL)
4104 goto error_free_vers;
4105
4106 for (i = 0; i < htab->root.table.size; i++)
4107 {
4108 struct bfd_hash_entry *p;
4109 struct elf_link_hash_entry *h;
4110
4111 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4112 {
4113 memcpy (old_ent, p, htab->root.table.entsize);
4114 old_ent = (char *) old_ent + htab->root.table.entsize;
4115 h = (struct elf_link_hash_entry *) p;
4116 if (h->root.type == bfd_link_hash_warning)
4117 {
4118 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4119 old_ent = (char *) old_ent + htab->root.table.entsize;
4120 }
4121 }
4122 }
4123 }
4124
4125 weaks = NULL;
4126 ever = extversym != NULL ? extversym + extsymoff : NULL;
4127 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4128 isym < isymend;
4129 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4130 {
4131 int bind;
4132 bfd_vma value;
4133 asection *sec, *new_sec;
4134 flagword flags;
4135 const char *name;
4136 struct elf_link_hash_entry *h;
4137 struct elf_link_hash_entry *hi;
4138 bfd_boolean definition;
4139 bfd_boolean size_change_ok;
4140 bfd_boolean type_change_ok;
4141 bfd_boolean new_weakdef;
4142 bfd_boolean new_weak;
4143 bfd_boolean old_weak;
4144 bfd_boolean override;
4145 bfd_boolean common;
4146 bfd_boolean discarded;
4147 unsigned int old_alignment;
4148 bfd *old_bfd;
4149 bfd_boolean matched;
4150
4151 override = FALSE;
4152
4153 flags = BSF_NO_FLAGS;
4154 sec = NULL;
4155 value = isym->st_value;
4156 common = bed->common_definition (isym);
4157 discarded = FALSE;
4158
4159 bind = ELF_ST_BIND (isym->st_info);
4160 switch (bind)
4161 {
4162 case STB_LOCAL:
4163 /* This should be impossible, since ELF requires that all
4164 global symbols follow all local symbols, and that sh_info
4165 point to the first global symbol. Unfortunately, Irix 5
4166 screws this up. */
4167 continue;
4168
4169 case STB_GLOBAL:
4170 if (isym->st_shndx != SHN_UNDEF && !common)
4171 flags = BSF_GLOBAL;
4172 break;
4173
4174 case STB_WEAK:
4175 flags = BSF_WEAK;
4176 break;
4177
4178 case STB_GNU_UNIQUE:
4179 flags = BSF_GNU_UNIQUE;
4180 break;
4181
4182 default:
4183 /* Leave it up to the processor backend. */
4184 break;
4185 }
4186
4187 if (isym->st_shndx == SHN_UNDEF)
4188 sec = bfd_und_section_ptr;
4189 else if (isym->st_shndx == SHN_ABS)
4190 sec = bfd_abs_section_ptr;
4191 else if (isym->st_shndx == SHN_COMMON)
4192 {
4193 sec = bfd_com_section_ptr;
4194 /* What ELF calls the size we call the value. What ELF
4195 calls the value we call the alignment. */
4196 value = isym->st_size;
4197 }
4198 else
4199 {
4200 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4201 if (sec == NULL)
4202 sec = bfd_abs_section_ptr;
4203 else if (discarded_section (sec))
4204 {
4205 /* Symbols from discarded section are undefined. We keep
4206 its visibility. */
4207 sec = bfd_und_section_ptr;
4208 discarded = TRUE;
4209 isym->st_shndx = SHN_UNDEF;
4210 }
4211 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4212 value -= sec->vma;
4213 }
4214
4215 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4216 isym->st_name);
4217 if (name == NULL)
4218 goto error_free_vers;
4219
4220 if (isym->st_shndx == SHN_COMMON
4221 && (abfd->flags & BFD_PLUGIN) != 0)
4222 {
4223 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4224
4225 if (xc == NULL)
4226 {
4227 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4228 | SEC_EXCLUDE);
4229 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4230 if (xc == NULL)
4231 goto error_free_vers;
4232 }
4233 sec = xc;
4234 }
4235 else if (isym->st_shndx == SHN_COMMON
4236 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4237 && !bfd_link_relocatable (info))
4238 {
4239 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4240
4241 if (tcomm == NULL)
4242 {
4243 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4244 | SEC_LINKER_CREATED);
4245 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4246 if (tcomm == NULL)
4247 goto error_free_vers;
4248 }
4249 sec = tcomm;
4250 }
4251 else if (bed->elf_add_symbol_hook)
4252 {
4253 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4254 &sec, &value))
4255 goto error_free_vers;
4256
4257 /* The hook function sets the name to NULL if this symbol
4258 should be skipped for some reason. */
4259 if (name == NULL)
4260 continue;
4261 }
4262
4263 /* Sanity check that all possibilities were handled. */
4264 if (sec == NULL)
4265 {
4266 bfd_set_error (bfd_error_bad_value);
4267 goto error_free_vers;
4268 }
4269
4270 /* Silently discard TLS symbols from --just-syms. There's
4271 no way to combine a static TLS block with a new TLS block
4272 for this executable. */
4273 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4274 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4275 continue;
4276
4277 if (bfd_is_und_section (sec)
4278 || bfd_is_com_section (sec))
4279 definition = FALSE;
4280 else
4281 definition = TRUE;
4282
4283 size_change_ok = FALSE;
4284 type_change_ok = bed->type_change_ok;
4285 old_weak = FALSE;
4286 matched = FALSE;
4287 old_alignment = 0;
4288 old_bfd = NULL;
4289 new_sec = sec;
4290
4291 if (is_elf_hash_table (htab))
4292 {
4293 Elf_Internal_Versym iver;
4294 unsigned int vernum = 0;
4295 bfd_boolean skip;
4296
4297 if (ever == NULL)
4298 {
4299 if (info->default_imported_symver)
4300 /* Use the default symbol version created earlier. */
4301 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4302 else
4303 iver.vs_vers = 0;
4304 }
4305 else
4306 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4307
4308 vernum = iver.vs_vers & VERSYM_VERSION;
4309
4310 /* If this is a hidden symbol, or if it is not version
4311 1, we append the version name to the symbol name.
4312 However, we do not modify a non-hidden absolute symbol
4313 if it is not a function, because it might be the version
4314 symbol itself. FIXME: What if it isn't? */
4315 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4316 || (vernum > 1
4317 && (!bfd_is_abs_section (sec)
4318 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4319 {
4320 const char *verstr;
4321 size_t namelen, verlen, newlen;
4322 char *newname, *p;
4323
4324 if (isym->st_shndx != SHN_UNDEF)
4325 {
4326 if (vernum > elf_tdata (abfd)->cverdefs)
4327 verstr = NULL;
4328 else if (vernum > 1)
4329 verstr =
4330 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4331 else
4332 verstr = "";
4333
4334 if (verstr == NULL)
4335 {
4336 _bfd_error_handler
4337 /* xgettext:c-format */
4338 (_("%B: %s: invalid version %u (max %d)"),
4339 abfd, name, vernum,
4340 elf_tdata (abfd)->cverdefs);
4341 bfd_set_error (bfd_error_bad_value);
4342 goto error_free_vers;
4343 }
4344 }
4345 else
4346 {
4347 /* We cannot simply test for the number of
4348 entries in the VERNEED section since the
4349 numbers for the needed versions do not start
4350 at 0. */
4351 Elf_Internal_Verneed *t;
4352
4353 verstr = NULL;
4354 for (t = elf_tdata (abfd)->verref;
4355 t != NULL;
4356 t = t->vn_nextref)
4357 {
4358 Elf_Internal_Vernaux *a;
4359
4360 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4361 {
4362 if (a->vna_other == vernum)
4363 {
4364 verstr = a->vna_nodename;
4365 break;
4366 }
4367 }
4368 if (a != NULL)
4369 break;
4370 }
4371 if (verstr == NULL)
4372 {
4373 _bfd_error_handler
4374 /* xgettext:c-format */
4375 (_("%B: %s: invalid needed version %d"),
4376 abfd, name, vernum);
4377 bfd_set_error (bfd_error_bad_value);
4378 goto error_free_vers;
4379 }
4380 }
4381
4382 namelen = strlen (name);
4383 verlen = strlen (verstr);
4384 newlen = namelen + verlen + 2;
4385 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4386 && isym->st_shndx != SHN_UNDEF)
4387 ++newlen;
4388
4389 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4390 if (newname == NULL)
4391 goto error_free_vers;
4392 memcpy (newname, name, namelen);
4393 p = newname + namelen;
4394 *p++ = ELF_VER_CHR;
4395 /* If this is a defined non-hidden version symbol,
4396 we add another @ to the name. This indicates the
4397 default version of the symbol. */
4398 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4399 && isym->st_shndx != SHN_UNDEF)
4400 *p++ = ELF_VER_CHR;
4401 memcpy (p, verstr, verlen + 1);
4402
4403 name = newname;
4404 }
4405
4406 /* If this symbol has default visibility and the user has
4407 requested we not re-export it, then mark it as hidden. */
4408 if (!bfd_is_und_section (sec)
4409 && !dynamic
4410 && abfd->no_export
4411 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4412 isym->st_other = (STV_HIDDEN
4413 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4414
4415 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4416 sym_hash, &old_bfd, &old_weak,
4417 &old_alignment, &skip, &override,
4418 &type_change_ok, &size_change_ok,
4419 &matched))
4420 goto error_free_vers;
4421
4422 if (skip)
4423 continue;
4424
4425 /* Override a definition only if the new symbol matches the
4426 existing one. */
4427 if (override && matched)
4428 definition = FALSE;
4429
4430 h = *sym_hash;
4431 while (h->root.type == bfd_link_hash_indirect
4432 || h->root.type == bfd_link_hash_warning)
4433 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4434
4435 if (elf_tdata (abfd)->verdef != NULL
4436 && vernum > 1
4437 && definition)
4438 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4439 }
4440
4441 if (! (_bfd_generic_link_add_one_symbol
4442 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4443 (struct bfd_link_hash_entry **) sym_hash)))
4444 goto error_free_vers;
4445
4446 if ((flags & BSF_GNU_UNIQUE)
4447 && (abfd->flags & DYNAMIC) == 0
4448 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4449 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique;
4450
4451 h = *sym_hash;
4452 /* We need to make sure that indirect symbol dynamic flags are
4453 updated. */
4454 hi = h;
4455 while (h->root.type == bfd_link_hash_indirect
4456 || h->root.type == bfd_link_hash_warning)
4457 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4458
4459 /* Setting the index to -3 tells elf_link_output_extsym that
4460 this symbol is defined in a discarded section. */
4461 if (discarded)
4462 h->indx = -3;
4463
4464 *sym_hash = h;
4465
4466 new_weak = (flags & BSF_WEAK) != 0;
4467 new_weakdef = FALSE;
4468 if (dynamic
4469 && definition
4470 && new_weak
4471 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4472 && is_elf_hash_table (htab)
4473 && h->u.weakdef == NULL)
4474 {
4475 /* Keep a list of all weak defined non function symbols from
4476 a dynamic object, using the weakdef field. Later in this
4477 function we will set the weakdef field to the correct
4478 value. We only put non-function symbols from dynamic
4479 objects on this list, because that happens to be the only
4480 time we need to know the normal symbol corresponding to a
4481 weak symbol, and the information is time consuming to
4482 figure out. If the weakdef field is not already NULL,
4483 then this symbol was already defined by some previous
4484 dynamic object, and we will be using that previous
4485 definition anyhow. */
4486
4487 h->u.weakdef = weaks;
4488 weaks = h;
4489 new_weakdef = TRUE;
4490 }
4491
4492 /* Set the alignment of a common symbol. */
4493 if ((common || bfd_is_com_section (sec))
4494 && h->root.type == bfd_link_hash_common)
4495 {
4496 unsigned int align;
4497
4498 if (common)
4499 align = bfd_log2 (isym->st_value);
4500 else
4501 {
4502 /* The new symbol is a common symbol in a shared object.
4503 We need to get the alignment from the section. */
4504 align = new_sec->alignment_power;
4505 }
4506 if (align > old_alignment)
4507 h->root.u.c.p->alignment_power = align;
4508 else
4509 h->root.u.c.p->alignment_power = old_alignment;
4510 }
4511
4512 if (is_elf_hash_table (htab))
4513 {
4514 /* Set a flag in the hash table entry indicating the type of
4515 reference or definition we just found. A dynamic symbol
4516 is one which is referenced or defined by both a regular
4517 object and a shared object. */
4518 bfd_boolean dynsym = FALSE;
4519
4520 /* Plugin symbols aren't normal. Don't set def_regular or
4521 ref_regular for them, or make them dynamic. */
4522 if ((abfd->flags & BFD_PLUGIN) != 0)
4523 ;
4524 else if (! dynamic)
4525 {
4526 if (! definition)
4527 {
4528 h->ref_regular = 1;
4529 if (bind != STB_WEAK)
4530 h->ref_regular_nonweak = 1;
4531 }
4532 else
4533 {
4534 h->def_regular = 1;
4535 if (h->def_dynamic)
4536 {
4537 h->def_dynamic = 0;
4538 h->ref_dynamic = 1;
4539 }
4540 }
4541
4542 /* If the indirect symbol has been forced local, don't
4543 make the real symbol dynamic. */
4544 if ((h == hi || !hi->forced_local)
4545 && (bfd_link_dll (info)
4546 || h->def_dynamic
4547 || h->ref_dynamic))
4548 dynsym = TRUE;
4549 }
4550 else
4551 {
4552 if (! definition)
4553 {
4554 h->ref_dynamic = 1;
4555 hi->ref_dynamic = 1;
4556 }
4557 else
4558 {
4559 h->def_dynamic = 1;
4560 hi->def_dynamic = 1;
4561 }
4562
4563 /* If the indirect symbol has been forced local, don't
4564 make the real symbol dynamic. */
4565 if ((h == hi || !hi->forced_local)
4566 && (h->def_regular
4567 || h->ref_regular
4568 || (h->u.weakdef != NULL
4569 && ! new_weakdef
4570 && h->u.weakdef->dynindx != -1)))
4571 dynsym = TRUE;
4572 }
4573
4574 /* Check to see if we need to add an indirect symbol for
4575 the default name. */
4576 if (definition
4577 || (!override && h->root.type == bfd_link_hash_common))
4578 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4579 sec, value, &old_bfd, &dynsym))
4580 goto error_free_vers;
4581
4582 /* Check the alignment when a common symbol is involved. This
4583 can change when a common symbol is overridden by a normal
4584 definition or a common symbol is ignored due to the old
4585 normal definition. We need to make sure the maximum
4586 alignment is maintained. */
4587 if ((old_alignment || common)
4588 && h->root.type != bfd_link_hash_common)
4589 {
4590 unsigned int common_align;
4591 unsigned int normal_align;
4592 unsigned int symbol_align;
4593 bfd *normal_bfd;
4594 bfd *common_bfd;
4595
4596 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4597 || h->root.type == bfd_link_hash_defweak);
4598
4599 symbol_align = ffs (h->root.u.def.value) - 1;
4600 if (h->root.u.def.section->owner != NULL
4601 && (h->root.u.def.section->owner->flags
4602 & (DYNAMIC | BFD_PLUGIN)) == 0)
4603 {
4604 normal_align = h->root.u.def.section->alignment_power;
4605 if (normal_align > symbol_align)
4606 normal_align = symbol_align;
4607 }
4608 else
4609 normal_align = symbol_align;
4610
4611 if (old_alignment)
4612 {
4613 common_align = old_alignment;
4614 common_bfd = old_bfd;
4615 normal_bfd = abfd;
4616 }
4617 else
4618 {
4619 common_align = bfd_log2 (isym->st_value);
4620 common_bfd = abfd;
4621 normal_bfd = old_bfd;
4622 }
4623
4624 if (normal_align < common_align)
4625 {
4626 /* PR binutils/2735 */
4627 if (normal_bfd == NULL)
4628 _bfd_error_handler
4629 /* xgettext:c-format */
4630 (_("Warning: alignment %u of common symbol `%s' in %B is"
4631 " greater than the alignment (%u) of its section %A"),
4632 common_bfd, h->root.u.def.section,
4633 1 << common_align, name, 1 << normal_align);
4634 else
4635 _bfd_error_handler
4636 /* xgettext:c-format */
4637 (_("Warning: alignment %u of symbol `%s' in %B"
4638 " is smaller than %u in %B"),
4639 normal_bfd, common_bfd,
4640 1 << normal_align, name, 1 << common_align);
4641 }
4642 }
4643
4644 /* Remember the symbol size if it isn't undefined. */
4645 if (isym->st_size != 0
4646 && isym->st_shndx != SHN_UNDEF
4647 && (definition || h->size == 0))
4648 {
4649 if (h->size != 0
4650 && h->size != isym->st_size
4651 && ! size_change_ok)
4652 _bfd_error_handler
4653 /* xgettext:c-format */
4654 (_("Warning: size of symbol `%s' changed"
4655 " from %lu in %B to %lu in %B"),
4656 old_bfd, abfd,
4657 name, (unsigned long) h->size,
4658 (unsigned long) isym->st_size);
4659
4660 h->size = isym->st_size;
4661 }
4662
4663 /* If this is a common symbol, then we always want H->SIZE
4664 to be the size of the common symbol. The code just above
4665 won't fix the size if a common symbol becomes larger. We
4666 don't warn about a size change here, because that is
4667 covered by --warn-common. Allow changes between different
4668 function types. */
4669 if (h->root.type == bfd_link_hash_common)
4670 h->size = h->root.u.c.size;
4671
4672 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4673 && ((definition && !new_weak)
4674 || (old_weak && h->root.type == bfd_link_hash_common)
4675 || h->type == STT_NOTYPE))
4676 {
4677 unsigned int type = ELF_ST_TYPE (isym->st_info);
4678
4679 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4680 symbol. */
4681 if (type == STT_GNU_IFUNC
4682 && (abfd->flags & DYNAMIC) != 0)
4683 type = STT_FUNC;
4684
4685 if (h->type != type)
4686 {
4687 if (h->type != STT_NOTYPE && ! type_change_ok)
4688 /* xgettext:c-format */
4689 _bfd_error_handler
4690 (_("Warning: type of symbol `%s' changed"
4691 " from %d to %d in %B"),
4692 abfd, name, h->type, type);
4693
4694 h->type = type;
4695 }
4696 }
4697
4698 /* Merge st_other field. */
4699 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4700
4701 /* We don't want to make debug symbol dynamic. */
4702 if (definition
4703 && (sec->flags & SEC_DEBUGGING)
4704 && !bfd_link_relocatable (info))
4705 dynsym = FALSE;
4706
4707 /* Nor should we make plugin symbols dynamic. */
4708 if ((abfd->flags & BFD_PLUGIN) != 0)
4709 dynsym = FALSE;
4710
4711 if (definition)
4712 {
4713 h->target_internal = isym->st_target_internal;
4714 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4715 }
4716
4717 if (definition && !dynamic)
4718 {
4719 char *p = strchr (name, ELF_VER_CHR);
4720 if (p != NULL && p[1] != ELF_VER_CHR)
4721 {
4722 /* Queue non-default versions so that .symver x, x@FOO
4723 aliases can be checked. */
4724 if (!nondeflt_vers)
4725 {
4726 amt = ((isymend - isym + 1)
4727 * sizeof (struct elf_link_hash_entry *));
4728 nondeflt_vers
4729 = (struct elf_link_hash_entry **) bfd_malloc (amt);
4730 if (!nondeflt_vers)
4731 goto error_free_vers;
4732 }
4733 nondeflt_vers[nondeflt_vers_cnt++] = h;
4734 }
4735 }
4736
4737 if (dynsym && h->dynindx == -1)
4738 {
4739 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4740 goto error_free_vers;
4741 if (h->u.weakdef != NULL
4742 && ! new_weakdef
4743 && h->u.weakdef->dynindx == -1)
4744 {
4745 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4746 goto error_free_vers;
4747 }
4748 }
4749 else if (h->dynindx != -1)
4750 /* If the symbol already has a dynamic index, but
4751 visibility says it should not be visible, turn it into
4752 a local symbol. */
4753 switch (ELF_ST_VISIBILITY (h->other))
4754 {
4755 case STV_INTERNAL:
4756 case STV_HIDDEN:
4757 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4758 dynsym = FALSE;
4759 break;
4760 }
4761
4762 /* Don't add DT_NEEDED for references from the dummy bfd nor
4763 for unmatched symbol. */
4764 if (!add_needed
4765 && matched
4766 && definition
4767 && ((dynsym
4768 && h->ref_regular_nonweak
4769 && (old_bfd == NULL
4770 || (old_bfd->flags & BFD_PLUGIN) == 0))
4771 || (h->ref_dynamic_nonweak
4772 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4773 && !on_needed_list (elf_dt_name (abfd),
4774 htab->needed, NULL))))
4775 {
4776 int ret;
4777 const char *soname = elf_dt_name (abfd);
4778
4779 info->callbacks->minfo ("%!", soname, old_bfd,
4780 h->root.root.string);
4781
4782 /* A symbol from a library loaded via DT_NEEDED of some
4783 other library is referenced by a regular object.
4784 Add a DT_NEEDED entry for it. Issue an error if
4785 --no-add-needed is used and the reference was not
4786 a weak one. */
4787 if (old_bfd != NULL
4788 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4789 {
4790 _bfd_error_handler
4791 /* xgettext:c-format */
4792 (_("%B: undefined reference to symbol '%s'"),
4793 old_bfd, name);
4794 bfd_set_error (bfd_error_missing_dso);
4795 goto error_free_vers;
4796 }
4797
4798 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4799 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
4800
4801 add_needed = TRUE;
4802 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4803 if (ret < 0)
4804 goto error_free_vers;
4805
4806 BFD_ASSERT (ret == 0);
4807 }
4808 }
4809 }
4810
4811 if (extversym != NULL)
4812 {
4813 free (extversym);
4814 extversym = NULL;
4815 }
4816
4817 if (isymbuf != NULL)
4818 {
4819 free (isymbuf);
4820 isymbuf = NULL;
4821 }
4822
4823 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4824 {
4825 unsigned int i;
4826
4827 /* Restore the symbol table. */
4828 old_ent = (char *) old_tab + tabsize;
4829 memset (elf_sym_hashes (abfd), 0,
4830 extsymcount * sizeof (struct elf_link_hash_entry *));
4831 htab->root.table.table = old_table;
4832 htab->root.table.size = old_size;
4833 htab->root.table.count = old_count;
4834 memcpy (htab->root.table.table, old_tab, tabsize);
4835 htab->root.undefs = old_undefs;
4836 htab->root.undefs_tail = old_undefs_tail;
4837 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
4838 free (old_strtab);
4839 old_strtab = NULL;
4840 for (i = 0; i < htab->root.table.size; i++)
4841 {
4842 struct bfd_hash_entry *p;
4843 struct elf_link_hash_entry *h;
4844 bfd_size_type size;
4845 unsigned int alignment_power;
4846
4847 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4848 {
4849 h = (struct elf_link_hash_entry *) p;
4850 if (h->root.type == bfd_link_hash_warning)
4851 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4852
4853 /* Preserve the maximum alignment and size for common
4854 symbols even if this dynamic lib isn't on DT_NEEDED
4855 since it can still be loaded at run time by another
4856 dynamic lib. */
4857 if (h->root.type == bfd_link_hash_common)
4858 {
4859 size = h->root.u.c.size;
4860 alignment_power = h->root.u.c.p->alignment_power;
4861 }
4862 else
4863 {
4864 size = 0;
4865 alignment_power = 0;
4866 }
4867 memcpy (p, old_ent, htab->root.table.entsize);
4868 old_ent = (char *) old_ent + htab->root.table.entsize;
4869 h = (struct elf_link_hash_entry *) p;
4870 if (h->root.type == bfd_link_hash_warning)
4871 {
4872 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4873 old_ent = (char *) old_ent + htab->root.table.entsize;
4874 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4875 }
4876 if (h->root.type == bfd_link_hash_common)
4877 {
4878 if (size > h->root.u.c.size)
4879 h->root.u.c.size = size;
4880 if (alignment_power > h->root.u.c.p->alignment_power)
4881 h->root.u.c.p->alignment_power = alignment_power;
4882 }
4883 }
4884 }
4885
4886 /* Make a special call to the linker "notice" function to
4887 tell it that symbols added for crefs may need to be removed. */
4888 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
4889 goto error_free_vers;
4890
4891 free (old_tab);
4892 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4893 alloc_mark);
4894 if (nondeflt_vers != NULL)
4895 free (nondeflt_vers);
4896 return TRUE;
4897 }
4898
4899 if (old_tab != NULL)
4900 {
4901 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
4902 goto error_free_vers;
4903 free (old_tab);
4904 old_tab = NULL;
4905 }
4906
4907 /* Now that all the symbols from this input file are created, if
4908 not performing a relocatable link, handle .symver foo, foo@BAR
4909 such that any relocs against foo become foo@BAR. */
4910 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4911 {
4912 size_t cnt, symidx;
4913
4914 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4915 {
4916 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4917 char *shortname, *p;
4918
4919 p = strchr (h->root.root.string, ELF_VER_CHR);
4920 if (p == NULL
4921 || (h->root.type != bfd_link_hash_defined
4922 && h->root.type != bfd_link_hash_defweak))
4923 continue;
4924
4925 amt = p - h->root.root.string;
4926 shortname = (char *) bfd_malloc (amt + 1);
4927 if (!shortname)
4928 goto error_free_vers;
4929 memcpy (shortname, h->root.root.string, amt);
4930 shortname[amt] = '\0';
4931
4932 hi = (struct elf_link_hash_entry *)
4933 bfd_link_hash_lookup (&htab->root, shortname,
4934 FALSE, FALSE, FALSE);
4935 if (hi != NULL
4936 && hi->root.type == h->root.type
4937 && hi->root.u.def.value == h->root.u.def.value
4938 && hi->root.u.def.section == h->root.u.def.section)
4939 {
4940 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4941 hi->root.type = bfd_link_hash_indirect;
4942 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4943 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4944 sym_hash = elf_sym_hashes (abfd);
4945 if (sym_hash)
4946 for (symidx = 0; symidx < extsymcount; ++symidx)
4947 if (sym_hash[symidx] == hi)
4948 {
4949 sym_hash[symidx] = h;
4950 break;
4951 }
4952 }
4953 free (shortname);
4954 }
4955 free (nondeflt_vers);
4956 nondeflt_vers = NULL;
4957 }
4958
4959 /* Now set the weakdefs field correctly for all the weak defined
4960 symbols we found. The only way to do this is to search all the
4961 symbols. Since we only need the information for non functions in
4962 dynamic objects, that's the only time we actually put anything on
4963 the list WEAKS. We need this information so that if a regular
4964 object refers to a symbol defined weakly in a dynamic object, the
4965 real symbol in the dynamic object is also put in the dynamic
4966 symbols; we also must arrange for both symbols to point to the
4967 same memory location. We could handle the general case of symbol
4968 aliasing, but a general symbol alias can only be generated in
4969 assembler code, handling it correctly would be very time
4970 consuming, and other ELF linkers don't handle general aliasing
4971 either. */
4972 if (weaks != NULL)
4973 {
4974 struct elf_link_hash_entry **hpp;
4975 struct elf_link_hash_entry **hppend;
4976 struct elf_link_hash_entry **sorted_sym_hash;
4977 struct elf_link_hash_entry *h;
4978 size_t sym_count;
4979
4980 /* Since we have to search the whole symbol list for each weak
4981 defined symbol, search time for N weak defined symbols will be
4982 O(N^2). Binary search will cut it down to O(NlogN). */
4983 amt = extsymcount;
4984 amt *= sizeof (struct elf_link_hash_entry *);
4985 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4986 if (sorted_sym_hash == NULL)
4987 goto error_return;
4988 sym_hash = sorted_sym_hash;
4989 hpp = elf_sym_hashes (abfd);
4990 hppend = hpp + extsymcount;
4991 sym_count = 0;
4992 for (; hpp < hppend; hpp++)
4993 {
4994 h = *hpp;
4995 if (h != NULL
4996 && h->root.type == bfd_link_hash_defined
4997 && !bed->is_function_type (h->type))
4998 {
4999 *sym_hash = h;
5000 sym_hash++;
5001 sym_count++;
5002 }
5003 }
5004
5005 qsort (sorted_sym_hash, sym_count,
5006 sizeof (struct elf_link_hash_entry *),
5007 elf_sort_symbol);
5008
5009 while (weaks != NULL)
5010 {
5011 struct elf_link_hash_entry *hlook;
5012 asection *slook;
5013 bfd_vma vlook;
5014 size_t i, j, idx = 0;
5015
5016 hlook = weaks;
5017 weaks = hlook->u.weakdef;
5018 hlook->u.weakdef = NULL;
5019
5020 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
5021 || hlook->root.type == bfd_link_hash_defweak
5022 || hlook->root.type == bfd_link_hash_common
5023 || hlook->root.type == bfd_link_hash_indirect);
5024 slook = hlook->root.u.def.section;
5025 vlook = hlook->root.u.def.value;
5026
5027 i = 0;
5028 j = sym_count;
5029 while (i != j)
5030 {
5031 bfd_signed_vma vdiff;
5032 idx = (i + j) / 2;
5033 h = sorted_sym_hash[idx];
5034 vdiff = vlook - h->root.u.def.value;
5035 if (vdiff < 0)
5036 j = idx;
5037 else if (vdiff > 0)
5038 i = idx + 1;
5039 else
5040 {
5041 int sdiff = slook->id - h->root.u.def.section->id;
5042 if (sdiff < 0)
5043 j = idx;
5044 else if (sdiff > 0)
5045 i = idx + 1;
5046 else
5047 break;
5048 }
5049 }
5050
5051 /* We didn't find a value/section match. */
5052 if (i == j)
5053 continue;
5054
5055 /* With multiple aliases, or when the weak symbol is already
5056 strongly defined, we have multiple matching symbols and
5057 the binary search above may land on any of them. Step
5058 one past the matching symbol(s). */
5059 while (++idx != j)
5060 {
5061 h = sorted_sym_hash[idx];
5062 if (h->root.u.def.section != slook
5063 || h->root.u.def.value != vlook)
5064 break;
5065 }
5066
5067 /* Now look back over the aliases. Since we sorted by size
5068 as well as value and section, we'll choose the one with
5069 the largest size. */
5070 while (idx-- != i)
5071 {
5072 h = sorted_sym_hash[idx];
5073
5074 /* Stop if value or section doesn't match. */
5075 if (h->root.u.def.section != slook
5076 || h->root.u.def.value != vlook)
5077 break;
5078 else if (h != hlook)
5079 {
5080 hlook->u.weakdef = h;
5081
5082 /* If the weak definition is in the list of dynamic
5083 symbols, make sure the real definition is put
5084 there as well. */
5085 if (hlook->dynindx != -1 && h->dynindx == -1)
5086 {
5087 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5088 {
5089 err_free_sym_hash:
5090 free (sorted_sym_hash);
5091 goto error_return;
5092 }
5093 }
5094
5095 /* If the real definition is in the list of dynamic
5096 symbols, make sure the weak definition is put
5097 there as well. If we don't do this, then the
5098 dynamic loader might not merge the entries for the
5099 real definition and the weak definition. */
5100 if (h->dynindx != -1 && hlook->dynindx == -1)
5101 {
5102 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5103 goto err_free_sym_hash;
5104 }
5105 break;
5106 }
5107 }
5108 }
5109
5110 free (sorted_sym_hash);
5111 }
5112
5113 if (bed->check_directives
5114 && !(*bed->check_directives) (abfd, info))
5115 return FALSE;
5116
5117 if (!info->check_relocs_after_open_input
5118 && !_bfd_elf_link_check_relocs (abfd, info))
5119 return FALSE;
5120
5121 /* If this is a non-traditional link, try to optimize the handling
5122 of the .stab/.stabstr sections. */
5123 if (! dynamic
5124 && ! info->traditional_format
5125 && is_elf_hash_table (htab)
5126 && (info->strip != strip_all && info->strip != strip_debugger))
5127 {
5128 asection *stabstr;
5129
5130 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5131 if (stabstr != NULL)
5132 {
5133 bfd_size_type string_offset = 0;
5134 asection *stab;
5135
5136 for (stab = abfd->sections; stab; stab = stab->next)
5137 if (CONST_STRNEQ (stab->name, ".stab")
5138 && (!stab->name[5] ||
5139 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5140 && (stab->flags & SEC_MERGE) == 0
5141 && !bfd_is_abs_section (stab->output_section))
5142 {
5143 struct bfd_elf_section_data *secdata;
5144
5145 secdata = elf_section_data (stab);
5146 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5147 stabstr, &secdata->sec_info,
5148 &string_offset))
5149 goto error_return;
5150 if (secdata->sec_info)
5151 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5152 }
5153 }
5154 }
5155
5156 if (is_elf_hash_table (htab) && add_needed)
5157 {
5158 /* Add this bfd to the loaded list. */
5159 struct elf_link_loaded_list *n;
5160
5161 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5162 if (n == NULL)
5163 goto error_return;
5164 n->abfd = abfd;
5165 n->next = htab->loaded;
5166 htab->loaded = n;
5167 }
5168
5169 return TRUE;
5170
5171 error_free_vers:
5172 if (old_tab != NULL)
5173 free (old_tab);
5174 if (old_strtab != NULL)
5175 free (old_strtab);
5176 if (nondeflt_vers != NULL)
5177 free (nondeflt_vers);
5178 if (extversym != NULL)
5179 free (extversym);
5180 error_free_sym:
5181 if (isymbuf != NULL)
5182 free (isymbuf);
5183 error_return:
5184 return FALSE;
5185 }
5186
5187 /* Return the linker hash table entry of a symbol that might be
5188 satisfied by an archive symbol. Return -1 on error. */
5189
5190 struct elf_link_hash_entry *
5191 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5192 struct bfd_link_info *info,
5193 const char *name)
5194 {
5195 struct elf_link_hash_entry *h;
5196 char *p, *copy;
5197 size_t len, first;
5198
5199 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5200 if (h != NULL)
5201 return h;
5202
5203 /* If this is a default version (the name contains @@), look up the
5204 symbol again with only one `@' as well as without the version.
5205 The effect is that references to the symbol with and without the
5206 version will be matched by the default symbol in the archive. */
5207
5208 p = strchr (name, ELF_VER_CHR);
5209 if (p == NULL || p[1] != ELF_VER_CHR)
5210 return h;
5211
5212 /* First check with only one `@'. */
5213 len = strlen (name);
5214 copy = (char *) bfd_alloc (abfd, len);
5215 if (copy == NULL)
5216 return (struct elf_link_hash_entry *) 0 - 1;
5217
5218 first = p - name + 1;
5219 memcpy (copy, name, first);
5220 memcpy (copy + first, name + first + 1, len - first);
5221
5222 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5223 if (h == NULL)
5224 {
5225 /* We also need to check references to the symbol without the
5226 version. */
5227 copy[first - 1] = '\0';
5228 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5229 FALSE, FALSE, TRUE);
5230 }
5231
5232 bfd_release (abfd, copy);
5233 return h;
5234 }
5235
5236 /* Add symbols from an ELF archive file to the linker hash table. We
5237 don't use _bfd_generic_link_add_archive_symbols because we need to
5238 handle versioned symbols.
5239
5240 Fortunately, ELF archive handling is simpler than that done by
5241 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5242 oddities. In ELF, if we find a symbol in the archive map, and the
5243 symbol is currently undefined, we know that we must pull in that
5244 object file.
5245
5246 Unfortunately, we do have to make multiple passes over the symbol
5247 table until nothing further is resolved. */
5248
5249 static bfd_boolean
5250 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5251 {
5252 symindex c;
5253 unsigned char *included = NULL;
5254 carsym *symdefs;
5255 bfd_boolean loop;
5256 bfd_size_type amt;
5257 const struct elf_backend_data *bed;
5258 struct elf_link_hash_entry * (*archive_symbol_lookup)
5259 (bfd *, struct bfd_link_info *, const char *);
5260
5261 if (! bfd_has_map (abfd))
5262 {
5263 /* An empty archive is a special case. */
5264 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5265 return TRUE;
5266 bfd_set_error (bfd_error_no_armap);
5267 return FALSE;
5268 }
5269
5270 /* Keep track of all symbols we know to be already defined, and all
5271 files we know to be already included. This is to speed up the
5272 second and subsequent passes. */
5273 c = bfd_ardata (abfd)->symdef_count;
5274 if (c == 0)
5275 return TRUE;
5276 amt = c;
5277 amt *= sizeof (*included);
5278 included = (unsigned char *) bfd_zmalloc (amt);
5279 if (included == NULL)
5280 return FALSE;
5281
5282 symdefs = bfd_ardata (abfd)->symdefs;
5283 bed = get_elf_backend_data (abfd);
5284 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5285
5286 do
5287 {
5288 file_ptr last;
5289 symindex i;
5290 carsym *symdef;
5291 carsym *symdefend;
5292
5293 loop = FALSE;
5294 last = -1;
5295
5296 symdef = symdefs;
5297 symdefend = symdef + c;
5298 for (i = 0; symdef < symdefend; symdef++, i++)
5299 {
5300 struct elf_link_hash_entry *h;
5301 bfd *element;
5302 struct bfd_link_hash_entry *undefs_tail;
5303 symindex mark;
5304
5305 if (included[i])
5306 continue;
5307 if (symdef->file_offset == last)
5308 {
5309 included[i] = TRUE;
5310 continue;
5311 }
5312
5313 h = archive_symbol_lookup (abfd, info, symdef->name);
5314 if (h == (struct elf_link_hash_entry *) 0 - 1)
5315 goto error_return;
5316
5317 if (h == NULL)
5318 continue;
5319
5320 if (h->root.type == bfd_link_hash_common)
5321 {
5322 /* We currently have a common symbol. The archive map contains
5323 a reference to this symbol, so we may want to include it. We
5324 only want to include it however, if this archive element
5325 contains a definition of the symbol, not just another common
5326 declaration of it.
5327
5328 Unfortunately some archivers (including GNU ar) will put
5329 declarations of common symbols into their archive maps, as
5330 well as real definitions, so we cannot just go by the archive
5331 map alone. Instead we must read in the element's symbol
5332 table and check that to see what kind of symbol definition
5333 this is. */
5334 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5335 continue;
5336 }
5337 else if (h->root.type != bfd_link_hash_undefined)
5338 {
5339 if (h->root.type != bfd_link_hash_undefweak)
5340 /* Symbol must be defined. Don't check it again. */
5341 included[i] = TRUE;
5342 continue;
5343 }
5344
5345 /* We need to include this archive member. */
5346 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5347 if (element == NULL)
5348 goto error_return;
5349
5350 if (! bfd_check_format (element, bfd_object))
5351 goto error_return;
5352
5353 undefs_tail = info->hash->undefs_tail;
5354
5355 if (!(*info->callbacks
5356 ->add_archive_element) (info, element, symdef->name, &element))
5357 continue;
5358 if (!bfd_link_add_symbols (element, info))
5359 goto error_return;
5360
5361 /* If there are any new undefined symbols, we need to make
5362 another pass through the archive in order to see whether
5363 they can be defined. FIXME: This isn't perfect, because
5364 common symbols wind up on undefs_tail and because an
5365 undefined symbol which is defined later on in this pass
5366 does not require another pass. This isn't a bug, but it
5367 does make the code less efficient than it could be. */
5368 if (undefs_tail != info->hash->undefs_tail)
5369 loop = TRUE;
5370
5371 /* Look backward to mark all symbols from this object file
5372 which we have already seen in this pass. */
5373 mark = i;
5374 do
5375 {
5376 included[mark] = TRUE;
5377 if (mark == 0)
5378 break;
5379 --mark;
5380 }
5381 while (symdefs[mark].file_offset == symdef->file_offset);
5382
5383 /* We mark subsequent symbols from this object file as we go
5384 on through the loop. */
5385 last = symdef->file_offset;
5386 }
5387 }
5388 while (loop);
5389
5390 free (included);
5391
5392 return TRUE;
5393
5394 error_return:
5395 if (included != NULL)
5396 free (included);
5397 return FALSE;
5398 }
5399
5400 /* Given an ELF BFD, add symbols to the global hash table as
5401 appropriate. */
5402
5403 bfd_boolean
5404 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5405 {
5406 switch (bfd_get_format (abfd))
5407 {
5408 case bfd_object:
5409 return elf_link_add_object_symbols (abfd, info);
5410 case bfd_archive:
5411 return elf_link_add_archive_symbols (abfd, info);
5412 default:
5413 bfd_set_error (bfd_error_wrong_format);
5414 return FALSE;
5415 }
5416 }
5417 \f
5418 struct hash_codes_info
5419 {
5420 unsigned long *hashcodes;
5421 bfd_boolean error;
5422 };
5423
5424 /* This function will be called though elf_link_hash_traverse to store
5425 all hash value of the exported symbols in an array. */
5426
5427 static bfd_boolean
5428 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5429 {
5430 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5431 const char *name;
5432 unsigned long ha;
5433 char *alc = NULL;
5434
5435 /* Ignore indirect symbols. These are added by the versioning code. */
5436 if (h->dynindx == -1)
5437 return TRUE;
5438
5439 name = h->root.root.string;
5440 if (h->versioned >= versioned)
5441 {
5442 char *p = strchr (name, ELF_VER_CHR);
5443 if (p != NULL)
5444 {
5445 alc = (char *) bfd_malloc (p - name + 1);
5446 if (alc == NULL)
5447 {
5448 inf->error = TRUE;
5449 return FALSE;
5450 }
5451 memcpy (alc, name, p - name);
5452 alc[p - name] = '\0';
5453 name = alc;
5454 }
5455 }
5456
5457 /* Compute the hash value. */
5458 ha = bfd_elf_hash (name);
5459
5460 /* Store the found hash value in the array given as the argument. */
5461 *(inf->hashcodes)++ = ha;
5462
5463 /* And store it in the struct so that we can put it in the hash table
5464 later. */
5465 h->u.elf_hash_value = ha;
5466
5467 if (alc != NULL)
5468 free (alc);
5469
5470 return TRUE;
5471 }
5472
5473 struct collect_gnu_hash_codes
5474 {
5475 bfd *output_bfd;
5476 const struct elf_backend_data *bed;
5477 unsigned long int nsyms;
5478 unsigned long int maskbits;
5479 unsigned long int *hashcodes;
5480 unsigned long int *hashval;
5481 unsigned long int *indx;
5482 unsigned long int *counts;
5483 bfd_vma *bitmask;
5484 bfd_byte *contents;
5485 long int min_dynindx;
5486 unsigned long int bucketcount;
5487 unsigned long int symindx;
5488 long int local_indx;
5489 long int shift1, shift2;
5490 unsigned long int mask;
5491 bfd_boolean error;
5492 };
5493
5494 /* This function will be called though elf_link_hash_traverse to store
5495 all hash value of the exported symbols in an array. */
5496
5497 static bfd_boolean
5498 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5499 {
5500 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5501 const char *name;
5502 unsigned long ha;
5503 char *alc = NULL;
5504
5505 /* Ignore indirect symbols. These are added by the versioning code. */
5506 if (h->dynindx == -1)
5507 return TRUE;
5508
5509 /* Ignore also local symbols and undefined symbols. */
5510 if (! (*s->bed->elf_hash_symbol) (h))
5511 return TRUE;
5512
5513 name = h->root.root.string;
5514 if (h->versioned >= versioned)
5515 {
5516 char *p = strchr (name, ELF_VER_CHR);
5517 if (p != NULL)
5518 {
5519 alc = (char *) bfd_malloc (p - name + 1);
5520 if (alc == NULL)
5521 {
5522 s->error = TRUE;
5523 return FALSE;
5524 }
5525 memcpy (alc, name, p - name);
5526 alc[p - name] = '\0';
5527 name = alc;
5528 }
5529 }
5530
5531 /* Compute the hash value. */
5532 ha = bfd_elf_gnu_hash (name);
5533
5534 /* Store the found hash value in the array for compute_bucket_count,
5535 and also for .dynsym reordering purposes. */
5536 s->hashcodes[s->nsyms] = ha;
5537 s->hashval[h->dynindx] = ha;
5538 ++s->nsyms;
5539 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5540 s->min_dynindx = h->dynindx;
5541
5542 if (alc != NULL)
5543 free (alc);
5544
5545 return TRUE;
5546 }
5547
5548 /* This function will be called though elf_link_hash_traverse to do
5549 final dynaminc symbol renumbering. */
5550
5551 static bfd_boolean
5552 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5553 {
5554 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5555 unsigned long int bucket;
5556 unsigned long int val;
5557
5558 /* Ignore indirect symbols. */
5559 if (h->dynindx == -1)
5560 return TRUE;
5561
5562 /* Ignore also local symbols and undefined symbols. */
5563 if (! (*s->bed->elf_hash_symbol) (h))
5564 {
5565 if (h->dynindx >= s->min_dynindx)
5566 h->dynindx = s->local_indx++;
5567 return TRUE;
5568 }
5569
5570 bucket = s->hashval[h->dynindx] % s->bucketcount;
5571 val = (s->hashval[h->dynindx] >> s->shift1)
5572 & ((s->maskbits >> s->shift1) - 1);
5573 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5574 s->bitmask[val]
5575 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5576 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5577 if (s->counts[bucket] == 1)
5578 /* Last element terminates the chain. */
5579 val |= 1;
5580 bfd_put_32 (s->output_bfd, val,
5581 s->contents + (s->indx[bucket] - s->symindx) * 4);
5582 --s->counts[bucket];
5583 h->dynindx = s->indx[bucket]++;
5584 return TRUE;
5585 }
5586
5587 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5588
5589 bfd_boolean
5590 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5591 {
5592 return !(h->forced_local
5593 || h->root.type == bfd_link_hash_undefined
5594 || h->root.type == bfd_link_hash_undefweak
5595 || ((h->root.type == bfd_link_hash_defined
5596 || h->root.type == bfd_link_hash_defweak)
5597 && h->root.u.def.section->output_section == NULL));
5598 }
5599
5600 /* Array used to determine the number of hash table buckets to use
5601 based on the number of symbols there are. If there are fewer than
5602 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5603 fewer than 37 we use 17 buckets, and so forth. We never use more
5604 than 32771 buckets. */
5605
5606 static const size_t elf_buckets[] =
5607 {
5608 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5609 16411, 32771, 0
5610 };
5611
5612 /* Compute bucket count for hashing table. We do not use a static set
5613 of possible tables sizes anymore. Instead we determine for all
5614 possible reasonable sizes of the table the outcome (i.e., the
5615 number of collisions etc) and choose the best solution. The
5616 weighting functions are not too simple to allow the table to grow
5617 without bounds. Instead one of the weighting factors is the size.
5618 Therefore the result is always a good payoff between few collisions
5619 (= short chain lengths) and table size. */
5620 static size_t
5621 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5622 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5623 unsigned long int nsyms,
5624 int gnu_hash)
5625 {
5626 size_t best_size = 0;
5627 unsigned long int i;
5628
5629 /* We have a problem here. The following code to optimize the table
5630 size requires an integer type with more the 32 bits. If
5631 BFD_HOST_U_64_BIT is set we know about such a type. */
5632 #ifdef BFD_HOST_U_64_BIT
5633 if (info->optimize)
5634 {
5635 size_t minsize;
5636 size_t maxsize;
5637 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5638 bfd *dynobj = elf_hash_table (info)->dynobj;
5639 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5640 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5641 unsigned long int *counts;
5642 bfd_size_type amt;
5643 unsigned int no_improvement_count = 0;
5644
5645 /* Possible optimization parameters: if we have NSYMS symbols we say
5646 that the hashing table must at least have NSYMS/4 and at most
5647 2*NSYMS buckets. */
5648 minsize = nsyms / 4;
5649 if (minsize == 0)
5650 minsize = 1;
5651 best_size = maxsize = nsyms * 2;
5652 if (gnu_hash)
5653 {
5654 if (minsize < 2)
5655 minsize = 2;
5656 if ((best_size & 31) == 0)
5657 ++best_size;
5658 }
5659
5660 /* Create array where we count the collisions in. We must use bfd_malloc
5661 since the size could be large. */
5662 amt = maxsize;
5663 amt *= sizeof (unsigned long int);
5664 counts = (unsigned long int *) bfd_malloc (amt);
5665 if (counts == NULL)
5666 return 0;
5667
5668 /* Compute the "optimal" size for the hash table. The criteria is a
5669 minimal chain length. The minor criteria is (of course) the size
5670 of the table. */
5671 for (i = minsize; i < maxsize; ++i)
5672 {
5673 /* Walk through the array of hashcodes and count the collisions. */
5674 BFD_HOST_U_64_BIT max;
5675 unsigned long int j;
5676 unsigned long int fact;
5677
5678 if (gnu_hash && (i & 31) == 0)
5679 continue;
5680
5681 memset (counts, '\0', i * sizeof (unsigned long int));
5682
5683 /* Determine how often each hash bucket is used. */
5684 for (j = 0; j < nsyms; ++j)
5685 ++counts[hashcodes[j] % i];
5686
5687 /* For the weight function we need some information about the
5688 pagesize on the target. This is information need not be 100%
5689 accurate. Since this information is not available (so far) we
5690 define it here to a reasonable default value. If it is crucial
5691 to have a better value some day simply define this value. */
5692 # ifndef BFD_TARGET_PAGESIZE
5693 # define BFD_TARGET_PAGESIZE (4096)
5694 # endif
5695
5696 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5697 and the chains. */
5698 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5699
5700 # if 1
5701 /* Variant 1: optimize for short chains. We add the squares
5702 of all the chain lengths (which favors many small chain
5703 over a few long chains). */
5704 for (j = 0; j < i; ++j)
5705 max += counts[j] * counts[j];
5706
5707 /* This adds penalties for the overall size of the table. */
5708 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5709 max *= fact * fact;
5710 # else
5711 /* Variant 2: Optimize a lot more for small table. Here we
5712 also add squares of the size but we also add penalties for
5713 empty slots (the +1 term). */
5714 for (j = 0; j < i; ++j)
5715 max += (1 + counts[j]) * (1 + counts[j]);
5716
5717 /* The overall size of the table is considered, but not as
5718 strong as in variant 1, where it is squared. */
5719 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5720 max *= fact;
5721 # endif
5722
5723 /* Compare with current best results. */
5724 if (max < best_chlen)
5725 {
5726 best_chlen = max;
5727 best_size = i;
5728 no_improvement_count = 0;
5729 }
5730 /* PR 11843: Avoid futile long searches for the best bucket size
5731 when there are a large number of symbols. */
5732 else if (++no_improvement_count == 100)
5733 break;
5734 }
5735
5736 free (counts);
5737 }
5738 else
5739 #endif /* defined (BFD_HOST_U_64_BIT) */
5740 {
5741 /* This is the fallback solution if no 64bit type is available or if we
5742 are not supposed to spend much time on optimizations. We select the
5743 bucket count using a fixed set of numbers. */
5744 for (i = 0; elf_buckets[i] != 0; i++)
5745 {
5746 best_size = elf_buckets[i];
5747 if (nsyms < elf_buckets[i + 1])
5748 break;
5749 }
5750 if (gnu_hash && best_size < 2)
5751 best_size = 2;
5752 }
5753
5754 return best_size;
5755 }
5756
5757 /* Size any SHT_GROUP section for ld -r. */
5758
5759 bfd_boolean
5760 _bfd_elf_size_group_sections (struct bfd_link_info *info)
5761 {
5762 bfd *ibfd;
5763
5764 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
5765 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5766 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5767 return FALSE;
5768 return TRUE;
5769 }
5770
5771 /* Set a default stack segment size. The value in INFO wins. If it
5772 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5773 undefined it is initialized. */
5774
5775 bfd_boolean
5776 bfd_elf_stack_segment_size (bfd *output_bfd,
5777 struct bfd_link_info *info,
5778 const char *legacy_symbol,
5779 bfd_vma default_size)
5780 {
5781 struct elf_link_hash_entry *h = NULL;
5782
5783 /* Look for legacy symbol. */
5784 if (legacy_symbol)
5785 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5786 FALSE, FALSE, FALSE);
5787 if (h && (h->root.type == bfd_link_hash_defined
5788 || h->root.type == bfd_link_hash_defweak)
5789 && h->def_regular
5790 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5791 {
5792 /* The symbol has no type if specified on the command line. */
5793 h->type = STT_OBJECT;
5794 if (info->stacksize)
5795 /* xgettext:c-format */
5796 _bfd_error_handler (_("%B: stack size specified and %s set"),
5797 output_bfd, legacy_symbol);
5798 else if (h->root.u.def.section != bfd_abs_section_ptr)
5799 /* xgettext:c-format */
5800 _bfd_error_handler (_("%B: %s not absolute"),
5801 output_bfd, legacy_symbol);
5802 else
5803 info->stacksize = h->root.u.def.value;
5804 }
5805
5806 if (!info->stacksize)
5807 /* If the user didn't set a size, or explicitly inhibit the
5808 size, set it now. */
5809 info->stacksize = default_size;
5810
5811 /* Provide the legacy symbol, if it is referenced. */
5812 if (h && (h->root.type == bfd_link_hash_undefined
5813 || h->root.type == bfd_link_hash_undefweak))
5814 {
5815 struct bfd_link_hash_entry *bh = NULL;
5816
5817 if (!(_bfd_generic_link_add_one_symbol
5818 (info, output_bfd, legacy_symbol,
5819 BSF_GLOBAL, bfd_abs_section_ptr,
5820 info->stacksize >= 0 ? info->stacksize : 0,
5821 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5822 return FALSE;
5823
5824 h = (struct elf_link_hash_entry *) bh;
5825 h->def_regular = 1;
5826 h->type = STT_OBJECT;
5827 }
5828
5829 return TRUE;
5830 }
5831
5832 /* Set up the sizes and contents of the ELF dynamic sections. This is
5833 called by the ELF linker emulation before_allocation routine. We
5834 must set the sizes of the sections before the linker sets the
5835 addresses of the various sections. */
5836
5837 bfd_boolean
5838 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5839 const char *soname,
5840 const char *rpath,
5841 const char *filter_shlib,
5842 const char *audit,
5843 const char *depaudit,
5844 const char * const *auxiliary_filters,
5845 struct bfd_link_info *info,
5846 asection **sinterpptr)
5847 {
5848 size_t soname_indx;
5849 bfd *dynobj;
5850 const struct elf_backend_data *bed;
5851 struct elf_info_failed asvinfo;
5852
5853 *sinterpptr = NULL;
5854
5855 soname_indx = (size_t) -1;
5856
5857 if (!is_elf_hash_table (info->hash))
5858 return TRUE;
5859
5860 bed = get_elf_backend_data (output_bfd);
5861
5862 /* Any syms created from now on start with -1 in
5863 got.refcount/offset and plt.refcount/offset. */
5864 elf_hash_table (info)->init_got_refcount
5865 = elf_hash_table (info)->init_got_offset;
5866 elf_hash_table (info)->init_plt_refcount
5867 = elf_hash_table (info)->init_plt_offset;
5868
5869 if (bfd_link_relocatable (info)
5870 && !_bfd_elf_size_group_sections (info))
5871 return FALSE;
5872
5873 /* The backend may have to create some sections regardless of whether
5874 we're dynamic or not. */
5875 if (bed->elf_backend_always_size_sections
5876 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5877 return FALSE;
5878
5879 /* Determine any GNU_STACK segment requirements, after the backend
5880 has had a chance to set a default segment size. */
5881 if (info->execstack)
5882 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
5883 else if (info->noexecstack)
5884 elf_stack_flags (output_bfd) = PF_R | PF_W;
5885 else
5886 {
5887 bfd *inputobj;
5888 asection *notesec = NULL;
5889 int exec = 0;
5890
5891 for (inputobj = info->input_bfds;
5892 inputobj;
5893 inputobj = inputobj->link.next)
5894 {
5895 asection *s;
5896
5897 if (inputobj->flags
5898 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5899 continue;
5900 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5901 if (s)
5902 {
5903 if (s->flags & SEC_CODE)
5904 exec = PF_X;
5905 notesec = s;
5906 }
5907 else if (bed->default_execstack)
5908 exec = PF_X;
5909 }
5910 if (notesec || info->stacksize > 0)
5911 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
5912 if (notesec && exec && bfd_link_relocatable (info)
5913 && notesec->output_section != bfd_abs_section_ptr)
5914 notesec->output_section->flags |= SEC_CODE;
5915 }
5916
5917 dynobj = elf_hash_table (info)->dynobj;
5918
5919 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5920 {
5921 struct elf_info_failed eif;
5922 struct elf_link_hash_entry *h;
5923 asection *dynstr;
5924 struct bfd_elf_version_tree *t;
5925 struct bfd_elf_version_expr *d;
5926 asection *s;
5927 bfd_boolean all_defined;
5928
5929 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
5930 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
5931
5932 if (soname != NULL)
5933 {
5934 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5935 soname, TRUE);
5936 if (soname_indx == (size_t) -1
5937 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5938 return FALSE;
5939 }
5940
5941 if (info->symbolic)
5942 {
5943 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5944 return FALSE;
5945 info->flags |= DF_SYMBOLIC;
5946 }
5947
5948 if (rpath != NULL)
5949 {
5950 size_t indx;
5951 bfd_vma tag;
5952
5953 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5954 TRUE);
5955 if (indx == (size_t) -1)
5956 return FALSE;
5957
5958 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
5959 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
5960 return FALSE;
5961 }
5962
5963 if (filter_shlib != NULL)
5964 {
5965 size_t indx;
5966
5967 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5968 filter_shlib, TRUE);
5969 if (indx == (size_t) -1
5970 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5971 return FALSE;
5972 }
5973
5974 if (auxiliary_filters != NULL)
5975 {
5976 const char * const *p;
5977
5978 for (p = auxiliary_filters; *p != NULL; p++)
5979 {
5980 size_t indx;
5981
5982 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5983 *p, TRUE);
5984 if (indx == (size_t) -1
5985 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5986 return FALSE;
5987 }
5988 }
5989
5990 if (audit != NULL)
5991 {
5992 size_t indx;
5993
5994 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5995 TRUE);
5996 if (indx == (size_t) -1
5997 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5998 return FALSE;
5999 }
6000
6001 if (depaudit != NULL)
6002 {
6003 size_t indx;
6004
6005 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6006 TRUE);
6007 if (indx == (size_t) -1
6008 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6009 return FALSE;
6010 }
6011
6012 eif.info = info;
6013 eif.failed = FALSE;
6014
6015 /* If we are supposed to export all symbols into the dynamic symbol
6016 table (this is not the normal case), then do so. */
6017 if (info->export_dynamic
6018 || (bfd_link_executable (info) && info->dynamic))
6019 {
6020 elf_link_hash_traverse (elf_hash_table (info),
6021 _bfd_elf_export_symbol,
6022 &eif);
6023 if (eif.failed)
6024 return FALSE;
6025 }
6026
6027 /* Make all global versions with definition. */
6028 for (t = info->version_info; t != NULL; t = t->next)
6029 for (d = t->globals.list; d != NULL; d = d->next)
6030 if (!d->symver && d->literal)
6031 {
6032 const char *verstr, *name;
6033 size_t namelen, verlen, newlen;
6034 char *newname, *p, leading_char;
6035 struct elf_link_hash_entry *newh;
6036
6037 leading_char = bfd_get_symbol_leading_char (output_bfd);
6038 name = d->pattern;
6039 namelen = strlen (name) + (leading_char != '\0');
6040 verstr = t->name;
6041 verlen = strlen (verstr);
6042 newlen = namelen + verlen + 3;
6043
6044 newname = (char *) bfd_malloc (newlen);
6045 if (newname == NULL)
6046 return FALSE;
6047 newname[0] = leading_char;
6048 memcpy (newname + (leading_char != '\0'), name, namelen);
6049
6050 /* Check the hidden versioned definition. */
6051 p = newname + namelen;
6052 *p++ = ELF_VER_CHR;
6053 memcpy (p, verstr, verlen + 1);
6054 newh = elf_link_hash_lookup (elf_hash_table (info),
6055 newname, FALSE, FALSE,
6056 FALSE);
6057 if (newh == NULL
6058 || (newh->root.type != bfd_link_hash_defined
6059 && newh->root.type != bfd_link_hash_defweak))
6060 {
6061 /* Check the default versioned definition. */
6062 *p++ = ELF_VER_CHR;
6063 memcpy (p, verstr, verlen + 1);
6064 newh = elf_link_hash_lookup (elf_hash_table (info),
6065 newname, FALSE, FALSE,
6066 FALSE);
6067 }
6068 free (newname);
6069
6070 /* Mark this version if there is a definition and it is
6071 not defined in a shared object. */
6072 if (newh != NULL
6073 && !newh->def_dynamic
6074 && (newh->root.type == bfd_link_hash_defined
6075 || newh->root.type == bfd_link_hash_defweak))
6076 d->symver = 1;
6077 }
6078
6079 /* Attach all the symbols to their version information. */
6080 asvinfo.info = info;
6081 asvinfo.failed = FALSE;
6082
6083 elf_link_hash_traverse (elf_hash_table (info),
6084 _bfd_elf_link_assign_sym_version,
6085 &asvinfo);
6086 if (asvinfo.failed)
6087 return FALSE;
6088
6089 if (!info->allow_undefined_version)
6090 {
6091 /* Check if all global versions have a definition. */
6092 all_defined = TRUE;
6093 for (t = info->version_info; t != NULL; t = t->next)
6094 for (d = t->globals.list; d != NULL; d = d->next)
6095 if (d->literal && !d->symver && !d->script)
6096 {
6097 _bfd_error_handler
6098 (_("%s: undefined version: %s"),
6099 d->pattern, t->name);
6100 all_defined = FALSE;
6101 }
6102
6103 if (!all_defined)
6104 {
6105 bfd_set_error (bfd_error_bad_value);
6106 return FALSE;
6107 }
6108 }
6109
6110 /* Find all symbols which were defined in a dynamic object and make
6111 the backend pick a reasonable value for them. */
6112 elf_link_hash_traverse (elf_hash_table (info),
6113 _bfd_elf_adjust_dynamic_symbol,
6114 &eif);
6115 if (eif.failed)
6116 return FALSE;
6117
6118 /* Add some entries to the .dynamic section. We fill in some of the
6119 values later, in bfd_elf_final_link, but we must add the entries
6120 now so that we know the final size of the .dynamic section. */
6121
6122 /* If there are initialization and/or finalization functions to
6123 call then add the corresponding DT_INIT/DT_FINI entries. */
6124 h = (info->init_function
6125 ? elf_link_hash_lookup (elf_hash_table (info),
6126 info->init_function, FALSE,
6127 FALSE, FALSE)
6128 : NULL);
6129 if (h != NULL
6130 && (h->ref_regular
6131 || h->def_regular))
6132 {
6133 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6134 return FALSE;
6135 }
6136 h = (info->fini_function
6137 ? elf_link_hash_lookup (elf_hash_table (info),
6138 info->fini_function, FALSE,
6139 FALSE, FALSE)
6140 : NULL);
6141 if (h != NULL
6142 && (h->ref_regular
6143 || h->def_regular))
6144 {
6145 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6146 return FALSE;
6147 }
6148
6149 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6150 if (s != NULL && s->linker_has_input)
6151 {
6152 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6153 if (! bfd_link_executable (info))
6154 {
6155 bfd *sub;
6156 asection *o;
6157
6158 for (sub = info->input_bfds; sub != NULL;
6159 sub = sub->link.next)
6160 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
6161 for (o = sub->sections; o != NULL; o = o->next)
6162 if (elf_section_data (o)->this_hdr.sh_type
6163 == SHT_PREINIT_ARRAY)
6164 {
6165 _bfd_error_handler
6166 (_("%B: .preinit_array section is not allowed in DSO"),
6167 sub);
6168 break;
6169 }
6170
6171 bfd_set_error (bfd_error_nonrepresentable_section);
6172 return FALSE;
6173 }
6174
6175 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6176 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6177 return FALSE;
6178 }
6179 s = bfd_get_section_by_name (output_bfd, ".init_array");
6180 if (s != NULL && s->linker_has_input)
6181 {
6182 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6183 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6184 return FALSE;
6185 }
6186 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6187 if (s != NULL && s->linker_has_input)
6188 {
6189 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6190 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6191 return FALSE;
6192 }
6193
6194 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6195 /* If .dynstr is excluded from the link, we don't want any of
6196 these tags. Strictly, we should be checking each section
6197 individually; This quick check covers for the case where
6198 someone does a /DISCARD/ : { *(*) }. */
6199 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6200 {
6201 bfd_size_type strsize;
6202
6203 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6204 if ((info->emit_hash
6205 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6206 || (info->emit_gnu_hash
6207 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6208 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6209 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6210 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6211 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6212 bed->s->sizeof_sym))
6213 return FALSE;
6214 }
6215 }
6216
6217 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6218 return FALSE;
6219
6220 /* The backend must work out the sizes of all the other dynamic
6221 sections. */
6222 if (dynobj != NULL
6223 && bed->elf_backend_size_dynamic_sections != NULL
6224 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6225 return FALSE;
6226
6227 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6228 {
6229 unsigned long section_sym_count;
6230 struct bfd_elf_version_tree *verdefs;
6231 asection *s;
6232
6233 /* Set up the version definition section. */
6234 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6235 BFD_ASSERT (s != NULL);
6236
6237 /* We may have created additional version definitions if we are
6238 just linking a regular application. */
6239 verdefs = info->version_info;
6240
6241 /* Skip anonymous version tag. */
6242 if (verdefs != NULL && verdefs->vernum == 0)
6243 verdefs = verdefs->next;
6244
6245 if (verdefs == NULL && !info->create_default_symver)
6246 s->flags |= SEC_EXCLUDE;
6247 else
6248 {
6249 unsigned int cdefs;
6250 bfd_size_type size;
6251 struct bfd_elf_version_tree *t;
6252 bfd_byte *p;
6253 Elf_Internal_Verdef def;
6254 Elf_Internal_Verdaux defaux;
6255 struct bfd_link_hash_entry *bh;
6256 struct elf_link_hash_entry *h;
6257 const char *name;
6258
6259 cdefs = 0;
6260 size = 0;
6261
6262 /* Make space for the base version. */
6263 size += sizeof (Elf_External_Verdef);
6264 size += sizeof (Elf_External_Verdaux);
6265 ++cdefs;
6266
6267 /* Make space for the default version. */
6268 if (info->create_default_symver)
6269 {
6270 size += sizeof (Elf_External_Verdef);
6271 ++cdefs;
6272 }
6273
6274 for (t = verdefs; t != NULL; t = t->next)
6275 {
6276 struct bfd_elf_version_deps *n;
6277
6278 /* Don't emit base version twice. */
6279 if (t->vernum == 0)
6280 continue;
6281
6282 size += sizeof (Elf_External_Verdef);
6283 size += sizeof (Elf_External_Verdaux);
6284 ++cdefs;
6285
6286 for (n = t->deps; n != NULL; n = n->next)
6287 size += sizeof (Elf_External_Verdaux);
6288 }
6289
6290 s->size = size;
6291 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6292 if (s->contents == NULL && s->size != 0)
6293 return FALSE;
6294
6295 /* Fill in the version definition section. */
6296
6297 p = s->contents;
6298
6299 def.vd_version = VER_DEF_CURRENT;
6300 def.vd_flags = VER_FLG_BASE;
6301 def.vd_ndx = 1;
6302 def.vd_cnt = 1;
6303 if (info->create_default_symver)
6304 {
6305 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6306 def.vd_next = sizeof (Elf_External_Verdef);
6307 }
6308 else
6309 {
6310 def.vd_aux = sizeof (Elf_External_Verdef);
6311 def.vd_next = (sizeof (Elf_External_Verdef)
6312 + sizeof (Elf_External_Verdaux));
6313 }
6314
6315 if (soname_indx != (size_t) -1)
6316 {
6317 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6318 soname_indx);
6319 def.vd_hash = bfd_elf_hash (soname);
6320 defaux.vda_name = soname_indx;
6321 name = soname;
6322 }
6323 else
6324 {
6325 size_t indx;
6326
6327 name = lbasename (output_bfd->filename);
6328 def.vd_hash = bfd_elf_hash (name);
6329 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6330 name, FALSE);
6331 if (indx == (size_t) -1)
6332 return FALSE;
6333 defaux.vda_name = indx;
6334 }
6335 defaux.vda_next = 0;
6336
6337 _bfd_elf_swap_verdef_out (output_bfd, &def,
6338 (Elf_External_Verdef *) p);
6339 p += sizeof (Elf_External_Verdef);
6340 if (info->create_default_symver)
6341 {
6342 /* Add a symbol representing this version. */
6343 bh = NULL;
6344 if (! (_bfd_generic_link_add_one_symbol
6345 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6346 0, NULL, FALSE,
6347 get_elf_backend_data (dynobj)->collect, &bh)))
6348 return FALSE;
6349 h = (struct elf_link_hash_entry *) bh;
6350 h->non_elf = 0;
6351 h->def_regular = 1;
6352 h->type = STT_OBJECT;
6353 h->verinfo.vertree = NULL;
6354
6355 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6356 return FALSE;
6357
6358 /* Create a duplicate of the base version with the same
6359 aux block, but different flags. */
6360 def.vd_flags = 0;
6361 def.vd_ndx = 2;
6362 def.vd_aux = sizeof (Elf_External_Verdef);
6363 if (verdefs)
6364 def.vd_next = (sizeof (Elf_External_Verdef)
6365 + sizeof (Elf_External_Verdaux));
6366 else
6367 def.vd_next = 0;
6368 _bfd_elf_swap_verdef_out (output_bfd, &def,
6369 (Elf_External_Verdef *) p);
6370 p += sizeof (Elf_External_Verdef);
6371 }
6372 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6373 (Elf_External_Verdaux *) p);
6374 p += sizeof (Elf_External_Verdaux);
6375
6376 for (t = verdefs; t != NULL; t = t->next)
6377 {
6378 unsigned int cdeps;
6379 struct bfd_elf_version_deps *n;
6380
6381 /* Don't emit the base version twice. */
6382 if (t->vernum == 0)
6383 continue;
6384
6385 cdeps = 0;
6386 for (n = t->deps; n != NULL; n = n->next)
6387 ++cdeps;
6388
6389 /* Add a symbol representing this version. */
6390 bh = NULL;
6391 if (! (_bfd_generic_link_add_one_symbol
6392 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6393 0, NULL, FALSE,
6394 get_elf_backend_data (dynobj)->collect, &bh)))
6395 return FALSE;
6396 h = (struct elf_link_hash_entry *) bh;
6397 h->non_elf = 0;
6398 h->def_regular = 1;
6399 h->type = STT_OBJECT;
6400 h->verinfo.vertree = t;
6401
6402 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6403 return FALSE;
6404
6405 def.vd_version = VER_DEF_CURRENT;
6406 def.vd_flags = 0;
6407 if (t->globals.list == NULL
6408 && t->locals.list == NULL
6409 && ! t->used)
6410 def.vd_flags |= VER_FLG_WEAK;
6411 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6412 def.vd_cnt = cdeps + 1;
6413 def.vd_hash = bfd_elf_hash (t->name);
6414 def.vd_aux = sizeof (Elf_External_Verdef);
6415 def.vd_next = 0;
6416
6417 /* If a basever node is next, it *must* be the last node in
6418 the chain, otherwise Verdef construction breaks. */
6419 if (t->next != NULL && t->next->vernum == 0)
6420 BFD_ASSERT (t->next->next == NULL);
6421
6422 if (t->next != NULL && t->next->vernum != 0)
6423 def.vd_next = (sizeof (Elf_External_Verdef)
6424 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6425
6426 _bfd_elf_swap_verdef_out (output_bfd, &def,
6427 (Elf_External_Verdef *) p);
6428 p += sizeof (Elf_External_Verdef);
6429
6430 defaux.vda_name = h->dynstr_index;
6431 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6432 h->dynstr_index);
6433 defaux.vda_next = 0;
6434 if (t->deps != NULL)
6435 defaux.vda_next = sizeof (Elf_External_Verdaux);
6436 t->name_indx = defaux.vda_name;
6437
6438 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6439 (Elf_External_Verdaux *) p);
6440 p += sizeof (Elf_External_Verdaux);
6441
6442 for (n = t->deps; n != NULL; n = n->next)
6443 {
6444 if (n->version_needed == NULL)
6445 {
6446 /* This can happen if there was an error in the
6447 version script. */
6448 defaux.vda_name = 0;
6449 }
6450 else
6451 {
6452 defaux.vda_name = n->version_needed->name_indx;
6453 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6454 defaux.vda_name);
6455 }
6456 if (n->next == NULL)
6457 defaux.vda_next = 0;
6458 else
6459 defaux.vda_next = sizeof (Elf_External_Verdaux);
6460
6461 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6462 (Elf_External_Verdaux *) p);
6463 p += sizeof (Elf_External_Verdaux);
6464 }
6465 }
6466
6467 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6468 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6469 return FALSE;
6470
6471 elf_tdata (output_bfd)->cverdefs = cdefs;
6472 }
6473
6474 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6475 {
6476 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6477 return FALSE;
6478 }
6479 else if (info->flags & DF_BIND_NOW)
6480 {
6481 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6482 return FALSE;
6483 }
6484
6485 if (info->flags_1)
6486 {
6487 if (bfd_link_executable (info))
6488 info->flags_1 &= ~ (DF_1_INITFIRST
6489 | DF_1_NODELETE
6490 | DF_1_NOOPEN);
6491 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6492 return FALSE;
6493 }
6494
6495 /* Work out the size of the version reference section. */
6496
6497 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6498 BFD_ASSERT (s != NULL);
6499 {
6500 struct elf_find_verdep_info sinfo;
6501
6502 sinfo.info = info;
6503 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6504 if (sinfo.vers == 0)
6505 sinfo.vers = 1;
6506 sinfo.failed = FALSE;
6507
6508 elf_link_hash_traverse (elf_hash_table (info),
6509 _bfd_elf_link_find_version_dependencies,
6510 &sinfo);
6511 if (sinfo.failed)
6512 return FALSE;
6513
6514 if (elf_tdata (output_bfd)->verref == NULL)
6515 s->flags |= SEC_EXCLUDE;
6516 else
6517 {
6518 Elf_Internal_Verneed *t;
6519 unsigned int size;
6520 unsigned int crefs;
6521 bfd_byte *p;
6522
6523 /* Build the version dependency section. */
6524 size = 0;
6525 crefs = 0;
6526 for (t = elf_tdata (output_bfd)->verref;
6527 t != NULL;
6528 t = t->vn_nextref)
6529 {
6530 Elf_Internal_Vernaux *a;
6531
6532 size += sizeof (Elf_External_Verneed);
6533 ++crefs;
6534 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6535 size += sizeof (Elf_External_Vernaux);
6536 }
6537
6538 s->size = size;
6539 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6540 if (s->contents == NULL)
6541 return FALSE;
6542
6543 p = s->contents;
6544 for (t = elf_tdata (output_bfd)->verref;
6545 t != NULL;
6546 t = t->vn_nextref)
6547 {
6548 unsigned int caux;
6549 Elf_Internal_Vernaux *a;
6550 size_t indx;
6551
6552 caux = 0;
6553 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6554 ++caux;
6555
6556 t->vn_version = VER_NEED_CURRENT;
6557 t->vn_cnt = caux;
6558 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6559 elf_dt_name (t->vn_bfd) != NULL
6560 ? elf_dt_name (t->vn_bfd)
6561 : lbasename (t->vn_bfd->filename),
6562 FALSE);
6563 if (indx == (size_t) -1)
6564 return FALSE;
6565 t->vn_file = indx;
6566 t->vn_aux = sizeof (Elf_External_Verneed);
6567 if (t->vn_nextref == NULL)
6568 t->vn_next = 0;
6569 else
6570 t->vn_next = (sizeof (Elf_External_Verneed)
6571 + caux * sizeof (Elf_External_Vernaux));
6572
6573 _bfd_elf_swap_verneed_out (output_bfd, t,
6574 (Elf_External_Verneed *) p);
6575 p += sizeof (Elf_External_Verneed);
6576
6577 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6578 {
6579 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6580 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6581 a->vna_nodename, FALSE);
6582 if (indx == (size_t) -1)
6583 return FALSE;
6584 a->vna_name = indx;
6585 if (a->vna_nextptr == NULL)
6586 a->vna_next = 0;
6587 else
6588 a->vna_next = sizeof (Elf_External_Vernaux);
6589
6590 _bfd_elf_swap_vernaux_out (output_bfd, a,
6591 (Elf_External_Vernaux *) p);
6592 p += sizeof (Elf_External_Vernaux);
6593 }
6594 }
6595
6596 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6597 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6598 return FALSE;
6599
6600 elf_tdata (output_bfd)->cverrefs = crefs;
6601 }
6602 }
6603
6604 if ((elf_tdata (output_bfd)->cverrefs == 0
6605 && elf_tdata (output_bfd)->cverdefs == 0)
6606 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6607 &section_sym_count) == 0)
6608 {
6609 s = bfd_get_linker_section (dynobj, ".gnu.version");
6610 s->flags |= SEC_EXCLUDE;
6611 }
6612 }
6613 return TRUE;
6614 }
6615
6616 /* Find the first non-excluded output section. We'll use its
6617 section symbol for some emitted relocs. */
6618 void
6619 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6620 {
6621 asection *s;
6622
6623 for (s = output_bfd->sections; s != NULL; s = s->next)
6624 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6625 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6626 {
6627 elf_hash_table (info)->text_index_section = s;
6628 break;
6629 }
6630 }
6631
6632 /* Find two non-excluded output sections, one for code, one for data.
6633 We'll use their section symbols for some emitted relocs. */
6634 void
6635 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6636 {
6637 asection *s;
6638
6639 /* Data first, since setting text_index_section changes
6640 _bfd_elf_link_omit_section_dynsym. */
6641 for (s = output_bfd->sections; s != NULL; s = s->next)
6642 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6643 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6644 {
6645 elf_hash_table (info)->data_index_section = s;
6646 break;
6647 }
6648
6649 for (s = output_bfd->sections; s != NULL; s = s->next)
6650 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6651 == (SEC_ALLOC | SEC_READONLY))
6652 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6653 {
6654 elf_hash_table (info)->text_index_section = s;
6655 break;
6656 }
6657
6658 if (elf_hash_table (info)->text_index_section == NULL)
6659 elf_hash_table (info)->text_index_section
6660 = elf_hash_table (info)->data_index_section;
6661 }
6662
6663 bfd_boolean
6664 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6665 {
6666 const struct elf_backend_data *bed;
6667
6668 if (!is_elf_hash_table (info->hash))
6669 return TRUE;
6670
6671 bed = get_elf_backend_data (output_bfd);
6672 (*bed->elf_backend_init_index_section) (output_bfd, info);
6673
6674 if (elf_hash_table (info)->dynamic_sections_created)
6675 {
6676 bfd *dynobj;
6677 asection *s;
6678 bfd_size_type dynsymcount;
6679 unsigned long section_sym_count;
6680 unsigned int dtagcount;
6681
6682 dynobj = elf_hash_table (info)->dynobj;
6683
6684 /* Assign dynsym indicies. In a shared library we generate a
6685 section symbol for each output section, which come first.
6686 Next come all of the back-end allocated local dynamic syms,
6687 followed by the rest of the global symbols. */
6688
6689 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6690 &section_sym_count);
6691
6692 /* Work out the size of the symbol version section. */
6693 s = bfd_get_linker_section (dynobj, ".gnu.version");
6694 BFD_ASSERT (s != NULL);
6695 if ((s->flags & SEC_EXCLUDE) == 0)
6696 {
6697 s->size = dynsymcount * sizeof (Elf_External_Versym);
6698 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6699 if (s->contents == NULL)
6700 return FALSE;
6701
6702 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6703 return FALSE;
6704 }
6705
6706 /* Set the size of the .dynsym and .hash sections. We counted
6707 the number of dynamic symbols in elf_link_add_object_symbols.
6708 We will build the contents of .dynsym and .hash when we build
6709 the final symbol table, because until then we do not know the
6710 correct value to give the symbols. We built the .dynstr
6711 section as we went along in elf_link_add_object_symbols. */
6712 s = elf_hash_table (info)->dynsym;
6713 BFD_ASSERT (s != NULL);
6714 s->size = dynsymcount * bed->s->sizeof_sym;
6715
6716 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6717 if (s->contents == NULL)
6718 return FALSE;
6719
6720 /* The first entry in .dynsym is a dummy symbol. Clear all the
6721 section syms, in case we don't output them all. */
6722 ++section_sym_count;
6723 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6724
6725 elf_hash_table (info)->bucketcount = 0;
6726
6727 /* Compute the size of the hashing table. As a side effect this
6728 computes the hash values for all the names we export. */
6729 if (info->emit_hash)
6730 {
6731 unsigned long int *hashcodes;
6732 struct hash_codes_info hashinf;
6733 bfd_size_type amt;
6734 unsigned long int nsyms;
6735 size_t bucketcount;
6736 size_t hash_entry_size;
6737
6738 /* Compute the hash values for all exported symbols. At the same
6739 time store the values in an array so that we could use them for
6740 optimizations. */
6741 amt = dynsymcount * sizeof (unsigned long int);
6742 hashcodes = (unsigned long int *) bfd_malloc (amt);
6743 if (hashcodes == NULL)
6744 return FALSE;
6745 hashinf.hashcodes = hashcodes;
6746 hashinf.error = FALSE;
6747
6748 /* Put all hash values in HASHCODES. */
6749 elf_link_hash_traverse (elf_hash_table (info),
6750 elf_collect_hash_codes, &hashinf);
6751 if (hashinf.error)
6752 {
6753 free (hashcodes);
6754 return FALSE;
6755 }
6756
6757 nsyms = hashinf.hashcodes - hashcodes;
6758 bucketcount
6759 = compute_bucket_count (info, hashcodes, nsyms, 0);
6760 free (hashcodes);
6761
6762 if (bucketcount == 0)
6763 return FALSE;
6764
6765 elf_hash_table (info)->bucketcount = bucketcount;
6766
6767 s = bfd_get_linker_section (dynobj, ".hash");
6768 BFD_ASSERT (s != NULL);
6769 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6770 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6771 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6772 if (s->contents == NULL)
6773 return FALSE;
6774
6775 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6776 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6777 s->contents + hash_entry_size);
6778 }
6779
6780 if (info->emit_gnu_hash)
6781 {
6782 size_t i, cnt;
6783 unsigned char *contents;
6784 struct collect_gnu_hash_codes cinfo;
6785 bfd_size_type amt;
6786 size_t bucketcount;
6787
6788 memset (&cinfo, 0, sizeof (cinfo));
6789
6790 /* Compute the hash values for all exported symbols. At the same
6791 time store the values in an array so that we could use them for
6792 optimizations. */
6793 amt = dynsymcount * 2 * sizeof (unsigned long int);
6794 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
6795 if (cinfo.hashcodes == NULL)
6796 return FALSE;
6797
6798 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6799 cinfo.min_dynindx = -1;
6800 cinfo.output_bfd = output_bfd;
6801 cinfo.bed = bed;
6802
6803 /* Put all hash values in HASHCODES. */
6804 elf_link_hash_traverse (elf_hash_table (info),
6805 elf_collect_gnu_hash_codes, &cinfo);
6806 if (cinfo.error)
6807 {
6808 free (cinfo.hashcodes);
6809 return FALSE;
6810 }
6811
6812 bucketcount
6813 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6814
6815 if (bucketcount == 0)
6816 {
6817 free (cinfo.hashcodes);
6818 return FALSE;
6819 }
6820
6821 s = bfd_get_linker_section (dynobj, ".gnu.hash");
6822 BFD_ASSERT (s != NULL);
6823
6824 if (cinfo.nsyms == 0)
6825 {
6826 /* Empty .gnu.hash section is special. */
6827 BFD_ASSERT (cinfo.min_dynindx == -1);
6828 free (cinfo.hashcodes);
6829 s->size = 5 * 4 + bed->s->arch_size / 8;
6830 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6831 if (contents == NULL)
6832 return FALSE;
6833 s->contents = contents;
6834 /* 1 empty bucket. */
6835 bfd_put_32 (output_bfd, 1, contents);
6836 /* SYMIDX above the special symbol 0. */
6837 bfd_put_32 (output_bfd, 1, contents + 4);
6838 /* Just one word for bitmask. */
6839 bfd_put_32 (output_bfd, 1, contents + 8);
6840 /* Only hash fn bloom filter. */
6841 bfd_put_32 (output_bfd, 0, contents + 12);
6842 /* No hashes are valid - empty bitmask. */
6843 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6844 /* No hashes in the only bucket. */
6845 bfd_put_32 (output_bfd, 0,
6846 contents + 16 + bed->s->arch_size / 8);
6847 }
6848 else
6849 {
6850 unsigned long int maskwords, maskbitslog2, x;
6851 BFD_ASSERT (cinfo.min_dynindx != -1);
6852
6853 x = cinfo.nsyms;
6854 maskbitslog2 = 1;
6855 while ((x >>= 1) != 0)
6856 ++maskbitslog2;
6857 if (maskbitslog2 < 3)
6858 maskbitslog2 = 5;
6859 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6860 maskbitslog2 = maskbitslog2 + 3;
6861 else
6862 maskbitslog2 = maskbitslog2 + 2;
6863 if (bed->s->arch_size == 64)
6864 {
6865 if (maskbitslog2 == 5)
6866 maskbitslog2 = 6;
6867 cinfo.shift1 = 6;
6868 }
6869 else
6870 cinfo.shift1 = 5;
6871 cinfo.mask = (1 << cinfo.shift1) - 1;
6872 cinfo.shift2 = maskbitslog2;
6873 cinfo.maskbits = 1 << maskbitslog2;
6874 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6875 amt = bucketcount * sizeof (unsigned long int) * 2;
6876 amt += maskwords * sizeof (bfd_vma);
6877 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
6878 if (cinfo.bitmask == NULL)
6879 {
6880 free (cinfo.hashcodes);
6881 return FALSE;
6882 }
6883
6884 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
6885 cinfo.indx = cinfo.counts + bucketcount;
6886 cinfo.symindx = dynsymcount - cinfo.nsyms;
6887 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6888
6889 /* Determine how often each hash bucket is used. */
6890 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6891 for (i = 0; i < cinfo.nsyms; ++i)
6892 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6893
6894 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6895 if (cinfo.counts[i] != 0)
6896 {
6897 cinfo.indx[i] = cnt;
6898 cnt += cinfo.counts[i];
6899 }
6900 BFD_ASSERT (cnt == dynsymcount);
6901 cinfo.bucketcount = bucketcount;
6902 cinfo.local_indx = cinfo.min_dynindx;
6903
6904 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6905 s->size += cinfo.maskbits / 8;
6906 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6907 if (contents == NULL)
6908 {
6909 free (cinfo.bitmask);
6910 free (cinfo.hashcodes);
6911 return FALSE;
6912 }
6913
6914 s->contents = contents;
6915 bfd_put_32 (output_bfd, bucketcount, contents);
6916 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6917 bfd_put_32 (output_bfd, maskwords, contents + 8);
6918 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6919 contents += 16 + cinfo.maskbits / 8;
6920
6921 for (i = 0; i < bucketcount; ++i)
6922 {
6923 if (cinfo.counts[i] == 0)
6924 bfd_put_32 (output_bfd, 0, contents);
6925 else
6926 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6927 contents += 4;
6928 }
6929
6930 cinfo.contents = contents;
6931
6932 /* Renumber dynamic symbols, populate .gnu.hash section. */
6933 elf_link_hash_traverse (elf_hash_table (info),
6934 elf_renumber_gnu_hash_syms, &cinfo);
6935
6936 contents = s->contents + 16;
6937 for (i = 0; i < maskwords; ++i)
6938 {
6939 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6940 contents);
6941 contents += bed->s->arch_size / 8;
6942 }
6943
6944 free (cinfo.bitmask);
6945 free (cinfo.hashcodes);
6946 }
6947 }
6948
6949 s = bfd_get_linker_section (dynobj, ".dynstr");
6950 BFD_ASSERT (s != NULL);
6951
6952 elf_finalize_dynstr (output_bfd, info);
6953
6954 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6955
6956 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6957 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6958 return FALSE;
6959 }
6960
6961 return TRUE;
6962 }
6963 \f
6964 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
6965
6966 static void
6967 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6968 asection *sec)
6969 {
6970 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
6971 sec->sec_info_type = SEC_INFO_TYPE_NONE;
6972 }
6973
6974 /* Finish SHF_MERGE section merging. */
6975
6976 bfd_boolean
6977 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
6978 {
6979 bfd *ibfd;
6980 asection *sec;
6981
6982 if (!is_elf_hash_table (info->hash))
6983 return FALSE;
6984
6985 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6986 if ((ibfd->flags & DYNAMIC) == 0
6987 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6988 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
6989 == get_elf_backend_data (obfd)->s->elfclass))
6990 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6991 if ((sec->flags & SEC_MERGE) != 0
6992 && !bfd_is_abs_section (sec->output_section))
6993 {
6994 struct bfd_elf_section_data *secdata;
6995
6996 secdata = elf_section_data (sec);
6997 if (! _bfd_add_merge_section (obfd,
6998 &elf_hash_table (info)->merge_info,
6999 sec, &secdata->sec_info))
7000 return FALSE;
7001 else if (secdata->sec_info)
7002 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7003 }
7004
7005 if (elf_hash_table (info)->merge_info != NULL)
7006 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7007 merge_sections_remove_hook);
7008 return TRUE;
7009 }
7010
7011 /* Create an entry in an ELF linker hash table. */
7012
7013 struct bfd_hash_entry *
7014 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7015 struct bfd_hash_table *table,
7016 const char *string)
7017 {
7018 /* Allocate the structure if it has not already been allocated by a
7019 subclass. */
7020 if (entry == NULL)
7021 {
7022 entry = (struct bfd_hash_entry *)
7023 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7024 if (entry == NULL)
7025 return entry;
7026 }
7027
7028 /* Call the allocation method of the superclass. */
7029 entry = _bfd_link_hash_newfunc (entry, table, string);
7030 if (entry != NULL)
7031 {
7032 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7033 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7034
7035 /* Set local fields. */
7036 ret->indx = -1;
7037 ret->dynindx = -1;
7038 ret->got = htab->init_got_refcount;
7039 ret->plt = htab->init_plt_refcount;
7040 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7041 - offsetof (struct elf_link_hash_entry, size)));
7042 /* Assume that we have been called by a non-ELF symbol reader.
7043 This flag is then reset by the code which reads an ELF input
7044 file. This ensures that a symbol created by a non-ELF symbol
7045 reader will have the flag set correctly. */
7046 ret->non_elf = 1;
7047 }
7048
7049 return entry;
7050 }
7051
7052 /* Copy data from an indirect symbol to its direct symbol, hiding the
7053 old indirect symbol. Also used for copying flags to a weakdef. */
7054
7055 void
7056 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7057 struct elf_link_hash_entry *dir,
7058 struct elf_link_hash_entry *ind)
7059 {
7060 struct elf_link_hash_table *htab;
7061
7062 /* Copy down any references that we may have already seen to the
7063 symbol which just became indirect if DIR isn't a hidden versioned
7064 symbol. */
7065
7066 if (dir->versioned != versioned_hidden)
7067 {
7068 dir->ref_dynamic |= ind->ref_dynamic;
7069 dir->ref_regular |= ind->ref_regular;
7070 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7071 dir->non_got_ref |= ind->non_got_ref;
7072 dir->needs_plt |= ind->needs_plt;
7073 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7074 }
7075
7076 if (ind->root.type != bfd_link_hash_indirect)
7077 return;
7078
7079 /* Copy over the global and procedure linkage table refcount entries.
7080 These may have been already set up by a check_relocs routine. */
7081 htab = elf_hash_table (info);
7082 if (ind->got.refcount > htab->init_got_refcount.refcount)
7083 {
7084 if (dir->got.refcount < 0)
7085 dir->got.refcount = 0;
7086 dir->got.refcount += ind->got.refcount;
7087 ind->got.refcount = htab->init_got_refcount.refcount;
7088 }
7089
7090 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7091 {
7092 if (dir->plt.refcount < 0)
7093 dir->plt.refcount = 0;
7094 dir->plt.refcount += ind->plt.refcount;
7095 ind->plt.refcount = htab->init_plt_refcount.refcount;
7096 }
7097
7098 if (ind->dynindx != -1)
7099 {
7100 if (dir->dynindx != -1)
7101 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7102 dir->dynindx = ind->dynindx;
7103 dir->dynstr_index = ind->dynstr_index;
7104 ind->dynindx = -1;
7105 ind->dynstr_index = 0;
7106 }
7107 }
7108
7109 void
7110 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7111 struct elf_link_hash_entry *h,
7112 bfd_boolean force_local)
7113 {
7114 /* STT_GNU_IFUNC symbol must go through PLT. */
7115 if (h->type != STT_GNU_IFUNC)
7116 {
7117 h->plt = elf_hash_table (info)->init_plt_offset;
7118 h->needs_plt = 0;
7119 }
7120 if (force_local)
7121 {
7122 h->forced_local = 1;
7123 if (h->dynindx != -1)
7124 {
7125 h->dynindx = -1;
7126 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7127 h->dynstr_index);
7128 }
7129 }
7130 }
7131
7132 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7133 caller. */
7134
7135 bfd_boolean
7136 _bfd_elf_link_hash_table_init
7137 (struct elf_link_hash_table *table,
7138 bfd *abfd,
7139 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7140 struct bfd_hash_table *,
7141 const char *),
7142 unsigned int entsize,
7143 enum elf_target_id target_id)
7144 {
7145 bfd_boolean ret;
7146 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7147
7148 table->init_got_refcount.refcount = can_refcount - 1;
7149 table->init_plt_refcount.refcount = can_refcount - 1;
7150 table->init_got_offset.offset = -(bfd_vma) 1;
7151 table->init_plt_offset.offset = -(bfd_vma) 1;
7152 /* The first dynamic symbol is a dummy. */
7153 table->dynsymcount = 1;
7154
7155 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7156
7157 table->root.type = bfd_link_elf_hash_table;
7158 table->hash_table_id = target_id;
7159
7160 return ret;
7161 }
7162
7163 /* Create an ELF linker hash table. */
7164
7165 struct bfd_link_hash_table *
7166 _bfd_elf_link_hash_table_create (bfd *abfd)
7167 {
7168 struct elf_link_hash_table *ret;
7169 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7170
7171 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7172 if (ret == NULL)
7173 return NULL;
7174
7175 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7176 sizeof (struct elf_link_hash_entry),
7177 GENERIC_ELF_DATA))
7178 {
7179 free (ret);
7180 return NULL;
7181 }
7182 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7183
7184 return &ret->root;
7185 }
7186
7187 /* Destroy an ELF linker hash table. */
7188
7189 void
7190 _bfd_elf_link_hash_table_free (bfd *obfd)
7191 {
7192 struct elf_link_hash_table *htab;
7193
7194 htab = (struct elf_link_hash_table *) obfd->link.hash;
7195 if (htab->dynstr != NULL)
7196 _bfd_elf_strtab_free (htab->dynstr);
7197 _bfd_merge_sections_free (htab->merge_info);
7198 _bfd_generic_link_hash_table_free (obfd);
7199 }
7200
7201 /* This is a hook for the ELF emulation code in the generic linker to
7202 tell the backend linker what file name to use for the DT_NEEDED
7203 entry for a dynamic object. */
7204
7205 void
7206 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7207 {
7208 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7209 && bfd_get_format (abfd) == bfd_object)
7210 elf_dt_name (abfd) = name;
7211 }
7212
7213 int
7214 bfd_elf_get_dyn_lib_class (bfd *abfd)
7215 {
7216 int lib_class;
7217 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7218 && bfd_get_format (abfd) == bfd_object)
7219 lib_class = elf_dyn_lib_class (abfd);
7220 else
7221 lib_class = 0;
7222 return lib_class;
7223 }
7224
7225 void
7226 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7227 {
7228 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7229 && bfd_get_format (abfd) == bfd_object)
7230 elf_dyn_lib_class (abfd) = lib_class;
7231 }
7232
7233 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7234 the linker ELF emulation code. */
7235
7236 struct bfd_link_needed_list *
7237 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7238 struct bfd_link_info *info)
7239 {
7240 if (! is_elf_hash_table (info->hash))
7241 return NULL;
7242 return elf_hash_table (info)->needed;
7243 }
7244
7245 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7246 hook for the linker ELF emulation code. */
7247
7248 struct bfd_link_needed_list *
7249 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7250 struct bfd_link_info *info)
7251 {
7252 if (! is_elf_hash_table (info->hash))
7253 return NULL;
7254 return elf_hash_table (info)->runpath;
7255 }
7256
7257 /* Get the name actually used for a dynamic object for a link. This
7258 is the SONAME entry if there is one. Otherwise, it is the string
7259 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7260
7261 const char *
7262 bfd_elf_get_dt_soname (bfd *abfd)
7263 {
7264 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7265 && bfd_get_format (abfd) == bfd_object)
7266 return elf_dt_name (abfd);
7267 return NULL;
7268 }
7269
7270 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7271 the ELF linker emulation code. */
7272
7273 bfd_boolean
7274 bfd_elf_get_bfd_needed_list (bfd *abfd,
7275 struct bfd_link_needed_list **pneeded)
7276 {
7277 asection *s;
7278 bfd_byte *dynbuf = NULL;
7279 unsigned int elfsec;
7280 unsigned long shlink;
7281 bfd_byte *extdyn, *extdynend;
7282 size_t extdynsize;
7283 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7284
7285 *pneeded = NULL;
7286
7287 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7288 || bfd_get_format (abfd) != bfd_object)
7289 return TRUE;
7290
7291 s = bfd_get_section_by_name (abfd, ".dynamic");
7292 if (s == NULL || s->size == 0)
7293 return TRUE;
7294
7295 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7296 goto error_return;
7297
7298 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7299 if (elfsec == SHN_BAD)
7300 goto error_return;
7301
7302 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7303
7304 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7305 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7306
7307 extdyn = dynbuf;
7308 extdynend = extdyn + s->size;
7309 for (; extdyn < extdynend; extdyn += extdynsize)
7310 {
7311 Elf_Internal_Dyn dyn;
7312
7313 (*swap_dyn_in) (abfd, extdyn, &dyn);
7314
7315 if (dyn.d_tag == DT_NULL)
7316 break;
7317
7318 if (dyn.d_tag == DT_NEEDED)
7319 {
7320 const char *string;
7321 struct bfd_link_needed_list *l;
7322 unsigned int tagv = dyn.d_un.d_val;
7323 bfd_size_type amt;
7324
7325 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7326 if (string == NULL)
7327 goto error_return;
7328
7329 amt = sizeof *l;
7330 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7331 if (l == NULL)
7332 goto error_return;
7333
7334 l->by = abfd;
7335 l->name = string;
7336 l->next = *pneeded;
7337 *pneeded = l;
7338 }
7339 }
7340
7341 free (dynbuf);
7342
7343 return TRUE;
7344
7345 error_return:
7346 if (dynbuf != NULL)
7347 free (dynbuf);
7348 return FALSE;
7349 }
7350
7351 struct elf_symbuf_symbol
7352 {
7353 unsigned long st_name; /* Symbol name, index in string tbl */
7354 unsigned char st_info; /* Type and binding attributes */
7355 unsigned char st_other; /* Visibilty, and target specific */
7356 };
7357
7358 struct elf_symbuf_head
7359 {
7360 struct elf_symbuf_symbol *ssym;
7361 size_t count;
7362 unsigned int st_shndx;
7363 };
7364
7365 struct elf_symbol
7366 {
7367 union
7368 {
7369 Elf_Internal_Sym *isym;
7370 struct elf_symbuf_symbol *ssym;
7371 } u;
7372 const char *name;
7373 };
7374
7375 /* Sort references to symbols by ascending section number. */
7376
7377 static int
7378 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7379 {
7380 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7381 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7382
7383 return s1->st_shndx - s2->st_shndx;
7384 }
7385
7386 static int
7387 elf_sym_name_compare (const void *arg1, const void *arg2)
7388 {
7389 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7390 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7391 return strcmp (s1->name, s2->name);
7392 }
7393
7394 static struct elf_symbuf_head *
7395 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
7396 {
7397 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7398 struct elf_symbuf_symbol *ssym;
7399 struct elf_symbuf_head *ssymbuf, *ssymhead;
7400 size_t i, shndx_count, total_size;
7401
7402 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7403 if (indbuf == NULL)
7404 return NULL;
7405
7406 for (ind = indbuf, i = 0; i < symcount; i++)
7407 if (isymbuf[i].st_shndx != SHN_UNDEF)
7408 *ind++ = &isymbuf[i];
7409 indbufend = ind;
7410
7411 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7412 elf_sort_elf_symbol);
7413
7414 shndx_count = 0;
7415 if (indbufend > indbuf)
7416 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7417 if (ind[0]->st_shndx != ind[1]->st_shndx)
7418 shndx_count++;
7419
7420 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7421 + (indbufend - indbuf) * sizeof (*ssym));
7422 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7423 if (ssymbuf == NULL)
7424 {
7425 free (indbuf);
7426 return NULL;
7427 }
7428
7429 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7430 ssymbuf->ssym = NULL;
7431 ssymbuf->count = shndx_count;
7432 ssymbuf->st_shndx = 0;
7433 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7434 {
7435 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7436 {
7437 ssymhead++;
7438 ssymhead->ssym = ssym;
7439 ssymhead->count = 0;
7440 ssymhead->st_shndx = (*ind)->st_shndx;
7441 }
7442 ssym->st_name = (*ind)->st_name;
7443 ssym->st_info = (*ind)->st_info;
7444 ssym->st_other = (*ind)->st_other;
7445 ssymhead->count++;
7446 }
7447 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
7448 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7449 == total_size));
7450
7451 free (indbuf);
7452 return ssymbuf;
7453 }
7454
7455 /* Check if 2 sections define the same set of local and global
7456 symbols. */
7457
7458 static bfd_boolean
7459 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7460 struct bfd_link_info *info)
7461 {
7462 bfd *bfd1, *bfd2;
7463 const struct elf_backend_data *bed1, *bed2;
7464 Elf_Internal_Shdr *hdr1, *hdr2;
7465 size_t symcount1, symcount2;
7466 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7467 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7468 Elf_Internal_Sym *isym, *isymend;
7469 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7470 size_t count1, count2, i;
7471 unsigned int shndx1, shndx2;
7472 bfd_boolean result;
7473
7474 bfd1 = sec1->owner;
7475 bfd2 = sec2->owner;
7476
7477 /* Both sections have to be in ELF. */
7478 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7479 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7480 return FALSE;
7481
7482 if (elf_section_type (sec1) != elf_section_type (sec2))
7483 return FALSE;
7484
7485 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7486 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7487 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7488 return FALSE;
7489
7490 bed1 = get_elf_backend_data (bfd1);
7491 bed2 = get_elf_backend_data (bfd2);
7492 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7493 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7494 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7495 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7496
7497 if (symcount1 == 0 || symcount2 == 0)
7498 return FALSE;
7499
7500 result = FALSE;
7501 isymbuf1 = NULL;
7502 isymbuf2 = NULL;
7503 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7504 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7505
7506 if (ssymbuf1 == NULL)
7507 {
7508 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7509 NULL, NULL, NULL);
7510 if (isymbuf1 == NULL)
7511 goto done;
7512
7513 if (!info->reduce_memory_overheads)
7514 elf_tdata (bfd1)->symbuf = ssymbuf1
7515 = elf_create_symbuf (symcount1, isymbuf1);
7516 }
7517
7518 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7519 {
7520 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7521 NULL, NULL, NULL);
7522 if (isymbuf2 == NULL)
7523 goto done;
7524
7525 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7526 elf_tdata (bfd2)->symbuf = ssymbuf2
7527 = elf_create_symbuf (symcount2, isymbuf2);
7528 }
7529
7530 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7531 {
7532 /* Optimized faster version. */
7533 size_t lo, hi, mid;
7534 struct elf_symbol *symp;
7535 struct elf_symbuf_symbol *ssym, *ssymend;
7536
7537 lo = 0;
7538 hi = ssymbuf1->count;
7539 ssymbuf1++;
7540 count1 = 0;
7541 while (lo < hi)
7542 {
7543 mid = (lo + hi) / 2;
7544 if (shndx1 < ssymbuf1[mid].st_shndx)
7545 hi = mid;
7546 else if (shndx1 > ssymbuf1[mid].st_shndx)
7547 lo = mid + 1;
7548 else
7549 {
7550 count1 = ssymbuf1[mid].count;
7551 ssymbuf1 += mid;
7552 break;
7553 }
7554 }
7555
7556 lo = 0;
7557 hi = ssymbuf2->count;
7558 ssymbuf2++;
7559 count2 = 0;
7560 while (lo < hi)
7561 {
7562 mid = (lo + hi) / 2;
7563 if (shndx2 < ssymbuf2[mid].st_shndx)
7564 hi = mid;
7565 else if (shndx2 > ssymbuf2[mid].st_shndx)
7566 lo = mid + 1;
7567 else
7568 {
7569 count2 = ssymbuf2[mid].count;
7570 ssymbuf2 += mid;
7571 break;
7572 }
7573 }
7574
7575 if (count1 == 0 || count2 == 0 || count1 != count2)
7576 goto done;
7577
7578 symtable1
7579 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7580 symtable2
7581 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
7582 if (symtable1 == NULL || symtable2 == NULL)
7583 goto done;
7584
7585 symp = symtable1;
7586 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7587 ssym < ssymend; ssym++, symp++)
7588 {
7589 symp->u.ssym = ssym;
7590 symp->name = bfd_elf_string_from_elf_section (bfd1,
7591 hdr1->sh_link,
7592 ssym->st_name);
7593 }
7594
7595 symp = symtable2;
7596 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7597 ssym < ssymend; ssym++, symp++)
7598 {
7599 symp->u.ssym = ssym;
7600 symp->name = bfd_elf_string_from_elf_section (bfd2,
7601 hdr2->sh_link,
7602 ssym->st_name);
7603 }
7604
7605 /* Sort symbol by name. */
7606 qsort (symtable1, count1, sizeof (struct elf_symbol),
7607 elf_sym_name_compare);
7608 qsort (symtable2, count1, sizeof (struct elf_symbol),
7609 elf_sym_name_compare);
7610
7611 for (i = 0; i < count1; i++)
7612 /* Two symbols must have the same binding, type and name. */
7613 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7614 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7615 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7616 goto done;
7617
7618 result = TRUE;
7619 goto done;
7620 }
7621
7622 symtable1 = (struct elf_symbol *)
7623 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7624 symtable2 = (struct elf_symbol *)
7625 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7626 if (symtable1 == NULL || symtable2 == NULL)
7627 goto done;
7628
7629 /* Count definitions in the section. */
7630 count1 = 0;
7631 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7632 if (isym->st_shndx == shndx1)
7633 symtable1[count1++].u.isym = isym;
7634
7635 count2 = 0;
7636 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7637 if (isym->st_shndx == shndx2)
7638 symtable2[count2++].u.isym = isym;
7639
7640 if (count1 == 0 || count2 == 0 || count1 != count2)
7641 goto done;
7642
7643 for (i = 0; i < count1; i++)
7644 symtable1[i].name
7645 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7646 symtable1[i].u.isym->st_name);
7647
7648 for (i = 0; i < count2; i++)
7649 symtable2[i].name
7650 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7651 symtable2[i].u.isym->st_name);
7652
7653 /* Sort symbol by name. */
7654 qsort (symtable1, count1, sizeof (struct elf_symbol),
7655 elf_sym_name_compare);
7656 qsort (symtable2, count1, sizeof (struct elf_symbol),
7657 elf_sym_name_compare);
7658
7659 for (i = 0; i < count1; i++)
7660 /* Two symbols must have the same binding, type and name. */
7661 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7662 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7663 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7664 goto done;
7665
7666 result = TRUE;
7667
7668 done:
7669 if (symtable1)
7670 free (symtable1);
7671 if (symtable2)
7672 free (symtable2);
7673 if (isymbuf1)
7674 free (isymbuf1);
7675 if (isymbuf2)
7676 free (isymbuf2);
7677
7678 return result;
7679 }
7680
7681 /* Return TRUE if 2 section types are compatible. */
7682
7683 bfd_boolean
7684 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7685 bfd *bbfd, const asection *bsec)
7686 {
7687 if (asec == NULL
7688 || bsec == NULL
7689 || abfd->xvec->flavour != bfd_target_elf_flavour
7690 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7691 return TRUE;
7692
7693 return elf_section_type (asec) == elf_section_type (bsec);
7694 }
7695 \f
7696 /* Final phase of ELF linker. */
7697
7698 /* A structure we use to avoid passing large numbers of arguments. */
7699
7700 struct elf_final_link_info
7701 {
7702 /* General link information. */
7703 struct bfd_link_info *info;
7704 /* Output BFD. */
7705 bfd *output_bfd;
7706 /* Symbol string table. */
7707 struct elf_strtab_hash *symstrtab;
7708 /* .hash section. */
7709 asection *hash_sec;
7710 /* symbol version section (.gnu.version). */
7711 asection *symver_sec;
7712 /* Buffer large enough to hold contents of any section. */
7713 bfd_byte *contents;
7714 /* Buffer large enough to hold external relocs of any section. */
7715 void *external_relocs;
7716 /* Buffer large enough to hold internal relocs of any section. */
7717 Elf_Internal_Rela *internal_relocs;
7718 /* Buffer large enough to hold external local symbols of any input
7719 BFD. */
7720 bfd_byte *external_syms;
7721 /* And a buffer for symbol section indices. */
7722 Elf_External_Sym_Shndx *locsym_shndx;
7723 /* Buffer large enough to hold internal local symbols of any input
7724 BFD. */
7725 Elf_Internal_Sym *internal_syms;
7726 /* Array large enough to hold a symbol index for each local symbol
7727 of any input BFD. */
7728 long *indices;
7729 /* Array large enough to hold a section pointer for each local
7730 symbol of any input BFD. */
7731 asection **sections;
7732 /* Buffer for SHT_SYMTAB_SHNDX section. */
7733 Elf_External_Sym_Shndx *symshndxbuf;
7734 /* Number of STT_FILE syms seen. */
7735 size_t filesym_count;
7736 };
7737
7738 /* This struct is used to pass information to elf_link_output_extsym. */
7739
7740 struct elf_outext_info
7741 {
7742 bfd_boolean failed;
7743 bfd_boolean localsyms;
7744 bfd_boolean file_sym_done;
7745 struct elf_final_link_info *flinfo;
7746 };
7747
7748
7749 /* Support for evaluating a complex relocation.
7750
7751 Complex relocations are generalized, self-describing relocations. The
7752 implementation of them consists of two parts: complex symbols, and the
7753 relocations themselves.
7754
7755 The relocations are use a reserved elf-wide relocation type code (R_RELC
7756 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7757 information (start bit, end bit, word width, etc) into the addend. This
7758 information is extracted from CGEN-generated operand tables within gas.
7759
7760 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7761 internal) representing prefix-notation expressions, including but not
7762 limited to those sorts of expressions normally encoded as addends in the
7763 addend field. The symbol mangling format is:
7764
7765 <node> := <literal>
7766 | <unary-operator> ':' <node>
7767 | <binary-operator> ':' <node> ':' <node>
7768 ;
7769
7770 <literal> := 's' <digits=N> ':' <N character symbol name>
7771 | 'S' <digits=N> ':' <N character section name>
7772 | '#' <hexdigits>
7773 ;
7774
7775 <binary-operator> := as in C
7776 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7777
7778 static void
7779 set_symbol_value (bfd *bfd_with_globals,
7780 Elf_Internal_Sym *isymbuf,
7781 size_t locsymcount,
7782 size_t symidx,
7783 bfd_vma val)
7784 {
7785 struct elf_link_hash_entry **sym_hashes;
7786 struct elf_link_hash_entry *h;
7787 size_t extsymoff = locsymcount;
7788
7789 if (symidx < locsymcount)
7790 {
7791 Elf_Internal_Sym *sym;
7792
7793 sym = isymbuf + symidx;
7794 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7795 {
7796 /* It is a local symbol: move it to the
7797 "absolute" section and give it a value. */
7798 sym->st_shndx = SHN_ABS;
7799 sym->st_value = val;
7800 return;
7801 }
7802 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7803 extsymoff = 0;
7804 }
7805
7806 /* It is a global symbol: set its link type
7807 to "defined" and give it a value. */
7808
7809 sym_hashes = elf_sym_hashes (bfd_with_globals);
7810 h = sym_hashes [symidx - extsymoff];
7811 while (h->root.type == bfd_link_hash_indirect
7812 || h->root.type == bfd_link_hash_warning)
7813 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7814 h->root.type = bfd_link_hash_defined;
7815 h->root.u.def.value = val;
7816 h->root.u.def.section = bfd_abs_section_ptr;
7817 }
7818
7819 static bfd_boolean
7820 resolve_symbol (const char *name,
7821 bfd *input_bfd,
7822 struct elf_final_link_info *flinfo,
7823 bfd_vma *result,
7824 Elf_Internal_Sym *isymbuf,
7825 size_t locsymcount)
7826 {
7827 Elf_Internal_Sym *sym;
7828 struct bfd_link_hash_entry *global_entry;
7829 const char *candidate = NULL;
7830 Elf_Internal_Shdr *symtab_hdr;
7831 size_t i;
7832
7833 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7834
7835 for (i = 0; i < locsymcount; ++ i)
7836 {
7837 sym = isymbuf + i;
7838
7839 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7840 continue;
7841
7842 candidate = bfd_elf_string_from_elf_section (input_bfd,
7843 symtab_hdr->sh_link,
7844 sym->st_name);
7845 #ifdef DEBUG
7846 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7847 name, candidate, (unsigned long) sym->st_value);
7848 #endif
7849 if (candidate && strcmp (candidate, name) == 0)
7850 {
7851 asection *sec = flinfo->sections [i];
7852
7853 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7854 *result += sec->output_offset + sec->output_section->vma;
7855 #ifdef DEBUG
7856 printf ("Found symbol with value %8.8lx\n",
7857 (unsigned long) *result);
7858 #endif
7859 return TRUE;
7860 }
7861 }
7862
7863 /* Hmm, haven't found it yet. perhaps it is a global. */
7864 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
7865 FALSE, FALSE, TRUE);
7866 if (!global_entry)
7867 return FALSE;
7868
7869 if (global_entry->type == bfd_link_hash_defined
7870 || global_entry->type == bfd_link_hash_defweak)
7871 {
7872 *result = (global_entry->u.def.value
7873 + global_entry->u.def.section->output_section->vma
7874 + global_entry->u.def.section->output_offset);
7875 #ifdef DEBUG
7876 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7877 global_entry->root.string, (unsigned long) *result);
7878 #endif
7879 return TRUE;
7880 }
7881
7882 return FALSE;
7883 }
7884
7885 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
7886 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
7887 names like "foo.end" which is the end address of section "foo". */
7888
7889 static bfd_boolean
7890 resolve_section (const char *name,
7891 asection *sections,
7892 bfd_vma *result,
7893 bfd * abfd)
7894 {
7895 asection *curr;
7896 unsigned int len;
7897
7898 for (curr = sections; curr; curr = curr->next)
7899 if (strcmp (curr->name, name) == 0)
7900 {
7901 *result = curr->vma;
7902 return TRUE;
7903 }
7904
7905 /* Hmm. still haven't found it. try pseudo-section names. */
7906 /* FIXME: This could be coded more efficiently... */
7907 for (curr = sections; curr; curr = curr->next)
7908 {
7909 len = strlen (curr->name);
7910 if (len > strlen (name))
7911 continue;
7912
7913 if (strncmp (curr->name, name, len) == 0)
7914 {
7915 if (strncmp (".end", name + len, 4) == 0)
7916 {
7917 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
7918 return TRUE;
7919 }
7920
7921 /* Insert more pseudo-section names here, if you like. */
7922 }
7923 }
7924
7925 return FALSE;
7926 }
7927
7928 static void
7929 undefined_reference (const char *reftype, const char *name)
7930 {
7931 /* xgettext:c-format */
7932 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7933 reftype, name);
7934 }
7935
7936 static bfd_boolean
7937 eval_symbol (bfd_vma *result,
7938 const char **symp,
7939 bfd *input_bfd,
7940 struct elf_final_link_info *flinfo,
7941 bfd_vma dot,
7942 Elf_Internal_Sym *isymbuf,
7943 size_t locsymcount,
7944 int signed_p)
7945 {
7946 size_t len;
7947 size_t symlen;
7948 bfd_vma a;
7949 bfd_vma b;
7950 char symbuf[4096];
7951 const char *sym = *symp;
7952 const char *symend;
7953 bfd_boolean symbol_is_section = FALSE;
7954
7955 len = strlen (sym);
7956 symend = sym + len;
7957
7958 if (len < 1 || len > sizeof (symbuf))
7959 {
7960 bfd_set_error (bfd_error_invalid_operation);
7961 return FALSE;
7962 }
7963
7964 switch (* sym)
7965 {
7966 case '.':
7967 *result = dot;
7968 *symp = sym + 1;
7969 return TRUE;
7970
7971 case '#':
7972 ++sym;
7973 *result = strtoul (sym, (char **) symp, 16);
7974 return TRUE;
7975
7976 case 'S':
7977 symbol_is_section = TRUE;
7978 /* Fall through. */
7979 case 's':
7980 ++sym;
7981 symlen = strtol (sym, (char **) symp, 10);
7982 sym = *symp + 1; /* Skip the trailing ':'. */
7983
7984 if (symend < sym || symlen + 1 > sizeof (symbuf))
7985 {
7986 bfd_set_error (bfd_error_invalid_operation);
7987 return FALSE;
7988 }
7989
7990 memcpy (symbuf, sym, symlen);
7991 symbuf[symlen] = '\0';
7992 *symp = sym + symlen;
7993
7994 /* Is it always possible, with complex symbols, that gas "mis-guessed"
7995 the symbol as a section, or vice-versa. so we're pretty liberal in our
7996 interpretation here; section means "try section first", not "must be a
7997 section", and likewise with symbol. */
7998
7999 if (symbol_is_section)
8000 {
8001 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8002 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8003 isymbuf, locsymcount))
8004 {
8005 undefined_reference ("section", symbuf);
8006 return FALSE;
8007 }
8008 }
8009 else
8010 {
8011 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8012 isymbuf, locsymcount)
8013 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8014 result, input_bfd))
8015 {
8016 undefined_reference ("symbol", symbuf);
8017 return FALSE;
8018 }
8019 }
8020
8021 return TRUE;
8022
8023 /* All that remains are operators. */
8024
8025 #define UNARY_OP(op) \
8026 if (strncmp (sym, #op, strlen (#op)) == 0) \
8027 { \
8028 sym += strlen (#op); \
8029 if (*sym == ':') \
8030 ++sym; \
8031 *symp = sym; \
8032 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8033 isymbuf, locsymcount, signed_p)) \
8034 return FALSE; \
8035 if (signed_p) \
8036 *result = op ((bfd_signed_vma) a); \
8037 else \
8038 *result = op a; \
8039 return TRUE; \
8040 }
8041
8042 #define BINARY_OP(op) \
8043 if (strncmp (sym, #op, strlen (#op)) == 0) \
8044 { \
8045 sym += strlen (#op); \
8046 if (*sym == ':') \
8047 ++sym; \
8048 *symp = sym; \
8049 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8050 isymbuf, locsymcount, signed_p)) \
8051 return FALSE; \
8052 ++*symp; \
8053 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8054 isymbuf, locsymcount, signed_p)) \
8055 return FALSE; \
8056 if (signed_p) \
8057 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8058 else \
8059 *result = a op b; \
8060 return TRUE; \
8061 }
8062
8063 default:
8064 UNARY_OP (0-);
8065 BINARY_OP (<<);
8066 BINARY_OP (>>);
8067 BINARY_OP (==);
8068 BINARY_OP (!=);
8069 BINARY_OP (<=);
8070 BINARY_OP (>=);
8071 BINARY_OP (&&);
8072 BINARY_OP (||);
8073 UNARY_OP (~);
8074 UNARY_OP (!);
8075 BINARY_OP (*);
8076 BINARY_OP (/);
8077 BINARY_OP (%);
8078 BINARY_OP (^);
8079 BINARY_OP (|);
8080 BINARY_OP (&);
8081 BINARY_OP (+);
8082 BINARY_OP (-);
8083 BINARY_OP (<);
8084 BINARY_OP (>);
8085 #undef UNARY_OP
8086 #undef BINARY_OP
8087 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8088 bfd_set_error (bfd_error_invalid_operation);
8089 return FALSE;
8090 }
8091 }
8092
8093 static void
8094 put_value (bfd_vma size,
8095 unsigned long chunksz,
8096 bfd *input_bfd,
8097 bfd_vma x,
8098 bfd_byte *location)
8099 {
8100 location += (size - chunksz);
8101
8102 for (; size; size -= chunksz, location -= chunksz)
8103 {
8104 switch (chunksz)
8105 {
8106 case 1:
8107 bfd_put_8 (input_bfd, x, location);
8108 x >>= 8;
8109 break;
8110 case 2:
8111 bfd_put_16 (input_bfd, x, location);
8112 x >>= 16;
8113 break;
8114 case 4:
8115 bfd_put_32 (input_bfd, x, location);
8116 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8117 x >>= 16;
8118 x >>= 16;
8119 break;
8120 #ifdef BFD64
8121 case 8:
8122 bfd_put_64 (input_bfd, x, location);
8123 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8124 x >>= 32;
8125 x >>= 32;
8126 break;
8127 #endif
8128 default:
8129 abort ();
8130 break;
8131 }
8132 }
8133 }
8134
8135 static bfd_vma
8136 get_value (bfd_vma size,
8137 unsigned long chunksz,
8138 bfd *input_bfd,
8139 bfd_byte *location)
8140 {
8141 int shift;
8142 bfd_vma x = 0;
8143
8144 /* Sanity checks. */
8145 BFD_ASSERT (chunksz <= sizeof (x)
8146 && size >= chunksz
8147 && chunksz != 0
8148 && (size % chunksz) == 0
8149 && input_bfd != NULL
8150 && location != NULL);
8151
8152 if (chunksz == sizeof (x))
8153 {
8154 BFD_ASSERT (size == chunksz);
8155
8156 /* Make sure that we do not perform an undefined shift operation.
8157 We know that size == chunksz so there will only be one iteration
8158 of the loop below. */
8159 shift = 0;
8160 }
8161 else
8162 shift = 8 * chunksz;
8163
8164 for (; size; size -= chunksz, location += chunksz)
8165 {
8166 switch (chunksz)
8167 {
8168 case 1:
8169 x = (x << shift) | bfd_get_8 (input_bfd, location);
8170 break;
8171 case 2:
8172 x = (x << shift) | bfd_get_16 (input_bfd, location);
8173 break;
8174 case 4:
8175 x = (x << shift) | bfd_get_32 (input_bfd, location);
8176 break;
8177 #ifdef BFD64
8178 case 8:
8179 x = (x << shift) | bfd_get_64 (input_bfd, location);
8180 break;
8181 #endif
8182 default:
8183 abort ();
8184 }
8185 }
8186 return x;
8187 }
8188
8189 static void
8190 decode_complex_addend (unsigned long *start, /* in bits */
8191 unsigned long *oplen, /* in bits */
8192 unsigned long *len, /* in bits */
8193 unsigned long *wordsz, /* in bytes */
8194 unsigned long *chunksz, /* in bytes */
8195 unsigned long *lsb0_p,
8196 unsigned long *signed_p,
8197 unsigned long *trunc_p,
8198 unsigned long encoded)
8199 {
8200 * start = encoded & 0x3F;
8201 * len = (encoded >> 6) & 0x3F;
8202 * oplen = (encoded >> 12) & 0x3F;
8203 * wordsz = (encoded >> 18) & 0xF;
8204 * chunksz = (encoded >> 22) & 0xF;
8205 * lsb0_p = (encoded >> 27) & 1;
8206 * signed_p = (encoded >> 28) & 1;
8207 * trunc_p = (encoded >> 29) & 1;
8208 }
8209
8210 bfd_reloc_status_type
8211 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8212 asection *input_section ATTRIBUTE_UNUSED,
8213 bfd_byte *contents,
8214 Elf_Internal_Rela *rel,
8215 bfd_vma relocation)
8216 {
8217 bfd_vma shift, x, mask;
8218 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8219 bfd_reloc_status_type r;
8220
8221 /* Perform this reloc, since it is complex.
8222 (this is not to say that it necessarily refers to a complex
8223 symbol; merely that it is a self-describing CGEN based reloc.
8224 i.e. the addend has the complete reloc information (bit start, end,
8225 word size, etc) encoded within it.). */
8226
8227 decode_complex_addend (&start, &oplen, &len, &wordsz,
8228 &chunksz, &lsb0_p, &signed_p,
8229 &trunc_p, rel->r_addend);
8230
8231 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8232
8233 if (lsb0_p)
8234 shift = (start + 1) - len;
8235 else
8236 shift = (8 * wordsz) - (start + len);
8237
8238 x = get_value (wordsz, chunksz, input_bfd,
8239 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8240
8241 #ifdef DEBUG
8242 printf ("Doing complex reloc: "
8243 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8244 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8245 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8246 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8247 oplen, (unsigned long) x, (unsigned long) mask,
8248 (unsigned long) relocation);
8249 #endif
8250
8251 r = bfd_reloc_ok;
8252 if (! trunc_p)
8253 /* Now do an overflow check. */
8254 r = bfd_check_overflow ((signed_p
8255 ? complain_overflow_signed
8256 : complain_overflow_unsigned),
8257 len, 0, (8 * wordsz),
8258 relocation);
8259
8260 /* Do the deed. */
8261 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8262
8263 #ifdef DEBUG
8264 printf (" relocation: %8.8lx\n"
8265 " shifted mask: %8.8lx\n"
8266 " shifted/masked reloc: %8.8lx\n"
8267 " result: %8.8lx\n",
8268 (unsigned long) relocation, (unsigned long) (mask << shift),
8269 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8270 #endif
8271 put_value (wordsz, chunksz, input_bfd, x,
8272 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8273 return r;
8274 }
8275
8276 /* Functions to read r_offset from external (target order) reloc
8277 entry. Faster than bfd_getl32 et al, because we let the compiler
8278 know the value is aligned. */
8279
8280 static bfd_vma
8281 ext32l_r_offset (const void *p)
8282 {
8283 union aligned32
8284 {
8285 uint32_t v;
8286 unsigned char c[4];
8287 };
8288 const union aligned32 *a
8289 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8290
8291 uint32_t aval = ( (uint32_t) a->c[0]
8292 | (uint32_t) a->c[1] << 8
8293 | (uint32_t) a->c[2] << 16
8294 | (uint32_t) a->c[3] << 24);
8295 return aval;
8296 }
8297
8298 static bfd_vma
8299 ext32b_r_offset (const void *p)
8300 {
8301 union aligned32
8302 {
8303 uint32_t v;
8304 unsigned char c[4];
8305 };
8306 const union aligned32 *a
8307 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8308
8309 uint32_t aval = ( (uint32_t) a->c[0] << 24
8310 | (uint32_t) a->c[1] << 16
8311 | (uint32_t) a->c[2] << 8
8312 | (uint32_t) a->c[3]);
8313 return aval;
8314 }
8315
8316 #ifdef BFD_HOST_64_BIT
8317 static bfd_vma
8318 ext64l_r_offset (const void *p)
8319 {
8320 union aligned64
8321 {
8322 uint64_t v;
8323 unsigned char c[8];
8324 };
8325 const union aligned64 *a
8326 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8327
8328 uint64_t aval = ( (uint64_t) a->c[0]
8329 | (uint64_t) a->c[1] << 8
8330 | (uint64_t) a->c[2] << 16
8331 | (uint64_t) a->c[3] << 24
8332 | (uint64_t) a->c[4] << 32
8333 | (uint64_t) a->c[5] << 40
8334 | (uint64_t) a->c[6] << 48
8335 | (uint64_t) a->c[7] << 56);
8336 return aval;
8337 }
8338
8339 static bfd_vma
8340 ext64b_r_offset (const void *p)
8341 {
8342 union aligned64
8343 {
8344 uint64_t v;
8345 unsigned char c[8];
8346 };
8347 const union aligned64 *a
8348 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8349
8350 uint64_t aval = ( (uint64_t) a->c[0] << 56
8351 | (uint64_t) a->c[1] << 48
8352 | (uint64_t) a->c[2] << 40
8353 | (uint64_t) a->c[3] << 32
8354 | (uint64_t) a->c[4] << 24
8355 | (uint64_t) a->c[5] << 16
8356 | (uint64_t) a->c[6] << 8
8357 | (uint64_t) a->c[7]);
8358 return aval;
8359 }
8360 #endif
8361
8362 /* When performing a relocatable link, the input relocations are
8363 preserved. But, if they reference global symbols, the indices
8364 referenced must be updated. Update all the relocations found in
8365 RELDATA. */
8366
8367 static bfd_boolean
8368 elf_link_adjust_relocs (bfd *abfd,
8369 asection *sec,
8370 struct bfd_elf_section_reloc_data *reldata,
8371 bfd_boolean sort)
8372 {
8373 unsigned int i;
8374 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8375 bfd_byte *erela;
8376 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8377 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8378 bfd_vma r_type_mask;
8379 int r_sym_shift;
8380 unsigned int count = reldata->count;
8381 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8382
8383 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8384 {
8385 swap_in = bed->s->swap_reloc_in;
8386 swap_out = bed->s->swap_reloc_out;
8387 }
8388 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8389 {
8390 swap_in = bed->s->swap_reloca_in;
8391 swap_out = bed->s->swap_reloca_out;
8392 }
8393 else
8394 abort ();
8395
8396 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8397 abort ();
8398
8399 if (bed->s->arch_size == 32)
8400 {
8401 r_type_mask = 0xff;
8402 r_sym_shift = 8;
8403 }
8404 else
8405 {
8406 r_type_mask = 0xffffffff;
8407 r_sym_shift = 32;
8408 }
8409
8410 erela = reldata->hdr->contents;
8411 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8412 {
8413 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8414 unsigned int j;
8415
8416 if (*rel_hash == NULL)
8417 continue;
8418
8419 BFD_ASSERT ((*rel_hash)->indx >= 0);
8420
8421 (*swap_in) (abfd, erela, irela);
8422 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8423 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8424 | (irela[j].r_info & r_type_mask));
8425 (*swap_out) (abfd, irela, erela);
8426 }
8427
8428 if (bed->elf_backend_update_relocs)
8429 (*bed->elf_backend_update_relocs) (sec, reldata);
8430
8431 if (sort && count != 0)
8432 {
8433 bfd_vma (*ext_r_off) (const void *);
8434 bfd_vma r_off;
8435 size_t elt_size;
8436 bfd_byte *base, *end, *p, *loc;
8437 bfd_byte *buf = NULL;
8438
8439 if (bed->s->arch_size == 32)
8440 {
8441 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8442 ext_r_off = ext32l_r_offset;
8443 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8444 ext_r_off = ext32b_r_offset;
8445 else
8446 abort ();
8447 }
8448 else
8449 {
8450 #ifdef BFD_HOST_64_BIT
8451 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8452 ext_r_off = ext64l_r_offset;
8453 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8454 ext_r_off = ext64b_r_offset;
8455 else
8456 #endif
8457 abort ();
8458 }
8459
8460 /* Must use a stable sort here. A modified insertion sort,
8461 since the relocs are mostly sorted already. */
8462 elt_size = reldata->hdr->sh_entsize;
8463 base = reldata->hdr->contents;
8464 end = base + count * elt_size;
8465 if (elt_size > sizeof (Elf64_External_Rela))
8466 abort ();
8467
8468 /* Ensure the first element is lowest. This acts as a sentinel,
8469 speeding the main loop below. */
8470 r_off = (*ext_r_off) (base);
8471 for (p = loc = base; (p += elt_size) < end; )
8472 {
8473 bfd_vma r_off2 = (*ext_r_off) (p);
8474 if (r_off > r_off2)
8475 {
8476 r_off = r_off2;
8477 loc = p;
8478 }
8479 }
8480 if (loc != base)
8481 {
8482 /* Don't just swap *base and *loc as that changes the order
8483 of the original base[0] and base[1] if they happen to
8484 have the same r_offset. */
8485 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8486 memcpy (onebuf, loc, elt_size);
8487 memmove (base + elt_size, base, loc - base);
8488 memcpy (base, onebuf, elt_size);
8489 }
8490
8491 for (p = base + elt_size; (p += elt_size) < end; )
8492 {
8493 /* base to p is sorted, *p is next to insert. */
8494 r_off = (*ext_r_off) (p);
8495 /* Search the sorted region for location to insert. */
8496 loc = p - elt_size;
8497 while (r_off < (*ext_r_off) (loc))
8498 loc -= elt_size;
8499 loc += elt_size;
8500 if (loc != p)
8501 {
8502 /* Chances are there is a run of relocs to insert here,
8503 from one of more input files. Files are not always
8504 linked in order due to the way elf_link_input_bfd is
8505 called. See pr17666. */
8506 size_t sortlen = p - loc;
8507 bfd_vma r_off2 = (*ext_r_off) (loc);
8508 size_t runlen = elt_size;
8509 size_t buf_size = 96 * 1024;
8510 while (p + runlen < end
8511 && (sortlen <= buf_size
8512 || runlen + elt_size <= buf_size)
8513 && r_off2 > (*ext_r_off) (p + runlen))
8514 runlen += elt_size;
8515 if (buf == NULL)
8516 {
8517 buf = bfd_malloc (buf_size);
8518 if (buf == NULL)
8519 return FALSE;
8520 }
8521 if (runlen < sortlen)
8522 {
8523 memcpy (buf, p, runlen);
8524 memmove (loc + runlen, loc, sortlen);
8525 memcpy (loc, buf, runlen);
8526 }
8527 else
8528 {
8529 memcpy (buf, loc, sortlen);
8530 memmove (loc, p, runlen);
8531 memcpy (loc + runlen, buf, sortlen);
8532 }
8533 p += runlen - elt_size;
8534 }
8535 }
8536 /* Hashes are no longer valid. */
8537 free (reldata->hashes);
8538 reldata->hashes = NULL;
8539 free (buf);
8540 }
8541 return TRUE;
8542 }
8543
8544 struct elf_link_sort_rela
8545 {
8546 union {
8547 bfd_vma offset;
8548 bfd_vma sym_mask;
8549 } u;
8550 enum elf_reloc_type_class type;
8551 /* We use this as an array of size int_rels_per_ext_rel. */
8552 Elf_Internal_Rela rela[1];
8553 };
8554
8555 static int
8556 elf_link_sort_cmp1 (const void *A, const void *B)
8557 {
8558 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8559 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8560 int relativea, relativeb;
8561
8562 relativea = a->type == reloc_class_relative;
8563 relativeb = b->type == reloc_class_relative;
8564
8565 if (relativea < relativeb)
8566 return 1;
8567 if (relativea > relativeb)
8568 return -1;
8569 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8570 return -1;
8571 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8572 return 1;
8573 if (a->rela->r_offset < b->rela->r_offset)
8574 return -1;
8575 if (a->rela->r_offset > b->rela->r_offset)
8576 return 1;
8577 return 0;
8578 }
8579
8580 static int
8581 elf_link_sort_cmp2 (const void *A, const void *B)
8582 {
8583 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8584 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8585
8586 if (a->type < b->type)
8587 return -1;
8588 if (a->type > b->type)
8589 return 1;
8590 if (a->u.offset < b->u.offset)
8591 return -1;
8592 if (a->u.offset > b->u.offset)
8593 return 1;
8594 if (a->rela->r_offset < b->rela->r_offset)
8595 return -1;
8596 if (a->rela->r_offset > b->rela->r_offset)
8597 return 1;
8598 return 0;
8599 }
8600
8601 static size_t
8602 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8603 {
8604 asection *dynamic_relocs;
8605 asection *rela_dyn;
8606 asection *rel_dyn;
8607 bfd_size_type count, size;
8608 size_t i, ret, sort_elt, ext_size;
8609 bfd_byte *sort, *s_non_relative, *p;
8610 struct elf_link_sort_rela *sq;
8611 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8612 int i2e = bed->s->int_rels_per_ext_rel;
8613 unsigned int opb = bfd_octets_per_byte (abfd);
8614 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8615 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8616 struct bfd_link_order *lo;
8617 bfd_vma r_sym_mask;
8618 bfd_boolean use_rela;
8619
8620 /* Find a dynamic reloc section. */
8621 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8622 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8623 if (rela_dyn != NULL && rela_dyn->size > 0
8624 && rel_dyn != NULL && rel_dyn->size > 0)
8625 {
8626 bfd_boolean use_rela_initialised = FALSE;
8627
8628 /* This is just here to stop gcc from complaining.
8629 Its initialization checking code is not perfect. */
8630 use_rela = TRUE;
8631
8632 /* Both sections are present. Examine the sizes
8633 of the indirect sections to help us choose. */
8634 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8635 if (lo->type == bfd_indirect_link_order)
8636 {
8637 asection *o = lo->u.indirect.section;
8638
8639 if ((o->size % bed->s->sizeof_rela) == 0)
8640 {
8641 if ((o->size % bed->s->sizeof_rel) == 0)
8642 /* Section size is divisible by both rel and rela sizes.
8643 It is of no help to us. */
8644 ;
8645 else
8646 {
8647 /* Section size is only divisible by rela. */
8648 if (use_rela_initialised && (use_rela == FALSE))
8649 {
8650 _bfd_error_handler (_("%B: Unable to sort relocs - "
8651 "they are in more than one size"),
8652 abfd);
8653 bfd_set_error (bfd_error_invalid_operation);
8654 return 0;
8655 }
8656 else
8657 {
8658 use_rela = TRUE;
8659 use_rela_initialised = TRUE;
8660 }
8661 }
8662 }
8663 else if ((o->size % bed->s->sizeof_rel) == 0)
8664 {
8665 /* Section size is only divisible by rel. */
8666 if (use_rela_initialised && (use_rela == TRUE))
8667 {
8668 _bfd_error_handler (_("%B: Unable to sort relocs - "
8669 "they are in more than one size"),
8670 abfd);
8671 bfd_set_error (bfd_error_invalid_operation);
8672 return 0;
8673 }
8674 else
8675 {
8676 use_rela = FALSE;
8677 use_rela_initialised = TRUE;
8678 }
8679 }
8680 else
8681 {
8682 /* The section size is not divisible by either -
8683 something is wrong. */
8684 _bfd_error_handler (_("%B: Unable to sort relocs - "
8685 "they are of an unknown size"), abfd);
8686 bfd_set_error (bfd_error_invalid_operation);
8687 return 0;
8688 }
8689 }
8690
8691 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8692 if (lo->type == bfd_indirect_link_order)
8693 {
8694 asection *o = lo->u.indirect.section;
8695
8696 if ((o->size % bed->s->sizeof_rela) == 0)
8697 {
8698 if ((o->size % bed->s->sizeof_rel) == 0)
8699 /* Section size is divisible by both rel and rela sizes.
8700 It is of no help to us. */
8701 ;
8702 else
8703 {
8704 /* Section size is only divisible by rela. */
8705 if (use_rela_initialised && (use_rela == FALSE))
8706 {
8707 _bfd_error_handler (_("%B: Unable to sort relocs - "
8708 "they are in more than one size"),
8709 abfd);
8710 bfd_set_error (bfd_error_invalid_operation);
8711 return 0;
8712 }
8713 else
8714 {
8715 use_rela = TRUE;
8716 use_rela_initialised = TRUE;
8717 }
8718 }
8719 }
8720 else if ((o->size % bed->s->sizeof_rel) == 0)
8721 {
8722 /* Section size is only divisible by rel. */
8723 if (use_rela_initialised && (use_rela == TRUE))
8724 {
8725 _bfd_error_handler (_("%B: Unable to sort relocs - "
8726 "they are in more than one size"),
8727 abfd);
8728 bfd_set_error (bfd_error_invalid_operation);
8729 return 0;
8730 }
8731 else
8732 {
8733 use_rela = FALSE;
8734 use_rela_initialised = TRUE;
8735 }
8736 }
8737 else
8738 {
8739 /* The section size is not divisible by either -
8740 something is wrong. */
8741 _bfd_error_handler (_("%B: Unable to sort relocs - "
8742 "they are of an unknown size"), abfd);
8743 bfd_set_error (bfd_error_invalid_operation);
8744 return 0;
8745 }
8746 }
8747
8748 if (! use_rela_initialised)
8749 /* Make a guess. */
8750 use_rela = TRUE;
8751 }
8752 else if (rela_dyn != NULL && rela_dyn->size > 0)
8753 use_rela = TRUE;
8754 else if (rel_dyn != NULL && rel_dyn->size > 0)
8755 use_rela = FALSE;
8756 else
8757 return 0;
8758
8759 if (use_rela)
8760 {
8761 dynamic_relocs = rela_dyn;
8762 ext_size = bed->s->sizeof_rela;
8763 swap_in = bed->s->swap_reloca_in;
8764 swap_out = bed->s->swap_reloca_out;
8765 }
8766 else
8767 {
8768 dynamic_relocs = rel_dyn;
8769 ext_size = bed->s->sizeof_rel;
8770 swap_in = bed->s->swap_reloc_in;
8771 swap_out = bed->s->swap_reloc_out;
8772 }
8773
8774 size = 0;
8775 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8776 if (lo->type == bfd_indirect_link_order)
8777 size += lo->u.indirect.section->size;
8778
8779 if (size != dynamic_relocs->size)
8780 return 0;
8781
8782 sort_elt = (sizeof (struct elf_link_sort_rela)
8783 + (i2e - 1) * sizeof (Elf_Internal_Rela));
8784
8785 count = dynamic_relocs->size / ext_size;
8786 if (count == 0)
8787 return 0;
8788 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
8789
8790 if (sort == NULL)
8791 {
8792 (*info->callbacks->warning)
8793 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8794 return 0;
8795 }
8796
8797 if (bed->s->arch_size == 32)
8798 r_sym_mask = ~(bfd_vma) 0xff;
8799 else
8800 r_sym_mask = ~(bfd_vma) 0xffffffff;
8801
8802 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8803 if (lo->type == bfd_indirect_link_order)
8804 {
8805 bfd_byte *erel, *erelend;
8806 asection *o = lo->u.indirect.section;
8807
8808 if (o->contents == NULL && o->size != 0)
8809 {
8810 /* This is a reloc section that is being handled as a normal
8811 section. See bfd_section_from_shdr. We can't combine
8812 relocs in this case. */
8813 free (sort);
8814 return 0;
8815 }
8816 erel = o->contents;
8817 erelend = o->contents + o->size;
8818 p = sort + o->output_offset * opb / ext_size * sort_elt;
8819
8820 while (erel < erelend)
8821 {
8822 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8823
8824 (*swap_in) (abfd, erel, s->rela);
8825 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
8826 s->u.sym_mask = r_sym_mask;
8827 p += sort_elt;
8828 erel += ext_size;
8829 }
8830 }
8831
8832 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8833
8834 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8835 {
8836 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8837 if (s->type != reloc_class_relative)
8838 break;
8839 }
8840 ret = i;
8841 s_non_relative = p;
8842
8843 sq = (struct elf_link_sort_rela *) s_non_relative;
8844 for (; i < count; i++, p += sort_elt)
8845 {
8846 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8847 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8848 sq = sp;
8849 sp->u.offset = sq->rela->r_offset;
8850 }
8851
8852 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8853
8854 struct elf_link_hash_table *htab = elf_hash_table (info);
8855 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
8856 {
8857 /* We have plt relocs in .rela.dyn. */
8858 sq = (struct elf_link_sort_rela *) sort;
8859 for (i = 0; i < count; i++)
8860 if (sq[count - i - 1].type != reloc_class_plt)
8861 break;
8862 if (i != 0 && htab->srelplt->size == i * ext_size)
8863 {
8864 struct bfd_link_order **plo;
8865 /* Put srelplt link_order last. This is so the output_offset
8866 set in the next loop is correct for DT_JMPREL. */
8867 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
8868 if ((*plo)->type == bfd_indirect_link_order
8869 && (*plo)->u.indirect.section == htab->srelplt)
8870 {
8871 lo = *plo;
8872 *plo = lo->next;
8873 }
8874 else
8875 plo = &(*plo)->next;
8876 *plo = lo;
8877 lo->next = NULL;
8878 dynamic_relocs->map_tail.link_order = lo;
8879 }
8880 }
8881
8882 p = sort;
8883 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8884 if (lo->type == bfd_indirect_link_order)
8885 {
8886 bfd_byte *erel, *erelend;
8887 asection *o = lo->u.indirect.section;
8888
8889 erel = o->contents;
8890 erelend = o->contents + o->size;
8891 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
8892 while (erel < erelend)
8893 {
8894 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8895 (*swap_out) (abfd, s->rela, erel);
8896 p += sort_elt;
8897 erel += ext_size;
8898 }
8899 }
8900
8901 free (sort);
8902 *psec = dynamic_relocs;
8903 return ret;
8904 }
8905
8906 /* Add a symbol to the output symbol string table. */
8907
8908 static int
8909 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
8910 const char *name,
8911 Elf_Internal_Sym *elfsym,
8912 asection *input_sec,
8913 struct elf_link_hash_entry *h)
8914 {
8915 int (*output_symbol_hook)
8916 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8917 struct elf_link_hash_entry *);
8918 struct elf_link_hash_table *hash_table;
8919 const struct elf_backend_data *bed;
8920 bfd_size_type strtabsize;
8921
8922 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8923
8924 bed = get_elf_backend_data (flinfo->output_bfd);
8925 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8926 if (output_symbol_hook != NULL)
8927 {
8928 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
8929 if (ret != 1)
8930 return ret;
8931 }
8932
8933 if (name == NULL
8934 || *name == '\0'
8935 || (input_sec->flags & SEC_EXCLUDE))
8936 elfsym->st_name = (unsigned long) -1;
8937 else
8938 {
8939 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8940 to get the final offset for st_name. */
8941 elfsym->st_name
8942 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
8943 name, FALSE);
8944 if (elfsym->st_name == (unsigned long) -1)
8945 return 0;
8946 }
8947
8948 hash_table = elf_hash_table (flinfo->info);
8949 strtabsize = hash_table->strtabsize;
8950 if (strtabsize <= hash_table->strtabcount)
8951 {
8952 strtabsize += strtabsize;
8953 hash_table->strtabsize = strtabsize;
8954 strtabsize *= sizeof (*hash_table->strtab);
8955 hash_table->strtab
8956 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
8957 strtabsize);
8958 if (hash_table->strtab == NULL)
8959 return 0;
8960 }
8961 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
8962 hash_table->strtab[hash_table->strtabcount].dest_index
8963 = hash_table->strtabcount;
8964 hash_table->strtab[hash_table->strtabcount].destshndx_index
8965 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
8966
8967 bfd_get_symcount (flinfo->output_bfd) += 1;
8968 hash_table->strtabcount += 1;
8969
8970 return 1;
8971 }
8972
8973 /* Swap symbols out to the symbol table and flush the output symbols to
8974 the file. */
8975
8976 static bfd_boolean
8977 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
8978 {
8979 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
8980 bfd_size_type amt;
8981 size_t i;
8982 const struct elf_backend_data *bed;
8983 bfd_byte *symbuf;
8984 Elf_Internal_Shdr *hdr;
8985 file_ptr pos;
8986 bfd_boolean ret;
8987
8988 if (!hash_table->strtabcount)
8989 return TRUE;
8990
8991 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8992
8993 bed = get_elf_backend_data (flinfo->output_bfd);
8994
8995 amt = bed->s->sizeof_sym * hash_table->strtabcount;
8996 symbuf = (bfd_byte *) bfd_malloc (amt);
8997 if (symbuf == NULL)
8998 return FALSE;
8999
9000 if (flinfo->symshndxbuf)
9001 {
9002 amt = sizeof (Elf_External_Sym_Shndx);
9003 amt *= bfd_get_symcount (flinfo->output_bfd);
9004 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9005 if (flinfo->symshndxbuf == NULL)
9006 {
9007 free (symbuf);
9008 return FALSE;
9009 }
9010 }
9011
9012 for (i = 0; i < hash_table->strtabcount; i++)
9013 {
9014 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9015 if (elfsym->sym.st_name == (unsigned long) -1)
9016 elfsym->sym.st_name = 0;
9017 else
9018 elfsym->sym.st_name
9019 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9020 elfsym->sym.st_name);
9021 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9022 ((bfd_byte *) symbuf
9023 + (elfsym->dest_index
9024 * bed->s->sizeof_sym)),
9025 (flinfo->symshndxbuf
9026 + elfsym->destshndx_index));
9027 }
9028
9029 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9030 pos = hdr->sh_offset + hdr->sh_size;
9031 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9032 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9033 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9034 {
9035 hdr->sh_size += amt;
9036 ret = TRUE;
9037 }
9038 else
9039 ret = FALSE;
9040
9041 free (symbuf);
9042
9043 free (hash_table->strtab);
9044 hash_table->strtab = NULL;
9045
9046 return ret;
9047 }
9048
9049 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9050
9051 static bfd_boolean
9052 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9053 {
9054 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9055 && sym->st_shndx < SHN_LORESERVE)
9056 {
9057 /* The gABI doesn't support dynamic symbols in output sections
9058 beyond 64k. */
9059 _bfd_error_handler
9060 /* xgettext:c-format */
9061 (_("%B: Too many sections: %d (>= %d)"),
9062 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9063 bfd_set_error (bfd_error_nonrepresentable_section);
9064 return FALSE;
9065 }
9066 return TRUE;
9067 }
9068
9069 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9070 allowing an unsatisfied unversioned symbol in the DSO to match a
9071 versioned symbol that would normally require an explicit version.
9072 We also handle the case that a DSO references a hidden symbol
9073 which may be satisfied by a versioned symbol in another DSO. */
9074
9075 static bfd_boolean
9076 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9077 const struct elf_backend_data *bed,
9078 struct elf_link_hash_entry *h)
9079 {
9080 bfd *abfd;
9081 struct elf_link_loaded_list *loaded;
9082
9083 if (!is_elf_hash_table (info->hash))
9084 return FALSE;
9085
9086 /* Check indirect symbol. */
9087 while (h->root.type == bfd_link_hash_indirect)
9088 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9089
9090 switch (h->root.type)
9091 {
9092 default:
9093 abfd = NULL;
9094 break;
9095
9096 case bfd_link_hash_undefined:
9097 case bfd_link_hash_undefweak:
9098 abfd = h->root.u.undef.abfd;
9099 if (abfd == NULL
9100 || (abfd->flags & DYNAMIC) == 0
9101 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9102 return FALSE;
9103 break;
9104
9105 case bfd_link_hash_defined:
9106 case bfd_link_hash_defweak:
9107 abfd = h->root.u.def.section->owner;
9108 break;
9109
9110 case bfd_link_hash_common:
9111 abfd = h->root.u.c.p->section->owner;
9112 break;
9113 }
9114 BFD_ASSERT (abfd != NULL);
9115
9116 for (loaded = elf_hash_table (info)->loaded;
9117 loaded != NULL;
9118 loaded = loaded->next)
9119 {
9120 bfd *input;
9121 Elf_Internal_Shdr *hdr;
9122 size_t symcount;
9123 size_t extsymcount;
9124 size_t extsymoff;
9125 Elf_Internal_Shdr *versymhdr;
9126 Elf_Internal_Sym *isym;
9127 Elf_Internal_Sym *isymend;
9128 Elf_Internal_Sym *isymbuf;
9129 Elf_External_Versym *ever;
9130 Elf_External_Versym *extversym;
9131
9132 input = loaded->abfd;
9133
9134 /* We check each DSO for a possible hidden versioned definition. */
9135 if (input == abfd
9136 || (input->flags & DYNAMIC) == 0
9137 || elf_dynversym (input) == 0)
9138 continue;
9139
9140 hdr = &elf_tdata (input)->dynsymtab_hdr;
9141
9142 symcount = hdr->sh_size / bed->s->sizeof_sym;
9143 if (elf_bad_symtab (input))
9144 {
9145 extsymcount = symcount;
9146 extsymoff = 0;
9147 }
9148 else
9149 {
9150 extsymcount = symcount - hdr->sh_info;
9151 extsymoff = hdr->sh_info;
9152 }
9153
9154 if (extsymcount == 0)
9155 continue;
9156
9157 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9158 NULL, NULL, NULL);
9159 if (isymbuf == NULL)
9160 return FALSE;
9161
9162 /* Read in any version definitions. */
9163 versymhdr = &elf_tdata (input)->dynversym_hdr;
9164 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
9165 if (extversym == NULL)
9166 goto error_ret;
9167
9168 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9169 || (bfd_bread (extversym, versymhdr->sh_size, input)
9170 != versymhdr->sh_size))
9171 {
9172 free (extversym);
9173 error_ret:
9174 free (isymbuf);
9175 return FALSE;
9176 }
9177
9178 ever = extversym + extsymoff;
9179 isymend = isymbuf + extsymcount;
9180 for (isym = isymbuf; isym < isymend; isym++, ever++)
9181 {
9182 const char *name;
9183 Elf_Internal_Versym iver;
9184 unsigned short version_index;
9185
9186 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9187 || isym->st_shndx == SHN_UNDEF)
9188 continue;
9189
9190 name = bfd_elf_string_from_elf_section (input,
9191 hdr->sh_link,
9192 isym->st_name);
9193 if (strcmp (name, h->root.root.string) != 0)
9194 continue;
9195
9196 _bfd_elf_swap_versym_in (input, ever, &iver);
9197
9198 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9199 && !(h->def_regular
9200 && h->forced_local))
9201 {
9202 /* If we have a non-hidden versioned sym, then it should
9203 have provided a definition for the undefined sym unless
9204 it is defined in a non-shared object and forced local.
9205 */
9206 abort ();
9207 }
9208
9209 version_index = iver.vs_vers & VERSYM_VERSION;
9210 if (version_index == 1 || version_index == 2)
9211 {
9212 /* This is the base or first version. We can use it. */
9213 free (extversym);
9214 free (isymbuf);
9215 return TRUE;
9216 }
9217 }
9218
9219 free (extversym);
9220 free (isymbuf);
9221 }
9222
9223 return FALSE;
9224 }
9225
9226 /* Convert ELF common symbol TYPE. */
9227
9228 static int
9229 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9230 {
9231 /* Commom symbol can only appear in relocatable link. */
9232 if (!bfd_link_relocatable (info))
9233 abort ();
9234 switch (info->elf_stt_common)
9235 {
9236 case unchanged:
9237 break;
9238 case elf_stt_common:
9239 type = STT_COMMON;
9240 break;
9241 case no_elf_stt_common:
9242 type = STT_OBJECT;
9243 break;
9244 }
9245 return type;
9246 }
9247
9248 /* Add an external symbol to the symbol table. This is called from
9249 the hash table traversal routine. When generating a shared object,
9250 we go through the symbol table twice. The first time we output
9251 anything that might have been forced to local scope in a version
9252 script. The second time we output the symbols that are still
9253 global symbols. */
9254
9255 static bfd_boolean
9256 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9257 {
9258 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9259 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9260 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9261 bfd_boolean strip;
9262 Elf_Internal_Sym sym;
9263 asection *input_sec;
9264 const struct elf_backend_data *bed;
9265 long indx;
9266 int ret;
9267 unsigned int type;
9268
9269 if (h->root.type == bfd_link_hash_warning)
9270 {
9271 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9272 if (h->root.type == bfd_link_hash_new)
9273 return TRUE;
9274 }
9275
9276 /* Decide whether to output this symbol in this pass. */
9277 if (eoinfo->localsyms)
9278 {
9279 if (!h->forced_local)
9280 return TRUE;
9281 }
9282 else
9283 {
9284 if (h->forced_local)
9285 return TRUE;
9286 }
9287
9288 bed = get_elf_backend_data (flinfo->output_bfd);
9289
9290 if (h->root.type == bfd_link_hash_undefined)
9291 {
9292 /* If we have an undefined symbol reference here then it must have
9293 come from a shared library that is being linked in. (Undefined
9294 references in regular files have already been handled unless
9295 they are in unreferenced sections which are removed by garbage
9296 collection). */
9297 bfd_boolean ignore_undef = FALSE;
9298
9299 /* Some symbols may be special in that the fact that they're
9300 undefined can be safely ignored - let backend determine that. */
9301 if (bed->elf_backend_ignore_undef_symbol)
9302 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9303
9304 /* If we are reporting errors for this situation then do so now. */
9305 if (!ignore_undef
9306 && h->ref_dynamic
9307 && (!h->ref_regular || flinfo->info->gc_sections)
9308 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9309 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9310 (*flinfo->info->callbacks->undefined_symbol)
9311 (flinfo->info, h->root.root.string,
9312 h->ref_regular ? NULL : h->root.u.undef.abfd,
9313 NULL, 0,
9314 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
9315
9316 /* Strip a global symbol defined in a discarded section. */
9317 if (h->indx == -3)
9318 return TRUE;
9319 }
9320
9321 /* We should also warn if a forced local symbol is referenced from
9322 shared libraries. */
9323 if (bfd_link_executable (flinfo->info)
9324 && h->forced_local
9325 && h->ref_dynamic
9326 && h->def_regular
9327 && !h->dynamic_def
9328 && h->ref_dynamic_nonweak
9329 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9330 {
9331 bfd *def_bfd;
9332 const char *msg;
9333 struct elf_link_hash_entry *hi = h;
9334
9335 /* Check indirect symbol. */
9336 while (hi->root.type == bfd_link_hash_indirect)
9337 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9338
9339 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9340 /* xgettext:c-format */
9341 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9342 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9343 /* xgettext:c-format */
9344 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9345 else
9346 /* xgettext:c-format */
9347 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
9348 def_bfd = flinfo->output_bfd;
9349 if (hi->root.u.def.section != bfd_abs_section_ptr)
9350 def_bfd = hi->root.u.def.section->owner;
9351 _bfd_error_handler (msg, flinfo->output_bfd, def_bfd,
9352 h->root.root.string);
9353 bfd_set_error (bfd_error_bad_value);
9354 eoinfo->failed = TRUE;
9355 return FALSE;
9356 }
9357
9358 /* We don't want to output symbols that have never been mentioned by
9359 a regular file, or that we have been told to strip. However, if
9360 h->indx is set to -2, the symbol is used by a reloc and we must
9361 output it. */
9362 strip = FALSE;
9363 if (h->indx == -2)
9364 ;
9365 else if ((h->def_dynamic
9366 || h->ref_dynamic
9367 || h->root.type == bfd_link_hash_new)
9368 && !h->def_regular
9369 && !h->ref_regular)
9370 strip = TRUE;
9371 else if (flinfo->info->strip == strip_all)
9372 strip = TRUE;
9373 else if (flinfo->info->strip == strip_some
9374 && bfd_hash_lookup (flinfo->info->keep_hash,
9375 h->root.root.string, FALSE, FALSE) == NULL)
9376 strip = TRUE;
9377 else if ((h->root.type == bfd_link_hash_defined
9378 || h->root.type == bfd_link_hash_defweak)
9379 && ((flinfo->info->strip_discarded
9380 && discarded_section (h->root.u.def.section))
9381 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9382 && h->root.u.def.section->owner != NULL
9383 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9384 strip = TRUE;
9385 else if ((h->root.type == bfd_link_hash_undefined
9386 || h->root.type == bfd_link_hash_undefweak)
9387 && h->root.u.undef.abfd != NULL
9388 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9389 strip = TRUE;
9390
9391 type = h->type;
9392
9393 /* If we're stripping it, and it's not a dynamic symbol, there's
9394 nothing else to do. However, if it is a forced local symbol or
9395 an ifunc symbol we need to give the backend finish_dynamic_symbol
9396 function a chance to make it dynamic. */
9397 if (strip
9398 && h->dynindx == -1
9399 && type != STT_GNU_IFUNC
9400 && !h->forced_local)
9401 return TRUE;
9402
9403 sym.st_value = 0;
9404 sym.st_size = h->size;
9405 sym.st_other = h->other;
9406 switch (h->root.type)
9407 {
9408 default:
9409 case bfd_link_hash_new:
9410 case bfd_link_hash_warning:
9411 abort ();
9412 return FALSE;
9413
9414 case bfd_link_hash_undefined:
9415 case bfd_link_hash_undefweak:
9416 input_sec = bfd_und_section_ptr;
9417 sym.st_shndx = SHN_UNDEF;
9418 break;
9419
9420 case bfd_link_hash_defined:
9421 case bfd_link_hash_defweak:
9422 {
9423 input_sec = h->root.u.def.section;
9424 if (input_sec->output_section != NULL)
9425 {
9426 sym.st_shndx =
9427 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9428 input_sec->output_section);
9429 if (sym.st_shndx == SHN_BAD)
9430 {
9431 _bfd_error_handler
9432 /* xgettext:c-format */
9433 (_("%B: could not find output section %A for input section %A"),
9434 flinfo->output_bfd, input_sec->output_section, input_sec);
9435 bfd_set_error (bfd_error_nonrepresentable_section);
9436 eoinfo->failed = TRUE;
9437 return FALSE;
9438 }
9439
9440 /* ELF symbols in relocatable files are section relative,
9441 but in nonrelocatable files they are virtual
9442 addresses. */
9443 sym.st_value = h->root.u.def.value + input_sec->output_offset;
9444 if (!bfd_link_relocatable (flinfo->info))
9445 {
9446 sym.st_value += input_sec->output_section->vma;
9447 if (h->type == STT_TLS)
9448 {
9449 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9450 if (tls_sec != NULL)
9451 sym.st_value -= tls_sec->vma;
9452 }
9453 }
9454 }
9455 else
9456 {
9457 BFD_ASSERT (input_sec->owner == NULL
9458 || (input_sec->owner->flags & DYNAMIC) != 0);
9459 sym.st_shndx = SHN_UNDEF;
9460 input_sec = bfd_und_section_ptr;
9461 }
9462 }
9463 break;
9464
9465 case bfd_link_hash_common:
9466 input_sec = h->root.u.c.p->section;
9467 sym.st_shndx = bed->common_section_index (input_sec);
9468 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9469 break;
9470
9471 case bfd_link_hash_indirect:
9472 /* These symbols are created by symbol versioning. They point
9473 to the decorated version of the name. For example, if the
9474 symbol foo@@GNU_1.2 is the default, which should be used when
9475 foo is used with no version, then we add an indirect symbol
9476 foo which points to foo@@GNU_1.2. We ignore these symbols,
9477 since the indirected symbol is already in the hash table. */
9478 return TRUE;
9479 }
9480
9481 if (type == STT_COMMON || type == STT_OBJECT)
9482 switch (h->root.type)
9483 {
9484 case bfd_link_hash_common:
9485 type = elf_link_convert_common_type (flinfo->info, type);
9486 break;
9487 case bfd_link_hash_defined:
9488 case bfd_link_hash_defweak:
9489 if (bed->common_definition (&sym))
9490 type = elf_link_convert_common_type (flinfo->info, type);
9491 else
9492 type = STT_OBJECT;
9493 break;
9494 case bfd_link_hash_undefined:
9495 case bfd_link_hash_undefweak:
9496 break;
9497 default:
9498 abort ();
9499 }
9500
9501 if (h->forced_local)
9502 {
9503 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9504 /* Turn off visibility on local symbol. */
9505 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9506 }
9507 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9508 else if (h->unique_global && h->def_regular)
9509 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9510 else if (h->root.type == bfd_link_hash_undefweak
9511 || h->root.type == bfd_link_hash_defweak)
9512 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9513 else
9514 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9515 sym.st_target_internal = h->target_internal;
9516
9517 /* Give the processor backend a chance to tweak the symbol value,
9518 and also to finish up anything that needs to be done for this
9519 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9520 forced local syms when non-shared is due to a historical quirk.
9521 STT_GNU_IFUNC symbol must go through PLT. */
9522 if ((h->type == STT_GNU_IFUNC
9523 && h->def_regular
9524 && !bfd_link_relocatable (flinfo->info))
9525 || ((h->dynindx != -1
9526 || h->forced_local)
9527 && ((bfd_link_pic (flinfo->info)
9528 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9529 || h->root.type != bfd_link_hash_undefweak))
9530 || !h->forced_local)
9531 && elf_hash_table (flinfo->info)->dynamic_sections_created))
9532 {
9533 if (! ((*bed->elf_backend_finish_dynamic_symbol)
9534 (flinfo->output_bfd, flinfo->info, h, &sym)))
9535 {
9536 eoinfo->failed = TRUE;
9537 return FALSE;
9538 }
9539 }
9540
9541 /* If we are marking the symbol as undefined, and there are no
9542 non-weak references to this symbol from a regular object, then
9543 mark the symbol as weak undefined; if there are non-weak
9544 references, mark the symbol as strong. We can't do this earlier,
9545 because it might not be marked as undefined until the
9546 finish_dynamic_symbol routine gets through with it. */
9547 if (sym.st_shndx == SHN_UNDEF
9548 && h->ref_regular
9549 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9550 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9551 {
9552 int bindtype;
9553 type = ELF_ST_TYPE (sym.st_info);
9554
9555 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9556 if (type == STT_GNU_IFUNC)
9557 type = STT_FUNC;
9558
9559 if (h->ref_regular_nonweak)
9560 bindtype = STB_GLOBAL;
9561 else
9562 bindtype = STB_WEAK;
9563 sym.st_info = ELF_ST_INFO (bindtype, type);
9564 }
9565
9566 /* If this is a symbol defined in a dynamic library, don't use the
9567 symbol size from the dynamic library. Relinking an executable
9568 against a new library may introduce gratuitous changes in the
9569 executable's symbols if we keep the size. */
9570 if (sym.st_shndx == SHN_UNDEF
9571 && !h->def_regular
9572 && h->def_dynamic)
9573 sym.st_size = 0;
9574
9575 /* If a non-weak symbol with non-default visibility is not defined
9576 locally, it is a fatal error. */
9577 if (!bfd_link_relocatable (flinfo->info)
9578 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9579 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9580 && h->root.type == bfd_link_hash_undefined
9581 && !h->def_regular)
9582 {
9583 const char *msg;
9584
9585 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9586 /* xgettext:c-format */
9587 msg = _("%B: protected symbol `%s' isn't defined");
9588 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9589 /* xgettext:c-format */
9590 msg = _("%B: internal symbol `%s' isn't defined");
9591 else
9592 /* xgettext:c-format */
9593 msg = _("%B: hidden symbol `%s' isn't defined");
9594 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
9595 bfd_set_error (bfd_error_bad_value);
9596 eoinfo->failed = TRUE;
9597 return FALSE;
9598 }
9599
9600 /* If this symbol should be put in the .dynsym section, then put it
9601 there now. We already know the symbol index. We also fill in
9602 the entry in the .hash section. */
9603 if (elf_hash_table (flinfo->info)->dynsym != NULL
9604 && h->dynindx != -1
9605 && elf_hash_table (flinfo->info)->dynamic_sections_created)
9606 {
9607 bfd_byte *esym;
9608
9609 /* Since there is no version information in the dynamic string,
9610 if there is no version info in symbol version section, we will
9611 have a run-time problem if not linking executable, referenced
9612 by shared library, or not bound locally. */
9613 if (h->verinfo.verdef == NULL
9614 && (!bfd_link_executable (flinfo->info)
9615 || h->ref_dynamic
9616 || !h->def_regular))
9617 {
9618 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9619
9620 if (p && p [1] != '\0')
9621 {
9622 _bfd_error_handler
9623 /* xgettext:c-format */
9624 (_("%B: No symbol version section for versioned symbol `%s'"),
9625 flinfo->output_bfd, h->root.root.string);
9626 eoinfo->failed = TRUE;
9627 return FALSE;
9628 }
9629 }
9630
9631 sym.st_name = h->dynstr_index;
9632 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9633 + h->dynindx * bed->s->sizeof_sym);
9634 if (!check_dynsym (flinfo->output_bfd, &sym))
9635 {
9636 eoinfo->failed = TRUE;
9637 return FALSE;
9638 }
9639 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
9640
9641 if (flinfo->hash_sec != NULL)
9642 {
9643 size_t hash_entry_size;
9644 bfd_byte *bucketpos;
9645 bfd_vma chain;
9646 size_t bucketcount;
9647 size_t bucket;
9648
9649 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
9650 bucket = h->u.elf_hash_value % bucketcount;
9651
9652 hash_entry_size
9653 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9654 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
9655 + (bucket + 2) * hash_entry_size);
9656 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9657 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9658 bucketpos);
9659 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9660 ((bfd_byte *) flinfo->hash_sec->contents
9661 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9662 }
9663
9664 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
9665 {
9666 Elf_Internal_Versym iversym;
9667 Elf_External_Versym *eversym;
9668
9669 if (!h->def_regular)
9670 {
9671 if (h->verinfo.verdef == NULL
9672 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9673 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
9674 iversym.vs_vers = 0;
9675 else
9676 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9677 }
9678 else
9679 {
9680 if (h->verinfo.vertree == NULL)
9681 iversym.vs_vers = 1;
9682 else
9683 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
9684 if (flinfo->info->create_default_symver)
9685 iversym.vs_vers++;
9686 }
9687
9688 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9689 defined locally. */
9690 if (h->versioned == versioned_hidden && h->def_regular)
9691 iversym.vs_vers |= VERSYM_HIDDEN;
9692
9693 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
9694 eversym += h->dynindx;
9695 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
9696 }
9697 }
9698
9699 /* If the symbol is undefined, and we didn't output it to .dynsym,
9700 strip it from .symtab too. Obviously we can't do this for
9701 relocatable output or when needed for --emit-relocs. */
9702 else if (input_sec == bfd_und_section_ptr
9703 && h->indx != -2
9704 && !bfd_link_relocatable (flinfo->info))
9705 return TRUE;
9706 /* Also strip others that we couldn't earlier due to dynamic symbol
9707 processing. */
9708 if (strip)
9709 return TRUE;
9710 if ((input_sec->flags & SEC_EXCLUDE) != 0)
9711 return TRUE;
9712
9713 /* Output a FILE symbol so that following locals are not associated
9714 with the wrong input file. We need one for forced local symbols
9715 if we've seen more than one FILE symbol or when we have exactly
9716 one FILE symbol but global symbols are present in a file other
9717 than the one with the FILE symbol. We also need one if linker
9718 defined symbols are present. In practice these conditions are
9719 always met, so just emit the FILE symbol unconditionally. */
9720 if (eoinfo->localsyms
9721 && !eoinfo->file_sym_done
9722 && eoinfo->flinfo->filesym_count != 0)
9723 {
9724 Elf_Internal_Sym fsym;
9725
9726 memset (&fsym, 0, sizeof (fsym));
9727 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9728 fsym.st_shndx = SHN_ABS;
9729 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9730 bfd_und_section_ptr, NULL))
9731 return FALSE;
9732
9733 eoinfo->file_sym_done = TRUE;
9734 }
9735
9736 indx = bfd_get_symcount (flinfo->output_bfd);
9737 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9738 input_sec, h);
9739 if (ret == 0)
9740 {
9741 eoinfo->failed = TRUE;
9742 return FALSE;
9743 }
9744 else if (ret == 1)
9745 h->indx = indx;
9746 else if (h->indx == -2)
9747 abort();
9748
9749 return TRUE;
9750 }
9751
9752 /* Return TRUE if special handling is done for relocs in SEC against
9753 symbols defined in discarded sections. */
9754
9755 static bfd_boolean
9756 elf_section_ignore_discarded_relocs (asection *sec)
9757 {
9758 const struct elf_backend_data *bed;
9759
9760 switch (sec->sec_info_type)
9761 {
9762 case SEC_INFO_TYPE_STABS:
9763 case SEC_INFO_TYPE_EH_FRAME:
9764 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
9765 return TRUE;
9766 default:
9767 break;
9768 }
9769
9770 bed = get_elf_backend_data (sec->owner);
9771 if (bed->elf_backend_ignore_discarded_relocs != NULL
9772 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9773 return TRUE;
9774
9775 return FALSE;
9776 }
9777
9778 /* Return a mask saying how ld should treat relocations in SEC against
9779 symbols defined in discarded sections. If this function returns
9780 COMPLAIN set, ld will issue a warning message. If this function
9781 returns PRETEND set, and the discarded section was link-once and the
9782 same size as the kept link-once section, ld will pretend that the
9783 symbol was actually defined in the kept section. Otherwise ld will
9784 zero the reloc (at least that is the intent, but some cooperation by
9785 the target dependent code is needed, particularly for REL targets). */
9786
9787 unsigned int
9788 _bfd_elf_default_action_discarded (asection *sec)
9789 {
9790 if (sec->flags & SEC_DEBUGGING)
9791 return PRETEND;
9792
9793 if (strcmp (".eh_frame", sec->name) == 0)
9794 return 0;
9795
9796 if (strcmp (".gcc_except_table", sec->name) == 0)
9797 return 0;
9798
9799 return COMPLAIN | PRETEND;
9800 }
9801
9802 /* Find a match between a section and a member of a section group. */
9803
9804 static asection *
9805 match_group_member (asection *sec, asection *group,
9806 struct bfd_link_info *info)
9807 {
9808 asection *first = elf_next_in_group (group);
9809 asection *s = first;
9810
9811 while (s != NULL)
9812 {
9813 if (bfd_elf_match_symbols_in_sections (s, sec, info))
9814 return s;
9815
9816 s = elf_next_in_group (s);
9817 if (s == first)
9818 break;
9819 }
9820
9821 return NULL;
9822 }
9823
9824 /* Check if the kept section of a discarded section SEC can be used
9825 to replace it. Return the replacement if it is OK. Otherwise return
9826 NULL. */
9827
9828 asection *
9829 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
9830 {
9831 asection *kept;
9832
9833 kept = sec->kept_section;
9834 if (kept != NULL)
9835 {
9836 if ((kept->flags & SEC_GROUP) != 0)
9837 kept = match_group_member (sec, kept, info);
9838 if (kept != NULL
9839 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9840 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
9841 kept = NULL;
9842 sec->kept_section = kept;
9843 }
9844 return kept;
9845 }
9846
9847 /* Link an input file into the linker output file. This function
9848 handles all the sections and relocations of the input file at once.
9849 This is so that we only have to read the local symbols once, and
9850 don't have to keep them in memory. */
9851
9852 static bfd_boolean
9853 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
9854 {
9855 int (*relocate_section)
9856 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9857 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9858 bfd *output_bfd;
9859 Elf_Internal_Shdr *symtab_hdr;
9860 size_t locsymcount;
9861 size_t extsymoff;
9862 Elf_Internal_Sym *isymbuf;
9863 Elf_Internal_Sym *isym;
9864 Elf_Internal_Sym *isymend;
9865 long *pindex;
9866 asection **ppsection;
9867 asection *o;
9868 const struct elf_backend_data *bed;
9869 struct elf_link_hash_entry **sym_hashes;
9870 bfd_size_type address_size;
9871 bfd_vma r_type_mask;
9872 int r_sym_shift;
9873 bfd_boolean have_file_sym = FALSE;
9874
9875 output_bfd = flinfo->output_bfd;
9876 bed = get_elf_backend_data (output_bfd);
9877 relocate_section = bed->elf_backend_relocate_section;
9878
9879 /* If this is a dynamic object, we don't want to do anything here:
9880 we don't want the local symbols, and we don't want the section
9881 contents. */
9882 if ((input_bfd->flags & DYNAMIC) != 0)
9883 return TRUE;
9884
9885 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9886 if (elf_bad_symtab (input_bfd))
9887 {
9888 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9889 extsymoff = 0;
9890 }
9891 else
9892 {
9893 locsymcount = symtab_hdr->sh_info;
9894 extsymoff = symtab_hdr->sh_info;
9895 }
9896
9897 /* Read the local symbols. */
9898 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9899 if (isymbuf == NULL && locsymcount != 0)
9900 {
9901 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
9902 flinfo->internal_syms,
9903 flinfo->external_syms,
9904 flinfo->locsym_shndx);
9905 if (isymbuf == NULL)
9906 return FALSE;
9907 }
9908
9909 /* Find local symbol sections and adjust values of symbols in
9910 SEC_MERGE sections. Write out those local symbols we know are
9911 going into the output file. */
9912 isymend = isymbuf + locsymcount;
9913 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
9914 isym < isymend;
9915 isym++, pindex++, ppsection++)
9916 {
9917 asection *isec;
9918 const char *name;
9919 Elf_Internal_Sym osym;
9920 long indx;
9921 int ret;
9922
9923 *pindex = -1;
9924
9925 if (elf_bad_symtab (input_bfd))
9926 {
9927 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9928 {
9929 *ppsection = NULL;
9930 continue;
9931 }
9932 }
9933
9934 if (isym->st_shndx == SHN_UNDEF)
9935 isec = bfd_und_section_ptr;
9936 else if (isym->st_shndx == SHN_ABS)
9937 isec = bfd_abs_section_ptr;
9938 else if (isym->st_shndx == SHN_COMMON)
9939 isec = bfd_com_section_ptr;
9940 else
9941 {
9942 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9943 if (isec == NULL)
9944 {
9945 /* Don't attempt to output symbols with st_shnx in the
9946 reserved range other than SHN_ABS and SHN_COMMON. */
9947 *ppsection = NULL;
9948 continue;
9949 }
9950 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
9951 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9952 isym->st_value =
9953 _bfd_merged_section_offset (output_bfd, &isec,
9954 elf_section_data (isec)->sec_info,
9955 isym->st_value);
9956 }
9957
9958 *ppsection = isec;
9959
9960 /* Don't output the first, undefined, symbol. In fact, don't
9961 output any undefined local symbol. */
9962 if (isec == bfd_und_section_ptr)
9963 continue;
9964
9965 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9966 {
9967 /* We never output section symbols. Instead, we use the
9968 section symbol of the corresponding section in the output
9969 file. */
9970 continue;
9971 }
9972
9973 /* If we are stripping all symbols, we don't want to output this
9974 one. */
9975 if (flinfo->info->strip == strip_all)
9976 continue;
9977
9978 /* If we are discarding all local symbols, we don't want to
9979 output this one. If we are generating a relocatable output
9980 file, then some of the local symbols may be required by
9981 relocs; we output them below as we discover that they are
9982 needed. */
9983 if (flinfo->info->discard == discard_all)
9984 continue;
9985
9986 /* If this symbol is defined in a section which we are
9987 discarding, we don't need to keep it. */
9988 if (isym->st_shndx != SHN_UNDEF
9989 && isym->st_shndx < SHN_LORESERVE
9990 && bfd_section_removed_from_list (output_bfd,
9991 isec->output_section))
9992 continue;
9993
9994 /* Get the name of the symbol. */
9995 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9996 isym->st_name);
9997 if (name == NULL)
9998 return FALSE;
9999
10000 /* See if we are discarding symbols with this name. */
10001 if ((flinfo->info->strip == strip_some
10002 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
10003 == NULL))
10004 || (((flinfo->info->discard == discard_sec_merge
10005 && (isec->flags & SEC_MERGE)
10006 && !bfd_link_relocatable (flinfo->info))
10007 || flinfo->info->discard == discard_l)
10008 && bfd_is_local_label_name (input_bfd, name)))
10009 continue;
10010
10011 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10012 {
10013 if (input_bfd->lto_output)
10014 /* -flto puts a temp file name here. This means builds
10015 are not reproducible. Discard the symbol. */
10016 continue;
10017 have_file_sym = TRUE;
10018 flinfo->filesym_count += 1;
10019 }
10020 if (!have_file_sym)
10021 {
10022 /* In the absence of debug info, bfd_find_nearest_line uses
10023 FILE symbols to determine the source file for local
10024 function symbols. Provide a FILE symbol here if input
10025 files lack such, so that their symbols won't be
10026 associated with a previous input file. It's not the
10027 source file, but the best we can do. */
10028 have_file_sym = TRUE;
10029 flinfo->filesym_count += 1;
10030 memset (&osym, 0, sizeof (osym));
10031 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10032 osym.st_shndx = SHN_ABS;
10033 if (!elf_link_output_symstrtab (flinfo,
10034 (input_bfd->lto_output ? NULL
10035 : input_bfd->filename),
10036 &osym, bfd_abs_section_ptr,
10037 NULL))
10038 return FALSE;
10039 }
10040
10041 osym = *isym;
10042
10043 /* Adjust the section index for the output file. */
10044 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10045 isec->output_section);
10046 if (osym.st_shndx == SHN_BAD)
10047 return FALSE;
10048
10049 /* ELF symbols in relocatable files are section relative, but
10050 in executable files they are virtual addresses. Note that
10051 this code assumes that all ELF sections have an associated
10052 BFD section with a reasonable value for output_offset; below
10053 we assume that they also have a reasonable value for
10054 output_section. Any special sections must be set up to meet
10055 these requirements. */
10056 osym.st_value += isec->output_offset;
10057 if (!bfd_link_relocatable (flinfo->info))
10058 {
10059 osym.st_value += isec->output_section->vma;
10060 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10061 {
10062 /* STT_TLS symbols are relative to PT_TLS segment base. */
10063 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10064 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10065 }
10066 }
10067
10068 indx = bfd_get_symcount (output_bfd);
10069 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10070 if (ret == 0)
10071 return FALSE;
10072 else if (ret == 1)
10073 *pindex = indx;
10074 }
10075
10076 if (bed->s->arch_size == 32)
10077 {
10078 r_type_mask = 0xff;
10079 r_sym_shift = 8;
10080 address_size = 4;
10081 }
10082 else
10083 {
10084 r_type_mask = 0xffffffff;
10085 r_sym_shift = 32;
10086 address_size = 8;
10087 }
10088
10089 /* Relocate the contents of each section. */
10090 sym_hashes = elf_sym_hashes (input_bfd);
10091 for (o = input_bfd->sections; o != NULL; o = o->next)
10092 {
10093 bfd_byte *contents;
10094
10095 if (! o->linker_mark)
10096 {
10097 /* This section was omitted from the link. */
10098 continue;
10099 }
10100
10101 if (bfd_link_relocatable (flinfo->info)
10102 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10103 {
10104 /* Deal with the group signature symbol. */
10105 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10106 unsigned long symndx = sec_data->this_hdr.sh_info;
10107 asection *osec = o->output_section;
10108
10109 if (symndx >= locsymcount
10110 || (elf_bad_symtab (input_bfd)
10111 && flinfo->sections[symndx] == NULL))
10112 {
10113 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10114 while (h->root.type == bfd_link_hash_indirect
10115 || h->root.type == bfd_link_hash_warning)
10116 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10117 /* Arrange for symbol to be output. */
10118 h->indx = -2;
10119 elf_section_data (osec)->this_hdr.sh_info = -2;
10120 }
10121 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10122 {
10123 /* We'll use the output section target_index. */
10124 asection *sec = flinfo->sections[symndx]->output_section;
10125 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10126 }
10127 else
10128 {
10129 if (flinfo->indices[symndx] == -1)
10130 {
10131 /* Otherwise output the local symbol now. */
10132 Elf_Internal_Sym sym = isymbuf[symndx];
10133 asection *sec = flinfo->sections[symndx]->output_section;
10134 const char *name;
10135 long indx;
10136 int ret;
10137
10138 name = bfd_elf_string_from_elf_section (input_bfd,
10139 symtab_hdr->sh_link,
10140 sym.st_name);
10141 if (name == NULL)
10142 return FALSE;
10143
10144 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10145 sec);
10146 if (sym.st_shndx == SHN_BAD)
10147 return FALSE;
10148
10149 sym.st_value += o->output_offset;
10150
10151 indx = bfd_get_symcount (output_bfd);
10152 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10153 NULL);
10154 if (ret == 0)
10155 return FALSE;
10156 else if (ret == 1)
10157 flinfo->indices[symndx] = indx;
10158 else
10159 abort ();
10160 }
10161 elf_section_data (osec)->this_hdr.sh_info
10162 = flinfo->indices[symndx];
10163 }
10164 }
10165
10166 if ((o->flags & SEC_HAS_CONTENTS) == 0
10167 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10168 continue;
10169
10170 if ((o->flags & SEC_LINKER_CREATED) != 0)
10171 {
10172 /* Section was created by _bfd_elf_link_create_dynamic_sections
10173 or somesuch. */
10174 continue;
10175 }
10176
10177 /* Get the contents of the section. They have been cached by a
10178 relaxation routine. Note that o is a section in an input
10179 file, so the contents field will not have been set by any of
10180 the routines which work on output files. */
10181 if (elf_section_data (o)->this_hdr.contents != NULL)
10182 {
10183 contents = elf_section_data (o)->this_hdr.contents;
10184 if (bed->caches_rawsize
10185 && o->rawsize != 0
10186 && o->rawsize < o->size)
10187 {
10188 memcpy (flinfo->contents, contents, o->rawsize);
10189 contents = flinfo->contents;
10190 }
10191 }
10192 else
10193 {
10194 contents = flinfo->contents;
10195 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10196 return FALSE;
10197 }
10198
10199 if ((o->flags & SEC_RELOC) != 0)
10200 {
10201 Elf_Internal_Rela *internal_relocs;
10202 Elf_Internal_Rela *rel, *relend;
10203 int action_discarded;
10204 int ret;
10205
10206 /* Get the swapped relocs. */
10207 internal_relocs
10208 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10209 flinfo->internal_relocs, FALSE);
10210 if (internal_relocs == NULL
10211 && o->reloc_count > 0)
10212 return FALSE;
10213
10214 /* We need to reverse-copy input .ctors/.dtors sections if
10215 they are placed in .init_array/.finit_array for output. */
10216 if (o->size > address_size
10217 && ((strncmp (o->name, ".ctors", 6) == 0
10218 && strcmp (o->output_section->name,
10219 ".init_array") == 0)
10220 || (strncmp (o->name, ".dtors", 6) == 0
10221 && strcmp (o->output_section->name,
10222 ".fini_array") == 0))
10223 && (o->name[6] == 0 || o->name[6] == '.'))
10224 {
10225 if (o->size != o->reloc_count * address_size)
10226 {
10227 _bfd_error_handler
10228 /* xgettext:c-format */
10229 (_("error: %B: size of section %A is not "
10230 "multiple of address size"),
10231 input_bfd, o);
10232 bfd_set_error (bfd_error_on_input);
10233 return FALSE;
10234 }
10235 o->flags |= SEC_ELF_REVERSE_COPY;
10236 }
10237
10238 action_discarded = -1;
10239 if (!elf_section_ignore_discarded_relocs (o))
10240 action_discarded = (*bed->action_discarded) (o);
10241
10242 /* Run through the relocs evaluating complex reloc symbols and
10243 looking for relocs against symbols from discarded sections
10244 or section symbols from removed link-once sections.
10245 Complain about relocs against discarded sections. Zero
10246 relocs against removed link-once sections. */
10247
10248 rel = internal_relocs;
10249 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
10250 for ( ; rel < relend; rel++)
10251 {
10252 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10253 unsigned int s_type;
10254 asection **ps, *sec;
10255 struct elf_link_hash_entry *h = NULL;
10256 const char *sym_name;
10257
10258 if (r_symndx == STN_UNDEF)
10259 continue;
10260
10261 if (r_symndx >= locsymcount
10262 || (elf_bad_symtab (input_bfd)
10263 && flinfo->sections[r_symndx] == NULL))
10264 {
10265 h = sym_hashes[r_symndx - extsymoff];
10266
10267 /* Badly formatted input files can contain relocs that
10268 reference non-existant symbols. Check here so that
10269 we do not seg fault. */
10270 if (h == NULL)
10271 {
10272 char buffer [32];
10273
10274 sprintf_vma (buffer, rel->r_info);
10275 _bfd_error_handler
10276 /* xgettext:c-format */
10277 (_("error: %B contains a reloc (0x%s) for section %A "
10278 "that references a non-existent global symbol"),
10279 input_bfd, o, buffer);
10280 bfd_set_error (bfd_error_bad_value);
10281 return FALSE;
10282 }
10283
10284 while (h->root.type == bfd_link_hash_indirect
10285 || h->root.type == bfd_link_hash_warning)
10286 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10287
10288 s_type = h->type;
10289
10290 /* If a plugin symbol is referenced from a non-IR file,
10291 mark the symbol as undefined. Note that the
10292 linker may attach linker created dynamic sections
10293 to the plugin bfd. Symbols defined in linker
10294 created sections are not plugin symbols. */
10295 if (h->root.non_ir_ref
10296 && (h->root.type == bfd_link_hash_defined
10297 || h->root.type == bfd_link_hash_defweak)
10298 && (h->root.u.def.section->flags
10299 & SEC_LINKER_CREATED) == 0
10300 && h->root.u.def.section->owner != NULL
10301 && (h->root.u.def.section->owner->flags
10302 & BFD_PLUGIN) != 0)
10303 {
10304 h->root.type = bfd_link_hash_undefined;
10305 h->root.u.undef.abfd = h->root.u.def.section->owner;
10306 }
10307
10308 ps = NULL;
10309 if (h->root.type == bfd_link_hash_defined
10310 || h->root.type == bfd_link_hash_defweak)
10311 ps = &h->root.u.def.section;
10312
10313 sym_name = h->root.root.string;
10314 }
10315 else
10316 {
10317 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10318
10319 s_type = ELF_ST_TYPE (sym->st_info);
10320 ps = &flinfo->sections[r_symndx];
10321 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10322 sym, *ps);
10323 }
10324
10325 if ((s_type == STT_RELC || s_type == STT_SRELC)
10326 && !bfd_link_relocatable (flinfo->info))
10327 {
10328 bfd_vma val;
10329 bfd_vma dot = (rel->r_offset
10330 + o->output_offset + o->output_section->vma);
10331 #ifdef DEBUG
10332 printf ("Encountered a complex symbol!");
10333 printf (" (input_bfd %s, section %s, reloc %ld\n",
10334 input_bfd->filename, o->name,
10335 (long) (rel - internal_relocs));
10336 printf (" symbol: idx %8.8lx, name %s\n",
10337 r_symndx, sym_name);
10338 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10339 (unsigned long) rel->r_info,
10340 (unsigned long) rel->r_offset);
10341 #endif
10342 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10343 isymbuf, locsymcount, s_type == STT_SRELC))
10344 return FALSE;
10345
10346 /* Symbol evaluated OK. Update to absolute value. */
10347 set_symbol_value (input_bfd, isymbuf, locsymcount,
10348 r_symndx, val);
10349 continue;
10350 }
10351
10352 if (action_discarded != -1 && ps != NULL)
10353 {
10354 /* Complain if the definition comes from a
10355 discarded section. */
10356 if ((sec = *ps) != NULL && discarded_section (sec))
10357 {
10358 BFD_ASSERT (r_symndx != STN_UNDEF);
10359 if (action_discarded & COMPLAIN)
10360 (*flinfo->info->callbacks->einfo)
10361 /* xgettext:c-format */
10362 (_("%X`%s' referenced in section `%A' of %B: "
10363 "defined in discarded section `%A' of %B\n"),
10364 sym_name, o, input_bfd, sec, sec->owner);
10365
10366 /* Try to do the best we can to support buggy old
10367 versions of gcc. Pretend that the symbol is
10368 really defined in the kept linkonce section.
10369 FIXME: This is quite broken. Modifying the
10370 symbol here means we will be changing all later
10371 uses of the symbol, not just in this section. */
10372 if (action_discarded & PRETEND)
10373 {
10374 asection *kept;
10375
10376 kept = _bfd_elf_check_kept_section (sec,
10377 flinfo->info);
10378 if (kept != NULL)
10379 {
10380 *ps = kept;
10381 continue;
10382 }
10383 }
10384 }
10385 }
10386 }
10387
10388 /* Relocate the section by invoking a back end routine.
10389
10390 The back end routine is responsible for adjusting the
10391 section contents as necessary, and (if using Rela relocs
10392 and generating a relocatable output file) adjusting the
10393 reloc addend as necessary.
10394
10395 The back end routine does not have to worry about setting
10396 the reloc address or the reloc symbol index.
10397
10398 The back end routine is given a pointer to the swapped in
10399 internal symbols, and can access the hash table entries
10400 for the external symbols via elf_sym_hashes (input_bfd).
10401
10402 When generating relocatable output, the back end routine
10403 must handle STB_LOCAL/STT_SECTION symbols specially. The
10404 output symbol is going to be a section symbol
10405 corresponding to the output section, which will require
10406 the addend to be adjusted. */
10407
10408 ret = (*relocate_section) (output_bfd, flinfo->info,
10409 input_bfd, o, contents,
10410 internal_relocs,
10411 isymbuf,
10412 flinfo->sections);
10413 if (!ret)
10414 return FALSE;
10415
10416 if (ret == 2
10417 || bfd_link_relocatable (flinfo->info)
10418 || flinfo->info->emitrelocations)
10419 {
10420 Elf_Internal_Rela *irela;
10421 Elf_Internal_Rela *irelaend, *irelamid;
10422 bfd_vma last_offset;
10423 struct elf_link_hash_entry **rel_hash;
10424 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10425 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10426 unsigned int next_erel;
10427 bfd_boolean rela_normal;
10428 struct bfd_elf_section_data *esdi, *esdo;
10429
10430 esdi = elf_section_data (o);
10431 esdo = elf_section_data (o->output_section);
10432 rela_normal = FALSE;
10433
10434 /* Adjust the reloc addresses and symbol indices. */
10435
10436 irela = internal_relocs;
10437 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
10438 rel_hash = esdo->rel.hashes + esdo->rel.count;
10439 /* We start processing the REL relocs, if any. When we reach
10440 IRELAMID in the loop, we switch to the RELA relocs. */
10441 irelamid = irela;
10442 if (esdi->rel.hdr != NULL)
10443 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10444 * bed->s->int_rels_per_ext_rel);
10445 rel_hash_list = rel_hash;
10446 rela_hash_list = NULL;
10447 last_offset = o->output_offset;
10448 if (!bfd_link_relocatable (flinfo->info))
10449 last_offset += o->output_section->vma;
10450 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10451 {
10452 unsigned long r_symndx;
10453 asection *sec;
10454 Elf_Internal_Sym sym;
10455
10456 if (next_erel == bed->s->int_rels_per_ext_rel)
10457 {
10458 rel_hash++;
10459 next_erel = 0;
10460 }
10461
10462 if (irela == irelamid)
10463 {
10464 rel_hash = esdo->rela.hashes + esdo->rela.count;
10465 rela_hash_list = rel_hash;
10466 rela_normal = bed->rela_normal;
10467 }
10468
10469 irela->r_offset = _bfd_elf_section_offset (output_bfd,
10470 flinfo->info, o,
10471 irela->r_offset);
10472 if (irela->r_offset >= (bfd_vma) -2)
10473 {
10474 /* This is a reloc for a deleted entry or somesuch.
10475 Turn it into an R_*_NONE reloc, at the same
10476 offset as the last reloc. elf_eh_frame.c and
10477 bfd_elf_discard_info rely on reloc offsets
10478 being ordered. */
10479 irela->r_offset = last_offset;
10480 irela->r_info = 0;
10481 irela->r_addend = 0;
10482 continue;
10483 }
10484
10485 irela->r_offset += o->output_offset;
10486
10487 /* Relocs in an executable have to be virtual addresses. */
10488 if (!bfd_link_relocatable (flinfo->info))
10489 irela->r_offset += o->output_section->vma;
10490
10491 last_offset = irela->r_offset;
10492
10493 r_symndx = irela->r_info >> r_sym_shift;
10494 if (r_symndx == STN_UNDEF)
10495 continue;
10496
10497 if (r_symndx >= locsymcount
10498 || (elf_bad_symtab (input_bfd)
10499 && flinfo->sections[r_symndx] == NULL))
10500 {
10501 struct elf_link_hash_entry *rh;
10502 unsigned long indx;
10503
10504 /* This is a reloc against a global symbol. We
10505 have not yet output all the local symbols, so
10506 we do not know the symbol index of any global
10507 symbol. We set the rel_hash entry for this
10508 reloc to point to the global hash table entry
10509 for this symbol. The symbol index is then
10510 set at the end of bfd_elf_final_link. */
10511 indx = r_symndx - extsymoff;
10512 rh = elf_sym_hashes (input_bfd)[indx];
10513 while (rh->root.type == bfd_link_hash_indirect
10514 || rh->root.type == bfd_link_hash_warning)
10515 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10516
10517 /* Setting the index to -2 tells
10518 elf_link_output_extsym that this symbol is
10519 used by a reloc. */
10520 BFD_ASSERT (rh->indx < 0);
10521 rh->indx = -2;
10522
10523 *rel_hash = rh;
10524
10525 continue;
10526 }
10527
10528 /* This is a reloc against a local symbol. */
10529
10530 *rel_hash = NULL;
10531 sym = isymbuf[r_symndx];
10532 sec = flinfo->sections[r_symndx];
10533 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10534 {
10535 /* I suppose the backend ought to fill in the
10536 section of any STT_SECTION symbol against a
10537 processor specific section. */
10538 r_symndx = STN_UNDEF;
10539 if (bfd_is_abs_section (sec))
10540 ;
10541 else if (sec == NULL || sec->owner == NULL)
10542 {
10543 bfd_set_error (bfd_error_bad_value);
10544 return FALSE;
10545 }
10546 else
10547 {
10548 asection *osec = sec->output_section;
10549
10550 /* If we have discarded a section, the output
10551 section will be the absolute section. In
10552 case of discarded SEC_MERGE sections, use
10553 the kept section. relocate_section should
10554 have already handled discarded linkonce
10555 sections. */
10556 if (bfd_is_abs_section (osec)
10557 && sec->kept_section != NULL
10558 && sec->kept_section->output_section != NULL)
10559 {
10560 osec = sec->kept_section->output_section;
10561 irela->r_addend -= osec->vma;
10562 }
10563
10564 if (!bfd_is_abs_section (osec))
10565 {
10566 r_symndx = osec->target_index;
10567 if (r_symndx == STN_UNDEF)
10568 {
10569 irela->r_addend += osec->vma;
10570 osec = _bfd_nearby_section (output_bfd, osec,
10571 osec->vma);
10572 irela->r_addend -= osec->vma;
10573 r_symndx = osec->target_index;
10574 }
10575 }
10576 }
10577
10578 /* Adjust the addend according to where the
10579 section winds up in the output section. */
10580 if (rela_normal)
10581 irela->r_addend += sec->output_offset;
10582 }
10583 else
10584 {
10585 if (flinfo->indices[r_symndx] == -1)
10586 {
10587 unsigned long shlink;
10588 const char *name;
10589 asection *osec;
10590 long indx;
10591
10592 if (flinfo->info->strip == strip_all)
10593 {
10594 /* You can't do ld -r -s. */
10595 bfd_set_error (bfd_error_invalid_operation);
10596 return FALSE;
10597 }
10598
10599 /* This symbol was skipped earlier, but
10600 since it is needed by a reloc, we
10601 must output it now. */
10602 shlink = symtab_hdr->sh_link;
10603 name = (bfd_elf_string_from_elf_section
10604 (input_bfd, shlink, sym.st_name));
10605 if (name == NULL)
10606 return FALSE;
10607
10608 osec = sec->output_section;
10609 sym.st_shndx =
10610 _bfd_elf_section_from_bfd_section (output_bfd,
10611 osec);
10612 if (sym.st_shndx == SHN_BAD)
10613 return FALSE;
10614
10615 sym.st_value += sec->output_offset;
10616 if (!bfd_link_relocatable (flinfo->info))
10617 {
10618 sym.st_value += osec->vma;
10619 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10620 {
10621 /* STT_TLS symbols are relative to PT_TLS
10622 segment base. */
10623 BFD_ASSERT (elf_hash_table (flinfo->info)
10624 ->tls_sec != NULL);
10625 sym.st_value -= (elf_hash_table (flinfo->info)
10626 ->tls_sec->vma);
10627 }
10628 }
10629
10630 indx = bfd_get_symcount (output_bfd);
10631 ret = elf_link_output_symstrtab (flinfo, name,
10632 &sym, sec,
10633 NULL);
10634 if (ret == 0)
10635 return FALSE;
10636 else if (ret == 1)
10637 flinfo->indices[r_symndx] = indx;
10638 else
10639 abort ();
10640 }
10641
10642 r_symndx = flinfo->indices[r_symndx];
10643 }
10644
10645 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10646 | (irela->r_info & r_type_mask));
10647 }
10648
10649 /* Swap out the relocs. */
10650 input_rel_hdr = esdi->rel.hdr;
10651 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
10652 {
10653 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10654 input_rel_hdr,
10655 internal_relocs,
10656 rel_hash_list))
10657 return FALSE;
10658 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10659 * bed->s->int_rels_per_ext_rel);
10660 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
10661 }
10662
10663 input_rela_hdr = esdi->rela.hdr;
10664 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10665 {
10666 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10667 input_rela_hdr,
10668 internal_relocs,
10669 rela_hash_list))
10670 return FALSE;
10671 }
10672 }
10673 }
10674
10675 /* Write out the modified section contents. */
10676 if (bed->elf_backend_write_section
10677 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
10678 contents))
10679 {
10680 /* Section written out. */
10681 }
10682 else switch (o->sec_info_type)
10683 {
10684 case SEC_INFO_TYPE_STABS:
10685 if (! (_bfd_write_section_stabs
10686 (output_bfd,
10687 &elf_hash_table (flinfo->info)->stab_info,
10688 o, &elf_section_data (o)->sec_info, contents)))
10689 return FALSE;
10690 break;
10691 case SEC_INFO_TYPE_MERGE:
10692 if (! _bfd_write_merged_section (output_bfd, o,
10693 elf_section_data (o)->sec_info))
10694 return FALSE;
10695 break;
10696 case SEC_INFO_TYPE_EH_FRAME:
10697 {
10698 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
10699 o, contents))
10700 return FALSE;
10701 }
10702 break;
10703 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10704 {
10705 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10706 flinfo->info,
10707 o, contents))
10708 return FALSE;
10709 }
10710 break;
10711 default:
10712 {
10713 if (! (o->flags & SEC_EXCLUDE))
10714 {
10715 file_ptr offset = (file_ptr) o->output_offset;
10716 bfd_size_type todo = o->size;
10717
10718 offset *= bfd_octets_per_byte (output_bfd);
10719
10720 if ((o->flags & SEC_ELF_REVERSE_COPY))
10721 {
10722 /* Reverse-copy input section to output. */
10723 do
10724 {
10725 todo -= address_size;
10726 if (! bfd_set_section_contents (output_bfd,
10727 o->output_section,
10728 contents + todo,
10729 offset,
10730 address_size))
10731 return FALSE;
10732 if (todo == 0)
10733 break;
10734 offset += address_size;
10735 }
10736 while (1);
10737 }
10738 else if (! bfd_set_section_contents (output_bfd,
10739 o->output_section,
10740 contents,
10741 offset, todo))
10742 return FALSE;
10743 }
10744 }
10745 break;
10746 }
10747 }
10748
10749 return TRUE;
10750 }
10751
10752 /* Generate a reloc when linking an ELF file. This is a reloc
10753 requested by the linker, and does not come from any input file. This
10754 is used to build constructor and destructor tables when linking
10755 with -Ur. */
10756
10757 static bfd_boolean
10758 elf_reloc_link_order (bfd *output_bfd,
10759 struct bfd_link_info *info,
10760 asection *output_section,
10761 struct bfd_link_order *link_order)
10762 {
10763 reloc_howto_type *howto;
10764 long indx;
10765 bfd_vma offset;
10766 bfd_vma addend;
10767 struct bfd_elf_section_reloc_data *reldata;
10768 struct elf_link_hash_entry **rel_hash_ptr;
10769 Elf_Internal_Shdr *rel_hdr;
10770 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10771 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10772 bfd_byte *erel;
10773 unsigned int i;
10774 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
10775
10776 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10777 if (howto == NULL)
10778 {
10779 bfd_set_error (bfd_error_bad_value);
10780 return FALSE;
10781 }
10782
10783 addend = link_order->u.reloc.p->addend;
10784
10785 if (esdo->rel.hdr)
10786 reldata = &esdo->rel;
10787 else if (esdo->rela.hdr)
10788 reldata = &esdo->rela;
10789 else
10790 {
10791 reldata = NULL;
10792 BFD_ASSERT (0);
10793 }
10794
10795 /* Figure out the symbol index. */
10796 rel_hash_ptr = reldata->hashes + reldata->count;
10797 if (link_order->type == bfd_section_reloc_link_order)
10798 {
10799 indx = link_order->u.reloc.p->u.section->target_index;
10800 BFD_ASSERT (indx != 0);
10801 *rel_hash_ptr = NULL;
10802 }
10803 else
10804 {
10805 struct elf_link_hash_entry *h;
10806
10807 /* Treat a reloc against a defined symbol as though it were
10808 actually against the section. */
10809 h = ((struct elf_link_hash_entry *)
10810 bfd_wrapped_link_hash_lookup (output_bfd, info,
10811 link_order->u.reloc.p->u.name,
10812 FALSE, FALSE, TRUE));
10813 if (h != NULL
10814 && (h->root.type == bfd_link_hash_defined
10815 || h->root.type == bfd_link_hash_defweak))
10816 {
10817 asection *section;
10818
10819 section = h->root.u.def.section;
10820 indx = section->output_section->target_index;
10821 *rel_hash_ptr = NULL;
10822 /* It seems that we ought to add the symbol value to the
10823 addend here, but in practice it has already been added
10824 because it was passed to constructor_callback. */
10825 addend += section->output_section->vma + section->output_offset;
10826 }
10827 else if (h != NULL)
10828 {
10829 /* Setting the index to -2 tells elf_link_output_extsym that
10830 this symbol is used by a reloc. */
10831 h->indx = -2;
10832 *rel_hash_ptr = h;
10833 indx = 0;
10834 }
10835 else
10836 {
10837 (*info->callbacks->unattached_reloc)
10838 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
10839 indx = 0;
10840 }
10841 }
10842
10843 /* If this is an inplace reloc, we must write the addend into the
10844 object file. */
10845 if (howto->partial_inplace && addend != 0)
10846 {
10847 bfd_size_type size;
10848 bfd_reloc_status_type rstat;
10849 bfd_byte *buf;
10850 bfd_boolean ok;
10851 const char *sym_name;
10852
10853 size = (bfd_size_type) bfd_get_reloc_size (howto);
10854 buf = (bfd_byte *) bfd_zmalloc (size);
10855 if (buf == NULL && size != 0)
10856 return FALSE;
10857 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10858 switch (rstat)
10859 {
10860 case bfd_reloc_ok:
10861 break;
10862
10863 default:
10864 case bfd_reloc_outofrange:
10865 abort ();
10866
10867 case bfd_reloc_overflow:
10868 if (link_order->type == bfd_section_reloc_link_order)
10869 sym_name = bfd_section_name (output_bfd,
10870 link_order->u.reloc.p->u.section);
10871 else
10872 sym_name = link_order->u.reloc.p->u.name;
10873 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
10874 howto->name, addend, NULL, NULL,
10875 (bfd_vma) 0);
10876 break;
10877 }
10878
10879 ok = bfd_set_section_contents (output_bfd, output_section, buf,
10880 link_order->offset
10881 * bfd_octets_per_byte (output_bfd),
10882 size);
10883 free (buf);
10884 if (! ok)
10885 return FALSE;
10886 }
10887
10888 /* The address of a reloc is relative to the section in a
10889 relocatable file, and is a virtual address in an executable
10890 file. */
10891 offset = link_order->offset;
10892 if (! bfd_link_relocatable (info))
10893 offset += output_section->vma;
10894
10895 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10896 {
10897 irel[i].r_offset = offset;
10898 irel[i].r_info = 0;
10899 irel[i].r_addend = 0;
10900 }
10901 if (bed->s->arch_size == 32)
10902 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10903 else
10904 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10905
10906 rel_hdr = reldata->hdr;
10907 erel = rel_hdr->contents;
10908 if (rel_hdr->sh_type == SHT_REL)
10909 {
10910 erel += reldata->count * bed->s->sizeof_rel;
10911 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10912 }
10913 else
10914 {
10915 irel[0].r_addend = addend;
10916 erel += reldata->count * bed->s->sizeof_rela;
10917 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10918 }
10919
10920 ++reldata->count;
10921
10922 return TRUE;
10923 }
10924
10925
10926 /* Get the output vma of the section pointed to by the sh_link field. */
10927
10928 static bfd_vma
10929 elf_get_linked_section_vma (struct bfd_link_order *p)
10930 {
10931 Elf_Internal_Shdr **elf_shdrp;
10932 asection *s;
10933 int elfsec;
10934
10935 s = p->u.indirect.section;
10936 elf_shdrp = elf_elfsections (s->owner);
10937 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10938 elfsec = elf_shdrp[elfsec]->sh_link;
10939 /* PR 290:
10940 The Intel C compiler generates SHT_IA_64_UNWIND with
10941 SHF_LINK_ORDER. But it doesn't set the sh_link or
10942 sh_info fields. Hence we could get the situation
10943 where elfsec is 0. */
10944 if (elfsec == 0)
10945 {
10946 const struct elf_backend_data *bed
10947 = get_elf_backend_data (s->owner);
10948 if (bed->link_order_error_handler)
10949 bed->link_order_error_handler
10950 /* xgettext:c-format */
10951 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
10952 return 0;
10953 }
10954 else
10955 {
10956 s = elf_shdrp[elfsec]->bfd_section;
10957 return s->output_section->vma + s->output_offset;
10958 }
10959 }
10960
10961
10962 /* Compare two sections based on the locations of the sections they are
10963 linked to. Used by elf_fixup_link_order. */
10964
10965 static int
10966 compare_link_order (const void * a, const void * b)
10967 {
10968 bfd_vma apos;
10969 bfd_vma bpos;
10970
10971 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10972 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10973 if (apos < bpos)
10974 return -1;
10975 return apos > bpos;
10976 }
10977
10978
10979 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10980 order as their linked sections. Returns false if this could not be done
10981 because an output section includes both ordered and unordered
10982 sections. Ideally we'd do this in the linker proper. */
10983
10984 static bfd_boolean
10985 elf_fixup_link_order (bfd *abfd, asection *o)
10986 {
10987 int seen_linkorder;
10988 int seen_other;
10989 int n;
10990 struct bfd_link_order *p;
10991 bfd *sub;
10992 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10993 unsigned elfsec;
10994 struct bfd_link_order **sections;
10995 asection *s, *other_sec, *linkorder_sec;
10996 bfd_vma offset;
10997
10998 other_sec = NULL;
10999 linkorder_sec = NULL;
11000 seen_other = 0;
11001 seen_linkorder = 0;
11002 for (p = o->map_head.link_order; p != NULL; p = p->next)
11003 {
11004 if (p->type == bfd_indirect_link_order)
11005 {
11006 s = p->u.indirect.section;
11007 sub = s->owner;
11008 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11009 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
11010 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11011 && elfsec < elf_numsections (sub)
11012 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11013 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
11014 {
11015 seen_linkorder++;
11016 linkorder_sec = s;
11017 }
11018 else
11019 {
11020 seen_other++;
11021 other_sec = s;
11022 }
11023 }
11024 else
11025 seen_other++;
11026
11027 if (seen_other && seen_linkorder)
11028 {
11029 if (other_sec && linkorder_sec)
11030 _bfd_error_handler
11031 /* xgettext:c-format */
11032 (_("%A has both ordered [`%A' in %B] "
11033 "and unordered [`%A' in %B] sections"),
11034 o, linkorder_sec,
11035 linkorder_sec->owner, other_sec,
11036 other_sec->owner);
11037 else
11038 _bfd_error_handler
11039 (_("%A has both ordered and unordered sections"), o);
11040 bfd_set_error (bfd_error_bad_value);
11041 return FALSE;
11042 }
11043 }
11044
11045 if (!seen_linkorder)
11046 return TRUE;
11047
11048 sections = (struct bfd_link_order **)
11049 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11050 if (sections == NULL)
11051 return FALSE;
11052 seen_linkorder = 0;
11053
11054 for (p = o->map_head.link_order; p != NULL; p = p->next)
11055 {
11056 sections[seen_linkorder++] = p;
11057 }
11058 /* Sort the input sections in the order of their linked section. */
11059 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11060 compare_link_order);
11061
11062 /* Change the offsets of the sections. */
11063 offset = 0;
11064 for (n = 0; n < seen_linkorder; n++)
11065 {
11066 s = sections[n]->u.indirect.section;
11067 offset &= ~(bfd_vma) 0 << s->alignment_power;
11068 s->output_offset = offset / bfd_octets_per_byte (abfd);
11069 sections[n]->offset = offset;
11070 offset += sections[n]->size;
11071 }
11072
11073 free (sections);
11074 return TRUE;
11075 }
11076
11077 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11078 Returns TRUE upon success, FALSE otherwise. */
11079
11080 static bfd_boolean
11081 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11082 {
11083 bfd_boolean ret = FALSE;
11084 bfd *implib_bfd;
11085 const struct elf_backend_data *bed;
11086 flagword flags;
11087 enum bfd_architecture arch;
11088 unsigned int mach;
11089 asymbol **sympp = NULL;
11090 long symsize;
11091 long symcount;
11092 long src_count;
11093 elf_symbol_type *osymbuf;
11094
11095 implib_bfd = info->out_implib_bfd;
11096 bed = get_elf_backend_data (abfd);
11097
11098 if (!bfd_set_format (implib_bfd, bfd_object))
11099 return FALSE;
11100
11101 flags = bfd_get_file_flags (abfd);
11102 flags &= ~HAS_RELOC;
11103 if (!bfd_set_start_address (implib_bfd, 0)
11104 || !bfd_set_file_flags (implib_bfd, flags))
11105 return FALSE;
11106
11107 /* Copy architecture of output file to import library file. */
11108 arch = bfd_get_arch (abfd);
11109 mach = bfd_get_mach (abfd);
11110 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11111 && (abfd->target_defaulted
11112 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11113 return FALSE;
11114
11115 /* Get symbol table size. */
11116 symsize = bfd_get_symtab_upper_bound (abfd);
11117 if (symsize < 0)
11118 return FALSE;
11119
11120 /* Read in the symbol table. */
11121 sympp = (asymbol **) xmalloc (symsize);
11122 symcount = bfd_canonicalize_symtab (abfd, sympp);
11123 if (symcount < 0)
11124 goto free_sym_buf;
11125
11126 /* Allow the BFD backend to copy any private header data it
11127 understands from the output BFD to the import library BFD. */
11128 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11129 goto free_sym_buf;
11130
11131 /* Filter symbols to appear in the import library. */
11132 if (bed->elf_backend_filter_implib_symbols)
11133 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11134 symcount);
11135 else
11136 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11137 if (symcount == 0)
11138 {
11139 bfd_set_error (bfd_error_no_symbols);
11140 _bfd_error_handler (_("%B: no symbol found for import library"),
11141 implib_bfd);
11142 goto free_sym_buf;
11143 }
11144
11145
11146 /* Make symbols absolute. */
11147 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11148 sizeof (*osymbuf));
11149 for (src_count = 0; src_count < symcount; src_count++)
11150 {
11151 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11152 sizeof (*osymbuf));
11153 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11154 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11155 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11156 osymbuf[src_count].internal_elf_sym.st_value =
11157 osymbuf[src_count].symbol.value;
11158 sympp[src_count] = &osymbuf[src_count].symbol;
11159 }
11160
11161 bfd_set_symtab (implib_bfd, sympp, symcount);
11162
11163 /* Allow the BFD backend to copy any private data it understands
11164 from the output BFD to the import library BFD. This is done last
11165 to permit the routine to look at the filtered symbol table. */
11166 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11167 goto free_sym_buf;
11168
11169 if (!bfd_close (implib_bfd))
11170 goto free_sym_buf;
11171
11172 ret = TRUE;
11173
11174 free_sym_buf:
11175 free (sympp);
11176 return ret;
11177 }
11178
11179 static void
11180 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11181 {
11182 asection *o;
11183
11184 if (flinfo->symstrtab != NULL)
11185 _bfd_elf_strtab_free (flinfo->symstrtab);
11186 if (flinfo->contents != NULL)
11187 free (flinfo->contents);
11188 if (flinfo->external_relocs != NULL)
11189 free (flinfo->external_relocs);
11190 if (flinfo->internal_relocs != NULL)
11191 free (flinfo->internal_relocs);
11192 if (flinfo->external_syms != NULL)
11193 free (flinfo->external_syms);
11194 if (flinfo->locsym_shndx != NULL)
11195 free (flinfo->locsym_shndx);
11196 if (flinfo->internal_syms != NULL)
11197 free (flinfo->internal_syms);
11198 if (flinfo->indices != NULL)
11199 free (flinfo->indices);
11200 if (flinfo->sections != NULL)
11201 free (flinfo->sections);
11202 if (flinfo->symshndxbuf != NULL)
11203 free (flinfo->symshndxbuf);
11204 for (o = obfd->sections; o != NULL; o = o->next)
11205 {
11206 struct bfd_elf_section_data *esdo = elf_section_data (o);
11207 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11208 free (esdo->rel.hashes);
11209 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11210 free (esdo->rela.hashes);
11211 }
11212 }
11213
11214 /* Do the final step of an ELF link. */
11215
11216 bfd_boolean
11217 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11218 {
11219 bfd_boolean dynamic;
11220 bfd_boolean emit_relocs;
11221 bfd *dynobj;
11222 struct elf_final_link_info flinfo;
11223 asection *o;
11224 struct bfd_link_order *p;
11225 bfd *sub;
11226 bfd_size_type max_contents_size;
11227 bfd_size_type max_external_reloc_size;
11228 bfd_size_type max_internal_reloc_count;
11229 bfd_size_type max_sym_count;
11230 bfd_size_type max_sym_shndx_count;
11231 Elf_Internal_Sym elfsym;
11232 unsigned int i;
11233 Elf_Internal_Shdr *symtab_hdr;
11234 Elf_Internal_Shdr *symtab_shndx_hdr;
11235 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11236 struct elf_outext_info eoinfo;
11237 bfd_boolean merged;
11238 size_t relativecount = 0;
11239 asection *reldyn = 0;
11240 bfd_size_type amt;
11241 asection *attr_section = NULL;
11242 bfd_vma attr_size = 0;
11243 const char *std_attrs_section;
11244 struct elf_link_hash_table *htab = elf_hash_table (info);
11245
11246 if (!is_elf_hash_table (htab))
11247 return FALSE;
11248
11249 if (bfd_link_pic (info))
11250 abfd->flags |= DYNAMIC;
11251
11252 dynamic = htab->dynamic_sections_created;
11253 dynobj = htab->dynobj;
11254
11255 emit_relocs = (bfd_link_relocatable (info)
11256 || info->emitrelocations);
11257
11258 flinfo.info = info;
11259 flinfo.output_bfd = abfd;
11260 flinfo.symstrtab = _bfd_elf_strtab_init ();
11261 if (flinfo.symstrtab == NULL)
11262 return FALSE;
11263
11264 if (! dynamic)
11265 {
11266 flinfo.hash_sec = NULL;
11267 flinfo.symver_sec = NULL;
11268 }
11269 else
11270 {
11271 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11272 /* Note that dynsym_sec can be NULL (on VMS). */
11273 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11274 /* Note that it is OK if symver_sec is NULL. */
11275 }
11276
11277 flinfo.contents = NULL;
11278 flinfo.external_relocs = NULL;
11279 flinfo.internal_relocs = NULL;
11280 flinfo.external_syms = NULL;
11281 flinfo.locsym_shndx = NULL;
11282 flinfo.internal_syms = NULL;
11283 flinfo.indices = NULL;
11284 flinfo.sections = NULL;
11285 flinfo.symshndxbuf = NULL;
11286 flinfo.filesym_count = 0;
11287
11288 /* The object attributes have been merged. Remove the input
11289 sections from the link, and set the contents of the output
11290 secton. */
11291 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11292 for (o = abfd->sections; o != NULL; o = o->next)
11293 {
11294 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11295 || strcmp (o->name, ".gnu.attributes") == 0)
11296 {
11297 for (p = o->map_head.link_order; p != NULL; p = p->next)
11298 {
11299 asection *input_section;
11300
11301 if (p->type != bfd_indirect_link_order)
11302 continue;
11303 input_section = p->u.indirect.section;
11304 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11305 elf_link_input_bfd ignores this section. */
11306 input_section->flags &= ~SEC_HAS_CONTENTS;
11307 }
11308
11309 attr_size = bfd_elf_obj_attr_size (abfd);
11310 if (attr_size)
11311 {
11312 bfd_set_section_size (abfd, o, attr_size);
11313 attr_section = o;
11314 /* Skip this section later on. */
11315 o->map_head.link_order = NULL;
11316 }
11317 else
11318 o->flags |= SEC_EXCLUDE;
11319 }
11320 }
11321
11322 /* Count up the number of relocations we will output for each output
11323 section, so that we know the sizes of the reloc sections. We
11324 also figure out some maximum sizes. */
11325 max_contents_size = 0;
11326 max_external_reloc_size = 0;
11327 max_internal_reloc_count = 0;
11328 max_sym_count = 0;
11329 max_sym_shndx_count = 0;
11330 merged = FALSE;
11331 for (o = abfd->sections; o != NULL; o = o->next)
11332 {
11333 struct bfd_elf_section_data *esdo = elf_section_data (o);
11334 o->reloc_count = 0;
11335
11336 for (p = o->map_head.link_order; p != NULL; p = p->next)
11337 {
11338 unsigned int reloc_count = 0;
11339 unsigned int additional_reloc_count = 0;
11340 struct bfd_elf_section_data *esdi = NULL;
11341
11342 if (p->type == bfd_section_reloc_link_order
11343 || p->type == bfd_symbol_reloc_link_order)
11344 reloc_count = 1;
11345 else if (p->type == bfd_indirect_link_order)
11346 {
11347 asection *sec;
11348
11349 sec = p->u.indirect.section;
11350
11351 /* Mark all sections which are to be included in the
11352 link. This will normally be every section. We need
11353 to do this so that we can identify any sections which
11354 the linker has decided to not include. */
11355 sec->linker_mark = TRUE;
11356
11357 if (sec->flags & SEC_MERGE)
11358 merged = TRUE;
11359
11360 if (sec->rawsize > max_contents_size)
11361 max_contents_size = sec->rawsize;
11362 if (sec->size > max_contents_size)
11363 max_contents_size = sec->size;
11364
11365 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11366 && (sec->owner->flags & DYNAMIC) == 0)
11367 {
11368 size_t sym_count;
11369
11370 /* We are interested in just local symbols, not all
11371 symbols. */
11372 if (elf_bad_symtab (sec->owner))
11373 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11374 / bed->s->sizeof_sym);
11375 else
11376 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11377
11378 if (sym_count > max_sym_count)
11379 max_sym_count = sym_count;
11380
11381 if (sym_count > max_sym_shndx_count
11382 && elf_symtab_shndx_list (sec->owner) != NULL)
11383 max_sym_shndx_count = sym_count;
11384
11385 if (esdo->this_hdr.sh_type == SHT_REL
11386 || esdo->this_hdr.sh_type == SHT_RELA)
11387 /* Some backends use reloc_count in relocation sections
11388 to count particular types of relocs. Of course,
11389 reloc sections themselves can't have relocations. */
11390 ;
11391 else if (emit_relocs)
11392 {
11393 reloc_count = sec->reloc_count;
11394 if (bed->elf_backend_count_additional_relocs)
11395 {
11396 int c;
11397 c = (*bed->elf_backend_count_additional_relocs) (sec);
11398 additional_reloc_count += c;
11399 }
11400 }
11401 else if (bed->elf_backend_count_relocs)
11402 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11403
11404 esdi = elf_section_data (sec);
11405
11406 if ((sec->flags & SEC_RELOC) != 0)
11407 {
11408 size_t ext_size = 0;
11409
11410 if (esdi->rel.hdr != NULL)
11411 ext_size = esdi->rel.hdr->sh_size;
11412 if (esdi->rela.hdr != NULL)
11413 ext_size += esdi->rela.hdr->sh_size;
11414
11415 if (ext_size > max_external_reloc_size)
11416 max_external_reloc_size = ext_size;
11417 if (sec->reloc_count > max_internal_reloc_count)
11418 max_internal_reloc_count = sec->reloc_count;
11419 }
11420 }
11421 }
11422
11423 if (reloc_count == 0)
11424 continue;
11425
11426 reloc_count += additional_reloc_count;
11427 o->reloc_count += reloc_count;
11428
11429 if (p->type == bfd_indirect_link_order && emit_relocs)
11430 {
11431 if (esdi->rel.hdr)
11432 {
11433 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11434 esdo->rel.count += additional_reloc_count;
11435 }
11436 if (esdi->rela.hdr)
11437 {
11438 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11439 esdo->rela.count += additional_reloc_count;
11440 }
11441 }
11442 else
11443 {
11444 if (o->use_rela_p)
11445 esdo->rela.count += reloc_count;
11446 else
11447 esdo->rel.count += reloc_count;
11448 }
11449 }
11450
11451 if (o->reloc_count > 0)
11452 o->flags |= SEC_RELOC;
11453 else
11454 {
11455 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11456 set it (this is probably a bug) and if it is set
11457 assign_section_numbers will create a reloc section. */
11458 o->flags &=~ SEC_RELOC;
11459 }
11460
11461 /* If the SEC_ALLOC flag is not set, force the section VMA to
11462 zero. This is done in elf_fake_sections as well, but forcing
11463 the VMA to 0 here will ensure that relocs against these
11464 sections are handled correctly. */
11465 if ((o->flags & SEC_ALLOC) == 0
11466 && ! o->user_set_vma)
11467 o->vma = 0;
11468 }
11469
11470 if (! bfd_link_relocatable (info) && merged)
11471 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
11472
11473 /* Figure out the file positions for everything but the symbol table
11474 and the relocs. We set symcount to force assign_section_numbers
11475 to create a symbol table. */
11476 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
11477 BFD_ASSERT (! abfd->output_has_begun);
11478 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11479 goto error_return;
11480
11481 /* Set sizes, and assign file positions for reloc sections. */
11482 for (o = abfd->sections; o != NULL; o = o->next)
11483 {
11484 struct bfd_elf_section_data *esdo = elf_section_data (o);
11485 if ((o->flags & SEC_RELOC) != 0)
11486 {
11487 if (esdo->rel.hdr
11488 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
11489 goto error_return;
11490
11491 if (esdo->rela.hdr
11492 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
11493 goto error_return;
11494 }
11495
11496 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11497 to count upwards while actually outputting the relocations. */
11498 esdo->rel.count = 0;
11499 esdo->rela.count = 0;
11500
11501 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11502 {
11503 /* Cache the section contents so that they can be compressed
11504 later. Use bfd_malloc since it will be freed by
11505 bfd_compress_section_contents. */
11506 unsigned char *contents = esdo->this_hdr.contents;
11507 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11508 abort ();
11509 contents
11510 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11511 if (contents == NULL)
11512 goto error_return;
11513 esdo->this_hdr.contents = contents;
11514 }
11515 }
11516
11517 /* We have now assigned file positions for all the sections except
11518 .symtab, .strtab, and non-loaded reloc sections. We start the
11519 .symtab section at the current file position, and write directly
11520 to it. We build the .strtab section in memory. */
11521 bfd_get_symcount (abfd) = 0;
11522 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11523 /* sh_name is set in prep_headers. */
11524 symtab_hdr->sh_type = SHT_SYMTAB;
11525 /* sh_flags, sh_addr and sh_size all start off zero. */
11526 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11527 /* sh_link is set in assign_section_numbers. */
11528 /* sh_info is set below. */
11529 /* sh_offset is set just below. */
11530 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
11531
11532 if (max_sym_count < 20)
11533 max_sym_count = 20;
11534 htab->strtabsize = max_sym_count;
11535 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11536 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
11537 if (htab->strtab == NULL)
11538 goto error_return;
11539 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11540 flinfo.symshndxbuf
11541 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11542 ? (Elf_External_Sym_Shndx *) -1 : NULL);
11543
11544 if (info->strip != strip_all || emit_relocs)
11545 {
11546 file_ptr off = elf_next_file_pos (abfd);
11547
11548 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11549
11550 /* Note that at this point elf_next_file_pos (abfd) is
11551 incorrect. We do not yet know the size of the .symtab section.
11552 We correct next_file_pos below, after we do know the size. */
11553
11554 /* Start writing out the symbol table. The first symbol is always a
11555 dummy symbol. */
11556 elfsym.st_value = 0;
11557 elfsym.st_size = 0;
11558 elfsym.st_info = 0;
11559 elfsym.st_other = 0;
11560 elfsym.st_shndx = SHN_UNDEF;
11561 elfsym.st_target_internal = 0;
11562 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11563 bfd_und_section_ptr, NULL) != 1)
11564 goto error_return;
11565
11566 /* Output a symbol for each section. We output these even if we are
11567 discarding local symbols, since they are used for relocs. These
11568 symbols have no names. We store the index of each one in the
11569 index field of the section, so that we can find it again when
11570 outputting relocs. */
11571
11572 elfsym.st_size = 0;
11573 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11574 elfsym.st_other = 0;
11575 elfsym.st_value = 0;
11576 elfsym.st_target_internal = 0;
11577 for (i = 1; i < elf_numsections (abfd); i++)
11578 {
11579 o = bfd_section_from_elf_index (abfd, i);
11580 if (o != NULL)
11581 {
11582 o->target_index = bfd_get_symcount (abfd);
11583 elfsym.st_shndx = i;
11584 if (!bfd_link_relocatable (info))
11585 elfsym.st_value = o->vma;
11586 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11587 NULL) != 1)
11588 goto error_return;
11589 }
11590 }
11591 }
11592
11593 /* Allocate some memory to hold information read in from the input
11594 files. */
11595 if (max_contents_size != 0)
11596 {
11597 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11598 if (flinfo.contents == NULL)
11599 goto error_return;
11600 }
11601
11602 if (max_external_reloc_size != 0)
11603 {
11604 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11605 if (flinfo.external_relocs == NULL)
11606 goto error_return;
11607 }
11608
11609 if (max_internal_reloc_count != 0)
11610 {
11611 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11612 amt *= sizeof (Elf_Internal_Rela);
11613 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11614 if (flinfo.internal_relocs == NULL)
11615 goto error_return;
11616 }
11617
11618 if (max_sym_count != 0)
11619 {
11620 amt = max_sym_count * bed->s->sizeof_sym;
11621 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11622 if (flinfo.external_syms == NULL)
11623 goto error_return;
11624
11625 amt = max_sym_count * sizeof (Elf_Internal_Sym);
11626 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11627 if (flinfo.internal_syms == NULL)
11628 goto error_return;
11629
11630 amt = max_sym_count * sizeof (long);
11631 flinfo.indices = (long int *) bfd_malloc (amt);
11632 if (flinfo.indices == NULL)
11633 goto error_return;
11634
11635 amt = max_sym_count * sizeof (asection *);
11636 flinfo.sections = (asection **) bfd_malloc (amt);
11637 if (flinfo.sections == NULL)
11638 goto error_return;
11639 }
11640
11641 if (max_sym_shndx_count != 0)
11642 {
11643 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
11644 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11645 if (flinfo.locsym_shndx == NULL)
11646 goto error_return;
11647 }
11648
11649 if (htab->tls_sec)
11650 {
11651 bfd_vma base, end = 0;
11652 asection *sec;
11653
11654 for (sec = htab->tls_sec;
11655 sec && (sec->flags & SEC_THREAD_LOCAL);
11656 sec = sec->next)
11657 {
11658 bfd_size_type size = sec->size;
11659
11660 if (size == 0
11661 && (sec->flags & SEC_HAS_CONTENTS) == 0)
11662 {
11663 struct bfd_link_order *ord = sec->map_tail.link_order;
11664
11665 if (ord != NULL)
11666 size = ord->offset + ord->size;
11667 }
11668 end = sec->vma + size;
11669 }
11670 base = htab->tls_sec->vma;
11671 /* Only align end of TLS section if static TLS doesn't have special
11672 alignment requirements. */
11673 if (bed->static_tls_alignment == 1)
11674 end = align_power (end, htab->tls_sec->alignment_power);
11675 htab->tls_size = end - base;
11676 }
11677
11678 /* Reorder SHF_LINK_ORDER sections. */
11679 for (o = abfd->sections; o != NULL; o = o->next)
11680 {
11681 if (!elf_fixup_link_order (abfd, o))
11682 return FALSE;
11683 }
11684
11685 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11686 return FALSE;
11687
11688 /* Since ELF permits relocations to be against local symbols, we
11689 must have the local symbols available when we do the relocations.
11690 Since we would rather only read the local symbols once, and we
11691 would rather not keep them in memory, we handle all the
11692 relocations for a single input file at the same time.
11693
11694 Unfortunately, there is no way to know the total number of local
11695 symbols until we have seen all of them, and the local symbol
11696 indices precede the global symbol indices. This means that when
11697 we are generating relocatable output, and we see a reloc against
11698 a global symbol, we can not know the symbol index until we have
11699 finished examining all the local symbols to see which ones we are
11700 going to output. To deal with this, we keep the relocations in
11701 memory, and don't output them until the end of the link. This is
11702 an unfortunate waste of memory, but I don't see a good way around
11703 it. Fortunately, it only happens when performing a relocatable
11704 link, which is not the common case. FIXME: If keep_memory is set
11705 we could write the relocs out and then read them again; I don't
11706 know how bad the memory loss will be. */
11707
11708 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11709 sub->output_has_begun = FALSE;
11710 for (o = abfd->sections; o != NULL; o = o->next)
11711 {
11712 for (p = o->map_head.link_order; p != NULL; p = p->next)
11713 {
11714 if (p->type == bfd_indirect_link_order
11715 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11716 == bfd_target_elf_flavour)
11717 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11718 {
11719 if (! sub->output_has_begun)
11720 {
11721 if (! elf_link_input_bfd (&flinfo, sub))
11722 goto error_return;
11723 sub->output_has_begun = TRUE;
11724 }
11725 }
11726 else if (p->type == bfd_section_reloc_link_order
11727 || p->type == bfd_symbol_reloc_link_order)
11728 {
11729 if (! elf_reloc_link_order (abfd, info, o, p))
11730 goto error_return;
11731 }
11732 else
11733 {
11734 if (! _bfd_default_link_order (abfd, info, o, p))
11735 {
11736 if (p->type == bfd_indirect_link_order
11737 && (bfd_get_flavour (sub)
11738 == bfd_target_elf_flavour)
11739 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11740 != bed->s->elfclass))
11741 {
11742 const char *iclass, *oclass;
11743
11744 switch (bed->s->elfclass)
11745 {
11746 case ELFCLASS64: oclass = "ELFCLASS64"; break;
11747 case ELFCLASS32: oclass = "ELFCLASS32"; break;
11748 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
11749 default: abort ();
11750 }
11751
11752 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
11753 {
11754 case ELFCLASS64: iclass = "ELFCLASS64"; break;
11755 case ELFCLASS32: iclass = "ELFCLASS32"; break;
11756 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
11757 default: abort ();
11758 }
11759
11760 bfd_set_error (bfd_error_wrong_format);
11761 _bfd_error_handler
11762 /* xgettext:c-format */
11763 (_("%B: file class %s incompatible with %s"),
11764 sub, iclass, oclass);
11765 }
11766
11767 goto error_return;
11768 }
11769 }
11770 }
11771 }
11772
11773 /* Free symbol buffer if needed. */
11774 if (!info->reduce_memory_overheads)
11775 {
11776 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11777 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11778 && elf_tdata (sub)->symbuf)
11779 {
11780 free (elf_tdata (sub)->symbuf);
11781 elf_tdata (sub)->symbuf = NULL;
11782 }
11783 }
11784
11785 /* Output any global symbols that got converted to local in a
11786 version script or due to symbol visibility. We do this in a
11787 separate step since ELF requires all local symbols to appear
11788 prior to any global symbols. FIXME: We should only do this if
11789 some global symbols were, in fact, converted to become local.
11790 FIXME: Will this work correctly with the Irix 5 linker? */
11791 eoinfo.failed = FALSE;
11792 eoinfo.flinfo = &flinfo;
11793 eoinfo.localsyms = TRUE;
11794 eoinfo.file_sym_done = FALSE;
11795 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11796 if (eoinfo.failed)
11797 return FALSE;
11798
11799 /* If backend needs to output some local symbols not present in the hash
11800 table, do it now. */
11801 if (bed->elf_backend_output_arch_local_syms
11802 && (info->strip != strip_all || emit_relocs))
11803 {
11804 typedef int (*out_sym_func)
11805 (void *, const char *, Elf_Internal_Sym *, asection *,
11806 struct elf_link_hash_entry *);
11807
11808 if (! ((*bed->elf_backend_output_arch_local_syms)
11809 (abfd, info, &flinfo,
11810 (out_sym_func) elf_link_output_symstrtab)))
11811 return FALSE;
11812 }
11813
11814 /* That wrote out all the local symbols. Finish up the symbol table
11815 with the global symbols. Even if we want to strip everything we
11816 can, we still need to deal with those global symbols that got
11817 converted to local in a version script. */
11818
11819 /* The sh_info field records the index of the first non local symbol. */
11820 symtab_hdr->sh_info = bfd_get_symcount (abfd);
11821
11822 if (dynamic
11823 && htab->dynsym != NULL
11824 && htab->dynsym->output_section != bfd_abs_section_ptr)
11825 {
11826 Elf_Internal_Sym sym;
11827 bfd_byte *dynsym = htab->dynsym->contents;
11828
11829 o = htab->dynsym->output_section;
11830 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
11831
11832 /* Write out the section symbols for the output sections. */
11833 if (bfd_link_pic (info)
11834 || htab->is_relocatable_executable)
11835 {
11836 asection *s;
11837
11838 sym.st_size = 0;
11839 sym.st_name = 0;
11840 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11841 sym.st_other = 0;
11842 sym.st_target_internal = 0;
11843
11844 for (s = abfd->sections; s != NULL; s = s->next)
11845 {
11846 int indx;
11847 bfd_byte *dest;
11848 long dynindx;
11849
11850 dynindx = elf_section_data (s)->dynindx;
11851 if (dynindx <= 0)
11852 continue;
11853 indx = elf_section_data (s)->this_idx;
11854 BFD_ASSERT (indx > 0);
11855 sym.st_shndx = indx;
11856 if (! check_dynsym (abfd, &sym))
11857 return FALSE;
11858 sym.st_value = s->vma;
11859 dest = dynsym + dynindx * bed->s->sizeof_sym;
11860 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11861 }
11862 }
11863
11864 /* Write out the local dynsyms. */
11865 if (htab->dynlocal)
11866 {
11867 struct elf_link_local_dynamic_entry *e;
11868 for (e = htab->dynlocal; e ; e = e->next)
11869 {
11870 asection *s;
11871 bfd_byte *dest;
11872
11873 /* Copy the internal symbol and turn off visibility.
11874 Note that we saved a word of storage and overwrote
11875 the original st_name with the dynstr_index. */
11876 sym = e->isym;
11877 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
11878
11879 s = bfd_section_from_elf_index (e->input_bfd,
11880 e->isym.st_shndx);
11881 if (s != NULL)
11882 {
11883 sym.st_shndx =
11884 elf_section_data (s->output_section)->this_idx;
11885 if (! check_dynsym (abfd, &sym))
11886 return FALSE;
11887 sym.st_value = (s->output_section->vma
11888 + s->output_offset
11889 + e->isym.st_value);
11890 }
11891
11892 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
11893 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11894 }
11895 }
11896 }
11897
11898 /* We get the global symbols from the hash table. */
11899 eoinfo.failed = FALSE;
11900 eoinfo.localsyms = FALSE;
11901 eoinfo.flinfo = &flinfo;
11902 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11903 if (eoinfo.failed)
11904 return FALSE;
11905
11906 /* If backend needs to output some symbols not present in the hash
11907 table, do it now. */
11908 if (bed->elf_backend_output_arch_syms
11909 && (info->strip != strip_all || emit_relocs))
11910 {
11911 typedef int (*out_sym_func)
11912 (void *, const char *, Elf_Internal_Sym *, asection *,
11913 struct elf_link_hash_entry *);
11914
11915 if (! ((*bed->elf_backend_output_arch_syms)
11916 (abfd, info, &flinfo,
11917 (out_sym_func) elf_link_output_symstrtab)))
11918 return FALSE;
11919 }
11920
11921 /* Finalize the .strtab section. */
11922 _bfd_elf_strtab_finalize (flinfo.symstrtab);
11923
11924 /* Swap out the .strtab section. */
11925 if (!elf_link_swap_symbols_out (&flinfo))
11926 return FALSE;
11927
11928 /* Now we know the size of the symtab section. */
11929 if (bfd_get_symcount (abfd) > 0)
11930 {
11931 /* Finish up and write out the symbol string table (.strtab)
11932 section. */
11933 Elf_Internal_Shdr *symstrtab_hdr;
11934 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
11935
11936 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
11937 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
11938 {
11939 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
11940 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
11941 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
11942 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
11943 symtab_shndx_hdr->sh_size = amt;
11944
11945 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
11946 off, TRUE);
11947
11948 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
11949 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
11950 return FALSE;
11951 }
11952
11953 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
11954 /* sh_name was set in prep_headers. */
11955 symstrtab_hdr->sh_type = SHT_STRTAB;
11956 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
11957 symstrtab_hdr->sh_addr = 0;
11958 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
11959 symstrtab_hdr->sh_entsize = 0;
11960 symstrtab_hdr->sh_link = 0;
11961 symstrtab_hdr->sh_info = 0;
11962 /* sh_offset is set just below. */
11963 symstrtab_hdr->sh_addralign = 1;
11964
11965 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
11966 off, TRUE);
11967 elf_next_file_pos (abfd) = off;
11968
11969 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
11970 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
11971 return FALSE;
11972 }
11973
11974 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
11975 {
11976 _bfd_error_handler (_("%B: failed to generate import library"),
11977 info->out_implib_bfd);
11978 return FALSE;
11979 }
11980
11981 /* Adjust the relocs to have the correct symbol indices. */
11982 for (o = abfd->sections; o != NULL; o = o->next)
11983 {
11984 struct bfd_elf_section_data *esdo = elf_section_data (o);
11985 bfd_boolean sort;
11986 if ((o->flags & SEC_RELOC) == 0)
11987 continue;
11988
11989 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
11990 if (esdo->rel.hdr != NULL
11991 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort))
11992 return FALSE;
11993 if (esdo->rela.hdr != NULL
11994 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort))
11995 return FALSE;
11996
11997 /* Set the reloc_count field to 0 to prevent write_relocs from
11998 trying to swap the relocs out itself. */
11999 o->reloc_count = 0;
12000 }
12001
12002 if (dynamic && info->combreloc && dynobj != NULL)
12003 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12004
12005 /* If we are linking against a dynamic object, or generating a
12006 shared library, finish up the dynamic linking information. */
12007 if (dynamic)
12008 {
12009 bfd_byte *dyncon, *dynconend;
12010
12011 /* Fix up .dynamic entries. */
12012 o = bfd_get_linker_section (dynobj, ".dynamic");
12013 BFD_ASSERT (o != NULL);
12014
12015 dyncon = o->contents;
12016 dynconend = o->contents + o->size;
12017 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12018 {
12019 Elf_Internal_Dyn dyn;
12020 const char *name;
12021 unsigned int type;
12022
12023 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12024
12025 switch (dyn.d_tag)
12026 {
12027 default:
12028 continue;
12029 case DT_NULL:
12030 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12031 {
12032 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12033 {
12034 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12035 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12036 default: continue;
12037 }
12038 dyn.d_un.d_val = relativecount;
12039 relativecount = 0;
12040 break;
12041 }
12042 continue;
12043
12044 case DT_INIT:
12045 name = info->init_function;
12046 goto get_sym;
12047 case DT_FINI:
12048 name = info->fini_function;
12049 get_sym:
12050 {
12051 struct elf_link_hash_entry *h;
12052
12053 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
12054 if (h != NULL
12055 && (h->root.type == bfd_link_hash_defined
12056 || h->root.type == bfd_link_hash_defweak))
12057 {
12058 dyn.d_un.d_ptr = h->root.u.def.value;
12059 o = h->root.u.def.section;
12060 if (o->output_section != NULL)
12061 dyn.d_un.d_ptr += (o->output_section->vma
12062 + o->output_offset);
12063 else
12064 {
12065 /* The symbol is imported from another shared
12066 library and does not apply to this one. */
12067 dyn.d_un.d_ptr = 0;
12068 }
12069 break;
12070 }
12071 }
12072 continue;
12073
12074 case DT_PREINIT_ARRAYSZ:
12075 name = ".preinit_array";
12076 goto get_out_size;
12077 case DT_INIT_ARRAYSZ:
12078 name = ".init_array";
12079 goto get_out_size;
12080 case DT_FINI_ARRAYSZ:
12081 name = ".fini_array";
12082 get_out_size:
12083 o = bfd_get_section_by_name (abfd, name);
12084 if (o == NULL)
12085 {
12086 _bfd_error_handler
12087 (_("could not find section %s"), name);
12088 goto error_return;
12089 }
12090 if (o->size == 0)
12091 _bfd_error_handler
12092 (_("warning: %s section has zero size"), name);
12093 dyn.d_un.d_val = o->size;
12094 break;
12095
12096 case DT_PREINIT_ARRAY:
12097 name = ".preinit_array";
12098 goto get_out_vma;
12099 case DT_INIT_ARRAY:
12100 name = ".init_array";
12101 goto get_out_vma;
12102 case DT_FINI_ARRAY:
12103 name = ".fini_array";
12104 get_out_vma:
12105 o = bfd_get_section_by_name (abfd, name);
12106 goto do_vma;
12107
12108 case DT_HASH:
12109 name = ".hash";
12110 goto get_vma;
12111 case DT_GNU_HASH:
12112 name = ".gnu.hash";
12113 goto get_vma;
12114 case DT_STRTAB:
12115 name = ".dynstr";
12116 goto get_vma;
12117 case DT_SYMTAB:
12118 name = ".dynsym";
12119 goto get_vma;
12120 case DT_VERDEF:
12121 name = ".gnu.version_d";
12122 goto get_vma;
12123 case DT_VERNEED:
12124 name = ".gnu.version_r";
12125 goto get_vma;
12126 case DT_VERSYM:
12127 name = ".gnu.version";
12128 get_vma:
12129 o = bfd_get_linker_section (dynobj, name);
12130 do_vma:
12131 if (o == NULL)
12132 {
12133 _bfd_error_handler
12134 (_("could not find section %s"), name);
12135 goto error_return;
12136 }
12137 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12138 {
12139 _bfd_error_handler
12140 (_("warning: section '%s' is being made into a note"), name);
12141 bfd_set_error (bfd_error_nonrepresentable_section);
12142 goto error_return;
12143 }
12144 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
12145 break;
12146
12147 case DT_REL:
12148 case DT_RELA:
12149 case DT_RELSZ:
12150 case DT_RELASZ:
12151 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12152 type = SHT_REL;
12153 else
12154 type = SHT_RELA;
12155 dyn.d_un.d_val = 0;
12156 dyn.d_un.d_ptr = 0;
12157 for (i = 1; i < elf_numsections (abfd); i++)
12158 {
12159 Elf_Internal_Shdr *hdr;
12160
12161 hdr = elf_elfsections (abfd)[i];
12162 if (hdr->sh_type == type
12163 && (hdr->sh_flags & SHF_ALLOC) != 0)
12164 {
12165 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12166 dyn.d_un.d_val += hdr->sh_size;
12167 else
12168 {
12169 if (dyn.d_un.d_ptr == 0
12170 || hdr->sh_addr < dyn.d_un.d_ptr)
12171 dyn.d_un.d_ptr = hdr->sh_addr;
12172 }
12173 }
12174 }
12175 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12176 {
12177 /* Don't count procedure linkage table relocs in the
12178 overall reloc count. */
12179 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12180 dyn.d_un.d_val -= htab->srelplt->size;
12181 /* If .rela.plt is the first .rela section, exclude
12182 it from DT_RELA. */
12183 else if (dyn.d_un.d_ptr == (htab->srelplt->output_section->vma
12184 + htab->srelplt->output_offset))
12185 dyn.d_un.d_ptr += htab->srelplt->size;
12186 }
12187 break;
12188 }
12189 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12190 }
12191 }
12192
12193 /* If we have created any dynamic sections, then output them. */
12194 if (dynobj != NULL)
12195 {
12196 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12197 goto error_return;
12198
12199 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12200 if (((info->warn_shared_textrel && bfd_link_pic (info))
12201 || info->error_textrel)
12202 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
12203 {
12204 bfd_byte *dyncon, *dynconend;
12205
12206 dyncon = o->contents;
12207 dynconend = o->contents + o->size;
12208 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12209 {
12210 Elf_Internal_Dyn dyn;
12211
12212 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12213
12214 if (dyn.d_tag == DT_TEXTREL)
12215 {
12216 if (info->error_textrel)
12217 info->callbacks->einfo
12218 (_("%P%X: read-only segment has dynamic relocations.\n"));
12219 else
12220 info->callbacks->einfo
12221 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
12222 break;
12223 }
12224 }
12225 }
12226
12227 for (o = dynobj->sections; o != NULL; o = o->next)
12228 {
12229 if ((o->flags & SEC_HAS_CONTENTS) == 0
12230 || o->size == 0
12231 || o->output_section == bfd_abs_section_ptr)
12232 continue;
12233 if ((o->flags & SEC_LINKER_CREATED) == 0)
12234 {
12235 /* At this point, we are only interested in sections
12236 created by _bfd_elf_link_create_dynamic_sections. */
12237 continue;
12238 }
12239 if (htab->stab_info.stabstr == o)
12240 continue;
12241 if (htab->eh_info.hdr_sec == o)
12242 continue;
12243 if (strcmp (o->name, ".dynstr") != 0)
12244 {
12245 if (! bfd_set_section_contents (abfd, o->output_section,
12246 o->contents,
12247 (file_ptr) o->output_offset
12248 * bfd_octets_per_byte (abfd),
12249 o->size))
12250 goto error_return;
12251 }
12252 else
12253 {
12254 /* The contents of the .dynstr section are actually in a
12255 stringtab. */
12256 file_ptr off;
12257
12258 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12259 if (bfd_seek (abfd, off, SEEK_SET) != 0
12260 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
12261 goto error_return;
12262 }
12263 }
12264 }
12265
12266 if (bfd_link_relocatable (info))
12267 {
12268 bfd_boolean failed = FALSE;
12269
12270 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12271 if (failed)
12272 goto error_return;
12273 }
12274
12275 /* If we have optimized stabs strings, output them. */
12276 if (htab->stab_info.stabstr != NULL)
12277 {
12278 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
12279 goto error_return;
12280 }
12281
12282 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12283 goto error_return;
12284
12285 elf_final_link_free (abfd, &flinfo);
12286
12287 elf_linker (abfd) = TRUE;
12288
12289 if (attr_section)
12290 {
12291 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
12292 if (contents == NULL)
12293 return FALSE; /* Bail out and fail. */
12294 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12295 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12296 free (contents);
12297 }
12298
12299 return TRUE;
12300
12301 error_return:
12302 elf_final_link_free (abfd, &flinfo);
12303 return FALSE;
12304 }
12305 \f
12306 /* Initialize COOKIE for input bfd ABFD. */
12307
12308 static bfd_boolean
12309 init_reloc_cookie (struct elf_reloc_cookie *cookie,
12310 struct bfd_link_info *info, bfd *abfd)
12311 {
12312 Elf_Internal_Shdr *symtab_hdr;
12313 const struct elf_backend_data *bed;
12314
12315 bed = get_elf_backend_data (abfd);
12316 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12317
12318 cookie->abfd = abfd;
12319 cookie->sym_hashes = elf_sym_hashes (abfd);
12320 cookie->bad_symtab = elf_bad_symtab (abfd);
12321 if (cookie->bad_symtab)
12322 {
12323 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12324 cookie->extsymoff = 0;
12325 }
12326 else
12327 {
12328 cookie->locsymcount = symtab_hdr->sh_info;
12329 cookie->extsymoff = symtab_hdr->sh_info;
12330 }
12331
12332 if (bed->s->arch_size == 32)
12333 cookie->r_sym_shift = 8;
12334 else
12335 cookie->r_sym_shift = 32;
12336
12337 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12338 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12339 {
12340 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12341 cookie->locsymcount, 0,
12342 NULL, NULL, NULL);
12343 if (cookie->locsyms == NULL)
12344 {
12345 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12346 return FALSE;
12347 }
12348 if (info->keep_memory)
12349 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12350 }
12351 return TRUE;
12352 }
12353
12354 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12355
12356 static void
12357 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12358 {
12359 Elf_Internal_Shdr *symtab_hdr;
12360
12361 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12362 if (cookie->locsyms != NULL
12363 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12364 free (cookie->locsyms);
12365 }
12366
12367 /* Initialize the relocation information in COOKIE for input section SEC
12368 of input bfd ABFD. */
12369
12370 static bfd_boolean
12371 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12372 struct bfd_link_info *info, bfd *abfd,
12373 asection *sec)
12374 {
12375 const struct elf_backend_data *bed;
12376
12377 if (sec->reloc_count == 0)
12378 {
12379 cookie->rels = NULL;
12380 cookie->relend = NULL;
12381 }
12382 else
12383 {
12384 bed = get_elf_backend_data (abfd);
12385
12386 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12387 info->keep_memory);
12388 if (cookie->rels == NULL)
12389 return FALSE;
12390 cookie->rel = cookie->rels;
12391 cookie->relend = (cookie->rels
12392 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
12393 }
12394 cookie->rel = cookie->rels;
12395 return TRUE;
12396 }
12397
12398 /* Free the memory allocated by init_reloc_cookie_rels,
12399 if appropriate. */
12400
12401 static void
12402 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12403 asection *sec)
12404 {
12405 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12406 free (cookie->rels);
12407 }
12408
12409 /* Initialize the whole of COOKIE for input section SEC. */
12410
12411 static bfd_boolean
12412 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12413 struct bfd_link_info *info,
12414 asection *sec)
12415 {
12416 if (!init_reloc_cookie (cookie, info, sec->owner))
12417 goto error1;
12418 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12419 goto error2;
12420 return TRUE;
12421
12422 error2:
12423 fini_reloc_cookie (cookie, sec->owner);
12424 error1:
12425 return FALSE;
12426 }
12427
12428 /* Free the memory allocated by init_reloc_cookie_for_section,
12429 if appropriate. */
12430
12431 static void
12432 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12433 asection *sec)
12434 {
12435 fini_reloc_cookie_rels (cookie, sec);
12436 fini_reloc_cookie (cookie, sec->owner);
12437 }
12438 \f
12439 /* Garbage collect unused sections. */
12440
12441 /* Default gc_mark_hook. */
12442
12443 asection *
12444 _bfd_elf_gc_mark_hook (asection *sec,
12445 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12446 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12447 struct elf_link_hash_entry *h,
12448 Elf_Internal_Sym *sym)
12449 {
12450 if (h != NULL)
12451 {
12452 switch (h->root.type)
12453 {
12454 case bfd_link_hash_defined:
12455 case bfd_link_hash_defweak:
12456 return h->root.u.def.section;
12457
12458 case bfd_link_hash_common:
12459 return h->root.u.c.p->section;
12460
12461 default:
12462 break;
12463 }
12464 }
12465 else
12466 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12467
12468 return NULL;
12469 }
12470
12471 /* For undefined __start_<name> and __stop_<name> symbols, return the
12472 first input section matching <name>. Return NULL otherwise. */
12473
12474 asection *
12475 _bfd_elf_is_start_stop (const struct bfd_link_info *info,
12476 struct elf_link_hash_entry *h)
12477 {
12478 asection *s;
12479 const char *sec_name;
12480
12481 if (h->root.type != bfd_link_hash_undefined
12482 && h->root.type != bfd_link_hash_undefweak)
12483 return NULL;
12484
12485 s = h->root.u.undef.section;
12486 if (s != NULL)
12487 {
12488 if (s == (asection *) 0 - 1)
12489 return NULL;
12490 return s;
12491 }
12492
12493 sec_name = NULL;
12494 if (strncmp (h->root.root.string, "__start_", 8) == 0)
12495 sec_name = h->root.root.string + 8;
12496 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
12497 sec_name = h->root.root.string + 7;
12498
12499 if (sec_name != NULL && *sec_name != '\0')
12500 {
12501 bfd *i;
12502
12503 for (i = info->input_bfds; i != NULL; i = i->link.next)
12504 {
12505 s = bfd_get_section_by_name (i, sec_name);
12506 if (s != NULL)
12507 {
12508 h->root.u.undef.section = s;
12509 break;
12510 }
12511 }
12512 }
12513
12514 if (s == NULL)
12515 h->root.u.undef.section = (asection *) 0 - 1;
12516
12517 return s;
12518 }
12519
12520 /* COOKIE->rel describes a relocation against section SEC, which is
12521 a section we've decided to keep. Return the section that contains
12522 the relocation symbol, or NULL if no section contains it. */
12523
12524 asection *
12525 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12526 elf_gc_mark_hook_fn gc_mark_hook,
12527 struct elf_reloc_cookie *cookie,
12528 bfd_boolean *start_stop)
12529 {
12530 unsigned long r_symndx;
12531 struct elf_link_hash_entry *h;
12532
12533 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
12534 if (r_symndx == STN_UNDEF)
12535 return NULL;
12536
12537 if (r_symndx >= cookie->locsymcount
12538 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12539 {
12540 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
12541 if (h == NULL)
12542 {
12543 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12544 sec->owner);
12545 return NULL;
12546 }
12547 while (h->root.type == bfd_link_hash_indirect
12548 || h->root.type == bfd_link_hash_warning)
12549 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12550 h->mark = 1;
12551 /* If this symbol is weak and there is a non-weak definition, we
12552 keep the non-weak definition because many backends put
12553 dynamic reloc info on the non-weak definition for code
12554 handling copy relocs. */
12555 if (h->u.weakdef != NULL)
12556 h->u.weakdef->mark = 1;
12557
12558 if (start_stop != NULL)
12559 {
12560 /* To work around a glibc bug, mark all XXX input sections
12561 when there is an as yet undefined reference to __start_XXX
12562 or __stop_XXX symbols. The linker will later define such
12563 symbols for orphan input sections that have a name
12564 representable as a C identifier. */
12565 asection *s = _bfd_elf_is_start_stop (info, h);
12566
12567 if (s != NULL)
12568 {
12569 *start_stop = !s->gc_mark;
12570 return s;
12571 }
12572 }
12573
12574 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12575 }
12576
12577 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12578 &cookie->locsyms[r_symndx]);
12579 }
12580
12581 /* COOKIE->rel describes a relocation against section SEC, which is
12582 a section we've decided to keep. Mark the section that contains
12583 the relocation symbol. */
12584
12585 bfd_boolean
12586 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12587 asection *sec,
12588 elf_gc_mark_hook_fn gc_mark_hook,
12589 struct elf_reloc_cookie *cookie)
12590 {
12591 asection *rsec;
12592 bfd_boolean start_stop = FALSE;
12593
12594 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12595 while (rsec != NULL)
12596 {
12597 if (!rsec->gc_mark)
12598 {
12599 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12600 || (rsec->owner->flags & DYNAMIC) != 0)
12601 rsec->gc_mark = 1;
12602 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12603 return FALSE;
12604 }
12605 if (!start_stop)
12606 break;
12607 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
12608 }
12609 return TRUE;
12610 }
12611
12612 /* The mark phase of garbage collection. For a given section, mark
12613 it and any sections in this section's group, and all the sections
12614 which define symbols to which it refers. */
12615
12616 bfd_boolean
12617 _bfd_elf_gc_mark (struct bfd_link_info *info,
12618 asection *sec,
12619 elf_gc_mark_hook_fn gc_mark_hook)
12620 {
12621 bfd_boolean ret;
12622 asection *group_sec, *eh_frame;
12623
12624 sec->gc_mark = 1;
12625
12626 /* Mark all the sections in the group. */
12627 group_sec = elf_section_data (sec)->next_in_group;
12628 if (group_sec && !group_sec->gc_mark)
12629 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
12630 return FALSE;
12631
12632 /* Look through the section relocs. */
12633 ret = TRUE;
12634 eh_frame = elf_eh_frame_section (sec->owner);
12635 if ((sec->flags & SEC_RELOC) != 0
12636 && sec->reloc_count > 0
12637 && sec != eh_frame)
12638 {
12639 struct elf_reloc_cookie cookie;
12640
12641 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12642 ret = FALSE;
12643 else
12644 {
12645 for (; cookie.rel < cookie.relend; cookie.rel++)
12646 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
12647 {
12648 ret = FALSE;
12649 break;
12650 }
12651 fini_reloc_cookie_for_section (&cookie, sec);
12652 }
12653 }
12654
12655 if (ret && eh_frame && elf_fde_list (sec))
12656 {
12657 struct elf_reloc_cookie cookie;
12658
12659 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12660 ret = FALSE;
12661 else
12662 {
12663 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12664 gc_mark_hook, &cookie))
12665 ret = FALSE;
12666 fini_reloc_cookie_for_section (&cookie, eh_frame);
12667 }
12668 }
12669
12670 eh_frame = elf_section_eh_frame_entry (sec);
12671 if (ret && eh_frame && !eh_frame->gc_mark)
12672 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12673 ret = FALSE;
12674
12675 return ret;
12676 }
12677
12678 /* Scan and mark sections in a special or debug section group. */
12679
12680 static void
12681 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12682 {
12683 /* Point to first section of section group. */
12684 asection *ssec;
12685 /* Used to iterate the section group. */
12686 asection *msec;
12687
12688 bfd_boolean is_special_grp = TRUE;
12689 bfd_boolean is_debug_grp = TRUE;
12690
12691 /* First scan to see if group contains any section other than debug
12692 and special section. */
12693 ssec = msec = elf_next_in_group (grp);
12694 do
12695 {
12696 if ((msec->flags & SEC_DEBUGGING) == 0)
12697 is_debug_grp = FALSE;
12698
12699 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12700 is_special_grp = FALSE;
12701
12702 msec = elf_next_in_group (msec);
12703 }
12704 while (msec != ssec);
12705
12706 /* If this is a pure debug section group or pure special section group,
12707 keep all sections in this group. */
12708 if (is_debug_grp || is_special_grp)
12709 {
12710 do
12711 {
12712 msec->gc_mark = 1;
12713 msec = elf_next_in_group (msec);
12714 }
12715 while (msec != ssec);
12716 }
12717 }
12718
12719 /* Keep debug and special sections. */
12720
12721 bfd_boolean
12722 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12723 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12724 {
12725 bfd *ibfd;
12726
12727 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12728 {
12729 asection *isec;
12730 bfd_boolean some_kept;
12731 bfd_boolean debug_frag_seen;
12732
12733 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12734 continue;
12735
12736 /* Ensure all linker created sections are kept,
12737 see if any other section is already marked,
12738 and note if we have any fragmented debug sections. */
12739 debug_frag_seen = some_kept = FALSE;
12740 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12741 {
12742 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12743 isec->gc_mark = 1;
12744 else if (isec->gc_mark)
12745 some_kept = TRUE;
12746
12747 if (debug_frag_seen == FALSE
12748 && (isec->flags & SEC_DEBUGGING)
12749 && CONST_STRNEQ (isec->name, ".debug_line."))
12750 debug_frag_seen = TRUE;
12751 }
12752
12753 /* If no section in this file will be kept, then we can
12754 toss out the debug and special sections. */
12755 if (!some_kept)
12756 continue;
12757
12758 /* Keep debug and special sections like .comment when they are
12759 not part of a group. Also keep section groups that contain
12760 just debug sections or special sections. */
12761 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12762 {
12763 if ((isec->flags & SEC_GROUP) != 0)
12764 _bfd_elf_gc_mark_debug_special_section_group (isec);
12765 else if (((isec->flags & SEC_DEBUGGING) != 0
12766 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12767 && elf_next_in_group (isec) == NULL)
12768 isec->gc_mark = 1;
12769 }
12770
12771 if (! debug_frag_seen)
12772 continue;
12773
12774 /* Look for CODE sections which are going to be discarded,
12775 and find and discard any fragmented debug sections which
12776 are associated with that code section. */
12777 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12778 if ((isec->flags & SEC_CODE) != 0
12779 && isec->gc_mark == 0)
12780 {
12781 unsigned int ilen;
12782 asection *dsec;
12783
12784 ilen = strlen (isec->name);
12785
12786 /* Association is determined by the name of the debug section
12787 containing the name of the code section as a suffix. For
12788 example .debug_line.text.foo is a debug section associated
12789 with .text.foo. */
12790 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12791 {
12792 unsigned int dlen;
12793
12794 if (dsec->gc_mark == 0
12795 || (dsec->flags & SEC_DEBUGGING) == 0)
12796 continue;
12797
12798 dlen = strlen (dsec->name);
12799
12800 if (dlen > ilen
12801 && strncmp (dsec->name + (dlen - ilen),
12802 isec->name, ilen) == 0)
12803 {
12804 dsec->gc_mark = 0;
12805 }
12806 }
12807 }
12808 }
12809 return TRUE;
12810 }
12811
12812 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
12813
12814 struct elf_gc_sweep_symbol_info
12815 {
12816 struct bfd_link_info *info;
12817 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
12818 bfd_boolean);
12819 };
12820
12821 static bfd_boolean
12822 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
12823 {
12824 if (!h->mark
12825 && (((h->root.type == bfd_link_hash_defined
12826 || h->root.type == bfd_link_hash_defweak)
12827 && !((h->def_regular || ELF_COMMON_DEF_P (h))
12828 && h->root.u.def.section->gc_mark))
12829 || h->root.type == bfd_link_hash_undefined
12830 || h->root.type == bfd_link_hash_undefweak))
12831 {
12832 struct elf_gc_sweep_symbol_info *inf;
12833
12834 inf = (struct elf_gc_sweep_symbol_info *) data;
12835 (*inf->hide_symbol) (inf->info, h, TRUE);
12836 h->def_regular = 0;
12837 h->ref_regular = 0;
12838 h->ref_regular_nonweak = 0;
12839 }
12840
12841 return TRUE;
12842 }
12843
12844 /* The sweep phase of garbage collection. Remove all garbage sections. */
12845
12846 typedef bfd_boolean (*gc_sweep_hook_fn)
12847 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
12848
12849 static bfd_boolean
12850 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
12851 {
12852 bfd *sub;
12853 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12854 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
12855 unsigned long section_sym_count;
12856 struct elf_gc_sweep_symbol_info sweep_info;
12857
12858 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12859 {
12860 asection *o;
12861
12862 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12863 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
12864 continue;
12865
12866 for (o = sub->sections; o != NULL; o = o->next)
12867 {
12868 /* When any section in a section group is kept, we keep all
12869 sections in the section group. If the first member of
12870 the section group is excluded, we will also exclude the
12871 group section. */
12872 if (o->flags & SEC_GROUP)
12873 {
12874 asection *first = elf_next_in_group (o);
12875 o->gc_mark = first->gc_mark;
12876 }
12877
12878 if (o->gc_mark)
12879 continue;
12880
12881 /* Skip sweeping sections already excluded. */
12882 if (o->flags & SEC_EXCLUDE)
12883 continue;
12884
12885 /* Since this is early in the link process, it is simple
12886 to remove a section from the output. */
12887 o->flags |= SEC_EXCLUDE;
12888
12889 if (info->print_gc_sections && o->size != 0)
12890 /* xgettext:c-format */
12891 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
12892
12893 /* But we also have to update some of the relocation
12894 info we collected before. */
12895 if (gc_sweep_hook
12896 && (o->flags & SEC_RELOC) != 0
12897 && o->reloc_count != 0
12898 && !((info->strip == strip_all || info->strip == strip_debugger)
12899 && (o->flags & SEC_DEBUGGING) != 0)
12900 && !bfd_is_abs_section (o->output_section))
12901 {
12902 Elf_Internal_Rela *internal_relocs;
12903 bfd_boolean r;
12904
12905 internal_relocs
12906 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
12907 info->keep_memory);
12908 if (internal_relocs == NULL)
12909 return FALSE;
12910
12911 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
12912
12913 if (elf_section_data (o)->relocs != internal_relocs)
12914 free (internal_relocs);
12915
12916 if (!r)
12917 return FALSE;
12918 }
12919 }
12920 }
12921
12922 /* Remove the symbols that were in the swept sections from the dynamic
12923 symbol table. GCFIXME: Anyone know how to get them out of the
12924 static symbol table as well? */
12925 sweep_info.info = info;
12926 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
12927 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
12928 &sweep_info);
12929
12930 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
12931 return TRUE;
12932 }
12933
12934 /* Propagate collected vtable information. This is called through
12935 elf_link_hash_traverse. */
12936
12937 static bfd_boolean
12938 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
12939 {
12940 /* Those that are not vtables. */
12941 if (h->vtable == NULL || h->vtable->parent == NULL)
12942 return TRUE;
12943
12944 /* Those vtables that do not have parents, we cannot merge. */
12945 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
12946 return TRUE;
12947
12948 /* If we've already been done, exit. */
12949 if (h->vtable->used && h->vtable->used[-1])
12950 return TRUE;
12951
12952 /* Make sure the parent's table is up to date. */
12953 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
12954
12955 if (h->vtable->used == NULL)
12956 {
12957 /* None of this table's entries were referenced. Re-use the
12958 parent's table. */
12959 h->vtable->used = h->vtable->parent->vtable->used;
12960 h->vtable->size = h->vtable->parent->vtable->size;
12961 }
12962 else
12963 {
12964 size_t n;
12965 bfd_boolean *cu, *pu;
12966
12967 /* Or the parent's entries into ours. */
12968 cu = h->vtable->used;
12969 cu[-1] = TRUE;
12970 pu = h->vtable->parent->vtable->used;
12971 if (pu != NULL)
12972 {
12973 const struct elf_backend_data *bed;
12974 unsigned int log_file_align;
12975
12976 bed = get_elf_backend_data (h->root.u.def.section->owner);
12977 log_file_align = bed->s->log_file_align;
12978 n = h->vtable->parent->vtable->size >> log_file_align;
12979 while (n--)
12980 {
12981 if (*pu)
12982 *cu = TRUE;
12983 pu++;
12984 cu++;
12985 }
12986 }
12987 }
12988
12989 return TRUE;
12990 }
12991
12992 static bfd_boolean
12993 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
12994 {
12995 asection *sec;
12996 bfd_vma hstart, hend;
12997 Elf_Internal_Rela *relstart, *relend, *rel;
12998 const struct elf_backend_data *bed;
12999 unsigned int log_file_align;
13000
13001 /* Take care of both those symbols that do not describe vtables as
13002 well as those that are not loaded. */
13003 if (h->vtable == NULL || h->vtable->parent == NULL)
13004 return TRUE;
13005
13006 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13007 || h->root.type == bfd_link_hash_defweak);
13008
13009 sec = h->root.u.def.section;
13010 hstart = h->root.u.def.value;
13011 hend = hstart + h->size;
13012
13013 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13014 if (!relstart)
13015 return *(bfd_boolean *) okp = FALSE;
13016 bed = get_elf_backend_data (sec->owner);
13017 log_file_align = bed->s->log_file_align;
13018
13019 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
13020
13021 for (rel = relstart; rel < relend; ++rel)
13022 if (rel->r_offset >= hstart && rel->r_offset < hend)
13023 {
13024 /* If the entry is in use, do nothing. */
13025 if (h->vtable->used
13026 && (rel->r_offset - hstart) < h->vtable->size)
13027 {
13028 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13029 if (h->vtable->used[entry])
13030 continue;
13031 }
13032 /* Otherwise, kill it. */
13033 rel->r_offset = rel->r_info = rel->r_addend = 0;
13034 }
13035
13036 return TRUE;
13037 }
13038
13039 /* Mark sections containing dynamically referenced symbols. When
13040 building shared libraries, we must assume that any visible symbol is
13041 referenced. */
13042
13043 bfd_boolean
13044 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
13045 {
13046 struct bfd_link_info *info = (struct bfd_link_info *) inf;
13047 struct bfd_elf_dynamic_list *d = info->dynamic_list;
13048
13049 if ((h->root.type == bfd_link_hash_defined
13050 || h->root.type == bfd_link_hash_defweak)
13051 && (h->ref_dynamic
13052 || ((h->def_regular || ELF_COMMON_DEF_P (h))
13053 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
13054 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
13055 && (!bfd_link_executable (info)
13056 || info->gc_keep_exported
13057 || info->export_dynamic
13058 || (h->dynamic
13059 && d != NULL
13060 && (*d->match) (&d->head, NULL, h->root.root.string)))
13061 && (h->versioned >= versioned
13062 || !bfd_hide_sym_by_version (info->version_info,
13063 h->root.root.string)))))
13064 h->root.u.def.section->flags |= SEC_KEEP;
13065
13066 return TRUE;
13067 }
13068
13069 /* Keep all sections containing symbols undefined on the command-line,
13070 and the section containing the entry symbol. */
13071
13072 void
13073 _bfd_elf_gc_keep (struct bfd_link_info *info)
13074 {
13075 struct bfd_sym_chain *sym;
13076
13077 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13078 {
13079 struct elf_link_hash_entry *h;
13080
13081 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13082 FALSE, FALSE, FALSE);
13083
13084 if (h != NULL
13085 && (h->root.type == bfd_link_hash_defined
13086 || h->root.type == bfd_link_hash_defweak)
13087 && !bfd_is_abs_section (h->root.u.def.section)
13088 && !bfd_is_und_section (h->root.u.def.section))
13089 h->root.u.def.section->flags |= SEC_KEEP;
13090 }
13091 }
13092
13093 bfd_boolean
13094 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13095 struct bfd_link_info *info)
13096 {
13097 bfd *ibfd = info->input_bfds;
13098
13099 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13100 {
13101 asection *sec;
13102 struct elf_reloc_cookie cookie;
13103
13104 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13105 continue;
13106
13107 if (!init_reloc_cookie (&cookie, info, ibfd))
13108 return FALSE;
13109
13110 for (sec = ibfd->sections; sec; sec = sec->next)
13111 {
13112 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13113 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13114 {
13115 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13116 fini_reloc_cookie_rels (&cookie, sec);
13117 }
13118 }
13119 }
13120 return TRUE;
13121 }
13122
13123 /* Do mark and sweep of unused sections. */
13124
13125 bfd_boolean
13126 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13127 {
13128 bfd_boolean ok = TRUE;
13129 bfd *sub;
13130 elf_gc_mark_hook_fn gc_mark_hook;
13131 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13132 struct elf_link_hash_table *htab;
13133
13134 if (!bed->can_gc_sections
13135 || !is_elf_hash_table (info->hash))
13136 {
13137 _bfd_error_handler(_("Warning: gc-sections option ignored"));
13138 return TRUE;
13139 }
13140
13141 bed->gc_keep (info);
13142 htab = elf_hash_table (info);
13143
13144 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13145 at the .eh_frame section if we can mark the FDEs individually. */
13146 for (sub = info->input_bfds;
13147 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13148 sub = sub->link.next)
13149 {
13150 asection *sec;
13151 struct elf_reloc_cookie cookie;
13152
13153 sec = bfd_get_section_by_name (sub, ".eh_frame");
13154 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
13155 {
13156 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
13157 if (elf_section_data (sec)->sec_info
13158 && (sec->flags & SEC_LINKER_CREATED) == 0)
13159 elf_eh_frame_section (sub) = sec;
13160 fini_reloc_cookie_for_section (&cookie, sec);
13161 sec = bfd_get_next_section_by_name (NULL, sec);
13162 }
13163 }
13164
13165 /* Apply transitive closure to the vtable entry usage info. */
13166 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
13167 if (!ok)
13168 return FALSE;
13169
13170 /* Kill the vtable relocations that were not used. */
13171 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
13172 if (!ok)
13173 return FALSE;
13174
13175 /* Mark dynamically referenced symbols. */
13176 if (htab->dynamic_sections_created || info->gc_keep_exported)
13177 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
13178
13179 /* Grovel through relocs to find out who stays ... */
13180 gc_mark_hook = bed->gc_mark_hook;
13181 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13182 {
13183 asection *o;
13184
13185 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13186 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13187 continue;
13188
13189 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13190 Also treat note sections as a root, if the section is not part
13191 of a group. */
13192 for (o = sub->sections; o != NULL; o = o->next)
13193 if (!o->gc_mark
13194 && (o->flags & SEC_EXCLUDE) == 0
13195 && ((o->flags & SEC_KEEP) != 0
13196 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13197 && elf_next_in_group (o) == NULL )))
13198 {
13199 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13200 return FALSE;
13201 }
13202 }
13203
13204 /* Allow the backend to mark additional target specific sections. */
13205 bed->gc_mark_extra_sections (info, gc_mark_hook);
13206
13207 /* ... and mark SEC_EXCLUDE for those that go. */
13208 return elf_gc_sweep (abfd, info);
13209 }
13210 \f
13211 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13212
13213 bfd_boolean
13214 bfd_elf_gc_record_vtinherit (bfd *abfd,
13215 asection *sec,
13216 struct elf_link_hash_entry *h,
13217 bfd_vma offset)
13218 {
13219 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13220 struct elf_link_hash_entry **search, *child;
13221 size_t extsymcount;
13222 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13223
13224 /* The sh_info field of the symtab header tells us where the
13225 external symbols start. We don't care about the local symbols at
13226 this point. */
13227 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13228 if (!elf_bad_symtab (abfd))
13229 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13230
13231 sym_hashes = elf_sym_hashes (abfd);
13232 sym_hashes_end = sym_hashes + extsymcount;
13233
13234 /* Hunt down the child symbol, which is in this section at the same
13235 offset as the relocation. */
13236 for (search = sym_hashes; search != sym_hashes_end; ++search)
13237 {
13238 if ((child = *search) != NULL
13239 && (child->root.type == bfd_link_hash_defined
13240 || child->root.type == bfd_link_hash_defweak)
13241 && child->root.u.def.section == sec
13242 && child->root.u.def.value == offset)
13243 goto win;
13244 }
13245
13246 /* xgettext:c-format */
13247 _bfd_error_handler (_("%B: %A+%lu: No symbol found for INHERIT"),
13248 abfd, sec, (unsigned long) offset);
13249 bfd_set_error (bfd_error_invalid_operation);
13250 return FALSE;
13251
13252 win:
13253 if (!child->vtable)
13254 {
13255 child->vtable = ((struct elf_link_virtual_table_entry *)
13256 bfd_zalloc (abfd, sizeof (*child->vtable)));
13257 if (!child->vtable)
13258 return FALSE;
13259 }
13260 if (!h)
13261 {
13262 /* This *should* only be the absolute section. It could potentially
13263 be that someone has defined a non-global vtable though, which
13264 would be bad. It isn't worth paging in the local symbols to be
13265 sure though; that case should simply be handled by the assembler. */
13266
13267 child->vtable->parent = (struct elf_link_hash_entry *) -1;
13268 }
13269 else
13270 child->vtable->parent = h;
13271
13272 return TRUE;
13273 }
13274
13275 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13276
13277 bfd_boolean
13278 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13279 asection *sec ATTRIBUTE_UNUSED,
13280 struct elf_link_hash_entry *h,
13281 bfd_vma addend)
13282 {
13283 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13284 unsigned int log_file_align = bed->s->log_file_align;
13285
13286 if (!h->vtable)
13287 {
13288 h->vtable = ((struct elf_link_virtual_table_entry *)
13289 bfd_zalloc (abfd, sizeof (*h->vtable)));
13290 if (!h->vtable)
13291 return FALSE;
13292 }
13293
13294 if (addend >= h->vtable->size)
13295 {
13296 size_t size, bytes, file_align;
13297 bfd_boolean *ptr = h->vtable->used;
13298
13299 /* While the symbol is undefined, we have to be prepared to handle
13300 a zero size. */
13301 file_align = 1 << log_file_align;
13302 if (h->root.type == bfd_link_hash_undefined)
13303 size = addend + file_align;
13304 else
13305 {
13306 size = h->size;
13307 if (addend >= size)
13308 {
13309 /* Oops! We've got a reference past the defined end of
13310 the table. This is probably a bug -- shall we warn? */
13311 size = addend + file_align;
13312 }
13313 }
13314 size = (size + file_align - 1) & -file_align;
13315
13316 /* Allocate one extra entry for use as a "done" flag for the
13317 consolidation pass. */
13318 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13319
13320 if (ptr)
13321 {
13322 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
13323
13324 if (ptr != NULL)
13325 {
13326 size_t oldbytes;
13327
13328 oldbytes = (((h->vtable->size >> log_file_align) + 1)
13329 * sizeof (bfd_boolean));
13330 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13331 }
13332 }
13333 else
13334 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
13335
13336 if (ptr == NULL)
13337 return FALSE;
13338
13339 /* And arrange for that done flag to be at index -1. */
13340 h->vtable->used = ptr + 1;
13341 h->vtable->size = size;
13342 }
13343
13344 h->vtable->used[addend >> log_file_align] = TRUE;
13345
13346 return TRUE;
13347 }
13348
13349 /* Map an ELF section header flag to its corresponding string. */
13350 typedef struct
13351 {
13352 char *flag_name;
13353 flagword flag_value;
13354 } elf_flags_to_name_table;
13355
13356 static elf_flags_to_name_table elf_flags_to_names [] =
13357 {
13358 { "SHF_WRITE", SHF_WRITE },
13359 { "SHF_ALLOC", SHF_ALLOC },
13360 { "SHF_EXECINSTR", SHF_EXECINSTR },
13361 { "SHF_MERGE", SHF_MERGE },
13362 { "SHF_STRINGS", SHF_STRINGS },
13363 { "SHF_INFO_LINK", SHF_INFO_LINK},
13364 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13365 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13366 { "SHF_GROUP", SHF_GROUP },
13367 { "SHF_TLS", SHF_TLS },
13368 { "SHF_MASKOS", SHF_MASKOS },
13369 { "SHF_EXCLUDE", SHF_EXCLUDE },
13370 };
13371
13372 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13373 bfd_boolean
13374 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
13375 struct flag_info *flaginfo,
13376 asection *section)
13377 {
13378 const bfd_vma sh_flags = elf_section_flags (section);
13379
13380 if (!flaginfo->flags_initialized)
13381 {
13382 bfd *obfd = info->output_bfd;
13383 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13384 struct flag_info_list *tf = flaginfo->flag_list;
13385 int with_hex = 0;
13386 int without_hex = 0;
13387
13388 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13389 {
13390 unsigned i;
13391 flagword (*lookup) (char *);
13392
13393 lookup = bed->elf_backend_lookup_section_flags_hook;
13394 if (lookup != NULL)
13395 {
13396 flagword hexval = (*lookup) ((char *) tf->name);
13397
13398 if (hexval != 0)
13399 {
13400 if (tf->with == with_flags)
13401 with_hex |= hexval;
13402 else if (tf->with == without_flags)
13403 without_hex |= hexval;
13404 tf->valid = TRUE;
13405 continue;
13406 }
13407 }
13408 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13409 {
13410 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
13411 {
13412 if (tf->with == with_flags)
13413 with_hex |= elf_flags_to_names[i].flag_value;
13414 else if (tf->with == without_flags)
13415 without_hex |= elf_flags_to_names[i].flag_value;
13416 tf->valid = TRUE;
13417 break;
13418 }
13419 }
13420 if (!tf->valid)
13421 {
13422 info->callbacks->einfo
13423 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
13424 return FALSE;
13425 }
13426 }
13427 flaginfo->flags_initialized = TRUE;
13428 flaginfo->only_with_flags |= with_hex;
13429 flaginfo->not_with_flags |= without_hex;
13430 }
13431
13432 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13433 return FALSE;
13434
13435 if ((flaginfo->not_with_flags & sh_flags) != 0)
13436 return FALSE;
13437
13438 return TRUE;
13439 }
13440
13441 struct alloc_got_off_arg {
13442 bfd_vma gotoff;
13443 struct bfd_link_info *info;
13444 };
13445
13446 /* We need a special top-level link routine to convert got reference counts
13447 to real got offsets. */
13448
13449 static bfd_boolean
13450 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13451 {
13452 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13453 bfd *obfd = gofarg->info->output_bfd;
13454 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13455
13456 if (h->got.refcount > 0)
13457 {
13458 h->got.offset = gofarg->gotoff;
13459 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13460 }
13461 else
13462 h->got.offset = (bfd_vma) -1;
13463
13464 return TRUE;
13465 }
13466
13467 /* And an accompanying bit to work out final got entry offsets once
13468 we're done. Should be called from final_link. */
13469
13470 bfd_boolean
13471 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13472 struct bfd_link_info *info)
13473 {
13474 bfd *i;
13475 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13476 bfd_vma gotoff;
13477 struct alloc_got_off_arg gofarg;
13478
13479 BFD_ASSERT (abfd == info->output_bfd);
13480
13481 if (! is_elf_hash_table (info->hash))
13482 return FALSE;
13483
13484 /* The GOT offset is relative to the .got section, but the GOT header is
13485 put into the .got.plt section, if the backend uses it. */
13486 if (bed->want_got_plt)
13487 gotoff = 0;
13488 else
13489 gotoff = bed->got_header_size;
13490
13491 /* Do the local .got entries first. */
13492 for (i = info->input_bfds; i; i = i->link.next)
13493 {
13494 bfd_signed_vma *local_got;
13495 size_t j, locsymcount;
13496 Elf_Internal_Shdr *symtab_hdr;
13497
13498 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13499 continue;
13500
13501 local_got = elf_local_got_refcounts (i);
13502 if (!local_got)
13503 continue;
13504
13505 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13506 if (elf_bad_symtab (i))
13507 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13508 else
13509 locsymcount = symtab_hdr->sh_info;
13510
13511 for (j = 0; j < locsymcount; ++j)
13512 {
13513 if (local_got[j] > 0)
13514 {
13515 local_got[j] = gotoff;
13516 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
13517 }
13518 else
13519 local_got[j] = (bfd_vma) -1;
13520 }
13521 }
13522
13523 /* Then the global .got entries. .plt refcounts are handled by
13524 adjust_dynamic_symbol */
13525 gofarg.gotoff = gotoff;
13526 gofarg.info = info;
13527 elf_link_hash_traverse (elf_hash_table (info),
13528 elf_gc_allocate_got_offsets,
13529 &gofarg);
13530 return TRUE;
13531 }
13532
13533 /* Many folk need no more in the way of final link than this, once
13534 got entry reference counting is enabled. */
13535
13536 bfd_boolean
13537 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13538 {
13539 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13540 return FALSE;
13541
13542 /* Invoke the regular ELF backend linker to do all the work. */
13543 return bfd_elf_final_link (abfd, info);
13544 }
13545
13546 bfd_boolean
13547 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13548 {
13549 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
13550
13551 if (rcookie->bad_symtab)
13552 rcookie->rel = rcookie->rels;
13553
13554 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13555 {
13556 unsigned long r_symndx;
13557
13558 if (! rcookie->bad_symtab)
13559 if (rcookie->rel->r_offset > offset)
13560 return FALSE;
13561 if (rcookie->rel->r_offset != offset)
13562 continue;
13563
13564 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
13565 if (r_symndx == STN_UNDEF)
13566 return TRUE;
13567
13568 if (r_symndx >= rcookie->locsymcount
13569 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13570 {
13571 struct elf_link_hash_entry *h;
13572
13573 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13574
13575 while (h->root.type == bfd_link_hash_indirect
13576 || h->root.type == bfd_link_hash_warning)
13577 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13578
13579 if ((h->root.type == bfd_link_hash_defined
13580 || h->root.type == bfd_link_hash_defweak)
13581 && (h->root.u.def.section->owner != rcookie->abfd
13582 || h->root.u.def.section->kept_section != NULL
13583 || discarded_section (h->root.u.def.section)))
13584 return TRUE;
13585 }
13586 else
13587 {
13588 /* It's not a relocation against a global symbol,
13589 but it could be a relocation against a local
13590 symbol for a discarded section. */
13591 asection *isec;
13592 Elf_Internal_Sym *isym;
13593
13594 /* Need to: get the symbol; get the section. */
13595 isym = &rcookie->locsyms[r_symndx];
13596 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
13597 if (isec != NULL
13598 && (isec->kept_section != NULL
13599 || discarded_section (isec)))
13600 return TRUE;
13601 }
13602 return FALSE;
13603 }
13604 return FALSE;
13605 }
13606
13607 /* Discard unneeded references to discarded sections.
13608 Returns -1 on error, 1 if any section's size was changed, 0 if
13609 nothing changed. This function assumes that the relocations are in
13610 sorted order, which is true for all known assemblers. */
13611
13612 int
13613 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13614 {
13615 struct elf_reloc_cookie cookie;
13616 asection *o;
13617 bfd *abfd;
13618 int changed = 0;
13619
13620 if (info->traditional_format
13621 || !is_elf_hash_table (info->hash))
13622 return 0;
13623
13624 o = bfd_get_section_by_name (output_bfd, ".stab");
13625 if (o != NULL)
13626 {
13627 asection *i;
13628
13629 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13630 {
13631 if (i->size == 0
13632 || i->reloc_count == 0
13633 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13634 continue;
13635
13636 abfd = i->owner;
13637 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13638 continue;
13639
13640 if (!init_reloc_cookie_for_section (&cookie, info, i))
13641 return -1;
13642
13643 if (_bfd_discard_section_stabs (abfd, i,
13644 elf_section_data (i)->sec_info,
13645 bfd_elf_reloc_symbol_deleted_p,
13646 &cookie))
13647 changed = 1;
13648
13649 fini_reloc_cookie_for_section (&cookie, i);
13650 }
13651 }
13652
13653 o = NULL;
13654 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13655 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
13656 if (o != NULL)
13657 {
13658 asection *i;
13659
13660 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13661 {
13662 if (i->size == 0)
13663 continue;
13664
13665 abfd = i->owner;
13666 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13667 continue;
13668
13669 if (!init_reloc_cookie_for_section (&cookie, info, i))
13670 return -1;
13671
13672 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13673 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
13674 bfd_elf_reloc_symbol_deleted_p,
13675 &cookie))
13676 changed = 1;
13677
13678 fini_reloc_cookie_for_section (&cookie, i);
13679 }
13680 }
13681
13682 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13683 {
13684 const struct elf_backend_data *bed;
13685
13686 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13687 continue;
13688
13689 bed = get_elf_backend_data (abfd);
13690
13691 if (bed->elf_backend_discard_info != NULL)
13692 {
13693 if (!init_reloc_cookie (&cookie, info, abfd))
13694 return -1;
13695
13696 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
13697 changed = 1;
13698
13699 fini_reloc_cookie (&cookie, abfd);
13700 }
13701 }
13702
13703 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13704 _bfd_elf_end_eh_frame_parsing (info);
13705
13706 if (info->eh_frame_hdr_type
13707 && !bfd_link_relocatable (info)
13708 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
13709 changed = 1;
13710
13711 return changed;
13712 }
13713
13714 bfd_boolean
13715 _bfd_elf_section_already_linked (bfd *abfd,
13716 asection *sec,
13717 struct bfd_link_info *info)
13718 {
13719 flagword flags;
13720 const char *name, *key;
13721 struct bfd_section_already_linked *l;
13722 struct bfd_section_already_linked_hash_entry *already_linked_list;
13723
13724 if (sec->output_section == bfd_abs_section_ptr)
13725 return FALSE;
13726
13727 flags = sec->flags;
13728
13729 /* Return if it isn't a linkonce section. A comdat group section
13730 also has SEC_LINK_ONCE set. */
13731 if ((flags & SEC_LINK_ONCE) == 0)
13732 return FALSE;
13733
13734 /* Don't put group member sections on our list of already linked
13735 sections. They are handled as a group via their group section. */
13736 if (elf_sec_group (sec) != NULL)
13737 return FALSE;
13738
13739 /* For a SHT_GROUP section, use the group signature as the key. */
13740 name = sec->name;
13741 if ((flags & SEC_GROUP) != 0
13742 && elf_next_in_group (sec) != NULL
13743 && elf_group_name (elf_next_in_group (sec)) != NULL)
13744 key = elf_group_name (elf_next_in_group (sec));
13745 else
13746 {
13747 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
13748 if (CONST_STRNEQ (name, ".gnu.linkonce.")
13749 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13750 key++;
13751 else
13752 /* Must be a user linkonce section that doesn't follow gcc's
13753 naming convention. In this case we won't be matching
13754 single member groups. */
13755 key = name;
13756 }
13757
13758 already_linked_list = bfd_section_already_linked_table_lookup (key);
13759
13760 for (l = already_linked_list->entry; l != NULL; l = l->next)
13761 {
13762 /* We may have 2 different types of sections on the list: group
13763 sections with a signature of <key> (<key> is some string),
13764 and linkonce sections named .gnu.linkonce.<type>.<key>.
13765 Match like sections. LTO plugin sections are an exception.
13766 They are always named .gnu.linkonce.t.<key> and match either
13767 type of section. */
13768 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13769 && ((flags & SEC_GROUP) != 0
13770 || strcmp (name, l->sec->name) == 0))
13771 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
13772 {
13773 /* The section has already been linked. See if we should
13774 issue a warning. */
13775 if (!_bfd_handle_already_linked (sec, l, info))
13776 return FALSE;
13777
13778 if (flags & SEC_GROUP)
13779 {
13780 asection *first = elf_next_in_group (sec);
13781 asection *s = first;
13782
13783 while (s != NULL)
13784 {
13785 s->output_section = bfd_abs_section_ptr;
13786 /* Record which group discards it. */
13787 s->kept_section = l->sec;
13788 s = elf_next_in_group (s);
13789 /* These lists are circular. */
13790 if (s == first)
13791 break;
13792 }
13793 }
13794
13795 return TRUE;
13796 }
13797 }
13798
13799 /* A single member comdat group section may be discarded by a
13800 linkonce section and vice versa. */
13801 if ((flags & SEC_GROUP) != 0)
13802 {
13803 asection *first = elf_next_in_group (sec);
13804
13805 if (first != NULL && elf_next_in_group (first) == first)
13806 /* Check this single member group against linkonce sections. */
13807 for (l = already_linked_list->entry; l != NULL; l = l->next)
13808 if ((l->sec->flags & SEC_GROUP) == 0
13809 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13810 {
13811 first->output_section = bfd_abs_section_ptr;
13812 first->kept_section = l->sec;
13813 sec->output_section = bfd_abs_section_ptr;
13814 break;
13815 }
13816 }
13817 else
13818 /* Check this linkonce section against single member groups. */
13819 for (l = already_linked_list->entry; l != NULL; l = l->next)
13820 if (l->sec->flags & SEC_GROUP)
13821 {
13822 asection *first = elf_next_in_group (l->sec);
13823
13824 if (first != NULL
13825 && elf_next_in_group (first) == first
13826 && bfd_elf_match_symbols_in_sections (first, sec, info))
13827 {
13828 sec->output_section = bfd_abs_section_ptr;
13829 sec->kept_section = first;
13830 break;
13831 }
13832 }
13833
13834 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13835 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13836 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13837 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13838 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13839 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13840 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13841 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13842 The reverse order cannot happen as there is never a bfd with only the
13843 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13844 matter as here were are looking only for cross-bfd sections. */
13845
13846 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
13847 for (l = already_linked_list->entry; l != NULL; l = l->next)
13848 if ((l->sec->flags & SEC_GROUP) == 0
13849 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
13850 {
13851 if (abfd != l->sec->owner)
13852 sec->output_section = bfd_abs_section_ptr;
13853 break;
13854 }
13855
13856 /* This is the first section with this name. Record it. */
13857 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
13858 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
13859 return sec->output_section == bfd_abs_section_ptr;
13860 }
13861
13862 bfd_boolean
13863 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
13864 {
13865 return sym->st_shndx == SHN_COMMON;
13866 }
13867
13868 unsigned int
13869 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
13870 {
13871 return SHN_COMMON;
13872 }
13873
13874 asection *
13875 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
13876 {
13877 return bfd_com_section_ptr;
13878 }
13879
13880 bfd_vma
13881 _bfd_elf_default_got_elt_size (bfd *abfd,
13882 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13883 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
13884 bfd *ibfd ATTRIBUTE_UNUSED,
13885 unsigned long symndx ATTRIBUTE_UNUSED)
13886 {
13887 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13888 return bed->s->arch_size / 8;
13889 }
13890
13891 /* Routines to support the creation of dynamic relocs. */
13892
13893 /* Returns the name of the dynamic reloc section associated with SEC. */
13894
13895 static const char *
13896 get_dynamic_reloc_section_name (bfd * abfd,
13897 asection * sec,
13898 bfd_boolean is_rela)
13899 {
13900 char *name;
13901 const char *old_name = bfd_get_section_name (NULL, sec);
13902 const char *prefix = is_rela ? ".rela" : ".rel";
13903
13904 if (old_name == NULL)
13905 return NULL;
13906
13907 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
13908 sprintf (name, "%s%s", prefix, old_name);
13909
13910 return name;
13911 }
13912
13913 /* Returns the dynamic reloc section associated with SEC.
13914 If necessary compute the name of the dynamic reloc section based
13915 on SEC's name (looked up in ABFD's string table) and the setting
13916 of IS_RELA. */
13917
13918 asection *
13919 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
13920 asection * sec,
13921 bfd_boolean is_rela)
13922 {
13923 asection * reloc_sec = elf_section_data (sec)->sreloc;
13924
13925 if (reloc_sec == NULL)
13926 {
13927 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13928
13929 if (name != NULL)
13930 {
13931 reloc_sec = bfd_get_linker_section (abfd, name);
13932
13933 if (reloc_sec != NULL)
13934 elf_section_data (sec)->sreloc = reloc_sec;
13935 }
13936 }
13937
13938 return reloc_sec;
13939 }
13940
13941 /* Returns the dynamic reloc section associated with SEC. If the
13942 section does not exist it is created and attached to the DYNOBJ
13943 bfd and stored in the SRELOC field of SEC's elf_section_data
13944 structure.
13945
13946 ALIGNMENT is the alignment for the newly created section and
13947 IS_RELA defines whether the name should be .rela.<SEC's name>
13948 or .rel.<SEC's name>. The section name is looked up in the
13949 string table associated with ABFD. */
13950
13951 asection *
13952 _bfd_elf_make_dynamic_reloc_section (asection *sec,
13953 bfd *dynobj,
13954 unsigned int alignment,
13955 bfd *abfd,
13956 bfd_boolean is_rela)
13957 {
13958 asection * reloc_sec = elf_section_data (sec)->sreloc;
13959
13960 if (reloc_sec == NULL)
13961 {
13962 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13963
13964 if (name == NULL)
13965 return NULL;
13966
13967 reloc_sec = bfd_get_linker_section (dynobj, name);
13968
13969 if (reloc_sec == NULL)
13970 {
13971 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
13972 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
13973 if ((sec->flags & SEC_ALLOC) != 0)
13974 flags |= SEC_ALLOC | SEC_LOAD;
13975
13976 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
13977 if (reloc_sec != NULL)
13978 {
13979 /* _bfd_elf_get_sec_type_attr chooses a section type by
13980 name. Override as it may be wrong, eg. for a user
13981 section named "auto" we'll get ".relauto" which is
13982 seen to be a .rela section. */
13983 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
13984 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
13985 reloc_sec = NULL;
13986 }
13987 }
13988
13989 elf_section_data (sec)->sreloc = reloc_sec;
13990 }
13991
13992 return reloc_sec;
13993 }
13994
13995 /* Copy the ELF symbol type and other attributes for a linker script
13996 assignment from HSRC to HDEST. Generally this should be treated as
13997 if we found a strong non-dynamic definition for HDEST (except that
13998 ld ignores multiple definition errors). */
13999 void
14000 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14001 struct bfd_link_hash_entry *hdest,
14002 struct bfd_link_hash_entry *hsrc)
14003 {
14004 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14005 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14006 Elf_Internal_Sym isym;
14007
14008 ehdest->type = ehsrc->type;
14009 ehdest->target_internal = ehsrc->target_internal;
14010
14011 isym.st_other = ehsrc->other;
14012 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
14013 }
14014
14015 /* Append a RELA relocation REL to section S in BFD. */
14016
14017 void
14018 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14019 {
14020 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14021 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14022 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14023 bed->s->swap_reloca_out (abfd, rel, loc);
14024 }
14025
14026 /* Append a REL relocation REL to section S in BFD. */
14027
14028 void
14029 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14030 {
14031 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14032 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14033 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
14034 bed->s->swap_reloc_out (abfd, rel, loc);
14035 }
This page took 0.53217 seconds and 4 git commands to generate.