[GOLD] PowerPC --stub-group-multi
[deliverable/binutils-gdb.git] / bfd / elflink.c
1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2016 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
20
21 #include "sysdep.h"
22 #include "bfd.h"
23 #include "bfd_stdint.h"
24 #include "bfdlink.h"
25 #include "libbfd.h"
26 #define ARCH_SIZE 0
27 #include "elf-bfd.h"
28 #include "safe-ctype.h"
29 #include "libiberty.h"
30 #include "objalloc.h"
31 #if BFD_SUPPORTS_PLUGINS
32 #include "plugin-api.h"
33 #include "plugin.h"
34 #endif
35
36 /* This struct is used to pass information to routines called via
37 elf_link_hash_traverse which must return failure. */
38
39 struct elf_info_failed
40 {
41 struct bfd_link_info *info;
42 bfd_boolean failed;
43 };
44
45 /* This structure is used to pass information to
46 _bfd_elf_link_find_version_dependencies. */
47
48 struct elf_find_verdep_info
49 {
50 /* General link information. */
51 struct bfd_link_info *info;
52 /* The number of dependencies. */
53 unsigned int vers;
54 /* Whether we had a failure. */
55 bfd_boolean failed;
56 };
57
58 static bfd_boolean _bfd_elf_fix_symbol_flags
59 (struct elf_link_hash_entry *, struct elf_info_failed *);
60
61 asection *
62 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
63 unsigned long r_symndx,
64 bfd_boolean discard)
65 {
66 if (r_symndx >= cookie->locsymcount
67 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
68 {
69 struct elf_link_hash_entry *h;
70
71 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
72
73 while (h->root.type == bfd_link_hash_indirect
74 || h->root.type == bfd_link_hash_warning)
75 h = (struct elf_link_hash_entry *) h->root.u.i.link;
76
77 if ((h->root.type == bfd_link_hash_defined
78 || h->root.type == bfd_link_hash_defweak)
79 && discarded_section (h->root.u.def.section))
80 return h->root.u.def.section;
81 else
82 return NULL;
83 }
84 else
85 {
86 /* It's not a relocation against a global symbol,
87 but it could be a relocation against a local
88 symbol for a discarded section. */
89 asection *isec;
90 Elf_Internal_Sym *isym;
91
92 /* Need to: get the symbol; get the section. */
93 isym = &cookie->locsyms[r_symndx];
94 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
95 if (isec != NULL
96 && discard ? discarded_section (isec) : 1)
97 return isec;
98 }
99 return NULL;
100 }
101
102 /* Define a symbol in a dynamic linkage section. */
103
104 struct elf_link_hash_entry *
105 _bfd_elf_define_linkage_sym (bfd *abfd,
106 struct bfd_link_info *info,
107 asection *sec,
108 const char *name)
109 {
110 struct elf_link_hash_entry *h;
111 struct bfd_link_hash_entry *bh;
112 const struct elf_backend_data *bed;
113
114 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
115 if (h != NULL)
116 {
117 /* Zap symbol defined in an as-needed lib that wasn't linked.
118 This is a symptom of a larger problem: Absolute symbols
119 defined in shared libraries can't be overridden, because we
120 lose the link to the bfd which is via the symbol section. */
121 h->root.type = bfd_link_hash_new;
122 }
123
124 bh = &h->root;
125 bed = get_elf_backend_data (abfd);
126 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
127 sec, 0, NULL, FALSE, bed->collect,
128 &bh))
129 return NULL;
130 h = (struct elf_link_hash_entry *) bh;
131 h->def_regular = 1;
132 h->non_elf = 0;
133 h->root.linker_def = 1;
134 h->type = STT_OBJECT;
135 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
136 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
137
138 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
139 return h;
140 }
141
142 bfd_boolean
143 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
144 {
145 flagword flags;
146 asection *s;
147 struct elf_link_hash_entry *h;
148 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
149 struct elf_link_hash_table *htab = elf_hash_table (info);
150
151 /* This function may be called more than once. */
152 if (htab->sgot != NULL)
153 return TRUE;
154
155 flags = bed->dynamic_sec_flags;
156
157 s = bfd_make_section_anyway_with_flags (abfd,
158 (bed->rela_plts_and_copies_p
159 ? ".rela.got" : ".rel.got"),
160 (bed->dynamic_sec_flags
161 | SEC_READONLY));
162 if (s == NULL
163 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
164 return FALSE;
165 htab->srelgot = s;
166
167 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
168 if (s == NULL
169 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
170 return FALSE;
171 htab->sgot = s;
172
173 if (bed->want_got_plt)
174 {
175 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
176 if (s == NULL
177 || !bfd_set_section_alignment (abfd, s,
178 bed->s->log_file_align))
179 return FALSE;
180 htab->sgotplt = s;
181 }
182
183 /* The first bit of the global offset table is the header. */
184 s->size += bed->got_header_size;
185
186 if (bed->want_got_sym)
187 {
188 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
189 (or .got.plt) section. We don't do this in the linker script
190 because we don't want to define the symbol if we are not creating
191 a global offset table. */
192 h = _bfd_elf_define_linkage_sym (abfd, info, s,
193 "_GLOBAL_OFFSET_TABLE_");
194 elf_hash_table (info)->hgot = h;
195 if (h == NULL)
196 return FALSE;
197 }
198
199 return TRUE;
200 }
201 \f
202 /* Create a strtab to hold the dynamic symbol names. */
203 static bfd_boolean
204 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
205 {
206 struct elf_link_hash_table *hash_table;
207
208 hash_table = elf_hash_table (info);
209 if (hash_table->dynobj == NULL)
210 {
211 /* We may not set dynobj, an input file holding linker created
212 dynamic sections to abfd, which may be a dynamic object with
213 its own dynamic sections. We need to find a normal input file
214 to hold linker created sections if possible. */
215 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
216 {
217 bfd *ibfd;
218 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
219 if ((ibfd->flags
220 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0)
221 {
222 abfd = ibfd;
223 break;
224 }
225 }
226 hash_table->dynobj = abfd;
227 }
228
229 if (hash_table->dynstr == NULL)
230 {
231 hash_table->dynstr = _bfd_elf_strtab_init ();
232 if (hash_table->dynstr == NULL)
233 return FALSE;
234 }
235 return TRUE;
236 }
237
238 /* Create some sections which will be filled in with dynamic linking
239 information. ABFD is an input file which requires dynamic sections
240 to be created. The dynamic sections take up virtual memory space
241 when the final executable is run, so we need to create them before
242 addresses are assigned to the output sections. We work out the
243 actual contents and size of these sections later. */
244
245 bfd_boolean
246 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
247 {
248 flagword flags;
249 asection *s;
250 const struct elf_backend_data *bed;
251 struct elf_link_hash_entry *h;
252
253 if (! is_elf_hash_table (info->hash))
254 return FALSE;
255
256 if (elf_hash_table (info)->dynamic_sections_created)
257 return TRUE;
258
259 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
260 return FALSE;
261
262 abfd = elf_hash_table (info)->dynobj;
263 bed = get_elf_backend_data (abfd);
264
265 flags = bed->dynamic_sec_flags;
266
267 /* A dynamically linked executable has a .interp section, but a
268 shared library does not. */
269 if (bfd_link_executable (info) && !info->nointerp)
270 {
271 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
272 flags | SEC_READONLY);
273 if (s == NULL)
274 return FALSE;
275 }
276
277 /* Create sections to hold version informations. These are removed
278 if they are not needed. */
279 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
280 flags | SEC_READONLY);
281 if (s == NULL
282 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
283 return FALSE;
284
285 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
286 flags | SEC_READONLY);
287 if (s == NULL
288 || ! bfd_set_section_alignment (abfd, s, 1))
289 return FALSE;
290
291 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
292 flags | SEC_READONLY);
293 if (s == NULL
294 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
295 return FALSE;
296
297 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
298 flags | SEC_READONLY);
299 if (s == NULL
300 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
301 return FALSE;
302 elf_hash_table (info)->dynsym = s;
303
304 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
305 flags | SEC_READONLY);
306 if (s == NULL)
307 return FALSE;
308
309 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
310 if (s == NULL
311 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
312 return FALSE;
313
314 /* The special symbol _DYNAMIC is always set to the start of the
315 .dynamic section. We could set _DYNAMIC in a linker script, but we
316 only want to define it if we are, in fact, creating a .dynamic
317 section. We don't want to define it if there is no .dynamic
318 section, since on some ELF platforms the start up code examines it
319 to decide how to initialize the process. */
320 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
321 elf_hash_table (info)->hdynamic = h;
322 if (h == NULL)
323 return FALSE;
324
325 if (info->emit_hash)
326 {
327 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
328 flags | SEC_READONLY);
329 if (s == NULL
330 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
331 return FALSE;
332 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
333 }
334
335 if (info->emit_gnu_hash)
336 {
337 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
338 flags | SEC_READONLY);
339 if (s == NULL
340 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
341 return FALSE;
342 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
343 4 32-bit words followed by variable count of 64-bit words, then
344 variable count of 32-bit words. */
345 if (bed->s->arch_size == 64)
346 elf_section_data (s)->this_hdr.sh_entsize = 0;
347 else
348 elf_section_data (s)->this_hdr.sh_entsize = 4;
349 }
350
351 /* Let the backend create the rest of the sections. This lets the
352 backend set the right flags. The backend will normally create
353 the .got and .plt sections. */
354 if (bed->elf_backend_create_dynamic_sections == NULL
355 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
356 return FALSE;
357
358 elf_hash_table (info)->dynamic_sections_created = TRUE;
359
360 return TRUE;
361 }
362
363 /* Create dynamic sections when linking against a dynamic object. */
364
365 bfd_boolean
366 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
367 {
368 flagword flags, pltflags;
369 struct elf_link_hash_entry *h;
370 asection *s;
371 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
372 struct elf_link_hash_table *htab = elf_hash_table (info);
373
374 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
375 .rel[a].bss sections. */
376 flags = bed->dynamic_sec_flags;
377
378 pltflags = flags;
379 if (bed->plt_not_loaded)
380 /* We do not clear SEC_ALLOC here because we still want the OS to
381 allocate space for the section; it's just that there's nothing
382 to read in from the object file. */
383 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
384 else
385 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
386 if (bed->plt_readonly)
387 pltflags |= SEC_READONLY;
388
389 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
390 if (s == NULL
391 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
392 return FALSE;
393 htab->splt = s;
394
395 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
396 .plt section. */
397 if (bed->want_plt_sym)
398 {
399 h = _bfd_elf_define_linkage_sym (abfd, info, s,
400 "_PROCEDURE_LINKAGE_TABLE_");
401 elf_hash_table (info)->hplt = h;
402 if (h == NULL)
403 return FALSE;
404 }
405
406 s = bfd_make_section_anyway_with_flags (abfd,
407 (bed->rela_plts_and_copies_p
408 ? ".rela.plt" : ".rel.plt"),
409 flags | SEC_READONLY);
410 if (s == NULL
411 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
412 return FALSE;
413 htab->srelplt = s;
414
415 if (! _bfd_elf_create_got_section (abfd, info))
416 return FALSE;
417
418 if (bed->want_dynbss)
419 {
420 /* The .dynbss section is a place to put symbols which are defined
421 by dynamic objects, are referenced by regular objects, and are
422 not functions. We must allocate space for them in the process
423 image and use a R_*_COPY reloc to tell the dynamic linker to
424 initialize them at run time. The linker script puts the .dynbss
425 section into the .bss section of the final image. */
426 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
427 (SEC_ALLOC | SEC_LINKER_CREATED));
428 if (s == NULL)
429 return FALSE;
430
431 /* The .rel[a].bss section holds copy relocs. This section is not
432 normally needed. We need to create it here, though, so that the
433 linker will map it to an output section. We can't just create it
434 only if we need it, because we will not know whether we need it
435 until we have seen all the input files, and the first time the
436 main linker code calls BFD after examining all the input files
437 (size_dynamic_sections) the input sections have already been
438 mapped to the output sections. If the section turns out not to
439 be needed, we can discard it later. We will never need this
440 section when generating a shared object, since they do not use
441 copy relocs. */
442 if (! bfd_link_pic (info))
443 {
444 s = bfd_make_section_anyway_with_flags (abfd,
445 (bed->rela_plts_and_copies_p
446 ? ".rela.bss" : ".rel.bss"),
447 flags | SEC_READONLY);
448 if (s == NULL
449 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
450 return FALSE;
451 }
452 }
453
454 return TRUE;
455 }
456 \f
457 /* Record a new dynamic symbol. We record the dynamic symbols as we
458 read the input files, since we need to have a list of all of them
459 before we can determine the final sizes of the output sections.
460 Note that we may actually call this function even though we are not
461 going to output any dynamic symbols; in some cases we know that a
462 symbol should be in the dynamic symbol table, but only if there is
463 one. */
464
465 bfd_boolean
466 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
467 struct elf_link_hash_entry *h)
468 {
469 if (h->dynindx == -1)
470 {
471 struct elf_strtab_hash *dynstr;
472 char *p;
473 const char *name;
474 size_t indx;
475
476 /* XXX: The ABI draft says the linker must turn hidden and
477 internal symbols into STB_LOCAL symbols when producing the
478 DSO. However, if ld.so honors st_other in the dynamic table,
479 this would not be necessary. */
480 switch (ELF_ST_VISIBILITY (h->other))
481 {
482 case STV_INTERNAL:
483 case STV_HIDDEN:
484 if (h->root.type != bfd_link_hash_undefined
485 && h->root.type != bfd_link_hash_undefweak)
486 {
487 h->forced_local = 1;
488 if (!elf_hash_table (info)->is_relocatable_executable)
489 return TRUE;
490 }
491
492 default:
493 break;
494 }
495
496 h->dynindx = elf_hash_table (info)->dynsymcount;
497 ++elf_hash_table (info)->dynsymcount;
498
499 dynstr = elf_hash_table (info)->dynstr;
500 if (dynstr == NULL)
501 {
502 /* Create a strtab to hold the dynamic symbol names. */
503 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
504 if (dynstr == NULL)
505 return FALSE;
506 }
507
508 /* We don't put any version information in the dynamic string
509 table. */
510 name = h->root.root.string;
511 p = strchr (name, ELF_VER_CHR);
512 if (p != NULL)
513 /* We know that the p points into writable memory. In fact,
514 there are only a few symbols that have read-only names, being
515 those like _GLOBAL_OFFSET_TABLE_ that are created specially
516 by the backends. Most symbols will have names pointing into
517 an ELF string table read from a file, or to objalloc memory. */
518 *p = 0;
519
520 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
521
522 if (p != NULL)
523 *p = ELF_VER_CHR;
524
525 if (indx == (size_t) -1)
526 return FALSE;
527 h->dynstr_index = indx;
528 }
529
530 return TRUE;
531 }
532 \f
533 /* Mark a symbol dynamic. */
534
535 static void
536 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
537 struct elf_link_hash_entry *h,
538 Elf_Internal_Sym *sym)
539 {
540 struct bfd_elf_dynamic_list *d = info->dynamic_list;
541
542 /* It may be called more than once on the same H. */
543 if(h->dynamic || bfd_link_relocatable (info))
544 return;
545
546 if ((info->dynamic_data
547 && (h->type == STT_OBJECT
548 || h->type == STT_COMMON
549 || (sym != NULL
550 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
551 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
552 || (d != NULL
553 && h->root.type == bfd_link_hash_new
554 && (*d->match) (&d->head, NULL, h->root.root.string)))
555 h->dynamic = 1;
556 }
557
558 /* Record an assignment to a symbol made by a linker script. We need
559 this in case some dynamic object refers to this symbol. */
560
561 bfd_boolean
562 bfd_elf_record_link_assignment (bfd *output_bfd,
563 struct bfd_link_info *info,
564 const char *name,
565 bfd_boolean provide,
566 bfd_boolean hidden)
567 {
568 struct elf_link_hash_entry *h, *hv;
569 struct elf_link_hash_table *htab;
570 const struct elf_backend_data *bed;
571
572 if (!is_elf_hash_table (info->hash))
573 return TRUE;
574
575 htab = elf_hash_table (info);
576 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
577 if (h == NULL)
578 return provide;
579
580 if (h->versioned == unknown)
581 {
582 /* Set versioned if symbol version is unknown. */
583 char *version = strrchr (name, ELF_VER_CHR);
584 if (version)
585 {
586 if (version > name && version[-1] != ELF_VER_CHR)
587 h->versioned = versioned_hidden;
588 else
589 h->versioned = versioned;
590 }
591 }
592
593 switch (h->root.type)
594 {
595 case bfd_link_hash_defined:
596 case bfd_link_hash_defweak:
597 case bfd_link_hash_common:
598 break;
599 case bfd_link_hash_undefweak:
600 case bfd_link_hash_undefined:
601 /* Since we're defining the symbol, don't let it seem to have not
602 been defined. record_dynamic_symbol and size_dynamic_sections
603 may depend on this. */
604 h->root.type = bfd_link_hash_new;
605 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
606 bfd_link_repair_undef_list (&htab->root);
607 break;
608 case bfd_link_hash_new:
609 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
610 h->non_elf = 0;
611 break;
612 case bfd_link_hash_indirect:
613 /* We had a versioned symbol in a dynamic library. We make the
614 the versioned symbol point to this one. */
615 bed = get_elf_backend_data (output_bfd);
616 hv = h;
617 while (hv->root.type == bfd_link_hash_indirect
618 || hv->root.type == bfd_link_hash_warning)
619 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
620 /* We don't need to update h->root.u since linker will set them
621 later. */
622 h->root.type = bfd_link_hash_undefined;
623 hv->root.type = bfd_link_hash_indirect;
624 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
625 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
626 break;
627 case bfd_link_hash_warning:
628 abort ();
629 break;
630 }
631
632 /* If this symbol is being provided by the linker script, and it is
633 currently defined by a dynamic object, but not by a regular
634 object, then mark it as undefined so that the generic linker will
635 force the correct value. */
636 if (provide
637 && h->def_dynamic
638 && !h->def_regular)
639 h->root.type = bfd_link_hash_undefined;
640
641 /* If this symbol is not being provided by the linker script, and it is
642 currently defined by a dynamic object, but not by a regular object,
643 then clear out any version information because the symbol will not be
644 associated with the dynamic object any more. */
645 if (!provide
646 && h->def_dynamic
647 && !h->def_regular)
648 h->verinfo.verdef = NULL;
649
650 h->def_regular = 1;
651
652 if (hidden)
653 {
654 bed = get_elf_backend_data (output_bfd);
655 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
656 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
657 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
658 }
659
660 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
661 and executables. */
662 if (!bfd_link_relocatable (info)
663 && h->dynindx != -1
664 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
665 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
666 h->forced_local = 1;
667
668 if ((h->def_dynamic
669 || h->ref_dynamic
670 || bfd_link_dll (info)
671 || elf_hash_table (info)->is_relocatable_executable)
672 && h->dynindx == -1)
673 {
674 if (! bfd_elf_link_record_dynamic_symbol (info, h))
675 return FALSE;
676
677 /* If this is a weak defined symbol, and we know a corresponding
678 real symbol from the same dynamic object, make sure the real
679 symbol is also made into a dynamic symbol. */
680 if (h->u.weakdef != NULL
681 && h->u.weakdef->dynindx == -1)
682 {
683 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
684 return FALSE;
685 }
686 }
687
688 return TRUE;
689 }
690
691 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
692 success, and 2 on a failure caused by attempting to record a symbol
693 in a discarded section, eg. a discarded link-once section symbol. */
694
695 int
696 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
697 bfd *input_bfd,
698 long input_indx)
699 {
700 bfd_size_type amt;
701 struct elf_link_local_dynamic_entry *entry;
702 struct elf_link_hash_table *eht;
703 struct elf_strtab_hash *dynstr;
704 size_t dynstr_index;
705 char *name;
706 Elf_External_Sym_Shndx eshndx;
707 char esym[sizeof (Elf64_External_Sym)];
708
709 if (! is_elf_hash_table (info->hash))
710 return 0;
711
712 /* See if the entry exists already. */
713 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
714 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
715 return 1;
716
717 amt = sizeof (*entry);
718 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
719 if (entry == NULL)
720 return 0;
721
722 /* Go find the symbol, so that we can find it's name. */
723 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
724 1, input_indx, &entry->isym, esym, &eshndx))
725 {
726 bfd_release (input_bfd, entry);
727 return 0;
728 }
729
730 if (entry->isym.st_shndx != SHN_UNDEF
731 && entry->isym.st_shndx < SHN_LORESERVE)
732 {
733 asection *s;
734
735 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
736 if (s == NULL || bfd_is_abs_section (s->output_section))
737 {
738 /* We can still bfd_release here as nothing has done another
739 bfd_alloc. We can't do this later in this function. */
740 bfd_release (input_bfd, entry);
741 return 2;
742 }
743 }
744
745 name = (bfd_elf_string_from_elf_section
746 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
747 entry->isym.st_name));
748
749 dynstr = elf_hash_table (info)->dynstr;
750 if (dynstr == NULL)
751 {
752 /* Create a strtab to hold the dynamic symbol names. */
753 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
754 if (dynstr == NULL)
755 return 0;
756 }
757
758 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
759 if (dynstr_index == (size_t) -1)
760 return 0;
761 entry->isym.st_name = dynstr_index;
762
763 eht = elf_hash_table (info);
764
765 entry->next = eht->dynlocal;
766 eht->dynlocal = entry;
767 entry->input_bfd = input_bfd;
768 entry->input_indx = input_indx;
769 eht->dynsymcount++;
770
771 /* Whatever binding the symbol had before, it's now local. */
772 entry->isym.st_info
773 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
774
775 /* The dynindx will be set at the end of size_dynamic_sections. */
776
777 return 1;
778 }
779
780 /* Return the dynindex of a local dynamic symbol. */
781
782 long
783 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
784 bfd *input_bfd,
785 long input_indx)
786 {
787 struct elf_link_local_dynamic_entry *e;
788
789 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
790 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
791 return e->dynindx;
792 return -1;
793 }
794
795 /* This function is used to renumber the dynamic symbols, if some of
796 them are removed because they are marked as local. This is called
797 via elf_link_hash_traverse. */
798
799 static bfd_boolean
800 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
801 void *data)
802 {
803 size_t *count = (size_t *) data;
804
805 if (h->forced_local)
806 return TRUE;
807
808 if (h->dynindx != -1)
809 h->dynindx = ++(*count);
810
811 return TRUE;
812 }
813
814
815 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
816 STB_LOCAL binding. */
817
818 static bfd_boolean
819 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
820 void *data)
821 {
822 size_t *count = (size_t *) data;
823
824 if (!h->forced_local)
825 return TRUE;
826
827 if (h->dynindx != -1)
828 h->dynindx = ++(*count);
829
830 return TRUE;
831 }
832
833 /* Return true if the dynamic symbol for a given section should be
834 omitted when creating a shared library. */
835 bfd_boolean
836 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
837 struct bfd_link_info *info,
838 asection *p)
839 {
840 struct elf_link_hash_table *htab;
841 asection *ip;
842
843 switch (elf_section_data (p)->this_hdr.sh_type)
844 {
845 case SHT_PROGBITS:
846 case SHT_NOBITS:
847 /* If sh_type is yet undecided, assume it could be
848 SHT_PROGBITS/SHT_NOBITS. */
849 case SHT_NULL:
850 htab = elf_hash_table (info);
851 if (p == htab->tls_sec)
852 return FALSE;
853
854 if (htab->text_index_section != NULL)
855 return p != htab->text_index_section && p != htab->data_index_section;
856
857 return (htab->dynobj != NULL
858 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
859 && ip->output_section == p);
860
861 /* There shouldn't be section relative relocations
862 against any other section. */
863 default:
864 return TRUE;
865 }
866 }
867
868 /* Assign dynsym indices. In a shared library we generate a section
869 symbol for each output section, which come first. Next come symbols
870 which have been forced to local binding. Then all of the back-end
871 allocated local dynamic syms, followed by the rest of the global
872 symbols. */
873
874 static unsigned long
875 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
876 struct bfd_link_info *info,
877 unsigned long *section_sym_count)
878 {
879 unsigned long dynsymcount = 0;
880
881 if (bfd_link_pic (info)
882 || elf_hash_table (info)->is_relocatable_executable)
883 {
884 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
885 asection *p;
886 for (p = output_bfd->sections; p ; p = p->next)
887 if ((p->flags & SEC_EXCLUDE) == 0
888 && (p->flags & SEC_ALLOC) != 0
889 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
890 elf_section_data (p)->dynindx = ++dynsymcount;
891 else
892 elf_section_data (p)->dynindx = 0;
893 }
894 *section_sym_count = dynsymcount;
895
896 elf_link_hash_traverse (elf_hash_table (info),
897 elf_link_renumber_local_hash_table_dynsyms,
898 &dynsymcount);
899
900 if (elf_hash_table (info)->dynlocal)
901 {
902 struct elf_link_local_dynamic_entry *p;
903 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
904 p->dynindx = ++dynsymcount;
905 }
906 elf_hash_table (info)->local_dynsymcount = dynsymcount;
907
908 elf_link_hash_traverse (elf_hash_table (info),
909 elf_link_renumber_hash_table_dynsyms,
910 &dynsymcount);
911
912 /* There is an unused NULL entry at the head of the table which we
913 must account for in our count even if the table is empty since it
914 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
915 .dynamic section. */
916 dynsymcount++;
917
918 elf_hash_table (info)->dynsymcount = dynsymcount;
919 return dynsymcount;
920 }
921
922 /* Merge st_other field. */
923
924 static void
925 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
926 const Elf_Internal_Sym *isym, asection *sec,
927 bfd_boolean definition, bfd_boolean dynamic)
928 {
929 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
930
931 /* If st_other has a processor-specific meaning, specific
932 code might be needed here. */
933 if (bed->elf_backend_merge_symbol_attribute)
934 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
935 dynamic);
936
937 if (!dynamic)
938 {
939 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
940 unsigned hvis = ELF_ST_VISIBILITY (h->other);
941
942 /* Keep the most constraining visibility. Leave the remainder
943 of the st_other field to elf_backend_merge_symbol_attribute. */
944 if (symvis - 1 < hvis - 1)
945 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
946 }
947 else if (definition
948 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
949 && (sec->flags & SEC_READONLY) == 0)
950 h->protected_def = 1;
951 }
952
953 /* This function is called when we want to merge a new symbol with an
954 existing symbol. It handles the various cases which arise when we
955 find a definition in a dynamic object, or when there is already a
956 definition in a dynamic object. The new symbol is described by
957 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
958 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
959 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
960 of an old common symbol. We set OVERRIDE if the old symbol is
961 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
962 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
963 to change. By OK to change, we mean that we shouldn't warn if the
964 type or size does change. */
965
966 static bfd_boolean
967 _bfd_elf_merge_symbol (bfd *abfd,
968 struct bfd_link_info *info,
969 const char *name,
970 Elf_Internal_Sym *sym,
971 asection **psec,
972 bfd_vma *pvalue,
973 struct elf_link_hash_entry **sym_hash,
974 bfd **poldbfd,
975 bfd_boolean *pold_weak,
976 unsigned int *pold_alignment,
977 bfd_boolean *skip,
978 bfd_boolean *override,
979 bfd_boolean *type_change_ok,
980 bfd_boolean *size_change_ok,
981 bfd_boolean *matched)
982 {
983 asection *sec, *oldsec;
984 struct elf_link_hash_entry *h;
985 struct elf_link_hash_entry *hi;
986 struct elf_link_hash_entry *flip;
987 int bind;
988 bfd *oldbfd;
989 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
990 bfd_boolean newweak, oldweak, newfunc, oldfunc;
991 const struct elf_backend_data *bed;
992 char *new_version;
993
994 *skip = FALSE;
995 *override = FALSE;
996
997 sec = *psec;
998 bind = ELF_ST_BIND (sym->st_info);
999
1000 if (! bfd_is_und_section (sec))
1001 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1002 else
1003 h = ((struct elf_link_hash_entry *)
1004 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1005 if (h == NULL)
1006 return FALSE;
1007 *sym_hash = h;
1008
1009 bed = get_elf_backend_data (abfd);
1010
1011 /* NEW_VERSION is the symbol version of the new symbol. */
1012 if (h->versioned != unversioned)
1013 {
1014 /* Symbol version is unknown or versioned. */
1015 new_version = strrchr (name, ELF_VER_CHR);
1016 if (new_version)
1017 {
1018 if (h->versioned == unknown)
1019 {
1020 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1021 h->versioned = versioned_hidden;
1022 else
1023 h->versioned = versioned;
1024 }
1025 new_version += 1;
1026 if (new_version[0] == '\0')
1027 new_version = NULL;
1028 }
1029 else
1030 h->versioned = unversioned;
1031 }
1032 else
1033 new_version = NULL;
1034
1035 /* For merging, we only care about real symbols. But we need to make
1036 sure that indirect symbol dynamic flags are updated. */
1037 hi = h;
1038 while (h->root.type == bfd_link_hash_indirect
1039 || h->root.type == bfd_link_hash_warning)
1040 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1041
1042 if (!*matched)
1043 {
1044 if (hi == h || h->root.type == bfd_link_hash_new)
1045 *matched = TRUE;
1046 else
1047 {
1048 /* OLD_HIDDEN is true if the existing symbol is only visible
1049 to the symbol with the same symbol version. NEW_HIDDEN is
1050 true if the new symbol is only visible to the symbol with
1051 the same symbol version. */
1052 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1053 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1054 if (!old_hidden && !new_hidden)
1055 /* The new symbol matches the existing symbol if both
1056 aren't hidden. */
1057 *matched = TRUE;
1058 else
1059 {
1060 /* OLD_VERSION is the symbol version of the existing
1061 symbol. */
1062 char *old_version;
1063
1064 if (h->versioned >= versioned)
1065 old_version = strrchr (h->root.root.string,
1066 ELF_VER_CHR) + 1;
1067 else
1068 old_version = NULL;
1069
1070 /* The new symbol matches the existing symbol if they
1071 have the same symbol version. */
1072 *matched = (old_version == new_version
1073 || (old_version != NULL
1074 && new_version != NULL
1075 && strcmp (old_version, new_version) == 0));
1076 }
1077 }
1078 }
1079
1080 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1081 existing symbol. */
1082
1083 oldbfd = NULL;
1084 oldsec = NULL;
1085 switch (h->root.type)
1086 {
1087 default:
1088 break;
1089
1090 case bfd_link_hash_undefined:
1091 case bfd_link_hash_undefweak:
1092 oldbfd = h->root.u.undef.abfd;
1093 break;
1094
1095 case bfd_link_hash_defined:
1096 case bfd_link_hash_defweak:
1097 oldbfd = h->root.u.def.section->owner;
1098 oldsec = h->root.u.def.section;
1099 break;
1100
1101 case bfd_link_hash_common:
1102 oldbfd = h->root.u.c.p->section->owner;
1103 oldsec = h->root.u.c.p->section;
1104 if (pold_alignment)
1105 *pold_alignment = h->root.u.c.p->alignment_power;
1106 break;
1107 }
1108 if (poldbfd && *poldbfd == NULL)
1109 *poldbfd = oldbfd;
1110
1111 /* Differentiate strong and weak symbols. */
1112 newweak = bind == STB_WEAK;
1113 oldweak = (h->root.type == bfd_link_hash_defweak
1114 || h->root.type == bfd_link_hash_undefweak);
1115 if (pold_weak)
1116 *pold_weak = oldweak;
1117
1118 /* This code is for coping with dynamic objects, and is only useful
1119 if we are doing an ELF link. */
1120 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1121 return TRUE;
1122
1123 /* We have to check it for every instance since the first few may be
1124 references and not all compilers emit symbol type for undefined
1125 symbols. */
1126 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1127
1128 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1129 respectively, is from a dynamic object. */
1130
1131 newdyn = (abfd->flags & DYNAMIC) != 0;
1132
1133 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1134 syms and defined syms in dynamic libraries respectively.
1135 ref_dynamic on the other hand can be set for a symbol defined in
1136 a dynamic library, and def_dynamic may not be set; When the
1137 definition in a dynamic lib is overridden by a definition in the
1138 executable use of the symbol in the dynamic lib becomes a
1139 reference to the executable symbol. */
1140 if (newdyn)
1141 {
1142 if (bfd_is_und_section (sec))
1143 {
1144 if (bind != STB_WEAK)
1145 {
1146 h->ref_dynamic_nonweak = 1;
1147 hi->ref_dynamic_nonweak = 1;
1148 }
1149 }
1150 else
1151 {
1152 /* Update the existing symbol only if they match. */
1153 if (*matched)
1154 h->dynamic_def = 1;
1155 hi->dynamic_def = 1;
1156 }
1157 }
1158
1159 /* If we just created the symbol, mark it as being an ELF symbol.
1160 Other than that, there is nothing to do--there is no merge issue
1161 with a newly defined symbol--so we just return. */
1162
1163 if (h->root.type == bfd_link_hash_new)
1164 {
1165 h->non_elf = 0;
1166 return TRUE;
1167 }
1168
1169 /* In cases involving weak versioned symbols, we may wind up trying
1170 to merge a symbol with itself. Catch that here, to avoid the
1171 confusion that results if we try to override a symbol with
1172 itself. The additional tests catch cases like
1173 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1174 dynamic object, which we do want to handle here. */
1175 if (abfd == oldbfd
1176 && (newweak || oldweak)
1177 && ((abfd->flags & DYNAMIC) == 0
1178 || !h->def_regular))
1179 return TRUE;
1180
1181 olddyn = FALSE;
1182 if (oldbfd != NULL)
1183 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1184 else if (oldsec != NULL)
1185 {
1186 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1187 indices used by MIPS ELF. */
1188 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1189 }
1190
1191 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1192 respectively, appear to be a definition rather than reference. */
1193
1194 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1195
1196 olddef = (h->root.type != bfd_link_hash_undefined
1197 && h->root.type != bfd_link_hash_undefweak
1198 && h->root.type != bfd_link_hash_common);
1199
1200 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1201 respectively, appear to be a function. */
1202
1203 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1204 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1205
1206 oldfunc = (h->type != STT_NOTYPE
1207 && bed->is_function_type (h->type));
1208
1209 /* If creating a default indirect symbol ("foo" or "foo@") from a
1210 dynamic versioned definition ("foo@@") skip doing so if there is
1211 an existing regular definition with a different type. We don't
1212 want, for example, a "time" variable in the executable overriding
1213 a "time" function in a shared library. */
1214 if (pold_alignment == NULL
1215 && newdyn
1216 && newdef
1217 && !olddyn
1218 && (olddef || h->root.type == bfd_link_hash_common)
1219 && ELF_ST_TYPE (sym->st_info) != h->type
1220 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1221 && h->type != STT_NOTYPE
1222 && !(newfunc && oldfunc))
1223 {
1224 *skip = TRUE;
1225 return TRUE;
1226 }
1227
1228 /* Check TLS symbols. We don't check undefined symbols introduced
1229 by "ld -u" which have no type (and oldbfd NULL), and we don't
1230 check symbols from plugins because they also have no type. */
1231 if (oldbfd != NULL
1232 && (oldbfd->flags & BFD_PLUGIN) == 0
1233 && (abfd->flags & BFD_PLUGIN) == 0
1234 && ELF_ST_TYPE (sym->st_info) != h->type
1235 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1236 {
1237 bfd *ntbfd, *tbfd;
1238 bfd_boolean ntdef, tdef;
1239 asection *ntsec, *tsec;
1240
1241 if (h->type == STT_TLS)
1242 {
1243 ntbfd = abfd;
1244 ntsec = sec;
1245 ntdef = newdef;
1246 tbfd = oldbfd;
1247 tsec = oldsec;
1248 tdef = olddef;
1249 }
1250 else
1251 {
1252 ntbfd = oldbfd;
1253 ntsec = oldsec;
1254 ntdef = olddef;
1255 tbfd = abfd;
1256 tsec = sec;
1257 tdef = newdef;
1258 }
1259
1260 if (tdef && ntdef)
1261 _bfd_error_handler
1262 /* xgettext:c-format */
1263 (_("%s: TLS definition in %B section %A "
1264 "mismatches non-TLS definition in %B section %A"),
1265 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1266 else if (!tdef && !ntdef)
1267 _bfd_error_handler
1268 /* xgettext:c-format */
1269 (_("%s: TLS reference in %B "
1270 "mismatches non-TLS reference in %B"),
1271 tbfd, ntbfd, h->root.root.string);
1272 else if (tdef)
1273 _bfd_error_handler
1274 /* xgettext:c-format */
1275 (_("%s: TLS definition in %B section %A "
1276 "mismatches non-TLS reference in %B"),
1277 tbfd, tsec, ntbfd, h->root.root.string);
1278 else
1279 _bfd_error_handler
1280 /* xgettext:c-format */
1281 (_("%s: TLS reference in %B "
1282 "mismatches non-TLS definition in %B section %A"),
1283 tbfd, ntbfd, ntsec, h->root.root.string);
1284
1285 bfd_set_error (bfd_error_bad_value);
1286 return FALSE;
1287 }
1288
1289 /* If the old symbol has non-default visibility, we ignore the new
1290 definition from a dynamic object. */
1291 if (newdyn
1292 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1293 && !bfd_is_und_section (sec))
1294 {
1295 *skip = TRUE;
1296 /* Make sure this symbol is dynamic. */
1297 h->ref_dynamic = 1;
1298 hi->ref_dynamic = 1;
1299 /* A protected symbol has external availability. Make sure it is
1300 recorded as dynamic.
1301
1302 FIXME: Should we check type and size for protected symbol? */
1303 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1304 return bfd_elf_link_record_dynamic_symbol (info, h);
1305 else
1306 return TRUE;
1307 }
1308 else if (!newdyn
1309 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1310 && h->def_dynamic)
1311 {
1312 /* If the new symbol with non-default visibility comes from a
1313 relocatable file and the old definition comes from a dynamic
1314 object, we remove the old definition. */
1315 if (hi->root.type == bfd_link_hash_indirect)
1316 {
1317 /* Handle the case where the old dynamic definition is
1318 default versioned. We need to copy the symbol info from
1319 the symbol with default version to the normal one if it
1320 was referenced before. */
1321 if (h->ref_regular)
1322 {
1323 hi->root.type = h->root.type;
1324 h->root.type = bfd_link_hash_indirect;
1325 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1326
1327 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1328 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1329 {
1330 /* If the new symbol is hidden or internal, completely undo
1331 any dynamic link state. */
1332 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1333 h->forced_local = 0;
1334 h->ref_dynamic = 0;
1335 }
1336 else
1337 h->ref_dynamic = 1;
1338
1339 h->def_dynamic = 0;
1340 /* FIXME: Should we check type and size for protected symbol? */
1341 h->size = 0;
1342 h->type = 0;
1343
1344 h = hi;
1345 }
1346 else
1347 h = hi;
1348 }
1349
1350 /* If the old symbol was undefined before, then it will still be
1351 on the undefs list. If the new symbol is undefined or
1352 common, we can't make it bfd_link_hash_new here, because new
1353 undefined or common symbols will be added to the undefs list
1354 by _bfd_generic_link_add_one_symbol. Symbols may not be
1355 added twice to the undefs list. Also, if the new symbol is
1356 undefweak then we don't want to lose the strong undef. */
1357 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1358 {
1359 h->root.type = bfd_link_hash_undefined;
1360 h->root.u.undef.abfd = abfd;
1361 }
1362 else
1363 {
1364 h->root.type = bfd_link_hash_new;
1365 h->root.u.undef.abfd = NULL;
1366 }
1367
1368 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1369 {
1370 /* If the new symbol is hidden or internal, completely undo
1371 any dynamic link state. */
1372 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1373 h->forced_local = 0;
1374 h->ref_dynamic = 0;
1375 }
1376 else
1377 h->ref_dynamic = 1;
1378 h->def_dynamic = 0;
1379 /* FIXME: Should we check type and size for protected symbol? */
1380 h->size = 0;
1381 h->type = 0;
1382 return TRUE;
1383 }
1384
1385 /* If a new weak symbol definition comes from a regular file and the
1386 old symbol comes from a dynamic library, we treat the new one as
1387 strong. Similarly, an old weak symbol definition from a regular
1388 file is treated as strong when the new symbol comes from a dynamic
1389 library. Further, an old weak symbol from a dynamic library is
1390 treated as strong if the new symbol is from a dynamic library.
1391 This reflects the way glibc's ld.so works.
1392
1393 Do this before setting *type_change_ok or *size_change_ok so that
1394 we warn properly when dynamic library symbols are overridden. */
1395
1396 if (newdef && !newdyn && olddyn)
1397 newweak = FALSE;
1398 if (olddef && newdyn)
1399 oldweak = FALSE;
1400
1401 /* Allow changes between different types of function symbol. */
1402 if (newfunc && oldfunc)
1403 *type_change_ok = TRUE;
1404
1405 /* It's OK to change the type if either the existing symbol or the
1406 new symbol is weak. A type change is also OK if the old symbol
1407 is undefined and the new symbol is defined. */
1408
1409 if (oldweak
1410 || newweak
1411 || (newdef
1412 && h->root.type == bfd_link_hash_undefined))
1413 *type_change_ok = TRUE;
1414
1415 /* It's OK to change the size if either the existing symbol or the
1416 new symbol is weak, or if the old symbol is undefined. */
1417
1418 if (*type_change_ok
1419 || h->root.type == bfd_link_hash_undefined)
1420 *size_change_ok = TRUE;
1421
1422 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1423 symbol, respectively, appears to be a common symbol in a dynamic
1424 object. If a symbol appears in an uninitialized section, and is
1425 not weak, and is not a function, then it may be a common symbol
1426 which was resolved when the dynamic object was created. We want
1427 to treat such symbols specially, because they raise special
1428 considerations when setting the symbol size: if the symbol
1429 appears as a common symbol in a regular object, and the size in
1430 the regular object is larger, we must make sure that we use the
1431 larger size. This problematic case can always be avoided in C,
1432 but it must be handled correctly when using Fortran shared
1433 libraries.
1434
1435 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1436 likewise for OLDDYNCOMMON and OLDDEF.
1437
1438 Note that this test is just a heuristic, and that it is quite
1439 possible to have an uninitialized symbol in a shared object which
1440 is really a definition, rather than a common symbol. This could
1441 lead to some minor confusion when the symbol really is a common
1442 symbol in some regular object. However, I think it will be
1443 harmless. */
1444
1445 if (newdyn
1446 && newdef
1447 && !newweak
1448 && (sec->flags & SEC_ALLOC) != 0
1449 && (sec->flags & SEC_LOAD) == 0
1450 && sym->st_size > 0
1451 && !newfunc)
1452 newdyncommon = TRUE;
1453 else
1454 newdyncommon = FALSE;
1455
1456 if (olddyn
1457 && olddef
1458 && h->root.type == bfd_link_hash_defined
1459 && h->def_dynamic
1460 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1461 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1462 && h->size > 0
1463 && !oldfunc)
1464 olddyncommon = TRUE;
1465 else
1466 olddyncommon = FALSE;
1467
1468 /* We now know everything about the old and new symbols. We ask the
1469 backend to check if we can merge them. */
1470 if (bed->merge_symbol != NULL)
1471 {
1472 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1473 return FALSE;
1474 sec = *psec;
1475 }
1476
1477 /* If both the old and the new symbols look like common symbols in a
1478 dynamic object, set the size of the symbol to the larger of the
1479 two. */
1480
1481 if (olddyncommon
1482 && newdyncommon
1483 && sym->st_size != h->size)
1484 {
1485 /* Since we think we have two common symbols, issue a multiple
1486 common warning if desired. Note that we only warn if the
1487 size is different. If the size is the same, we simply let
1488 the old symbol override the new one as normally happens with
1489 symbols defined in dynamic objects. */
1490
1491 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1492 bfd_link_hash_common, sym->st_size);
1493 if (sym->st_size > h->size)
1494 h->size = sym->st_size;
1495
1496 *size_change_ok = TRUE;
1497 }
1498
1499 /* If we are looking at a dynamic object, and we have found a
1500 definition, we need to see if the symbol was already defined by
1501 some other object. If so, we want to use the existing
1502 definition, and we do not want to report a multiple symbol
1503 definition error; we do this by clobbering *PSEC to be
1504 bfd_und_section_ptr.
1505
1506 We treat a common symbol as a definition if the symbol in the
1507 shared library is a function, since common symbols always
1508 represent variables; this can cause confusion in principle, but
1509 any such confusion would seem to indicate an erroneous program or
1510 shared library. We also permit a common symbol in a regular
1511 object to override a weak symbol in a shared object. A common
1512 symbol in executable also overrides a symbol in a shared object. */
1513
1514 if (newdyn
1515 && newdef
1516 && (olddef
1517 || (h->root.type == bfd_link_hash_common
1518 && (newweak
1519 || newfunc
1520 || (!olddyn && bfd_link_executable (info))))))
1521 {
1522 *override = TRUE;
1523 newdef = FALSE;
1524 newdyncommon = FALSE;
1525
1526 *psec = sec = bfd_und_section_ptr;
1527 *size_change_ok = TRUE;
1528
1529 /* If we get here when the old symbol is a common symbol, then
1530 we are explicitly letting it override a weak symbol or
1531 function in a dynamic object, and we don't want to warn about
1532 a type change. If the old symbol is a defined symbol, a type
1533 change warning may still be appropriate. */
1534
1535 if (h->root.type == bfd_link_hash_common)
1536 *type_change_ok = TRUE;
1537 }
1538
1539 /* Handle the special case of an old common symbol merging with a
1540 new symbol which looks like a common symbol in a shared object.
1541 We change *PSEC and *PVALUE to make the new symbol look like a
1542 common symbol, and let _bfd_generic_link_add_one_symbol do the
1543 right thing. */
1544
1545 if (newdyncommon
1546 && h->root.type == bfd_link_hash_common)
1547 {
1548 *override = TRUE;
1549 newdef = FALSE;
1550 newdyncommon = FALSE;
1551 *pvalue = sym->st_size;
1552 *psec = sec = bed->common_section (oldsec);
1553 *size_change_ok = TRUE;
1554 }
1555
1556 /* Skip weak definitions of symbols that are already defined. */
1557 if (newdef && olddef && newweak)
1558 {
1559 /* Don't skip new non-IR weak syms. */
1560 if (!(oldbfd != NULL
1561 && (oldbfd->flags & BFD_PLUGIN) != 0
1562 && (abfd->flags & BFD_PLUGIN) == 0))
1563 {
1564 newdef = FALSE;
1565 *skip = TRUE;
1566 }
1567
1568 /* Merge st_other. If the symbol already has a dynamic index,
1569 but visibility says it should not be visible, turn it into a
1570 local symbol. */
1571 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1572 if (h->dynindx != -1)
1573 switch (ELF_ST_VISIBILITY (h->other))
1574 {
1575 case STV_INTERNAL:
1576 case STV_HIDDEN:
1577 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1578 break;
1579 }
1580 }
1581
1582 /* If the old symbol is from a dynamic object, and the new symbol is
1583 a definition which is not from a dynamic object, then the new
1584 symbol overrides the old symbol. Symbols from regular files
1585 always take precedence over symbols from dynamic objects, even if
1586 they are defined after the dynamic object in the link.
1587
1588 As above, we again permit a common symbol in a regular object to
1589 override a definition in a shared object if the shared object
1590 symbol is a function or is weak. */
1591
1592 flip = NULL;
1593 if (!newdyn
1594 && (newdef
1595 || (bfd_is_com_section (sec)
1596 && (oldweak || oldfunc)))
1597 && olddyn
1598 && olddef
1599 && h->def_dynamic)
1600 {
1601 /* Change the hash table entry to undefined, and let
1602 _bfd_generic_link_add_one_symbol do the right thing with the
1603 new definition. */
1604
1605 h->root.type = bfd_link_hash_undefined;
1606 h->root.u.undef.abfd = h->root.u.def.section->owner;
1607 *size_change_ok = TRUE;
1608
1609 olddef = FALSE;
1610 olddyncommon = FALSE;
1611
1612 /* We again permit a type change when a common symbol may be
1613 overriding a function. */
1614
1615 if (bfd_is_com_section (sec))
1616 {
1617 if (oldfunc)
1618 {
1619 /* If a common symbol overrides a function, make sure
1620 that it isn't defined dynamically nor has type
1621 function. */
1622 h->def_dynamic = 0;
1623 h->type = STT_NOTYPE;
1624 }
1625 *type_change_ok = TRUE;
1626 }
1627
1628 if (hi->root.type == bfd_link_hash_indirect)
1629 flip = hi;
1630 else
1631 /* This union may have been set to be non-NULL when this symbol
1632 was seen in a dynamic object. We must force the union to be
1633 NULL, so that it is correct for a regular symbol. */
1634 h->verinfo.vertree = NULL;
1635 }
1636
1637 /* Handle the special case of a new common symbol merging with an
1638 old symbol that looks like it might be a common symbol defined in
1639 a shared object. Note that we have already handled the case in
1640 which a new common symbol should simply override the definition
1641 in the shared library. */
1642
1643 if (! newdyn
1644 && bfd_is_com_section (sec)
1645 && olddyncommon)
1646 {
1647 /* It would be best if we could set the hash table entry to a
1648 common symbol, but we don't know what to use for the section
1649 or the alignment. */
1650 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1651 bfd_link_hash_common, sym->st_size);
1652
1653 /* If the presumed common symbol in the dynamic object is
1654 larger, pretend that the new symbol has its size. */
1655
1656 if (h->size > *pvalue)
1657 *pvalue = h->size;
1658
1659 /* We need to remember the alignment required by the symbol
1660 in the dynamic object. */
1661 BFD_ASSERT (pold_alignment);
1662 *pold_alignment = h->root.u.def.section->alignment_power;
1663
1664 olddef = FALSE;
1665 olddyncommon = FALSE;
1666
1667 h->root.type = bfd_link_hash_undefined;
1668 h->root.u.undef.abfd = h->root.u.def.section->owner;
1669
1670 *size_change_ok = TRUE;
1671 *type_change_ok = TRUE;
1672
1673 if (hi->root.type == bfd_link_hash_indirect)
1674 flip = hi;
1675 else
1676 h->verinfo.vertree = NULL;
1677 }
1678
1679 if (flip != NULL)
1680 {
1681 /* Handle the case where we had a versioned symbol in a dynamic
1682 library and now find a definition in a normal object. In this
1683 case, we make the versioned symbol point to the normal one. */
1684 flip->root.type = h->root.type;
1685 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1686 h->root.type = bfd_link_hash_indirect;
1687 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1688 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1689 if (h->def_dynamic)
1690 {
1691 h->def_dynamic = 0;
1692 flip->ref_dynamic = 1;
1693 }
1694 }
1695
1696 return TRUE;
1697 }
1698
1699 /* This function is called to create an indirect symbol from the
1700 default for the symbol with the default version if needed. The
1701 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1702 set DYNSYM if the new indirect symbol is dynamic. */
1703
1704 static bfd_boolean
1705 _bfd_elf_add_default_symbol (bfd *abfd,
1706 struct bfd_link_info *info,
1707 struct elf_link_hash_entry *h,
1708 const char *name,
1709 Elf_Internal_Sym *sym,
1710 asection *sec,
1711 bfd_vma value,
1712 bfd **poldbfd,
1713 bfd_boolean *dynsym)
1714 {
1715 bfd_boolean type_change_ok;
1716 bfd_boolean size_change_ok;
1717 bfd_boolean skip;
1718 char *shortname;
1719 struct elf_link_hash_entry *hi;
1720 struct bfd_link_hash_entry *bh;
1721 const struct elf_backend_data *bed;
1722 bfd_boolean collect;
1723 bfd_boolean dynamic;
1724 bfd_boolean override;
1725 char *p;
1726 size_t len, shortlen;
1727 asection *tmp_sec;
1728 bfd_boolean matched;
1729
1730 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1731 return TRUE;
1732
1733 /* If this symbol has a version, and it is the default version, we
1734 create an indirect symbol from the default name to the fully
1735 decorated name. This will cause external references which do not
1736 specify a version to be bound to this version of the symbol. */
1737 p = strchr (name, ELF_VER_CHR);
1738 if (h->versioned == unknown)
1739 {
1740 if (p == NULL)
1741 {
1742 h->versioned = unversioned;
1743 return TRUE;
1744 }
1745 else
1746 {
1747 if (p[1] != ELF_VER_CHR)
1748 {
1749 h->versioned = versioned_hidden;
1750 return TRUE;
1751 }
1752 else
1753 h->versioned = versioned;
1754 }
1755 }
1756 else
1757 {
1758 /* PR ld/19073: We may see an unversioned definition after the
1759 default version. */
1760 if (p == NULL)
1761 return TRUE;
1762 }
1763
1764 bed = get_elf_backend_data (abfd);
1765 collect = bed->collect;
1766 dynamic = (abfd->flags & DYNAMIC) != 0;
1767
1768 shortlen = p - name;
1769 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1770 if (shortname == NULL)
1771 return FALSE;
1772 memcpy (shortname, name, shortlen);
1773 shortname[shortlen] = '\0';
1774
1775 /* We are going to create a new symbol. Merge it with any existing
1776 symbol with this name. For the purposes of the merge, act as
1777 though we were defining the symbol we just defined, although we
1778 actually going to define an indirect symbol. */
1779 type_change_ok = FALSE;
1780 size_change_ok = FALSE;
1781 matched = TRUE;
1782 tmp_sec = sec;
1783 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1784 &hi, poldbfd, NULL, NULL, &skip, &override,
1785 &type_change_ok, &size_change_ok, &matched))
1786 return FALSE;
1787
1788 if (skip)
1789 goto nondefault;
1790
1791 if (hi->def_regular)
1792 {
1793 /* If the undecorated symbol will have a version added by a
1794 script different to H, then don't indirect to/from the
1795 undecorated symbol. This isn't ideal because we may not yet
1796 have seen symbol versions, if given by a script on the
1797 command line rather than via --version-script. */
1798 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1799 {
1800 bfd_boolean hide;
1801
1802 hi->verinfo.vertree
1803 = bfd_find_version_for_sym (info->version_info,
1804 hi->root.root.string, &hide);
1805 if (hi->verinfo.vertree != NULL && hide)
1806 {
1807 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1808 goto nondefault;
1809 }
1810 }
1811 if (hi->verinfo.vertree != NULL
1812 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1813 goto nondefault;
1814 }
1815
1816 if (! override)
1817 {
1818 /* Add the default symbol if not performing a relocatable link. */
1819 if (! bfd_link_relocatable (info))
1820 {
1821 bh = &hi->root;
1822 if (! (_bfd_generic_link_add_one_symbol
1823 (info, abfd, shortname, BSF_INDIRECT,
1824 bfd_ind_section_ptr,
1825 0, name, FALSE, collect, &bh)))
1826 return FALSE;
1827 hi = (struct elf_link_hash_entry *) bh;
1828 }
1829 }
1830 else
1831 {
1832 /* In this case the symbol named SHORTNAME is overriding the
1833 indirect symbol we want to add. We were planning on making
1834 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1835 is the name without a version. NAME is the fully versioned
1836 name, and it is the default version.
1837
1838 Overriding means that we already saw a definition for the
1839 symbol SHORTNAME in a regular object, and it is overriding
1840 the symbol defined in the dynamic object.
1841
1842 When this happens, we actually want to change NAME, the
1843 symbol we just added, to refer to SHORTNAME. This will cause
1844 references to NAME in the shared object to become references
1845 to SHORTNAME in the regular object. This is what we expect
1846 when we override a function in a shared object: that the
1847 references in the shared object will be mapped to the
1848 definition in the regular object. */
1849
1850 while (hi->root.type == bfd_link_hash_indirect
1851 || hi->root.type == bfd_link_hash_warning)
1852 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1853
1854 h->root.type = bfd_link_hash_indirect;
1855 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1856 if (h->def_dynamic)
1857 {
1858 h->def_dynamic = 0;
1859 hi->ref_dynamic = 1;
1860 if (hi->ref_regular
1861 || hi->def_regular)
1862 {
1863 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1864 return FALSE;
1865 }
1866 }
1867
1868 /* Now set HI to H, so that the following code will set the
1869 other fields correctly. */
1870 hi = h;
1871 }
1872
1873 /* Check if HI is a warning symbol. */
1874 if (hi->root.type == bfd_link_hash_warning)
1875 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1876
1877 /* If there is a duplicate definition somewhere, then HI may not
1878 point to an indirect symbol. We will have reported an error to
1879 the user in that case. */
1880
1881 if (hi->root.type == bfd_link_hash_indirect)
1882 {
1883 struct elf_link_hash_entry *ht;
1884
1885 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1886 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1887
1888 /* A reference to the SHORTNAME symbol from a dynamic library
1889 will be satisfied by the versioned symbol at runtime. In
1890 effect, we have a reference to the versioned symbol. */
1891 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1892 hi->dynamic_def |= ht->dynamic_def;
1893
1894 /* See if the new flags lead us to realize that the symbol must
1895 be dynamic. */
1896 if (! *dynsym)
1897 {
1898 if (! dynamic)
1899 {
1900 if (! bfd_link_executable (info)
1901 || hi->def_dynamic
1902 || hi->ref_dynamic)
1903 *dynsym = TRUE;
1904 }
1905 else
1906 {
1907 if (hi->ref_regular)
1908 *dynsym = TRUE;
1909 }
1910 }
1911 }
1912
1913 /* We also need to define an indirection from the nondefault version
1914 of the symbol. */
1915
1916 nondefault:
1917 len = strlen (name);
1918 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
1919 if (shortname == NULL)
1920 return FALSE;
1921 memcpy (shortname, name, shortlen);
1922 memcpy (shortname + shortlen, p + 1, len - shortlen);
1923
1924 /* Once again, merge with any existing symbol. */
1925 type_change_ok = FALSE;
1926 size_change_ok = FALSE;
1927 tmp_sec = sec;
1928 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1929 &hi, poldbfd, NULL, NULL, &skip, &override,
1930 &type_change_ok, &size_change_ok, &matched))
1931 return FALSE;
1932
1933 if (skip)
1934 return TRUE;
1935
1936 if (override)
1937 {
1938 /* Here SHORTNAME is a versioned name, so we don't expect to see
1939 the type of override we do in the case above unless it is
1940 overridden by a versioned definition. */
1941 if (hi->root.type != bfd_link_hash_defined
1942 && hi->root.type != bfd_link_hash_defweak)
1943 _bfd_error_handler
1944 /* xgettext:c-format */
1945 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1946 abfd, shortname);
1947 }
1948 else
1949 {
1950 bh = &hi->root;
1951 if (! (_bfd_generic_link_add_one_symbol
1952 (info, abfd, shortname, BSF_INDIRECT,
1953 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1954 return FALSE;
1955 hi = (struct elf_link_hash_entry *) bh;
1956
1957 /* If there is a duplicate definition somewhere, then HI may not
1958 point to an indirect symbol. We will have reported an error
1959 to the user in that case. */
1960
1961 if (hi->root.type == bfd_link_hash_indirect)
1962 {
1963 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1964 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1965 hi->dynamic_def |= h->dynamic_def;
1966
1967 /* See if the new flags lead us to realize that the symbol
1968 must be dynamic. */
1969 if (! *dynsym)
1970 {
1971 if (! dynamic)
1972 {
1973 if (! bfd_link_executable (info)
1974 || hi->ref_dynamic)
1975 *dynsym = TRUE;
1976 }
1977 else
1978 {
1979 if (hi->ref_regular)
1980 *dynsym = TRUE;
1981 }
1982 }
1983 }
1984 }
1985
1986 return TRUE;
1987 }
1988 \f
1989 /* This routine is used to export all defined symbols into the dynamic
1990 symbol table. It is called via elf_link_hash_traverse. */
1991
1992 static bfd_boolean
1993 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1994 {
1995 struct elf_info_failed *eif = (struct elf_info_failed *) data;
1996
1997 /* Ignore indirect symbols. These are added by the versioning code. */
1998 if (h->root.type == bfd_link_hash_indirect)
1999 return TRUE;
2000
2001 /* Ignore this if we won't export it. */
2002 if (!eif->info->export_dynamic && !h->dynamic)
2003 return TRUE;
2004
2005 if (h->dynindx == -1
2006 && (h->def_regular || h->ref_regular)
2007 && ! bfd_hide_sym_by_version (eif->info->version_info,
2008 h->root.root.string))
2009 {
2010 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2011 {
2012 eif->failed = TRUE;
2013 return FALSE;
2014 }
2015 }
2016
2017 return TRUE;
2018 }
2019 \f
2020 /* Look through the symbols which are defined in other shared
2021 libraries and referenced here. Update the list of version
2022 dependencies. This will be put into the .gnu.version_r section.
2023 This function is called via elf_link_hash_traverse. */
2024
2025 static bfd_boolean
2026 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2027 void *data)
2028 {
2029 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2030 Elf_Internal_Verneed *t;
2031 Elf_Internal_Vernaux *a;
2032 bfd_size_type amt;
2033
2034 /* We only care about symbols defined in shared objects with version
2035 information. */
2036 if (!h->def_dynamic
2037 || h->def_regular
2038 || h->dynindx == -1
2039 || h->verinfo.verdef == NULL
2040 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2041 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2042 return TRUE;
2043
2044 /* See if we already know about this version. */
2045 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2046 t != NULL;
2047 t = t->vn_nextref)
2048 {
2049 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2050 continue;
2051
2052 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2053 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2054 return TRUE;
2055
2056 break;
2057 }
2058
2059 /* This is a new version. Add it to tree we are building. */
2060
2061 if (t == NULL)
2062 {
2063 amt = sizeof *t;
2064 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2065 if (t == NULL)
2066 {
2067 rinfo->failed = TRUE;
2068 return FALSE;
2069 }
2070
2071 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2072 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2073 elf_tdata (rinfo->info->output_bfd)->verref = t;
2074 }
2075
2076 amt = sizeof *a;
2077 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2078 if (a == NULL)
2079 {
2080 rinfo->failed = TRUE;
2081 return FALSE;
2082 }
2083
2084 /* Note that we are copying a string pointer here, and testing it
2085 above. If bfd_elf_string_from_elf_section is ever changed to
2086 discard the string data when low in memory, this will have to be
2087 fixed. */
2088 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2089
2090 a->vna_flags = h->verinfo.verdef->vd_flags;
2091 a->vna_nextptr = t->vn_auxptr;
2092
2093 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2094 ++rinfo->vers;
2095
2096 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2097
2098 t->vn_auxptr = a;
2099
2100 return TRUE;
2101 }
2102
2103 /* Figure out appropriate versions for all the symbols. We may not
2104 have the version number script until we have read all of the input
2105 files, so until that point we don't know which symbols should be
2106 local. This function is called via elf_link_hash_traverse. */
2107
2108 static bfd_boolean
2109 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2110 {
2111 struct elf_info_failed *sinfo;
2112 struct bfd_link_info *info;
2113 const struct elf_backend_data *bed;
2114 struct elf_info_failed eif;
2115 char *p;
2116
2117 sinfo = (struct elf_info_failed *) data;
2118 info = sinfo->info;
2119
2120 /* Fix the symbol flags. */
2121 eif.failed = FALSE;
2122 eif.info = info;
2123 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2124 {
2125 if (eif.failed)
2126 sinfo->failed = TRUE;
2127 return FALSE;
2128 }
2129
2130 /* We only need version numbers for symbols defined in regular
2131 objects. */
2132 if (!h->def_regular)
2133 return TRUE;
2134
2135 bed = get_elf_backend_data (info->output_bfd);
2136 p = strchr (h->root.root.string, ELF_VER_CHR);
2137 if (p != NULL && h->verinfo.vertree == NULL)
2138 {
2139 struct bfd_elf_version_tree *t;
2140
2141 ++p;
2142 if (*p == ELF_VER_CHR)
2143 ++p;
2144
2145 /* If there is no version string, we can just return out. */
2146 if (*p == '\0')
2147 return TRUE;
2148
2149 /* Look for the version. If we find it, it is no longer weak. */
2150 for (t = sinfo->info->version_info; t != NULL; t = t->next)
2151 {
2152 if (strcmp (t->name, p) == 0)
2153 {
2154 size_t len;
2155 char *alc;
2156 struct bfd_elf_version_expr *d;
2157
2158 len = p - h->root.root.string;
2159 alc = (char *) bfd_malloc (len);
2160 if (alc == NULL)
2161 {
2162 sinfo->failed = TRUE;
2163 return FALSE;
2164 }
2165 memcpy (alc, h->root.root.string, len - 1);
2166 alc[len - 1] = '\0';
2167 if (alc[len - 2] == ELF_VER_CHR)
2168 alc[len - 2] = '\0';
2169
2170 h->verinfo.vertree = t;
2171 t->used = TRUE;
2172 d = NULL;
2173
2174 if (t->globals.list != NULL)
2175 d = (*t->match) (&t->globals, NULL, alc);
2176
2177 /* See if there is anything to force this symbol to
2178 local scope. */
2179 if (d == NULL && t->locals.list != NULL)
2180 {
2181 d = (*t->match) (&t->locals, NULL, alc);
2182 if (d != NULL
2183 && h->dynindx != -1
2184 && ! info->export_dynamic)
2185 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2186 }
2187
2188 free (alc);
2189 break;
2190 }
2191 }
2192
2193 /* If we are building an application, we need to create a
2194 version node for this version. */
2195 if (t == NULL && bfd_link_executable (info))
2196 {
2197 struct bfd_elf_version_tree **pp;
2198 int version_index;
2199
2200 /* If we aren't going to export this symbol, we don't need
2201 to worry about it. */
2202 if (h->dynindx == -1)
2203 return TRUE;
2204
2205 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2206 sizeof *t);
2207 if (t == NULL)
2208 {
2209 sinfo->failed = TRUE;
2210 return FALSE;
2211 }
2212
2213 t->name = p;
2214 t->name_indx = (unsigned int) -1;
2215 t->used = TRUE;
2216
2217 version_index = 1;
2218 /* Don't count anonymous version tag. */
2219 if (sinfo->info->version_info != NULL
2220 && sinfo->info->version_info->vernum == 0)
2221 version_index = 0;
2222 for (pp = &sinfo->info->version_info;
2223 *pp != NULL;
2224 pp = &(*pp)->next)
2225 ++version_index;
2226 t->vernum = version_index;
2227
2228 *pp = t;
2229
2230 h->verinfo.vertree = t;
2231 }
2232 else if (t == NULL)
2233 {
2234 /* We could not find the version for a symbol when
2235 generating a shared archive. Return an error. */
2236 _bfd_error_handler
2237 /* xgettext:c-format */
2238 (_("%B: version node not found for symbol %s"),
2239 info->output_bfd, h->root.root.string);
2240 bfd_set_error (bfd_error_bad_value);
2241 sinfo->failed = TRUE;
2242 return FALSE;
2243 }
2244 }
2245
2246 /* If we don't have a version for this symbol, see if we can find
2247 something. */
2248 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
2249 {
2250 bfd_boolean hide;
2251
2252 h->verinfo.vertree
2253 = bfd_find_version_for_sym (sinfo->info->version_info,
2254 h->root.root.string, &hide);
2255 if (h->verinfo.vertree != NULL && hide)
2256 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2257 }
2258
2259 return TRUE;
2260 }
2261 \f
2262 /* Read and swap the relocs from the section indicated by SHDR. This
2263 may be either a REL or a RELA section. The relocations are
2264 translated into RELA relocations and stored in INTERNAL_RELOCS,
2265 which should have already been allocated to contain enough space.
2266 The EXTERNAL_RELOCS are a buffer where the external form of the
2267 relocations should be stored.
2268
2269 Returns FALSE if something goes wrong. */
2270
2271 static bfd_boolean
2272 elf_link_read_relocs_from_section (bfd *abfd,
2273 asection *sec,
2274 Elf_Internal_Shdr *shdr,
2275 void *external_relocs,
2276 Elf_Internal_Rela *internal_relocs)
2277 {
2278 const struct elf_backend_data *bed;
2279 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2280 const bfd_byte *erela;
2281 const bfd_byte *erelaend;
2282 Elf_Internal_Rela *irela;
2283 Elf_Internal_Shdr *symtab_hdr;
2284 size_t nsyms;
2285
2286 /* Position ourselves at the start of the section. */
2287 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2288 return FALSE;
2289
2290 /* Read the relocations. */
2291 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2292 return FALSE;
2293
2294 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2295 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2296
2297 bed = get_elf_backend_data (abfd);
2298
2299 /* Convert the external relocations to the internal format. */
2300 if (shdr->sh_entsize == bed->s->sizeof_rel)
2301 swap_in = bed->s->swap_reloc_in;
2302 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2303 swap_in = bed->s->swap_reloca_in;
2304 else
2305 {
2306 bfd_set_error (bfd_error_wrong_format);
2307 return FALSE;
2308 }
2309
2310 erela = (const bfd_byte *) external_relocs;
2311 erelaend = erela + shdr->sh_size;
2312 irela = internal_relocs;
2313 while (erela < erelaend)
2314 {
2315 bfd_vma r_symndx;
2316
2317 (*swap_in) (abfd, erela, irela);
2318 r_symndx = ELF32_R_SYM (irela->r_info);
2319 if (bed->s->arch_size == 64)
2320 r_symndx >>= 24;
2321 if (nsyms > 0)
2322 {
2323 if ((size_t) r_symndx >= nsyms)
2324 {
2325 _bfd_error_handler
2326 /* xgettext:c-format */
2327 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2328 " for offset 0x%lx in section `%A'"),
2329 abfd, sec,
2330 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2331 bfd_set_error (bfd_error_bad_value);
2332 return FALSE;
2333 }
2334 }
2335 else if (r_symndx != STN_UNDEF)
2336 {
2337 _bfd_error_handler
2338 /* xgettext:c-format */
2339 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2340 " when the object file has no symbol table"),
2341 abfd, sec,
2342 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2343 bfd_set_error (bfd_error_bad_value);
2344 return FALSE;
2345 }
2346 irela += bed->s->int_rels_per_ext_rel;
2347 erela += shdr->sh_entsize;
2348 }
2349
2350 return TRUE;
2351 }
2352
2353 /* Read and swap the relocs for a section O. They may have been
2354 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2355 not NULL, they are used as buffers to read into. They are known to
2356 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2357 the return value is allocated using either malloc or bfd_alloc,
2358 according to the KEEP_MEMORY argument. If O has two relocation
2359 sections (both REL and RELA relocations), then the REL_HDR
2360 relocations will appear first in INTERNAL_RELOCS, followed by the
2361 RELA_HDR relocations. */
2362
2363 Elf_Internal_Rela *
2364 _bfd_elf_link_read_relocs (bfd *abfd,
2365 asection *o,
2366 void *external_relocs,
2367 Elf_Internal_Rela *internal_relocs,
2368 bfd_boolean keep_memory)
2369 {
2370 void *alloc1 = NULL;
2371 Elf_Internal_Rela *alloc2 = NULL;
2372 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2373 struct bfd_elf_section_data *esdo = elf_section_data (o);
2374 Elf_Internal_Rela *internal_rela_relocs;
2375
2376 if (esdo->relocs != NULL)
2377 return esdo->relocs;
2378
2379 if (o->reloc_count == 0)
2380 return NULL;
2381
2382 if (internal_relocs == NULL)
2383 {
2384 bfd_size_type size;
2385
2386 size = o->reloc_count;
2387 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2388 if (keep_memory)
2389 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2390 else
2391 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2392 if (internal_relocs == NULL)
2393 goto error_return;
2394 }
2395
2396 if (external_relocs == NULL)
2397 {
2398 bfd_size_type size = 0;
2399
2400 if (esdo->rel.hdr)
2401 size += esdo->rel.hdr->sh_size;
2402 if (esdo->rela.hdr)
2403 size += esdo->rela.hdr->sh_size;
2404
2405 alloc1 = bfd_malloc (size);
2406 if (alloc1 == NULL)
2407 goto error_return;
2408 external_relocs = alloc1;
2409 }
2410
2411 internal_rela_relocs = internal_relocs;
2412 if (esdo->rel.hdr)
2413 {
2414 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2415 external_relocs,
2416 internal_relocs))
2417 goto error_return;
2418 external_relocs = (((bfd_byte *) external_relocs)
2419 + esdo->rel.hdr->sh_size);
2420 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2421 * bed->s->int_rels_per_ext_rel);
2422 }
2423
2424 if (esdo->rela.hdr
2425 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2426 external_relocs,
2427 internal_rela_relocs)))
2428 goto error_return;
2429
2430 /* Cache the results for next time, if we can. */
2431 if (keep_memory)
2432 esdo->relocs = internal_relocs;
2433
2434 if (alloc1 != NULL)
2435 free (alloc1);
2436
2437 /* Don't free alloc2, since if it was allocated we are passing it
2438 back (under the name of internal_relocs). */
2439
2440 return internal_relocs;
2441
2442 error_return:
2443 if (alloc1 != NULL)
2444 free (alloc1);
2445 if (alloc2 != NULL)
2446 {
2447 if (keep_memory)
2448 bfd_release (abfd, alloc2);
2449 else
2450 free (alloc2);
2451 }
2452 return NULL;
2453 }
2454
2455 /* Compute the size of, and allocate space for, REL_HDR which is the
2456 section header for a section containing relocations for O. */
2457
2458 static bfd_boolean
2459 _bfd_elf_link_size_reloc_section (bfd *abfd,
2460 struct bfd_elf_section_reloc_data *reldata)
2461 {
2462 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2463
2464 /* That allows us to calculate the size of the section. */
2465 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2466
2467 /* The contents field must last into write_object_contents, so we
2468 allocate it with bfd_alloc rather than malloc. Also since we
2469 cannot be sure that the contents will actually be filled in,
2470 we zero the allocated space. */
2471 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2472 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2473 return FALSE;
2474
2475 if (reldata->hashes == NULL && reldata->count)
2476 {
2477 struct elf_link_hash_entry **p;
2478
2479 p = ((struct elf_link_hash_entry **)
2480 bfd_zmalloc (reldata->count * sizeof (*p)));
2481 if (p == NULL)
2482 return FALSE;
2483
2484 reldata->hashes = p;
2485 }
2486
2487 return TRUE;
2488 }
2489
2490 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2491 originated from the section given by INPUT_REL_HDR) to the
2492 OUTPUT_BFD. */
2493
2494 bfd_boolean
2495 _bfd_elf_link_output_relocs (bfd *output_bfd,
2496 asection *input_section,
2497 Elf_Internal_Shdr *input_rel_hdr,
2498 Elf_Internal_Rela *internal_relocs,
2499 struct elf_link_hash_entry **rel_hash
2500 ATTRIBUTE_UNUSED)
2501 {
2502 Elf_Internal_Rela *irela;
2503 Elf_Internal_Rela *irelaend;
2504 bfd_byte *erel;
2505 struct bfd_elf_section_reloc_data *output_reldata;
2506 asection *output_section;
2507 const struct elf_backend_data *bed;
2508 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2509 struct bfd_elf_section_data *esdo;
2510
2511 output_section = input_section->output_section;
2512
2513 bed = get_elf_backend_data (output_bfd);
2514 esdo = elf_section_data (output_section);
2515 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2516 {
2517 output_reldata = &esdo->rel;
2518 swap_out = bed->s->swap_reloc_out;
2519 }
2520 else if (esdo->rela.hdr
2521 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2522 {
2523 output_reldata = &esdo->rela;
2524 swap_out = bed->s->swap_reloca_out;
2525 }
2526 else
2527 {
2528 _bfd_error_handler
2529 /* xgettext:c-format */
2530 (_("%B: relocation size mismatch in %B section %A"),
2531 output_bfd, input_section->owner, input_section);
2532 bfd_set_error (bfd_error_wrong_format);
2533 return FALSE;
2534 }
2535
2536 erel = output_reldata->hdr->contents;
2537 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2538 irela = internal_relocs;
2539 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2540 * bed->s->int_rels_per_ext_rel);
2541 while (irela < irelaend)
2542 {
2543 (*swap_out) (output_bfd, irela, erel);
2544 irela += bed->s->int_rels_per_ext_rel;
2545 erel += input_rel_hdr->sh_entsize;
2546 }
2547
2548 /* Bump the counter, so that we know where to add the next set of
2549 relocations. */
2550 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2551
2552 return TRUE;
2553 }
2554 \f
2555 /* Make weak undefined symbols in PIE dynamic. */
2556
2557 bfd_boolean
2558 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2559 struct elf_link_hash_entry *h)
2560 {
2561 if (bfd_link_pie (info)
2562 && h->dynindx == -1
2563 && h->root.type == bfd_link_hash_undefweak)
2564 return bfd_elf_link_record_dynamic_symbol (info, h);
2565
2566 return TRUE;
2567 }
2568
2569 /* Fix up the flags for a symbol. This handles various cases which
2570 can only be fixed after all the input files are seen. This is
2571 currently called by both adjust_dynamic_symbol and
2572 assign_sym_version, which is unnecessary but perhaps more robust in
2573 the face of future changes. */
2574
2575 static bfd_boolean
2576 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2577 struct elf_info_failed *eif)
2578 {
2579 const struct elf_backend_data *bed;
2580
2581 /* If this symbol was mentioned in a non-ELF file, try to set
2582 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2583 permit a non-ELF file to correctly refer to a symbol defined in
2584 an ELF dynamic object. */
2585 if (h->non_elf)
2586 {
2587 while (h->root.type == bfd_link_hash_indirect)
2588 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2589
2590 if (h->root.type != bfd_link_hash_defined
2591 && h->root.type != bfd_link_hash_defweak)
2592 {
2593 h->ref_regular = 1;
2594 h->ref_regular_nonweak = 1;
2595 }
2596 else
2597 {
2598 if (h->root.u.def.section->owner != NULL
2599 && (bfd_get_flavour (h->root.u.def.section->owner)
2600 == bfd_target_elf_flavour))
2601 {
2602 h->ref_regular = 1;
2603 h->ref_regular_nonweak = 1;
2604 }
2605 else
2606 h->def_regular = 1;
2607 }
2608
2609 if (h->dynindx == -1
2610 && (h->def_dynamic
2611 || h->ref_dynamic))
2612 {
2613 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2614 {
2615 eif->failed = TRUE;
2616 return FALSE;
2617 }
2618 }
2619 }
2620 else
2621 {
2622 /* Unfortunately, NON_ELF is only correct if the symbol
2623 was first seen in a non-ELF file. Fortunately, if the symbol
2624 was first seen in an ELF file, we're probably OK unless the
2625 symbol was defined in a non-ELF file. Catch that case here.
2626 FIXME: We're still in trouble if the symbol was first seen in
2627 a dynamic object, and then later in a non-ELF regular object. */
2628 if ((h->root.type == bfd_link_hash_defined
2629 || h->root.type == bfd_link_hash_defweak)
2630 && !h->def_regular
2631 && (h->root.u.def.section->owner != NULL
2632 ? (bfd_get_flavour (h->root.u.def.section->owner)
2633 != bfd_target_elf_flavour)
2634 : (bfd_is_abs_section (h->root.u.def.section)
2635 && !h->def_dynamic)))
2636 h->def_regular = 1;
2637 }
2638
2639 /* Backend specific symbol fixup. */
2640 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2641 if (bed->elf_backend_fixup_symbol
2642 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2643 return FALSE;
2644
2645 /* If this is a final link, and the symbol was defined as a common
2646 symbol in a regular object file, and there was no definition in
2647 any dynamic object, then the linker will have allocated space for
2648 the symbol in a common section but the DEF_REGULAR
2649 flag will not have been set. */
2650 if (h->root.type == bfd_link_hash_defined
2651 && !h->def_regular
2652 && h->ref_regular
2653 && !h->def_dynamic
2654 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2655 h->def_regular = 1;
2656
2657 /* If a weak undefined symbol has non-default visibility, we also
2658 hide it from the dynamic linker. */
2659 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2660 && h->root.type == bfd_link_hash_undefweak)
2661 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2662
2663 /* A hidden versioned symbol in executable should be forced local if
2664 it is is locally defined, not referenced by shared library and not
2665 exported. */
2666 else if (bfd_link_executable (eif->info)
2667 && h->versioned == versioned_hidden
2668 && !eif->info->export_dynamic
2669 && !h->dynamic
2670 && !h->ref_dynamic
2671 && h->def_regular)
2672 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2673
2674 /* If -Bsymbolic was used (which means to bind references to global
2675 symbols to the definition within the shared object), and this
2676 symbol was defined in a regular object, then it actually doesn't
2677 need a PLT entry. Likewise, if the symbol has non-default
2678 visibility. If the symbol has hidden or internal visibility, we
2679 will force it local. */
2680 else if (h->needs_plt
2681 && bfd_link_pic (eif->info)
2682 && is_elf_hash_table (eif->info->hash)
2683 && (SYMBOLIC_BIND (eif->info, h)
2684 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2685 && h->def_regular)
2686 {
2687 bfd_boolean force_local;
2688
2689 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2690 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2691 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2692 }
2693
2694 /* If this is a weak defined symbol in a dynamic object, and we know
2695 the real definition in the dynamic object, copy interesting flags
2696 over to the real definition. */
2697 if (h->u.weakdef != NULL)
2698 {
2699 /* If the real definition is defined by a regular object file,
2700 don't do anything special. See the longer description in
2701 _bfd_elf_adjust_dynamic_symbol, below. */
2702 if (h->u.weakdef->def_regular)
2703 h->u.weakdef = NULL;
2704 else
2705 {
2706 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2707
2708 while (h->root.type == bfd_link_hash_indirect)
2709 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2710
2711 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2712 || h->root.type == bfd_link_hash_defweak);
2713 BFD_ASSERT (weakdef->def_dynamic);
2714 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2715 || weakdef->root.type == bfd_link_hash_defweak);
2716 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2717 }
2718 }
2719
2720 return TRUE;
2721 }
2722
2723 /* Make the backend pick a good value for a dynamic symbol. This is
2724 called via elf_link_hash_traverse, and also calls itself
2725 recursively. */
2726
2727 static bfd_boolean
2728 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2729 {
2730 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2731 bfd *dynobj;
2732 const struct elf_backend_data *bed;
2733
2734 if (! is_elf_hash_table (eif->info->hash))
2735 return FALSE;
2736
2737 /* Ignore indirect symbols. These are added by the versioning code. */
2738 if (h->root.type == bfd_link_hash_indirect)
2739 return TRUE;
2740
2741 /* Fix the symbol flags. */
2742 if (! _bfd_elf_fix_symbol_flags (h, eif))
2743 return FALSE;
2744
2745 /* If this symbol does not require a PLT entry, and it is not
2746 defined by a dynamic object, or is not referenced by a regular
2747 object, ignore it. We do have to handle a weak defined symbol,
2748 even if no regular object refers to it, if we decided to add it
2749 to the dynamic symbol table. FIXME: Do we normally need to worry
2750 about symbols which are defined by one dynamic object and
2751 referenced by another one? */
2752 if (!h->needs_plt
2753 && h->type != STT_GNU_IFUNC
2754 && (h->def_regular
2755 || !h->def_dynamic
2756 || (!h->ref_regular
2757 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2758 {
2759 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2760 return TRUE;
2761 }
2762
2763 /* If we've already adjusted this symbol, don't do it again. This
2764 can happen via a recursive call. */
2765 if (h->dynamic_adjusted)
2766 return TRUE;
2767
2768 /* Don't look at this symbol again. Note that we must set this
2769 after checking the above conditions, because we may look at a
2770 symbol once, decide not to do anything, and then get called
2771 recursively later after REF_REGULAR is set below. */
2772 h->dynamic_adjusted = 1;
2773
2774 /* If this is a weak definition, and we know a real definition, and
2775 the real symbol is not itself defined by a regular object file,
2776 then get a good value for the real definition. We handle the
2777 real symbol first, for the convenience of the backend routine.
2778
2779 Note that there is a confusing case here. If the real definition
2780 is defined by a regular object file, we don't get the real symbol
2781 from the dynamic object, but we do get the weak symbol. If the
2782 processor backend uses a COPY reloc, then if some routine in the
2783 dynamic object changes the real symbol, we will not see that
2784 change in the corresponding weak symbol. This is the way other
2785 ELF linkers work as well, and seems to be a result of the shared
2786 library model.
2787
2788 I will clarify this issue. Most SVR4 shared libraries define the
2789 variable _timezone and define timezone as a weak synonym. The
2790 tzset call changes _timezone. If you write
2791 extern int timezone;
2792 int _timezone = 5;
2793 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2794 you might expect that, since timezone is a synonym for _timezone,
2795 the same number will print both times. However, if the processor
2796 backend uses a COPY reloc, then actually timezone will be copied
2797 into your process image, and, since you define _timezone
2798 yourself, _timezone will not. Thus timezone and _timezone will
2799 wind up at different memory locations. The tzset call will set
2800 _timezone, leaving timezone unchanged. */
2801
2802 if (h->u.weakdef != NULL)
2803 {
2804 /* If we get to this point, there is an implicit reference to
2805 H->U.WEAKDEF by a regular object file via the weak symbol H. */
2806 h->u.weakdef->ref_regular = 1;
2807
2808 /* Ensure that the backend adjust_dynamic_symbol function sees
2809 H->U.WEAKDEF before H by recursively calling ourselves. */
2810 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2811 return FALSE;
2812 }
2813
2814 /* If a symbol has no type and no size and does not require a PLT
2815 entry, then we are probably about to do the wrong thing here: we
2816 are probably going to create a COPY reloc for an empty object.
2817 This case can arise when a shared object is built with assembly
2818 code, and the assembly code fails to set the symbol type. */
2819 if (h->size == 0
2820 && h->type == STT_NOTYPE
2821 && !h->needs_plt)
2822 _bfd_error_handler
2823 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2824 h->root.root.string);
2825
2826 dynobj = elf_hash_table (eif->info)->dynobj;
2827 bed = get_elf_backend_data (dynobj);
2828
2829 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2830 {
2831 eif->failed = TRUE;
2832 return FALSE;
2833 }
2834
2835 return TRUE;
2836 }
2837
2838 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2839 DYNBSS. */
2840
2841 bfd_boolean
2842 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2843 struct elf_link_hash_entry *h,
2844 asection *dynbss)
2845 {
2846 unsigned int power_of_two;
2847 bfd_vma mask;
2848 asection *sec = h->root.u.def.section;
2849
2850 /* The section aligment of definition is the maximum alignment
2851 requirement of symbols defined in the section. Since we don't
2852 know the symbol alignment requirement, we start with the
2853 maximum alignment and check low bits of the symbol address
2854 for the minimum alignment. */
2855 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2856 mask = ((bfd_vma) 1 << power_of_two) - 1;
2857 while ((h->root.u.def.value & mask) != 0)
2858 {
2859 mask >>= 1;
2860 --power_of_two;
2861 }
2862
2863 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2864 dynbss))
2865 {
2866 /* Adjust the section alignment if needed. */
2867 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2868 power_of_two))
2869 return FALSE;
2870 }
2871
2872 /* We make sure that the symbol will be aligned properly. */
2873 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2874
2875 /* Define the symbol as being at this point in DYNBSS. */
2876 h->root.u.def.section = dynbss;
2877 h->root.u.def.value = dynbss->size;
2878
2879 /* Increment the size of DYNBSS to make room for the symbol. */
2880 dynbss->size += h->size;
2881
2882 /* No error if extern_protected_data is true. */
2883 if (h->protected_def
2884 && (!info->extern_protected_data
2885 || (info->extern_protected_data < 0
2886 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
2887 info->callbacks->einfo
2888 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2889 h->root.root.string);
2890
2891 return TRUE;
2892 }
2893
2894 /* Adjust all external symbols pointing into SEC_MERGE sections
2895 to reflect the object merging within the sections. */
2896
2897 static bfd_boolean
2898 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2899 {
2900 asection *sec;
2901
2902 if ((h->root.type == bfd_link_hash_defined
2903 || h->root.type == bfd_link_hash_defweak)
2904 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2905 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
2906 {
2907 bfd *output_bfd = (bfd *) data;
2908
2909 h->root.u.def.value =
2910 _bfd_merged_section_offset (output_bfd,
2911 &h->root.u.def.section,
2912 elf_section_data (sec)->sec_info,
2913 h->root.u.def.value);
2914 }
2915
2916 return TRUE;
2917 }
2918
2919 /* Returns false if the symbol referred to by H should be considered
2920 to resolve local to the current module, and true if it should be
2921 considered to bind dynamically. */
2922
2923 bfd_boolean
2924 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2925 struct bfd_link_info *info,
2926 bfd_boolean not_local_protected)
2927 {
2928 bfd_boolean binding_stays_local_p;
2929 const struct elf_backend_data *bed;
2930 struct elf_link_hash_table *hash_table;
2931
2932 if (h == NULL)
2933 return FALSE;
2934
2935 while (h->root.type == bfd_link_hash_indirect
2936 || h->root.type == bfd_link_hash_warning)
2937 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2938
2939 /* If it was forced local, then clearly it's not dynamic. */
2940 if (h->dynindx == -1)
2941 return FALSE;
2942 if (h->forced_local)
2943 return FALSE;
2944
2945 /* Identify the cases where name binding rules say that a
2946 visible symbol resolves locally. */
2947 binding_stays_local_p = (bfd_link_executable (info)
2948 || SYMBOLIC_BIND (info, h));
2949
2950 switch (ELF_ST_VISIBILITY (h->other))
2951 {
2952 case STV_INTERNAL:
2953 case STV_HIDDEN:
2954 return FALSE;
2955
2956 case STV_PROTECTED:
2957 hash_table = elf_hash_table (info);
2958 if (!is_elf_hash_table (hash_table))
2959 return FALSE;
2960
2961 bed = get_elf_backend_data (hash_table->dynobj);
2962
2963 /* Proper resolution for function pointer equality may require
2964 that these symbols perhaps be resolved dynamically, even though
2965 we should be resolving them to the current module. */
2966 if (!not_local_protected || !bed->is_function_type (h->type))
2967 binding_stays_local_p = TRUE;
2968 break;
2969
2970 default:
2971 break;
2972 }
2973
2974 /* If it isn't defined locally, then clearly it's dynamic. */
2975 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2976 return TRUE;
2977
2978 /* Otherwise, the symbol is dynamic if binding rules don't tell
2979 us that it remains local. */
2980 return !binding_stays_local_p;
2981 }
2982
2983 /* Return true if the symbol referred to by H should be considered
2984 to resolve local to the current module, and false otherwise. Differs
2985 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2986 undefined symbols. The two functions are virtually identical except
2987 for the place where forced_local and dynindx == -1 are tested. If
2988 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2989 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2990 the symbol is local only for defined symbols.
2991 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2992 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2993 treatment of undefined weak symbols. For those that do not make
2994 undefined weak symbols dynamic, both functions may return false. */
2995
2996 bfd_boolean
2997 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2998 struct bfd_link_info *info,
2999 bfd_boolean local_protected)
3000 {
3001 const struct elf_backend_data *bed;
3002 struct elf_link_hash_table *hash_table;
3003
3004 /* If it's a local sym, of course we resolve locally. */
3005 if (h == NULL)
3006 return TRUE;
3007
3008 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3009 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3010 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3011 return TRUE;
3012
3013 /* Common symbols that become definitions don't get the DEF_REGULAR
3014 flag set, so test it first, and don't bail out. */
3015 if (ELF_COMMON_DEF_P (h))
3016 /* Do nothing. */;
3017 /* If we don't have a definition in a regular file, then we can't
3018 resolve locally. The sym is either undefined or dynamic. */
3019 else if (!h->def_regular)
3020 return FALSE;
3021
3022 /* Forced local symbols resolve locally. */
3023 if (h->forced_local)
3024 return TRUE;
3025
3026 /* As do non-dynamic symbols. */
3027 if (h->dynindx == -1)
3028 return TRUE;
3029
3030 /* At this point, we know the symbol is defined and dynamic. In an
3031 executable it must resolve locally, likewise when building symbolic
3032 shared libraries. */
3033 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3034 return TRUE;
3035
3036 /* Now deal with defined dynamic symbols in shared libraries. Ones
3037 with default visibility might not resolve locally. */
3038 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3039 return FALSE;
3040
3041 hash_table = elf_hash_table (info);
3042 if (!is_elf_hash_table (hash_table))
3043 return TRUE;
3044
3045 bed = get_elf_backend_data (hash_table->dynobj);
3046
3047 /* If extern_protected_data is false, STV_PROTECTED non-function
3048 symbols are local. */
3049 if ((!info->extern_protected_data
3050 || (info->extern_protected_data < 0
3051 && !bed->extern_protected_data))
3052 && !bed->is_function_type (h->type))
3053 return TRUE;
3054
3055 /* Function pointer equality tests may require that STV_PROTECTED
3056 symbols be treated as dynamic symbols. If the address of a
3057 function not defined in an executable is set to that function's
3058 plt entry in the executable, then the address of the function in
3059 a shared library must also be the plt entry in the executable. */
3060 return local_protected;
3061 }
3062
3063 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3064 aligned. Returns the first TLS output section. */
3065
3066 struct bfd_section *
3067 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3068 {
3069 struct bfd_section *sec, *tls;
3070 unsigned int align = 0;
3071
3072 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3073 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3074 break;
3075 tls = sec;
3076
3077 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3078 if (sec->alignment_power > align)
3079 align = sec->alignment_power;
3080
3081 elf_hash_table (info)->tls_sec = tls;
3082
3083 /* Ensure the alignment of the first section is the largest alignment,
3084 so that the tls segment starts aligned. */
3085 if (tls != NULL)
3086 tls->alignment_power = align;
3087
3088 return tls;
3089 }
3090
3091 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3092 static bfd_boolean
3093 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3094 Elf_Internal_Sym *sym)
3095 {
3096 const struct elf_backend_data *bed;
3097
3098 /* Local symbols do not count, but target specific ones might. */
3099 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3100 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3101 return FALSE;
3102
3103 bed = get_elf_backend_data (abfd);
3104 /* Function symbols do not count. */
3105 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3106 return FALSE;
3107
3108 /* If the section is undefined, then so is the symbol. */
3109 if (sym->st_shndx == SHN_UNDEF)
3110 return FALSE;
3111
3112 /* If the symbol is defined in the common section, then
3113 it is a common definition and so does not count. */
3114 if (bed->common_definition (sym))
3115 return FALSE;
3116
3117 /* If the symbol is in a target specific section then we
3118 must rely upon the backend to tell us what it is. */
3119 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3120 /* FIXME - this function is not coded yet:
3121
3122 return _bfd_is_global_symbol_definition (abfd, sym);
3123
3124 Instead for now assume that the definition is not global,
3125 Even if this is wrong, at least the linker will behave
3126 in the same way that it used to do. */
3127 return FALSE;
3128
3129 return TRUE;
3130 }
3131
3132 /* Search the symbol table of the archive element of the archive ABFD
3133 whose archive map contains a mention of SYMDEF, and determine if
3134 the symbol is defined in this element. */
3135 static bfd_boolean
3136 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3137 {
3138 Elf_Internal_Shdr * hdr;
3139 size_t symcount;
3140 size_t extsymcount;
3141 size_t extsymoff;
3142 Elf_Internal_Sym *isymbuf;
3143 Elf_Internal_Sym *isym;
3144 Elf_Internal_Sym *isymend;
3145 bfd_boolean result;
3146
3147 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3148 if (abfd == NULL)
3149 return FALSE;
3150
3151 if (! bfd_check_format (abfd, bfd_object))
3152 return FALSE;
3153
3154 /* Select the appropriate symbol table. If we don't know if the
3155 object file is an IR object, give linker LTO plugin a chance to
3156 get the correct symbol table. */
3157 if (abfd->plugin_format == bfd_plugin_yes
3158 #if BFD_SUPPORTS_PLUGINS
3159 || (abfd->plugin_format == bfd_plugin_unknown
3160 && bfd_link_plugin_object_p (abfd))
3161 #endif
3162 )
3163 {
3164 /* Use the IR symbol table if the object has been claimed by
3165 plugin. */
3166 abfd = abfd->plugin_dummy_bfd;
3167 hdr = &elf_tdata (abfd)->symtab_hdr;
3168 }
3169 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3170 hdr = &elf_tdata (abfd)->symtab_hdr;
3171 else
3172 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3173
3174 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3175
3176 /* The sh_info field of the symtab header tells us where the
3177 external symbols start. We don't care about the local symbols. */
3178 if (elf_bad_symtab (abfd))
3179 {
3180 extsymcount = symcount;
3181 extsymoff = 0;
3182 }
3183 else
3184 {
3185 extsymcount = symcount - hdr->sh_info;
3186 extsymoff = hdr->sh_info;
3187 }
3188
3189 if (extsymcount == 0)
3190 return FALSE;
3191
3192 /* Read in the symbol table. */
3193 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3194 NULL, NULL, NULL);
3195 if (isymbuf == NULL)
3196 return FALSE;
3197
3198 /* Scan the symbol table looking for SYMDEF. */
3199 result = FALSE;
3200 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3201 {
3202 const char *name;
3203
3204 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3205 isym->st_name);
3206 if (name == NULL)
3207 break;
3208
3209 if (strcmp (name, symdef->name) == 0)
3210 {
3211 result = is_global_data_symbol_definition (abfd, isym);
3212 break;
3213 }
3214 }
3215
3216 free (isymbuf);
3217
3218 return result;
3219 }
3220 \f
3221 /* Add an entry to the .dynamic table. */
3222
3223 bfd_boolean
3224 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3225 bfd_vma tag,
3226 bfd_vma val)
3227 {
3228 struct elf_link_hash_table *hash_table;
3229 const struct elf_backend_data *bed;
3230 asection *s;
3231 bfd_size_type newsize;
3232 bfd_byte *newcontents;
3233 Elf_Internal_Dyn dyn;
3234
3235 hash_table = elf_hash_table (info);
3236 if (! is_elf_hash_table (hash_table))
3237 return FALSE;
3238
3239 bed = get_elf_backend_data (hash_table->dynobj);
3240 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3241 BFD_ASSERT (s != NULL);
3242
3243 newsize = s->size + bed->s->sizeof_dyn;
3244 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3245 if (newcontents == NULL)
3246 return FALSE;
3247
3248 dyn.d_tag = tag;
3249 dyn.d_un.d_val = val;
3250 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3251
3252 s->size = newsize;
3253 s->contents = newcontents;
3254
3255 return TRUE;
3256 }
3257
3258 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3259 otherwise just check whether one already exists. Returns -1 on error,
3260 1 if a DT_NEEDED tag already exists, and 0 on success. */
3261
3262 static int
3263 elf_add_dt_needed_tag (bfd *abfd,
3264 struct bfd_link_info *info,
3265 const char *soname,
3266 bfd_boolean do_it)
3267 {
3268 struct elf_link_hash_table *hash_table;
3269 size_t strindex;
3270
3271 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3272 return -1;
3273
3274 hash_table = elf_hash_table (info);
3275 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3276 if (strindex == (size_t) -1)
3277 return -1;
3278
3279 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3280 {
3281 asection *sdyn;
3282 const struct elf_backend_data *bed;
3283 bfd_byte *extdyn;
3284
3285 bed = get_elf_backend_data (hash_table->dynobj);
3286 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3287 if (sdyn != NULL)
3288 for (extdyn = sdyn->contents;
3289 extdyn < sdyn->contents + sdyn->size;
3290 extdyn += bed->s->sizeof_dyn)
3291 {
3292 Elf_Internal_Dyn dyn;
3293
3294 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3295 if (dyn.d_tag == DT_NEEDED
3296 && dyn.d_un.d_val == strindex)
3297 {
3298 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3299 return 1;
3300 }
3301 }
3302 }
3303
3304 if (do_it)
3305 {
3306 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3307 return -1;
3308
3309 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3310 return -1;
3311 }
3312 else
3313 /* We were just checking for existence of the tag. */
3314 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3315
3316 return 0;
3317 }
3318
3319 /* Return true if SONAME is on the needed list between NEEDED and STOP
3320 (or the end of list if STOP is NULL), and needed by a library that
3321 will be loaded. */
3322
3323 static bfd_boolean
3324 on_needed_list (const char *soname,
3325 struct bfd_link_needed_list *needed,
3326 struct bfd_link_needed_list *stop)
3327 {
3328 struct bfd_link_needed_list *look;
3329 for (look = needed; look != stop; look = look->next)
3330 if (strcmp (soname, look->name) == 0
3331 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3332 /* If needed by a library that itself is not directly
3333 needed, recursively check whether that library is
3334 indirectly needed. Since we add DT_NEEDED entries to
3335 the end of the list, library dependencies appear after
3336 the library. Therefore search prior to the current
3337 LOOK, preventing possible infinite recursion. */
3338 || on_needed_list (elf_dt_name (look->by), needed, look)))
3339 return TRUE;
3340
3341 return FALSE;
3342 }
3343
3344 /* Sort symbol by value, section, and size. */
3345 static int
3346 elf_sort_symbol (const void *arg1, const void *arg2)
3347 {
3348 const struct elf_link_hash_entry *h1;
3349 const struct elf_link_hash_entry *h2;
3350 bfd_signed_vma vdiff;
3351
3352 h1 = *(const struct elf_link_hash_entry **) arg1;
3353 h2 = *(const struct elf_link_hash_entry **) arg2;
3354 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3355 if (vdiff != 0)
3356 return vdiff > 0 ? 1 : -1;
3357 else
3358 {
3359 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3360 if (sdiff != 0)
3361 return sdiff > 0 ? 1 : -1;
3362 }
3363 vdiff = h1->size - h2->size;
3364 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3365 }
3366
3367 /* This function is used to adjust offsets into .dynstr for
3368 dynamic symbols. This is called via elf_link_hash_traverse. */
3369
3370 static bfd_boolean
3371 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3372 {
3373 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3374
3375 if (h->dynindx != -1)
3376 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3377 return TRUE;
3378 }
3379
3380 /* Assign string offsets in .dynstr, update all structures referencing
3381 them. */
3382
3383 static bfd_boolean
3384 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3385 {
3386 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3387 struct elf_link_local_dynamic_entry *entry;
3388 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3389 bfd *dynobj = hash_table->dynobj;
3390 asection *sdyn;
3391 bfd_size_type size;
3392 const struct elf_backend_data *bed;
3393 bfd_byte *extdyn;
3394
3395 _bfd_elf_strtab_finalize (dynstr);
3396 size = _bfd_elf_strtab_size (dynstr);
3397
3398 bed = get_elf_backend_data (dynobj);
3399 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3400 BFD_ASSERT (sdyn != NULL);
3401
3402 /* Update all .dynamic entries referencing .dynstr strings. */
3403 for (extdyn = sdyn->contents;
3404 extdyn < sdyn->contents + sdyn->size;
3405 extdyn += bed->s->sizeof_dyn)
3406 {
3407 Elf_Internal_Dyn dyn;
3408
3409 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3410 switch (dyn.d_tag)
3411 {
3412 case DT_STRSZ:
3413 dyn.d_un.d_val = size;
3414 break;
3415 case DT_NEEDED:
3416 case DT_SONAME:
3417 case DT_RPATH:
3418 case DT_RUNPATH:
3419 case DT_FILTER:
3420 case DT_AUXILIARY:
3421 case DT_AUDIT:
3422 case DT_DEPAUDIT:
3423 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3424 break;
3425 default:
3426 continue;
3427 }
3428 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3429 }
3430
3431 /* Now update local dynamic symbols. */
3432 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3433 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3434 entry->isym.st_name);
3435
3436 /* And the rest of dynamic symbols. */
3437 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3438
3439 /* Adjust version definitions. */
3440 if (elf_tdata (output_bfd)->cverdefs)
3441 {
3442 asection *s;
3443 bfd_byte *p;
3444 size_t i;
3445 Elf_Internal_Verdef def;
3446 Elf_Internal_Verdaux defaux;
3447
3448 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3449 p = s->contents;
3450 do
3451 {
3452 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3453 &def);
3454 p += sizeof (Elf_External_Verdef);
3455 if (def.vd_aux != sizeof (Elf_External_Verdef))
3456 continue;
3457 for (i = 0; i < def.vd_cnt; ++i)
3458 {
3459 _bfd_elf_swap_verdaux_in (output_bfd,
3460 (Elf_External_Verdaux *) p, &defaux);
3461 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3462 defaux.vda_name);
3463 _bfd_elf_swap_verdaux_out (output_bfd,
3464 &defaux, (Elf_External_Verdaux *) p);
3465 p += sizeof (Elf_External_Verdaux);
3466 }
3467 }
3468 while (def.vd_next);
3469 }
3470
3471 /* Adjust version references. */
3472 if (elf_tdata (output_bfd)->verref)
3473 {
3474 asection *s;
3475 bfd_byte *p;
3476 size_t i;
3477 Elf_Internal_Verneed need;
3478 Elf_Internal_Vernaux needaux;
3479
3480 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3481 p = s->contents;
3482 do
3483 {
3484 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3485 &need);
3486 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3487 _bfd_elf_swap_verneed_out (output_bfd, &need,
3488 (Elf_External_Verneed *) p);
3489 p += sizeof (Elf_External_Verneed);
3490 for (i = 0; i < need.vn_cnt; ++i)
3491 {
3492 _bfd_elf_swap_vernaux_in (output_bfd,
3493 (Elf_External_Vernaux *) p, &needaux);
3494 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3495 needaux.vna_name);
3496 _bfd_elf_swap_vernaux_out (output_bfd,
3497 &needaux,
3498 (Elf_External_Vernaux *) p);
3499 p += sizeof (Elf_External_Vernaux);
3500 }
3501 }
3502 while (need.vn_next);
3503 }
3504
3505 return TRUE;
3506 }
3507 \f
3508 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3509 The default is to only match when the INPUT and OUTPUT are exactly
3510 the same target. */
3511
3512 bfd_boolean
3513 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3514 const bfd_target *output)
3515 {
3516 return input == output;
3517 }
3518
3519 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3520 This version is used when different targets for the same architecture
3521 are virtually identical. */
3522
3523 bfd_boolean
3524 _bfd_elf_relocs_compatible (const bfd_target *input,
3525 const bfd_target *output)
3526 {
3527 const struct elf_backend_data *obed, *ibed;
3528
3529 if (input == output)
3530 return TRUE;
3531
3532 ibed = xvec_get_elf_backend_data (input);
3533 obed = xvec_get_elf_backend_data (output);
3534
3535 if (ibed->arch != obed->arch)
3536 return FALSE;
3537
3538 /* If both backends are using this function, deem them compatible. */
3539 return ibed->relocs_compatible == obed->relocs_compatible;
3540 }
3541
3542 /* Make a special call to the linker "notice" function to tell it that
3543 we are about to handle an as-needed lib, or have finished
3544 processing the lib. */
3545
3546 bfd_boolean
3547 _bfd_elf_notice_as_needed (bfd *ibfd,
3548 struct bfd_link_info *info,
3549 enum notice_asneeded_action act)
3550 {
3551 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3552 }
3553
3554 /* Check relocations an ELF object file. */
3555
3556 bfd_boolean
3557 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3558 {
3559 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3560 struct elf_link_hash_table *htab = elf_hash_table (info);
3561
3562 /* If this object is the same format as the output object, and it is
3563 not a shared library, then let the backend look through the
3564 relocs.
3565
3566 This is required to build global offset table entries and to
3567 arrange for dynamic relocs. It is not required for the
3568 particular common case of linking non PIC code, even when linking
3569 against shared libraries, but unfortunately there is no way of
3570 knowing whether an object file has been compiled PIC or not.
3571 Looking through the relocs is not particularly time consuming.
3572 The problem is that we must either (1) keep the relocs in memory,
3573 which causes the linker to require additional runtime memory or
3574 (2) read the relocs twice from the input file, which wastes time.
3575 This would be a good case for using mmap.
3576
3577 I have no idea how to handle linking PIC code into a file of a
3578 different format. It probably can't be done. */
3579 if ((abfd->flags & DYNAMIC) == 0
3580 && is_elf_hash_table (htab)
3581 && bed->check_relocs != NULL
3582 && elf_object_id (abfd) == elf_hash_table_id (htab)
3583 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3584 {
3585 asection *o;
3586
3587 for (o = abfd->sections; o != NULL; o = o->next)
3588 {
3589 Elf_Internal_Rela *internal_relocs;
3590 bfd_boolean ok;
3591
3592 /* Don't check relocations in excluded sections. */
3593 if ((o->flags & SEC_RELOC) == 0
3594 || (o->flags & SEC_EXCLUDE) != 0
3595 || o->reloc_count == 0
3596 || ((info->strip == strip_all || info->strip == strip_debugger)
3597 && (o->flags & SEC_DEBUGGING) != 0)
3598 || bfd_is_abs_section (o->output_section))
3599 continue;
3600
3601 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3602 info->keep_memory);
3603 if (internal_relocs == NULL)
3604 return FALSE;
3605
3606 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3607
3608 if (elf_section_data (o)->relocs != internal_relocs)
3609 free (internal_relocs);
3610
3611 if (! ok)
3612 return FALSE;
3613 }
3614 }
3615
3616 return TRUE;
3617 }
3618
3619 /* Add symbols from an ELF object file to the linker hash table. */
3620
3621 static bfd_boolean
3622 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3623 {
3624 Elf_Internal_Ehdr *ehdr;
3625 Elf_Internal_Shdr *hdr;
3626 size_t symcount;
3627 size_t extsymcount;
3628 size_t extsymoff;
3629 struct elf_link_hash_entry **sym_hash;
3630 bfd_boolean dynamic;
3631 Elf_External_Versym *extversym = NULL;
3632 Elf_External_Versym *ever;
3633 struct elf_link_hash_entry *weaks;
3634 struct elf_link_hash_entry **nondeflt_vers = NULL;
3635 size_t nondeflt_vers_cnt = 0;
3636 Elf_Internal_Sym *isymbuf = NULL;
3637 Elf_Internal_Sym *isym;
3638 Elf_Internal_Sym *isymend;
3639 const struct elf_backend_data *bed;
3640 bfd_boolean add_needed;
3641 struct elf_link_hash_table *htab;
3642 bfd_size_type amt;
3643 void *alloc_mark = NULL;
3644 struct bfd_hash_entry **old_table = NULL;
3645 unsigned int old_size = 0;
3646 unsigned int old_count = 0;
3647 void *old_tab = NULL;
3648 void *old_ent;
3649 struct bfd_link_hash_entry *old_undefs = NULL;
3650 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3651 void *old_strtab = NULL;
3652 size_t tabsize = 0;
3653 asection *s;
3654 bfd_boolean just_syms;
3655
3656 htab = elf_hash_table (info);
3657 bed = get_elf_backend_data (abfd);
3658
3659 if ((abfd->flags & DYNAMIC) == 0)
3660 dynamic = FALSE;
3661 else
3662 {
3663 dynamic = TRUE;
3664
3665 /* You can't use -r against a dynamic object. Also, there's no
3666 hope of using a dynamic object which does not exactly match
3667 the format of the output file. */
3668 if (bfd_link_relocatable (info)
3669 || !is_elf_hash_table (htab)
3670 || info->output_bfd->xvec != abfd->xvec)
3671 {
3672 if (bfd_link_relocatable (info))
3673 bfd_set_error (bfd_error_invalid_operation);
3674 else
3675 bfd_set_error (bfd_error_wrong_format);
3676 goto error_return;
3677 }
3678 }
3679
3680 ehdr = elf_elfheader (abfd);
3681 if (info->warn_alternate_em
3682 && bed->elf_machine_code != ehdr->e_machine
3683 && ((bed->elf_machine_alt1 != 0
3684 && ehdr->e_machine == bed->elf_machine_alt1)
3685 || (bed->elf_machine_alt2 != 0
3686 && ehdr->e_machine == bed->elf_machine_alt2)))
3687 info->callbacks->einfo
3688 /* xgettext:c-format */
3689 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3690 ehdr->e_machine, abfd, bed->elf_machine_code);
3691
3692 /* As a GNU extension, any input sections which are named
3693 .gnu.warning.SYMBOL are treated as warning symbols for the given
3694 symbol. This differs from .gnu.warning sections, which generate
3695 warnings when they are included in an output file. */
3696 /* PR 12761: Also generate this warning when building shared libraries. */
3697 for (s = abfd->sections; s != NULL; s = s->next)
3698 {
3699 const char *name;
3700
3701 name = bfd_get_section_name (abfd, s);
3702 if (CONST_STRNEQ (name, ".gnu.warning."))
3703 {
3704 char *msg;
3705 bfd_size_type sz;
3706
3707 name += sizeof ".gnu.warning." - 1;
3708
3709 /* If this is a shared object, then look up the symbol
3710 in the hash table. If it is there, and it is already
3711 been defined, then we will not be using the entry
3712 from this shared object, so we don't need to warn.
3713 FIXME: If we see the definition in a regular object
3714 later on, we will warn, but we shouldn't. The only
3715 fix is to keep track of what warnings we are supposed
3716 to emit, and then handle them all at the end of the
3717 link. */
3718 if (dynamic)
3719 {
3720 struct elf_link_hash_entry *h;
3721
3722 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3723
3724 /* FIXME: What about bfd_link_hash_common? */
3725 if (h != NULL
3726 && (h->root.type == bfd_link_hash_defined
3727 || h->root.type == bfd_link_hash_defweak))
3728 continue;
3729 }
3730
3731 sz = s->size;
3732 msg = (char *) bfd_alloc (abfd, sz + 1);
3733 if (msg == NULL)
3734 goto error_return;
3735
3736 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3737 goto error_return;
3738
3739 msg[sz] = '\0';
3740
3741 if (! (_bfd_generic_link_add_one_symbol
3742 (info, abfd, name, BSF_WARNING, s, 0, msg,
3743 FALSE, bed->collect, NULL)))
3744 goto error_return;
3745
3746 if (bfd_link_executable (info))
3747 {
3748 /* Clobber the section size so that the warning does
3749 not get copied into the output file. */
3750 s->size = 0;
3751
3752 /* Also set SEC_EXCLUDE, so that symbols defined in
3753 the warning section don't get copied to the output. */
3754 s->flags |= SEC_EXCLUDE;
3755 }
3756 }
3757 }
3758
3759 just_syms = ((s = abfd->sections) != NULL
3760 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3761
3762 add_needed = TRUE;
3763 if (! dynamic)
3764 {
3765 /* If we are creating a shared library, create all the dynamic
3766 sections immediately. We need to attach them to something,
3767 so we attach them to this BFD, provided it is the right
3768 format and is not from ld --just-symbols. Always create the
3769 dynamic sections for -E/--dynamic-list. FIXME: If there
3770 are no input BFD's of the same format as the output, we can't
3771 make a shared library. */
3772 if (!just_syms
3773 && (bfd_link_pic (info)
3774 || (!bfd_link_relocatable (info)
3775 && (info->export_dynamic || info->dynamic)))
3776 && is_elf_hash_table (htab)
3777 && info->output_bfd->xvec == abfd->xvec
3778 && !htab->dynamic_sections_created)
3779 {
3780 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3781 goto error_return;
3782 }
3783 }
3784 else if (!is_elf_hash_table (htab))
3785 goto error_return;
3786 else
3787 {
3788 const char *soname = NULL;
3789 char *audit = NULL;
3790 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3791 int ret;
3792
3793 /* ld --just-symbols and dynamic objects don't mix very well.
3794 ld shouldn't allow it. */
3795 if (just_syms)
3796 abort ();
3797
3798 /* If this dynamic lib was specified on the command line with
3799 --as-needed in effect, then we don't want to add a DT_NEEDED
3800 tag unless the lib is actually used. Similary for libs brought
3801 in by another lib's DT_NEEDED. When --no-add-needed is used
3802 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3803 any dynamic library in DT_NEEDED tags in the dynamic lib at
3804 all. */
3805 add_needed = (elf_dyn_lib_class (abfd)
3806 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3807 | DYN_NO_NEEDED)) == 0;
3808
3809 s = bfd_get_section_by_name (abfd, ".dynamic");
3810 if (s != NULL)
3811 {
3812 bfd_byte *dynbuf;
3813 bfd_byte *extdyn;
3814 unsigned int elfsec;
3815 unsigned long shlink;
3816
3817 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3818 {
3819 error_free_dyn:
3820 free (dynbuf);
3821 goto error_return;
3822 }
3823
3824 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3825 if (elfsec == SHN_BAD)
3826 goto error_free_dyn;
3827 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3828
3829 for (extdyn = dynbuf;
3830 extdyn < dynbuf + s->size;
3831 extdyn += bed->s->sizeof_dyn)
3832 {
3833 Elf_Internal_Dyn dyn;
3834
3835 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3836 if (dyn.d_tag == DT_SONAME)
3837 {
3838 unsigned int tagv = dyn.d_un.d_val;
3839 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3840 if (soname == NULL)
3841 goto error_free_dyn;
3842 }
3843 if (dyn.d_tag == DT_NEEDED)
3844 {
3845 struct bfd_link_needed_list *n, **pn;
3846 char *fnm, *anm;
3847 unsigned int tagv = dyn.d_un.d_val;
3848
3849 amt = sizeof (struct bfd_link_needed_list);
3850 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3851 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3852 if (n == NULL || fnm == NULL)
3853 goto error_free_dyn;
3854 amt = strlen (fnm) + 1;
3855 anm = (char *) bfd_alloc (abfd, amt);
3856 if (anm == NULL)
3857 goto error_free_dyn;
3858 memcpy (anm, fnm, amt);
3859 n->name = anm;
3860 n->by = abfd;
3861 n->next = NULL;
3862 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3863 ;
3864 *pn = n;
3865 }
3866 if (dyn.d_tag == DT_RUNPATH)
3867 {
3868 struct bfd_link_needed_list *n, **pn;
3869 char *fnm, *anm;
3870 unsigned int tagv = dyn.d_un.d_val;
3871
3872 amt = sizeof (struct bfd_link_needed_list);
3873 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3874 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3875 if (n == NULL || fnm == NULL)
3876 goto error_free_dyn;
3877 amt = strlen (fnm) + 1;
3878 anm = (char *) bfd_alloc (abfd, amt);
3879 if (anm == NULL)
3880 goto error_free_dyn;
3881 memcpy (anm, fnm, amt);
3882 n->name = anm;
3883 n->by = abfd;
3884 n->next = NULL;
3885 for (pn = & runpath;
3886 *pn != NULL;
3887 pn = &(*pn)->next)
3888 ;
3889 *pn = n;
3890 }
3891 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3892 if (!runpath && dyn.d_tag == DT_RPATH)
3893 {
3894 struct bfd_link_needed_list *n, **pn;
3895 char *fnm, *anm;
3896 unsigned int tagv = dyn.d_un.d_val;
3897
3898 amt = sizeof (struct bfd_link_needed_list);
3899 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3900 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3901 if (n == NULL || fnm == NULL)
3902 goto error_free_dyn;
3903 amt = strlen (fnm) + 1;
3904 anm = (char *) bfd_alloc (abfd, amt);
3905 if (anm == NULL)
3906 goto error_free_dyn;
3907 memcpy (anm, fnm, amt);
3908 n->name = anm;
3909 n->by = abfd;
3910 n->next = NULL;
3911 for (pn = & rpath;
3912 *pn != NULL;
3913 pn = &(*pn)->next)
3914 ;
3915 *pn = n;
3916 }
3917 if (dyn.d_tag == DT_AUDIT)
3918 {
3919 unsigned int tagv = dyn.d_un.d_val;
3920 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3921 }
3922 }
3923
3924 free (dynbuf);
3925 }
3926
3927 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3928 frees all more recently bfd_alloc'd blocks as well. */
3929 if (runpath)
3930 rpath = runpath;
3931
3932 if (rpath)
3933 {
3934 struct bfd_link_needed_list **pn;
3935 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3936 ;
3937 *pn = rpath;
3938 }
3939
3940 /* We do not want to include any of the sections in a dynamic
3941 object in the output file. We hack by simply clobbering the
3942 list of sections in the BFD. This could be handled more
3943 cleanly by, say, a new section flag; the existing
3944 SEC_NEVER_LOAD flag is not the one we want, because that one
3945 still implies that the section takes up space in the output
3946 file. */
3947 bfd_section_list_clear (abfd);
3948
3949 /* Find the name to use in a DT_NEEDED entry that refers to this
3950 object. If the object has a DT_SONAME entry, we use it.
3951 Otherwise, if the generic linker stuck something in
3952 elf_dt_name, we use that. Otherwise, we just use the file
3953 name. */
3954 if (soname == NULL || *soname == '\0')
3955 {
3956 soname = elf_dt_name (abfd);
3957 if (soname == NULL || *soname == '\0')
3958 soname = bfd_get_filename (abfd);
3959 }
3960
3961 /* Save the SONAME because sometimes the linker emulation code
3962 will need to know it. */
3963 elf_dt_name (abfd) = soname;
3964
3965 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3966 if (ret < 0)
3967 goto error_return;
3968
3969 /* If we have already included this dynamic object in the
3970 link, just ignore it. There is no reason to include a
3971 particular dynamic object more than once. */
3972 if (ret > 0)
3973 return TRUE;
3974
3975 /* Save the DT_AUDIT entry for the linker emulation code. */
3976 elf_dt_audit (abfd) = audit;
3977 }
3978
3979 /* If this is a dynamic object, we always link against the .dynsym
3980 symbol table, not the .symtab symbol table. The dynamic linker
3981 will only see the .dynsym symbol table, so there is no reason to
3982 look at .symtab for a dynamic object. */
3983
3984 if (! dynamic || elf_dynsymtab (abfd) == 0)
3985 hdr = &elf_tdata (abfd)->symtab_hdr;
3986 else
3987 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3988
3989 symcount = hdr->sh_size / bed->s->sizeof_sym;
3990
3991 /* The sh_info field of the symtab header tells us where the
3992 external symbols start. We don't care about the local symbols at
3993 this point. */
3994 if (elf_bad_symtab (abfd))
3995 {
3996 extsymcount = symcount;
3997 extsymoff = 0;
3998 }
3999 else
4000 {
4001 extsymcount = symcount - hdr->sh_info;
4002 extsymoff = hdr->sh_info;
4003 }
4004
4005 sym_hash = elf_sym_hashes (abfd);
4006 if (extsymcount != 0)
4007 {
4008 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4009 NULL, NULL, NULL);
4010 if (isymbuf == NULL)
4011 goto error_return;
4012
4013 if (sym_hash == NULL)
4014 {
4015 /* We store a pointer to the hash table entry for each
4016 external symbol. */
4017 amt = extsymcount;
4018 amt *= sizeof (struct elf_link_hash_entry *);
4019 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4020 if (sym_hash == NULL)
4021 goto error_free_sym;
4022 elf_sym_hashes (abfd) = sym_hash;
4023 }
4024 }
4025
4026 if (dynamic)
4027 {
4028 /* Read in any version definitions. */
4029 if (!_bfd_elf_slurp_version_tables (abfd,
4030 info->default_imported_symver))
4031 goto error_free_sym;
4032
4033 /* Read in the symbol versions, but don't bother to convert them
4034 to internal format. */
4035 if (elf_dynversym (abfd) != 0)
4036 {
4037 Elf_Internal_Shdr *versymhdr;
4038
4039 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4040 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4041 if (extversym == NULL)
4042 goto error_free_sym;
4043 amt = versymhdr->sh_size;
4044 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4045 || bfd_bread (extversym, amt, abfd) != amt)
4046 goto error_free_vers;
4047 }
4048 }
4049
4050 /* If we are loading an as-needed shared lib, save the symbol table
4051 state before we start adding symbols. If the lib turns out
4052 to be unneeded, restore the state. */
4053 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4054 {
4055 unsigned int i;
4056 size_t entsize;
4057
4058 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4059 {
4060 struct bfd_hash_entry *p;
4061 struct elf_link_hash_entry *h;
4062
4063 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4064 {
4065 h = (struct elf_link_hash_entry *) p;
4066 entsize += htab->root.table.entsize;
4067 if (h->root.type == bfd_link_hash_warning)
4068 entsize += htab->root.table.entsize;
4069 }
4070 }
4071
4072 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4073 old_tab = bfd_malloc (tabsize + entsize);
4074 if (old_tab == NULL)
4075 goto error_free_vers;
4076
4077 /* Remember the current objalloc pointer, so that all mem for
4078 symbols added can later be reclaimed. */
4079 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4080 if (alloc_mark == NULL)
4081 goto error_free_vers;
4082
4083 /* Make a special call to the linker "notice" function to
4084 tell it that we are about to handle an as-needed lib. */
4085 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4086 goto error_free_vers;
4087
4088 /* Clone the symbol table. Remember some pointers into the
4089 symbol table, and dynamic symbol count. */
4090 old_ent = (char *) old_tab + tabsize;
4091 memcpy (old_tab, htab->root.table.table, tabsize);
4092 old_undefs = htab->root.undefs;
4093 old_undefs_tail = htab->root.undefs_tail;
4094 old_table = htab->root.table.table;
4095 old_size = htab->root.table.size;
4096 old_count = htab->root.table.count;
4097 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4098 if (old_strtab == NULL)
4099 goto error_free_vers;
4100
4101 for (i = 0; i < htab->root.table.size; i++)
4102 {
4103 struct bfd_hash_entry *p;
4104 struct elf_link_hash_entry *h;
4105
4106 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4107 {
4108 memcpy (old_ent, p, htab->root.table.entsize);
4109 old_ent = (char *) old_ent + htab->root.table.entsize;
4110 h = (struct elf_link_hash_entry *) p;
4111 if (h->root.type == bfd_link_hash_warning)
4112 {
4113 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4114 old_ent = (char *) old_ent + htab->root.table.entsize;
4115 }
4116 }
4117 }
4118 }
4119
4120 weaks = NULL;
4121 ever = extversym != NULL ? extversym + extsymoff : NULL;
4122 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4123 isym < isymend;
4124 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4125 {
4126 int bind;
4127 bfd_vma value;
4128 asection *sec, *new_sec;
4129 flagword flags;
4130 const char *name;
4131 struct elf_link_hash_entry *h;
4132 struct elf_link_hash_entry *hi;
4133 bfd_boolean definition;
4134 bfd_boolean size_change_ok;
4135 bfd_boolean type_change_ok;
4136 bfd_boolean new_weakdef;
4137 bfd_boolean new_weak;
4138 bfd_boolean old_weak;
4139 bfd_boolean override;
4140 bfd_boolean common;
4141 bfd_boolean discarded;
4142 unsigned int old_alignment;
4143 bfd *old_bfd;
4144 bfd_boolean matched;
4145
4146 override = FALSE;
4147
4148 flags = BSF_NO_FLAGS;
4149 sec = NULL;
4150 value = isym->st_value;
4151 common = bed->common_definition (isym);
4152 discarded = FALSE;
4153
4154 bind = ELF_ST_BIND (isym->st_info);
4155 switch (bind)
4156 {
4157 case STB_LOCAL:
4158 /* This should be impossible, since ELF requires that all
4159 global symbols follow all local symbols, and that sh_info
4160 point to the first global symbol. Unfortunately, Irix 5
4161 screws this up. */
4162 continue;
4163
4164 case STB_GLOBAL:
4165 if (isym->st_shndx != SHN_UNDEF && !common)
4166 flags = BSF_GLOBAL;
4167 break;
4168
4169 case STB_WEAK:
4170 flags = BSF_WEAK;
4171 break;
4172
4173 case STB_GNU_UNIQUE:
4174 flags = BSF_GNU_UNIQUE;
4175 break;
4176
4177 default:
4178 /* Leave it up to the processor backend. */
4179 break;
4180 }
4181
4182 if (isym->st_shndx == SHN_UNDEF)
4183 sec = bfd_und_section_ptr;
4184 else if (isym->st_shndx == SHN_ABS)
4185 sec = bfd_abs_section_ptr;
4186 else if (isym->st_shndx == SHN_COMMON)
4187 {
4188 sec = bfd_com_section_ptr;
4189 /* What ELF calls the size we call the value. What ELF
4190 calls the value we call the alignment. */
4191 value = isym->st_size;
4192 }
4193 else
4194 {
4195 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4196 if (sec == NULL)
4197 sec = bfd_abs_section_ptr;
4198 else if (discarded_section (sec))
4199 {
4200 /* Symbols from discarded section are undefined. We keep
4201 its visibility. */
4202 sec = bfd_und_section_ptr;
4203 discarded = TRUE;
4204 isym->st_shndx = SHN_UNDEF;
4205 }
4206 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4207 value -= sec->vma;
4208 }
4209
4210 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4211 isym->st_name);
4212 if (name == NULL)
4213 goto error_free_vers;
4214
4215 if (isym->st_shndx == SHN_COMMON
4216 && (abfd->flags & BFD_PLUGIN) != 0)
4217 {
4218 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4219
4220 if (xc == NULL)
4221 {
4222 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4223 | SEC_EXCLUDE);
4224 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4225 if (xc == NULL)
4226 goto error_free_vers;
4227 }
4228 sec = xc;
4229 }
4230 else if (isym->st_shndx == SHN_COMMON
4231 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4232 && !bfd_link_relocatable (info))
4233 {
4234 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4235
4236 if (tcomm == NULL)
4237 {
4238 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4239 | SEC_LINKER_CREATED);
4240 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4241 if (tcomm == NULL)
4242 goto error_free_vers;
4243 }
4244 sec = tcomm;
4245 }
4246 else if (bed->elf_add_symbol_hook)
4247 {
4248 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4249 &sec, &value))
4250 goto error_free_vers;
4251
4252 /* The hook function sets the name to NULL if this symbol
4253 should be skipped for some reason. */
4254 if (name == NULL)
4255 continue;
4256 }
4257
4258 /* Sanity check that all possibilities were handled. */
4259 if (sec == NULL)
4260 {
4261 bfd_set_error (bfd_error_bad_value);
4262 goto error_free_vers;
4263 }
4264
4265 /* Silently discard TLS symbols from --just-syms. There's
4266 no way to combine a static TLS block with a new TLS block
4267 for this executable. */
4268 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4269 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4270 continue;
4271
4272 if (bfd_is_und_section (sec)
4273 || bfd_is_com_section (sec))
4274 definition = FALSE;
4275 else
4276 definition = TRUE;
4277
4278 size_change_ok = FALSE;
4279 type_change_ok = bed->type_change_ok;
4280 old_weak = FALSE;
4281 matched = FALSE;
4282 old_alignment = 0;
4283 old_bfd = NULL;
4284 new_sec = sec;
4285
4286 if (is_elf_hash_table (htab))
4287 {
4288 Elf_Internal_Versym iver;
4289 unsigned int vernum = 0;
4290 bfd_boolean skip;
4291
4292 if (ever == NULL)
4293 {
4294 if (info->default_imported_symver)
4295 /* Use the default symbol version created earlier. */
4296 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4297 else
4298 iver.vs_vers = 0;
4299 }
4300 else
4301 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4302
4303 vernum = iver.vs_vers & VERSYM_VERSION;
4304
4305 /* If this is a hidden symbol, or if it is not version
4306 1, we append the version name to the symbol name.
4307 However, we do not modify a non-hidden absolute symbol
4308 if it is not a function, because it might be the version
4309 symbol itself. FIXME: What if it isn't? */
4310 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4311 || (vernum > 1
4312 && (!bfd_is_abs_section (sec)
4313 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4314 {
4315 const char *verstr;
4316 size_t namelen, verlen, newlen;
4317 char *newname, *p;
4318
4319 if (isym->st_shndx != SHN_UNDEF)
4320 {
4321 if (vernum > elf_tdata (abfd)->cverdefs)
4322 verstr = NULL;
4323 else if (vernum > 1)
4324 verstr =
4325 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4326 else
4327 verstr = "";
4328
4329 if (verstr == NULL)
4330 {
4331 _bfd_error_handler
4332 /* xgettext:c-format */
4333 (_("%B: %s: invalid version %u (max %d)"),
4334 abfd, name, vernum,
4335 elf_tdata (abfd)->cverdefs);
4336 bfd_set_error (bfd_error_bad_value);
4337 goto error_free_vers;
4338 }
4339 }
4340 else
4341 {
4342 /* We cannot simply test for the number of
4343 entries in the VERNEED section since the
4344 numbers for the needed versions do not start
4345 at 0. */
4346 Elf_Internal_Verneed *t;
4347
4348 verstr = NULL;
4349 for (t = elf_tdata (abfd)->verref;
4350 t != NULL;
4351 t = t->vn_nextref)
4352 {
4353 Elf_Internal_Vernaux *a;
4354
4355 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4356 {
4357 if (a->vna_other == vernum)
4358 {
4359 verstr = a->vna_nodename;
4360 break;
4361 }
4362 }
4363 if (a != NULL)
4364 break;
4365 }
4366 if (verstr == NULL)
4367 {
4368 _bfd_error_handler
4369 /* xgettext:c-format */
4370 (_("%B: %s: invalid needed version %d"),
4371 abfd, name, vernum);
4372 bfd_set_error (bfd_error_bad_value);
4373 goto error_free_vers;
4374 }
4375 }
4376
4377 namelen = strlen (name);
4378 verlen = strlen (verstr);
4379 newlen = namelen + verlen + 2;
4380 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4381 && isym->st_shndx != SHN_UNDEF)
4382 ++newlen;
4383
4384 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4385 if (newname == NULL)
4386 goto error_free_vers;
4387 memcpy (newname, name, namelen);
4388 p = newname + namelen;
4389 *p++ = ELF_VER_CHR;
4390 /* If this is a defined non-hidden version symbol,
4391 we add another @ to the name. This indicates the
4392 default version of the symbol. */
4393 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4394 && isym->st_shndx != SHN_UNDEF)
4395 *p++ = ELF_VER_CHR;
4396 memcpy (p, verstr, verlen + 1);
4397
4398 name = newname;
4399 }
4400
4401 /* If this symbol has default visibility and the user has
4402 requested we not re-export it, then mark it as hidden. */
4403 if (!bfd_is_und_section (sec)
4404 && !dynamic
4405 && abfd->no_export
4406 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4407 isym->st_other = (STV_HIDDEN
4408 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4409
4410 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4411 sym_hash, &old_bfd, &old_weak,
4412 &old_alignment, &skip, &override,
4413 &type_change_ok, &size_change_ok,
4414 &matched))
4415 goto error_free_vers;
4416
4417 if (skip)
4418 continue;
4419
4420 /* Override a definition only if the new symbol matches the
4421 existing one. */
4422 if (override && matched)
4423 definition = FALSE;
4424
4425 h = *sym_hash;
4426 while (h->root.type == bfd_link_hash_indirect
4427 || h->root.type == bfd_link_hash_warning)
4428 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4429
4430 if (elf_tdata (abfd)->verdef != NULL
4431 && vernum > 1
4432 && definition)
4433 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4434 }
4435
4436 if (! (_bfd_generic_link_add_one_symbol
4437 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4438 (struct bfd_link_hash_entry **) sym_hash)))
4439 goto error_free_vers;
4440
4441 if ((flags & BSF_GNU_UNIQUE)
4442 && (abfd->flags & DYNAMIC) == 0
4443 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4444 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique;
4445
4446 h = *sym_hash;
4447 /* We need to make sure that indirect symbol dynamic flags are
4448 updated. */
4449 hi = h;
4450 while (h->root.type == bfd_link_hash_indirect
4451 || h->root.type == bfd_link_hash_warning)
4452 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4453
4454 /* Setting the index to -3 tells elf_link_output_extsym that
4455 this symbol is defined in a discarded section. */
4456 if (discarded)
4457 h->indx = -3;
4458
4459 *sym_hash = h;
4460
4461 new_weak = (flags & BSF_WEAK) != 0;
4462 new_weakdef = FALSE;
4463 if (dynamic
4464 && definition
4465 && new_weak
4466 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4467 && is_elf_hash_table (htab)
4468 && h->u.weakdef == NULL)
4469 {
4470 /* Keep a list of all weak defined non function symbols from
4471 a dynamic object, using the weakdef field. Later in this
4472 function we will set the weakdef field to the correct
4473 value. We only put non-function symbols from dynamic
4474 objects on this list, because that happens to be the only
4475 time we need to know the normal symbol corresponding to a
4476 weak symbol, and the information is time consuming to
4477 figure out. If the weakdef field is not already NULL,
4478 then this symbol was already defined by some previous
4479 dynamic object, and we will be using that previous
4480 definition anyhow. */
4481
4482 h->u.weakdef = weaks;
4483 weaks = h;
4484 new_weakdef = TRUE;
4485 }
4486
4487 /* Set the alignment of a common symbol. */
4488 if ((common || bfd_is_com_section (sec))
4489 && h->root.type == bfd_link_hash_common)
4490 {
4491 unsigned int align;
4492
4493 if (common)
4494 align = bfd_log2 (isym->st_value);
4495 else
4496 {
4497 /* The new symbol is a common symbol in a shared object.
4498 We need to get the alignment from the section. */
4499 align = new_sec->alignment_power;
4500 }
4501 if (align > old_alignment)
4502 h->root.u.c.p->alignment_power = align;
4503 else
4504 h->root.u.c.p->alignment_power = old_alignment;
4505 }
4506
4507 if (is_elf_hash_table (htab))
4508 {
4509 /* Set a flag in the hash table entry indicating the type of
4510 reference or definition we just found. A dynamic symbol
4511 is one which is referenced or defined by both a regular
4512 object and a shared object. */
4513 bfd_boolean dynsym = FALSE;
4514
4515 /* Plugin symbols aren't normal. Don't set def_regular or
4516 ref_regular for them, or make them dynamic. */
4517 if ((abfd->flags & BFD_PLUGIN) != 0)
4518 ;
4519 else if (! dynamic)
4520 {
4521 if (! definition)
4522 {
4523 h->ref_regular = 1;
4524 if (bind != STB_WEAK)
4525 h->ref_regular_nonweak = 1;
4526 }
4527 else
4528 {
4529 h->def_regular = 1;
4530 if (h->def_dynamic)
4531 {
4532 h->def_dynamic = 0;
4533 h->ref_dynamic = 1;
4534 }
4535 }
4536
4537 /* If the indirect symbol has been forced local, don't
4538 make the real symbol dynamic. */
4539 if ((h == hi || !hi->forced_local)
4540 && (bfd_link_dll (info)
4541 || h->def_dynamic
4542 || h->ref_dynamic))
4543 dynsym = TRUE;
4544 }
4545 else
4546 {
4547 if (! definition)
4548 {
4549 h->ref_dynamic = 1;
4550 hi->ref_dynamic = 1;
4551 }
4552 else
4553 {
4554 h->def_dynamic = 1;
4555 hi->def_dynamic = 1;
4556 }
4557
4558 /* If the indirect symbol has been forced local, don't
4559 make the real symbol dynamic. */
4560 if ((h == hi || !hi->forced_local)
4561 && (h->def_regular
4562 || h->ref_regular
4563 || (h->u.weakdef != NULL
4564 && ! new_weakdef
4565 && h->u.weakdef->dynindx != -1)))
4566 dynsym = TRUE;
4567 }
4568
4569 /* Check to see if we need to add an indirect symbol for
4570 the default name. */
4571 if (definition
4572 || (!override && h->root.type == bfd_link_hash_common))
4573 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4574 sec, value, &old_bfd, &dynsym))
4575 goto error_free_vers;
4576
4577 /* Check the alignment when a common symbol is involved. This
4578 can change when a common symbol is overridden by a normal
4579 definition or a common symbol is ignored due to the old
4580 normal definition. We need to make sure the maximum
4581 alignment is maintained. */
4582 if ((old_alignment || common)
4583 && h->root.type != bfd_link_hash_common)
4584 {
4585 unsigned int common_align;
4586 unsigned int normal_align;
4587 unsigned int symbol_align;
4588 bfd *normal_bfd;
4589 bfd *common_bfd;
4590
4591 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4592 || h->root.type == bfd_link_hash_defweak);
4593
4594 symbol_align = ffs (h->root.u.def.value) - 1;
4595 if (h->root.u.def.section->owner != NULL
4596 && (h->root.u.def.section->owner->flags
4597 & (DYNAMIC | BFD_PLUGIN)) == 0)
4598 {
4599 normal_align = h->root.u.def.section->alignment_power;
4600 if (normal_align > symbol_align)
4601 normal_align = symbol_align;
4602 }
4603 else
4604 normal_align = symbol_align;
4605
4606 if (old_alignment)
4607 {
4608 common_align = old_alignment;
4609 common_bfd = old_bfd;
4610 normal_bfd = abfd;
4611 }
4612 else
4613 {
4614 common_align = bfd_log2 (isym->st_value);
4615 common_bfd = abfd;
4616 normal_bfd = old_bfd;
4617 }
4618
4619 if (normal_align < common_align)
4620 {
4621 /* PR binutils/2735 */
4622 if (normal_bfd == NULL)
4623 _bfd_error_handler
4624 /* xgettext:c-format */
4625 (_("Warning: alignment %u of common symbol `%s' in %B is"
4626 " greater than the alignment (%u) of its section %A"),
4627 common_bfd, h->root.u.def.section,
4628 1 << common_align, name, 1 << normal_align);
4629 else
4630 _bfd_error_handler
4631 /* xgettext:c-format */
4632 (_("Warning: alignment %u of symbol `%s' in %B"
4633 " is smaller than %u in %B"),
4634 normal_bfd, common_bfd,
4635 1 << normal_align, name, 1 << common_align);
4636 }
4637 }
4638
4639 /* Remember the symbol size if it isn't undefined. */
4640 if (isym->st_size != 0
4641 && isym->st_shndx != SHN_UNDEF
4642 && (definition || h->size == 0))
4643 {
4644 if (h->size != 0
4645 && h->size != isym->st_size
4646 && ! size_change_ok)
4647 _bfd_error_handler
4648 /* xgettext:c-format */
4649 (_("Warning: size of symbol `%s' changed"
4650 " from %lu in %B to %lu in %B"),
4651 old_bfd, abfd,
4652 name, (unsigned long) h->size,
4653 (unsigned long) isym->st_size);
4654
4655 h->size = isym->st_size;
4656 }
4657
4658 /* If this is a common symbol, then we always want H->SIZE
4659 to be the size of the common symbol. The code just above
4660 won't fix the size if a common symbol becomes larger. We
4661 don't warn about a size change here, because that is
4662 covered by --warn-common. Allow changes between different
4663 function types. */
4664 if (h->root.type == bfd_link_hash_common)
4665 h->size = h->root.u.c.size;
4666
4667 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4668 && ((definition && !new_weak)
4669 || (old_weak && h->root.type == bfd_link_hash_common)
4670 || h->type == STT_NOTYPE))
4671 {
4672 unsigned int type = ELF_ST_TYPE (isym->st_info);
4673
4674 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4675 symbol. */
4676 if (type == STT_GNU_IFUNC
4677 && (abfd->flags & DYNAMIC) != 0)
4678 type = STT_FUNC;
4679
4680 if (h->type != type)
4681 {
4682 if (h->type != STT_NOTYPE && ! type_change_ok)
4683 /* xgettext:c-format */
4684 _bfd_error_handler
4685 (_("Warning: type of symbol `%s' changed"
4686 " from %d to %d in %B"),
4687 abfd, name, h->type, type);
4688
4689 h->type = type;
4690 }
4691 }
4692
4693 /* Merge st_other field. */
4694 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4695
4696 /* We don't want to make debug symbol dynamic. */
4697 if (definition
4698 && (sec->flags & SEC_DEBUGGING)
4699 && !bfd_link_relocatable (info))
4700 dynsym = FALSE;
4701
4702 /* Nor should we make plugin symbols dynamic. */
4703 if ((abfd->flags & BFD_PLUGIN) != 0)
4704 dynsym = FALSE;
4705
4706 if (definition)
4707 {
4708 h->target_internal = isym->st_target_internal;
4709 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4710 }
4711
4712 if (definition && !dynamic)
4713 {
4714 char *p = strchr (name, ELF_VER_CHR);
4715 if (p != NULL && p[1] != ELF_VER_CHR)
4716 {
4717 /* Queue non-default versions so that .symver x, x@FOO
4718 aliases can be checked. */
4719 if (!nondeflt_vers)
4720 {
4721 amt = ((isymend - isym + 1)
4722 * sizeof (struct elf_link_hash_entry *));
4723 nondeflt_vers
4724 = (struct elf_link_hash_entry **) bfd_malloc (amt);
4725 if (!nondeflt_vers)
4726 goto error_free_vers;
4727 }
4728 nondeflt_vers[nondeflt_vers_cnt++] = h;
4729 }
4730 }
4731
4732 if (dynsym && h->dynindx == -1)
4733 {
4734 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4735 goto error_free_vers;
4736 if (h->u.weakdef != NULL
4737 && ! new_weakdef
4738 && h->u.weakdef->dynindx == -1)
4739 {
4740 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4741 goto error_free_vers;
4742 }
4743 }
4744 else if (h->dynindx != -1)
4745 /* If the symbol already has a dynamic index, but
4746 visibility says it should not be visible, turn it into
4747 a local symbol. */
4748 switch (ELF_ST_VISIBILITY (h->other))
4749 {
4750 case STV_INTERNAL:
4751 case STV_HIDDEN:
4752 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4753 dynsym = FALSE;
4754 break;
4755 }
4756
4757 /* Don't add DT_NEEDED for references from the dummy bfd nor
4758 for unmatched symbol. */
4759 if (!add_needed
4760 && matched
4761 && definition
4762 && ((dynsym
4763 && h->ref_regular_nonweak
4764 && (old_bfd == NULL
4765 || (old_bfd->flags & BFD_PLUGIN) == 0))
4766 || (h->ref_dynamic_nonweak
4767 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4768 && !on_needed_list (elf_dt_name (abfd),
4769 htab->needed, NULL))))
4770 {
4771 int ret;
4772 const char *soname = elf_dt_name (abfd);
4773
4774 info->callbacks->minfo ("%!", soname, old_bfd,
4775 h->root.root.string);
4776
4777 /* A symbol from a library loaded via DT_NEEDED of some
4778 other library is referenced by a regular object.
4779 Add a DT_NEEDED entry for it. Issue an error if
4780 --no-add-needed is used and the reference was not
4781 a weak one. */
4782 if (old_bfd != NULL
4783 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4784 {
4785 _bfd_error_handler
4786 /* xgettext:c-format */
4787 (_("%B: undefined reference to symbol '%s'"),
4788 old_bfd, name);
4789 bfd_set_error (bfd_error_missing_dso);
4790 goto error_free_vers;
4791 }
4792
4793 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4794 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
4795
4796 add_needed = TRUE;
4797 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4798 if (ret < 0)
4799 goto error_free_vers;
4800
4801 BFD_ASSERT (ret == 0);
4802 }
4803 }
4804 }
4805
4806 if (extversym != NULL)
4807 {
4808 free (extversym);
4809 extversym = NULL;
4810 }
4811
4812 if (isymbuf != NULL)
4813 {
4814 free (isymbuf);
4815 isymbuf = NULL;
4816 }
4817
4818 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4819 {
4820 unsigned int i;
4821
4822 /* Restore the symbol table. */
4823 old_ent = (char *) old_tab + tabsize;
4824 memset (elf_sym_hashes (abfd), 0,
4825 extsymcount * sizeof (struct elf_link_hash_entry *));
4826 htab->root.table.table = old_table;
4827 htab->root.table.size = old_size;
4828 htab->root.table.count = old_count;
4829 memcpy (htab->root.table.table, old_tab, tabsize);
4830 htab->root.undefs = old_undefs;
4831 htab->root.undefs_tail = old_undefs_tail;
4832 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
4833 free (old_strtab);
4834 old_strtab = NULL;
4835 for (i = 0; i < htab->root.table.size; i++)
4836 {
4837 struct bfd_hash_entry *p;
4838 struct elf_link_hash_entry *h;
4839 bfd_size_type size;
4840 unsigned int alignment_power;
4841
4842 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4843 {
4844 h = (struct elf_link_hash_entry *) p;
4845 if (h->root.type == bfd_link_hash_warning)
4846 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4847
4848 /* Preserve the maximum alignment and size for common
4849 symbols even if this dynamic lib isn't on DT_NEEDED
4850 since it can still be loaded at run time by another
4851 dynamic lib. */
4852 if (h->root.type == bfd_link_hash_common)
4853 {
4854 size = h->root.u.c.size;
4855 alignment_power = h->root.u.c.p->alignment_power;
4856 }
4857 else
4858 {
4859 size = 0;
4860 alignment_power = 0;
4861 }
4862 memcpy (p, old_ent, htab->root.table.entsize);
4863 old_ent = (char *) old_ent + htab->root.table.entsize;
4864 h = (struct elf_link_hash_entry *) p;
4865 if (h->root.type == bfd_link_hash_warning)
4866 {
4867 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4868 old_ent = (char *) old_ent + htab->root.table.entsize;
4869 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4870 }
4871 if (h->root.type == bfd_link_hash_common)
4872 {
4873 if (size > h->root.u.c.size)
4874 h->root.u.c.size = size;
4875 if (alignment_power > h->root.u.c.p->alignment_power)
4876 h->root.u.c.p->alignment_power = alignment_power;
4877 }
4878 }
4879 }
4880
4881 /* Make a special call to the linker "notice" function to
4882 tell it that symbols added for crefs may need to be removed. */
4883 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
4884 goto error_free_vers;
4885
4886 free (old_tab);
4887 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4888 alloc_mark);
4889 if (nondeflt_vers != NULL)
4890 free (nondeflt_vers);
4891 return TRUE;
4892 }
4893
4894 if (old_tab != NULL)
4895 {
4896 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
4897 goto error_free_vers;
4898 free (old_tab);
4899 old_tab = NULL;
4900 }
4901
4902 /* Now that all the symbols from this input file are created, if
4903 not performing a relocatable link, handle .symver foo, foo@BAR
4904 such that any relocs against foo become foo@BAR. */
4905 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4906 {
4907 size_t cnt, symidx;
4908
4909 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4910 {
4911 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4912 char *shortname, *p;
4913
4914 p = strchr (h->root.root.string, ELF_VER_CHR);
4915 if (p == NULL
4916 || (h->root.type != bfd_link_hash_defined
4917 && h->root.type != bfd_link_hash_defweak))
4918 continue;
4919
4920 amt = p - h->root.root.string;
4921 shortname = (char *) bfd_malloc (amt + 1);
4922 if (!shortname)
4923 goto error_free_vers;
4924 memcpy (shortname, h->root.root.string, amt);
4925 shortname[amt] = '\0';
4926
4927 hi = (struct elf_link_hash_entry *)
4928 bfd_link_hash_lookup (&htab->root, shortname,
4929 FALSE, FALSE, FALSE);
4930 if (hi != NULL
4931 && hi->root.type == h->root.type
4932 && hi->root.u.def.value == h->root.u.def.value
4933 && hi->root.u.def.section == h->root.u.def.section)
4934 {
4935 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4936 hi->root.type = bfd_link_hash_indirect;
4937 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4938 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4939 sym_hash = elf_sym_hashes (abfd);
4940 if (sym_hash)
4941 for (symidx = 0; symidx < extsymcount; ++symidx)
4942 if (sym_hash[symidx] == hi)
4943 {
4944 sym_hash[symidx] = h;
4945 break;
4946 }
4947 }
4948 free (shortname);
4949 }
4950 free (nondeflt_vers);
4951 nondeflt_vers = NULL;
4952 }
4953
4954 /* Now set the weakdefs field correctly for all the weak defined
4955 symbols we found. The only way to do this is to search all the
4956 symbols. Since we only need the information for non functions in
4957 dynamic objects, that's the only time we actually put anything on
4958 the list WEAKS. We need this information so that if a regular
4959 object refers to a symbol defined weakly in a dynamic object, the
4960 real symbol in the dynamic object is also put in the dynamic
4961 symbols; we also must arrange for both symbols to point to the
4962 same memory location. We could handle the general case of symbol
4963 aliasing, but a general symbol alias can only be generated in
4964 assembler code, handling it correctly would be very time
4965 consuming, and other ELF linkers don't handle general aliasing
4966 either. */
4967 if (weaks != NULL)
4968 {
4969 struct elf_link_hash_entry **hpp;
4970 struct elf_link_hash_entry **hppend;
4971 struct elf_link_hash_entry **sorted_sym_hash;
4972 struct elf_link_hash_entry *h;
4973 size_t sym_count;
4974
4975 /* Since we have to search the whole symbol list for each weak
4976 defined symbol, search time for N weak defined symbols will be
4977 O(N^2). Binary search will cut it down to O(NlogN). */
4978 amt = extsymcount;
4979 amt *= sizeof (struct elf_link_hash_entry *);
4980 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4981 if (sorted_sym_hash == NULL)
4982 goto error_return;
4983 sym_hash = sorted_sym_hash;
4984 hpp = elf_sym_hashes (abfd);
4985 hppend = hpp + extsymcount;
4986 sym_count = 0;
4987 for (; hpp < hppend; hpp++)
4988 {
4989 h = *hpp;
4990 if (h != NULL
4991 && h->root.type == bfd_link_hash_defined
4992 && !bed->is_function_type (h->type))
4993 {
4994 *sym_hash = h;
4995 sym_hash++;
4996 sym_count++;
4997 }
4998 }
4999
5000 qsort (sorted_sym_hash, sym_count,
5001 sizeof (struct elf_link_hash_entry *),
5002 elf_sort_symbol);
5003
5004 while (weaks != NULL)
5005 {
5006 struct elf_link_hash_entry *hlook;
5007 asection *slook;
5008 bfd_vma vlook;
5009 size_t i, j, idx = 0;
5010
5011 hlook = weaks;
5012 weaks = hlook->u.weakdef;
5013 hlook->u.weakdef = NULL;
5014
5015 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
5016 || hlook->root.type == bfd_link_hash_defweak
5017 || hlook->root.type == bfd_link_hash_common
5018 || hlook->root.type == bfd_link_hash_indirect);
5019 slook = hlook->root.u.def.section;
5020 vlook = hlook->root.u.def.value;
5021
5022 i = 0;
5023 j = sym_count;
5024 while (i != j)
5025 {
5026 bfd_signed_vma vdiff;
5027 idx = (i + j) / 2;
5028 h = sorted_sym_hash[idx];
5029 vdiff = vlook - h->root.u.def.value;
5030 if (vdiff < 0)
5031 j = idx;
5032 else if (vdiff > 0)
5033 i = idx + 1;
5034 else
5035 {
5036 int sdiff = slook->id - h->root.u.def.section->id;
5037 if (sdiff < 0)
5038 j = idx;
5039 else if (sdiff > 0)
5040 i = idx + 1;
5041 else
5042 break;
5043 }
5044 }
5045
5046 /* We didn't find a value/section match. */
5047 if (i == j)
5048 continue;
5049
5050 /* With multiple aliases, or when the weak symbol is already
5051 strongly defined, we have multiple matching symbols and
5052 the binary search above may land on any of them. Step
5053 one past the matching symbol(s). */
5054 while (++idx != j)
5055 {
5056 h = sorted_sym_hash[idx];
5057 if (h->root.u.def.section != slook
5058 || h->root.u.def.value != vlook)
5059 break;
5060 }
5061
5062 /* Now look back over the aliases. Since we sorted by size
5063 as well as value and section, we'll choose the one with
5064 the largest size. */
5065 while (idx-- != i)
5066 {
5067 h = sorted_sym_hash[idx];
5068
5069 /* Stop if value or section doesn't match. */
5070 if (h->root.u.def.section != slook
5071 || h->root.u.def.value != vlook)
5072 break;
5073 else if (h != hlook)
5074 {
5075 hlook->u.weakdef = h;
5076
5077 /* If the weak definition is in the list of dynamic
5078 symbols, make sure the real definition is put
5079 there as well. */
5080 if (hlook->dynindx != -1 && h->dynindx == -1)
5081 {
5082 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5083 {
5084 err_free_sym_hash:
5085 free (sorted_sym_hash);
5086 goto error_return;
5087 }
5088 }
5089
5090 /* If the real definition is in the list of dynamic
5091 symbols, make sure the weak definition is put
5092 there as well. If we don't do this, then the
5093 dynamic loader might not merge the entries for the
5094 real definition and the weak definition. */
5095 if (h->dynindx != -1 && hlook->dynindx == -1)
5096 {
5097 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5098 goto err_free_sym_hash;
5099 }
5100 break;
5101 }
5102 }
5103 }
5104
5105 free (sorted_sym_hash);
5106 }
5107
5108 if (bed->check_directives
5109 && !(*bed->check_directives) (abfd, info))
5110 return FALSE;
5111
5112 if (!info->check_relocs_after_open_input
5113 && !_bfd_elf_link_check_relocs (abfd, info))
5114 return FALSE;
5115
5116 /* If this is a non-traditional link, try to optimize the handling
5117 of the .stab/.stabstr sections. */
5118 if (! dynamic
5119 && ! info->traditional_format
5120 && is_elf_hash_table (htab)
5121 && (info->strip != strip_all && info->strip != strip_debugger))
5122 {
5123 asection *stabstr;
5124
5125 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5126 if (stabstr != NULL)
5127 {
5128 bfd_size_type string_offset = 0;
5129 asection *stab;
5130
5131 for (stab = abfd->sections; stab; stab = stab->next)
5132 if (CONST_STRNEQ (stab->name, ".stab")
5133 && (!stab->name[5] ||
5134 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5135 && (stab->flags & SEC_MERGE) == 0
5136 && !bfd_is_abs_section (stab->output_section))
5137 {
5138 struct bfd_elf_section_data *secdata;
5139
5140 secdata = elf_section_data (stab);
5141 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5142 stabstr, &secdata->sec_info,
5143 &string_offset))
5144 goto error_return;
5145 if (secdata->sec_info)
5146 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5147 }
5148 }
5149 }
5150
5151 if (is_elf_hash_table (htab) && add_needed)
5152 {
5153 /* Add this bfd to the loaded list. */
5154 struct elf_link_loaded_list *n;
5155
5156 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5157 if (n == NULL)
5158 goto error_return;
5159 n->abfd = abfd;
5160 n->next = htab->loaded;
5161 htab->loaded = n;
5162 }
5163
5164 return TRUE;
5165
5166 error_free_vers:
5167 if (old_tab != NULL)
5168 free (old_tab);
5169 if (old_strtab != NULL)
5170 free (old_strtab);
5171 if (nondeflt_vers != NULL)
5172 free (nondeflt_vers);
5173 if (extversym != NULL)
5174 free (extversym);
5175 error_free_sym:
5176 if (isymbuf != NULL)
5177 free (isymbuf);
5178 error_return:
5179 return FALSE;
5180 }
5181
5182 /* Return the linker hash table entry of a symbol that might be
5183 satisfied by an archive symbol. Return -1 on error. */
5184
5185 struct elf_link_hash_entry *
5186 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5187 struct bfd_link_info *info,
5188 const char *name)
5189 {
5190 struct elf_link_hash_entry *h;
5191 char *p, *copy;
5192 size_t len, first;
5193
5194 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5195 if (h != NULL)
5196 return h;
5197
5198 /* If this is a default version (the name contains @@), look up the
5199 symbol again with only one `@' as well as without the version.
5200 The effect is that references to the symbol with and without the
5201 version will be matched by the default symbol in the archive. */
5202
5203 p = strchr (name, ELF_VER_CHR);
5204 if (p == NULL || p[1] != ELF_VER_CHR)
5205 return h;
5206
5207 /* First check with only one `@'. */
5208 len = strlen (name);
5209 copy = (char *) bfd_alloc (abfd, len);
5210 if (copy == NULL)
5211 return (struct elf_link_hash_entry *) 0 - 1;
5212
5213 first = p - name + 1;
5214 memcpy (copy, name, first);
5215 memcpy (copy + first, name + first + 1, len - first);
5216
5217 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5218 if (h == NULL)
5219 {
5220 /* We also need to check references to the symbol without the
5221 version. */
5222 copy[first - 1] = '\0';
5223 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5224 FALSE, FALSE, TRUE);
5225 }
5226
5227 bfd_release (abfd, copy);
5228 return h;
5229 }
5230
5231 /* Add symbols from an ELF archive file to the linker hash table. We
5232 don't use _bfd_generic_link_add_archive_symbols because we need to
5233 handle versioned symbols.
5234
5235 Fortunately, ELF archive handling is simpler than that done by
5236 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5237 oddities. In ELF, if we find a symbol in the archive map, and the
5238 symbol is currently undefined, we know that we must pull in that
5239 object file.
5240
5241 Unfortunately, we do have to make multiple passes over the symbol
5242 table until nothing further is resolved. */
5243
5244 static bfd_boolean
5245 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5246 {
5247 symindex c;
5248 unsigned char *included = NULL;
5249 carsym *symdefs;
5250 bfd_boolean loop;
5251 bfd_size_type amt;
5252 const struct elf_backend_data *bed;
5253 struct elf_link_hash_entry * (*archive_symbol_lookup)
5254 (bfd *, struct bfd_link_info *, const char *);
5255
5256 if (! bfd_has_map (abfd))
5257 {
5258 /* An empty archive is a special case. */
5259 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5260 return TRUE;
5261 bfd_set_error (bfd_error_no_armap);
5262 return FALSE;
5263 }
5264
5265 /* Keep track of all symbols we know to be already defined, and all
5266 files we know to be already included. This is to speed up the
5267 second and subsequent passes. */
5268 c = bfd_ardata (abfd)->symdef_count;
5269 if (c == 0)
5270 return TRUE;
5271 amt = c;
5272 amt *= sizeof (*included);
5273 included = (unsigned char *) bfd_zmalloc (amt);
5274 if (included == NULL)
5275 return FALSE;
5276
5277 symdefs = bfd_ardata (abfd)->symdefs;
5278 bed = get_elf_backend_data (abfd);
5279 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5280
5281 do
5282 {
5283 file_ptr last;
5284 symindex i;
5285 carsym *symdef;
5286 carsym *symdefend;
5287
5288 loop = FALSE;
5289 last = -1;
5290
5291 symdef = symdefs;
5292 symdefend = symdef + c;
5293 for (i = 0; symdef < symdefend; symdef++, i++)
5294 {
5295 struct elf_link_hash_entry *h;
5296 bfd *element;
5297 struct bfd_link_hash_entry *undefs_tail;
5298 symindex mark;
5299
5300 if (included[i])
5301 continue;
5302 if (symdef->file_offset == last)
5303 {
5304 included[i] = TRUE;
5305 continue;
5306 }
5307
5308 h = archive_symbol_lookup (abfd, info, symdef->name);
5309 if (h == (struct elf_link_hash_entry *) 0 - 1)
5310 goto error_return;
5311
5312 if (h == NULL)
5313 continue;
5314
5315 if (h->root.type == bfd_link_hash_common)
5316 {
5317 /* We currently have a common symbol. The archive map contains
5318 a reference to this symbol, so we may want to include it. We
5319 only want to include it however, if this archive element
5320 contains a definition of the symbol, not just another common
5321 declaration of it.
5322
5323 Unfortunately some archivers (including GNU ar) will put
5324 declarations of common symbols into their archive maps, as
5325 well as real definitions, so we cannot just go by the archive
5326 map alone. Instead we must read in the element's symbol
5327 table and check that to see what kind of symbol definition
5328 this is. */
5329 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5330 continue;
5331 }
5332 else if (h->root.type != bfd_link_hash_undefined)
5333 {
5334 if (h->root.type != bfd_link_hash_undefweak)
5335 /* Symbol must be defined. Don't check it again. */
5336 included[i] = TRUE;
5337 continue;
5338 }
5339
5340 /* We need to include this archive member. */
5341 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5342 if (element == NULL)
5343 goto error_return;
5344
5345 if (! bfd_check_format (element, bfd_object))
5346 goto error_return;
5347
5348 undefs_tail = info->hash->undefs_tail;
5349
5350 if (!(*info->callbacks
5351 ->add_archive_element) (info, element, symdef->name, &element))
5352 continue;
5353 if (!bfd_link_add_symbols (element, info))
5354 goto error_return;
5355
5356 /* If there are any new undefined symbols, we need to make
5357 another pass through the archive in order to see whether
5358 they can be defined. FIXME: This isn't perfect, because
5359 common symbols wind up on undefs_tail and because an
5360 undefined symbol which is defined later on in this pass
5361 does not require another pass. This isn't a bug, but it
5362 does make the code less efficient than it could be. */
5363 if (undefs_tail != info->hash->undefs_tail)
5364 loop = TRUE;
5365
5366 /* Look backward to mark all symbols from this object file
5367 which we have already seen in this pass. */
5368 mark = i;
5369 do
5370 {
5371 included[mark] = TRUE;
5372 if (mark == 0)
5373 break;
5374 --mark;
5375 }
5376 while (symdefs[mark].file_offset == symdef->file_offset);
5377
5378 /* We mark subsequent symbols from this object file as we go
5379 on through the loop. */
5380 last = symdef->file_offset;
5381 }
5382 }
5383 while (loop);
5384
5385 free (included);
5386
5387 return TRUE;
5388
5389 error_return:
5390 if (included != NULL)
5391 free (included);
5392 return FALSE;
5393 }
5394
5395 /* Given an ELF BFD, add symbols to the global hash table as
5396 appropriate. */
5397
5398 bfd_boolean
5399 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5400 {
5401 switch (bfd_get_format (abfd))
5402 {
5403 case bfd_object:
5404 return elf_link_add_object_symbols (abfd, info);
5405 case bfd_archive:
5406 return elf_link_add_archive_symbols (abfd, info);
5407 default:
5408 bfd_set_error (bfd_error_wrong_format);
5409 return FALSE;
5410 }
5411 }
5412 \f
5413 struct hash_codes_info
5414 {
5415 unsigned long *hashcodes;
5416 bfd_boolean error;
5417 };
5418
5419 /* This function will be called though elf_link_hash_traverse to store
5420 all hash value of the exported symbols in an array. */
5421
5422 static bfd_boolean
5423 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5424 {
5425 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5426 const char *name;
5427 unsigned long ha;
5428 char *alc = NULL;
5429
5430 /* Ignore indirect symbols. These are added by the versioning code. */
5431 if (h->dynindx == -1)
5432 return TRUE;
5433
5434 name = h->root.root.string;
5435 if (h->versioned >= versioned)
5436 {
5437 char *p = strchr (name, ELF_VER_CHR);
5438 if (p != NULL)
5439 {
5440 alc = (char *) bfd_malloc (p - name + 1);
5441 if (alc == NULL)
5442 {
5443 inf->error = TRUE;
5444 return FALSE;
5445 }
5446 memcpy (alc, name, p - name);
5447 alc[p - name] = '\0';
5448 name = alc;
5449 }
5450 }
5451
5452 /* Compute the hash value. */
5453 ha = bfd_elf_hash (name);
5454
5455 /* Store the found hash value in the array given as the argument. */
5456 *(inf->hashcodes)++ = ha;
5457
5458 /* And store it in the struct so that we can put it in the hash table
5459 later. */
5460 h->u.elf_hash_value = ha;
5461
5462 if (alc != NULL)
5463 free (alc);
5464
5465 return TRUE;
5466 }
5467
5468 struct collect_gnu_hash_codes
5469 {
5470 bfd *output_bfd;
5471 const struct elf_backend_data *bed;
5472 unsigned long int nsyms;
5473 unsigned long int maskbits;
5474 unsigned long int *hashcodes;
5475 unsigned long int *hashval;
5476 unsigned long int *indx;
5477 unsigned long int *counts;
5478 bfd_vma *bitmask;
5479 bfd_byte *contents;
5480 long int min_dynindx;
5481 unsigned long int bucketcount;
5482 unsigned long int symindx;
5483 long int local_indx;
5484 long int shift1, shift2;
5485 unsigned long int mask;
5486 bfd_boolean error;
5487 };
5488
5489 /* This function will be called though elf_link_hash_traverse to store
5490 all hash value of the exported symbols in an array. */
5491
5492 static bfd_boolean
5493 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5494 {
5495 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5496 const char *name;
5497 unsigned long ha;
5498 char *alc = NULL;
5499
5500 /* Ignore indirect symbols. These are added by the versioning code. */
5501 if (h->dynindx == -1)
5502 return TRUE;
5503
5504 /* Ignore also local symbols and undefined symbols. */
5505 if (! (*s->bed->elf_hash_symbol) (h))
5506 return TRUE;
5507
5508 name = h->root.root.string;
5509 if (h->versioned >= versioned)
5510 {
5511 char *p = strchr (name, ELF_VER_CHR);
5512 if (p != NULL)
5513 {
5514 alc = (char *) bfd_malloc (p - name + 1);
5515 if (alc == NULL)
5516 {
5517 s->error = TRUE;
5518 return FALSE;
5519 }
5520 memcpy (alc, name, p - name);
5521 alc[p - name] = '\0';
5522 name = alc;
5523 }
5524 }
5525
5526 /* Compute the hash value. */
5527 ha = bfd_elf_gnu_hash (name);
5528
5529 /* Store the found hash value in the array for compute_bucket_count,
5530 and also for .dynsym reordering purposes. */
5531 s->hashcodes[s->nsyms] = ha;
5532 s->hashval[h->dynindx] = ha;
5533 ++s->nsyms;
5534 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5535 s->min_dynindx = h->dynindx;
5536
5537 if (alc != NULL)
5538 free (alc);
5539
5540 return TRUE;
5541 }
5542
5543 /* This function will be called though elf_link_hash_traverse to do
5544 final dynaminc symbol renumbering. */
5545
5546 static bfd_boolean
5547 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5548 {
5549 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5550 unsigned long int bucket;
5551 unsigned long int val;
5552
5553 /* Ignore indirect symbols. */
5554 if (h->dynindx == -1)
5555 return TRUE;
5556
5557 /* Ignore also local symbols and undefined symbols. */
5558 if (! (*s->bed->elf_hash_symbol) (h))
5559 {
5560 if (h->dynindx >= s->min_dynindx)
5561 h->dynindx = s->local_indx++;
5562 return TRUE;
5563 }
5564
5565 bucket = s->hashval[h->dynindx] % s->bucketcount;
5566 val = (s->hashval[h->dynindx] >> s->shift1)
5567 & ((s->maskbits >> s->shift1) - 1);
5568 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5569 s->bitmask[val]
5570 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5571 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5572 if (s->counts[bucket] == 1)
5573 /* Last element terminates the chain. */
5574 val |= 1;
5575 bfd_put_32 (s->output_bfd, val,
5576 s->contents + (s->indx[bucket] - s->symindx) * 4);
5577 --s->counts[bucket];
5578 h->dynindx = s->indx[bucket]++;
5579 return TRUE;
5580 }
5581
5582 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5583
5584 bfd_boolean
5585 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5586 {
5587 return !(h->forced_local
5588 || h->root.type == bfd_link_hash_undefined
5589 || h->root.type == bfd_link_hash_undefweak
5590 || ((h->root.type == bfd_link_hash_defined
5591 || h->root.type == bfd_link_hash_defweak)
5592 && h->root.u.def.section->output_section == NULL));
5593 }
5594
5595 /* Array used to determine the number of hash table buckets to use
5596 based on the number of symbols there are. If there are fewer than
5597 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5598 fewer than 37 we use 17 buckets, and so forth. We never use more
5599 than 32771 buckets. */
5600
5601 static const size_t elf_buckets[] =
5602 {
5603 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5604 16411, 32771, 0
5605 };
5606
5607 /* Compute bucket count for hashing table. We do not use a static set
5608 of possible tables sizes anymore. Instead we determine for all
5609 possible reasonable sizes of the table the outcome (i.e., the
5610 number of collisions etc) and choose the best solution. The
5611 weighting functions are not too simple to allow the table to grow
5612 without bounds. Instead one of the weighting factors is the size.
5613 Therefore the result is always a good payoff between few collisions
5614 (= short chain lengths) and table size. */
5615 static size_t
5616 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5617 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5618 unsigned long int nsyms,
5619 int gnu_hash)
5620 {
5621 size_t best_size = 0;
5622 unsigned long int i;
5623
5624 /* We have a problem here. The following code to optimize the table
5625 size requires an integer type with more the 32 bits. If
5626 BFD_HOST_U_64_BIT is set we know about such a type. */
5627 #ifdef BFD_HOST_U_64_BIT
5628 if (info->optimize)
5629 {
5630 size_t minsize;
5631 size_t maxsize;
5632 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5633 bfd *dynobj = elf_hash_table (info)->dynobj;
5634 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5635 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5636 unsigned long int *counts;
5637 bfd_size_type amt;
5638 unsigned int no_improvement_count = 0;
5639
5640 /* Possible optimization parameters: if we have NSYMS symbols we say
5641 that the hashing table must at least have NSYMS/4 and at most
5642 2*NSYMS buckets. */
5643 minsize = nsyms / 4;
5644 if (minsize == 0)
5645 minsize = 1;
5646 best_size = maxsize = nsyms * 2;
5647 if (gnu_hash)
5648 {
5649 if (minsize < 2)
5650 minsize = 2;
5651 if ((best_size & 31) == 0)
5652 ++best_size;
5653 }
5654
5655 /* Create array where we count the collisions in. We must use bfd_malloc
5656 since the size could be large. */
5657 amt = maxsize;
5658 amt *= sizeof (unsigned long int);
5659 counts = (unsigned long int *) bfd_malloc (amt);
5660 if (counts == NULL)
5661 return 0;
5662
5663 /* Compute the "optimal" size for the hash table. The criteria is a
5664 minimal chain length. The minor criteria is (of course) the size
5665 of the table. */
5666 for (i = minsize; i < maxsize; ++i)
5667 {
5668 /* Walk through the array of hashcodes and count the collisions. */
5669 BFD_HOST_U_64_BIT max;
5670 unsigned long int j;
5671 unsigned long int fact;
5672
5673 if (gnu_hash && (i & 31) == 0)
5674 continue;
5675
5676 memset (counts, '\0', i * sizeof (unsigned long int));
5677
5678 /* Determine how often each hash bucket is used. */
5679 for (j = 0; j < nsyms; ++j)
5680 ++counts[hashcodes[j] % i];
5681
5682 /* For the weight function we need some information about the
5683 pagesize on the target. This is information need not be 100%
5684 accurate. Since this information is not available (so far) we
5685 define it here to a reasonable default value. If it is crucial
5686 to have a better value some day simply define this value. */
5687 # ifndef BFD_TARGET_PAGESIZE
5688 # define BFD_TARGET_PAGESIZE (4096)
5689 # endif
5690
5691 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5692 and the chains. */
5693 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5694
5695 # if 1
5696 /* Variant 1: optimize for short chains. We add the squares
5697 of all the chain lengths (which favors many small chain
5698 over a few long chains). */
5699 for (j = 0; j < i; ++j)
5700 max += counts[j] * counts[j];
5701
5702 /* This adds penalties for the overall size of the table. */
5703 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5704 max *= fact * fact;
5705 # else
5706 /* Variant 2: Optimize a lot more for small table. Here we
5707 also add squares of the size but we also add penalties for
5708 empty slots (the +1 term). */
5709 for (j = 0; j < i; ++j)
5710 max += (1 + counts[j]) * (1 + counts[j]);
5711
5712 /* The overall size of the table is considered, but not as
5713 strong as in variant 1, where it is squared. */
5714 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5715 max *= fact;
5716 # endif
5717
5718 /* Compare with current best results. */
5719 if (max < best_chlen)
5720 {
5721 best_chlen = max;
5722 best_size = i;
5723 no_improvement_count = 0;
5724 }
5725 /* PR 11843: Avoid futile long searches for the best bucket size
5726 when there are a large number of symbols. */
5727 else if (++no_improvement_count == 100)
5728 break;
5729 }
5730
5731 free (counts);
5732 }
5733 else
5734 #endif /* defined (BFD_HOST_U_64_BIT) */
5735 {
5736 /* This is the fallback solution if no 64bit type is available or if we
5737 are not supposed to spend much time on optimizations. We select the
5738 bucket count using a fixed set of numbers. */
5739 for (i = 0; elf_buckets[i] != 0; i++)
5740 {
5741 best_size = elf_buckets[i];
5742 if (nsyms < elf_buckets[i + 1])
5743 break;
5744 }
5745 if (gnu_hash && best_size < 2)
5746 best_size = 2;
5747 }
5748
5749 return best_size;
5750 }
5751
5752 /* Size any SHT_GROUP section for ld -r. */
5753
5754 bfd_boolean
5755 _bfd_elf_size_group_sections (struct bfd_link_info *info)
5756 {
5757 bfd *ibfd;
5758
5759 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
5760 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5761 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5762 return FALSE;
5763 return TRUE;
5764 }
5765
5766 /* Set a default stack segment size. The value in INFO wins. If it
5767 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5768 undefined it is initialized. */
5769
5770 bfd_boolean
5771 bfd_elf_stack_segment_size (bfd *output_bfd,
5772 struct bfd_link_info *info,
5773 const char *legacy_symbol,
5774 bfd_vma default_size)
5775 {
5776 struct elf_link_hash_entry *h = NULL;
5777
5778 /* Look for legacy symbol. */
5779 if (legacy_symbol)
5780 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5781 FALSE, FALSE, FALSE);
5782 if (h && (h->root.type == bfd_link_hash_defined
5783 || h->root.type == bfd_link_hash_defweak)
5784 && h->def_regular
5785 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5786 {
5787 /* The symbol has no type if specified on the command line. */
5788 h->type = STT_OBJECT;
5789 if (info->stacksize)
5790 /* xgettext:c-format */
5791 _bfd_error_handler (_("%B: stack size specified and %s set"),
5792 output_bfd, legacy_symbol);
5793 else if (h->root.u.def.section != bfd_abs_section_ptr)
5794 /* xgettext:c-format */
5795 _bfd_error_handler (_("%B: %s not absolute"),
5796 output_bfd, legacy_symbol);
5797 else
5798 info->stacksize = h->root.u.def.value;
5799 }
5800
5801 if (!info->stacksize)
5802 /* If the user didn't set a size, or explicitly inhibit the
5803 size, set it now. */
5804 info->stacksize = default_size;
5805
5806 /* Provide the legacy symbol, if it is referenced. */
5807 if (h && (h->root.type == bfd_link_hash_undefined
5808 || h->root.type == bfd_link_hash_undefweak))
5809 {
5810 struct bfd_link_hash_entry *bh = NULL;
5811
5812 if (!(_bfd_generic_link_add_one_symbol
5813 (info, output_bfd, legacy_symbol,
5814 BSF_GLOBAL, bfd_abs_section_ptr,
5815 info->stacksize >= 0 ? info->stacksize : 0,
5816 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5817 return FALSE;
5818
5819 h = (struct elf_link_hash_entry *) bh;
5820 h->def_regular = 1;
5821 h->type = STT_OBJECT;
5822 }
5823
5824 return TRUE;
5825 }
5826
5827 /* Set up the sizes and contents of the ELF dynamic sections. This is
5828 called by the ELF linker emulation before_allocation routine. We
5829 must set the sizes of the sections before the linker sets the
5830 addresses of the various sections. */
5831
5832 bfd_boolean
5833 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5834 const char *soname,
5835 const char *rpath,
5836 const char *filter_shlib,
5837 const char *audit,
5838 const char *depaudit,
5839 const char * const *auxiliary_filters,
5840 struct bfd_link_info *info,
5841 asection **sinterpptr)
5842 {
5843 size_t soname_indx;
5844 bfd *dynobj;
5845 const struct elf_backend_data *bed;
5846 struct elf_info_failed asvinfo;
5847
5848 *sinterpptr = NULL;
5849
5850 soname_indx = (size_t) -1;
5851
5852 if (!is_elf_hash_table (info->hash))
5853 return TRUE;
5854
5855 bed = get_elf_backend_data (output_bfd);
5856
5857 /* Any syms created from now on start with -1 in
5858 got.refcount/offset and plt.refcount/offset. */
5859 elf_hash_table (info)->init_got_refcount
5860 = elf_hash_table (info)->init_got_offset;
5861 elf_hash_table (info)->init_plt_refcount
5862 = elf_hash_table (info)->init_plt_offset;
5863
5864 if (bfd_link_relocatable (info)
5865 && !_bfd_elf_size_group_sections (info))
5866 return FALSE;
5867
5868 /* The backend may have to create some sections regardless of whether
5869 we're dynamic or not. */
5870 if (bed->elf_backend_always_size_sections
5871 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5872 return FALSE;
5873
5874 /* Determine any GNU_STACK segment requirements, after the backend
5875 has had a chance to set a default segment size. */
5876 if (info->execstack)
5877 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
5878 else if (info->noexecstack)
5879 elf_stack_flags (output_bfd) = PF_R | PF_W;
5880 else
5881 {
5882 bfd *inputobj;
5883 asection *notesec = NULL;
5884 int exec = 0;
5885
5886 for (inputobj = info->input_bfds;
5887 inputobj;
5888 inputobj = inputobj->link.next)
5889 {
5890 asection *s;
5891
5892 if (inputobj->flags
5893 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5894 continue;
5895 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5896 if (s)
5897 {
5898 if (s->flags & SEC_CODE)
5899 exec = PF_X;
5900 notesec = s;
5901 }
5902 else if (bed->default_execstack)
5903 exec = PF_X;
5904 }
5905 if (notesec || info->stacksize > 0)
5906 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
5907 if (notesec && exec && bfd_link_relocatable (info)
5908 && notesec->output_section != bfd_abs_section_ptr)
5909 notesec->output_section->flags |= SEC_CODE;
5910 }
5911
5912 dynobj = elf_hash_table (info)->dynobj;
5913
5914 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5915 {
5916 struct elf_info_failed eif;
5917 struct elf_link_hash_entry *h;
5918 asection *dynstr;
5919 struct bfd_elf_version_tree *t;
5920 struct bfd_elf_version_expr *d;
5921 asection *s;
5922 bfd_boolean all_defined;
5923
5924 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
5925 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
5926
5927 if (soname != NULL)
5928 {
5929 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5930 soname, TRUE);
5931 if (soname_indx == (size_t) -1
5932 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5933 return FALSE;
5934 }
5935
5936 if (info->symbolic)
5937 {
5938 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5939 return FALSE;
5940 info->flags |= DF_SYMBOLIC;
5941 }
5942
5943 if (rpath != NULL)
5944 {
5945 size_t indx;
5946 bfd_vma tag;
5947
5948 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5949 TRUE);
5950 if (indx == (size_t) -1)
5951 return FALSE;
5952
5953 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
5954 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
5955 return FALSE;
5956 }
5957
5958 if (filter_shlib != NULL)
5959 {
5960 size_t indx;
5961
5962 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5963 filter_shlib, TRUE);
5964 if (indx == (size_t) -1
5965 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5966 return FALSE;
5967 }
5968
5969 if (auxiliary_filters != NULL)
5970 {
5971 const char * const *p;
5972
5973 for (p = auxiliary_filters; *p != NULL; p++)
5974 {
5975 size_t indx;
5976
5977 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5978 *p, TRUE);
5979 if (indx == (size_t) -1
5980 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5981 return FALSE;
5982 }
5983 }
5984
5985 if (audit != NULL)
5986 {
5987 size_t indx;
5988
5989 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5990 TRUE);
5991 if (indx == (size_t) -1
5992 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5993 return FALSE;
5994 }
5995
5996 if (depaudit != NULL)
5997 {
5998 size_t indx;
5999
6000 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6001 TRUE);
6002 if (indx == (size_t) -1
6003 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6004 return FALSE;
6005 }
6006
6007 eif.info = info;
6008 eif.failed = FALSE;
6009
6010 /* If we are supposed to export all symbols into the dynamic symbol
6011 table (this is not the normal case), then do so. */
6012 if (info->export_dynamic
6013 || (bfd_link_executable (info) && info->dynamic))
6014 {
6015 elf_link_hash_traverse (elf_hash_table (info),
6016 _bfd_elf_export_symbol,
6017 &eif);
6018 if (eif.failed)
6019 return FALSE;
6020 }
6021
6022 /* Make all global versions with definition. */
6023 for (t = info->version_info; t != NULL; t = t->next)
6024 for (d = t->globals.list; d != NULL; d = d->next)
6025 if (!d->symver && d->literal)
6026 {
6027 const char *verstr, *name;
6028 size_t namelen, verlen, newlen;
6029 char *newname, *p, leading_char;
6030 struct elf_link_hash_entry *newh;
6031
6032 leading_char = bfd_get_symbol_leading_char (output_bfd);
6033 name = d->pattern;
6034 namelen = strlen (name) + (leading_char != '\0');
6035 verstr = t->name;
6036 verlen = strlen (verstr);
6037 newlen = namelen + verlen + 3;
6038
6039 newname = (char *) bfd_malloc (newlen);
6040 if (newname == NULL)
6041 return FALSE;
6042 newname[0] = leading_char;
6043 memcpy (newname + (leading_char != '\0'), name, namelen);
6044
6045 /* Check the hidden versioned definition. */
6046 p = newname + namelen;
6047 *p++ = ELF_VER_CHR;
6048 memcpy (p, verstr, verlen + 1);
6049 newh = elf_link_hash_lookup (elf_hash_table (info),
6050 newname, FALSE, FALSE,
6051 FALSE);
6052 if (newh == NULL
6053 || (newh->root.type != bfd_link_hash_defined
6054 && newh->root.type != bfd_link_hash_defweak))
6055 {
6056 /* Check the default versioned definition. */
6057 *p++ = ELF_VER_CHR;
6058 memcpy (p, verstr, verlen + 1);
6059 newh = elf_link_hash_lookup (elf_hash_table (info),
6060 newname, FALSE, FALSE,
6061 FALSE);
6062 }
6063 free (newname);
6064
6065 /* Mark this version if there is a definition and it is
6066 not defined in a shared object. */
6067 if (newh != NULL
6068 && !newh->def_dynamic
6069 && (newh->root.type == bfd_link_hash_defined
6070 || newh->root.type == bfd_link_hash_defweak))
6071 d->symver = 1;
6072 }
6073
6074 /* Attach all the symbols to their version information. */
6075 asvinfo.info = info;
6076 asvinfo.failed = FALSE;
6077
6078 elf_link_hash_traverse (elf_hash_table (info),
6079 _bfd_elf_link_assign_sym_version,
6080 &asvinfo);
6081 if (asvinfo.failed)
6082 return FALSE;
6083
6084 if (!info->allow_undefined_version)
6085 {
6086 /* Check if all global versions have a definition. */
6087 all_defined = TRUE;
6088 for (t = info->version_info; t != NULL; t = t->next)
6089 for (d = t->globals.list; d != NULL; d = d->next)
6090 if (d->literal && !d->symver && !d->script)
6091 {
6092 _bfd_error_handler
6093 (_("%s: undefined version: %s"),
6094 d->pattern, t->name);
6095 all_defined = FALSE;
6096 }
6097
6098 if (!all_defined)
6099 {
6100 bfd_set_error (bfd_error_bad_value);
6101 return FALSE;
6102 }
6103 }
6104
6105 /* Find all symbols which were defined in a dynamic object and make
6106 the backend pick a reasonable value for them. */
6107 elf_link_hash_traverse (elf_hash_table (info),
6108 _bfd_elf_adjust_dynamic_symbol,
6109 &eif);
6110 if (eif.failed)
6111 return FALSE;
6112
6113 /* Add some entries to the .dynamic section. We fill in some of the
6114 values later, in bfd_elf_final_link, but we must add the entries
6115 now so that we know the final size of the .dynamic section. */
6116
6117 /* If there are initialization and/or finalization functions to
6118 call then add the corresponding DT_INIT/DT_FINI entries. */
6119 h = (info->init_function
6120 ? elf_link_hash_lookup (elf_hash_table (info),
6121 info->init_function, FALSE,
6122 FALSE, FALSE)
6123 : NULL);
6124 if (h != NULL
6125 && (h->ref_regular
6126 || h->def_regular))
6127 {
6128 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6129 return FALSE;
6130 }
6131 h = (info->fini_function
6132 ? elf_link_hash_lookup (elf_hash_table (info),
6133 info->fini_function, FALSE,
6134 FALSE, FALSE)
6135 : NULL);
6136 if (h != NULL
6137 && (h->ref_regular
6138 || h->def_regular))
6139 {
6140 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6141 return FALSE;
6142 }
6143
6144 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6145 if (s != NULL && s->linker_has_input)
6146 {
6147 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6148 if (! bfd_link_executable (info))
6149 {
6150 bfd *sub;
6151 asection *o;
6152
6153 for (sub = info->input_bfds; sub != NULL;
6154 sub = sub->link.next)
6155 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
6156 for (o = sub->sections; o != NULL; o = o->next)
6157 if (elf_section_data (o)->this_hdr.sh_type
6158 == SHT_PREINIT_ARRAY)
6159 {
6160 _bfd_error_handler
6161 (_("%B: .preinit_array section is not allowed in DSO"),
6162 sub);
6163 break;
6164 }
6165
6166 bfd_set_error (bfd_error_nonrepresentable_section);
6167 return FALSE;
6168 }
6169
6170 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6171 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6172 return FALSE;
6173 }
6174 s = bfd_get_section_by_name (output_bfd, ".init_array");
6175 if (s != NULL && s->linker_has_input)
6176 {
6177 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6178 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6179 return FALSE;
6180 }
6181 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6182 if (s != NULL && s->linker_has_input)
6183 {
6184 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6185 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6186 return FALSE;
6187 }
6188
6189 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6190 /* If .dynstr is excluded from the link, we don't want any of
6191 these tags. Strictly, we should be checking each section
6192 individually; This quick check covers for the case where
6193 someone does a /DISCARD/ : { *(*) }. */
6194 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6195 {
6196 bfd_size_type strsize;
6197
6198 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6199 if ((info->emit_hash
6200 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6201 || (info->emit_gnu_hash
6202 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6203 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6204 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6205 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6206 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6207 bed->s->sizeof_sym))
6208 return FALSE;
6209 }
6210 }
6211
6212 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6213 return FALSE;
6214
6215 /* The backend must work out the sizes of all the other dynamic
6216 sections. */
6217 if (dynobj != NULL
6218 && bed->elf_backend_size_dynamic_sections != NULL
6219 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6220 return FALSE;
6221
6222 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6223 {
6224 unsigned long section_sym_count;
6225 struct bfd_elf_version_tree *verdefs;
6226 asection *s;
6227
6228 /* Set up the version definition section. */
6229 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6230 BFD_ASSERT (s != NULL);
6231
6232 /* We may have created additional version definitions if we are
6233 just linking a regular application. */
6234 verdefs = info->version_info;
6235
6236 /* Skip anonymous version tag. */
6237 if (verdefs != NULL && verdefs->vernum == 0)
6238 verdefs = verdefs->next;
6239
6240 if (verdefs == NULL && !info->create_default_symver)
6241 s->flags |= SEC_EXCLUDE;
6242 else
6243 {
6244 unsigned int cdefs;
6245 bfd_size_type size;
6246 struct bfd_elf_version_tree *t;
6247 bfd_byte *p;
6248 Elf_Internal_Verdef def;
6249 Elf_Internal_Verdaux defaux;
6250 struct bfd_link_hash_entry *bh;
6251 struct elf_link_hash_entry *h;
6252 const char *name;
6253
6254 cdefs = 0;
6255 size = 0;
6256
6257 /* Make space for the base version. */
6258 size += sizeof (Elf_External_Verdef);
6259 size += sizeof (Elf_External_Verdaux);
6260 ++cdefs;
6261
6262 /* Make space for the default version. */
6263 if (info->create_default_symver)
6264 {
6265 size += sizeof (Elf_External_Verdef);
6266 ++cdefs;
6267 }
6268
6269 for (t = verdefs; t != NULL; t = t->next)
6270 {
6271 struct bfd_elf_version_deps *n;
6272
6273 /* Don't emit base version twice. */
6274 if (t->vernum == 0)
6275 continue;
6276
6277 size += sizeof (Elf_External_Verdef);
6278 size += sizeof (Elf_External_Verdaux);
6279 ++cdefs;
6280
6281 for (n = t->deps; n != NULL; n = n->next)
6282 size += sizeof (Elf_External_Verdaux);
6283 }
6284
6285 s->size = size;
6286 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6287 if (s->contents == NULL && s->size != 0)
6288 return FALSE;
6289
6290 /* Fill in the version definition section. */
6291
6292 p = s->contents;
6293
6294 def.vd_version = VER_DEF_CURRENT;
6295 def.vd_flags = VER_FLG_BASE;
6296 def.vd_ndx = 1;
6297 def.vd_cnt = 1;
6298 if (info->create_default_symver)
6299 {
6300 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6301 def.vd_next = sizeof (Elf_External_Verdef);
6302 }
6303 else
6304 {
6305 def.vd_aux = sizeof (Elf_External_Verdef);
6306 def.vd_next = (sizeof (Elf_External_Verdef)
6307 + sizeof (Elf_External_Verdaux));
6308 }
6309
6310 if (soname_indx != (size_t) -1)
6311 {
6312 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6313 soname_indx);
6314 def.vd_hash = bfd_elf_hash (soname);
6315 defaux.vda_name = soname_indx;
6316 name = soname;
6317 }
6318 else
6319 {
6320 size_t indx;
6321
6322 name = lbasename (output_bfd->filename);
6323 def.vd_hash = bfd_elf_hash (name);
6324 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6325 name, FALSE);
6326 if (indx == (size_t) -1)
6327 return FALSE;
6328 defaux.vda_name = indx;
6329 }
6330 defaux.vda_next = 0;
6331
6332 _bfd_elf_swap_verdef_out (output_bfd, &def,
6333 (Elf_External_Verdef *) p);
6334 p += sizeof (Elf_External_Verdef);
6335 if (info->create_default_symver)
6336 {
6337 /* Add a symbol representing this version. */
6338 bh = NULL;
6339 if (! (_bfd_generic_link_add_one_symbol
6340 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6341 0, NULL, FALSE,
6342 get_elf_backend_data (dynobj)->collect, &bh)))
6343 return FALSE;
6344 h = (struct elf_link_hash_entry *) bh;
6345 h->non_elf = 0;
6346 h->def_regular = 1;
6347 h->type = STT_OBJECT;
6348 h->verinfo.vertree = NULL;
6349
6350 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6351 return FALSE;
6352
6353 /* Create a duplicate of the base version with the same
6354 aux block, but different flags. */
6355 def.vd_flags = 0;
6356 def.vd_ndx = 2;
6357 def.vd_aux = sizeof (Elf_External_Verdef);
6358 if (verdefs)
6359 def.vd_next = (sizeof (Elf_External_Verdef)
6360 + sizeof (Elf_External_Verdaux));
6361 else
6362 def.vd_next = 0;
6363 _bfd_elf_swap_verdef_out (output_bfd, &def,
6364 (Elf_External_Verdef *) p);
6365 p += sizeof (Elf_External_Verdef);
6366 }
6367 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6368 (Elf_External_Verdaux *) p);
6369 p += sizeof (Elf_External_Verdaux);
6370
6371 for (t = verdefs; t != NULL; t = t->next)
6372 {
6373 unsigned int cdeps;
6374 struct bfd_elf_version_deps *n;
6375
6376 /* Don't emit the base version twice. */
6377 if (t->vernum == 0)
6378 continue;
6379
6380 cdeps = 0;
6381 for (n = t->deps; n != NULL; n = n->next)
6382 ++cdeps;
6383
6384 /* Add a symbol representing this version. */
6385 bh = NULL;
6386 if (! (_bfd_generic_link_add_one_symbol
6387 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6388 0, NULL, FALSE,
6389 get_elf_backend_data (dynobj)->collect, &bh)))
6390 return FALSE;
6391 h = (struct elf_link_hash_entry *) bh;
6392 h->non_elf = 0;
6393 h->def_regular = 1;
6394 h->type = STT_OBJECT;
6395 h->verinfo.vertree = t;
6396
6397 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6398 return FALSE;
6399
6400 def.vd_version = VER_DEF_CURRENT;
6401 def.vd_flags = 0;
6402 if (t->globals.list == NULL
6403 && t->locals.list == NULL
6404 && ! t->used)
6405 def.vd_flags |= VER_FLG_WEAK;
6406 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6407 def.vd_cnt = cdeps + 1;
6408 def.vd_hash = bfd_elf_hash (t->name);
6409 def.vd_aux = sizeof (Elf_External_Verdef);
6410 def.vd_next = 0;
6411
6412 /* If a basever node is next, it *must* be the last node in
6413 the chain, otherwise Verdef construction breaks. */
6414 if (t->next != NULL && t->next->vernum == 0)
6415 BFD_ASSERT (t->next->next == NULL);
6416
6417 if (t->next != NULL && t->next->vernum != 0)
6418 def.vd_next = (sizeof (Elf_External_Verdef)
6419 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6420
6421 _bfd_elf_swap_verdef_out (output_bfd, &def,
6422 (Elf_External_Verdef *) p);
6423 p += sizeof (Elf_External_Verdef);
6424
6425 defaux.vda_name = h->dynstr_index;
6426 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6427 h->dynstr_index);
6428 defaux.vda_next = 0;
6429 if (t->deps != NULL)
6430 defaux.vda_next = sizeof (Elf_External_Verdaux);
6431 t->name_indx = defaux.vda_name;
6432
6433 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6434 (Elf_External_Verdaux *) p);
6435 p += sizeof (Elf_External_Verdaux);
6436
6437 for (n = t->deps; n != NULL; n = n->next)
6438 {
6439 if (n->version_needed == NULL)
6440 {
6441 /* This can happen if there was an error in the
6442 version script. */
6443 defaux.vda_name = 0;
6444 }
6445 else
6446 {
6447 defaux.vda_name = n->version_needed->name_indx;
6448 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6449 defaux.vda_name);
6450 }
6451 if (n->next == NULL)
6452 defaux.vda_next = 0;
6453 else
6454 defaux.vda_next = sizeof (Elf_External_Verdaux);
6455
6456 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6457 (Elf_External_Verdaux *) p);
6458 p += sizeof (Elf_External_Verdaux);
6459 }
6460 }
6461
6462 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6463 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6464 return FALSE;
6465
6466 elf_tdata (output_bfd)->cverdefs = cdefs;
6467 }
6468
6469 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6470 {
6471 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6472 return FALSE;
6473 }
6474 else if (info->flags & DF_BIND_NOW)
6475 {
6476 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6477 return FALSE;
6478 }
6479
6480 if (info->flags_1)
6481 {
6482 if (bfd_link_executable (info))
6483 info->flags_1 &= ~ (DF_1_INITFIRST
6484 | DF_1_NODELETE
6485 | DF_1_NOOPEN);
6486 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6487 return FALSE;
6488 }
6489
6490 /* Work out the size of the version reference section. */
6491
6492 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6493 BFD_ASSERT (s != NULL);
6494 {
6495 struct elf_find_verdep_info sinfo;
6496
6497 sinfo.info = info;
6498 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6499 if (sinfo.vers == 0)
6500 sinfo.vers = 1;
6501 sinfo.failed = FALSE;
6502
6503 elf_link_hash_traverse (elf_hash_table (info),
6504 _bfd_elf_link_find_version_dependencies,
6505 &sinfo);
6506 if (sinfo.failed)
6507 return FALSE;
6508
6509 if (elf_tdata (output_bfd)->verref == NULL)
6510 s->flags |= SEC_EXCLUDE;
6511 else
6512 {
6513 Elf_Internal_Verneed *t;
6514 unsigned int size;
6515 unsigned int crefs;
6516 bfd_byte *p;
6517
6518 /* Build the version dependency section. */
6519 size = 0;
6520 crefs = 0;
6521 for (t = elf_tdata (output_bfd)->verref;
6522 t != NULL;
6523 t = t->vn_nextref)
6524 {
6525 Elf_Internal_Vernaux *a;
6526
6527 size += sizeof (Elf_External_Verneed);
6528 ++crefs;
6529 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6530 size += sizeof (Elf_External_Vernaux);
6531 }
6532
6533 s->size = size;
6534 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6535 if (s->contents == NULL)
6536 return FALSE;
6537
6538 p = s->contents;
6539 for (t = elf_tdata (output_bfd)->verref;
6540 t != NULL;
6541 t = t->vn_nextref)
6542 {
6543 unsigned int caux;
6544 Elf_Internal_Vernaux *a;
6545 size_t indx;
6546
6547 caux = 0;
6548 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6549 ++caux;
6550
6551 t->vn_version = VER_NEED_CURRENT;
6552 t->vn_cnt = caux;
6553 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6554 elf_dt_name (t->vn_bfd) != NULL
6555 ? elf_dt_name (t->vn_bfd)
6556 : lbasename (t->vn_bfd->filename),
6557 FALSE);
6558 if (indx == (size_t) -1)
6559 return FALSE;
6560 t->vn_file = indx;
6561 t->vn_aux = sizeof (Elf_External_Verneed);
6562 if (t->vn_nextref == NULL)
6563 t->vn_next = 0;
6564 else
6565 t->vn_next = (sizeof (Elf_External_Verneed)
6566 + caux * sizeof (Elf_External_Vernaux));
6567
6568 _bfd_elf_swap_verneed_out (output_bfd, t,
6569 (Elf_External_Verneed *) p);
6570 p += sizeof (Elf_External_Verneed);
6571
6572 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6573 {
6574 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6575 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6576 a->vna_nodename, FALSE);
6577 if (indx == (size_t) -1)
6578 return FALSE;
6579 a->vna_name = indx;
6580 if (a->vna_nextptr == NULL)
6581 a->vna_next = 0;
6582 else
6583 a->vna_next = sizeof (Elf_External_Vernaux);
6584
6585 _bfd_elf_swap_vernaux_out (output_bfd, a,
6586 (Elf_External_Vernaux *) p);
6587 p += sizeof (Elf_External_Vernaux);
6588 }
6589 }
6590
6591 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6592 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6593 return FALSE;
6594
6595 elf_tdata (output_bfd)->cverrefs = crefs;
6596 }
6597 }
6598
6599 if ((elf_tdata (output_bfd)->cverrefs == 0
6600 && elf_tdata (output_bfd)->cverdefs == 0)
6601 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6602 &section_sym_count) == 0)
6603 {
6604 s = bfd_get_linker_section (dynobj, ".gnu.version");
6605 s->flags |= SEC_EXCLUDE;
6606 }
6607 }
6608 return TRUE;
6609 }
6610
6611 /* Find the first non-excluded output section. We'll use its
6612 section symbol for some emitted relocs. */
6613 void
6614 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6615 {
6616 asection *s;
6617
6618 for (s = output_bfd->sections; s != NULL; s = s->next)
6619 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6620 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6621 {
6622 elf_hash_table (info)->text_index_section = s;
6623 break;
6624 }
6625 }
6626
6627 /* Find two non-excluded output sections, one for code, one for data.
6628 We'll use their section symbols for some emitted relocs. */
6629 void
6630 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6631 {
6632 asection *s;
6633
6634 /* Data first, since setting text_index_section changes
6635 _bfd_elf_link_omit_section_dynsym. */
6636 for (s = output_bfd->sections; s != NULL; s = s->next)
6637 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6638 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6639 {
6640 elf_hash_table (info)->data_index_section = s;
6641 break;
6642 }
6643
6644 for (s = output_bfd->sections; s != NULL; s = s->next)
6645 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6646 == (SEC_ALLOC | SEC_READONLY))
6647 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6648 {
6649 elf_hash_table (info)->text_index_section = s;
6650 break;
6651 }
6652
6653 if (elf_hash_table (info)->text_index_section == NULL)
6654 elf_hash_table (info)->text_index_section
6655 = elf_hash_table (info)->data_index_section;
6656 }
6657
6658 bfd_boolean
6659 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6660 {
6661 const struct elf_backend_data *bed;
6662
6663 if (!is_elf_hash_table (info->hash))
6664 return TRUE;
6665
6666 bed = get_elf_backend_data (output_bfd);
6667 (*bed->elf_backend_init_index_section) (output_bfd, info);
6668
6669 if (elf_hash_table (info)->dynamic_sections_created)
6670 {
6671 bfd *dynobj;
6672 asection *s;
6673 bfd_size_type dynsymcount;
6674 unsigned long section_sym_count;
6675 unsigned int dtagcount;
6676
6677 dynobj = elf_hash_table (info)->dynobj;
6678
6679 /* Assign dynsym indicies. In a shared library we generate a
6680 section symbol for each output section, which come first.
6681 Next come all of the back-end allocated local dynamic syms,
6682 followed by the rest of the global symbols. */
6683
6684 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6685 &section_sym_count);
6686
6687 /* Work out the size of the symbol version section. */
6688 s = bfd_get_linker_section (dynobj, ".gnu.version");
6689 BFD_ASSERT (s != NULL);
6690 if ((s->flags & SEC_EXCLUDE) == 0)
6691 {
6692 s->size = dynsymcount * sizeof (Elf_External_Versym);
6693 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6694 if (s->contents == NULL)
6695 return FALSE;
6696
6697 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6698 return FALSE;
6699 }
6700
6701 /* Set the size of the .dynsym and .hash sections. We counted
6702 the number of dynamic symbols in elf_link_add_object_symbols.
6703 We will build the contents of .dynsym and .hash when we build
6704 the final symbol table, because until then we do not know the
6705 correct value to give the symbols. We built the .dynstr
6706 section as we went along in elf_link_add_object_symbols. */
6707 s = elf_hash_table (info)->dynsym;
6708 BFD_ASSERT (s != NULL);
6709 s->size = dynsymcount * bed->s->sizeof_sym;
6710
6711 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6712 if (s->contents == NULL)
6713 return FALSE;
6714
6715 /* The first entry in .dynsym is a dummy symbol. Clear all the
6716 section syms, in case we don't output them all. */
6717 ++section_sym_count;
6718 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6719
6720 elf_hash_table (info)->bucketcount = 0;
6721
6722 /* Compute the size of the hashing table. As a side effect this
6723 computes the hash values for all the names we export. */
6724 if (info->emit_hash)
6725 {
6726 unsigned long int *hashcodes;
6727 struct hash_codes_info hashinf;
6728 bfd_size_type amt;
6729 unsigned long int nsyms;
6730 size_t bucketcount;
6731 size_t hash_entry_size;
6732
6733 /* Compute the hash values for all exported symbols. At the same
6734 time store the values in an array so that we could use them for
6735 optimizations. */
6736 amt = dynsymcount * sizeof (unsigned long int);
6737 hashcodes = (unsigned long int *) bfd_malloc (amt);
6738 if (hashcodes == NULL)
6739 return FALSE;
6740 hashinf.hashcodes = hashcodes;
6741 hashinf.error = FALSE;
6742
6743 /* Put all hash values in HASHCODES. */
6744 elf_link_hash_traverse (elf_hash_table (info),
6745 elf_collect_hash_codes, &hashinf);
6746 if (hashinf.error)
6747 {
6748 free (hashcodes);
6749 return FALSE;
6750 }
6751
6752 nsyms = hashinf.hashcodes - hashcodes;
6753 bucketcount
6754 = compute_bucket_count (info, hashcodes, nsyms, 0);
6755 free (hashcodes);
6756
6757 if (bucketcount == 0)
6758 return FALSE;
6759
6760 elf_hash_table (info)->bucketcount = bucketcount;
6761
6762 s = bfd_get_linker_section (dynobj, ".hash");
6763 BFD_ASSERT (s != NULL);
6764 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6765 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6766 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6767 if (s->contents == NULL)
6768 return FALSE;
6769
6770 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6771 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6772 s->contents + hash_entry_size);
6773 }
6774
6775 if (info->emit_gnu_hash)
6776 {
6777 size_t i, cnt;
6778 unsigned char *contents;
6779 struct collect_gnu_hash_codes cinfo;
6780 bfd_size_type amt;
6781 size_t bucketcount;
6782
6783 memset (&cinfo, 0, sizeof (cinfo));
6784
6785 /* Compute the hash values for all exported symbols. At the same
6786 time store the values in an array so that we could use them for
6787 optimizations. */
6788 amt = dynsymcount * 2 * sizeof (unsigned long int);
6789 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
6790 if (cinfo.hashcodes == NULL)
6791 return FALSE;
6792
6793 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6794 cinfo.min_dynindx = -1;
6795 cinfo.output_bfd = output_bfd;
6796 cinfo.bed = bed;
6797
6798 /* Put all hash values in HASHCODES. */
6799 elf_link_hash_traverse (elf_hash_table (info),
6800 elf_collect_gnu_hash_codes, &cinfo);
6801 if (cinfo.error)
6802 {
6803 free (cinfo.hashcodes);
6804 return FALSE;
6805 }
6806
6807 bucketcount
6808 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6809
6810 if (bucketcount == 0)
6811 {
6812 free (cinfo.hashcodes);
6813 return FALSE;
6814 }
6815
6816 s = bfd_get_linker_section (dynobj, ".gnu.hash");
6817 BFD_ASSERT (s != NULL);
6818
6819 if (cinfo.nsyms == 0)
6820 {
6821 /* Empty .gnu.hash section is special. */
6822 BFD_ASSERT (cinfo.min_dynindx == -1);
6823 free (cinfo.hashcodes);
6824 s->size = 5 * 4 + bed->s->arch_size / 8;
6825 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6826 if (contents == NULL)
6827 return FALSE;
6828 s->contents = contents;
6829 /* 1 empty bucket. */
6830 bfd_put_32 (output_bfd, 1, contents);
6831 /* SYMIDX above the special symbol 0. */
6832 bfd_put_32 (output_bfd, 1, contents + 4);
6833 /* Just one word for bitmask. */
6834 bfd_put_32 (output_bfd, 1, contents + 8);
6835 /* Only hash fn bloom filter. */
6836 bfd_put_32 (output_bfd, 0, contents + 12);
6837 /* No hashes are valid - empty bitmask. */
6838 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6839 /* No hashes in the only bucket. */
6840 bfd_put_32 (output_bfd, 0,
6841 contents + 16 + bed->s->arch_size / 8);
6842 }
6843 else
6844 {
6845 unsigned long int maskwords, maskbitslog2, x;
6846 BFD_ASSERT (cinfo.min_dynindx != -1);
6847
6848 x = cinfo.nsyms;
6849 maskbitslog2 = 1;
6850 while ((x >>= 1) != 0)
6851 ++maskbitslog2;
6852 if (maskbitslog2 < 3)
6853 maskbitslog2 = 5;
6854 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6855 maskbitslog2 = maskbitslog2 + 3;
6856 else
6857 maskbitslog2 = maskbitslog2 + 2;
6858 if (bed->s->arch_size == 64)
6859 {
6860 if (maskbitslog2 == 5)
6861 maskbitslog2 = 6;
6862 cinfo.shift1 = 6;
6863 }
6864 else
6865 cinfo.shift1 = 5;
6866 cinfo.mask = (1 << cinfo.shift1) - 1;
6867 cinfo.shift2 = maskbitslog2;
6868 cinfo.maskbits = 1 << maskbitslog2;
6869 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6870 amt = bucketcount * sizeof (unsigned long int) * 2;
6871 amt += maskwords * sizeof (bfd_vma);
6872 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
6873 if (cinfo.bitmask == NULL)
6874 {
6875 free (cinfo.hashcodes);
6876 return FALSE;
6877 }
6878
6879 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
6880 cinfo.indx = cinfo.counts + bucketcount;
6881 cinfo.symindx = dynsymcount - cinfo.nsyms;
6882 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6883
6884 /* Determine how often each hash bucket is used. */
6885 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6886 for (i = 0; i < cinfo.nsyms; ++i)
6887 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6888
6889 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6890 if (cinfo.counts[i] != 0)
6891 {
6892 cinfo.indx[i] = cnt;
6893 cnt += cinfo.counts[i];
6894 }
6895 BFD_ASSERT (cnt == dynsymcount);
6896 cinfo.bucketcount = bucketcount;
6897 cinfo.local_indx = cinfo.min_dynindx;
6898
6899 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6900 s->size += cinfo.maskbits / 8;
6901 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6902 if (contents == NULL)
6903 {
6904 free (cinfo.bitmask);
6905 free (cinfo.hashcodes);
6906 return FALSE;
6907 }
6908
6909 s->contents = contents;
6910 bfd_put_32 (output_bfd, bucketcount, contents);
6911 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6912 bfd_put_32 (output_bfd, maskwords, contents + 8);
6913 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6914 contents += 16 + cinfo.maskbits / 8;
6915
6916 for (i = 0; i < bucketcount; ++i)
6917 {
6918 if (cinfo.counts[i] == 0)
6919 bfd_put_32 (output_bfd, 0, contents);
6920 else
6921 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6922 contents += 4;
6923 }
6924
6925 cinfo.contents = contents;
6926
6927 /* Renumber dynamic symbols, populate .gnu.hash section. */
6928 elf_link_hash_traverse (elf_hash_table (info),
6929 elf_renumber_gnu_hash_syms, &cinfo);
6930
6931 contents = s->contents + 16;
6932 for (i = 0; i < maskwords; ++i)
6933 {
6934 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6935 contents);
6936 contents += bed->s->arch_size / 8;
6937 }
6938
6939 free (cinfo.bitmask);
6940 free (cinfo.hashcodes);
6941 }
6942 }
6943
6944 s = bfd_get_linker_section (dynobj, ".dynstr");
6945 BFD_ASSERT (s != NULL);
6946
6947 elf_finalize_dynstr (output_bfd, info);
6948
6949 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6950
6951 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6952 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6953 return FALSE;
6954 }
6955
6956 return TRUE;
6957 }
6958 \f
6959 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
6960
6961 static void
6962 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6963 asection *sec)
6964 {
6965 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
6966 sec->sec_info_type = SEC_INFO_TYPE_NONE;
6967 }
6968
6969 /* Finish SHF_MERGE section merging. */
6970
6971 bfd_boolean
6972 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
6973 {
6974 bfd *ibfd;
6975 asection *sec;
6976
6977 if (!is_elf_hash_table (info->hash))
6978 return FALSE;
6979
6980 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6981 if ((ibfd->flags & DYNAMIC) == 0
6982 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6983 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
6984 == get_elf_backend_data (obfd)->s->elfclass))
6985 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6986 if ((sec->flags & SEC_MERGE) != 0
6987 && !bfd_is_abs_section (sec->output_section))
6988 {
6989 struct bfd_elf_section_data *secdata;
6990
6991 secdata = elf_section_data (sec);
6992 if (! _bfd_add_merge_section (obfd,
6993 &elf_hash_table (info)->merge_info,
6994 sec, &secdata->sec_info))
6995 return FALSE;
6996 else if (secdata->sec_info)
6997 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
6998 }
6999
7000 if (elf_hash_table (info)->merge_info != NULL)
7001 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7002 merge_sections_remove_hook);
7003 return TRUE;
7004 }
7005
7006 /* Create an entry in an ELF linker hash table. */
7007
7008 struct bfd_hash_entry *
7009 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7010 struct bfd_hash_table *table,
7011 const char *string)
7012 {
7013 /* Allocate the structure if it has not already been allocated by a
7014 subclass. */
7015 if (entry == NULL)
7016 {
7017 entry = (struct bfd_hash_entry *)
7018 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7019 if (entry == NULL)
7020 return entry;
7021 }
7022
7023 /* Call the allocation method of the superclass. */
7024 entry = _bfd_link_hash_newfunc (entry, table, string);
7025 if (entry != NULL)
7026 {
7027 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7028 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7029
7030 /* Set local fields. */
7031 ret->indx = -1;
7032 ret->dynindx = -1;
7033 ret->got = htab->init_got_refcount;
7034 ret->plt = htab->init_plt_refcount;
7035 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7036 - offsetof (struct elf_link_hash_entry, size)));
7037 /* Assume that we have been called by a non-ELF symbol reader.
7038 This flag is then reset by the code which reads an ELF input
7039 file. This ensures that a symbol created by a non-ELF symbol
7040 reader will have the flag set correctly. */
7041 ret->non_elf = 1;
7042 }
7043
7044 return entry;
7045 }
7046
7047 /* Copy data from an indirect symbol to its direct symbol, hiding the
7048 old indirect symbol. Also used for copying flags to a weakdef. */
7049
7050 void
7051 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7052 struct elf_link_hash_entry *dir,
7053 struct elf_link_hash_entry *ind)
7054 {
7055 struct elf_link_hash_table *htab;
7056
7057 /* Copy down any references that we may have already seen to the
7058 symbol which just became indirect if DIR isn't a hidden versioned
7059 symbol. */
7060
7061 if (dir->versioned != versioned_hidden)
7062 {
7063 dir->ref_dynamic |= ind->ref_dynamic;
7064 dir->ref_regular |= ind->ref_regular;
7065 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7066 dir->non_got_ref |= ind->non_got_ref;
7067 dir->needs_plt |= ind->needs_plt;
7068 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7069 }
7070
7071 if (ind->root.type != bfd_link_hash_indirect)
7072 return;
7073
7074 /* Copy over the global and procedure linkage table refcount entries.
7075 These may have been already set up by a check_relocs routine. */
7076 htab = elf_hash_table (info);
7077 if (ind->got.refcount > htab->init_got_refcount.refcount)
7078 {
7079 if (dir->got.refcount < 0)
7080 dir->got.refcount = 0;
7081 dir->got.refcount += ind->got.refcount;
7082 ind->got.refcount = htab->init_got_refcount.refcount;
7083 }
7084
7085 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7086 {
7087 if (dir->plt.refcount < 0)
7088 dir->plt.refcount = 0;
7089 dir->plt.refcount += ind->plt.refcount;
7090 ind->plt.refcount = htab->init_plt_refcount.refcount;
7091 }
7092
7093 if (ind->dynindx != -1)
7094 {
7095 if (dir->dynindx != -1)
7096 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7097 dir->dynindx = ind->dynindx;
7098 dir->dynstr_index = ind->dynstr_index;
7099 ind->dynindx = -1;
7100 ind->dynstr_index = 0;
7101 }
7102 }
7103
7104 void
7105 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7106 struct elf_link_hash_entry *h,
7107 bfd_boolean force_local)
7108 {
7109 /* STT_GNU_IFUNC symbol must go through PLT. */
7110 if (h->type != STT_GNU_IFUNC)
7111 {
7112 h->plt = elf_hash_table (info)->init_plt_offset;
7113 h->needs_plt = 0;
7114 }
7115 if (force_local)
7116 {
7117 h->forced_local = 1;
7118 if (h->dynindx != -1)
7119 {
7120 h->dynindx = -1;
7121 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7122 h->dynstr_index);
7123 }
7124 }
7125 }
7126
7127 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7128 caller. */
7129
7130 bfd_boolean
7131 _bfd_elf_link_hash_table_init
7132 (struct elf_link_hash_table *table,
7133 bfd *abfd,
7134 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7135 struct bfd_hash_table *,
7136 const char *),
7137 unsigned int entsize,
7138 enum elf_target_id target_id)
7139 {
7140 bfd_boolean ret;
7141 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7142
7143 table->init_got_refcount.refcount = can_refcount - 1;
7144 table->init_plt_refcount.refcount = can_refcount - 1;
7145 table->init_got_offset.offset = -(bfd_vma) 1;
7146 table->init_plt_offset.offset = -(bfd_vma) 1;
7147 /* The first dynamic symbol is a dummy. */
7148 table->dynsymcount = 1;
7149
7150 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7151
7152 table->root.type = bfd_link_elf_hash_table;
7153 table->hash_table_id = target_id;
7154
7155 return ret;
7156 }
7157
7158 /* Create an ELF linker hash table. */
7159
7160 struct bfd_link_hash_table *
7161 _bfd_elf_link_hash_table_create (bfd *abfd)
7162 {
7163 struct elf_link_hash_table *ret;
7164 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7165
7166 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7167 if (ret == NULL)
7168 return NULL;
7169
7170 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7171 sizeof (struct elf_link_hash_entry),
7172 GENERIC_ELF_DATA))
7173 {
7174 free (ret);
7175 return NULL;
7176 }
7177 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7178
7179 return &ret->root;
7180 }
7181
7182 /* Destroy an ELF linker hash table. */
7183
7184 void
7185 _bfd_elf_link_hash_table_free (bfd *obfd)
7186 {
7187 struct elf_link_hash_table *htab;
7188
7189 htab = (struct elf_link_hash_table *) obfd->link.hash;
7190 if (htab->dynstr != NULL)
7191 _bfd_elf_strtab_free (htab->dynstr);
7192 _bfd_merge_sections_free (htab->merge_info);
7193 _bfd_generic_link_hash_table_free (obfd);
7194 }
7195
7196 /* This is a hook for the ELF emulation code in the generic linker to
7197 tell the backend linker what file name to use for the DT_NEEDED
7198 entry for a dynamic object. */
7199
7200 void
7201 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7202 {
7203 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7204 && bfd_get_format (abfd) == bfd_object)
7205 elf_dt_name (abfd) = name;
7206 }
7207
7208 int
7209 bfd_elf_get_dyn_lib_class (bfd *abfd)
7210 {
7211 int lib_class;
7212 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7213 && bfd_get_format (abfd) == bfd_object)
7214 lib_class = elf_dyn_lib_class (abfd);
7215 else
7216 lib_class = 0;
7217 return lib_class;
7218 }
7219
7220 void
7221 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7222 {
7223 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7224 && bfd_get_format (abfd) == bfd_object)
7225 elf_dyn_lib_class (abfd) = lib_class;
7226 }
7227
7228 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7229 the linker ELF emulation code. */
7230
7231 struct bfd_link_needed_list *
7232 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7233 struct bfd_link_info *info)
7234 {
7235 if (! is_elf_hash_table (info->hash))
7236 return NULL;
7237 return elf_hash_table (info)->needed;
7238 }
7239
7240 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7241 hook for the linker ELF emulation code. */
7242
7243 struct bfd_link_needed_list *
7244 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7245 struct bfd_link_info *info)
7246 {
7247 if (! is_elf_hash_table (info->hash))
7248 return NULL;
7249 return elf_hash_table (info)->runpath;
7250 }
7251
7252 /* Get the name actually used for a dynamic object for a link. This
7253 is the SONAME entry if there is one. Otherwise, it is the string
7254 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7255
7256 const char *
7257 bfd_elf_get_dt_soname (bfd *abfd)
7258 {
7259 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7260 && bfd_get_format (abfd) == bfd_object)
7261 return elf_dt_name (abfd);
7262 return NULL;
7263 }
7264
7265 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7266 the ELF linker emulation code. */
7267
7268 bfd_boolean
7269 bfd_elf_get_bfd_needed_list (bfd *abfd,
7270 struct bfd_link_needed_list **pneeded)
7271 {
7272 asection *s;
7273 bfd_byte *dynbuf = NULL;
7274 unsigned int elfsec;
7275 unsigned long shlink;
7276 bfd_byte *extdyn, *extdynend;
7277 size_t extdynsize;
7278 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7279
7280 *pneeded = NULL;
7281
7282 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7283 || bfd_get_format (abfd) != bfd_object)
7284 return TRUE;
7285
7286 s = bfd_get_section_by_name (abfd, ".dynamic");
7287 if (s == NULL || s->size == 0)
7288 return TRUE;
7289
7290 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7291 goto error_return;
7292
7293 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7294 if (elfsec == SHN_BAD)
7295 goto error_return;
7296
7297 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7298
7299 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7300 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7301
7302 extdyn = dynbuf;
7303 extdynend = extdyn + s->size;
7304 for (; extdyn < extdynend; extdyn += extdynsize)
7305 {
7306 Elf_Internal_Dyn dyn;
7307
7308 (*swap_dyn_in) (abfd, extdyn, &dyn);
7309
7310 if (dyn.d_tag == DT_NULL)
7311 break;
7312
7313 if (dyn.d_tag == DT_NEEDED)
7314 {
7315 const char *string;
7316 struct bfd_link_needed_list *l;
7317 unsigned int tagv = dyn.d_un.d_val;
7318 bfd_size_type amt;
7319
7320 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7321 if (string == NULL)
7322 goto error_return;
7323
7324 amt = sizeof *l;
7325 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7326 if (l == NULL)
7327 goto error_return;
7328
7329 l->by = abfd;
7330 l->name = string;
7331 l->next = *pneeded;
7332 *pneeded = l;
7333 }
7334 }
7335
7336 free (dynbuf);
7337
7338 return TRUE;
7339
7340 error_return:
7341 if (dynbuf != NULL)
7342 free (dynbuf);
7343 return FALSE;
7344 }
7345
7346 struct elf_symbuf_symbol
7347 {
7348 unsigned long st_name; /* Symbol name, index in string tbl */
7349 unsigned char st_info; /* Type and binding attributes */
7350 unsigned char st_other; /* Visibilty, and target specific */
7351 };
7352
7353 struct elf_symbuf_head
7354 {
7355 struct elf_symbuf_symbol *ssym;
7356 size_t count;
7357 unsigned int st_shndx;
7358 };
7359
7360 struct elf_symbol
7361 {
7362 union
7363 {
7364 Elf_Internal_Sym *isym;
7365 struct elf_symbuf_symbol *ssym;
7366 } u;
7367 const char *name;
7368 };
7369
7370 /* Sort references to symbols by ascending section number. */
7371
7372 static int
7373 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7374 {
7375 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7376 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7377
7378 return s1->st_shndx - s2->st_shndx;
7379 }
7380
7381 static int
7382 elf_sym_name_compare (const void *arg1, const void *arg2)
7383 {
7384 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7385 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7386 return strcmp (s1->name, s2->name);
7387 }
7388
7389 static struct elf_symbuf_head *
7390 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
7391 {
7392 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7393 struct elf_symbuf_symbol *ssym;
7394 struct elf_symbuf_head *ssymbuf, *ssymhead;
7395 size_t i, shndx_count, total_size;
7396
7397 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7398 if (indbuf == NULL)
7399 return NULL;
7400
7401 for (ind = indbuf, i = 0; i < symcount; i++)
7402 if (isymbuf[i].st_shndx != SHN_UNDEF)
7403 *ind++ = &isymbuf[i];
7404 indbufend = ind;
7405
7406 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7407 elf_sort_elf_symbol);
7408
7409 shndx_count = 0;
7410 if (indbufend > indbuf)
7411 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7412 if (ind[0]->st_shndx != ind[1]->st_shndx)
7413 shndx_count++;
7414
7415 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7416 + (indbufend - indbuf) * sizeof (*ssym));
7417 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7418 if (ssymbuf == NULL)
7419 {
7420 free (indbuf);
7421 return NULL;
7422 }
7423
7424 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7425 ssymbuf->ssym = NULL;
7426 ssymbuf->count = shndx_count;
7427 ssymbuf->st_shndx = 0;
7428 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7429 {
7430 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7431 {
7432 ssymhead++;
7433 ssymhead->ssym = ssym;
7434 ssymhead->count = 0;
7435 ssymhead->st_shndx = (*ind)->st_shndx;
7436 }
7437 ssym->st_name = (*ind)->st_name;
7438 ssym->st_info = (*ind)->st_info;
7439 ssym->st_other = (*ind)->st_other;
7440 ssymhead->count++;
7441 }
7442 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
7443 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7444 == total_size));
7445
7446 free (indbuf);
7447 return ssymbuf;
7448 }
7449
7450 /* Check if 2 sections define the same set of local and global
7451 symbols. */
7452
7453 static bfd_boolean
7454 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7455 struct bfd_link_info *info)
7456 {
7457 bfd *bfd1, *bfd2;
7458 const struct elf_backend_data *bed1, *bed2;
7459 Elf_Internal_Shdr *hdr1, *hdr2;
7460 size_t symcount1, symcount2;
7461 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7462 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7463 Elf_Internal_Sym *isym, *isymend;
7464 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7465 size_t count1, count2, i;
7466 unsigned int shndx1, shndx2;
7467 bfd_boolean result;
7468
7469 bfd1 = sec1->owner;
7470 bfd2 = sec2->owner;
7471
7472 /* Both sections have to be in ELF. */
7473 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7474 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7475 return FALSE;
7476
7477 if (elf_section_type (sec1) != elf_section_type (sec2))
7478 return FALSE;
7479
7480 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7481 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7482 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7483 return FALSE;
7484
7485 bed1 = get_elf_backend_data (bfd1);
7486 bed2 = get_elf_backend_data (bfd2);
7487 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7488 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7489 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7490 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7491
7492 if (symcount1 == 0 || symcount2 == 0)
7493 return FALSE;
7494
7495 result = FALSE;
7496 isymbuf1 = NULL;
7497 isymbuf2 = NULL;
7498 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7499 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7500
7501 if (ssymbuf1 == NULL)
7502 {
7503 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7504 NULL, NULL, NULL);
7505 if (isymbuf1 == NULL)
7506 goto done;
7507
7508 if (!info->reduce_memory_overheads)
7509 elf_tdata (bfd1)->symbuf = ssymbuf1
7510 = elf_create_symbuf (symcount1, isymbuf1);
7511 }
7512
7513 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7514 {
7515 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7516 NULL, NULL, NULL);
7517 if (isymbuf2 == NULL)
7518 goto done;
7519
7520 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7521 elf_tdata (bfd2)->symbuf = ssymbuf2
7522 = elf_create_symbuf (symcount2, isymbuf2);
7523 }
7524
7525 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7526 {
7527 /* Optimized faster version. */
7528 size_t lo, hi, mid;
7529 struct elf_symbol *symp;
7530 struct elf_symbuf_symbol *ssym, *ssymend;
7531
7532 lo = 0;
7533 hi = ssymbuf1->count;
7534 ssymbuf1++;
7535 count1 = 0;
7536 while (lo < hi)
7537 {
7538 mid = (lo + hi) / 2;
7539 if (shndx1 < ssymbuf1[mid].st_shndx)
7540 hi = mid;
7541 else if (shndx1 > ssymbuf1[mid].st_shndx)
7542 lo = mid + 1;
7543 else
7544 {
7545 count1 = ssymbuf1[mid].count;
7546 ssymbuf1 += mid;
7547 break;
7548 }
7549 }
7550
7551 lo = 0;
7552 hi = ssymbuf2->count;
7553 ssymbuf2++;
7554 count2 = 0;
7555 while (lo < hi)
7556 {
7557 mid = (lo + hi) / 2;
7558 if (shndx2 < ssymbuf2[mid].st_shndx)
7559 hi = mid;
7560 else if (shndx2 > ssymbuf2[mid].st_shndx)
7561 lo = mid + 1;
7562 else
7563 {
7564 count2 = ssymbuf2[mid].count;
7565 ssymbuf2 += mid;
7566 break;
7567 }
7568 }
7569
7570 if (count1 == 0 || count2 == 0 || count1 != count2)
7571 goto done;
7572
7573 symtable1
7574 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7575 symtable2
7576 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
7577 if (symtable1 == NULL || symtable2 == NULL)
7578 goto done;
7579
7580 symp = symtable1;
7581 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7582 ssym < ssymend; ssym++, symp++)
7583 {
7584 symp->u.ssym = ssym;
7585 symp->name = bfd_elf_string_from_elf_section (bfd1,
7586 hdr1->sh_link,
7587 ssym->st_name);
7588 }
7589
7590 symp = symtable2;
7591 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7592 ssym < ssymend; ssym++, symp++)
7593 {
7594 symp->u.ssym = ssym;
7595 symp->name = bfd_elf_string_from_elf_section (bfd2,
7596 hdr2->sh_link,
7597 ssym->st_name);
7598 }
7599
7600 /* Sort symbol by name. */
7601 qsort (symtable1, count1, sizeof (struct elf_symbol),
7602 elf_sym_name_compare);
7603 qsort (symtable2, count1, sizeof (struct elf_symbol),
7604 elf_sym_name_compare);
7605
7606 for (i = 0; i < count1; i++)
7607 /* Two symbols must have the same binding, type and name. */
7608 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7609 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7610 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7611 goto done;
7612
7613 result = TRUE;
7614 goto done;
7615 }
7616
7617 symtable1 = (struct elf_symbol *)
7618 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7619 symtable2 = (struct elf_symbol *)
7620 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7621 if (symtable1 == NULL || symtable2 == NULL)
7622 goto done;
7623
7624 /* Count definitions in the section. */
7625 count1 = 0;
7626 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7627 if (isym->st_shndx == shndx1)
7628 symtable1[count1++].u.isym = isym;
7629
7630 count2 = 0;
7631 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7632 if (isym->st_shndx == shndx2)
7633 symtable2[count2++].u.isym = isym;
7634
7635 if (count1 == 0 || count2 == 0 || count1 != count2)
7636 goto done;
7637
7638 for (i = 0; i < count1; i++)
7639 symtable1[i].name
7640 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7641 symtable1[i].u.isym->st_name);
7642
7643 for (i = 0; i < count2; i++)
7644 symtable2[i].name
7645 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7646 symtable2[i].u.isym->st_name);
7647
7648 /* Sort symbol by name. */
7649 qsort (symtable1, count1, sizeof (struct elf_symbol),
7650 elf_sym_name_compare);
7651 qsort (symtable2, count1, sizeof (struct elf_symbol),
7652 elf_sym_name_compare);
7653
7654 for (i = 0; i < count1; i++)
7655 /* Two symbols must have the same binding, type and name. */
7656 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7657 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7658 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7659 goto done;
7660
7661 result = TRUE;
7662
7663 done:
7664 if (symtable1)
7665 free (symtable1);
7666 if (symtable2)
7667 free (symtable2);
7668 if (isymbuf1)
7669 free (isymbuf1);
7670 if (isymbuf2)
7671 free (isymbuf2);
7672
7673 return result;
7674 }
7675
7676 /* Return TRUE if 2 section types are compatible. */
7677
7678 bfd_boolean
7679 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7680 bfd *bbfd, const asection *bsec)
7681 {
7682 if (asec == NULL
7683 || bsec == NULL
7684 || abfd->xvec->flavour != bfd_target_elf_flavour
7685 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7686 return TRUE;
7687
7688 return elf_section_type (asec) == elf_section_type (bsec);
7689 }
7690 \f
7691 /* Final phase of ELF linker. */
7692
7693 /* A structure we use to avoid passing large numbers of arguments. */
7694
7695 struct elf_final_link_info
7696 {
7697 /* General link information. */
7698 struct bfd_link_info *info;
7699 /* Output BFD. */
7700 bfd *output_bfd;
7701 /* Symbol string table. */
7702 struct elf_strtab_hash *symstrtab;
7703 /* .hash section. */
7704 asection *hash_sec;
7705 /* symbol version section (.gnu.version). */
7706 asection *symver_sec;
7707 /* Buffer large enough to hold contents of any section. */
7708 bfd_byte *contents;
7709 /* Buffer large enough to hold external relocs of any section. */
7710 void *external_relocs;
7711 /* Buffer large enough to hold internal relocs of any section. */
7712 Elf_Internal_Rela *internal_relocs;
7713 /* Buffer large enough to hold external local symbols of any input
7714 BFD. */
7715 bfd_byte *external_syms;
7716 /* And a buffer for symbol section indices. */
7717 Elf_External_Sym_Shndx *locsym_shndx;
7718 /* Buffer large enough to hold internal local symbols of any input
7719 BFD. */
7720 Elf_Internal_Sym *internal_syms;
7721 /* Array large enough to hold a symbol index for each local symbol
7722 of any input BFD. */
7723 long *indices;
7724 /* Array large enough to hold a section pointer for each local
7725 symbol of any input BFD. */
7726 asection **sections;
7727 /* Buffer for SHT_SYMTAB_SHNDX section. */
7728 Elf_External_Sym_Shndx *symshndxbuf;
7729 /* Number of STT_FILE syms seen. */
7730 size_t filesym_count;
7731 };
7732
7733 /* This struct is used to pass information to elf_link_output_extsym. */
7734
7735 struct elf_outext_info
7736 {
7737 bfd_boolean failed;
7738 bfd_boolean localsyms;
7739 bfd_boolean file_sym_done;
7740 struct elf_final_link_info *flinfo;
7741 };
7742
7743
7744 /* Support for evaluating a complex relocation.
7745
7746 Complex relocations are generalized, self-describing relocations. The
7747 implementation of them consists of two parts: complex symbols, and the
7748 relocations themselves.
7749
7750 The relocations are use a reserved elf-wide relocation type code (R_RELC
7751 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7752 information (start bit, end bit, word width, etc) into the addend. This
7753 information is extracted from CGEN-generated operand tables within gas.
7754
7755 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7756 internal) representing prefix-notation expressions, including but not
7757 limited to those sorts of expressions normally encoded as addends in the
7758 addend field. The symbol mangling format is:
7759
7760 <node> := <literal>
7761 | <unary-operator> ':' <node>
7762 | <binary-operator> ':' <node> ':' <node>
7763 ;
7764
7765 <literal> := 's' <digits=N> ':' <N character symbol name>
7766 | 'S' <digits=N> ':' <N character section name>
7767 | '#' <hexdigits>
7768 ;
7769
7770 <binary-operator> := as in C
7771 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7772
7773 static void
7774 set_symbol_value (bfd *bfd_with_globals,
7775 Elf_Internal_Sym *isymbuf,
7776 size_t locsymcount,
7777 size_t symidx,
7778 bfd_vma val)
7779 {
7780 struct elf_link_hash_entry **sym_hashes;
7781 struct elf_link_hash_entry *h;
7782 size_t extsymoff = locsymcount;
7783
7784 if (symidx < locsymcount)
7785 {
7786 Elf_Internal_Sym *sym;
7787
7788 sym = isymbuf + symidx;
7789 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7790 {
7791 /* It is a local symbol: move it to the
7792 "absolute" section and give it a value. */
7793 sym->st_shndx = SHN_ABS;
7794 sym->st_value = val;
7795 return;
7796 }
7797 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7798 extsymoff = 0;
7799 }
7800
7801 /* It is a global symbol: set its link type
7802 to "defined" and give it a value. */
7803
7804 sym_hashes = elf_sym_hashes (bfd_with_globals);
7805 h = sym_hashes [symidx - extsymoff];
7806 while (h->root.type == bfd_link_hash_indirect
7807 || h->root.type == bfd_link_hash_warning)
7808 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7809 h->root.type = bfd_link_hash_defined;
7810 h->root.u.def.value = val;
7811 h->root.u.def.section = bfd_abs_section_ptr;
7812 }
7813
7814 static bfd_boolean
7815 resolve_symbol (const char *name,
7816 bfd *input_bfd,
7817 struct elf_final_link_info *flinfo,
7818 bfd_vma *result,
7819 Elf_Internal_Sym *isymbuf,
7820 size_t locsymcount)
7821 {
7822 Elf_Internal_Sym *sym;
7823 struct bfd_link_hash_entry *global_entry;
7824 const char *candidate = NULL;
7825 Elf_Internal_Shdr *symtab_hdr;
7826 size_t i;
7827
7828 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7829
7830 for (i = 0; i < locsymcount; ++ i)
7831 {
7832 sym = isymbuf + i;
7833
7834 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7835 continue;
7836
7837 candidate = bfd_elf_string_from_elf_section (input_bfd,
7838 symtab_hdr->sh_link,
7839 sym->st_name);
7840 #ifdef DEBUG
7841 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7842 name, candidate, (unsigned long) sym->st_value);
7843 #endif
7844 if (candidate && strcmp (candidate, name) == 0)
7845 {
7846 asection *sec = flinfo->sections [i];
7847
7848 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7849 *result += sec->output_offset + sec->output_section->vma;
7850 #ifdef DEBUG
7851 printf ("Found symbol with value %8.8lx\n",
7852 (unsigned long) *result);
7853 #endif
7854 return TRUE;
7855 }
7856 }
7857
7858 /* Hmm, haven't found it yet. perhaps it is a global. */
7859 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
7860 FALSE, FALSE, TRUE);
7861 if (!global_entry)
7862 return FALSE;
7863
7864 if (global_entry->type == bfd_link_hash_defined
7865 || global_entry->type == bfd_link_hash_defweak)
7866 {
7867 *result = (global_entry->u.def.value
7868 + global_entry->u.def.section->output_section->vma
7869 + global_entry->u.def.section->output_offset);
7870 #ifdef DEBUG
7871 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7872 global_entry->root.string, (unsigned long) *result);
7873 #endif
7874 return TRUE;
7875 }
7876
7877 return FALSE;
7878 }
7879
7880 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
7881 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
7882 names like "foo.end" which is the end address of section "foo". */
7883
7884 static bfd_boolean
7885 resolve_section (const char *name,
7886 asection *sections,
7887 bfd_vma *result,
7888 bfd * abfd)
7889 {
7890 asection *curr;
7891 unsigned int len;
7892
7893 for (curr = sections; curr; curr = curr->next)
7894 if (strcmp (curr->name, name) == 0)
7895 {
7896 *result = curr->vma;
7897 return TRUE;
7898 }
7899
7900 /* Hmm. still haven't found it. try pseudo-section names. */
7901 /* FIXME: This could be coded more efficiently... */
7902 for (curr = sections; curr; curr = curr->next)
7903 {
7904 len = strlen (curr->name);
7905 if (len > strlen (name))
7906 continue;
7907
7908 if (strncmp (curr->name, name, len) == 0)
7909 {
7910 if (strncmp (".end", name + len, 4) == 0)
7911 {
7912 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
7913 return TRUE;
7914 }
7915
7916 /* Insert more pseudo-section names here, if you like. */
7917 }
7918 }
7919
7920 return FALSE;
7921 }
7922
7923 static void
7924 undefined_reference (const char *reftype, const char *name)
7925 {
7926 /* xgettext:c-format */
7927 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7928 reftype, name);
7929 }
7930
7931 static bfd_boolean
7932 eval_symbol (bfd_vma *result,
7933 const char **symp,
7934 bfd *input_bfd,
7935 struct elf_final_link_info *flinfo,
7936 bfd_vma dot,
7937 Elf_Internal_Sym *isymbuf,
7938 size_t locsymcount,
7939 int signed_p)
7940 {
7941 size_t len;
7942 size_t symlen;
7943 bfd_vma a;
7944 bfd_vma b;
7945 char symbuf[4096];
7946 const char *sym = *symp;
7947 const char *symend;
7948 bfd_boolean symbol_is_section = FALSE;
7949
7950 len = strlen (sym);
7951 symend = sym + len;
7952
7953 if (len < 1 || len > sizeof (symbuf))
7954 {
7955 bfd_set_error (bfd_error_invalid_operation);
7956 return FALSE;
7957 }
7958
7959 switch (* sym)
7960 {
7961 case '.':
7962 *result = dot;
7963 *symp = sym + 1;
7964 return TRUE;
7965
7966 case '#':
7967 ++sym;
7968 *result = strtoul (sym, (char **) symp, 16);
7969 return TRUE;
7970
7971 case 'S':
7972 symbol_is_section = TRUE;
7973 /* Fall through. */
7974 case 's':
7975 ++sym;
7976 symlen = strtol (sym, (char **) symp, 10);
7977 sym = *symp + 1; /* Skip the trailing ':'. */
7978
7979 if (symend < sym || symlen + 1 > sizeof (symbuf))
7980 {
7981 bfd_set_error (bfd_error_invalid_operation);
7982 return FALSE;
7983 }
7984
7985 memcpy (symbuf, sym, symlen);
7986 symbuf[symlen] = '\0';
7987 *symp = sym + symlen;
7988
7989 /* Is it always possible, with complex symbols, that gas "mis-guessed"
7990 the symbol as a section, or vice-versa. so we're pretty liberal in our
7991 interpretation here; section means "try section first", not "must be a
7992 section", and likewise with symbol. */
7993
7994 if (symbol_is_section)
7995 {
7996 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
7997 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
7998 isymbuf, locsymcount))
7999 {
8000 undefined_reference ("section", symbuf);
8001 return FALSE;
8002 }
8003 }
8004 else
8005 {
8006 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8007 isymbuf, locsymcount)
8008 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8009 result, input_bfd))
8010 {
8011 undefined_reference ("symbol", symbuf);
8012 return FALSE;
8013 }
8014 }
8015
8016 return TRUE;
8017
8018 /* All that remains are operators. */
8019
8020 #define UNARY_OP(op) \
8021 if (strncmp (sym, #op, strlen (#op)) == 0) \
8022 { \
8023 sym += strlen (#op); \
8024 if (*sym == ':') \
8025 ++sym; \
8026 *symp = sym; \
8027 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8028 isymbuf, locsymcount, signed_p)) \
8029 return FALSE; \
8030 if (signed_p) \
8031 *result = op ((bfd_signed_vma) a); \
8032 else \
8033 *result = op a; \
8034 return TRUE; \
8035 }
8036
8037 #define BINARY_OP(op) \
8038 if (strncmp (sym, #op, strlen (#op)) == 0) \
8039 { \
8040 sym += strlen (#op); \
8041 if (*sym == ':') \
8042 ++sym; \
8043 *symp = sym; \
8044 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8045 isymbuf, locsymcount, signed_p)) \
8046 return FALSE; \
8047 ++*symp; \
8048 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8049 isymbuf, locsymcount, signed_p)) \
8050 return FALSE; \
8051 if (signed_p) \
8052 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8053 else \
8054 *result = a op b; \
8055 return TRUE; \
8056 }
8057
8058 default:
8059 UNARY_OP (0-);
8060 BINARY_OP (<<);
8061 BINARY_OP (>>);
8062 BINARY_OP (==);
8063 BINARY_OP (!=);
8064 BINARY_OP (<=);
8065 BINARY_OP (>=);
8066 BINARY_OP (&&);
8067 BINARY_OP (||);
8068 UNARY_OP (~);
8069 UNARY_OP (!);
8070 BINARY_OP (*);
8071 BINARY_OP (/);
8072 BINARY_OP (%);
8073 BINARY_OP (^);
8074 BINARY_OP (|);
8075 BINARY_OP (&);
8076 BINARY_OP (+);
8077 BINARY_OP (-);
8078 BINARY_OP (<);
8079 BINARY_OP (>);
8080 #undef UNARY_OP
8081 #undef BINARY_OP
8082 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8083 bfd_set_error (bfd_error_invalid_operation);
8084 return FALSE;
8085 }
8086 }
8087
8088 static void
8089 put_value (bfd_vma size,
8090 unsigned long chunksz,
8091 bfd *input_bfd,
8092 bfd_vma x,
8093 bfd_byte *location)
8094 {
8095 location += (size - chunksz);
8096
8097 for (; size; size -= chunksz, location -= chunksz)
8098 {
8099 switch (chunksz)
8100 {
8101 case 1:
8102 bfd_put_8 (input_bfd, x, location);
8103 x >>= 8;
8104 break;
8105 case 2:
8106 bfd_put_16 (input_bfd, x, location);
8107 x >>= 16;
8108 break;
8109 case 4:
8110 bfd_put_32 (input_bfd, x, location);
8111 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8112 x >>= 16;
8113 x >>= 16;
8114 break;
8115 #ifdef BFD64
8116 case 8:
8117 bfd_put_64 (input_bfd, x, location);
8118 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8119 x >>= 32;
8120 x >>= 32;
8121 break;
8122 #endif
8123 default:
8124 abort ();
8125 break;
8126 }
8127 }
8128 }
8129
8130 static bfd_vma
8131 get_value (bfd_vma size,
8132 unsigned long chunksz,
8133 bfd *input_bfd,
8134 bfd_byte *location)
8135 {
8136 int shift;
8137 bfd_vma x = 0;
8138
8139 /* Sanity checks. */
8140 BFD_ASSERT (chunksz <= sizeof (x)
8141 && size >= chunksz
8142 && chunksz != 0
8143 && (size % chunksz) == 0
8144 && input_bfd != NULL
8145 && location != NULL);
8146
8147 if (chunksz == sizeof (x))
8148 {
8149 BFD_ASSERT (size == chunksz);
8150
8151 /* Make sure that we do not perform an undefined shift operation.
8152 We know that size == chunksz so there will only be one iteration
8153 of the loop below. */
8154 shift = 0;
8155 }
8156 else
8157 shift = 8 * chunksz;
8158
8159 for (; size; size -= chunksz, location += chunksz)
8160 {
8161 switch (chunksz)
8162 {
8163 case 1:
8164 x = (x << shift) | bfd_get_8 (input_bfd, location);
8165 break;
8166 case 2:
8167 x = (x << shift) | bfd_get_16 (input_bfd, location);
8168 break;
8169 case 4:
8170 x = (x << shift) | bfd_get_32 (input_bfd, location);
8171 break;
8172 #ifdef BFD64
8173 case 8:
8174 x = (x << shift) | bfd_get_64 (input_bfd, location);
8175 break;
8176 #endif
8177 default:
8178 abort ();
8179 }
8180 }
8181 return x;
8182 }
8183
8184 static void
8185 decode_complex_addend (unsigned long *start, /* in bits */
8186 unsigned long *oplen, /* in bits */
8187 unsigned long *len, /* in bits */
8188 unsigned long *wordsz, /* in bytes */
8189 unsigned long *chunksz, /* in bytes */
8190 unsigned long *lsb0_p,
8191 unsigned long *signed_p,
8192 unsigned long *trunc_p,
8193 unsigned long encoded)
8194 {
8195 * start = encoded & 0x3F;
8196 * len = (encoded >> 6) & 0x3F;
8197 * oplen = (encoded >> 12) & 0x3F;
8198 * wordsz = (encoded >> 18) & 0xF;
8199 * chunksz = (encoded >> 22) & 0xF;
8200 * lsb0_p = (encoded >> 27) & 1;
8201 * signed_p = (encoded >> 28) & 1;
8202 * trunc_p = (encoded >> 29) & 1;
8203 }
8204
8205 bfd_reloc_status_type
8206 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8207 asection *input_section ATTRIBUTE_UNUSED,
8208 bfd_byte *contents,
8209 Elf_Internal_Rela *rel,
8210 bfd_vma relocation)
8211 {
8212 bfd_vma shift, x, mask;
8213 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8214 bfd_reloc_status_type r;
8215
8216 /* Perform this reloc, since it is complex.
8217 (this is not to say that it necessarily refers to a complex
8218 symbol; merely that it is a self-describing CGEN based reloc.
8219 i.e. the addend has the complete reloc information (bit start, end,
8220 word size, etc) encoded within it.). */
8221
8222 decode_complex_addend (&start, &oplen, &len, &wordsz,
8223 &chunksz, &lsb0_p, &signed_p,
8224 &trunc_p, rel->r_addend);
8225
8226 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8227
8228 if (lsb0_p)
8229 shift = (start + 1) - len;
8230 else
8231 shift = (8 * wordsz) - (start + len);
8232
8233 x = get_value (wordsz, chunksz, input_bfd,
8234 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8235
8236 #ifdef DEBUG
8237 printf ("Doing complex reloc: "
8238 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8239 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8240 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8241 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8242 oplen, (unsigned long) x, (unsigned long) mask,
8243 (unsigned long) relocation);
8244 #endif
8245
8246 r = bfd_reloc_ok;
8247 if (! trunc_p)
8248 /* Now do an overflow check. */
8249 r = bfd_check_overflow ((signed_p
8250 ? complain_overflow_signed
8251 : complain_overflow_unsigned),
8252 len, 0, (8 * wordsz),
8253 relocation);
8254
8255 /* Do the deed. */
8256 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8257
8258 #ifdef DEBUG
8259 printf (" relocation: %8.8lx\n"
8260 " shifted mask: %8.8lx\n"
8261 " shifted/masked reloc: %8.8lx\n"
8262 " result: %8.8lx\n",
8263 (unsigned long) relocation, (unsigned long) (mask << shift),
8264 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8265 #endif
8266 put_value (wordsz, chunksz, input_bfd, x,
8267 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8268 return r;
8269 }
8270
8271 /* Functions to read r_offset from external (target order) reloc
8272 entry. Faster than bfd_getl32 et al, because we let the compiler
8273 know the value is aligned. */
8274
8275 static bfd_vma
8276 ext32l_r_offset (const void *p)
8277 {
8278 union aligned32
8279 {
8280 uint32_t v;
8281 unsigned char c[4];
8282 };
8283 const union aligned32 *a
8284 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8285
8286 uint32_t aval = ( (uint32_t) a->c[0]
8287 | (uint32_t) a->c[1] << 8
8288 | (uint32_t) a->c[2] << 16
8289 | (uint32_t) a->c[3] << 24);
8290 return aval;
8291 }
8292
8293 static bfd_vma
8294 ext32b_r_offset (const void *p)
8295 {
8296 union aligned32
8297 {
8298 uint32_t v;
8299 unsigned char c[4];
8300 };
8301 const union aligned32 *a
8302 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8303
8304 uint32_t aval = ( (uint32_t) a->c[0] << 24
8305 | (uint32_t) a->c[1] << 16
8306 | (uint32_t) a->c[2] << 8
8307 | (uint32_t) a->c[3]);
8308 return aval;
8309 }
8310
8311 #ifdef BFD_HOST_64_BIT
8312 static bfd_vma
8313 ext64l_r_offset (const void *p)
8314 {
8315 union aligned64
8316 {
8317 uint64_t v;
8318 unsigned char c[8];
8319 };
8320 const union aligned64 *a
8321 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8322
8323 uint64_t aval = ( (uint64_t) a->c[0]
8324 | (uint64_t) a->c[1] << 8
8325 | (uint64_t) a->c[2] << 16
8326 | (uint64_t) a->c[3] << 24
8327 | (uint64_t) a->c[4] << 32
8328 | (uint64_t) a->c[5] << 40
8329 | (uint64_t) a->c[6] << 48
8330 | (uint64_t) a->c[7] << 56);
8331 return aval;
8332 }
8333
8334 static bfd_vma
8335 ext64b_r_offset (const void *p)
8336 {
8337 union aligned64
8338 {
8339 uint64_t v;
8340 unsigned char c[8];
8341 };
8342 const union aligned64 *a
8343 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8344
8345 uint64_t aval = ( (uint64_t) a->c[0] << 56
8346 | (uint64_t) a->c[1] << 48
8347 | (uint64_t) a->c[2] << 40
8348 | (uint64_t) a->c[3] << 32
8349 | (uint64_t) a->c[4] << 24
8350 | (uint64_t) a->c[5] << 16
8351 | (uint64_t) a->c[6] << 8
8352 | (uint64_t) a->c[7]);
8353 return aval;
8354 }
8355 #endif
8356
8357 /* When performing a relocatable link, the input relocations are
8358 preserved. But, if they reference global symbols, the indices
8359 referenced must be updated. Update all the relocations found in
8360 RELDATA. */
8361
8362 static bfd_boolean
8363 elf_link_adjust_relocs (bfd *abfd,
8364 asection *sec,
8365 struct bfd_elf_section_reloc_data *reldata,
8366 bfd_boolean sort)
8367 {
8368 unsigned int i;
8369 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8370 bfd_byte *erela;
8371 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8372 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8373 bfd_vma r_type_mask;
8374 int r_sym_shift;
8375 unsigned int count = reldata->count;
8376 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8377
8378 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8379 {
8380 swap_in = bed->s->swap_reloc_in;
8381 swap_out = bed->s->swap_reloc_out;
8382 }
8383 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8384 {
8385 swap_in = bed->s->swap_reloca_in;
8386 swap_out = bed->s->swap_reloca_out;
8387 }
8388 else
8389 abort ();
8390
8391 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8392 abort ();
8393
8394 if (bed->s->arch_size == 32)
8395 {
8396 r_type_mask = 0xff;
8397 r_sym_shift = 8;
8398 }
8399 else
8400 {
8401 r_type_mask = 0xffffffff;
8402 r_sym_shift = 32;
8403 }
8404
8405 erela = reldata->hdr->contents;
8406 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8407 {
8408 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8409 unsigned int j;
8410
8411 if (*rel_hash == NULL)
8412 continue;
8413
8414 BFD_ASSERT ((*rel_hash)->indx >= 0);
8415
8416 (*swap_in) (abfd, erela, irela);
8417 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8418 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8419 | (irela[j].r_info & r_type_mask));
8420 (*swap_out) (abfd, irela, erela);
8421 }
8422
8423 if (bed->elf_backend_update_relocs)
8424 (*bed->elf_backend_update_relocs) (sec, reldata);
8425
8426 if (sort && count != 0)
8427 {
8428 bfd_vma (*ext_r_off) (const void *);
8429 bfd_vma r_off;
8430 size_t elt_size;
8431 bfd_byte *base, *end, *p, *loc;
8432 bfd_byte *buf = NULL;
8433
8434 if (bed->s->arch_size == 32)
8435 {
8436 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8437 ext_r_off = ext32l_r_offset;
8438 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8439 ext_r_off = ext32b_r_offset;
8440 else
8441 abort ();
8442 }
8443 else
8444 {
8445 #ifdef BFD_HOST_64_BIT
8446 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8447 ext_r_off = ext64l_r_offset;
8448 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8449 ext_r_off = ext64b_r_offset;
8450 else
8451 #endif
8452 abort ();
8453 }
8454
8455 /* Must use a stable sort here. A modified insertion sort,
8456 since the relocs are mostly sorted already. */
8457 elt_size = reldata->hdr->sh_entsize;
8458 base = reldata->hdr->contents;
8459 end = base + count * elt_size;
8460 if (elt_size > sizeof (Elf64_External_Rela))
8461 abort ();
8462
8463 /* Ensure the first element is lowest. This acts as a sentinel,
8464 speeding the main loop below. */
8465 r_off = (*ext_r_off) (base);
8466 for (p = loc = base; (p += elt_size) < end; )
8467 {
8468 bfd_vma r_off2 = (*ext_r_off) (p);
8469 if (r_off > r_off2)
8470 {
8471 r_off = r_off2;
8472 loc = p;
8473 }
8474 }
8475 if (loc != base)
8476 {
8477 /* Don't just swap *base and *loc as that changes the order
8478 of the original base[0] and base[1] if they happen to
8479 have the same r_offset. */
8480 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8481 memcpy (onebuf, loc, elt_size);
8482 memmove (base + elt_size, base, loc - base);
8483 memcpy (base, onebuf, elt_size);
8484 }
8485
8486 for (p = base + elt_size; (p += elt_size) < end; )
8487 {
8488 /* base to p is sorted, *p is next to insert. */
8489 r_off = (*ext_r_off) (p);
8490 /* Search the sorted region for location to insert. */
8491 loc = p - elt_size;
8492 while (r_off < (*ext_r_off) (loc))
8493 loc -= elt_size;
8494 loc += elt_size;
8495 if (loc != p)
8496 {
8497 /* Chances are there is a run of relocs to insert here,
8498 from one of more input files. Files are not always
8499 linked in order due to the way elf_link_input_bfd is
8500 called. See pr17666. */
8501 size_t sortlen = p - loc;
8502 bfd_vma r_off2 = (*ext_r_off) (loc);
8503 size_t runlen = elt_size;
8504 size_t buf_size = 96 * 1024;
8505 while (p + runlen < end
8506 && (sortlen <= buf_size
8507 || runlen + elt_size <= buf_size)
8508 && r_off2 > (*ext_r_off) (p + runlen))
8509 runlen += elt_size;
8510 if (buf == NULL)
8511 {
8512 buf = bfd_malloc (buf_size);
8513 if (buf == NULL)
8514 return FALSE;
8515 }
8516 if (runlen < sortlen)
8517 {
8518 memcpy (buf, p, runlen);
8519 memmove (loc + runlen, loc, sortlen);
8520 memcpy (loc, buf, runlen);
8521 }
8522 else
8523 {
8524 memcpy (buf, loc, sortlen);
8525 memmove (loc, p, runlen);
8526 memcpy (loc + runlen, buf, sortlen);
8527 }
8528 p += runlen - elt_size;
8529 }
8530 }
8531 /* Hashes are no longer valid. */
8532 free (reldata->hashes);
8533 reldata->hashes = NULL;
8534 free (buf);
8535 }
8536 return TRUE;
8537 }
8538
8539 struct elf_link_sort_rela
8540 {
8541 union {
8542 bfd_vma offset;
8543 bfd_vma sym_mask;
8544 } u;
8545 enum elf_reloc_type_class type;
8546 /* We use this as an array of size int_rels_per_ext_rel. */
8547 Elf_Internal_Rela rela[1];
8548 };
8549
8550 static int
8551 elf_link_sort_cmp1 (const void *A, const void *B)
8552 {
8553 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8554 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8555 int relativea, relativeb;
8556
8557 relativea = a->type == reloc_class_relative;
8558 relativeb = b->type == reloc_class_relative;
8559
8560 if (relativea < relativeb)
8561 return 1;
8562 if (relativea > relativeb)
8563 return -1;
8564 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8565 return -1;
8566 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8567 return 1;
8568 if (a->rela->r_offset < b->rela->r_offset)
8569 return -1;
8570 if (a->rela->r_offset > b->rela->r_offset)
8571 return 1;
8572 return 0;
8573 }
8574
8575 static int
8576 elf_link_sort_cmp2 (const void *A, const void *B)
8577 {
8578 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8579 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8580
8581 if (a->type < b->type)
8582 return -1;
8583 if (a->type > b->type)
8584 return 1;
8585 if (a->u.offset < b->u.offset)
8586 return -1;
8587 if (a->u.offset > b->u.offset)
8588 return 1;
8589 if (a->rela->r_offset < b->rela->r_offset)
8590 return -1;
8591 if (a->rela->r_offset > b->rela->r_offset)
8592 return 1;
8593 return 0;
8594 }
8595
8596 static size_t
8597 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8598 {
8599 asection *dynamic_relocs;
8600 asection *rela_dyn;
8601 asection *rel_dyn;
8602 bfd_size_type count, size;
8603 size_t i, ret, sort_elt, ext_size;
8604 bfd_byte *sort, *s_non_relative, *p;
8605 struct elf_link_sort_rela *sq;
8606 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8607 int i2e = bed->s->int_rels_per_ext_rel;
8608 unsigned int opb = bfd_octets_per_byte (abfd);
8609 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8610 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8611 struct bfd_link_order *lo;
8612 bfd_vma r_sym_mask;
8613 bfd_boolean use_rela;
8614
8615 /* Find a dynamic reloc section. */
8616 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8617 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8618 if (rela_dyn != NULL && rela_dyn->size > 0
8619 && rel_dyn != NULL && rel_dyn->size > 0)
8620 {
8621 bfd_boolean use_rela_initialised = FALSE;
8622
8623 /* This is just here to stop gcc from complaining.
8624 Its initialization checking code is not perfect. */
8625 use_rela = TRUE;
8626
8627 /* Both sections are present. Examine the sizes
8628 of the indirect sections to help us choose. */
8629 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8630 if (lo->type == bfd_indirect_link_order)
8631 {
8632 asection *o = lo->u.indirect.section;
8633
8634 if ((o->size % bed->s->sizeof_rela) == 0)
8635 {
8636 if ((o->size % bed->s->sizeof_rel) == 0)
8637 /* Section size is divisible by both rel and rela sizes.
8638 It is of no help to us. */
8639 ;
8640 else
8641 {
8642 /* Section size is only divisible by rela. */
8643 if (use_rela_initialised && (use_rela == FALSE))
8644 {
8645 _bfd_error_handler (_("%B: Unable to sort relocs - "
8646 "they are in more than one size"),
8647 abfd);
8648 bfd_set_error (bfd_error_invalid_operation);
8649 return 0;
8650 }
8651 else
8652 {
8653 use_rela = TRUE;
8654 use_rela_initialised = TRUE;
8655 }
8656 }
8657 }
8658 else if ((o->size % bed->s->sizeof_rel) == 0)
8659 {
8660 /* Section size is only divisible by rel. */
8661 if (use_rela_initialised && (use_rela == TRUE))
8662 {
8663 _bfd_error_handler (_("%B: Unable to sort relocs - "
8664 "they are in more than one size"),
8665 abfd);
8666 bfd_set_error (bfd_error_invalid_operation);
8667 return 0;
8668 }
8669 else
8670 {
8671 use_rela = FALSE;
8672 use_rela_initialised = TRUE;
8673 }
8674 }
8675 else
8676 {
8677 /* The section size is not divisible by either -
8678 something is wrong. */
8679 _bfd_error_handler (_("%B: Unable to sort relocs - "
8680 "they are of an unknown size"), abfd);
8681 bfd_set_error (bfd_error_invalid_operation);
8682 return 0;
8683 }
8684 }
8685
8686 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8687 if (lo->type == bfd_indirect_link_order)
8688 {
8689 asection *o = lo->u.indirect.section;
8690
8691 if ((o->size % bed->s->sizeof_rela) == 0)
8692 {
8693 if ((o->size % bed->s->sizeof_rel) == 0)
8694 /* Section size is divisible by both rel and rela sizes.
8695 It is of no help to us. */
8696 ;
8697 else
8698 {
8699 /* Section size is only divisible by rela. */
8700 if (use_rela_initialised && (use_rela == FALSE))
8701 {
8702 _bfd_error_handler (_("%B: Unable to sort relocs - "
8703 "they are in more than one size"),
8704 abfd);
8705 bfd_set_error (bfd_error_invalid_operation);
8706 return 0;
8707 }
8708 else
8709 {
8710 use_rela = TRUE;
8711 use_rela_initialised = TRUE;
8712 }
8713 }
8714 }
8715 else if ((o->size % bed->s->sizeof_rel) == 0)
8716 {
8717 /* Section size is only divisible by rel. */
8718 if (use_rela_initialised && (use_rela == TRUE))
8719 {
8720 _bfd_error_handler (_("%B: Unable to sort relocs - "
8721 "they are in more than one size"),
8722 abfd);
8723 bfd_set_error (bfd_error_invalid_operation);
8724 return 0;
8725 }
8726 else
8727 {
8728 use_rela = FALSE;
8729 use_rela_initialised = TRUE;
8730 }
8731 }
8732 else
8733 {
8734 /* The section size is not divisible by either -
8735 something is wrong. */
8736 _bfd_error_handler (_("%B: Unable to sort relocs - "
8737 "they are of an unknown size"), abfd);
8738 bfd_set_error (bfd_error_invalid_operation);
8739 return 0;
8740 }
8741 }
8742
8743 if (! use_rela_initialised)
8744 /* Make a guess. */
8745 use_rela = TRUE;
8746 }
8747 else if (rela_dyn != NULL && rela_dyn->size > 0)
8748 use_rela = TRUE;
8749 else if (rel_dyn != NULL && rel_dyn->size > 0)
8750 use_rela = FALSE;
8751 else
8752 return 0;
8753
8754 if (use_rela)
8755 {
8756 dynamic_relocs = rela_dyn;
8757 ext_size = bed->s->sizeof_rela;
8758 swap_in = bed->s->swap_reloca_in;
8759 swap_out = bed->s->swap_reloca_out;
8760 }
8761 else
8762 {
8763 dynamic_relocs = rel_dyn;
8764 ext_size = bed->s->sizeof_rel;
8765 swap_in = bed->s->swap_reloc_in;
8766 swap_out = bed->s->swap_reloc_out;
8767 }
8768
8769 size = 0;
8770 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8771 if (lo->type == bfd_indirect_link_order)
8772 size += lo->u.indirect.section->size;
8773
8774 if (size != dynamic_relocs->size)
8775 return 0;
8776
8777 sort_elt = (sizeof (struct elf_link_sort_rela)
8778 + (i2e - 1) * sizeof (Elf_Internal_Rela));
8779
8780 count = dynamic_relocs->size / ext_size;
8781 if (count == 0)
8782 return 0;
8783 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
8784
8785 if (sort == NULL)
8786 {
8787 (*info->callbacks->warning)
8788 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8789 return 0;
8790 }
8791
8792 if (bed->s->arch_size == 32)
8793 r_sym_mask = ~(bfd_vma) 0xff;
8794 else
8795 r_sym_mask = ~(bfd_vma) 0xffffffff;
8796
8797 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8798 if (lo->type == bfd_indirect_link_order)
8799 {
8800 bfd_byte *erel, *erelend;
8801 asection *o = lo->u.indirect.section;
8802
8803 if (o->contents == NULL && o->size != 0)
8804 {
8805 /* This is a reloc section that is being handled as a normal
8806 section. See bfd_section_from_shdr. We can't combine
8807 relocs in this case. */
8808 free (sort);
8809 return 0;
8810 }
8811 erel = o->contents;
8812 erelend = o->contents + o->size;
8813 p = sort + o->output_offset * opb / ext_size * sort_elt;
8814
8815 while (erel < erelend)
8816 {
8817 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8818
8819 (*swap_in) (abfd, erel, s->rela);
8820 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
8821 s->u.sym_mask = r_sym_mask;
8822 p += sort_elt;
8823 erel += ext_size;
8824 }
8825 }
8826
8827 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8828
8829 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8830 {
8831 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8832 if (s->type != reloc_class_relative)
8833 break;
8834 }
8835 ret = i;
8836 s_non_relative = p;
8837
8838 sq = (struct elf_link_sort_rela *) s_non_relative;
8839 for (; i < count; i++, p += sort_elt)
8840 {
8841 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8842 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8843 sq = sp;
8844 sp->u.offset = sq->rela->r_offset;
8845 }
8846
8847 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8848
8849 struct elf_link_hash_table *htab = elf_hash_table (info);
8850 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
8851 {
8852 /* We have plt relocs in .rela.dyn. */
8853 sq = (struct elf_link_sort_rela *) sort;
8854 for (i = 0; i < count; i++)
8855 if (sq[count - i - 1].type != reloc_class_plt)
8856 break;
8857 if (i != 0 && htab->srelplt->size == i * ext_size)
8858 {
8859 struct bfd_link_order **plo;
8860 /* Put srelplt link_order last. This is so the output_offset
8861 set in the next loop is correct for DT_JMPREL. */
8862 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
8863 if ((*plo)->type == bfd_indirect_link_order
8864 && (*plo)->u.indirect.section == htab->srelplt)
8865 {
8866 lo = *plo;
8867 *plo = lo->next;
8868 }
8869 else
8870 plo = &(*plo)->next;
8871 *plo = lo;
8872 lo->next = NULL;
8873 dynamic_relocs->map_tail.link_order = lo;
8874 }
8875 }
8876
8877 p = sort;
8878 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8879 if (lo->type == bfd_indirect_link_order)
8880 {
8881 bfd_byte *erel, *erelend;
8882 asection *o = lo->u.indirect.section;
8883
8884 erel = o->contents;
8885 erelend = o->contents + o->size;
8886 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
8887 while (erel < erelend)
8888 {
8889 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8890 (*swap_out) (abfd, s->rela, erel);
8891 p += sort_elt;
8892 erel += ext_size;
8893 }
8894 }
8895
8896 free (sort);
8897 *psec = dynamic_relocs;
8898 return ret;
8899 }
8900
8901 /* Add a symbol to the output symbol string table. */
8902
8903 static int
8904 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
8905 const char *name,
8906 Elf_Internal_Sym *elfsym,
8907 asection *input_sec,
8908 struct elf_link_hash_entry *h)
8909 {
8910 int (*output_symbol_hook)
8911 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8912 struct elf_link_hash_entry *);
8913 struct elf_link_hash_table *hash_table;
8914 const struct elf_backend_data *bed;
8915 bfd_size_type strtabsize;
8916
8917 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8918
8919 bed = get_elf_backend_data (flinfo->output_bfd);
8920 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8921 if (output_symbol_hook != NULL)
8922 {
8923 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
8924 if (ret != 1)
8925 return ret;
8926 }
8927
8928 if (name == NULL
8929 || *name == '\0'
8930 || (input_sec->flags & SEC_EXCLUDE))
8931 elfsym->st_name = (unsigned long) -1;
8932 else
8933 {
8934 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8935 to get the final offset for st_name. */
8936 elfsym->st_name
8937 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
8938 name, FALSE);
8939 if (elfsym->st_name == (unsigned long) -1)
8940 return 0;
8941 }
8942
8943 hash_table = elf_hash_table (flinfo->info);
8944 strtabsize = hash_table->strtabsize;
8945 if (strtabsize <= hash_table->strtabcount)
8946 {
8947 strtabsize += strtabsize;
8948 hash_table->strtabsize = strtabsize;
8949 strtabsize *= sizeof (*hash_table->strtab);
8950 hash_table->strtab
8951 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
8952 strtabsize);
8953 if (hash_table->strtab == NULL)
8954 return 0;
8955 }
8956 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
8957 hash_table->strtab[hash_table->strtabcount].dest_index
8958 = hash_table->strtabcount;
8959 hash_table->strtab[hash_table->strtabcount].destshndx_index
8960 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
8961
8962 bfd_get_symcount (flinfo->output_bfd) += 1;
8963 hash_table->strtabcount += 1;
8964
8965 return 1;
8966 }
8967
8968 /* Swap symbols out to the symbol table and flush the output symbols to
8969 the file. */
8970
8971 static bfd_boolean
8972 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
8973 {
8974 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
8975 bfd_size_type amt;
8976 size_t i;
8977 const struct elf_backend_data *bed;
8978 bfd_byte *symbuf;
8979 Elf_Internal_Shdr *hdr;
8980 file_ptr pos;
8981 bfd_boolean ret;
8982
8983 if (!hash_table->strtabcount)
8984 return TRUE;
8985
8986 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8987
8988 bed = get_elf_backend_data (flinfo->output_bfd);
8989
8990 amt = bed->s->sizeof_sym * hash_table->strtabcount;
8991 symbuf = (bfd_byte *) bfd_malloc (amt);
8992 if (symbuf == NULL)
8993 return FALSE;
8994
8995 if (flinfo->symshndxbuf)
8996 {
8997 amt = sizeof (Elf_External_Sym_Shndx);
8998 amt *= bfd_get_symcount (flinfo->output_bfd);
8999 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9000 if (flinfo->symshndxbuf == NULL)
9001 {
9002 free (symbuf);
9003 return FALSE;
9004 }
9005 }
9006
9007 for (i = 0; i < hash_table->strtabcount; i++)
9008 {
9009 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9010 if (elfsym->sym.st_name == (unsigned long) -1)
9011 elfsym->sym.st_name = 0;
9012 else
9013 elfsym->sym.st_name
9014 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9015 elfsym->sym.st_name);
9016 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9017 ((bfd_byte *) symbuf
9018 + (elfsym->dest_index
9019 * bed->s->sizeof_sym)),
9020 (flinfo->symshndxbuf
9021 + elfsym->destshndx_index));
9022 }
9023
9024 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9025 pos = hdr->sh_offset + hdr->sh_size;
9026 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9027 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9028 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9029 {
9030 hdr->sh_size += amt;
9031 ret = TRUE;
9032 }
9033 else
9034 ret = FALSE;
9035
9036 free (symbuf);
9037
9038 free (hash_table->strtab);
9039 hash_table->strtab = NULL;
9040
9041 return ret;
9042 }
9043
9044 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9045
9046 static bfd_boolean
9047 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9048 {
9049 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9050 && sym->st_shndx < SHN_LORESERVE)
9051 {
9052 /* The gABI doesn't support dynamic symbols in output sections
9053 beyond 64k. */
9054 _bfd_error_handler
9055 /* xgettext:c-format */
9056 (_("%B: Too many sections: %d (>= %d)"),
9057 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9058 bfd_set_error (bfd_error_nonrepresentable_section);
9059 return FALSE;
9060 }
9061 return TRUE;
9062 }
9063
9064 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9065 allowing an unsatisfied unversioned symbol in the DSO to match a
9066 versioned symbol that would normally require an explicit version.
9067 We also handle the case that a DSO references a hidden symbol
9068 which may be satisfied by a versioned symbol in another DSO. */
9069
9070 static bfd_boolean
9071 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9072 const struct elf_backend_data *bed,
9073 struct elf_link_hash_entry *h)
9074 {
9075 bfd *abfd;
9076 struct elf_link_loaded_list *loaded;
9077
9078 if (!is_elf_hash_table (info->hash))
9079 return FALSE;
9080
9081 /* Check indirect symbol. */
9082 while (h->root.type == bfd_link_hash_indirect)
9083 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9084
9085 switch (h->root.type)
9086 {
9087 default:
9088 abfd = NULL;
9089 break;
9090
9091 case bfd_link_hash_undefined:
9092 case bfd_link_hash_undefweak:
9093 abfd = h->root.u.undef.abfd;
9094 if (abfd == NULL
9095 || (abfd->flags & DYNAMIC) == 0
9096 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9097 return FALSE;
9098 break;
9099
9100 case bfd_link_hash_defined:
9101 case bfd_link_hash_defweak:
9102 abfd = h->root.u.def.section->owner;
9103 break;
9104
9105 case bfd_link_hash_common:
9106 abfd = h->root.u.c.p->section->owner;
9107 break;
9108 }
9109 BFD_ASSERT (abfd != NULL);
9110
9111 for (loaded = elf_hash_table (info)->loaded;
9112 loaded != NULL;
9113 loaded = loaded->next)
9114 {
9115 bfd *input;
9116 Elf_Internal_Shdr *hdr;
9117 size_t symcount;
9118 size_t extsymcount;
9119 size_t extsymoff;
9120 Elf_Internal_Shdr *versymhdr;
9121 Elf_Internal_Sym *isym;
9122 Elf_Internal_Sym *isymend;
9123 Elf_Internal_Sym *isymbuf;
9124 Elf_External_Versym *ever;
9125 Elf_External_Versym *extversym;
9126
9127 input = loaded->abfd;
9128
9129 /* We check each DSO for a possible hidden versioned definition. */
9130 if (input == abfd
9131 || (input->flags & DYNAMIC) == 0
9132 || elf_dynversym (input) == 0)
9133 continue;
9134
9135 hdr = &elf_tdata (input)->dynsymtab_hdr;
9136
9137 symcount = hdr->sh_size / bed->s->sizeof_sym;
9138 if (elf_bad_symtab (input))
9139 {
9140 extsymcount = symcount;
9141 extsymoff = 0;
9142 }
9143 else
9144 {
9145 extsymcount = symcount - hdr->sh_info;
9146 extsymoff = hdr->sh_info;
9147 }
9148
9149 if (extsymcount == 0)
9150 continue;
9151
9152 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9153 NULL, NULL, NULL);
9154 if (isymbuf == NULL)
9155 return FALSE;
9156
9157 /* Read in any version definitions. */
9158 versymhdr = &elf_tdata (input)->dynversym_hdr;
9159 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
9160 if (extversym == NULL)
9161 goto error_ret;
9162
9163 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9164 || (bfd_bread (extversym, versymhdr->sh_size, input)
9165 != versymhdr->sh_size))
9166 {
9167 free (extversym);
9168 error_ret:
9169 free (isymbuf);
9170 return FALSE;
9171 }
9172
9173 ever = extversym + extsymoff;
9174 isymend = isymbuf + extsymcount;
9175 for (isym = isymbuf; isym < isymend; isym++, ever++)
9176 {
9177 const char *name;
9178 Elf_Internal_Versym iver;
9179 unsigned short version_index;
9180
9181 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9182 || isym->st_shndx == SHN_UNDEF)
9183 continue;
9184
9185 name = bfd_elf_string_from_elf_section (input,
9186 hdr->sh_link,
9187 isym->st_name);
9188 if (strcmp (name, h->root.root.string) != 0)
9189 continue;
9190
9191 _bfd_elf_swap_versym_in (input, ever, &iver);
9192
9193 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9194 && !(h->def_regular
9195 && h->forced_local))
9196 {
9197 /* If we have a non-hidden versioned sym, then it should
9198 have provided a definition for the undefined sym unless
9199 it is defined in a non-shared object and forced local.
9200 */
9201 abort ();
9202 }
9203
9204 version_index = iver.vs_vers & VERSYM_VERSION;
9205 if (version_index == 1 || version_index == 2)
9206 {
9207 /* This is the base or first version. We can use it. */
9208 free (extversym);
9209 free (isymbuf);
9210 return TRUE;
9211 }
9212 }
9213
9214 free (extversym);
9215 free (isymbuf);
9216 }
9217
9218 return FALSE;
9219 }
9220
9221 /* Convert ELF common symbol TYPE. */
9222
9223 static int
9224 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9225 {
9226 /* Commom symbol can only appear in relocatable link. */
9227 if (!bfd_link_relocatable (info))
9228 abort ();
9229 switch (info->elf_stt_common)
9230 {
9231 case unchanged:
9232 break;
9233 case elf_stt_common:
9234 type = STT_COMMON;
9235 break;
9236 case no_elf_stt_common:
9237 type = STT_OBJECT;
9238 break;
9239 }
9240 return type;
9241 }
9242
9243 /* Add an external symbol to the symbol table. This is called from
9244 the hash table traversal routine. When generating a shared object,
9245 we go through the symbol table twice. The first time we output
9246 anything that might have been forced to local scope in a version
9247 script. The second time we output the symbols that are still
9248 global symbols. */
9249
9250 static bfd_boolean
9251 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9252 {
9253 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9254 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9255 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9256 bfd_boolean strip;
9257 Elf_Internal_Sym sym;
9258 asection *input_sec;
9259 const struct elf_backend_data *bed;
9260 long indx;
9261 int ret;
9262 unsigned int type;
9263
9264 if (h->root.type == bfd_link_hash_warning)
9265 {
9266 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9267 if (h->root.type == bfd_link_hash_new)
9268 return TRUE;
9269 }
9270
9271 /* Decide whether to output this symbol in this pass. */
9272 if (eoinfo->localsyms)
9273 {
9274 if (!h->forced_local)
9275 return TRUE;
9276 }
9277 else
9278 {
9279 if (h->forced_local)
9280 return TRUE;
9281 }
9282
9283 bed = get_elf_backend_data (flinfo->output_bfd);
9284
9285 if (h->root.type == bfd_link_hash_undefined)
9286 {
9287 /* If we have an undefined symbol reference here then it must have
9288 come from a shared library that is being linked in. (Undefined
9289 references in regular files have already been handled unless
9290 they are in unreferenced sections which are removed by garbage
9291 collection). */
9292 bfd_boolean ignore_undef = FALSE;
9293
9294 /* Some symbols may be special in that the fact that they're
9295 undefined can be safely ignored - let backend determine that. */
9296 if (bed->elf_backend_ignore_undef_symbol)
9297 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9298
9299 /* If we are reporting errors for this situation then do so now. */
9300 if (!ignore_undef
9301 && h->ref_dynamic
9302 && (!h->ref_regular || flinfo->info->gc_sections)
9303 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9304 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9305 (*flinfo->info->callbacks->undefined_symbol)
9306 (flinfo->info, h->root.root.string,
9307 h->ref_regular ? NULL : h->root.u.undef.abfd,
9308 NULL, 0,
9309 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
9310
9311 /* Strip a global symbol defined in a discarded section. */
9312 if (h->indx == -3)
9313 return TRUE;
9314 }
9315
9316 /* We should also warn if a forced local symbol is referenced from
9317 shared libraries. */
9318 if (bfd_link_executable (flinfo->info)
9319 && h->forced_local
9320 && h->ref_dynamic
9321 && h->def_regular
9322 && !h->dynamic_def
9323 && h->ref_dynamic_nonweak
9324 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9325 {
9326 bfd *def_bfd;
9327 const char *msg;
9328 struct elf_link_hash_entry *hi = h;
9329
9330 /* Check indirect symbol. */
9331 while (hi->root.type == bfd_link_hash_indirect)
9332 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9333
9334 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9335 /* xgettext:c-format */
9336 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9337 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9338 /* xgettext:c-format */
9339 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9340 else
9341 /* xgettext:c-format */
9342 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
9343 def_bfd = flinfo->output_bfd;
9344 if (hi->root.u.def.section != bfd_abs_section_ptr)
9345 def_bfd = hi->root.u.def.section->owner;
9346 _bfd_error_handler (msg, flinfo->output_bfd, def_bfd,
9347 h->root.root.string);
9348 bfd_set_error (bfd_error_bad_value);
9349 eoinfo->failed = TRUE;
9350 return FALSE;
9351 }
9352
9353 /* We don't want to output symbols that have never been mentioned by
9354 a regular file, or that we have been told to strip. However, if
9355 h->indx is set to -2, the symbol is used by a reloc and we must
9356 output it. */
9357 strip = FALSE;
9358 if (h->indx == -2)
9359 ;
9360 else if ((h->def_dynamic
9361 || h->ref_dynamic
9362 || h->root.type == bfd_link_hash_new)
9363 && !h->def_regular
9364 && !h->ref_regular)
9365 strip = TRUE;
9366 else if (flinfo->info->strip == strip_all)
9367 strip = TRUE;
9368 else if (flinfo->info->strip == strip_some
9369 && bfd_hash_lookup (flinfo->info->keep_hash,
9370 h->root.root.string, FALSE, FALSE) == NULL)
9371 strip = TRUE;
9372 else if ((h->root.type == bfd_link_hash_defined
9373 || h->root.type == bfd_link_hash_defweak)
9374 && ((flinfo->info->strip_discarded
9375 && discarded_section (h->root.u.def.section))
9376 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9377 && h->root.u.def.section->owner != NULL
9378 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9379 strip = TRUE;
9380 else if ((h->root.type == bfd_link_hash_undefined
9381 || h->root.type == bfd_link_hash_undefweak)
9382 && h->root.u.undef.abfd != NULL
9383 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9384 strip = TRUE;
9385
9386 type = h->type;
9387
9388 /* If we're stripping it, and it's not a dynamic symbol, there's
9389 nothing else to do. However, if it is a forced local symbol or
9390 an ifunc symbol we need to give the backend finish_dynamic_symbol
9391 function a chance to make it dynamic. */
9392 if (strip
9393 && h->dynindx == -1
9394 && type != STT_GNU_IFUNC
9395 && !h->forced_local)
9396 return TRUE;
9397
9398 sym.st_value = 0;
9399 sym.st_size = h->size;
9400 sym.st_other = h->other;
9401 switch (h->root.type)
9402 {
9403 default:
9404 case bfd_link_hash_new:
9405 case bfd_link_hash_warning:
9406 abort ();
9407 return FALSE;
9408
9409 case bfd_link_hash_undefined:
9410 case bfd_link_hash_undefweak:
9411 input_sec = bfd_und_section_ptr;
9412 sym.st_shndx = SHN_UNDEF;
9413 break;
9414
9415 case bfd_link_hash_defined:
9416 case bfd_link_hash_defweak:
9417 {
9418 input_sec = h->root.u.def.section;
9419 if (input_sec->output_section != NULL)
9420 {
9421 sym.st_shndx =
9422 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9423 input_sec->output_section);
9424 if (sym.st_shndx == SHN_BAD)
9425 {
9426 _bfd_error_handler
9427 /* xgettext:c-format */
9428 (_("%B: could not find output section %A for input section %A"),
9429 flinfo->output_bfd, input_sec->output_section, input_sec);
9430 bfd_set_error (bfd_error_nonrepresentable_section);
9431 eoinfo->failed = TRUE;
9432 return FALSE;
9433 }
9434
9435 /* ELF symbols in relocatable files are section relative,
9436 but in nonrelocatable files they are virtual
9437 addresses. */
9438 sym.st_value = h->root.u.def.value + input_sec->output_offset;
9439 if (!bfd_link_relocatable (flinfo->info))
9440 {
9441 sym.st_value += input_sec->output_section->vma;
9442 if (h->type == STT_TLS)
9443 {
9444 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9445 if (tls_sec != NULL)
9446 sym.st_value -= tls_sec->vma;
9447 }
9448 }
9449 }
9450 else
9451 {
9452 BFD_ASSERT (input_sec->owner == NULL
9453 || (input_sec->owner->flags & DYNAMIC) != 0);
9454 sym.st_shndx = SHN_UNDEF;
9455 input_sec = bfd_und_section_ptr;
9456 }
9457 }
9458 break;
9459
9460 case bfd_link_hash_common:
9461 input_sec = h->root.u.c.p->section;
9462 sym.st_shndx = bed->common_section_index (input_sec);
9463 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9464 break;
9465
9466 case bfd_link_hash_indirect:
9467 /* These symbols are created by symbol versioning. They point
9468 to the decorated version of the name. For example, if the
9469 symbol foo@@GNU_1.2 is the default, which should be used when
9470 foo is used with no version, then we add an indirect symbol
9471 foo which points to foo@@GNU_1.2. We ignore these symbols,
9472 since the indirected symbol is already in the hash table. */
9473 return TRUE;
9474 }
9475
9476 if (type == STT_COMMON || type == STT_OBJECT)
9477 switch (h->root.type)
9478 {
9479 case bfd_link_hash_common:
9480 type = elf_link_convert_common_type (flinfo->info, type);
9481 break;
9482 case bfd_link_hash_defined:
9483 case bfd_link_hash_defweak:
9484 if (bed->common_definition (&sym))
9485 type = elf_link_convert_common_type (flinfo->info, type);
9486 else
9487 type = STT_OBJECT;
9488 break;
9489 case bfd_link_hash_undefined:
9490 case bfd_link_hash_undefweak:
9491 break;
9492 default:
9493 abort ();
9494 }
9495
9496 if (h->forced_local)
9497 {
9498 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9499 /* Turn off visibility on local symbol. */
9500 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9501 }
9502 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9503 else if (h->unique_global && h->def_regular)
9504 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9505 else if (h->root.type == bfd_link_hash_undefweak
9506 || h->root.type == bfd_link_hash_defweak)
9507 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9508 else
9509 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9510 sym.st_target_internal = h->target_internal;
9511
9512 /* Give the processor backend a chance to tweak the symbol value,
9513 and also to finish up anything that needs to be done for this
9514 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9515 forced local syms when non-shared is due to a historical quirk.
9516 STT_GNU_IFUNC symbol must go through PLT. */
9517 if ((h->type == STT_GNU_IFUNC
9518 && h->def_regular
9519 && !bfd_link_relocatable (flinfo->info))
9520 || ((h->dynindx != -1
9521 || h->forced_local)
9522 && ((bfd_link_pic (flinfo->info)
9523 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9524 || h->root.type != bfd_link_hash_undefweak))
9525 || !h->forced_local)
9526 && elf_hash_table (flinfo->info)->dynamic_sections_created))
9527 {
9528 if (! ((*bed->elf_backend_finish_dynamic_symbol)
9529 (flinfo->output_bfd, flinfo->info, h, &sym)))
9530 {
9531 eoinfo->failed = TRUE;
9532 return FALSE;
9533 }
9534 }
9535
9536 /* If we are marking the symbol as undefined, and there are no
9537 non-weak references to this symbol from a regular object, then
9538 mark the symbol as weak undefined; if there are non-weak
9539 references, mark the symbol as strong. We can't do this earlier,
9540 because it might not be marked as undefined until the
9541 finish_dynamic_symbol routine gets through with it. */
9542 if (sym.st_shndx == SHN_UNDEF
9543 && h->ref_regular
9544 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9545 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9546 {
9547 int bindtype;
9548 type = ELF_ST_TYPE (sym.st_info);
9549
9550 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9551 if (type == STT_GNU_IFUNC)
9552 type = STT_FUNC;
9553
9554 if (h->ref_regular_nonweak)
9555 bindtype = STB_GLOBAL;
9556 else
9557 bindtype = STB_WEAK;
9558 sym.st_info = ELF_ST_INFO (bindtype, type);
9559 }
9560
9561 /* If this is a symbol defined in a dynamic library, don't use the
9562 symbol size from the dynamic library. Relinking an executable
9563 against a new library may introduce gratuitous changes in the
9564 executable's symbols if we keep the size. */
9565 if (sym.st_shndx == SHN_UNDEF
9566 && !h->def_regular
9567 && h->def_dynamic)
9568 sym.st_size = 0;
9569
9570 /* If a non-weak symbol with non-default visibility is not defined
9571 locally, it is a fatal error. */
9572 if (!bfd_link_relocatable (flinfo->info)
9573 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9574 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9575 && h->root.type == bfd_link_hash_undefined
9576 && !h->def_regular)
9577 {
9578 const char *msg;
9579
9580 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9581 /* xgettext:c-format */
9582 msg = _("%B: protected symbol `%s' isn't defined");
9583 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9584 /* xgettext:c-format */
9585 msg = _("%B: internal symbol `%s' isn't defined");
9586 else
9587 /* xgettext:c-format */
9588 msg = _("%B: hidden symbol `%s' isn't defined");
9589 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
9590 bfd_set_error (bfd_error_bad_value);
9591 eoinfo->failed = TRUE;
9592 return FALSE;
9593 }
9594
9595 /* If this symbol should be put in the .dynsym section, then put it
9596 there now. We already know the symbol index. We also fill in
9597 the entry in the .hash section. */
9598 if (elf_hash_table (flinfo->info)->dynsym != NULL
9599 && h->dynindx != -1
9600 && elf_hash_table (flinfo->info)->dynamic_sections_created)
9601 {
9602 bfd_byte *esym;
9603
9604 /* Since there is no version information in the dynamic string,
9605 if there is no version info in symbol version section, we will
9606 have a run-time problem if not linking executable, referenced
9607 by shared library, or not bound locally. */
9608 if (h->verinfo.verdef == NULL
9609 && (!bfd_link_executable (flinfo->info)
9610 || h->ref_dynamic
9611 || !h->def_regular))
9612 {
9613 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9614
9615 if (p && p [1] != '\0')
9616 {
9617 _bfd_error_handler
9618 /* xgettext:c-format */
9619 (_("%B: No symbol version section for versioned symbol `%s'"),
9620 flinfo->output_bfd, h->root.root.string);
9621 eoinfo->failed = TRUE;
9622 return FALSE;
9623 }
9624 }
9625
9626 sym.st_name = h->dynstr_index;
9627 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9628 + h->dynindx * bed->s->sizeof_sym);
9629 if (!check_dynsym (flinfo->output_bfd, &sym))
9630 {
9631 eoinfo->failed = TRUE;
9632 return FALSE;
9633 }
9634 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
9635
9636 if (flinfo->hash_sec != NULL)
9637 {
9638 size_t hash_entry_size;
9639 bfd_byte *bucketpos;
9640 bfd_vma chain;
9641 size_t bucketcount;
9642 size_t bucket;
9643
9644 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
9645 bucket = h->u.elf_hash_value % bucketcount;
9646
9647 hash_entry_size
9648 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9649 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
9650 + (bucket + 2) * hash_entry_size);
9651 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9652 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9653 bucketpos);
9654 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9655 ((bfd_byte *) flinfo->hash_sec->contents
9656 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9657 }
9658
9659 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
9660 {
9661 Elf_Internal_Versym iversym;
9662 Elf_External_Versym *eversym;
9663
9664 if (!h->def_regular)
9665 {
9666 if (h->verinfo.verdef == NULL
9667 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9668 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
9669 iversym.vs_vers = 0;
9670 else
9671 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9672 }
9673 else
9674 {
9675 if (h->verinfo.vertree == NULL)
9676 iversym.vs_vers = 1;
9677 else
9678 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
9679 if (flinfo->info->create_default_symver)
9680 iversym.vs_vers++;
9681 }
9682
9683 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9684 defined locally. */
9685 if (h->versioned == versioned_hidden && h->def_regular)
9686 iversym.vs_vers |= VERSYM_HIDDEN;
9687
9688 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
9689 eversym += h->dynindx;
9690 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
9691 }
9692 }
9693
9694 /* If the symbol is undefined, and we didn't output it to .dynsym,
9695 strip it from .symtab too. Obviously we can't do this for
9696 relocatable output or when needed for --emit-relocs. */
9697 else if (input_sec == bfd_und_section_ptr
9698 && h->indx != -2
9699 && !bfd_link_relocatable (flinfo->info))
9700 return TRUE;
9701 /* Also strip others that we couldn't earlier due to dynamic symbol
9702 processing. */
9703 if (strip)
9704 return TRUE;
9705 if ((input_sec->flags & SEC_EXCLUDE) != 0)
9706 return TRUE;
9707
9708 /* Output a FILE symbol so that following locals are not associated
9709 with the wrong input file. We need one for forced local symbols
9710 if we've seen more than one FILE symbol or when we have exactly
9711 one FILE symbol but global symbols are present in a file other
9712 than the one with the FILE symbol. We also need one if linker
9713 defined symbols are present. In practice these conditions are
9714 always met, so just emit the FILE symbol unconditionally. */
9715 if (eoinfo->localsyms
9716 && !eoinfo->file_sym_done
9717 && eoinfo->flinfo->filesym_count != 0)
9718 {
9719 Elf_Internal_Sym fsym;
9720
9721 memset (&fsym, 0, sizeof (fsym));
9722 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9723 fsym.st_shndx = SHN_ABS;
9724 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9725 bfd_und_section_ptr, NULL))
9726 return FALSE;
9727
9728 eoinfo->file_sym_done = TRUE;
9729 }
9730
9731 indx = bfd_get_symcount (flinfo->output_bfd);
9732 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9733 input_sec, h);
9734 if (ret == 0)
9735 {
9736 eoinfo->failed = TRUE;
9737 return FALSE;
9738 }
9739 else if (ret == 1)
9740 h->indx = indx;
9741 else if (h->indx == -2)
9742 abort();
9743
9744 return TRUE;
9745 }
9746
9747 /* Return TRUE if special handling is done for relocs in SEC against
9748 symbols defined in discarded sections. */
9749
9750 static bfd_boolean
9751 elf_section_ignore_discarded_relocs (asection *sec)
9752 {
9753 const struct elf_backend_data *bed;
9754
9755 switch (sec->sec_info_type)
9756 {
9757 case SEC_INFO_TYPE_STABS:
9758 case SEC_INFO_TYPE_EH_FRAME:
9759 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
9760 return TRUE;
9761 default:
9762 break;
9763 }
9764
9765 bed = get_elf_backend_data (sec->owner);
9766 if (bed->elf_backend_ignore_discarded_relocs != NULL
9767 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9768 return TRUE;
9769
9770 return FALSE;
9771 }
9772
9773 /* Return a mask saying how ld should treat relocations in SEC against
9774 symbols defined in discarded sections. If this function returns
9775 COMPLAIN set, ld will issue a warning message. If this function
9776 returns PRETEND set, and the discarded section was link-once and the
9777 same size as the kept link-once section, ld will pretend that the
9778 symbol was actually defined in the kept section. Otherwise ld will
9779 zero the reloc (at least that is the intent, but some cooperation by
9780 the target dependent code is needed, particularly for REL targets). */
9781
9782 unsigned int
9783 _bfd_elf_default_action_discarded (asection *sec)
9784 {
9785 if (sec->flags & SEC_DEBUGGING)
9786 return PRETEND;
9787
9788 if (strcmp (".eh_frame", sec->name) == 0)
9789 return 0;
9790
9791 if (strcmp (".gcc_except_table", sec->name) == 0)
9792 return 0;
9793
9794 return COMPLAIN | PRETEND;
9795 }
9796
9797 /* Find a match between a section and a member of a section group. */
9798
9799 static asection *
9800 match_group_member (asection *sec, asection *group,
9801 struct bfd_link_info *info)
9802 {
9803 asection *first = elf_next_in_group (group);
9804 asection *s = first;
9805
9806 while (s != NULL)
9807 {
9808 if (bfd_elf_match_symbols_in_sections (s, sec, info))
9809 return s;
9810
9811 s = elf_next_in_group (s);
9812 if (s == first)
9813 break;
9814 }
9815
9816 return NULL;
9817 }
9818
9819 /* Check if the kept section of a discarded section SEC can be used
9820 to replace it. Return the replacement if it is OK. Otherwise return
9821 NULL. */
9822
9823 asection *
9824 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
9825 {
9826 asection *kept;
9827
9828 kept = sec->kept_section;
9829 if (kept != NULL)
9830 {
9831 if ((kept->flags & SEC_GROUP) != 0)
9832 kept = match_group_member (sec, kept, info);
9833 if (kept != NULL
9834 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9835 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
9836 kept = NULL;
9837 sec->kept_section = kept;
9838 }
9839 return kept;
9840 }
9841
9842 /* Link an input file into the linker output file. This function
9843 handles all the sections and relocations of the input file at once.
9844 This is so that we only have to read the local symbols once, and
9845 don't have to keep them in memory. */
9846
9847 static bfd_boolean
9848 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
9849 {
9850 int (*relocate_section)
9851 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9852 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9853 bfd *output_bfd;
9854 Elf_Internal_Shdr *symtab_hdr;
9855 size_t locsymcount;
9856 size_t extsymoff;
9857 Elf_Internal_Sym *isymbuf;
9858 Elf_Internal_Sym *isym;
9859 Elf_Internal_Sym *isymend;
9860 long *pindex;
9861 asection **ppsection;
9862 asection *o;
9863 const struct elf_backend_data *bed;
9864 struct elf_link_hash_entry **sym_hashes;
9865 bfd_size_type address_size;
9866 bfd_vma r_type_mask;
9867 int r_sym_shift;
9868 bfd_boolean have_file_sym = FALSE;
9869
9870 output_bfd = flinfo->output_bfd;
9871 bed = get_elf_backend_data (output_bfd);
9872 relocate_section = bed->elf_backend_relocate_section;
9873
9874 /* If this is a dynamic object, we don't want to do anything here:
9875 we don't want the local symbols, and we don't want the section
9876 contents. */
9877 if ((input_bfd->flags & DYNAMIC) != 0)
9878 return TRUE;
9879
9880 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9881 if (elf_bad_symtab (input_bfd))
9882 {
9883 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9884 extsymoff = 0;
9885 }
9886 else
9887 {
9888 locsymcount = symtab_hdr->sh_info;
9889 extsymoff = symtab_hdr->sh_info;
9890 }
9891
9892 /* Read the local symbols. */
9893 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9894 if (isymbuf == NULL && locsymcount != 0)
9895 {
9896 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
9897 flinfo->internal_syms,
9898 flinfo->external_syms,
9899 flinfo->locsym_shndx);
9900 if (isymbuf == NULL)
9901 return FALSE;
9902 }
9903
9904 /* Find local symbol sections and adjust values of symbols in
9905 SEC_MERGE sections. Write out those local symbols we know are
9906 going into the output file. */
9907 isymend = isymbuf + locsymcount;
9908 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
9909 isym < isymend;
9910 isym++, pindex++, ppsection++)
9911 {
9912 asection *isec;
9913 const char *name;
9914 Elf_Internal_Sym osym;
9915 long indx;
9916 int ret;
9917
9918 *pindex = -1;
9919
9920 if (elf_bad_symtab (input_bfd))
9921 {
9922 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9923 {
9924 *ppsection = NULL;
9925 continue;
9926 }
9927 }
9928
9929 if (isym->st_shndx == SHN_UNDEF)
9930 isec = bfd_und_section_ptr;
9931 else if (isym->st_shndx == SHN_ABS)
9932 isec = bfd_abs_section_ptr;
9933 else if (isym->st_shndx == SHN_COMMON)
9934 isec = bfd_com_section_ptr;
9935 else
9936 {
9937 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9938 if (isec == NULL)
9939 {
9940 /* Don't attempt to output symbols with st_shnx in the
9941 reserved range other than SHN_ABS and SHN_COMMON. */
9942 *ppsection = NULL;
9943 continue;
9944 }
9945 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
9946 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9947 isym->st_value =
9948 _bfd_merged_section_offset (output_bfd, &isec,
9949 elf_section_data (isec)->sec_info,
9950 isym->st_value);
9951 }
9952
9953 *ppsection = isec;
9954
9955 /* Don't output the first, undefined, symbol. In fact, don't
9956 output any undefined local symbol. */
9957 if (isec == bfd_und_section_ptr)
9958 continue;
9959
9960 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9961 {
9962 /* We never output section symbols. Instead, we use the
9963 section symbol of the corresponding section in the output
9964 file. */
9965 continue;
9966 }
9967
9968 /* If we are stripping all symbols, we don't want to output this
9969 one. */
9970 if (flinfo->info->strip == strip_all)
9971 continue;
9972
9973 /* If we are discarding all local symbols, we don't want to
9974 output this one. If we are generating a relocatable output
9975 file, then some of the local symbols may be required by
9976 relocs; we output them below as we discover that they are
9977 needed. */
9978 if (flinfo->info->discard == discard_all)
9979 continue;
9980
9981 /* If this symbol is defined in a section which we are
9982 discarding, we don't need to keep it. */
9983 if (isym->st_shndx != SHN_UNDEF
9984 && isym->st_shndx < SHN_LORESERVE
9985 && bfd_section_removed_from_list (output_bfd,
9986 isec->output_section))
9987 continue;
9988
9989 /* Get the name of the symbol. */
9990 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9991 isym->st_name);
9992 if (name == NULL)
9993 return FALSE;
9994
9995 /* See if we are discarding symbols with this name. */
9996 if ((flinfo->info->strip == strip_some
9997 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
9998 == NULL))
9999 || (((flinfo->info->discard == discard_sec_merge
10000 && (isec->flags & SEC_MERGE)
10001 && !bfd_link_relocatable (flinfo->info))
10002 || flinfo->info->discard == discard_l)
10003 && bfd_is_local_label_name (input_bfd, name)))
10004 continue;
10005
10006 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10007 {
10008 if (input_bfd->lto_output)
10009 /* -flto puts a temp file name here. This means builds
10010 are not reproducible. Discard the symbol. */
10011 continue;
10012 have_file_sym = TRUE;
10013 flinfo->filesym_count += 1;
10014 }
10015 if (!have_file_sym)
10016 {
10017 /* In the absence of debug info, bfd_find_nearest_line uses
10018 FILE symbols to determine the source file for local
10019 function symbols. Provide a FILE symbol here if input
10020 files lack such, so that their symbols won't be
10021 associated with a previous input file. It's not the
10022 source file, but the best we can do. */
10023 have_file_sym = TRUE;
10024 flinfo->filesym_count += 1;
10025 memset (&osym, 0, sizeof (osym));
10026 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10027 osym.st_shndx = SHN_ABS;
10028 if (!elf_link_output_symstrtab (flinfo,
10029 (input_bfd->lto_output ? NULL
10030 : input_bfd->filename),
10031 &osym, bfd_abs_section_ptr,
10032 NULL))
10033 return FALSE;
10034 }
10035
10036 osym = *isym;
10037
10038 /* Adjust the section index for the output file. */
10039 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10040 isec->output_section);
10041 if (osym.st_shndx == SHN_BAD)
10042 return FALSE;
10043
10044 /* ELF symbols in relocatable files are section relative, but
10045 in executable files they are virtual addresses. Note that
10046 this code assumes that all ELF sections have an associated
10047 BFD section with a reasonable value for output_offset; below
10048 we assume that they also have a reasonable value for
10049 output_section. Any special sections must be set up to meet
10050 these requirements. */
10051 osym.st_value += isec->output_offset;
10052 if (!bfd_link_relocatable (flinfo->info))
10053 {
10054 osym.st_value += isec->output_section->vma;
10055 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10056 {
10057 /* STT_TLS symbols are relative to PT_TLS segment base. */
10058 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10059 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10060 }
10061 }
10062
10063 indx = bfd_get_symcount (output_bfd);
10064 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10065 if (ret == 0)
10066 return FALSE;
10067 else if (ret == 1)
10068 *pindex = indx;
10069 }
10070
10071 if (bed->s->arch_size == 32)
10072 {
10073 r_type_mask = 0xff;
10074 r_sym_shift = 8;
10075 address_size = 4;
10076 }
10077 else
10078 {
10079 r_type_mask = 0xffffffff;
10080 r_sym_shift = 32;
10081 address_size = 8;
10082 }
10083
10084 /* Relocate the contents of each section. */
10085 sym_hashes = elf_sym_hashes (input_bfd);
10086 for (o = input_bfd->sections; o != NULL; o = o->next)
10087 {
10088 bfd_byte *contents;
10089
10090 if (! o->linker_mark)
10091 {
10092 /* This section was omitted from the link. */
10093 continue;
10094 }
10095
10096 if (bfd_link_relocatable (flinfo->info)
10097 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10098 {
10099 /* Deal with the group signature symbol. */
10100 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10101 unsigned long symndx = sec_data->this_hdr.sh_info;
10102 asection *osec = o->output_section;
10103
10104 if (symndx >= locsymcount
10105 || (elf_bad_symtab (input_bfd)
10106 && flinfo->sections[symndx] == NULL))
10107 {
10108 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10109 while (h->root.type == bfd_link_hash_indirect
10110 || h->root.type == bfd_link_hash_warning)
10111 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10112 /* Arrange for symbol to be output. */
10113 h->indx = -2;
10114 elf_section_data (osec)->this_hdr.sh_info = -2;
10115 }
10116 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10117 {
10118 /* We'll use the output section target_index. */
10119 asection *sec = flinfo->sections[symndx]->output_section;
10120 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10121 }
10122 else
10123 {
10124 if (flinfo->indices[symndx] == -1)
10125 {
10126 /* Otherwise output the local symbol now. */
10127 Elf_Internal_Sym sym = isymbuf[symndx];
10128 asection *sec = flinfo->sections[symndx]->output_section;
10129 const char *name;
10130 long indx;
10131 int ret;
10132
10133 name = bfd_elf_string_from_elf_section (input_bfd,
10134 symtab_hdr->sh_link,
10135 sym.st_name);
10136 if (name == NULL)
10137 return FALSE;
10138
10139 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10140 sec);
10141 if (sym.st_shndx == SHN_BAD)
10142 return FALSE;
10143
10144 sym.st_value += o->output_offset;
10145
10146 indx = bfd_get_symcount (output_bfd);
10147 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10148 NULL);
10149 if (ret == 0)
10150 return FALSE;
10151 else if (ret == 1)
10152 flinfo->indices[symndx] = indx;
10153 else
10154 abort ();
10155 }
10156 elf_section_data (osec)->this_hdr.sh_info
10157 = flinfo->indices[symndx];
10158 }
10159 }
10160
10161 if ((o->flags & SEC_HAS_CONTENTS) == 0
10162 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10163 continue;
10164
10165 if ((o->flags & SEC_LINKER_CREATED) != 0)
10166 {
10167 /* Section was created by _bfd_elf_link_create_dynamic_sections
10168 or somesuch. */
10169 continue;
10170 }
10171
10172 /* Get the contents of the section. They have been cached by a
10173 relaxation routine. Note that o is a section in an input
10174 file, so the contents field will not have been set by any of
10175 the routines which work on output files. */
10176 if (elf_section_data (o)->this_hdr.contents != NULL)
10177 {
10178 contents = elf_section_data (o)->this_hdr.contents;
10179 if (bed->caches_rawsize
10180 && o->rawsize != 0
10181 && o->rawsize < o->size)
10182 {
10183 memcpy (flinfo->contents, contents, o->rawsize);
10184 contents = flinfo->contents;
10185 }
10186 }
10187 else
10188 {
10189 contents = flinfo->contents;
10190 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10191 return FALSE;
10192 }
10193
10194 if ((o->flags & SEC_RELOC) != 0)
10195 {
10196 Elf_Internal_Rela *internal_relocs;
10197 Elf_Internal_Rela *rel, *relend;
10198 int action_discarded;
10199 int ret;
10200
10201 /* Get the swapped relocs. */
10202 internal_relocs
10203 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10204 flinfo->internal_relocs, FALSE);
10205 if (internal_relocs == NULL
10206 && o->reloc_count > 0)
10207 return FALSE;
10208
10209 /* We need to reverse-copy input .ctors/.dtors sections if
10210 they are placed in .init_array/.finit_array for output. */
10211 if (o->size > address_size
10212 && ((strncmp (o->name, ".ctors", 6) == 0
10213 && strcmp (o->output_section->name,
10214 ".init_array") == 0)
10215 || (strncmp (o->name, ".dtors", 6) == 0
10216 && strcmp (o->output_section->name,
10217 ".fini_array") == 0))
10218 && (o->name[6] == 0 || o->name[6] == '.'))
10219 {
10220 if (o->size != o->reloc_count * address_size)
10221 {
10222 _bfd_error_handler
10223 /* xgettext:c-format */
10224 (_("error: %B: size of section %A is not "
10225 "multiple of address size"),
10226 input_bfd, o);
10227 bfd_set_error (bfd_error_on_input);
10228 return FALSE;
10229 }
10230 o->flags |= SEC_ELF_REVERSE_COPY;
10231 }
10232
10233 action_discarded = -1;
10234 if (!elf_section_ignore_discarded_relocs (o))
10235 action_discarded = (*bed->action_discarded) (o);
10236
10237 /* Run through the relocs evaluating complex reloc symbols and
10238 looking for relocs against symbols from discarded sections
10239 or section symbols from removed link-once sections.
10240 Complain about relocs against discarded sections. Zero
10241 relocs against removed link-once sections. */
10242
10243 rel = internal_relocs;
10244 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
10245 for ( ; rel < relend; rel++)
10246 {
10247 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10248 unsigned int s_type;
10249 asection **ps, *sec;
10250 struct elf_link_hash_entry *h = NULL;
10251 const char *sym_name;
10252
10253 if (r_symndx == STN_UNDEF)
10254 continue;
10255
10256 if (r_symndx >= locsymcount
10257 || (elf_bad_symtab (input_bfd)
10258 && flinfo->sections[r_symndx] == NULL))
10259 {
10260 h = sym_hashes[r_symndx - extsymoff];
10261
10262 /* Badly formatted input files can contain relocs that
10263 reference non-existant symbols. Check here so that
10264 we do not seg fault. */
10265 if (h == NULL)
10266 {
10267 char buffer [32];
10268
10269 sprintf_vma (buffer, rel->r_info);
10270 _bfd_error_handler
10271 /* xgettext:c-format */
10272 (_("error: %B contains a reloc (0x%s) for section %A "
10273 "that references a non-existent global symbol"),
10274 input_bfd, o, buffer);
10275 bfd_set_error (bfd_error_bad_value);
10276 return FALSE;
10277 }
10278
10279 while (h->root.type == bfd_link_hash_indirect
10280 || h->root.type == bfd_link_hash_warning)
10281 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10282
10283 s_type = h->type;
10284
10285 /* If a plugin symbol is referenced from a non-IR file,
10286 mark the symbol as undefined. Note that the
10287 linker may attach linker created dynamic sections
10288 to the plugin bfd. Symbols defined in linker
10289 created sections are not plugin symbols. */
10290 if (h->root.non_ir_ref
10291 && (h->root.type == bfd_link_hash_defined
10292 || h->root.type == bfd_link_hash_defweak)
10293 && (h->root.u.def.section->flags
10294 & SEC_LINKER_CREATED) == 0
10295 && h->root.u.def.section->owner != NULL
10296 && (h->root.u.def.section->owner->flags
10297 & BFD_PLUGIN) != 0)
10298 {
10299 h->root.type = bfd_link_hash_undefined;
10300 h->root.u.undef.abfd = h->root.u.def.section->owner;
10301 }
10302
10303 ps = NULL;
10304 if (h->root.type == bfd_link_hash_defined
10305 || h->root.type == bfd_link_hash_defweak)
10306 ps = &h->root.u.def.section;
10307
10308 sym_name = h->root.root.string;
10309 }
10310 else
10311 {
10312 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10313
10314 s_type = ELF_ST_TYPE (sym->st_info);
10315 ps = &flinfo->sections[r_symndx];
10316 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10317 sym, *ps);
10318 }
10319
10320 if ((s_type == STT_RELC || s_type == STT_SRELC)
10321 && !bfd_link_relocatable (flinfo->info))
10322 {
10323 bfd_vma val;
10324 bfd_vma dot = (rel->r_offset
10325 + o->output_offset + o->output_section->vma);
10326 #ifdef DEBUG
10327 printf ("Encountered a complex symbol!");
10328 printf (" (input_bfd %s, section %s, reloc %ld\n",
10329 input_bfd->filename, o->name,
10330 (long) (rel - internal_relocs));
10331 printf (" symbol: idx %8.8lx, name %s\n",
10332 r_symndx, sym_name);
10333 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10334 (unsigned long) rel->r_info,
10335 (unsigned long) rel->r_offset);
10336 #endif
10337 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10338 isymbuf, locsymcount, s_type == STT_SRELC))
10339 return FALSE;
10340
10341 /* Symbol evaluated OK. Update to absolute value. */
10342 set_symbol_value (input_bfd, isymbuf, locsymcount,
10343 r_symndx, val);
10344 continue;
10345 }
10346
10347 if (action_discarded != -1 && ps != NULL)
10348 {
10349 /* Complain if the definition comes from a
10350 discarded section. */
10351 if ((sec = *ps) != NULL && discarded_section (sec))
10352 {
10353 BFD_ASSERT (r_symndx != STN_UNDEF);
10354 if (action_discarded & COMPLAIN)
10355 (*flinfo->info->callbacks->einfo)
10356 /* xgettext:c-format */
10357 (_("%X`%s' referenced in section `%A' of %B: "
10358 "defined in discarded section `%A' of %B\n"),
10359 sym_name, o, input_bfd, sec, sec->owner);
10360
10361 /* Try to do the best we can to support buggy old
10362 versions of gcc. Pretend that the symbol is
10363 really defined in the kept linkonce section.
10364 FIXME: This is quite broken. Modifying the
10365 symbol here means we will be changing all later
10366 uses of the symbol, not just in this section. */
10367 if (action_discarded & PRETEND)
10368 {
10369 asection *kept;
10370
10371 kept = _bfd_elf_check_kept_section (sec,
10372 flinfo->info);
10373 if (kept != NULL)
10374 {
10375 *ps = kept;
10376 continue;
10377 }
10378 }
10379 }
10380 }
10381 }
10382
10383 /* Relocate the section by invoking a back end routine.
10384
10385 The back end routine is responsible for adjusting the
10386 section contents as necessary, and (if using Rela relocs
10387 and generating a relocatable output file) adjusting the
10388 reloc addend as necessary.
10389
10390 The back end routine does not have to worry about setting
10391 the reloc address or the reloc symbol index.
10392
10393 The back end routine is given a pointer to the swapped in
10394 internal symbols, and can access the hash table entries
10395 for the external symbols via elf_sym_hashes (input_bfd).
10396
10397 When generating relocatable output, the back end routine
10398 must handle STB_LOCAL/STT_SECTION symbols specially. The
10399 output symbol is going to be a section symbol
10400 corresponding to the output section, which will require
10401 the addend to be adjusted. */
10402
10403 ret = (*relocate_section) (output_bfd, flinfo->info,
10404 input_bfd, o, contents,
10405 internal_relocs,
10406 isymbuf,
10407 flinfo->sections);
10408 if (!ret)
10409 return FALSE;
10410
10411 if (ret == 2
10412 || bfd_link_relocatable (flinfo->info)
10413 || flinfo->info->emitrelocations)
10414 {
10415 Elf_Internal_Rela *irela;
10416 Elf_Internal_Rela *irelaend, *irelamid;
10417 bfd_vma last_offset;
10418 struct elf_link_hash_entry **rel_hash;
10419 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10420 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10421 unsigned int next_erel;
10422 bfd_boolean rela_normal;
10423 struct bfd_elf_section_data *esdi, *esdo;
10424
10425 esdi = elf_section_data (o);
10426 esdo = elf_section_data (o->output_section);
10427 rela_normal = FALSE;
10428
10429 /* Adjust the reloc addresses and symbol indices. */
10430
10431 irela = internal_relocs;
10432 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
10433 rel_hash = esdo->rel.hashes + esdo->rel.count;
10434 /* We start processing the REL relocs, if any. When we reach
10435 IRELAMID in the loop, we switch to the RELA relocs. */
10436 irelamid = irela;
10437 if (esdi->rel.hdr != NULL)
10438 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10439 * bed->s->int_rels_per_ext_rel);
10440 rel_hash_list = rel_hash;
10441 rela_hash_list = NULL;
10442 last_offset = o->output_offset;
10443 if (!bfd_link_relocatable (flinfo->info))
10444 last_offset += o->output_section->vma;
10445 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10446 {
10447 unsigned long r_symndx;
10448 asection *sec;
10449 Elf_Internal_Sym sym;
10450
10451 if (next_erel == bed->s->int_rels_per_ext_rel)
10452 {
10453 rel_hash++;
10454 next_erel = 0;
10455 }
10456
10457 if (irela == irelamid)
10458 {
10459 rel_hash = esdo->rela.hashes + esdo->rela.count;
10460 rela_hash_list = rel_hash;
10461 rela_normal = bed->rela_normal;
10462 }
10463
10464 irela->r_offset = _bfd_elf_section_offset (output_bfd,
10465 flinfo->info, o,
10466 irela->r_offset);
10467 if (irela->r_offset >= (bfd_vma) -2)
10468 {
10469 /* This is a reloc for a deleted entry or somesuch.
10470 Turn it into an R_*_NONE reloc, at the same
10471 offset as the last reloc. elf_eh_frame.c and
10472 bfd_elf_discard_info rely on reloc offsets
10473 being ordered. */
10474 irela->r_offset = last_offset;
10475 irela->r_info = 0;
10476 irela->r_addend = 0;
10477 continue;
10478 }
10479
10480 irela->r_offset += o->output_offset;
10481
10482 /* Relocs in an executable have to be virtual addresses. */
10483 if (!bfd_link_relocatable (flinfo->info))
10484 irela->r_offset += o->output_section->vma;
10485
10486 last_offset = irela->r_offset;
10487
10488 r_symndx = irela->r_info >> r_sym_shift;
10489 if (r_symndx == STN_UNDEF)
10490 continue;
10491
10492 if (r_symndx >= locsymcount
10493 || (elf_bad_symtab (input_bfd)
10494 && flinfo->sections[r_symndx] == NULL))
10495 {
10496 struct elf_link_hash_entry *rh;
10497 unsigned long indx;
10498
10499 /* This is a reloc against a global symbol. We
10500 have not yet output all the local symbols, so
10501 we do not know the symbol index of any global
10502 symbol. We set the rel_hash entry for this
10503 reloc to point to the global hash table entry
10504 for this symbol. The symbol index is then
10505 set at the end of bfd_elf_final_link. */
10506 indx = r_symndx - extsymoff;
10507 rh = elf_sym_hashes (input_bfd)[indx];
10508 while (rh->root.type == bfd_link_hash_indirect
10509 || rh->root.type == bfd_link_hash_warning)
10510 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10511
10512 /* Setting the index to -2 tells
10513 elf_link_output_extsym that this symbol is
10514 used by a reloc. */
10515 BFD_ASSERT (rh->indx < 0);
10516 rh->indx = -2;
10517
10518 *rel_hash = rh;
10519
10520 continue;
10521 }
10522
10523 /* This is a reloc against a local symbol. */
10524
10525 *rel_hash = NULL;
10526 sym = isymbuf[r_symndx];
10527 sec = flinfo->sections[r_symndx];
10528 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10529 {
10530 /* I suppose the backend ought to fill in the
10531 section of any STT_SECTION symbol against a
10532 processor specific section. */
10533 r_symndx = STN_UNDEF;
10534 if (bfd_is_abs_section (sec))
10535 ;
10536 else if (sec == NULL || sec->owner == NULL)
10537 {
10538 bfd_set_error (bfd_error_bad_value);
10539 return FALSE;
10540 }
10541 else
10542 {
10543 asection *osec = sec->output_section;
10544
10545 /* If we have discarded a section, the output
10546 section will be the absolute section. In
10547 case of discarded SEC_MERGE sections, use
10548 the kept section. relocate_section should
10549 have already handled discarded linkonce
10550 sections. */
10551 if (bfd_is_abs_section (osec)
10552 && sec->kept_section != NULL
10553 && sec->kept_section->output_section != NULL)
10554 {
10555 osec = sec->kept_section->output_section;
10556 irela->r_addend -= osec->vma;
10557 }
10558
10559 if (!bfd_is_abs_section (osec))
10560 {
10561 r_symndx = osec->target_index;
10562 if (r_symndx == STN_UNDEF)
10563 {
10564 irela->r_addend += osec->vma;
10565 osec = _bfd_nearby_section (output_bfd, osec,
10566 osec->vma);
10567 irela->r_addend -= osec->vma;
10568 r_symndx = osec->target_index;
10569 }
10570 }
10571 }
10572
10573 /* Adjust the addend according to where the
10574 section winds up in the output section. */
10575 if (rela_normal)
10576 irela->r_addend += sec->output_offset;
10577 }
10578 else
10579 {
10580 if (flinfo->indices[r_symndx] == -1)
10581 {
10582 unsigned long shlink;
10583 const char *name;
10584 asection *osec;
10585 long indx;
10586
10587 if (flinfo->info->strip == strip_all)
10588 {
10589 /* You can't do ld -r -s. */
10590 bfd_set_error (bfd_error_invalid_operation);
10591 return FALSE;
10592 }
10593
10594 /* This symbol was skipped earlier, but
10595 since it is needed by a reloc, we
10596 must output it now. */
10597 shlink = symtab_hdr->sh_link;
10598 name = (bfd_elf_string_from_elf_section
10599 (input_bfd, shlink, sym.st_name));
10600 if (name == NULL)
10601 return FALSE;
10602
10603 osec = sec->output_section;
10604 sym.st_shndx =
10605 _bfd_elf_section_from_bfd_section (output_bfd,
10606 osec);
10607 if (sym.st_shndx == SHN_BAD)
10608 return FALSE;
10609
10610 sym.st_value += sec->output_offset;
10611 if (!bfd_link_relocatable (flinfo->info))
10612 {
10613 sym.st_value += osec->vma;
10614 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10615 {
10616 /* STT_TLS symbols are relative to PT_TLS
10617 segment base. */
10618 BFD_ASSERT (elf_hash_table (flinfo->info)
10619 ->tls_sec != NULL);
10620 sym.st_value -= (elf_hash_table (flinfo->info)
10621 ->tls_sec->vma);
10622 }
10623 }
10624
10625 indx = bfd_get_symcount (output_bfd);
10626 ret = elf_link_output_symstrtab (flinfo, name,
10627 &sym, sec,
10628 NULL);
10629 if (ret == 0)
10630 return FALSE;
10631 else if (ret == 1)
10632 flinfo->indices[r_symndx] = indx;
10633 else
10634 abort ();
10635 }
10636
10637 r_symndx = flinfo->indices[r_symndx];
10638 }
10639
10640 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10641 | (irela->r_info & r_type_mask));
10642 }
10643
10644 /* Swap out the relocs. */
10645 input_rel_hdr = esdi->rel.hdr;
10646 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
10647 {
10648 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10649 input_rel_hdr,
10650 internal_relocs,
10651 rel_hash_list))
10652 return FALSE;
10653 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10654 * bed->s->int_rels_per_ext_rel);
10655 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
10656 }
10657
10658 input_rela_hdr = esdi->rela.hdr;
10659 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10660 {
10661 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10662 input_rela_hdr,
10663 internal_relocs,
10664 rela_hash_list))
10665 return FALSE;
10666 }
10667 }
10668 }
10669
10670 /* Write out the modified section contents. */
10671 if (bed->elf_backend_write_section
10672 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
10673 contents))
10674 {
10675 /* Section written out. */
10676 }
10677 else switch (o->sec_info_type)
10678 {
10679 case SEC_INFO_TYPE_STABS:
10680 if (! (_bfd_write_section_stabs
10681 (output_bfd,
10682 &elf_hash_table (flinfo->info)->stab_info,
10683 o, &elf_section_data (o)->sec_info, contents)))
10684 return FALSE;
10685 break;
10686 case SEC_INFO_TYPE_MERGE:
10687 if (! _bfd_write_merged_section (output_bfd, o,
10688 elf_section_data (o)->sec_info))
10689 return FALSE;
10690 break;
10691 case SEC_INFO_TYPE_EH_FRAME:
10692 {
10693 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
10694 o, contents))
10695 return FALSE;
10696 }
10697 break;
10698 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10699 {
10700 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10701 flinfo->info,
10702 o, contents))
10703 return FALSE;
10704 }
10705 break;
10706 default:
10707 {
10708 if (! (o->flags & SEC_EXCLUDE))
10709 {
10710 file_ptr offset = (file_ptr) o->output_offset;
10711 bfd_size_type todo = o->size;
10712
10713 offset *= bfd_octets_per_byte (output_bfd);
10714
10715 if ((o->flags & SEC_ELF_REVERSE_COPY))
10716 {
10717 /* Reverse-copy input section to output. */
10718 do
10719 {
10720 todo -= address_size;
10721 if (! bfd_set_section_contents (output_bfd,
10722 o->output_section,
10723 contents + todo,
10724 offset,
10725 address_size))
10726 return FALSE;
10727 if (todo == 0)
10728 break;
10729 offset += address_size;
10730 }
10731 while (1);
10732 }
10733 else if (! bfd_set_section_contents (output_bfd,
10734 o->output_section,
10735 contents,
10736 offset, todo))
10737 return FALSE;
10738 }
10739 }
10740 break;
10741 }
10742 }
10743
10744 return TRUE;
10745 }
10746
10747 /* Generate a reloc when linking an ELF file. This is a reloc
10748 requested by the linker, and does not come from any input file. This
10749 is used to build constructor and destructor tables when linking
10750 with -Ur. */
10751
10752 static bfd_boolean
10753 elf_reloc_link_order (bfd *output_bfd,
10754 struct bfd_link_info *info,
10755 asection *output_section,
10756 struct bfd_link_order *link_order)
10757 {
10758 reloc_howto_type *howto;
10759 long indx;
10760 bfd_vma offset;
10761 bfd_vma addend;
10762 struct bfd_elf_section_reloc_data *reldata;
10763 struct elf_link_hash_entry **rel_hash_ptr;
10764 Elf_Internal_Shdr *rel_hdr;
10765 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10766 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10767 bfd_byte *erel;
10768 unsigned int i;
10769 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
10770
10771 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10772 if (howto == NULL)
10773 {
10774 bfd_set_error (bfd_error_bad_value);
10775 return FALSE;
10776 }
10777
10778 addend = link_order->u.reloc.p->addend;
10779
10780 if (esdo->rel.hdr)
10781 reldata = &esdo->rel;
10782 else if (esdo->rela.hdr)
10783 reldata = &esdo->rela;
10784 else
10785 {
10786 reldata = NULL;
10787 BFD_ASSERT (0);
10788 }
10789
10790 /* Figure out the symbol index. */
10791 rel_hash_ptr = reldata->hashes + reldata->count;
10792 if (link_order->type == bfd_section_reloc_link_order)
10793 {
10794 indx = link_order->u.reloc.p->u.section->target_index;
10795 BFD_ASSERT (indx != 0);
10796 *rel_hash_ptr = NULL;
10797 }
10798 else
10799 {
10800 struct elf_link_hash_entry *h;
10801
10802 /* Treat a reloc against a defined symbol as though it were
10803 actually against the section. */
10804 h = ((struct elf_link_hash_entry *)
10805 bfd_wrapped_link_hash_lookup (output_bfd, info,
10806 link_order->u.reloc.p->u.name,
10807 FALSE, FALSE, TRUE));
10808 if (h != NULL
10809 && (h->root.type == bfd_link_hash_defined
10810 || h->root.type == bfd_link_hash_defweak))
10811 {
10812 asection *section;
10813
10814 section = h->root.u.def.section;
10815 indx = section->output_section->target_index;
10816 *rel_hash_ptr = NULL;
10817 /* It seems that we ought to add the symbol value to the
10818 addend here, but in practice it has already been added
10819 because it was passed to constructor_callback. */
10820 addend += section->output_section->vma + section->output_offset;
10821 }
10822 else if (h != NULL)
10823 {
10824 /* Setting the index to -2 tells elf_link_output_extsym that
10825 this symbol is used by a reloc. */
10826 h->indx = -2;
10827 *rel_hash_ptr = h;
10828 indx = 0;
10829 }
10830 else
10831 {
10832 (*info->callbacks->unattached_reloc)
10833 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
10834 indx = 0;
10835 }
10836 }
10837
10838 /* If this is an inplace reloc, we must write the addend into the
10839 object file. */
10840 if (howto->partial_inplace && addend != 0)
10841 {
10842 bfd_size_type size;
10843 bfd_reloc_status_type rstat;
10844 bfd_byte *buf;
10845 bfd_boolean ok;
10846 const char *sym_name;
10847
10848 size = (bfd_size_type) bfd_get_reloc_size (howto);
10849 buf = (bfd_byte *) bfd_zmalloc (size);
10850 if (buf == NULL && size != 0)
10851 return FALSE;
10852 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10853 switch (rstat)
10854 {
10855 case bfd_reloc_ok:
10856 break;
10857
10858 default:
10859 case bfd_reloc_outofrange:
10860 abort ();
10861
10862 case bfd_reloc_overflow:
10863 if (link_order->type == bfd_section_reloc_link_order)
10864 sym_name = bfd_section_name (output_bfd,
10865 link_order->u.reloc.p->u.section);
10866 else
10867 sym_name = link_order->u.reloc.p->u.name;
10868 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
10869 howto->name, addend, NULL, NULL,
10870 (bfd_vma) 0);
10871 break;
10872 }
10873
10874 ok = bfd_set_section_contents (output_bfd, output_section, buf,
10875 link_order->offset
10876 * bfd_octets_per_byte (output_bfd),
10877 size);
10878 free (buf);
10879 if (! ok)
10880 return FALSE;
10881 }
10882
10883 /* The address of a reloc is relative to the section in a
10884 relocatable file, and is a virtual address in an executable
10885 file. */
10886 offset = link_order->offset;
10887 if (! bfd_link_relocatable (info))
10888 offset += output_section->vma;
10889
10890 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10891 {
10892 irel[i].r_offset = offset;
10893 irel[i].r_info = 0;
10894 irel[i].r_addend = 0;
10895 }
10896 if (bed->s->arch_size == 32)
10897 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10898 else
10899 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10900
10901 rel_hdr = reldata->hdr;
10902 erel = rel_hdr->contents;
10903 if (rel_hdr->sh_type == SHT_REL)
10904 {
10905 erel += reldata->count * bed->s->sizeof_rel;
10906 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10907 }
10908 else
10909 {
10910 irel[0].r_addend = addend;
10911 erel += reldata->count * bed->s->sizeof_rela;
10912 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10913 }
10914
10915 ++reldata->count;
10916
10917 return TRUE;
10918 }
10919
10920
10921 /* Get the output vma of the section pointed to by the sh_link field. */
10922
10923 static bfd_vma
10924 elf_get_linked_section_vma (struct bfd_link_order *p)
10925 {
10926 Elf_Internal_Shdr **elf_shdrp;
10927 asection *s;
10928 int elfsec;
10929
10930 s = p->u.indirect.section;
10931 elf_shdrp = elf_elfsections (s->owner);
10932 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10933 elfsec = elf_shdrp[elfsec]->sh_link;
10934 /* PR 290:
10935 The Intel C compiler generates SHT_IA_64_UNWIND with
10936 SHF_LINK_ORDER. But it doesn't set the sh_link or
10937 sh_info fields. Hence we could get the situation
10938 where elfsec is 0. */
10939 if (elfsec == 0)
10940 {
10941 const struct elf_backend_data *bed
10942 = get_elf_backend_data (s->owner);
10943 if (bed->link_order_error_handler)
10944 bed->link_order_error_handler
10945 /* xgettext:c-format */
10946 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
10947 return 0;
10948 }
10949 else
10950 {
10951 s = elf_shdrp[elfsec]->bfd_section;
10952 return s->output_section->vma + s->output_offset;
10953 }
10954 }
10955
10956
10957 /* Compare two sections based on the locations of the sections they are
10958 linked to. Used by elf_fixup_link_order. */
10959
10960 static int
10961 compare_link_order (const void * a, const void * b)
10962 {
10963 bfd_vma apos;
10964 bfd_vma bpos;
10965
10966 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10967 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10968 if (apos < bpos)
10969 return -1;
10970 return apos > bpos;
10971 }
10972
10973
10974 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10975 order as their linked sections. Returns false if this could not be done
10976 because an output section includes both ordered and unordered
10977 sections. Ideally we'd do this in the linker proper. */
10978
10979 static bfd_boolean
10980 elf_fixup_link_order (bfd *abfd, asection *o)
10981 {
10982 int seen_linkorder;
10983 int seen_other;
10984 int n;
10985 struct bfd_link_order *p;
10986 bfd *sub;
10987 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10988 unsigned elfsec;
10989 struct bfd_link_order **sections;
10990 asection *s, *other_sec, *linkorder_sec;
10991 bfd_vma offset;
10992
10993 other_sec = NULL;
10994 linkorder_sec = NULL;
10995 seen_other = 0;
10996 seen_linkorder = 0;
10997 for (p = o->map_head.link_order; p != NULL; p = p->next)
10998 {
10999 if (p->type == bfd_indirect_link_order)
11000 {
11001 s = p->u.indirect.section;
11002 sub = s->owner;
11003 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11004 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
11005 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11006 && elfsec < elf_numsections (sub)
11007 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11008 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
11009 {
11010 seen_linkorder++;
11011 linkorder_sec = s;
11012 }
11013 else
11014 {
11015 seen_other++;
11016 other_sec = s;
11017 }
11018 }
11019 else
11020 seen_other++;
11021
11022 if (seen_other && seen_linkorder)
11023 {
11024 if (other_sec && linkorder_sec)
11025 _bfd_error_handler
11026 /* xgettext:c-format */
11027 (_("%A has both ordered [`%A' in %B] "
11028 "and unordered [`%A' in %B] sections"),
11029 o, linkorder_sec,
11030 linkorder_sec->owner, other_sec,
11031 other_sec->owner);
11032 else
11033 _bfd_error_handler
11034 (_("%A has both ordered and unordered sections"), o);
11035 bfd_set_error (bfd_error_bad_value);
11036 return FALSE;
11037 }
11038 }
11039
11040 if (!seen_linkorder)
11041 return TRUE;
11042
11043 sections = (struct bfd_link_order **)
11044 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11045 if (sections == NULL)
11046 return FALSE;
11047 seen_linkorder = 0;
11048
11049 for (p = o->map_head.link_order; p != NULL; p = p->next)
11050 {
11051 sections[seen_linkorder++] = p;
11052 }
11053 /* Sort the input sections in the order of their linked section. */
11054 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11055 compare_link_order);
11056
11057 /* Change the offsets of the sections. */
11058 offset = 0;
11059 for (n = 0; n < seen_linkorder; n++)
11060 {
11061 s = sections[n]->u.indirect.section;
11062 offset &= ~(bfd_vma) 0 << s->alignment_power;
11063 s->output_offset = offset / bfd_octets_per_byte (abfd);
11064 sections[n]->offset = offset;
11065 offset += sections[n]->size;
11066 }
11067
11068 free (sections);
11069 return TRUE;
11070 }
11071
11072 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11073 Returns TRUE upon success, FALSE otherwise. */
11074
11075 static bfd_boolean
11076 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11077 {
11078 bfd_boolean ret = FALSE;
11079 bfd *implib_bfd;
11080 const struct elf_backend_data *bed;
11081 flagword flags;
11082 enum bfd_architecture arch;
11083 unsigned int mach;
11084 asymbol **sympp = NULL;
11085 long symsize;
11086 long symcount;
11087 long src_count;
11088 elf_symbol_type *osymbuf;
11089
11090 implib_bfd = info->out_implib_bfd;
11091 bed = get_elf_backend_data (abfd);
11092
11093 if (!bfd_set_format (implib_bfd, bfd_object))
11094 return FALSE;
11095
11096 flags = bfd_get_file_flags (abfd);
11097 flags &= ~HAS_RELOC;
11098 if (!bfd_set_start_address (implib_bfd, 0)
11099 || !bfd_set_file_flags (implib_bfd, flags))
11100 return FALSE;
11101
11102 /* Copy architecture of output file to import library file. */
11103 arch = bfd_get_arch (abfd);
11104 mach = bfd_get_mach (abfd);
11105 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11106 && (abfd->target_defaulted
11107 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11108 return FALSE;
11109
11110 /* Get symbol table size. */
11111 symsize = bfd_get_symtab_upper_bound (abfd);
11112 if (symsize < 0)
11113 return FALSE;
11114
11115 /* Read in the symbol table. */
11116 sympp = (asymbol **) xmalloc (symsize);
11117 symcount = bfd_canonicalize_symtab (abfd, sympp);
11118 if (symcount < 0)
11119 goto free_sym_buf;
11120
11121 /* Allow the BFD backend to copy any private header data it
11122 understands from the output BFD to the import library BFD. */
11123 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11124 goto free_sym_buf;
11125
11126 /* Filter symbols to appear in the import library. */
11127 if (bed->elf_backend_filter_implib_symbols)
11128 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11129 symcount);
11130 else
11131 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11132 if (symcount == 0)
11133 {
11134 bfd_set_error (bfd_error_no_symbols);
11135 _bfd_error_handler (_("%B: no symbol found for import library"),
11136 implib_bfd);
11137 goto free_sym_buf;
11138 }
11139
11140
11141 /* Make symbols absolute. */
11142 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11143 sizeof (*osymbuf));
11144 for (src_count = 0; src_count < symcount; src_count++)
11145 {
11146 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11147 sizeof (*osymbuf));
11148 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11149 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11150 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11151 osymbuf[src_count].internal_elf_sym.st_value =
11152 osymbuf[src_count].symbol.value;
11153 sympp[src_count] = &osymbuf[src_count].symbol;
11154 }
11155
11156 bfd_set_symtab (implib_bfd, sympp, symcount);
11157
11158 /* Allow the BFD backend to copy any private data it understands
11159 from the output BFD to the import library BFD. This is done last
11160 to permit the routine to look at the filtered symbol table. */
11161 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11162 goto free_sym_buf;
11163
11164 if (!bfd_close (implib_bfd))
11165 goto free_sym_buf;
11166
11167 ret = TRUE;
11168
11169 free_sym_buf:
11170 free (sympp);
11171 return ret;
11172 }
11173
11174 static void
11175 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11176 {
11177 asection *o;
11178
11179 if (flinfo->symstrtab != NULL)
11180 _bfd_elf_strtab_free (flinfo->symstrtab);
11181 if (flinfo->contents != NULL)
11182 free (flinfo->contents);
11183 if (flinfo->external_relocs != NULL)
11184 free (flinfo->external_relocs);
11185 if (flinfo->internal_relocs != NULL)
11186 free (flinfo->internal_relocs);
11187 if (flinfo->external_syms != NULL)
11188 free (flinfo->external_syms);
11189 if (flinfo->locsym_shndx != NULL)
11190 free (flinfo->locsym_shndx);
11191 if (flinfo->internal_syms != NULL)
11192 free (flinfo->internal_syms);
11193 if (flinfo->indices != NULL)
11194 free (flinfo->indices);
11195 if (flinfo->sections != NULL)
11196 free (flinfo->sections);
11197 if (flinfo->symshndxbuf != NULL)
11198 free (flinfo->symshndxbuf);
11199 for (o = obfd->sections; o != NULL; o = o->next)
11200 {
11201 struct bfd_elf_section_data *esdo = elf_section_data (o);
11202 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11203 free (esdo->rel.hashes);
11204 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11205 free (esdo->rela.hashes);
11206 }
11207 }
11208
11209 /* Do the final step of an ELF link. */
11210
11211 bfd_boolean
11212 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11213 {
11214 bfd_boolean dynamic;
11215 bfd_boolean emit_relocs;
11216 bfd *dynobj;
11217 struct elf_final_link_info flinfo;
11218 asection *o;
11219 struct bfd_link_order *p;
11220 bfd *sub;
11221 bfd_size_type max_contents_size;
11222 bfd_size_type max_external_reloc_size;
11223 bfd_size_type max_internal_reloc_count;
11224 bfd_size_type max_sym_count;
11225 bfd_size_type max_sym_shndx_count;
11226 Elf_Internal_Sym elfsym;
11227 unsigned int i;
11228 Elf_Internal_Shdr *symtab_hdr;
11229 Elf_Internal_Shdr *symtab_shndx_hdr;
11230 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11231 struct elf_outext_info eoinfo;
11232 bfd_boolean merged;
11233 size_t relativecount = 0;
11234 asection *reldyn = 0;
11235 bfd_size_type amt;
11236 asection *attr_section = NULL;
11237 bfd_vma attr_size = 0;
11238 const char *std_attrs_section;
11239 struct elf_link_hash_table *htab = elf_hash_table (info);
11240
11241 if (!is_elf_hash_table (htab))
11242 return FALSE;
11243
11244 if (bfd_link_pic (info))
11245 abfd->flags |= DYNAMIC;
11246
11247 dynamic = htab->dynamic_sections_created;
11248 dynobj = htab->dynobj;
11249
11250 emit_relocs = (bfd_link_relocatable (info)
11251 || info->emitrelocations);
11252
11253 flinfo.info = info;
11254 flinfo.output_bfd = abfd;
11255 flinfo.symstrtab = _bfd_elf_strtab_init ();
11256 if (flinfo.symstrtab == NULL)
11257 return FALSE;
11258
11259 if (! dynamic)
11260 {
11261 flinfo.hash_sec = NULL;
11262 flinfo.symver_sec = NULL;
11263 }
11264 else
11265 {
11266 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11267 /* Note that dynsym_sec can be NULL (on VMS). */
11268 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11269 /* Note that it is OK if symver_sec is NULL. */
11270 }
11271
11272 flinfo.contents = NULL;
11273 flinfo.external_relocs = NULL;
11274 flinfo.internal_relocs = NULL;
11275 flinfo.external_syms = NULL;
11276 flinfo.locsym_shndx = NULL;
11277 flinfo.internal_syms = NULL;
11278 flinfo.indices = NULL;
11279 flinfo.sections = NULL;
11280 flinfo.symshndxbuf = NULL;
11281 flinfo.filesym_count = 0;
11282
11283 /* The object attributes have been merged. Remove the input
11284 sections from the link, and set the contents of the output
11285 secton. */
11286 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11287 for (o = abfd->sections; o != NULL; o = o->next)
11288 {
11289 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11290 || strcmp (o->name, ".gnu.attributes") == 0)
11291 {
11292 for (p = o->map_head.link_order; p != NULL; p = p->next)
11293 {
11294 asection *input_section;
11295
11296 if (p->type != bfd_indirect_link_order)
11297 continue;
11298 input_section = p->u.indirect.section;
11299 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11300 elf_link_input_bfd ignores this section. */
11301 input_section->flags &= ~SEC_HAS_CONTENTS;
11302 }
11303
11304 attr_size = bfd_elf_obj_attr_size (abfd);
11305 if (attr_size)
11306 {
11307 bfd_set_section_size (abfd, o, attr_size);
11308 attr_section = o;
11309 /* Skip this section later on. */
11310 o->map_head.link_order = NULL;
11311 }
11312 else
11313 o->flags |= SEC_EXCLUDE;
11314 }
11315 }
11316
11317 /* Count up the number of relocations we will output for each output
11318 section, so that we know the sizes of the reloc sections. We
11319 also figure out some maximum sizes. */
11320 max_contents_size = 0;
11321 max_external_reloc_size = 0;
11322 max_internal_reloc_count = 0;
11323 max_sym_count = 0;
11324 max_sym_shndx_count = 0;
11325 merged = FALSE;
11326 for (o = abfd->sections; o != NULL; o = o->next)
11327 {
11328 struct bfd_elf_section_data *esdo = elf_section_data (o);
11329 o->reloc_count = 0;
11330
11331 for (p = o->map_head.link_order; p != NULL; p = p->next)
11332 {
11333 unsigned int reloc_count = 0;
11334 unsigned int additional_reloc_count = 0;
11335 struct bfd_elf_section_data *esdi = NULL;
11336
11337 if (p->type == bfd_section_reloc_link_order
11338 || p->type == bfd_symbol_reloc_link_order)
11339 reloc_count = 1;
11340 else if (p->type == bfd_indirect_link_order)
11341 {
11342 asection *sec;
11343
11344 sec = p->u.indirect.section;
11345 /* See PR 20908 for a reproducer. */
11346 if (bfd_get_flavour (sec->owner) != bfd_target_elf_flavour)
11347 {
11348 _bfd_error_handler (_("%B: not in ELF format"), sec->owner);
11349 goto error_return;
11350 }
11351 esdi = elf_section_data (sec);
11352
11353 /* Mark all sections which are to be included in the
11354 link. This will normally be every section. We need
11355 to do this so that we can identify any sections which
11356 the linker has decided to not include. */
11357 sec->linker_mark = TRUE;
11358
11359 if (sec->flags & SEC_MERGE)
11360 merged = TRUE;
11361
11362 if (esdo->this_hdr.sh_type == SHT_REL
11363 || esdo->this_hdr.sh_type == SHT_RELA)
11364 /* Some backends use reloc_count in relocation sections
11365 to count particular types of relocs. Of course,
11366 reloc sections themselves can't have relocations. */
11367 reloc_count = 0;
11368 else if (emit_relocs)
11369 {
11370 reloc_count = sec->reloc_count;
11371 if (bed->elf_backend_count_additional_relocs)
11372 {
11373 int c;
11374 c = (*bed->elf_backend_count_additional_relocs) (sec);
11375 additional_reloc_count += c;
11376 }
11377 }
11378 else if (bed->elf_backend_count_relocs)
11379 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11380
11381 if (sec->rawsize > max_contents_size)
11382 max_contents_size = sec->rawsize;
11383 if (sec->size > max_contents_size)
11384 max_contents_size = sec->size;
11385
11386 /* We are interested in just local symbols, not all
11387 symbols. */
11388 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11389 && (sec->owner->flags & DYNAMIC) == 0)
11390 {
11391 size_t sym_count;
11392
11393 if (elf_bad_symtab (sec->owner))
11394 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11395 / bed->s->sizeof_sym);
11396 else
11397 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11398
11399 if (sym_count > max_sym_count)
11400 max_sym_count = sym_count;
11401
11402 if (sym_count > max_sym_shndx_count
11403 && elf_symtab_shndx_list (sec->owner) != NULL)
11404 max_sym_shndx_count = sym_count;
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->export_dynamic
13057 || (h->dynamic
13058 && d != NULL
13059 && (*d->match) (&d->head, NULL, h->root.root.string)))
13060 && (h->versioned >= versioned
13061 || !bfd_hide_sym_by_version (info->version_info,
13062 h->root.root.string)))))
13063 h->root.u.def.section->flags |= SEC_KEEP;
13064
13065 return TRUE;
13066 }
13067
13068 /* Keep all sections containing symbols undefined on the command-line,
13069 and the section containing the entry symbol. */
13070
13071 void
13072 _bfd_elf_gc_keep (struct bfd_link_info *info)
13073 {
13074 struct bfd_sym_chain *sym;
13075
13076 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13077 {
13078 struct elf_link_hash_entry *h;
13079
13080 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13081 FALSE, FALSE, FALSE);
13082
13083 if (h != NULL
13084 && (h->root.type == bfd_link_hash_defined
13085 || h->root.type == bfd_link_hash_defweak)
13086 && !bfd_is_abs_section (h->root.u.def.section)
13087 && !bfd_is_und_section (h->root.u.def.section))
13088 h->root.u.def.section->flags |= SEC_KEEP;
13089 }
13090 }
13091
13092 bfd_boolean
13093 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13094 struct bfd_link_info *info)
13095 {
13096 bfd *ibfd = info->input_bfds;
13097
13098 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13099 {
13100 asection *sec;
13101 struct elf_reloc_cookie cookie;
13102
13103 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13104 continue;
13105
13106 if (!init_reloc_cookie (&cookie, info, ibfd))
13107 return FALSE;
13108
13109 for (sec = ibfd->sections; sec; sec = sec->next)
13110 {
13111 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13112 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13113 {
13114 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13115 fini_reloc_cookie_rels (&cookie, sec);
13116 }
13117 }
13118 }
13119 return TRUE;
13120 }
13121
13122 /* Do mark and sweep of unused sections. */
13123
13124 bfd_boolean
13125 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13126 {
13127 bfd_boolean ok = TRUE;
13128 bfd *sub;
13129 elf_gc_mark_hook_fn gc_mark_hook;
13130 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13131 struct elf_link_hash_table *htab;
13132
13133 if (!bed->can_gc_sections
13134 || !is_elf_hash_table (info->hash))
13135 {
13136 _bfd_error_handler(_("Warning: gc-sections option ignored"));
13137 return TRUE;
13138 }
13139
13140 bed->gc_keep (info);
13141 htab = elf_hash_table (info);
13142
13143 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13144 at the .eh_frame section if we can mark the FDEs individually. */
13145 for (sub = info->input_bfds;
13146 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13147 sub = sub->link.next)
13148 {
13149 asection *sec;
13150 struct elf_reloc_cookie cookie;
13151
13152 sec = bfd_get_section_by_name (sub, ".eh_frame");
13153 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
13154 {
13155 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
13156 if (elf_section_data (sec)->sec_info
13157 && (sec->flags & SEC_LINKER_CREATED) == 0)
13158 elf_eh_frame_section (sub) = sec;
13159 fini_reloc_cookie_for_section (&cookie, sec);
13160 sec = bfd_get_next_section_by_name (NULL, sec);
13161 }
13162 }
13163
13164 /* Apply transitive closure to the vtable entry usage info. */
13165 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
13166 if (!ok)
13167 return FALSE;
13168
13169 /* Kill the vtable relocations that were not used. */
13170 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
13171 if (!ok)
13172 return FALSE;
13173
13174 /* Mark dynamically referenced symbols. */
13175 if (htab->dynamic_sections_created)
13176 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
13177
13178 /* Grovel through relocs to find out who stays ... */
13179 gc_mark_hook = bed->gc_mark_hook;
13180 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13181 {
13182 asection *o;
13183
13184 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13185 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13186 continue;
13187
13188 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13189 Also treat note sections as a root, if the section is not part
13190 of a group. */
13191 for (o = sub->sections; o != NULL; o = o->next)
13192 if (!o->gc_mark
13193 && (o->flags & SEC_EXCLUDE) == 0
13194 && ((o->flags & SEC_KEEP) != 0
13195 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13196 && elf_next_in_group (o) == NULL )))
13197 {
13198 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13199 return FALSE;
13200 }
13201 }
13202
13203 /* Allow the backend to mark additional target specific sections. */
13204 bed->gc_mark_extra_sections (info, gc_mark_hook);
13205
13206 /* ... and mark SEC_EXCLUDE for those that go. */
13207 return elf_gc_sweep (abfd, info);
13208 }
13209 \f
13210 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13211
13212 bfd_boolean
13213 bfd_elf_gc_record_vtinherit (bfd *abfd,
13214 asection *sec,
13215 struct elf_link_hash_entry *h,
13216 bfd_vma offset)
13217 {
13218 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13219 struct elf_link_hash_entry **search, *child;
13220 size_t extsymcount;
13221 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13222
13223 /* The sh_info field of the symtab header tells us where the
13224 external symbols start. We don't care about the local symbols at
13225 this point. */
13226 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13227 if (!elf_bad_symtab (abfd))
13228 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13229
13230 sym_hashes = elf_sym_hashes (abfd);
13231 sym_hashes_end = sym_hashes + extsymcount;
13232
13233 /* Hunt down the child symbol, which is in this section at the same
13234 offset as the relocation. */
13235 for (search = sym_hashes; search != sym_hashes_end; ++search)
13236 {
13237 if ((child = *search) != NULL
13238 && (child->root.type == bfd_link_hash_defined
13239 || child->root.type == bfd_link_hash_defweak)
13240 && child->root.u.def.section == sec
13241 && child->root.u.def.value == offset)
13242 goto win;
13243 }
13244
13245 /* xgettext:c-format */
13246 _bfd_error_handler (_("%B: %A+%lu: No symbol found for INHERIT"),
13247 abfd, sec, (unsigned long) offset);
13248 bfd_set_error (bfd_error_invalid_operation);
13249 return FALSE;
13250
13251 win:
13252 if (!child->vtable)
13253 {
13254 child->vtable = ((struct elf_link_virtual_table_entry *)
13255 bfd_zalloc (abfd, sizeof (*child->vtable)));
13256 if (!child->vtable)
13257 return FALSE;
13258 }
13259 if (!h)
13260 {
13261 /* This *should* only be the absolute section. It could potentially
13262 be that someone has defined a non-global vtable though, which
13263 would be bad. It isn't worth paging in the local symbols to be
13264 sure though; that case should simply be handled by the assembler. */
13265
13266 child->vtable->parent = (struct elf_link_hash_entry *) -1;
13267 }
13268 else
13269 child->vtable->parent = h;
13270
13271 return TRUE;
13272 }
13273
13274 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13275
13276 bfd_boolean
13277 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13278 asection *sec ATTRIBUTE_UNUSED,
13279 struct elf_link_hash_entry *h,
13280 bfd_vma addend)
13281 {
13282 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13283 unsigned int log_file_align = bed->s->log_file_align;
13284
13285 if (!h->vtable)
13286 {
13287 h->vtable = ((struct elf_link_virtual_table_entry *)
13288 bfd_zalloc (abfd, sizeof (*h->vtable)));
13289 if (!h->vtable)
13290 return FALSE;
13291 }
13292
13293 if (addend >= h->vtable->size)
13294 {
13295 size_t size, bytes, file_align;
13296 bfd_boolean *ptr = h->vtable->used;
13297
13298 /* While the symbol is undefined, we have to be prepared to handle
13299 a zero size. */
13300 file_align = 1 << log_file_align;
13301 if (h->root.type == bfd_link_hash_undefined)
13302 size = addend + file_align;
13303 else
13304 {
13305 size = h->size;
13306 if (addend >= size)
13307 {
13308 /* Oops! We've got a reference past the defined end of
13309 the table. This is probably a bug -- shall we warn? */
13310 size = addend + file_align;
13311 }
13312 }
13313 size = (size + file_align - 1) & -file_align;
13314
13315 /* Allocate one extra entry for use as a "done" flag for the
13316 consolidation pass. */
13317 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13318
13319 if (ptr)
13320 {
13321 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
13322
13323 if (ptr != NULL)
13324 {
13325 size_t oldbytes;
13326
13327 oldbytes = (((h->vtable->size >> log_file_align) + 1)
13328 * sizeof (bfd_boolean));
13329 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13330 }
13331 }
13332 else
13333 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
13334
13335 if (ptr == NULL)
13336 return FALSE;
13337
13338 /* And arrange for that done flag to be at index -1. */
13339 h->vtable->used = ptr + 1;
13340 h->vtable->size = size;
13341 }
13342
13343 h->vtable->used[addend >> log_file_align] = TRUE;
13344
13345 return TRUE;
13346 }
13347
13348 /* Map an ELF section header flag to its corresponding string. */
13349 typedef struct
13350 {
13351 char *flag_name;
13352 flagword flag_value;
13353 } elf_flags_to_name_table;
13354
13355 static elf_flags_to_name_table elf_flags_to_names [] =
13356 {
13357 { "SHF_WRITE", SHF_WRITE },
13358 { "SHF_ALLOC", SHF_ALLOC },
13359 { "SHF_EXECINSTR", SHF_EXECINSTR },
13360 { "SHF_MERGE", SHF_MERGE },
13361 { "SHF_STRINGS", SHF_STRINGS },
13362 { "SHF_INFO_LINK", SHF_INFO_LINK},
13363 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13364 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13365 { "SHF_GROUP", SHF_GROUP },
13366 { "SHF_TLS", SHF_TLS },
13367 { "SHF_MASKOS", SHF_MASKOS },
13368 { "SHF_EXCLUDE", SHF_EXCLUDE },
13369 };
13370
13371 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13372 bfd_boolean
13373 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
13374 struct flag_info *flaginfo,
13375 asection *section)
13376 {
13377 const bfd_vma sh_flags = elf_section_flags (section);
13378
13379 if (!flaginfo->flags_initialized)
13380 {
13381 bfd *obfd = info->output_bfd;
13382 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13383 struct flag_info_list *tf = flaginfo->flag_list;
13384 int with_hex = 0;
13385 int without_hex = 0;
13386
13387 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13388 {
13389 unsigned i;
13390 flagword (*lookup) (char *);
13391
13392 lookup = bed->elf_backend_lookup_section_flags_hook;
13393 if (lookup != NULL)
13394 {
13395 flagword hexval = (*lookup) ((char *) tf->name);
13396
13397 if (hexval != 0)
13398 {
13399 if (tf->with == with_flags)
13400 with_hex |= hexval;
13401 else if (tf->with == without_flags)
13402 without_hex |= hexval;
13403 tf->valid = TRUE;
13404 continue;
13405 }
13406 }
13407 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13408 {
13409 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
13410 {
13411 if (tf->with == with_flags)
13412 with_hex |= elf_flags_to_names[i].flag_value;
13413 else if (tf->with == without_flags)
13414 without_hex |= elf_flags_to_names[i].flag_value;
13415 tf->valid = TRUE;
13416 break;
13417 }
13418 }
13419 if (!tf->valid)
13420 {
13421 info->callbacks->einfo
13422 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
13423 return FALSE;
13424 }
13425 }
13426 flaginfo->flags_initialized = TRUE;
13427 flaginfo->only_with_flags |= with_hex;
13428 flaginfo->not_with_flags |= without_hex;
13429 }
13430
13431 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13432 return FALSE;
13433
13434 if ((flaginfo->not_with_flags & sh_flags) != 0)
13435 return FALSE;
13436
13437 return TRUE;
13438 }
13439
13440 struct alloc_got_off_arg {
13441 bfd_vma gotoff;
13442 struct bfd_link_info *info;
13443 };
13444
13445 /* We need a special top-level link routine to convert got reference counts
13446 to real got offsets. */
13447
13448 static bfd_boolean
13449 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13450 {
13451 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13452 bfd *obfd = gofarg->info->output_bfd;
13453 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13454
13455 if (h->got.refcount > 0)
13456 {
13457 h->got.offset = gofarg->gotoff;
13458 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13459 }
13460 else
13461 h->got.offset = (bfd_vma) -1;
13462
13463 return TRUE;
13464 }
13465
13466 /* And an accompanying bit to work out final got entry offsets once
13467 we're done. Should be called from final_link. */
13468
13469 bfd_boolean
13470 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13471 struct bfd_link_info *info)
13472 {
13473 bfd *i;
13474 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13475 bfd_vma gotoff;
13476 struct alloc_got_off_arg gofarg;
13477
13478 BFD_ASSERT (abfd == info->output_bfd);
13479
13480 if (! is_elf_hash_table (info->hash))
13481 return FALSE;
13482
13483 /* The GOT offset is relative to the .got section, but the GOT header is
13484 put into the .got.plt section, if the backend uses it. */
13485 if (bed->want_got_plt)
13486 gotoff = 0;
13487 else
13488 gotoff = bed->got_header_size;
13489
13490 /* Do the local .got entries first. */
13491 for (i = info->input_bfds; i; i = i->link.next)
13492 {
13493 bfd_signed_vma *local_got;
13494 size_t j, locsymcount;
13495 Elf_Internal_Shdr *symtab_hdr;
13496
13497 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13498 continue;
13499
13500 local_got = elf_local_got_refcounts (i);
13501 if (!local_got)
13502 continue;
13503
13504 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13505 if (elf_bad_symtab (i))
13506 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13507 else
13508 locsymcount = symtab_hdr->sh_info;
13509
13510 for (j = 0; j < locsymcount; ++j)
13511 {
13512 if (local_got[j] > 0)
13513 {
13514 local_got[j] = gotoff;
13515 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
13516 }
13517 else
13518 local_got[j] = (bfd_vma) -1;
13519 }
13520 }
13521
13522 /* Then the global .got entries. .plt refcounts are handled by
13523 adjust_dynamic_symbol */
13524 gofarg.gotoff = gotoff;
13525 gofarg.info = info;
13526 elf_link_hash_traverse (elf_hash_table (info),
13527 elf_gc_allocate_got_offsets,
13528 &gofarg);
13529 return TRUE;
13530 }
13531
13532 /* Many folk need no more in the way of final link than this, once
13533 got entry reference counting is enabled. */
13534
13535 bfd_boolean
13536 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13537 {
13538 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13539 return FALSE;
13540
13541 /* Invoke the regular ELF backend linker to do all the work. */
13542 return bfd_elf_final_link (abfd, info);
13543 }
13544
13545 bfd_boolean
13546 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13547 {
13548 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
13549
13550 if (rcookie->bad_symtab)
13551 rcookie->rel = rcookie->rels;
13552
13553 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13554 {
13555 unsigned long r_symndx;
13556
13557 if (! rcookie->bad_symtab)
13558 if (rcookie->rel->r_offset > offset)
13559 return FALSE;
13560 if (rcookie->rel->r_offset != offset)
13561 continue;
13562
13563 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
13564 if (r_symndx == STN_UNDEF)
13565 return TRUE;
13566
13567 if (r_symndx >= rcookie->locsymcount
13568 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13569 {
13570 struct elf_link_hash_entry *h;
13571
13572 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13573
13574 while (h->root.type == bfd_link_hash_indirect
13575 || h->root.type == bfd_link_hash_warning)
13576 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13577
13578 if ((h->root.type == bfd_link_hash_defined
13579 || h->root.type == bfd_link_hash_defweak)
13580 && (h->root.u.def.section->owner != rcookie->abfd
13581 || h->root.u.def.section->kept_section != NULL
13582 || discarded_section (h->root.u.def.section)))
13583 return TRUE;
13584 }
13585 else
13586 {
13587 /* It's not a relocation against a global symbol,
13588 but it could be a relocation against a local
13589 symbol for a discarded section. */
13590 asection *isec;
13591 Elf_Internal_Sym *isym;
13592
13593 /* Need to: get the symbol; get the section. */
13594 isym = &rcookie->locsyms[r_symndx];
13595 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
13596 if (isec != NULL
13597 && (isec->kept_section != NULL
13598 || discarded_section (isec)))
13599 return TRUE;
13600 }
13601 return FALSE;
13602 }
13603 return FALSE;
13604 }
13605
13606 /* Discard unneeded references to discarded sections.
13607 Returns -1 on error, 1 if any section's size was changed, 0 if
13608 nothing changed. This function assumes that the relocations are in
13609 sorted order, which is true for all known assemblers. */
13610
13611 int
13612 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13613 {
13614 struct elf_reloc_cookie cookie;
13615 asection *o;
13616 bfd *abfd;
13617 int changed = 0;
13618
13619 if (info->traditional_format
13620 || !is_elf_hash_table (info->hash))
13621 return 0;
13622
13623 o = bfd_get_section_by_name (output_bfd, ".stab");
13624 if (o != NULL)
13625 {
13626 asection *i;
13627
13628 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13629 {
13630 if (i->size == 0
13631 || i->reloc_count == 0
13632 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13633 continue;
13634
13635 abfd = i->owner;
13636 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13637 continue;
13638
13639 if (!init_reloc_cookie_for_section (&cookie, info, i))
13640 return -1;
13641
13642 if (_bfd_discard_section_stabs (abfd, i,
13643 elf_section_data (i)->sec_info,
13644 bfd_elf_reloc_symbol_deleted_p,
13645 &cookie))
13646 changed = 1;
13647
13648 fini_reloc_cookie_for_section (&cookie, i);
13649 }
13650 }
13651
13652 o = NULL;
13653 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13654 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
13655 if (o != NULL)
13656 {
13657 asection *i;
13658
13659 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13660 {
13661 if (i->size == 0)
13662 continue;
13663
13664 abfd = i->owner;
13665 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13666 continue;
13667
13668 if (!init_reloc_cookie_for_section (&cookie, info, i))
13669 return -1;
13670
13671 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13672 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
13673 bfd_elf_reloc_symbol_deleted_p,
13674 &cookie))
13675 changed = 1;
13676
13677 fini_reloc_cookie_for_section (&cookie, i);
13678 }
13679 }
13680
13681 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13682 {
13683 const struct elf_backend_data *bed;
13684
13685 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13686 continue;
13687
13688 bed = get_elf_backend_data (abfd);
13689
13690 if (bed->elf_backend_discard_info != NULL)
13691 {
13692 if (!init_reloc_cookie (&cookie, info, abfd))
13693 return -1;
13694
13695 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
13696 changed = 1;
13697
13698 fini_reloc_cookie (&cookie, abfd);
13699 }
13700 }
13701
13702 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13703 _bfd_elf_end_eh_frame_parsing (info);
13704
13705 if (info->eh_frame_hdr_type
13706 && !bfd_link_relocatable (info)
13707 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
13708 changed = 1;
13709
13710 return changed;
13711 }
13712
13713 bfd_boolean
13714 _bfd_elf_section_already_linked (bfd *abfd,
13715 asection *sec,
13716 struct bfd_link_info *info)
13717 {
13718 flagword flags;
13719 const char *name, *key;
13720 struct bfd_section_already_linked *l;
13721 struct bfd_section_already_linked_hash_entry *already_linked_list;
13722
13723 if (sec->output_section == bfd_abs_section_ptr)
13724 return FALSE;
13725
13726 flags = sec->flags;
13727
13728 /* Return if it isn't a linkonce section. A comdat group section
13729 also has SEC_LINK_ONCE set. */
13730 if ((flags & SEC_LINK_ONCE) == 0)
13731 return FALSE;
13732
13733 /* Don't put group member sections on our list of already linked
13734 sections. They are handled as a group via their group section. */
13735 if (elf_sec_group (sec) != NULL)
13736 return FALSE;
13737
13738 /* For a SHT_GROUP section, use the group signature as the key. */
13739 name = sec->name;
13740 if ((flags & SEC_GROUP) != 0
13741 && elf_next_in_group (sec) != NULL
13742 && elf_group_name (elf_next_in_group (sec)) != NULL)
13743 key = elf_group_name (elf_next_in_group (sec));
13744 else
13745 {
13746 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
13747 if (CONST_STRNEQ (name, ".gnu.linkonce.")
13748 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13749 key++;
13750 else
13751 /* Must be a user linkonce section that doesn't follow gcc's
13752 naming convention. In this case we won't be matching
13753 single member groups. */
13754 key = name;
13755 }
13756
13757 already_linked_list = bfd_section_already_linked_table_lookup (key);
13758
13759 for (l = already_linked_list->entry; l != NULL; l = l->next)
13760 {
13761 /* We may have 2 different types of sections on the list: group
13762 sections with a signature of <key> (<key> is some string),
13763 and linkonce sections named .gnu.linkonce.<type>.<key>.
13764 Match like sections. LTO plugin sections are an exception.
13765 They are always named .gnu.linkonce.t.<key> and match either
13766 type of section. */
13767 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13768 && ((flags & SEC_GROUP) != 0
13769 || strcmp (name, l->sec->name) == 0))
13770 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
13771 {
13772 /* The section has already been linked. See if we should
13773 issue a warning. */
13774 if (!_bfd_handle_already_linked (sec, l, info))
13775 return FALSE;
13776
13777 if (flags & SEC_GROUP)
13778 {
13779 asection *first = elf_next_in_group (sec);
13780 asection *s = first;
13781
13782 while (s != NULL)
13783 {
13784 s->output_section = bfd_abs_section_ptr;
13785 /* Record which group discards it. */
13786 s->kept_section = l->sec;
13787 s = elf_next_in_group (s);
13788 /* These lists are circular. */
13789 if (s == first)
13790 break;
13791 }
13792 }
13793
13794 return TRUE;
13795 }
13796 }
13797
13798 /* A single member comdat group section may be discarded by a
13799 linkonce section and vice versa. */
13800 if ((flags & SEC_GROUP) != 0)
13801 {
13802 asection *first = elf_next_in_group (sec);
13803
13804 if (first != NULL && elf_next_in_group (first) == first)
13805 /* Check this single member group against linkonce sections. */
13806 for (l = already_linked_list->entry; l != NULL; l = l->next)
13807 if ((l->sec->flags & SEC_GROUP) == 0
13808 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13809 {
13810 first->output_section = bfd_abs_section_ptr;
13811 first->kept_section = l->sec;
13812 sec->output_section = bfd_abs_section_ptr;
13813 break;
13814 }
13815 }
13816 else
13817 /* Check this linkonce section against single member groups. */
13818 for (l = already_linked_list->entry; l != NULL; l = l->next)
13819 if (l->sec->flags & SEC_GROUP)
13820 {
13821 asection *first = elf_next_in_group (l->sec);
13822
13823 if (first != NULL
13824 && elf_next_in_group (first) == first
13825 && bfd_elf_match_symbols_in_sections (first, sec, info))
13826 {
13827 sec->output_section = bfd_abs_section_ptr;
13828 sec->kept_section = first;
13829 break;
13830 }
13831 }
13832
13833 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13834 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13835 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13836 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13837 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13838 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13839 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13840 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13841 The reverse order cannot happen as there is never a bfd with only the
13842 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13843 matter as here were are looking only for cross-bfd sections. */
13844
13845 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
13846 for (l = already_linked_list->entry; l != NULL; l = l->next)
13847 if ((l->sec->flags & SEC_GROUP) == 0
13848 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
13849 {
13850 if (abfd != l->sec->owner)
13851 sec->output_section = bfd_abs_section_ptr;
13852 break;
13853 }
13854
13855 /* This is the first section with this name. Record it. */
13856 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
13857 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
13858 return sec->output_section == bfd_abs_section_ptr;
13859 }
13860
13861 bfd_boolean
13862 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
13863 {
13864 return sym->st_shndx == SHN_COMMON;
13865 }
13866
13867 unsigned int
13868 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
13869 {
13870 return SHN_COMMON;
13871 }
13872
13873 asection *
13874 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
13875 {
13876 return bfd_com_section_ptr;
13877 }
13878
13879 bfd_vma
13880 _bfd_elf_default_got_elt_size (bfd *abfd,
13881 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13882 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
13883 bfd *ibfd ATTRIBUTE_UNUSED,
13884 unsigned long symndx ATTRIBUTE_UNUSED)
13885 {
13886 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13887 return bed->s->arch_size / 8;
13888 }
13889
13890 /* Routines to support the creation of dynamic relocs. */
13891
13892 /* Returns the name of the dynamic reloc section associated with SEC. */
13893
13894 static const char *
13895 get_dynamic_reloc_section_name (bfd * abfd,
13896 asection * sec,
13897 bfd_boolean is_rela)
13898 {
13899 char *name;
13900 const char *old_name = bfd_get_section_name (NULL, sec);
13901 const char *prefix = is_rela ? ".rela" : ".rel";
13902
13903 if (old_name == NULL)
13904 return NULL;
13905
13906 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
13907 sprintf (name, "%s%s", prefix, old_name);
13908
13909 return name;
13910 }
13911
13912 /* Returns the dynamic reloc section associated with SEC.
13913 If necessary compute the name of the dynamic reloc section based
13914 on SEC's name (looked up in ABFD's string table) and the setting
13915 of IS_RELA. */
13916
13917 asection *
13918 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
13919 asection * sec,
13920 bfd_boolean is_rela)
13921 {
13922 asection * reloc_sec = elf_section_data (sec)->sreloc;
13923
13924 if (reloc_sec == NULL)
13925 {
13926 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13927
13928 if (name != NULL)
13929 {
13930 reloc_sec = bfd_get_linker_section (abfd, name);
13931
13932 if (reloc_sec != NULL)
13933 elf_section_data (sec)->sreloc = reloc_sec;
13934 }
13935 }
13936
13937 return reloc_sec;
13938 }
13939
13940 /* Returns the dynamic reloc section associated with SEC. If the
13941 section does not exist it is created and attached to the DYNOBJ
13942 bfd and stored in the SRELOC field of SEC's elf_section_data
13943 structure.
13944
13945 ALIGNMENT is the alignment for the newly created section and
13946 IS_RELA defines whether the name should be .rela.<SEC's name>
13947 or .rel.<SEC's name>. The section name is looked up in the
13948 string table associated with ABFD. */
13949
13950 asection *
13951 _bfd_elf_make_dynamic_reloc_section (asection *sec,
13952 bfd *dynobj,
13953 unsigned int alignment,
13954 bfd *abfd,
13955 bfd_boolean is_rela)
13956 {
13957 asection * reloc_sec = elf_section_data (sec)->sreloc;
13958
13959 if (reloc_sec == NULL)
13960 {
13961 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13962
13963 if (name == NULL)
13964 return NULL;
13965
13966 reloc_sec = bfd_get_linker_section (dynobj, name);
13967
13968 if (reloc_sec == NULL)
13969 {
13970 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
13971 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
13972 if ((sec->flags & SEC_ALLOC) != 0)
13973 flags |= SEC_ALLOC | SEC_LOAD;
13974
13975 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
13976 if (reloc_sec != NULL)
13977 {
13978 /* _bfd_elf_get_sec_type_attr chooses a section type by
13979 name. Override as it may be wrong, eg. for a user
13980 section named "auto" we'll get ".relauto" which is
13981 seen to be a .rela section. */
13982 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
13983 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
13984 reloc_sec = NULL;
13985 }
13986 }
13987
13988 elf_section_data (sec)->sreloc = reloc_sec;
13989 }
13990
13991 return reloc_sec;
13992 }
13993
13994 /* Copy the ELF symbol type and other attributes for a linker script
13995 assignment from HSRC to HDEST. Generally this should be treated as
13996 if we found a strong non-dynamic definition for HDEST (except that
13997 ld ignores multiple definition errors). */
13998 void
13999 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14000 struct bfd_link_hash_entry *hdest,
14001 struct bfd_link_hash_entry *hsrc)
14002 {
14003 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14004 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14005 Elf_Internal_Sym isym;
14006
14007 ehdest->type = ehsrc->type;
14008 ehdest->target_internal = ehsrc->target_internal;
14009
14010 isym.st_other = ehsrc->other;
14011 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
14012 }
14013
14014 /* Append a RELA relocation REL to section S in BFD. */
14015
14016 void
14017 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14018 {
14019 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14020 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14021 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14022 bed->s->swap_reloca_out (abfd, rel, loc);
14023 }
14024
14025 /* Append a REL relocation REL to section S in BFD. */
14026
14027 void
14028 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14029 {
14030 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14031 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14032 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
14033 bed->s->swap_reloc_out (abfd, rel, loc);
14034 }
This page took 0.322154 seconds and 5 git commands to generate.