99f867dc75c70c70fdd1544abcaf69f7c7d26de5
[deliverable/binutils-gdb.git] / bfd / elflink.c
1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2017 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 bh = &h->root;
123 }
124 else
125 bh = NULL;
126
127 bed = get_elf_backend_data (abfd);
128 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
129 sec, 0, NULL, FALSE, bed->collect,
130 &bh))
131 return NULL;
132 h = (struct elf_link_hash_entry *) bh;
133 BFD_ASSERT (h != NULL);
134 h->def_regular = 1;
135 h->non_elf = 0;
136 h->root.linker_def = 1;
137 h->type = STT_OBJECT;
138 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
139 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
140
141 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
142 return h;
143 }
144
145 bfd_boolean
146 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
147 {
148 flagword flags;
149 asection *s;
150 struct elf_link_hash_entry *h;
151 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
152 struct elf_link_hash_table *htab = elf_hash_table (info);
153
154 /* This function may be called more than once. */
155 if (htab->sgot != NULL)
156 return TRUE;
157
158 flags = bed->dynamic_sec_flags;
159
160 s = bfd_make_section_anyway_with_flags (abfd,
161 (bed->rela_plts_and_copies_p
162 ? ".rela.got" : ".rel.got"),
163 (bed->dynamic_sec_flags
164 | SEC_READONLY));
165 if (s == NULL
166 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
167 return FALSE;
168 htab->srelgot = s;
169
170 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
171 if (s == NULL
172 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
173 return FALSE;
174 htab->sgot = s;
175
176 if (bed->want_got_plt)
177 {
178 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
179 if (s == NULL
180 || !bfd_set_section_alignment (abfd, s,
181 bed->s->log_file_align))
182 return FALSE;
183 htab->sgotplt = s;
184 }
185
186 /* The first bit of the global offset table is the header. */
187 s->size += bed->got_header_size;
188
189 if (bed->want_got_sym)
190 {
191 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
192 (or .got.plt) section. We don't do this in the linker script
193 because we don't want to define the symbol if we are not creating
194 a global offset table. */
195 h = _bfd_elf_define_linkage_sym (abfd, info, s,
196 "_GLOBAL_OFFSET_TABLE_");
197 elf_hash_table (info)->hgot = h;
198 if (h == NULL)
199 return FALSE;
200 }
201
202 return TRUE;
203 }
204 \f
205 /* Create a strtab to hold the dynamic symbol names. */
206 static bfd_boolean
207 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
208 {
209 struct elf_link_hash_table *hash_table;
210
211 hash_table = elf_hash_table (info);
212 if (hash_table->dynobj == NULL)
213 {
214 /* We may not set dynobj, an input file holding linker created
215 dynamic sections to abfd, which may be a dynamic object with
216 its own dynamic sections. We need to find a normal input file
217 to hold linker created sections if possible. */
218 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
219 {
220 bfd *ibfd;
221 asection *s;
222 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
223 if ((ibfd->flags
224 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
225 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
226 && !((s = ibfd->sections) != NULL
227 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
228 {
229 abfd = ibfd;
230 break;
231 }
232 }
233 hash_table->dynobj = abfd;
234 }
235
236 if (hash_table->dynstr == NULL)
237 {
238 hash_table->dynstr = _bfd_elf_strtab_init ();
239 if (hash_table->dynstr == NULL)
240 return FALSE;
241 }
242 return TRUE;
243 }
244
245 /* Create some sections which will be filled in with dynamic linking
246 information. ABFD is an input file which requires dynamic sections
247 to be created. The dynamic sections take up virtual memory space
248 when the final executable is run, so we need to create them before
249 addresses are assigned to the output sections. We work out the
250 actual contents and size of these sections later. */
251
252 bfd_boolean
253 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
254 {
255 flagword flags;
256 asection *s;
257 const struct elf_backend_data *bed;
258 struct elf_link_hash_entry *h;
259
260 if (! is_elf_hash_table (info->hash))
261 return FALSE;
262
263 if (elf_hash_table (info)->dynamic_sections_created)
264 return TRUE;
265
266 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
267 return FALSE;
268
269 abfd = elf_hash_table (info)->dynobj;
270 bed = get_elf_backend_data (abfd);
271
272 flags = bed->dynamic_sec_flags;
273
274 /* A dynamically linked executable has a .interp section, but a
275 shared library does not. */
276 if (bfd_link_executable (info) && !info->nointerp)
277 {
278 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
279 flags | SEC_READONLY);
280 if (s == NULL)
281 return FALSE;
282 }
283
284 /* Create sections to hold version informations. These are removed
285 if they are not needed. */
286 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
287 flags | SEC_READONLY);
288 if (s == NULL
289 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
290 return FALSE;
291
292 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
293 flags | SEC_READONLY);
294 if (s == NULL
295 || ! bfd_set_section_alignment (abfd, s, 1))
296 return FALSE;
297
298 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
299 flags | SEC_READONLY);
300 if (s == NULL
301 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
302 return FALSE;
303
304 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
305 flags | SEC_READONLY);
306 if (s == NULL
307 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
308 return FALSE;
309 elf_hash_table (info)->dynsym = s;
310
311 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
312 flags | SEC_READONLY);
313 if (s == NULL)
314 return FALSE;
315
316 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
317 if (s == NULL
318 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
319 return FALSE;
320
321 /* The special symbol _DYNAMIC is always set to the start of the
322 .dynamic section. We could set _DYNAMIC in a linker script, but we
323 only want to define it if we are, in fact, creating a .dynamic
324 section. We don't want to define it if there is no .dynamic
325 section, since on some ELF platforms the start up code examines it
326 to decide how to initialize the process. */
327 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
328 elf_hash_table (info)->hdynamic = h;
329 if (h == NULL)
330 return FALSE;
331
332 if (info->emit_hash)
333 {
334 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
335 flags | SEC_READONLY);
336 if (s == NULL
337 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
338 return FALSE;
339 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
340 }
341
342 if (info->emit_gnu_hash)
343 {
344 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
345 flags | SEC_READONLY);
346 if (s == NULL
347 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
348 return FALSE;
349 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
350 4 32-bit words followed by variable count of 64-bit words, then
351 variable count of 32-bit words. */
352 if (bed->s->arch_size == 64)
353 elf_section_data (s)->this_hdr.sh_entsize = 0;
354 else
355 elf_section_data (s)->this_hdr.sh_entsize = 4;
356 }
357
358 /* Let the backend create the rest of the sections. This lets the
359 backend set the right flags. The backend will normally create
360 the .got and .plt sections. */
361 if (bed->elf_backend_create_dynamic_sections == NULL
362 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
363 return FALSE;
364
365 elf_hash_table (info)->dynamic_sections_created = TRUE;
366
367 return TRUE;
368 }
369
370 /* Create dynamic sections when linking against a dynamic object. */
371
372 bfd_boolean
373 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
374 {
375 flagword flags, pltflags;
376 struct elf_link_hash_entry *h;
377 asection *s;
378 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
379 struct elf_link_hash_table *htab = elf_hash_table (info);
380
381 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
382 .rel[a].bss sections. */
383 flags = bed->dynamic_sec_flags;
384
385 pltflags = flags;
386 if (bed->plt_not_loaded)
387 /* We do not clear SEC_ALLOC here because we still want the OS to
388 allocate space for the section; it's just that there's nothing
389 to read in from the object file. */
390 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
391 else
392 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
393 if (bed->plt_readonly)
394 pltflags |= SEC_READONLY;
395
396 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
397 if (s == NULL
398 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
399 return FALSE;
400 htab->splt = s;
401
402 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
403 .plt section. */
404 if (bed->want_plt_sym)
405 {
406 h = _bfd_elf_define_linkage_sym (abfd, info, s,
407 "_PROCEDURE_LINKAGE_TABLE_");
408 elf_hash_table (info)->hplt = h;
409 if (h == NULL)
410 return FALSE;
411 }
412
413 s = bfd_make_section_anyway_with_flags (abfd,
414 (bed->rela_plts_and_copies_p
415 ? ".rela.plt" : ".rel.plt"),
416 flags | SEC_READONLY);
417 if (s == NULL
418 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
419 return FALSE;
420 htab->srelplt = s;
421
422 if (! _bfd_elf_create_got_section (abfd, info))
423 return FALSE;
424
425 if (bed->want_dynbss)
426 {
427 /* The .dynbss section is a place to put symbols which are defined
428 by dynamic objects, are referenced by regular objects, and are
429 not functions. We must allocate space for them in the process
430 image and use a R_*_COPY reloc to tell the dynamic linker to
431 initialize them at run time. The linker script puts the .dynbss
432 section into the .bss section of the final image. */
433 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
434 SEC_ALLOC | SEC_LINKER_CREATED);
435 if (s == NULL)
436 return FALSE;
437 htab->sdynbss = s;
438
439 if (bed->want_dynrelro)
440 {
441 /* Similarly, but for symbols that were originally in read-only
442 sections. This section doesn't really need to have contents,
443 but make it like other .data.rel.ro sections. */
444 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
445 flags);
446 if (s == NULL)
447 return FALSE;
448 htab->sdynrelro = s;
449 }
450
451 /* The .rel[a].bss section holds copy relocs. This section is not
452 normally needed. We need to create it here, though, so that the
453 linker will map it to an output section. We can't just create it
454 only if we need it, because we will not know whether we need it
455 until we have seen all the input files, and the first time the
456 main linker code calls BFD after examining all the input files
457 (size_dynamic_sections) the input sections have already been
458 mapped to the output sections. If the section turns out not to
459 be needed, we can discard it later. We will never need this
460 section when generating a shared object, since they do not use
461 copy relocs. */
462 if (bfd_link_executable (info))
463 {
464 s = bfd_make_section_anyway_with_flags (abfd,
465 (bed->rela_plts_and_copies_p
466 ? ".rela.bss" : ".rel.bss"),
467 flags | SEC_READONLY);
468 if (s == NULL
469 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
470 return FALSE;
471 htab->srelbss = s;
472
473 if (bed->want_dynrelro)
474 {
475 s = (bfd_make_section_anyway_with_flags
476 (abfd, (bed->rela_plts_and_copies_p
477 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
478 flags | SEC_READONLY));
479 if (s == NULL
480 || ! bfd_set_section_alignment (abfd, s,
481 bed->s->log_file_align))
482 return FALSE;
483 htab->sreldynrelro = s;
484 }
485 }
486 }
487
488 return TRUE;
489 }
490 \f
491 /* Record a new dynamic symbol. We record the dynamic symbols as we
492 read the input files, since we need to have a list of all of them
493 before we can determine the final sizes of the output sections.
494 Note that we may actually call this function even though we are not
495 going to output any dynamic symbols; in some cases we know that a
496 symbol should be in the dynamic symbol table, but only if there is
497 one. */
498
499 bfd_boolean
500 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
501 struct elf_link_hash_entry *h)
502 {
503 if (h->dynindx == -1)
504 {
505 struct elf_strtab_hash *dynstr;
506 char *p;
507 const char *name;
508 size_t indx;
509
510 /* XXX: The ABI draft says the linker must turn hidden and
511 internal symbols into STB_LOCAL symbols when producing the
512 DSO. However, if ld.so honors st_other in the dynamic table,
513 this would not be necessary. */
514 switch (ELF_ST_VISIBILITY (h->other))
515 {
516 case STV_INTERNAL:
517 case STV_HIDDEN:
518 if (h->root.type != bfd_link_hash_undefined
519 && h->root.type != bfd_link_hash_undefweak)
520 {
521 h->forced_local = 1;
522 if (!elf_hash_table (info)->is_relocatable_executable)
523 return TRUE;
524 }
525
526 default:
527 break;
528 }
529
530 h->dynindx = elf_hash_table (info)->dynsymcount;
531 ++elf_hash_table (info)->dynsymcount;
532
533 dynstr = elf_hash_table (info)->dynstr;
534 if (dynstr == NULL)
535 {
536 /* Create a strtab to hold the dynamic symbol names. */
537 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
538 if (dynstr == NULL)
539 return FALSE;
540 }
541
542 /* We don't put any version information in the dynamic string
543 table. */
544 name = h->root.root.string;
545 p = strchr (name, ELF_VER_CHR);
546 if (p != NULL)
547 /* We know that the p points into writable memory. In fact,
548 there are only a few symbols that have read-only names, being
549 those like _GLOBAL_OFFSET_TABLE_ that are created specially
550 by the backends. Most symbols will have names pointing into
551 an ELF string table read from a file, or to objalloc memory. */
552 *p = 0;
553
554 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
555
556 if (p != NULL)
557 *p = ELF_VER_CHR;
558
559 if (indx == (size_t) -1)
560 return FALSE;
561 h->dynstr_index = indx;
562 }
563
564 return TRUE;
565 }
566 \f
567 /* Mark a symbol dynamic. */
568
569 static void
570 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
571 struct elf_link_hash_entry *h,
572 Elf_Internal_Sym *sym)
573 {
574 struct bfd_elf_dynamic_list *d = info->dynamic_list;
575
576 /* It may be called more than once on the same H. */
577 if(h->dynamic || bfd_link_relocatable (info))
578 return;
579
580 if ((info->dynamic_data
581 && (h->type == STT_OBJECT
582 || h->type == STT_COMMON
583 || (sym != NULL
584 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
585 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
586 || (d != NULL
587 && h->non_elf
588 && (*d->match) (&d->head, NULL, h->root.root.string)))
589 h->dynamic = 1;
590 }
591
592 /* Record an assignment to a symbol made by a linker script. We need
593 this in case some dynamic object refers to this symbol. */
594
595 bfd_boolean
596 bfd_elf_record_link_assignment (bfd *output_bfd,
597 struct bfd_link_info *info,
598 const char *name,
599 bfd_boolean provide,
600 bfd_boolean hidden)
601 {
602 struct elf_link_hash_entry *h, *hv;
603 struct elf_link_hash_table *htab;
604 const struct elf_backend_data *bed;
605
606 if (!is_elf_hash_table (info->hash))
607 return TRUE;
608
609 htab = elf_hash_table (info);
610 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
611 if (h == NULL)
612 return provide;
613
614 if (h->root.type == bfd_link_hash_warning)
615 h = (struct elf_link_hash_entry *) h->root.u.i.link;
616
617 if (h->versioned == unknown)
618 {
619 /* Set versioned if symbol version is unknown. */
620 char *version = strrchr (name, ELF_VER_CHR);
621 if (version)
622 {
623 if (version > name && version[-1] != ELF_VER_CHR)
624 h->versioned = versioned_hidden;
625 else
626 h->versioned = versioned;
627 }
628 }
629
630 /* Symbols defined in a linker script but not referenced anywhere
631 else will have non_elf set. */
632 if (h->non_elf)
633 {
634 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
635 h->non_elf = 0;
636 }
637
638 switch (h->root.type)
639 {
640 case bfd_link_hash_defined:
641 case bfd_link_hash_defweak:
642 case bfd_link_hash_common:
643 break;
644 case bfd_link_hash_undefweak:
645 case bfd_link_hash_undefined:
646 /* Since we're defining the symbol, don't let it seem to have not
647 been defined. record_dynamic_symbol and size_dynamic_sections
648 may depend on this. */
649 h->root.type = bfd_link_hash_new;
650 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
651 bfd_link_repair_undef_list (&htab->root);
652 break;
653 case bfd_link_hash_new:
654 break;
655 case bfd_link_hash_indirect:
656 /* We had a versioned symbol in a dynamic library. We make the
657 the versioned symbol point to this one. */
658 bed = get_elf_backend_data (output_bfd);
659 hv = h;
660 while (hv->root.type == bfd_link_hash_indirect
661 || hv->root.type == bfd_link_hash_warning)
662 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
663 /* We don't need to update h->root.u since linker will set them
664 later. */
665 h->root.type = bfd_link_hash_undefined;
666 hv->root.type = bfd_link_hash_indirect;
667 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
668 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
669 break;
670 default:
671 BFD_FAIL ();
672 return FALSE;
673 }
674
675 /* If this symbol is being provided by the linker script, and it is
676 currently defined by a dynamic object, but not by a regular
677 object, then mark it as undefined so that the generic linker will
678 force the correct value. */
679 if (provide
680 && h->def_dynamic
681 && !h->def_regular)
682 h->root.type = bfd_link_hash_undefined;
683
684 /* If this symbol is not being provided by the linker script, and it is
685 currently defined by a dynamic object, but not by a regular object,
686 then clear out any version information because the symbol will not be
687 associated with the dynamic object any more. */
688 if (!provide
689 && h->def_dynamic
690 && !h->def_regular)
691 h->verinfo.verdef = NULL;
692
693 /* Make sure this symbol is not garbage collected. */
694 h->mark = 1;
695
696 h->def_regular = 1;
697
698 if (hidden)
699 {
700 bed = get_elf_backend_data (output_bfd);
701 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
702 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
703 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
704 }
705
706 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
707 and executables. */
708 if (!bfd_link_relocatable (info)
709 && h->dynindx != -1
710 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
711 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
712 h->forced_local = 1;
713
714 if ((h->def_dynamic
715 || h->ref_dynamic
716 || bfd_link_dll (info)
717 || elf_hash_table (info)->is_relocatable_executable)
718 && h->dynindx == -1)
719 {
720 if (! bfd_elf_link_record_dynamic_symbol (info, h))
721 return FALSE;
722
723 /* If this is a weak defined symbol, and we know a corresponding
724 real symbol from the same dynamic object, make sure the real
725 symbol is also made into a dynamic symbol. */
726 if (h->is_weakalias)
727 {
728 struct elf_link_hash_entry *def = weakdef (h);
729
730 if (def->dynindx == -1
731 && !bfd_elf_link_record_dynamic_symbol (info, def))
732 return FALSE;
733 }
734 }
735
736 return TRUE;
737 }
738
739 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
740 success, and 2 on a failure caused by attempting to record a symbol
741 in a discarded section, eg. a discarded link-once section symbol. */
742
743 int
744 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
745 bfd *input_bfd,
746 long input_indx)
747 {
748 bfd_size_type amt;
749 struct elf_link_local_dynamic_entry *entry;
750 struct elf_link_hash_table *eht;
751 struct elf_strtab_hash *dynstr;
752 size_t dynstr_index;
753 char *name;
754 Elf_External_Sym_Shndx eshndx;
755 char esym[sizeof (Elf64_External_Sym)];
756
757 if (! is_elf_hash_table (info->hash))
758 return 0;
759
760 /* See if the entry exists already. */
761 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
762 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
763 return 1;
764
765 amt = sizeof (*entry);
766 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
767 if (entry == NULL)
768 return 0;
769
770 /* Go find the symbol, so that we can find it's name. */
771 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
772 1, input_indx, &entry->isym, esym, &eshndx))
773 {
774 bfd_release (input_bfd, entry);
775 return 0;
776 }
777
778 if (entry->isym.st_shndx != SHN_UNDEF
779 && entry->isym.st_shndx < SHN_LORESERVE)
780 {
781 asection *s;
782
783 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
784 if (s == NULL || bfd_is_abs_section (s->output_section))
785 {
786 /* We can still bfd_release here as nothing has done another
787 bfd_alloc. We can't do this later in this function. */
788 bfd_release (input_bfd, entry);
789 return 2;
790 }
791 }
792
793 name = (bfd_elf_string_from_elf_section
794 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
795 entry->isym.st_name));
796
797 dynstr = elf_hash_table (info)->dynstr;
798 if (dynstr == NULL)
799 {
800 /* Create a strtab to hold the dynamic symbol names. */
801 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
802 if (dynstr == NULL)
803 return 0;
804 }
805
806 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
807 if (dynstr_index == (size_t) -1)
808 return 0;
809 entry->isym.st_name = dynstr_index;
810
811 eht = elf_hash_table (info);
812
813 entry->next = eht->dynlocal;
814 eht->dynlocal = entry;
815 entry->input_bfd = input_bfd;
816 entry->input_indx = input_indx;
817 eht->dynsymcount++;
818
819 /* Whatever binding the symbol had before, it's now local. */
820 entry->isym.st_info
821 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
822
823 /* The dynindx will be set at the end of size_dynamic_sections. */
824
825 return 1;
826 }
827
828 /* Return the dynindex of a local dynamic symbol. */
829
830 long
831 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
832 bfd *input_bfd,
833 long input_indx)
834 {
835 struct elf_link_local_dynamic_entry *e;
836
837 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
838 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
839 return e->dynindx;
840 return -1;
841 }
842
843 /* This function is used to renumber the dynamic symbols, if some of
844 them are removed because they are marked as local. This is called
845 via elf_link_hash_traverse. */
846
847 static bfd_boolean
848 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
849 void *data)
850 {
851 size_t *count = (size_t *) data;
852
853 if (h->forced_local)
854 return TRUE;
855
856 if (h->dynindx != -1)
857 h->dynindx = ++(*count);
858
859 return TRUE;
860 }
861
862
863 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
864 STB_LOCAL binding. */
865
866 static bfd_boolean
867 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
868 void *data)
869 {
870 size_t *count = (size_t *) data;
871
872 if (!h->forced_local)
873 return TRUE;
874
875 if (h->dynindx != -1)
876 h->dynindx = ++(*count);
877
878 return TRUE;
879 }
880
881 /* Return true if the dynamic symbol for a given section should be
882 omitted when creating a shared library. */
883 bfd_boolean
884 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
885 struct bfd_link_info *info,
886 asection *p)
887 {
888 struct elf_link_hash_table *htab;
889 asection *ip;
890
891 switch (elf_section_data (p)->this_hdr.sh_type)
892 {
893 case SHT_PROGBITS:
894 case SHT_NOBITS:
895 /* If sh_type is yet undecided, assume it could be
896 SHT_PROGBITS/SHT_NOBITS. */
897 case SHT_NULL:
898 htab = elf_hash_table (info);
899 if (p == htab->tls_sec)
900 return FALSE;
901
902 if (htab->text_index_section != NULL)
903 return p != htab->text_index_section && p != htab->data_index_section;
904
905 return (htab->dynobj != NULL
906 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
907 && ip->output_section == p);
908
909 /* There shouldn't be section relative relocations
910 against any other section. */
911 default:
912 return TRUE;
913 }
914 }
915
916 /* Assign dynsym indices. In a shared library we generate a section
917 symbol for each output section, which come first. Next come symbols
918 which have been forced to local binding. Then all of the back-end
919 allocated local dynamic syms, followed by the rest of the global
920 symbols. */
921
922 static unsigned long
923 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
924 struct bfd_link_info *info,
925 unsigned long *section_sym_count)
926 {
927 unsigned long dynsymcount = 0;
928
929 if (bfd_link_pic (info)
930 || elf_hash_table (info)->is_relocatable_executable)
931 {
932 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
933 asection *p;
934 for (p = output_bfd->sections; p ; p = p->next)
935 if ((p->flags & SEC_EXCLUDE) == 0
936 && (p->flags & SEC_ALLOC) != 0
937 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
938 elf_section_data (p)->dynindx = ++dynsymcount;
939 else
940 elf_section_data (p)->dynindx = 0;
941 }
942 *section_sym_count = dynsymcount;
943
944 elf_link_hash_traverse (elf_hash_table (info),
945 elf_link_renumber_local_hash_table_dynsyms,
946 &dynsymcount);
947
948 if (elf_hash_table (info)->dynlocal)
949 {
950 struct elf_link_local_dynamic_entry *p;
951 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
952 p->dynindx = ++dynsymcount;
953 }
954 elf_hash_table (info)->local_dynsymcount = dynsymcount;
955
956 elf_link_hash_traverse (elf_hash_table (info),
957 elf_link_renumber_hash_table_dynsyms,
958 &dynsymcount);
959
960 /* There is an unused NULL entry at the head of the table which we
961 must account for in our count even if the table is empty since it
962 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
963 .dynamic section. */
964 dynsymcount++;
965
966 elf_hash_table (info)->dynsymcount = dynsymcount;
967 return dynsymcount;
968 }
969
970 /* Merge st_other field. */
971
972 static void
973 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
974 const Elf_Internal_Sym *isym, asection *sec,
975 bfd_boolean definition, bfd_boolean dynamic)
976 {
977 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
978
979 /* If st_other has a processor-specific meaning, specific
980 code might be needed here. */
981 if (bed->elf_backend_merge_symbol_attribute)
982 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
983 dynamic);
984
985 if (!dynamic)
986 {
987 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
988 unsigned hvis = ELF_ST_VISIBILITY (h->other);
989
990 /* Keep the most constraining visibility. Leave the remainder
991 of the st_other field to elf_backend_merge_symbol_attribute. */
992 if (symvis - 1 < hvis - 1)
993 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
994 }
995 else if (definition
996 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
997 && (sec->flags & SEC_READONLY) == 0)
998 h->protected_def = 1;
999 }
1000
1001 /* This function is called when we want to merge a new symbol with an
1002 existing symbol. It handles the various cases which arise when we
1003 find a definition in a dynamic object, or when there is already a
1004 definition in a dynamic object. The new symbol is described by
1005 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1006 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1007 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1008 of an old common symbol. We set OVERRIDE if the old symbol is
1009 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1010 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1011 to change. By OK to change, we mean that we shouldn't warn if the
1012 type or size does change. */
1013
1014 static bfd_boolean
1015 _bfd_elf_merge_symbol (bfd *abfd,
1016 struct bfd_link_info *info,
1017 const char *name,
1018 Elf_Internal_Sym *sym,
1019 asection **psec,
1020 bfd_vma *pvalue,
1021 struct elf_link_hash_entry **sym_hash,
1022 bfd **poldbfd,
1023 bfd_boolean *pold_weak,
1024 unsigned int *pold_alignment,
1025 bfd_boolean *skip,
1026 bfd_boolean *override,
1027 bfd_boolean *type_change_ok,
1028 bfd_boolean *size_change_ok,
1029 bfd_boolean *matched)
1030 {
1031 asection *sec, *oldsec;
1032 struct elf_link_hash_entry *h;
1033 struct elf_link_hash_entry *hi;
1034 struct elf_link_hash_entry *flip;
1035 int bind;
1036 bfd *oldbfd;
1037 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1038 bfd_boolean newweak, oldweak, newfunc, oldfunc;
1039 const struct elf_backend_data *bed;
1040 char *new_version;
1041 bfd_boolean default_sym = *matched;
1042
1043 *skip = FALSE;
1044 *override = FALSE;
1045
1046 sec = *psec;
1047 bind = ELF_ST_BIND (sym->st_info);
1048
1049 if (! bfd_is_und_section (sec))
1050 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1051 else
1052 h = ((struct elf_link_hash_entry *)
1053 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1054 if (h == NULL)
1055 return FALSE;
1056 *sym_hash = h;
1057
1058 bed = get_elf_backend_data (abfd);
1059
1060 /* NEW_VERSION is the symbol version of the new symbol. */
1061 if (h->versioned != unversioned)
1062 {
1063 /* Symbol version is unknown or versioned. */
1064 new_version = strrchr (name, ELF_VER_CHR);
1065 if (new_version)
1066 {
1067 if (h->versioned == unknown)
1068 {
1069 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1070 h->versioned = versioned_hidden;
1071 else
1072 h->versioned = versioned;
1073 }
1074 new_version += 1;
1075 if (new_version[0] == '\0')
1076 new_version = NULL;
1077 }
1078 else
1079 h->versioned = unversioned;
1080 }
1081 else
1082 new_version = NULL;
1083
1084 /* For merging, we only care about real symbols. But we need to make
1085 sure that indirect symbol dynamic flags are updated. */
1086 hi = h;
1087 while (h->root.type == bfd_link_hash_indirect
1088 || h->root.type == bfd_link_hash_warning)
1089 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1090
1091 if (!*matched)
1092 {
1093 if (hi == h || h->root.type == bfd_link_hash_new)
1094 *matched = TRUE;
1095 else
1096 {
1097 /* OLD_HIDDEN is true if the existing symbol is only visible
1098 to the symbol with the same symbol version. NEW_HIDDEN is
1099 true if the new symbol is only visible to the symbol with
1100 the same symbol version. */
1101 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1102 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1103 if (!old_hidden && !new_hidden)
1104 /* The new symbol matches the existing symbol if both
1105 aren't hidden. */
1106 *matched = TRUE;
1107 else
1108 {
1109 /* OLD_VERSION is the symbol version of the existing
1110 symbol. */
1111 char *old_version;
1112
1113 if (h->versioned >= versioned)
1114 old_version = strrchr (h->root.root.string,
1115 ELF_VER_CHR) + 1;
1116 else
1117 old_version = NULL;
1118
1119 /* The new symbol matches the existing symbol if they
1120 have the same symbol version. */
1121 *matched = (old_version == new_version
1122 || (old_version != NULL
1123 && new_version != NULL
1124 && strcmp (old_version, new_version) == 0));
1125 }
1126 }
1127 }
1128
1129 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1130 existing symbol. */
1131
1132 oldbfd = NULL;
1133 oldsec = NULL;
1134 switch (h->root.type)
1135 {
1136 default:
1137 break;
1138
1139 case bfd_link_hash_undefined:
1140 case bfd_link_hash_undefweak:
1141 oldbfd = h->root.u.undef.abfd;
1142 break;
1143
1144 case bfd_link_hash_defined:
1145 case bfd_link_hash_defweak:
1146 oldbfd = h->root.u.def.section->owner;
1147 oldsec = h->root.u.def.section;
1148 break;
1149
1150 case bfd_link_hash_common:
1151 oldbfd = h->root.u.c.p->section->owner;
1152 oldsec = h->root.u.c.p->section;
1153 if (pold_alignment)
1154 *pold_alignment = h->root.u.c.p->alignment_power;
1155 break;
1156 }
1157 if (poldbfd && *poldbfd == NULL)
1158 *poldbfd = oldbfd;
1159
1160 /* Differentiate strong and weak symbols. */
1161 newweak = bind == STB_WEAK;
1162 oldweak = (h->root.type == bfd_link_hash_defweak
1163 || h->root.type == bfd_link_hash_undefweak);
1164 if (pold_weak)
1165 *pold_weak = oldweak;
1166
1167 /* We have to check it for every instance since the first few may be
1168 references and not all compilers emit symbol type for undefined
1169 symbols. */
1170 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1171
1172 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1173 respectively, is from a dynamic object. */
1174
1175 newdyn = (abfd->flags & DYNAMIC) != 0;
1176
1177 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1178 syms and defined syms in dynamic libraries respectively.
1179 ref_dynamic on the other hand can be set for a symbol defined in
1180 a dynamic library, and def_dynamic may not be set; When the
1181 definition in a dynamic lib is overridden by a definition in the
1182 executable use of the symbol in the dynamic lib becomes a
1183 reference to the executable symbol. */
1184 if (newdyn)
1185 {
1186 if (bfd_is_und_section (sec))
1187 {
1188 if (bind != STB_WEAK)
1189 {
1190 h->ref_dynamic_nonweak = 1;
1191 hi->ref_dynamic_nonweak = 1;
1192 }
1193 }
1194 else
1195 {
1196 /* Update the existing symbol only if they match. */
1197 if (*matched)
1198 h->dynamic_def = 1;
1199 hi->dynamic_def = 1;
1200 }
1201 }
1202
1203 /* If we just created the symbol, mark it as being an ELF symbol.
1204 Other than that, there is nothing to do--there is no merge issue
1205 with a newly defined symbol--so we just return. */
1206
1207 if (h->root.type == bfd_link_hash_new)
1208 {
1209 h->non_elf = 0;
1210 return TRUE;
1211 }
1212
1213 /* In cases involving weak versioned symbols, we may wind up trying
1214 to merge a symbol with itself. Catch that here, to avoid the
1215 confusion that results if we try to override a symbol with
1216 itself. The additional tests catch cases like
1217 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1218 dynamic object, which we do want to handle here. */
1219 if (abfd == oldbfd
1220 && (newweak || oldweak)
1221 && ((abfd->flags & DYNAMIC) == 0
1222 || !h->def_regular))
1223 return TRUE;
1224
1225 olddyn = FALSE;
1226 if (oldbfd != NULL)
1227 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1228 else if (oldsec != NULL)
1229 {
1230 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1231 indices used by MIPS ELF. */
1232 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1233 }
1234
1235 /* Handle a case where plugin_notice won't be called and thus won't
1236 set the non_ir_ref flags on the first pass over symbols. */
1237 if (oldbfd != NULL
1238 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1239 && newdyn != olddyn)
1240 {
1241 h->root.non_ir_ref_dynamic = TRUE;
1242 hi->root.non_ir_ref_dynamic = TRUE;
1243 }
1244
1245 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1246 respectively, appear to be a definition rather than reference. */
1247
1248 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1249
1250 olddef = (h->root.type != bfd_link_hash_undefined
1251 && h->root.type != bfd_link_hash_undefweak
1252 && h->root.type != bfd_link_hash_common);
1253
1254 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1255 respectively, appear to be a function. */
1256
1257 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1258 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1259
1260 oldfunc = (h->type != STT_NOTYPE
1261 && bed->is_function_type (h->type));
1262
1263 if (!(newfunc && oldfunc)
1264 && ELF_ST_TYPE (sym->st_info) != h->type
1265 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1266 && h->type != STT_NOTYPE
1267 && (newdef || bfd_is_com_section (sec))
1268 && (olddef || h->root.type == bfd_link_hash_common))
1269 {
1270 /* If creating a default indirect symbol ("foo" or "foo@") from
1271 a dynamic versioned definition ("foo@@") skip doing so if
1272 there is an existing regular definition with a different
1273 type. We don't want, for example, a "time" variable in the
1274 executable overriding a "time" function in a shared library. */
1275 if (newdyn
1276 && !olddyn)
1277 {
1278 *skip = TRUE;
1279 return TRUE;
1280 }
1281
1282 /* When adding a symbol from a regular object file after we have
1283 created indirect symbols, undo the indirection and any
1284 dynamic state. */
1285 if (hi != h
1286 && !newdyn
1287 && olddyn)
1288 {
1289 h = hi;
1290 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1291 h->forced_local = 0;
1292 h->ref_dynamic = 0;
1293 h->def_dynamic = 0;
1294 h->dynamic_def = 0;
1295 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1296 {
1297 h->root.type = bfd_link_hash_undefined;
1298 h->root.u.undef.abfd = abfd;
1299 }
1300 else
1301 {
1302 h->root.type = bfd_link_hash_new;
1303 h->root.u.undef.abfd = NULL;
1304 }
1305 return TRUE;
1306 }
1307 }
1308
1309 /* Check TLS symbols. We don't check undefined symbols introduced
1310 by "ld -u" which have no type (and oldbfd NULL), and we don't
1311 check symbols from plugins because they also have no type. */
1312 if (oldbfd != NULL
1313 && (oldbfd->flags & BFD_PLUGIN) == 0
1314 && (abfd->flags & BFD_PLUGIN) == 0
1315 && ELF_ST_TYPE (sym->st_info) != h->type
1316 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1317 {
1318 bfd *ntbfd, *tbfd;
1319 bfd_boolean ntdef, tdef;
1320 asection *ntsec, *tsec;
1321
1322 if (h->type == STT_TLS)
1323 {
1324 ntbfd = abfd;
1325 ntsec = sec;
1326 ntdef = newdef;
1327 tbfd = oldbfd;
1328 tsec = oldsec;
1329 tdef = olddef;
1330 }
1331 else
1332 {
1333 ntbfd = oldbfd;
1334 ntsec = oldsec;
1335 ntdef = olddef;
1336 tbfd = abfd;
1337 tsec = sec;
1338 tdef = newdef;
1339 }
1340
1341 if (tdef && ntdef)
1342 _bfd_error_handler
1343 /* xgettext:c-format */
1344 (_("%s: TLS definition in %B section %A "
1345 "mismatches non-TLS definition in %B section %A"),
1346 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1347 else if (!tdef && !ntdef)
1348 _bfd_error_handler
1349 /* xgettext:c-format */
1350 (_("%s: TLS reference in %B "
1351 "mismatches non-TLS reference in %B"),
1352 h->root.root.string, tbfd, ntbfd);
1353 else if (tdef)
1354 _bfd_error_handler
1355 /* xgettext:c-format */
1356 (_("%s: TLS definition in %B section %A "
1357 "mismatches non-TLS reference in %B"),
1358 h->root.root.string, tbfd, tsec, ntbfd);
1359 else
1360 _bfd_error_handler
1361 /* xgettext:c-format */
1362 (_("%s: TLS reference in %B "
1363 "mismatches non-TLS definition in %B section %A"),
1364 h->root.root.string, tbfd, ntbfd, ntsec);
1365
1366 bfd_set_error (bfd_error_bad_value);
1367 return FALSE;
1368 }
1369
1370 /* If the old symbol has non-default visibility, we ignore the new
1371 definition from a dynamic object. */
1372 if (newdyn
1373 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1374 && !bfd_is_und_section (sec))
1375 {
1376 *skip = TRUE;
1377 /* Make sure this symbol is dynamic. */
1378 h->ref_dynamic = 1;
1379 hi->ref_dynamic = 1;
1380 /* A protected symbol has external availability. Make sure it is
1381 recorded as dynamic.
1382
1383 FIXME: Should we check type and size for protected symbol? */
1384 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1385 return bfd_elf_link_record_dynamic_symbol (info, h);
1386 else
1387 return TRUE;
1388 }
1389 else if (!newdyn
1390 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1391 && h->def_dynamic)
1392 {
1393 /* If the new symbol with non-default visibility comes from a
1394 relocatable file and the old definition comes from a dynamic
1395 object, we remove the old definition. */
1396 if (hi->root.type == bfd_link_hash_indirect)
1397 {
1398 /* Handle the case where the old dynamic definition is
1399 default versioned. We need to copy the symbol info from
1400 the symbol with default version to the normal one if it
1401 was referenced before. */
1402 if (h->ref_regular)
1403 {
1404 hi->root.type = h->root.type;
1405 h->root.type = bfd_link_hash_indirect;
1406 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1407
1408 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1409 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1410 {
1411 /* If the new symbol is hidden or internal, completely undo
1412 any dynamic link state. */
1413 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1414 h->forced_local = 0;
1415 h->ref_dynamic = 0;
1416 }
1417 else
1418 h->ref_dynamic = 1;
1419
1420 h->def_dynamic = 0;
1421 /* FIXME: Should we check type and size for protected symbol? */
1422 h->size = 0;
1423 h->type = 0;
1424
1425 h = hi;
1426 }
1427 else
1428 h = hi;
1429 }
1430
1431 /* If the old symbol was undefined before, then it will still be
1432 on the undefs list. If the new symbol is undefined or
1433 common, we can't make it bfd_link_hash_new here, because new
1434 undefined or common symbols will be added to the undefs list
1435 by _bfd_generic_link_add_one_symbol. Symbols may not be
1436 added twice to the undefs list. Also, if the new symbol is
1437 undefweak then we don't want to lose the strong undef. */
1438 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1439 {
1440 h->root.type = bfd_link_hash_undefined;
1441 h->root.u.undef.abfd = abfd;
1442 }
1443 else
1444 {
1445 h->root.type = bfd_link_hash_new;
1446 h->root.u.undef.abfd = NULL;
1447 }
1448
1449 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1450 {
1451 /* If the new symbol is hidden or internal, completely undo
1452 any dynamic link state. */
1453 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1454 h->forced_local = 0;
1455 h->ref_dynamic = 0;
1456 }
1457 else
1458 h->ref_dynamic = 1;
1459 h->def_dynamic = 0;
1460 /* FIXME: Should we check type and size for protected symbol? */
1461 h->size = 0;
1462 h->type = 0;
1463 return TRUE;
1464 }
1465
1466 /* If a new weak symbol definition comes from a regular file and the
1467 old symbol comes from a dynamic library, we treat the new one as
1468 strong. Similarly, an old weak symbol definition from a regular
1469 file is treated as strong when the new symbol comes from a dynamic
1470 library. Further, an old weak symbol from a dynamic library is
1471 treated as strong if the new symbol is from a dynamic library.
1472 This reflects the way glibc's ld.so works.
1473
1474 Do this before setting *type_change_ok or *size_change_ok so that
1475 we warn properly when dynamic library symbols are overridden. */
1476
1477 if (newdef && !newdyn && olddyn)
1478 newweak = FALSE;
1479 if (olddef && newdyn)
1480 oldweak = FALSE;
1481
1482 /* Allow changes between different types of function symbol. */
1483 if (newfunc && oldfunc)
1484 *type_change_ok = TRUE;
1485
1486 /* It's OK to change the type if either the existing symbol or the
1487 new symbol is weak. A type change is also OK if the old symbol
1488 is undefined and the new symbol is defined. */
1489
1490 if (oldweak
1491 || newweak
1492 || (newdef
1493 && h->root.type == bfd_link_hash_undefined))
1494 *type_change_ok = TRUE;
1495
1496 /* It's OK to change the size if either the existing symbol or the
1497 new symbol is weak, or if the old symbol is undefined. */
1498
1499 if (*type_change_ok
1500 || h->root.type == bfd_link_hash_undefined)
1501 *size_change_ok = TRUE;
1502
1503 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1504 symbol, respectively, appears to be a common symbol in a dynamic
1505 object. If a symbol appears in an uninitialized section, and is
1506 not weak, and is not a function, then it may be a common symbol
1507 which was resolved when the dynamic object was created. We want
1508 to treat such symbols specially, because they raise special
1509 considerations when setting the symbol size: if the symbol
1510 appears as a common symbol in a regular object, and the size in
1511 the regular object is larger, we must make sure that we use the
1512 larger size. This problematic case can always be avoided in C,
1513 but it must be handled correctly when using Fortran shared
1514 libraries.
1515
1516 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1517 likewise for OLDDYNCOMMON and OLDDEF.
1518
1519 Note that this test is just a heuristic, and that it is quite
1520 possible to have an uninitialized symbol in a shared object which
1521 is really a definition, rather than a common symbol. This could
1522 lead to some minor confusion when the symbol really is a common
1523 symbol in some regular object. However, I think it will be
1524 harmless. */
1525
1526 if (newdyn
1527 && newdef
1528 && !newweak
1529 && (sec->flags & SEC_ALLOC) != 0
1530 && (sec->flags & SEC_LOAD) == 0
1531 && sym->st_size > 0
1532 && !newfunc)
1533 newdyncommon = TRUE;
1534 else
1535 newdyncommon = FALSE;
1536
1537 if (olddyn
1538 && olddef
1539 && h->root.type == bfd_link_hash_defined
1540 && h->def_dynamic
1541 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1542 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1543 && h->size > 0
1544 && !oldfunc)
1545 olddyncommon = TRUE;
1546 else
1547 olddyncommon = FALSE;
1548
1549 /* We now know everything about the old and new symbols. We ask the
1550 backend to check if we can merge them. */
1551 if (bed->merge_symbol != NULL)
1552 {
1553 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1554 return FALSE;
1555 sec = *psec;
1556 }
1557
1558 /* There are multiple definitions of a normal symbol.
1559 Skip the default symbol as well. */
1560 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1561 && !default_sym && h->def_regular)
1562 {
1563 /* Handle a multiple definition. */
1564 (*info->callbacks->multiple_definition) (info, &h->root,
1565 abfd, sec, *pvalue);
1566 *skip = TRUE;
1567 return TRUE;
1568 }
1569
1570 /* If both the old and the new symbols look like common symbols in a
1571 dynamic object, set the size of the symbol to the larger of the
1572 two. */
1573
1574 if (olddyncommon
1575 && newdyncommon
1576 && sym->st_size != h->size)
1577 {
1578 /* Since we think we have two common symbols, issue a multiple
1579 common warning if desired. Note that we only warn if the
1580 size is different. If the size is the same, we simply let
1581 the old symbol override the new one as normally happens with
1582 symbols defined in dynamic objects. */
1583
1584 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1585 bfd_link_hash_common, sym->st_size);
1586 if (sym->st_size > h->size)
1587 h->size = sym->st_size;
1588
1589 *size_change_ok = TRUE;
1590 }
1591
1592 /* If we are looking at a dynamic object, and we have found a
1593 definition, we need to see if the symbol was already defined by
1594 some other object. If so, we want to use the existing
1595 definition, and we do not want to report a multiple symbol
1596 definition error; we do this by clobbering *PSEC to be
1597 bfd_und_section_ptr.
1598
1599 We treat a common symbol as a definition if the symbol in the
1600 shared library is a function, since common symbols always
1601 represent variables; this can cause confusion in principle, but
1602 any such confusion would seem to indicate an erroneous program or
1603 shared library. We also permit a common symbol in a regular
1604 object to override a weak symbol in a shared object. */
1605
1606 if (newdyn
1607 && newdef
1608 && (olddef
1609 || (h->root.type == bfd_link_hash_common
1610 && (newweak || newfunc))))
1611 {
1612 *override = TRUE;
1613 newdef = FALSE;
1614 newdyncommon = FALSE;
1615
1616 *psec = sec = bfd_und_section_ptr;
1617 *size_change_ok = TRUE;
1618
1619 /* If we get here when the old symbol is a common symbol, then
1620 we are explicitly letting it override a weak symbol or
1621 function in a dynamic object, and we don't want to warn about
1622 a type change. If the old symbol is a defined symbol, a type
1623 change warning may still be appropriate. */
1624
1625 if (h->root.type == bfd_link_hash_common)
1626 *type_change_ok = TRUE;
1627 }
1628
1629 /* Handle the special case of an old common symbol merging with a
1630 new symbol which looks like a common symbol in a shared object.
1631 We change *PSEC and *PVALUE to make the new symbol look like a
1632 common symbol, and let _bfd_generic_link_add_one_symbol do the
1633 right thing. */
1634
1635 if (newdyncommon
1636 && h->root.type == bfd_link_hash_common)
1637 {
1638 *override = TRUE;
1639 newdef = FALSE;
1640 newdyncommon = FALSE;
1641 *pvalue = sym->st_size;
1642 *psec = sec = bed->common_section (oldsec);
1643 *size_change_ok = TRUE;
1644 }
1645
1646 /* Skip weak definitions of symbols that are already defined. */
1647 if (newdef && olddef && newweak)
1648 {
1649 /* Don't skip new non-IR weak syms. */
1650 if (!(oldbfd != NULL
1651 && (oldbfd->flags & BFD_PLUGIN) != 0
1652 && (abfd->flags & BFD_PLUGIN) == 0))
1653 {
1654 newdef = FALSE;
1655 *skip = TRUE;
1656 }
1657
1658 /* Merge st_other. If the symbol already has a dynamic index,
1659 but visibility says it should not be visible, turn it into a
1660 local symbol. */
1661 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1662 if (h->dynindx != -1)
1663 switch (ELF_ST_VISIBILITY (h->other))
1664 {
1665 case STV_INTERNAL:
1666 case STV_HIDDEN:
1667 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1668 break;
1669 }
1670 }
1671
1672 /* If the old symbol is from a dynamic object, and the new symbol is
1673 a definition which is not from a dynamic object, then the new
1674 symbol overrides the old symbol. Symbols from regular files
1675 always take precedence over symbols from dynamic objects, even if
1676 they are defined after the dynamic object in the link.
1677
1678 As above, we again permit a common symbol in a regular object to
1679 override a definition in a shared object if the shared object
1680 symbol is a function or is weak. */
1681
1682 flip = NULL;
1683 if (!newdyn
1684 && (newdef
1685 || (bfd_is_com_section (sec)
1686 && (oldweak || oldfunc)))
1687 && olddyn
1688 && olddef
1689 && h->def_dynamic)
1690 {
1691 /* Change the hash table entry to undefined, and let
1692 _bfd_generic_link_add_one_symbol do the right thing with the
1693 new definition. */
1694
1695 h->root.type = bfd_link_hash_undefined;
1696 h->root.u.undef.abfd = h->root.u.def.section->owner;
1697 *size_change_ok = TRUE;
1698
1699 olddef = FALSE;
1700 olddyncommon = FALSE;
1701
1702 /* We again permit a type change when a common symbol may be
1703 overriding a function. */
1704
1705 if (bfd_is_com_section (sec))
1706 {
1707 if (oldfunc)
1708 {
1709 /* If a common symbol overrides a function, make sure
1710 that it isn't defined dynamically nor has type
1711 function. */
1712 h->def_dynamic = 0;
1713 h->type = STT_NOTYPE;
1714 }
1715 *type_change_ok = TRUE;
1716 }
1717
1718 if (hi->root.type == bfd_link_hash_indirect)
1719 flip = hi;
1720 else
1721 /* This union may have been set to be non-NULL when this symbol
1722 was seen in a dynamic object. We must force the union to be
1723 NULL, so that it is correct for a regular symbol. */
1724 h->verinfo.vertree = NULL;
1725 }
1726
1727 /* Handle the special case of a new common symbol merging with an
1728 old symbol that looks like it might be a common symbol defined in
1729 a shared object. Note that we have already handled the case in
1730 which a new common symbol should simply override the definition
1731 in the shared library. */
1732
1733 if (! newdyn
1734 && bfd_is_com_section (sec)
1735 && olddyncommon)
1736 {
1737 /* It would be best if we could set the hash table entry to a
1738 common symbol, but we don't know what to use for the section
1739 or the alignment. */
1740 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1741 bfd_link_hash_common, sym->st_size);
1742
1743 /* If the presumed common symbol in the dynamic object is
1744 larger, pretend that the new symbol has its size. */
1745
1746 if (h->size > *pvalue)
1747 *pvalue = h->size;
1748
1749 /* We need to remember the alignment required by the symbol
1750 in the dynamic object. */
1751 BFD_ASSERT (pold_alignment);
1752 *pold_alignment = h->root.u.def.section->alignment_power;
1753
1754 olddef = FALSE;
1755 olddyncommon = FALSE;
1756
1757 h->root.type = bfd_link_hash_undefined;
1758 h->root.u.undef.abfd = h->root.u.def.section->owner;
1759
1760 *size_change_ok = TRUE;
1761 *type_change_ok = TRUE;
1762
1763 if (hi->root.type == bfd_link_hash_indirect)
1764 flip = hi;
1765 else
1766 h->verinfo.vertree = NULL;
1767 }
1768
1769 if (flip != NULL)
1770 {
1771 /* Handle the case where we had a versioned symbol in a dynamic
1772 library and now find a definition in a normal object. In this
1773 case, we make the versioned symbol point to the normal one. */
1774 flip->root.type = h->root.type;
1775 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1776 h->root.type = bfd_link_hash_indirect;
1777 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1778 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1779 if (h->def_dynamic)
1780 {
1781 h->def_dynamic = 0;
1782 flip->ref_dynamic = 1;
1783 }
1784 }
1785
1786 return TRUE;
1787 }
1788
1789 /* This function is called to create an indirect symbol from the
1790 default for the symbol with the default version if needed. The
1791 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1792 set DYNSYM if the new indirect symbol is dynamic. */
1793
1794 static bfd_boolean
1795 _bfd_elf_add_default_symbol (bfd *abfd,
1796 struct bfd_link_info *info,
1797 struct elf_link_hash_entry *h,
1798 const char *name,
1799 Elf_Internal_Sym *sym,
1800 asection *sec,
1801 bfd_vma value,
1802 bfd **poldbfd,
1803 bfd_boolean *dynsym)
1804 {
1805 bfd_boolean type_change_ok;
1806 bfd_boolean size_change_ok;
1807 bfd_boolean skip;
1808 char *shortname;
1809 struct elf_link_hash_entry *hi;
1810 struct bfd_link_hash_entry *bh;
1811 const struct elf_backend_data *bed;
1812 bfd_boolean collect;
1813 bfd_boolean dynamic;
1814 bfd_boolean override;
1815 char *p;
1816 size_t len, shortlen;
1817 asection *tmp_sec;
1818 bfd_boolean matched;
1819
1820 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1821 return TRUE;
1822
1823 /* If this symbol has a version, and it is the default version, we
1824 create an indirect symbol from the default name to the fully
1825 decorated name. This will cause external references which do not
1826 specify a version to be bound to this version of the symbol. */
1827 p = strchr (name, ELF_VER_CHR);
1828 if (h->versioned == unknown)
1829 {
1830 if (p == NULL)
1831 {
1832 h->versioned = unversioned;
1833 return TRUE;
1834 }
1835 else
1836 {
1837 if (p[1] != ELF_VER_CHR)
1838 {
1839 h->versioned = versioned_hidden;
1840 return TRUE;
1841 }
1842 else
1843 h->versioned = versioned;
1844 }
1845 }
1846 else
1847 {
1848 /* PR ld/19073: We may see an unversioned definition after the
1849 default version. */
1850 if (p == NULL)
1851 return TRUE;
1852 }
1853
1854 bed = get_elf_backend_data (abfd);
1855 collect = bed->collect;
1856 dynamic = (abfd->flags & DYNAMIC) != 0;
1857
1858 shortlen = p - name;
1859 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1860 if (shortname == NULL)
1861 return FALSE;
1862 memcpy (shortname, name, shortlen);
1863 shortname[shortlen] = '\0';
1864
1865 /* We are going to create a new symbol. Merge it with any existing
1866 symbol with this name. For the purposes of the merge, act as
1867 though we were defining the symbol we just defined, although we
1868 actually going to define an indirect symbol. */
1869 type_change_ok = FALSE;
1870 size_change_ok = FALSE;
1871 matched = TRUE;
1872 tmp_sec = sec;
1873 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1874 &hi, poldbfd, NULL, NULL, &skip, &override,
1875 &type_change_ok, &size_change_ok, &matched))
1876 return FALSE;
1877
1878 if (skip)
1879 goto nondefault;
1880
1881 if (hi->def_regular)
1882 {
1883 /* If the undecorated symbol will have a version added by a
1884 script different to H, then don't indirect to/from the
1885 undecorated symbol. This isn't ideal because we may not yet
1886 have seen symbol versions, if given by a script on the
1887 command line rather than via --version-script. */
1888 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1889 {
1890 bfd_boolean hide;
1891
1892 hi->verinfo.vertree
1893 = bfd_find_version_for_sym (info->version_info,
1894 hi->root.root.string, &hide);
1895 if (hi->verinfo.vertree != NULL && hide)
1896 {
1897 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1898 goto nondefault;
1899 }
1900 }
1901 if (hi->verinfo.vertree != NULL
1902 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1903 goto nondefault;
1904 }
1905
1906 if (! override)
1907 {
1908 /* Add the default symbol if not performing a relocatable link. */
1909 if (! bfd_link_relocatable (info))
1910 {
1911 bh = &hi->root;
1912 if (! (_bfd_generic_link_add_one_symbol
1913 (info, abfd, shortname, BSF_INDIRECT,
1914 bfd_ind_section_ptr,
1915 0, name, FALSE, collect, &bh)))
1916 return FALSE;
1917 hi = (struct elf_link_hash_entry *) bh;
1918 }
1919 }
1920 else
1921 {
1922 /* In this case the symbol named SHORTNAME is overriding the
1923 indirect symbol we want to add. We were planning on making
1924 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1925 is the name without a version. NAME is the fully versioned
1926 name, and it is the default version.
1927
1928 Overriding means that we already saw a definition for the
1929 symbol SHORTNAME in a regular object, and it is overriding
1930 the symbol defined in the dynamic object.
1931
1932 When this happens, we actually want to change NAME, the
1933 symbol we just added, to refer to SHORTNAME. This will cause
1934 references to NAME in the shared object to become references
1935 to SHORTNAME in the regular object. This is what we expect
1936 when we override a function in a shared object: that the
1937 references in the shared object will be mapped to the
1938 definition in the regular object. */
1939
1940 while (hi->root.type == bfd_link_hash_indirect
1941 || hi->root.type == bfd_link_hash_warning)
1942 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1943
1944 h->root.type = bfd_link_hash_indirect;
1945 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1946 if (h->def_dynamic)
1947 {
1948 h->def_dynamic = 0;
1949 hi->ref_dynamic = 1;
1950 if (hi->ref_regular
1951 || hi->def_regular)
1952 {
1953 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1954 return FALSE;
1955 }
1956 }
1957
1958 /* Now set HI to H, so that the following code will set the
1959 other fields correctly. */
1960 hi = h;
1961 }
1962
1963 /* Check if HI is a warning symbol. */
1964 if (hi->root.type == bfd_link_hash_warning)
1965 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1966
1967 /* If there is a duplicate definition somewhere, then HI may not
1968 point to an indirect symbol. We will have reported an error to
1969 the user in that case. */
1970
1971 if (hi->root.type == bfd_link_hash_indirect)
1972 {
1973 struct elf_link_hash_entry *ht;
1974
1975 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1976 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1977
1978 /* A reference to the SHORTNAME symbol from a dynamic library
1979 will be satisfied by the versioned symbol at runtime. In
1980 effect, we have a reference to the versioned symbol. */
1981 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1982 hi->dynamic_def |= ht->dynamic_def;
1983
1984 /* See if the new flags lead us to realize that the symbol must
1985 be dynamic. */
1986 if (! *dynsym)
1987 {
1988 if (! dynamic)
1989 {
1990 if (! bfd_link_executable (info)
1991 || hi->def_dynamic
1992 || hi->ref_dynamic)
1993 *dynsym = TRUE;
1994 }
1995 else
1996 {
1997 if (hi->ref_regular)
1998 *dynsym = TRUE;
1999 }
2000 }
2001 }
2002
2003 /* We also need to define an indirection from the nondefault version
2004 of the symbol. */
2005
2006 nondefault:
2007 len = strlen (name);
2008 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2009 if (shortname == NULL)
2010 return FALSE;
2011 memcpy (shortname, name, shortlen);
2012 memcpy (shortname + shortlen, p + 1, len - shortlen);
2013
2014 /* Once again, merge with any existing symbol. */
2015 type_change_ok = FALSE;
2016 size_change_ok = FALSE;
2017 tmp_sec = sec;
2018 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2019 &hi, poldbfd, NULL, NULL, &skip, &override,
2020 &type_change_ok, &size_change_ok, &matched))
2021 return FALSE;
2022
2023 if (skip)
2024 return TRUE;
2025
2026 if (override)
2027 {
2028 /* Here SHORTNAME is a versioned name, so we don't expect to see
2029 the type of override we do in the case above unless it is
2030 overridden by a versioned definition. */
2031 if (hi->root.type != bfd_link_hash_defined
2032 && hi->root.type != bfd_link_hash_defweak)
2033 _bfd_error_handler
2034 /* xgettext:c-format */
2035 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
2036 abfd, shortname);
2037 }
2038 else
2039 {
2040 bh = &hi->root;
2041 if (! (_bfd_generic_link_add_one_symbol
2042 (info, abfd, shortname, BSF_INDIRECT,
2043 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
2044 return FALSE;
2045 hi = (struct elf_link_hash_entry *) bh;
2046
2047 /* If there is a duplicate definition somewhere, then HI may not
2048 point to an indirect symbol. We will have reported an error
2049 to the user in that case. */
2050
2051 if (hi->root.type == bfd_link_hash_indirect)
2052 {
2053 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2054 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2055 hi->dynamic_def |= h->dynamic_def;
2056
2057 /* See if the new flags lead us to realize that the symbol
2058 must be dynamic. */
2059 if (! *dynsym)
2060 {
2061 if (! dynamic)
2062 {
2063 if (! bfd_link_executable (info)
2064 || hi->ref_dynamic)
2065 *dynsym = TRUE;
2066 }
2067 else
2068 {
2069 if (hi->ref_regular)
2070 *dynsym = TRUE;
2071 }
2072 }
2073 }
2074 }
2075
2076 return TRUE;
2077 }
2078 \f
2079 /* This routine is used to export all defined symbols into the dynamic
2080 symbol table. It is called via elf_link_hash_traverse. */
2081
2082 static bfd_boolean
2083 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2084 {
2085 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2086
2087 /* Ignore indirect symbols. These are added by the versioning code. */
2088 if (h->root.type == bfd_link_hash_indirect)
2089 return TRUE;
2090
2091 /* Ignore this if we won't export it. */
2092 if (!eif->info->export_dynamic && !h->dynamic)
2093 return TRUE;
2094
2095 if (h->dynindx == -1
2096 && (h->def_regular || h->ref_regular)
2097 && ! bfd_hide_sym_by_version (eif->info->version_info,
2098 h->root.root.string))
2099 {
2100 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2101 {
2102 eif->failed = TRUE;
2103 return FALSE;
2104 }
2105 }
2106
2107 return TRUE;
2108 }
2109 \f
2110 /* Look through the symbols which are defined in other shared
2111 libraries and referenced here. Update the list of version
2112 dependencies. This will be put into the .gnu.version_r section.
2113 This function is called via elf_link_hash_traverse. */
2114
2115 static bfd_boolean
2116 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2117 void *data)
2118 {
2119 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2120 Elf_Internal_Verneed *t;
2121 Elf_Internal_Vernaux *a;
2122 bfd_size_type amt;
2123
2124 /* We only care about symbols defined in shared objects with version
2125 information. */
2126 if (!h->def_dynamic
2127 || h->def_regular
2128 || h->dynindx == -1
2129 || h->verinfo.verdef == NULL
2130 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2131 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2132 return TRUE;
2133
2134 /* See if we already know about this version. */
2135 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2136 t != NULL;
2137 t = t->vn_nextref)
2138 {
2139 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2140 continue;
2141
2142 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2143 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2144 return TRUE;
2145
2146 break;
2147 }
2148
2149 /* This is a new version. Add it to tree we are building. */
2150
2151 if (t == NULL)
2152 {
2153 amt = sizeof *t;
2154 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2155 if (t == NULL)
2156 {
2157 rinfo->failed = TRUE;
2158 return FALSE;
2159 }
2160
2161 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2162 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2163 elf_tdata (rinfo->info->output_bfd)->verref = t;
2164 }
2165
2166 amt = sizeof *a;
2167 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2168 if (a == NULL)
2169 {
2170 rinfo->failed = TRUE;
2171 return FALSE;
2172 }
2173
2174 /* Note that we are copying a string pointer here, and testing it
2175 above. If bfd_elf_string_from_elf_section is ever changed to
2176 discard the string data when low in memory, this will have to be
2177 fixed. */
2178 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2179
2180 a->vna_flags = h->verinfo.verdef->vd_flags;
2181 a->vna_nextptr = t->vn_auxptr;
2182
2183 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2184 ++rinfo->vers;
2185
2186 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2187
2188 t->vn_auxptr = a;
2189
2190 return TRUE;
2191 }
2192
2193 /* Figure out appropriate versions for all the symbols. We may not
2194 have the version number script until we have read all of the input
2195 files, so until that point we don't know which symbols should be
2196 local. This function is called via elf_link_hash_traverse. */
2197
2198 static bfd_boolean
2199 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2200 {
2201 struct elf_info_failed *sinfo;
2202 struct bfd_link_info *info;
2203 const struct elf_backend_data *bed;
2204 struct elf_info_failed eif;
2205 char *p;
2206
2207 sinfo = (struct elf_info_failed *) data;
2208 info = sinfo->info;
2209
2210 /* Fix the symbol flags. */
2211 eif.failed = FALSE;
2212 eif.info = info;
2213 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2214 {
2215 if (eif.failed)
2216 sinfo->failed = TRUE;
2217 return FALSE;
2218 }
2219
2220 /* We only need version numbers for symbols defined in regular
2221 objects. */
2222 if (!h->def_regular)
2223 return TRUE;
2224
2225 bed = get_elf_backend_data (info->output_bfd);
2226 p = strchr (h->root.root.string, ELF_VER_CHR);
2227 if (p != NULL && h->verinfo.vertree == NULL)
2228 {
2229 struct bfd_elf_version_tree *t;
2230
2231 ++p;
2232 if (*p == ELF_VER_CHR)
2233 ++p;
2234
2235 /* If there is no version string, we can just return out. */
2236 if (*p == '\0')
2237 return TRUE;
2238
2239 /* Look for the version. If we find it, it is no longer weak. */
2240 for (t = sinfo->info->version_info; t != NULL; t = t->next)
2241 {
2242 if (strcmp (t->name, p) == 0)
2243 {
2244 size_t len;
2245 char *alc;
2246 struct bfd_elf_version_expr *d;
2247
2248 len = p - h->root.root.string;
2249 alc = (char *) bfd_malloc (len);
2250 if (alc == NULL)
2251 {
2252 sinfo->failed = TRUE;
2253 return FALSE;
2254 }
2255 memcpy (alc, h->root.root.string, len - 1);
2256 alc[len - 1] = '\0';
2257 if (alc[len - 2] == ELF_VER_CHR)
2258 alc[len - 2] = '\0';
2259
2260 h->verinfo.vertree = t;
2261 t->used = TRUE;
2262 d = NULL;
2263
2264 if (t->globals.list != NULL)
2265 d = (*t->match) (&t->globals, NULL, alc);
2266
2267 /* See if there is anything to force this symbol to
2268 local scope. */
2269 if (d == NULL && t->locals.list != NULL)
2270 {
2271 d = (*t->match) (&t->locals, NULL, alc);
2272 if (d != NULL
2273 && h->dynindx != -1
2274 && ! info->export_dynamic)
2275 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2276 }
2277
2278 free (alc);
2279 break;
2280 }
2281 }
2282
2283 /* If we are building an application, we need to create a
2284 version node for this version. */
2285 if (t == NULL && bfd_link_executable (info))
2286 {
2287 struct bfd_elf_version_tree **pp;
2288 int version_index;
2289
2290 /* If we aren't going to export this symbol, we don't need
2291 to worry about it. */
2292 if (h->dynindx == -1)
2293 return TRUE;
2294
2295 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2296 sizeof *t);
2297 if (t == NULL)
2298 {
2299 sinfo->failed = TRUE;
2300 return FALSE;
2301 }
2302
2303 t->name = p;
2304 t->name_indx = (unsigned int) -1;
2305 t->used = TRUE;
2306
2307 version_index = 1;
2308 /* Don't count anonymous version tag. */
2309 if (sinfo->info->version_info != NULL
2310 && sinfo->info->version_info->vernum == 0)
2311 version_index = 0;
2312 for (pp = &sinfo->info->version_info;
2313 *pp != NULL;
2314 pp = &(*pp)->next)
2315 ++version_index;
2316 t->vernum = version_index;
2317
2318 *pp = t;
2319
2320 h->verinfo.vertree = t;
2321 }
2322 else if (t == NULL)
2323 {
2324 /* We could not find the version for a symbol when
2325 generating a shared archive. Return an error. */
2326 _bfd_error_handler
2327 /* xgettext:c-format */
2328 (_("%B: version node not found for symbol %s"),
2329 info->output_bfd, h->root.root.string);
2330 bfd_set_error (bfd_error_bad_value);
2331 sinfo->failed = TRUE;
2332 return FALSE;
2333 }
2334 }
2335
2336 /* If we don't have a version for this symbol, see if we can find
2337 something. */
2338 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
2339 {
2340 bfd_boolean hide;
2341
2342 h->verinfo.vertree
2343 = bfd_find_version_for_sym (sinfo->info->version_info,
2344 h->root.root.string, &hide);
2345 if (h->verinfo.vertree != NULL && hide)
2346 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2347 }
2348
2349 return TRUE;
2350 }
2351 \f
2352 /* Read and swap the relocs from the section indicated by SHDR. This
2353 may be either a REL or a RELA section. The relocations are
2354 translated into RELA relocations and stored in INTERNAL_RELOCS,
2355 which should have already been allocated to contain enough space.
2356 The EXTERNAL_RELOCS are a buffer where the external form of the
2357 relocations should be stored.
2358
2359 Returns FALSE if something goes wrong. */
2360
2361 static bfd_boolean
2362 elf_link_read_relocs_from_section (bfd *abfd,
2363 asection *sec,
2364 Elf_Internal_Shdr *shdr,
2365 void *external_relocs,
2366 Elf_Internal_Rela *internal_relocs)
2367 {
2368 const struct elf_backend_data *bed;
2369 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2370 const bfd_byte *erela;
2371 const bfd_byte *erelaend;
2372 Elf_Internal_Rela *irela;
2373 Elf_Internal_Shdr *symtab_hdr;
2374 size_t nsyms;
2375
2376 /* Position ourselves at the start of the section. */
2377 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2378 return FALSE;
2379
2380 /* Read the relocations. */
2381 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2382 return FALSE;
2383
2384 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2385 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2386
2387 bed = get_elf_backend_data (abfd);
2388
2389 /* Convert the external relocations to the internal format. */
2390 if (shdr->sh_entsize == bed->s->sizeof_rel)
2391 swap_in = bed->s->swap_reloc_in;
2392 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2393 swap_in = bed->s->swap_reloca_in;
2394 else
2395 {
2396 bfd_set_error (bfd_error_wrong_format);
2397 return FALSE;
2398 }
2399
2400 erela = (const bfd_byte *) external_relocs;
2401 erelaend = erela + shdr->sh_size;
2402 irela = internal_relocs;
2403 while (erela < erelaend)
2404 {
2405 bfd_vma r_symndx;
2406
2407 (*swap_in) (abfd, erela, irela);
2408 r_symndx = ELF32_R_SYM (irela->r_info);
2409 if (bed->s->arch_size == 64)
2410 r_symndx >>= 24;
2411 if (nsyms > 0)
2412 {
2413 if ((size_t) r_symndx >= nsyms)
2414 {
2415 _bfd_error_handler
2416 /* xgettext:c-format */
2417 (_("%B: bad reloc symbol index (%#Lx >= %#lx)"
2418 " for offset %#Lx in section `%A'"),
2419 abfd, r_symndx, (unsigned long) nsyms,
2420 irela->r_offset, sec);
2421 bfd_set_error (bfd_error_bad_value);
2422 return FALSE;
2423 }
2424 }
2425 else if (r_symndx != STN_UNDEF)
2426 {
2427 _bfd_error_handler
2428 /* xgettext:c-format */
2429 (_("%B: non-zero symbol index (%#Lx)"
2430 " for offset %#Lx in section `%A'"
2431 " when the object file has no symbol table"),
2432 abfd, r_symndx,
2433 irela->r_offset, sec);
2434 bfd_set_error (bfd_error_bad_value);
2435 return FALSE;
2436 }
2437 irela += bed->s->int_rels_per_ext_rel;
2438 erela += shdr->sh_entsize;
2439 }
2440
2441 return TRUE;
2442 }
2443
2444 /* Read and swap the relocs for a section O. They may have been
2445 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2446 not NULL, they are used as buffers to read into. They are known to
2447 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2448 the return value is allocated using either malloc or bfd_alloc,
2449 according to the KEEP_MEMORY argument. If O has two relocation
2450 sections (both REL and RELA relocations), then the REL_HDR
2451 relocations will appear first in INTERNAL_RELOCS, followed by the
2452 RELA_HDR relocations. */
2453
2454 Elf_Internal_Rela *
2455 _bfd_elf_link_read_relocs (bfd *abfd,
2456 asection *o,
2457 void *external_relocs,
2458 Elf_Internal_Rela *internal_relocs,
2459 bfd_boolean keep_memory)
2460 {
2461 void *alloc1 = NULL;
2462 Elf_Internal_Rela *alloc2 = NULL;
2463 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2464 struct bfd_elf_section_data *esdo = elf_section_data (o);
2465 Elf_Internal_Rela *internal_rela_relocs;
2466
2467 if (esdo->relocs != NULL)
2468 return esdo->relocs;
2469
2470 if (o->reloc_count == 0)
2471 return NULL;
2472
2473 if (internal_relocs == NULL)
2474 {
2475 bfd_size_type size;
2476
2477 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2478 if (keep_memory)
2479 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2480 else
2481 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2482 if (internal_relocs == NULL)
2483 goto error_return;
2484 }
2485
2486 if (external_relocs == NULL)
2487 {
2488 bfd_size_type size = 0;
2489
2490 if (esdo->rel.hdr)
2491 size += esdo->rel.hdr->sh_size;
2492 if (esdo->rela.hdr)
2493 size += esdo->rela.hdr->sh_size;
2494
2495 alloc1 = bfd_malloc (size);
2496 if (alloc1 == NULL)
2497 goto error_return;
2498 external_relocs = alloc1;
2499 }
2500
2501 internal_rela_relocs = internal_relocs;
2502 if (esdo->rel.hdr)
2503 {
2504 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2505 external_relocs,
2506 internal_relocs))
2507 goto error_return;
2508 external_relocs = (((bfd_byte *) external_relocs)
2509 + esdo->rel.hdr->sh_size);
2510 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2511 * bed->s->int_rels_per_ext_rel);
2512 }
2513
2514 if (esdo->rela.hdr
2515 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2516 external_relocs,
2517 internal_rela_relocs)))
2518 goto error_return;
2519
2520 /* Cache the results for next time, if we can. */
2521 if (keep_memory)
2522 esdo->relocs = internal_relocs;
2523
2524 if (alloc1 != NULL)
2525 free (alloc1);
2526
2527 /* Don't free alloc2, since if it was allocated we are passing it
2528 back (under the name of internal_relocs). */
2529
2530 return internal_relocs;
2531
2532 error_return:
2533 if (alloc1 != NULL)
2534 free (alloc1);
2535 if (alloc2 != NULL)
2536 {
2537 if (keep_memory)
2538 bfd_release (abfd, alloc2);
2539 else
2540 free (alloc2);
2541 }
2542 return NULL;
2543 }
2544
2545 /* Compute the size of, and allocate space for, REL_HDR which is the
2546 section header for a section containing relocations for O. */
2547
2548 static bfd_boolean
2549 _bfd_elf_link_size_reloc_section (bfd *abfd,
2550 struct bfd_elf_section_reloc_data *reldata)
2551 {
2552 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2553
2554 /* That allows us to calculate the size of the section. */
2555 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2556
2557 /* The contents field must last into write_object_contents, so we
2558 allocate it with bfd_alloc rather than malloc. Also since we
2559 cannot be sure that the contents will actually be filled in,
2560 we zero the allocated space. */
2561 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2562 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2563 return FALSE;
2564
2565 if (reldata->hashes == NULL && reldata->count)
2566 {
2567 struct elf_link_hash_entry **p;
2568
2569 p = ((struct elf_link_hash_entry **)
2570 bfd_zmalloc (reldata->count * sizeof (*p)));
2571 if (p == NULL)
2572 return FALSE;
2573
2574 reldata->hashes = p;
2575 }
2576
2577 return TRUE;
2578 }
2579
2580 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2581 originated from the section given by INPUT_REL_HDR) to the
2582 OUTPUT_BFD. */
2583
2584 bfd_boolean
2585 _bfd_elf_link_output_relocs (bfd *output_bfd,
2586 asection *input_section,
2587 Elf_Internal_Shdr *input_rel_hdr,
2588 Elf_Internal_Rela *internal_relocs,
2589 struct elf_link_hash_entry **rel_hash
2590 ATTRIBUTE_UNUSED)
2591 {
2592 Elf_Internal_Rela *irela;
2593 Elf_Internal_Rela *irelaend;
2594 bfd_byte *erel;
2595 struct bfd_elf_section_reloc_data *output_reldata;
2596 asection *output_section;
2597 const struct elf_backend_data *bed;
2598 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2599 struct bfd_elf_section_data *esdo;
2600
2601 output_section = input_section->output_section;
2602
2603 bed = get_elf_backend_data (output_bfd);
2604 esdo = elf_section_data (output_section);
2605 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2606 {
2607 output_reldata = &esdo->rel;
2608 swap_out = bed->s->swap_reloc_out;
2609 }
2610 else if (esdo->rela.hdr
2611 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2612 {
2613 output_reldata = &esdo->rela;
2614 swap_out = bed->s->swap_reloca_out;
2615 }
2616 else
2617 {
2618 _bfd_error_handler
2619 /* xgettext:c-format */
2620 (_("%B: relocation size mismatch in %B section %A"),
2621 output_bfd, input_section->owner, input_section);
2622 bfd_set_error (bfd_error_wrong_format);
2623 return FALSE;
2624 }
2625
2626 erel = output_reldata->hdr->contents;
2627 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2628 irela = internal_relocs;
2629 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2630 * bed->s->int_rels_per_ext_rel);
2631 while (irela < irelaend)
2632 {
2633 (*swap_out) (output_bfd, irela, erel);
2634 irela += bed->s->int_rels_per_ext_rel;
2635 erel += input_rel_hdr->sh_entsize;
2636 }
2637
2638 /* Bump the counter, so that we know where to add the next set of
2639 relocations. */
2640 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2641
2642 return TRUE;
2643 }
2644 \f
2645 /* Make weak undefined symbols in PIE dynamic. */
2646
2647 bfd_boolean
2648 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2649 struct elf_link_hash_entry *h)
2650 {
2651 if (bfd_link_pie (info)
2652 && h->dynindx == -1
2653 && h->root.type == bfd_link_hash_undefweak)
2654 return bfd_elf_link_record_dynamic_symbol (info, h);
2655
2656 return TRUE;
2657 }
2658
2659 /* Fix up the flags for a symbol. This handles various cases which
2660 can only be fixed after all the input files are seen. This is
2661 currently called by both adjust_dynamic_symbol and
2662 assign_sym_version, which is unnecessary but perhaps more robust in
2663 the face of future changes. */
2664
2665 static bfd_boolean
2666 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2667 struct elf_info_failed *eif)
2668 {
2669 const struct elf_backend_data *bed;
2670
2671 /* If this symbol was mentioned in a non-ELF file, try to set
2672 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2673 permit a non-ELF file to correctly refer to a symbol defined in
2674 an ELF dynamic object. */
2675 if (h->non_elf)
2676 {
2677 while (h->root.type == bfd_link_hash_indirect)
2678 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2679
2680 if (h->root.type != bfd_link_hash_defined
2681 && h->root.type != bfd_link_hash_defweak)
2682 {
2683 h->ref_regular = 1;
2684 h->ref_regular_nonweak = 1;
2685 }
2686 else
2687 {
2688 if (h->root.u.def.section->owner != NULL
2689 && (bfd_get_flavour (h->root.u.def.section->owner)
2690 == bfd_target_elf_flavour))
2691 {
2692 h->ref_regular = 1;
2693 h->ref_regular_nonweak = 1;
2694 }
2695 else
2696 h->def_regular = 1;
2697 }
2698
2699 if (h->dynindx == -1
2700 && (h->def_dynamic
2701 || h->ref_dynamic))
2702 {
2703 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2704 {
2705 eif->failed = TRUE;
2706 return FALSE;
2707 }
2708 }
2709 }
2710 else
2711 {
2712 /* Unfortunately, NON_ELF is only correct if the symbol
2713 was first seen in a non-ELF file. Fortunately, if the symbol
2714 was first seen in an ELF file, we're probably OK unless the
2715 symbol was defined in a non-ELF file. Catch that case here.
2716 FIXME: We're still in trouble if the symbol was first seen in
2717 a dynamic object, and then later in a non-ELF regular object. */
2718 if ((h->root.type == bfd_link_hash_defined
2719 || h->root.type == bfd_link_hash_defweak)
2720 && !h->def_regular
2721 && (h->root.u.def.section->owner != NULL
2722 ? (bfd_get_flavour (h->root.u.def.section->owner)
2723 != bfd_target_elf_flavour)
2724 : (bfd_is_abs_section (h->root.u.def.section)
2725 && !h->def_dynamic)))
2726 h->def_regular = 1;
2727 }
2728
2729 /* Backend specific symbol fixup. */
2730 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2731 if (bed->elf_backend_fixup_symbol
2732 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2733 return FALSE;
2734
2735 /* If this is a final link, and the symbol was defined as a common
2736 symbol in a regular object file, and there was no definition in
2737 any dynamic object, then the linker will have allocated space for
2738 the symbol in a common section but the DEF_REGULAR
2739 flag will not have been set. */
2740 if (h->root.type == bfd_link_hash_defined
2741 && !h->def_regular
2742 && h->ref_regular
2743 && !h->def_dynamic
2744 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2745 h->def_regular = 1;
2746
2747 /* If a weak undefined symbol has non-default visibility, we also
2748 hide it from the dynamic linker. */
2749 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2750 && h->root.type == bfd_link_hash_undefweak)
2751 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2752
2753 /* A hidden versioned symbol in executable should be forced local if
2754 it is is locally defined, not referenced by shared library and not
2755 exported. */
2756 else if (bfd_link_executable (eif->info)
2757 && h->versioned == versioned_hidden
2758 && !eif->info->export_dynamic
2759 && !h->dynamic
2760 && !h->ref_dynamic
2761 && h->def_regular)
2762 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2763
2764 /* If -Bsymbolic was used (which means to bind references to global
2765 symbols to the definition within the shared object), and this
2766 symbol was defined in a regular object, then it actually doesn't
2767 need a PLT entry. Likewise, if the symbol has non-default
2768 visibility. If the symbol has hidden or internal visibility, we
2769 will force it local. */
2770 else if (h->needs_plt
2771 && bfd_link_pic (eif->info)
2772 && is_elf_hash_table (eif->info->hash)
2773 && (SYMBOLIC_BIND (eif->info, h)
2774 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2775 && h->def_regular)
2776 {
2777 bfd_boolean force_local;
2778
2779 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2780 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2781 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2782 }
2783
2784 /* If this is a weak defined symbol in a dynamic object, and we know
2785 the real definition in the dynamic object, copy interesting flags
2786 over to the real definition. */
2787 if (h->is_weakalias)
2788 {
2789 struct elf_link_hash_entry *def = weakdef (h);
2790
2791 /* If the real definition is defined by a regular object file,
2792 don't do anything special. See the longer description in
2793 _bfd_elf_adjust_dynamic_symbol, below. */
2794 if (def->def_regular)
2795 {
2796 h = def;
2797 while ((h = h->u.alias) != def)
2798 h->is_weakalias = 0;
2799 }
2800 else
2801 {
2802 while (h->root.type == bfd_link_hash_indirect)
2803 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2804 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2805 || h->root.type == bfd_link_hash_defweak);
2806 BFD_ASSERT (def->def_dynamic);
2807 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
2808 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
2809 }
2810 }
2811
2812 return TRUE;
2813 }
2814
2815 /* Make the backend pick a good value for a dynamic symbol. This is
2816 called via elf_link_hash_traverse, and also calls itself
2817 recursively. */
2818
2819 static bfd_boolean
2820 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2821 {
2822 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2823 struct elf_link_hash_table *htab;
2824 const struct elf_backend_data *bed;
2825
2826 if (! is_elf_hash_table (eif->info->hash))
2827 return FALSE;
2828
2829 /* Ignore indirect symbols. These are added by the versioning code. */
2830 if (h->root.type == bfd_link_hash_indirect)
2831 return TRUE;
2832
2833 /* Fix the symbol flags. */
2834 if (! _bfd_elf_fix_symbol_flags (h, eif))
2835 return FALSE;
2836
2837 htab = elf_hash_table (eif->info);
2838 bed = get_elf_backend_data (htab->dynobj);
2839
2840 if (h->root.type == bfd_link_hash_undefweak)
2841 {
2842 if (eif->info->dynamic_undefined_weak == 0)
2843 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2844 else if (eif->info->dynamic_undefined_weak > 0
2845 && h->ref_regular
2846 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2847 && !bfd_hide_sym_by_version (eif->info->version_info,
2848 h->root.root.string))
2849 {
2850 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2851 {
2852 eif->failed = TRUE;
2853 return FALSE;
2854 }
2855 }
2856 }
2857
2858 /* If this symbol does not require a PLT entry, and it is not
2859 defined by a dynamic object, or is not referenced by a regular
2860 object, ignore it. We do have to handle a weak defined symbol,
2861 even if no regular object refers to it, if we decided to add it
2862 to the dynamic symbol table. FIXME: Do we normally need to worry
2863 about symbols which are defined by one dynamic object and
2864 referenced by another one? */
2865 if (!h->needs_plt
2866 && h->type != STT_GNU_IFUNC
2867 && (h->def_regular
2868 || !h->def_dynamic
2869 || (!h->ref_regular
2870 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
2871 {
2872 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2873 return TRUE;
2874 }
2875
2876 /* If we've already adjusted this symbol, don't do it again. This
2877 can happen via a recursive call. */
2878 if (h->dynamic_adjusted)
2879 return TRUE;
2880
2881 /* Don't look at this symbol again. Note that we must set this
2882 after checking the above conditions, because we may look at a
2883 symbol once, decide not to do anything, and then get called
2884 recursively later after REF_REGULAR is set below. */
2885 h->dynamic_adjusted = 1;
2886
2887 /* If this is a weak definition, and we know a real definition, and
2888 the real symbol is not itself defined by a regular object file,
2889 then get a good value for the real definition. We handle the
2890 real symbol first, for the convenience of the backend routine.
2891
2892 Note that there is a confusing case here. If the real definition
2893 is defined by a regular object file, we don't get the real symbol
2894 from the dynamic object, but we do get the weak symbol. If the
2895 processor backend uses a COPY reloc, then if some routine in the
2896 dynamic object changes the real symbol, we will not see that
2897 change in the corresponding weak symbol. This is the way other
2898 ELF linkers work as well, and seems to be a result of the shared
2899 library model.
2900
2901 I will clarify this issue. Most SVR4 shared libraries define the
2902 variable _timezone and define timezone as a weak synonym. The
2903 tzset call changes _timezone. If you write
2904 extern int timezone;
2905 int _timezone = 5;
2906 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2907 you might expect that, since timezone is a synonym for _timezone,
2908 the same number will print both times. However, if the processor
2909 backend uses a COPY reloc, then actually timezone will be copied
2910 into your process image, and, since you define _timezone
2911 yourself, _timezone will not. Thus timezone and _timezone will
2912 wind up at different memory locations. The tzset call will set
2913 _timezone, leaving timezone unchanged. */
2914
2915 if (h->is_weakalias)
2916 {
2917 struct elf_link_hash_entry *def = weakdef (h);
2918
2919 /* If we get to this point, there is an implicit reference to
2920 the alias by a regular object file via the weak symbol H. */
2921 def->ref_regular = 1;
2922
2923 /* Ensure that the backend adjust_dynamic_symbol function sees
2924 the strong alias before H by recursively calling ourselves. */
2925 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
2926 return FALSE;
2927 }
2928
2929 /* If a symbol has no type and no size and does not require a PLT
2930 entry, then we are probably about to do the wrong thing here: we
2931 are probably going to create a COPY reloc for an empty object.
2932 This case can arise when a shared object is built with assembly
2933 code, and the assembly code fails to set the symbol type. */
2934 if (h->size == 0
2935 && h->type == STT_NOTYPE
2936 && !h->needs_plt)
2937 _bfd_error_handler
2938 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2939 h->root.root.string);
2940
2941 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2942 {
2943 eif->failed = TRUE;
2944 return FALSE;
2945 }
2946
2947 return TRUE;
2948 }
2949
2950 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2951 DYNBSS. */
2952
2953 bfd_boolean
2954 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2955 struct elf_link_hash_entry *h,
2956 asection *dynbss)
2957 {
2958 unsigned int power_of_two;
2959 bfd_vma mask;
2960 asection *sec = h->root.u.def.section;
2961
2962 /* The section alignment of the definition is the maximum alignment
2963 requirement of symbols defined in the section. Since we don't
2964 know the symbol alignment requirement, we start with the
2965 maximum alignment and check low bits of the symbol address
2966 for the minimum alignment. */
2967 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2968 mask = ((bfd_vma) 1 << power_of_two) - 1;
2969 while ((h->root.u.def.value & mask) != 0)
2970 {
2971 mask >>= 1;
2972 --power_of_two;
2973 }
2974
2975 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2976 dynbss))
2977 {
2978 /* Adjust the section alignment if needed. */
2979 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2980 power_of_two))
2981 return FALSE;
2982 }
2983
2984 /* We make sure that the symbol will be aligned properly. */
2985 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2986
2987 /* Define the symbol as being at this point in DYNBSS. */
2988 h->root.u.def.section = dynbss;
2989 h->root.u.def.value = dynbss->size;
2990
2991 /* Increment the size of DYNBSS to make room for the symbol. */
2992 dynbss->size += h->size;
2993
2994 /* No error if extern_protected_data is true. */
2995 if (h->protected_def
2996 && (!info->extern_protected_data
2997 || (info->extern_protected_data < 0
2998 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
2999 info->callbacks->einfo
3000 (_("%P: copy reloc against protected `%T' is dangerous\n"),
3001 h->root.root.string);
3002
3003 return TRUE;
3004 }
3005
3006 /* Adjust all external symbols pointing into SEC_MERGE sections
3007 to reflect the object merging within the sections. */
3008
3009 static bfd_boolean
3010 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3011 {
3012 asection *sec;
3013
3014 if ((h->root.type == bfd_link_hash_defined
3015 || h->root.type == bfd_link_hash_defweak)
3016 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3017 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3018 {
3019 bfd *output_bfd = (bfd *) data;
3020
3021 h->root.u.def.value =
3022 _bfd_merged_section_offset (output_bfd,
3023 &h->root.u.def.section,
3024 elf_section_data (sec)->sec_info,
3025 h->root.u.def.value);
3026 }
3027
3028 return TRUE;
3029 }
3030
3031 /* Returns false if the symbol referred to by H should be considered
3032 to resolve local to the current module, and true if it should be
3033 considered to bind dynamically. */
3034
3035 bfd_boolean
3036 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3037 struct bfd_link_info *info,
3038 bfd_boolean not_local_protected)
3039 {
3040 bfd_boolean binding_stays_local_p;
3041 const struct elf_backend_data *bed;
3042 struct elf_link_hash_table *hash_table;
3043
3044 if (h == NULL)
3045 return FALSE;
3046
3047 while (h->root.type == bfd_link_hash_indirect
3048 || h->root.type == bfd_link_hash_warning)
3049 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3050
3051 /* If it was forced local, then clearly it's not dynamic. */
3052 if (h->dynindx == -1)
3053 return FALSE;
3054 if (h->forced_local)
3055 return FALSE;
3056
3057 /* Identify the cases where name binding rules say that a
3058 visible symbol resolves locally. */
3059 binding_stays_local_p = (bfd_link_executable (info)
3060 || SYMBOLIC_BIND (info, h));
3061
3062 switch (ELF_ST_VISIBILITY (h->other))
3063 {
3064 case STV_INTERNAL:
3065 case STV_HIDDEN:
3066 return FALSE;
3067
3068 case STV_PROTECTED:
3069 hash_table = elf_hash_table (info);
3070 if (!is_elf_hash_table (hash_table))
3071 return FALSE;
3072
3073 bed = get_elf_backend_data (hash_table->dynobj);
3074
3075 /* Proper resolution for function pointer equality may require
3076 that these symbols perhaps be resolved dynamically, even though
3077 we should be resolving them to the current module. */
3078 if (!not_local_protected || !bed->is_function_type (h->type))
3079 binding_stays_local_p = TRUE;
3080 break;
3081
3082 default:
3083 break;
3084 }
3085
3086 /* If it isn't defined locally, then clearly it's dynamic. */
3087 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3088 return TRUE;
3089
3090 /* Otherwise, the symbol is dynamic if binding rules don't tell
3091 us that it remains local. */
3092 return !binding_stays_local_p;
3093 }
3094
3095 /* Return true if the symbol referred to by H should be considered
3096 to resolve local to the current module, and false otherwise. Differs
3097 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3098 undefined symbols. The two functions are virtually identical except
3099 for the place where dynindx == -1 is tested. If that test is true,
3100 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3101 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3102 defined symbols.
3103 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3104 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3105 treatment of undefined weak symbols. For those that do not make
3106 undefined weak symbols dynamic, both functions may return false. */
3107
3108 bfd_boolean
3109 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3110 struct bfd_link_info *info,
3111 bfd_boolean local_protected)
3112 {
3113 const struct elf_backend_data *bed;
3114 struct elf_link_hash_table *hash_table;
3115
3116 /* If it's a local sym, of course we resolve locally. */
3117 if (h == NULL)
3118 return TRUE;
3119
3120 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3121 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3122 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3123 return TRUE;
3124
3125 /* Forced local symbols resolve locally. */
3126 if (h->forced_local)
3127 return TRUE;
3128
3129 /* Common symbols that become definitions don't get the DEF_REGULAR
3130 flag set, so test it first, and don't bail out. */
3131 if (ELF_COMMON_DEF_P (h))
3132 /* Do nothing. */;
3133 /* If we don't have a definition in a regular file, then we can't
3134 resolve locally. The sym is either undefined or dynamic. */
3135 else if (!h->def_regular)
3136 return FALSE;
3137
3138 /* Non-dynamic symbols resolve locally. */
3139 if (h->dynindx == -1)
3140 return TRUE;
3141
3142 /* At this point, we know the symbol is defined and dynamic. In an
3143 executable it must resolve locally, likewise when building symbolic
3144 shared libraries. */
3145 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3146 return TRUE;
3147
3148 /* Now deal with defined dynamic symbols in shared libraries. Ones
3149 with default visibility might not resolve locally. */
3150 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3151 return FALSE;
3152
3153 hash_table = elf_hash_table (info);
3154 if (!is_elf_hash_table (hash_table))
3155 return TRUE;
3156
3157 bed = get_elf_backend_data (hash_table->dynobj);
3158
3159 /* If extern_protected_data is false, STV_PROTECTED non-function
3160 symbols are local. */
3161 if ((!info->extern_protected_data
3162 || (info->extern_protected_data < 0
3163 && !bed->extern_protected_data))
3164 && !bed->is_function_type (h->type))
3165 return TRUE;
3166
3167 /* Function pointer equality tests may require that STV_PROTECTED
3168 symbols be treated as dynamic symbols. If the address of a
3169 function not defined in an executable is set to that function's
3170 plt entry in the executable, then the address of the function in
3171 a shared library must also be the plt entry in the executable. */
3172 return local_protected;
3173 }
3174
3175 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3176 aligned. Returns the first TLS output section. */
3177
3178 struct bfd_section *
3179 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3180 {
3181 struct bfd_section *sec, *tls;
3182 unsigned int align = 0;
3183
3184 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3185 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3186 break;
3187 tls = sec;
3188
3189 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3190 if (sec->alignment_power > align)
3191 align = sec->alignment_power;
3192
3193 elf_hash_table (info)->tls_sec = tls;
3194
3195 /* Ensure the alignment of the first section is the largest alignment,
3196 so that the tls segment starts aligned. */
3197 if (tls != NULL)
3198 tls->alignment_power = align;
3199
3200 return tls;
3201 }
3202
3203 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3204 static bfd_boolean
3205 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3206 Elf_Internal_Sym *sym)
3207 {
3208 const struct elf_backend_data *bed;
3209
3210 /* Local symbols do not count, but target specific ones might. */
3211 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3212 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3213 return FALSE;
3214
3215 bed = get_elf_backend_data (abfd);
3216 /* Function symbols do not count. */
3217 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3218 return FALSE;
3219
3220 /* If the section is undefined, then so is the symbol. */
3221 if (sym->st_shndx == SHN_UNDEF)
3222 return FALSE;
3223
3224 /* If the symbol is defined in the common section, then
3225 it is a common definition and so does not count. */
3226 if (bed->common_definition (sym))
3227 return FALSE;
3228
3229 /* If the symbol is in a target specific section then we
3230 must rely upon the backend to tell us what it is. */
3231 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3232 /* FIXME - this function is not coded yet:
3233
3234 return _bfd_is_global_symbol_definition (abfd, sym);
3235
3236 Instead for now assume that the definition is not global,
3237 Even if this is wrong, at least the linker will behave
3238 in the same way that it used to do. */
3239 return FALSE;
3240
3241 return TRUE;
3242 }
3243
3244 /* Search the symbol table of the archive element of the archive ABFD
3245 whose archive map contains a mention of SYMDEF, and determine if
3246 the symbol is defined in this element. */
3247 static bfd_boolean
3248 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3249 {
3250 Elf_Internal_Shdr * hdr;
3251 size_t symcount;
3252 size_t extsymcount;
3253 size_t extsymoff;
3254 Elf_Internal_Sym *isymbuf;
3255 Elf_Internal_Sym *isym;
3256 Elf_Internal_Sym *isymend;
3257 bfd_boolean result;
3258
3259 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3260 if (abfd == NULL)
3261 return FALSE;
3262
3263 if (! bfd_check_format (abfd, bfd_object))
3264 return FALSE;
3265
3266 /* Select the appropriate symbol table. If we don't know if the
3267 object file is an IR object, give linker LTO plugin a chance to
3268 get the correct symbol table. */
3269 if (abfd->plugin_format == bfd_plugin_yes
3270 #if BFD_SUPPORTS_PLUGINS
3271 || (abfd->plugin_format == bfd_plugin_unknown
3272 && bfd_link_plugin_object_p (abfd))
3273 #endif
3274 )
3275 {
3276 /* Use the IR symbol table if the object has been claimed by
3277 plugin. */
3278 abfd = abfd->plugin_dummy_bfd;
3279 hdr = &elf_tdata (abfd)->symtab_hdr;
3280 }
3281 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3282 hdr = &elf_tdata (abfd)->symtab_hdr;
3283 else
3284 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3285
3286 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3287
3288 /* The sh_info field of the symtab header tells us where the
3289 external symbols start. We don't care about the local symbols. */
3290 if (elf_bad_symtab (abfd))
3291 {
3292 extsymcount = symcount;
3293 extsymoff = 0;
3294 }
3295 else
3296 {
3297 extsymcount = symcount - hdr->sh_info;
3298 extsymoff = hdr->sh_info;
3299 }
3300
3301 if (extsymcount == 0)
3302 return FALSE;
3303
3304 /* Read in the symbol table. */
3305 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3306 NULL, NULL, NULL);
3307 if (isymbuf == NULL)
3308 return FALSE;
3309
3310 /* Scan the symbol table looking for SYMDEF. */
3311 result = FALSE;
3312 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3313 {
3314 const char *name;
3315
3316 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3317 isym->st_name);
3318 if (name == NULL)
3319 break;
3320
3321 if (strcmp (name, symdef->name) == 0)
3322 {
3323 result = is_global_data_symbol_definition (abfd, isym);
3324 break;
3325 }
3326 }
3327
3328 free (isymbuf);
3329
3330 return result;
3331 }
3332 \f
3333 /* Add an entry to the .dynamic table. */
3334
3335 bfd_boolean
3336 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3337 bfd_vma tag,
3338 bfd_vma val)
3339 {
3340 struct elf_link_hash_table *hash_table;
3341 const struct elf_backend_data *bed;
3342 asection *s;
3343 bfd_size_type newsize;
3344 bfd_byte *newcontents;
3345 Elf_Internal_Dyn dyn;
3346
3347 hash_table = elf_hash_table (info);
3348 if (! is_elf_hash_table (hash_table))
3349 return FALSE;
3350
3351 bed = get_elf_backend_data (hash_table->dynobj);
3352 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3353 BFD_ASSERT (s != NULL);
3354
3355 newsize = s->size + bed->s->sizeof_dyn;
3356 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3357 if (newcontents == NULL)
3358 return FALSE;
3359
3360 dyn.d_tag = tag;
3361 dyn.d_un.d_val = val;
3362 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3363
3364 s->size = newsize;
3365 s->contents = newcontents;
3366
3367 return TRUE;
3368 }
3369
3370 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3371 otherwise just check whether one already exists. Returns -1 on error,
3372 1 if a DT_NEEDED tag already exists, and 0 on success. */
3373
3374 static int
3375 elf_add_dt_needed_tag (bfd *abfd,
3376 struct bfd_link_info *info,
3377 const char *soname,
3378 bfd_boolean do_it)
3379 {
3380 struct elf_link_hash_table *hash_table;
3381 size_t strindex;
3382
3383 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3384 return -1;
3385
3386 hash_table = elf_hash_table (info);
3387 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3388 if (strindex == (size_t) -1)
3389 return -1;
3390
3391 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3392 {
3393 asection *sdyn;
3394 const struct elf_backend_data *bed;
3395 bfd_byte *extdyn;
3396
3397 bed = get_elf_backend_data (hash_table->dynobj);
3398 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3399 if (sdyn != NULL)
3400 for (extdyn = sdyn->contents;
3401 extdyn < sdyn->contents + sdyn->size;
3402 extdyn += bed->s->sizeof_dyn)
3403 {
3404 Elf_Internal_Dyn dyn;
3405
3406 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3407 if (dyn.d_tag == DT_NEEDED
3408 && dyn.d_un.d_val == strindex)
3409 {
3410 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3411 return 1;
3412 }
3413 }
3414 }
3415
3416 if (do_it)
3417 {
3418 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3419 return -1;
3420
3421 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3422 return -1;
3423 }
3424 else
3425 /* We were just checking for existence of the tag. */
3426 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3427
3428 return 0;
3429 }
3430
3431 /* Return true if SONAME is on the needed list between NEEDED and STOP
3432 (or the end of list if STOP is NULL), and needed by a library that
3433 will be loaded. */
3434
3435 static bfd_boolean
3436 on_needed_list (const char *soname,
3437 struct bfd_link_needed_list *needed,
3438 struct bfd_link_needed_list *stop)
3439 {
3440 struct bfd_link_needed_list *look;
3441 for (look = needed; look != stop; look = look->next)
3442 if (strcmp (soname, look->name) == 0
3443 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3444 /* If needed by a library that itself is not directly
3445 needed, recursively check whether that library is
3446 indirectly needed. Since we add DT_NEEDED entries to
3447 the end of the list, library dependencies appear after
3448 the library. Therefore search prior to the current
3449 LOOK, preventing possible infinite recursion. */
3450 || on_needed_list (elf_dt_name (look->by), needed, look)))
3451 return TRUE;
3452
3453 return FALSE;
3454 }
3455
3456 /* Sort symbol by value, section, and size. */
3457 static int
3458 elf_sort_symbol (const void *arg1, const void *arg2)
3459 {
3460 const struct elf_link_hash_entry *h1;
3461 const struct elf_link_hash_entry *h2;
3462 bfd_signed_vma vdiff;
3463
3464 h1 = *(const struct elf_link_hash_entry **) arg1;
3465 h2 = *(const struct elf_link_hash_entry **) arg2;
3466 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3467 if (vdiff != 0)
3468 return vdiff > 0 ? 1 : -1;
3469 else
3470 {
3471 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3472 if (sdiff != 0)
3473 return sdiff > 0 ? 1 : -1;
3474 }
3475 vdiff = h1->size - h2->size;
3476 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3477 }
3478
3479 /* This function is used to adjust offsets into .dynstr for
3480 dynamic symbols. This is called via elf_link_hash_traverse. */
3481
3482 static bfd_boolean
3483 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3484 {
3485 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3486
3487 if (h->dynindx != -1)
3488 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3489 return TRUE;
3490 }
3491
3492 /* Assign string offsets in .dynstr, update all structures referencing
3493 them. */
3494
3495 static bfd_boolean
3496 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3497 {
3498 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3499 struct elf_link_local_dynamic_entry *entry;
3500 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3501 bfd *dynobj = hash_table->dynobj;
3502 asection *sdyn;
3503 bfd_size_type size;
3504 const struct elf_backend_data *bed;
3505 bfd_byte *extdyn;
3506
3507 _bfd_elf_strtab_finalize (dynstr);
3508 size = _bfd_elf_strtab_size (dynstr);
3509
3510 bed = get_elf_backend_data (dynobj);
3511 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3512 BFD_ASSERT (sdyn != NULL);
3513
3514 /* Update all .dynamic entries referencing .dynstr strings. */
3515 for (extdyn = sdyn->contents;
3516 extdyn < sdyn->contents + sdyn->size;
3517 extdyn += bed->s->sizeof_dyn)
3518 {
3519 Elf_Internal_Dyn dyn;
3520
3521 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3522 switch (dyn.d_tag)
3523 {
3524 case DT_STRSZ:
3525 dyn.d_un.d_val = size;
3526 break;
3527 case DT_NEEDED:
3528 case DT_SONAME:
3529 case DT_RPATH:
3530 case DT_RUNPATH:
3531 case DT_FILTER:
3532 case DT_AUXILIARY:
3533 case DT_AUDIT:
3534 case DT_DEPAUDIT:
3535 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3536 break;
3537 default:
3538 continue;
3539 }
3540 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3541 }
3542
3543 /* Now update local dynamic symbols. */
3544 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3545 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3546 entry->isym.st_name);
3547
3548 /* And the rest of dynamic symbols. */
3549 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3550
3551 /* Adjust version definitions. */
3552 if (elf_tdata (output_bfd)->cverdefs)
3553 {
3554 asection *s;
3555 bfd_byte *p;
3556 size_t i;
3557 Elf_Internal_Verdef def;
3558 Elf_Internal_Verdaux defaux;
3559
3560 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3561 p = s->contents;
3562 do
3563 {
3564 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3565 &def);
3566 p += sizeof (Elf_External_Verdef);
3567 if (def.vd_aux != sizeof (Elf_External_Verdef))
3568 continue;
3569 for (i = 0; i < def.vd_cnt; ++i)
3570 {
3571 _bfd_elf_swap_verdaux_in (output_bfd,
3572 (Elf_External_Verdaux *) p, &defaux);
3573 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3574 defaux.vda_name);
3575 _bfd_elf_swap_verdaux_out (output_bfd,
3576 &defaux, (Elf_External_Verdaux *) p);
3577 p += sizeof (Elf_External_Verdaux);
3578 }
3579 }
3580 while (def.vd_next);
3581 }
3582
3583 /* Adjust version references. */
3584 if (elf_tdata (output_bfd)->verref)
3585 {
3586 asection *s;
3587 bfd_byte *p;
3588 size_t i;
3589 Elf_Internal_Verneed need;
3590 Elf_Internal_Vernaux needaux;
3591
3592 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3593 p = s->contents;
3594 do
3595 {
3596 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3597 &need);
3598 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3599 _bfd_elf_swap_verneed_out (output_bfd, &need,
3600 (Elf_External_Verneed *) p);
3601 p += sizeof (Elf_External_Verneed);
3602 for (i = 0; i < need.vn_cnt; ++i)
3603 {
3604 _bfd_elf_swap_vernaux_in (output_bfd,
3605 (Elf_External_Vernaux *) p, &needaux);
3606 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3607 needaux.vna_name);
3608 _bfd_elf_swap_vernaux_out (output_bfd,
3609 &needaux,
3610 (Elf_External_Vernaux *) p);
3611 p += sizeof (Elf_External_Vernaux);
3612 }
3613 }
3614 while (need.vn_next);
3615 }
3616
3617 return TRUE;
3618 }
3619 \f
3620 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3621 The default is to only match when the INPUT and OUTPUT are exactly
3622 the same target. */
3623
3624 bfd_boolean
3625 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3626 const bfd_target *output)
3627 {
3628 return input == output;
3629 }
3630
3631 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3632 This version is used when different targets for the same architecture
3633 are virtually identical. */
3634
3635 bfd_boolean
3636 _bfd_elf_relocs_compatible (const bfd_target *input,
3637 const bfd_target *output)
3638 {
3639 const struct elf_backend_data *obed, *ibed;
3640
3641 if (input == output)
3642 return TRUE;
3643
3644 ibed = xvec_get_elf_backend_data (input);
3645 obed = xvec_get_elf_backend_data (output);
3646
3647 if (ibed->arch != obed->arch)
3648 return FALSE;
3649
3650 /* If both backends are using this function, deem them compatible. */
3651 return ibed->relocs_compatible == obed->relocs_compatible;
3652 }
3653
3654 /* Make a special call to the linker "notice" function to tell it that
3655 we are about to handle an as-needed lib, or have finished
3656 processing the lib. */
3657
3658 bfd_boolean
3659 _bfd_elf_notice_as_needed (bfd *ibfd,
3660 struct bfd_link_info *info,
3661 enum notice_asneeded_action act)
3662 {
3663 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3664 }
3665
3666 /* Check relocations an ELF object file. */
3667
3668 bfd_boolean
3669 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3670 {
3671 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3672 struct elf_link_hash_table *htab = elf_hash_table (info);
3673
3674 /* If this object is the same format as the output object, and it is
3675 not a shared library, then let the backend look through the
3676 relocs.
3677
3678 This is required to build global offset table entries and to
3679 arrange for dynamic relocs. It is not required for the
3680 particular common case of linking non PIC code, even when linking
3681 against shared libraries, but unfortunately there is no way of
3682 knowing whether an object file has been compiled PIC or not.
3683 Looking through the relocs is not particularly time consuming.
3684 The problem is that we must either (1) keep the relocs in memory,
3685 which causes the linker to require additional runtime memory or
3686 (2) read the relocs twice from the input file, which wastes time.
3687 This would be a good case for using mmap.
3688
3689 I have no idea how to handle linking PIC code into a file of a
3690 different format. It probably can't be done. */
3691 if ((abfd->flags & DYNAMIC) == 0
3692 && is_elf_hash_table (htab)
3693 && bed->check_relocs != NULL
3694 && elf_object_id (abfd) == elf_hash_table_id (htab)
3695 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3696 {
3697 asection *o;
3698
3699 for (o = abfd->sections; o != NULL; o = o->next)
3700 {
3701 Elf_Internal_Rela *internal_relocs;
3702 bfd_boolean ok;
3703
3704 /* Don't check relocations in excluded sections. */
3705 if ((o->flags & SEC_RELOC) == 0
3706 || (o->flags & SEC_EXCLUDE) != 0
3707 || o->reloc_count == 0
3708 || ((info->strip == strip_all || info->strip == strip_debugger)
3709 && (o->flags & SEC_DEBUGGING) != 0)
3710 || bfd_is_abs_section (o->output_section))
3711 continue;
3712
3713 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3714 info->keep_memory);
3715 if (internal_relocs == NULL)
3716 return FALSE;
3717
3718 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3719
3720 if (elf_section_data (o)->relocs != internal_relocs)
3721 free (internal_relocs);
3722
3723 if (! ok)
3724 return FALSE;
3725 }
3726 }
3727
3728 return TRUE;
3729 }
3730
3731 /* Add symbols from an ELF object file to the linker hash table. */
3732
3733 static bfd_boolean
3734 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3735 {
3736 Elf_Internal_Ehdr *ehdr;
3737 Elf_Internal_Shdr *hdr;
3738 size_t symcount;
3739 size_t extsymcount;
3740 size_t extsymoff;
3741 struct elf_link_hash_entry **sym_hash;
3742 bfd_boolean dynamic;
3743 Elf_External_Versym *extversym = NULL;
3744 Elf_External_Versym *ever;
3745 struct elf_link_hash_entry *weaks;
3746 struct elf_link_hash_entry **nondeflt_vers = NULL;
3747 size_t nondeflt_vers_cnt = 0;
3748 Elf_Internal_Sym *isymbuf = NULL;
3749 Elf_Internal_Sym *isym;
3750 Elf_Internal_Sym *isymend;
3751 const struct elf_backend_data *bed;
3752 bfd_boolean add_needed;
3753 struct elf_link_hash_table *htab;
3754 bfd_size_type amt;
3755 void *alloc_mark = NULL;
3756 struct bfd_hash_entry **old_table = NULL;
3757 unsigned int old_size = 0;
3758 unsigned int old_count = 0;
3759 void *old_tab = NULL;
3760 void *old_ent;
3761 struct bfd_link_hash_entry *old_undefs = NULL;
3762 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3763 void *old_strtab = NULL;
3764 size_t tabsize = 0;
3765 asection *s;
3766 bfd_boolean just_syms;
3767
3768 htab = elf_hash_table (info);
3769 bed = get_elf_backend_data (abfd);
3770
3771 if ((abfd->flags & DYNAMIC) == 0)
3772 dynamic = FALSE;
3773 else
3774 {
3775 dynamic = TRUE;
3776
3777 /* You can't use -r against a dynamic object. Also, there's no
3778 hope of using a dynamic object which does not exactly match
3779 the format of the output file. */
3780 if (bfd_link_relocatable (info)
3781 || !is_elf_hash_table (htab)
3782 || info->output_bfd->xvec != abfd->xvec)
3783 {
3784 if (bfd_link_relocatable (info))
3785 bfd_set_error (bfd_error_invalid_operation);
3786 else
3787 bfd_set_error (bfd_error_wrong_format);
3788 goto error_return;
3789 }
3790 }
3791
3792 ehdr = elf_elfheader (abfd);
3793 if (info->warn_alternate_em
3794 && bed->elf_machine_code != ehdr->e_machine
3795 && ((bed->elf_machine_alt1 != 0
3796 && ehdr->e_machine == bed->elf_machine_alt1)
3797 || (bed->elf_machine_alt2 != 0
3798 && ehdr->e_machine == bed->elf_machine_alt2)))
3799 info->callbacks->einfo
3800 /* xgettext:c-format */
3801 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3802 ehdr->e_machine, abfd, bed->elf_machine_code);
3803
3804 /* As a GNU extension, any input sections which are named
3805 .gnu.warning.SYMBOL are treated as warning symbols for the given
3806 symbol. This differs from .gnu.warning sections, which generate
3807 warnings when they are included in an output file. */
3808 /* PR 12761: Also generate this warning when building shared libraries. */
3809 for (s = abfd->sections; s != NULL; s = s->next)
3810 {
3811 const char *name;
3812
3813 name = bfd_get_section_name (abfd, s);
3814 if (CONST_STRNEQ (name, ".gnu.warning."))
3815 {
3816 char *msg;
3817 bfd_size_type sz;
3818
3819 name += sizeof ".gnu.warning." - 1;
3820
3821 /* If this is a shared object, then look up the symbol
3822 in the hash table. If it is there, and it is already
3823 been defined, then we will not be using the entry
3824 from this shared object, so we don't need to warn.
3825 FIXME: If we see the definition in a regular object
3826 later on, we will warn, but we shouldn't. The only
3827 fix is to keep track of what warnings we are supposed
3828 to emit, and then handle them all at the end of the
3829 link. */
3830 if (dynamic)
3831 {
3832 struct elf_link_hash_entry *h;
3833
3834 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3835
3836 /* FIXME: What about bfd_link_hash_common? */
3837 if (h != NULL
3838 && (h->root.type == bfd_link_hash_defined
3839 || h->root.type == bfd_link_hash_defweak))
3840 continue;
3841 }
3842
3843 sz = s->size;
3844 msg = (char *) bfd_alloc (abfd, sz + 1);
3845 if (msg == NULL)
3846 goto error_return;
3847
3848 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3849 goto error_return;
3850
3851 msg[sz] = '\0';
3852
3853 if (! (_bfd_generic_link_add_one_symbol
3854 (info, abfd, name, BSF_WARNING, s, 0, msg,
3855 FALSE, bed->collect, NULL)))
3856 goto error_return;
3857
3858 if (bfd_link_executable (info))
3859 {
3860 /* Clobber the section size so that the warning does
3861 not get copied into the output file. */
3862 s->size = 0;
3863
3864 /* Also set SEC_EXCLUDE, so that symbols defined in
3865 the warning section don't get copied to the output. */
3866 s->flags |= SEC_EXCLUDE;
3867 }
3868 }
3869 }
3870
3871 just_syms = ((s = abfd->sections) != NULL
3872 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3873
3874 add_needed = TRUE;
3875 if (! dynamic)
3876 {
3877 /* If we are creating a shared library, create all the dynamic
3878 sections immediately. We need to attach them to something,
3879 so we attach them to this BFD, provided it is the right
3880 format and is not from ld --just-symbols. Always create the
3881 dynamic sections for -E/--dynamic-list. FIXME: If there
3882 are no input BFD's of the same format as the output, we can't
3883 make a shared library. */
3884 if (!just_syms
3885 && (bfd_link_pic (info)
3886 || (!bfd_link_relocatable (info)
3887 && info->nointerp
3888 && (info->export_dynamic || info->dynamic)))
3889 && is_elf_hash_table (htab)
3890 && info->output_bfd->xvec == abfd->xvec
3891 && !htab->dynamic_sections_created)
3892 {
3893 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3894 goto error_return;
3895 }
3896 }
3897 else if (!is_elf_hash_table (htab))
3898 goto error_return;
3899 else
3900 {
3901 const char *soname = NULL;
3902 char *audit = NULL;
3903 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3904 const Elf_Internal_Phdr *phdr;
3905 int ret;
3906
3907 /* ld --just-symbols and dynamic objects don't mix very well.
3908 ld shouldn't allow it. */
3909 if (just_syms)
3910 abort ();
3911
3912 /* If this dynamic lib was specified on the command line with
3913 --as-needed in effect, then we don't want to add a DT_NEEDED
3914 tag unless the lib is actually used. Similary for libs brought
3915 in by another lib's DT_NEEDED. When --no-add-needed is used
3916 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3917 any dynamic library in DT_NEEDED tags in the dynamic lib at
3918 all. */
3919 add_needed = (elf_dyn_lib_class (abfd)
3920 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3921 | DYN_NO_NEEDED)) == 0;
3922
3923 s = bfd_get_section_by_name (abfd, ".dynamic");
3924 if (s != NULL)
3925 {
3926 bfd_byte *dynbuf;
3927 bfd_byte *extdyn;
3928 unsigned int elfsec;
3929 unsigned long shlink;
3930
3931 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3932 {
3933 error_free_dyn:
3934 free (dynbuf);
3935 goto error_return;
3936 }
3937
3938 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3939 if (elfsec == SHN_BAD)
3940 goto error_free_dyn;
3941 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3942
3943 for (extdyn = dynbuf;
3944 extdyn < dynbuf + s->size;
3945 extdyn += bed->s->sizeof_dyn)
3946 {
3947 Elf_Internal_Dyn dyn;
3948
3949 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3950 if (dyn.d_tag == DT_SONAME)
3951 {
3952 unsigned int tagv = dyn.d_un.d_val;
3953 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3954 if (soname == NULL)
3955 goto error_free_dyn;
3956 }
3957 if (dyn.d_tag == DT_NEEDED)
3958 {
3959 struct bfd_link_needed_list *n, **pn;
3960 char *fnm, *anm;
3961 unsigned int tagv = dyn.d_un.d_val;
3962
3963 amt = sizeof (struct bfd_link_needed_list);
3964 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3965 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3966 if (n == NULL || fnm == NULL)
3967 goto error_free_dyn;
3968 amt = strlen (fnm) + 1;
3969 anm = (char *) bfd_alloc (abfd, amt);
3970 if (anm == NULL)
3971 goto error_free_dyn;
3972 memcpy (anm, fnm, amt);
3973 n->name = anm;
3974 n->by = abfd;
3975 n->next = NULL;
3976 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3977 ;
3978 *pn = n;
3979 }
3980 if (dyn.d_tag == DT_RUNPATH)
3981 {
3982 struct bfd_link_needed_list *n, **pn;
3983 char *fnm, *anm;
3984 unsigned int tagv = dyn.d_un.d_val;
3985
3986 amt = sizeof (struct bfd_link_needed_list);
3987 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3988 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3989 if (n == NULL || fnm == NULL)
3990 goto error_free_dyn;
3991 amt = strlen (fnm) + 1;
3992 anm = (char *) bfd_alloc (abfd, amt);
3993 if (anm == NULL)
3994 goto error_free_dyn;
3995 memcpy (anm, fnm, amt);
3996 n->name = anm;
3997 n->by = abfd;
3998 n->next = NULL;
3999 for (pn = & runpath;
4000 *pn != NULL;
4001 pn = &(*pn)->next)
4002 ;
4003 *pn = n;
4004 }
4005 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4006 if (!runpath && dyn.d_tag == DT_RPATH)
4007 {
4008 struct bfd_link_needed_list *n, **pn;
4009 char *fnm, *anm;
4010 unsigned int tagv = dyn.d_un.d_val;
4011
4012 amt = sizeof (struct bfd_link_needed_list);
4013 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4014 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4015 if (n == NULL || fnm == NULL)
4016 goto error_free_dyn;
4017 amt = strlen (fnm) + 1;
4018 anm = (char *) bfd_alloc (abfd, amt);
4019 if (anm == NULL)
4020 goto error_free_dyn;
4021 memcpy (anm, fnm, amt);
4022 n->name = anm;
4023 n->by = abfd;
4024 n->next = NULL;
4025 for (pn = & rpath;
4026 *pn != NULL;
4027 pn = &(*pn)->next)
4028 ;
4029 *pn = n;
4030 }
4031 if (dyn.d_tag == DT_AUDIT)
4032 {
4033 unsigned int tagv = dyn.d_un.d_val;
4034 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4035 }
4036 }
4037
4038 free (dynbuf);
4039 }
4040
4041 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4042 frees all more recently bfd_alloc'd blocks as well. */
4043 if (runpath)
4044 rpath = runpath;
4045
4046 if (rpath)
4047 {
4048 struct bfd_link_needed_list **pn;
4049 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4050 ;
4051 *pn = rpath;
4052 }
4053
4054 /* If we have a PT_GNU_RELRO program header, mark as read-only
4055 all sections contained fully therein. This makes relro
4056 shared library sections appear as they will at run-time. */
4057 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4058 while (--phdr >= elf_tdata (abfd)->phdr)
4059 if (phdr->p_type == PT_GNU_RELRO)
4060 {
4061 for (s = abfd->sections; s != NULL; s = s->next)
4062 if ((s->flags & SEC_ALLOC) != 0
4063 && s->vma >= phdr->p_vaddr
4064 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4065 s->flags |= SEC_READONLY;
4066 break;
4067 }
4068
4069 /* We do not want to include any of the sections in a dynamic
4070 object in the output file. We hack by simply clobbering the
4071 list of sections in the BFD. This could be handled more
4072 cleanly by, say, a new section flag; the existing
4073 SEC_NEVER_LOAD flag is not the one we want, because that one
4074 still implies that the section takes up space in the output
4075 file. */
4076 bfd_section_list_clear (abfd);
4077
4078 /* Find the name to use in a DT_NEEDED entry that refers to this
4079 object. If the object has a DT_SONAME entry, we use it.
4080 Otherwise, if the generic linker stuck something in
4081 elf_dt_name, we use that. Otherwise, we just use the file
4082 name. */
4083 if (soname == NULL || *soname == '\0')
4084 {
4085 soname = elf_dt_name (abfd);
4086 if (soname == NULL || *soname == '\0')
4087 soname = bfd_get_filename (abfd);
4088 }
4089
4090 /* Save the SONAME because sometimes the linker emulation code
4091 will need to know it. */
4092 elf_dt_name (abfd) = soname;
4093
4094 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4095 if (ret < 0)
4096 goto error_return;
4097
4098 /* If we have already included this dynamic object in the
4099 link, just ignore it. There is no reason to include a
4100 particular dynamic object more than once. */
4101 if (ret > 0)
4102 return TRUE;
4103
4104 /* Save the DT_AUDIT entry for the linker emulation code. */
4105 elf_dt_audit (abfd) = audit;
4106 }
4107
4108 /* If this is a dynamic object, we always link against the .dynsym
4109 symbol table, not the .symtab symbol table. The dynamic linker
4110 will only see the .dynsym symbol table, so there is no reason to
4111 look at .symtab for a dynamic object. */
4112
4113 if (! dynamic || elf_dynsymtab (abfd) == 0)
4114 hdr = &elf_tdata (abfd)->symtab_hdr;
4115 else
4116 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4117
4118 symcount = hdr->sh_size / bed->s->sizeof_sym;
4119
4120 /* The sh_info field of the symtab header tells us where the
4121 external symbols start. We don't care about the local symbols at
4122 this point. */
4123 if (elf_bad_symtab (abfd))
4124 {
4125 extsymcount = symcount;
4126 extsymoff = 0;
4127 }
4128 else
4129 {
4130 extsymcount = symcount - hdr->sh_info;
4131 extsymoff = hdr->sh_info;
4132 }
4133
4134 sym_hash = elf_sym_hashes (abfd);
4135 if (extsymcount != 0)
4136 {
4137 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4138 NULL, NULL, NULL);
4139 if (isymbuf == NULL)
4140 goto error_return;
4141
4142 if (sym_hash == NULL)
4143 {
4144 /* We store a pointer to the hash table entry for each
4145 external symbol. */
4146 amt = extsymcount;
4147 amt *= sizeof (struct elf_link_hash_entry *);
4148 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4149 if (sym_hash == NULL)
4150 goto error_free_sym;
4151 elf_sym_hashes (abfd) = sym_hash;
4152 }
4153 }
4154
4155 if (dynamic)
4156 {
4157 /* Read in any version definitions. */
4158 if (!_bfd_elf_slurp_version_tables (abfd,
4159 info->default_imported_symver))
4160 goto error_free_sym;
4161
4162 /* Read in the symbol versions, but don't bother to convert them
4163 to internal format. */
4164 if (elf_dynversym (abfd) != 0)
4165 {
4166 Elf_Internal_Shdr *versymhdr;
4167
4168 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4169 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4170 if (extversym == NULL)
4171 goto error_free_sym;
4172 amt = versymhdr->sh_size;
4173 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4174 || bfd_bread (extversym, amt, abfd) != amt)
4175 goto error_free_vers;
4176 }
4177 }
4178
4179 /* If we are loading an as-needed shared lib, save the symbol table
4180 state before we start adding symbols. If the lib turns out
4181 to be unneeded, restore the state. */
4182 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4183 {
4184 unsigned int i;
4185 size_t entsize;
4186
4187 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4188 {
4189 struct bfd_hash_entry *p;
4190 struct elf_link_hash_entry *h;
4191
4192 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4193 {
4194 h = (struct elf_link_hash_entry *) p;
4195 entsize += htab->root.table.entsize;
4196 if (h->root.type == bfd_link_hash_warning)
4197 entsize += htab->root.table.entsize;
4198 }
4199 }
4200
4201 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4202 old_tab = bfd_malloc (tabsize + entsize);
4203 if (old_tab == NULL)
4204 goto error_free_vers;
4205
4206 /* Remember the current objalloc pointer, so that all mem for
4207 symbols added can later be reclaimed. */
4208 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4209 if (alloc_mark == NULL)
4210 goto error_free_vers;
4211
4212 /* Make a special call to the linker "notice" function to
4213 tell it that we are about to handle an as-needed lib. */
4214 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4215 goto error_free_vers;
4216
4217 /* Clone the symbol table. Remember some pointers into the
4218 symbol table, and dynamic symbol count. */
4219 old_ent = (char *) old_tab + tabsize;
4220 memcpy (old_tab, htab->root.table.table, tabsize);
4221 old_undefs = htab->root.undefs;
4222 old_undefs_tail = htab->root.undefs_tail;
4223 old_table = htab->root.table.table;
4224 old_size = htab->root.table.size;
4225 old_count = htab->root.table.count;
4226 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4227 if (old_strtab == NULL)
4228 goto error_free_vers;
4229
4230 for (i = 0; i < htab->root.table.size; i++)
4231 {
4232 struct bfd_hash_entry *p;
4233 struct elf_link_hash_entry *h;
4234
4235 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4236 {
4237 memcpy (old_ent, p, htab->root.table.entsize);
4238 old_ent = (char *) old_ent + htab->root.table.entsize;
4239 h = (struct elf_link_hash_entry *) p;
4240 if (h->root.type == bfd_link_hash_warning)
4241 {
4242 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4243 old_ent = (char *) old_ent + htab->root.table.entsize;
4244 }
4245 }
4246 }
4247 }
4248
4249 weaks = NULL;
4250 ever = extversym != NULL ? extversym + extsymoff : NULL;
4251 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4252 isym < isymend;
4253 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4254 {
4255 int bind;
4256 bfd_vma value;
4257 asection *sec, *new_sec;
4258 flagword flags;
4259 const char *name;
4260 struct elf_link_hash_entry *h;
4261 struct elf_link_hash_entry *hi;
4262 bfd_boolean definition;
4263 bfd_boolean size_change_ok;
4264 bfd_boolean type_change_ok;
4265 bfd_boolean new_weak;
4266 bfd_boolean old_weak;
4267 bfd_boolean override;
4268 bfd_boolean common;
4269 bfd_boolean discarded;
4270 unsigned int old_alignment;
4271 bfd *old_bfd;
4272 bfd_boolean matched;
4273
4274 override = FALSE;
4275
4276 flags = BSF_NO_FLAGS;
4277 sec = NULL;
4278 value = isym->st_value;
4279 common = bed->common_definition (isym);
4280 if (common && info->inhibit_common_definition)
4281 {
4282 /* Treat common symbol as undefined for --no-define-common. */
4283 isym->st_shndx = SHN_UNDEF;
4284 common = FALSE;
4285 }
4286 discarded = FALSE;
4287
4288 bind = ELF_ST_BIND (isym->st_info);
4289 switch (bind)
4290 {
4291 case STB_LOCAL:
4292 /* This should be impossible, since ELF requires that all
4293 global symbols follow all local symbols, and that sh_info
4294 point to the first global symbol. Unfortunately, Irix 5
4295 screws this up. */
4296 continue;
4297
4298 case STB_GLOBAL:
4299 if (isym->st_shndx != SHN_UNDEF && !common)
4300 flags = BSF_GLOBAL;
4301 break;
4302
4303 case STB_WEAK:
4304 flags = BSF_WEAK;
4305 break;
4306
4307 case STB_GNU_UNIQUE:
4308 flags = BSF_GNU_UNIQUE;
4309 break;
4310
4311 default:
4312 /* Leave it up to the processor backend. */
4313 break;
4314 }
4315
4316 if (isym->st_shndx == SHN_UNDEF)
4317 sec = bfd_und_section_ptr;
4318 else if (isym->st_shndx == SHN_ABS)
4319 sec = bfd_abs_section_ptr;
4320 else if (isym->st_shndx == SHN_COMMON)
4321 {
4322 sec = bfd_com_section_ptr;
4323 /* What ELF calls the size we call the value. What ELF
4324 calls the value we call the alignment. */
4325 value = isym->st_size;
4326 }
4327 else
4328 {
4329 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4330 if (sec == NULL)
4331 sec = bfd_abs_section_ptr;
4332 else if (discarded_section (sec))
4333 {
4334 /* Symbols from discarded section are undefined. We keep
4335 its visibility. */
4336 sec = bfd_und_section_ptr;
4337 discarded = TRUE;
4338 isym->st_shndx = SHN_UNDEF;
4339 }
4340 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4341 value -= sec->vma;
4342 }
4343
4344 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4345 isym->st_name);
4346 if (name == NULL)
4347 goto error_free_vers;
4348
4349 if (isym->st_shndx == SHN_COMMON
4350 && (abfd->flags & BFD_PLUGIN) != 0)
4351 {
4352 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4353
4354 if (xc == NULL)
4355 {
4356 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4357 | SEC_EXCLUDE);
4358 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4359 if (xc == NULL)
4360 goto error_free_vers;
4361 }
4362 sec = xc;
4363 }
4364 else if (isym->st_shndx == SHN_COMMON
4365 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4366 && !bfd_link_relocatable (info))
4367 {
4368 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4369
4370 if (tcomm == NULL)
4371 {
4372 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4373 | SEC_LINKER_CREATED);
4374 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4375 if (tcomm == NULL)
4376 goto error_free_vers;
4377 }
4378 sec = tcomm;
4379 }
4380 else if (bed->elf_add_symbol_hook)
4381 {
4382 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4383 &sec, &value))
4384 goto error_free_vers;
4385
4386 /* The hook function sets the name to NULL if this symbol
4387 should be skipped for some reason. */
4388 if (name == NULL)
4389 continue;
4390 }
4391
4392 /* Sanity check that all possibilities were handled. */
4393 if (sec == NULL)
4394 {
4395 bfd_set_error (bfd_error_bad_value);
4396 goto error_free_vers;
4397 }
4398
4399 /* Silently discard TLS symbols from --just-syms. There's
4400 no way to combine a static TLS block with a new TLS block
4401 for this executable. */
4402 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4403 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4404 continue;
4405
4406 if (bfd_is_und_section (sec)
4407 || bfd_is_com_section (sec))
4408 definition = FALSE;
4409 else
4410 definition = TRUE;
4411
4412 size_change_ok = FALSE;
4413 type_change_ok = bed->type_change_ok;
4414 old_weak = FALSE;
4415 matched = FALSE;
4416 old_alignment = 0;
4417 old_bfd = NULL;
4418 new_sec = sec;
4419
4420 if (is_elf_hash_table (htab))
4421 {
4422 Elf_Internal_Versym iver;
4423 unsigned int vernum = 0;
4424 bfd_boolean skip;
4425
4426 if (ever == NULL)
4427 {
4428 if (info->default_imported_symver)
4429 /* Use the default symbol version created earlier. */
4430 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4431 else
4432 iver.vs_vers = 0;
4433 }
4434 else
4435 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4436
4437 vernum = iver.vs_vers & VERSYM_VERSION;
4438
4439 /* If this is a hidden symbol, or if it is not version
4440 1, we append the version name to the symbol name.
4441 However, we do not modify a non-hidden absolute symbol
4442 if it is not a function, because it might be the version
4443 symbol itself. FIXME: What if it isn't? */
4444 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4445 || (vernum > 1
4446 && (!bfd_is_abs_section (sec)
4447 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4448 {
4449 const char *verstr;
4450 size_t namelen, verlen, newlen;
4451 char *newname, *p;
4452
4453 if (isym->st_shndx != SHN_UNDEF)
4454 {
4455 if (vernum > elf_tdata (abfd)->cverdefs)
4456 verstr = NULL;
4457 else if (vernum > 1)
4458 verstr =
4459 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4460 else
4461 verstr = "";
4462
4463 if (verstr == NULL)
4464 {
4465 _bfd_error_handler
4466 /* xgettext:c-format */
4467 (_("%B: %s: invalid version %u (max %d)"),
4468 abfd, name, vernum,
4469 elf_tdata (abfd)->cverdefs);
4470 bfd_set_error (bfd_error_bad_value);
4471 goto error_free_vers;
4472 }
4473 }
4474 else
4475 {
4476 /* We cannot simply test for the number of
4477 entries in the VERNEED section since the
4478 numbers for the needed versions do not start
4479 at 0. */
4480 Elf_Internal_Verneed *t;
4481
4482 verstr = NULL;
4483 for (t = elf_tdata (abfd)->verref;
4484 t != NULL;
4485 t = t->vn_nextref)
4486 {
4487 Elf_Internal_Vernaux *a;
4488
4489 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4490 {
4491 if (a->vna_other == vernum)
4492 {
4493 verstr = a->vna_nodename;
4494 break;
4495 }
4496 }
4497 if (a != NULL)
4498 break;
4499 }
4500 if (verstr == NULL)
4501 {
4502 _bfd_error_handler
4503 /* xgettext:c-format */
4504 (_("%B: %s: invalid needed version %d"),
4505 abfd, name, vernum);
4506 bfd_set_error (bfd_error_bad_value);
4507 goto error_free_vers;
4508 }
4509 }
4510
4511 namelen = strlen (name);
4512 verlen = strlen (verstr);
4513 newlen = namelen + verlen + 2;
4514 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4515 && isym->st_shndx != SHN_UNDEF)
4516 ++newlen;
4517
4518 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4519 if (newname == NULL)
4520 goto error_free_vers;
4521 memcpy (newname, name, namelen);
4522 p = newname + namelen;
4523 *p++ = ELF_VER_CHR;
4524 /* If this is a defined non-hidden version symbol,
4525 we add another @ to the name. This indicates the
4526 default version of the symbol. */
4527 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4528 && isym->st_shndx != SHN_UNDEF)
4529 *p++ = ELF_VER_CHR;
4530 memcpy (p, verstr, verlen + 1);
4531
4532 name = newname;
4533 }
4534
4535 /* If this symbol has default visibility and the user has
4536 requested we not re-export it, then mark it as hidden. */
4537 if (!bfd_is_und_section (sec)
4538 && !dynamic
4539 && abfd->no_export
4540 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4541 isym->st_other = (STV_HIDDEN
4542 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4543
4544 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4545 sym_hash, &old_bfd, &old_weak,
4546 &old_alignment, &skip, &override,
4547 &type_change_ok, &size_change_ok,
4548 &matched))
4549 goto error_free_vers;
4550
4551 if (skip)
4552 continue;
4553
4554 /* Override a definition only if the new symbol matches the
4555 existing one. */
4556 if (override && matched)
4557 definition = FALSE;
4558
4559 h = *sym_hash;
4560 while (h->root.type == bfd_link_hash_indirect
4561 || h->root.type == bfd_link_hash_warning)
4562 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4563
4564 if (elf_tdata (abfd)->verdef != NULL
4565 && vernum > 1
4566 && definition)
4567 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4568 }
4569
4570 if (! (_bfd_generic_link_add_one_symbol
4571 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4572 (struct bfd_link_hash_entry **) sym_hash)))
4573 goto error_free_vers;
4574
4575 if ((flags & BSF_GNU_UNIQUE)
4576 && (abfd->flags & DYNAMIC) == 0
4577 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4578 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique;
4579
4580 h = *sym_hash;
4581 /* We need to make sure that indirect symbol dynamic flags are
4582 updated. */
4583 hi = h;
4584 while (h->root.type == bfd_link_hash_indirect
4585 || h->root.type == bfd_link_hash_warning)
4586 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4587
4588 /* Setting the index to -3 tells elf_link_output_extsym that
4589 this symbol is defined in a discarded section. */
4590 if (discarded)
4591 h->indx = -3;
4592
4593 *sym_hash = h;
4594
4595 new_weak = (flags & BSF_WEAK) != 0;
4596 if (dynamic
4597 && definition
4598 && new_weak
4599 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4600 && is_elf_hash_table (htab)
4601 && h->u.alias == NULL)
4602 {
4603 /* Keep a list of all weak defined non function symbols from
4604 a dynamic object, using the alias field. Later in this
4605 function we will set the alias field to the correct
4606 value. We only put non-function symbols from dynamic
4607 objects on this list, because that happens to be the only
4608 time we need to know the normal symbol corresponding to a
4609 weak symbol, and the information is time consuming to
4610 figure out. If the alias field is not already NULL,
4611 then this symbol was already defined by some previous
4612 dynamic object, and we will be using that previous
4613 definition anyhow. */
4614
4615 h->u.alias = weaks;
4616 weaks = h;
4617 }
4618
4619 /* Set the alignment of a common symbol. */
4620 if ((common || bfd_is_com_section (sec))
4621 && h->root.type == bfd_link_hash_common)
4622 {
4623 unsigned int align;
4624
4625 if (common)
4626 align = bfd_log2 (isym->st_value);
4627 else
4628 {
4629 /* The new symbol is a common symbol in a shared object.
4630 We need to get the alignment from the section. */
4631 align = new_sec->alignment_power;
4632 }
4633 if (align > old_alignment)
4634 h->root.u.c.p->alignment_power = align;
4635 else
4636 h->root.u.c.p->alignment_power = old_alignment;
4637 }
4638
4639 if (is_elf_hash_table (htab))
4640 {
4641 /* Set a flag in the hash table entry indicating the type of
4642 reference or definition we just found. A dynamic symbol
4643 is one which is referenced or defined by both a regular
4644 object and a shared object. */
4645 bfd_boolean dynsym = FALSE;
4646
4647 /* Plugin symbols aren't normal. Don't set def_regular or
4648 ref_regular for them, or make them dynamic. */
4649 if ((abfd->flags & BFD_PLUGIN) != 0)
4650 ;
4651 else if (! dynamic)
4652 {
4653 if (! definition)
4654 {
4655 h->ref_regular = 1;
4656 if (bind != STB_WEAK)
4657 h->ref_regular_nonweak = 1;
4658 }
4659 else
4660 {
4661 h->def_regular = 1;
4662 if (h->def_dynamic)
4663 {
4664 h->def_dynamic = 0;
4665 h->ref_dynamic = 1;
4666 }
4667 }
4668
4669 /* If the indirect symbol has been forced local, don't
4670 make the real symbol dynamic. */
4671 if ((h == hi || !hi->forced_local)
4672 && (bfd_link_dll (info)
4673 || h->def_dynamic
4674 || h->ref_dynamic))
4675 dynsym = TRUE;
4676 }
4677 else
4678 {
4679 if (! definition)
4680 {
4681 h->ref_dynamic = 1;
4682 hi->ref_dynamic = 1;
4683 }
4684 else
4685 {
4686 h->def_dynamic = 1;
4687 hi->def_dynamic = 1;
4688 }
4689
4690 /* If the indirect symbol has been forced local, don't
4691 make the real symbol dynamic. */
4692 if ((h == hi || !hi->forced_local)
4693 && (h->def_regular
4694 || h->ref_regular
4695 || (h->is_weakalias
4696 && weakdef (h)->dynindx != -1)))
4697 dynsym = TRUE;
4698 }
4699
4700 /* Check to see if we need to add an indirect symbol for
4701 the default name. */
4702 if (definition
4703 || (!override && h->root.type == bfd_link_hash_common))
4704 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4705 sec, value, &old_bfd, &dynsym))
4706 goto error_free_vers;
4707
4708 /* Check the alignment when a common symbol is involved. This
4709 can change when a common symbol is overridden by a normal
4710 definition or a common symbol is ignored due to the old
4711 normal definition. We need to make sure the maximum
4712 alignment is maintained. */
4713 if ((old_alignment || common)
4714 && h->root.type != bfd_link_hash_common)
4715 {
4716 unsigned int common_align;
4717 unsigned int normal_align;
4718 unsigned int symbol_align;
4719 bfd *normal_bfd;
4720 bfd *common_bfd;
4721
4722 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4723 || h->root.type == bfd_link_hash_defweak);
4724
4725 symbol_align = ffs (h->root.u.def.value) - 1;
4726 if (h->root.u.def.section->owner != NULL
4727 && (h->root.u.def.section->owner->flags
4728 & (DYNAMIC | BFD_PLUGIN)) == 0)
4729 {
4730 normal_align = h->root.u.def.section->alignment_power;
4731 if (normal_align > symbol_align)
4732 normal_align = symbol_align;
4733 }
4734 else
4735 normal_align = symbol_align;
4736
4737 if (old_alignment)
4738 {
4739 common_align = old_alignment;
4740 common_bfd = old_bfd;
4741 normal_bfd = abfd;
4742 }
4743 else
4744 {
4745 common_align = bfd_log2 (isym->st_value);
4746 common_bfd = abfd;
4747 normal_bfd = old_bfd;
4748 }
4749
4750 if (normal_align < common_align)
4751 {
4752 /* PR binutils/2735 */
4753 if (normal_bfd == NULL)
4754 _bfd_error_handler
4755 /* xgettext:c-format */
4756 (_("Warning: alignment %u of common symbol `%s' in %B is"
4757 " greater than the alignment (%u) of its section %A"),
4758 1 << common_align, name, common_bfd,
4759 1 << normal_align, h->root.u.def.section);
4760 else
4761 _bfd_error_handler
4762 /* xgettext:c-format */
4763 (_("Warning: alignment %u of symbol `%s' in %B"
4764 " is smaller than %u in %B"),
4765 1 << normal_align, name, normal_bfd,
4766 1 << common_align, common_bfd);
4767 }
4768 }
4769
4770 /* Remember the symbol size if it isn't undefined. */
4771 if (isym->st_size != 0
4772 && isym->st_shndx != SHN_UNDEF
4773 && (definition || h->size == 0))
4774 {
4775 if (h->size != 0
4776 && h->size != isym->st_size
4777 && ! size_change_ok)
4778 _bfd_error_handler
4779 /* xgettext:c-format */
4780 (_("Warning: size of symbol `%s' changed"
4781 " from %Lu in %B to %Lu in %B"),
4782 name, h->size, old_bfd, isym->st_size, abfd);
4783
4784 h->size = isym->st_size;
4785 }
4786
4787 /* If this is a common symbol, then we always want H->SIZE
4788 to be the size of the common symbol. The code just above
4789 won't fix the size if a common symbol becomes larger. We
4790 don't warn about a size change here, because that is
4791 covered by --warn-common. Allow changes between different
4792 function types. */
4793 if (h->root.type == bfd_link_hash_common)
4794 h->size = h->root.u.c.size;
4795
4796 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4797 && ((definition && !new_weak)
4798 || (old_weak && h->root.type == bfd_link_hash_common)
4799 || h->type == STT_NOTYPE))
4800 {
4801 unsigned int type = ELF_ST_TYPE (isym->st_info);
4802
4803 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4804 symbol. */
4805 if (type == STT_GNU_IFUNC
4806 && (abfd->flags & DYNAMIC) != 0)
4807 type = STT_FUNC;
4808
4809 if (h->type != type)
4810 {
4811 if (h->type != STT_NOTYPE && ! type_change_ok)
4812 /* xgettext:c-format */
4813 _bfd_error_handler
4814 (_("Warning: type of symbol `%s' changed"
4815 " from %d to %d in %B"),
4816 name, h->type, type, abfd);
4817
4818 h->type = type;
4819 }
4820 }
4821
4822 /* Merge st_other field. */
4823 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4824
4825 /* We don't want to make debug symbol dynamic. */
4826 if (definition
4827 && (sec->flags & SEC_DEBUGGING)
4828 && !bfd_link_relocatable (info))
4829 dynsym = FALSE;
4830
4831 /* Nor should we make plugin symbols dynamic. */
4832 if ((abfd->flags & BFD_PLUGIN) != 0)
4833 dynsym = FALSE;
4834
4835 if (definition)
4836 {
4837 h->target_internal = isym->st_target_internal;
4838 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4839 }
4840
4841 if (definition && !dynamic)
4842 {
4843 char *p = strchr (name, ELF_VER_CHR);
4844 if (p != NULL && p[1] != ELF_VER_CHR)
4845 {
4846 /* Queue non-default versions so that .symver x, x@FOO
4847 aliases can be checked. */
4848 if (!nondeflt_vers)
4849 {
4850 amt = ((isymend - isym + 1)
4851 * sizeof (struct elf_link_hash_entry *));
4852 nondeflt_vers
4853 = (struct elf_link_hash_entry **) bfd_malloc (amt);
4854 if (!nondeflt_vers)
4855 goto error_free_vers;
4856 }
4857 nondeflt_vers[nondeflt_vers_cnt++] = h;
4858 }
4859 }
4860
4861 if (dynsym && h->dynindx == -1)
4862 {
4863 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4864 goto error_free_vers;
4865 if (h->is_weakalias
4866 && weakdef (h)->dynindx == -1)
4867 {
4868 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
4869 goto error_free_vers;
4870 }
4871 }
4872 else if (h->dynindx != -1)
4873 /* If the symbol already has a dynamic index, but
4874 visibility says it should not be visible, turn it into
4875 a local symbol. */
4876 switch (ELF_ST_VISIBILITY (h->other))
4877 {
4878 case STV_INTERNAL:
4879 case STV_HIDDEN:
4880 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4881 dynsym = FALSE;
4882 break;
4883 }
4884
4885 /* Don't add DT_NEEDED for references from the dummy bfd nor
4886 for unmatched symbol. */
4887 if (!add_needed
4888 && matched
4889 && definition
4890 && ((dynsym
4891 && h->ref_regular_nonweak
4892 && (old_bfd == NULL
4893 || (old_bfd->flags & BFD_PLUGIN) == 0))
4894 || (h->ref_dynamic_nonweak
4895 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4896 && !on_needed_list (elf_dt_name (abfd),
4897 htab->needed, NULL))))
4898 {
4899 int ret;
4900 const char *soname = elf_dt_name (abfd);
4901
4902 info->callbacks->minfo ("%!", soname, old_bfd,
4903 h->root.root.string);
4904
4905 /* A symbol from a library loaded via DT_NEEDED of some
4906 other library is referenced by a regular object.
4907 Add a DT_NEEDED entry for it. Issue an error if
4908 --no-add-needed is used and the reference was not
4909 a weak one. */
4910 if (old_bfd != NULL
4911 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4912 {
4913 _bfd_error_handler
4914 /* xgettext:c-format */
4915 (_("%B: undefined reference to symbol '%s'"),
4916 old_bfd, name);
4917 bfd_set_error (bfd_error_missing_dso);
4918 goto error_free_vers;
4919 }
4920
4921 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4922 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
4923
4924 add_needed = TRUE;
4925 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4926 if (ret < 0)
4927 goto error_free_vers;
4928
4929 BFD_ASSERT (ret == 0);
4930 }
4931 }
4932 }
4933
4934 if (extversym != NULL)
4935 {
4936 free (extversym);
4937 extversym = NULL;
4938 }
4939
4940 if (isymbuf != NULL)
4941 {
4942 free (isymbuf);
4943 isymbuf = NULL;
4944 }
4945
4946 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4947 {
4948 unsigned int i;
4949
4950 /* Restore the symbol table. */
4951 old_ent = (char *) old_tab + tabsize;
4952 memset (elf_sym_hashes (abfd), 0,
4953 extsymcount * sizeof (struct elf_link_hash_entry *));
4954 htab->root.table.table = old_table;
4955 htab->root.table.size = old_size;
4956 htab->root.table.count = old_count;
4957 memcpy (htab->root.table.table, old_tab, tabsize);
4958 htab->root.undefs = old_undefs;
4959 htab->root.undefs_tail = old_undefs_tail;
4960 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
4961 free (old_strtab);
4962 old_strtab = NULL;
4963 for (i = 0; i < htab->root.table.size; i++)
4964 {
4965 struct bfd_hash_entry *p;
4966 struct elf_link_hash_entry *h;
4967 bfd_size_type size;
4968 unsigned int alignment_power;
4969 unsigned int non_ir_ref_dynamic;
4970
4971 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4972 {
4973 h = (struct elf_link_hash_entry *) p;
4974 if (h->root.type == bfd_link_hash_warning)
4975 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4976
4977 /* Preserve the maximum alignment and size for common
4978 symbols even if this dynamic lib isn't on DT_NEEDED
4979 since it can still be loaded at run time by another
4980 dynamic lib. */
4981 if (h->root.type == bfd_link_hash_common)
4982 {
4983 size = h->root.u.c.size;
4984 alignment_power = h->root.u.c.p->alignment_power;
4985 }
4986 else
4987 {
4988 size = 0;
4989 alignment_power = 0;
4990 }
4991 /* Preserve non_ir_ref_dynamic so that this symbol
4992 will be exported when the dynamic lib becomes needed
4993 in the second pass. */
4994 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
4995 memcpy (p, old_ent, htab->root.table.entsize);
4996 old_ent = (char *) old_ent + htab->root.table.entsize;
4997 h = (struct elf_link_hash_entry *) p;
4998 if (h->root.type == bfd_link_hash_warning)
4999 {
5000 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5001 old_ent = (char *) old_ent + htab->root.table.entsize;
5002 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5003 }
5004 if (h->root.type == bfd_link_hash_common)
5005 {
5006 if (size > h->root.u.c.size)
5007 h->root.u.c.size = size;
5008 if (alignment_power > h->root.u.c.p->alignment_power)
5009 h->root.u.c.p->alignment_power = alignment_power;
5010 }
5011 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5012 }
5013 }
5014
5015 /* Make a special call to the linker "notice" function to
5016 tell it that symbols added for crefs may need to be removed. */
5017 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5018 goto error_free_vers;
5019
5020 free (old_tab);
5021 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5022 alloc_mark);
5023 if (nondeflt_vers != NULL)
5024 free (nondeflt_vers);
5025 return TRUE;
5026 }
5027
5028 if (old_tab != NULL)
5029 {
5030 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5031 goto error_free_vers;
5032 free (old_tab);
5033 old_tab = NULL;
5034 }
5035
5036 /* Now that all the symbols from this input file are created, if
5037 not performing a relocatable link, handle .symver foo, foo@BAR
5038 such that any relocs against foo become foo@BAR. */
5039 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5040 {
5041 size_t cnt, symidx;
5042
5043 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5044 {
5045 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5046 char *shortname, *p;
5047
5048 p = strchr (h->root.root.string, ELF_VER_CHR);
5049 if (p == NULL
5050 || (h->root.type != bfd_link_hash_defined
5051 && h->root.type != bfd_link_hash_defweak))
5052 continue;
5053
5054 amt = p - h->root.root.string;
5055 shortname = (char *) bfd_malloc (amt + 1);
5056 if (!shortname)
5057 goto error_free_vers;
5058 memcpy (shortname, h->root.root.string, amt);
5059 shortname[amt] = '\0';
5060
5061 hi = (struct elf_link_hash_entry *)
5062 bfd_link_hash_lookup (&htab->root, shortname,
5063 FALSE, FALSE, FALSE);
5064 if (hi != NULL
5065 && hi->root.type == h->root.type
5066 && hi->root.u.def.value == h->root.u.def.value
5067 && hi->root.u.def.section == h->root.u.def.section)
5068 {
5069 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5070 hi->root.type = bfd_link_hash_indirect;
5071 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5072 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5073 sym_hash = elf_sym_hashes (abfd);
5074 if (sym_hash)
5075 for (symidx = 0; symidx < extsymcount; ++symidx)
5076 if (sym_hash[symidx] == hi)
5077 {
5078 sym_hash[symidx] = h;
5079 break;
5080 }
5081 }
5082 free (shortname);
5083 }
5084 free (nondeflt_vers);
5085 nondeflt_vers = NULL;
5086 }
5087
5088 /* Now set the alias field correctly for all the weak defined
5089 symbols we found. The only way to do this is to search all the
5090 symbols. Since we only need the information for non functions in
5091 dynamic objects, that's the only time we actually put anything on
5092 the list WEAKS. We need this information so that if a regular
5093 object refers to a symbol defined weakly in a dynamic object, the
5094 real symbol in the dynamic object is also put in the dynamic
5095 symbols; we also must arrange for both symbols to point to the
5096 same memory location. We could handle the general case of symbol
5097 aliasing, but a general symbol alias can only be generated in
5098 assembler code, handling it correctly would be very time
5099 consuming, and other ELF linkers don't handle general aliasing
5100 either. */
5101 if (weaks != NULL)
5102 {
5103 struct elf_link_hash_entry **hpp;
5104 struct elf_link_hash_entry **hppend;
5105 struct elf_link_hash_entry **sorted_sym_hash;
5106 struct elf_link_hash_entry *h;
5107 size_t sym_count;
5108
5109 /* Since we have to search the whole symbol list for each weak
5110 defined symbol, search time for N weak defined symbols will be
5111 O(N^2). Binary search will cut it down to O(NlogN). */
5112 amt = extsymcount;
5113 amt *= sizeof (struct elf_link_hash_entry *);
5114 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
5115 if (sorted_sym_hash == NULL)
5116 goto error_return;
5117 sym_hash = sorted_sym_hash;
5118 hpp = elf_sym_hashes (abfd);
5119 hppend = hpp + extsymcount;
5120 sym_count = 0;
5121 for (; hpp < hppend; hpp++)
5122 {
5123 h = *hpp;
5124 if (h != NULL
5125 && h->root.type == bfd_link_hash_defined
5126 && !bed->is_function_type (h->type))
5127 {
5128 *sym_hash = h;
5129 sym_hash++;
5130 sym_count++;
5131 }
5132 }
5133
5134 qsort (sorted_sym_hash, sym_count,
5135 sizeof (struct elf_link_hash_entry *),
5136 elf_sort_symbol);
5137
5138 while (weaks != NULL)
5139 {
5140 struct elf_link_hash_entry *hlook;
5141 asection *slook;
5142 bfd_vma vlook;
5143 size_t i, j, idx = 0;
5144
5145 hlook = weaks;
5146 weaks = hlook->u.alias;
5147 hlook->u.alias = NULL;
5148
5149 if (hlook->root.type != bfd_link_hash_defined
5150 && hlook->root.type != bfd_link_hash_defweak)
5151 continue;
5152
5153 slook = hlook->root.u.def.section;
5154 vlook = hlook->root.u.def.value;
5155
5156 i = 0;
5157 j = sym_count;
5158 while (i != j)
5159 {
5160 bfd_signed_vma vdiff;
5161 idx = (i + j) / 2;
5162 h = sorted_sym_hash[idx];
5163 vdiff = vlook - h->root.u.def.value;
5164 if (vdiff < 0)
5165 j = idx;
5166 else if (vdiff > 0)
5167 i = idx + 1;
5168 else
5169 {
5170 int sdiff = slook->id - h->root.u.def.section->id;
5171 if (sdiff < 0)
5172 j = idx;
5173 else if (sdiff > 0)
5174 i = idx + 1;
5175 else
5176 break;
5177 }
5178 }
5179
5180 /* We didn't find a value/section match. */
5181 if (i == j)
5182 continue;
5183
5184 /* With multiple aliases, or when the weak symbol is already
5185 strongly defined, we have multiple matching symbols and
5186 the binary search above may land on any of them. Step
5187 one past the matching symbol(s). */
5188 while (++idx != j)
5189 {
5190 h = sorted_sym_hash[idx];
5191 if (h->root.u.def.section != slook
5192 || h->root.u.def.value != vlook)
5193 break;
5194 }
5195
5196 /* Now look back over the aliases. Since we sorted by size
5197 as well as value and section, we'll choose the one with
5198 the largest size. */
5199 while (idx-- != i)
5200 {
5201 h = sorted_sym_hash[idx];
5202
5203 /* Stop if value or section doesn't match. */
5204 if (h->root.u.def.section != slook
5205 || h->root.u.def.value != vlook)
5206 break;
5207 else if (h != hlook)
5208 {
5209 struct elf_link_hash_entry *t;
5210
5211 hlook->u.alias = h;
5212 hlook->is_weakalias = 1;
5213 t = h;
5214 if (t->u.alias != NULL)
5215 while (t->u.alias != h)
5216 t = t->u.alias;
5217 t->u.alias = hlook;
5218
5219 /* If the weak definition is in the list of dynamic
5220 symbols, make sure the real definition is put
5221 there as well. */
5222 if (hlook->dynindx != -1 && h->dynindx == -1)
5223 {
5224 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5225 {
5226 err_free_sym_hash:
5227 free (sorted_sym_hash);
5228 goto error_return;
5229 }
5230 }
5231
5232 /* If the real definition is in the list of dynamic
5233 symbols, make sure the weak definition is put
5234 there as well. If we don't do this, then the
5235 dynamic loader might not merge the entries for the
5236 real definition and the weak definition. */
5237 if (h->dynindx != -1 && hlook->dynindx == -1)
5238 {
5239 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5240 goto err_free_sym_hash;
5241 }
5242 break;
5243 }
5244 }
5245 }
5246
5247 free (sorted_sym_hash);
5248 }
5249
5250 if (bed->check_directives
5251 && !(*bed->check_directives) (abfd, info))
5252 return FALSE;
5253
5254 /* If this is a non-traditional link, try to optimize the handling
5255 of the .stab/.stabstr sections. */
5256 if (! dynamic
5257 && ! info->traditional_format
5258 && is_elf_hash_table (htab)
5259 && (info->strip != strip_all && info->strip != strip_debugger))
5260 {
5261 asection *stabstr;
5262
5263 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5264 if (stabstr != NULL)
5265 {
5266 bfd_size_type string_offset = 0;
5267 asection *stab;
5268
5269 for (stab = abfd->sections; stab; stab = stab->next)
5270 if (CONST_STRNEQ (stab->name, ".stab")
5271 && (!stab->name[5] ||
5272 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5273 && (stab->flags & SEC_MERGE) == 0
5274 && !bfd_is_abs_section (stab->output_section))
5275 {
5276 struct bfd_elf_section_data *secdata;
5277
5278 secdata = elf_section_data (stab);
5279 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5280 stabstr, &secdata->sec_info,
5281 &string_offset))
5282 goto error_return;
5283 if (secdata->sec_info)
5284 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5285 }
5286 }
5287 }
5288
5289 if (is_elf_hash_table (htab) && add_needed)
5290 {
5291 /* Add this bfd to the loaded list. */
5292 struct elf_link_loaded_list *n;
5293
5294 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5295 if (n == NULL)
5296 goto error_return;
5297 n->abfd = abfd;
5298 n->next = htab->loaded;
5299 htab->loaded = n;
5300 }
5301
5302 return TRUE;
5303
5304 error_free_vers:
5305 if (old_tab != NULL)
5306 free (old_tab);
5307 if (old_strtab != NULL)
5308 free (old_strtab);
5309 if (nondeflt_vers != NULL)
5310 free (nondeflt_vers);
5311 if (extversym != NULL)
5312 free (extversym);
5313 error_free_sym:
5314 if (isymbuf != NULL)
5315 free (isymbuf);
5316 error_return:
5317 return FALSE;
5318 }
5319
5320 /* Return the linker hash table entry of a symbol that might be
5321 satisfied by an archive symbol. Return -1 on error. */
5322
5323 struct elf_link_hash_entry *
5324 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5325 struct bfd_link_info *info,
5326 const char *name)
5327 {
5328 struct elf_link_hash_entry *h;
5329 char *p, *copy;
5330 size_t len, first;
5331
5332 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5333 if (h != NULL)
5334 return h;
5335
5336 /* If this is a default version (the name contains @@), look up the
5337 symbol again with only one `@' as well as without the version.
5338 The effect is that references to the symbol with and without the
5339 version will be matched by the default symbol in the archive. */
5340
5341 p = strchr (name, ELF_VER_CHR);
5342 if (p == NULL || p[1] != ELF_VER_CHR)
5343 return h;
5344
5345 /* First check with only one `@'. */
5346 len = strlen (name);
5347 copy = (char *) bfd_alloc (abfd, len);
5348 if (copy == NULL)
5349 return (struct elf_link_hash_entry *) 0 - 1;
5350
5351 first = p - name + 1;
5352 memcpy (copy, name, first);
5353 memcpy (copy + first, name + first + 1, len - first);
5354
5355 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5356 if (h == NULL)
5357 {
5358 /* We also need to check references to the symbol without the
5359 version. */
5360 copy[first - 1] = '\0';
5361 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5362 FALSE, FALSE, TRUE);
5363 }
5364
5365 bfd_release (abfd, copy);
5366 return h;
5367 }
5368
5369 /* Add symbols from an ELF archive file to the linker hash table. We
5370 don't use _bfd_generic_link_add_archive_symbols because we need to
5371 handle versioned symbols.
5372
5373 Fortunately, ELF archive handling is simpler than that done by
5374 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5375 oddities. In ELF, if we find a symbol in the archive map, and the
5376 symbol is currently undefined, we know that we must pull in that
5377 object file.
5378
5379 Unfortunately, we do have to make multiple passes over the symbol
5380 table until nothing further is resolved. */
5381
5382 static bfd_boolean
5383 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5384 {
5385 symindex c;
5386 unsigned char *included = NULL;
5387 carsym *symdefs;
5388 bfd_boolean loop;
5389 bfd_size_type amt;
5390 const struct elf_backend_data *bed;
5391 struct elf_link_hash_entry * (*archive_symbol_lookup)
5392 (bfd *, struct bfd_link_info *, const char *);
5393
5394 if (! bfd_has_map (abfd))
5395 {
5396 /* An empty archive is a special case. */
5397 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5398 return TRUE;
5399 bfd_set_error (bfd_error_no_armap);
5400 return FALSE;
5401 }
5402
5403 /* Keep track of all symbols we know to be already defined, and all
5404 files we know to be already included. This is to speed up the
5405 second and subsequent passes. */
5406 c = bfd_ardata (abfd)->symdef_count;
5407 if (c == 0)
5408 return TRUE;
5409 amt = c;
5410 amt *= sizeof (*included);
5411 included = (unsigned char *) bfd_zmalloc (amt);
5412 if (included == NULL)
5413 return FALSE;
5414
5415 symdefs = bfd_ardata (abfd)->symdefs;
5416 bed = get_elf_backend_data (abfd);
5417 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5418
5419 do
5420 {
5421 file_ptr last;
5422 symindex i;
5423 carsym *symdef;
5424 carsym *symdefend;
5425
5426 loop = FALSE;
5427 last = -1;
5428
5429 symdef = symdefs;
5430 symdefend = symdef + c;
5431 for (i = 0; symdef < symdefend; symdef++, i++)
5432 {
5433 struct elf_link_hash_entry *h;
5434 bfd *element;
5435 struct bfd_link_hash_entry *undefs_tail;
5436 symindex mark;
5437
5438 if (included[i])
5439 continue;
5440 if (symdef->file_offset == last)
5441 {
5442 included[i] = TRUE;
5443 continue;
5444 }
5445
5446 h = archive_symbol_lookup (abfd, info, symdef->name);
5447 if (h == (struct elf_link_hash_entry *) 0 - 1)
5448 goto error_return;
5449
5450 if (h == NULL)
5451 continue;
5452
5453 if (h->root.type == bfd_link_hash_common)
5454 {
5455 /* We currently have a common symbol. The archive map contains
5456 a reference to this symbol, so we may want to include it. We
5457 only want to include it however, if this archive element
5458 contains a definition of the symbol, not just another common
5459 declaration of it.
5460
5461 Unfortunately some archivers (including GNU ar) will put
5462 declarations of common symbols into their archive maps, as
5463 well as real definitions, so we cannot just go by the archive
5464 map alone. Instead we must read in the element's symbol
5465 table and check that to see what kind of symbol definition
5466 this is. */
5467 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5468 continue;
5469 }
5470 else if (h->root.type != bfd_link_hash_undefined)
5471 {
5472 if (h->root.type != bfd_link_hash_undefweak)
5473 /* Symbol must be defined. Don't check it again. */
5474 included[i] = TRUE;
5475 continue;
5476 }
5477
5478 /* We need to include this archive member. */
5479 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5480 if (element == NULL)
5481 goto error_return;
5482
5483 if (! bfd_check_format (element, bfd_object))
5484 goto error_return;
5485
5486 undefs_tail = info->hash->undefs_tail;
5487
5488 if (!(*info->callbacks
5489 ->add_archive_element) (info, element, symdef->name, &element))
5490 continue;
5491 if (!bfd_link_add_symbols (element, info))
5492 goto error_return;
5493
5494 /* If there are any new undefined symbols, we need to make
5495 another pass through the archive in order to see whether
5496 they can be defined. FIXME: This isn't perfect, because
5497 common symbols wind up on undefs_tail and because an
5498 undefined symbol which is defined later on in this pass
5499 does not require another pass. This isn't a bug, but it
5500 does make the code less efficient than it could be. */
5501 if (undefs_tail != info->hash->undefs_tail)
5502 loop = TRUE;
5503
5504 /* Look backward to mark all symbols from this object file
5505 which we have already seen in this pass. */
5506 mark = i;
5507 do
5508 {
5509 included[mark] = TRUE;
5510 if (mark == 0)
5511 break;
5512 --mark;
5513 }
5514 while (symdefs[mark].file_offset == symdef->file_offset);
5515
5516 /* We mark subsequent symbols from this object file as we go
5517 on through the loop. */
5518 last = symdef->file_offset;
5519 }
5520 }
5521 while (loop);
5522
5523 free (included);
5524
5525 return TRUE;
5526
5527 error_return:
5528 if (included != NULL)
5529 free (included);
5530 return FALSE;
5531 }
5532
5533 /* Given an ELF BFD, add symbols to the global hash table as
5534 appropriate. */
5535
5536 bfd_boolean
5537 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5538 {
5539 switch (bfd_get_format (abfd))
5540 {
5541 case bfd_object:
5542 return elf_link_add_object_symbols (abfd, info);
5543 case bfd_archive:
5544 return elf_link_add_archive_symbols (abfd, info);
5545 default:
5546 bfd_set_error (bfd_error_wrong_format);
5547 return FALSE;
5548 }
5549 }
5550 \f
5551 struct hash_codes_info
5552 {
5553 unsigned long *hashcodes;
5554 bfd_boolean error;
5555 };
5556
5557 /* This function will be called though elf_link_hash_traverse to store
5558 all hash value of the exported symbols in an array. */
5559
5560 static bfd_boolean
5561 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5562 {
5563 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5564 const char *name;
5565 unsigned long ha;
5566 char *alc = NULL;
5567
5568 /* Ignore indirect symbols. These are added by the versioning code. */
5569 if (h->dynindx == -1)
5570 return TRUE;
5571
5572 name = h->root.root.string;
5573 if (h->versioned >= versioned)
5574 {
5575 char *p = strchr (name, ELF_VER_CHR);
5576 if (p != NULL)
5577 {
5578 alc = (char *) bfd_malloc (p - name + 1);
5579 if (alc == NULL)
5580 {
5581 inf->error = TRUE;
5582 return FALSE;
5583 }
5584 memcpy (alc, name, p - name);
5585 alc[p - name] = '\0';
5586 name = alc;
5587 }
5588 }
5589
5590 /* Compute the hash value. */
5591 ha = bfd_elf_hash (name);
5592
5593 /* Store the found hash value in the array given as the argument. */
5594 *(inf->hashcodes)++ = ha;
5595
5596 /* And store it in the struct so that we can put it in the hash table
5597 later. */
5598 h->u.elf_hash_value = ha;
5599
5600 if (alc != NULL)
5601 free (alc);
5602
5603 return TRUE;
5604 }
5605
5606 struct collect_gnu_hash_codes
5607 {
5608 bfd *output_bfd;
5609 const struct elf_backend_data *bed;
5610 unsigned long int nsyms;
5611 unsigned long int maskbits;
5612 unsigned long int *hashcodes;
5613 unsigned long int *hashval;
5614 unsigned long int *indx;
5615 unsigned long int *counts;
5616 bfd_vma *bitmask;
5617 bfd_byte *contents;
5618 long int min_dynindx;
5619 unsigned long int bucketcount;
5620 unsigned long int symindx;
5621 long int local_indx;
5622 long int shift1, shift2;
5623 unsigned long int mask;
5624 bfd_boolean error;
5625 };
5626
5627 /* This function will be called though elf_link_hash_traverse to store
5628 all hash value of the exported symbols in an array. */
5629
5630 static bfd_boolean
5631 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5632 {
5633 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5634 const char *name;
5635 unsigned long ha;
5636 char *alc = NULL;
5637
5638 /* Ignore indirect symbols. These are added by the versioning code. */
5639 if (h->dynindx == -1)
5640 return TRUE;
5641
5642 /* Ignore also local symbols and undefined symbols. */
5643 if (! (*s->bed->elf_hash_symbol) (h))
5644 return TRUE;
5645
5646 name = h->root.root.string;
5647 if (h->versioned >= versioned)
5648 {
5649 char *p = strchr (name, ELF_VER_CHR);
5650 if (p != NULL)
5651 {
5652 alc = (char *) bfd_malloc (p - name + 1);
5653 if (alc == NULL)
5654 {
5655 s->error = TRUE;
5656 return FALSE;
5657 }
5658 memcpy (alc, name, p - name);
5659 alc[p - name] = '\0';
5660 name = alc;
5661 }
5662 }
5663
5664 /* Compute the hash value. */
5665 ha = bfd_elf_gnu_hash (name);
5666
5667 /* Store the found hash value in the array for compute_bucket_count,
5668 and also for .dynsym reordering purposes. */
5669 s->hashcodes[s->nsyms] = ha;
5670 s->hashval[h->dynindx] = ha;
5671 ++s->nsyms;
5672 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5673 s->min_dynindx = h->dynindx;
5674
5675 if (alc != NULL)
5676 free (alc);
5677
5678 return TRUE;
5679 }
5680
5681 /* This function will be called though elf_link_hash_traverse to do
5682 final dynaminc symbol renumbering. */
5683
5684 static bfd_boolean
5685 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5686 {
5687 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5688 unsigned long int bucket;
5689 unsigned long int val;
5690
5691 /* Ignore indirect symbols. */
5692 if (h->dynindx == -1)
5693 return TRUE;
5694
5695 /* Ignore also local symbols and undefined symbols. */
5696 if (! (*s->bed->elf_hash_symbol) (h))
5697 {
5698 if (h->dynindx >= s->min_dynindx)
5699 h->dynindx = s->local_indx++;
5700 return TRUE;
5701 }
5702
5703 bucket = s->hashval[h->dynindx] % s->bucketcount;
5704 val = (s->hashval[h->dynindx] >> s->shift1)
5705 & ((s->maskbits >> s->shift1) - 1);
5706 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5707 s->bitmask[val]
5708 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5709 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5710 if (s->counts[bucket] == 1)
5711 /* Last element terminates the chain. */
5712 val |= 1;
5713 bfd_put_32 (s->output_bfd, val,
5714 s->contents + (s->indx[bucket] - s->symindx) * 4);
5715 --s->counts[bucket];
5716 h->dynindx = s->indx[bucket]++;
5717 return TRUE;
5718 }
5719
5720 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5721
5722 bfd_boolean
5723 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5724 {
5725 return !(h->forced_local
5726 || h->root.type == bfd_link_hash_undefined
5727 || h->root.type == bfd_link_hash_undefweak
5728 || ((h->root.type == bfd_link_hash_defined
5729 || h->root.type == bfd_link_hash_defweak)
5730 && h->root.u.def.section->output_section == NULL));
5731 }
5732
5733 /* Array used to determine the number of hash table buckets to use
5734 based on the number of symbols there are. If there are fewer than
5735 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5736 fewer than 37 we use 17 buckets, and so forth. We never use more
5737 than 32771 buckets. */
5738
5739 static const size_t elf_buckets[] =
5740 {
5741 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5742 16411, 32771, 0
5743 };
5744
5745 /* Compute bucket count for hashing table. We do not use a static set
5746 of possible tables sizes anymore. Instead we determine for all
5747 possible reasonable sizes of the table the outcome (i.e., the
5748 number of collisions etc) and choose the best solution. The
5749 weighting functions are not too simple to allow the table to grow
5750 without bounds. Instead one of the weighting factors is the size.
5751 Therefore the result is always a good payoff between few collisions
5752 (= short chain lengths) and table size. */
5753 static size_t
5754 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5755 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5756 unsigned long int nsyms,
5757 int gnu_hash)
5758 {
5759 size_t best_size = 0;
5760 unsigned long int i;
5761
5762 /* We have a problem here. The following code to optimize the table
5763 size requires an integer type with more the 32 bits. If
5764 BFD_HOST_U_64_BIT is set we know about such a type. */
5765 #ifdef BFD_HOST_U_64_BIT
5766 if (info->optimize)
5767 {
5768 size_t minsize;
5769 size_t maxsize;
5770 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5771 bfd *dynobj = elf_hash_table (info)->dynobj;
5772 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5773 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5774 unsigned long int *counts;
5775 bfd_size_type amt;
5776 unsigned int no_improvement_count = 0;
5777
5778 /* Possible optimization parameters: if we have NSYMS symbols we say
5779 that the hashing table must at least have NSYMS/4 and at most
5780 2*NSYMS buckets. */
5781 minsize = nsyms / 4;
5782 if (minsize == 0)
5783 minsize = 1;
5784 best_size = maxsize = nsyms * 2;
5785 if (gnu_hash)
5786 {
5787 if (minsize < 2)
5788 minsize = 2;
5789 if ((best_size & 31) == 0)
5790 ++best_size;
5791 }
5792
5793 /* Create array where we count the collisions in. We must use bfd_malloc
5794 since the size could be large. */
5795 amt = maxsize;
5796 amt *= sizeof (unsigned long int);
5797 counts = (unsigned long int *) bfd_malloc (amt);
5798 if (counts == NULL)
5799 return 0;
5800
5801 /* Compute the "optimal" size for the hash table. The criteria is a
5802 minimal chain length. The minor criteria is (of course) the size
5803 of the table. */
5804 for (i = minsize; i < maxsize; ++i)
5805 {
5806 /* Walk through the array of hashcodes and count the collisions. */
5807 BFD_HOST_U_64_BIT max;
5808 unsigned long int j;
5809 unsigned long int fact;
5810
5811 if (gnu_hash && (i & 31) == 0)
5812 continue;
5813
5814 memset (counts, '\0', i * sizeof (unsigned long int));
5815
5816 /* Determine how often each hash bucket is used. */
5817 for (j = 0; j < nsyms; ++j)
5818 ++counts[hashcodes[j] % i];
5819
5820 /* For the weight function we need some information about the
5821 pagesize on the target. This is information need not be 100%
5822 accurate. Since this information is not available (so far) we
5823 define it here to a reasonable default value. If it is crucial
5824 to have a better value some day simply define this value. */
5825 # ifndef BFD_TARGET_PAGESIZE
5826 # define BFD_TARGET_PAGESIZE (4096)
5827 # endif
5828
5829 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5830 and the chains. */
5831 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5832
5833 # if 1
5834 /* Variant 1: optimize for short chains. We add the squares
5835 of all the chain lengths (which favors many small chain
5836 over a few long chains). */
5837 for (j = 0; j < i; ++j)
5838 max += counts[j] * counts[j];
5839
5840 /* This adds penalties for the overall size of the table. */
5841 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5842 max *= fact * fact;
5843 # else
5844 /* Variant 2: Optimize a lot more for small table. Here we
5845 also add squares of the size but we also add penalties for
5846 empty slots (the +1 term). */
5847 for (j = 0; j < i; ++j)
5848 max += (1 + counts[j]) * (1 + counts[j]);
5849
5850 /* The overall size of the table is considered, but not as
5851 strong as in variant 1, where it is squared. */
5852 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5853 max *= fact;
5854 # endif
5855
5856 /* Compare with current best results. */
5857 if (max < best_chlen)
5858 {
5859 best_chlen = max;
5860 best_size = i;
5861 no_improvement_count = 0;
5862 }
5863 /* PR 11843: Avoid futile long searches for the best bucket size
5864 when there are a large number of symbols. */
5865 else if (++no_improvement_count == 100)
5866 break;
5867 }
5868
5869 free (counts);
5870 }
5871 else
5872 #endif /* defined (BFD_HOST_U_64_BIT) */
5873 {
5874 /* This is the fallback solution if no 64bit type is available or if we
5875 are not supposed to spend much time on optimizations. We select the
5876 bucket count using a fixed set of numbers. */
5877 for (i = 0; elf_buckets[i] != 0; i++)
5878 {
5879 best_size = elf_buckets[i];
5880 if (nsyms < elf_buckets[i + 1])
5881 break;
5882 }
5883 if (gnu_hash && best_size < 2)
5884 best_size = 2;
5885 }
5886
5887 return best_size;
5888 }
5889
5890 /* Size any SHT_GROUP section for ld -r. */
5891
5892 bfd_boolean
5893 _bfd_elf_size_group_sections (struct bfd_link_info *info)
5894 {
5895 bfd *ibfd;
5896 asection *s;
5897
5898 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
5899 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5900 && (s = ibfd->sections) != NULL
5901 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
5902 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5903 return FALSE;
5904 return TRUE;
5905 }
5906
5907 /* Set a default stack segment size. The value in INFO wins. If it
5908 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5909 undefined it is initialized. */
5910
5911 bfd_boolean
5912 bfd_elf_stack_segment_size (bfd *output_bfd,
5913 struct bfd_link_info *info,
5914 const char *legacy_symbol,
5915 bfd_vma default_size)
5916 {
5917 struct elf_link_hash_entry *h = NULL;
5918
5919 /* Look for legacy symbol. */
5920 if (legacy_symbol)
5921 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5922 FALSE, FALSE, FALSE);
5923 if (h && (h->root.type == bfd_link_hash_defined
5924 || h->root.type == bfd_link_hash_defweak)
5925 && h->def_regular
5926 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5927 {
5928 /* The symbol has no type if specified on the command line. */
5929 h->type = STT_OBJECT;
5930 if (info->stacksize)
5931 /* xgettext:c-format */
5932 _bfd_error_handler (_("%B: stack size specified and %s set"),
5933 output_bfd, legacy_symbol);
5934 else if (h->root.u.def.section != bfd_abs_section_ptr)
5935 /* xgettext:c-format */
5936 _bfd_error_handler (_("%B: %s not absolute"),
5937 output_bfd, legacy_symbol);
5938 else
5939 info->stacksize = h->root.u.def.value;
5940 }
5941
5942 if (!info->stacksize)
5943 /* If the user didn't set a size, or explicitly inhibit the
5944 size, set it now. */
5945 info->stacksize = default_size;
5946
5947 /* Provide the legacy symbol, if it is referenced. */
5948 if (h && (h->root.type == bfd_link_hash_undefined
5949 || h->root.type == bfd_link_hash_undefweak))
5950 {
5951 struct bfd_link_hash_entry *bh = NULL;
5952
5953 if (!(_bfd_generic_link_add_one_symbol
5954 (info, output_bfd, legacy_symbol,
5955 BSF_GLOBAL, bfd_abs_section_ptr,
5956 info->stacksize >= 0 ? info->stacksize : 0,
5957 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5958 return FALSE;
5959
5960 h = (struct elf_link_hash_entry *) bh;
5961 h->def_regular = 1;
5962 h->type = STT_OBJECT;
5963 }
5964
5965 return TRUE;
5966 }
5967
5968 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
5969
5970 struct elf_gc_sweep_symbol_info
5971 {
5972 struct bfd_link_info *info;
5973 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
5974 bfd_boolean);
5975 };
5976
5977 static bfd_boolean
5978 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
5979 {
5980 if (!h->mark
5981 && (((h->root.type == bfd_link_hash_defined
5982 || h->root.type == bfd_link_hash_defweak)
5983 && !((h->def_regular || ELF_COMMON_DEF_P (h))
5984 && h->root.u.def.section->gc_mark))
5985 || h->root.type == bfd_link_hash_undefined
5986 || h->root.type == bfd_link_hash_undefweak))
5987 {
5988 struct elf_gc_sweep_symbol_info *inf;
5989
5990 inf = (struct elf_gc_sweep_symbol_info *) data;
5991 (*inf->hide_symbol) (inf->info, h, TRUE);
5992 h->def_regular = 0;
5993 h->ref_regular = 0;
5994 h->ref_regular_nonweak = 0;
5995 }
5996
5997 return TRUE;
5998 }
5999
6000 /* Set up the sizes and contents of the ELF dynamic sections. This is
6001 called by the ELF linker emulation before_allocation routine. We
6002 must set the sizes of the sections before the linker sets the
6003 addresses of the various sections. */
6004
6005 bfd_boolean
6006 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6007 const char *soname,
6008 const char *rpath,
6009 const char *filter_shlib,
6010 const char *audit,
6011 const char *depaudit,
6012 const char * const *auxiliary_filters,
6013 struct bfd_link_info *info,
6014 asection **sinterpptr)
6015 {
6016 bfd *dynobj;
6017 const struct elf_backend_data *bed;
6018
6019 *sinterpptr = NULL;
6020
6021 if (!is_elf_hash_table (info->hash))
6022 return TRUE;
6023
6024 dynobj = elf_hash_table (info)->dynobj;
6025
6026 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6027 {
6028 struct bfd_elf_version_tree *verdefs;
6029 struct elf_info_failed asvinfo;
6030 struct bfd_elf_version_tree *t;
6031 struct bfd_elf_version_expr *d;
6032 asection *s;
6033 size_t soname_indx;
6034
6035 /* If we are supposed to export all symbols into the dynamic symbol
6036 table (this is not the normal case), then do so. */
6037 if (info->export_dynamic
6038 || (bfd_link_executable (info) && info->dynamic))
6039 {
6040 struct elf_info_failed eif;
6041
6042 eif.info = info;
6043 eif.failed = FALSE;
6044 elf_link_hash_traverse (elf_hash_table (info),
6045 _bfd_elf_export_symbol,
6046 &eif);
6047 if (eif.failed)
6048 return FALSE;
6049 }
6050
6051 if (soname != NULL)
6052 {
6053 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6054 soname, TRUE);
6055 if (soname_indx == (size_t) -1
6056 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6057 return FALSE;
6058 }
6059 else
6060 soname_indx = (size_t) -1;
6061
6062 /* Make all global versions with definition. */
6063 for (t = info->version_info; t != NULL; t = t->next)
6064 for (d = t->globals.list; d != NULL; d = d->next)
6065 if (!d->symver && d->literal)
6066 {
6067 const char *verstr, *name;
6068 size_t namelen, verlen, newlen;
6069 char *newname, *p, leading_char;
6070 struct elf_link_hash_entry *newh;
6071
6072 leading_char = bfd_get_symbol_leading_char (output_bfd);
6073 name = d->pattern;
6074 namelen = strlen (name) + (leading_char != '\0');
6075 verstr = t->name;
6076 verlen = strlen (verstr);
6077 newlen = namelen + verlen + 3;
6078
6079 newname = (char *) bfd_malloc (newlen);
6080 if (newname == NULL)
6081 return FALSE;
6082 newname[0] = leading_char;
6083 memcpy (newname + (leading_char != '\0'), name, namelen);
6084
6085 /* Check the hidden versioned definition. */
6086 p = newname + namelen;
6087 *p++ = ELF_VER_CHR;
6088 memcpy (p, verstr, verlen + 1);
6089 newh = elf_link_hash_lookup (elf_hash_table (info),
6090 newname, FALSE, FALSE,
6091 FALSE);
6092 if (newh == NULL
6093 || (newh->root.type != bfd_link_hash_defined
6094 && newh->root.type != bfd_link_hash_defweak))
6095 {
6096 /* Check the default versioned definition. */
6097 *p++ = ELF_VER_CHR;
6098 memcpy (p, verstr, verlen + 1);
6099 newh = elf_link_hash_lookup (elf_hash_table (info),
6100 newname, FALSE, FALSE,
6101 FALSE);
6102 }
6103 free (newname);
6104
6105 /* Mark this version if there is a definition and it is
6106 not defined in a shared object. */
6107 if (newh != NULL
6108 && !newh->def_dynamic
6109 && (newh->root.type == bfd_link_hash_defined
6110 || newh->root.type == bfd_link_hash_defweak))
6111 d->symver = 1;
6112 }
6113
6114 /* Attach all the symbols to their version information. */
6115 asvinfo.info = info;
6116 asvinfo.failed = FALSE;
6117
6118 elf_link_hash_traverse (elf_hash_table (info),
6119 _bfd_elf_link_assign_sym_version,
6120 &asvinfo);
6121 if (asvinfo.failed)
6122 return FALSE;
6123
6124 if (!info->allow_undefined_version)
6125 {
6126 /* Check if all global versions have a definition. */
6127 bfd_boolean all_defined = TRUE;
6128 for (t = info->version_info; t != NULL; t = t->next)
6129 for (d = t->globals.list; d != NULL; d = d->next)
6130 if (d->literal && !d->symver && !d->script)
6131 {
6132 _bfd_error_handler
6133 (_("%s: undefined version: %s"),
6134 d->pattern, t->name);
6135 all_defined = FALSE;
6136 }
6137
6138 if (!all_defined)
6139 {
6140 bfd_set_error (bfd_error_bad_value);
6141 return FALSE;
6142 }
6143 }
6144
6145 /* Set up the version definition section. */
6146 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6147 BFD_ASSERT (s != NULL);
6148
6149 /* We may have created additional version definitions if we are
6150 just linking a regular application. */
6151 verdefs = info->version_info;
6152
6153 /* Skip anonymous version tag. */
6154 if (verdefs != NULL && verdefs->vernum == 0)
6155 verdefs = verdefs->next;
6156
6157 if (verdefs == NULL && !info->create_default_symver)
6158 s->flags |= SEC_EXCLUDE;
6159 else
6160 {
6161 unsigned int cdefs;
6162 bfd_size_type size;
6163 bfd_byte *p;
6164 Elf_Internal_Verdef def;
6165 Elf_Internal_Verdaux defaux;
6166 struct bfd_link_hash_entry *bh;
6167 struct elf_link_hash_entry *h;
6168 const char *name;
6169
6170 cdefs = 0;
6171 size = 0;
6172
6173 /* Make space for the base version. */
6174 size += sizeof (Elf_External_Verdef);
6175 size += sizeof (Elf_External_Verdaux);
6176 ++cdefs;
6177
6178 /* Make space for the default version. */
6179 if (info->create_default_symver)
6180 {
6181 size += sizeof (Elf_External_Verdef);
6182 ++cdefs;
6183 }
6184
6185 for (t = verdefs; t != NULL; t = t->next)
6186 {
6187 struct bfd_elf_version_deps *n;
6188
6189 /* Don't emit base version twice. */
6190 if (t->vernum == 0)
6191 continue;
6192
6193 size += sizeof (Elf_External_Verdef);
6194 size += sizeof (Elf_External_Verdaux);
6195 ++cdefs;
6196
6197 for (n = t->deps; n != NULL; n = n->next)
6198 size += sizeof (Elf_External_Verdaux);
6199 }
6200
6201 s->size = size;
6202 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6203 if (s->contents == NULL && s->size != 0)
6204 return FALSE;
6205
6206 /* Fill in the version definition section. */
6207
6208 p = s->contents;
6209
6210 def.vd_version = VER_DEF_CURRENT;
6211 def.vd_flags = VER_FLG_BASE;
6212 def.vd_ndx = 1;
6213 def.vd_cnt = 1;
6214 if (info->create_default_symver)
6215 {
6216 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6217 def.vd_next = sizeof (Elf_External_Verdef);
6218 }
6219 else
6220 {
6221 def.vd_aux = sizeof (Elf_External_Verdef);
6222 def.vd_next = (sizeof (Elf_External_Verdef)
6223 + sizeof (Elf_External_Verdaux));
6224 }
6225
6226 if (soname_indx != (size_t) -1)
6227 {
6228 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6229 soname_indx);
6230 def.vd_hash = bfd_elf_hash (soname);
6231 defaux.vda_name = soname_indx;
6232 name = soname;
6233 }
6234 else
6235 {
6236 size_t indx;
6237
6238 name = lbasename (output_bfd->filename);
6239 def.vd_hash = bfd_elf_hash (name);
6240 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6241 name, FALSE);
6242 if (indx == (size_t) -1)
6243 return FALSE;
6244 defaux.vda_name = indx;
6245 }
6246 defaux.vda_next = 0;
6247
6248 _bfd_elf_swap_verdef_out (output_bfd, &def,
6249 (Elf_External_Verdef *) p);
6250 p += sizeof (Elf_External_Verdef);
6251 if (info->create_default_symver)
6252 {
6253 /* Add a symbol representing this version. */
6254 bh = NULL;
6255 if (! (_bfd_generic_link_add_one_symbol
6256 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6257 0, NULL, FALSE,
6258 get_elf_backend_data (dynobj)->collect, &bh)))
6259 return FALSE;
6260 h = (struct elf_link_hash_entry *) bh;
6261 h->non_elf = 0;
6262 h->def_regular = 1;
6263 h->type = STT_OBJECT;
6264 h->verinfo.vertree = NULL;
6265
6266 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6267 return FALSE;
6268
6269 /* Create a duplicate of the base version with the same
6270 aux block, but different flags. */
6271 def.vd_flags = 0;
6272 def.vd_ndx = 2;
6273 def.vd_aux = sizeof (Elf_External_Verdef);
6274 if (verdefs)
6275 def.vd_next = (sizeof (Elf_External_Verdef)
6276 + sizeof (Elf_External_Verdaux));
6277 else
6278 def.vd_next = 0;
6279 _bfd_elf_swap_verdef_out (output_bfd, &def,
6280 (Elf_External_Verdef *) p);
6281 p += sizeof (Elf_External_Verdef);
6282 }
6283 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6284 (Elf_External_Verdaux *) p);
6285 p += sizeof (Elf_External_Verdaux);
6286
6287 for (t = verdefs; t != NULL; t = t->next)
6288 {
6289 unsigned int cdeps;
6290 struct bfd_elf_version_deps *n;
6291
6292 /* Don't emit the base version twice. */
6293 if (t->vernum == 0)
6294 continue;
6295
6296 cdeps = 0;
6297 for (n = t->deps; n != NULL; n = n->next)
6298 ++cdeps;
6299
6300 /* Add a symbol representing this version. */
6301 bh = NULL;
6302 if (! (_bfd_generic_link_add_one_symbol
6303 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6304 0, NULL, FALSE,
6305 get_elf_backend_data (dynobj)->collect, &bh)))
6306 return FALSE;
6307 h = (struct elf_link_hash_entry *) bh;
6308 h->non_elf = 0;
6309 h->def_regular = 1;
6310 h->type = STT_OBJECT;
6311 h->verinfo.vertree = t;
6312
6313 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6314 return FALSE;
6315
6316 def.vd_version = VER_DEF_CURRENT;
6317 def.vd_flags = 0;
6318 if (t->globals.list == NULL
6319 && t->locals.list == NULL
6320 && ! t->used)
6321 def.vd_flags |= VER_FLG_WEAK;
6322 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6323 def.vd_cnt = cdeps + 1;
6324 def.vd_hash = bfd_elf_hash (t->name);
6325 def.vd_aux = sizeof (Elf_External_Verdef);
6326 def.vd_next = 0;
6327
6328 /* If a basever node is next, it *must* be the last node in
6329 the chain, otherwise Verdef construction breaks. */
6330 if (t->next != NULL && t->next->vernum == 0)
6331 BFD_ASSERT (t->next->next == NULL);
6332
6333 if (t->next != NULL && t->next->vernum != 0)
6334 def.vd_next = (sizeof (Elf_External_Verdef)
6335 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6336
6337 _bfd_elf_swap_verdef_out (output_bfd, &def,
6338 (Elf_External_Verdef *) p);
6339 p += sizeof (Elf_External_Verdef);
6340
6341 defaux.vda_name = h->dynstr_index;
6342 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6343 h->dynstr_index);
6344 defaux.vda_next = 0;
6345 if (t->deps != NULL)
6346 defaux.vda_next = sizeof (Elf_External_Verdaux);
6347 t->name_indx = defaux.vda_name;
6348
6349 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6350 (Elf_External_Verdaux *) p);
6351 p += sizeof (Elf_External_Verdaux);
6352
6353 for (n = t->deps; n != NULL; n = n->next)
6354 {
6355 if (n->version_needed == NULL)
6356 {
6357 /* This can happen if there was an error in the
6358 version script. */
6359 defaux.vda_name = 0;
6360 }
6361 else
6362 {
6363 defaux.vda_name = n->version_needed->name_indx;
6364 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6365 defaux.vda_name);
6366 }
6367 if (n->next == NULL)
6368 defaux.vda_next = 0;
6369 else
6370 defaux.vda_next = sizeof (Elf_External_Verdaux);
6371
6372 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6373 (Elf_External_Verdaux *) p);
6374 p += sizeof (Elf_External_Verdaux);
6375 }
6376 }
6377
6378 elf_tdata (output_bfd)->cverdefs = cdefs;
6379 }
6380 }
6381
6382 bed = get_elf_backend_data (output_bfd);
6383
6384 if (info->gc_sections && bed->can_gc_sections)
6385 {
6386 struct elf_gc_sweep_symbol_info sweep_info;
6387
6388 /* Remove the symbols that were in the swept sections from the
6389 dynamic symbol table. */
6390 sweep_info.info = info;
6391 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6392 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6393 &sweep_info);
6394 }
6395
6396 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6397 {
6398 asection *s;
6399 struct elf_find_verdep_info sinfo;
6400
6401 /* Work out the size of the version reference section. */
6402
6403 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6404 BFD_ASSERT (s != NULL);
6405
6406 sinfo.info = info;
6407 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6408 if (sinfo.vers == 0)
6409 sinfo.vers = 1;
6410 sinfo.failed = FALSE;
6411
6412 elf_link_hash_traverse (elf_hash_table (info),
6413 _bfd_elf_link_find_version_dependencies,
6414 &sinfo);
6415 if (sinfo.failed)
6416 return FALSE;
6417
6418 if (elf_tdata (output_bfd)->verref == NULL)
6419 s->flags |= SEC_EXCLUDE;
6420 else
6421 {
6422 Elf_Internal_Verneed *vn;
6423 unsigned int size;
6424 unsigned int crefs;
6425 bfd_byte *p;
6426
6427 /* Build the version dependency section. */
6428 size = 0;
6429 crefs = 0;
6430 for (vn = elf_tdata (output_bfd)->verref;
6431 vn != NULL;
6432 vn = vn->vn_nextref)
6433 {
6434 Elf_Internal_Vernaux *a;
6435
6436 size += sizeof (Elf_External_Verneed);
6437 ++crefs;
6438 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6439 size += sizeof (Elf_External_Vernaux);
6440 }
6441
6442 s->size = size;
6443 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6444 if (s->contents == NULL)
6445 return FALSE;
6446
6447 p = s->contents;
6448 for (vn = elf_tdata (output_bfd)->verref;
6449 vn != NULL;
6450 vn = vn->vn_nextref)
6451 {
6452 unsigned int caux;
6453 Elf_Internal_Vernaux *a;
6454 size_t indx;
6455
6456 caux = 0;
6457 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6458 ++caux;
6459
6460 vn->vn_version = VER_NEED_CURRENT;
6461 vn->vn_cnt = caux;
6462 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6463 elf_dt_name (vn->vn_bfd) != NULL
6464 ? elf_dt_name (vn->vn_bfd)
6465 : lbasename (vn->vn_bfd->filename),
6466 FALSE);
6467 if (indx == (size_t) -1)
6468 return FALSE;
6469 vn->vn_file = indx;
6470 vn->vn_aux = sizeof (Elf_External_Verneed);
6471 if (vn->vn_nextref == NULL)
6472 vn->vn_next = 0;
6473 else
6474 vn->vn_next = (sizeof (Elf_External_Verneed)
6475 + caux * sizeof (Elf_External_Vernaux));
6476
6477 _bfd_elf_swap_verneed_out (output_bfd, vn,
6478 (Elf_External_Verneed *) p);
6479 p += sizeof (Elf_External_Verneed);
6480
6481 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6482 {
6483 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6484 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6485 a->vna_nodename, FALSE);
6486 if (indx == (size_t) -1)
6487 return FALSE;
6488 a->vna_name = indx;
6489 if (a->vna_nextptr == NULL)
6490 a->vna_next = 0;
6491 else
6492 a->vna_next = sizeof (Elf_External_Vernaux);
6493
6494 _bfd_elf_swap_vernaux_out (output_bfd, a,
6495 (Elf_External_Vernaux *) p);
6496 p += sizeof (Elf_External_Vernaux);
6497 }
6498 }
6499
6500 elf_tdata (output_bfd)->cverrefs = crefs;
6501 }
6502 }
6503
6504 /* Any syms created from now on start with -1 in
6505 got.refcount/offset and plt.refcount/offset. */
6506 elf_hash_table (info)->init_got_refcount
6507 = elf_hash_table (info)->init_got_offset;
6508 elf_hash_table (info)->init_plt_refcount
6509 = elf_hash_table (info)->init_plt_offset;
6510
6511 if (bfd_link_relocatable (info)
6512 && !_bfd_elf_size_group_sections (info))
6513 return FALSE;
6514
6515 /* The backend may have to create some sections regardless of whether
6516 we're dynamic or not. */
6517 if (bed->elf_backend_always_size_sections
6518 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6519 return FALSE;
6520
6521 /* Determine any GNU_STACK segment requirements, after the backend
6522 has had a chance to set a default segment size. */
6523 if (info->execstack)
6524 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6525 else if (info->noexecstack)
6526 elf_stack_flags (output_bfd) = PF_R | PF_W;
6527 else
6528 {
6529 bfd *inputobj;
6530 asection *notesec = NULL;
6531 int exec = 0;
6532
6533 for (inputobj = info->input_bfds;
6534 inputobj;
6535 inputobj = inputobj->link.next)
6536 {
6537 asection *s;
6538
6539 if (inputobj->flags
6540 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6541 continue;
6542 s = inputobj->sections;
6543 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6544 continue;
6545
6546 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6547 if (s)
6548 {
6549 if (s->flags & SEC_CODE)
6550 exec = PF_X;
6551 notesec = s;
6552 }
6553 else if (bed->default_execstack)
6554 exec = PF_X;
6555 }
6556 if (notesec || info->stacksize > 0)
6557 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6558 if (notesec && exec && bfd_link_relocatable (info)
6559 && notesec->output_section != bfd_abs_section_ptr)
6560 notesec->output_section->flags |= SEC_CODE;
6561 }
6562
6563 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6564 {
6565 struct elf_info_failed eif;
6566 struct elf_link_hash_entry *h;
6567 asection *dynstr;
6568 asection *s;
6569
6570 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6571 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6572
6573 if (info->symbolic)
6574 {
6575 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6576 return FALSE;
6577 info->flags |= DF_SYMBOLIC;
6578 }
6579
6580 if (rpath != NULL)
6581 {
6582 size_t indx;
6583 bfd_vma tag;
6584
6585 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6586 TRUE);
6587 if (indx == (size_t) -1)
6588 return FALSE;
6589
6590 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6591 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6592 return FALSE;
6593 }
6594
6595 if (filter_shlib != NULL)
6596 {
6597 size_t indx;
6598
6599 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6600 filter_shlib, TRUE);
6601 if (indx == (size_t) -1
6602 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6603 return FALSE;
6604 }
6605
6606 if (auxiliary_filters != NULL)
6607 {
6608 const char * const *p;
6609
6610 for (p = auxiliary_filters; *p != NULL; p++)
6611 {
6612 size_t indx;
6613
6614 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6615 *p, TRUE);
6616 if (indx == (size_t) -1
6617 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6618 return FALSE;
6619 }
6620 }
6621
6622 if (audit != NULL)
6623 {
6624 size_t indx;
6625
6626 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6627 TRUE);
6628 if (indx == (size_t) -1
6629 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6630 return FALSE;
6631 }
6632
6633 if (depaudit != NULL)
6634 {
6635 size_t indx;
6636
6637 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6638 TRUE);
6639 if (indx == (size_t) -1
6640 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6641 return FALSE;
6642 }
6643
6644 eif.info = info;
6645 eif.failed = FALSE;
6646
6647 /* Find all symbols which were defined in a dynamic object and make
6648 the backend pick a reasonable value for them. */
6649 elf_link_hash_traverse (elf_hash_table (info),
6650 _bfd_elf_adjust_dynamic_symbol,
6651 &eif);
6652 if (eif.failed)
6653 return FALSE;
6654
6655 /* Add some entries to the .dynamic section. We fill in some of the
6656 values later, in bfd_elf_final_link, but we must add the entries
6657 now so that we know the final size of the .dynamic section. */
6658
6659 /* If there are initialization and/or finalization functions to
6660 call then add the corresponding DT_INIT/DT_FINI entries. */
6661 h = (info->init_function
6662 ? elf_link_hash_lookup (elf_hash_table (info),
6663 info->init_function, FALSE,
6664 FALSE, FALSE)
6665 : NULL);
6666 if (h != NULL
6667 && (h->ref_regular
6668 || h->def_regular))
6669 {
6670 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6671 return FALSE;
6672 }
6673 h = (info->fini_function
6674 ? elf_link_hash_lookup (elf_hash_table (info),
6675 info->fini_function, FALSE,
6676 FALSE, FALSE)
6677 : NULL);
6678 if (h != NULL
6679 && (h->ref_regular
6680 || h->def_regular))
6681 {
6682 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6683 return FALSE;
6684 }
6685
6686 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6687 if (s != NULL && s->linker_has_input)
6688 {
6689 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6690 if (! bfd_link_executable (info))
6691 {
6692 bfd *sub;
6693 asection *o;
6694
6695 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6696 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
6697 && (o = sub->sections) != NULL
6698 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
6699 for (o = sub->sections; o != NULL; o = o->next)
6700 if (elf_section_data (o)->this_hdr.sh_type
6701 == SHT_PREINIT_ARRAY)
6702 {
6703 _bfd_error_handler
6704 (_("%B: .preinit_array section is not allowed in DSO"),
6705 sub);
6706 break;
6707 }
6708
6709 bfd_set_error (bfd_error_nonrepresentable_section);
6710 return FALSE;
6711 }
6712
6713 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6714 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6715 return FALSE;
6716 }
6717 s = bfd_get_section_by_name (output_bfd, ".init_array");
6718 if (s != NULL && s->linker_has_input)
6719 {
6720 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6721 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6722 return FALSE;
6723 }
6724 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6725 if (s != NULL && s->linker_has_input)
6726 {
6727 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6728 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6729 return FALSE;
6730 }
6731
6732 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6733 /* If .dynstr is excluded from the link, we don't want any of
6734 these tags. Strictly, we should be checking each section
6735 individually; This quick check covers for the case where
6736 someone does a /DISCARD/ : { *(*) }. */
6737 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6738 {
6739 bfd_size_type strsize;
6740
6741 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6742 if ((info->emit_hash
6743 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6744 || (info->emit_gnu_hash
6745 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6746 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6747 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6748 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6749 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6750 bed->s->sizeof_sym))
6751 return FALSE;
6752 }
6753 }
6754
6755 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6756 return FALSE;
6757
6758 /* The backend must work out the sizes of all the other dynamic
6759 sections. */
6760 if (dynobj != NULL
6761 && bed->elf_backend_size_dynamic_sections != NULL
6762 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6763 return FALSE;
6764
6765 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6766 {
6767 unsigned long section_sym_count;
6768
6769 if (elf_tdata (output_bfd)->cverdefs)
6770 {
6771 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
6772
6773 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6774 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
6775 return FALSE;
6776 }
6777
6778 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6779 {
6780 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6781 return FALSE;
6782 }
6783 else if (info->flags & DF_BIND_NOW)
6784 {
6785 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6786 return FALSE;
6787 }
6788
6789 if (info->flags_1)
6790 {
6791 if (bfd_link_executable (info))
6792 info->flags_1 &= ~ (DF_1_INITFIRST
6793 | DF_1_NODELETE
6794 | DF_1_NOOPEN);
6795 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6796 return FALSE;
6797 }
6798
6799 if (elf_tdata (output_bfd)->cverrefs)
6800 {
6801 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
6802
6803 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6804 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6805 return FALSE;
6806 }
6807
6808 if ((elf_tdata (output_bfd)->cverrefs == 0
6809 && elf_tdata (output_bfd)->cverdefs == 0)
6810 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6811 &section_sym_count) <= 1)
6812 {
6813 asection *s;
6814
6815 s = bfd_get_linker_section (dynobj, ".gnu.version");
6816 s->flags |= SEC_EXCLUDE;
6817 }
6818 }
6819 return TRUE;
6820 }
6821
6822 /* Find the first non-excluded output section. We'll use its
6823 section symbol for some emitted relocs. */
6824 void
6825 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6826 {
6827 asection *s;
6828
6829 for (s = output_bfd->sections; s != NULL; s = s->next)
6830 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6831 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6832 {
6833 elf_hash_table (info)->text_index_section = s;
6834 break;
6835 }
6836 }
6837
6838 /* Find two non-excluded output sections, one for code, one for data.
6839 We'll use their section symbols for some emitted relocs. */
6840 void
6841 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6842 {
6843 asection *s;
6844
6845 /* Data first, since setting text_index_section changes
6846 _bfd_elf_link_omit_section_dynsym. */
6847 for (s = output_bfd->sections; s != NULL; s = s->next)
6848 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6849 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6850 {
6851 elf_hash_table (info)->data_index_section = s;
6852 break;
6853 }
6854
6855 for (s = output_bfd->sections; s != NULL; s = s->next)
6856 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6857 == (SEC_ALLOC | SEC_READONLY))
6858 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6859 {
6860 elf_hash_table (info)->text_index_section = s;
6861 break;
6862 }
6863
6864 if (elf_hash_table (info)->text_index_section == NULL)
6865 elf_hash_table (info)->text_index_section
6866 = elf_hash_table (info)->data_index_section;
6867 }
6868
6869 bfd_boolean
6870 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6871 {
6872 const struct elf_backend_data *bed;
6873 unsigned long section_sym_count;
6874 bfd_size_type dynsymcount = 0;
6875
6876 if (!is_elf_hash_table (info->hash))
6877 return TRUE;
6878
6879 bed = get_elf_backend_data (output_bfd);
6880 (*bed->elf_backend_init_index_section) (output_bfd, info);
6881
6882 /* Assign dynsym indices. In a shared library we generate a section
6883 symbol for each output section, which come first. Next come all
6884 of the back-end allocated local dynamic syms, followed by the rest
6885 of the global symbols.
6886
6887 This is usually not needed for static binaries, however backends
6888 can request to always do it, e.g. the MIPS backend uses dynamic
6889 symbol counts to lay out GOT, which will be produced in the
6890 presence of GOT relocations even in static binaries (holding fixed
6891 data in that case, to satisfy those relocations). */
6892
6893 if (elf_hash_table (info)->dynamic_sections_created
6894 || bed->always_renumber_dynsyms)
6895 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6896 &section_sym_count);
6897
6898 if (elf_hash_table (info)->dynamic_sections_created)
6899 {
6900 bfd *dynobj;
6901 asection *s;
6902 unsigned int dtagcount;
6903
6904 dynobj = elf_hash_table (info)->dynobj;
6905
6906 /* Work out the size of the symbol version section. */
6907 s = bfd_get_linker_section (dynobj, ".gnu.version");
6908 BFD_ASSERT (s != NULL);
6909 if ((s->flags & SEC_EXCLUDE) == 0)
6910 {
6911 s->size = dynsymcount * sizeof (Elf_External_Versym);
6912 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6913 if (s->contents == NULL)
6914 return FALSE;
6915
6916 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6917 return FALSE;
6918 }
6919
6920 /* Set the size of the .dynsym and .hash sections. We counted
6921 the number of dynamic symbols in elf_link_add_object_symbols.
6922 We will build the contents of .dynsym and .hash when we build
6923 the final symbol table, because until then we do not know the
6924 correct value to give the symbols. We built the .dynstr
6925 section as we went along in elf_link_add_object_symbols. */
6926 s = elf_hash_table (info)->dynsym;
6927 BFD_ASSERT (s != NULL);
6928 s->size = dynsymcount * bed->s->sizeof_sym;
6929
6930 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6931 if (s->contents == NULL)
6932 return FALSE;
6933
6934 /* The first entry in .dynsym is a dummy symbol. Clear all the
6935 section syms, in case we don't output them all. */
6936 ++section_sym_count;
6937 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6938
6939 elf_hash_table (info)->bucketcount = 0;
6940
6941 /* Compute the size of the hashing table. As a side effect this
6942 computes the hash values for all the names we export. */
6943 if (info->emit_hash)
6944 {
6945 unsigned long int *hashcodes;
6946 struct hash_codes_info hashinf;
6947 bfd_size_type amt;
6948 unsigned long int nsyms;
6949 size_t bucketcount;
6950 size_t hash_entry_size;
6951
6952 /* Compute the hash values for all exported symbols. At the same
6953 time store the values in an array so that we could use them for
6954 optimizations. */
6955 amt = dynsymcount * sizeof (unsigned long int);
6956 hashcodes = (unsigned long int *) bfd_malloc (amt);
6957 if (hashcodes == NULL)
6958 return FALSE;
6959 hashinf.hashcodes = hashcodes;
6960 hashinf.error = FALSE;
6961
6962 /* Put all hash values in HASHCODES. */
6963 elf_link_hash_traverse (elf_hash_table (info),
6964 elf_collect_hash_codes, &hashinf);
6965 if (hashinf.error)
6966 {
6967 free (hashcodes);
6968 return FALSE;
6969 }
6970
6971 nsyms = hashinf.hashcodes - hashcodes;
6972 bucketcount
6973 = compute_bucket_count (info, hashcodes, nsyms, 0);
6974 free (hashcodes);
6975
6976 if (bucketcount == 0 && nsyms > 0)
6977 return FALSE;
6978
6979 elf_hash_table (info)->bucketcount = bucketcount;
6980
6981 s = bfd_get_linker_section (dynobj, ".hash");
6982 BFD_ASSERT (s != NULL);
6983 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6984 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6985 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6986 if (s->contents == NULL)
6987 return FALSE;
6988
6989 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6990 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6991 s->contents + hash_entry_size);
6992 }
6993
6994 if (info->emit_gnu_hash)
6995 {
6996 size_t i, cnt;
6997 unsigned char *contents;
6998 struct collect_gnu_hash_codes cinfo;
6999 bfd_size_type amt;
7000 size_t bucketcount;
7001
7002 memset (&cinfo, 0, sizeof (cinfo));
7003
7004 /* Compute the hash values for all exported symbols. At the same
7005 time store the values in an array so that we could use them for
7006 optimizations. */
7007 amt = dynsymcount * 2 * sizeof (unsigned long int);
7008 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7009 if (cinfo.hashcodes == NULL)
7010 return FALSE;
7011
7012 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7013 cinfo.min_dynindx = -1;
7014 cinfo.output_bfd = output_bfd;
7015 cinfo.bed = bed;
7016
7017 /* Put all hash values in HASHCODES. */
7018 elf_link_hash_traverse (elf_hash_table (info),
7019 elf_collect_gnu_hash_codes, &cinfo);
7020 if (cinfo.error)
7021 {
7022 free (cinfo.hashcodes);
7023 return FALSE;
7024 }
7025
7026 bucketcount
7027 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7028
7029 if (bucketcount == 0)
7030 {
7031 free (cinfo.hashcodes);
7032 return FALSE;
7033 }
7034
7035 s = bfd_get_linker_section (dynobj, ".gnu.hash");
7036 BFD_ASSERT (s != NULL);
7037
7038 if (cinfo.nsyms == 0)
7039 {
7040 /* Empty .gnu.hash section is special. */
7041 BFD_ASSERT (cinfo.min_dynindx == -1);
7042 free (cinfo.hashcodes);
7043 s->size = 5 * 4 + bed->s->arch_size / 8;
7044 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7045 if (contents == NULL)
7046 return FALSE;
7047 s->contents = contents;
7048 /* 1 empty bucket. */
7049 bfd_put_32 (output_bfd, 1, contents);
7050 /* SYMIDX above the special symbol 0. */
7051 bfd_put_32 (output_bfd, 1, contents + 4);
7052 /* Just one word for bitmask. */
7053 bfd_put_32 (output_bfd, 1, contents + 8);
7054 /* Only hash fn bloom filter. */
7055 bfd_put_32 (output_bfd, 0, contents + 12);
7056 /* No hashes are valid - empty bitmask. */
7057 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7058 /* No hashes in the only bucket. */
7059 bfd_put_32 (output_bfd, 0,
7060 contents + 16 + bed->s->arch_size / 8);
7061 }
7062 else
7063 {
7064 unsigned long int maskwords, maskbitslog2, x;
7065 BFD_ASSERT (cinfo.min_dynindx != -1);
7066
7067 x = cinfo.nsyms;
7068 maskbitslog2 = 1;
7069 while ((x >>= 1) != 0)
7070 ++maskbitslog2;
7071 if (maskbitslog2 < 3)
7072 maskbitslog2 = 5;
7073 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7074 maskbitslog2 = maskbitslog2 + 3;
7075 else
7076 maskbitslog2 = maskbitslog2 + 2;
7077 if (bed->s->arch_size == 64)
7078 {
7079 if (maskbitslog2 == 5)
7080 maskbitslog2 = 6;
7081 cinfo.shift1 = 6;
7082 }
7083 else
7084 cinfo.shift1 = 5;
7085 cinfo.mask = (1 << cinfo.shift1) - 1;
7086 cinfo.shift2 = maskbitslog2;
7087 cinfo.maskbits = 1 << maskbitslog2;
7088 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7089 amt = bucketcount * sizeof (unsigned long int) * 2;
7090 amt += maskwords * sizeof (bfd_vma);
7091 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7092 if (cinfo.bitmask == NULL)
7093 {
7094 free (cinfo.hashcodes);
7095 return FALSE;
7096 }
7097
7098 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7099 cinfo.indx = cinfo.counts + bucketcount;
7100 cinfo.symindx = dynsymcount - cinfo.nsyms;
7101 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7102
7103 /* Determine how often each hash bucket is used. */
7104 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7105 for (i = 0; i < cinfo.nsyms; ++i)
7106 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7107
7108 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7109 if (cinfo.counts[i] != 0)
7110 {
7111 cinfo.indx[i] = cnt;
7112 cnt += cinfo.counts[i];
7113 }
7114 BFD_ASSERT (cnt == dynsymcount);
7115 cinfo.bucketcount = bucketcount;
7116 cinfo.local_indx = cinfo.min_dynindx;
7117
7118 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7119 s->size += cinfo.maskbits / 8;
7120 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7121 if (contents == NULL)
7122 {
7123 free (cinfo.bitmask);
7124 free (cinfo.hashcodes);
7125 return FALSE;
7126 }
7127
7128 s->contents = contents;
7129 bfd_put_32 (output_bfd, bucketcount, contents);
7130 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7131 bfd_put_32 (output_bfd, maskwords, contents + 8);
7132 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7133 contents += 16 + cinfo.maskbits / 8;
7134
7135 for (i = 0; i < bucketcount; ++i)
7136 {
7137 if (cinfo.counts[i] == 0)
7138 bfd_put_32 (output_bfd, 0, contents);
7139 else
7140 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7141 contents += 4;
7142 }
7143
7144 cinfo.contents = contents;
7145
7146 /* Renumber dynamic symbols, populate .gnu.hash section. */
7147 elf_link_hash_traverse (elf_hash_table (info),
7148 elf_renumber_gnu_hash_syms, &cinfo);
7149
7150 contents = s->contents + 16;
7151 for (i = 0; i < maskwords; ++i)
7152 {
7153 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7154 contents);
7155 contents += bed->s->arch_size / 8;
7156 }
7157
7158 free (cinfo.bitmask);
7159 free (cinfo.hashcodes);
7160 }
7161 }
7162
7163 s = bfd_get_linker_section (dynobj, ".dynstr");
7164 BFD_ASSERT (s != NULL);
7165
7166 elf_finalize_dynstr (output_bfd, info);
7167
7168 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7169
7170 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7171 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7172 return FALSE;
7173 }
7174
7175 return TRUE;
7176 }
7177 \f
7178 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7179
7180 static void
7181 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7182 asection *sec)
7183 {
7184 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7185 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7186 }
7187
7188 /* Finish SHF_MERGE section merging. */
7189
7190 bfd_boolean
7191 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7192 {
7193 bfd *ibfd;
7194 asection *sec;
7195
7196 if (!is_elf_hash_table (info->hash))
7197 return FALSE;
7198
7199 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7200 if ((ibfd->flags & DYNAMIC) == 0
7201 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7202 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7203 == get_elf_backend_data (obfd)->s->elfclass))
7204 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7205 if ((sec->flags & SEC_MERGE) != 0
7206 && !bfd_is_abs_section (sec->output_section))
7207 {
7208 struct bfd_elf_section_data *secdata;
7209
7210 secdata = elf_section_data (sec);
7211 if (! _bfd_add_merge_section (obfd,
7212 &elf_hash_table (info)->merge_info,
7213 sec, &secdata->sec_info))
7214 return FALSE;
7215 else if (secdata->sec_info)
7216 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7217 }
7218
7219 if (elf_hash_table (info)->merge_info != NULL)
7220 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7221 merge_sections_remove_hook);
7222 return TRUE;
7223 }
7224
7225 /* Create an entry in an ELF linker hash table. */
7226
7227 struct bfd_hash_entry *
7228 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7229 struct bfd_hash_table *table,
7230 const char *string)
7231 {
7232 /* Allocate the structure if it has not already been allocated by a
7233 subclass. */
7234 if (entry == NULL)
7235 {
7236 entry = (struct bfd_hash_entry *)
7237 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7238 if (entry == NULL)
7239 return entry;
7240 }
7241
7242 /* Call the allocation method of the superclass. */
7243 entry = _bfd_link_hash_newfunc (entry, table, string);
7244 if (entry != NULL)
7245 {
7246 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7247 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7248
7249 /* Set local fields. */
7250 ret->indx = -1;
7251 ret->dynindx = -1;
7252 ret->got = htab->init_got_refcount;
7253 ret->plt = htab->init_plt_refcount;
7254 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7255 - offsetof (struct elf_link_hash_entry, size)));
7256 /* Assume that we have been called by a non-ELF symbol reader.
7257 This flag is then reset by the code which reads an ELF input
7258 file. This ensures that a symbol created by a non-ELF symbol
7259 reader will have the flag set correctly. */
7260 ret->non_elf = 1;
7261 }
7262
7263 return entry;
7264 }
7265
7266 /* Copy data from an indirect symbol to its direct symbol, hiding the
7267 old indirect symbol. Also used for copying flags to a weakdef. */
7268
7269 void
7270 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7271 struct elf_link_hash_entry *dir,
7272 struct elf_link_hash_entry *ind)
7273 {
7274 struct elf_link_hash_table *htab;
7275
7276 /* Copy down any references that we may have already seen to the
7277 symbol which just became indirect. */
7278
7279 if (dir->versioned != versioned_hidden)
7280 dir->ref_dynamic |= ind->ref_dynamic;
7281 dir->ref_regular |= ind->ref_regular;
7282 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7283 dir->non_got_ref |= ind->non_got_ref;
7284 dir->needs_plt |= ind->needs_plt;
7285 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7286
7287 if (ind->root.type != bfd_link_hash_indirect)
7288 return;
7289
7290 /* Copy over the global and procedure linkage table refcount entries.
7291 These may have been already set up by a check_relocs routine. */
7292 htab = elf_hash_table (info);
7293 if (ind->got.refcount > htab->init_got_refcount.refcount)
7294 {
7295 if (dir->got.refcount < 0)
7296 dir->got.refcount = 0;
7297 dir->got.refcount += ind->got.refcount;
7298 ind->got.refcount = htab->init_got_refcount.refcount;
7299 }
7300
7301 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7302 {
7303 if (dir->plt.refcount < 0)
7304 dir->plt.refcount = 0;
7305 dir->plt.refcount += ind->plt.refcount;
7306 ind->plt.refcount = htab->init_plt_refcount.refcount;
7307 }
7308
7309 if (ind->dynindx != -1)
7310 {
7311 if (dir->dynindx != -1)
7312 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7313 dir->dynindx = ind->dynindx;
7314 dir->dynstr_index = ind->dynstr_index;
7315 ind->dynindx = -1;
7316 ind->dynstr_index = 0;
7317 }
7318 }
7319
7320 void
7321 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7322 struct elf_link_hash_entry *h,
7323 bfd_boolean force_local)
7324 {
7325 /* STT_GNU_IFUNC symbol must go through PLT. */
7326 if (h->type != STT_GNU_IFUNC)
7327 {
7328 h->plt = elf_hash_table (info)->init_plt_offset;
7329 h->needs_plt = 0;
7330 }
7331 if (force_local)
7332 {
7333 h->forced_local = 1;
7334 if (h->dynindx != -1)
7335 {
7336 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7337 h->dynstr_index);
7338 h->dynindx = -1;
7339 h->dynstr_index = 0;
7340 }
7341 }
7342 }
7343
7344 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7345 caller. */
7346
7347 bfd_boolean
7348 _bfd_elf_link_hash_table_init
7349 (struct elf_link_hash_table *table,
7350 bfd *abfd,
7351 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7352 struct bfd_hash_table *,
7353 const char *),
7354 unsigned int entsize,
7355 enum elf_target_id target_id)
7356 {
7357 bfd_boolean ret;
7358 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7359
7360 table->init_got_refcount.refcount = can_refcount - 1;
7361 table->init_plt_refcount.refcount = can_refcount - 1;
7362 table->init_got_offset.offset = -(bfd_vma) 1;
7363 table->init_plt_offset.offset = -(bfd_vma) 1;
7364 /* The first dynamic symbol is a dummy. */
7365 table->dynsymcount = 1;
7366
7367 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7368
7369 table->root.type = bfd_link_elf_hash_table;
7370 table->hash_table_id = target_id;
7371
7372 return ret;
7373 }
7374
7375 /* Create an ELF linker hash table. */
7376
7377 struct bfd_link_hash_table *
7378 _bfd_elf_link_hash_table_create (bfd *abfd)
7379 {
7380 struct elf_link_hash_table *ret;
7381 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7382
7383 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7384 if (ret == NULL)
7385 return NULL;
7386
7387 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7388 sizeof (struct elf_link_hash_entry),
7389 GENERIC_ELF_DATA))
7390 {
7391 free (ret);
7392 return NULL;
7393 }
7394 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7395
7396 return &ret->root;
7397 }
7398
7399 /* Destroy an ELF linker hash table. */
7400
7401 void
7402 _bfd_elf_link_hash_table_free (bfd *obfd)
7403 {
7404 struct elf_link_hash_table *htab;
7405
7406 htab = (struct elf_link_hash_table *) obfd->link.hash;
7407 if (htab->dynstr != NULL)
7408 _bfd_elf_strtab_free (htab->dynstr);
7409 _bfd_merge_sections_free (htab->merge_info);
7410 _bfd_generic_link_hash_table_free (obfd);
7411 }
7412
7413 /* This is a hook for the ELF emulation code in the generic linker to
7414 tell the backend linker what file name to use for the DT_NEEDED
7415 entry for a dynamic object. */
7416
7417 void
7418 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7419 {
7420 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7421 && bfd_get_format (abfd) == bfd_object)
7422 elf_dt_name (abfd) = name;
7423 }
7424
7425 int
7426 bfd_elf_get_dyn_lib_class (bfd *abfd)
7427 {
7428 int lib_class;
7429 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7430 && bfd_get_format (abfd) == bfd_object)
7431 lib_class = elf_dyn_lib_class (abfd);
7432 else
7433 lib_class = 0;
7434 return lib_class;
7435 }
7436
7437 void
7438 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7439 {
7440 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7441 && bfd_get_format (abfd) == bfd_object)
7442 elf_dyn_lib_class (abfd) = lib_class;
7443 }
7444
7445 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7446 the linker ELF emulation code. */
7447
7448 struct bfd_link_needed_list *
7449 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7450 struct bfd_link_info *info)
7451 {
7452 if (! is_elf_hash_table (info->hash))
7453 return NULL;
7454 return elf_hash_table (info)->needed;
7455 }
7456
7457 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7458 hook for the linker ELF emulation code. */
7459
7460 struct bfd_link_needed_list *
7461 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7462 struct bfd_link_info *info)
7463 {
7464 if (! is_elf_hash_table (info->hash))
7465 return NULL;
7466 return elf_hash_table (info)->runpath;
7467 }
7468
7469 /* Get the name actually used for a dynamic object for a link. This
7470 is the SONAME entry if there is one. Otherwise, it is the string
7471 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7472
7473 const char *
7474 bfd_elf_get_dt_soname (bfd *abfd)
7475 {
7476 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7477 && bfd_get_format (abfd) == bfd_object)
7478 return elf_dt_name (abfd);
7479 return NULL;
7480 }
7481
7482 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7483 the ELF linker emulation code. */
7484
7485 bfd_boolean
7486 bfd_elf_get_bfd_needed_list (bfd *abfd,
7487 struct bfd_link_needed_list **pneeded)
7488 {
7489 asection *s;
7490 bfd_byte *dynbuf = NULL;
7491 unsigned int elfsec;
7492 unsigned long shlink;
7493 bfd_byte *extdyn, *extdynend;
7494 size_t extdynsize;
7495 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7496
7497 *pneeded = NULL;
7498
7499 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7500 || bfd_get_format (abfd) != bfd_object)
7501 return TRUE;
7502
7503 s = bfd_get_section_by_name (abfd, ".dynamic");
7504 if (s == NULL || s->size == 0)
7505 return TRUE;
7506
7507 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7508 goto error_return;
7509
7510 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7511 if (elfsec == SHN_BAD)
7512 goto error_return;
7513
7514 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7515
7516 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7517 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7518
7519 extdyn = dynbuf;
7520 extdynend = extdyn + s->size;
7521 for (; extdyn < extdynend; extdyn += extdynsize)
7522 {
7523 Elf_Internal_Dyn dyn;
7524
7525 (*swap_dyn_in) (abfd, extdyn, &dyn);
7526
7527 if (dyn.d_tag == DT_NULL)
7528 break;
7529
7530 if (dyn.d_tag == DT_NEEDED)
7531 {
7532 const char *string;
7533 struct bfd_link_needed_list *l;
7534 unsigned int tagv = dyn.d_un.d_val;
7535 bfd_size_type amt;
7536
7537 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7538 if (string == NULL)
7539 goto error_return;
7540
7541 amt = sizeof *l;
7542 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7543 if (l == NULL)
7544 goto error_return;
7545
7546 l->by = abfd;
7547 l->name = string;
7548 l->next = *pneeded;
7549 *pneeded = l;
7550 }
7551 }
7552
7553 free (dynbuf);
7554
7555 return TRUE;
7556
7557 error_return:
7558 if (dynbuf != NULL)
7559 free (dynbuf);
7560 return FALSE;
7561 }
7562
7563 struct elf_symbuf_symbol
7564 {
7565 unsigned long st_name; /* Symbol name, index in string tbl */
7566 unsigned char st_info; /* Type and binding attributes */
7567 unsigned char st_other; /* Visibilty, and target specific */
7568 };
7569
7570 struct elf_symbuf_head
7571 {
7572 struct elf_symbuf_symbol *ssym;
7573 size_t count;
7574 unsigned int st_shndx;
7575 };
7576
7577 struct elf_symbol
7578 {
7579 union
7580 {
7581 Elf_Internal_Sym *isym;
7582 struct elf_symbuf_symbol *ssym;
7583 } u;
7584 const char *name;
7585 };
7586
7587 /* Sort references to symbols by ascending section number. */
7588
7589 static int
7590 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7591 {
7592 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7593 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7594
7595 return s1->st_shndx - s2->st_shndx;
7596 }
7597
7598 static int
7599 elf_sym_name_compare (const void *arg1, const void *arg2)
7600 {
7601 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7602 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7603 return strcmp (s1->name, s2->name);
7604 }
7605
7606 static struct elf_symbuf_head *
7607 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
7608 {
7609 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7610 struct elf_symbuf_symbol *ssym;
7611 struct elf_symbuf_head *ssymbuf, *ssymhead;
7612 size_t i, shndx_count, total_size;
7613
7614 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7615 if (indbuf == NULL)
7616 return NULL;
7617
7618 for (ind = indbuf, i = 0; i < symcount; i++)
7619 if (isymbuf[i].st_shndx != SHN_UNDEF)
7620 *ind++ = &isymbuf[i];
7621 indbufend = ind;
7622
7623 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7624 elf_sort_elf_symbol);
7625
7626 shndx_count = 0;
7627 if (indbufend > indbuf)
7628 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7629 if (ind[0]->st_shndx != ind[1]->st_shndx)
7630 shndx_count++;
7631
7632 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7633 + (indbufend - indbuf) * sizeof (*ssym));
7634 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7635 if (ssymbuf == NULL)
7636 {
7637 free (indbuf);
7638 return NULL;
7639 }
7640
7641 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7642 ssymbuf->ssym = NULL;
7643 ssymbuf->count = shndx_count;
7644 ssymbuf->st_shndx = 0;
7645 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7646 {
7647 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7648 {
7649 ssymhead++;
7650 ssymhead->ssym = ssym;
7651 ssymhead->count = 0;
7652 ssymhead->st_shndx = (*ind)->st_shndx;
7653 }
7654 ssym->st_name = (*ind)->st_name;
7655 ssym->st_info = (*ind)->st_info;
7656 ssym->st_other = (*ind)->st_other;
7657 ssymhead->count++;
7658 }
7659 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
7660 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7661 == total_size));
7662
7663 free (indbuf);
7664 return ssymbuf;
7665 }
7666
7667 /* Check if 2 sections define the same set of local and global
7668 symbols. */
7669
7670 static bfd_boolean
7671 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7672 struct bfd_link_info *info)
7673 {
7674 bfd *bfd1, *bfd2;
7675 const struct elf_backend_data *bed1, *bed2;
7676 Elf_Internal_Shdr *hdr1, *hdr2;
7677 size_t symcount1, symcount2;
7678 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7679 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7680 Elf_Internal_Sym *isym, *isymend;
7681 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7682 size_t count1, count2, i;
7683 unsigned int shndx1, shndx2;
7684 bfd_boolean result;
7685
7686 bfd1 = sec1->owner;
7687 bfd2 = sec2->owner;
7688
7689 /* Both sections have to be in ELF. */
7690 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7691 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7692 return FALSE;
7693
7694 if (elf_section_type (sec1) != elf_section_type (sec2))
7695 return FALSE;
7696
7697 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7698 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7699 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7700 return FALSE;
7701
7702 bed1 = get_elf_backend_data (bfd1);
7703 bed2 = get_elf_backend_data (bfd2);
7704 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7705 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7706 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7707 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7708
7709 if (symcount1 == 0 || symcount2 == 0)
7710 return FALSE;
7711
7712 result = FALSE;
7713 isymbuf1 = NULL;
7714 isymbuf2 = NULL;
7715 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7716 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7717
7718 if (ssymbuf1 == NULL)
7719 {
7720 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7721 NULL, NULL, NULL);
7722 if (isymbuf1 == NULL)
7723 goto done;
7724
7725 if (!info->reduce_memory_overheads)
7726 elf_tdata (bfd1)->symbuf = ssymbuf1
7727 = elf_create_symbuf (symcount1, isymbuf1);
7728 }
7729
7730 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7731 {
7732 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7733 NULL, NULL, NULL);
7734 if (isymbuf2 == NULL)
7735 goto done;
7736
7737 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7738 elf_tdata (bfd2)->symbuf = ssymbuf2
7739 = elf_create_symbuf (symcount2, isymbuf2);
7740 }
7741
7742 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7743 {
7744 /* Optimized faster version. */
7745 size_t lo, hi, mid;
7746 struct elf_symbol *symp;
7747 struct elf_symbuf_symbol *ssym, *ssymend;
7748
7749 lo = 0;
7750 hi = ssymbuf1->count;
7751 ssymbuf1++;
7752 count1 = 0;
7753 while (lo < hi)
7754 {
7755 mid = (lo + hi) / 2;
7756 if (shndx1 < ssymbuf1[mid].st_shndx)
7757 hi = mid;
7758 else if (shndx1 > ssymbuf1[mid].st_shndx)
7759 lo = mid + 1;
7760 else
7761 {
7762 count1 = ssymbuf1[mid].count;
7763 ssymbuf1 += mid;
7764 break;
7765 }
7766 }
7767
7768 lo = 0;
7769 hi = ssymbuf2->count;
7770 ssymbuf2++;
7771 count2 = 0;
7772 while (lo < hi)
7773 {
7774 mid = (lo + hi) / 2;
7775 if (shndx2 < ssymbuf2[mid].st_shndx)
7776 hi = mid;
7777 else if (shndx2 > ssymbuf2[mid].st_shndx)
7778 lo = mid + 1;
7779 else
7780 {
7781 count2 = ssymbuf2[mid].count;
7782 ssymbuf2 += mid;
7783 break;
7784 }
7785 }
7786
7787 if (count1 == 0 || count2 == 0 || count1 != count2)
7788 goto done;
7789
7790 symtable1
7791 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7792 symtable2
7793 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
7794 if (symtable1 == NULL || symtable2 == NULL)
7795 goto done;
7796
7797 symp = symtable1;
7798 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7799 ssym < ssymend; ssym++, symp++)
7800 {
7801 symp->u.ssym = ssym;
7802 symp->name = bfd_elf_string_from_elf_section (bfd1,
7803 hdr1->sh_link,
7804 ssym->st_name);
7805 }
7806
7807 symp = symtable2;
7808 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7809 ssym < ssymend; ssym++, symp++)
7810 {
7811 symp->u.ssym = ssym;
7812 symp->name = bfd_elf_string_from_elf_section (bfd2,
7813 hdr2->sh_link,
7814 ssym->st_name);
7815 }
7816
7817 /* Sort symbol by name. */
7818 qsort (symtable1, count1, sizeof (struct elf_symbol),
7819 elf_sym_name_compare);
7820 qsort (symtable2, count1, sizeof (struct elf_symbol),
7821 elf_sym_name_compare);
7822
7823 for (i = 0; i < count1; i++)
7824 /* Two symbols must have the same binding, type and name. */
7825 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7826 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7827 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7828 goto done;
7829
7830 result = TRUE;
7831 goto done;
7832 }
7833
7834 symtable1 = (struct elf_symbol *)
7835 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7836 symtable2 = (struct elf_symbol *)
7837 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7838 if (symtable1 == NULL || symtable2 == NULL)
7839 goto done;
7840
7841 /* Count definitions in the section. */
7842 count1 = 0;
7843 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7844 if (isym->st_shndx == shndx1)
7845 symtable1[count1++].u.isym = isym;
7846
7847 count2 = 0;
7848 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7849 if (isym->st_shndx == shndx2)
7850 symtable2[count2++].u.isym = isym;
7851
7852 if (count1 == 0 || count2 == 0 || count1 != count2)
7853 goto done;
7854
7855 for (i = 0; i < count1; i++)
7856 symtable1[i].name
7857 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7858 symtable1[i].u.isym->st_name);
7859
7860 for (i = 0; i < count2; i++)
7861 symtable2[i].name
7862 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7863 symtable2[i].u.isym->st_name);
7864
7865 /* Sort symbol by name. */
7866 qsort (symtable1, count1, sizeof (struct elf_symbol),
7867 elf_sym_name_compare);
7868 qsort (symtable2, count1, sizeof (struct elf_symbol),
7869 elf_sym_name_compare);
7870
7871 for (i = 0; i < count1; i++)
7872 /* Two symbols must have the same binding, type and name. */
7873 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7874 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7875 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7876 goto done;
7877
7878 result = TRUE;
7879
7880 done:
7881 if (symtable1)
7882 free (symtable1);
7883 if (symtable2)
7884 free (symtable2);
7885 if (isymbuf1)
7886 free (isymbuf1);
7887 if (isymbuf2)
7888 free (isymbuf2);
7889
7890 return result;
7891 }
7892
7893 /* Return TRUE if 2 section types are compatible. */
7894
7895 bfd_boolean
7896 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7897 bfd *bbfd, const asection *bsec)
7898 {
7899 if (asec == NULL
7900 || bsec == NULL
7901 || abfd->xvec->flavour != bfd_target_elf_flavour
7902 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7903 return TRUE;
7904
7905 return elf_section_type (asec) == elf_section_type (bsec);
7906 }
7907 \f
7908 /* Final phase of ELF linker. */
7909
7910 /* A structure we use to avoid passing large numbers of arguments. */
7911
7912 struct elf_final_link_info
7913 {
7914 /* General link information. */
7915 struct bfd_link_info *info;
7916 /* Output BFD. */
7917 bfd *output_bfd;
7918 /* Symbol string table. */
7919 struct elf_strtab_hash *symstrtab;
7920 /* .hash section. */
7921 asection *hash_sec;
7922 /* symbol version section (.gnu.version). */
7923 asection *symver_sec;
7924 /* Buffer large enough to hold contents of any section. */
7925 bfd_byte *contents;
7926 /* Buffer large enough to hold external relocs of any section. */
7927 void *external_relocs;
7928 /* Buffer large enough to hold internal relocs of any section. */
7929 Elf_Internal_Rela *internal_relocs;
7930 /* Buffer large enough to hold external local symbols of any input
7931 BFD. */
7932 bfd_byte *external_syms;
7933 /* And a buffer for symbol section indices. */
7934 Elf_External_Sym_Shndx *locsym_shndx;
7935 /* Buffer large enough to hold internal local symbols of any input
7936 BFD. */
7937 Elf_Internal_Sym *internal_syms;
7938 /* Array large enough to hold a symbol index for each local symbol
7939 of any input BFD. */
7940 long *indices;
7941 /* Array large enough to hold a section pointer for each local
7942 symbol of any input BFD. */
7943 asection **sections;
7944 /* Buffer for SHT_SYMTAB_SHNDX section. */
7945 Elf_External_Sym_Shndx *symshndxbuf;
7946 /* Number of STT_FILE syms seen. */
7947 size_t filesym_count;
7948 };
7949
7950 /* This struct is used to pass information to elf_link_output_extsym. */
7951
7952 struct elf_outext_info
7953 {
7954 bfd_boolean failed;
7955 bfd_boolean localsyms;
7956 bfd_boolean file_sym_done;
7957 struct elf_final_link_info *flinfo;
7958 };
7959
7960
7961 /* Support for evaluating a complex relocation.
7962
7963 Complex relocations are generalized, self-describing relocations. The
7964 implementation of them consists of two parts: complex symbols, and the
7965 relocations themselves.
7966
7967 The relocations are use a reserved elf-wide relocation type code (R_RELC
7968 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7969 information (start bit, end bit, word width, etc) into the addend. This
7970 information is extracted from CGEN-generated operand tables within gas.
7971
7972 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7973 internal) representing prefix-notation expressions, including but not
7974 limited to those sorts of expressions normally encoded as addends in the
7975 addend field. The symbol mangling format is:
7976
7977 <node> := <literal>
7978 | <unary-operator> ':' <node>
7979 | <binary-operator> ':' <node> ':' <node>
7980 ;
7981
7982 <literal> := 's' <digits=N> ':' <N character symbol name>
7983 | 'S' <digits=N> ':' <N character section name>
7984 | '#' <hexdigits>
7985 ;
7986
7987 <binary-operator> := as in C
7988 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7989
7990 static void
7991 set_symbol_value (bfd *bfd_with_globals,
7992 Elf_Internal_Sym *isymbuf,
7993 size_t locsymcount,
7994 size_t symidx,
7995 bfd_vma val)
7996 {
7997 struct elf_link_hash_entry **sym_hashes;
7998 struct elf_link_hash_entry *h;
7999 size_t extsymoff = locsymcount;
8000
8001 if (symidx < locsymcount)
8002 {
8003 Elf_Internal_Sym *sym;
8004
8005 sym = isymbuf + symidx;
8006 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8007 {
8008 /* It is a local symbol: move it to the
8009 "absolute" section and give it a value. */
8010 sym->st_shndx = SHN_ABS;
8011 sym->st_value = val;
8012 return;
8013 }
8014 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8015 extsymoff = 0;
8016 }
8017
8018 /* It is a global symbol: set its link type
8019 to "defined" and give it a value. */
8020
8021 sym_hashes = elf_sym_hashes (bfd_with_globals);
8022 h = sym_hashes [symidx - extsymoff];
8023 while (h->root.type == bfd_link_hash_indirect
8024 || h->root.type == bfd_link_hash_warning)
8025 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8026 h->root.type = bfd_link_hash_defined;
8027 h->root.u.def.value = val;
8028 h->root.u.def.section = bfd_abs_section_ptr;
8029 }
8030
8031 static bfd_boolean
8032 resolve_symbol (const char *name,
8033 bfd *input_bfd,
8034 struct elf_final_link_info *flinfo,
8035 bfd_vma *result,
8036 Elf_Internal_Sym *isymbuf,
8037 size_t locsymcount)
8038 {
8039 Elf_Internal_Sym *sym;
8040 struct bfd_link_hash_entry *global_entry;
8041 const char *candidate = NULL;
8042 Elf_Internal_Shdr *symtab_hdr;
8043 size_t i;
8044
8045 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8046
8047 for (i = 0; i < locsymcount; ++ i)
8048 {
8049 sym = isymbuf + i;
8050
8051 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8052 continue;
8053
8054 candidate = bfd_elf_string_from_elf_section (input_bfd,
8055 symtab_hdr->sh_link,
8056 sym->st_name);
8057 #ifdef DEBUG
8058 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8059 name, candidate, (unsigned long) sym->st_value);
8060 #endif
8061 if (candidate && strcmp (candidate, name) == 0)
8062 {
8063 asection *sec = flinfo->sections [i];
8064
8065 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8066 *result += sec->output_offset + sec->output_section->vma;
8067 #ifdef DEBUG
8068 printf ("Found symbol with value %8.8lx\n",
8069 (unsigned long) *result);
8070 #endif
8071 return TRUE;
8072 }
8073 }
8074
8075 /* Hmm, haven't found it yet. perhaps it is a global. */
8076 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8077 FALSE, FALSE, TRUE);
8078 if (!global_entry)
8079 return FALSE;
8080
8081 if (global_entry->type == bfd_link_hash_defined
8082 || global_entry->type == bfd_link_hash_defweak)
8083 {
8084 *result = (global_entry->u.def.value
8085 + global_entry->u.def.section->output_section->vma
8086 + global_entry->u.def.section->output_offset);
8087 #ifdef DEBUG
8088 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8089 global_entry->root.string, (unsigned long) *result);
8090 #endif
8091 return TRUE;
8092 }
8093
8094 return FALSE;
8095 }
8096
8097 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8098 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8099 names like "foo.end" which is the end address of section "foo". */
8100
8101 static bfd_boolean
8102 resolve_section (const char *name,
8103 asection *sections,
8104 bfd_vma *result,
8105 bfd * abfd)
8106 {
8107 asection *curr;
8108 unsigned int len;
8109
8110 for (curr = sections; curr; curr = curr->next)
8111 if (strcmp (curr->name, name) == 0)
8112 {
8113 *result = curr->vma;
8114 return TRUE;
8115 }
8116
8117 /* Hmm. still haven't found it. try pseudo-section names. */
8118 /* FIXME: This could be coded more efficiently... */
8119 for (curr = sections; curr; curr = curr->next)
8120 {
8121 len = strlen (curr->name);
8122 if (len > strlen (name))
8123 continue;
8124
8125 if (strncmp (curr->name, name, len) == 0)
8126 {
8127 if (strncmp (".end", name + len, 4) == 0)
8128 {
8129 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
8130 return TRUE;
8131 }
8132
8133 /* Insert more pseudo-section names here, if you like. */
8134 }
8135 }
8136
8137 return FALSE;
8138 }
8139
8140 static void
8141 undefined_reference (const char *reftype, const char *name)
8142 {
8143 /* xgettext:c-format */
8144 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8145 reftype, name);
8146 }
8147
8148 static bfd_boolean
8149 eval_symbol (bfd_vma *result,
8150 const char **symp,
8151 bfd *input_bfd,
8152 struct elf_final_link_info *flinfo,
8153 bfd_vma dot,
8154 Elf_Internal_Sym *isymbuf,
8155 size_t locsymcount,
8156 int signed_p)
8157 {
8158 size_t len;
8159 size_t symlen;
8160 bfd_vma a;
8161 bfd_vma b;
8162 char symbuf[4096];
8163 const char *sym = *symp;
8164 const char *symend;
8165 bfd_boolean symbol_is_section = FALSE;
8166
8167 len = strlen (sym);
8168 symend = sym + len;
8169
8170 if (len < 1 || len > sizeof (symbuf))
8171 {
8172 bfd_set_error (bfd_error_invalid_operation);
8173 return FALSE;
8174 }
8175
8176 switch (* sym)
8177 {
8178 case '.':
8179 *result = dot;
8180 *symp = sym + 1;
8181 return TRUE;
8182
8183 case '#':
8184 ++sym;
8185 *result = strtoul (sym, (char **) symp, 16);
8186 return TRUE;
8187
8188 case 'S':
8189 symbol_is_section = TRUE;
8190 /* Fall through. */
8191 case 's':
8192 ++sym;
8193 symlen = strtol (sym, (char **) symp, 10);
8194 sym = *symp + 1; /* Skip the trailing ':'. */
8195
8196 if (symend < sym || symlen + 1 > sizeof (symbuf))
8197 {
8198 bfd_set_error (bfd_error_invalid_operation);
8199 return FALSE;
8200 }
8201
8202 memcpy (symbuf, sym, symlen);
8203 symbuf[symlen] = '\0';
8204 *symp = sym + symlen;
8205
8206 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8207 the symbol as a section, or vice-versa. so we're pretty liberal in our
8208 interpretation here; section means "try section first", not "must be a
8209 section", and likewise with symbol. */
8210
8211 if (symbol_is_section)
8212 {
8213 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8214 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8215 isymbuf, locsymcount))
8216 {
8217 undefined_reference ("section", symbuf);
8218 return FALSE;
8219 }
8220 }
8221 else
8222 {
8223 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8224 isymbuf, locsymcount)
8225 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8226 result, input_bfd))
8227 {
8228 undefined_reference ("symbol", symbuf);
8229 return FALSE;
8230 }
8231 }
8232
8233 return TRUE;
8234
8235 /* All that remains are operators. */
8236
8237 #define UNARY_OP(op) \
8238 if (strncmp (sym, #op, strlen (#op)) == 0) \
8239 { \
8240 sym += strlen (#op); \
8241 if (*sym == ':') \
8242 ++sym; \
8243 *symp = sym; \
8244 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8245 isymbuf, locsymcount, signed_p)) \
8246 return FALSE; \
8247 if (signed_p) \
8248 *result = op ((bfd_signed_vma) a); \
8249 else \
8250 *result = op a; \
8251 return TRUE; \
8252 }
8253
8254 #define BINARY_OP(op) \
8255 if (strncmp (sym, #op, strlen (#op)) == 0) \
8256 { \
8257 sym += strlen (#op); \
8258 if (*sym == ':') \
8259 ++sym; \
8260 *symp = sym; \
8261 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8262 isymbuf, locsymcount, signed_p)) \
8263 return FALSE; \
8264 ++*symp; \
8265 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8266 isymbuf, locsymcount, signed_p)) \
8267 return FALSE; \
8268 if (signed_p) \
8269 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8270 else \
8271 *result = a op b; \
8272 return TRUE; \
8273 }
8274
8275 default:
8276 UNARY_OP (0-);
8277 BINARY_OP (<<);
8278 BINARY_OP (>>);
8279 BINARY_OP (==);
8280 BINARY_OP (!=);
8281 BINARY_OP (<=);
8282 BINARY_OP (>=);
8283 BINARY_OP (&&);
8284 BINARY_OP (||);
8285 UNARY_OP (~);
8286 UNARY_OP (!);
8287 BINARY_OP (*);
8288 BINARY_OP (/);
8289 BINARY_OP (%);
8290 BINARY_OP (^);
8291 BINARY_OP (|);
8292 BINARY_OP (&);
8293 BINARY_OP (+);
8294 BINARY_OP (-);
8295 BINARY_OP (<);
8296 BINARY_OP (>);
8297 #undef UNARY_OP
8298 #undef BINARY_OP
8299 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8300 bfd_set_error (bfd_error_invalid_operation);
8301 return FALSE;
8302 }
8303 }
8304
8305 static void
8306 put_value (bfd_vma size,
8307 unsigned long chunksz,
8308 bfd *input_bfd,
8309 bfd_vma x,
8310 bfd_byte *location)
8311 {
8312 location += (size - chunksz);
8313
8314 for (; size; size -= chunksz, location -= chunksz)
8315 {
8316 switch (chunksz)
8317 {
8318 case 1:
8319 bfd_put_8 (input_bfd, x, location);
8320 x >>= 8;
8321 break;
8322 case 2:
8323 bfd_put_16 (input_bfd, x, location);
8324 x >>= 16;
8325 break;
8326 case 4:
8327 bfd_put_32 (input_bfd, x, location);
8328 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8329 x >>= 16;
8330 x >>= 16;
8331 break;
8332 #ifdef BFD64
8333 case 8:
8334 bfd_put_64 (input_bfd, x, location);
8335 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8336 x >>= 32;
8337 x >>= 32;
8338 break;
8339 #endif
8340 default:
8341 abort ();
8342 break;
8343 }
8344 }
8345 }
8346
8347 static bfd_vma
8348 get_value (bfd_vma size,
8349 unsigned long chunksz,
8350 bfd *input_bfd,
8351 bfd_byte *location)
8352 {
8353 int shift;
8354 bfd_vma x = 0;
8355
8356 /* Sanity checks. */
8357 BFD_ASSERT (chunksz <= sizeof (x)
8358 && size >= chunksz
8359 && chunksz != 0
8360 && (size % chunksz) == 0
8361 && input_bfd != NULL
8362 && location != NULL);
8363
8364 if (chunksz == sizeof (x))
8365 {
8366 BFD_ASSERT (size == chunksz);
8367
8368 /* Make sure that we do not perform an undefined shift operation.
8369 We know that size == chunksz so there will only be one iteration
8370 of the loop below. */
8371 shift = 0;
8372 }
8373 else
8374 shift = 8 * chunksz;
8375
8376 for (; size; size -= chunksz, location += chunksz)
8377 {
8378 switch (chunksz)
8379 {
8380 case 1:
8381 x = (x << shift) | bfd_get_8 (input_bfd, location);
8382 break;
8383 case 2:
8384 x = (x << shift) | bfd_get_16 (input_bfd, location);
8385 break;
8386 case 4:
8387 x = (x << shift) | bfd_get_32 (input_bfd, location);
8388 break;
8389 #ifdef BFD64
8390 case 8:
8391 x = (x << shift) | bfd_get_64 (input_bfd, location);
8392 break;
8393 #endif
8394 default:
8395 abort ();
8396 }
8397 }
8398 return x;
8399 }
8400
8401 static void
8402 decode_complex_addend (unsigned long *start, /* in bits */
8403 unsigned long *oplen, /* in bits */
8404 unsigned long *len, /* in bits */
8405 unsigned long *wordsz, /* in bytes */
8406 unsigned long *chunksz, /* in bytes */
8407 unsigned long *lsb0_p,
8408 unsigned long *signed_p,
8409 unsigned long *trunc_p,
8410 unsigned long encoded)
8411 {
8412 * start = encoded & 0x3F;
8413 * len = (encoded >> 6) & 0x3F;
8414 * oplen = (encoded >> 12) & 0x3F;
8415 * wordsz = (encoded >> 18) & 0xF;
8416 * chunksz = (encoded >> 22) & 0xF;
8417 * lsb0_p = (encoded >> 27) & 1;
8418 * signed_p = (encoded >> 28) & 1;
8419 * trunc_p = (encoded >> 29) & 1;
8420 }
8421
8422 bfd_reloc_status_type
8423 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8424 asection *input_section ATTRIBUTE_UNUSED,
8425 bfd_byte *contents,
8426 Elf_Internal_Rela *rel,
8427 bfd_vma relocation)
8428 {
8429 bfd_vma shift, x, mask;
8430 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8431 bfd_reloc_status_type r;
8432
8433 /* Perform this reloc, since it is complex.
8434 (this is not to say that it necessarily refers to a complex
8435 symbol; merely that it is a self-describing CGEN based reloc.
8436 i.e. the addend has the complete reloc information (bit start, end,
8437 word size, etc) encoded within it.). */
8438
8439 decode_complex_addend (&start, &oplen, &len, &wordsz,
8440 &chunksz, &lsb0_p, &signed_p,
8441 &trunc_p, rel->r_addend);
8442
8443 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8444
8445 if (lsb0_p)
8446 shift = (start + 1) - len;
8447 else
8448 shift = (8 * wordsz) - (start + len);
8449
8450 x = get_value (wordsz, chunksz, input_bfd,
8451 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8452
8453 #ifdef DEBUG
8454 printf ("Doing complex reloc: "
8455 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8456 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8457 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8458 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8459 oplen, (unsigned long) x, (unsigned long) mask,
8460 (unsigned long) relocation);
8461 #endif
8462
8463 r = bfd_reloc_ok;
8464 if (! trunc_p)
8465 /* Now do an overflow check. */
8466 r = bfd_check_overflow ((signed_p
8467 ? complain_overflow_signed
8468 : complain_overflow_unsigned),
8469 len, 0, (8 * wordsz),
8470 relocation);
8471
8472 /* Do the deed. */
8473 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8474
8475 #ifdef DEBUG
8476 printf (" relocation: %8.8lx\n"
8477 " shifted mask: %8.8lx\n"
8478 " shifted/masked reloc: %8.8lx\n"
8479 " result: %8.8lx\n",
8480 (unsigned long) relocation, (unsigned long) (mask << shift),
8481 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8482 #endif
8483 put_value (wordsz, chunksz, input_bfd, x,
8484 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8485 return r;
8486 }
8487
8488 /* Functions to read r_offset from external (target order) reloc
8489 entry. Faster than bfd_getl32 et al, because we let the compiler
8490 know the value is aligned. */
8491
8492 static bfd_vma
8493 ext32l_r_offset (const void *p)
8494 {
8495 union aligned32
8496 {
8497 uint32_t v;
8498 unsigned char c[4];
8499 };
8500 const union aligned32 *a
8501 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8502
8503 uint32_t aval = ( (uint32_t) a->c[0]
8504 | (uint32_t) a->c[1] << 8
8505 | (uint32_t) a->c[2] << 16
8506 | (uint32_t) a->c[3] << 24);
8507 return aval;
8508 }
8509
8510 static bfd_vma
8511 ext32b_r_offset (const void *p)
8512 {
8513 union aligned32
8514 {
8515 uint32_t v;
8516 unsigned char c[4];
8517 };
8518 const union aligned32 *a
8519 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8520
8521 uint32_t aval = ( (uint32_t) a->c[0] << 24
8522 | (uint32_t) a->c[1] << 16
8523 | (uint32_t) a->c[2] << 8
8524 | (uint32_t) a->c[3]);
8525 return aval;
8526 }
8527
8528 #ifdef BFD_HOST_64_BIT
8529 static bfd_vma
8530 ext64l_r_offset (const void *p)
8531 {
8532 union aligned64
8533 {
8534 uint64_t v;
8535 unsigned char c[8];
8536 };
8537 const union aligned64 *a
8538 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8539
8540 uint64_t aval = ( (uint64_t) a->c[0]
8541 | (uint64_t) a->c[1] << 8
8542 | (uint64_t) a->c[2] << 16
8543 | (uint64_t) a->c[3] << 24
8544 | (uint64_t) a->c[4] << 32
8545 | (uint64_t) a->c[5] << 40
8546 | (uint64_t) a->c[6] << 48
8547 | (uint64_t) a->c[7] << 56);
8548 return aval;
8549 }
8550
8551 static bfd_vma
8552 ext64b_r_offset (const void *p)
8553 {
8554 union aligned64
8555 {
8556 uint64_t v;
8557 unsigned char c[8];
8558 };
8559 const union aligned64 *a
8560 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8561
8562 uint64_t aval = ( (uint64_t) a->c[0] << 56
8563 | (uint64_t) a->c[1] << 48
8564 | (uint64_t) a->c[2] << 40
8565 | (uint64_t) a->c[3] << 32
8566 | (uint64_t) a->c[4] << 24
8567 | (uint64_t) a->c[5] << 16
8568 | (uint64_t) a->c[6] << 8
8569 | (uint64_t) a->c[7]);
8570 return aval;
8571 }
8572 #endif
8573
8574 /* When performing a relocatable link, the input relocations are
8575 preserved. But, if they reference global symbols, the indices
8576 referenced must be updated. Update all the relocations found in
8577 RELDATA. */
8578
8579 static bfd_boolean
8580 elf_link_adjust_relocs (bfd *abfd,
8581 asection *sec,
8582 struct bfd_elf_section_reloc_data *reldata,
8583 bfd_boolean sort,
8584 struct bfd_link_info *info)
8585 {
8586 unsigned int i;
8587 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8588 bfd_byte *erela;
8589 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8590 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8591 bfd_vma r_type_mask;
8592 int r_sym_shift;
8593 unsigned int count = reldata->count;
8594 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8595
8596 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8597 {
8598 swap_in = bed->s->swap_reloc_in;
8599 swap_out = bed->s->swap_reloc_out;
8600 }
8601 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8602 {
8603 swap_in = bed->s->swap_reloca_in;
8604 swap_out = bed->s->swap_reloca_out;
8605 }
8606 else
8607 abort ();
8608
8609 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8610 abort ();
8611
8612 if (bed->s->arch_size == 32)
8613 {
8614 r_type_mask = 0xff;
8615 r_sym_shift = 8;
8616 }
8617 else
8618 {
8619 r_type_mask = 0xffffffff;
8620 r_sym_shift = 32;
8621 }
8622
8623 erela = reldata->hdr->contents;
8624 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8625 {
8626 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8627 unsigned int j;
8628
8629 if (*rel_hash == NULL)
8630 continue;
8631
8632 if ((*rel_hash)->indx == -2
8633 && info->gc_sections
8634 && ! info->gc_keep_exported)
8635 {
8636 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
8637 _bfd_error_handler (_("%B:%A: error: relocation references symbol %s which was removed by garbage collection."),
8638 abfd, sec,
8639 (*rel_hash)->root.root.string);
8640 _bfd_error_handler (_("%B:%A: error: try relinking with --gc-keep-exported enabled."),
8641 abfd, sec);
8642 bfd_set_error (bfd_error_invalid_operation);
8643 return FALSE;
8644 }
8645 BFD_ASSERT ((*rel_hash)->indx >= 0);
8646
8647 (*swap_in) (abfd, erela, irela);
8648 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8649 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8650 | (irela[j].r_info & r_type_mask));
8651 (*swap_out) (abfd, irela, erela);
8652 }
8653
8654 if (bed->elf_backend_update_relocs)
8655 (*bed->elf_backend_update_relocs) (sec, reldata);
8656
8657 if (sort && count != 0)
8658 {
8659 bfd_vma (*ext_r_off) (const void *);
8660 bfd_vma r_off;
8661 size_t elt_size;
8662 bfd_byte *base, *end, *p, *loc;
8663 bfd_byte *buf = NULL;
8664
8665 if (bed->s->arch_size == 32)
8666 {
8667 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8668 ext_r_off = ext32l_r_offset;
8669 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8670 ext_r_off = ext32b_r_offset;
8671 else
8672 abort ();
8673 }
8674 else
8675 {
8676 #ifdef BFD_HOST_64_BIT
8677 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8678 ext_r_off = ext64l_r_offset;
8679 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8680 ext_r_off = ext64b_r_offset;
8681 else
8682 #endif
8683 abort ();
8684 }
8685
8686 /* Must use a stable sort here. A modified insertion sort,
8687 since the relocs are mostly sorted already. */
8688 elt_size = reldata->hdr->sh_entsize;
8689 base = reldata->hdr->contents;
8690 end = base + count * elt_size;
8691 if (elt_size > sizeof (Elf64_External_Rela))
8692 abort ();
8693
8694 /* Ensure the first element is lowest. This acts as a sentinel,
8695 speeding the main loop below. */
8696 r_off = (*ext_r_off) (base);
8697 for (p = loc = base; (p += elt_size) < end; )
8698 {
8699 bfd_vma r_off2 = (*ext_r_off) (p);
8700 if (r_off > r_off2)
8701 {
8702 r_off = r_off2;
8703 loc = p;
8704 }
8705 }
8706 if (loc != base)
8707 {
8708 /* Don't just swap *base and *loc as that changes the order
8709 of the original base[0] and base[1] if they happen to
8710 have the same r_offset. */
8711 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8712 memcpy (onebuf, loc, elt_size);
8713 memmove (base + elt_size, base, loc - base);
8714 memcpy (base, onebuf, elt_size);
8715 }
8716
8717 for (p = base + elt_size; (p += elt_size) < end; )
8718 {
8719 /* base to p is sorted, *p is next to insert. */
8720 r_off = (*ext_r_off) (p);
8721 /* Search the sorted region for location to insert. */
8722 loc = p - elt_size;
8723 while (r_off < (*ext_r_off) (loc))
8724 loc -= elt_size;
8725 loc += elt_size;
8726 if (loc != p)
8727 {
8728 /* Chances are there is a run of relocs to insert here,
8729 from one of more input files. Files are not always
8730 linked in order due to the way elf_link_input_bfd is
8731 called. See pr17666. */
8732 size_t sortlen = p - loc;
8733 bfd_vma r_off2 = (*ext_r_off) (loc);
8734 size_t runlen = elt_size;
8735 size_t buf_size = 96 * 1024;
8736 while (p + runlen < end
8737 && (sortlen <= buf_size
8738 || runlen + elt_size <= buf_size)
8739 && r_off2 > (*ext_r_off) (p + runlen))
8740 runlen += elt_size;
8741 if (buf == NULL)
8742 {
8743 buf = bfd_malloc (buf_size);
8744 if (buf == NULL)
8745 return FALSE;
8746 }
8747 if (runlen < sortlen)
8748 {
8749 memcpy (buf, p, runlen);
8750 memmove (loc + runlen, loc, sortlen);
8751 memcpy (loc, buf, runlen);
8752 }
8753 else
8754 {
8755 memcpy (buf, loc, sortlen);
8756 memmove (loc, p, runlen);
8757 memcpy (loc + runlen, buf, sortlen);
8758 }
8759 p += runlen - elt_size;
8760 }
8761 }
8762 /* Hashes are no longer valid. */
8763 free (reldata->hashes);
8764 reldata->hashes = NULL;
8765 free (buf);
8766 }
8767 return TRUE;
8768 }
8769
8770 struct elf_link_sort_rela
8771 {
8772 union {
8773 bfd_vma offset;
8774 bfd_vma sym_mask;
8775 } u;
8776 enum elf_reloc_type_class type;
8777 /* We use this as an array of size int_rels_per_ext_rel. */
8778 Elf_Internal_Rela rela[1];
8779 };
8780
8781 static int
8782 elf_link_sort_cmp1 (const void *A, const void *B)
8783 {
8784 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8785 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8786 int relativea, relativeb;
8787
8788 relativea = a->type == reloc_class_relative;
8789 relativeb = b->type == reloc_class_relative;
8790
8791 if (relativea < relativeb)
8792 return 1;
8793 if (relativea > relativeb)
8794 return -1;
8795 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8796 return -1;
8797 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8798 return 1;
8799 if (a->rela->r_offset < b->rela->r_offset)
8800 return -1;
8801 if (a->rela->r_offset > b->rela->r_offset)
8802 return 1;
8803 return 0;
8804 }
8805
8806 static int
8807 elf_link_sort_cmp2 (const void *A, const void *B)
8808 {
8809 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8810 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8811
8812 if (a->type < b->type)
8813 return -1;
8814 if (a->type > b->type)
8815 return 1;
8816 if (a->u.offset < b->u.offset)
8817 return -1;
8818 if (a->u.offset > b->u.offset)
8819 return 1;
8820 if (a->rela->r_offset < b->rela->r_offset)
8821 return -1;
8822 if (a->rela->r_offset > b->rela->r_offset)
8823 return 1;
8824 return 0;
8825 }
8826
8827 static size_t
8828 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8829 {
8830 asection *dynamic_relocs;
8831 asection *rela_dyn;
8832 asection *rel_dyn;
8833 bfd_size_type count, size;
8834 size_t i, ret, sort_elt, ext_size;
8835 bfd_byte *sort, *s_non_relative, *p;
8836 struct elf_link_sort_rela *sq;
8837 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8838 int i2e = bed->s->int_rels_per_ext_rel;
8839 unsigned int opb = bfd_octets_per_byte (abfd);
8840 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8841 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8842 struct bfd_link_order *lo;
8843 bfd_vma r_sym_mask;
8844 bfd_boolean use_rela;
8845
8846 /* Find a dynamic reloc section. */
8847 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8848 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8849 if (rela_dyn != NULL && rela_dyn->size > 0
8850 && rel_dyn != NULL && rel_dyn->size > 0)
8851 {
8852 bfd_boolean use_rela_initialised = FALSE;
8853
8854 /* This is just here to stop gcc from complaining.
8855 Its initialization checking code is not perfect. */
8856 use_rela = TRUE;
8857
8858 /* Both sections are present. Examine the sizes
8859 of the indirect sections to help us choose. */
8860 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8861 if (lo->type == bfd_indirect_link_order)
8862 {
8863 asection *o = lo->u.indirect.section;
8864
8865 if ((o->size % bed->s->sizeof_rela) == 0)
8866 {
8867 if ((o->size % bed->s->sizeof_rel) == 0)
8868 /* Section size is divisible by both rel and rela sizes.
8869 It is of no help to us. */
8870 ;
8871 else
8872 {
8873 /* Section size is only divisible by rela. */
8874 if (use_rela_initialised && !use_rela)
8875 {
8876 _bfd_error_handler (_("%B: Unable to sort relocs - "
8877 "they are in more than one size"),
8878 abfd);
8879 bfd_set_error (bfd_error_invalid_operation);
8880 return 0;
8881 }
8882 else
8883 {
8884 use_rela = TRUE;
8885 use_rela_initialised = TRUE;
8886 }
8887 }
8888 }
8889 else if ((o->size % bed->s->sizeof_rel) == 0)
8890 {
8891 /* Section size is only divisible by rel. */
8892 if (use_rela_initialised && use_rela)
8893 {
8894 _bfd_error_handler (_("%B: Unable to sort relocs - "
8895 "they are in more than one size"),
8896 abfd);
8897 bfd_set_error (bfd_error_invalid_operation);
8898 return 0;
8899 }
8900 else
8901 {
8902 use_rela = FALSE;
8903 use_rela_initialised = TRUE;
8904 }
8905 }
8906 else
8907 {
8908 /* The section size is not divisible by either -
8909 something is wrong. */
8910 _bfd_error_handler (_("%B: Unable to sort relocs - "
8911 "they are of an unknown size"), abfd);
8912 bfd_set_error (bfd_error_invalid_operation);
8913 return 0;
8914 }
8915 }
8916
8917 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8918 if (lo->type == bfd_indirect_link_order)
8919 {
8920 asection *o = lo->u.indirect.section;
8921
8922 if ((o->size % bed->s->sizeof_rela) == 0)
8923 {
8924 if ((o->size % bed->s->sizeof_rel) == 0)
8925 /* Section size is divisible by both rel and rela sizes.
8926 It is of no help to us. */
8927 ;
8928 else
8929 {
8930 /* Section size is only divisible by rela. */
8931 if (use_rela_initialised && !use_rela)
8932 {
8933 _bfd_error_handler (_("%B: Unable to sort relocs - "
8934 "they are in more than one size"),
8935 abfd);
8936 bfd_set_error (bfd_error_invalid_operation);
8937 return 0;
8938 }
8939 else
8940 {
8941 use_rela = TRUE;
8942 use_rela_initialised = TRUE;
8943 }
8944 }
8945 }
8946 else if ((o->size % bed->s->sizeof_rel) == 0)
8947 {
8948 /* Section size is only divisible by rel. */
8949 if (use_rela_initialised && use_rela)
8950 {
8951 _bfd_error_handler (_("%B: Unable to sort relocs - "
8952 "they are in more than one size"),
8953 abfd);
8954 bfd_set_error (bfd_error_invalid_operation);
8955 return 0;
8956 }
8957 else
8958 {
8959 use_rela = FALSE;
8960 use_rela_initialised = TRUE;
8961 }
8962 }
8963 else
8964 {
8965 /* The section size is not divisible by either -
8966 something is wrong. */
8967 _bfd_error_handler (_("%B: Unable to sort relocs - "
8968 "they are of an unknown size"), abfd);
8969 bfd_set_error (bfd_error_invalid_operation);
8970 return 0;
8971 }
8972 }
8973
8974 if (! use_rela_initialised)
8975 /* Make a guess. */
8976 use_rela = TRUE;
8977 }
8978 else if (rela_dyn != NULL && rela_dyn->size > 0)
8979 use_rela = TRUE;
8980 else if (rel_dyn != NULL && rel_dyn->size > 0)
8981 use_rela = FALSE;
8982 else
8983 return 0;
8984
8985 if (use_rela)
8986 {
8987 dynamic_relocs = rela_dyn;
8988 ext_size = bed->s->sizeof_rela;
8989 swap_in = bed->s->swap_reloca_in;
8990 swap_out = bed->s->swap_reloca_out;
8991 }
8992 else
8993 {
8994 dynamic_relocs = rel_dyn;
8995 ext_size = bed->s->sizeof_rel;
8996 swap_in = bed->s->swap_reloc_in;
8997 swap_out = bed->s->swap_reloc_out;
8998 }
8999
9000 size = 0;
9001 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9002 if (lo->type == bfd_indirect_link_order)
9003 size += lo->u.indirect.section->size;
9004
9005 if (size != dynamic_relocs->size)
9006 return 0;
9007
9008 sort_elt = (sizeof (struct elf_link_sort_rela)
9009 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9010
9011 count = dynamic_relocs->size / ext_size;
9012 if (count == 0)
9013 return 0;
9014 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9015
9016 if (sort == NULL)
9017 {
9018 (*info->callbacks->warning)
9019 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
9020 return 0;
9021 }
9022
9023 if (bed->s->arch_size == 32)
9024 r_sym_mask = ~(bfd_vma) 0xff;
9025 else
9026 r_sym_mask = ~(bfd_vma) 0xffffffff;
9027
9028 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9029 if (lo->type == bfd_indirect_link_order)
9030 {
9031 bfd_byte *erel, *erelend;
9032 asection *o = lo->u.indirect.section;
9033
9034 if (o->contents == NULL && o->size != 0)
9035 {
9036 /* This is a reloc section that is being handled as a normal
9037 section. See bfd_section_from_shdr. We can't combine
9038 relocs in this case. */
9039 free (sort);
9040 return 0;
9041 }
9042 erel = o->contents;
9043 erelend = o->contents + o->size;
9044 p = sort + o->output_offset * opb / ext_size * sort_elt;
9045
9046 while (erel < erelend)
9047 {
9048 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9049
9050 (*swap_in) (abfd, erel, s->rela);
9051 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9052 s->u.sym_mask = r_sym_mask;
9053 p += sort_elt;
9054 erel += ext_size;
9055 }
9056 }
9057
9058 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9059
9060 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9061 {
9062 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9063 if (s->type != reloc_class_relative)
9064 break;
9065 }
9066 ret = i;
9067 s_non_relative = p;
9068
9069 sq = (struct elf_link_sort_rela *) s_non_relative;
9070 for (; i < count; i++, p += sort_elt)
9071 {
9072 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9073 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9074 sq = sp;
9075 sp->u.offset = sq->rela->r_offset;
9076 }
9077
9078 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9079
9080 struct elf_link_hash_table *htab = elf_hash_table (info);
9081 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9082 {
9083 /* We have plt relocs in .rela.dyn. */
9084 sq = (struct elf_link_sort_rela *) sort;
9085 for (i = 0; i < count; i++)
9086 if (sq[count - i - 1].type != reloc_class_plt)
9087 break;
9088 if (i != 0 && htab->srelplt->size == i * ext_size)
9089 {
9090 struct bfd_link_order **plo;
9091 /* Put srelplt link_order last. This is so the output_offset
9092 set in the next loop is correct for DT_JMPREL. */
9093 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9094 if ((*plo)->type == bfd_indirect_link_order
9095 && (*plo)->u.indirect.section == htab->srelplt)
9096 {
9097 lo = *plo;
9098 *plo = lo->next;
9099 }
9100 else
9101 plo = &(*plo)->next;
9102 *plo = lo;
9103 lo->next = NULL;
9104 dynamic_relocs->map_tail.link_order = lo;
9105 }
9106 }
9107
9108 p = sort;
9109 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9110 if (lo->type == bfd_indirect_link_order)
9111 {
9112 bfd_byte *erel, *erelend;
9113 asection *o = lo->u.indirect.section;
9114
9115 erel = o->contents;
9116 erelend = o->contents + o->size;
9117 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9118 while (erel < erelend)
9119 {
9120 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9121 (*swap_out) (abfd, s->rela, erel);
9122 p += sort_elt;
9123 erel += ext_size;
9124 }
9125 }
9126
9127 free (sort);
9128 *psec = dynamic_relocs;
9129 return ret;
9130 }
9131
9132 /* Add a symbol to the output symbol string table. */
9133
9134 static int
9135 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9136 const char *name,
9137 Elf_Internal_Sym *elfsym,
9138 asection *input_sec,
9139 struct elf_link_hash_entry *h)
9140 {
9141 int (*output_symbol_hook)
9142 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9143 struct elf_link_hash_entry *);
9144 struct elf_link_hash_table *hash_table;
9145 const struct elf_backend_data *bed;
9146 bfd_size_type strtabsize;
9147
9148 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9149
9150 bed = get_elf_backend_data (flinfo->output_bfd);
9151 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9152 if (output_symbol_hook != NULL)
9153 {
9154 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9155 if (ret != 1)
9156 return ret;
9157 }
9158
9159 if (name == NULL
9160 || *name == '\0'
9161 || (input_sec->flags & SEC_EXCLUDE))
9162 elfsym->st_name = (unsigned long) -1;
9163 else
9164 {
9165 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9166 to get the final offset for st_name. */
9167 elfsym->st_name
9168 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9169 name, FALSE);
9170 if (elfsym->st_name == (unsigned long) -1)
9171 return 0;
9172 }
9173
9174 hash_table = elf_hash_table (flinfo->info);
9175 strtabsize = hash_table->strtabsize;
9176 if (strtabsize <= hash_table->strtabcount)
9177 {
9178 strtabsize += strtabsize;
9179 hash_table->strtabsize = strtabsize;
9180 strtabsize *= sizeof (*hash_table->strtab);
9181 hash_table->strtab
9182 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9183 strtabsize);
9184 if (hash_table->strtab == NULL)
9185 return 0;
9186 }
9187 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9188 hash_table->strtab[hash_table->strtabcount].dest_index
9189 = hash_table->strtabcount;
9190 hash_table->strtab[hash_table->strtabcount].destshndx_index
9191 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9192
9193 bfd_get_symcount (flinfo->output_bfd) += 1;
9194 hash_table->strtabcount += 1;
9195
9196 return 1;
9197 }
9198
9199 /* Swap symbols out to the symbol table and flush the output symbols to
9200 the file. */
9201
9202 static bfd_boolean
9203 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9204 {
9205 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9206 bfd_size_type amt;
9207 size_t i;
9208 const struct elf_backend_data *bed;
9209 bfd_byte *symbuf;
9210 Elf_Internal_Shdr *hdr;
9211 file_ptr pos;
9212 bfd_boolean ret;
9213
9214 if (!hash_table->strtabcount)
9215 return TRUE;
9216
9217 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9218
9219 bed = get_elf_backend_data (flinfo->output_bfd);
9220
9221 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9222 symbuf = (bfd_byte *) bfd_malloc (amt);
9223 if (symbuf == NULL)
9224 return FALSE;
9225
9226 if (flinfo->symshndxbuf)
9227 {
9228 amt = sizeof (Elf_External_Sym_Shndx);
9229 amt *= bfd_get_symcount (flinfo->output_bfd);
9230 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9231 if (flinfo->symshndxbuf == NULL)
9232 {
9233 free (symbuf);
9234 return FALSE;
9235 }
9236 }
9237
9238 for (i = 0; i < hash_table->strtabcount; i++)
9239 {
9240 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9241 if (elfsym->sym.st_name == (unsigned long) -1)
9242 elfsym->sym.st_name = 0;
9243 else
9244 elfsym->sym.st_name
9245 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9246 elfsym->sym.st_name);
9247 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9248 ((bfd_byte *) symbuf
9249 + (elfsym->dest_index
9250 * bed->s->sizeof_sym)),
9251 (flinfo->symshndxbuf
9252 + elfsym->destshndx_index));
9253 }
9254
9255 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9256 pos = hdr->sh_offset + hdr->sh_size;
9257 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9258 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9259 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9260 {
9261 hdr->sh_size += amt;
9262 ret = TRUE;
9263 }
9264 else
9265 ret = FALSE;
9266
9267 free (symbuf);
9268
9269 free (hash_table->strtab);
9270 hash_table->strtab = NULL;
9271
9272 return ret;
9273 }
9274
9275 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9276
9277 static bfd_boolean
9278 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9279 {
9280 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9281 && sym->st_shndx < SHN_LORESERVE)
9282 {
9283 /* The gABI doesn't support dynamic symbols in output sections
9284 beyond 64k. */
9285 _bfd_error_handler
9286 /* xgettext:c-format */
9287 (_("%B: Too many sections: %d (>= %d)"),
9288 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9289 bfd_set_error (bfd_error_nonrepresentable_section);
9290 return FALSE;
9291 }
9292 return TRUE;
9293 }
9294
9295 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9296 allowing an unsatisfied unversioned symbol in the DSO to match a
9297 versioned symbol that would normally require an explicit version.
9298 We also handle the case that a DSO references a hidden symbol
9299 which may be satisfied by a versioned symbol in another DSO. */
9300
9301 static bfd_boolean
9302 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9303 const struct elf_backend_data *bed,
9304 struct elf_link_hash_entry *h)
9305 {
9306 bfd *abfd;
9307 struct elf_link_loaded_list *loaded;
9308
9309 if (!is_elf_hash_table (info->hash))
9310 return FALSE;
9311
9312 /* Check indirect symbol. */
9313 while (h->root.type == bfd_link_hash_indirect)
9314 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9315
9316 switch (h->root.type)
9317 {
9318 default:
9319 abfd = NULL;
9320 break;
9321
9322 case bfd_link_hash_undefined:
9323 case bfd_link_hash_undefweak:
9324 abfd = h->root.u.undef.abfd;
9325 if (abfd == NULL
9326 || (abfd->flags & DYNAMIC) == 0
9327 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9328 return FALSE;
9329 break;
9330
9331 case bfd_link_hash_defined:
9332 case bfd_link_hash_defweak:
9333 abfd = h->root.u.def.section->owner;
9334 break;
9335
9336 case bfd_link_hash_common:
9337 abfd = h->root.u.c.p->section->owner;
9338 break;
9339 }
9340 BFD_ASSERT (abfd != NULL);
9341
9342 for (loaded = elf_hash_table (info)->loaded;
9343 loaded != NULL;
9344 loaded = loaded->next)
9345 {
9346 bfd *input;
9347 Elf_Internal_Shdr *hdr;
9348 size_t symcount;
9349 size_t extsymcount;
9350 size_t extsymoff;
9351 Elf_Internal_Shdr *versymhdr;
9352 Elf_Internal_Sym *isym;
9353 Elf_Internal_Sym *isymend;
9354 Elf_Internal_Sym *isymbuf;
9355 Elf_External_Versym *ever;
9356 Elf_External_Versym *extversym;
9357
9358 input = loaded->abfd;
9359
9360 /* We check each DSO for a possible hidden versioned definition. */
9361 if (input == abfd
9362 || (input->flags & DYNAMIC) == 0
9363 || elf_dynversym (input) == 0)
9364 continue;
9365
9366 hdr = &elf_tdata (input)->dynsymtab_hdr;
9367
9368 symcount = hdr->sh_size / bed->s->sizeof_sym;
9369 if (elf_bad_symtab (input))
9370 {
9371 extsymcount = symcount;
9372 extsymoff = 0;
9373 }
9374 else
9375 {
9376 extsymcount = symcount - hdr->sh_info;
9377 extsymoff = hdr->sh_info;
9378 }
9379
9380 if (extsymcount == 0)
9381 continue;
9382
9383 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9384 NULL, NULL, NULL);
9385 if (isymbuf == NULL)
9386 return FALSE;
9387
9388 /* Read in any version definitions. */
9389 versymhdr = &elf_tdata (input)->dynversym_hdr;
9390 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
9391 if (extversym == NULL)
9392 goto error_ret;
9393
9394 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9395 || (bfd_bread (extversym, versymhdr->sh_size, input)
9396 != versymhdr->sh_size))
9397 {
9398 free (extversym);
9399 error_ret:
9400 free (isymbuf);
9401 return FALSE;
9402 }
9403
9404 ever = extversym + extsymoff;
9405 isymend = isymbuf + extsymcount;
9406 for (isym = isymbuf; isym < isymend; isym++, ever++)
9407 {
9408 const char *name;
9409 Elf_Internal_Versym iver;
9410 unsigned short version_index;
9411
9412 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9413 || isym->st_shndx == SHN_UNDEF)
9414 continue;
9415
9416 name = bfd_elf_string_from_elf_section (input,
9417 hdr->sh_link,
9418 isym->st_name);
9419 if (strcmp (name, h->root.root.string) != 0)
9420 continue;
9421
9422 _bfd_elf_swap_versym_in (input, ever, &iver);
9423
9424 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9425 && !(h->def_regular
9426 && h->forced_local))
9427 {
9428 /* If we have a non-hidden versioned sym, then it should
9429 have provided a definition for the undefined sym unless
9430 it is defined in a non-shared object and forced local.
9431 */
9432 abort ();
9433 }
9434
9435 version_index = iver.vs_vers & VERSYM_VERSION;
9436 if (version_index == 1 || version_index == 2)
9437 {
9438 /* This is the base or first version. We can use it. */
9439 free (extversym);
9440 free (isymbuf);
9441 return TRUE;
9442 }
9443 }
9444
9445 free (extversym);
9446 free (isymbuf);
9447 }
9448
9449 return FALSE;
9450 }
9451
9452 /* Convert ELF common symbol TYPE. */
9453
9454 static int
9455 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9456 {
9457 /* Commom symbol can only appear in relocatable link. */
9458 if (!bfd_link_relocatable (info))
9459 abort ();
9460 switch (info->elf_stt_common)
9461 {
9462 case unchanged:
9463 break;
9464 case elf_stt_common:
9465 type = STT_COMMON;
9466 break;
9467 case no_elf_stt_common:
9468 type = STT_OBJECT;
9469 break;
9470 }
9471 return type;
9472 }
9473
9474 /* Add an external symbol to the symbol table. This is called from
9475 the hash table traversal routine. When generating a shared object,
9476 we go through the symbol table twice. The first time we output
9477 anything that might have been forced to local scope in a version
9478 script. The second time we output the symbols that are still
9479 global symbols. */
9480
9481 static bfd_boolean
9482 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9483 {
9484 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9485 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9486 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9487 bfd_boolean strip;
9488 Elf_Internal_Sym sym;
9489 asection *input_sec;
9490 const struct elf_backend_data *bed;
9491 long indx;
9492 int ret;
9493 unsigned int type;
9494
9495 if (h->root.type == bfd_link_hash_warning)
9496 {
9497 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9498 if (h->root.type == bfd_link_hash_new)
9499 return TRUE;
9500 }
9501
9502 /* Decide whether to output this symbol in this pass. */
9503 if (eoinfo->localsyms)
9504 {
9505 if (!h->forced_local)
9506 return TRUE;
9507 }
9508 else
9509 {
9510 if (h->forced_local)
9511 return TRUE;
9512 }
9513
9514 bed = get_elf_backend_data (flinfo->output_bfd);
9515
9516 if (h->root.type == bfd_link_hash_undefined)
9517 {
9518 /* If we have an undefined symbol reference here then it must have
9519 come from a shared library that is being linked in. (Undefined
9520 references in regular files have already been handled unless
9521 they are in unreferenced sections which are removed by garbage
9522 collection). */
9523 bfd_boolean ignore_undef = FALSE;
9524
9525 /* Some symbols may be special in that the fact that they're
9526 undefined can be safely ignored - let backend determine that. */
9527 if (bed->elf_backend_ignore_undef_symbol)
9528 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9529
9530 /* If we are reporting errors for this situation then do so now. */
9531 if (!ignore_undef
9532 && h->ref_dynamic
9533 && (!h->ref_regular || flinfo->info->gc_sections)
9534 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9535 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9536 (*flinfo->info->callbacks->undefined_symbol)
9537 (flinfo->info, h->root.root.string,
9538 h->ref_regular ? NULL : h->root.u.undef.abfd,
9539 NULL, 0,
9540 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
9541
9542 /* Strip a global symbol defined in a discarded section. */
9543 if (h->indx == -3)
9544 return TRUE;
9545 }
9546
9547 /* We should also warn if a forced local symbol is referenced from
9548 shared libraries. */
9549 if (bfd_link_executable (flinfo->info)
9550 && h->forced_local
9551 && h->ref_dynamic
9552 && h->def_regular
9553 && !h->dynamic_def
9554 && h->ref_dynamic_nonweak
9555 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9556 {
9557 bfd *def_bfd;
9558 const char *msg;
9559 struct elf_link_hash_entry *hi = h;
9560
9561 /* Check indirect symbol. */
9562 while (hi->root.type == bfd_link_hash_indirect)
9563 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9564
9565 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9566 /* xgettext:c-format */
9567 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9568 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9569 /* xgettext:c-format */
9570 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9571 else
9572 /* xgettext:c-format */
9573 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
9574 def_bfd = flinfo->output_bfd;
9575 if (hi->root.u.def.section != bfd_abs_section_ptr)
9576 def_bfd = hi->root.u.def.section->owner;
9577 _bfd_error_handler (msg, flinfo->output_bfd,
9578 h->root.root.string, def_bfd);
9579 bfd_set_error (bfd_error_bad_value);
9580 eoinfo->failed = TRUE;
9581 return FALSE;
9582 }
9583
9584 /* We don't want to output symbols that have never been mentioned by
9585 a regular file, or that we have been told to strip. However, if
9586 h->indx is set to -2, the symbol is used by a reloc and we must
9587 output it. */
9588 strip = FALSE;
9589 if (h->indx == -2)
9590 ;
9591 else if ((h->def_dynamic
9592 || h->ref_dynamic
9593 || h->root.type == bfd_link_hash_new)
9594 && !h->def_regular
9595 && !h->ref_regular)
9596 strip = TRUE;
9597 else if (flinfo->info->strip == strip_all)
9598 strip = TRUE;
9599 else if (flinfo->info->strip == strip_some
9600 && bfd_hash_lookup (flinfo->info->keep_hash,
9601 h->root.root.string, FALSE, FALSE) == NULL)
9602 strip = TRUE;
9603 else if ((h->root.type == bfd_link_hash_defined
9604 || h->root.type == bfd_link_hash_defweak)
9605 && ((flinfo->info->strip_discarded
9606 && discarded_section (h->root.u.def.section))
9607 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9608 && h->root.u.def.section->owner != NULL
9609 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9610 strip = TRUE;
9611 else if ((h->root.type == bfd_link_hash_undefined
9612 || h->root.type == bfd_link_hash_undefweak)
9613 && h->root.u.undef.abfd != NULL
9614 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9615 strip = TRUE;
9616
9617 type = h->type;
9618
9619 /* If we're stripping it, and it's not a dynamic symbol, there's
9620 nothing else to do. However, if it is a forced local symbol or
9621 an ifunc symbol we need to give the backend finish_dynamic_symbol
9622 function a chance to make it dynamic. */
9623 if (strip
9624 && h->dynindx == -1
9625 && type != STT_GNU_IFUNC
9626 && !h->forced_local)
9627 return TRUE;
9628
9629 sym.st_value = 0;
9630 sym.st_size = h->size;
9631 sym.st_other = h->other;
9632 switch (h->root.type)
9633 {
9634 default:
9635 case bfd_link_hash_new:
9636 case bfd_link_hash_warning:
9637 abort ();
9638 return FALSE;
9639
9640 case bfd_link_hash_undefined:
9641 case bfd_link_hash_undefweak:
9642 input_sec = bfd_und_section_ptr;
9643 sym.st_shndx = SHN_UNDEF;
9644 break;
9645
9646 case bfd_link_hash_defined:
9647 case bfd_link_hash_defweak:
9648 {
9649 input_sec = h->root.u.def.section;
9650 if (input_sec->output_section != NULL)
9651 {
9652 sym.st_shndx =
9653 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9654 input_sec->output_section);
9655 if (sym.st_shndx == SHN_BAD)
9656 {
9657 _bfd_error_handler
9658 /* xgettext:c-format */
9659 (_("%B: could not find output section %A for input section %A"),
9660 flinfo->output_bfd, input_sec->output_section, input_sec);
9661 bfd_set_error (bfd_error_nonrepresentable_section);
9662 eoinfo->failed = TRUE;
9663 return FALSE;
9664 }
9665
9666 /* ELF symbols in relocatable files are section relative,
9667 but in nonrelocatable files they are virtual
9668 addresses. */
9669 sym.st_value = h->root.u.def.value + input_sec->output_offset;
9670 if (!bfd_link_relocatable (flinfo->info))
9671 {
9672 sym.st_value += input_sec->output_section->vma;
9673 if (h->type == STT_TLS)
9674 {
9675 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9676 if (tls_sec != NULL)
9677 sym.st_value -= tls_sec->vma;
9678 }
9679 }
9680 }
9681 else
9682 {
9683 BFD_ASSERT (input_sec->owner == NULL
9684 || (input_sec->owner->flags & DYNAMIC) != 0);
9685 sym.st_shndx = SHN_UNDEF;
9686 input_sec = bfd_und_section_ptr;
9687 }
9688 }
9689 break;
9690
9691 case bfd_link_hash_common:
9692 input_sec = h->root.u.c.p->section;
9693 sym.st_shndx = bed->common_section_index (input_sec);
9694 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9695 break;
9696
9697 case bfd_link_hash_indirect:
9698 /* These symbols are created by symbol versioning. They point
9699 to the decorated version of the name. For example, if the
9700 symbol foo@@GNU_1.2 is the default, which should be used when
9701 foo is used with no version, then we add an indirect symbol
9702 foo which points to foo@@GNU_1.2. We ignore these symbols,
9703 since the indirected symbol is already in the hash table. */
9704 return TRUE;
9705 }
9706
9707 if (type == STT_COMMON || type == STT_OBJECT)
9708 switch (h->root.type)
9709 {
9710 case bfd_link_hash_common:
9711 type = elf_link_convert_common_type (flinfo->info, type);
9712 break;
9713 case bfd_link_hash_defined:
9714 case bfd_link_hash_defweak:
9715 if (bed->common_definition (&sym))
9716 type = elf_link_convert_common_type (flinfo->info, type);
9717 else
9718 type = STT_OBJECT;
9719 break;
9720 case bfd_link_hash_undefined:
9721 case bfd_link_hash_undefweak:
9722 break;
9723 default:
9724 abort ();
9725 }
9726
9727 if (h->forced_local)
9728 {
9729 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9730 /* Turn off visibility on local symbol. */
9731 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9732 }
9733 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9734 else if (h->unique_global && h->def_regular)
9735 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9736 else if (h->root.type == bfd_link_hash_undefweak
9737 || h->root.type == bfd_link_hash_defweak)
9738 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9739 else
9740 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9741 sym.st_target_internal = h->target_internal;
9742
9743 /* Give the processor backend a chance to tweak the symbol value,
9744 and also to finish up anything that needs to be done for this
9745 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9746 forced local syms when non-shared is due to a historical quirk.
9747 STT_GNU_IFUNC symbol must go through PLT. */
9748 if ((h->type == STT_GNU_IFUNC
9749 && h->def_regular
9750 && !bfd_link_relocatable (flinfo->info))
9751 || ((h->dynindx != -1
9752 || h->forced_local)
9753 && ((bfd_link_pic (flinfo->info)
9754 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9755 || h->root.type != bfd_link_hash_undefweak))
9756 || !h->forced_local)
9757 && elf_hash_table (flinfo->info)->dynamic_sections_created))
9758 {
9759 if (! ((*bed->elf_backend_finish_dynamic_symbol)
9760 (flinfo->output_bfd, flinfo->info, h, &sym)))
9761 {
9762 eoinfo->failed = TRUE;
9763 return FALSE;
9764 }
9765 }
9766
9767 /* If we are marking the symbol as undefined, and there are no
9768 non-weak references to this symbol from a regular object, then
9769 mark the symbol as weak undefined; if there are non-weak
9770 references, mark the symbol as strong. We can't do this earlier,
9771 because it might not be marked as undefined until the
9772 finish_dynamic_symbol routine gets through with it. */
9773 if (sym.st_shndx == SHN_UNDEF
9774 && h->ref_regular
9775 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9776 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9777 {
9778 int bindtype;
9779 type = ELF_ST_TYPE (sym.st_info);
9780
9781 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9782 if (type == STT_GNU_IFUNC)
9783 type = STT_FUNC;
9784
9785 if (h->ref_regular_nonweak)
9786 bindtype = STB_GLOBAL;
9787 else
9788 bindtype = STB_WEAK;
9789 sym.st_info = ELF_ST_INFO (bindtype, type);
9790 }
9791
9792 /* If this is a symbol defined in a dynamic library, don't use the
9793 symbol size from the dynamic library. Relinking an executable
9794 against a new library may introduce gratuitous changes in the
9795 executable's symbols if we keep the size. */
9796 if (sym.st_shndx == SHN_UNDEF
9797 && !h->def_regular
9798 && h->def_dynamic)
9799 sym.st_size = 0;
9800
9801 /* If a non-weak symbol with non-default visibility is not defined
9802 locally, it is a fatal error. */
9803 if (!bfd_link_relocatable (flinfo->info)
9804 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9805 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9806 && h->root.type == bfd_link_hash_undefined
9807 && !h->def_regular)
9808 {
9809 const char *msg;
9810
9811 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9812 /* xgettext:c-format */
9813 msg = _("%B: protected symbol `%s' isn't defined");
9814 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9815 /* xgettext:c-format */
9816 msg = _("%B: internal symbol `%s' isn't defined");
9817 else
9818 /* xgettext:c-format */
9819 msg = _("%B: hidden symbol `%s' isn't defined");
9820 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
9821 bfd_set_error (bfd_error_bad_value);
9822 eoinfo->failed = TRUE;
9823 return FALSE;
9824 }
9825
9826 /* If this symbol should be put in the .dynsym section, then put it
9827 there now. We already know the symbol index. We also fill in
9828 the entry in the .hash section. */
9829 if (elf_hash_table (flinfo->info)->dynsym != NULL
9830 && h->dynindx != -1
9831 && elf_hash_table (flinfo->info)->dynamic_sections_created)
9832 {
9833 bfd_byte *esym;
9834
9835 /* Since there is no version information in the dynamic string,
9836 if there is no version info in symbol version section, we will
9837 have a run-time problem if not linking executable, referenced
9838 by shared library, or not bound locally. */
9839 if (h->verinfo.verdef == NULL
9840 && (!bfd_link_executable (flinfo->info)
9841 || h->ref_dynamic
9842 || !h->def_regular))
9843 {
9844 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9845
9846 if (p && p [1] != '\0')
9847 {
9848 _bfd_error_handler
9849 /* xgettext:c-format */
9850 (_("%B: No symbol version section for versioned symbol `%s'"),
9851 flinfo->output_bfd, h->root.root.string);
9852 eoinfo->failed = TRUE;
9853 return FALSE;
9854 }
9855 }
9856
9857 sym.st_name = h->dynstr_index;
9858 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9859 + h->dynindx * bed->s->sizeof_sym);
9860 if (!check_dynsym (flinfo->output_bfd, &sym))
9861 {
9862 eoinfo->failed = TRUE;
9863 return FALSE;
9864 }
9865 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
9866
9867 if (flinfo->hash_sec != NULL)
9868 {
9869 size_t hash_entry_size;
9870 bfd_byte *bucketpos;
9871 bfd_vma chain;
9872 size_t bucketcount;
9873 size_t bucket;
9874
9875 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
9876 bucket = h->u.elf_hash_value % bucketcount;
9877
9878 hash_entry_size
9879 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9880 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
9881 + (bucket + 2) * hash_entry_size);
9882 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9883 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9884 bucketpos);
9885 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9886 ((bfd_byte *) flinfo->hash_sec->contents
9887 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9888 }
9889
9890 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
9891 {
9892 Elf_Internal_Versym iversym;
9893 Elf_External_Versym *eversym;
9894
9895 if (!h->def_regular)
9896 {
9897 if (h->verinfo.verdef == NULL
9898 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9899 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
9900 iversym.vs_vers = 0;
9901 else
9902 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9903 }
9904 else
9905 {
9906 if (h->verinfo.vertree == NULL)
9907 iversym.vs_vers = 1;
9908 else
9909 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
9910 if (flinfo->info->create_default_symver)
9911 iversym.vs_vers++;
9912 }
9913
9914 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9915 defined locally. */
9916 if (h->versioned == versioned_hidden && h->def_regular)
9917 iversym.vs_vers |= VERSYM_HIDDEN;
9918
9919 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
9920 eversym += h->dynindx;
9921 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
9922 }
9923 }
9924
9925 /* If the symbol is undefined, and we didn't output it to .dynsym,
9926 strip it from .symtab too. Obviously we can't do this for
9927 relocatable output or when needed for --emit-relocs. */
9928 else if (input_sec == bfd_und_section_ptr
9929 && h->indx != -2
9930 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
9931 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
9932 && !bfd_link_relocatable (flinfo->info))
9933 return TRUE;
9934
9935 /* Also strip others that we couldn't earlier due to dynamic symbol
9936 processing. */
9937 if (strip)
9938 return TRUE;
9939 if ((input_sec->flags & SEC_EXCLUDE) != 0)
9940 return TRUE;
9941
9942 /* Output a FILE symbol so that following locals are not associated
9943 with the wrong input file. We need one for forced local symbols
9944 if we've seen more than one FILE symbol or when we have exactly
9945 one FILE symbol but global symbols are present in a file other
9946 than the one with the FILE symbol. We also need one if linker
9947 defined symbols are present. In practice these conditions are
9948 always met, so just emit the FILE symbol unconditionally. */
9949 if (eoinfo->localsyms
9950 && !eoinfo->file_sym_done
9951 && eoinfo->flinfo->filesym_count != 0)
9952 {
9953 Elf_Internal_Sym fsym;
9954
9955 memset (&fsym, 0, sizeof (fsym));
9956 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9957 fsym.st_shndx = SHN_ABS;
9958 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9959 bfd_und_section_ptr, NULL))
9960 return FALSE;
9961
9962 eoinfo->file_sym_done = TRUE;
9963 }
9964
9965 indx = bfd_get_symcount (flinfo->output_bfd);
9966 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9967 input_sec, h);
9968 if (ret == 0)
9969 {
9970 eoinfo->failed = TRUE;
9971 return FALSE;
9972 }
9973 else if (ret == 1)
9974 h->indx = indx;
9975 else if (h->indx == -2)
9976 abort();
9977
9978 return TRUE;
9979 }
9980
9981 /* Return TRUE if special handling is done for relocs in SEC against
9982 symbols defined in discarded sections. */
9983
9984 static bfd_boolean
9985 elf_section_ignore_discarded_relocs (asection *sec)
9986 {
9987 const struct elf_backend_data *bed;
9988
9989 switch (sec->sec_info_type)
9990 {
9991 case SEC_INFO_TYPE_STABS:
9992 case SEC_INFO_TYPE_EH_FRAME:
9993 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
9994 return TRUE;
9995 default:
9996 break;
9997 }
9998
9999 bed = get_elf_backend_data (sec->owner);
10000 if (bed->elf_backend_ignore_discarded_relocs != NULL
10001 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10002 return TRUE;
10003
10004 return FALSE;
10005 }
10006
10007 /* Return a mask saying how ld should treat relocations in SEC against
10008 symbols defined in discarded sections. If this function returns
10009 COMPLAIN set, ld will issue a warning message. If this function
10010 returns PRETEND set, and the discarded section was link-once and the
10011 same size as the kept link-once section, ld will pretend that the
10012 symbol was actually defined in the kept section. Otherwise ld will
10013 zero the reloc (at least that is the intent, but some cooperation by
10014 the target dependent code is needed, particularly for REL targets). */
10015
10016 unsigned int
10017 _bfd_elf_default_action_discarded (asection *sec)
10018 {
10019 if (sec->flags & SEC_DEBUGGING)
10020 return PRETEND;
10021
10022 if (strcmp (".eh_frame", sec->name) == 0)
10023 return 0;
10024
10025 if (strcmp (".gcc_except_table", sec->name) == 0)
10026 return 0;
10027
10028 return COMPLAIN | PRETEND;
10029 }
10030
10031 /* Find a match between a section and a member of a section group. */
10032
10033 static asection *
10034 match_group_member (asection *sec, asection *group,
10035 struct bfd_link_info *info)
10036 {
10037 asection *first = elf_next_in_group (group);
10038 asection *s = first;
10039
10040 while (s != NULL)
10041 {
10042 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10043 return s;
10044
10045 s = elf_next_in_group (s);
10046 if (s == first)
10047 break;
10048 }
10049
10050 return NULL;
10051 }
10052
10053 /* Check if the kept section of a discarded section SEC can be used
10054 to replace it. Return the replacement if it is OK. Otherwise return
10055 NULL. */
10056
10057 asection *
10058 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10059 {
10060 asection *kept;
10061
10062 kept = sec->kept_section;
10063 if (kept != NULL)
10064 {
10065 if ((kept->flags & SEC_GROUP) != 0)
10066 kept = match_group_member (sec, kept, info);
10067 if (kept != NULL
10068 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10069 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
10070 kept = NULL;
10071 sec->kept_section = kept;
10072 }
10073 return kept;
10074 }
10075
10076 /* Link an input file into the linker output file. This function
10077 handles all the sections and relocations of the input file at once.
10078 This is so that we only have to read the local symbols once, and
10079 don't have to keep them in memory. */
10080
10081 static bfd_boolean
10082 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
10083 {
10084 int (*relocate_section)
10085 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10086 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10087 bfd *output_bfd;
10088 Elf_Internal_Shdr *symtab_hdr;
10089 size_t locsymcount;
10090 size_t extsymoff;
10091 Elf_Internal_Sym *isymbuf;
10092 Elf_Internal_Sym *isym;
10093 Elf_Internal_Sym *isymend;
10094 long *pindex;
10095 asection **ppsection;
10096 asection *o;
10097 const struct elf_backend_data *bed;
10098 struct elf_link_hash_entry **sym_hashes;
10099 bfd_size_type address_size;
10100 bfd_vma r_type_mask;
10101 int r_sym_shift;
10102 bfd_boolean have_file_sym = FALSE;
10103
10104 output_bfd = flinfo->output_bfd;
10105 bed = get_elf_backend_data (output_bfd);
10106 relocate_section = bed->elf_backend_relocate_section;
10107
10108 /* If this is a dynamic object, we don't want to do anything here:
10109 we don't want the local symbols, and we don't want the section
10110 contents. */
10111 if ((input_bfd->flags & DYNAMIC) != 0)
10112 return TRUE;
10113
10114 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10115 if (elf_bad_symtab (input_bfd))
10116 {
10117 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10118 extsymoff = 0;
10119 }
10120 else
10121 {
10122 locsymcount = symtab_hdr->sh_info;
10123 extsymoff = symtab_hdr->sh_info;
10124 }
10125
10126 /* Read the local symbols. */
10127 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10128 if (isymbuf == NULL && locsymcount != 0)
10129 {
10130 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
10131 flinfo->internal_syms,
10132 flinfo->external_syms,
10133 flinfo->locsym_shndx);
10134 if (isymbuf == NULL)
10135 return FALSE;
10136 }
10137
10138 /* Find local symbol sections and adjust values of symbols in
10139 SEC_MERGE sections. Write out those local symbols we know are
10140 going into the output file. */
10141 isymend = isymbuf + locsymcount;
10142 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
10143 isym < isymend;
10144 isym++, pindex++, ppsection++)
10145 {
10146 asection *isec;
10147 const char *name;
10148 Elf_Internal_Sym osym;
10149 long indx;
10150 int ret;
10151
10152 *pindex = -1;
10153
10154 if (elf_bad_symtab (input_bfd))
10155 {
10156 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10157 {
10158 *ppsection = NULL;
10159 continue;
10160 }
10161 }
10162
10163 if (isym->st_shndx == SHN_UNDEF)
10164 isec = bfd_und_section_ptr;
10165 else if (isym->st_shndx == SHN_ABS)
10166 isec = bfd_abs_section_ptr;
10167 else if (isym->st_shndx == SHN_COMMON)
10168 isec = bfd_com_section_ptr;
10169 else
10170 {
10171 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10172 if (isec == NULL)
10173 {
10174 /* Don't attempt to output symbols with st_shnx in the
10175 reserved range other than SHN_ABS and SHN_COMMON. */
10176 *ppsection = NULL;
10177 continue;
10178 }
10179 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
10180 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10181 isym->st_value =
10182 _bfd_merged_section_offset (output_bfd, &isec,
10183 elf_section_data (isec)->sec_info,
10184 isym->st_value);
10185 }
10186
10187 *ppsection = isec;
10188
10189 /* Don't output the first, undefined, symbol. In fact, don't
10190 output any undefined local symbol. */
10191 if (isec == bfd_und_section_ptr)
10192 continue;
10193
10194 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10195 {
10196 /* We never output section symbols. Instead, we use the
10197 section symbol of the corresponding section in the output
10198 file. */
10199 continue;
10200 }
10201
10202 /* If we are stripping all symbols, we don't want to output this
10203 one. */
10204 if (flinfo->info->strip == strip_all)
10205 continue;
10206
10207 /* If we are discarding all local symbols, we don't want to
10208 output this one. If we are generating a relocatable output
10209 file, then some of the local symbols may be required by
10210 relocs; we output them below as we discover that they are
10211 needed. */
10212 if (flinfo->info->discard == discard_all)
10213 continue;
10214
10215 /* If this symbol is defined in a section which we are
10216 discarding, we don't need to keep it. */
10217 if (isym->st_shndx != SHN_UNDEF
10218 && isym->st_shndx < SHN_LORESERVE
10219 && bfd_section_removed_from_list (output_bfd,
10220 isec->output_section))
10221 continue;
10222
10223 /* Get the name of the symbol. */
10224 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10225 isym->st_name);
10226 if (name == NULL)
10227 return FALSE;
10228
10229 /* See if we are discarding symbols with this name. */
10230 if ((flinfo->info->strip == strip_some
10231 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
10232 == NULL))
10233 || (((flinfo->info->discard == discard_sec_merge
10234 && (isec->flags & SEC_MERGE)
10235 && !bfd_link_relocatable (flinfo->info))
10236 || flinfo->info->discard == discard_l)
10237 && bfd_is_local_label_name (input_bfd, name)))
10238 continue;
10239
10240 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10241 {
10242 if (input_bfd->lto_output)
10243 /* -flto puts a temp file name here. This means builds
10244 are not reproducible. Discard the symbol. */
10245 continue;
10246 have_file_sym = TRUE;
10247 flinfo->filesym_count += 1;
10248 }
10249 if (!have_file_sym)
10250 {
10251 /* In the absence of debug info, bfd_find_nearest_line uses
10252 FILE symbols to determine the source file for local
10253 function symbols. Provide a FILE symbol here if input
10254 files lack such, so that their symbols won't be
10255 associated with a previous input file. It's not the
10256 source file, but the best we can do. */
10257 have_file_sym = TRUE;
10258 flinfo->filesym_count += 1;
10259 memset (&osym, 0, sizeof (osym));
10260 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10261 osym.st_shndx = SHN_ABS;
10262 if (!elf_link_output_symstrtab (flinfo,
10263 (input_bfd->lto_output ? NULL
10264 : input_bfd->filename),
10265 &osym, bfd_abs_section_ptr,
10266 NULL))
10267 return FALSE;
10268 }
10269
10270 osym = *isym;
10271
10272 /* Adjust the section index for the output file. */
10273 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10274 isec->output_section);
10275 if (osym.st_shndx == SHN_BAD)
10276 return FALSE;
10277
10278 /* ELF symbols in relocatable files are section relative, but
10279 in executable files they are virtual addresses. Note that
10280 this code assumes that all ELF sections have an associated
10281 BFD section with a reasonable value for output_offset; below
10282 we assume that they also have a reasonable value for
10283 output_section. Any special sections must be set up to meet
10284 these requirements. */
10285 osym.st_value += isec->output_offset;
10286 if (!bfd_link_relocatable (flinfo->info))
10287 {
10288 osym.st_value += isec->output_section->vma;
10289 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10290 {
10291 /* STT_TLS symbols are relative to PT_TLS segment base. */
10292 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10293 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10294 }
10295 }
10296
10297 indx = bfd_get_symcount (output_bfd);
10298 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10299 if (ret == 0)
10300 return FALSE;
10301 else if (ret == 1)
10302 *pindex = indx;
10303 }
10304
10305 if (bed->s->arch_size == 32)
10306 {
10307 r_type_mask = 0xff;
10308 r_sym_shift = 8;
10309 address_size = 4;
10310 }
10311 else
10312 {
10313 r_type_mask = 0xffffffff;
10314 r_sym_shift = 32;
10315 address_size = 8;
10316 }
10317
10318 /* Relocate the contents of each section. */
10319 sym_hashes = elf_sym_hashes (input_bfd);
10320 for (o = input_bfd->sections; o != NULL; o = o->next)
10321 {
10322 bfd_byte *contents;
10323
10324 if (! o->linker_mark)
10325 {
10326 /* This section was omitted from the link. */
10327 continue;
10328 }
10329
10330 if (!flinfo->info->resolve_section_groups
10331 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10332 {
10333 /* Deal with the group signature symbol. */
10334 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10335 unsigned long symndx = sec_data->this_hdr.sh_info;
10336 asection *osec = o->output_section;
10337
10338 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
10339 if (symndx >= locsymcount
10340 || (elf_bad_symtab (input_bfd)
10341 && flinfo->sections[symndx] == NULL))
10342 {
10343 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10344 while (h->root.type == bfd_link_hash_indirect
10345 || h->root.type == bfd_link_hash_warning)
10346 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10347 /* Arrange for symbol to be output. */
10348 h->indx = -2;
10349 elf_section_data (osec)->this_hdr.sh_info = -2;
10350 }
10351 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10352 {
10353 /* We'll use the output section target_index. */
10354 asection *sec = flinfo->sections[symndx]->output_section;
10355 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10356 }
10357 else
10358 {
10359 if (flinfo->indices[symndx] == -1)
10360 {
10361 /* Otherwise output the local symbol now. */
10362 Elf_Internal_Sym sym = isymbuf[symndx];
10363 asection *sec = flinfo->sections[symndx]->output_section;
10364 const char *name;
10365 long indx;
10366 int ret;
10367
10368 name = bfd_elf_string_from_elf_section (input_bfd,
10369 symtab_hdr->sh_link,
10370 sym.st_name);
10371 if (name == NULL)
10372 return FALSE;
10373
10374 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10375 sec);
10376 if (sym.st_shndx == SHN_BAD)
10377 return FALSE;
10378
10379 sym.st_value += o->output_offset;
10380
10381 indx = bfd_get_symcount (output_bfd);
10382 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10383 NULL);
10384 if (ret == 0)
10385 return FALSE;
10386 else if (ret == 1)
10387 flinfo->indices[symndx] = indx;
10388 else
10389 abort ();
10390 }
10391 elf_section_data (osec)->this_hdr.sh_info
10392 = flinfo->indices[symndx];
10393 }
10394 }
10395
10396 if ((o->flags & SEC_HAS_CONTENTS) == 0
10397 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10398 continue;
10399
10400 if ((o->flags & SEC_LINKER_CREATED) != 0)
10401 {
10402 /* Section was created by _bfd_elf_link_create_dynamic_sections
10403 or somesuch. */
10404 continue;
10405 }
10406
10407 /* Get the contents of the section. They have been cached by a
10408 relaxation routine. Note that o is a section in an input
10409 file, so the contents field will not have been set by any of
10410 the routines which work on output files. */
10411 if (elf_section_data (o)->this_hdr.contents != NULL)
10412 {
10413 contents = elf_section_data (o)->this_hdr.contents;
10414 if (bed->caches_rawsize
10415 && o->rawsize != 0
10416 && o->rawsize < o->size)
10417 {
10418 memcpy (flinfo->contents, contents, o->rawsize);
10419 contents = flinfo->contents;
10420 }
10421 }
10422 else
10423 {
10424 contents = flinfo->contents;
10425 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10426 return FALSE;
10427 }
10428
10429 if ((o->flags & SEC_RELOC) != 0)
10430 {
10431 Elf_Internal_Rela *internal_relocs;
10432 Elf_Internal_Rela *rel, *relend;
10433 int action_discarded;
10434 int ret;
10435
10436 /* Get the swapped relocs. */
10437 internal_relocs
10438 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10439 flinfo->internal_relocs, FALSE);
10440 if (internal_relocs == NULL
10441 && o->reloc_count > 0)
10442 return FALSE;
10443
10444 /* We need to reverse-copy input .ctors/.dtors sections if
10445 they are placed in .init_array/.finit_array for output. */
10446 if (o->size > address_size
10447 && ((strncmp (o->name, ".ctors", 6) == 0
10448 && strcmp (o->output_section->name,
10449 ".init_array") == 0)
10450 || (strncmp (o->name, ".dtors", 6) == 0
10451 && strcmp (o->output_section->name,
10452 ".fini_array") == 0))
10453 && (o->name[6] == 0 || o->name[6] == '.'))
10454 {
10455 if (o->size * bed->s->int_rels_per_ext_rel
10456 != o->reloc_count * address_size)
10457 {
10458 _bfd_error_handler
10459 /* xgettext:c-format */
10460 (_("error: %B: size of section %A is not "
10461 "multiple of address size"),
10462 input_bfd, o);
10463 bfd_set_error (bfd_error_bad_value);
10464 return FALSE;
10465 }
10466 o->flags |= SEC_ELF_REVERSE_COPY;
10467 }
10468
10469 action_discarded = -1;
10470 if (!elf_section_ignore_discarded_relocs (o))
10471 action_discarded = (*bed->action_discarded) (o);
10472
10473 /* Run through the relocs evaluating complex reloc symbols and
10474 looking for relocs against symbols from discarded sections
10475 or section symbols from removed link-once sections.
10476 Complain about relocs against discarded sections. Zero
10477 relocs against removed link-once sections. */
10478
10479 rel = internal_relocs;
10480 relend = rel + o->reloc_count;
10481 for ( ; rel < relend; rel++)
10482 {
10483 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10484 unsigned int s_type;
10485 asection **ps, *sec;
10486 struct elf_link_hash_entry *h = NULL;
10487 const char *sym_name;
10488
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 h = sym_hashes[r_symndx - extsymoff];
10497
10498 /* Badly formatted input files can contain relocs that
10499 reference non-existant symbols. Check here so that
10500 we do not seg fault. */
10501 if (h == NULL)
10502 {
10503 _bfd_error_handler
10504 /* xgettext:c-format */
10505 (_("error: %B contains a reloc (%#Lx) for section %A "
10506 "that references a non-existent global symbol"),
10507 input_bfd, rel->r_info, o);
10508 bfd_set_error (bfd_error_bad_value);
10509 return FALSE;
10510 }
10511
10512 while (h->root.type == bfd_link_hash_indirect
10513 || h->root.type == bfd_link_hash_warning)
10514 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10515
10516 s_type = h->type;
10517
10518 /* If a plugin symbol is referenced from a non-IR file,
10519 mark the symbol as undefined. Note that the
10520 linker may attach linker created dynamic sections
10521 to the plugin bfd. Symbols defined in linker
10522 created sections are not plugin symbols. */
10523 if ((h->root.non_ir_ref_regular
10524 || h->root.non_ir_ref_dynamic)
10525 && (h->root.type == bfd_link_hash_defined
10526 || h->root.type == bfd_link_hash_defweak)
10527 && (h->root.u.def.section->flags
10528 & SEC_LINKER_CREATED) == 0
10529 && h->root.u.def.section->owner != NULL
10530 && (h->root.u.def.section->owner->flags
10531 & BFD_PLUGIN) != 0)
10532 {
10533 h->root.type = bfd_link_hash_undefined;
10534 h->root.u.undef.abfd = h->root.u.def.section->owner;
10535 }
10536
10537 ps = NULL;
10538 if (h->root.type == bfd_link_hash_defined
10539 || h->root.type == bfd_link_hash_defweak)
10540 ps = &h->root.u.def.section;
10541
10542 sym_name = h->root.root.string;
10543 }
10544 else
10545 {
10546 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10547
10548 s_type = ELF_ST_TYPE (sym->st_info);
10549 ps = &flinfo->sections[r_symndx];
10550 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10551 sym, *ps);
10552 }
10553
10554 if ((s_type == STT_RELC || s_type == STT_SRELC)
10555 && !bfd_link_relocatable (flinfo->info))
10556 {
10557 bfd_vma val;
10558 bfd_vma dot = (rel->r_offset
10559 + o->output_offset + o->output_section->vma);
10560 #ifdef DEBUG
10561 printf ("Encountered a complex symbol!");
10562 printf (" (input_bfd %s, section %s, reloc %ld\n",
10563 input_bfd->filename, o->name,
10564 (long) (rel - internal_relocs));
10565 printf (" symbol: idx %8.8lx, name %s\n",
10566 r_symndx, sym_name);
10567 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10568 (unsigned long) rel->r_info,
10569 (unsigned long) rel->r_offset);
10570 #endif
10571 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10572 isymbuf, locsymcount, s_type == STT_SRELC))
10573 return FALSE;
10574
10575 /* Symbol evaluated OK. Update to absolute value. */
10576 set_symbol_value (input_bfd, isymbuf, locsymcount,
10577 r_symndx, val);
10578 continue;
10579 }
10580
10581 if (action_discarded != -1 && ps != NULL)
10582 {
10583 /* Complain if the definition comes from a
10584 discarded section. */
10585 if ((sec = *ps) != NULL && discarded_section (sec))
10586 {
10587 BFD_ASSERT (r_symndx != STN_UNDEF);
10588 if (action_discarded & COMPLAIN)
10589 (*flinfo->info->callbacks->einfo)
10590 /* xgettext:c-format */
10591 (_("%X`%s' referenced in section `%A' of %B: "
10592 "defined in discarded section `%A' of %B\n"),
10593 sym_name, o, input_bfd, sec, sec->owner);
10594
10595 /* Try to do the best we can to support buggy old
10596 versions of gcc. Pretend that the symbol is
10597 really defined in the kept linkonce section.
10598 FIXME: This is quite broken. Modifying the
10599 symbol here means we will be changing all later
10600 uses of the symbol, not just in this section. */
10601 if (action_discarded & PRETEND)
10602 {
10603 asection *kept;
10604
10605 kept = _bfd_elf_check_kept_section (sec,
10606 flinfo->info);
10607 if (kept != NULL)
10608 {
10609 *ps = kept;
10610 continue;
10611 }
10612 }
10613 }
10614 }
10615 }
10616
10617 /* Relocate the section by invoking a back end routine.
10618
10619 The back end routine is responsible for adjusting the
10620 section contents as necessary, and (if using Rela relocs
10621 and generating a relocatable output file) adjusting the
10622 reloc addend as necessary.
10623
10624 The back end routine does not have to worry about setting
10625 the reloc address or the reloc symbol index.
10626
10627 The back end routine is given a pointer to the swapped in
10628 internal symbols, and can access the hash table entries
10629 for the external symbols via elf_sym_hashes (input_bfd).
10630
10631 When generating relocatable output, the back end routine
10632 must handle STB_LOCAL/STT_SECTION symbols specially. The
10633 output symbol is going to be a section symbol
10634 corresponding to the output section, which will require
10635 the addend to be adjusted. */
10636
10637 ret = (*relocate_section) (output_bfd, flinfo->info,
10638 input_bfd, o, contents,
10639 internal_relocs,
10640 isymbuf,
10641 flinfo->sections);
10642 if (!ret)
10643 return FALSE;
10644
10645 if (ret == 2
10646 || bfd_link_relocatable (flinfo->info)
10647 || flinfo->info->emitrelocations)
10648 {
10649 Elf_Internal_Rela *irela;
10650 Elf_Internal_Rela *irelaend, *irelamid;
10651 bfd_vma last_offset;
10652 struct elf_link_hash_entry **rel_hash;
10653 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10654 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10655 unsigned int next_erel;
10656 bfd_boolean rela_normal;
10657 struct bfd_elf_section_data *esdi, *esdo;
10658
10659 esdi = elf_section_data (o);
10660 esdo = elf_section_data (o->output_section);
10661 rela_normal = FALSE;
10662
10663 /* Adjust the reloc addresses and symbol indices. */
10664
10665 irela = internal_relocs;
10666 irelaend = irela + o->reloc_count;
10667 rel_hash = esdo->rel.hashes + esdo->rel.count;
10668 /* We start processing the REL relocs, if any. When we reach
10669 IRELAMID in the loop, we switch to the RELA relocs. */
10670 irelamid = irela;
10671 if (esdi->rel.hdr != NULL)
10672 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10673 * bed->s->int_rels_per_ext_rel);
10674 rel_hash_list = rel_hash;
10675 rela_hash_list = NULL;
10676 last_offset = o->output_offset;
10677 if (!bfd_link_relocatable (flinfo->info))
10678 last_offset += o->output_section->vma;
10679 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10680 {
10681 unsigned long r_symndx;
10682 asection *sec;
10683 Elf_Internal_Sym sym;
10684
10685 if (next_erel == bed->s->int_rels_per_ext_rel)
10686 {
10687 rel_hash++;
10688 next_erel = 0;
10689 }
10690
10691 if (irela == irelamid)
10692 {
10693 rel_hash = esdo->rela.hashes + esdo->rela.count;
10694 rela_hash_list = rel_hash;
10695 rela_normal = bed->rela_normal;
10696 }
10697
10698 irela->r_offset = _bfd_elf_section_offset (output_bfd,
10699 flinfo->info, o,
10700 irela->r_offset);
10701 if (irela->r_offset >= (bfd_vma) -2)
10702 {
10703 /* This is a reloc for a deleted entry or somesuch.
10704 Turn it into an R_*_NONE reloc, at the same
10705 offset as the last reloc. elf_eh_frame.c and
10706 bfd_elf_discard_info rely on reloc offsets
10707 being ordered. */
10708 irela->r_offset = last_offset;
10709 irela->r_info = 0;
10710 irela->r_addend = 0;
10711 continue;
10712 }
10713
10714 irela->r_offset += o->output_offset;
10715
10716 /* Relocs in an executable have to be virtual addresses. */
10717 if (!bfd_link_relocatable (flinfo->info))
10718 irela->r_offset += o->output_section->vma;
10719
10720 last_offset = irela->r_offset;
10721
10722 r_symndx = irela->r_info >> r_sym_shift;
10723 if (r_symndx == STN_UNDEF)
10724 continue;
10725
10726 if (r_symndx >= locsymcount
10727 || (elf_bad_symtab (input_bfd)
10728 && flinfo->sections[r_symndx] == NULL))
10729 {
10730 struct elf_link_hash_entry *rh;
10731 unsigned long indx;
10732
10733 /* This is a reloc against a global symbol. We
10734 have not yet output all the local symbols, so
10735 we do not know the symbol index of any global
10736 symbol. We set the rel_hash entry for this
10737 reloc to point to the global hash table entry
10738 for this symbol. The symbol index is then
10739 set at the end of bfd_elf_final_link. */
10740 indx = r_symndx - extsymoff;
10741 rh = elf_sym_hashes (input_bfd)[indx];
10742 while (rh->root.type == bfd_link_hash_indirect
10743 || rh->root.type == bfd_link_hash_warning)
10744 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10745
10746 /* Setting the index to -2 tells
10747 elf_link_output_extsym that this symbol is
10748 used by a reloc. */
10749 BFD_ASSERT (rh->indx < 0);
10750 rh->indx = -2;
10751 *rel_hash = rh;
10752
10753 continue;
10754 }
10755
10756 /* This is a reloc against a local symbol. */
10757
10758 *rel_hash = NULL;
10759 sym = isymbuf[r_symndx];
10760 sec = flinfo->sections[r_symndx];
10761 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10762 {
10763 /* I suppose the backend ought to fill in the
10764 section of any STT_SECTION symbol against a
10765 processor specific section. */
10766 r_symndx = STN_UNDEF;
10767 if (bfd_is_abs_section (sec))
10768 ;
10769 else if (sec == NULL || sec->owner == NULL)
10770 {
10771 bfd_set_error (bfd_error_bad_value);
10772 return FALSE;
10773 }
10774 else
10775 {
10776 asection *osec = sec->output_section;
10777
10778 /* If we have discarded a section, the output
10779 section will be the absolute section. In
10780 case of discarded SEC_MERGE sections, use
10781 the kept section. relocate_section should
10782 have already handled discarded linkonce
10783 sections. */
10784 if (bfd_is_abs_section (osec)
10785 && sec->kept_section != NULL
10786 && sec->kept_section->output_section != NULL)
10787 {
10788 osec = sec->kept_section->output_section;
10789 irela->r_addend -= osec->vma;
10790 }
10791
10792 if (!bfd_is_abs_section (osec))
10793 {
10794 r_symndx = osec->target_index;
10795 if (r_symndx == STN_UNDEF)
10796 {
10797 irela->r_addend += osec->vma;
10798 osec = _bfd_nearby_section (output_bfd, osec,
10799 osec->vma);
10800 irela->r_addend -= osec->vma;
10801 r_symndx = osec->target_index;
10802 }
10803 }
10804 }
10805
10806 /* Adjust the addend according to where the
10807 section winds up in the output section. */
10808 if (rela_normal)
10809 irela->r_addend += sec->output_offset;
10810 }
10811 else
10812 {
10813 if (flinfo->indices[r_symndx] == -1)
10814 {
10815 unsigned long shlink;
10816 const char *name;
10817 asection *osec;
10818 long indx;
10819
10820 if (flinfo->info->strip == strip_all)
10821 {
10822 /* You can't do ld -r -s. */
10823 bfd_set_error (bfd_error_invalid_operation);
10824 return FALSE;
10825 }
10826
10827 /* This symbol was skipped earlier, but
10828 since it is needed by a reloc, we
10829 must output it now. */
10830 shlink = symtab_hdr->sh_link;
10831 name = (bfd_elf_string_from_elf_section
10832 (input_bfd, shlink, sym.st_name));
10833 if (name == NULL)
10834 return FALSE;
10835
10836 osec = sec->output_section;
10837 sym.st_shndx =
10838 _bfd_elf_section_from_bfd_section (output_bfd,
10839 osec);
10840 if (sym.st_shndx == SHN_BAD)
10841 return FALSE;
10842
10843 sym.st_value += sec->output_offset;
10844 if (!bfd_link_relocatable (flinfo->info))
10845 {
10846 sym.st_value += osec->vma;
10847 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10848 {
10849 /* STT_TLS symbols are relative to PT_TLS
10850 segment base. */
10851 BFD_ASSERT (elf_hash_table (flinfo->info)
10852 ->tls_sec != NULL);
10853 sym.st_value -= (elf_hash_table (flinfo->info)
10854 ->tls_sec->vma);
10855 }
10856 }
10857
10858 indx = bfd_get_symcount (output_bfd);
10859 ret = elf_link_output_symstrtab (flinfo, name,
10860 &sym, sec,
10861 NULL);
10862 if (ret == 0)
10863 return FALSE;
10864 else if (ret == 1)
10865 flinfo->indices[r_symndx] = indx;
10866 else
10867 abort ();
10868 }
10869
10870 r_symndx = flinfo->indices[r_symndx];
10871 }
10872
10873 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10874 | (irela->r_info & r_type_mask));
10875 }
10876
10877 /* Swap out the relocs. */
10878 input_rel_hdr = esdi->rel.hdr;
10879 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
10880 {
10881 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10882 input_rel_hdr,
10883 internal_relocs,
10884 rel_hash_list))
10885 return FALSE;
10886 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10887 * bed->s->int_rels_per_ext_rel);
10888 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
10889 }
10890
10891 input_rela_hdr = esdi->rela.hdr;
10892 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10893 {
10894 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10895 input_rela_hdr,
10896 internal_relocs,
10897 rela_hash_list))
10898 return FALSE;
10899 }
10900 }
10901 }
10902
10903 /* Write out the modified section contents. */
10904 if (bed->elf_backend_write_section
10905 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
10906 contents))
10907 {
10908 /* Section written out. */
10909 }
10910 else switch (o->sec_info_type)
10911 {
10912 case SEC_INFO_TYPE_STABS:
10913 if (! (_bfd_write_section_stabs
10914 (output_bfd,
10915 &elf_hash_table (flinfo->info)->stab_info,
10916 o, &elf_section_data (o)->sec_info, contents)))
10917 return FALSE;
10918 break;
10919 case SEC_INFO_TYPE_MERGE:
10920 if (! _bfd_write_merged_section (output_bfd, o,
10921 elf_section_data (o)->sec_info))
10922 return FALSE;
10923 break;
10924 case SEC_INFO_TYPE_EH_FRAME:
10925 {
10926 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
10927 o, contents))
10928 return FALSE;
10929 }
10930 break;
10931 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10932 {
10933 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10934 flinfo->info,
10935 o, contents))
10936 return FALSE;
10937 }
10938 break;
10939 default:
10940 {
10941 if (! (o->flags & SEC_EXCLUDE))
10942 {
10943 file_ptr offset = (file_ptr) o->output_offset;
10944 bfd_size_type todo = o->size;
10945
10946 offset *= bfd_octets_per_byte (output_bfd);
10947
10948 if ((o->flags & SEC_ELF_REVERSE_COPY))
10949 {
10950 /* Reverse-copy input section to output. */
10951 do
10952 {
10953 todo -= address_size;
10954 if (! bfd_set_section_contents (output_bfd,
10955 o->output_section,
10956 contents + todo,
10957 offset,
10958 address_size))
10959 return FALSE;
10960 if (todo == 0)
10961 break;
10962 offset += address_size;
10963 }
10964 while (1);
10965 }
10966 else if (! bfd_set_section_contents (output_bfd,
10967 o->output_section,
10968 contents,
10969 offset, todo))
10970 return FALSE;
10971 }
10972 }
10973 break;
10974 }
10975 }
10976
10977 return TRUE;
10978 }
10979
10980 /* Generate a reloc when linking an ELF file. This is a reloc
10981 requested by the linker, and does not come from any input file. This
10982 is used to build constructor and destructor tables when linking
10983 with -Ur. */
10984
10985 static bfd_boolean
10986 elf_reloc_link_order (bfd *output_bfd,
10987 struct bfd_link_info *info,
10988 asection *output_section,
10989 struct bfd_link_order *link_order)
10990 {
10991 reloc_howto_type *howto;
10992 long indx;
10993 bfd_vma offset;
10994 bfd_vma addend;
10995 struct bfd_elf_section_reloc_data *reldata;
10996 struct elf_link_hash_entry **rel_hash_ptr;
10997 Elf_Internal_Shdr *rel_hdr;
10998 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10999 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11000 bfd_byte *erel;
11001 unsigned int i;
11002 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11003
11004 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11005 if (howto == NULL)
11006 {
11007 bfd_set_error (bfd_error_bad_value);
11008 return FALSE;
11009 }
11010
11011 addend = link_order->u.reloc.p->addend;
11012
11013 if (esdo->rel.hdr)
11014 reldata = &esdo->rel;
11015 else if (esdo->rela.hdr)
11016 reldata = &esdo->rela;
11017 else
11018 {
11019 reldata = NULL;
11020 BFD_ASSERT (0);
11021 }
11022
11023 /* Figure out the symbol index. */
11024 rel_hash_ptr = reldata->hashes + reldata->count;
11025 if (link_order->type == bfd_section_reloc_link_order)
11026 {
11027 indx = link_order->u.reloc.p->u.section->target_index;
11028 BFD_ASSERT (indx != 0);
11029 *rel_hash_ptr = NULL;
11030 }
11031 else
11032 {
11033 struct elf_link_hash_entry *h;
11034
11035 /* Treat a reloc against a defined symbol as though it were
11036 actually against the section. */
11037 h = ((struct elf_link_hash_entry *)
11038 bfd_wrapped_link_hash_lookup (output_bfd, info,
11039 link_order->u.reloc.p->u.name,
11040 FALSE, FALSE, TRUE));
11041 if (h != NULL
11042 && (h->root.type == bfd_link_hash_defined
11043 || h->root.type == bfd_link_hash_defweak))
11044 {
11045 asection *section;
11046
11047 section = h->root.u.def.section;
11048 indx = section->output_section->target_index;
11049 *rel_hash_ptr = NULL;
11050 /* It seems that we ought to add the symbol value to the
11051 addend here, but in practice it has already been added
11052 because it was passed to constructor_callback. */
11053 addend += section->output_section->vma + section->output_offset;
11054 }
11055 else if (h != NULL)
11056 {
11057 /* Setting the index to -2 tells elf_link_output_extsym that
11058 this symbol is used by a reloc. */
11059 h->indx = -2;
11060 *rel_hash_ptr = h;
11061 indx = 0;
11062 }
11063 else
11064 {
11065 (*info->callbacks->unattached_reloc)
11066 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
11067 indx = 0;
11068 }
11069 }
11070
11071 /* If this is an inplace reloc, we must write the addend into the
11072 object file. */
11073 if (howto->partial_inplace && addend != 0)
11074 {
11075 bfd_size_type size;
11076 bfd_reloc_status_type rstat;
11077 bfd_byte *buf;
11078 bfd_boolean ok;
11079 const char *sym_name;
11080
11081 size = (bfd_size_type) bfd_get_reloc_size (howto);
11082 buf = (bfd_byte *) bfd_zmalloc (size);
11083 if (buf == NULL && size != 0)
11084 return FALSE;
11085 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11086 switch (rstat)
11087 {
11088 case bfd_reloc_ok:
11089 break;
11090
11091 default:
11092 case bfd_reloc_outofrange:
11093 abort ();
11094
11095 case bfd_reloc_overflow:
11096 if (link_order->type == bfd_section_reloc_link_order)
11097 sym_name = bfd_section_name (output_bfd,
11098 link_order->u.reloc.p->u.section);
11099 else
11100 sym_name = link_order->u.reloc.p->u.name;
11101 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11102 howto->name, addend, NULL, NULL,
11103 (bfd_vma) 0);
11104 break;
11105 }
11106
11107 ok = bfd_set_section_contents (output_bfd, output_section, buf,
11108 link_order->offset
11109 * bfd_octets_per_byte (output_bfd),
11110 size);
11111 free (buf);
11112 if (! ok)
11113 return FALSE;
11114 }
11115
11116 /* The address of a reloc is relative to the section in a
11117 relocatable file, and is a virtual address in an executable
11118 file. */
11119 offset = link_order->offset;
11120 if (! bfd_link_relocatable (info))
11121 offset += output_section->vma;
11122
11123 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11124 {
11125 irel[i].r_offset = offset;
11126 irel[i].r_info = 0;
11127 irel[i].r_addend = 0;
11128 }
11129 if (bed->s->arch_size == 32)
11130 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11131 else
11132 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11133
11134 rel_hdr = reldata->hdr;
11135 erel = rel_hdr->contents;
11136 if (rel_hdr->sh_type == SHT_REL)
11137 {
11138 erel += reldata->count * bed->s->sizeof_rel;
11139 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11140 }
11141 else
11142 {
11143 irel[0].r_addend = addend;
11144 erel += reldata->count * bed->s->sizeof_rela;
11145 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11146 }
11147
11148 ++reldata->count;
11149
11150 return TRUE;
11151 }
11152
11153
11154 /* Get the output vma of the section pointed to by the sh_link field. */
11155
11156 static bfd_vma
11157 elf_get_linked_section_vma (struct bfd_link_order *p)
11158 {
11159 Elf_Internal_Shdr **elf_shdrp;
11160 asection *s;
11161 int elfsec;
11162
11163 s = p->u.indirect.section;
11164 elf_shdrp = elf_elfsections (s->owner);
11165 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
11166 elfsec = elf_shdrp[elfsec]->sh_link;
11167 /* PR 290:
11168 The Intel C compiler generates SHT_IA_64_UNWIND with
11169 SHF_LINK_ORDER. But it doesn't set the sh_link or
11170 sh_info fields. Hence we could get the situation
11171 where elfsec is 0. */
11172 if (elfsec == 0)
11173 {
11174 const struct elf_backend_data *bed
11175 = get_elf_backend_data (s->owner);
11176 if (bed->link_order_error_handler)
11177 bed->link_order_error_handler
11178 /* xgettext:c-format */
11179 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
11180 return 0;
11181 }
11182 else
11183 {
11184 s = elf_shdrp[elfsec]->bfd_section;
11185 return s->output_section->vma + s->output_offset;
11186 }
11187 }
11188
11189
11190 /* Compare two sections based on the locations of the sections they are
11191 linked to. Used by elf_fixup_link_order. */
11192
11193 static int
11194 compare_link_order (const void * a, const void * b)
11195 {
11196 bfd_vma apos;
11197 bfd_vma bpos;
11198
11199 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11200 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11201 if (apos < bpos)
11202 return -1;
11203 return apos > bpos;
11204 }
11205
11206
11207 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11208 order as their linked sections. Returns false if this could not be done
11209 because an output section includes both ordered and unordered
11210 sections. Ideally we'd do this in the linker proper. */
11211
11212 static bfd_boolean
11213 elf_fixup_link_order (bfd *abfd, asection *o)
11214 {
11215 int seen_linkorder;
11216 int seen_other;
11217 int n;
11218 struct bfd_link_order *p;
11219 bfd *sub;
11220 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11221 unsigned elfsec;
11222 struct bfd_link_order **sections;
11223 asection *s, *other_sec, *linkorder_sec;
11224 bfd_vma offset;
11225
11226 other_sec = NULL;
11227 linkorder_sec = NULL;
11228 seen_other = 0;
11229 seen_linkorder = 0;
11230 for (p = o->map_head.link_order; p != NULL; p = p->next)
11231 {
11232 if (p->type == bfd_indirect_link_order)
11233 {
11234 s = p->u.indirect.section;
11235 sub = s->owner;
11236 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11237 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
11238 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11239 && elfsec < elf_numsections (sub)
11240 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11241 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
11242 {
11243 seen_linkorder++;
11244 linkorder_sec = s;
11245 }
11246 else
11247 {
11248 seen_other++;
11249 other_sec = s;
11250 }
11251 }
11252 else
11253 seen_other++;
11254
11255 if (seen_other && seen_linkorder)
11256 {
11257 if (other_sec && linkorder_sec)
11258 _bfd_error_handler
11259 /* xgettext:c-format */
11260 (_("%A has both ordered [`%A' in %B] "
11261 "and unordered [`%A' in %B] sections"),
11262 o, linkorder_sec, linkorder_sec->owner,
11263 other_sec, other_sec->owner);
11264 else
11265 _bfd_error_handler
11266 (_("%A has both ordered and unordered sections"), o);
11267 bfd_set_error (bfd_error_bad_value);
11268 return FALSE;
11269 }
11270 }
11271
11272 if (!seen_linkorder)
11273 return TRUE;
11274
11275 sections = (struct bfd_link_order **)
11276 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11277 if (sections == NULL)
11278 return FALSE;
11279 seen_linkorder = 0;
11280
11281 for (p = o->map_head.link_order; p != NULL; p = p->next)
11282 {
11283 sections[seen_linkorder++] = p;
11284 }
11285 /* Sort the input sections in the order of their linked section. */
11286 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11287 compare_link_order);
11288
11289 /* Change the offsets of the sections. */
11290 offset = 0;
11291 for (n = 0; n < seen_linkorder; n++)
11292 {
11293 s = sections[n]->u.indirect.section;
11294 offset &= ~(bfd_vma) 0 << s->alignment_power;
11295 s->output_offset = offset / bfd_octets_per_byte (abfd);
11296 sections[n]->offset = offset;
11297 offset += sections[n]->size;
11298 }
11299
11300 free (sections);
11301 return TRUE;
11302 }
11303
11304 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11305 Returns TRUE upon success, FALSE otherwise. */
11306
11307 static bfd_boolean
11308 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11309 {
11310 bfd_boolean ret = FALSE;
11311 bfd *implib_bfd;
11312 const struct elf_backend_data *bed;
11313 flagword flags;
11314 enum bfd_architecture arch;
11315 unsigned int mach;
11316 asymbol **sympp = NULL;
11317 long symsize;
11318 long symcount;
11319 long src_count;
11320 elf_symbol_type *osymbuf;
11321
11322 implib_bfd = info->out_implib_bfd;
11323 bed = get_elf_backend_data (abfd);
11324
11325 if (!bfd_set_format (implib_bfd, bfd_object))
11326 return FALSE;
11327
11328 /* Use flag from executable but make it a relocatable object. */
11329 flags = bfd_get_file_flags (abfd);
11330 flags &= ~HAS_RELOC;
11331 if (!bfd_set_start_address (implib_bfd, 0)
11332 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
11333 return FALSE;
11334
11335 /* Copy architecture of output file to import library file. */
11336 arch = bfd_get_arch (abfd);
11337 mach = bfd_get_mach (abfd);
11338 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11339 && (abfd->target_defaulted
11340 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11341 return FALSE;
11342
11343 /* Get symbol table size. */
11344 symsize = bfd_get_symtab_upper_bound (abfd);
11345 if (symsize < 0)
11346 return FALSE;
11347
11348 /* Read in the symbol table. */
11349 sympp = (asymbol **) xmalloc (symsize);
11350 symcount = bfd_canonicalize_symtab (abfd, sympp);
11351 if (symcount < 0)
11352 goto free_sym_buf;
11353
11354 /* Allow the BFD backend to copy any private header data it
11355 understands from the output BFD to the import library BFD. */
11356 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11357 goto free_sym_buf;
11358
11359 /* Filter symbols to appear in the import library. */
11360 if (bed->elf_backend_filter_implib_symbols)
11361 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11362 symcount);
11363 else
11364 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11365 if (symcount == 0)
11366 {
11367 bfd_set_error (bfd_error_no_symbols);
11368 _bfd_error_handler (_("%B: no symbol found for import library"),
11369 implib_bfd);
11370 goto free_sym_buf;
11371 }
11372
11373
11374 /* Make symbols absolute. */
11375 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11376 sizeof (*osymbuf));
11377 for (src_count = 0; src_count < symcount; src_count++)
11378 {
11379 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11380 sizeof (*osymbuf));
11381 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11382 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11383 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11384 osymbuf[src_count].internal_elf_sym.st_value =
11385 osymbuf[src_count].symbol.value;
11386 sympp[src_count] = &osymbuf[src_count].symbol;
11387 }
11388
11389 bfd_set_symtab (implib_bfd, sympp, symcount);
11390
11391 /* Allow the BFD backend to copy any private data it understands
11392 from the output BFD to the import library BFD. This is done last
11393 to permit the routine to look at the filtered symbol table. */
11394 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11395 goto free_sym_buf;
11396
11397 if (!bfd_close (implib_bfd))
11398 goto free_sym_buf;
11399
11400 ret = TRUE;
11401
11402 free_sym_buf:
11403 free (sympp);
11404 return ret;
11405 }
11406
11407 static void
11408 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11409 {
11410 asection *o;
11411
11412 if (flinfo->symstrtab != NULL)
11413 _bfd_elf_strtab_free (flinfo->symstrtab);
11414 if (flinfo->contents != NULL)
11415 free (flinfo->contents);
11416 if (flinfo->external_relocs != NULL)
11417 free (flinfo->external_relocs);
11418 if (flinfo->internal_relocs != NULL)
11419 free (flinfo->internal_relocs);
11420 if (flinfo->external_syms != NULL)
11421 free (flinfo->external_syms);
11422 if (flinfo->locsym_shndx != NULL)
11423 free (flinfo->locsym_shndx);
11424 if (flinfo->internal_syms != NULL)
11425 free (flinfo->internal_syms);
11426 if (flinfo->indices != NULL)
11427 free (flinfo->indices);
11428 if (flinfo->sections != NULL)
11429 free (flinfo->sections);
11430 if (flinfo->symshndxbuf != NULL)
11431 free (flinfo->symshndxbuf);
11432 for (o = obfd->sections; o != NULL; o = o->next)
11433 {
11434 struct bfd_elf_section_data *esdo = elf_section_data (o);
11435 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11436 free (esdo->rel.hashes);
11437 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11438 free (esdo->rela.hashes);
11439 }
11440 }
11441
11442 /* Do the final step of an ELF link. */
11443
11444 bfd_boolean
11445 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11446 {
11447 bfd_boolean dynamic;
11448 bfd_boolean emit_relocs;
11449 bfd *dynobj;
11450 struct elf_final_link_info flinfo;
11451 asection *o;
11452 struct bfd_link_order *p;
11453 bfd *sub;
11454 bfd_size_type max_contents_size;
11455 bfd_size_type max_external_reloc_size;
11456 bfd_size_type max_internal_reloc_count;
11457 bfd_size_type max_sym_count;
11458 bfd_size_type max_sym_shndx_count;
11459 Elf_Internal_Sym elfsym;
11460 unsigned int i;
11461 Elf_Internal_Shdr *symtab_hdr;
11462 Elf_Internal_Shdr *symtab_shndx_hdr;
11463 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11464 struct elf_outext_info eoinfo;
11465 bfd_boolean merged;
11466 size_t relativecount = 0;
11467 asection *reldyn = 0;
11468 bfd_size_type amt;
11469 asection *attr_section = NULL;
11470 bfd_vma attr_size = 0;
11471 const char *std_attrs_section;
11472 struct elf_link_hash_table *htab = elf_hash_table (info);
11473
11474 if (!is_elf_hash_table (htab))
11475 return FALSE;
11476
11477 if (bfd_link_pic (info))
11478 abfd->flags |= DYNAMIC;
11479
11480 dynamic = htab->dynamic_sections_created;
11481 dynobj = htab->dynobj;
11482
11483 emit_relocs = (bfd_link_relocatable (info)
11484 || info->emitrelocations);
11485
11486 flinfo.info = info;
11487 flinfo.output_bfd = abfd;
11488 flinfo.symstrtab = _bfd_elf_strtab_init ();
11489 if (flinfo.symstrtab == NULL)
11490 return FALSE;
11491
11492 if (! dynamic)
11493 {
11494 flinfo.hash_sec = NULL;
11495 flinfo.symver_sec = NULL;
11496 }
11497 else
11498 {
11499 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11500 /* Note that dynsym_sec can be NULL (on VMS). */
11501 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11502 /* Note that it is OK if symver_sec is NULL. */
11503 }
11504
11505 flinfo.contents = NULL;
11506 flinfo.external_relocs = NULL;
11507 flinfo.internal_relocs = NULL;
11508 flinfo.external_syms = NULL;
11509 flinfo.locsym_shndx = NULL;
11510 flinfo.internal_syms = NULL;
11511 flinfo.indices = NULL;
11512 flinfo.sections = NULL;
11513 flinfo.symshndxbuf = NULL;
11514 flinfo.filesym_count = 0;
11515
11516 /* The object attributes have been merged. Remove the input
11517 sections from the link, and set the contents of the output
11518 secton. */
11519 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11520 for (o = abfd->sections; o != NULL; o = o->next)
11521 {
11522 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11523 || strcmp (o->name, ".gnu.attributes") == 0)
11524 {
11525 for (p = o->map_head.link_order; p != NULL; p = p->next)
11526 {
11527 asection *input_section;
11528
11529 if (p->type != bfd_indirect_link_order)
11530 continue;
11531 input_section = p->u.indirect.section;
11532 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11533 elf_link_input_bfd ignores this section. */
11534 input_section->flags &= ~SEC_HAS_CONTENTS;
11535 }
11536
11537 attr_size = bfd_elf_obj_attr_size (abfd);
11538 if (attr_size)
11539 {
11540 bfd_set_section_size (abfd, o, attr_size);
11541 attr_section = o;
11542 /* Skip this section later on. */
11543 o->map_head.link_order = NULL;
11544 }
11545 else
11546 o->flags |= SEC_EXCLUDE;
11547 }
11548 }
11549
11550 /* Count up the number of relocations we will output for each output
11551 section, so that we know the sizes of the reloc sections. We
11552 also figure out some maximum sizes. */
11553 max_contents_size = 0;
11554 max_external_reloc_size = 0;
11555 max_internal_reloc_count = 0;
11556 max_sym_count = 0;
11557 max_sym_shndx_count = 0;
11558 merged = FALSE;
11559 for (o = abfd->sections; o != NULL; o = o->next)
11560 {
11561 struct bfd_elf_section_data *esdo = elf_section_data (o);
11562 o->reloc_count = 0;
11563
11564 for (p = o->map_head.link_order; p != NULL; p = p->next)
11565 {
11566 unsigned int reloc_count = 0;
11567 unsigned int additional_reloc_count = 0;
11568 struct bfd_elf_section_data *esdi = NULL;
11569
11570 if (p->type == bfd_section_reloc_link_order
11571 || p->type == bfd_symbol_reloc_link_order)
11572 reloc_count = 1;
11573 else if (p->type == bfd_indirect_link_order)
11574 {
11575 asection *sec;
11576
11577 sec = p->u.indirect.section;
11578
11579 /* Mark all sections which are to be included in the
11580 link. This will normally be every section. We need
11581 to do this so that we can identify any sections which
11582 the linker has decided to not include. */
11583 sec->linker_mark = TRUE;
11584
11585 if (sec->flags & SEC_MERGE)
11586 merged = TRUE;
11587
11588 if (sec->rawsize > max_contents_size)
11589 max_contents_size = sec->rawsize;
11590 if (sec->size > max_contents_size)
11591 max_contents_size = sec->size;
11592
11593 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11594 && (sec->owner->flags & DYNAMIC) == 0)
11595 {
11596 size_t sym_count;
11597
11598 /* We are interested in just local symbols, not all
11599 symbols. */
11600 if (elf_bad_symtab (sec->owner))
11601 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11602 / bed->s->sizeof_sym);
11603 else
11604 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11605
11606 if (sym_count > max_sym_count)
11607 max_sym_count = sym_count;
11608
11609 if (sym_count > max_sym_shndx_count
11610 && elf_symtab_shndx_list (sec->owner) != NULL)
11611 max_sym_shndx_count = sym_count;
11612
11613 if (esdo->this_hdr.sh_type == SHT_REL
11614 || esdo->this_hdr.sh_type == SHT_RELA)
11615 /* Some backends use reloc_count in relocation sections
11616 to count particular types of relocs. Of course,
11617 reloc sections themselves can't have relocations. */
11618 ;
11619 else if (emit_relocs)
11620 {
11621 reloc_count = sec->reloc_count;
11622 if (bed->elf_backend_count_additional_relocs)
11623 {
11624 int c;
11625 c = (*bed->elf_backend_count_additional_relocs) (sec);
11626 additional_reloc_count += c;
11627 }
11628 }
11629 else if (bed->elf_backend_count_relocs)
11630 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11631
11632 esdi = elf_section_data (sec);
11633
11634 if ((sec->flags & SEC_RELOC) != 0)
11635 {
11636 size_t ext_size = 0;
11637
11638 if (esdi->rel.hdr != NULL)
11639 ext_size = esdi->rel.hdr->sh_size;
11640 if (esdi->rela.hdr != NULL)
11641 ext_size += esdi->rela.hdr->sh_size;
11642
11643 if (ext_size > max_external_reloc_size)
11644 max_external_reloc_size = ext_size;
11645 if (sec->reloc_count > max_internal_reloc_count)
11646 max_internal_reloc_count = sec->reloc_count;
11647 }
11648 }
11649 }
11650
11651 if (reloc_count == 0)
11652 continue;
11653
11654 reloc_count += additional_reloc_count;
11655 o->reloc_count += reloc_count;
11656
11657 if (p->type == bfd_indirect_link_order && emit_relocs)
11658 {
11659 if (esdi->rel.hdr)
11660 {
11661 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11662 esdo->rel.count += additional_reloc_count;
11663 }
11664 if (esdi->rela.hdr)
11665 {
11666 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11667 esdo->rela.count += additional_reloc_count;
11668 }
11669 }
11670 else
11671 {
11672 if (o->use_rela_p)
11673 esdo->rela.count += reloc_count;
11674 else
11675 esdo->rel.count += reloc_count;
11676 }
11677 }
11678
11679 if (o->reloc_count > 0)
11680 o->flags |= SEC_RELOC;
11681 else
11682 {
11683 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11684 set it (this is probably a bug) and if it is set
11685 assign_section_numbers will create a reloc section. */
11686 o->flags &=~ SEC_RELOC;
11687 }
11688
11689 /* If the SEC_ALLOC flag is not set, force the section VMA to
11690 zero. This is done in elf_fake_sections as well, but forcing
11691 the VMA to 0 here will ensure that relocs against these
11692 sections are handled correctly. */
11693 if ((o->flags & SEC_ALLOC) == 0
11694 && ! o->user_set_vma)
11695 o->vma = 0;
11696 }
11697
11698 if (! bfd_link_relocatable (info) && merged)
11699 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
11700
11701 /* Figure out the file positions for everything but the symbol table
11702 and the relocs. We set symcount to force assign_section_numbers
11703 to create a symbol table. */
11704 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
11705 BFD_ASSERT (! abfd->output_has_begun);
11706 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11707 goto error_return;
11708
11709 /* Set sizes, and assign file positions for reloc sections. */
11710 for (o = abfd->sections; o != NULL; o = o->next)
11711 {
11712 struct bfd_elf_section_data *esdo = elf_section_data (o);
11713 if ((o->flags & SEC_RELOC) != 0)
11714 {
11715 if (esdo->rel.hdr
11716 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
11717 goto error_return;
11718
11719 if (esdo->rela.hdr
11720 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
11721 goto error_return;
11722 }
11723
11724 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11725 to count upwards while actually outputting the relocations. */
11726 esdo->rel.count = 0;
11727 esdo->rela.count = 0;
11728
11729 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11730 {
11731 /* Cache the section contents so that they can be compressed
11732 later. Use bfd_malloc since it will be freed by
11733 bfd_compress_section_contents. */
11734 unsigned char *contents = esdo->this_hdr.contents;
11735 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11736 abort ();
11737 contents
11738 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11739 if (contents == NULL)
11740 goto error_return;
11741 esdo->this_hdr.contents = contents;
11742 }
11743 }
11744
11745 /* We have now assigned file positions for all the sections except
11746 .symtab, .strtab, and non-loaded reloc sections. We start the
11747 .symtab section at the current file position, and write directly
11748 to it. We build the .strtab section in memory. */
11749 bfd_get_symcount (abfd) = 0;
11750 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11751 /* sh_name is set in prep_headers. */
11752 symtab_hdr->sh_type = SHT_SYMTAB;
11753 /* sh_flags, sh_addr and sh_size all start off zero. */
11754 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11755 /* sh_link is set in assign_section_numbers. */
11756 /* sh_info is set below. */
11757 /* sh_offset is set just below. */
11758 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
11759
11760 if (max_sym_count < 20)
11761 max_sym_count = 20;
11762 htab->strtabsize = max_sym_count;
11763 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11764 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
11765 if (htab->strtab == NULL)
11766 goto error_return;
11767 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11768 flinfo.symshndxbuf
11769 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11770 ? (Elf_External_Sym_Shndx *) -1 : NULL);
11771
11772 if (info->strip != strip_all || emit_relocs)
11773 {
11774 file_ptr off = elf_next_file_pos (abfd);
11775
11776 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11777
11778 /* Note that at this point elf_next_file_pos (abfd) is
11779 incorrect. We do not yet know the size of the .symtab section.
11780 We correct next_file_pos below, after we do know the size. */
11781
11782 /* Start writing out the symbol table. The first symbol is always a
11783 dummy symbol. */
11784 elfsym.st_value = 0;
11785 elfsym.st_size = 0;
11786 elfsym.st_info = 0;
11787 elfsym.st_other = 0;
11788 elfsym.st_shndx = SHN_UNDEF;
11789 elfsym.st_target_internal = 0;
11790 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11791 bfd_und_section_ptr, NULL) != 1)
11792 goto error_return;
11793
11794 /* Output a symbol for each section. We output these even if we are
11795 discarding local symbols, since they are used for relocs. These
11796 symbols have no names. We store the index of each one in the
11797 index field of the section, so that we can find it again when
11798 outputting relocs. */
11799
11800 elfsym.st_size = 0;
11801 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11802 elfsym.st_other = 0;
11803 elfsym.st_value = 0;
11804 elfsym.st_target_internal = 0;
11805 for (i = 1; i < elf_numsections (abfd); i++)
11806 {
11807 o = bfd_section_from_elf_index (abfd, i);
11808 if (o != NULL)
11809 {
11810 o->target_index = bfd_get_symcount (abfd);
11811 elfsym.st_shndx = i;
11812 if (!bfd_link_relocatable (info))
11813 elfsym.st_value = o->vma;
11814 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11815 NULL) != 1)
11816 goto error_return;
11817 }
11818 }
11819 }
11820
11821 /* Allocate some memory to hold information read in from the input
11822 files. */
11823 if (max_contents_size != 0)
11824 {
11825 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11826 if (flinfo.contents == NULL)
11827 goto error_return;
11828 }
11829
11830 if (max_external_reloc_size != 0)
11831 {
11832 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11833 if (flinfo.external_relocs == NULL)
11834 goto error_return;
11835 }
11836
11837 if (max_internal_reloc_count != 0)
11838 {
11839 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
11840 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11841 if (flinfo.internal_relocs == NULL)
11842 goto error_return;
11843 }
11844
11845 if (max_sym_count != 0)
11846 {
11847 amt = max_sym_count * bed->s->sizeof_sym;
11848 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11849 if (flinfo.external_syms == NULL)
11850 goto error_return;
11851
11852 amt = max_sym_count * sizeof (Elf_Internal_Sym);
11853 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11854 if (flinfo.internal_syms == NULL)
11855 goto error_return;
11856
11857 amt = max_sym_count * sizeof (long);
11858 flinfo.indices = (long int *) bfd_malloc (amt);
11859 if (flinfo.indices == NULL)
11860 goto error_return;
11861
11862 amt = max_sym_count * sizeof (asection *);
11863 flinfo.sections = (asection **) bfd_malloc (amt);
11864 if (flinfo.sections == NULL)
11865 goto error_return;
11866 }
11867
11868 if (max_sym_shndx_count != 0)
11869 {
11870 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
11871 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11872 if (flinfo.locsym_shndx == NULL)
11873 goto error_return;
11874 }
11875
11876 if (htab->tls_sec)
11877 {
11878 bfd_vma base, end = 0;
11879 asection *sec;
11880
11881 for (sec = htab->tls_sec;
11882 sec && (sec->flags & SEC_THREAD_LOCAL);
11883 sec = sec->next)
11884 {
11885 bfd_size_type size = sec->size;
11886
11887 if (size == 0
11888 && (sec->flags & SEC_HAS_CONTENTS) == 0)
11889 {
11890 struct bfd_link_order *ord = sec->map_tail.link_order;
11891
11892 if (ord != NULL)
11893 size = ord->offset + ord->size;
11894 }
11895 end = sec->vma + size;
11896 }
11897 base = htab->tls_sec->vma;
11898 /* Only align end of TLS section if static TLS doesn't have special
11899 alignment requirements. */
11900 if (bed->static_tls_alignment == 1)
11901 end = align_power (end, htab->tls_sec->alignment_power);
11902 htab->tls_size = end - base;
11903 }
11904
11905 /* Reorder SHF_LINK_ORDER sections. */
11906 for (o = abfd->sections; o != NULL; o = o->next)
11907 {
11908 if (!elf_fixup_link_order (abfd, o))
11909 return FALSE;
11910 }
11911
11912 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11913 return FALSE;
11914
11915 /* Since ELF permits relocations to be against local symbols, we
11916 must have the local symbols available when we do the relocations.
11917 Since we would rather only read the local symbols once, and we
11918 would rather not keep them in memory, we handle all the
11919 relocations for a single input file at the same time.
11920
11921 Unfortunately, there is no way to know the total number of local
11922 symbols until we have seen all of them, and the local symbol
11923 indices precede the global symbol indices. This means that when
11924 we are generating relocatable output, and we see a reloc against
11925 a global symbol, we can not know the symbol index until we have
11926 finished examining all the local symbols to see which ones we are
11927 going to output. To deal with this, we keep the relocations in
11928 memory, and don't output them until the end of the link. This is
11929 an unfortunate waste of memory, but I don't see a good way around
11930 it. Fortunately, it only happens when performing a relocatable
11931 link, which is not the common case. FIXME: If keep_memory is set
11932 we could write the relocs out and then read them again; I don't
11933 know how bad the memory loss will be. */
11934
11935 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11936 sub->output_has_begun = FALSE;
11937 for (o = abfd->sections; o != NULL; o = o->next)
11938 {
11939 for (p = o->map_head.link_order; p != NULL; p = p->next)
11940 {
11941 if (p->type == bfd_indirect_link_order
11942 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11943 == bfd_target_elf_flavour)
11944 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11945 {
11946 if (! sub->output_has_begun)
11947 {
11948 if (! elf_link_input_bfd (&flinfo, sub))
11949 goto error_return;
11950 sub->output_has_begun = TRUE;
11951 }
11952 }
11953 else if (p->type == bfd_section_reloc_link_order
11954 || p->type == bfd_symbol_reloc_link_order)
11955 {
11956 if (! elf_reloc_link_order (abfd, info, o, p))
11957 goto error_return;
11958 }
11959 else
11960 {
11961 if (! _bfd_default_link_order (abfd, info, o, p))
11962 {
11963 if (p->type == bfd_indirect_link_order
11964 && (bfd_get_flavour (sub)
11965 == bfd_target_elf_flavour)
11966 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11967 != bed->s->elfclass))
11968 {
11969 const char *iclass, *oclass;
11970
11971 switch (bed->s->elfclass)
11972 {
11973 case ELFCLASS64: oclass = "ELFCLASS64"; break;
11974 case ELFCLASS32: oclass = "ELFCLASS32"; break;
11975 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
11976 default: abort ();
11977 }
11978
11979 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
11980 {
11981 case ELFCLASS64: iclass = "ELFCLASS64"; break;
11982 case ELFCLASS32: iclass = "ELFCLASS32"; break;
11983 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
11984 default: abort ();
11985 }
11986
11987 bfd_set_error (bfd_error_wrong_format);
11988 _bfd_error_handler
11989 /* xgettext:c-format */
11990 (_("%B: file class %s incompatible with %s"),
11991 sub, iclass, oclass);
11992 }
11993
11994 goto error_return;
11995 }
11996 }
11997 }
11998 }
11999
12000 /* Free symbol buffer if needed. */
12001 if (!info->reduce_memory_overheads)
12002 {
12003 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12004 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
12005 && elf_tdata (sub)->symbuf)
12006 {
12007 free (elf_tdata (sub)->symbuf);
12008 elf_tdata (sub)->symbuf = NULL;
12009 }
12010 }
12011
12012 /* Output any global symbols that got converted to local in a
12013 version script or due to symbol visibility. We do this in a
12014 separate step since ELF requires all local symbols to appear
12015 prior to any global symbols. FIXME: We should only do this if
12016 some global symbols were, in fact, converted to become local.
12017 FIXME: Will this work correctly with the Irix 5 linker? */
12018 eoinfo.failed = FALSE;
12019 eoinfo.flinfo = &flinfo;
12020 eoinfo.localsyms = TRUE;
12021 eoinfo.file_sym_done = FALSE;
12022 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12023 if (eoinfo.failed)
12024 return FALSE;
12025
12026 /* If backend needs to output some local symbols not present in the hash
12027 table, do it now. */
12028 if (bed->elf_backend_output_arch_local_syms
12029 && (info->strip != strip_all || emit_relocs))
12030 {
12031 typedef int (*out_sym_func)
12032 (void *, const char *, Elf_Internal_Sym *, asection *,
12033 struct elf_link_hash_entry *);
12034
12035 if (! ((*bed->elf_backend_output_arch_local_syms)
12036 (abfd, info, &flinfo,
12037 (out_sym_func) elf_link_output_symstrtab)))
12038 return FALSE;
12039 }
12040
12041 /* That wrote out all the local symbols. Finish up the symbol table
12042 with the global symbols. Even if we want to strip everything we
12043 can, we still need to deal with those global symbols that got
12044 converted to local in a version script. */
12045
12046 /* The sh_info field records the index of the first non local symbol. */
12047 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12048
12049 if (dynamic
12050 && htab->dynsym != NULL
12051 && htab->dynsym->output_section != bfd_abs_section_ptr)
12052 {
12053 Elf_Internal_Sym sym;
12054 bfd_byte *dynsym = htab->dynsym->contents;
12055
12056 o = htab->dynsym->output_section;
12057 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12058
12059 /* Write out the section symbols for the output sections. */
12060 if (bfd_link_pic (info)
12061 || htab->is_relocatable_executable)
12062 {
12063 asection *s;
12064
12065 sym.st_size = 0;
12066 sym.st_name = 0;
12067 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12068 sym.st_other = 0;
12069 sym.st_target_internal = 0;
12070
12071 for (s = abfd->sections; s != NULL; s = s->next)
12072 {
12073 int indx;
12074 bfd_byte *dest;
12075 long dynindx;
12076
12077 dynindx = elf_section_data (s)->dynindx;
12078 if (dynindx <= 0)
12079 continue;
12080 indx = elf_section_data (s)->this_idx;
12081 BFD_ASSERT (indx > 0);
12082 sym.st_shndx = indx;
12083 if (! check_dynsym (abfd, &sym))
12084 return FALSE;
12085 sym.st_value = s->vma;
12086 dest = dynsym + dynindx * bed->s->sizeof_sym;
12087 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12088 }
12089 }
12090
12091 /* Write out the local dynsyms. */
12092 if (htab->dynlocal)
12093 {
12094 struct elf_link_local_dynamic_entry *e;
12095 for (e = htab->dynlocal; e ; e = e->next)
12096 {
12097 asection *s;
12098 bfd_byte *dest;
12099
12100 /* Copy the internal symbol and turn off visibility.
12101 Note that we saved a word of storage and overwrote
12102 the original st_name with the dynstr_index. */
12103 sym = e->isym;
12104 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12105
12106 s = bfd_section_from_elf_index (e->input_bfd,
12107 e->isym.st_shndx);
12108 if (s != NULL)
12109 {
12110 sym.st_shndx =
12111 elf_section_data (s->output_section)->this_idx;
12112 if (! check_dynsym (abfd, &sym))
12113 return FALSE;
12114 sym.st_value = (s->output_section->vma
12115 + s->output_offset
12116 + e->isym.st_value);
12117 }
12118
12119 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12120 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12121 }
12122 }
12123 }
12124
12125 /* We get the global symbols from the hash table. */
12126 eoinfo.failed = FALSE;
12127 eoinfo.localsyms = FALSE;
12128 eoinfo.flinfo = &flinfo;
12129 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12130 if (eoinfo.failed)
12131 return FALSE;
12132
12133 /* If backend needs to output some symbols not present in the hash
12134 table, do it now. */
12135 if (bed->elf_backend_output_arch_syms
12136 && (info->strip != strip_all || emit_relocs))
12137 {
12138 typedef int (*out_sym_func)
12139 (void *, const char *, Elf_Internal_Sym *, asection *,
12140 struct elf_link_hash_entry *);
12141
12142 if (! ((*bed->elf_backend_output_arch_syms)
12143 (abfd, info, &flinfo,
12144 (out_sym_func) elf_link_output_symstrtab)))
12145 return FALSE;
12146 }
12147
12148 /* Finalize the .strtab section. */
12149 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12150
12151 /* Swap out the .strtab section. */
12152 if (!elf_link_swap_symbols_out (&flinfo))
12153 return FALSE;
12154
12155 /* Now we know the size of the symtab section. */
12156 if (bfd_get_symcount (abfd) > 0)
12157 {
12158 /* Finish up and write out the symbol string table (.strtab)
12159 section. */
12160 Elf_Internal_Shdr *symstrtab_hdr = NULL;
12161 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12162
12163 if (elf_symtab_shndx_list (abfd))
12164 {
12165 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12166
12167 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12168 {
12169 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12170 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12171 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12172 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12173 symtab_shndx_hdr->sh_size = amt;
12174
12175 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12176 off, TRUE);
12177
12178 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12179 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12180 return FALSE;
12181 }
12182 }
12183
12184 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12185 /* sh_name was set in prep_headers. */
12186 symstrtab_hdr->sh_type = SHT_STRTAB;
12187 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
12188 symstrtab_hdr->sh_addr = 0;
12189 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
12190 symstrtab_hdr->sh_entsize = 0;
12191 symstrtab_hdr->sh_link = 0;
12192 symstrtab_hdr->sh_info = 0;
12193 /* sh_offset is set just below. */
12194 symstrtab_hdr->sh_addralign = 1;
12195
12196 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12197 off, TRUE);
12198 elf_next_file_pos (abfd) = off;
12199
12200 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12201 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12202 return FALSE;
12203 }
12204
12205 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12206 {
12207 _bfd_error_handler (_("%B: failed to generate import library"),
12208 info->out_implib_bfd);
12209 return FALSE;
12210 }
12211
12212 /* Adjust the relocs to have the correct symbol indices. */
12213 for (o = abfd->sections; o != NULL; o = o->next)
12214 {
12215 struct bfd_elf_section_data *esdo = elf_section_data (o);
12216 bfd_boolean sort;
12217
12218 if ((o->flags & SEC_RELOC) == 0)
12219 continue;
12220
12221 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12222 if (esdo->rel.hdr != NULL
12223 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
12224 return FALSE;
12225 if (esdo->rela.hdr != NULL
12226 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
12227 return FALSE;
12228
12229 /* Set the reloc_count field to 0 to prevent write_relocs from
12230 trying to swap the relocs out itself. */
12231 o->reloc_count = 0;
12232 }
12233
12234 if (dynamic && info->combreloc && dynobj != NULL)
12235 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12236
12237 /* If we are linking against a dynamic object, or generating a
12238 shared library, finish up the dynamic linking information. */
12239 if (dynamic)
12240 {
12241 bfd_byte *dyncon, *dynconend;
12242
12243 /* Fix up .dynamic entries. */
12244 o = bfd_get_linker_section (dynobj, ".dynamic");
12245 BFD_ASSERT (o != NULL);
12246
12247 dyncon = o->contents;
12248 dynconend = o->contents + o->size;
12249 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12250 {
12251 Elf_Internal_Dyn dyn;
12252 const char *name;
12253 unsigned int type;
12254 bfd_size_type sh_size;
12255 bfd_vma sh_addr;
12256
12257 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12258
12259 switch (dyn.d_tag)
12260 {
12261 default:
12262 continue;
12263 case DT_NULL:
12264 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12265 {
12266 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12267 {
12268 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12269 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12270 default: continue;
12271 }
12272 dyn.d_un.d_val = relativecount;
12273 relativecount = 0;
12274 break;
12275 }
12276 continue;
12277
12278 case DT_INIT:
12279 name = info->init_function;
12280 goto get_sym;
12281 case DT_FINI:
12282 name = info->fini_function;
12283 get_sym:
12284 {
12285 struct elf_link_hash_entry *h;
12286
12287 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
12288 if (h != NULL
12289 && (h->root.type == bfd_link_hash_defined
12290 || h->root.type == bfd_link_hash_defweak))
12291 {
12292 dyn.d_un.d_ptr = h->root.u.def.value;
12293 o = h->root.u.def.section;
12294 if (o->output_section != NULL)
12295 dyn.d_un.d_ptr += (o->output_section->vma
12296 + o->output_offset);
12297 else
12298 {
12299 /* The symbol is imported from another shared
12300 library and does not apply to this one. */
12301 dyn.d_un.d_ptr = 0;
12302 }
12303 break;
12304 }
12305 }
12306 continue;
12307
12308 case DT_PREINIT_ARRAYSZ:
12309 name = ".preinit_array";
12310 goto get_out_size;
12311 case DT_INIT_ARRAYSZ:
12312 name = ".init_array";
12313 goto get_out_size;
12314 case DT_FINI_ARRAYSZ:
12315 name = ".fini_array";
12316 get_out_size:
12317 o = bfd_get_section_by_name (abfd, name);
12318 if (o == NULL)
12319 {
12320 _bfd_error_handler
12321 (_("could not find section %s"), name);
12322 goto error_return;
12323 }
12324 if (o->size == 0)
12325 _bfd_error_handler
12326 (_("warning: %s section has zero size"), name);
12327 dyn.d_un.d_val = o->size;
12328 break;
12329
12330 case DT_PREINIT_ARRAY:
12331 name = ".preinit_array";
12332 goto get_out_vma;
12333 case DT_INIT_ARRAY:
12334 name = ".init_array";
12335 goto get_out_vma;
12336 case DT_FINI_ARRAY:
12337 name = ".fini_array";
12338 get_out_vma:
12339 o = bfd_get_section_by_name (abfd, name);
12340 goto do_vma;
12341
12342 case DT_HASH:
12343 name = ".hash";
12344 goto get_vma;
12345 case DT_GNU_HASH:
12346 name = ".gnu.hash";
12347 goto get_vma;
12348 case DT_STRTAB:
12349 name = ".dynstr";
12350 goto get_vma;
12351 case DT_SYMTAB:
12352 name = ".dynsym";
12353 goto get_vma;
12354 case DT_VERDEF:
12355 name = ".gnu.version_d";
12356 goto get_vma;
12357 case DT_VERNEED:
12358 name = ".gnu.version_r";
12359 goto get_vma;
12360 case DT_VERSYM:
12361 name = ".gnu.version";
12362 get_vma:
12363 o = bfd_get_linker_section (dynobj, name);
12364 do_vma:
12365 if (o == NULL || bfd_is_abs_section (o->output_section))
12366 {
12367 _bfd_error_handler
12368 (_("could not find section %s"), name);
12369 goto error_return;
12370 }
12371 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12372 {
12373 _bfd_error_handler
12374 (_("warning: section '%s' is being made into a note"), name);
12375 bfd_set_error (bfd_error_nonrepresentable_section);
12376 goto error_return;
12377 }
12378 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
12379 break;
12380
12381 case DT_REL:
12382 case DT_RELA:
12383 case DT_RELSZ:
12384 case DT_RELASZ:
12385 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12386 type = SHT_REL;
12387 else
12388 type = SHT_RELA;
12389 sh_size = 0;
12390 sh_addr = 0;
12391 for (i = 1; i < elf_numsections (abfd); i++)
12392 {
12393 Elf_Internal_Shdr *hdr;
12394
12395 hdr = elf_elfsections (abfd)[i];
12396 if (hdr->sh_type == type
12397 && (hdr->sh_flags & SHF_ALLOC) != 0)
12398 {
12399 sh_size += hdr->sh_size;
12400 if (sh_addr == 0
12401 || sh_addr > hdr->sh_addr)
12402 sh_addr = hdr->sh_addr;
12403 }
12404 }
12405
12406 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12407 {
12408 /* Don't count procedure linkage table relocs in the
12409 overall reloc count. */
12410 sh_size -= htab->srelplt->size;
12411 if (sh_size == 0)
12412 /* If the size is zero, make the address zero too.
12413 This is to avoid a glibc bug. If the backend
12414 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12415 zero, then we'll put DT_RELA at the end of
12416 DT_JMPREL. glibc will interpret the end of
12417 DT_RELA matching the end of DT_JMPREL as the
12418 case where DT_RELA includes DT_JMPREL, and for
12419 LD_BIND_NOW will decide that processing DT_RELA
12420 will process the PLT relocs too. Net result:
12421 No PLT relocs applied. */
12422 sh_addr = 0;
12423
12424 /* If .rela.plt is the first .rela section, exclude
12425 it from DT_RELA. */
12426 else if (sh_addr == (htab->srelplt->output_section->vma
12427 + htab->srelplt->output_offset))
12428 sh_addr += htab->srelplt->size;
12429 }
12430
12431 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12432 dyn.d_un.d_val = sh_size;
12433 else
12434 dyn.d_un.d_ptr = sh_addr;
12435 break;
12436 }
12437 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12438 }
12439 }
12440
12441 /* If we have created any dynamic sections, then output them. */
12442 if (dynobj != NULL)
12443 {
12444 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12445 goto error_return;
12446
12447 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12448 if (((info->warn_shared_textrel && bfd_link_pic (info))
12449 || info->error_textrel)
12450 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
12451 {
12452 bfd_byte *dyncon, *dynconend;
12453
12454 dyncon = o->contents;
12455 dynconend = o->contents + o->size;
12456 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12457 {
12458 Elf_Internal_Dyn dyn;
12459
12460 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12461
12462 if (dyn.d_tag == DT_TEXTREL)
12463 {
12464 if (info->error_textrel)
12465 info->callbacks->einfo
12466 (_("%P%X: read-only segment has dynamic relocations.\n"));
12467 else
12468 info->callbacks->einfo
12469 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
12470 break;
12471 }
12472 }
12473 }
12474
12475 for (o = dynobj->sections; o != NULL; o = o->next)
12476 {
12477 if ((o->flags & SEC_HAS_CONTENTS) == 0
12478 || o->size == 0
12479 || o->output_section == bfd_abs_section_ptr)
12480 continue;
12481 if ((o->flags & SEC_LINKER_CREATED) == 0)
12482 {
12483 /* At this point, we are only interested in sections
12484 created by _bfd_elf_link_create_dynamic_sections. */
12485 continue;
12486 }
12487 if (htab->stab_info.stabstr == o)
12488 continue;
12489 if (htab->eh_info.hdr_sec == o)
12490 continue;
12491 if (strcmp (o->name, ".dynstr") != 0)
12492 {
12493 if (! bfd_set_section_contents (abfd, o->output_section,
12494 o->contents,
12495 (file_ptr) o->output_offset
12496 * bfd_octets_per_byte (abfd),
12497 o->size))
12498 goto error_return;
12499 }
12500 else
12501 {
12502 /* The contents of the .dynstr section are actually in a
12503 stringtab. */
12504 file_ptr off;
12505
12506 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12507 if (bfd_seek (abfd, off, SEEK_SET) != 0
12508 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
12509 goto error_return;
12510 }
12511 }
12512 }
12513
12514 if (!info->resolve_section_groups)
12515 {
12516 bfd_boolean failed = FALSE;
12517
12518 BFD_ASSERT (bfd_link_relocatable (info));
12519 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12520 if (failed)
12521 goto error_return;
12522 }
12523
12524 /* If we have optimized stabs strings, output them. */
12525 if (htab->stab_info.stabstr != NULL)
12526 {
12527 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
12528 goto error_return;
12529 }
12530
12531 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12532 goto error_return;
12533
12534 elf_final_link_free (abfd, &flinfo);
12535
12536 elf_linker (abfd) = TRUE;
12537
12538 if (attr_section)
12539 {
12540 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
12541 if (contents == NULL)
12542 return FALSE; /* Bail out and fail. */
12543 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12544 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12545 free (contents);
12546 }
12547
12548 return TRUE;
12549
12550 error_return:
12551 elf_final_link_free (abfd, &flinfo);
12552 return FALSE;
12553 }
12554 \f
12555 /* Initialize COOKIE for input bfd ABFD. */
12556
12557 static bfd_boolean
12558 init_reloc_cookie (struct elf_reloc_cookie *cookie,
12559 struct bfd_link_info *info, bfd *abfd)
12560 {
12561 Elf_Internal_Shdr *symtab_hdr;
12562 const struct elf_backend_data *bed;
12563
12564 bed = get_elf_backend_data (abfd);
12565 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12566
12567 cookie->abfd = abfd;
12568 cookie->sym_hashes = elf_sym_hashes (abfd);
12569 cookie->bad_symtab = elf_bad_symtab (abfd);
12570 if (cookie->bad_symtab)
12571 {
12572 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12573 cookie->extsymoff = 0;
12574 }
12575 else
12576 {
12577 cookie->locsymcount = symtab_hdr->sh_info;
12578 cookie->extsymoff = symtab_hdr->sh_info;
12579 }
12580
12581 if (bed->s->arch_size == 32)
12582 cookie->r_sym_shift = 8;
12583 else
12584 cookie->r_sym_shift = 32;
12585
12586 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12587 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12588 {
12589 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12590 cookie->locsymcount, 0,
12591 NULL, NULL, NULL);
12592 if (cookie->locsyms == NULL)
12593 {
12594 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12595 return FALSE;
12596 }
12597 if (info->keep_memory)
12598 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12599 }
12600 return TRUE;
12601 }
12602
12603 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12604
12605 static void
12606 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12607 {
12608 Elf_Internal_Shdr *symtab_hdr;
12609
12610 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12611 if (cookie->locsyms != NULL
12612 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12613 free (cookie->locsyms);
12614 }
12615
12616 /* Initialize the relocation information in COOKIE for input section SEC
12617 of input bfd ABFD. */
12618
12619 static bfd_boolean
12620 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12621 struct bfd_link_info *info, bfd *abfd,
12622 asection *sec)
12623 {
12624 if (sec->reloc_count == 0)
12625 {
12626 cookie->rels = NULL;
12627 cookie->relend = NULL;
12628 }
12629 else
12630 {
12631 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12632 info->keep_memory);
12633 if (cookie->rels == NULL)
12634 return FALSE;
12635 cookie->rel = cookie->rels;
12636 cookie->relend = cookie->rels + sec->reloc_count;
12637 }
12638 cookie->rel = cookie->rels;
12639 return TRUE;
12640 }
12641
12642 /* Free the memory allocated by init_reloc_cookie_rels,
12643 if appropriate. */
12644
12645 static void
12646 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12647 asection *sec)
12648 {
12649 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12650 free (cookie->rels);
12651 }
12652
12653 /* Initialize the whole of COOKIE for input section SEC. */
12654
12655 static bfd_boolean
12656 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12657 struct bfd_link_info *info,
12658 asection *sec)
12659 {
12660 if (!init_reloc_cookie (cookie, info, sec->owner))
12661 goto error1;
12662 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12663 goto error2;
12664 return TRUE;
12665
12666 error2:
12667 fini_reloc_cookie (cookie, sec->owner);
12668 error1:
12669 return FALSE;
12670 }
12671
12672 /* Free the memory allocated by init_reloc_cookie_for_section,
12673 if appropriate. */
12674
12675 static void
12676 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12677 asection *sec)
12678 {
12679 fini_reloc_cookie_rels (cookie, sec);
12680 fini_reloc_cookie (cookie, sec->owner);
12681 }
12682 \f
12683 /* Garbage collect unused sections. */
12684
12685 /* Default gc_mark_hook. */
12686
12687 asection *
12688 _bfd_elf_gc_mark_hook (asection *sec,
12689 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12690 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12691 struct elf_link_hash_entry *h,
12692 Elf_Internal_Sym *sym)
12693 {
12694 if (h != NULL)
12695 {
12696 switch (h->root.type)
12697 {
12698 case bfd_link_hash_defined:
12699 case bfd_link_hash_defweak:
12700 return h->root.u.def.section;
12701
12702 case bfd_link_hash_common:
12703 return h->root.u.c.p->section;
12704
12705 default:
12706 break;
12707 }
12708 }
12709 else
12710 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12711
12712 return NULL;
12713 }
12714
12715 /* Return the global debug definition section. */
12716
12717 static asection *
12718 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
12719 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12720 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12721 struct elf_link_hash_entry *h,
12722 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
12723 {
12724 if (h != NULL
12725 && (h->root.type == bfd_link_hash_defined
12726 || h->root.type == bfd_link_hash_defweak)
12727 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
12728 return h->root.u.def.section;
12729
12730 return NULL;
12731 }
12732
12733 /* COOKIE->rel describes a relocation against section SEC, which is
12734 a section we've decided to keep. Return the section that contains
12735 the relocation symbol, or NULL if no section contains it. */
12736
12737 asection *
12738 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12739 elf_gc_mark_hook_fn gc_mark_hook,
12740 struct elf_reloc_cookie *cookie,
12741 bfd_boolean *start_stop)
12742 {
12743 unsigned long r_symndx;
12744 struct elf_link_hash_entry *h;
12745
12746 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
12747 if (r_symndx == STN_UNDEF)
12748 return NULL;
12749
12750 if (r_symndx >= cookie->locsymcount
12751 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12752 {
12753 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
12754 if (h == NULL)
12755 {
12756 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12757 sec->owner);
12758 return NULL;
12759 }
12760 while (h->root.type == bfd_link_hash_indirect
12761 || h->root.type == bfd_link_hash_warning)
12762 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12763 h->mark = 1;
12764 /* If this symbol is weak and there is a non-weak definition, we
12765 keep the non-weak definition because many backends put
12766 dynamic reloc info on the non-weak definition for code
12767 handling copy relocs. */
12768 if (h->is_weakalias)
12769 weakdef (h)->mark = 1;
12770
12771 if (start_stop != NULL)
12772 {
12773 /* To work around a glibc bug, mark XXX input sections
12774 when there is a reference to __start_XXX or __stop_XXX
12775 symbols. */
12776 if (h->start_stop)
12777 {
12778 asection *s = h->u2.start_stop_section;
12779 *start_stop = !s->gc_mark;
12780 return s;
12781 }
12782 }
12783
12784 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12785 }
12786
12787 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12788 &cookie->locsyms[r_symndx]);
12789 }
12790
12791 /* COOKIE->rel describes a relocation against section SEC, which is
12792 a section we've decided to keep. Mark the section that contains
12793 the relocation symbol. */
12794
12795 bfd_boolean
12796 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12797 asection *sec,
12798 elf_gc_mark_hook_fn gc_mark_hook,
12799 struct elf_reloc_cookie *cookie)
12800 {
12801 asection *rsec;
12802 bfd_boolean start_stop = FALSE;
12803
12804 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12805 while (rsec != NULL)
12806 {
12807 if (!rsec->gc_mark)
12808 {
12809 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12810 || (rsec->owner->flags & DYNAMIC) != 0)
12811 rsec->gc_mark = 1;
12812 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12813 return FALSE;
12814 }
12815 if (!start_stop)
12816 break;
12817 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
12818 }
12819 return TRUE;
12820 }
12821
12822 /* The mark phase of garbage collection. For a given section, mark
12823 it and any sections in this section's group, and all the sections
12824 which define symbols to which it refers. */
12825
12826 bfd_boolean
12827 _bfd_elf_gc_mark (struct bfd_link_info *info,
12828 asection *sec,
12829 elf_gc_mark_hook_fn gc_mark_hook)
12830 {
12831 bfd_boolean ret;
12832 asection *group_sec, *eh_frame;
12833
12834 sec->gc_mark = 1;
12835
12836 /* Mark all the sections in the group. */
12837 group_sec = elf_section_data (sec)->next_in_group;
12838 if (group_sec && !group_sec->gc_mark)
12839 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
12840 return FALSE;
12841
12842 /* Look through the section relocs. */
12843 ret = TRUE;
12844 eh_frame = elf_eh_frame_section (sec->owner);
12845 if ((sec->flags & SEC_RELOC) != 0
12846 && sec->reloc_count > 0
12847 && sec != eh_frame)
12848 {
12849 struct elf_reloc_cookie cookie;
12850
12851 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12852 ret = FALSE;
12853 else
12854 {
12855 for (; cookie.rel < cookie.relend; cookie.rel++)
12856 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
12857 {
12858 ret = FALSE;
12859 break;
12860 }
12861 fini_reloc_cookie_for_section (&cookie, sec);
12862 }
12863 }
12864
12865 if (ret && eh_frame && elf_fde_list (sec))
12866 {
12867 struct elf_reloc_cookie cookie;
12868
12869 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12870 ret = FALSE;
12871 else
12872 {
12873 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12874 gc_mark_hook, &cookie))
12875 ret = FALSE;
12876 fini_reloc_cookie_for_section (&cookie, eh_frame);
12877 }
12878 }
12879
12880 eh_frame = elf_section_eh_frame_entry (sec);
12881 if (ret && eh_frame && !eh_frame->gc_mark)
12882 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12883 ret = FALSE;
12884
12885 return ret;
12886 }
12887
12888 /* Scan and mark sections in a special or debug section group. */
12889
12890 static void
12891 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12892 {
12893 /* Point to first section of section group. */
12894 asection *ssec;
12895 /* Used to iterate the section group. */
12896 asection *msec;
12897
12898 bfd_boolean is_special_grp = TRUE;
12899 bfd_boolean is_debug_grp = TRUE;
12900
12901 /* First scan to see if group contains any section other than debug
12902 and special section. */
12903 ssec = msec = elf_next_in_group (grp);
12904 do
12905 {
12906 if ((msec->flags & SEC_DEBUGGING) == 0)
12907 is_debug_grp = FALSE;
12908
12909 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12910 is_special_grp = FALSE;
12911
12912 msec = elf_next_in_group (msec);
12913 }
12914 while (msec != ssec);
12915
12916 /* If this is a pure debug section group or pure special section group,
12917 keep all sections in this group. */
12918 if (is_debug_grp || is_special_grp)
12919 {
12920 do
12921 {
12922 msec->gc_mark = 1;
12923 msec = elf_next_in_group (msec);
12924 }
12925 while (msec != ssec);
12926 }
12927 }
12928
12929 /* Keep debug and special sections. */
12930
12931 bfd_boolean
12932 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12933 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12934 {
12935 bfd *ibfd;
12936
12937 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12938 {
12939 asection *isec;
12940 bfd_boolean some_kept;
12941 bfd_boolean debug_frag_seen;
12942 bfd_boolean has_kept_debug_info;
12943
12944 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12945 continue;
12946 isec = ibfd->sections;
12947 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
12948 continue;
12949
12950 /* Ensure all linker created sections are kept,
12951 see if any other section is already marked,
12952 and note if we have any fragmented debug sections. */
12953 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
12954 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12955 {
12956 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12957 isec->gc_mark = 1;
12958 else if (isec->gc_mark
12959 && (isec->flags & SEC_ALLOC) != 0
12960 && elf_section_type (isec) != SHT_NOTE)
12961 some_kept = TRUE;
12962
12963 if (!debug_frag_seen
12964 && (isec->flags & SEC_DEBUGGING)
12965 && CONST_STRNEQ (isec->name, ".debug_line."))
12966 debug_frag_seen = TRUE;
12967 }
12968
12969 /* If no non-note alloc section in this file will be kept, then
12970 we can toss out the debug and special sections. */
12971 if (!some_kept)
12972 continue;
12973
12974 /* Keep debug and special sections like .comment when they are
12975 not part of a group. Also keep section groups that contain
12976 just debug sections or special sections. */
12977 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12978 {
12979 if ((isec->flags & SEC_GROUP) != 0)
12980 _bfd_elf_gc_mark_debug_special_section_group (isec);
12981 else if (((isec->flags & SEC_DEBUGGING) != 0
12982 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12983 && elf_next_in_group (isec) == NULL)
12984 isec->gc_mark = 1;
12985 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
12986 has_kept_debug_info = TRUE;
12987 }
12988
12989 /* Look for CODE sections which are going to be discarded,
12990 and find and discard any fragmented debug sections which
12991 are associated with that code section. */
12992 if (debug_frag_seen)
12993 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12994 if ((isec->flags & SEC_CODE) != 0
12995 && isec->gc_mark == 0)
12996 {
12997 unsigned int ilen;
12998 asection *dsec;
12999
13000 ilen = strlen (isec->name);
13001
13002 /* Association is determined by the name of the debug
13003 section containing the name of the code section as
13004 a suffix. For example .debug_line.text.foo is a
13005 debug section associated with .text.foo. */
13006 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13007 {
13008 unsigned int dlen;
13009
13010 if (dsec->gc_mark == 0
13011 || (dsec->flags & SEC_DEBUGGING) == 0)
13012 continue;
13013
13014 dlen = strlen (dsec->name);
13015
13016 if (dlen > ilen
13017 && strncmp (dsec->name + (dlen - ilen),
13018 isec->name, ilen) == 0)
13019 dsec->gc_mark = 0;
13020 }
13021 }
13022
13023 /* Mark debug sections referenced by kept debug sections. */
13024 if (has_kept_debug_info)
13025 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13026 if (isec->gc_mark
13027 && (isec->flags & SEC_DEBUGGING) != 0)
13028 if (!_bfd_elf_gc_mark (info, isec,
13029 elf_gc_mark_debug_section))
13030 return FALSE;
13031 }
13032 return TRUE;
13033 }
13034
13035 static bfd_boolean
13036 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
13037 {
13038 bfd *sub;
13039 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13040
13041 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13042 {
13043 asection *o;
13044
13045 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13046 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
13047 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13048 continue;
13049 o = sub->sections;
13050 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13051 continue;
13052
13053 for (o = sub->sections; o != NULL; o = o->next)
13054 {
13055 /* When any section in a section group is kept, we keep all
13056 sections in the section group. If the first member of
13057 the section group is excluded, we will also exclude the
13058 group section. */
13059 if (o->flags & SEC_GROUP)
13060 {
13061 asection *first = elf_next_in_group (o);
13062 o->gc_mark = first->gc_mark;
13063 }
13064
13065 if (o->gc_mark)
13066 continue;
13067
13068 /* Skip sweeping sections already excluded. */
13069 if (o->flags & SEC_EXCLUDE)
13070 continue;
13071
13072 /* Since this is early in the link process, it is simple
13073 to remove a section from the output. */
13074 o->flags |= SEC_EXCLUDE;
13075
13076 if (info->print_gc_sections && o->size != 0)
13077 /* xgettext:c-format */
13078 _bfd_error_handler (_("Removing unused section '%A' in file '%B'"),
13079 o, sub);
13080 }
13081 }
13082
13083 return TRUE;
13084 }
13085
13086 /* Propagate collected vtable information. This is called through
13087 elf_link_hash_traverse. */
13088
13089 static bfd_boolean
13090 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13091 {
13092 /* Those that are not vtables. */
13093 if (h->start_stop
13094 || h->u2.vtable == NULL
13095 || h->u2.vtable->parent == NULL)
13096 return TRUE;
13097
13098 /* Those vtables that do not have parents, we cannot merge. */
13099 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
13100 return TRUE;
13101
13102 /* If we've already been done, exit. */
13103 if (h->u2.vtable->used && h->u2.vtable->used[-1])
13104 return TRUE;
13105
13106 /* Make sure the parent's table is up to date. */
13107 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
13108
13109 if (h->u2.vtable->used == NULL)
13110 {
13111 /* None of this table's entries were referenced. Re-use the
13112 parent's table. */
13113 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13114 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
13115 }
13116 else
13117 {
13118 size_t n;
13119 bfd_boolean *cu, *pu;
13120
13121 /* Or the parent's entries into ours. */
13122 cu = h->u2.vtable->used;
13123 cu[-1] = TRUE;
13124 pu = h->u2.vtable->parent->u2.vtable->used;
13125 if (pu != NULL)
13126 {
13127 const struct elf_backend_data *bed;
13128 unsigned int log_file_align;
13129
13130 bed = get_elf_backend_data (h->root.u.def.section->owner);
13131 log_file_align = bed->s->log_file_align;
13132 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
13133 while (n--)
13134 {
13135 if (*pu)
13136 *cu = TRUE;
13137 pu++;
13138 cu++;
13139 }
13140 }
13141 }
13142
13143 return TRUE;
13144 }
13145
13146 static bfd_boolean
13147 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13148 {
13149 asection *sec;
13150 bfd_vma hstart, hend;
13151 Elf_Internal_Rela *relstart, *relend, *rel;
13152 const struct elf_backend_data *bed;
13153 unsigned int log_file_align;
13154
13155 /* Take care of both those symbols that do not describe vtables as
13156 well as those that are not loaded. */
13157 if (h->start_stop
13158 || h->u2.vtable == NULL
13159 || h->u2.vtable->parent == NULL)
13160 return TRUE;
13161
13162 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13163 || h->root.type == bfd_link_hash_defweak);
13164
13165 sec = h->root.u.def.section;
13166 hstart = h->root.u.def.value;
13167 hend = hstart + h->size;
13168
13169 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13170 if (!relstart)
13171 return *(bfd_boolean *) okp = FALSE;
13172 bed = get_elf_backend_data (sec->owner);
13173 log_file_align = bed->s->log_file_align;
13174
13175 relend = relstart + sec->reloc_count;
13176
13177 for (rel = relstart; rel < relend; ++rel)
13178 if (rel->r_offset >= hstart && rel->r_offset < hend)
13179 {
13180 /* If the entry is in use, do nothing. */
13181 if (h->u2.vtable->used
13182 && (rel->r_offset - hstart) < h->u2.vtable->size)
13183 {
13184 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13185 if (h->u2.vtable->used[entry])
13186 continue;
13187 }
13188 /* Otherwise, kill it. */
13189 rel->r_offset = rel->r_info = rel->r_addend = 0;
13190 }
13191
13192 return TRUE;
13193 }
13194
13195 /* Mark sections containing dynamically referenced symbols. When
13196 building shared libraries, we must assume that any visible symbol is
13197 referenced. */
13198
13199 bfd_boolean
13200 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
13201 {
13202 struct bfd_link_info *info = (struct bfd_link_info *) inf;
13203 struct bfd_elf_dynamic_list *d = info->dynamic_list;
13204
13205 if ((h->root.type == bfd_link_hash_defined
13206 || h->root.type == bfd_link_hash_defweak)
13207 && (h->ref_dynamic
13208 || ((h->def_regular || ELF_COMMON_DEF_P (h))
13209 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
13210 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
13211 && (!bfd_link_executable (info)
13212 || info->gc_keep_exported
13213 || info->export_dynamic
13214 || (h->dynamic
13215 && d != NULL
13216 && (*d->match) (&d->head, NULL, h->root.root.string)))
13217 && (h->versioned >= versioned
13218 || !bfd_hide_sym_by_version (info->version_info,
13219 h->root.root.string)))))
13220 h->root.u.def.section->flags |= SEC_KEEP;
13221
13222 return TRUE;
13223 }
13224
13225 /* Keep all sections containing symbols undefined on the command-line,
13226 and the section containing the entry symbol. */
13227
13228 void
13229 _bfd_elf_gc_keep (struct bfd_link_info *info)
13230 {
13231 struct bfd_sym_chain *sym;
13232
13233 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13234 {
13235 struct elf_link_hash_entry *h;
13236
13237 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13238 FALSE, FALSE, FALSE);
13239
13240 if (h != NULL
13241 && (h->root.type == bfd_link_hash_defined
13242 || h->root.type == bfd_link_hash_defweak)
13243 && !bfd_is_abs_section (h->root.u.def.section)
13244 && !bfd_is_und_section (h->root.u.def.section))
13245 h->root.u.def.section->flags |= SEC_KEEP;
13246 }
13247 }
13248
13249 bfd_boolean
13250 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13251 struct bfd_link_info *info)
13252 {
13253 bfd *ibfd = info->input_bfds;
13254
13255 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13256 {
13257 asection *sec;
13258 struct elf_reloc_cookie cookie;
13259
13260 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13261 continue;
13262 sec = ibfd->sections;
13263 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13264 continue;
13265
13266 if (!init_reloc_cookie (&cookie, info, ibfd))
13267 return FALSE;
13268
13269 for (sec = ibfd->sections; sec; sec = sec->next)
13270 {
13271 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13272 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13273 {
13274 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13275 fini_reloc_cookie_rels (&cookie, sec);
13276 }
13277 }
13278 }
13279 return TRUE;
13280 }
13281
13282 /* Do mark and sweep of unused sections. */
13283
13284 bfd_boolean
13285 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13286 {
13287 bfd_boolean ok = TRUE;
13288 bfd *sub;
13289 elf_gc_mark_hook_fn gc_mark_hook;
13290 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13291 struct elf_link_hash_table *htab;
13292
13293 if (!bed->can_gc_sections
13294 || !is_elf_hash_table (info->hash))
13295 {
13296 _bfd_error_handler(_("Warning: gc-sections option ignored"));
13297 return TRUE;
13298 }
13299
13300 bed->gc_keep (info);
13301 htab = elf_hash_table (info);
13302
13303 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13304 at the .eh_frame section if we can mark the FDEs individually. */
13305 for (sub = info->input_bfds;
13306 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13307 sub = sub->link.next)
13308 {
13309 asection *sec;
13310 struct elf_reloc_cookie cookie;
13311
13312 sec = sub->sections;
13313 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13314 continue;
13315 sec = bfd_get_section_by_name (sub, ".eh_frame");
13316 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
13317 {
13318 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
13319 if (elf_section_data (sec)->sec_info
13320 && (sec->flags & SEC_LINKER_CREATED) == 0)
13321 elf_eh_frame_section (sub) = sec;
13322 fini_reloc_cookie_for_section (&cookie, sec);
13323 sec = bfd_get_next_section_by_name (NULL, sec);
13324 }
13325 }
13326
13327 /* Apply transitive closure to the vtable entry usage info. */
13328 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
13329 if (!ok)
13330 return FALSE;
13331
13332 /* Kill the vtable relocations that were not used. */
13333 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
13334 if (!ok)
13335 return FALSE;
13336
13337 /* Mark dynamically referenced symbols. */
13338 if (htab->dynamic_sections_created || info->gc_keep_exported)
13339 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
13340
13341 /* Grovel through relocs to find out who stays ... */
13342 gc_mark_hook = bed->gc_mark_hook;
13343 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13344 {
13345 asection *o;
13346
13347 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13348 || elf_object_id (sub) != elf_hash_table_id (htab)
13349 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13350 continue;
13351
13352 o = sub->sections;
13353 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13354 continue;
13355
13356 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13357 Also treat note sections as a root, if the section is not part
13358 of a group. */
13359 for (o = sub->sections; o != NULL; o = o->next)
13360 if (!o->gc_mark
13361 && (o->flags & SEC_EXCLUDE) == 0
13362 && ((o->flags & SEC_KEEP) != 0
13363 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13364 && elf_next_in_group (o) == NULL )))
13365 {
13366 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13367 return FALSE;
13368 }
13369 }
13370
13371 /* Allow the backend to mark additional target specific sections. */
13372 bed->gc_mark_extra_sections (info, gc_mark_hook);
13373
13374 /* ... and mark SEC_EXCLUDE for those that go. */
13375 return elf_gc_sweep (abfd, info);
13376 }
13377 \f
13378 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13379
13380 bfd_boolean
13381 bfd_elf_gc_record_vtinherit (bfd *abfd,
13382 asection *sec,
13383 struct elf_link_hash_entry *h,
13384 bfd_vma offset)
13385 {
13386 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13387 struct elf_link_hash_entry **search, *child;
13388 size_t extsymcount;
13389 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13390
13391 /* The sh_info field of the symtab header tells us where the
13392 external symbols start. We don't care about the local symbols at
13393 this point. */
13394 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13395 if (!elf_bad_symtab (abfd))
13396 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13397
13398 sym_hashes = elf_sym_hashes (abfd);
13399 sym_hashes_end = sym_hashes + extsymcount;
13400
13401 /* Hunt down the child symbol, which is in this section at the same
13402 offset as the relocation. */
13403 for (search = sym_hashes; search != sym_hashes_end; ++search)
13404 {
13405 if ((child = *search) != NULL
13406 && (child->root.type == bfd_link_hash_defined
13407 || child->root.type == bfd_link_hash_defweak)
13408 && child->root.u.def.section == sec
13409 && child->root.u.def.value == offset)
13410 goto win;
13411 }
13412
13413 /* xgettext:c-format */
13414 _bfd_error_handler (_("%B: %A+%#Lx: No symbol found for INHERIT"),
13415 abfd, sec, offset);
13416 bfd_set_error (bfd_error_invalid_operation);
13417 return FALSE;
13418
13419 win:
13420 if (!child->u2.vtable)
13421 {
13422 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13423 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13424 if (!child->u2.vtable)
13425 return FALSE;
13426 }
13427 if (!h)
13428 {
13429 /* This *should* only be the absolute section. It could potentially
13430 be that someone has defined a non-global vtable though, which
13431 would be bad. It isn't worth paging in the local symbols to be
13432 sure though; that case should simply be handled by the assembler. */
13433
13434 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
13435 }
13436 else
13437 child->u2.vtable->parent = h;
13438
13439 return TRUE;
13440 }
13441
13442 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13443
13444 bfd_boolean
13445 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13446 asection *sec ATTRIBUTE_UNUSED,
13447 struct elf_link_hash_entry *h,
13448 bfd_vma addend)
13449 {
13450 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13451 unsigned int log_file_align = bed->s->log_file_align;
13452
13453 if (!h->u2.vtable)
13454 {
13455 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
13456 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
13457 if (!h->u2.vtable)
13458 return FALSE;
13459 }
13460
13461 if (addend >= h->u2.vtable->size)
13462 {
13463 size_t size, bytes, file_align;
13464 bfd_boolean *ptr = h->u2.vtable->used;
13465
13466 /* While the symbol is undefined, we have to be prepared to handle
13467 a zero size. */
13468 file_align = 1 << log_file_align;
13469 if (h->root.type == bfd_link_hash_undefined)
13470 size = addend + file_align;
13471 else
13472 {
13473 size = h->size;
13474 if (addend >= size)
13475 {
13476 /* Oops! We've got a reference past the defined end of
13477 the table. This is probably a bug -- shall we warn? */
13478 size = addend + file_align;
13479 }
13480 }
13481 size = (size + file_align - 1) & -file_align;
13482
13483 /* Allocate one extra entry for use as a "done" flag for the
13484 consolidation pass. */
13485 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13486
13487 if (ptr)
13488 {
13489 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
13490
13491 if (ptr != NULL)
13492 {
13493 size_t oldbytes;
13494
13495 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
13496 * sizeof (bfd_boolean));
13497 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13498 }
13499 }
13500 else
13501 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
13502
13503 if (ptr == NULL)
13504 return FALSE;
13505
13506 /* And arrange for that done flag to be at index -1. */
13507 h->u2.vtable->used = ptr + 1;
13508 h->u2.vtable->size = size;
13509 }
13510
13511 h->u2.vtable->used[addend >> log_file_align] = TRUE;
13512
13513 return TRUE;
13514 }
13515
13516 /* Map an ELF section header flag to its corresponding string. */
13517 typedef struct
13518 {
13519 char *flag_name;
13520 flagword flag_value;
13521 } elf_flags_to_name_table;
13522
13523 static elf_flags_to_name_table elf_flags_to_names [] =
13524 {
13525 { "SHF_WRITE", SHF_WRITE },
13526 { "SHF_ALLOC", SHF_ALLOC },
13527 { "SHF_EXECINSTR", SHF_EXECINSTR },
13528 { "SHF_MERGE", SHF_MERGE },
13529 { "SHF_STRINGS", SHF_STRINGS },
13530 { "SHF_INFO_LINK", SHF_INFO_LINK},
13531 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13532 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13533 { "SHF_GROUP", SHF_GROUP },
13534 { "SHF_TLS", SHF_TLS },
13535 { "SHF_MASKOS", SHF_MASKOS },
13536 { "SHF_EXCLUDE", SHF_EXCLUDE },
13537 };
13538
13539 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13540 bfd_boolean
13541 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
13542 struct flag_info *flaginfo,
13543 asection *section)
13544 {
13545 const bfd_vma sh_flags = elf_section_flags (section);
13546
13547 if (!flaginfo->flags_initialized)
13548 {
13549 bfd *obfd = info->output_bfd;
13550 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13551 struct flag_info_list *tf = flaginfo->flag_list;
13552 int with_hex = 0;
13553 int without_hex = 0;
13554
13555 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13556 {
13557 unsigned i;
13558 flagword (*lookup) (char *);
13559
13560 lookup = bed->elf_backend_lookup_section_flags_hook;
13561 if (lookup != NULL)
13562 {
13563 flagword hexval = (*lookup) ((char *) tf->name);
13564
13565 if (hexval != 0)
13566 {
13567 if (tf->with == with_flags)
13568 with_hex |= hexval;
13569 else if (tf->with == without_flags)
13570 without_hex |= hexval;
13571 tf->valid = TRUE;
13572 continue;
13573 }
13574 }
13575 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13576 {
13577 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
13578 {
13579 if (tf->with == with_flags)
13580 with_hex |= elf_flags_to_names[i].flag_value;
13581 else if (tf->with == without_flags)
13582 without_hex |= elf_flags_to_names[i].flag_value;
13583 tf->valid = TRUE;
13584 break;
13585 }
13586 }
13587 if (!tf->valid)
13588 {
13589 info->callbacks->einfo
13590 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
13591 return FALSE;
13592 }
13593 }
13594 flaginfo->flags_initialized = TRUE;
13595 flaginfo->only_with_flags |= with_hex;
13596 flaginfo->not_with_flags |= without_hex;
13597 }
13598
13599 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13600 return FALSE;
13601
13602 if ((flaginfo->not_with_flags & sh_flags) != 0)
13603 return FALSE;
13604
13605 return TRUE;
13606 }
13607
13608 struct alloc_got_off_arg {
13609 bfd_vma gotoff;
13610 struct bfd_link_info *info;
13611 };
13612
13613 /* We need a special top-level link routine to convert got reference counts
13614 to real got offsets. */
13615
13616 static bfd_boolean
13617 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13618 {
13619 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13620 bfd *obfd = gofarg->info->output_bfd;
13621 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13622
13623 if (h->got.refcount > 0)
13624 {
13625 h->got.offset = gofarg->gotoff;
13626 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13627 }
13628 else
13629 h->got.offset = (bfd_vma) -1;
13630
13631 return TRUE;
13632 }
13633
13634 /* And an accompanying bit to work out final got entry offsets once
13635 we're done. Should be called from final_link. */
13636
13637 bfd_boolean
13638 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13639 struct bfd_link_info *info)
13640 {
13641 bfd *i;
13642 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13643 bfd_vma gotoff;
13644 struct alloc_got_off_arg gofarg;
13645
13646 BFD_ASSERT (abfd == info->output_bfd);
13647
13648 if (! is_elf_hash_table (info->hash))
13649 return FALSE;
13650
13651 /* The GOT offset is relative to the .got section, but the GOT header is
13652 put into the .got.plt section, if the backend uses it. */
13653 if (bed->want_got_plt)
13654 gotoff = 0;
13655 else
13656 gotoff = bed->got_header_size;
13657
13658 /* Do the local .got entries first. */
13659 for (i = info->input_bfds; i; i = i->link.next)
13660 {
13661 bfd_signed_vma *local_got;
13662 size_t j, locsymcount;
13663 Elf_Internal_Shdr *symtab_hdr;
13664
13665 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13666 continue;
13667
13668 local_got = elf_local_got_refcounts (i);
13669 if (!local_got)
13670 continue;
13671
13672 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13673 if (elf_bad_symtab (i))
13674 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13675 else
13676 locsymcount = symtab_hdr->sh_info;
13677
13678 for (j = 0; j < locsymcount; ++j)
13679 {
13680 if (local_got[j] > 0)
13681 {
13682 local_got[j] = gotoff;
13683 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
13684 }
13685 else
13686 local_got[j] = (bfd_vma) -1;
13687 }
13688 }
13689
13690 /* Then the global .got entries. .plt refcounts are handled by
13691 adjust_dynamic_symbol */
13692 gofarg.gotoff = gotoff;
13693 gofarg.info = info;
13694 elf_link_hash_traverse (elf_hash_table (info),
13695 elf_gc_allocate_got_offsets,
13696 &gofarg);
13697 return TRUE;
13698 }
13699
13700 /* Many folk need no more in the way of final link than this, once
13701 got entry reference counting is enabled. */
13702
13703 bfd_boolean
13704 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13705 {
13706 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13707 return FALSE;
13708
13709 /* Invoke the regular ELF backend linker to do all the work. */
13710 return bfd_elf_final_link (abfd, info);
13711 }
13712
13713 bfd_boolean
13714 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13715 {
13716 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
13717
13718 if (rcookie->bad_symtab)
13719 rcookie->rel = rcookie->rels;
13720
13721 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13722 {
13723 unsigned long r_symndx;
13724
13725 if (! rcookie->bad_symtab)
13726 if (rcookie->rel->r_offset > offset)
13727 return FALSE;
13728 if (rcookie->rel->r_offset != offset)
13729 continue;
13730
13731 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
13732 if (r_symndx == STN_UNDEF)
13733 return TRUE;
13734
13735 if (r_symndx >= rcookie->locsymcount
13736 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13737 {
13738 struct elf_link_hash_entry *h;
13739
13740 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13741
13742 while (h->root.type == bfd_link_hash_indirect
13743 || h->root.type == bfd_link_hash_warning)
13744 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13745
13746 if ((h->root.type == bfd_link_hash_defined
13747 || h->root.type == bfd_link_hash_defweak)
13748 && (h->root.u.def.section->owner != rcookie->abfd
13749 || h->root.u.def.section->kept_section != NULL
13750 || discarded_section (h->root.u.def.section)))
13751 return TRUE;
13752 }
13753 else
13754 {
13755 /* It's not a relocation against a global symbol,
13756 but it could be a relocation against a local
13757 symbol for a discarded section. */
13758 asection *isec;
13759 Elf_Internal_Sym *isym;
13760
13761 /* Need to: get the symbol; get the section. */
13762 isym = &rcookie->locsyms[r_symndx];
13763 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
13764 if (isec != NULL
13765 && (isec->kept_section != NULL
13766 || discarded_section (isec)))
13767 return TRUE;
13768 }
13769 return FALSE;
13770 }
13771 return FALSE;
13772 }
13773
13774 /* Discard unneeded references to discarded sections.
13775 Returns -1 on error, 1 if any section's size was changed, 0 if
13776 nothing changed. This function assumes that the relocations are in
13777 sorted order, which is true for all known assemblers. */
13778
13779 int
13780 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13781 {
13782 struct elf_reloc_cookie cookie;
13783 asection *o;
13784 bfd *abfd;
13785 int changed = 0;
13786
13787 if (info->traditional_format
13788 || !is_elf_hash_table (info->hash))
13789 return 0;
13790
13791 o = bfd_get_section_by_name (output_bfd, ".stab");
13792 if (o != NULL)
13793 {
13794 asection *i;
13795
13796 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13797 {
13798 if (i->size == 0
13799 || i->reloc_count == 0
13800 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13801 continue;
13802
13803 abfd = i->owner;
13804 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13805 continue;
13806
13807 if (!init_reloc_cookie_for_section (&cookie, info, i))
13808 return -1;
13809
13810 if (_bfd_discard_section_stabs (abfd, i,
13811 elf_section_data (i)->sec_info,
13812 bfd_elf_reloc_symbol_deleted_p,
13813 &cookie))
13814 changed = 1;
13815
13816 fini_reloc_cookie_for_section (&cookie, i);
13817 }
13818 }
13819
13820 o = NULL;
13821 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13822 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
13823 if (o != NULL)
13824 {
13825 asection *i;
13826 int eh_changed = 0;
13827 unsigned int eh_alignment;
13828
13829 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13830 {
13831 if (i->size == 0)
13832 continue;
13833
13834 abfd = i->owner;
13835 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13836 continue;
13837
13838 if (!init_reloc_cookie_for_section (&cookie, info, i))
13839 return -1;
13840
13841 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13842 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
13843 bfd_elf_reloc_symbol_deleted_p,
13844 &cookie))
13845 {
13846 eh_changed = 1;
13847 if (i->size != i->rawsize)
13848 changed = 1;
13849 }
13850
13851 fini_reloc_cookie_for_section (&cookie, i);
13852 }
13853
13854 eh_alignment = 1 << o->alignment_power;
13855 /* Skip over zero terminator, and prevent empty sections from
13856 adding alignment padding at the end. */
13857 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
13858 if (i->size == 0)
13859 i->flags |= SEC_EXCLUDE;
13860 else if (i->size > 4)
13861 break;
13862 /* The last non-empty eh_frame section doesn't need padding. */
13863 if (i != NULL)
13864 i = i->map_tail.s;
13865 /* Any prior sections must pad the last FDE out to the output
13866 section alignment. Otherwise we might have zero padding
13867 between sections, which would be seen as a terminator. */
13868 for (; i != NULL; i = i->map_tail.s)
13869 if (i->size == 4)
13870 /* All but the last zero terminator should have been removed. */
13871 BFD_FAIL ();
13872 else
13873 {
13874 bfd_size_type size
13875 = (i->size + eh_alignment - 1) & -eh_alignment;
13876 if (i->size != size)
13877 {
13878 i->size = size;
13879 changed = 1;
13880 eh_changed = 1;
13881 }
13882 }
13883 if (eh_changed)
13884 elf_link_hash_traverse (elf_hash_table (info),
13885 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
13886 }
13887
13888 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13889 {
13890 const struct elf_backend_data *bed;
13891 asection *s;
13892
13893 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13894 continue;
13895 s = abfd->sections;
13896 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13897 continue;
13898
13899 bed = get_elf_backend_data (abfd);
13900
13901 if (bed->elf_backend_discard_info != NULL)
13902 {
13903 if (!init_reloc_cookie (&cookie, info, abfd))
13904 return -1;
13905
13906 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
13907 changed = 1;
13908
13909 fini_reloc_cookie (&cookie, abfd);
13910 }
13911 }
13912
13913 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13914 _bfd_elf_end_eh_frame_parsing (info);
13915
13916 if (info->eh_frame_hdr_type
13917 && !bfd_link_relocatable (info)
13918 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
13919 changed = 1;
13920
13921 return changed;
13922 }
13923
13924 bfd_boolean
13925 _bfd_elf_section_already_linked (bfd *abfd,
13926 asection *sec,
13927 struct bfd_link_info *info)
13928 {
13929 flagword flags;
13930 const char *name, *key;
13931 struct bfd_section_already_linked *l;
13932 struct bfd_section_already_linked_hash_entry *already_linked_list;
13933
13934 if (sec->output_section == bfd_abs_section_ptr)
13935 return FALSE;
13936
13937 flags = sec->flags;
13938
13939 /* Return if it isn't a linkonce section. A comdat group section
13940 also has SEC_LINK_ONCE set. */
13941 if ((flags & SEC_LINK_ONCE) == 0)
13942 return FALSE;
13943
13944 /* Don't put group member sections on our list of already linked
13945 sections. They are handled as a group via their group section. */
13946 if (elf_sec_group (sec) != NULL)
13947 return FALSE;
13948
13949 /* For a SHT_GROUP section, use the group signature as the key. */
13950 name = sec->name;
13951 if ((flags & SEC_GROUP) != 0
13952 && elf_next_in_group (sec) != NULL
13953 && elf_group_name (elf_next_in_group (sec)) != NULL)
13954 key = elf_group_name (elf_next_in_group (sec));
13955 else
13956 {
13957 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
13958 if (CONST_STRNEQ (name, ".gnu.linkonce.")
13959 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13960 key++;
13961 else
13962 /* Must be a user linkonce section that doesn't follow gcc's
13963 naming convention. In this case we won't be matching
13964 single member groups. */
13965 key = name;
13966 }
13967
13968 already_linked_list = bfd_section_already_linked_table_lookup (key);
13969
13970 for (l = already_linked_list->entry; l != NULL; l = l->next)
13971 {
13972 /* We may have 2 different types of sections on the list: group
13973 sections with a signature of <key> (<key> is some string),
13974 and linkonce sections named .gnu.linkonce.<type>.<key>.
13975 Match like sections. LTO plugin sections are an exception.
13976 They are always named .gnu.linkonce.t.<key> and match either
13977 type of section. */
13978 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13979 && ((flags & SEC_GROUP) != 0
13980 || strcmp (name, l->sec->name) == 0))
13981 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
13982 {
13983 /* The section has already been linked. See if we should
13984 issue a warning. */
13985 if (!_bfd_handle_already_linked (sec, l, info))
13986 return FALSE;
13987
13988 if (flags & SEC_GROUP)
13989 {
13990 asection *first = elf_next_in_group (sec);
13991 asection *s = first;
13992
13993 while (s != NULL)
13994 {
13995 s->output_section = bfd_abs_section_ptr;
13996 /* Record which group discards it. */
13997 s->kept_section = l->sec;
13998 s = elf_next_in_group (s);
13999 /* These lists are circular. */
14000 if (s == first)
14001 break;
14002 }
14003 }
14004
14005 return TRUE;
14006 }
14007 }
14008
14009 /* A single member comdat group section may be discarded by a
14010 linkonce section and vice versa. */
14011 if ((flags & SEC_GROUP) != 0)
14012 {
14013 asection *first = elf_next_in_group (sec);
14014
14015 if (first != NULL && elf_next_in_group (first) == first)
14016 /* Check this single member group against linkonce sections. */
14017 for (l = already_linked_list->entry; l != NULL; l = l->next)
14018 if ((l->sec->flags & SEC_GROUP) == 0
14019 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14020 {
14021 first->output_section = bfd_abs_section_ptr;
14022 first->kept_section = l->sec;
14023 sec->output_section = bfd_abs_section_ptr;
14024 break;
14025 }
14026 }
14027 else
14028 /* Check this linkonce section against single member groups. */
14029 for (l = already_linked_list->entry; l != NULL; l = l->next)
14030 if (l->sec->flags & SEC_GROUP)
14031 {
14032 asection *first = elf_next_in_group (l->sec);
14033
14034 if (first != NULL
14035 && elf_next_in_group (first) == first
14036 && bfd_elf_match_symbols_in_sections (first, sec, info))
14037 {
14038 sec->output_section = bfd_abs_section_ptr;
14039 sec->kept_section = first;
14040 break;
14041 }
14042 }
14043
14044 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14045 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14046 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14047 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14048 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14049 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14050 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14051 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14052 The reverse order cannot happen as there is never a bfd with only the
14053 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14054 matter as here were are looking only for cross-bfd sections. */
14055
14056 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14057 for (l = already_linked_list->entry; l != NULL; l = l->next)
14058 if ((l->sec->flags & SEC_GROUP) == 0
14059 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14060 {
14061 if (abfd != l->sec->owner)
14062 sec->output_section = bfd_abs_section_ptr;
14063 break;
14064 }
14065
14066 /* This is the first section with this name. Record it. */
14067 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
14068 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
14069 return sec->output_section == bfd_abs_section_ptr;
14070 }
14071
14072 bfd_boolean
14073 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
14074 {
14075 return sym->st_shndx == SHN_COMMON;
14076 }
14077
14078 unsigned int
14079 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14080 {
14081 return SHN_COMMON;
14082 }
14083
14084 asection *
14085 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14086 {
14087 return bfd_com_section_ptr;
14088 }
14089
14090 bfd_vma
14091 _bfd_elf_default_got_elt_size (bfd *abfd,
14092 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14093 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14094 bfd *ibfd ATTRIBUTE_UNUSED,
14095 unsigned long symndx ATTRIBUTE_UNUSED)
14096 {
14097 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14098 return bed->s->arch_size / 8;
14099 }
14100
14101 /* Routines to support the creation of dynamic relocs. */
14102
14103 /* Returns the name of the dynamic reloc section associated with SEC. */
14104
14105 static const char *
14106 get_dynamic_reloc_section_name (bfd * abfd,
14107 asection * sec,
14108 bfd_boolean is_rela)
14109 {
14110 char *name;
14111 const char *old_name = bfd_get_section_name (NULL, sec);
14112 const char *prefix = is_rela ? ".rela" : ".rel";
14113
14114 if (old_name == NULL)
14115 return NULL;
14116
14117 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
14118 sprintf (name, "%s%s", prefix, old_name);
14119
14120 return name;
14121 }
14122
14123 /* Returns the dynamic reloc section associated with SEC.
14124 If necessary compute the name of the dynamic reloc section based
14125 on SEC's name (looked up in ABFD's string table) and the setting
14126 of IS_RELA. */
14127
14128 asection *
14129 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14130 asection * sec,
14131 bfd_boolean is_rela)
14132 {
14133 asection * reloc_sec = elf_section_data (sec)->sreloc;
14134
14135 if (reloc_sec == NULL)
14136 {
14137 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14138
14139 if (name != NULL)
14140 {
14141 reloc_sec = bfd_get_linker_section (abfd, name);
14142
14143 if (reloc_sec != NULL)
14144 elf_section_data (sec)->sreloc = reloc_sec;
14145 }
14146 }
14147
14148 return reloc_sec;
14149 }
14150
14151 /* Returns the dynamic reloc section associated with SEC. If the
14152 section does not exist it is created and attached to the DYNOBJ
14153 bfd and stored in the SRELOC field of SEC's elf_section_data
14154 structure.
14155
14156 ALIGNMENT is the alignment for the newly created section and
14157 IS_RELA defines whether the name should be .rela.<SEC's name>
14158 or .rel.<SEC's name>. The section name is looked up in the
14159 string table associated with ABFD. */
14160
14161 asection *
14162 _bfd_elf_make_dynamic_reloc_section (asection *sec,
14163 bfd *dynobj,
14164 unsigned int alignment,
14165 bfd *abfd,
14166 bfd_boolean is_rela)
14167 {
14168 asection * reloc_sec = elf_section_data (sec)->sreloc;
14169
14170 if (reloc_sec == NULL)
14171 {
14172 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14173
14174 if (name == NULL)
14175 return NULL;
14176
14177 reloc_sec = bfd_get_linker_section (dynobj, name);
14178
14179 if (reloc_sec == NULL)
14180 {
14181 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14182 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
14183 if ((sec->flags & SEC_ALLOC) != 0)
14184 flags |= SEC_ALLOC | SEC_LOAD;
14185
14186 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
14187 if (reloc_sec != NULL)
14188 {
14189 /* _bfd_elf_get_sec_type_attr chooses a section type by
14190 name. Override as it may be wrong, eg. for a user
14191 section named "auto" we'll get ".relauto" which is
14192 seen to be a .rela section. */
14193 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
14194 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14195 reloc_sec = NULL;
14196 }
14197 }
14198
14199 elf_section_data (sec)->sreloc = reloc_sec;
14200 }
14201
14202 return reloc_sec;
14203 }
14204
14205 /* Copy the ELF symbol type and other attributes for a linker script
14206 assignment from HSRC to HDEST. Generally this should be treated as
14207 if we found a strong non-dynamic definition for HDEST (except that
14208 ld ignores multiple definition errors). */
14209 void
14210 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14211 struct bfd_link_hash_entry *hdest,
14212 struct bfd_link_hash_entry *hsrc)
14213 {
14214 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14215 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14216 Elf_Internal_Sym isym;
14217
14218 ehdest->type = ehsrc->type;
14219 ehdest->target_internal = ehsrc->target_internal;
14220
14221 isym.st_other = ehsrc->other;
14222 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
14223 }
14224
14225 /* Append a RELA relocation REL to section S in BFD. */
14226
14227 void
14228 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14229 {
14230 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14231 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14232 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14233 bed->s->swap_reloca_out (abfd, rel, loc);
14234 }
14235
14236 /* Append a REL relocation REL to section S in BFD. */
14237
14238 void
14239 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14240 {
14241 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14242 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14243 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
14244 bed->s->swap_reloc_out (abfd, rel, loc);
14245 }
14246
14247 /* Define __start, __stop, .startof. or .sizeof. symbol. */
14248
14249 struct bfd_link_hash_entry *
14250 bfd_elf_define_start_stop (struct bfd_link_info *info,
14251 const char *symbol, asection *sec)
14252 {
14253 struct elf_link_hash_entry *h;
14254
14255 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14256 FALSE, FALSE, TRUE);
14257 if (h != NULL
14258 && (h->root.type == bfd_link_hash_undefined
14259 || h->root.type == bfd_link_hash_undefweak
14260 || (h->ref_regular && !h->def_regular)))
14261 {
14262 h->root.type = bfd_link_hash_defined;
14263 h->root.u.def.section = sec;
14264 h->root.u.def.value = 0;
14265 h->def_regular = 1;
14266 h->def_dynamic = 0;
14267 h->start_stop = 1;
14268 h->u2.start_stop_section = sec;
14269 if (symbol[0] == '.')
14270 {
14271 /* .startof. and .sizeof. symbols are local. */
14272 const struct elf_backend_data *bed;
14273 bed = get_elf_backend_data (info->output_bfd);
14274 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
14275 }
14276 else if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14277 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
14278 return &h->root;
14279 }
14280 return NULL;
14281 }
This page took 0.328405 seconds and 4 git commands to generate.