1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2022 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 /* This structure is used to pass information to
50 _bfd_elf_link_find_version_dependencies. */
51
52 struct elf_find_verdep_info
53 {
54 /* General link information. */
55 struct bfd_link_info *info;
56 /* The number of dependencies. */
57 unsigned int vers;
58 /* Whether we had a failure. */
59 bool failed;
60 };
61
62 static bool _bfd_elf_fix_symbol_flags
63 (struct elf_link_hash_entry *, struct elf_info_failed *);
64
65 asection *
_bfd_elf_section_for_symbol(struct elf_reloc_cookie * cookie,unsigned long r_symndx,bool discard)66 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
67 unsigned long r_symndx,
68 bool discard)
69 {
70 if (r_symndx >= cookie->locsymcount
71 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
72 {
73 struct elf_link_hash_entry *h;
74
75 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
76
77 while (h->root.type == bfd_link_hash_indirect
78 || h->root.type == bfd_link_hash_warning)
79 h = (struct elf_link_hash_entry *) h->root.u.i.link;
80
81 if ((h->root.type == bfd_link_hash_defined
82 || h->root.type == bfd_link_hash_defweak)
83 && discarded_section (h->root.u.def.section))
84 return h->root.u.def.section;
85 else
86 return NULL;
87 }
88 else
89 {
90 /* It's not a relocation against a global symbol,
91 but it could be a relocation against a local
92 symbol for a discarded section. */
93 asection *isec;
94 Elf_Internal_Sym *isym;
95
96 /* Need to: get the symbol; get the section. */
97 isym = &cookie->locsyms[r_symndx];
98 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
99 if (isec != NULL
100 && discard ? discarded_section (isec) : 1)
101 return isec;
102 }
103 return NULL;
104 }
105
106 /* Define a symbol in a dynamic linkage section. */
107
108 struct elf_link_hash_entry *
_bfd_elf_define_linkage_sym(bfd * abfd,struct bfd_link_info * info,asection * sec,const char * name)109 _bfd_elf_define_linkage_sym (bfd *abfd,
110 struct bfd_link_info *info,
111 asection *sec,
112 const char *name)
113 {
114 struct elf_link_hash_entry *h;
115 struct bfd_link_hash_entry *bh;
116 const struct elf_backend_data *bed;
117
118 h = elf_link_hash_lookup (elf_hash_table (info), name, false, false, false);
119 if (h != NULL)
120 {
121 /* Zap symbol defined in an as-needed lib that wasn't linked.
122 This is a symptom of a larger problem: Absolute symbols
123 defined in shared libraries can't be overridden, because we
124 lose the link to the bfd which is via the symbol section. */
125 h->root.type = bfd_link_hash_new;
126 bh = &h->root;
127 }
128 else
129 bh = NULL;
130
131 bed = get_elf_backend_data (abfd);
132 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
133 sec, 0, NULL, false, bed->collect,
134 &bh))
135 return NULL;
136 h = (struct elf_link_hash_entry *) bh;
137 BFD_ASSERT (h != NULL);
138 h->def_regular = 1;
139 h->non_elf = 0;
140 h->root.linker_def = 1;
141 h->type = STT_OBJECT;
142 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
143 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
144
145 (*bed->elf_backend_hide_symbol) (info, h, true);
146 return h;
147 }
148
149 bool
_bfd_elf_create_got_section(bfd * abfd,struct bfd_link_info * info)150 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
151 {
152 flagword flags;
153 asection *s;
154 struct elf_link_hash_entry *h;
155 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
156 struct elf_link_hash_table *htab = elf_hash_table (info);
157
158 /* This function may be called more than once. */
159 if (htab->sgot != NULL)
160 return true;
161
162 flags = bed->dynamic_sec_flags;
163
164 s = bfd_make_section_anyway_with_flags (abfd,
165 (bed->rela_plts_and_copies_p
166 ? ".rela.got" : ".rel.got"),
167 (bed->dynamic_sec_flags
168 | SEC_READONLY));
169 if (s == NULL
170 || !bfd_set_section_alignment (s, bed->s->log_file_align))
171 return false;
172 htab->srelgot = s;
173
174 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
175 if (s == NULL
176 || !bfd_set_section_alignment (s, bed->s->log_file_align))
177 return false;
178 htab->sgot = s;
179
180 if (bed->want_got_plt)
181 {
182 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
183 if (s == NULL
184 || !bfd_set_section_alignment (s, bed->s->log_file_align))
185 return false;
186 htab->sgotplt = s;
187 }
188
189 /* The first bit of the global offset table is the header. */
190 s->size += bed->got_header_size;
191
192 if (bed->want_got_sym)
193 {
194 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
195 (or .got.plt) section. We don't do this in the linker script
196 because we don't want to define the symbol if we are not creating
197 a global offset table. */
198 h = _bfd_elf_define_linkage_sym (abfd, info, s,
199 "_GLOBAL_OFFSET_TABLE_");
200 elf_hash_table (info)->hgot = h;
201 if (h == NULL)
202 return false;
203 }
204
205 return true;
206 }
207
208 /* Create a strtab to hold the dynamic symbol names. */
209 static bool
_bfd_elf_link_create_dynstrtab(bfd * abfd,struct bfd_link_info * info)210 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
211 {
212 struct elf_link_hash_table *hash_table;
213
214 hash_table = elf_hash_table (info);
215 if (hash_table->dynobj == NULL)
216 {
217 /* We may not set dynobj, an input file holding linker created
218 dynamic sections to abfd, which may be a dynamic object with
219 its own dynamic sections. We need to find a normal input file
220 to hold linker created sections if possible. */
221 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
222 {
223 bfd *ibfd;
224 asection *s;
225 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
226 if ((ibfd->flags
227 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
228 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
229 && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
230 && !((s = ibfd->sections) != NULL
231 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
232 {
233 abfd = ibfd;
234 break;
235 }
236 }
237 hash_table->dynobj = abfd;
238 }
239
240 if (hash_table->dynstr == NULL)
241 {
242 hash_table->dynstr = _bfd_elf_strtab_init ();
243 if (hash_table->dynstr == NULL)
244 return false;
245 }
246 return true;
247 }
248
249 /* Create some sections which will be filled in with dynamic linking
250 information. ABFD is an input file which requires dynamic sections
251 to be created. The dynamic sections take up virtual memory space
252 when the final executable is run, so we need to create them before
253 addresses are assigned to the output sections. We work out the
254 actual contents and size of these sections later. */
255
256 bool
_bfd_elf_link_create_dynamic_sections(bfd * abfd,struct bfd_link_info * info)257 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
258 {
259 flagword flags;
260 asection *s;
261 const struct elf_backend_data *bed;
262 struct elf_link_hash_entry *h;
263
264 if (! is_elf_hash_table (info->hash))
265 return false;
266
267 if (elf_hash_table (info)->dynamic_sections_created)
268 return true;
269
270 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
271 return false;
272
273 abfd = elf_hash_table (info)->dynobj;
274 bed = get_elf_backend_data (abfd);
275
276 flags = bed->dynamic_sec_flags;
277
278 /* A dynamically linked executable has a .interp section, but a
279 shared library does not. */
280 if (bfd_link_executable (info) && !info->nointerp)
281 {
282 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
283 flags | SEC_READONLY);
284 if (s == NULL)
285 return false;
286 }
287
288 /* Create sections to hold version informations. These are removed
289 if they are not needed. */
290 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
291 flags | SEC_READONLY);
292 if (s == NULL
293 || !bfd_set_section_alignment (s, bed->s->log_file_align))
294 return false;
295
296 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
297 flags | SEC_READONLY);
298 if (s == NULL
299 || !bfd_set_section_alignment (s, 1))
300 return false;
301
302 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
303 flags | SEC_READONLY);
304 if (s == NULL
305 || !bfd_set_section_alignment (s, bed->s->log_file_align))
306 return false;
307
308 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
309 flags | SEC_READONLY);
310 if (s == NULL
311 || !bfd_set_section_alignment (s, bed->s->log_file_align))
312 return false;
313 elf_hash_table (info)->dynsym = s;
314
315 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
316 flags | SEC_READONLY);
317 if (s == NULL)
318 return false;
319
320 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
321 if (s == NULL
322 || !bfd_set_section_alignment (s, bed->s->log_file_align))
323 return false;
324
325 /* The special symbol _DYNAMIC is always set to the start of the
326 .dynamic section. We could set _DYNAMIC in a linker script, but we
327 only want to define it if we are, in fact, creating a .dynamic
328 section. We don't want to define it if there is no .dynamic
329 section, since on some ELF platforms the start up code examines it
330 to decide how to initialize the process. */
331 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
332 elf_hash_table (info)->hdynamic = h;
333 if (h == NULL)
334 return false;
335
336 if (info->emit_hash)
337 {
338 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
339 flags | SEC_READONLY);
340 if (s == NULL
341 || !bfd_set_section_alignment (s, bed->s->log_file_align))
342 return false;
343 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
344 }
345
346 if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL)
347 {
348 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
349 flags | SEC_READONLY);
350 if (s == NULL
351 || !bfd_set_section_alignment (s, bed->s->log_file_align))
352 return false;
353 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
354 4 32-bit words followed by variable count of 64-bit words, then
355 variable count of 32-bit words. */
356 if (bed->s->arch_size == 64)
357 elf_section_data (s)->this_hdr.sh_entsize = 0;
358 else
359 elf_section_data (s)->this_hdr.sh_entsize = 4;
360 }
361
362 if (info->enable_dt_relr)
363 {
364 s = bfd_make_section_anyway_with_flags (abfd, ".relr.dyn",
365 (bed->dynamic_sec_flags
366 | SEC_READONLY));
367 if (s == NULL
368 || !bfd_set_section_alignment (s, bed->s->log_file_align))
369 return false;
370 elf_hash_table (info)->srelrdyn = s;
371 }
372
373 /* Let the backend create the rest of the sections. This lets the
374 backend set the right flags. The backend will normally create
375 the .got and .plt sections. */
376 if (bed->elf_backend_create_dynamic_sections == NULL
377 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
378 return false;
379
380 elf_hash_table (info)->dynamic_sections_created = true;
381
382 return true;
383 }
384
385 /* Create dynamic sections when linking against a dynamic object. */
386
387 bool
_bfd_elf_create_dynamic_sections(bfd * abfd,struct bfd_link_info * info)388 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
389 {
390 flagword flags, pltflags;
391 struct elf_link_hash_entry *h;
392 asection *s;
393 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
394 struct elf_link_hash_table *htab = elf_hash_table (info);
395
396 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
397 .rel[a].bss sections. */
398 flags = bed->dynamic_sec_flags;
399
400 pltflags = flags;
401 if (bed->plt_not_loaded)
402 /* We do not clear SEC_ALLOC here because we still want the OS to
403 allocate space for the section; it's just that there's nothing
404 to read in from the object file. */
405 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
406 else
407 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
408 if (bed->plt_readonly)
409 pltflags |= SEC_READONLY;
410
411 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
412 if (s == NULL
413 || !bfd_set_section_alignment (s, bed->plt_alignment))
414 return false;
415 htab->splt = s;
416
417 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
418 .plt section. */
419 if (bed->want_plt_sym)
420 {
421 h = _bfd_elf_define_linkage_sym (abfd, info, s,
422 "_PROCEDURE_LINKAGE_TABLE_");
423 elf_hash_table (info)->hplt = h;
424 if (h == NULL)
425 return false;
426 }
427
428 s = bfd_make_section_anyway_with_flags (abfd,
429 (bed->rela_plts_and_copies_p
430 ? ".rela.plt" : ".rel.plt"),
431 flags | SEC_READONLY);
432 if (s == NULL
433 || !bfd_set_section_alignment (s, bed->s->log_file_align))
434 return false;
435 htab->srelplt = s;
436
437 if (! _bfd_elf_create_got_section (abfd, info))
438 return false;
439
440 if (bed->want_dynbss)
441 {
442 /* The .dynbss section is a place to put symbols which are defined
443 by dynamic objects, are referenced by regular objects, and are
444 not functions. We must allocate space for them in the process
445 image and use a R_*_COPY reloc to tell the dynamic linker to
446 initialize them at run time. The linker script puts the .dynbss
447 section into the .bss section of the final image. */
448 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
449 SEC_ALLOC | SEC_LINKER_CREATED);
450 if (s == NULL)
451 return false;
452 htab->sdynbss = s;
453
454 if (bed->want_dynrelro)
455 {
456 /* Similarly, but for symbols that were originally in read-only
457 sections. This section doesn't really need to have contents,
458 but make it like other .data.rel.ro sections. */
459 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
460 flags);
461 if (s == NULL)
462 return false;
463 htab->sdynrelro = s;
464 }
465
466 /* The .rel[a].bss section holds copy relocs. This section is not
467 normally needed. We need to create it here, though, so that the
468 linker will map it to an output section. We can't just create it
469 only if we need it, because we will not know whether we need it
470 until we have seen all the input files, and the first time the
471 main linker code calls BFD after examining all the input files
472 (size_dynamic_sections) the input sections have already been
473 mapped to the output sections. If the section turns out not to
474 be needed, we can discard it later. We will never need this
475 section when generating a shared object, since they do not use
476 copy relocs. */
477 if (bfd_link_executable (info))
478 {
479 s = bfd_make_section_anyway_with_flags (abfd,
480 (bed->rela_plts_and_copies_p
481 ? ".rela.bss" : ".rel.bss"),
482 flags | SEC_READONLY);
483 if (s == NULL
484 || !bfd_set_section_alignment (s, bed->s->log_file_align))
485 return false;
486 htab->srelbss = s;
487
488 if (bed->want_dynrelro)
489 {
490 s = (bfd_make_section_anyway_with_flags
491 (abfd, (bed->rela_plts_and_copies_p
492 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
493 flags | SEC_READONLY));
494 if (s == NULL
495 || !bfd_set_section_alignment (s, bed->s->log_file_align))
496 return false;
497 htab->sreldynrelro = s;
498 }
499 }
500 }
501
502 return true;
503 }
504
505 /* Record a new dynamic symbol. We record the dynamic symbols as we
506 read the input files, since we need to have a list of all of them
507 before we can determine the final sizes of the output sections.
508 Note that we may actually call this function even though we are not
509 going to output any dynamic symbols; in some cases we know that a
510 symbol should be in the dynamic symbol table, but only if there is
511 one. */
512
513 bool
bfd_elf_link_record_dynamic_symbol(struct bfd_link_info * info,struct elf_link_hash_entry * h)514 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
515 struct elf_link_hash_entry *h)
516 {
517 if (h->dynindx == -1)
518 {
519 struct elf_strtab_hash *dynstr;
520 char *p;
521 const char *name;
522 size_t indx;
523
524 if (h->root.type == bfd_link_hash_defined
525 || h->root.type == bfd_link_hash_defweak)
526 {
527 /* An IR symbol should not be made dynamic. */
528 if (h->root.u.def.section != NULL
529 && h->root.u.def.section->owner != NULL
530 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)
531 return true;
532 }
533
534 /* XXX: The ABI draft says the linker must turn hidden and
535 internal symbols into STB_LOCAL symbols when producing the
536 DSO. However, if ld.so honors st_other in the dynamic table,
537 this would not be necessary. */
538 switch (ELF_ST_VISIBILITY (h->other))
539 {
540 case STV_INTERNAL:
541 case STV_HIDDEN:
542 if (h->root.type != bfd_link_hash_undefined
543 && h->root.type != bfd_link_hash_undefweak)
544 {
545 h->forced_local = 1;
546 if (!elf_hash_table (info)->is_relocatable_executable
547 || ((h->root.type == bfd_link_hash_defined
548 || h->root.type == bfd_link_hash_defweak)
549 && h->root.u.def.section->owner != NULL
550 && h->root.u.def.section->owner->no_export)
551 || (h->root.type == bfd_link_hash_common
552 && h->root.u.c.p->section->owner != NULL
553 && h->root.u.c.p->section->owner->no_export))
554 return true;
555 }
556
557 default:
558 break;
559 }
560
561 h->dynindx = elf_hash_table (info)->dynsymcount;
562 ++elf_hash_table (info)->dynsymcount;
563
564 dynstr = elf_hash_table (info)->dynstr;
565 if (dynstr == NULL)
566 {
567 /* Create a strtab to hold the dynamic symbol names. */
568 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
569 if (dynstr == NULL)
570 return false;
571 }
572
573 /* We don't put any version information in the dynamic string
574 table. */
575 name = h->root.root.string;
576 p = strchr (name, ELF_VER_CHR);
577 if (p != NULL)
578 /* We know that the p points into writable memory. In fact,
579 there are only a few symbols that have read-only names, being
580 those like _GLOBAL_OFFSET_TABLE_ that are created specially
581 by the backends. Most symbols will have names pointing into
582 an ELF string table read from a file, or to objalloc memory. */
583 *p = 0;
584
585 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
586
587 if (p != NULL)
588 *p = ELF_VER_CHR;
589
590 if (indx == (size_t) -1)
591 return false;
592 h->dynstr_index = indx;
593 }
594
595 return true;
596 }
597
598 /* Mark a symbol dynamic. */
599
600 static void
bfd_elf_link_mark_dynamic_symbol(struct bfd_link_info * info,struct elf_link_hash_entry * h,Elf_Internal_Sym * sym)601 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
602 struct elf_link_hash_entry *h,
603 Elf_Internal_Sym *sym)
604 {
605 struct bfd_elf_dynamic_list *d = info->dynamic_list;
606
607 /* It may be called more than once on the same H. */
608 if(h->dynamic || bfd_link_relocatable (info))
609 return;
610
611 if ((info->dynamic_data
612 && (h->type == STT_OBJECT
613 || h->type == STT_COMMON
614 || (sym != NULL
615 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
616 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
617 || (d != NULL
618 && h->non_elf
619 && (*d->match) (&d->head, NULL, h->root.root.string)))
620 {
621 h->dynamic = 1;
622 /* NB: If a symbol is made dynamic by --dynamic-list, it has
623 non-IR reference. */
624 h->root.non_ir_ref_dynamic = 1;
625 }
626 }
627
628 /* Record an assignment to a symbol made by a linker script. We need
629 this in case some dynamic object refers to this symbol. */
630
631 bool
bfd_elf_record_link_assignment(bfd * output_bfd,struct bfd_link_info * info,const char * name,bool provide,bool hidden)632 bfd_elf_record_link_assignment (bfd *output_bfd,
633 struct bfd_link_info *info,
634 const char *name,
635 bool provide,
636 bool hidden)
637 {
638 struct elf_link_hash_entry *h, *hv;
639 struct elf_link_hash_table *htab;
640 const struct elf_backend_data *bed;
641
642 if (!is_elf_hash_table (info->hash))
643 return true;
644
645 htab = elf_hash_table (info);
646 h = elf_link_hash_lookup (htab, name, !provide, true, false);
647 if (h == NULL)
648 return provide;
649
650 if (h->root.type == bfd_link_hash_warning)
651 h = (struct elf_link_hash_entry *) h->root.u.i.link;
652
653 if (h->versioned == unknown)
654 {
655 /* Set versioned if symbol version is unknown. */
656 char *version = strrchr (name, ELF_VER_CHR);
657 if (version)
658 {
659 if (version > name && version[-1] != ELF_VER_CHR)
660 h->versioned = versioned_hidden;
661 else
662 h->versioned = versioned;
663 }
664 }
665
666 /* Symbols defined in a linker script but not referenced anywhere
667 else will have non_elf set. */
668 if (h->non_elf)
669 {
670 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
671 h->non_elf = 0;
672 }
673
674 switch (h->root.type)
675 {
676 case bfd_link_hash_defined:
677 case bfd_link_hash_defweak:
678 case bfd_link_hash_common:
679 break;
680 case bfd_link_hash_undefweak:
681 case bfd_link_hash_undefined:
682 /* Since we're defining the symbol, don't let it seem to have not
683 been defined. record_dynamic_symbol and size_dynamic_sections
684 may depend on this. */
685 h->root.type = bfd_link_hash_new;
686 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
687 bfd_link_repair_undef_list (&htab->root);
688 break;
689 case bfd_link_hash_new:
690 break;
691 case bfd_link_hash_indirect:
692 /* We had a versioned symbol in a dynamic library. We make the
693 the versioned symbol point to this one. */
694 bed = get_elf_backend_data (output_bfd);
695 hv = h;
696 while (hv->root.type == bfd_link_hash_indirect
697 || hv->root.type == bfd_link_hash_warning)
698 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
699 /* We don't need to update h->root.u since linker will set them
700 later. */
701 h->root.type = bfd_link_hash_undefined;
702 hv->root.type = bfd_link_hash_indirect;
703 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
704 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
705 break;
706 default:
707 BFD_FAIL ();
708 return false;
709 }
710
711 /* If this symbol is being provided by the linker script, and it is
712 currently defined by a dynamic object, but not by a regular
713 object, then mark it as undefined so that the generic linker will
714 force the correct value. */
715 if (provide
716 && h->def_dynamic
717 && !h->def_regular)
718 h->root.type = bfd_link_hash_undefined;
719
720 /* If this symbol is currently defined by a dynamic object, but not
721 by a regular object, then clear out any version information because
722 the symbol will not be associated with the dynamic object any
723 more. */
724 if (h->def_dynamic && !h->def_regular)
725 h->verinfo.verdef = NULL;
726
727 /* Make sure this symbol is not garbage collected. */
728 h->mark = 1;
729
730 h->def_regular = 1;
731
732 if (hidden)
733 {
734 bed = get_elf_backend_data (output_bfd);
735 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
736 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
737 (*bed->elf_backend_hide_symbol) (info, h, true);
738 }
739
740 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
741 and executables. */
742 if (!bfd_link_relocatable (info)
743 && h->dynindx != -1
744 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
745 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
746 h->forced_local = 1;
747
748 if ((h->def_dynamic
749 || h->ref_dynamic
750 || bfd_link_dll (info)
751 || elf_hash_table (info)->is_relocatable_executable)
752 && !h->forced_local
753 && h->dynindx == -1)
754 {
755 if (! bfd_elf_link_record_dynamic_symbol (info, h))
756 return false;
757
758 /* If this is a weak defined symbol, and we know a corresponding
759 real symbol from the same dynamic object, make sure the real
760 symbol is also made into a dynamic symbol. */
761 if (h->is_weakalias)
762 {
763 struct elf_link_hash_entry *def = weakdef (h);
764
765 if (def->dynindx == -1
766 && !bfd_elf_link_record_dynamic_symbol (info, def))
767 return false;
768 }
769 }
770
771 return true;
772 }
773
774 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
775 success, and 2 on a failure caused by attempting to record a symbol
776 in a discarded section, eg. a discarded link-once section symbol. */
777
778 int
bfd_elf_link_record_local_dynamic_symbol(struct bfd_link_info * info,bfd * input_bfd,long input_indx)779 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
780 bfd *input_bfd,
781 long input_indx)
782 {
783 size_t amt;
784 struct elf_link_local_dynamic_entry *entry;
785 struct elf_link_hash_table *eht;
786 struct elf_strtab_hash *dynstr;
787 size_t dynstr_index;
788 char *name;
789 Elf_External_Sym_Shndx eshndx;
790 char esym[sizeof (Elf64_External_Sym)];
791
792 if (! is_elf_hash_table (info->hash))
793 return 0;
794
795 /* See if the entry exists already. */
796 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
797 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
798 return 1;
799
800 amt = sizeof (*entry);
801 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
802 if (entry == NULL)
803 return 0;
804
805 /* Go find the symbol, so that we can find it's name. */
806 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
807 1, input_indx, &entry->isym, esym, &eshndx))
808 {
809 bfd_release (input_bfd, entry);
810 return 0;
811 }
812
813 if (entry->isym.st_shndx != SHN_UNDEF
814 && entry->isym.st_shndx < SHN_LORESERVE)
815 {
816 asection *s;
817
818 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
819 if (s == NULL || bfd_is_abs_section (s->output_section))
820 {
821 /* We can still bfd_release here as nothing has done another
822 bfd_alloc. We can't do this later in this function. */
823 bfd_release (input_bfd, entry);
824 return 2;
825 }
826 }
827
828 name = (bfd_elf_string_from_elf_section
829 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
830 entry->isym.st_name));
831
832 dynstr = elf_hash_table (info)->dynstr;
833 if (dynstr == NULL)
834 {
835 /* Create a strtab to hold the dynamic symbol names. */
836 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
837 if (dynstr == NULL)
838 return 0;
839 }
840
841 dynstr_index = _bfd_elf_strtab_add (dynstr, name, false);
842 if (dynstr_index == (size_t) -1)
843 return 0;
844 entry->isym.st_name = dynstr_index;
845
846 eht = elf_hash_table (info);
847
848 entry->next = eht->dynlocal;
849 eht->dynlocal = entry;
850 entry->input_bfd = input_bfd;
851 entry->input_indx = input_indx;
852 eht->dynsymcount++;
853
854 /* Whatever binding the symbol had before, it's now local. */
855 entry->isym.st_info
856 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
857
858 /* The dynindx will be set at the end of size_dynamic_sections. */
859
860 return 1;
861 }
862
863 /* Return the dynindex of a local dynamic symbol. */
864
865 long
_bfd_elf_link_lookup_local_dynindx(struct bfd_link_info * info,bfd * input_bfd,long input_indx)866 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
867 bfd *input_bfd,
868 long input_indx)
869 {
870 struct elf_link_local_dynamic_entry *e;
871
872 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
873 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
874 return e->dynindx;
875 return -1;
876 }
877
878 /* This function is used to renumber the dynamic symbols, if some of
879 them are removed because they are marked as local. This is called
880 via elf_link_hash_traverse. */
881
882 static bool
elf_link_renumber_hash_table_dynsyms(struct elf_link_hash_entry * h,void * data)883 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
884 void *data)
885 {
886 size_t *count = (size_t *) data;
887
888 if (h->forced_local)
889 return true;
890
891 if (h->dynindx != -1)
892 h->dynindx = ++(*count);
893
894 return true;
895 }
896
897
898 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
899 STB_LOCAL binding. */
900
901 static bool
elf_link_renumber_local_hash_table_dynsyms(struct elf_link_hash_entry * h,void * data)902 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
903 void *data)
904 {
905 size_t *count = (size_t *) data;
906
907 if (!h->forced_local)
908 return true;
909
910 if (h->dynindx != -1)
911 h->dynindx = ++(*count);
912
913 return true;
914 }
915
916 /* Return true if the dynamic symbol for a given section should be
917 omitted when creating a shared library. */
918 bool
_bfd_elf_omit_section_dynsym_default(bfd * output_bfd ATTRIBUTE_UNUSED,struct bfd_link_info * info,asection * p)919 _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
920 struct bfd_link_info *info,
921 asection *p)
922 {
923 struct elf_link_hash_table *htab;
924 asection *ip;
925
926 switch (elf_section_data (p)->this_hdr.sh_type)
927 {
928 case SHT_PROGBITS:
929 case SHT_NOBITS:
930 /* If sh_type is yet undecided, assume it could be
931 SHT_PROGBITS/SHT_NOBITS. */
932 case SHT_NULL:
933 htab = elf_hash_table (info);
934 if (htab->text_index_section != NULL)
935 return p != htab->text_index_section && p != htab->data_index_section;
936
937 return (htab->dynobj != NULL
938 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
939 && ip->output_section == p);
940
941 /* There shouldn't be section relative relocations
942 against any other section. */
943 default:
944 return true;
945 }
946 }
947
948 bool
_bfd_elf_omit_section_dynsym_all(bfd * output_bfd ATTRIBUTE_UNUSED,struct bfd_link_info * info ATTRIBUTE_UNUSED,asection * p ATTRIBUTE_UNUSED)949 _bfd_elf_omit_section_dynsym_all
950 (bfd *output_bfd ATTRIBUTE_UNUSED,
951 struct bfd_link_info *info ATTRIBUTE_UNUSED,
952 asection *p ATTRIBUTE_UNUSED)
953 {
954 return true;
955 }
956
957 /* Assign dynsym indices. In a shared library we generate a section
958 symbol for each output section, which come first. Next come symbols
959 which have been forced to local binding. Then all of the back-end
960 allocated local dynamic syms, followed by the rest of the global
961 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
962 (This prevents the early call before elf_backend_init_index_section
963 and strip_excluded_output_sections setting dynindx for sections
964 that are stripped.) */
965
966 static unsigned long
_bfd_elf_link_renumber_dynsyms(bfd * output_bfd,struct bfd_link_info * info,unsigned long * section_sym_count)967 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
968 struct bfd_link_info *info,
969 unsigned long *section_sym_count)
970 {
971 unsigned long dynsymcount = 0;
972 bool do_sec = section_sym_count != NULL;
973
974 if (bfd_link_pic (info)
975 || elf_hash_table (info)->is_relocatable_executable)
976 {
977 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
978 asection *p;
979 for (p = output_bfd->sections; p ; p = p->next)
980 if ((p->flags & SEC_EXCLUDE) == 0
981 && (p->flags & SEC_ALLOC) != 0
982 && elf_hash_table (info)->dynamic_relocs
983 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
984 {
985 ++dynsymcount;
986 if (do_sec)
987 elf_section_data (p)->dynindx = dynsymcount;
988 }
989 else if (do_sec)
990 elf_section_data (p)->dynindx = 0;
991 }
992 if (do_sec)
993 *section_sym_count = dynsymcount;
994
995 elf_link_hash_traverse (elf_hash_table (info),
996 elf_link_renumber_local_hash_table_dynsyms,
997 &dynsymcount);
998
999 if (elf_hash_table (info)->dynlocal)
1000 {
1001 struct elf_link_local_dynamic_entry *p;
1002 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
1003 p->dynindx = ++dynsymcount;
1004 }
1005 elf_hash_table (info)->local_dynsymcount = dynsymcount;
1006
1007 elf_link_hash_traverse (elf_hash_table (info),
1008 elf_link_renumber_hash_table_dynsyms,
1009 &dynsymcount);
1010
1011 /* There is an unused NULL entry at the head of the table which we
1012 must account for in our count even if the table is empty since it
1013 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
1014 .dynamic section. */
1015 dynsymcount++;
1016
1017 elf_hash_table (info)->dynsymcount = dynsymcount;
1018 return dynsymcount;
1019 }
1020
1021 /* Merge st_other field. */
1022
1023 static void
elf_merge_st_other(bfd * abfd,struct elf_link_hash_entry * h,unsigned int st_other,asection * sec,bool definition,bool dynamic)1024 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
1025 unsigned int st_other, asection *sec,
1026 bool definition, bool dynamic)
1027 {
1028 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1029
1030 /* If st_other has a processor-specific meaning, specific
1031 code might be needed here. */
1032 if (bed->elf_backend_merge_symbol_attribute)
1033 (*bed->elf_backend_merge_symbol_attribute) (h, st_other, definition,
1034 dynamic);
1035
1036 if (!dynamic)
1037 {
1038 unsigned symvis = ELF_ST_VISIBILITY (st_other);
1039 unsigned hvis = ELF_ST_VISIBILITY (h->other);
1040
1041 /* Keep the most constraining visibility. Leave the remainder
1042 of the st_other field to elf_backend_merge_symbol_attribute. */
1043 if (symvis - 1 < hvis - 1)
1044 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1045 }
1046 else if (definition
1047 && ELF_ST_VISIBILITY (st_other) != STV_DEFAULT
1048 && (sec->flags & SEC_READONLY) == 0)
1049 h->protected_def = 1;
1050 }
1051
1052 /* This function is called when we want to merge a new symbol with an
1053 existing symbol. It handles the various cases which arise when we
1054 find a definition in a dynamic object, or when there is already a
1055 definition in a dynamic object. The new symbol is described by
1056 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1057 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1058 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1059 of an old common symbol. We set OVERRIDE if the old symbol is
1060 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1061 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1062 to change. By OK to change, we mean that we shouldn't warn if the
1063 type or size does change. */
1064
1065 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)1066 _bfd_elf_merge_symbol (bfd *abfd,
1067 struct bfd_link_info *info,
1068 const char *name,
1069 Elf_Internal_Sym *sym,
1070 asection **psec,
1071 bfd_vma *pvalue,
1072 struct elf_link_hash_entry **sym_hash,
1073 bfd **poldbfd,
1074 bool *pold_weak,
1075 unsigned int *pold_alignment,
1076 bool *skip,
1077 bfd **override,
1078 bool *type_change_ok,
1079 bool *size_change_ok,
1080 bool *matched)
1081 {
1082 asection *sec, *oldsec;
1083 struct elf_link_hash_entry *h;
1084 struct elf_link_hash_entry *hi;
1085 struct elf_link_hash_entry *flip;
1086 int bind;
1087 bfd *oldbfd;
1088 bool newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1089 bool newweak, oldweak, newfunc, oldfunc;
1090 const struct elf_backend_data *bed;
1091 char *new_version;
1092 bool default_sym = *matched;
1093 struct elf_link_hash_table *htab;
1094
1095 *skip = false;
1096 *override = NULL;
1097
1098 sec = *psec;
1099 bind = ELF_ST_BIND (sym->st_info);
1100
1101 if (! bfd_is_und_section (sec))
1102 h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
1103 else
1104 h = ((struct elf_link_hash_entry *)
1105 bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
1106 if (h == NULL)
1107 return false;
1108 *sym_hash = h;
1109
1110 bed = get_elf_backend_data (abfd);
1111
1112 /* NEW_VERSION is the symbol version of the new symbol. */
1113 if (h->versioned != unversioned)
1114 {
1115 /* Symbol version is unknown or versioned. */
1116 new_version = strrchr (name, ELF_VER_CHR);
1117 if (new_version)
1118 {
1119 if (h->versioned == unknown)
1120 {
1121 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1122 h->versioned = versioned_hidden;
1123 else
1124 h->versioned = versioned;
1125 }
1126 new_version += 1;
1127 if (new_version[0] == '\0')
1128 new_version = NULL;
1129 }
1130 else
1131 h->versioned = unversioned;
1132 }
1133 else
1134 new_version = NULL;
1135
1136 /* For merging, we only care about real symbols. But we need to make
1137 sure that indirect symbol dynamic flags are updated. */
1138 hi = h;
1139 while (h->root.type == bfd_link_hash_indirect
1140 || h->root.type == bfd_link_hash_warning)
1141 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1142
1143 if (!*matched)
1144 {
1145 if (hi == h || h->root.type == bfd_link_hash_new)
1146 *matched = true;
1147 else
1148 {
1149 /* OLD_HIDDEN is true if the existing symbol is only visible
1150 to the symbol with the same symbol version. NEW_HIDDEN is
1151 true if the new symbol is only visible to the symbol with
1152 the same symbol version. */
1153 bool old_hidden = h->versioned == versioned_hidden;
1154 bool new_hidden = hi->versioned == versioned_hidden;
1155 if (!old_hidden && !new_hidden)
1156 /* The new symbol matches the existing symbol if both
1157 aren't hidden. */
1158 *matched = true;
1159 else
1160 {
1161 /* OLD_VERSION is the symbol version of the existing
1162 symbol. */
1163 char *old_version;
1164
1165 if (h->versioned >= versioned)
1166 old_version = strrchr (h->root.root.string,
1167 ELF_VER_CHR) + 1;
1168 else
1169 old_version = NULL;
1170
1171 /* The new symbol matches the existing symbol if they
1172 have the same symbol version. */
1173 *matched = (old_version == new_version
1174 || (old_version != NULL
1175 && new_version != NULL
1176 && strcmp (old_version, new_version) == 0));
1177 }
1178 }
1179 }
1180
1181 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1182 existing symbol. */
1183
1184 oldbfd = NULL;
1185 oldsec = NULL;
1186 switch (h->root.type)
1187 {
1188 default:
1189 break;
1190
1191 case bfd_link_hash_undefined:
1192 case bfd_link_hash_undefweak:
1193 oldbfd = h->root.u.undef.abfd;
1194 break;
1195
1196 case bfd_link_hash_defined:
1197 case bfd_link_hash_defweak:
1198 oldbfd = h->root.u.def.section->owner;
1199 oldsec = h->root.u.def.section;
1200 break;
1201
1202 case bfd_link_hash_common:
1203 oldbfd = h->root.u.c.p->section->owner;
1204 oldsec = h->root.u.c.p->section;
1205 if (pold_alignment)
1206 *pold_alignment = h->root.u.c.p->alignment_power;
1207 break;
1208 }
1209 if (poldbfd && *poldbfd == NULL)
1210 *poldbfd = oldbfd;
1211
1212 /* Differentiate strong and weak symbols. */
1213 newweak = bind == STB_WEAK;
1214 oldweak = (h->root.type == bfd_link_hash_defweak
1215 || h->root.type == bfd_link_hash_undefweak);
1216 if (pold_weak)
1217 *pold_weak = oldweak;
1218
1219 /* We have to check it for every instance since the first few may be
1220 references and not all compilers emit symbol type for undefined
1221 symbols. */
1222 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1223
1224 htab = elf_hash_table (info);
1225
1226 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1227 respectively, is from a dynamic object. */
1228
1229 newdyn = (abfd->flags & DYNAMIC) != 0;
1230
1231 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1232 syms and defined syms in dynamic libraries respectively.
1233 ref_dynamic on the other hand can be set for a symbol defined in
1234 a dynamic library, and def_dynamic may not be set; When the
1235 definition in a dynamic lib is overridden by a definition in the
1236 executable use of the symbol in the dynamic lib becomes a
1237 reference to the executable symbol. */
1238 if (newdyn)
1239 {
1240 if (bfd_is_und_section (sec))
1241 {
1242 if (bind != STB_WEAK)
1243 {
1244 h->ref_dynamic_nonweak = 1;
1245 hi->ref_dynamic_nonweak = 1;
1246 }
1247 }
1248 else
1249 {
1250 /* Update the existing symbol only if they match. */
1251 if (*matched)
1252 h->dynamic_def = 1;
1253 hi->dynamic_def = 1;
1254 }
1255 }
1256
1257 /* If we just created the symbol, mark it as being an ELF symbol.
1258 Other than that, there is nothing to do--there is no merge issue
1259 with a newly defined symbol--so we just return. */
1260
1261 if (h->root.type == bfd_link_hash_new)
1262 {
1263 h->non_elf = 0;
1264 return true;
1265 }
1266
1267 /* In cases involving weak versioned symbols, we may wind up trying
1268 to merge a symbol with itself. Catch that here, to avoid the
1269 confusion that results if we try to override a symbol with
1270 itself. The additional tests catch cases like
1271 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1272 dynamic object, which we do want to handle here. */
1273 if (abfd == oldbfd
1274 && (newweak || oldweak)
1275 && ((abfd->flags & DYNAMIC) == 0
1276 || !h->def_regular))
1277 return true;
1278
1279 olddyn = false;
1280 if (oldbfd != NULL)
1281 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1282 else if (oldsec != NULL)
1283 {
1284 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1285 indices used by MIPS ELF. */
1286 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1287 }
1288
1289 /* Set non_ir_ref_dynamic only when not handling DT_NEEDED entries. */
1290 if (!htab->handling_dt_needed
1291 && oldbfd != NULL
1292 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN))
1293 {
1294 if (newdyn != olddyn)
1295 {
1296 /* Handle a case where plugin_notice won't be called and thus
1297 won't set the non_ir_ref flags on the first pass over
1298 symbols. */
1299 h->root.non_ir_ref_dynamic = true;
1300 hi->root.non_ir_ref_dynamic = true;
1301 }
1302 else if ((oldbfd->flags & BFD_PLUGIN) != 0
1303 && hi->root.type == bfd_link_hash_indirect)
1304 {
1305 /* Change indirect symbol from IR to undefined. */
1306 hi->root.type = bfd_link_hash_undefined;
1307 hi->root.u.undef.abfd = oldbfd;
1308 }
1309 }
1310
1311 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1312 respectively, appear to be a definition rather than reference. */
1313
1314 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1315
1316 olddef = (h->root.type != bfd_link_hash_undefined
1317 && h->root.type != bfd_link_hash_undefweak
1318 && h->root.type != bfd_link_hash_common);
1319
1320 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1321 respectively, appear to be a function. */
1322
1323 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1324 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1325
1326 oldfunc = (h->type != STT_NOTYPE
1327 && bed->is_function_type (h->type));
1328
1329 if (!(newfunc && oldfunc)
1330 && ELF_ST_TYPE (sym->st_info) != h->type
1331 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1332 && h->type != STT_NOTYPE
1333 && (newdef || bfd_is_com_section (sec))
1334 && (olddef || h->root.type == bfd_link_hash_common))
1335 {
1336 /* If creating a default indirect symbol ("foo" or "foo@") from
1337 a dynamic versioned definition ("foo@@") skip doing so if
1338 there is an existing regular definition with a different
1339 type. We don't want, for example, a "time" variable in the
1340 executable overriding a "time" function in a shared library. */
1341 if (newdyn
1342 && !olddyn)
1343 {
1344 *skip = true;
1345 return true;
1346 }
1347
1348 /* When adding a symbol from a regular object file after we have
1349 created indirect symbols, undo the indirection and any
1350 dynamic state. */
1351 if (hi != h
1352 && !newdyn
1353 && olddyn)
1354 {
1355 h = hi;
1356 (*bed->elf_backend_hide_symbol) (info, h, true);
1357 h->forced_local = 0;
1358 h->ref_dynamic = 0;
1359 h->def_dynamic = 0;
1360 h->dynamic_def = 0;
1361 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1362 {
1363 h->root.type = bfd_link_hash_undefined;
1364 h->root.u.undef.abfd = abfd;
1365 }
1366 else
1367 {
1368 h->root.type = bfd_link_hash_new;
1369 h->root.u.undef.abfd = NULL;
1370 }
1371 return true;
1372 }
1373 }
1374
1375 /* Check TLS symbols. We don't check undefined symbols introduced
1376 by "ld -u" which have no type (and oldbfd NULL), and we don't
1377 check symbols from plugins because they also have no type. */
1378 if (oldbfd != NULL
1379 && (oldbfd->flags & BFD_PLUGIN) == 0
1380 && (abfd->flags & BFD_PLUGIN) == 0
1381 && ELF_ST_TYPE (sym->st_info) != h->type
1382 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1383 {
1384 bfd *ntbfd, *tbfd;
1385 bool ntdef, tdef;
1386 asection *ntsec, *tsec;
1387
1388 if (h->type == STT_TLS)
1389 {
1390 ntbfd = abfd;
1391 ntsec = sec;
1392 ntdef = newdef;
1393 tbfd = oldbfd;
1394 tsec = oldsec;
1395 tdef = olddef;
1396 }
1397 else
1398 {
1399 ntbfd = oldbfd;
1400 ntsec = oldsec;
1401 ntdef = olddef;
1402 tbfd = abfd;
1403 tsec = sec;
1404 tdef = newdef;
1405 }
1406
1407 if (tdef && ntdef)
1408 _bfd_error_handler
1409 /* xgettext:c-format */
1410 (_("%s: TLS definition in %pB section %pA "
1411 "mismatches non-TLS definition in %pB section %pA"),
1412 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1413 else if (!tdef && !ntdef)
1414 _bfd_error_handler
1415 /* xgettext:c-format */
1416 (_("%s: TLS reference in %pB "
1417 "mismatches non-TLS reference in %pB"),
1418 h->root.root.string, tbfd, ntbfd);
1419 else if (tdef)
1420 _bfd_error_handler
1421 /* xgettext:c-format */
1422 (_("%s: TLS definition in %pB section %pA "
1423 "mismatches non-TLS reference in %pB"),
1424 h->root.root.string, tbfd, tsec, ntbfd);
1425 else
1426 _bfd_error_handler
1427 /* xgettext:c-format */
1428 (_("%s: TLS reference in %pB "
1429 "mismatches non-TLS definition in %pB section %pA"),
1430 h->root.root.string, tbfd, ntbfd, ntsec);
1431
1432 bfd_set_error (bfd_error_bad_value);
1433 return false;
1434 }
1435
1436 /* If the old symbol has non-default visibility, we ignore the new
1437 definition from a dynamic object. */
1438 if (newdyn
1439 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1440 && !bfd_is_und_section (sec))
1441 {
1442 *skip = true;
1443 /* Make sure this symbol is dynamic. */
1444 h->ref_dynamic = 1;
1445 hi->ref_dynamic = 1;
1446 /* A protected symbol has external availability. Make sure it is
1447 recorded as dynamic.
1448
1449 FIXME: Should we check type and size for protected symbol? */
1450 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1451 return bfd_elf_link_record_dynamic_symbol (info, h);
1452 else
1453 return true;
1454 }
1455 else if (!newdyn
1456 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1457 && h->def_dynamic)
1458 {
1459 /* If the new symbol with non-default visibility comes from a
1460 relocatable file and the old definition comes from a dynamic
1461 object, we remove the old definition. */
1462 if (hi->root.type == bfd_link_hash_indirect)
1463 {
1464 /* Handle the case where the old dynamic definition is
1465 default versioned. We need to copy the symbol info from
1466 the symbol with default version to the normal one if it
1467 was referenced before. */
1468 if (h->ref_regular)
1469 {
1470 hi->root.type = h->root.type;
1471 h->root.type = bfd_link_hash_indirect;
1472 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1473
1474 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1475 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1476 {
1477 /* If the new symbol is hidden or internal, completely undo
1478 any dynamic link state. */
1479 (*bed->elf_backend_hide_symbol) (info, h, true);
1480 h->forced_local = 0;
1481 h->ref_dynamic = 0;
1482 }
1483 else
1484 h->ref_dynamic = 1;
1485
1486 h->def_dynamic = 0;
1487 /* FIXME: Should we check type and size for protected symbol? */
1488 h->size = 0;
1489 h->type = 0;
1490
1491 h = hi;
1492 }
1493 else
1494 h = hi;
1495 }
1496
1497 /* If the old symbol was undefined before, then it will still be
1498 on the undefs list. If the new symbol is undefined or
1499 common, we can't make it bfd_link_hash_new here, because new
1500 undefined or common symbols will be added to the undefs list
1501 by _bfd_generic_link_add_one_symbol. Symbols may not be
1502 added twice to the undefs list. Also, if the new symbol is
1503 undefweak then we don't want to lose the strong undef. */
1504 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1505 {
1506 h->root.type = bfd_link_hash_undefined;
1507 h->root.u.undef.abfd = abfd;
1508 }
1509 else
1510 {
1511 h->root.type = bfd_link_hash_new;
1512 h->root.u.undef.abfd = NULL;
1513 }
1514
1515 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1516 {
1517 /* If the new symbol is hidden or internal, completely undo
1518 any dynamic link state. */
1519 (*bed->elf_backend_hide_symbol) (info, h, true);
1520 h->forced_local = 0;
1521 h->ref_dynamic = 0;
1522 }
1523 else
1524 h->ref_dynamic = 1;
1525 h->def_dynamic = 0;
1526 /* FIXME: Should we check type and size for protected symbol? */
1527 h->size = 0;
1528 h->type = 0;
1529 return true;
1530 }
1531
1532 /* If a new weak symbol definition comes from a regular file and the
1533 old symbol comes from a dynamic library, we treat the new one as
1534 strong. Similarly, an old weak symbol definition from a regular
1535 file is treated as strong when the new symbol comes from a dynamic
1536 library. Further, an old weak symbol from a dynamic library is
1537 treated as strong if the new symbol is from a dynamic library.
1538 This reflects the way glibc's ld.so works.
1539
1540 Also allow a weak symbol to override a linker script symbol
1541 defined by an early pass over the script. This is done so the
1542 linker knows the symbol is defined in an object file, for the
1543 DEFINED script function.
1544
1545 Do this before setting *type_change_ok or *size_change_ok so that
1546 we warn properly when dynamic library symbols are overridden. */
1547
1548 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1549 newweak = false;
1550 if (olddef && newdyn)
1551 oldweak = false;
1552
1553 /* Allow changes between different types of function symbol. */
1554 if (newfunc && oldfunc)
1555 *type_change_ok = true;
1556
1557 /* It's OK to change the type if either the existing symbol or the
1558 new symbol is weak. A type change is also OK if the old symbol
1559 is undefined and the new symbol is defined. */
1560
1561 if (oldweak
1562 || newweak
1563 || (newdef
1564 && h->root.type == bfd_link_hash_undefined))
1565 *type_change_ok = true;
1566
1567 /* It's OK to change the size if either the existing symbol or the
1568 new symbol is weak, or if the old symbol is undefined. */
1569
1570 if (*type_change_ok
1571 || h->root.type == bfd_link_hash_undefined)
1572 *size_change_ok = true;
1573
1574 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1575 symbol, respectively, appears to be a common symbol in a dynamic
1576 object. If a symbol appears in an uninitialized section, and is
1577 not weak, and is not a function, then it may be a common symbol
1578 which was resolved when the dynamic object was created. We want
1579 to treat such symbols specially, because they raise special
1580 considerations when setting the symbol size: if the symbol
1581 appears as a common symbol in a regular object, and the size in
1582 the regular object is larger, we must make sure that we use the
1583 larger size. This problematic case can always be avoided in C,
1584 but it must be handled correctly when using Fortran shared
1585 libraries.
1586
1587 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1588 likewise for OLDDYNCOMMON and OLDDEF.
1589
1590 Note that this test is just a heuristic, and that it is quite
1591 possible to have an uninitialized symbol in a shared object which
1592 is really a definition, rather than a common symbol. This could
1593 lead to some minor confusion when the symbol really is a common
1594 symbol in some regular object. However, I think it will be
1595 harmless. */
1596
1597 if (newdyn
1598 && newdef
1599 && !newweak
1600 && (sec->flags & SEC_ALLOC) != 0
1601 && (sec->flags & SEC_LOAD) == 0
1602 && sym->st_size > 0
1603 && !newfunc)
1604 newdyncommon = true;
1605 else
1606 newdyncommon = false;
1607
1608 if (olddyn
1609 && olddef
1610 && h->root.type == bfd_link_hash_defined
1611 && h->def_dynamic
1612 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1613 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1614 && h->size > 0
1615 && !oldfunc)
1616 olddyncommon = true;
1617 else
1618 olddyncommon = false;
1619
1620 /* We now know everything about the old and new symbols. We ask the
1621 backend to check if we can merge them. */
1622 if (bed->merge_symbol != NULL)
1623 {
1624 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1625 return false;
1626 sec = *psec;
1627 }
1628
1629 /* There are multiple definitions of a normal symbol. Skip the
1630 default symbol as well as definition from an IR object. */
1631 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1632 && !default_sym && h->def_regular
1633 && !(oldbfd != NULL
1634 && (oldbfd->flags & BFD_PLUGIN) != 0
1635 && (abfd->flags & BFD_PLUGIN) == 0))
1636 {
1637 /* Handle a multiple definition. */
1638 (*info->callbacks->multiple_definition) (info, &h->root,
1639 abfd, sec, *pvalue);
1640 *skip = true;
1641 return true;
1642 }
1643
1644 /* If both the old and the new symbols look like common symbols in a
1645 dynamic object, set the size of the symbol to the larger of the
1646 two. */
1647
1648 if (olddyncommon
1649 && newdyncommon
1650 && sym->st_size != h->size)
1651 {
1652 /* Since we think we have two common symbols, issue a multiple
1653 common warning if desired. Note that we only warn if the
1654 size is different. If the size is the same, we simply let
1655 the old symbol override the new one as normally happens with
1656 symbols defined in dynamic objects. */
1657
1658 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1659 bfd_link_hash_common, sym->st_size);
1660 if (sym->st_size > h->size)
1661 h->size = sym->st_size;
1662
1663 *size_change_ok = true;
1664 }
1665
1666 /* If we are looking at a dynamic object, and we have found a
1667 definition, we need to see if the symbol was already defined by
1668 some other object. If so, we want to use the existing
1669 definition, and we do not want to report a multiple symbol
1670 definition error; we do this by clobbering *PSEC to be
1671 bfd_und_section_ptr.
1672
1673 We treat a common symbol as a definition if the symbol in the
1674 shared library is a function, since common symbols always
1675 represent variables; this can cause confusion in principle, but
1676 any such confusion would seem to indicate an erroneous program or
1677 shared library. We also permit a common symbol in a regular
1678 object to override a weak symbol in a shared object. */
1679
1680 if (newdyn
1681 && newdef
1682 && (olddef
1683 || (h->root.type == bfd_link_hash_common
1684 && (newweak || newfunc))))
1685 {
1686 *override = abfd;
1687 newdef = false;
1688 newdyncommon = false;
1689
1690 *psec = sec = bfd_und_section_ptr;
1691 *size_change_ok = true;
1692
1693 /* If we get here when the old symbol is a common symbol, then
1694 we are explicitly letting it override a weak symbol or
1695 function in a dynamic object, and we don't want to warn about
1696 a type change. If the old symbol is a defined symbol, a type
1697 change warning may still be appropriate. */
1698
1699 if (h->root.type == bfd_link_hash_common)
1700 *type_change_ok = true;
1701 }
1702
1703 /* Handle the special case of an old common symbol merging with a
1704 new symbol which looks like a common symbol in a shared object.
1705 We change *PSEC and *PVALUE to make the new symbol look like a
1706 common symbol, and let _bfd_generic_link_add_one_symbol do the
1707 right thing. */
1708
1709 if (newdyncommon
1710 && h->root.type == bfd_link_hash_common)
1711 {
1712 *override = oldbfd;
1713 newdef = false;
1714 newdyncommon = false;
1715 *pvalue = sym->st_size;
1716 *psec = sec = bed->common_section (oldsec);
1717 *size_change_ok = true;
1718 }
1719
1720 /* Skip weak definitions of symbols that are already defined. */
1721 if (newdef && olddef && newweak)
1722 {
1723 /* Don't skip new non-IR weak syms. */
1724 if (!(oldbfd != NULL
1725 && (oldbfd->flags & BFD_PLUGIN) != 0
1726 && (abfd->flags & BFD_PLUGIN) == 0))
1727 {
1728 newdef = false;
1729 *skip = true;
1730 }
1731
1732 /* Merge st_other. If the symbol already has a dynamic index,
1733 but visibility says it should not be visible, turn it into a
1734 local symbol. */
1735 elf_merge_st_other (abfd, h, sym->st_other, sec, newdef, newdyn);
1736 if (h->dynindx != -1)
1737 switch (ELF_ST_VISIBILITY (h->other))
1738 {
1739 case STV_INTERNAL:
1740 case STV_HIDDEN:
1741 (*bed->elf_backend_hide_symbol) (info, h, true);
1742 break;
1743 }
1744 }
1745
1746 /* If the old symbol is from a dynamic object, and the new symbol is
1747 a definition which is not from a dynamic object, then the new
1748 symbol overrides the old symbol. Symbols from regular files
1749 always take precedence over symbols from dynamic objects, even if
1750 they are defined after the dynamic object in the link.
1751
1752 As above, we again permit a common symbol in a regular object to
1753 override a definition in a shared object if the shared object
1754 symbol is a function or is weak. */
1755
1756 flip = NULL;
1757 if (!newdyn
1758 && (newdef
1759 || (bfd_is_com_section (sec)
1760 && (oldweak || oldfunc)))
1761 && olddyn
1762 && olddef
1763 && h->def_dynamic)
1764 {
1765 /* Change the hash table entry to undefined, and let
1766 _bfd_generic_link_add_one_symbol do the right thing with the
1767 new definition. */
1768
1769 h->root.type = bfd_link_hash_undefined;
1770 h->root.u.undef.abfd = h->root.u.def.section->owner;
1771 *size_change_ok = true;
1772
1773 olddef = false;
1774 olddyncommon = false;
1775
1776 /* We again permit a type change when a common symbol may be
1777 overriding a function. */
1778
1779 if (bfd_is_com_section (sec))
1780 {
1781 if (oldfunc)
1782 {
1783 /* If a common symbol overrides a function, make sure
1784 that it isn't defined dynamically nor has type
1785 function. */
1786 h->def_dynamic = 0;
1787 h->type = STT_NOTYPE;
1788 }
1789 *type_change_ok = true;
1790 }
1791
1792 if (hi->root.type == bfd_link_hash_indirect)
1793 flip = hi;
1794 else
1795 /* This union may have been set to be non-NULL when this symbol
1796 was seen in a dynamic object. We must force the union to be
1797 NULL, so that it is correct for a regular symbol. */
1798 h->verinfo.vertree = NULL;
1799 }
1800
1801 /* Handle the special case of a new common symbol merging with an
1802 old symbol that looks like it might be a common symbol defined in
1803 a shared object. Note that we have already handled the case in
1804 which a new common symbol should simply override the definition
1805 in the shared library. */
1806
1807 if (! newdyn
1808 && bfd_is_com_section (sec)
1809 && olddyncommon)
1810 {
1811 /* It would be best if we could set the hash table entry to a
1812 common symbol, but we don't know what to use for the section
1813 or the alignment. */
1814 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1815 bfd_link_hash_common, sym->st_size);
1816
1817 /* If the presumed common symbol in the dynamic object is
1818 larger, pretend that the new symbol has its size. */
1819
1820 if (h->size > *pvalue)
1821 *pvalue = h->size;
1822
1823 /* We need to remember the alignment required by the symbol
1824 in the dynamic object. */
1825 BFD_ASSERT (pold_alignment);
1826 *pold_alignment = h->root.u.def.section->alignment_power;
1827
1828 olddef = false;
1829 olddyncommon = false;
1830
1831 h->root.type = bfd_link_hash_undefined;
1832 h->root.u.undef.abfd = h->root.u.def.section->owner;
1833
1834 *size_change_ok = true;
1835 *type_change_ok = true;
1836
1837 if (hi->root.type == bfd_link_hash_indirect)
1838 flip = hi;
1839 else
1840 h->verinfo.vertree = NULL;
1841 }
1842
1843 if (flip != NULL)
1844 {
1845 /* Handle the case where we had a versioned symbol in a dynamic
1846 library and now find a definition in a normal object. In this
1847 case, we make the versioned symbol point to the normal one. */
1848 flip->root.type = h->root.type;
1849 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1850 h->root.type = bfd_link_hash_indirect;
1851 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1852 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1853 if (h->def_dynamic)
1854 {
1855 h->def_dynamic = 0;
1856 flip->ref_dynamic = 1;
1857 }
1858 }
1859
1860 return true;
1861 }
1862
1863 /* This function is called to create an indirect symbol from the
1864 default for the symbol with the default version if needed. The
1865 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1866 set DYNSYM if the new indirect symbol is dynamic. */
1867
1868 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)1869 _bfd_elf_add_default_symbol (bfd *abfd,
1870 struct bfd_link_info *info,
1871 struct elf_link_hash_entry *h,
1872 const char *name,
1873 Elf_Internal_Sym *sym,
1874 asection *sec,
1875 bfd_vma value,
1876 bfd **poldbfd,
1877 bool *dynsym)
1878 {
1879 bool type_change_ok;
1880 bool size_change_ok;
1881 bool skip;
1882 char *shortname;
1883 struct elf_link_hash_entry *hi;
1884 struct bfd_link_hash_entry *bh;
1885 const struct elf_backend_data *bed;
1886 bool collect;
1887 bool dynamic;
1888 bfd *override;
1889 char *p;
1890 size_t len, shortlen;
1891 asection *tmp_sec;
1892 bool matched;
1893
1894 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1895 return true;
1896
1897 /* If this symbol has a version, and it is the default version, we
1898 create an indirect symbol from the default name to the fully
1899 decorated name. This will cause external references which do not
1900 specify a version to be bound to this version of the symbol. */
1901 p = strchr (name, ELF_VER_CHR);
1902 if (h->versioned == unknown)
1903 {
1904 if (p == NULL)
1905 {
1906 h->versioned = unversioned;
1907 return true;
1908 }
1909 else
1910 {
1911 if (p[1] != ELF_VER_CHR)
1912 {
1913 h->versioned = versioned_hidden;
1914 return true;
1915 }
1916 else
1917 h->versioned = versioned;
1918 }
1919 }
1920 else
1921 {
1922 /* PR ld/19073: We may see an unversioned definition after the
1923 default version. */
1924 if (p == NULL)
1925 return true;
1926 }
1927
1928 bed = get_elf_backend_data (abfd);
1929 collect = bed->collect;
1930 dynamic = (abfd->flags & DYNAMIC) != 0;
1931
1932 shortlen = p - name;
1933 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1934 if (shortname == NULL)
1935 return false;
1936 memcpy (shortname, name, shortlen);
1937 shortname[shortlen] = '\0';
1938
1939 /* We are going to create a new symbol. Merge it with any existing
1940 symbol with this name. For the purposes of the merge, act as
1941 though we were defining the symbol we just defined, although we
1942 actually going to define an indirect symbol. */
1943 type_change_ok = false;
1944 size_change_ok = false;
1945 matched = true;
1946 tmp_sec = sec;
1947 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1948 &hi, poldbfd, NULL, NULL, &skip, &override,
1949 &type_change_ok, &size_change_ok, &matched))
1950 return false;
1951
1952 if (skip)
1953 goto nondefault;
1954
1955 if (hi->def_regular || ELF_COMMON_DEF_P (hi))
1956 {
1957 /* If the undecorated symbol will have a version added by a
1958 script different to H, then don't indirect to/from the
1959 undecorated symbol. This isn't ideal because we may not yet
1960 have seen symbol versions, if given by a script on the
1961 command line rather than via --version-script. */
1962 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1963 {
1964 bool hide;
1965
1966 hi->verinfo.vertree
1967 = bfd_find_version_for_sym (info->version_info,
1968 hi->root.root.string, &hide);
1969 if (hi->verinfo.vertree != NULL && hide)
1970 {
1971 (*bed->elf_backend_hide_symbol) (info, hi, true);
1972 goto nondefault;
1973 }
1974 }
1975 if (hi->verinfo.vertree != NULL
1976 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1977 goto nondefault;
1978 }
1979
1980 if (! override)
1981 {
1982 /* Add the default symbol if not performing a relocatable link. */
1983 if (! bfd_link_relocatable (info))
1984 {
1985 bh = &hi->root;
1986 if (bh->type == bfd_link_hash_defined
1987 && bh->u.def.section->owner != NULL
1988 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
1989 {
1990 /* Mark the previous definition from IR object as
1991 undefined so that the generic linker will override
1992 it. */
1993 bh->type = bfd_link_hash_undefined;
1994 bh->u.undef.abfd = bh->u.def.section->owner;
1995 }
1996 if (! (_bfd_generic_link_add_one_symbol
1997 (info, abfd, shortname, BSF_INDIRECT,
1998 bfd_ind_section_ptr,
1999 0, name, false, collect, &bh)))
2000 return false;
2001 hi = (struct elf_link_hash_entry *) bh;
2002 }
2003 }
2004 else
2005 {
2006 /* In this case the symbol named SHORTNAME is overriding the
2007 indirect symbol we want to add. We were planning on making
2008 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
2009 is the name without a version. NAME is the fully versioned
2010 name, and it is the default version.
2011
2012 Overriding means that we already saw a definition for the
2013 symbol SHORTNAME in a regular object, and it is overriding
2014 the symbol defined in the dynamic object.
2015
2016 When this happens, we actually want to change NAME, the
2017 symbol we just added, to refer to SHORTNAME. This will cause
2018 references to NAME in the shared object to become references
2019 to SHORTNAME in the regular object. This is what we expect
2020 when we override a function in a shared object: that the
2021 references in the shared object will be mapped to the
2022 definition in the regular object. */
2023
2024 while (hi->root.type == bfd_link_hash_indirect
2025 || hi->root.type == bfd_link_hash_warning)
2026 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2027
2028 h->root.type = bfd_link_hash_indirect;
2029 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
2030 if (h->def_dynamic)
2031 {
2032 h->def_dynamic = 0;
2033 hi->ref_dynamic = 1;
2034 if (hi->ref_regular
2035 || hi->def_regular)
2036 {
2037 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
2038 return false;
2039 }
2040 }
2041
2042 /* Now set HI to H, so that the following code will set the
2043 other fields correctly. */
2044 hi = h;
2045 }
2046
2047 /* Check if HI is a warning symbol. */
2048 if (hi->root.type == bfd_link_hash_warning)
2049 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2050
2051 /* If there is a duplicate definition somewhere, then HI may not
2052 point to an indirect symbol. We will have reported an error to
2053 the user in that case. */
2054
2055 if (hi->root.type == bfd_link_hash_indirect)
2056 {
2057 struct elf_link_hash_entry *ht;
2058
2059 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2060 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2061
2062 /* If we first saw a reference to SHORTNAME with non-default
2063 visibility, merge that visibility to the @@VER symbol. */
2064 elf_merge_st_other (abfd, ht, hi->other, sec, true, dynamic);
2065
2066 /* A reference to the SHORTNAME symbol from a dynamic library
2067 will be satisfied by the versioned symbol at runtime. In
2068 effect, we have a reference to the versioned symbol. */
2069 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2070 hi->dynamic_def |= ht->dynamic_def;
2071
2072 /* See if the new flags lead us to realize that the symbol must
2073 be dynamic. */
2074 if (! *dynsym)
2075 {
2076 if (! dynamic)
2077 {
2078 if (! bfd_link_executable (info)
2079 || hi->def_dynamic
2080 || hi->ref_dynamic)
2081 *dynsym = true;
2082 }
2083 else
2084 {
2085 if (hi->ref_regular)
2086 *dynsym = true;
2087 }
2088 }
2089 }
2090
2091 /* We also need to define an indirection from the nondefault version
2092 of the symbol. */
2093
2094 nondefault:
2095 len = strlen (name);
2096 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2097 if (shortname == NULL)
2098 return false;
2099 memcpy (shortname, name, shortlen);
2100 memcpy (shortname + shortlen, p + 1, len - shortlen);
2101
2102 /* Once again, merge with any existing symbol. */
2103 type_change_ok = false;
2104 size_change_ok = false;
2105 tmp_sec = sec;
2106 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2107 &hi, poldbfd, NULL, NULL, &skip, &override,
2108 &type_change_ok, &size_change_ok, &matched))
2109 return false;
2110
2111 if (skip)
2112 {
2113 if (!dynamic
2114 && h->root.type == bfd_link_hash_defweak
2115 && hi->root.type == bfd_link_hash_defined)
2116 {
2117 /* We are handling a weak sym@@ver and attempting to define
2118 a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2119 new weak sym@ver because there is already a strong sym@ver.
2120 However, sym@ver and sym@@ver are really the same symbol.
2121 The existing strong sym@ver ought to override sym@@ver. */
2122 h->root.type = bfd_link_hash_defined;
2123 h->root.u.def.section = hi->root.u.def.section;
2124 h->root.u.def.value = hi->root.u.def.value;
2125 hi->root.type = bfd_link_hash_indirect;
2126 hi->root.u.i.link = &h->root;
2127 }
2128 else
2129 return true;
2130 }
2131 else if (override)
2132 {
2133 /* Here SHORTNAME is a versioned name, so we don't expect to see
2134 the type of override we do in the case above unless it is
2135 overridden by a versioned definition. */
2136 if (hi->root.type != bfd_link_hash_defined
2137 && hi->root.type != bfd_link_hash_defweak)
2138 _bfd_error_handler
2139 /* xgettext:c-format */
2140 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2141 abfd, shortname);
2142 return true;
2143 }
2144 else
2145 {
2146 bh = &hi->root;
2147 if (! (_bfd_generic_link_add_one_symbol
2148 (info, abfd, shortname, BSF_INDIRECT,
2149 bfd_ind_section_ptr, 0, name, false, collect, &bh)))
2150 return false;
2151 hi = (struct elf_link_hash_entry *) bh;
2152 }
2153
2154 /* If there is a duplicate definition somewhere, then HI may not
2155 point to an indirect symbol. We will have reported an error
2156 to the user in that case. */
2157 if (hi->root.type == bfd_link_hash_indirect)
2158 {
2159 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2160 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2161 hi->dynamic_def |= h->dynamic_def;
2162
2163 /* If we first saw a reference to @VER symbol with
2164 non-default visibility, merge that visibility to the
2165 @@VER symbol. */
2166 elf_merge_st_other (abfd, h, hi->other, sec, true, dynamic);
2167
2168 /* See if the new flags lead us to realize that the symbol
2169 must be dynamic. */
2170 if (! *dynsym)
2171 {
2172 if (! dynamic)
2173 {
2174 if (! bfd_link_executable (info)
2175 || hi->ref_dynamic)
2176 *dynsym = true;
2177 }
2178 else
2179 {
2180 if (hi->ref_regular)
2181 *dynsym = true;
2182 }
2183 }
2184 }
2185
2186 return true;
2187 }
2188
2189 /* This routine is used to export all defined symbols into the dynamic
2190 symbol table. It is called via elf_link_hash_traverse. */
2191
2192 static bool
_bfd_elf_export_symbol(struct elf_link_hash_entry * h,void * data)2193 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2194 {
2195 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2196
2197 /* Ignore indirect symbols. These are added by the versioning code. */
2198 if (h->root.type == bfd_link_hash_indirect)
2199 return true;
2200
2201 /* Ignore this if we won't export it. */
2202 if (!eif->info->export_dynamic && !h->dynamic)
2203 return true;
2204
2205 if (h->dynindx == -1
2206 && (h->def_regular || h->ref_regular)
2207 && ! bfd_hide_sym_by_version (eif->info->version_info,
2208 h->root.root.string))
2209 {
2210 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2211 {
2212 eif->failed = true;
2213 return false;
2214 }
2215 }
2216
2217 return true;
2218 }
2219
2220 /* Return true if GLIBC_ABI_DT_RELR is added to the list of version
2221 dependencies successfully. GLIBC_ABI_DT_RELR will be put into the
2222 .gnu.version_r section. */
2223
2224 static bool
elf_link_add_dt_relr_dependency(struct elf_find_verdep_info * rinfo)2225 elf_link_add_dt_relr_dependency (struct elf_find_verdep_info *rinfo)
2226 {
2227 bfd *glibc_bfd = NULL;
2228 Elf_Internal_Verneed *t;
2229 Elf_Internal_Vernaux *a;
2230 size_t amt;
2231 const char *relr = "GLIBC_ABI_DT_RELR";
2232
2233 /* See if we already know about GLIBC_PRIVATE_DT_RELR. */
2234 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2235 t != NULL;
2236 t = t->vn_nextref)
2237 {
2238 const char *soname = bfd_elf_get_dt_soname (t->vn_bfd);
2239 /* Skip the shared library if it isn't libc.so. */
2240 if (!soname || !startswith (soname, "libc.so."))
2241 continue;
2242
2243 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2244 {
2245 /* Return if GLIBC_PRIVATE_DT_RELR dependency has been
2246 added. */
2247 if (a->vna_nodename == relr
2248 || strcmp (a->vna_nodename, relr) == 0)
2249 return true;
2250
2251 /* Check if libc.so provides GLIBC_2.XX version. */
2252 if (!glibc_bfd && startswith (a->vna_nodename, "GLIBC_2."))
2253 glibc_bfd = t->vn_bfd;
2254 }
2255
2256 break;
2257 }
2258
2259 /* Skip if it isn't linked against glibc. */
2260 if (glibc_bfd == NULL)
2261 return true;
2262
2263 /* This is a new version. Add it to tree we are building. */
2264 if (t == NULL)
2265 {
2266 amt = sizeof *t;
2267 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd,
2268 amt);
2269 if (t == NULL)
2270 {
2271 rinfo->failed = true;
2272 return false;
2273 }
2274
2275 t->vn_bfd = glibc_bfd;
2276 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2277 elf_tdata (rinfo->info->output_bfd)->verref = t;
2278 }
2279
2280 amt = sizeof *a;
2281 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2282 if (a == NULL)
2283 {
2284 rinfo->failed = true;
2285 return false;
2286 }
2287
2288 a->vna_nodename = relr;
2289 a->vna_flags = 0;
2290 a->vna_nextptr = t->vn_auxptr;
2291 a->vna_other = rinfo->vers + 1;
2292 ++rinfo->vers;
2293
2294 t->vn_auxptr = a;
2295
2296 return true;
2297 }
2298
2299 /* Look through the symbols which are defined in other shared
2300 libraries and referenced here. Update the list of version
2301 dependencies. This will be put into the .gnu.version_r section.
2302 This function is called via elf_link_hash_traverse. */
2303
2304 static bool
_bfd_elf_link_find_version_dependencies(struct elf_link_hash_entry * h,void * data)2305 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2306 void *data)
2307 {
2308 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2309 Elf_Internal_Verneed *t;
2310 Elf_Internal_Vernaux *a;
2311 size_t amt;
2312
2313 /* We only care about symbols defined in shared objects with version
2314 information. */
2315 if (!h->def_dynamic
2316 || h->def_regular
2317 || h->dynindx == -1
2318 || h->verinfo.verdef == NULL
2319 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2320 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2321 return true;
2322
2323 /* See if we already know about this version. */
2324 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2325 t != NULL;
2326 t = t->vn_nextref)
2327 {
2328 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2329 continue;
2330
2331 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2332 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2333 return true;
2334
2335 break;
2336 }
2337
2338 /* This is a new version. Add it to tree we are building. */
2339
2340 if (t == NULL)
2341 {
2342 amt = sizeof *t;
2343 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2344 if (t == NULL)
2345 {
2346 rinfo->failed = true;
2347 return false;
2348 }
2349
2350 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2351 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2352 elf_tdata (rinfo->info->output_bfd)->verref = t;
2353 }
2354
2355 amt = sizeof *a;
2356 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2357 if (a == NULL)
2358 {
2359 rinfo->failed = true;
2360 return false;
2361 }
2362
2363 /* Note that we are copying a string pointer here, and testing it
2364 above. If bfd_elf_string_from_elf_section is ever changed to
2365 discard the string data when low in memory, this will have to be
2366 fixed. */
2367 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2368
2369 a->vna_flags = h->verinfo.verdef->vd_flags;
2370 a->vna_nextptr = t->vn_auxptr;
2371
2372 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2373 ++rinfo->vers;
2374
2375 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2376
2377 t->vn_auxptr = a;
2378
2379 return true;
2380 }
2381
2382 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2383 hidden. Set *T_P to NULL if there is no match. */
2384
2385 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)2386 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2387 struct elf_link_hash_entry *h,
2388 const char *version_p,
2389 struct bfd_elf_version_tree **t_p,
2390 bool *hide)
2391 {
2392 struct bfd_elf_version_tree *t;
2393
2394 /* Look for the version. If we find it, it is no longer weak. */
2395 for (t = info->version_info; t != NULL; t = t->next)
2396 {
2397 if (strcmp (t->name, version_p) == 0)
2398 {
2399 size_t len;
2400 char *alc;
2401 struct bfd_elf_version_expr *d;
2402
2403 len = version_p - h->root.root.string;
2404 alc = (char *) bfd_malloc (len);
2405 if (alc == NULL)
2406 return false;
2407 memcpy (alc, h->root.root.string, len - 1);
2408 alc[len - 1] = '\0';
2409 if (alc[len - 2] == ELF_VER_CHR)
2410 alc[len - 2] = '\0';
2411
2412 h->verinfo.vertree = t;
2413 t->used = true;
2414 d = NULL;
2415
2416 if (t->globals.list != NULL)
2417 d = (*t->match) (&t->globals, NULL, alc);
2418
2419 /* See if there is anything to force this symbol to
2420 local scope. */
2421 if (d == NULL && t->locals.list != NULL)
2422 {
2423 d = (*t->match) (&t->locals, NULL, alc);
2424 if (d != NULL
2425 && h->dynindx != -1
2426 && ! info->export_dynamic)
2427 *hide = true;
2428 }
2429
2430 free (alc);
2431 break;
2432 }
2433 }
2434
2435 *t_p = t;
2436
2437 return true;
2438 }
2439
2440 /* Return TRUE if the symbol H is hidden by version script. */
2441
2442 bool
_bfd_elf_link_hide_sym_by_version(struct bfd_link_info * info,struct elf_link_hash_entry * h)2443 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2444 struct elf_link_hash_entry *h)
2445 {
2446 const char *p;
2447 bool hide = false;
2448 const struct elf_backend_data *bed
2449 = get_elf_backend_data (info->output_bfd);
2450
2451 /* Version script only hides symbols defined in regular objects. */
2452 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2453 return true;
2454
2455 p = strchr (h->root.root.string, ELF_VER_CHR);
2456 if (p != NULL && h->verinfo.vertree == NULL)
2457 {
2458 struct bfd_elf_version_tree *t;
2459
2460 ++p;
2461 if (*p == ELF_VER_CHR)
2462 ++p;
2463
2464 if (*p != '\0'
2465 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2466 && hide)
2467 {
2468 if (hide)
2469 (*bed->elf_backend_hide_symbol) (info, h, true);
2470 return true;
2471 }
2472 }
2473
2474 /* If we don't have a version for this symbol, see if we can find
2475 something. */
2476 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2477 {
2478 h->verinfo.vertree
2479 = bfd_find_version_for_sym (info->version_info,
2480 h->root.root.string, &hide);
2481 if (h->verinfo.vertree != NULL && hide)
2482 {
2483 (*bed->elf_backend_hide_symbol) (info, h, true);
2484 return true;
2485 }
2486 }
2487
2488 return false;
2489 }
2490
2491 /* Figure out appropriate versions for all the symbols. We may not
2492 have the version number script until we have read all of the input
2493 files, so until that point we don't know which symbols should be
2494 local. This function is called via elf_link_hash_traverse. */
2495
2496 static bool
_bfd_elf_link_assign_sym_version(struct elf_link_hash_entry * h,void * data)2497 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2498 {
2499 struct elf_info_failed *sinfo;
2500 struct bfd_link_info *info;
2501 const struct elf_backend_data *bed;
2502 struct elf_info_failed eif;
2503 char *p;
2504 bool hide;
2505
2506 sinfo = (struct elf_info_failed *) data;
2507 info = sinfo->info;
2508
2509 /* Fix the symbol flags. */
2510 eif.failed = false;
2511 eif.info = info;
2512 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2513 {
2514 if (eif.failed)
2515 sinfo->failed = true;
2516 return false;
2517 }
2518
2519 bed = get_elf_backend_data (info->output_bfd);
2520
2521 /* We only need version numbers for symbols defined in regular
2522 objects. */
2523 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2524 {
2525 /* Hide symbols defined in discarded input sections. */
2526 if ((h->root.type == bfd_link_hash_defined
2527 || h->root.type == bfd_link_hash_defweak)
2528 && discarded_section (h->root.u.def.section))
2529 (*bed->elf_backend_hide_symbol) (info, h, true);
2530 return true;
2531 }
2532
2533 hide = false;
2534 p = strchr (h->root.root.string, ELF_VER_CHR);
2535 if (p != NULL && h->verinfo.vertree == NULL)
2536 {
2537 struct bfd_elf_version_tree *t;
2538
2539 ++p;
2540 if (*p == ELF_VER_CHR)
2541 ++p;
2542
2543 /* If there is no version string, we can just return out. */
2544 if (*p == '\0')
2545 return true;
2546
2547 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2548 {
2549 sinfo->failed = true;
2550 return false;
2551 }
2552
2553 if (hide)
2554 (*bed->elf_backend_hide_symbol) (info, h, true);
2555
2556 /* If we are building an application, we need to create a
2557 version node for this version. */
2558 if (t == NULL && bfd_link_executable (info))
2559 {
2560 struct bfd_elf_version_tree **pp;
2561 int version_index;
2562
2563 /* If we aren't going to export this symbol, we don't need
2564 to worry about it. */
2565 if (h->dynindx == -1)
2566 return true;
2567
2568 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2569 sizeof *t);
2570 if (t == NULL)
2571 {
2572 sinfo->failed = true;
2573 return false;
2574 }
2575
2576 t->name = p;
2577 t->name_indx = (unsigned int) -1;
2578 t->used = true;
2579
2580 version_index = 1;
2581 /* Don't count anonymous version tag. */
2582 if (sinfo->info->version_info != NULL
2583 && sinfo->info->version_info->vernum == 0)
2584 version_index = 0;
2585 for (pp = &sinfo->info->version_info;
2586 *pp != NULL;
2587 pp = &(*pp)->next)
2588 ++version_index;
2589 t->vernum = version_index;
2590
2591 *pp = t;
2592
2593 h->verinfo.vertree = t;
2594 }
2595 else if (t == NULL)
2596 {
2597 /* We could not find the version for a symbol when
2598 generating a shared archive. Return an error. */
2599 _bfd_error_handler
2600 /* xgettext:c-format */
2601 (_("%pB: version node not found for symbol %s"),
2602 info->output_bfd, h->root.root.string);
2603 bfd_set_error (bfd_error_bad_value);
2604 sinfo->failed = true;
2605 return false;
2606 }
2607 }
2608
2609 /* If we don't have a version for this symbol, see if we can find
2610 something. */
2611 if (!hide
2612 && h->verinfo.vertree == NULL
2613 && sinfo->info->version_info != NULL)
2614 {
2615 h->verinfo.vertree
2616 = bfd_find_version_for_sym (sinfo->info->version_info,
2617 h->root.root.string, &hide);
2618 if (h->verinfo.vertree != NULL && hide)
2619 (*bed->elf_backend_hide_symbol) (info, h, true);
2620 }
2621
2622 return true;
2623 }
2624
2625 /* Read and swap the relocs from the section indicated by SHDR. This
2626 may be either a REL or a RELA section. The relocations are
2627 translated into RELA relocations and stored in INTERNAL_RELOCS,
2628 which should have already been allocated to contain enough space.
2629 The EXTERNAL_RELOCS are a buffer where the external form of the
2630 relocations should be stored.
2631
2632 Returns FALSE if something goes wrong. */
2633
2634 static bool
elf_link_read_relocs_from_section(bfd * abfd,asection * sec,Elf_Internal_Shdr * shdr,void * external_relocs,Elf_Internal_Rela * internal_relocs)2635 elf_link_read_relocs_from_section (bfd *abfd,
2636 asection *sec,
2637 Elf_Internal_Shdr *shdr,
2638 void *external_relocs,
2639 Elf_Internal_Rela *internal_relocs)
2640 {
2641 const struct elf_backend_data *bed;
2642 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2643 const bfd_byte *erela;
2644 const bfd_byte *erelaend;
2645 Elf_Internal_Rela *irela;
2646 Elf_Internal_Shdr *symtab_hdr;
2647 size_t nsyms;
2648
2649 /* Position ourselves at the start of the section. */
2650 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2651 return false;
2652
2653 /* Read the relocations. */
2654 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2655 return false;
2656
2657 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2658 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2659
2660 bed = get_elf_backend_data (abfd);
2661
2662 /* Convert the external relocations to the internal format. */
2663 if (shdr->sh_entsize == bed->s->sizeof_rel)
2664 swap_in = bed->s->swap_reloc_in;
2665 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2666 swap_in = bed->s->swap_reloca_in;
2667 else
2668 {
2669 bfd_set_error (bfd_error_wrong_format);
2670 return false;
2671 }
2672
2673 erela = (const bfd_byte *) external_relocs;
2674 /* Setting erelaend like this and comparing with <= handles case of
2675 a fuzzed object with sh_size not a multiple of sh_entsize. */
2676 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
2677 irela = internal_relocs;
2678 while (erela <= erelaend)
2679 {
2680 bfd_vma r_symndx;
2681
2682 (*swap_in) (abfd, erela, irela);
2683 r_symndx = ELF32_R_SYM (irela->r_info);
2684 if (bed->s->arch_size == 64)
2685 r_symndx >>= 24;
2686 if (nsyms > 0)
2687 {
2688 if ((size_t) r_symndx >= nsyms)
2689 {
2690 _bfd_error_handler
2691 /* xgettext:c-format */
2692 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2693 " for offset %#" PRIx64 " in section `%pA'"),
2694 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2695 (uint64_t) irela->r_offset, sec);
2696 bfd_set_error (bfd_error_bad_value);
2697 return false;
2698 }
2699 }
2700 else if (r_symndx != STN_UNDEF)
2701 {
2702 _bfd_error_handler
2703 /* xgettext:c-format */
2704 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2705 " for offset %#" PRIx64 " in section `%pA'"
2706 " when the object file has no symbol table"),
2707 abfd, (uint64_t) r_symndx,
2708 (uint64_t) irela->r_offset, sec);
2709 bfd_set_error (bfd_error_bad_value);
2710 return false;
2711 }
2712 irela += bed->s->int_rels_per_ext_rel;
2713 erela += shdr->sh_entsize;
2714 }
2715
2716 return true;
2717 }
2718
2719 /* Read and swap the relocs for a section O. They may have been
2720 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2721 not NULL, they are used as buffers to read into. They are known to
2722 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2723 the return value is allocated using either malloc or bfd_alloc,
2724 according to the KEEP_MEMORY argument. If O has two relocation
2725 sections (both REL and RELA relocations), then the REL_HDR
2726 relocations will appear first in INTERNAL_RELOCS, followed by the
2727 RELA_HDR relocations. If INFO isn't NULL and KEEP_MEMORY is true,
2728 update cache_size. */
2729
2730 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)2731 _bfd_elf_link_info_read_relocs (bfd *abfd,
2732 struct bfd_link_info *info,
2733 asection *o,
2734 void *external_relocs,
2735 Elf_Internal_Rela *internal_relocs,
2736 bool keep_memory)
2737 {
2738 void *alloc1 = NULL;
2739 Elf_Internal_Rela *alloc2 = NULL;
2740 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2741 struct bfd_elf_section_data *esdo = elf_section_data (o);
2742 Elf_Internal_Rela *internal_rela_relocs;
2743
2744 if (esdo->relocs != NULL)
2745 return esdo->relocs;
2746
2747 if (o->reloc_count == 0)
2748 return NULL;
2749
2750 if (internal_relocs == NULL)
2751 {
2752 bfd_size_type size;
2753
2754 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2755 if (keep_memory)
2756 {
2757 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2758 if (info)
2759 info->cache_size += size;
2760 }
2761 else
2762 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2763 if (internal_relocs == NULL)
2764 goto error_return;
2765 }
2766
2767 if (external_relocs == NULL)
2768 {
2769 bfd_size_type size = 0;
2770
2771 if (esdo->rel.hdr)
2772 size += esdo->rel.hdr->sh_size;
2773 if (esdo->rela.hdr)
2774 size += esdo->rela.hdr->sh_size;
2775
2776 alloc1 = bfd_malloc (size);
2777 if (alloc1 == NULL)
2778 goto error_return;
2779 external_relocs = alloc1;
2780 }
2781
2782 internal_rela_relocs = internal_relocs;
2783 if (esdo->rel.hdr)
2784 {
2785 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2786 external_relocs,
2787 internal_relocs))
2788 goto error_return;
2789 external_relocs = (((bfd_byte *) external_relocs)
2790 + esdo->rel.hdr->sh_size);
2791 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2792 * bed->s->int_rels_per_ext_rel);
2793 }
2794
2795 if (esdo->rela.hdr
2796 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2797 external_relocs,
2798 internal_rela_relocs)))
2799 goto error_return;
2800
2801 /* Cache the results for next time, if we can. */
2802 if (keep_memory)
2803 esdo->relocs = internal_relocs;
2804
2805 free (alloc1);
2806
2807 /* Don't free alloc2, since if it was allocated we are passing it
2808 back (under the name of internal_relocs). */
2809
2810 return internal_relocs;
2811
2812 error_return:
2813 free (alloc1);
2814 if (alloc2 != NULL)
2815 {
2816 if (keep_memory)
2817 bfd_release (abfd, alloc2);
2818 else
2819 free (alloc2);
2820 }
2821 return NULL;
2822 }
2823
2824 /* This is similar to _bfd_elf_link_info_read_relocs, except for that
2825 NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
2826 struct bfd_link_info. */
2827
2828 Elf_Internal_Rela *
_bfd_elf_link_read_relocs(bfd * abfd,asection * o,void * external_relocs,Elf_Internal_Rela * internal_relocs,bool keep_memory)2829 _bfd_elf_link_read_relocs (bfd *abfd,
2830 asection *o,
2831 void *external_relocs,
2832 Elf_Internal_Rela *internal_relocs,
2833 bool keep_memory)
2834 {
2835 return _bfd_elf_link_info_read_relocs (abfd, NULL, o, external_relocs,
2836 internal_relocs, keep_memory);
2837
2838 }
2839
2840 /* Compute the size of, and allocate space for, REL_HDR which is the
2841 section header for a section containing relocations for O. */
2842
2843 static bool
_bfd_elf_link_size_reloc_section(bfd * abfd,struct bfd_elf_section_reloc_data * reldata)2844 _bfd_elf_link_size_reloc_section (bfd *abfd,
2845 struct bfd_elf_section_reloc_data *reldata)
2846 {
2847 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2848
2849 /* That allows us to calculate the size of the section. */
2850 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2851
2852 /* The contents field must last into write_object_contents, so we
2853 allocate it with bfd_alloc rather than malloc. Also since we
2854 cannot be sure that the contents will actually be filled in,
2855 we zero the allocated space. */
2856 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2857 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2858 return false;
2859
2860 if (reldata->hashes == NULL && reldata->count)
2861 {
2862 struct elf_link_hash_entry **p;
2863
2864 p = ((struct elf_link_hash_entry **)
2865 bfd_zmalloc (reldata->count * sizeof (*p)));
2866 if (p == NULL)
2867 return false;
2868
2869 reldata->hashes = p;
2870 }
2871
2872 return true;
2873 }
2874
2875 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2876 originated from the section given by INPUT_REL_HDR) to the
2877 OUTPUT_BFD. */
2878
2879 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)2880 _bfd_elf_link_output_relocs (bfd *output_bfd,
2881 asection *input_section,
2882 Elf_Internal_Shdr *input_rel_hdr,
2883 Elf_Internal_Rela *internal_relocs,
2884 struct elf_link_hash_entry **rel_hash
2885 ATTRIBUTE_UNUSED)
2886 {
2887 Elf_Internal_Rela *irela;
2888 Elf_Internal_Rela *irelaend;
2889 bfd_byte *erel;
2890 struct bfd_elf_section_reloc_data *output_reldata;
2891 asection *output_section;
2892 const struct elf_backend_data *bed;
2893 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2894 struct bfd_elf_section_data *esdo;
2895
2896 output_section = input_section->output_section;
2897
2898 bed = get_elf_backend_data (output_bfd);
2899 esdo = elf_section_data (output_section);
2900 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2901 {
2902 output_reldata = &esdo->rel;
2903 swap_out = bed->s->swap_reloc_out;
2904 }
2905 else if (esdo->rela.hdr
2906 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2907 {
2908 output_reldata = &esdo->rela;
2909 swap_out = bed->s->swap_reloca_out;
2910 }
2911 else
2912 {
2913 _bfd_error_handler
2914 /* xgettext:c-format */
2915 (_("%pB: relocation size mismatch in %pB section %pA"),
2916 output_bfd, input_section->owner, input_section);
2917 bfd_set_error (bfd_error_wrong_format);
2918 return false;
2919 }
2920
2921 erel = output_reldata->hdr->contents;
2922 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2923 irela = internal_relocs;
2924 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2925 * bed->s->int_rels_per_ext_rel);
2926 while (irela < irelaend)
2927 {
2928 (*swap_out) (output_bfd, irela, erel);
2929 irela += bed->s->int_rels_per_ext_rel;
2930 erel += input_rel_hdr->sh_entsize;
2931 }
2932
2933 /* Bump the counter, so that we know where to add the next set of
2934 relocations. */
2935 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2936
2937 return true;
2938 }
2939
2940 /* Make weak undefined symbols in PIE dynamic. */
2941
2942 bool
_bfd_elf_link_hash_fixup_symbol(struct bfd_link_info * info,struct elf_link_hash_entry * h)2943 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2944 struct elf_link_hash_entry *h)
2945 {
2946 if (bfd_link_pie (info)
2947 && h->dynindx == -1
2948 && h->root.type == bfd_link_hash_undefweak)
2949 return bfd_elf_link_record_dynamic_symbol (info, h);
2950
2951 return true;
2952 }
2953
2954 /* Fix up the flags for a symbol. This handles various cases which
2955 can only be fixed after all the input files are seen. This is
2956 currently called by both adjust_dynamic_symbol and
2957 assign_sym_version, which is unnecessary but perhaps more robust in
2958 the face of future changes. */
2959
2960 static bool
_bfd_elf_fix_symbol_flags(struct elf_link_hash_entry * h,struct elf_info_failed * eif)2961 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2962 struct elf_info_failed *eif)
2963 {
2964 const struct elf_backend_data *bed;
2965
2966 /* If this symbol was mentioned in a non-ELF file, try to set
2967 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2968 permit a non-ELF file to correctly refer to a symbol defined in
2969 an ELF dynamic object. */
2970 if (h->non_elf)
2971 {
2972 while (h->root.type == bfd_link_hash_indirect)
2973 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2974
2975 if (h->root.type != bfd_link_hash_defined
2976 && h->root.type != bfd_link_hash_defweak)
2977 {
2978 h->ref_regular = 1;
2979 h->ref_regular_nonweak = 1;
2980 }
2981 else
2982 {
2983 if (h->root.u.def.section->owner != NULL
2984 && (bfd_get_flavour (h->root.u.def.section->owner)
2985 == bfd_target_elf_flavour))
2986 {
2987 h->ref_regular = 1;
2988 h->ref_regular_nonweak = 1;
2989 }
2990 else
2991 h->def_regular = 1;
2992 }
2993
2994 if (h->dynindx == -1
2995 && (h->def_dynamic
2996 || h->ref_dynamic))
2997 {
2998 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2999 {
3000 eif->failed = true;
3001 return false;
3002 }
3003 }
3004 }
3005 else
3006 {
3007 /* Unfortunately, NON_ELF is only correct if the symbol
3008 was first seen in a non-ELF file. Fortunately, if the symbol
3009 was first seen in an ELF file, we're probably OK unless the
3010 symbol was defined in a non-ELF file. Catch that case here.
3011 FIXME: We're still in trouble if the symbol was first seen in
3012 a dynamic object, and then later in a non-ELF regular object. */
3013 if ((h->root.type == bfd_link_hash_defined
3014 || h->root.type == bfd_link_hash_defweak)
3015 && !h->def_regular
3016 && (h->root.u.def.section->owner != NULL
3017 ? (bfd_get_flavour (h->root.u.def.section->owner)
3018 != bfd_target_elf_flavour)
3019 : (bfd_is_abs_section (h->root.u.def.section)
3020 && !h->def_dynamic)))
3021 h->def_regular = 1;
3022 }
3023
3024 /* Backend specific symbol fixup. */
3025 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
3026 if (bed->elf_backend_fixup_symbol
3027 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
3028 return false;
3029
3030 /* If this is a final link, and the symbol was defined as a common
3031 symbol in a regular object file, and there was no definition in
3032 any dynamic object, then the linker will have allocated space for
3033 the symbol in a common section but the DEF_REGULAR
3034 flag will not have been set. */
3035 if (h->root.type == bfd_link_hash_defined
3036 && !h->def_regular
3037 && h->ref_regular
3038 && !h->def_dynamic
3039 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
3040 h->def_regular = 1;
3041
3042 /* Symbols defined in discarded sections shouldn't be dynamic. */
3043 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
3044 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3045
3046 /* If a weak undefined symbol has non-default visibility, we also
3047 hide it from the dynamic linker. */
3048 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3049 && h->root.type == bfd_link_hash_undefweak)
3050 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3051
3052 /* A hidden versioned symbol in executable should be forced local if
3053 it is is locally defined, not referenced by shared library and not
3054 exported. */
3055 else if (bfd_link_executable (eif->info)
3056 && h->versioned == versioned_hidden
3057 && !eif->info->export_dynamic
3058 && !h->dynamic
3059 && !h->ref_dynamic
3060 && h->def_regular)
3061 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3062
3063 /* If -Bsymbolic was used (which means to bind references to global
3064 symbols to the definition within the shared object), and this
3065 symbol was defined in a regular object, then it actually doesn't
3066 need a PLT entry. Likewise, if the symbol has non-default
3067 visibility. If the symbol has hidden or internal visibility, we
3068 will force it local. */
3069 else if (h->needs_plt
3070 && bfd_link_pic (eif->info)
3071 && is_elf_hash_table (eif->info->hash)
3072 && (SYMBOLIC_BIND (eif->info, h)
3073 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3074 && h->def_regular)
3075 {
3076 bool force_local;
3077
3078 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3079 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
3080 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
3081 }
3082
3083 /* If this is a weak defined symbol in a dynamic object, and we know
3084 the real definition in the dynamic object, copy interesting flags
3085 over to the real definition. */
3086 if (h->is_weakalias)
3087 {
3088 struct elf_link_hash_entry *def = weakdef (h);
3089 while (def->root.type == bfd_link_hash_indirect)
3090 def = (struct elf_link_hash_entry *) def->root.u.i.link;
3091
3092 /* If the real definition is defined by a regular object file,
3093 don't do anything special. See the longer description in
3094 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
3095 bfd_link_hash_defined as it was when put on the alias list
3096 then it must have originally been a versioned symbol (for
3097 which a non-versioned indirect symbol is created) and later
3098 a definition for the non-versioned symbol is found. In that
3099 case the indirection is flipped with the versioned symbol
3100 becoming an indirect pointing at the non-versioned symbol.
3101 Thus, not an alias any more. */
3102 if (def->def_regular
3103 || def->root.type != bfd_link_hash_defined)
3104 {
3105 h = def;
3106 while ((h = h->u.alias) != def)
3107 h->is_weakalias = 0;
3108 }
3109 else
3110 {
3111 while (h->root.type == bfd_link_hash_indirect)
3112 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3113 BFD_ASSERT (h->root.type == bfd_link_hash_defined
3114 || h->root.type == bfd_link_hash_defweak);
3115 BFD_ASSERT (def->def_dynamic);
3116 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
3117 }
3118 }
3119
3120 return true;
3121 }
3122
3123 /* Make the backend pick a good value for a dynamic symbol. This is
3124 called via elf_link_hash_traverse, and also calls itself
3125 recursively. */
3126
3127 static bool
_bfd_elf_adjust_dynamic_symbol(struct elf_link_hash_entry * h,void * data)3128 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
3129 {
3130 struct elf_info_failed *eif = (struct elf_info_failed *) data;
3131 struct elf_link_hash_table *htab;
3132 const struct elf_backend_data *bed;
3133
3134 if (! is_elf_hash_table (eif->info->hash))
3135 return false;
3136
3137 /* Ignore indirect symbols. These are added by the versioning code. */
3138 if (h->root.type == bfd_link_hash_indirect)
3139 return true;
3140
3141 /* Fix the symbol flags. */
3142 if (! _bfd_elf_fix_symbol_flags (h, eif))
3143 return false;
3144
3145 htab = elf_hash_table (eif->info);
3146 bed = get_elf_backend_data (htab->dynobj);
3147
3148 if (h->root.type == bfd_link_hash_undefweak)
3149 {
3150 if (eif->info->dynamic_undefined_weak == 0)
3151 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
3152 else if (eif->info->dynamic_undefined_weak > 0
3153 && h->ref_regular
3154 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3155 && !bfd_hide_sym_by_version (eif->info->version_info,
3156 h->root.root.string))
3157 {
3158 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
3159 {
3160 eif->failed = true;
3161 return false;
3162 }
3163 }
3164 }
3165
3166 /* If this symbol does not require a PLT entry, and it is not
3167 defined by a dynamic object, or is not referenced by a regular
3168 object, ignore it. We do have to handle a weak defined symbol,
3169 even if no regular object refers to it, if we decided to add it
3170 to the dynamic symbol table. FIXME: Do we normally need to worry
3171 about symbols which are defined by one dynamic object and
3172 referenced by another one? */
3173 if (!h->needs_plt
3174 && h->type != STT_GNU_IFUNC
3175 && (h->def_regular
3176 || !h->def_dynamic
3177 || (!h->ref_regular
3178 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3179 {
3180 h->plt = elf_hash_table (eif->info)->init_plt_offset;
3181 return true;
3182 }
3183
3184 /* If we've already adjusted this symbol, don't do it again. This
3185 can happen via a recursive call. */
3186 if (h->dynamic_adjusted)
3187 return true;
3188
3189 /* Don't look at this symbol again. Note that we must set this
3190 after checking the above conditions, because we may look at a
3191 symbol once, decide not to do anything, and then get called
3192 recursively later after REF_REGULAR is set below. */
3193 h->dynamic_adjusted = 1;
3194
3195 /* If this is a weak definition, and we know a real definition, and
3196 the real symbol is not itself defined by a regular object file,
3197 then get a good value for the real definition. We handle the
3198 real symbol first, for the convenience of the backend routine.
3199
3200 Note that there is a confusing case here. If the real definition
3201 is defined by a regular object file, we don't get the real symbol
3202 from the dynamic object, but we do get the weak symbol. If the
3203 processor backend uses a COPY reloc, then if some routine in the
3204 dynamic object changes the real symbol, we will not see that
3205 change in the corresponding weak symbol. This is the way other
3206 ELF linkers work as well, and seems to be a result of the shared
3207 library model.
3208
3209 I will clarify this issue. Most SVR4 shared libraries define the
3210 variable _timezone and define timezone as a weak synonym. The
3211 tzset call changes _timezone. If you write
3212 extern int timezone;
3213 int _timezone = 5;
3214 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3215 you might expect that, since timezone is a synonym for _timezone,
3216 the same number will print both times. However, if the processor
3217 backend uses a COPY reloc, then actually timezone will be copied
3218 into your process image, and, since you define _timezone
3219 yourself, _timezone will not. Thus timezone and _timezone will
3220 wind up at different memory locations. The tzset call will set
3221 _timezone, leaving timezone unchanged. */
3222
3223 if (h->is_weakalias)
3224 {
3225 struct elf_link_hash_entry *def = weakdef (h);
3226
3227 /* If we get to this point, there is an implicit reference to
3228 the alias by a regular object file via the weak symbol H. */
3229 def->ref_regular = 1;
3230
3231 /* Ensure that the backend adjust_dynamic_symbol function sees
3232 the strong alias before H by recursively calling ourselves. */
3233 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3234 return false;
3235 }
3236
3237 /* If a symbol has no type and no size and does not require a PLT
3238 entry, then we are probably about to do the wrong thing here: we
3239 are probably going to create a COPY reloc for an empty object.
3240 This case can arise when a shared object is built with assembly
3241 code, and the assembly code fails to set the symbol type. */
3242 if (h->size == 0
3243 && h->type == STT_NOTYPE
3244 && !h->needs_plt)
3245 _bfd_error_handler
3246 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3247 h->root.root.string);
3248
3249 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3250 {
3251 eif->failed = true;
3252 return false;
3253 }
3254
3255 return true;
3256 }
3257
3258 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3259 DYNBSS. */
3260
3261 bool
_bfd_elf_adjust_dynamic_copy(struct bfd_link_info * info,struct elf_link_hash_entry * h,asection * dynbss)3262 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3263 struct elf_link_hash_entry *h,
3264 asection *dynbss)
3265 {
3266 unsigned int power_of_two;
3267 bfd_vma mask;
3268 asection *sec = h->root.u.def.section;
3269
3270 /* The section alignment of the definition is the maximum alignment
3271 requirement of symbols defined in the section. Since we don't
3272 know the symbol alignment requirement, we start with the
3273 maximum alignment and check low bits of the symbol address
3274 for the minimum alignment. */
3275 power_of_two = bfd_section_alignment (sec);
3276 mask = ((bfd_vma) 1 << power_of_two) - 1;
3277 while ((h->root.u.def.value & mask) != 0)
3278 {
3279 mask >>= 1;
3280 --power_of_two;
3281 }
3282
3283 if (power_of_two > bfd_section_alignment (dynbss))
3284 {
3285 /* Adjust the section alignment if needed. */
3286 if (!bfd_set_section_alignment (dynbss, power_of_two))
3287 return false;
3288 }
3289
3290 /* We make sure that the symbol will be aligned properly. */
3291 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3292
3293 /* Define the symbol as being at this point in DYNBSS. */
3294 h->root.u.def.section = dynbss;
3295 h->root.u.def.value = dynbss->size;
3296
3297 /* Increment the size of DYNBSS to make room for the symbol. */
3298 dynbss->size += h->size;
3299
3300 /* No error if extern_protected_data is true. */
3301 if (h->protected_def
3302 && (!info->extern_protected_data
3303 || (info->extern_protected_data < 0
3304 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3305 info->callbacks->einfo
3306 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3307 h->root.root.string);
3308
3309 return true;
3310 }
3311
3312 /* Adjust all external symbols pointing into SEC_MERGE sections
3313 to reflect the object merging within the sections. */
3314
3315 static bool
_bfd_elf_link_sec_merge_syms(struct elf_link_hash_entry * h,void * data)3316 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3317 {
3318 asection *sec;
3319
3320 if ((h->root.type == bfd_link_hash_defined
3321 || h->root.type == bfd_link_hash_defweak)
3322 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3323 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3324 {
3325 bfd *output_bfd = (bfd *) data;
3326
3327 h->root.u.def.value =
3328 _bfd_merged_section_offset (output_bfd,
3329 &h->root.u.def.section,
3330 elf_section_data (sec)->sec_info,
3331 h->root.u.def.value);
3332 }
3333
3334 return true;
3335 }
3336
3337 /* Returns false if the symbol referred to by H should be considered
3338 to resolve local to the current module, and true if it should be
3339 considered to bind dynamically. */
3340
3341 bool
_bfd_elf_dynamic_symbol_p(struct elf_link_hash_entry * h,struct bfd_link_info * info,bool not_local_protected)3342 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3343 struct bfd_link_info *info,
3344 bool not_local_protected)
3345 {
3346 bool binding_stays_local_p;
3347 const struct elf_backend_data *bed;
3348 struct elf_link_hash_table *hash_table;
3349
3350 if (h == NULL)
3351 return false;
3352
3353 while (h->root.type == bfd_link_hash_indirect
3354 || h->root.type == bfd_link_hash_warning)
3355 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3356
3357 /* If it was forced local, then clearly it's not dynamic. */
3358 if (h->dynindx == -1)
3359 return false;
3360 if (h->forced_local)
3361 return false;
3362
3363 /* Identify the cases where name binding rules say that a
3364 visible symbol resolves locally. */
3365 binding_stays_local_p = (bfd_link_executable (info)
3366 || SYMBOLIC_BIND (info, h));
3367
3368 switch (ELF_ST_VISIBILITY (h->other))
3369 {
3370 case STV_INTERNAL:
3371 case STV_HIDDEN:
3372 return false;
3373
3374 case STV_PROTECTED:
3375 hash_table = elf_hash_table (info);
3376 if (!is_elf_hash_table (&hash_table->root))
3377 return false;
3378
3379 bed = get_elf_backend_data (hash_table->dynobj);
3380
3381 /* Proper resolution for function pointer equality may require
3382 that these symbols perhaps be resolved dynamically, even though
3383 we should be resolving them to the current module. */
3384 if (!not_local_protected || !bed->is_function_type (h->type))
3385 binding_stays_local_p = true;
3386 break;
3387
3388 default:
3389 break;
3390 }
3391
3392 /* If it isn't defined locally, then clearly it's dynamic. */
3393 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3394 return true;
3395
3396 /* Otherwise, the symbol is dynamic if binding rules don't tell
3397 us that it remains local. */
3398 return !binding_stays_local_p;
3399 }
3400
3401 /* Return true if the symbol referred to by H should be considered
3402 to resolve local to the current module, and false otherwise. Differs
3403 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3404 undefined symbols. The two functions are virtually identical except
3405 for the place where dynindx == -1 is tested. If that test is true,
3406 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3407 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3408 defined symbols.
3409 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3410 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3411 treatment of undefined weak symbols. For those that do not make
3412 undefined weak symbols dynamic, both functions may return false. */
3413
3414 bool
_bfd_elf_symbol_refs_local_p(struct elf_link_hash_entry * h,struct bfd_link_info * info,bool local_protected)3415 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3416 struct bfd_link_info *info,
3417 bool local_protected)
3418 {
3419 const struct elf_backend_data *bed;
3420 struct elf_link_hash_table *hash_table;
3421
3422 /* If it's a local sym, of course we resolve locally. */
3423 if (h == NULL)
3424 return true;
3425
3426 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3427 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3428 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3429 return true;
3430
3431 /* Forced local symbols resolve locally. */
3432 if (h->forced_local)
3433 return true;
3434
3435 /* Common symbols that become definitions don't get the DEF_REGULAR
3436 flag set, so test it first, and don't bail out. */
3437 if (ELF_COMMON_DEF_P (h))
3438 /* Do nothing. */;
3439 /* If we don't have a definition in a regular file, then we can't
3440 resolve locally. The sym is either undefined or dynamic. */
3441 else if (!h->def_regular)
3442 return false;
3443
3444 /* Non-dynamic symbols resolve locally. */
3445 if (h->dynindx == -1)
3446 return true;
3447
3448 /* At this point, we know the symbol is defined and dynamic. In an
3449 executable it must resolve locally, likewise when building symbolic
3450 shared libraries. */
3451 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3452 return true;
3453
3454 /* Now deal with defined dynamic symbols in shared libraries. Ones
3455 with default visibility might not resolve locally. */
3456 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3457 return false;
3458
3459 hash_table = elf_hash_table (info);
3460 if (!is_elf_hash_table (&hash_table->root))
3461 return true;
3462
3463 /* STV_PROTECTED symbols with indirect external access are local. */
3464 if (info->indirect_extern_access > 0)
3465 return true;
3466
3467 bed = get_elf_backend_data (hash_table->dynobj);
3468
3469 /* If extern_protected_data is false, STV_PROTECTED non-function
3470 symbols are local. */
3471 if ((!info->extern_protected_data
3472 || (info->extern_protected_data < 0
3473 && !bed->extern_protected_data))
3474 && !bed->is_function_type (h->type))
3475 return true;
3476
3477 /* Function pointer equality tests may require that STV_PROTECTED
3478 symbols be treated as dynamic symbols. If the address of a
3479 function not defined in an executable is set to that function's
3480 plt entry in the executable, then the address of the function in
3481 a shared library must also be the plt entry in the executable. */
3482 return local_protected;
3483 }
3484
3485 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3486 aligned. Returns the first TLS output section. */
3487
3488 struct bfd_section *
_bfd_elf_tls_setup(bfd * obfd,struct bfd_link_info * info)3489 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3490 {
3491 struct bfd_section *sec, *tls;
3492 unsigned int align = 0;
3493
3494 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3495 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3496 break;
3497 tls = sec;
3498
3499 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3500 if (sec->alignment_power > align)
3501 align = sec->alignment_power;
3502
3503 elf_hash_table (info)->tls_sec = tls;
3504
3505 /* Ensure the alignment of the first section (usually .tdata) is the largest
3506 alignment, so that the tls segment starts aligned. */
3507 if (tls != NULL)
3508 tls->alignment_power = align;
3509
3510 return tls;
3511 }
3512
3513 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3514 static bool
is_global_data_symbol_definition(bfd * abfd ATTRIBUTE_UNUSED,Elf_Internal_Sym * sym)3515 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3516 Elf_Internal_Sym *sym)
3517 {
3518 const struct elf_backend_data *bed;
3519
3520 /* Local symbols do not count, but target specific ones might. */
3521 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3522 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3523 return false;
3524
3525 bed = get_elf_backend_data (abfd);
3526 /* Function symbols do not count. */
3527 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3528 return false;
3529
3530 /* If the section is undefined, then so is the symbol. */
3531 if (sym->st_shndx == SHN_UNDEF)
3532 return false;
3533
3534 /* If the symbol is defined in the common section, then
3535 it is a common definition and so does not count. */
3536 if (bed->common_definition (sym))
3537 return false;
3538
3539 /* If the symbol is in a target specific section then we
3540 must rely upon the backend to tell us what it is. */
3541 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3542 /* FIXME - this function is not coded yet:
3543
3544 return _bfd_is_global_symbol_definition (abfd, sym);
3545
3546 Instead for now assume that the definition is not global,
3547 Even if this is wrong, at least the linker will behave
3548 in the same way that it used to do. */
3549 return false;
3550
3551 return true;
3552 }
3553
3554 /* Search the symbol table of the archive element of the archive ABFD
3555 whose archive map contains a mention of SYMDEF, and determine if
3556 the symbol is defined in this element. */
3557 static bool
elf_link_is_defined_archive_symbol(bfd * abfd,carsym * symdef)3558 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3559 {
3560 Elf_Internal_Shdr * hdr;
3561 size_t symcount;
3562 size_t extsymcount;
3563 size_t extsymoff;
3564 Elf_Internal_Sym *isymbuf;
3565 Elf_Internal_Sym *isym;
3566 Elf_Internal_Sym *isymend;
3567 bool result;
3568
3569 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset, NULL);
3570 if (abfd == NULL)
3571 return false;
3572
3573 if (! bfd_check_format (abfd, bfd_object))
3574 return false;
3575
3576 /* Select the appropriate symbol table. If we don't know if the
3577 object file is an IR object, give linker LTO plugin a chance to
3578 get the correct symbol table. */
3579 if (abfd->plugin_format == bfd_plugin_yes
3580 #if BFD_SUPPORTS_PLUGINS
3581 || (abfd->plugin_format == bfd_plugin_unknown
3582 && bfd_link_plugin_object_p (abfd))
3583 #endif
3584 )
3585 {
3586 /* Use the IR symbol table if the object has been claimed by
3587 plugin. */
3588 abfd = abfd->plugin_dummy_bfd;
3589 hdr = &elf_tdata (abfd)->symtab_hdr;
3590 }
3591 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3592 hdr = &elf_tdata (abfd)->symtab_hdr;
3593 else
3594 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3595
3596 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3597
3598 /* The sh_info field of the symtab header tells us where the
3599 external symbols start. We don't care about the local symbols. */
3600 if (elf_bad_symtab (abfd))
3601 {
3602 extsymcount = symcount;
3603 extsymoff = 0;
3604 }
3605 else
3606 {
3607 extsymcount = symcount - hdr->sh_info;
3608 extsymoff = hdr->sh_info;
3609 }
3610
3611 if (extsymcount == 0)
3612 return false;
3613
3614 /* Read in the symbol table. */
3615 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3616 NULL, NULL, NULL);
3617 if (isymbuf == NULL)
3618 return false;
3619
3620 /* Scan the symbol table looking for SYMDEF. */
3621 result = false;
3622 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3623 {
3624 const char *name;
3625
3626 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3627 isym->st_name);
3628 if (name == NULL)
3629 break;
3630
3631 if (strcmp (name, symdef->name) == 0)
3632 {
3633 result = is_global_data_symbol_definition (abfd, isym);
3634 break;
3635 }
3636 }
3637
3638 free (isymbuf);
3639
3640 return result;
3641 }
3642
3643 /* Add an entry to the .dynamic table. */
3644
3645 bool
_bfd_elf_add_dynamic_entry(struct bfd_link_info * info,bfd_vma tag,bfd_vma val)3646 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3647 bfd_vma tag,
3648 bfd_vma val)
3649 {
3650 struct elf_link_hash_table *hash_table;
3651 const struct elf_backend_data *bed;
3652 asection *s;
3653 bfd_size_type newsize;
3654 bfd_byte *newcontents;
3655 Elf_Internal_Dyn dyn;
3656
3657 hash_table = elf_hash_table (info);
3658 if (! is_elf_hash_table (&hash_table->root))
3659 return false;
3660
3661 if (tag == DT_RELA || tag == DT_REL)
3662 hash_table->dynamic_relocs = true;
3663
3664 bed = get_elf_backend_data (hash_table->dynobj);
3665 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3666 BFD_ASSERT (s != NULL);
3667
3668 newsize = s->size + bed->s->sizeof_dyn;
3669 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3670 if (newcontents == NULL)
3671 return false;
3672
3673 dyn.d_tag = tag;
3674 dyn.d_un.d_val = val;
3675 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3676
3677 s->size = newsize;
3678 s->contents = newcontents;
3679
3680 return true;
3681 }
3682
3683 /* Strip zero-sized dynamic sections. */
3684
3685 bool
_bfd_elf_strip_zero_sized_dynamic_sections(struct bfd_link_info * info)3686 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3687 {
3688 struct elf_link_hash_table *hash_table;
3689 const struct elf_backend_data *bed;
3690 asection *s, *sdynamic, **pp;
3691 asection *rela_dyn, *rel_dyn;
3692 Elf_Internal_Dyn dyn;
3693 bfd_byte *extdyn, *next;
3694 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3695 bool strip_zero_sized;
3696 bool strip_zero_sized_plt;
3697
3698 if (bfd_link_relocatable (info))
3699 return true;
3700
3701 hash_table = elf_hash_table (info);
3702 if (!is_elf_hash_table (&hash_table->root))
3703 return false;
3704
3705 if (!hash_table->dynobj)
3706 return true;
3707
3708 sdynamic= bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3709 if (!sdynamic)
3710 return true;
3711
3712 bed = get_elf_backend_data (hash_table->dynobj);
3713 swap_dyn_in = bed->s->swap_dyn_in;
3714
3715 strip_zero_sized = false;
3716 strip_zero_sized_plt = false;
3717
3718 /* Strip zero-sized dynamic sections. */
3719 rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3720 rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3721 for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3722 if (s->size == 0
3723 && (s == rela_dyn
3724 || s == rel_dyn
3725 || s == hash_table->srelplt->output_section
3726 || s == hash_table->splt->output_section))
3727 {
3728 *pp = s->next;
3729 info->output_bfd->section_count--;
3730 strip_zero_sized = true;
3731 if (s == rela_dyn)
3732 s = rela_dyn;
3733 if (s == rel_dyn)
3734 s = rel_dyn;
3735 else if (s == hash_table->splt->output_section)
3736 {
3737 s = hash_table->splt;
3738 strip_zero_sized_plt = true;
3739 }
3740 else
3741 s = hash_table->srelplt;
3742 s->flags |= SEC_EXCLUDE;
3743 s->output_section = bfd_abs_section_ptr;
3744 }
3745 else
3746 pp = &s->next;
3747
3748 if (strip_zero_sized_plt && sdynamic->size != 0)
3749 for (extdyn = sdynamic->contents;
3750 extdyn < sdynamic->contents + sdynamic->size;
3751 extdyn = next)
3752 {
3753 next = extdyn + bed->s->sizeof_dyn;
3754 swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3755 switch (dyn.d_tag)
3756 {
3757 default:
3758 break;
3759 case DT_JMPREL:
3760 case DT_PLTRELSZ:
3761 case DT_PLTREL:
3762 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3763 the procedure linkage table (the .plt section) has been
3764 removed. */
3765 memmove (extdyn, next,
3766 sdynamic->size - (next - sdynamic->contents));
3767 next = extdyn;
3768 }
3769 }
3770
3771 if (strip_zero_sized)
3772 {
3773 /* Regenerate program headers. */
3774 elf_seg_map (info->output_bfd) = NULL;
3775 return _bfd_elf_map_sections_to_segments (info->output_bfd, info,
3776 NULL);
3777 }
3778
3779 return true;
3780 }
3781
3782 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3783 1 if a DT_NEEDED tag already exists, and 0 on success. */
3784
3785 int
bfd_elf_add_dt_needed_tag(bfd * abfd,struct bfd_link_info * info)3786 bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
3787 {
3788 struct elf_link_hash_table *hash_table;
3789 size_t strindex;
3790 const char *soname;
3791
3792 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3793 return -1;
3794
3795 hash_table = elf_hash_table (info);
3796 soname = elf_dt_name (abfd);
3797 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, false);
3798 if (strindex == (size_t) -1)
3799 return -1;
3800
3801 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3802 {
3803 asection *sdyn;
3804 const struct elf_backend_data *bed;
3805 bfd_byte *extdyn;
3806
3807 bed = get_elf_backend_data (hash_table->dynobj);
3808 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3809 if (sdyn != NULL && sdyn->size != 0)
3810 for (extdyn = sdyn->contents;
3811 extdyn < sdyn->contents + sdyn->size;
3812 extdyn += bed->s->sizeof_dyn)
3813 {
3814 Elf_Internal_Dyn dyn;
3815
3816 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3817 if (dyn.d_tag == DT_NEEDED
3818 && dyn.d_un.d_val == strindex)
3819 {
3820 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3821 return 1;
3822 }
3823 }
3824 }
3825
3826 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3827 return -1;
3828
3829 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3830 return -1;
3831
3832 return 0;
3833 }
3834
3835 /* Return true if SONAME is on the needed list between NEEDED and STOP
3836 (or the end of list if STOP is NULL), and needed by a library that
3837 will be loaded. */
3838
3839 static bool
on_needed_list(const char * soname,struct bfd_link_needed_list * needed,struct bfd_link_needed_list * stop)3840 on_needed_list (const char *soname,
3841 struct bfd_link_needed_list *needed,
3842 struct bfd_link_needed_list *stop)
3843 {
3844 struct bfd_link_needed_list *look;
3845 for (look = needed; look != stop; look = look->next)
3846 if (strcmp (soname, look->name) == 0
3847 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3848 /* If needed by a library that itself is not directly
3849 needed, recursively check whether that library is
3850 indirectly needed. Since we add DT_NEEDED entries to
3851 the end of the list, library dependencies appear after
3852 the library. Therefore search prior to the current
3853 LOOK, preventing possible infinite recursion. */
3854 || on_needed_list (elf_dt_name (look->by), needed, look)))
3855 return true;
3856
3857 return false;
3858 }
3859
3860 /* Sort symbol by value, section, size, and type. */
3861 static int
elf_sort_symbol(const void * arg1,const void * arg2)3862 elf_sort_symbol (const void *arg1, const void *arg2)
3863 {
3864 const struct elf_link_hash_entry *h1;
3865 const struct elf_link_hash_entry *h2;
3866 bfd_signed_vma vdiff;
3867 int sdiff;
3868 const char *n1;
3869 const char *n2;
3870
3871 h1 = *(const struct elf_link_hash_entry **) arg1;
3872 h2 = *(const struct elf_link_hash_entry **) arg2;
3873 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3874 if (vdiff != 0)
3875 return vdiff > 0 ? 1 : -1;
3876
3877 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3878 if (sdiff != 0)
3879 return sdiff;
3880
3881 /* Sort so that sized symbols are selected over zero size symbols. */
3882 vdiff = h1->size - h2->size;
3883 if (vdiff != 0)
3884 return vdiff > 0 ? 1 : -1;
3885
3886 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3887 if (h1->type != h2->type)
3888 return h1->type - h2->type;
3889
3890 /* If symbols are properly sized and typed, and multiple strong
3891 aliases are not defined in a shared library by the user we
3892 shouldn't get here. Unfortunately linker script symbols like
3893 __bss_start sometimes match a user symbol defined at the start of
3894 .bss without proper size and type. We'd like to preference the
3895 user symbol over reserved system symbols. Sort on leading
3896 underscores. */
3897 n1 = h1->root.root.string;
3898 n2 = h2->root.root.string;
3899 while (*n1 == *n2)
3900 {
3901 if (*n1 == 0)
3902 break;
3903 ++n1;
3904 ++n2;
3905 }
3906 if (*n1 == '_')
3907 return -1;
3908 if (*n2 == '_')
3909 return 1;
3910
3911 /* Final sort on name selects user symbols like '_u' over reserved
3912 system symbols like '_Z' and also will avoid qsort instability. */
3913 return *n1 - *n2;
3914 }
3915
3916 /* This function is used to adjust offsets into .dynstr for
3917 dynamic symbols. This is called via elf_link_hash_traverse. */
3918
3919 static bool
elf_adjust_dynstr_offsets(struct elf_link_hash_entry * h,void * data)3920 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3921 {
3922 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3923
3924 if (h->dynindx != -1)
3925 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3926 return true;
3927 }
3928
3929 /* Assign string offsets in .dynstr, update all structures referencing
3930 them. */
3931
3932 static bool
elf_finalize_dynstr(bfd * output_bfd,struct bfd_link_info * info)3933 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3934 {
3935 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3936 struct elf_link_local_dynamic_entry *entry;
3937 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3938 bfd *dynobj = hash_table->dynobj;
3939 asection *sdyn;
3940 bfd_size_type size;
3941 const struct elf_backend_data *bed;
3942 bfd_byte *extdyn;
3943
3944 _bfd_elf_strtab_finalize (dynstr);
3945 size = _bfd_elf_strtab_size (dynstr);
3946
3947 /* Allow the linker to examine the dynsymtab now it's fully populated. */
3948
3949 if (info->callbacks->examine_strtab)
3950 info->callbacks->examine_strtab (dynstr);
3951
3952 bed = get_elf_backend_data (dynobj);
3953 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3954 BFD_ASSERT (sdyn != NULL);
3955
3956 /* Update all .dynamic entries referencing .dynstr strings. */
3957 for (extdyn = sdyn->contents;
3958 extdyn < PTR_ADD (sdyn->contents, sdyn->size);
3959 extdyn += bed->s->sizeof_dyn)
3960 {
3961 Elf_Internal_Dyn dyn;
3962
3963 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3964 switch (dyn.d_tag)
3965 {
3966 case DT_STRSZ:
3967 dyn.d_un.d_val = size;
3968 break;
3969 case DT_NEEDED:
3970 case DT_SONAME:
3971 case DT_RPATH:
3972 case DT_RUNPATH:
3973 case DT_FILTER:
3974 case DT_AUXILIARY:
3975 case DT_AUDIT:
3976 case DT_DEPAUDIT:
3977 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3978 break;
3979 default:
3980 continue;
3981 }
3982 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3983 }
3984
3985 /* Now update local dynamic symbols. */
3986 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3987 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3988 entry->isym.st_name);
3989
3990 /* And the rest of dynamic symbols. */
3991 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3992
3993 /* Adjust version definitions. */
3994 if (elf_tdata (output_bfd)->cverdefs)
3995 {
3996 asection *s;
3997 bfd_byte *p;
3998 size_t i;
3999 Elf_Internal_Verdef def;
4000 Elf_Internal_Verdaux defaux;
4001
4002 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
4003 p = s->contents;
4004 do
4005 {
4006 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
4007 &def);
4008 p += sizeof (Elf_External_Verdef);
4009 if (def.vd_aux != sizeof (Elf_External_Verdef))
4010 continue;
4011 for (i = 0; i < def.vd_cnt; ++i)
4012 {
4013 _bfd_elf_swap_verdaux_in (output_bfd,
4014 (Elf_External_Verdaux *) p, &defaux);
4015 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
4016 defaux.vda_name);
4017 _bfd_elf_swap_verdaux_out (output_bfd,
4018 &defaux, (Elf_External_Verdaux *) p);
4019 p += sizeof (Elf_External_Verdaux);
4020 }
4021 }
4022 while (def.vd_next);
4023 }
4024
4025 /* Adjust version references. */
4026 if (elf_tdata (output_bfd)->verref)
4027 {
4028 asection *s;
4029 bfd_byte *p;
4030 size_t i;
4031 Elf_Internal_Verneed need;
4032 Elf_Internal_Vernaux needaux;
4033
4034 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
4035 p = s->contents;
4036 do
4037 {
4038 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
4039 &need);
4040 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
4041 _bfd_elf_swap_verneed_out (output_bfd, &need,
4042 (Elf_External_Verneed *) p);
4043 p += sizeof (Elf_External_Verneed);
4044 for (i = 0; i < need.vn_cnt; ++i)
4045 {
4046 _bfd_elf_swap_vernaux_in (output_bfd,
4047 (Elf_External_Vernaux *) p, &needaux);
4048 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
4049 needaux.vna_name);
4050 _bfd_elf_swap_vernaux_out (output_bfd,
4051 &needaux,
4052 (Elf_External_Vernaux *) p);
4053 p += sizeof (Elf_External_Vernaux);
4054 }
4055 }
4056 while (need.vn_next);
4057 }
4058
4059 return true;
4060 }
4061
4062 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4063 The default is to only match when the INPUT and OUTPUT are exactly
4064 the same target. */
4065
4066 bool
_bfd_elf_default_relocs_compatible(const bfd_target * input,const bfd_target * output)4067 _bfd_elf_default_relocs_compatible (const bfd_target *input,
4068 const bfd_target *output)
4069 {
4070 return input == output;
4071 }
4072
4073 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4074 This version is used when different targets for the same architecture
4075 are virtually identical. */
4076
4077 bool
_bfd_elf_relocs_compatible(const bfd_target * input,const bfd_target * output)4078 _bfd_elf_relocs_compatible (const bfd_target *input,
4079 const bfd_target *output)
4080 {
4081 const struct elf_backend_data *obed, *ibed;
4082
4083 if (input == output)
4084 return true;
4085
4086 ibed = xvec_get_elf_backend_data (input);
4087 obed = xvec_get_elf_backend_data (output);
4088
4089 if (ibed->arch != obed->arch)
4090 return false;
4091
4092 /* If both backends are using this function, deem them compatible. */
4093 return ibed->relocs_compatible == obed->relocs_compatible;
4094 }
4095
4096 /* Make a special call to the linker "notice" function to tell it that
4097 we are about to handle an as-needed lib, or have finished
4098 processing the lib. */
4099
4100 bool
_bfd_elf_notice_as_needed(bfd * ibfd,struct bfd_link_info * info,enum notice_asneeded_action act)4101 _bfd_elf_notice_as_needed (bfd *ibfd,
4102 struct bfd_link_info *info,
4103 enum notice_asneeded_action act)
4104 {
4105 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
4106 }
4107
4108 /* Call ACTION on each relocation in an ELF object file. */
4109
4110 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 *))4111 _bfd_elf_link_iterate_on_relocs
4112 (bfd *abfd, struct bfd_link_info *info,
4113 bool (*action) (bfd *, struct bfd_link_info *, asection *,
4114 const Elf_Internal_Rela *))
4115 {
4116 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4117 struct elf_link_hash_table *htab = elf_hash_table (info);
4118
4119 /* If this object is the same format as the output object, and it is
4120 not a shared library, then let the backend look through the
4121 relocs.
4122
4123 This is required to build global offset table entries and to
4124 arrange for dynamic relocs. It is not required for the
4125 particular common case of linking non PIC code, even when linking
4126 against shared libraries, but unfortunately there is no way of
4127 knowing whether an object file has been compiled PIC or not.
4128 Looking through the relocs is not particularly time consuming.
4129 The problem is that we must either (1) keep the relocs in memory,
4130 which causes the linker to require additional runtime memory or
4131 (2) read the relocs twice from the input file, which wastes time.
4132 This would be a good case for using mmap.
4133
4134 I have no idea how to handle linking PIC code into a file of a
4135 different format. It probably can't be done. */
4136 if ((abfd->flags & DYNAMIC) == 0
4137 && is_elf_hash_table (&htab->root)
4138 && elf_object_id (abfd) == elf_hash_table_id (htab)
4139 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4140 {
4141 asection *o;
4142
4143 for (o = abfd->sections; o != NULL; o = o->next)
4144 {
4145 Elf_Internal_Rela *internal_relocs;
4146 bool ok;
4147
4148 /* Don't check relocations in excluded sections. Don't do
4149 anything special with non-loaded, non-alloced sections.
4150 In particular, any relocs in such sections should not
4151 affect GOT and PLT reference counting (ie. we don't
4152 allow them to create GOT or PLT entries), there's no
4153 possibility or desire to optimize TLS relocs, and
4154 there's not much point in propagating relocs to shared
4155 libs that the dynamic linker won't relocate. */
4156 if ((o->flags & SEC_ALLOC) == 0
4157 || (o->flags & SEC_RELOC) == 0
4158 || (o->flags & SEC_EXCLUDE) != 0
4159 || o->reloc_count == 0
4160 || ((info->strip == strip_all || info->strip == strip_debugger)
4161 && (o->flags & SEC_DEBUGGING) != 0)
4162 || bfd_is_abs_section (o->output_section))
4163 continue;
4164
4165 internal_relocs = _bfd_elf_link_info_read_relocs (abfd, info,
4166 o, NULL,
4167 NULL,
4168 _bfd_link_keep_memory (info));
4169 if (internal_relocs == NULL)
4170 return false;
4171
4172 ok = action (abfd, info, o, internal_relocs);
4173
4174 if (elf_section_data (o)->relocs != internal_relocs)
4175 free (internal_relocs);
4176
4177 if (! ok)
4178 return false;
4179 }
4180 }
4181
4182 return true;
4183 }
4184
4185 /* Check relocations in an ELF object file. This is called after
4186 all input files have been opened. */
4187
4188 bool
_bfd_elf_link_check_relocs(bfd * abfd,struct bfd_link_info * info)4189 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
4190 {
4191 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4192 if (bed->check_relocs != NULL)
4193 return _bfd_elf_link_iterate_on_relocs (abfd, info,
4194 bed->check_relocs);
4195 return true;
4196 }
4197
4198 /* Add symbols from an ELF object file to the linker hash table. */
4199
4200 static bool
elf_link_add_object_symbols(bfd * abfd,struct bfd_link_info * info)4201 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4202 {
4203 Elf_Internal_Ehdr *ehdr;
4204 Elf_Internal_Shdr *hdr;
4205 size_t symcount;
4206 size_t extsymcount;
4207 size_t extsymoff;
4208 struct elf_link_hash_entry **sym_hash;
4209 bool dynamic;
4210 Elf_External_Versym *extversym = NULL;
4211 Elf_External_Versym *extversym_end = NULL;
4212 Elf_External_Versym *ever;
4213 struct elf_link_hash_entry *weaks;
4214 struct elf_link_hash_entry **nondeflt_vers = NULL;
4215 size_t nondeflt_vers_cnt = 0;
4216 Elf_Internal_Sym *isymbuf = NULL;
4217 Elf_Internal_Sym *isym;
4218 Elf_Internal_Sym *isymend;
4219 const struct elf_backend_data *bed;
4220 bool add_needed;
4221 struct elf_link_hash_table *htab;
4222 void *alloc_mark = NULL;
4223 struct bfd_hash_entry **old_table = NULL;
4224 unsigned int old_size = 0;
4225 unsigned int old_count = 0;
4226 void *old_tab = NULL;
4227 void *old_ent;
4228 struct bfd_link_hash_entry *old_undefs = NULL;
4229 struct bfd_link_hash_entry *old_undefs_tail = NULL;
4230 void *old_strtab = NULL;
4231 size_t tabsize = 0;
4232 asection *s;
4233 bool just_syms;
4234
4235 htab = elf_hash_table (info);
4236 bed = get_elf_backend_data (abfd);
4237
4238 if ((abfd->flags & DYNAMIC) == 0)
4239 dynamic = false;
4240 else
4241 {
4242 dynamic = true;
4243
4244 /* You can't use -r against a dynamic object. Also, there's no
4245 hope of using a dynamic object which does not exactly match
4246 the format of the output file. */
4247 if (bfd_link_relocatable (info)
4248 || !is_elf_hash_table (&htab->root)
4249 || info->output_bfd->xvec != abfd->xvec)
4250 {
4251 if (bfd_link_relocatable (info))
4252 bfd_set_error (bfd_error_invalid_operation);
4253 else
4254 bfd_set_error (bfd_error_wrong_format);
4255 goto error_return;
4256 }
4257 }
4258
4259 ehdr = elf_elfheader (abfd);
4260 if (info->warn_alternate_em
4261 && bed->elf_machine_code != ehdr->e_machine
4262 && ((bed->elf_machine_alt1 != 0
4263 && ehdr->e_machine == bed->elf_machine_alt1)
4264 || (bed->elf_machine_alt2 != 0
4265 && ehdr->e_machine == bed->elf_machine_alt2)))
4266 _bfd_error_handler
4267 /* xgettext:c-format */
4268 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4269 ehdr->e_machine, abfd, bed->elf_machine_code);
4270
4271 /* As a GNU extension, any input sections which are named
4272 .gnu.warning.SYMBOL are treated as warning symbols for the given
4273 symbol. This differs from .gnu.warning sections, which generate
4274 warnings when they are included in an output file. */
4275 /* PR 12761: Also generate this warning when building shared libraries. */
4276 for (s = abfd->sections; s != NULL; s = s->next)
4277 {
4278 const char *name;
4279
4280 name = bfd_section_name (s);
4281 if (startswith (name, ".gnu.warning."))
4282 {
4283 char *msg;
4284 bfd_size_type sz;
4285
4286 name += sizeof ".gnu.warning." - 1;
4287
4288 /* If this is a shared object, then look up the symbol
4289 in the hash table. If it is there, and it is already
4290 been defined, then we will not be using the entry
4291 from this shared object, so we don't need to warn.
4292 FIXME: If we see the definition in a regular object
4293 later on, we will warn, but we shouldn't. The only
4294 fix is to keep track of what warnings we are supposed
4295 to emit, and then handle them all at the end of the
4296 link. */
4297 if (dynamic)
4298 {
4299 struct elf_link_hash_entry *h;
4300
4301 h = elf_link_hash_lookup (htab, name, false, false, true);
4302
4303 /* FIXME: What about bfd_link_hash_common? */
4304 if (h != NULL
4305 && (h->root.type == bfd_link_hash_defined
4306 || h->root.type == bfd_link_hash_defweak))
4307 continue;
4308 }
4309
4310 sz = s->size;
4311 msg = (char *) bfd_alloc (abfd, sz + 1);
4312 if (msg == NULL)
4313 goto error_return;
4314
4315 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4316 goto error_return;
4317
4318 msg[sz] = '\0';
4319
4320 if (! (_bfd_generic_link_add_one_symbol
4321 (info, abfd, name, BSF_WARNING, s, 0, msg,
4322 false, bed->collect, NULL)))
4323 goto error_return;
4324
4325 if (bfd_link_executable (info))
4326 {
4327 /* Clobber the section size so that the warning does
4328 not get copied into the output file. */
4329 s->size = 0;
4330
4331 /* Also set SEC_EXCLUDE, so that symbols defined in
4332 the warning section don't get copied to the output. */
4333 s->flags |= SEC_EXCLUDE;
4334 }
4335 }
4336 }
4337
4338 just_syms = ((s = abfd->sections) != NULL
4339 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4340
4341 add_needed = true;
4342 if (! dynamic)
4343 {
4344 /* If we are creating a shared library, create all the dynamic
4345 sections immediately. We need to attach them to something,
4346 so we attach them to this BFD, provided it is the right
4347 format and is not from ld --just-symbols. Always create the
4348 dynamic sections for -E/--dynamic-list. FIXME: If there
4349 are no input BFD's of the same format as the output, we can't
4350 make a shared library. */
4351 if (!just_syms
4352 && (bfd_link_pic (info)
4353 || (!bfd_link_relocatable (info)
4354 && info->nointerp
4355 && (info->export_dynamic || info->dynamic)))
4356 && is_elf_hash_table (&htab->root)
4357 && info->output_bfd->xvec == abfd->xvec
4358 && !htab->dynamic_sections_created)
4359 {
4360 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4361 goto error_return;
4362 }
4363 }
4364 else if (!is_elf_hash_table (&htab->root))
4365 goto error_return;
4366 else
4367 {
4368 const char *soname = NULL;
4369 char *audit = NULL;
4370 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4371 const Elf_Internal_Phdr *phdr;
4372 struct elf_link_loaded_list *loaded_lib;
4373
4374 /* ld --just-symbols and dynamic objects don't mix very well.
4375 ld shouldn't allow it. */
4376 if (just_syms)
4377 abort ();
4378
4379 /* If this dynamic lib was specified on the command line with
4380 --as-needed in effect, then we don't want to add a DT_NEEDED
4381 tag unless the lib is actually used. Similary for libs brought
4382 in by another lib's DT_NEEDED. When --no-add-needed is used
4383 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4384 any dynamic library in DT_NEEDED tags in the dynamic lib at
4385 all. */
4386 add_needed = (elf_dyn_lib_class (abfd)
4387 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4388 | DYN_NO_NEEDED)) == 0;
4389
4390 s = bfd_get_section_by_name (abfd, ".dynamic");
4391 if (s != NULL && s->size != 0)
4392 {
4393 bfd_byte *dynbuf;
4394 bfd_byte *extdyn;
4395 unsigned int elfsec;
4396 unsigned long shlink;
4397
4398 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4399 {
4400 error_free_dyn:
4401 free (dynbuf);
4402 goto error_return;
4403 }
4404
4405 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4406 if (elfsec == SHN_BAD)
4407 goto error_free_dyn;
4408 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4409
4410 for (extdyn = dynbuf;
4411 extdyn <= dynbuf + s->size - bed->s->sizeof_dyn;
4412 extdyn += bed->s->sizeof_dyn)
4413 {
4414 Elf_Internal_Dyn dyn;
4415
4416 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4417 if (dyn.d_tag == DT_SONAME)
4418 {
4419 unsigned int tagv = dyn.d_un.d_val;
4420 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4421 if (soname == NULL)
4422 goto error_free_dyn;
4423 }
4424 if (dyn.d_tag == DT_NEEDED)
4425 {
4426 struct bfd_link_needed_list *n, **pn;
4427 char *fnm, *anm;
4428 unsigned int tagv = dyn.d_un.d_val;
4429 size_t amt = sizeof (struct bfd_link_needed_list);
4430
4431 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4432 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4433 if (n == NULL || fnm == NULL)
4434 goto error_free_dyn;
4435 amt = strlen (fnm) + 1;
4436 anm = (char *) bfd_alloc (abfd, amt);
4437 if (anm == NULL)
4438 goto error_free_dyn;
4439 memcpy (anm, fnm, amt);
4440 n->name = anm;
4441 n->by = abfd;
4442 n->next = NULL;
4443 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4444 ;
4445 *pn = n;
4446 }
4447 if (dyn.d_tag == DT_RUNPATH)
4448 {
4449 struct bfd_link_needed_list *n, **pn;
4450 char *fnm, *anm;
4451 unsigned int tagv = dyn.d_un.d_val;
4452 size_t amt = sizeof (struct bfd_link_needed_list);
4453
4454 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4455 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4456 if (n == NULL || fnm == NULL)
4457 goto error_free_dyn;
4458 amt = strlen (fnm) + 1;
4459 anm = (char *) bfd_alloc (abfd, amt);
4460 if (anm == NULL)
4461 goto error_free_dyn;
4462 memcpy (anm, fnm, amt);
4463 n->name = anm;
4464 n->by = abfd;
4465 n->next = NULL;
4466 for (pn = & runpath;
4467 *pn != NULL;
4468 pn = &(*pn)->next)
4469 ;
4470 *pn = n;
4471 }
4472 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4473 if (!runpath && dyn.d_tag == DT_RPATH)
4474 {
4475 struct bfd_link_needed_list *n, **pn;
4476 char *fnm, *anm;
4477 unsigned int tagv = dyn.d_un.d_val;
4478 size_t amt = sizeof (struct bfd_link_needed_list);
4479
4480 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4481 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4482 if (n == NULL || fnm == NULL)
4483 goto error_free_dyn;
4484 amt = strlen (fnm) + 1;
4485 anm = (char *) bfd_alloc (abfd, amt);
4486 if (anm == NULL)
4487 goto error_free_dyn;
4488 memcpy (anm, fnm, amt);
4489 n->name = anm;
4490 n->by = abfd;
4491 n->next = NULL;
4492 for (pn = & rpath;
4493 *pn != NULL;
4494 pn = &(*pn)->next)
4495 ;
4496 *pn = n;
4497 }
4498 if (dyn.d_tag == DT_AUDIT)
4499 {
4500 unsigned int tagv = dyn.d_un.d_val;
4501 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4502 }
4503 if (dyn.d_tag == DT_FLAGS_1)
4504 elf_tdata (abfd)->is_pie = (dyn.d_un.d_val & DF_1_PIE) != 0;
4505 }
4506
4507 free (dynbuf);
4508 }
4509
4510 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4511 frees all more recently bfd_alloc'd blocks as well. */
4512 if (runpath)
4513 rpath = runpath;
4514
4515 if (rpath)
4516 {
4517 struct bfd_link_needed_list **pn;
4518 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4519 ;
4520 *pn = rpath;
4521 }
4522
4523 /* If we have a PT_GNU_RELRO program header, mark as read-only
4524 all sections contained fully therein. This makes relro
4525 shared library sections appear as they will at run-time. */
4526 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4527 while (phdr-- > elf_tdata (abfd)->phdr)
4528 if (phdr->p_type == PT_GNU_RELRO)
4529 {
4530 for (s = abfd->sections; s != NULL; s = s->next)
4531 {
4532 unsigned int opb = bfd_octets_per_byte (abfd, s);
4533
4534 if ((s->flags & SEC_ALLOC) != 0
4535 && s->vma * opb >= phdr->p_vaddr
4536 && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4537 s->flags |= SEC_READONLY;
4538 }
4539 break;
4540 }
4541
4542 /* We do not want to include any of the sections in a dynamic
4543 object in the output file. We hack by simply clobbering the
4544 list of sections in the BFD. This could be handled more
4545 cleanly by, say, a new section flag; the existing
4546 SEC_NEVER_LOAD flag is not the one we want, because that one
4547 still implies that the section takes up space in the output
4548 file. */
4549 bfd_section_list_clear (abfd);
4550
4551 /* Find the name to use in a DT_NEEDED entry that refers to this
4552 object. If the object has a DT_SONAME entry, we use it.
4553 Otherwise, if the generic linker stuck something in
4554 elf_dt_name, we use that. Otherwise, we just use the file
4555 name. */
4556 if (soname == NULL || *soname == '\0')
4557 {
4558 soname = elf_dt_name (abfd);
4559 if (soname == NULL || *soname == '\0')
4560 soname = bfd_get_filename (abfd);
4561 }
4562
4563 /* Save the SONAME because sometimes the linker emulation code
4564 will need to know it. */
4565 elf_dt_name (abfd) = soname;
4566
4567 /* If we have already included this dynamic object in the
4568 link, just ignore it. There is no reason to include a
4569 particular dynamic object more than once. */
4570 for (loaded_lib = htab->dyn_loaded;
4571 loaded_lib != NULL;
4572 loaded_lib = loaded_lib->next)
4573 {
4574 if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4575 return true;
4576 }
4577
4578 /* Create dynamic sections for backends that require that be done
4579 before setup_gnu_properties. */
4580 if (add_needed
4581 && !_bfd_elf_link_create_dynamic_sections (abfd, info))
4582 return false;
4583
4584 /* Save the DT_AUDIT entry for the linker emulation code. */
4585 elf_dt_audit (abfd) = audit;
4586 }
4587
4588 /* If this is a dynamic object, we always link against the .dynsym
4589 symbol table, not the .symtab symbol table. The dynamic linker
4590 will only see the .dynsym symbol table, so there is no reason to
4591 look at .symtab for a dynamic object. */
4592
4593 if (! dynamic || elf_dynsymtab (abfd) == 0)
4594 hdr = &elf_tdata (abfd)->symtab_hdr;
4595 else
4596 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4597
4598 symcount = hdr->sh_size / bed->s->sizeof_sym;
4599
4600 /* The sh_info field of the symtab header tells us where the
4601 external symbols start. We don't care about the local symbols at
4602 this point. */
4603 if (elf_bad_symtab (abfd))
4604 {
4605 extsymcount = symcount;
4606 extsymoff = 0;
4607 }
4608 else
4609 {
4610 extsymcount = symcount - hdr->sh_info;
4611 extsymoff = hdr->sh_info;
4612 }
4613
4614 sym_hash = elf_sym_hashes (abfd);
4615 if (extsymcount != 0)
4616 {
4617 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4618 NULL, NULL, NULL);
4619 if (isymbuf == NULL)
4620 goto error_return;
4621
4622 if (sym_hash == NULL)
4623 {
4624 /* We store a pointer to the hash table entry for each
4625 external symbol. */
4626 size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4627 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4628 if (sym_hash == NULL)
4629 goto error_free_sym;
4630 elf_sym_hashes (abfd) = sym_hash;
4631 }
4632 }
4633
4634 if (dynamic)
4635 {
4636 /* Read in any version definitions. */
4637 if (!_bfd_elf_slurp_version_tables (abfd,
4638 info->default_imported_symver))
4639 goto error_free_sym;
4640
4641 /* Read in the symbol versions, but don't bother to convert them
4642 to internal format. */
4643 if (elf_dynversym (abfd) != 0)
4644 {
4645 Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4646 bfd_size_type amt = versymhdr->sh_size;
4647
4648 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4649 goto error_free_sym;
4650 extversym = (Elf_External_Versym *)
4651 _bfd_malloc_and_read (abfd, amt, amt);
4652 if (extversym == NULL)
4653 goto error_free_sym;
4654 extversym_end = extversym + amt / sizeof (*extversym);
4655 }
4656 }
4657
4658 /* If we are loading an as-needed shared lib, save the symbol table
4659 state before we start adding symbols. If the lib turns out
4660 to be unneeded, restore the state. */
4661 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4662 {
4663 unsigned int i;
4664 size_t entsize;
4665
4666 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4667 {
4668 struct bfd_hash_entry *p;
4669 struct elf_link_hash_entry *h;
4670
4671 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4672 {
4673 h = (struct elf_link_hash_entry *) p;
4674 entsize += htab->root.table.entsize;
4675 if (h->root.type == bfd_link_hash_warning)
4676 {
4677 entsize += htab->root.table.entsize;
4678 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4679 }
4680 if (h->root.type == bfd_link_hash_common)
4681 entsize += sizeof (*h->root.u.c.p);
4682 }
4683 }
4684
4685 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4686 old_tab = bfd_malloc (tabsize + entsize);
4687 if (old_tab == NULL)
4688 goto error_free_vers;
4689
4690 /* Remember the current objalloc pointer, so that all mem for
4691 symbols added can later be reclaimed. */
4692 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4693 if (alloc_mark == NULL)
4694 goto error_free_vers;
4695
4696 /* Make a special call to the linker "notice" function to
4697 tell it that we are about to handle an as-needed lib. */
4698 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4699 goto error_free_vers;
4700
4701 /* Clone the symbol table. Remember some pointers into the
4702 symbol table, and dynamic symbol count. */
4703 old_ent = (char *) old_tab + tabsize;
4704 memcpy (old_tab, htab->root.table.table, tabsize);
4705 old_undefs = htab->root.undefs;
4706 old_undefs_tail = htab->root.undefs_tail;
4707 old_table = htab->root.table.table;
4708 old_size = htab->root.table.size;
4709 old_count = htab->root.table.count;
4710 old_strtab = NULL;
4711 if (htab->dynstr != NULL)
4712 {
4713 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4714 if (old_strtab == NULL)
4715 goto error_free_vers;
4716 }
4717
4718 for (i = 0; i < htab->root.table.size; i++)
4719 {
4720 struct bfd_hash_entry *p;
4721 struct elf_link_hash_entry *h;
4722
4723 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4724 {
4725 h = (struct elf_link_hash_entry *) p;
4726 memcpy (old_ent, h, htab->root.table.entsize);
4727 old_ent = (char *) old_ent + htab->root.table.entsize;
4728 if (h->root.type == bfd_link_hash_warning)
4729 {
4730 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4731 memcpy (old_ent, h, htab->root.table.entsize);
4732 old_ent = (char *) old_ent + htab->root.table.entsize;
4733 }
4734 if (h->root.type == bfd_link_hash_common)
4735 {
4736 memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
4737 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
4738 }
4739 }
4740 }
4741 }
4742
4743 weaks = NULL;
4744 if (extversym == NULL)
4745 ever = NULL;
4746 else if (extversym + extsymoff < extversym_end)
4747 ever = extversym + extsymoff;
4748 else
4749 {
4750 /* xgettext:c-format */
4751 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4752 abfd, (long) extsymoff,
4753 (long) (extversym_end - extversym) / sizeof (* extversym));
4754 bfd_set_error (bfd_error_bad_value);
4755 goto error_free_vers;
4756 }
4757
4758 if (!bfd_link_relocatable (info)
4759 && abfd->lto_slim_object)
4760 {
4761 _bfd_error_handler
4762 (_("%pB: plugin needed to handle lto object"), abfd);
4763 }
4764
4765 for (isym = isymbuf, isymend = PTR_ADD (isymbuf, extsymcount);
4766 isym < isymend;
4767 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4768 {
4769 int bind;
4770 bfd_vma value;
4771 asection *sec, *new_sec;
4772 flagword flags;
4773 const char *name;
4774 struct elf_link_hash_entry *h;
4775 struct elf_link_hash_entry *hi;
4776 bool definition;
4777 bool size_change_ok;
4778 bool type_change_ok;
4779 bool new_weak;
4780 bool old_weak;
4781 bfd *override;
4782 bool common;
4783 bool discarded;
4784 unsigned int old_alignment;
4785 unsigned int shindex;
4786 bfd *old_bfd;
4787 bool matched;
4788
4789 override = NULL;
4790
4791 flags = BSF_NO_FLAGS;
4792 sec = NULL;
4793 value = isym->st_value;
4794 common = bed->common_definition (isym);
4795 if (common && info->inhibit_common_definition)
4796 {
4797 /* Treat common symbol as undefined for --no-define-common. */
4798 isym->st_shndx = SHN_UNDEF;
4799 common = false;
4800 }
4801 discarded = false;
4802
4803 bind = ELF_ST_BIND (isym->st_info);
4804 switch (bind)
4805 {
4806 case STB_LOCAL:
4807 /* This should be impossible, since ELF requires that all
4808 global symbols follow all local symbols, and that sh_info
4809 point to the first global symbol. Unfortunately, Irix 5
4810 screws this up. */
4811 if (elf_bad_symtab (abfd))
4812 continue;
4813
4814 /* If we aren't prepared to handle locals within the globals
4815 then we'll likely segfault on a NULL symbol hash if the
4816 symbol is ever referenced in relocations. */
4817 shindex = elf_elfheader (abfd)->e_shstrndx;
4818 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4819 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4820 " (>= sh_info of %lu)"),
4821 abfd, name, (long) (isym - isymbuf + extsymoff),
4822 (long) extsymoff);
4823
4824 /* Dynamic object relocations are not processed by ld, so
4825 ld won't run into the problem mentioned above. */
4826 if (dynamic)
4827 continue;
4828 bfd_set_error (bfd_error_bad_value);
4829 goto error_free_vers;
4830
4831 case STB_GLOBAL:
4832 if (isym->st_shndx != SHN_UNDEF && !common)
4833 flags = BSF_GLOBAL;
4834 break;
4835
4836 case STB_WEAK:
4837 flags = BSF_WEAK;
4838 break;
4839
4840 case STB_GNU_UNIQUE:
4841 flags = BSF_GNU_UNIQUE;
4842 break;
4843
4844 default:
4845 /* Leave it up to the processor backend. */
4846 break;
4847 }
4848
4849 if (isym->st_shndx == SHN_UNDEF)
4850 sec = bfd_und_section_ptr;
4851 else if (isym->st_shndx == SHN_ABS)
4852 sec = bfd_abs_section_ptr;
4853 else if (isym->st_shndx == SHN_COMMON)
4854 {
4855 sec = bfd_com_section_ptr;
4856 /* What ELF calls the size we call the value. What ELF
4857 calls the value we call the alignment. */
4858 value = isym->st_size;
4859 }
4860 else
4861 {
4862 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4863 if (sec == NULL)
4864 sec = bfd_abs_section_ptr;
4865 else if (discarded_section (sec))
4866 {
4867 /* Symbols from discarded section are undefined. We keep
4868 its visibility. */
4869 sec = bfd_und_section_ptr;
4870 discarded = true;
4871 isym->st_shndx = SHN_UNDEF;
4872 }
4873 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4874 value -= sec->vma;
4875 }
4876
4877 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4878 isym->st_name);
4879 if (name == NULL)
4880 goto error_free_vers;
4881
4882 if (isym->st_shndx == SHN_COMMON
4883 && (abfd->flags & BFD_PLUGIN) != 0)
4884 {
4885 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4886
4887 if (xc == NULL)
4888 {
4889 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4890 | SEC_EXCLUDE);
4891 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4892 if (xc == NULL)
4893 goto error_free_vers;
4894 }
4895 sec = xc;
4896 }
4897 else if (isym->st_shndx == SHN_COMMON
4898 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4899 && !bfd_link_relocatable (info))
4900 {
4901 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4902
4903 if (tcomm == NULL)
4904 {
4905 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4906 | SEC_LINKER_CREATED);
4907 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4908 if (tcomm == NULL)
4909 goto error_free_vers;
4910 }
4911 sec = tcomm;
4912 }
4913 else if (bed->elf_add_symbol_hook)
4914 {
4915 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4916 &sec, &value))
4917 goto error_free_vers;
4918
4919 /* The hook function sets the name to NULL if this symbol
4920 should be skipped for some reason. */
4921 if (name == NULL)
4922 continue;
4923 }
4924
4925 /* Sanity check that all possibilities were handled. */
4926 if (sec == NULL)
4927 abort ();
4928
4929 /* Silently discard TLS symbols from --just-syms. There's
4930 no way to combine a static TLS block with a new TLS block
4931 for this executable. */
4932 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4933 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4934 continue;
4935
4936 if (bfd_is_und_section (sec)
4937 || bfd_is_com_section (sec))
4938 definition = false;
4939 else
4940 definition = true;
4941
4942 size_change_ok = false;
4943 type_change_ok = bed->type_change_ok;
4944 old_weak = false;
4945 matched = false;
4946 old_alignment = 0;
4947 old_bfd = NULL;
4948 new_sec = sec;
4949
4950 if (is_elf_hash_table (&htab->root))
4951 {
4952 Elf_Internal_Versym iver;
4953 unsigned int vernum = 0;
4954 bool skip;
4955
4956 if (ever == NULL)
4957 {
4958 if (info->default_imported_symver)
4959 /* Use the default symbol version created earlier. */
4960 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4961 else
4962 iver.vs_vers = 0;
4963 }
4964 else if (ever >= extversym_end)
4965 {
4966 /* xgettext:c-format */
4967 _bfd_error_handler (_("%pB: not enough version information"),
4968 abfd);
4969 bfd_set_error (bfd_error_bad_value);
4970 goto error_free_vers;
4971 }
4972 else
4973 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4974
4975 vernum = iver.vs_vers & VERSYM_VERSION;
4976
4977 /* If this is a hidden symbol, or if it is not version
4978 1, we append the version name to the symbol name.
4979 However, we do not modify a non-hidden absolute symbol
4980 if it is not a function, because it might be the version
4981 symbol itself. FIXME: What if it isn't? */
4982 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4983 || (vernum > 1
4984 && (!bfd_is_abs_section (sec)
4985 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4986 {
4987 const char *verstr;
4988 size_t namelen, verlen, newlen;
4989 char *newname, *p;
4990
4991 if (isym->st_shndx != SHN_UNDEF)
4992 {
4993 if (vernum > elf_tdata (abfd)->cverdefs)
4994 verstr = NULL;
4995 else if (vernum > 1)
4996 verstr =
4997 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4998 else
4999 verstr = "";
5000
5001 if (verstr == NULL)
5002 {
5003 _bfd_error_handler
5004 /* xgettext:c-format */
5005 (_("%pB: %s: invalid version %u (max %d)"),
5006 abfd, name, vernum,
5007 elf_tdata (abfd)->cverdefs);
5008 bfd_set_error (bfd_error_bad_value);
5009 goto error_free_vers;
5010 }
5011 }
5012 else
5013 {
5014 /* We cannot simply test for the number of
5015 entries in the VERNEED section since the
5016 numbers for the needed versions do not start
5017 at 0. */
5018 Elf_Internal_Verneed *t;
5019
5020 verstr = NULL;
5021 for (t = elf_tdata (abfd)->verref;
5022 t != NULL;
5023 t = t->vn_nextref)
5024 {
5025 Elf_Internal_Vernaux *a;
5026
5027 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5028 {
5029 if (a->vna_other == vernum)
5030 {
5031 verstr = a->vna_nodename;
5032 break;
5033 }
5034 }
5035 if (a != NULL)
5036 break;
5037 }
5038 if (verstr == NULL)
5039 {
5040 _bfd_error_handler
5041 /* xgettext:c-format */
5042 (_("%pB: %s: invalid needed version %d"),
5043 abfd, name, vernum);
5044 bfd_set_error (bfd_error_bad_value);
5045 goto error_free_vers;
5046 }
5047 }
5048
5049 namelen = strlen (name);
5050 verlen = strlen (verstr);
5051 newlen = namelen + verlen + 2;
5052 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5053 && isym->st_shndx != SHN_UNDEF)
5054 ++newlen;
5055
5056 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
5057 if (newname == NULL)
5058 goto error_free_vers;
5059 memcpy (newname, name, namelen);
5060 p = newname + namelen;
5061 *p++ = ELF_VER_CHR;
5062 /* If this is a defined non-hidden version symbol,
5063 we add another @ to the name. This indicates the
5064 default version of the symbol. */
5065 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5066 && isym->st_shndx != SHN_UNDEF)
5067 *p++ = ELF_VER_CHR;
5068 memcpy (p, verstr, verlen + 1);
5069
5070 name = newname;
5071 }
5072
5073 /* If this symbol has default visibility and the user has
5074 requested we not re-export it, then mark it as hidden. */
5075 if (!bfd_is_und_section (sec)
5076 && !dynamic
5077 && abfd->no_export
5078 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
5079 isym->st_other = (STV_HIDDEN
5080 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
5081
5082 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
5083 sym_hash, &old_bfd, &old_weak,
5084 &old_alignment, &skip, &override,
5085 &type_change_ok, &size_change_ok,
5086 &matched))
5087 goto error_free_vers;
5088
5089 if (skip)
5090 continue;
5091
5092 /* Override a definition only if the new symbol matches the
5093 existing one. */
5094 if (override && matched)
5095 definition = false;
5096
5097 h = *sym_hash;
5098 while (h->root.type == bfd_link_hash_indirect
5099 || h->root.type == bfd_link_hash_warning)
5100 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5101
5102 if (h->versioned != unversioned
5103 && elf_tdata (abfd)->verdef != NULL
5104 && vernum > 1
5105 && definition)
5106 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
5107 }
5108
5109 if (! (_bfd_generic_link_add_one_symbol
5110 (info, override ? override : abfd, name, flags, sec, value,
5111 NULL, false, bed->collect,
5112 (struct bfd_link_hash_entry **) sym_hash)))
5113 goto error_free_vers;
5114
5115 h = *sym_hash;
5116 /* We need to make sure that indirect symbol dynamic flags are
5117 updated. */
5118 hi = h;
5119 while (h->root.type == bfd_link_hash_indirect
5120 || h->root.type == bfd_link_hash_warning)
5121 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5122
5123 *sym_hash = h;
5124
5125 /* Setting the index to -3 tells elf_link_output_extsym that
5126 this symbol is defined in a discarded section. */
5127 if (discarded && is_elf_hash_table (&htab->root))
5128 h->indx = -3;
5129
5130 new_weak = (flags & BSF_WEAK) != 0;
5131 if (dynamic
5132 && definition
5133 && new_weak
5134 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
5135 && is_elf_hash_table (&htab->root)
5136 && h->u.alias == NULL)
5137 {
5138 /* Keep a list of all weak defined non function symbols from
5139 a dynamic object, using the alias field. Later in this
5140 function we will set the alias field to the correct
5141 value. We only put non-function symbols from dynamic
5142 objects on this list, because that happens to be the only
5143 time we need to know the normal symbol corresponding to a
5144 weak symbol, and the information is time consuming to
5145 figure out. If the alias field is not already NULL,
5146 then this symbol was already defined by some previous
5147 dynamic object, and we will be using that previous
5148 definition anyhow. */
5149
5150 h->u.alias = weaks;
5151 weaks = h;
5152 }
5153
5154 /* Set the alignment of a common symbol. */
5155 if ((common || bfd_is_com_section (sec))
5156 && h->root.type == bfd_link_hash_common)
5157 {
5158 unsigned int align;
5159
5160 if (common)
5161 align = bfd_log2 (isym->st_value);
5162 else
5163 {
5164 /* The new symbol is a common symbol in a shared object.
5165 We need to get the alignment from the section. */
5166 align = new_sec->alignment_power;
5167 }
5168 if (align > old_alignment)
5169 h->root.u.c.p->alignment_power = align;
5170 else
5171 h->root.u.c.p->alignment_power = old_alignment;
5172 }
5173
5174 if (is_elf_hash_table (&htab->root))
5175 {
5176 /* Set a flag in the hash table entry indicating the type of
5177 reference or definition we just found. A dynamic symbol
5178 is one which is referenced or defined by both a regular
5179 object and a shared object. */
5180 bool dynsym = false;
5181
5182 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5183 if ((abfd->flags & BFD_PLUGIN) != 0)
5184 {
5185 /* Except for this flag to track nonweak references. */
5186 if (!definition
5187 && bind != STB_WEAK)
5188 h->ref_ir_nonweak = 1;
5189 }
5190 else if (!dynamic)
5191 {
5192 if (! definition)
5193 {
5194 h->ref_regular = 1;
5195 if (bind != STB_WEAK)
5196 h->ref_regular_nonweak = 1;
5197 }
5198 else
5199 {
5200 h->def_regular = 1;
5201 if (h->def_dynamic)
5202 {
5203 h->def_dynamic = 0;
5204 h->ref_dynamic = 1;
5205 }
5206 }
5207 }
5208 else
5209 {
5210 if (! definition)
5211 {
5212 h->ref_dynamic = 1;
5213 hi->ref_dynamic = 1;
5214 }
5215 else
5216 {
5217 h->def_dynamic = 1;
5218 hi->def_dynamic = 1;
5219 }
5220 }
5221
5222 /* If an indirect symbol has been forced local, don't
5223 make the real symbol dynamic. */
5224 if (h != hi && hi->forced_local)
5225 ;
5226 else if (!dynamic)
5227 {
5228 if (bfd_link_dll (info)
5229 || h->def_dynamic
5230 || h->ref_dynamic)
5231 dynsym = true;
5232 }
5233 else
5234 {
5235 if (h->def_regular
5236 || h->ref_regular
5237 || (h->is_weakalias
5238 && weakdef (h)->dynindx != -1))
5239 dynsym = true;
5240 }
5241
5242 /* Check to see if we need to add an indirect symbol for
5243 the default name. */
5244 if ((definition
5245 || (!override && h->root.type == bfd_link_hash_common))
5246 && !(hi != h
5247 && hi->versioned == versioned_hidden))
5248 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5249 sec, value, &old_bfd, &dynsym))
5250 goto error_free_vers;
5251
5252 /* Check the alignment when a common symbol is involved. This
5253 can change when a common symbol is overridden by a normal
5254 definition or a common symbol is ignored due to the old
5255 normal definition. We need to make sure the maximum
5256 alignment is maintained. */
5257 if ((old_alignment || common)
5258 && h->root.type != bfd_link_hash_common)
5259 {
5260 unsigned int common_align;
5261 unsigned int normal_align;
5262 unsigned int symbol_align;
5263 bfd *normal_bfd;
5264 bfd *common_bfd;
5265
5266 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5267 || h->root.type == bfd_link_hash_defweak);
5268
5269 symbol_align = ffs (h->root.u.def.value) - 1;
5270 if (h->root.u.def.section->owner != NULL
5271 && (h->root.u.def.section->owner->flags
5272 & (DYNAMIC | BFD_PLUGIN)) == 0)
5273 {
5274 normal_align = h->root.u.def.section->alignment_power;
5275 if (normal_align > symbol_align)
5276 normal_align = symbol_align;
5277 }
5278 else
5279 normal_align = symbol_align;
5280
5281 if (old_alignment)
5282 {
5283 common_align = old_alignment;
5284 common_bfd = old_bfd;
5285 normal_bfd = abfd;
5286 }
5287 else
5288 {
5289 common_align = bfd_log2 (isym->st_value);
5290 common_bfd = abfd;
5291 normal_bfd = old_bfd;
5292 }
5293
5294 if (normal_align < common_align)
5295 {
5296 /* PR binutils/2735 */
5297 if (normal_bfd == NULL)
5298 _bfd_error_handler
5299 /* xgettext:c-format */
5300 (_("warning: alignment %u of common symbol `%s' in %pB is"
5301 " greater than the alignment (%u) of its section %pA"),
5302 1 << common_align, name, common_bfd,
5303 1 << normal_align, h->root.u.def.section);
5304 else
5305 _bfd_error_handler
5306 /* xgettext:c-format */
5307 (_("warning: alignment %u of symbol `%s' in %pB"
5308 " is smaller than %u in %pB"),
5309 1 << normal_align, name, normal_bfd,
5310 1 << common_align, common_bfd);
5311 }
5312 }
5313
5314 /* Remember the symbol size if it isn't undefined. */
5315 if (isym->st_size != 0
5316 && isym->st_shndx != SHN_UNDEF
5317 && (definition || h->size == 0))
5318 {
5319 if (h->size != 0
5320 && h->size != isym->st_size
5321 && ! size_change_ok)
5322 _bfd_error_handler
5323 /* xgettext:c-format */
5324 (_("warning: size of symbol `%s' changed"
5325 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5326 name, (uint64_t) h->size, old_bfd,
5327 (uint64_t) isym->st_size, abfd);
5328
5329 h->size = isym->st_size;
5330 }
5331
5332 /* If this is a common symbol, then we always want H->SIZE
5333 to be the size of the common symbol. The code just above
5334 won't fix the size if a common symbol becomes larger. We
5335 don't warn about a size change here, because that is
5336 covered by --warn-common. Allow changes between different
5337 function types. */
5338 if (h->root.type == bfd_link_hash_common)
5339 h->size = h->root.u.c.size;
5340
5341 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5342 && ((definition && !new_weak)
5343 || (old_weak && h->root.type == bfd_link_hash_common)
5344 || h->type == STT_NOTYPE))
5345 {
5346 unsigned int type = ELF_ST_TYPE (isym->st_info);
5347
5348 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5349 symbol. */
5350 if (type == STT_GNU_IFUNC
5351 && (abfd->flags & DYNAMIC) != 0)
5352 type = STT_FUNC;
5353
5354 if (h->type != type)
5355 {
5356 if (h->type != STT_NOTYPE && ! type_change_ok)
5357 /* xgettext:c-format */
5358 _bfd_error_handler
5359 (_("warning: type of symbol `%s' changed"
5360 " from %d to %d in %pB"),
5361 name, h->type, type, abfd);
5362
5363 h->type = type;
5364 }
5365 }
5366
5367 /* Merge st_other field. */
5368 elf_merge_st_other (abfd, h, isym->st_other, sec,
5369 definition, dynamic);
5370
5371 /* We don't want to make debug symbol dynamic. */
5372 if (definition
5373 && (sec->flags & SEC_DEBUGGING)
5374 && !bfd_link_relocatable (info))
5375 dynsym = false;
5376
5377 /* Nor should we make plugin symbols dynamic. */
5378 if ((abfd->flags & BFD_PLUGIN) != 0)
5379 dynsym = false;
5380
5381 if (definition)
5382 {
5383 h->target_internal = isym->st_target_internal;
5384 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5385 }
5386
5387 if (definition && !dynamic)
5388 {
5389 char *p = strchr (name, ELF_VER_CHR);
5390 if (p != NULL && p[1] != ELF_VER_CHR)
5391 {
5392 /* Queue non-default versions so that .symver x, x@FOO
5393 aliases can be checked. */
5394 if (!nondeflt_vers)
5395 {
5396 size_t amt = ((isymend - isym + 1)
5397 * sizeof (struct elf_link_hash_entry *));
5398 nondeflt_vers
5399 = (struct elf_link_hash_entry **) bfd_malloc (amt);
5400 if (!nondeflt_vers)
5401 goto error_free_vers;
5402 }
5403 nondeflt_vers[nondeflt_vers_cnt++] = h;
5404 }
5405 }
5406
5407 if (dynsym && h->dynindx == -1)
5408 {
5409 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5410 goto error_free_vers;
5411 if (h->is_weakalias
5412 && weakdef (h)->dynindx == -1)
5413 {
5414 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5415 goto error_free_vers;
5416 }
5417 }
5418 else if (h->dynindx != -1)
5419 /* If the symbol already has a dynamic index, but
5420 visibility says it should not be visible, turn it into
5421 a local symbol. */
5422 switch (ELF_ST_VISIBILITY (h->other))
5423 {
5424 case STV_INTERNAL:
5425 case STV_HIDDEN:
5426 (*bed->elf_backend_hide_symbol) (info, h, true);
5427 dynsym = false;
5428 break;
5429 }
5430
5431 if (!add_needed
5432 && matched
5433 && definition
5434 && h->root.type != bfd_link_hash_indirect
5435 && ((dynsym
5436 && h->ref_regular_nonweak)
5437 || (old_bfd != NULL
5438 && (old_bfd->flags & BFD_PLUGIN) != 0
5439 && h->ref_ir_nonweak
5440 && !info->lto_all_symbols_read)
5441 || (h->ref_dynamic_nonweak
5442 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5443 && !on_needed_list (elf_dt_name (abfd),
5444 htab->needed, NULL))))
5445 {
5446 const char *soname = elf_dt_name (abfd);
5447
5448 info->callbacks->minfo ("%!", soname, old_bfd,
5449 h->root.root.string);
5450
5451 /* A symbol from a library loaded via DT_NEEDED of some
5452 other library is referenced by a regular object.
5453 Add a DT_NEEDED entry for it. Issue an error if
5454 --no-add-needed is used and the reference was not
5455 a weak one. */
5456 if (old_bfd != NULL
5457 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5458 {
5459 _bfd_error_handler
5460 /* xgettext:c-format */
5461 (_("%pB: undefined reference to symbol '%s'"),
5462 old_bfd, name);
5463 bfd_set_error (bfd_error_missing_dso);
5464 goto error_free_vers;
5465 }
5466
5467 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5468 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5469
5470 /* Create dynamic sections for backends that require
5471 that be done before setup_gnu_properties. */
5472 if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
5473 return false;
5474 add_needed = true;
5475 }
5476 }
5477 }
5478
5479 if (info->lto_plugin_active
5480 && !bfd_link_relocatable (info)
5481 && (abfd->flags & BFD_PLUGIN) == 0
5482 && !just_syms
5483 && extsymcount)
5484 {
5485 int r_sym_shift;
5486
5487 if (bed->s->arch_size == 32)
5488 r_sym_shift = 8;
5489 else
5490 r_sym_shift = 32;
5491
5492 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5493 referenced in regular objects so that linker plugin will get
5494 the correct symbol resolution. */
5495
5496 sym_hash = elf_sym_hashes (abfd);
5497 for (s = abfd->sections; s != NULL; s = s->next)
5498 {
5499 Elf_Internal_Rela *internal_relocs;
5500 Elf_Internal_Rela *rel, *relend;
5501
5502 /* Don't check relocations in excluded sections. */
5503 if ((s->flags & SEC_RELOC) == 0
5504 || s->reloc_count == 0
5505 || (s->flags & SEC_EXCLUDE) != 0
5506 || ((info->strip == strip_all
5507 || info->strip == strip_debugger)
5508 && (s->flags & SEC_DEBUGGING) != 0))
5509 continue;
5510
5511 internal_relocs = _bfd_elf_link_info_read_relocs (abfd, info,
5512 s, NULL,
5513 NULL,
5514 _bfd_link_keep_memory (info));
5515 if (internal_relocs == NULL)
5516 goto error_free_vers;
5517
5518 rel = internal_relocs;
5519 relend = rel + s->reloc_count;
5520 for ( ; rel < relend; rel++)
5521 {
5522 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5523 struct elf_link_hash_entry *h;
5524
5525 /* Skip local symbols. */
5526 if (r_symndx < extsymoff)
5527 continue;
5528
5529 h = sym_hash[r_symndx - extsymoff];
5530 if (h != NULL)
5531 h->root.non_ir_ref_regular = 1;
5532 }
5533
5534 if (elf_section_data (s)->relocs != internal_relocs)
5535 free (internal_relocs);
5536 }
5537 }
5538
5539 free (extversym);
5540 extversym = NULL;
5541 free (isymbuf);
5542 isymbuf = NULL;
5543
5544 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5545 {
5546 unsigned int i;
5547
5548 /* Restore the symbol table. */
5549 old_ent = (char *) old_tab + tabsize;
5550 memset (elf_sym_hashes (abfd), 0,
5551 extsymcount * sizeof (struct elf_link_hash_entry *));
5552 htab->root.table.table = old_table;
5553 htab->root.table.size = old_size;
5554 htab->root.table.count = old_count;
5555 memcpy (htab->root.table.table, old_tab, tabsize);
5556 htab->root.undefs = old_undefs;
5557 htab->root.undefs_tail = old_undefs_tail;
5558 if (htab->dynstr != NULL)
5559 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5560 free (old_strtab);
5561 old_strtab = NULL;
5562 for (i = 0; i < htab->root.table.size; i++)
5563 {
5564 struct bfd_hash_entry *p;
5565 struct elf_link_hash_entry *h;
5566 unsigned int non_ir_ref_dynamic;
5567
5568 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5569 {
5570 /* Preserve non_ir_ref_dynamic so that this symbol
5571 will be exported when the dynamic lib becomes needed
5572 in the second pass. */
5573 h = (struct elf_link_hash_entry *) p;
5574 if (h->root.type == bfd_link_hash_warning)
5575 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5576 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5577
5578 h = (struct elf_link_hash_entry *) p;
5579 memcpy (h, old_ent, htab->root.table.entsize);
5580 old_ent = (char *) old_ent + htab->root.table.entsize;
5581 if (h->root.type == bfd_link_hash_warning)
5582 {
5583 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5584 memcpy (h, old_ent, htab->root.table.entsize);
5585 old_ent = (char *) old_ent + htab->root.table.entsize;
5586 }
5587 if (h->root.type == bfd_link_hash_common)
5588 {
5589 memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5590 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
5591 }
5592 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5593 }
5594 }
5595
5596 /* Make a special call to the linker "notice" function to
5597 tell it that symbols added for crefs may need to be removed. */
5598 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5599 goto error_free_vers;
5600
5601 free (old_tab);
5602 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5603 alloc_mark);
5604 free (nondeflt_vers);
5605 return true;
5606 }
5607
5608 if (old_tab != NULL)
5609 {
5610 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5611 goto error_free_vers;
5612 free (old_tab);
5613 old_tab = NULL;
5614 }
5615
5616 /* Now that all the symbols from this input file are created, if
5617 not performing a relocatable link, handle .symver foo, foo@BAR
5618 such that any relocs against foo become foo@BAR. */
5619 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5620 {
5621 size_t cnt, symidx;
5622
5623 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5624 {
5625 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5626 char *shortname, *p;
5627 size_t amt;
5628
5629 p = strchr (h->root.root.string, ELF_VER_CHR);
5630 if (p == NULL
5631 || (h->root.type != bfd_link_hash_defined
5632 && h->root.type != bfd_link_hash_defweak))
5633 continue;
5634
5635 amt = p - h->root.root.string;
5636 shortname = (char *) bfd_malloc (amt + 1);
5637 if (!shortname)
5638 goto error_free_vers;
5639 memcpy (shortname, h->root.root.string, amt);
5640 shortname[amt] = '\0';
5641
5642 hi = (struct elf_link_hash_entry *)
5643 bfd_link_hash_lookup (&htab->root, shortname,
5644 false, false, false);
5645 if (hi != NULL
5646 && hi->root.type == h->root.type
5647 && hi->root.u.def.value == h->root.u.def.value
5648 && hi->root.u.def.section == h->root.u.def.section)
5649 {
5650 (*bed->elf_backend_hide_symbol) (info, hi, true);
5651 hi->root.type = bfd_link_hash_indirect;
5652 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5653 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5654 sym_hash = elf_sym_hashes (abfd);
5655 if (sym_hash)
5656 for (symidx = 0; symidx < extsymcount; ++symidx)
5657 if (sym_hash[symidx] == hi)
5658 {
5659 sym_hash[symidx] = h;
5660 break;
5661 }
5662 }
5663 free (shortname);
5664 }
5665 free (nondeflt_vers);
5666 nondeflt_vers = NULL;
5667 }
5668
5669 /* Now set the alias field correctly for all the weak defined
5670 symbols we found. The only way to do this is to search all the
5671 symbols. Since we only need the information for non functions in
5672 dynamic objects, that's the only time we actually put anything on
5673 the list WEAKS. We need this information so that if a regular
5674 object refers to a symbol defined weakly in a dynamic object, the
5675 real symbol in the dynamic object is also put in the dynamic
5676 symbols; we also must arrange for both symbols to point to the
5677 same memory location. We could handle the general case of symbol
5678 aliasing, but a general symbol alias can only be generated in
5679 assembler code, handling it correctly would be very time
5680 consuming, and other ELF linkers don't handle general aliasing
5681 either. */
5682 if (weaks != NULL)
5683 {
5684 struct elf_link_hash_entry **hpp;
5685 struct elf_link_hash_entry **hppend;
5686 struct elf_link_hash_entry **sorted_sym_hash;
5687 struct elf_link_hash_entry *h;
5688 size_t sym_count, amt;
5689
5690 /* Since we have to search the whole symbol list for each weak
5691 defined symbol, search time for N weak defined symbols will be
5692 O(N^2). Binary search will cut it down to O(NlogN). */
5693 amt = extsymcount * sizeof (*sorted_sym_hash);
5694 sorted_sym_hash = bfd_malloc (amt);
5695 if (sorted_sym_hash == NULL)
5696 goto error_return;
5697 sym_hash = sorted_sym_hash;
5698 hpp = elf_sym_hashes (abfd);
5699 hppend = hpp + extsymcount;
5700 sym_count = 0;
5701 for (; hpp < hppend; hpp++)
5702 {
5703 h = *hpp;
5704 if (h != NULL
5705 && h->root.type == bfd_link_hash_defined
5706 && !bed->is_function_type (h->type))
5707 {
5708 *sym_hash = h;
5709 sym_hash++;
5710 sym_count++;
5711 }
5712 }
5713
5714 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
5715 elf_sort_symbol);
5716
5717 while (weaks != NULL)
5718 {
5719 struct elf_link_hash_entry *hlook;
5720 asection *slook;
5721 bfd_vma vlook;
5722 size_t i, j, idx = 0;
5723
5724 hlook = weaks;
5725 weaks = hlook->u.alias;
5726 hlook->u.alias = NULL;
5727
5728 if (hlook->root.type != bfd_link_hash_defined
5729 && hlook->root.type != bfd_link_hash_defweak)
5730 continue;
5731
5732 slook = hlook->root.u.def.section;
5733 vlook = hlook->root.u.def.value;
5734
5735 i = 0;
5736 j = sym_count;
5737 while (i != j)
5738 {
5739 bfd_signed_vma vdiff;
5740 idx = (i + j) / 2;
5741 h = sorted_sym_hash[idx];
5742 vdiff = vlook - h->root.u.def.value;
5743 if (vdiff < 0)
5744 j = idx;
5745 else if (vdiff > 0)
5746 i = idx + 1;
5747 else
5748 {
5749 int sdiff = slook->id - h->root.u.def.section->id;
5750 if (sdiff < 0)
5751 j = idx;
5752 else if (sdiff > 0)
5753 i = idx + 1;
5754 else
5755 break;
5756 }
5757 }
5758
5759 /* We didn't find a value/section match. */
5760 if (i == j)
5761 continue;
5762
5763 /* With multiple aliases, or when the weak symbol is already
5764 strongly defined, we have multiple matching symbols and
5765 the binary search above may land on any of them. Step
5766 one past the matching symbol(s). */
5767 while (++idx != j)
5768 {
5769 h = sorted_sym_hash[idx];
5770 if (h->root.u.def.section != slook
5771 || h->root.u.def.value != vlook)
5772 break;
5773 }
5774
5775 /* Now look back over the aliases. Since we sorted by size
5776 as well as value and section, we'll choose the one with
5777 the largest size. */
5778 while (idx-- != i)
5779 {
5780 h = sorted_sym_hash[idx];
5781
5782 /* Stop if value or section doesn't match. */
5783 if (h->root.u.def.section != slook
5784 || h->root.u.def.value != vlook)
5785 break;
5786 else if (h != hlook)
5787 {
5788 struct elf_link_hash_entry *t;
5789
5790 hlook->u.alias = h;
5791 hlook->is_weakalias = 1;
5792 t = h;
5793 if (t->u.alias != NULL)
5794 while (t->u.alias != h)
5795 t = t->u.alias;
5796 t->u.alias = hlook;
5797
5798 /* If the weak definition is in the list of dynamic
5799 symbols, make sure the real definition is put
5800 there as well. */
5801 if (hlook->dynindx != -1 && h->dynindx == -1)
5802 {
5803 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5804 {
5805 err_free_sym_hash:
5806 free (sorted_sym_hash);
5807 goto error_return;
5808 }
5809 }
5810
5811 /* If the real definition is in the list of dynamic
5812 symbols, make sure the weak definition is put
5813 there as well. If we don't do this, then the
5814 dynamic loader might not merge the entries for the
5815 real definition and the weak definition. */
5816 if (h->dynindx != -1 && hlook->dynindx == -1)
5817 {
5818 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5819 goto err_free_sym_hash;
5820 }
5821 break;
5822 }
5823 }
5824 }
5825
5826 free (sorted_sym_hash);
5827 }
5828
5829 if (bed->check_directives
5830 && !(*bed->check_directives) (abfd, info))
5831 return false;
5832
5833 /* If this is a non-traditional link, try to optimize the handling
5834 of the .stab/.stabstr sections. */
5835 if (! dynamic
5836 && ! info->traditional_format
5837 && is_elf_hash_table (&htab->root)
5838 && (info->strip != strip_all && info->strip != strip_debugger))
5839 {
5840 asection *stabstr;
5841
5842 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5843 if (stabstr != NULL)
5844 {
5845 bfd_size_type string_offset = 0;
5846 asection *stab;
5847
5848 for (stab = abfd->sections; stab; stab = stab->next)
5849 if (startswith (stab->name, ".stab")
5850 && (!stab->name[5] ||
5851 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5852 && (stab->flags & SEC_MERGE) == 0
5853 && !bfd_is_abs_section (stab->output_section))
5854 {
5855 struct bfd_elf_section_data *secdata;
5856
5857 secdata = elf_section_data (stab);
5858 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5859 stabstr, &secdata->sec_info,
5860 &string_offset))
5861 goto error_return;
5862 if (secdata->sec_info)
5863 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5864 }
5865 }
5866 }
5867
5868 if (dynamic && add_needed)
5869 {
5870 /* Add this bfd to the loaded list. */
5871 struct elf_link_loaded_list *n;
5872
5873 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5874 if (n == NULL)
5875 goto error_return;
5876 n->abfd = abfd;
5877 n->next = htab->dyn_loaded;
5878 htab->dyn_loaded = n;
5879 }
5880 if (dynamic && !add_needed
5881 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
5882 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
5883
5884 return true;
5885
5886 error_free_vers:
5887 free (old_tab);
5888 free (old_strtab);
5889 free (nondeflt_vers);
5890 free (extversym);
5891 error_free_sym:
5892 free (isymbuf);
5893 error_return:
5894 return false;
5895 }
5896
5897 /* Return the linker hash table entry of a symbol that might be
5898 satisfied by an archive symbol. Return -1 on error. */
5899
5900 struct bfd_link_hash_entry *
_bfd_elf_archive_symbol_lookup(bfd * abfd,struct bfd_link_info * info,const char * name)5901 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5902 struct bfd_link_info *info,
5903 const char *name)
5904 {
5905 struct bfd_link_hash_entry *h;
5906 char *p, *copy;
5907 size_t len, first;
5908
5909 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
5910 if (h != NULL)
5911 return h;
5912
5913 /* If this is a default version (the name contains @@), look up the
5914 symbol again with only one `@' as well as without the version.
5915 The effect is that references to the symbol with and without the
5916 version will be matched by the default symbol in the archive. */
5917
5918 p = strchr (name, ELF_VER_CHR);
5919 if (p == NULL || p[1] != ELF_VER_CHR)
5920 return h;
5921
5922 /* First check with only one `@'. */
5923 len = strlen (name);
5924 copy = (char *) bfd_alloc (abfd, len);
5925 if (copy == NULL)
5926 return (struct bfd_link_hash_entry *) -1;
5927
5928 first = p - name + 1;
5929 memcpy (copy, name, first);
5930 memcpy (copy + first, name + first + 1, len - first);
5931
5932 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
5933 if (h == NULL)
5934 {
5935 /* We also need to check references to the symbol without the
5936 version. */
5937 copy[first - 1] = '\0';
5938 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
5939 }
5940
5941 bfd_release (abfd, copy);
5942 return h;
5943 }
5944
5945 /* Add symbols from an ELF archive file to the linker hash table. We
5946 don't use _bfd_generic_link_add_archive_symbols because we need to
5947 handle versioned symbols.
5948
5949 Fortunately, ELF archive handling is simpler than that done by
5950 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5951 oddities. In ELF, if we find a symbol in the archive map, and the
5952 symbol is currently undefined, we know that we must pull in that
5953 object file.
5954
5955 Unfortunately, we do have to make multiple passes over the symbol
5956 table until nothing further is resolved. */
5957
5958 static bool
elf_link_add_archive_symbols(bfd * abfd,struct bfd_link_info * info)5959 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5960 {
5961 symindex c;
5962 unsigned char *included = NULL;
5963 carsym *symdefs;
5964 bool loop;
5965 size_t amt;
5966 const struct elf_backend_data *bed;
5967 struct bfd_link_hash_entry * (*archive_symbol_lookup)
5968 (bfd *, struct bfd_link_info *, const char *);
5969
5970 if (! bfd_has_map (abfd))
5971 {
5972 /* An empty archive is a special case. */
5973 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5974 return true;
5975 bfd_set_error (bfd_error_no_armap);
5976 return false;
5977 }
5978
5979 /* Keep track of all symbols we know to be already defined, and all
5980 files we know to be already included. This is to speed up the
5981 second and subsequent passes. */
5982 c = bfd_ardata (abfd)->symdef_count;
5983 if (c == 0)
5984 return true;
5985 amt = c * sizeof (*included);
5986 included = (unsigned char *) bfd_zmalloc (amt);
5987 if (included == NULL)
5988 return false;
5989
5990 symdefs = bfd_ardata (abfd)->symdefs;
5991 bed = get_elf_backend_data (abfd);
5992 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5993
5994 do
5995 {
5996 file_ptr last;
5997 symindex i;
5998 carsym *symdef;
5999 carsym *symdefend;
6000
6001 loop = false;
6002 last = -1;
6003
6004 symdef = symdefs;
6005 symdefend = symdef + c;
6006 for (i = 0; symdef < symdefend; symdef++, i++)
6007 {
6008 struct bfd_link_hash_entry *h;
6009 bfd *element;
6010 struct bfd_link_hash_entry *undefs_tail;
6011 symindex mark;
6012
6013 if (included[i])
6014 continue;
6015 if (symdef->file_offset == last)
6016 {
6017 included[i] = true;
6018 continue;
6019 }
6020
6021 h = archive_symbol_lookup (abfd, info, symdef->name);
6022 if (h == (struct bfd_link_hash_entry *) -1)
6023 goto error_return;
6024
6025 if (h == NULL)
6026 continue;
6027
6028 if (h->type == bfd_link_hash_undefined)
6029 {
6030 /* If the archive element has already been loaded then one
6031 of the symbols defined by that element might have been
6032 made undefined due to being in a discarded section. */
6033 if (is_elf_hash_table (info->hash)
6034 && ((struct elf_link_hash_entry *) h)->indx == -3)
6035 continue;
6036 }
6037 else if (h->type == bfd_link_hash_common)
6038 {
6039 /* We currently have a common symbol. The archive map contains
6040 a reference to this symbol, so we may want to include it. We
6041 only want to include it however, if this archive element
6042 contains a definition of the symbol, not just another common
6043 declaration of it.
6044
6045 Unfortunately some archivers (including GNU ar) will put
6046 declarations of common symbols into their archive maps, as
6047 well as real definitions, so we cannot just go by the archive
6048 map alone. Instead we must read in the element's symbol
6049 table and check that to see what kind of symbol definition
6050 this is. */
6051 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
6052 continue;
6053 }
6054 else
6055 {
6056 if (h->type != bfd_link_hash_undefweak)
6057 /* Symbol must be defined. Don't check it again. */
6058 included[i] = true;
6059 continue;
6060 }
6061
6062 /* We need to include this archive member. */
6063 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset,
6064 info);
6065 if (element == NULL)
6066 goto error_return;
6067
6068 if (! bfd_check_format (element, bfd_object))
6069 goto error_return;
6070
6071 undefs_tail = info->hash->undefs_tail;
6072
6073 if (!(*info->callbacks
6074 ->add_archive_element) (info, element, symdef->name, &element))
6075 continue;
6076 if (!bfd_link_add_symbols (element, info))
6077 goto error_return;
6078
6079 /* If there are any new undefined symbols, we need to make
6080 another pass through the archive in order to see whether
6081 they can be defined. FIXME: This isn't perfect, because
6082 common symbols wind up on undefs_tail and because an
6083 undefined symbol which is defined later on in this pass
6084 does not require another pass. This isn't a bug, but it
6085 does make the code less efficient than it could be. */
6086 if (undefs_tail != info->hash->undefs_tail)
6087 loop = true;
6088
6089 /* Look backward to mark all symbols from this object file
6090 which we have already seen in this pass. */
6091 mark = i;
6092 do
6093 {
6094 included[mark] = true;
6095 if (mark == 0)
6096 break;
6097 --mark;
6098 }
6099 while (symdefs[mark].file_offset == symdef->file_offset);
6100
6101 /* We mark subsequent symbols from this object file as we go
6102 on through the loop. */
6103 last = symdef->file_offset;
6104 }
6105 }
6106 while (loop);
6107
6108 free (included);
6109 return true;
6110
6111 error_return:
6112 free (included);
6113 return false;
6114 }
6115
6116 /* Given an ELF BFD, add symbols to the global hash table as
6117 appropriate. */
6118
6119 bool
bfd_elf_link_add_symbols(bfd * abfd,struct bfd_link_info * info)6120 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
6121 {
6122 switch (bfd_get_format (abfd))
6123 {
6124 case bfd_object:
6125 return elf_link_add_object_symbols (abfd, info);
6126 case bfd_archive:
6127 return elf_link_add_archive_symbols (abfd, info);
6128 default:
6129 bfd_set_error (bfd_error_wrong_format);
6130 return false;
6131 }
6132 }
6133
6134 struct hash_codes_info
6135 {
6136 unsigned long *hashcodes;
6137 bool error;
6138 };
6139
6140 /* This function will be called though elf_link_hash_traverse to store
6141 all hash value of the exported symbols in an array. */
6142
6143 static bool
elf_collect_hash_codes(struct elf_link_hash_entry * h,void * data)6144 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
6145 {
6146 struct hash_codes_info *inf = (struct hash_codes_info *) data;
6147 const char *name;
6148 unsigned long ha;
6149 char *alc = NULL;
6150
6151 /* Ignore indirect symbols. These are added by the versioning code. */
6152 if (h->dynindx == -1)
6153 return true;
6154
6155 name = h->root.root.string;
6156 if (h->versioned >= versioned)
6157 {
6158 char *p = strchr (name, ELF_VER_CHR);
6159 if (p != NULL)
6160 {
6161 alc = (char *) bfd_malloc (p - name + 1);
6162 if (alc == NULL)
6163 {
6164 inf->error = true;
6165 return false;
6166 }
6167 memcpy (alc, name, p - name);
6168 alc[p - name] = '\0';
6169 name = alc;
6170 }
6171 }
6172
6173 /* Compute the hash value. */
6174 ha = bfd_elf_hash (name);
6175
6176 /* Store the found hash value in the array given as the argument. */
6177 *(inf->hashcodes)++ = ha;
6178
6179 /* And store it in the struct so that we can put it in the hash table
6180 later. */
6181 h->u.elf_hash_value = ha;
6182
6183 free (alc);
6184 return true;
6185 }
6186
6187 struct collect_gnu_hash_codes
6188 {
6189 bfd *output_bfd;
6190 const struct elf_backend_data *bed;
6191 unsigned long int nsyms;
6192 unsigned long int maskbits;
6193 unsigned long int *hashcodes;
6194 unsigned long int *hashval;
6195 unsigned long int *indx;
6196 unsigned long int *counts;
6197 bfd_vma *bitmask;
6198 bfd_byte *contents;
6199 bfd_size_type xlat;
6200 long int min_dynindx;
6201 unsigned long int bucketcount;
6202 unsigned long int symindx;
6203 long int local_indx;
6204 long int shift1, shift2;
6205 unsigned long int mask;
6206 bool error;
6207 };
6208
6209 /* This function will be called though elf_link_hash_traverse to store
6210 all hash value of the exported symbols in an array. */
6211
6212 static bool
elf_collect_gnu_hash_codes(struct elf_link_hash_entry * h,void * data)6213 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6214 {
6215 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6216 const char *name;
6217 unsigned long ha;
6218 char *alc = NULL;
6219
6220 /* Ignore indirect symbols. These are added by the versioning code. */
6221 if (h->dynindx == -1)
6222 return true;
6223
6224 /* Ignore also local symbols and undefined symbols. */
6225 if (! (*s->bed->elf_hash_symbol) (h))
6226 return true;
6227
6228 name = h->root.root.string;
6229 if (h->versioned >= versioned)
6230 {
6231 char *p = strchr (name, ELF_VER_CHR);
6232 if (p != NULL)
6233 {
6234 alc = (char *) bfd_malloc (p - name + 1);
6235 if (alc == NULL)
6236 {
6237 s->error = true;
6238 return false;
6239 }
6240 memcpy (alc, name, p - name);
6241 alc[p - name] = '\0';
6242 name = alc;
6243 }
6244 }
6245
6246 /* Compute the hash value. */
6247 ha = bfd_elf_gnu_hash (name);
6248
6249 /* Store the found hash value in the array for compute_bucket_count,
6250 and also for .dynsym reordering purposes. */
6251 s->hashcodes[s->nsyms] = ha;
6252 s->hashval[h->dynindx] = ha;
6253 ++s->nsyms;
6254 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6255 s->min_dynindx = h->dynindx;
6256
6257 free (alc);
6258 return true;
6259 }
6260
6261 /* This function will be called though elf_link_hash_traverse to do
6262 final dynamic symbol renumbering in case of .gnu.hash.
6263 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6264 to the translation table. */
6265
6266 static bool
elf_gnu_hash_process_symidx(struct elf_link_hash_entry * h,void * data)6267 elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
6268 {
6269 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
6270 unsigned long int bucket;
6271 unsigned long int val;
6272
6273 /* Ignore indirect symbols. */
6274 if (h->dynindx == -1)
6275 return true;
6276
6277 /* Ignore also local symbols and undefined symbols. */
6278 if (! (*s->bed->elf_hash_symbol) (h))
6279 {
6280 if (h->dynindx >= s->min_dynindx)
6281 {
6282 if (s->bed->record_xhash_symbol != NULL)
6283 {
6284 (*s->bed->record_xhash_symbol) (h, 0);
6285 s->local_indx++;
6286 }
6287 else
6288 h->dynindx = s->local_indx++;
6289 }
6290 return true;
6291 }
6292
6293 bucket = s->hashval[h->dynindx] % s->bucketcount;
6294 val = (s->hashval[h->dynindx] >> s->shift1)
6295 & ((s->maskbits >> s->shift1) - 1);
6296 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6297 s->bitmask[val]
6298 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6299 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6300 if (s->counts[bucket] == 1)
6301 /* Last element terminates the chain. */
6302 val |= 1;
6303 bfd_put_32 (s->output_bfd, val,
6304 s->contents + (s->indx[bucket] - s->symindx) * 4);
6305 --s->counts[bucket];
6306 if (s->bed->record_xhash_symbol != NULL)
6307 {
6308 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6309
6310 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6311 }
6312 else
6313 h->dynindx = s->indx[bucket]++;
6314 return true;
6315 }
6316
6317 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6318
6319 bool
_bfd_elf_hash_symbol(struct elf_link_hash_entry * h)6320 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6321 {
6322 return !(h->forced_local
6323 || h->root.type == bfd_link_hash_undefined
6324 || h->root.type == bfd_link_hash_undefweak
6325 || ((h->root.type == bfd_link_hash_defined
6326 || h->root.type == bfd_link_hash_defweak)
6327 && h->root.u.def.section->output_section == NULL));
6328 }
6329
6330 /* Array used to determine the number of hash table buckets to use
6331 based on the number of symbols there are. If there are fewer than
6332 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6333 fewer than 37 we use 17 buckets, and so forth. We never use more
6334 than 32771 buckets. */
6335
6336 static const size_t elf_buckets[] =
6337 {
6338 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6339 16411, 32771, 0
6340 };
6341
6342 /* Compute bucket count for hashing table. We do not use a static set
6343 of possible tables sizes anymore. Instead we determine for all
6344 possible reasonable sizes of the table the outcome (i.e., the
6345 number of collisions etc) and choose the best solution. The
6346 weighting functions are not too simple to allow the table to grow
6347 without bounds. Instead one of the weighting factors is the size.
6348 Therefore the result is always a good payoff between few collisions
6349 (= short chain lengths) and table size. */
6350 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)6351 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6352 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6353 unsigned long int nsyms,
6354 int gnu_hash)
6355 {
6356 size_t best_size = 0;
6357 unsigned long int i;
6358
6359 if (info->optimize)
6360 {
6361 size_t minsize;
6362 size_t maxsize;
6363 uint64_t best_chlen = ~((uint64_t) 0);
6364 bfd *dynobj = elf_hash_table (info)->dynobj;
6365 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6366 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
6367 unsigned long int *counts;
6368 bfd_size_type amt;
6369 unsigned int no_improvement_count = 0;
6370
6371 /* Possible optimization parameters: if we have NSYMS symbols we say
6372 that the hashing table must at least have NSYMS/4 and at most
6373 2*NSYMS buckets. */
6374 minsize = nsyms / 4;
6375 if (minsize == 0)
6376 minsize = 1;
6377 best_size = maxsize = nsyms * 2;
6378 if (gnu_hash)
6379 {
6380 if (minsize < 2)
6381 minsize = 2;
6382 if ((best_size & 31) == 0)
6383 ++best_size;
6384 }
6385
6386 /* Create array where we count the collisions in. We must use bfd_malloc
6387 since the size could be large. */
6388 amt = maxsize;
6389 amt *= sizeof (unsigned long int);
6390 counts = (unsigned long int *) bfd_malloc (amt);
6391 if (counts == NULL)
6392 return 0;
6393
6394 /* Compute the "optimal" size for the hash table. The criteria is a
6395 minimal chain length. The minor criteria is (of course) the size
6396 of the table. */
6397 for (i = minsize; i < maxsize; ++i)
6398 {
6399 /* Walk through the array of hashcodes and count the collisions. */
6400 uint64_t max;
6401 unsigned long int j;
6402 unsigned long int fact;
6403
6404 if (gnu_hash && (i & 31) == 0)
6405 continue;
6406
6407 memset (counts, '\0', i * sizeof (unsigned long int));
6408
6409 /* Determine how often each hash bucket is used. */
6410 for (j = 0; j < nsyms; ++j)
6411 ++counts[hashcodes[j] % i];
6412
6413 /* For the weight function we need some information about the
6414 pagesize on the target. This is information need not be 100%
6415 accurate. Since this information is not available (so far) we
6416 define it here to a reasonable default value. If it is crucial
6417 to have a better value some day simply define this value. */
6418 # ifndef BFD_TARGET_PAGESIZE
6419 # define BFD_TARGET_PAGESIZE (4096)
6420 # endif
6421
6422 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6423 and the chains. */
6424 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6425
6426 # if 1
6427 /* Variant 1: optimize for short chains. We add the squares
6428 of all the chain lengths (which favors many small chain
6429 over a few long chains). */
6430 for (j = 0; j < i; ++j)
6431 max += counts[j] * counts[j];
6432
6433 /* This adds penalties for the overall size of the table. */
6434 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6435 max *= fact * fact;
6436 # else
6437 /* Variant 2: Optimize a lot more for small table. Here we
6438 also add squares of the size but we also add penalties for
6439 empty slots (the +1 term). */
6440 for (j = 0; j < i; ++j)
6441 max += (1 + counts[j]) * (1 + counts[j]);
6442
6443 /* The overall size of the table is considered, but not as
6444 strong as in variant 1, where it is squared. */
6445 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6446 max *= fact;
6447 # endif
6448
6449 /* Compare with current best results. */
6450 if (max < best_chlen)
6451 {
6452 best_chlen = max;
6453 best_size = i;
6454 no_improvement_count = 0;
6455 }
6456 /* PR 11843: Avoid futile long searches for the best bucket size
6457 when there are a large number of symbols. */
6458 else if (++no_improvement_count == 100)
6459 break;
6460 }
6461
6462 free (counts);
6463 }
6464 else
6465 {
6466 for (i = 0; elf_buckets[i] != 0; i++)
6467 {
6468 best_size = elf_buckets[i];
6469 if (nsyms < elf_buckets[i + 1])
6470 break;
6471 }
6472 if (gnu_hash && best_size < 2)
6473 best_size = 2;
6474 }
6475
6476 return best_size;
6477 }
6478
6479 /* Size any SHT_GROUP section for ld -r. */
6480
6481 bool
_bfd_elf_size_group_sections(struct bfd_link_info * info)6482 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6483 {
6484 bfd *ibfd;
6485 asection *s;
6486
6487 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6488 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6489 && (s = ibfd->sections) != NULL
6490 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6491 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6492 return false;
6493 return true;
6494 }
6495
6496 /* Set a default stack segment size. The value in INFO wins. If it
6497 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6498 undefined it is initialized. */
6499
6500 bool
bfd_elf_stack_segment_size(bfd * output_bfd,struct bfd_link_info * info,const char * legacy_symbol,bfd_vma default_size)6501 bfd_elf_stack_segment_size (bfd *output_bfd,
6502 struct bfd_link_info *info,
6503 const char *legacy_symbol,
6504 bfd_vma default_size)
6505 {
6506 struct elf_link_hash_entry *h = NULL;
6507
6508 /* Look for legacy symbol. */
6509 if (legacy_symbol)
6510 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6511 false, false, false);
6512 if (h && (h->root.type == bfd_link_hash_defined
6513 || h->root.type == bfd_link_hash_defweak)
6514 && h->def_regular
6515 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6516 {
6517 /* The symbol has no type if specified on the command line. */
6518 h->type = STT_OBJECT;
6519 if (info->stacksize)
6520 /* xgettext:c-format */
6521 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6522 output_bfd, legacy_symbol);
6523 else if (h->root.u.def.section != bfd_abs_section_ptr)
6524 /* xgettext:c-format */
6525 _bfd_error_handler (_("%pB: %s not absolute"),
6526 output_bfd, legacy_symbol);
6527 else
6528 info->stacksize = h->root.u.def.value;
6529 }
6530
6531 if (!info->stacksize)
6532 /* If the user didn't set a size, or explicitly inhibit the
6533 size, set it now. */
6534 info->stacksize = default_size;
6535
6536 /* Provide the legacy symbol, if it is referenced. */
6537 if (h && (h->root.type == bfd_link_hash_undefined
6538 || h->root.type == bfd_link_hash_undefweak))
6539 {
6540 struct bfd_link_hash_entry *bh = NULL;
6541
6542 if (!(_bfd_generic_link_add_one_symbol
6543 (info, output_bfd, legacy_symbol,
6544 BSF_GLOBAL, bfd_abs_section_ptr,
6545 info->stacksize >= 0 ? info->stacksize : 0,
6546 NULL, false, get_elf_backend_data (output_bfd)->collect, &bh)))
6547 return false;
6548
6549 h = (struct elf_link_hash_entry *) bh;
6550 h->def_regular = 1;
6551 h->type = STT_OBJECT;
6552 }
6553
6554 return true;
6555 }
6556
6557 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6558
6559 struct elf_gc_sweep_symbol_info
6560 {
6561 struct bfd_link_info *info;
6562 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6563 bool);
6564 };
6565
6566 static bool
elf_gc_sweep_symbol(struct elf_link_hash_entry * h,void * data)6567 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6568 {
6569 if (!h->mark
6570 && (((h->root.type == bfd_link_hash_defined
6571 || h->root.type == bfd_link_hash_defweak)
6572 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6573 && h->root.u.def.section->gc_mark))
6574 || h->root.type == bfd_link_hash_undefined
6575 || h->root.type == bfd_link_hash_undefweak))
6576 {
6577 struct elf_gc_sweep_symbol_info *inf;
6578
6579 inf = (struct elf_gc_sweep_symbol_info *) data;
6580 (*inf->hide_symbol) (inf->info, h, true);
6581 h->def_regular = 0;
6582 h->ref_regular = 0;
6583 h->ref_regular_nonweak = 0;
6584 }
6585
6586 return true;
6587 }
6588
6589 /* Set up the sizes and contents of the ELF dynamic sections. This is
6590 called by the ELF linker emulation before_allocation routine. We
6591 must set the sizes of the sections before the linker sets the
6592 addresses of the various sections. */
6593
6594 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)6595 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6596 const char *soname,
6597 const char *rpath,
6598 const char *filter_shlib,
6599 const char *audit,
6600 const char *depaudit,
6601 const char * const *auxiliary_filters,
6602 struct bfd_link_info *info,
6603 asection **sinterpptr)
6604 {
6605 bfd *dynobj;
6606 const struct elf_backend_data *bed;
6607
6608 *sinterpptr = NULL;
6609
6610 if (!is_elf_hash_table (info->hash))
6611 return true;
6612
6613 /* Any syms created from now on start with -1 in
6614 got.refcount/offset and plt.refcount/offset. */
6615 elf_hash_table (info)->init_got_refcount
6616 = elf_hash_table (info)->init_got_offset;
6617 elf_hash_table (info)->init_plt_refcount
6618 = elf_hash_table (info)->init_plt_offset;
6619
6620 bed = get_elf_backend_data (output_bfd);
6621
6622 /* The backend may have to create some sections regardless of whether
6623 we're dynamic or not. */
6624 if (bed->elf_backend_always_size_sections
6625 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6626 return false;
6627
6628 dynobj = elf_hash_table (info)->dynobj;
6629
6630 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6631 {
6632 struct bfd_elf_version_tree *verdefs;
6633 struct elf_info_failed asvinfo;
6634 struct bfd_elf_version_tree *t;
6635 struct bfd_elf_version_expr *d;
6636 asection *s;
6637 size_t soname_indx;
6638
6639 /* If we are supposed to export all symbols into the dynamic symbol
6640 table (this is not the normal case), then do so. */
6641 if (info->export_dynamic
6642 || (bfd_link_executable (info) && info->dynamic))
6643 {
6644 struct elf_info_failed eif;
6645
6646 eif.info = info;
6647 eif.failed = false;
6648 elf_link_hash_traverse (elf_hash_table (info),
6649 _bfd_elf_export_symbol,
6650 &eif);
6651 if (eif.failed)
6652 return false;
6653 }
6654
6655 if (soname != NULL)
6656 {
6657 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6658 soname, true);
6659 if (soname_indx == (size_t) -1
6660 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6661 return false;
6662 }
6663 else
6664 soname_indx = (size_t) -1;
6665
6666 /* Make all global versions with definition. */
6667 for (t = info->version_info; t != NULL; t = t->next)
6668 for (d = t->globals.list; d != NULL; d = d->next)
6669 if (!d->symver && d->literal)
6670 {
6671 const char *verstr, *name;
6672 size_t namelen, verlen, newlen;
6673 char *newname, *p, leading_char;
6674 struct elf_link_hash_entry *newh;
6675
6676 leading_char = bfd_get_symbol_leading_char (output_bfd);
6677 name = d->pattern;
6678 namelen = strlen (name) + (leading_char != '\0');
6679 verstr = t->name;
6680 verlen = strlen (verstr);
6681 newlen = namelen + verlen + 3;
6682
6683 newname = (char *) bfd_malloc (newlen);
6684 if (newname == NULL)
6685 return false;
6686 newname[0] = leading_char;
6687 memcpy (newname + (leading_char != '\0'), name, namelen);
6688
6689 /* Check the hidden versioned definition. */
6690 p = newname + namelen;
6691 *p++ = ELF_VER_CHR;
6692 memcpy (p, verstr, verlen + 1);
6693 newh = elf_link_hash_lookup (elf_hash_table (info),
6694 newname, false, false,
6695 false);
6696 if (newh == NULL
6697 || (newh->root.type != bfd_link_hash_defined
6698 && newh->root.type != bfd_link_hash_defweak))
6699 {
6700 /* Check the default versioned definition. */
6701 *p++ = ELF_VER_CHR;
6702 memcpy (p, verstr, verlen + 1);
6703 newh = elf_link_hash_lookup (elf_hash_table (info),
6704 newname, false, false,
6705 false);
6706 }
6707 free (newname);
6708
6709 /* Mark this version if there is a definition and it is
6710 not defined in a shared object. */
6711 if (newh != NULL
6712 && !newh->def_dynamic
6713 && (newh->root.type == bfd_link_hash_defined
6714 || newh->root.type == bfd_link_hash_defweak))
6715 d->symver = 1;
6716 }
6717
6718 /* Attach all the symbols to their version information. */
6719 asvinfo.info = info;
6720 asvinfo.failed = false;
6721
6722 elf_link_hash_traverse (elf_hash_table (info),
6723 _bfd_elf_link_assign_sym_version,
6724 &asvinfo);
6725 if (asvinfo.failed)
6726 return false;
6727
6728 if (!info->allow_undefined_version)
6729 {
6730 /* Check if all global versions have a definition. */
6731 bool all_defined = true;
6732 for (t = info->version_info; t != NULL; t = t->next)
6733 for (d = t->globals.list; d != NULL; d = d->next)
6734 if (d->literal && !d->symver && !d->script)
6735 {
6736 _bfd_error_handler
6737 (_("%s: undefined version: %s"),
6738 d->pattern, t->name);
6739 all_defined = false;
6740 }
6741
6742 if (!all_defined)
6743 {
6744 bfd_set_error (bfd_error_bad_value);
6745 return false;
6746 }
6747 }
6748
6749 /* Set up the version definition section. */
6750 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6751 BFD_ASSERT (s != NULL);
6752
6753 /* We may have created additional version definitions if we are
6754 just linking a regular application. */
6755 verdefs = info->version_info;
6756
6757 /* Skip anonymous version tag. */
6758 if (verdefs != NULL && verdefs->vernum == 0)
6759 verdefs = verdefs->next;
6760
6761 if (verdefs == NULL && !info->create_default_symver)
6762 s->flags |= SEC_EXCLUDE;
6763 else
6764 {
6765 unsigned int cdefs;
6766 bfd_size_type size;
6767 bfd_byte *p;
6768 Elf_Internal_Verdef def;
6769 Elf_Internal_Verdaux defaux;
6770 struct bfd_link_hash_entry *bh;
6771 struct elf_link_hash_entry *h;
6772 const char *name;
6773
6774 cdefs = 0;
6775 size = 0;
6776
6777 /* Make space for the base version. */
6778 size += sizeof (Elf_External_Verdef);
6779 size += sizeof (Elf_External_Verdaux);
6780 ++cdefs;
6781
6782 /* Make space for the default version. */
6783 if (info->create_default_symver)
6784 {
6785 size += sizeof (Elf_External_Verdef);
6786 ++cdefs;
6787 }
6788
6789 for (t = verdefs; t != NULL; t = t->next)
6790 {
6791 struct bfd_elf_version_deps *n;
6792
6793 /* Don't emit base version twice. */
6794 if (t->vernum == 0)
6795 continue;
6796
6797 size += sizeof (Elf_External_Verdef);
6798 size += sizeof (Elf_External_Verdaux);
6799 ++cdefs;
6800
6801 for (n = t->deps; n != NULL; n = n->next)
6802 size += sizeof (Elf_External_Verdaux);
6803 }
6804
6805 s->size = size;
6806 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6807 if (s->contents == NULL && s->size != 0)
6808 return false;
6809
6810 /* Fill in the version definition section. */
6811
6812 p = s->contents;
6813
6814 def.vd_version = VER_DEF_CURRENT;
6815 def.vd_flags = VER_FLG_BASE;
6816 def.vd_ndx = 1;
6817 def.vd_cnt = 1;
6818 if (info->create_default_symver)
6819 {
6820 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6821 def.vd_next = sizeof (Elf_External_Verdef);
6822 }
6823 else
6824 {
6825 def.vd_aux = sizeof (Elf_External_Verdef);
6826 def.vd_next = (sizeof (Elf_External_Verdef)
6827 + sizeof (Elf_External_Verdaux));
6828 }
6829
6830 if (soname_indx != (size_t) -1)
6831 {
6832 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6833 soname_indx);
6834 def.vd_hash = bfd_elf_hash (soname);
6835 defaux.vda_name = soname_indx;
6836 name = soname;
6837 }
6838 else
6839 {
6840 size_t indx;
6841
6842 name = lbasename (bfd_get_filename (output_bfd));
6843 def.vd_hash = bfd_elf_hash (name);
6844 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6845 name, false);
6846 if (indx == (size_t) -1)
6847 return false;
6848 defaux.vda_name = indx;
6849 }
6850 defaux.vda_next = 0;
6851
6852 _bfd_elf_swap_verdef_out (output_bfd, &def,
6853 (Elf_External_Verdef *) p);
6854 p += sizeof (Elf_External_Verdef);
6855 if (info->create_default_symver)
6856 {
6857 /* Add a symbol representing this version. */
6858 bh = NULL;
6859 if (! (_bfd_generic_link_add_one_symbol
6860 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6861 0, NULL, false,
6862 get_elf_backend_data (dynobj)->collect, &bh)))
6863 return false;
6864 h = (struct elf_link_hash_entry *) bh;
6865 h->non_elf = 0;
6866 h->def_regular = 1;
6867 h->type = STT_OBJECT;
6868 h->verinfo.vertree = NULL;
6869
6870 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6871 return false;
6872
6873 /* Create a duplicate of the base version with the same
6874 aux block, but different flags. */
6875 def.vd_flags = 0;
6876 def.vd_ndx = 2;
6877 def.vd_aux = sizeof (Elf_External_Verdef);
6878 if (verdefs)
6879 def.vd_next = (sizeof (Elf_External_Verdef)
6880 + sizeof (Elf_External_Verdaux));
6881 else
6882 def.vd_next = 0;
6883 _bfd_elf_swap_verdef_out (output_bfd, &def,
6884 (Elf_External_Verdef *) p);
6885 p += sizeof (Elf_External_Verdef);
6886 }
6887 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6888 (Elf_External_Verdaux *) p);
6889 p += sizeof (Elf_External_Verdaux);
6890
6891 for (t = verdefs; t != NULL; t = t->next)
6892 {
6893 unsigned int cdeps;
6894 struct bfd_elf_version_deps *n;
6895
6896 /* Don't emit the base version twice. */
6897 if (t->vernum == 0)
6898 continue;
6899
6900 cdeps = 0;
6901 for (n = t->deps; n != NULL; n = n->next)
6902 ++cdeps;
6903
6904 /* Add a symbol representing this version. */
6905 bh = NULL;
6906 if (! (_bfd_generic_link_add_one_symbol
6907 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6908 0, NULL, false,
6909 get_elf_backend_data (dynobj)->collect, &bh)))
6910 return false;
6911 h = (struct elf_link_hash_entry *) bh;
6912 h->non_elf = 0;
6913 h->def_regular = 1;
6914 h->type = STT_OBJECT;
6915 h->verinfo.vertree = t;
6916
6917 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6918 return false;
6919
6920 def.vd_version = VER_DEF_CURRENT;
6921 def.vd_flags = 0;
6922 if (t->globals.list == NULL
6923 && t->locals.list == NULL
6924 && ! t->used)
6925 def.vd_flags |= VER_FLG_WEAK;
6926 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6927 def.vd_cnt = cdeps + 1;
6928 def.vd_hash = bfd_elf_hash (t->name);
6929 def.vd_aux = sizeof (Elf_External_Verdef);
6930 def.vd_next = 0;
6931
6932 /* If a basever node is next, it *must* be the last node in
6933 the chain, otherwise Verdef construction breaks. */
6934 if (t->next != NULL && t->next->vernum == 0)
6935 BFD_ASSERT (t->next->next == NULL);
6936
6937 if (t->next != NULL && t->next->vernum != 0)
6938 def.vd_next = (sizeof (Elf_External_Verdef)
6939 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6940
6941 _bfd_elf_swap_verdef_out (output_bfd, &def,
6942 (Elf_External_Verdef *) p);
6943 p += sizeof (Elf_External_Verdef);
6944
6945 defaux.vda_name = h->dynstr_index;
6946 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6947 h->dynstr_index);
6948 defaux.vda_next = 0;
6949 if (t->deps != NULL)
6950 defaux.vda_next = sizeof (Elf_External_Verdaux);
6951 t->name_indx = defaux.vda_name;
6952
6953 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6954 (Elf_External_Verdaux *) p);
6955 p += sizeof (Elf_External_Verdaux);
6956
6957 for (n = t->deps; n != NULL; n = n->next)
6958 {
6959 if (n->version_needed == NULL)
6960 {
6961 /* This can happen if there was an error in the
6962 version script. */
6963 defaux.vda_name = 0;
6964 }
6965 else
6966 {
6967 defaux.vda_name = n->version_needed->name_indx;
6968 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6969 defaux.vda_name);
6970 }
6971 if (n->next == NULL)
6972 defaux.vda_next = 0;
6973 else
6974 defaux.vda_next = sizeof (Elf_External_Verdaux);
6975
6976 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6977 (Elf_External_Verdaux *) p);
6978 p += sizeof (Elf_External_Verdaux);
6979 }
6980 }
6981
6982 elf_tdata (output_bfd)->cverdefs = cdefs;
6983 }
6984 }
6985
6986 if (info->gc_sections && bed->can_gc_sections)
6987 {
6988 struct elf_gc_sweep_symbol_info sweep_info;
6989
6990 /* Remove the symbols that were in the swept sections from the
6991 dynamic symbol table. */
6992 sweep_info.info = info;
6993 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6994 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6995 &sweep_info);
6996 }
6997
6998 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6999 {
7000 asection *s;
7001 struct elf_find_verdep_info sinfo;
7002
7003 /* Work out the size of the version reference section. */
7004
7005 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
7006 BFD_ASSERT (s != NULL);
7007
7008 sinfo.info = info;
7009 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
7010 if (sinfo.vers == 0)
7011 sinfo.vers = 1;
7012 sinfo.failed = false;
7013
7014 elf_link_hash_traverse (elf_hash_table (info),
7015 _bfd_elf_link_find_version_dependencies,
7016 &sinfo);
7017 if (sinfo.failed)
7018 return false;
7019
7020 if (info->enable_dt_relr)
7021 {
7022 elf_link_add_dt_relr_dependency (&sinfo);
7023 if (sinfo.failed)
7024 return false;
7025 }
7026
7027 if (elf_tdata (output_bfd)->verref == NULL)
7028 s->flags |= SEC_EXCLUDE;
7029 else
7030 {
7031 Elf_Internal_Verneed *vn;
7032 unsigned int size;
7033 unsigned int crefs;
7034 bfd_byte *p;
7035
7036 /* Build the version dependency section. */
7037 size = 0;
7038 crefs = 0;
7039 for (vn = elf_tdata (output_bfd)->verref;
7040 vn != NULL;
7041 vn = vn->vn_nextref)
7042 {
7043 Elf_Internal_Vernaux *a;
7044
7045 size += sizeof (Elf_External_Verneed);
7046 ++crefs;
7047 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7048 size += sizeof (Elf_External_Vernaux);
7049 }
7050
7051 s->size = size;
7052 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7053 if (s->contents == NULL)
7054 return false;
7055
7056 p = s->contents;
7057 for (vn = elf_tdata (output_bfd)->verref;
7058 vn != NULL;
7059 vn = vn->vn_nextref)
7060 {
7061 unsigned int caux;
7062 Elf_Internal_Vernaux *a;
7063 size_t indx;
7064
7065 caux = 0;
7066 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7067 ++caux;
7068
7069 vn->vn_version = VER_NEED_CURRENT;
7070 vn->vn_cnt = caux;
7071 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7072 elf_dt_name (vn->vn_bfd) != NULL
7073 ? elf_dt_name (vn->vn_bfd)
7074 : lbasename (bfd_get_filename
7075 (vn->vn_bfd)),
7076 false);
7077 if (indx == (size_t) -1)
7078 return false;
7079 vn->vn_file = indx;
7080 vn->vn_aux = sizeof (Elf_External_Verneed);
7081 if (vn->vn_nextref == NULL)
7082 vn->vn_next = 0;
7083 else
7084 vn->vn_next = (sizeof (Elf_External_Verneed)
7085 + caux * sizeof (Elf_External_Vernaux));
7086
7087 _bfd_elf_swap_verneed_out (output_bfd, vn,
7088 (Elf_External_Verneed *) p);
7089 p += sizeof (Elf_External_Verneed);
7090
7091 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7092 {
7093 a->vna_hash = bfd_elf_hash (a->vna_nodename);
7094 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7095 a->vna_nodename, false);
7096 if (indx == (size_t) -1)
7097 return false;
7098 a->vna_name = indx;
7099 if (a->vna_nextptr == NULL)
7100 a->vna_next = 0;
7101 else
7102 a->vna_next = sizeof (Elf_External_Vernaux);
7103
7104 _bfd_elf_swap_vernaux_out (output_bfd, a,
7105 (Elf_External_Vernaux *) p);
7106 p += sizeof (Elf_External_Vernaux);
7107 }
7108 }
7109
7110 elf_tdata (output_bfd)->cverrefs = crefs;
7111 }
7112 }
7113
7114 if (bfd_link_relocatable (info)
7115 && !_bfd_elf_size_group_sections (info))
7116 return false;
7117
7118 /* Determine any GNU_STACK segment requirements, after the backend
7119 has had a chance to set a default segment size. */
7120 if (info->execstack)
7121 {
7122 /* If the user has explicitly requested warnings, then generate one even
7123 though the choice is the result of another command line option. */
7124 if (info->warn_execstack == 1)
7125 _bfd_error_handler
7126 (_("\
7127 warning: enabling an executable stack because of -z execstack command line option"));
7128 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
7129 }
7130 else if (info->noexecstack)
7131 elf_stack_flags (output_bfd) = PF_R | PF_W;
7132 else
7133 {
7134 bfd *inputobj;
7135 asection *notesec = NULL;
7136 bfd *noteobj = NULL;
7137 bfd *emptyobj = NULL;
7138 int exec = 0;
7139
7140 for (inputobj = info->input_bfds;
7141 inputobj;
7142 inputobj = inputobj->link.next)
7143 {
7144 asection *s;
7145
7146 if (inputobj->flags
7147 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
7148 continue;
7149 s = inputobj->sections;
7150 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7151 continue;
7152
7153 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
7154 if (s)
7155 {
7156 notesec = s;
7157 if (s->flags & SEC_CODE)
7158 {
7159 noteobj = inputobj;
7160 exec = PF_X;
7161 /* There is no point in scanning the remaining bfds. */
7162 break;
7163 }
7164 }
7165 else if (bed->default_execstack && info->default_execstack)
7166 {
7167 exec = PF_X;
7168 emptyobj = inputobj;
7169 }
7170 }
7171
7172 if (notesec || info->stacksize > 0)
7173 {
7174 if (exec)
7175 {
7176 if (info->warn_execstack != 0)
7177 {
7178 /* PR 29072: Because an executable stack is a serious
7179 security risk, make sure that the user knows that it is
7180 being enabled despite the fact that it was not requested
7181 on the command line. */
7182 if (noteobj)
7183 _bfd_error_handler (_("\
7184 warning: %s: requires executable stack (because the .note.GNU-stack section is executable)"),
7185 bfd_get_filename (noteobj));
7186 else if (emptyobj)
7187 {
7188 _bfd_error_handler (_("\
7189 warning: %s: missing .note.GNU-stack section implies executable stack"),
7190 bfd_get_filename (emptyobj));
7191 _bfd_error_handler (_("\
7192 NOTE: This behaviour is deprecated and will be removed in a future version of the linker"));
7193 }
7194 }
7195 }
7196 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
7197 }
7198
7199 if (notesec && exec && bfd_link_relocatable (info)
7200 && notesec->output_section != bfd_abs_section_ptr)
7201 notesec->output_section->flags |= SEC_CODE;
7202 }
7203
7204 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7205 {
7206 struct elf_info_failed eif;
7207 struct elf_link_hash_entry *h;
7208 asection *dynstr;
7209 asection *s;
7210
7211 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
7212 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
7213
7214 if (info->symbolic)
7215 {
7216 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
7217 return false;
7218 info->flags |= DF_SYMBOLIC;
7219 }
7220
7221 if (rpath != NULL)
7222 {
7223 size_t indx;
7224 bfd_vma tag;
7225
7226 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
7227 true);
7228 if (indx == (size_t) -1)
7229 return false;
7230
7231 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
7232 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
7233 return false;
7234 }
7235
7236 if (filter_shlib != NULL)
7237 {
7238 size_t indx;
7239
7240 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7241 filter_shlib, true);
7242 if (indx == (size_t) -1
7243 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
7244 return false;
7245 }
7246
7247 if (auxiliary_filters != NULL)
7248 {
7249 const char * const *p;
7250
7251 for (p = auxiliary_filters; *p != NULL; p++)
7252 {
7253 size_t indx;
7254
7255 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7256 *p, true);
7257 if (indx == (size_t) -1
7258 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7259 return false;
7260 }
7261 }
7262
7263 if (audit != NULL)
7264 {
7265 size_t indx;
7266
7267 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7268 true);
7269 if (indx == (size_t) -1
7270 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7271 return false;
7272 }
7273
7274 if (depaudit != NULL)
7275 {
7276 size_t indx;
7277
7278 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7279 true);
7280 if (indx == (size_t) -1
7281 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7282 return false;
7283 }
7284
7285 eif.info = info;
7286 eif.failed = false;
7287
7288 /* Find all symbols which were defined in a dynamic object and make
7289 the backend pick a reasonable value for them. */
7290 elf_link_hash_traverse (elf_hash_table (info),
7291 _bfd_elf_adjust_dynamic_symbol,
7292 &eif);
7293 if (eif.failed)
7294 return false;
7295
7296 /* Add some entries to the .dynamic section. We fill in some of the
7297 values later, in bfd_elf_final_link, but we must add the entries
7298 now so that we know the final size of the .dynamic section. */
7299
7300 /* If there are initialization and/or finalization functions to
7301 call then add the corresponding DT_INIT/DT_FINI entries. */
7302 h = (info->init_function
7303 ? elf_link_hash_lookup (elf_hash_table (info),
7304 info->init_function, false,
7305 false, false)
7306 : NULL);
7307 if (h != NULL
7308 && (h->ref_regular
7309 || h->def_regular))
7310 {
7311 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7312 return false;
7313 }
7314 h = (info->fini_function
7315 ? elf_link_hash_lookup (elf_hash_table (info),
7316 info->fini_function, false,
7317 false, false)
7318 : NULL);
7319 if (h != NULL
7320 && (h->ref_regular
7321 || h->def_regular))
7322 {
7323 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7324 return false;
7325 }
7326
7327 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7328 if (s != NULL && s->linker_has_input)
7329 {
7330 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7331 if (! bfd_link_executable (info))
7332 {
7333 bfd *sub;
7334 asection *o;
7335
7336 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7337 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7338 && (o = sub->sections) != NULL
7339 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
7340 for (o = sub->sections; o != NULL; o = o->next)
7341 if (elf_section_data (o)->this_hdr.sh_type
7342 == SHT_PREINIT_ARRAY)
7343 {
7344 _bfd_error_handler
7345 (_("%pB: .preinit_array section is not allowed in DSO"),
7346 sub);
7347 break;
7348 }
7349
7350 bfd_set_error (bfd_error_nonrepresentable_section);
7351 return false;
7352 }
7353
7354 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7355 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7356 return false;
7357 }
7358 s = bfd_get_section_by_name (output_bfd, ".init_array");
7359 if (s != NULL && s->linker_has_input)
7360 {
7361 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7362 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7363 return false;
7364 }
7365 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7366 if (s != NULL && s->linker_has_input)
7367 {
7368 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7369 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7370 return false;
7371 }
7372
7373 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7374 /* If .dynstr is excluded from the link, we don't want any of
7375 these tags. Strictly, we should be checking each section
7376 individually; This quick check covers for the case where
7377 someone does a /DISCARD/ : { *(*) }. */
7378 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7379 {
7380 bfd_size_type strsize;
7381
7382 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7383 if ((info->emit_hash
7384 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7385 || (info->emit_gnu_hash
7386 && (bed->record_xhash_symbol == NULL
7387 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7388 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7389 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7390 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7391 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7392 bed->s->sizeof_sym)
7393 || (info->gnu_flags_1
7394 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1,
7395 info->gnu_flags_1)))
7396 return false;
7397 }
7398 }
7399
7400 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7401 return false;
7402
7403 /* The backend must work out the sizes of all the other dynamic
7404 sections. */
7405 if (dynobj != NULL
7406 && bed->elf_backend_size_dynamic_sections != NULL
7407 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
7408 return false;
7409
7410 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7411 {
7412 if (elf_tdata (output_bfd)->cverdefs)
7413 {
7414 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7415
7416 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7417 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7418 return false;
7419 }
7420
7421 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7422 {
7423 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7424 return false;
7425 }
7426 else if (info->flags & DF_BIND_NOW)
7427 {
7428 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7429 return false;
7430 }
7431
7432 if (info->flags_1)
7433 {
7434 if (bfd_link_executable (info))
7435 info->flags_1 &= ~ (DF_1_INITFIRST
7436 | DF_1_NODELETE
7437 | DF_1_NOOPEN);
7438 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7439 return false;
7440 }
7441
7442 if (elf_tdata (output_bfd)->cverrefs)
7443 {
7444 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7445
7446 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7447 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7448 return false;
7449 }
7450
7451 if ((elf_tdata (output_bfd)->cverrefs == 0
7452 && elf_tdata (output_bfd)->cverdefs == 0)
7453 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7454 {
7455 asection *s;
7456
7457 s = bfd_get_linker_section (dynobj, ".gnu.version");
7458 s->flags |= SEC_EXCLUDE;
7459 }
7460 }
7461 return true;
7462 }
7463
7464 /* Find the first non-excluded output section. We'll use its
7465 section symbol for some emitted relocs. */
7466 void
_bfd_elf_init_1_index_section(bfd * output_bfd,struct bfd_link_info * info)7467 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7468 {
7469 asection *s;
7470 asection *found = NULL;
7471
7472 for (s = output_bfd->sections; s != NULL; s = s->next)
7473 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7474 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7475 {
7476 found = s;
7477 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7478 break;
7479 }
7480 elf_hash_table (info)->text_index_section = found;
7481 }
7482
7483 /* Find two non-excluded output sections, one for code, one for data.
7484 We'll use their section symbols for some emitted relocs. */
7485 void
_bfd_elf_init_2_index_sections(bfd * output_bfd,struct bfd_link_info * info)7486 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7487 {
7488 asection *s;
7489 asection *found = NULL;
7490
7491 /* Data first, since setting text_index_section changes
7492 _bfd_elf_omit_section_dynsym_default. */
7493 for (s = output_bfd->sections; s != NULL; s = s->next)
7494 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7495 && !(s->flags & SEC_READONLY)
7496 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7497 {
7498 found = s;
7499 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7500 break;
7501 }
7502 elf_hash_table (info)->data_index_section = found;
7503
7504 for (s = output_bfd->sections; s != NULL; s = s->next)
7505 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7506 && (s->flags & SEC_READONLY)
7507 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7508 {
7509 found = s;
7510 break;
7511 }
7512 elf_hash_table (info)->text_index_section = found;
7513 }
7514
7515 #define GNU_HASH_SECTION_NAME(bed) \
7516 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7517
7518 bool
bfd_elf_size_dynsym_hash_dynstr(bfd * output_bfd,struct bfd_link_info * info)7519 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7520 {
7521 const struct elf_backend_data *bed;
7522 unsigned long section_sym_count;
7523 bfd_size_type dynsymcount = 0;
7524
7525 if (!is_elf_hash_table (info->hash))
7526 return true;
7527
7528 bed = get_elf_backend_data (output_bfd);
7529 (*bed->elf_backend_init_index_section) (output_bfd, info);
7530
7531 /* Assign dynsym indices. In a shared library we generate a section
7532 symbol for each output section, which come first. Next come all
7533 of the back-end allocated local dynamic syms, followed by the rest
7534 of the global symbols.
7535
7536 This is usually not needed for static binaries, however backends
7537 can request to always do it, e.g. the MIPS backend uses dynamic
7538 symbol counts to lay out GOT, which will be produced in the
7539 presence of GOT relocations even in static binaries (holding fixed
7540 data in that case, to satisfy those relocations). */
7541
7542 if (elf_hash_table (info)->dynamic_sections_created
7543 || bed->always_renumber_dynsyms)
7544 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7545 §ion_sym_count);
7546
7547 if (elf_hash_table (info)->dynamic_sections_created)
7548 {
7549 bfd *dynobj;
7550 asection *s;
7551 unsigned int dtagcount;
7552
7553 dynobj = elf_hash_table (info)->dynobj;
7554
7555 /* Work out the size of the symbol version section. */
7556 s = bfd_get_linker_section (dynobj, ".gnu.version");
7557 BFD_ASSERT (s != NULL);
7558 if ((s->flags & SEC_EXCLUDE) == 0)
7559 {
7560 s->size = dynsymcount * sizeof (Elf_External_Versym);
7561 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7562 if (s->contents == NULL)
7563 return false;
7564
7565 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7566 return false;
7567 }
7568
7569 /* Set the size of the .dynsym and .hash sections. We counted
7570 the number of dynamic symbols in elf_link_add_object_symbols.
7571 We will build the contents of .dynsym and .hash when we build
7572 the final symbol table, because until then we do not know the
7573 correct value to give the symbols. We built the .dynstr
7574 section as we went along in elf_link_add_object_symbols. */
7575 s = elf_hash_table (info)->dynsym;
7576 BFD_ASSERT (s != NULL);
7577 s->size = dynsymcount * bed->s->sizeof_sym;
7578
7579 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7580 if (s->contents == NULL)
7581 return false;
7582
7583 /* The first entry in .dynsym is a dummy symbol. Clear all the
7584 section syms, in case we don't output them all. */
7585 ++section_sym_count;
7586 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7587
7588 elf_hash_table (info)->bucketcount = 0;
7589
7590 /* Compute the size of the hashing table. As a side effect this
7591 computes the hash values for all the names we export. */
7592 if (info->emit_hash)
7593 {
7594 unsigned long int *hashcodes;
7595 struct hash_codes_info hashinf;
7596 bfd_size_type amt;
7597 unsigned long int nsyms;
7598 size_t bucketcount;
7599 size_t hash_entry_size;
7600
7601 /* Compute the hash values for all exported symbols. At the same
7602 time store the values in an array so that we could use them for
7603 optimizations. */
7604 amt = dynsymcount * sizeof (unsigned long int);
7605 hashcodes = (unsigned long int *) bfd_malloc (amt);
7606 if (hashcodes == NULL)
7607 return false;
7608 hashinf.hashcodes = hashcodes;
7609 hashinf.error = false;
7610
7611 /* Put all hash values in HASHCODES. */
7612 elf_link_hash_traverse (elf_hash_table (info),
7613 elf_collect_hash_codes, &hashinf);
7614 if (hashinf.error)
7615 {
7616 free (hashcodes);
7617 return false;
7618 }
7619
7620 nsyms = hashinf.hashcodes - hashcodes;
7621 bucketcount
7622 = compute_bucket_count (info, hashcodes, nsyms, 0);
7623 free (hashcodes);
7624
7625 if (bucketcount == 0 && nsyms > 0)
7626 return false;
7627
7628 elf_hash_table (info)->bucketcount = bucketcount;
7629
7630 s = bfd_get_linker_section (dynobj, ".hash");
7631 BFD_ASSERT (s != NULL);
7632 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7633 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7634 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7635 if (s->contents == NULL)
7636 return false;
7637
7638 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7639 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7640 s->contents + hash_entry_size);
7641 }
7642
7643 if (info->emit_gnu_hash)
7644 {
7645 size_t i, cnt;
7646 unsigned char *contents;
7647 struct collect_gnu_hash_codes cinfo;
7648 bfd_size_type amt;
7649 size_t bucketcount;
7650
7651 memset (&cinfo, 0, sizeof (cinfo));
7652
7653 /* Compute the hash values for all exported symbols. At the same
7654 time store the values in an array so that we could use them for
7655 optimizations. */
7656 amt = dynsymcount * 2 * sizeof (unsigned long int);
7657 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7658 if (cinfo.hashcodes == NULL)
7659 return false;
7660
7661 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7662 cinfo.min_dynindx = -1;
7663 cinfo.output_bfd = output_bfd;
7664 cinfo.bed = bed;
7665
7666 /* Put all hash values in HASHCODES. */
7667 elf_link_hash_traverse (elf_hash_table (info),
7668 elf_collect_gnu_hash_codes, &cinfo);
7669 if (cinfo.error)
7670 {
7671 free (cinfo.hashcodes);
7672 return false;
7673 }
7674
7675 bucketcount
7676 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7677
7678 if (bucketcount == 0)
7679 {
7680 free (cinfo.hashcodes);
7681 return false;
7682 }
7683
7684 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
7685 BFD_ASSERT (s != NULL);
7686
7687 if (cinfo.nsyms == 0)
7688 {
7689 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7690 BFD_ASSERT (cinfo.min_dynindx == -1);
7691 free (cinfo.hashcodes);
7692 s->size = 5 * 4 + bed->s->arch_size / 8;
7693 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7694 if (contents == NULL)
7695 return false;
7696 s->contents = contents;
7697 /* 1 empty bucket. */
7698 bfd_put_32 (output_bfd, 1, contents);
7699 /* SYMIDX above the special symbol 0. */
7700 bfd_put_32 (output_bfd, 1, contents + 4);
7701 /* Just one word for bitmask. */
7702 bfd_put_32 (output_bfd, 1, contents + 8);
7703 /* Only hash fn bloom filter. */
7704 bfd_put_32 (output_bfd, 0, contents + 12);
7705 /* No hashes are valid - empty bitmask. */
7706 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7707 /* No hashes in the only bucket. */
7708 bfd_put_32 (output_bfd, 0,
7709 contents + 16 + bed->s->arch_size / 8);
7710 }
7711 else
7712 {
7713 unsigned long int maskwords, maskbitslog2, x;
7714 BFD_ASSERT (cinfo.min_dynindx != -1);
7715
7716 x = cinfo.nsyms;
7717 maskbitslog2 = 1;
7718 while ((x >>= 1) != 0)
7719 ++maskbitslog2;
7720 if (maskbitslog2 < 3)
7721 maskbitslog2 = 5;
7722 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7723 maskbitslog2 = maskbitslog2 + 3;
7724 else
7725 maskbitslog2 = maskbitslog2 + 2;
7726 if (bed->s->arch_size == 64)
7727 {
7728 if (maskbitslog2 == 5)
7729 maskbitslog2 = 6;
7730 cinfo.shift1 = 6;
7731 }
7732 else
7733 cinfo.shift1 = 5;
7734 cinfo.mask = (1 << cinfo.shift1) - 1;
7735 cinfo.shift2 = maskbitslog2;
7736 cinfo.maskbits = 1 << maskbitslog2;
7737 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7738 amt = bucketcount * sizeof (unsigned long int) * 2;
7739 amt += maskwords * sizeof (bfd_vma);
7740 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7741 if (cinfo.bitmask == NULL)
7742 {
7743 free (cinfo.hashcodes);
7744 return false;
7745 }
7746
7747 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7748 cinfo.indx = cinfo.counts + bucketcount;
7749 cinfo.symindx = dynsymcount - cinfo.nsyms;
7750 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7751
7752 /* Determine how often each hash bucket is used. */
7753 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7754 for (i = 0; i < cinfo.nsyms; ++i)
7755 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7756
7757 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7758 if (cinfo.counts[i] != 0)
7759 {
7760 cinfo.indx[i] = cnt;
7761 cnt += cinfo.counts[i];
7762 }
7763 BFD_ASSERT (cnt == dynsymcount);
7764 cinfo.bucketcount = bucketcount;
7765 cinfo.local_indx = cinfo.min_dynindx;
7766
7767 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7768 s->size += cinfo.maskbits / 8;
7769 if (bed->record_xhash_symbol != NULL)
7770 s->size += cinfo.nsyms * 4;
7771 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7772 if (contents == NULL)
7773 {
7774 free (cinfo.bitmask);
7775 free (cinfo.hashcodes);
7776 return false;
7777 }
7778
7779 s->contents = contents;
7780 bfd_put_32 (output_bfd, bucketcount, contents);
7781 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7782 bfd_put_32 (output_bfd, maskwords, contents + 8);
7783 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7784 contents += 16 + cinfo.maskbits / 8;
7785
7786 for (i = 0; i < bucketcount; ++i)
7787 {
7788 if (cinfo.counts[i] == 0)
7789 bfd_put_32 (output_bfd, 0, contents);
7790 else
7791 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7792 contents += 4;
7793 }
7794
7795 cinfo.contents = contents;
7796
7797 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7798 /* Renumber dynamic symbols, if populating .gnu.hash section.
7799 If using .MIPS.xhash, populate the translation table. */
7800 elf_link_hash_traverse (elf_hash_table (info),
7801 elf_gnu_hash_process_symidx, &cinfo);
7802
7803 contents = s->contents + 16;
7804 for (i = 0; i < maskwords; ++i)
7805 {
7806 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7807 contents);
7808 contents += bed->s->arch_size / 8;
7809 }
7810
7811 free (cinfo.bitmask);
7812 free (cinfo.hashcodes);
7813 }
7814 }
7815
7816 s = bfd_get_linker_section (dynobj, ".dynstr");
7817 BFD_ASSERT (s != NULL);
7818
7819 elf_finalize_dynstr (output_bfd, info);
7820
7821 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7822
7823 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7824 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7825 return false;
7826 }
7827
7828 return true;
7829 }
7830
7831 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7832
7833 static void
merge_sections_remove_hook(bfd * abfd ATTRIBUTE_UNUSED,asection * sec)7834 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7835 asection *sec)
7836 {
7837 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7838 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7839 }
7840
7841 /* Finish SHF_MERGE section merging. */
7842
7843 bool
_bfd_elf_merge_sections(bfd * obfd,struct bfd_link_info * info)7844 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7845 {
7846 bfd *ibfd;
7847 asection *sec;
7848
7849 if (!is_elf_hash_table (info->hash))
7850 return false;
7851
7852 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7853 if ((ibfd->flags & DYNAMIC) == 0
7854 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7855 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7856 == get_elf_backend_data (obfd)->s->elfclass))
7857 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7858 if ((sec->flags & SEC_MERGE) != 0
7859 && !bfd_is_abs_section (sec->output_section))
7860 {
7861 struct bfd_elf_section_data *secdata;
7862
7863 secdata = elf_section_data (sec);
7864 if (! _bfd_add_merge_section (obfd,
7865 &elf_hash_table (info)->merge_info,
7866 sec, &secdata->sec_info))
7867 return false;
7868 else if (secdata->sec_info)
7869 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7870 }
7871
7872 if (elf_hash_table (info)->merge_info != NULL)
7873 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7874 merge_sections_remove_hook);
7875 return true;
7876 }
7877
7878 /* Create an entry in an ELF linker hash table. */
7879
7880 struct bfd_hash_entry *
_bfd_elf_link_hash_newfunc(struct bfd_hash_entry * entry,struct bfd_hash_table * table,const char * string)7881 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7882 struct bfd_hash_table *table,
7883 const char *string)
7884 {
7885 /* Allocate the structure if it has not already been allocated by a
7886 subclass. */
7887 if (entry == NULL)
7888 {
7889 entry = (struct bfd_hash_entry *)
7890 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7891 if (entry == NULL)
7892 return entry;
7893 }
7894
7895 /* Call the allocation method of the superclass. */
7896 entry = _bfd_link_hash_newfunc (entry, table, string);
7897 if (entry != NULL)
7898 {
7899 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7900 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7901
7902 /* Set local fields. */
7903 ret->indx = -1;
7904 ret->dynindx = -1;
7905 ret->got = htab->init_got_refcount;
7906 ret->plt = htab->init_plt_refcount;
7907 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7908 - offsetof (struct elf_link_hash_entry, size)));
7909 /* Assume that we have been called by a non-ELF symbol reader.
7910 This flag is then reset by the code which reads an ELF input
7911 file. This ensures that a symbol created by a non-ELF symbol
7912 reader will have the flag set correctly. */
7913 ret->non_elf = 1;
7914 }
7915
7916 return entry;
7917 }
7918
7919 /* Copy data from an indirect symbol to its direct symbol, hiding the
7920 old indirect symbol. Also used for copying flags to a weakdef. */
7921
7922 void
_bfd_elf_link_hash_copy_indirect(struct bfd_link_info * info,struct elf_link_hash_entry * dir,struct elf_link_hash_entry * ind)7923 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7924 struct elf_link_hash_entry *dir,
7925 struct elf_link_hash_entry *ind)
7926 {
7927 struct elf_link_hash_table *htab;
7928
7929 if (ind->dyn_relocs != NULL)
7930 {
7931 if (dir->dyn_relocs != NULL)
7932 {
7933 struct elf_dyn_relocs **pp;
7934 struct elf_dyn_relocs *p;
7935
7936 /* Add reloc counts against the indirect sym to the direct sym
7937 list. Merge any entries against the same section. */
7938 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
7939 {
7940 struct elf_dyn_relocs *q;
7941
7942 for (q = dir->dyn_relocs; q != NULL; q = q->next)
7943 if (q->sec == p->sec)
7944 {
7945 q->pc_count += p->pc_count;
7946 q->count += p->count;
7947 *pp = p->next;
7948 break;
7949 }
7950 if (q == NULL)
7951 pp = &p->next;
7952 }
7953 *pp = dir->dyn_relocs;
7954 }
7955
7956 dir->dyn_relocs = ind->dyn_relocs;
7957 ind->dyn_relocs = NULL;
7958 }
7959
7960 /* Copy down any references that we may have already seen to the
7961 symbol which just became indirect. */
7962
7963 if (dir->versioned != versioned_hidden)
7964 dir->ref_dynamic |= ind->ref_dynamic;
7965 dir->ref_regular |= ind->ref_regular;
7966 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7967 dir->non_got_ref |= ind->non_got_ref;
7968 dir->needs_plt |= ind->needs_plt;
7969 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7970
7971 if (ind->root.type != bfd_link_hash_indirect)
7972 return;
7973
7974 /* Copy over the global and procedure linkage table refcount entries.
7975 These may have been already set up by a check_relocs routine. */
7976 htab = elf_hash_table (info);
7977 if (ind->got.refcount > htab->init_got_refcount.refcount)
7978 {
7979 if (dir->got.refcount < 0)
7980 dir->got.refcount = 0;
7981 dir->got.refcount += ind->got.refcount;
7982 ind->got.refcount = htab->init_got_refcount.refcount;
7983 }
7984
7985 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7986 {
7987 if (dir->plt.refcount < 0)
7988 dir->plt.refcount = 0;
7989 dir->plt.refcount += ind->plt.refcount;
7990 ind->plt.refcount = htab->init_plt_refcount.refcount;
7991 }
7992
7993 if (ind->dynindx != -1)
7994 {
7995 if (dir->dynindx != -1)
7996 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7997 dir->dynindx = ind->dynindx;
7998 dir->dynstr_index = ind->dynstr_index;
7999 ind->dynindx = -1;
8000 ind->dynstr_index = 0;
8001 }
8002 }
8003
8004 void
_bfd_elf_link_hash_hide_symbol(struct bfd_link_info * info,struct elf_link_hash_entry * h,bool force_local)8005 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
8006 struct elf_link_hash_entry *h,
8007 bool force_local)
8008 {
8009 /* STT_GNU_IFUNC symbol must go through PLT. */
8010 if (h->type != STT_GNU_IFUNC)
8011 {
8012 h->plt = elf_hash_table (info)->init_plt_offset;
8013 h->needs_plt = 0;
8014 }
8015 if (force_local)
8016 {
8017 h->forced_local = 1;
8018 if (h->dynindx != -1)
8019 {
8020 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8021 h->dynstr_index);
8022 h->dynindx = -1;
8023 h->dynstr_index = 0;
8024 }
8025 }
8026 }
8027
8028 /* Hide a symbol. */
8029
8030 void
_bfd_elf_link_hide_symbol(bfd * output_bfd,struct bfd_link_info * info,struct bfd_link_hash_entry * h)8031 _bfd_elf_link_hide_symbol (bfd *output_bfd,
8032 struct bfd_link_info *info,
8033 struct bfd_link_hash_entry *h)
8034 {
8035 if (is_elf_hash_table (info->hash))
8036 {
8037 const struct elf_backend_data *bed
8038 = get_elf_backend_data (output_bfd);
8039 struct elf_link_hash_entry *eh
8040 = (struct elf_link_hash_entry *) h;
8041 bed->elf_backend_hide_symbol (info, eh, true);
8042 eh->def_dynamic = 0;
8043 eh->ref_dynamic = 0;
8044 eh->dynamic_def = 0;
8045 }
8046 }
8047
8048 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
8049 caller. */
8050
8051 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)8052 _bfd_elf_link_hash_table_init
8053 (struct elf_link_hash_table *table,
8054 bfd *abfd,
8055 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
8056 struct bfd_hash_table *,
8057 const char *),
8058 unsigned int entsize,
8059 enum elf_target_id target_id)
8060 {
8061 bool ret;
8062 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
8063
8064 table->init_got_refcount.refcount = can_refcount - 1;
8065 table->init_plt_refcount.refcount = can_refcount - 1;
8066 table->init_got_offset.offset = -(bfd_vma) 1;
8067 table->init_plt_offset.offset = -(bfd_vma) 1;
8068 /* The first dynamic symbol is a dummy. */
8069 table->dynsymcount = 1;
8070
8071 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
8072
8073 table->root.type = bfd_link_elf_hash_table;
8074 table->hash_table_id = target_id;
8075 table->target_os = get_elf_backend_data (abfd)->target_os;
8076
8077 return ret;
8078 }
8079
8080 /* Create an ELF linker hash table. */
8081
8082 struct bfd_link_hash_table *
_bfd_elf_link_hash_table_create(bfd * abfd)8083 _bfd_elf_link_hash_table_create (bfd *abfd)
8084 {
8085 struct elf_link_hash_table *ret;
8086 size_t amt = sizeof (struct elf_link_hash_table);
8087
8088 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
8089 if (ret == NULL)
8090 return NULL;
8091
8092 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
8093 sizeof (struct elf_link_hash_entry),
8094 GENERIC_ELF_DATA))
8095 {
8096 free (ret);
8097 return NULL;
8098 }
8099 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
8100
8101 return &ret->root;
8102 }
8103
8104 /* Destroy an ELF linker hash table. */
8105
8106 void
_bfd_elf_link_hash_table_free(bfd * obfd)8107 _bfd_elf_link_hash_table_free (bfd *obfd)
8108 {
8109 struct elf_link_hash_table *htab;
8110
8111 htab = (struct elf_link_hash_table *) obfd->link.hash;
8112 if (htab->dynstr != NULL)
8113 _bfd_elf_strtab_free (htab->dynstr);
8114 _bfd_merge_sections_free (htab->merge_info);
8115 _bfd_generic_link_hash_table_free (obfd);
8116 }
8117
8118 /* This is a hook for the ELF emulation code in the generic linker to
8119 tell the backend linker what file name to use for the DT_NEEDED
8120 entry for a dynamic object. */
8121
8122 void
bfd_elf_set_dt_needed_name(bfd * abfd,const char * name)8123 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
8124 {
8125 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8126 && bfd_get_format (abfd) == bfd_object)
8127 elf_dt_name (abfd) = name;
8128 }
8129
8130 int
bfd_elf_get_dyn_lib_class(bfd * abfd)8131 bfd_elf_get_dyn_lib_class (bfd *abfd)
8132 {
8133 int lib_class;
8134 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8135 && bfd_get_format (abfd) == bfd_object)
8136 lib_class = elf_dyn_lib_class (abfd);
8137 else
8138 lib_class = 0;
8139 return lib_class;
8140 }
8141
8142 void
bfd_elf_set_dyn_lib_class(bfd * abfd,enum dynamic_lib_link_class lib_class)8143 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
8144 {
8145 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8146 && bfd_get_format (abfd) == bfd_object)
8147 elf_dyn_lib_class (abfd) = lib_class;
8148 }
8149
8150 /* Get the list of DT_NEEDED entries for a link. This is a hook for
8151 the linker ELF emulation code. */
8152
8153 struct bfd_link_needed_list *
bfd_elf_get_needed_list(bfd * abfd ATTRIBUTE_UNUSED,struct bfd_link_info * info)8154 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
8155 struct bfd_link_info *info)
8156 {
8157 if (! is_elf_hash_table (info->hash))
8158 return NULL;
8159 return elf_hash_table (info)->needed;
8160 }
8161
8162 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
8163 hook for the linker ELF emulation code. */
8164
8165 struct bfd_link_needed_list *
bfd_elf_get_runpath_list(bfd * abfd ATTRIBUTE_UNUSED,struct bfd_link_info * info)8166 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
8167 struct bfd_link_info *info)
8168 {
8169 if (! is_elf_hash_table (info->hash))
8170 return NULL;
8171 return elf_hash_table (info)->runpath;
8172 }
8173
8174 /* Get the name actually used for a dynamic object for a link. This
8175 is the SONAME entry if there is one. Otherwise, it is the string
8176 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
8177
8178 const char *
bfd_elf_get_dt_soname(bfd * abfd)8179 bfd_elf_get_dt_soname (bfd *abfd)
8180 {
8181 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8182 && bfd_get_format (abfd) == bfd_object)
8183 return elf_dt_name (abfd);
8184 return NULL;
8185 }
8186
8187 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
8188 the ELF linker emulation code. */
8189
8190 bool
bfd_elf_get_bfd_needed_list(bfd * abfd,struct bfd_link_needed_list ** pneeded)8191 bfd_elf_get_bfd_needed_list (bfd *abfd,
8192 struct bfd_link_needed_list **pneeded)
8193 {
8194 asection *s;
8195 bfd_byte *dynbuf = NULL;
8196 unsigned int elfsec;
8197 unsigned long shlink;
8198 bfd_byte *extdyn, *extdynend;
8199 size_t extdynsize;
8200 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
8201
8202 *pneeded = NULL;
8203
8204 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
8205 || bfd_get_format (abfd) != bfd_object)
8206 return true;
8207
8208 s = bfd_get_section_by_name (abfd, ".dynamic");
8209 if (s == NULL || s->size == 0)
8210 return true;
8211
8212 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
8213 goto error_return;
8214
8215 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
8216 if (elfsec == SHN_BAD)
8217 goto error_return;
8218
8219 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
8220
8221 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
8222 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
8223
8224 extdyn = dynbuf;
8225 extdynend = extdyn + s->size;
8226 for (; extdyn < extdynend; extdyn += extdynsize)
8227 {
8228 Elf_Internal_Dyn dyn;
8229
8230 (*swap_dyn_in) (abfd, extdyn, &dyn);
8231
8232 if (dyn.d_tag == DT_NULL)
8233 break;
8234
8235 if (dyn.d_tag == DT_NEEDED)
8236 {
8237 const char *string;
8238 struct bfd_link_needed_list *l;
8239 unsigned int tagv = dyn.d_un.d_val;
8240 size_t amt;
8241
8242 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
8243 if (string == NULL)
8244 goto error_return;
8245
8246 amt = sizeof *l;
8247 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
8248 if (l == NULL)
8249 goto error_return;
8250
8251 l->by = abfd;
8252 l->name = string;
8253 l->next = *pneeded;
8254 *pneeded = l;
8255 }
8256 }
8257
8258 free (dynbuf);
8259
8260 return true;
8261
8262 error_return:
8263 free (dynbuf);
8264 return false;
8265 }
8266
8267 struct elf_symbuf_symbol
8268 {
8269 unsigned long st_name; /* Symbol name, index in string tbl */
8270 unsigned char st_info; /* Type and binding attributes */
8271 unsigned char st_other; /* Visibilty, and target specific */
8272 };
8273
8274 struct elf_symbuf_head
8275 {
8276 struct elf_symbuf_symbol *ssym;
8277 size_t count;
8278 unsigned int st_shndx;
8279 };
8280
8281 struct elf_symbol
8282 {
8283 union
8284 {
8285 Elf_Internal_Sym *isym;
8286 struct elf_symbuf_symbol *ssym;
8287 void *p;
8288 } u;
8289 const char *name;
8290 };
8291
8292 /* Sort references to symbols by ascending section number. */
8293
8294 static int
elf_sort_elf_symbol(const void * arg1,const void * arg2)8295 elf_sort_elf_symbol (const void *arg1, const void *arg2)
8296 {
8297 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8298 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8299
8300 if (s1->st_shndx != s2->st_shndx)
8301 return s1->st_shndx > s2->st_shndx ? 1 : -1;
8302 /* Final sort by the address of the sym in the symbuf ensures
8303 a stable sort. */
8304 if (s1 != s2)
8305 return s1 > s2 ? 1 : -1;
8306 return 0;
8307 }
8308
8309 static int
elf_sym_name_compare(const void * arg1,const void * arg2)8310 elf_sym_name_compare (const void *arg1, const void *arg2)
8311 {
8312 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8313 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
8314 int ret = strcmp (s1->name, s2->name);
8315 if (ret != 0)
8316 return ret;
8317 if (s1->u.p != s2->u.p)
8318 return s1->u.p > s2->u.p ? 1 : -1;
8319 return 0;
8320 }
8321
8322 static struct elf_symbuf_head *
elf_create_symbuf(size_t symcount,Elf_Internal_Sym * isymbuf)8323 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
8324 {
8325 Elf_Internal_Sym **ind, **indbufend, **indbuf;
8326 struct elf_symbuf_symbol *ssym;
8327 struct elf_symbuf_head *ssymbuf, *ssymhead;
8328 size_t i, shndx_count, total_size, amt;
8329
8330 amt = symcount * sizeof (*indbuf);
8331 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
8332 if (indbuf == NULL)
8333 return NULL;
8334
8335 for (ind = indbuf, i = 0; i < symcount; i++)
8336 if (isymbuf[i].st_shndx != SHN_UNDEF)
8337 *ind++ = &isymbuf[i];
8338 indbufend = ind;
8339
8340 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8341 elf_sort_elf_symbol);
8342
8343 shndx_count = 0;
8344 if (indbufend > indbuf)
8345 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8346 if (ind[0]->st_shndx != ind[1]->st_shndx)
8347 shndx_count++;
8348
8349 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8350 + (indbufend - indbuf) * sizeof (*ssym));
8351 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
8352 if (ssymbuf == NULL)
8353 {
8354 free (indbuf);
8355 return NULL;
8356 }
8357
8358 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
8359 ssymbuf->ssym = NULL;
8360 ssymbuf->count = shndx_count;
8361 ssymbuf->st_shndx = 0;
8362 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8363 {
8364 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8365 {
8366 ssymhead++;
8367 ssymhead->ssym = ssym;
8368 ssymhead->count = 0;
8369 ssymhead->st_shndx = (*ind)->st_shndx;
8370 }
8371 ssym->st_name = (*ind)->st_name;
8372 ssym->st_info = (*ind)->st_info;
8373 ssym->st_other = (*ind)->st_other;
8374 ssymhead->count++;
8375 }
8376 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
8377 && (uintptr_t) ssym - (uintptr_t) ssymbuf == total_size);
8378
8379 free (indbuf);
8380 return ssymbuf;
8381 }
8382
8383 /* Check if 2 sections define the same set of local and global
8384 symbols. */
8385
8386 static bool
bfd_elf_match_symbols_in_sections(asection * sec1,asection * sec2,struct bfd_link_info * info)8387 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8388 struct bfd_link_info *info)
8389 {
8390 bfd *bfd1, *bfd2;
8391 const struct elf_backend_data *bed1, *bed2;
8392 Elf_Internal_Shdr *hdr1, *hdr2;
8393 size_t symcount1, symcount2;
8394 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8395 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8396 Elf_Internal_Sym *isym, *isymend;
8397 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8398 size_t count1, count2, sec_count1, sec_count2, i;
8399 unsigned int shndx1, shndx2;
8400 bool result;
8401 bool ignore_section_symbol_p;
8402
8403 bfd1 = sec1->owner;
8404 bfd2 = sec2->owner;
8405
8406 /* Both sections have to be in ELF. */
8407 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8408 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8409 return false;
8410
8411 if (elf_section_type (sec1) != elf_section_type (sec2))
8412 return false;
8413
8414 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8415 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8416 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
8417 return false;
8418
8419 bed1 = get_elf_backend_data (bfd1);
8420 bed2 = get_elf_backend_data (bfd2);
8421 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8422 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8423 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8424 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8425
8426 if (symcount1 == 0 || symcount2 == 0)
8427 return false;
8428
8429 result = false;
8430 isymbuf1 = NULL;
8431 isymbuf2 = NULL;
8432 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8433 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8434
8435 /* Ignore section symbols only when matching non-debugging sections
8436 or linkonce section with comdat section. */
8437 ignore_section_symbol_p
8438 = ((sec1->flags & SEC_DEBUGGING) == 0
8439 || ((elf_section_flags (sec1) & SHF_GROUP)
8440 != (elf_section_flags (sec2) & SHF_GROUP)));
8441
8442 if (ssymbuf1 == NULL)
8443 {
8444 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8445 NULL, NULL, NULL);
8446 if (isymbuf1 == NULL)
8447 goto done;
8448
8449 if (info != NULL && !info->reduce_memory_overheads)
8450 {
8451 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8452 elf_tdata (bfd1)->symbuf = ssymbuf1;
8453 }
8454 }
8455
8456 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8457 {
8458 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8459 NULL, NULL, NULL);
8460 if (isymbuf2 == NULL)
8461 goto done;
8462
8463 if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
8464 {
8465 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8466 elf_tdata (bfd2)->symbuf = ssymbuf2;
8467 }
8468 }
8469
8470 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8471 {
8472 /* Optimized faster version. */
8473 size_t lo, hi, mid;
8474 struct elf_symbol *symp;
8475 struct elf_symbuf_symbol *ssym, *ssymend;
8476
8477 lo = 0;
8478 hi = ssymbuf1->count;
8479 ssymbuf1++;
8480 count1 = 0;
8481 sec_count1 = 0;
8482 while (lo < hi)
8483 {
8484 mid = (lo + hi) / 2;
8485 if (shndx1 < ssymbuf1[mid].st_shndx)
8486 hi = mid;
8487 else if (shndx1 > ssymbuf1[mid].st_shndx)
8488 lo = mid + 1;
8489 else
8490 {
8491 count1 = ssymbuf1[mid].count;
8492 ssymbuf1 += mid;
8493 break;
8494 }
8495 }
8496 if (ignore_section_symbol_p)
8497 {
8498 for (i = 0; i < count1; i++)
8499 if (ELF_ST_TYPE (ssymbuf1->ssym[i].st_info) == STT_SECTION)
8500 sec_count1++;
8501 count1 -= sec_count1;
8502 }
8503
8504 lo = 0;
8505 hi = ssymbuf2->count;
8506 ssymbuf2++;
8507 count2 = 0;
8508 sec_count2 = 0;
8509 while (lo < hi)
8510 {
8511 mid = (lo + hi) / 2;
8512 if (shndx2 < ssymbuf2[mid].st_shndx)
8513 hi = mid;
8514 else if (shndx2 > ssymbuf2[mid].st_shndx)
8515 lo = mid + 1;
8516 else
8517 {
8518 count2 = ssymbuf2[mid].count;
8519 ssymbuf2 += mid;
8520 break;
8521 }
8522 }
8523 if (ignore_section_symbol_p)
8524 {
8525 for (i = 0; i < count2; i++)
8526 if (ELF_ST_TYPE (ssymbuf2->ssym[i].st_info) == STT_SECTION)
8527 sec_count2++;
8528 count2 -= sec_count2;
8529 }
8530
8531 if (count1 == 0 || count2 == 0 || count1 != count2)
8532 goto done;
8533
8534 symtable1
8535 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8536 symtable2
8537 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8538 if (symtable1 == NULL || symtable2 == NULL)
8539 goto done;
8540
8541 symp = symtable1;
8542 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1 + sec_count1;
8543 ssym < ssymend; ssym++)
8544 if (sec_count1 == 0
8545 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8546 {
8547 symp->u.ssym = ssym;
8548 symp->name = bfd_elf_string_from_elf_section (bfd1,
8549 hdr1->sh_link,
8550 ssym->st_name);
8551 symp++;
8552 }
8553
8554 symp = symtable2;
8555 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2 + sec_count2;
8556 ssym < ssymend; ssym++)
8557 if (sec_count2 == 0
8558 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8559 {
8560 symp->u.ssym = ssym;
8561 symp->name = bfd_elf_string_from_elf_section (bfd2,
8562 hdr2->sh_link,
8563 ssym->st_name);
8564 symp++;
8565 }
8566
8567 /* Sort symbol by name. */
8568 qsort (symtable1, count1, sizeof (struct elf_symbol),
8569 elf_sym_name_compare);
8570 qsort (symtable2, count1, sizeof (struct elf_symbol),
8571 elf_sym_name_compare);
8572
8573 for (i = 0; i < count1; i++)
8574 /* Two symbols must have the same binding, type and name. */
8575 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8576 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8577 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8578 goto done;
8579
8580 result = true;
8581 goto done;
8582 }
8583
8584 symtable1 = (struct elf_symbol *)
8585 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8586 symtable2 = (struct elf_symbol *)
8587 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8588 if (symtable1 == NULL || symtable2 == NULL)
8589 goto done;
8590
8591 /* Count definitions in the section. */
8592 count1 = 0;
8593 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8594 if (isym->st_shndx == shndx1
8595 && (!ignore_section_symbol_p
8596 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8597 symtable1[count1++].u.isym = isym;
8598
8599 count2 = 0;
8600 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8601 if (isym->st_shndx == shndx2
8602 && (!ignore_section_symbol_p
8603 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
8604 symtable2[count2++].u.isym = isym;
8605
8606 if (count1 == 0 || count2 == 0 || count1 != count2)
8607 goto done;
8608
8609 for (i = 0; i < count1; i++)
8610 symtable1[i].name
8611 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8612 symtable1[i].u.isym->st_name);
8613
8614 for (i = 0; i < count2; i++)
8615 symtable2[i].name
8616 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8617 symtable2[i].u.isym->st_name);
8618
8619 /* Sort symbol by name. */
8620 qsort (symtable1, count1, sizeof (struct elf_symbol),
8621 elf_sym_name_compare);
8622 qsort (symtable2, count1, sizeof (struct elf_symbol),
8623 elf_sym_name_compare);
8624
8625 for (i = 0; i < count1; i++)
8626 /* Two symbols must have the same binding, type and name. */
8627 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8628 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8629 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8630 goto done;
8631
8632 result = true;
8633
8634 done:
8635 free (symtable1);
8636 free (symtable2);
8637 free (isymbuf1);
8638 free (isymbuf2);
8639
8640 return result;
8641 }
8642
8643 /* Return TRUE if 2 section types are compatible. */
8644
8645 bool
_bfd_elf_match_sections_by_type(bfd * abfd,const asection * asec,bfd * bbfd,const asection * bsec)8646 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8647 bfd *bbfd, const asection *bsec)
8648 {
8649 if (asec == NULL
8650 || bsec == NULL
8651 || abfd->xvec->flavour != bfd_target_elf_flavour
8652 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8653 return true;
8654
8655 return elf_section_type (asec) == elf_section_type (bsec);
8656 }
8657
8658 /* Final phase of ELF linker. */
8659
8660 /* A structure we use to avoid passing large numbers of arguments. */
8661
8662 struct elf_final_link_info
8663 {
8664 /* General link information. */
8665 struct bfd_link_info *info;
8666 /* Output BFD. */
8667 bfd *output_bfd;
8668 /* Symbol string table. */
8669 struct elf_strtab_hash *symstrtab;
8670 /* .hash section. */
8671 asection *hash_sec;
8672 /* symbol version section (.gnu.version). */
8673 asection *symver_sec;
8674 /* Buffer large enough to hold contents of any section. */
8675 bfd_byte *contents;
8676 /* Buffer large enough to hold external relocs of any section. */
8677 void *external_relocs;
8678 /* Buffer large enough to hold internal relocs of any section. */
8679 Elf_Internal_Rela *internal_relocs;
8680 /* Buffer large enough to hold external local symbols of any input
8681 BFD. */
8682 bfd_byte *external_syms;
8683 /* And a buffer for symbol section indices. */
8684 Elf_External_Sym_Shndx *locsym_shndx;
8685 /* Buffer large enough to hold internal local symbols of any input
8686 BFD. */
8687 Elf_Internal_Sym *internal_syms;
8688 /* Array large enough to hold a symbol index for each local symbol
8689 of any input BFD. */
8690 long *indices;
8691 /* Array large enough to hold a section pointer for each local
8692 symbol of any input BFD. */
8693 asection **sections;
8694 /* Buffer for SHT_SYMTAB_SHNDX section. */
8695 Elf_External_Sym_Shndx *symshndxbuf;
8696 /* Number of STT_FILE syms seen. */
8697 size_t filesym_count;
8698 /* Local symbol hash table. */
8699 struct bfd_hash_table local_hash_table;
8700 };
8701
8702 struct local_hash_entry
8703 {
8704 /* Base hash table entry structure. */
8705 struct bfd_hash_entry root;
8706 /* Size of the local symbol name. */
8707 size_t size;
8708 /* Number of the duplicated local symbol names. */
8709 long count;
8710 };
8711
8712 /* Create an entry in the local symbol hash table. */
8713
8714 static struct bfd_hash_entry *
local_hash_newfunc(struct bfd_hash_entry * entry,struct bfd_hash_table * table,const char * string)8715 local_hash_newfunc (struct bfd_hash_entry *entry,
8716 struct bfd_hash_table *table,
8717 const char *string)
8718 {
8719
8720 /* Allocate the structure if it has not already been allocated by a
8721 subclass. */
8722 if (entry == NULL)
8723 {
8724 entry = bfd_hash_allocate (table,
8725 sizeof (struct local_hash_entry));
8726 if (entry == NULL)
8727 return entry;
8728 }
8729
8730 /* Call the allocation method of the superclass. */
8731 entry = bfd_hash_newfunc (entry, table, string);
8732 if (entry != NULL)
8733 {
8734 ((struct local_hash_entry *) entry)->count = 0;
8735 ((struct local_hash_entry *) entry)->size = 0;
8736 }
8737
8738 return entry;
8739 }
8740
8741 /* This struct is used to pass information to elf_link_output_extsym. */
8742
8743 struct elf_outext_info
8744 {
8745 bool failed;
8746 bool localsyms;
8747 bool file_sym_done;
8748 struct elf_final_link_info *flinfo;
8749 };
8750
8751
8752 /* Support for evaluating a complex relocation.
8753
8754 Complex relocations are generalized, self-describing relocations. The
8755 implementation of them consists of two parts: complex symbols, and the
8756 relocations themselves.
8757
8758 The relocations use a reserved elf-wide relocation type code (R_RELC
8759 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8760 information (start bit, end bit, word width, etc) into the addend. This
8761 information is extracted from CGEN-generated operand tables within gas.
8762
8763 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
8764 internal) representing prefix-notation expressions, including but not
8765 limited to those sorts of expressions normally encoded as addends in the
8766 addend field. The symbol mangling format is:
8767
8768 <node> := <literal>
8769 | <unary-operator> ':' <node>
8770 | <binary-operator> ':' <node> ':' <node>
8771 ;
8772
8773 <literal> := 's' <digits=N> ':' <N character symbol name>
8774 | 'S' <digits=N> ':' <N character section name>
8775 | '#' <hexdigits>
8776 ;
8777
8778 <binary-operator> := as in C
8779 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8780
8781 static void
set_symbol_value(bfd * bfd_with_globals,Elf_Internal_Sym * isymbuf,size_t locsymcount,size_t symidx,bfd_vma val)8782 set_symbol_value (bfd *bfd_with_globals,
8783 Elf_Internal_Sym *isymbuf,
8784 size_t locsymcount,
8785 size_t symidx,
8786 bfd_vma val)
8787 {
8788 struct elf_link_hash_entry **sym_hashes;
8789 struct elf_link_hash_entry *h;
8790 size_t extsymoff = locsymcount;
8791
8792 if (symidx < locsymcount)
8793 {
8794 Elf_Internal_Sym *sym;
8795
8796 sym = isymbuf + symidx;
8797 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8798 {
8799 /* It is a local symbol: move it to the
8800 "absolute" section and give it a value. */
8801 sym->st_shndx = SHN_ABS;
8802 sym->st_value = val;
8803 return;
8804 }
8805 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8806 extsymoff = 0;
8807 }
8808
8809 /* It is a global symbol: set its link type
8810 to "defined" and give it a value. */
8811
8812 sym_hashes = elf_sym_hashes (bfd_with_globals);
8813 h = sym_hashes [symidx - extsymoff];
8814 while (h->root.type == bfd_link_hash_indirect
8815 || h->root.type == bfd_link_hash_warning)
8816 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8817 h->root.type = bfd_link_hash_defined;
8818 h->root.u.def.value = val;
8819 h->root.u.def.section = bfd_abs_section_ptr;
8820 }
8821
8822 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)8823 resolve_symbol (const char *name,
8824 bfd *input_bfd,
8825 struct elf_final_link_info *flinfo,
8826 bfd_vma *result,
8827 Elf_Internal_Sym *isymbuf,
8828 size_t locsymcount)
8829 {
8830 Elf_Internal_Sym *sym;
8831 struct bfd_link_hash_entry *global_entry;
8832 const char *candidate = NULL;
8833 Elf_Internal_Shdr *symtab_hdr;
8834 size_t i;
8835
8836 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8837
8838 for (i = 0; i < locsymcount; ++ i)
8839 {
8840 sym = isymbuf + i;
8841
8842 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8843 continue;
8844
8845 candidate = bfd_elf_string_from_elf_section (input_bfd,
8846 symtab_hdr->sh_link,
8847 sym->st_name);
8848 #ifdef DEBUG
8849 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8850 name, candidate, (unsigned long) sym->st_value);
8851 #endif
8852 if (candidate && strcmp (candidate, name) == 0)
8853 {
8854 asection *sec = flinfo->sections [i];
8855
8856 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8857 *result += sec->output_offset + sec->output_section->vma;
8858 #ifdef DEBUG
8859 printf ("Found symbol with value %8.8lx\n",
8860 (unsigned long) *result);
8861 #endif
8862 return true;
8863 }
8864 }
8865
8866 /* Hmm, haven't found it yet. perhaps it is a global. */
8867 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8868 false, false, true);
8869 if (!global_entry)
8870 return false;
8871
8872 if (global_entry->type == bfd_link_hash_defined
8873 || global_entry->type == bfd_link_hash_defweak)
8874 {
8875 *result = (global_entry->u.def.value
8876 + global_entry->u.def.section->output_section->vma
8877 + global_entry->u.def.section->output_offset);
8878 #ifdef DEBUG
8879 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8880 global_entry->root.string, (unsigned long) *result);
8881 #endif
8882 return true;
8883 }
8884
8885 return false;
8886 }
8887
8888 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8889 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8890 names like "foo.end" which is the end address of section "foo". */
8891
8892 static bool
resolve_section(const char * name,asection * sections,bfd_vma * result,bfd * abfd)8893 resolve_section (const char *name,
8894 asection *sections,
8895 bfd_vma *result,
8896 bfd * abfd)
8897 {
8898 asection *curr;
8899 unsigned int len;
8900
8901 for (curr = sections; curr; curr = curr->next)
8902 if (strcmp (curr->name, name) == 0)
8903 {
8904 *result = curr->vma;
8905 return true;
8906 }
8907
8908 /* Hmm. still haven't found it. try pseudo-section names. */
8909 /* FIXME: This could be coded more efficiently... */
8910 for (curr = sections; curr; curr = curr->next)
8911 {
8912 len = strlen (curr->name);
8913 if (len > strlen (name))
8914 continue;
8915
8916 if (strncmp (curr->name, name, len) == 0)
8917 {
8918 if (startswith (name + len, ".end"))
8919 {
8920 *result = (curr->vma
8921 + curr->size / bfd_octets_per_byte (abfd, curr));
8922 return true;
8923 }
8924
8925 /* Insert more pseudo-section names here, if you like. */
8926 }
8927 }
8928
8929 return false;
8930 }
8931
8932 static void
undefined_reference(const char * reftype,const char * name)8933 undefined_reference (const char *reftype, const char *name)
8934 {
8935 /* xgettext:c-format */
8936 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8937 reftype, name);
8938 bfd_set_error (bfd_error_bad_value);
8939 }
8940
8941 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)8942 eval_symbol (bfd_vma *result,
8943 const char **symp,
8944 bfd *input_bfd,
8945 struct elf_final_link_info *flinfo,
8946 bfd_vma dot,
8947 Elf_Internal_Sym *isymbuf,
8948 size_t locsymcount,
8949 int signed_p)
8950 {
8951 size_t len;
8952 size_t symlen;
8953 bfd_vma a;
8954 bfd_vma b;
8955 char symbuf[4096];
8956 const char *sym = *symp;
8957 const char *symend;
8958 bool symbol_is_section = false;
8959
8960 len = strlen (sym);
8961 symend = sym + len;
8962
8963 if (len < 1 || len > sizeof (symbuf))
8964 {
8965 bfd_set_error (bfd_error_invalid_operation);
8966 return false;
8967 }
8968
8969 switch (* sym)
8970 {
8971 case '.':
8972 *result = dot;
8973 *symp = sym + 1;
8974 return true;
8975
8976 case '#':
8977 ++sym;
8978 *result = strtoul (sym, (char **) symp, 16);
8979 return true;
8980
8981 case 'S':
8982 symbol_is_section = true;
8983 /* Fall through. */
8984 case 's':
8985 ++sym;
8986 symlen = strtol (sym, (char **) symp, 10);
8987 sym = *symp + 1; /* Skip the trailing ':'. */
8988
8989 if (symend < sym || symlen + 1 > sizeof (symbuf))
8990 {
8991 bfd_set_error (bfd_error_invalid_operation);
8992 return false;
8993 }
8994
8995 memcpy (symbuf, sym, symlen);
8996 symbuf[symlen] = '\0';
8997 *symp = sym + symlen;
8998
8999 /* Is it always possible, with complex symbols, that gas "mis-guessed"
9000 the symbol as a section, or vice-versa. so we're pretty liberal in our
9001 interpretation here; section means "try section first", not "must be a
9002 section", and likewise with symbol. */
9003
9004 if (symbol_is_section)
9005 {
9006 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
9007 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
9008 isymbuf, locsymcount))
9009 {
9010 undefined_reference ("section", symbuf);
9011 return false;
9012 }
9013 }
9014 else
9015 {
9016 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
9017 isymbuf, locsymcount)
9018 && !resolve_section (symbuf, flinfo->output_bfd->sections,
9019 result, input_bfd))
9020 {
9021 undefined_reference ("symbol", symbuf);
9022 return false;
9023 }
9024 }
9025
9026 return true;
9027
9028 /* All that remains are operators. */
9029
9030 #define UNARY_OP(op) \
9031 if (startswith (sym, #op)) \
9032 { \
9033 sym += strlen (#op); \
9034 if (*sym == ':') \
9035 ++sym; \
9036 *symp = sym; \
9037 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9038 isymbuf, locsymcount, signed_p)) \
9039 return false; \
9040 if (signed_p) \
9041 *result = op ((bfd_signed_vma) a); \
9042 else \
9043 *result = op a; \
9044 return true; \
9045 }
9046
9047 #define BINARY_OP_HEAD(op) \
9048 if (startswith (sym, #op)) \
9049 { \
9050 sym += strlen (#op); \
9051 if (*sym == ':') \
9052 ++sym; \
9053 *symp = sym; \
9054 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9055 isymbuf, locsymcount, signed_p)) \
9056 return false; \
9057 ++*symp; \
9058 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
9059 isymbuf, locsymcount, signed_p)) \
9060 return false;
9061 #define BINARY_OP_TAIL(op) \
9062 if (signed_p) \
9063 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
9064 else \
9065 *result = a op b; \
9066 return true; \
9067 }
9068 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
9069
9070 default:
9071 UNARY_OP (0-);
9072 BINARY_OP_HEAD (<<);
9073 if (b >= sizeof (a) * CHAR_BIT)
9074 {
9075 *result = 0;
9076 return true;
9077 }
9078 signed_p = 0;
9079 BINARY_OP_TAIL (<<);
9080 BINARY_OP_HEAD (>>);
9081 if (b >= sizeof (a) * CHAR_BIT)
9082 {
9083 *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
9084 return true;
9085 }
9086 BINARY_OP_TAIL (>>);
9087 BINARY_OP (==);
9088 BINARY_OP (!=);
9089 BINARY_OP (<=);
9090 BINARY_OP (>=);
9091 BINARY_OP (&&);
9092 BINARY_OP (||);
9093 UNARY_OP (~);
9094 UNARY_OP (!);
9095 BINARY_OP (*);
9096 BINARY_OP_HEAD (/);
9097 if (b == 0)
9098 {
9099 _bfd_error_handler (_("division by zero"));
9100 bfd_set_error (bfd_error_bad_value);
9101 return false;
9102 }
9103 BINARY_OP_TAIL (/);
9104 BINARY_OP_HEAD (%);
9105 if (b == 0)
9106 {
9107 _bfd_error_handler (_("division by zero"));
9108 bfd_set_error (bfd_error_bad_value);
9109 return false;
9110 }
9111 BINARY_OP_TAIL (%);
9112 BINARY_OP (^);
9113 BINARY_OP (|);
9114 BINARY_OP (&);
9115 BINARY_OP (+);
9116 BINARY_OP (-);
9117 BINARY_OP (<);
9118 BINARY_OP (>);
9119 #undef UNARY_OP
9120 #undef BINARY_OP
9121 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
9122 bfd_set_error (bfd_error_invalid_operation);
9123 return false;
9124 }
9125 }
9126
9127 static void
put_value(bfd_vma size,unsigned long chunksz,bfd * input_bfd,bfd_vma x,bfd_byte * location)9128 put_value (bfd_vma size,
9129 unsigned long chunksz,
9130 bfd *input_bfd,
9131 bfd_vma x,
9132 bfd_byte *location)
9133 {
9134 location += (size - chunksz);
9135
9136 for (; size; size -= chunksz, location -= chunksz)
9137 {
9138 switch (chunksz)
9139 {
9140 case 1:
9141 bfd_put_8 (input_bfd, x, location);
9142 x >>= 8;
9143 break;
9144 case 2:
9145 bfd_put_16 (input_bfd, x, location);
9146 x >>= 16;
9147 break;
9148 case 4:
9149 bfd_put_32 (input_bfd, x, location);
9150 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
9151 x >>= 16;
9152 x >>= 16;
9153 break;
9154 #ifdef BFD64
9155 case 8:
9156 bfd_put_64 (input_bfd, x, location);
9157 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
9158 x >>= 32;
9159 x >>= 32;
9160 break;
9161 #endif
9162 default:
9163 abort ();
9164 break;
9165 }
9166 }
9167 }
9168
9169 static bfd_vma
get_value(bfd_vma size,unsigned long chunksz,bfd * input_bfd,bfd_byte * location)9170 get_value (bfd_vma size,
9171 unsigned long chunksz,
9172 bfd *input_bfd,
9173 bfd_byte *location)
9174 {
9175 int shift;
9176 bfd_vma x = 0;
9177
9178 /* Sanity checks. */
9179 BFD_ASSERT (chunksz <= sizeof (x)
9180 && size >= chunksz
9181 && chunksz != 0
9182 && (size % chunksz) == 0
9183 && input_bfd != NULL
9184 && location != NULL);
9185
9186 if (chunksz == sizeof (x))
9187 {
9188 BFD_ASSERT (size == chunksz);
9189
9190 /* Make sure that we do not perform an undefined shift operation.
9191 We know that size == chunksz so there will only be one iteration
9192 of the loop below. */
9193 shift = 0;
9194 }
9195 else
9196 shift = 8 * chunksz;
9197
9198 for (; size; size -= chunksz, location += chunksz)
9199 {
9200 switch (chunksz)
9201 {
9202 case 1:
9203 x = (x << shift) | bfd_get_8 (input_bfd, location);
9204 break;
9205 case 2:
9206 x = (x << shift) | bfd_get_16 (input_bfd, location);
9207 break;
9208 case 4:
9209 x = (x << shift) | bfd_get_32 (input_bfd, location);
9210 break;
9211 #ifdef BFD64
9212 case 8:
9213 x = (x << shift) | bfd_get_64 (input_bfd, location);
9214 break;
9215 #endif
9216 default:
9217 abort ();
9218 }
9219 }
9220 return x;
9221 }
9222
9223 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)9224 decode_complex_addend (unsigned long *start, /* in bits */
9225 unsigned long *oplen, /* in bits */
9226 unsigned long *len, /* in bits */
9227 unsigned long *wordsz, /* in bytes */
9228 unsigned long *chunksz, /* in bytes */
9229 unsigned long *lsb0_p,
9230 unsigned long *signed_p,
9231 unsigned long *trunc_p,
9232 unsigned long encoded)
9233 {
9234 * start = encoded & 0x3F;
9235 * len = (encoded >> 6) & 0x3F;
9236 * oplen = (encoded >> 12) & 0x3F;
9237 * wordsz = (encoded >> 18) & 0xF;
9238 * chunksz = (encoded >> 22) & 0xF;
9239 * lsb0_p = (encoded >> 27) & 1;
9240 * signed_p = (encoded >> 28) & 1;
9241 * trunc_p = (encoded >> 29) & 1;
9242 }
9243
9244 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)9245 bfd_elf_perform_complex_relocation (bfd *input_bfd,
9246 asection *input_section,
9247 bfd_byte *contents,
9248 Elf_Internal_Rela *rel,
9249 bfd_vma relocation)
9250 {
9251 bfd_vma shift, x, mask;
9252 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
9253 bfd_reloc_status_type r;
9254 bfd_size_type octets;
9255
9256 /* Perform this reloc, since it is complex.
9257 (this is not to say that it necessarily refers to a complex
9258 symbol; merely that it is a self-describing CGEN based reloc.
9259 i.e. the addend has the complete reloc information (bit start, end,
9260 word size, etc) encoded within it.). */
9261
9262 decode_complex_addend (&start, &oplen, &len, &wordsz,
9263 &chunksz, &lsb0_p, &signed_p,
9264 &trunc_p, rel->r_addend);
9265
9266 mask = (((1L << (len - 1)) - 1) << 1) | 1;
9267
9268 if (lsb0_p)
9269 shift = (start + 1) - len;
9270 else
9271 shift = (8 * wordsz) - (start + len);
9272
9273 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
9274 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
9275
9276 #ifdef DEBUG
9277 printf ("Doing complex reloc: "
9278 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9279 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9280 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9281 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9282 oplen, (unsigned long) x, (unsigned long) mask,
9283 (unsigned long) relocation);
9284 #endif
9285
9286 r = bfd_reloc_ok;
9287 if (! trunc_p)
9288 /* Now do an overflow check. */
9289 r = bfd_check_overflow ((signed_p
9290 ? complain_overflow_signed
9291 : complain_overflow_unsigned),
9292 len, 0, (8 * wordsz),
9293 relocation);
9294
9295 /* Do the deed. */
9296 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
9297
9298 #ifdef DEBUG
9299 printf (" relocation: %8.8lx\n"
9300 " shifted mask: %8.8lx\n"
9301 " shifted/masked reloc: %8.8lx\n"
9302 " result: %8.8lx\n",
9303 (unsigned long) relocation, (unsigned long) (mask << shift),
9304 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
9305 #endif
9306 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
9307 return r;
9308 }
9309
9310 /* Functions to read r_offset from external (target order) reloc
9311 entry. Faster than bfd_getl32 et al, because we let the compiler
9312 know the value is aligned. */
9313
9314 static bfd_vma
ext32l_r_offset(const void * p)9315 ext32l_r_offset (const void *p)
9316 {
9317 union aligned32
9318 {
9319 uint32_t v;
9320 unsigned char c[4];
9321 };
9322 const union aligned32 *a
9323 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9324
9325 uint32_t aval = ( (uint32_t) a->c[0]
9326 | (uint32_t) a->c[1] << 8
9327 | (uint32_t) a->c[2] << 16
9328 | (uint32_t) a->c[3] << 24);
9329 return aval;
9330 }
9331
9332 static bfd_vma
ext32b_r_offset(const void * p)9333 ext32b_r_offset (const void *p)
9334 {
9335 union aligned32
9336 {
9337 uint32_t v;
9338 unsigned char c[4];
9339 };
9340 const union aligned32 *a
9341 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
9342
9343 uint32_t aval = ( (uint32_t) a->c[0] << 24
9344 | (uint32_t) a->c[1] << 16
9345 | (uint32_t) a->c[2] << 8
9346 | (uint32_t) a->c[3]);
9347 return aval;
9348 }
9349
9350 static bfd_vma
ext64l_r_offset(const void * p)9351 ext64l_r_offset (const void *p)
9352 {
9353 union aligned64
9354 {
9355 uint64_t v;
9356 unsigned char c[8];
9357 };
9358 const union aligned64 *a
9359 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9360
9361 uint64_t aval = ( (uint64_t) a->c[0]
9362 | (uint64_t) a->c[1] << 8
9363 | (uint64_t) a->c[2] << 16
9364 | (uint64_t) a->c[3] << 24
9365 | (uint64_t) a->c[4] << 32
9366 | (uint64_t) a->c[5] << 40
9367 | (uint64_t) a->c[6] << 48
9368 | (uint64_t) a->c[7] << 56);
9369 return aval;
9370 }
9371
9372 static bfd_vma
ext64b_r_offset(const void * p)9373 ext64b_r_offset (const void *p)
9374 {
9375 union aligned64
9376 {
9377 uint64_t v;
9378 unsigned char c[8];
9379 };
9380 const union aligned64 *a
9381 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
9382
9383 uint64_t aval = ( (uint64_t) a->c[0] << 56
9384 | (uint64_t) a->c[1] << 48
9385 | (uint64_t) a->c[2] << 40
9386 | (uint64_t) a->c[3] << 32
9387 | (uint64_t) a->c[4] << 24
9388 | (uint64_t) a->c[5] << 16
9389 | (uint64_t) a->c[6] << 8
9390 | (uint64_t) a->c[7]);
9391 return aval;
9392 }
9393
9394 /* When performing a relocatable link, the input relocations are
9395 preserved. But, if they reference global symbols, the indices
9396 referenced must be updated. Update all the relocations found in
9397 RELDATA. */
9398
9399 static bool
elf_link_adjust_relocs(bfd * abfd,asection * sec,struct bfd_elf_section_reloc_data * reldata,bool sort,struct bfd_link_info * info)9400 elf_link_adjust_relocs (bfd *abfd,
9401 asection *sec,
9402 struct bfd_elf_section_reloc_data *reldata,
9403 bool sort,
9404 struct bfd_link_info *info)
9405 {
9406 unsigned int i;
9407 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9408 bfd_byte *erela;
9409 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9410 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9411 bfd_vma r_type_mask;
9412 int r_sym_shift;
9413 unsigned int count = reldata->count;
9414 struct elf_link_hash_entry **rel_hash = reldata->hashes;
9415
9416 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
9417 {
9418 swap_in = bed->s->swap_reloc_in;
9419 swap_out = bed->s->swap_reloc_out;
9420 }
9421 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
9422 {
9423 swap_in = bed->s->swap_reloca_in;
9424 swap_out = bed->s->swap_reloca_out;
9425 }
9426 else
9427 abort ();
9428
9429 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9430 abort ();
9431
9432 if (bed->s->arch_size == 32)
9433 {
9434 r_type_mask = 0xff;
9435 r_sym_shift = 8;
9436 }
9437 else
9438 {
9439 r_type_mask = 0xffffffff;
9440 r_sym_shift = 32;
9441 }
9442
9443 erela = reldata->hdr->contents;
9444 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
9445 {
9446 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9447 unsigned int j;
9448
9449 if (*rel_hash == NULL)
9450 continue;
9451
9452 if ((*rel_hash)->indx == -2
9453 && info->gc_sections
9454 && ! info->gc_keep_exported)
9455 {
9456 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9457 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9458 abfd, sec,
9459 (*rel_hash)->root.root.string);
9460 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9461 abfd, sec);
9462 bfd_set_error (bfd_error_invalid_operation);
9463 return false;
9464 }
9465 BFD_ASSERT ((*rel_hash)->indx >= 0);
9466
9467 (*swap_in) (abfd, erela, irela);
9468 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9469 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9470 | (irela[j].r_info & r_type_mask));
9471 (*swap_out) (abfd, irela, erela);
9472 }
9473
9474 if (bed->elf_backend_update_relocs)
9475 (*bed->elf_backend_update_relocs) (sec, reldata);
9476
9477 if (sort && count != 0)
9478 {
9479 bfd_vma (*ext_r_off) (const void *);
9480 bfd_vma r_off;
9481 size_t elt_size;
9482 bfd_byte *base, *end, *p, *loc;
9483 bfd_byte *buf = NULL;
9484
9485 if (bed->s->arch_size == 32)
9486 {
9487 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9488 ext_r_off = ext32l_r_offset;
9489 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9490 ext_r_off = ext32b_r_offset;
9491 else
9492 abort ();
9493 }
9494 else
9495 {
9496 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9497 ext_r_off = ext64l_r_offset;
9498 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9499 ext_r_off = ext64b_r_offset;
9500 else
9501 abort ();
9502 }
9503
9504 /* Must use a stable sort here. A modified insertion sort,
9505 since the relocs are mostly sorted already. */
9506 elt_size = reldata->hdr->sh_entsize;
9507 base = reldata->hdr->contents;
9508 end = base + count * elt_size;
9509 if (elt_size > sizeof (Elf64_External_Rela))
9510 abort ();
9511
9512 /* Ensure the first element is lowest. This acts as a sentinel,
9513 speeding the main loop below. */
9514 r_off = (*ext_r_off) (base);
9515 for (p = loc = base; (p += elt_size) < end; )
9516 {
9517 bfd_vma r_off2 = (*ext_r_off) (p);
9518 if (r_off > r_off2)
9519 {
9520 r_off = r_off2;
9521 loc = p;
9522 }
9523 }
9524 if (loc != base)
9525 {
9526 /* Don't just swap *base and *loc as that changes the order
9527 of the original base[0] and base[1] if they happen to
9528 have the same r_offset. */
9529 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9530 memcpy (onebuf, loc, elt_size);
9531 memmove (base + elt_size, base, loc - base);
9532 memcpy (base, onebuf, elt_size);
9533 }
9534
9535 for (p = base + elt_size; (p += elt_size) < end; )
9536 {
9537 /* base to p is sorted, *p is next to insert. */
9538 r_off = (*ext_r_off) (p);
9539 /* Search the sorted region for location to insert. */
9540 loc = p - elt_size;
9541 while (r_off < (*ext_r_off) (loc))
9542 loc -= elt_size;
9543 loc += elt_size;
9544 if (loc != p)
9545 {
9546 /* Chances are there is a run of relocs to insert here,
9547 from one of more input files. Files are not always
9548 linked in order due to the way elf_link_input_bfd is
9549 called. See pr17666. */
9550 size_t sortlen = p - loc;
9551 bfd_vma r_off2 = (*ext_r_off) (loc);
9552 size_t runlen = elt_size;
9553 bfd_vma r_off_runend = r_off;
9554 bfd_vma r_off_runend_next;
9555 size_t buf_size = 96 * 1024;
9556 while (p + runlen < end
9557 && (sortlen <= buf_size
9558 || runlen + elt_size <= buf_size)
9559 /* run must not break the ordering of base..loc+1 */
9560 && r_off2 > (r_off_runend_next = (*ext_r_off) (p + runlen))
9561 /* run must be already sorted */
9562 && r_off_runend_next >= r_off_runend)
9563 {
9564 runlen += elt_size;
9565 r_off_runend = r_off_runend_next;
9566 }
9567 if (buf == NULL)
9568 {
9569 buf = bfd_malloc (buf_size);
9570 if (buf == NULL)
9571 return false;
9572 }
9573 if (runlen < sortlen)
9574 {
9575 memcpy (buf, p, runlen);
9576 memmove (loc + runlen, loc, sortlen);
9577 memcpy (loc, buf, runlen);
9578 }
9579 else
9580 {
9581 memcpy (buf, loc, sortlen);
9582 memmove (loc, p, runlen);
9583 memcpy (loc + runlen, buf, sortlen);
9584 }
9585 p += runlen - elt_size;
9586 }
9587 }
9588 /* Hashes are no longer valid. */
9589 free (reldata->hashes);
9590 reldata->hashes = NULL;
9591 free (buf);
9592 }
9593 return true;
9594 }
9595
9596 struct elf_link_sort_rela
9597 {
9598 union {
9599 bfd_vma offset;
9600 bfd_vma sym_mask;
9601 } u;
9602 enum elf_reloc_type_class type;
9603 /* We use this as an array of size int_rels_per_ext_rel. */
9604 Elf_Internal_Rela rela[1];
9605 };
9606
9607 /* qsort stability here and for cmp2 is only an issue if multiple
9608 dynamic relocations are emitted at the same address. But targets
9609 that apply a series of dynamic relocations each operating on the
9610 result of the prior relocation can't use -z combreloc as
9611 implemented anyway. Such schemes tend to be broken by sorting on
9612 symbol index. That leaves dynamic NONE relocs as the only other
9613 case where ld might emit multiple relocs at the same address, and
9614 those are only emitted due to target bugs. */
9615
9616 static int
elf_link_sort_cmp1(const void * A,const void * B)9617 elf_link_sort_cmp1 (const void *A, const void *B)
9618 {
9619 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9620 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9621 int relativea, relativeb;
9622
9623 relativea = a->type == reloc_class_relative;
9624 relativeb = b->type == reloc_class_relative;
9625
9626 if (relativea < relativeb)
9627 return 1;
9628 if (relativea > relativeb)
9629 return -1;
9630 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9631 return -1;
9632 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9633 return 1;
9634 if (a->rela->r_offset < b->rela->r_offset)
9635 return -1;
9636 if (a->rela->r_offset > b->rela->r_offset)
9637 return 1;
9638 return 0;
9639 }
9640
9641 static int
elf_link_sort_cmp2(const void * A,const void * B)9642 elf_link_sort_cmp2 (const void *A, const void *B)
9643 {
9644 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9645 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9646
9647 if (a->type < b->type)
9648 return -1;
9649 if (a->type > b->type)
9650 return 1;
9651 if (a->u.offset < b->u.offset)
9652 return -1;
9653 if (a->u.offset > b->u.offset)
9654 return 1;
9655 if (a->rela->r_offset < b->rela->r_offset)
9656 return -1;
9657 if (a->rela->r_offset > b->rela->r_offset)
9658 return 1;
9659 return 0;
9660 }
9661
9662 static size_t
elf_link_sort_relocs(bfd * abfd,struct bfd_link_info * info,asection ** psec)9663 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9664 {
9665 asection *dynamic_relocs;
9666 asection *rela_dyn;
9667 asection *rel_dyn;
9668 bfd_size_type count, size;
9669 size_t i, ret, sort_elt, ext_size;
9670 bfd_byte *sort, *s_non_relative, *p;
9671 struct elf_link_sort_rela *sq;
9672 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9673 int i2e = bed->s->int_rels_per_ext_rel;
9674 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
9675 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9676 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9677 struct bfd_link_order *lo;
9678 bfd_vma r_sym_mask;
9679 bool use_rela;
9680
9681 /* Find a dynamic reloc section. */
9682 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9683 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9684 if (rela_dyn != NULL && rela_dyn->size > 0
9685 && rel_dyn != NULL && rel_dyn->size > 0)
9686 {
9687 bool use_rela_initialised = false;
9688
9689 /* This is just here to stop gcc from complaining.
9690 Its initialization checking code is not perfect. */
9691 use_rela = true;
9692
9693 /* Both sections are present. Examine the sizes
9694 of the indirect sections to help us choose. */
9695 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9696 if (lo->type == bfd_indirect_link_order)
9697 {
9698 asection *o = lo->u.indirect.section;
9699
9700 if ((o->size % bed->s->sizeof_rela) == 0)
9701 {
9702 if ((o->size % bed->s->sizeof_rel) == 0)
9703 /* Section size is divisible by both rel and rela sizes.
9704 It is of no help to us. */
9705 ;
9706 else
9707 {
9708 /* Section size is only divisible by rela. */
9709 if (use_rela_initialised && !use_rela)
9710 {
9711 _bfd_error_handler (_("%pB: unable to sort relocs - "
9712 "they are in more than one size"),
9713 abfd);
9714 bfd_set_error (bfd_error_invalid_operation);
9715 return 0;
9716 }
9717 else
9718 {
9719 use_rela = true;
9720 use_rela_initialised = true;
9721 }
9722 }
9723 }
9724 else if ((o->size % bed->s->sizeof_rel) == 0)
9725 {
9726 /* Section size is only divisible by rel. */
9727 if (use_rela_initialised && use_rela)
9728 {
9729 _bfd_error_handler (_("%pB: unable to sort relocs - "
9730 "they are in more than one size"),
9731 abfd);
9732 bfd_set_error (bfd_error_invalid_operation);
9733 return 0;
9734 }
9735 else
9736 {
9737 use_rela = false;
9738 use_rela_initialised = true;
9739 }
9740 }
9741 else
9742 {
9743 /* The section size is not divisible by either -
9744 something is wrong. */
9745 _bfd_error_handler (_("%pB: unable to sort relocs - "
9746 "they are of an unknown size"), abfd);
9747 bfd_set_error (bfd_error_invalid_operation);
9748 return 0;
9749 }
9750 }
9751
9752 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9753 if (lo->type == bfd_indirect_link_order)
9754 {
9755 asection *o = lo->u.indirect.section;
9756
9757 if ((o->size % bed->s->sizeof_rela) == 0)
9758 {
9759 if ((o->size % bed->s->sizeof_rel) == 0)
9760 /* Section size is divisible by both rel and rela sizes.
9761 It is of no help to us. */
9762 ;
9763 else
9764 {
9765 /* Section size is only divisible by rela. */
9766 if (use_rela_initialised && !use_rela)
9767 {
9768 _bfd_error_handler (_("%pB: unable to sort relocs - "
9769 "they are in more than one size"),
9770 abfd);
9771 bfd_set_error (bfd_error_invalid_operation);
9772 return 0;
9773 }
9774 else
9775 {
9776 use_rela = true;
9777 use_rela_initialised = true;
9778 }
9779 }
9780 }
9781 else if ((o->size % bed->s->sizeof_rel) == 0)
9782 {
9783 /* Section size is only divisible by rel. */
9784 if (use_rela_initialised && use_rela)
9785 {
9786 _bfd_error_handler (_("%pB: unable to sort relocs - "
9787 "they are in more than one size"),
9788 abfd);
9789 bfd_set_error (bfd_error_invalid_operation);
9790 return 0;
9791 }
9792 else
9793 {
9794 use_rela = false;
9795 use_rela_initialised = true;
9796 }
9797 }
9798 else
9799 {
9800 /* The section size is not divisible by either -
9801 something is wrong. */
9802 _bfd_error_handler (_("%pB: unable to sort relocs - "
9803 "they are of an unknown size"), abfd);
9804 bfd_set_error (bfd_error_invalid_operation);
9805 return 0;
9806 }
9807 }
9808
9809 if (! use_rela_initialised)
9810 /* Make a guess. */
9811 use_rela = true;
9812 }
9813 else if (rela_dyn != NULL && rela_dyn->size > 0)
9814 use_rela = true;
9815 else if (rel_dyn != NULL && rel_dyn->size > 0)
9816 use_rela = false;
9817 else
9818 return 0;
9819
9820 if (use_rela)
9821 {
9822 dynamic_relocs = rela_dyn;
9823 ext_size = bed->s->sizeof_rela;
9824 swap_in = bed->s->swap_reloca_in;
9825 swap_out = bed->s->swap_reloca_out;
9826 }
9827 else
9828 {
9829 dynamic_relocs = rel_dyn;
9830 ext_size = bed->s->sizeof_rel;
9831 swap_in = bed->s->swap_reloc_in;
9832 swap_out = bed->s->swap_reloc_out;
9833 }
9834
9835 size = 0;
9836 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9837 if (lo->type == bfd_indirect_link_order)
9838 size += lo->u.indirect.section->size;
9839
9840 if (size != dynamic_relocs->size)
9841 return 0;
9842
9843 sort_elt = (sizeof (struct elf_link_sort_rela)
9844 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9845
9846 count = dynamic_relocs->size / ext_size;
9847 if (count == 0)
9848 return 0;
9849 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9850
9851 if (sort == NULL)
9852 {
9853 (*info->callbacks->warning)
9854 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
9855 return 0;
9856 }
9857
9858 if (bed->s->arch_size == 32)
9859 r_sym_mask = ~(bfd_vma) 0xff;
9860 else
9861 r_sym_mask = ~(bfd_vma) 0xffffffff;
9862
9863 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9864 if (lo->type == bfd_indirect_link_order)
9865 {
9866 bfd_byte *erel, *erelend;
9867 asection *o = lo->u.indirect.section;
9868
9869 if (o->contents == NULL && o->size != 0)
9870 {
9871 /* This is a reloc section that is being handled as a normal
9872 section. See bfd_section_from_shdr. We can't combine
9873 relocs in this case. */
9874 free (sort);
9875 return 0;
9876 }
9877 erel = o->contents;
9878 erelend = o->contents + o->size;
9879 p = sort + o->output_offset * opb / ext_size * sort_elt;
9880
9881 while (erel < erelend)
9882 {
9883 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9884
9885 (*swap_in) (abfd, erel, s->rela);
9886 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9887 s->u.sym_mask = r_sym_mask;
9888 p += sort_elt;
9889 erel += ext_size;
9890 }
9891 }
9892
9893 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9894
9895 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9896 {
9897 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9898 if (s->type != reloc_class_relative)
9899 break;
9900 }
9901 ret = i;
9902 s_non_relative = p;
9903
9904 sq = (struct elf_link_sort_rela *) s_non_relative;
9905 for (; i < count; i++, p += sort_elt)
9906 {
9907 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9908 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9909 sq = sp;
9910 sp->u.offset = sq->rela->r_offset;
9911 }
9912
9913 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9914
9915 struct elf_link_hash_table *htab = elf_hash_table (info);
9916 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9917 {
9918 /* We have plt relocs in .rela.dyn. */
9919 sq = (struct elf_link_sort_rela *) sort;
9920 for (i = 0; i < count; i++)
9921 if (sq[count - i - 1].type != reloc_class_plt)
9922 break;
9923 if (i != 0 && htab->srelplt->size == i * ext_size)
9924 {
9925 struct bfd_link_order **plo;
9926 /* Put srelplt link_order last. This is so the output_offset
9927 set in the next loop is correct for DT_JMPREL. */
9928 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9929 if ((*plo)->type == bfd_indirect_link_order
9930 && (*plo)->u.indirect.section == htab->srelplt)
9931 {
9932 lo = *plo;
9933 *plo = lo->next;
9934 }
9935 else
9936 plo = &(*plo)->next;
9937 *plo = lo;
9938 lo->next = NULL;
9939 dynamic_relocs->map_tail.link_order = lo;
9940 }
9941 }
9942
9943 p = sort;
9944 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9945 if (lo->type == bfd_indirect_link_order)
9946 {
9947 bfd_byte *erel, *erelend;
9948 asection *o = lo->u.indirect.section;
9949
9950 erel = o->contents;
9951 erelend = o->contents + o->size;
9952 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9953 while (erel < erelend)
9954 {
9955 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9956 (*swap_out) (abfd, s->rela, erel);
9957 p += sort_elt;
9958 erel += ext_size;
9959 }
9960 }
9961
9962 free (sort);
9963 *psec = dynamic_relocs;
9964 return ret;
9965 }
9966
9967 /* Add a symbol to the output symbol string table. */
9968
9969 static int
elf_link_output_symstrtab(void * finf,const char * name,Elf_Internal_Sym * elfsym,asection * input_sec,struct elf_link_hash_entry * h)9970 elf_link_output_symstrtab (void *finf,
9971 const char *name,
9972 Elf_Internal_Sym *elfsym,
9973 asection *input_sec,
9974 struct elf_link_hash_entry *h)
9975 {
9976 struct elf_final_link_info *flinfo = finf;
9977 int (*output_symbol_hook)
9978 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9979 struct elf_link_hash_entry *);
9980 struct elf_link_hash_table *hash_table;
9981 const struct elf_backend_data *bed;
9982 bfd_size_type strtabsize;
9983
9984 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9985
9986 bed = get_elf_backend_data (flinfo->output_bfd);
9987 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9988 if (output_symbol_hook != NULL)
9989 {
9990 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9991 if (ret != 1)
9992 return ret;
9993 }
9994
9995 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
9996 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
9997 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
9998 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
9999
10000 if (name == NULL
10001 || *name == '\0'
10002 || (input_sec->flags & SEC_EXCLUDE))
10003 elfsym->st_name = (unsigned long) -1;
10004 else
10005 {
10006 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
10007 to get the final offset for st_name. */
10008 char *versioned_name = (char *) name;
10009 if (h != NULL)
10010 {
10011 if (h->versioned == versioned && h->def_dynamic)
10012 {
10013 /* Keep only one '@' for versioned symbols defined in
10014 shared objects. */
10015 char *version = strrchr (name, ELF_VER_CHR);
10016 char *base_end = strchr (name, ELF_VER_CHR);
10017 if (version != base_end)
10018 {
10019 size_t base_len;
10020 size_t len = strlen (name);
10021 versioned_name = bfd_alloc (flinfo->output_bfd, len);
10022 if (versioned_name == NULL)
10023 return 0;
10024 base_len = base_end - name;
10025 memcpy (versioned_name, name, base_len);
10026 memcpy (versioned_name + base_len, version,
10027 len - base_len);
10028 }
10029 }
10030 }
10031 else if (flinfo->info->unique_symbol
10032 && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
10033 {
10034 struct local_hash_entry *lh;
10035 size_t count_len;
10036 size_t base_len;
10037 char buf[30];
10038 switch (ELF_ST_TYPE (elfsym->st_info))
10039 {
10040 case STT_FILE:
10041 case STT_SECTION:
10042 break;
10043 default:
10044 lh = (struct local_hash_entry *) bfd_hash_lookup
10045 (&flinfo->local_hash_table, name, true, false);
10046 if (lh == NULL)
10047 return 0;
10048 /* Always append ".COUNT" to local symbols to avoid
10049 potential conflicts with local symbol "XXX.COUNT". */
10050 sprintf (buf, "%lx", lh->count);
10051 base_len = lh->size;
10052 if (!base_len)
10053 {
10054 base_len = strlen (name);
10055 lh->size = base_len;
10056 }
10057 count_len = strlen (buf);
10058 versioned_name = bfd_alloc (flinfo->output_bfd,
10059 base_len + count_len + 2);
10060 if (versioned_name == NULL)
10061 return 0;
10062 memcpy (versioned_name, name, base_len);
10063 versioned_name[base_len] = '.';
10064 memcpy (versioned_name + base_len + 1, buf,
10065 count_len + 1);
10066 lh->count++;
10067 break;
10068 }
10069 }
10070 elfsym->st_name
10071 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
10072 versioned_name, false);
10073 if (elfsym->st_name == (unsigned long) -1)
10074 return 0;
10075 }
10076
10077 hash_table = elf_hash_table (flinfo->info);
10078 strtabsize = hash_table->strtabsize;
10079 if (strtabsize <= flinfo->output_bfd->symcount)
10080 {
10081 strtabsize += strtabsize;
10082 hash_table->strtabsize = strtabsize;
10083 strtabsize *= sizeof (*hash_table->strtab);
10084 hash_table->strtab
10085 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
10086 strtabsize);
10087 if (hash_table->strtab == NULL)
10088 return 0;
10089 }
10090 hash_table->strtab[flinfo->output_bfd->symcount].sym = *elfsym;
10091 hash_table->strtab[flinfo->output_bfd->symcount].dest_index
10092 = flinfo->output_bfd->symcount;
10093 flinfo->output_bfd->symcount += 1;
10094
10095 return 1;
10096 }
10097
10098 /* Swap symbols out to the symbol table and flush the output symbols to
10099 the file. */
10100
10101 static bool
elf_link_swap_symbols_out(struct elf_final_link_info * flinfo)10102 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
10103 {
10104 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
10105 size_t amt;
10106 size_t i;
10107 const struct elf_backend_data *bed;
10108 bfd_byte *symbuf;
10109 Elf_Internal_Shdr *hdr;
10110 file_ptr pos;
10111 bool ret;
10112
10113 if (flinfo->output_bfd->symcount == 0)
10114 return true;
10115
10116 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10117
10118 bed = get_elf_backend_data (flinfo->output_bfd);
10119
10120 amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10121 symbuf = (bfd_byte *) bfd_malloc (amt);
10122 if (symbuf == NULL)
10123 return false;
10124
10125 if (flinfo->symshndxbuf)
10126 {
10127 amt = sizeof (Elf_External_Sym_Shndx);
10128 amt *= bfd_get_symcount (flinfo->output_bfd);
10129 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
10130 if (flinfo->symshndxbuf == NULL)
10131 {
10132 free (symbuf);
10133 return false;
10134 }
10135 }
10136
10137 /* Now swap out the symbols. */
10138 for (i = 0; i < flinfo->output_bfd->symcount; i++)
10139 {
10140 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
10141 if (elfsym->sym.st_name == (unsigned long) -1)
10142 elfsym->sym.st_name = 0;
10143 else
10144 elfsym->sym.st_name
10145 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
10146 elfsym->sym.st_name);
10147
10148 /* Inform the linker of the addition of this symbol. */
10149
10150 if (flinfo->info->callbacks->ctf_new_symbol)
10151 flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
10152 &elfsym->sym);
10153
10154 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
10155 ((bfd_byte *) symbuf
10156 + (elfsym->dest_index
10157 * bed->s->sizeof_sym)),
10158 NPTR_ADD (flinfo->symshndxbuf,
10159 elfsym->dest_index));
10160 }
10161
10162 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
10163 pos = hdr->sh_offset + hdr->sh_size;
10164 amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
10165 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
10166 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
10167 {
10168 hdr->sh_size += amt;
10169 ret = true;
10170 }
10171 else
10172 ret = false;
10173
10174 free (symbuf);
10175
10176 free (hash_table->strtab);
10177 hash_table->strtab = NULL;
10178
10179 return ret;
10180 }
10181
10182 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
10183
10184 static bool
check_dynsym(bfd * abfd,Elf_Internal_Sym * sym)10185 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
10186 {
10187 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
10188 && sym->st_shndx < SHN_LORESERVE)
10189 {
10190 /* The gABI doesn't support dynamic symbols in output sections
10191 beyond 64k. */
10192 _bfd_error_handler
10193 /* xgettext:c-format */
10194 (_("%pB: too many sections: %d (>= %d)"),
10195 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
10196 bfd_set_error (bfd_error_nonrepresentable_section);
10197 return false;
10198 }
10199 return true;
10200 }
10201
10202 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
10203 allowing an unsatisfied unversioned symbol in the DSO to match a
10204 versioned symbol that would normally require an explicit version.
10205 We also handle the case that a DSO references a hidden symbol
10206 which may be satisfied by a versioned symbol in another DSO. */
10207
10208 static bool
elf_link_check_versioned_symbol(struct bfd_link_info * info,const struct elf_backend_data * bed,struct elf_link_hash_entry * h)10209 elf_link_check_versioned_symbol (struct bfd_link_info *info,
10210 const struct elf_backend_data *bed,
10211 struct elf_link_hash_entry *h)
10212 {
10213 bfd *abfd;
10214 struct elf_link_loaded_list *loaded;
10215
10216 if (!is_elf_hash_table (info->hash))
10217 return false;
10218
10219 /* Check indirect symbol. */
10220 while (h->root.type == bfd_link_hash_indirect)
10221 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10222
10223 switch (h->root.type)
10224 {
10225 default:
10226 abfd = NULL;
10227 break;
10228
10229 case bfd_link_hash_undefined:
10230 case bfd_link_hash_undefweak:
10231 abfd = h->root.u.undef.abfd;
10232 if (abfd == NULL
10233 || (abfd->flags & DYNAMIC) == 0
10234 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
10235 return false;
10236 break;
10237
10238 case bfd_link_hash_defined:
10239 case bfd_link_hash_defweak:
10240 abfd = h->root.u.def.section->owner;
10241 break;
10242
10243 case bfd_link_hash_common:
10244 abfd = h->root.u.c.p->section->owner;
10245 break;
10246 }
10247 BFD_ASSERT (abfd != NULL);
10248
10249 for (loaded = elf_hash_table (info)->dyn_loaded;
10250 loaded != NULL;
10251 loaded = loaded->next)
10252 {
10253 bfd *input;
10254 Elf_Internal_Shdr *hdr;
10255 size_t symcount;
10256 size_t extsymcount;
10257 size_t extsymoff;
10258 Elf_Internal_Shdr *versymhdr;
10259 Elf_Internal_Sym *isym;
10260 Elf_Internal_Sym *isymend;
10261 Elf_Internal_Sym *isymbuf;
10262 Elf_External_Versym *ever;
10263 Elf_External_Versym *extversym;
10264
10265 input = loaded->abfd;
10266
10267 /* We check each DSO for a possible hidden versioned definition. */
10268 if (input == abfd
10269 || elf_dynversym (input) == 0)
10270 continue;
10271
10272 hdr = &elf_tdata (input)->dynsymtab_hdr;
10273
10274 symcount = hdr->sh_size / bed->s->sizeof_sym;
10275 if (elf_bad_symtab (input))
10276 {
10277 extsymcount = symcount;
10278 extsymoff = 0;
10279 }
10280 else
10281 {
10282 extsymcount = symcount - hdr->sh_info;
10283 extsymoff = hdr->sh_info;
10284 }
10285
10286 if (extsymcount == 0)
10287 continue;
10288
10289 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
10290 NULL, NULL, NULL);
10291 if (isymbuf == NULL)
10292 return false;
10293
10294 /* Read in any version definitions. */
10295 versymhdr = &elf_tdata (input)->dynversym_hdr;
10296 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
10297 || (extversym = (Elf_External_Versym *)
10298 _bfd_malloc_and_read (input, versymhdr->sh_size,
10299 versymhdr->sh_size)) == NULL)
10300 {
10301 free (isymbuf);
10302 return false;
10303 }
10304
10305 ever = extversym + extsymoff;
10306 isymend = isymbuf + extsymcount;
10307 for (isym = isymbuf; isym < isymend; isym++, ever++)
10308 {
10309 const char *name;
10310 Elf_Internal_Versym iver;
10311 unsigned short version_index;
10312
10313 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
10314 || isym->st_shndx == SHN_UNDEF)
10315 continue;
10316
10317 name = bfd_elf_string_from_elf_section (input,
10318 hdr->sh_link,
10319 isym->st_name);
10320 if (strcmp (name, h->root.root.string) != 0)
10321 continue;
10322
10323 _bfd_elf_swap_versym_in (input, ever, &iver);
10324
10325 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
10326 && !(h->def_regular
10327 && h->forced_local))
10328 {
10329 /* If we have a non-hidden versioned sym, then it should
10330 have provided a definition for the undefined sym unless
10331 it is defined in a non-shared object and forced local.
10332 */
10333 abort ();
10334 }
10335
10336 version_index = iver.vs_vers & VERSYM_VERSION;
10337 if (version_index == 1 || version_index == 2)
10338 {
10339 /* This is the base or first version. We can use it. */
10340 free (extversym);
10341 free (isymbuf);
10342 return true;
10343 }
10344 }
10345
10346 free (extversym);
10347 free (isymbuf);
10348 }
10349
10350 return false;
10351 }
10352
10353 /* Convert ELF common symbol TYPE. */
10354
10355 static int
elf_link_convert_common_type(struct bfd_link_info * info,int type)10356 elf_link_convert_common_type (struct bfd_link_info *info, int type)
10357 {
10358 /* Commom symbol can only appear in relocatable link. */
10359 if (!bfd_link_relocatable (info))
10360 abort ();
10361 switch (info->elf_stt_common)
10362 {
10363 case unchanged:
10364 break;
10365 case elf_stt_common:
10366 type = STT_COMMON;
10367 break;
10368 case no_elf_stt_common:
10369 type = STT_OBJECT;
10370 break;
10371 }
10372 return type;
10373 }
10374
10375 /* Add an external symbol to the symbol table. This is called from
10376 the hash table traversal routine. When generating a shared object,
10377 we go through the symbol table twice. The first time we output
10378 anything that might have been forced to local scope in a version
10379 script. The second time we output the symbols that are still
10380 global symbols. */
10381
10382 static bool
elf_link_output_extsym(struct bfd_hash_entry * bh,void * data)10383 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
10384 {
10385 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
10386 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
10387 struct elf_final_link_info *flinfo = eoinfo->flinfo;
10388 bool strip;
10389 Elf_Internal_Sym sym;
10390 asection *input_sec;
10391 const struct elf_backend_data *bed;
10392 long indx;
10393 int ret;
10394 unsigned int type;
10395
10396 if (h->root.type == bfd_link_hash_warning)
10397 {
10398 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10399 if (h->root.type == bfd_link_hash_new)
10400 return true;
10401 }
10402
10403 /* Decide whether to output this symbol in this pass. */
10404 if (eoinfo->localsyms)
10405 {
10406 if (!h->forced_local)
10407 return true;
10408 }
10409 else
10410 {
10411 if (h->forced_local)
10412 return true;
10413 }
10414
10415 bed = get_elf_backend_data (flinfo->output_bfd);
10416
10417 if (h->root.type == bfd_link_hash_undefined)
10418 {
10419 /* If we have an undefined symbol reference here then it must have
10420 come from a shared library that is being linked in. (Undefined
10421 references in regular files have already been handled unless
10422 they are in unreferenced sections which are removed by garbage
10423 collection). */
10424 bool ignore_undef = false;
10425
10426 /* Some symbols may be special in that the fact that they're
10427 undefined can be safely ignored - let backend determine that. */
10428 if (bed->elf_backend_ignore_undef_symbol)
10429 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10430
10431 /* If we are reporting errors for this situation then do so now. */
10432 if (!ignore_undef
10433 && h->ref_dynamic_nonweak
10434 && (!h->ref_regular || flinfo->info->gc_sections)
10435 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10436 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
10437 {
10438 flinfo->info->callbacks->undefined_symbol
10439 (flinfo->info, h->root.root.string,
10440 h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10441 flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10442 && !flinfo->info->warn_unresolved_syms);
10443 }
10444
10445 /* Strip a global symbol defined in a discarded section. */
10446 if (h->indx == -3)
10447 return true;
10448 }
10449
10450 /* We should also warn if a forced local symbol is referenced from
10451 shared libraries. */
10452 if (bfd_link_executable (flinfo->info)
10453 && h->forced_local
10454 && h->ref_dynamic
10455 && h->def_regular
10456 && !h->dynamic_def
10457 && h->ref_dynamic_nonweak
10458 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
10459 {
10460 bfd *def_bfd;
10461 const char *msg;
10462 struct elf_link_hash_entry *hi = h;
10463
10464 /* Check indirect symbol. */
10465 while (hi->root.type == bfd_link_hash_indirect)
10466 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
10467
10468 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
10469 /* xgettext:c-format */
10470 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10471 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
10472 /* xgettext:c-format */
10473 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10474 else
10475 /* xgettext:c-format */
10476 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
10477 def_bfd = flinfo->output_bfd;
10478 if (hi->root.u.def.section != bfd_abs_section_ptr)
10479 def_bfd = hi->root.u.def.section->owner;
10480 _bfd_error_handler (msg, flinfo->output_bfd,
10481 h->root.root.string, def_bfd);
10482 bfd_set_error (bfd_error_bad_value);
10483 eoinfo->failed = true;
10484 return false;
10485 }
10486
10487 /* We don't want to output symbols that have never been mentioned by
10488 a regular file, or that we have been told to strip. However, if
10489 h->indx is set to -2, the symbol is used by a reloc and we must
10490 output it. */
10491 strip = false;
10492 if (h->indx == -2)
10493 ;
10494 else if ((h->def_dynamic
10495 || h->ref_dynamic
10496 || h->root.type == bfd_link_hash_new)
10497 && !h->def_regular
10498 && !h->ref_regular)
10499 strip = true;
10500 else if (flinfo->info->strip == strip_all)
10501 strip = true;
10502 else if (flinfo->info->strip == strip_some
10503 && bfd_hash_lookup (flinfo->info->keep_hash,
10504 h->root.root.string, false, false) == NULL)
10505 strip = true;
10506 else if ((h->root.type == bfd_link_hash_defined
10507 || h->root.type == bfd_link_hash_defweak)
10508 && ((flinfo->info->strip_discarded
10509 && discarded_section (h->root.u.def.section))
10510 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10511 && h->root.u.def.section->owner != NULL
10512 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
10513 strip = true;
10514 else if ((h->root.type == bfd_link_hash_undefined
10515 || h->root.type == bfd_link_hash_undefweak)
10516 && h->root.u.undef.abfd != NULL
10517 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10518 strip = true;
10519
10520 type = h->type;
10521
10522 /* If we're stripping it, and it's not a dynamic symbol, there's
10523 nothing else to do. However, if it is a forced local symbol or
10524 an ifunc symbol we need to give the backend finish_dynamic_symbol
10525 function a chance to make it dynamic. */
10526 if (strip
10527 && h->dynindx == -1
10528 && type != STT_GNU_IFUNC
10529 && !h->forced_local)
10530 return true;
10531
10532 sym.st_value = 0;
10533 sym.st_size = h->size;
10534 sym.st_other = h->other;
10535 switch (h->root.type)
10536 {
10537 default:
10538 case bfd_link_hash_new:
10539 case bfd_link_hash_warning:
10540 abort ();
10541 return false;
10542
10543 case bfd_link_hash_undefined:
10544 case bfd_link_hash_undefweak:
10545 input_sec = bfd_und_section_ptr;
10546 sym.st_shndx = SHN_UNDEF;
10547 break;
10548
10549 case bfd_link_hash_defined:
10550 case bfd_link_hash_defweak:
10551 {
10552 input_sec = h->root.u.def.section;
10553 if (input_sec->output_section != NULL)
10554 {
10555 sym.st_shndx =
10556 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
10557 input_sec->output_section);
10558 if (sym.st_shndx == SHN_BAD)
10559 {
10560 _bfd_error_handler
10561 /* xgettext:c-format */
10562 (_("%pB: could not find output section %pA for input section %pA"),
10563 flinfo->output_bfd, input_sec->output_section, input_sec);
10564 bfd_set_error (bfd_error_nonrepresentable_section);
10565 eoinfo->failed = true;
10566 return false;
10567 }
10568
10569 /* ELF symbols in relocatable files are section relative,
10570 but in nonrelocatable files they are virtual
10571 addresses. */
10572 sym.st_value = h->root.u.def.value + input_sec->output_offset;
10573 if (!bfd_link_relocatable (flinfo->info))
10574 {
10575 sym.st_value += input_sec->output_section->vma;
10576 if (h->type == STT_TLS)
10577 {
10578 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
10579 if (tls_sec != NULL)
10580 sym.st_value -= tls_sec->vma;
10581 }
10582 }
10583 }
10584 else
10585 {
10586 BFD_ASSERT (input_sec->owner == NULL
10587 || (input_sec->owner->flags & DYNAMIC) != 0);
10588 sym.st_shndx = SHN_UNDEF;
10589 input_sec = bfd_und_section_ptr;
10590 }
10591 }
10592 break;
10593
10594 case bfd_link_hash_common:
10595 input_sec = h->root.u.c.p->section;
10596 sym.st_shndx = bed->common_section_index (input_sec);
10597 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10598 break;
10599
10600 case bfd_link_hash_indirect:
10601 /* These symbols are created by symbol versioning. They point
10602 to the decorated version of the name. For example, if the
10603 symbol foo@@GNU_1.2 is the default, which should be used when
10604 foo is used with no version, then we add an indirect symbol
10605 foo which points to foo@@GNU_1.2. We ignore these symbols,
10606 since the indirected symbol is already in the hash table. */
10607 return true;
10608 }
10609
10610 if (type == STT_COMMON || type == STT_OBJECT)
10611 switch (h->root.type)
10612 {
10613 case bfd_link_hash_common:
10614 type = elf_link_convert_common_type (flinfo->info, type);
10615 break;
10616 case bfd_link_hash_defined:
10617 case bfd_link_hash_defweak:
10618 if (bed->common_definition (&sym))
10619 type = elf_link_convert_common_type (flinfo->info, type);
10620 else
10621 type = STT_OBJECT;
10622 break;
10623 case bfd_link_hash_undefined:
10624 case bfd_link_hash_undefweak:
10625 break;
10626 default:
10627 abort ();
10628 }
10629
10630 if (h->forced_local)
10631 {
10632 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10633 /* Turn off visibility on local symbol. */
10634 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10635 }
10636 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10637 else if (h->unique_global && h->def_regular)
10638 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10639 else if (h->root.type == bfd_link_hash_undefweak
10640 || h->root.type == bfd_link_hash_defweak)
10641 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10642 else
10643 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10644 sym.st_target_internal = h->target_internal;
10645
10646 /* Give the processor backend a chance to tweak the symbol value,
10647 and also to finish up anything that needs to be done for this
10648 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10649 forced local syms when non-shared is due to a historical quirk.
10650 STT_GNU_IFUNC symbol must go through PLT. */
10651 if ((h->type == STT_GNU_IFUNC
10652 && h->def_regular
10653 && !bfd_link_relocatable (flinfo->info))
10654 || ((h->dynindx != -1
10655 || h->forced_local)
10656 && ((bfd_link_pic (flinfo->info)
10657 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10658 || h->root.type != bfd_link_hash_undefweak))
10659 || !h->forced_local)
10660 && elf_hash_table (flinfo->info)->dynamic_sections_created))
10661 {
10662 if (! ((*bed->elf_backend_finish_dynamic_symbol)
10663 (flinfo->output_bfd, flinfo->info, h, &sym)))
10664 {
10665 eoinfo->failed = true;
10666 return false;
10667 }
10668 }
10669
10670 /* If we are marking the symbol as undefined, and there are no
10671 non-weak references to this symbol from a regular object, then
10672 mark the symbol as weak undefined; if there are non-weak
10673 references, mark the symbol as strong. We can't do this earlier,
10674 because it might not be marked as undefined until the
10675 finish_dynamic_symbol routine gets through with it. */
10676 if (sym.st_shndx == SHN_UNDEF
10677 && h->ref_regular
10678 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10679 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10680 {
10681 int bindtype;
10682 type = ELF_ST_TYPE (sym.st_info);
10683
10684 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10685 if (type == STT_GNU_IFUNC)
10686 type = STT_FUNC;
10687
10688 if (h->ref_regular_nonweak)
10689 bindtype = STB_GLOBAL;
10690 else
10691 bindtype = STB_WEAK;
10692 sym.st_info = ELF_ST_INFO (bindtype, type);
10693 }
10694
10695 /* If this is a symbol defined in a dynamic library, don't use the
10696 symbol size from the dynamic library. Relinking an executable
10697 against a new library may introduce gratuitous changes in the
10698 executable's symbols if we keep the size. */
10699 if (sym.st_shndx == SHN_UNDEF
10700 && !h->def_regular
10701 && h->def_dynamic)
10702 sym.st_size = 0;
10703
10704 /* If a non-weak symbol with non-default visibility is not defined
10705 locally, it is a fatal error. */
10706 if (!bfd_link_relocatable (flinfo->info)
10707 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10708 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10709 && h->root.type == bfd_link_hash_undefined
10710 && !h->def_regular)
10711 {
10712 const char *msg;
10713
10714 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10715 /* xgettext:c-format */
10716 msg = _("%pB: protected symbol `%s' isn't defined");
10717 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10718 /* xgettext:c-format */
10719 msg = _("%pB: internal symbol `%s' isn't defined");
10720 else
10721 /* xgettext:c-format */
10722 msg = _("%pB: hidden symbol `%s' isn't defined");
10723 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10724 bfd_set_error (bfd_error_bad_value);
10725 eoinfo->failed = true;
10726 return false;
10727 }
10728
10729 /* If this symbol should be put in the .dynsym section, then put it
10730 there now. We already know the symbol index. We also fill in
10731 the entry in the .hash section. */
10732 if (h->dynindx != -1
10733 && elf_hash_table (flinfo->info)->dynamic_sections_created
10734 && elf_hash_table (flinfo->info)->dynsym != NULL
10735 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
10736 {
10737 bfd_byte *esym;
10738
10739 /* Since there is no version information in the dynamic string,
10740 if there is no version info in symbol version section, we will
10741 have a run-time problem if not linking executable, referenced
10742 by shared library, or not bound locally. */
10743 if (h->verinfo.verdef == NULL
10744 && (!bfd_link_executable (flinfo->info)
10745 || h->ref_dynamic
10746 || !h->def_regular))
10747 {
10748 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10749
10750 if (p && p [1] != '\0')
10751 {
10752 _bfd_error_handler
10753 /* xgettext:c-format */
10754 (_("%pB: no symbol version section for versioned symbol `%s'"),
10755 flinfo->output_bfd, h->root.root.string);
10756 eoinfo->failed = true;
10757 return false;
10758 }
10759 }
10760
10761 sym.st_name = h->dynstr_index;
10762 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10763 + h->dynindx * bed->s->sizeof_sym);
10764 if (!check_dynsym (flinfo->output_bfd, &sym))
10765 {
10766 eoinfo->failed = true;
10767 return false;
10768 }
10769
10770 /* Inform the linker of the addition of this symbol. */
10771
10772 if (flinfo->info->callbacks->ctf_new_dynsym)
10773 flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
10774
10775 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
10776
10777 if (flinfo->hash_sec != NULL)
10778 {
10779 size_t hash_entry_size;
10780 bfd_byte *bucketpos;
10781 bfd_vma chain;
10782 size_t bucketcount;
10783 size_t bucket;
10784
10785 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
10786 bucket = h->u.elf_hash_value % bucketcount;
10787
10788 hash_entry_size
10789 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10790 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
10791 + (bucket + 2) * hash_entry_size);
10792 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10793 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10794 bucketpos);
10795 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10796 ((bfd_byte *) flinfo->hash_sec->contents
10797 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10798 }
10799
10800 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
10801 {
10802 Elf_Internal_Versym iversym;
10803 Elf_External_Versym *eversym;
10804
10805 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
10806 {
10807 if (h->verinfo.verdef == NULL
10808 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10809 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
10810 iversym.vs_vers = 1;
10811 else
10812 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10813 }
10814 else
10815 {
10816 if (h->verinfo.vertree == NULL)
10817 iversym.vs_vers = 1;
10818 else
10819 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
10820 if (flinfo->info->create_default_symver)
10821 iversym.vs_vers++;
10822 }
10823
10824 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10825 defined locally. */
10826 if (h->versioned == versioned_hidden && h->def_regular)
10827 iversym.vs_vers |= VERSYM_HIDDEN;
10828
10829 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10830 eversym += h->dynindx;
10831 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10832 }
10833 }
10834
10835 /* If the symbol is undefined, and we didn't output it to .dynsym,
10836 strip it from .symtab too. Obviously we can't do this for
10837 relocatable output or when needed for --emit-relocs. */
10838 else if (input_sec == bfd_und_section_ptr
10839 && h->indx != -2
10840 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10841 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10842 && !bfd_link_relocatable (flinfo->info))
10843 return true;
10844
10845 /* Also strip others that we couldn't earlier due to dynamic symbol
10846 processing. */
10847 if (strip)
10848 return true;
10849 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10850 return true;
10851
10852 /* Output a FILE symbol so that following locals are not associated
10853 with the wrong input file. We need one for forced local symbols
10854 if we've seen more than one FILE symbol or when we have exactly
10855 one FILE symbol but global symbols are present in a file other
10856 than the one with the FILE symbol. We also need one if linker
10857 defined symbols are present. In practice these conditions are
10858 always met, so just emit the FILE symbol unconditionally. */
10859 if (eoinfo->localsyms
10860 && !eoinfo->file_sym_done
10861 && eoinfo->flinfo->filesym_count != 0)
10862 {
10863 Elf_Internal_Sym fsym;
10864
10865 memset (&fsym, 0, sizeof (fsym));
10866 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10867 fsym.st_shndx = SHN_ABS;
10868 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10869 bfd_und_section_ptr, NULL))
10870 return false;
10871
10872 eoinfo->file_sym_done = true;
10873 }
10874
10875 indx = bfd_get_symcount (flinfo->output_bfd);
10876 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10877 input_sec, h);
10878 if (ret == 0)
10879 {
10880 eoinfo->failed = true;
10881 return false;
10882 }
10883 else if (ret == 1)
10884 h->indx = indx;
10885 else if (h->indx == -2)
10886 abort();
10887
10888 return true;
10889 }
10890
10891 /* Return TRUE if special handling is done for relocs in SEC against
10892 symbols defined in discarded sections. */
10893
10894 static bool
elf_section_ignore_discarded_relocs(asection * sec)10895 elf_section_ignore_discarded_relocs (asection *sec)
10896 {
10897 const struct elf_backend_data *bed;
10898
10899 switch (sec->sec_info_type)
10900 {
10901 case SEC_INFO_TYPE_STABS:
10902 case SEC_INFO_TYPE_EH_FRAME:
10903 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10904 return true;
10905 default:
10906 break;
10907 }
10908
10909 bed = get_elf_backend_data (sec->owner);
10910 if (bed->elf_backend_ignore_discarded_relocs != NULL
10911 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10912 return true;
10913
10914 return false;
10915 }
10916
10917 /* Return a mask saying how ld should treat relocations in SEC against
10918 symbols defined in discarded sections. If this function returns
10919 COMPLAIN set, ld will issue a warning message. If this function
10920 returns PRETEND set, and the discarded section was link-once and the
10921 same size as the kept link-once section, ld will pretend that the
10922 symbol was actually defined in the kept section. Otherwise ld will
10923 zero the reloc (at least that is the intent, but some cooperation by
10924 the target dependent code is needed, particularly for REL targets). */
10925
10926 unsigned int
_bfd_elf_default_action_discarded(asection * sec)10927 _bfd_elf_default_action_discarded (asection *sec)
10928 {
10929 if (sec->flags & SEC_DEBUGGING)
10930 return PRETEND;
10931
10932 if (strcmp (".eh_frame", sec->name) == 0)
10933 return 0;
10934
10935 if (strcmp (".gcc_except_table", sec->name) == 0)
10936 return 0;
10937
10938 return COMPLAIN | PRETEND;
10939 }
10940
10941 /* Find a match between a section and a member of a section group. */
10942
10943 static asection *
match_group_member(asection * sec,asection * group,struct bfd_link_info * info)10944 match_group_member (asection *sec, asection *group,
10945 struct bfd_link_info *info)
10946 {
10947 asection *first = elf_next_in_group (group);
10948 asection *s = first;
10949
10950 while (s != NULL)
10951 {
10952 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10953 return s;
10954
10955 s = elf_next_in_group (s);
10956 if (s == first)
10957 break;
10958 }
10959
10960 return NULL;
10961 }
10962
10963 /* Check if the kept section of a discarded section SEC can be used
10964 to replace it. Return the replacement if it is OK. Otherwise return
10965 NULL. */
10966
10967 asection *
_bfd_elf_check_kept_section(asection * sec,struct bfd_link_info * info)10968 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10969 {
10970 asection *kept;
10971
10972 kept = sec->kept_section;
10973 if (kept != NULL)
10974 {
10975 if ((kept->flags & SEC_GROUP) != 0)
10976 kept = match_group_member (sec, kept, info);
10977 if (kept != NULL)
10978 {
10979 if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10980 != (kept->rawsize != 0 ? kept->rawsize : kept->size))
10981 kept = NULL;
10982 else
10983 {
10984 /* Get the real kept section. */
10985 asection *next;
10986 for (next = kept->kept_section;
10987 next != NULL;
10988 next = next->kept_section)
10989 kept = next;
10990 }
10991 }
10992 sec->kept_section = kept;
10993 }
10994 return kept;
10995 }
10996
10997 /* Link an input file into the linker output file. This function
10998 handles all the sections and relocations of the input file at once.
10999 This is so that we only have to read the local symbols once, and
11000 don't have to keep them in memory. */
11001
11002 static bool
elf_link_input_bfd(struct elf_final_link_info * flinfo,bfd * input_bfd)11003 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
11004 {
11005 int (*relocate_section)
11006 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
11007 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
11008 bfd *output_bfd;
11009 Elf_Internal_Shdr *symtab_hdr;
11010 size_t locsymcount;
11011 size_t extsymoff;
11012 Elf_Internal_Sym *isymbuf;
11013 Elf_Internal_Sym *isym;
11014 Elf_Internal_Sym *isymend;
11015 long *pindex;
11016 asection **ppsection;
11017 asection *o;
11018 const struct elf_backend_data *bed;
11019 struct elf_link_hash_entry **sym_hashes;
11020 bfd_size_type address_size;
11021 bfd_vma r_type_mask;
11022 int r_sym_shift;
11023 bool have_file_sym = false;
11024
11025 output_bfd = flinfo->output_bfd;
11026 bed = get_elf_backend_data (output_bfd);
11027 relocate_section = bed->elf_backend_relocate_section;
11028
11029 /* If this is a dynamic object, we don't want to do anything here:
11030 we don't want the local symbols, and we don't want the section
11031 contents. */
11032 if ((input_bfd->flags & DYNAMIC) != 0)
11033 return true;
11034
11035 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
11036 if (elf_bad_symtab (input_bfd))
11037 {
11038 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11039 extsymoff = 0;
11040 }
11041 else
11042 {
11043 locsymcount = symtab_hdr->sh_info;
11044 extsymoff = symtab_hdr->sh_info;
11045 }
11046
11047 /* Enable GNU OSABI features in the output BFD that are used in the input
11048 BFD. */
11049 if (bed->elf_osabi == ELFOSABI_NONE
11050 || bed->elf_osabi == ELFOSABI_GNU
11051 || bed->elf_osabi == ELFOSABI_FREEBSD)
11052 elf_tdata (output_bfd)->has_gnu_osabi
11053 |= (elf_tdata (input_bfd)->has_gnu_osabi
11054 & (bfd_link_relocatable (flinfo->info)
11055 ? -1 : ~elf_gnu_osabi_retain));
11056
11057 /* Read the local symbols. */
11058 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
11059 if (isymbuf == NULL && locsymcount != 0)
11060 {
11061 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
11062 flinfo->internal_syms,
11063 flinfo->external_syms,
11064 flinfo->locsym_shndx);
11065 if (isymbuf == NULL)
11066 return false;
11067 }
11068
11069 /* Find local symbol sections and adjust values of symbols in
11070 SEC_MERGE sections. Write out those local symbols we know are
11071 going into the output file. */
11072 isymend = PTR_ADD (isymbuf, locsymcount);
11073 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
11074 isym < isymend;
11075 isym++, pindex++, ppsection++)
11076 {
11077 asection *isec;
11078 const char *name;
11079 Elf_Internal_Sym osym;
11080 long indx;
11081 int ret;
11082
11083 *pindex = -1;
11084
11085 if (elf_bad_symtab (input_bfd))
11086 {
11087 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
11088 {
11089 *ppsection = NULL;
11090 continue;
11091 }
11092 }
11093
11094 if (isym->st_shndx == SHN_UNDEF)
11095 isec = bfd_und_section_ptr;
11096 else if (isym->st_shndx == SHN_ABS)
11097 isec = bfd_abs_section_ptr;
11098 else if (isym->st_shndx == SHN_COMMON)
11099 isec = bfd_com_section_ptr;
11100 else
11101 {
11102 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
11103 if (isec == NULL)
11104 {
11105 /* Don't attempt to output symbols with st_shnx in the
11106 reserved range other than SHN_ABS and SHN_COMMON. */
11107 isec = bfd_und_section_ptr;
11108 }
11109 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
11110 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
11111 isym->st_value =
11112 _bfd_merged_section_offset (output_bfd, &isec,
11113 elf_section_data (isec)->sec_info,
11114 isym->st_value);
11115 }
11116
11117 *ppsection = isec;
11118
11119 /* Don't output the first, undefined, symbol. In fact, don't
11120 output any undefined local symbol. */
11121 if (isec == bfd_und_section_ptr)
11122 continue;
11123
11124 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
11125 {
11126 /* We never output section symbols. Instead, we use the
11127 section symbol of the corresponding section in the output
11128 file. */
11129 continue;
11130 }
11131
11132 /* If we are stripping all symbols, we don't want to output this
11133 one. */
11134 if (flinfo->info->strip == strip_all)
11135 continue;
11136
11137 /* If we are discarding all local symbols, we don't want to
11138 output this one. If we are generating a relocatable output
11139 file, then some of the local symbols may be required by
11140 relocs; we output them below as we discover that they are
11141 needed. */
11142 if (flinfo->info->discard == discard_all)
11143 continue;
11144
11145 /* If this symbol is defined in a section which we are
11146 discarding, we don't need to keep it. */
11147 if (isym->st_shndx != SHN_UNDEF
11148 && isym->st_shndx < SHN_LORESERVE
11149 && isec->output_section == NULL
11150 && flinfo->info->non_contiguous_regions
11151 && flinfo->info->non_contiguous_regions_warnings)
11152 {
11153 _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
11154 "discards section `%s' from '%s'\n"),
11155 isec->name, bfd_get_filename (isec->owner));
11156 continue;
11157 }
11158
11159 if (isym->st_shndx != SHN_UNDEF
11160 && isym->st_shndx < SHN_LORESERVE
11161 && bfd_section_removed_from_list (output_bfd,
11162 isec->output_section))
11163 continue;
11164
11165 /* Get the name of the symbol. */
11166 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
11167 isym->st_name);
11168 if (name == NULL)
11169 return false;
11170
11171 /* See if we are discarding symbols with this name. */
11172 if ((flinfo->info->strip == strip_some
11173 && (bfd_hash_lookup (flinfo->info->keep_hash, name, false, false)
11174 == NULL))
11175 || (((flinfo->info->discard == discard_sec_merge
11176 && (isec->flags & SEC_MERGE)
11177 && !bfd_link_relocatable (flinfo->info))
11178 || flinfo->info->discard == discard_l)
11179 && bfd_is_local_label_name (input_bfd, name)))
11180 continue;
11181
11182 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
11183 {
11184 if (input_bfd->lto_output)
11185 /* -flto puts a temp file name here. This means builds
11186 are not reproducible. Discard the symbol. */
11187 continue;
11188 have_file_sym = true;
11189 flinfo->filesym_count += 1;
11190 }
11191 if (!have_file_sym)
11192 {
11193 /* In the absence of debug info, bfd_find_nearest_line uses
11194 FILE symbols to determine the source file for local
11195 function symbols. Provide a FILE symbol here if input
11196 files lack such, so that their symbols won't be
11197 associated with a previous input file. It's not the
11198 source file, but the best we can do. */
11199 const char *filename;
11200 have_file_sym = true;
11201 flinfo->filesym_count += 1;
11202 memset (&osym, 0, sizeof (osym));
11203 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
11204 osym.st_shndx = SHN_ABS;
11205 if (input_bfd->lto_output)
11206 filename = NULL;
11207 else
11208 filename = lbasename (bfd_get_filename (input_bfd));
11209 if (!elf_link_output_symstrtab (flinfo, filename, &osym,
11210 bfd_abs_section_ptr, NULL))
11211 return false;
11212 }
11213
11214 osym = *isym;
11215
11216 /* Adjust the section index for the output file. */
11217 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11218 isec->output_section);
11219 if (osym.st_shndx == SHN_BAD)
11220 return false;
11221
11222 /* ELF symbols in relocatable files are section relative, but
11223 in executable files they are virtual addresses. Note that
11224 this code assumes that all ELF sections have an associated
11225 BFD section with a reasonable value for output_offset; below
11226 we assume that they also have a reasonable value for
11227 output_section. Any special sections must be set up to meet
11228 these requirements. */
11229 osym.st_value += isec->output_offset;
11230 if (!bfd_link_relocatable (flinfo->info))
11231 {
11232 osym.st_value += isec->output_section->vma;
11233 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
11234 {
11235 /* STT_TLS symbols are relative to PT_TLS segment base. */
11236 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
11237 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
11238 else
11239 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
11240 STT_NOTYPE);
11241 }
11242 }
11243
11244 indx = bfd_get_symcount (output_bfd);
11245 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
11246 if (ret == 0)
11247 return false;
11248 else if (ret == 1)
11249 *pindex = indx;
11250 }
11251
11252 if (bed->s->arch_size == 32)
11253 {
11254 r_type_mask = 0xff;
11255 r_sym_shift = 8;
11256 address_size = 4;
11257 }
11258 else
11259 {
11260 r_type_mask = 0xffffffff;
11261 r_sym_shift = 32;
11262 address_size = 8;
11263 }
11264
11265 /* Relocate the contents of each section. */
11266 sym_hashes = elf_sym_hashes (input_bfd);
11267 for (o = input_bfd->sections; o != NULL; o = o->next)
11268 {
11269 bfd_byte *contents;
11270
11271 if (! o->linker_mark)
11272 {
11273 /* This section was omitted from the link. */
11274 continue;
11275 }
11276
11277 if (!flinfo->info->resolve_section_groups
11278 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
11279 {
11280 /* Deal with the group signature symbol. */
11281 struct bfd_elf_section_data *sec_data = elf_section_data (o);
11282 unsigned long symndx = sec_data->this_hdr.sh_info;
11283 asection *osec = o->output_section;
11284
11285 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
11286 if (symndx >= locsymcount
11287 || (elf_bad_symtab (input_bfd)
11288 && flinfo->sections[symndx] == NULL))
11289 {
11290 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
11291 while (h->root.type == bfd_link_hash_indirect
11292 || h->root.type == bfd_link_hash_warning)
11293 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11294 /* Arrange for symbol to be output. */
11295 h->indx = -2;
11296 elf_section_data (osec)->this_hdr.sh_info = -2;
11297 }
11298 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
11299 {
11300 /* We'll use the output section target_index. */
11301 asection *sec = flinfo->sections[symndx]->output_section;
11302 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
11303 }
11304 else
11305 {
11306 if (flinfo->indices[symndx] == -1)
11307 {
11308 /* Otherwise output the local symbol now. */
11309 Elf_Internal_Sym sym = isymbuf[symndx];
11310 asection *sec = flinfo->sections[symndx]->output_section;
11311 const char *name;
11312 long indx;
11313 int ret;
11314
11315 name = bfd_elf_string_from_elf_section (input_bfd,
11316 symtab_hdr->sh_link,
11317 sym.st_name);
11318 if (name == NULL)
11319 return false;
11320
11321 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11322 sec);
11323 if (sym.st_shndx == SHN_BAD)
11324 return false;
11325
11326 sym.st_value += o->output_offset;
11327
11328 indx = bfd_get_symcount (output_bfd);
11329 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
11330 NULL);
11331 if (ret == 0)
11332 return false;
11333 else if (ret == 1)
11334 flinfo->indices[symndx] = indx;
11335 else
11336 abort ();
11337 }
11338 elf_section_data (osec)->this_hdr.sh_info
11339 = flinfo->indices[symndx];
11340 }
11341 }
11342
11343 if ((o->flags & SEC_HAS_CONTENTS) == 0
11344 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
11345 continue;
11346
11347 if ((o->flags & SEC_LINKER_CREATED) != 0)
11348 {
11349 /* Section was created by _bfd_elf_link_create_dynamic_sections
11350 or somesuch. */
11351 continue;
11352 }
11353
11354 /* Get the contents of the section. They have been cached by a
11355 relaxation routine. Note that o is a section in an input
11356 file, so the contents field will not have been set by any of
11357 the routines which work on output files. */
11358 if (elf_section_data (o)->this_hdr.contents != NULL)
11359 {
11360 contents = elf_section_data (o)->this_hdr.contents;
11361 if (bed->caches_rawsize
11362 && o->rawsize != 0
11363 && o->rawsize < o->size)
11364 {
11365 memcpy (flinfo->contents, contents, o->rawsize);
11366 contents = flinfo->contents;
11367 }
11368 }
11369 else
11370 {
11371 contents = flinfo->contents;
11372 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
11373 return false;
11374 }
11375
11376 if ((o->flags & SEC_RELOC) != 0)
11377 {
11378 Elf_Internal_Rela *internal_relocs;
11379 Elf_Internal_Rela *rel, *relend;
11380 int action_discarded;
11381 int ret;
11382
11383 /* Get the swapped relocs. */
11384 internal_relocs
11385 = _bfd_elf_link_info_read_relocs (input_bfd, flinfo->info, o,
11386 flinfo->external_relocs,
11387 flinfo->internal_relocs,
11388 false);
11389 if (internal_relocs == NULL
11390 && o->reloc_count > 0)
11391 return false;
11392
11393 action_discarded = -1;
11394 if (!elf_section_ignore_discarded_relocs (o))
11395 action_discarded = (*bed->action_discarded) (o);
11396
11397 /* Run through the relocs evaluating complex reloc symbols and
11398 looking for relocs against symbols from discarded sections
11399 or section symbols from removed link-once sections.
11400 Complain about relocs against discarded sections. Zero
11401 relocs against removed link-once sections. */
11402
11403 rel = internal_relocs;
11404 relend = rel + o->reloc_count;
11405 for ( ; rel < relend; rel++)
11406 {
11407 unsigned long r_symndx = rel->r_info >> r_sym_shift;
11408 unsigned int s_type;
11409 asection **ps, *sec;
11410 struct elf_link_hash_entry *h = NULL;
11411 const char *sym_name;
11412
11413 if (r_symndx == STN_UNDEF)
11414 continue;
11415
11416 if (r_symndx >= locsymcount
11417 || (elf_bad_symtab (input_bfd)
11418 && flinfo->sections[r_symndx] == NULL))
11419 {
11420 h = sym_hashes[r_symndx - extsymoff];
11421
11422 /* Badly formatted input files can contain relocs that
11423 reference non-existant symbols. Check here so that
11424 we do not seg fault. */
11425 if (h == NULL)
11426 {
11427 _bfd_error_handler
11428 /* xgettext:c-format */
11429 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
11430 "that references a non-existent global symbol"),
11431 input_bfd, (uint64_t) rel->r_info, o);
11432 bfd_set_error (bfd_error_bad_value);
11433 return false;
11434 }
11435
11436 while (h->root.type == bfd_link_hash_indirect
11437 || h->root.type == bfd_link_hash_warning)
11438 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11439
11440 s_type = h->type;
11441
11442 /* If a plugin symbol is referenced from a non-IR file,
11443 mark the symbol as undefined. Note that the
11444 linker may attach linker created dynamic sections
11445 to the plugin bfd. Symbols defined in linker
11446 created sections are not plugin symbols. */
11447 if ((h->root.non_ir_ref_regular
11448 || h->root.non_ir_ref_dynamic)
11449 && (h->root.type == bfd_link_hash_defined
11450 || h->root.type == bfd_link_hash_defweak)
11451 && (h->root.u.def.section->flags
11452 & SEC_LINKER_CREATED) == 0
11453 && h->root.u.def.section->owner != NULL
11454 && (h->root.u.def.section->owner->flags
11455 & BFD_PLUGIN) != 0)
11456 {
11457 h->root.type = bfd_link_hash_undefined;
11458 h->root.u.undef.abfd = h->root.u.def.section->owner;
11459 }
11460
11461 ps = NULL;
11462 if (h->root.type == bfd_link_hash_defined
11463 || h->root.type == bfd_link_hash_defweak)
11464 ps = &h->root.u.def.section;
11465
11466 sym_name = h->root.root.string;
11467 }
11468 else
11469 {
11470 Elf_Internal_Sym *sym = isymbuf + r_symndx;
11471
11472 s_type = ELF_ST_TYPE (sym->st_info);
11473 ps = &flinfo->sections[r_symndx];
11474 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11475 sym, *ps);
11476 }
11477
11478 if ((s_type == STT_RELC || s_type == STT_SRELC)
11479 && !bfd_link_relocatable (flinfo->info))
11480 {
11481 bfd_vma val;
11482 bfd_vma dot = (rel->r_offset
11483 + o->output_offset + o->output_section->vma);
11484 #ifdef DEBUG
11485 printf ("Encountered a complex symbol!");
11486 printf (" (input_bfd %s, section %s, reloc %ld\n",
11487 bfd_get_filename (input_bfd), o->name,
11488 (long) (rel - internal_relocs));
11489 printf (" symbol: idx %8.8lx, name %s\n",
11490 r_symndx, sym_name);
11491 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11492 (unsigned long) rel->r_info,
11493 (unsigned long) rel->r_offset);
11494 #endif
11495 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
11496 isymbuf, locsymcount, s_type == STT_SRELC))
11497 return false;
11498
11499 /* Symbol evaluated OK. Update to absolute value. */
11500 set_symbol_value (input_bfd, isymbuf, locsymcount,
11501 r_symndx, val);
11502 continue;
11503 }
11504
11505 if (action_discarded != -1 && ps != NULL)
11506 {
11507 /* Complain if the definition comes from a
11508 discarded section. */
11509 if ((sec = *ps) != NULL && discarded_section (sec))
11510 {
11511 BFD_ASSERT (r_symndx != STN_UNDEF);
11512 if (action_discarded & COMPLAIN)
11513 (*flinfo->info->callbacks->einfo)
11514 /* xgettext:c-format */
11515 (_("%X`%s' referenced in section `%pA' of %pB: "
11516 "defined in discarded section `%pA' of %pB\n"),
11517 sym_name, o, input_bfd, sec, sec->owner);
11518
11519 /* Try to do the best we can to support buggy old
11520 versions of gcc. Pretend that the symbol is
11521 really defined in the kept linkonce section.
11522 FIXME: This is quite broken. Modifying the
11523 symbol here means we will be changing all later
11524 uses of the symbol, not just in this section. */
11525 if (action_discarded & PRETEND)
11526 {
11527 asection *kept;
11528
11529 kept = _bfd_elf_check_kept_section (sec,
11530 flinfo->info);
11531 if (kept != NULL)
11532 {
11533 *ps = kept;
11534 continue;
11535 }
11536 }
11537 }
11538 }
11539 }
11540
11541 /* Relocate the section by invoking a back end routine.
11542
11543 The back end routine is responsible for adjusting the
11544 section contents as necessary, and (if using Rela relocs
11545 and generating a relocatable output file) adjusting the
11546 reloc addend as necessary.
11547
11548 The back end routine does not have to worry about setting
11549 the reloc address or the reloc symbol index.
11550
11551 The back end routine is given a pointer to the swapped in
11552 internal symbols, and can access the hash table entries
11553 for the external symbols via elf_sym_hashes (input_bfd).
11554
11555 When generating relocatable output, the back end routine
11556 must handle STB_LOCAL/STT_SECTION symbols specially. The
11557 output symbol is going to be a section symbol
11558 corresponding to the output section, which will require
11559 the addend to be adjusted. */
11560
11561 ret = (*relocate_section) (output_bfd, flinfo->info,
11562 input_bfd, o, contents,
11563 internal_relocs,
11564 isymbuf,
11565 flinfo->sections);
11566 if (!ret)
11567 return false;
11568
11569 if (ret == 2
11570 || bfd_link_relocatable (flinfo->info)
11571 || flinfo->info->emitrelocations)
11572 {
11573 Elf_Internal_Rela *irela;
11574 Elf_Internal_Rela *irelaend, *irelamid;
11575 bfd_vma last_offset;
11576 struct elf_link_hash_entry **rel_hash;
11577 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11578 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
11579 unsigned int next_erel;
11580 bool rela_normal;
11581 struct bfd_elf_section_data *esdi, *esdo;
11582
11583 esdi = elf_section_data (o);
11584 esdo = elf_section_data (o->output_section);
11585 rela_normal = false;
11586
11587 /* Adjust the reloc addresses and symbol indices. */
11588
11589 irela = internal_relocs;
11590 irelaend = irela + o->reloc_count;
11591 rel_hash = PTR_ADD (esdo->rel.hashes, esdo->rel.count);
11592 /* We start processing the REL relocs, if any. When we reach
11593 IRELAMID in the loop, we switch to the RELA relocs. */
11594 irelamid = irela;
11595 if (esdi->rel.hdr != NULL)
11596 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11597 * bed->s->int_rels_per_ext_rel);
11598 rel_hash_list = rel_hash;
11599 rela_hash_list = NULL;
11600 last_offset = o->output_offset;
11601 if (!bfd_link_relocatable (flinfo->info))
11602 last_offset += o->output_section->vma;
11603 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11604 {
11605 unsigned long r_symndx;
11606 asection *sec;
11607 Elf_Internal_Sym sym;
11608
11609 if (next_erel == bed->s->int_rels_per_ext_rel)
11610 {
11611 rel_hash++;
11612 next_erel = 0;
11613 }
11614
11615 if (irela == irelamid)
11616 {
11617 rel_hash = PTR_ADD (esdo->rela.hashes, esdo->rela.count);
11618 rela_hash_list = rel_hash;
11619 rela_normal = bed->rela_normal;
11620 }
11621
11622 irela->r_offset = _bfd_elf_section_offset (output_bfd,
11623 flinfo->info, o,
11624 irela->r_offset);
11625 if (irela->r_offset >= (bfd_vma) -2)
11626 {
11627 /* This is a reloc for a deleted entry or somesuch.
11628 Turn it into an R_*_NONE reloc, at the same
11629 offset as the last reloc. elf_eh_frame.c and
11630 bfd_elf_discard_info rely on reloc offsets
11631 being ordered. */
11632 irela->r_offset = last_offset;
11633 irela->r_info = 0;
11634 irela->r_addend = 0;
11635 continue;
11636 }
11637
11638 irela->r_offset += o->output_offset;
11639
11640 /* Relocs in an executable have to be virtual addresses. */
11641 if (!bfd_link_relocatable (flinfo->info))
11642 irela->r_offset += o->output_section->vma;
11643
11644 last_offset = irela->r_offset;
11645
11646 r_symndx = irela->r_info >> r_sym_shift;
11647 if (r_symndx == STN_UNDEF)
11648 continue;
11649
11650 if (r_symndx >= locsymcount
11651 || (elf_bad_symtab (input_bfd)
11652 && flinfo->sections[r_symndx] == NULL))
11653 {
11654 struct elf_link_hash_entry *rh;
11655 unsigned long indx;
11656
11657 /* This is a reloc against a global symbol. We
11658 have not yet output all the local symbols, so
11659 we do not know the symbol index of any global
11660 symbol. We set the rel_hash entry for this
11661 reloc to point to the global hash table entry
11662 for this symbol. The symbol index is then
11663 set at the end of bfd_elf_final_link. */
11664 indx = r_symndx - extsymoff;
11665 rh = elf_sym_hashes (input_bfd)[indx];
11666 while (rh->root.type == bfd_link_hash_indirect
11667 || rh->root.type == bfd_link_hash_warning)
11668 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11669
11670 /* Setting the index to -2 tells
11671 elf_link_output_extsym that this symbol is
11672 used by a reloc. */
11673 BFD_ASSERT (rh->indx < 0);
11674 rh->indx = -2;
11675 *rel_hash = rh;
11676
11677 continue;
11678 }
11679
11680 /* This is a reloc against a local symbol. */
11681
11682 *rel_hash = NULL;
11683 sym = isymbuf[r_symndx];
11684 sec = flinfo->sections[r_symndx];
11685 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11686 {
11687 /* I suppose the backend ought to fill in the
11688 section of any STT_SECTION symbol against a
11689 processor specific section. */
11690 r_symndx = STN_UNDEF;
11691 if (bfd_is_abs_section (sec))
11692 ;
11693 else if (sec == NULL || sec->owner == NULL)
11694 {
11695 bfd_set_error (bfd_error_bad_value);
11696 return false;
11697 }
11698 else
11699 {
11700 asection *osec = sec->output_section;
11701
11702 /* If we have discarded a section, the output
11703 section will be the absolute section. In
11704 case of discarded SEC_MERGE sections, use
11705 the kept section. relocate_section should
11706 have already handled discarded linkonce
11707 sections. */
11708 if (bfd_is_abs_section (osec)
11709 && sec->kept_section != NULL
11710 && sec->kept_section->output_section != NULL)
11711 {
11712 osec = sec->kept_section->output_section;
11713 irela->r_addend -= osec->vma;
11714 }
11715
11716 if (!bfd_is_abs_section (osec))
11717 {
11718 r_symndx = osec->target_index;
11719 if (r_symndx == STN_UNDEF)
11720 {
11721 irela->r_addend += osec->vma;
11722 osec = _bfd_nearby_section (output_bfd, osec,
11723 osec->vma);
11724 irela->r_addend -= osec->vma;
11725 r_symndx = osec->target_index;
11726 }
11727 }
11728 }
11729
11730 /* Adjust the addend according to where the
11731 section winds up in the output section. */
11732 if (rela_normal)
11733 irela->r_addend += sec->output_offset;
11734 }
11735 else
11736 {
11737 if (flinfo->indices[r_symndx] == -1)
11738 {
11739 unsigned long shlink;
11740 const char *name;
11741 asection *osec;
11742 long indx;
11743
11744 if (flinfo->info->strip == strip_all)
11745 {
11746 /* You can't do ld -r -s. */
11747 bfd_set_error (bfd_error_invalid_operation);
11748 return false;
11749 }
11750
11751 /* This symbol was skipped earlier, but
11752 since it is needed by a reloc, we
11753 must output it now. */
11754 shlink = symtab_hdr->sh_link;
11755 name = (bfd_elf_string_from_elf_section
11756 (input_bfd, shlink, sym.st_name));
11757 if (name == NULL)
11758 return false;
11759
11760 osec = sec->output_section;
11761 sym.st_shndx =
11762 _bfd_elf_section_from_bfd_section (output_bfd,
11763 osec);
11764 if (sym.st_shndx == SHN_BAD)
11765 return false;
11766
11767 sym.st_value += sec->output_offset;
11768 if (!bfd_link_relocatable (flinfo->info))
11769 {
11770 sym.st_value += osec->vma;
11771 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11772 {
11773 struct elf_link_hash_table *htab
11774 = elf_hash_table (flinfo->info);
11775
11776 /* STT_TLS symbols are relative to PT_TLS
11777 segment base. */
11778 if (htab->tls_sec != NULL)
11779 sym.st_value -= htab->tls_sec->vma;
11780 else
11781 sym.st_info
11782 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11783 STT_NOTYPE);
11784 }
11785 }
11786
11787 indx = bfd_get_symcount (output_bfd);
11788 ret = elf_link_output_symstrtab (flinfo, name,
11789 &sym, sec,
11790 NULL);
11791 if (ret == 0)
11792 return false;
11793 else if (ret == 1)
11794 flinfo->indices[r_symndx] = indx;
11795 else
11796 abort ();
11797 }
11798
11799 r_symndx = flinfo->indices[r_symndx];
11800 }
11801
11802 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11803 | (irela->r_info & r_type_mask));
11804 }
11805
11806 /* Swap out the relocs. */
11807 input_rel_hdr = esdi->rel.hdr;
11808 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
11809 {
11810 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11811 input_rel_hdr,
11812 internal_relocs,
11813 rel_hash_list))
11814 return false;
11815 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11816 * bed->s->int_rels_per_ext_rel);
11817 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
11818 }
11819
11820 input_rela_hdr = esdi->rela.hdr;
11821 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11822 {
11823 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11824 input_rela_hdr,
11825 internal_relocs,
11826 rela_hash_list))
11827 return false;
11828 }
11829 }
11830 }
11831
11832 /* Write out the modified section contents. */
11833 if (bed->elf_backend_write_section
11834 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
11835 contents))
11836 {
11837 /* Section written out. */
11838 }
11839 else switch (o->sec_info_type)
11840 {
11841 case SEC_INFO_TYPE_STABS:
11842 if (! (_bfd_write_section_stabs
11843 (output_bfd,
11844 &elf_hash_table (flinfo->info)->stab_info,
11845 o, &elf_section_data (o)->sec_info, contents)))
11846 return false;
11847 break;
11848 case SEC_INFO_TYPE_MERGE:
11849 if (! _bfd_write_merged_section (output_bfd, o,
11850 elf_section_data (o)->sec_info))
11851 return false;
11852 break;
11853 case SEC_INFO_TYPE_EH_FRAME:
11854 {
11855 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11856 o, contents))
11857 return false;
11858 }
11859 break;
11860 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11861 {
11862 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11863 flinfo->info,
11864 o, contents))
11865 return false;
11866 }
11867 break;
11868 default:
11869 {
11870 if (! (o->flags & SEC_EXCLUDE))
11871 {
11872 file_ptr offset = (file_ptr) o->output_offset;
11873 bfd_size_type todo = o->size;
11874
11875 offset *= bfd_octets_per_byte (output_bfd, o);
11876
11877 if ((o->flags & SEC_ELF_REVERSE_COPY)
11878 && o->size > address_size)
11879 {
11880 /* Reverse-copy input section to output. */
11881
11882 if ((o->size & (address_size - 1)) != 0
11883 || (o->reloc_count != 0
11884 && (o->size * bed->s->int_rels_per_ext_rel
11885 != o->reloc_count * address_size)))
11886 {
11887 _bfd_error_handler
11888 /* xgettext:c-format */
11889 (_("error: %pB: size of section %pA is not "
11890 "multiple of address size"),
11891 input_bfd, o);
11892 bfd_set_error (bfd_error_bad_value);
11893 return false;
11894 }
11895
11896 do
11897 {
11898 todo -= address_size;
11899 if (! bfd_set_section_contents (output_bfd,
11900 o->output_section,
11901 contents + todo,
11902 offset,
11903 address_size))
11904 return false;
11905 if (todo == 0)
11906 break;
11907 offset += address_size;
11908 }
11909 while (1);
11910 }
11911 else if (! bfd_set_section_contents (output_bfd,
11912 o->output_section,
11913 contents,
11914 offset, todo))
11915 return false;
11916 }
11917 }
11918 break;
11919 }
11920 }
11921
11922 return true;
11923 }
11924
11925 /* Generate a reloc when linking an ELF file. This is a reloc
11926 requested by the linker, and does not come from any input file. This
11927 is used to build constructor and destructor tables when linking
11928 with -Ur. */
11929
11930 static bool
elf_reloc_link_order(bfd * output_bfd,struct bfd_link_info * info,asection * output_section,struct bfd_link_order * link_order)11931 elf_reloc_link_order (bfd *output_bfd,
11932 struct bfd_link_info *info,
11933 asection *output_section,
11934 struct bfd_link_order *link_order)
11935 {
11936 reloc_howto_type *howto;
11937 long indx;
11938 bfd_vma offset;
11939 bfd_vma addend;
11940 struct bfd_elf_section_reloc_data *reldata;
11941 struct elf_link_hash_entry **rel_hash_ptr;
11942 Elf_Internal_Shdr *rel_hdr;
11943 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11944 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11945 bfd_byte *erel;
11946 unsigned int i;
11947 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11948
11949 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11950 if (howto == NULL)
11951 {
11952 bfd_set_error (bfd_error_bad_value);
11953 return false;
11954 }
11955
11956 addend = link_order->u.reloc.p->addend;
11957
11958 if (esdo->rel.hdr)
11959 reldata = &esdo->rel;
11960 else if (esdo->rela.hdr)
11961 reldata = &esdo->rela;
11962 else
11963 {
11964 reldata = NULL;
11965 BFD_ASSERT (0);
11966 }
11967
11968 /* Figure out the symbol index. */
11969 rel_hash_ptr = reldata->hashes + reldata->count;
11970 if (link_order->type == bfd_section_reloc_link_order)
11971 {
11972 indx = link_order->u.reloc.p->u.section->target_index;
11973 BFD_ASSERT (indx != 0);
11974 *rel_hash_ptr = NULL;
11975 }
11976 else
11977 {
11978 struct elf_link_hash_entry *h;
11979
11980 /* Treat a reloc against a defined symbol as though it were
11981 actually against the section. */
11982 h = ((struct elf_link_hash_entry *)
11983 bfd_wrapped_link_hash_lookup (output_bfd, info,
11984 link_order->u.reloc.p->u.name,
11985 false, false, true));
11986 if (h != NULL
11987 && (h->root.type == bfd_link_hash_defined
11988 || h->root.type == bfd_link_hash_defweak))
11989 {
11990 asection *section;
11991
11992 section = h->root.u.def.section;
11993 indx = section->output_section->target_index;
11994 *rel_hash_ptr = NULL;
11995 /* It seems that we ought to add the symbol value to the
11996 addend here, but in practice it has already been added
11997 because it was passed to constructor_callback. */
11998 addend += section->output_section->vma + section->output_offset;
11999 }
12000 else if (h != NULL)
12001 {
12002 /* Setting the index to -2 tells elf_link_output_extsym that
12003 this symbol is used by a reloc. */
12004 h->indx = -2;
12005 *rel_hash_ptr = h;
12006 indx = 0;
12007 }
12008 else
12009 {
12010 (*info->callbacks->unattached_reloc)
12011 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
12012 indx = 0;
12013 }
12014 }
12015
12016 /* If this is an inplace reloc, we must write the addend into the
12017 object file. */
12018 if (howto->partial_inplace && addend != 0)
12019 {
12020 bfd_size_type size;
12021 bfd_reloc_status_type rstat;
12022 bfd_byte *buf;
12023 bool ok;
12024 const char *sym_name;
12025 bfd_size_type octets;
12026
12027 size = (bfd_size_type) bfd_get_reloc_size (howto);
12028 buf = (bfd_byte *) bfd_zmalloc (size);
12029 if (buf == NULL && size != 0)
12030 return false;
12031 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
12032 switch (rstat)
12033 {
12034 case bfd_reloc_ok:
12035 break;
12036
12037 default:
12038 case bfd_reloc_outofrange:
12039 abort ();
12040
12041 case bfd_reloc_overflow:
12042 if (link_order->type == bfd_section_reloc_link_order)
12043 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
12044 else
12045 sym_name = link_order->u.reloc.p->u.name;
12046 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
12047 howto->name, addend, NULL, NULL,
12048 (bfd_vma) 0);
12049 break;
12050 }
12051
12052 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
12053 output_section);
12054 ok = bfd_set_section_contents (output_bfd, output_section, buf,
12055 octets, size);
12056 free (buf);
12057 if (! ok)
12058 return false;
12059 }
12060
12061 /* The address of a reloc is relative to the section in a
12062 relocatable file, and is a virtual address in an executable
12063 file. */
12064 offset = link_order->offset;
12065 if (! bfd_link_relocatable (info))
12066 offset += output_section->vma;
12067
12068 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
12069 {
12070 irel[i].r_offset = offset;
12071 irel[i].r_info = 0;
12072 irel[i].r_addend = 0;
12073 }
12074 if (bed->s->arch_size == 32)
12075 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
12076 else
12077 #ifdef BFD64
12078 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
12079 #else
12080 BFD_FAIL();
12081 #endif
12082
12083 rel_hdr = reldata->hdr;
12084 erel = rel_hdr->contents;
12085 if (rel_hdr->sh_type == SHT_REL)
12086 {
12087 erel += reldata->count * bed->s->sizeof_rel;
12088 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
12089 }
12090 else
12091 {
12092 irel[0].r_addend = addend;
12093 erel += reldata->count * bed->s->sizeof_rela;
12094 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
12095 }
12096
12097 ++reldata->count;
12098
12099 return true;
12100 }
12101
12102 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
12103 Returns TRUE upon success, FALSE otherwise. */
12104
12105 static bool
elf_output_implib(bfd * abfd,struct bfd_link_info * info)12106 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
12107 {
12108 bool ret = false;
12109 bfd *implib_bfd;
12110 const struct elf_backend_data *bed;
12111 flagword flags;
12112 enum bfd_architecture arch;
12113 unsigned int mach;
12114 asymbol **sympp = NULL;
12115 long symsize;
12116 long symcount;
12117 long src_count;
12118 elf_symbol_type *osymbuf;
12119 size_t amt;
12120
12121 implib_bfd = info->out_implib_bfd;
12122 bed = get_elf_backend_data (abfd);
12123
12124 if (!bfd_set_format (implib_bfd, bfd_object))
12125 return false;
12126
12127 /* Use flag from executable but make it a relocatable object. */
12128 flags = bfd_get_file_flags (abfd);
12129 flags &= ~HAS_RELOC;
12130 if (!bfd_set_start_address (implib_bfd, 0)
12131 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
12132 return false;
12133
12134 /* Copy architecture of output file to import library file. */
12135 arch = bfd_get_arch (abfd);
12136 mach = bfd_get_mach (abfd);
12137 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
12138 && (abfd->target_defaulted
12139 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
12140 return false;
12141
12142 /* Get symbol table size. */
12143 symsize = bfd_get_symtab_upper_bound (abfd);
12144 if (symsize < 0)
12145 return false;
12146
12147 /* Read in the symbol table. */
12148 sympp = (asymbol **) bfd_malloc (symsize);
12149 if (sympp == NULL)
12150 return false;
12151
12152 symcount = bfd_canonicalize_symtab (abfd, sympp);
12153 if (symcount < 0)
12154 goto free_sym_buf;
12155
12156 /* Allow the BFD backend to copy any private header data it
12157 understands from the output BFD to the import library BFD. */
12158 if (! bfd_copy_private_header_data (abfd, implib_bfd))
12159 goto free_sym_buf;
12160
12161 /* Filter symbols to appear in the import library. */
12162 if (bed->elf_backend_filter_implib_symbols)
12163 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
12164 symcount);
12165 else
12166 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
12167 if (symcount == 0)
12168 {
12169 bfd_set_error (bfd_error_no_symbols);
12170 _bfd_error_handler (_("%pB: no symbol found for import library"),
12171 implib_bfd);
12172 goto free_sym_buf;
12173 }
12174
12175
12176 /* Make symbols absolute. */
12177 amt = symcount * sizeof (*osymbuf);
12178 osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
12179 if (osymbuf == NULL)
12180 goto free_sym_buf;
12181
12182 for (src_count = 0; src_count < symcount; src_count++)
12183 {
12184 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
12185 sizeof (*osymbuf));
12186 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
12187 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
12188 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
12189 osymbuf[src_count].internal_elf_sym.st_value =
12190 osymbuf[src_count].symbol.value;
12191 sympp[src_count] = &osymbuf[src_count].symbol;
12192 }
12193
12194 bfd_set_symtab (implib_bfd, sympp, symcount);
12195
12196 /* Allow the BFD backend to copy any private data it understands
12197 from the output BFD to the import library BFD. This is done last
12198 to permit the routine to look at the filtered symbol table. */
12199 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
12200 goto free_sym_buf;
12201
12202 if (!bfd_close (implib_bfd))
12203 goto free_sym_buf;
12204
12205 ret = true;
12206
12207 free_sym_buf:
12208 free (sympp);
12209 return ret;
12210 }
12211
12212 static void
elf_final_link_free(bfd * obfd,struct elf_final_link_info * flinfo)12213 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
12214 {
12215 asection *o;
12216
12217 if (flinfo->symstrtab != NULL)
12218 _bfd_elf_strtab_free (flinfo->symstrtab);
12219 free (flinfo->contents);
12220 free (flinfo->external_relocs);
12221 free (flinfo->internal_relocs);
12222 free (flinfo->external_syms);
12223 free (flinfo->locsym_shndx);
12224 free (flinfo->internal_syms);
12225 free (flinfo->indices);
12226 free (flinfo->sections);
12227 if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
12228 free (flinfo->symshndxbuf);
12229 for (o = obfd->sections; o != NULL; o = o->next)
12230 {
12231 struct bfd_elf_section_data *esdo = elf_section_data (o);
12232 free (esdo->rel.hashes);
12233 free (esdo->rela.hashes);
12234 }
12235 }
12236
12237 /* Do the final step of an ELF link. */
12238
12239 bool
bfd_elf_final_link(bfd * abfd,struct bfd_link_info * info)12240 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
12241 {
12242 bool dynamic;
12243 bool emit_relocs;
12244 bfd *dynobj;
12245 struct elf_final_link_info flinfo;
12246 asection *o;
12247 struct bfd_link_order *p;
12248 bfd *sub;
12249 bfd_size_type max_contents_size;
12250 bfd_size_type max_external_reloc_size;
12251 bfd_size_type max_internal_reloc_count;
12252 bfd_size_type max_sym_count;
12253 bfd_size_type max_sym_shndx_count;
12254 Elf_Internal_Sym elfsym;
12255 unsigned int i;
12256 Elf_Internal_Shdr *symtab_hdr;
12257 Elf_Internal_Shdr *symtab_shndx_hdr;
12258 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12259 struct elf_outext_info eoinfo;
12260 bool merged;
12261 size_t relativecount;
12262 size_t relr_entsize;
12263 asection *reldyn = 0;
12264 bfd_size_type amt;
12265 asection *attr_section = NULL;
12266 bfd_vma attr_size = 0;
12267 const char *std_attrs_section;
12268 struct elf_link_hash_table *htab = elf_hash_table (info);
12269 bool sections_removed;
12270 bool ret;
12271
12272 if (!is_elf_hash_table (&htab->root))
12273 return false;
12274
12275 if (bfd_link_pic (info))
12276 abfd->flags |= DYNAMIC;
12277
12278 dynamic = htab->dynamic_sections_created;
12279 dynobj = htab->dynobj;
12280
12281 emit_relocs = (bfd_link_relocatable (info)
12282 || info->emitrelocations);
12283
12284 memset (&flinfo, 0, sizeof (flinfo));
12285 flinfo.info = info;
12286 flinfo.output_bfd = abfd;
12287 flinfo.symstrtab = _bfd_elf_strtab_init ();
12288 if (flinfo.symstrtab == NULL)
12289 return false;
12290
12291 if (! dynamic)
12292 {
12293 flinfo.hash_sec = NULL;
12294 flinfo.symver_sec = NULL;
12295 }
12296 else
12297 {
12298 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
12299 /* Note that dynsym_sec can be NULL (on VMS). */
12300 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
12301 /* Note that it is OK if symver_sec is NULL. */
12302 }
12303
12304 if (info->unique_symbol
12305 && !bfd_hash_table_init (&flinfo.local_hash_table,
12306 local_hash_newfunc,
12307 sizeof (struct local_hash_entry)))
12308 return false;
12309
12310 /* The object attributes have been merged. Remove the input
12311 sections from the link, and set the contents of the output
12312 section. */
12313 sections_removed = false;
12314 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
12315 for (o = abfd->sections; o != NULL; o = o->next)
12316 {
12317 bool remove_section = false;
12318
12319 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
12320 || strcmp (o->name, ".gnu.attributes") == 0)
12321 {
12322 for (p = o->map_head.link_order; p != NULL; p = p->next)
12323 {
12324 asection *input_section;
12325
12326 if (p->type != bfd_indirect_link_order)
12327 continue;
12328 input_section = p->u.indirect.section;
12329 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12330 elf_link_input_bfd ignores this section. */
12331 input_section->flags &= ~SEC_HAS_CONTENTS;
12332 }
12333
12334 attr_size = bfd_elf_obj_attr_size (abfd);
12335 bfd_set_section_size (o, attr_size);
12336 /* Skip this section later on. */
12337 o->map_head.link_order = NULL;
12338 if (attr_size)
12339 attr_section = o;
12340 else
12341 remove_section = true;
12342 }
12343 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12344 {
12345 /* Remove empty group section from linker output. */
12346 remove_section = true;
12347 }
12348 if (remove_section)
12349 {
12350 o->flags |= SEC_EXCLUDE;
12351 bfd_section_list_remove (abfd, o);
12352 abfd->section_count--;
12353 sections_removed = true;
12354 }
12355 }
12356 if (sections_removed)
12357 _bfd_fix_excluded_sec_syms (abfd, info);
12358
12359 /* Count up the number of relocations we will output for each output
12360 section, so that we know the sizes of the reloc sections. We
12361 also figure out some maximum sizes. */
12362 max_contents_size = 0;
12363 max_external_reloc_size = 0;
12364 max_internal_reloc_count = 0;
12365 max_sym_count = 0;
12366 max_sym_shndx_count = 0;
12367 merged = false;
12368 for (o = abfd->sections; o != NULL; o = o->next)
12369 {
12370 struct bfd_elf_section_data *esdo = elf_section_data (o);
12371 o->reloc_count = 0;
12372
12373 for (p = o->map_head.link_order; p != NULL; p = p->next)
12374 {
12375 unsigned int reloc_count = 0;
12376 unsigned int additional_reloc_count = 0;
12377 struct bfd_elf_section_data *esdi = NULL;
12378
12379 if (p->type == bfd_section_reloc_link_order
12380 || p->type == bfd_symbol_reloc_link_order)
12381 reloc_count = 1;
12382 else if (p->type == bfd_indirect_link_order)
12383 {
12384 asection *sec;
12385
12386 sec = p->u.indirect.section;
12387
12388 /* Mark all sections which are to be included in the
12389 link. This will normally be every section. We need
12390 to do this so that we can identify any sections which
12391 the linker has decided to not include. */
12392 sec->linker_mark = true;
12393
12394 if (sec->flags & SEC_MERGE)
12395 merged = true;
12396
12397 if (sec->rawsize > max_contents_size)
12398 max_contents_size = sec->rawsize;
12399 if (sec->size > max_contents_size)
12400 max_contents_size = sec->size;
12401
12402 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12403 && (sec->owner->flags & DYNAMIC) == 0)
12404 {
12405 size_t sym_count;
12406
12407 /* We are interested in just local symbols, not all
12408 symbols. */
12409 if (elf_bad_symtab (sec->owner))
12410 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12411 / bed->s->sizeof_sym);
12412 else
12413 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12414
12415 if (sym_count > max_sym_count)
12416 max_sym_count = sym_count;
12417
12418 if (sym_count > max_sym_shndx_count
12419 && elf_symtab_shndx_list (sec->owner) != NULL)
12420 max_sym_shndx_count = sym_count;
12421
12422 esdi = elf_section_data (sec);
12423
12424 if (esdi->this_hdr.sh_type == SHT_REL
12425 || esdi->this_hdr.sh_type == SHT_RELA)
12426 /* Some backends use reloc_count in relocation sections
12427 to count particular types of relocs. Of course,
12428 reloc sections themselves can't have relocations. */
12429 ;
12430 else if (emit_relocs)
12431 {
12432 reloc_count = sec->reloc_count;
12433 if (bed->elf_backend_count_additional_relocs)
12434 {
12435 int c;
12436 c = (*bed->elf_backend_count_additional_relocs) (sec);
12437 additional_reloc_count += c;
12438 }
12439 }
12440 else if (bed->elf_backend_count_relocs)
12441 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12442
12443 if ((sec->flags & SEC_RELOC) != 0)
12444 {
12445 size_t ext_size = 0;
12446
12447 if (esdi->rel.hdr != NULL)
12448 ext_size = esdi->rel.hdr->sh_size;
12449 if (esdi->rela.hdr != NULL)
12450 ext_size += esdi->rela.hdr->sh_size;
12451
12452 if (ext_size > max_external_reloc_size)
12453 max_external_reloc_size = ext_size;
12454 if (sec->reloc_count > max_internal_reloc_count)
12455 max_internal_reloc_count = sec->reloc_count;
12456 }
12457 }
12458 }
12459
12460 if (reloc_count == 0)
12461 continue;
12462
12463 reloc_count += additional_reloc_count;
12464 o->reloc_count += reloc_count;
12465
12466 if (p->type == bfd_indirect_link_order && emit_relocs)
12467 {
12468 if (esdi->rel.hdr)
12469 {
12470 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
12471 esdo->rel.count += additional_reloc_count;
12472 }
12473 if (esdi->rela.hdr)
12474 {
12475 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
12476 esdo->rela.count += additional_reloc_count;
12477 }
12478 }
12479 else
12480 {
12481 if (o->use_rela_p)
12482 esdo->rela.count += reloc_count;
12483 else
12484 esdo->rel.count += reloc_count;
12485 }
12486 }
12487
12488 if (o->reloc_count > 0)
12489 o->flags |= SEC_RELOC;
12490 else
12491 {
12492 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12493 set it (this is probably a bug) and if it is set
12494 assign_section_numbers will create a reloc section. */
12495 o->flags &=~ SEC_RELOC;
12496 }
12497
12498 /* If the SEC_ALLOC flag is not set, force the section VMA to
12499 zero. This is done in elf_fake_sections as well, but forcing
12500 the VMA to 0 here will ensure that relocs against these
12501 sections are handled correctly. */
12502 if ((o->flags & SEC_ALLOC) == 0
12503 && ! o->user_set_vma)
12504 o->vma = 0;
12505 }
12506
12507 if (! bfd_link_relocatable (info) && merged)
12508 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
12509
12510 /* Figure out the file positions for everything but the symbol table
12511 and the relocs. We set symcount to force assign_section_numbers
12512 to create a symbol table. */
12513 abfd->symcount = info->strip != strip_all || emit_relocs;
12514 BFD_ASSERT (! abfd->output_has_begun);
12515 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12516 goto error_return;
12517
12518 /* Set sizes, and assign file positions for reloc sections. */
12519 for (o = abfd->sections; o != NULL; o = o->next)
12520 {
12521 struct bfd_elf_section_data *esdo = elf_section_data (o);
12522 if ((o->flags & SEC_RELOC) != 0)
12523 {
12524 if (esdo->rel.hdr
12525 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
12526 goto error_return;
12527
12528 if (esdo->rela.hdr
12529 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
12530 goto error_return;
12531 }
12532
12533 /* _bfd_elf_compute_section_file_positions makes temporary use
12534 of target_index. Reset it. */
12535 o->target_index = 0;
12536
12537 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12538 to count upwards while actually outputting the relocations. */
12539 esdo->rel.count = 0;
12540 esdo->rela.count = 0;
12541
12542 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12543 && !bfd_section_is_ctf (o))
12544 {
12545 /* Cache the section contents so that they can be compressed
12546 later. Use bfd_malloc since it will be freed by
12547 bfd_compress_section_contents. */
12548 unsigned char *contents = esdo->this_hdr.contents;
12549 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
12550 abort ();
12551 contents
12552 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12553 if (contents == NULL)
12554 goto error_return;
12555 esdo->this_hdr.contents = contents;
12556 }
12557 }
12558
12559 /* We have now assigned file positions for all the sections except .symtab,
12560 .strtab, and non-loaded reloc and compressed debugging sections. We start
12561 the .symtab section at the current file position, and write directly to it.
12562 We build the .strtab section in memory. */
12563 abfd->symcount = 0;
12564 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12565 /* sh_name is set in prep_headers. */
12566 symtab_hdr->sh_type = SHT_SYMTAB;
12567 /* sh_flags, sh_addr and sh_size all start off zero. */
12568 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12569 /* sh_link is set in assign_section_numbers. */
12570 /* sh_info is set below. */
12571 /* sh_offset is set just below. */
12572 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
12573
12574 if (max_sym_count < 20)
12575 max_sym_count = 20;
12576 htab->strtabsize = max_sym_count;
12577 amt = max_sym_count * sizeof (struct elf_sym_strtab);
12578 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12579 if (htab->strtab == NULL)
12580 goto error_return;
12581 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12582 flinfo.symshndxbuf
12583 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12584 ? (Elf_External_Sym_Shndx *) -1 : NULL);
12585
12586 if (info->strip != strip_all || emit_relocs)
12587 {
12588 file_ptr off = elf_next_file_pos (abfd);
12589
12590 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true);
12591
12592 /* Note that at this point elf_next_file_pos (abfd) is
12593 incorrect. We do not yet know the size of the .symtab section.
12594 We correct next_file_pos below, after we do know the size. */
12595
12596 /* Start writing out the symbol table. The first symbol is always a
12597 dummy symbol. */
12598 elfsym.st_value = 0;
12599 elfsym.st_size = 0;
12600 elfsym.st_info = 0;
12601 elfsym.st_other = 0;
12602 elfsym.st_shndx = SHN_UNDEF;
12603 elfsym.st_target_internal = 0;
12604 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12605 bfd_und_section_ptr, NULL) != 1)
12606 goto error_return;
12607
12608 /* Output a symbol for each section if asked or they are used for
12609 relocs. These symbols usually have no names. We store the
12610 index of each one in the index field of the section, so that
12611 we can find it again when outputting relocs. */
12612
12613 if (bfd_keep_unused_section_symbols (abfd) || emit_relocs)
12614 {
12615 bool name_local_sections
12616 = (bed->elf_backend_name_local_section_symbols
12617 && bed->elf_backend_name_local_section_symbols (abfd));
12618 const char *name = NULL;
12619
12620 elfsym.st_size = 0;
12621 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12622 elfsym.st_other = 0;
12623 elfsym.st_value = 0;
12624 elfsym.st_target_internal = 0;
12625 for (i = 1; i < elf_numsections (abfd); i++)
12626 {
12627 o = bfd_section_from_elf_index (abfd, i);
12628 if (o != NULL)
12629 {
12630 o->target_index = bfd_get_symcount (abfd);
12631 elfsym.st_shndx = i;
12632 if (!bfd_link_relocatable (info))
12633 elfsym.st_value = o->vma;
12634 if (name_local_sections)
12635 name = o->name;
12636 if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
12637 NULL) != 1)
12638 goto error_return;
12639 }
12640 }
12641 }
12642 }
12643
12644 /* On some targets like Irix 5 the symbol split between local and global
12645 ones recorded in the sh_info field needs to be done between section
12646 and all other symbols. */
12647 if (bed->elf_backend_elfsym_local_is_section
12648 && bed->elf_backend_elfsym_local_is_section (abfd))
12649 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12650
12651 /* Allocate some memory to hold information read in from the input
12652 files. */
12653 if (max_contents_size != 0)
12654 {
12655 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12656 if (flinfo.contents == NULL)
12657 goto error_return;
12658 }
12659
12660 if (max_external_reloc_size != 0)
12661 {
12662 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12663 if (flinfo.external_relocs == NULL)
12664 goto error_return;
12665 }
12666
12667 if (max_internal_reloc_count != 0)
12668 {
12669 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12670 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12671 if (flinfo.internal_relocs == NULL)
12672 goto error_return;
12673 }
12674
12675 if (max_sym_count != 0)
12676 {
12677 amt = max_sym_count * bed->s->sizeof_sym;
12678 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12679 if (flinfo.external_syms == NULL)
12680 goto error_return;
12681
12682 amt = max_sym_count * sizeof (Elf_Internal_Sym);
12683 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12684 if (flinfo.internal_syms == NULL)
12685 goto error_return;
12686
12687 amt = max_sym_count * sizeof (long);
12688 flinfo.indices = (long int *) bfd_malloc (amt);
12689 if (flinfo.indices == NULL)
12690 goto error_return;
12691
12692 amt = max_sym_count * sizeof (asection *);
12693 flinfo.sections = (asection **) bfd_malloc (amt);
12694 if (flinfo.sections == NULL)
12695 goto error_return;
12696 }
12697
12698 if (max_sym_shndx_count != 0)
12699 {
12700 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
12701 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12702 if (flinfo.locsym_shndx == NULL)
12703 goto error_return;
12704 }
12705
12706 if (htab->tls_sec)
12707 {
12708 bfd_vma base, end = 0; /* Both bytes. */
12709 asection *sec;
12710
12711 for (sec = htab->tls_sec;
12712 sec && (sec->flags & SEC_THREAD_LOCAL);
12713 sec = sec->next)
12714 {
12715 bfd_size_type size = sec->size;
12716 unsigned int opb = bfd_octets_per_byte (abfd, sec);
12717
12718 if (size == 0
12719 && (sec->flags & SEC_HAS_CONTENTS) == 0)
12720 {
12721 struct bfd_link_order *ord = sec->map_tail.link_order;
12722
12723 if (ord != NULL)
12724 size = ord->offset * opb + ord->size;
12725 }
12726 end = sec->vma + size / opb;
12727 }
12728 base = htab->tls_sec->vma;
12729 /* Only align end of TLS section if static TLS doesn't have special
12730 alignment requirements. */
12731 if (bed->static_tls_alignment == 1)
12732 end = align_power (end, htab->tls_sec->alignment_power);
12733 htab->tls_size = end - base;
12734 }
12735
12736 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12737 return false;
12738
12739 /* Finish relative relocations here after regular symbol processing
12740 is finished if DT_RELR is enabled. */
12741 if (info->enable_dt_relr
12742 && bed->finish_relative_relocs
12743 && !bed->finish_relative_relocs (info))
12744 info->callbacks->einfo
12745 (_("%F%P: %pB: failed to finish relative relocations\n"), abfd);
12746
12747 /* Since ELF permits relocations to be against local symbols, we
12748 must have the local symbols available when we do the relocations.
12749 Since we would rather only read the local symbols once, and we
12750 would rather not keep them in memory, we handle all the
12751 relocations for a single input file at the same time.
12752
12753 Unfortunately, there is no way to know the total number of local
12754 symbols until we have seen all of them, and the local symbol
12755 indices precede the global symbol indices. This means that when
12756 we are generating relocatable output, and we see a reloc against
12757 a global symbol, we can not know the symbol index until we have
12758 finished examining all the local symbols to see which ones we are
12759 going to output. To deal with this, we keep the relocations in
12760 memory, and don't output them until the end of the link. This is
12761 an unfortunate waste of memory, but I don't see a good way around
12762 it. Fortunately, it only happens when performing a relocatable
12763 link, which is not the common case. FIXME: If keep_memory is set
12764 we could write the relocs out and then read them again; I don't
12765 know how bad the memory loss will be. */
12766
12767 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12768 sub->output_has_begun = false;
12769 for (o = abfd->sections; o != NULL; o = o->next)
12770 {
12771 for (p = o->map_head.link_order; p != NULL; p = p->next)
12772 {
12773 if (p->type == bfd_indirect_link_order
12774 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12775 == bfd_target_elf_flavour)
12776 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12777 {
12778 if (! sub->output_has_begun)
12779 {
12780 if (! elf_link_input_bfd (&flinfo, sub))
12781 goto error_return;
12782 sub->output_has_begun = true;
12783 }
12784 }
12785 else if (p->type == bfd_section_reloc_link_order
12786 || p->type == bfd_symbol_reloc_link_order)
12787 {
12788 if (! elf_reloc_link_order (abfd, info, o, p))
12789 goto error_return;
12790 }
12791 else
12792 {
12793 if (! _bfd_default_link_order (abfd, info, o, p))
12794 {
12795 if (p->type == bfd_indirect_link_order
12796 && (bfd_get_flavour (sub)
12797 == bfd_target_elf_flavour)
12798 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12799 != bed->s->elfclass))
12800 {
12801 const char *iclass, *oclass;
12802
12803 switch (bed->s->elfclass)
12804 {
12805 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12806 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12807 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12808 default: abort ();
12809 }
12810
12811 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12812 {
12813 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12814 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12815 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12816 default: abort ();
12817 }
12818
12819 bfd_set_error (bfd_error_wrong_format);
12820 _bfd_error_handler
12821 /* xgettext:c-format */
12822 (_("%pB: file class %s incompatible with %s"),
12823 sub, iclass, oclass);
12824 }
12825
12826 goto error_return;
12827 }
12828 }
12829 }
12830 }
12831
12832 /* Free symbol buffer if needed. */
12833 if (!info->reduce_memory_overheads)
12834 {
12835 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12836 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
12837 {
12838 free (elf_tdata (sub)->symbuf);
12839 elf_tdata (sub)->symbuf = NULL;
12840 }
12841 }
12842
12843 ret = true;
12844
12845 /* Output any global symbols that got converted to local in a
12846 version script or due to symbol visibility. We do this in a
12847 separate step since ELF requires all local symbols to appear
12848 prior to any global symbols. FIXME: We should only do this if
12849 some global symbols were, in fact, converted to become local.
12850 FIXME: Will this work correctly with the Irix 5 linker? */
12851 eoinfo.failed = false;
12852 eoinfo.flinfo = &flinfo;
12853 eoinfo.localsyms = true;
12854 eoinfo.file_sym_done = false;
12855 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12856 if (eoinfo.failed)
12857 {
12858 ret = false;
12859 goto return_local_hash_table;
12860 }
12861
12862 /* If backend needs to output some local symbols not present in the hash
12863 table, do it now. */
12864 if (bed->elf_backend_output_arch_local_syms
12865 && (info->strip != strip_all || emit_relocs))
12866 {
12867 if (! ((*bed->elf_backend_output_arch_local_syms)
12868 (abfd, info, &flinfo, elf_link_output_symstrtab)))
12869 {
12870 ret = false;
12871 goto return_local_hash_table;
12872 }
12873 }
12874
12875 /* That wrote out all the local symbols. Finish up the symbol table
12876 with the global symbols. Even if we want to strip everything we
12877 can, we still need to deal with those global symbols that got
12878 converted to local in a version script. */
12879
12880 /* The sh_info field records the index of the first non local symbol. */
12881 if (!symtab_hdr->sh_info)
12882 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12883
12884 if (dynamic
12885 && htab->dynsym != NULL
12886 && htab->dynsym->output_section != bfd_abs_section_ptr)
12887 {
12888 Elf_Internal_Sym sym;
12889 bfd_byte *dynsym = htab->dynsym->contents;
12890
12891 o = htab->dynsym->output_section;
12892 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12893
12894 /* Write out the section symbols for the output sections. */
12895 if (bfd_link_pic (info)
12896 || htab->is_relocatable_executable)
12897 {
12898 asection *s;
12899
12900 sym.st_size = 0;
12901 sym.st_name = 0;
12902 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12903 sym.st_other = 0;
12904 sym.st_target_internal = 0;
12905
12906 for (s = abfd->sections; s != NULL; s = s->next)
12907 {
12908 int indx;
12909 bfd_byte *dest;
12910 long dynindx;
12911
12912 dynindx = elf_section_data (s)->dynindx;
12913 if (dynindx <= 0)
12914 continue;
12915 indx = elf_section_data (s)->this_idx;
12916 BFD_ASSERT (indx > 0);
12917 sym.st_shndx = indx;
12918 if (! check_dynsym (abfd, &sym))
12919 {
12920 ret = false;
12921 goto return_local_hash_table;
12922 }
12923 sym.st_value = s->vma;
12924 dest = dynsym + dynindx * bed->s->sizeof_sym;
12925
12926 /* Inform the linker of the addition of this symbol. */
12927
12928 if (info->callbacks->ctf_new_dynsym)
12929 info->callbacks->ctf_new_dynsym (dynindx, &sym);
12930
12931 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12932 }
12933 }
12934
12935 /* Write out the local dynsyms. */
12936 if (htab->dynlocal)
12937 {
12938 struct elf_link_local_dynamic_entry *e;
12939 for (e = htab->dynlocal; e ; e = e->next)
12940 {
12941 asection *s;
12942 bfd_byte *dest;
12943
12944 /* Copy the internal symbol and turn off visibility.
12945 Note that we saved a word of storage and overwrote
12946 the original st_name with the dynstr_index. */
12947 sym = e->isym;
12948 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12949 sym.st_shndx = SHN_UNDEF;
12950
12951 s = bfd_section_from_elf_index (e->input_bfd,
12952 e->isym.st_shndx);
12953 if (s != NULL
12954 && s->output_section != NULL
12955 && elf_section_data (s->output_section) != NULL)
12956 {
12957 sym.st_shndx =
12958 elf_section_data (s->output_section)->this_idx;
12959 if (! check_dynsym (abfd, &sym))
12960 {
12961 ret = false;
12962 goto return_local_hash_table;
12963 }
12964 sym.st_value = (s->output_section->vma
12965 + s->output_offset
12966 + e->isym.st_value);
12967 }
12968
12969 /* Inform the linker of the addition of this symbol. */
12970
12971 if (info->callbacks->ctf_new_dynsym)
12972 info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
12973
12974 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12975 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12976 }
12977 }
12978 }
12979
12980 /* We get the global symbols from the hash table. */
12981 eoinfo.failed = false;
12982 eoinfo.localsyms = false;
12983 eoinfo.flinfo = &flinfo;
12984 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12985 if (eoinfo.failed)
12986 {
12987 ret = false;
12988 goto return_local_hash_table;
12989 }
12990
12991 /* If backend needs to output some symbols not present in the hash
12992 table, do it now. */
12993 if (bed->elf_backend_output_arch_syms
12994 && (info->strip != strip_all || emit_relocs))
12995 {
12996 if (! ((*bed->elf_backend_output_arch_syms)
12997 (abfd, info, &flinfo, elf_link_output_symstrtab)))
12998 {
12999 ret = false;
13000 goto return_local_hash_table;
13001 }
13002 }
13003
13004 /* Finalize the .strtab section. */
13005 _bfd_elf_strtab_finalize (flinfo.symstrtab);
13006
13007 /* Swap out the .strtab section. */
13008 if (!elf_link_swap_symbols_out (&flinfo))
13009 {
13010 ret = false;
13011 goto return_local_hash_table;
13012 }
13013
13014 /* Now we know the size of the symtab section. */
13015 if (bfd_get_symcount (abfd) > 0)
13016 {
13017 /* Finish up and write out the symbol string table (.strtab)
13018 section. */
13019 Elf_Internal_Shdr *symstrtab_hdr = NULL;
13020 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
13021
13022 if (elf_symtab_shndx_list (abfd))
13023 {
13024 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
13025
13026 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
13027 {
13028 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
13029 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
13030 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
13031 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
13032 symtab_shndx_hdr->sh_size = amt;
13033
13034 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
13035 off, true);
13036
13037 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
13038 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
13039 {
13040 ret = false;
13041 goto return_local_hash_table;
13042 }
13043 }
13044 }
13045
13046 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
13047 /* sh_name was set in prep_headers. */
13048 symstrtab_hdr->sh_type = SHT_STRTAB;
13049 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
13050 symstrtab_hdr->sh_addr = 0;
13051 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
13052 symstrtab_hdr->sh_entsize = 0;
13053 symstrtab_hdr->sh_link = 0;
13054 symstrtab_hdr->sh_info = 0;
13055 /* sh_offset is set just below. */
13056 symstrtab_hdr->sh_addralign = 1;
13057
13058 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
13059 off, true);
13060 elf_next_file_pos (abfd) = off;
13061
13062 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
13063 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
13064 {
13065 ret = false;
13066 goto return_local_hash_table;
13067 }
13068 }
13069
13070 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
13071 {
13072 _bfd_error_handler (_("%pB: failed to generate import library"),
13073 info->out_implib_bfd);
13074 ret = false;
13075 goto return_local_hash_table;
13076 }
13077
13078 /* Adjust the relocs to have the correct symbol indices. */
13079 for (o = abfd->sections; o != NULL; o = o->next)
13080 {
13081 struct bfd_elf_section_data *esdo = elf_section_data (o);
13082 bool sort;
13083
13084 if ((o->flags & SEC_RELOC) == 0)
13085 continue;
13086
13087 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
13088 if (esdo->rel.hdr != NULL
13089 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
13090 {
13091 ret = false;
13092 goto return_local_hash_table;
13093 }
13094 if (esdo->rela.hdr != NULL
13095 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
13096 {
13097 ret = false;
13098 goto return_local_hash_table;
13099 }
13100
13101 /* Set the reloc_count field to 0 to prevent write_relocs from
13102 trying to swap the relocs out itself. */
13103 o->reloc_count = 0;
13104 }
13105
13106 relativecount = 0;
13107 if (dynamic && info->combreloc && dynobj != NULL)
13108 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
13109
13110 relr_entsize = 0;
13111 if (htab->srelrdyn != NULL
13112 && htab->srelrdyn->output_section != NULL
13113 && htab->srelrdyn->size != 0)
13114 {
13115 asection *s = htab->srelrdyn->output_section;
13116 relr_entsize = elf_section_data (s)->this_hdr.sh_entsize;
13117 if (relr_entsize == 0)
13118 {
13119 relr_entsize = bed->s->arch_size / 8;
13120 elf_section_data (s)->this_hdr.sh_entsize = relr_entsize;
13121 }
13122 }
13123
13124 /* If we are linking against a dynamic object, or generating a
13125 shared library, finish up the dynamic linking information. */
13126 if (dynamic)
13127 {
13128 bfd_byte *dyncon, *dynconend;
13129
13130 /* Fix up .dynamic entries. */
13131 o = bfd_get_linker_section (dynobj, ".dynamic");
13132 BFD_ASSERT (o != NULL);
13133
13134 dyncon = o->contents;
13135 dynconend = PTR_ADD (o->contents, o->size);
13136 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13137 {
13138 Elf_Internal_Dyn dyn;
13139 const char *name;
13140 unsigned int type;
13141 bfd_size_type sh_size;
13142 bfd_vma sh_addr;
13143
13144 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13145
13146 switch (dyn.d_tag)
13147 {
13148 default:
13149 continue;
13150 case DT_NULL:
13151 if (relativecount != 0)
13152 {
13153 switch (elf_section_data (reldyn)->this_hdr.sh_type)
13154 {
13155 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
13156 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
13157 }
13158 if (dyn.d_tag != DT_NULL
13159 && dynconend - dyncon >= bed->s->sizeof_dyn)
13160 {
13161 dyn.d_un.d_val = relativecount;
13162 relativecount = 0;
13163 break;
13164 }
13165 relativecount = 0;
13166 }
13167 if (relr_entsize != 0)
13168 {
13169 if (dynconend - dyncon >= 3 * bed->s->sizeof_dyn)
13170 {
13171 asection *s = htab->srelrdyn;
13172 dyn.d_tag = DT_RELR;
13173 dyn.d_un.d_ptr
13174 = s->output_section->vma + s->output_offset;
13175 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13176 dyncon += bed->s->sizeof_dyn;
13177
13178 dyn.d_tag = DT_RELRSZ;
13179 dyn.d_un.d_val = s->size;
13180 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13181 dyncon += bed->s->sizeof_dyn;
13182
13183 dyn.d_tag = DT_RELRENT;
13184 dyn.d_un.d_val = relr_entsize;
13185 relr_entsize = 0;
13186 break;
13187 }
13188 relr_entsize = 0;
13189 }
13190 continue;
13191
13192 case DT_INIT:
13193 name = info->init_function;
13194 goto get_sym;
13195 case DT_FINI:
13196 name = info->fini_function;
13197 get_sym:
13198 {
13199 struct elf_link_hash_entry *h;
13200
13201 h = elf_link_hash_lookup (htab, name, false, false, true);
13202 if (h != NULL
13203 && (h->root.type == bfd_link_hash_defined
13204 || h->root.type == bfd_link_hash_defweak))
13205 {
13206 dyn.d_un.d_ptr = h->root.u.def.value;
13207 o = h->root.u.def.section;
13208 if (o->output_section != NULL)
13209 dyn.d_un.d_ptr += (o->output_section->vma
13210 + o->output_offset);
13211 else
13212 {
13213 /* The symbol is imported from another shared
13214 library and does not apply to this one. */
13215 dyn.d_un.d_ptr = 0;
13216 }
13217 break;
13218 }
13219 }
13220 continue;
13221
13222 case DT_PREINIT_ARRAYSZ:
13223 name = ".preinit_array";
13224 goto get_out_size;
13225 case DT_INIT_ARRAYSZ:
13226 name = ".init_array";
13227 goto get_out_size;
13228 case DT_FINI_ARRAYSZ:
13229 name = ".fini_array";
13230 get_out_size:
13231 o = bfd_get_section_by_name (abfd, name);
13232 if (o == NULL)
13233 {
13234 _bfd_error_handler
13235 (_("could not find section %s"), name);
13236 goto error_return;
13237 }
13238 if (o->size == 0)
13239 _bfd_error_handler
13240 (_("warning: %s section has zero size"), name);
13241 dyn.d_un.d_val = o->size;
13242 break;
13243
13244 case DT_PREINIT_ARRAY:
13245 name = ".preinit_array";
13246 goto get_out_vma;
13247 case DT_INIT_ARRAY:
13248 name = ".init_array";
13249 goto get_out_vma;
13250 case DT_FINI_ARRAY:
13251 name = ".fini_array";
13252 get_out_vma:
13253 o = bfd_get_section_by_name (abfd, name);
13254 goto do_vma;
13255
13256 case DT_HASH:
13257 name = ".hash";
13258 goto get_vma;
13259 case DT_GNU_HASH:
13260 name = ".gnu.hash";
13261 goto get_vma;
13262 case DT_STRTAB:
13263 name = ".dynstr";
13264 goto get_vma;
13265 case DT_SYMTAB:
13266 name = ".dynsym";
13267 goto get_vma;
13268 case DT_VERDEF:
13269 name = ".gnu.version_d";
13270 goto get_vma;
13271 case DT_VERNEED:
13272 name = ".gnu.version_r";
13273 goto get_vma;
13274 case DT_VERSYM:
13275 name = ".gnu.version";
13276 get_vma:
13277 o = bfd_get_linker_section (dynobj, name);
13278 do_vma:
13279 if (o == NULL || bfd_is_abs_section (o->output_section))
13280 {
13281 _bfd_error_handler
13282 (_("could not find section %s"), name);
13283 goto error_return;
13284 }
13285 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
13286 {
13287 _bfd_error_handler
13288 (_("warning: section '%s' is being made into a note"), name);
13289 bfd_set_error (bfd_error_nonrepresentable_section);
13290 goto error_return;
13291 }
13292 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
13293 break;
13294
13295 case DT_REL:
13296 case DT_RELA:
13297 case DT_RELSZ:
13298 case DT_RELASZ:
13299 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
13300 type = SHT_REL;
13301 else
13302 type = SHT_RELA;
13303 sh_size = 0;
13304 sh_addr = 0;
13305 for (i = 1; i < elf_numsections (abfd); i++)
13306 {
13307 Elf_Internal_Shdr *hdr;
13308
13309 hdr = elf_elfsections (abfd)[i];
13310 if (hdr->sh_type == type
13311 && (hdr->sh_flags & SHF_ALLOC) != 0)
13312 {
13313 sh_size += hdr->sh_size;
13314 if (sh_addr == 0
13315 || sh_addr > hdr->sh_addr)
13316 sh_addr = hdr->sh_addr;
13317 }
13318 }
13319
13320 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
13321 {
13322 unsigned int opb = bfd_octets_per_byte (abfd, o);
13323
13324 /* Don't count procedure linkage table relocs in the
13325 overall reloc count. */
13326 sh_size -= htab->srelplt->size;
13327 if (sh_size == 0)
13328 /* If the size is zero, make the address zero too.
13329 This is to avoid a glibc bug. If the backend
13330 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13331 zero, then we'll put DT_RELA at the end of
13332 DT_JMPREL. glibc will interpret the end of
13333 DT_RELA matching the end of DT_JMPREL as the
13334 case where DT_RELA includes DT_JMPREL, and for
13335 LD_BIND_NOW will decide that processing DT_RELA
13336 will process the PLT relocs too. Net result:
13337 No PLT relocs applied. */
13338 sh_addr = 0;
13339
13340 /* If .rela.plt is the first .rela section, exclude
13341 it from DT_RELA. */
13342 else if (sh_addr == (htab->srelplt->output_section->vma
13343 + htab->srelplt->output_offset) * opb)
13344 sh_addr += htab->srelplt->size;
13345 }
13346
13347 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13348 dyn.d_un.d_val = sh_size;
13349 else
13350 dyn.d_un.d_ptr = sh_addr;
13351 break;
13352 }
13353 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13354 }
13355 }
13356
13357 /* If we have created any dynamic sections, then output them. */
13358 if (dynobj != NULL)
13359 {
13360 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
13361 goto error_return;
13362
13363 /* Check for DT_TEXTREL (late, in case the backend removes it). */
13364 if (bfd_link_textrel_check (info)
13365 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL
13366 && o->size != 0)
13367 {
13368 bfd_byte *dyncon, *dynconend;
13369
13370 dyncon = o->contents;
13371 dynconend = o->contents + o->size;
13372 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13373 {
13374 Elf_Internal_Dyn dyn;
13375
13376 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13377
13378 if (dyn.d_tag == DT_TEXTREL)
13379 {
13380 if (info->textrel_check == textrel_check_error)
13381 info->callbacks->einfo
13382 (_("%P%X: read-only segment has dynamic relocations\n"));
13383 else if (bfd_link_dll (info))
13384 info->callbacks->einfo
13385 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13386 else if (bfd_link_pde (info))
13387 info->callbacks->einfo
13388 (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
13389 else
13390 info->callbacks->einfo
13391 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13392 break;
13393 }
13394 }
13395 }
13396
13397 for (o = dynobj->sections; o != NULL; o = o->next)
13398 {
13399 if ((o->flags & SEC_HAS_CONTENTS) == 0
13400 || o->size == 0
13401 || o->output_section == bfd_abs_section_ptr)
13402 continue;
13403 if ((o->flags & SEC_LINKER_CREATED) == 0)
13404 {
13405 /* At this point, we are only interested in sections
13406 created by _bfd_elf_link_create_dynamic_sections. */
13407 continue;
13408 }
13409 if (htab->stab_info.stabstr == o)
13410 continue;
13411 if (htab->eh_info.hdr_sec == o)
13412 continue;
13413 if (strcmp (o->name, ".dynstr") != 0)
13414 {
13415 bfd_size_type octets = ((file_ptr) o->output_offset
13416 * bfd_octets_per_byte (abfd, o));
13417 if (!bfd_set_section_contents (abfd, o->output_section,
13418 o->contents, octets, o->size))
13419 goto error_return;
13420 }
13421 else
13422 {
13423 /* The contents of the .dynstr section are actually in a
13424 stringtab. */
13425 file_ptr off;
13426
13427 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13428 if (bfd_seek (abfd, off, SEEK_SET) != 0
13429 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
13430 goto error_return;
13431 }
13432 }
13433 }
13434
13435 if (!info->resolve_section_groups)
13436 {
13437 bool failed = false;
13438
13439 BFD_ASSERT (bfd_link_relocatable (info));
13440 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13441 if (failed)
13442 goto error_return;
13443 }
13444
13445 /* If we have optimized stabs strings, output them. */
13446 if (htab->stab_info.stabstr != NULL)
13447 {
13448 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
13449 goto error_return;
13450 }
13451
13452 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13453 goto error_return;
13454
13455 if (info->callbacks->emit_ctf)
13456 info->callbacks->emit_ctf ();
13457
13458 elf_final_link_free (abfd, &flinfo);
13459
13460 if (attr_section)
13461 {
13462 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
13463 if (contents == NULL)
13464 {
13465 /* Bail out and fail. */
13466 ret = false;
13467 goto return_local_hash_table;
13468 }
13469 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
13470 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
13471 free (contents);
13472 }
13473
13474 return_local_hash_table:
13475 if (info->unique_symbol)
13476 bfd_hash_table_free (&flinfo.local_hash_table);
13477 return ret;
13478
13479 error_return:
13480 elf_final_link_free (abfd, &flinfo);
13481 ret = false;
13482 goto return_local_hash_table;
13483 }
13484
13485 /* Initialize COOKIE for input bfd ABFD. */
13486
13487 static bool
init_reloc_cookie(struct elf_reloc_cookie * cookie,struct bfd_link_info * info,bfd * abfd)13488 init_reloc_cookie (struct elf_reloc_cookie *cookie,
13489 struct bfd_link_info *info, bfd *abfd)
13490 {
13491 Elf_Internal_Shdr *symtab_hdr;
13492 const struct elf_backend_data *bed;
13493
13494 bed = get_elf_backend_data (abfd);
13495 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13496
13497 cookie->abfd = abfd;
13498 cookie->sym_hashes = elf_sym_hashes (abfd);
13499 cookie->bad_symtab = elf_bad_symtab (abfd);
13500 if (cookie->bad_symtab)
13501 {
13502 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13503 cookie->extsymoff = 0;
13504 }
13505 else
13506 {
13507 cookie->locsymcount = symtab_hdr->sh_info;
13508 cookie->extsymoff = symtab_hdr->sh_info;
13509 }
13510
13511 if (bed->s->arch_size == 32)
13512 cookie->r_sym_shift = 8;
13513 else
13514 cookie->r_sym_shift = 32;
13515
13516 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13517 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13518 {
13519 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13520 cookie->locsymcount, 0,
13521 NULL, NULL, NULL);
13522 if (cookie->locsyms == NULL)
13523 {
13524 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
13525 return false;
13526 }
13527 if (_bfd_link_keep_memory (info) )
13528 {
13529 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13530 info->cache_size += (cookie->locsymcount
13531 * sizeof (Elf_External_Sym_Shndx));
13532 }
13533 }
13534 return true;
13535 }
13536
13537 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13538
13539 static void
fini_reloc_cookie(struct elf_reloc_cookie * cookie,bfd * abfd)13540 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13541 {
13542 Elf_Internal_Shdr *symtab_hdr;
13543
13544 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13545 if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
13546 free (cookie->locsyms);
13547 }
13548
13549 /* Initialize the relocation information in COOKIE for input section SEC
13550 of input bfd ABFD. */
13551
13552 static bool
init_reloc_cookie_rels(struct elf_reloc_cookie * cookie,struct bfd_link_info * info,bfd * abfd,asection * sec)13553 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13554 struct bfd_link_info *info, bfd *abfd,
13555 asection *sec)
13556 {
13557 if (sec->reloc_count == 0)
13558 {
13559 cookie->rels = NULL;
13560 cookie->relend = NULL;
13561 }
13562 else
13563 {
13564 cookie->rels = _bfd_elf_link_info_read_relocs (abfd, info, sec,
13565 NULL, NULL,
13566 _bfd_link_keep_memory (info));
13567 if (cookie->rels == NULL)
13568 return false;
13569 cookie->rel = cookie->rels;
13570 cookie->relend = cookie->rels + sec->reloc_count;
13571 }
13572 cookie->rel = cookie->rels;
13573 return true;
13574 }
13575
13576 /* Free the memory allocated by init_reloc_cookie_rels,
13577 if appropriate. */
13578
13579 static void
fini_reloc_cookie_rels(struct elf_reloc_cookie * cookie,asection * sec)13580 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13581 asection *sec)
13582 {
13583 if (elf_section_data (sec)->relocs != cookie->rels)
13584 free (cookie->rels);
13585 }
13586
13587 /* Initialize the whole of COOKIE for input section SEC. */
13588
13589 static bool
init_reloc_cookie_for_section(struct elf_reloc_cookie * cookie,struct bfd_link_info * info,asection * sec)13590 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13591 struct bfd_link_info *info,
13592 asection *sec)
13593 {
13594 if (!init_reloc_cookie (cookie, info, sec->owner))
13595 goto error1;
13596 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
13597 goto error2;
13598 return true;
13599
13600 error2:
13601 fini_reloc_cookie (cookie, sec->owner);
13602 error1:
13603 return false;
13604 }
13605
13606 /* Free the memory allocated by init_reloc_cookie_for_section,
13607 if appropriate. */
13608
13609 static void
fini_reloc_cookie_for_section(struct elf_reloc_cookie * cookie,asection * sec)13610 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13611 asection *sec)
13612 {
13613 fini_reloc_cookie_rels (cookie, sec);
13614 fini_reloc_cookie (cookie, sec->owner);
13615 }
13616
13617 /* Garbage collect unused sections. */
13618
13619 /* Default gc_mark_hook. */
13620
13621 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)13622 _bfd_elf_gc_mark_hook (asection *sec,
13623 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13624 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13625 struct elf_link_hash_entry *h,
13626 Elf_Internal_Sym *sym)
13627 {
13628 if (h != NULL)
13629 {
13630 switch (h->root.type)
13631 {
13632 case bfd_link_hash_defined:
13633 case bfd_link_hash_defweak:
13634 return h->root.u.def.section;
13635
13636 case bfd_link_hash_common:
13637 return h->root.u.c.p->section;
13638
13639 default:
13640 break;
13641 }
13642 }
13643 else
13644 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13645
13646 return NULL;
13647 }
13648
13649 /* Return the debug definition section. */
13650
13651 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)13652 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13653 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13654 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13655 struct elf_link_hash_entry *h,
13656 Elf_Internal_Sym *sym)
13657 {
13658 if (h != NULL)
13659 {
13660 /* Return the global debug definition section. */
13661 if ((h->root.type == bfd_link_hash_defined
13662 || h->root.type == bfd_link_hash_defweak)
13663 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13664 return h->root.u.def.section;
13665 }
13666 else
13667 {
13668 /* Return the local debug definition section. */
13669 asection *isec = bfd_section_from_elf_index (sec->owner,
13670 sym->st_shndx);
13671 if ((isec->flags & SEC_DEBUGGING) != 0)
13672 return isec;
13673 }
13674
13675 return NULL;
13676 }
13677
13678 /* COOKIE->rel describes a relocation against section SEC, which is
13679 a section we've decided to keep. Return the section that contains
13680 the relocation symbol, or NULL if no section contains it. */
13681
13682 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)13683 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13684 elf_gc_mark_hook_fn gc_mark_hook,
13685 struct elf_reloc_cookie *cookie,
13686 bool *start_stop)
13687 {
13688 unsigned long r_symndx;
13689 struct elf_link_hash_entry *h, *hw;
13690
13691 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
13692 if (r_symndx == STN_UNDEF)
13693 return NULL;
13694
13695 if (r_symndx >= cookie->locsymcount
13696 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13697 {
13698 bool was_marked;
13699
13700 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
13701 if (h == NULL)
13702 {
13703 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
13704 sec->owner);
13705 return NULL;
13706 }
13707 while (h->root.type == bfd_link_hash_indirect
13708 || h->root.type == bfd_link_hash_warning)
13709 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13710
13711 was_marked = h->mark;
13712 h->mark = 1;
13713 /* Keep all aliases of the symbol too. If an object symbol
13714 needs to be copied into .dynbss then all of its aliases
13715 should be present as dynamic symbols, not just the one used
13716 on the copy relocation. */
13717 hw = h;
13718 while (hw->is_weakalias)
13719 {
13720 hw = hw->u.alias;
13721 hw->mark = 1;
13722 }
13723
13724 if (!was_marked && h->start_stop && !h->root.ldscript_def)
13725 {
13726 if (info->start_stop_gc)
13727 return NULL;
13728
13729 /* To work around a glibc bug, mark XXX input sections
13730 when there is a reference to __start_XXX or __stop_XXX
13731 symbols. */
13732 else if (start_stop != NULL)
13733 {
13734 asection *s = h->u2.start_stop_section;
13735 *start_stop = true;
13736 return s;
13737 }
13738 }
13739
13740 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13741 }
13742
13743 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13744 &cookie->locsyms[r_symndx]);
13745 }
13746
13747 /* COOKIE->rel describes a relocation against section SEC, which is
13748 a section we've decided to keep. Mark the section that contains
13749 the relocation symbol. */
13750
13751 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)13752 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13753 asection *sec,
13754 elf_gc_mark_hook_fn gc_mark_hook,
13755 struct elf_reloc_cookie *cookie)
13756 {
13757 asection *rsec;
13758 bool start_stop = false;
13759
13760 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13761 while (rsec != NULL)
13762 {
13763 if (!rsec->gc_mark)
13764 {
13765 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13766 || (rsec->owner->flags & DYNAMIC) != 0)
13767 rsec->gc_mark = 1;
13768 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13769 return false;
13770 }
13771 if (!start_stop)
13772 break;
13773 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
13774 }
13775 return true;
13776 }
13777
13778 /* The mark phase of garbage collection. For a given section, mark
13779 it and any sections in this section's group, and all the sections
13780 which define symbols to which it refers. */
13781
13782 bool
_bfd_elf_gc_mark(struct bfd_link_info * info,asection * sec,elf_gc_mark_hook_fn gc_mark_hook)13783 _bfd_elf_gc_mark (struct bfd_link_info *info,
13784 asection *sec,
13785 elf_gc_mark_hook_fn gc_mark_hook)
13786 {
13787 bool ret;
13788 asection *group_sec, *eh_frame;
13789
13790 sec->gc_mark = 1;
13791
13792 /* Mark all the sections in the group. */
13793 group_sec = elf_section_data (sec)->next_in_group;
13794 if (group_sec && !group_sec->gc_mark)
13795 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
13796 return false;
13797
13798 /* Look through the section relocs. */
13799 ret = true;
13800 eh_frame = elf_eh_frame_section (sec->owner);
13801 if ((sec->flags & SEC_RELOC) != 0
13802 && sec->reloc_count > 0
13803 && sec != eh_frame)
13804 {
13805 struct elf_reloc_cookie cookie;
13806
13807 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13808 ret = false;
13809 else
13810 {
13811 for (; cookie.rel < cookie.relend; cookie.rel++)
13812 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
13813 {
13814 ret = false;
13815 break;
13816 }
13817 fini_reloc_cookie_for_section (&cookie, sec);
13818 }
13819 }
13820
13821 if (ret && eh_frame && elf_fde_list (sec))
13822 {
13823 struct elf_reloc_cookie cookie;
13824
13825 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13826 ret = false;
13827 else
13828 {
13829 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13830 gc_mark_hook, &cookie))
13831 ret = false;
13832 fini_reloc_cookie_for_section (&cookie, eh_frame);
13833 }
13834 }
13835
13836 eh_frame = elf_section_eh_frame_entry (sec);
13837 if (ret && eh_frame && !eh_frame->gc_mark)
13838 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13839 ret = false;
13840
13841 return ret;
13842 }
13843
13844 /* Scan and mark sections in a special or debug section group. */
13845
13846 static void
_bfd_elf_gc_mark_debug_special_section_group(asection * grp)13847 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13848 {
13849 /* Point to first section of section group. */
13850 asection *ssec;
13851 /* Used to iterate the section group. */
13852 asection *msec;
13853
13854 bool is_special_grp = true;
13855 bool is_debug_grp = true;
13856
13857 /* First scan to see if group contains any section other than debug
13858 and special section. */
13859 ssec = msec = elf_next_in_group (grp);
13860 do
13861 {
13862 if ((msec->flags & SEC_DEBUGGING) == 0)
13863 is_debug_grp = false;
13864
13865 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13866 is_special_grp = false;
13867
13868 msec = elf_next_in_group (msec);
13869 }
13870 while (msec != ssec);
13871
13872 /* If this is a pure debug section group or pure special section group,
13873 keep all sections in this group. */
13874 if (is_debug_grp || is_special_grp)
13875 {
13876 do
13877 {
13878 msec->gc_mark = 1;
13879 msec = elf_next_in_group (msec);
13880 }
13881 while (msec != ssec);
13882 }
13883 }
13884
13885 /* Keep debug and special sections. */
13886
13887 bool
_bfd_elf_gc_mark_extra_sections(struct bfd_link_info * info,elf_gc_mark_hook_fn mark_hook)13888 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13889 elf_gc_mark_hook_fn mark_hook)
13890 {
13891 bfd *ibfd;
13892
13893 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13894 {
13895 asection *isec;
13896 bool some_kept;
13897 bool debug_frag_seen;
13898 bool has_kept_debug_info;
13899
13900 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13901 continue;
13902 isec = ibfd->sections;
13903 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13904 continue;
13905
13906 /* Ensure all linker created sections are kept,
13907 see if any other section is already marked,
13908 and note if we have any fragmented debug sections. */
13909 debug_frag_seen = some_kept = has_kept_debug_info = false;
13910 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13911 {
13912 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13913 isec->gc_mark = 1;
13914 else if (isec->gc_mark
13915 && (isec->flags & SEC_ALLOC) != 0
13916 && elf_section_type (isec) != SHT_NOTE)
13917 some_kept = true;
13918 else
13919 {
13920 /* Since all sections, except for backend specific ones,
13921 have been garbage collected, call mark_hook on this
13922 section if any of its linked-to sections is marked. */
13923 asection *linked_to_sec;
13924 for (linked_to_sec = elf_linked_to_section (isec);
13925 linked_to_sec != NULL && !linked_to_sec->linker_mark;
13926 linked_to_sec = elf_linked_to_section (linked_to_sec))
13927 {
13928 if (linked_to_sec->gc_mark)
13929 {
13930 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
13931 return false;
13932 break;
13933 }
13934 linked_to_sec->linker_mark = 1;
13935 }
13936 for (linked_to_sec = elf_linked_to_section (isec);
13937 linked_to_sec != NULL && linked_to_sec->linker_mark;
13938 linked_to_sec = elf_linked_to_section (linked_to_sec))
13939 linked_to_sec->linker_mark = 0;
13940 }
13941
13942 if (!debug_frag_seen
13943 && (isec->flags & SEC_DEBUGGING)
13944 && startswith (isec->name, ".debug_line."))
13945 debug_frag_seen = true;
13946 else if (strcmp (bfd_section_name (isec),
13947 "__patchable_function_entries") == 0
13948 && elf_linked_to_section (isec) == NULL)
13949 info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
13950 "need linked-to section "
13951 "for --gc-sections\n"),
13952 isec->owner, isec);
13953 }
13954
13955 /* If no non-note alloc section in this file will be kept, then
13956 we can toss out the debug and special sections. */
13957 if (!some_kept)
13958 continue;
13959
13960 /* Keep debug and special sections like .comment when they are
13961 not part of a group. Also keep section groups that contain
13962 just debug sections or special sections. NB: Sections with
13963 linked-to section has been handled above. */
13964 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13965 {
13966 if ((isec->flags & SEC_GROUP) != 0)
13967 _bfd_elf_gc_mark_debug_special_section_group (isec);
13968 else if (((isec->flags & SEC_DEBUGGING) != 0
13969 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13970 && elf_next_in_group (isec) == NULL
13971 && elf_linked_to_section (isec) == NULL)
13972 isec->gc_mark = 1;
13973 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13974 has_kept_debug_info = true;
13975 }
13976
13977 /* Look for CODE sections which are going to be discarded,
13978 and find and discard any fragmented debug sections which
13979 are associated with that code section. */
13980 if (debug_frag_seen)
13981 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13982 if ((isec->flags & SEC_CODE) != 0
13983 && isec->gc_mark == 0)
13984 {
13985 unsigned int ilen;
13986 asection *dsec;
13987
13988 ilen = strlen (isec->name);
13989
13990 /* Association is determined by the name of the debug
13991 section containing the name of the code section as
13992 a suffix. For example .debug_line.text.foo is a
13993 debug section associated with .text.foo. */
13994 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13995 {
13996 unsigned int dlen;
13997
13998 if (dsec->gc_mark == 0
13999 || (dsec->flags & SEC_DEBUGGING) == 0)
14000 continue;
14001
14002 dlen = strlen (dsec->name);
14003
14004 if (dlen > ilen
14005 && strncmp (dsec->name + (dlen - ilen),
14006 isec->name, ilen) == 0)
14007 dsec->gc_mark = 0;
14008 }
14009 }
14010
14011 /* Mark debug sections referenced by kept debug sections. */
14012 if (has_kept_debug_info)
14013 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14014 if (isec->gc_mark
14015 && (isec->flags & SEC_DEBUGGING) != 0)
14016 if (!_bfd_elf_gc_mark (info, isec,
14017 elf_gc_mark_debug_section))
14018 return false;
14019 }
14020 return true;
14021 }
14022
14023 static bool
elf_gc_sweep(bfd * abfd,struct bfd_link_info * info)14024 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
14025 {
14026 bfd *sub;
14027 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14028
14029 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14030 {
14031 asection *o;
14032
14033 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14034 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
14035 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14036 continue;
14037 o = sub->sections;
14038 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14039 continue;
14040
14041 for (o = sub->sections; o != NULL; o = o->next)
14042 {
14043 /* When any section in a section group is kept, we keep all
14044 sections in the section group. If the first member of
14045 the section group is excluded, we will also exclude the
14046 group section. */
14047 if (o->flags & SEC_GROUP)
14048 {
14049 asection *first = elf_next_in_group (o);
14050 o->gc_mark = first->gc_mark;
14051 }
14052
14053 if (o->gc_mark)
14054 continue;
14055
14056 /* Skip sweeping sections already excluded. */
14057 if (o->flags & SEC_EXCLUDE)
14058 continue;
14059
14060 /* Since this is early in the link process, it is simple
14061 to remove a section from the output. */
14062 o->flags |= SEC_EXCLUDE;
14063
14064 if (info->print_gc_sections && o->size != 0)
14065 /* xgettext:c-format */
14066 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
14067 o, sub);
14068 }
14069 }
14070
14071 return true;
14072 }
14073
14074 /* Propagate collected vtable information. This is called through
14075 elf_link_hash_traverse. */
14076
14077 static bool
elf_gc_propagate_vtable_entries_used(struct elf_link_hash_entry * h,void * okp)14078 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
14079 {
14080 /* Those that are not vtables. */
14081 if (h->start_stop
14082 || h->u2.vtable == NULL
14083 || h->u2.vtable->parent == NULL)
14084 return true;
14085
14086 /* Those vtables that do not have parents, we cannot merge. */
14087 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
14088 return true;
14089
14090 /* If we've already been done, exit. */
14091 if (h->u2.vtable->used && h->u2.vtable->used[-1])
14092 return true;
14093
14094 /* Make sure the parent's table is up to date. */
14095 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
14096
14097 if (h->u2.vtable->used == NULL)
14098 {
14099 /* None of this table's entries were referenced. Re-use the
14100 parent's table. */
14101 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
14102 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
14103 }
14104 else
14105 {
14106 size_t n;
14107 bool *cu, *pu;
14108
14109 /* Or the parent's entries into ours. */
14110 cu = h->u2.vtable->used;
14111 cu[-1] = true;
14112 pu = h->u2.vtable->parent->u2.vtable->used;
14113 if (pu != NULL)
14114 {
14115 const struct elf_backend_data *bed;
14116 unsigned int log_file_align;
14117
14118 bed = get_elf_backend_data (h->root.u.def.section->owner);
14119 log_file_align = bed->s->log_file_align;
14120 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
14121 while (n--)
14122 {
14123 if (*pu)
14124 *cu = true;
14125 pu++;
14126 cu++;
14127 }
14128 }
14129 }
14130
14131 return true;
14132 }
14133
14134 struct link_info_ok
14135 {
14136 struct bfd_link_info *info;
14137 bool ok;
14138 };
14139
14140 static bool
elf_gc_smash_unused_vtentry_relocs(struct elf_link_hash_entry * h,void * ptr)14141 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h,
14142 void *ptr)
14143 {
14144 asection *sec;
14145 bfd_vma hstart, hend;
14146 Elf_Internal_Rela *relstart, *relend, *rel;
14147 const struct elf_backend_data *bed;
14148 unsigned int log_file_align;
14149 struct link_info_ok *info = (struct link_info_ok *) ptr;
14150
14151 /* Take care of both those symbols that do not describe vtables as
14152 well as those that are not loaded. */
14153 if (h->start_stop
14154 || h->u2.vtable == NULL
14155 || h->u2.vtable->parent == NULL)
14156 return true;
14157
14158 BFD_ASSERT (h->root.type == bfd_link_hash_defined
14159 || h->root.type == bfd_link_hash_defweak);
14160
14161 sec = h->root.u.def.section;
14162 hstart = h->root.u.def.value;
14163 hend = hstart + h->size;
14164
14165 relstart = _bfd_elf_link_info_read_relocs (sec->owner, info->info,
14166 sec, NULL, NULL, true);
14167 if (!relstart)
14168 return info->ok = false;
14169 bed = get_elf_backend_data (sec->owner);
14170 log_file_align = bed->s->log_file_align;
14171
14172 relend = relstart + sec->reloc_count;
14173
14174 for (rel = relstart; rel < relend; ++rel)
14175 if (rel->r_offset >= hstart && rel->r_offset < hend)
14176 {
14177 /* If the entry is in use, do nothing. */
14178 if (h->u2.vtable->used
14179 && (rel->r_offset - hstart) < h->u2.vtable->size)
14180 {
14181 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
14182 if (h->u2.vtable->used[entry])
14183 continue;
14184 }
14185 /* Otherwise, kill it. */
14186 rel->r_offset = rel->r_info = rel->r_addend = 0;
14187 }
14188
14189 return true;
14190 }
14191
14192 /* Mark sections containing dynamically referenced symbols. When
14193 building shared libraries, we must assume that any visible symbol is
14194 referenced. */
14195
14196 bool
bfd_elf_gc_mark_dynamic_ref_symbol(struct elf_link_hash_entry * h,void * inf)14197 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
14198 {
14199 struct bfd_link_info *info = (struct bfd_link_info *) inf;
14200 struct bfd_elf_dynamic_list *d = info->dynamic_list;
14201
14202 if ((h->root.type == bfd_link_hash_defined
14203 || h->root.type == bfd_link_hash_defweak)
14204 && (!h->start_stop
14205 || h->root.ldscript_def
14206 || !info->start_stop_gc)
14207 && ((h->ref_dynamic && !h->forced_local)
14208 || ((h->def_regular || ELF_COMMON_DEF_P (h))
14209 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
14210 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
14211 && (!bfd_link_executable (info)
14212 || info->gc_keep_exported
14213 || info->export_dynamic
14214 || (h->dynamic
14215 && d != NULL
14216 && (*d->match) (&d->head, NULL, h->root.root.string)))
14217 && (h->versioned >= versioned
14218 || !bfd_hide_sym_by_version (info->version_info,
14219 h->root.root.string)))))
14220 h->root.u.def.section->flags |= SEC_KEEP;
14221
14222 return true;
14223 }
14224
14225 /* Keep all sections containing symbols undefined on the command-line,
14226 and the section containing the entry symbol. */
14227
14228 void
_bfd_elf_gc_keep(struct bfd_link_info * info)14229 _bfd_elf_gc_keep (struct bfd_link_info *info)
14230 {
14231 struct bfd_sym_chain *sym;
14232
14233 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
14234 {
14235 struct elf_link_hash_entry *h;
14236
14237 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
14238 false, false, false);
14239
14240 if (h != NULL
14241 && (h->root.type == bfd_link_hash_defined
14242 || h->root.type == bfd_link_hash_defweak)
14243 && !bfd_is_const_section (h->root.u.def.section))
14244 h->root.u.def.section->flags |= SEC_KEEP;
14245 }
14246 }
14247
14248 bool
bfd_elf_parse_eh_frame_entries(bfd * abfd ATTRIBUTE_UNUSED,struct bfd_link_info * info)14249 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
14250 struct bfd_link_info *info)
14251 {
14252 bfd *ibfd = info->input_bfds;
14253
14254 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14255 {
14256 asection *sec;
14257 struct elf_reloc_cookie cookie;
14258
14259 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14260 continue;
14261 sec = ibfd->sections;
14262 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14263 continue;
14264
14265 if (!init_reloc_cookie (&cookie, info, ibfd))
14266 return false;
14267
14268 for (sec = ibfd->sections; sec; sec = sec->next)
14269 {
14270 if (startswith (bfd_section_name (sec), ".eh_frame_entry")
14271 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
14272 {
14273 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
14274 fini_reloc_cookie_rels (&cookie, sec);
14275 }
14276 }
14277 }
14278 return true;
14279 }
14280
14281 /* Do mark and sweep of unused sections. */
14282
14283 bool
bfd_elf_gc_sections(bfd * abfd,struct bfd_link_info * info)14284 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
14285 {
14286 bool ok = true;
14287 bfd *sub;
14288 elf_gc_mark_hook_fn gc_mark_hook;
14289 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14290 struct elf_link_hash_table *htab;
14291 struct link_info_ok info_ok;
14292
14293 if (!bed->can_gc_sections
14294 || !is_elf_hash_table (info->hash))
14295 {
14296 _bfd_error_handler(_("warning: gc-sections option ignored"));
14297 return true;
14298 }
14299
14300 bed->gc_keep (info);
14301 htab = elf_hash_table (info);
14302
14303 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14304 at the .eh_frame section if we can mark the FDEs individually. */
14305 for (sub = info->input_bfds;
14306 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
14307 sub = sub->link.next)
14308 {
14309 asection *sec;
14310 struct elf_reloc_cookie cookie;
14311
14312 sec = sub->sections;
14313 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14314 continue;
14315 sec = bfd_get_section_by_name (sub, ".eh_frame");
14316 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
14317 {
14318 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
14319 if (elf_section_data (sec)->sec_info
14320 && (sec->flags & SEC_LINKER_CREATED) == 0)
14321 elf_eh_frame_section (sub) = sec;
14322 fini_reloc_cookie_for_section (&cookie, sec);
14323 sec = bfd_get_next_section_by_name (NULL, sec);
14324 }
14325 }
14326
14327 /* Apply transitive closure to the vtable entry usage info. */
14328 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
14329 if (!ok)
14330 return false;
14331
14332 /* Kill the vtable relocations that were not used. */
14333 info_ok.info = info;
14334 info_ok.ok = true;
14335 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &info_ok);
14336 if (!info_ok.ok)
14337 return false;
14338
14339 /* Mark dynamically referenced symbols. */
14340 if (htab->dynamic_sections_created || info->gc_keep_exported)
14341 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
14342
14343 /* Grovel through relocs to find out who stays ... */
14344 gc_mark_hook = bed->gc_mark_hook;
14345 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
14346 {
14347 asection *o;
14348
14349 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
14350 || elf_object_id (sub) != elf_hash_table_id (htab)
14351 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
14352 continue;
14353
14354 o = sub->sections;
14355 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14356 continue;
14357
14358 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14359 Also treat note sections as a root, if the section is not part
14360 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14361 well as FINI_ARRAY sections for ld -r. */
14362 for (o = sub->sections; o != NULL; o = o->next)
14363 if (!o->gc_mark
14364 && (o->flags & SEC_EXCLUDE) == 0
14365 && ((o->flags & SEC_KEEP) != 0
14366 || (bfd_link_relocatable (info)
14367 && ((elf_section_data (o)->this_hdr.sh_type
14368 == SHT_PREINIT_ARRAY)
14369 || (elf_section_data (o)->this_hdr.sh_type
14370 == SHT_INIT_ARRAY)
14371 || (elf_section_data (o)->this_hdr.sh_type
14372 == SHT_FINI_ARRAY)))
14373 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
14374 && elf_next_in_group (o) == NULL
14375 && elf_linked_to_section (o) == NULL)
14376 || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
14377 && (elf_section_flags (o) & SHF_GNU_RETAIN))))
14378 {
14379 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
14380 return false;
14381 }
14382 }
14383
14384 /* Allow the backend to mark additional target specific sections. */
14385 bed->gc_mark_extra_sections (info, gc_mark_hook);
14386
14387 /* ... and mark SEC_EXCLUDE for those that go. */
14388 return elf_gc_sweep (abfd, info);
14389 }
14390
14391 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14392
14393 bool
bfd_elf_gc_record_vtinherit(bfd * abfd,asection * sec,struct elf_link_hash_entry * h,bfd_vma offset)14394 bfd_elf_gc_record_vtinherit (bfd *abfd,
14395 asection *sec,
14396 struct elf_link_hash_entry *h,
14397 bfd_vma offset)
14398 {
14399 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14400 struct elf_link_hash_entry **search, *child;
14401 size_t extsymcount;
14402 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14403
14404 /* The sh_info field of the symtab header tells us where the
14405 external symbols start. We don't care about the local symbols at
14406 this point. */
14407 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
14408 if (!elf_bad_symtab (abfd))
14409 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
14410
14411 sym_hashes = elf_sym_hashes (abfd);
14412 sym_hashes_end = PTR_ADD (sym_hashes, extsymcount);
14413
14414 /* Hunt down the child symbol, which is in this section at the same
14415 offset as the relocation. */
14416 for (search = sym_hashes; search != sym_hashes_end; ++search)
14417 {
14418 if ((child = *search) != NULL
14419 && (child->root.type == bfd_link_hash_defined
14420 || child->root.type == bfd_link_hash_defweak)
14421 && child->root.u.def.section == sec
14422 && child->root.u.def.value == offset)
14423 goto win;
14424 }
14425
14426 /* xgettext:c-format */
14427 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
14428 abfd, sec, (uint64_t) offset);
14429 bfd_set_error (bfd_error_invalid_operation);
14430 return false;
14431
14432 win:
14433 if (!child->u2.vtable)
14434 {
14435 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
14436 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
14437 if (!child->u2.vtable)
14438 return false;
14439 }
14440 if (!h)
14441 {
14442 /* This *should* only be the absolute section. It could potentially
14443 be that someone has defined a non-global vtable though, which
14444 would be bad. It isn't worth paging in the local symbols to be
14445 sure though; that case should simply be handled by the assembler. */
14446
14447 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
14448 }
14449 else
14450 child->u2.vtable->parent = h;
14451
14452 return true;
14453 }
14454
14455 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14456
14457 bool
bfd_elf_gc_record_vtentry(bfd * abfd,asection * sec,struct elf_link_hash_entry * h,bfd_vma addend)14458 bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
14459 struct elf_link_hash_entry *h,
14460 bfd_vma addend)
14461 {
14462 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14463 unsigned int log_file_align = bed->s->log_file_align;
14464
14465 if (!h)
14466 {
14467 /* xgettext:c-format */
14468 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14469 abfd, sec);
14470 bfd_set_error (bfd_error_bad_value);
14471 return false;
14472 }
14473
14474 if (!h->u2.vtable)
14475 {
14476 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14477 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14478 if (!h->u2.vtable)
14479 return false;
14480 }
14481
14482 if (addend >= h->u2.vtable->size)
14483 {
14484 size_t size, bytes, file_align;
14485 bool *ptr = h->u2.vtable->used;
14486
14487 /* While the symbol is undefined, we have to be prepared to handle
14488 a zero size. */
14489 file_align = 1 << log_file_align;
14490 if (h->root.type == bfd_link_hash_undefined)
14491 size = addend + file_align;
14492 else
14493 {
14494 size = h->size;
14495 if (addend >= size)
14496 {
14497 /* Oops! We've got a reference past the defined end of
14498 the table. This is probably a bug -- shall we warn? */
14499 size = addend + file_align;
14500 }
14501 }
14502 size = (size + file_align - 1) & -file_align;
14503
14504 /* Allocate one extra entry for use as a "done" flag for the
14505 consolidation pass. */
14506 bytes = ((size >> log_file_align) + 1) * sizeof (bool);
14507
14508 if (ptr)
14509 {
14510 ptr = (bool *) bfd_realloc (ptr - 1, bytes);
14511
14512 if (ptr != NULL)
14513 {
14514 size_t oldbytes;
14515
14516 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
14517 * sizeof (bool));
14518 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14519 }
14520 }
14521 else
14522 ptr = (bool *) bfd_zmalloc (bytes);
14523
14524 if (ptr == NULL)
14525 return false;
14526
14527 /* And arrange for that done flag to be at index -1. */
14528 h->u2.vtable->used = ptr + 1;
14529 h->u2.vtable->size = size;
14530 }
14531
14532 h->u2.vtable->used[addend >> log_file_align] = true;
14533
14534 return true;
14535 }
14536
14537 /* Map an ELF section header flag to its corresponding string. */
14538 typedef struct
14539 {
14540 char *flag_name;
14541 flagword flag_value;
14542 } elf_flags_to_name_table;
14543
14544 static const elf_flags_to_name_table elf_flags_to_names [] =
14545 {
14546 { "SHF_WRITE", SHF_WRITE },
14547 { "SHF_ALLOC", SHF_ALLOC },
14548 { "SHF_EXECINSTR", SHF_EXECINSTR },
14549 { "SHF_MERGE", SHF_MERGE },
14550 { "SHF_STRINGS", SHF_STRINGS },
14551 { "SHF_INFO_LINK", SHF_INFO_LINK},
14552 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14553 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14554 { "SHF_GROUP", SHF_GROUP },
14555 { "SHF_TLS", SHF_TLS },
14556 { "SHF_MASKOS", SHF_MASKOS },
14557 { "SHF_EXCLUDE", SHF_EXCLUDE },
14558 };
14559
14560 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14561 bool
bfd_elf_lookup_section_flags(struct bfd_link_info * info,struct flag_info * flaginfo,asection * section)14562 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
14563 struct flag_info *flaginfo,
14564 asection *section)
14565 {
14566 const bfd_vma sh_flags = elf_section_flags (section);
14567
14568 if (!flaginfo->flags_initialized)
14569 {
14570 bfd *obfd = info->output_bfd;
14571 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14572 struct flag_info_list *tf = flaginfo->flag_list;
14573 int with_hex = 0;
14574 int without_hex = 0;
14575
14576 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
14577 {
14578 unsigned i;
14579 flagword (*lookup) (char *);
14580
14581 lookup = bed->elf_backend_lookup_section_flags_hook;
14582 if (lookup != NULL)
14583 {
14584 flagword hexval = (*lookup) ((char *) tf->name);
14585
14586 if (hexval != 0)
14587 {
14588 if (tf->with == with_flags)
14589 with_hex |= hexval;
14590 else if (tf->with == without_flags)
14591 without_hex |= hexval;
14592 tf->valid = true;
14593 continue;
14594 }
14595 }
14596 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
14597 {
14598 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
14599 {
14600 if (tf->with == with_flags)
14601 with_hex |= elf_flags_to_names[i].flag_value;
14602 else if (tf->with == without_flags)
14603 without_hex |= elf_flags_to_names[i].flag_value;
14604 tf->valid = true;
14605 break;
14606 }
14607 }
14608 if (!tf->valid)
14609 {
14610 info->callbacks->einfo
14611 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
14612 return false;
14613 }
14614 }
14615 flaginfo->flags_initialized = true;
14616 flaginfo->only_with_flags |= with_hex;
14617 flaginfo->not_with_flags |= without_hex;
14618 }
14619
14620 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
14621 return false;
14622
14623 if ((flaginfo->not_with_flags & sh_flags) != 0)
14624 return false;
14625
14626 return true;
14627 }
14628
14629 struct alloc_got_off_arg {
14630 bfd_vma gotoff;
14631 struct bfd_link_info *info;
14632 };
14633
14634 /* We need a special top-level link routine to convert got reference counts
14635 to real got offsets. */
14636
14637 static bool
elf_gc_allocate_got_offsets(struct elf_link_hash_entry * h,void * arg)14638 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14639 {
14640 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
14641 bfd *obfd = gofarg->info->output_bfd;
14642 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14643
14644 if (h->got.refcount > 0)
14645 {
14646 h->got.offset = gofarg->gotoff;
14647 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
14648 }
14649 else
14650 h->got.offset = (bfd_vma) -1;
14651
14652 return true;
14653 }
14654
14655 /* And an accompanying bit to work out final got entry offsets once
14656 we're done. Should be called from final_link. */
14657
14658 bool
bfd_elf_gc_common_finalize_got_offsets(bfd * abfd,struct bfd_link_info * info)14659 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14660 struct bfd_link_info *info)
14661 {
14662 bfd *i;
14663 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14664 bfd_vma gotoff;
14665 struct alloc_got_off_arg gofarg;
14666
14667 BFD_ASSERT (abfd == info->output_bfd);
14668
14669 if (! is_elf_hash_table (info->hash))
14670 return false;
14671
14672 /* The GOT offset is relative to the .got section, but the GOT header is
14673 put into the .got.plt section, if the backend uses it. */
14674 if (bed->want_got_plt)
14675 gotoff = 0;
14676 else
14677 gotoff = bed->got_header_size;
14678
14679 /* Do the local .got entries first. */
14680 for (i = info->input_bfds; i; i = i->link.next)
14681 {
14682 bfd_signed_vma *local_got;
14683 size_t j, locsymcount;
14684 Elf_Internal_Shdr *symtab_hdr;
14685
14686 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14687 continue;
14688
14689 local_got = elf_local_got_refcounts (i);
14690 if (!local_got)
14691 continue;
14692
14693 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14694 if (elf_bad_symtab (i))
14695 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14696 else
14697 locsymcount = symtab_hdr->sh_info;
14698
14699 for (j = 0; j < locsymcount; ++j)
14700 {
14701 if (local_got[j] > 0)
14702 {
14703 local_got[j] = gotoff;
14704 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
14705 }
14706 else
14707 local_got[j] = (bfd_vma) -1;
14708 }
14709 }
14710
14711 /* Then the global .got entries. .plt refcounts are handled by
14712 adjust_dynamic_symbol */
14713 gofarg.gotoff = gotoff;
14714 gofarg.info = info;
14715 elf_link_hash_traverse (elf_hash_table (info),
14716 elf_gc_allocate_got_offsets,
14717 &gofarg);
14718 return true;
14719 }
14720
14721 /* Many folk need no more in the way of final link than this, once
14722 got entry reference counting is enabled. */
14723
14724 bool
bfd_elf_gc_common_final_link(bfd * abfd,struct bfd_link_info * info)14725 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14726 {
14727 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14728 return false;
14729
14730 /* Invoke the regular ELF backend linker to do all the work. */
14731 return bfd_elf_final_link (abfd, info);
14732 }
14733
14734 bool
bfd_elf_reloc_symbol_deleted_p(bfd_vma offset,void * cookie)14735 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14736 {
14737 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
14738
14739 if (rcookie->bad_symtab)
14740 rcookie->rel = rcookie->rels;
14741
14742 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14743 {
14744 unsigned long r_symndx;
14745
14746 if (! rcookie->bad_symtab)
14747 if (rcookie->rel->r_offset > offset)
14748 return false;
14749 if (rcookie->rel->r_offset != offset)
14750 continue;
14751
14752 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
14753 if (r_symndx == STN_UNDEF)
14754 return true;
14755
14756 if (r_symndx >= rcookie->locsymcount
14757 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14758 {
14759 struct elf_link_hash_entry *h;
14760
14761 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14762
14763 while (h->root.type == bfd_link_hash_indirect
14764 || h->root.type == bfd_link_hash_warning)
14765 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14766
14767 if ((h->root.type == bfd_link_hash_defined
14768 || h->root.type == bfd_link_hash_defweak)
14769 && (h->root.u.def.section->owner != rcookie->abfd
14770 || h->root.u.def.section->kept_section != NULL
14771 || discarded_section (h->root.u.def.section)))
14772 return true;
14773 }
14774 else
14775 {
14776 /* It's not a relocation against a global symbol,
14777 but it could be a relocation against a local
14778 symbol for a discarded section. */
14779 asection *isec;
14780 Elf_Internal_Sym *isym;
14781
14782 /* Need to: get the symbol; get the section. */
14783 isym = &rcookie->locsyms[r_symndx];
14784 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
14785 if (isec != NULL
14786 && (isec->kept_section != NULL
14787 || discarded_section (isec)))
14788 return true;
14789 }
14790 return false;
14791 }
14792 return false;
14793 }
14794
14795 /* Discard unneeded references to discarded sections.
14796 Returns -1 on error, 1 if any section's size was changed, 0 if
14797 nothing changed. This function assumes that the relocations are in
14798 sorted order, which is true for all known assemblers. */
14799
14800 int
bfd_elf_discard_info(bfd * output_bfd,struct bfd_link_info * info)14801 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14802 {
14803 struct elf_reloc_cookie cookie;
14804 asection *o;
14805 bfd *abfd;
14806 int changed = 0;
14807
14808 if (info->traditional_format
14809 || !is_elf_hash_table (info->hash))
14810 return 0;
14811
14812 o = bfd_get_section_by_name (output_bfd, ".stab");
14813 if (o != NULL)
14814 {
14815 asection *i;
14816
14817 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14818 {
14819 if (i->size == 0
14820 || i->reloc_count == 0
14821 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14822 continue;
14823
14824 abfd = i->owner;
14825 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14826 continue;
14827
14828 if (!init_reloc_cookie_for_section (&cookie, info, i))
14829 return -1;
14830
14831 if (_bfd_discard_section_stabs (abfd, i,
14832 elf_section_data (i)->sec_info,
14833 bfd_elf_reloc_symbol_deleted_p,
14834 &cookie))
14835 changed = 1;
14836
14837 fini_reloc_cookie_for_section (&cookie, i);
14838 }
14839 }
14840
14841 o = NULL;
14842 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14843 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
14844 if (o != NULL)
14845 {
14846 asection *i;
14847 int eh_changed = 0;
14848 unsigned int eh_alignment; /* Octets. */
14849
14850 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14851 {
14852 if (i->size == 0)
14853 continue;
14854
14855 abfd = i->owner;
14856 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14857 continue;
14858
14859 if (!init_reloc_cookie_for_section (&cookie, info, i))
14860 return -1;
14861
14862 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14863 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
14864 bfd_elf_reloc_symbol_deleted_p,
14865 &cookie))
14866 {
14867 eh_changed = 1;
14868 if (i->size != i->rawsize)
14869 changed = 1;
14870 }
14871
14872 fini_reloc_cookie_for_section (&cookie, i);
14873 }
14874
14875 eh_alignment = ((1 << o->alignment_power)
14876 * bfd_octets_per_byte (output_bfd, o));
14877 /* Skip over zero terminator, and prevent empty sections from
14878 adding alignment padding at the end. */
14879 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14880 if (i->size == 0)
14881 i->flags |= SEC_EXCLUDE;
14882 else if (i->size > 4)
14883 break;
14884 /* The last non-empty eh_frame section doesn't need padding. */
14885 if (i != NULL)
14886 i = i->map_tail.s;
14887 /* Any prior sections must pad the last FDE out to the output
14888 section alignment. Otherwise we might have zero padding
14889 between sections, which would be seen as a terminator. */
14890 for (; i != NULL; i = i->map_tail.s)
14891 if (i->size == 4)
14892 /* All but the last zero terminator should have been removed. */
14893 BFD_FAIL ();
14894 else
14895 {
14896 bfd_size_type size
14897 = (i->size + eh_alignment - 1) & -eh_alignment;
14898 if (i->size != size)
14899 {
14900 i->size = size;
14901 changed = 1;
14902 eh_changed = 1;
14903 }
14904 }
14905 if (eh_changed)
14906 elf_link_hash_traverse (elf_hash_table (info),
14907 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
14908 }
14909
14910 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14911 {
14912 const struct elf_backend_data *bed;
14913 asection *s;
14914
14915 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14916 continue;
14917 s = abfd->sections;
14918 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14919 continue;
14920
14921 bed = get_elf_backend_data (abfd);
14922
14923 if (bed->elf_backend_discard_info != NULL)
14924 {
14925 if (!init_reloc_cookie (&cookie, info, abfd))
14926 return -1;
14927
14928 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
14929 changed = 1;
14930
14931 fini_reloc_cookie (&cookie, abfd);
14932 }
14933 }
14934
14935 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14936 _bfd_elf_end_eh_frame_parsing (info);
14937
14938 if (info->eh_frame_hdr_type
14939 && !bfd_link_relocatable (info)
14940 && _bfd_elf_discard_section_eh_frame_hdr (info))
14941 changed = 1;
14942
14943 return changed;
14944 }
14945
14946 bool
_bfd_elf_section_already_linked(bfd * abfd,asection * sec,struct bfd_link_info * info)14947 _bfd_elf_section_already_linked (bfd *abfd,
14948 asection *sec,
14949 struct bfd_link_info *info)
14950 {
14951 flagword flags;
14952 const char *name, *key;
14953 struct bfd_section_already_linked *l;
14954 struct bfd_section_already_linked_hash_entry *already_linked_list;
14955
14956 if (sec->output_section == bfd_abs_section_ptr)
14957 return false;
14958
14959 flags = sec->flags;
14960
14961 /* Return if it isn't a linkonce section. A comdat group section
14962 also has SEC_LINK_ONCE set. */
14963 if ((flags & SEC_LINK_ONCE) == 0)
14964 return false;
14965
14966 /* Don't put group member sections on our list of already linked
14967 sections. They are handled as a group via their group section. */
14968 if (elf_sec_group (sec) != NULL)
14969 return false;
14970
14971 /* For a SHT_GROUP section, use the group signature as the key. */
14972 name = sec->name;
14973 if ((flags & SEC_GROUP) != 0
14974 && elf_next_in_group (sec) != NULL
14975 && elf_group_name (elf_next_in_group (sec)) != NULL)
14976 key = elf_group_name (elf_next_in_group (sec));
14977 else
14978 {
14979 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14980 if (startswith (name, ".gnu.linkonce.")
14981 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14982 key++;
14983 else
14984 /* Must be a user linkonce section that doesn't follow gcc's
14985 naming convention. In this case we won't be matching
14986 single member groups. */
14987 key = name;
14988 }
14989
14990 already_linked_list = bfd_section_already_linked_table_lookup (key);
14991
14992 for (l = already_linked_list->entry; l != NULL; l = l->next)
14993 {
14994 /* We may have 2 different types of sections on the list: group
14995 sections with a signature of <key> (<key> is some string),
14996 and linkonce sections named .gnu.linkonce.<type>.<key>.
14997 Match like sections. LTO plugin sections are an exception.
14998 They are always named .gnu.linkonce.t.<key> and match either
14999 type of section. */
15000 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
15001 && ((flags & SEC_GROUP) != 0
15002 || strcmp (name, l->sec->name) == 0))
15003 || (l->sec->owner->flags & BFD_PLUGIN) != 0
15004 || (sec->owner->flags & BFD_PLUGIN) != 0)
15005 {
15006 /* The section has already been linked. See if we should
15007 issue a warning. */
15008 if (!_bfd_handle_already_linked (sec, l, info))
15009 return false;
15010
15011 if (flags & SEC_GROUP)
15012 {
15013 asection *first = elf_next_in_group (sec);
15014 asection *s = first;
15015
15016 while (s != NULL)
15017 {
15018 s->output_section = bfd_abs_section_ptr;
15019 /* Record which group discards it. */
15020 s->kept_section = l->sec;
15021 s = elf_next_in_group (s);
15022 /* These lists are circular. */
15023 if (s == first)
15024 break;
15025 }
15026 }
15027
15028 return true;
15029 }
15030 }
15031
15032 /* A single member comdat group section may be discarded by a
15033 linkonce section and vice versa. */
15034 if ((flags & SEC_GROUP) != 0)
15035 {
15036 asection *first = elf_next_in_group (sec);
15037
15038 if (first != NULL && elf_next_in_group (first) == first)
15039 /* Check this single member group against linkonce sections. */
15040 for (l = already_linked_list->entry; l != NULL; l = l->next)
15041 if ((l->sec->flags & SEC_GROUP) == 0
15042 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
15043 {
15044 first->output_section = bfd_abs_section_ptr;
15045 first->kept_section = l->sec;
15046 sec->output_section = bfd_abs_section_ptr;
15047 break;
15048 }
15049 }
15050 else
15051 /* Check this linkonce section against single member groups. */
15052 for (l = already_linked_list->entry; l != NULL; l = l->next)
15053 if (l->sec->flags & SEC_GROUP)
15054 {
15055 asection *first = elf_next_in_group (l->sec);
15056
15057 if (first != NULL
15058 && elf_next_in_group (first) == first
15059 && bfd_elf_match_symbols_in_sections (first, sec, info))
15060 {
15061 sec->output_section = bfd_abs_section_ptr;
15062 sec->kept_section = first;
15063 break;
15064 }
15065 }
15066
15067 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
15068 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
15069 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
15070 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
15071 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
15072 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
15073 `.gnu.linkonce.t.F' section from a different bfd not requiring any
15074 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
15075 The reverse order cannot happen as there is never a bfd with only the
15076 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
15077 matter as here were are looking only for cross-bfd sections. */
15078
15079 if ((flags & SEC_GROUP) == 0 && startswith (name, ".gnu.linkonce.r."))
15080 for (l = already_linked_list->entry; l != NULL; l = l->next)
15081 if ((l->sec->flags & SEC_GROUP) == 0
15082 && startswith (l->sec->name, ".gnu.linkonce.t."))
15083 {
15084 if (abfd != l->sec->owner)
15085 sec->output_section = bfd_abs_section_ptr;
15086 break;
15087 }
15088
15089 /* This is the first section with this name. Record it. */
15090 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
15091 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
15092 return sec->output_section == bfd_abs_section_ptr;
15093 }
15094
15095 bool
_bfd_elf_common_definition(Elf_Internal_Sym * sym)15096 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
15097 {
15098 return sym->st_shndx == SHN_COMMON;
15099 }
15100
15101 unsigned int
_bfd_elf_common_section_index(asection * sec ATTRIBUTE_UNUSED)15102 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
15103 {
15104 return SHN_COMMON;
15105 }
15106
15107 asection *
_bfd_elf_common_section(asection * sec ATTRIBUTE_UNUSED)15108 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
15109 {
15110 return bfd_com_section_ptr;
15111 }
15112
15113 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)15114 _bfd_elf_default_got_elt_size (bfd *abfd,
15115 struct bfd_link_info *info ATTRIBUTE_UNUSED,
15116 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
15117 bfd *ibfd ATTRIBUTE_UNUSED,
15118 unsigned long symndx ATTRIBUTE_UNUSED)
15119 {
15120 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15121 return bed->s->arch_size / 8;
15122 }
15123
15124 /* Routines to support the creation of dynamic relocs. */
15125
15126 /* Returns the name of the dynamic reloc section associated with SEC. */
15127
15128 static const char *
get_dynamic_reloc_section_name(bfd * abfd,asection * sec,bool is_rela)15129 get_dynamic_reloc_section_name (bfd * abfd,
15130 asection * sec,
15131 bool is_rela)
15132 {
15133 char *name;
15134 const char *old_name = bfd_section_name (sec);
15135 const char *prefix = is_rela ? ".rela" : ".rel";
15136
15137 if (old_name == NULL)
15138 return NULL;
15139
15140 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
15141 sprintf (name, "%s%s", prefix, old_name);
15142
15143 return name;
15144 }
15145
15146 /* Returns the dynamic reloc section associated with SEC.
15147 If necessary compute the name of the dynamic reloc section based
15148 on SEC's name (looked up in ABFD's string table) and the setting
15149 of IS_RELA. */
15150
15151 asection *
_bfd_elf_get_dynamic_reloc_section(bfd * abfd,asection * sec,bool is_rela)15152 _bfd_elf_get_dynamic_reloc_section (bfd *abfd,
15153 asection *sec,
15154 bool is_rela)
15155 {
15156 asection *reloc_sec = elf_section_data (sec)->sreloc;
15157
15158 if (reloc_sec == NULL)
15159 {
15160 const char *name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15161
15162 if (name != NULL)
15163 {
15164 reloc_sec = bfd_get_linker_section (abfd, name);
15165
15166 if (reloc_sec != NULL)
15167 elf_section_data (sec)->sreloc = reloc_sec;
15168 }
15169 }
15170
15171 return reloc_sec;
15172 }
15173
15174 /* Returns the dynamic reloc section associated with SEC. If the
15175 section does not exist it is created and attached to the DYNOBJ
15176 bfd and stored in the SRELOC field of SEC's elf_section_data
15177 structure.
15178
15179 ALIGNMENT is the alignment for the newly created section and
15180 IS_RELA defines whether the name should be .rela.<SEC's name>
15181 or .rel.<SEC's name>. The section name is looked up in the
15182 string table associated with ABFD. */
15183
15184 asection *
_bfd_elf_make_dynamic_reloc_section(asection * sec,bfd * dynobj,unsigned int alignment,bfd * abfd,bool is_rela)15185 _bfd_elf_make_dynamic_reloc_section (asection *sec,
15186 bfd *dynobj,
15187 unsigned int alignment,
15188 bfd *abfd,
15189 bool is_rela)
15190 {
15191 asection * reloc_sec = elf_section_data (sec)->sreloc;
15192
15193 if (reloc_sec == NULL)
15194 {
15195 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15196
15197 if (name == NULL)
15198 return NULL;
15199
15200 reloc_sec = bfd_get_linker_section (dynobj, name);
15201
15202 if (reloc_sec == NULL)
15203 {
15204 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
15205 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
15206 if ((sec->flags & SEC_ALLOC) != 0)
15207 flags |= SEC_ALLOC | SEC_LOAD;
15208
15209 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
15210 if (reloc_sec != NULL)
15211 {
15212 /* _bfd_elf_get_sec_type_attr chooses a section type by
15213 name. Override as it may be wrong, eg. for a user
15214 section named "auto" we'll get ".relauto" which is
15215 seen to be a .rela section. */
15216 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
15217 if (!bfd_set_section_alignment (reloc_sec, alignment))
15218 reloc_sec = NULL;
15219 }
15220 }
15221
15222 elf_section_data (sec)->sreloc = reloc_sec;
15223 }
15224
15225 return reloc_sec;
15226 }
15227
15228 /* Copy the ELF symbol type and other attributes for a linker script
15229 assignment from HSRC to HDEST. Generally this should be treated as
15230 if we found a strong non-dynamic definition for HDEST (except that
15231 ld ignores multiple definition errors). */
15232 void
_bfd_elf_copy_link_hash_symbol_type(bfd * abfd,struct bfd_link_hash_entry * hdest,struct bfd_link_hash_entry * hsrc)15233 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
15234 struct bfd_link_hash_entry *hdest,
15235 struct bfd_link_hash_entry *hsrc)
15236 {
15237 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
15238 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
15239 Elf_Internal_Sym isym;
15240
15241 ehdest->type = ehsrc->type;
15242 ehdest->target_internal = ehsrc->target_internal;
15243
15244 isym.st_other = ehsrc->other;
15245 elf_merge_st_other (abfd, ehdest, isym.st_other, NULL, true, false);
15246 }
15247
15248 /* Append a RELA relocation REL to section S in BFD. */
15249
15250 void
elf_append_rela(bfd * abfd,asection * s,Elf_Internal_Rela * rel)15251 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15252 {
15253 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15254 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
15255 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
15256 bed->s->swap_reloca_out (abfd, rel, loc);
15257 }
15258
15259 /* Append a REL relocation REL to section S in BFD. */
15260
15261 void
elf_append_rel(bfd * abfd,asection * s,Elf_Internal_Rela * rel)15262 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15263 {
15264 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15265 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
15266 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
15267 bed->s->swap_reloc_out (abfd, rel, loc);
15268 }
15269
15270 /* Define __start, __stop, .startof. or .sizeof. symbol. */
15271
15272 struct bfd_link_hash_entry *
bfd_elf_define_start_stop(struct bfd_link_info * info,const char * symbol,asection * sec)15273 bfd_elf_define_start_stop (struct bfd_link_info *info,
15274 const char *symbol, asection *sec)
15275 {
15276 struct elf_link_hash_entry *h;
15277
15278 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
15279 false, false, true);
15280 /* NB: Common symbols will be turned into definition later. */
15281 if (h != NULL
15282 && !h->root.ldscript_def
15283 && (h->root.type == bfd_link_hash_undefined
15284 || h->root.type == bfd_link_hash_undefweak
15285 || ((h->ref_regular || h->def_dynamic)
15286 && !h->def_regular
15287 && h->root.type != bfd_link_hash_common)))
15288 {
15289 bool was_dynamic = h->ref_dynamic || h->def_dynamic;
15290 h->verinfo.verdef = NULL;
15291 h->root.type = bfd_link_hash_defined;
15292 h->root.u.def.section = sec;
15293 h->root.u.def.value = 0;
15294 h->def_regular = 1;
15295 h->def_dynamic = 0;
15296 h->start_stop = 1;
15297 h->u2.start_stop_section = sec;
15298 if (symbol[0] == '.')
15299 {
15300 /* .startof. and .sizeof. symbols are local. */
15301 const struct elf_backend_data *bed;
15302 bed = get_elf_backend_data (info->output_bfd);
15303 (*bed->elf_backend_hide_symbol) (info, h, true);
15304 }
15305 else
15306 {
15307 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
15308 h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
15309 | info->start_stop_visibility);
15310 if (was_dynamic)
15311 bfd_elf_link_record_dynamic_symbol (info, h);
15312 }
15313 return &h->root;
15314 }
15315 return NULL;
15316 }
15317
15318 /* Find dynamic relocs for H that apply to read-only sections. */
15319
15320 asection *
_bfd_elf_readonly_dynrelocs(struct elf_link_hash_entry * h)15321 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
15322 {
15323 struct elf_dyn_relocs *p;
15324
15325 for (p = h->dyn_relocs; p != NULL; p = p->next)
15326 {
15327 asection *s = p->sec->output_section;
15328
15329 if (s != NULL && (s->flags & SEC_READONLY) != 0)
15330 return p->sec;
15331 }
15332 return NULL;
15333 }
15334
15335 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
15336 read-only sections. */
15337
15338 bool
_bfd_elf_maybe_set_textrel(struct elf_link_hash_entry * h,void * inf)15339 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
15340 {
15341 asection *sec;
15342
15343 if (h->root.type == bfd_link_hash_indirect)
15344 return true;
15345
15346 sec = _bfd_elf_readonly_dynrelocs (h);
15347 if (sec != NULL)
15348 {
15349 struct bfd_link_info *info = (struct bfd_link_info *) inf;
15350
15351 info->flags |= DF_TEXTREL;
15352 /* xgettext:c-format */
15353 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
15354 "in read-only section `%pA'\n"),
15355 sec->owner, h->root.root.string, sec);
15356
15357 if (bfd_link_textrel_check (info))
15358 /* xgettext:c-format */
15359 info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
15360 "in read-only section `%pA'\n"),
15361 sec->owner, h->root.root.string, sec);
15362
15363 /* Not an error, just cut short the traversal. */
15364 return false;
15365 }
15366 return true;
15367 }
15368
15369 /* Add dynamic tags. */
15370
15371 bool
_bfd_elf_add_dynamic_tags(bfd * output_bfd,struct bfd_link_info * info,bool need_dynamic_reloc)15372 _bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
15373 bool need_dynamic_reloc)
15374 {
15375 struct elf_link_hash_table *htab = elf_hash_table (info);
15376
15377 if (htab->dynamic_sections_created)
15378 {
15379 /* Add some entries to the .dynamic section. We fill in the
15380 values later, in finish_dynamic_sections, but we must add
15381 the entries now so that we get the correct size for the
15382 .dynamic section. The DT_DEBUG entry is filled in by the
15383 dynamic linker and used by the debugger. */
15384 #define add_dynamic_entry(TAG, VAL) \
15385 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15386
15387 const struct elf_backend_data *bed
15388 = get_elf_backend_data (output_bfd);
15389
15390 if (bfd_link_executable (info))
15391 {
15392 if (!add_dynamic_entry (DT_DEBUG, 0))
15393 return false;
15394 }
15395
15396 if (htab->dt_pltgot_required || htab->splt->size != 0)
15397 {
15398 /* DT_PLTGOT is used by prelink even if there is no PLT
15399 relocation. */
15400 if (!add_dynamic_entry (DT_PLTGOT, 0))
15401 return false;
15402 }
15403
15404 if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15405 {
15406 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15407 || !add_dynamic_entry (DT_PLTREL,
15408 (bed->rela_plts_and_copies_p
15409 ? DT_RELA : DT_REL))
15410 || !add_dynamic_entry (DT_JMPREL, 0))
15411 return false;
15412 }
15413
15414 if (htab->tlsdesc_plt
15415 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15416 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
15417 return false;
15418
15419 if (need_dynamic_reloc)
15420 {
15421 if (bed->rela_plts_and_copies_p)
15422 {
15423 if (!add_dynamic_entry (DT_RELA, 0)
15424 || !add_dynamic_entry (DT_RELASZ, 0)
15425 || !add_dynamic_entry (DT_RELAENT,
15426 bed->s->sizeof_rela))
15427 return false;
15428 }
15429 else
15430 {
15431 if (!add_dynamic_entry (DT_REL, 0)
15432 || !add_dynamic_entry (DT_RELSZ, 0)
15433 || !add_dynamic_entry (DT_RELENT,
15434 bed->s->sizeof_rel))
15435 return false;
15436 }
15437
15438 /* If any dynamic relocs apply to a read-only section,
15439 then we need a DT_TEXTREL entry. */
15440 if ((info->flags & DF_TEXTREL) == 0)
15441 elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15442 info);
15443
15444 if ((info->flags & DF_TEXTREL) != 0)
15445 {
15446 if (htab->ifunc_resolvers)
15447 info->callbacks->einfo
15448 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15449 "may result in a segfault at runtime; recompile with %s\n"),
15450 bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15451
15452 if (!add_dynamic_entry (DT_TEXTREL, 0))
15453 return false;
15454 }
15455 }
15456 }
15457 #undef add_dynamic_entry
15458
15459 return true;
15460 }
15461