1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2024 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
20
21 #include "sysdep.h"
22 #include "bfd.h"
23 #include "bfdlink.h"
24 #include "libbfd.h"
25 #define ARCH_SIZE 0
26 #include "elf-bfd.h"
27 #include "safe-ctype.h"
28 #include "libiberty.h"
29 #include "objalloc.h"
30 #if BFD_SUPPORTS_PLUGINS
31 #include "plugin-api.h"
32 #include "plugin.h"
33 #endif
34
35 #include <limits.h>
36 #ifndef CHAR_BIT
37 #define CHAR_BIT 8
38 #endif
39
40 /* This struct is used to pass information to routines called via
41 elf_link_hash_traverse which must return failure. */
42
43 struct elf_info_failed
44 {
45 struct bfd_link_info *info;
46 bool failed;
47 };
48
49 static bool _bfd_elf_fix_symbol_flags
50 (struct elf_link_hash_entry *, struct elf_info_failed *);
51
52 asection *
_bfd_elf_section_for_symbol(struct elf_reloc_cookie * cookie,unsigned long r_symndx,bool discard)53 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
54 unsigned long r_symndx,
55 bool discard)
56 {
57 if (r_symndx >= cookie->locsymcount
58 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
59 {
60 struct elf_link_hash_entry *h;
61
62 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
63
64 while (h->root.type == bfd_link_hash_indirect
65 || h->root.type == bfd_link_hash_warning)
66 h = (struct elf_link_hash_entry *) h->root.u.i.link;
67
68 if ((h->root.type == bfd_link_hash_defined
69 || h->root.type == bfd_link_hash_defweak)
70 && discarded_section (h->root.u.def.section))
71 return h->root.u.def.section;
72 else
73 return NULL;
74 }
75 else
76 {
77 /* It's not a relocation against a global symbol,
78 but it could be a relocation against a local
79 symbol for a discarded section. */
80 asection *isec;
81 Elf_Internal_Sym *isym;
82
83 /* Need to: get the symbol; get the section. */
84 isym = &cookie->locsyms[r_symndx];
85 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
86 if (isec != NULL
87 && discard ? discarded_section (isec) : 1)
88 return isec;
89 }
90 return NULL;
91 }
92
93 /* Define a symbol in a dynamic linkage section. */
94
95 struct elf_link_hash_entry *
_bfd_elf_define_linkage_sym(bfd * abfd,struct bfd_link_info * info,asection * sec,const char * name)96 _bfd_elf_define_linkage_sym (bfd *abfd,
97 struct bfd_link_info *info,
98 asection *sec,
99 const char *name)
100 {
101 struct elf_link_hash_entry *h;
102 struct bfd_link_hash_entry *bh;
103 const struct elf_backend_data *bed;
104
105 h = elf_link_hash_lookup (elf_hash_table (info), name, false, false, false);
106 if (h != NULL)
107 {
108 /* Zap symbol defined in an as-needed lib that wasn't linked.
109 This is a symptom of a larger problem: Absolute symbols
110 defined in shared libraries can't be overridden, because we
111 lose the link to the bfd which is via the symbol section. */
112 h->root.type = bfd_link_hash_new;
113 bh = &h->root;
114 }
115 else
116 bh = NULL;
117
118 bed = get_elf_backend_data (abfd);
119 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
120 sec, 0, NULL, false, bed->collect,
121 &bh))
122 return NULL;
123 h = (struct elf_link_hash_entry *) bh;
124 BFD_ASSERT (h != NULL);
125 h->def_regular = 1;
126 h->non_elf = 0;
127 h->root.linker_def = 1;
128 h->type = STT_OBJECT;
129 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
130 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
131
132 (*bed->elf_backend_hide_symbol) (info, h, true);
133 return h;
134 }
135
136 bool
_bfd_elf_create_got_section(bfd * abfd,struct bfd_link_info * info)137 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
138 {
139 flagword flags;
140 asection *s;
141 struct elf_link_hash_entry *h;
142 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
143 struct elf_link_hash_table *htab = elf_hash_table (info);
144
145 /* This function may be called more than once. */
146 if (htab->sgot != NULL)
147 return true;
148
149 flags = bed->dynamic_sec_flags;
150
151 s = bfd_make_section_anyway_with_flags (abfd,
152 (bed->rela_plts_and_copies_p
153 ? ".rela.got" : ".rel.got"),
154 (bed->dynamic_sec_flags
155 | SEC_READONLY));
156 if (s == NULL
157 || !bfd_set_section_alignment (s, bed->s->log_file_align))
158 return false;
159 htab->srelgot = s;
160
161 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
162 if (s == NULL
163 || !bfd_set_section_alignment (s, bed->s->log_file_align))
164 return false;
165 htab->sgot = s;
166
167 if (bed->want_got_plt)
168 {
169 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
170 if (s == NULL
171 || !bfd_set_section_alignment (s, bed->s->log_file_align))
172 return false;
173 htab->sgotplt = s;
174 }
175
176 /* The first bit of the global offset table is the header. */
177 s->size += bed->got_header_size;
178
179 if (bed->want_got_sym)
180 {
181 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
182 (or .got.plt) section. We don't do this in the linker script
183 because we don't want to define the symbol if we are not creating
184 a global offset table. */
185 h = _bfd_elf_define_linkage_sym (abfd, info, s,
186 "_GLOBAL_OFFSET_TABLE_");
187 elf_hash_table (info)->hgot = h;
188 if (h == NULL)
189 return false;
190 }
191
192 return true;
193 }
194
195 /* Create a strtab to hold the dynamic symbol names. */
196 static bool
_bfd_elf_link_create_dynstrtab(bfd * abfd,struct bfd_link_info * info)197 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
198 {
199 struct elf_link_hash_table *hash_table;
200
201 hash_table = elf_hash_table (info);
202 if (hash_table->dynobj == NULL)
203 {
204 /* We may not set dynobj, an input file holding linker created
205 dynamic sections to abfd, which may be a dynamic object with
206 its own dynamic sections. We need to find a normal input file
207 to hold linker created sections if possible. */
208 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
209 {
210 bfd *ibfd;
211 asection *s;
212 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
213 if ((ibfd->flags
214 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
215 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
216 && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
217 && !((s = ibfd->sections) != NULL
218 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
219 {
220 abfd = ibfd;
221 break;
222 }
223 }
224 hash_table->dynobj = abfd;
225 }
226
227 if (hash_table->dynstr == NULL)
228 {
229 hash_table->dynstr = _bfd_elf_strtab_init ();
230 if (hash_table->dynstr == NULL)
231 return false;
232 }
233 return true;
234 }
235
236 /* Create some sections which will be filled in with dynamic linking
237 information. ABFD is an input file which requires dynamic sections
238 to be created. The dynamic sections take up virtual memory space
239 when the final executable is run, so we need to create them before
240 addresses are assigned to the output sections. We work out the
241 actual contents and size of these sections later. */
242
243 bool
_bfd_elf_link_create_dynamic_sections(bfd * abfd,struct bfd_link_info * info)244 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
245 {
246 flagword flags;
247 asection *s;
248 const struct elf_backend_data *bed;
249 struct elf_link_hash_entry *h;
250
251 if (! is_elf_hash_table (info->hash))
252 return false;
253
254 if (elf_hash_table (info)->dynamic_sections_created)
255 return true;
256
257 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
258 return false;
259
260 abfd = elf_hash_table (info)->dynobj;
261 bed = get_elf_backend_data (abfd);
262
263 flags = bed->dynamic_sec_flags;
264
265 /* A dynamically linked executable has a .interp section, but a
266 shared library does not. */
267 if (bfd_link_executable (info) && !info->nointerp)
268 {
269 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
270 flags | SEC_READONLY);
271 if (s == NULL)
272 return false;
273 }
274
275 /* Create sections to hold version informations. These are removed
276 if they are not needed. */
277 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
278 flags | SEC_READONLY);
279 if (s == NULL
280 || !bfd_set_section_alignment (s, bed->s->log_file_align))
281 return false;
282
283 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
284 flags | SEC_READONLY);
285 if (s == NULL
286 || !bfd_set_section_alignment (s, 1))
287 return false;
288
289 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
290 flags | SEC_READONLY);
291 if (s == NULL
292 || !bfd_set_section_alignment (s, bed->s->log_file_align))
293 return false;
294
295 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
296 flags | SEC_READONLY);
297 if (s == NULL
298 || !bfd_set_section_alignment (s, bed->s->log_file_align))
299 return false;
300 elf_hash_table (info)->dynsym = s;
301
302 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
303 flags | SEC_READONLY);
304 if (s == NULL)
305 return false;
306
307 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
308 if (s == NULL
309 || !bfd_set_section_alignment (s, bed->s->log_file_align))
310 return false;
311
312 /* The special symbol _DYNAMIC is always set to the start of the
313 .dynamic section. We could set _DYNAMIC in a linker script, but we
314 only want to define it if we are, in fact, creating a .dynamic
315 section. We don't want to define it if there is no .dynamic
316 section, since on some ELF platforms the start up code examines it
317 to decide how to initialize the process. */
318 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
319 elf_hash_table (info)->hdynamic = h;
320 if (h == NULL)
321 return false;
322
323 if (info->emit_hash)
324 {
325 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
326 flags | SEC_READONLY);
327 if (s == NULL
328 || !bfd_set_section_alignment (s, bed->s->log_file_align))
329 return false;
330 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
331 }
332
333 if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL)
334 {
335 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
336 flags | SEC_READONLY);
337 if (s == NULL
338 || !bfd_set_section_alignment (s, bed->s->log_file_align))
339 return false;
340 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
341 4 32-bit words followed by variable count of 64-bit words, then
342 variable count of 32-bit words. */
343 if (bed->s->arch_size == 64)
344 elf_section_data (s)->this_hdr.sh_entsize = 0;
345 else
346 elf_section_data (s)->this_hdr.sh_entsize = 4;
347 }
348
349 if (info->enable_dt_relr)
350 {
351 s = bfd_make_section_anyway_with_flags (abfd, ".relr.dyn",
352 (bed->dynamic_sec_flags
353 | SEC_READONLY));
354 if (s == NULL
355 || !bfd_set_section_alignment (s, bed->s->log_file_align))
356 return false;
357 elf_hash_table (info)->srelrdyn = s;
358 }
359
360 /* Let the backend create the rest of the sections. This lets the
361 backend set the right flags. The backend will normally create
362 the .got and .plt sections. */
363 if (bed->elf_backend_create_dynamic_sections == NULL
364 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
365 return false;
366
367 elf_hash_table (info)->dynamic_sections_created = true;
368
369 return true;
370 }
371
372 /* Create dynamic sections when linking against a dynamic object. */
373
374 bool
_bfd_elf_create_dynamic_sections(bfd * abfd,struct bfd_link_info * info)375 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
376 {
377 flagword flags, pltflags;
378 struct elf_link_hash_entry *h;
379 asection *s;
380 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
381 struct elf_link_hash_table *htab = elf_hash_table (info);
382
383 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
384 .rel[a].bss sections. */
385 flags = bed->dynamic_sec_flags;
386
387 pltflags = flags;
388 if (bed->plt_not_loaded)
389 /* We do not clear SEC_ALLOC here because we still want the OS to
390 allocate space for the section; it's just that there's nothing
391 to read in from the object file. */
392 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
393 else
394 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
395 if (bed->plt_readonly)
396 pltflags |= SEC_READONLY;
397
398 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
399 if (s == NULL
400 || !bfd_set_section_alignment (s, bed->plt_alignment))
401 return false;
402 htab->splt = s;
403
404 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
405 .plt section. */
406 if (bed->want_plt_sym)
407 {
408 h = _bfd_elf_define_linkage_sym (abfd, info, s,
409 "_PROCEDURE_LINKAGE_TABLE_");
410 elf_hash_table (info)->hplt = h;
411 if (h == NULL)
412 return false;
413 }
414
415 s = bfd_make_section_anyway_with_flags (abfd,
416 (bed->rela_plts_and_copies_p
417 ? ".rela.plt" : ".rel.plt"),
418 flags | SEC_READONLY);
419 if (s == NULL
420 || !bfd_set_section_alignment (s, bed->s->log_file_align))
421 return false;
422 htab->srelplt = s;
423
424 if (! _bfd_elf_create_got_section (abfd, info))
425 return false;
426
427 if (bed->want_dynbss)
428 {
429 /* The .dynbss section is a place to put symbols which are defined
430 by dynamic objects, are referenced by regular objects, and are
431 not functions. We must allocate space for them in the process
432 image and use a R_*_COPY reloc to tell the dynamic linker to
433 initialize them at run time. The linker script puts the .dynbss
434 section into the .bss section of the final image. */
435 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
436 SEC_ALLOC | SEC_LINKER_CREATED);
437 if (s == NULL)
438 return false;
439 htab->sdynbss = s;
440
441 if (bed->want_dynrelro)
442 {
443 /* Similarly, but for symbols that were originally in read-only
444 sections. This section doesn't really need to have contents,
445 but make it like other .data.rel.ro sections. */
446 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
447 flags);
448 if (s == NULL)
449 return false;
450 htab->sdynrelro = s;
451 }
452
453 /* The .rel[a].bss section holds copy relocs. This section is not
454 normally needed. We need to create it here, though, so that the
455 linker will map it to an output section. We can't just create it
456 only if we need it, because we will not know whether we need it
457 until we have seen all the input files, and the first time the
458 main linker code calls BFD after examining all the input files
459 (size_dynamic_sections) the input sections have already been
460 mapped to the output sections. If the section turns out not to
461 be needed, we can discard it later. We will never need this
462 section when generating a shared object, since they do not use
463 copy relocs. */
464 if (bfd_link_executable (info))
465 {
466 s = bfd_make_section_anyway_with_flags (abfd,
467 (bed->rela_plts_and_copies_p
468 ? ".rela.bss" : ".rel.bss"),
469 flags | SEC_READONLY);
470 if (s == NULL
471 || !bfd_set_section_alignment (s, bed->s->log_file_align))
472 return false;
473 htab->srelbss = s;
474
475 if (bed->want_dynrelro)
476 {
477 s = (bfd_make_section_anyway_with_flags
478 (abfd, (bed->rela_plts_and_copies_p
479 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
480 flags | SEC_READONLY));
481 if (s == NULL
482 || !bfd_set_section_alignment (s, bed->s->log_file_align))
483 return false;
484 htab->sreldynrelro = s;
485 }
486 }
487 }
488
489 return true;
490 }
491
492 /* Record a new dynamic symbol. We record the dynamic symbols as we
493 read the input files, since we need to have a list of all of them
494 before we can determine the final sizes of the output sections.
495 Note that we may actually call this function even though we are not
496 going to output any dynamic symbols; in some cases we know that a
497 symbol should be in the dynamic symbol table, but only if there is
498 one. */
499
500 bool
bfd_elf_link_record_dynamic_symbol(struct bfd_link_info * info,struct elf_link_hash_entry * h)501 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
502 struct elf_link_hash_entry *h)
503 {
504 if (h->dynindx == -1)
505 {
506 struct elf_strtab_hash *dynstr;
507 char *p;
508 const char *name;
509 size_t indx;
510
511 if (h->root.type == bfd_link_hash_defined
512 || h->root.type == bfd_link_hash_defweak)
513 {
514 /* An IR symbol should not be made dynamic. */
515 if (h->root.u.def.section != NULL
516 && h->root.u.def.section->owner != NULL
517 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)
518 return true;
519 }
520
521 /* XXX: The ABI draft says the linker must turn hidden and
522 internal symbols into STB_LOCAL symbols when producing the
523 DSO. However, if ld.so honors st_other in the dynamic table,
524 this would not be necessary. */
525 switch (ELF_ST_VISIBILITY (h->other))
526 {
527 case STV_INTERNAL:
528 case STV_HIDDEN:
529 if (h->root.type != bfd_link_hash_undefined
530 && h->root.type != bfd_link_hash_undefweak)
531 {
532 h->forced_local = 1;
533 if (!elf_hash_table (info)->is_relocatable_executable
534 || ((h->root.type == bfd_link_hash_defined
535 || h->root.type == bfd_link_hash_defweak)
536 && h->root.u.def.section->owner != NULL
537 && h->root.u.def.section->owner->no_export)
538 || (h->root.type == bfd_link_hash_common
539 && h->root.u.c.p->section->owner != NULL
540 && h->root.u.c.p->section->owner->no_export))
541 return true;
542 }
543
544 default:
545 break;
546 }
547
548 h->dynindx = elf_hash_table (info)->dynsymcount;
549 ++elf_hash_table (info)->dynsymcount;
550
551 dynstr = elf_hash_table (info)->dynstr;
552 if (dynstr == NULL)
553 {
554 /* Create a strtab to hold the dynamic symbol names. */
555 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
556 if (dynstr == NULL)
557 return false;
558 }
559
560 /* We don't put any version information in the dynamic string
561 table. */
562 name = h->root.root.string;
563 p = strchr (name, ELF_VER_CHR);
564 if (p != NULL)
565 /* We know that the p points into writable memory. In fact,
566 there are only a few symbols that have read-only names, being
567 those like _GLOBAL_OFFSET_TABLE_ that are created specially
568 by the backends. Most symbols will have names pointing into
569 an ELF string table read from a file, or to objalloc memory. */
570 *p = 0;
571
572 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
573
574 if (p != NULL)
575 *p = ELF_VER_CHR;
576
577 if (indx == (size_t) -1)
578 return false;
579 h->dynstr_index = indx;
580 }
581
582 return true;
583 }
584
585 /* Mark a symbol dynamic. */
586
587 static void
bfd_elf_link_mark_dynamic_symbol(struct bfd_link_info * info,struct elf_link_hash_entry * h,Elf_Internal_Sym * sym)588 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
589 struct elf_link_hash_entry *h,
590 Elf_Internal_Sym *sym)
591 {
592 struct bfd_elf_dynamic_list *d = info->dynamic_list;
593
594 /* It may be called more than once on the same H. */
595 if(h->dynamic || bfd_link_relocatable (info))
596 return;
597
598 if ((info->dynamic_data
599 && (h->type == STT_OBJECT
600 || h->type == STT_COMMON
601 || (sym != NULL
602 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
603 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
604 || (d != NULL
605 && h->non_elf
606 && (*d->match) (&d->head, NULL, h->root.root.string)))
607 {
608 h->dynamic = 1;
609 /* NB: If a symbol is made dynamic by --dynamic-list, it has
610 non-IR reference. */
611 h->root.non_ir_ref_dynamic = 1;
612 }
613 }
614
615 /* Record an assignment to a symbol made by a linker script. We need
616 this in case some dynamic object refers to this symbol. */
617
618 bool
bfd_elf_record_link_assignment(bfd * output_bfd,struct bfd_link_info * info,const char * name,bool provide,bool hidden)619 bfd_elf_record_link_assignment (bfd *output_bfd,
620 struct bfd_link_info *info,
621 const char *name,
622 bool provide,
623 bool hidden)
624 {
625 struct elf_link_hash_entry *h, *hv;
626 struct elf_link_hash_table *htab;
627 const struct elf_backend_data *bed;
628
629 if (!is_elf_hash_table (info->hash))
630 return true;
631
632 htab = elf_hash_table (info);
633 h = elf_link_hash_lookup (htab, name, !provide, true, false);
634 if (h == NULL)
635 return provide;
636
637 if (h->root.type == bfd_link_hash_warning)
638 h = (struct elf_link_hash_entry *) h->root.u.i.link;
639
640 if (h->versioned == unknown)
641 {
642 /* Set versioned if symbol version is unknown. */
643 char *version = strrchr (name, ELF_VER_CHR);
644 if (version)
645 {
646 if (version > name && version[-1] != ELF_VER_CHR)
647 h->versioned = versioned_hidden;
648 else
649 h->versioned = versioned;
650 }
651 }
652
653 /* Symbols defined in a linker script but not referenced anywhere
654 else will have non_elf set. */
655 if (h->non_elf)
656 {
657 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
658 h->non_elf = 0;
659 }
660
661 switch (h->root.type)
662 {
663 case bfd_link_hash_defined:
664 case bfd_link_hash_defweak:
665 case bfd_link_hash_common:
666 break;
667 case bfd_link_hash_undefweak:
668 case bfd_link_hash_undefined:
669 /* Since we're defining the symbol, don't let it seem to have not
670 been defined. record_dynamic_symbol and size_dynamic_sections
671 may depend on this. */
672 h->root.type = bfd_link_hash_new;
673 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
674 bfd_link_repair_undef_list (&htab->root);
675 break;
676 case bfd_link_hash_new:
677 break;
678 case bfd_link_hash_indirect:
679 /* We had a versioned symbol in a dynamic library. We make the
680 the versioned symbol point to this one. */
681 bed = get_elf_backend_data (output_bfd);
682 hv = h;
683 while (hv->root.type == bfd_link_hash_indirect
684 || hv->root.type == bfd_link_hash_warning)
685 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
686 /* We don't need to update h->root.u since linker will set them
687 later. */
688 h->root.type = bfd_link_hash_undefined;
689 hv->root.type = bfd_link_hash_indirect;
690 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
691 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
692 break;
693 default:
694 BFD_FAIL ();
695 return false;
696 }
697
698 /* If this symbol is being provided by the linker script, and it is
699 currently defined by a dynamic object, but not by a regular
700 object, then mark it as undefined so that the generic linker will
701 force the correct value. */
702 if (provide
703 && h->def_dynamic
704 && !h->def_regular)
705 h->root.type = bfd_link_hash_undefined;
706
707 /* If this symbol is currently defined by a dynamic object, but not
708 by a regular object, then clear out any version information because
709 the symbol will not be associated with the dynamic object any
710 more. */
711 if (h->def_dynamic && !h->def_regular)
712 h->verinfo.verdef = NULL;
713
714 /* Make sure this symbol is not garbage collected. */
715 h->mark = 1;
716
717 h->def_regular = 1;
718
719 if (hidden)
720 {
721 bed = get_elf_backend_data (output_bfd);
722 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
723 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
724 (*bed->elf_backend_hide_symbol) (info, h, true);
725 }
726
727 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
728 and executables. */
729 if (!bfd_link_relocatable (info)
730 && h->dynindx != -1
731 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
732 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
733 h->forced_local = 1;
734
735 if ((h->def_dynamic
736 || h->ref_dynamic
737 || bfd_link_dll (info)
738 || elf_hash_table (info)->is_relocatable_executable)
739 && !h->forced_local
740 && h->dynindx == -1)
741 {
742 if (! bfd_elf_link_record_dynamic_symbol (info, h))
743 return false;
744
745 /* If this is a weak defined symbol, and we know a corresponding
746 real symbol from the same dynamic object, make sure the real
747 symbol is also made into a dynamic symbol. */
748 if (h->is_weakalias)
749 {
750 struct elf_link_hash_entry *def = weakdef (h);
751
752 if (def->dynindx == -1
753 && !bfd_elf_link_record_dynamic_symbol (info, def))
754 return false;
755 }
756 }
757
758 return true;
759 }
760
761 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
762 success, and 2 on a failure caused by attempting to record a symbol
763 in a discarded section, eg. a discarded link-once section symbol. */
764
765 int
bfd_elf_link_record_local_dynamic_symbol(struct bfd_link_info * info,bfd * input_bfd,long input_indx)766 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
767 bfd *input_bfd,
768 long input_indx)
769 {
770 size_t amt;
771 struct elf_link_local_dynamic_entry *entry;
772 struct elf_link_hash_table *eht;
773 struct elf_strtab_hash *dynstr;
774 size_t dynstr_index;
775 char *name;
776 Elf_External_Sym_Shndx eshndx;
777 char esym[sizeof (Elf64_External_Sym)];
778
779 if (! is_elf_hash_table (info->hash))
780 return 0;
781
782 /* See if the entry exists already. */
783 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
784 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
785 return 1;
786
787 amt = sizeof (*entry);
788 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
789 if (entry == NULL)
790 return 0;
791
792 /* Go find the symbol, so that we can find it's name. */
793 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
794 1, input_indx, &entry->isym, esym, &eshndx))
795 {
796 bfd_release (input_bfd, entry);
797 return 0;
798 }
799
800 if (entry->isym.st_shndx != SHN_UNDEF
801 && entry->isym.st_shndx < SHN_LORESERVE)
802 {
803 asection *s;
804
805 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
806 if (s == NULL || bfd_is_abs_section (s->output_section))
807 {
808 /* We can still bfd_release here as nothing has done another
809 bfd_alloc. We can't do this later in this function. */
810 bfd_release (input_bfd, entry);
811 return 2;
812 }
813 }
814
815 name = (bfd_elf_string_from_elf_section
816 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
817 entry->isym.st_name));
818
819 dynstr = elf_hash_table (info)->dynstr;
820 if (dynstr == NULL)
821 {
822 /* Create a strtab to hold the dynamic symbol names. */
823 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
824 if (dynstr == NULL)
825 return 0;
826 }
827
828 dynstr_index = _bfd_elf_strtab_add (dynstr, name, false);
829 if (dynstr_index == (size_t) -1)
830 return 0;
831 entry->isym.st_name = dynstr_index;
832
833 eht = elf_hash_table (info);
834
835 entry->next = eht->dynlocal;
836 eht->dynlocal = entry;
837 entry->input_bfd = input_bfd;
838 entry->input_indx = input_indx;
839 eht->dynsymcount++;
840
841 /* Whatever binding the symbol had before, it's now local. */
842 entry->isym.st_info
843 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
844
845 /* The dynindx will be set at the end of size_dynamic_sections. */
846
847 return 1;
848 }
849
850 /* Return the dynindex of a local dynamic symbol. */
851
852 long
_bfd_elf_link_lookup_local_dynindx(struct bfd_link_info * info,bfd * input_bfd,long input_indx)853 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
854 bfd *input_bfd,
855 long input_indx)
856 {
857 struct elf_link_local_dynamic_entry *e;
858
859 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
860 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
861 return e->dynindx;
862 return -1;
863 }
864
865 /* This function is used to renumber the dynamic symbols, if some of
866 them are removed because they are marked as local. This is called
867 via elf_link_hash_traverse. */
868
869 static bool
elf_link_renumber_hash_table_dynsyms(struct elf_link_hash_entry * h,void * data)870 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
871 void *data)
872 {
873 size_t *count = (size_t *) data;
874
875 if (h->forced_local)
876 return true;
877
878 if (h->dynindx != -1)
879 h->dynindx = ++(*count);
880
881 return true;
882 }
883
884
885 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
886 STB_LOCAL binding. */
887
888 static bool
elf_link_renumber_local_hash_table_dynsyms(struct elf_link_hash_entry * h,void * data)889 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
890 void *data)
891 {
892 size_t *count = (size_t *) data;
893
894 if (!h->forced_local)
895 return true;
896
897 if (h->dynindx != -1)
898 h->dynindx = ++(*count);
899
900 return true;
901 }
902
903 /* Return true if the dynamic symbol for a given section should be
904 omitted when creating a shared library. */
905 bool
_bfd_elf_omit_section_dynsym_default(bfd * output_bfd ATTRIBUTE_UNUSED,struct bfd_link_info * info,asection * p)906 _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
907 struct bfd_link_info *info,
908 asection *p)
909 {
910 struct elf_link_hash_table *htab;
911 asection *ip;
912
913 switch (elf_section_data (p)->this_hdr.sh_type)
914 {
915 case SHT_PROGBITS:
916 case SHT_NOBITS:
917 /* If sh_type is yet undecided, assume it could be
918 SHT_PROGBITS/SHT_NOBITS. */
919 case SHT_NULL:
920 htab = elf_hash_table (info);
921 if (htab->text_index_section != NULL)
922 return p != htab->text_index_section && p != htab->data_index_section;
923
924 return (htab->dynobj != NULL
925 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
926 && ip->output_section == p);
927
928 /* There shouldn't be section relative relocations
929 against any other section. */
930 default:
931 return true;
932 }
933 }
934
935 bool
_bfd_elf_omit_section_dynsym_all(bfd * output_bfd ATTRIBUTE_UNUSED,struct bfd_link_info * info ATTRIBUTE_UNUSED,asection * p ATTRIBUTE_UNUSED)936 _bfd_elf_omit_section_dynsym_all
937 (bfd *output_bfd ATTRIBUTE_UNUSED,
938 struct bfd_link_info *info ATTRIBUTE_UNUSED,
939 asection *p ATTRIBUTE_UNUSED)
940 {
941 return true;
942 }
943
944 /* Assign dynsym indices. In a shared library we generate a section
945 symbol for each output section, which come first. Next come symbols
946 which have been forced to local binding. Then all of the back-end
947 allocated local dynamic syms, followed by the rest of the global
948 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
949 (This prevents the early call before elf_backend_init_index_section
950 and strip_excluded_output_sections setting dynindx for sections
951 that are stripped.) */
952
953 static unsigned long
_bfd_elf_link_renumber_dynsyms(bfd * output_bfd,struct bfd_link_info * info,unsigned long * section_sym_count)954 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
955 struct bfd_link_info *info,
956 unsigned long *section_sym_count)
957 {
958 unsigned long dynsymcount = 0;
959 bool do_sec = section_sym_count != NULL;
960
961 if (bfd_link_pic (info)
962 || elf_hash_table (info)->is_relocatable_executable)
963 {
964 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
965 asection *p;
966 for (p = output_bfd->sections; p ; p = p->next)
967 if ((p->flags & SEC_EXCLUDE) == 0
968 && (p->flags & SEC_ALLOC) != 0
969 && elf_hash_table (info)->dynamic_relocs
970 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
971 {
972 ++dynsymcount;
973 if (do_sec)
974 elf_section_data (p)->dynindx = dynsymcount;
975 }
976 else if (do_sec)
977 elf_section_data (p)->dynindx = 0;
978 }
979 if (do_sec)
980 *section_sym_count = dynsymcount;
981
982 elf_link_hash_traverse (elf_hash_table (info),
983 elf_link_renumber_local_hash_table_dynsyms,
984 &dynsymcount);
985
986 if (elf_hash_table (info)->dynlocal)
987 {
988 struct elf_link_local_dynamic_entry *p;
989 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
990 p->dynindx = ++dynsymcount;
991 }
992 elf_hash_table (info)->local_dynsymcount = dynsymcount;
993
994 elf_link_hash_traverse (elf_hash_table (info),
995 elf_link_renumber_hash_table_dynsyms,
996 &dynsymcount);
997
998 /* There is an unused NULL entry at the head of the table which we
999 must account for in our count even if the table is empty since it
1000 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
1001 .dynamic section. */
1002 dynsymcount++;
1003
1004 elf_hash_table (info)->dynsymcount = dynsymcount;
1005 return dynsymcount;
1006 }
1007
1008 /* Merge st_other field. */
1009
1010 static void
elf_merge_st_other(bfd * abfd,struct elf_link_hash_entry * h,unsigned int st_other,asection * sec,bool definition,bool dynamic)1011 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
1012 unsigned int st_other, asection *sec,
1013 bool definition, bool dynamic)
1014 {
1015 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1016
1017 /* If st_other has a processor-specific meaning, specific
1018 code might be needed here. */
1019 if (bed->elf_backend_merge_symbol_attribute)
1020 (*bed->elf_backend_merge_symbol_attribute) (h, st_other, definition,
1021 dynamic);
1022
1023 if (!dynamic)
1024 {
1025 unsigned symvis = ELF_ST_VISIBILITY (st_other);
1026 unsigned hvis = ELF_ST_VISIBILITY (h->other);
1027
1028 /* Keep the most constraining visibility. Leave the remainder
1029 of the st_other field to elf_backend_merge_symbol_attribute. */
1030 if (symvis - 1 < hvis - 1)
1031 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1032 }
1033 else if (definition
1034 && ELF_ST_VISIBILITY (st_other) != STV_DEFAULT
1035 && (sec->flags & SEC_READONLY) == 0)
1036 h->protected_def = 1;
1037 }
1038
1039 /* This function is called when we want to merge a new symbol with an
1040 existing symbol. It handles the various cases which arise when we
1041 find a definition in a dynamic object, or when there is already a
1042 definition in a dynamic object. The new symbol is described by
1043 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1044 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1045 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1046 of an old common symbol. We set OVERRIDE if the old symbol is
1047 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1048 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1049 to change. By OK to change, we mean that we shouldn't warn if the
1050 type or size does change. */
1051
1052 static bool
_bfd_elf_merge_symbol(bfd * abfd,struct bfd_link_info * info,const char * name,Elf_Internal_Sym * sym,asection ** psec,bfd_vma * pvalue,struct elf_link_hash_entry ** sym_hash,bfd ** poldbfd,bool * pold_weak,unsigned int * pold_alignment,bool * skip,bfd ** override,bool * type_change_ok,bool * size_change_ok,bool * matched)1053 _bfd_elf_merge_symbol (bfd *abfd,
1054 struct bfd_link_info *info,
1055 const char *name,
1056 Elf_Internal_Sym *sym,
1057 asection **psec,
1058 bfd_vma *pvalue,
1059 struct elf_link_hash_entry **sym_hash,
1060 bfd **poldbfd,
1061 bool *pold_weak,
1062 unsigned int *pold_alignment,
1063 bool *skip,
1064 bfd **override,
1065 bool *type_change_ok,
1066 bool *size_change_ok,
1067 bool *matched)
1068 {
1069 asection *sec, *oldsec;
1070 struct elf_link_hash_entry *h;
1071 struct elf_link_hash_entry *hi;
1072 struct elf_link_hash_entry *flip;
1073 int bind;
1074 bfd *oldbfd;
1075 bool newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1076 bool newweak, oldweak, newfunc, oldfunc;
1077 const struct elf_backend_data *bed;
1078 char *new_version;
1079 bool default_sym = *matched;
1080 struct elf_link_hash_table *htab;
1081
1082 *skip = false;
1083 *override = NULL;
1084
1085 sec = *psec;
1086 bind = ELF_ST_BIND (sym->st_info);
1087
1088 if (! bfd_is_und_section (sec))
1089 h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
1090 else
1091 h = ((struct elf_link_hash_entry *)
1092 bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
1093 if (h == NULL)
1094 return false;
1095 *sym_hash = h;
1096
1097 bed = get_elf_backend_data (abfd);
1098
1099 /* NEW_VERSION is the symbol version of the new symbol. */
1100 if (h->versioned != unversioned)
1101 {
1102 /* Symbol version is unknown or versioned. */
1103 new_version = strrchr (name, ELF_VER_CHR);
1104 if (new_version)
1105 {
1106 if (h->versioned == unknown)
1107 {
1108 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1109 h->versioned = versioned_hidden;
1110 else
1111 h->versioned = versioned;
1112 }
1113 new_version += 1;
1114 if (new_version[0] == '\0')
1115 new_version = NULL;
1116 }
1117 else
1118 h->versioned = unversioned;
1119 }
1120 else
1121 new_version = NULL;
1122
1123 /* For merging, we only care about real symbols. But we need to make
1124 sure that indirect symbol dynamic flags are updated. */
1125 hi = h;
1126 while (h->root.type == bfd_link_hash_indirect
1127 || h->root.type == bfd_link_hash_warning)
1128 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1129
1130 if (!*matched)
1131 {
1132 if (hi == h || h->root.type == bfd_link_hash_new)
1133 *matched = true;
1134 else
1135 {
1136 /* OLD_HIDDEN is true if the existing symbol is only visible
1137 to the symbol with the same symbol version. NEW_HIDDEN is
1138 true if the new symbol is only visible to the symbol with
1139 the same symbol version. */
1140 bool old_hidden = h->versioned == versioned_hidden;
1141 bool new_hidden = hi->versioned == versioned_hidden;
1142 if (!old_hidden && !new_hidden)
1143 /* The new symbol matches the existing symbol if both
1144 aren't hidden. */
1145 *matched = true;
1146 else
1147 {
1148 /* OLD_VERSION is the symbol version of the existing
1149 symbol. */
1150 char *old_version;
1151
1152 if (h->versioned >= versioned)
1153 old_version = strrchr (h->root.root.string,
1154 ELF_VER_CHR) + 1;
1155 else
1156 old_version = NULL;
1157
1158 /* The new symbol matches the existing symbol if they
1159 have the same symbol version. */
1160 *matched = (old_version == new_version
1161 || (old_version != NULL
1162 && new_version != NULL
1163 && strcmp (old_version, new_version) == 0));
1164 }
1165 }
1166 }
1167
1168 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1169 existing symbol. */
1170
1171 oldbfd = NULL;
1172 oldsec = NULL;
1173 switch (h->root.type)
1174 {
1175 default:
1176 break;
1177
1178 case bfd_link_hash_undefined:
1179 case bfd_link_hash_undefweak:
1180 oldbfd = h->root.u.undef.abfd;
1181 break;
1182
1183 case bfd_link_hash_defined:
1184 case bfd_link_hash_defweak:
1185 oldbfd = h->root.u.def.section->owner;
1186 oldsec = h->root.u.def.section;
1187 break;
1188
1189 case bfd_link_hash_common:
1190 oldbfd = h->root.u.c.p->section->owner;
1191 oldsec = h->root.u.c.p->section;
1192 if (pold_alignment)
1193 *pold_alignment = h->root.u.c.p->alignment_power;
1194 break;
1195 }
1196 if (poldbfd && *poldbfd == NULL)
1197 *poldbfd = oldbfd;
1198
1199 /* Differentiate strong and weak symbols. */
1200 newweak = bind == STB_WEAK;
1201 oldweak = (h->root.type == bfd_link_hash_defweak
1202 || h->root.type == bfd_link_hash_undefweak);
1203 if (pold_weak)
1204 *pold_weak = oldweak;
1205
1206 /* We have to check it for every instance since the first few may be
1207 references and not all compilers emit symbol type for undefined
1208 symbols. */
1209 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1210
1211 htab = elf_hash_table (info);
1212
1213 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1214 respectively, is from a dynamic object. */
1215
1216 newdyn = (abfd->flags & DYNAMIC) != 0;
1217
1218 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1219 syms and defined syms in dynamic libraries respectively.
1220 ref_dynamic on the other hand can be set for a symbol defined in
1221 a dynamic library, and def_dynamic may not be set; When the
1222 definition in a dynamic lib is overridden by a definition in the
1223 executable use of the symbol in the dynamic lib becomes a
1224 reference to the executable symbol. */
1225 if (newdyn)
1226 {
1227 if (bfd_is_und_section (sec))
1228 {
1229 if (bind != STB_WEAK)
1230 {
1231 h->ref_dynamic_nonweak = 1;
1232 hi->ref_dynamic_nonweak = 1;
1233 }
1234 }
1235 else
1236 {
1237 /* Update the existing symbol only if they match. */
1238 if (*matched)
1239 h->dynamic_def = 1;
1240 hi->dynamic_def = 1;
1241 }
1242 }
1243
1244 /* If we just created the symbol, mark it as being an ELF symbol.
1245 Other than that, there is nothing to do--there is no merge issue
1246 with a newly defined symbol--so we just return. */
1247
1248 if (h->root.type == bfd_link_hash_new)
1249 {
1250 h->non_elf = 0;
1251 return true;
1252 }
1253
1254 /* In cases involving weak versioned symbols, we may wind up trying
1255 to merge a symbol with itself. Catch that here, to avoid the
1256 confusion that results if we try to override a symbol with
1257 itself. The additional tests catch cases like
1258 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1259 dynamic object, which we do want to handle here. */
1260 if (abfd == oldbfd
1261 && (newweak || oldweak)
1262 && ((abfd->flags & DYNAMIC) == 0
1263 || !h->def_regular))
1264 return true;
1265
1266 olddyn = false;
1267 if (oldbfd != NULL)
1268 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1269 else if (oldsec != NULL)
1270 {
1271 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1272 indices used by MIPS ELF. */
1273 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1274 }
1275
1276 /* Set non_ir_ref_dynamic only when not handling DT_NEEDED entries. */
1277 if (!htab->handling_dt_needed
1278 && oldbfd != NULL
1279 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN))
1280 {
1281 if (newdyn != olddyn)
1282 {
1283 /* Handle a case where plugin_notice won't be called and thus
1284 won't set the non_ir_ref flags on the first pass over
1285 symbols. */
1286 h->root.non_ir_ref_dynamic = true;
1287 hi->root.non_ir_ref_dynamic = true;
1288 }
1289 else if ((oldbfd->flags & BFD_PLUGIN) != 0
1290 && hi->root.type == bfd_link_hash_indirect)
1291 {
1292 /* Change indirect symbol from IR to undefined. */
1293 hi->root.type = bfd_link_hash_undefined;
1294 hi->root.u.undef.abfd = oldbfd;
1295 }
1296 }
1297
1298 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1299 respectively, appear to be a definition rather than reference. */
1300
1301 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1302
1303 olddef = (h->root.type != bfd_link_hash_undefined
1304 && h->root.type != bfd_link_hash_undefweak
1305 && h->root.type != bfd_link_hash_common);
1306
1307 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1308 respectively, appear to be a function. */
1309
1310 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1311 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1312
1313 oldfunc = (h->type != STT_NOTYPE
1314 && bed->is_function_type (h->type));
1315
1316 if (!(newfunc && oldfunc)
1317 && ELF_ST_TYPE (sym->st_info) != h->type
1318 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1319 && h->type != STT_NOTYPE
1320 && (newdef || bfd_is_com_section (sec))
1321 && (olddef || h->root.type == bfd_link_hash_common))
1322 {
1323 /* If creating a default indirect symbol ("foo" or "foo@") from
1324 a dynamic versioned definition ("foo@@") skip doing so if
1325 there is an existing regular definition with a different
1326 type. We don't want, for example, a "time" variable in the
1327 executable overriding a "time" function in a shared library. */
1328 if (newdyn
1329 && !olddyn)
1330 {
1331 *skip = true;
1332 return true;
1333 }
1334
1335 /* When adding a symbol from a regular object file after we have
1336 created indirect symbols, undo the indirection and any
1337 dynamic state. */
1338 if (hi != h
1339 && !newdyn
1340 && olddyn)
1341 {
1342 h = hi;
1343 (*bed->elf_backend_hide_symbol) (info, h, true);
1344 h->forced_local = 0;
1345 h->ref_dynamic = 0;
1346 h->def_dynamic = 0;
1347 h->dynamic_def = 0;
1348 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1349 {
1350 h->root.type = bfd_link_hash_undefined;
1351 h->root.u.undef.abfd = abfd;
1352 }
1353 else
1354 {
1355 h->root.type = bfd_link_hash_new;
1356 h->root.u.undef.abfd = NULL;
1357 }
1358 return true;
1359 }
1360 }
1361
1362 /* Check TLS symbols. We don't check undefined symbols introduced
1363 by "ld -u" which have no type (and oldbfd NULL), and we don't
1364 check symbols from plugins because they also have no type. */
1365 if (oldbfd != NULL
1366 && (oldbfd->flags & BFD_PLUGIN) == 0
1367 && (abfd->flags & BFD_PLUGIN) == 0
1368 && ELF_ST_TYPE (sym->st_info) != h->type
1369 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1370 {
1371 bfd *ntbfd, *tbfd;
1372 bool ntdef, tdef;
1373 asection *ntsec, *tsec;
1374
1375 if (h->type == STT_TLS)
1376 {
1377 ntbfd = abfd;
1378 ntsec = sec;
1379 ntdef = newdef;
1380 tbfd = oldbfd;
1381 tsec = oldsec;
1382 tdef = olddef;
1383 }
1384 else
1385 {
1386 ntbfd = oldbfd;
1387 ntsec = oldsec;
1388 ntdef = olddef;
1389 tbfd = abfd;
1390 tsec = sec;
1391 tdef = newdef;
1392 }
1393
1394 if (tdef && ntdef)
1395 _bfd_error_handler
1396 /* xgettext:c-format */
1397 (_("%s: TLS definition in %pB section %pA "
1398 "mismatches non-TLS definition in %pB section %pA"),
1399 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1400 else if (!tdef && !ntdef)
1401 _bfd_error_handler
1402 /* xgettext:c-format */
1403 (_("%s: TLS reference in %pB "
1404 "mismatches non-TLS reference in %pB"),
1405 h->root.root.string, tbfd, ntbfd);
1406 else if (tdef)
1407 _bfd_error_handler
1408 /* xgettext:c-format */
1409 (_("%s: TLS definition in %pB section %pA "
1410 "mismatches non-TLS reference in %pB"),
1411 h->root.root.string, tbfd, tsec, ntbfd);
1412 else
1413 _bfd_error_handler
1414 /* xgettext:c-format */
1415 (_("%s: TLS reference in %pB "
1416 "mismatches non-TLS definition in %pB section %pA"),
1417 h->root.root.string, tbfd, ntbfd, ntsec);
1418
1419 bfd_set_error (bfd_error_bad_value);
1420 return false;
1421 }
1422
1423 /* If the old symbol has non-default visibility, we ignore the new
1424 definition from a dynamic object. */
1425 if (newdyn
1426 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1427 && !bfd_is_und_section (sec))
1428 {
1429 *skip = true;
1430 /* Make sure this symbol is dynamic. */
1431 h->ref_dynamic = 1;
1432 hi->ref_dynamic = 1;
1433 /* A protected symbol has external availability. Make sure it is
1434 recorded as dynamic.
1435
1436 FIXME: Should we check type and size for protected symbol? */
1437 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1438 return bfd_elf_link_record_dynamic_symbol (info, h);
1439 else
1440 return true;
1441 }
1442 else if (!newdyn
1443 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1444 && h->def_dynamic)
1445 {
1446 /* If the new symbol with non-default visibility comes from a
1447 relocatable file and the old definition comes from a dynamic
1448 object, we remove the old definition. */
1449 if (hi->root.type == bfd_link_hash_indirect)
1450 {
1451 /* Handle the case where the old dynamic definition is
1452 default versioned. We need to copy the symbol info from
1453 the symbol with default version to the normal one if it
1454 was referenced before. */
1455 if (h->ref_regular)
1456 {
1457 hi->root.type = h->root.type;
1458 h->root.type = bfd_link_hash_indirect;
1459 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1460
1461 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1462 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1463 {
1464 /* If the new symbol is hidden or internal, completely undo
1465 any dynamic link state. */
1466 (*bed->elf_backend_hide_symbol) (info, h, true);
1467 h->forced_local = 0;
1468 h->ref_dynamic = 0;
1469 }
1470 else
1471 h->ref_dynamic = 1;
1472
1473 h->def_dynamic = 0;
1474 /* FIXME: Should we check type and size for protected symbol? */
1475 h->size = 0;
1476 h->type = 0;
1477
1478 h = hi;
1479 }
1480 else
1481 h = hi;
1482 }
1483
1484 /* If the old symbol was undefined before, then it will still be
1485 on the undefs list. If the new symbol is undefined or
1486 common, we can't make it bfd_link_hash_new here, because new
1487 undefined or common symbols will be added to the undefs list
1488 by _bfd_generic_link_add_one_symbol. Symbols may not be
1489 added twice to the undefs list. Also, if the new symbol is
1490 undefweak then we don't want to lose the strong undef. */
1491 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1492 {
1493 h->root.type = bfd_link_hash_undefined;
1494 h->root.u.undef.abfd = abfd;
1495 }
1496 else
1497 {
1498 h->root.type = bfd_link_hash_new;
1499 h->root.u.undef.abfd = NULL;
1500 }
1501
1502 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1503 {
1504 /* If the new symbol is hidden or internal, completely undo
1505 any dynamic link state. */
1506 (*bed->elf_backend_hide_symbol) (info, h, true);
1507 h->forced_local = 0;
1508 h->ref_dynamic = 0;
1509 }
1510 else
1511 h->ref_dynamic = 1;
1512 h->def_dynamic = 0;
1513 /* FIXME: Should we check type and size for protected symbol? */
1514 h->size = 0;
1515 h->type = 0;
1516 return true;
1517 }
1518
1519 /* If a new weak symbol definition comes from a regular file and the
1520 old symbol comes from a dynamic library, we treat the new one as
1521 strong. Similarly, an old weak symbol definition from a regular
1522 file is treated as strong when the new symbol comes from a dynamic
1523 library. Further, an old weak symbol from a dynamic library is
1524 treated as strong if the new symbol is from a dynamic library.
1525 This reflects the way glibc's ld.so works.
1526
1527 Also allow a weak symbol to override a linker script symbol
1528 defined by an early pass over the script. This is done so the
1529 linker knows the symbol is defined in an object file, for the
1530 DEFINED script function.
1531
1532 Do this before setting *type_change_ok or *size_change_ok so that
1533 we warn properly when dynamic library symbols are overridden. */
1534
1535 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1536 newweak = false;
1537 if (olddef && newdyn)
1538 oldweak = false;
1539
1540 /* Allow changes between different types of function symbol. */
1541 if (newfunc && oldfunc)
1542 *type_change_ok = true;
1543
1544 /* It's OK to change the type if either the existing symbol or the
1545 new symbol is weak. A type change is also OK if the old symbol
1546 is undefined and the new symbol is defined. */
1547
1548 if (oldweak
1549 || newweak
1550 || (newdef
1551 && h->root.type == bfd_link_hash_undefined))
1552 *type_change_ok = true;
1553
1554 /* It's OK to change the size if either the existing symbol or the
1555 new symbol is weak, or if the old symbol is undefined. */
1556
1557 if (*type_change_ok
1558 || h->root.type == bfd_link_hash_undefined)
1559 *size_change_ok = true;
1560
1561 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1562 symbol, respectively, appears to be a common symbol in a dynamic
1563 object. If a symbol appears in an uninitialized section, and is
1564 not weak, and is not a function, then it may be a common symbol
1565 which was resolved when the dynamic object was created. We want
1566 to treat such symbols specially, because they raise special
1567 considerations when setting the symbol size: if the symbol
1568 appears as a common symbol in a regular object, and the size in
1569 the regular object is larger, we must make sure that we use the
1570 larger size. This problematic case can always be avoided in C,
1571 but it must be handled correctly when using Fortran shared
1572 libraries.
1573
1574 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1575 likewise for OLDDYNCOMMON and OLDDEF.
1576
1577 Note that this test is just a heuristic, and that it is quite
1578 possible to have an uninitialized symbol in a shared object which
1579 is really a definition, rather than a common symbol. This could
1580 lead to some minor confusion when the symbol really is a common
1581 symbol in some regular object. However, I think it will be
1582 harmless. */
1583
1584 if (newdyn
1585 && newdef
1586 && !newweak
1587 && (sec->flags & SEC_ALLOC) != 0
1588 && (sec->flags & SEC_LOAD) == 0
1589 && sym->st_size > 0
1590 && !newfunc)
1591 newdyncommon = true;
1592 else
1593 newdyncommon = false;
1594
1595 if (olddyn
1596 && olddef
1597 && h->root.type == bfd_link_hash_defined
1598 && h->def_dynamic
1599 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1600 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1601 && h->size > 0
1602 && !oldfunc)
1603 olddyncommon = true;
1604 else
1605 olddyncommon = false;
1606
1607 /* We now know everything about the old and new symbols. We ask the
1608 backend to check if we can merge them. */
1609 if (bed->merge_symbol != NULL)
1610 {
1611 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1612 return false;
1613 sec = *psec;
1614 }
1615
1616 /* There are multiple definitions of a normal symbol. Skip the
1617 default symbol as well as definition from an IR object. */
1618 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1619 && !default_sym && h->def_regular
1620 && !(oldbfd != NULL
1621 && (oldbfd->flags & BFD_PLUGIN) != 0
1622 && (abfd->flags & BFD_PLUGIN) == 0))
1623 {
1624 /* Handle a multiple definition. */
1625 (*info->callbacks->multiple_definition) (info, &h->root,
1626 abfd, sec, *pvalue);
1627 *skip = true;
1628 return true;
1629 }
1630
1631 /* If both the old and the new symbols look like common symbols in a
1632 dynamic object, set the size of the symbol to the larger of the
1633 two. */
1634
1635 if (olddyncommon
1636 && newdyncommon
1637 && sym->st_size != h->size)
1638 {
1639 /* Since we think we have two common symbols, issue a multiple
1640 common warning if desired. Note that we only warn if the
1641 size is different. If the size is the same, we simply let
1642 the old symbol override the new one as normally happens with
1643 symbols defined in dynamic objects. */
1644
1645 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1646 bfd_link_hash_common, sym->st_size);
1647 if (sym->st_size > h->size)
1648 h->size = sym->st_size;
1649
1650 *size_change_ok = true;
1651 }
1652
1653 /* If we are looking at a dynamic object, and we have found a
1654 definition, we need to see if the symbol was already defined by
1655 some other object. If so, we want to use the existing
1656 definition, and we do not want to report a multiple symbol
1657 definition error; we do this by clobbering *PSEC to be
1658 bfd_und_section_ptr.
1659
1660 We treat a common symbol as a definition if the symbol in the
1661 shared library is a function, since common symbols always
1662 represent variables; this can cause confusion in principle, but
1663 any such confusion would seem to indicate an erroneous program or
1664 shared library. We also permit a common symbol in a regular
1665 object to override a weak symbol in a shared object. */
1666
1667 if (newdyn
1668 && newdef
1669 && (olddef
1670 || (h->root.type == bfd_link_hash_common
1671 && (newweak || newfunc))))
1672 {
1673 *override = abfd;
1674 newdef = false;
1675 newdyncommon = false;
1676
1677 *psec = sec = bfd_und_section_ptr;
1678 *size_change_ok = true;
1679
1680 /* If we get here when the old symbol is a common symbol, then
1681 we are explicitly letting it override a weak symbol or
1682 function in a dynamic object, and we don't want to warn about
1683 a type change. If the old symbol is a defined symbol, a type
1684 change warning may still be appropriate. */
1685
1686 if (h->root.type == bfd_link_hash_common)
1687 *type_change_ok = true;
1688 }
1689
1690 /* Handle the special case of an old common symbol merging with a
1691 new symbol which looks like a common symbol in a shared object.
1692 We change *PSEC and *PVALUE to make the new symbol look like a
1693 common symbol, and let _bfd_generic_link_add_one_symbol do the
1694 right thing. */
1695
1696 if (newdyncommon
1697 && h->root.type == bfd_link_hash_common)
1698 {
1699 *override = oldbfd;
1700 newdef = false;
1701 newdyncommon = false;
1702 *pvalue = sym->st_size;
1703 *psec = sec = bed->common_section (oldsec);
1704 *size_change_ok = true;
1705 }
1706
1707 /* Skip weak definitions of symbols that are already defined. */
1708 if (newdef && olddef && newweak)
1709 {
1710 /* Don't skip new non-IR weak syms. */
1711 if (!(oldbfd != NULL
1712 && (oldbfd->flags & BFD_PLUGIN) != 0
1713 && (abfd->flags & BFD_PLUGIN) == 0))
1714 {
1715 newdef = false;
1716 *skip = true;
1717 }
1718
1719 /* Merge st_other. If the symbol already has a dynamic index,
1720 but visibility says it should not be visible, turn it into a
1721 local symbol. */
1722 elf_merge_st_other (abfd, h, sym->st_other, sec, newdef, newdyn);
1723 if (h->dynindx != -1)
1724 switch (ELF_ST_VISIBILITY (h->other))
1725 {
1726 case STV_INTERNAL:
1727 case STV_HIDDEN:
1728 (*bed->elf_backend_hide_symbol) (info, h, true);
1729 break;
1730 }
1731 }
1732
1733 /* If the old symbol is from a dynamic object, and the new symbol is
1734 a definition which is not from a dynamic object, then the new
1735 symbol overrides the old symbol. Symbols from regular files
1736 always take precedence over symbols from dynamic objects, even if
1737 they are defined after the dynamic object in the link.
1738
1739 As above, we again permit a common symbol in a regular object to
1740 override a definition in a shared object if the shared object
1741 symbol is a function or is weak. */
1742
1743 flip = NULL;
1744 if (!newdyn
1745 && (newdef
1746 || (bfd_is_com_section (sec)
1747 && (oldweak || oldfunc)))
1748 && olddyn
1749 && olddef
1750 && h->def_dynamic)
1751 {
1752 /* Change the hash table entry to undefined, and let
1753 _bfd_generic_link_add_one_symbol do the right thing with the
1754 new definition. */
1755
1756 h->root.type = bfd_link_hash_undefined;
1757 h->root.u.undef.abfd = h->root.u.def.section->owner;
1758 *size_change_ok = true;
1759
1760 olddef = false;
1761 olddyncommon = false;
1762
1763 /* We again permit a type change when a common symbol may be
1764 overriding a function. */
1765
1766 if (bfd_is_com_section (sec))
1767 {
1768 if (oldfunc)
1769 {
1770 /* If a common symbol overrides a function, make sure
1771 that it isn't defined dynamically nor has type
1772 function. */
1773 h->def_dynamic = 0;
1774 h->type = STT_NOTYPE;
1775 }
1776 *type_change_ok = true;
1777 }
1778
1779 if (hi->root.type == bfd_link_hash_indirect)
1780 flip = hi;
1781 else
1782 /* This union may have been set to be non-NULL when this symbol
1783 was seen in a dynamic object. We must force the union to be
1784 NULL, so that it is correct for a regular symbol. */
1785 h->verinfo.vertree = NULL;
1786 }
1787
1788 /* Handle the special case of a new common symbol merging with an
1789 old symbol that looks like it might be a common symbol defined in
1790 a shared object. Note that we have already handled the case in
1791 which a new common symbol should simply override the definition
1792 in the shared library. */
1793
1794 if (! newdyn
1795 && bfd_is_com_section (sec)
1796 && olddyncommon)
1797 {
1798 /* It would be best if we could set the hash table entry to a
1799 common symbol, but we don't know what to use for the section
1800 or the alignment. */
1801 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1802 bfd_link_hash_common, sym->st_size);
1803
1804 /* If the presumed common symbol in the dynamic object is
1805 larger, pretend that the new symbol has its size. */
1806
1807 if (h->size > *pvalue)
1808 *pvalue = h->size;
1809
1810 /* We need to remember the alignment required by the symbol
1811 in the dynamic object. */
1812 BFD_ASSERT (pold_alignment);
1813 *pold_alignment = h->root.u.def.section->alignment_power;
1814
1815 olddef = false;
1816 olddyncommon = false;
1817
1818 h->root.type = bfd_link_hash_undefined;
1819 h->root.u.undef.abfd = h->root.u.def.section->owner;
1820
1821 *size_change_ok = true;
1822 *type_change_ok = true;
1823
1824 if (hi->root.type == bfd_link_hash_indirect)
1825 flip = hi;
1826 else
1827 h->verinfo.vertree = NULL;
1828 }
1829
1830 if (flip != NULL)
1831 {
1832 /* Handle the case where we had a versioned symbol in a dynamic
1833 library and now find a definition in a normal object. In this
1834 case, we make the versioned symbol point to the normal one. */
1835 flip->root.type = h->root.type;
1836 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1837 h->root.type = bfd_link_hash_indirect;
1838 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1839 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1840 if (h->def_dynamic)
1841 {
1842 h->def_dynamic = 0;
1843 flip->ref_dynamic = 1;
1844 }
1845 }
1846
1847 return true;
1848 }
1849
1850 /* This function is called to create an indirect symbol from the
1851 default for the symbol with the default version if needed. The
1852 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1853 set DYNSYM if the new indirect symbol is dynamic. */
1854
1855 static bool
_bfd_elf_add_default_symbol(bfd * abfd,struct bfd_link_info * info,struct elf_link_hash_entry * h,const char * name,Elf_Internal_Sym * sym,asection * sec,bfd_vma value,bfd ** poldbfd,bool * dynsym)1856 _bfd_elf_add_default_symbol (bfd *abfd,
1857 struct bfd_link_info *info,
1858 struct elf_link_hash_entry *h,
1859 const char *name,
1860 Elf_Internal_Sym *sym,
1861 asection *sec,
1862 bfd_vma value,
1863 bfd **poldbfd,
1864 bool *dynsym)
1865 {
1866 bool type_change_ok;
1867 bool size_change_ok;
1868 bool skip;
1869 char *shortname;
1870 struct elf_link_hash_entry *hi;
1871 struct bfd_link_hash_entry *bh;
1872 const struct elf_backend_data *bed;
1873 bool collect;
1874 bool dynamic;
1875 bfd *override;
1876 char *p;
1877 size_t len, shortlen;
1878 asection *tmp_sec;
1879 bool matched;
1880
1881 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1882 return true;
1883
1884 /* If this symbol has a version, and it is the default version, we
1885 create an indirect symbol from the default name to the fully
1886 decorated name. This will cause external references which do not
1887 specify a version to be bound to this version of the symbol. */
1888 p = strchr (name, ELF_VER_CHR);
1889 if (h->versioned == unknown)
1890 {
1891 if (p == NULL)
1892 {
1893 h->versioned = unversioned;
1894 return true;
1895 }
1896 else
1897 {
1898 if (p[1] != ELF_VER_CHR)
1899 {
1900 h->versioned = versioned_hidden;
1901 return true;
1902 }
1903 else
1904 h->versioned = versioned;
1905 }
1906 }
1907 else
1908 {
1909 /* PR ld/19073: We may see an unversioned definition after the
1910 default version. */
1911 if (p == NULL)
1912 return true;
1913 }
1914
1915 bed = get_elf_backend_data (abfd);
1916 collect = bed->collect;
1917 dynamic = (abfd->flags & DYNAMIC) != 0;
1918
1919 shortlen = p - name;
1920 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1921 if (shortname == NULL)
1922 return false;
1923 memcpy (shortname, name, shortlen);
1924 shortname[shortlen] = '\0';
1925
1926 /* We are going to create a new symbol. Merge it with any existing
1927 symbol with this name. For the purposes of the merge, act as
1928 though we were defining the symbol we just defined, although we
1929 actually going to define an indirect symbol. */
1930 type_change_ok = false;
1931 size_change_ok = false;
1932 matched = true;
1933 tmp_sec = sec;
1934 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1935 &hi, poldbfd, NULL, NULL, &skip, &override,
1936 &type_change_ok, &size_change_ok, &matched))
1937 return false;
1938
1939 if (skip)
1940 goto nondefault;
1941
1942 if (hi->def_regular || ELF_COMMON_DEF_P (hi))
1943 {
1944 /* If the undecorated symbol will have a version added by a
1945 script different to H, then don't indirect to/from the
1946 undecorated symbol. This isn't ideal because we may not yet
1947 have seen symbol versions, if given by a script on the
1948 command line rather than via --version-script. */
1949 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1950 {
1951 bool hide;
1952
1953 hi->verinfo.vertree
1954 = bfd_find_version_for_sym (info->version_info,
1955 hi->root.root.string, &hide);
1956 if (hi->verinfo.vertree != NULL && hide)
1957 {
1958 (*bed->elf_backend_hide_symbol) (info, hi, true);
1959 goto nondefault;
1960 }
1961 }
1962 if (hi->verinfo.vertree != NULL
1963 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1964 goto nondefault;
1965 }
1966
1967 if (! override)
1968 {
1969 /* Add the default symbol if not performing a relocatable link. */
1970 if (! bfd_link_relocatable (info))
1971 {
1972 bh = &hi->root;
1973 if (bh->type == bfd_link_hash_defined
1974 && bh->u.def.section->owner != NULL
1975 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
1976 {
1977 /* Mark the previous definition from IR object as
1978 undefined so that the generic linker will override
1979 it. */
1980 bh->type = bfd_link_hash_undefined;
1981 bh->u.undef.abfd = bh->u.def.section->owner;
1982 }
1983 if (! (_bfd_generic_link_add_one_symbol
1984 (info, abfd, shortname, BSF_INDIRECT,
1985 bfd_ind_section_ptr,
1986 0, name, false, collect, &bh)))
1987 return false;
1988 hi = (struct elf_link_hash_entry *) bh;
1989 }
1990 }
1991 else
1992 {
1993 /* In this case the symbol named SHORTNAME is overriding the
1994 indirect symbol we want to add. We were planning on making
1995 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1996 is the name without a version. NAME is the fully versioned
1997 name, and it is the default version.
1998
1999 Overriding means that we already saw a definition for the
2000 symbol SHORTNAME in a regular object, and it is overriding
2001 the symbol defined in the dynamic object.
2002
2003 When this happens, we actually want to change NAME, the
2004 symbol we just added, to refer to SHORTNAME. This will cause
2005 references to NAME in the shared object to become references
2006 to SHORTNAME in the regular object. This is what we expect
2007 when we override a function in a shared object: that the
2008 references in the shared object will be mapped to the
2009 definition in the regular object. */
2010
2011 while (hi->root.type == bfd_link_hash_indirect
2012 || hi->root.type == bfd_link_hash_warning)
2013 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2014
2015 h->root.type = bfd_link_hash_indirect;
2016 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
2017 if (h->def_dynamic)
2018 {
2019 h->def_dynamic = 0;
2020 hi->ref_dynamic = 1;
2021 if (hi->ref_regular
2022 || hi->def_regular)
2023 {
2024 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
2025 return false;
2026 }
2027 }
2028
2029 /* Now set HI to H, so that the following code will set the
2030 other fields correctly. */
2031 hi = h;
2032 }
2033
2034 /* Check if HI is a warning symbol. */
2035 if (hi->root.type == bfd_link_hash_warning)
2036 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2037
2038 /* If there is a duplicate definition somewhere, then HI may not
2039 point to an indirect symbol. We will have reported an error to
2040 the user in that case. */
2041
2042 if (hi->root.type == bfd_link_hash_indirect)
2043 {
2044 struct elf_link_hash_entry *ht;
2045
2046 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2047 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2048
2049 /* If we first saw a reference to SHORTNAME with non-default
2050 visibility, merge that visibility to the @@VER symbol. */
2051 elf_merge_st_other (abfd, ht, hi->other, sec, true, dynamic);
2052
2053 /* A reference to the SHORTNAME symbol from a dynamic library
2054 will be satisfied by the versioned symbol at runtime. In
2055 effect, we have a reference to the versioned symbol. */
2056 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2057 hi->dynamic_def |= ht->dynamic_def;
2058
2059 /* See if the new flags lead us to realize that the symbol must
2060 be dynamic. */
2061 if (! *dynsym)
2062 {
2063 if (! dynamic)
2064 {
2065 if (! bfd_link_executable (info)
2066 || hi->def_dynamic
2067 || hi->ref_dynamic)
2068 *dynsym = true;
2069 }
2070 else
2071 {
2072 if (hi->ref_regular)
2073 *dynsym = true;
2074 }
2075 }
2076 }
2077
2078 /* We also need to define an indirection from the nondefault version
2079 of the symbol. */
2080
2081 nondefault:
2082 len = strlen (name);
2083 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2084 if (shortname == NULL)
2085 return false;
2086 memcpy (shortname, name, shortlen);
2087 memcpy (shortname + shortlen, p + 1, len - shortlen);
2088
2089 /* Once again, merge with any existing symbol. */
2090 type_change_ok = false;
2091 size_change_ok = false;
2092 tmp_sec = sec;
2093 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2094 &hi, poldbfd, NULL, NULL, &skip, &override,
2095 &type_change_ok, &size_change_ok, &matched))
2096 return false;
2097
2098 if (skip)
2099 {
2100 if (!dynamic
2101 && h->root.type == bfd_link_hash_defweak
2102 && hi->root.type == bfd_link_hash_defined)
2103 {
2104 /* We are handling a weak sym@@ver and attempting to define
2105 a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2106 new weak sym@ver because there is already a strong sym@ver.
2107 However, sym@ver and sym@@ver are really the same symbol.
2108 The existing strong sym@ver ought to override sym@@ver. */
2109 h->root.type = bfd_link_hash_defined;
2110 h->root.u.def.section = hi->root.u.def.section;
2111 h->root.u.def.value = hi->root.u.def.value;
2112 hi->root.type = bfd_link_hash_indirect;
2113 hi->root.u.i.link = &h->root;
2114 }
2115 else
2116 return true;
2117 }
2118 else if (override)
2119 {
2120 /* Here SHORTNAME is a versioned name, so we don't expect to see
2121 the type of override we do in the case above unless it is
2122 overridden by a versioned definition. */
2123 if (hi->root.type != bfd_link_hash_defined
2124 && hi->root.type != bfd_link_hash_defweak)
2125 _bfd_error_handler
2126 /* xgettext:c-format */
2127 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2128 abfd, shortname);
2129 return true;
2130 }
2131 else
2132 {
2133 bh = &hi->root;
2134 if (! (_bfd_generic_link_add_one_symbol
2135 (info, abfd, shortname, BSF_INDIRECT,
2136 bfd_ind_section_ptr, 0, name, false, collect, &bh)))
2137 return false;
2138 hi = (struct elf_link_hash_entry *) bh;
2139 }
2140
2141 /* If there is a duplicate definition somewhere, then HI may not
2142 point to an indirect symbol. We will have reported an error
2143 to the user in that case. */
2144 if (hi->root.type == bfd_link_hash_indirect)
2145 {
2146 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2147 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2148 hi->dynamic_def |= h->dynamic_def;
2149
2150 /* If we first saw a reference to @VER symbol with
2151 non-default visibility, merge that visibility to the
2152 @@VER symbol. */
2153 elf_merge_st_other (abfd, h, hi->other, sec, true, dynamic);
2154
2155 /* See if the new flags lead us to realize that the symbol
2156 must be dynamic. */
2157 if (! *dynsym)
2158 {
2159 if (! dynamic)
2160 {
2161 if (! bfd_link_executable (info)
2162 || hi->ref_dynamic)
2163 *dynsym = true;
2164 }
2165 else
2166 {
2167 if (hi->ref_regular)
2168 *dynsym = true;
2169 }
2170 }
2171 }
2172
2173 return true;
2174 }
2175
2176 /* This routine is used to export all defined symbols into the dynamic
2177 symbol table. It is called via elf_link_hash_traverse. */
2178
2179 static bool
_bfd_elf_export_symbol(struct elf_link_hash_entry * h,void * data)2180 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2181 {
2182 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2183
2184 /* Ignore indirect symbols. These are added by the versioning code. */
2185 if (h->root.type == bfd_link_hash_indirect)
2186 return true;
2187
2188 /* Ignore this if we won't export it. */
2189 if (!eif->info->export_dynamic && !h->dynamic)
2190 return true;
2191
2192 if (h->dynindx == -1
2193 && (h->def_regular || h->ref_regular)
2194 && ! bfd_hide_sym_by_version (eif->info->version_info,
2195 h->root.root.string))
2196 {
2197 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2198 {
2199 eif->failed = true;
2200 return false;
2201 }
2202 }
2203
2204 return true;
2205 }
2206
2207 /* Return the glibc version reference if VERSION_DEP is added to the
2208 list of glibc version dependencies successfully. VERSION_DEP will
2209 be put into the .gnu.version_r section. */
2210
2211 static Elf_Internal_Verneed *
elf_link_add_glibc_verneed(struct elf_find_verdep_info * rinfo,Elf_Internal_Verneed * glibc_verref,const char * version_dep)2212 elf_link_add_glibc_verneed (struct elf_find_verdep_info *rinfo,
2213 Elf_Internal_Verneed *glibc_verref,
2214 const char *version_dep)
2215 {
2216 Elf_Internal_Verneed *t;
2217 Elf_Internal_Vernaux *a;
2218 size_t amt;
2219
2220 if (glibc_verref != NULL)
2221 {
2222 t = glibc_verref;
2223
2224 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2225 {
2226 /* Return if VERSION_DEP dependency has been added. */
2227 if (a->vna_nodename == version_dep
2228 || strcmp (a->vna_nodename, version_dep) == 0)
2229 return t;
2230 }
2231 }
2232 else
2233 {
2234 bool is_glibc;
2235
2236 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2237 t != NULL;
2238 t = t->vn_nextref)
2239 {
2240 const char *soname = bfd_elf_get_dt_soname (t->vn_bfd);
2241 if (soname != NULL && startswith (soname, "libc.so."))
2242 break;
2243 }
2244
2245 /* Skip the shared library if it isn't libc.so. */
2246 if (t == NULL)
2247 return t;
2248
2249 is_glibc = false;
2250 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2251 {
2252 /* Return if VERSION_DEP dependency has been added. */
2253 if (a->vna_nodename == version_dep
2254 || strcmp (a->vna_nodename, version_dep) == 0)
2255 return t;
2256
2257 /* Check if libc.so provides GLIBC_2.XX version. */
2258 if (!is_glibc && startswith (a->vna_nodename, "GLIBC_2."))
2259 is_glibc = true;
2260 }
2261
2262 /* Skip if it isn't linked against glibc. */
2263 if (!is_glibc)
2264 return NULL;
2265 }
2266
2267 amt = sizeof *a;
2268 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2269 if (a == NULL)
2270 {
2271 rinfo->failed = true;
2272 return NULL;
2273 }
2274
2275 a->vna_nodename = version_dep;
2276 a->vna_flags = 0;
2277 a->vna_nextptr = t->vn_auxptr;
2278 a->vna_other = rinfo->vers + 1;
2279 ++rinfo->vers;
2280
2281 t->vn_auxptr = a;
2282
2283 return t;
2284 }
2285
2286 /* Add VERSION_DEP to the list of version dependencies when linked
2287 against glibc. */
2288
2289 void
_bfd_elf_link_add_glibc_version_dependency(struct elf_find_verdep_info * rinfo,const char * version_dep[])2290 _bfd_elf_link_add_glibc_version_dependency
2291 (struct elf_find_verdep_info *rinfo,
2292 const char *version_dep[])
2293 {
2294 Elf_Internal_Verneed *t = NULL;
2295
2296 do
2297 {
2298 t = elf_link_add_glibc_verneed (rinfo, t, *version_dep);
2299 /* Return if there is no glibc version reference. */
2300 if (t == NULL)
2301 return;
2302 version_dep++;
2303 }
2304 while (*version_dep != NULL);
2305 }
2306
2307 /* Add GLIBC_ABI_DT_RELR to the list of version dependencies when
2308 linked against glibc. */
2309
2310 void
_bfd_elf_link_add_dt_relr_dependency(struct elf_find_verdep_info * rinfo)2311 _bfd_elf_link_add_dt_relr_dependency (struct elf_find_verdep_info *rinfo)
2312 {
2313 if (rinfo->info->enable_dt_relr)
2314 {
2315 const char *version[] =
2316 {
2317 "GLIBC_ABI_DT_RELR",
2318 NULL
2319 };
2320 _bfd_elf_link_add_glibc_version_dependency (rinfo, version);
2321 }
2322 }
2323
2324 /* Look through the symbols which are defined in other shared
2325 libraries and referenced here. Update the list of version
2326 dependencies. This will be put into the .gnu.version_r section.
2327 This function is called via elf_link_hash_traverse. */
2328
2329 static bool
_bfd_elf_link_find_version_dependencies(struct elf_link_hash_entry * h,void * data)2330 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2331 void *data)
2332 {
2333 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2334 Elf_Internal_Verneed *t;
2335 Elf_Internal_Vernaux *a;
2336 size_t amt;
2337
2338 /* We only care about symbols defined in shared objects with version
2339 information. */
2340 if (!h->def_dynamic
2341 || h->def_regular
2342 || h->dynindx == -1
2343 || h->verinfo.verdef == NULL
2344 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2345 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2346 return true;
2347
2348 /* See if we already know about this version. */
2349 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2350 t != NULL;
2351 t = t->vn_nextref)
2352 {
2353 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2354 continue;
2355
2356 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2357 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2358 return true;
2359
2360 break;
2361 }
2362
2363 /* This is a new version. Add it to tree we are building. */
2364
2365 if (t == NULL)
2366 {
2367 amt = sizeof *t;
2368 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2369 if (t == NULL)
2370 {
2371 rinfo->failed = true;
2372 return false;
2373 }
2374
2375 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2376 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2377 elf_tdata (rinfo->info->output_bfd)->verref = t;
2378 }
2379
2380 amt = sizeof *a;
2381 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2382 if (a == NULL)
2383 {
2384 rinfo->failed = true;
2385 return false;
2386 }
2387
2388 /* Note that we are copying a string pointer here, and testing it
2389 above. If bfd_elf_string_from_elf_section is ever changed to
2390 discard the string data when low in memory, this will have to be
2391 fixed. */
2392 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2393
2394 a->vna_flags = h->verinfo.verdef->vd_flags;
2395 a->vna_nextptr = t->vn_auxptr;
2396
2397 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2398 ++rinfo->vers;
2399
2400 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2401
2402 t->vn_auxptr = a;
2403
2404 return true;
2405 }
2406
2407 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2408 hidden. Set *T_P to NULL if there is no match. */
2409
2410 static bool
_bfd_elf_link_hide_versioned_symbol(struct bfd_link_info * info,struct elf_link_hash_entry * h,const char * version_p,struct bfd_elf_version_tree ** t_p,bool * hide)2411 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2412 struct elf_link_hash_entry *h,
2413 const char *version_p,
2414 struct bfd_elf_version_tree **t_p,
2415 bool *hide)
2416 {
2417 struct bfd_elf_version_tree *t;
2418
2419 /* Look for the version. If we find it, it is no longer weak. */
2420 for (t = info->version_info; t != NULL; t = t->next)
2421 {
2422 if (strcmp (t->name, version_p) == 0)
2423 {
2424 size_t len;
2425 char *alc;
2426 struct bfd_elf_version_expr *d;
2427
2428 len = version_p - h->root.root.string;
2429 alc = (char *) bfd_malloc (len);
2430 if (alc == NULL)
2431 return false;
2432 memcpy (alc, h->root.root.string, len - 1);
2433 alc[len - 1] = '\0';
2434 if (alc[len - 2] == ELF_VER_CHR)
2435 alc[len - 2] = '\0';
2436
2437 h->verinfo.vertree = t;
2438 t->used = true;
2439 d = NULL;
2440
2441 if (t->globals.list != NULL)
2442 d = (*t->match) (&t->globals, NULL, alc);
2443
2444 /* See if there is anything to force this symbol to
2445 local scope. */
2446 if (d == NULL && t->locals.list != NULL)
2447 {
2448 d = (*t->match) (&t->locals, NULL, alc);
2449 if (d != NULL
2450 && h->dynindx != -1
2451 && ! info->export_dynamic)
2452 *hide = true;
2453 }
2454
2455 free (alc);
2456 break;
2457 }
2458 }
2459
2460 *t_p = t;
2461
2462 return true;
2463 }
2464
2465 /* Return TRUE if the symbol H is hidden by version script. */
2466
2467 bool
_bfd_elf_link_hide_sym_by_version(struct bfd_link_info * info,struct elf_link_hash_entry * h)2468 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2469 struct elf_link_hash_entry *h)
2470 {
2471 const char *p;
2472 bool hide = false;
2473 const struct elf_backend_data *bed
2474 = get_elf_backend_data (info->output_bfd);
2475
2476 /* Version script only hides symbols defined in regular objects. */
2477 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2478 return true;
2479
2480 p = strchr (h->root.root.string, ELF_VER_CHR);
2481 if (p != NULL && h->verinfo.vertree == NULL)
2482 {
2483 struct bfd_elf_version_tree *t;
2484
2485 ++p;
2486 if (*p == ELF_VER_CHR)
2487 ++p;
2488
2489 if (*p != '\0'
2490 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2491 && hide)
2492 {
2493 if (hide)
2494 (*bed->elf_backend_hide_symbol) (info, h, true);
2495 return true;
2496 }
2497 }
2498
2499 /* If we don't have a version for this symbol, see if we can find
2500 something. */
2501 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2502 {
2503 h->verinfo.vertree
2504 = bfd_find_version_for_sym (info->version_info,
2505 h->root.root.string, &hide);
2506 if (h->verinfo.vertree != NULL && hide)
2507 {
2508 (*bed->elf_backend_hide_symbol) (info, h, true);
2509 return true;
2510 }
2511 }
2512
2513 return false;
2514 }
2515
2516 /* Figure out appropriate versions for all the symbols. We may not
2517 have the version number script until we have read all of the input
2518 files, so until that point we don't know which symbols should be
2519 local. This function is called via elf_link_hash_traverse. */
2520
2521 static bool
_bfd_elf_link_assign_sym_version(struct elf_link_hash_entry * h,void * data)2522 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2523 {
2524 struct elf_info_failed *sinfo;
2525 struct bfd_link_info *info;
2526 const struct elf_backend_data *bed;
2527 struct elf_info_failed eif;
2528 char *p;
2529 bool hide;
2530
2531 sinfo = (struct elf_info_failed *) data;
2532 info = sinfo->info;
2533
2534 /* Fix the symbol flags. */
2535 eif.failed = false;
2536 eif.info = info;
2537 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2538 {
2539 if (eif.failed)
2540 sinfo->failed = true;
2541 return false;
2542 }
2543
2544 bed = get_elf_backend_data (info->output_bfd);
2545
2546 /* We only need version numbers for symbols defined in regular
2547 objects. */
2548 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2549 {
2550 /* Hide symbols defined in discarded input sections. */
2551 if ((h->root.type == bfd_link_hash_defined
2552 || h->root.type == bfd_link_hash_defweak)
2553 && discarded_section (h->root.u.def.section))
2554 (*bed->elf_backend_hide_symbol) (info, h, true);
2555 return true;
2556 }
2557
2558 hide = false;
2559 p = strchr (h->root.root.string, ELF_VER_CHR);
2560 if (p != NULL && h->verinfo.vertree == NULL)
2561 {
2562 struct bfd_elf_version_tree *t;
2563
2564 ++p;
2565 if (*p == ELF_VER_CHR)
2566 ++p;
2567
2568 /* If there is no version string, we can just return out. */
2569 if (*p == '\0')
2570 return true;
2571
2572 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2573 {
2574 sinfo->failed = true;
2575 return false;
2576 }
2577
2578 if (hide)
2579 (*bed->elf_backend_hide_symbol) (info, h, true);
2580
2581 /* If we are building an application, we need to create a
2582 version node for this version. */
2583 if (t == NULL && bfd_link_executable (info))
2584 {
2585 struct bfd_elf_version_tree **pp;
2586 int version_index;
2587
2588 /* If we aren't going to export this symbol, we don't need
2589 to worry about it. */
2590 if (h->dynindx == -1)
2591 return true;
2592
2593 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2594 sizeof *t);
2595 if (t == NULL)
2596 {
2597 sinfo->failed = true;
2598 return false;
2599 }
2600
2601 t->name = p;
2602 t->name_indx = (unsigned int) -1;
2603 t->used = true;
2604
2605 version_index = 1;
2606 /* Don't count anonymous version tag. */
2607 if (sinfo->info->version_info != NULL
2608 && sinfo->info->version_info->vernum == 0)
2609 version_index = 0;
2610 for (pp = &sinfo->info->version_info;
2611 *pp != NULL;
2612 pp = &(*pp)->next)
2613 ++version_index;
2614 t->vernum = version_index;
2615
2616 *pp = t;
2617
2618 h->verinfo.vertree = t;
2619 }
2620 else if (t == NULL)
2621 {
2622 /* We could not find the version for a symbol when
2623 generating a shared archive. Return an error. */
2624 _bfd_error_handler
2625 /* xgettext:c-format */
2626 (_("%pB: version node not found for symbol %s"),
2627 info->output_bfd, h->root.root.string);
2628 bfd_set_error (bfd_error_bad_value);
2629 sinfo->failed = true;
2630 return false;
2631 }
2632 }
2633
2634 /* If we don't have a version for this symbol, see if we can find
2635 something. */
2636 if (!hide
2637 && h->verinfo.vertree == NULL
2638 && sinfo->info->version_info != NULL)
2639 {
2640 h->verinfo.vertree
2641 = bfd_find_version_for_sym (sinfo->info->version_info,
2642 h->root.root.string, &hide);
2643 if (h->verinfo.vertree != NULL && hide)
2644 (*bed->elf_backend_hide_symbol) (info, h, true);
2645 }
2646
2647 return true;
2648 }
2649
2650 /* Read and swap the relocs from the section indicated by SHDR. This
2651 may be either a REL or a RELA section. The relocations are
2652 translated into RELA relocations and stored in INTERNAL_RELOCS,
2653 which should have already been allocated to contain enough space.
2654 The EXTERNAL_RELOCS are a buffer where the external form of the
2655 relocations should be stored.
2656
2657 Returns FALSE if something goes wrong. */
2658
2659 static bool
elf_link_read_relocs_from_section(bfd * abfd,asection * sec,Elf_Internal_Shdr * shdr,void * external_relocs,Elf_Internal_Rela * internal_relocs)2660 elf_link_read_relocs_from_section (bfd *abfd,
2661 asection *sec,
2662 Elf_Internal_Shdr *shdr,
2663 void *external_relocs,
2664 Elf_Internal_Rela *internal_relocs)
2665 {
2666 const struct elf_backend_data *bed;
2667 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2668 const bfd_byte *erela;
2669 const bfd_byte *erelaend;
2670 Elf_Internal_Rela *irela;
2671 Elf_Internal_Shdr *symtab_hdr;
2672 size_t nsyms;
2673
2674 /* Position ourselves at the start of the section. */
2675 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2676 return false;
2677
2678 /* Read the relocations. */
2679 if (bfd_read (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2680 return false;
2681
2682 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2683 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2684
2685 bed = get_elf_backend_data (abfd);
2686
2687 /* Convert the external relocations to the internal format. */
2688 if (shdr->sh_entsize == bed->s->sizeof_rel)
2689 swap_in = bed->s->swap_reloc_in;
2690 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2691 swap_in = bed->s->swap_reloca_in;
2692 else
2693 {
2694 bfd_set_error (bfd_error_wrong_format);
2695 return false;
2696 }
2697
2698 erela = (const bfd_byte *) external_relocs;
2699 /* Setting erelaend like this and comparing with <= handles case of
2700 a fuzzed object with sh_size not a multiple of sh_entsize. */
2701 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
2702 irela = internal_relocs;
2703 while (erela <= erelaend)
2704 {
2705 bfd_vma r_symndx;
2706
2707 (*swap_in) (abfd, erela, irela);
2708 r_symndx = ELF32_R_SYM (irela->r_info);
2709 if (bed->s->arch_size == 64)
2710 r_symndx >>= 24;
2711 if (nsyms > 0)
2712 {
2713 if ((size_t) r_symndx >= nsyms)
2714 {
2715 _bfd_error_handler
2716 /* xgettext:c-format */
2717 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2718 " for offset %#" PRIx64 " in section `%pA'"),
2719 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2720 (uint64_t) irela->r_offset, sec);
2721 bfd_set_error (bfd_error_bad_value);
2722 return false;
2723 }
2724 }
2725 else if (r_symndx != STN_UNDEF)
2726 {
2727 _bfd_error_handler
2728 /* xgettext:c-format */
2729 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2730 " for offset %#" PRIx64 " in section `%pA'"
2731 " when the object file has no symbol table"),
2732 abfd, (uint64_t) r_symndx,
2733 (uint64_t) irela->r_offset, sec);
2734 bfd_set_error (bfd_error_bad_value);
2735 return false;
2736 }
2737 irela += bed->s->int_rels_per_ext_rel;
2738 erela += shdr->sh_entsize;
2739 }
2740
2741 return true;
2742 }
2743
2744 /* Read and swap the relocs for a section O. They may have been
2745 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2746 not NULL, they are used as buffers to read into. They are known to
2747 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2748 the return value is allocated using either malloc or bfd_alloc,
2749 according to the KEEP_MEMORY argument. If O has two relocation
2750 sections (both REL and RELA relocations), then the REL_HDR
2751 relocations will appear first in INTERNAL_RELOCS, followed by the
2752 RELA_HDR relocations. If INFO isn't NULL and KEEP_MEMORY is true,
2753 update cache_size. */
2754
2755 Elf_Internal_Rela *
_bfd_elf_link_info_read_relocs(bfd * abfd,struct bfd_link_info * info,asection * o,void * external_relocs,Elf_Internal_Rela * internal_relocs,bool keep_memory)2756 _bfd_elf_link_info_read_relocs (bfd *abfd,
2757 struct bfd_link_info *info,
2758 asection *o,
2759 void *external_relocs,
2760 Elf_Internal_Rela *internal_relocs,
2761 bool keep_memory)
2762 {
2763 void *alloc1 = NULL;
2764 Elf_Internal_Rela *alloc2 = NULL;
2765 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2766 struct bfd_elf_section_data *esdo = elf_section_data (o);
2767 Elf_Internal_Rela *internal_rela_relocs;
2768
2769 if (esdo->relocs != NULL)
2770 return esdo->relocs;
2771
2772 if (o->reloc_count == 0)
2773 return NULL;
2774
2775 if (internal_relocs == NULL)
2776 {
2777 bfd_size_type size;
2778
2779 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2780 if (keep_memory)
2781 {
2782 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2783 if (info)
2784 info->cache_size += size;
2785 }
2786 else
2787 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2788 if (internal_relocs == NULL)
2789 goto error_return;
2790 }
2791
2792 if (external_relocs == NULL)
2793 {
2794 bfd_size_type size = 0;
2795
2796 if (esdo->rel.hdr)
2797 size += esdo->rel.hdr->sh_size;
2798 if (esdo->rela.hdr)
2799 size += esdo->rela.hdr->sh_size;
2800
2801 alloc1 = bfd_malloc (size);
2802 if (alloc1 == NULL)
2803 goto error_return;
2804 external_relocs = alloc1;
2805 }
2806
2807 internal_rela_relocs = internal_relocs;
2808 if (esdo->rel.hdr)
2809 {
2810 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2811 external_relocs,
2812 internal_relocs))
2813 goto error_return;
2814 external_relocs = (((bfd_byte *) external_relocs)
2815 + esdo->rel.hdr->sh_size);
2816 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2817 * bed->s->int_rels_per_ext_rel);
2818 }
2819
2820 if (esdo->rela.hdr
2821 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2822 external_relocs,
2823 internal_rela_relocs)))
2824 goto error_return;
2825
2826 /* Cache the results for next time, if we can. */
2827 if (keep_memory)
2828 esdo->relocs = internal_relocs;
2829
2830 free (alloc1);
2831
2832 /* Don't free alloc2, since if it was allocated we are passing it
2833 back (under the name of internal_relocs). */
2834
2835 return internal_relocs;
2836
2837 error_return:
2838 free (alloc1);
2839 if (alloc2 != NULL)
2840 {
2841 if (keep_memory)
2842 bfd_release (abfd, alloc2);
2843 else
2844 free (alloc2);
2845 }
2846 return NULL;
2847 }
2848
2849 /* This is similar to _bfd_elf_link_info_read_relocs, except for that
2850 NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
2851 struct bfd_link_info. */
2852
2853 Elf_Internal_Rela *
_bfd_elf_link_read_relocs(bfd * abfd,asection * o,void * external_relocs,Elf_Internal_Rela * internal_relocs,bool keep_memory)2854 _bfd_elf_link_read_relocs (bfd *abfd,
2855 asection *o,
2856 void *external_relocs,
2857 Elf_Internal_Rela *internal_relocs,
2858 bool keep_memory)
2859 {
2860 return _bfd_elf_link_info_read_relocs (abfd, NULL, o, external_relocs,
2861 internal_relocs, keep_memory);
2862
2863 }
2864
2865 /* Compute the size of, and allocate space for, REL_HDR which is the
2866 section header for a section containing relocations for O. */
2867
2868 static bool
_bfd_elf_link_size_reloc_section(bfd * abfd,struct bfd_elf_section_reloc_data * reldata)2869 _bfd_elf_link_size_reloc_section (bfd *abfd,
2870 struct bfd_elf_section_reloc_data *reldata)
2871 {
2872 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2873
2874 /* That allows us to calculate the size of the section. */
2875 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2876
2877 /* The contents field must last into write_object_contents, so we
2878 allocate it with bfd_alloc rather than malloc. Also since we
2879 cannot be sure that the contents will actually be filled in,
2880 we zero the allocated space. */
2881 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2882 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2883 return false;
2884
2885 if (reldata->hashes == NULL && reldata->count)
2886 {
2887 struct elf_link_hash_entry **p;
2888
2889 p = ((struct elf_link_hash_entry **)
2890 bfd_zmalloc (reldata->count * sizeof (*p)));
2891 if (p == NULL)
2892 return false;
2893
2894 reldata->hashes = p;
2895 }
2896
2897 return true;
2898 }
2899
2900 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2901 originated from the section given by INPUT_REL_HDR) to the
2902 OUTPUT_BFD. */
2903
2904 bool
_bfd_elf_link_output_relocs(bfd * output_bfd,asection * input_section,Elf_Internal_Shdr * input_rel_hdr,Elf_Internal_Rela * internal_relocs,struct elf_link_hash_entry ** rel_hash ATTRIBUTE_UNUSED)2905 _bfd_elf_link_output_relocs (bfd *output_bfd,
2906 asection *input_section,
2907 Elf_Internal_Shdr *input_rel_hdr,
2908 Elf_Internal_Rela *internal_relocs,
2909 struct elf_link_hash_entry **rel_hash
2910 ATTRIBUTE_UNUSED)
2911 {
2912 Elf_Internal_Rela *irela;
2913 Elf_Internal_Rela *irelaend;
2914 bfd_byte *erel;
2915 struct bfd_elf_section_reloc_data *output_reldata;
2916 asection *output_section;
2917 const struct elf_backend_data *bed;
2918 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2919 struct bfd_elf_section_data *esdo;
2920
2921 output_section = input_section->output_section;
2922
2923 bed = get_elf_backend_data (output_bfd);
2924 esdo = elf_section_data (output_section);
2925 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2926 {
2927 output_reldata = &esdo->rel;
2928 swap_out = bed->s->swap_reloc_out;
2929 }
2930 else if (esdo->rela.hdr
2931 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2932 {
2933 output_reldata = &esdo->rela;
2934 swap_out = bed->s->swap_reloca_out;
2935 }
2936 else
2937 {
2938 _bfd_error_handler
2939 /* xgettext:c-format */
2940 (_("%pB: relocation size mismatch in %pB section %pA"),
2941 output_bfd, input_section->owner, input_section);
2942 bfd_set_error (bfd_error_wrong_format);
2943 return false;
2944 }
2945
2946 erel = output_reldata->hdr->contents;
2947 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2948 irela = internal_relocs;
2949 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2950 * bed->s->int_rels_per_ext_rel);
2951 while (irela < irelaend)
2952 {
2953 (*swap_out) (output_bfd, irela, erel);
2954 irela += bed->s->int_rels_per_ext_rel;
2955 erel += input_rel_hdr->sh_entsize;
2956 }
2957
2958 /* Bump the counter, so that we know where to add the next set of
2959 relocations. */
2960 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2961
2962 return true;
2963 }
2964
2965 /* Make weak undefined symbols in PIE dynamic. */
2966
2967 bool
_bfd_elf_link_hash_fixup_symbol(struct bfd_link_info * info,struct elf_link_hash_entry * h)2968 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2969 struct elf_link_hash_entry *h)
2970 {
2971 if (bfd_link_pie (info)
2972 && h->dynindx == -1
2973 && h->root.type == bfd_link_hash_undefweak)
2974 return bfd_elf_link_record_dynamic_symbol (info, h);
2975
2976 return true;
2977 }
2978
2979 /* Fix up the flags for a symbol. This handles various cases which
2980 can only be fixed after all the input files are seen. This is
2981 currently called by both adjust_dynamic_symbol and
2982 assign_sym_version, which is unnecessary but perhaps more robust in
2983 the face of future changes. */
2984
2985 static bool
_bfd_elf_fix_symbol_flags(struct elf_link_hash_entry * h,struct elf_info_failed * eif)2986 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2987 struct elf_info_failed *eif)
2988 {
2989 const struct elf_backend_data *bed;
2990
2991 /* If this symbol was mentioned in a non-ELF file, try to set
2992 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2993 permit a non-ELF file to correctly refer to a symbol defined in
2994 an ELF dynamic object. */
2995 if (h->non_elf)
2996 {
2997 while (h->root.type == bfd_link_hash_indirect)
2998 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2999
3000 if (h->root.type != bfd_link_hash_defined
3001 && h->root.type != bfd_link_hash_defweak)
3002 {
3003 h->ref_regular = 1;
3004 h->ref_regular_nonweak = 1;
3005 }
3006 else
3007 {
3008 if (h->root.u.def.section->owner != NULL
3009 && (bfd_get_flavour (h->root.u.def.section->owner)
3010 == bfd_target_elf_flavour))
3011 {
3012 h->ref_regular = 1;
3013 h->ref_regular_nonweak = 1;
3014 }
3015 else
3016 h->def_regular = 1;
3017 }
3018
3019 if (h->dynindx == -1
3020 && (h->def_dynamic
3021 || h->ref_dynamic))
3022 {
3023 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
3024 {
3025 eif->failed = true;
3026 return false;
3027 }
3028 }
3029 }
3030 else
3031 {
3032 /* Unfortunately, NON_ELF is only correct if the symbol
3033 was first seen in a non-ELF file. Fortunately, if the symbol
3034 was first seen in an ELF file, we're probably OK unless the
3035 symbol was defined in a non-ELF file. Catch that case here.
3036 FIXME: We're still in trouble if the symbol was first seen in
3037 a dynamic object, and then later in a non-ELF regular object. */
3038 if ((h->root.type == bfd_link_hash_defined
3039 || h->root.type == bfd_link_hash_defweak)
3040 && !h->def_regular
3041 && (h->root.u.def.section->owner != NULL
3042 ? (bfd_get_flavour (h->root.u.def.section->owner)
3043 != bfd_target_elf_flavour)
3044 : (bfd_is_abs_section (h->root.u.def.section)
3045 && !h->def_dynamic)))
3046 h->def_regular = 1;
3047 }
3048
3049 /* Backend specific symbol fixup. */
3050 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
3051 if (bed->elf_backend_fixup_symbol
3052 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
3053 return false;
3054
3055 /* If this is a final link, and the symbol was defined as a common
3056 symbol in a regular object file, and there was no definition in
3057 any dynamic object, then the linker will have allocated space for
3058 the symbol in a common section but the DEF_REGULAR
3059 flag will not have been set. */
3060 if (h->root.type == bfd_link_hash_defined
3061 && !h->def_regular
3062 && h->ref_regular
3063 && !h->def_dynamic
3064 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
3065 h->def_regular = 1;
3066
3067 /* Symbols defined in discarded sections shouldn't be dynamic. */
3068 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
3069 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3070
3071 /* If a weak undefined symbol has non-default visibility, we also
3072 hide it from the dynamic linker. */
3073 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3074 && h->root.type == bfd_link_hash_undefweak)
3075 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3076
3077 /* A hidden versioned symbol in executable should be forced local if
3078 it is is locally defined, not referenced by shared library and not
3079 exported. */
3080 else if (bfd_link_executable (eif->info)
3081 && h->versioned == versioned_hidden
3082 && !eif->info->export_dynamic
3083 && !h->dynamic
3084 && !h->ref_dynamic
3085 && h->def_regular)
3086 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3087
3088 /* If -Bsymbolic was used (which means to bind references to global
3089 symbols to the definition within the shared object), and this
3090 symbol was defined in a regular object, then it actually doesn't
3091 need a PLT entry. Likewise, if the symbol has non-default
3092 visibility. If the symbol has hidden or internal visibility, we
3093 will force it local. */
3094 else if (h->needs_plt
3095 && bfd_link_pic (eif->info)
3096 && is_elf_hash_table (eif->info->hash)
3097 && (SYMBOLIC_BIND (eif->info, h)
3098 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3099 && h->def_regular)
3100 {
3101 bool force_local;
3102
3103 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3104 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
3105 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
3106 }
3107
3108 /* If this is a weak defined symbol in a dynamic object, and we know
3109 the real definition in the dynamic object, copy interesting flags
3110 over to the real definition. */
3111 if (h->is_weakalias)
3112 {
3113 struct elf_link_hash_entry *def = weakdef (h);
3114 while (def->root.type == bfd_link_hash_indirect)
3115 def = (struct elf_link_hash_entry *) def->root.u.i.link;
3116
3117 /* If the real definition is defined by a regular object file,
3118 don't do anything special. See the longer description in
3119 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
3120 bfd_link_hash_defined as it was when put on the alias list
3121 then it must have originally been a versioned symbol (for
3122 which a non-versioned indirect symbol is created) and later
3123 a definition for the non-versioned symbol is found. In that
3124 case the indirection is flipped with the versioned symbol
3125 becoming an indirect pointing at the non-versioned symbol.
3126 Thus, not an alias any more. */
3127 if (def->def_regular
3128 || def->root.type != bfd_link_hash_defined)
3129 {
3130 h = def;
3131 while ((h = h->u.alias) != def)
3132 h->is_weakalias = 0;
3133 }
3134 else
3135 {
3136 while (h->root.type == bfd_link_hash_indirect)
3137 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3138 BFD_ASSERT (h->root.type == bfd_link_hash_defined
3139 || h->root.type == bfd_link_hash_defweak);
3140 BFD_ASSERT (def->def_dynamic);
3141 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
3142 }
3143 }
3144
3145 return true;
3146 }
3147
3148 /* Make the backend pick a good value for a dynamic symbol. This is
3149 called via elf_link_hash_traverse, and also calls itself
3150 recursively. */
3151
3152 static bool
_bfd_elf_adjust_dynamic_symbol(struct elf_link_hash_entry * h,void * data)3153 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
3154 {
3155 struct elf_info_failed *eif = (struct elf_info_failed *) data;
3156 struct elf_link_hash_table *htab;
3157 const struct elf_backend_data *bed;
3158
3159 if (! is_elf_hash_table (eif->info->hash))
3160 return false;
3161
3162 /* Ignore indirect symbols. These are added by the versioning code. */
3163 if (h->root.type == bfd_link_hash_indirect)
3164 return true;
3165
3166 /* Fix the symbol flags. */
3167 if (! _bfd_elf_fix_symbol_flags (h, eif))
3168 return false;
3169
3170 htab = elf_hash_table (eif->info);
3171 bed = get_elf_backend_data (htab->dynobj);
3172
3173 if (h->root.type == bfd_link_hash_undefweak)
3174 {
3175 if (eif->info->dynamic_undefined_weak == 0)
3176 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3177 else if (eif->info->dynamic_undefined_weak > 0
3178 && h->ref_regular
3179 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3180 && !bfd_hide_sym_by_version (eif->info->version_info,
3181 h->root.root.string))
3182 {
3183 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
3184 {
3185 eif->failed = true;
3186 return false;
3187 }
3188 }
3189 }
3190
3191 /* If this symbol does not require a PLT entry, and it is not
3192 defined by a dynamic object, or is not referenced by a regular
3193 object, ignore it. We do have to handle a weak defined symbol,
3194 even if no regular object refers to it, if we decided to add it
3195 to the dynamic symbol table. FIXME: Do we normally need to worry
3196 about symbols which are defined by one dynamic object and
3197 referenced by another one? */
3198 if (!h->needs_plt
3199 && h->type != STT_GNU_IFUNC
3200 && (h->def_regular
3201 || !h->def_dynamic
3202 || (!h->ref_regular
3203 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3204 {
3205 h->plt = elf_hash_table (eif->info)->init_plt_offset;
3206 return true;
3207 }
3208
3209 /* If we've already adjusted this symbol, don't do it again. This
3210 can happen via a recursive call. */
3211 if (h->dynamic_adjusted)
3212 return true;
3213
3214 /* Don't look at this symbol again. Note that we must set this
3215 after checking the above conditions, because we may look at a
3216 symbol once, decide not to do anything, and then get called
3217 recursively later after REF_REGULAR is set below. */
3218 h->dynamic_adjusted = 1;
3219
3220 /* If this is a weak definition, and we know a real definition, and
3221 the real symbol is not itself defined by a regular object file,
3222 then get a good value for the real definition. We handle the
3223 real symbol first, for the convenience of the backend routine.
3224
3225 Note that there is a confusing case here. If the real definition
3226 is defined by a regular object file, we don't get the real symbol
3227 from the dynamic object, but we do get the weak symbol. If the
3228 processor backend uses a COPY reloc, then if some routine in the
3229 dynamic object changes the real symbol, we will not see that
3230 change in the corresponding weak symbol. This is the way other
3231 ELF linkers work as well, and seems to be a result of the shared
3232 library model.
3233
3234 I will clarify this issue. Most SVR4 shared libraries define the
3235 variable _timezone and define timezone as a weak synonym. The
3236 tzset call changes _timezone. If you write
3237 extern int timezone;
3238 int _timezone = 5;
3239 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3240 you might expect that, since timezone is a synonym for _timezone,
3241 the same number will print both times. However, if the processor
3242 backend uses a COPY reloc, then actually timezone will be copied
3243 into your process image, and, since you define _timezone
3244 yourself, _timezone will not. Thus timezone and _timezone will
3245 wind up at different memory locations. The tzset call will set
3246 _timezone, leaving timezone unchanged. */
3247
3248 if (h->is_weakalias)
3249 {
3250 struct elf_link_hash_entry *def = weakdef (h);
3251
3252 /* If we get to this point, there is an implicit reference to
3253 the alias by a regular object file via the weak symbol H. */
3254 def->ref_regular = 1;
3255
3256 /* Ensure that the backend adjust_dynamic_symbol function sees
3257 the strong alias before H by recursively calling ourselves. */
3258 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3259 return false;
3260 }
3261
3262 /* If a symbol has no type and no size and does not require a PLT
3263 entry, then we are probably about to do the wrong thing here: we
3264 are probably going to create a COPY reloc for an empty object.
3265 This case can arise when a shared object is built with assembly
3266 code, and the assembly code fails to set the symbol type. */
3267 if (h->size == 0
3268 && h->type == STT_NOTYPE
3269 && !h->needs_plt)
3270 _bfd_error_handler
3271 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3272 h->root.root.string);
3273
3274 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3275 {
3276 eif->failed = true;
3277 return false;
3278 }
3279
3280 return true;
3281 }
3282
3283 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3284 DYNBSS. */
3285
3286 bool
_bfd_elf_adjust_dynamic_copy(struct bfd_link_info * info,struct elf_link_hash_entry * h,asection * dynbss)3287 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3288 struct elf_link_hash_entry *h,
3289 asection *dynbss)
3290 {
3291 unsigned int power_of_two;
3292 bfd_vma mask;
3293 asection *sec = h->root.u.def.section;
3294
3295 /* The section alignment of the definition is the maximum alignment
3296 requirement of symbols defined in the section. Since we don't
3297 know the symbol alignment requirement, we start with the
3298 maximum alignment and check low bits of the symbol address
3299 for the minimum alignment. */
3300 power_of_two = bfd_section_alignment (sec);
3301 mask = ((bfd_vma) 1 << power_of_two) - 1;
3302 while ((h->root.u.def.value & mask) != 0)
3303 {
3304 mask >>= 1;
3305 --power_of_two;
3306 }
3307
3308 if (power_of_two > bfd_section_alignment (dynbss))
3309 {
3310 /* Adjust the section alignment if needed. */
3311 if (!bfd_set_section_alignment (dynbss, power_of_two))
3312 return false;
3313 }
3314
3315 /* We make sure that the symbol will be aligned properly. */
3316 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3317
3318 /* Define the symbol as being at this point in DYNBSS. */
3319 h->root.u.def.section = dynbss;
3320 h->root.u.def.value = dynbss->size;
3321
3322 /* Increment the size of DYNBSS to make room for the symbol. */
3323 dynbss->size += h->size;
3324
3325 /* No error if extern_protected_data is true. */
3326 if (h->protected_def
3327 && (!info->extern_protected_data
3328 || (info->extern_protected_data < 0
3329 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3330 info->callbacks->einfo
3331 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3332 h->root.root.string);
3333
3334 return true;
3335 }
3336
3337 /* Adjust all external symbols pointing into SEC_MERGE sections
3338 to reflect the object merging within the sections. */
3339
3340 static bool
_bfd_elf_link_sec_merge_syms(struct elf_link_hash_entry * h,void * data)3341 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3342 {
3343 asection *sec;
3344
3345 if ((h->root.type == bfd_link_hash_defined
3346 || h->root.type == bfd_link_hash_defweak)
3347 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3348 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3349 {
3350 bfd *output_bfd = (bfd *) data;
3351
3352 h->root.u.def.value =
3353 _bfd_merged_section_offset (output_bfd,
3354 &h->root.u.def.section,
3355 elf_section_data (sec)->sec_info,
3356 h->root.u.def.value);
3357 }
3358
3359 return true;
3360 }
3361
3362 /* Returns false if the symbol referred to by H should be considered
3363 to resolve local to the current module, and true if it should be
3364 considered to bind dynamically. */
3365
3366 bool
_bfd_elf_dynamic_symbol_p(struct elf_link_hash_entry * h,struct bfd_link_info * info,bool not_local_protected)3367 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3368 struct bfd_link_info *info,
3369 bool not_local_protected)
3370 {
3371 bool binding_stays_local_p;
3372 const struct elf_backend_data *bed;
3373 struct elf_link_hash_table *hash_table;
3374
3375 if (h == NULL)
3376 return false;
3377
3378 while (h->root.type == bfd_link_hash_indirect
3379 || h->root.type == bfd_link_hash_warning)
3380 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3381
3382 /* If it was forced local, then clearly it's not dynamic. */
3383 if (h->dynindx == -1)
3384 return false;
3385 if (h->forced_local)
3386 return false;
3387
3388 /* Identify the cases where name binding rules say that a
3389 visible symbol resolves locally. */
3390 binding_stays_local_p = (bfd_link_executable (info)
3391 || SYMBOLIC_BIND (info, h));
3392
3393 switch (ELF_ST_VISIBILITY (h->other))
3394 {
3395 case STV_INTERNAL:
3396 case STV_HIDDEN:
3397 return false;
3398
3399 case STV_PROTECTED:
3400 hash_table = elf_hash_table (info);
3401 if (!is_elf_hash_table (&hash_table->root))
3402 return false;
3403
3404 bed = get_elf_backend_data (hash_table->dynobj);
3405
3406 /* Proper resolution for function pointer equality may require
3407 that these symbols perhaps be resolved dynamically, even though
3408 we should be resolving them to the current module. */
3409 if (!not_local_protected || !bed->is_function_type (h->type))
3410 binding_stays_local_p = true;
3411 break;
3412
3413 default:
3414 break;
3415 }
3416
3417 /* If it isn't defined locally, then clearly it's dynamic. */
3418 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3419 return true;
3420
3421 /* Otherwise, the symbol is dynamic if binding rules don't tell
3422 us that it remains local. */
3423 return !binding_stays_local_p;
3424 }
3425
3426 /* Return true if the symbol referred to by H should be considered
3427 to resolve local to the current module, and false otherwise. Differs
3428 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3429 undefined symbols. The two functions are virtually identical except
3430 for the place where dynindx == -1 is tested. If that test is true,
3431 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3432 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3433 defined symbols.
3434 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3435 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3436 treatment of undefined weak symbols. For those that do not make
3437 undefined weak symbols dynamic, both functions may return false. */
3438
3439 bool
_bfd_elf_symbol_refs_local_p(struct elf_link_hash_entry * h,struct bfd_link_info * info,bool local_protected)3440 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3441 struct bfd_link_info *info,
3442 bool local_protected)
3443 {
3444 const struct elf_backend_data *bed;
3445 struct elf_link_hash_table *hash_table;
3446
3447 /* If it's a local sym, of course we resolve locally. */
3448 if (h == NULL)
3449 return true;
3450
3451 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3452 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3453 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3454 return true;
3455
3456 /* Forced local symbols resolve locally. */
3457 if (h->forced_local)
3458 return true;
3459
3460 /* Common symbols that become definitions don't get the DEF_REGULAR
3461 flag set, so test it first, and don't bail out. */
3462 if (ELF_COMMON_DEF_P (h))
3463 /* Do nothing. */;
3464 /* If we don't have a definition in a regular file, then we can't
3465 resolve locally. The sym is either undefined or dynamic. */
3466 else if (!h->def_regular)
3467 return false;
3468
3469 /* Non-dynamic symbols resolve locally. */
3470 if (h->dynindx == -1)
3471 return true;
3472
3473 /* At this point, we know the symbol is defined and dynamic. In an
3474 executable it must resolve locally, likewise when building symbolic
3475 shared libraries. */
3476 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3477 return true;
3478
3479 /* Now deal with defined dynamic symbols in shared libraries. Ones
3480 with default visibility might not resolve locally. */
3481 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3482 return false;
3483
3484 hash_table = elf_hash_table (info);
3485 if (!is_elf_hash_table (&hash_table->root))
3486 return true;
3487
3488 /* STV_PROTECTED symbols with indirect external access are local. */
3489 if (info->indirect_extern_access > 0)
3490 return true;
3491
3492 bed = get_elf_backend_data (hash_table->dynobj);
3493
3494 /* If extern_protected_data is false, STV_PROTECTED non-function
3495 symbols are local. */
3496 if ((!info->extern_protected_data
3497 || (info->extern_protected_data < 0
3498 && !bed->extern_protected_data))
3499 && !bed->is_function_type (h->type))
3500 return true;
3501
3502 /* Function pointer equality tests may require that STV_PROTECTED
3503 symbols be treated as dynamic symbols. If the address of a
3504 function not defined in an executable is set to that function's
3505 plt entry in the executable, then the address of the function in
3506 a shared library must also be the plt entry in the executable. */
3507 return local_protected;
3508 }
3509
3510 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3511 aligned. Returns the first TLS output section. */
3512
3513 struct bfd_section *
_bfd_elf_tls_setup(bfd * obfd,struct bfd_link_info * info)3514 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3515 {
3516 struct bfd_section *sec, *tls;
3517 unsigned int align = 0;
3518
3519 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3520 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3521 break;
3522 tls = sec;
3523
3524 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3525 if (sec->alignment_power > align)
3526 align = sec->alignment_power;
3527
3528 elf_hash_table (info)->tls_sec = tls;
3529
3530 /* Ensure the alignment of the first section (usually .tdata) is the largest
3531 alignment, so that the tls segment starts aligned. */
3532 if (tls != NULL)
3533 tls->alignment_power = align;
3534
3535 return tls;
3536 }
3537
3538 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3539 static bool
is_global_data_symbol_definition(bfd * abfd ATTRIBUTE_UNUSED,Elf_Internal_Sym * sym)3540 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3541 Elf_Internal_Sym *sym)
3542 {
3543 const struct elf_backend_data *bed;
3544
3545 /* Local symbols do not count, but target specific ones might. */
3546 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3547 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3548 return false;
3549
3550 bed = get_elf_backend_data (abfd);
3551 /* Function symbols do not count. */
3552 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3553 return false;
3554
3555 /* If the section is undefined, then so is the symbol. */
3556 if (sym->st_shndx == SHN_UNDEF)
3557 return false;
3558
3559 /* If the symbol is defined in the common section, then
3560 it is a common definition and so does not count. */
3561 if (bed->common_definition (sym))
3562 return false;
3563
3564 /* If the symbol is in a target specific section then we
3565 must rely upon the backend to tell us what it is. */
3566 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3567 /* FIXME - this function is not coded yet:
3568
3569 return _bfd_is_global_symbol_definition (abfd, sym);
3570
3571 Instead for now assume that the definition is not global,
3572 Even if this is wrong, at least the linker will behave
3573 in the same way that it used to do. */
3574 return false;
3575
3576 return true;
3577 }
3578
3579 /* Search the symbol table of the archive element of the archive ABFD
3580 whose archive map contains a mention of SYMDEF, and determine if
3581 the symbol is defined in this element. */
3582 static bool
elf_link_is_defined_archive_symbol(bfd * abfd,carsym * symdef)3583 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3584 {
3585 Elf_Internal_Shdr * hdr;
3586 size_t symcount;
3587 size_t extsymcount;
3588 size_t extsymoff;
3589 Elf_Internal_Sym *isymbuf;
3590 Elf_Internal_Sym *isym;
3591 Elf_Internal_Sym *isymend;
3592 bool result;
3593
3594 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset, NULL);
3595 if (abfd == NULL)
3596 return false;
3597
3598 if (! bfd_check_format (abfd, bfd_object))
3599 return false;
3600
3601 /* Select the appropriate symbol table. If we don't know if the
3602 object file is an IR object, give linker LTO plugin a chance to
3603 get the correct symbol table. */
3604 if (abfd->plugin_format == bfd_plugin_yes
3605 #if BFD_SUPPORTS_PLUGINS
3606 || (abfd->plugin_format == bfd_plugin_unknown
3607 && bfd_link_plugin_object_p (abfd))
3608 #endif
3609 )
3610 {
3611 /* Use the IR symbol table if the object has been claimed by
3612 plugin. */
3613 abfd = abfd->plugin_dummy_bfd;
3614 hdr = &elf_tdata (abfd)->symtab_hdr;
3615 }
3616 else
3617 {
3618 if (elf_use_dt_symtab_p (abfd))
3619 {
3620 bfd_set_error (bfd_error_wrong_format);
3621 return false;
3622 }
3623
3624 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3625 hdr = &elf_tdata (abfd)->symtab_hdr;
3626 else
3627 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3628 }
3629
3630 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3631
3632 /* The sh_info field of the symtab header tells us where the
3633 external symbols start. We don't care about the local symbols. */
3634 if (elf_bad_symtab (abfd))
3635 {
3636 extsymcount = symcount;
3637 extsymoff = 0;
3638 }
3639 else
3640 {
3641 extsymcount = symcount - hdr->sh_info;
3642 extsymoff = hdr->sh_info;
3643 }
3644
3645 if (extsymcount == 0)
3646 return false;
3647
3648 /* Read in the symbol table. */
3649 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3650 NULL, NULL, NULL);
3651 if (isymbuf == NULL)
3652 return false;
3653
3654 /* Scan the symbol table looking for SYMDEF. */
3655 result = false;
3656 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3657 {
3658 const char *name;
3659
3660 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3661 isym->st_name);
3662 if (name == NULL)
3663 break;
3664
3665 if (strcmp (name, symdef->name) == 0)
3666 {
3667 result = is_global_data_symbol_definition (abfd, isym);
3668 break;
3669 }
3670 }
3671
3672 free (isymbuf);
3673
3674 return result;
3675 }
3676
3677 /* Add an entry to the .dynamic table. */
3678
3679 bool
_bfd_elf_add_dynamic_entry(struct bfd_link_info * info,bfd_vma tag,bfd_vma val)3680 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3681 bfd_vma tag,
3682 bfd_vma val)
3683 {
3684 struct elf_link_hash_table *hash_table;
3685 const struct elf_backend_data *bed;
3686 asection *s;
3687 bfd_size_type newsize;
3688 bfd_byte *newcontents;
3689 Elf_Internal_Dyn dyn;
3690
3691 hash_table = elf_hash_table (info);
3692 if (! is_elf_hash_table (&hash_table->root))
3693 return false;
3694
3695 if (tag == DT_RELA || tag == DT_REL)
3696 hash_table->dynamic_relocs = true;
3697
3698 bed = get_elf_backend_data (hash_table->dynobj);
3699 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3700 BFD_ASSERT (s != NULL);
3701
3702 newsize = s->size + bed->s->sizeof_dyn;
3703 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3704 if (newcontents == NULL)
3705 return false;
3706
3707 dyn.d_tag = tag;
3708 dyn.d_un.d_val = val;
3709 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3710
3711 s->size = newsize;
3712 s->contents = newcontents;
3713
3714 return true;
3715 }
3716
3717 /* Strip zero-sized dynamic sections. */
3718
3719 bool
_bfd_elf_strip_zero_sized_dynamic_sections(struct bfd_link_info * info)3720 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3721 {
3722 struct elf_link_hash_table *hash_table;
3723 const struct elf_backend_data *bed;
3724 asection *s, *sdynamic, **pp;
3725 asection *rela_dyn, *rel_dyn;
3726 Elf_Internal_Dyn dyn;
3727 bfd_byte *extdyn, *next;
3728 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3729 bool strip_zero_sized;
3730 bool strip_zero_sized_plt;
3731
3732 if (bfd_link_relocatable (info))
3733 return true;
3734
3735 hash_table = elf_hash_table (info);
3736 if (!is_elf_hash_table (&hash_table->root))
3737 return false;
3738
3739 if (!hash_table->dynobj)
3740 return true;
3741
3742 sdynamic= bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3743 if (!sdynamic)
3744 return true;
3745
3746 bed = get_elf_backend_data (hash_table->dynobj);
3747 swap_dyn_in = bed->s->swap_dyn_in;
3748
3749 strip_zero_sized = false;
3750 strip_zero_sized_plt = false;
3751
3752 /* Strip zero-sized dynamic sections. */
3753 rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3754 rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3755 for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3756 if (s->size == 0
3757 && (s == rela_dyn
3758 || s == rel_dyn
3759 || s == hash_table->srelplt->output_section
3760 || s == hash_table->splt->output_section))
3761 {
3762 *pp = s->next;
3763 info->output_bfd->section_count--;
3764 strip_zero_sized = true;
3765 if (s == rela_dyn)
3766 s = rela_dyn;
3767 if (s == rel_dyn)
3768 s = rel_dyn;
3769 else if (s == hash_table->splt->output_section)
3770 {
3771 s = hash_table->splt;
3772 strip_zero_sized_plt = true;
3773 }
3774 else
3775 s = hash_table->srelplt;
3776 s->flags |= SEC_EXCLUDE;
3777 s->output_section = bfd_abs_section_ptr;
3778 }
3779 else
3780 pp = &s->next;
3781
3782 if (strip_zero_sized_plt && sdynamic->size != 0)
3783 for (extdyn = sdynamic->contents;
3784 extdyn < sdynamic->contents + sdynamic->size;
3785 extdyn = next)
3786 {
3787 next = extdyn + bed->s->sizeof_dyn;
3788 swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3789 switch (dyn.d_tag)
3790 {
3791 default:
3792 break;
3793 case DT_JMPREL:
3794 case DT_PLTRELSZ:
3795 case DT_PLTREL:
3796 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3797 the procedure linkage table (the .plt section) has been
3798 removed. */
3799 memmove (extdyn, next,
3800 sdynamic->size - (next - sdynamic->contents));
3801 next = extdyn;
3802 }
3803 }
3804
3805 if (strip_zero_sized)
3806 {
3807 /* Regenerate program headers. */
3808 elf_seg_map (info->output_bfd) = NULL;
3809 return _bfd_elf_map_sections_to_segments (info->output_bfd, info,
3810 NULL);
3811 }
3812
3813 return true;
3814 }
3815
3816 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3817 1 if a DT_NEEDED tag already exists, and 0 on success. */
3818
3819 int
bfd_elf_add_dt_needed_tag(bfd * abfd,struct bfd_link_info * info)3820 bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
3821 {
3822 struct elf_link_hash_table *hash_table;
3823 size_t strindex;
3824 const char *soname;
3825
3826 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3827 return -1;
3828
3829 hash_table = elf_hash_table (info);
3830 soname = elf_dt_name (abfd);
3831 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, false);
3832 if (strindex == (size_t) -1)
3833 return -1;
3834
3835 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3836 {
3837 asection *sdyn;
3838 const struct elf_backend_data *bed;
3839 bfd_byte *extdyn;
3840
3841 bed = get_elf_backend_data (hash_table->dynobj);
3842 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3843 if (sdyn != NULL && sdyn->size != 0)
3844 for (extdyn = sdyn->contents;
3845 extdyn < sdyn->contents + sdyn->size;
3846 extdyn += bed->s->sizeof_dyn)
3847 {
3848 Elf_Internal_Dyn dyn;
3849
3850 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3851 if (dyn.d_tag == DT_NEEDED
3852 && dyn.d_un.d_val == strindex)
3853 {
3854 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3855 return 1;
3856 }
3857 }
3858 }
3859
3860 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3861 return -1;
3862
3863 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3864 return -1;
3865
3866 return 0;
3867 }
3868
3869 /* Return true if SONAME is on the needed list between NEEDED and STOP
3870 (or the end of list if STOP is NULL), and needed by a library that
3871 will be loaded. */
3872
3873 static bool
on_needed_list(const char * soname,struct bfd_link_needed_list * needed,struct bfd_link_needed_list * stop)3874 on_needed_list (const char *soname,
3875 struct bfd_link_needed_list *needed,
3876 struct bfd_link_needed_list *stop)
3877 {
3878 struct bfd_link_needed_list *look;
3879 for (look = needed; look != stop; look = look->next)
3880 if (strcmp (soname, look->name) == 0
3881 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3882 /* If needed by a library that itself is not directly
3883 needed, recursively check whether that library is
3884 indirectly needed. Since we add DT_NEEDED entries to
3885 the end of the list, library dependencies appear after
3886 the library. Therefore search prior to the current
3887 LOOK, preventing possible infinite recursion. */
3888 || on_needed_list (elf_dt_name (look->by), needed, look)))
3889 return true;
3890
3891 return false;
3892 }
3893
3894 /* Sort symbol by value, section, size, and type. */
3895 static int
elf_sort_symbol(const void * arg1,const void * arg2)3896 elf_sort_symbol (const void *arg1, const void *arg2)
3897 {
3898 const struct elf_link_hash_entry *h1;
3899 const struct elf_link_hash_entry *h2;
3900 bfd_signed_vma vdiff;
3901 int sdiff;
3902 const char *n1;
3903 const char *n2;
3904
3905 h1 = *(const struct elf_link_hash_entry **) arg1;
3906 h2 = *(const struct elf_link_hash_entry **) arg2;
3907 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3908 if (vdiff != 0)
3909 return vdiff > 0 ? 1 : -1;
3910
3911 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3912 if (sdiff != 0)
3913 return sdiff;
3914
3915 /* Sort so that sized symbols are selected over zero size symbols. */
3916 vdiff = h1->size - h2->size;
3917 if (vdiff != 0)
3918 return vdiff > 0 ? 1 : -1;
3919
3920 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3921 if (h1->type != h2->type)
3922 return h1->type - h2->type;
3923
3924 /* If symbols are properly sized and typed, and multiple strong
3925 aliases are not defined in a shared library by the user we
3926 shouldn't get here. Unfortunately linker script symbols like
3927 __bss_start sometimes match a user symbol defined at the start of
3928 .bss without proper size and type. We'd like to preference the
3929 user symbol over reserved system symbols. Sort on leading
3930 underscores. */
3931 n1 = h1->root.root.string;
3932 n2 = h2->root.root.string;
3933 while (*n1 == *n2)
3934 {
3935 if (*n1 == 0)
3936 break;
3937 ++n1;
3938 ++n2;
3939 }
3940 if (*n1 == '_')
3941 return -1;
3942 if (*n2 == '_')
3943 return 1;
3944
3945 /* Final sort on name selects user symbols like '_u' over reserved
3946 system symbols like '_Z' and also will avoid qsort instability. */
3947 return *n1 - *n2;
3948 }
3949
3950 /* This function is used to adjust offsets into .dynstr for
3951 dynamic symbols. This is called via elf_link_hash_traverse. */
3952
3953 static bool
elf_adjust_dynstr_offsets(struct elf_link_hash_entry * h,void * data)3954 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3955 {
3956 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3957
3958 if (h->dynindx != -1)
3959 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3960 return true;
3961 }
3962
3963 /* Assign string offsets in .dynstr, update all structures referencing
3964 them. */
3965
3966 static bool
elf_finalize_dynstr(bfd * output_bfd,struct bfd_link_info * info)3967 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3968 {
3969 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3970 struct elf_link_local_dynamic_entry *entry;
3971 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3972 bfd *dynobj = hash_table->dynobj;
3973 asection *sdyn;
3974 bfd_size_type size;
3975 const struct elf_backend_data *bed;
3976 bfd_byte *extdyn;
3977
3978 _bfd_elf_strtab_finalize (dynstr);
3979 size = _bfd_elf_strtab_size (dynstr);
3980
3981 /* Allow the linker to examine the dynsymtab now it's fully populated. */
3982
3983 if (info->callbacks->examine_strtab)
3984 info->callbacks->examine_strtab (dynstr);
3985
3986 bed = get_elf_backend_data (dynobj);
3987 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3988 BFD_ASSERT (sdyn != NULL);
3989
3990 /* Update all .dynamic entries referencing .dynstr strings. */
3991 for (extdyn = sdyn->contents;
3992 extdyn < PTR_ADD (sdyn->contents, sdyn->size);
3993 extdyn += bed->s->sizeof_dyn)
3994 {
3995 Elf_Internal_Dyn dyn;
3996
3997 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3998 switch (dyn.d_tag)
3999 {
4000 case DT_STRSZ:
4001 dyn.d_un.d_val = size;
4002 break;
4003 case DT_NEEDED:
4004 case DT_SONAME:
4005 case DT_RPATH:
4006 case DT_RUNPATH:
4007 case DT_FILTER:
4008 case DT_AUXILIARY:
4009 case DT_AUDIT:
4010 case DT_DEPAUDIT:
4011 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
4012 break;
4013 default:
4014 continue;
4015 }
4016 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
4017 }
4018
4019 /* Now update local dynamic symbols. */
4020 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
4021 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
4022 entry->isym.st_name);
4023
4024 /* And the rest of dynamic symbols. */
4025 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
4026
4027 /* Adjust version definitions. */
4028 if (elf_tdata (output_bfd)->cverdefs)
4029 {
4030 asection *s;
4031 bfd_byte *p;
4032 size_t i;
4033 Elf_Internal_Verdef def;
4034 Elf_Internal_Verdaux defaux;
4035
4036 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
4037 p = s->contents;
4038 do
4039 {
4040 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
4041 &def);
4042 p += sizeof (Elf_External_Verdef);
4043 if (def.vd_aux != sizeof (Elf_External_Verdef))
4044 continue;
4045 for (i = 0; i < def.vd_cnt; ++i)
4046 {
4047 _bfd_elf_swap_verdaux_in (output_bfd,
4048 (Elf_External_Verdaux *) p, &defaux);
4049 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
4050 defaux.vda_name);
4051 _bfd_elf_swap_verdaux_out (output_bfd,
4052 &defaux, (Elf_External_Verdaux *) p);
4053 p += sizeof (Elf_External_Verdaux);
4054 }
4055 }
4056 while (def.vd_next);
4057 }
4058
4059 /* Adjust version references. */
4060 if (elf_tdata (output_bfd)->verref)
4061 {
4062 asection *s;
4063 bfd_byte *p;
4064 size_t i;
4065 Elf_Internal_Verneed need;
4066 Elf_Internal_Vernaux needaux;
4067
4068 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
4069 p = s->contents;
4070 do
4071 {
4072 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
4073 &need);
4074 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
4075 _bfd_elf_swap_verneed_out (output_bfd, &need,
4076 (Elf_External_Verneed *) p);
4077 p += sizeof (Elf_External_Verneed);
4078 for (i = 0; i < need.vn_cnt; ++i)
4079 {
4080 _bfd_elf_swap_vernaux_in (output_bfd,
4081 (Elf_External_Vernaux *) p, &needaux);
4082 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
4083 needaux.vna_name);
4084 _bfd_elf_swap_vernaux_out (output_bfd,
4085 &needaux,
4086 (Elf_External_Vernaux *) p);
4087 p += sizeof (Elf_External_Vernaux);
4088 }
4089 }
4090 while (need.vn_next);
4091 }
4092
4093 return true;
4094 }
4095
4096 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4097 The default is to only match when the INPUT and OUTPUT are exactly
4098 the same target. */
4099
4100 bool
_bfd_elf_default_relocs_compatible(const bfd_target * input,const bfd_target * output)4101 _bfd_elf_default_relocs_compatible (const bfd_target *input,
4102 const bfd_target *output)
4103 {
4104 return input == output;
4105 }
4106
4107 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4108 This version is used when different targets for the same architecture
4109 are virtually identical. */
4110
4111 bool
_bfd_elf_relocs_compatible(const bfd_target * input,const bfd_target * output)4112 _bfd_elf_relocs_compatible (const bfd_target *input,
4113 const bfd_target *output)
4114 {
4115 const struct elf_backend_data *obed, *ibed;
4116
4117 if (input == output)
4118 return true;
4119
4120 ibed = xvec_get_elf_backend_data (input);
4121 obed = xvec_get_elf_backend_data (output);
4122
4123 if (ibed->arch != obed->arch)
4124 return false;
4125
4126 /* If both backends are using this function, deem them compatible. */
4127 return ibed->relocs_compatible == obed->relocs_compatible;
4128 }
4129
4130 /* Make a special call to the linker "notice" function to tell it that
4131 we are about to handle an as-needed lib, or have finished
4132 processing the lib. */
4133
4134 bool
_bfd_elf_notice_as_needed(bfd * ibfd,struct bfd_link_info * info,enum notice_asneeded_action act)4135 _bfd_elf_notice_as_needed (bfd *ibfd,
4136 struct bfd_link_info *info,
4137 enum notice_asneeded_action act)
4138 {
4139 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
4140 }
4141
4142 /* Call ACTION on each relocation in an ELF object file. */
4143
4144 bool
_bfd_elf_link_iterate_on_relocs(bfd * abfd,struct bfd_link_info * info,bool (* action)(bfd *,struct bfd_link_info *,asection *,const Elf_Internal_Rela *))4145 _bfd_elf_link_iterate_on_relocs
4146 (bfd *abfd, struct bfd_link_info *info,
4147 bool (*action) (bfd *, struct bfd_link_info *, asection *,
4148 const Elf_Internal_Rela *))
4149 {
4150 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4151 struct elf_link_hash_table *htab = elf_hash_table (info);
4152
4153 /* If this object is the same format as the output object, and it is
4154 not a shared library, then let the backend look through the
4155 relocs.
4156
4157 This is required to build global offset table entries and to
4158 arrange for dynamic relocs. It is not required for the
4159 particular common case of linking non PIC code, even when linking
4160 against shared libraries, but unfortunately there is no way of
4161 knowing whether an object file has been compiled PIC or not.
4162 Looking through the relocs is not particularly time consuming.
4163 The problem is that we must either (1) keep the relocs in memory,
4164 which causes the linker to require additional runtime memory or
4165 (2) read the relocs twice from the input file, which wastes time.
4166 This would be a good case for using mmap.
4167
4168 I have no idea how to handle linking PIC code into a file of a
4169 different format. It probably can't be done. */
4170 if ((abfd->flags & DYNAMIC) == 0
4171 && is_elf_hash_table (&htab->root)
4172 && elf_object_id (abfd) == elf_hash_table_id (htab)
4173 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4174 {
4175 asection *o;
4176
4177 for (o = abfd->sections; o != NULL; o = o->next)
4178 {
4179 Elf_Internal_Rela *internal_relocs;
4180 bool ok;
4181
4182 /* Don't check relocations in excluded sections. Don't do
4183 anything special with non-loaded, non-alloced sections.
4184 In particular, any relocs in such sections should not
4185 affect GOT and PLT reference counting (ie. we don't
4186 allow them to create GOT or PLT entries), there's no
4187 possibility or desire to optimize TLS relocs, and
4188 there's not much point in propagating relocs to shared
4189 libs that the dynamic linker won't relocate. */
4190 if ((o->flags & SEC_ALLOC) == 0
4191 || (o->flags & SEC_RELOC) == 0
4192 || (o->flags & SEC_EXCLUDE) != 0
4193 || o->reloc_count == 0
4194 || ((info->strip == strip_all || info->strip == strip_debugger)
4195 && (o->flags & SEC_DEBUGGING) != 0)
4196 || bfd_is_abs_section (o->output_section))
4197 continue;
4198
4199 internal_relocs = _bfd_elf_link_info_read_relocs (abfd, info,
4200 o, NULL,
4201 NULL,
4202 _bfd_link_keep_memory (info));
4203 if (internal_relocs == NULL)
4204 return false;
4205
4206 ok = action (abfd, info, o, internal_relocs);
4207
4208 if (elf_section_data (o)->relocs != internal_relocs)
4209 free (internal_relocs);
4210
4211 if (! ok)
4212 return false;
4213 }
4214 }
4215
4216 return true;
4217 }
4218
4219 /* Check relocations in an ELF object file. This is called after
4220 all input files have been opened. */
4221
4222 bool
_bfd_elf_link_check_relocs(bfd * abfd,struct bfd_link_info * info)4223 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
4224 {
4225 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4226 if (bed->check_relocs != NULL)
4227 return _bfd_elf_link_iterate_on_relocs (abfd, info,
4228 bed->check_relocs);
4229 return true;
4230 }
4231
4232 /* Add symbols from an ELF object file to the linker hash table. */
4233
4234 static bool
elf_link_add_object_symbols(bfd * abfd,struct bfd_link_info * info)4235 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4236 {
4237 Elf_Internal_Ehdr *ehdr;
4238 Elf_Internal_Shdr *hdr;
4239 size_t symcount;
4240 size_t extsymcount;
4241 size_t extsymoff;
4242 struct elf_link_hash_entry **sym_hash;
4243 bool dynamic;
4244 Elf_External_Versym *extversym = NULL;
4245 Elf_External_Versym *extversym_end = NULL;
4246 Elf_External_Versym *ever;
4247 struct elf_link_hash_entry *weaks;
4248 struct elf_link_hash_entry **nondeflt_vers = NULL;
4249 size_t nondeflt_vers_cnt = 0;
4250 Elf_Internal_Sym *isymbuf = NULL;
4251 Elf_Internal_Sym *isym;
4252 Elf_Internal_Sym *isymend;
4253 const struct elf_backend_data *bed;
4254 bool add_needed;
4255 struct elf_link_hash_table *htab;
4256 void *alloc_mark = NULL;
4257 struct bfd_hash_entry **old_table = NULL;
4258 unsigned int old_size = 0;
4259 unsigned int old_count = 0;
4260 void *old_tab = NULL;
4261 void *old_ent;
4262 struct bfd_link_hash_entry *old_undefs = NULL;
4263 struct bfd_link_hash_entry *old_undefs_tail = NULL;
4264 void *old_strtab = NULL;
4265 size_t tabsize = 0;
4266 asection *s;
4267 bool just_syms;
4268
4269 htab = elf_hash_table (info);
4270 bed = get_elf_backend_data (abfd);
4271
4272 if (elf_use_dt_symtab_p (abfd))
4273 {
4274 bfd_set_error (bfd_error_wrong_format);
4275 return false;
4276 }
4277
4278 if ((abfd->flags & DYNAMIC) == 0)
4279 dynamic = false;
4280 else
4281 {
4282 dynamic = true;
4283
4284 /* You can't use -r against a dynamic object. Also, there's no
4285 hope of using a dynamic object which does not exactly match
4286 the format of the output file. */
4287 if (bfd_link_relocatable (info)
4288 || !is_elf_hash_table (&htab->root)
4289 || info->output_bfd->xvec != abfd->xvec)
4290 {
4291 if (bfd_link_relocatable (info))
4292 bfd_set_error (bfd_error_invalid_operation);
4293 else
4294 bfd_set_error (bfd_error_wrong_format);
4295 goto error_return;
4296 }
4297 }
4298
4299 ehdr = elf_elfheader (abfd);
4300 if (info->warn_alternate_em
4301 && bed->elf_machine_code != ehdr->e_machine
4302 && ((bed->elf_machine_alt1 != 0
4303 && ehdr->e_machine == bed->elf_machine_alt1)
4304 || (bed->elf_machine_alt2 != 0
4305 && ehdr->e_machine == bed->elf_machine_alt2)))
4306 _bfd_error_handler
4307 /* xgettext:c-format */
4308 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4309 ehdr->e_machine, abfd, bed->elf_machine_code);
4310
4311 /* As a GNU extension, any input sections which are named
4312 .gnu.warning.SYMBOL are treated as warning symbols for the given
4313 symbol. This differs from .gnu.warning sections, which generate
4314 warnings when they are included in an output file. */
4315 /* PR 12761: Also generate this warning when building shared libraries. */
4316 for (s = abfd->sections; s != NULL; s = s->next)
4317 {
4318 const char *name;
4319
4320 name = bfd_section_name (s);
4321 if (startswith (name, ".gnu.warning."))
4322 {
4323 char *msg;
4324 bfd_size_type sz;
4325
4326 name += sizeof ".gnu.warning." - 1;
4327
4328 /* If this is a shared object, then look up the symbol
4329 in the hash table. If it is there, and it is already
4330 been defined, then we will not be using the entry
4331 from this shared object, so we don't need to warn.
4332 FIXME: If we see the definition in a regular object
4333 later on, we will warn, but we shouldn't. The only
4334 fix is to keep track of what warnings we are supposed
4335 to emit, and then handle them all at the end of the
4336 link. */
4337 if (dynamic)
4338 {
4339 struct elf_link_hash_entry *h;
4340
4341 h = elf_link_hash_lookup (htab, name, false, false, true);
4342
4343 /* FIXME: What about bfd_link_hash_common? */
4344 if (h != NULL
4345 && (h->root.type == bfd_link_hash_defined
4346 || h->root.type == bfd_link_hash_defweak))
4347 continue;
4348 }
4349
4350 sz = s->size;
4351 msg = (char *) bfd_alloc (abfd, sz + 1);
4352 if (msg == NULL)
4353 goto error_return;
4354
4355 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4356 goto error_return;
4357
4358 msg[sz] = '\0';
4359
4360 if (! (_bfd_generic_link_add_one_symbol
4361 (info, abfd, name, BSF_WARNING, s, 0, msg,
4362 false, bed->collect, NULL)))
4363 goto error_return;
4364
4365 if (bfd_link_executable (info))
4366 {
4367 /* Clobber the section size so that the warning does
4368 not get copied into the output file. */
4369 s->size = 0;
4370
4371 /* Also set SEC_EXCLUDE, so that symbols defined in
4372 the warning section don't get copied to the output. */
4373 s->flags |= SEC_EXCLUDE;
4374 }
4375 }
4376 }
4377
4378 just_syms = ((s = abfd->sections) != NULL
4379 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4380
4381 add_needed = true;
4382 if (! dynamic)
4383 {
4384 /* If we are creating a shared library, create all the dynamic
4385 sections immediately. We need to attach them to something,
4386 so we attach them to this BFD, provided it is the right
4387 format and is not from ld --just-symbols. Always create the
4388 dynamic sections for -E/--dynamic-list. FIXME: If there
4389 are no input BFD's of the same format as the output, we can't
4390 make a shared library. */
4391 if (!just_syms
4392 && (bfd_link_pic (info)
4393 || (!bfd_link_relocatable (info)
4394 && info->nointerp
4395 && (info->export_dynamic || info->dynamic)))
4396 && is_elf_hash_table (&htab->root)
4397 && info->output_bfd->xvec == abfd->xvec
4398 && !htab->dynamic_sections_created)
4399 {
4400 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4401 goto error_return;
4402 }
4403 }
4404 else if (!is_elf_hash_table (&htab->root))
4405 goto error_return;
4406 else
4407 {
4408 const char *soname = NULL;
4409 char *audit = NULL;
4410 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4411 const Elf_Internal_Phdr *phdr;
4412 struct elf_link_loaded_list *loaded_lib;
4413
4414 /* ld --just-symbols and dynamic objects don't mix very well.
4415 ld shouldn't allow it. */
4416 if (just_syms)
4417 abort ();
4418
4419 /* If this dynamic lib was specified on the command line with
4420 --as-needed in effect, then we don't want to add a DT_NEEDED
4421 tag unless the lib is actually used. Similary for libs brought
4422 in by another lib's DT_NEEDED. When --no-add-needed is used
4423 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4424 any dynamic library in DT_NEEDED tags in the dynamic lib at
4425 all. */
4426 add_needed = (elf_dyn_lib_class (abfd)
4427 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4428 | DYN_NO_NEEDED)) == 0;
4429
4430 s = bfd_get_section_by_name (abfd, ".dynamic");
4431 if (s != NULL && s->size != 0 && (s->flags & SEC_HAS_CONTENTS) != 0)
4432 {
4433 bfd_byte *dynbuf;
4434 bfd_byte *extdyn;
4435 unsigned int elfsec;
4436 unsigned long shlink;
4437
4438 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4439 {
4440 error_free_dyn:
4441 free (dynbuf);
4442 goto error_return;
4443 }
4444
4445 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4446 if (elfsec == SHN_BAD)
4447 goto error_free_dyn;
4448 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4449
4450 for (extdyn = dynbuf;
4451 (size_t) (dynbuf + s->size - extdyn) >= bed->s->sizeof_dyn;
4452 extdyn += bed->s->sizeof_dyn)
4453 {
4454 Elf_Internal_Dyn dyn;
4455
4456 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4457 if (dyn.d_tag == DT_SONAME)
4458 {
4459 unsigned int tagv = dyn.d_un.d_val;
4460 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4461 if (soname == NULL)
4462 goto error_free_dyn;
4463 }
4464 if (dyn.d_tag == DT_NEEDED)
4465 {
4466 struct bfd_link_needed_list *n, **pn;
4467 char *fnm, *anm;
4468 unsigned int tagv = dyn.d_un.d_val;
4469 size_t amt = sizeof (struct bfd_link_needed_list);
4470
4471 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4472 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4473 if (n == NULL || fnm == NULL)
4474 goto error_free_dyn;
4475 amt = strlen (fnm) + 1;
4476 anm = (char *) bfd_alloc (abfd, amt);
4477 if (anm == NULL)
4478 goto error_free_dyn;
4479 memcpy (anm, fnm, amt);
4480 n->name = anm;
4481 n->by = abfd;
4482 n->next = NULL;
4483 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4484 ;
4485 *pn = n;
4486 }
4487 if (dyn.d_tag == DT_RUNPATH)
4488 {
4489 struct bfd_link_needed_list *n, **pn;
4490 char *fnm, *anm;
4491 unsigned int tagv = dyn.d_un.d_val;
4492 size_t amt = sizeof (struct bfd_link_needed_list);
4493
4494 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4495 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4496 if (n == NULL || fnm == NULL)
4497 goto error_free_dyn;
4498 amt = strlen (fnm) + 1;
4499 anm = (char *) bfd_alloc (abfd, amt);
4500 if (anm == NULL)
4501 goto error_free_dyn;
4502 memcpy (anm, fnm, amt);
4503 n->name = anm;
4504 n->by = abfd;
4505 n->next = NULL;
4506 for (pn = & runpath;
4507 *pn != NULL;
4508 pn = &(*pn)->next)
4509 ;
4510 *pn = n;
4511 }
4512 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4513 if (!runpath && dyn.d_tag == DT_RPATH)
4514 {
4515 struct bfd_link_needed_list *n, **pn;
4516 char *fnm, *anm;
4517 unsigned int tagv = dyn.d_un.d_val;
4518 size_t amt = sizeof (struct bfd_link_needed_list);
4519
4520 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4521 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4522 if (n == NULL || fnm == NULL)
4523 goto error_free_dyn;
4524 amt = strlen (fnm) + 1;
4525 anm = (char *) bfd_alloc (abfd, amt);
4526 if (anm == NULL)
4527 goto error_free_dyn;
4528 memcpy (anm, fnm, amt);
4529 n->name = anm;
4530 n->by = abfd;
4531 n->next = NULL;
4532 for (pn = & rpath;
4533 *pn != NULL;
4534 pn = &(*pn)->next)
4535 ;
4536 *pn = n;
4537 }
4538 if (dyn.d_tag == DT_AUDIT)
4539 {
4540 unsigned int tagv = dyn.d_un.d_val;
4541 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4542 }
4543 if (dyn.d_tag == DT_FLAGS_1)
4544 elf_tdata (abfd)->is_pie = (dyn.d_un.d_val & DF_1_PIE) != 0;
4545 }
4546
4547 free (dynbuf);
4548 }
4549
4550 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4551 frees all more recently bfd_alloc'd blocks as well. */
4552 if (runpath)
4553 rpath = runpath;
4554
4555 if (rpath)
4556 {
4557 struct bfd_link_needed_list **pn;
4558 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4559 ;
4560 *pn = rpath;
4561 }
4562
4563 /* If we have a PT_GNU_RELRO program header, mark as read-only
4564 all sections contained fully therein. This makes relro
4565 shared library sections appear as they will at run-time. */
4566 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4567 while (phdr-- > elf_tdata (abfd)->phdr)
4568 if (phdr->p_type == PT_GNU_RELRO)
4569 {
4570 for (s = abfd->sections; s != NULL; s = s->next)
4571 {
4572 unsigned int opb = bfd_octets_per_byte (abfd, s);
4573
4574 if ((s->flags & SEC_ALLOC) != 0
4575 && s->vma * opb >= phdr->p_vaddr
4576 && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4577 s->flags |= SEC_READONLY;
4578 }
4579 break;
4580 }
4581
4582 /* We do not want to include any of the sections in a dynamic
4583 object in the output file. We hack by simply clobbering the
4584 list of sections in the BFD. This could be handled more
4585 cleanly by, say, a new section flag; the existing
4586 SEC_NEVER_LOAD flag is not the one we want, because that one
4587 still implies that the section takes up space in the output
4588 file. */
4589 bfd_section_list_clear (abfd);
4590
4591 /* Find the name to use in a DT_NEEDED entry that refers to this
4592 object. If the object has a DT_SONAME entry, we use it.
4593 Otherwise, if the generic linker stuck something in
4594 elf_dt_name, we use that. Otherwise, we just use the file
4595 name. */
4596 if (soname == NULL || *soname == '\0')
4597 {
4598 soname = elf_dt_name (abfd);
4599 if (soname == NULL || *soname == '\0')
4600 soname = bfd_get_filename (abfd);
4601 }
4602
4603 /* Save the SONAME because sometimes the linker emulation code
4604 will need to know it. */
4605 elf_dt_name (abfd) = soname;
4606
4607 /* If we have already included this dynamic object in the
4608 link, just ignore it. There is no reason to include a
4609 particular dynamic object more than once. */
4610 for (loaded_lib = htab->dyn_loaded;
4611 loaded_lib != NULL;
4612 loaded_lib = loaded_lib->next)
4613 {
4614 if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4615 return true;
4616 }
4617
4618 /* Create dynamic sections for backends that require that be done
4619 before setup_gnu_properties. */
4620 if (add_needed
4621 && !_bfd_elf_link_create_dynamic_sections (abfd, info))
4622 return false;
4623
4624 /* Save the DT_AUDIT entry for the linker emulation code. */
4625 elf_dt_audit (abfd) = audit;
4626 }
4627
4628 /* If this is a dynamic object, we always link against the .dynsym
4629 symbol table, not the .symtab symbol table. The dynamic linker
4630 will only see the .dynsym symbol table, so there is no reason to
4631 look at .symtab for a dynamic object. */
4632
4633 if (! dynamic || elf_dynsymtab (abfd) == 0)
4634 hdr = &elf_tdata (abfd)->symtab_hdr;
4635 else
4636 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4637
4638 symcount = hdr->sh_size / bed->s->sizeof_sym;
4639
4640 /* The sh_info field of the symtab header tells us where the
4641 external symbols start. We don't care about the local symbols at
4642 this point. */
4643 if (elf_bad_symtab (abfd))
4644 {
4645 extsymcount = symcount;
4646 extsymoff = 0;
4647 }
4648 else
4649 {
4650 extsymcount = symcount - hdr->sh_info;
4651 extsymoff = hdr->sh_info;
4652 }
4653
4654 sym_hash = elf_sym_hashes (abfd);
4655 if (extsymcount != 0)
4656 {
4657 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4658 NULL, NULL, NULL);
4659 if (isymbuf == NULL)
4660 goto error_return;
4661
4662 if (sym_hash == NULL)
4663 {
4664 /* We store a pointer to the hash table entry for each
4665 external symbol. */
4666 size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4667 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4668 if (sym_hash == NULL)
4669 goto error_free_sym;
4670 elf_sym_hashes (abfd) = sym_hash;
4671 }
4672 }
4673
4674 if (dynamic)
4675 {
4676 /* Read in any version definitions. */
4677 if (!_bfd_elf_slurp_version_tables (abfd,
4678 info->default_imported_symver))
4679 goto error_free_sym;
4680
4681 /* Read in the symbol versions, but don't bother to convert them
4682 to internal format. */
4683 if (elf_dynversym (abfd) != 0)
4684 {
4685 Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4686 bfd_size_type amt = versymhdr->sh_size;
4687
4688 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4689 goto error_free_sym;
4690 extversym = (Elf_External_Versym *)
4691 _bfd_malloc_and_read (abfd, amt, amt);
4692 if (extversym == NULL)
4693 goto error_free_sym;
4694 extversym_end = extversym + amt / sizeof (*extversym);
4695 }
4696 }
4697
4698 /* If we are loading an as-needed shared lib, save the symbol table
4699 state before we start adding symbols. If the lib turns out
4700 to be unneeded, restore the state. */
4701 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4702 {
4703 unsigned int i;
4704 size_t entsize;
4705
4706 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4707 {
4708 struct bfd_hash_entry *p;
4709 struct elf_link_hash_entry *h;
4710
4711 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4712 {
4713 h = (struct elf_link_hash_entry *) p;
4714 entsize += htab->root.table.entsize;
4715 if (h->root.type == bfd_link_hash_warning)
4716 {
4717 entsize += htab->root.table.entsize;
4718 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4719 }
4720 if (h->root.type == bfd_link_hash_common)
4721 entsize += sizeof (*h->root.u.c.p);
4722 }
4723 }
4724
4725 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4726 old_tab = bfd_malloc (tabsize + entsize);
4727 if (old_tab == NULL)
4728 goto error_free_vers;
4729
4730 /* Remember the current objalloc pointer, so that all mem for
4731 symbols added can later be reclaimed. */
4732 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4733 if (alloc_mark == NULL)
4734 goto error_free_vers;
4735
4736 /* Make a special call to the linker "notice" function to
4737 tell it that we are about to handle an as-needed lib. */
4738 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4739 goto error_free_vers;
4740
4741 /* Clone the symbol table. Remember some pointers into the
4742 symbol table, and dynamic symbol count. */
4743 old_ent = (char *) old_tab + tabsize;
4744 memcpy (old_tab, htab->root.table.table, tabsize);
4745 old_undefs = htab->root.undefs;
4746 old_undefs_tail = htab->root.undefs_tail;
4747 old_table = htab->root.table.table;
4748 old_size = htab->root.table.size;
4749 old_count = htab->root.table.count;
4750 old_strtab = NULL;
4751 if (htab->dynstr != NULL)
4752 {
4753 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4754 if (old_strtab == NULL)
4755 goto error_free_vers;
4756 }
4757
4758 for (i = 0; i < htab->root.table.size; i++)
4759 {
4760 struct bfd_hash_entry *p;
4761 struct elf_link_hash_entry *h;
4762
4763 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4764 {
4765 h = (struct elf_link_hash_entry *) p;
4766 memcpy (old_ent, h, htab->root.table.entsize);
4767 old_ent = (char *) old_ent + htab->root.table.entsize;
4768 if (h->root.type == bfd_link_hash_warning)
4769 {
4770 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4771 memcpy (old_ent, h, htab->root.table.entsize);
4772 old_ent = (char *) old_ent + htab->root.table.entsize;
4773 }
4774 if (h->root.type == bfd_link_hash_common)
4775 {
4776 memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
4777 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
4778 }
4779 }
4780 }
4781 }
4782
4783 weaks = NULL;
4784 if (extversym == NULL)
4785 ever = NULL;
4786 else if (extversym + extsymoff < extversym_end)
4787 ever = extversym + extsymoff;
4788 else
4789 {
4790 /* xgettext:c-format */
4791 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4792 abfd, (long) extsymoff,
4793 (long) (extversym_end - extversym) / sizeof (* extversym));
4794 bfd_set_error (bfd_error_bad_value);
4795 goto error_free_vers;
4796 }
4797
4798 if (!bfd_link_relocatable (info)
4799 && abfd->lto_slim_object)
4800 {
4801 _bfd_error_handler
4802 (_("%pB: plugin needed to handle lto object"), abfd);
4803 }
4804
4805 for (isym = isymbuf, isymend = PTR_ADD (isymbuf, extsymcount);
4806 isym < isymend;
4807 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4808 {
4809 int bind;
4810 bfd_vma value;
4811 asection *sec, *new_sec;
4812 flagword flags;
4813 const char *name;
4814 struct elf_link_hash_entry *h;
4815 struct elf_link_hash_entry *hi;
4816 bool definition;
4817 bool size_change_ok;
4818 bool type_change_ok;
4819 bool new_weak;
4820 bool old_weak;
4821 bfd *override;
4822 bool common;
4823 bool discarded;
4824 unsigned int old_alignment;
4825 unsigned int shindex;
4826 bfd *old_bfd;
4827 bool matched;
4828
4829 override = NULL;
4830
4831 flags = BSF_NO_FLAGS;
4832 sec = NULL;
4833 value = isym->st_value;
4834 common = bed->common_definition (isym);
4835 if (common && info->inhibit_common_definition)
4836 {
4837 /* Treat common symbol as undefined for --no-define-common. */
4838 isym->st_shndx = SHN_UNDEF;
4839 common = false;
4840 }
4841 discarded = false;
4842
4843 bind = ELF_ST_BIND (isym->st_info);
4844 switch (bind)
4845 {
4846 case STB_LOCAL:
4847 /* This should be impossible, since ELF requires that all
4848 global symbols follow all local symbols, and that sh_info
4849 point to the first global symbol. Unfortunately, Irix 5
4850 screws this up. */
4851 if (elf_bad_symtab (abfd))
4852 continue;
4853
4854 /* If we aren't prepared to handle locals within the globals
4855 then we'll likely segfault on a NULL symbol hash if the
4856 symbol is ever referenced in relocations. */
4857 shindex = elf_elfheader (abfd)->e_shstrndx;
4858 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4859 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4860 " (>= sh_info of %lu)"),
4861 abfd, name, (long) (isym - isymbuf + extsymoff),
4862 (long) extsymoff);
4863
4864 /* Dynamic object relocations are not processed by ld, so
4865 ld won't run into the problem mentioned above. */
4866 if (dynamic)
4867 continue;
4868 bfd_set_error (bfd_error_bad_value);
4869 goto error_free_vers;
4870
4871 case STB_GLOBAL:
4872 if (isym->st_shndx != SHN_UNDEF && !common)
4873 flags = BSF_GLOBAL;
4874 break;
4875
4876 case STB_WEAK:
4877 flags = BSF_WEAK;
4878 break;
4879
4880 case STB_GNU_UNIQUE:
4881 flags = BSF_GNU_UNIQUE;
4882 break;
4883
4884 default:
4885 /* Leave it up to the processor backend. */
4886 break;
4887 }
4888
4889 if (isym->st_shndx == SHN_UNDEF)
4890 sec = bfd_und_section_ptr;
4891 else if (isym->st_shndx == SHN_ABS)
4892 sec = bfd_abs_section_ptr;
4893 else if (isym->st_shndx == SHN_COMMON)
4894 {
4895 sec = bfd_com_section_ptr;
4896 /* What ELF calls the size we call the value. What ELF
4897 calls the value we call the alignment. */
4898 value = isym->st_size;
4899 }
4900 else
4901 {
4902 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4903 if (sec == NULL)
4904 sec = bfd_abs_section_ptr;
4905 else if (discarded_section (sec))
4906 {
4907 /* Symbols from discarded section are undefined. We keep
4908 its visibility. */
4909 sec = bfd_und_section_ptr;
4910 discarded = true;
4911 isym->st_shndx = SHN_UNDEF;
4912 }
4913 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4914 value -= sec->vma;
4915 }
4916
4917 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4918 isym->st_name);
4919 if (name == NULL)
4920 goto error_free_vers;
4921
4922 if (isym->st_shndx == SHN_COMMON
4923 && (abfd->flags & BFD_PLUGIN) != 0)
4924 {
4925 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4926
4927 if (xc == NULL)
4928 {
4929 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4930 | SEC_EXCLUDE);
4931 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4932 if (xc == NULL)
4933 goto error_free_vers;
4934 }
4935 sec = xc;
4936 }
4937 else if (isym->st_shndx == SHN_COMMON
4938 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4939 && !bfd_link_relocatable (info))
4940 {
4941 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4942
4943 if (tcomm == NULL)
4944 {
4945 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4946 | SEC_LINKER_CREATED);
4947 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4948 if (tcomm == NULL)
4949 goto error_free_vers;
4950 }
4951 sec = tcomm;
4952 }
4953 else if (bed->elf_add_symbol_hook)
4954 {
4955 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4956 &sec, &value))
4957 goto error_free_vers;
4958
4959 /* The hook function sets the name to NULL if this symbol
4960 should be skipped for some reason. */
4961 if (name == NULL)
4962 continue;
4963 }
4964
4965 /* Sanity check that all possibilities were handled. */
4966 if (sec == NULL)
4967 abort ();
4968
4969 /* Silently discard TLS symbols from --just-syms. There's
4970 no way to combine a static TLS block with a new TLS block
4971 for this executable. */
4972 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4973 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4974 continue;
4975
4976 if (bfd_is_und_section (sec)
4977 || bfd_is_com_section (sec))
4978 definition = false;
4979 else
4980 definition = true;
4981
4982 size_change_ok = false;
4983 type_change_ok = bed->type_change_ok;
4984 old_weak = false;
4985 matched = false;
4986 old_alignment = 0;
4987 old_bfd = NULL;
4988 new_sec = sec;
4989
4990 if (is_elf_hash_table (&htab->root))
4991 {
4992 Elf_Internal_Versym iver;
4993 unsigned int vernum = 0;
4994 bool skip;
4995
4996 if (ever == NULL)
4997 {
4998 if (info->default_imported_symver)
4999 /* Use the default symbol version created earlier. */
5000 iver.vs_vers = elf_tdata (abfd)->cverdefs;
5001 else
5002 iver.vs_vers = 0;
5003 }
5004 else if (ever >= extversym_end)
5005 {
5006 /* xgettext:c-format */
5007 _bfd_error_handler (_("%pB: not enough version information"),
5008 abfd);
5009 bfd_set_error (bfd_error_bad_value);
5010 goto error_free_vers;
5011 }
5012 else
5013 _bfd_elf_swap_versym_in (abfd, ever, &iver);
5014
5015 vernum = iver.vs_vers & VERSYM_VERSION;
5016
5017 /* If this is a hidden symbol, or if it is not version
5018 1, we append the version name to the symbol name.
5019 However, we do not modify a non-hidden absolute symbol
5020 if it is not a function, because it might be the version
5021 symbol itself. FIXME: What if it isn't? */
5022 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
5023 || (vernum > 1
5024 && (!bfd_is_abs_section (sec)
5025 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
5026 {
5027 const char *verstr;
5028 size_t namelen, verlen, newlen;
5029 char *newname, *p;
5030
5031 if (isym->st_shndx != SHN_UNDEF)
5032 {
5033 if (vernum > elf_tdata (abfd)->cverdefs)
5034 verstr = NULL;
5035 else if (vernum > 1)
5036 verstr =
5037 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
5038 else
5039 verstr = "";
5040
5041 if (verstr == NULL)
5042 {
5043 _bfd_error_handler
5044 /* xgettext:c-format */
5045 (_("%pB: %s: invalid version %u (max %d)"),
5046 abfd, name, vernum,
5047 elf_tdata (abfd)->cverdefs);
5048 bfd_set_error (bfd_error_bad_value);
5049 goto error_free_vers;
5050 }
5051 }
5052 else
5053 {
5054 /* We cannot simply test for the number of
5055 entries in the VERNEED section since the
5056 numbers for the needed versions do not start
5057 at 0. */
5058 Elf_Internal_Verneed *t;
5059
5060 verstr = NULL;
5061 for (t = elf_tdata (abfd)->verref;
5062 t != NULL;
5063 t = t->vn_nextref)
5064 {
5065 Elf_Internal_Vernaux *a;
5066
5067 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5068 {
5069 if (a->vna_other == vernum)
5070 {
5071 verstr = a->vna_nodename;
5072 break;
5073 }
5074 }
5075 if (a != NULL)
5076 break;
5077 }
5078 if (verstr == NULL)
5079 {
5080 _bfd_error_handler
5081 /* xgettext:c-format */
5082 (_("%pB: %s: invalid needed version %d"),
5083 abfd, name, vernum);
5084 bfd_set_error (bfd_error_bad_value);
5085 goto error_free_vers;
5086 }
5087 }
5088
5089 namelen = strlen (name);
5090 verlen = strlen (verstr);
5091 newlen = namelen + verlen + 2;
5092 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5093 && isym->st_shndx != SHN_UNDEF)
5094 ++newlen;
5095
5096 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
5097 if (newname == NULL)
5098 goto error_free_vers;
5099 memcpy (newname, name, namelen);
5100 p = newname + namelen;
5101 *p++ = ELF_VER_CHR;
5102 /* If this is a defined non-hidden version symbol,
5103 we add another @ to the name. This indicates the
5104 default version of the symbol. */
5105 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5106 && isym->st_shndx != SHN_UNDEF)
5107 *p++ = ELF_VER_CHR;
5108 memcpy (p, verstr, verlen + 1);
5109
5110 name = newname;
5111 }
5112
5113 /* If this symbol has default visibility and the user has
5114 requested we not re-export it, then mark it as hidden. */
5115 if (!bfd_is_und_section (sec)
5116 && !dynamic
5117 && abfd->no_export
5118 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
5119 isym->st_other = (STV_HIDDEN
5120 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
5121
5122 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
5123 sym_hash, &old_bfd, &old_weak,
5124 &old_alignment, &skip, &override,
5125 &type_change_ok, &size_change_ok,
5126 &matched))
5127 goto error_free_vers;
5128
5129 if (skip)
5130 continue;
5131
5132 /* Override a definition only if the new symbol matches the
5133 existing one. */
5134 if (override && matched)
5135 definition = false;
5136
5137 h = *sym_hash;
5138 while (h->root.type == bfd_link_hash_indirect
5139 || h->root.type == bfd_link_hash_warning)
5140 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5141
5142 if (h->versioned != unversioned
5143 && elf_tdata (abfd)->verdef != NULL
5144 && vernum > 1
5145 && definition)
5146 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
5147 }
5148
5149 if (! (_bfd_generic_link_add_one_symbol
5150 (info, override ? override : abfd, name, flags, sec, value,
5151 NULL, false, bed->collect,
5152 (struct bfd_link_hash_entry **) sym_hash)))
5153 goto error_free_vers;
5154
5155 h = *sym_hash;
5156 /* We need to make sure that indirect symbol dynamic flags are
5157 updated. */
5158 hi = h;
5159 while (h->root.type == bfd_link_hash_indirect
5160 || h->root.type == bfd_link_hash_warning)
5161 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5162
5163 *sym_hash = h;
5164
5165 /* Setting the index to -3 tells elf_link_output_extsym that
5166 this symbol is defined in a discarded section. */
5167 if (discarded && is_elf_hash_table (&htab->root))
5168 h->indx = -3;
5169
5170 new_weak = (flags & BSF_WEAK) != 0;
5171 if (dynamic
5172 && definition
5173 && new_weak
5174 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
5175 && is_elf_hash_table (&htab->root)
5176 && h->u.alias == NULL)
5177 {
5178 /* Keep a list of all weak defined non function symbols from
5179 a dynamic object, using the alias field. Later in this
5180 function we will set the alias field to the correct
5181 value. We only put non-function symbols from dynamic
5182 objects on this list, because that happens to be the only
5183 time we need to know the normal symbol corresponding to a
5184 weak symbol, and the information is time consuming to
5185 figure out. If the alias field is not already NULL,
5186 then this symbol was already defined by some previous
5187 dynamic object, and we will be using that previous
5188 definition anyhow. */
5189
5190 h->u.alias = weaks;
5191 weaks = h;
5192 }
5193
5194 /* Set the alignment of a common symbol. */
5195 if ((common || bfd_is_com_section (sec))
5196 && h->root.type == bfd_link_hash_common)
5197 {
5198 unsigned int align;
5199
5200 if (common)
5201 align = bfd_log2 (isym->st_value);
5202 else
5203 {
5204 /* The new symbol is a common symbol in a shared object.
5205 We need to get the alignment from the section. */
5206 align = new_sec->alignment_power;
5207 }
5208 if (align > old_alignment)
5209 h->root.u.c.p->alignment_power = align;
5210 else
5211 h->root.u.c.p->alignment_power = old_alignment;
5212 }
5213
5214 if (is_elf_hash_table (&htab->root))
5215 {
5216 /* Set a flag in the hash table entry indicating the type of
5217 reference or definition we just found. A dynamic symbol
5218 is one which is referenced or defined by both a regular
5219 object and a shared object. */
5220 bool dynsym = false;
5221
5222 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5223 if ((abfd->flags & BFD_PLUGIN) != 0)
5224 {
5225 /* Except for this flag to track nonweak references. */
5226 if (!definition
5227 && bind != STB_WEAK)
5228 h->ref_ir_nonweak = 1;
5229 }
5230 else if (!dynamic)
5231 {
5232 if (! definition)
5233 {
5234 h->ref_regular = 1;
5235 if (bind != STB_WEAK)
5236 h->ref_regular_nonweak = 1;
5237 }
5238 else
5239 {
5240 h->def_regular = 1;
5241 if (h->def_dynamic)
5242 {
5243 h->def_dynamic = 0;
5244 h->ref_dynamic = 1;
5245 }
5246 }
5247 }
5248 else
5249 {
5250 if (! definition)
5251 {
5252 h->ref_dynamic = 1;
5253 hi->ref_dynamic = 1;
5254 }
5255 else
5256 {
5257 h->def_dynamic = 1;
5258 hi->def_dynamic = 1;
5259 }
5260 }
5261
5262 /* If an indirect symbol has been forced local, don't
5263 make the real symbol dynamic. */
5264 if (h != hi && hi->forced_local)
5265 ;
5266 else if (!dynamic)
5267 {
5268 if (bfd_link_dll (info)
5269 || h->def_dynamic
5270 || h->ref_dynamic)
5271 dynsym = true;
5272 }
5273 else
5274 {
5275 if (h->def_regular
5276 || h->ref_regular
5277 || (h->is_weakalias
5278 && weakdef (h)->dynindx != -1))
5279 dynsym = true;
5280 }
5281
5282 /* Check to see if we need to add an indirect symbol for
5283 the default name. */
5284 if ((definition
5285 || (!override && h->root.type == bfd_link_hash_common))
5286 && !(hi != h
5287 && hi->versioned == versioned_hidden))
5288 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5289 sec, value, &old_bfd, &dynsym))
5290 goto error_free_vers;
5291
5292 /* Check the alignment when a common symbol is involved. This
5293 can change when a common symbol is overridden by a normal
5294 definition or a common symbol is ignored due to the old
5295 normal definition. We need to make sure the maximum
5296 alignment is maintained. */
5297 if ((old_alignment || common)
5298 && h->root.type != bfd_link_hash_common)
5299 {
5300 unsigned int common_align;
5301 unsigned int normal_align;
5302 unsigned int symbol_align;
5303 bfd *normal_bfd;
5304 bfd *common_bfd;
5305
5306 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5307 || h->root.type == bfd_link_hash_defweak);
5308
5309 symbol_align = ffs (h->root.u.def.value) - 1;
5310 if (h->root.u.def.section->owner != NULL
5311 && (h->root.u.def.section->owner->flags
5312 & (DYNAMIC | BFD_PLUGIN)) == 0)
5313 {
5314 normal_align = h->root.u.def.section->alignment_power;
5315 if (normal_align > symbol_align)
5316 normal_align = symbol_align;
5317 }
5318 else
5319 normal_align = symbol_align;
5320
5321 if (old_alignment)
5322 {
5323 common_align = old_alignment;
5324 common_bfd = old_bfd;
5325 normal_bfd = abfd;
5326 }
5327 else
5328 {
5329 common_align = bfd_log2 (isym->st_value);
5330 common_bfd = abfd;
5331 normal_bfd = old_bfd;
5332 }
5333
5334 if (normal_align < common_align)
5335 {
5336 /* PR binutils/2735 */
5337 if (normal_bfd == NULL)
5338 _bfd_error_handler
5339 /* xgettext:c-format */
5340 (_("warning: alignment %u of common symbol `%s' in %pB is"
5341 " greater than the alignment (%u) of its section %pA"),
5342 1 << common_align, name, common_bfd,
5343 1 << normal_align, h->root.u.def.section);
5344 else
5345 _bfd_error_handler
5346 /* xgettext:c-format */
5347 (_("warning: alignment %u of normal symbol `%s' in %pB"
5348 " is smaller than %u used by the common definition in %pB"),
5349 1 << normal_align, name, normal_bfd,
5350 1 << common_align, common_bfd);
5351
5352 /* PR 30499: make sure that users understand that this warning is serious. */
5353 _bfd_error_handler
5354 (_("warning: NOTE: alignment discrepancies can cause real problems. Investigation is advised."));
5355 }
5356 }
5357
5358 /* Remember the symbol size if it isn't undefined. */
5359 if (isym->st_size != 0
5360 && isym->st_shndx != SHN_UNDEF
5361 && (definition || h->size == 0))
5362 {
5363 if (h->size != 0
5364 && h->size != isym->st_size
5365 && ! size_change_ok)
5366 {
5367 _bfd_error_handler
5368 /* xgettext:c-format */
5369 (_("warning: size of symbol `%s' changed"
5370 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5371 name, (uint64_t) h->size, old_bfd,
5372 (uint64_t) isym->st_size, abfd);
5373
5374 /* PR 30499: make sure that users understand that this warning is serious. */
5375 _bfd_error_handler
5376 (_("warning: NOTE: size discrepancies can cause real problems. Investigation is advised."));
5377 }
5378
5379 h->size = isym->st_size;
5380 }
5381
5382 /* If this is a common symbol, then we always want H->SIZE
5383 to be the size of the common symbol. The code just above
5384 won't fix the size if a common symbol becomes larger. We
5385 don't warn about a size change here, because that is
5386 covered by --warn-common. Allow changes between different
5387 function types. */
5388 if (h->root.type == bfd_link_hash_common)
5389 h->size = h->root.u.c.size;
5390
5391 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5392 && ((definition && !new_weak)
5393 || (old_weak && h->root.type == bfd_link_hash_common)
5394 || h->type == STT_NOTYPE))
5395 {
5396 unsigned int type = ELF_ST_TYPE (isym->st_info);
5397
5398 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5399 symbol. */
5400 if (type == STT_GNU_IFUNC
5401 && (abfd->flags & DYNAMIC) != 0)
5402 type = STT_FUNC;
5403
5404 if (h->type != type)
5405 {
5406 if (h->type != STT_NOTYPE && ! type_change_ok)
5407 /* xgettext:c-format */
5408 _bfd_error_handler
5409 (_("warning: type of symbol `%s' changed"
5410 " from %d to %d in %pB"),
5411 name, h->type, type, abfd);
5412
5413 h->type = type;
5414 }
5415 }
5416
5417 /* Merge st_other field. */
5418 elf_merge_st_other (abfd, h, isym->st_other, sec,
5419 definition, dynamic);
5420
5421 /* We don't want to make debug symbol dynamic. */
5422 if (definition
5423 && (sec->flags & SEC_DEBUGGING)
5424 && !bfd_link_relocatable (info))
5425 dynsym = false;
5426
5427 /* Nor should we make plugin symbols dynamic. */
5428 if ((abfd->flags & BFD_PLUGIN) != 0)
5429 dynsym = false;
5430
5431 if (definition)
5432 {
5433 h->target_internal = isym->st_target_internal;
5434 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5435 }
5436
5437 /* Don't add indirect symbols for .symver x, x@FOO aliases
5438 in IR. Since all data or text symbols in IR have the
5439 same type, value and section, we can't tell if a symbol
5440 is an alias of another symbol by their types, values and
5441 sections. */
5442 if (definition
5443 && !dynamic
5444 && (abfd->flags & BFD_PLUGIN) == 0)
5445 {
5446 char *p = strchr (name, ELF_VER_CHR);
5447 if (p != NULL && p[1] != ELF_VER_CHR)
5448 {
5449 /* Queue non-default versions so that .symver x, x@FOO
5450 aliases can be checked. */
5451 if (!nondeflt_vers)
5452 {
5453 size_t amt = ((isymend - isym + 1)
5454 * sizeof (struct elf_link_hash_entry *));
5455 nondeflt_vers
5456 = (struct elf_link_hash_entry **) bfd_malloc (amt);
5457 if (!nondeflt_vers)
5458 goto error_free_vers;
5459 }
5460 nondeflt_vers[nondeflt_vers_cnt++] = h;
5461 }
5462 }
5463
5464 if (dynsym && h->dynindx == -1)
5465 {
5466 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5467 goto error_free_vers;
5468 if (h->is_weakalias
5469 && weakdef (h)->dynindx == -1)
5470 {
5471 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5472 goto error_free_vers;
5473 }
5474 }
5475 else if (h->dynindx != -1)
5476 /* If the symbol already has a dynamic index, but
5477 visibility says it should not be visible, turn it into
5478 a local symbol. */
5479 switch (ELF_ST_VISIBILITY (h->other))
5480 {
5481 case STV_INTERNAL:
5482 case STV_HIDDEN:
5483 (*bed->elf_backend_hide_symbol) (info, h, true);
5484 dynsym = false;
5485 break;
5486 }
5487
5488 if (!add_needed
5489 && matched
5490 && definition
5491 && h->root.type != bfd_link_hash_indirect
5492 && ((dynsym
5493 && h->ref_regular_nonweak)
5494 || (old_bfd != NULL
5495 && (old_bfd->flags & BFD_PLUGIN) != 0
5496 && h->ref_ir_nonweak
5497 && !info->lto_all_symbols_read)
5498 || (h->ref_dynamic_nonweak
5499 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5500 && !on_needed_list (elf_dt_name (abfd),
5501 htab->needed, NULL))))
5502 {
5503 const char *soname = elf_dt_name (abfd);
5504
5505 info->callbacks->minfo ("%!", soname, old_bfd,
5506 h->root.root.string);
5507
5508 /* A symbol from a library loaded via DT_NEEDED of some
5509 other library is referenced by a regular object.
5510 Add a DT_NEEDED entry for it. Issue an error if
5511 --no-add-needed is used and the reference was not
5512 a weak one. */
5513 if (old_bfd != NULL
5514 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5515 {
5516 _bfd_error_handler
5517 /* xgettext:c-format */
5518 (_("%pB: undefined reference to symbol '%s'"),
5519 old_bfd, name);
5520 bfd_set_error (bfd_error_missing_dso);
5521 goto error_free_vers;
5522 }
5523
5524 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5525 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5526
5527 /* Create dynamic sections for backends that require
5528 that be done before setup_gnu_properties. */
5529 if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
5530 return false;
5531 add_needed = true;
5532 }
5533 }
5534 }
5535
5536 if (info->lto_plugin_active
5537 && !bfd_link_relocatable (info)
5538 && (abfd->flags & BFD_PLUGIN) == 0
5539 && !just_syms
5540 && extsymcount)
5541 {
5542 int r_sym_shift;
5543
5544 if (bed->s->arch_size == 32)
5545 r_sym_shift = 8;
5546 else
5547 r_sym_shift = 32;
5548
5549 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5550 referenced in regular objects so that linker plugin will get
5551 the correct symbol resolution. */
5552
5553 sym_hash = elf_sym_hashes (abfd);
5554 for (s = abfd->sections; s != NULL; s = s->next)
5555 {
5556 Elf_Internal_Rela *internal_relocs;
5557 Elf_Internal_Rela *rel, *relend;
5558
5559 /* Don't check relocations in excluded sections. */
5560 if ((s->flags & SEC_RELOC) == 0
5561 || s->reloc_count == 0
5562 || (s->flags & SEC_EXCLUDE) != 0
5563 || ((info->strip == strip_all
5564 || info->strip == strip_debugger)
5565 && (s->flags & SEC_DEBUGGING) != 0))
5566 continue;
5567
5568 internal_relocs = _bfd_elf_link_info_read_relocs (abfd, info,
5569 s, NULL,
5570 NULL,
5571 _bfd_link_keep_memory (info));
5572 if (internal_relocs == NULL)
5573 goto error_free_vers;
5574
5575 rel = internal_relocs;
5576 relend = rel + s->reloc_count;
5577 for ( ; rel < relend; rel++)
5578 {
5579 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5580 struct elf_link_hash_entry *h;
5581
5582 /* Skip local symbols. */
5583 if (r_symndx < extsymoff)
5584 continue;
5585
5586 h = sym_hash[r_symndx - extsymoff];
5587 if (h != NULL)
5588 h->root.non_ir_ref_regular = 1;
5589 }
5590
5591 if (elf_section_data (s)->relocs != internal_relocs)
5592 free (internal_relocs);
5593 }
5594 }
5595
5596 free (extversym);
5597 extversym = NULL;
5598 free (isymbuf);
5599 isymbuf = NULL;
5600
5601 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5602 {
5603 unsigned int i;
5604
5605 /* Restore the symbol table. */
5606 old_ent = (char *) old_tab + tabsize;
5607 memset (elf_sym_hashes (abfd), 0,
5608 extsymcount * sizeof (struct elf_link_hash_entry *));
5609 htab->root.table.table = old_table;
5610 htab->root.table.size = old_size;
5611 htab->root.table.count = old_count;
5612 memcpy (htab->root.table.table, old_tab, tabsize);
5613 htab->root.undefs = old_undefs;
5614 htab->root.undefs_tail = old_undefs_tail;
5615 if (htab->dynstr != NULL)
5616 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5617 free (old_strtab);
5618 old_strtab = NULL;
5619 for (i = 0; i < htab->root.table.size; i++)
5620 {
5621 struct bfd_hash_entry *p;
5622 struct elf_link_hash_entry *h;
5623 unsigned int non_ir_ref_dynamic;
5624
5625 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5626 {
5627 /* Preserve non_ir_ref_dynamic so that this symbol
5628 will be exported when the dynamic lib becomes needed
5629 in the second pass. */
5630 h = (struct elf_link_hash_entry *) p;
5631 if (h->root.type == bfd_link_hash_warning)
5632 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5633 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5634
5635 h = (struct elf_link_hash_entry *) p;
5636 memcpy (h, old_ent, htab->root.table.entsize);
5637 old_ent = (char *) old_ent + htab->root.table.entsize;
5638 if (h->root.type == bfd_link_hash_warning)
5639 {
5640 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5641 memcpy (h, old_ent, htab->root.table.entsize);
5642 old_ent = (char *) old_ent + htab->root.table.entsize;
5643 }
5644 if (h->root.type == bfd_link_hash_common)
5645 {
5646 memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5647 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
5648 }
5649 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5650 }
5651 }
5652
5653 /* Make a special call to the linker "notice" function to
5654 tell it that symbols added for crefs may need to be removed. */
5655 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5656 goto error_free_vers;
5657
5658 free (old_tab);
5659 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5660 alloc_mark);
5661 free (nondeflt_vers);
5662 return true;
5663 }
5664
5665 if (old_tab != NULL)
5666 {
5667 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5668 goto error_free_vers;
5669 free (old_tab);
5670 old_tab = NULL;
5671 }
5672
5673 /* Now that all the symbols from this input file are created, if
5674 not performing a relocatable link, handle .symver foo, foo@BAR
5675 such that any relocs against foo become foo@BAR. */
5676 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5677 {
5678 size_t cnt, symidx;
5679
5680 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5681 {
5682 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5683 char *shortname, *p;
5684 size_t amt;
5685
5686 p = strchr (h->root.root.string, ELF_VER_CHR);
5687 if (p == NULL
5688 || (h->root.type != bfd_link_hash_defined
5689 && h->root.type != bfd_link_hash_defweak))
5690 continue;
5691
5692 amt = p - h->root.root.string;
5693 shortname = (char *) bfd_malloc (amt + 1);
5694 if (!shortname)
5695 goto error_free_vers;
5696 memcpy (shortname, h->root.root.string, amt);
5697 shortname[amt] = '\0';
5698
5699 hi = (struct elf_link_hash_entry *)
5700 bfd_link_hash_lookup (&htab->root, shortname,
5701 false, false, false);
5702 if (hi != NULL
5703 && hi->root.type == h->root.type
5704 && hi->root.u.def.value == h->root.u.def.value
5705 && hi->root.u.def.section == h->root.u.def.section)
5706 {
5707 (*bed->elf_backend_hide_symbol) (info, hi, true);
5708 hi->root.type = bfd_link_hash_indirect;
5709 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5710 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5711 sym_hash = elf_sym_hashes (abfd);
5712 if (sym_hash)
5713 for (symidx = 0; symidx < extsymcount; ++symidx)
5714 if (sym_hash[symidx] == hi)
5715 {
5716 sym_hash[symidx] = h;
5717 break;
5718 }
5719 }
5720 free (shortname);
5721 }
5722 free (nondeflt_vers);
5723 nondeflt_vers = NULL;
5724 }
5725
5726 /* Now set the alias field correctly for all the weak defined
5727 symbols we found. The only way to do this is to search all the
5728 symbols. Since we only need the information for non functions in
5729 dynamic objects, that's the only time we actually put anything on
5730 the list WEAKS. We need this information so that if a regular
5731 object refers to a symbol defined weakly in a dynamic object, the
5732 real symbol in the dynamic object is also put in the dynamic
5733 symbols; we also must arrange for both symbols to point to the
5734 same memory location. We could handle the general case of symbol
5735 aliasing, but a general symbol alias can only be generated in
5736 assembler code, handling it correctly would be very time
5737 consuming, and other ELF linkers don't handle general aliasing
5738 either. */
5739 if (weaks != NULL)
5740 {
5741 struct elf_link_hash_entry **hpp;
5742 struct elf_link_hash_entry **hppend;
5743 struct elf_link_hash_entry **sorted_sym_hash;
5744 struct elf_link_hash_entry *h;
5745 size_t sym_count, amt;
5746
5747 /* Since we have to search the whole symbol list for each weak
5748 defined symbol, search time for N weak defined symbols will be
5749 O(N^2). Binary search will cut it down to O(NlogN). */
5750 amt = extsymcount * sizeof (*sorted_sym_hash);
5751 sorted_sym_hash = bfd_malloc (amt);
5752 if (sorted_sym_hash == NULL)
5753 goto error_return;
5754 sym_hash = sorted_sym_hash;
5755 hpp = elf_sym_hashes (abfd);
5756 hppend = hpp + extsymcount;
5757 sym_count = 0;
5758 for (; hpp < hppend; hpp++)
5759 {
5760 h = *hpp;
5761 if (h != NULL
5762 && h->root.type == bfd_link_hash_defined
5763 && !bed->is_function_type (h->type))
5764 {
5765 *sym_hash = h;
5766 sym_hash++;
5767 sym_count++;
5768 }
5769 }
5770
5771 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
5772 elf_sort_symbol);
5773
5774 while (weaks != NULL)
5775 {
5776 struct elf_link_hash_entry *hlook;
5777 asection *slook;
5778 bfd_vma vlook;
5779 size_t i, j, idx = 0;
5780
5781 hlook = weaks;
5782 weaks = hlook->u.alias;
5783 hlook->u.alias = NULL;
5784
5785 if (hlook->root.type != bfd_link_hash_defined
5786 && hlook->root.type != bfd_link_hash_defweak)
5787 continue;
5788
5789 slook = hlook->root.u.def.section;
5790 vlook = hlook->root.u.def.value;
5791
5792 i = 0;
5793 j = sym_count;
5794 while (i != j)
5795 {
5796 bfd_signed_vma vdiff;
5797 idx = (i + j) / 2;
5798 h = sorted_sym_hash[idx];
5799 vdiff = vlook - h->root.u.def.value;
5800 if (vdiff < 0)
5801 j = idx;
5802 else if (vdiff > 0)
5803 i = idx + 1;
5804 else
5805 {
5806 int sdiff = slook->id - h->root.u.def.section->id;
5807 if (sdiff < 0)
5808 j = idx;
5809 else if (sdiff > 0)
5810 i = idx + 1;
5811 else
5812 break;
5813 }
5814 }
5815
5816 /* We didn't find a value/section match. */
5817 if (i == j)
5818 continue;
5819
5820 /* With multiple aliases, or when the weak symbol is already
5821 strongly defined, we have multiple matching symbols and
5822 the binary search above may land on any of them. Step
5823 one past the matching symbol(s). */
5824 while (++idx != j)
5825 {
5826 h = sorted_sym_hash[idx];
5827 if (h->root.u.def.section != slook
5828 || h->root.u.def.value != vlook)
5829 break;
5830 }
5831
5832 /* Now look back over the aliases. Since we sorted by size
5833 as well as value and section, we'll choose the one with
5834 the largest size. */
5835 while (idx-- != i)
5836 {
5837 h = sorted_sym_hash[idx];
5838
5839 /* Stop if value or section doesn't match. */
5840 if (h->root.u.def.section != slook
5841 || h->root.u.def.value != vlook)
5842 break;
5843 else if (h != hlook)
5844 {
5845 struct elf_link_hash_entry *t;
5846
5847 hlook->u.alias = h;
5848 hlook->is_weakalias = 1;
5849 t = h;
5850 if (t->u.alias != NULL)
5851 while (t->u.alias != h)
5852 t = t->u.alias;
5853 t->u.alias = hlook;
5854
5855 /* If the weak definition is in the list of dynamic
5856 symbols, make sure the real definition is put
5857 there as well. */
5858 if (hlook->dynindx != -1 && h->dynindx == -1)
5859 {
5860 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5861 {
5862 err_free_sym_hash:
5863 free (sorted_sym_hash);
5864 goto error_return;
5865 }
5866 }
5867
5868 /* If the real definition is in the list of dynamic
5869 symbols, make sure the weak definition is put
5870 there as well. If we don't do this, then the
5871 dynamic loader might not merge the entries for the
5872 real definition and the weak definition. */
5873 if (h->dynindx != -1 && hlook->dynindx == -1)
5874 {
5875 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5876 goto err_free_sym_hash;
5877 }
5878 break;
5879 }
5880 }
5881 }
5882
5883 free (sorted_sym_hash);
5884 }
5885
5886 if (bed->check_directives
5887 && !(*bed->check_directives) (abfd, info))
5888 return false;
5889
5890 /* If this is a non-traditional link, try to optimize the handling
5891 of the .stab/.stabstr sections. */
5892 if (! dynamic
5893 && ! info->traditional_format
5894 && is_elf_hash_table (&htab->root)
5895 && (info->strip != strip_all && info->strip != strip_debugger))
5896 {
5897 asection *stabstr;
5898
5899 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5900 if (stabstr != NULL)
5901 {
5902 bfd_size_type string_offset = 0;
5903 asection *stab;
5904
5905 for (stab = abfd->sections; stab; stab = stab->next)
5906 if (startswith (stab->name, ".stab")
5907 && (!stab->name[5] ||
5908 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5909 && (stab->flags & SEC_MERGE) == 0
5910 && !bfd_is_abs_section (stab->output_section))
5911 {
5912 struct bfd_elf_section_data *secdata;
5913
5914 secdata = elf_section_data (stab);
5915 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5916 stabstr, &secdata->sec_info,
5917 &string_offset))
5918 goto error_return;
5919 if (secdata->sec_info)
5920 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5921 }
5922 }
5923 }
5924
5925 if (dynamic && add_needed)
5926 {
5927 /* Add this bfd to the loaded list. */
5928 struct elf_link_loaded_list *n;
5929
5930 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5931 if (n == NULL)
5932 goto error_return;
5933 n->abfd = abfd;
5934 n->next = htab->dyn_loaded;
5935 htab->dyn_loaded = n;
5936 }
5937 if (dynamic && !add_needed
5938 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
5939 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
5940
5941 return true;
5942
5943 error_free_vers:
5944 free (old_tab);
5945 free (old_strtab);
5946 free (nondeflt_vers);
5947 free (extversym);
5948 error_free_sym:
5949 free (isymbuf);
5950 error_return:
5951 return false;
5952 }
5953
5954 /* Return the linker hash table entry of a symbol that might be
5955 satisfied by an archive symbol. Return -1 on error. */
5956
5957 struct bfd_link_hash_entry *
_bfd_elf_archive_symbol_lookup(bfd * abfd,struct bfd_link_info * info,const char * name)5958 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5959 struct bfd_link_info *info,
5960 const char *name)
5961 {
5962 struct bfd_link_hash_entry *h;
5963 char *p, *copy;
5964 size_t len, first;
5965
5966 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
5967 if (h != NULL)
5968 return h;
5969
5970 /* If this is a default version (the name contains @@), look up the
5971 symbol again with only one `@' as well as without the version.
5972 The effect is that references to the symbol with and without the
5973 version will be matched by the default symbol in the archive. */
5974
5975 p = strchr (name, ELF_VER_CHR);
5976 if (p == NULL || p[1] != ELF_VER_CHR)
5977 return h;
5978
5979 /* First check with only one `@'. */
5980 len = strlen (name);
5981 copy = (char *) bfd_alloc (abfd, len);
5982 if (copy == NULL)
5983 return (struct bfd_link_hash_entry *) -1;
5984
5985 first = p - name + 1;
5986 memcpy (copy, name, first);
5987 memcpy (copy + first, name + first + 1, len - first);
5988
5989 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
5990 if (h == NULL)
5991 {
5992 /* We also need to check references to the symbol without the
5993 version. */
5994 copy[first - 1] = '\0';
5995 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
5996 }
5997
5998 bfd_release (abfd, copy);
5999 return h;
6000 }
6001
6002 /* Add symbols from an ELF archive file to the linker hash table. We
6003 don't use _bfd_generic_link_add_archive_symbols because we need to
6004 handle versioned symbols.
6005
6006 Fortunately, ELF archive handling is simpler than that done by
6007 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
6008 oddities. In ELF, if we find a symbol in the archive map, and the
6009 symbol is currently undefined, we know that we must pull in that
6010 object file.
6011
6012 Unfortunately, we do have to make multiple passes over the symbol
6013 table until nothing further is resolved. */
6014
6015 static bool
elf_link_add_archive_symbols(bfd * abfd,struct bfd_link_info * info)6016 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
6017 {
6018 symindex c;
6019 unsigned char *included = NULL;
6020 carsym *symdefs;
6021 bool loop;
6022 size_t amt;
6023 const struct elf_backend_data *bed;
6024 struct bfd_link_hash_entry * (*archive_symbol_lookup)
6025 (bfd *, struct bfd_link_info *, const char *);
6026
6027 if (! bfd_has_map (abfd))
6028 {
6029 /* An empty archive is a special case. */
6030 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
6031 return true;
6032 bfd_set_error (bfd_error_no_armap);
6033 return false;
6034 }
6035
6036 /* Keep track of all symbols we know to be already defined, and all
6037 files we know to be already included. This is to speed up the
6038 second and subsequent passes. */
6039 c = bfd_ardata (abfd)->symdef_count;
6040 if (c == 0)
6041 return true;
6042 amt = c * sizeof (*included);
6043 included = (unsigned char *) bfd_zmalloc (amt);
6044 if (included == NULL)
6045 return false;
6046
6047 symdefs = bfd_ardata (abfd)->symdefs;
6048 bed = get_elf_backend_data (abfd);
6049 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
6050
6051 do
6052 {
6053 file_ptr last;
6054 symindex i;
6055 carsym *symdef;
6056 carsym *symdefend;
6057
6058 loop = false;
6059 last = -1;
6060
6061 symdef = symdefs;
6062 symdefend = symdef + c;
6063 for (i = 0; symdef < symdefend; symdef++, i++)
6064 {
6065 struct bfd_link_hash_entry *h;
6066 bfd *element;
6067 struct bfd_link_hash_entry *undefs_tail;
6068 symindex mark;
6069
6070 if (included[i])
6071 continue;
6072 if (symdef->file_offset == last)
6073 {
6074 included[i] = true;
6075 continue;
6076 }
6077
6078 h = archive_symbol_lookup (abfd, info, symdef->name);
6079 if (h == (struct bfd_link_hash_entry *) -1)
6080 goto error_return;
6081
6082 if (h == NULL)
6083 continue;
6084
6085 if (h->type == bfd_link_hash_undefined)
6086 {
6087 /* If the archive element has already been loaded then one
6088 of the symbols defined by that element might have been
6089 made undefined due to being in a discarded section. */
6090 if (is_elf_hash_table (info->hash)
6091 && ((struct elf_link_hash_entry *) h)->indx == -3)
6092 continue;
6093 }
6094 else if (h->type == bfd_link_hash_common)
6095 {
6096 /* We currently have a common symbol. The archive map contains
6097 a reference to this symbol, so we may want to include it. We
6098 only want to include it however, if this archive element
6099 contains a definition of the symbol, not just another common
6100 declaration of it.
6101
6102 Unfortunately some archivers (including GNU ar) will put
6103 declarations of common symbols into their archive maps, as
6104 well as real definitions, so we cannot just go by the archive
6105 map alone. Instead we must read in the element's symbol
6106 table and check that to see what kind of symbol definition
6107 this is. */
6108 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
6109 continue;
6110 }
6111 else
6112 {
6113 if (h->type != bfd_link_hash_undefweak)
6114 /* Symbol must be defined. Don't check it again. */
6115 included[i] = true;
6116 continue;
6117 }
6118
6119 /* We need to include this archive member. */
6120 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset,
6121 info);
6122 if (element == NULL)
6123 goto error_return;
6124
6125 if (! bfd_check_format (element, bfd_object))
6126 goto error_return;
6127
6128 undefs_tail = info->hash->undefs_tail;
6129
6130 if (!(*info->callbacks
6131 ->add_archive_element) (info, element, symdef->name, &element))
6132 continue;
6133 if (!bfd_link_add_symbols (element, info))
6134 goto error_return;
6135
6136 /* If there are any new undefined symbols, we need to make
6137 another pass through the archive in order to see whether
6138 they can be defined. FIXME: This isn't perfect, because
6139 common symbols wind up on undefs_tail and because an
6140 undefined symbol which is defined later on in this pass
6141 does not require another pass. This isn't a bug, but it
6142 does make the code less efficient than it could be. */
6143 if (undefs_tail != info->hash->undefs_tail)
6144 loop = true;
6145
6146 /* Look backward to mark all symbols from this object file
6147 which we have already seen in this pass. */
6148 mark = i;
6149 do
6150 {
6151 included[mark] = true;
6152 if (mark == 0)
6153 break;
6154 --mark;
6155 }
6156 while (symdefs[mark].file_offset == symdef->file_offset);
6157
6158 /* We mark subsequent symbols from this object file as we go
6159 on through the loop. */
6160 last = symdef->file_offset;
6161 }
6162 }
6163 while (loop);
6164
6165 free (included);
6166 return true;
6167
6168 error_return:
6169 free (included);
6170 return false;
6171 }
6172
6173 /* Given an ELF BFD, add symbols to the global hash table as
6174 appropriate. */
6175
6176 bool
bfd_elf_link_add_symbols(bfd * abfd,struct bfd_link_info * info)6177 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
6178 {
6179 switch (bfd_get_format (abfd))
6180 {
6181 case bfd_object:
6182 return elf_link_add_object_symbols (abfd, info);
6183 case bfd_archive:
6184 return elf_link_add_archive_symbols (abfd, info);
6185 default:
6186 bfd_set_error (bfd_error_wrong_format);
6187 return false;
6188 }
6189 }
6190
6191 struct hash_codes_info
6192 {
6193 unsigned long *hashcodes;
6194 bool error;
6195 };
6196
6197 /* This function will be called though elf_link_hash_traverse to store
6198 all hash value of the exported symbols in an array. */
6199
6200 static bool
elf_collect_hash_codes(struct elf_link_hash_entry * h,void * data)6201 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
6202 {
6203 struct hash_codes_info *inf = (struct hash_codes_info *) data;
6204 const char *name;
6205 unsigned long ha;
6206 char *alc = NULL;
6207
6208 /* Ignore indirect symbols. These are added by the versioning code. */
6209 if (h->dynindx == -1)
6210 return true;
6211
6212 name = h->root.root.string;
6213 if (h->versioned >= versioned)
6214 {
6215 char *p = strchr (name, ELF_VER_CHR);
6216 if (p != NULL)
6217 {
6218 alc = (char *) bfd_malloc (p - name + 1);
6219 if (alc == NULL)
6220 {
6221 inf->error = true;
6222 return false;
6223 }
6224 memcpy (alc, name, p - name);
6225 alc[p - name] = '\0';
6226 name = alc;
6227 }
6228 }
6229
6230 /* Compute the hash value. */
6231 ha = bfd_elf_hash (name);
6232
6233 /* Store the found hash value in the array given as the argument. */
6234 *(inf->hashcodes)++ = ha;
6235
6236 /* And store it in the struct so that we can put it in the hash table
6237 later. */
6238 h->u.elf_hash_value = ha;
6239
6240 free (alc);
6241 return true;
6242 }
6243
6244 struct collect_gnu_hash_codes
6245 {
6246 bfd *output_bfd;
6247 const struct elf_backend_data *bed;
6248 unsigned long int nsyms;
6249 unsigned long int maskbits;
6250 unsigned long int *hashcodes;
6251 unsigned long int *hashval;
6252 unsigned long int *indx;
6253 unsigned long int *counts;
6254 bfd_vma *bitmask;
6255 bfd_byte *contents;
6256 bfd_size_type xlat;
6257 long int min_dynindx;
6258 unsigned long int bucketcount;
6259 unsigned long int symindx;
6260 long int local_indx;
6261 long int shift1, shift2;
6262 unsigned long int mask;
6263 bool error;
6264 };
6265
6266 /* This function will be called though elf_link_hash_traverse to store
6267 all hash value of the exported symbols in an array. */
6268
6269 static bool
elf_collect_gnu_hash_codes(struct elf_link_hash_entry * h,void * data)6270 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6271 {
6272 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6273 const char *name;
6274 unsigned long ha;
6275 char *alc = NULL;
6276
6277 /* Ignore indirect symbols. These are added by the versioning code. */
6278 if (h->dynindx == -1)
6279 return true;
6280
6281 /* Ignore also local symbols and undefined symbols. */
6282 if (! (*s->bed->elf_hash_symbol) (h))
6283 return true;
6284
6285 name = h->root.root.string;
6286 if (h->versioned >= versioned)
6287 {
6288 char *p = strchr (name, ELF_VER_CHR);
6289 if (p != NULL)
6290 {
6291 alc = (char *) bfd_malloc (p - name + 1);
6292 if (alc == NULL)
6293 {
6294 s->error = true;
6295 return false;
6296 }
6297 memcpy (alc, name, p - name);
6298 alc[p - name] = '\0';
6299 name = alc;
6300 }
6301 }
6302
6303 /* Compute the hash value. */
6304 ha = bfd_elf_gnu_hash (name);
6305
6306 /* Store the found hash value in the array for compute_bucket_count,
6307 and also for .dynsym reordering purposes. */
6308 s->hashcodes[s->nsyms] = ha;
6309 s->hashval[h->dynindx] = ha;
6310 ++s->nsyms;
6311 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6312 s->min_dynindx = h->dynindx;
6313
6314 free (alc);
6315 return true;
6316 }
6317
6318 /* This function will be called though elf_link_hash_traverse to do
6319 final dynamic symbol renumbering in case of .gnu.hash.
6320 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6321 to the translation table. */
6322
6323 static bool
elf_gnu_hash_process_symidx(struct elf_link_hash_entry * h,void * data)6324 elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
6325 {
6326 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6327 unsigned long int bucket;
6328 unsigned long int val;
6329
6330 /* Ignore indirect symbols. */
6331 if (h->dynindx == -1)
6332 return true;
6333
6334 /* Ignore also local symbols and undefined symbols. */
6335 if (! (*s->bed->elf_hash_symbol) (h))
6336 {
6337 if (h->dynindx >= s->min_dynindx)
6338 {
6339 if (s->bed->record_xhash_symbol != NULL)
6340 {
6341 (*s->bed->record_xhash_symbol) (h, 0);
6342 s->local_indx++;
6343 }
6344 else
6345 h->dynindx = s->local_indx++;
6346 }
6347 return true;
6348 }
6349
6350 bucket = s->hashval[h->dynindx] % s->bucketcount;
6351 val = (s->hashval[h->dynindx] >> s->shift1)
6352 & ((s->maskbits >> s->shift1) - 1);
6353 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6354 s->bitmask[val]
6355 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6356 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6357 if (s->counts[bucket] == 1)
6358 /* Last element terminates the chain. */
6359 val |= 1;
6360 bfd_put_32 (s->output_bfd, val,
6361 s->contents + (s->indx[bucket] - s->symindx) * 4);
6362 --s->counts[bucket];
6363 if (s->bed->record_xhash_symbol != NULL)
6364 {
6365 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6366
6367 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6368 }
6369 else
6370 h->dynindx = s->indx[bucket]++;
6371 return true;
6372 }
6373
6374 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6375
6376 bool
_bfd_elf_hash_symbol(struct elf_link_hash_entry * h)6377 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6378 {
6379 return !(h->forced_local
6380 || h->root.type == bfd_link_hash_undefined
6381 || h->root.type == bfd_link_hash_undefweak
6382 || ((h->root.type == bfd_link_hash_defined
6383 || h->root.type == bfd_link_hash_defweak)
6384 && h->root.u.def.section->output_section == NULL));
6385 }
6386
6387 /* Array used to determine the number of hash table buckets to use
6388 based on the number of symbols there are. If there are fewer than
6389 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6390 fewer than 37 we use 17 buckets, and so forth. We never use more
6391 than 32771 buckets. */
6392
6393 static const size_t elf_buckets[] =
6394 {
6395 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6396 16411, 32771, 0
6397 };
6398
6399 /* Compute bucket count for hashing table. We do not use a static set
6400 of possible tables sizes anymore. Instead we determine for all
6401 possible reasonable sizes of the table the outcome (i.e., the
6402 number of collisions etc) and choose the best solution. The
6403 weighting functions are not too simple to allow the table to grow
6404 without bounds. Instead one of the weighting factors is the size.
6405 Therefore the result is always a good payoff between few collisions
6406 (= short chain lengths) and table size. */
6407 static size_t
compute_bucket_count(struct bfd_link_info * info ATTRIBUTE_UNUSED,unsigned long int * hashcodes ATTRIBUTE_UNUSED,unsigned long int nsyms,int gnu_hash)6408 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6409 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6410 unsigned long int nsyms,
6411 int gnu_hash)
6412 {
6413 size_t best_size = 0;
6414 unsigned long int i;
6415
6416 if (info->optimize)
6417 {
6418 size_t minsize;
6419 size_t maxsize;
6420 uint64_t best_chlen = ~((uint64_t) 0);
6421 bfd *dynobj = elf_hash_table (info)->dynobj;
6422 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6423 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
6424 unsigned long int *counts;
6425 bfd_size_type amt;
6426 unsigned int no_improvement_count = 0;
6427
6428 /* Possible optimization parameters: if we have NSYMS symbols we say
6429 that the hashing table must at least have NSYMS/4 and at most
6430 2*NSYMS buckets. */
6431 minsize = nsyms / 4;
6432 if (minsize == 0)
6433 minsize = 1;
6434 best_size = maxsize = nsyms * 2;
6435 if (gnu_hash)
6436 {
6437 if (minsize < 2)
6438 minsize = 2;
6439 if ((best_size & 31) == 0)
6440 ++best_size;
6441 }
6442
6443 /* Create array where we count the collisions in. We must use bfd_malloc
6444 since the size could be large. */
6445 amt = maxsize;
6446 amt *= sizeof (unsigned long int);
6447 counts = (unsigned long int *) bfd_malloc (amt);
6448 if (counts == NULL)
6449 return 0;
6450
6451 /* Compute the "optimal" size for the hash table. The criteria is a
6452 minimal chain length. The minor criteria is (of course) the size
6453 of the table. */
6454 for (i = minsize; i < maxsize; ++i)
6455 {
6456 /* Walk through the array of hashcodes and count the collisions. */
6457 uint64_t max;
6458 unsigned long int j;
6459 unsigned long int fact;
6460
6461 if (gnu_hash && (i & 31) == 0)
6462 continue;
6463
6464 memset (counts, '\0', i * sizeof (unsigned long int));
6465
6466 /* Determine how often each hash bucket is used. */
6467 for (j = 0; j < nsyms; ++j)
6468 ++counts[hashcodes[j] % i];
6469
6470 /* For the weight function we need some information about the
6471 pagesize on the target. This is information need not be 100%
6472 accurate. Since this information is not available (so far) we
6473 define it here to a reasonable default value. If it is crucial
6474 to have a better value some day simply define this value. */
6475 # ifndef BFD_TARGET_PAGESIZE
6476 # define BFD_TARGET_PAGESIZE (4096)
6477 # endif
6478
6479 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6480 and the chains. */
6481 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6482
6483 # if 1
6484 /* Variant 1: optimize for short chains. We add the squares
6485 of all the chain lengths (which favors many small chain
6486 over a few long chains). */
6487 for (j = 0; j < i; ++j)
6488 max += counts[j] * counts[j];
6489
6490 /* This adds penalties for the overall size of the table. */
6491 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6492 max *= fact * fact;
6493 # else
6494 /* Variant 2: Optimize a lot more for small table. Here we
6495 also add squares of the size but we also add penalties for
6496 empty slots (the +1 term). */
6497 for (j = 0; j < i; ++j)
6498 max += (1 + counts[j]) * (1 + counts[j]);
6499
6500 /* The overall size of the table is considered, but not as
6501 strong as in variant 1, where it is squared. */
6502 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6503 max *= fact;
6504 # endif
6505
6506 /* Compare with current best results. */
6507 if (max < best_chlen)
6508 {
6509 best_chlen = max;
6510 best_size = i;
6511 no_improvement_count = 0;
6512 }
6513 /* PR 11843: Avoid futile long searches for the best bucket size
6514 when there are a large number of symbols. */
6515 else if (++no_improvement_count == 100)
6516 break;
6517 }
6518
6519 free (counts);
6520 }
6521 else
6522 {
6523 for (i = 0; elf_buckets[i] != 0; i++)
6524 {
6525 best_size = elf_buckets[i];
6526 if (nsyms < elf_buckets[i + 1])
6527 break;
6528 }
6529 if (gnu_hash && best_size < 2)
6530 best_size = 2;
6531 }
6532
6533 return best_size;
6534 }
6535
6536 /* Size any SHT_GROUP section for ld -r. */
6537
6538 bool
_bfd_elf_size_group_sections(struct bfd_link_info * info)6539 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6540 {
6541 bfd *ibfd;
6542 asection *s;
6543
6544 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6545 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6546 && (s = ibfd->sections) != NULL
6547 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6548 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6549 return false;
6550 return true;
6551 }
6552
6553 /* Set a default stack segment size. The value in INFO wins. If it
6554 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6555 undefined it is initialized. */
6556
6557 bool
bfd_elf_stack_segment_size(bfd * output_bfd,struct bfd_link_info * info,const char * legacy_symbol,bfd_vma default_size)6558 bfd_elf_stack_segment_size (bfd *output_bfd,
6559 struct bfd_link_info *info,
6560 const char *legacy_symbol,
6561 bfd_vma default_size)
6562 {
6563 struct elf_link_hash_entry *h = NULL;
6564
6565 /* Look for legacy symbol. */
6566 if (legacy_symbol)
6567 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6568 false, false, false);
6569 if (h && (h->root.type == bfd_link_hash_defined
6570 || h->root.type == bfd_link_hash_defweak)
6571 && h->def_regular
6572 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6573 {
6574 /* The symbol has no type if specified on the command line. */
6575 h->type = STT_OBJECT;
6576 if (info->stacksize)
6577 /* xgettext:c-format */
6578 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6579 output_bfd, legacy_symbol);
6580 else if (h->root.u.def.section != bfd_abs_section_ptr)
6581 /* xgettext:c-format */
6582 _bfd_error_handler (_("%pB: %s not absolute"),
6583 output_bfd, legacy_symbol);
6584 else
6585 info->stacksize = h->root.u.def.value;
6586 }
6587
6588 if (!info->stacksize)
6589 /* If the user didn't set a size, or explicitly inhibit the
6590 size, set it now. */
6591 info->stacksize = default_size;
6592
6593 /* Provide the legacy symbol, if it is referenced. */
6594 if (h && (h->root.type == bfd_link_hash_undefined
6595 || h->root.type == bfd_link_hash_undefweak))
6596 {
6597 struct bfd_link_hash_entry *bh = NULL;
6598
6599 if (!(_bfd_generic_link_add_one_symbol
6600 (info, output_bfd, legacy_symbol,
6601 BSF_GLOBAL, bfd_abs_section_ptr,
6602 info->stacksize >= 0 ? info->stacksize : 0,
6603 NULL, false, get_elf_backend_data (output_bfd)->collect, &bh)))
6604 return false;
6605
6606 h = (struct elf_link_hash_entry *) bh;
6607 h->def_regular = 1;
6608 h->type = STT_OBJECT;
6609 }
6610
6611 return true;
6612 }
6613
6614 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6615
6616 struct elf_gc_sweep_symbol_info
6617 {
6618 struct bfd_link_info *info;
6619 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6620 bool);
6621 };
6622
6623 static bool
elf_gc_sweep_symbol(struct elf_link_hash_entry * h,void * data)6624 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6625 {
6626 if (!h->mark
6627 && (((h->root.type == bfd_link_hash_defined
6628 || h->root.type == bfd_link_hash_defweak)
6629 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6630 && h->root.u.def.section->gc_mark))
6631 || h->root.type == bfd_link_hash_undefined
6632 || h->root.type == bfd_link_hash_undefweak))
6633 {
6634 struct elf_gc_sweep_symbol_info *inf;
6635
6636 inf = (struct elf_gc_sweep_symbol_info *) data;
6637 (*inf->hide_symbol) (inf->info, h, true);
6638 h->def_regular = 0;
6639 h->ref_regular = 0;
6640 h->ref_regular_nonweak = 0;
6641 }
6642
6643 return true;
6644 }
6645
6646 /* Set up the sizes and contents of the ELF dynamic sections. This is
6647 called by the ELF linker emulation before_allocation routine. We
6648 must set the sizes of the sections before the linker sets the
6649 addresses of the various sections. */
6650
6651 bool
bfd_elf_size_dynamic_sections(bfd * output_bfd,const char * soname,const char * rpath,const char * filter_shlib,const char * audit,const char * depaudit,const char * const * auxiliary_filters,struct bfd_link_info * info,asection ** sinterpptr)6652 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6653 const char *soname,
6654 const char *rpath,
6655 const char *filter_shlib,
6656 const char *audit,
6657 const char *depaudit,
6658 const char * const *auxiliary_filters,
6659 struct bfd_link_info *info,
6660 asection **sinterpptr)
6661 {
6662 bfd *dynobj;
6663 const struct elf_backend_data *bed;
6664
6665 *sinterpptr = NULL;
6666
6667 if (!is_elf_hash_table (info->hash))
6668 return true;
6669
6670 /* Any syms created from now on start with -1 in
6671 got.refcount/offset and plt.refcount/offset. */
6672 elf_hash_table (info)->init_got_refcount
6673 = elf_hash_table (info)->init_got_offset;
6674 elf_hash_table (info)->init_plt_refcount
6675 = elf_hash_table (info)->init_plt_offset;
6676
6677 bed = get_elf_backend_data (output_bfd);
6678
6679 /* The backend may have to create some sections regardless of whether
6680 we're dynamic or not. */
6681 if (bed->elf_backend_always_size_sections
6682 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6683 return false;
6684
6685 dynobj = elf_hash_table (info)->dynobj;
6686
6687 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6688 {
6689 struct bfd_elf_version_tree *verdefs;
6690 struct elf_info_failed asvinfo;
6691 struct bfd_elf_version_tree *t;
6692 struct bfd_elf_version_expr *d;
6693 asection *s;
6694 size_t soname_indx;
6695
6696 /* If we are supposed to export all symbols into the dynamic symbol
6697 table (this is not the normal case), then do so. */
6698 if (info->export_dynamic
6699 || (bfd_link_executable (info) && info->dynamic))
6700 {
6701 struct elf_info_failed eif;
6702
6703 eif.info = info;
6704 eif.failed = false;
6705 elf_link_hash_traverse (elf_hash_table (info),
6706 _bfd_elf_export_symbol,
6707 &eif);
6708 if (eif.failed)
6709 return false;
6710 }
6711
6712 if (soname != NULL)
6713 {
6714 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6715 soname, true);
6716 if (soname_indx == (size_t) -1
6717 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6718 return false;
6719 }
6720 else
6721 soname_indx = (size_t) -1;
6722
6723 /* Make all global versions with definition. */
6724 for (t = info->version_info; t != NULL; t = t->next)
6725 for (d = t->globals.list; d != NULL; d = d->next)
6726 if (!d->symver && d->literal)
6727 {
6728 const char *verstr, *name;
6729 size_t namelen, verlen, newlen;
6730 char *newname, *p, leading_char;
6731 struct elf_link_hash_entry *newh;
6732
6733 leading_char = bfd_get_symbol_leading_char (output_bfd);
6734 name = d->pattern;
6735 namelen = strlen (name) + (leading_char != '\0');
6736 verstr = t->name;
6737 verlen = strlen (verstr);
6738 newlen = namelen + verlen + 3;
6739
6740 newname = (char *) bfd_malloc (newlen);
6741 if (newname == NULL)
6742 return false;
6743 newname[0] = leading_char;
6744 memcpy (newname + (leading_char != '\0'), name, namelen);
6745
6746 /* Check the hidden versioned definition. */
6747 p = newname + namelen;
6748 *p++ = ELF_VER_CHR;
6749 memcpy (p, verstr, verlen + 1);
6750 newh = elf_link_hash_lookup (elf_hash_table (info),
6751 newname, false, false,
6752 false);
6753 if (newh == NULL
6754 || (newh->root.type != bfd_link_hash_defined
6755 && newh->root.type != bfd_link_hash_defweak))
6756 {
6757 /* Check the default versioned definition. */
6758 *p++ = ELF_VER_CHR;
6759 memcpy (p, verstr, verlen + 1);
6760 newh = elf_link_hash_lookup (elf_hash_table (info),
6761 newname, false, false,
6762 false);
6763 }
6764 free (newname);
6765
6766 /* Mark this version if there is a definition and it is
6767 not defined in a shared object. */
6768 if (newh != NULL
6769 && !newh->def_dynamic
6770 && (newh->root.type == bfd_link_hash_defined
6771 || newh->root.type == bfd_link_hash_defweak))
6772 d->symver = 1;
6773 }
6774
6775 /* Attach all the symbols to their version information. */
6776 asvinfo.info = info;
6777 asvinfo.failed = false;
6778
6779 elf_link_hash_traverse (elf_hash_table (info),
6780 _bfd_elf_link_assign_sym_version,
6781 &asvinfo);
6782 if (asvinfo.failed)
6783 return false;
6784
6785 if (!info->allow_undefined_version)
6786 {
6787 /* Check if all global versions have a definition. */
6788 bool all_defined = true;
6789 for (t = info->version_info; t != NULL; t = t->next)
6790 for (d = t->globals.list; d != NULL; d = d->next)
6791 if (d->literal && !d->symver && !d->script)
6792 {
6793 _bfd_error_handler
6794 (_("%s: undefined version: %s"),
6795 d->pattern, t->name);
6796 all_defined = false;
6797 }
6798
6799 if (!all_defined)
6800 {
6801 bfd_set_error (bfd_error_bad_value);
6802 return false;
6803 }
6804 }
6805
6806 /* Set up the version definition section. */
6807 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6808 BFD_ASSERT (s != NULL);
6809
6810 /* We may have created additional version definitions if we are
6811 just linking a regular application. */
6812 verdefs = info->version_info;
6813
6814 /* Skip anonymous version tag. */
6815 if (verdefs != NULL && verdefs->vernum == 0)
6816 verdefs = verdefs->next;
6817
6818 if (verdefs == NULL && !info->create_default_symver)
6819 s->flags |= SEC_EXCLUDE;
6820 else
6821 {
6822 unsigned int cdefs;
6823 bfd_size_type size;
6824 bfd_byte *p;
6825 Elf_Internal_Verdef def;
6826 Elf_Internal_Verdaux defaux;
6827 struct bfd_link_hash_entry *bh;
6828 struct elf_link_hash_entry *h;
6829 const char *name;
6830
6831 cdefs = 0;
6832 size = 0;
6833
6834 /* Make space for the base version. */
6835 size += sizeof (Elf_External_Verdef);
6836 size += sizeof (Elf_External_Verdaux);
6837 ++cdefs;
6838
6839 /* Make space for the default version. */
6840 if (info->create_default_symver)
6841 {
6842 size += sizeof (Elf_External_Verdef);
6843 ++cdefs;
6844 }
6845
6846 for (t = verdefs; t != NULL; t = t->next)
6847 {
6848 struct bfd_elf_version_deps *n;
6849
6850 /* Don't emit base version twice. */
6851 if (t->vernum == 0)
6852 continue;
6853
6854 size += sizeof (Elf_External_Verdef);
6855 size += sizeof (Elf_External_Verdaux);
6856 ++cdefs;
6857
6858 for (n = t->deps; n != NULL; n = n->next)
6859 size += sizeof (Elf_External_Verdaux);
6860 }
6861
6862 s->size = size;
6863 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6864 if (s->contents == NULL && s->size != 0)
6865 return false;
6866
6867 /* Fill in the version definition section. */
6868
6869 p = s->contents;
6870
6871 def.vd_version = VER_DEF_CURRENT;
6872 def.vd_flags = VER_FLG_BASE;
6873 def.vd_ndx = 1;
6874 def.vd_cnt = 1;
6875 if (info->create_default_symver)
6876 {
6877 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6878 def.vd_next = sizeof (Elf_External_Verdef);
6879 }
6880 else
6881 {
6882 def.vd_aux = sizeof (Elf_External_Verdef);
6883 def.vd_next = (sizeof (Elf_External_Verdef)
6884 + sizeof (Elf_External_Verdaux));
6885 }
6886
6887 if (soname_indx != (size_t) -1)
6888 {
6889 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6890 soname_indx);
6891 def.vd_hash = bfd_elf_hash (soname);
6892 defaux.vda_name = soname_indx;
6893 name = soname;
6894 }
6895 else
6896 {
6897 size_t indx;
6898
6899 name = lbasename (bfd_get_filename (output_bfd));
6900 def.vd_hash = bfd_elf_hash (name);
6901 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6902 name, false);
6903 if (indx == (size_t) -1)
6904 return false;
6905 defaux.vda_name = indx;
6906 }
6907 defaux.vda_next = 0;
6908
6909 _bfd_elf_swap_verdef_out (output_bfd, &def,
6910 (Elf_External_Verdef *) p);
6911 p += sizeof (Elf_External_Verdef);
6912 if (info->create_default_symver)
6913 {
6914 /* Add a symbol representing this version. */
6915 bh = NULL;
6916 if (! (_bfd_generic_link_add_one_symbol
6917 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6918 0, NULL, false,
6919 get_elf_backend_data (dynobj)->collect, &bh)))
6920 return false;
6921 h = (struct elf_link_hash_entry *) bh;
6922 h->non_elf = 0;
6923 h->def_regular = 1;
6924 h->type = STT_OBJECT;
6925 h->verinfo.vertree = NULL;
6926
6927 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6928 return false;
6929
6930 /* Create a duplicate of the base version with the same
6931 aux block, but different flags. */
6932 def.vd_flags = 0;
6933 def.vd_ndx = 2;
6934 def.vd_aux = sizeof (Elf_External_Verdef);
6935 if (verdefs)
6936 def.vd_next = (sizeof (Elf_External_Verdef)
6937 + sizeof (Elf_External_Verdaux));
6938 else
6939 def.vd_next = 0;
6940 _bfd_elf_swap_verdef_out (output_bfd, &def,
6941 (Elf_External_Verdef *) p);
6942 p += sizeof (Elf_External_Verdef);
6943 }
6944 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6945 (Elf_External_Verdaux *) p);
6946 p += sizeof (Elf_External_Verdaux);
6947
6948 for (t = verdefs; t != NULL; t = t->next)
6949 {
6950 unsigned int cdeps;
6951 struct bfd_elf_version_deps *n;
6952
6953 /* Don't emit the base version twice. */
6954 if (t->vernum == 0)
6955 continue;
6956
6957 cdeps = 0;
6958 for (n = t->deps; n != NULL; n = n->next)
6959 ++cdeps;
6960
6961 /* Add a symbol representing this version. */
6962 bh = NULL;
6963 if (! (_bfd_generic_link_add_one_symbol
6964 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6965 0, NULL, false,
6966 get_elf_backend_data (dynobj)->collect, &bh)))
6967 return false;
6968 h = (struct elf_link_hash_entry *) bh;
6969 h->non_elf = 0;
6970 h->def_regular = 1;
6971 h->type = STT_OBJECT;
6972 h->verinfo.vertree = t;
6973
6974 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6975 return false;
6976
6977 def.vd_version = VER_DEF_CURRENT;
6978 def.vd_flags = 0;
6979 if (t->globals.list == NULL
6980 && t->locals.list == NULL
6981 && ! t->used)
6982 def.vd_flags |= VER_FLG_WEAK;
6983 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6984 def.vd_cnt = cdeps + 1;
6985 def.vd_hash = bfd_elf_hash (t->name);
6986 def.vd_aux = sizeof (Elf_External_Verdef);
6987 def.vd_next = 0;
6988
6989 /* If a basever node is next, it *must* be the last node in
6990 the chain, otherwise Verdef construction breaks. */
6991 if (t->next != NULL && t->next->vernum == 0)
6992 BFD_ASSERT (t->next->next == NULL);
6993
6994 if (t->next != NULL && t->next->vernum != 0)
6995 def.vd_next = (sizeof (Elf_External_Verdef)
6996 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6997
6998 _bfd_elf_swap_verdef_out (output_bfd, &def,
6999 (Elf_External_Verdef *) p);
7000 p += sizeof (Elf_External_Verdef);
7001
7002 defaux.vda_name = h->dynstr_index;
7003 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7004 h->dynstr_index);
7005 defaux.vda_next = 0;
7006 if (t->deps != NULL)
7007 defaux.vda_next = sizeof (Elf_External_Verdaux);
7008 t->name_indx = defaux.vda_name;
7009
7010 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
7011 (Elf_External_Verdaux *) p);
7012 p += sizeof (Elf_External_Verdaux);
7013
7014 for (n = t->deps; n != NULL; n = n->next)
7015 {
7016 if (n->version_needed == NULL)
7017 {
7018 /* This can happen if there was an error in the
7019 version script. */
7020 defaux.vda_name = 0;
7021 }
7022 else
7023 {
7024 defaux.vda_name = n->version_needed->name_indx;
7025 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7026 defaux.vda_name);
7027 }
7028 if (n->next == NULL)
7029 defaux.vda_next = 0;
7030 else
7031 defaux.vda_next = sizeof (Elf_External_Verdaux);
7032
7033 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
7034 (Elf_External_Verdaux *) p);
7035 p += sizeof (Elf_External_Verdaux);
7036 }
7037 }
7038
7039 elf_tdata (output_bfd)->cverdefs = cdefs;
7040 }
7041 }
7042
7043 if (info->gc_sections && bed->can_gc_sections)
7044 {
7045 struct elf_gc_sweep_symbol_info sweep_info;
7046
7047 /* Remove the symbols that were in the swept sections from the
7048 dynamic symbol table. */
7049 sweep_info.info = info;
7050 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
7051 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
7052 &sweep_info);
7053 }
7054
7055 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7056 {
7057 asection *s;
7058 struct elf_find_verdep_info sinfo;
7059
7060 /* Work out the size of the version reference section. */
7061
7062 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
7063 BFD_ASSERT (s != NULL);
7064
7065 sinfo.info = info;
7066 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
7067 if (sinfo.vers == 0)
7068 sinfo.vers = 1;
7069 sinfo.failed = false;
7070
7071 elf_link_hash_traverse (elf_hash_table (info),
7072 _bfd_elf_link_find_version_dependencies,
7073 &sinfo);
7074 if (sinfo.failed)
7075 return false;
7076
7077 bed->elf_backend_add_glibc_version_dependency (&sinfo);
7078 if (sinfo.failed)
7079 return false;
7080
7081 if (elf_tdata (output_bfd)->verref == NULL)
7082 s->flags |= SEC_EXCLUDE;
7083 else
7084 {
7085 Elf_Internal_Verneed *vn;
7086 unsigned int size;
7087 unsigned int crefs;
7088 bfd_byte *p;
7089
7090 /* Build the version dependency section. */
7091 size = 0;
7092 crefs = 0;
7093 for (vn = elf_tdata (output_bfd)->verref;
7094 vn != NULL;
7095 vn = vn->vn_nextref)
7096 {
7097 Elf_Internal_Vernaux *a;
7098
7099 size += sizeof (Elf_External_Verneed);
7100 ++crefs;
7101 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7102 size += sizeof (Elf_External_Vernaux);
7103 }
7104
7105 s->size = size;
7106 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7107 if (s->contents == NULL)
7108 return false;
7109
7110 p = s->contents;
7111 for (vn = elf_tdata (output_bfd)->verref;
7112 vn != NULL;
7113 vn = vn->vn_nextref)
7114 {
7115 unsigned int caux;
7116 Elf_Internal_Vernaux *a;
7117 size_t indx;
7118
7119 caux = 0;
7120 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7121 ++caux;
7122
7123 vn->vn_version = VER_NEED_CURRENT;
7124 vn->vn_cnt = caux;
7125 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7126 elf_dt_name (vn->vn_bfd) != NULL
7127 ? elf_dt_name (vn->vn_bfd)
7128 : lbasename (bfd_get_filename
7129 (vn->vn_bfd)),
7130 false);
7131 if (indx == (size_t) -1)
7132 return false;
7133 vn->vn_file = indx;
7134 vn->vn_aux = sizeof (Elf_External_Verneed);
7135 if (vn->vn_nextref == NULL)
7136 vn->vn_next = 0;
7137 else
7138 vn->vn_next = (sizeof (Elf_External_Verneed)
7139 + caux * sizeof (Elf_External_Vernaux));
7140
7141 _bfd_elf_swap_verneed_out (output_bfd, vn,
7142 (Elf_External_Verneed *) p);
7143 p += sizeof (Elf_External_Verneed);
7144
7145 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7146 {
7147 a->vna_hash = bfd_elf_hash (a->vna_nodename);
7148 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7149 a->vna_nodename, false);
7150 if (indx == (size_t) -1)
7151 return false;
7152 a->vna_name = indx;
7153 if (a->vna_nextptr == NULL)
7154 a->vna_next = 0;
7155 else
7156 a->vna_next = sizeof (Elf_External_Vernaux);
7157
7158 _bfd_elf_swap_vernaux_out (output_bfd, a,
7159 (Elf_External_Vernaux *) p);
7160 p += sizeof (Elf_External_Vernaux);
7161 }
7162 }
7163
7164 elf_tdata (output_bfd)->cverrefs = crefs;
7165 }
7166 }
7167
7168 if (bfd_link_relocatable (info)
7169 && !_bfd_elf_size_group_sections (info))
7170 return false;
7171
7172 /* Determine any GNU_STACK segment requirements, after the backend
7173 has had a chance to set a default segment size. */
7174 if (info->execstack)
7175 {
7176 /* If the user has explicitly requested warnings, then generate one even
7177 though the choice is the result of another command line option. */
7178 if (info->warn_execstack == 1)
7179 {
7180 if (info->error_execstack)
7181 {
7182 _bfd_error_handler
7183 (_("\
7184 error: creating an executable stack because of -z execstack command line option"));
7185 return false;
7186 }
7187
7188 _bfd_error_handler
7189 (_("\
7190 warning: enabling an executable stack because of -z execstack command line option"));
7191 }
7192
7193 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
7194 }
7195 else if (info->noexecstack)
7196 elf_stack_flags (output_bfd) = PF_R | PF_W;
7197 else
7198 {
7199 bfd *inputobj;
7200 asection *notesec = NULL;
7201 bfd *noteobj = NULL;
7202 bfd *emptyobj = NULL;
7203 int exec = 0;
7204
7205 for (inputobj = info->input_bfds;
7206 inputobj;
7207 inputobj = inputobj->link.next)
7208 {
7209 asection *s;
7210
7211 if (inputobj->flags
7212 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
7213 continue;
7214 s = inputobj->sections;
7215 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7216 continue;
7217
7218 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
7219 if (s)
7220 {
7221 notesec = s;
7222 if (s->flags & SEC_CODE)
7223 {
7224 noteobj = inputobj;
7225 exec = PF_X;
7226 /* There is no point in scanning the remaining bfds. */
7227 break;
7228 }
7229 }
7230 else if (bed->default_execstack && info->default_execstack)
7231 {
7232 exec = PF_X;
7233 emptyobj = inputobj;
7234 }
7235 }
7236
7237 if (notesec || info->stacksize > 0)
7238 {
7239 if (exec)
7240 {
7241 if (info->warn_execstack != 0)
7242 {
7243 /* PR 29072: Because an executable stack is a serious
7244 security risk, make sure that the user knows that it is
7245 being enabled despite the fact that it was not requested
7246 on the command line. */
7247 if (noteobj)
7248 {
7249 if (info->error_execstack)
7250 {
7251 _bfd_error_handler (_("\
7252 error: %s: is triggering the generation of an executable stack (because it has an executable .note.GNU-stack section)"),
7253 bfd_get_filename (noteobj));
7254 return false;
7255 }
7256
7257 _bfd_error_handler (_("\
7258 warning: %s: requires executable stack (because the .note.GNU-stack section is executable)"),
7259 bfd_get_filename (noteobj));
7260 }
7261 else if (emptyobj)
7262 {
7263 if (info->error_execstack)
7264 {
7265 _bfd_error_handler (_("\
7266 error: %s: is triggering the generation of an executable stack because it does not have a .note.GNU-stack section"),
7267 bfd_get_filename (emptyobj));
7268 return false;
7269 }
7270
7271 _bfd_error_handler (_("\
7272 warning: %s: missing .note.GNU-stack section implies executable stack"),
7273 bfd_get_filename (emptyobj));
7274 _bfd_error_handler (_("\
7275 NOTE: This behaviour is deprecated and will be removed in a future version of the linker"));
7276 }
7277 }
7278 }
7279 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
7280 }
7281
7282 if (notesec && exec && bfd_link_relocatable (info)
7283 && notesec->output_section != bfd_abs_section_ptr)
7284 notesec->output_section->flags |= SEC_CODE;
7285 }
7286
7287 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7288 {
7289 struct elf_info_failed eif;
7290 struct elf_link_hash_entry *h;
7291 asection *dynstr;
7292 asection *s;
7293
7294 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
7295 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
7296
7297 if (info->symbolic)
7298 {
7299 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
7300 return false;
7301 info->flags |= DF_SYMBOLIC;
7302 }
7303
7304 if (rpath != NULL)
7305 {
7306 size_t indx;
7307 bfd_vma tag;
7308
7309 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
7310 true);
7311 if (indx == (size_t) -1)
7312 return false;
7313
7314 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
7315 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
7316 return false;
7317 }
7318
7319 if (filter_shlib != NULL)
7320 {
7321 size_t indx;
7322
7323 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7324 filter_shlib, true);
7325 if (indx == (size_t) -1
7326 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
7327 return false;
7328 }
7329
7330 if (auxiliary_filters != NULL)
7331 {
7332 const char * const *p;
7333
7334 for (p = auxiliary_filters; *p != NULL; p++)
7335 {
7336 size_t indx;
7337
7338 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7339 *p, true);
7340 if (indx == (size_t) -1
7341 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7342 return false;
7343 }
7344 }
7345
7346 if (audit != NULL)
7347 {
7348 size_t indx;
7349
7350 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7351 true);
7352 if (indx == (size_t) -1
7353 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7354 return false;
7355 }
7356
7357 if (depaudit != NULL)
7358 {
7359 size_t indx;
7360
7361 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7362 true);
7363 if (indx == (size_t) -1
7364 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7365 return false;
7366 }
7367
7368 eif.info = info;
7369 eif.failed = false;
7370
7371 /* Find all symbols which were defined in a dynamic object and make
7372 the backend pick a reasonable value for them. */
7373 elf_link_hash_traverse (elf_hash_table (info),
7374 _bfd_elf_adjust_dynamic_symbol,
7375 &eif);
7376 if (eif.failed)
7377 return false;
7378
7379 /* Add some entries to the .dynamic section. We fill in some of the
7380 values later, in bfd_elf_final_link, but we must add the entries
7381 now so that we know the final size of the .dynamic section. */
7382
7383 /* If there are initialization and/or finalization functions to
7384 call then add the corresponding DT_INIT/DT_FINI entries. */
7385 h = (info->init_function
7386 ? elf_link_hash_lookup (elf_hash_table (info),
7387 info->init_function, false,
7388 false, false)
7389 : NULL);
7390 if (h != NULL
7391 && (h->ref_regular
7392 || h->def_regular))
7393 {
7394 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7395 return false;
7396 }
7397 h = (info->fini_function
7398 ? elf_link_hash_lookup (elf_hash_table (info),
7399 info->fini_function, false,
7400 false, false)
7401 : NULL);
7402 if (h != NULL
7403 && (h->ref_regular
7404 || h->def_regular))
7405 {
7406 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7407 return false;
7408 }
7409
7410 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7411 if (s != NULL && s->linker_has_input)
7412 {
7413 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7414 if (! bfd_link_executable (info))
7415 {
7416 bfd *sub;
7417 asection *o;
7418
7419 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7420 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7421 && (o = sub->sections) != NULL
7422 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
7423 for (o = sub->sections; o != NULL; o = o->next)
7424 if (elf_section_data (o)->this_hdr.sh_type
7425 == SHT_PREINIT_ARRAY)
7426 {
7427 _bfd_error_handler
7428 (_("%pB: .preinit_array section is not allowed in DSO"),
7429 sub);
7430 break;
7431 }
7432
7433 bfd_set_error (bfd_error_nonrepresentable_section);
7434 return false;
7435 }
7436
7437 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7438 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7439 return false;
7440 }
7441 s = bfd_get_section_by_name (output_bfd, ".init_array");
7442 if (s != NULL && s->linker_has_input)
7443 {
7444 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7445 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7446 return false;
7447 }
7448 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7449 if (s != NULL && s->linker_has_input)
7450 {
7451 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7452 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7453 return false;
7454 }
7455
7456 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7457 /* If .dynstr is excluded from the link, we don't want any of
7458 these tags. Strictly, we should be checking each section
7459 individually; This quick check covers for the case where
7460 someone does a /DISCARD/ : { *(*) }. */
7461 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7462 {
7463 bfd_size_type strsize;
7464
7465 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7466 if ((info->emit_hash
7467 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7468 || (info->emit_gnu_hash
7469 && (bed->record_xhash_symbol == NULL
7470 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7471 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7472 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7473 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7474 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7475 bed->s->sizeof_sym)
7476 || (info->gnu_flags_1
7477 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1,
7478 info->gnu_flags_1)))
7479 return false;
7480 }
7481 }
7482
7483 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7484 return false;
7485
7486 /* The backend must work out the sizes of all the other dynamic
7487 sections. */
7488 if (dynobj != NULL
7489 && bed->elf_backend_size_dynamic_sections != NULL
7490 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
7491 return false;
7492
7493 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7494 {
7495 if (elf_tdata (output_bfd)->cverdefs)
7496 {
7497 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7498
7499 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7500 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7501 return false;
7502 }
7503
7504 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7505 {
7506 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7507 return false;
7508 }
7509 else if (info->flags & DF_BIND_NOW)
7510 {
7511 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7512 return false;
7513 }
7514
7515 if (info->flags_1)
7516 {
7517 if (bfd_link_executable (info))
7518 info->flags_1 &= ~ (DF_1_INITFIRST
7519 | DF_1_NODELETE
7520 | DF_1_NOOPEN);
7521 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7522 return false;
7523 }
7524
7525 if (elf_tdata (output_bfd)->cverrefs)
7526 {
7527 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7528
7529 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7530 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7531 return false;
7532 }
7533
7534 if ((elf_tdata (output_bfd)->cverrefs == 0
7535 && elf_tdata (output_bfd)->cverdefs == 0)
7536 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7537 {
7538 asection *s;
7539
7540 s = bfd_get_linker_section (dynobj, ".gnu.version");
7541 s->flags |= SEC_EXCLUDE;
7542 }
7543 }
7544 return true;
7545 }
7546
7547 /* Find the first non-excluded output section. We'll use its
7548 section symbol for some emitted relocs. */
7549 void
_bfd_elf_init_1_index_section(bfd * output_bfd,struct bfd_link_info * info)7550 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7551 {
7552 asection *s;
7553 asection *found = NULL;
7554
7555 for (s = output_bfd->sections; s != NULL; s = s->next)
7556 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7557 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7558 {
7559 found = s;
7560 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7561 break;
7562 }
7563 elf_hash_table (info)->text_index_section = found;
7564 }
7565
7566 /* Find two non-excluded output sections, one for code, one for data.
7567 We'll use their section symbols for some emitted relocs. */
7568 void
_bfd_elf_init_2_index_sections(bfd * output_bfd,struct bfd_link_info * info)7569 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7570 {
7571 asection *s;
7572 asection *found = NULL;
7573
7574 /* Data first, since setting text_index_section changes
7575 _bfd_elf_omit_section_dynsym_default. */
7576 for (s = output_bfd->sections; s != NULL; s = s->next)
7577 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7578 && !(s->flags & SEC_READONLY)
7579 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7580 {
7581 found = s;
7582 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7583 break;
7584 }
7585 elf_hash_table (info)->data_index_section = found;
7586
7587 for (s = output_bfd->sections; s != NULL; s = s->next)
7588 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7589 && (s->flags & SEC_READONLY)
7590 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7591 {
7592 found = s;
7593 break;
7594 }
7595 elf_hash_table (info)->text_index_section = found;
7596 }
7597
7598 #define GNU_HASH_SECTION_NAME(bed) \
7599 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7600
7601 bool
bfd_elf_size_dynsym_hash_dynstr(bfd * output_bfd,struct bfd_link_info * info)7602 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7603 {
7604 const struct elf_backend_data *bed;
7605 unsigned long section_sym_count;
7606 bfd_size_type dynsymcount = 0;
7607
7608 if (!is_elf_hash_table (info->hash))
7609 return true;
7610
7611 bed = get_elf_backend_data (output_bfd);
7612 (*bed->elf_backend_init_index_section) (output_bfd, info);
7613
7614 /* Assign dynsym indices. In a shared library we generate a section
7615 symbol for each output section, which come first. Next come all
7616 of the back-end allocated local dynamic syms, followed by the rest
7617 of the global symbols.
7618
7619 This is usually not needed for static binaries, however backends
7620 can request to always do it, e.g. the MIPS backend uses dynamic
7621 symbol counts to lay out GOT, which will be produced in the
7622 presence of GOT relocations even in static binaries (holding fixed
7623 data in that case, to satisfy those relocations). */
7624
7625 if (elf_hash_table (info)->dynamic_sections_created
7626 || bed->always_renumber_dynsyms)
7627 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7628 §ion_sym_count);
7629
7630 if (elf_hash_table (info)->dynamic_sections_created)
7631 {
7632 bfd *dynobj;
7633 asection *s;
7634 unsigned int dtagcount;
7635
7636 dynobj = elf_hash_table (info)->dynobj;
7637
7638 /* Work out the size of the symbol version section. */
7639 s = bfd_get_linker_section (dynobj, ".gnu.version");
7640 BFD_ASSERT (s != NULL);
7641 if ((s->flags & SEC_EXCLUDE) == 0)
7642 {
7643 s->size = dynsymcount * sizeof (Elf_External_Versym);
7644 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7645 if (s->contents == NULL)
7646 return false;
7647
7648 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7649 return false;
7650 }
7651
7652 /* Set the size of the .dynsym and .hash sections. We counted
7653 the number of dynamic symbols in elf_link_add_object_symbols.
7654 We will build the contents of .dynsym and .hash when we build
7655 the final symbol table, because until then we do not know the
7656 correct value to give the symbols. We built the .dynstr
7657 section as we went along in elf_link_add_object_symbols. */
7658 s = elf_hash_table (info)->dynsym;
7659 BFD_ASSERT (s != NULL);
7660 s->size = dynsymcount * bed->s->sizeof_sym;
7661
7662 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7663 if (s->contents == NULL)
7664 return false;
7665
7666 /* The first entry in .dynsym is a dummy symbol. Clear all the
7667 section syms, in case we don't output them all. */
7668 ++section_sym_count;
7669 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7670
7671 elf_hash_table (info)->bucketcount = 0;
7672
7673 /* Compute the size of the hashing table. As a side effect this
7674 computes the hash values for all the names we export. */
7675 if (info->emit_hash)
7676 {
7677 unsigned long int *hashcodes;
7678 struct hash_codes_info hashinf;
7679 bfd_size_type amt;
7680 unsigned long int nsyms;
7681 size_t bucketcount;
7682 size_t hash_entry_size;
7683
7684 /* Compute the hash values for all exported symbols. At the same
7685 time store the values in an array so that we could use them for
7686 optimizations. */
7687 amt = dynsymcount * sizeof (unsigned long int);
7688 hashcodes = (unsigned long int *) bfd_malloc (amt);
7689 if (hashcodes == NULL)
7690 return false;
7691 hashinf.hashcodes = hashcodes;
7692 hashinf.error = false;
7693
7694 /* Put all hash values in HASHCODES. */
7695 elf_link_hash_traverse (elf_hash_table (info),
7696 elf_collect_hash_codes, &hashinf);
7697 if (hashinf.error)
7698 {
7699 free (hashcodes);
7700 return false;
7701 }
7702
7703 nsyms = hashinf.hashcodes - hashcodes;
7704 bucketcount
7705 = compute_bucket_count (info, hashcodes, nsyms, 0);
7706 free (hashcodes);
7707
7708 if (bucketcount == 0 && nsyms > 0)
7709 return false;
7710
7711 elf_hash_table (info)->bucketcount = bucketcount;
7712
7713 s = bfd_get_linker_section (dynobj, ".hash");
7714 BFD_ASSERT (s != NULL);
7715 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7716 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7717 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7718 if (s->contents == NULL)
7719 return false;
7720
7721 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7722 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7723 s->contents + hash_entry_size);
7724 }
7725
7726 if (info->emit_gnu_hash)
7727 {
7728 size_t i, cnt;
7729 unsigned char *contents;
7730 struct collect_gnu_hash_codes cinfo;
7731 bfd_size_type amt;
7732 size_t bucketcount;
7733
7734 memset (&cinfo, 0, sizeof (cinfo));
7735
7736 /* Compute the hash values for all exported symbols. At the same
7737 time store the values in an array so that we could use them for
7738 optimizations. */
7739 amt = dynsymcount * 2 * sizeof (unsigned long int);
7740 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7741 if (cinfo.hashcodes == NULL)
7742 return false;
7743
7744 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7745 cinfo.min_dynindx = -1;
7746 cinfo.output_bfd = output_bfd;
7747 cinfo.bed = bed;
7748
7749 /* Put all hash values in HASHCODES. */
7750 elf_link_hash_traverse (elf_hash_table (info),
7751 elf_collect_gnu_hash_codes, &cinfo);
7752 if (cinfo.error)
7753 {
7754 free (cinfo.hashcodes);
7755 return false;
7756 }
7757
7758 bucketcount
7759 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7760
7761 if (bucketcount == 0)
7762 {
7763 free (cinfo.hashcodes);
7764 return false;
7765 }
7766
7767 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
7768 BFD_ASSERT (s != NULL);
7769
7770 if (cinfo.nsyms == 0)
7771 {
7772 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7773 BFD_ASSERT (cinfo.min_dynindx == -1);
7774 free (cinfo.hashcodes);
7775 s->size = 5 * 4 + bed->s->arch_size / 8;
7776 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7777 if (contents == NULL)
7778 return false;
7779 s->contents = contents;
7780 /* 1 empty bucket. */
7781 bfd_put_32 (output_bfd, 1, contents);
7782 /* SYMIDX above the special symbol 0. */
7783 bfd_put_32 (output_bfd, 1, contents + 4);
7784 /* Just one word for bitmask. */
7785 bfd_put_32 (output_bfd, 1, contents + 8);
7786 /* Only hash fn bloom filter. */
7787 bfd_put_32 (output_bfd, 0, contents + 12);
7788 /* No hashes are valid - empty bitmask. */
7789 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7790 /* No hashes in the only bucket. */
7791 bfd_put_32 (output_bfd, 0,
7792 contents + 16 + bed->s->arch_size / 8);
7793 }
7794 else
7795 {
7796 unsigned long int maskwords, maskbitslog2, x;
7797 BFD_ASSERT (cinfo.min_dynindx != -1);
7798
7799 x = cinfo.nsyms;
7800 maskbitslog2 = 1;
7801 while ((x >>= 1) != 0)
7802 ++maskbitslog2;
7803 if (maskbitslog2 < 3)
7804 maskbitslog2 = 5;
7805 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7806 maskbitslog2 = maskbitslog2 + 3;
7807 else
7808 maskbitslog2 = maskbitslog2 + 2;
7809 if (bed->s->arch_size == 64)
7810 {
7811 if (maskbitslog2 == 5)
7812 maskbitslog2 = 6;
7813 cinfo.shift1 = 6;
7814 }
7815 else
7816 cinfo.shift1 = 5;
7817 cinfo.mask = (1 << cinfo.shift1) - 1;
7818 cinfo.shift2 = maskbitslog2;
7819 cinfo.maskbits = 1 << maskbitslog2;
7820 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7821 amt = bucketcount * sizeof (unsigned long int) * 2;
7822 amt += maskwords * sizeof (bfd_vma);
7823 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7824 if (cinfo.bitmask == NULL)
7825 {
7826 free (cinfo.hashcodes);
7827 return false;
7828 }
7829
7830 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7831 cinfo.indx = cinfo.counts + bucketcount;
7832 cinfo.symindx = dynsymcount - cinfo.nsyms;
7833 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7834
7835 /* Determine how often each hash bucket is used. */
7836 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7837 for (i = 0; i < cinfo.nsyms; ++i)
7838 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7839
7840 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7841 if (cinfo.counts[i] != 0)
7842 {
7843 cinfo.indx[i] = cnt;
7844 cnt += cinfo.counts[i];
7845 }
7846 BFD_ASSERT (cnt == dynsymcount);
7847 cinfo.bucketcount = bucketcount;
7848 cinfo.local_indx = cinfo.min_dynindx;
7849
7850 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7851 s->size += cinfo.maskbits / 8;
7852 if (bed->record_xhash_symbol != NULL)
7853 s->size += cinfo.nsyms * 4;
7854 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7855 if (contents == NULL)
7856 {
7857 free (cinfo.bitmask);
7858 free (cinfo.hashcodes);
7859 return false;
7860 }
7861
7862 s->contents = contents;
7863 bfd_put_32 (output_bfd, bucketcount, contents);
7864 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7865 bfd_put_32 (output_bfd, maskwords, contents + 8);
7866 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7867 contents += 16 + cinfo.maskbits / 8;
7868
7869 for (i = 0; i < bucketcount; ++i)
7870 {
7871 if (cinfo.counts[i] == 0)
7872 bfd_put_32 (output_bfd, 0, contents);
7873 else
7874 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7875 contents += 4;
7876 }
7877
7878 cinfo.contents = contents;
7879
7880 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7881 /* Renumber dynamic symbols, if populating .gnu.hash section.
7882 If using .MIPS.xhash, populate the translation table. */
7883 elf_link_hash_traverse (elf_hash_table (info),
7884 elf_gnu_hash_process_symidx, &cinfo);
7885
7886 contents = s->contents + 16;
7887 for (i = 0; i < maskwords; ++i)
7888 {
7889 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7890 contents);
7891 contents += bed->s->arch_size / 8;
7892 }
7893
7894 free (cinfo.bitmask);
7895 free (cinfo.hashcodes);
7896 }
7897 }
7898
7899 s = bfd_get_linker_section (dynobj, ".dynstr");
7900 BFD_ASSERT (s != NULL);
7901
7902 elf_finalize_dynstr (output_bfd, info);
7903
7904 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7905
7906 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7907 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7908 return false;
7909 }
7910
7911 return true;
7912 }
7913
7914 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7915
7916 static void
merge_sections_remove_hook(bfd * abfd ATTRIBUTE_UNUSED,asection * sec)7917 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7918 asection *sec)
7919 {
7920 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7921 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7922 }
7923
7924 /* Finish SHF_MERGE section merging. */
7925
7926 bool
_bfd_elf_merge_sections(bfd * obfd,struct bfd_link_info * info)7927 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7928 {
7929 bfd *ibfd;
7930 asection *sec;
7931
7932 if (!is_elf_hash_table (info->hash))
7933 return false;
7934
7935 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7936 if ((ibfd->flags & DYNAMIC) == 0
7937 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7938 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7939 == get_elf_backend_data (obfd)->s->elfclass))
7940 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7941 if ((sec->flags & SEC_MERGE) != 0
7942 && !bfd_is_abs_section (sec->output_section))
7943 {
7944 struct bfd_elf_section_data *secdata;
7945
7946 secdata = elf_section_data (sec);
7947 if (! _bfd_add_merge_section (obfd,
7948 &elf_hash_table (info)->merge_info,
7949 sec, &secdata->sec_info))
7950 return false;
7951 else if (secdata->sec_info)
7952 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7953 }
7954
7955 if (elf_hash_table (info)->merge_info != NULL)
7956 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7957 merge_sections_remove_hook);
7958 return true;
7959 }
7960
7961 /* Create an entry in an ELF linker hash table. */
7962
7963 struct bfd_hash_entry *
_bfd_elf_link_hash_newfunc(struct bfd_hash_entry * entry,struct bfd_hash_table * table,const char * string)7964 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7965 struct bfd_hash_table *table,
7966 const char *string)
7967 {
7968 /* Allocate the structure if it has not already been allocated by a
7969 subclass. */
7970 if (entry == NULL)
7971 {
7972 entry = (struct bfd_hash_entry *)
7973 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7974 if (entry == NULL)
7975 return entry;
7976 }
7977
7978 /* Call the allocation method of the superclass. */
7979 entry = _bfd_link_hash_newfunc (entry, table, string);
7980 if (entry != NULL)
7981 {
7982 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7983 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7984
7985 /* Set local fields. */
7986 ret->indx = -1;
7987 ret->dynindx = -1;
7988 ret->got = htab->init_got_refcount;
7989 ret->plt = htab->init_plt_refcount;
7990 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7991 - offsetof (struct elf_link_hash_entry, size)));
7992 /* Assume that we have been called by a non-ELF symbol reader.
7993 This flag is then reset by the code which reads an ELF input
7994 file. This ensures that a symbol created by a non-ELF symbol
7995 reader will have the flag set correctly. */
7996 ret->non_elf = 1;
7997 }
7998
7999 return entry;
8000 }
8001
8002 /* Copy data from an indirect symbol to its direct symbol, hiding the
8003 old indirect symbol. Also used for copying flags to a weakdef. */
8004
8005 void
_bfd_elf_link_hash_copy_indirect(struct bfd_link_info * info,struct elf_link_hash_entry * dir,struct elf_link_hash_entry * ind)8006 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
8007 struct elf_link_hash_entry *dir,
8008 struct elf_link_hash_entry *ind)
8009 {
8010 struct elf_link_hash_table *htab;
8011
8012 if (ind->dyn_relocs != NULL)
8013 {
8014 if (dir->dyn_relocs != NULL)
8015 {
8016 struct elf_dyn_relocs **pp;
8017 struct elf_dyn_relocs *p;
8018
8019 /* Add reloc counts against the indirect sym to the direct sym
8020 list. Merge any entries against the same section. */
8021 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
8022 {
8023 struct elf_dyn_relocs *q;
8024
8025 for (q = dir->dyn_relocs; q != NULL; q = q->next)
8026 if (q->sec == p->sec)
8027 {
8028 q->pc_count += p->pc_count;
8029 q->count += p->count;
8030 *pp = p->next;
8031 break;
8032 }
8033 if (q == NULL)
8034 pp = &p->next;
8035 }
8036 *pp = dir->dyn_relocs;
8037 }
8038
8039 dir->dyn_relocs = ind->dyn_relocs;
8040 ind->dyn_relocs = NULL;
8041 }
8042
8043 /* Copy down any references that we may have already seen to the
8044 symbol which just became indirect. */
8045
8046 if (dir->versioned != versioned_hidden)
8047 dir->ref_dynamic |= ind->ref_dynamic;
8048 dir->ref_regular |= ind->ref_regular;
8049 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
8050 dir->non_got_ref |= ind->non_got_ref;
8051 dir->needs_plt |= ind->needs_plt;
8052 dir->pointer_equality_needed |= ind->pointer_equality_needed;
8053
8054 if (ind->root.type != bfd_link_hash_indirect)
8055 return;
8056
8057 /* Copy over the global and procedure linkage table refcount entries.
8058 These may have been already set up by a check_relocs routine. */
8059 htab = elf_hash_table (info);
8060 if (ind->got.refcount > htab->init_got_refcount.refcount)
8061 {
8062 if (dir->got.refcount < 0)
8063 dir->got.refcount = 0;
8064 dir->got.refcount += ind->got.refcount;
8065 ind->got.refcount = htab->init_got_refcount.refcount;
8066 }
8067
8068 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
8069 {
8070 if (dir->plt.refcount < 0)
8071 dir->plt.refcount = 0;
8072 dir->plt.refcount += ind->plt.refcount;
8073 ind->plt.refcount = htab->init_plt_refcount.refcount;
8074 }
8075
8076 if (ind->dynindx != -1)
8077 {
8078 if (dir->dynindx != -1)
8079 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
8080 dir->dynindx = ind->dynindx;
8081 dir->dynstr_index = ind->dynstr_index;
8082 ind->dynindx = -1;
8083 ind->dynstr_index = 0;
8084 }
8085 }
8086
8087 void
_bfd_elf_link_hash_hide_symbol(struct bfd_link_info * info,struct elf_link_hash_entry * h,bool force_local)8088 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
8089 struct elf_link_hash_entry *h,
8090 bool force_local)
8091 {
8092 /* STT_GNU_IFUNC symbol must go through PLT. */
8093 if (h->type != STT_GNU_IFUNC)
8094 {
8095 h->plt = elf_hash_table (info)->init_plt_offset;
8096 h->needs_plt = 0;
8097 }
8098 if (force_local)
8099 {
8100 h->forced_local = 1;
8101 if (h->dynindx != -1)
8102 {
8103 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8104 h->dynstr_index);
8105 h->dynindx = -1;
8106 h->dynstr_index = 0;
8107 }
8108 }
8109 }
8110
8111 /* Hide a symbol. */
8112
8113 void
_bfd_elf_link_hide_symbol(bfd * output_bfd,struct bfd_link_info * info,struct bfd_link_hash_entry * h)8114 _bfd_elf_link_hide_symbol (bfd *output_bfd,
8115 struct bfd_link_info *info,
8116 struct bfd_link_hash_entry *h)
8117 {
8118 if (is_elf_hash_table (info->hash))
8119 {
8120 const struct elf_backend_data *bed
8121 = get_elf_backend_data (output_bfd);
8122 struct elf_link_hash_entry *eh
8123 = (struct elf_link_hash_entry *) h;
8124 bed->elf_backend_hide_symbol (info, eh, true);
8125 eh->def_dynamic = 0;
8126 eh->ref_dynamic = 0;
8127 eh->dynamic_def = 0;
8128 }
8129 }
8130
8131 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
8132 caller. */
8133
8134 bool
_bfd_elf_link_hash_table_init(struct elf_link_hash_table * table,bfd * abfd,struct bfd_hash_entry * (* newfunc)(struct bfd_hash_entry *,struct bfd_hash_table *,const char *),unsigned int entsize,enum elf_target_id target_id)8135 _bfd_elf_link_hash_table_init
8136 (struct elf_link_hash_table *table,
8137 bfd *abfd,
8138 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
8139 struct bfd_hash_table *,
8140 const char *),
8141 unsigned int entsize,
8142 enum elf_target_id target_id)
8143 {
8144 bool ret;
8145 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
8146
8147 table->init_got_refcount.refcount = can_refcount - 1;
8148 table->init_plt_refcount.refcount = can_refcount - 1;
8149 table->init_got_offset.offset = -(bfd_vma) 1;
8150 table->init_plt_offset.offset = -(bfd_vma) 1;
8151 /* The first dynamic symbol is a dummy. */
8152 table->dynsymcount = 1;
8153
8154 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
8155
8156 table->root.type = bfd_link_elf_hash_table;
8157 table->hash_table_id = target_id;
8158 table->target_os = get_elf_backend_data (abfd)->target_os;
8159
8160 return ret;
8161 }
8162
8163 /* Create an ELF linker hash table. */
8164
8165 struct bfd_link_hash_table *
_bfd_elf_link_hash_table_create(bfd * abfd)8166 _bfd_elf_link_hash_table_create (bfd *abfd)
8167 {
8168 struct elf_link_hash_table *ret;
8169 size_t amt = sizeof (struct elf_link_hash_table);
8170
8171 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
8172 if (ret == NULL)
8173 return NULL;
8174
8175 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
8176 sizeof (struct elf_link_hash_entry),
8177 GENERIC_ELF_DATA))
8178 {
8179 free (ret);
8180 return NULL;
8181 }
8182 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
8183
8184 return &ret->root;
8185 }
8186
8187 /* Destroy an ELF linker hash table. */
8188
8189 void
_bfd_elf_link_hash_table_free(bfd * obfd)8190 _bfd_elf_link_hash_table_free (bfd *obfd)
8191 {
8192 struct elf_link_hash_table *htab;
8193
8194 htab = (struct elf_link_hash_table *) obfd->link.hash;
8195 if (htab->dynstr != NULL)
8196 _bfd_elf_strtab_free (htab->dynstr);
8197 _bfd_merge_sections_free (htab->merge_info);
8198 _bfd_generic_link_hash_table_free (obfd);
8199 }
8200
8201 /* This is a hook for the ELF emulation code in the generic linker to
8202 tell the backend linker what file name to use for the DT_NEEDED
8203 entry for a dynamic object. */
8204
8205 void
bfd_elf_set_dt_needed_name(bfd * abfd,const char * name)8206 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
8207 {
8208 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8209 && bfd_get_format (abfd) == bfd_object)
8210 elf_dt_name (abfd) = name;
8211 }
8212
8213 int
bfd_elf_get_dyn_lib_class(bfd * abfd)8214 bfd_elf_get_dyn_lib_class (bfd *abfd)
8215 {
8216 int lib_class;
8217 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8218 && bfd_get_format (abfd) == bfd_object)
8219 lib_class = elf_dyn_lib_class (abfd);
8220 else
8221 lib_class = 0;
8222 return lib_class;
8223 }
8224
8225 void
bfd_elf_set_dyn_lib_class(bfd * abfd,enum dynamic_lib_link_class lib_class)8226 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
8227 {
8228 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8229 && bfd_get_format (abfd) == bfd_object)
8230 elf_dyn_lib_class (abfd) = lib_class;
8231 }
8232
8233 /* Get the list of DT_NEEDED entries for a link. This is a hook for
8234 the linker ELF emulation code. */
8235
8236 struct bfd_link_needed_list *
bfd_elf_get_needed_list(bfd * abfd ATTRIBUTE_UNUSED,struct bfd_link_info * info)8237 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
8238 struct bfd_link_info *info)
8239 {
8240 if (! is_elf_hash_table (info->hash))
8241 return NULL;
8242 return elf_hash_table (info)->needed;
8243 }
8244
8245 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
8246 hook for the linker ELF emulation code. */
8247
8248 struct bfd_link_needed_list *
bfd_elf_get_runpath_list(bfd * abfd ATTRIBUTE_UNUSED,struct bfd_link_info * info)8249 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
8250 struct bfd_link_info *info)
8251 {
8252 if (! is_elf_hash_table (info->hash))
8253 return NULL;
8254 return elf_hash_table (info)->runpath;
8255 }
8256
8257 /* Get the name actually used for a dynamic object for a link. This
8258 is the SONAME entry if there is one. Otherwise, it is the string
8259 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
8260
8261 const char *
bfd_elf_get_dt_soname(bfd * abfd)8262 bfd_elf_get_dt_soname (bfd *abfd)
8263 {
8264 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8265 && bfd_get_format (abfd) == bfd_object)
8266 return elf_dt_name (abfd);
8267 return NULL;
8268 }
8269
8270 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
8271 the ELF linker emulation code. */
8272
8273 bool
bfd_elf_get_bfd_needed_list(bfd * abfd,struct bfd_link_needed_list ** pneeded)8274 bfd_elf_get_bfd_needed_list (bfd *abfd,
8275 struct bfd_link_needed_list **pneeded)
8276 {
8277 asection *s;
8278 bfd_byte *dynbuf = NULL;
8279 unsigned int elfsec;
8280 unsigned long shlink;
8281 bfd_byte *extdyn, *extdynend;
8282 size_t extdynsize;
8283 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
8284
8285 *pneeded = NULL;
8286
8287 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
8288 || bfd_get_format (abfd) != bfd_object)
8289 return true;
8290
8291 s = bfd_get_section_by_name (abfd, ".dynamic");
8292 if (s == NULL || s->size == 0 || (s->flags & SEC_HAS_CONTENTS) == 0)
8293 return true;
8294
8295 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
8296 goto error_return;
8297
8298 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
8299 if (elfsec == SHN_BAD)
8300 goto error_return;
8301
8302 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
8303
8304 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
8305 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
8306
8307 for (extdyn = dynbuf, extdynend = dynbuf + s->size;
8308 (size_t) (extdynend - extdyn) >= extdynsize;
8309 extdyn += extdynsize)
8310 {
8311 Elf_Internal_Dyn dyn;
8312
8313 (*swap_dyn_in) (abfd, extdyn, &dyn);
8314
8315 if (dyn.d_tag == DT_NULL)
8316 break;
8317
8318 if (dyn.d_tag == DT_NEEDED)
8319 {
8320 const char *string;
8321 struct bfd_link_needed_list *l;
8322 unsigned int tagv = dyn.d_un.d_val;
8323 size_t amt;
8324
8325 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
8326 if (string == NULL)
8327 goto error_return;
8328
8329 amt = sizeof *l;
8330 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
8331 if (l == NULL)
8332 goto error_return;
8333
8334 l->by = abfd;
8335 l->name = string;
8336 l->next = *pneeded;
8337 *pneeded = l;
8338 }
8339 }
8340
8341 free (dynbuf);
8342
8343 return true;
8344
8345 error_return:
8346 free (dynbuf);
8347 return false;
8348 }
8349
8350 struct elf_symbuf_symbol
8351 {
8352 unsigned long st_name; /* Symbol name, index in string tbl */
8353 unsigned char st_info; /* Type and binding attributes */
8354 unsigned char st_other; /* Visibilty, and target specific */
8355 };
8356
8357 struct elf_symbuf_head
8358 {
8359 struct elf_symbuf_symbol *ssym;
8360 size_t count;
8361 unsigned int st_shndx;
8362 };
8363
8364 struct elf_symbol
8365 {
8366 union
8367 {
8368 Elf_Internal_Sym *isym;
8369 struct elf_symbuf_symbol *ssym;
8370 void *p;
8371 } u;
8372 const char *name;
8373 };
8374
8375 /* Sort references to symbols by ascending section number. */
8376
8377 static int
elf_sort_elf_symbol(const void * arg1,const void * arg2)8378 elf_sort_elf_symbol (const void *arg1, const void *arg2)
8379 {
8380 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8381 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8382
8383 if (s1->st_shndx != s2->st_shndx)
8384 return s1->st_shndx > s2->st_shndx ? 1 : -1;
8385 /* Final sort by the address of the sym in the symbuf ensures
8386 a stable sort. */
8387 if (s1 != s2)
8388 return s1 > s2 ? 1 : -1;
8389 return 0;
8390 }
8391
8392 static int
elf_sym_name_compare(const void * arg1,const void * arg2)8393 elf_sym_name_compare (const void *arg1, const void *arg2)
8394 {
8395 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8396 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
8397 int ret = strcmp (s1->name, s2->name);
8398 if (ret != 0)
8399 return ret;
8400 if (s1->u.p != s2->u.p)
8401 return s1->u.p > s2->u.p ? 1 : -1;
8402 return 0;
8403 }
8404
8405 static struct elf_symbuf_head *
elf_create_symbuf(size_t symcount,Elf_Internal_Sym * isymbuf)8406 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
8407 {
8408 Elf_Internal_Sym **ind, **indbufend, **indbuf;
8409 struct elf_symbuf_symbol *ssym;
8410 struct elf_symbuf_head *ssymbuf, *ssymhead;
8411 size_t i, shndx_count, total_size, amt;
8412
8413 amt = symcount * sizeof (*indbuf);
8414 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
8415 if (indbuf == NULL)
8416 return NULL;
8417
8418 for (ind = indbuf, i = 0; i < symcount; i++)
8419 if (isymbuf[i].st_shndx != SHN_UNDEF)
8420 *ind++ = &isymbuf[i];
8421 indbufend = ind;
8422
8423 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8424 elf_sort_elf_symbol);
8425
8426 shndx_count = 0;
8427 if (indbufend > indbuf)
8428 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8429 if (ind[0]->st_shndx != ind[1]->st_shndx)
8430 shndx_count++;
8431
8432 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8433 + (indbufend - indbuf) * sizeof (*ssym));
8434 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
8435 if (ssymbuf == NULL)
8436 {
8437 free (indbuf);
8438 return NULL;
8439 }
8440
8441 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
8442 ssymbuf->ssym = NULL;
8443 ssymbuf->count = shndx_count;
8444 ssymbuf->st_shndx = 0;
8445 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8446 {
8447 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8448 {
8449 ssymhead++;
8450 ssymhead->ssym = ssym;
8451 ssymhead->count = 0;
8452 ssymhead->st_shndx = (*ind)->st_shndx;
8453 }
8454 ssym->st_name = (*ind)->st_name;
8455 ssym->st_info = (*ind)->st_info;
8456 ssym->st_other = (*ind)->st_other;
8457 ssymhead->count++;
8458 }
8459 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
8460 && (uintptr_t) ssym - (uintptr_t) ssymbuf == total_size);
8461
8462 free (indbuf);
8463 return ssymbuf;
8464 }
8465
8466 /* Check if 2 sections define the same set of local and global
8467 symbols. */
8468
8469 static bool
bfd_elf_match_symbols_in_sections(asection * sec1,asection * sec2,struct bfd_link_info * info)8470 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8471 struct bfd_link_info *info)
8472 {
8473 bfd *bfd1, *bfd2;
8474 const struct elf_backend_data *bed1, *bed2;
8475 Elf_Internal_Shdr *hdr1, *hdr2;
8476 size_t symcount1, symcount2;
8477 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8478 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8479 Elf_Internal_Sym *isym, *isymend;
8480 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8481 size_t count1, count2, sec_count1, sec_count2, i;
8482 unsigned int shndx1, shndx2;
8483 bool result;
8484 bool ignore_section_symbol_p;
8485
8486 bfd1 = sec1->owner;
8487 bfd2 = sec2->owner;
8488
8489 /* Both sections have to be in ELF. */
8490 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8491 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8492 return false;
8493
8494 if (elf_section_type (sec1) != elf_section_type (sec2))
8495 return false;
8496
8497 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8498 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8499 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
8500 return false;
8501
8502 bed1 = get_elf_backend_data (bfd1);
8503 bed2 = get_elf_backend_data (bfd2);
8504 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8505 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8506 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8507 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8508
8509 if (symcount1 == 0 || symcount2 == 0)
8510 return false;
8511
8512 result = false;
8513 isymbuf1 = NULL;
8514 isymbuf2 = NULL;
8515 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8516 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8517
8518 /* Ignore section symbols only when matching non-debugging sections
8519 or linkonce section with comdat section. */
8520 ignore_section_symbol_p
8521 = ((sec1->flags & SEC_DEBUGGING) == 0
8522 || ((elf_section_flags (sec1) & SHF_GROUP)
8523 != (elf_section_flags (sec2) & SHF_GROUP)));
8524
8525 if (ssymbuf1 == NULL)
8526 {
8527 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8528 NULL, NULL, NULL);
8529 if (isymbuf1 == NULL)
8530 goto done;
8531
8532 if (info != NULL && !info->reduce_memory_overheads)
8533 {
8534 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8535 elf_tdata (bfd1)->symbuf = ssymbuf1;
8536 }
8537 }
8538
8539 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8540 {
8541 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8542 NULL, NULL, NULL);
8543 if (isymbuf2 == NULL)
8544 goto done;
8545
8546 if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
8547 {
8548 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8549 elf_tdata (bfd2)->symbuf = ssymbuf2;
8550 }
8551 }
8552
8553 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8554 {
8555 /* Optimized faster version. */
8556 size_t lo, hi, mid;
8557 struct elf_symbol *symp;
8558 struct elf_symbuf_symbol *ssym, *ssymend;
8559
8560 lo = 0;
8561 hi = ssymbuf1->count;
8562 ssymbuf1++;
8563 count1 = 0;
8564 sec_count1 = 0;
8565 while (lo < hi)
8566 {
8567 mid = (lo + hi) / 2;
8568 if (shndx1 < ssymbuf1[mid].st_shndx)
8569 hi = mid;
8570 else if (shndx1 > ssymbuf1[mid].st_shndx)
8571 lo = mid + 1;
8572 else
8573 {
8574 count1 = ssymbuf1[mid].count;
8575 ssymbuf1 += mid;
8576 break;
8577 }
8578 }
8579 if (ignore_section_symbol_p)
8580 {
8581 for (i = 0; i < count1; i++)
8582 if (ELF_ST_TYPE (ssymbuf1->ssym[i].st_info) == STT_SECTION)
8583 sec_count1++;
8584 count1 -= sec_count1;
8585 }
8586
8587 lo = 0;
8588 hi = ssymbuf2->count;
8589 ssymbuf2++;
8590 count2 = 0;
8591 sec_count2 = 0;
8592 while (lo < hi)
8593 {
8594 mid = (lo + hi) / 2;
8595 if (shndx2 < ssymbuf2[mid].st_shndx)
8596 hi = mid;
8597 else if (shndx2 > ssymbuf2[mid].st_shndx)
8598 lo = mid + 1;
8599 else
8600 {
8601 count2 = ssymbuf2[mid].count;
8602 ssymbuf2 += mid;
8603 break;
8604 }
8605 }
8606 if (ignore_section_symbol_p)
8607 {
8608 for (i = 0; i < count2; i++)
8609 if (ELF_ST_TYPE (ssymbuf2->ssym[i].st_info) == STT_SECTION)
8610 sec_count2++;
8611 count2 -= sec_count2;
8612 }
8613
8614 if (count1 == 0 || count2 == 0 || count1 != count2)
8615 goto done;
8616
8617 symtable1
8618 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8619 symtable2
8620 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8621 if (symtable1 == NULL || symtable2 == NULL)
8622 goto done;
8623
8624 symp = symtable1;
8625 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1 + sec_count1;
8626 ssym < ssymend; ssym++)
8627 if (sec_count1 == 0
8628 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8629 {
8630 symp->u.ssym = ssym;
8631 symp->name = bfd_elf_string_from_elf_section (bfd1,
8632 hdr1->sh_link,
8633 ssym->st_name);
8634 symp++;
8635 }
8636
8637 symp = symtable2;
8638 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2 + sec_count2;
8639 ssym < ssymend; ssym++)
8640 if (sec_count2 == 0
8641 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8642 {
8643 symp->u.ssym = ssym;
8644 symp->name = bfd_elf_string_from_elf_section (bfd2,
8645 hdr2->sh_link,
8646 ssym->st_name);
8647 symp++;
8648 }
8649
8650 /* Sort symbol by name. */
8651 qsort (symtable1, count1, sizeof (struct elf_symbol),
8652 elf_sym_name_compare);
8653 qsort (symtable2, count1, sizeof (struct elf_symbol),
8654 elf_sym_name_compare);
8655
8656 for (i = 0; i < count1; i++)
8657 /* Two symbols must have the same binding, type and name. */
8658 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8659 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8660 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8661 goto done;
8662
8663 result = true;
8664 goto done;
8665 }
8666
8667 symtable1 = (struct elf_symbol *)
8668 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8669 symtable2 = (struct elf_symbol *)
8670 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8671 if (symtable1 == NULL || symtable2 == NULL)
8672 goto done;
8673
8674 /* Count definitions in the section. */
8675 count1 = 0;
8676 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8677 if (isym->st_shndx == shndx1
8678 && (!ignore_section_symbol_p
8679 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8680 symtable1[count1++].u.isym = isym;
8681
8682 count2 = 0;
8683 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8684 if (isym->st_shndx == shndx2
8685 && (!ignore_section_symbol_p
8686 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8687 symtable2[count2++].u.isym = isym;
8688
8689 if (count1 == 0 || count2 == 0 || count1 != count2)
8690 goto done;
8691
8692 for (i = 0; i < count1; i++)
8693 symtable1[i].name
8694 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8695 symtable1[i].u.isym->st_name);
8696
8697 for (i = 0; i < count2; i++)
8698 symtable2[i].name
8699 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8700 symtable2[i].u.isym->st_name);
8701
8702 /* Sort symbol by name. */
8703 qsort (symtable1, count1, sizeof (struct elf_symbol),
8704 elf_sym_name_compare);
8705 qsort (symtable2, count1, sizeof (struct elf_symbol),
8706 elf_sym_name_compare);
8707
8708 for (i = 0; i < count1; i++)
8709 /* Two symbols must have the same binding, type and name. */
8710 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8711 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8712 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8713 goto done;
8714
8715 result = true;
8716
8717 done:
8718 free (symtable1);
8719 free (symtable2);
8720 free (isymbuf1);
8721 free (isymbuf2);
8722
8723 return result;
8724 }
8725
8726 /* Return TRUE if 2 section types are compatible. */
8727
8728 bool
_bfd_elf_match_sections_by_type(bfd * abfd,const asection * asec,bfd * bbfd,const asection * bsec)8729 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8730 bfd *bbfd, const asection *bsec)
8731 {
8732 if (asec == NULL
8733 || bsec == NULL
8734 || abfd->xvec->flavour != bfd_target_elf_flavour
8735 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8736 return true;
8737
8738 return elf_section_type (asec) == elf_section_type (bsec);
8739 }
8740
8741 /* Final phase of ELF linker. */
8742
8743 /* A structure we use to avoid passing large numbers of arguments. */
8744
8745 struct elf_final_link_info
8746 {
8747 /* General link information. */
8748 struct bfd_link_info *info;
8749 /* Output BFD. */
8750 bfd *output_bfd;
8751 /* Symbol string table. */
8752 struct elf_strtab_hash *symstrtab;
8753 /* .hash section. */
8754 asection *hash_sec;
8755 /* symbol version section (.gnu.version). */
8756 asection *symver_sec;
8757 /* Buffer large enough to hold contents of any section. */
8758 bfd_byte *contents;
8759 /* Buffer large enough to hold external relocs of any section. */
8760 void *external_relocs;
8761 /* Buffer large enough to hold internal relocs of any section. */
8762 Elf_Internal_Rela *internal_relocs;
8763 /* Buffer large enough to hold external local symbols of any input
8764 BFD. */
8765 bfd_byte *external_syms;
8766 /* And a buffer for symbol section indices. */
8767 Elf_External_Sym_Shndx *locsym_shndx;
8768 /* Buffer large enough to hold internal local symbols of any input
8769 BFD. */
8770 Elf_Internal_Sym *internal_syms;
8771 /* Array large enough to hold a symbol index for each local symbol
8772 of any input BFD. */
8773 long *indices;
8774 /* Array large enough to hold a section pointer for each local
8775 symbol of any input BFD. */
8776 asection **sections;
8777 /* Buffer for SHT_SYMTAB_SHNDX section. */
8778 Elf_External_Sym_Shndx *symshndxbuf;
8779 /* Number of STT_FILE syms seen. */
8780 size_t filesym_count;
8781 /* Local symbol hash table. */
8782 struct bfd_hash_table local_hash_table;
8783 };
8784
8785 struct local_hash_entry
8786 {
8787 /* Base hash table entry structure. */
8788 struct bfd_hash_entry root;
8789 /* Size of the local symbol name. */
8790 size_t size;
8791 /* Number of the duplicated local symbol names. */
8792 long count;
8793 };
8794
8795 /* Create an entry in the local symbol hash table. */
8796
8797 static struct bfd_hash_entry *
local_hash_newfunc(struct bfd_hash_entry * entry,struct bfd_hash_table * table,const char * string)8798 local_hash_newfunc (struct bfd_hash_entry *entry,
8799 struct bfd_hash_table *table,
8800 const char *string)
8801 {
8802
8803 /* Allocate the structure if it has not already been allocated by a
8804 subclass. */
8805 if (entry == NULL)
8806 {
8807 entry = bfd_hash_allocate (table,
8808 sizeof (struct local_hash_entry));
8809 if (entry == NULL)
8810 return entry;
8811 }
8812
8813 /* Call the allocation method of the superclass. */
8814 entry = bfd_hash_newfunc (entry, table, string);
8815 if (entry != NULL)
8816 {
8817 ((struct local_hash_entry *) entry)->count = 0;
8818 ((struct local_hash_entry *) entry)->size = 0;
8819 }
8820
8821 return entry;
8822 }
8823
8824 /* This struct is used to pass information to elf_link_output_extsym. */
8825
8826 struct elf_outext_info
8827 {
8828 bool failed;
8829 bool localsyms;
8830 bool file_sym_done;
8831 struct elf_final_link_info *flinfo;
8832 };
8833
8834
8835 /* Support for evaluating a complex relocation.
8836
8837 Complex relocations are generalized, self-describing relocations. The
8838 implementation of them consists of two parts: complex symbols, and the
8839 relocations themselves.
8840
8841 The relocations use a reserved elf-wide relocation type code (R_RELC
8842 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8843 information (start bit, end bit, word width, etc) into the addend. This
8844 information is extracted from CGEN-generated operand tables within gas.
8845
8846 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
8847 internal) representing prefix-notation expressions, including but not
8848 limited to those sorts of expressions normally encoded as addends in the
8849 addend field. The symbol mangling format is:
8850
8851 <node> := <literal>
8852 | <unary-operator> ':' <node>
8853 | <binary-operator> ':' <node> ':' <node>
8854 ;
8855
8856 <literal> := 's' <digits=N> ':' <N character symbol name>
8857 | 'S' <digits=N> ':' <N character section name>
8858 | '#' <hexdigits>
8859 ;
8860
8861 <binary-operator> := as in C
8862 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8863
8864 static void
set_symbol_value(bfd * bfd_with_globals,Elf_Internal_Sym * isymbuf,size_t locsymcount,size_t symidx,bfd_vma val)8865 set_symbol_value (bfd *bfd_with_globals,
8866 Elf_Internal_Sym *isymbuf,
8867 size_t locsymcount,
8868 size_t symidx,
8869 bfd_vma val)
8870 {
8871 struct elf_link_hash_entry **sym_hashes;
8872 struct elf_link_hash_entry *h;
8873 size_t extsymoff = locsymcount;
8874
8875 if (symidx < locsymcount)
8876 {
8877 Elf_Internal_Sym *sym;
8878
8879 sym = isymbuf + symidx;
8880 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8881 {
8882 /* It is a local symbol: move it to the
8883 "absolute" section and give it a value. */
8884 sym->st_shndx = SHN_ABS;
8885 sym->st_value = val;
8886 return;
8887 }
8888 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8889 extsymoff = 0;
8890 }
8891
8892 /* It is a global symbol: set its link type
8893 to "defined" and give it a value. */
8894
8895 sym_hashes = elf_sym_hashes (bfd_with_globals);
8896 h = sym_hashes [symidx - extsymoff];
8897 while (h->root.type == bfd_link_hash_indirect
8898 || h->root.type == bfd_link_hash_warning)
8899 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8900 h->root.type = bfd_link_hash_defined;
8901 h->root.u.def.value = val;
8902 h->root.u.def.section = bfd_abs_section_ptr;
8903 }
8904
8905 static bool
resolve_symbol(const char * name,bfd * input_bfd,struct elf_final_link_info * flinfo,bfd_vma * result,Elf_Internal_Sym * isymbuf,size_t locsymcount)8906 resolve_symbol (const char *name,
8907 bfd *input_bfd,
8908 struct elf_final_link_info *flinfo,
8909 bfd_vma *result,
8910 Elf_Internal_Sym *isymbuf,
8911 size_t locsymcount)
8912 {
8913 Elf_Internal_Sym *sym;
8914 struct bfd_link_hash_entry *global_entry;
8915 const char *candidate = NULL;
8916 Elf_Internal_Shdr *symtab_hdr;
8917 size_t i;
8918
8919 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8920
8921 for (i = 0; i < locsymcount; ++ i)
8922 {
8923 sym = isymbuf + i;
8924
8925 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8926 continue;
8927
8928 candidate = bfd_elf_string_from_elf_section (input_bfd,
8929 symtab_hdr->sh_link,
8930 sym->st_name);
8931 #ifdef DEBUG
8932 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8933 name, candidate, (unsigned long) sym->st_value);
8934 #endif
8935 if (candidate && strcmp (candidate, name) == 0)
8936 {
8937 asection *sec = flinfo->sections [i];
8938
8939 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8940 *result += sec->output_offset + sec->output_section->vma;
8941 #ifdef DEBUG
8942 printf ("Found symbol with value %8.8lx\n",
8943 (unsigned long) *result);
8944 #endif
8945 return true;
8946 }
8947 }
8948
8949 /* Hmm, haven't found it yet. perhaps it is a global. */
8950 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8951 false, false, true);
8952 if (!global_entry)
8953 return false;
8954
8955 if (global_entry->type == bfd_link_hash_defined
8956 || global_entry->type == bfd_link_hash_defweak)
8957 {
8958 *result = (global_entry->u.def.value
8959 + global_entry->u.def.section->output_section->vma
8960 + global_entry->u.def.section->output_offset);
8961 #ifdef DEBUG
8962 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8963 global_entry->root.string, (unsigned long) *result);
8964 #endif
8965 return true;
8966 }
8967
8968 return false;
8969 }
8970
8971 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8972 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8973 names like "foo.end" which is the end address of section "foo". */
8974
8975 static bool
resolve_section(const char * name,asection * sections,bfd_vma * result,bfd * abfd)8976 resolve_section (const char *name,
8977 asection *sections,
8978 bfd_vma *result,
8979 bfd * abfd)
8980 {
8981 asection *curr;
8982 unsigned int len;
8983
8984 for (curr = sections; curr; curr = curr->next)
8985 if (strcmp (curr->name, name) == 0)
8986 {
8987 *result = curr->vma;
8988 return true;
8989 }
8990
8991 /* Hmm. still haven't found it. try pseudo-section names. */
8992 /* FIXME: This could be coded more efficiently... */
8993 for (curr = sections; curr; curr = curr->next)
8994 {
8995 len = strlen (curr->name);
8996 if (len > strlen (name))
8997 continue;
8998
8999 if (strncmp (curr->name, name, len) == 0)
9000 {
9001 if (startswith (name + len, ".end"))
9002 {
9003 *result = (curr->vma
9004 + curr->size / bfd_octets_per_byte (abfd, curr));
9005 return true;
9006 }
9007
9008 /* Insert more pseudo-section names here, if you like. */
9009 }
9010 }
9011
9012 return false;
9013 }
9014
9015 static void
undefined_reference(const char * reftype,const char * name)9016 undefined_reference (const char *reftype, const char *name)
9017 {
9018 /* xgettext:c-format */
9019 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
9020 reftype, name);
9021 bfd_set_error (bfd_error_bad_value);
9022 }
9023
9024 static bool
eval_symbol(bfd_vma * result,const char ** symp,bfd * input_bfd,struct elf_final_link_info * flinfo,bfd_vma dot,Elf_Internal_Sym * isymbuf,size_t locsymcount,int signed_p)9025 eval_symbol (bfd_vma *result,
9026 const char **symp,
9027 bfd *input_bfd,
9028 struct elf_final_link_info *flinfo,
9029 bfd_vma dot,
9030 Elf_Internal_Sym *isymbuf,
9031 size_t locsymcount,
9032 int signed_p)
9033 {
9034 size_t len;
9035 size_t symlen;
9036 bfd_vma a;
9037 bfd_vma b;
9038 char symbuf[4096];
9039 const char *sym = *symp;
9040 const char *symend;
9041 bool symbol_is_section = false;
9042
9043 len = strlen (sym);
9044 symend = sym + len;
9045
9046 if (len < 1 || len > sizeof (symbuf))
9047 {
9048 bfd_set_error (bfd_error_invalid_operation);
9049 return false;
9050 }
9051
9052 switch (* sym)
9053 {
9054 case '.':
9055 *result = dot;
9056 *symp = sym + 1;
9057 return true;
9058
9059 case '#':
9060 ++sym;
9061 *result = strtoul (sym, (char **) symp, 16);
9062 return true;
9063
9064 case 'S':
9065 symbol_is_section = true;
9066 /* Fall through. */
9067 case 's':
9068 ++sym;
9069 symlen = strtol (sym, (char **) symp, 10);
9070 sym = *symp + 1; /* Skip the trailing ':'. */
9071
9072 if (symend < sym || symlen + 1 > sizeof (symbuf))
9073 {
9074 bfd_set_error (bfd_error_invalid_operation);
9075 return false;
9076 }
9077
9078 memcpy (symbuf, sym, symlen);
9079 symbuf[symlen] = '\0';
9080 *symp = sym + symlen;
9081
9082 /* Is it always possible, with complex symbols, that gas "mis-guessed"
9083 the symbol as a section, or vice-versa. so we're pretty liberal in our
9084 interpretation here; section means "try section first", not "must be a
9085 section", and likewise with symbol. */
9086
9087 if (symbol_is_section)
9088 {
9089 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
9090 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
9091 isymbuf, locsymcount))
9092 {
9093 undefined_reference ("section", symbuf);
9094 return false;
9095 }
9096 }
9097 else
9098 {
9099 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
9100 isymbuf, locsymcount)
9101 && !resolve_section (symbuf, flinfo->output_bfd->sections,
9102 result, input_bfd))
9103 {
9104 undefined_reference ("symbol", symbuf);
9105 return false;
9106 }
9107 }
9108
9109 return true;
9110
9111 /* All that remains are operators. */
9112
9113 #define UNARY_OP(op) \
9114 if (startswith (sym, #op)) \
9115 { \
9116 sym += strlen (#op); \
9117 if (*sym == ':') \
9118 ++sym; \
9119 *symp = sym; \
9120 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9121 isymbuf, locsymcount, signed_p)) \
9122 return false; \
9123 if (signed_p) \
9124 *result = op ((bfd_signed_vma) a); \
9125 else \
9126 *result = op a; \
9127 return true; \
9128 }
9129
9130 #define BINARY_OP_HEAD(op) \
9131 if (startswith (sym, #op)) \
9132 { \
9133 sym += strlen (#op); \
9134 if (*sym == ':') \
9135 ++sym; \
9136 *symp = sym; \
9137 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9138 isymbuf, locsymcount, signed_p)) \
9139 return false; \
9140 ++*symp; \
9141 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
9142 isymbuf, locsymcount, signed_p)) \
9143 return false;
9144 #define BINARY_OP_TAIL(op) \
9145 if (signed_p) \
9146 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
9147 else \
9148 *result = a op b; \
9149 return true; \
9150 }
9151 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
9152
9153 default:
9154 UNARY_OP (0-);
9155 BINARY_OP_HEAD (<<);
9156 if (b >= sizeof (a) * CHAR_BIT)
9157 {
9158 *result = 0;
9159 return true;
9160 }
9161 signed_p = 0;
9162 BINARY_OP_TAIL (<<);
9163 BINARY_OP_HEAD (>>);
9164 if (b >= sizeof (a) * CHAR_BIT)
9165 {
9166 *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
9167 return true;
9168 }
9169 BINARY_OP_TAIL (>>);
9170 BINARY_OP (==);
9171 BINARY_OP (!=);
9172 BINARY_OP (<=);
9173 BINARY_OP (>=);
9174 BINARY_OP (&&);
9175 BINARY_OP (||);
9176 UNARY_OP (~);
9177 UNARY_OP (!);
9178 BINARY_OP (*);
9179 BINARY_OP_HEAD (/);
9180 if (b == 0)
9181 {
9182 _bfd_error_handler (_("division by zero"));
9183 bfd_set_error (bfd_error_bad_value);
9184 return false;
9185 }
9186 BINARY_OP_TAIL (/);
9187 BINARY_OP_HEAD (%);
9188 if (b == 0)
9189 {
9190 _bfd_error_handler (_("division by zero"));
9191 bfd_set_error (bfd_error_bad_value);
9192 return false;
9193 }
9194 BINARY_OP_TAIL (%);
9195 BINARY_OP (^);
9196 BINARY_OP (|);
9197 BINARY_OP (&);
9198 BINARY_OP (+);
9199 BINARY_OP (-);
9200 BINARY_OP (<);
9201 BINARY_OP (>);
9202 #undef UNARY_OP
9203 #undef BINARY_OP
9204 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
9205 bfd_set_error (bfd_error_invalid_operation);
9206 return false;
9207 }
9208 }
9209
9210 static void
put_value(bfd_vma size,unsigned long chunksz,bfd * input_bfd,bfd_vma x,bfd_byte * location)9211 put_value (bfd_vma size,
9212 unsigned long chunksz,
9213 bfd *input_bfd,
9214 bfd_vma x,
9215 bfd_byte *location)
9216 {
9217 location += (size - chunksz);
9218
9219 for (; size; size -= chunksz, location -= chunksz)
9220 {
9221 switch (chunksz)
9222 {
9223 case 1:
9224 bfd_put_8 (input_bfd, x, location);
9225 x >>= 8;
9226 break;
9227 case 2:
9228 bfd_put_16 (input_bfd, x, location);
9229 x >>= 16;
9230 break;
9231 case 4:
9232 bfd_put_32 (input_bfd, x, location);
9233 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
9234 x >>= 16;
9235 x >>= 16;
9236 break;
9237 #ifdef BFD64
9238 case 8:
9239 bfd_put_64 (input_bfd, x, location);
9240 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
9241 x >>= 32;
9242 x >>= 32;
9243 break;
9244 #endif
9245 default:
9246 abort ();
9247 break;
9248 }
9249 }
9250 }
9251
9252 static bfd_vma
get_value(bfd_vma size,unsigned long chunksz,bfd * input_bfd,bfd_byte * location)9253 get_value (bfd_vma size,
9254 unsigned long chunksz,
9255 bfd *input_bfd,
9256 bfd_byte *location)
9257 {
9258 int shift;
9259 bfd_vma x = 0;
9260
9261 /* Sanity checks. */
9262 BFD_ASSERT (chunksz <= sizeof (x)
9263 && size >= chunksz
9264 && chunksz != 0
9265 && (size % chunksz) == 0
9266 && input_bfd != NULL
9267 && location != NULL);
9268
9269 if (chunksz == sizeof (x))
9270 {
9271 BFD_ASSERT (size == chunksz);
9272
9273 /* Make sure that we do not perform an undefined shift operation.
9274 We know that size == chunksz so there will only be one iteration
9275 of the loop below. */
9276 shift = 0;
9277 }
9278 else
9279 shift = 8 * chunksz;
9280
9281 for (; size; size -= chunksz, location += chunksz)
9282 {
9283 switch (chunksz)
9284 {
9285 case 1:
9286 x = (x << shift) | bfd_get_8 (input_bfd, location);
9287 break;
9288 case 2:
9289 x = (x << shift) | bfd_get_16 (input_bfd, location);
9290 break;
9291 case 4:
9292 x = (x << shift) | bfd_get_32 (input_bfd, location);
9293 break;
9294 #ifdef BFD64
9295 case 8:
9296 x = (x << shift) | bfd_get_64 (input_bfd, location);
9297 break;
9298 #endif
9299 default:
9300 abort ();
9301 }
9302 }
9303 return x;
9304 }
9305
9306 static void
decode_complex_addend(unsigned long * start,unsigned long * oplen,unsigned long * len,unsigned long * wordsz,unsigned long * chunksz,unsigned long * lsb0_p,unsigned long * signed_p,unsigned long * trunc_p,unsigned long encoded)9307 decode_complex_addend (unsigned long *start, /* in bits */
9308 unsigned long *oplen, /* in bits */
9309 unsigned long *len, /* in bits */
9310 unsigned long *wordsz, /* in bytes */
9311 unsigned long *chunksz, /* in bytes */
9312 unsigned long *lsb0_p,
9313 unsigned long *signed_p,
9314 unsigned long *trunc_p,
9315 unsigned long encoded)
9316 {
9317 * start = encoded & 0x3F;
9318 * len = (encoded >> 6) & 0x3F;
9319 * oplen = (encoded >> 12) & 0x3F;
9320 * wordsz = (encoded >> 18) & 0xF;
9321 * chunksz = (encoded >> 22) & 0xF;
9322 * lsb0_p = (encoded >> 27) & 1;
9323 * signed_p = (encoded >> 28) & 1;
9324 * trunc_p = (encoded >> 29) & 1;
9325 }
9326
9327 bfd_reloc_status_type
bfd_elf_perform_complex_relocation(bfd * input_bfd,asection * input_section,bfd_byte * contents,Elf_Internal_Rela * rel,bfd_vma relocation)9328 bfd_elf_perform_complex_relocation (bfd *input_bfd,
9329 asection *input_section,
9330 bfd_byte *contents,
9331 Elf_Internal_Rela *rel,
9332 bfd_vma relocation)
9333 {
9334 bfd_vma shift, x, mask;
9335 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
9336 bfd_reloc_status_type r;
9337 bfd_size_type octets;
9338
9339 /* Perform this reloc, since it is complex.
9340 (this is not to say that it necessarily refers to a complex
9341 symbol; merely that it is a self-describing CGEN based reloc.
9342 i.e. the addend has the complete reloc information (bit start, end,
9343 word size, etc) encoded within it.). */
9344
9345 decode_complex_addend (&start, &oplen, &len, &wordsz,
9346 &chunksz, &lsb0_p, &signed_p,
9347 &trunc_p, rel->r_addend);
9348
9349 mask = (((1L << (len - 1)) - 1) << 1) | 1;
9350
9351 if (lsb0_p)
9352 shift = (start + 1) - len;
9353 else
9354 shift = (8 * wordsz) - (start + len);
9355
9356 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
9357 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
9358
9359 #ifdef DEBUG
9360 printf ("Doing complex reloc: "
9361 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9362 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9363 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9364 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9365 oplen, (unsigned long) x, (unsigned long) mask,
9366 (unsigned long) relocation);
9367 #endif
9368
9369 r = bfd_reloc_ok;
9370 if (! trunc_p)
9371 /* Now do an overflow check. */
9372 r = bfd_check_overflow ((signed_p
9373 ? complain_overflow_signed
9374 : complain_overflow_unsigned),
9375 len, 0, (8 * wordsz),
9376 relocation);
9377
9378 /* Do the deed. */
9379 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
9380
9381 #ifdef DEBUG
9382 printf (" relocation: %8.8lx\n"
9383 " shifted mask: %8.8lx\n"
9384 " shifted/masked reloc: %8.8lx\n"
9385 " result: %8.8lx\n",
9386 (unsigned long) relocation, (unsigned long) (mask << shift),
9387 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
9388 #endif
9389 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
9390 return r;
9391 }
9392
9393 /* Functions to read r_offset from external (target order) reloc
9394 entry. Faster than bfd_getl32 et al, because we let the compiler
9395 know the value is aligned. */
9396
9397 static bfd_vma
ext32l_r_offset(const void * p)9398 ext32l_r_offset (const void *p)
9399 {
9400 union aligned32
9401 {
9402 uint32_t v;
9403 unsigned char c[4];
9404 };
9405 const union aligned32 *a
9406 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9407
9408 uint32_t aval = ( (uint32_t) a->c[0]
9409 | (uint32_t) a->c[1] << 8
9410 | (uint32_t) a->c[2] << 16
9411 | (uint32_t) a->c[3] << 24);
9412 return aval;
9413 }
9414
9415 static bfd_vma
ext32b_r_offset(const void * p)9416 ext32b_r_offset (const void *p)
9417 {
9418 union aligned32
9419 {
9420 uint32_t v;
9421 unsigned char c[4];
9422 };
9423 const union aligned32 *a
9424 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9425
9426 uint32_t aval = ( (uint32_t) a->c[0] << 24
9427 | (uint32_t) a->c[1] << 16
9428 | (uint32_t) a->c[2] << 8
9429 | (uint32_t) a->c[3]);
9430 return aval;
9431 }
9432
9433 static bfd_vma
ext64l_r_offset(const void * p)9434 ext64l_r_offset (const void *p)
9435 {
9436 union aligned64
9437 {
9438 uint64_t v;
9439 unsigned char c[8];
9440 };
9441 const union aligned64 *a
9442 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9443
9444 uint64_t aval = ( (uint64_t) a->c[0]
9445 | (uint64_t) a->c[1] << 8
9446 | (uint64_t) a->c[2] << 16
9447 | (uint64_t) a->c[3] << 24
9448 | (uint64_t) a->c[4] << 32
9449 | (uint64_t) a->c[5] << 40
9450 | (uint64_t) a->c[6] << 48
9451 | (uint64_t) a->c[7] << 56);
9452 return aval;
9453 }
9454
9455 static bfd_vma
ext64b_r_offset(const void * p)9456 ext64b_r_offset (const void *p)
9457 {
9458 union aligned64
9459 {
9460 uint64_t v;
9461 unsigned char c[8];
9462 };
9463 const union aligned64 *a
9464 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9465
9466 uint64_t aval = ( (uint64_t) a->c[0] << 56
9467 | (uint64_t) a->c[1] << 48
9468 | (uint64_t) a->c[2] << 40
9469 | (uint64_t) a->c[3] << 32
9470 | (uint64_t) a->c[4] << 24
9471 | (uint64_t) a->c[5] << 16
9472 | (uint64_t) a->c[6] << 8
9473 | (uint64_t) a->c[7]);
9474 return aval;
9475 }
9476
9477 /* When performing a relocatable link, the input relocations are
9478 preserved. But, if they reference global symbols, the indices
9479 referenced must be updated. Update all the relocations found in
9480 RELDATA. */
9481
9482 static bool
elf_link_adjust_relocs(bfd * abfd,asection * sec,struct bfd_elf_section_reloc_data * reldata,bool sort,struct bfd_link_info * info)9483 elf_link_adjust_relocs (bfd *abfd,
9484 asection *sec,
9485 struct bfd_elf_section_reloc_data *reldata,
9486 bool sort,
9487 struct bfd_link_info *info)
9488 {
9489 unsigned int i;
9490 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9491 bfd_byte *erela;
9492 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9493 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9494 bfd_vma r_type_mask;
9495 int r_sym_shift;
9496 unsigned int count = reldata->count;
9497 struct elf_link_hash_entry **rel_hash = reldata->hashes;
9498
9499 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
9500 {
9501 swap_in = bed->s->swap_reloc_in;
9502 swap_out = bed->s->swap_reloc_out;
9503 }
9504 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
9505 {
9506 swap_in = bed->s->swap_reloca_in;
9507 swap_out = bed->s->swap_reloca_out;
9508 }
9509 else
9510 abort ();
9511
9512 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9513 abort ();
9514
9515 if (bed->s->arch_size == 32)
9516 {
9517 r_type_mask = 0xff;
9518 r_sym_shift = 8;
9519 }
9520 else
9521 {
9522 r_type_mask = 0xffffffff;
9523 r_sym_shift = 32;
9524 }
9525
9526 erela = reldata->hdr->contents;
9527 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
9528 {
9529 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9530 unsigned int j;
9531
9532 if (*rel_hash == NULL)
9533 continue;
9534
9535 if ((*rel_hash)->indx == -2
9536 && info->gc_sections
9537 && ! info->gc_keep_exported)
9538 {
9539 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9540 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9541 abfd, sec,
9542 (*rel_hash)->root.root.string);
9543 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9544 abfd, sec);
9545 bfd_set_error (bfd_error_invalid_operation);
9546 return false;
9547 }
9548 BFD_ASSERT ((*rel_hash)->indx >= 0);
9549
9550 (*swap_in) (abfd, erela, irela);
9551 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9552 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9553 | (irela[j].r_info & r_type_mask));
9554 (*swap_out) (abfd, irela, erela);
9555 }
9556
9557 if (bed->elf_backend_update_relocs)
9558 (*bed->elf_backend_update_relocs) (sec, reldata);
9559
9560 if (sort && count != 0)
9561 {
9562 bfd_vma (*ext_r_off) (const void *);
9563 bfd_vma r_off;
9564 size_t elt_size;
9565 bfd_byte *base, *end, *p, *loc;
9566 bfd_byte *buf = NULL;
9567
9568 if (bed->s->arch_size == 32)
9569 {
9570 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9571 ext_r_off = ext32l_r_offset;
9572 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9573 ext_r_off = ext32b_r_offset;
9574 else
9575 abort ();
9576 }
9577 else
9578 {
9579 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9580 ext_r_off = ext64l_r_offset;
9581 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9582 ext_r_off = ext64b_r_offset;
9583 else
9584 abort ();
9585 }
9586
9587 /* Must use a stable sort here. A modified insertion sort,
9588 since the relocs are mostly sorted already. */
9589 elt_size = reldata->hdr->sh_entsize;
9590 base = reldata->hdr->contents;
9591 end = base + count * elt_size;
9592 if (elt_size > sizeof (Elf64_External_Rela))
9593 abort ();
9594
9595 /* Ensure the first element is lowest. This acts as a sentinel,
9596 speeding the main loop below. */
9597 r_off = (*ext_r_off) (base);
9598 for (p = loc = base; (p += elt_size) < end; )
9599 {
9600 bfd_vma r_off2 = (*ext_r_off) (p);
9601 if (r_off > r_off2)
9602 {
9603 r_off = r_off2;
9604 loc = p;
9605 }
9606 }
9607 if (loc != base)
9608 {
9609 /* Don't just swap *base and *loc as that changes the order
9610 of the original base[0] and base[1] if they happen to
9611 have the same r_offset. */
9612 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9613 memcpy (onebuf, loc, elt_size);
9614 memmove (base + elt_size, base, loc - base);
9615 memcpy (base, onebuf, elt_size);
9616 }
9617
9618 for (p = base + elt_size; (p += elt_size) < end; )
9619 {
9620 /* base to p is sorted, *p is next to insert. */
9621 r_off = (*ext_r_off) (p);
9622 /* Search the sorted region for location to insert. */
9623 loc = p - elt_size;
9624 while (r_off < (*ext_r_off) (loc))
9625 loc -= elt_size;
9626 loc += elt_size;
9627 if (loc != p)
9628 {
9629 /* Chances are there is a run of relocs to insert here,
9630 from one of more input files. Files are not always
9631 linked in order due to the way elf_link_input_bfd is
9632 called. See pr17666. */
9633 size_t sortlen = p - loc;
9634 bfd_vma r_off2 = (*ext_r_off) (loc);
9635 size_t runlen = elt_size;
9636 bfd_vma r_off_runend = r_off;
9637 bfd_vma r_off_runend_next;
9638 size_t buf_size = 96 * 1024;
9639 while (p + runlen < end
9640 && (sortlen <= buf_size
9641 || runlen + elt_size <= buf_size)
9642 /* run must not break the ordering of base..loc+1 */
9643 && r_off2 > (r_off_runend_next = (*ext_r_off) (p + runlen))
9644 /* run must be already sorted */
9645 && r_off_runend_next >= r_off_runend)
9646 {
9647 runlen += elt_size;
9648 r_off_runend = r_off_runend_next;
9649 }
9650 if (buf == NULL)
9651 {
9652 buf = bfd_malloc (buf_size);
9653 if (buf == NULL)
9654 return false;
9655 }
9656 if (runlen < sortlen)
9657 {
9658 memcpy (buf, p, runlen);
9659 memmove (loc + runlen, loc, sortlen);
9660 memcpy (loc, buf, runlen);
9661 }
9662 else
9663 {
9664 memcpy (buf, loc, sortlen);
9665 memmove (loc, p, runlen);
9666 memcpy (loc + runlen, buf, sortlen);
9667 }
9668 p += runlen - elt_size;
9669 }
9670 }
9671 /* Hashes are no longer valid. */
9672 free (reldata->hashes);
9673 reldata->hashes = NULL;
9674 free (buf);
9675 }
9676 return true;
9677 }
9678
9679 struct elf_link_sort_rela
9680 {
9681 union {
9682 bfd_vma offset;
9683 bfd_vma sym_mask;
9684 } u;
9685 enum elf_reloc_type_class type;
9686 /* We use this as an array of size int_rels_per_ext_rel. */
9687 Elf_Internal_Rela rela[1];
9688 };
9689
9690 /* qsort stability here and for cmp2 is only an issue if multiple
9691 dynamic relocations are emitted at the same address. But targets
9692 that apply a series of dynamic relocations each operating on the
9693 result of the prior relocation can't use -z combreloc as
9694 implemented anyway. Such schemes tend to be broken by sorting on
9695 symbol index. That leaves dynamic NONE relocs as the only other
9696 case where ld might emit multiple relocs at the same address, and
9697 those are only emitted due to target bugs. */
9698
9699 static int
elf_link_sort_cmp1(const void * A,const void * B)9700 elf_link_sort_cmp1 (const void *A, const void *B)
9701 {
9702 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9703 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9704 int relativea, relativeb;
9705
9706 relativea = a->type == reloc_class_relative;
9707 relativeb = b->type == reloc_class_relative;
9708
9709 if (relativea < relativeb)
9710 return 1;
9711 if (relativea > relativeb)
9712 return -1;
9713 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9714 return -1;
9715 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9716 return 1;
9717 if (a->rela->r_offset < b->rela->r_offset)
9718 return -1;
9719 if (a->rela->r_offset > b->rela->r_offset)
9720 return 1;
9721 return 0;
9722 }
9723
9724 static int
elf_link_sort_cmp2(const void * A,const void * B)9725 elf_link_sort_cmp2 (const void *A, const void *B)
9726 {
9727 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9728 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9729
9730 if (a->type < b->type)
9731 return -1;
9732 if (a->type > b->type)
9733 return 1;
9734 if (a->u.offset < b->u.offset)
9735 return -1;
9736 if (a->u.offset > b->u.offset)
9737 return 1;
9738 if (a->rela->r_offset < b->rela->r_offset)
9739 return -1;
9740 if (a->rela->r_offset > b->rela->r_offset)
9741 return 1;
9742 return 0;
9743 }
9744
9745 static size_t
elf_link_sort_relocs(bfd * abfd,struct bfd_link_info * info,asection ** psec)9746 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9747 {
9748 asection *dynamic_relocs;
9749 asection *rela_dyn;
9750 asection *rel_dyn;
9751 bfd_size_type count, size;
9752 size_t i, ret, sort_elt, ext_size;
9753 bfd_byte *sort, *s_non_relative, *p;
9754 struct elf_link_sort_rela *sq;
9755 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9756 int i2e = bed->s->int_rels_per_ext_rel;
9757 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
9758 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9759 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9760 struct bfd_link_order *lo;
9761 bfd_vma r_sym_mask;
9762 bool use_rela;
9763
9764 /* Find a dynamic reloc section. */
9765 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9766 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9767 if (rela_dyn != NULL && rela_dyn->size > 0
9768 && rel_dyn != NULL && rel_dyn->size > 0)
9769 {
9770 bool use_rela_initialised = false;
9771
9772 /* This is just here to stop gcc from complaining.
9773 Its initialization checking code is not perfect. */
9774 use_rela = true;
9775
9776 /* Both sections are present. Examine the sizes
9777 of the indirect sections to help us choose. */
9778 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9779 if (lo->type == bfd_indirect_link_order)
9780 {
9781 asection *o = lo->u.indirect.section;
9782
9783 if ((o->size % bed->s->sizeof_rela) == 0)
9784 {
9785 if ((o->size % bed->s->sizeof_rel) == 0)
9786 /* Section size is divisible by both rel and rela sizes.
9787 It is of no help to us. */
9788 ;
9789 else
9790 {
9791 /* Section size is only divisible by rela. */
9792 if (use_rela_initialised && !use_rela)
9793 {
9794 _bfd_error_handler (_("%pB: unable to sort relocs - "
9795 "they are in more than one size"),
9796 abfd);
9797 bfd_set_error (bfd_error_invalid_operation);
9798 return 0;
9799 }
9800 else
9801 {
9802 use_rela = true;
9803 use_rela_initialised = true;
9804 }
9805 }
9806 }
9807 else if ((o->size % bed->s->sizeof_rel) == 0)
9808 {
9809 /* Section size is only divisible by rel. */
9810 if (use_rela_initialised && use_rela)
9811 {
9812 _bfd_error_handler (_("%pB: unable to sort relocs - "
9813 "they are in more than one size"),
9814 abfd);
9815 bfd_set_error (bfd_error_invalid_operation);
9816 return 0;
9817 }
9818 else
9819 {
9820 use_rela = false;
9821 use_rela_initialised = true;
9822 }
9823 }
9824 else
9825 {
9826 /* The section size is not divisible by either -
9827 something is wrong. */
9828 _bfd_error_handler (_("%pB: unable to sort relocs - "
9829 "they are of an unknown size"), abfd);
9830 bfd_set_error (bfd_error_invalid_operation);
9831 return 0;
9832 }
9833 }
9834
9835 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9836 if (lo->type == bfd_indirect_link_order)
9837 {
9838 asection *o = lo->u.indirect.section;
9839
9840 if ((o->size % bed->s->sizeof_rela) == 0)
9841 {
9842 if ((o->size % bed->s->sizeof_rel) == 0)
9843 /* Section size is divisible by both rel and rela sizes.
9844 It is of no help to us. */
9845 ;
9846 else
9847 {
9848 /* Section size is only divisible by rela. */
9849 if (use_rela_initialised && !use_rela)
9850 {
9851 _bfd_error_handler (_("%pB: unable to sort relocs - "
9852 "they are in more than one size"),
9853 abfd);
9854 bfd_set_error (bfd_error_invalid_operation);
9855 return 0;
9856 }
9857 else
9858 {
9859 use_rela = true;
9860 use_rela_initialised = true;
9861 }
9862 }
9863 }
9864 else if ((o->size % bed->s->sizeof_rel) == 0)
9865 {
9866 /* Section size is only divisible by rel. */
9867 if (use_rela_initialised && use_rela)
9868 {
9869 _bfd_error_handler (_("%pB: unable to sort relocs - "
9870 "they are in more than one size"),
9871 abfd);
9872 bfd_set_error (bfd_error_invalid_operation);
9873 return 0;
9874 }
9875 else
9876 {
9877 use_rela = false;
9878 use_rela_initialised = true;
9879 }
9880 }
9881 else
9882 {
9883 /* The section size is not divisible by either -
9884 something is wrong. */
9885 _bfd_error_handler (_("%pB: unable to sort relocs - "
9886 "they are of an unknown size"), abfd);
9887 bfd_set_error (bfd_error_invalid_operation);
9888 return 0;
9889 }
9890 }
9891
9892 if (! use_rela_initialised)
9893 /* Make a guess. */
9894 use_rela = true;
9895 }
9896 else if (rela_dyn != NULL && rela_dyn->size > 0)
9897 use_rela = true;
9898 else if (rel_dyn != NULL && rel_dyn->size > 0)
9899 use_rela = false;
9900 else
9901 return 0;
9902
9903 if (use_rela)
9904 {
9905 dynamic_relocs = rela_dyn;
9906 ext_size = bed->s->sizeof_rela;
9907 swap_in = bed->s->swap_reloca_in;
9908 swap_out = bed->s->swap_reloca_out;
9909 }
9910 else
9911 {
9912 dynamic_relocs = rel_dyn;
9913 ext_size = bed->s->sizeof_rel;
9914 swap_in = bed->s->swap_reloc_in;
9915 swap_out = bed->s->swap_reloc_out;
9916 }
9917
9918 size = 0;
9919 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9920 if (lo->type == bfd_indirect_link_order)
9921 size += lo->u.indirect.section->size;
9922
9923 if (size != dynamic_relocs->size)
9924 return 0;
9925
9926 sort_elt = (sizeof (struct elf_link_sort_rela)
9927 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9928
9929 count = dynamic_relocs->size / ext_size;
9930 if (count == 0)
9931 return 0;
9932 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9933
9934 if (sort == NULL)
9935 {
9936 (*info->callbacks->warning)
9937 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
9938 return 0;
9939 }
9940
9941 if (bed->s->arch_size == 32)
9942 r_sym_mask = ~(bfd_vma) 0xff;
9943 else
9944 r_sym_mask = ~(bfd_vma) 0xffffffff;
9945
9946 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9947 if (lo->type == bfd_indirect_link_order)
9948 {
9949 bfd_byte *erel, *erelend;
9950 asection *o = lo->u.indirect.section;
9951
9952 if (o->contents == NULL && o->size != 0)
9953 {
9954 /* This is a reloc section that is being handled as a normal
9955 section. See bfd_section_from_shdr. We can't combine
9956 relocs in this case. */
9957 free (sort);
9958 return 0;
9959 }
9960 erel = o->contents;
9961 erelend = o->contents + o->size;
9962 p = sort + o->output_offset * opb / ext_size * sort_elt;
9963
9964 while (erel < erelend)
9965 {
9966 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9967
9968 (*swap_in) (abfd, erel, s->rela);
9969 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9970 s->u.sym_mask = r_sym_mask;
9971 p += sort_elt;
9972 erel += ext_size;
9973 }
9974 }
9975
9976 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9977
9978 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9979 {
9980 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9981 if (s->type != reloc_class_relative)
9982 break;
9983 }
9984 ret = i;
9985 s_non_relative = p;
9986
9987 sq = (struct elf_link_sort_rela *) s_non_relative;
9988 for (; i < count; i++, p += sort_elt)
9989 {
9990 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9991 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9992 sq = sp;
9993 sp->u.offset = sq->rela->r_offset;
9994 }
9995
9996 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9997
9998 struct elf_link_hash_table *htab = elf_hash_table (info);
9999 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
10000 {
10001 /* We have plt relocs in .rela.dyn. */
10002 sq = (struct elf_link_sort_rela *) sort;
10003 for (i = 0; i < count; i++)
10004 if (sq[count - i - 1].type != reloc_class_plt)
10005 break;
10006 if (i != 0 && htab->srelplt->size == i * ext_size)
10007 {
10008 struct bfd_link_order **plo;
10009 /* Put srelplt link_order last. This is so the output_offset
10010 set in the next loop is correct for DT_JMPREL. */
10011 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
10012 if ((*plo)->type == bfd_indirect_link_order
10013 && (*plo)->u.indirect.section == htab->srelplt)
10014 {
10015 lo = *plo;
10016 *plo = lo->next;
10017 }
10018 else
10019 plo = &(*plo)->next;
10020 *plo = lo;
10021 lo->next = NULL;
10022 dynamic_relocs->map_tail.link_order = lo;
10023 }
10024 }
10025
10026 p = sort;
10027 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
10028 if (lo->type == bfd_indirect_link_order)
10029 {
10030 bfd_byte *erel, *erelend;
10031 asection *o = lo->u.indirect.section;
10032
10033 erel = o->contents;
10034 erelend = o->contents + o->size;
10035 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
10036 while (erel < erelend)
10037 {
10038 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
10039 (*swap_out) (abfd, s->rela, erel);
10040 p += sort_elt;
10041 erel += ext_size;
10042 }
10043 }
10044
10045 free (sort);
10046 *psec = dynamic_relocs;
10047 return ret;
10048 }
10049
10050 /* Add a symbol to the output symbol string table. */
10051
10052 static int
elf_link_output_symstrtab(void * finf,const char * name,Elf_Internal_Sym * elfsym,asection * input_sec,struct elf_link_hash_entry * h)10053 elf_link_output_symstrtab (void *finf,
10054 const char *name,
10055 Elf_Internal_Sym *elfsym,
10056 asection *input_sec,
10057 struct elf_link_hash_entry *h)
10058 {
10059 struct elf_final_link_info *flinfo = finf;
10060 int (*output_symbol_hook)
10061 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
10062 struct elf_link_hash_entry *);
10063 struct elf_link_hash_table *hash_table;
10064 const struct elf_backend_data *bed;
10065 bfd_size_type strtabsize;
10066
10067 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10068
10069 bed = get_elf_backend_data (flinfo->output_bfd);
10070 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
10071 if (output_symbol_hook != NULL)
10072 {
10073 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
10074 if (ret != 1)
10075 return ret;
10076 }
10077
10078 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
10079 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
10080 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
10081 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
10082
10083 if (name == NULL || *name == '\0')
10084 elfsym->st_name = (unsigned long) -1;
10085 else
10086 {
10087 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
10088 to get the final offset for st_name. */
10089 char *versioned_name = (char *) name;
10090 if (h != NULL)
10091 {
10092 if (h->versioned == versioned && h->def_dynamic)
10093 {
10094 /* Keep only one '@' for versioned symbols defined in
10095 shared objects. */
10096 char *version = strrchr (name, ELF_VER_CHR);
10097 char *base_end = strchr (name, ELF_VER_CHR);
10098 if (version != base_end)
10099 {
10100 size_t base_len;
10101 size_t len = strlen (name);
10102 versioned_name = bfd_alloc (flinfo->output_bfd, len);
10103 if (versioned_name == NULL)
10104 return 0;
10105 base_len = base_end - name;
10106 memcpy (versioned_name, name, base_len);
10107 memcpy (versioned_name + base_len, version,
10108 len - base_len);
10109 }
10110 }
10111 }
10112 else if (flinfo->info->unique_symbol
10113 && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
10114 {
10115 struct local_hash_entry *lh;
10116 size_t count_len;
10117 size_t base_len;
10118 char buf[30];
10119 switch (ELF_ST_TYPE (elfsym->st_info))
10120 {
10121 case STT_FILE:
10122 case STT_SECTION:
10123 break;
10124 default:
10125 lh = (struct local_hash_entry *) bfd_hash_lookup
10126 (&flinfo->local_hash_table, name, true, false);
10127 if (lh == NULL)
10128 return 0;
10129 /* Always append ".COUNT" to local symbols to avoid
10130 potential conflicts with local symbol "XXX.COUNT". */
10131 sprintf (buf, "%lx", lh->count);
10132 base_len = lh->size;
10133 if (!base_len)
10134 {
10135 base_len = strlen (name);
10136 lh->size = base_len;
10137 }
10138 count_len = strlen (buf);
10139 versioned_name = bfd_alloc (flinfo->output_bfd,
10140 base_len + count_len + 2);
10141 if (versioned_name == NULL)
10142 return 0;
10143 memcpy (versioned_name, name, base_len);
10144 versioned_name[base_len] = '.';
10145 memcpy (versioned_name + base_len + 1, buf,
10146 count_len + 1);
10147 lh->count++;
10148 break;
10149 }
10150 }
10151 elfsym->st_name
10152 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
10153 versioned_name, false);
10154 if (elfsym->st_name == (unsigned long) -1)
10155 return 0;
10156 }
10157
10158 hash_table = elf_hash_table (flinfo->info);
10159 strtabsize = hash_table->strtabsize;
10160 if (strtabsize <= flinfo->output_bfd->symcount)
10161 {
10162 strtabsize += strtabsize;
10163 hash_table->strtabsize = strtabsize;
10164 strtabsize *= sizeof (*hash_table->strtab);
10165 hash_table->strtab
10166 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
10167 strtabsize);
10168 if (hash_table->strtab == NULL)
10169 return 0;
10170 }
10171 hash_table->strtab[flinfo->output_bfd->symcount].sym = *elfsym;
10172 hash_table->strtab[flinfo->output_bfd->symcount].dest_index
10173 = flinfo->output_bfd->symcount;
10174 flinfo->output_bfd->symcount += 1;
10175
10176 return 1;
10177 }
10178
10179 /* Swap symbols out to the symbol table and flush the output symbols to
10180 the file. */
10181
10182 static bool
elf_link_swap_symbols_out(struct elf_final_link_info * flinfo)10183 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
10184 {
10185 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
10186 size_t amt;
10187 size_t i;
10188 const struct elf_backend_data *bed;
10189 bfd_byte *symbuf;
10190 Elf_Internal_Shdr *hdr;
10191 file_ptr pos;
10192 bool ret;
10193
10194 if (flinfo->output_bfd->symcount == 0)
10195 return true;
10196
10197 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10198
10199 bed = get_elf_backend_data (flinfo->output_bfd);
10200
10201 amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10202 symbuf = (bfd_byte *) bfd_malloc (amt);
10203 if (symbuf == NULL)
10204 return false;
10205
10206 if (flinfo->symshndxbuf)
10207 {
10208 amt = sizeof (Elf_External_Sym_Shndx);
10209 amt *= bfd_get_symcount (flinfo->output_bfd);
10210 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
10211 if (flinfo->symshndxbuf == NULL)
10212 {
10213 free (symbuf);
10214 return false;
10215 }
10216 }
10217
10218 /* Now swap out the symbols. */
10219 for (i = 0; i < flinfo->output_bfd->symcount; i++)
10220 {
10221 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
10222 if (elfsym->sym.st_name == (unsigned long) -1)
10223 elfsym->sym.st_name = 0;
10224 else
10225 elfsym->sym.st_name
10226 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
10227 elfsym->sym.st_name);
10228
10229 /* Inform the linker of the addition of this symbol. */
10230
10231 if (flinfo->info->callbacks->ctf_new_symbol)
10232 flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
10233 &elfsym->sym);
10234
10235 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
10236 ((bfd_byte *) symbuf
10237 + (elfsym->dest_index
10238 * bed->s->sizeof_sym)),
10239 NPTR_ADD (flinfo->symshndxbuf,
10240 elfsym->dest_index));
10241 }
10242
10243 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
10244 pos = hdr->sh_offset + hdr->sh_size;
10245 amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10246 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
10247 && bfd_write (symbuf, amt, flinfo->output_bfd) == amt)
10248 {
10249 hdr->sh_size += amt;
10250 ret = true;
10251 }
10252 else
10253 ret = false;
10254
10255 free (symbuf);
10256
10257 free (hash_table->strtab);
10258 hash_table->strtab = NULL;
10259
10260 return ret;
10261 }
10262
10263 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
10264
10265 static bool
check_dynsym(bfd * abfd,Elf_Internal_Sym * sym)10266 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
10267 {
10268 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
10269 && sym->st_shndx < SHN_LORESERVE)
10270 {
10271 /* The gABI doesn't support dynamic symbols in output sections
10272 beyond 64k. */
10273 _bfd_error_handler
10274 /* xgettext:c-format */
10275 (_("%pB: too many sections: %d (>= %d)"),
10276 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
10277 bfd_set_error (bfd_error_nonrepresentable_section);
10278 return false;
10279 }
10280 return true;
10281 }
10282
10283 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
10284 allowing an unsatisfied unversioned symbol in the DSO to match a
10285 versioned symbol that would normally require an explicit version.
10286 We also handle the case that a DSO references a hidden symbol
10287 which may be satisfied by a versioned symbol in another DSO. */
10288
10289 static bool
elf_link_check_versioned_symbol(struct bfd_link_info * info,const struct elf_backend_data * bed,struct elf_link_hash_entry * h)10290 elf_link_check_versioned_symbol (struct bfd_link_info *info,
10291 const struct elf_backend_data *bed,
10292 struct elf_link_hash_entry *h)
10293 {
10294 bfd *abfd;
10295 struct elf_link_loaded_list *loaded;
10296
10297 if (!is_elf_hash_table (info->hash))
10298 return false;
10299
10300 /* Check indirect symbol. */
10301 while (h->root.type == bfd_link_hash_indirect)
10302 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10303
10304 switch (h->root.type)
10305 {
10306 default:
10307 abfd = NULL;
10308 break;
10309
10310 case bfd_link_hash_undefined:
10311 case bfd_link_hash_undefweak:
10312 abfd = h->root.u.undef.abfd;
10313 if (abfd == NULL
10314 || (abfd->flags & DYNAMIC) == 0
10315 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
10316 return false;
10317 break;
10318
10319 case bfd_link_hash_defined:
10320 case bfd_link_hash_defweak:
10321 abfd = h->root.u.def.section->owner;
10322 break;
10323
10324 case bfd_link_hash_common:
10325 abfd = h->root.u.c.p->section->owner;
10326 break;
10327 }
10328 BFD_ASSERT (abfd != NULL);
10329
10330 for (loaded = elf_hash_table (info)->dyn_loaded;
10331 loaded != NULL;
10332 loaded = loaded->next)
10333 {
10334 bfd *input;
10335 Elf_Internal_Shdr *hdr;
10336 size_t symcount;
10337 size_t extsymcount;
10338 size_t extsymoff;
10339 Elf_Internal_Shdr *versymhdr;
10340 Elf_Internal_Sym *isym;
10341 Elf_Internal_Sym *isymend;
10342 Elf_Internal_Sym *isymbuf;
10343 Elf_External_Versym *ever;
10344 Elf_External_Versym *extversym;
10345
10346 input = loaded->abfd;
10347
10348 /* We check each DSO for a possible hidden versioned definition. */
10349 if (input == abfd
10350 || elf_dynversym (input) == 0)
10351 continue;
10352
10353 hdr = &elf_tdata (input)->dynsymtab_hdr;
10354
10355 symcount = hdr->sh_size / bed->s->sizeof_sym;
10356 if (elf_bad_symtab (input))
10357 {
10358 extsymcount = symcount;
10359 extsymoff = 0;
10360 }
10361 else
10362 {
10363 extsymcount = symcount - hdr->sh_info;
10364 extsymoff = hdr->sh_info;
10365 }
10366
10367 if (extsymcount == 0)
10368 continue;
10369
10370 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
10371 NULL, NULL, NULL);
10372 if (isymbuf == NULL)
10373 return false;
10374
10375 /* Read in any version definitions. */
10376 versymhdr = &elf_tdata (input)->dynversym_hdr;
10377 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
10378 || (extversym = (Elf_External_Versym *)
10379 _bfd_malloc_and_read (input, versymhdr->sh_size,
10380 versymhdr->sh_size)) == NULL)
10381 {
10382 free (isymbuf);
10383 return false;
10384 }
10385
10386 ever = extversym + extsymoff;
10387 isymend = isymbuf + extsymcount;
10388 for (isym = isymbuf; isym < isymend; isym++, ever++)
10389 {
10390 const char *name;
10391 Elf_Internal_Versym iver;
10392 unsigned short version_index;
10393
10394 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
10395 || isym->st_shndx == SHN_UNDEF)
10396 continue;
10397
10398 name = bfd_elf_string_from_elf_section (input,
10399 hdr->sh_link,
10400 isym->st_name);
10401 if (strcmp (name, h->root.root.string) != 0)
10402 continue;
10403
10404 _bfd_elf_swap_versym_in (input, ever, &iver);
10405
10406 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
10407 && !(h->def_regular
10408 && h->forced_local))
10409 {
10410 /* If we have a non-hidden versioned sym, then it should
10411 have provided a definition for the undefined sym unless
10412 it is defined in a non-shared object and forced local.
10413 */
10414 abort ();
10415 }
10416
10417 version_index = iver.vs_vers & VERSYM_VERSION;
10418 if (version_index == 1 || version_index == 2)
10419 {
10420 /* This is the base or first version. We can use it. */
10421 free (extversym);
10422 free (isymbuf);
10423 return true;
10424 }
10425 }
10426
10427 free (extversym);
10428 free (isymbuf);
10429 }
10430
10431 return false;
10432 }
10433
10434 /* Convert ELF common symbol TYPE. */
10435
10436 static int
elf_link_convert_common_type(struct bfd_link_info * info,int type)10437 elf_link_convert_common_type (struct bfd_link_info *info, int type)
10438 {
10439 /* Commom symbol can only appear in relocatable link. */
10440 if (!bfd_link_relocatable (info))
10441 abort ();
10442 switch (info->elf_stt_common)
10443 {
10444 case unchanged:
10445 break;
10446 case elf_stt_common:
10447 type = STT_COMMON;
10448 break;
10449 case no_elf_stt_common:
10450 type = STT_OBJECT;
10451 break;
10452 }
10453 return type;
10454 }
10455
10456 /* Add an external symbol to the symbol table. This is called from
10457 the hash table traversal routine. When generating a shared object,
10458 we go through the symbol table twice. The first time we output
10459 anything that might have been forced to local scope in a version
10460 script. The second time we output the symbols that are still
10461 global symbols. */
10462
10463 static bool
elf_link_output_extsym(struct bfd_hash_entry * bh,void * data)10464 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
10465 {
10466 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
10467 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
10468 struct elf_final_link_info *flinfo = eoinfo->flinfo;
10469 bool strip;
10470 Elf_Internal_Sym sym;
10471 asection *input_sec;
10472 const struct elf_backend_data *bed;
10473 long indx;
10474 int ret;
10475 unsigned int type;
10476
10477 if (h->root.type == bfd_link_hash_warning)
10478 {
10479 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10480 if (h->root.type == bfd_link_hash_new)
10481 return true;
10482 }
10483
10484 /* Decide whether to output this symbol in this pass. */
10485 if (eoinfo->localsyms)
10486 {
10487 if (!h->forced_local)
10488 return true;
10489 }
10490 else
10491 {
10492 if (h->forced_local)
10493 return true;
10494 }
10495
10496 bed = get_elf_backend_data (flinfo->output_bfd);
10497
10498 if (h->root.type == bfd_link_hash_undefined)
10499 {
10500 /* If we have an undefined symbol reference here then it must have
10501 come from a shared library that is being linked in. (Undefined
10502 references in regular files have already been handled unless
10503 they are in unreferenced sections which are removed by garbage
10504 collection). */
10505 bool ignore_undef = false;
10506
10507 /* Some symbols may be special in that the fact that they're
10508 undefined can be safely ignored - let backend determine that. */
10509 if (bed->elf_backend_ignore_undef_symbol)
10510 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10511
10512 /* If we are reporting errors for this situation then do so now. */
10513 if (!ignore_undef
10514 && h->ref_dynamic_nonweak
10515 && (!h->ref_regular || flinfo->info->gc_sections)
10516 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10517 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
10518 {
10519 flinfo->info->callbacks->undefined_symbol
10520 (flinfo->info, h->root.root.string,
10521 h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10522 flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10523 && !flinfo->info->warn_unresolved_syms);
10524 }
10525
10526 /* Strip a global symbol defined in a discarded section. */
10527 if (h->indx == -3)
10528 return true;
10529 }
10530
10531 /* We should also warn if a forced local symbol is referenced from
10532 shared libraries. */
10533 if (bfd_link_executable (flinfo->info)
10534 && h->forced_local
10535 && h->ref_dynamic
10536 && h->def_regular
10537 && !h->dynamic_def
10538 && h->ref_dynamic_nonweak
10539 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
10540 {
10541 bfd *def_bfd;
10542 const char *msg;
10543 struct elf_link_hash_entry *hi = h;
10544
10545 /* Check indirect symbol. */
10546 while (hi->root.type == bfd_link_hash_indirect)
10547 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
10548
10549 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
10550 /* xgettext:c-format */
10551 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10552 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
10553 /* xgettext:c-format */
10554 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10555 else
10556 /* xgettext:c-format */
10557 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
10558 def_bfd = flinfo->output_bfd;
10559 if (hi->root.u.def.section != bfd_abs_section_ptr)
10560 def_bfd = hi->root.u.def.section->owner;
10561 _bfd_error_handler (msg, flinfo->output_bfd,
10562 h->root.root.string, def_bfd);
10563 bfd_set_error (bfd_error_bad_value);
10564 eoinfo->failed = true;
10565 return false;
10566 }
10567
10568 /* We don't want to output symbols that have never been mentioned by
10569 a regular file, or that we have been told to strip. However, if
10570 h->indx is set to -2, the symbol is used by a reloc and we must
10571 output it. */
10572 strip = false;
10573 if (h->indx == -2)
10574 ;
10575 else if ((h->def_dynamic
10576 || h->ref_dynamic
10577 || h->root.type == bfd_link_hash_new)
10578 && !h->def_regular
10579 && !h->ref_regular)
10580 strip = true;
10581 else if (flinfo->info->strip == strip_all)
10582 strip = true;
10583 else if (flinfo->info->strip == strip_some
10584 && bfd_hash_lookup (flinfo->info->keep_hash,
10585 h->root.root.string, false, false) == NULL)
10586 strip = true;
10587 else if ((h->root.type == bfd_link_hash_defined
10588 || h->root.type == bfd_link_hash_defweak)
10589 && ((flinfo->info->strip_discarded
10590 && discarded_section (h->root.u.def.section))
10591 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10592 && h->root.u.def.section->owner != NULL
10593 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
10594 strip = true;
10595 else if ((h->root.type == bfd_link_hash_undefined
10596 || h->root.type == bfd_link_hash_undefweak)
10597 && h->root.u.undef.abfd != NULL
10598 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10599 strip = true;
10600
10601 type = h->type;
10602
10603 /* If we're stripping it, and it's not a dynamic symbol, there's
10604 nothing else to do. However, if it is a forced local symbol or
10605 an ifunc symbol we need to give the backend finish_dynamic_symbol
10606 function a chance to make it dynamic. */
10607 if (strip
10608 && h->dynindx == -1
10609 && type != STT_GNU_IFUNC
10610 && !h->forced_local)
10611 return true;
10612
10613 sym.st_value = 0;
10614 sym.st_size = h->size;
10615 sym.st_other = h->other;
10616 switch (h->root.type)
10617 {
10618 default:
10619 case bfd_link_hash_new:
10620 case bfd_link_hash_warning:
10621 abort ();
10622 return false;
10623
10624 case bfd_link_hash_undefined:
10625 case bfd_link_hash_undefweak:
10626 input_sec = bfd_und_section_ptr;
10627 sym.st_shndx = SHN_UNDEF;
10628 break;
10629
10630 case bfd_link_hash_defined:
10631 case bfd_link_hash_defweak:
10632 {
10633 input_sec = h->root.u.def.section;
10634 if (input_sec->output_section != NULL)
10635 {
10636 sym.st_shndx =
10637 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
10638 input_sec->output_section);
10639 if (sym.st_shndx == SHN_BAD)
10640 {
10641 _bfd_error_handler
10642 /* xgettext:c-format */
10643 (_("%pB: could not find output section %pA for input section %pA"),
10644 flinfo->output_bfd, input_sec->output_section, input_sec);
10645 bfd_set_error (bfd_error_nonrepresentable_section);
10646 eoinfo->failed = true;
10647 return false;
10648 }
10649
10650 /* ELF symbols in relocatable files are section relative,
10651 but in nonrelocatable files they are virtual
10652 addresses. */
10653 sym.st_value = h->root.u.def.value + input_sec->output_offset;
10654 if (!bfd_link_relocatable (flinfo->info))
10655 {
10656 sym.st_value += input_sec->output_section->vma;
10657 if (h->type == STT_TLS)
10658 {
10659 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
10660 if (tls_sec != NULL)
10661 sym.st_value -= tls_sec->vma;
10662 }
10663 }
10664 }
10665 else
10666 {
10667 BFD_ASSERT (input_sec->owner == NULL
10668 || (input_sec->owner->flags & DYNAMIC) != 0);
10669 sym.st_shndx = SHN_UNDEF;
10670 input_sec = bfd_und_section_ptr;
10671 }
10672 }
10673 break;
10674
10675 case bfd_link_hash_common:
10676 input_sec = h->root.u.c.p->section;
10677 sym.st_shndx = bed->common_section_index (input_sec);
10678 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10679 break;
10680
10681 case bfd_link_hash_indirect:
10682 /* These symbols are created by symbol versioning. They point
10683 to the decorated version of the name. For example, if the
10684 symbol foo@@GNU_1.2 is the default, which should be used when
10685 foo is used with no version, then we add an indirect symbol
10686 foo which points to foo@@GNU_1.2. We ignore these symbols,
10687 since the indirected symbol is already in the hash table. */
10688 return true;
10689 }
10690
10691 if (type == STT_COMMON || type == STT_OBJECT)
10692 switch (h->root.type)
10693 {
10694 case bfd_link_hash_common:
10695 type = elf_link_convert_common_type (flinfo->info, type);
10696 break;
10697 case bfd_link_hash_defined:
10698 case bfd_link_hash_defweak:
10699 if (bed->common_definition (&sym))
10700 type = elf_link_convert_common_type (flinfo->info, type);
10701 else
10702 type = STT_OBJECT;
10703 break;
10704 case bfd_link_hash_undefined:
10705 case bfd_link_hash_undefweak:
10706 break;
10707 default:
10708 abort ();
10709 }
10710
10711 if (h->forced_local)
10712 {
10713 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10714 /* Turn off visibility on local symbol. */
10715 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10716 }
10717 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10718 else if (h->unique_global && h->def_regular)
10719 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10720 else if (h->root.type == bfd_link_hash_undefweak
10721 || h->root.type == bfd_link_hash_defweak)
10722 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10723 else
10724 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10725 sym.st_target_internal = h->target_internal;
10726
10727 /* Give the processor backend a chance to tweak the symbol value,
10728 and also to finish up anything that needs to be done for this
10729 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10730 forced local syms when non-shared is due to a historical quirk.
10731 STT_GNU_IFUNC symbol must go through PLT. */
10732 if ((h->type == STT_GNU_IFUNC
10733 && h->def_regular
10734 && !bfd_link_relocatable (flinfo->info))
10735 || ((h->dynindx != -1
10736 || h->forced_local)
10737 && ((bfd_link_pic (flinfo->info)
10738 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10739 || h->root.type != bfd_link_hash_undefweak))
10740 || !h->forced_local)
10741 && elf_hash_table (flinfo->info)->dynamic_sections_created))
10742 {
10743 if (! ((*bed->elf_backend_finish_dynamic_symbol)
10744 (flinfo->output_bfd, flinfo->info, h, &sym)))
10745 {
10746 eoinfo->failed = true;
10747 return false;
10748 }
10749 }
10750
10751 /* If we are marking the symbol as undefined, and there are no
10752 non-weak references to this symbol from a regular object, then
10753 mark the symbol as weak undefined; if there are non-weak
10754 references, mark the symbol as strong. We can't do this earlier,
10755 because it might not be marked as undefined until the
10756 finish_dynamic_symbol routine gets through with it. */
10757 if (sym.st_shndx == SHN_UNDEF
10758 && h->ref_regular
10759 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10760 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10761 {
10762 int bindtype;
10763 type = ELF_ST_TYPE (sym.st_info);
10764
10765 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10766 if (type == STT_GNU_IFUNC)
10767 type = STT_FUNC;
10768
10769 if (h->ref_regular_nonweak)
10770 bindtype = STB_GLOBAL;
10771 else
10772 bindtype = STB_WEAK;
10773 sym.st_info = ELF_ST_INFO (bindtype, type);
10774 }
10775
10776 /* If this is a symbol defined in a dynamic library, don't use the
10777 symbol size from the dynamic library. Relinking an executable
10778 against a new library may introduce gratuitous changes in the
10779 executable's symbols if we keep the size. */
10780 if (sym.st_shndx == SHN_UNDEF
10781 && !h->def_regular
10782 && h->def_dynamic)
10783 sym.st_size = 0;
10784
10785 /* If a non-weak symbol with non-default visibility is not defined
10786 locally, it is a fatal error. */
10787 if (!bfd_link_relocatable (flinfo->info)
10788 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10789 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10790 && h->root.type == bfd_link_hash_undefined
10791 && !h->def_regular)
10792 {
10793 const char *msg;
10794
10795 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10796 /* xgettext:c-format */
10797 msg = _("%pB: protected symbol `%s' isn't defined");
10798 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10799 /* xgettext:c-format */
10800 msg = _("%pB: internal symbol `%s' isn't defined");
10801 else
10802 /* xgettext:c-format */
10803 msg = _("%pB: hidden symbol `%s' isn't defined");
10804 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10805 bfd_set_error (bfd_error_bad_value);
10806 eoinfo->failed = true;
10807 return false;
10808 }
10809
10810 /* If this symbol should be put in the .dynsym section, then put it
10811 there now. We already know the symbol index. We also fill in
10812 the entry in the .hash section. */
10813 if (h->dynindx != -1
10814 && elf_hash_table (flinfo->info)->dynamic_sections_created
10815 && elf_hash_table (flinfo->info)->dynsym != NULL
10816 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
10817 {
10818 bfd_byte *esym;
10819
10820 /* Since there is no version information in the dynamic string,
10821 if there is no version info in symbol version section, we will
10822 have a run-time problem if not linking executable, referenced
10823 by shared library, or not bound locally. */
10824 if (h->verinfo.verdef == NULL
10825 && (!bfd_link_executable (flinfo->info)
10826 || h->ref_dynamic
10827 || !h->def_regular))
10828 {
10829 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10830
10831 if (p && p [1] != '\0')
10832 {
10833 _bfd_error_handler
10834 /* xgettext:c-format */
10835 (_("%pB: no symbol version section for versioned symbol `%s'"),
10836 flinfo->output_bfd, h->root.root.string);
10837 eoinfo->failed = true;
10838 return false;
10839 }
10840 }
10841
10842 sym.st_name = h->dynstr_index;
10843 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10844 + h->dynindx * bed->s->sizeof_sym);
10845 if (!check_dynsym (flinfo->output_bfd, &sym))
10846 {
10847 eoinfo->failed = true;
10848 return false;
10849 }
10850
10851 /* Inform the linker of the addition of this symbol. */
10852
10853 if (flinfo->info->callbacks->ctf_new_dynsym)
10854 flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
10855
10856 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
10857
10858 if (flinfo->hash_sec != NULL)
10859 {
10860 size_t hash_entry_size;
10861 bfd_byte *bucketpos;
10862 bfd_vma chain;
10863 size_t bucketcount;
10864 size_t bucket;
10865
10866 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
10867 bucket = h->u.elf_hash_value % bucketcount;
10868
10869 hash_entry_size
10870 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10871 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
10872 + (bucket + 2) * hash_entry_size);
10873 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10874 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10875 bucketpos);
10876 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10877 ((bfd_byte *) flinfo->hash_sec->contents
10878 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10879 }
10880
10881 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
10882 {
10883 Elf_Internal_Versym iversym;
10884 Elf_External_Versym *eversym;
10885
10886 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
10887 {
10888 if (h->verinfo.verdef == NULL
10889 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10890 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
10891 iversym.vs_vers = 1;
10892 else
10893 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10894 }
10895 else
10896 {
10897 if (h->verinfo.vertree == NULL)
10898 iversym.vs_vers = 1;
10899 else
10900 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
10901 if (flinfo->info->create_default_symver)
10902 iversym.vs_vers++;
10903 }
10904
10905 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10906 defined locally. */
10907 if (h->versioned == versioned_hidden && h->def_regular)
10908 iversym.vs_vers |= VERSYM_HIDDEN;
10909
10910 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10911 eversym += h->dynindx;
10912 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10913 }
10914 }
10915
10916 /* If the symbol is undefined, and we didn't output it to .dynsym,
10917 strip it from .symtab too. Obviously we can't do this for
10918 relocatable output or when needed for --emit-relocs. */
10919 else if (input_sec == bfd_und_section_ptr
10920 && h->indx != -2
10921 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10922 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10923 && !bfd_link_relocatable (flinfo->info))
10924 return true;
10925
10926 /* Also strip others that we couldn't earlier due to dynamic symbol
10927 processing. */
10928 if (strip)
10929 return true;
10930 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10931 return true;
10932
10933 /* Output a FILE symbol so that following locals are not associated
10934 with the wrong input file. We need one for forced local symbols
10935 if we've seen more than one FILE symbol or when we have exactly
10936 one FILE symbol but global symbols are present in a file other
10937 than the one with the FILE symbol. We also need one if linker
10938 defined symbols are present. In practice these conditions are
10939 always met, so just emit the FILE symbol unconditionally. */
10940 if (eoinfo->localsyms
10941 && !eoinfo->file_sym_done
10942 && eoinfo->flinfo->filesym_count != 0)
10943 {
10944 Elf_Internal_Sym fsym;
10945
10946 memset (&fsym, 0, sizeof (fsym));
10947 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10948 fsym.st_shndx = SHN_ABS;
10949 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10950 bfd_und_section_ptr, NULL))
10951 return false;
10952
10953 eoinfo->file_sym_done = true;
10954 }
10955
10956 indx = bfd_get_symcount (flinfo->output_bfd);
10957 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10958 input_sec, h);
10959 if (ret == 0)
10960 {
10961 eoinfo->failed = true;
10962 return false;
10963 }
10964 else if (ret == 1)
10965 h->indx = indx;
10966 else if (h->indx == -2)
10967 abort();
10968
10969 return true;
10970 }
10971
10972 /* Return TRUE if special handling is done for relocs in SEC against
10973 symbols defined in discarded sections. */
10974
10975 static bool
elf_section_ignore_discarded_relocs(asection * sec)10976 elf_section_ignore_discarded_relocs (asection *sec)
10977 {
10978 const struct elf_backend_data *bed;
10979
10980 switch (sec->sec_info_type)
10981 {
10982 case SEC_INFO_TYPE_STABS:
10983 case SEC_INFO_TYPE_EH_FRAME:
10984 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10985 case SEC_INFO_TYPE_SFRAME:
10986 return true;
10987 default:
10988 break;
10989 }
10990
10991 bed = get_elf_backend_data (sec->owner);
10992 if (bed->elf_backend_ignore_discarded_relocs != NULL
10993 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10994 return true;
10995
10996 return false;
10997 }
10998
10999 /* Return a mask saying how ld should treat relocations in SEC against
11000 symbols defined in discarded sections. If this function returns
11001 COMPLAIN set, ld will issue a warning message. If this function
11002 returns PRETEND set, and the discarded section was link-once and the
11003 same size as the kept link-once section, ld will pretend that the
11004 symbol was actually defined in the kept section. Otherwise ld will
11005 zero the reloc (at least that is the intent, but some cooperation by
11006 the target dependent code is needed, particularly for REL targets). */
11007
11008 unsigned int
_bfd_elf_default_action_discarded(asection * sec)11009 _bfd_elf_default_action_discarded (asection *sec)
11010 {
11011 const struct elf_backend_data *bed;
11012 bed = get_elf_backend_data (sec->owner);
11013
11014 if (sec->flags & SEC_DEBUGGING)
11015 return PRETEND;
11016
11017 if (strcmp (".eh_frame", sec->name) == 0)
11018 return 0;
11019
11020 if (bed->elf_backend_can_make_multiple_eh_frame
11021 && strncmp (sec->name, ".eh_frame.", 10) == 0)
11022 return 0;
11023
11024 if (strcmp (".sframe", sec->name) == 0)
11025 return 0;
11026
11027 if (strcmp (".gcc_except_table", sec->name) == 0)
11028 return 0;
11029
11030 return COMPLAIN | PRETEND;
11031 }
11032
11033 /* Find a match between a section and a member of a section group. */
11034
11035 static asection *
match_group_member(asection * sec,asection * group,struct bfd_link_info * info)11036 match_group_member (asection *sec, asection *group,
11037 struct bfd_link_info *info)
11038 {
11039 asection *first = elf_next_in_group (group);
11040 asection *s = first;
11041
11042 while (s != NULL)
11043 {
11044 if (bfd_elf_match_symbols_in_sections (s, sec, info))
11045 return s;
11046
11047 s = elf_next_in_group (s);
11048 if (s == first)
11049 break;
11050 }
11051
11052 return NULL;
11053 }
11054
11055 /* Check if the kept section of a discarded section SEC can be used
11056 to replace it. Return the replacement if it is OK. Otherwise return
11057 NULL. */
11058
11059 asection *
_bfd_elf_check_kept_section(asection * sec,struct bfd_link_info * info)11060 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
11061 {
11062 asection *kept;
11063
11064 kept = sec->kept_section;
11065 if (kept != NULL)
11066 {
11067 if ((kept->flags & SEC_GROUP) != 0)
11068 kept = match_group_member (sec, kept, info);
11069 if (kept != NULL)
11070 {
11071 if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
11072 != (kept->rawsize != 0 ? kept->rawsize : kept->size))
11073 kept = NULL;
11074 else
11075 {
11076 /* Get the real kept section. */
11077 asection *next;
11078 for (next = kept->kept_section;
11079 next != NULL;
11080 next = next->kept_section)
11081 kept = next;
11082 }
11083 }
11084 sec->kept_section = kept;
11085 }
11086 return kept;
11087 }
11088
11089 /* Link an input file into the linker output file. This function
11090 handles all the sections and relocations of the input file at once.
11091 This is so that we only have to read the local symbols once, and
11092 don't have to keep them in memory. */
11093
11094 static bool
elf_link_input_bfd(struct elf_final_link_info * flinfo,bfd * input_bfd)11095 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
11096 {
11097 int (*relocate_section)
11098 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
11099 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
11100 bfd *output_bfd;
11101 Elf_Internal_Shdr *symtab_hdr;
11102 size_t locsymcount;
11103 size_t extsymoff;
11104 Elf_Internal_Sym *isymbuf;
11105 Elf_Internal_Sym *isym;
11106 Elf_Internal_Sym *isymend;
11107 long *pindex;
11108 asection **ppsection;
11109 asection *o;
11110 const struct elf_backend_data *bed;
11111 struct elf_link_hash_entry **sym_hashes;
11112 bfd_size_type address_size;
11113 bfd_vma r_type_mask;
11114 int r_sym_shift;
11115 bool have_file_sym = false;
11116
11117 output_bfd = flinfo->output_bfd;
11118 bed = get_elf_backend_data (output_bfd);
11119 relocate_section = bed->elf_backend_relocate_section;
11120
11121 /* If this is a dynamic object, we don't want to do anything here:
11122 we don't want the local symbols, and we don't want the section
11123 contents. */
11124 if ((input_bfd->flags & DYNAMIC) != 0)
11125 return true;
11126
11127 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
11128 if (elf_bad_symtab (input_bfd))
11129 {
11130 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11131 extsymoff = 0;
11132 }
11133 else
11134 {
11135 locsymcount = symtab_hdr->sh_info;
11136 extsymoff = symtab_hdr->sh_info;
11137 }
11138
11139 /* Enable GNU OSABI features in the output BFD that are used in the input
11140 BFD. */
11141 if (bed->elf_osabi == ELFOSABI_NONE
11142 || bed->elf_osabi == ELFOSABI_GNU
11143 || bed->elf_osabi == ELFOSABI_FREEBSD)
11144 elf_tdata (output_bfd)->has_gnu_osabi
11145 |= (elf_tdata (input_bfd)->has_gnu_osabi
11146 & (bfd_link_relocatable (flinfo->info)
11147 ? -1 : ~elf_gnu_osabi_retain));
11148
11149 /* Read the local symbols. */
11150 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
11151 if (isymbuf == NULL && locsymcount != 0)
11152 {
11153 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
11154 flinfo->internal_syms,
11155 flinfo->external_syms,
11156 flinfo->locsym_shndx);
11157 if (isymbuf == NULL)
11158 return false;
11159 }
11160
11161 /* Find local symbol sections and adjust values of symbols in
11162 SEC_MERGE sections. Write out those local symbols we know are
11163 going into the output file. */
11164 isymend = PTR_ADD (isymbuf, locsymcount);
11165 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
11166 isym < isymend;
11167 isym++, pindex++, ppsection++)
11168 {
11169 asection *isec;
11170 const char *name;
11171 Elf_Internal_Sym osym;
11172 long indx;
11173 int ret;
11174
11175 *pindex = -1;
11176
11177 if (elf_bad_symtab (input_bfd))
11178 {
11179 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
11180 {
11181 *ppsection = NULL;
11182 continue;
11183 }
11184 }
11185
11186 if (isym->st_shndx == SHN_UNDEF)
11187 isec = bfd_und_section_ptr;
11188 else if (isym->st_shndx == SHN_ABS)
11189 isec = bfd_abs_section_ptr;
11190 else if (isym->st_shndx == SHN_COMMON)
11191 isec = bfd_com_section_ptr;
11192 else
11193 {
11194 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
11195 if (isec == NULL)
11196 {
11197 /* Don't attempt to output symbols with st_shnx in the
11198 reserved range other than SHN_ABS and SHN_COMMON. */
11199 isec = bfd_und_section_ptr;
11200 }
11201 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
11202 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
11203 isym->st_value =
11204 _bfd_merged_section_offset (output_bfd, &isec,
11205 elf_section_data (isec)->sec_info,
11206 isym->st_value);
11207 }
11208
11209 *ppsection = isec;
11210
11211 /* Don't output the first, undefined, symbol. In fact, don't
11212 output any undefined local symbol. */
11213 if (isec == bfd_und_section_ptr)
11214 continue;
11215
11216 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
11217 {
11218 /* We never output section symbols. Instead, we use the
11219 section symbol of the corresponding section in the output
11220 file. */
11221 continue;
11222 }
11223
11224 /* If we are stripping all symbols, we don't want to output this
11225 one. */
11226 if (flinfo->info->strip == strip_all)
11227 continue;
11228
11229 /* If we are discarding all local symbols, we don't want to
11230 output this one. If we are generating a relocatable output
11231 file, then some of the local symbols may be required by
11232 relocs; we output them below as we discover that they are
11233 needed. */
11234 if (flinfo->info->discard == discard_all)
11235 continue;
11236
11237 /* If this symbol is defined in a section which we are
11238 discarding, we don't need to keep it. */
11239 if (isym->st_shndx < SHN_LORESERVE
11240 && (isec->output_section == NULL
11241 || bfd_section_removed_from_list (output_bfd,
11242 isec->output_section)))
11243 continue;
11244
11245 /* Get the name of the symbol. */
11246 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
11247 isym->st_name);
11248 if (name == NULL)
11249 return false;
11250
11251 /* See if we are discarding symbols with this name. */
11252 if ((flinfo->info->strip == strip_some
11253 && (bfd_hash_lookup (flinfo->info->keep_hash, name, false, false)
11254 == NULL))
11255 || (((flinfo->info->discard == discard_sec_merge
11256 && (isec->flags & SEC_MERGE)
11257 && !bfd_link_relocatable (flinfo->info))
11258 || flinfo->info->discard == discard_l)
11259 && bfd_is_local_label_name (input_bfd, name)))
11260 continue;
11261
11262 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
11263 {
11264 if (input_bfd->lto_output)
11265 /* -flto puts a temp file name here. This means builds
11266 are not reproducible. Discard the symbol. */
11267 continue;
11268 have_file_sym = true;
11269 flinfo->filesym_count += 1;
11270 }
11271 if (!have_file_sym)
11272 {
11273 /* In the absence of debug info, bfd_find_nearest_line uses
11274 FILE symbols to determine the source file for local
11275 function symbols. Provide a FILE symbol here if input
11276 files lack such, so that their symbols won't be
11277 associated with a previous input file. It's not the
11278 source file, but the best we can do. */
11279 const char *filename;
11280 have_file_sym = true;
11281 flinfo->filesym_count += 1;
11282 memset (&osym, 0, sizeof (osym));
11283 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
11284 osym.st_shndx = SHN_ABS;
11285 if (input_bfd->lto_output)
11286 filename = NULL;
11287 else
11288 filename = lbasename (bfd_get_filename (input_bfd));
11289 if (!elf_link_output_symstrtab (flinfo, filename, &osym,
11290 bfd_abs_section_ptr, NULL))
11291 return false;
11292 }
11293
11294 osym = *isym;
11295
11296 /* Adjust the section index for the output file. */
11297 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11298 isec->output_section);
11299 if (osym.st_shndx == SHN_BAD)
11300 return false;
11301
11302 /* ELF symbols in relocatable files are section relative, but
11303 in executable files they are virtual addresses. Note that
11304 this code assumes that all ELF sections have an associated
11305 BFD section with a reasonable value for output_offset; below
11306 we assume that they also have a reasonable value for
11307 output_section. Any special sections must be set up to meet
11308 these requirements. */
11309 osym.st_value += isec->output_offset;
11310 if (!bfd_link_relocatable (flinfo->info))
11311 {
11312 osym.st_value += isec->output_section->vma;
11313 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
11314 {
11315 /* STT_TLS symbols are relative to PT_TLS segment base. */
11316 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
11317 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
11318 else
11319 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
11320 STT_NOTYPE);
11321 }
11322 }
11323
11324 indx = bfd_get_symcount (output_bfd);
11325 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
11326 if (ret == 0)
11327 return false;
11328 else if (ret == 1)
11329 *pindex = indx;
11330 }
11331
11332 if (bed->s->arch_size == 32)
11333 {
11334 r_type_mask = 0xff;
11335 r_sym_shift = 8;
11336 address_size = 4;
11337 }
11338 else
11339 {
11340 r_type_mask = 0xffffffff;
11341 r_sym_shift = 32;
11342 address_size = 8;
11343 }
11344
11345 /* Relocate the contents of each section. */
11346 sym_hashes = elf_sym_hashes (input_bfd);
11347 for (o = input_bfd->sections; o != NULL; o = o->next)
11348 {
11349 bfd_byte *contents;
11350
11351 if (! o->linker_mark)
11352 {
11353 /* This section was omitted from the link. */
11354 continue;
11355 }
11356
11357 if (!flinfo->info->resolve_section_groups
11358 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
11359 {
11360 /* Deal with the group signature symbol. */
11361 struct bfd_elf_section_data *sec_data = elf_section_data (o);
11362 unsigned long symndx = sec_data->this_hdr.sh_info;
11363 asection *osec = o->output_section;
11364
11365 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
11366 if (symndx >= locsymcount
11367 || (elf_bad_symtab (input_bfd)
11368 && flinfo->sections[symndx] == NULL))
11369 {
11370 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
11371 while (h->root.type == bfd_link_hash_indirect
11372 || h->root.type == bfd_link_hash_warning)
11373 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11374 /* Arrange for symbol to be output. */
11375 h->indx = -2;
11376 elf_section_data (osec)->this_hdr.sh_info = -2;
11377 }
11378 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
11379 {
11380 /* We'll use the output section target_index. */
11381 asection *sec = flinfo->sections[symndx]->output_section;
11382 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
11383 }
11384 else
11385 {
11386 if (flinfo->indices[symndx] == -1)
11387 {
11388 /* Otherwise output the local symbol now. */
11389 Elf_Internal_Sym sym = isymbuf[symndx];
11390 asection *sec = flinfo->sections[symndx]->output_section;
11391 const char *name;
11392 long indx;
11393 int ret;
11394
11395 name = bfd_elf_string_from_elf_section (input_bfd,
11396 symtab_hdr->sh_link,
11397 sym.st_name);
11398 if (name == NULL)
11399 return false;
11400
11401 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11402 sec);
11403 if (sym.st_shndx == SHN_BAD)
11404 return false;
11405
11406 sym.st_value += o->output_offset;
11407
11408 indx = bfd_get_symcount (output_bfd);
11409 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
11410 NULL);
11411 if (ret == 0)
11412 return false;
11413 else if (ret == 1)
11414 flinfo->indices[symndx] = indx;
11415 else
11416 abort ();
11417 }
11418 elf_section_data (osec)->this_hdr.sh_info
11419 = flinfo->indices[symndx];
11420 }
11421 }
11422
11423 if ((o->flags & SEC_HAS_CONTENTS) == 0
11424 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
11425 continue;
11426
11427 if ((o->flags & SEC_LINKER_CREATED) != 0)
11428 {
11429 /* Section was created by _bfd_elf_link_create_dynamic_sections
11430 or somesuch. */
11431 continue;
11432 }
11433
11434 /* Get the contents of the section. They have been cached by a
11435 relaxation routine. Note that o is a section in an input
11436 file, so the contents field will not have been set by any of
11437 the routines which work on output files. */
11438 if (elf_section_data (o)->this_hdr.contents != NULL)
11439 {
11440 contents = elf_section_data (o)->this_hdr.contents;
11441 if (bed->caches_rawsize
11442 && o->rawsize != 0
11443 && o->rawsize < o->size)
11444 {
11445 memcpy (flinfo->contents, contents, o->rawsize);
11446 contents = flinfo->contents;
11447 }
11448 }
11449 else if (!(o->flags & SEC_RELOC)
11450 && !bed->elf_backend_write_section
11451 && o->sec_info_type == SEC_INFO_TYPE_MERGE)
11452 /* A MERGE section that has no relocations doesn't need the
11453 contents anymore, they have been recorded earlier. Except
11454 if the backend has special provisions for writing sections. */
11455 contents = NULL;
11456 else
11457 {
11458 contents = flinfo->contents;
11459 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
11460 return false;
11461 }
11462
11463 if ((o->flags & SEC_RELOC) != 0)
11464 {
11465 Elf_Internal_Rela *internal_relocs;
11466 Elf_Internal_Rela *rel, *relend;
11467 int action_discarded;
11468 int ret;
11469
11470 /* Get the swapped relocs. */
11471 internal_relocs
11472 = _bfd_elf_link_info_read_relocs (input_bfd, flinfo->info, o,
11473 flinfo->external_relocs,
11474 flinfo->internal_relocs,
11475 false);
11476 if (internal_relocs == NULL
11477 && o->reloc_count > 0)
11478 return false;
11479
11480 action_discarded = -1;
11481 if (!elf_section_ignore_discarded_relocs (o))
11482 action_discarded = (*bed->action_discarded) (o);
11483
11484 /* Run through the relocs evaluating complex reloc symbols and
11485 looking for relocs against symbols from discarded sections
11486 or section symbols from removed link-once sections.
11487 Complain about relocs against discarded sections. Zero
11488 relocs against removed link-once sections. */
11489
11490 rel = internal_relocs;
11491 relend = rel + o->reloc_count;
11492 for ( ; rel < relend; rel++)
11493 {
11494 unsigned long r_symndx = rel->r_info >> r_sym_shift;
11495 unsigned int s_type;
11496 asection **ps, *sec;
11497 struct elf_link_hash_entry *h = NULL;
11498 const char *sym_name;
11499
11500 if (r_symndx == STN_UNDEF)
11501 continue;
11502
11503 if (r_symndx >= locsymcount
11504 || (elf_bad_symtab (input_bfd)
11505 && flinfo->sections[r_symndx] == NULL))
11506 {
11507 h = sym_hashes[r_symndx - extsymoff];
11508
11509 /* Badly formatted input files can contain relocs that
11510 reference non-existant symbols. Check here so that
11511 we do not seg fault. */
11512 if (h == NULL)
11513 {
11514 _bfd_error_handler
11515 /* xgettext:c-format */
11516 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
11517 "that references a non-existent global symbol"),
11518 input_bfd, (uint64_t) rel->r_info, o);
11519 bfd_set_error (bfd_error_bad_value);
11520 return false;
11521 }
11522
11523 while (h->root.type == bfd_link_hash_indirect
11524 || h->root.type == bfd_link_hash_warning)
11525 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11526
11527 s_type = h->type;
11528
11529 /* If a plugin symbol is referenced from a non-IR file,
11530 mark the symbol as undefined. Note that the
11531 linker may attach linker created dynamic sections
11532 to the plugin bfd. Symbols defined in linker
11533 created sections are not plugin symbols. */
11534 if ((h->root.non_ir_ref_regular
11535 || h->root.non_ir_ref_dynamic)
11536 && (h->root.type == bfd_link_hash_defined
11537 || h->root.type == bfd_link_hash_defweak)
11538 && (h->root.u.def.section->flags
11539 & SEC_LINKER_CREATED) == 0
11540 && h->root.u.def.section->owner != NULL
11541 && (h->root.u.def.section->owner->flags
11542 & BFD_PLUGIN) != 0)
11543 {
11544 h->root.type = bfd_link_hash_undefined;
11545 h->root.u.undef.abfd = h->root.u.def.section->owner;
11546 }
11547
11548 ps = NULL;
11549 if (h->root.type == bfd_link_hash_defined
11550 || h->root.type == bfd_link_hash_defweak)
11551 ps = &h->root.u.def.section;
11552
11553 sym_name = h->root.root.string;
11554 }
11555 else
11556 {
11557 Elf_Internal_Sym *sym = isymbuf + r_symndx;
11558
11559 s_type = ELF_ST_TYPE (sym->st_info);
11560 ps = &flinfo->sections[r_symndx];
11561 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11562 sym, *ps);
11563 }
11564
11565 if ((s_type == STT_RELC || s_type == STT_SRELC)
11566 && !bfd_link_relocatable (flinfo->info))
11567 {
11568 bfd_vma val;
11569 bfd_vma dot = (rel->r_offset
11570 + o->output_offset + o->output_section->vma);
11571 #ifdef DEBUG
11572 printf ("Encountered a complex symbol!");
11573 printf (" (input_bfd %s, section %s, reloc %ld\n",
11574 bfd_get_filename (input_bfd), o->name,
11575 (long) (rel - internal_relocs));
11576 printf (" symbol: idx %8.8lx, name %s\n",
11577 r_symndx, sym_name);
11578 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11579 (unsigned long) rel->r_info,
11580 (unsigned long) rel->r_offset);
11581 #endif
11582 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
11583 isymbuf, locsymcount, s_type == STT_SRELC))
11584 return false;
11585
11586 /* Symbol evaluated OK. Update to absolute value. */
11587 set_symbol_value (input_bfd, isymbuf, locsymcount,
11588 r_symndx, val);
11589 continue;
11590 }
11591
11592 if (action_discarded != -1 && ps != NULL)
11593 {
11594 /* Complain if the definition comes from a
11595 discarded section. */
11596 if ((sec = *ps) != NULL && discarded_section (sec))
11597 {
11598 BFD_ASSERT (r_symndx != STN_UNDEF);
11599 if (action_discarded & COMPLAIN)
11600 (*flinfo->info->callbacks->einfo)
11601 /* xgettext:c-format */
11602 (_("%X`%s' referenced in section `%pA' of %pB: "
11603 "defined in discarded section `%pA' of %pB\n"),
11604 sym_name, o, input_bfd, sec, sec->owner);
11605
11606 /* Try to do the best we can to support buggy old
11607 versions of gcc. Pretend that the symbol is
11608 really defined in the kept linkonce section.
11609 FIXME: This is quite broken. Modifying the
11610 symbol here means we will be changing all later
11611 uses of the symbol, not just in this section. */
11612 if (action_discarded & PRETEND)
11613 {
11614 asection *kept;
11615
11616 kept = _bfd_elf_check_kept_section (sec,
11617 flinfo->info);
11618 if (kept != NULL)
11619 {
11620 *ps = kept;
11621 continue;
11622 }
11623 }
11624 }
11625 }
11626 }
11627
11628 /* Relocate the section by invoking a back end routine.
11629
11630 The back end routine is responsible for adjusting the
11631 section contents as necessary, and (if using Rela relocs
11632 and generating a relocatable output file) adjusting the
11633 reloc addend as necessary.
11634
11635 The back end routine does not have to worry about setting
11636 the reloc address or the reloc symbol index.
11637
11638 The back end routine is given a pointer to the swapped in
11639 internal symbols, and can access the hash table entries
11640 for the external symbols via elf_sym_hashes (input_bfd).
11641
11642 When generating relocatable output, the back end routine
11643 must handle STB_LOCAL/STT_SECTION symbols specially. The
11644 output symbol is going to be a section symbol
11645 corresponding to the output section, which will require
11646 the addend to be adjusted. */
11647
11648 ret = (*relocate_section) (output_bfd, flinfo->info,
11649 input_bfd, o, contents,
11650 internal_relocs,
11651 isymbuf,
11652 flinfo->sections);
11653 if (!ret)
11654 return false;
11655
11656 if (ret == 2
11657 || bfd_link_relocatable (flinfo->info)
11658 || flinfo->info->emitrelocations)
11659 {
11660 Elf_Internal_Rela *irela;
11661 Elf_Internal_Rela *irelaend, *irelamid;
11662 bfd_vma last_offset;
11663 struct elf_link_hash_entry **rel_hash;
11664 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11665 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
11666 unsigned int next_erel;
11667 bool rela_normal;
11668 struct bfd_elf_section_data *esdi, *esdo;
11669
11670 esdi = elf_section_data (o);
11671 esdo = elf_section_data (o->output_section);
11672 rela_normal = false;
11673
11674 /* Adjust the reloc addresses and symbol indices. */
11675
11676 irela = internal_relocs;
11677 irelaend = irela + o->reloc_count;
11678 rel_hash = PTR_ADD (esdo->rel.hashes, esdo->rel.count);
11679 /* We start processing the REL relocs, if any. When we reach
11680 IRELAMID in the loop, we switch to the RELA relocs. */
11681 irelamid = irela;
11682 if (esdi->rel.hdr != NULL)
11683 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11684 * bed->s->int_rels_per_ext_rel);
11685 rel_hash_list = rel_hash;
11686 rela_hash_list = NULL;
11687 last_offset = o->output_offset;
11688 if (!bfd_link_relocatable (flinfo->info))
11689 last_offset += o->output_section->vma;
11690 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11691 {
11692 unsigned long r_symndx;
11693 asection *sec;
11694 Elf_Internal_Sym sym;
11695
11696 if (next_erel == bed->s->int_rels_per_ext_rel)
11697 {
11698 rel_hash++;
11699 next_erel = 0;
11700 }
11701
11702 if (irela == irelamid)
11703 {
11704 rel_hash = PTR_ADD (esdo->rela.hashes, esdo->rela.count);
11705 rela_hash_list = rel_hash;
11706 rela_normal = bed->rela_normal;
11707 }
11708
11709 irela->r_offset = _bfd_elf_section_offset (output_bfd,
11710 flinfo->info, o,
11711 irela->r_offset);
11712 if (irela->r_offset >= (bfd_vma) -2)
11713 {
11714 /* This is a reloc for a deleted entry or somesuch.
11715 Turn it into an R_*_NONE reloc, at the same
11716 offset as the last reloc. elf_eh_frame.c and
11717 bfd_elf_discard_info rely on reloc offsets
11718 being ordered. */
11719 irela->r_offset = last_offset;
11720 irela->r_info = 0;
11721 irela->r_addend = 0;
11722 continue;
11723 }
11724
11725 irela->r_offset += o->output_offset;
11726
11727 /* Relocs in an executable have to be virtual addresses. */
11728 if (!bfd_link_relocatable (flinfo->info))
11729 irela->r_offset += o->output_section->vma;
11730
11731 last_offset = irela->r_offset;
11732
11733 r_symndx = irela->r_info >> r_sym_shift;
11734 if (r_symndx == STN_UNDEF)
11735 continue;
11736
11737 if (r_symndx >= locsymcount
11738 || (elf_bad_symtab (input_bfd)
11739 && flinfo->sections[r_symndx] == NULL))
11740 {
11741 struct elf_link_hash_entry *rh;
11742 unsigned long indx;
11743
11744 /* This is a reloc against a global symbol. We
11745 have not yet output all the local symbols, so
11746 we do not know the symbol index of any global
11747 symbol. We set the rel_hash entry for this
11748 reloc to point to the global hash table entry
11749 for this symbol. The symbol index is then
11750 set at the end of bfd_elf_final_link. */
11751 indx = r_symndx - extsymoff;
11752 rh = elf_sym_hashes (input_bfd)[indx];
11753 while (rh->root.type == bfd_link_hash_indirect
11754 || rh->root.type == bfd_link_hash_warning)
11755 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11756
11757 /* Setting the index to -2 tells
11758 elf_link_output_extsym that this symbol is
11759 used by a reloc. */
11760 BFD_ASSERT (rh->indx < 0);
11761 rh->indx = -2;
11762 *rel_hash = rh;
11763
11764 continue;
11765 }
11766
11767 /* This is a reloc against a local symbol. */
11768
11769 *rel_hash = NULL;
11770 sym = isymbuf[r_symndx];
11771 sec = flinfo->sections[r_symndx];
11772 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11773 {
11774 /* I suppose the backend ought to fill in the
11775 section of any STT_SECTION symbol against a
11776 processor specific section. */
11777 r_symndx = STN_UNDEF;
11778 if (bfd_is_abs_section (sec))
11779 ;
11780 else if (sec == NULL || sec->owner == NULL)
11781 {
11782 bfd_set_error (bfd_error_bad_value);
11783 return false;
11784 }
11785 else
11786 {
11787 asection *osec = sec->output_section;
11788
11789 /* If we have discarded a section, the output
11790 section will be the absolute section. In
11791 case of discarded SEC_MERGE sections, use
11792 the kept section. relocate_section should
11793 have already handled discarded linkonce
11794 sections. */
11795 if (bfd_is_abs_section (osec)
11796 && sec->kept_section != NULL
11797 && sec->kept_section->output_section != NULL)
11798 {
11799 osec = sec->kept_section->output_section;
11800 irela->r_addend -= osec->vma;
11801 }
11802
11803 if (!bfd_is_abs_section (osec))
11804 {
11805 r_symndx = osec->target_index;
11806 if (r_symndx == STN_UNDEF)
11807 {
11808 irela->r_addend += osec->vma;
11809 osec = _bfd_nearby_section (output_bfd, osec,
11810 osec->vma);
11811 irela->r_addend -= osec->vma;
11812 r_symndx = osec->target_index;
11813 }
11814 }
11815 }
11816
11817 /* Adjust the addend according to where the
11818 section winds up in the output section. */
11819 if (rela_normal)
11820 irela->r_addend += sec->output_offset;
11821 }
11822 else
11823 {
11824 if (flinfo->indices[r_symndx] == -1)
11825 {
11826 unsigned long shlink;
11827 const char *name;
11828 asection *osec;
11829 long indx;
11830
11831 if (flinfo->info->strip == strip_all)
11832 {
11833 /* You can't do ld -r -s. */
11834 bfd_set_error (bfd_error_invalid_operation);
11835 return false;
11836 }
11837
11838 /* This symbol was skipped earlier, but
11839 since it is needed by a reloc, we
11840 must output it now. */
11841 shlink = symtab_hdr->sh_link;
11842 name = (bfd_elf_string_from_elf_section
11843 (input_bfd, shlink, sym.st_name));
11844 if (name == NULL)
11845 return false;
11846
11847 osec = sec->output_section;
11848 sym.st_shndx =
11849 _bfd_elf_section_from_bfd_section (output_bfd,
11850 osec);
11851 if (sym.st_shndx == SHN_BAD)
11852 return false;
11853
11854 sym.st_value += sec->output_offset;
11855 if (!bfd_link_relocatable (flinfo->info))
11856 {
11857 sym.st_value += osec->vma;
11858 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11859 {
11860 struct elf_link_hash_table *htab
11861 = elf_hash_table (flinfo->info);
11862
11863 /* STT_TLS symbols are relative to PT_TLS
11864 segment base. */
11865 if (htab->tls_sec != NULL)
11866 sym.st_value -= htab->tls_sec->vma;
11867 else
11868 sym.st_info
11869 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11870 STT_NOTYPE);
11871 }
11872 }
11873
11874 indx = bfd_get_symcount (output_bfd);
11875 ret = elf_link_output_symstrtab (flinfo, name,
11876 &sym, sec,
11877 NULL);
11878 if (ret == 0)
11879 return false;
11880 else if (ret == 1)
11881 flinfo->indices[r_symndx] = indx;
11882 else
11883 abort ();
11884 }
11885
11886 r_symndx = flinfo->indices[r_symndx];
11887 }
11888
11889 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11890 | (irela->r_info & r_type_mask));
11891 }
11892
11893 /* Swap out the relocs. */
11894 input_rel_hdr = esdi->rel.hdr;
11895 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
11896 {
11897 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11898 input_rel_hdr,
11899 internal_relocs,
11900 rel_hash_list))
11901 return false;
11902 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11903 * bed->s->int_rels_per_ext_rel);
11904 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
11905 }
11906
11907 input_rela_hdr = esdi->rela.hdr;
11908 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11909 {
11910 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11911 input_rela_hdr,
11912 internal_relocs,
11913 rela_hash_list))
11914 return false;
11915 }
11916 }
11917 }
11918
11919 /* Write out the modified section contents. */
11920 if (bed->elf_backend_write_section
11921 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
11922 contents))
11923 {
11924 /* Section written out. */
11925 }
11926 else switch (o->sec_info_type)
11927 {
11928 case SEC_INFO_TYPE_STABS:
11929 if (! (_bfd_write_section_stabs
11930 (output_bfd,
11931 &elf_hash_table (flinfo->info)->stab_info,
11932 o, &elf_section_data (o)->sec_info, contents)))
11933 return false;
11934 break;
11935 case SEC_INFO_TYPE_MERGE:
11936 if (! _bfd_write_merged_section (output_bfd, o,
11937 elf_section_data (o)->sec_info))
11938 return false;
11939 break;
11940 case SEC_INFO_TYPE_EH_FRAME:
11941 {
11942 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11943 o, contents))
11944 return false;
11945 }
11946 break;
11947 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11948 {
11949 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11950 flinfo->info,
11951 o, contents))
11952 return false;
11953 }
11954 break;
11955 case SEC_INFO_TYPE_SFRAME:
11956 {
11957 /* Merge .sframe sections into the ctf frame encoder
11958 context of the output_bfd's section. The final .sframe
11959 output section will be written out later. */
11960 if (!_bfd_elf_merge_section_sframe (output_bfd, flinfo->info,
11961 o, contents))
11962 return false;
11963 }
11964 break;
11965 default:
11966 {
11967 if (! (o->flags & SEC_EXCLUDE))
11968 {
11969 file_ptr offset = (file_ptr) o->output_offset;
11970 bfd_size_type todo = o->size;
11971
11972 offset *= bfd_octets_per_byte (output_bfd, o);
11973
11974 if ((o->flags & SEC_ELF_REVERSE_COPY)
11975 && o->size > address_size)
11976 {
11977 /* Reverse-copy input section to output. */
11978
11979 if ((o->size & (address_size - 1)) != 0
11980 || (o->reloc_count != 0
11981 && (o->size * bed->s->int_rels_per_ext_rel
11982 != o->reloc_count * address_size)))
11983 {
11984 _bfd_error_handler
11985 /* xgettext:c-format */
11986 (_("error: %pB: size of section %pA is not "
11987 "multiple of address size"),
11988 input_bfd, o);
11989 bfd_set_error (bfd_error_bad_value);
11990 return false;
11991 }
11992
11993 do
11994 {
11995 todo -= address_size;
11996 if (! bfd_set_section_contents (output_bfd,
11997 o->output_section,
11998 contents + todo,
11999 offset,
12000 address_size))
12001 return false;
12002 if (todo == 0)
12003 break;
12004 offset += address_size;
12005 }
12006 while (1);
12007 }
12008 else if (! bfd_set_section_contents (output_bfd,
12009 o->output_section,
12010 contents,
12011 offset, todo))
12012 return false;
12013 }
12014 }
12015 break;
12016 }
12017 }
12018
12019 return true;
12020 }
12021
12022 /* Generate a reloc when linking an ELF file. This is a reloc
12023 requested by the linker, and does not come from any input file. This
12024 is used to build constructor and destructor tables when linking
12025 with -Ur. */
12026
12027 static bool
elf_reloc_link_order(bfd * output_bfd,struct bfd_link_info * info,asection * output_section,struct bfd_link_order * link_order)12028 elf_reloc_link_order (bfd *output_bfd,
12029 struct bfd_link_info *info,
12030 asection *output_section,
12031 struct bfd_link_order *link_order)
12032 {
12033 reloc_howto_type *howto;
12034 long indx;
12035 bfd_vma offset;
12036 bfd_vma addend;
12037 struct bfd_elf_section_reloc_data *reldata;
12038 struct elf_link_hash_entry **rel_hash_ptr;
12039 Elf_Internal_Shdr *rel_hdr;
12040 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
12041 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
12042 bfd_byte *erel;
12043 unsigned int i;
12044 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
12045
12046 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
12047 if (howto == NULL)
12048 {
12049 bfd_set_error (bfd_error_bad_value);
12050 return false;
12051 }
12052
12053 addend = link_order->u.reloc.p->addend;
12054
12055 if (esdo->rel.hdr)
12056 reldata = &esdo->rel;
12057 else if (esdo->rela.hdr)
12058 reldata = &esdo->rela;
12059 else
12060 {
12061 reldata = NULL;
12062 BFD_ASSERT (0);
12063 }
12064
12065 /* Figure out the symbol index. */
12066 rel_hash_ptr = reldata->hashes + reldata->count;
12067 if (link_order->type == bfd_section_reloc_link_order)
12068 {
12069 indx = link_order->u.reloc.p->u.section->target_index;
12070 BFD_ASSERT (indx != 0);
12071 *rel_hash_ptr = NULL;
12072 }
12073 else
12074 {
12075 struct elf_link_hash_entry *h;
12076
12077 /* Treat a reloc against a defined symbol as though it were
12078 actually against the section. */
12079 h = ((struct elf_link_hash_entry *)
12080 bfd_wrapped_link_hash_lookup (output_bfd, info,
12081 link_order->u.reloc.p->u.name,
12082 false, false, true));
12083 if (h != NULL
12084 && (h->root.type == bfd_link_hash_defined
12085 || h->root.type == bfd_link_hash_defweak))
12086 {
12087 asection *section;
12088
12089 section = h->root.u.def.section;
12090 indx = section->output_section->target_index;
12091 *rel_hash_ptr = NULL;
12092 /* It seems that we ought to add the symbol value to the
12093 addend here, but in practice it has already been added
12094 because it was passed to constructor_callback. */
12095 addend += section->output_section->vma + section->output_offset;
12096 }
12097 else if (h != NULL)
12098 {
12099 /* Setting the index to -2 tells elf_link_output_extsym that
12100 this symbol is used by a reloc. */
12101 h->indx = -2;
12102 *rel_hash_ptr = h;
12103 indx = 0;
12104 }
12105 else
12106 {
12107 (*info->callbacks->unattached_reloc)
12108 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
12109 indx = 0;
12110 }
12111 }
12112
12113 /* If this is an inplace reloc, we must write the addend into the
12114 object file. */
12115 if (howto->partial_inplace && addend != 0)
12116 {
12117 bfd_size_type size;
12118 bfd_reloc_status_type rstat;
12119 bfd_byte *buf;
12120 bool ok;
12121 const char *sym_name;
12122 bfd_size_type octets;
12123
12124 size = (bfd_size_type) bfd_get_reloc_size (howto);
12125 buf = (bfd_byte *) bfd_zmalloc (size);
12126 if (buf == NULL && size != 0)
12127 return false;
12128 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
12129 switch (rstat)
12130 {
12131 case bfd_reloc_ok:
12132 break;
12133
12134 default:
12135 case bfd_reloc_outofrange:
12136 abort ();
12137
12138 case bfd_reloc_overflow:
12139 if (link_order->type == bfd_section_reloc_link_order)
12140 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
12141 else
12142 sym_name = link_order->u.reloc.p->u.name;
12143 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
12144 howto->name, addend, NULL, NULL,
12145 (bfd_vma) 0);
12146 break;
12147 }
12148
12149 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
12150 output_section);
12151 ok = bfd_set_section_contents (output_bfd, output_section, buf,
12152 octets, size);
12153 free (buf);
12154 if (! ok)
12155 return false;
12156 }
12157
12158 /* The address of a reloc is relative to the section in a
12159 relocatable file, and is a virtual address in an executable
12160 file. */
12161 offset = link_order->offset;
12162 if (! bfd_link_relocatable (info))
12163 offset += output_section->vma;
12164
12165 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
12166 {
12167 irel[i].r_offset = offset;
12168 irel[i].r_info = 0;
12169 irel[i].r_addend = 0;
12170 }
12171 if (bed->s->arch_size == 32)
12172 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
12173 else
12174 #ifdef BFD64
12175 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
12176 #else
12177 BFD_FAIL();
12178 #endif
12179
12180 rel_hdr = reldata->hdr;
12181 erel = rel_hdr->contents;
12182 if (rel_hdr->sh_type == SHT_REL)
12183 {
12184 erel += reldata->count * bed->s->sizeof_rel;
12185 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
12186 }
12187 else
12188 {
12189 irel[0].r_addend = addend;
12190 erel += reldata->count * bed->s->sizeof_rela;
12191 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
12192 }
12193
12194 ++reldata->count;
12195
12196 return true;
12197 }
12198
12199 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
12200 Returns TRUE upon success, FALSE otherwise. */
12201
12202 static bool
elf_output_implib(bfd * abfd,struct bfd_link_info * info)12203 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
12204 {
12205 bool ret = false;
12206 bfd *implib_bfd;
12207 const struct elf_backend_data *bed;
12208 flagword flags;
12209 enum bfd_architecture arch;
12210 unsigned int mach;
12211 asymbol **sympp = NULL;
12212 long symsize;
12213 long symcount;
12214 long src_count;
12215 elf_symbol_type *osymbuf;
12216 size_t amt;
12217
12218 implib_bfd = info->out_implib_bfd;
12219 bed = get_elf_backend_data (abfd);
12220
12221 if (!bfd_set_format (implib_bfd, bfd_object))
12222 return false;
12223
12224 /* Use flag from executable but make it a relocatable object. */
12225 flags = bfd_get_file_flags (abfd);
12226 flags &= ~HAS_RELOC;
12227 if (!bfd_set_start_address (implib_bfd, 0)
12228 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
12229 return false;
12230
12231 /* Copy architecture of output file to import library file. */
12232 arch = bfd_get_arch (abfd);
12233 mach = bfd_get_mach (abfd);
12234 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
12235 && (abfd->target_defaulted
12236 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
12237 return false;
12238
12239 /* Get symbol table size. */
12240 symsize = bfd_get_symtab_upper_bound (abfd);
12241 if (symsize < 0)
12242 return false;
12243
12244 /* Read in the symbol table. */
12245 sympp = (asymbol **) bfd_malloc (symsize);
12246 if (sympp == NULL)
12247 return false;
12248
12249 symcount = bfd_canonicalize_symtab (abfd, sympp);
12250 if (symcount < 0)
12251 goto free_sym_buf;
12252
12253 /* Allow the BFD backend to copy any private header data it
12254 understands from the output BFD to the import library BFD. */
12255 if (! bfd_copy_private_header_data (abfd, implib_bfd))
12256 goto free_sym_buf;
12257
12258 /* Filter symbols to appear in the import library. */
12259 if (bed->elf_backend_filter_implib_symbols)
12260 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
12261 symcount);
12262 else
12263 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
12264 if (symcount == 0)
12265 {
12266 bfd_set_error (bfd_error_no_symbols);
12267 _bfd_error_handler (_("%pB: no symbol found for import library"),
12268 implib_bfd);
12269 goto free_sym_buf;
12270 }
12271
12272
12273 /* Make symbols absolute. */
12274 amt = symcount * sizeof (*osymbuf);
12275 osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
12276 if (osymbuf == NULL)
12277 goto free_sym_buf;
12278
12279 for (src_count = 0; src_count < symcount; src_count++)
12280 {
12281 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
12282 sizeof (*osymbuf));
12283 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
12284 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
12285 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
12286 osymbuf[src_count].internal_elf_sym.st_value =
12287 osymbuf[src_count].symbol.value;
12288 sympp[src_count] = &osymbuf[src_count].symbol;
12289 }
12290
12291 bfd_set_symtab (implib_bfd, sympp, symcount);
12292
12293 /* Allow the BFD backend to copy any private data it understands
12294 from the output BFD to the import library BFD. This is done last
12295 to permit the routine to look at the filtered symbol table. */
12296 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
12297 goto free_sym_buf;
12298
12299 if (!bfd_close (implib_bfd))
12300 goto free_sym_buf;
12301
12302 ret = true;
12303
12304 free_sym_buf:
12305 free (sympp);
12306 return ret;
12307 }
12308
12309 static void
elf_final_link_free(bfd * obfd,struct elf_final_link_info * flinfo)12310 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
12311 {
12312 asection *o;
12313
12314 if (flinfo->symstrtab != NULL)
12315 _bfd_elf_strtab_free (flinfo->symstrtab);
12316 free (flinfo->contents);
12317 free (flinfo->external_relocs);
12318 free (flinfo->internal_relocs);
12319 free (flinfo->external_syms);
12320 free (flinfo->locsym_shndx);
12321 free (flinfo->internal_syms);
12322 free (flinfo->indices);
12323 free (flinfo->sections);
12324 if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
12325 free (flinfo->symshndxbuf);
12326 for (o = obfd->sections; o != NULL; o = o->next)
12327 {
12328 struct bfd_elf_section_data *esdo = elf_section_data (o);
12329 free (esdo->rel.hashes);
12330 free (esdo->rela.hashes);
12331 }
12332 }
12333
12334 /* Do the final step of an ELF link. */
12335
12336 bool
bfd_elf_final_link(bfd * abfd,struct bfd_link_info * info)12337 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
12338 {
12339 bool dynamic;
12340 bool emit_relocs;
12341 bfd *dynobj;
12342 struct elf_final_link_info flinfo;
12343 asection *o;
12344 struct bfd_link_order *p;
12345 bfd *sub;
12346 bfd_size_type max_contents_size;
12347 bfd_size_type max_external_reloc_size;
12348 bfd_size_type max_internal_reloc_count;
12349 bfd_size_type max_sym_count;
12350 bfd_size_type max_sym_shndx_count;
12351 Elf_Internal_Sym elfsym;
12352 unsigned int i;
12353 Elf_Internal_Shdr *symtab_hdr;
12354 Elf_Internal_Shdr *symtab_shndx_hdr;
12355 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12356 struct elf_outext_info eoinfo;
12357 bool merged;
12358 size_t relativecount;
12359 size_t relr_entsize;
12360 asection *reldyn = 0;
12361 bfd_size_type amt;
12362 asection *attr_section = NULL;
12363 bfd_vma attr_size = 0;
12364 const char *std_attrs_section;
12365 struct elf_link_hash_table *htab = elf_hash_table (info);
12366 bool sections_removed;
12367 bool ret;
12368
12369 if (!is_elf_hash_table (&htab->root))
12370 return false;
12371
12372 if (bfd_link_pic (info))
12373 abfd->flags |= DYNAMIC;
12374
12375 dynamic = htab->dynamic_sections_created;
12376 dynobj = htab->dynobj;
12377
12378 emit_relocs = (bfd_link_relocatable (info)
12379 || info->emitrelocations);
12380
12381 memset (&flinfo, 0, sizeof (flinfo));
12382 flinfo.info = info;
12383 flinfo.output_bfd = abfd;
12384 flinfo.symstrtab = _bfd_elf_strtab_init ();
12385 if (flinfo.symstrtab == NULL)
12386 return false;
12387
12388 if (! dynamic)
12389 {
12390 flinfo.hash_sec = NULL;
12391 flinfo.symver_sec = NULL;
12392 }
12393 else
12394 {
12395 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
12396 /* Note that dynsym_sec can be NULL (on VMS). */
12397 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
12398 /* Note that it is OK if symver_sec is NULL. */
12399 }
12400
12401 if (info->unique_symbol
12402 && !bfd_hash_table_init (&flinfo.local_hash_table,
12403 local_hash_newfunc,
12404 sizeof (struct local_hash_entry)))
12405 return false;
12406
12407 /* The object attributes have been merged. Remove the input
12408 sections from the link, and set the contents of the output
12409 section. */
12410 sections_removed = false;
12411 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
12412 for (o = abfd->sections; o != NULL; o = o->next)
12413 {
12414 bool remove_section = false;
12415
12416 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
12417 || strcmp (o->name, ".gnu.attributes") == 0)
12418 {
12419 for (p = o->map_head.link_order; p != NULL; p = p->next)
12420 {
12421 asection *input_section;
12422
12423 if (p->type != bfd_indirect_link_order)
12424 continue;
12425 input_section = p->u.indirect.section;
12426 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12427 elf_link_input_bfd ignores this section. */
12428 input_section->flags &= ~SEC_HAS_CONTENTS;
12429 }
12430
12431 attr_size = bfd_elf_obj_attr_size (abfd);
12432 bfd_set_section_size (o, attr_size);
12433 /* Skip this section later on. */
12434 o->map_head.link_order = NULL;
12435 if (attr_size)
12436 attr_section = o;
12437 else
12438 remove_section = true;
12439 }
12440 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12441 {
12442 /* Remove empty group section from linker output. */
12443 remove_section = true;
12444 }
12445 if (remove_section)
12446 {
12447 o->flags |= SEC_EXCLUDE;
12448 bfd_section_list_remove (abfd, o);
12449 abfd->section_count--;
12450 sections_removed = true;
12451 }
12452 }
12453 if (sections_removed)
12454 _bfd_fix_excluded_sec_syms (abfd, info);
12455
12456 /* Count up the number of relocations we will output for each output
12457 section, so that we know the sizes of the reloc sections. We
12458 also figure out some maximum sizes. */
12459 max_contents_size = 0;
12460 max_external_reloc_size = 0;
12461 max_internal_reloc_count = 0;
12462 max_sym_count = 0;
12463 max_sym_shndx_count = 0;
12464 merged = false;
12465 for (o = abfd->sections; o != NULL; o = o->next)
12466 {
12467 struct bfd_elf_section_data *esdo = elf_section_data (o);
12468 o->reloc_count = 0;
12469
12470 for (p = o->map_head.link_order; p != NULL; p = p->next)
12471 {
12472 unsigned int reloc_count = 0;
12473 unsigned int additional_reloc_count = 0;
12474 struct bfd_elf_section_data *esdi = NULL;
12475
12476 if (p->type == bfd_section_reloc_link_order
12477 || p->type == bfd_symbol_reloc_link_order)
12478 reloc_count = 1;
12479 else if (p->type == bfd_indirect_link_order)
12480 {
12481 asection *sec;
12482
12483 sec = p->u.indirect.section;
12484
12485 /* Mark all sections which are to be included in the
12486 link. This will normally be every section. We need
12487 to do this so that we can identify any sections which
12488 the linker has decided to not include. */
12489 sec->linker_mark = true;
12490
12491 if (sec->flags & SEC_MERGE)
12492 merged = true;
12493
12494 if (sec->rawsize > max_contents_size)
12495 max_contents_size = sec->rawsize;
12496 if (sec->size > max_contents_size)
12497 max_contents_size = sec->size;
12498
12499 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12500 && (sec->owner->flags & DYNAMIC) == 0)
12501 {
12502 size_t sym_count;
12503
12504 /* We are interested in just local symbols, not all
12505 symbols. */
12506 if (elf_bad_symtab (sec->owner))
12507 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12508 / bed->s->sizeof_sym);
12509 else
12510 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12511
12512 if (sym_count > max_sym_count)
12513 max_sym_count = sym_count;
12514
12515 if (sym_count > max_sym_shndx_count
12516 && elf_symtab_shndx_list (sec->owner) != NULL)
12517 max_sym_shndx_count = sym_count;
12518
12519 esdi = elf_section_data (sec);
12520
12521 if (esdi->this_hdr.sh_type == SHT_REL
12522 || esdi->this_hdr.sh_type == SHT_RELA)
12523 /* Some backends use reloc_count in relocation sections
12524 to count particular types of relocs. Of course,
12525 reloc sections themselves can't have relocations. */
12526 ;
12527 else if (emit_relocs)
12528 {
12529 reloc_count = sec->reloc_count;
12530 if (bed->elf_backend_count_additional_relocs)
12531 {
12532 int c;
12533 c = (*bed->elf_backend_count_additional_relocs) (sec);
12534 additional_reloc_count += c;
12535 }
12536 }
12537 else if (bed->elf_backend_count_relocs)
12538 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12539
12540 if ((sec->flags & SEC_RELOC) != 0)
12541 {
12542 size_t ext_size = 0;
12543
12544 if (esdi->rel.hdr != NULL)
12545 ext_size = esdi->rel.hdr->sh_size;
12546 if (esdi->rela.hdr != NULL)
12547 ext_size += esdi->rela.hdr->sh_size;
12548
12549 if (ext_size > max_external_reloc_size)
12550 max_external_reloc_size = ext_size;
12551 if (sec->reloc_count > max_internal_reloc_count)
12552 max_internal_reloc_count = sec->reloc_count;
12553 }
12554 }
12555 }
12556
12557 if (reloc_count == 0)
12558 continue;
12559
12560 reloc_count += additional_reloc_count;
12561 o->reloc_count += reloc_count;
12562
12563 if (p->type == bfd_indirect_link_order && emit_relocs)
12564 {
12565 if (esdi->rel.hdr)
12566 {
12567 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
12568 esdo->rel.count += additional_reloc_count;
12569 }
12570 if (esdi->rela.hdr)
12571 {
12572 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
12573 esdo->rela.count += additional_reloc_count;
12574 }
12575 }
12576 else
12577 {
12578 if (o->use_rela_p)
12579 esdo->rela.count += reloc_count;
12580 else
12581 esdo->rel.count += reloc_count;
12582 }
12583 }
12584
12585 if (o->reloc_count > 0)
12586 o->flags |= SEC_RELOC;
12587 else
12588 {
12589 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12590 set it (this is probably a bug) and if it is set
12591 assign_section_numbers will create a reloc section. */
12592 o->flags &=~ SEC_RELOC;
12593 }
12594
12595 /* If the SEC_ALLOC flag is not set, force the section VMA to
12596 zero. This is done in elf_fake_sections as well, but forcing
12597 the VMA to 0 here will ensure that relocs against these
12598 sections are handled correctly. */
12599 if ((o->flags & SEC_ALLOC) == 0
12600 && ! o->user_set_vma)
12601 o->vma = 0;
12602 }
12603
12604 if (! bfd_link_relocatable (info) && merged)
12605 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
12606
12607 /* Figure out the file positions for everything but the symbol table
12608 and the relocs. We set symcount to force assign_section_numbers
12609 to create a symbol table. */
12610 abfd->symcount = info->strip != strip_all || emit_relocs;
12611 BFD_ASSERT (! abfd->output_has_begun);
12612 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12613 goto error_return;
12614
12615 /* Set sizes, and assign file positions for reloc sections. */
12616 for (o = abfd->sections; o != NULL; o = o->next)
12617 {
12618 struct bfd_elf_section_data *esdo = elf_section_data (o);
12619 if ((o->flags & SEC_RELOC) != 0)
12620 {
12621 if (esdo->rel.hdr
12622 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
12623 goto error_return;
12624
12625 if (esdo->rela.hdr
12626 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
12627 goto error_return;
12628 }
12629
12630 /* _bfd_elf_compute_section_file_positions makes temporary use
12631 of target_index. Reset it. */
12632 o->target_index = 0;
12633
12634 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12635 to count upwards while actually outputting the relocations. */
12636 esdo->rel.count = 0;
12637 esdo->rela.count = 0;
12638
12639 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12640 && !bfd_section_is_ctf (o))
12641 {
12642 /* Cache the section contents so that they can be compressed
12643 later. Use bfd_malloc since it will be freed by
12644 bfd_compress_section_contents. */
12645 unsigned char *contents = esdo->this_hdr.contents;
12646 if (contents != NULL)
12647 abort ();
12648 contents
12649 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12650 if (contents == NULL)
12651 goto error_return;
12652 esdo->this_hdr.contents = contents;
12653 }
12654 }
12655
12656 /* We have now assigned file positions for all the sections except .symtab,
12657 .strtab, and non-loaded reloc and compressed debugging sections. We start
12658 the .symtab section at the current file position, and write directly to it.
12659 We build the .strtab section in memory. */
12660 abfd->symcount = 0;
12661 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12662 /* sh_name is set in prep_headers. */
12663 symtab_hdr->sh_type = SHT_SYMTAB;
12664 /* sh_flags, sh_addr and sh_size all start off zero. */
12665 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12666 /* sh_link is set in assign_section_numbers. */
12667 /* sh_info is set below. */
12668 /* sh_offset is set just below. */
12669 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
12670
12671 if (max_sym_count < 20)
12672 max_sym_count = 20;
12673 htab->strtabsize = max_sym_count;
12674 amt = max_sym_count * sizeof (struct elf_sym_strtab);
12675 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12676 if (htab->strtab == NULL)
12677 goto error_return;
12678 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12679 flinfo.symshndxbuf
12680 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12681 ? (Elf_External_Sym_Shndx *) -1 : NULL);
12682
12683 if (info->strip != strip_all || emit_relocs)
12684 {
12685 file_ptr off = elf_next_file_pos (abfd);
12686
12687 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true);
12688
12689 /* Note that at this point elf_next_file_pos (abfd) is
12690 incorrect. We do not yet know the size of the .symtab section.
12691 We correct next_file_pos below, after we do know the size. */
12692
12693 /* Start writing out the symbol table. The first symbol is always a
12694 dummy symbol. */
12695 elfsym.st_value = 0;
12696 elfsym.st_size = 0;
12697 elfsym.st_info = 0;
12698 elfsym.st_other = 0;
12699 elfsym.st_shndx = SHN_UNDEF;
12700 elfsym.st_target_internal = 0;
12701 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12702 bfd_und_section_ptr, NULL) != 1)
12703 goto error_return;
12704
12705 /* Output a symbol for each section if asked or they are used for
12706 relocs. These symbols usually have no names. We store the
12707 index of each one in the index field of the section, so that
12708 we can find it again when outputting relocs. */
12709
12710 if (bfd_keep_unused_section_symbols (abfd) || emit_relocs)
12711 {
12712 bool name_local_sections
12713 = (bed->elf_backend_name_local_section_symbols
12714 && bed->elf_backend_name_local_section_symbols (abfd));
12715 const char *name = NULL;
12716
12717 elfsym.st_size = 0;
12718 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12719 elfsym.st_other = 0;
12720 elfsym.st_value = 0;
12721 elfsym.st_target_internal = 0;
12722 for (i = 1; i < elf_numsections (abfd); i++)
12723 {
12724 o = bfd_section_from_elf_index (abfd, i);
12725 if (o != NULL)
12726 {
12727 o->target_index = bfd_get_symcount (abfd);
12728 elfsym.st_shndx = i;
12729 if (!bfd_link_relocatable (info))
12730 elfsym.st_value = o->vma;
12731 if (name_local_sections)
12732 name = o->name;
12733 if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
12734 NULL) != 1)
12735 goto error_return;
12736 }
12737 }
12738 }
12739 }
12740
12741 /* On some targets like Irix 5 the symbol split between local and global
12742 ones recorded in the sh_info field needs to be done between section
12743 and all other symbols. */
12744 if (bed->elf_backend_elfsym_local_is_section
12745 && bed->elf_backend_elfsym_local_is_section (abfd))
12746 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12747
12748 /* Allocate some memory to hold information read in from the input
12749 files. */
12750 if (max_contents_size != 0)
12751 {
12752 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12753 if (flinfo.contents == NULL)
12754 goto error_return;
12755 }
12756
12757 if (max_external_reloc_size != 0)
12758 {
12759 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12760 if (flinfo.external_relocs == NULL)
12761 goto error_return;
12762 }
12763
12764 if (max_internal_reloc_count != 0)
12765 {
12766 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12767 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12768 if (flinfo.internal_relocs == NULL)
12769 goto error_return;
12770 }
12771
12772 if (max_sym_count != 0)
12773 {
12774 amt = max_sym_count * bed->s->sizeof_sym;
12775 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12776 if (flinfo.external_syms == NULL)
12777 goto error_return;
12778
12779 amt = max_sym_count * sizeof (Elf_Internal_Sym);
12780 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12781 if (flinfo.internal_syms == NULL)
12782 goto error_return;
12783
12784 amt = max_sym_count * sizeof (long);
12785 flinfo.indices = (long int *) bfd_malloc (amt);
12786 if (flinfo.indices == NULL)
12787 goto error_return;
12788
12789 amt = max_sym_count * sizeof (asection *);
12790 flinfo.sections = (asection **) bfd_malloc (amt);
12791 if (flinfo.sections == NULL)
12792 goto error_return;
12793 }
12794
12795 if (max_sym_shndx_count != 0)
12796 {
12797 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
12798 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12799 if (flinfo.locsym_shndx == NULL)
12800 goto error_return;
12801 }
12802
12803 if (htab->tls_sec)
12804 {
12805 bfd_vma base, end = 0; /* Both bytes. */
12806 asection *sec;
12807
12808 for (sec = htab->tls_sec;
12809 sec && (sec->flags & SEC_THREAD_LOCAL);
12810 sec = sec->next)
12811 {
12812 bfd_size_type size = sec->size;
12813 unsigned int opb = bfd_octets_per_byte (abfd, sec);
12814
12815 if (size == 0
12816 && (sec->flags & SEC_HAS_CONTENTS) == 0)
12817 {
12818 struct bfd_link_order *ord = sec->map_tail.link_order;
12819
12820 if (ord != NULL)
12821 size = ord->offset * opb + ord->size;
12822 }
12823 end = sec->vma + size / opb;
12824 }
12825 base = htab->tls_sec->vma;
12826 /* Only align end of TLS section if static TLS doesn't have special
12827 alignment requirements. */
12828 if (bed->static_tls_alignment == 1)
12829 end = align_power (end, htab->tls_sec->alignment_power);
12830 htab->tls_size = end - base;
12831 }
12832
12833 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12834 return false;
12835
12836 /* Finish relative relocations here after regular symbol processing
12837 is finished if DT_RELR is enabled. */
12838 if (info->enable_dt_relr
12839 && bed->finish_relative_relocs
12840 && !bed->finish_relative_relocs (info))
12841 info->callbacks->einfo
12842 (_("%F%P: %pB: failed to finish relative relocations\n"), abfd);
12843
12844 /* Since ELF permits relocations to be against local symbols, we
12845 must have the local symbols available when we do the relocations.
12846 Since we would rather only read the local symbols once, and we
12847 would rather not keep them in memory, we handle all the
12848 relocations for a single input file at the same time.
12849
12850 Unfortunately, there is no way to know the total number of local
12851 symbols until we have seen all of them, and the local symbol
12852 indices precede the global symbol indices. This means that when
12853 we are generating relocatable output, and we see a reloc against
12854 a global symbol, we can not know the symbol index until we have
12855 finished examining all the local symbols to see which ones we are
12856 going to output. To deal with this, we keep the relocations in
12857 memory, and don't output them until the end of the link. This is
12858 an unfortunate waste of memory, but I don't see a good way around
12859 it. Fortunately, it only happens when performing a relocatable
12860 link, which is not the common case. FIXME: If keep_memory is set
12861 we could write the relocs out and then read them again; I don't
12862 know how bad the memory loss will be. */
12863
12864 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12865 sub->output_has_begun = false;
12866 for (o = abfd->sections; o != NULL; o = o->next)
12867 {
12868 for (p = o->map_head.link_order; p != NULL; p = p->next)
12869 {
12870 if (p->type == bfd_indirect_link_order
12871 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12872 == bfd_target_elf_flavour)
12873 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12874 {
12875 if (! sub->output_has_begun)
12876 {
12877 if (! elf_link_input_bfd (&flinfo, sub))
12878 goto error_return;
12879 sub->output_has_begun = true;
12880 }
12881 }
12882 else if (p->type == bfd_section_reloc_link_order
12883 || p->type == bfd_symbol_reloc_link_order)
12884 {
12885 if (! elf_reloc_link_order (abfd, info, o, p))
12886 goto error_return;
12887 }
12888 else
12889 {
12890 if (! _bfd_default_link_order (abfd, info, o, p))
12891 {
12892 if (p->type == bfd_indirect_link_order
12893 && (bfd_get_flavour (sub)
12894 == bfd_target_elf_flavour)
12895 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12896 != bed->s->elfclass))
12897 {
12898 const char *iclass, *oclass;
12899
12900 switch (bed->s->elfclass)
12901 {
12902 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12903 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12904 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12905 default: abort ();
12906 }
12907
12908 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12909 {
12910 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12911 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12912 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12913 default: abort ();
12914 }
12915
12916 bfd_set_error (bfd_error_wrong_format);
12917 _bfd_error_handler
12918 /* xgettext:c-format */
12919 (_("%pB: file class %s incompatible with %s"),
12920 sub, iclass, oclass);
12921 }
12922
12923 goto error_return;
12924 }
12925 }
12926 }
12927 }
12928
12929 /* Free symbol buffer if needed. */
12930 if (!info->reduce_memory_overheads)
12931 {
12932 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12933 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
12934 {
12935 free (elf_tdata (sub)->symbuf);
12936 elf_tdata (sub)->symbuf = NULL;
12937 }
12938 }
12939
12940 ret = true;
12941
12942 /* Output any global symbols that got converted to local in a
12943 version script or due to symbol visibility. We do this in a
12944 separate step since ELF requires all local symbols to appear
12945 prior to any global symbols. FIXME: We should only do this if
12946 some global symbols were, in fact, converted to become local.
12947 FIXME: Will this work correctly with the Irix 5 linker? */
12948 eoinfo.failed = false;
12949 eoinfo.flinfo = &flinfo;
12950 eoinfo.localsyms = true;
12951 eoinfo.file_sym_done = false;
12952 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12953 if (eoinfo.failed)
12954 {
12955 ret = false;
12956 goto return_local_hash_table;
12957 }
12958
12959 /* If backend needs to output some local symbols not present in the hash
12960 table, do it now. */
12961 if (bed->elf_backend_output_arch_local_syms)
12962 {
12963 if (! ((*bed->elf_backend_output_arch_local_syms)
12964 (abfd, info, &flinfo, elf_link_output_symstrtab)))
12965 {
12966 ret = false;
12967 goto return_local_hash_table;
12968 }
12969 }
12970
12971 /* That wrote out all the local symbols. Finish up the symbol table
12972 with the global symbols. Even if we want to strip everything we
12973 can, we still need to deal with those global symbols that got
12974 converted to local in a version script. */
12975
12976 /* The sh_info field records the index of the first non local symbol. */
12977 if (!symtab_hdr->sh_info)
12978 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12979
12980 if (dynamic
12981 && htab->dynsym != NULL
12982 && htab->dynsym->output_section != bfd_abs_section_ptr)
12983 {
12984 Elf_Internal_Sym sym;
12985 bfd_byte *dynsym = htab->dynsym->contents;
12986
12987 o = htab->dynsym->output_section;
12988 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12989
12990 /* Write out the section symbols for the output sections. */
12991 if (bfd_link_pic (info)
12992 || htab->is_relocatable_executable)
12993 {
12994 asection *s;
12995
12996 sym.st_size = 0;
12997 sym.st_name = 0;
12998 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12999 sym.st_other = 0;
13000 sym.st_target_internal = 0;
13001
13002 for (s = abfd->sections; s != NULL; s = s->next)
13003 {
13004 int indx;
13005 bfd_byte *dest;
13006 long dynindx;
13007
13008 dynindx = elf_section_data (s)->dynindx;
13009 if (dynindx <= 0)
13010 continue;
13011 indx = elf_section_data (s)->this_idx;
13012 BFD_ASSERT (indx > 0);
13013 sym.st_shndx = indx;
13014 if (! check_dynsym (abfd, &sym))
13015 {
13016 ret = false;
13017 goto return_local_hash_table;
13018 }
13019 sym.st_value = s->vma;
13020 dest = dynsym + dynindx * bed->s->sizeof_sym;
13021
13022 /* Inform the linker of the addition of this symbol. */
13023
13024 if (info->callbacks->ctf_new_dynsym)
13025 info->callbacks->ctf_new_dynsym (dynindx, &sym);
13026
13027 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
13028 }
13029 }
13030
13031 /* Write out the local dynsyms. */
13032 if (htab->dynlocal)
13033 {
13034 struct elf_link_local_dynamic_entry *e;
13035 for (e = htab->dynlocal; e ; e = e->next)
13036 {
13037 asection *s;
13038 bfd_byte *dest;
13039
13040 /* Copy the internal symbol and turn off visibility.
13041 Note that we saved a word of storage and overwrote
13042 the original st_name with the dynstr_index. */
13043 sym = e->isym;
13044 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
13045 sym.st_shndx = SHN_UNDEF;
13046
13047 s = bfd_section_from_elf_index (e->input_bfd,
13048 e->isym.st_shndx);
13049 if (s != NULL
13050 && s->output_section != NULL
13051 && elf_section_data (s->output_section) != NULL)
13052 {
13053 sym.st_shndx =
13054 elf_section_data (s->output_section)->this_idx;
13055 if (! check_dynsym (abfd, &sym))
13056 {
13057 ret = false;
13058 goto return_local_hash_table;
13059 }
13060 sym.st_value = (s->output_section->vma
13061 + s->output_offset
13062 + e->isym.st_value);
13063 }
13064
13065 /* Inform the linker of the addition of this symbol. */
13066
13067 if (info->callbacks->ctf_new_dynsym)
13068 info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
13069
13070 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
13071 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
13072 }
13073 }
13074 }
13075
13076 /* We get the global symbols from the hash table. */
13077 eoinfo.failed = false;
13078 eoinfo.localsyms = false;
13079 eoinfo.flinfo = &flinfo;
13080 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
13081 if (eoinfo.failed)
13082 {
13083 ret = false;
13084 goto return_local_hash_table;
13085 }
13086
13087 /* If backend needs to output some symbols not present in the hash
13088 table, do it now. */
13089 if (bed->elf_backend_output_arch_syms
13090 && (info->strip != strip_all || emit_relocs))
13091 {
13092 if (! ((*bed->elf_backend_output_arch_syms)
13093 (abfd, info, &flinfo, elf_link_output_symstrtab)))
13094 {
13095 ret = false;
13096 goto return_local_hash_table;
13097 }
13098 }
13099
13100 /* Finalize the .strtab section. */
13101 _bfd_elf_strtab_finalize (flinfo.symstrtab);
13102
13103 /* Swap out the .strtab section. */
13104 if (!elf_link_swap_symbols_out (&flinfo))
13105 {
13106 ret = false;
13107 goto return_local_hash_table;
13108 }
13109
13110 /* Now we know the size of the symtab section. */
13111 if (bfd_get_symcount (abfd) > 0)
13112 {
13113 /* Finish up and write out the symbol string table (.strtab)
13114 section. */
13115 Elf_Internal_Shdr *symstrtab_hdr = NULL;
13116 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
13117
13118 if (elf_symtab_shndx_list (abfd))
13119 {
13120 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
13121
13122 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
13123 {
13124 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
13125 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
13126 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
13127 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
13128 symtab_shndx_hdr->sh_size = amt;
13129
13130 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
13131 off, true);
13132
13133 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
13134 || (bfd_write (flinfo.symshndxbuf, amt, abfd) != amt))
13135 {
13136 ret = false;
13137 goto return_local_hash_table;
13138 }
13139 }
13140 }
13141
13142 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
13143 /* sh_name was set in prep_headers. */
13144 symstrtab_hdr->sh_type = SHT_STRTAB;
13145 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
13146 symstrtab_hdr->sh_addr = 0;
13147 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
13148 symstrtab_hdr->sh_entsize = 0;
13149 symstrtab_hdr->sh_link = 0;
13150 symstrtab_hdr->sh_info = 0;
13151 /* sh_offset is set just below. */
13152 symstrtab_hdr->sh_addralign = 1;
13153
13154 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
13155 off, true);
13156 elf_next_file_pos (abfd) = off;
13157
13158 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
13159 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
13160 {
13161 ret = false;
13162 goto return_local_hash_table;
13163 }
13164 }
13165
13166 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
13167 {
13168 _bfd_error_handler (_("%pB: failed to generate import library"),
13169 info->out_implib_bfd);
13170 ret = false;
13171 goto return_local_hash_table;
13172 }
13173
13174 /* Adjust the relocs to have the correct symbol indices. */
13175 for (o = abfd->sections; o != NULL; o = o->next)
13176 {
13177 struct bfd_elf_section_data *esdo = elf_section_data (o);
13178 bool sort;
13179
13180 if ((o->flags & SEC_RELOC) == 0)
13181 continue;
13182
13183 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
13184 if (esdo->rel.hdr != NULL
13185 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
13186 {
13187 ret = false;
13188 goto return_local_hash_table;
13189 }
13190 if (esdo->rela.hdr != NULL
13191 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
13192 {
13193 ret = false;
13194 goto return_local_hash_table;
13195 }
13196
13197 /* Set the reloc_count field to 0 to prevent write_relocs from
13198 trying to swap the relocs out itself. */
13199 o->reloc_count = 0;
13200 }
13201
13202 relativecount = 0;
13203 if (dynamic && info->combreloc && dynobj != NULL)
13204 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
13205
13206 relr_entsize = 0;
13207 if (htab->srelrdyn != NULL
13208 && htab->srelrdyn->output_section != NULL
13209 && htab->srelrdyn->size != 0)
13210 {
13211 asection *s = htab->srelrdyn->output_section;
13212 relr_entsize = elf_section_data (s)->this_hdr.sh_entsize;
13213 if (relr_entsize == 0)
13214 {
13215 relr_entsize = bed->s->arch_size / 8;
13216 elf_section_data (s)->this_hdr.sh_entsize = relr_entsize;
13217 }
13218 }
13219
13220 /* If we are linking against a dynamic object, or generating a
13221 shared library, finish up the dynamic linking information. */
13222 if (dynamic)
13223 {
13224 bfd_byte *dyncon, *dynconend;
13225
13226 /* Fix up .dynamic entries. */
13227 o = bfd_get_linker_section (dynobj, ".dynamic");
13228 BFD_ASSERT (o != NULL);
13229
13230 dyncon = o->contents;
13231 dynconend = PTR_ADD (o->contents, o->size);
13232 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13233 {
13234 Elf_Internal_Dyn dyn;
13235 const char *name;
13236 unsigned int type;
13237 bfd_size_type sh_size;
13238 bfd_vma sh_addr;
13239
13240 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13241
13242 switch (dyn.d_tag)
13243 {
13244 default:
13245 continue;
13246 case DT_NULL:
13247 if (relativecount != 0)
13248 {
13249 switch (elf_section_data (reldyn)->this_hdr.sh_type)
13250 {
13251 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
13252 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
13253 }
13254 if (dyn.d_tag != DT_NULL
13255 && dynconend - dyncon >= bed->s->sizeof_dyn)
13256 {
13257 dyn.d_un.d_val = relativecount;
13258 relativecount = 0;
13259 break;
13260 }
13261 relativecount = 0;
13262 }
13263 if (relr_entsize != 0)
13264 {
13265 if (dynconend - dyncon >= 3 * bed->s->sizeof_dyn)
13266 {
13267 asection *s = htab->srelrdyn;
13268 dyn.d_tag = DT_RELR;
13269 dyn.d_un.d_ptr
13270 = s->output_section->vma + s->output_offset;
13271 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13272 dyncon += bed->s->sizeof_dyn;
13273
13274 dyn.d_tag = DT_RELRSZ;
13275 dyn.d_un.d_val = s->size;
13276 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13277 dyncon += bed->s->sizeof_dyn;
13278
13279 dyn.d_tag = DT_RELRENT;
13280 dyn.d_un.d_val = relr_entsize;
13281 relr_entsize = 0;
13282 break;
13283 }
13284 relr_entsize = 0;
13285 }
13286 continue;
13287
13288 case DT_INIT:
13289 name = info->init_function;
13290 goto get_sym;
13291 case DT_FINI:
13292 name = info->fini_function;
13293 get_sym:
13294 {
13295 struct elf_link_hash_entry *h;
13296
13297 h = elf_link_hash_lookup (htab, name, false, false, true);
13298 if (h != NULL
13299 && (h->root.type == bfd_link_hash_defined
13300 || h->root.type == bfd_link_hash_defweak))
13301 {
13302 dyn.d_un.d_ptr = h->root.u.def.value;
13303 o = h->root.u.def.section;
13304 if (o->output_section != NULL)
13305 dyn.d_un.d_ptr += (o->output_section->vma
13306 + o->output_offset);
13307 else
13308 {
13309 /* The symbol is imported from another shared
13310 library and does not apply to this one. */
13311 dyn.d_un.d_ptr = 0;
13312 }
13313 break;
13314 }
13315 }
13316 continue;
13317
13318 case DT_PREINIT_ARRAYSZ:
13319 name = ".preinit_array";
13320 goto get_out_size;
13321 case DT_INIT_ARRAYSZ:
13322 name = ".init_array";
13323 goto get_out_size;
13324 case DT_FINI_ARRAYSZ:
13325 name = ".fini_array";
13326 get_out_size:
13327 o = bfd_get_section_by_name (abfd, name);
13328 if (o == NULL)
13329 {
13330 _bfd_error_handler
13331 (_("could not find section %s"), name);
13332 goto error_return;
13333 }
13334 if (o->size == 0)
13335 _bfd_error_handler
13336 (_("warning: %s section has zero size"), name);
13337 dyn.d_un.d_val = o->size;
13338 break;
13339
13340 case DT_PREINIT_ARRAY:
13341 name = ".preinit_array";
13342 goto get_out_vma;
13343 case DT_INIT_ARRAY:
13344 name = ".init_array";
13345 goto get_out_vma;
13346 case DT_FINI_ARRAY:
13347 name = ".fini_array";
13348 get_out_vma:
13349 o = bfd_get_section_by_name (abfd, name);
13350 goto do_vma;
13351
13352 case DT_HASH:
13353 name = ".hash";
13354 goto get_vma;
13355 case DT_GNU_HASH:
13356 name = ".gnu.hash";
13357 goto get_vma;
13358 case DT_STRTAB:
13359 name = ".dynstr";
13360 goto get_vma;
13361 case DT_SYMTAB:
13362 name = ".dynsym";
13363 goto get_vma;
13364 case DT_VERDEF:
13365 name = ".gnu.version_d";
13366 goto get_vma;
13367 case DT_VERNEED:
13368 name = ".gnu.version_r";
13369 goto get_vma;
13370 case DT_VERSYM:
13371 name = ".gnu.version";
13372 get_vma:
13373 o = bfd_get_linker_section (dynobj, name);
13374 do_vma:
13375 if (o == NULL || bfd_is_abs_section (o->output_section))
13376 {
13377 _bfd_error_handler
13378 (_("could not find section %s"), name);
13379 goto error_return;
13380 }
13381 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
13382 {
13383 _bfd_error_handler
13384 (_("warning: section '%s' is being made into a note"), name);
13385 bfd_set_error (bfd_error_nonrepresentable_section);
13386 goto error_return;
13387 }
13388 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
13389 break;
13390
13391 case DT_REL:
13392 case DT_RELA:
13393 case DT_RELSZ:
13394 case DT_RELASZ:
13395 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
13396 type = SHT_REL;
13397 else
13398 type = SHT_RELA;
13399 sh_size = 0;
13400 sh_addr = 0;
13401 for (i = 1; i < elf_numsections (abfd); i++)
13402 {
13403 Elf_Internal_Shdr *hdr;
13404
13405 hdr = elf_elfsections (abfd)[i];
13406 if (hdr->sh_type == type
13407 && (hdr->sh_flags & SHF_ALLOC) != 0)
13408 {
13409 sh_size += hdr->sh_size;
13410 if (sh_addr == 0
13411 || sh_addr > hdr->sh_addr)
13412 sh_addr = hdr->sh_addr;
13413 }
13414 }
13415
13416 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
13417 {
13418 unsigned int opb = bfd_octets_per_byte (abfd, o);
13419
13420 /* Don't count procedure linkage table relocs in the
13421 overall reloc count. */
13422 sh_size -= htab->srelplt->size;
13423 if (sh_size == 0)
13424 /* If the size is zero, make the address zero too.
13425 This is to avoid a glibc bug. If the backend
13426 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13427 zero, then we'll put DT_RELA at the end of
13428 DT_JMPREL. glibc will interpret the end of
13429 DT_RELA matching the end of DT_JMPREL as the
13430 case where DT_RELA includes DT_JMPREL, and for
13431 LD_BIND_NOW will decide that processing DT_RELA
13432 will process the PLT relocs too. Net result:
13433 No PLT relocs applied. */
13434 sh_addr = 0;
13435
13436 /* If .rela.plt is the first .rela section, exclude
13437 it from DT_RELA. */
13438 else if (sh_addr == (htab->srelplt->output_section->vma
13439 + htab->srelplt->output_offset) * opb)
13440 sh_addr += htab->srelplt->size;
13441 }
13442
13443 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13444 dyn.d_un.d_val = sh_size;
13445 else
13446 dyn.d_un.d_ptr = sh_addr;
13447 break;
13448 }
13449 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13450 }
13451 }
13452
13453 /* If we have created any dynamic sections, then output them. */
13454 if (dynobj != NULL)
13455 {
13456 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
13457 goto error_return;
13458
13459 /* Check for DT_TEXTREL (late, in case the backend removes it). */
13460 if (bfd_link_textrel_check (info)
13461 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL
13462 && o->size != 0)
13463 {
13464 bfd_byte *dyncon, *dynconend;
13465
13466 dyncon = o->contents;
13467 dynconend = o->contents + o->size;
13468 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13469 {
13470 Elf_Internal_Dyn dyn;
13471
13472 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13473
13474 if (dyn.d_tag == DT_TEXTREL)
13475 {
13476 if (info->textrel_check == textrel_check_error)
13477 info->callbacks->einfo
13478 (_("%P%X: read-only segment has dynamic relocations\n"));
13479 else if (bfd_link_dll (info))
13480 info->callbacks->einfo
13481 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13482 else if (bfd_link_pde (info))
13483 info->callbacks->einfo
13484 (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
13485 else
13486 info->callbacks->einfo
13487 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13488 break;
13489 }
13490 }
13491 }
13492
13493 for (o = dynobj->sections; o != NULL; o = o->next)
13494 {
13495 if ((o->flags & SEC_HAS_CONTENTS) == 0
13496 || o->size == 0
13497 || o->output_section == bfd_abs_section_ptr)
13498 continue;
13499 if ((o->flags & SEC_LINKER_CREATED) == 0)
13500 {
13501 /* At this point, we are only interested in sections
13502 created by _bfd_elf_link_create_dynamic_sections. */
13503 continue;
13504 }
13505 if (htab->stab_info.stabstr == o)
13506 continue;
13507 if (htab->eh_info.hdr_sec == o)
13508 continue;
13509 if (strcmp (o->name, ".dynstr") != 0)
13510 {
13511 bfd_size_type octets = ((file_ptr) o->output_offset
13512 * bfd_octets_per_byte (abfd, o));
13513 if (!bfd_set_section_contents (abfd, o->output_section,
13514 o->contents, octets, o->size))
13515 goto error_return;
13516 }
13517 else
13518 {
13519 /* The contents of the .dynstr section are actually in a
13520 stringtab. */
13521 file_ptr off;
13522
13523 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13524 if (bfd_seek (abfd, off, SEEK_SET) != 0
13525 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
13526 goto error_return;
13527 }
13528 }
13529 }
13530
13531 if (!info->resolve_section_groups)
13532 {
13533 bool failed = false;
13534
13535 BFD_ASSERT (bfd_link_relocatable (info));
13536 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13537 if (failed)
13538 goto error_return;
13539 }
13540
13541 /* If we have optimized stabs strings, output them. */
13542 if (htab->stab_info.stabstr != NULL)
13543 {
13544 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
13545 goto error_return;
13546 }
13547
13548 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13549 goto error_return;
13550
13551 if (! _bfd_elf_write_section_sframe (abfd, info))
13552 goto error_return;
13553
13554 if (info->callbacks->emit_ctf)
13555 info->callbacks->emit_ctf ();
13556
13557 elf_final_link_free (abfd, &flinfo);
13558
13559 if (attr_section)
13560 {
13561 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
13562 if (contents == NULL)
13563 {
13564 /* Bail out and fail. */
13565 ret = false;
13566 goto return_local_hash_table;
13567 }
13568 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
13569 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
13570 free (contents);
13571 }
13572
13573 return_local_hash_table:
13574 if (info->unique_symbol)
13575 bfd_hash_table_free (&flinfo.local_hash_table);
13576 return ret;
13577
13578 error_return:
13579 elf_final_link_free (abfd, &flinfo);
13580 ret = false;
13581 goto return_local_hash_table;
13582 }
13583
13584 /* Initialize COOKIE for input bfd ABFD. */
13585
13586 static bool
init_reloc_cookie(struct elf_reloc_cookie * cookie,struct bfd_link_info * info,bfd * abfd)13587 init_reloc_cookie (struct elf_reloc_cookie *cookie,
13588 struct bfd_link_info *info, bfd *abfd)
13589 {
13590 Elf_Internal_Shdr *symtab_hdr;
13591 const struct elf_backend_data *bed;
13592
13593 bed = get_elf_backend_data (abfd);
13594 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13595
13596 cookie->abfd = abfd;
13597 cookie->sym_hashes = elf_sym_hashes (abfd);
13598 cookie->bad_symtab = elf_bad_symtab (abfd);
13599 if (cookie->bad_symtab)
13600 {
13601 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13602 cookie->extsymoff = 0;
13603 }
13604 else
13605 {
13606 cookie->locsymcount = symtab_hdr->sh_info;
13607 cookie->extsymoff = symtab_hdr->sh_info;
13608 }
13609
13610 if (bed->s->arch_size == 32)
13611 cookie->r_sym_shift = 8;
13612 else
13613 cookie->r_sym_shift = 32;
13614
13615 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13616 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13617 {
13618 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13619 cookie->locsymcount, 0,
13620 NULL, NULL, NULL);
13621 if (cookie->locsyms == NULL)
13622 {
13623 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
13624 return false;
13625 }
13626 if (_bfd_link_keep_memory (info) )
13627 {
13628 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13629 info->cache_size += (cookie->locsymcount
13630 * sizeof (Elf_External_Sym_Shndx));
13631 }
13632 }
13633 return true;
13634 }
13635
13636 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13637
13638 static void
fini_reloc_cookie(struct elf_reloc_cookie * cookie,bfd * abfd)13639 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13640 {
13641 Elf_Internal_Shdr *symtab_hdr;
13642
13643 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13644 if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
13645 free (cookie->locsyms);
13646 }
13647
13648 /* Initialize the relocation information in COOKIE for input section SEC
13649 of input bfd ABFD. */
13650
13651 static bool
init_reloc_cookie_rels(struct elf_reloc_cookie * cookie,struct bfd_link_info * info,bfd * abfd,asection * sec)13652 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13653 struct bfd_link_info *info, bfd *abfd,
13654 asection *sec)
13655 {
13656 if (sec->reloc_count == 0)
13657 {
13658 cookie->rels = NULL;
13659 cookie->relend = NULL;
13660 }
13661 else
13662 {
13663 cookie->rels = _bfd_elf_link_info_read_relocs (abfd, info, sec,
13664 NULL, NULL,
13665 _bfd_link_keep_memory (info));
13666 if (cookie->rels == NULL)
13667 return false;
13668 cookie->rel = cookie->rels;
13669 cookie->relend = cookie->rels + sec->reloc_count;
13670 }
13671 cookie->rel = cookie->rels;
13672 return true;
13673 }
13674
13675 /* Free the memory allocated by init_reloc_cookie_rels,
13676 if appropriate. */
13677
13678 static void
fini_reloc_cookie_rels(struct elf_reloc_cookie * cookie,asection * sec)13679 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13680 asection *sec)
13681 {
13682 if (elf_section_data (sec)->relocs != cookie->rels)
13683 free (cookie->rels);
13684 }
13685
13686 /* Initialize the whole of COOKIE for input section SEC. */
13687
13688 static bool
init_reloc_cookie_for_section(struct elf_reloc_cookie * cookie,struct bfd_link_info * info,asection * sec)13689 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13690 struct bfd_link_info *info,
13691 asection *sec)
13692 {
13693 if (!init_reloc_cookie (cookie, info, sec->owner))
13694 goto error1;
13695 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
13696 goto error2;
13697 return true;
13698
13699 error2:
13700 fini_reloc_cookie (cookie, sec->owner);
13701 error1:
13702 return false;
13703 }
13704
13705 /* Free the memory allocated by init_reloc_cookie_for_section,
13706 if appropriate. */
13707
13708 static void
fini_reloc_cookie_for_section(struct elf_reloc_cookie * cookie,asection * sec)13709 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13710 asection *sec)
13711 {
13712 fini_reloc_cookie_rels (cookie, sec);
13713 fini_reloc_cookie (cookie, sec->owner);
13714 }
13715
13716 /* Garbage collect unused sections. */
13717
13718 /* Default gc_mark_hook. */
13719
13720 asection *
_bfd_elf_gc_mark_hook(asection * sec,struct bfd_link_info * info ATTRIBUTE_UNUSED,Elf_Internal_Rela * rel ATTRIBUTE_UNUSED,struct elf_link_hash_entry * h,Elf_Internal_Sym * sym)13721 _bfd_elf_gc_mark_hook (asection *sec,
13722 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13723 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13724 struct elf_link_hash_entry *h,
13725 Elf_Internal_Sym *sym)
13726 {
13727 if (h != NULL)
13728 {
13729 switch (h->root.type)
13730 {
13731 case bfd_link_hash_defined:
13732 case bfd_link_hash_defweak:
13733 return h->root.u.def.section;
13734
13735 case bfd_link_hash_common:
13736 return h->root.u.c.p->section;
13737
13738 default:
13739 break;
13740 }
13741 }
13742 else
13743 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13744
13745 return NULL;
13746 }
13747
13748 /* Return the debug definition section. */
13749
13750 static asection *
elf_gc_mark_debug_section(asection * sec ATTRIBUTE_UNUSED,struct bfd_link_info * info ATTRIBUTE_UNUSED,Elf_Internal_Rela * rel ATTRIBUTE_UNUSED,struct elf_link_hash_entry * h,Elf_Internal_Sym * sym)13751 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13752 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13753 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13754 struct elf_link_hash_entry *h,
13755 Elf_Internal_Sym *sym)
13756 {
13757 if (h != NULL)
13758 {
13759 /* Return the global debug definition section. */
13760 if ((h->root.type == bfd_link_hash_defined
13761 || h->root.type == bfd_link_hash_defweak)
13762 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13763 return h->root.u.def.section;
13764 }
13765 else
13766 {
13767 /* Return the local debug definition section. */
13768 asection *isec = bfd_section_from_elf_index (sec->owner,
13769 sym->st_shndx);
13770 if (isec != NULL && (isec->flags & SEC_DEBUGGING) != 0)
13771 return isec;
13772 }
13773
13774 return NULL;
13775 }
13776
13777 /* COOKIE->rel describes a relocation against section SEC, which is
13778 a section we've decided to keep. Return the section that contains
13779 the relocation symbol, or NULL if no section contains it. */
13780
13781 asection *
_bfd_elf_gc_mark_rsec(struct bfd_link_info * info,asection * sec,elf_gc_mark_hook_fn gc_mark_hook,struct elf_reloc_cookie * cookie,bool * start_stop)13782 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13783 elf_gc_mark_hook_fn gc_mark_hook,
13784 struct elf_reloc_cookie *cookie,
13785 bool *start_stop)
13786 {
13787 unsigned long r_symndx;
13788 struct elf_link_hash_entry *h, *hw;
13789
13790 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
13791 if (r_symndx == STN_UNDEF)
13792 return NULL;
13793
13794 if (r_symndx >= cookie->locsymcount
13795 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13796 {
13797 bool was_marked;
13798
13799 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
13800 if (h == NULL)
13801 {
13802 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
13803 sec->owner);
13804 return NULL;
13805 }
13806 while (h->root.type == bfd_link_hash_indirect
13807 || h->root.type == bfd_link_hash_warning)
13808 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13809
13810 was_marked = h->mark;
13811 h->mark = 1;
13812 /* Keep all aliases of the symbol too. If an object symbol
13813 needs to be copied into .dynbss then all of its aliases
13814 should be present as dynamic symbols, not just the one used
13815 on the copy relocation. */
13816 hw = h;
13817 while (hw->is_weakalias)
13818 {
13819 hw = hw->u.alias;
13820 hw->mark = 1;
13821 }
13822
13823 if (!was_marked && h->start_stop && !h->root.ldscript_def)
13824 {
13825 if (info->start_stop_gc)
13826 return NULL;
13827
13828 /* To work around a glibc bug, mark XXX input sections
13829 when there is a reference to __start_XXX or __stop_XXX
13830 symbols. */
13831 else if (start_stop != NULL)
13832 {
13833 asection *s = h->u2.start_stop_section;
13834 *start_stop = true;
13835 return s;
13836 }
13837 }
13838
13839 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13840 }
13841
13842 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13843 &cookie->locsyms[r_symndx]);
13844 }
13845
13846 /* COOKIE->rel describes a relocation against section SEC, which is
13847 a section we've decided to keep. Mark the section that contains
13848 the relocation symbol. */
13849
13850 bool
_bfd_elf_gc_mark_reloc(struct bfd_link_info * info,asection * sec,elf_gc_mark_hook_fn gc_mark_hook,struct elf_reloc_cookie * cookie)13851 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13852 asection *sec,
13853 elf_gc_mark_hook_fn gc_mark_hook,
13854 struct elf_reloc_cookie *cookie)
13855 {
13856 asection *rsec;
13857 bool start_stop = false;
13858
13859 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13860 while (rsec != NULL)
13861 {
13862 if (!rsec->gc_mark)
13863 {
13864 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13865 || (rsec->owner->flags & DYNAMIC) != 0)
13866 rsec->gc_mark = 1;
13867 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13868 return false;
13869 }
13870 if (!start_stop)
13871 break;
13872 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
13873 }
13874 return true;
13875 }
13876
13877 /* The mark phase of garbage collection. For a given section, mark
13878 it and any sections in this section's group, and all the sections
13879 which define symbols to which it refers. */
13880
13881 bool
_bfd_elf_gc_mark(struct bfd_link_info * info,asection * sec,elf_gc_mark_hook_fn gc_mark_hook)13882 _bfd_elf_gc_mark (struct bfd_link_info *info,
13883 asection *sec,
13884 elf_gc_mark_hook_fn gc_mark_hook)
13885 {
13886 bool ret;
13887 asection *group_sec, *eh_frame;
13888
13889 sec->gc_mark = 1;
13890
13891 /* Mark all the sections in the group. */
13892 group_sec = elf_section_data (sec)->next_in_group;
13893 if (group_sec && !group_sec->gc_mark)
13894 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
13895 return false;
13896
13897 /* Look through the section relocs. */
13898 ret = true;
13899 eh_frame = elf_eh_frame_section (sec->owner);
13900 if ((sec->flags & SEC_RELOC) != 0
13901 && sec->reloc_count > 0
13902 && sec != eh_frame)
13903 {
13904 struct elf_reloc_cookie cookie;
13905
13906 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13907 ret = false;
13908 else
13909 {
13910 for (; cookie.rel < cookie.relend; cookie.rel++)
13911 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
13912 {
13913 ret = false;
13914 break;
13915 }
13916 fini_reloc_cookie_for_section (&cookie, sec);
13917 }
13918 }
13919
13920 if (ret && eh_frame && elf_fde_list (sec))
13921 {
13922 struct elf_reloc_cookie cookie;
13923
13924 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13925 ret = false;
13926 else
13927 {
13928 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13929 gc_mark_hook, &cookie))
13930 ret = false;
13931 fini_reloc_cookie_for_section (&cookie, eh_frame);
13932 }
13933 }
13934
13935 eh_frame = elf_section_eh_frame_entry (sec);
13936 if (ret && eh_frame && !eh_frame->gc_mark)
13937 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13938 ret = false;
13939
13940 return ret;
13941 }
13942
13943 /* Scan and mark sections in a special or debug section group. */
13944
13945 static void
_bfd_elf_gc_mark_debug_special_section_group(asection * grp)13946 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13947 {
13948 /* Point to first section of section group. */
13949 asection *ssec;
13950 /* Used to iterate the section group. */
13951 asection *msec;
13952
13953 bool is_special_grp = true;
13954 bool is_debug_grp = true;
13955
13956 /* First scan to see if group contains any section other than debug
13957 and special section. */
13958 ssec = msec = elf_next_in_group (grp);
13959 do
13960 {
13961 if ((msec->flags & SEC_DEBUGGING) == 0)
13962 is_debug_grp = false;
13963
13964 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13965 is_special_grp = false;
13966
13967 msec = elf_next_in_group (msec);
13968 }
13969 while (msec != ssec);
13970
13971 /* If this is a pure debug section group or pure special section group,
13972 keep all sections in this group. */
13973 if (is_debug_grp || is_special_grp)
13974 {
13975 do
13976 {
13977 msec->gc_mark = 1;
13978 msec = elf_next_in_group (msec);
13979 }
13980 while (msec != ssec);
13981 }
13982 }
13983
13984 /* Keep debug and special sections. */
13985
13986 bool
_bfd_elf_gc_mark_extra_sections(struct bfd_link_info * info,elf_gc_mark_hook_fn mark_hook)13987 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13988 elf_gc_mark_hook_fn mark_hook)
13989 {
13990 bfd *ibfd;
13991
13992 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13993 {
13994 asection *isec;
13995 bool some_kept;
13996 bool debug_frag_seen;
13997 bool has_kept_debug_info;
13998
13999 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14000 continue;
14001 isec = ibfd->sections;
14002 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14003 continue;
14004
14005 /* Ensure all linker created sections are kept,
14006 see if any other section is already marked,
14007 and note if we have any fragmented debug sections. */
14008 debug_frag_seen = some_kept = has_kept_debug_info = false;
14009 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14010 {
14011 if ((isec->flags & SEC_LINKER_CREATED) != 0)
14012 isec->gc_mark = 1;
14013 else if (isec->gc_mark
14014 && (isec->flags & SEC_ALLOC) != 0
14015 && elf_section_type (isec) != SHT_NOTE)
14016 some_kept = true;
14017 else
14018 {
14019 /* Since all sections, except for backend specific ones,
14020 have been garbage collected, call mark_hook on this
14021 section if any of its linked-to sections is marked. */
14022 asection *linked_to_sec;
14023 for (linked_to_sec = elf_linked_to_section (isec);
14024 linked_to_sec != NULL && !linked_to_sec->linker_mark;
14025 linked_to_sec = elf_linked_to_section (linked_to_sec))
14026 {
14027 if (linked_to_sec->gc_mark)
14028 {
14029 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
14030 return false;
14031 break;
14032 }
14033 linked_to_sec->linker_mark = 1;
14034 }
14035 for (linked_to_sec = elf_linked_to_section (isec);
14036 linked_to_sec != NULL && linked_to_sec->linker_mark;
14037 linked_to_sec = elf_linked_to_section (linked_to_sec))
14038 linked_to_sec->linker_mark = 0;
14039 }
14040
14041 if (!debug_frag_seen
14042 && (isec->flags & SEC_DEBUGGING)
14043 && startswith (isec->name, ".debug_line."))
14044 debug_frag_seen = true;
14045 else if (strcmp (bfd_section_name (isec),
14046 "__patchable_function_entries") == 0
14047 && elf_linked_to_section (isec) == NULL)
14048 info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
14049 "need linked-to section "
14050 "for --gc-sections\n"),
14051 isec->owner, isec);
14052 }
14053
14054 /* If no non-note alloc section in this file will be kept, then
14055 we can toss out the debug and special sections. */
14056 if (!some_kept)
14057 continue;
14058
14059 /* Keep debug and special sections like .comment when they are
14060 not part of a group. Also keep section groups that contain
14061 just debug sections or special sections. NB: Sections with
14062 linked-to section has been handled above. */
14063 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14064 {
14065 if ((isec->flags & SEC_GROUP) != 0)
14066 _bfd_elf_gc_mark_debug_special_section_group (isec);
14067 else if (((isec->flags & SEC_DEBUGGING) != 0
14068 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
14069 && elf_next_in_group (isec) == NULL
14070 && elf_linked_to_section (isec) == NULL)
14071 isec->gc_mark = 1;
14072 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
14073 has_kept_debug_info = true;
14074 }
14075
14076 /* Look for CODE sections which are going to be discarded,
14077 and find and discard any fragmented debug sections which
14078 are associated with that code section. */
14079 if (debug_frag_seen)
14080 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14081 if ((isec->flags & SEC_CODE) != 0
14082 && isec->gc_mark == 0)
14083 {
14084 unsigned int ilen;
14085 asection *dsec;
14086
14087 ilen = strlen (isec->name);
14088
14089 /* Association is determined by the name of the debug
14090 section containing the name of the code section as
14091 a suffix. For example .debug_line.text.foo is a
14092 debug section associated with .text.foo. */
14093 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
14094 {
14095 unsigned int dlen;
14096
14097 if (dsec->gc_mark == 0
14098 || (dsec->flags & SEC_DEBUGGING) == 0)
14099 continue;
14100
14101 dlen = strlen (dsec->name);
14102
14103 if (dlen > ilen
14104 && strncmp (dsec->name + (dlen - ilen),
14105 isec->name, ilen) == 0)
14106 dsec->gc_mark = 0;
14107 }
14108 }
14109
14110 /* Mark debug sections referenced by kept debug sections. */
14111 if (has_kept_debug_info)
14112 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14113 if (isec->gc_mark
14114 && (isec->flags & SEC_DEBUGGING) != 0)
14115 if (!_bfd_elf_gc_mark (info, isec,
14116 elf_gc_mark_debug_section))
14117 return false;
14118 }
14119 return true;
14120 }
14121
14122 static bool
elf_gc_sweep(bfd * abfd,struct bfd_link_info * info)14123 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
14124 {
14125 bfd *sub;
14126 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14127
14128 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14129 {
14130 asection *o;
14131
14132 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14133 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
14134 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14135 continue;
14136 o = sub->sections;
14137 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14138 continue;
14139
14140 for (o = sub->sections; o != NULL; o = o->next)
14141 {
14142 /* When any section in a section group is kept, we keep all
14143 sections in the section group. If the first member of
14144 the section group is excluded, we will also exclude the
14145 group section. */
14146 if (o->flags & SEC_GROUP)
14147 {
14148 asection *first = elf_next_in_group (o);
14149 o->gc_mark = first->gc_mark;
14150 }
14151
14152 if (o->gc_mark)
14153 continue;
14154
14155 /* Skip sweeping sections already excluded. */
14156 if (o->flags & SEC_EXCLUDE)
14157 continue;
14158
14159 /* Since this is early in the link process, it is simple
14160 to remove a section from the output. */
14161 o->flags |= SEC_EXCLUDE;
14162
14163 if (info->print_gc_sections && o->size != 0)
14164 /* xgettext:c-format */
14165 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
14166 o, sub);
14167 }
14168 }
14169
14170 return true;
14171 }
14172
14173 /* Propagate collected vtable information. This is called through
14174 elf_link_hash_traverse. */
14175
14176 static bool
elf_gc_propagate_vtable_entries_used(struct elf_link_hash_entry * h,void * okp)14177 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
14178 {
14179 /* Those that are not vtables. */
14180 if (h->start_stop
14181 || h->u2.vtable == NULL
14182 || h->u2.vtable->parent == NULL)
14183 return true;
14184
14185 /* Those vtables that do not have parents, we cannot merge. */
14186 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
14187 return true;
14188
14189 /* If we've already been done, exit. */
14190 if (h->u2.vtable->used && h->u2.vtable->used[-1])
14191 return true;
14192
14193 /* Make sure the parent's table is up to date. */
14194 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
14195
14196 if (h->u2.vtable->used == NULL)
14197 {
14198 /* None of this table's entries were referenced. Re-use the
14199 parent's table. */
14200 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
14201 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
14202 }
14203 else
14204 {
14205 size_t n;
14206 bool *cu, *pu;
14207
14208 /* Or the parent's entries into ours. */
14209 cu = h->u2.vtable->used;
14210 cu[-1] = true;
14211 pu = h->u2.vtable->parent->u2.vtable->used;
14212 if (pu != NULL)
14213 {
14214 const struct elf_backend_data *bed;
14215 unsigned int log_file_align;
14216
14217 bed = get_elf_backend_data (h->root.u.def.section->owner);
14218 log_file_align = bed->s->log_file_align;
14219 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
14220 while (n--)
14221 {
14222 if (*pu)
14223 *cu = true;
14224 pu++;
14225 cu++;
14226 }
14227 }
14228 }
14229
14230 return true;
14231 }
14232
14233 struct link_info_ok
14234 {
14235 struct bfd_link_info *info;
14236 bool ok;
14237 };
14238
14239 static bool
elf_gc_smash_unused_vtentry_relocs(struct elf_link_hash_entry * h,void * ptr)14240 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h,
14241 void *ptr)
14242 {
14243 asection *sec;
14244 bfd_vma hstart, hend;
14245 Elf_Internal_Rela *relstart, *relend, *rel;
14246 const struct elf_backend_data *bed;
14247 unsigned int log_file_align;
14248 struct link_info_ok *info = (struct link_info_ok *) ptr;
14249
14250 /* Take care of both those symbols that do not describe vtables as
14251 well as those that are not loaded. */
14252 if (h->start_stop
14253 || h->u2.vtable == NULL
14254 || h->u2.vtable->parent == NULL)
14255 return true;
14256
14257 BFD_ASSERT (h->root.type == bfd_link_hash_defined
14258 || h->root.type == bfd_link_hash_defweak);
14259
14260 sec = h->root.u.def.section;
14261 hstart = h->root.u.def.value;
14262 hend = hstart + h->size;
14263
14264 relstart = _bfd_elf_link_info_read_relocs (sec->owner, info->info,
14265 sec, NULL, NULL, true);
14266 if (!relstart)
14267 return info->ok = false;
14268 bed = get_elf_backend_data (sec->owner);
14269 log_file_align = bed->s->log_file_align;
14270
14271 relend = relstart + sec->reloc_count;
14272
14273 for (rel = relstart; rel < relend; ++rel)
14274 if (rel->r_offset >= hstart && rel->r_offset < hend)
14275 {
14276 /* If the entry is in use, do nothing. */
14277 if (h->u2.vtable->used
14278 && (rel->r_offset - hstart) < h->u2.vtable->size)
14279 {
14280 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
14281 if (h->u2.vtable->used[entry])
14282 continue;
14283 }
14284 /* Otherwise, kill it. */
14285 rel->r_offset = rel->r_info = rel->r_addend = 0;
14286 }
14287
14288 return true;
14289 }
14290
14291 /* Mark sections containing dynamically referenced symbols. When
14292 building shared libraries, we must assume that any visible symbol is
14293 referenced. */
14294
14295 bool
bfd_elf_gc_mark_dynamic_ref_symbol(struct elf_link_hash_entry * h,void * inf)14296 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
14297 {
14298 struct bfd_link_info *info = (struct bfd_link_info *) inf;
14299 struct bfd_elf_dynamic_list *d = info->dynamic_list;
14300
14301 if ((h->root.type == bfd_link_hash_defined
14302 || h->root.type == bfd_link_hash_defweak)
14303 && (!h->start_stop
14304 || h->root.ldscript_def
14305 || !info->start_stop_gc)
14306 && ((h->ref_dynamic && !h->forced_local)
14307 || ((h->def_regular || ELF_COMMON_DEF_P (h))
14308 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
14309 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
14310 && (!bfd_link_executable (info)
14311 || info->gc_keep_exported
14312 || info->export_dynamic
14313 || (h->dynamic
14314 && d != NULL
14315 && (*d->match) (&d->head, NULL, h->root.root.string)))
14316 && (h->versioned >= versioned
14317 || !bfd_hide_sym_by_version (info->version_info,
14318 h->root.root.string)))))
14319 h->root.u.def.section->flags |= SEC_KEEP;
14320
14321 return true;
14322 }
14323
14324 /* Keep all sections containing symbols undefined on the command-line,
14325 and the section containing the entry symbol. */
14326
14327 void
_bfd_elf_gc_keep(struct bfd_link_info * info)14328 _bfd_elf_gc_keep (struct bfd_link_info *info)
14329 {
14330 struct bfd_sym_chain *sym;
14331
14332 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
14333 {
14334 struct elf_link_hash_entry *h;
14335
14336 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
14337 false, false, false);
14338
14339 if (h != NULL
14340 && (h->root.type == bfd_link_hash_defined
14341 || h->root.type == bfd_link_hash_defweak)
14342 && !bfd_is_const_section (h->root.u.def.section))
14343 h->root.u.def.section->flags |= SEC_KEEP;
14344 }
14345 }
14346
14347 bool
bfd_elf_parse_eh_frame_entries(bfd * abfd ATTRIBUTE_UNUSED,struct bfd_link_info * info)14348 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
14349 struct bfd_link_info *info)
14350 {
14351 bfd *ibfd = info->input_bfds;
14352
14353 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14354 {
14355 asection *sec;
14356 struct elf_reloc_cookie cookie;
14357
14358 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14359 continue;
14360 sec = ibfd->sections;
14361 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14362 continue;
14363
14364 if (!init_reloc_cookie (&cookie, info, ibfd))
14365 return false;
14366
14367 for (sec = ibfd->sections; sec; sec = sec->next)
14368 {
14369 if (startswith (bfd_section_name (sec), ".eh_frame_entry")
14370 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
14371 {
14372 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
14373 fini_reloc_cookie_rels (&cookie, sec);
14374 }
14375 }
14376 }
14377 return true;
14378 }
14379
14380 /* Do mark and sweep of unused sections. */
14381
14382 bool
bfd_elf_gc_sections(bfd * abfd,struct bfd_link_info * info)14383 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
14384 {
14385 bool ok = true;
14386 bfd *sub;
14387 elf_gc_mark_hook_fn gc_mark_hook;
14388 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14389 struct elf_link_hash_table *htab;
14390 struct link_info_ok info_ok;
14391
14392 if (!bed->can_gc_sections
14393 || !is_elf_hash_table (info->hash))
14394 {
14395 _bfd_error_handler(_("warning: gc-sections option ignored"));
14396 return true;
14397 }
14398
14399 bed->gc_keep (info);
14400 htab = elf_hash_table (info);
14401
14402 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14403 at the .eh_frame section if we can mark the FDEs individually. */
14404 for (sub = info->input_bfds;
14405 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
14406 sub = sub->link.next)
14407 {
14408 asection *sec;
14409 struct elf_reloc_cookie cookie;
14410
14411 sec = sub->sections;
14412 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14413 continue;
14414 sec = bfd_get_section_by_name (sub, ".eh_frame");
14415 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
14416 {
14417 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
14418 if (elf_section_data (sec)->sec_info
14419 && (sec->flags & SEC_LINKER_CREATED) == 0)
14420 elf_eh_frame_section (sub) = sec;
14421 fini_reloc_cookie_for_section (&cookie, sec);
14422 sec = bfd_get_next_section_by_name (NULL, sec);
14423 }
14424 }
14425
14426 /* Apply transitive closure to the vtable entry usage info. */
14427 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
14428 if (!ok)
14429 return false;
14430
14431 /* Kill the vtable relocations that were not used. */
14432 info_ok.info = info;
14433 info_ok.ok = true;
14434 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &info_ok);
14435 if (!info_ok.ok)
14436 return false;
14437
14438 /* Mark dynamically referenced symbols. */
14439 if (htab->dynamic_sections_created || info->gc_keep_exported)
14440 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
14441
14442 /* Grovel through relocs to find out who stays ... */
14443 gc_mark_hook = bed->gc_mark_hook;
14444 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14445 {
14446 asection *o;
14447
14448 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14449 || elf_object_id (sub) != elf_hash_table_id (htab)
14450 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14451 continue;
14452
14453 o = sub->sections;
14454 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14455 continue;
14456
14457 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14458 Also treat note sections as a root, if the section is not part
14459 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14460 well as FINI_ARRAY sections for ld -r. */
14461 for (o = sub->sections; o != NULL; o = o->next)
14462 if (!o->gc_mark
14463 && (o->flags & SEC_EXCLUDE) == 0
14464 && ((o->flags & SEC_KEEP) != 0
14465 || (bfd_link_relocatable (info)
14466 && ((elf_section_data (o)->this_hdr.sh_type
14467 == SHT_PREINIT_ARRAY)
14468 || (elf_section_data (o)->this_hdr.sh_type
14469 == SHT_INIT_ARRAY)
14470 || (elf_section_data (o)->this_hdr.sh_type
14471 == SHT_FINI_ARRAY)))
14472 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
14473 && elf_next_in_group (o) == NULL
14474 && elf_linked_to_section (o) == NULL)
14475 || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
14476 && (elf_section_flags (o) & SHF_GNU_RETAIN))))
14477 {
14478 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
14479 return false;
14480 }
14481 }
14482
14483 /* Allow the backend to mark additional target specific sections. */
14484 bed->gc_mark_extra_sections (info, gc_mark_hook);
14485
14486 /* ... and mark SEC_EXCLUDE for those that go. */
14487 return elf_gc_sweep (abfd, info);
14488 }
14489
14490 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14491
14492 bool
bfd_elf_gc_record_vtinherit(bfd * abfd,asection * sec,struct elf_link_hash_entry * h,bfd_vma offset)14493 bfd_elf_gc_record_vtinherit (bfd *abfd,
14494 asection *sec,
14495 struct elf_link_hash_entry *h,
14496 bfd_vma offset)
14497 {
14498 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14499 struct elf_link_hash_entry **search, *child;
14500 size_t extsymcount;
14501 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14502
14503 /* The sh_info field of the symtab header tells us where the
14504 external symbols start. We don't care about the local symbols at
14505 this point. */
14506 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
14507 if (!elf_bad_symtab (abfd))
14508 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
14509
14510 sym_hashes = elf_sym_hashes (abfd);
14511 sym_hashes_end = PTR_ADD (sym_hashes, extsymcount);
14512
14513 /* Hunt down the child symbol, which is in this section at the same
14514 offset as the relocation. */
14515 for (search = sym_hashes; search != sym_hashes_end; ++search)
14516 {
14517 if ((child = *search) != NULL
14518 && (child->root.type == bfd_link_hash_defined
14519 || child->root.type == bfd_link_hash_defweak)
14520 && child->root.u.def.section == sec
14521 && child->root.u.def.value == offset)
14522 goto win;
14523 }
14524
14525 /* xgettext:c-format */
14526 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
14527 abfd, sec, (uint64_t) offset);
14528 bfd_set_error (bfd_error_invalid_operation);
14529 return false;
14530
14531 win:
14532 if (!child->u2.vtable)
14533 {
14534 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
14535 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
14536 if (!child->u2.vtable)
14537 return false;
14538 }
14539 if (!h)
14540 {
14541 /* This *should* only be the absolute section. It could potentially
14542 be that someone has defined a non-global vtable though, which
14543 would be bad. It isn't worth paging in the local symbols to be
14544 sure though; that case should simply be handled by the assembler. */
14545
14546 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
14547 }
14548 else
14549 child->u2.vtable->parent = h;
14550
14551 return true;
14552 }
14553
14554 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14555
14556 bool
bfd_elf_gc_record_vtentry(bfd * abfd,asection * sec,struct elf_link_hash_entry * h,bfd_vma addend)14557 bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
14558 struct elf_link_hash_entry *h,
14559 bfd_vma addend)
14560 {
14561 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14562 unsigned int log_file_align = bed->s->log_file_align;
14563
14564 if (!h)
14565 {
14566 /* xgettext:c-format */
14567 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14568 abfd, sec);
14569 bfd_set_error (bfd_error_bad_value);
14570 return false;
14571 }
14572
14573 if (!h->u2.vtable)
14574 {
14575 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14576 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14577 if (!h->u2.vtable)
14578 return false;
14579 }
14580
14581 if (addend >= h->u2.vtable->size)
14582 {
14583 size_t size, bytes, file_align;
14584 bool *ptr = h->u2.vtable->used;
14585
14586 /* While the symbol is undefined, we have to be prepared to handle
14587 a zero size. */
14588 file_align = 1 << log_file_align;
14589 if (h->root.type == bfd_link_hash_undefined)
14590 size = addend + file_align;
14591 else
14592 {
14593 size = h->size;
14594 if (addend >= size)
14595 {
14596 /* Oops! We've got a reference past the defined end of
14597 the table. This is probably a bug -- shall we warn? */
14598 size = addend + file_align;
14599 }
14600 }
14601 size = (size + file_align - 1) & -file_align;
14602
14603 /* Allocate one extra entry for use as a "done" flag for the
14604 consolidation pass. */
14605 bytes = ((size >> log_file_align) + 1) * sizeof (bool);
14606
14607 if (ptr)
14608 {
14609 ptr = (bool *) bfd_realloc (ptr - 1, bytes);
14610
14611 if (ptr != NULL)
14612 {
14613 size_t oldbytes;
14614
14615 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
14616 * sizeof (bool));
14617 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14618 }
14619 }
14620 else
14621 ptr = (bool *) bfd_zmalloc (bytes);
14622
14623 if (ptr == NULL)
14624 return false;
14625
14626 /* And arrange for that done flag to be at index -1. */
14627 h->u2.vtable->used = ptr + 1;
14628 h->u2.vtable->size = size;
14629 }
14630
14631 h->u2.vtable->used[addend >> log_file_align] = true;
14632
14633 return true;
14634 }
14635
14636 /* Map an ELF section header flag to its corresponding string. */
14637 typedef struct
14638 {
14639 char *flag_name;
14640 flagword flag_value;
14641 } elf_flags_to_name_table;
14642
14643 static const elf_flags_to_name_table elf_flags_to_names [] =
14644 {
14645 { "SHF_WRITE", SHF_WRITE },
14646 { "SHF_ALLOC", SHF_ALLOC },
14647 { "SHF_EXECINSTR", SHF_EXECINSTR },
14648 { "SHF_MERGE", SHF_MERGE },
14649 { "SHF_STRINGS", SHF_STRINGS },
14650 { "SHF_INFO_LINK", SHF_INFO_LINK},
14651 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14652 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14653 { "SHF_GROUP", SHF_GROUP },
14654 { "SHF_TLS", SHF_TLS },
14655 { "SHF_MASKOS", SHF_MASKOS },
14656 { "SHF_EXCLUDE", SHF_EXCLUDE },
14657 };
14658
14659 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14660 bool
bfd_elf_lookup_section_flags(struct bfd_link_info * info,struct flag_info * flaginfo,asection * section)14661 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
14662 struct flag_info *flaginfo,
14663 asection *section)
14664 {
14665 const bfd_vma sh_flags = elf_section_flags (section);
14666
14667 if (!flaginfo->flags_initialized)
14668 {
14669 bfd *obfd = info->output_bfd;
14670 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14671 struct flag_info_list *tf = flaginfo->flag_list;
14672 int with_hex = 0;
14673 int without_hex = 0;
14674
14675 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
14676 {
14677 unsigned i;
14678 flagword (*lookup) (char *);
14679
14680 lookup = bed->elf_backend_lookup_section_flags_hook;
14681 if (lookup != NULL)
14682 {
14683 flagword hexval = (*lookup) ((char *) tf->name);
14684
14685 if (hexval != 0)
14686 {
14687 if (tf->with == with_flags)
14688 with_hex |= hexval;
14689 else if (tf->with == without_flags)
14690 without_hex |= hexval;
14691 tf->valid = true;
14692 continue;
14693 }
14694 }
14695 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
14696 {
14697 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
14698 {
14699 if (tf->with == with_flags)
14700 with_hex |= elf_flags_to_names[i].flag_value;
14701 else if (tf->with == without_flags)
14702 without_hex |= elf_flags_to_names[i].flag_value;
14703 tf->valid = true;
14704 break;
14705 }
14706 }
14707 if (!tf->valid)
14708 {
14709 info->callbacks->einfo
14710 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
14711 return false;
14712 }
14713 }
14714 flaginfo->flags_initialized = true;
14715 flaginfo->only_with_flags |= with_hex;
14716 flaginfo->not_with_flags |= without_hex;
14717 }
14718
14719 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
14720 return false;
14721
14722 if ((flaginfo->not_with_flags & sh_flags) != 0)
14723 return false;
14724
14725 return true;
14726 }
14727
14728 struct alloc_got_off_arg {
14729 bfd_vma gotoff;
14730 struct bfd_link_info *info;
14731 };
14732
14733 /* We need a special top-level link routine to convert got reference counts
14734 to real got offsets. */
14735
14736 static bool
elf_gc_allocate_got_offsets(struct elf_link_hash_entry * h,void * arg)14737 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14738 {
14739 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
14740 bfd *obfd = gofarg->info->output_bfd;
14741 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14742
14743 if (h->got.refcount > 0)
14744 {
14745 h->got.offset = gofarg->gotoff;
14746 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
14747 }
14748 else
14749 h->got.offset = (bfd_vma) -1;
14750
14751 return true;
14752 }
14753
14754 /* And an accompanying bit to work out final got entry offsets once
14755 we're done. Should be called from final_link. */
14756
14757 bool
bfd_elf_gc_common_finalize_got_offsets(bfd * abfd,struct bfd_link_info * info)14758 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14759 struct bfd_link_info *info)
14760 {
14761 bfd *i;
14762 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14763 bfd_vma gotoff;
14764 struct alloc_got_off_arg gofarg;
14765
14766 BFD_ASSERT (abfd == info->output_bfd);
14767
14768 if (! is_elf_hash_table (info->hash))
14769 return false;
14770
14771 /* The GOT offset is relative to the .got section, but the GOT header is
14772 put into the .got.plt section, if the backend uses it. */
14773 if (bed->want_got_plt)
14774 gotoff = 0;
14775 else
14776 gotoff = bed->got_header_size;
14777
14778 /* Do the local .got entries first. */
14779 for (i = info->input_bfds; i; i = i->link.next)
14780 {
14781 bfd_signed_vma *local_got;
14782 size_t j, locsymcount;
14783 Elf_Internal_Shdr *symtab_hdr;
14784
14785 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14786 continue;
14787
14788 local_got = elf_local_got_refcounts (i);
14789 if (!local_got)
14790 continue;
14791
14792 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14793 if (elf_bad_symtab (i))
14794 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14795 else
14796 locsymcount = symtab_hdr->sh_info;
14797
14798 for (j = 0; j < locsymcount; ++j)
14799 {
14800 if (local_got[j] > 0)
14801 {
14802 local_got[j] = gotoff;
14803 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
14804 }
14805 else
14806 local_got[j] = (bfd_vma) -1;
14807 }
14808 }
14809
14810 /* Then the global .got entries. .plt refcounts are handled by
14811 adjust_dynamic_symbol */
14812 gofarg.gotoff = gotoff;
14813 gofarg.info = info;
14814 elf_link_hash_traverse (elf_hash_table (info),
14815 elf_gc_allocate_got_offsets,
14816 &gofarg);
14817 return true;
14818 }
14819
14820 /* Many folk need no more in the way of final link than this, once
14821 got entry reference counting is enabled. */
14822
14823 bool
bfd_elf_gc_common_final_link(bfd * abfd,struct bfd_link_info * info)14824 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14825 {
14826 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14827 return false;
14828
14829 /* Invoke the regular ELF backend linker to do all the work. */
14830 return bfd_elf_final_link (abfd, info);
14831 }
14832
14833 bool
bfd_elf_reloc_symbol_deleted_p(bfd_vma offset,void * cookie)14834 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14835 {
14836 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
14837
14838 if (rcookie->bad_symtab)
14839 rcookie->rel = rcookie->rels;
14840
14841 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14842 {
14843 unsigned long r_symndx;
14844
14845 if (! rcookie->bad_symtab)
14846 if (rcookie->rel->r_offset > offset)
14847 return false;
14848 if (rcookie->rel->r_offset != offset)
14849 continue;
14850
14851 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
14852 if (r_symndx == STN_UNDEF)
14853 return true;
14854
14855 if (r_symndx >= rcookie->locsymcount
14856 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14857 {
14858 struct elf_link_hash_entry *h;
14859
14860 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14861
14862 while (h->root.type == bfd_link_hash_indirect
14863 || h->root.type == bfd_link_hash_warning)
14864 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14865
14866 if ((h->root.type == bfd_link_hash_defined
14867 || h->root.type == bfd_link_hash_defweak)
14868 && (h->root.u.def.section->owner != rcookie->abfd
14869 || h->root.u.def.section->kept_section != NULL
14870 || discarded_section (h->root.u.def.section)))
14871 return true;
14872 }
14873 else
14874 {
14875 /* It's not a relocation against a global symbol,
14876 but it could be a relocation against a local
14877 symbol for a discarded section. */
14878 asection *isec;
14879 Elf_Internal_Sym *isym;
14880
14881 /* Need to: get the symbol; get the section. */
14882 isym = &rcookie->locsyms[r_symndx];
14883 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
14884 if (isec != NULL
14885 && (isec->kept_section != NULL
14886 || discarded_section (isec)))
14887 return true;
14888 }
14889 return false;
14890 }
14891 return false;
14892 }
14893
14894 /* Discard unneeded references to discarded sections.
14895 Returns -1 on error, 1 if any section's size was changed, 0 if
14896 nothing changed. This function assumes that the relocations are in
14897 sorted order, which is true for all known assemblers. */
14898
14899 int
bfd_elf_discard_info(bfd * output_bfd,struct bfd_link_info * info)14900 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14901 {
14902 struct elf_reloc_cookie cookie;
14903 asection *o;
14904 bfd *abfd;
14905 int changed = 0;
14906
14907 if (info->traditional_format
14908 || !is_elf_hash_table (info->hash))
14909 return 0;
14910
14911 o = bfd_get_section_by_name (output_bfd, ".stab");
14912 if (o != NULL)
14913 {
14914 asection *i;
14915
14916 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14917 {
14918 if (i->size == 0
14919 || i->reloc_count == 0
14920 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14921 continue;
14922
14923 abfd = i->owner;
14924 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14925 continue;
14926
14927 if (!init_reloc_cookie_for_section (&cookie, info, i))
14928 return -1;
14929
14930 if (_bfd_discard_section_stabs (abfd, i,
14931 elf_section_data (i)->sec_info,
14932 bfd_elf_reloc_symbol_deleted_p,
14933 &cookie))
14934 changed = 1;
14935
14936 fini_reloc_cookie_for_section (&cookie, i);
14937 }
14938 }
14939
14940 o = NULL;
14941 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14942 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
14943 if (o != NULL)
14944 {
14945 asection *i;
14946 int eh_changed = 0;
14947 unsigned int eh_alignment; /* Octets. */
14948
14949 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14950 {
14951 if (i->size == 0)
14952 continue;
14953
14954 abfd = i->owner;
14955 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14956 continue;
14957
14958 if (!init_reloc_cookie_for_section (&cookie, info, i))
14959 return -1;
14960
14961 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14962 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
14963 bfd_elf_reloc_symbol_deleted_p,
14964 &cookie))
14965 {
14966 eh_changed = 1;
14967 if (i->size != i->rawsize)
14968 changed = 1;
14969 }
14970
14971 fini_reloc_cookie_for_section (&cookie, i);
14972 }
14973
14974 eh_alignment = ((1 << o->alignment_power)
14975 * bfd_octets_per_byte (output_bfd, o));
14976 /* Skip over zero terminator, and prevent empty sections from
14977 adding alignment padding at the end. */
14978 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14979 if (i->size == 0)
14980 i->flags |= SEC_EXCLUDE;
14981 else if (i->size > 4)
14982 break;
14983 /* The last non-empty eh_frame section doesn't need padding. */
14984 if (i != NULL)
14985 i = i->map_tail.s;
14986 /* Any prior sections must pad the last FDE out to the output
14987 section alignment. Otherwise we might have zero padding
14988 between sections, which would be seen as a terminator. */
14989 for (; i != NULL; i = i->map_tail.s)
14990 if (i->size == 4)
14991 /* All but the last zero terminator should have been removed. */
14992 BFD_FAIL ();
14993 else
14994 {
14995 bfd_size_type size
14996 = (i->size + eh_alignment - 1) & -eh_alignment;
14997 if (i->size != size)
14998 {
14999 i->size = size;
15000 changed = 1;
15001 eh_changed = 1;
15002 }
15003 }
15004 if (eh_changed)
15005 elf_link_hash_traverse (elf_hash_table (info),
15006 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
15007 }
15008
15009 o = bfd_get_section_by_name (output_bfd, ".sframe");
15010 if (o != NULL)
15011 {
15012 asection *i;
15013
15014 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
15015 {
15016 if (i->size == 0)
15017 continue;
15018
15019 abfd = i->owner;
15020 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15021 continue;
15022
15023 if (!init_reloc_cookie_for_section (&cookie, info, i))
15024 return -1;
15025
15026 if (_bfd_elf_parse_sframe (abfd, info, i, &cookie))
15027 {
15028 if (_bfd_elf_discard_section_sframe (i,
15029 bfd_elf_reloc_symbol_deleted_p,
15030 &cookie))
15031 {
15032 if (i->size != i->rawsize)
15033 changed = 1;
15034 }
15035 }
15036 fini_reloc_cookie_for_section (&cookie, i);
15037 }
15038 /* Update the reference to the output .sframe section. Used to
15039 determine later if PT_GNU_SFRAME segment is to be generated. */
15040 if (!_bfd_elf_set_section_sframe (output_bfd, info))
15041 return -1;
15042 }
15043
15044 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
15045 {
15046 const struct elf_backend_data *bed;
15047 asection *s;
15048
15049 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15050 continue;
15051 s = abfd->sections;
15052 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
15053 continue;
15054
15055 bed = get_elf_backend_data (abfd);
15056
15057 if (bed->elf_backend_discard_info != NULL)
15058 {
15059 if (!init_reloc_cookie (&cookie, info, abfd))
15060 return -1;
15061
15062 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
15063 changed = 1;
15064
15065 fini_reloc_cookie (&cookie, abfd);
15066 }
15067 }
15068
15069 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
15070 _bfd_elf_end_eh_frame_parsing (info);
15071
15072 if (info->eh_frame_hdr_type
15073 && !bfd_link_relocatable (info)
15074 && _bfd_elf_discard_section_eh_frame_hdr (info))
15075 changed = 1;
15076
15077 return changed;
15078 }
15079
15080 bool
_bfd_elf_section_already_linked(bfd * abfd,asection * sec,struct bfd_link_info * info)15081 _bfd_elf_section_already_linked (bfd *abfd,
15082 asection *sec,
15083 struct bfd_link_info *info)
15084 {
15085 flagword flags;
15086 const char *name, *key;
15087 struct bfd_section_already_linked *l;
15088 struct bfd_section_already_linked_hash_entry *already_linked_list;
15089
15090 if (sec->output_section == bfd_abs_section_ptr)
15091 return false;
15092
15093 flags = sec->flags;
15094
15095 /* Return if it isn't a linkonce section. A comdat group section
15096 also has SEC_LINK_ONCE set. */
15097 if ((flags & SEC_LINK_ONCE) == 0)
15098 return false;
15099
15100 /* Don't put group member sections on our list of already linked
15101 sections. They are handled as a group via their group section. */
15102 if (elf_sec_group (sec) != NULL)
15103 return false;
15104
15105 /* For a SHT_GROUP section, use the group signature as the key. */
15106 name = sec->name;
15107 if ((flags & SEC_GROUP) != 0
15108 && elf_next_in_group (sec) != NULL
15109 && elf_group_name (elf_next_in_group (sec)) != NULL)
15110 key = elf_group_name (elf_next_in_group (sec));
15111 else
15112 {
15113 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
15114 if (startswith (name, ".gnu.linkonce.")
15115 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
15116 key++;
15117 else
15118 /* Must be a user linkonce section that doesn't follow gcc's
15119 naming convention. In this case we won't be matching
15120 single member groups. */
15121 key = name;
15122 }
15123
15124 already_linked_list = bfd_section_already_linked_table_lookup (key);
15125
15126 for (l = already_linked_list->entry; l != NULL; l = l->next)
15127 {
15128 /* We may have 2 different types of sections on the list: group
15129 sections with a signature of <key> (<key> is some string),
15130 and linkonce sections named .gnu.linkonce.<type>.<key>.
15131 Match like sections. LTO plugin sections are an exception.
15132 They are always named .gnu.linkonce.t.<key> and match either
15133 type of section. */
15134 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
15135 && ((flags & SEC_GROUP) != 0
15136 || strcmp (name, l->sec->name) == 0))
15137 || (l->sec->owner->flags & BFD_PLUGIN) != 0
15138 || (sec->owner->flags & BFD_PLUGIN) != 0)
15139 {
15140 /* The section has already been linked. See if we should
15141 issue a warning. */
15142 if (!_bfd_handle_already_linked (sec, l, info))
15143 return false;
15144
15145 if (flags & SEC_GROUP)
15146 {
15147 asection *first = elf_next_in_group (sec);
15148 asection *s = first;
15149
15150 while (s != NULL)
15151 {
15152 s->output_section = bfd_abs_section_ptr;
15153 /* Record which group discards it. */
15154 s->kept_section = l->sec;
15155 s = elf_next_in_group (s);
15156 /* These lists are circular. */
15157 if (s == first)
15158 break;
15159 }
15160 }
15161
15162 return true;
15163 }
15164 }
15165
15166 /* A single member comdat group section may be discarded by a
15167 linkonce section and vice versa. */
15168 if ((flags & SEC_GROUP) != 0)
15169 {
15170 asection *first = elf_next_in_group (sec);
15171
15172 if (first != NULL && elf_next_in_group (first) == first)
15173 /* Check this single member group against linkonce sections. */
15174 for (l = already_linked_list->entry; l != NULL; l = l->next)
15175 if ((l->sec->flags & SEC_GROUP) == 0
15176 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
15177 {
15178 first->output_section = bfd_abs_section_ptr;
15179 first->kept_section = l->sec;
15180 sec->output_section = bfd_abs_section_ptr;
15181 break;
15182 }
15183 }
15184 else
15185 /* Check this linkonce section against single member groups. */
15186 for (l = already_linked_list->entry; l != NULL; l = l->next)
15187 if (l->sec->flags & SEC_GROUP)
15188 {
15189 asection *first = elf_next_in_group (l->sec);
15190
15191 if (first != NULL
15192 && elf_next_in_group (first) == first
15193 && bfd_elf_match_symbols_in_sections (first, sec, info))
15194 {
15195 sec->output_section = bfd_abs_section_ptr;
15196 sec->kept_section = first;
15197 break;
15198 }
15199 }
15200
15201 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
15202 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
15203 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
15204 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
15205 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
15206 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
15207 `.gnu.linkonce.t.F' section from a different bfd not requiring any
15208 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
15209 The reverse order cannot happen as there is never a bfd with only the
15210 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
15211 matter as here were are looking only for cross-bfd sections. */
15212
15213 if ((flags & SEC_GROUP) == 0 && startswith (name, ".gnu.linkonce.r."))
15214 for (l = already_linked_list->entry; l != NULL; l = l->next)
15215 if ((l->sec->flags & SEC_GROUP) == 0
15216 && startswith (l->sec->name, ".gnu.linkonce.t."))
15217 {
15218 if (abfd != l->sec->owner)
15219 sec->output_section = bfd_abs_section_ptr;
15220 break;
15221 }
15222
15223 /* This is the first section with this name. Record it. */
15224 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
15225 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
15226 return sec->output_section == bfd_abs_section_ptr;
15227 }
15228
15229 bool
_bfd_elf_common_definition(Elf_Internal_Sym * sym)15230 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
15231 {
15232 return sym->st_shndx == SHN_COMMON;
15233 }
15234
15235 unsigned int
_bfd_elf_common_section_index(asection * sec ATTRIBUTE_UNUSED)15236 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
15237 {
15238 return SHN_COMMON;
15239 }
15240
15241 asection *
_bfd_elf_common_section(asection * sec ATTRIBUTE_UNUSED)15242 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
15243 {
15244 return bfd_com_section_ptr;
15245 }
15246
15247 bfd_vma
_bfd_elf_default_got_elt_size(bfd * abfd,struct bfd_link_info * info ATTRIBUTE_UNUSED,struct elf_link_hash_entry * h ATTRIBUTE_UNUSED,bfd * ibfd ATTRIBUTE_UNUSED,unsigned long symndx ATTRIBUTE_UNUSED)15248 _bfd_elf_default_got_elt_size (bfd *abfd,
15249 struct bfd_link_info *info ATTRIBUTE_UNUSED,
15250 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
15251 bfd *ibfd ATTRIBUTE_UNUSED,
15252 unsigned long symndx ATTRIBUTE_UNUSED)
15253 {
15254 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15255 return bed->s->arch_size / 8;
15256 }
15257
15258 /* Routines to support the creation of dynamic relocs. */
15259
15260 /* Returns the name of the dynamic reloc section associated with SEC. */
15261
15262 static const char *
get_dynamic_reloc_section_name(bfd * abfd,asection * sec,bool is_rela)15263 get_dynamic_reloc_section_name (bfd * abfd,
15264 asection * sec,
15265 bool is_rela)
15266 {
15267 char *name;
15268 const char *old_name = bfd_section_name (sec);
15269 const char *prefix = is_rela ? ".rela" : ".rel";
15270
15271 if (old_name == NULL)
15272 return NULL;
15273
15274 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
15275 sprintf (name, "%s%s", prefix, old_name);
15276
15277 return name;
15278 }
15279
15280 /* Returns the dynamic reloc section associated with SEC.
15281 If necessary compute the name of the dynamic reloc section based
15282 on SEC's name (looked up in ABFD's string table) and the setting
15283 of IS_RELA. */
15284
15285 asection *
_bfd_elf_get_dynamic_reloc_section(bfd * abfd,asection * sec,bool is_rela)15286 _bfd_elf_get_dynamic_reloc_section (bfd *abfd,
15287 asection *sec,
15288 bool is_rela)
15289 {
15290 asection *reloc_sec = elf_section_data (sec)->sreloc;
15291
15292 if (reloc_sec == NULL)
15293 {
15294 const char *name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15295
15296 if (name != NULL)
15297 {
15298 reloc_sec = bfd_get_linker_section (abfd, name);
15299
15300 if (reloc_sec != NULL)
15301 elf_section_data (sec)->sreloc = reloc_sec;
15302 }
15303 }
15304
15305 return reloc_sec;
15306 }
15307
15308 /* Returns the dynamic reloc section associated with SEC. If the
15309 section does not exist it is created and attached to the DYNOBJ
15310 bfd and stored in the SRELOC field of SEC's elf_section_data
15311 structure.
15312
15313 ALIGNMENT is the alignment for the newly created section and
15314 IS_RELA defines whether the name should be .rela.<SEC's name>
15315 or .rel.<SEC's name>. The section name is looked up in the
15316 string table associated with ABFD. */
15317
15318 asection *
_bfd_elf_make_dynamic_reloc_section(asection * sec,bfd * dynobj,unsigned int alignment,bfd * abfd,bool is_rela)15319 _bfd_elf_make_dynamic_reloc_section (asection *sec,
15320 bfd *dynobj,
15321 unsigned int alignment,
15322 bfd *abfd,
15323 bool is_rela)
15324 {
15325 asection * reloc_sec = elf_section_data (sec)->sreloc;
15326
15327 if (reloc_sec == NULL)
15328 {
15329 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15330
15331 if (name == NULL)
15332 return NULL;
15333
15334 reloc_sec = bfd_get_linker_section (dynobj, name);
15335
15336 if (reloc_sec == NULL)
15337 {
15338 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
15339 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
15340 if ((sec->flags & SEC_ALLOC) != 0)
15341 flags |= SEC_ALLOC | SEC_LOAD;
15342
15343 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
15344 if (reloc_sec != NULL)
15345 {
15346 /* _bfd_elf_get_sec_type_attr chooses a section type by
15347 name. Override as it may be wrong, eg. for a user
15348 section named "auto" we'll get ".relauto" which is
15349 seen to be a .rela section. */
15350 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
15351 if (!bfd_set_section_alignment (reloc_sec, alignment))
15352 reloc_sec = NULL;
15353 }
15354 }
15355
15356 elf_section_data (sec)->sreloc = reloc_sec;
15357 }
15358
15359 return reloc_sec;
15360 }
15361
15362 /* Copy the ELF symbol type and other attributes for a linker script
15363 assignment from HSRC to HDEST. Generally this should be treated as
15364 if we found a strong non-dynamic definition for HDEST (except that
15365 ld ignores multiple definition errors). */
15366 void
_bfd_elf_copy_link_hash_symbol_type(bfd * abfd,struct bfd_link_hash_entry * hdest,struct bfd_link_hash_entry * hsrc)15367 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
15368 struct bfd_link_hash_entry *hdest,
15369 struct bfd_link_hash_entry *hsrc)
15370 {
15371 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
15372 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
15373 Elf_Internal_Sym isym;
15374
15375 ehdest->type = ehsrc->type;
15376 ehdest->target_internal = ehsrc->target_internal;
15377
15378 isym.st_other = ehsrc->other;
15379 elf_merge_st_other (abfd, ehdest, isym.st_other, NULL, true, false);
15380 }
15381
15382 /* Append a RELA relocation REL to section S in BFD. */
15383
15384 void
elf_append_rela(bfd * abfd,asection * s,Elf_Internal_Rela * rel)15385 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15386 {
15387 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15388 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
15389 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
15390 bed->s->swap_reloca_out (abfd, rel, loc);
15391 }
15392
15393 /* Append a REL relocation REL to section S in BFD. */
15394
15395 void
elf_append_rel(bfd * abfd,asection * s,Elf_Internal_Rela * rel)15396 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15397 {
15398 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15399 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
15400 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
15401 bed->s->swap_reloc_out (abfd, rel, loc);
15402 }
15403
15404 /* Define __start, __stop, .startof. or .sizeof. symbol. */
15405
15406 struct bfd_link_hash_entry *
bfd_elf_define_start_stop(struct bfd_link_info * info,const char * symbol,asection * sec)15407 bfd_elf_define_start_stop (struct bfd_link_info *info,
15408 const char *symbol, asection *sec)
15409 {
15410 struct elf_link_hash_entry *h;
15411
15412 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
15413 false, false, true);
15414 /* NB: Common symbols will be turned into definition later. */
15415 if (h != NULL
15416 && !h->root.ldscript_def
15417 && (h->root.type == bfd_link_hash_undefined
15418 || h->root.type == bfd_link_hash_undefweak
15419 || ((h->ref_regular || h->def_dynamic)
15420 && !h->def_regular
15421 && h->root.type != bfd_link_hash_common)))
15422 {
15423 bool was_dynamic = h->ref_dynamic || h->def_dynamic;
15424 h->verinfo.verdef = NULL;
15425 h->root.type = bfd_link_hash_defined;
15426 h->root.u.def.section = sec;
15427 h->root.u.def.value = 0;
15428 h->def_regular = 1;
15429 h->def_dynamic = 0;
15430 h->start_stop = 1;
15431 h->u2.start_stop_section = sec;
15432 if (symbol[0] == '.')
15433 {
15434 /* .startof. and .sizeof. symbols are local. */
15435 const struct elf_backend_data *bed;
15436 bed = get_elf_backend_data (info->output_bfd);
15437 (*bed->elf_backend_hide_symbol) (info, h, true);
15438 }
15439 else
15440 {
15441 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
15442 h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
15443 | info->start_stop_visibility);
15444 if (was_dynamic)
15445 bfd_elf_link_record_dynamic_symbol (info, h);
15446 }
15447 return &h->root;
15448 }
15449 return NULL;
15450 }
15451
15452 /* Find dynamic relocs for H that apply to read-only sections. */
15453
15454 asection *
_bfd_elf_readonly_dynrelocs(struct elf_link_hash_entry * h)15455 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
15456 {
15457 struct elf_dyn_relocs *p;
15458
15459 for (p = h->dyn_relocs; p != NULL; p = p->next)
15460 {
15461 asection *s = p->sec->output_section;
15462
15463 if (s != NULL && (s->flags & SEC_READONLY) != 0)
15464 return p->sec;
15465 }
15466 return NULL;
15467 }
15468
15469 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
15470 read-only sections. */
15471
15472 bool
_bfd_elf_maybe_set_textrel(struct elf_link_hash_entry * h,void * inf)15473 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
15474 {
15475 asection *sec;
15476
15477 if (h->root.type == bfd_link_hash_indirect)
15478 return true;
15479
15480 sec = _bfd_elf_readonly_dynrelocs (h);
15481 if (sec != NULL)
15482 {
15483 struct bfd_link_info *info = (struct bfd_link_info *) inf;
15484
15485 info->flags |= DF_TEXTREL;
15486 /* xgettext:c-format */
15487 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
15488 "in read-only section `%pA'\n"),
15489 sec->owner, h->root.root.string, sec);
15490
15491 if (bfd_link_textrel_check (info))
15492 /* xgettext:c-format */
15493 info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
15494 "in read-only section `%pA'\n"),
15495 sec->owner, h->root.root.string, sec);
15496
15497 /* Not an error, just cut short the traversal. */
15498 return false;
15499 }
15500 return true;
15501 }
15502
15503 /* Add dynamic tags. */
15504
15505 bool
_bfd_elf_add_dynamic_tags(bfd * output_bfd,struct bfd_link_info * info,bool need_dynamic_reloc)15506 _bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
15507 bool need_dynamic_reloc)
15508 {
15509 struct elf_link_hash_table *htab = elf_hash_table (info);
15510
15511 if (htab->dynamic_sections_created)
15512 {
15513 /* Add some entries to the .dynamic section. We fill in the
15514 values later, in finish_dynamic_sections, but we must add
15515 the entries now so that we get the correct size for the
15516 .dynamic section. The DT_DEBUG entry is filled in by the
15517 dynamic linker and used by the debugger. */
15518 #define add_dynamic_entry(TAG, VAL) \
15519 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15520
15521 const struct elf_backend_data *bed
15522 = get_elf_backend_data (output_bfd);
15523
15524 if (bfd_link_executable (info))
15525 {
15526 if (!add_dynamic_entry (DT_DEBUG, 0))
15527 return false;
15528 }
15529
15530 if (htab->dt_pltgot_required || htab->splt->size != 0)
15531 {
15532 /* DT_PLTGOT is used by prelink even if there is no PLT
15533 relocation. */
15534 if (!add_dynamic_entry (DT_PLTGOT, 0))
15535 return false;
15536 }
15537
15538 if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15539 {
15540 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15541 || !add_dynamic_entry (DT_PLTREL,
15542 (bed->rela_plts_and_copies_p
15543 ? DT_RELA : DT_REL))
15544 || !add_dynamic_entry (DT_JMPREL, 0))
15545 return false;
15546 }
15547
15548 if (htab->tlsdesc_plt
15549 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15550 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
15551 return false;
15552
15553 if (need_dynamic_reloc)
15554 {
15555 if (bed->rela_plts_and_copies_p)
15556 {
15557 if (!add_dynamic_entry (DT_RELA, 0)
15558 || !add_dynamic_entry (DT_RELASZ, 0)
15559 || !add_dynamic_entry (DT_RELAENT,
15560 bed->s->sizeof_rela))
15561 return false;
15562 }
15563 else
15564 {
15565 if (!add_dynamic_entry (DT_REL, 0)
15566 || !add_dynamic_entry (DT_RELSZ, 0)
15567 || !add_dynamic_entry (DT_RELENT,
15568 bed->s->sizeof_rel))
15569 return false;
15570 }
15571
15572 /* If any dynamic relocs apply to a read-only section,
15573 then we need a DT_TEXTREL entry. */
15574 if ((info->flags & DF_TEXTREL) == 0)
15575 elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15576 info);
15577
15578 if ((info->flags & DF_TEXTREL) != 0)
15579 {
15580 if (htab->ifunc_resolvers)
15581 info->callbacks->einfo
15582 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15583 "may result in a segfault at runtime; recompile with %s\n"),
15584 bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15585
15586 if (!add_dynamic_entry (DT_TEXTREL, 0))
15587 return false;
15588 }
15589 }
15590 }
15591 #undef add_dynamic_entry
15592
15593 return true;
15594 }
15595