Set dynamic tag VMA and size from dynamic section when possible
[deliverable/binutils-gdb.git] / bfd / elflink.c
1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2016 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
20
21 #include "sysdep.h"
22 #include "bfd.h"
23 #include "bfd_stdint.h"
24 #include "bfdlink.h"
25 #include "libbfd.h"
26 #define ARCH_SIZE 0
27 #include "elf-bfd.h"
28 #include "safe-ctype.h"
29 #include "libiberty.h"
30 #include "objalloc.h"
31
32 /* This struct is used to pass information to routines called via
33 elf_link_hash_traverse which must return failure. */
34
35 struct elf_info_failed
36 {
37 struct bfd_link_info *info;
38 bfd_boolean failed;
39 };
40
41 /* This structure is used to pass information to
42 _bfd_elf_link_find_version_dependencies. */
43
44 struct elf_find_verdep_info
45 {
46 /* General link information. */
47 struct bfd_link_info *info;
48 /* The number of dependencies. */
49 unsigned int vers;
50 /* Whether we had a failure. */
51 bfd_boolean failed;
52 };
53
54 static bfd_boolean _bfd_elf_fix_symbol_flags
55 (struct elf_link_hash_entry *, struct elf_info_failed *);
56
57 asection *
58 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
59 unsigned long r_symndx,
60 bfd_boolean discard)
61 {
62 if (r_symndx >= cookie->locsymcount
63 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
64 {
65 struct elf_link_hash_entry *h;
66
67 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
68
69 while (h->root.type == bfd_link_hash_indirect
70 || h->root.type == bfd_link_hash_warning)
71 h = (struct elf_link_hash_entry *) h->root.u.i.link;
72
73 if ((h->root.type == bfd_link_hash_defined
74 || h->root.type == bfd_link_hash_defweak)
75 && discarded_section (h->root.u.def.section))
76 return h->root.u.def.section;
77 else
78 return NULL;
79 }
80 else
81 {
82 /* It's not a relocation against a global symbol,
83 but it could be a relocation against a local
84 symbol for a discarded section. */
85 asection *isec;
86 Elf_Internal_Sym *isym;
87
88 /* Need to: get the symbol; get the section. */
89 isym = &cookie->locsyms[r_symndx];
90 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
91 if (isec != NULL
92 && discard ? discarded_section (isec) : 1)
93 return isec;
94 }
95 return NULL;
96 }
97
98 /* Define a symbol in a dynamic linkage section. */
99
100 struct elf_link_hash_entry *
101 _bfd_elf_define_linkage_sym (bfd *abfd,
102 struct bfd_link_info *info,
103 asection *sec,
104 const char *name)
105 {
106 struct elf_link_hash_entry *h;
107 struct bfd_link_hash_entry *bh;
108 const struct elf_backend_data *bed;
109
110 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
111 if (h != NULL)
112 {
113 /* Zap symbol defined in an as-needed lib that wasn't linked.
114 This is a symptom of a larger problem: Absolute symbols
115 defined in shared libraries can't be overridden, because we
116 lose the link to the bfd which is via the symbol section. */
117 h->root.type = bfd_link_hash_new;
118 }
119
120 bh = &h->root;
121 bed = get_elf_backend_data (abfd);
122 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
123 sec, 0, NULL, FALSE, bed->collect,
124 &bh))
125 return NULL;
126 h = (struct elf_link_hash_entry *) bh;
127 h->def_regular = 1;
128 h->non_elf = 0;
129 h->root.linker_def = 1;
130 h->type = STT_OBJECT;
131 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
132 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
133
134 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
135 return h;
136 }
137
138 bfd_boolean
139 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
140 {
141 flagword flags;
142 asection *s;
143 struct elf_link_hash_entry *h;
144 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
145 struct elf_link_hash_table *htab = elf_hash_table (info);
146
147 /* This function may be called more than once. */
148 s = bfd_get_linker_section (abfd, ".got");
149 if (s != NULL)
150 return TRUE;
151
152 flags = bed->dynamic_sec_flags;
153
154 s = bfd_make_section_anyway_with_flags (abfd,
155 (bed->rela_plts_and_copies_p
156 ? ".rela.got" : ".rel.got"),
157 (bed->dynamic_sec_flags
158 | SEC_READONLY));
159 if (s == NULL
160 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
161 return FALSE;
162 htab->srelgot = s;
163
164 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
165 if (s == NULL
166 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
167 return FALSE;
168 htab->sgot = s;
169
170 if (bed->want_got_plt)
171 {
172 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
173 if (s == NULL
174 || !bfd_set_section_alignment (abfd, s,
175 bed->s->log_file_align))
176 return FALSE;
177 htab->sgotplt = s;
178 }
179
180 /* The first bit of the global offset table is the header. */
181 s->size += bed->got_header_size;
182
183 if (bed->want_got_sym)
184 {
185 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
186 (or .got.plt) section. We don't do this in the linker script
187 because we don't want to define the symbol if we are not creating
188 a global offset table. */
189 h = _bfd_elf_define_linkage_sym (abfd, info, s,
190 "_GLOBAL_OFFSET_TABLE_");
191 elf_hash_table (info)->hgot = h;
192 if (h == NULL)
193 return FALSE;
194 }
195
196 return TRUE;
197 }
198 \f
199 /* Create a strtab to hold the dynamic symbol names. */
200 static bfd_boolean
201 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
202 {
203 struct elf_link_hash_table *hash_table;
204
205 hash_table = elf_hash_table (info);
206 if (hash_table->dynobj == NULL)
207 {
208 /* We may not set dynobj, an input file holding linker created
209 dynamic sections to abfd, which may be a dynamic object with
210 its own dynamic sections. We need to find a normal input file
211 to hold linker created sections if possible. */
212 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
213 {
214 bfd *ibfd;
215 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
216 if ((ibfd->flags
217 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0)
218 {
219 abfd = ibfd;
220 break;
221 }
222 }
223 hash_table->dynobj = abfd;
224 }
225
226 if (hash_table->dynstr == NULL)
227 {
228 hash_table->dynstr = _bfd_elf_strtab_init ();
229 if (hash_table->dynstr == NULL)
230 return FALSE;
231 }
232 return TRUE;
233 }
234
235 /* Create some sections which will be filled in with dynamic linking
236 information. ABFD is an input file which requires dynamic sections
237 to be created. The dynamic sections take up virtual memory space
238 when the final executable is run, so we need to create them before
239 addresses are assigned to the output sections. We work out the
240 actual contents and size of these sections later. */
241
242 bfd_boolean
243 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
244 {
245 flagword flags;
246 asection *s;
247 const struct elf_backend_data *bed;
248 struct elf_link_hash_entry *h;
249
250 if (! is_elf_hash_table (info->hash))
251 return FALSE;
252
253 if (elf_hash_table (info)->dynamic_sections_created)
254 return TRUE;
255
256 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
257 return FALSE;
258
259 abfd = elf_hash_table (info)->dynobj;
260 bed = get_elf_backend_data (abfd);
261
262 flags = bed->dynamic_sec_flags;
263
264 /* A dynamically linked executable has a .interp section, but a
265 shared library does not. */
266 if (bfd_link_executable (info) && !info->nointerp)
267 {
268 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
269 flags | SEC_READONLY);
270 if (s == NULL)
271 return FALSE;
272 }
273
274 /* Create sections to hold version informations. These are removed
275 if they are not needed. */
276 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
277 flags | SEC_READONLY);
278 if (s == NULL
279 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
280 return FALSE;
281
282 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
283 flags | SEC_READONLY);
284 if (s == NULL
285 || ! bfd_set_section_alignment (abfd, s, 1))
286 return FALSE;
287
288 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
289 flags | SEC_READONLY);
290 if (s == NULL
291 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
292 return FALSE;
293
294 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
295 flags | SEC_READONLY);
296 if (s == NULL
297 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
298 return FALSE;
299 elf_hash_table (info)->dynsym = s;
300
301 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
302 flags | SEC_READONLY);
303 if (s == NULL)
304 return FALSE;
305
306 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
307 if (s == NULL
308 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
309 return FALSE;
310
311 /* The special symbol _DYNAMIC is always set to the start of the
312 .dynamic section. We could set _DYNAMIC in a linker script, but we
313 only want to define it if we are, in fact, creating a .dynamic
314 section. We don't want to define it if there is no .dynamic
315 section, since on some ELF platforms the start up code examines it
316 to decide how to initialize the process. */
317 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
318 elf_hash_table (info)->hdynamic = h;
319 if (h == NULL)
320 return FALSE;
321
322 if (info->emit_hash)
323 {
324 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
325 flags | SEC_READONLY);
326 if (s == NULL
327 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
328 return FALSE;
329 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
330 }
331
332 if (info->emit_gnu_hash)
333 {
334 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
335 flags | SEC_READONLY);
336 if (s == NULL
337 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
338 return FALSE;
339 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
340 4 32-bit words followed by variable count of 64-bit words, then
341 variable count of 32-bit words. */
342 if (bed->s->arch_size == 64)
343 elf_section_data (s)->this_hdr.sh_entsize = 0;
344 else
345 elf_section_data (s)->this_hdr.sh_entsize = 4;
346 }
347
348 /* Let the backend create the rest of the sections. This lets the
349 backend set the right flags. The backend will normally create
350 the .got and .plt sections. */
351 if (bed->elf_backend_create_dynamic_sections == NULL
352 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
353 return FALSE;
354
355 elf_hash_table (info)->dynamic_sections_created = TRUE;
356
357 return TRUE;
358 }
359
360 /* Create dynamic sections when linking against a dynamic object. */
361
362 bfd_boolean
363 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
364 {
365 flagword flags, pltflags;
366 struct elf_link_hash_entry *h;
367 asection *s;
368 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
369 struct elf_link_hash_table *htab = elf_hash_table (info);
370
371 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
372 .rel[a].bss sections. */
373 flags = bed->dynamic_sec_flags;
374
375 pltflags = flags;
376 if (bed->plt_not_loaded)
377 /* We do not clear SEC_ALLOC here because we still want the OS to
378 allocate space for the section; it's just that there's nothing
379 to read in from the object file. */
380 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
381 else
382 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
383 if (bed->plt_readonly)
384 pltflags |= SEC_READONLY;
385
386 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
387 if (s == NULL
388 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
389 return FALSE;
390 htab->splt = s;
391
392 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
393 .plt section. */
394 if (bed->want_plt_sym)
395 {
396 h = _bfd_elf_define_linkage_sym (abfd, info, s,
397 "_PROCEDURE_LINKAGE_TABLE_");
398 elf_hash_table (info)->hplt = h;
399 if (h == NULL)
400 return FALSE;
401 }
402
403 s = bfd_make_section_anyway_with_flags (abfd,
404 (bed->rela_plts_and_copies_p
405 ? ".rela.plt" : ".rel.plt"),
406 flags | SEC_READONLY);
407 if (s == NULL
408 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
409 return FALSE;
410 htab->srelplt = s;
411
412 if (! _bfd_elf_create_got_section (abfd, info))
413 return FALSE;
414
415 if (bed->want_dynbss)
416 {
417 /* The .dynbss section is a place to put symbols which are defined
418 by dynamic objects, are referenced by regular objects, and are
419 not functions. We must allocate space for them in the process
420 image and use a R_*_COPY reloc to tell the dynamic linker to
421 initialize them at run time. The linker script puts the .dynbss
422 section into the .bss section of the final image. */
423 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
424 (SEC_ALLOC | SEC_LINKER_CREATED));
425 if (s == NULL)
426 return FALSE;
427
428 /* The .rel[a].bss section holds copy relocs. This section is not
429 normally needed. We need to create it here, though, so that the
430 linker will map it to an output section. We can't just create it
431 only if we need it, because we will not know whether we need it
432 until we have seen all the input files, and the first time the
433 main linker code calls BFD after examining all the input files
434 (size_dynamic_sections) the input sections have already been
435 mapped to the output sections. If the section turns out not to
436 be needed, we can discard it later. We will never need this
437 section when generating a shared object, since they do not use
438 copy relocs. */
439 if (! bfd_link_pic (info))
440 {
441 s = bfd_make_section_anyway_with_flags (abfd,
442 (bed->rela_plts_and_copies_p
443 ? ".rela.bss" : ".rel.bss"),
444 flags | SEC_READONLY);
445 if (s == NULL
446 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
447 return FALSE;
448 }
449 }
450
451 return TRUE;
452 }
453 \f
454 /* Record a new dynamic symbol. We record the dynamic symbols as we
455 read the input files, since we need to have a list of all of them
456 before we can determine the final sizes of the output sections.
457 Note that we may actually call this function even though we are not
458 going to output any dynamic symbols; in some cases we know that a
459 symbol should be in the dynamic symbol table, but only if there is
460 one. */
461
462 bfd_boolean
463 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
464 struct elf_link_hash_entry *h)
465 {
466 if (h->dynindx == -1)
467 {
468 struct elf_strtab_hash *dynstr;
469 char *p;
470 const char *name;
471 bfd_size_type indx;
472
473 /* XXX: The ABI draft says the linker must turn hidden and
474 internal symbols into STB_LOCAL symbols when producing the
475 DSO. However, if ld.so honors st_other in the dynamic table,
476 this would not be necessary. */
477 switch (ELF_ST_VISIBILITY (h->other))
478 {
479 case STV_INTERNAL:
480 case STV_HIDDEN:
481 if (h->root.type != bfd_link_hash_undefined
482 && h->root.type != bfd_link_hash_undefweak)
483 {
484 h->forced_local = 1;
485 if (!elf_hash_table (info)->is_relocatable_executable)
486 return TRUE;
487 }
488
489 default:
490 break;
491 }
492
493 h->dynindx = elf_hash_table (info)->dynsymcount;
494 ++elf_hash_table (info)->dynsymcount;
495
496 dynstr = elf_hash_table (info)->dynstr;
497 if (dynstr == NULL)
498 {
499 /* Create a strtab to hold the dynamic symbol names. */
500 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
501 if (dynstr == NULL)
502 return FALSE;
503 }
504
505 /* We don't put any version information in the dynamic string
506 table. */
507 name = h->root.root.string;
508 p = strchr (name, ELF_VER_CHR);
509 if (p != NULL)
510 /* We know that the p points into writable memory. In fact,
511 there are only a few symbols that have read-only names, being
512 those like _GLOBAL_OFFSET_TABLE_ that are created specially
513 by the backends. Most symbols will have names pointing into
514 an ELF string table read from a file, or to objalloc memory. */
515 *p = 0;
516
517 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
518
519 if (p != NULL)
520 *p = ELF_VER_CHR;
521
522 if (indx == (bfd_size_type) -1)
523 return FALSE;
524 h->dynstr_index = indx;
525 }
526
527 return TRUE;
528 }
529 \f
530 /* Mark a symbol dynamic. */
531
532 static void
533 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
534 struct elf_link_hash_entry *h,
535 Elf_Internal_Sym *sym)
536 {
537 struct bfd_elf_dynamic_list *d = info->dynamic_list;
538
539 /* It may be called more than once on the same H. */
540 if(h->dynamic || bfd_link_relocatable (info))
541 return;
542
543 if ((info->dynamic_data
544 && (h->type == STT_OBJECT
545 || h->type == STT_COMMON
546 || (sym != NULL
547 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
548 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
549 || (d != NULL
550 && h->root.type == bfd_link_hash_new
551 && (*d->match) (&d->head, NULL, h->root.root.string)))
552 h->dynamic = 1;
553 }
554
555 /* Record an assignment to a symbol made by a linker script. We need
556 this in case some dynamic object refers to this symbol. */
557
558 bfd_boolean
559 bfd_elf_record_link_assignment (bfd *output_bfd,
560 struct bfd_link_info *info,
561 const char *name,
562 bfd_boolean provide,
563 bfd_boolean hidden)
564 {
565 struct elf_link_hash_entry *h, *hv;
566 struct elf_link_hash_table *htab;
567 const struct elf_backend_data *bed;
568
569 if (!is_elf_hash_table (info->hash))
570 return TRUE;
571
572 htab = elf_hash_table (info);
573 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
574 if (h == NULL)
575 return provide;
576
577 if (h->versioned == unknown)
578 {
579 /* Set versioned if symbol version is unknown. */
580 char *version = strrchr (name, ELF_VER_CHR);
581 if (version)
582 {
583 if (version > name && version[-1] != ELF_VER_CHR)
584 h->versioned = versioned_hidden;
585 else
586 h->versioned = versioned;
587 }
588 }
589
590 switch (h->root.type)
591 {
592 case bfd_link_hash_defined:
593 case bfd_link_hash_defweak:
594 case bfd_link_hash_common:
595 break;
596 case bfd_link_hash_undefweak:
597 case bfd_link_hash_undefined:
598 /* Since we're defining the symbol, don't let it seem to have not
599 been defined. record_dynamic_symbol and size_dynamic_sections
600 may depend on this. */
601 h->root.type = bfd_link_hash_new;
602 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
603 bfd_link_repair_undef_list (&htab->root);
604 break;
605 case bfd_link_hash_new:
606 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
607 h->non_elf = 0;
608 break;
609 case bfd_link_hash_indirect:
610 /* We had a versioned symbol in a dynamic library. We make the
611 the versioned symbol point to this one. */
612 bed = get_elf_backend_data (output_bfd);
613 hv = h;
614 while (hv->root.type == bfd_link_hash_indirect
615 || hv->root.type == bfd_link_hash_warning)
616 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
617 /* We don't need to update h->root.u since linker will set them
618 later. */
619 h->root.type = bfd_link_hash_undefined;
620 hv->root.type = bfd_link_hash_indirect;
621 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
622 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
623 break;
624 case bfd_link_hash_warning:
625 abort ();
626 break;
627 }
628
629 /* If this symbol is being provided by the linker script, and it is
630 currently defined by a dynamic object, but not by a regular
631 object, then mark it as undefined so that the generic linker will
632 force the correct value. */
633 if (provide
634 && h->def_dynamic
635 && !h->def_regular)
636 h->root.type = bfd_link_hash_undefined;
637
638 /* If this symbol is not being provided by the linker script, and it is
639 currently defined by a dynamic object, but not by a regular object,
640 then clear out any version information because the symbol will not be
641 associated with the dynamic object any more. */
642 if (!provide
643 && h->def_dynamic
644 && !h->def_regular)
645 h->verinfo.verdef = NULL;
646
647 h->def_regular = 1;
648
649 if (hidden)
650 {
651 bed = get_elf_backend_data (output_bfd);
652 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
653 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
654 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
655 }
656
657 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
658 and executables. */
659 if (!bfd_link_relocatable (info)
660 && h->dynindx != -1
661 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
662 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
663 h->forced_local = 1;
664
665 if ((h->def_dynamic
666 || h->ref_dynamic
667 || bfd_link_dll (info)
668 || elf_hash_table (info)->is_relocatable_executable)
669 && h->dynindx == -1)
670 {
671 if (! bfd_elf_link_record_dynamic_symbol (info, h))
672 return FALSE;
673
674 /* If this is a weak defined symbol, and we know a corresponding
675 real symbol from the same dynamic object, make sure the real
676 symbol is also made into a dynamic symbol. */
677 if (h->u.weakdef != NULL
678 && h->u.weakdef->dynindx == -1)
679 {
680 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
681 return FALSE;
682 }
683 }
684
685 return TRUE;
686 }
687
688 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
689 success, and 2 on a failure caused by attempting to record a symbol
690 in a discarded section, eg. a discarded link-once section symbol. */
691
692 int
693 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
694 bfd *input_bfd,
695 long input_indx)
696 {
697 bfd_size_type amt;
698 struct elf_link_local_dynamic_entry *entry;
699 struct elf_link_hash_table *eht;
700 struct elf_strtab_hash *dynstr;
701 unsigned long dynstr_index;
702 char *name;
703 Elf_External_Sym_Shndx eshndx;
704 char esym[sizeof (Elf64_External_Sym)];
705
706 if (! is_elf_hash_table (info->hash))
707 return 0;
708
709 /* See if the entry exists already. */
710 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
711 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
712 return 1;
713
714 amt = sizeof (*entry);
715 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
716 if (entry == NULL)
717 return 0;
718
719 /* Go find the symbol, so that we can find it's name. */
720 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
721 1, input_indx, &entry->isym, esym, &eshndx))
722 {
723 bfd_release (input_bfd, entry);
724 return 0;
725 }
726
727 if (entry->isym.st_shndx != SHN_UNDEF
728 && entry->isym.st_shndx < SHN_LORESERVE)
729 {
730 asection *s;
731
732 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
733 if (s == NULL || bfd_is_abs_section (s->output_section))
734 {
735 /* We can still bfd_release here as nothing has done another
736 bfd_alloc. We can't do this later in this function. */
737 bfd_release (input_bfd, entry);
738 return 2;
739 }
740 }
741
742 name = (bfd_elf_string_from_elf_section
743 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
744 entry->isym.st_name));
745
746 dynstr = elf_hash_table (info)->dynstr;
747 if (dynstr == NULL)
748 {
749 /* Create a strtab to hold the dynamic symbol names. */
750 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
751 if (dynstr == NULL)
752 return 0;
753 }
754
755 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
756 if (dynstr_index == (unsigned long) -1)
757 return 0;
758 entry->isym.st_name = dynstr_index;
759
760 eht = elf_hash_table (info);
761
762 entry->next = eht->dynlocal;
763 eht->dynlocal = entry;
764 entry->input_bfd = input_bfd;
765 entry->input_indx = input_indx;
766 eht->dynsymcount++;
767
768 /* Whatever binding the symbol had before, it's now local. */
769 entry->isym.st_info
770 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
771
772 /* The dynindx will be set at the end of size_dynamic_sections. */
773
774 return 1;
775 }
776
777 /* Return the dynindex of a local dynamic symbol. */
778
779 long
780 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
781 bfd *input_bfd,
782 long input_indx)
783 {
784 struct elf_link_local_dynamic_entry *e;
785
786 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
787 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
788 return e->dynindx;
789 return -1;
790 }
791
792 /* This function is used to renumber the dynamic symbols, if some of
793 them are removed because they are marked as local. This is called
794 via elf_link_hash_traverse. */
795
796 static bfd_boolean
797 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
798 void *data)
799 {
800 size_t *count = (size_t *) data;
801
802 if (h->forced_local)
803 return TRUE;
804
805 if (h->dynindx != -1)
806 h->dynindx = ++(*count);
807
808 return TRUE;
809 }
810
811
812 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
813 STB_LOCAL binding. */
814
815 static bfd_boolean
816 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
817 void *data)
818 {
819 size_t *count = (size_t *) data;
820
821 if (!h->forced_local)
822 return TRUE;
823
824 if (h->dynindx != -1)
825 h->dynindx = ++(*count);
826
827 return TRUE;
828 }
829
830 /* Return true if the dynamic symbol for a given section should be
831 omitted when creating a shared library. */
832 bfd_boolean
833 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
834 struct bfd_link_info *info,
835 asection *p)
836 {
837 struct elf_link_hash_table *htab;
838 asection *ip;
839
840 switch (elf_section_data (p)->this_hdr.sh_type)
841 {
842 case SHT_PROGBITS:
843 case SHT_NOBITS:
844 /* If sh_type is yet undecided, assume it could be
845 SHT_PROGBITS/SHT_NOBITS. */
846 case SHT_NULL:
847 htab = elf_hash_table (info);
848 if (p == htab->tls_sec)
849 return FALSE;
850
851 if (htab->text_index_section != NULL)
852 return p != htab->text_index_section && p != htab->data_index_section;
853
854 return (htab->dynobj != NULL
855 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
856 && ip->output_section == p);
857
858 /* There shouldn't be section relative relocations
859 against any other section. */
860 default:
861 return TRUE;
862 }
863 }
864
865 /* Assign dynsym indices. In a shared library we generate a section
866 symbol for each output section, which come first. Next come symbols
867 which have been forced to local binding. Then all of the back-end
868 allocated local dynamic syms, followed by the rest of the global
869 symbols. */
870
871 static unsigned long
872 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
873 struct bfd_link_info *info,
874 unsigned long *section_sym_count)
875 {
876 unsigned long dynsymcount = 0;
877
878 if (bfd_link_pic (info)
879 || elf_hash_table (info)->is_relocatable_executable)
880 {
881 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
882 asection *p;
883 for (p = output_bfd->sections; p ; p = p->next)
884 if ((p->flags & SEC_EXCLUDE) == 0
885 && (p->flags & SEC_ALLOC) != 0
886 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
887 elf_section_data (p)->dynindx = ++dynsymcount;
888 else
889 elf_section_data (p)->dynindx = 0;
890 }
891 *section_sym_count = dynsymcount;
892
893 elf_link_hash_traverse (elf_hash_table (info),
894 elf_link_renumber_local_hash_table_dynsyms,
895 &dynsymcount);
896
897 if (elf_hash_table (info)->dynlocal)
898 {
899 struct elf_link_local_dynamic_entry *p;
900 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
901 p->dynindx = ++dynsymcount;
902 }
903
904 elf_link_hash_traverse (elf_hash_table (info),
905 elf_link_renumber_hash_table_dynsyms,
906 &dynsymcount);
907
908 /* There is an unused NULL entry at the head of the table which we
909 must account for in our count even if the table is empty since it
910 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
911 .dynamic section. */
912 dynsymcount++;
913
914 elf_hash_table (info)->dynsymcount = dynsymcount;
915 return dynsymcount;
916 }
917
918 /* Merge st_other field. */
919
920 static void
921 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
922 const Elf_Internal_Sym *isym, asection *sec,
923 bfd_boolean definition, bfd_boolean dynamic)
924 {
925 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
926
927 /* If st_other has a processor-specific meaning, specific
928 code might be needed here. */
929 if (bed->elf_backend_merge_symbol_attribute)
930 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
931 dynamic);
932
933 if (!dynamic)
934 {
935 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
936 unsigned hvis = ELF_ST_VISIBILITY (h->other);
937
938 /* Keep the most constraining visibility. Leave the remainder
939 of the st_other field to elf_backend_merge_symbol_attribute. */
940 if (symvis - 1 < hvis - 1)
941 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
942 }
943 else if (definition
944 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
945 && (sec->flags & SEC_READONLY) == 0)
946 h->protected_def = 1;
947 }
948
949 /* This function is called when we want to merge a new symbol with an
950 existing symbol. It handles the various cases which arise when we
951 find a definition in a dynamic object, or when there is already a
952 definition in a dynamic object. The new symbol is described by
953 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
954 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
955 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
956 of an old common symbol. We set OVERRIDE if the old symbol is
957 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
958 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
959 to change. By OK to change, we mean that we shouldn't warn if the
960 type or size does change. */
961
962 static bfd_boolean
963 _bfd_elf_merge_symbol (bfd *abfd,
964 struct bfd_link_info *info,
965 const char *name,
966 Elf_Internal_Sym *sym,
967 asection **psec,
968 bfd_vma *pvalue,
969 struct elf_link_hash_entry **sym_hash,
970 bfd **poldbfd,
971 bfd_boolean *pold_weak,
972 unsigned int *pold_alignment,
973 bfd_boolean *skip,
974 bfd_boolean *override,
975 bfd_boolean *type_change_ok,
976 bfd_boolean *size_change_ok,
977 bfd_boolean *matched)
978 {
979 asection *sec, *oldsec;
980 struct elf_link_hash_entry *h;
981 struct elf_link_hash_entry *hi;
982 struct elf_link_hash_entry *flip;
983 int bind;
984 bfd *oldbfd;
985 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
986 bfd_boolean newweak, oldweak, newfunc, oldfunc;
987 const struct elf_backend_data *bed;
988 char *new_version;
989
990 *skip = FALSE;
991 *override = FALSE;
992
993 sec = *psec;
994 bind = ELF_ST_BIND (sym->st_info);
995
996 if (! bfd_is_und_section (sec))
997 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
998 else
999 h = ((struct elf_link_hash_entry *)
1000 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1001 if (h == NULL)
1002 return FALSE;
1003 *sym_hash = h;
1004
1005 bed = get_elf_backend_data (abfd);
1006
1007 /* NEW_VERSION is the symbol version of the new symbol. */
1008 if (h->versioned != unversioned)
1009 {
1010 /* Symbol version is unknown or versioned. */
1011 new_version = strrchr (name, ELF_VER_CHR);
1012 if (new_version)
1013 {
1014 if (h->versioned == unknown)
1015 {
1016 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1017 h->versioned = versioned_hidden;
1018 else
1019 h->versioned = versioned;
1020 }
1021 new_version += 1;
1022 if (new_version[0] == '\0')
1023 new_version = NULL;
1024 }
1025 else
1026 h->versioned = unversioned;
1027 }
1028 else
1029 new_version = NULL;
1030
1031 /* For merging, we only care about real symbols. But we need to make
1032 sure that indirect symbol dynamic flags are updated. */
1033 hi = h;
1034 while (h->root.type == bfd_link_hash_indirect
1035 || h->root.type == bfd_link_hash_warning)
1036 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1037
1038 if (!*matched)
1039 {
1040 if (hi == h || h->root.type == bfd_link_hash_new)
1041 *matched = TRUE;
1042 else
1043 {
1044 /* OLD_HIDDEN is true if the existing symbol is only visible
1045 to the symbol with the same symbol version. NEW_HIDDEN is
1046 true if the new symbol is only visible to the symbol with
1047 the same symbol version. */
1048 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1049 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1050 if (!old_hidden && !new_hidden)
1051 /* The new symbol matches the existing symbol if both
1052 aren't hidden. */
1053 *matched = TRUE;
1054 else
1055 {
1056 /* OLD_VERSION is the symbol version of the existing
1057 symbol. */
1058 char *old_version;
1059
1060 if (h->versioned >= versioned)
1061 old_version = strrchr (h->root.root.string,
1062 ELF_VER_CHR) + 1;
1063 else
1064 old_version = NULL;
1065
1066 /* The new symbol matches the existing symbol if they
1067 have the same symbol version. */
1068 *matched = (old_version == new_version
1069 || (old_version != NULL
1070 && new_version != NULL
1071 && strcmp (old_version, new_version) == 0));
1072 }
1073 }
1074 }
1075
1076 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1077 existing symbol. */
1078
1079 oldbfd = NULL;
1080 oldsec = NULL;
1081 switch (h->root.type)
1082 {
1083 default:
1084 break;
1085
1086 case bfd_link_hash_undefined:
1087 case bfd_link_hash_undefweak:
1088 oldbfd = h->root.u.undef.abfd;
1089 break;
1090
1091 case bfd_link_hash_defined:
1092 case bfd_link_hash_defweak:
1093 oldbfd = h->root.u.def.section->owner;
1094 oldsec = h->root.u.def.section;
1095 break;
1096
1097 case bfd_link_hash_common:
1098 oldbfd = h->root.u.c.p->section->owner;
1099 oldsec = h->root.u.c.p->section;
1100 if (pold_alignment)
1101 *pold_alignment = h->root.u.c.p->alignment_power;
1102 break;
1103 }
1104 if (poldbfd && *poldbfd == NULL)
1105 *poldbfd = oldbfd;
1106
1107 /* Differentiate strong and weak symbols. */
1108 newweak = bind == STB_WEAK;
1109 oldweak = (h->root.type == bfd_link_hash_defweak
1110 || h->root.type == bfd_link_hash_undefweak);
1111 if (pold_weak)
1112 *pold_weak = oldweak;
1113
1114 /* This code is for coping with dynamic objects, and is only useful
1115 if we are doing an ELF link. */
1116 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1117 return TRUE;
1118
1119 /* We have to check it for every instance since the first few may be
1120 references and not all compilers emit symbol type for undefined
1121 symbols. */
1122 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1123
1124 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1125 respectively, is from a dynamic object. */
1126
1127 newdyn = (abfd->flags & DYNAMIC) != 0;
1128
1129 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1130 syms and defined syms in dynamic libraries respectively.
1131 ref_dynamic on the other hand can be set for a symbol defined in
1132 a dynamic library, and def_dynamic may not be set; When the
1133 definition in a dynamic lib is overridden by a definition in the
1134 executable use of the symbol in the dynamic lib becomes a
1135 reference to the executable symbol. */
1136 if (newdyn)
1137 {
1138 if (bfd_is_und_section (sec))
1139 {
1140 if (bind != STB_WEAK)
1141 {
1142 h->ref_dynamic_nonweak = 1;
1143 hi->ref_dynamic_nonweak = 1;
1144 }
1145 }
1146 else
1147 {
1148 /* Update the existing symbol only if they match. */
1149 if (*matched)
1150 h->dynamic_def = 1;
1151 hi->dynamic_def = 1;
1152 }
1153 }
1154
1155 /* If we just created the symbol, mark it as being an ELF symbol.
1156 Other than that, there is nothing to do--there is no merge issue
1157 with a newly defined symbol--so we just return. */
1158
1159 if (h->root.type == bfd_link_hash_new)
1160 {
1161 h->non_elf = 0;
1162 return TRUE;
1163 }
1164
1165 /* In cases involving weak versioned symbols, we may wind up trying
1166 to merge a symbol with itself. Catch that here, to avoid the
1167 confusion that results if we try to override a symbol with
1168 itself. The additional tests catch cases like
1169 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1170 dynamic object, which we do want to handle here. */
1171 if (abfd == oldbfd
1172 && (newweak || oldweak)
1173 && ((abfd->flags & DYNAMIC) == 0
1174 || !h->def_regular))
1175 return TRUE;
1176
1177 olddyn = FALSE;
1178 if (oldbfd != NULL)
1179 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1180 else if (oldsec != NULL)
1181 {
1182 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1183 indices used by MIPS ELF. */
1184 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1185 }
1186
1187 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1188 respectively, appear to be a definition rather than reference. */
1189
1190 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1191
1192 olddef = (h->root.type != bfd_link_hash_undefined
1193 && h->root.type != bfd_link_hash_undefweak
1194 && h->root.type != bfd_link_hash_common);
1195
1196 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1197 respectively, appear to be a function. */
1198
1199 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1200 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1201
1202 oldfunc = (h->type != STT_NOTYPE
1203 && bed->is_function_type (h->type));
1204
1205 /* When we try to create a default indirect symbol from the dynamic
1206 definition with the default version, we skip it if its type and
1207 the type of existing regular definition mismatch. */
1208 if (pold_alignment == NULL
1209 && newdyn
1210 && newdef
1211 && !olddyn
1212 && (((olddef || h->root.type == bfd_link_hash_common)
1213 && ELF_ST_TYPE (sym->st_info) != h->type
1214 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1215 && h->type != STT_NOTYPE
1216 && !(newfunc && oldfunc))
1217 || (olddef
1218 && ((h->type == STT_GNU_IFUNC)
1219 != (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))))
1220 {
1221 *skip = TRUE;
1222 return TRUE;
1223 }
1224
1225 /* Check TLS symbols. We don't check undefined symbols introduced
1226 by "ld -u" which have no type (and oldbfd NULL), and we don't
1227 check symbols from plugins because they also have no type. */
1228 if (oldbfd != NULL
1229 && (oldbfd->flags & BFD_PLUGIN) == 0
1230 && (abfd->flags & BFD_PLUGIN) == 0
1231 && ELF_ST_TYPE (sym->st_info) != h->type
1232 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1233 {
1234 bfd *ntbfd, *tbfd;
1235 bfd_boolean ntdef, tdef;
1236 asection *ntsec, *tsec;
1237
1238 if (h->type == STT_TLS)
1239 {
1240 ntbfd = abfd;
1241 ntsec = sec;
1242 ntdef = newdef;
1243 tbfd = oldbfd;
1244 tsec = oldsec;
1245 tdef = olddef;
1246 }
1247 else
1248 {
1249 ntbfd = oldbfd;
1250 ntsec = oldsec;
1251 ntdef = olddef;
1252 tbfd = abfd;
1253 tsec = sec;
1254 tdef = newdef;
1255 }
1256
1257 if (tdef && ntdef)
1258 (*_bfd_error_handler)
1259 (_("%s: TLS definition in %B section %A "
1260 "mismatches non-TLS definition in %B section %A"),
1261 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1262 else if (!tdef && !ntdef)
1263 (*_bfd_error_handler)
1264 (_("%s: TLS reference in %B "
1265 "mismatches non-TLS reference in %B"),
1266 tbfd, ntbfd, h->root.root.string);
1267 else if (tdef)
1268 (*_bfd_error_handler)
1269 (_("%s: TLS definition in %B section %A "
1270 "mismatches non-TLS reference in %B"),
1271 tbfd, tsec, ntbfd, h->root.root.string);
1272 else
1273 (*_bfd_error_handler)
1274 (_("%s: TLS reference in %B "
1275 "mismatches non-TLS definition in %B section %A"),
1276 tbfd, ntbfd, ntsec, h->root.root.string);
1277
1278 bfd_set_error (bfd_error_bad_value);
1279 return FALSE;
1280 }
1281
1282 /* If the old symbol has non-default visibility, we ignore the new
1283 definition from a dynamic object. */
1284 if (newdyn
1285 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1286 && !bfd_is_und_section (sec))
1287 {
1288 *skip = TRUE;
1289 /* Make sure this symbol is dynamic. */
1290 h->ref_dynamic = 1;
1291 hi->ref_dynamic = 1;
1292 /* A protected symbol has external availability. Make sure it is
1293 recorded as dynamic.
1294
1295 FIXME: Should we check type and size for protected symbol? */
1296 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1297 return bfd_elf_link_record_dynamic_symbol (info, h);
1298 else
1299 return TRUE;
1300 }
1301 else if (!newdyn
1302 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1303 && h->def_dynamic)
1304 {
1305 /* If the new symbol with non-default visibility comes from a
1306 relocatable file and the old definition comes from a dynamic
1307 object, we remove the old definition. */
1308 if (hi->root.type == bfd_link_hash_indirect)
1309 {
1310 /* Handle the case where the old dynamic definition is
1311 default versioned. We need to copy the symbol info from
1312 the symbol with default version to the normal one if it
1313 was referenced before. */
1314 if (h->ref_regular)
1315 {
1316 hi->root.type = h->root.type;
1317 h->root.type = bfd_link_hash_indirect;
1318 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1319
1320 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1321 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1322 {
1323 /* If the new symbol is hidden or internal, completely undo
1324 any dynamic link state. */
1325 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1326 h->forced_local = 0;
1327 h->ref_dynamic = 0;
1328 }
1329 else
1330 h->ref_dynamic = 1;
1331
1332 h->def_dynamic = 0;
1333 /* FIXME: Should we check type and size for protected symbol? */
1334 h->size = 0;
1335 h->type = 0;
1336
1337 h = hi;
1338 }
1339 else
1340 h = hi;
1341 }
1342
1343 /* If the old symbol was undefined before, then it will still be
1344 on the undefs list. If the new symbol is undefined or
1345 common, we can't make it bfd_link_hash_new here, because new
1346 undefined or common symbols will be added to the undefs list
1347 by _bfd_generic_link_add_one_symbol. Symbols may not be
1348 added twice to the undefs list. Also, if the new symbol is
1349 undefweak then we don't want to lose the strong undef. */
1350 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1351 {
1352 h->root.type = bfd_link_hash_undefined;
1353 h->root.u.undef.abfd = abfd;
1354 }
1355 else
1356 {
1357 h->root.type = bfd_link_hash_new;
1358 h->root.u.undef.abfd = NULL;
1359 }
1360
1361 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1362 {
1363 /* If the new symbol is hidden or internal, completely undo
1364 any dynamic link state. */
1365 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1366 h->forced_local = 0;
1367 h->ref_dynamic = 0;
1368 }
1369 else
1370 h->ref_dynamic = 1;
1371 h->def_dynamic = 0;
1372 /* FIXME: Should we check type and size for protected symbol? */
1373 h->size = 0;
1374 h->type = 0;
1375 return TRUE;
1376 }
1377
1378 /* If a new weak symbol definition comes from a regular file and the
1379 old symbol comes from a dynamic library, we treat the new one as
1380 strong. Similarly, an old weak symbol definition from a regular
1381 file is treated as strong when the new symbol comes from a dynamic
1382 library. Further, an old weak symbol from a dynamic library is
1383 treated as strong if the new symbol is from a dynamic library.
1384 This reflects the way glibc's ld.so works.
1385
1386 Do this before setting *type_change_ok or *size_change_ok so that
1387 we warn properly when dynamic library symbols are overridden. */
1388
1389 if (newdef && !newdyn && olddyn)
1390 newweak = FALSE;
1391 if (olddef && newdyn)
1392 oldweak = FALSE;
1393
1394 /* Allow changes between different types of function symbol. */
1395 if (newfunc && oldfunc)
1396 *type_change_ok = TRUE;
1397
1398 /* It's OK to change the type if either the existing symbol or the
1399 new symbol is weak. A type change is also OK if the old symbol
1400 is undefined and the new symbol is defined. */
1401
1402 if (oldweak
1403 || newweak
1404 || (newdef
1405 && h->root.type == bfd_link_hash_undefined))
1406 *type_change_ok = TRUE;
1407
1408 /* It's OK to change the size if either the existing symbol or the
1409 new symbol is weak, or if the old symbol is undefined. */
1410
1411 if (*type_change_ok
1412 || h->root.type == bfd_link_hash_undefined)
1413 *size_change_ok = TRUE;
1414
1415 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1416 symbol, respectively, appears to be a common symbol in a dynamic
1417 object. If a symbol appears in an uninitialized section, and is
1418 not weak, and is not a function, then it may be a common symbol
1419 which was resolved when the dynamic object was created. We want
1420 to treat such symbols specially, because they raise special
1421 considerations when setting the symbol size: if the symbol
1422 appears as a common symbol in a regular object, and the size in
1423 the regular object is larger, we must make sure that we use the
1424 larger size. This problematic case can always be avoided in C,
1425 but it must be handled correctly when using Fortran shared
1426 libraries.
1427
1428 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1429 likewise for OLDDYNCOMMON and OLDDEF.
1430
1431 Note that this test is just a heuristic, and that it is quite
1432 possible to have an uninitialized symbol in a shared object which
1433 is really a definition, rather than a common symbol. This could
1434 lead to some minor confusion when the symbol really is a common
1435 symbol in some regular object. However, I think it will be
1436 harmless. */
1437
1438 if (newdyn
1439 && newdef
1440 && !newweak
1441 && (sec->flags & SEC_ALLOC) != 0
1442 && (sec->flags & SEC_LOAD) == 0
1443 && sym->st_size > 0
1444 && !newfunc)
1445 newdyncommon = TRUE;
1446 else
1447 newdyncommon = FALSE;
1448
1449 if (olddyn
1450 && olddef
1451 && h->root.type == bfd_link_hash_defined
1452 && h->def_dynamic
1453 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1454 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1455 && h->size > 0
1456 && !oldfunc)
1457 olddyncommon = TRUE;
1458 else
1459 olddyncommon = FALSE;
1460
1461 /* We now know everything about the old and new symbols. We ask the
1462 backend to check if we can merge them. */
1463 if (bed->merge_symbol != NULL)
1464 {
1465 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1466 return FALSE;
1467 sec = *psec;
1468 }
1469
1470 /* If both the old and the new symbols look like common symbols in a
1471 dynamic object, set the size of the symbol to the larger of the
1472 two. */
1473
1474 if (olddyncommon
1475 && newdyncommon
1476 && sym->st_size != h->size)
1477 {
1478 /* Since we think we have two common symbols, issue a multiple
1479 common warning if desired. Note that we only warn if the
1480 size is different. If the size is the same, we simply let
1481 the old symbol override the new one as normally happens with
1482 symbols defined in dynamic objects. */
1483
1484 if (! ((*info->callbacks->multiple_common)
1485 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
1486 return FALSE;
1487
1488 if (sym->st_size > h->size)
1489 h->size = sym->st_size;
1490
1491 *size_change_ok = TRUE;
1492 }
1493
1494 /* If we are looking at a dynamic object, and we have found a
1495 definition, we need to see if the symbol was already defined by
1496 some other object. If so, we want to use the existing
1497 definition, and we do not want to report a multiple symbol
1498 definition error; we do this by clobbering *PSEC to be
1499 bfd_und_section_ptr.
1500
1501 We treat a common symbol as a definition if the symbol in the
1502 shared library is a function, since common symbols always
1503 represent variables; this can cause confusion in principle, but
1504 any such confusion would seem to indicate an erroneous program or
1505 shared library. We also permit a common symbol in a regular
1506 object to override a weak symbol in a shared object. A common
1507 symbol in executable also overrides a symbol in a shared object. */
1508
1509 if (newdyn
1510 && newdef
1511 && (olddef
1512 || (h->root.type == bfd_link_hash_common
1513 && (newweak
1514 || newfunc
1515 || (!olddyn && bfd_link_executable (info))))))
1516 {
1517 *override = TRUE;
1518 newdef = FALSE;
1519 newdyncommon = FALSE;
1520
1521 *psec = sec = bfd_und_section_ptr;
1522 *size_change_ok = TRUE;
1523
1524 /* If we get here when the old symbol is a common symbol, then
1525 we are explicitly letting it override a weak symbol or
1526 function in a dynamic object, and we don't want to warn about
1527 a type change. If the old symbol is a defined symbol, a type
1528 change warning may still be appropriate. */
1529
1530 if (h->root.type == bfd_link_hash_common)
1531 *type_change_ok = TRUE;
1532 }
1533
1534 /* Handle the special case of an old common symbol merging with a
1535 new symbol which looks like a common symbol in a shared object.
1536 We change *PSEC and *PVALUE to make the new symbol look like a
1537 common symbol, and let _bfd_generic_link_add_one_symbol do the
1538 right thing. */
1539
1540 if (newdyncommon
1541 && h->root.type == bfd_link_hash_common)
1542 {
1543 *override = TRUE;
1544 newdef = FALSE;
1545 newdyncommon = FALSE;
1546 *pvalue = sym->st_size;
1547 *psec = sec = bed->common_section (oldsec);
1548 *size_change_ok = TRUE;
1549 }
1550
1551 /* Skip weak definitions of symbols that are already defined. */
1552 if (newdef && olddef && newweak)
1553 {
1554 /* Don't skip new non-IR weak syms. */
1555 if (!(oldbfd != NULL
1556 && (oldbfd->flags & BFD_PLUGIN) != 0
1557 && (abfd->flags & BFD_PLUGIN) == 0))
1558 {
1559 newdef = FALSE;
1560 *skip = TRUE;
1561 }
1562
1563 /* Merge st_other. If the symbol already has a dynamic index,
1564 but visibility says it should not be visible, turn it into a
1565 local symbol. */
1566 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1567 if (h->dynindx != -1)
1568 switch (ELF_ST_VISIBILITY (h->other))
1569 {
1570 case STV_INTERNAL:
1571 case STV_HIDDEN:
1572 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1573 break;
1574 }
1575 }
1576
1577 /* If the old symbol is from a dynamic object, and the new symbol is
1578 a definition which is not from a dynamic object, then the new
1579 symbol overrides the old symbol. Symbols from regular files
1580 always take precedence over symbols from dynamic objects, even if
1581 they are defined after the dynamic object in the link.
1582
1583 As above, we again permit a common symbol in a regular object to
1584 override a definition in a shared object if the shared object
1585 symbol is a function or is weak. */
1586
1587 flip = NULL;
1588 if (!newdyn
1589 && (newdef
1590 || (bfd_is_com_section (sec)
1591 && (oldweak || oldfunc)))
1592 && olddyn
1593 && olddef
1594 && h->def_dynamic)
1595 {
1596 /* Change the hash table entry to undefined, and let
1597 _bfd_generic_link_add_one_symbol do the right thing with the
1598 new definition. */
1599
1600 h->root.type = bfd_link_hash_undefined;
1601 h->root.u.undef.abfd = h->root.u.def.section->owner;
1602 *size_change_ok = TRUE;
1603
1604 olddef = FALSE;
1605 olddyncommon = FALSE;
1606
1607 /* We again permit a type change when a common symbol may be
1608 overriding a function. */
1609
1610 if (bfd_is_com_section (sec))
1611 {
1612 if (oldfunc)
1613 {
1614 /* If a common symbol overrides a function, make sure
1615 that it isn't defined dynamically nor has type
1616 function. */
1617 h->def_dynamic = 0;
1618 h->type = STT_NOTYPE;
1619 }
1620 *type_change_ok = TRUE;
1621 }
1622
1623 if (hi->root.type == bfd_link_hash_indirect)
1624 flip = hi;
1625 else
1626 /* This union may have been set to be non-NULL when this symbol
1627 was seen in a dynamic object. We must force the union to be
1628 NULL, so that it is correct for a regular symbol. */
1629 h->verinfo.vertree = NULL;
1630 }
1631
1632 /* Handle the special case of a new common symbol merging with an
1633 old symbol that looks like it might be a common symbol defined in
1634 a shared object. Note that we have already handled the case in
1635 which a new common symbol should simply override the definition
1636 in the shared library. */
1637
1638 if (! newdyn
1639 && bfd_is_com_section (sec)
1640 && olddyncommon)
1641 {
1642 /* It would be best if we could set the hash table entry to a
1643 common symbol, but we don't know what to use for the section
1644 or the alignment. */
1645 if (! ((*info->callbacks->multiple_common)
1646 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
1647 return FALSE;
1648
1649 /* If the presumed common symbol in the dynamic object is
1650 larger, pretend that the new symbol has its size. */
1651
1652 if (h->size > *pvalue)
1653 *pvalue = h->size;
1654
1655 /* We need to remember the alignment required by the symbol
1656 in the dynamic object. */
1657 BFD_ASSERT (pold_alignment);
1658 *pold_alignment = h->root.u.def.section->alignment_power;
1659
1660 olddef = FALSE;
1661 olddyncommon = FALSE;
1662
1663 h->root.type = bfd_link_hash_undefined;
1664 h->root.u.undef.abfd = h->root.u.def.section->owner;
1665
1666 *size_change_ok = TRUE;
1667 *type_change_ok = TRUE;
1668
1669 if (hi->root.type == bfd_link_hash_indirect)
1670 flip = hi;
1671 else
1672 h->verinfo.vertree = NULL;
1673 }
1674
1675 if (flip != NULL)
1676 {
1677 /* Handle the case where we had a versioned symbol in a dynamic
1678 library and now find a definition in a normal object. In this
1679 case, we make the versioned symbol point to the normal one. */
1680 flip->root.type = h->root.type;
1681 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1682 h->root.type = bfd_link_hash_indirect;
1683 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1684 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1685 if (h->def_dynamic)
1686 {
1687 h->def_dynamic = 0;
1688 flip->ref_dynamic = 1;
1689 }
1690 }
1691
1692 return TRUE;
1693 }
1694
1695 /* This function is called to create an indirect symbol from the
1696 default for the symbol with the default version if needed. The
1697 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1698 set DYNSYM if the new indirect symbol is dynamic. */
1699
1700 static bfd_boolean
1701 _bfd_elf_add_default_symbol (bfd *abfd,
1702 struct bfd_link_info *info,
1703 struct elf_link_hash_entry *h,
1704 const char *name,
1705 Elf_Internal_Sym *sym,
1706 asection *sec,
1707 bfd_vma value,
1708 bfd **poldbfd,
1709 bfd_boolean *dynsym)
1710 {
1711 bfd_boolean type_change_ok;
1712 bfd_boolean size_change_ok;
1713 bfd_boolean skip;
1714 char *shortname;
1715 struct elf_link_hash_entry *hi;
1716 struct bfd_link_hash_entry *bh;
1717 const struct elf_backend_data *bed;
1718 bfd_boolean collect;
1719 bfd_boolean dynamic;
1720 bfd_boolean override;
1721 char *p;
1722 size_t len, shortlen;
1723 asection *tmp_sec;
1724 bfd_boolean matched;
1725
1726 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1727 return TRUE;
1728
1729 /* If this symbol has a version, and it is the default version, we
1730 create an indirect symbol from the default name to the fully
1731 decorated name. This will cause external references which do not
1732 specify a version to be bound to this version of the symbol. */
1733 p = strchr (name, ELF_VER_CHR);
1734 if (h->versioned == unknown)
1735 {
1736 if (p == NULL)
1737 {
1738 h->versioned = unversioned;
1739 return TRUE;
1740 }
1741 else
1742 {
1743 if (p[1] != ELF_VER_CHR)
1744 {
1745 h->versioned = versioned_hidden;
1746 return TRUE;
1747 }
1748 else
1749 h->versioned = versioned;
1750 }
1751 }
1752 else
1753 {
1754 /* PR ld/19073: We may see an unversioned definition after the
1755 default version. */
1756 if (p == NULL)
1757 return TRUE;
1758 }
1759
1760 bed = get_elf_backend_data (abfd);
1761 collect = bed->collect;
1762 dynamic = (abfd->flags & DYNAMIC) != 0;
1763
1764 shortlen = p - name;
1765 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1766 if (shortname == NULL)
1767 return FALSE;
1768 memcpy (shortname, name, shortlen);
1769 shortname[shortlen] = '\0';
1770
1771 /* We are going to create a new symbol. Merge it with any existing
1772 symbol with this name. For the purposes of the merge, act as
1773 though we were defining the symbol we just defined, although we
1774 actually going to define an indirect symbol. */
1775 type_change_ok = FALSE;
1776 size_change_ok = FALSE;
1777 matched = TRUE;
1778 tmp_sec = sec;
1779 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1780 &hi, poldbfd, NULL, NULL, &skip, &override,
1781 &type_change_ok, &size_change_ok, &matched))
1782 return FALSE;
1783
1784 if (skip)
1785 goto nondefault;
1786
1787 if (! override)
1788 {
1789 /* Add the default symbol if not performing a relocatable link. */
1790 if (! bfd_link_relocatable (info))
1791 {
1792 bh = &hi->root;
1793 if (! (_bfd_generic_link_add_one_symbol
1794 (info, abfd, shortname, BSF_INDIRECT,
1795 bfd_ind_section_ptr,
1796 0, name, FALSE, collect, &bh)))
1797 return FALSE;
1798 hi = (struct elf_link_hash_entry *) bh;
1799 }
1800 }
1801 else
1802 {
1803 /* In this case the symbol named SHORTNAME is overriding the
1804 indirect symbol we want to add. We were planning on making
1805 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1806 is the name without a version. NAME is the fully versioned
1807 name, and it is the default version.
1808
1809 Overriding means that we already saw a definition for the
1810 symbol SHORTNAME in a regular object, and it is overriding
1811 the symbol defined in the dynamic object.
1812
1813 When this happens, we actually want to change NAME, the
1814 symbol we just added, to refer to SHORTNAME. This will cause
1815 references to NAME in the shared object to become references
1816 to SHORTNAME in the regular object. This is what we expect
1817 when we override a function in a shared object: that the
1818 references in the shared object will be mapped to the
1819 definition in the regular object. */
1820
1821 while (hi->root.type == bfd_link_hash_indirect
1822 || hi->root.type == bfd_link_hash_warning)
1823 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1824
1825 h->root.type = bfd_link_hash_indirect;
1826 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1827 if (h->def_dynamic)
1828 {
1829 h->def_dynamic = 0;
1830 hi->ref_dynamic = 1;
1831 if (hi->ref_regular
1832 || hi->def_regular)
1833 {
1834 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1835 return FALSE;
1836 }
1837 }
1838
1839 /* Now set HI to H, so that the following code will set the
1840 other fields correctly. */
1841 hi = h;
1842 }
1843
1844 /* Check if HI is a warning symbol. */
1845 if (hi->root.type == bfd_link_hash_warning)
1846 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1847
1848 /* If there is a duplicate definition somewhere, then HI may not
1849 point to an indirect symbol. We will have reported an error to
1850 the user in that case. */
1851
1852 if (hi->root.type == bfd_link_hash_indirect)
1853 {
1854 struct elf_link_hash_entry *ht;
1855
1856 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1857 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1858
1859 /* A reference to the SHORTNAME symbol from a dynamic library
1860 will be satisfied by the versioned symbol at runtime. In
1861 effect, we have a reference to the versioned symbol. */
1862 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1863 hi->dynamic_def |= ht->dynamic_def;
1864
1865 /* See if the new flags lead us to realize that the symbol must
1866 be dynamic. */
1867 if (! *dynsym)
1868 {
1869 if (! dynamic)
1870 {
1871 if (! bfd_link_executable (info)
1872 || hi->def_dynamic
1873 || hi->ref_dynamic)
1874 *dynsym = TRUE;
1875 }
1876 else
1877 {
1878 if (hi->ref_regular)
1879 *dynsym = TRUE;
1880 }
1881 }
1882 }
1883
1884 /* We also need to define an indirection from the nondefault version
1885 of the symbol. */
1886
1887 nondefault:
1888 len = strlen (name);
1889 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
1890 if (shortname == NULL)
1891 return FALSE;
1892 memcpy (shortname, name, shortlen);
1893 memcpy (shortname + shortlen, p + 1, len - shortlen);
1894
1895 /* Once again, merge with any existing symbol. */
1896 type_change_ok = FALSE;
1897 size_change_ok = FALSE;
1898 tmp_sec = sec;
1899 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1900 &hi, poldbfd, NULL, NULL, &skip, &override,
1901 &type_change_ok, &size_change_ok, &matched))
1902 return FALSE;
1903
1904 if (skip)
1905 return TRUE;
1906
1907 if (override)
1908 {
1909 /* Here SHORTNAME is a versioned name, so we don't expect to see
1910 the type of override we do in the case above unless it is
1911 overridden by a versioned definition. */
1912 if (hi->root.type != bfd_link_hash_defined
1913 && hi->root.type != bfd_link_hash_defweak)
1914 (*_bfd_error_handler)
1915 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1916 abfd, shortname);
1917 }
1918 else
1919 {
1920 bh = &hi->root;
1921 if (! (_bfd_generic_link_add_one_symbol
1922 (info, abfd, shortname, BSF_INDIRECT,
1923 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1924 return FALSE;
1925 hi = (struct elf_link_hash_entry *) bh;
1926
1927 /* If there is a duplicate definition somewhere, then HI may not
1928 point to an indirect symbol. We will have reported an error
1929 to the user in that case. */
1930
1931 if (hi->root.type == bfd_link_hash_indirect)
1932 {
1933 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1934 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1935 hi->dynamic_def |= h->dynamic_def;
1936
1937 /* See if the new flags lead us to realize that the symbol
1938 must be dynamic. */
1939 if (! *dynsym)
1940 {
1941 if (! dynamic)
1942 {
1943 if (! bfd_link_executable (info)
1944 || hi->ref_dynamic)
1945 *dynsym = TRUE;
1946 }
1947 else
1948 {
1949 if (hi->ref_regular)
1950 *dynsym = TRUE;
1951 }
1952 }
1953 }
1954 }
1955
1956 return TRUE;
1957 }
1958 \f
1959 /* This routine is used to export all defined symbols into the dynamic
1960 symbol table. It is called via elf_link_hash_traverse. */
1961
1962 static bfd_boolean
1963 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1964 {
1965 struct elf_info_failed *eif = (struct elf_info_failed *) data;
1966
1967 /* Ignore indirect symbols. These are added by the versioning code. */
1968 if (h->root.type == bfd_link_hash_indirect)
1969 return TRUE;
1970
1971 /* Ignore this if we won't export it. */
1972 if (!eif->info->export_dynamic && !h->dynamic)
1973 return TRUE;
1974
1975 if (h->dynindx == -1
1976 && (h->def_regular || h->ref_regular)
1977 && ! bfd_hide_sym_by_version (eif->info->version_info,
1978 h->root.root.string))
1979 {
1980 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1981 {
1982 eif->failed = TRUE;
1983 return FALSE;
1984 }
1985 }
1986
1987 return TRUE;
1988 }
1989 \f
1990 /* Look through the symbols which are defined in other shared
1991 libraries and referenced here. Update the list of version
1992 dependencies. This will be put into the .gnu.version_r section.
1993 This function is called via elf_link_hash_traverse. */
1994
1995 static bfd_boolean
1996 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1997 void *data)
1998 {
1999 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2000 Elf_Internal_Verneed *t;
2001 Elf_Internal_Vernaux *a;
2002 bfd_size_type amt;
2003
2004 /* We only care about symbols defined in shared objects with version
2005 information. */
2006 if (!h->def_dynamic
2007 || h->def_regular
2008 || h->dynindx == -1
2009 || h->verinfo.verdef == NULL
2010 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2011 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2012 return TRUE;
2013
2014 /* See if we already know about this version. */
2015 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2016 t != NULL;
2017 t = t->vn_nextref)
2018 {
2019 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2020 continue;
2021
2022 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2023 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2024 return TRUE;
2025
2026 break;
2027 }
2028
2029 /* This is a new version. Add it to tree we are building. */
2030
2031 if (t == NULL)
2032 {
2033 amt = sizeof *t;
2034 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2035 if (t == NULL)
2036 {
2037 rinfo->failed = TRUE;
2038 return FALSE;
2039 }
2040
2041 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2042 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2043 elf_tdata (rinfo->info->output_bfd)->verref = t;
2044 }
2045
2046 amt = sizeof *a;
2047 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2048 if (a == NULL)
2049 {
2050 rinfo->failed = TRUE;
2051 return FALSE;
2052 }
2053
2054 /* Note that we are copying a string pointer here, and testing it
2055 above. If bfd_elf_string_from_elf_section is ever changed to
2056 discard the string data when low in memory, this will have to be
2057 fixed. */
2058 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2059
2060 a->vna_flags = h->verinfo.verdef->vd_flags;
2061 a->vna_nextptr = t->vn_auxptr;
2062
2063 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2064 ++rinfo->vers;
2065
2066 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2067
2068 t->vn_auxptr = a;
2069
2070 return TRUE;
2071 }
2072
2073 /* Figure out appropriate versions for all the symbols. We may not
2074 have the version number script until we have read all of the input
2075 files, so until that point we don't know which symbols should be
2076 local. This function is called via elf_link_hash_traverse. */
2077
2078 static bfd_boolean
2079 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2080 {
2081 struct elf_info_failed *sinfo;
2082 struct bfd_link_info *info;
2083 const struct elf_backend_data *bed;
2084 struct elf_info_failed eif;
2085 char *p;
2086 bfd_size_type amt;
2087
2088 sinfo = (struct elf_info_failed *) data;
2089 info = sinfo->info;
2090
2091 /* Fix the symbol flags. */
2092 eif.failed = FALSE;
2093 eif.info = info;
2094 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2095 {
2096 if (eif.failed)
2097 sinfo->failed = TRUE;
2098 return FALSE;
2099 }
2100
2101 /* We only need version numbers for symbols defined in regular
2102 objects. */
2103 if (!h->def_regular)
2104 return TRUE;
2105
2106 bed = get_elf_backend_data (info->output_bfd);
2107 p = strchr (h->root.root.string, ELF_VER_CHR);
2108 if (p != NULL && h->verinfo.vertree == NULL)
2109 {
2110 struct bfd_elf_version_tree *t;
2111
2112 ++p;
2113 if (*p == ELF_VER_CHR)
2114 ++p;
2115
2116 /* If there is no version string, we can just return out. */
2117 if (*p == '\0')
2118 return TRUE;
2119
2120 /* Look for the version. If we find it, it is no longer weak. */
2121 for (t = sinfo->info->version_info; t != NULL; t = t->next)
2122 {
2123 if (strcmp (t->name, p) == 0)
2124 {
2125 size_t len;
2126 char *alc;
2127 struct bfd_elf_version_expr *d;
2128
2129 len = p - h->root.root.string;
2130 alc = (char *) bfd_malloc (len);
2131 if (alc == NULL)
2132 {
2133 sinfo->failed = TRUE;
2134 return FALSE;
2135 }
2136 memcpy (alc, h->root.root.string, len - 1);
2137 alc[len - 1] = '\0';
2138 if (alc[len - 2] == ELF_VER_CHR)
2139 alc[len - 2] = '\0';
2140
2141 h->verinfo.vertree = t;
2142 t->used = TRUE;
2143 d = NULL;
2144
2145 if (t->globals.list != NULL)
2146 d = (*t->match) (&t->globals, NULL, alc);
2147
2148 /* See if there is anything to force this symbol to
2149 local scope. */
2150 if (d == NULL && t->locals.list != NULL)
2151 {
2152 d = (*t->match) (&t->locals, NULL, alc);
2153 if (d != NULL
2154 && h->dynindx != -1
2155 && ! info->export_dynamic)
2156 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2157 }
2158
2159 free (alc);
2160 break;
2161 }
2162 }
2163
2164 /* If we are building an application, we need to create a
2165 version node for this version. */
2166 if (t == NULL && bfd_link_executable (info))
2167 {
2168 struct bfd_elf_version_tree **pp;
2169 int version_index;
2170
2171 /* If we aren't going to export this symbol, we don't need
2172 to worry about it. */
2173 if (h->dynindx == -1)
2174 return TRUE;
2175
2176 amt = sizeof *t;
2177 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, amt);
2178 if (t == NULL)
2179 {
2180 sinfo->failed = TRUE;
2181 return FALSE;
2182 }
2183
2184 t->name = p;
2185 t->name_indx = (unsigned int) -1;
2186 t->used = TRUE;
2187
2188 version_index = 1;
2189 /* Don't count anonymous version tag. */
2190 if (sinfo->info->version_info != NULL
2191 && sinfo->info->version_info->vernum == 0)
2192 version_index = 0;
2193 for (pp = &sinfo->info->version_info;
2194 *pp != NULL;
2195 pp = &(*pp)->next)
2196 ++version_index;
2197 t->vernum = version_index;
2198
2199 *pp = t;
2200
2201 h->verinfo.vertree = t;
2202 }
2203 else if (t == NULL)
2204 {
2205 /* We could not find the version for a symbol when
2206 generating a shared archive. Return an error. */
2207 (*_bfd_error_handler)
2208 (_("%B: version node not found for symbol %s"),
2209 info->output_bfd, h->root.root.string);
2210 bfd_set_error (bfd_error_bad_value);
2211 sinfo->failed = TRUE;
2212 return FALSE;
2213 }
2214 }
2215
2216 /* If we don't have a version for this symbol, see if we can find
2217 something. */
2218 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
2219 {
2220 bfd_boolean hide;
2221
2222 h->verinfo.vertree
2223 = bfd_find_version_for_sym (sinfo->info->version_info,
2224 h->root.root.string, &hide);
2225 if (h->verinfo.vertree != NULL && hide)
2226 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2227 }
2228
2229 return TRUE;
2230 }
2231 \f
2232 /* Read and swap the relocs from the section indicated by SHDR. This
2233 may be either a REL or a RELA section. The relocations are
2234 translated into RELA relocations and stored in INTERNAL_RELOCS,
2235 which should have already been allocated to contain enough space.
2236 The EXTERNAL_RELOCS are a buffer where the external form of the
2237 relocations should be stored.
2238
2239 Returns FALSE if something goes wrong. */
2240
2241 static bfd_boolean
2242 elf_link_read_relocs_from_section (bfd *abfd,
2243 asection *sec,
2244 Elf_Internal_Shdr *shdr,
2245 void *external_relocs,
2246 Elf_Internal_Rela *internal_relocs)
2247 {
2248 const struct elf_backend_data *bed;
2249 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2250 const bfd_byte *erela;
2251 const bfd_byte *erelaend;
2252 Elf_Internal_Rela *irela;
2253 Elf_Internal_Shdr *symtab_hdr;
2254 size_t nsyms;
2255
2256 /* Position ourselves at the start of the section. */
2257 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2258 return FALSE;
2259
2260 /* Read the relocations. */
2261 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2262 return FALSE;
2263
2264 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2265 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2266
2267 bed = get_elf_backend_data (abfd);
2268
2269 /* Convert the external relocations to the internal format. */
2270 if (shdr->sh_entsize == bed->s->sizeof_rel)
2271 swap_in = bed->s->swap_reloc_in;
2272 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2273 swap_in = bed->s->swap_reloca_in;
2274 else
2275 {
2276 bfd_set_error (bfd_error_wrong_format);
2277 return FALSE;
2278 }
2279
2280 erela = (const bfd_byte *) external_relocs;
2281 erelaend = erela + shdr->sh_size;
2282 irela = internal_relocs;
2283 while (erela < erelaend)
2284 {
2285 bfd_vma r_symndx;
2286
2287 (*swap_in) (abfd, erela, irela);
2288 r_symndx = ELF32_R_SYM (irela->r_info);
2289 if (bed->s->arch_size == 64)
2290 r_symndx >>= 24;
2291 if (nsyms > 0)
2292 {
2293 if ((size_t) r_symndx >= nsyms)
2294 {
2295 (*_bfd_error_handler)
2296 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2297 " for offset 0x%lx in section `%A'"),
2298 abfd, sec,
2299 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2300 bfd_set_error (bfd_error_bad_value);
2301 return FALSE;
2302 }
2303 }
2304 else if (r_symndx != STN_UNDEF)
2305 {
2306 (*_bfd_error_handler)
2307 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2308 " when the object file has no symbol table"),
2309 abfd, sec,
2310 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2311 bfd_set_error (bfd_error_bad_value);
2312 return FALSE;
2313 }
2314 irela += bed->s->int_rels_per_ext_rel;
2315 erela += shdr->sh_entsize;
2316 }
2317
2318 return TRUE;
2319 }
2320
2321 /* Read and swap the relocs for a section O. They may have been
2322 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2323 not NULL, they are used as buffers to read into. They are known to
2324 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2325 the return value is allocated using either malloc or bfd_alloc,
2326 according to the KEEP_MEMORY argument. If O has two relocation
2327 sections (both REL and RELA relocations), then the REL_HDR
2328 relocations will appear first in INTERNAL_RELOCS, followed by the
2329 RELA_HDR relocations. */
2330
2331 Elf_Internal_Rela *
2332 _bfd_elf_link_read_relocs (bfd *abfd,
2333 asection *o,
2334 void *external_relocs,
2335 Elf_Internal_Rela *internal_relocs,
2336 bfd_boolean keep_memory)
2337 {
2338 void *alloc1 = NULL;
2339 Elf_Internal_Rela *alloc2 = NULL;
2340 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2341 struct bfd_elf_section_data *esdo = elf_section_data (o);
2342 Elf_Internal_Rela *internal_rela_relocs;
2343
2344 if (esdo->relocs != NULL)
2345 return esdo->relocs;
2346
2347 if (o->reloc_count == 0)
2348 return NULL;
2349
2350 if (internal_relocs == NULL)
2351 {
2352 bfd_size_type size;
2353
2354 size = o->reloc_count;
2355 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2356 if (keep_memory)
2357 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2358 else
2359 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2360 if (internal_relocs == NULL)
2361 goto error_return;
2362 }
2363
2364 if (external_relocs == NULL)
2365 {
2366 bfd_size_type size = 0;
2367
2368 if (esdo->rel.hdr)
2369 size += esdo->rel.hdr->sh_size;
2370 if (esdo->rela.hdr)
2371 size += esdo->rela.hdr->sh_size;
2372
2373 alloc1 = bfd_malloc (size);
2374 if (alloc1 == NULL)
2375 goto error_return;
2376 external_relocs = alloc1;
2377 }
2378
2379 internal_rela_relocs = internal_relocs;
2380 if (esdo->rel.hdr)
2381 {
2382 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2383 external_relocs,
2384 internal_relocs))
2385 goto error_return;
2386 external_relocs = (((bfd_byte *) external_relocs)
2387 + esdo->rel.hdr->sh_size);
2388 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2389 * bed->s->int_rels_per_ext_rel);
2390 }
2391
2392 if (esdo->rela.hdr
2393 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2394 external_relocs,
2395 internal_rela_relocs)))
2396 goto error_return;
2397
2398 /* Cache the results for next time, if we can. */
2399 if (keep_memory)
2400 esdo->relocs = internal_relocs;
2401
2402 if (alloc1 != NULL)
2403 free (alloc1);
2404
2405 /* Don't free alloc2, since if it was allocated we are passing it
2406 back (under the name of internal_relocs). */
2407
2408 return internal_relocs;
2409
2410 error_return:
2411 if (alloc1 != NULL)
2412 free (alloc1);
2413 if (alloc2 != NULL)
2414 {
2415 if (keep_memory)
2416 bfd_release (abfd, alloc2);
2417 else
2418 free (alloc2);
2419 }
2420 return NULL;
2421 }
2422
2423 /* Compute the size of, and allocate space for, REL_HDR which is the
2424 section header for a section containing relocations for O. */
2425
2426 static bfd_boolean
2427 _bfd_elf_link_size_reloc_section (bfd *abfd,
2428 struct bfd_elf_section_reloc_data *reldata)
2429 {
2430 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2431
2432 /* That allows us to calculate the size of the section. */
2433 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2434
2435 /* The contents field must last into write_object_contents, so we
2436 allocate it with bfd_alloc rather than malloc. Also since we
2437 cannot be sure that the contents will actually be filled in,
2438 we zero the allocated space. */
2439 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2440 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2441 return FALSE;
2442
2443 if (reldata->hashes == NULL && reldata->count)
2444 {
2445 struct elf_link_hash_entry **p;
2446
2447 p = ((struct elf_link_hash_entry **)
2448 bfd_zmalloc (reldata->count * sizeof (*p)));
2449 if (p == NULL)
2450 return FALSE;
2451
2452 reldata->hashes = p;
2453 }
2454
2455 return TRUE;
2456 }
2457
2458 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2459 originated from the section given by INPUT_REL_HDR) to the
2460 OUTPUT_BFD. */
2461
2462 bfd_boolean
2463 _bfd_elf_link_output_relocs (bfd *output_bfd,
2464 asection *input_section,
2465 Elf_Internal_Shdr *input_rel_hdr,
2466 Elf_Internal_Rela *internal_relocs,
2467 struct elf_link_hash_entry **rel_hash
2468 ATTRIBUTE_UNUSED)
2469 {
2470 Elf_Internal_Rela *irela;
2471 Elf_Internal_Rela *irelaend;
2472 bfd_byte *erel;
2473 struct bfd_elf_section_reloc_data *output_reldata;
2474 asection *output_section;
2475 const struct elf_backend_data *bed;
2476 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2477 struct bfd_elf_section_data *esdo;
2478
2479 output_section = input_section->output_section;
2480
2481 bed = get_elf_backend_data (output_bfd);
2482 esdo = elf_section_data (output_section);
2483 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2484 {
2485 output_reldata = &esdo->rel;
2486 swap_out = bed->s->swap_reloc_out;
2487 }
2488 else if (esdo->rela.hdr
2489 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2490 {
2491 output_reldata = &esdo->rela;
2492 swap_out = bed->s->swap_reloca_out;
2493 }
2494 else
2495 {
2496 (*_bfd_error_handler)
2497 (_("%B: relocation size mismatch in %B section %A"),
2498 output_bfd, input_section->owner, input_section);
2499 bfd_set_error (bfd_error_wrong_format);
2500 return FALSE;
2501 }
2502
2503 erel = output_reldata->hdr->contents;
2504 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2505 irela = internal_relocs;
2506 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2507 * bed->s->int_rels_per_ext_rel);
2508 while (irela < irelaend)
2509 {
2510 (*swap_out) (output_bfd, irela, erel);
2511 irela += bed->s->int_rels_per_ext_rel;
2512 erel += input_rel_hdr->sh_entsize;
2513 }
2514
2515 /* Bump the counter, so that we know where to add the next set of
2516 relocations. */
2517 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2518
2519 return TRUE;
2520 }
2521 \f
2522 /* Make weak undefined symbols in PIE dynamic. */
2523
2524 bfd_boolean
2525 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2526 struct elf_link_hash_entry *h)
2527 {
2528 if (bfd_link_pie (info)
2529 && h->dynindx == -1
2530 && h->root.type == bfd_link_hash_undefweak)
2531 return bfd_elf_link_record_dynamic_symbol (info, h);
2532
2533 return TRUE;
2534 }
2535
2536 /* Fix up the flags for a symbol. This handles various cases which
2537 can only be fixed after all the input files are seen. This is
2538 currently called by both adjust_dynamic_symbol and
2539 assign_sym_version, which is unnecessary but perhaps more robust in
2540 the face of future changes. */
2541
2542 static bfd_boolean
2543 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2544 struct elf_info_failed *eif)
2545 {
2546 const struct elf_backend_data *bed;
2547
2548 /* If this symbol was mentioned in a non-ELF file, try to set
2549 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2550 permit a non-ELF file to correctly refer to a symbol defined in
2551 an ELF dynamic object. */
2552 if (h->non_elf)
2553 {
2554 while (h->root.type == bfd_link_hash_indirect)
2555 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2556
2557 if (h->root.type != bfd_link_hash_defined
2558 && h->root.type != bfd_link_hash_defweak)
2559 {
2560 h->ref_regular = 1;
2561 h->ref_regular_nonweak = 1;
2562 }
2563 else
2564 {
2565 if (h->root.u.def.section->owner != NULL
2566 && (bfd_get_flavour (h->root.u.def.section->owner)
2567 == bfd_target_elf_flavour))
2568 {
2569 h->ref_regular = 1;
2570 h->ref_regular_nonweak = 1;
2571 }
2572 else
2573 h->def_regular = 1;
2574 }
2575
2576 if (h->dynindx == -1
2577 && (h->def_dynamic
2578 || h->ref_dynamic))
2579 {
2580 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2581 {
2582 eif->failed = TRUE;
2583 return FALSE;
2584 }
2585 }
2586 }
2587 else
2588 {
2589 /* Unfortunately, NON_ELF is only correct if the symbol
2590 was first seen in a non-ELF file. Fortunately, if the symbol
2591 was first seen in an ELF file, we're probably OK unless the
2592 symbol was defined in a non-ELF file. Catch that case here.
2593 FIXME: We're still in trouble if the symbol was first seen in
2594 a dynamic object, and then later in a non-ELF regular object. */
2595 if ((h->root.type == bfd_link_hash_defined
2596 || h->root.type == bfd_link_hash_defweak)
2597 && !h->def_regular
2598 && (h->root.u.def.section->owner != NULL
2599 ? (bfd_get_flavour (h->root.u.def.section->owner)
2600 != bfd_target_elf_flavour)
2601 : (bfd_is_abs_section (h->root.u.def.section)
2602 && !h->def_dynamic)))
2603 h->def_regular = 1;
2604 }
2605
2606 /* Backend specific symbol fixup. */
2607 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2608 if (bed->elf_backend_fixup_symbol
2609 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2610 return FALSE;
2611
2612 /* If this is a final link, and the symbol was defined as a common
2613 symbol in a regular object file, and there was no definition in
2614 any dynamic object, then the linker will have allocated space for
2615 the symbol in a common section but the DEF_REGULAR
2616 flag will not have been set. */
2617 if (h->root.type == bfd_link_hash_defined
2618 && !h->def_regular
2619 && h->ref_regular
2620 && !h->def_dynamic
2621 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2622 h->def_regular = 1;
2623
2624 /* If -Bsymbolic was used (which means to bind references to global
2625 symbols to the definition within the shared object), and this
2626 symbol was defined in a regular object, then it actually doesn't
2627 need a PLT entry. Likewise, if the symbol has non-default
2628 visibility. If the symbol has hidden or internal visibility, we
2629 will force it local. */
2630 if (h->needs_plt
2631 && bfd_link_pic (eif->info)
2632 && is_elf_hash_table (eif->info->hash)
2633 && (SYMBOLIC_BIND (eif->info, h)
2634 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2635 && h->def_regular)
2636 {
2637 bfd_boolean force_local;
2638
2639 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2640 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2641 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2642 }
2643
2644 /* If a weak undefined symbol has non-default visibility, we also
2645 hide it from the dynamic linker. */
2646 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2647 && h->root.type == bfd_link_hash_undefweak)
2648 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2649
2650 /* If this is a weak defined symbol in a dynamic object, and we know
2651 the real definition in the dynamic object, copy interesting flags
2652 over to the real definition. */
2653 if (h->u.weakdef != NULL)
2654 {
2655 /* If the real definition is defined by a regular object file,
2656 don't do anything special. See the longer description in
2657 _bfd_elf_adjust_dynamic_symbol, below. */
2658 if (h->u.weakdef->def_regular)
2659 h->u.weakdef = NULL;
2660 else
2661 {
2662 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2663
2664 while (h->root.type == bfd_link_hash_indirect)
2665 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2666
2667 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2668 || h->root.type == bfd_link_hash_defweak);
2669 BFD_ASSERT (weakdef->def_dynamic);
2670 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2671 || weakdef->root.type == bfd_link_hash_defweak);
2672 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2673 }
2674 }
2675
2676 return TRUE;
2677 }
2678
2679 /* Make the backend pick a good value for a dynamic symbol. This is
2680 called via elf_link_hash_traverse, and also calls itself
2681 recursively. */
2682
2683 static bfd_boolean
2684 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2685 {
2686 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2687 bfd *dynobj;
2688 const struct elf_backend_data *bed;
2689
2690 if (! is_elf_hash_table (eif->info->hash))
2691 return FALSE;
2692
2693 /* Ignore indirect symbols. These are added by the versioning code. */
2694 if (h->root.type == bfd_link_hash_indirect)
2695 return TRUE;
2696
2697 /* Fix the symbol flags. */
2698 if (! _bfd_elf_fix_symbol_flags (h, eif))
2699 return FALSE;
2700
2701 /* If this symbol does not require a PLT entry, and it is not
2702 defined by a dynamic object, or is not referenced by a regular
2703 object, ignore it. We do have to handle a weak defined symbol,
2704 even if no regular object refers to it, if we decided to add it
2705 to the dynamic symbol table. FIXME: Do we normally need to worry
2706 about symbols which are defined by one dynamic object and
2707 referenced by another one? */
2708 if (!h->needs_plt
2709 && h->type != STT_GNU_IFUNC
2710 && (h->def_regular
2711 || !h->def_dynamic
2712 || (!h->ref_regular
2713 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2714 {
2715 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2716 return TRUE;
2717 }
2718
2719 /* If we've already adjusted this symbol, don't do it again. This
2720 can happen via a recursive call. */
2721 if (h->dynamic_adjusted)
2722 return TRUE;
2723
2724 /* Don't look at this symbol again. Note that we must set this
2725 after checking the above conditions, because we may look at a
2726 symbol once, decide not to do anything, and then get called
2727 recursively later after REF_REGULAR is set below. */
2728 h->dynamic_adjusted = 1;
2729
2730 /* If this is a weak definition, and we know a real definition, and
2731 the real symbol is not itself defined by a regular object file,
2732 then get a good value for the real definition. We handle the
2733 real symbol first, for the convenience of the backend routine.
2734
2735 Note that there is a confusing case here. If the real definition
2736 is defined by a regular object file, we don't get the real symbol
2737 from the dynamic object, but we do get the weak symbol. If the
2738 processor backend uses a COPY reloc, then if some routine in the
2739 dynamic object changes the real symbol, we will not see that
2740 change in the corresponding weak symbol. This is the way other
2741 ELF linkers work as well, and seems to be a result of the shared
2742 library model.
2743
2744 I will clarify this issue. Most SVR4 shared libraries define the
2745 variable _timezone and define timezone as a weak synonym. The
2746 tzset call changes _timezone. If you write
2747 extern int timezone;
2748 int _timezone = 5;
2749 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2750 you might expect that, since timezone is a synonym for _timezone,
2751 the same number will print both times. However, if the processor
2752 backend uses a COPY reloc, then actually timezone will be copied
2753 into your process image, and, since you define _timezone
2754 yourself, _timezone will not. Thus timezone and _timezone will
2755 wind up at different memory locations. The tzset call will set
2756 _timezone, leaving timezone unchanged. */
2757
2758 if (h->u.weakdef != NULL)
2759 {
2760 /* If we get to this point, there is an implicit reference to
2761 H->U.WEAKDEF by a regular object file via the weak symbol H. */
2762 h->u.weakdef->ref_regular = 1;
2763
2764 /* Ensure that the backend adjust_dynamic_symbol function sees
2765 H->U.WEAKDEF before H by recursively calling ourselves. */
2766 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2767 return FALSE;
2768 }
2769
2770 /* If a symbol has no type and no size and does not require a PLT
2771 entry, then we are probably about to do the wrong thing here: we
2772 are probably going to create a COPY reloc for an empty object.
2773 This case can arise when a shared object is built with assembly
2774 code, and the assembly code fails to set the symbol type. */
2775 if (h->size == 0
2776 && h->type == STT_NOTYPE
2777 && !h->needs_plt)
2778 (*_bfd_error_handler)
2779 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2780 h->root.root.string);
2781
2782 dynobj = elf_hash_table (eif->info)->dynobj;
2783 bed = get_elf_backend_data (dynobj);
2784
2785 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2786 {
2787 eif->failed = TRUE;
2788 return FALSE;
2789 }
2790
2791 return TRUE;
2792 }
2793
2794 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2795 DYNBSS. */
2796
2797 bfd_boolean
2798 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2799 struct elf_link_hash_entry *h,
2800 asection *dynbss)
2801 {
2802 unsigned int power_of_two;
2803 bfd_vma mask;
2804 asection *sec = h->root.u.def.section;
2805
2806 /* The section aligment of definition is the maximum alignment
2807 requirement of symbols defined in the section. Since we don't
2808 know the symbol alignment requirement, we start with the
2809 maximum alignment and check low bits of the symbol address
2810 for the minimum alignment. */
2811 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2812 mask = ((bfd_vma) 1 << power_of_two) - 1;
2813 while ((h->root.u.def.value & mask) != 0)
2814 {
2815 mask >>= 1;
2816 --power_of_two;
2817 }
2818
2819 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2820 dynbss))
2821 {
2822 /* Adjust the section alignment if needed. */
2823 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2824 power_of_two))
2825 return FALSE;
2826 }
2827
2828 /* We make sure that the symbol will be aligned properly. */
2829 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2830
2831 /* Define the symbol as being at this point in DYNBSS. */
2832 h->root.u.def.section = dynbss;
2833 h->root.u.def.value = dynbss->size;
2834
2835 /* Increment the size of DYNBSS to make room for the symbol. */
2836 dynbss->size += h->size;
2837
2838 /* No error if extern_protected_data is true. */
2839 if (h->protected_def
2840 && (!info->extern_protected_data
2841 || (info->extern_protected_data < 0
2842 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
2843 info->callbacks->einfo
2844 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2845 h->root.root.string);
2846
2847 return TRUE;
2848 }
2849
2850 /* Adjust all external symbols pointing into SEC_MERGE sections
2851 to reflect the object merging within the sections. */
2852
2853 static bfd_boolean
2854 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2855 {
2856 asection *sec;
2857
2858 if ((h->root.type == bfd_link_hash_defined
2859 || h->root.type == bfd_link_hash_defweak)
2860 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2861 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
2862 {
2863 bfd *output_bfd = (bfd *) data;
2864
2865 h->root.u.def.value =
2866 _bfd_merged_section_offset (output_bfd,
2867 &h->root.u.def.section,
2868 elf_section_data (sec)->sec_info,
2869 h->root.u.def.value);
2870 }
2871
2872 return TRUE;
2873 }
2874
2875 /* Returns false if the symbol referred to by H should be considered
2876 to resolve local to the current module, and true if it should be
2877 considered to bind dynamically. */
2878
2879 bfd_boolean
2880 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2881 struct bfd_link_info *info,
2882 bfd_boolean not_local_protected)
2883 {
2884 bfd_boolean binding_stays_local_p;
2885 const struct elf_backend_data *bed;
2886 struct elf_link_hash_table *hash_table;
2887
2888 if (h == NULL)
2889 return FALSE;
2890
2891 while (h->root.type == bfd_link_hash_indirect
2892 || h->root.type == bfd_link_hash_warning)
2893 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2894
2895 /* If it was forced local, then clearly it's not dynamic. */
2896 if (h->dynindx == -1)
2897 return FALSE;
2898 if (h->forced_local)
2899 return FALSE;
2900
2901 /* Identify the cases where name binding rules say that a
2902 visible symbol resolves locally. */
2903 binding_stays_local_p = (bfd_link_executable (info)
2904 || SYMBOLIC_BIND (info, h));
2905
2906 switch (ELF_ST_VISIBILITY (h->other))
2907 {
2908 case STV_INTERNAL:
2909 case STV_HIDDEN:
2910 return FALSE;
2911
2912 case STV_PROTECTED:
2913 hash_table = elf_hash_table (info);
2914 if (!is_elf_hash_table (hash_table))
2915 return FALSE;
2916
2917 bed = get_elf_backend_data (hash_table->dynobj);
2918
2919 /* Proper resolution for function pointer equality may require
2920 that these symbols perhaps be resolved dynamically, even though
2921 we should be resolving them to the current module. */
2922 if (!not_local_protected || !bed->is_function_type (h->type))
2923 binding_stays_local_p = TRUE;
2924 break;
2925
2926 default:
2927 break;
2928 }
2929
2930 /* If it isn't defined locally, then clearly it's dynamic. */
2931 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2932 return TRUE;
2933
2934 /* Otherwise, the symbol is dynamic if binding rules don't tell
2935 us that it remains local. */
2936 return !binding_stays_local_p;
2937 }
2938
2939 /* Return true if the symbol referred to by H should be considered
2940 to resolve local to the current module, and false otherwise. Differs
2941 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2942 undefined symbols. The two functions are virtually identical except
2943 for the place where forced_local and dynindx == -1 are tested. If
2944 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2945 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2946 the symbol is local only for defined symbols.
2947 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2948 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2949 treatment of undefined weak symbols. For those that do not make
2950 undefined weak symbols dynamic, both functions may return false. */
2951
2952 bfd_boolean
2953 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2954 struct bfd_link_info *info,
2955 bfd_boolean local_protected)
2956 {
2957 const struct elf_backend_data *bed;
2958 struct elf_link_hash_table *hash_table;
2959
2960 /* If it's a local sym, of course we resolve locally. */
2961 if (h == NULL)
2962 return TRUE;
2963
2964 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2965 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2966 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2967 return TRUE;
2968
2969 /* Common symbols that become definitions don't get the DEF_REGULAR
2970 flag set, so test it first, and don't bail out. */
2971 if (ELF_COMMON_DEF_P (h))
2972 /* Do nothing. */;
2973 /* If we don't have a definition in a regular file, then we can't
2974 resolve locally. The sym is either undefined or dynamic. */
2975 else if (!h->def_regular)
2976 return FALSE;
2977
2978 /* Forced local symbols resolve locally. */
2979 if (h->forced_local)
2980 return TRUE;
2981
2982 /* As do non-dynamic symbols. */
2983 if (h->dynindx == -1)
2984 return TRUE;
2985
2986 /* At this point, we know the symbol is defined and dynamic. In an
2987 executable it must resolve locally, likewise when building symbolic
2988 shared libraries. */
2989 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
2990 return TRUE;
2991
2992 /* Now deal with defined dynamic symbols in shared libraries. Ones
2993 with default visibility might not resolve locally. */
2994 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2995 return FALSE;
2996
2997 hash_table = elf_hash_table (info);
2998 if (!is_elf_hash_table (hash_table))
2999 return TRUE;
3000
3001 bed = get_elf_backend_data (hash_table->dynobj);
3002
3003 /* If extern_protected_data is false, STV_PROTECTED non-function
3004 symbols are local. */
3005 if ((!info->extern_protected_data
3006 || (info->extern_protected_data < 0
3007 && !bed->extern_protected_data))
3008 && !bed->is_function_type (h->type))
3009 return TRUE;
3010
3011 /* Function pointer equality tests may require that STV_PROTECTED
3012 symbols be treated as dynamic symbols. If the address of a
3013 function not defined in an executable is set to that function's
3014 plt entry in the executable, then the address of the function in
3015 a shared library must also be the plt entry in the executable. */
3016 return local_protected;
3017 }
3018
3019 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3020 aligned. Returns the first TLS output section. */
3021
3022 struct bfd_section *
3023 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3024 {
3025 struct bfd_section *sec, *tls;
3026 unsigned int align = 0;
3027
3028 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3029 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3030 break;
3031 tls = sec;
3032
3033 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3034 if (sec->alignment_power > align)
3035 align = sec->alignment_power;
3036
3037 elf_hash_table (info)->tls_sec = tls;
3038
3039 /* Ensure the alignment of the first section is the largest alignment,
3040 so that the tls segment starts aligned. */
3041 if (tls != NULL)
3042 tls->alignment_power = align;
3043
3044 return tls;
3045 }
3046
3047 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3048 static bfd_boolean
3049 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3050 Elf_Internal_Sym *sym)
3051 {
3052 const struct elf_backend_data *bed;
3053
3054 /* Local symbols do not count, but target specific ones might. */
3055 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3056 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3057 return FALSE;
3058
3059 bed = get_elf_backend_data (abfd);
3060 /* Function symbols do not count. */
3061 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3062 return FALSE;
3063
3064 /* If the section is undefined, then so is the symbol. */
3065 if (sym->st_shndx == SHN_UNDEF)
3066 return FALSE;
3067
3068 /* If the symbol is defined in the common section, then
3069 it is a common definition and so does not count. */
3070 if (bed->common_definition (sym))
3071 return FALSE;
3072
3073 /* If the symbol is in a target specific section then we
3074 must rely upon the backend to tell us what it is. */
3075 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3076 /* FIXME - this function is not coded yet:
3077
3078 return _bfd_is_global_symbol_definition (abfd, sym);
3079
3080 Instead for now assume that the definition is not global,
3081 Even if this is wrong, at least the linker will behave
3082 in the same way that it used to do. */
3083 return FALSE;
3084
3085 return TRUE;
3086 }
3087
3088 /* Search the symbol table of the archive element of the archive ABFD
3089 whose archive map contains a mention of SYMDEF, and determine if
3090 the symbol is defined in this element. */
3091 static bfd_boolean
3092 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3093 {
3094 Elf_Internal_Shdr * hdr;
3095 bfd_size_type symcount;
3096 bfd_size_type extsymcount;
3097 bfd_size_type extsymoff;
3098 Elf_Internal_Sym *isymbuf;
3099 Elf_Internal_Sym *isym;
3100 Elf_Internal_Sym *isymend;
3101 bfd_boolean result;
3102
3103 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3104 if (abfd == NULL)
3105 return FALSE;
3106
3107 /* Return FALSE if the object has been claimed by plugin. */
3108 if (abfd->plugin_format == bfd_plugin_yes)
3109 return FALSE;
3110
3111 if (! bfd_check_format (abfd, bfd_object))
3112 return FALSE;
3113
3114 /* Select the appropriate symbol table. */
3115 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3116 hdr = &elf_tdata (abfd)->symtab_hdr;
3117 else
3118 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3119
3120 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3121
3122 /* The sh_info field of the symtab header tells us where the
3123 external symbols start. We don't care about the local symbols. */
3124 if (elf_bad_symtab (abfd))
3125 {
3126 extsymcount = symcount;
3127 extsymoff = 0;
3128 }
3129 else
3130 {
3131 extsymcount = symcount - hdr->sh_info;
3132 extsymoff = hdr->sh_info;
3133 }
3134
3135 if (extsymcount == 0)
3136 return FALSE;
3137
3138 /* Read in the symbol table. */
3139 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3140 NULL, NULL, NULL);
3141 if (isymbuf == NULL)
3142 return FALSE;
3143
3144 /* Scan the symbol table looking for SYMDEF. */
3145 result = FALSE;
3146 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3147 {
3148 const char *name;
3149
3150 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3151 isym->st_name);
3152 if (name == NULL)
3153 break;
3154
3155 if (strcmp (name, symdef->name) == 0)
3156 {
3157 result = is_global_data_symbol_definition (abfd, isym);
3158 break;
3159 }
3160 }
3161
3162 free (isymbuf);
3163
3164 return result;
3165 }
3166 \f
3167 /* Add an entry to the .dynamic table. */
3168
3169 bfd_boolean
3170 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3171 bfd_vma tag,
3172 bfd_vma val)
3173 {
3174 struct elf_link_hash_table *hash_table;
3175 const struct elf_backend_data *bed;
3176 asection *s;
3177 bfd_size_type newsize;
3178 bfd_byte *newcontents;
3179 Elf_Internal_Dyn dyn;
3180
3181 hash_table = elf_hash_table (info);
3182 if (! is_elf_hash_table (hash_table))
3183 return FALSE;
3184
3185 bed = get_elf_backend_data (hash_table->dynobj);
3186 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3187 BFD_ASSERT (s != NULL);
3188
3189 newsize = s->size + bed->s->sizeof_dyn;
3190 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3191 if (newcontents == NULL)
3192 return FALSE;
3193
3194 dyn.d_tag = tag;
3195 dyn.d_un.d_val = val;
3196 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3197
3198 s->size = newsize;
3199 s->contents = newcontents;
3200
3201 return TRUE;
3202 }
3203
3204 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3205 otherwise just check whether one already exists. Returns -1 on error,
3206 1 if a DT_NEEDED tag already exists, and 0 on success. */
3207
3208 static int
3209 elf_add_dt_needed_tag (bfd *abfd,
3210 struct bfd_link_info *info,
3211 const char *soname,
3212 bfd_boolean do_it)
3213 {
3214 struct elf_link_hash_table *hash_table;
3215 bfd_size_type strindex;
3216
3217 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3218 return -1;
3219
3220 hash_table = elf_hash_table (info);
3221 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3222 if (strindex == (bfd_size_type) -1)
3223 return -1;
3224
3225 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3226 {
3227 asection *sdyn;
3228 const struct elf_backend_data *bed;
3229 bfd_byte *extdyn;
3230
3231 bed = get_elf_backend_data (hash_table->dynobj);
3232 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3233 if (sdyn != NULL)
3234 for (extdyn = sdyn->contents;
3235 extdyn < sdyn->contents + sdyn->size;
3236 extdyn += bed->s->sizeof_dyn)
3237 {
3238 Elf_Internal_Dyn dyn;
3239
3240 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3241 if (dyn.d_tag == DT_NEEDED
3242 && dyn.d_un.d_val == strindex)
3243 {
3244 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3245 return 1;
3246 }
3247 }
3248 }
3249
3250 if (do_it)
3251 {
3252 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3253 return -1;
3254
3255 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3256 return -1;
3257 }
3258 else
3259 /* We were just checking for existence of the tag. */
3260 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3261
3262 return 0;
3263 }
3264
3265 /* Return true if SONAME is on the needed list between NEEDED and STOP
3266 (or the end of list if STOP is NULL), and needed by a library that
3267 will be loaded. */
3268
3269 static bfd_boolean
3270 on_needed_list (const char *soname,
3271 struct bfd_link_needed_list *needed,
3272 struct bfd_link_needed_list *stop)
3273 {
3274 struct bfd_link_needed_list *look;
3275 for (look = needed; look != stop; look = look->next)
3276 if (strcmp (soname, look->name) == 0
3277 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3278 /* If needed by a library that itself is not directly
3279 needed, recursively check whether that library is
3280 indirectly needed. Since we add DT_NEEDED entries to
3281 the end of the list, library dependencies appear after
3282 the library. Therefore search prior to the current
3283 LOOK, preventing possible infinite recursion. */
3284 || on_needed_list (elf_dt_name (look->by), needed, look)))
3285 return TRUE;
3286
3287 return FALSE;
3288 }
3289
3290 /* Sort symbol by value, section, and size. */
3291 static int
3292 elf_sort_symbol (const void *arg1, const void *arg2)
3293 {
3294 const struct elf_link_hash_entry *h1;
3295 const struct elf_link_hash_entry *h2;
3296 bfd_signed_vma vdiff;
3297
3298 h1 = *(const struct elf_link_hash_entry **) arg1;
3299 h2 = *(const struct elf_link_hash_entry **) arg2;
3300 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3301 if (vdiff != 0)
3302 return vdiff > 0 ? 1 : -1;
3303 else
3304 {
3305 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3306 if (sdiff != 0)
3307 return sdiff > 0 ? 1 : -1;
3308 }
3309 vdiff = h1->size - h2->size;
3310 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3311 }
3312
3313 /* This function is used to adjust offsets into .dynstr for
3314 dynamic symbols. This is called via elf_link_hash_traverse. */
3315
3316 static bfd_boolean
3317 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3318 {
3319 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3320
3321 if (h->dynindx != -1)
3322 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3323 return TRUE;
3324 }
3325
3326 /* Assign string offsets in .dynstr, update all structures referencing
3327 them. */
3328
3329 static bfd_boolean
3330 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3331 {
3332 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3333 struct elf_link_local_dynamic_entry *entry;
3334 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3335 bfd *dynobj = hash_table->dynobj;
3336 asection *sdyn;
3337 bfd_size_type size;
3338 const struct elf_backend_data *bed;
3339 bfd_byte *extdyn;
3340
3341 _bfd_elf_strtab_finalize (dynstr);
3342 size = _bfd_elf_strtab_size (dynstr);
3343
3344 bed = get_elf_backend_data (dynobj);
3345 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3346 BFD_ASSERT (sdyn != NULL);
3347
3348 /* Update all .dynamic entries referencing .dynstr strings. */
3349 for (extdyn = sdyn->contents;
3350 extdyn < sdyn->contents + sdyn->size;
3351 extdyn += bed->s->sizeof_dyn)
3352 {
3353 Elf_Internal_Dyn dyn;
3354
3355 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3356 switch (dyn.d_tag)
3357 {
3358 case DT_STRSZ:
3359 dyn.d_un.d_val = size;
3360 break;
3361 case DT_NEEDED:
3362 case DT_SONAME:
3363 case DT_RPATH:
3364 case DT_RUNPATH:
3365 case DT_FILTER:
3366 case DT_AUXILIARY:
3367 case DT_AUDIT:
3368 case DT_DEPAUDIT:
3369 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3370 break;
3371 default:
3372 continue;
3373 }
3374 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3375 }
3376
3377 /* Now update local dynamic symbols. */
3378 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3379 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3380 entry->isym.st_name);
3381
3382 /* And the rest of dynamic symbols. */
3383 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3384
3385 /* Adjust version definitions. */
3386 if (elf_tdata (output_bfd)->cverdefs)
3387 {
3388 asection *s;
3389 bfd_byte *p;
3390 bfd_size_type i;
3391 Elf_Internal_Verdef def;
3392 Elf_Internal_Verdaux defaux;
3393
3394 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3395 p = s->contents;
3396 do
3397 {
3398 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3399 &def);
3400 p += sizeof (Elf_External_Verdef);
3401 if (def.vd_aux != sizeof (Elf_External_Verdef))
3402 continue;
3403 for (i = 0; i < def.vd_cnt; ++i)
3404 {
3405 _bfd_elf_swap_verdaux_in (output_bfd,
3406 (Elf_External_Verdaux *) p, &defaux);
3407 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3408 defaux.vda_name);
3409 _bfd_elf_swap_verdaux_out (output_bfd,
3410 &defaux, (Elf_External_Verdaux *) p);
3411 p += sizeof (Elf_External_Verdaux);
3412 }
3413 }
3414 while (def.vd_next);
3415 }
3416
3417 /* Adjust version references. */
3418 if (elf_tdata (output_bfd)->verref)
3419 {
3420 asection *s;
3421 bfd_byte *p;
3422 bfd_size_type i;
3423 Elf_Internal_Verneed need;
3424 Elf_Internal_Vernaux needaux;
3425
3426 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3427 p = s->contents;
3428 do
3429 {
3430 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3431 &need);
3432 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3433 _bfd_elf_swap_verneed_out (output_bfd, &need,
3434 (Elf_External_Verneed *) p);
3435 p += sizeof (Elf_External_Verneed);
3436 for (i = 0; i < need.vn_cnt; ++i)
3437 {
3438 _bfd_elf_swap_vernaux_in (output_bfd,
3439 (Elf_External_Vernaux *) p, &needaux);
3440 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3441 needaux.vna_name);
3442 _bfd_elf_swap_vernaux_out (output_bfd,
3443 &needaux,
3444 (Elf_External_Vernaux *) p);
3445 p += sizeof (Elf_External_Vernaux);
3446 }
3447 }
3448 while (need.vn_next);
3449 }
3450
3451 return TRUE;
3452 }
3453 \f
3454 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3455 The default is to only match when the INPUT and OUTPUT are exactly
3456 the same target. */
3457
3458 bfd_boolean
3459 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3460 const bfd_target *output)
3461 {
3462 return input == output;
3463 }
3464
3465 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3466 This version is used when different targets for the same architecture
3467 are virtually identical. */
3468
3469 bfd_boolean
3470 _bfd_elf_relocs_compatible (const bfd_target *input,
3471 const bfd_target *output)
3472 {
3473 const struct elf_backend_data *obed, *ibed;
3474
3475 if (input == output)
3476 return TRUE;
3477
3478 ibed = xvec_get_elf_backend_data (input);
3479 obed = xvec_get_elf_backend_data (output);
3480
3481 if (ibed->arch != obed->arch)
3482 return FALSE;
3483
3484 /* If both backends are using this function, deem them compatible. */
3485 return ibed->relocs_compatible == obed->relocs_compatible;
3486 }
3487
3488 /* Make a special call to the linker "notice" function to tell it that
3489 we are about to handle an as-needed lib, or have finished
3490 processing the lib. */
3491
3492 bfd_boolean
3493 _bfd_elf_notice_as_needed (bfd *ibfd,
3494 struct bfd_link_info *info,
3495 enum notice_asneeded_action act)
3496 {
3497 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3498 }
3499
3500 /* Check relocations an ELF object file. */
3501
3502 bfd_boolean
3503 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3504 {
3505 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3506 struct elf_link_hash_table *htab = elf_hash_table (info);
3507
3508 /* If this object is the same format as the output object, and it is
3509 not a shared library, then let the backend look through the
3510 relocs.
3511
3512 This is required to build global offset table entries and to
3513 arrange for dynamic relocs. It is not required for the
3514 particular common case of linking non PIC code, even when linking
3515 against shared libraries, but unfortunately there is no way of
3516 knowing whether an object file has been compiled PIC or not.
3517 Looking through the relocs is not particularly time consuming.
3518 The problem is that we must either (1) keep the relocs in memory,
3519 which causes the linker to require additional runtime memory or
3520 (2) read the relocs twice from the input file, which wastes time.
3521 This would be a good case for using mmap.
3522
3523 I have no idea how to handle linking PIC code into a file of a
3524 different format. It probably can't be done. */
3525 if ((abfd->flags & DYNAMIC) == 0
3526 && is_elf_hash_table (htab)
3527 && bed->check_relocs != NULL
3528 && elf_object_id (abfd) == elf_hash_table_id (htab)
3529 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3530 {
3531 asection *o;
3532
3533 for (o = abfd->sections; o != NULL; o = o->next)
3534 {
3535 Elf_Internal_Rela *internal_relocs;
3536 bfd_boolean ok;
3537
3538 /* Don't check relocations in excluded sections. */
3539 if ((o->flags & SEC_RELOC) == 0
3540 || (o->flags & SEC_EXCLUDE) != 0
3541 || o->reloc_count == 0
3542 || ((info->strip == strip_all || info->strip == strip_debugger)
3543 && (o->flags & SEC_DEBUGGING) != 0)
3544 || bfd_is_abs_section (o->output_section))
3545 continue;
3546
3547 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3548 info->keep_memory);
3549 if (internal_relocs == NULL)
3550 return FALSE;
3551
3552 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3553
3554 if (elf_section_data (o)->relocs != internal_relocs)
3555 free (internal_relocs);
3556
3557 if (! ok)
3558 return FALSE;
3559 }
3560 }
3561
3562 return TRUE;
3563 }
3564
3565 /* Add symbols from an ELF object file to the linker hash table. */
3566
3567 static bfd_boolean
3568 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3569 {
3570 Elf_Internal_Ehdr *ehdr;
3571 Elf_Internal_Shdr *hdr;
3572 bfd_size_type symcount;
3573 bfd_size_type extsymcount;
3574 bfd_size_type extsymoff;
3575 struct elf_link_hash_entry **sym_hash;
3576 bfd_boolean dynamic;
3577 Elf_External_Versym *extversym = NULL;
3578 Elf_External_Versym *ever;
3579 struct elf_link_hash_entry *weaks;
3580 struct elf_link_hash_entry **nondeflt_vers = NULL;
3581 bfd_size_type nondeflt_vers_cnt = 0;
3582 Elf_Internal_Sym *isymbuf = NULL;
3583 Elf_Internal_Sym *isym;
3584 Elf_Internal_Sym *isymend;
3585 const struct elf_backend_data *bed;
3586 bfd_boolean add_needed;
3587 struct elf_link_hash_table *htab;
3588 bfd_size_type amt;
3589 void *alloc_mark = NULL;
3590 struct bfd_hash_entry **old_table = NULL;
3591 unsigned int old_size = 0;
3592 unsigned int old_count = 0;
3593 void *old_tab = NULL;
3594 void *old_ent;
3595 struct bfd_link_hash_entry *old_undefs = NULL;
3596 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3597 long old_dynsymcount = 0;
3598 bfd_size_type old_dynstr_size = 0;
3599 size_t tabsize = 0;
3600 asection *s;
3601 bfd_boolean just_syms;
3602
3603 htab = elf_hash_table (info);
3604 bed = get_elf_backend_data (abfd);
3605
3606 if ((abfd->flags & DYNAMIC) == 0)
3607 dynamic = FALSE;
3608 else
3609 {
3610 dynamic = TRUE;
3611
3612 /* You can't use -r against a dynamic object. Also, there's no
3613 hope of using a dynamic object which does not exactly match
3614 the format of the output file. */
3615 if (bfd_link_relocatable (info)
3616 || !is_elf_hash_table (htab)
3617 || info->output_bfd->xvec != abfd->xvec)
3618 {
3619 if (bfd_link_relocatable (info))
3620 bfd_set_error (bfd_error_invalid_operation);
3621 else
3622 bfd_set_error (bfd_error_wrong_format);
3623 goto error_return;
3624 }
3625 }
3626
3627 ehdr = elf_elfheader (abfd);
3628 if (info->warn_alternate_em
3629 && bed->elf_machine_code != ehdr->e_machine
3630 && ((bed->elf_machine_alt1 != 0
3631 && ehdr->e_machine == bed->elf_machine_alt1)
3632 || (bed->elf_machine_alt2 != 0
3633 && ehdr->e_machine == bed->elf_machine_alt2)))
3634 info->callbacks->einfo
3635 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3636 ehdr->e_machine, abfd, bed->elf_machine_code);
3637
3638 /* As a GNU extension, any input sections which are named
3639 .gnu.warning.SYMBOL are treated as warning symbols for the given
3640 symbol. This differs from .gnu.warning sections, which generate
3641 warnings when they are included in an output file. */
3642 /* PR 12761: Also generate this warning when building shared libraries. */
3643 for (s = abfd->sections; s != NULL; s = s->next)
3644 {
3645 const char *name;
3646
3647 name = bfd_get_section_name (abfd, s);
3648 if (CONST_STRNEQ (name, ".gnu.warning."))
3649 {
3650 char *msg;
3651 bfd_size_type sz;
3652
3653 name += sizeof ".gnu.warning." - 1;
3654
3655 /* If this is a shared object, then look up the symbol
3656 in the hash table. If it is there, and it is already
3657 been defined, then we will not be using the entry
3658 from this shared object, so we don't need to warn.
3659 FIXME: If we see the definition in a regular object
3660 later on, we will warn, but we shouldn't. The only
3661 fix is to keep track of what warnings we are supposed
3662 to emit, and then handle them all at the end of the
3663 link. */
3664 if (dynamic)
3665 {
3666 struct elf_link_hash_entry *h;
3667
3668 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3669
3670 /* FIXME: What about bfd_link_hash_common? */
3671 if (h != NULL
3672 && (h->root.type == bfd_link_hash_defined
3673 || h->root.type == bfd_link_hash_defweak))
3674 continue;
3675 }
3676
3677 sz = s->size;
3678 msg = (char *) bfd_alloc (abfd, sz + 1);
3679 if (msg == NULL)
3680 goto error_return;
3681
3682 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3683 goto error_return;
3684
3685 msg[sz] = '\0';
3686
3687 if (! (_bfd_generic_link_add_one_symbol
3688 (info, abfd, name, BSF_WARNING, s, 0, msg,
3689 FALSE, bed->collect, NULL)))
3690 goto error_return;
3691
3692 if (bfd_link_executable (info))
3693 {
3694 /* Clobber the section size so that the warning does
3695 not get copied into the output file. */
3696 s->size = 0;
3697
3698 /* Also set SEC_EXCLUDE, so that symbols defined in
3699 the warning section don't get copied to the output. */
3700 s->flags |= SEC_EXCLUDE;
3701 }
3702 }
3703 }
3704
3705 just_syms = ((s = abfd->sections) != NULL
3706 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3707
3708 add_needed = TRUE;
3709 if (! dynamic)
3710 {
3711 /* If we are creating a shared library, create all the dynamic
3712 sections immediately. We need to attach them to something,
3713 so we attach them to this BFD, provided it is the right
3714 format and is not from ld --just-symbols. Always create the
3715 dynamic sections for -E/--dynamic-list. FIXME: If there
3716 are no input BFD's of the same format as the output, we can't
3717 make a shared library. */
3718 if (!just_syms
3719 && (bfd_link_pic (info)
3720 || (!bfd_link_relocatable (info)
3721 && (info->export_dynamic || info->dynamic)))
3722 && is_elf_hash_table (htab)
3723 && info->output_bfd->xvec == abfd->xvec
3724 && !htab->dynamic_sections_created)
3725 {
3726 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3727 goto error_return;
3728 }
3729 }
3730 else if (!is_elf_hash_table (htab))
3731 goto error_return;
3732 else
3733 {
3734 const char *soname = NULL;
3735 char *audit = NULL;
3736 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3737 int ret;
3738
3739 /* ld --just-symbols and dynamic objects don't mix very well.
3740 ld shouldn't allow it. */
3741 if (just_syms)
3742 abort ();
3743
3744 /* If this dynamic lib was specified on the command line with
3745 --as-needed in effect, then we don't want to add a DT_NEEDED
3746 tag unless the lib is actually used. Similary for libs brought
3747 in by another lib's DT_NEEDED. When --no-add-needed is used
3748 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3749 any dynamic library in DT_NEEDED tags in the dynamic lib at
3750 all. */
3751 add_needed = (elf_dyn_lib_class (abfd)
3752 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3753 | DYN_NO_NEEDED)) == 0;
3754
3755 s = bfd_get_section_by_name (abfd, ".dynamic");
3756 if (s != NULL)
3757 {
3758 bfd_byte *dynbuf;
3759 bfd_byte *extdyn;
3760 unsigned int elfsec;
3761 unsigned long shlink;
3762
3763 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3764 {
3765 error_free_dyn:
3766 free (dynbuf);
3767 goto error_return;
3768 }
3769
3770 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3771 if (elfsec == SHN_BAD)
3772 goto error_free_dyn;
3773 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3774
3775 for (extdyn = dynbuf;
3776 extdyn < dynbuf + s->size;
3777 extdyn += bed->s->sizeof_dyn)
3778 {
3779 Elf_Internal_Dyn dyn;
3780
3781 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3782 if (dyn.d_tag == DT_SONAME)
3783 {
3784 unsigned int tagv = dyn.d_un.d_val;
3785 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3786 if (soname == NULL)
3787 goto error_free_dyn;
3788 }
3789 if (dyn.d_tag == DT_NEEDED)
3790 {
3791 struct bfd_link_needed_list *n, **pn;
3792 char *fnm, *anm;
3793 unsigned int tagv = dyn.d_un.d_val;
3794
3795 amt = sizeof (struct bfd_link_needed_list);
3796 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3797 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3798 if (n == NULL || fnm == NULL)
3799 goto error_free_dyn;
3800 amt = strlen (fnm) + 1;
3801 anm = (char *) bfd_alloc (abfd, amt);
3802 if (anm == NULL)
3803 goto error_free_dyn;
3804 memcpy (anm, fnm, amt);
3805 n->name = anm;
3806 n->by = abfd;
3807 n->next = NULL;
3808 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3809 ;
3810 *pn = n;
3811 }
3812 if (dyn.d_tag == DT_RUNPATH)
3813 {
3814 struct bfd_link_needed_list *n, **pn;
3815 char *fnm, *anm;
3816 unsigned int tagv = dyn.d_un.d_val;
3817
3818 amt = sizeof (struct bfd_link_needed_list);
3819 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3820 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3821 if (n == NULL || fnm == NULL)
3822 goto error_free_dyn;
3823 amt = strlen (fnm) + 1;
3824 anm = (char *) bfd_alloc (abfd, amt);
3825 if (anm == NULL)
3826 goto error_free_dyn;
3827 memcpy (anm, fnm, amt);
3828 n->name = anm;
3829 n->by = abfd;
3830 n->next = NULL;
3831 for (pn = & runpath;
3832 *pn != NULL;
3833 pn = &(*pn)->next)
3834 ;
3835 *pn = n;
3836 }
3837 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3838 if (!runpath && dyn.d_tag == DT_RPATH)
3839 {
3840 struct bfd_link_needed_list *n, **pn;
3841 char *fnm, *anm;
3842 unsigned int tagv = dyn.d_un.d_val;
3843
3844 amt = sizeof (struct bfd_link_needed_list);
3845 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3846 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3847 if (n == NULL || fnm == NULL)
3848 goto error_free_dyn;
3849 amt = strlen (fnm) + 1;
3850 anm = (char *) bfd_alloc (abfd, amt);
3851 if (anm == NULL)
3852 goto error_free_dyn;
3853 memcpy (anm, fnm, amt);
3854 n->name = anm;
3855 n->by = abfd;
3856 n->next = NULL;
3857 for (pn = & rpath;
3858 *pn != NULL;
3859 pn = &(*pn)->next)
3860 ;
3861 *pn = n;
3862 }
3863 if (dyn.d_tag == DT_AUDIT)
3864 {
3865 unsigned int tagv = dyn.d_un.d_val;
3866 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3867 }
3868 }
3869
3870 free (dynbuf);
3871 }
3872
3873 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3874 frees all more recently bfd_alloc'd blocks as well. */
3875 if (runpath)
3876 rpath = runpath;
3877
3878 if (rpath)
3879 {
3880 struct bfd_link_needed_list **pn;
3881 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3882 ;
3883 *pn = rpath;
3884 }
3885
3886 /* We do not want to include any of the sections in a dynamic
3887 object in the output file. We hack by simply clobbering the
3888 list of sections in the BFD. This could be handled more
3889 cleanly by, say, a new section flag; the existing
3890 SEC_NEVER_LOAD flag is not the one we want, because that one
3891 still implies that the section takes up space in the output
3892 file. */
3893 bfd_section_list_clear (abfd);
3894
3895 /* Find the name to use in a DT_NEEDED entry that refers to this
3896 object. If the object has a DT_SONAME entry, we use it.
3897 Otherwise, if the generic linker stuck something in
3898 elf_dt_name, we use that. Otherwise, we just use the file
3899 name. */
3900 if (soname == NULL || *soname == '\0')
3901 {
3902 soname = elf_dt_name (abfd);
3903 if (soname == NULL || *soname == '\0')
3904 soname = bfd_get_filename (abfd);
3905 }
3906
3907 /* Save the SONAME because sometimes the linker emulation code
3908 will need to know it. */
3909 elf_dt_name (abfd) = soname;
3910
3911 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3912 if (ret < 0)
3913 goto error_return;
3914
3915 /* If we have already included this dynamic object in the
3916 link, just ignore it. There is no reason to include a
3917 particular dynamic object more than once. */
3918 if (ret > 0)
3919 return TRUE;
3920
3921 /* Save the DT_AUDIT entry for the linker emulation code. */
3922 elf_dt_audit (abfd) = audit;
3923 }
3924
3925 /* If this is a dynamic object, we always link against the .dynsym
3926 symbol table, not the .symtab symbol table. The dynamic linker
3927 will only see the .dynsym symbol table, so there is no reason to
3928 look at .symtab for a dynamic object. */
3929
3930 if (! dynamic || elf_dynsymtab (abfd) == 0)
3931 hdr = &elf_tdata (abfd)->symtab_hdr;
3932 else
3933 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3934
3935 symcount = hdr->sh_size / bed->s->sizeof_sym;
3936
3937 /* The sh_info field of the symtab header tells us where the
3938 external symbols start. We don't care about the local symbols at
3939 this point. */
3940 if (elf_bad_symtab (abfd))
3941 {
3942 extsymcount = symcount;
3943 extsymoff = 0;
3944 }
3945 else
3946 {
3947 extsymcount = symcount - hdr->sh_info;
3948 extsymoff = hdr->sh_info;
3949 }
3950
3951 sym_hash = elf_sym_hashes (abfd);
3952 if (extsymcount != 0)
3953 {
3954 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3955 NULL, NULL, NULL);
3956 if (isymbuf == NULL)
3957 goto error_return;
3958
3959 if (sym_hash == NULL)
3960 {
3961 /* We store a pointer to the hash table entry for each
3962 external symbol. */
3963 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3964 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
3965 if (sym_hash == NULL)
3966 goto error_free_sym;
3967 elf_sym_hashes (abfd) = sym_hash;
3968 }
3969 }
3970
3971 if (dynamic)
3972 {
3973 /* Read in any version definitions. */
3974 if (!_bfd_elf_slurp_version_tables (abfd,
3975 info->default_imported_symver))
3976 goto error_free_sym;
3977
3978 /* Read in the symbol versions, but don't bother to convert them
3979 to internal format. */
3980 if (elf_dynversym (abfd) != 0)
3981 {
3982 Elf_Internal_Shdr *versymhdr;
3983
3984 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3985 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
3986 if (extversym == NULL)
3987 goto error_free_sym;
3988 amt = versymhdr->sh_size;
3989 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3990 || bfd_bread (extversym, amt, abfd) != amt)
3991 goto error_free_vers;
3992 }
3993 }
3994
3995 /* If we are loading an as-needed shared lib, save the symbol table
3996 state before we start adding symbols. If the lib turns out
3997 to be unneeded, restore the state. */
3998 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3999 {
4000 unsigned int i;
4001 size_t entsize;
4002
4003 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4004 {
4005 struct bfd_hash_entry *p;
4006 struct elf_link_hash_entry *h;
4007
4008 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4009 {
4010 h = (struct elf_link_hash_entry *) p;
4011 entsize += htab->root.table.entsize;
4012 if (h->root.type == bfd_link_hash_warning)
4013 entsize += htab->root.table.entsize;
4014 }
4015 }
4016
4017 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4018 old_tab = bfd_malloc (tabsize + entsize);
4019 if (old_tab == NULL)
4020 goto error_free_vers;
4021
4022 /* Remember the current objalloc pointer, so that all mem for
4023 symbols added can later be reclaimed. */
4024 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4025 if (alloc_mark == NULL)
4026 goto error_free_vers;
4027
4028 /* Make a special call to the linker "notice" function to
4029 tell it that we are about to handle an as-needed lib. */
4030 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4031 goto error_free_vers;
4032
4033 /* Clone the symbol table. Remember some pointers into the
4034 symbol table, and dynamic symbol count. */
4035 old_ent = (char *) old_tab + tabsize;
4036 memcpy (old_tab, htab->root.table.table, tabsize);
4037 old_undefs = htab->root.undefs;
4038 old_undefs_tail = htab->root.undefs_tail;
4039 old_table = htab->root.table.table;
4040 old_size = htab->root.table.size;
4041 old_count = htab->root.table.count;
4042 old_dynsymcount = htab->dynsymcount;
4043 old_dynstr_size = _bfd_elf_strtab_size (htab->dynstr);
4044
4045 for (i = 0; i < htab->root.table.size; i++)
4046 {
4047 struct bfd_hash_entry *p;
4048 struct elf_link_hash_entry *h;
4049
4050 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4051 {
4052 memcpy (old_ent, p, htab->root.table.entsize);
4053 old_ent = (char *) old_ent + htab->root.table.entsize;
4054 h = (struct elf_link_hash_entry *) p;
4055 if (h->root.type == bfd_link_hash_warning)
4056 {
4057 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4058 old_ent = (char *) old_ent + htab->root.table.entsize;
4059 }
4060 }
4061 }
4062 }
4063
4064 weaks = NULL;
4065 ever = extversym != NULL ? extversym + extsymoff : NULL;
4066 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4067 isym < isymend;
4068 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4069 {
4070 int bind;
4071 bfd_vma value;
4072 asection *sec, *new_sec;
4073 flagword flags;
4074 const char *name;
4075 struct elf_link_hash_entry *h;
4076 struct elf_link_hash_entry *hi;
4077 bfd_boolean definition;
4078 bfd_boolean size_change_ok;
4079 bfd_boolean type_change_ok;
4080 bfd_boolean new_weakdef;
4081 bfd_boolean new_weak;
4082 bfd_boolean old_weak;
4083 bfd_boolean override;
4084 bfd_boolean common;
4085 bfd_boolean discarded;
4086 unsigned int old_alignment;
4087 bfd *old_bfd;
4088 bfd_boolean matched;
4089
4090 override = FALSE;
4091
4092 flags = BSF_NO_FLAGS;
4093 sec = NULL;
4094 value = isym->st_value;
4095 common = bed->common_definition (isym);
4096 discarded = FALSE;
4097
4098 bind = ELF_ST_BIND (isym->st_info);
4099 switch (bind)
4100 {
4101 case STB_LOCAL:
4102 /* This should be impossible, since ELF requires that all
4103 global symbols follow all local symbols, and that sh_info
4104 point to the first global symbol. Unfortunately, Irix 5
4105 screws this up. */
4106 continue;
4107
4108 case STB_GLOBAL:
4109 if (isym->st_shndx != SHN_UNDEF && !common)
4110 flags = BSF_GLOBAL;
4111 break;
4112
4113 case STB_WEAK:
4114 flags = BSF_WEAK;
4115 break;
4116
4117 case STB_GNU_UNIQUE:
4118 flags = BSF_GNU_UNIQUE;
4119 break;
4120
4121 default:
4122 /* Leave it up to the processor backend. */
4123 break;
4124 }
4125
4126 if (isym->st_shndx == SHN_UNDEF)
4127 sec = bfd_und_section_ptr;
4128 else if (isym->st_shndx == SHN_ABS)
4129 sec = bfd_abs_section_ptr;
4130 else if (isym->st_shndx == SHN_COMMON)
4131 {
4132 sec = bfd_com_section_ptr;
4133 /* What ELF calls the size we call the value. What ELF
4134 calls the value we call the alignment. */
4135 value = isym->st_size;
4136 }
4137 else
4138 {
4139 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4140 if (sec == NULL)
4141 sec = bfd_abs_section_ptr;
4142 else if (discarded_section (sec))
4143 {
4144 /* Symbols from discarded section are undefined. We keep
4145 its visibility. */
4146 sec = bfd_und_section_ptr;
4147 discarded = TRUE;
4148 isym->st_shndx = SHN_UNDEF;
4149 }
4150 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4151 value -= sec->vma;
4152 }
4153
4154 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4155 isym->st_name);
4156 if (name == NULL)
4157 goto error_free_vers;
4158
4159 if (isym->st_shndx == SHN_COMMON
4160 && (abfd->flags & BFD_PLUGIN) != 0)
4161 {
4162 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4163
4164 if (xc == NULL)
4165 {
4166 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4167 | SEC_EXCLUDE);
4168 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4169 if (xc == NULL)
4170 goto error_free_vers;
4171 }
4172 sec = xc;
4173 }
4174 else if (isym->st_shndx == SHN_COMMON
4175 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4176 && !bfd_link_relocatable (info))
4177 {
4178 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4179
4180 if (tcomm == NULL)
4181 {
4182 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4183 | SEC_LINKER_CREATED);
4184 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4185 if (tcomm == NULL)
4186 goto error_free_vers;
4187 }
4188 sec = tcomm;
4189 }
4190 else if (bed->elf_add_symbol_hook)
4191 {
4192 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4193 &sec, &value))
4194 goto error_free_vers;
4195
4196 /* The hook function sets the name to NULL if this symbol
4197 should be skipped for some reason. */
4198 if (name == NULL)
4199 continue;
4200 }
4201
4202 /* Sanity check that all possibilities were handled. */
4203 if (sec == NULL)
4204 {
4205 bfd_set_error (bfd_error_bad_value);
4206 goto error_free_vers;
4207 }
4208
4209 /* Silently discard TLS symbols from --just-syms. There's
4210 no way to combine a static TLS block with a new TLS block
4211 for this executable. */
4212 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4213 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4214 continue;
4215
4216 if (bfd_is_und_section (sec)
4217 || bfd_is_com_section (sec))
4218 definition = FALSE;
4219 else
4220 definition = TRUE;
4221
4222 size_change_ok = FALSE;
4223 type_change_ok = bed->type_change_ok;
4224 old_weak = FALSE;
4225 matched = FALSE;
4226 old_alignment = 0;
4227 old_bfd = NULL;
4228 new_sec = sec;
4229
4230 if (is_elf_hash_table (htab))
4231 {
4232 Elf_Internal_Versym iver;
4233 unsigned int vernum = 0;
4234 bfd_boolean skip;
4235
4236 if (ever == NULL)
4237 {
4238 if (info->default_imported_symver)
4239 /* Use the default symbol version created earlier. */
4240 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4241 else
4242 iver.vs_vers = 0;
4243 }
4244 else
4245 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4246
4247 vernum = iver.vs_vers & VERSYM_VERSION;
4248
4249 /* If this is a hidden symbol, or if it is not version
4250 1, we append the version name to the symbol name.
4251 However, we do not modify a non-hidden absolute symbol
4252 if it is not a function, because it might be the version
4253 symbol itself. FIXME: What if it isn't? */
4254 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4255 || (vernum > 1
4256 && (!bfd_is_abs_section (sec)
4257 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4258 {
4259 const char *verstr;
4260 size_t namelen, verlen, newlen;
4261 char *newname, *p;
4262
4263 if (isym->st_shndx != SHN_UNDEF)
4264 {
4265 if (vernum > elf_tdata (abfd)->cverdefs)
4266 verstr = NULL;
4267 else if (vernum > 1)
4268 verstr =
4269 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4270 else
4271 verstr = "";
4272
4273 if (verstr == NULL)
4274 {
4275 (*_bfd_error_handler)
4276 (_("%B: %s: invalid version %u (max %d)"),
4277 abfd, name, vernum,
4278 elf_tdata (abfd)->cverdefs);
4279 bfd_set_error (bfd_error_bad_value);
4280 goto error_free_vers;
4281 }
4282 }
4283 else
4284 {
4285 /* We cannot simply test for the number of
4286 entries in the VERNEED section since the
4287 numbers for the needed versions do not start
4288 at 0. */
4289 Elf_Internal_Verneed *t;
4290
4291 verstr = NULL;
4292 for (t = elf_tdata (abfd)->verref;
4293 t != NULL;
4294 t = t->vn_nextref)
4295 {
4296 Elf_Internal_Vernaux *a;
4297
4298 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4299 {
4300 if (a->vna_other == vernum)
4301 {
4302 verstr = a->vna_nodename;
4303 break;
4304 }
4305 }
4306 if (a != NULL)
4307 break;
4308 }
4309 if (verstr == NULL)
4310 {
4311 (*_bfd_error_handler)
4312 (_("%B: %s: invalid needed version %d"),
4313 abfd, name, vernum);
4314 bfd_set_error (bfd_error_bad_value);
4315 goto error_free_vers;
4316 }
4317 }
4318
4319 namelen = strlen (name);
4320 verlen = strlen (verstr);
4321 newlen = namelen + verlen + 2;
4322 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4323 && isym->st_shndx != SHN_UNDEF)
4324 ++newlen;
4325
4326 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4327 if (newname == NULL)
4328 goto error_free_vers;
4329 memcpy (newname, name, namelen);
4330 p = newname + namelen;
4331 *p++ = ELF_VER_CHR;
4332 /* If this is a defined non-hidden version symbol,
4333 we add another @ to the name. This indicates the
4334 default version of the symbol. */
4335 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4336 && isym->st_shndx != SHN_UNDEF)
4337 *p++ = ELF_VER_CHR;
4338 memcpy (p, verstr, verlen + 1);
4339
4340 name = newname;
4341 }
4342
4343 /* If this symbol has default visibility and the user has
4344 requested we not re-export it, then mark it as hidden. */
4345 if (!bfd_is_und_section (sec)
4346 && !dynamic
4347 && abfd->no_export
4348 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4349 isym->st_other = (STV_HIDDEN
4350 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4351
4352 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4353 sym_hash, &old_bfd, &old_weak,
4354 &old_alignment, &skip, &override,
4355 &type_change_ok, &size_change_ok,
4356 &matched))
4357 goto error_free_vers;
4358
4359 if (skip)
4360 continue;
4361
4362 /* Override a definition only if the new symbol matches the
4363 existing one. */
4364 if (override && matched)
4365 definition = FALSE;
4366
4367 h = *sym_hash;
4368 while (h->root.type == bfd_link_hash_indirect
4369 || h->root.type == bfd_link_hash_warning)
4370 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4371
4372 if (elf_tdata (abfd)->verdef != NULL
4373 && vernum > 1
4374 && definition)
4375 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4376 }
4377
4378 if (! (_bfd_generic_link_add_one_symbol
4379 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4380 (struct bfd_link_hash_entry **) sym_hash)))
4381 goto error_free_vers;
4382
4383 h = *sym_hash;
4384 /* We need to make sure that indirect symbol dynamic flags are
4385 updated. */
4386 hi = h;
4387 while (h->root.type == bfd_link_hash_indirect
4388 || h->root.type == bfd_link_hash_warning)
4389 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4390
4391 /* Setting the index to -3 tells elf_link_output_extsym that
4392 this symbol is defined in a discarded section. */
4393 if (discarded)
4394 h->indx = -3;
4395
4396 *sym_hash = h;
4397
4398 new_weak = (flags & BSF_WEAK) != 0;
4399 new_weakdef = FALSE;
4400 if (dynamic
4401 && definition
4402 && new_weak
4403 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4404 && is_elf_hash_table (htab)
4405 && h->u.weakdef == NULL)
4406 {
4407 /* Keep a list of all weak defined non function symbols from
4408 a dynamic object, using the weakdef field. Later in this
4409 function we will set the weakdef field to the correct
4410 value. We only put non-function symbols from dynamic
4411 objects on this list, because that happens to be the only
4412 time we need to know the normal symbol corresponding to a
4413 weak symbol, and the information is time consuming to
4414 figure out. If the weakdef field is not already NULL,
4415 then this symbol was already defined by some previous
4416 dynamic object, and we will be using that previous
4417 definition anyhow. */
4418
4419 h->u.weakdef = weaks;
4420 weaks = h;
4421 new_weakdef = TRUE;
4422 }
4423
4424 /* Set the alignment of a common symbol. */
4425 if ((common || bfd_is_com_section (sec))
4426 && h->root.type == bfd_link_hash_common)
4427 {
4428 unsigned int align;
4429
4430 if (common)
4431 align = bfd_log2 (isym->st_value);
4432 else
4433 {
4434 /* The new symbol is a common symbol in a shared object.
4435 We need to get the alignment from the section. */
4436 align = new_sec->alignment_power;
4437 }
4438 if (align > old_alignment)
4439 h->root.u.c.p->alignment_power = align;
4440 else
4441 h->root.u.c.p->alignment_power = old_alignment;
4442 }
4443
4444 if (is_elf_hash_table (htab))
4445 {
4446 /* Set a flag in the hash table entry indicating the type of
4447 reference or definition we just found. A dynamic symbol
4448 is one which is referenced or defined by both a regular
4449 object and a shared object. */
4450 bfd_boolean dynsym = FALSE;
4451
4452 /* Plugin symbols aren't normal. Don't set def_regular or
4453 ref_regular for them, or make them dynamic. */
4454 if ((abfd->flags & BFD_PLUGIN) != 0)
4455 ;
4456 else if (! dynamic)
4457 {
4458 if (! definition)
4459 {
4460 h->ref_regular = 1;
4461 if (bind != STB_WEAK)
4462 h->ref_regular_nonweak = 1;
4463 }
4464 else
4465 {
4466 h->def_regular = 1;
4467 if (h->def_dynamic)
4468 {
4469 h->def_dynamic = 0;
4470 h->ref_dynamic = 1;
4471 }
4472 }
4473
4474 /* If the indirect symbol has been forced local, don't
4475 make the real symbol dynamic. */
4476 if ((h == hi || !hi->forced_local)
4477 && (bfd_link_dll (info)
4478 || h->def_dynamic
4479 || h->ref_dynamic))
4480 dynsym = TRUE;
4481 }
4482 else
4483 {
4484 if (! definition)
4485 {
4486 h->ref_dynamic = 1;
4487 hi->ref_dynamic = 1;
4488 }
4489 else
4490 {
4491 h->def_dynamic = 1;
4492 hi->def_dynamic = 1;
4493 }
4494
4495 /* If the indirect symbol has been forced local, don't
4496 make the real symbol dynamic. */
4497 if ((h == hi || !hi->forced_local)
4498 && (h->def_regular
4499 || h->ref_regular
4500 || (h->u.weakdef != NULL
4501 && ! new_weakdef
4502 && h->u.weakdef->dynindx != -1)))
4503 dynsym = TRUE;
4504 }
4505
4506 /* Check to see if we need to add an indirect symbol for
4507 the default name. */
4508 if (definition
4509 || (!override && h->root.type == bfd_link_hash_common))
4510 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4511 sec, value, &old_bfd, &dynsym))
4512 goto error_free_vers;
4513
4514 /* Check the alignment when a common symbol is involved. This
4515 can change when a common symbol is overridden by a normal
4516 definition or a common symbol is ignored due to the old
4517 normal definition. We need to make sure the maximum
4518 alignment is maintained. */
4519 if ((old_alignment || common)
4520 && h->root.type != bfd_link_hash_common)
4521 {
4522 unsigned int common_align;
4523 unsigned int normal_align;
4524 unsigned int symbol_align;
4525 bfd *normal_bfd;
4526 bfd *common_bfd;
4527
4528 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4529 || h->root.type == bfd_link_hash_defweak);
4530
4531 symbol_align = ffs (h->root.u.def.value) - 1;
4532 if (h->root.u.def.section->owner != NULL
4533 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4534 {
4535 normal_align = h->root.u.def.section->alignment_power;
4536 if (normal_align > symbol_align)
4537 normal_align = symbol_align;
4538 }
4539 else
4540 normal_align = symbol_align;
4541
4542 if (old_alignment)
4543 {
4544 common_align = old_alignment;
4545 common_bfd = old_bfd;
4546 normal_bfd = abfd;
4547 }
4548 else
4549 {
4550 common_align = bfd_log2 (isym->st_value);
4551 common_bfd = abfd;
4552 normal_bfd = old_bfd;
4553 }
4554
4555 if (normal_align < common_align)
4556 {
4557 /* PR binutils/2735 */
4558 if (normal_bfd == NULL)
4559 (*_bfd_error_handler)
4560 (_("Warning: alignment %u of common symbol `%s' in %B is"
4561 " greater than the alignment (%u) of its section %A"),
4562 common_bfd, h->root.u.def.section,
4563 1 << common_align, name, 1 << normal_align);
4564 else
4565 (*_bfd_error_handler)
4566 (_("Warning: alignment %u of symbol `%s' in %B"
4567 " is smaller than %u in %B"),
4568 normal_bfd, common_bfd,
4569 1 << normal_align, name, 1 << common_align);
4570 }
4571 }
4572
4573 /* Remember the symbol size if it isn't undefined. */
4574 if (isym->st_size != 0
4575 && isym->st_shndx != SHN_UNDEF
4576 && (definition || h->size == 0))
4577 {
4578 if (h->size != 0
4579 && h->size != isym->st_size
4580 && ! size_change_ok)
4581 (*_bfd_error_handler)
4582 (_("Warning: size of symbol `%s' changed"
4583 " from %lu in %B to %lu in %B"),
4584 old_bfd, abfd,
4585 name, (unsigned long) h->size,
4586 (unsigned long) isym->st_size);
4587
4588 h->size = isym->st_size;
4589 }
4590
4591 /* If this is a common symbol, then we always want H->SIZE
4592 to be the size of the common symbol. The code just above
4593 won't fix the size if a common symbol becomes larger. We
4594 don't warn about a size change here, because that is
4595 covered by --warn-common. Allow changes between different
4596 function types. */
4597 if (h->root.type == bfd_link_hash_common)
4598 h->size = h->root.u.c.size;
4599
4600 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4601 && ((definition && !new_weak)
4602 || (old_weak && h->root.type == bfd_link_hash_common)
4603 || h->type == STT_NOTYPE))
4604 {
4605 unsigned int type = ELF_ST_TYPE (isym->st_info);
4606
4607 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4608 symbol. */
4609 if (type == STT_GNU_IFUNC
4610 && (abfd->flags & DYNAMIC) != 0)
4611 type = STT_FUNC;
4612
4613 if (h->type != type)
4614 {
4615 if (h->type != STT_NOTYPE && ! type_change_ok)
4616 (*_bfd_error_handler)
4617 (_("Warning: type of symbol `%s' changed"
4618 " from %d to %d in %B"),
4619 abfd, name, h->type, type);
4620
4621 h->type = type;
4622 }
4623 }
4624
4625 /* Merge st_other field. */
4626 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4627
4628 /* We don't want to make debug symbol dynamic. */
4629 if (definition
4630 && (sec->flags & SEC_DEBUGGING)
4631 && !bfd_link_relocatable (info))
4632 dynsym = FALSE;
4633
4634 /* Nor should we make plugin symbols dynamic. */
4635 if ((abfd->flags & BFD_PLUGIN) != 0)
4636 dynsym = FALSE;
4637
4638 if (definition)
4639 {
4640 h->target_internal = isym->st_target_internal;
4641 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4642 }
4643
4644 if (definition && !dynamic)
4645 {
4646 char *p = strchr (name, ELF_VER_CHR);
4647 if (p != NULL && p[1] != ELF_VER_CHR)
4648 {
4649 /* Queue non-default versions so that .symver x, x@FOO
4650 aliases can be checked. */
4651 if (!nondeflt_vers)
4652 {
4653 amt = ((isymend - isym + 1)
4654 * sizeof (struct elf_link_hash_entry *));
4655 nondeflt_vers
4656 = (struct elf_link_hash_entry **) bfd_malloc (amt);
4657 if (!nondeflt_vers)
4658 goto error_free_vers;
4659 }
4660 nondeflt_vers[nondeflt_vers_cnt++] = h;
4661 }
4662 }
4663
4664 if (dynsym && h->dynindx == -1)
4665 {
4666 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4667 goto error_free_vers;
4668 if (h->u.weakdef != NULL
4669 && ! new_weakdef
4670 && h->u.weakdef->dynindx == -1)
4671 {
4672 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4673 goto error_free_vers;
4674 }
4675 }
4676 else if (h->dynindx != -1)
4677 /* If the symbol already has a dynamic index, but
4678 visibility says it should not be visible, turn it into
4679 a local symbol. */
4680 switch (ELF_ST_VISIBILITY (h->other))
4681 {
4682 case STV_INTERNAL:
4683 case STV_HIDDEN:
4684 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4685 dynsym = FALSE;
4686 break;
4687 }
4688
4689 /* Don't add DT_NEEDED for references from the dummy bfd nor
4690 for unmatched symbol. */
4691 if (!add_needed
4692 && matched
4693 && definition
4694 && ((dynsym
4695 && h->ref_regular_nonweak
4696 && (old_bfd == NULL
4697 || (old_bfd->flags & BFD_PLUGIN) == 0))
4698 || (h->ref_dynamic_nonweak
4699 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4700 && !on_needed_list (elf_dt_name (abfd),
4701 htab->needed, NULL))))
4702 {
4703 int ret;
4704 const char *soname = elf_dt_name (abfd);
4705
4706 info->callbacks->minfo ("%!", soname, old_bfd,
4707 h->root.root.string);
4708
4709 /* A symbol from a library loaded via DT_NEEDED of some
4710 other library is referenced by a regular object.
4711 Add a DT_NEEDED entry for it. Issue an error if
4712 --no-add-needed is used and the reference was not
4713 a weak one. */
4714 if (old_bfd != NULL
4715 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4716 {
4717 (*_bfd_error_handler)
4718 (_("%B: undefined reference to symbol '%s'"),
4719 old_bfd, name);
4720 bfd_set_error (bfd_error_missing_dso);
4721 goto error_free_vers;
4722 }
4723
4724 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4725 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
4726
4727 add_needed = TRUE;
4728 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4729 if (ret < 0)
4730 goto error_free_vers;
4731
4732 BFD_ASSERT (ret == 0);
4733 }
4734 }
4735 }
4736
4737 if (extversym != NULL)
4738 {
4739 free (extversym);
4740 extversym = NULL;
4741 }
4742
4743 if (isymbuf != NULL)
4744 {
4745 free (isymbuf);
4746 isymbuf = NULL;
4747 }
4748
4749 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4750 {
4751 unsigned int i;
4752
4753 /* Restore the symbol table. */
4754 old_ent = (char *) old_tab + tabsize;
4755 memset (elf_sym_hashes (abfd), 0,
4756 extsymcount * sizeof (struct elf_link_hash_entry *));
4757 htab->root.table.table = old_table;
4758 htab->root.table.size = old_size;
4759 htab->root.table.count = old_count;
4760 memcpy (htab->root.table.table, old_tab, tabsize);
4761 htab->root.undefs = old_undefs;
4762 htab->root.undefs_tail = old_undefs_tail;
4763 _bfd_elf_strtab_restore_size (htab->dynstr, old_dynstr_size);
4764 for (i = 0; i < htab->root.table.size; i++)
4765 {
4766 struct bfd_hash_entry *p;
4767 struct elf_link_hash_entry *h;
4768 bfd_size_type size;
4769 unsigned int alignment_power;
4770
4771 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4772 {
4773 h = (struct elf_link_hash_entry *) p;
4774 if (h->root.type == bfd_link_hash_warning)
4775 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4776 if (h->dynindx >= old_dynsymcount
4777 && h->dynstr_index < old_dynstr_size)
4778 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
4779
4780 /* Preserve the maximum alignment and size for common
4781 symbols even if this dynamic lib isn't on DT_NEEDED
4782 since it can still be loaded at run time by another
4783 dynamic lib. */
4784 if (h->root.type == bfd_link_hash_common)
4785 {
4786 size = h->root.u.c.size;
4787 alignment_power = h->root.u.c.p->alignment_power;
4788 }
4789 else
4790 {
4791 size = 0;
4792 alignment_power = 0;
4793 }
4794 memcpy (p, old_ent, htab->root.table.entsize);
4795 old_ent = (char *) old_ent + htab->root.table.entsize;
4796 h = (struct elf_link_hash_entry *) p;
4797 if (h->root.type == bfd_link_hash_warning)
4798 {
4799 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4800 old_ent = (char *) old_ent + htab->root.table.entsize;
4801 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4802 }
4803 if (h->root.type == bfd_link_hash_common)
4804 {
4805 if (size > h->root.u.c.size)
4806 h->root.u.c.size = size;
4807 if (alignment_power > h->root.u.c.p->alignment_power)
4808 h->root.u.c.p->alignment_power = alignment_power;
4809 }
4810 }
4811 }
4812
4813 /* Make a special call to the linker "notice" function to
4814 tell it that symbols added for crefs may need to be removed. */
4815 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
4816 goto error_free_vers;
4817
4818 free (old_tab);
4819 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4820 alloc_mark);
4821 if (nondeflt_vers != NULL)
4822 free (nondeflt_vers);
4823 return TRUE;
4824 }
4825
4826 if (old_tab != NULL)
4827 {
4828 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
4829 goto error_free_vers;
4830 free (old_tab);
4831 old_tab = NULL;
4832 }
4833
4834 /* Now that all the symbols from this input file are created, if
4835 not performing a relocatable link, handle .symver foo, foo@BAR
4836 such that any relocs against foo become foo@BAR. */
4837 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4838 {
4839 bfd_size_type cnt, symidx;
4840
4841 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4842 {
4843 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4844 char *shortname, *p;
4845
4846 p = strchr (h->root.root.string, ELF_VER_CHR);
4847 if (p == NULL
4848 || (h->root.type != bfd_link_hash_defined
4849 && h->root.type != bfd_link_hash_defweak))
4850 continue;
4851
4852 amt = p - h->root.root.string;
4853 shortname = (char *) bfd_malloc (amt + 1);
4854 if (!shortname)
4855 goto error_free_vers;
4856 memcpy (shortname, h->root.root.string, amt);
4857 shortname[amt] = '\0';
4858
4859 hi = (struct elf_link_hash_entry *)
4860 bfd_link_hash_lookup (&htab->root, shortname,
4861 FALSE, FALSE, FALSE);
4862 if (hi != NULL
4863 && hi->root.type == h->root.type
4864 && hi->root.u.def.value == h->root.u.def.value
4865 && hi->root.u.def.section == h->root.u.def.section)
4866 {
4867 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4868 hi->root.type = bfd_link_hash_indirect;
4869 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4870 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4871 sym_hash = elf_sym_hashes (abfd);
4872 if (sym_hash)
4873 for (symidx = 0; symidx < extsymcount; ++symidx)
4874 if (sym_hash[symidx] == hi)
4875 {
4876 sym_hash[symidx] = h;
4877 break;
4878 }
4879 }
4880 free (shortname);
4881 }
4882 free (nondeflt_vers);
4883 nondeflt_vers = NULL;
4884 }
4885
4886 /* Now set the weakdefs field correctly for all the weak defined
4887 symbols we found. The only way to do this is to search all the
4888 symbols. Since we only need the information for non functions in
4889 dynamic objects, that's the only time we actually put anything on
4890 the list WEAKS. We need this information so that if a regular
4891 object refers to a symbol defined weakly in a dynamic object, the
4892 real symbol in the dynamic object is also put in the dynamic
4893 symbols; we also must arrange for both symbols to point to the
4894 same memory location. We could handle the general case of symbol
4895 aliasing, but a general symbol alias can only be generated in
4896 assembler code, handling it correctly would be very time
4897 consuming, and other ELF linkers don't handle general aliasing
4898 either. */
4899 if (weaks != NULL)
4900 {
4901 struct elf_link_hash_entry **hpp;
4902 struct elf_link_hash_entry **hppend;
4903 struct elf_link_hash_entry **sorted_sym_hash;
4904 struct elf_link_hash_entry *h;
4905 size_t sym_count;
4906
4907 /* Since we have to search the whole symbol list for each weak
4908 defined symbol, search time for N weak defined symbols will be
4909 O(N^2). Binary search will cut it down to O(NlogN). */
4910 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4911 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4912 if (sorted_sym_hash == NULL)
4913 goto error_return;
4914 sym_hash = sorted_sym_hash;
4915 hpp = elf_sym_hashes (abfd);
4916 hppend = hpp + extsymcount;
4917 sym_count = 0;
4918 for (; hpp < hppend; hpp++)
4919 {
4920 h = *hpp;
4921 if (h != NULL
4922 && h->root.type == bfd_link_hash_defined
4923 && !bed->is_function_type (h->type))
4924 {
4925 *sym_hash = h;
4926 sym_hash++;
4927 sym_count++;
4928 }
4929 }
4930
4931 qsort (sorted_sym_hash, sym_count,
4932 sizeof (struct elf_link_hash_entry *),
4933 elf_sort_symbol);
4934
4935 while (weaks != NULL)
4936 {
4937 struct elf_link_hash_entry *hlook;
4938 asection *slook;
4939 bfd_vma vlook;
4940 size_t i, j, idx = 0;
4941
4942 hlook = weaks;
4943 weaks = hlook->u.weakdef;
4944 hlook->u.weakdef = NULL;
4945
4946 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4947 || hlook->root.type == bfd_link_hash_defweak
4948 || hlook->root.type == bfd_link_hash_common
4949 || hlook->root.type == bfd_link_hash_indirect);
4950 slook = hlook->root.u.def.section;
4951 vlook = hlook->root.u.def.value;
4952
4953 i = 0;
4954 j = sym_count;
4955 while (i != j)
4956 {
4957 bfd_signed_vma vdiff;
4958 idx = (i + j) / 2;
4959 h = sorted_sym_hash[idx];
4960 vdiff = vlook - h->root.u.def.value;
4961 if (vdiff < 0)
4962 j = idx;
4963 else if (vdiff > 0)
4964 i = idx + 1;
4965 else
4966 {
4967 int sdiff = slook->id - h->root.u.def.section->id;
4968 if (sdiff < 0)
4969 j = idx;
4970 else if (sdiff > 0)
4971 i = idx + 1;
4972 else
4973 break;
4974 }
4975 }
4976
4977 /* We didn't find a value/section match. */
4978 if (i == j)
4979 continue;
4980
4981 /* With multiple aliases, or when the weak symbol is already
4982 strongly defined, we have multiple matching symbols and
4983 the binary search above may land on any of them. Step
4984 one past the matching symbol(s). */
4985 while (++idx != j)
4986 {
4987 h = sorted_sym_hash[idx];
4988 if (h->root.u.def.section != slook
4989 || h->root.u.def.value != vlook)
4990 break;
4991 }
4992
4993 /* Now look back over the aliases. Since we sorted by size
4994 as well as value and section, we'll choose the one with
4995 the largest size. */
4996 while (idx-- != i)
4997 {
4998 h = sorted_sym_hash[idx];
4999
5000 /* Stop if value or section doesn't match. */
5001 if (h->root.u.def.section != slook
5002 || h->root.u.def.value != vlook)
5003 break;
5004 else if (h != hlook)
5005 {
5006 hlook->u.weakdef = h;
5007
5008 /* If the weak definition is in the list of dynamic
5009 symbols, make sure the real definition is put
5010 there as well. */
5011 if (hlook->dynindx != -1 && h->dynindx == -1)
5012 {
5013 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5014 {
5015 err_free_sym_hash:
5016 free (sorted_sym_hash);
5017 goto error_return;
5018 }
5019 }
5020
5021 /* If the real definition is in the list of dynamic
5022 symbols, make sure the weak definition is put
5023 there as well. If we don't do this, then the
5024 dynamic loader might not merge the entries for the
5025 real definition and the weak definition. */
5026 if (h->dynindx != -1 && hlook->dynindx == -1)
5027 {
5028 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5029 goto err_free_sym_hash;
5030 }
5031 break;
5032 }
5033 }
5034 }
5035
5036 free (sorted_sym_hash);
5037 }
5038
5039 if (bed->check_directives
5040 && !(*bed->check_directives) (abfd, info))
5041 return FALSE;
5042
5043 if (!info->check_relocs_after_open_input
5044 && !_bfd_elf_link_check_relocs (abfd, info))
5045 return FALSE;
5046
5047 /* If this is a non-traditional link, try to optimize the handling
5048 of the .stab/.stabstr sections. */
5049 if (! dynamic
5050 && ! info->traditional_format
5051 && is_elf_hash_table (htab)
5052 && (info->strip != strip_all && info->strip != strip_debugger))
5053 {
5054 asection *stabstr;
5055
5056 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5057 if (stabstr != NULL)
5058 {
5059 bfd_size_type string_offset = 0;
5060 asection *stab;
5061
5062 for (stab = abfd->sections; stab; stab = stab->next)
5063 if (CONST_STRNEQ (stab->name, ".stab")
5064 && (!stab->name[5] ||
5065 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5066 && (stab->flags & SEC_MERGE) == 0
5067 && !bfd_is_abs_section (stab->output_section))
5068 {
5069 struct bfd_elf_section_data *secdata;
5070
5071 secdata = elf_section_data (stab);
5072 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5073 stabstr, &secdata->sec_info,
5074 &string_offset))
5075 goto error_return;
5076 if (secdata->sec_info)
5077 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5078 }
5079 }
5080 }
5081
5082 if (is_elf_hash_table (htab) && add_needed)
5083 {
5084 /* Add this bfd to the loaded list. */
5085 struct elf_link_loaded_list *n;
5086
5087 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5088 if (n == NULL)
5089 goto error_return;
5090 n->abfd = abfd;
5091 n->next = htab->loaded;
5092 htab->loaded = n;
5093 }
5094
5095 return TRUE;
5096
5097 error_free_vers:
5098 if (old_tab != NULL)
5099 free (old_tab);
5100 if (nondeflt_vers != NULL)
5101 free (nondeflt_vers);
5102 if (extversym != NULL)
5103 free (extversym);
5104 error_free_sym:
5105 if (isymbuf != NULL)
5106 free (isymbuf);
5107 error_return:
5108 return FALSE;
5109 }
5110
5111 /* Return the linker hash table entry of a symbol that might be
5112 satisfied by an archive symbol. Return -1 on error. */
5113
5114 struct elf_link_hash_entry *
5115 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5116 struct bfd_link_info *info,
5117 const char *name)
5118 {
5119 struct elf_link_hash_entry *h;
5120 char *p, *copy;
5121 size_t len, first;
5122
5123 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5124 if (h != NULL)
5125 return h;
5126
5127 /* If this is a default version (the name contains @@), look up the
5128 symbol again with only one `@' as well as without the version.
5129 The effect is that references to the symbol with and without the
5130 version will be matched by the default symbol in the archive. */
5131
5132 p = strchr (name, ELF_VER_CHR);
5133 if (p == NULL || p[1] != ELF_VER_CHR)
5134 return h;
5135
5136 /* First check with only one `@'. */
5137 len = strlen (name);
5138 copy = (char *) bfd_alloc (abfd, len);
5139 if (copy == NULL)
5140 return (struct elf_link_hash_entry *) 0 - 1;
5141
5142 first = p - name + 1;
5143 memcpy (copy, name, first);
5144 memcpy (copy + first, name + first + 1, len - first);
5145
5146 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5147 if (h == NULL)
5148 {
5149 /* We also need to check references to the symbol without the
5150 version. */
5151 copy[first - 1] = '\0';
5152 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5153 FALSE, FALSE, TRUE);
5154 }
5155
5156 bfd_release (abfd, copy);
5157 return h;
5158 }
5159
5160 /* Add symbols from an ELF archive file to the linker hash table. We
5161 don't use _bfd_generic_link_add_archive_symbols because we need to
5162 handle versioned symbols.
5163
5164 Fortunately, ELF archive handling is simpler than that done by
5165 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5166 oddities. In ELF, if we find a symbol in the archive map, and the
5167 symbol is currently undefined, we know that we must pull in that
5168 object file.
5169
5170 Unfortunately, we do have to make multiple passes over the symbol
5171 table until nothing further is resolved. */
5172
5173 static bfd_boolean
5174 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5175 {
5176 symindex c;
5177 unsigned char *included = NULL;
5178 carsym *symdefs;
5179 bfd_boolean loop;
5180 bfd_size_type amt;
5181 const struct elf_backend_data *bed;
5182 struct elf_link_hash_entry * (*archive_symbol_lookup)
5183 (bfd *, struct bfd_link_info *, const char *);
5184
5185 if (! bfd_has_map (abfd))
5186 {
5187 /* An empty archive is a special case. */
5188 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5189 return TRUE;
5190 bfd_set_error (bfd_error_no_armap);
5191 return FALSE;
5192 }
5193
5194 /* Keep track of all symbols we know to be already defined, and all
5195 files we know to be already included. This is to speed up the
5196 second and subsequent passes. */
5197 c = bfd_ardata (abfd)->symdef_count;
5198 if (c == 0)
5199 return TRUE;
5200 amt = c;
5201 amt *= sizeof (*included);
5202 included = (unsigned char *) bfd_zmalloc (amt);
5203 if (included == NULL)
5204 return FALSE;
5205
5206 symdefs = bfd_ardata (abfd)->symdefs;
5207 bed = get_elf_backend_data (abfd);
5208 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5209
5210 do
5211 {
5212 file_ptr last;
5213 symindex i;
5214 carsym *symdef;
5215 carsym *symdefend;
5216
5217 loop = FALSE;
5218 last = -1;
5219
5220 symdef = symdefs;
5221 symdefend = symdef + c;
5222 for (i = 0; symdef < symdefend; symdef++, i++)
5223 {
5224 struct elf_link_hash_entry *h;
5225 bfd *element;
5226 struct bfd_link_hash_entry *undefs_tail;
5227 symindex mark;
5228
5229 if (included[i])
5230 continue;
5231 if (symdef->file_offset == last)
5232 {
5233 included[i] = TRUE;
5234 continue;
5235 }
5236
5237 h = archive_symbol_lookup (abfd, info, symdef->name);
5238 if (h == (struct elf_link_hash_entry *) 0 - 1)
5239 goto error_return;
5240
5241 if (h == NULL)
5242 continue;
5243
5244 if (h->root.type == bfd_link_hash_common)
5245 {
5246 /* We currently have a common symbol. The archive map contains
5247 a reference to this symbol, so we may want to include it. We
5248 only want to include it however, if this archive element
5249 contains a definition of the symbol, not just another common
5250 declaration of it.
5251
5252 Unfortunately some archivers (including GNU ar) will put
5253 declarations of common symbols into their archive maps, as
5254 well as real definitions, so we cannot just go by the archive
5255 map alone. Instead we must read in the element's symbol
5256 table and check that to see what kind of symbol definition
5257 this is. */
5258 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5259 continue;
5260 }
5261 else if (h->root.type != bfd_link_hash_undefined)
5262 {
5263 if (h->root.type != bfd_link_hash_undefweak)
5264 /* Symbol must be defined. Don't check it again. */
5265 included[i] = TRUE;
5266 continue;
5267 }
5268
5269 /* We need to include this archive member. */
5270 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5271 if (element == NULL)
5272 goto error_return;
5273
5274 if (! bfd_check_format (element, bfd_object))
5275 goto error_return;
5276
5277 undefs_tail = info->hash->undefs_tail;
5278
5279 if (!(*info->callbacks
5280 ->add_archive_element) (info, element, symdef->name, &element))
5281 goto error_return;
5282 if (!bfd_link_add_symbols (element, info))
5283 goto error_return;
5284
5285 /* If there are any new undefined symbols, we need to make
5286 another pass through the archive in order to see whether
5287 they can be defined. FIXME: This isn't perfect, because
5288 common symbols wind up on undefs_tail and because an
5289 undefined symbol which is defined later on in this pass
5290 does not require another pass. This isn't a bug, but it
5291 does make the code less efficient than it could be. */
5292 if (undefs_tail != info->hash->undefs_tail)
5293 loop = TRUE;
5294
5295 /* Look backward to mark all symbols from this object file
5296 which we have already seen in this pass. */
5297 mark = i;
5298 do
5299 {
5300 included[mark] = TRUE;
5301 if (mark == 0)
5302 break;
5303 --mark;
5304 }
5305 while (symdefs[mark].file_offset == symdef->file_offset);
5306
5307 /* We mark subsequent symbols from this object file as we go
5308 on through the loop. */
5309 last = symdef->file_offset;
5310 }
5311 }
5312 while (loop);
5313
5314 free (included);
5315
5316 return TRUE;
5317
5318 error_return:
5319 if (included != NULL)
5320 free (included);
5321 return FALSE;
5322 }
5323
5324 /* Given an ELF BFD, add symbols to the global hash table as
5325 appropriate. */
5326
5327 bfd_boolean
5328 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5329 {
5330 switch (bfd_get_format (abfd))
5331 {
5332 case bfd_object:
5333 return elf_link_add_object_symbols (abfd, info);
5334 case bfd_archive:
5335 return elf_link_add_archive_symbols (abfd, info);
5336 default:
5337 bfd_set_error (bfd_error_wrong_format);
5338 return FALSE;
5339 }
5340 }
5341 \f
5342 struct hash_codes_info
5343 {
5344 unsigned long *hashcodes;
5345 bfd_boolean error;
5346 };
5347
5348 /* This function will be called though elf_link_hash_traverse to store
5349 all hash value of the exported symbols in an array. */
5350
5351 static bfd_boolean
5352 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5353 {
5354 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5355 const char *name;
5356 unsigned long ha;
5357 char *alc = NULL;
5358
5359 /* Ignore indirect symbols. These are added by the versioning code. */
5360 if (h->dynindx == -1)
5361 return TRUE;
5362
5363 name = h->root.root.string;
5364 if (h->versioned >= versioned)
5365 {
5366 char *p = strchr (name, ELF_VER_CHR);
5367 if (p != NULL)
5368 {
5369 alc = (char *) bfd_malloc (p - name + 1);
5370 if (alc == NULL)
5371 {
5372 inf->error = TRUE;
5373 return FALSE;
5374 }
5375 memcpy (alc, name, p - name);
5376 alc[p - name] = '\0';
5377 name = alc;
5378 }
5379 }
5380
5381 /* Compute the hash value. */
5382 ha = bfd_elf_hash (name);
5383
5384 /* Store the found hash value in the array given as the argument. */
5385 *(inf->hashcodes)++ = ha;
5386
5387 /* And store it in the struct so that we can put it in the hash table
5388 later. */
5389 h->u.elf_hash_value = ha;
5390
5391 if (alc != NULL)
5392 free (alc);
5393
5394 return TRUE;
5395 }
5396
5397 struct collect_gnu_hash_codes
5398 {
5399 bfd *output_bfd;
5400 const struct elf_backend_data *bed;
5401 unsigned long int nsyms;
5402 unsigned long int maskbits;
5403 unsigned long int *hashcodes;
5404 unsigned long int *hashval;
5405 unsigned long int *indx;
5406 unsigned long int *counts;
5407 bfd_vma *bitmask;
5408 bfd_byte *contents;
5409 long int min_dynindx;
5410 unsigned long int bucketcount;
5411 unsigned long int symindx;
5412 long int local_indx;
5413 long int shift1, shift2;
5414 unsigned long int mask;
5415 bfd_boolean error;
5416 };
5417
5418 /* This function will be called though elf_link_hash_traverse to store
5419 all hash value of the exported symbols in an array. */
5420
5421 static bfd_boolean
5422 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5423 {
5424 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5425 const char *name;
5426 unsigned long ha;
5427 char *alc = NULL;
5428
5429 /* Ignore indirect symbols. These are added by the versioning code. */
5430 if (h->dynindx == -1)
5431 return TRUE;
5432
5433 /* Ignore also local symbols and undefined symbols. */
5434 if (! (*s->bed->elf_hash_symbol) (h))
5435 return TRUE;
5436
5437 name = h->root.root.string;
5438 if (h->versioned >= versioned)
5439 {
5440 char *p = strchr (name, ELF_VER_CHR);
5441 if (p != NULL)
5442 {
5443 alc = (char *) bfd_malloc (p - name + 1);
5444 if (alc == NULL)
5445 {
5446 s->error = TRUE;
5447 return FALSE;
5448 }
5449 memcpy (alc, name, p - name);
5450 alc[p - name] = '\0';
5451 name = alc;
5452 }
5453 }
5454
5455 /* Compute the hash value. */
5456 ha = bfd_elf_gnu_hash (name);
5457
5458 /* Store the found hash value in the array for compute_bucket_count,
5459 and also for .dynsym reordering purposes. */
5460 s->hashcodes[s->nsyms] = ha;
5461 s->hashval[h->dynindx] = ha;
5462 ++s->nsyms;
5463 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5464 s->min_dynindx = h->dynindx;
5465
5466 if (alc != NULL)
5467 free (alc);
5468
5469 return TRUE;
5470 }
5471
5472 /* This function will be called though elf_link_hash_traverse to do
5473 final dynaminc symbol renumbering. */
5474
5475 static bfd_boolean
5476 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5477 {
5478 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5479 unsigned long int bucket;
5480 unsigned long int val;
5481
5482 /* Ignore indirect symbols. */
5483 if (h->dynindx == -1)
5484 return TRUE;
5485
5486 /* Ignore also local symbols and undefined symbols. */
5487 if (! (*s->bed->elf_hash_symbol) (h))
5488 {
5489 if (h->dynindx >= s->min_dynindx)
5490 h->dynindx = s->local_indx++;
5491 return TRUE;
5492 }
5493
5494 bucket = s->hashval[h->dynindx] % s->bucketcount;
5495 val = (s->hashval[h->dynindx] >> s->shift1)
5496 & ((s->maskbits >> s->shift1) - 1);
5497 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5498 s->bitmask[val]
5499 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5500 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5501 if (s->counts[bucket] == 1)
5502 /* Last element terminates the chain. */
5503 val |= 1;
5504 bfd_put_32 (s->output_bfd, val,
5505 s->contents + (s->indx[bucket] - s->symindx) * 4);
5506 --s->counts[bucket];
5507 h->dynindx = s->indx[bucket]++;
5508 return TRUE;
5509 }
5510
5511 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5512
5513 bfd_boolean
5514 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5515 {
5516 return !(h->forced_local
5517 || h->root.type == bfd_link_hash_undefined
5518 || h->root.type == bfd_link_hash_undefweak
5519 || ((h->root.type == bfd_link_hash_defined
5520 || h->root.type == bfd_link_hash_defweak)
5521 && h->root.u.def.section->output_section == NULL));
5522 }
5523
5524 /* Array used to determine the number of hash table buckets to use
5525 based on the number of symbols there are. If there are fewer than
5526 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5527 fewer than 37 we use 17 buckets, and so forth. We never use more
5528 than 32771 buckets. */
5529
5530 static const size_t elf_buckets[] =
5531 {
5532 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5533 16411, 32771, 0
5534 };
5535
5536 /* Compute bucket count for hashing table. We do not use a static set
5537 of possible tables sizes anymore. Instead we determine for all
5538 possible reasonable sizes of the table the outcome (i.e., the
5539 number of collisions etc) and choose the best solution. The
5540 weighting functions are not too simple to allow the table to grow
5541 without bounds. Instead one of the weighting factors is the size.
5542 Therefore the result is always a good payoff between few collisions
5543 (= short chain lengths) and table size. */
5544 static size_t
5545 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5546 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5547 unsigned long int nsyms,
5548 int gnu_hash)
5549 {
5550 size_t best_size = 0;
5551 unsigned long int i;
5552
5553 /* We have a problem here. The following code to optimize the table
5554 size requires an integer type with more the 32 bits. If
5555 BFD_HOST_U_64_BIT is set we know about such a type. */
5556 #ifdef BFD_HOST_U_64_BIT
5557 if (info->optimize)
5558 {
5559 size_t minsize;
5560 size_t maxsize;
5561 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5562 bfd *dynobj = elf_hash_table (info)->dynobj;
5563 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5564 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5565 unsigned long int *counts;
5566 bfd_size_type amt;
5567 unsigned int no_improvement_count = 0;
5568
5569 /* Possible optimization parameters: if we have NSYMS symbols we say
5570 that the hashing table must at least have NSYMS/4 and at most
5571 2*NSYMS buckets. */
5572 minsize = nsyms / 4;
5573 if (minsize == 0)
5574 minsize = 1;
5575 best_size = maxsize = nsyms * 2;
5576 if (gnu_hash)
5577 {
5578 if (minsize < 2)
5579 minsize = 2;
5580 if ((best_size & 31) == 0)
5581 ++best_size;
5582 }
5583
5584 /* Create array where we count the collisions in. We must use bfd_malloc
5585 since the size could be large. */
5586 amt = maxsize;
5587 amt *= sizeof (unsigned long int);
5588 counts = (unsigned long int *) bfd_malloc (amt);
5589 if (counts == NULL)
5590 return 0;
5591
5592 /* Compute the "optimal" size for the hash table. The criteria is a
5593 minimal chain length. The minor criteria is (of course) the size
5594 of the table. */
5595 for (i = minsize; i < maxsize; ++i)
5596 {
5597 /* Walk through the array of hashcodes and count the collisions. */
5598 BFD_HOST_U_64_BIT max;
5599 unsigned long int j;
5600 unsigned long int fact;
5601
5602 if (gnu_hash && (i & 31) == 0)
5603 continue;
5604
5605 memset (counts, '\0', i * sizeof (unsigned long int));
5606
5607 /* Determine how often each hash bucket is used. */
5608 for (j = 0; j < nsyms; ++j)
5609 ++counts[hashcodes[j] % i];
5610
5611 /* For the weight function we need some information about the
5612 pagesize on the target. This is information need not be 100%
5613 accurate. Since this information is not available (so far) we
5614 define it here to a reasonable default value. If it is crucial
5615 to have a better value some day simply define this value. */
5616 # ifndef BFD_TARGET_PAGESIZE
5617 # define BFD_TARGET_PAGESIZE (4096)
5618 # endif
5619
5620 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5621 and the chains. */
5622 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5623
5624 # if 1
5625 /* Variant 1: optimize for short chains. We add the squares
5626 of all the chain lengths (which favors many small chain
5627 over a few long chains). */
5628 for (j = 0; j < i; ++j)
5629 max += counts[j] * counts[j];
5630
5631 /* This adds penalties for the overall size of the table. */
5632 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5633 max *= fact * fact;
5634 # else
5635 /* Variant 2: Optimize a lot more for small table. Here we
5636 also add squares of the size but we also add penalties for
5637 empty slots (the +1 term). */
5638 for (j = 0; j < i; ++j)
5639 max += (1 + counts[j]) * (1 + counts[j]);
5640
5641 /* The overall size of the table is considered, but not as
5642 strong as in variant 1, where it is squared. */
5643 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5644 max *= fact;
5645 # endif
5646
5647 /* Compare with current best results. */
5648 if (max < best_chlen)
5649 {
5650 best_chlen = max;
5651 best_size = i;
5652 no_improvement_count = 0;
5653 }
5654 /* PR 11843: Avoid futile long searches for the best bucket size
5655 when there are a large number of symbols. */
5656 else if (++no_improvement_count == 100)
5657 break;
5658 }
5659
5660 free (counts);
5661 }
5662 else
5663 #endif /* defined (BFD_HOST_U_64_BIT) */
5664 {
5665 /* This is the fallback solution if no 64bit type is available or if we
5666 are not supposed to spend much time on optimizations. We select the
5667 bucket count using a fixed set of numbers. */
5668 for (i = 0; elf_buckets[i] != 0; i++)
5669 {
5670 best_size = elf_buckets[i];
5671 if (nsyms < elf_buckets[i + 1])
5672 break;
5673 }
5674 if (gnu_hash && best_size < 2)
5675 best_size = 2;
5676 }
5677
5678 return best_size;
5679 }
5680
5681 /* Size any SHT_GROUP section for ld -r. */
5682
5683 bfd_boolean
5684 _bfd_elf_size_group_sections (struct bfd_link_info *info)
5685 {
5686 bfd *ibfd;
5687
5688 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
5689 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5690 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5691 return FALSE;
5692 return TRUE;
5693 }
5694
5695 /* Set a default stack segment size. The value in INFO wins. If it
5696 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5697 undefined it is initialized. */
5698
5699 bfd_boolean
5700 bfd_elf_stack_segment_size (bfd *output_bfd,
5701 struct bfd_link_info *info,
5702 const char *legacy_symbol,
5703 bfd_vma default_size)
5704 {
5705 struct elf_link_hash_entry *h = NULL;
5706
5707 /* Look for legacy symbol. */
5708 if (legacy_symbol)
5709 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5710 FALSE, FALSE, FALSE);
5711 if (h && (h->root.type == bfd_link_hash_defined
5712 || h->root.type == bfd_link_hash_defweak)
5713 && h->def_regular
5714 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5715 {
5716 /* The symbol has no type if specified on the command line. */
5717 h->type = STT_OBJECT;
5718 if (info->stacksize)
5719 (*_bfd_error_handler) (_("%B: stack size specified and %s set"),
5720 output_bfd, legacy_symbol);
5721 else if (h->root.u.def.section != bfd_abs_section_ptr)
5722 (*_bfd_error_handler) (_("%B: %s not absolute"),
5723 output_bfd, legacy_symbol);
5724 else
5725 info->stacksize = h->root.u.def.value;
5726 }
5727
5728 if (!info->stacksize)
5729 /* If the user didn't set a size, or explicitly inhibit the
5730 size, set it now. */
5731 info->stacksize = default_size;
5732
5733 /* Provide the legacy symbol, if it is referenced. */
5734 if (h && (h->root.type == bfd_link_hash_undefined
5735 || h->root.type == bfd_link_hash_undefweak))
5736 {
5737 struct bfd_link_hash_entry *bh = NULL;
5738
5739 if (!(_bfd_generic_link_add_one_symbol
5740 (info, output_bfd, legacy_symbol,
5741 BSF_GLOBAL, bfd_abs_section_ptr,
5742 info->stacksize >= 0 ? info->stacksize : 0,
5743 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5744 return FALSE;
5745
5746 h = (struct elf_link_hash_entry *) bh;
5747 h->def_regular = 1;
5748 h->type = STT_OBJECT;
5749 }
5750
5751 return TRUE;
5752 }
5753
5754 /* Set up the sizes and contents of the ELF dynamic sections. This is
5755 called by the ELF linker emulation before_allocation routine. We
5756 must set the sizes of the sections before the linker sets the
5757 addresses of the various sections. */
5758
5759 bfd_boolean
5760 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5761 const char *soname,
5762 const char *rpath,
5763 const char *filter_shlib,
5764 const char *audit,
5765 const char *depaudit,
5766 const char * const *auxiliary_filters,
5767 struct bfd_link_info *info,
5768 asection **sinterpptr)
5769 {
5770 bfd_size_type soname_indx;
5771 bfd *dynobj;
5772 const struct elf_backend_data *bed;
5773 struct elf_info_failed asvinfo;
5774
5775 *sinterpptr = NULL;
5776
5777 soname_indx = (bfd_size_type) -1;
5778
5779 if (!is_elf_hash_table (info->hash))
5780 return TRUE;
5781
5782 bed = get_elf_backend_data (output_bfd);
5783
5784 /* Any syms created from now on start with -1 in
5785 got.refcount/offset and plt.refcount/offset. */
5786 elf_hash_table (info)->init_got_refcount
5787 = elf_hash_table (info)->init_got_offset;
5788 elf_hash_table (info)->init_plt_refcount
5789 = elf_hash_table (info)->init_plt_offset;
5790
5791 if (bfd_link_relocatable (info)
5792 && !_bfd_elf_size_group_sections (info))
5793 return FALSE;
5794
5795 /* The backend may have to create some sections regardless of whether
5796 we're dynamic or not. */
5797 if (bed->elf_backend_always_size_sections
5798 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5799 return FALSE;
5800
5801 /* Determine any GNU_STACK segment requirements, after the backend
5802 has had a chance to set a default segment size. */
5803 if (info->execstack)
5804 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
5805 else if (info->noexecstack)
5806 elf_stack_flags (output_bfd) = PF_R | PF_W;
5807 else
5808 {
5809 bfd *inputobj;
5810 asection *notesec = NULL;
5811 int exec = 0;
5812
5813 for (inputobj = info->input_bfds;
5814 inputobj;
5815 inputobj = inputobj->link.next)
5816 {
5817 asection *s;
5818
5819 if (inputobj->flags
5820 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5821 continue;
5822 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5823 if (s)
5824 {
5825 if (s->flags & SEC_CODE)
5826 exec = PF_X;
5827 notesec = s;
5828 }
5829 else if (bed->default_execstack)
5830 exec = PF_X;
5831 }
5832 if (notesec || info->stacksize > 0)
5833 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
5834 if (notesec && exec && bfd_link_relocatable (info)
5835 && notesec->output_section != bfd_abs_section_ptr)
5836 notesec->output_section->flags |= SEC_CODE;
5837 }
5838
5839 dynobj = elf_hash_table (info)->dynobj;
5840
5841 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5842 {
5843 struct elf_info_failed eif;
5844 struct elf_link_hash_entry *h;
5845 asection *dynstr;
5846 struct bfd_elf_version_tree *t;
5847 struct bfd_elf_version_expr *d;
5848 asection *s;
5849 bfd_boolean all_defined;
5850
5851 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
5852 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
5853
5854 if (soname != NULL)
5855 {
5856 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5857 soname, TRUE);
5858 if (soname_indx == (bfd_size_type) -1
5859 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5860 return FALSE;
5861 }
5862
5863 if (info->symbolic)
5864 {
5865 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5866 return FALSE;
5867 info->flags |= DF_SYMBOLIC;
5868 }
5869
5870 if (rpath != NULL)
5871 {
5872 bfd_size_type indx;
5873 bfd_vma tag;
5874
5875 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5876 TRUE);
5877 if (indx == (bfd_size_type) -1)
5878 return FALSE;
5879
5880 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
5881 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
5882 return FALSE;
5883 }
5884
5885 if (filter_shlib != NULL)
5886 {
5887 bfd_size_type indx;
5888
5889 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5890 filter_shlib, TRUE);
5891 if (indx == (bfd_size_type) -1
5892 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5893 return FALSE;
5894 }
5895
5896 if (auxiliary_filters != NULL)
5897 {
5898 const char * const *p;
5899
5900 for (p = auxiliary_filters; *p != NULL; p++)
5901 {
5902 bfd_size_type indx;
5903
5904 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5905 *p, TRUE);
5906 if (indx == (bfd_size_type) -1
5907 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5908 return FALSE;
5909 }
5910 }
5911
5912 if (audit != NULL)
5913 {
5914 bfd_size_type indx;
5915
5916 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5917 TRUE);
5918 if (indx == (bfd_size_type) -1
5919 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5920 return FALSE;
5921 }
5922
5923 if (depaudit != NULL)
5924 {
5925 bfd_size_type indx;
5926
5927 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
5928 TRUE);
5929 if (indx == (bfd_size_type) -1
5930 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
5931 return FALSE;
5932 }
5933
5934 eif.info = info;
5935 eif.failed = FALSE;
5936
5937 /* If we are supposed to export all symbols into the dynamic symbol
5938 table (this is not the normal case), then do so. */
5939 if (info->export_dynamic
5940 || (bfd_link_executable (info) && info->dynamic))
5941 {
5942 elf_link_hash_traverse (elf_hash_table (info),
5943 _bfd_elf_export_symbol,
5944 &eif);
5945 if (eif.failed)
5946 return FALSE;
5947 }
5948
5949 /* Make all global versions with definition. */
5950 for (t = info->version_info; t != NULL; t = t->next)
5951 for (d = t->globals.list; d != NULL; d = d->next)
5952 if (!d->symver && d->literal)
5953 {
5954 const char *verstr, *name;
5955 size_t namelen, verlen, newlen;
5956 char *newname, *p, leading_char;
5957 struct elf_link_hash_entry *newh;
5958
5959 leading_char = bfd_get_symbol_leading_char (output_bfd);
5960 name = d->pattern;
5961 namelen = strlen (name) + (leading_char != '\0');
5962 verstr = t->name;
5963 verlen = strlen (verstr);
5964 newlen = namelen + verlen + 3;
5965
5966 newname = (char *) bfd_malloc (newlen);
5967 if (newname == NULL)
5968 return FALSE;
5969 newname[0] = leading_char;
5970 memcpy (newname + (leading_char != '\0'), name, namelen);
5971
5972 /* Check the hidden versioned definition. */
5973 p = newname + namelen;
5974 *p++ = ELF_VER_CHR;
5975 memcpy (p, verstr, verlen + 1);
5976 newh = elf_link_hash_lookup (elf_hash_table (info),
5977 newname, FALSE, FALSE,
5978 FALSE);
5979 if (newh == NULL
5980 || (newh->root.type != bfd_link_hash_defined
5981 && newh->root.type != bfd_link_hash_defweak))
5982 {
5983 /* Check the default versioned definition. */
5984 *p++ = ELF_VER_CHR;
5985 memcpy (p, verstr, verlen + 1);
5986 newh = elf_link_hash_lookup (elf_hash_table (info),
5987 newname, FALSE, FALSE,
5988 FALSE);
5989 }
5990 free (newname);
5991
5992 /* Mark this version if there is a definition and it is
5993 not defined in a shared object. */
5994 if (newh != NULL
5995 && !newh->def_dynamic
5996 && (newh->root.type == bfd_link_hash_defined
5997 || newh->root.type == bfd_link_hash_defweak))
5998 d->symver = 1;
5999 }
6000
6001 /* Attach all the symbols to their version information. */
6002 asvinfo.info = info;
6003 asvinfo.failed = FALSE;
6004
6005 elf_link_hash_traverse (elf_hash_table (info),
6006 _bfd_elf_link_assign_sym_version,
6007 &asvinfo);
6008 if (asvinfo.failed)
6009 return FALSE;
6010
6011 if (!info->allow_undefined_version)
6012 {
6013 /* Check if all global versions have a definition. */
6014 all_defined = TRUE;
6015 for (t = info->version_info; t != NULL; t = t->next)
6016 for (d = t->globals.list; d != NULL; d = d->next)
6017 if (d->literal && !d->symver && !d->script)
6018 {
6019 (*_bfd_error_handler)
6020 (_("%s: undefined version: %s"),
6021 d->pattern, t->name);
6022 all_defined = FALSE;
6023 }
6024
6025 if (!all_defined)
6026 {
6027 bfd_set_error (bfd_error_bad_value);
6028 return FALSE;
6029 }
6030 }
6031
6032 /* Find all symbols which were defined in a dynamic object and make
6033 the backend pick a reasonable value for them. */
6034 elf_link_hash_traverse (elf_hash_table (info),
6035 _bfd_elf_adjust_dynamic_symbol,
6036 &eif);
6037 if (eif.failed)
6038 return FALSE;
6039
6040 /* Add some entries to the .dynamic section. We fill in some of the
6041 values later, in bfd_elf_final_link, but we must add the entries
6042 now so that we know the final size of the .dynamic section. */
6043
6044 /* If there are initialization and/or finalization functions to
6045 call then add the corresponding DT_INIT/DT_FINI entries. */
6046 h = (info->init_function
6047 ? elf_link_hash_lookup (elf_hash_table (info),
6048 info->init_function, FALSE,
6049 FALSE, FALSE)
6050 : NULL);
6051 if (h != NULL
6052 && (h->ref_regular
6053 || h->def_regular))
6054 {
6055 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6056 return FALSE;
6057 }
6058 h = (info->fini_function
6059 ? elf_link_hash_lookup (elf_hash_table (info),
6060 info->fini_function, FALSE,
6061 FALSE, FALSE)
6062 : NULL);
6063 if (h != NULL
6064 && (h->ref_regular
6065 || h->def_regular))
6066 {
6067 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6068 return FALSE;
6069 }
6070
6071 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6072 if (s != NULL && s->linker_has_input)
6073 {
6074 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6075 if (! bfd_link_executable (info))
6076 {
6077 bfd *sub;
6078 asection *o;
6079
6080 for (sub = info->input_bfds; sub != NULL;
6081 sub = sub->link.next)
6082 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
6083 for (o = sub->sections; o != NULL; o = o->next)
6084 if (elf_section_data (o)->this_hdr.sh_type
6085 == SHT_PREINIT_ARRAY)
6086 {
6087 (*_bfd_error_handler)
6088 (_("%B: .preinit_array section is not allowed in DSO"),
6089 sub);
6090 break;
6091 }
6092
6093 bfd_set_error (bfd_error_nonrepresentable_section);
6094 return FALSE;
6095 }
6096
6097 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6098 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6099 return FALSE;
6100 }
6101 s = bfd_get_section_by_name (output_bfd, ".init_array");
6102 if (s != NULL && s->linker_has_input)
6103 {
6104 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6105 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6106 return FALSE;
6107 }
6108 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6109 if (s != NULL && s->linker_has_input)
6110 {
6111 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6112 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6113 return FALSE;
6114 }
6115
6116 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6117 /* If .dynstr is excluded from the link, we don't want any of
6118 these tags. Strictly, we should be checking each section
6119 individually; This quick check covers for the case where
6120 someone does a /DISCARD/ : { *(*) }. */
6121 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6122 {
6123 bfd_size_type strsize;
6124
6125 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6126 if ((info->emit_hash
6127 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6128 || (info->emit_gnu_hash
6129 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6130 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6131 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6132 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6133 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6134 bed->s->sizeof_sym))
6135 return FALSE;
6136 }
6137 }
6138
6139 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6140 return FALSE;
6141
6142 /* The backend must work out the sizes of all the other dynamic
6143 sections. */
6144 if (dynobj != NULL
6145 && bed->elf_backend_size_dynamic_sections != NULL
6146 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6147 return FALSE;
6148
6149 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6150 {
6151 unsigned long section_sym_count;
6152 struct bfd_elf_version_tree *verdefs;
6153 asection *s;
6154
6155 /* Set up the version definition section. */
6156 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6157 BFD_ASSERT (s != NULL);
6158
6159 /* We may have created additional version definitions if we are
6160 just linking a regular application. */
6161 verdefs = info->version_info;
6162
6163 /* Skip anonymous version tag. */
6164 if (verdefs != NULL && verdefs->vernum == 0)
6165 verdefs = verdefs->next;
6166
6167 if (verdefs == NULL && !info->create_default_symver)
6168 s->flags |= SEC_EXCLUDE;
6169 else
6170 {
6171 unsigned int cdefs;
6172 bfd_size_type size;
6173 struct bfd_elf_version_tree *t;
6174 bfd_byte *p;
6175 Elf_Internal_Verdef def;
6176 Elf_Internal_Verdaux defaux;
6177 struct bfd_link_hash_entry *bh;
6178 struct elf_link_hash_entry *h;
6179 const char *name;
6180
6181 cdefs = 0;
6182 size = 0;
6183
6184 /* Make space for the base version. */
6185 size += sizeof (Elf_External_Verdef);
6186 size += sizeof (Elf_External_Verdaux);
6187 ++cdefs;
6188
6189 /* Make space for the default version. */
6190 if (info->create_default_symver)
6191 {
6192 size += sizeof (Elf_External_Verdef);
6193 ++cdefs;
6194 }
6195
6196 for (t = verdefs; t != NULL; t = t->next)
6197 {
6198 struct bfd_elf_version_deps *n;
6199
6200 /* Don't emit base version twice. */
6201 if (t->vernum == 0)
6202 continue;
6203
6204 size += sizeof (Elf_External_Verdef);
6205 size += sizeof (Elf_External_Verdaux);
6206 ++cdefs;
6207
6208 for (n = t->deps; n != NULL; n = n->next)
6209 size += sizeof (Elf_External_Verdaux);
6210 }
6211
6212 s->size = size;
6213 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6214 if (s->contents == NULL && s->size != 0)
6215 return FALSE;
6216
6217 /* Fill in the version definition section. */
6218
6219 p = s->contents;
6220
6221 def.vd_version = VER_DEF_CURRENT;
6222 def.vd_flags = VER_FLG_BASE;
6223 def.vd_ndx = 1;
6224 def.vd_cnt = 1;
6225 if (info->create_default_symver)
6226 {
6227 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6228 def.vd_next = sizeof (Elf_External_Verdef);
6229 }
6230 else
6231 {
6232 def.vd_aux = sizeof (Elf_External_Verdef);
6233 def.vd_next = (sizeof (Elf_External_Verdef)
6234 + sizeof (Elf_External_Verdaux));
6235 }
6236
6237 if (soname_indx != (bfd_size_type) -1)
6238 {
6239 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6240 soname_indx);
6241 def.vd_hash = bfd_elf_hash (soname);
6242 defaux.vda_name = soname_indx;
6243 name = soname;
6244 }
6245 else
6246 {
6247 bfd_size_type indx;
6248
6249 name = lbasename (output_bfd->filename);
6250 def.vd_hash = bfd_elf_hash (name);
6251 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6252 name, FALSE);
6253 if (indx == (bfd_size_type) -1)
6254 return FALSE;
6255 defaux.vda_name = indx;
6256 }
6257 defaux.vda_next = 0;
6258
6259 _bfd_elf_swap_verdef_out (output_bfd, &def,
6260 (Elf_External_Verdef *) p);
6261 p += sizeof (Elf_External_Verdef);
6262 if (info->create_default_symver)
6263 {
6264 /* Add a symbol representing this version. */
6265 bh = NULL;
6266 if (! (_bfd_generic_link_add_one_symbol
6267 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6268 0, NULL, FALSE,
6269 get_elf_backend_data (dynobj)->collect, &bh)))
6270 return FALSE;
6271 h = (struct elf_link_hash_entry *) bh;
6272 h->non_elf = 0;
6273 h->def_regular = 1;
6274 h->type = STT_OBJECT;
6275 h->verinfo.vertree = NULL;
6276
6277 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6278 return FALSE;
6279
6280 /* Create a duplicate of the base version with the same
6281 aux block, but different flags. */
6282 def.vd_flags = 0;
6283 def.vd_ndx = 2;
6284 def.vd_aux = sizeof (Elf_External_Verdef);
6285 if (verdefs)
6286 def.vd_next = (sizeof (Elf_External_Verdef)
6287 + sizeof (Elf_External_Verdaux));
6288 else
6289 def.vd_next = 0;
6290 _bfd_elf_swap_verdef_out (output_bfd, &def,
6291 (Elf_External_Verdef *) p);
6292 p += sizeof (Elf_External_Verdef);
6293 }
6294 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6295 (Elf_External_Verdaux *) p);
6296 p += sizeof (Elf_External_Verdaux);
6297
6298 for (t = verdefs; t != NULL; t = t->next)
6299 {
6300 unsigned int cdeps;
6301 struct bfd_elf_version_deps *n;
6302
6303 /* Don't emit the base version twice. */
6304 if (t->vernum == 0)
6305 continue;
6306
6307 cdeps = 0;
6308 for (n = t->deps; n != NULL; n = n->next)
6309 ++cdeps;
6310
6311 /* Add a symbol representing this version. */
6312 bh = NULL;
6313 if (! (_bfd_generic_link_add_one_symbol
6314 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6315 0, NULL, FALSE,
6316 get_elf_backend_data (dynobj)->collect, &bh)))
6317 return FALSE;
6318 h = (struct elf_link_hash_entry *) bh;
6319 h->non_elf = 0;
6320 h->def_regular = 1;
6321 h->type = STT_OBJECT;
6322 h->verinfo.vertree = t;
6323
6324 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6325 return FALSE;
6326
6327 def.vd_version = VER_DEF_CURRENT;
6328 def.vd_flags = 0;
6329 if (t->globals.list == NULL
6330 && t->locals.list == NULL
6331 && ! t->used)
6332 def.vd_flags |= VER_FLG_WEAK;
6333 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6334 def.vd_cnt = cdeps + 1;
6335 def.vd_hash = bfd_elf_hash (t->name);
6336 def.vd_aux = sizeof (Elf_External_Verdef);
6337 def.vd_next = 0;
6338
6339 /* If a basever node is next, it *must* be the last node in
6340 the chain, otherwise Verdef construction breaks. */
6341 if (t->next != NULL && t->next->vernum == 0)
6342 BFD_ASSERT (t->next->next == NULL);
6343
6344 if (t->next != NULL && t->next->vernum != 0)
6345 def.vd_next = (sizeof (Elf_External_Verdef)
6346 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6347
6348 _bfd_elf_swap_verdef_out (output_bfd, &def,
6349 (Elf_External_Verdef *) p);
6350 p += sizeof (Elf_External_Verdef);
6351
6352 defaux.vda_name = h->dynstr_index;
6353 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6354 h->dynstr_index);
6355 defaux.vda_next = 0;
6356 if (t->deps != NULL)
6357 defaux.vda_next = sizeof (Elf_External_Verdaux);
6358 t->name_indx = defaux.vda_name;
6359
6360 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6361 (Elf_External_Verdaux *) p);
6362 p += sizeof (Elf_External_Verdaux);
6363
6364 for (n = t->deps; n != NULL; n = n->next)
6365 {
6366 if (n->version_needed == NULL)
6367 {
6368 /* This can happen if there was an error in the
6369 version script. */
6370 defaux.vda_name = 0;
6371 }
6372 else
6373 {
6374 defaux.vda_name = n->version_needed->name_indx;
6375 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6376 defaux.vda_name);
6377 }
6378 if (n->next == NULL)
6379 defaux.vda_next = 0;
6380 else
6381 defaux.vda_next = sizeof (Elf_External_Verdaux);
6382
6383 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6384 (Elf_External_Verdaux *) p);
6385 p += sizeof (Elf_External_Verdaux);
6386 }
6387 }
6388
6389 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6390 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6391 return FALSE;
6392
6393 elf_tdata (output_bfd)->cverdefs = cdefs;
6394 }
6395
6396 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6397 {
6398 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6399 return FALSE;
6400 }
6401 else if (info->flags & DF_BIND_NOW)
6402 {
6403 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6404 return FALSE;
6405 }
6406
6407 if (info->flags_1)
6408 {
6409 if (bfd_link_executable (info))
6410 info->flags_1 &= ~ (DF_1_INITFIRST
6411 | DF_1_NODELETE
6412 | DF_1_NOOPEN);
6413 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6414 return FALSE;
6415 }
6416
6417 /* Work out the size of the version reference section. */
6418
6419 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6420 BFD_ASSERT (s != NULL);
6421 {
6422 struct elf_find_verdep_info sinfo;
6423
6424 sinfo.info = info;
6425 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6426 if (sinfo.vers == 0)
6427 sinfo.vers = 1;
6428 sinfo.failed = FALSE;
6429
6430 elf_link_hash_traverse (elf_hash_table (info),
6431 _bfd_elf_link_find_version_dependencies,
6432 &sinfo);
6433 if (sinfo.failed)
6434 return FALSE;
6435
6436 if (elf_tdata (output_bfd)->verref == NULL)
6437 s->flags |= SEC_EXCLUDE;
6438 else
6439 {
6440 Elf_Internal_Verneed *t;
6441 unsigned int size;
6442 unsigned int crefs;
6443 bfd_byte *p;
6444
6445 /* Build the version dependency section. */
6446 size = 0;
6447 crefs = 0;
6448 for (t = elf_tdata (output_bfd)->verref;
6449 t != NULL;
6450 t = t->vn_nextref)
6451 {
6452 Elf_Internal_Vernaux *a;
6453
6454 size += sizeof (Elf_External_Verneed);
6455 ++crefs;
6456 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6457 size += sizeof (Elf_External_Vernaux);
6458 }
6459
6460 s->size = size;
6461 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6462 if (s->contents == NULL)
6463 return FALSE;
6464
6465 p = s->contents;
6466 for (t = elf_tdata (output_bfd)->verref;
6467 t != NULL;
6468 t = t->vn_nextref)
6469 {
6470 unsigned int caux;
6471 Elf_Internal_Vernaux *a;
6472 bfd_size_type indx;
6473
6474 caux = 0;
6475 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6476 ++caux;
6477
6478 t->vn_version = VER_NEED_CURRENT;
6479 t->vn_cnt = caux;
6480 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6481 elf_dt_name (t->vn_bfd) != NULL
6482 ? elf_dt_name (t->vn_bfd)
6483 : lbasename (t->vn_bfd->filename),
6484 FALSE);
6485 if (indx == (bfd_size_type) -1)
6486 return FALSE;
6487 t->vn_file = indx;
6488 t->vn_aux = sizeof (Elf_External_Verneed);
6489 if (t->vn_nextref == NULL)
6490 t->vn_next = 0;
6491 else
6492 t->vn_next = (sizeof (Elf_External_Verneed)
6493 + caux * sizeof (Elf_External_Vernaux));
6494
6495 _bfd_elf_swap_verneed_out (output_bfd, t,
6496 (Elf_External_Verneed *) p);
6497 p += sizeof (Elf_External_Verneed);
6498
6499 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6500 {
6501 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6502 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6503 a->vna_nodename, FALSE);
6504 if (indx == (bfd_size_type) -1)
6505 return FALSE;
6506 a->vna_name = indx;
6507 if (a->vna_nextptr == NULL)
6508 a->vna_next = 0;
6509 else
6510 a->vna_next = sizeof (Elf_External_Vernaux);
6511
6512 _bfd_elf_swap_vernaux_out (output_bfd, a,
6513 (Elf_External_Vernaux *) p);
6514 p += sizeof (Elf_External_Vernaux);
6515 }
6516 }
6517
6518 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6519 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6520 return FALSE;
6521
6522 elf_tdata (output_bfd)->cverrefs = crefs;
6523 }
6524 }
6525
6526 if ((elf_tdata (output_bfd)->cverrefs == 0
6527 && elf_tdata (output_bfd)->cverdefs == 0)
6528 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6529 &section_sym_count) == 0)
6530 {
6531 s = bfd_get_linker_section (dynobj, ".gnu.version");
6532 s->flags |= SEC_EXCLUDE;
6533 }
6534 }
6535 return TRUE;
6536 }
6537
6538 /* Find the first non-excluded output section. We'll use its
6539 section symbol for some emitted relocs. */
6540 void
6541 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6542 {
6543 asection *s;
6544
6545 for (s = output_bfd->sections; s != NULL; s = s->next)
6546 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6547 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6548 {
6549 elf_hash_table (info)->text_index_section = s;
6550 break;
6551 }
6552 }
6553
6554 /* Find two non-excluded output sections, one for code, one for data.
6555 We'll use their section symbols for some emitted relocs. */
6556 void
6557 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6558 {
6559 asection *s;
6560
6561 /* Data first, since setting text_index_section changes
6562 _bfd_elf_link_omit_section_dynsym. */
6563 for (s = output_bfd->sections; s != NULL; s = s->next)
6564 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6565 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6566 {
6567 elf_hash_table (info)->data_index_section = s;
6568 break;
6569 }
6570
6571 for (s = output_bfd->sections; s != NULL; s = s->next)
6572 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6573 == (SEC_ALLOC | SEC_READONLY))
6574 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6575 {
6576 elf_hash_table (info)->text_index_section = s;
6577 break;
6578 }
6579
6580 if (elf_hash_table (info)->text_index_section == NULL)
6581 elf_hash_table (info)->text_index_section
6582 = elf_hash_table (info)->data_index_section;
6583 }
6584
6585 bfd_boolean
6586 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6587 {
6588 const struct elf_backend_data *bed;
6589
6590 if (!is_elf_hash_table (info->hash))
6591 return TRUE;
6592
6593 bed = get_elf_backend_data (output_bfd);
6594 (*bed->elf_backend_init_index_section) (output_bfd, info);
6595
6596 if (elf_hash_table (info)->dynamic_sections_created)
6597 {
6598 bfd *dynobj;
6599 asection *s;
6600 bfd_size_type dynsymcount;
6601 unsigned long section_sym_count;
6602 unsigned int dtagcount;
6603
6604 dynobj = elf_hash_table (info)->dynobj;
6605
6606 /* Assign dynsym indicies. In a shared library we generate a
6607 section symbol for each output section, which come first.
6608 Next come all of the back-end allocated local dynamic syms,
6609 followed by the rest of the global symbols. */
6610
6611 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6612 &section_sym_count);
6613
6614 /* Work out the size of the symbol version section. */
6615 s = bfd_get_linker_section (dynobj, ".gnu.version");
6616 BFD_ASSERT (s != NULL);
6617 if ((s->flags & SEC_EXCLUDE) == 0)
6618 {
6619 s->size = dynsymcount * sizeof (Elf_External_Versym);
6620 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6621 if (s->contents == NULL)
6622 return FALSE;
6623
6624 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6625 return FALSE;
6626 }
6627
6628 /* Set the size of the .dynsym and .hash sections. We counted
6629 the number of dynamic symbols in elf_link_add_object_symbols.
6630 We will build the contents of .dynsym and .hash when we build
6631 the final symbol table, because until then we do not know the
6632 correct value to give the symbols. We built the .dynstr
6633 section as we went along in elf_link_add_object_symbols. */
6634 s = elf_hash_table (info)->dynsym;
6635 BFD_ASSERT (s != NULL);
6636 s->size = dynsymcount * bed->s->sizeof_sym;
6637
6638 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6639 if (s->contents == NULL)
6640 return FALSE;
6641
6642 /* The first entry in .dynsym is a dummy symbol. Clear all the
6643 section syms, in case we don't output them all. */
6644 ++section_sym_count;
6645 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6646
6647 elf_hash_table (info)->bucketcount = 0;
6648
6649 /* Compute the size of the hashing table. As a side effect this
6650 computes the hash values for all the names we export. */
6651 if (info->emit_hash)
6652 {
6653 unsigned long int *hashcodes;
6654 struct hash_codes_info hashinf;
6655 bfd_size_type amt;
6656 unsigned long int nsyms;
6657 size_t bucketcount;
6658 size_t hash_entry_size;
6659
6660 /* Compute the hash values for all exported symbols. At the same
6661 time store the values in an array so that we could use them for
6662 optimizations. */
6663 amt = dynsymcount * sizeof (unsigned long int);
6664 hashcodes = (unsigned long int *) bfd_malloc (amt);
6665 if (hashcodes == NULL)
6666 return FALSE;
6667 hashinf.hashcodes = hashcodes;
6668 hashinf.error = FALSE;
6669
6670 /* Put all hash values in HASHCODES. */
6671 elf_link_hash_traverse (elf_hash_table (info),
6672 elf_collect_hash_codes, &hashinf);
6673 if (hashinf.error)
6674 {
6675 free (hashcodes);
6676 return FALSE;
6677 }
6678
6679 nsyms = hashinf.hashcodes - hashcodes;
6680 bucketcount
6681 = compute_bucket_count (info, hashcodes, nsyms, 0);
6682 free (hashcodes);
6683
6684 if (bucketcount == 0)
6685 return FALSE;
6686
6687 elf_hash_table (info)->bucketcount = bucketcount;
6688
6689 s = bfd_get_linker_section (dynobj, ".hash");
6690 BFD_ASSERT (s != NULL);
6691 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6692 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6693 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6694 if (s->contents == NULL)
6695 return FALSE;
6696
6697 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6698 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6699 s->contents + hash_entry_size);
6700 }
6701
6702 if (info->emit_gnu_hash)
6703 {
6704 size_t i, cnt;
6705 unsigned char *contents;
6706 struct collect_gnu_hash_codes cinfo;
6707 bfd_size_type amt;
6708 size_t bucketcount;
6709
6710 memset (&cinfo, 0, sizeof (cinfo));
6711
6712 /* Compute the hash values for all exported symbols. At the same
6713 time store the values in an array so that we could use them for
6714 optimizations. */
6715 amt = dynsymcount * 2 * sizeof (unsigned long int);
6716 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
6717 if (cinfo.hashcodes == NULL)
6718 return FALSE;
6719
6720 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6721 cinfo.min_dynindx = -1;
6722 cinfo.output_bfd = output_bfd;
6723 cinfo.bed = bed;
6724
6725 /* Put all hash values in HASHCODES. */
6726 elf_link_hash_traverse (elf_hash_table (info),
6727 elf_collect_gnu_hash_codes, &cinfo);
6728 if (cinfo.error)
6729 {
6730 free (cinfo.hashcodes);
6731 return FALSE;
6732 }
6733
6734 bucketcount
6735 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6736
6737 if (bucketcount == 0)
6738 {
6739 free (cinfo.hashcodes);
6740 return FALSE;
6741 }
6742
6743 s = bfd_get_linker_section (dynobj, ".gnu.hash");
6744 BFD_ASSERT (s != NULL);
6745
6746 if (cinfo.nsyms == 0)
6747 {
6748 /* Empty .gnu.hash section is special. */
6749 BFD_ASSERT (cinfo.min_dynindx == -1);
6750 free (cinfo.hashcodes);
6751 s->size = 5 * 4 + bed->s->arch_size / 8;
6752 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6753 if (contents == NULL)
6754 return FALSE;
6755 s->contents = contents;
6756 /* 1 empty bucket. */
6757 bfd_put_32 (output_bfd, 1, contents);
6758 /* SYMIDX above the special symbol 0. */
6759 bfd_put_32 (output_bfd, 1, contents + 4);
6760 /* Just one word for bitmask. */
6761 bfd_put_32 (output_bfd, 1, contents + 8);
6762 /* Only hash fn bloom filter. */
6763 bfd_put_32 (output_bfd, 0, contents + 12);
6764 /* No hashes are valid - empty bitmask. */
6765 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6766 /* No hashes in the only bucket. */
6767 bfd_put_32 (output_bfd, 0,
6768 contents + 16 + bed->s->arch_size / 8);
6769 }
6770 else
6771 {
6772 unsigned long int maskwords, maskbitslog2, x;
6773 BFD_ASSERT (cinfo.min_dynindx != -1);
6774
6775 x = cinfo.nsyms;
6776 maskbitslog2 = 1;
6777 while ((x >>= 1) != 0)
6778 ++maskbitslog2;
6779 if (maskbitslog2 < 3)
6780 maskbitslog2 = 5;
6781 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6782 maskbitslog2 = maskbitslog2 + 3;
6783 else
6784 maskbitslog2 = maskbitslog2 + 2;
6785 if (bed->s->arch_size == 64)
6786 {
6787 if (maskbitslog2 == 5)
6788 maskbitslog2 = 6;
6789 cinfo.shift1 = 6;
6790 }
6791 else
6792 cinfo.shift1 = 5;
6793 cinfo.mask = (1 << cinfo.shift1) - 1;
6794 cinfo.shift2 = maskbitslog2;
6795 cinfo.maskbits = 1 << maskbitslog2;
6796 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6797 amt = bucketcount * sizeof (unsigned long int) * 2;
6798 amt += maskwords * sizeof (bfd_vma);
6799 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
6800 if (cinfo.bitmask == NULL)
6801 {
6802 free (cinfo.hashcodes);
6803 return FALSE;
6804 }
6805
6806 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
6807 cinfo.indx = cinfo.counts + bucketcount;
6808 cinfo.symindx = dynsymcount - cinfo.nsyms;
6809 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6810
6811 /* Determine how often each hash bucket is used. */
6812 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6813 for (i = 0; i < cinfo.nsyms; ++i)
6814 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6815
6816 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6817 if (cinfo.counts[i] != 0)
6818 {
6819 cinfo.indx[i] = cnt;
6820 cnt += cinfo.counts[i];
6821 }
6822 BFD_ASSERT (cnt == dynsymcount);
6823 cinfo.bucketcount = bucketcount;
6824 cinfo.local_indx = cinfo.min_dynindx;
6825
6826 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6827 s->size += cinfo.maskbits / 8;
6828 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6829 if (contents == NULL)
6830 {
6831 free (cinfo.bitmask);
6832 free (cinfo.hashcodes);
6833 return FALSE;
6834 }
6835
6836 s->contents = contents;
6837 bfd_put_32 (output_bfd, bucketcount, contents);
6838 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6839 bfd_put_32 (output_bfd, maskwords, contents + 8);
6840 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6841 contents += 16 + cinfo.maskbits / 8;
6842
6843 for (i = 0; i < bucketcount; ++i)
6844 {
6845 if (cinfo.counts[i] == 0)
6846 bfd_put_32 (output_bfd, 0, contents);
6847 else
6848 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6849 contents += 4;
6850 }
6851
6852 cinfo.contents = contents;
6853
6854 /* Renumber dynamic symbols, populate .gnu.hash section. */
6855 elf_link_hash_traverse (elf_hash_table (info),
6856 elf_renumber_gnu_hash_syms, &cinfo);
6857
6858 contents = s->contents + 16;
6859 for (i = 0; i < maskwords; ++i)
6860 {
6861 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6862 contents);
6863 contents += bed->s->arch_size / 8;
6864 }
6865
6866 free (cinfo.bitmask);
6867 free (cinfo.hashcodes);
6868 }
6869 }
6870
6871 s = bfd_get_linker_section (dynobj, ".dynstr");
6872 BFD_ASSERT (s != NULL);
6873
6874 elf_finalize_dynstr (output_bfd, info);
6875
6876 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6877
6878 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6879 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6880 return FALSE;
6881 }
6882
6883 return TRUE;
6884 }
6885 \f
6886 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
6887
6888 static void
6889 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6890 asection *sec)
6891 {
6892 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
6893 sec->sec_info_type = SEC_INFO_TYPE_NONE;
6894 }
6895
6896 /* Finish SHF_MERGE section merging. */
6897
6898 bfd_boolean
6899 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
6900 {
6901 bfd *ibfd;
6902 asection *sec;
6903
6904 if (!is_elf_hash_table (info->hash))
6905 return FALSE;
6906
6907 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6908 if ((ibfd->flags & DYNAMIC) == 0
6909 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6910 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
6911 == get_elf_backend_data (obfd)->s->elfclass))
6912 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6913 if ((sec->flags & SEC_MERGE) != 0
6914 && !bfd_is_abs_section (sec->output_section))
6915 {
6916 struct bfd_elf_section_data *secdata;
6917
6918 secdata = elf_section_data (sec);
6919 if (! _bfd_add_merge_section (obfd,
6920 &elf_hash_table (info)->merge_info,
6921 sec, &secdata->sec_info))
6922 return FALSE;
6923 else if (secdata->sec_info)
6924 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
6925 }
6926
6927 if (elf_hash_table (info)->merge_info != NULL)
6928 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
6929 merge_sections_remove_hook);
6930 return TRUE;
6931 }
6932
6933 /* Create an entry in an ELF linker hash table. */
6934
6935 struct bfd_hash_entry *
6936 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6937 struct bfd_hash_table *table,
6938 const char *string)
6939 {
6940 /* Allocate the structure if it has not already been allocated by a
6941 subclass. */
6942 if (entry == NULL)
6943 {
6944 entry = (struct bfd_hash_entry *)
6945 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
6946 if (entry == NULL)
6947 return entry;
6948 }
6949
6950 /* Call the allocation method of the superclass. */
6951 entry = _bfd_link_hash_newfunc (entry, table, string);
6952 if (entry != NULL)
6953 {
6954 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6955 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6956
6957 /* Set local fields. */
6958 ret->indx = -1;
6959 ret->dynindx = -1;
6960 ret->got = htab->init_got_refcount;
6961 ret->plt = htab->init_plt_refcount;
6962 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
6963 - offsetof (struct elf_link_hash_entry, size)));
6964 /* Assume that we have been called by a non-ELF symbol reader.
6965 This flag is then reset by the code which reads an ELF input
6966 file. This ensures that a symbol created by a non-ELF symbol
6967 reader will have the flag set correctly. */
6968 ret->non_elf = 1;
6969 }
6970
6971 return entry;
6972 }
6973
6974 /* Copy data from an indirect symbol to its direct symbol, hiding the
6975 old indirect symbol. Also used for copying flags to a weakdef. */
6976
6977 void
6978 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
6979 struct elf_link_hash_entry *dir,
6980 struct elf_link_hash_entry *ind)
6981 {
6982 struct elf_link_hash_table *htab;
6983
6984 /* Copy down any references that we may have already seen to the
6985 symbol which just became indirect if DIR isn't a hidden versioned
6986 symbol. */
6987
6988 if (dir->versioned != versioned_hidden)
6989 {
6990 dir->ref_dynamic |= ind->ref_dynamic;
6991 dir->ref_regular |= ind->ref_regular;
6992 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
6993 dir->non_got_ref |= ind->non_got_ref;
6994 dir->needs_plt |= ind->needs_plt;
6995 dir->pointer_equality_needed |= ind->pointer_equality_needed;
6996 }
6997
6998 if (ind->root.type != bfd_link_hash_indirect)
6999 return;
7000
7001 /* Copy over the global and procedure linkage table refcount entries.
7002 These may have been already set up by a check_relocs routine. */
7003 htab = elf_hash_table (info);
7004 if (ind->got.refcount > htab->init_got_refcount.refcount)
7005 {
7006 if (dir->got.refcount < 0)
7007 dir->got.refcount = 0;
7008 dir->got.refcount += ind->got.refcount;
7009 ind->got.refcount = htab->init_got_refcount.refcount;
7010 }
7011
7012 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7013 {
7014 if (dir->plt.refcount < 0)
7015 dir->plt.refcount = 0;
7016 dir->plt.refcount += ind->plt.refcount;
7017 ind->plt.refcount = htab->init_plt_refcount.refcount;
7018 }
7019
7020 if (ind->dynindx != -1)
7021 {
7022 if (dir->dynindx != -1)
7023 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7024 dir->dynindx = ind->dynindx;
7025 dir->dynstr_index = ind->dynstr_index;
7026 ind->dynindx = -1;
7027 ind->dynstr_index = 0;
7028 }
7029 }
7030
7031 void
7032 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7033 struct elf_link_hash_entry *h,
7034 bfd_boolean force_local)
7035 {
7036 /* STT_GNU_IFUNC symbol must go through PLT. */
7037 if (h->type != STT_GNU_IFUNC)
7038 {
7039 h->plt = elf_hash_table (info)->init_plt_offset;
7040 h->needs_plt = 0;
7041 }
7042 if (force_local)
7043 {
7044 h->forced_local = 1;
7045 if (h->dynindx != -1)
7046 {
7047 h->dynindx = -1;
7048 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7049 h->dynstr_index);
7050 }
7051 }
7052 }
7053
7054 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7055 caller. */
7056
7057 bfd_boolean
7058 _bfd_elf_link_hash_table_init
7059 (struct elf_link_hash_table *table,
7060 bfd *abfd,
7061 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7062 struct bfd_hash_table *,
7063 const char *),
7064 unsigned int entsize,
7065 enum elf_target_id target_id)
7066 {
7067 bfd_boolean ret;
7068 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7069
7070 table->init_got_refcount.refcount = can_refcount - 1;
7071 table->init_plt_refcount.refcount = can_refcount - 1;
7072 table->init_got_offset.offset = -(bfd_vma) 1;
7073 table->init_plt_offset.offset = -(bfd_vma) 1;
7074 /* The first dynamic symbol is a dummy. */
7075 table->dynsymcount = 1;
7076
7077 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7078
7079 table->root.type = bfd_link_elf_hash_table;
7080 table->hash_table_id = target_id;
7081
7082 return ret;
7083 }
7084
7085 /* Create an ELF linker hash table. */
7086
7087 struct bfd_link_hash_table *
7088 _bfd_elf_link_hash_table_create (bfd *abfd)
7089 {
7090 struct elf_link_hash_table *ret;
7091 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7092
7093 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7094 if (ret == NULL)
7095 return NULL;
7096
7097 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7098 sizeof (struct elf_link_hash_entry),
7099 GENERIC_ELF_DATA))
7100 {
7101 free (ret);
7102 return NULL;
7103 }
7104 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7105
7106 return &ret->root;
7107 }
7108
7109 /* Destroy an ELF linker hash table. */
7110
7111 void
7112 _bfd_elf_link_hash_table_free (bfd *obfd)
7113 {
7114 struct elf_link_hash_table *htab;
7115
7116 htab = (struct elf_link_hash_table *) obfd->link.hash;
7117 if (htab->dynstr != NULL)
7118 _bfd_elf_strtab_free (htab->dynstr);
7119 _bfd_merge_sections_free (htab->merge_info);
7120 _bfd_generic_link_hash_table_free (obfd);
7121 }
7122
7123 /* This is a hook for the ELF emulation code in the generic linker to
7124 tell the backend linker what file name to use for the DT_NEEDED
7125 entry for a dynamic object. */
7126
7127 void
7128 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7129 {
7130 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7131 && bfd_get_format (abfd) == bfd_object)
7132 elf_dt_name (abfd) = name;
7133 }
7134
7135 int
7136 bfd_elf_get_dyn_lib_class (bfd *abfd)
7137 {
7138 int lib_class;
7139 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7140 && bfd_get_format (abfd) == bfd_object)
7141 lib_class = elf_dyn_lib_class (abfd);
7142 else
7143 lib_class = 0;
7144 return lib_class;
7145 }
7146
7147 void
7148 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7149 {
7150 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7151 && bfd_get_format (abfd) == bfd_object)
7152 elf_dyn_lib_class (abfd) = lib_class;
7153 }
7154
7155 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7156 the linker ELF emulation code. */
7157
7158 struct bfd_link_needed_list *
7159 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7160 struct bfd_link_info *info)
7161 {
7162 if (! is_elf_hash_table (info->hash))
7163 return NULL;
7164 return elf_hash_table (info)->needed;
7165 }
7166
7167 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7168 hook for the linker ELF emulation code. */
7169
7170 struct bfd_link_needed_list *
7171 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7172 struct bfd_link_info *info)
7173 {
7174 if (! is_elf_hash_table (info->hash))
7175 return NULL;
7176 return elf_hash_table (info)->runpath;
7177 }
7178
7179 /* Get the name actually used for a dynamic object for a link. This
7180 is the SONAME entry if there is one. Otherwise, it is the string
7181 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7182
7183 const char *
7184 bfd_elf_get_dt_soname (bfd *abfd)
7185 {
7186 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7187 && bfd_get_format (abfd) == bfd_object)
7188 return elf_dt_name (abfd);
7189 return NULL;
7190 }
7191
7192 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7193 the ELF linker emulation code. */
7194
7195 bfd_boolean
7196 bfd_elf_get_bfd_needed_list (bfd *abfd,
7197 struct bfd_link_needed_list **pneeded)
7198 {
7199 asection *s;
7200 bfd_byte *dynbuf = NULL;
7201 unsigned int elfsec;
7202 unsigned long shlink;
7203 bfd_byte *extdyn, *extdynend;
7204 size_t extdynsize;
7205 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7206
7207 *pneeded = NULL;
7208
7209 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7210 || bfd_get_format (abfd) != bfd_object)
7211 return TRUE;
7212
7213 s = bfd_get_section_by_name (abfd, ".dynamic");
7214 if (s == NULL || s->size == 0)
7215 return TRUE;
7216
7217 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7218 goto error_return;
7219
7220 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7221 if (elfsec == SHN_BAD)
7222 goto error_return;
7223
7224 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7225
7226 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7227 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7228
7229 extdyn = dynbuf;
7230 extdynend = extdyn + s->size;
7231 for (; extdyn < extdynend; extdyn += extdynsize)
7232 {
7233 Elf_Internal_Dyn dyn;
7234
7235 (*swap_dyn_in) (abfd, extdyn, &dyn);
7236
7237 if (dyn.d_tag == DT_NULL)
7238 break;
7239
7240 if (dyn.d_tag == DT_NEEDED)
7241 {
7242 const char *string;
7243 struct bfd_link_needed_list *l;
7244 unsigned int tagv = dyn.d_un.d_val;
7245 bfd_size_type amt;
7246
7247 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7248 if (string == NULL)
7249 goto error_return;
7250
7251 amt = sizeof *l;
7252 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7253 if (l == NULL)
7254 goto error_return;
7255
7256 l->by = abfd;
7257 l->name = string;
7258 l->next = *pneeded;
7259 *pneeded = l;
7260 }
7261 }
7262
7263 free (dynbuf);
7264
7265 return TRUE;
7266
7267 error_return:
7268 if (dynbuf != NULL)
7269 free (dynbuf);
7270 return FALSE;
7271 }
7272
7273 struct elf_symbuf_symbol
7274 {
7275 unsigned long st_name; /* Symbol name, index in string tbl */
7276 unsigned char st_info; /* Type and binding attributes */
7277 unsigned char st_other; /* Visibilty, and target specific */
7278 };
7279
7280 struct elf_symbuf_head
7281 {
7282 struct elf_symbuf_symbol *ssym;
7283 bfd_size_type count;
7284 unsigned int st_shndx;
7285 };
7286
7287 struct elf_symbol
7288 {
7289 union
7290 {
7291 Elf_Internal_Sym *isym;
7292 struct elf_symbuf_symbol *ssym;
7293 } u;
7294 const char *name;
7295 };
7296
7297 /* Sort references to symbols by ascending section number. */
7298
7299 static int
7300 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7301 {
7302 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7303 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7304
7305 return s1->st_shndx - s2->st_shndx;
7306 }
7307
7308 static int
7309 elf_sym_name_compare (const void *arg1, const void *arg2)
7310 {
7311 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7312 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7313 return strcmp (s1->name, s2->name);
7314 }
7315
7316 static struct elf_symbuf_head *
7317 elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
7318 {
7319 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7320 struct elf_symbuf_symbol *ssym;
7321 struct elf_symbuf_head *ssymbuf, *ssymhead;
7322 bfd_size_type i, shndx_count, total_size;
7323
7324 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7325 if (indbuf == NULL)
7326 return NULL;
7327
7328 for (ind = indbuf, i = 0; i < symcount; i++)
7329 if (isymbuf[i].st_shndx != SHN_UNDEF)
7330 *ind++ = &isymbuf[i];
7331 indbufend = ind;
7332
7333 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7334 elf_sort_elf_symbol);
7335
7336 shndx_count = 0;
7337 if (indbufend > indbuf)
7338 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7339 if (ind[0]->st_shndx != ind[1]->st_shndx)
7340 shndx_count++;
7341
7342 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7343 + (indbufend - indbuf) * sizeof (*ssym));
7344 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7345 if (ssymbuf == NULL)
7346 {
7347 free (indbuf);
7348 return NULL;
7349 }
7350
7351 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7352 ssymbuf->ssym = NULL;
7353 ssymbuf->count = shndx_count;
7354 ssymbuf->st_shndx = 0;
7355 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7356 {
7357 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7358 {
7359 ssymhead++;
7360 ssymhead->ssym = ssym;
7361 ssymhead->count = 0;
7362 ssymhead->st_shndx = (*ind)->st_shndx;
7363 }
7364 ssym->st_name = (*ind)->st_name;
7365 ssym->st_info = (*ind)->st_info;
7366 ssym->st_other = (*ind)->st_other;
7367 ssymhead->count++;
7368 }
7369 BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
7370 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7371 == total_size));
7372
7373 free (indbuf);
7374 return ssymbuf;
7375 }
7376
7377 /* Check if 2 sections define the same set of local and global
7378 symbols. */
7379
7380 static bfd_boolean
7381 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7382 struct bfd_link_info *info)
7383 {
7384 bfd *bfd1, *bfd2;
7385 const struct elf_backend_data *bed1, *bed2;
7386 Elf_Internal_Shdr *hdr1, *hdr2;
7387 bfd_size_type symcount1, symcount2;
7388 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7389 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7390 Elf_Internal_Sym *isym, *isymend;
7391 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7392 bfd_size_type count1, count2, i;
7393 unsigned int shndx1, shndx2;
7394 bfd_boolean result;
7395
7396 bfd1 = sec1->owner;
7397 bfd2 = sec2->owner;
7398
7399 /* Both sections have to be in ELF. */
7400 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7401 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7402 return FALSE;
7403
7404 if (elf_section_type (sec1) != elf_section_type (sec2))
7405 return FALSE;
7406
7407 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7408 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7409 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7410 return FALSE;
7411
7412 bed1 = get_elf_backend_data (bfd1);
7413 bed2 = get_elf_backend_data (bfd2);
7414 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7415 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7416 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7417 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7418
7419 if (symcount1 == 0 || symcount2 == 0)
7420 return FALSE;
7421
7422 result = FALSE;
7423 isymbuf1 = NULL;
7424 isymbuf2 = NULL;
7425 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7426 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7427
7428 if (ssymbuf1 == NULL)
7429 {
7430 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7431 NULL, NULL, NULL);
7432 if (isymbuf1 == NULL)
7433 goto done;
7434
7435 if (!info->reduce_memory_overheads)
7436 elf_tdata (bfd1)->symbuf = ssymbuf1
7437 = elf_create_symbuf (symcount1, isymbuf1);
7438 }
7439
7440 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7441 {
7442 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7443 NULL, NULL, NULL);
7444 if (isymbuf2 == NULL)
7445 goto done;
7446
7447 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7448 elf_tdata (bfd2)->symbuf = ssymbuf2
7449 = elf_create_symbuf (symcount2, isymbuf2);
7450 }
7451
7452 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7453 {
7454 /* Optimized faster version. */
7455 bfd_size_type lo, hi, mid;
7456 struct elf_symbol *symp;
7457 struct elf_symbuf_symbol *ssym, *ssymend;
7458
7459 lo = 0;
7460 hi = ssymbuf1->count;
7461 ssymbuf1++;
7462 count1 = 0;
7463 while (lo < hi)
7464 {
7465 mid = (lo + hi) / 2;
7466 if (shndx1 < ssymbuf1[mid].st_shndx)
7467 hi = mid;
7468 else if (shndx1 > ssymbuf1[mid].st_shndx)
7469 lo = mid + 1;
7470 else
7471 {
7472 count1 = ssymbuf1[mid].count;
7473 ssymbuf1 += mid;
7474 break;
7475 }
7476 }
7477
7478 lo = 0;
7479 hi = ssymbuf2->count;
7480 ssymbuf2++;
7481 count2 = 0;
7482 while (lo < hi)
7483 {
7484 mid = (lo + hi) / 2;
7485 if (shndx2 < ssymbuf2[mid].st_shndx)
7486 hi = mid;
7487 else if (shndx2 > ssymbuf2[mid].st_shndx)
7488 lo = mid + 1;
7489 else
7490 {
7491 count2 = ssymbuf2[mid].count;
7492 ssymbuf2 += mid;
7493 break;
7494 }
7495 }
7496
7497 if (count1 == 0 || count2 == 0 || count1 != count2)
7498 goto done;
7499
7500 symtable1
7501 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7502 symtable2
7503 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
7504 if (symtable1 == NULL || symtable2 == NULL)
7505 goto done;
7506
7507 symp = symtable1;
7508 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7509 ssym < ssymend; ssym++, symp++)
7510 {
7511 symp->u.ssym = ssym;
7512 symp->name = bfd_elf_string_from_elf_section (bfd1,
7513 hdr1->sh_link,
7514 ssym->st_name);
7515 }
7516
7517 symp = symtable2;
7518 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7519 ssym < ssymend; ssym++, symp++)
7520 {
7521 symp->u.ssym = ssym;
7522 symp->name = bfd_elf_string_from_elf_section (bfd2,
7523 hdr2->sh_link,
7524 ssym->st_name);
7525 }
7526
7527 /* Sort symbol by name. */
7528 qsort (symtable1, count1, sizeof (struct elf_symbol),
7529 elf_sym_name_compare);
7530 qsort (symtable2, count1, sizeof (struct elf_symbol),
7531 elf_sym_name_compare);
7532
7533 for (i = 0; i < count1; i++)
7534 /* Two symbols must have the same binding, type and name. */
7535 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7536 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7537 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7538 goto done;
7539
7540 result = TRUE;
7541 goto done;
7542 }
7543
7544 symtable1 = (struct elf_symbol *)
7545 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7546 symtable2 = (struct elf_symbol *)
7547 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7548 if (symtable1 == NULL || symtable2 == NULL)
7549 goto done;
7550
7551 /* Count definitions in the section. */
7552 count1 = 0;
7553 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7554 if (isym->st_shndx == shndx1)
7555 symtable1[count1++].u.isym = isym;
7556
7557 count2 = 0;
7558 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7559 if (isym->st_shndx == shndx2)
7560 symtable2[count2++].u.isym = isym;
7561
7562 if (count1 == 0 || count2 == 0 || count1 != count2)
7563 goto done;
7564
7565 for (i = 0; i < count1; i++)
7566 symtable1[i].name
7567 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7568 symtable1[i].u.isym->st_name);
7569
7570 for (i = 0; i < count2; i++)
7571 symtable2[i].name
7572 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7573 symtable2[i].u.isym->st_name);
7574
7575 /* Sort symbol by name. */
7576 qsort (symtable1, count1, sizeof (struct elf_symbol),
7577 elf_sym_name_compare);
7578 qsort (symtable2, count1, sizeof (struct elf_symbol),
7579 elf_sym_name_compare);
7580
7581 for (i = 0; i < count1; i++)
7582 /* Two symbols must have the same binding, type and name. */
7583 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7584 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7585 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7586 goto done;
7587
7588 result = TRUE;
7589
7590 done:
7591 if (symtable1)
7592 free (symtable1);
7593 if (symtable2)
7594 free (symtable2);
7595 if (isymbuf1)
7596 free (isymbuf1);
7597 if (isymbuf2)
7598 free (isymbuf2);
7599
7600 return result;
7601 }
7602
7603 /* Return TRUE if 2 section types are compatible. */
7604
7605 bfd_boolean
7606 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7607 bfd *bbfd, const asection *bsec)
7608 {
7609 if (asec == NULL
7610 || bsec == NULL
7611 || abfd->xvec->flavour != bfd_target_elf_flavour
7612 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7613 return TRUE;
7614
7615 return elf_section_type (asec) == elf_section_type (bsec);
7616 }
7617 \f
7618 /* Final phase of ELF linker. */
7619
7620 /* A structure we use to avoid passing large numbers of arguments. */
7621
7622 struct elf_final_link_info
7623 {
7624 /* General link information. */
7625 struct bfd_link_info *info;
7626 /* Output BFD. */
7627 bfd *output_bfd;
7628 /* Symbol string table. */
7629 struct elf_strtab_hash *symstrtab;
7630 /* .hash section. */
7631 asection *hash_sec;
7632 /* symbol version section (.gnu.version). */
7633 asection *symver_sec;
7634 /* Buffer large enough to hold contents of any section. */
7635 bfd_byte *contents;
7636 /* Buffer large enough to hold external relocs of any section. */
7637 void *external_relocs;
7638 /* Buffer large enough to hold internal relocs of any section. */
7639 Elf_Internal_Rela *internal_relocs;
7640 /* Buffer large enough to hold external local symbols of any input
7641 BFD. */
7642 bfd_byte *external_syms;
7643 /* And a buffer for symbol section indices. */
7644 Elf_External_Sym_Shndx *locsym_shndx;
7645 /* Buffer large enough to hold internal local symbols of any input
7646 BFD. */
7647 Elf_Internal_Sym *internal_syms;
7648 /* Array large enough to hold a symbol index for each local symbol
7649 of any input BFD. */
7650 long *indices;
7651 /* Array large enough to hold a section pointer for each local
7652 symbol of any input BFD. */
7653 asection **sections;
7654 /* Buffer for SHT_SYMTAB_SHNDX section. */
7655 Elf_External_Sym_Shndx *symshndxbuf;
7656 /* Number of STT_FILE syms seen. */
7657 size_t filesym_count;
7658 };
7659
7660 /* This struct is used to pass information to elf_link_output_extsym. */
7661
7662 struct elf_outext_info
7663 {
7664 bfd_boolean failed;
7665 bfd_boolean localsyms;
7666 bfd_boolean file_sym_done;
7667 struct elf_final_link_info *flinfo;
7668 };
7669
7670
7671 /* Support for evaluating a complex relocation.
7672
7673 Complex relocations are generalized, self-describing relocations. The
7674 implementation of them consists of two parts: complex symbols, and the
7675 relocations themselves.
7676
7677 The relocations are use a reserved elf-wide relocation type code (R_RELC
7678 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7679 information (start bit, end bit, word width, etc) into the addend. This
7680 information is extracted from CGEN-generated operand tables within gas.
7681
7682 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7683 internal) representing prefix-notation expressions, including but not
7684 limited to those sorts of expressions normally encoded as addends in the
7685 addend field. The symbol mangling format is:
7686
7687 <node> := <literal>
7688 | <unary-operator> ':' <node>
7689 | <binary-operator> ':' <node> ':' <node>
7690 ;
7691
7692 <literal> := 's' <digits=N> ':' <N character symbol name>
7693 | 'S' <digits=N> ':' <N character section name>
7694 | '#' <hexdigits>
7695 ;
7696
7697 <binary-operator> := as in C
7698 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7699
7700 static void
7701 set_symbol_value (bfd *bfd_with_globals,
7702 Elf_Internal_Sym *isymbuf,
7703 size_t locsymcount,
7704 size_t symidx,
7705 bfd_vma val)
7706 {
7707 struct elf_link_hash_entry **sym_hashes;
7708 struct elf_link_hash_entry *h;
7709 size_t extsymoff = locsymcount;
7710
7711 if (symidx < locsymcount)
7712 {
7713 Elf_Internal_Sym *sym;
7714
7715 sym = isymbuf + symidx;
7716 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7717 {
7718 /* It is a local symbol: move it to the
7719 "absolute" section and give it a value. */
7720 sym->st_shndx = SHN_ABS;
7721 sym->st_value = val;
7722 return;
7723 }
7724 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7725 extsymoff = 0;
7726 }
7727
7728 /* It is a global symbol: set its link type
7729 to "defined" and give it a value. */
7730
7731 sym_hashes = elf_sym_hashes (bfd_with_globals);
7732 h = sym_hashes [symidx - extsymoff];
7733 while (h->root.type == bfd_link_hash_indirect
7734 || h->root.type == bfd_link_hash_warning)
7735 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7736 h->root.type = bfd_link_hash_defined;
7737 h->root.u.def.value = val;
7738 h->root.u.def.section = bfd_abs_section_ptr;
7739 }
7740
7741 static bfd_boolean
7742 resolve_symbol (const char *name,
7743 bfd *input_bfd,
7744 struct elf_final_link_info *flinfo,
7745 bfd_vma *result,
7746 Elf_Internal_Sym *isymbuf,
7747 size_t locsymcount)
7748 {
7749 Elf_Internal_Sym *sym;
7750 struct bfd_link_hash_entry *global_entry;
7751 const char *candidate = NULL;
7752 Elf_Internal_Shdr *symtab_hdr;
7753 size_t i;
7754
7755 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7756
7757 for (i = 0; i < locsymcount; ++ i)
7758 {
7759 sym = isymbuf + i;
7760
7761 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7762 continue;
7763
7764 candidate = bfd_elf_string_from_elf_section (input_bfd,
7765 symtab_hdr->sh_link,
7766 sym->st_name);
7767 #ifdef DEBUG
7768 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7769 name, candidate, (unsigned long) sym->st_value);
7770 #endif
7771 if (candidate && strcmp (candidate, name) == 0)
7772 {
7773 asection *sec = flinfo->sections [i];
7774
7775 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7776 *result += sec->output_offset + sec->output_section->vma;
7777 #ifdef DEBUG
7778 printf ("Found symbol with value %8.8lx\n",
7779 (unsigned long) *result);
7780 #endif
7781 return TRUE;
7782 }
7783 }
7784
7785 /* Hmm, haven't found it yet. perhaps it is a global. */
7786 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
7787 FALSE, FALSE, TRUE);
7788 if (!global_entry)
7789 return FALSE;
7790
7791 if (global_entry->type == bfd_link_hash_defined
7792 || global_entry->type == bfd_link_hash_defweak)
7793 {
7794 *result = (global_entry->u.def.value
7795 + global_entry->u.def.section->output_section->vma
7796 + global_entry->u.def.section->output_offset);
7797 #ifdef DEBUG
7798 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7799 global_entry->root.string, (unsigned long) *result);
7800 #endif
7801 return TRUE;
7802 }
7803
7804 return FALSE;
7805 }
7806
7807 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
7808 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
7809 names like "foo.end" which is the end address of section "foo". */
7810
7811 static bfd_boolean
7812 resolve_section (const char *name,
7813 asection *sections,
7814 bfd_vma *result,
7815 bfd * abfd)
7816 {
7817 asection *curr;
7818 unsigned int len;
7819
7820 for (curr = sections; curr; curr = curr->next)
7821 if (strcmp (curr->name, name) == 0)
7822 {
7823 *result = curr->vma;
7824 return TRUE;
7825 }
7826
7827 /* Hmm. still haven't found it. try pseudo-section names. */
7828 /* FIXME: This could be coded more efficiently... */
7829 for (curr = sections; curr; curr = curr->next)
7830 {
7831 len = strlen (curr->name);
7832 if (len > strlen (name))
7833 continue;
7834
7835 if (strncmp (curr->name, name, len) == 0)
7836 {
7837 if (strncmp (".end", name + len, 4) == 0)
7838 {
7839 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
7840 return TRUE;
7841 }
7842
7843 /* Insert more pseudo-section names here, if you like. */
7844 }
7845 }
7846
7847 return FALSE;
7848 }
7849
7850 static void
7851 undefined_reference (const char *reftype, const char *name)
7852 {
7853 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7854 reftype, name);
7855 }
7856
7857 static bfd_boolean
7858 eval_symbol (bfd_vma *result,
7859 const char **symp,
7860 bfd *input_bfd,
7861 struct elf_final_link_info *flinfo,
7862 bfd_vma dot,
7863 Elf_Internal_Sym *isymbuf,
7864 size_t locsymcount,
7865 int signed_p)
7866 {
7867 size_t len;
7868 size_t symlen;
7869 bfd_vma a;
7870 bfd_vma b;
7871 char symbuf[4096];
7872 const char *sym = *symp;
7873 const char *symend;
7874 bfd_boolean symbol_is_section = FALSE;
7875
7876 len = strlen (sym);
7877 symend = sym + len;
7878
7879 if (len < 1 || len > sizeof (symbuf))
7880 {
7881 bfd_set_error (bfd_error_invalid_operation);
7882 return FALSE;
7883 }
7884
7885 switch (* sym)
7886 {
7887 case '.':
7888 *result = dot;
7889 *symp = sym + 1;
7890 return TRUE;
7891
7892 case '#':
7893 ++sym;
7894 *result = strtoul (sym, (char **) symp, 16);
7895 return TRUE;
7896
7897 case 'S':
7898 symbol_is_section = TRUE;
7899 case 's':
7900 ++sym;
7901 symlen = strtol (sym, (char **) symp, 10);
7902 sym = *symp + 1; /* Skip the trailing ':'. */
7903
7904 if (symend < sym || symlen + 1 > sizeof (symbuf))
7905 {
7906 bfd_set_error (bfd_error_invalid_operation);
7907 return FALSE;
7908 }
7909
7910 memcpy (symbuf, sym, symlen);
7911 symbuf[symlen] = '\0';
7912 *symp = sym + symlen;
7913
7914 /* Is it always possible, with complex symbols, that gas "mis-guessed"
7915 the symbol as a section, or vice-versa. so we're pretty liberal in our
7916 interpretation here; section means "try section first", not "must be a
7917 section", and likewise with symbol. */
7918
7919 if (symbol_is_section)
7920 {
7921 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
7922 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
7923 isymbuf, locsymcount))
7924 {
7925 undefined_reference ("section", symbuf);
7926 return FALSE;
7927 }
7928 }
7929 else
7930 {
7931 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
7932 isymbuf, locsymcount)
7933 && !resolve_section (symbuf, flinfo->output_bfd->sections,
7934 result, input_bfd))
7935 {
7936 undefined_reference ("symbol", symbuf);
7937 return FALSE;
7938 }
7939 }
7940
7941 return TRUE;
7942
7943 /* All that remains are operators. */
7944
7945 #define UNARY_OP(op) \
7946 if (strncmp (sym, #op, strlen (#op)) == 0) \
7947 { \
7948 sym += strlen (#op); \
7949 if (*sym == ':') \
7950 ++sym; \
7951 *symp = sym; \
7952 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
7953 isymbuf, locsymcount, signed_p)) \
7954 return FALSE; \
7955 if (signed_p) \
7956 *result = op ((bfd_signed_vma) a); \
7957 else \
7958 *result = op a; \
7959 return TRUE; \
7960 }
7961
7962 #define BINARY_OP(op) \
7963 if (strncmp (sym, #op, strlen (#op)) == 0) \
7964 { \
7965 sym += strlen (#op); \
7966 if (*sym == ':') \
7967 ++sym; \
7968 *symp = sym; \
7969 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
7970 isymbuf, locsymcount, signed_p)) \
7971 return FALSE; \
7972 ++*symp; \
7973 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
7974 isymbuf, locsymcount, signed_p)) \
7975 return FALSE; \
7976 if (signed_p) \
7977 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
7978 else \
7979 *result = a op b; \
7980 return TRUE; \
7981 }
7982
7983 default:
7984 UNARY_OP (0-);
7985 BINARY_OP (<<);
7986 BINARY_OP (>>);
7987 BINARY_OP (==);
7988 BINARY_OP (!=);
7989 BINARY_OP (<=);
7990 BINARY_OP (>=);
7991 BINARY_OP (&&);
7992 BINARY_OP (||);
7993 UNARY_OP (~);
7994 UNARY_OP (!);
7995 BINARY_OP (*);
7996 BINARY_OP (/);
7997 BINARY_OP (%);
7998 BINARY_OP (^);
7999 BINARY_OP (|);
8000 BINARY_OP (&);
8001 BINARY_OP (+);
8002 BINARY_OP (-);
8003 BINARY_OP (<);
8004 BINARY_OP (>);
8005 #undef UNARY_OP
8006 #undef BINARY_OP
8007 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8008 bfd_set_error (bfd_error_invalid_operation);
8009 return FALSE;
8010 }
8011 }
8012
8013 static void
8014 put_value (bfd_vma size,
8015 unsigned long chunksz,
8016 bfd *input_bfd,
8017 bfd_vma x,
8018 bfd_byte *location)
8019 {
8020 location += (size - chunksz);
8021
8022 for (; size; size -= chunksz, location -= chunksz)
8023 {
8024 switch (chunksz)
8025 {
8026 case 1:
8027 bfd_put_8 (input_bfd, x, location);
8028 x >>= 8;
8029 break;
8030 case 2:
8031 bfd_put_16 (input_bfd, x, location);
8032 x >>= 16;
8033 break;
8034 case 4:
8035 bfd_put_32 (input_bfd, x, location);
8036 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8037 x >>= 16;
8038 x >>= 16;
8039 break;
8040 #ifdef BFD64
8041 case 8:
8042 bfd_put_64 (input_bfd, x, location);
8043 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8044 x >>= 32;
8045 x >>= 32;
8046 break;
8047 #endif
8048 default:
8049 abort ();
8050 break;
8051 }
8052 }
8053 }
8054
8055 static bfd_vma
8056 get_value (bfd_vma size,
8057 unsigned long chunksz,
8058 bfd *input_bfd,
8059 bfd_byte *location)
8060 {
8061 int shift;
8062 bfd_vma x = 0;
8063
8064 /* Sanity checks. */
8065 BFD_ASSERT (chunksz <= sizeof (x)
8066 && size >= chunksz
8067 && chunksz != 0
8068 && (size % chunksz) == 0
8069 && input_bfd != NULL
8070 && location != NULL);
8071
8072 if (chunksz == sizeof (x))
8073 {
8074 BFD_ASSERT (size == chunksz);
8075
8076 /* Make sure that we do not perform an undefined shift operation.
8077 We know that size == chunksz so there will only be one iteration
8078 of the loop below. */
8079 shift = 0;
8080 }
8081 else
8082 shift = 8 * chunksz;
8083
8084 for (; size; size -= chunksz, location += chunksz)
8085 {
8086 switch (chunksz)
8087 {
8088 case 1:
8089 x = (x << shift) | bfd_get_8 (input_bfd, location);
8090 break;
8091 case 2:
8092 x = (x << shift) | bfd_get_16 (input_bfd, location);
8093 break;
8094 case 4:
8095 x = (x << shift) | bfd_get_32 (input_bfd, location);
8096 break;
8097 #ifdef BFD64
8098 case 8:
8099 x = (x << shift) | bfd_get_64 (input_bfd, location);
8100 break;
8101 #endif
8102 default:
8103 abort ();
8104 }
8105 }
8106 return x;
8107 }
8108
8109 static void
8110 decode_complex_addend (unsigned long *start, /* in bits */
8111 unsigned long *oplen, /* in bits */
8112 unsigned long *len, /* in bits */
8113 unsigned long *wordsz, /* in bytes */
8114 unsigned long *chunksz, /* in bytes */
8115 unsigned long *lsb0_p,
8116 unsigned long *signed_p,
8117 unsigned long *trunc_p,
8118 unsigned long encoded)
8119 {
8120 * start = encoded & 0x3F;
8121 * len = (encoded >> 6) & 0x3F;
8122 * oplen = (encoded >> 12) & 0x3F;
8123 * wordsz = (encoded >> 18) & 0xF;
8124 * chunksz = (encoded >> 22) & 0xF;
8125 * lsb0_p = (encoded >> 27) & 1;
8126 * signed_p = (encoded >> 28) & 1;
8127 * trunc_p = (encoded >> 29) & 1;
8128 }
8129
8130 bfd_reloc_status_type
8131 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8132 asection *input_section ATTRIBUTE_UNUSED,
8133 bfd_byte *contents,
8134 Elf_Internal_Rela *rel,
8135 bfd_vma relocation)
8136 {
8137 bfd_vma shift, x, mask;
8138 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8139 bfd_reloc_status_type r;
8140
8141 /* Perform this reloc, since it is complex.
8142 (this is not to say that it necessarily refers to a complex
8143 symbol; merely that it is a self-describing CGEN based reloc.
8144 i.e. the addend has the complete reloc information (bit start, end,
8145 word size, etc) encoded within it.). */
8146
8147 decode_complex_addend (&start, &oplen, &len, &wordsz,
8148 &chunksz, &lsb0_p, &signed_p,
8149 &trunc_p, rel->r_addend);
8150
8151 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8152
8153 if (lsb0_p)
8154 shift = (start + 1) - len;
8155 else
8156 shift = (8 * wordsz) - (start + len);
8157
8158 x = get_value (wordsz, chunksz, input_bfd,
8159 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8160
8161 #ifdef DEBUG
8162 printf ("Doing complex reloc: "
8163 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8164 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8165 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8166 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8167 oplen, (unsigned long) x, (unsigned long) mask,
8168 (unsigned long) relocation);
8169 #endif
8170
8171 r = bfd_reloc_ok;
8172 if (! trunc_p)
8173 /* Now do an overflow check. */
8174 r = bfd_check_overflow ((signed_p
8175 ? complain_overflow_signed
8176 : complain_overflow_unsigned),
8177 len, 0, (8 * wordsz),
8178 relocation);
8179
8180 /* Do the deed. */
8181 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8182
8183 #ifdef DEBUG
8184 printf (" relocation: %8.8lx\n"
8185 " shifted mask: %8.8lx\n"
8186 " shifted/masked reloc: %8.8lx\n"
8187 " result: %8.8lx\n",
8188 (unsigned long) relocation, (unsigned long) (mask << shift),
8189 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8190 #endif
8191 put_value (wordsz, chunksz, input_bfd, x,
8192 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8193 return r;
8194 }
8195
8196 /* Functions to read r_offset from external (target order) reloc
8197 entry. Faster than bfd_getl32 et al, because we let the compiler
8198 know the value is aligned. */
8199
8200 static bfd_vma
8201 ext32l_r_offset (const void *p)
8202 {
8203 union aligned32
8204 {
8205 uint32_t v;
8206 unsigned char c[4];
8207 };
8208 const union aligned32 *a
8209 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8210
8211 uint32_t aval = ( (uint32_t) a->c[0]
8212 | (uint32_t) a->c[1] << 8
8213 | (uint32_t) a->c[2] << 16
8214 | (uint32_t) a->c[3] << 24);
8215 return aval;
8216 }
8217
8218 static bfd_vma
8219 ext32b_r_offset (const void *p)
8220 {
8221 union aligned32
8222 {
8223 uint32_t v;
8224 unsigned char c[4];
8225 };
8226 const union aligned32 *a
8227 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8228
8229 uint32_t aval = ( (uint32_t) a->c[0] << 24
8230 | (uint32_t) a->c[1] << 16
8231 | (uint32_t) a->c[2] << 8
8232 | (uint32_t) a->c[3]);
8233 return aval;
8234 }
8235
8236 #ifdef BFD_HOST_64_BIT
8237 static bfd_vma
8238 ext64l_r_offset (const void *p)
8239 {
8240 union aligned64
8241 {
8242 uint64_t v;
8243 unsigned char c[8];
8244 };
8245 const union aligned64 *a
8246 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8247
8248 uint64_t aval = ( (uint64_t) a->c[0]
8249 | (uint64_t) a->c[1] << 8
8250 | (uint64_t) a->c[2] << 16
8251 | (uint64_t) a->c[3] << 24
8252 | (uint64_t) a->c[4] << 32
8253 | (uint64_t) a->c[5] << 40
8254 | (uint64_t) a->c[6] << 48
8255 | (uint64_t) a->c[7] << 56);
8256 return aval;
8257 }
8258
8259 static bfd_vma
8260 ext64b_r_offset (const void *p)
8261 {
8262 union aligned64
8263 {
8264 uint64_t v;
8265 unsigned char c[8];
8266 };
8267 const union aligned64 *a
8268 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8269
8270 uint64_t aval = ( (uint64_t) a->c[0] << 56
8271 | (uint64_t) a->c[1] << 48
8272 | (uint64_t) a->c[2] << 40
8273 | (uint64_t) a->c[3] << 32
8274 | (uint64_t) a->c[4] << 24
8275 | (uint64_t) a->c[5] << 16
8276 | (uint64_t) a->c[6] << 8
8277 | (uint64_t) a->c[7]);
8278 return aval;
8279 }
8280 #endif
8281
8282 /* When performing a relocatable link, the input relocations are
8283 preserved. But, if they reference global symbols, the indices
8284 referenced must be updated. Update all the relocations found in
8285 RELDATA. */
8286
8287 static bfd_boolean
8288 elf_link_adjust_relocs (bfd *abfd,
8289 struct bfd_elf_section_reloc_data *reldata,
8290 bfd_boolean sort)
8291 {
8292 unsigned int i;
8293 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8294 bfd_byte *erela;
8295 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8296 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8297 bfd_vma r_type_mask;
8298 int r_sym_shift;
8299 unsigned int count = reldata->count;
8300 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8301
8302 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8303 {
8304 swap_in = bed->s->swap_reloc_in;
8305 swap_out = bed->s->swap_reloc_out;
8306 }
8307 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8308 {
8309 swap_in = bed->s->swap_reloca_in;
8310 swap_out = bed->s->swap_reloca_out;
8311 }
8312 else
8313 abort ();
8314
8315 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8316 abort ();
8317
8318 if (bed->s->arch_size == 32)
8319 {
8320 r_type_mask = 0xff;
8321 r_sym_shift = 8;
8322 }
8323 else
8324 {
8325 r_type_mask = 0xffffffff;
8326 r_sym_shift = 32;
8327 }
8328
8329 erela = reldata->hdr->contents;
8330 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8331 {
8332 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8333 unsigned int j;
8334
8335 if (*rel_hash == NULL)
8336 continue;
8337
8338 BFD_ASSERT ((*rel_hash)->indx >= 0);
8339
8340 (*swap_in) (abfd, erela, irela);
8341 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8342 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8343 | (irela[j].r_info & r_type_mask));
8344 (*swap_out) (abfd, irela, erela);
8345 }
8346
8347 if (sort && count != 0)
8348 {
8349 bfd_vma (*ext_r_off) (const void *);
8350 bfd_vma r_off;
8351 size_t elt_size;
8352 bfd_byte *base, *end, *p, *loc;
8353 bfd_byte *buf = NULL;
8354
8355 if (bed->s->arch_size == 32)
8356 {
8357 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8358 ext_r_off = ext32l_r_offset;
8359 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8360 ext_r_off = ext32b_r_offset;
8361 else
8362 abort ();
8363 }
8364 else
8365 {
8366 #ifdef BFD_HOST_64_BIT
8367 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8368 ext_r_off = ext64l_r_offset;
8369 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8370 ext_r_off = ext64b_r_offset;
8371 else
8372 #endif
8373 abort ();
8374 }
8375
8376 /* Must use a stable sort here. A modified insertion sort,
8377 since the relocs are mostly sorted already. */
8378 elt_size = reldata->hdr->sh_entsize;
8379 base = reldata->hdr->contents;
8380 end = base + count * elt_size;
8381 if (elt_size > sizeof (Elf64_External_Rela))
8382 abort ();
8383
8384 /* Ensure the first element is lowest. This acts as a sentinel,
8385 speeding the main loop below. */
8386 r_off = (*ext_r_off) (base);
8387 for (p = loc = base; (p += elt_size) < end; )
8388 {
8389 bfd_vma r_off2 = (*ext_r_off) (p);
8390 if (r_off > r_off2)
8391 {
8392 r_off = r_off2;
8393 loc = p;
8394 }
8395 }
8396 if (loc != base)
8397 {
8398 /* Don't just swap *base and *loc as that changes the order
8399 of the original base[0] and base[1] if they happen to
8400 have the same r_offset. */
8401 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8402 memcpy (onebuf, loc, elt_size);
8403 memmove (base + elt_size, base, loc - base);
8404 memcpy (base, onebuf, elt_size);
8405 }
8406
8407 for (p = base + elt_size; (p += elt_size) < end; )
8408 {
8409 /* base to p is sorted, *p is next to insert. */
8410 r_off = (*ext_r_off) (p);
8411 /* Search the sorted region for location to insert. */
8412 loc = p - elt_size;
8413 while (r_off < (*ext_r_off) (loc))
8414 loc -= elt_size;
8415 loc += elt_size;
8416 if (loc != p)
8417 {
8418 /* Chances are there is a run of relocs to insert here,
8419 from one of more input files. Files are not always
8420 linked in order due to the way elf_link_input_bfd is
8421 called. See pr17666. */
8422 size_t sortlen = p - loc;
8423 bfd_vma r_off2 = (*ext_r_off) (loc);
8424 size_t runlen = elt_size;
8425 size_t buf_size = 96 * 1024;
8426 while (p + runlen < end
8427 && (sortlen <= buf_size
8428 || runlen + elt_size <= buf_size)
8429 && r_off2 > (*ext_r_off) (p + runlen))
8430 runlen += elt_size;
8431 if (buf == NULL)
8432 {
8433 buf = bfd_malloc (buf_size);
8434 if (buf == NULL)
8435 return FALSE;
8436 }
8437 if (runlen < sortlen)
8438 {
8439 memcpy (buf, p, runlen);
8440 memmove (loc + runlen, loc, sortlen);
8441 memcpy (loc, buf, runlen);
8442 }
8443 else
8444 {
8445 memcpy (buf, loc, sortlen);
8446 memmove (loc, p, runlen);
8447 memcpy (loc + runlen, buf, sortlen);
8448 }
8449 p += runlen - elt_size;
8450 }
8451 }
8452 /* Hashes are no longer valid. */
8453 free (reldata->hashes);
8454 reldata->hashes = NULL;
8455 free (buf);
8456 }
8457 return TRUE;
8458 }
8459
8460 struct elf_link_sort_rela
8461 {
8462 union {
8463 bfd_vma offset;
8464 bfd_vma sym_mask;
8465 } u;
8466 enum elf_reloc_type_class type;
8467 /* We use this as an array of size int_rels_per_ext_rel. */
8468 Elf_Internal_Rela rela[1];
8469 };
8470
8471 static int
8472 elf_link_sort_cmp1 (const void *A, const void *B)
8473 {
8474 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8475 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8476 int relativea, relativeb;
8477
8478 relativea = a->type == reloc_class_relative;
8479 relativeb = b->type == reloc_class_relative;
8480
8481 if (relativea < relativeb)
8482 return 1;
8483 if (relativea > relativeb)
8484 return -1;
8485 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8486 return -1;
8487 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8488 return 1;
8489 if (a->rela->r_offset < b->rela->r_offset)
8490 return -1;
8491 if (a->rela->r_offset > b->rela->r_offset)
8492 return 1;
8493 return 0;
8494 }
8495
8496 static int
8497 elf_link_sort_cmp2 (const void *A, const void *B)
8498 {
8499 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8500 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8501
8502 if (a->type < b->type)
8503 return -1;
8504 if (a->type > b->type)
8505 return 1;
8506 if (a->u.offset < b->u.offset)
8507 return -1;
8508 if (a->u.offset > b->u.offset)
8509 return 1;
8510 if (a->rela->r_offset < b->rela->r_offset)
8511 return -1;
8512 if (a->rela->r_offset > b->rela->r_offset)
8513 return 1;
8514 return 0;
8515 }
8516
8517 static size_t
8518 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8519 {
8520 asection *dynamic_relocs;
8521 asection *rela_dyn;
8522 asection *rel_dyn;
8523 bfd_size_type count, size;
8524 size_t i, ret, sort_elt, ext_size;
8525 bfd_byte *sort, *s_non_relative, *p;
8526 struct elf_link_sort_rela *sq;
8527 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8528 int i2e = bed->s->int_rels_per_ext_rel;
8529 unsigned int opb = bfd_octets_per_byte (abfd);
8530 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8531 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8532 struct bfd_link_order *lo;
8533 bfd_vma r_sym_mask;
8534 bfd_boolean use_rela;
8535
8536 /* Find a dynamic reloc section. */
8537 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8538 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8539 if (rela_dyn != NULL && rela_dyn->size > 0
8540 && rel_dyn != NULL && rel_dyn->size > 0)
8541 {
8542 bfd_boolean use_rela_initialised = FALSE;
8543
8544 /* This is just here to stop gcc from complaining.
8545 Its initialization checking code is not perfect. */
8546 use_rela = TRUE;
8547
8548 /* Both sections are present. Examine the sizes
8549 of the indirect sections to help us choose. */
8550 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8551 if (lo->type == bfd_indirect_link_order)
8552 {
8553 asection *o = lo->u.indirect.section;
8554
8555 if ((o->size % bed->s->sizeof_rela) == 0)
8556 {
8557 if ((o->size % bed->s->sizeof_rel) == 0)
8558 /* Section size is divisible by both rel and rela sizes.
8559 It is of no help to us. */
8560 ;
8561 else
8562 {
8563 /* Section size is only divisible by rela. */
8564 if (use_rela_initialised && (use_rela == FALSE))
8565 {
8566 _bfd_error_handler (_("%B: Unable to sort relocs - "
8567 "they are in more than one size"),
8568 abfd);
8569 bfd_set_error (bfd_error_invalid_operation);
8570 return 0;
8571 }
8572 else
8573 {
8574 use_rela = TRUE;
8575 use_rela_initialised = TRUE;
8576 }
8577 }
8578 }
8579 else if ((o->size % bed->s->sizeof_rel) == 0)
8580 {
8581 /* Section size is only divisible by rel. */
8582 if (use_rela_initialised && (use_rela == TRUE))
8583 {
8584 _bfd_error_handler (_("%B: Unable to sort relocs - "
8585 "they are in more than one size"),
8586 abfd);
8587 bfd_set_error (bfd_error_invalid_operation);
8588 return 0;
8589 }
8590 else
8591 {
8592 use_rela = FALSE;
8593 use_rela_initialised = TRUE;
8594 }
8595 }
8596 else
8597 {
8598 /* The section size is not divisible by either -
8599 something is wrong. */
8600 _bfd_error_handler (_("%B: Unable to sort relocs - "
8601 "they are of an unknown size"), abfd);
8602 bfd_set_error (bfd_error_invalid_operation);
8603 return 0;
8604 }
8605 }
8606
8607 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8608 if (lo->type == bfd_indirect_link_order)
8609 {
8610 asection *o = lo->u.indirect.section;
8611
8612 if ((o->size % bed->s->sizeof_rela) == 0)
8613 {
8614 if ((o->size % bed->s->sizeof_rel) == 0)
8615 /* Section size is divisible by both rel and rela sizes.
8616 It is of no help to us. */
8617 ;
8618 else
8619 {
8620 /* Section size is only divisible by rela. */
8621 if (use_rela_initialised && (use_rela == FALSE))
8622 {
8623 _bfd_error_handler (_("%B: Unable to sort relocs - "
8624 "they are in more than one size"),
8625 abfd);
8626 bfd_set_error (bfd_error_invalid_operation);
8627 return 0;
8628 }
8629 else
8630 {
8631 use_rela = TRUE;
8632 use_rela_initialised = TRUE;
8633 }
8634 }
8635 }
8636 else if ((o->size % bed->s->sizeof_rel) == 0)
8637 {
8638 /* Section size is only divisible by rel. */
8639 if (use_rela_initialised && (use_rela == TRUE))
8640 {
8641 _bfd_error_handler (_("%B: Unable to sort relocs - "
8642 "they are in more than one size"),
8643 abfd);
8644 bfd_set_error (bfd_error_invalid_operation);
8645 return 0;
8646 }
8647 else
8648 {
8649 use_rela = FALSE;
8650 use_rela_initialised = TRUE;
8651 }
8652 }
8653 else
8654 {
8655 /* The section size is not divisible by either -
8656 something is wrong. */
8657 _bfd_error_handler (_("%B: Unable to sort relocs - "
8658 "they are of an unknown size"), abfd);
8659 bfd_set_error (bfd_error_invalid_operation);
8660 return 0;
8661 }
8662 }
8663
8664 if (! use_rela_initialised)
8665 /* Make a guess. */
8666 use_rela = TRUE;
8667 }
8668 else if (rela_dyn != NULL && rela_dyn->size > 0)
8669 use_rela = TRUE;
8670 else if (rel_dyn != NULL && rel_dyn->size > 0)
8671 use_rela = FALSE;
8672 else
8673 return 0;
8674
8675 if (use_rela)
8676 {
8677 dynamic_relocs = rela_dyn;
8678 ext_size = bed->s->sizeof_rela;
8679 swap_in = bed->s->swap_reloca_in;
8680 swap_out = bed->s->swap_reloca_out;
8681 }
8682 else
8683 {
8684 dynamic_relocs = rel_dyn;
8685 ext_size = bed->s->sizeof_rel;
8686 swap_in = bed->s->swap_reloc_in;
8687 swap_out = bed->s->swap_reloc_out;
8688 }
8689
8690 size = 0;
8691 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8692 if (lo->type == bfd_indirect_link_order)
8693 size += lo->u.indirect.section->size;
8694
8695 if (size != dynamic_relocs->size)
8696 return 0;
8697
8698 sort_elt = (sizeof (struct elf_link_sort_rela)
8699 + (i2e - 1) * sizeof (Elf_Internal_Rela));
8700
8701 count = dynamic_relocs->size / ext_size;
8702 if (count == 0)
8703 return 0;
8704 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
8705
8706 if (sort == NULL)
8707 {
8708 (*info->callbacks->warning)
8709 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8710 return 0;
8711 }
8712
8713 if (bed->s->arch_size == 32)
8714 r_sym_mask = ~(bfd_vma) 0xff;
8715 else
8716 r_sym_mask = ~(bfd_vma) 0xffffffff;
8717
8718 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8719 if (lo->type == bfd_indirect_link_order)
8720 {
8721 bfd_byte *erel, *erelend;
8722 asection *o = lo->u.indirect.section;
8723
8724 if (o->contents == NULL && o->size != 0)
8725 {
8726 /* This is a reloc section that is being handled as a normal
8727 section. See bfd_section_from_shdr. We can't combine
8728 relocs in this case. */
8729 free (sort);
8730 return 0;
8731 }
8732 erel = o->contents;
8733 erelend = o->contents + o->size;
8734 p = sort + o->output_offset * opb / ext_size * sort_elt;
8735
8736 while (erel < erelend)
8737 {
8738 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8739
8740 (*swap_in) (abfd, erel, s->rela);
8741 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
8742 s->u.sym_mask = r_sym_mask;
8743 p += sort_elt;
8744 erel += ext_size;
8745 }
8746 }
8747
8748 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8749
8750 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8751 {
8752 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8753 if (s->type != reloc_class_relative)
8754 break;
8755 }
8756 ret = i;
8757 s_non_relative = p;
8758
8759 sq = (struct elf_link_sort_rela *) s_non_relative;
8760 for (; i < count; i++, p += sort_elt)
8761 {
8762 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8763 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8764 sq = sp;
8765 sp->u.offset = sq->rela->r_offset;
8766 }
8767
8768 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8769
8770 struct elf_link_hash_table *htab = elf_hash_table (info);
8771 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
8772 {
8773 /* We have plt relocs in .rela.dyn. */
8774 sq = (struct elf_link_sort_rela *) sort;
8775 for (i = 0; i < count; i++)
8776 if (sq[count - i - 1].type != reloc_class_plt)
8777 break;
8778 if (i != 0 && htab->srelplt->size == i * ext_size)
8779 {
8780 struct bfd_link_order **plo;
8781 /* Put srelplt link_order last. This is so the output_offset
8782 set in the next loop is correct for DT_JMPREL. */
8783 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
8784 if ((*plo)->type == bfd_indirect_link_order
8785 && (*plo)->u.indirect.section == htab->srelplt)
8786 {
8787 lo = *plo;
8788 *plo = lo->next;
8789 }
8790 else
8791 plo = &(*plo)->next;
8792 *plo = lo;
8793 lo->next = NULL;
8794 dynamic_relocs->map_tail.link_order = lo;
8795 }
8796 }
8797
8798 p = sort;
8799 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8800 if (lo->type == bfd_indirect_link_order)
8801 {
8802 bfd_byte *erel, *erelend;
8803 asection *o = lo->u.indirect.section;
8804
8805 erel = o->contents;
8806 erelend = o->contents + o->size;
8807 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
8808 while (erel < erelend)
8809 {
8810 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8811 (*swap_out) (abfd, s->rela, erel);
8812 p += sort_elt;
8813 erel += ext_size;
8814 }
8815 }
8816
8817 free (sort);
8818 *psec = dynamic_relocs;
8819 return ret;
8820 }
8821
8822 /* Add a symbol to the output symbol string table. */
8823
8824 static int
8825 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
8826 const char *name,
8827 Elf_Internal_Sym *elfsym,
8828 asection *input_sec,
8829 struct elf_link_hash_entry *h)
8830 {
8831 int (*output_symbol_hook)
8832 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8833 struct elf_link_hash_entry *);
8834 struct elf_link_hash_table *hash_table;
8835 const struct elf_backend_data *bed;
8836 bfd_size_type strtabsize;
8837
8838 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8839
8840 bed = get_elf_backend_data (flinfo->output_bfd);
8841 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8842 if (output_symbol_hook != NULL)
8843 {
8844 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
8845 if (ret != 1)
8846 return ret;
8847 }
8848
8849 if (name == NULL
8850 || *name == '\0'
8851 || (input_sec->flags & SEC_EXCLUDE))
8852 elfsym->st_name = (unsigned long) -1;
8853 else
8854 {
8855 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8856 to get the final offset for st_name. */
8857 elfsym->st_name
8858 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
8859 name, FALSE);
8860 if (elfsym->st_name == (unsigned long) -1)
8861 return 0;
8862 }
8863
8864 hash_table = elf_hash_table (flinfo->info);
8865 strtabsize = hash_table->strtabsize;
8866 if (strtabsize <= hash_table->strtabcount)
8867 {
8868 strtabsize += strtabsize;
8869 hash_table->strtabsize = strtabsize;
8870 strtabsize *= sizeof (*hash_table->strtab);
8871 hash_table->strtab
8872 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
8873 strtabsize);
8874 if (hash_table->strtab == NULL)
8875 return 0;
8876 }
8877 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
8878 hash_table->strtab[hash_table->strtabcount].dest_index
8879 = hash_table->strtabcount;
8880 hash_table->strtab[hash_table->strtabcount].destshndx_index
8881 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
8882
8883 bfd_get_symcount (flinfo->output_bfd) += 1;
8884 hash_table->strtabcount += 1;
8885
8886 return 1;
8887 }
8888
8889 /* Swap symbols out to the symbol table and flush the output symbols to
8890 the file. */
8891
8892 static bfd_boolean
8893 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
8894 {
8895 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
8896 bfd_size_type amt, i;
8897 const struct elf_backend_data *bed;
8898 bfd_byte *symbuf;
8899 Elf_Internal_Shdr *hdr;
8900 file_ptr pos;
8901 bfd_boolean ret;
8902
8903 if (!hash_table->strtabcount)
8904 return TRUE;
8905
8906 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8907
8908 bed = get_elf_backend_data (flinfo->output_bfd);
8909
8910 amt = bed->s->sizeof_sym * hash_table->strtabcount;
8911 symbuf = (bfd_byte *) bfd_malloc (amt);
8912 if (symbuf == NULL)
8913 return FALSE;
8914
8915 if (flinfo->symshndxbuf)
8916 {
8917 amt = (sizeof (Elf_External_Sym_Shndx)
8918 * (bfd_get_symcount (flinfo->output_bfd)));
8919 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
8920 if (flinfo->symshndxbuf == NULL)
8921 {
8922 free (symbuf);
8923 return FALSE;
8924 }
8925 }
8926
8927 for (i = 0; i < hash_table->strtabcount; i++)
8928 {
8929 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
8930 if (elfsym->sym.st_name == (unsigned long) -1)
8931 elfsym->sym.st_name = 0;
8932 else
8933 elfsym->sym.st_name
8934 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
8935 elfsym->sym.st_name);
8936 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
8937 ((bfd_byte *) symbuf
8938 + (elfsym->dest_index
8939 * bed->s->sizeof_sym)),
8940 (flinfo->symshndxbuf
8941 + elfsym->destshndx_index));
8942 }
8943
8944 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
8945 pos = hdr->sh_offset + hdr->sh_size;
8946 amt = hash_table->strtabcount * bed->s->sizeof_sym;
8947 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
8948 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
8949 {
8950 hdr->sh_size += amt;
8951 ret = TRUE;
8952 }
8953 else
8954 ret = FALSE;
8955
8956 free (symbuf);
8957
8958 free (hash_table->strtab);
8959 hash_table->strtab = NULL;
8960
8961 return ret;
8962 }
8963
8964 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8965
8966 static bfd_boolean
8967 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
8968 {
8969 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
8970 && sym->st_shndx < SHN_LORESERVE)
8971 {
8972 /* The gABI doesn't support dynamic symbols in output sections
8973 beyond 64k. */
8974 (*_bfd_error_handler)
8975 (_("%B: Too many sections: %d (>= %d)"),
8976 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
8977 bfd_set_error (bfd_error_nonrepresentable_section);
8978 return FALSE;
8979 }
8980 return TRUE;
8981 }
8982
8983 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8984 allowing an unsatisfied unversioned symbol in the DSO to match a
8985 versioned symbol that would normally require an explicit version.
8986 We also handle the case that a DSO references a hidden symbol
8987 which may be satisfied by a versioned symbol in another DSO. */
8988
8989 static bfd_boolean
8990 elf_link_check_versioned_symbol (struct bfd_link_info *info,
8991 const struct elf_backend_data *bed,
8992 struct elf_link_hash_entry *h)
8993 {
8994 bfd *abfd;
8995 struct elf_link_loaded_list *loaded;
8996
8997 if (!is_elf_hash_table (info->hash))
8998 return FALSE;
8999
9000 /* Check indirect symbol. */
9001 while (h->root.type == bfd_link_hash_indirect)
9002 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9003
9004 switch (h->root.type)
9005 {
9006 default:
9007 abfd = NULL;
9008 break;
9009
9010 case bfd_link_hash_undefined:
9011 case bfd_link_hash_undefweak:
9012 abfd = h->root.u.undef.abfd;
9013 if ((abfd->flags & DYNAMIC) == 0
9014 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9015 return FALSE;
9016 break;
9017
9018 case bfd_link_hash_defined:
9019 case bfd_link_hash_defweak:
9020 abfd = h->root.u.def.section->owner;
9021 break;
9022
9023 case bfd_link_hash_common:
9024 abfd = h->root.u.c.p->section->owner;
9025 break;
9026 }
9027 BFD_ASSERT (abfd != NULL);
9028
9029 for (loaded = elf_hash_table (info)->loaded;
9030 loaded != NULL;
9031 loaded = loaded->next)
9032 {
9033 bfd *input;
9034 Elf_Internal_Shdr *hdr;
9035 bfd_size_type symcount;
9036 bfd_size_type extsymcount;
9037 bfd_size_type extsymoff;
9038 Elf_Internal_Shdr *versymhdr;
9039 Elf_Internal_Sym *isym;
9040 Elf_Internal_Sym *isymend;
9041 Elf_Internal_Sym *isymbuf;
9042 Elf_External_Versym *ever;
9043 Elf_External_Versym *extversym;
9044
9045 input = loaded->abfd;
9046
9047 /* We check each DSO for a possible hidden versioned definition. */
9048 if (input == abfd
9049 || (input->flags & DYNAMIC) == 0
9050 || elf_dynversym (input) == 0)
9051 continue;
9052
9053 hdr = &elf_tdata (input)->dynsymtab_hdr;
9054
9055 symcount = hdr->sh_size / bed->s->sizeof_sym;
9056 if (elf_bad_symtab (input))
9057 {
9058 extsymcount = symcount;
9059 extsymoff = 0;
9060 }
9061 else
9062 {
9063 extsymcount = symcount - hdr->sh_info;
9064 extsymoff = hdr->sh_info;
9065 }
9066
9067 if (extsymcount == 0)
9068 continue;
9069
9070 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9071 NULL, NULL, NULL);
9072 if (isymbuf == NULL)
9073 return FALSE;
9074
9075 /* Read in any version definitions. */
9076 versymhdr = &elf_tdata (input)->dynversym_hdr;
9077 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
9078 if (extversym == NULL)
9079 goto error_ret;
9080
9081 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9082 || (bfd_bread (extversym, versymhdr->sh_size, input)
9083 != versymhdr->sh_size))
9084 {
9085 free (extversym);
9086 error_ret:
9087 free (isymbuf);
9088 return FALSE;
9089 }
9090
9091 ever = extversym + extsymoff;
9092 isymend = isymbuf + extsymcount;
9093 for (isym = isymbuf; isym < isymend; isym++, ever++)
9094 {
9095 const char *name;
9096 Elf_Internal_Versym iver;
9097 unsigned short version_index;
9098
9099 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9100 || isym->st_shndx == SHN_UNDEF)
9101 continue;
9102
9103 name = bfd_elf_string_from_elf_section (input,
9104 hdr->sh_link,
9105 isym->st_name);
9106 if (strcmp (name, h->root.root.string) != 0)
9107 continue;
9108
9109 _bfd_elf_swap_versym_in (input, ever, &iver);
9110
9111 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9112 && !(h->def_regular
9113 && h->forced_local))
9114 {
9115 /* If we have a non-hidden versioned sym, then it should
9116 have provided a definition for the undefined sym unless
9117 it is defined in a non-shared object and forced local.
9118 */
9119 abort ();
9120 }
9121
9122 version_index = iver.vs_vers & VERSYM_VERSION;
9123 if (version_index == 1 || version_index == 2)
9124 {
9125 /* This is the base or first version. We can use it. */
9126 free (extversym);
9127 free (isymbuf);
9128 return TRUE;
9129 }
9130 }
9131
9132 free (extversym);
9133 free (isymbuf);
9134 }
9135
9136 return FALSE;
9137 }
9138
9139 /* Convert ELF common symbol TYPE. */
9140
9141 static int
9142 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9143 {
9144 /* Commom symbol can only appear in relocatable link. */
9145 if (!bfd_link_relocatable (info))
9146 abort ();
9147 switch (info->elf_stt_common)
9148 {
9149 case unchanged:
9150 break;
9151 case elf_stt_common:
9152 type = STT_COMMON;
9153 break;
9154 case no_elf_stt_common:
9155 type = STT_OBJECT;
9156 break;
9157 }
9158 return type;
9159 }
9160
9161 /* Add an external symbol to the symbol table. This is called from
9162 the hash table traversal routine. When generating a shared object,
9163 we go through the symbol table twice. The first time we output
9164 anything that might have been forced to local scope in a version
9165 script. The second time we output the symbols that are still
9166 global symbols. */
9167
9168 static bfd_boolean
9169 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9170 {
9171 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9172 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9173 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9174 bfd_boolean strip;
9175 Elf_Internal_Sym sym;
9176 asection *input_sec;
9177 const struct elf_backend_data *bed;
9178 long indx;
9179 int ret;
9180 unsigned int type;
9181 /* A symbol is bound locally if it is forced local or it is locally
9182 defined, hidden versioned, not referenced by shared library and
9183 not exported when linking executable. */
9184 bfd_boolean local_bind = (h->forced_local
9185 || (bfd_link_executable (flinfo->info)
9186 && !flinfo->info->export_dynamic
9187 && !h->dynamic
9188 && !h->ref_dynamic
9189 && h->def_regular
9190 && h->versioned == versioned_hidden));
9191
9192 if (h->root.type == bfd_link_hash_warning)
9193 {
9194 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9195 if (h->root.type == bfd_link_hash_new)
9196 return TRUE;
9197 }
9198
9199 /* Decide whether to output this symbol in this pass. */
9200 if (eoinfo->localsyms)
9201 {
9202 if (!local_bind)
9203 return TRUE;
9204 }
9205 else
9206 {
9207 if (local_bind)
9208 return TRUE;
9209 }
9210
9211 bed = get_elf_backend_data (flinfo->output_bfd);
9212
9213 if (h->root.type == bfd_link_hash_undefined)
9214 {
9215 /* If we have an undefined symbol reference here then it must have
9216 come from a shared library that is being linked in. (Undefined
9217 references in regular files have already been handled unless
9218 they are in unreferenced sections which are removed by garbage
9219 collection). */
9220 bfd_boolean ignore_undef = FALSE;
9221
9222 /* Some symbols may be special in that the fact that they're
9223 undefined can be safely ignored - let backend determine that. */
9224 if (bed->elf_backend_ignore_undef_symbol)
9225 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9226
9227 /* If we are reporting errors for this situation then do so now. */
9228 if (!ignore_undef
9229 && h->ref_dynamic
9230 && (!h->ref_regular || flinfo->info->gc_sections)
9231 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9232 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9233 {
9234 if (!(flinfo->info->callbacks->undefined_symbol
9235 (flinfo->info, h->root.root.string,
9236 h->ref_regular ? NULL : h->root.u.undef.abfd,
9237 NULL, 0,
9238 (flinfo->info->unresolved_syms_in_shared_libs
9239 == RM_GENERATE_ERROR))))
9240 {
9241 bfd_set_error (bfd_error_bad_value);
9242 eoinfo->failed = TRUE;
9243 return FALSE;
9244 }
9245 }
9246
9247 /* Strip a global symbol defined in a discarded section. */
9248 if (h->indx == -3)
9249 return TRUE;
9250 }
9251
9252 /* We should also warn if a forced local symbol is referenced from
9253 shared libraries. */
9254 if (bfd_link_executable (flinfo->info)
9255 && h->forced_local
9256 && h->ref_dynamic
9257 && h->def_regular
9258 && !h->dynamic_def
9259 && h->ref_dynamic_nonweak
9260 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9261 {
9262 bfd *def_bfd;
9263 const char *msg;
9264 struct elf_link_hash_entry *hi = h;
9265
9266 /* Check indirect symbol. */
9267 while (hi->root.type == bfd_link_hash_indirect)
9268 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9269
9270 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9271 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9272 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9273 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9274 else
9275 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
9276 def_bfd = flinfo->output_bfd;
9277 if (hi->root.u.def.section != bfd_abs_section_ptr)
9278 def_bfd = hi->root.u.def.section->owner;
9279 (*_bfd_error_handler) (msg, flinfo->output_bfd, def_bfd,
9280 h->root.root.string);
9281 bfd_set_error (bfd_error_bad_value);
9282 eoinfo->failed = TRUE;
9283 return FALSE;
9284 }
9285
9286 /* We don't want to output symbols that have never been mentioned by
9287 a regular file, or that we have been told to strip. However, if
9288 h->indx is set to -2, the symbol is used by a reloc and we must
9289 output it. */
9290 strip = FALSE;
9291 if (h->indx == -2)
9292 ;
9293 else if ((h->def_dynamic
9294 || h->ref_dynamic
9295 || h->root.type == bfd_link_hash_new)
9296 && !h->def_regular
9297 && !h->ref_regular)
9298 strip = TRUE;
9299 else if (flinfo->info->strip == strip_all)
9300 strip = TRUE;
9301 else if (flinfo->info->strip == strip_some
9302 && bfd_hash_lookup (flinfo->info->keep_hash,
9303 h->root.root.string, FALSE, FALSE) == NULL)
9304 strip = TRUE;
9305 else if ((h->root.type == bfd_link_hash_defined
9306 || h->root.type == bfd_link_hash_defweak)
9307 && ((flinfo->info->strip_discarded
9308 && discarded_section (h->root.u.def.section))
9309 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9310 && h->root.u.def.section->owner != NULL
9311 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9312 strip = TRUE;
9313 else if ((h->root.type == bfd_link_hash_undefined
9314 || h->root.type == bfd_link_hash_undefweak)
9315 && h->root.u.undef.abfd != NULL
9316 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9317 strip = TRUE;
9318
9319 type = h->type;
9320
9321 /* If we're stripping it, and it's not a dynamic symbol, there's
9322 nothing else to do. However, if it is a forced local symbol or
9323 an ifunc symbol we need to give the backend finish_dynamic_symbol
9324 function a chance to make it dynamic. */
9325 if (strip
9326 && h->dynindx == -1
9327 && type != STT_GNU_IFUNC
9328 && !h->forced_local)
9329 return TRUE;
9330
9331 sym.st_value = 0;
9332 sym.st_size = h->size;
9333 sym.st_other = h->other;
9334 switch (h->root.type)
9335 {
9336 default:
9337 case bfd_link_hash_new:
9338 case bfd_link_hash_warning:
9339 abort ();
9340 return FALSE;
9341
9342 case bfd_link_hash_undefined:
9343 case bfd_link_hash_undefweak:
9344 input_sec = bfd_und_section_ptr;
9345 sym.st_shndx = SHN_UNDEF;
9346 break;
9347
9348 case bfd_link_hash_defined:
9349 case bfd_link_hash_defweak:
9350 {
9351 input_sec = h->root.u.def.section;
9352 if (input_sec->output_section != NULL)
9353 {
9354 sym.st_shndx =
9355 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9356 input_sec->output_section);
9357 if (sym.st_shndx == SHN_BAD)
9358 {
9359 (*_bfd_error_handler)
9360 (_("%B: could not find output section %A for input section %A"),
9361 flinfo->output_bfd, input_sec->output_section, input_sec);
9362 bfd_set_error (bfd_error_nonrepresentable_section);
9363 eoinfo->failed = TRUE;
9364 return FALSE;
9365 }
9366
9367 /* ELF symbols in relocatable files are section relative,
9368 but in nonrelocatable files they are virtual
9369 addresses. */
9370 sym.st_value = h->root.u.def.value + input_sec->output_offset;
9371 if (!bfd_link_relocatable (flinfo->info))
9372 {
9373 sym.st_value += input_sec->output_section->vma;
9374 if (h->type == STT_TLS)
9375 {
9376 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9377 if (tls_sec != NULL)
9378 sym.st_value -= tls_sec->vma;
9379 }
9380 }
9381 }
9382 else
9383 {
9384 BFD_ASSERT (input_sec->owner == NULL
9385 || (input_sec->owner->flags & DYNAMIC) != 0);
9386 sym.st_shndx = SHN_UNDEF;
9387 input_sec = bfd_und_section_ptr;
9388 }
9389 }
9390 break;
9391
9392 case bfd_link_hash_common:
9393 input_sec = h->root.u.c.p->section;
9394 sym.st_shndx = bed->common_section_index (input_sec);
9395 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9396 break;
9397
9398 case bfd_link_hash_indirect:
9399 /* These symbols are created by symbol versioning. They point
9400 to the decorated version of the name. For example, if the
9401 symbol foo@@GNU_1.2 is the default, which should be used when
9402 foo is used with no version, then we add an indirect symbol
9403 foo which points to foo@@GNU_1.2. We ignore these symbols,
9404 since the indirected symbol is already in the hash table. */
9405 return TRUE;
9406 }
9407
9408 if (type == STT_COMMON || type == STT_OBJECT)
9409 switch (h->root.type)
9410 {
9411 case bfd_link_hash_common:
9412 type = elf_link_convert_common_type (flinfo->info, type);
9413 break;
9414 case bfd_link_hash_defined:
9415 case bfd_link_hash_defweak:
9416 if (bed->common_definition (&sym))
9417 type = elf_link_convert_common_type (flinfo->info, type);
9418 else
9419 type = STT_OBJECT;
9420 break;
9421 case bfd_link_hash_undefined:
9422 case bfd_link_hash_undefweak:
9423 break;
9424 default:
9425 abort ();
9426 }
9427
9428 if (local_bind)
9429 {
9430 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9431 /* Turn off visibility on local symbol. */
9432 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9433 }
9434 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9435 else if (h->unique_global && h->def_regular)
9436 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9437 else if (h->root.type == bfd_link_hash_undefweak
9438 || h->root.type == bfd_link_hash_defweak)
9439 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9440 else
9441 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9442 sym.st_target_internal = h->target_internal;
9443
9444 /* Give the processor backend a chance to tweak the symbol value,
9445 and also to finish up anything that needs to be done for this
9446 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9447 forced local syms when non-shared is due to a historical quirk.
9448 STT_GNU_IFUNC symbol must go through PLT. */
9449 if ((h->type == STT_GNU_IFUNC
9450 && h->def_regular
9451 && !bfd_link_relocatable (flinfo->info))
9452 || ((h->dynindx != -1
9453 || h->forced_local)
9454 && ((bfd_link_pic (flinfo->info)
9455 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9456 || h->root.type != bfd_link_hash_undefweak))
9457 || !h->forced_local)
9458 && elf_hash_table (flinfo->info)->dynamic_sections_created))
9459 {
9460 if (! ((*bed->elf_backend_finish_dynamic_symbol)
9461 (flinfo->output_bfd, flinfo->info, h, &sym)))
9462 {
9463 eoinfo->failed = TRUE;
9464 return FALSE;
9465 }
9466 }
9467
9468 /* If we are marking the symbol as undefined, and there are no
9469 non-weak references to this symbol from a regular object, then
9470 mark the symbol as weak undefined; if there are non-weak
9471 references, mark the symbol as strong. We can't do this earlier,
9472 because it might not be marked as undefined until the
9473 finish_dynamic_symbol routine gets through with it. */
9474 if (sym.st_shndx == SHN_UNDEF
9475 && h->ref_regular
9476 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9477 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9478 {
9479 int bindtype;
9480 type = ELF_ST_TYPE (sym.st_info);
9481
9482 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9483 if (type == STT_GNU_IFUNC)
9484 type = STT_FUNC;
9485
9486 if (h->ref_regular_nonweak)
9487 bindtype = STB_GLOBAL;
9488 else
9489 bindtype = STB_WEAK;
9490 sym.st_info = ELF_ST_INFO (bindtype, type);
9491 }
9492
9493 /* If this is a symbol defined in a dynamic library, don't use the
9494 symbol size from the dynamic library. Relinking an executable
9495 against a new library may introduce gratuitous changes in the
9496 executable's symbols if we keep the size. */
9497 if (sym.st_shndx == SHN_UNDEF
9498 && !h->def_regular
9499 && h->def_dynamic)
9500 sym.st_size = 0;
9501
9502 /* If a non-weak symbol with non-default visibility is not defined
9503 locally, it is a fatal error. */
9504 if (!bfd_link_relocatable (flinfo->info)
9505 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9506 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9507 && h->root.type == bfd_link_hash_undefined
9508 && !h->def_regular)
9509 {
9510 const char *msg;
9511
9512 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9513 msg = _("%B: protected symbol `%s' isn't defined");
9514 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9515 msg = _("%B: internal symbol `%s' isn't defined");
9516 else
9517 msg = _("%B: hidden symbol `%s' isn't defined");
9518 (*_bfd_error_handler) (msg, flinfo->output_bfd, h->root.root.string);
9519 bfd_set_error (bfd_error_bad_value);
9520 eoinfo->failed = TRUE;
9521 return FALSE;
9522 }
9523
9524 /* If this symbol should be put in the .dynsym section, then put it
9525 there now. We already know the symbol index. We also fill in
9526 the entry in the .hash section. */
9527 if (elf_hash_table (flinfo->info)->dynsym != NULL
9528 && h->dynindx != -1
9529 && elf_hash_table (flinfo->info)->dynamic_sections_created)
9530 {
9531 bfd_byte *esym;
9532
9533 /* Since there is no version information in the dynamic string,
9534 if there is no version info in symbol version section, we will
9535 have a run-time problem if not linking executable, referenced
9536 by shared library, not locally defined, or not bound locally.
9537 */
9538 if (h->verinfo.verdef == NULL
9539 && !local_bind
9540 && (!bfd_link_executable (flinfo->info)
9541 || h->ref_dynamic
9542 || !h->def_regular))
9543 {
9544 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9545
9546 if (p && p [1] != '\0')
9547 {
9548 (*_bfd_error_handler)
9549 (_("%B: No symbol version section for versioned symbol `%s'"),
9550 flinfo->output_bfd, h->root.root.string);
9551 eoinfo->failed = TRUE;
9552 return FALSE;
9553 }
9554 }
9555
9556 sym.st_name = h->dynstr_index;
9557 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9558 + h->dynindx * bed->s->sizeof_sym);
9559 if (!check_dynsym (flinfo->output_bfd, &sym))
9560 {
9561 eoinfo->failed = TRUE;
9562 return FALSE;
9563 }
9564 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
9565
9566 if (flinfo->hash_sec != NULL)
9567 {
9568 size_t hash_entry_size;
9569 bfd_byte *bucketpos;
9570 bfd_vma chain;
9571 size_t bucketcount;
9572 size_t bucket;
9573
9574 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
9575 bucket = h->u.elf_hash_value % bucketcount;
9576
9577 hash_entry_size
9578 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9579 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
9580 + (bucket + 2) * hash_entry_size);
9581 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9582 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9583 bucketpos);
9584 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9585 ((bfd_byte *) flinfo->hash_sec->contents
9586 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9587 }
9588
9589 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
9590 {
9591 Elf_Internal_Versym iversym;
9592 Elf_External_Versym *eversym;
9593
9594 if (!h->def_regular)
9595 {
9596 if (h->verinfo.verdef == NULL
9597 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9598 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
9599 iversym.vs_vers = 0;
9600 else
9601 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9602 }
9603 else
9604 {
9605 if (h->verinfo.vertree == NULL)
9606 iversym.vs_vers = 1;
9607 else
9608 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
9609 if (flinfo->info->create_default_symver)
9610 iversym.vs_vers++;
9611 }
9612
9613 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9614 defined locally. */
9615 if (h->versioned == versioned_hidden && h->def_regular)
9616 iversym.vs_vers |= VERSYM_HIDDEN;
9617
9618 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
9619 eversym += h->dynindx;
9620 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
9621 }
9622 }
9623
9624 /* If the symbol is undefined, and we didn't output it to .dynsym,
9625 strip it from .symtab too. Obviously we can't do this for
9626 relocatable output or when needed for --emit-relocs. */
9627 else if (input_sec == bfd_und_section_ptr
9628 && h->indx != -2
9629 && !bfd_link_relocatable (flinfo->info))
9630 return TRUE;
9631 /* Also strip others that we couldn't earlier due to dynamic symbol
9632 processing. */
9633 if (strip)
9634 return TRUE;
9635 if ((input_sec->flags & SEC_EXCLUDE) != 0)
9636 return TRUE;
9637
9638 /* Output a FILE symbol so that following locals are not associated
9639 with the wrong input file. We need one for forced local symbols
9640 if we've seen more than one FILE symbol or when we have exactly
9641 one FILE symbol but global symbols are present in a file other
9642 than the one with the FILE symbol. We also need one if linker
9643 defined symbols are present. In practice these conditions are
9644 always met, so just emit the FILE symbol unconditionally. */
9645 if (eoinfo->localsyms
9646 && !eoinfo->file_sym_done
9647 && eoinfo->flinfo->filesym_count != 0)
9648 {
9649 Elf_Internal_Sym fsym;
9650
9651 memset (&fsym, 0, sizeof (fsym));
9652 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9653 fsym.st_shndx = SHN_ABS;
9654 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9655 bfd_und_section_ptr, NULL))
9656 return FALSE;
9657
9658 eoinfo->file_sym_done = TRUE;
9659 }
9660
9661 indx = bfd_get_symcount (flinfo->output_bfd);
9662 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9663 input_sec, h);
9664 if (ret == 0)
9665 {
9666 eoinfo->failed = TRUE;
9667 return FALSE;
9668 }
9669 else if (ret == 1)
9670 h->indx = indx;
9671 else if (h->indx == -2)
9672 abort();
9673
9674 return TRUE;
9675 }
9676
9677 /* Return TRUE if special handling is done for relocs in SEC against
9678 symbols defined in discarded sections. */
9679
9680 static bfd_boolean
9681 elf_section_ignore_discarded_relocs (asection *sec)
9682 {
9683 const struct elf_backend_data *bed;
9684
9685 switch (sec->sec_info_type)
9686 {
9687 case SEC_INFO_TYPE_STABS:
9688 case SEC_INFO_TYPE_EH_FRAME:
9689 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
9690 return TRUE;
9691 default:
9692 break;
9693 }
9694
9695 bed = get_elf_backend_data (sec->owner);
9696 if (bed->elf_backend_ignore_discarded_relocs != NULL
9697 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9698 return TRUE;
9699
9700 return FALSE;
9701 }
9702
9703 /* Return a mask saying how ld should treat relocations in SEC against
9704 symbols defined in discarded sections. If this function returns
9705 COMPLAIN set, ld will issue a warning message. If this function
9706 returns PRETEND set, and the discarded section was link-once and the
9707 same size as the kept link-once section, ld will pretend that the
9708 symbol was actually defined in the kept section. Otherwise ld will
9709 zero the reloc (at least that is the intent, but some cooperation by
9710 the target dependent code is needed, particularly for REL targets). */
9711
9712 unsigned int
9713 _bfd_elf_default_action_discarded (asection *sec)
9714 {
9715 if (sec->flags & SEC_DEBUGGING)
9716 return PRETEND;
9717
9718 if (strcmp (".eh_frame", sec->name) == 0)
9719 return 0;
9720
9721 if (strcmp (".gcc_except_table", sec->name) == 0)
9722 return 0;
9723
9724 return COMPLAIN | PRETEND;
9725 }
9726
9727 /* Find a match between a section and a member of a section group. */
9728
9729 static asection *
9730 match_group_member (asection *sec, asection *group,
9731 struct bfd_link_info *info)
9732 {
9733 asection *first = elf_next_in_group (group);
9734 asection *s = first;
9735
9736 while (s != NULL)
9737 {
9738 if (bfd_elf_match_symbols_in_sections (s, sec, info))
9739 return s;
9740
9741 s = elf_next_in_group (s);
9742 if (s == first)
9743 break;
9744 }
9745
9746 return NULL;
9747 }
9748
9749 /* Check if the kept section of a discarded section SEC can be used
9750 to replace it. Return the replacement if it is OK. Otherwise return
9751 NULL. */
9752
9753 asection *
9754 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
9755 {
9756 asection *kept;
9757
9758 kept = sec->kept_section;
9759 if (kept != NULL)
9760 {
9761 if ((kept->flags & SEC_GROUP) != 0)
9762 kept = match_group_member (sec, kept, info);
9763 if (kept != NULL
9764 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9765 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
9766 kept = NULL;
9767 sec->kept_section = kept;
9768 }
9769 return kept;
9770 }
9771
9772 /* Link an input file into the linker output file. This function
9773 handles all the sections and relocations of the input file at once.
9774 This is so that we only have to read the local symbols once, and
9775 don't have to keep them in memory. */
9776
9777 static bfd_boolean
9778 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
9779 {
9780 int (*relocate_section)
9781 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9782 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9783 bfd *output_bfd;
9784 Elf_Internal_Shdr *symtab_hdr;
9785 size_t locsymcount;
9786 size_t extsymoff;
9787 Elf_Internal_Sym *isymbuf;
9788 Elf_Internal_Sym *isym;
9789 Elf_Internal_Sym *isymend;
9790 long *pindex;
9791 asection **ppsection;
9792 asection *o;
9793 const struct elf_backend_data *bed;
9794 struct elf_link_hash_entry **sym_hashes;
9795 bfd_size_type address_size;
9796 bfd_vma r_type_mask;
9797 int r_sym_shift;
9798 bfd_boolean have_file_sym = FALSE;
9799
9800 output_bfd = flinfo->output_bfd;
9801 bed = get_elf_backend_data (output_bfd);
9802 relocate_section = bed->elf_backend_relocate_section;
9803
9804 /* If this is a dynamic object, we don't want to do anything here:
9805 we don't want the local symbols, and we don't want the section
9806 contents. */
9807 if ((input_bfd->flags & DYNAMIC) != 0)
9808 return TRUE;
9809
9810 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9811 if (elf_bad_symtab (input_bfd))
9812 {
9813 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9814 extsymoff = 0;
9815 }
9816 else
9817 {
9818 locsymcount = symtab_hdr->sh_info;
9819 extsymoff = symtab_hdr->sh_info;
9820 }
9821
9822 /* Read the local symbols. */
9823 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9824 if (isymbuf == NULL && locsymcount != 0)
9825 {
9826 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
9827 flinfo->internal_syms,
9828 flinfo->external_syms,
9829 flinfo->locsym_shndx);
9830 if (isymbuf == NULL)
9831 return FALSE;
9832 }
9833
9834 /* Find local symbol sections and adjust values of symbols in
9835 SEC_MERGE sections. Write out those local symbols we know are
9836 going into the output file. */
9837 isymend = isymbuf + locsymcount;
9838 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
9839 isym < isymend;
9840 isym++, pindex++, ppsection++)
9841 {
9842 asection *isec;
9843 const char *name;
9844 Elf_Internal_Sym osym;
9845 long indx;
9846 int ret;
9847
9848 *pindex = -1;
9849
9850 if (elf_bad_symtab (input_bfd))
9851 {
9852 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9853 {
9854 *ppsection = NULL;
9855 continue;
9856 }
9857 }
9858
9859 if (isym->st_shndx == SHN_UNDEF)
9860 isec = bfd_und_section_ptr;
9861 else if (isym->st_shndx == SHN_ABS)
9862 isec = bfd_abs_section_ptr;
9863 else if (isym->st_shndx == SHN_COMMON)
9864 isec = bfd_com_section_ptr;
9865 else
9866 {
9867 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9868 if (isec == NULL)
9869 {
9870 /* Don't attempt to output symbols with st_shnx in the
9871 reserved range other than SHN_ABS and SHN_COMMON. */
9872 *ppsection = NULL;
9873 continue;
9874 }
9875 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
9876 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9877 isym->st_value =
9878 _bfd_merged_section_offset (output_bfd, &isec,
9879 elf_section_data (isec)->sec_info,
9880 isym->st_value);
9881 }
9882
9883 *ppsection = isec;
9884
9885 /* Don't output the first, undefined, symbol. In fact, don't
9886 output any undefined local symbol. */
9887 if (isec == bfd_und_section_ptr)
9888 continue;
9889
9890 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9891 {
9892 /* We never output section symbols. Instead, we use the
9893 section symbol of the corresponding section in the output
9894 file. */
9895 continue;
9896 }
9897
9898 /* If we are stripping all symbols, we don't want to output this
9899 one. */
9900 if (flinfo->info->strip == strip_all)
9901 continue;
9902
9903 /* If we are discarding all local symbols, we don't want to
9904 output this one. If we are generating a relocatable output
9905 file, then some of the local symbols may be required by
9906 relocs; we output them below as we discover that they are
9907 needed. */
9908 if (flinfo->info->discard == discard_all)
9909 continue;
9910
9911 /* If this symbol is defined in a section which we are
9912 discarding, we don't need to keep it. */
9913 if (isym->st_shndx != SHN_UNDEF
9914 && isym->st_shndx < SHN_LORESERVE
9915 && bfd_section_removed_from_list (output_bfd,
9916 isec->output_section))
9917 continue;
9918
9919 /* Get the name of the symbol. */
9920 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9921 isym->st_name);
9922 if (name == NULL)
9923 return FALSE;
9924
9925 /* See if we are discarding symbols with this name. */
9926 if ((flinfo->info->strip == strip_some
9927 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
9928 == NULL))
9929 || (((flinfo->info->discard == discard_sec_merge
9930 && (isec->flags & SEC_MERGE)
9931 && !bfd_link_relocatable (flinfo->info))
9932 || flinfo->info->discard == discard_l)
9933 && bfd_is_local_label_name (input_bfd, name)))
9934 continue;
9935
9936 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
9937 {
9938 if (input_bfd->lto_output)
9939 /* -flto puts a temp file name here. This means builds
9940 are not reproducible. Discard the symbol. */
9941 continue;
9942 have_file_sym = TRUE;
9943 flinfo->filesym_count += 1;
9944 }
9945 if (!have_file_sym)
9946 {
9947 /* In the absence of debug info, bfd_find_nearest_line uses
9948 FILE symbols to determine the source file for local
9949 function symbols. Provide a FILE symbol here if input
9950 files lack such, so that their symbols won't be
9951 associated with a previous input file. It's not the
9952 source file, but the best we can do. */
9953 have_file_sym = TRUE;
9954 flinfo->filesym_count += 1;
9955 memset (&osym, 0, sizeof (osym));
9956 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9957 osym.st_shndx = SHN_ABS;
9958 if (!elf_link_output_symstrtab (flinfo,
9959 (input_bfd->lto_output ? NULL
9960 : input_bfd->filename),
9961 &osym, bfd_abs_section_ptr,
9962 NULL))
9963 return FALSE;
9964 }
9965
9966 osym = *isym;
9967
9968 /* Adjust the section index for the output file. */
9969 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9970 isec->output_section);
9971 if (osym.st_shndx == SHN_BAD)
9972 return FALSE;
9973
9974 /* ELF symbols in relocatable files are section relative, but
9975 in executable files they are virtual addresses. Note that
9976 this code assumes that all ELF sections have an associated
9977 BFD section with a reasonable value for output_offset; below
9978 we assume that they also have a reasonable value for
9979 output_section. Any special sections must be set up to meet
9980 these requirements. */
9981 osym.st_value += isec->output_offset;
9982 if (!bfd_link_relocatable (flinfo->info))
9983 {
9984 osym.st_value += isec->output_section->vma;
9985 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
9986 {
9987 /* STT_TLS symbols are relative to PT_TLS segment base. */
9988 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
9989 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
9990 }
9991 }
9992
9993 indx = bfd_get_symcount (output_bfd);
9994 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
9995 if (ret == 0)
9996 return FALSE;
9997 else if (ret == 1)
9998 *pindex = indx;
9999 }
10000
10001 if (bed->s->arch_size == 32)
10002 {
10003 r_type_mask = 0xff;
10004 r_sym_shift = 8;
10005 address_size = 4;
10006 }
10007 else
10008 {
10009 r_type_mask = 0xffffffff;
10010 r_sym_shift = 32;
10011 address_size = 8;
10012 }
10013
10014 /* Relocate the contents of each section. */
10015 sym_hashes = elf_sym_hashes (input_bfd);
10016 for (o = input_bfd->sections; o != NULL; o = o->next)
10017 {
10018 bfd_byte *contents;
10019
10020 if (! o->linker_mark)
10021 {
10022 /* This section was omitted from the link. */
10023 continue;
10024 }
10025
10026 if (bfd_link_relocatable (flinfo->info)
10027 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10028 {
10029 /* Deal with the group signature symbol. */
10030 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10031 unsigned long symndx = sec_data->this_hdr.sh_info;
10032 asection *osec = o->output_section;
10033
10034 if (symndx >= locsymcount
10035 || (elf_bad_symtab (input_bfd)
10036 && flinfo->sections[symndx] == NULL))
10037 {
10038 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10039 while (h->root.type == bfd_link_hash_indirect
10040 || h->root.type == bfd_link_hash_warning)
10041 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10042 /* Arrange for symbol to be output. */
10043 h->indx = -2;
10044 elf_section_data (osec)->this_hdr.sh_info = -2;
10045 }
10046 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10047 {
10048 /* We'll use the output section target_index. */
10049 asection *sec = flinfo->sections[symndx]->output_section;
10050 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10051 }
10052 else
10053 {
10054 if (flinfo->indices[symndx] == -1)
10055 {
10056 /* Otherwise output the local symbol now. */
10057 Elf_Internal_Sym sym = isymbuf[symndx];
10058 asection *sec = flinfo->sections[symndx]->output_section;
10059 const char *name;
10060 long indx;
10061 int ret;
10062
10063 name = bfd_elf_string_from_elf_section (input_bfd,
10064 symtab_hdr->sh_link,
10065 sym.st_name);
10066 if (name == NULL)
10067 return FALSE;
10068
10069 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10070 sec);
10071 if (sym.st_shndx == SHN_BAD)
10072 return FALSE;
10073
10074 sym.st_value += o->output_offset;
10075
10076 indx = bfd_get_symcount (output_bfd);
10077 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10078 NULL);
10079 if (ret == 0)
10080 return FALSE;
10081 else if (ret == 1)
10082 flinfo->indices[symndx] = indx;
10083 else
10084 abort ();
10085 }
10086 elf_section_data (osec)->this_hdr.sh_info
10087 = flinfo->indices[symndx];
10088 }
10089 }
10090
10091 if ((o->flags & SEC_HAS_CONTENTS) == 0
10092 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10093 continue;
10094
10095 if ((o->flags & SEC_LINKER_CREATED) != 0)
10096 {
10097 /* Section was created by _bfd_elf_link_create_dynamic_sections
10098 or somesuch. */
10099 continue;
10100 }
10101
10102 /* Get the contents of the section. They have been cached by a
10103 relaxation routine. Note that o is a section in an input
10104 file, so the contents field will not have been set by any of
10105 the routines which work on output files. */
10106 if (elf_section_data (o)->this_hdr.contents != NULL)
10107 {
10108 contents = elf_section_data (o)->this_hdr.contents;
10109 if (bed->caches_rawsize
10110 && o->rawsize != 0
10111 && o->rawsize < o->size)
10112 {
10113 memcpy (flinfo->contents, contents, o->rawsize);
10114 contents = flinfo->contents;
10115 }
10116 }
10117 else
10118 {
10119 contents = flinfo->contents;
10120 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10121 return FALSE;
10122 }
10123
10124 if ((o->flags & SEC_RELOC) != 0)
10125 {
10126 Elf_Internal_Rela *internal_relocs;
10127 Elf_Internal_Rela *rel, *relend;
10128 int action_discarded;
10129 int ret;
10130
10131 /* Get the swapped relocs. */
10132 internal_relocs
10133 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10134 flinfo->internal_relocs, FALSE);
10135 if (internal_relocs == NULL
10136 && o->reloc_count > 0)
10137 return FALSE;
10138
10139 /* We need to reverse-copy input .ctors/.dtors sections if
10140 they are placed in .init_array/.finit_array for output. */
10141 if (o->size > address_size
10142 && ((strncmp (o->name, ".ctors", 6) == 0
10143 && strcmp (o->output_section->name,
10144 ".init_array") == 0)
10145 || (strncmp (o->name, ".dtors", 6) == 0
10146 && strcmp (o->output_section->name,
10147 ".fini_array") == 0))
10148 && (o->name[6] == 0 || o->name[6] == '.'))
10149 {
10150 if (o->size != o->reloc_count * address_size)
10151 {
10152 (*_bfd_error_handler)
10153 (_("error: %B: size of section %A is not "
10154 "multiple of address size"),
10155 input_bfd, o);
10156 bfd_set_error (bfd_error_on_input);
10157 return FALSE;
10158 }
10159 o->flags |= SEC_ELF_REVERSE_COPY;
10160 }
10161
10162 action_discarded = -1;
10163 if (!elf_section_ignore_discarded_relocs (o))
10164 action_discarded = (*bed->action_discarded) (o);
10165
10166 /* Run through the relocs evaluating complex reloc symbols and
10167 looking for relocs against symbols from discarded sections
10168 or section symbols from removed link-once sections.
10169 Complain about relocs against discarded sections. Zero
10170 relocs against removed link-once sections. */
10171
10172 rel = internal_relocs;
10173 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
10174 for ( ; rel < relend; rel++)
10175 {
10176 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10177 unsigned int s_type;
10178 asection **ps, *sec;
10179 struct elf_link_hash_entry *h = NULL;
10180 const char *sym_name;
10181
10182 if (r_symndx == STN_UNDEF)
10183 continue;
10184
10185 if (r_symndx >= locsymcount
10186 || (elf_bad_symtab (input_bfd)
10187 && flinfo->sections[r_symndx] == NULL))
10188 {
10189 h = sym_hashes[r_symndx - extsymoff];
10190
10191 /* Badly formatted input files can contain relocs that
10192 reference non-existant symbols. Check here so that
10193 we do not seg fault. */
10194 if (h == NULL)
10195 {
10196 char buffer [32];
10197
10198 sprintf_vma (buffer, rel->r_info);
10199 (*_bfd_error_handler)
10200 (_("error: %B contains a reloc (0x%s) for section %A "
10201 "that references a non-existent global symbol"),
10202 input_bfd, o, buffer);
10203 bfd_set_error (bfd_error_bad_value);
10204 return FALSE;
10205 }
10206
10207 while (h->root.type == bfd_link_hash_indirect
10208 || h->root.type == bfd_link_hash_warning)
10209 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10210
10211 s_type = h->type;
10212
10213 /* If a plugin symbol is referenced from a non-IR file,
10214 mark the symbol as undefined. Note that the
10215 linker may attach linker created dynamic sections
10216 to the plugin bfd. Symbols defined in linker
10217 created sections are not plugin symbols. */
10218 if (h->root.non_ir_ref
10219 && (h->root.type == bfd_link_hash_defined
10220 || h->root.type == bfd_link_hash_defweak)
10221 && (h->root.u.def.section->flags
10222 & SEC_LINKER_CREATED) == 0
10223 && h->root.u.def.section->owner != NULL
10224 && (h->root.u.def.section->owner->flags
10225 & BFD_PLUGIN) != 0)
10226 {
10227 h->root.type = bfd_link_hash_undefined;
10228 h->root.u.undef.abfd = h->root.u.def.section->owner;
10229 }
10230
10231 ps = NULL;
10232 if (h->root.type == bfd_link_hash_defined
10233 || h->root.type == bfd_link_hash_defweak)
10234 ps = &h->root.u.def.section;
10235
10236 sym_name = h->root.root.string;
10237 }
10238 else
10239 {
10240 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10241
10242 s_type = ELF_ST_TYPE (sym->st_info);
10243 ps = &flinfo->sections[r_symndx];
10244 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10245 sym, *ps);
10246 }
10247
10248 if ((s_type == STT_RELC || s_type == STT_SRELC)
10249 && !bfd_link_relocatable (flinfo->info))
10250 {
10251 bfd_vma val;
10252 bfd_vma dot = (rel->r_offset
10253 + o->output_offset + o->output_section->vma);
10254 #ifdef DEBUG
10255 printf ("Encountered a complex symbol!");
10256 printf (" (input_bfd %s, section %s, reloc %ld\n",
10257 input_bfd->filename, o->name,
10258 (long) (rel - internal_relocs));
10259 printf (" symbol: idx %8.8lx, name %s\n",
10260 r_symndx, sym_name);
10261 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10262 (unsigned long) rel->r_info,
10263 (unsigned long) rel->r_offset);
10264 #endif
10265 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10266 isymbuf, locsymcount, s_type == STT_SRELC))
10267 return FALSE;
10268
10269 /* Symbol evaluated OK. Update to absolute value. */
10270 set_symbol_value (input_bfd, isymbuf, locsymcount,
10271 r_symndx, val);
10272 continue;
10273 }
10274
10275 if (action_discarded != -1 && ps != NULL)
10276 {
10277 /* Complain if the definition comes from a
10278 discarded section. */
10279 if ((sec = *ps) != NULL && discarded_section (sec))
10280 {
10281 BFD_ASSERT (r_symndx != STN_UNDEF);
10282 if (action_discarded & COMPLAIN)
10283 (*flinfo->info->callbacks->einfo)
10284 (_("%X`%s' referenced in section `%A' of %B: "
10285 "defined in discarded section `%A' of %B\n"),
10286 sym_name, o, input_bfd, sec, sec->owner);
10287
10288 /* Try to do the best we can to support buggy old
10289 versions of gcc. Pretend that the symbol is
10290 really defined in the kept linkonce section.
10291 FIXME: This is quite broken. Modifying the
10292 symbol here means we will be changing all later
10293 uses of the symbol, not just in this section. */
10294 if (action_discarded & PRETEND)
10295 {
10296 asection *kept;
10297
10298 kept = _bfd_elf_check_kept_section (sec,
10299 flinfo->info);
10300 if (kept != NULL)
10301 {
10302 *ps = kept;
10303 continue;
10304 }
10305 }
10306 }
10307 }
10308 }
10309
10310 /* Relocate the section by invoking a back end routine.
10311
10312 The back end routine is responsible for adjusting the
10313 section contents as necessary, and (if using Rela relocs
10314 and generating a relocatable output file) adjusting the
10315 reloc addend as necessary.
10316
10317 The back end routine does not have to worry about setting
10318 the reloc address or the reloc symbol index.
10319
10320 The back end routine is given a pointer to the swapped in
10321 internal symbols, and can access the hash table entries
10322 for the external symbols via elf_sym_hashes (input_bfd).
10323
10324 When generating relocatable output, the back end routine
10325 must handle STB_LOCAL/STT_SECTION symbols specially. The
10326 output symbol is going to be a section symbol
10327 corresponding to the output section, which will require
10328 the addend to be adjusted. */
10329
10330 ret = (*relocate_section) (output_bfd, flinfo->info,
10331 input_bfd, o, contents,
10332 internal_relocs,
10333 isymbuf,
10334 flinfo->sections);
10335 if (!ret)
10336 return FALSE;
10337
10338 if (ret == 2
10339 || bfd_link_relocatable (flinfo->info)
10340 || flinfo->info->emitrelocations)
10341 {
10342 Elf_Internal_Rela *irela;
10343 Elf_Internal_Rela *irelaend, *irelamid;
10344 bfd_vma last_offset;
10345 struct elf_link_hash_entry **rel_hash;
10346 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10347 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10348 unsigned int next_erel;
10349 bfd_boolean rela_normal;
10350 struct bfd_elf_section_data *esdi, *esdo;
10351
10352 esdi = elf_section_data (o);
10353 esdo = elf_section_data (o->output_section);
10354 rela_normal = FALSE;
10355
10356 /* Adjust the reloc addresses and symbol indices. */
10357
10358 irela = internal_relocs;
10359 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
10360 rel_hash = esdo->rel.hashes + esdo->rel.count;
10361 /* We start processing the REL relocs, if any. When we reach
10362 IRELAMID in the loop, we switch to the RELA relocs. */
10363 irelamid = irela;
10364 if (esdi->rel.hdr != NULL)
10365 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10366 * bed->s->int_rels_per_ext_rel);
10367 rel_hash_list = rel_hash;
10368 rela_hash_list = NULL;
10369 last_offset = o->output_offset;
10370 if (!bfd_link_relocatable (flinfo->info))
10371 last_offset += o->output_section->vma;
10372 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10373 {
10374 unsigned long r_symndx;
10375 asection *sec;
10376 Elf_Internal_Sym sym;
10377
10378 if (next_erel == bed->s->int_rels_per_ext_rel)
10379 {
10380 rel_hash++;
10381 next_erel = 0;
10382 }
10383
10384 if (irela == irelamid)
10385 {
10386 rel_hash = esdo->rela.hashes + esdo->rela.count;
10387 rela_hash_list = rel_hash;
10388 rela_normal = bed->rela_normal;
10389 }
10390
10391 irela->r_offset = _bfd_elf_section_offset (output_bfd,
10392 flinfo->info, o,
10393 irela->r_offset);
10394 if (irela->r_offset >= (bfd_vma) -2)
10395 {
10396 /* This is a reloc for a deleted entry or somesuch.
10397 Turn it into an R_*_NONE reloc, at the same
10398 offset as the last reloc. elf_eh_frame.c and
10399 bfd_elf_discard_info rely on reloc offsets
10400 being ordered. */
10401 irela->r_offset = last_offset;
10402 irela->r_info = 0;
10403 irela->r_addend = 0;
10404 continue;
10405 }
10406
10407 irela->r_offset += o->output_offset;
10408
10409 /* Relocs in an executable have to be virtual addresses. */
10410 if (!bfd_link_relocatable (flinfo->info))
10411 irela->r_offset += o->output_section->vma;
10412
10413 last_offset = irela->r_offset;
10414
10415 r_symndx = irela->r_info >> r_sym_shift;
10416 if (r_symndx == STN_UNDEF)
10417 continue;
10418
10419 if (r_symndx >= locsymcount
10420 || (elf_bad_symtab (input_bfd)
10421 && flinfo->sections[r_symndx] == NULL))
10422 {
10423 struct elf_link_hash_entry *rh;
10424 unsigned long indx;
10425
10426 /* This is a reloc against a global symbol. We
10427 have not yet output all the local symbols, so
10428 we do not know the symbol index of any global
10429 symbol. We set the rel_hash entry for this
10430 reloc to point to the global hash table entry
10431 for this symbol. The symbol index is then
10432 set at the end of bfd_elf_final_link. */
10433 indx = r_symndx - extsymoff;
10434 rh = elf_sym_hashes (input_bfd)[indx];
10435 while (rh->root.type == bfd_link_hash_indirect
10436 || rh->root.type == bfd_link_hash_warning)
10437 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10438
10439 /* Setting the index to -2 tells
10440 elf_link_output_extsym that this symbol is
10441 used by a reloc. */
10442 BFD_ASSERT (rh->indx < 0);
10443 rh->indx = -2;
10444
10445 *rel_hash = rh;
10446
10447 continue;
10448 }
10449
10450 /* This is a reloc against a local symbol. */
10451
10452 *rel_hash = NULL;
10453 sym = isymbuf[r_symndx];
10454 sec = flinfo->sections[r_symndx];
10455 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10456 {
10457 /* I suppose the backend ought to fill in the
10458 section of any STT_SECTION symbol against a
10459 processor specific section. */
10460 r_symndx = STN_UNDEF;
10461 if (bfd_is_abs_section (sec))
10462 ;
10463 else if (sec == NULL || sec->owner == NULL)
10464 {
10465 bfd_set_error (bfd_error_bad_value);
10466 return FALSE;
10467 }
10468 else
10469 {
10470 asection *osec = sec->output_section;
10471
10472 /* If we have discarded a section, the output
10473 section will be the absolute section. In
10474 case of discarded SEC_MERGE sections, use
10475 the kept section. relocate_section should
10476 have already handled discarded linkonce
10477 sections. */
10478 if (bfd_is_abs_section (osec)
10479 && sec->kept_section != NULL
10480 && sec->kept_section->output_section != NULL)
10481 {
10482 osec = sec->kept_section->output_section;
10483 irela->r_addend -= osec->vma;
10484 }
10485
10486 if (!bfd_is_abs_section (osec))
10487 {
10488 r_symndx = osec->target_index;
10489 if (r_symndx == STN_UNDEF)
10490 {
10491 irela->r_addend += osec->vma;
10492 osec = _bfd_nearby_section (output_bfd, osec,
10493 osec->vma);
10494 irela->r_addend -= osec->vma;
10495 r_symndx = osec->target_index;
10496 }
10497 }
10498 }
10499
10500 /* Adjust the addend according to where the
10501 section winds up in the output section. */
10502 if (rela_normal)
10503 irela->r_addend += sec->output_offset;
10504 }
10505 else
10506 {
10507 if (flinfo->indices[r_symndx] == -1)
10508 {
10509 unsigned long shlink;
10510 const char *name;
10511 asection *osec;
10512 long indx;
10513
10514 if (flinfo->info->strip == strip_all)
10515 {
10516 /* You can't do ld -r -s. */
10517 bfd_set_error (bfd_error_invalid_operation);
10518 return FALSE;
10519 }
10520
10521 /* This symbol was skipped earlier, but
10522 since it is needed by a reloc, we
10523 must output it now. */
10524 shlink = symtab_hdr->sh_link;
10525 name = (bfd_elf_string_from_elf_section
10526 (input_bfd, shlink, sym.st_name));
10527 if (name == NULL)
10528 return FALSE;
10529
10530 osec = sec->output_section;
10531 sym.st_shndx =
10532 _bfd_elf_section_from_bfd_section (output_bfd,
10533 osec);
10534 if (sym.st_shndx == SHN_BAD)
10535 return FALSE;
10536
10537 sym.st_value += sec->output_offset;
10538 if (!bfd_link_relocatable (flinfo->info))
10539 {
10540 sym.st_value += osec->vma;
10541 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10542 {
10543 /* STT_TLS symbols are relative to PT_TLS
10544 segment base. */
10545 BFD_ASSERT (elf_hash_table (flinfo->info)
10546 ->tls_sec != NULL);
10547 sym.st_value -= (elf_hash_table (flinfo->info)
10548 ->tls_sec->vma);
10549 }
10550 }
10551
10552 indx = bfd_get_symcount (output_bfd);
10553 ret = elf_link_output_symstrtab (flinfo, name,
10554 &sym, sec,
10555 NULL);
10556 if (ret == 0)
10557 return FALSE;
10558 else if (ret == 1)
10559 flinfo->indices[r_symndx] = indx;
10560 else
10561 abort ();
10562 }
10563
10564 r_symndx = flinfo->indices[r_symndx];
10565 }
10566
10567 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10568 | (irela->r_info & r_type_mask));
10569 }
10570
10571 /* Swap out the relocs. */
10572 input_rel_hdr = esdi->rel.hdr;
10573 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
10574 {
10575 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10576 input_rel_hdr,
10577 internal_relocs,
10578 rel_hash_list))
10579 return FALSE;
10580 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10581 * bed->s->int_rels_per_ext_rel);
10582 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
10583 }
10584
10585 input_rela_hdr = esdi->rela.hdr;
10586 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10587 {
10588 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10589 input_rela_hdr,
10590 internal_relocs,
10591 rela_hash_list))
10592 return FALSE;
10593 }
10594 }
10595 }
10596
10597 /* Write out the modified section contents. */
10598 if (bed->elf_backend_write_section
10599 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
10600 contents))
10601 {
10602 /* Section written out. */
10603 }
10604 else switch (o->sec_info_type)
10605 {
10606 case SEC_INFO_TYPE_STABS:
10607 if (! (_bfd_write_section_stabs
10608 (output_bfd,
10609 &elf_hash_table (flinfo->info)->stab_info,
10610 o, &elf_section_data (o)->sec_info, contents)))
10611 return FALSE;
10612 break;
10613 case SEC_INFO_TYPE_MERGE:
10614 if (! _bfd_write_merged_section (output_bfd, o,
10615 elf_section_data (o)->sec_info))
10616 return FALSE;
10617 break;
10618 case SEC_INFO_TYPE_EH_FRAME:
10619 {
10620 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
10621 o, contents))
10622 return FALSE;
10623 }
10624 break;
10625 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10626 {
10627 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10628 flinfo->info,
10629 o, contents))
10630 return FALSE;
10631 }
10632 break;
10633 default:
10634 {
10635 if (! (o->flags & SEC_EXCLUDE))
10636 {
10637 file_ptr offset = (file_ptr) o->output_offset;
10638 bfd_size_type todo = o->size;
10639
10640 offset *= bfd_octets_per_byte (output_bfd);
10641
10642 if ((o->flags & SEC_ELF_REVERSE_COPY))
10643 {
10644 /* Reverse-copy input section to output. */
10645 do
10646 {
10647 todo -= address_size;
10648 if (! bfd_set_section_contents (output_bfd,
10649 o->output_section,
10650 contents + todo,
10651 offset,
10652 address_size))
10653 return FALSE;
10654 if (todo == 0)
10655 break;
10656 offset += address_size;
10657 }
10658 while (1);
10659 }
10660 else if (! bfd_set_section_contents (output_bfd,
10661 o->output_section,
10662 contents,
10663 offset, todo))
10664 return FALSE;
10665 }
10666 }
10667 break;
10668 }
10669 }
10670
10671 return TRUE;
10672 }
10673
10674 /* Generate a reloc when linking an ELF file. This is a reloc
10675 requested by the linker, and does not come from any input file. This
10676 is used to build constructor and destructor tables when linking
10677 with -Ur. */
10678
10679 static bfd_boolean
10680 elf_reloc_link_order (bfd *output_bfd,
10681 struct bfd_link_info *info,
10682 asection *output_section,
10683 struct bfd_link_order *link_order)
10684 {
10685 reloc_howto_type *howto;
10686 long indx;
10687 bfd_vma offset;
10688 bfd_vma addend;
10689 struct bfd_elf_section_reloc_data *reldata;
10690 struct elf_link_hash_entry **rel_hash_ptr;
10691 Elf_Internal_Shdr *rel_hdr;
10692 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10693 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10694 bfd_byte *erel;
10695 unsigned int i;
10696 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
10697
10698 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10699 if (howto == NULL)
10700 {
10701 bfd_set_error (bfd_error_bad_value);
10702 return FALSE;
10703 }
10704
10705 addend = link_order->u.reloc.p->addend;
10706
10707 if (esdo->rel.hdr)
10708 reldata = &esdo->rel;
10709 else if (esdo->rela.hdr)
10710 reldata = &esdo->rela;
10711 else
10712 {
10713 reldata = NULL;
10714 BFD_ASSERT (0);
10715 }
10716
10717 /* Figure out the symbol index. */
10718 rel_hash_ptr = reldata->hashes + reldata->count;
10719 if (link_order->type == bfd_section_reloc_link_order)
10720 {
10721 indx = link_order->u.reloc.p->u.section->target_index;
10722 BFD_ASSERT (indx != 0);
10723 *rel_hash_ptr = NULL;
10724 }
10725 else
10726 {
10727 struct elf_link_hash_entry *h;
10728
10729 /* Treat a reloc against a defined symbol as though it were
10730 actually against the section. */
10731 h = ((struct elf_link_hash_entry *)
10732 bfd_wrapped_link_hash_lookup (output_bfd, info,
10733 link_order->u.reloc.p->u.name,
10734 FALSE, FALSE, TRUE));
10735 if (h != NULL
10736 && (h->root.type == bfd_link_hash_defined
10737 || h->root.type == bfd_link_hash_defweak))
10738 {
10739 asection *section;
10740
10741 section = h->root.u.def.section;
10742 indx = section->output_section->target_index;
10743 *rel_hash_ptr = NULL;
10744 /* It seems that we ought to add the symbol value to the
10745 addend here, but in practice it has already been added
10746 because it was passed to constructor_callback. */
10747 addend += section->output_section->vma + section->output_offset;
10748 }
10749 else if (h != NULL)
10750 {
10751 /* Setting the index to -2 tells elf_link_output_extsym that
10752 this symbol is used by a reloc. */
10753 h->indx = -2;
10754 *rel_hash_ptr = h;
10755 indx = 0;
10756 }
10757 else
10758 {
10759 if (! ((*info->callbacks->unattached_reloc)
10760 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
10761 return FALSE;
10762 indx = 0;
10763 }
10764 }
10765
10766 /* If this is an inplace reloc, we must write the addend into the
10767 object file. */
10768 if (howto->partial_inplace && addend != 0)
10769 {
10770 bfd_size_type size;
10771 bfd_reloc_status_type rstat;
10772 bfd_byte *buf;
10773 bfd_boolean ok;
10774 const char *sym_name;
10775
10776 size = (bfd_size_type) bfd_get_reloc_size (howto);
10777 buf = (bfd_byte *) bfd_zmalloc (size);
10778 if (buf == NULL && size != 0)
10779 return FALSE;
10780 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10781 switch (rstat)
10782 {
10783 case bfd_reloc_ok:
10784 break;
10785
10786 default:
10787 case bfd_reloc_outofrange:
10788 abort ();
10789
10790 case bfd_reloc_overflow:
10791 if (link_order->type == bfd_section_reloc_link_order)
10792 sym_name = bfd_section_name (output_bfd,
10793 link_order->u.reloc.p->u.section);
10794 else
10795 sym_name = link_order->u.reloc.p->u.name;
10796 if (! ((*info->callbacks->reloc_overflow)
10797 (info, NULL, sym_name, howto->name, addend, NULL,
10798 NULL, (bfd_vma) 0)))
10799 {
10800 free (buf);
10801 return FALSE;
10802 }
10803 break;
10804 }
10805
10806 ok = bfd_set_section_contents (output_bfd, output_section, buf,
10807 link_order->offset
10808 * bfd_octets_per_byte (output_bfd),
10809 size);
10810 free (buf);
10811 if (! ok)
10812 return FALSE;
10813 }
10814
10815 /* The address of a reloc is relative to the section in a
10816 relocatable file, and is a virtual address in an executable
10817 file. */
10818 offset = link_order->offset;
10819 if (! bfd_link_relocatable (info))
10820 offset += output_section->vma;
10821
10822 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10823 {
10824 irel[i].r_offset = offset;
10825 irel[i].r_info = 0;
10826 irel[i].r_addend = 0;
10827 }
10828 if (bed->s->arch_size == 32)
10829 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10830 else
10831 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10832
10833 rel_hdr = reldata->hdr;
10834 erel = rel_hdr->contents;
10835 if (rel_hdr->sh_type == SHT_REL)
10836 {
10837 erel += reldata->count * bed->s->sizeof_rel;
10838 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10839 }
10840 else
10841 {
10842 irel[0].r_addend = addend;
10843 erel += reldata->count * bed->s->sizeof_rela;
10844 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10845 }
10846
10847 ++reldata->count;
10848
10849 return TRUE;
10850 }
10851
10852
10853 /* Get the output vma of the section pointed to by the sh_link field. */
10854
10855 static bfd_vma
10856 elf_get_linked_section_vma (struct bfd_link_order *p)
10857 {
10858 Elf_Internal_Shdr **elf_shdrp;
10859 asection *s;
10860 int elfsec;
10861
10862 s = p->u.indirect.section;
10863 elf_shdrp = elf_elfsections (s->owner);
10864 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10865 elfsec = elf_shdrp[elfsec]->sh_link;
10866 /* PR 290:
10867 The Intel C compiler generates SHT_IA_64_UNWIND with
10868 SHF_LINK_ORDER. But it doesn't set the sh_link or
10869 sh_info fields. Hence we could get the situation
10870 where elfsec is 0. */
10871 if (elfsec == 0)
10872 {
10873 const struct elf_backend_data *bed
10874 = get_elf_backend_data (s->owner);
10875 if (bed->link_order_error_handler)
10876 bed->link_order_error_handler
10877 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
10878 return 0;
10879 }
10880 else
10881 {
10882 s = elf_shdrp[elfsec]->bfd_section;
10883 return s->output_section->vma + s->output_offset;
10884 }
10885 }
10886
10887
10888 /* Compare two sections based on the locations of the sections they are
10889 linked to. Used by elf_fixup_link_order. */
10890
10891 static int
10892 compare_link_order (const void * a, const void * b)
10893 {
10894 bfd_vma apos;
10895 bfd_vma bpos;
10896
10897 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10898 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10899 if (apos < bpos)
10900 return -1;
10901 return apos > bpos;
10902 }
10903
10904
10905 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10906 order as their linked sections. Returns false if this could not be done
10907 because an output section includes both ordered and unordered
10908 sections. Ideally we'd do this in the linker proper. */
10909
10910 static bfd_boolean
10911 elf_fixup_link_order (bfd *abfd, asection *o)
10912 {
10913 int seen_linkorder;
10914 int seen_other;
10915 int n;
10916 struct bfd_link_order *p;
10917 bfd *sub;
10918 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10919 unsigned elfsec;
10920 struct bfd_link_order **sections;
10921 asection *s, *other_sec, *linkorder_sec;
10922 bfd_vma offset;
10923
10924 other_sec = NULL;
10925 linkorder_sec = NULL;
10926 seen_other = 0;
10927 seen_linkorder = 0;
10928 for (p = o->map_head.link_order; p != NULL; p = p->next)
10929 {
10930 if (p->type == bfd_indirect_link_order)
10931 {
10932 s = p->u.indirect.section;
10933 sub = s->owner;
10934 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10935 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
10936 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
10937 && elfsec < elf_numsections (sub)
10938 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
10939 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
10940 {
10941 seen_linkorder++;
10942 linkorder_sec = s;
10943 }
10944 else
10945 {
10946 seen_other++;
10947 other_sec = s;
10948 }
10949 }
10950 else
10951 seen_other++;
10952
10953 if (seen_other && seen_linkorder)
10954 {
10955 if (other_sec && linkorder_sec)
10956 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
10957 o, linkorder_sec,
10958 linkorder_sec->owner, other_sec,
10959 other_sec->owner);
10960 else
10961 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
10962 o);
10963 bfd_set_error (bfd_error_bad_value);
10964 return FALSE;
10965 }
10966 }
10967
10968 if (!seen_linkorder)
10969 return TRUE;
10970
10971 sections = (struct bfd_link_order **)
10972 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
10973 if (sections == NULL)
10974 return FALSE;
10975 seen_linkorder = 0;
10976
10977 for (p = o->map_head.link_order; p != NULL; p = p->next)
10978 {
10979 sections[seen_linkorder++] = p;
10980 }
10981 /* Sort the input sections in the order of their linked section. */
10982 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
10983 compare_link_order);
10984
10985 /* Change the offsets of the sections. */
10986 offset = 0;
10987 for (n = 0; n < seen_linkorder; n++)
10988 {
10989 s = sections[n]->u.indirect.section;
10990 offset &= ~(bfd_vma) 0 << s->alignment_power;
10991 s->output_offset = offset / bfd_octets_per_byte (abfd);
10992 sections[n]->offset = offset;
10993 offset += sections[n]->size;
10994 }
10995
10996 free (sections);
10997 return TRUE;
10998 }
10999
11000 static void
11001 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11002 {
11003 asection *o;
11004
11005 if (flinfo->symstrtab != NULL)
11006 _bfd_elf_strtab_free (flinfo->symstrtab);
11007 if (flinfo->contents != NULL)
11008 free (flinfo->contents);
11009 if (flinfo->external_relocs != NULL)
11010 free (flinfo->external_relocs);
11011 if (flinfo->internal_relocs != NULL)
11012 free (flinfo->internal_relocs);
11013 if (flinfo->external_syms != NULL)
11014 free (flinfo->external_syms);
11015 if (flinfo->locsym_shndx != NULL)
11016 free (flinfo->locsym_shndx);
11017 if (flinfo->internal_syms != NULL)
11018 free (flinfo->internal_syms);
11019 if (flinfo->indices != NULL)
11020 free (flinfo->indices);
11021 if (flinfo->sections != NULL)
11022 free (flinfo->sections);
11023 if (flinfo->symshndxbuf != NULL)
11024 free (flinfo->symshndxbuf);
11025 for (o = obfd->sections; o != NULL; o = o->next)
11026 {
11027 struct bfd_elf_section_data *esdo = elf_section_data (o);
11028 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11029 free (esdo->rel.hashes);
11030 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11031 free (esdo->rela.hashes);
11032 }
11033 }
11034
11035 /* Do the final step of an ELF link. */
11036
11037 bfd_boolean
11038 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11039 {
11040 bfd_boolean dynamic;
11041 bfd_boolean emit_relocs;
11042 bfd *dynobj;
11043 struct elf_final_link_info flinfo;
11044 asection *o;
11045 struct bfd_link_order *p;
11046 bfd *sub;
11047 bfd_size_type max_contents_size;
11048 bfd_size_type max_external_reloc_size;
11049 bfd_size_type max_internal_reloc_count;
11050 bfd_size_type max_sym_count;
11051 bfd_size_type max_sym_shndx_count;
11052 Elf_Internal_Sym elfsym;
11053 unsigned int i;
11054 Elf_Internal_Shdr *symtab_hdr;
11055 Elf_Internal_Shdr *symtab_shndx_hdr;
11056 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11057 struct elf_outext_info eoinfo;
11058 bfd_boolean merged;
11059 size_t relativecount = 0;
11060 asection *reldyn = 0;
11061 bfd_size_type amt;
11062 asection *attr_section = NULL;
11063 bfd_vma attr_size = 0;
11064 const char *std_attrs_section;
11065
11066 if (! is_elf_hash_table (info->hash))
11067 return FALSE;
11068
11069 if (bfd_link_pic (info))
11070 abfd->flags |= DYNAMIC;
11071
11072 dynamic = elf_hash_table (info)->dynamic_sections_created;
11073 dynobj = elf_hash_table (info)->dynobj;
11074
11075 emit_relocs = (bfd_link_relocatable (info)
11076 || info->emitrelocations);
11077
11078 flinfo.info = info;
11079 flinfo.output_bfd = abfd;
11080 flinfo.symstrtab = _bfd_elf_strtab_init ();
11081 if (flinfo.symstrtab == NULL)
11082 return FALSE;
11083
11084 if (! dynamic)
11085 {
11086 flinfo.hash_sec = NULL;
11087 flinfo.symver_sec = NULL;
11088 }
11089 else
11090 {
11091 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11092 /* Note that dynsym_sec can be NULL (on VMS). */
11093 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11094 /* Note that it is OK if symver_sec is NULL. */
11095 }
11096
11097 flinfo.contents = NULL;
11098 flinfo.external_relocs = NULL;
11099 flinfo.internal_relocs = NULL;
11100 flinfo.external_syms = NULL;
11101 flinfo.locsym_shndx = NULL;
11102 flinfo.internal_syms = NULL;
11103 flinfo.indices = NULL;
11104 flinfo.sections = NULL;
11105 flinfo.symshndxbuf = NULL;
11106 flinfo.filesym_count = 0;
11107
11108 /* The object attributes have been merged. Remove the input
11109 sections from the link, and set the contents of the output
11110 secton. */
11111 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11112 for (o = abfd->sections; o != NULL; o = o->next)
11113 {
11114 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11115 || strcmp (o->name, ".gnu.attributes") == 0)
11116 {
11117 for (p = o->map_head.link_order; p != NULL; p = p->next)
11118 {
11119 asection *input_section;
11120
11121 if (p->type != bfd_indirect_link_order)
11122 continue;
11123 input_section = p->u.indirect.section;
11124 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11125 elf_link_input_bfd ignores this section. */
11126 input_section->flags &= ~SEC_HAS_CONTENTS;
11127 }
11128
11129 attr_size = bfd_elf_obj_attr_size (abfd);
11130 if (attr_size)
11131 {
11132 bfd_set_section_size (abfd, o, attr_size);
11133 attr_section = o;
11134 /* Skip this section later on. */
11135 o->map_head.link_order = NULL;
11136 }
11137 else
11138 o->flags |= SEC_EXCLUDE;
11139 }
11140 }
11141
11142 /* Count up the number of relocations we will output for each output
11143 section, so that we know the sizes of the reloc sections. We
11144 also figure out some maximum sizes. */
11145 max_contents_size = 0;
11146 max_external_reloc_size = 0;
11147 max_internal_reloc_count = 0;
11148 max_sym_count = 0;
11149 max_sym_shndx_count = 0;
11150 merged = FALSE;
11151 for (o = abfd->sections; o != NULL; o = o->next)
11152 {
11153 struct bfd_elf_section_data *esdo = elf_section_data (o);
11154 o->reloc_count = 0;
11155
11156 for (p = o->map_head.link_order; p != NULL; p = p->next)
11157 {
11158 unsigned int reloc_count = 0;
11159 unsigned int additional_reloc_count = 0;
11160 struct bfd_elf_section_data *esdi = NULL;
11161
11162 if (p->type == bfd_section_reloc_link_order
11163 || p->type == bfd_symbol_reloc_link_order)
11164 reloc_count = 1;
11165 else if (p->type == bfd_indirect_link_order)
11166 {
11167 asection *sec;
11168
11169 sec = p->u.indirect.section;
11170 esdi = elf_section_data (sec);
11171
11172 /* Mark all sections which are to be included in the
11173 link. This will normally be every section. We need
11174 to do this so that we can identify any sections which
11175 the linker has decided to not include. */
11176 sec->linker_mark = TRUE;
11177
11178 if (sec->flags & SEC_MERGE)
11179 merged = TRUE;
11180
11181 if (esdo->this_hdr.sh_type == SHT_REL
11182 || esdo->this_hdr.sh_type == SHT_RELA)
11183 /* Some backends use reloc_count in relocation sections
11184 to count particular types of relocs. Of course,
11185 reloc sections themselves can't have relocations. */
11186 reloc_count = 0;
11187 else if (emit_relocs)
11188 {
11189 reloc_count = sec->reloc_count;
11190 if (bed->elf_backend_count_additional_relocs)
11191 {
11192 int c;
11193 c = (*bed->elf_backend_count_additional_relocs) (sec);
11194 additional_reloc_count += c;
11195 }
11196 }
11197 else if (bed->elf_backend_count_relocs)
11198 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11199
11200 if (sec->rawsize > max_contents_size)
11201 max_contents_size = sec->rawsize;
11202 if (sec->size > max_contents_size)
11203 max_contents_size = sec->size;
11204
11205 /* We are interested in just local symbols, not all
11206 symbols. */
11207 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11208 && (sec->owner->flags & DYNAMIC) == 0)
11209 {
11210 size_t sym_count;
11211
11212 if (elf_bad_symtab (sec->owner))
11213 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11214 / bed->s->sizeof_sym);
11215 else
11216 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11217
11218 if (sym_count > max_sym_count)
11219 max_sym_count = sym_count;
11220
11221 if (sym_count > max_sym_shndx_count
11222 && elf_symtab_shndx_list (sec->owner) != NULL)
11223 max_sym_shndx_count = sym_count;
11224
11225 if ((sec->flags & SEC_RELOC) != 0)
11226 {
11227 size_t ext_size = 0;
11228
11229 if (esdi->rel.hdr != NULL)
11230 ext_size = esdi->rel.hdr->sh_size;
11231 if (esdi->rela.hdr != NULL)
11232 ext_size += esdi->rela.hdr->sh_size;
11233
11234 if (ext_size > max_external_reloc_size)
11235 max_external_reloc_size = ext_size;
11236 if (sec->reloc_count > max_internal_reloc_count)
11237 max_internal_reloc_count = sec->reloc_count;
11238 }
11239 }
11240 }
11241
11242 if (reloc_count == 0)
11243 continue;
11244
11245 reloc_count += additional_reloc_count;
11246 o->reloc_count += reloc_count;
11247
11248 if (p->type == bfd_indirect_link_order && emit_relocs)
11249 {
11250 if (esdi->rel.hdr)
11251 {
11252 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11253 esdo->rel.count += additional_reloc_count;
11254 }
11255 if (esdi->rela.hdr)
11256 {
11257 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11258 esdo->rela.count += additional_reloc_count;
11259 }
11260 }
11261 else
11262 {
11263 if (o->use_rela_p)
11264 esdo->rela.count += reloc_count;
11265 else
11266 esdo->rel.count += reloc_count;
11267 }
11268 }
11269
11270 if (o->reloc_count > 0)
11271 o->flags |= SEC_RELOC;
11272 else
11273 {
11274 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11275 set it (this is probably a bug) and if it is set
11276 assign_section_numbers will create a reloc section. */
11277 o->flags &=~ SEC_RELOC;
11278 }
11279
11280 /* If the SEC_ALLOC flag is not set, force the section VMA to
11281 zero. This is done in elf_fake_sections as well, but forcing
11282 the VMA to 0 here will ensure that relocs against these
11283 sections are handled correctly. */
11284 if ((o->flags & SEC_ALLOC) == 0
11285 && ! o->user_set_vma)
11286 o->vma = 0;
11287 }
11288
11289 if (! bfd_link_relocatable (info) && merged)
11290 elf_link_hash_traverse (elf_hash_table (info),
11291 _bfd_elf_link_sec_merge_syms, abfd);
11292
11293 /* Figure out the file positions for everything but the symbol table
11294 and the relocs. We set symcount to force assign_section_numbers
11295 to create a symbol table. */
11296 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
11297 BFD_ASSERT (! abfd->output_has_begun);
11298 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11299 goto error_return;
11300
11301 /* Set sizes, and assign file positions for reloc sections. */
11302 for (o = abfd->sections; o != NULL; o = o->next)
11303 {
11304 struct bfd_elf_section_data *esdo = elf_section_data (o);
11305 if ((o->flags & SEC_RELOC) != 0)
11306 {
11307 if (esdo->rel.hdr
11308 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
11309 goto error_return;
11310
11311 if (esdo->rela.hdr
11312 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
11313 goto error_return;
11314 }
11315
11316 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11317 to count upwards while actually outputting the relocations. */
11318 esdo->rel.count = 0;
11319 esdo->rela.count = 0;
11320
11321 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11322 {
11323 /* Cache the section contents so that they can be compressed
11324 later. Use bfd_malloc since it will be freed by
11325 bfd_compress_section_contents. */
11326 unsigned char *contents = esdo->this_hdr.contents;
11327 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11328 abort ();
11329 contents
11330 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11331 if (contents == NULL)
11332 goto error_return;
11333 esdo->this_hdr.contents = contents;
11334 }
11335 }
11336
11337 /* We have now assigned file positions for all the sections except
11338 .symtab, .strtab, and non-loaded reloc sections. We start the
11339 .symtab section at the current file position, and write directly
11340 to it. We build the .strtab section in memory. */
11341 bfd_get_symcount (abfd) = 0;
11342 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11343 /* sh_name is set in prep_headers. */
11344 symtab_hdr->sh_type = SHT_SYMTAB;
11345 /* sh_flags, sh_addr and sh_size all start off zero. */
11346 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11347 /* sh_link is set in assign_section_numbers. */
11348 /* sh_info is set below. */
11349 /* sh_offset is set just below. */
11350 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
11351
11352 if (max_sym_count < 20)
11353 max_sym_count = 20;
11354 elf_hash_table (info)->strtabsize = max_sym_count;
11355 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11356 elf_hash_table (info)->strtab
11357 = (struct elf_sym_strtab *) bfd_malloc (amt);
11358 if (elf_hash_table (info)->strtab == NULL)
11359 goto error_return;
11360 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11361 flinfo.symshndxbuf
11362 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11363 ? (Elf_External_Sym_Shndx *) -1 : NULL);
11364
11365 if (info->strip != strip_all || emit_relocs)
11366 {
11367 file_ptr off = elf_next_file_pos (abfd);
11368
11369 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11370
11371 /* Note that at this point elf_next_file_pos (abfd) is
11372 incorrect. We do not yet know the size of the .symtab section.
11373 We correct next_file_pos below, after we do know the size. */
11374
11375 /* Start writing out the symbol table. The first symbol is always a
11376 dummy symbol. */
11377 elfsym.st_value = 0;
11378 elfsym.st_size = 0;
11379 elfsym.st_info = 0;
11380 elfsym.st_other = 0;
11381 elfsym.st_shndx = SHN_UNDEF;
11382 elfsym.st_target_internal = 0;
11383 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11384 bfd_und_section_ptr, NULL) != 1)
11385 goto error_return;
11386
11387 /* Output a symbol for each section. We output these even if we are
11388 discarding local symbols, since they are used for relocs. These
11389 symbols have no names. We store the index of each one in the
11390 index field of the section, so that we can find it again when
11391 outputting relocs. */
11392
11393 elfsym.st_size = 0;
11394 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11395 elfsym.st_other = 0;
11396 elfsym.st_value = 0;
11397 elfsym.st_target_internal = 0;
11398 for (i = 1; i < elf_numsections (abfd); i++)
11399 {
11400 o = bfd_section_from_elf_index (abfd, i);
11401 if (o != NULL)
11402 {
11403 o->target_index = bfd_get_symcount (abfd);
11404 elfsym.st_shndx = i;
11405 if (!bfd_link_relocatable (info))
11406 elfsym.st_value = o->vma;
11407 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11408 NULL) != 1)
11409 goto error_return;
11410 }
11411 }
11412 }
11413
11414 /* Allocate some memory to hold information read in from the input
11415 files. */
11416 if (max_contents_size != 0)
11417 {
11418 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11419 if (flinfo.contents == NULL)
11420 goto error_return;
11421 }
11422
11423 if (max_external_reloc_size != 0)
11424 {
11425 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11426 if (flinfo.external_relocs == NULL)
11427 goto error_return;
11428 }
11429
11430 if (max_internal_reloc_count != 0)
11431 {
11432 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11433 amt *= sizeof (Elf_Internal_Rela);
11434 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11435 if (flinfo.internal_relocs == NULL)
11436 goto error_return;
11437 }
11438
11439 if (max_sym_count != 0)
11440 {
11441 amt = max_sym_count * bed->s->sizeof_sym;
11442 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11443 if (flinfo.external_syms == NULL)
11444 goto error_return;
11445
11446 amt = max_sym_count * sizeof (Elf_Internal_Sym);
11447 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11448 if (flinfo.internal_syms == NULL)
11449 goto error_return;
11450
11451 amt = max_sym_count * sizeof (long);
11452 flinfo.indices = (long int *) bfd_malloc (amt);
11453 if (flinfo.indices == NULL)
11454 goto error_return;
11455
11456 amt = max_sym_count * sizeof (asection *);
11457 flinfo.sections = (asection **) bfd_malloc (amt);
11458 if (flinfo.sections == NULL)
11459 goto error_return;
11460 }
11461
11462 if (max_sym_shndx_count != 0)
11463 {
11464 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
11465 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11466 if (flinfo.locsym_shndx == NULL)
11467 goto error_return;
11468 }
11469
11470 if (elf_hash_table (info)->tls_sec)
11471 {
11472 bfd_vma base, end = 0;
11473 asection *sec;
11474
11475 for (sec = elf_hash_table (info)->tls_sec;
11476 sec && (sec->flags & SEC_THREAD_LOCAL);
11477 sec = sec->next)
11478 {
11479 bfd_size_type size = sec->size;
11480
11481 if (size == 0
11482 && (sec->flags & SEC_HAS_CONTENTS) == 0)
11483 {
11484 struct bfd_link_order *ord = sec->map_tail.link_order;
11485
11486 if (ord != NULL)
11487 size = ord->offset + ord->size;
11488 }
11489 end = sec->vma + size;
11490 }
11491 base = elf_hash_table (info)->tls_sec->vma;
11492 /* Only align end of TLS section if static TLS doesn't have special
11493 alignment requirements. */
11494 if (bed->static_tls_alignment == 1)
11495 end = align_power (end,
11496 elf_hash_table (info)->tls_sec->alignment_power);
11497 elf_hash_table (info)->tls_size = end - base;
11498 }
11499
11500 /* Reorder SHF_LINK_ORDER sections. */
11501 for (o = abfd->sections; o != NULL; o = o->next)
11502 {
11503 if (!elf_fixup_link_order (abfd, o))
11504 return FALSE;
11505 }
11506
11507 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11508 return FALSE;
11509
11510 /* Since ELF permits relocations to be against local symbols, we
11511 must have the local symbols available when we do the relocations.
11512 Since we would rather only read the local symbols once, and we
11513 would rather not keep them in memory, we handle all the
11514 relocations for a single input file at the same time.
11515
11516 Unfortunately, there is no way to know the total number of local
11517 symbols until we have seen all of them, and the local symbol
11518 indices precede the global symbol indices. This means that when
11519 we are generating relocatable output, and we see a reloc against
11520 a global symbol, we can not know the symbol index until we have
11521 finished examining all the local symbols to see which ones we are
11522 going to output. To deal with this, we keep the relocations in
11523 memory, and don't output them until the end of the link. This is
11524 an unfortunate waste of memory, but I don't see a good way around
11525 it. Fortunately, it only happens when performing a relocatable
11526 link, which is not the common case. FIXME: If keep_memory is set
11527 we could write the relocs out and then read them again; I don't
11528 know how bad the memory loss will be. */
11529
11530 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11531 sub->output_has_begun = FALSE;
11532 for (o = abfd->sections; o != NULL; o = o->next)
11533 {
11534 for (p = o->map_head.link_order; p != NULL; p = p->next)
11535 {
11536 if (p->type == bfd_indirect_link_order
11537 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11538 == bfd_target_elf_flavour)
11539 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11540 {
11541 if (! sub->output_has_begun)
11542 {
11543 if (! elf_link_input_bfd (&flinfo, sub))
11544 goto error_return;
11545 sub->output_has_begun = TRUE;
11546 }
11547 }
11548 else if (p->type == bfd_section_reloc_link_order
11549 || p->type == bfd_symbol_reloc_link_order)
11550 {
11551 if (! elf_reloc_link_order (abfd, info, o, p))
11552 goto error_return;
11553 }
11554 else
11555 {
11556 if (! _bfd_default_link_order (abfd, info, o, p))
11557 {
11558 if (p->type == bfd_indirect_link_order
11559 && (bfd_get_flavour (sub)
11560 == bfd_target_elf_flavour)
11561 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11562 != bed->s->elfclass))
11563 {
11564 const char *iclass, *oclass;
11565
11566 switch (bed->s->elfclass)
11567 {
11568 case ELFCLASS64: oclass = "ELFCLASS64"; break;
11569 case ELFCLASS32: oclass = "ELFCLASS32"; break;
11570 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
11571 default: abort ();
11572 }
11573
11574 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
11575 {
11576 case ELFCLASS64: iclass = "ELFCLASS64"; break;
11577 case ELFCLASS32: iclass = "ELFCLASS32"; break;
11578 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
11579 default: abort ();
11580 }
11581
11582 bfd_set_error (bfd_error_wrong_format);
11583 (*_bfd_error_handler)
11584 (_("%B: file class %s incompatible with %s"),
11585 sub, iclass, oclass);
11586 }
11587
11588 goto error_return;
11589 }
11590 }
11591 }
11592 }
11593
11594 /* Free symbol buffer if needed. */
11595 if (!info->reduce_memory_overheads)
11596 {
11597 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11598 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11599 && elf_tdata (sub)->symbuf)
11600 {
11601 free (elf_tdata (sub)->symbuf);
11602 elf_tdata (sub)->symbuf = NULL;
11603 }
11604 }
11605
11606 /* Output any global symbols that got converted to local in a
11607 version script or due to symbol visibility. We do this in a
11608 separate step since ELF requires all local symbols to appear
11609 prior to any global symbols. FIXME: We should only do this if
11610 some global symbols were, in fact, converted to become local.
11611 FIXME: Will this work correctly with the Irix 5 linker? */
11612 eoinfo.failed = FALSE;
11613 eoinfo.flinfo = &flinfo;
11614 eoinfo.localsyms = TRUE;
11615 eoinfo.file_sym_done = FALSE;
11616 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11617 if (eoinfo.failed)
11618 return FALSE;
11619
11620 /* If backend needs to output some local symbols not present in the hash
11621 table, do it now. */
11622 if (bed->elf_backend_output_arch_local_syms
11623 && (info->strip != strip_all || emit_relocs))
11624 {
11625 typedef int (*out_sym_func)
11626 (void *, const char *, Elf_Internal_Sym *, asection *,
11627 struct elf_link_hash_entry *);
11628
11629 if (! ((*bed->elf_backend_output_arch_local_syms)
11630 (abfd, info, &flinfo,
11631 (out_sym_func) elf_link_output_symstrtab)))
11632 return FALSE;
11633 }
11634
11635 /* That wrote out all the local symbols. Finish up the symbol table
11636 with the global symbols. Even if we want to strip everything we
11637 can, we still need to deal with those global symbols that got
11638 converted to local in a version script. */
11639
11640 /* The sh_info field records the index of the first non local symbol. */
11641 symtab_hdr->sh_info = bfd_get_symcount (abfd);
11642
11643 if (dynamic
11644 && elf_hash_table (info)->dynsym != NULL
11645 && (elf_hash_table (info)->dynsym->output_section
11646 != bfd_abs_section_ptr))
11647 {
11648 Elf_Internal_Sym sym;
11649 bfd_byte *dynsym = elf_hash_table (info)->dynsym->contents;
11650 long last_local = 0;
11651
11652 /* Write out the section symbols for the output sections. */
11653 if (bfd_link_pic (info)
11654 || elf_hash_table (info)->is_relocatable_executable)
11655 {
11656 asection *s;
11657
11658 sym.st_size = 0;
11659 sym.st_name = 0;
11660 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11661 sym.st_other = 0;
11662 sym.st_target_internal = 0;
11663
11664 for (s = abfd->sections; s != NULL; s = s->next)
11665 {
11666 int indx;
11667 bfd_byte *dest;
11668 long dynindx;
11669
11670 dynindx = elf_section_data (s)->dynindx;
11671 if (dynindx <= 0)
11672 continue;
11673 indx = elf_section_data (s)->this_idx;
11674 BFD_ASSERT (indx > 0);
11675 sym.st_shndx = indx;
11676 if (! check_dynsym (abfd, &sym))
11677 return FALSE;
11678 sym.st_value = s->vma;
11679 dest = dynsym + dynindx * bed->s->sizeof_sym;
11680 if (last_local < dynindx)
11681 last_local = dynindx;
11682 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11683 }
11684 }
11685
11686 /* Write out the local dynsyms. */
11687 if (elf_hash_table (info)->dynlocal)
11688 {
11689 struct elf_link_local_dynamic_entry *e;
11690 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
11691 {
11692 asection *s;
11693 bfd_byte *dest;
11694
11695 /* Copy the internal symbol and turn off visibility.
11696 Note that we saved a word of storage and overwrote
11697 the original st_name with the dynstr_index. */
11698 sym = e->isym;
11699 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
11700
11701 s = bfd_section_from_elf_index (e->input_bfd,
11702 e->isym.st_shndx);
11703 if (s != NULL)
11704 {
11705 sym.st_shndx =
11706 elf_section_data (s->output_section)->this_idx;
11707 if (! check_dynsym (abfd, &sym))
11708 return FALSE;
11709 sym.st_value = (s->output_section->vma
11710 + s->output_offset
11711 + e->isym.st_value);
11712 }
11713
11714 if (last_local < e->dynindx)
11715 last_local = e->dynindx;
11716
11717 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
11718 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11719 }
11720 }
11721
11722 elf_section_data (elf_hash_table (info)->dynsym->output_section)->this_hdr.sh_info =
11723 last_local + 1;
11724 }
11725
11726 /* We get the global symbols from the hash table. */
11727 eoinfo.failed = FALSE;
11728 eoinfo.localsyms = FALSE;
11729 eoinfo.flinfo = &flinfo;
11730 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11731 if (eoinfo.failed)
11732 return FALSE;
11733
11734 /* If backend needs to output some symbols not present in the hash
11735 table, do it now. */
11736 if (bed->elf_backend_output_arch_syms
11737 && (info->strip != strip_all || emit_relocs))
11738 {
11739 typedef int (*out_sym_func)
11740 (void *, const char *, Elf_Internal_Sym *, asection *,
11741 struct elf_link_hash_entry *);
11742
11743 if (! ((*bed->elf_backend_output_arch_syms)
11744 (abfd, info, &flinfo,
11745 (out_sym_func) elf_link_output_symstrtab)))
11746 return FALSE;
11747 }
11748
11749 /* Finalize the .strtab section. */
11750 _bfd_elf_strtab_finalize (flinfo.symstrtab);
11751
11752 /* Swap out the .strtab section. */
11753 if (!elf_link_swap_symbols_out (&flinfo))
11754 return FALSE;
11755
11756 /* Now we know the size of the symtab section. */
11757 if (bfd_get_symcount (abfd) > 0)
11758 {
11759 /* Finish up and write out the symbol string table (.strtab)
11760 section. */
11761 Elf_Internal_Shdr *symstrtab_hdr;
11762 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
11763
11764 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
11765 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
11766 {
11767 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
11768 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
11769 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
11770 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
11771 symtab_shndx_hdr->sh_size = amt;
11772
11773 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
11774 off, TRUE);
11775
11776 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
11777 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
11778 return FALSE;
11779 }
11780
11781 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
11782 /* sh_name was set in prep_headers. */
11783 symstrtab_hdr->sh_type = SHT_STRTAB;
11784 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
11785 symstrtab_hdr->sh_addr = 0;
11786 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
11787 symstrtab_hdr->sh_entsize = 0;
11788 symstrtab_hdr->sh_link = 0;
11789 symstrtab_hdr->sh_info = 0;
11790 /* sh_offset is set just below. */
11791 symstrtab_hdr->sh_addralign = 1;
11792
11793 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
11794 off, TRUE);
11795 elf_next_file_pos (abfd) = off;
11796
11797 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
11798 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
11799 return FALSE;
11800 }
11801
11802 /* Adjust the relocs to have the correct symbol indices. */
11803 for (o = abfd->sections; o != NULL; o = o->next)
11804 {
11805 struct bfd_elf_section_data *esdo = elf_section_data (o);
11806 bfd_boolean sort;
11807 if ((o->flags & SEC_RELOC) == 0)
11808 continue;
11809
11810 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
11811 if (esdo->rel.hdr != NULL
11812 && !elf_link_adjust_relocs (abfd, &esdo->rel, sort))
11813 return FALSE;
11814 if (esdo->rela.hdr != NULL
11815 && !elf_link_adjust_relocs (abfd, &esdo->rela, sort))
11816 return FALSE;
11817
11818 /* Set the reloc_count field to 0 to prevent write_relocs from
11819 trying to swap the relocs out itself. */
11820 o->reloc_count = 0;
11821 }
11822
11823 if (dynamic && info->combreloc && dynobj != NULL)
11824 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
11825
11826 /* If we are linking against a dynamic object, or generating a
11827 shared library, finish up the dynamic linking information. */
11828 if (dynamic)
11829 {
11830 bfd_byte *dyncon, *dynconend;
11831
11832 /* Fix up .dynamic entries. */
11833 o = bfd_get_linker_section (dynobj, ".dynamic");
11834 BFD_ASSERT (o != NULL);
11835
11836 dyncon = o->contents;
11837 dynconend = o->contents + o->size;
11838 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11839 {
11840 Elf_Internal_Dyn dyn;
11841 const char *name;
11842 unsigned int type;
11843
11844 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11845
11846 switch (dyn.d_tag)
11847 {
11848 default:
11849 continue;
11850 case DT_NULL:
11851 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
11852 {
11853 switch (elf_section_data (reldyn)->this_hdr.sh_type)
11854 {
11855 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
11856 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
11857 default: continue;
11858 }
11859 dyn.d_un.d_val = relativecount;
11860 relativecount = 0;
11861 break;
11862 }
11863 continue;
11864
11865 case DT_INIT:
11866 name = info->init_function;
11867 goto get_sym;
11868 case DT_FINI:
11869 name = info->fini_function;
11870 get_sym:
11871 {
11872 struct elf_link_hash_entry *h;
11873
11874 h = elf_link_hash_lookup (elf_hash_table (info), name,
11875 FALSE, FALSE, TRUE);
11876 if (h != NULL
11877 && (h->root.type == bfd_link_hash_defined
11878 || h->root.type == bfd_link_hash_defweak))
11879 {
11880 dyn.d_un.d_ptr = h->root.u.def.value;
11881 o = h->root.u.def.section;
11882 if (o->output_section != NULL)
11883 dyn.d_un.d_ptr += (o->output_section->vma
11884 + o->output_offset);
11885 else
11886 {
11887 /* The symbol is imported from another shared
11888 library and does not apply to this one. */
11889 dyn.d_un.d_ptr = 0;
11890 }
11891 break;
11892 }
11893 }
11894 continue;
11895
11896 case DT_PREINIT_ARRAYSZ:
11897 name = ".preinit_array";
11898 goto get_out_size;
11899 case DT_INIT_ARRAYSZ:
11900 name = ".init_array";
11901 goto get_out_size;
11902 case DT_FINI_ARRAYSZ:
11903 name = ".fini_array";
11904 get_out_size:
11905 o = bfd_get_section_by_name (abfd, name);
11906 if (o == NULL)
11907 {
11908 (*_bfd_error_handler)
11909 (_("could not find section %s"), name);
11910 goto error_return;
11911 }
11912 if (o->size == 0)
11913 (*_bfd_error_handler)
11914 (_("warning: %s section has zero size"), name);
11915 dyn.d_un.d_val = o->size;
11916 break;
11917
11918 case DT_PREINIT_ARRAY:
11919 name = ".preinit_array";
11920 goto get_out_vma;
11921 case DT_INIT_ARRAY:
11922 name = ".init_array";
11923 goto get_out_vma;
11924 case DT_FINI_ARRAY:
11925 name = ".fini_array";
11926 get_out_vma:
11927 o = bfd_get_section_by_name (abfd, name);
11928 goto do_vma;
11929
11930 case DT_HASH:
11931 name = ".hash";
11932 goto get_vma;
11933 case DT_GNU_HASH:
11934 name = ".gnu.hash";
11935 goto get_vma;
11936 case DT_STRTAB:
11937 name = ".dynstr";
11938 goto get_vma;
11939 case DT_SYMTAB:
11940 name = ".dynsym";
11941 goto get_vma;
11942 case DT_VERDEF:
11943 name = ".gnu.version_d";
11944 goto get_vma;
11945 case DT_VERNEED:
11946 name = ".gnu.version_r";
11947 goto get_vma;
11948 case DT_VERSYM:
11949 name = ".gnu.version";
11950 get_vma:
11951 o = bfd_get_linker_section (dynobj, name);
11952 do_vma:
11953 if (o == NULL)
11954 {
11955 (*_bfd_error_handler)
11956 (_("could not find section %s"), name);
11957 goto error_return;
11958 }
11959 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
11960 {
11961 (*_bfd_error_handler)
11962 (_("warning: section '%s' is being made into a note"), name);
11963 bfd_set_error (bfd_error_nonrepresentable_section);
11964 goto error_return;
11965 }
11966 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
11967 break;
11968
11969 case DT_REL:
11970 case DT_RELA:
11971 case DT_RELSZ:
11972 case DT_RELASZ:
11973 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
11974 type = SHT_REL;
11975 else
11976 type = SHT_RELA;
11977 dyn.d_un.d_val = 0;
11978 dyn.d_un.d_ptr = 0;
11979 for (i = 1; i < elf_numsections (abfd); i++)
11980 {
11981 Elf_Internal_Shdr *hdr;
11982
11983 hdr = elf_elfsections (abfd)[i];
11984 if (hdr->sh_type == type
11985 && (hdr->sh_flags & SHF_ALLOC) != 0)
11986 {
11987 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
11988 dyn.d_un.d_val += hdr->sh_size;
11989 else
11990 {
11991 if (dyn.d_un.d_ptr == 0
11992 || hdr->sh_addr < dyn.d_un.d_ptr)
11993 dyn.d_un.d_ptr = hdr->sh_addr;
11994 }
11995 }
11996 }
11997 break;
11998 }
11999 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12000 }
12001 }
12002
12003 /* If we have created any dynamic sections, then output them. */
12004 if (dynobj != NULL)
12005 {
12006 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12007 goto error_return;
12008
12009 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12010 if (((info->warn_shared_textrel && bfd_link_pic (info))
12011 || info->error_textrel)
12012 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
12013 {
12014 bfd_byte *dyncon, *dynconend;
12015
12016 dyncon = o->contents;
12017 dynconend = o->contents + o->size;
12018 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12019 {
12020 Elf_Internal_Dyn dyn;
12021
12022 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12023
12024 if (dyn.d_tag == DT_TEXTREL)
12025 {
12026 if (info->error_textrel)
12027 info->callbacks->einfo
12028 (_("%P%X: read-only segment has dynamic relocations.\n"));
12029 else
12030 info->callbacks->einfo
12031 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
12032 break;
12033 }
12034 }
12035 }
12036
12037 for (o = dynobj->sections; o != NULL; o = o->next)
12038 {
12039 if ((o->flags & SEC_HAS_CONTENTS) == 0
12040 || o->size == 0
12041 || o->output_section == bfd_abs_section_ptr)
12042 continue;
12043 if ((o->flags & SEC_LINKER_CREATED) == 0)
12044 {
12045 /* At this point, we are only interested in sections
12046 created by _bfd_elf_link_create_dynamic_sections. */
12047 continue;
12048 }
12049 if (elf_hash_table (info)->stab_info.stabstr == o)
12050 continue;
12051 if (elf_hash_table (info)->eh_info.hdr_sec == o)
12052 continue;
12053 if (strcmp (o->name, ".dynstr") != 0)
12054 {
12055 if (! bfd_set_section_contents (abfd, o->output_section,
12056 o->contents,
12057 (file_ptr) o->output_offset
12058 * bfd_octets_per_byte (abfd),
12059 o->size))
12060 goto error_return;
12061 }
12062 else
12063 {
12064 /* The contents of the .dynstr section are actually in a
12065 stringtab. */
12066 file_ptr off;
12067
12068 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12069 if (bfd_seek (abfd, off, SEEK_SET) != 0
12070 || ! _bfd_elf_strtab_emit (abfd,
12071 elf_hash_table (info)->dynstr))
12072 goto error_return;
12073 }
12074 }
12075 }
12076
12077 if (bfd_link_relocatable (info))
12078 {
12079 bfd_boolean failed = FALSE;
12080
12081 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12082 if (failed)
12083 goto error_return;
12084 }
12085
12086 /* If we have optimized stabs strings, output them. */
12087 if (elf_hash_table (info)->stab_info.stabstr != NULL)
12088 {
12089 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
12090 goto error_return;
12091 }
12092
12093 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12094 goto error_return;
12095
12096 elf_final_link_free (abfd, &flinfo);
12097
12098 elf_linker (abfd) = TRUE;
12099
12100 if (attr_section)
12101 {
12102 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
12103 if (contents == NULL)
12104 return FALSE; /* Bail out and fail. */
12105 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12106 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12107 free (contents);
12108 }
12109
12110 return TRUE;
12111
12112 error_return:
12113 elf_final_link_free (abfd, &flinfo);
12114 return FALSE;
12115 }
12116 \f
12117 /* Initialize COOKIE for input bfd ABFD. */
12118
12119 static bfd_boolean
12120 init_reloc_cookie (struct elf_reloc_cookie *cookie,
12121 struct bfd_link_info *info, bfd *abfd)
12122 {
12123 Elf_Internal_Shdr *symtab_hdr;
12124 const struct elf_backend_data *bed;
12125
12126 bed = get_elf_backend_data (abfd);
12127 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12128
12129 cookie->abfd = abfd;
12130 cookie->sym_hashes = elf_sym_hashes (abfd);
12131 cookie->bad_symtab = elf_bad_symtab (abfd);
12132 if (cookie->bad_symtab)
12133 {
12134 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12135 cookie->extsymoff = 0;
12136 }
12137 else
12138 {
12139 cookie->locsymcount = symtab_hdr->sh_info;
12140 cookie->extsymoff = symtab_hdr->sh_info;
12141 }
12142
12143 if (bed->s->arch_size == 32)
12144 cookie->r_sym_shift = 8;
12145 else
12146 cookie->r_sym_shift = 32;
12147
12148 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12149 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12150 {
12151 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12152 cookie->locsymcount, 0,
12153 NULL, NULL, NULL);
12154 if (cookie->locsyms == NULL)
12155 {
12156 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12157 return FALSE;
12158 }
12159 if (info->keep_memory)
12160 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12161 }
12162 return TRUE;
12163 }
12164
12165 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12166
12167 static void
12168 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12169 {
12170 Elf_Internal_Shdr *symtab_hdr;
12171
12172 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12173 if (cookie->locsyms != NULL
12174 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12175 free (cookie->locsyms);
12176 }
12177
12178 /* Initialize the relocation information in COOKIE for input section SEC
12179 of input bfd ABFD. */
12180
12181 static bfd_boolean
12182 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12183 struct bfd_link_info *info, bfd *abfd,
12184 asection *sec)
12185 {
12186 const struct elf_backend_data *bed;
12187
12188 if (sec->reloc_count == 0)
12189 {
12190 cookie->rels = NULL;
12191 cookie->relend = NULL;
12192 }
12193 else
12194 {
12195 bed = get_elf_backend_data (abfd);
12196
12197 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12198 info->keep_memory);
12199 if (cookie->rels == NULL)
12200 return FALSE;
12201 cookie->rel = cookie->rels;
12202 cookie->relend = (cookie->rels
12203 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
12204 }
12205 cookie->rel = cookie->rels;
12206 return TRUE;
12207 }
12208
12209 /* Free the memory allocated by init_reloc_cookie_rels,
12210 if appropriate. */
12211
12212 static void
12213 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12214 asection *sec)
12215 {
12216 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12217 free (cookie->rels);
12218 }
12219
12220 /* Initialize the whole of COOKIE for input section SEC. */
12221
12222 static bfd_boolean
12223 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12224 struct bfd_link_info *info,
12225 asection *sec)
12226 {
12227 if (!init_reloc_cookie (cookie, info, sec->owner))
12228 goto error1;
12229 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12230 goto error2;
12231 return TRUE;
12232
12233 error2:
12234 fini_reloc_cookie (cookie, sec->owner);
12235 error1:
12236 return FALSE;
12237 }
12238
12239 /* Free the memory allocated by init_reloc_cookie_for_section,
12240 if appropriate. */
12241
12242 static void
12243 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12244 asection *sec)
12245 {
12246 fini_reloc_cookie_rels (cookie, sec);
12247 fini_reloc_cookie (cookie, sec->owner);
12248 }
12249 \f
12250 /* Garbage collect unused sections. */
12251
12252 /* Default gc_mark_hook. */
12253
12254 asection *
12255 _bfd_elf_gc_mark_hook (asection *sec,
12256 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12257 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12258 struct elf_link_hash_entry *h,
12259 Elf_Internal_Sym *sym)
12260 {
12261 if (h != NULL)
12262 {
12263 switch (h->root.type)
12264 {
12265 case bfd_link_hash_defined:
12266 case bfd_link_hash_defweak:
12267 return h->root.u.def.section;
12268
12269 case bfd_link_hash_common:
12270 return h->root.u.c.p->section;
12271
12272 default:
12273 break;
12274 }
12275 }
12276 else
12277 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12278
12279 return NULL;
12280 }
12281
12282 /* For undefined __start_<name> and __stop_<name> symbols, return the
12283 first input section matching <name>. Return NULL otherwise. */
12284
12285 asection *
12286 _bfd_elf_is_start_stop (const struct bfd_link_info *info,
12287 struct elf_link_hash_entry *h)
12288 {
12289 asection *s;
12290 const char *sec_name;
12291
12292 if (h->root.type != bfd_link_hash_undefined
12293 && h->root.type != bfd_link_hash_undefweak)
12294 return NULL;
12295
12296 s = h->root.u.undef.section;
12297 if (s != NULL)
12298 {
12299 if (s == (asection *) 0 - 1)
12300 return NULL;
12301 return s;
12302 }
12303
12304 sec_name = NULL;
12305 if (strncmp (h->root.root.string, "__start_", 8) == 0)
12306 sec_name = h->root.root.string + 8;
12307 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
12308 sec_name = h->root.root.string + 7;
12309
12310 if (sec_name != NULL && *sec_name != '\0')
12311 {
12312 bfd *i;
12313
12314 for (i = info->input_bfds; i != NULL; i = i->link.next)
12315 {
12316 s = bfd_get_section_by_name (i, sec_name);
12317 if (s != NULL)
12318 {
12319 h->root.u.undef.section = s;
12320 break;
12321 }
12322 }
12323 }
12324
12325 if (s == NULL)
12326 h->root.u.undef.section = (asection *) 0 - 1;
12327
12328 return s;
12329 }
12330
12331 /* COOKIE->rel describes a relocation against section SEC, which is
12332 a section we've decided to keep. Return the section that contains
12333 the relocation symbol, or NULL if no section contains it. */
12334
12335 asection *
12336 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12337 elf_gc_mark_hook_fn gc_mark_hook,
12338 struct elf_reloc_cookie *cookie,
12339 bfd_boolean *start_stop)
12340 {
12341 unsigned long r_symndx;
12342 struct elf_link_hash_entry *h;
12343
12344 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
12345 if (r_symndx == STN_UNDEF)
12346 return NULL;
12347
12348 if (r_symndx >= cookie->locsymcount
12349 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12350 {
12351 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
12352 if (h == NULL)
12353 {
12354 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12355 sec->owner);
12356 return NULL;
12357 }
12358 while (h->root.type == bfd_link_hash_indirect
12359 || h->root.type == bfd_link_hash_warning)
12360 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12361 h->mark = 1;
12362 /* If this symbol is weak and there is a non-weak definition, we
12363 keep the non-weak definition because many backends put
12364 dynamic reloc info on the non-weak definition for code
12365 handling copy relocs. */
12366 if (h->u.weakdef != NULL)
12367 h->u.weakdef->mark = 1;
12368
12369 if (start_stop != NULL)
12370 {
12371 /* To work around a glibc bug, mark all XXX input sections
12372 when there is an as yet undefined reference to __start_XXX
12373 or __stop_XXX symbols. The linker will later define such
12374 symbols for orphan input sections that have a name
12375 representable as a C identifier. */
12376 asection *s = _bfd_elf_is_start_stop (info, h);
12377
12378 if (s != NULL)
12379 {
12380 *start_stop = !s->gc_mark;
12381 return s;
12382 }
12383 }
12384
12385 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12386 }
12387
12388 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12389 &cookie->locsyms[r_symndx]);
12390 }
12391
12392 /* COOKIE->rel describes a relocation against section SEC, which is
12393 a section we've decided to keep. Mark the section that contains
12394 the relocation symbol. */
12395
12396 bfd_boolean
12397 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12398 asection *sec,
12399 elf_gc_mark_hook_fn gc_mark_hook,
12400 struct elf_reloc_cookie *cookie)
12401 {
12402 asection *rsec;
12403 bfd_boolean start_stop = FALSE;
12404
12405 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12406 while (rsec != NULL)
12407 {
12408 if (!rsec->gc_mark)
12409 {
12410 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12411 || (rsec->owner->flags & DYNAMIC) != 0)
12412 rsec->gc_mark = 1;
12413 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12414 return FALSE;
12415 }
12416 if (!start_stop)
12417 break;
12418 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
12419 }
12420 return TRUE;
12421 }
12422
12423 /* The mark phase of garbage collection. For a given section, mark
12424 it and any sections in this section's group, and all the sections
12425 which define symbols to which it refers. */
12426
12427 bfd_boolean
12428 _bfd_elf_gc_mark (struct bfd_link_info *info,
12429 asection *sec,
12430 elf_gc_mark_hook_fn gc_mark_hook)
12431 {
12432 bfd_boolean ret;
12433 asection *group_sec, *eh_frame;
12434
12435 sec->gc_mark = 1;
12436
12437 /* Mark all the sections in the group. */
12438 group_sec = elf_section_data (sec)->next_in_group;
12439 if (group_sec && !group_sec->gc_mark)
12440 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
12441 return FALSE;
12442
12443 /* Look through the section relocs. */
12444 ret = TRUE;
12445 eh_frame = elf_eh_frame_section (sec->owner);
12446 if ((sec->flags & SEC_RELOC) != 0
12447 && sec->reloc_count > 0
12448 && sec != eh_frame)
12449 {
12450 struct elf_reloc_cookie cookie;
12451
12452 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12453 ret = FALSE;
12454 else
12455 {
12456 for (; cookie.rel < cookie.relend; cookie.rel++)
12457 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
12458 {
12459 ret = FALSE;
12460 break;
12461 }
12462 fini_reloc_cookie_for_section (&cookie, sec);
12463 }
12464 }
12465
12466 if (ret && eh_frame && elf_fde_list (sec))
12467 {
12468 struct elf_reloc_cookie cookie;
12469
12470 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12471 ret = FALSE;
12472 else
12473 {
12474 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12475 gc_mark_hook, &cookie))
12476 ret = FALSE;
12477 fini_reloc_cookie_for_section (&cookie, eh_frame);
12478 }
12479 }
12480
12481 eh_frame = elf_section_eh_frame_entry (sec);
12482 if (ret && eh_frame && !eh_frame->gc_mark)
12483 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12484 ret = FALSE;
12485
12486 return ret;
12487 }
12488
12489 /* Scan and mark sections in a special or debug section group. */
12490
12491 static void
12492 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12493 {
12494 /* Point to first section of section group. */
12495 asection *ssec;
12496 /* Used to iterate the section group. */
12497 asection *msec;
12498
12499 bfd_boolean is_special_grp = TRUE;
12500 bfd_boolean is_debug_grp = TRUE;
12501
12502 /* First scan to see if group contains any section other than debug
12503 and special section. */
12504 ssec = msec = elf_next_in_group (grp);
12505 do
12506 {
12507 if ((msec->flags & SEC_DEBUGGING) == 0)
12508 is_debug_grp = FALSE;
12509
12510 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12511 is_special_grp = FALSE;
12512
12513 msec = elf_next_in_group (msec);
12514 }
12515 while (msec != ssec);
12516
12517 /* If this is a pure debug section group or pure special section group,
12518 keep all sections in this group. */
12519 if (is_debug_grp || is_special_grp)
12520 {
12521 do
12522 {
12523 msec->gc_mark = 1;
12524 msec = elf_next_in_group (msec);
12525 }
12526 while (msec != ssec);
12527 }
12528 }
12529
12530 /* Keep debug and special sections. */
12531
12532 bfd_boolean
12533 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12534 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12535 {
12536 bfd *ibfd;
12537
12538 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12539 {
12540 asection *isec;
12541 bfd_boolean some_kept;
12542 bfd_boolean debug_frag_seen;
12543
12544 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12545 continue;
12546
12547 /* Ensure all linker created sections are kept,
12548 see if any other section is already marked,
12549 and note if we have any fragmented debug sections. */
12550 debug_frag_seen = some_kept = FALSE;
12551 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12552 {
12553 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12554 isec->gc_mark = 1;
12555 else if (isec->gc_mark)
12556 some_kept = TRUE;
12557
12558 if (debug_frag_seen == FALSE
12559 && (isec->flags & SEC_DEBUGGING)
12560 && CONST_STRNEQ (isec->name, ".debug_line."))
12561 debug_frag_seen = TRUE;
12562 }
12563
12564 /* If no section in this file will be kept, then we can
12565 toss out the debug and special sections. */
12566 if (!some_kept)
12567 continue;
12568
12569 /* Keep debug and special sections like .comment when they are
12570 not part of a group. Also keep section groups that contain
12571 just debug sections or special sections. */
12572 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12573 {
12574 if ((isec->flags & SEC_GROUP) != 0)
12575 _bfd_elf_gc_mark_debug_special_section_group (isec);
12576 else if (((isec->flags & SEC_DEBUGGING) != 0
12577 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12578 && elf_next_in_group (isec) == NULL)
12579 isec->gc_mark = 1;
12580 }
12581
12582 if (! debug_frag_seen)
12583 continue;
12584
12585 /* Look for CODE sections which are going to be discarded,
12586 and find and discard any fragmented debug sections which
12587 are associated with that code section. */
12588 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12589 if ((isec->flags & SEC_CODE) != 0
12590 && isec->gc_mark == 0)
12591 {
12592 unsigned int ilen;
12593 asection *dsec;
12594
12595 ilen = strlen (isec->name);
12596
12597 /* Association is determined by the name of the debug section
12598 containing the name of the code section as a suffix. For
12599 example .debug_line.text.foo is a debug section associated
12600 with .text.foo. */
12601 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12602 {
12603 unsigned int dlen;
12604
12605 if (dsec->gc_mark == 0
12606 || (dsec->flags & SEC_DEBUGGING) == 0)
12607 continue;
12608
12609 dlen = strlen (dsec->name);
12610
12611 if (dlen > ilen
12612 && strncmp (dsec->name + (dlen - ilen),
12613 isec->name, ilen) == 0)
12614 {
12615 dsec->gc_mark = 0;
12616 }
12617 }
12618 }
12619 }
12620 return TRUE;
12621 }
12622
12623 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
12624
12625 struct elf_gc_sweep_symbol_info
12626 {
12627 struct bfd_link_info *info;
12628 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
12629 bfd_boolean);
12630 };
12631
12632 static bfd_boolean
12633 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
12634 {
12635 if (!h->mark
12636 && (((h->root.type == bfd_link_hash_defined
12637 || h->root.type == bfd_link_hash_defweak)
12638 && !((h->def_regular || ELF_COMMON_DEF_P (h))
12639 && h->root.u.def.section->gc_mark))
12640 || h->root.type == bfd_link_hash_undefined
12641 || h->root.type == bfd_link_hash_undefweak))
12642 {
12643 struct elf_gc_sweep_symbol_info *inf;
12644
12645 inf = (struct elf_gc_sweep_symbol_info *) data;
12646 (*inf->hide_symbol) (inf->info, h, TRUE);
12647 h->def_regular = 0;
12648 h->ref_regular = 0;
12649 h->ref_regular_nonweak = 0;
12650 }
12651
12652 return TRUE;
12653 }
12654
12655 /* The sweep phase of garbage collection. Remove all garbage sections. */
12656
12657 typedef bfd_boolean (*gc_sweep_hook_fn)
12658 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
12659
12660 static bfd_boolean
12661 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
12662 {
12663 bfd *sub;
12664 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12665 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
12666 unsigned long section_sym_count;
12667 struct elf_gc_sweep_symbol_info sweep_info;
12668
12669 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12670 {
12671 asection *o;
12672
12673 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12674 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
12675 continue;
12676
12677 for (o = sub->sections; o != NULL; o = o->next)
12678 {
12679 /* When any section in a section group is kept, we keep all
12680 sections in the section group. If the first member of
12681 the section group is excluded, we will also exclude the
12682 group section. */
12683 if (o->flags & SEC_GROUP)
12684 {
12685 asection *first = elf_next_in_group (o);
12686 o->gc_mark = first->gc_mark;
12687 }
12688
12689 if (o->gc_mark)
12690 continue;
12691
12692 /* Skip sweeping sections already excluded. */
12693 if (o->flags & SEC_EXCLUDE)
12694 continue;
12695
12696 /* Since this is early in the link process, it is simple
12697 to remove a section from the output. */
12698 o->flags |= SEC_EXCLUDE;
12699
12700 if (info->print_gc_sections && o->size != 0)
12701 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
12702
12703 /* But we also have to update some of the relocation
12704 info we collected before. */
12705 if (gc_sweep_hook
12706 && (o->flags & SEC_RELOC) != 0
12707 && o->reloc_count != 0
12708 && !((info->strip == strip_all || info->strip == strip_debugger)
12709 && (o->flags & SEC_DEBUGGING) != 0)
12710 && !bfd_is_abs_section (o->output_section))
12711 {
12712 Elf_Internal_Rela *internal_relocs;
12713 bfd_boolean r;
12714
12715 internal_relocs
12716 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
12717 info->keep_memory);
12718 if (internal_relocs == NULL)
12719 return FALSE;
12720
12721 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
12722
12723 if (elf_section_data (o)->relocs != internal_relocs)
12724 free (internal_relocs);
12725
12726 if (!r)
12727 return FALSE;
12728 }
12729 }
12730 }
12731
12732 /* Remove the symbols that were in the swept sections from the dynamic
12733 symbol table. GCFIXME: Anyone know how to get them out of the
12734 static symbol table as well? */
12735 sweep_info.info = info;
12736 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
12737 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
12738 &sweep_info);
12739
12740 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
12741 return TRUE;
12742 }
12743
12744 /* Propagate collected vtable information. This is called through
12745 elf_link_hash_traverse. */
12746
12747 static bfd_boolean
12748 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
12749 {
12750 /* Those that are not vtables. */
12751 if (h->vtable == NULL || h->vtable->parent == NULL)
12752 return TRUE;
12753
12754 /* Those vtables that do not have parents, we cannot merge. */
12755 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
12756 return TRUE;
12757
12758 /* If we've already been done, exit. */
12759 if (h->vtable->used && h->vtable->used[-1])
12760 return TRUE;
12761
12762 /* Make sure the parent's table is up to date. */
12763 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
12764
12765 if (h->vtable->used == NULL)
12766 {
12767 /* None of this table's entries were referenced. Re-use the
12768 parent's table. */
12769 h->vtable->used = h->vtable->parent->vtable->used;
12770 h->vtable->size = h->vtable->parent->vtable->size;
12771 }
12772 else
12773 {
12774 size_t n;
12775 bfd_boolean *cu, *pu;
12776
12777 /* Or the parent's entries into ours. */
12778 cu = h->vtable->used;
12779 cu[-1] = TRUE;
12780 pu = h->vtable->parent->vtable->used;
12781 if (pu != NULL)
12782 {
12783 const struct elf_backend_data *bed;
12784 unsigned int log_file_align;
12785
12786 bed = get_elf_backend_data (h->root.u.def.section->owner);
12787 log_file_align = bed->s->log_file_align;
12788 n = h->vtable->parent->vtable->size >> log_file_align;
12789 while (n--)
12790 {
12791 if (*pu)
12792 *cu = TRUE;
12793 pu++;
12794 cu++;
12795 }
12796 }
12797 }
12798
12799 return TRUE;
12800 }
12801
12802 static bfd_boolean
12803 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
12804 {
12805 asection *sec;
12806 bfd_vma hstart, hend;
12807 Elf_Internal_Rela *relstart, *relend, *rel;
12808 const struct elf_backend_data *bed;
12809 unsigned int log_file_align;
12810
12811 /* Take care of both those symbols that do not describe vtables as
12812 well as those that are not loaded. */
12813 if (h->vtable == NULL || h->vtable->parent == NULL)
12814 return TRUE;
12815
12816 BFD_ASSERT (h->root.type == bfd_link_hash_defined
12817 || h->root.type == bfd_link_hash_defweak);
12818
12819 sec = h->root.u.def.section;
12820 hstart = h->root.u.def.value;
12821 hend = hstart + h->size;
12822
12823 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
12824 if (!relstart)
12825 return *(bfd_boolean *) okp = FALSE;
12826 bed = get_elf_backend_data (sec->owner);
12827 log_file_align = bed->s->log_file_align;
12828
12829 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
12830
12831 for (rel = relstart; rel < relend; ++rel)
12832 if (rel->r_offset >= hstart && rel->r_offset < hend)
12833 {
12834 /* If the entry is in use, do nothing. */
12835 if (h->vtable->used
12836 && (rel->r_offset - hstart) < h->vtable->size)
12837 {
12838 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
12839 if (h->vtable->used[entry])
12840 continue;
12841 }
12842 /* Otherwise, kill it. */
12843 rel->r_offset = rel->r_info = rel->r_addend = 0;
12844 }
12845
12846 return TRUE;
12847 }
12848
12849 /* Mark sections containing dynamically referenced symbols. When
12850 building shared libraries, we must assume that any visible symbol is
12851 referenced. */
12852
12853 bfd_boolean
12854 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
12855 {
12856 struct bfd_link_info *info = (struct bfd_link_info *) inf;
12857 struct bfd_elf_dynamic_list *d = info->dynamic_list;
12858
12859 if ((h->root.type == bfd_link_hash_defined
12860 || h->root.type == bfd_link_hash_defweak)
12861 && (h->ref_dynamic
12862 || ((h->def_regular || ELF_COMMON_DEF_P (h))
12863 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
12864 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
12865 && (!bfd_link_executable (info)
12866 || info->export_dynamic
12867 || (h->dynamic
12868 && d != NULL
12869 && (*d->match) (&d->head, NULL, h->root.root.string)))
12870 && (h->versioned >= versioned
12871 || !bfd_hide_sym_by_version (info->version_info,
12872 h->root.root.string)))))
12873 h->root.u.def.section->flags |= SEC_KEEP;
12874
12875 return TRUE;
12876 }
12877
12878 /* Keep all sections containing symbols undefined on the command-line,
12879 and the section containing the entry symbol. */
12880
12881 void
12882 _bfd_elf_gc_keep (struct bfd_link_info *info)
12883 {
12884 struct bfd_sym_chain *sym;
12885
12886 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
12887 {
12888 struct elf_link_hash_entry *h;
12889
12890 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
12891 FALSE, FALSE, FALSE);
12892
12893 if (h != NULL
12894 && (h->root.type == bfd_link_hash_defined
12895 || h->root.type == bfd_link_hash_defweak)
12896 && !bfd_is_abs_section (h->root.u.def.section))
12897 h->root.u.def.section->flags |= SEC_KEEP;
12898 }
12899 }
12900
12901 bfd_boolean
12902 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
12903 struct bfd_link_info *info)
12904 {
12905 bfd *ibfd = info->input_bfds;
12906
12907 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12908 {
12909 asection *sec;
12910 struct elf_reloc_cookie cookie;
12911
12912 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12913 continue;
12914
12915 if (!init_reloc_cookie (&cookie, info, ibfd))
12916 return FALSE;
12917
12918 for (sec = ibfd->sections; sec; sec = sec->next)
12919 {
12920 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
12921 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
12922 {
12923 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
12924 fini_reloc_cookie_rels (&cookie, sec);
12925 }
12926 }
12927 }
12928 return TRUE;
12929 }
12930
12931 /* Do mark and sweep of unused sections. */
12932
12933 bfd_boolean
12934 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
12935 {
12936 bfd_boolean ok = TRUE;
12937 bfd *sub;
12938 elf_gc_mark_hook_fn gc_mark_hook;
12939 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12940 struct elf_link_hash_table *htab;
12941
12942 if (!bed->can_gc_sections
12943 || !is_elf_hash_table (info->hash))
12944 {
12945 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
12946 return TRUE;
12947 }
12948
12949 bed->gc_keep (info);
12950 htab = elf_hash_table (info);
12951
12952 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
12953 at the .eh_frame section if we can mark the FDEs individually. */
12954 for (sub = info->input_bfds;
12955 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
12956 sub = sub->link.next)
12957 {
12958 asection *sec;
12959 struct elf_reloc_cookie cookie;
12960
12961 sec = bfd_get_section_by_name (sub, ".eh_frame");
12962 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
12963 {
12964 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
12965 if (elf_section_data (sec)->sec_info
12966 && (sec->flags & SEC_LINKER_CREATED) == 0)
12967 elf_eh_frame_section (sub) = sec;
12968 fini_reloc_cookie_for_section (&cookie, sec);
12969 sec = bfd_get_next_section_by_name (NULL, sec);
12970 }
12971 }
12972
12973 /* Apply transitive closure to the vtable entry usage info. */
12974 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
12975 if (!ok)
12976 return FALSE;
12977
12978 /* Kill the vtable relocations that were not used. */
12979 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
12980 if (!ok)
12981 return FALSE;
12982
12983 /* Mark dynamically referenced symbols. */
12984 if (htab->dynamic_sections_created)
12985 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
12986
12987 /* Grovel through relocs to find out who stays ... */
12988 gc_mark_hook = bed->gc_mark_hook;
12989 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12990 {
12991 asection *o;
12992
12993 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12994 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
12995 continue;
12996
12997 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
12998 Also treat note sections as a root, if the section is not part
12999 of a group. */
13000 for (o = sub->sections; o != NULL; o = o->next)
13001 if (!o->gc_mark
13002 && (o->flags & SEC_EXCLUDE) == 0
13003 && ((o->flags & SEC_KEEP) != 0
13004 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13005 && elf_next_in_group (o) == NULL )))
13006 {
13007 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13008 return FALSE;
13009 }
13010 }
13011
13012 /* Allow the backend to mark additional target specific sections. */
13013 bed->gc_mark_extra_sections (info, gc_mark_hook);
13014
13015 /* ... and mark SEC_EXCLUDE for those that go. */
13016 return elf_gc_sweep (abfd, info);
13017 }
13018 \f
13019 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13020
13021 bfd_boolean
13022 bfd_elf_gc_record_vtinherit (bfd *abfd,
13023 asection *sec,
13024 struct elf_link_hash_entry *h,
13025 bfd_vma offset)
13026 {
13027 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13028 struct elf_link_hash_entry **search, *child;
13029 bfd_size_type extsymcount;
13030 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13031
13032 /* The sh_info field of the symtab header tells us where the
13033 external symbols start. We don't care about the local symbols at
13034 this point. */
13035 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13036 if (!elf_bad_symtab (abfd))
13037 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13038
13039 sym_hashes = elf_sym_hashes (abfd);
13040 sym_hashes_end = sym_hashes + extsymcount;
13041
13042 /* Hunt down the child symbol, which is in this section at the same
13043 offset as the relocation. */
13044 for (search = sym_hashes; search != sym_hashes_end; ++search)
13045 {
13046 if ((child = *search) != NULL
13047 && (child->root.type == bfd_link_hash_defined
13048 || child->root.type == bfd_link_hash_defweak)
13049 && child->root.u.def.section == sec
13050 && child->root.u.def.value == offset)
13051 goto win;
13052 }
13053
13054 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
13055 abfd, sec, (unsigned long) offset);
13056 bfd_set_error (bfd_error_invalid_operation);
13057 return FALSE;
13058
13059 win:
13060 if (!child->vtable)
13061 {
13062 child->vtable = ((struct elf_link_virtual_table_entry *)
13063 bfd_zalloc (abfd, sizeof (*child->vtable)));
13064 if (!child->vtable)
13065 return FALSE;
13066 }
13067 if (!h)
13068 {
13069 /* This *should* only be the absolute section. It could potentially
13070 be that someone has defined a non-global vtable though, which
13071 would be bad. It isn't worth paging in the local symbols to be
13072 sure though; that case should simply be handled by the assembler. */
13073
13074 child->vtable->parent = (struct elf_link_hash_entry *) -1;
13075 }
13076 else
13077 child->vtable->parent = h;
13078
13079 return TRUE;
13080 }
13081
13082 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13083
13084 bfd_boolean
13085 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13086 asection *sec ATTRIBUTE_UNUSED,
13087 struct elf_link_hash_entry *h,
13088 bfd_vma addend)
13089 {
13090 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13091 unsigned int log_file_align = bed->s->log_file_align;
13092
13093 if (!h->vtable)
13094 {
13095 h->vtable = ((struct elf_link_virtual_table_entry *)
13096 bfd_zalloc (abfd, sizeof (*h->vtable)));
13097 if (!h->vtable)
13098 return FALSE;
13099 }
13100
13101 if (addend >= h->vtable->size)
13102 {
13103 size_t size, bytes, file_align;
13104 bfd_boolean *ptr = h->vtable->used;
13105
13106 /* While the symbol is undefined, we have to be prepared to handle
13107 a zero size. */
13108 file_align = 1 << log_file_align;
13109 if (h->root.type == bfd_link_hash_undefined)
13110 size = addend + file_align;
13111 else
13112 {
13113 size = h->size;
13114 if (addend >= size)
13115 {
13116 /* Oops! We've got a reference past the defined end of
13117 the table. This is probably a bug -- shall we warn? */
13118 size = addend + file_align;
13119 }
13120 }
13121 size = (size + file_align - 1) & -file_align;
13122
13123 /* Allocate one extra entry for use as a "done" flag for the
13124 consolidation pass. */
13125 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13126
13127 if (ptr)
13128 {
13129 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
13130
13131 if (ptr != NULL)
13132 {
13133 size_t oldbytes;
13134
13135 oldbytes = (((h->vtable->size >> log_file_align) + 1)
13136 * sizeof (bfd_boolean));
13137 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13138 }
13139 }
13140 else
13141 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
13142
13143 if (ptr == NULL)
13144 return FALSE;
13145
13146 /* And arrange for that done flag to be at index -1. */
13147 h->vtable->used = ptr + 1;
13148 h->vtable->size = size;
13149 }
13150
13151 h->vtable->used[addend >> log_file_align] = TRUE;
13152
13153 return TRUE;
13154 }
13155
13156 /* Map an ELF section header flag to its corresponding string. */
13157 typedef struct
13158 {
13159 char *flag_name;
13160 flagword flag_value;
13161 } elf_flags_to_name_table;
13162
13163 static elf_flags_to_name_table elf_flags_to_names [] =
13164 {
13165 { "SHF_WRITE", SHF_WRITE },
13166 { "SHF_ALLOC", SHF_ALLOC },
13167 { "SHF_EXECINSTR", SHF_EXECINSTR },
13168 { "SHF_MERGE", SHF_MERGE },
13169 { "SHF_STRINGS", SHF_STRINGS },
13170 { "SHF_INFO_LINK", SHF_INFO_LINK},
13171 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13172 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13173 { "SHF_GROUP", SHF_GROUP },
13174 { "SHF_TLS", SHF_TLS },
13175 { "SHF_MASKOS", SHF_MASKOS },
13176 { "SHF_EXCLUDE", SHF_EXCLUDE },
13177 };
13178
13179 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13180 bfd_boolean
13181 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
13182 struct flag_info *flaginfo,
13183 asection *section)
13184 {
13185 const bfd_vma sh_flags = elf_section_flags (section);
13186
13187 if (!flaginfo->flags_initialized)
13188 {
13189 bfd *obfd = info->output_bfd;
13190 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13191 struct flag_info_list *tf = flaginfo->flag_list;
13192 int with_hex = 0;
13193 int without_hex = 0;
13194
13195 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13196 {
13197 unsigned i;
13198 flagword (*lookup) (char *);
13199
13200 lookup = bed->elf_backend_lookup_section_flags_hook;
13201 if (lookup != NULL)
13202 {
13203 flagword hexval = (*lookup) ((char *) tf->name);
13204
13205 if (hexval != 0)
13206 {
13207 if (tf->with == with_flags)
13208 with_hex |= hexval;
13209 else if (tf->with == without_flags)
13210 without_hex |= hexval;
13211 tf->valid = TRUE;
13212 continue;
13213 }
13214 }
13215 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13216 {
13217 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
13218 {
13219 if (tf->with == with_flags)
13220 with_hex |= elf_flags_to_names[i].flag_value;
13221 else if (tf->with == without_flags)
13222 without_hex |= elf_flags_to_names[i].flag_value;
13223 tf->valid = TRUE;
13224 break;
13225 }
13226 }
13227 if (!tf->valid)
13228 {
13229 info->callbacks->einfo
13230 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
13231 return FALSE;
13232 }
13233 }
13234 flaginfo->flags_initialized = TRUE;
13235 flaginfo->only_with_flags |= with_hex;
13236 flaginfo->not_with_flags |= without_hex;
13237 }
13238
13239 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13240 return FALSE;
13241
13242 if ((flaginfo->not_with_flags & sh_flags) != 0)
13243 return FALSE;
13244
13245 return TRUE;
13246 }
13247
13248 struct alloc_got_off_arg {
13249 bfd_vma gotoff;
13250 struct bfd_link_info *info;
13251 };
13252
13253 /* We need a special top-level link routine to convert got reference counts
13254 to real got offsets. */
13255
13256 static bfd_boolean
13257 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13258 {
13259 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13260 bfd *obfd = gofarg->info->output_bfd;
13261 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13262
13263 if (h->got.refcount > 0)
13264 {
13265 h->got.offset = gofarg->gotoff;
13266 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13267 }
13268 else
13269 h->got.offset = (bfd_vma) -1;
13270
13271 return TRUE;
13272 }
13273
13274 /* And an accompanying bit to work out final got entry offsets once
13275 we're done. Should be called from final_link. */
13276
13277 bfd_boolean
13278 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13279 struct bfd_link_info *info)
13280 {
13281 bfd *i;
13282 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13283 bfd_vma gotoff;
13284 struct alloc_got_off_arg gofarg;
13285
13286 BFD_ASSERT (abfd == info->output_bfd);
13287
13288 if (! is_elf_hash_table (info->hash))
13289 return FALSE;
13290
13291 /* The GOT offset is relative to the .got section, but the GOT header is
13292 put into the .got.plt section, if the backend uses it. */
13293 if (bed->want_got_plt)
13294 gotoff = 0;
13295 else
13296 gotoff = bed->got_header_size;
13297
13298 /* Do the local .got entries first. */
13299 for (i = info->input_bfds; i; i = i->link.next)
13300 {
13301 bfd_signed_vma *local_got;
13302 bfd_size_type j, locsymcount;
13303 Elf_Internal_Shdr *symtab_hdr;
13304
13305 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13306 continue;
13307
13308 local_got = elf_local_got_refcounts (i);
13309 if (!local_got)
13310 continue;
13311
13312 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13313 if (elf_bad_symtab (i))
13314 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13315 else
13316 locsymcount = symtab_hdr->sh_info;
13317
13318 for (j = 0; j < locsymcount; ++j)
13319 {
13320 if (local_got[j] > 0)
13321 {
13322 local_got[j] = gotoff;
13323 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
13324 }
13325 else
13326 local_got[j] = (bfd_vma) -1;
13327 }
13328 }
13329
13330 /* Then the global .got entries. .plt refcounts are handled by
13331 adjust_dynamic_symbol */
13332 gofarg.gotoff = gotoff;
13333 gofarg.info = info;
13334 elf_link_hash_traverse (elf_hash_table (info),
13335 elf_gc_allocate_got_offsets,
13336 &gofarg);
13337 return TRUE;
13338 }
13339
13340 /* Many folk need no more in the way of final link than this, once
13341 got entry reference counting is enabled. */
13342
13343 bfd_boolean
13344 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13345 {
13346 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13347 return FALSE;
13348
13349 /* Invoke the regular ELF backend linker to do all the work. */
13350 return bfd_elf_final_link (abfd, info);
13351 }
13352
13353 bfd_boolean
13354 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13355 {
13356 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
13357
13358 if (rcookie->bad_symtab)
13359 rcookie->rel = rcookie->rels;
13360
13361 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13362 {
13363 unsigned long r_symndx;
13364
13365 if (! rcookie->bad_symtab)
13366 if (rcookie->rel->r_offset > offset)
13367 return FALSE;
13368 if (rcookie->rel->r_offset != offset)
13369 continue;
13370
13371 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
13372 if (r_symndx == STN_UNDEF)
13373 return TRUE;
13374
13375 if (r_symndx >= rcookie->locsymcount
13376 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13377 {
13378 struct elf_link_hash_entry *h;
13379
13380 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13381
13382 while (h->root.type == bfd_link_hash_indirect
13383 || h->root.type == bfd_link_hash_warning)
13384 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13385
13386 if ((h->root.type == bfd_link_hash_defined
13387 || h->root.type == bfd_link_hash_defweak)
13388 && (h->root.u.def.section->owner != rcookie->abfd
13389 || h->root.u.def.section->kept_section != NULL
13390 || discarded_section (h->root.u.def.section)))
13391 return TRUE;
13392 }
13393 else
13394 {
13395 /* It's not a relocation against a global symbol,
13396 but it could be a relocation against a local
13397 symbol for a discarded section. */
13398 asection *isec;
13399 Elf_Internal_Sym *isym;
13400
13401 /* Need to: get the symbol; get the section. */
13402 isym = &rcookie->locsyms[r_symndx];
13403 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
13404 if (isec != NULL
13405 && (isec->kept_section != NULL
13406 || discarded_section (isec)))
13407 return TRUE;
13408 }
13409 return FALSE;
13410 }
13411 return FALSE;
13412 }
13413
13414 /* Discard unneeded references to discarded sections.
13415 Returns -1 on error, 1 if any section's size was changed, 0 if
13416 nothing changed. This function assumes that the relocations are in
13417 sorted order, which is true for all known assemblers. */
13418
13419 int
13420 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13421 {
13422 struct elf_reloc_cookie cookie;
13423 asection *o;
13424 bfd *abfd;
13425 int changed = 0;
13426
13427 if (info->traditional_format
13428 || !is_elf_hash_table (info->hash))
13429 return 0;
13430
13431 o = bfd_get_section_by_name (output_bfd, ".stab");
13432 if (o != NULL)
13433 {
13434 asection *i;
13435
13436 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13437 {
13438 if (i->size == 0
13439 || i->reloc_count == 0
13440 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13441 continue;
13442
13443 abfd = i->owner;
13444 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13445 continue;
13446
13447 if (!init_reloc_cookie_for_section (&cookie, info, i))
13448 return -1;
13449
13450 if (_bfd_discard_section_stabs (abfd, i,
13451 elf_section_data (i)->sec_info,
13452 bfd_elf_reloc_symbol_deleted_p,
13453 &cookie))
13454 changed = 1;
13455
13456 fini_reloc_cookie_for_section (&cookie, i);
13457 }
13458 }
13459
13460 o = NULL;
13461 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13462 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
13463 if (o != NULL)
13464 {
13465 asection *i;
13466
13467 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13468 {
13469 if (i->size == 0)
13470 continue;
13471
13472 abfd = i->owner;
13473 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13474 continue;
13475
13476 if (!init_reloc_cookie_for_section (&cookie, info, i))
13477 return -1;
13478
13479 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13480 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
13481 bfd_elf_reloc_symbol_deleted_p,
13482 &cookie))
13483 changed = 1;
13484
13485 fini_reloc_cookie_for_section (&cookie, i);
13486 }
13487 }
13488
13489 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13490 {
13491 const struct elf_backend_data *bed;
13492
13493 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13494 continue;
13495
13496 bed = get_elf_backend_data (abfd);
13497
13498 if (bed->elf_backend_discard_info != NULL)
13499 {
13500 if (!init_reloc_cookie (&cookie, info, abfd))
13501 return -1;
13502
13503 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
13504 changed = 1;
13505
13506 fini_reloc_cookie (&cookie, abfd);
13507 }
13508 }
13509
13510 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13511 _bfd_elf_end_eh_frame_parsing (info);
13512
13513 if (info->eh_frame_hdr_type
13514 && !bfd_link_relocatable (info)
13515 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
13516 changed = 1;
13517
13518 return changed;
13519 }
13520
13521 bfd_boolean
13522 _bfd_elf_section_already_linked (bfd *abfd,
13523 asection *sec,
13524 struct bfd_link_info *info)
13525 {
13526 flagword flags;
13527 const char *name, *key;
13528 struct bfd_section_already_linked *l;
13529 struct bfd_section_already_linked_hash_entry *already_linked_list;
13530
13531 if (sec->output_section == bfd_abs_section_ptr)
13532 return FALSE;
13533
13534 flags = sec->flags;
13535
13536 /* Return if it isn't a linkonce section. A comdat group section
13537 also has SEC_LINK_ONCE set. */
13538 if ((flags & SEC_LINK_ONCE) == 0)
13539 return FALSE;
13540
13541 /* Don't put group member sections on our list of already linked
13542 sections. They are handled as a group via their group section. */
13543 if (elf_sec_group (sec) != NULL)
13544 return FALSE;
13545
13546 /* For a SHT_GROUP section, use the group signature as the key. */
13547 name = sec->name;
13548 if ((flags & SEC_GROUP) != 0
13549 && elf_next_in_group (sec) != NULL
13550 && elf_group_name (elf_next_in_group (sec)) != NULL)
13551 key = elf_group_name (elf_next_in_group (sec));
13552 else
13553 {
13554 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
13555 if (CONST_STRNEQ (name, ".gnu.linkonce.")
13556 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13557 key++;
13558 else
13559 /* Must be a user linkonce section that doesn't follow gcc's
13560 naming convention. In this case we won't be matching
13561 single member groups. */
13562 key = name;
13563 }
13564
13565 already_linked_list = bfd_section_already_linked_table_lookup (key);
13566
13567 for (l = already_linked_list->entry; l != NULL; l = l->next)
13568 {
13569 /* We may have 2 different types of sections on the list: group
13570 sections with a signature of <key> (<key> is some string),
13571 and linkonce sections named .gnu.linkonce.<type>.<key>.
13572 Match like sections. LTO plugin sections are an exception.
13573 They are always named .gnu.linkonce.t.<key> and match either
13574 type of section. */
13575 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13576 && ((flags & SEC_GROUP) != 0
13577 || strcmp (name, l->sec->name) == 0))
13578 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
13579 {
13580 /* The section has already been linked. See if we should
13581 issue a warning. */
13582 if (!_bfd_handle_already_linked (sec, l, info))
13583 return FALSE;
13584
13585 if (flags & SEC_GROUP)
13586 {
13587 asection *first = elf_next_in_group (sec);
13588 asection *s = first;
13589
13590 while (s != NULL)
13591 {
13592 s->output_section = bfd_abs_section_ptr;
13593 /* Record which group discards it. */
13594 s->kept_section = l->sec;
13595 s = elf_next_in_group (s);
13596 /* These lists are circular. */
13597 if (s == first)
13598 break;
13599 }
13600 }
13601
13602 return TRUE;
13603 }
13604 }
13605
13606 /* A single member comdat group section may be discarded by a
13607 linkonce section and vice versa. */
13608 if ((flags & SEC_GROUP) != 0)
13609 {
13610 asection *first = elf_next_in_group (sec);
13611
13612 if (first != NULL && elf_next_in_group (first) == first)
13613 /* Check this single member group against linkonce sections. */
13614 for (l = already_linked_list->entry; l != NULL; l = l->next)
13615 if ((l->sec->flags & SEC_GROUP) == 0
13616 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13617 {
13618 first->output_section = bfd_abs_section_ptr;
13619 first->kept_section = l->sec;
13620 sec->output_section = bfd_abs_section_ptr;
13621 break;
13622 }
13623 }
13624 else
13625 /* Check this linkonce section against single member groups. */
13626 for (l = already_linked_list->entry; l != NULL; l = l->next)
13627 if (l->sec->flags & SEC_GROUP)
13628 {
13629 asection *first = elf_next_in_group (l->sec);
13630
13631 if (first != NULL
13632 && elf_next_in_group (first) == first
13633 && bfd_elf_match_symbols_in_sections (first, sec, info))
13634 {
13635 sec->output_section = bfd_abs_section_ptr;
13636 sec->kept_section = first;
13637 break;
13638 }
13639 }
13640
13641 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13642 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13643 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13644 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13645 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13646 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13647 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13648 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13649 The reverse order cannot happen as there is never a bfd with only the
13650 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13651 matter as here were are looking only for cross-bfd sections. */
13652
13653 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
13654 for (l = already_linked_list->entry; l != NULL; l = l->next)
13655 if ((l->sec->flags & SEC_GROUP) == 0
13656 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
13657 {
13658 if (abfd != l->sec->owner)
13659 sec->output_section = bfd_abs_section_ptr;
13660 break;
13661 }
13662
13663 /* This is the first section with this name. Record it. */
13664 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
13665 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
13666 return sec->output_section == bfd_abs_section_ptr;
13667 }
13668
13669 bfd_boolean
13670 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
13671 {
13672 return sym->st_shndx == SHN_COMMON;
13673 }
13674
13675 unsigned int
13676 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
13677 {
13678 return SHN_COMMON;
13679 }
13680
13681 asection *
13682 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
13683 {
13684 return bfd_com_section_ptr;
13685 }
13686
13687 bfd_vma
13688 _bfd_elf_default_got_elt_size (bfd *abfd,
13689 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13690 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
13691 bfd *ibfd ATTRIBUTE_UNUSED,
13692 unsigned long symndx ATTRIBUTE_UNUSED)
13693 {
13694 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13695 return bed->s->arch_size / 8;
13696 }
13697
13698 /* Routines to support the creation of dynamic relocs. */
13699
13700 /* Returns the name of the dynamic reloc section associated with SEC. */
13701
13702 static const char *
13703 get_dynamic_reloc_section_name (bfd * abfd,
13704 asection * sec,
13705 bfd_boolean is_rela)
13706 {
13707 char *name;
13708 const char *old_name = bfd_get_section_name (NULL, sec);
13709 const char *prefix = is_rela ? ".rela" : ".rel";
13710
13711 if (old_name == NULL)
13712 return NULL;
13713
13714 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
13715 sprintf (name, "%s%s", prefix, old_name);
13716
13717 return name;
13718 }
13719
13720 /* Returns the dynamic reloc section associated with SEC.
13721 If necessary compute the name of the dynamic reloc section based
13722 on SEC's name (looked up in ABFD's string table) and the setting
13723 of IS_RELA. */
13724
13725 asection *
13726 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
13727 asection * sec,
13728 bfd_boolean is_rela)
13729 {
13730 asection * reloc_sec = elf_section_data (sec)->sreloc;
13731
13732 if (reloc_sec == NULL)
13733 {
13734 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13735
13736 if (name != NULL)
13737 {
13738 reloc_sec = bfd_get_linker_section (abfd, name);
13739
13740 if (reloc_sec != NULL)
13741 elf_section_data (sec)->sreloc = reloc_sec;
13742 }
13743 }
13744
13745 return reloc_sec;
13746 }
13747
13748 /* Returns the dynamic reloc section associated with SEC. If the
13749 section does not exist it is created and attached to the DYNOBJ
13750 bfd and stored in the SRELOC field of SEC's elf_section_data
13751 structure.
13752
13753 ALIGNMENT is the alignment for the newly created section and
13754 IS_RELA defines whether the name should be .rela.<SEC's name>
13755 or .rel.<SEC's name>. The section name is looked up in the
13756 string table associated with ABFD. */
13757
13758 asection *
13759 _bfd_elf_make_dynamic_reloc_section (asection *sec,
13760 bfd *dynobj,
13761 unsigned int alignment,
13762 bfd *abfd,
13763 bfd_boolean is_rela)
13764 {
13765 asection * reloc_sec = elf_section_data (sec)->sreloc;
13766
13767 if (reloc_sec == NULL)
13768 {
13769 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13770
13771 if (name == NULL)
13772 return NULL;
13773
13774 reloc_sec = bfd_get_linker_section (dynobj, name);
13775
13776 if (reloc_sec == NULL)
13777 {
13778 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
13779 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
13780 if ((sec->flags & SEC_ALLOC) != 0)
13781 flags |= SEC_ALLOC | SEC_LOAD;
13782
13783 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
13784 if (reloc_sec != NULL)
13785 {
13786 /* _bfd_elf_get_sec_type_attr chooses a section type by
13787 name. Override as it may be wrong, eg. for a user
13788 section named "auto" we'll get ".relauto" which is
13789 seen to be a .rela section. */
13790 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
13791 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
13792 reloc_sec = NULL;
13793 }
13794 }
13795
13796 elf_section_data (sec)->sreloc = reloc_sec;
13797 }
13798
13799 return reloc_sec;
13800 }
13801
13802 /* Copy the ELF symbol type and other attributes for a linker script
13803 assignment from HSRC to HDEST. Generally this should be treated as
13804 if we found a strong non-dynamic definition for HDEST (except that
13805 ld ignores multiple definition errors). */
13806 void
13807 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
13808 struct bfd_link_hash_entry *hdest,
13809 struct bfd_link_hash_entry *hsrc)
13810 {
13811 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
13812 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
13813 Elf_Internal_Sym isym;
13814
13815 ehdest->type = ehsrc->type;
13816 ehdest->target_internal = ehsrc->target_internal;
13817
13818 isym.st_other = ehsrc->other;
13819 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
13820 }
13821
13822 /* Append a RELA relocation REL to section S in BFD. */
13823
13824 void
13825 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13826 {
13827 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13828 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
13829 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
13830 bed->s->swap_reloca_out (abfd, rel, loc);
13831 }
13832
13833 /* Append a REL relocation REL to section S in BFD. */
13834
13835 void
13836 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13837 {
13838 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13839 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
13840 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
13841 bed->s->swap_reloc_out (abfd, rel, loc);
13842 }
This page took 0.487236 seconds and 4 git commands to generate.