Add output_type to bfd_link_info
[binutils-gdb.git] / bfd / elflink.c
1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2015 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 hash_table->dynobj = abfd;
208
209 if (hash_table->dynstr == NULL)
210 {
211 hash_table->dynstr = _bfd_elf_strtab_init ();
212 if (hash_table->dynstr == NULL)
213 return FALSE;
214 }
215 return TRUE;
216 }
217
218 /* Create some sections which will be filled in with dynamic linking
219 information. ABFD is an input file which requires dynamic sections
220 to be created. The dynamic sections take up virtual memory space
221 when the final executable is run, so we need to create them before
222 addresses are assigned to the output sections. We work out the
223 actual contents and size of these sections later. */
224
225 bfd_boolean
226 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
227 {
228 flagword flags;
229 asection *s;
230 const struct elf_backend_data *bed;
231 struct elf_link_hash_entry *h;
232
233 if (! is_elf_hash_table (info->hash))
234 return FALSE;
235
236 if (elf_hash_table (info)->dynamic_sections_created)
237 return TRUE;
238
239 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
240 return FALSE;
241
242 abfd = elf_hash_table (info)->dynobj;
243 bed = get_elf_backend_data (abfd);
244
245 flags = bed->dynamic_sec_flags;
246
247 /* A dynamically linked executable has a .interp section, but a
248 shared library does not. */
249 if (bfd_link_executable (info))
250 {
251 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
252 flags | SEC_READONLY);
253 if (s == NULL)
254 return FALSE;
255 }
256
257 /* Create sections to hold version informations. These are removed
258 if they are not needed. */
259 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
260 flags | SEC_READONLY);
261 if (s == NULL
262 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
263 return FALSE;
264
265 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
266 flags | SEC_READONLY);
267 if (s == NULL
268 || ! bfd_set_section_alignment (abfd, s, 1))
269 return FALSE;
270
271 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
272 flags | SEC_READONLY);
273 if (s == NULL
274 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
275 return FALSE;
276
277 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
278 flags | SEC_READONLY);
279 if (s == NULL
280 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
281 return FALSE;
282
283 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
284 flags | SEC_READONLY);
285 if (s == NULL)
286 return FALSE;
287
288 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
289 if (s == NULL
290 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
291 return FALSE;
292
293 /* The special symbol _DYNAMIC is always set to the start of the
294 .dynamic section. We could set _DYNAMIC in a linker script, but we
295 only want to define it if we are, in fact, creating a .dynamic
296 section. We don't want to define it if there is no .dynamic
297 section, since on some ELF platforms the start up code examines it
298 to decide how to initialize the process. */
299 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
300 elf_hash_table (info)->hdynamic = h;
301 if (h == NULL)
302 return FALSE;
303
304 if (info->emit_hash)
305 {
306 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
307 flags | SEC_READONLY);
308 if (s == NULL
309 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
310 return FALSE;
311 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
312 }
313
314 if (info->emit_gnu_hash)
315 {
316 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
317 flags | SEC_READONLY);
318 if (s == NULL
319 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
320 return FALSE;
321 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
322 4 32-bit words followed by variable count of 64-bit words, then
323 variable count of 32-bit words. */
324 if (bed->s->arch_size == 64)
325 elf_section_data (s)->this_hdr.sh_entsize = 0;
326 else
327 elf_section_data (s)->this_hdr.sh_entsize = 4;
328 }
329
330 /* Let the backend create the rest of the sections. This lets the
331 backend set the right flags. The backend will normally create
332 the .got and .plt sections. */
333 if (bed->elf_backend_create_dynamic_sections == NULL
334 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
335 return FALSE;
336
337 elf_hash_table (info)->dynamic_sections_created = TRUE;
338
339 return TRUE;
340 }
341
342 /* Create dynamic sections when linking against a dynamic object. */
343
344 bfd_boolean
345 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
346 {
347 flagword flags, pltflags;
348 struct elf_link_hash_entry *h;
349 asection *s;
350 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
351 struct elf_link_hash_table *htab = elf_hash_table (info);
352
353 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
354 .rel[a].bss sections. */
355 flags = bed->dynamic_sec_flags;
356
357 pltflags = flags;
358 if (bed->plt_not_loaded)
359 /* We do not clear SEC_ALLOC here because we still want the OS to
360 allocate space for the section; it's just that there's nothing
361 to read in from the object file. */
362 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
363 else
364 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
365 if (bed->plt_readonly)
366 pltflags |= SEC_READONLY;
367
368 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
369 if (s == NULL
370 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
371 return FALSE;
372 htab->splt = s;
373
374 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
375 .plt section. */
376 if (bed->want_plt_sym)
377 {
378 h = _bfd_elf_define_linkage_sym (abfd, info, s,
379 "_PROCEDURE_LINKAGE_TABLE_");
380 elf_hash_table (info)->hplt = h;
381 if (h == NULL)
382 return FALSE;
383 }
384
385 s = bfd_make_section_anyway_with_flags (abfd,
386 (bed->rela_plts_and_copies_p
387 ? ".rela.plt" : ".rel.plt"),
388 flags | SEC_READONLY);
389 if (s == NULL
390 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
391 return FALSE;
392 htab->srelplt = s;
393
394 if (! _bfd_elf_create_got_section (abfd, info))
395 return FALSE;
396
397 if (bed->want_dynbss)
398 {
399 /* The .dynbss section is a place to put symbols which are defined
400 by dynamic objects, are referenced by regular objects, and are
401 not functions. We must allocate space for them in the process
402 image and use a R_*_COPY reloc to tell the dynamic linker to
403 initialize them at run time. The linker script puts the .dynbss
404 section into the .bss section of the final image. */
405 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
406 (SEC_ALLOC | SEC_LINKER_CREATED));
407 if (s == NULL)
408 return FALSE;
409
410 /* The .rel[a].bss section holds copy relocs. This section is not
411 normally needed. We need to create it here, though, so that the
412 linker will map it to an output section. We can't just create it
413 only if we need it, because we will not know whether we need it
414 until we have seen all the input files, and the first time the
415 main linker code calls BFD after examining all the input files
416 (size_dynamic_sections) the input sections have already been
417 mapped to the output sections. If the section turns out not to
418 be needed, we can discard it later. We will never need this
419 section when generating a shared object, since they do not use
420 copy relocs. */
421 if (! bfd_link_pic (info))
422 {
423 s = bfd_make_section_anyway_with_flags (abfd,
424 (bed->rela_plts_and_copies_p
425 ? ".rela.bss" : ".rel.bss"),
426 flags | SEC_READONLY);
427 if (s == NULL
428 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
429 return FALSE;
430 }
431 }
432
433 return TRUE;
434 }
435 \f
436 /* Record a new dynamic symbol. We record the dynamic symbols as we
437 read the input files, since we need to have a list of all of them
438 before we can determine the final sizes of the output sections.
439 Note that we may actually call this function even though we are not
440 going to output any dynamic symbols; in some cases we know that a
441 symbol should be in the dynamic symbol table, but only if there is
442 one. */
443
444 bfd_boolean
445 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
446 struct elf_link_hash_entry *h)
447 {
448 if (h->dynindx == -1)
449 {
450 struct elf_strtab_hash *dynstr;
451 char *p;
452 const char *name;
453 bfd_size_type indx;
454
455 /* XXX: The ABI draft says the linker must turn hidden and
456 internal symbols into STB_LOCAL symbols when producing the
457 DSO. However, if ld.so honors st_other in the dynamic table,
458 this would not be necessary. */
459 switch (ELF_ST_VISIBILITY (h->other))
460 {
461 case STV_INTERNAL:
462 case STV_HIDDEN:
463 if (h->root.type != bfd_link_hash_undefined
464 && h->root.type != bfd_link_hash_undefweak)
465 {
466 h->forced_local = 1;
467 if (!elf_hash_table (info)->is_relocatable_executable)
468 return TRUE;
469 }
470
471 default:
472 break;
473 }
474
475 h->dynindx = elf_hash_table (info)->dynsymcount;
476 ++elf_hash_table (info)->dynsymcount;
477
478 dynstr = elf_hash_table (info)->dynstr;
479 if (dynstr == NULL)
480 {
481 /* Create a strtab to hold the dynamic symbol names. */
482 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
483 if (dynstr == NULL)
484 return FALSE;
485 }
486
487 /* We don't put any version information in the dynamic string
488 table. */
489 name = h->root.root.string;
490 p = strchr (name, ELF_VER_CHR);
491 if (p != NULL)
492 /* We know that the p points into writable memory. In fact,
493 there are only a few symbols that have read-only names, being
494 those like _GLOBAL_OFFSET_TABLE_ that are created specially
495 by the backends. Most symbols will have names pointing into
496 an ELF string table read from a file, or to objalloc memory. */
497 *p = 0;
498
499 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
500
501 if (p != NULL)
502 *p = ELF_VER_CHR;
503
504 if (indx == (bfd_size_type) -1)
505 return FALSE;
506 h->dynstr_index = indx;
507 }
508
509 return TRUE;
510 }
511 \f
512 /* Mark a symbol dynamic. */
513
514 static void
515 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
516 struct elf_link_hash_entry *h,
517 Elf_Internal_Sym *sym)
518 {
519 struct bfd_elf_dynamic_list *d = info->dynamic_list;
520
521 /* It may be called more than once on the same H. */
522 if(h->dynamic || bfd_link_relocatable (info))
523 return;
524
525 if ((info->dynamic_data
526 && (h->type == STT_OBJECT
527 || (sym != NULL
528 && ELF_ST_TYPE (sym->st_info) == STT_OBJECT)))
529 || (d != NULL
530 && h->root.type == bfd_link_hash_new
531 && (*d->match) (&d->head, NULL, h->root.root.string)))
532 h->dynamic = 1;
533 }
534
535 /* Record an assignment to a symbol made by a linker script. We need
536 this in case some dynamic object refers to this symbol. */
537
538 bfd_boolean
539 bfd_elf_record_link_assignment (bfd *output_bfd,
540 struct bfd_link_info *info,
541 const char *name,
542 bfd_boolean provide,
543 bfd_boolean hidden)
544 {
545 struct elf_link_hash_entry *h, *hv;
546 struct elf_link_hash_table *htab;
547 const struct elf_backend_data *bed;
548
549 if (!is_elf_hash_table (info->hash))
550 return TRUE;
551
552 htab = elf_hash_table (info);
553 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
554 if (h == NULL)
555 return provide;
556
557 switch (h->root.type)
558 {
559 case bfd_link_hash_defined:
560 case bfd_link_hash_defweak:
561 case bfd_link_hash_common:
562 break;
563 case bfd_link_hash_undefweak:
564 case bfd_link_hash_undefined:
565 /* Since we're defining the symbol, don't let it seem to have not
566 been defined. record_dynamic_symbol and size_dynamic_sections
567 may depend on this. */
568 h->root.type = bfd_link_hash_new;
569 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
570 bfd_link_repair_undef_list (&htab->root);
571 break;
572 case bfd_link_hash_new:
573 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
574 h->non_elf = 0;
575 break;
576 case bfd_link_hash_indirect:
577 /* We had a versioned symbol in a dynamic library. We make the
578 the versioned symbol point to this one. */
579 bed = get_elf_backend_data (output_bfd);
580 hv = h;
581 while (hv->root.type == bfd_link_hash_indirect
582 || hv->root.type == bfd_link_hash_warning)
583 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
584 /* We don't need to update h->root.u since linker will set them
585 later. */
586 h->root.type = bfd_link_hash_undefined;
587 hv->root.type = bfd_link_hash_indirect;
588 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
589 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
590 break;
591 case bfd_link_hash_warning:
592 abort ();
593 break;
594 }
595
596 /* If this symbol is being provided by the linker script, and it is
597 currently defined by a dynamic object, but not by a regular
598 object, then mark it as undefined so that the generic linker will
599 force the correct value. */
600 if (provide
601 && h->def_dynamic
602 && !h->def_regular)
603 h->root.type = bfd_link_hash_undefined;
604
605 /* If this symbol is not being provided by the linker script, and it is
606 currently defined by a dynamic object, but not by a regular object,
607 then clear out any version information because the symbol will not be
608 associated with the dynamic object any more. */
609 if (!provide
610 && h->def_dynamic
611 && !h->def_regular)
612 h->verinfo.verdef = NULL;
613
614 h->def_regular = 1;
615
616 if (hidden)
617 {
618 bed = get_elf_backend_data (output_bfd);
619 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
620 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
621 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
622 }
623
624 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
625 and executables. */
626 if (!bfd_link_relocatable (info)
627 && h->dynindx != -1
628 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
629 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
630 h->forced_local = 1;
631
632 if ((h->def_dynamic
633 || h->ref_dynamic
634 || bfd_link_pic (info)
635 || (bfd_link_executable (info)
636 && elf_hash_table (info)->is_relocatable_executable))
637 && h->dynindx == -1)
638 {
639 if (! bfd_elf_link_record_dynamic_symbol (info, h))
640 return FALSE;
641
642 /* If this is a weak defined symbol, and we know a corresponding
643 real symbol from the same dynamic object, make sure the real
644 symbol is also made into a dynamic symbol. */
645 if (h->u.weakdef != NULL
646 && h->u.weakdef->dynindx == -1)
647 {
648 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
649 return FALSE;
650 }
651 }
652
653 return TRUE;
654 }
655
656 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
657 success, and 2 on a failure caused by attempting to record a symbol
658 in a discarded section, eg. a discarded link-once section symbol. */
659
660 int
661 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
662 bfd *input_bfd,
663 long input_indx)
664 {
665 bfd_size_type amt;
666 struct elf_link_local_dynamic_entry *entry;
667 struct elf_link_hash_table *eht;
668 struct elf_strtab_hash *dynstr;
669 unsigned long dynstr_index;
670 char *name;
671 Elf_External_Sym_Shndx eshndx;
672 char esym[sizeof (Elf64_External_Sym)];
673
674 if (! is_elf_hash_table (info->hash))
675 return 0;
676
677 /* See if the entry exists already. */
678 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
679 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
680 return 1;
681
682 amt = sizeof (*entry);
683 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
684 if (entry == NULL)
685 return 0;
686
687 /* Go find the symbol, so that we can find it's name. */
688 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
689 1, input_indx, &entry->isym, esym, &eshndx))
690 {
691 bfd_release (input_bfd, entry);
692 return 0;
693 }
694
695 if (entry->isym.st_shndx != SHN_UNDEF
696 && entry->isym.st_shndx < SHN_LORESERVE)
697 {
698 asection *s;
699
700 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
701 if (s == NULL || bfd_is_abs_section (s->output_section))
702 {
703 /* We can still bfd_release here as nothing has done another
704 bfd_alloc. We can't do this later in this function. */
705 bfd_release (input_bfd, entry);
706 return 2;
707 }
708 }
709
710 name = (bfd_elf_string_from_elf_section
711 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
712 entry->isym.st_name));
713
714 dynstr = elf_hash_table (info)->dynstr;
715 if (dynstr == NULL)
716 {
717 /* Create a strtab to hold the dynamic symbol names. */
718 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
719 if (dynstr == NULL)
720 return 0;
721 }
722
723 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
724 if (dynstr_index == (unsigned long) -1)
725 return 0;
726 entry->isym.st_name = dynstr_index;
727
728 eht = elf_hash_table (info);
729
730 entry->next = eht->dynlocal;
731 eht->dynlocal = entry;
732 entry->input_bfd = input_bfd;
733 entry->input_indx = input_indx;
734 eht->dynsymcount++;
735
736 /* Whatever binding the symbol had before, it's now local. */
737 entry->isym.st_info
738 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
739
740 /* The dynindx will be set at the end of size_dynamic_sections. */
741
742 return 1;
743 }
744
745 /* Return the dynindex of a local dynamic symbol. */
746
747 long
748 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
749 bfd *input_bfd,
750 long input_indx)
751 {
752 struct elf_link_local_dynamic_entry *e;
753
754 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
755 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
756 return e->dynindx;
757 return -1;
758 }
759
760 /* This function is used to renumber the dynamic symbols, if some of
761 them are removed because they are marked as local. This is called
762 via elf_link_hash_traverse. */
763
764 static bfd_boolean
765 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
766 void *data)
767 {
768 size_t *count = (size_t *) data;
769
770 if (h->forced_local)
771 return TRUE;
772
773 if (h->dynindx != -1)
774 h->dynindx = ++(*count);
775
776 return TRUE;
777 }
778
779
780 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
781 STB_LOCAL binding. */
782
783 static bfd_boolean
784 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
785 void *data)
786 {
787 size_t *count = (size_t *) data;
788
789 if (!h->forced_local)
790 return TRUE;
791
792 if (h->dynindx != -1)
793 h->dynindx = ++(*count);
794
795 return TRUE;
796 }
797
798 /* Return true if the dynamic symbol for a given section should be
799 omitted when creating a shared library. */
800 bfd_boolean
801 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
802 struct bfd_link_info *info,
803 asection *p)
804 {
805 struct elf_link_hash_table *htab;
806 asection *ip;
807
808 switch (elf_section_data (p)->this_hdr.sh_type)
809 {
810 case SHT_PROGBITS:
811 case SHT_NOBITS:
812 /* If sh_type is yet undecided, assume it could be
813 SHT_PROGBITS/SHT_NOBITS. */
814 case SHT_NULL:
815 htab = elf_hash_table (info);
816 if (p == htab->tls_sec)
817 return FALSE;
818
819 if (htab->text_index_section != NULL)
820 return p != htab->text_index_section && p != htab->data_index_section;
821
822 return (htab->dynobj != NULL
823 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
824 && ip->output_section == p);
825
826 /* There shouldn't be section relative relocations
827 against any other section. */
828 default:
829 return TRUE;
830 }
831 }
832
833 /* Assign dynsym indices. In a shared library we generate a section
834 symbol for each output section, which come first. Next come symbols
835 which have been forced to local binding. Then all of the back-end
836 allocated local dynamic syms, followed by the rest of the global
837 symbols. */
838
839 static unsigned long
840 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
841 struct bfd_link_info *info,
842 unsigned long *section_sym_count)
843 {
844 unsigned long dynsymcount = 0;
845
846 if (bfd_link_pic (info)
847 || elf_hash_table (info)->is_relocatable_executable)
848 {
849 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
850 asection *p;
851 for (p = output_bfd->sections; p ; p = p->next)
852 if ((p->flags & SEC_EXCLUDE) == 0
853 && (p->flags & SEC_ALLOC) != 0
854 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
855 elf_section_data (p)->dynindx = ++dynsymcount;
856 else
857 elf_section_data (p)->dynindx = 0;
858 }
859 *section_sym_count = dynsymcount;
860
861 elf_link_hash_traverse (elf_hash_table (info),
862 elf_link_renumber_local_hash_table_dynsyms,
863 &dynsymcount);
864
865 if (elf_hash_table (info)->dynlocal)
866 {
867 struct elf_link_local_dynamic_entry *p;
868 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
869 p->dynindx = ++dynsymcount;
870 }
871
872 elf_link_hash_traverse (elf_hash_table (info),
873 elf_link_renumber_hash_table_dynsyms,
874 &dynsymcount);
875
876 /* There is an unused NULL entry at the head of the table which
877 we must account for in our count. Unless there weren't any
878 symbols, which means we'll have no table at all. */
879 if (dynsymcount != 0)
880 ++dynsymcount;
881
882 elf_hash_table (info)->dynsymcount = dynsymcount;
883 return dynsymcount;
884 }
885
886 /* Merge st_other field. */
887
888 static void
889 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
890 const Elf_Internal_Sym *isym, asection *sec,
891 bfd_boolean definition, bfd_boolean dynamic)
892 {
893 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
894
895 /* If st_other has a processor-specific meaning, specific
896 code might be needed here. */
897 if (bed->elf_backend_merge_symbol_attribute)
898 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
899 dynamic);
900
901 if (!dynamic)
902 {
903 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
904 unsigned hvis = ELF_ST_VISIBILITY (h->other);
905
906 /* Keep the most constraining visibility. Leave the remainder
907 of the st_other field to elf_backend_merge_symbol_attribute. */
908 if (symvis - 1 < hvis - 1)
909 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
910 }
911 else if (definition
912 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
913 && (sec->flags & SEC_READONLY) == 0)
914 h->protected_def = 1;
915 }
916
917 /* This function is called when we want to merge a new symbol with an
918 existing symbol. It handles the various cases which arise when we
919 find a definition in a dynamic object, or when there is already a
920 definition in a dynamic object. The new symbol is described by
921 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
922 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
923 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
924 of an old common symbol. We set OVERRIDE if the old symbol is
925 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
926 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
927 to change. By OK to change, we mean that we shouldn't warn if the
928 type or size does change. */
929
930 static bfd_boolean
931 _bfd_elf_merge_symbol (bfd *abfd,
932 struct bfd_link_info *info,
933 const char *name,
934 Elf_Internal_Sym *sym,
935 asection **psec,
936 bfd_vma *pvalue,
937 struct elf_link_hash_entry **sym_hash,
938 bfd **poldbfd,
939 bfd_boolean *pold_weak,
940 unsigned int *pold_alignment,
941 bfd_boolean *skip,
942 bfd_boolean *override,
943 bfd_boolean *type_change_ok,
944 bfd_boolean *size_change_ok,
945 bfd_boolean *matched)
946 {
947 asection *sec, *oldsec;
948 struct elf_link_hash_entry *h;
949 struct elf_link_hash_entry *hi;
950 struct elf_link_hash_entry *flip;
951 int bind;
952 bfd *oldbfd;
953 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
954 bfd_boolean newweak, oldweak, newfunc, oldfunc;
955 const struct elf_backend_data *bed;
956 char *new_version;
957
958 *skip = FALSE;
959 *override = FALSE;
960
961 sec = *psec;
962 bind = ELF_ST_BIND (sym->st_info);
963
964 if (! bfd_is_und_section (sec))
965 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
966 else
967 h = ((struct elf_link_hash_entry *)
968 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
969 if (h == NULL)
970 return FALSE;
971 *sym_hash = h;
972
973 bed = get_elf_backend_data (abfd);
974
975 /* NEW_VERSION is the symbol version of the new symbol. */
976 if (h->versioned != unversioned)
977 {
978 /* Symbol version is unknown or versioned. */
979 new_version = strrchr (name, ELF_VER_CHR);
980 if (new_version)
981 {
982 if (h->versioned == unknown)
983 {
984 if (new_version > name && new_version[-1] != ELF_VER_CHR)
985 h->versioned = versioned_hidden;
986 else
987 h->versioned = versioned;
988 }
989 new_version += 1;
990 if (new_version[0] == '\0')
991 new_version = NULL;
992 }
993 else
994 h->versioned = unversioned;
995 }
996 else
997 new_version = NULL;
998
999 /* For merging, we only care about real symbols. But we need to make
1000 sure that indirect symbol dynamic flags are updated. */
1001 hi = h;
1002 while (h->root.type == bfd_link_hash_indirect
1003 || h->root.type == bfd_link_hash_warning)
1004 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1005
1006 if (!*matched)
1007 {
1008 if (hi == h || h->root.type == bfd_link_hash_new)
1009 *matched = TRUE;
1010 else
1011 {
1012 /* OLD_HIDDEN is true if the existing symbol is only visibile
1013 to the symbol with the same symbol version. NEW_HIDDEN is
1014 true if the new symbol is only visibile to the symbol with
1015 the same symbol version. */
1016 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1017 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1018 if (!old_hidden && !new_hidden)
1019 /* The new symbol matches the existing symbol if both
1020 aren't hidden. */
1021 *matched = TRUE;
1022 else
1023 {
1024 /* OLD_VERSION is the symbol version of the existing
1025 symbol. */
1026 char *old_version;
1027
1028 if (h->versioned >= versioned)
1029 old_version = strrchr (h->root.root.string,
1030 ELF_VER_CHR) + 1;
1031 else
1032 old_version = NULL;
1033
1034 /* The new symbol matches the existing symbol if they
1035 have the same symbol version. */
1036 *matched = (old_version == new_version
1037 || (old_version != NULL
1038 && new_version != NULL
1039 && strcmp (old_version, new_version) == 0));
1040 }
1041 }
1042 }
1043
1044 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1045 existing symbol. */
1046
1047 oldbfd = NULL;
1048 oldsec = NULL;
1049 switch (h->root.type)
1050 {
1051 default:
1052 break;
1053
1054 case bfd_link_hash_undefined:
1055 case bfd_link_hash_undefweak:
1056 oldbfd = h->root.u.undef.abfd;
1057 break;
1058
1059 case bfd_link_hash_defined:
1060 case bfd_link_hash_defweak:
1061 oldbfd = h->root.u.def.section->owner;
1062 oldsec = h->root.u.def.section;
1063 break;
1064
1065 case bfd_link_hash_common:
1066 oldbfd = h->root.u.c.p->section->owner;
1067 oldsec = h->root.u.c.p->section;
1068 if (pold_alignment)
1069 *pold_alignment = h->root.u.c.p->alignment_power;
1070 break;
1071 }
1072 if (poldbfd && *poldbfd == NULL)
1073 *poldbfd = oldbfd;
1074
1075 /* Differentiate strong and weak symbols. */
1076 newweak = bind == STB_WEAK;
1077 oldweak = (h->root.type == bfd_link_hash_defweak
1078 || h->root.type == bfd_link_hash_undefweak);
1079 if (pold_weak)
1080 *pold_weak = oldweak;
1081
1082 /* This code is for coping with dynamic objects, and is only useful
1083 if we are doing an ELF link. */
1084 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1085 return TRUE;
1086
1087 /* We have to check it for every instance since the first few may be
1088 references and not all compilers emit symbol type for undefined
1089 symbols. */
1090 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1091
1092 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1093 respectively, is from a dynamic object. */
1094
1095 newdyn = (abfd->flags & DYNAMIC) != 0;
1096
1097 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1098 syms and defined syms in dynamic libraries respectively.
1099 ref_dynamic on the other hand can be set for a symbol defined in
1100 a dynamic library, and def_dynamic may not be set; When the
1101 definition in a dynamic lib is overridden by a definition in the
1102 executable use of the symbol in the dynamic lib becomes a
1103 reference to the executable symbol. */
1104 if (newdyn)
1105 {
1106 if (bfd_is_und_section (sec))
1107 {
1108 if (bind != STB_WEAK)
1109 {
1110 h->ref_dynamic_nonweak = 1;
1111 hi->ref_dynamic_nonweak = 1;
1112 }
1113 }
1114 else
1115 {
1116 /* Update the existing symbol only if they match. */
1117 if (*matched)
1118 h->dynamic_def = 1;
1119 hi->dynamic_def = 1;
1120 }
1121 }
1122
1123 /* If we just created the symbol, mark it as being an ELF symbol.
1124 Other than that, there is nothing to do--there is no merge issue
1125 with a newly defined symbol--so we just return. */
1126
1127 if (h->root.type == bfd_link_hash_new)
1128 {
1129 h->non_elf = 0;
1130 return TRUE;
1131 }
1132
1133 /* In cases involving weak versioned symbols, we may wind up trying
1134 to merge a symbol with itself. Catch that here, to avoid the
1135 confusion that results if we try to override a symbol with
1136 itself. The additional tests catch cases like
1137 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1138 dynamic object, which we do want to handle here. */
1139 if (abfd == oldbfd
1140 && (newweak || oldweak)
1141 && ((abfd->flags & DYNAMIC) == 0
1142 || !h->def_regular))
1143 return TRUE;
1144
1145 olddyn = FALSE;
1146 if (oldbfd != NULL)
1147 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1148 else if (oldsec != NULL)
1149 {
1150 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1151 indices used by MIPS ELF. */
1152 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1153 }
1154
1155 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1156 respectively, appear to be a definition rather than reference. */
1157
1158 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1159
1160 olddef = (h->root.type != bfd_link_hash_undefined
1161 && h->root.type != bfd_link_hash_undefweak
1162 && h->root.type != bfd_link_hash_common);
1163
1164 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1165 respectively, appear to be a function. */
1166
1167 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1168 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1169
1170 oldfunc = (h->type != STT_NOTYPE
1171 && bed->is_function_type (h->type));
1172
1173 /* When we try to create a default indirect symbol from the dynamic
1174 definition with the default version, we skip it if its type and
1175 the type of existing regular definition mismatch. */
1176 if (pold_alignment == NULL
1177 && newdyn
1178 && newdef
1179 && !olddyn
1180 && (((olddef || h->root.type == bfd_link_hash_common)
1181 && ELF_ST_TYPE (sym->st_info) != h->type
1182 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1183 && h->type != STT_NOTYPE
1184 && !(newfunc && oldfunc))
1185 || (olddef
1186 && ((h->type == STT_GNU_IFUNC)
1187 != (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))))
1188 {
1189 *skip = TRUE;
1190 return TRUE;
1191 }
1192
1193 /* Check TLS symbols. We don't check undefined symbols introduced
1194 by "ld -u" which have no type (and oldbfd NULL), and we don't
1195 check symbols from plugins because they also have no type. */
1196 if (oldbfd != NULL
1197 && (oldbfd->flags & BFD_PLUGIN) == 0
1198 && (abfd->flags & BFD_PLUGIN) == 0
1199 && ELF_ST_TYPE (sym->st_info) != h->type
1200 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1201 {
1202 bfd *ntbfd, *tbfd;
1203 bfd_boolean ntdef, tdef;
1204 asection *ntsec, *tsec;
1205
1206 if (h->type == STT_TLS)
1207 {
1208 ntbfd = abfd;
1209 ntsec = sec;
1210 ntdef = newdef;
1211 tbfd = oldbfd;
1212 tsec = oldsec;
1213 tdef = olddef;
1214 }
1215 else
1216 {
1217 ntbfd = oldbfd;
1218 ntsec = oldsec;
1219 ntdef = olddef;
1220 tbfd = abfd;
1221 tsec = sec;
1222 tdef = newdef;
1223 }
1224
1225 if (tdef && ntdef)
1226 (*_bfd_error_handler)
1227 (_("%s: TLS definition in %B section %A "
1228 "mismatches non-TLS definition in %B section %A"),
1229 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1230 else if (!tdef && !ntdef)
1231 (*_bfd_error_handler)
1232 (_("%s: TLS reference in %B "
1233 "mismatches non-TLS reference in %B"),
1234 tbfd, ntbfd, h->root.root.string);
1235 else if (tdef)
1236 (*_bfd_error_handler)
1237 (_("%s: TLS definition in %B section %A "
1238 "mismatches non-TLS reference in %B"),
1239 tbfd, tsec, ntbfd, h->root.root.string);
1240 else
1241 (*_bfd_error_handler)
1242 (_("%s: TLS reference in %B "
1243 "mismatches non-TLS definition in %B section %A"),
1244 tbfd, ntbfd, ntsec, h->root.root.string);
1245
1246 bfd_set_error (bfd_error_bad_value);
1247 return FALSE;
1248 }
1249
1250 /* If the old symbol has non-default visibility, we ignore the new
1251 definition from a dynamic object. */
1252 if (newdyn
1253 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1254 && !bfd_is_und_section (sec))
1255 {
1256 *skip = TRUE;
1257 /* Make sure this symbol is dynamic. */
1258 h->ref_dynamic = 1;
1259 hi->ref_dynamic = 1;
1260 /* A protected symbol has external availability. Make sure it is
1261 recorded as dynamic.
1262
1263 FIXME: Should we check type and size for protected symbol? */
1264 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1265 return bfd_elf_link_record_dynamic_symbol (info, h);
1266 else
1267 return TRUE;
1268 }
1269 else if (!newdyn
1270 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1271 && h->def_dynamic)
1272 {
1273 /* If the new symbol with non-default visibility comes from a
1274 relocatable file and the old definition comes from a dynamic
1275 object, we remove the old definition. */
1276 if (hi->root.type == bfd_link_hash_indirect)
1277 {
1278 /* Handle the case where the old dynamic definition is
1279 default versioned. We need to copy the symbol info from
1280 the symbol with default version to the normal one if it
1281 was referenced before. */
1282 if (h->ref_regular)
1283 {
1284 hi->root.type = h->root.type;
1285 h->root.type = bfd_link_hash_indirect;
1286 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1287
1288 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1289 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1290 {
1291 /* If the new symbol is hidden or internal, completely undo
1292 any dynamic link state. */
1293 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1294 h->forced_local = 0;
1295 h->ref_dynamic = 0;
1296 }
1297 else
1298 h->ref_dynamic = 1;
1299
1300 h->def_dynamic = 0;
1301 /* FIXME: Should we check type and size for protected symbol? */
1302 h->size = 0;
1303 h->type = 0;
1304
1305 h = hi;
1306 }
1307 else
1308 h = hi;
1309 }
1310
1311 /* If the old symbol was undefined before, then it will still be
1312 on the undefs list. If the new symbol is undefined or
1313 common, we can't make it bfd_link_hash_new here, because new
1314 undefined or common symbols will be added to the undefs list
1315 by _bfd_generic_link_add_one_symbol. Symbols may not be
1316 added twice to the undefs list. Also, if the new symbol is
1317 undefweak then we don't want to lose the strong undef. */
1318 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1319 {
1320 h->root.type = bfd_link_hash_undefined;
1321 h->root.u.undef.abfd = abfd;
1322 }
1323 else
1324 {
1325 h->root.type = bfd_link_hash_new;
1326 h->root.u.undef.abfd = NULL;
1327 }
1328
1329 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1330 {
1331 /* If the new symbol is hidden or internal, completely undo
1332 any dynamic link state. */
1333 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1334 h->forced_local = 0;
1335 h->ref_dynamic = 0;
1336 }
1337 else
1338 h->ref_dynamic = 1;
1339 h->def_dynamic = 0;
1340 /* FIXME: Should we check type and size for protected symbol? */
1341 h->size = 0;
1342 h->type = 0;
1343 return TRUE;
1344 }
1345
1346 /* If a new weak symbol definition comes from a regular file and the
1347 old symbol comes from a dynamic library, we treat the new one as
1348 strong. Similarly, an old weak symbol definition from a regular
1349 file is treated as strong when the new symbol comes from a dynamic
1350 library. Further, an old weak symbol from a dynamic library is
1351 treated as strong if the new symbol is from a dynamic library.
1352 This reflects the way glibc's ld.so works.
1353
1354 Do this before setting *type_change_ok or *size_change_ok so that
1355 we warn properly when dynamic library symbols are overridden. */
1356
1357 if (newdef && !newdyn && olddyn)
1358 newweak = FALSE;
1359 if (olddef && newdyn)
1360 oldweak = FALSE;
1361
1362 /* Allow changes between different types of function symbol. */
1363 if (newfunc && oldfunc)
1364 *type_change_ok = TRUE;
1365
1366 /* It's OK to change the type if either the existing symbol or the
1367 new symbol is weak. A type change is also OK if the old symbol
1368 is undefined and the new symbol is defined. */
1369
1370 if (oldweak
1371 || newweak
1372 || (newdef
1373 && h->root.type == bfd_link_hash_undefined))
1374 *type_change_ok = TRUE;
1375
1376 /* It's OK to change the size if either the existing symbol or the
1377 new symbol is weak, or if the old symbol is undefined. */
1378
1379 if (*type_change_ok
1380 || h->root.type == bfd_link_hash_undefined)
1381 *size_change_ok = TRUE;
1382
1383 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1384 symbol, respectively, appears to be a common symbol in a dynamic
1385 object. If a symbol appears in an uninitialized section, and is
1386 not weak, and is not a function, then it may be a common symbol
1387 which was resolved when the dynamic object was created. We want
1388 to treat such symbols specially, because they raise special
1389 considerations when setting the symbol size: if the symbol
1390 appears as a common symbol in a regular object, and the size in
1391 the regular object is larger, we must make sure that we use the
1392 larger size. This problematic case can always be avoided in C,
1393 but it must be handled correctly when using Fortran shared
1394 libraries.
1395
1396 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1397 likewise for OLDDYNCOMMON and OLDDEF.
1398
1399 Note that this test is just a heuristic, and that it is quite
1400 possible to have an uninitialized symbol in a shared object which
1401 is really a definition, rather than a common symbol. This could
1402 lead to some minor confusion when the symbol really is a common
1403 symbol in some regular object. However, I think it will be
1404 harmless. */
1405
1406 if (newdyn
1407 && newdef
1408 && !newweak
1409 && (sec->flags & SEC_ALLOC) != 0
1410 && (sec->flags & SEC_LOAD) == 0
1411 && sym->st_size > 0
1412 && !newfunc)
1413 newdyncommon = TRUE;
1414 else
1415 newdyncommon = FALSE;
1416
1417 if (olddyn
1418 && olddef
1419 && h->root.type == bfd_link_hash_defined
1420 && h->def_dynamic
1421 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1422 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1423 && h->size > 0
1424 && !oldfunc)
1425 olddyncommon = TRUE;
1426 else
1427 olddyncommon = FALSE;
1428
1429 /* We now know everything about the old and new symbols. We ask the
1430 backend to check if we can merge them. */
1431 if (bed->merge_symbol != NULL)
1432 {
1433 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1434 return FALSE;
1435 sec = *psec;
1436 }
1437
1438 /* If both the old and the new symbols look like common symbols in a
1439 dynamic object, set the size of the symbol to the larger of the
1440 two. */
1441
1442 if (olddyncommon
1443 && newdyncommon
1444 && sym->st_size != h->size)
1445 {
1446 /* Since we think we have two common symbols, issue a multiple
1447 common warning if desired. Note that we only warn if the
1448 size is different. If the size is the same, we simply let
1449 the old symbol override the new one as normally happens with
1450 symbols defined in dynamic objects. */
1451
1452 if (! ((*info->callbacks->multiple_common)
1453 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
1454 return FALSE;
1455
1456 if (sym->st_size > h->size)
1457 h->size = sym->st_size;
1458
1459 *size_change_ok = TRUE;
1460 }
1461
1462 /* If we are looking at a dynamic object, and we have found a
1463 definition, we need to see if the symbol was already defined by
1464 some other object. If so, we want to use the existing
1465 definition, and we do not want to report a multiple symbol
1466 definition error; we do this by clobbering *PSEC to be
1467 bfd_und_section_ptr.
1468
1469 We treat a common symbol as a definition if the symbol in the
1470 shared library is a function, since common symbols always
1471 represent variables; this can cause confusion in principle, but
1472 any such confusion would seem to indicate an erroneous program or
1473 shared library. We also permit a common symbol in a regular
1474 object to override a weak symbol in a shared object. */
1475
1476 if (newdyn
1477 && newdef
1478 && (olddef
1479 || (h->root.type == bfd_link_hash_common
1480 && (newweak || newfunc))))
1481 {
1482 *override = TRUE;
1483 newdef = FALSE;
1484 newdyncommon = FALSE;
1485
1486 *psec = sec = bfd_und_section_ptr;
1487 *size_change_ok = TRUE;
1488
1489 /* If we get here when the old symbol is a common symbol, then
1490 we are explicitly letting it override a weak symbol or
1491 function in a dynamic object, and we don't want to warn about
1492 a type change. If the old symbol is a defined symbol, a type
1493 change warning may still be appropriate. */
1494
1495 if (h->root.type == bfd_link_hash_common)
1496 *type_change_ok = TRUE;
1497 }
1498
1499 /* Handle the special case of an old common symbol merging with a
1500 new symbol which looks like a common symbol in a shared object.
1501 We change *PSEC and *PVALUE to make the new symbol look like a
1502 common symbol, and let _bfd_generic_link_add_one_symbol do the
1503 right thing. */
1504
1505 if (newdyncommon
1506 && h->root.type == bfd_link_hash_common)
1507 {
1508 *override = TRUE;
1509 newdef = FALSE;
1510 newdyncommon = FALSE;
1511 *pvalue = sym->st_size;
1512 *psec = sec = bed->common_section (oldsec);
1513 *size_change_ok = TRUE;
1514 }
1515
1516 /* Skip weak definitions of symbols that are already defined. */
1517 if (newdef && olddef && newweak)
1518 {
1519 /* Don't skip new non-IR weak syms. */
1520 if (!(oldbfd != NULL
1521 && (oldbfd->flags & BFD_PLUGIN) != 0
1522 && (abfd->flags & BFD_PLUGIN) == 0))
1523 {
1524 newdef = FALSE;
1525 *skip = TRUE;
1526 }
1527
1528 /* Merge st_other. If the symbol already has a dynamic index,
1529 but visibility says it should not be visible, turn it into a
1530 local symbol. */
1531 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1532 if (h->dynindx != -1)
1533 switch (ELF_ST_VISIBILITY (h->other))
1534 {
1535 case STV_INTERNAL:
1536 case STV_HIDDEN:
1537 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1538 break;
1539 }
1540 }
1541
1542 /* If the old symbol is from a dynamic object, and the new symbol is
1543 a definition which is not from a dynamic object, then the new
1544 symbol overrides the old symbol. Symbols from regular files
1545 always take precedence over symbols from dynamic objects, even if
1546 they are defined after the dynamic object in the link.
1547
1548 As above, we again permit a common symbol in a regular object to
1549 override a definition in a shared object if the shared object
1550 symbol is a function or is weak. */
1551
1552 flip = NULL;
1553 if (!newdyn
1554 && (newdef
1555 || (bfd_is_com_section (sec)
1556 && (oldweak || oldfunc)))
1557 && olddyn
1558 && olddef
1559 && h->def_dynamic)
1560 {
1561 /* Change the hash table entry to undefined, and let
1562 _bfd_generic_link_add_one_symbol do the right thing with the
1563 new definition. */
1564
1565 h->root.type = bfd_link_hash_undefined;
1566 h->root.u.undef.abfd = h->root.u.def.section->owner;
1567 *size_change_ok = TRUE;
1568
1569 olddef = FALSE;
1570 olddyncommon = FALSE;
1571
1572 /* We again permit a type change when a common symbol may be
1573 overriding a function. */
1574
1575 if (bfd_is_com_section (sec))
1576 {
1577 if (oldfunc)
1578 {
1579 /* If a common symbol overrides a function, make sure
1580 that it isn't defined dynamically nor has type
1581 function. */
1582 h->def_dynamic = 0;
1583 h->type = STT_NOTYPE;
1584 }
1585 *type_change_ok = TRUE;
1586 }
1587
1588 if (hi->root.type == bfd_link_hash_indirect)
1589 flip = hi;
1590 else
1591 /* This union may have been set to be non-NULL when this symbol
1592 was seen in a dynamic object. We must force the union to be
1593 NULL, so that it is correct for a regular symbol. */
1594 h->verinfo.vertree = NULL;
1595 }
1596
1597 /* Handle the special case of a new common symbol merging with an
1598 old symbol that looks like it might be a common symbol defined in
1599 a shared object. Note that we have already handled the case in
1600 which a new common symbol should simply override the definition
1601 in the shared library. */
1602
1603 if (! newdyn
1604 && bfd_is_com_section (sec)
1605 && olddyncommon)
1606 {
1607 /* It would be best if we could set the hash table entry to a
1608 common symbol, but we don't know what to use for the section
1609 or the alignment. */
1610 if (! ((*info->callbacks->multiple_common)
1611 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
1612 return FALSE;
1613
1614 /* If the presumed common symbol in the dynamic object is
1615 larger, pretend that the new symbol has its size. */
1616
1617 if (h->size > *pvalue)
1618 *pvalue = h->size;
1619
1620 /* We need to remember the alignment required by the symbol
1621 in the dynamic object. */
1622 BFD_ASSERT (pold_alignment);
1623 *pold_alignment = h->root.u.def.section->alignment_power;
1624
1625 olddef = FALSE;
1626 olddyncommon = FALSE;
1627
1628 h->root.type = bfd_link_hash_undefined;
1629 h->root.u.undef.abfd = h->root.u.def.section->owner;
1630
1631 *size_change_ok = TRUE;
1632 *type_change_ok = TRUE;
1633
1634 if (hi->root.type == bfd_link_hash_indirect)
1635 flip = hi;
1636 else
1637 h->verinfo.vertree = NULL;
1638 }
1639
1640 if (flip != NULL)
1641 {
1642 /* Handle the case where we had a versioned symbol in a dynamic
1643 library and now find a definition in a normal object. In this
1644 case, we make the versioned symbol point to the normal one. */
1645 flip->root.type = h->root.type;
1646 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1647 h->root.type = bfd_link_hash_indirect;
1648 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1649 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1650 if (h->def_dynamic)
1651 {
1652 h->def_dynamic = 0;
1653 flip->ref_dynamic = 1;
1654 }
1655 }
1656
1657 return TRUE;
1658 }
1659
1660 /* This function is called to create an indirect symbol from the
1661 default for the symbol with the default version if needed. The
1662 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1663 set DYNSYM if the new indirect symbol is dynamic. */
1664
1665 static bfd_boolean
1666 _bfd_elf_add_default_symbol (bfd *abfd,
1667 struct bfd_link_info *info,
1668 struct elf_link_hash_entry *h,
1669 const char *name,
1670 Elf_Internal_Sym *sym,
1671 asection *sec,
1672 bfd_vma value,
1673 bfd **poldbfd,
1674 bfd_boolean *dynsym)
1675 {
1676 bfd_boolean type_change_ok;
1677 bfd_boolean size_change_ok;
1678 bfd_boolean skip;
1679 char *shortname;
1680 struct elf_link_hash_entry *hi;
1681 struct bfd_link_hash_entry *bh;
1682 const struct elf_backend_data *bed;
1683 bfd_boolean collect;
1684 bfd_boolean dynamic;
1685 bfd_boolean override;
1686 char *p;
1687 size_t len, shortlen;
1688 asection *tmp_sec;
1689 bfd_boolean matched;
1690
1691 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1692 return TRUE;
1693
1694 /* If this symbol has a version, and it is the default version, we
1695 create an indirect symbol from the default name to the fully
1696 decorated name. This will cause external references which do not
1697 specify a version to be bound to this version of the symbol. */
1698 p = strchr (name, ELF_VER_CHR);
1699 if (h->versioned == unknown)
1700 {
1701 if (p == NULL)
1702 {
1703 h->versioned = unversioned;
1704 return TRUE;
1705 }
1706 else
1707 {
1708 if (p[1] != ELF_VER_CHR)
1709 {
1710 h->versioned = versioned_hidden;
1711 return TRUE;
1712 }
1713 else
1714 h->versioned = versioned;
1715 }
1716 }
1717
1718 bed = get_elf_backend_data (abfd);
1719 collect = bed->collect;
1720 dynamic = (abfd->flags & DYNAMIC) != 0;
1721
1722 shortlen = p - name;
1723 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1724 if (shortname == NULL)
1725 return FALSE;
1726 memcpy (shortname, name, shortlen);
1727 shortname[shortlen] = '\0';
1728
1729 /* We are going to create a new symbol. Merge it with any existing
1730 symbol with this name. For the purposes of the merge, act as
1731 though we were defining the symbol we just defined, although we
1732 actually going to define an indirect symbol. */
1733 type_change_ok = FALSE;
1734 size_change_ok = FALSE;
1735 matched = TRUE;
1736 tmp_sec = sec;
1737 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1738 &hi, poldbfd, NULL, NULL, &skip, &override,
1739 &type_change_ok, &size_change_ok, &matched))
1740 return FALSE;
1741
1742 if (skip)
1743 goto nondefault;
1744
1745 if (! override)
1746 {
1747 /* Add the default symbol if not performing a relocatable link. */
1748 if (! bfd_link_relocatable (info))
1749 {
1750 bh = &hi->root;
1751 if (! (_bfd_generic_link_add_one_symbol
1752 (info, abfd, shortname, BSF_INDIRECT,
1753 bfd_ind_section_ptr,
1754 0, name, FALSE, collect, &bh)))
1755 return FALSE;
1756 hi = (struct elf_link_hash_entry *) bh;
1757 }
1758 }
1759 else
1760 {
1761 /* In this case the symbol named SHORTNAME is overriding the
1762 indirect symbol we want to add. We were planning on making
1763 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1764 is the name without a version. NAME is the fully versioned
1765 name, and it is the default version.
1766
1767 Overriding means that we already saw a definition for the
1768 symbol SHORTNAME in a regular object, and it is overriding
1769 the symbol defined in the dynamic object.
1770
1771 When this happens, we actually want to change NAME, the
1772 symbol we just added, to refer to SHORTNAME. This will cause
1773 references to NAME in the shared object to become references
1774 to SHORTNAME in the regular object. This is what we expect
1775 when we override a function in a shared object: that the
1776 references in the shared object will be mapped to the
1777 definition in the regular object. */
1778
1779 while (hi->root.type == bfd_link_hash_indirect
1780 || hi->root.type == bfd_link_hash_warning)
1781 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1782
1783 h->root.type = bfd_link_hash_indirect;
1784 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1785 if (h->def_dynamic)
1786 {
1787 h->def_dynamic = 0;
1788 hi->ref_dynamic = 1;
1789 if (hi->ref_regular
1790 || hi->def_regular)
1791 {
1792 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1793 return FALSE;
1794 }
1795 }
1796
1797 /* Now set HI to H, so that the following code will set the
1798 other fields correctly. */
1799 hi = h;
1800 }
1801
1802 /* Check if HI is a warning symbol. */
1803 if (hi->root.type == bfd_link_hash_warning)
1804 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1805
1806 /* If there is a duplicate definition somewhere, then HI may not
1807 point to an indirect symbol. We will have reported an error to
1808 the user in that case. */
1809
1810 if (hi->root.type == bfd_link_hash_indirect)
1811 {
1812 struct elf_link_hash_entry *ht;
1813
1814 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1815 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1816
1817 /* A reference to the SHORTNAME symbol from a dynamic library
1818 will be satisfied by the versioned symbol at runtime. In
1819 effect, we have a reference to the versioned symbol. */
1820 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1821 hi->dynamic_def |= ht->dynamic_def;
1822
1823 /* See if the new flags lead us to realize that the symbol must
1824 be dynamic. */
1825 if (! *dynsym)
1826 {
1827 if (! dynamic)
1828 {
1829 if (! bfd_link_executable (info)
1830 || hi->def_dynamic
1831 || hi->ref_dynamic)
1832 *dynsym = TRUE;
1833 }
1834 else
1835 {
1836 if (hi->ref_regular)
1837 *dynsym = TRUE;
1838 }
1839 }
1840 }
1841
1842 /* We also need to define an indirection from the nondefault version
1843 of the symbol. */
1844
1845 nondefault:
1846 len = strlen (name);
1847 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
1848 if (shortname == NULL)
1849 return FALSE;
1850 memcpy (shortname, name, shortlen);
1851 memcpy (shortname + shortlen, p + 1, len - shortlen);
1852
1853 /* Once again, merge with any existing symbol. */
1854 type_change_ok = FALSE;
1855 size_change_ok = FALSE;
1856 tmp_sec = sec;
1857 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1858 &hi, poldbfd, NULL, NULL, &skip, &override,
1859 &type_change_ok, &size_change_ok, &matched))
1860 return FALSE;
1861
1862 if (skip)
1863 return TRUE;
1864
1865 if (override)
1866 {
1867 /* Here SHORTNAME is a versioned name, so we don't expect to see
1868 the type of override we do in the case above unless it is
1869 overridden by a versioned definition. */
1870 if (hi->root.type != bfd_link_hash_defined
1871 && hi->root.type != bfd_link_hash_defweak)
1872 (*_bfd_error_handler)
1873 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1874 abfd, shortname);
1875 }
1876 else
1877 {
1878 bh = &hi->root;
1879 if (! (_bfd_generic_link_add_one_symbol
1880 (info, abfd, shortname, BSF_INDIRECT,
1881 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1882 return FALSE;
1883 hi = (struct elf_link_hash_entry *) bh;
1884
1885 /* If there is a duplicate definition somewhere, then HI may not
1886 point to an indirect symbol. We will have reported an error
1887 to the user in that case. */
1888
1889 if (hi->root.type == bfd_link_hash_indirect)
1890 {
1891 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1892 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1893 hi->dynamic_def |= h->dynamic_def;
1894
1895 /* See if the new flags lead us to realize that the symbol
1896 must be dynamic. */
1897 if (! *dynsym)
1898 {
1899 if (! dynamic)
1900 {
1901 if (! bfd_link_executable (info)
1902 || hi->ref_dynamic)
1903 *dynsym = TRUE;
1904 }
1905 else
1906 {
1907 if (hi->ref_regular)
1908 *dynsym = TRUE;
1909 }
1910 }
1911 }
1912 }
1913
1914 return TRUE;
1915 }
1916 \f
1917 /* This routine is used to export all defined symbols into the dynamic
1918 symbol table. It is called via elf_link_hash_traverse. */
1919
1920 static bfd_boolean
1921 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1922 {
1923 struct elf_info_failed *eif = (struct elf_info_failed *) data;
1924
1925 /* Ignore indirect symbols. These are added by the versioning code. */
1926 if (h->root.type == bfd_link_hash_indirect)
1927 return TRUE;
1928
1929 /* Ignore this if we won't export it. */
1930 if (!eif->info->export_dynamic && !h->dynamic)
1931 return TRUE;
1932
1933 if (h->dynindx == -1
1934 && (h->def_regular || h->ref_regular)
1935 && ! bfd_hide_sym_by_version (eif->info->version_info,
1936 h->root.root.string))
1937 {
1938 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1939 {
1940 eif->failed = TRUE;
1941 return FALSE;
1942 }
1943 }
1944
1945 return TRUE;
1946 }
1947 \f
1948 /* Look through the symbols which are defined in other shared
1949 libraries and referenced here. Update the list of version
1950 dependencies. This will be put into the .gnu.version_r section.
1951 This function is called via elf_link_hash_traverse. */
1952
1953 static bfd_boolean
1954 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1955 void *data)
1956 {
1957 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
1958 Elf_Internal_Verneed *t;
1959 Elf_Internal_Vernaux *a;
1960 bfd_size_type amt;
1961
1962 /* We only care about symbols defined in shared objects with version
1963 information. */
1964 if (!h->def_dynamic
1965 || h->def_regular
1966 || h->dynindx == -1
1967 || h->verinfo.verdef == NULL
1968 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
1969 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
1970 return TRUE;
1971
1972 /* See if we already know about this version. */
1973 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
1974 t != NULL;
1975 t = t->vn_nextref)
1976 {
1977 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1978 continue;
1979
1980 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1981 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1982 return TRUE;
1983
1984 break;
1985 }
1986
1987 /* This is a new version. Add it to tree we are building. */
1988
1989 if (t == NULL)
1990 {
1991 amt = sizeof *t;
1992 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
1993 if (t == NULL)
1994 {
1995 rinfo->failed = TRUE;
1996 return FALSE;
1997 }
1998
1999 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2000 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2001 elf_tdata (rinfo->info->output_bfd)->verref = t;
2002 }
2003
2004 amt = sizeof *a;
2005 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2006 if (a == NULL)
2007 {
2008 rinfo->failed = TRUE;
2009 return FALSE;
2010 }
2011
2012 /* Note that we are copying a string pointer here, and testing it
2013 above. If bfd_elf_string_from_elf_section is ever changed to
2014 discard the string data when low in memory, this will have to be
2015 fixed. */
2016 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2017
2018 a->vna_flags = h->verinfo.verdef->vd_flags;
2019 a->vna_nextptr = t->vn_auxptr;
2020
2021 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2022 ++rinfo->vers;
2023
2024 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2025
2026 t->vn_auxptr = a;
2027
2028 return TRUE;
2029 }
2030
2031 /* Figure out appropriate versions for all the symbols. We may not
2032 have the version number script until we have read all of the input
2033 files, so until that point we don't know which symbols should be
2034 local. This function is called via elf_link_hash_traverse. */
2035
2036 static bfd_boolean
2037 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2038 {
2039 struct elf_info_failed *sinfo;
2040 struct bfd_link_info *info;
2041 const struct elf_backend_data *bed;
2042 struct elf_info_failed eif;
2043 char *p;
2044 bfd_size_type amt;
2045
2046 sinfo = (struct elf_info_failed *) data;
2047 info = sinfo->info;
2048
2049 /* Fix the symbol flags. */
2050 eif.failed = FALSE;
2051 eif.info = info;
2052 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2053 {
2054 if (eif.failed)
2055 sinfo->failed = TRUE;
2056 return FALSE;
2057 }
2058
2059 /* We only need version numbers for symbols defined in regular
2060 objects. */
2061 if (!h->def_regular)
2062 return TRUE;
2063
2064 bed = get_elf_backend_data (info->output_bfd);
2065 p = strchr (h->root.root.string, ELF_VER_CHR);
2066 if (p != NULL && h->verinfo.vertree == NULL)
2067 {
2068 struct bfd_elf_version_tree *t;
2069
2070 ++p;
2071 if (*p == ELF_VER_CHR)
2072 ++p;
2073
2074 /* If there is no version string, we can just return out. */
2075 if (*p == '\0')
2076 return TRUE;
2077
2078 /* Look for the version. If we find it, it is no longer weak. */
2079 for (t = sinfo->info->version_info; t != NULL; t = t->next)
2080 {
2081 if (strcmp (t->name, p) == 0)
2082 {
2083 size_t len;
2084 char *alc;
2085 struct bfd_elf_version_expr *d;
2086
2087 len = p - h->root.root.string;
2088 alc = (char *) bfd_malloc (len);
2089 if (alc == NULL)
2090 {
2091 sinfo->failed = TRUE;
2092 return FALSE;
2093 }
2094 memcpy (alc, h->root.root.string, len - 1);
2095 alc[len - 1] = '\0';
2096 if (alc[len - 2] == ELF_VER_CHR)
2097 alc[len - 2] = '\0';
2098
2099 h->verinfo.vertree = t;
2100 t->used = TRUE;
2101 d = NULL;
2102
2103 if (t->globals.list != NULL)
2104 d = (*t->match) (&t->globals, NULL, alc);
2105
2106 /* See if there is anything to force this symbol to
2107 local scope. */
2108 if (d == NULL && t->locals.list != NULL)
2109 {
2110 d = (*t->match) (&t->locals, NULL, alc);
2111 if (d != NULL
2112 && h->dynindx != -1
2113 && ! info->export_dynamic)
2114 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2115 }
2116
2117 free (alc);
2118 break;
2119 }
2120 }
2121
2122 /* If we are building an application, we need to create a
2123 version node for this version. */
2124 if (t == NULL && bfd_link_executable (info))
2125 {
2126 struct bfd_elf_version_tree **pp;
2127 int version_index;
2128
2129 /* If we aren't going to export this symbol, we don't need
2130 to worry about it. */
2131 if (h->dynindx == -1)
2132 return TRUE;
2133
2134 amt = sizeof *t;
2135 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, amt);
2136 if (t == NULL)
2137 {
2138 sinfo->failed = TRUE;
2139 return FALSE;
2140 }
2141
2142 t->name = p;
2143 t->name_indx = (unsigned int) -1;
2144 t->used = TRUE;
2145
2146 version_index = 1;
2147 /* Don't count anonymous version tag. */
2148 if (sinfo->info->version_info != NULL
2149 && sinfo->info->version_info->vernum == 0)
2150 version_index = 0;
2151 for (pp = &sinfo->info->version_info;
2152 *pp != NULL;
2153 pp = &(*pp)->next)
2154 ++version_index;
2155 t->vernum = version_index;
2156
2157 *pp = t;
2158
2159 h->verinfo.vertree = t;
2160 }
2161 else if (t == NULL)
2162 {
2163 /* We could not find the version for a symbol when
2164 generating a shared archive. Return an error. */
2165 (*_bfd_error_handler)
2166 (_("%B: version node not found for symbol %s"),
2167 info->output_bfd, h->root.root.string);
2168 bfd_set_error (bfd_error_bad_value);
2169 sinfo->failed = TRUE;
2170 return FALSE;
2171 }
2172 }
2173
2174 /* If we don't have a version for this symbol, see if we can find
2175 something. */
2176 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
2177 {
2178 bfd_boolean hide;
2179
2180 h->verinfo.vertree
2181 = bfd_find_version_for_sym (sinfo->info->version_info,
2182 h->root.root.string, &hide);
2183 if (h->verinfo.vertree != NULL && hide)
2184 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2185 }
2186
2187 return TRUE;
2188 }
2189 \f
2190 /* Read and swap the relocs from the section indicated by SHDR. This
2191 may be either a REL or a RELA section. The relocations are
2192 translated into RELA relocations and stored in INTERNAL_RELOCS,
2193 which should have already been allocated to contain enough space.
2194 The EXTERNAL_RELOCS are a buffer where the external form of the
2195 relocations should be stored.
2196
2197 Returns FALSE if something goes wrong. */
2198
2199 static bfd_boolean
2200 elf_link_read_relocs_from_section (bfd *abfd,
2201 asection *sec,
2202 Elf_Internal_Shdr *shdr,
2203 void *external_relocs,
2204 Elf_Internal_Rela *internal_relocs)
2205 {
2206 const struct elf_backend_data *bed;
2207 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2208 const bfd_byte *erela;
2209 const bfd_byte *erelaend;
2210 Elf_Internal_Rela *irela;
2211 Elf_Internal_Shdr *symtab_hdr;
2212 size_t nsyms;
2213
2214 /* Position ourselves at the start of the section. */
2215 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2216 return FALSE;
2217
2218 /* Read the relocations. */
2219 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2220 return FALSE;
2221
2222 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2223 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2224
2225 bed = get_elf_backend_data (abfd);
2226
2227 /* Convert the external relocations to the internal format. */
2228 if (shdr->sh_entsize == bed->s->sizeof_rel)
2229 swap_in = bed->s->swap_reloc_in;
2230 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2231 swap_in = bed->s->swap_reloca_in;
2232 else
2233 {
2234 bfd_set_error (bfd_error_wrong_format);
2235 return FALSE;
2236 }
2237
2238 erela = (const bfd_byte *) external_relocs;
2239 erelaend = erela + shdr->sh_size;
2240 irela = internal_relocs;
2241 while (erela < erelaend)
2242 {
2243 bfd_vma r_symndx;
2244
2245 (*swap_in) (abfd, erela, irela);
2246 r_symndx = ELF32_R_SYM (irela->r_info);
2247 if (bed->s->arch_size == 64)
2248 r_symndx >>= 24;
2249 if (nsyms > 0)
2250 {
2251 if ((size_t) r_symndx >= nsyms)
2252 {
2253 (*_bfd_error_handler)
2254 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2255 " for offset 0x%lx in section `%A'"),
2256 abfd, sec,
2257 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2258 bfd_set_error (bfd_error_bad_value);
2259 return FALSE;
2260 }
2261 }
2262 else if (r_symndx != STN_UNDEF)
2263 {
2264 (*_bfd_error_handler)
2265 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2266 " when the object file has no symbol table"),
2267 abfd, sec,
2268 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2269 bfd_set_error (bfd_error_bad_value);
2270 return FALSE;
2271 }
2272 irela += bed->s->int_rels_per_ext_rel;
2273 erela += shdr->sh_entsize;
2274 }
2275
2276 return TRUE;
2277 }
2278
2279 /* Read and swap the relocs for a section O. They may have been
2280 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2281 not NULL, they are used as buffers to read into. They are known to
2282 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2283 the return value is allocated using either malloc or bfd_alloc,
2284 according to the KEEP_MEMORY argument. If O has two relocation
2285 sections (both REL and RELA relocations), then the REL_HDR
2286 relocations will appear first in INTERNAL_RELOCS, followed by the
2287 RELA_HDR relocations. */
2288
2289 Elf_Internal_Rela *
2290 _bfd_elf_link_read_relocs (bfd *abfd,
2291 asection *o,
2292 void *external_relocs,
2293 Elf_Internal_Rela *internal_relocs,
2294 bfd_boolean keep_memory)
2295 {
2296 void *alloc1 = NULL;
2297 Elf_Internal_Rela *alloc2 = NULL;
2298 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2299 struct bfd_elf_section_data *esdo = elf_section_data (o);
2300 Elf_Internal_Rela *internal_rela_relocs;
2301
2302 if (esdo->relocs != NULL)
2303 return esdo->relocs;
2304
2305 if (o->reloc_count == 0)
2306 return NULL;
2307
2308 if (internal_relocs == NULL)
2309 {
2310 bfd_size_type size;
2311
2312 size = o->reloc_count;
2313 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2314 if (keep_memory)
2315 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2316 else
2317 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2318 if (internal_relocs == NULL)
2319 goto error_return;
2320 }
2321
2322 if (external_relocs == NULL)
2323 {
2324 bfd_size_type size = 0;
2325
2326 if (esdo->rel.hdr)
2327 size += esdo->rel.hdr->sh_size;
2328 if (esdo->rela.hdr)
2329 size += esdo->rela.hdr->sh_size;
2330
2331 alloc1 = bfd_malloc (size);
2332 if (alloc1 == NULL)
2333 goto error_return;
2334 external_relocs = alloc1;
2335 }
2336
2337 internal_rela_relocs = internal_relocs;
2338 if (esdo->rel.hdr)
2339 {
2340 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2341 external_relocs,
2342 internal_relocs))
2343 goto error_return;
2344 external_relocs = (((bfd_byte *) external_relocs)
2345 + esdo->rel.hdr->sh_size);
2346 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2347 * bed->s->int_rels_per_ext_rel);
2348 }
2349
2350 if (esdo->rela.hdr
2351 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2352 external_relocs,
2353 internal_rela_relocs)))
2354 goto error_return;
2355
2356 /* Cache the results for next time, if we can. */
2357 if (keep_memory)
2358 esdo->relocs = internal_relocs;
2359
2360 if (alloc1 != NULL)
2361 free (alloc1);
2362
2363 /* Don't free alloc2, since if it was allocated we are passing it
2364 back (under the name of internal_relocs). */
2365
2366 return internal_relocs;
2367
2368 error_return:
2369 if (alloc1 != NULL)
2370 free (alloc1);
2371 if (alloc2 != NULL)
2372 {
2373 if (keep_memory)
2374 bfd_release (abfd, alloc2);
2375 else
2376 free (alloc2);
2377 }
2378 return NULL;
2379 }
2380
2381 /* Compute the size of, and allocate space for, REL_HDR which is the
2382 section header for a section containing relocations for O. */
2383
2384 static bfd_boolean
2385 _bfd_elf_link_size_reloc_section (bfd *abfd,
2386 struct bfd_elf_section_reloc_data *reldata)
2387 {
2388 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2389
2390 /* That allows us to calculate the size of the section. */
2391 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2392
2393 /* The contents field must last into write_object_contents, so we
2394 allocate it with bfd_alloc rather than malloc. Also since we
2395 cannot be sure that the contents will actually be filled in,
2396 we zero the allocated space. */
2397 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2398 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2399 return FALSE;
2400
2401 if (reldata->hashes == NULL && reldata->count)
2402 {
2403 struct elf_link_hash_entry **p;
2404
2405 p = ((struct elf_link_hash_entry **)
2406 bfd_zmalloc (reldata->count * sizeof (*p)));
2407 if (p == NULL)
2408 return FALSE;
2409
2410 reldata->hashes = p;
2411 }
2412
2413 return TRUE;
2414 }
2415
2416 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2417 originated from the section given by INPUT_REL_HDR) to the
2418 OUTPUT_BFD. */
2419
2420 bfd_boolean
2421 _bfd_elf_link_output_relocs (bfd *output_bfd,
2422 asection *input_section,
2423 Elf_Internal_Shdr *input_rel_hdr,
2424 Elf_Internal_Rela *internal_relocs,
2425 struct elf_link_hash_entry **rel_hash
2426 ATTRIBUTE_UNUSED)
2427 {
2428 Elf_Internal_Rela *irela;
2429 Elf_Internal_Rela *irelaend;
2430 bfd_byte *erel;
2431 struct bfd_elf_section_reloc_data *output_reldata;
2432 asection *output_section;
2433 const struct elf_backend_data *bed;
2434 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2435 struct bfd_elf_section_data *esdo;
2436
2437 output_section = input_section->output_section;
2438
2439 bed = get_elf_backend_data (output_bfd);
2440 esdo = elf_section_data (output_section);
2441 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2442 {
2443 output_reldata = &esdo->rel;
2444 swap_out = bed->s->swap_reloc_out;
2445 }
2446 else if (esdo->rela.hdr
2447 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2448 {
2449 output_reldata = &esdo->rela;
2450 swap_out = bed->s->swap_reloca_out;
2451 }
2452 else
2453 {
2454 (*_bfd_error_handler)
2455 (_("%B: relocation size mismatch in %B section %A"),
2456 output_bfd, input_section->owner, input_section);
2457 bfd_set_error (bfd_error_wrong_format);
2458 return FALSE;
2459 }
2460
2461 erel = output_reldata->hdr->contents;
2462 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2463 irela = internal_relocs;
2464 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2465 * bed->s->int_rels_per_ext_rel);
2466 while (irela < irelaend)
2467 {
2468 (*swap_out) (output_bfd, irela, erel);
2469 irela += bed->s->int_rels_per_ext_rel;
2470 erel += input_rel_hdr->sh_entsize;
2471 }
2472
2473 /* Bump the counter, so that we know where to add the next set of
2474 relocations. */
2475 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2476
2477 return TRUE;
2478 }
2479 \f
2480 /* Make weak undefined symbols in PIE dynamic. */
2481
2482 bfd_boolean
2483 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2484 struct elf_link_hash_entry *h)
2485 {
2486 if (bfd_link_pie (info)
2487 && h->dynindx == -1
2488 && h->root.type == bfd_link_hash_undefweak)
2489 return bfd_elf_link_record_dynamic_symbol (info, h);
2490
2491 return TRUE;
2492 }
2493
2494 /* Fix up the flags for a symbol. This handles various cases which
2495 can only be fixed after all the input files are seen. This is
2496 currently called by both adjust_dynamic_symbol and
2497 assign_sym_version, which is unnecessary but perhaps more robust in
2498 the face of future changes. */
2499
2500 static bfd_boolean
2501 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2502 struct elf_info_failed *eif)
2503 {
2504 const struct elf_backend_data *bed;
2505
2506 /* If this symbol was mentioned in a non-ELF file, try to set
2507 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2508 permit a non-ELF file to correctly refer to a symbol defined in
2509 an ELF dynamic object. */
2510 if (h->non_elf)
2511 {
2512 while (h->root.type == bfd_link_hash_indirect)
2513 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2514
2515 if (h->root.type != bfd_link_hash_defined
2516 && h->root.type != bfd_link_hash_defweak)
2517 {
2518 h->ref_regular = 1;
2519 h->ref_regular_nonweak = 1;
2520 }
2521 else
2522 {
2523 if (h->root.u.def.section->owner != NULL
2524 && (bfd_get_flavour (h->root.u.def.section->owner)
2525 == bfd_target_elf_flavour))
2526 {
2527 h->ref_regular = 1;
2528 h->ref_regular_nonweak = 1;
2529 }
2530 else
2531 h->def_regular = 1;
2532 }
2533
2534 if (h->dynindx == -1
2535 && (h->def_dynamic
2536 || h->ref_dynamic))
2537 {
2538 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2539 {
2540 eif->failed = TRUE;
2541 return FALSE;
2542 }
2543 }
2544 }
2545 else
2546 {
2547 /* Unfortunately, NON_ELF is only correct if the symbol
2548 was first seen in a non-ELF file. Fortunately, if the symbol
2549 was first seen in an ELF file, we're probably OK unless the
2550 symbol was defined in a non-ELF file. Catch that case here.
2551 FIXME: We're still in trouble if the symbol was first seen in
2552 a dynamic object, and then later in a non-ELF regular object. */
2553 if ((h->root.type == bfd_link_hash_defined
2554 || h->root.type == bfd_link_hash_defweak)
2555 && !h->def_regular
2556 && (h->root.u.def.section->owner != NULL
2557 ? (bfd_get_flavour (h->root.u.def.section->owner)
2558 != bfd_target_elf_flavour)
2559 : (bfd_is_abs_section (h->root.u.def.section)
2560 && !h->def_dynamic)))
2561 h->def_regular = 1;
2562 }
2563
2564 /* Backend specific symbol fixup. */
2565 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2566 if (bed->elf_backend_fixup_symbol
2567 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2568 return FALSE;
2569
2570 /* If this is a final link, and the symbol was defined as a common
2571 symbol in a regular object file, and there was no definition in
2572 any dynamic object, then the linker will have allocated space for
2573 the symbol in a common section but the DEF_REGULAR
2574 flag will not have been set. */
2575 if (h->root.type == bfd_link_hash_defined
2576 && !h->def_regular
2577 && h->ref_regular
2578 && !h->def_dynamic
2579 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2580 h->def_regular = 1;
2581
2582 /* If -Bsymbolic was used (which means to bind references to global
2583 symbols to the definition within the shared object), and this
2584 symbol was defined in a regular object, then it actually doesn't
2585 need a PLT entry. Likewise, if the symbol has non-default
2586 visibility. If the symbol has hidden or internal visibility, we
2587 will force it local. */
2588 if (h->needs_plt
2589 && bfd_link_pic (eif->info)
2590 && is_elf_hash_table (eif->info->hash)
2591 && (SYMBOLIC_BIND (eif->info, h)
2592 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2593 && h->def_regular)
2594 {
2595 bfd_boolean force_local;
2596
2597 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2598 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2599 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2600 }
2601
2602 /* If a weak undefined symbol has non-default visibility, we also
2603 hide it from the dynamic linker. */
2604 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2605 && h->root.type == bfd_link_hash_undefweak)
2606 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2607
2608 /* If this is a weak defined symbol in a dynamic object, and we know
2609 the real definition in the dynamic object, copy interesting flags
2610 over to the real definition. */
2611 if (h->u.weakdef != NULL)
2612 {
2613 /* If the real definition is defined by a regular object file,
2614 don't do anything special. See the longer description in
2615 _bfd_elf_adjust_dynamic_symbol, below. */
2616 if (h->u.weakdef->def_regular)
2617 h->u.weakdef = NULL;
2618 else
2619 {
2620 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2621
2622 while (h->root.type == bfd_link_hash_indirect)
2623 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2624
2625 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2626 || h->root.type == bfd_link_hash_defweak);
2627 BFD_ASSERT (weakdef->def_dynamic);
2628 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2629 || weakdef->root.type == bfd_link_hash_defweak);
2630 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2631 }
2632 }
2633
2634 return TRUE;
2635 }
2636
2637 /* Make the backend pick a good value for a dynamic symbol. This is
2638 called via elf_link_hash_traverse, and also calls itself
2639 recursively. */
2640
2641 static bfd_boolean
2642 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2643 {
2644 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2645 bfd *dynobj;
2646 const struct elf_backend_data *bed;
2647
2648 if (! is_elf_hash_table (eif->info->hash))
2649 return FALSE;
2650
2651 /* Ignore indirect symbols. These are added by the versioning code. */
2652 if (h->root.type == bfd_link_hash_indirect)
2653 return TRUE;
2654
2655 /* Fix the symbol flags. */
2656 if (! _bfd_elf_fix_symbol_flags (h, eif))
2657 return FALSE;
2658
2659 /* If this symbol does not require a PLT entry, and it is not
2660 defined by a dynamic object, or is not referenced by a regular
2661 object, ignore it. We do have to handle a weak defined symbol,
2662 even if no regular object refers to it, if we decided to add it
2663 to the dynamic symbol table. FIXME: Do we normally need to worry
2664 about symbols which are defined by one dynamic object and
2665 referenced by another one? */
2666 if (!h->needs_plt
2667 && h->type != STT_GNU_IFUNC
2668 && (h->def_regular
2669 || !h->def_dynamic
2670 || (!h->ref_regular
2671 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2672 {
2673 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2674 return TRUE;
2675 }
2676
2677 /* If we've already adjusted this symbol, don't do it again. This
2678 can happen via a recursive call. */
2679 if (h->dynamic_adjusted)
2680 return TRUE;
2681
2682 /* Don't look at this symbol again. Note that we must set this
2683 after checking the above conditions, because we may look at a
2684 symbol once, decide not to do anything, and then get called
2685 recursively later after REF_REGULAR is set below. */
2686 h->dynamic_adjusted = 1;
2687
2688 /* If this is a weak definition, and we know a real definition, and
2689 the real symbol is not itself defined by a regular object file,
2690 then get a good value for the real definition. We handle the
2691 real symbol first, for the convenience of the backend routine.
2692
2693 Note that there is a confusing case here. If the real definition
2694 is defined by a regular object file, we don't get the real symbol
2695 from the dynamic object, but we do get the weak symbol. If the
2696 processor backend uses a COPY reloc, then if some routine in the
2697 dynamic object changes the real symbol, we will not see that
2698 change in the corresponding weak symbol. This is the way other
2699 ELF linkers work as well, and seems to be a result of the shared
2700 library model.
2701
2702 I will clarify this issue. Most SVR4 shared libraries define the
2703 variable _timezone and define timezone as a weak synonym. The
2704 tzset call changes _timezone. If you write
2705 extern int timezone;
2706 int _timezone = 5;
2707 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2708 you might expect that, since timezone is a synonym for _timezone,
2709 the same number will print both times. However, if the processor
2710 backend uses a COPY reloc, then actually timezone will be copied
2711 into your process image, and, since you define _timezone
2712 yourself, _timezone will not. Thus timezone and _timezone will
2713 wind up at different memory locations. The tzset call will set
2714 _timezone, leaving timezone unchanged. */
2715
2716 if (h->u.weakdef != NULL)
2717 {
2718 /* If we get to this point, there is an implicit reference to
2719 H->U.WEAKDEF by a regular object file via the weak symbol H. */
2720 h->u.weakdef->ref_regular = 1;
2721
2722 /* Ensure that the backend adjust_dynamic_symbol function sees
2723 H->U.WEAKDEF before H by recursively calling ourselves. */
2724 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2725 return FALSE;
2726 }
2727
2728 /* If a symbol has no type and no size and does not require a PLT
2729 entry, then we are probably about to do the wrong thing here: we
2730 are probably going to create a COPY reloc for an empty object.
2731 This case can arise when a shared object is built with assembly
2732 code, and the assembly code fails to set the symbol type. */
2733 if (h->size == 0
2734 && h->type == STT_NOTYPE
2735 && !h->needs_plt)
2736 (*_bfd_error_handler)
2737 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2738 h->root.root.string);
2739
2740 dynobj = elf_hash_table (eif->info)->dynobj;
2741 bed = get_elf_backend_data (dynobj);
2742
2743 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2744 {
2745 eif->failed = TRUE;
2746 return FALSE;
2747 }
2748
2749 return TRUE;
2750 }
2751
2752 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2753 DYNBSS. */
2754
2755 bfd_boolean
2756 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2757 struct elf_link_hash_entry *h,
2758 asection *dynbss)
2759 {
2760 unsigned int power_of_two;
2761 bfd_vma mask;
2762 asection *sec = h->root.u.def.section;
2763
2764 /* The section aligment of definition is the maximum alignment
2765 requirement of symbols defined in the section. Since we don't
2766 know the symbol alignment requirement, we start with the
2767 maximum alignment and check low bits of the symbol address
2768 for the minimum alignment. */
2769 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2770 mask = ((bfd_vma) 1 << power_of_two) - 1;
2771 while ((h->root.u.def.value & mask) != 0)
2772 {
2773 mask >>= 1;
2774 --power_of_two;
2775 }
2776
2777 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2778 dynbss))
2779 {
2780 /* Adjust the section alignment if needed. */
2781 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2782 power_of_two))
2783 return FALSE;
2784 }
2785
2786 /* We make sure that the symbol will be aligned properly. */
2787 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2788
2789 /* Define the symbol as being at this point in DYNBSS. */
2790 h->root.u.def.section = dynbss;
2791 h->root.u.def.value = dynbss->size;
2792
2793 /* Increment the size of DYNBSS to make room for the symbol. */
2794 dynbss->size += h->size;
2795
2796 /* No error if extern_protected_data is true. */
2797 if (h->protected_def
2798 && (!info->extern_protected_data
2799 || (info->extern_protected_data < 0
2800 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
2801 info->callbacks->einfo
2802 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2803 h->root.root.string);
2804
2805 return TRUE;
2806 }
2807
2808 /* Adjust all external symbols pointing into SEC_MERGE sections
2809 to reflect the object merging within the sections. */
2810
2811 static bfd_boolean
2812 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2813 {
2814 asection *sec;
2815
2816 if ((h->root.type == bfd_link_hash_defined
2817 || h->root.type == bfd_link_hash_defweak)
2818 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2819 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
2820 {
2821 bfd *output_bfd = (bfd *) data;
2822
2823 h->root.u.def.value =
2824 _bfd_merged_section_offset (output_bfd,
2825 &h->root.u.def.section,
2826 elf_section_data (sec)->sec_info,
2827 h->root.u.def.value);
2828 }
2829
2830 return TRUE;
2831 }
2832
2833 /* Returns false if the symbol referred to by H should be considered
2834 to resolve local to the current module, and true if it should be
2835 considered to bind dynamically. */
2836
2837 bfd_boolean
2838 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2839 struct bfd_link_info *info,
2840 bfd_boolean not_local_protected)
2841 {
2842 bfd_boolean binding_stays_local_p;
2843 const struct elf_backend_data *bed;
2844 struct elf_link_hash_table *hash_table;
2845
2846 if (h == NULL)
2847 return FALSE;
2848
2849 while (h->root.type == bfd_link_hash_indirect
2850 || h->root.type == bfd_link_hash_warning)
2851 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2852
2853 /* If it was forced local, then clearly it's not dynamic. */
2854 if (h->dynindx == -1)
2855 return FALSE;
2856 if (h->forced_local)
2857 return FALSE;
2858
2859 /* Identify the cases where name binding rules say that a
2860 visible symbol resolves locally. */
2861 binding_stays_local_p = (bfd_link_executable (info)
2862 || SYMBOLIC_BIND (info, h));
2863
2864 switch (ELF_ST_VISIBILITY (h->other))
2865 {
2866 case STV_INTERNAL:
2867 case STV_HIDDEN:
2868 return FALSE;
2869
2870 case STV_PROTECTED:
2871 hash_table = elf_hash_table (info);
2872 if (!is_elf_hash_table (hash_table))
2873 return FALSE;
2874
2875 bed = get_elf_backend_data (hash_table->dynobj);
2876
2877 /* Proper resolution for function pointer equality may require
2878 that these symbols perhaps be resolved dynamically, even though
2879 we should be resolving them to the current module. */
2880 if (!not_local_protected || !bed->is_function_type (h->type))
2881 binding_stays_local_p = TRUE;
2882 break;
2883
2884 default:
2885 break;
2886 }
2887
2888 /* If it isn't defined locally, then clearly it's dynamic. */
2889 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2890 return TRUE;
2891
2892 /* Otherwise, the symbol is dynamic if binding rules don't tell
2893 us that it remains local. */
2894 return !binding_stays_local_p;
2895 }
2896
2897 /* Return true if the symbol referred to by H should be considered
2898 to resolve local to the current module, and false otherwise. Differs
2899 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2900 undefined symbols. The two functions are virtually identical except
2901 for the place where forced_local and dynindx == -1 are tested. If
2902 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2903 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2904 the symbol is local only for defined symbols.
2905 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2906 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2907 treatment of undefined weak symbols. For those that do not make
2908 undefined weak symbols dynamic, both functions may return false. */
2909
2910 bfd_boolean
2911 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2912 struct bfd_link_info *info,
2913 bfd_boolean local_protected)
2914 {
2915 const struct elf_backend_data *bed;
2916 struct elf_link_hash_table *hash_table;
2917
2918 /* If it's a local sym, of course we resolve locally. */
2919 if (h == NULL)
2920 return TRUE;
2921
2922 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2923 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2924 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2925 return TRUE;
2926
2927 /* Common symbols that become definitions don't get the DEF_REGULAR
2928 flag set, so test it first, and don't bail out. */
2929 if (ELF_COMMON_DEF_P (h))
2930 /* Do nothing. */;
2931 /* If we don't have a definition in a regular file, then we can't
2932 resolve locally. The sym is either undefined or dynamic. */
2933 else if (!h->def_regular)
2934 return FALSE;
2935
2936 /* Forced local symbols resolve locally. */
2937 if (h->forced_local)
2938 return TRUE;
2939
2940 /* As do non-dynamic symbols. */
2941 if (h->dynindx == -1)
2942 return TRUE;
2943
2944 /* At this point, we know the symbol is defined and dynamic. In an
2945 executable it must resolve locally, likewise when building symbolic
2946 shared libraries. */
2947 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
2948 return TRUE;
2949
2950 /* Now deal with defined dynamic symbols in shared libraries. Ones
2951 with default visibility might not resolve locally. */
2952 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2953 return FALSE;
2954
2955 hash_table = elf_hash_table (info);
2956 if (!is_elf_hash_table (hash_table))
2957 return TRUE;
2958
2959 bed = get_elf_backend_data (hash_table->dynobj);
2960
2961 /* If extern_protected_data is false, STV_PROTECTED non-function
2962 symbols are local. */
2963 if ((!info->extern_protected_data
2964 || (info->extern_protected_data < 0
2965 && !bed->extern_protected_data))
2966 && !bed->is_function_type (h->type))
2967 return TRUE;
2968
2969 /* Function pointer equality tests may require that STV_PROTECTED
2970 symbols be treated as dynamic symbols. If the address of a
2971 function not defined in an executable is set to that function's
2972 plt entry in the executable, then the address of the function in
2973 a shared library must also be the plt entry in the executable. */
2974 return local_protected;
2975 }
2976
2977 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2978 aligned. Returns the first TLS output section. */
2979
2980 struct bfd_section *
2981 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2982 {
2983 struct bfd_section *sec, *tls;
2984 unsigned int align = 0;
2985
2986 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2987 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2988 break;
2989 tls = sec;
2990
2991 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2992 if (sec->alignment_power > align)
2993 align = sec->alignment_power;
2994
2995 elf_hash_table (info)->tls_sec = tls;
2996
2997 /* Ensure the alignment of the first section is the largest alignment,
2998 so that the tls segment starts aligned. */
2999 if (tls != NULL)
3000 tls->alignment_power = align;
3001
3002 return tls;
3003 }
3004
3005 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3006 static bfd_boolean
3007 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3008 Elf_Internal_Sym *sym)
3009 {
3010 const struct elf_backend_data *bed;
3011
3012 /* Local symbols do not count, but target specific ones might. */
3013 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3014 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3015 return FALSE;
3016
3017 bed = get_elf_backend_data (abfd);
3018 /* Function symbols do not count. */
3019 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3020 return FALSE;
3021
3022 /* If the section is undefined, then so is the symbol. */
3023 if (sym->st_shndx == SHN_UNDEF)
3024 return FALSE;
3025
3026 /* If the symbol is defined in the common section, then
3027 it is a common definition and so does not count. */
3028 if (bed->common_definition (sym))
3029 return FALSE;
3030
3031 /* If the symbol is in a target specific section then we
3032 must rely upon the backend to tell us what it is. */
3033 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3034 /* FIXME - this function is not coded yet:
3035
3036 return _bfd_is_global_symbol_definition (abfd, sym);
3037
3038 Instead for now assume that the definition is not global,
3039 Even if this is wrong, at least the linker will behave
3040 in the same way that it used to do. */
3041 return FALSE;
3042
3043 return TRUE;
3044 }
3045
3046 /* Search the symbol table of the archive element of the archive ABFD
3047 whose archive map contains a mention of SYMDEF, and determine if
3048 the symbol is defined in this element. */
3049 static bfd_boolean
3050 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3051 {
3052 Elf_Internal_Shdr * hdr;
3053 bfd_size_type symcount;
3054 bfd_size_type extsymcount;
3055 bfd_size_type extsymoff;
3056 Elf_Internal_Sym *isymbuf;
3057 Elf_Internal_Sym *isym;
3058 Elf_Internal_Sym *isymend;
3059 bfd_boolean result;
3060
3061 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3062 if (abfd == NULL)
3063 return FALSE;
3064
3065 /* Return FALSE if the object has been claimed by plugin. */
3066 if (abfd->plugin_format == bfd_plugin_yes)
3067 return FALSE;
3068
3069 if (! bfd_check_format (abfd, bfd_object))
3070 return FALSE;
3071
3072 /* Select the appropriate symbol table. */
3073 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3074 hdr = &elf_tdata (abfd)->symtab_hdr;
3075 else
3076 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3077
3078 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3079
3080 /* The sh_info field of the symtab header tells us where the
3081 external symbols start. We don't care about the local symbols. */
3082 if (elf_bad_symtab (abfd))
3083 {
3084 extsymcount = symcount;
3085 extsymoff = 0;
3086 }
3087 else
3088 {
3089 extsymcount = symcount - hdr->sh_info;
3090 extsymoff = hdr->sh_info;
3091 }
3092
3093 if (extsymcount == 0)
3094 return FALSE;
3095
3096 /* Read in the symbol table. */
3097 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3098 NULL, NULL, NULL);
3099 if (isymbuf == NULL)
3100 return FALSE;
3101
3102 /* Scan the symbol table looking for SYMDEF. */
3103 result = FALSE;
3104 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3105 {
3106 const char *name;
3107
3108 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3109 isym->st_name);
3110 if (name == NULL)
3111 break;
3112
3113 if (strcmp (name, symdef->name) == 0)
3114 {
3115 result = is_global_data_symbol_definition (abfd, isym);
3116 break;
3117 }
3118 }
3119
3120 free (isymbuf);
3121
3122 return result;
3123 }
3124 \f
3125 /* Add an entry to the .dynamic table. */
3126
3127 bfd_boolean
3128 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3129 bfd_vma tag,
3130 bfd_vma val)
3131 {
3132 struct elf_link_hash_table *hash_table;
3133 const struct elf_backend_data *bed;
3134 asection *s;
3135 bfd_size_type newsize;
3136 bfd_byte *newcontents;
3137 Elf_Internal_Dyn dyn;
3138
3139 hash_table = elf_hash_table (info);
3140 if (! is_elf_hash_table (hash_table))
3141 return FALSE;
3142
3143 bed = get_elf_backend_data (hash_table->dynobj);
3144 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3145 BFD_ASSERT (s != NULL);
3146
3147 newsize = s->size + bed->s->sizeof_dyn;
3148 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3149 if (newcontents == NULL)
3150 return FALSE;
3151
3152 dyn.d_tag = tag;
3153 dyn.d_un.d_val = val;
3154 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3155
3156 s->size = newsize;
3157 s->contents = newcontents;
3158
3159 return TRUE;
3160 }
3161
3162 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3163 otherwise just check whether one already exists. Returns -1 on error,
3164 1 if a DT_NEEDED tag already exists, and 0 on success. */
3165
3166 static int
3167 elf_add_dt_needed_tag (bfd *abfd,
3168 struct bfd_link_info *info,
3169 const char *soname,
3170 bfd_boolean do_it)
3171 {
3172 struct elf_link_hash_table *hash_table;
3173 bfd_size_type strindex;
3174
3175 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3176 return -1;
3177
3178 hash_table = elf_hash_table (info);
3179 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3180 if (strindex == (bfd_size_type) -1)
3181 return -1;
3182
3183 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3184 {
3185 asection *sdyn;
3186 const struct elf_backend_data *bed;
3187 bfd_byte *extdyn;
3188
3189 bed = get_elf_backend_data (hash_table->dynobj);
3190 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3191 if (sdyn != NULL)
3192 for (extdyn = sdyn->contents;
3193 extdyn < sdyn->contents + sdyn->size;
3194 extdyn += bed->s->sizeof_dyn)
3195 {
3196 Elf_Internal_Dyn dyn;
3197
3198 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3199 if (dyn.d_tag == DT_NEEDED
3200 && dyn.d_un.d_val == strindex)
3201 {
3202 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3203 return 1;
3204 }
3205 }
3206 }
3207
3208 if (do_it)
3209 {
3210 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3211 return -1;
3212
3213 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3214 return -1;
3215 }
3216 else
3217 /* We were just checking for existence of the tag. */
3218 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3219
3220 return 0;
3221 }
3222
3223 static bfd_boolean
3224 on_needed_list (const char *soname, struct bfd_link_needed_list *needed)
3225 {
3226 for (; needed != NULL; needed = needed->next)
3227 if ((elf_dyn_lib_class (needed->by) & DYN_AS_NEEDED) == 0
3228 && strcmp (soname, needed->name) == 0)
3229 return TRUE;
3230
3231 return FALSE;
3232 }
3233
3234 /* Sort symbol by value, section, and size. */
3235 static int
3236 elf_sort_symbol (const void *arg1, const void *arg2)
3237 {
3238 const struct elf_link_hash_entry *h1;
3239 const struct elf_link_hash_entry *h2;
3240 bfd_signed_vma vdiff;
3241
3242 h1 = *(const struct elf_link_hash_entry **) arg1;
3243 h2 = *(const struct elf_link_hash_entry **) arg2;
3244 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3245 if (vdiff != 0)
3246 return vdiff > 0 ? 1 : -1;
3247 else
3248 {
3249 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3250 if (sdiff != 0)
3251 return sdiff > 0 ? 1 : -1;
3252 }
3253 vdiff = h1->size - h2->size;
3254 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3255 }
3256
3257 /* This function is used to adjust offsets into .dynstr for
3258 dynamic symbols. This is called via elf_link_hash_traverse. */
3259
3260 static bfd_boolean
3261 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3262 {
3263 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3264
3265 if (h->dynindx != -1)
3266 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3267 return TRUE;
3268 }
3269
3270 /* Assign string offsets in .dynstr, update all structures referencing
3271 them. */
3272
3273 static bfd_boolean
3274 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3275 {
3276 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3277 struct elf_link_local_dynamic_entry *entry;
3278 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3279 bfd *dynobj = hash_table->dynobj;
3280 asection *sdyn;
3281 bfd_size_type size;
3282 const struct elf_backend_data *bed;
3283 bfd_byte *extdyn;
3284
3285 _bfd_elf_strtab_finalize (dynstr);
3286 size = _bfd_elf_strtab_size (dynstr);
3287
3288 bed = get_elf_backend_data (dynobj);
3289 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3290 BFD_ASSERT (sdyn != NULL);
3291
3292 /* Update all .dynamic entries referencing .dynstr strings. */
3293 for (extdyn = sdyn->contents;
3294 extdyn < sdyn->contents + sdyn->size;
3295 extdyn += bed->s->sizeof_dyn)
3296 {
3297 Elf_Internal_Dyn dyn;
3298
3299 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3300 switch (dyn.d_tag)
3301 {
3302 case DT_STRSZ:
3303 dyn.d_un.d_val = size;
3304 break;
3305 case DT_NEEDED:
3306 case DT_SONAME:
3307 case DT_RPATH:
3308 case DT_RUNPATH:
3309 case DT_FILTER:
3310 case DT_AUXILIARY:
3311 case DT_AUDIT:
3312 case DT_DEPAUDIT:
3313 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3314 break;
3315 default:
3316 continue;
3317 }
3318 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3319 }
3320
3321 /* Now update local dynamic symbols. */
3322 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3323 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3324 entry->isym.st_name);
3325
3326 /* And the rest of dynamic symbols. */
3327 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3328
3329 /* Adjust version definitions. */
3330 if (elf_tdata (output_bfd)->cverdefs)
3331 {
3332 asection *s;
3333 bfd_byte *p;
3334 bfd_size_type i;
3335 Elf_Internal_Verdef def;
3336 Elf_Internal_Verdaux defaux;
3337
3338 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3339 p = s->contents;
3340 do
3341 {
3342 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3343 &def);
3344 p += sizeof (Elf_External_Verdef);
3345 if (def.vd_aux != sizeof (Elf_External_Verdef))
3346 continue;
3347 for (i = 0; i < def.vd_cnt; ++i)
3348 {
3349 _bfd_elf_swap_verdaux_in (output_bfd,
3350 (Elf_External_Verdaux *) p, &defaux);
3351 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3352 defaux.vda_name);
3353 _bfd_elf_swap_verdaux_out (output_bfd,
3354 &defaux, (Elf_External_Verdaux *) p);
3355 p += sizeof (Elf_External_Verdaux);
3356 }
3357 }
3358 while (def.vd_next);
3359 }
3360
3361 /* Adjust version references. */
3362 if (elf_tdata (output_bfd)->verref)
3363 {
3364 asection *s;
3365 bfd_byte *p;
3366 bfd_size_type i;
3367 Elf_Internal_Verneed need;
3368 Elf_Internal_Vernaux needaux;
3369
3370 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3371 p = s->contents;
3372 do
3373 {
3374 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3375 &need);
3376 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3377 _bfd_elf_swap_verneed_out (output_bfd, &need,
3378 (Elf_External_Verneed *) p);
3379 p += sizeof (Elf_External_Verneed);
3380 for (i = 0; i < need.vn_cnt; ++i)
3381 {
3382 _bfd_elf_swap_vernaux_in (output_bfd,
3383 (Elf_External_Vernaux *) p, &needaux);
3384 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3385 needaux.vna_name);
3386 _bfd_elf_swap_vernaux_out (output_bfd,
3387 &needaux,
3388 (Elf_External_Vernaux *) p);
3389 p += sizeof (Elf_External_Vernaux);
3390 }
3391 }
3392 while (need.vn_next);
3393 }
3394
3395 return TRUE;
3396 }
3397 \f
3398 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3399 The default is to only match when the INPUT and OUTPUT are exactly
3400 the same target. */
3401
3402 bfd_boolean
3403 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3404 const bfd_target *output)
3405 {
3406 return input == output;
3407 }
3408
3409 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3410 This version is used when different targets for the same architecture
3411 are virtually identical. */
3412
3413 bfd_boolean
3414 _bfd_elf_relocs_compatible (const bfd_target *input,
3415 const bfd_target *output)
3416 {
3417 const struct elf_backend_data *obed, *ibed;
3418
3419 if (input == output)
3420 return TRUE;
3421
3422 ibed = xvec_get_elf_backend_data (input);
3423 obed = xvec_get_elf_backend_data (output);
3424
3425 if (ibed->arch != obed->arch)
3426 return FALSE;
3427
3428 /* If both backends are using this function, deem them compatible. */
3429 return ibed->relocs_compatible == obed->relocs_compatible;
3430 }
3431
3432 /* Make a special call to the linker "notice" function to tell it that
3433 we are about to handle an as-needed lib, or have finished
3434 processing the lib. */
3435
3436 bfd_boolean
3437 _bfd_elf_notice_as_needed (bfd *ibfd,
3438 struct bfd_link_info *info,
3439 enum notice_asneeded_action act)
3440 {
3441 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3442 }
3443
3444 /* Add symbols from an ELF object file to the linker hash table. */
3445
3446 static bfd_boolean
3447 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3448 {
3449 Elf_Internal_Ehdr *ehdr;
3450 Elf_Internal_Shdr *hdr;
3451 bfd_size_type symcount;
3452 bfd_size_type extsymcount;
3453 bfd_size_type extsymoff;
3454 struct elf_link_hash_entry **sym_hash;
3455 bfd_boolean dynamic;
3456 Elf_External_Versym *extversym = NULL;
3457 Elf_External_Versym *ever;
3458 struct elf_link_hash_entry *weaks;
3459 struct elf_link_hash_entry **nondeflt_vers = NULL;
3460 bfd_size_type nondeflt_vers_cnt = 0;
3461 Elf_Internal_Sym *isymbuf = NULL;
3462 Elf_Internal_Sym *isym;
3463 Elf_Internal_Sym *isymend;
3464 const struct elf_backend_data *bed;
3465 bfd_boolean add_needed;
3466 struct elf_link_hash_table *htab;
3467 bfd_size_type amt;
3468 void *alloc_mark = NULL;
3469 struct bfd_hash_entry **old_table = NULL;
3470 unsigned int old_size = 0;
3471 unsigned int old_count = 0;
3472 void *old_tab = NULL;
3473 void *old_ent;
3474 struct bfd_link_hash_entry *old_undefs = NULL;
3475 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3476 long old_dynsymcount = 0;
3477 bfd_size_type old_dynstr_size = 0;
3478 size_t tabsize = 0;
3479 asection *s;
3480 bfd_boolean just_syms;
3481
3482 htab = elf_hash_table (info);
3483 bed = get_elf_backend_data (abfd);
3484
3485 if ((abfd->flags & DYNAMIC) == 0)
3486 dynamic = FALSE;
3487 else
3488 {
3489 dynamic = TRUE;
3490
3491 /* You can't use -r against a dynamic object. Also, there's no
3492 hope of using a dynamic object which does not exactly match
3493 the format of the output file. */
3494 if (bfd_link_relocatable (info)
3495 || !is_elf_hash_table (htab)
3496 || info->output_bfd->xvec != abfd->xvec)
3497 {
3498 if (bfd_link_relocatable (info))
3499 bfd_set_error (bfd_error_invalid_operation);
3500 else
3501 bfd_set_error (bfd_error_wrong_format);
3502 goto error_return;
3503 }
3504 }
3505
3506 ehdr = elf_elfheader (abfd);
3507 if (info->warn_alternate_em
3508 && bed->elf_machine_code != ehdr->e_machine
3509 && ((bed->elf_machine_alt1 != 0
3510 && ehdr->e_machine == bed->elf_machine_alt1)
3511 || (bed->elf_machine_alt2 != 0
3512 && ehdr->e_machine == bed->elf_machine_alt2)))
3513 info->callbacks->einfo
3514 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3515 ehdr->e_machine, abfd, bed->elf_machine_code);
3516
3517 /* As a GNU extension, any input sections which are named
3518 .gnu.warning.SYMBOL are treated as warning symbols for the given
3519 symbol. This differs from .gnu.warning sections, which generate
3520 warnings when they are included in an output file. */
3521 /* PR 12761: Also generate this warning when building shared libraries. */
3522 for (s = abfd->sections; s != NULL; s = s->next)
3523 {
3524 const char *name;
3525
3526 name = bfd_get_section_name (abfd, s);
3527 if (CONST_STRNEQ (name, ".gnu.warning."))
3528 {
3529 char *msg;
3530 bfd_size_type sz;
3531
3532 name += sizeof ".gnu.warning." - 1;
3533
3534 /* If this is a shared object, then look up the symbol
3535 in the hash table. If it is there, and it is already
3536 been defined, then we will not be using the entry
3537 from this shared object, so we don't need to warn.
3538 FIXME: If we see the definition in a regular object
3539 later on, we will warn, but we shouldn't. The only
3540 fix is to keep track of what warnings we are supposed
3541 to emit, and then handle them all at the end of the
3542 link. */
3543 if (dynamic)
3544 {
3545 struct elf_link_hash_entry *h;
3546
3547 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3548
3549 /* FIXME: What about bfd_link_hash_common? */
3550 if (h != NULL
3551 && (h->root.type == bfd_link_hash_defined
3552 || h->root.type == bfd_link_hash_defweak))
3553 continue;
3554 }
3555
3556 sz = s->size;
3557 msg = (char *) bfd_alloc (abfd, sz + 1);
3558 if (msg == NULL)
3559 goto error_return;
3560
3561 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3562 goto error_return;
3563
3564 msg[sz] = '\0';
3565
3566 if (! (_bfd_generic_link_add_one_symbol
3567 (info, abfd, name, BSF_WARNING, s, 0, msg,
3568 FALSE, bed->collect, NULL)))
3569 goto error_return;
3570
3571 if (bfd_link_executable (info))
3572 {
3573 /* Clobber the section size so that the warning does
3574 not get copied into the output file. */
3575 s->size = 0;
3576
3577 /* Also set SEC_EXCLUDE, so that symbols defined in
3578 the warning section don't get copied to the output. */
3579 s->flags |= SEC_EXCLUDE;
3580 }
3581 }
3582 }
3583
3584 just_syms = ((s = abfd->sections) != NULL
3585 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3586
3587 add_needed = TRUE;
3588 if (! dynamic)
3589 {
3590 /* If we are creating a shared library, create all the dynamic
3591 sections immediately. We need to attach them to something,
3592 so we attach them to this BFD, provided it is the right
3593 format and is not from ld --just-symbols. FIXME: If there
3594 are no input BFD's of the same format as the output, we can't
3595 make a shared library. */
3596 if (!just_syms
3597 && bfd_link_pic (info)
3598 && is_elf_hash_table (htab)
3599 && info->output_bfd->xvec == abfd->xvec
3600 && !htab->dynamic_sections_created)
3601 {
3602 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3603 goto error_return;
3604 }
3605 }
3606 else if (!is_elf_hash_table (htab))
3607 goto error_return;
3608 else
3609 {
3610 const char *soname = NULL;
3611 char *audit = NULL;
3612 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3613 int ret;
3614
3615 /* ld --just-symbols and dynamic objects don't mix very well.
3616 ld shouldn't allow it. */
3617 if (just_syms)
3618 abort ();
3619
3620 /* If this dynamic lib was specified on the command line with
3621 --as-needed in effect, then we don't want to add a DT_NEEDED
3622 tag unless the lib is actually used. Similary for libs brought
3623 in by another lib's DT_NEEDED. When --no-add-needed is used
3624 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3625 any dynamic library in DT_NEEDED tags in the dynamic lib at
3626 all. */
3627 add_needed = (elf_dyn_lib_class (abfd)
3628 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3629 | DYN_NO_NEEDED)) == 0;
3630
3631 s = bfd_get_section_by_name (abfd, ".dynamic");
3632 if (s != NULL)
3633 {
3634 bfd_byte *dynbuf;
3635 bfd_byte *extdyn;
3636 unsigned int elfsec;
3637 unsigned long shlink;
3638
3639 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3640 {
3641 error_free_dyn:
3642 free (dynbuf);
3643 goto error_return;
3644 }
3645
3646 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3647 if (elfsec == SHN_BAD)
3648 goto error_free_dyn;
3649 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3650
3651 for (extdyn = dynbuf;
3652 extdyn < dynbuf + s->size;
3653 extdyn += bed->s->sizeof_dyn)
3654 {
3655 Elf_Internal_Dyn dyn;
3656
3657 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3658 if (dyn.d_tag == DT_SONAME)
3659 {
3660 unsigned int tagv = dyn.d_un.d_val;
3661 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3662 if (soname == NULL)
3663 goto error_free_dyn;
3664 }
3665 if (dyn.d_tag == DT_NEEDED)
3666 {
3667 struct bfd_link_needed_list *n, **pn;
3668 char *fnm, *anm;
3669 unsigned int tagv = dyn.d_un.d_val;
3670
3671 amt = sizeof (struct bfd_link_needed_list);
3672 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3673 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3674 if (n == NULL || fnm == NULL)
3675 goto error_free_dyn;
3676 amt = strlen (fnm) + 1;
3677 anm = (char *) bfd_alloc (abfd, amt);
3678 if (anm == NULL)
3679 goto error_free_dyn;
3680 memcpy (anm, fnm, amt);
3681 n->name = anm;
3682 n->by = abfd;
3683 n->next = NULL;
3684 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3685 ;
3686 *pn = n;
3687 }
3688 if (dyn.d_tag == DT_RUNPATH)
3689 {
3690 struct bfd_link_needed_list *n, **pn;
3691 char *fnm, *anm;
3692 unsigned int tagv = dyn.d_un.d_val;
3693
3694 amt = sizeof (struct bfd_link_needed_list);
3695 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3696 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3697 if (n == NULL || fnm == NULL)
3698 goto error_free_dyn;
3699 amt = strlen (fnm) + 1;
3700 anm = (char *) bfd_alloc (abfd, amt);
3701 if (anm == NULL)
3702 goto error_free_dyn;
3703 memcpy (anm, fnm, amt);
3704 n->name = anm;
3705 n->by = abfd;
3706 n->next = NULL;
3707 for (pn = & runpath;
3708 *pn != NULL;
3709 pn = &(*pn)->next)
3710 ;
3711 *pn = n;
3712 }
3713 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3714 if (!runpath && dyn.d_tag == DT_RPATH)
3715 {
3716 struct bfd_link_needed_list *n, **pn;
3717 char *fnm, *anm;
3718 unsigned int tagv = dyn.d_un.d_val;
3719
3720 amt = sizeof (struct bfd_link_needed_list);
3721 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3722 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3723 if (n == NULL || fnm == NULL)
3724 goto error_free_dyn;
3725 amt = strlen (fnm) + 1;
3726 anm = (char *) bfd_alloc (abfd, amt);
3727 if (anm == NULL)
3728 goto error_free_dyn;
3729 memcpy (anm, fnm, amt);
3730 n->name = anm;
3731 n->by = abfd;
3732 n->next = NULL;
3733 for (pn = & rpath;
3734 *pn != NULL;
3735 pn = &(*pn)->next)
3736 ;
3737 *pn = n;
3738 }
3739 if (dyn.d_tag == DT_AUDIT)
3740 {
3741 unsigned int tagv = dyn.d_un.d_val;
3742 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3743 }
3744 }
3745
3746 free (dynbuf);
3747 }
3748
3749 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3750 frees all more recently bfd_alloc'd blocks as well. */
3751 if (runpath)
3752 rpath = runpath;
3753
3754 if (rpath)
3755 {
3756 struct bfd_link_needed_list **pn;
3757 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3758 ;
3759 *pn = rpath;
3760 }
3761
3762 /* We do not want to include any of the sections in a dynamic
3763 object in the output file. We hack by simply clobbering the
3764 list of sections in the BFD. This could be handled more
3765 cleanly by, say, a new section flag; the existing
3766 SEC_NEVER_LOAD flag is not the one we want, because that one
3767 still implies that the section takes up space in the output
3768 file. */
3769 bfd_section_list_clear (abfd);
3770
3771 /* Find the name to use in a DT_NEEDED entry that refers to this
3772 object. If the object has a DT_SONAME entry, we use it.
3773 Otherwise, if the generic linker stuck something in
3774 elf_dt_name, we use that. Otherwise, we just use the file
3775 name. */
3776 if (soname == NULL || *soname == '\0')
3777 {
3778 soname = elf_dt_name (abfd);
3779 if (soname == NULL || *soname == '\0')
3780 soname = bfd_get_filename (abfd);
3781 }
3782
3783 /* Save the SONAME because sometimes the linker emulation code
3784 will need to know it. */
3785 elf_dt_name (abfd) = soname;
3786
3787 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3788 if (ret < 0)
3789 goto error_return;
3790
3791 /* If we have already included this dynamic object in the
3792 link, just ignore it. There is no reason to include a
3793 particular dynamic object more than once. */
3794 if (ret > 0)
3795 return TRUE;
3796
3797 /* Save the DT_AUDIT entry for the linker emulation code. */
3798 elf_dt_audit (abfd) = audit;
3799 }
3800
3801 /* If this is a dynamic object, we always link against the .dynsym
3802 symbol table, not the .symtab symbol table. The dynamic linker
3803 will only see the .dynsym symbol table, so there is no reason to
3804 look at .symtab for a dynamic object. */
3805
3806 if (! dynamic || elf_dynsymtab (abfd) == 0)
3807 hdr = &elf_tdata (abfd)->symtab_hdr;
3808 else
3809 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3810
3811 symcount = hdr->sh_size / bed->s->sizeof_sym;
3812
3813 /* The sh_info field of the symtab header tells us where the
3814 external symbols start. We don't care about the local symbols at
3815 this point. */
3816 if (elf_bad_symtab (abfd))
3817 {
3818 extsymcount = symcount;
3819 extsymoff = 0;
3820 }
3821 else
3822 {
3823 extsymcount = symcount - hdr->sh_info;
3824 extsymoff = hdr->sh_info;
3825 }
3826
3827 sym_hash = elf_sym_hashes (abfd);
3828 if (extsymcount != 0)
3829 {
3830 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3831 NULL, NULL, NULL);
3832 if (isymbuf == NULL)
3833 goto error_return;
3834
3835 if (sym_hash == NULL)
3836 {
3837 /* We store a pointer to the hash table entry for each
3838 external symbol. */
3839 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3840 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
3841 if (sym_hash == NULL)
3842 goto error_free_sym;
3843 elf_sym_hashes (abfd) = sym_hash;
3844 }
3845 }
3846
3847 if (dynamic)
3848 {
3849 /* Read in any version definitions. */
3850 if (!_bfd_elf_slurp_version_tables (abfd,
3851 info->default_imported_symver))
3852 goto error_free_sym;
3853
3854 /* Read in the symbol versions, but don't bother to convert them
3855 to internal format. */
3856 if (elf_dynversym (abfd) != 0)
3857 {
3858 Elf_Internal_Shdr *versymhdr;
3859
3860 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3861 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
3862 if (extversym == NULL)
3863 goto error_free_sym;
3864 amt = versymhdr->sh_size;
3865 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3866 || bfd_bread (extversym, amt, abfd) != amt)
3867 goto error_free_vers;
3868 }
3869 }
3870
3871 /* If we are loading an as-needed shared lib, save the symbol table
3872 state before we start adding symbols. If the lib turns out
3873 to be unneeded, restore the state. */
3874 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3875 {
3876 unsigned int i;
3877 size_t entsize;
3878
3879 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3880 {
3881 struct bfd_hash_entry *p;
3882 struct elf_link_hash_entry *h;
3883
3884 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3885 {
3886 h = (struct elf_link_hash_entry *) p;
3887 entsize += htab->root.table.entsize;
3888 if (h->root.type == bfd_link_hash_warning)
3889 entsize += htab->root.table.entsize;
3890 }
3891 }
3892
3893 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
3894 old_tab = bfd_malloc (tabsize + entsize);
3895 if (old_tab == NULL)
3896 goto error_free_vers;
3897
3898 /* Remember the current objalloc pointer, so that all mem for
3899 symbols added can later be reclaimed. */
3900 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3901 if (alloc_mark == NULL)
3902 goto error_free_vers;
3903
3904 /* Make a special call to the linker "notice" function to
3905 tell it that we are about to handle an as-needed lib. */
3906 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
3907 goto error_free_vers;
3908
3909 /* Clone the symbol table. Remember some pointers into the
3910 symbol table, and dynamic symbol count. */
3911 old_ent = (char *) old_tab + tabsize;
3912 memcpy (old_tab, htab->root.table.table, tabsize);
3913 old_undefs = htab->root.undefs;
3914 old_undefs_tail = htab->root.undefs_tail;
3915 old_table = htab->root.table.table;
3916 old_size = htab->root.table.size;
3917 old_count = htab->root.table.count;
3918 old_dynsymcount = htab->dynsymcount;
3919 old_dynstr_size = _bfd_elf_strtab_size (htab->dynstr);
3920
3921 for (i = 0; i < htab->root.table.size; i++)
3922 {
3923 struct bfd_hash_entry *p;
3924 struct elf_link_hash_entry *h;
3925
3926 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3927 {
3928 memcpy (old_ent, p, htab->root.table.entsize);
3929 old_ent = (char *) old_ent + htab->root.table.entsize;
3930 h = (struct elf_link_hash_entry *) p;
3931 if (h->root.type == bfd_link_hash_warning)
3932 {
3933 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3934 old_ent = (char *) old_ent + htab->root.table.entsize;
3935 }
3936 }
3937 }
3938 }
3939
3940 weaks = NULL;
3941 ever = extversym != NULL ? extversym + extsymoff : NULL;
3942 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3943 isym < isymend;
3944 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3945 {
3946 int bind;
3947 bfd_vma value;
3948 asection *sec, *new_sec;
3949 flagword flags;
3950 const char *name;
3951 struct elf_link_hash_entry *h;
3952 struct elf_link_hash_entry *hi;
3953 bfd_boolean definition;
3954 bfd_boolean size_change_ok;
3955 bfd_boolean type_change_ok;
3956 bfd_boolean new_weakdef;
3957 bfd_boolean new_weak;
3958 bfd_boolean old_weak;
3959 bfd_boolean override;
3960 bfd_boolean common;
3961 unsigned int old_alignment;
3962 bfd *old_bfd;
3963 bfd_boolean matched;
3964
3965 override = FALSE;
3966
3967 flags = BSF_NO_FLAGS;
3968 sec = NULL;
3969 value = isym->st_value;
3970 common = bed->common_definition (isym);
3971
3972 bind = ELF_ST_BIND (isym->st_info);
3973 switch (bind)
3974 {
3975 case STB_LOCAL:
3976 /* This should be impossible, since ELF requires that all
3977 global symbols follow all local symbols, and that sh_info
3978 point to the first global symbol. Unfortunately, Irix 5
3979 screws this up. */
3980 continue;
3981
3982 case STB_GLOBAL:
3983 if (isym->st_shndx != SHN_UNDEF && !common)
3984 flags = BSF_GLOBAL;
3985 break;
3986
3987 case STB_WEAK:
3988 flags = BSF_WEAK;
3989 break;
3990
3991 case STB_GNU_UNIQUE:
3992 flags = BSF_GNU_UNIQUE;
3993 break;
3994
3995 default:
3996 /* Leave it up to the processor backend. */
3997 break;
3998 }
3999
4000 if (isym->st_shndx == SHN_UNDEF)
4001 sec = bfd_und_section_ptr;
4002 else if (isym->st_shndx == SHN_ABS)
4003 sec = bfd_abs_section_ptr;
4004 else if (isym->st_shndx == SHN_COMMON)
4005 {
4006 sec = bfd_com_section_ptr;
4007 /* What ELF calls the size we call the value. What ELF
4008 calls the value we call the alignment. */
4009 value = isym->st_size;
4010 }
4011 else
4012 {
4013 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4014 if (sec == NULL)
4015 sec = bfd_abs_section_ptr;
4016 else if (discarded_section (sec))
4017 {
4018 /* Symbols from discarded section are undefined. We keep
4019 its visibility. */
4020 sec = bfd_und_section_ptr;
4021 isym->st_shndx = SHN_UNDEF;
4022 }
4023 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4024 value -= sec->vma;
4025 }
4026
4027 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4028 isym->st_name);
4029 if (name == NULL)
4030 goto error_free_vers;
4031
4032 if (isym->st_shndx == SHN_COMMON
4033 && (abfd->flags & BFD_PLUGIN) != 0)
4034 {
4035 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4036
4037 if (xc == NULL)
4038 {
4039 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4040 | SEC_EXCLUDE);
4041 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4042 if (xc == NULL)
4043 goto error_free_vers;
4044 }
4045 sec = xc;
4046 }
4047 else if (isym->st_shndx == SHN_COMMON
4048 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4049 && !bfd_link_relocatable (info))
4050 {
4051 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4052
4053 if (tcomm == NULL)
4054 {
4055 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4056 | SEC_LINKER_CREATED);
4057 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4058 if (tcomm == NULL)
4059 goto error_free_vers;
4060 }
4061 sec = tcomm;
4062 }
4063 else if (bed->elf_add_symbol_hook)
4064 {
4065 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4066 &sec, &value))
4067 goto error_free_vers;
4068
4069 /* The hook function sets the name to NULL if this symbol
4070 should be skipped for some reason. */
4071 if (name == NULL)
4072 continue;
4073 }
4074
4075 /* Sanity check that all possibilities were handled. */
4076 if (sec == NULL)
4077 {
4078 bfd_set_error (bfd_error_bad_value);
4079 goto error_free_vers;
4080 }
4081
4082 /* Silently discard TLS symbols from --just-syms. There's
4083 no way to combine a static TLS block with a new TLS block
4084 for this executable. */
4085 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4086 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4087 continue;
4088
4089 if (bfd_is_und_section (sec)
4090 || bfd_is_com_section (sec))
4091 definition = FALSE;
4092 else
4093 definition = TRUE;
4094
4095 size_change_ok = FALSE;
4096 type_change_ok = bed->type_change_ok;
4097 old_weak = FALSE;
4098 matched = FALSE;
4099 old_alignment = 0;
4100 old_bfd = NULL;
4101 new_sec = sec;
4102
4103 if (is_elf_hash_table (htab))
4104 {
4105 Elf_Internal_Versym iver;
4106 unsigned int vernum = 0;
4107 bfd_boolean skip;
4108
4109 if (ever == NULL)
4110 {
4111 if (info->default_imported_symver)
4112 /* Use the default symbol version created earlier. */
4113 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4114 else
4115 iver.vs_vers = 0;
4116 }
4117 else
4118 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4119
4120 vernum = iver.vs_vers & VERSYM_VERSION;
4121
4122 /* If this is a hidden symbol, or if it is not version
4123 1, we append the version name to the symbol name.
4124 However, we do not modify a non-hidden absolute symbol
4125 if it is not a function, because it might be the version
4126 symbol itself. FIXME: What if it isn't? */
4127 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4128 || (vernum > 1
4129 && (!bfd_is_abs_section (sec)
4130 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4131 {
4132 const char *verstr;
4133 size_t namelen, verlen, newlen;
4134 char *newname, *p;
4135
4136 if (isym->st_shndx != SHN_UNDEF)
4137 {
4138 if (vernum > elf_tdata (abfd)->cverdefs)
4139 verstr = NULL;
4140 else if (vernum > 1)
4141 verstr =
4142 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4143 else
4144 verstr = "";
4145
4146 if (verstr == NULL)
4147 {
4148 (*_bfd_error_handler)
4149 (_("%B: %s: invalid version %u (max %d)"),
4150 abfd, name, vernum,
4151 elf_tdata (abfd)->cverdefs);
4152 bfd_set_error (bfd_error_bad_value);
4153 goto error_free_vers;
4154 }
4155 }
4156 else
4157 {
4158 /* We cannot simply test for the number of
4159 entries in the VERNEED section since the
4160 numbers for the needed versions do not start
4161 at 0. */
4162 Elf_Internal_Verneed *t;
4163
4164 verstr = NULL;
4165 for (t = elf_tdata (abfd)->verref;
4166 t != NULL;
4167 t = t->vn_nextref)
4168 {
4169 Elf_Internal_Vernaux *a;
4170
4171 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4172 {
4173 if (a->vna_other == vernum)
4174 {
4175 verstr = a->vna_nodename;
4176 break;
4177 }
4178 }
4179 if (a != NULL)
4180 break;
4181 }
4182 if (verstr == NULL)
4183 {
4184 (*_bfd_error_handler)
4185 (_("%B: %s: invalid needed version %d"),
4186 abfd, name, vernum);
4187 bfd_set_error (bfd_error_bad_value);
4188 goto error_free_vers;
4189 }
4190 }
4191
4192 namelen = strlen (name);
4193 verlen = strlen (verstr);
4194 newlen = namelen + verlen + 2;
4195 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4196 && isym->st_shndx != SHN_UNDEF)
4197 ++newlen;
4198
4199 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4200 if (newname == NULL)
4201 goto error_free_vers;
4202 memcpy (newname, name, namelen);
4203 p = newname + namelen;
4204 *p++ = ELF_VER_CHR;
4205 /* If this is a defined non-hidden version symbol,
4206 we add another @ to the name. This indicates the
4207 default version of the symbol. */
4208 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4209 && isym->st_shndx != SHN_UNDEF)
4210 *p++ = ELF_VER_CHR;
4211 memcpy (p, verstr, verlen + 1);
4212
4213 name = newname;
4214 }
4215
4216 /* If this symbol has default visibility and the user has
4217 requested we not re-export it, then mark it as hidden. */
4218 if (definition
4219 && !dynamic
4220 && abfd->no_export
4221 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4222 isym->st_other = (STV_HIDDEN
4223 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4224
4225 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4226 sym_hash, &old_bfd, &old_weak,
4227 &old_alignment, &skip, &override,
4228 &type_change_ok, &size_change_ok,
4229 &matched))
4230 goto error_free_vers;
4231
4232 if (skip)
4233 continue;
4234
4235 /* Override a definition only if the new symbol matches the
4236 existing one. */
4237 if (override && matched)
4238 definition = FALSE;
4239
4240 h = *sym_hash;
4241 while (h->root.type == bfd_link_hash_indirect
4242 || h->root.type == bfd_link_hash_warning)
4243 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4244
4245 if (elf_tdata (abfd)->verdef != NULL
4246 && vernum > 1
4247 && definition)
4248 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4249 }
4250
4251 if (! (_bfd_generic_link_add_one_symbol
4252 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4253 (struct bfd_link_hash_entry **) sym_hash)))
4254 goto error_free_vers;
4255
4256 h = *sym_hash;
4257 /* We need to make sure that indirect symbol dynamic flags are
4258 updated. */
4259 hi = h;
4260 while (h->root.type == bfd_link_hash_indirect
4261 || h->root.type == bfd_link_hash_warning)
4262 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4263
4264 *sym_hash = h;
4265
4266 new_weak = (flags & BSF_WEAK) != 0;
4267 new_weakdef = FALSE;
4268 if (dynamic
4269 && definition
4270 && new_weak
4271 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4272 && is_elf_hash_table (htab)
4273 && h->u.weakdef == NULL)
4274 {
4275 /* Keep a list of all weak defined non function symbols from
4276 a dynamic object, using the weakdef field. Later in this
4277 function we will set the weakdef field to the correct
4278 value. We only put non-function symbols from dynamic
4279 objects on this list, because that happens to be the only
4280 time we need to know the normal symbol corresponding to a
4281 weak symbol, and the information is time consuming to
4282 figure out. If the weakdef field is not already NULL,
4283 then this symbol was already defined by some previous
4284 dynamic object, and we will be using that previous
4285 definition anyhow. */
4286
4287 h->u.weakdef = weaks;
4288 weaks = h;
4289 new_weakdef = TRUE;
4290 }
4291
4292 /* Set the alignment of a common symbol. */
4293 if ((common || bfd_is_com_section (sec))
4294 && h->root.type == bfd_link_hash_common)
4295 {
4296 unsigned int align;
4297
4298 if (common)
4299 align = bfd_log2 (isym->st_value);
4300 else
4301 {
4302 /* The new symbol is a common symbol in a shared object.
4303 We need to get the alignment from the section. */
4304 align = new_sec->alignment_power;
4305 }
4306 if (align > old_alignment)
4307 h->root.u.c.p->alignment_power = align;
4308 else
4309 h->root.u.c.p->alignment_power = old_alignment;
4310 }
4311
4312 if (is_elf_hash_table (htab))
4313 {
4314 /* Set a flag in the hash table entry indicating the type of
4315 reference or definition we just found. A dynamic symbol
4316 is one which is referenced or defined by both a regular
4317 object and a shared object. */
4318 bfd_boolean dynsym = FALSE;
4319
4320 /* Plugin symbols aren't normal. Don't set def_regular or
4321 ref_regular for them, or make them dynamic. */
4322 if ((abfd->flags & BFD_PLUGIN) != 0)
4323 ;
4324 else if (! dynamic)
4325 {
4326 if (! definition)
4327 {
4328 h->ref_regular = 1;
4329 if (bind != STB_WEAK)
4330 h->ref_regular_nonweak = 1;
4331 }
4332 else
4333 {
4334 h->def_regular = 1;
4335 if (h->def_dynamic)
4336 {
4337 h->def_dynamic = 0;
4338 h->ref_dynamic = 1;
4339 }
4340 }
4341
4342 /* If the indirect symbol has been forced local, don't
4343 make the real symbol dynamic. */
4344 if ((h == hi || !hi->forced_local)
4345 && (bfd_link_dll (info)
4346 || h->def_dynamic
4347 || h->ref_dynamic))
4348 dynsym = TRUE;
4349 }
4350 else
4351 {
4352 if (! definition)
4353 {
4354 h->ref_dynamic = 1;
4355 hi->ref_dynamic = 1;
4356 }
4357 else
4358 {
4359 h->def_dynamic = 1;
4360 hi->def_dynamic = 1;
4361 }
4362
4363 /* If the indirect symbol has been forced local, don't
4364 make the real symbol dynamic. */
4365 if ((h == hi || !hi->forced_local)
4366 && (h->def_regular
4367 || h->ref_regular
4368 || (h->u.weakdef != NULL
4369 && ! new_weakdef
4370 && h->u.weakdef->dynindx != -1)))
4371 dynsym = TRUE;
4372 }
4373
4374 /* Check to see if we need to add an indirect symbol for
4375 the default name. */
4376 if (definition
4377 || (!override && h->root.type == bfd_link_hash_common))
4378 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4379 sec, value, &old_bfd, &dynsym))
4380 goto error_free_vers;
4381
4382 /* Check the alignment when a common symbol is involved. This
4383 can change when a common symbol is overridden by a normal
4384 definition or a common symbol is ignored due to the old
4385 normal definition. We need to make sure the maximum
4386 alignment is maintained. */
4387 if ((old_alignment || common)
4388 && h->root.type != bfd_link_hash_common)
4389 {
4390 unsigned int common_align;
4391 unsigned int normal_align;
4392 unsigned int symbol_align;
4393 bfd *normal_bfd;
4394 bfd *common_bfd;
4395
4396 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4397 || h->root.type == bfd_link_hash_defweak);
4398
4399 symbol_align = ffs (h->root.u.def.value) - 1;
4400 if (h->root.u.def.section->owner != NULL
4401 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4402 {
4403 normal_align = h->root.u.def.section->alignment_power;
4404 if (normal_align > symbol_align)
4405 normal_align = symbol_align;
4406 }
4407 else
4408 normal_align = symbol_align;
4409
4410 if (old_alignment)
4411 {
4412 common_align = old_alignment;
4413 common_bfd = old_bfd;
4414 normal_bfd = abfd;
4415 }
4416 else
4417 {
4418 common_align = bfd_log2 (isym->st_value);
4419 common_bfd = abfd;
4420 normal_bfd = old_bfd;
4421 }
4422
4423 if (normal_align < common_align)
4424 {
4425 /* PR binutils/2735 */
4426 if (normal_bfd == NULL)
4427 (*_bfd_error_handler)
4428 (_("Warning: alignment %u of common symbol `%s' in %B is"
4429 " greater than the alignment (%u) of its section %A"),
4430 common_bfd, h->root.u.def.section,
4431 1 << common_align, name, 1 << normal_align);
4432 else
4433 (*_bfd_error_handler)
4434 (_("Warning: alignment %u of symbol `%s' in %B"
4435 " is smaller than %u in %B"),
4436 normal_bfd, common_bfd,
4437 1 << normal_align, name, 1 << common_align);
4438 }
4439 }
4440
4441 /* Remember the symbol size if it isn't undefined. */
4442 if (isym->st_size != 0
4443 && isym->st_shndx != SHN_UNDEF
4444 && (definition || h->size == 0))
4445 {
4446 if (h->size != 0
4447 && h->size != isym->st_size
4448 && ! size_change_ok)
4449 (*_bfd_error_handler)
4450 (_("Warning: size of symbol `%s' changed"
4451 " from %lu in %B to %lu in %B"),
4452 old_bfd, abfd,
4453 name, (unsigned long) h->size,
4454 (unsigned long) isym->st_size);
4455
4456 h->size = isym->st_size;
4457 }
4458
4459 /* If this is a common symbol, then we always want H->SIZE
4460 to be the size of the common symbol. The code just above
4461 won't fix the size if a common symbol becomes larger. We
4462 don't warn about a size change here, because that is
4463 covered by --warn-common. Allow changes between different
4464 function types. */
4465 if (h->root.type == bfd_link_hash_common)
4466 h->size = h->root.u.c.size;
4467
4468 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4469 && ((definition && !new_weak)
4470 || (old_weak && h->root.type == bfd_link_hash_common)
4471 || h->type == STT_NOTYPE))
4472 {
4473 unsigned int type = ELF_ST_TYPE (isym->st_info);
4474
4475 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4476 symbol. */
4477 if (type == STT_GNU_IFUNC
4478 && (abfd->flags & DYNAMIC) != 0)
4479 type = STT_FUNC;
4480
4481 if (h->type != type)
4482 {
4483 if (h->type != STT_NOTYPE && ! type_change_ok)
4484 (*_bfd_error_handler)
4485 (_("Warning: type of symbol `%s' changed"
4486 " from %d to %d in %B"),
4487 abfd, name, h->type, type);
4488
4489 h->type = type;
4490 }
4491 }
4492
4493 /* Merge st_other field. */
4494 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4495
4496 /* We don't want to make debug symbol dynamic. */
4497 if (definition
4498 && (sec->flags & SEC_DEBUGGING)
4499 && !bfd_link_relocatable (info))
4500 dynsym = FALSE;
4501
4502 /* Nor should we make plugin symbols dynamic. */
4503 if ((abfd->flags & BFD_PLUGIN) != 0)
4504 dynsym = FALSE;
4505
4506 if (definition)
4507 {
4508 h->target_internal = isym->st_target_internal;
4509 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4510 }
4511
4512 if (definition && !dynamic)
4513 {
4514 char *p = strchr (name, ELF_VER_CHR);
4515 if (p != NULL && p[1] != ELF_VER_CHR)
4516 {
4517 /* Queue non-default versions so that .symver x, x@FOO
4518 aliases can be checked. */
4519 if (!nondeflt_vers)
4520 {
4521 amt = ((isymend - isym + 1)
4522 * sizeof (struct elf_link_hash_entry *));
4523 nondeflt_vers
4524 = (struct elf_link_hash_entry **) bfd_malloc (amt);
4525 if (!nondeflt_vers)
4526 goto error_free_vers;
4527 }
4528 nondeflt_vers[nondeflt_vers_cnt++] = h;
4529 }
4530 }
4531
4532 if (dynsym && h->dynindx == -1)
4533 {
4534 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4535 goto error_free_vers;
4536 if (h->u.weakdef != NULL
4537 && ! new_weakdef
4538 && h->u.weakdef->dynindx == -1)
4539 {
4540 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4541 goto error_free_vers;
4542 }
4543 }
4544 else if (dynsym && h->dynindx != -1)
4545 /* If the symbol already has a dynamic index, but
4546 visibility says it should not be visible, turn it into
4547 a local symbol. */
4548 switch (ELF_ST_VISIBILITY (h->other))
4549 {
4550 case STV_INTERNAL:
4551 case STV_HIDDEN:
4552 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4553 dynsym = FALSE;
4554 break;
4555 }
4556
4557 /* Don't add DT_NEEDED for references from the dummy bfd. */
4558 if (!add_needed
4559 && definition
4560 && ((dynsym
4561 && h->ref_regular_nonweak
4562 && (old_bfd == NULL
4563 || (old_bfd->flags & BFD_PLUGIN) == 0))
4564 || (h->ref_dynamic_nonweak
4565 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4566 && !on_needed_list (elf_dt_name (abfd), htab->needed))))
4567 {
4568 int ret;
4569 const char *soname = elf_dt_name (abfd);
4570
4571 info->callbacks->minfo ("%!", soname, old_bfd,
4572 h->root.root.string);
4573
4574 /* A symbol from a library loaded via DT_NEEDED of some
4575 other library is referenced by a regular object.
4576 Add a DT_NEEDED entry for it. Issue an error if
4577 --no-add-needed is used and the reference was not
4578 a weak one. */
4579 if (old_bfd != NULL
4580 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4581 {
4582 (*_bfd_error_handler)
4583 (_("%B: undefined reference to symbol '%s'"),
4584 old_bfd, name);
4585 bfd_set_error (bfd_error_missing_dso);
4586 goto error_free_vers;
4587 }
4588
4589 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4590 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
4591
4592 add_needed = TRUE;
4593 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4594 if (ret < 0)
4595 goto error_free_vers;
4596
4597 BFD_ASSERT (ret == 0);
4598 }
4599 }
4600 }
4601
4602 if (extversym != NULL)
4603 {
4604 free (extversym);
4605 extversym = NULL;
4606 }
4607
4608 if (isymbuf != NULL)
4609 {
4610 free (isymbuf);
4611 isymbuf = NULL;
4612 }
4613
4614 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4615 {
4616 unsigned int i;
4617
4618 /* Restore the symbol table. */
4619 old_ent = (char *) old_tab + tabsize;
4620 memset (elf_sym_hashes (abfd), 0,
4621 extsymcount * sizeof (struct elf_link_hash_entry *));
4622 htab->root.table.table = old_table;
4623 htab->root.table.size = old_size;
4624 htab->root.table.count = old_count;
4625 memcpy (htab->root.table.table, old_tab, tabsize);
4626 htab->root.undefs = old_undefs;
4627 htab->root.undefs_tail = old_undefs_tail;
4628 _bfd_elf_strtab_restore_size (htab->dynstr, old_dynstr_size);
4629 for (i = 0; i < htab->root.table.size; i++)
4630 {
4631 struct bfd_hash_entry *p;
4632 struct elf_link_hash_entry *h;
4633 bfd_size_type size;
4634 unsigned int alignment_power;
4635
4636 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4637 {
4638 h = (struct elf_link_hash_entry *) p;
4639 if (h->root.type == bfd_link_hash_warning)
4640 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4641 if (h->dynindx >= old_dynsymcount
4642 && h->dynstr_index < old_dynstr_size)
4643 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
4644
4645 /* Preserve the maximum alignment and size for common
4646 symbols even if this dynamic lib isn't on DT_NEEDED
4647 since it can still be loaded at run time by another
4648 dynamic lib. */
4649 if (h->root.type == bfd_link_hash_common)
4650 {
4651 size = h->root.u.c.size;
4652 alignment_power = h->root.u.c.p->alignment_power;
4653 }
4654 else
4655 {
4656 size = 0;
4657 alignment_power = 0;
4658 }
4659 memcpy (p, old_ent, htab->root.table.entsize);
4660 old_ent = (char *) old_ent + htab->root.table.entsize;
4661 h = (struct elf_link_hash_entry *) p;
4662 if (h->root.type == bfd_link_hash_warning)
4663 {
4664 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4665 old_ent = (char *) old_ent + htab->root.table.entsize;
4666 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4667 }
4668 if (h->root.type == bfd_link_hash_common)
4669 {
4670 if (size > h->root.u.c.size)
4671 h->root.u.c.size = size;
4672 if (alignment_power > h->root.u.c.p->alignment_power)
4673 h->root.u.c.p->alignment_power = alignment_power;
4674 }
4675 }
4676 }
4677
4678 /* Make a special call to the linker "notice" function to
4679 tell it that symbols added for crefs may need to be removed. */
4680 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
4681 goto error_free_vers;
4682
4683 free (old_tab);
4684 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4685 alloc_mark);
4686 if (nondeflt_vers != NULL)
4687 free (nondeflt_vers);
4688 return TRUE;
4689 }
4690
4691 if (old_tab != NULL)
4692 {
4693 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
4694 goto error_free_vers;
4695 free (old_tab);
4696 old_tab = NULL;
4697 }
4698
4699 /* Now that all the symbols from this input file are created, if
4700 not performing a relocatable link, handle .symver foo, foo@BAR
4701 such that any relocs against foo become foo@BAR. */
4702 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4703 {
4704 bfd_size_type cnt, symidx;
4705
4706 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4707 {
4708 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4709 char *shortname, *p;
4710
4711 p = strchr (h->root.root.string, ELF_VER_CHR);
4712 if (p == NULL
4713 || (h->root.type != bfd_link_hash_defined
4714 && h->root.type != bfd_link_hash_defweak))
4715 continue;
4716
4717 amt = p - h->root.root.string;
4718 shortname = (char *) bfd_malloc (amt + 1);
4719 if (!shortname)
4720 goto error_free_vers;
4721 memcpy (shortname, h->root.root.string, amt);
4722 shortname[amt] = '\0';
4723
4724 hi = (struct elf_link_hash_entry *)
4725 bfd_link_hash_lookup (&htab->root, shortname,
4726 FALSE, FALSE, FALSE);
4727 if (hi != NULL
4728 && hi->root.type == h->root.type
4729 && hi->root.u.def.value == h->root.u.def.value
4730 && hi->root.u.def.section == h->root.u.def.section)
4731 {
4732 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4733 hi->root.type = bfd_link_hash_indirect;
4734 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4735 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4736 sym_hash = elf_sym_hashes (abfd);
4737 if (sym_hash)
4738 for (symidx = 0; symidx < extsymcount; ++symidx)
4739 if (sym_hash[symidx] == hi)
4740 {
4741 sym_hash[symidx] = h;
4742 break;
4743 }
4744 }
4745 free (shortname);
4746 }
4747 free (nondeflt_vers);
4748 nondeflt_vers = NULL;
4749 }
4750
4751 /* Now set the weakdefs field correctly for all the weak defined
4752 symbols we found. The only way to do this is to search all the
4753 symbols. Since we only need the information for non functions in
4754 dynamic objects, that's the only time we actually put anything on
4755 the list WEAKS. We need this information so that if a regular
4756 object refers to a symbol defined weakly in a dynamic object, the
4757 real symbol in the dynamic object is also put in the dynamic
4758 symbols; we also must arrange for both symbols to point to the
4759 same memory location. We could handle the general case of symbol
4760 aliasing, but a general symbol alias can only be generated in
4761 assembler code, handling it correctly would be very time
4762 consuming, and other ELF linkers don't handle general aliasing
4763 either. */
4764 if (weaks != NULL)
4765 {
4766 struct elf_link_hash_entry **hpp;
4767 struct elf_link_hash_entry **hppend;
4768 struct elf_link_hash_entry **sorted_sym_hash;
4769 struct elf_link_hash_entry *h;
4770 size_t sym_count;
4771
4772 /* Since we have to search the whole symbol list for each weak
4773 defined symbol, search time for N weak defined symbols will be
4774 O(N^2). Binary search will cut it down to O(NlogN). */
4775 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4776 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4777 if (sorted_sym_hash == NULL)
4778 goto error_return;
4779 sym_hash = sorted_sym_hash;
4780 hpp = elf_sym_hashes (abfd);
4781 hppend = hpp + extsymcount;
4782 sym_count = 0;
4783 for (; hpp < hppend; hpp++)
4784 {
4785 h = *hpp;
4786 if (h != NULL
4787 && h->root.type == bfd_link_hash_defined
4788 && !bed->is_function_type (h->type))
4789 {
4790 *sym_hash = h;
4791 sym_hash++;
4792 sym_count++;
4793 }
4794 }
4795
4796 qsort (sorted_sym_hash, sym_count,
4797 sizeof (struct elf_link_hash_entry *),
4798 elf_sort_symbol);
4799
4800 while (weaks != NULL)
4801 {
4802 struct elf_link_hash_entry *hlook;
4803 asection *slook;
4804 bfd_vma vlook;
4805 size_t i, j, idx = 0;
4806
4807 hlook = weaks;
4808 weaks = hlook->u.weakdef;
4809 hlook->u.weakdef = NULL;
4810
4811 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4812 || hlook->root.type == bfd_link_hash_defweak
4813 || hlook->root.type == bfd_link_hash_common
4814 || hlook->root.type == bfd_link_hash_indirect);
4815 slook = hlook->root.u.def.section;
4816 vlook = hlook->root.u.def.value;
4817
4818 i = 0;
4819 j = sym_count;
4820 while (i != j)
4821 {
4822 bfd_signed_vma vdiff;
4823 idx = (i + j) / 2;
4824 h = sorted_sym_hash[idx];
4825 vdiff = vlook - h->root.u.def.value;
4826 if (vdiff < 0)
4827 j = idx;
4828 else if (vdiff > 0)
4829 i = idx + 1;
4830 else
4831 {
4832 long sdiff = slook->id - h->root.u.def.section->id;
4833 if (sdiff < 0)
4834 j = idx;
4835 else if (sdiff > 0)
4836 i = idx + 1;
4837 else
4838 break;
4839 }
4840 }
4841
4842 /* We didn't find a value/section match. */
4843 if (i == j)
4844 continue;
4845
4846 /* With multiple aliases, or when the weak symbol is already
4847 strongly defined, we have multiple matching symbols and
4848 the binary search above may land on any of them. Step
4849 one past the matching symbol(s). */
4850 while (++idx != j)
4851 {
4852 h = sorted_sym_hash[idx];
4853 if (h->root.u.def.section != slook
4854 || h->root.u.def.value != vlook)
4855 break;
4856 }
4857
4858 /* Now look back over the aliases. Since we sorted by size
4859 as well as value and section, we'll choose the one with
4860 the largest size. */
4861 while (idx-- != i)
4862 {
4863 h = sorted_sym_hash[idx];
4864
4865 /* Stop if value or section doesn't match. */
4866 if (h->root.u.def.section != slook
4867 || h->root.u.def.value != vlook)
4868 break;
4869 else if (h != hlook)
4870 {
4871 hlook->u.weakdef = h;
4872
4873 /* If the weak definition is in the list of dynamic
4874 symbols, make sure the real definition is put
4875 there as well. */
4876 if (hlook->dynindx != -1 && h->dynindx == -1)
4877 {
4878 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4879 {
4880 err_free_sym_hash:
4881 free (sorted_sym_hash);
4882 goto error_return;
4883 }
4884 }
4885
4886 /* If the real definition is in the list of dynamic
4887 symbols, make sure the weak definition is put
4888 there as well. If we don't do this, then the
4889 dynamic loader might not merge the entries for the
4890 real definition and the weak definition. */
4891 if (h->dynindx != -1 && hlook->dynindx == -1)
4892 {
4893 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4894 goto err_free_sym_hash;
4895 }
4896 break;
4897 }
4898 }
4899 }
4900
4901 free (sorted_sym_hash);
4902 }
4903
4904 if (bed->check_directives
4905 && !(*bed->check_directives) (abfd, info))
4906 return FALSE;
4907
4908 /* If this object is the same format as the output object, and it is
4909 not a shared library, then let the backend look through the
4910 relocs.
4911
4912 This is required to build global offset table entries and to
4913 arrange for dynamic relocs. It is not required for the
4914 particular common case of linking non PIC code, even when linking
4915 against shared libraries, but unfortunately there is no way of
4916 knowing whether an object file has been compiled PIC or not.
4917 Looking through the relocs is not particularly time consuming.
4918 The problem is that we must either (1) keep the relocs in memory,
4919 which causes the linker to require additional runtime memory or
4920 (2) read the relocs twice from the input file, which wastes time.
4921 This would be a good case for using mmap.
4922
4923 I have no idea how to handle linking PIC code into a file of a
4924 different format. It probably can't be done. */
4925 if (! dynamic
4926 && is_elf_hash_table (htab)
4927 && bed->check_relocs != NULL
4928 && elf_object_id (abfd) == elf_hash_table_id (htab)
4929 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4930 {
4931 asection *o;
4932
4933 for (o = abfd->sections; o != NULL; o = o->next)
4934 {
4935 Elf_Internal_Rela *internal_relocs;
4936 bfd_boolean ok;
4937
4938 if ((o->flags & SEC_RELOC) == 0
4939 || o->reloc_count == 0
4940 || ((info->strip == strip_all || info->strip == strip_debugger)
4941 && (o->flags & SEC_DEBUGGING) != 0)
4942 || bfd_is_abs_section (o->output_section))
4943 continue;
4944
4945 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4946 info->keep_memory);
4947 if (internal_relocs == NULL)
4948 goto error_return;
4949
4950 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4951
4952 if (elf_section_data (o)->relocs != internal_relocs)
4953 free (internal_relocs);
4954
4955 if (! ok)
4956 goto error_return;
4957 }
4958 }
4959
4960 /* If this is a non-traditional link, try to optimize the handling
4961 of the .stab/.stabstr sections. */
4962 if (! dynamic
4963 && ! info->traditional_format
4964 && is_elf_hash_table (htab)
4965 && (info->strip != strip_all && info->strip != strip_debugger))
4966 {
4967 asection *stabstr;
4968
4969 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4970 if (stabstr != NULL)
4971 {
4972 bfd_size_type string_offset = 0;
4973 asection *stab;
4974
4975 for (stab = abfd->sections; stab; stab = stab->next)
4976 if (CONST_STRNEQ (stab->name, ".stab")
4977 && (!stab->name[5] ||
4978 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4979 && (stab->flags & SEC_MERGE) == 0
4980 && !bfd_is_abs_section (stab->output_section))
4981 {
4982 struct bfd_elf_section_data *secdata;
4983
4984 secdata = elf_section_data (stab);
4985 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
4986 stabstr, &secdata->sec_info,
4987 &string_offset))
4988 goto error_return;
4989 if (secdata->sec_info)
4990 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4991 }
4992 }
4993 }
4994
4995 if (is_elf_hash_table (htab) && add_needed)
4996 {
4997 /* Add this bfd to the loaded list. */
4998 struct elf_link_loaded_list *n;
4999
5000 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5001 if (n == NULL)
5002 goto error_return;
5003 n->abfd = abfd;
5004 n->next = htab->loaded;
5005 htab->loaded = n;
5006 }
5007
5008 return TRUE;
5009
5010 error_free_vers:
5011 if (old_tab != NULL)
5012 free (old_tab);
5013 if (nondeflt_vers != NULL)
5014 free (nondeflt_vers);
5015 if (extversym != NULL)
5016 free (extversym);
5017 error_free_sym:
5018 if (isymbuf != NULL)
5019 free (isymbuf);
5020 error_return:
5021 return FALSE;
5022 }
5023
5024 /* Return the linker hash table entry of a symbol that might be
5025 satisfied by an archive symbol. Return -1 on error. */
5026
5027 struct elf_link_hash_entry *
5028 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5029 struct bfd_link_info *info,
5030 const char *name)
5031 {
5032 struct elf_link_hash_entry *h;
5033 char *p, *copy;
5034 size_t len, first;
5035
5036 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5037 if (h != NULL)
5038 return h;
5039
5040 /* If this is a default version (the name contains @@), look up the
5041 symbol again with only one `@' as well as without the version.
5042 The effect is that references to the symbol with and without the
5043 version will be matched by the default symbol in the archive. */
5044
5045 p = strchr (name, ELF_VER_CHR);
5046 if (p == NULL || p[1] != ELF_VER_CHR)
5047 return h;
5048
5049 /* First check with only one `@'. */
5050 len = strlen (name);
5051 copy = (char *) bfd_alloc (abfd, len);
5052 if (copy == NULL)
5053 return (struct elf_link_hash_entry *) 0 - 1;
5054
5055 first = p - name + 1;
5056 memcpy (copy, name, first);
5057 memcpy (copy + first, name + first + 1, len - first);
5058
5059 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5060 if (h == NULL)
5061 {
5062 /* We also need to check references to the symbol without the
5063 version. */
5064 copy[first - 1] = '\0';
5065 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5066 FALSE, FALSE, TRUE);
5067 }
5068
5069 bfd_release (abfd, copy);
5070 return h;
5071 }
5072
5073 /* Add symbols from an ELF archive file to the linker hash table. We
5074 don't use _bfd_generic_link_add_archive_symbols because we need to
5075 handle versioned symbols.
5076
5077 Fortunately, ELF archive handling is simpler than that done by
5078 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5079 oddities. In ELF, if we find a symbol in the archive map, and the
5080 symbol is currently undefined, we know that we must pull in that
5081 object file.
5082
5083 Unfortunately, we do have to make multiple passes over the symbol
5084 table until nothing further is resolved. */
5085
5086 static bfd_boolean
5087 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5088 {
5089 symindex c;
5090 unsigned char *included = NULL;
5091 carsym *symdefs;
5092 bfd_boolean loop;
5093 bfd_size_type amt;
5094 const struct elf_backend_data *bed;
5095 struct elf_link_hash_entry * (*archive_symbol_lookup)
5096 (bfd *, struct bfd_link_info *, const char *);
5097
5098 if (! bfd_has_map (abfd))
5099 {
5100 /* An empty archive is a special case. */
5101 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5102 return TRUE;
5103 bfd_set_error (bfd_error_no_armap);
5104 return FALSE;
5105 }
5106
5107 /* Keep track of all symbols we know to be already defined, and all
5108 files we know to be already included. This is to speed up the
5109 second and subsequent passes. */
5110 c = bfd_ardata (abfd)->symdef_count;
5111 if (c == 0)
5112 return TRUE;
5113 amt = c;
5114 amt *= sizeof (*included);
5115 included = (unsigned char *) bfd_zmalloc (amt);
5116 if (included == NULL)
5117 return FALSE;
5118
5119 symdefs = bfd_ardata (abfd)->symdefs;
5120 bed = get_elf_backend_data (abfd);
5121 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5122
5123 do
5124 {
5125 file_ptr last;
5126 symindex i;
5127 carsym *symdef;
5128 carsym *symdefend;
5129
5130 loop = FALSE;
5131 last = -1;
5132
5133 symdef = symdefs;
5134 symdefend = symdef + c;
5135 for (i = 0; symdef < symdefend; symdef++, i++)
5136 {
5137 struct elf_link_hash_entry *h;
5138 bfd *element;
5139 struct bfd_link_hash_entry *undefs_tail;
5140 symindex mark;
5141
5142 if (included[i])
5143 continue;
5144 if (symdef->file_offset == last)
5145 {
5146 included[i] = TRUE;
5147 continue;
5148 }
5149
5150 h = archive_symbol_lookup (abfd, info, symdef->name);
5151 if (h == (struct elf_link_hash_entry *) 0 - 1)
5152 goto error_return;
5153
5154 if (h == NULL)
5155 continue;
5156
5157 if (h->root.type == bfd_link_hash_common)
5158 {
5159 /* We currently have a common symbol. The archive map contains
5160 a reference to this symbol, so we may want to include it. We
5161 only want to include it however, if this archive element
5162 contains a definition of the symbol, not just another common
5163 declaration of it.
5164
5165 Unfortunately some archivers (including GNU ar) will put
5166 declarations of common symbols into their archive maps, as
5167 well as real definitions, so we cannot just go by the archive
5168 map alone. Instead we must read in the element's symbol
5169 table and check that to see what kind of symbol definition
5170 this is. */
5171 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5172 continue;
5173 }
5174 else if (h->root.type != bfd_link_hash_undefined)
5175 {
5176 if (h->root.type != bfd_link_hash_undefweak)
5177 /* Symbol must be defined. Don't check it again. */
5178 included[i] = TRUE;
5179 continue;
5180 }
5181
5182 /* We need to include this archive member. */
5183 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5184 if (element == NULL)
5185 goto error_return;
5186
5187 if (! bfd_check_format (element, bfd_object))
5188 goto error_return;
5189
5190 undefs_tail = info->hash->undefs_tail;
5191
5192 if (!(*info->callbacks
5193 ->add_archive_element) (info, element, symdef->name, &element))
5194 goto error_return;
5195 if (!bfd_link_add_symbols (element, info))
5196 goto error_return;
5197
5198 /* If there are any new undefined symbols, we need to make
5199 another pass through the archive in order to see whether
5200 they can be defined. FIXME: This isn't perfect, because
5201 common symbols wind up on undefs_tail and because an
5202 undefined symbol which is defined later on in this pass
5203 does not require another pass. This isn't a bug, but it
5204 does make the code less efficient than it could be. */
5205 if (undefs_tail != info->hash->undefs_tail)
5206 loop = TRUE;
5207
5208 /* Look backward to mark all symbols from this object file
5209 which we have already seen in this pass. */
5210 mark = i;
5211 do
5212 {
5213 included[mark] = TRUE;
5214 if (mark == 0)
5215 break;
5216 --mark;
5217 }
5218 while (symdefs[mark].file_offset == symdef->file_offset);
5219
5220 /* We mark subsequent symbols from this object file as we go
5221 on through the loop. */
5222 last = symdef->file_offset;
5223 }
5224 }
5225 while (loop);
5226
5227 free (included);
5228
5229 return TRUE;
5230
5231 error_return:
5232 if (included != NULL)
5233 free (included);
5234 return FALSE;
5235 }
5236
5237 /* Given an ELF BFD, add symbols to the global hash table as
5238 appropriate. */
5239
5240 bfd_boolean
5241 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5242 {
5243 switch (bfd_get_format (abfd))
5244 {
5245 case bfd_object:
5246 return elf_link_add_object_symbols (abfd, info);
5247 case bfd_archive:
5248 return elf_link_add_archive_symbols (abfd, info);
5249 default:
5250 bfd_set_error (bfd_error_wrong_format);
5251 return FALSE;
5252 }
5253 }
5254 \f
5255 struct hash_codes_info
5256 {
5257 unsigned long *hashcodes;
5258 bfd_boolean error;
5259 };
5260
5261 /* This function will be called though elf_link_hash_traverse to store
5262 all hash value of the exported symbols in an array. */
5263
5264 static bfd_boolean
5265 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5266 {
5267 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5268 const char *name;
5269 unsigned long ha;
5270 char *alc = NULL;
5271
5272 /* Ignore indirect symbols. These are added by the versioning code. */
5273 if (h->dynindx == -1)
5274 return TRUE;
5275
5276 name = h->root.root.string;
5277 if (h->versioned >= versioned)
5278 {
5279 char *p = strchr (name, ELF_VER_CHR);
5280 if (p != NULL)
5281 {
5282 alc = (char *) bfd_malloc (p - name + 1);
5283 if (alc == NULL)
5284 {
5285 inf->error = TRUE;
5286 return FALSE;
5287 }
5288 memcpy (alc, name, p - name);
5289 alc[p - name] = '\0';
5290 name = alc;
5291 }
5292 }
5293
5294 /* Compute the hash value. */
5295 ha = bfd_elf_hash (name);
5296
5297 /* Store the found hash value in the array given as the argument. */
5298 *(inf->hashcodes)++ = ha;
5299
5300 /* And store it in the struct so that we can put it in the hash table
5301 later. */
5302 h->u.elf_hash_value = ha;
5303
5304 if (alc != NULL)
5305 free (alc);
5306
5307 return TRUE;
5308 }
5309
5310 struct collect_gnu_hash_codes
5311 {
5312 bfd *output_bfd;
5313 const struct elf_backend_data *bed;
5314 unsigned long int nsyms;
5315 unsigned long int maskbits;
5316 unsigned long int *hashcodes;
5317 unsigned long int *hashval;
5318 unsigned long int *indx;
5319 unsigned long int *counts;
5320 bfd_vma *bitmask;
5321 bfd_byte *contents;
5322 long int min_dynindx;
5323 unsigned long int bucketcount;
5324 unsigned long int symindx;
5325 long int local_indx;
5326 long int shift1, shift2;
5327 unsigned long int mask;
5328 bfd_boolean error;
5329 };
5330
5331 /* This function will be called though elf_link_hash_traverse to store
5332 all hash value of the exported symbols in an array. */
5333
5334 static bfd_boolean
5335 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5336 {
5337 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5338 const char *name;
5339 unsigned long ha;
5340 char *alc = NULL;
5341
5342 /* Ignore indirect symbols. These are added by the versioning code. */
5343 if (h->dynindx == -1)
5344 return TRUE;
5345
5346 /* Ignore also local symbols and undefined symbols. */
5347 if (! (*s->bed->elf_hash_symbol) (h))
5348 return TRUE;
5349
5350 name = h->root.root.string;
5351 if (h->versioned >= versioned)
5352 {
5353 char *p = strchr (name, ELF_VER_CHR);
5354 if (p != NULL)
5355 {
5356 alc = (char *) bfd_malloc (p - name + 1);
5357 if (alc == NULL)
5358 {
5359 s->error = TRUE;
5360 return FALSE;
5361 }
5362 memcpy (alc, name, p - name);
5363 alc[p - name] = '\0';
5364 name = alc;
5365 }
5366 }
5367
5368 /* Compute the hash value. */
5369 ha = bfd_elf_gnu_hash (name);
5370
5371 /* Store the found hash value in the array for compute_bucket_count,
5372 and also for .dynsym reordering purposes. */
5373 s->hashcodes[s->nsyms] = ha;
5374 s->hashval[h->dynindx] = ha;
5375 ++s->nsyms;
5376 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5377 s->min_dynindx = h->dynindx;
5378
5379 if (alc != NULL)
5380 free (alc);
5381
5382 return TRUE;
5383 }
5384
5385 /* This function will be called though elf_link_hash_traverse to do
5386 final dynaminc symbol renumbering. */
5387
5388 static bfd_boolean
5389 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5390 {
5391 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5392 unsigned long int bucket;
5393 unsigned long int val;
5394
5395 /* Ignore indirect symbols. */
5396 if (h->dynindx == -1)
5397 return TRUE;
5398
5399 /* Ignore also local symbols and undefined symbols. */
5400 if (! (*s->bed->elf_hash_symbol) (h))
5401 {
5402 if (h->dynindx >= s->min_dynindx)
5403 h->dynindx = s->local_indx++;
5404 return TRUE;
5405 }
5406
5407 bucket = s->hashval[h->dynindx] % s->bucketcount;
5408 val = (s->hashval[h->dynindx] >> s->shift1)
5409 & ((s->maskbits >> s->shift1) - 1);
5410 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5411 s->bitmask[val]
5412 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5413 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5414 if (s->counts[bucket] == 1)
5415 /* Last element terminates the chain. */
5416 val |= 1;
5417 bfd_put_32 (s->output_bfd, val,
5418 s->contents + (s->indx[bucket] - s->symindx) * 4);
5419 --s->counts[bucket];
5420 h->dynindx = s->indx[bucket]++;
5421 return TRUE;
5422 }
5423
5424 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5425
5426 bfd_boolean
5427 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5428 {
5429 return !(h->forced_local
5430 || h->root.type == bfd_link_hash_undefined
5431 || h->root.type == bfd_link_hash_undefweak
5432 || ((h->root.type == bfd_link_hash_defined
5433 || h->root.type == bfd_link_hash_defweak)
5434 && h->root.u.def.section->output_section == NULL));
5435 }
5436
5437 /* Array used to determine the number of hash table buckets to use
5438 based on the number of symbols there are. If there are fewer than
5439 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5440 fewer than 37 we use 17 buckets, and so forth. We never use more
5441 than 32771 buckets. */
5442
5443 static const size_t elf_buckets[] =
5444 {
5445 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5446 16411, 32771, 0
5447 };
5448
5449 /* Compute bucket count for hashing table. We do not use a static set
5450 of possible tables sizes anymore. Instead we determine for all
5451 possible reasonable sizes of the table the outcome (i.e., the
5452 number of collisions etc) and choose the best solution. The
5453 weighting functions are not too simple to allow the table to grow
5454 without bounds. Instead one of the weighting factors is the size.
5455 Therefore the result is always a good payoff between few collisions
5456 (= short chain lengths) and table size. */
5457 static size_t
5458 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5459 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5460 unsigned long int nsyms,
5461 int gnu_hash)
5462 {
5463 size_t best_size = 0;
5464 unsigned long int i;
5465
5466 /* We have a problem here. The following code to optimize the table
5467 size requires an integer type with more the 32 bits. If
5468 BFD_HOST_U_64_BIT is set we know about such a type. */
5469 #ifdef BFD_HOST_U_64_BIT
5470 if (info->optimize)
5471 {
5472 size_t minsize;
5473 size_t maxsize;
5474 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5475 bfd *dynobj = elf_hash_table (info)->dynobj;
5476 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5477 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5478 unsigned long int *counts;
5479 bfd_size_type amt;
5480 unsigned int no_improvement_count = 0;
5481
5482 /* Possible optimization parameters: if we have NSYMS symbols we say
5483 that the hashing table must at least have NSYMS/4 and at most
5484 2*NSYMS buckets. */
5485 minsize = nsyms / 4;
5486 if (minsize == 0)
5487 minsize = 1;
5488 best_size = maxsize = nsyms * 2;
5489 if (gnu_hash)
5490 {
5491 if (minsize < 2)
5492 minsize = 2;
5493 if ((best_size & 31) == 0)
5494 ++best_size;
5495 }
5496
5497 /* Create array where we count the collisions in. We must use bfd_malloc
5498 since the size could be large. */
5499 amt = maxsize;
5500 amt *= sizeof (unsigned long int);
5501 counts = (unsigned long int *) bfd_malloc (amt);
5502 if (counts == NULL)
5503 return 0;
5504
5505 /* Compute the "optimal" size for the hash table. The criteria is a
5506 minimal chain length. The minor criteria is (of course) the size
5507 of the table. */
5508 for (i = minsize; i < maxsize; ++i)
5509 {
5510 /* Walk through the array of hashcodes and count the collisions. */
5511 BFD_HOST_U_64_BIT max;
5512 unsigned long int j;
5513 unsigned long int fact;
5514
5515 if (gnu_hash && (i & 31) == 0)
5516 continue;
5517
5518 memset (counts, '\0', i * sizeof (unsigned long int));
5519
5520 /* Determine how often each hash bucket is used. */
5521 for (j = 0; j < nsyms; ++j)
5522 ++counts[hashcodes[j] % i];
5523
5524 /* For the weight function we need some information about the
5525 pagesize on the target. This is information need not be 100%
5526 accurate. Since this information is not available (so far) we
5527 define it here to a reasonable default value. If it is crucial
5528 to have a better value some day simply define this value. */
5529 # ifndef BFD_TARGET_PAGESIZE
5530 # define BFD_TARGET_PAGESIZE (4096)
5531 # endif
5532
5533 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5534 and the chains. */
5535 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5536
5537 # if 1
5538 /* Variant 1: optimize for short chains. We add the squares
5539 of all the chain lengths (which favors many small chain
5540 over a few long chains). */
5541 for (j = 0; j < i; ++j)
5542 max += counts[j] * counts[j];
5543
5544 /* This adds penalties for the overall size of the table. */
5545 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5546 max *= fact * fact;
5547 # else
5548 /* Variant 2: Optimize a lot more for small table. Here we
5549 also add squares of the size but we also add penalties for
5550 empty slots (the +1 term). */
5551 for (j = 0; j < i; ++j)
5552 max += (1 + counts[j]) * (1 + counts[j]);
5553
5554 /* The overall size of the table is considered, but not as
5555 strong as in variant 1, where it is squared. */
5556 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5557 max *= fact;
5558 # endif
5559
5560 /* Compare with current best results. */
5561 if (max < best_chlen)
5562 {
5563 best_chlen = max;
5564 best_size = i;
5565 no_improvement_count = 0;
5566 }
5567 /* PR 11843: Avoid futile long searches for the best bucket size
5568 when there are a large number of symbols. */
5569 else if (++no_improvement_count == 100)
5570 break;
5571 }
5572
5573 free (counts);
5574 }
5575 else
5576 #endif /* defined (BFD_HOST_U_64_BIT) */
5577 {
5578 /* This is the fallback solution if no 64bit type is available or if we
5579 are not supposed to spend much time on optimizations. We select the
5580 bucket count using a fixed set of numbers. */
5581 for (i = 0; elf_buckets[i] != 0; i++)
5582 {
5583 best_size = elf_buckets[i];
5584 if (nsyms < elf_buckets[i + 1])
5585 break;
5586 }
5587 if (gnu_hash && best_size < 2)
5588 best_size = 2;
5589 }
5590
5591 return best_size;
5592 }
5593
5594 /* Size any SHT_GROUP section for ld -r. */
5595
5596 bfd_boolean
5597 _bfd_elf_size_group_sections (struct bfd_link_info *info)
5598 {
5599 bfd *ibfd;
5600
5601 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
5602 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5603 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5604 return FALSE;
5605 return TRUE;
5606 }
5607
5608 /* Set a default stack segment size. The value in INFO wins. If it
5609 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5610 undefined it is initialized. */
5611
5612 bfd_boolean
5613 bfd_elf_stack_segment_size (bfd *output_bfd,
5614 struct bfd_link_info *info,
5615 const char *legacy_symbol,
5616 bfd_vma default_size)
5617 {
5618 struct elf_link_hash_entry *h = NULL;
5619
5620 /* Look for legacy symbol. */
5621 if (legacy_symbol)
5622 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5623 FALSE, FALSE, FALSE);
5624 if (h && (h->root.type == bfd_link_hash_defined
5625 || h->root.type == bfd_link_hash_defweak)
5626 && h->def_regular
5627 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5628 {
5629 /* The symbol has no type if specified on the command line. */
5630 h->type = STT_OBJECT;
5631 if (info->stacksize)
5632 (*_bfd_error_handler) (_("%B: stack size specified and %s set"),
5633 output_bfd, legacy_symbol);
5634 else if (h->root.u.def.section != bfd_abs_section_ptr)
5635 (*_bfd_error_handler) (_("%B: %s not absolute"),
5636 output_bfd, legacy_symbol);
5637 else
5638 info->stacksize = h->root.u.def.value;
5639 }
5640
5641 if (!info->stacksize)
5642 /* If the user didn't set a size, or explicitly inhibit the
5643 size, set it now. */
5644 info->stacksize = default_size;
5645
5646 /* Provide the legacy symbol, if it is referenced. */
5647 if (h && (h->root.type == bfd_link_hash_undefined
5648 || h->root.type == bfd_link_hash_undefweak))
5649 {
5650 struct bfd_link_hash_entry *bh = NULL;
5651
5652 if (!(_bfd_generic_link_add_one_symbol
5653 (info, output_bfd, legacy_symbol,
5654 BSF_GLOBAL, bfd_abs_section_ptr,
5655 info->stacksize >= 0 ? info->stacksize : 0,
5656 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5657 return FALSE;
5658
5659 h = (struct elf_link_hash_entry *) bh;
5660 h->def_regular = 1;
5661 h->type = STT_OBJECT;
5662 }
5663
5664 return TRUE;
5665 }
5666
5667 /* Set up the sizes and contents of the ELF dynamic sections. This is
5668 called by the ELF linker emulation before_allocation routine. We
5669 must set the sizes of the sections before the linker sets the
5670 addresses of the various sections. */
5671
5672 bfd_boolean
5673 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5674 const char *soname,
5675 const char *rpath,
5676 const char *filter_shlib,
5677 const char *audit,
5678 const char *depaudit,
5679 const char * const *auxiliary_filters,
5680 struct bfd_link_info *info,
5681 asection **sinterpptr)
5682 {
5683 bfd_size_type soname_indx;
5684 bfd *dynobj;
5685 const struct elf_backend_data *bed;
5686 struct elf_info_failed asvinfo;
5687
5688 *sinterpptr = NULL;
5689
5690 soname_indx = (bfd_size_type) -1;
5691
5692 if (!is_elf_hash_table (info->hash))
5693 return TRUE;
5694
5695 bed = get_elf_backend_data (output_bfd);
5696
5697 /* Any syms created from now on start with -1 in
5698 got.refcount/offset and plt.refcount/offset. */
5699 elf_hash_table (info)->init_got_refcount
5700 = elf_hash_table (info)->init_got_offset;
5701 elf_hash_table (info)->init_plt_refcount
5702 = elf_hash_table (info)->init_plt_offset;
5703
5704 if (bfd_link_relocatable (info)
5705 && !_bfd_elf_size_group_sections (info))
5706 return FALSE;
5707
5708 /* The backend may have to create some sections regardless of whether
5709 we're dynamic or not. */
5710 if (bed->elf_backend_always_size_sections
5711 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5712 return FALSE;
5713
5714 /* Determine any GNU_STACK segment requirements, after the backend
5715 has had a chance to set a default segment size. */
5716 if (info->execstack)
5717 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
5718 else if (info->noexecstack)
5719 elf_stack_flags (output_bfd) = PF_R | PF_W;
5720 else
5721 {
5722 bfd *inputobj;
5723 asection *notesec = NULL;
5724 int exec = 0;
5725
5726 for (inputobj = info->input_bfds;
5727 inputobj;
5728 inputobj = inputobj->link.next)
5729 {
5730 asection *s;
5731
5732 if (inputobj->flags
5733 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5734 continue;
5735 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5736 if (s)
5737 {
5738 if (s->flags & SEC_CODE)
5739 exec = PF_X;
5740 notesec = s;
5741 }
5742 else if (bed->default_execstack)
5743 exec = PF_X;
5744 }
5745 if (notesec || info->stacksize > 0)
5746 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
5747 if (notesec && exec && bfd_link_relocatable (info)
5748 && notesec->output_section != bfd_abs_section_ptr)
5749 notesec->output_section->flags |= SEC_CODE;
5750 }
5751
5752 dynobj = elf_hash_table (info)->dynobj;
5753
5754 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5755 {
5756 struct elf_info_failed eif;
5757 struct elf_link_hash_entry *h;
5758 asection *dynstr;
5759 struct bfd_elf_version_tree *t;
5760 struct bfd_elf_version_expr *d;
5761 asection *s;
5762 bfd_boolean all_defined;
5763
5764 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
5765 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info));
5766
5767 if (soname != NULL)
5768 {
5769 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5770 soname, TRUE);
5771 if (soname_indx == (bfd_size_type) -1
5772 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5773 return FALSE;
5774 }
5775
5776 if (info->symbolic)
5777 {
5778 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5779 return FALSE;
5780 info->flags |= DF_SYMBOLIC;
5781 }
5782
5783 if (rpath != NULL)
5784 {
5785 bfd_size_type indx;
5786 bfd_vma tag;
5787
5788 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5789 TRUE);
5790 if (indx == (bfd_size_type) -1)
5791 return FALSE;
5792
5793 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
5794 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
5795 return FALSE;
5796 }
5797
5798 if (filter_shlib != NULL)
5799 {
5800 bfd_size_type indx;
5801
5802 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5803 filter_shlib, TRUE);
5804 if (indx == (bfd_size_type) -1
5805 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5806 return FALSE;
5807 }
5808
5809 if (auxiliary_filters != NULL)
5810 {
5811 const char * const *p;
5812
5813 for (p = auxiliary_filters; *p != NULL; p++)
5814 {
5815 bfd_size_type indx;
5816
5817 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5818 *p, TRUE);
5819 if (indx == (bfd_size_type) -1
5820 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5821 return FALSE;
5822 }
5823 }
5824
5825 if (audit != NULL)
5826 {
5827 bfd_size_type indx;
5828
5829 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5830 TRUE);
5831 if (indx == (bfd_size_type) -1
5832 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5833 return FALSE;
5834 }
5835
5836 if (depaudit != NULL)
5837 {
5838 bfd_size_type indx;
5839
5840 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
5841 TRUE);
5842 if (indx == (bfd_size_type) -1
5843 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
5844 return FALSE;
5845 }
5846
5847 eif.info = info;
5848 eif.failed = FALSE;
5849
5850 /* If we are supposed to export all symbols into the dynamic symbol
5851 table (this is not the normal case), then do so. */
5852 if (info->export_dynamic
5853 || (bfd_link_executable (info) && info->dynamic))
5854 {
5855 elf_link_hash_traverse (elf_hash_table (info),
5856 _bfd_elf_export_symbol,
5857 &eif);
5858 if (eif.failed)
5859 return FALSE;
5860 }
5861
5862 /* Make all global versions with definition. */
5863 for (t = info->version_info; t != NULL; t = t->next)
5864 for (d = t->globals.list; d != NULL; d = d->next)
5865 if (!d->symver && d->literal)
5866 {
5867 const char *verstr, *name;
5868 size_t namelen, verlen, newlen;
5869 char *newname, *p, leading_char;
5870 struct elf_link_hash_entry *newh;
5871
5872 leading_char = bfd_get_symbol_leading_char (output_bfd);
5873 name = d->pattern;
5874 namelen = strlen (name) + (leading_char != '\0');
5875 verstr = t->name;
5876 verlen = strlen (verstr);
5877 newlen = namelen + verlen + 3;
5878
5879 newname = (char *) bfd_malloc (newlen);
5880 if (newname == NULL)
5881 return FALSE;
5882 newname[0] = leading_char;
5883 memcpy (newname + (leading_char != '\0'), name, namelen);
5884
5885 /* Check the hidden versioned definition. */
5886 p = newname + namelen;
5887 *p++ = ELF_VER_CHR;
5888 memcpy (p, verstr, verlen + 1);
5889 newh = elf_link_hash_lookup (elf_hash_table (info),
5890 newname, FALSE, FALSE,
5891 FALSE);
5892 if (newh == NULL
5893 || (newh->root.type != bfd_link_hash_defined
5894 && newh->root.type != bfd_link_hash_defweak))
5895 {
5896 /* Check the default versioned definition. */
5897 *p++ = ELF_VER_CHR;
5898 memcpy (p, verstr, verlen + 1);
5899 newh = elf_link_hash_lookup (elf_hash_table (info),
5900 newname, FALSE, FALSE,
5901 FALSE);
5902 }
5903 free (newname);
5904
5905 /* Mark this version if there is a definition and it is
5906 not defined in a shared object. */
5907 if (newh != NULL
5908 && !newh->def_dynamic
5909 && (newh->root.type == bfd_link_hash_defined
5910 || newh->root.type == bfd_link_hash_defweak))
5911 d->symver = 1;
5912 }
5913
5914 /* Attach all the symbols to their version information. */
5915 asvinfo.info = info;
5916 asvinfo.failed = FALSE;
5917
5918 elf_link_hash_traverse (elf_hash_table (info),
5919 _bfd_elf_link_assign_sym_version,
5920 &asvinfo);
5921 if (asvinfo.failed)
5922 return FALSE;
5923
5924 if (!info->allow_undefined_version)
5925 {
5926 /* Check if all global versions have a definition. */
5927 all_defined = TRUE;
5928 for (t = info->version_info; t != NULL; t = t->next)
5929 for (d = t->globals.list; d != NULL; d = d->next)
5930 if (d->literal && !d->symver && !d->script)
5931 {
5932 (*_bfd_error_handler)
5933 (_("%s: undefined version: %s"),
5934 d->pattern, t->name);
5935 all_defined = FALSE;
5936 }
5937
5938 if (!all_defined)
5939 {
5940 bfd_set_error (bfd_error_bad_value);
5941 return FALSE;
5942 }
5943 }
5944
5945 /* Find all symbols which were defined in a dynamic object and make
5946 the backend pick a reasonable value for them. */
5947 elf_link_hash_traverse (elf_hash_table (info),
5948 _bfd_elf_adjust_dynamic_symbol,
5949 &eif);
5950 if (eif.failed)
5951 return FALSE;
5952
5953 /* Add some entries to the .dynamic section. We fill in some of the
5954 values later, in bfd_elf_final_link, but we must add the entries
5955 now so that we know the final size of the .dynamic section. */
5956
5957 /* If there are initialization and/or finalization functions to
5958 call then add the corresponding DT_INIT/DT_FINI entries. */
5959 h = (info->init_function
5960 ? elf_link_hash_lookup (elf_hash_table (info),
5961 info->init_function, FALSE,
5962 FALSE, FALSE)
5963 : NULL);
5964 if (h != NULL
5965 && (h->ref_regular
5966 || h->def_regular))
5967 {
5968 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5969 return FALSE;
5970 }
5971 h = (info->fini_function
5972 ? elf_link_hash_lookup (elf_hash_table (info),
5973 info->fini_function, FALSE,
5974 FALSE, FALSE)
5975 : NULL);
5976 if (h != NULL
5977 && (h->ref_regular
5978 || h->def_regular))
5979 {
5980 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5981 return FALSE;
5982 }
5983
5984 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5985 if (s != NULL && s->linker_has_input)
5986 {
5987 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5988 if (! bfd_link_executable (info))
5989 {
5990 bfd *sub;
5991 asection *o;
5992
5993 for (sub = info->input_bfds; sub != NULL;
5994 sub = sub->link.next)
5995 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
5996 for (o = sub->sections; o != NULL; o = o->next)
5997 if (elf_section_data (o)->this_hdr.sh_type
5998 == SHT_PREINIT_ARRAY)
5999 {
6000 (*_bfd_error_handler)
6001 (_("%B: .preinit_array section is not allowed in DSO"),
6002 sub);
6003 break;
6004 }
6005
6006 bfd_set_error (bfd_error_nonrepresentable_section);
6007 return FALSE;
6008 }
6009
6010 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6011 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6012 return FALSE;
6013 }
6014 s = bfd_get_section_by_name (output_bfd, ".init_array");
6015 if (s != NULL && s->linker_has_input)
6016 {
6017 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6018 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6019 return FALSE;
6020 }
6021 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6022 if (s != NULL && s->linker_has_input)
6023 {
6024 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6025 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6026 return FALSE;
6027 }
6028
6029 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6030 /* If .dynstr is excluded from the link, we don't want any of
6031 these tags. Strictly, we should be checking each section
6032 individually; This quick check covers for the case where
6033 someone does a /DISCARD/ : { *(*) }. */
6034 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6035 {
6036 bfd_size_type strsize;
6037
6038 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6039 if ((info->emit_hash
6040 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6041 || (info->emit_gnu_hash
6042 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6043 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6044 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6045 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6046 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6047 bed->s->sizeof_sym))
6048 return FALSE;
6049 }
6050 }
6051
6052 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6053 return FALSE;
6054
6055 /* The backend must work out the sizes of all the other dynamic
6056 sections. */
6057 if (dynobj != NULL
6058 && bed->elf_backend_size_dynamic_sections != NULL
6059 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6060 return FALSE;
6061
6062 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6063 {
6064 unsigned long section_sym_count;
6065 struct bfd_elf_version_tree *verdefs;
6066 asection *s;
6067
6068 /* Set up the version definition section. */
6069 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6070 BFD_ASSERT (s != NULL);
6071
6072 /* We may have created additional version definitions if we are
6073 just linking a regular application. */
6074 verdefs = info->version_info;
6075
6076 /* Skip anonymous version tag. */
6077 if (verdefs != NULL && verdefs->vernum == 0)
6078 verdefs = verdefs->next;
6079
6080 if (verdefs == NULL && !info->create_default_symver)
6081 s->flags |= SEC_EXCLUDE;
6082 else
6083 {
6084 unsigned int cdefs;
6085 bfd_size_type size;
6086 struct bfd_elf_version_tree *t;
6087 bfd_byte *p;
6088 Elf_Internal_Verdef def;
6089 Elf_Internal_Verdaux defaux;
6090 struct bfd_link_hash_entry *bh;
6091 struct elf_link_hash_entry *h;
6092 const char *name;
6093
6094 cdefs = 0;
6095 size = 0;
6096
6097 /* Make space for the base version. */
6098 size += sizeof (Elf_External_Verdef);
6099 size += sizeof (Elf_External_Verdaux);
6100 ++cdefs;
6101
6102 /* Make space for the default version. */
6103 if (info->create_default_symver)
6104 {
6105 size += sizeof (Elf_External_Verdef);
6106 ++cdefs;
6107 }
6108
6109 for (t = verdefs; t != NULL; t = t->next)
6110 {
6111 struct bfd_elf_version_deps *n;
6112
6113 /* Don't emit base version twice. */
6114 if (t->vernum == 0)
6115 continue;
6116
6117 size += sizeof (Elf_External_Verdef);
6118 size += sizeof (Elf_External_Verdaux);
6119 ++cdefs;
6120
6121 for (n = t->deps; n != NULL; n = n->next)
6122 size += sizeof (Elf_External_Verdaux);
6123 }
6124
6125 s->size = size;
6126 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6127 if (s->contents == NULL && s->size != 0)
6128 return FALSE;
6129
6130 /* Fill in the version definition section. */
6131
6132 p = s->contents;
6133
6134 def.vd_version = VER_DEF_CURRENT;
6135 def.vd_flags = VER_FLG_BASE;
6136 def.vd_ndx = 1;
6137 def.vd_cnt = 1;
6138 if (info->create_default_symver)
6139 {
6140 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6141 def.vd_next = sizeof (Elf_External_Verdef);
6142 }
6143 else
6144 {
6145 def.vd_aux = sizeof (Elf_External_Verdef);
6146 def.vd_next = (sizeof (Elf_External_Verdef)
6147 + sizeof (Elf_External_Verdaux));
6148 }
6149
6150 if (soname_indx != (bfd_size_type) -1)
6151 {
6152 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6153 soname_indx);
6154 def.vd_hash = bfd_elf_hash (soname);
6155 defaux.vda_name = soname_indx;
6156 name = soname;
6157 }
6158 else
6159 {
6160 bfd_size_type indx;
6161
6162 name = lbasename (output_bfd->filename);
6163 def.vd_hash = bfd_elf_hash (name);
6164 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6165 name, FALSE);
6166 if (indx == (bfd_size_type) -1)
6167 return FALSE;
6168 defaux.vda_name = indx;
6169 }
6170 defaux.vda_next = 0;
6171
6172 _bfd_elf_swap_verdef_out (output_bfd, &def,
6173 (Elf_External_Verdef *) p);
6174 p += sizeof (Elf_External_Verdef);
6175 if (info->create_default_symver)
6176 {
6177 /* Add a symbol representing this version. */
6178 bh = NULL;
6179 if (! (_bfd_generic_link_add_one_symbol
6180 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6181 0, NULL, FALSE,
6182 get_elf_backend_data (dynobj)->collect, &bh)))
6183 return FALSE;
6184 h = (struct elf_link_hash_entry *) bh;
6185 h->non_elf = 0;
6186 h->def_regular = 1;
6187 h->type = STT_OBJECT;
6188 h->verinfo.vertree = NULL;
6189
6190 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6191 return FALSE;
6192
6193 /* Create a duplicate of the base version with the same
6194 aux block, but different flags. */
6195 def.vd_flags = 0;
6196 def.vd_ndx = 2;
6197 def.vd_aux = sizeof (Elf_External_Verdef);
6198 if (verdefs)
6199 def.vd_next = (sizeof (Elf_External_Verdef)
6200 + sizeof (Elf_External_Verdaux));
6201 else
6202 def.vd_next = 0;
6203 _bfd_elf_swap_verdef_out (output_bfd, &def,
6204 (Elf_External_Verdef *) p);
6205 p += sizeof (Elf_External_Verdef);
6206 }
6207 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6208 (Elf_External_Verdaux *) p);
6209 p += sizeof (Elf_External_Verdaux);
6210
6211 for (t = verdefs; t != NULL; t = t->next)
6212 {
6213 unsigned int cdeps;
6214 struct bfd_elf_version_deps *n;
6215
6216 /* Don't emit the base version twice. */
6217 if (t->vernum == 0)
6218 continue;
6219
6220 cdeps = 0;
6221 for (n = t->deps; n != NULL; n = n->next)
6222 ++cdeps;
6223
6224 /* Add a symbol representing this version. */
6225 bh = NULL;
6226 if (! (_bfd_generic_link_add_one_symbol
6227 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6228 0, NULL, FALSE,
6229 get_elf_backend_data (dynobj)->collect, &bh)))
6230 return FALSE;
6231 h = (struct elf_link_hash_entry *) bh;
6232 h->non_elf = 0;
6233 h->def_regular = 1;
6234 h->type = STT_OBJECT;
6235 h->verinfo.vertree = t;
6236
6237 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6238 return FALSE;
6239
6240 def.vd_version = VER_DEF_CURRENT;
6241 def.vd_flags = 0;
6242 if (t->globals.list == NULL
6243 && t->locals.list == NULL
6244 && ! t->used)
6245 def.vd_flags |= VER_FLG_WEAK;
6246 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6247 def.vd_cnt = cdeps + 1;
6248 def.vd_hash = bfd_elf_hash (t->name);
6249 def.vd_aux = sizeof (Elf_External_Verdef);
6250 def.vd_next = 0;
6251
6252 /* If a basever node is next, it *must* be the last node in
6253 the chain, otherwise Verdef construction breaks. */
6254 if (t->next != NULL && t->next->vernum == 0)
6255 BFD_ASSERT (t->next->next == NULL);
6256
6257 if (t->next != NULL && t->next->vernum != 0)
6258 def.vd_next = (sizeof (Elf_External_Verdef)
6259 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6260
6261 _bfd_elf_swap_verdef_out (output_bfd, &def,
6262 (Elf_External_Verdef *) p);
6263 p += sizeof (Elf_External_Verdef);
6264
6265 defaux.vda_name = h->dynstr_index;
6266 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6267 h->dynstr_index);
6268 defaux.vda_next = 0;
6269 if (t->deps != NULL)
6270 defaux.vda_next = sizeof (Elf_External_Verdaux);
6271 t->name_indx = defaux.vda_name;
6272
6273 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6274 (Elf_External_Verdaux *) p);
6275 p += sizeof (Elf_External_Verdaux);
6276
6277 for (n = t->deps; n != NULL; n = n->next)
6278 {
6279 if (n->version_needed == NULL)
6280 {
6281 /* This can happen if there was an error in the
6282 version script. */
6283 defaux.vda_name = 0;
6284 }
6285 else
6286 {
6287 defaux.vda_name = n->version_needed->name_indx;
6288 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6289 defaux.vda_name);
6290 }
6291 if (n->next == NULL)
6292 defaux.vda_next = 0;
6293 else
6294 defaux.vda_next = sizeof (Elf_External_Verdaux);
6295
6296 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6297 (Elf_External_Verdaux *) p);
6298 p += sizeof (Elf_External_Verdaux);
6299 }
6300 }
6301
6302 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6303 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6304 return FALSE;
6305
6306 elf_tdata (output_bfd)->cverdefs = cdefs;
6307 }
6308
6309 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6310 {
6311 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6312 return FALSE;
6313 }
6314 else if (info->flags & DF_BIND_NOW)
6315 {
6316 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6317 return FALSE;
6318 }
6319
6320 if (info->flags_1)
6321 {
6322 if (bfd_link_executable (info))
6323 info->flags_1 &= ~ (DF_1_INITFIRST
6324 | DF_1_NODELETE
6325 | DF_1_NOOPEN);
6326 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6327 return FALSE;
6328 }
6329
6330 /* Work out the size of the version reference section. */
6331
6332 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6333 BFD_ASSERT (s != NULL);
6334 {
6335 struct elf_find_verdep_info sinfo;
6336
6337 sinfo.info = info;
6338 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6339 if (sinfo.vers == 0)
6340 sinfo.vers = 1;
6341 sinfo.failed = FALSE;
6342
6343 elf_link_hash_traverse (elf_hash_table (info),
6344 _bfd_elf_link_find_version_dependencies,
6345 &sinfo);
6346 if (sinfo.failed)
6347 return FALSE;
6348
6349 if (elf_tdata (output_bfd)->verref == NULL)
6350 s->flags |= SEC_EXCLUDE;
6351 else
6352 {
6353 Elf_Internal_Verneed *t;
6354 unsigned int size;
6355 unsigned int crefs;
6356 bfd_byte *p;
6357
6358 /* Build the version dependency section. */
6359 size = 0;
6360 crefs = 0;
6361 for (t = elf_tdata (output_bfd)->verref;
6362 t != NULL;
6363 t = t->vn_nextref)
6364 {
6365 Elf_Internal_Vernaux *a;
6366
6367 size += sizeof (Elf_External_Verneed);
6368 ++crefs;
6369 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6370 size += sizeof (Elf_External_Vernaux);
6371 }
6372
6373 s->size = size;
6374 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6375 if (s->contents == NULL)
6376 return FALSE;
6377
6378 p = s->contents;
6379 for (t = elf_tdata (output_bfd)->verref;
6380 t != NULL;
6381 t = t->vn_nextref)
6382 {
6383 unsigned int caux;
6384 Elf_Internal_Vernaux *a;
6385 bfd_size_type indx;
6386
6387 caux = 0;
6388 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6389 ++caux;
6390
6391 t->vn_version = VER_NEED_CURRENT;
6392 t->vn_cnt = caux;
6393 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6394 elf_dt_name (t->vn_bfd) != NULL
6395 ? elf_dt_name (t->vn_bfd)
6396 : lbasename (t->vn_bfd->filename),
6397 FALSE);
6398 if (indx == (bfd_size_type) -1)
6399 return FALSE;
6400 t->vn_file = indx;
6401 t->vn_aux = sizeof (Elf_External_Verneed);
6402 if (t->vn_nextref == NULL)
6403 t->vn_next = 0;
6404 else
6405 t->vn_next = (sizeof (Elf_External_Verneed)
6406 + caux * sizeof (Elf_External_Vernaux));
6407
6408 _bfd_elf_swap_verneed_out (output_bfd, t,
6409 (Elf_External_Verneed *) p);
6410 p += sizeof (Elf_External_Verneed);
6411
6412 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6413 {
6414 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6415 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6416 a->vna_nodename, FALSE);
6417 if (indx == (bfd_size_type) -1)
6418 return FALSE;
6419 a->vna_name = indx;
6420 if (a->vna_nextptr == NULL)
6421 a->vna_next = 0;
6422 else
6423 a->vna_next = sizeof (Elf_External_Vernaux);
6424
6425 _bfd_elf_swap_vernaux_out (output_bfd, a,
6426 (Elf_External_Vernaux *) p);
6427 p += sizeof (Elf_External_Vernaux);
6428 }
6429 }
6430
6431 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6432 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6433 return FALSE;
6434
6435 elf_tdata (output_bfd)->cverrefs = crefs;
6436 }
6437 }
6438
6439 if ((elf_tdata (output_bfd)->cverrefs == 0
6440 && elf_tdata (output_bfd)->cverdefs == 0)
6441 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6442 &section_sym_count) == 0)
6443 {
6444 s = bfd_get_linker_section (dynobj, ".gnu.version");
6445 s->flags |= SEC_EXCLUDE;
6446 }
6447 }
6448 return TRUE;
6449 }
6450
6451 /* Find the first non-excluded output section. We'll use its
6452 section symbol for some emitted relocs. */
6453 void
6454 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6455 {
6456 asection *s;
6457
6458 for (s = output_bfd->sections; s != NULL; s = s->next)
6459 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6460 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6461 {
6462 elf_hash_table (info)->text_index_section = s;
6463 break;
6464 }
6465 }
6466
6467 /* Find two non-excluded output sections, one for code, one for data.
6468 We'll use their section symbols for some emitted relocs. */
6469 void
6470 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6471 {
6472 asection *s;
6473
6474 /* Data first, since setting text_index_section changes
6475 _bfd_elf_link_omit_section_dynsym. */
6476 for (s = output_bfd->sections; s != NULL; s = s->next)
6477 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6478 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6479 {
6480 elf_hash_table (info)->data_index_section = s;
6481 break;
6482 }
6483
6484 for (s = output_bfd->sections; s != NULL; s = s->next)
6485 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6486 == (SEC_ALLOC | SEC_READONLY))
6487 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6488 {
6489 elf_hash_table (info)->text_index_section = s;
6490 break;
6491 }
6492
6493 if (elf_hash_table (info)->text_index_section == NULL)
6494 elf_hash_table (info)->text_index_section
6495 = elf_hash_table (info)->data_index_section;
6496 }
6497
6498 bfd_boolean
6499 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6500 {
6501 const struct elf_backend_data *bed;
6502
6503 if (!is_elf_hash_table (info->hash))
6504 return TRUE;
6505
6506 bed = get_elf_backend_data (output_bfd);
6507 (*bed->elf_backend_init_index_section) (output_bfd, info);
6508
6509 if (elf_hash_table (info)->dynamic_sections_created)
6510 {
6511 bfd *dynobj;
6512 asection *s;
6513 bfd_size_type dynsymcount;
6514 unsigned long section_sym_count;
6515 unsigned int dtagcount;
6516
6517 dynobj = elf_hash_table (info)->dynobj;
6518
6519 /* Assign dynsym indicies. In a shared library we generate a
6520 section symbol for each output section, which come first.
6521 Next come all of the back-end allocated local dynamic syms,
6522 followed by the rest of the global symbols. */
6523
6524 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6525 &section_sym_count);
6526
6527 /* Work out the size of the symbol version section. */
6528 s = bfd_get_linker_section (dynobj, ".gnu.version");
6529 BFD_ASSERT (s != NULL);
6530 if (dynsymcount != 0
6531 && (s->flags & SEC_EXCLUDE) == 0)
6532 {
6533 s->size = dynsymcount * sizeof (Elf_External_Versym);
6534 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6535 if (s->contents == NULL)
6536 return FALSE;
6537
6538 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6539 return FALSE;
6540 }
6541
6542 /* Set the size of the .dynsym and .hash sections. We counted
6543 the number of dynamic symbols in elf_link_add_object_symbols.
6544 We will build the contents of .dynsym and .hash when we build
6545 the final symbol table, because until then we do not know the
6546 correct value to give the symbols. We built the .dynstr
6547 section as we went along in elf_link_add_object_symbols. */
6548 s = bfd_get_linker_section (dynobj, ".dynsym");
6549 BFD_ASSERT (s != NULL);
6550 s->size = dynsymcount * bed->s->sizeof_sym;
6551
6552 if (dynsymcount != 0)
6553 {
6554 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6555 if (s->contents == NULL)
6556 return FALSE;
6557
6558 /* The first entry in .dynsym is a dummy symbol.
6559 Clear all the section syms, in case we don't output them all. */
6560 ++section_sym_count;
6561 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6562 }
6563
6564 elf_hash_table (info)->bucketcount = 0;
6565
6566 /* Compute the size of the hashing table. As a side effect this
6567 computes the hash values for all the names we export. */
6568 if (info->emit_hash)
6569 {
6570 unsigned long int *hashcodes;
6571 struct hash_codes_info hashinf;
6572 bfd_size_type amt;
6573 unsigned long int nsyms;
6574 size_t bucketcount;
6575 size_t hash_entry_size;
6576
6577 /* Compute the hash values for all exported symbols. At the same
6578 time store the values in an array so that we could use them for
6579 optimizations. */
6580 amt = dynsymcount * sizeof (unsigned long int);
6581 hashcodes = (unsigned long int *) bfd_malloc (amt);
6582 if (hashcodes == NULL)
6583 return FALSE;
6584 hashinf.hashcodes = hashcodes;
6585 hashinf.error = FALSE;
6586
6587 /* Put all hash values in HASHCODES. */
6588 elf_link_hash_traverse (elf_hash_table (info),
6589 elf_collect_hash_codes, &hashinf);
6590 if (hashinf.error)
6591 {
6592 free (hashcodes);
6593 return FALSE;
6594 }
6595
6596 nsyms = hashinf.hashcodes - hashcodes;
6597 bucketcount
6598 = compute_bucket_count (info, hashcodes, nsyms, 0);
6599 free (hashcodes);
6600
6601 if (bucketcount == 0)
6602 return FALSE;
6603
6604 elf_hash_table (info)->bucketcount = bucketcount;
6605
6606 s = bfd_get_linker_section (dynobj, ".hash");
6607 BFD_ASSERT (s != NULL);
6608 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6609 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6610 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6611 if (s->contents == NULL)
6612 return FALSE;
6613
6614 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6615 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6616 s->contents + hash_entry_size);
6617 }
6618
6619 if (info->emit_gnu_hash)
6620 {
6621 size_t i, cnt;
6622 unsigned char *contents;
6623 struct collect_gnu_hash_codes cinfo;
6624 bfd_size_type amt;
6625 size_t bucketcount;
6626
6627 memset (&cinfo, 0, sizeof (cinfo));
6628
6629 /* Compute the hash values for all exported symbols. At the same
6630 time store the values in an array so that we could use them for
6631 optimizations. */
6632 amt = dynsymcount * 2 * sizeof (unsigned long int);
6633 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
6634 if (cinfo.hashcodes == NULL)
6635 return FALSE;
6636
6637 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6638 cinfo.min_dynindx = -1;
6639 cinfo.output_bfd = output_bfd;
6640 cinfo.bed = bed;
6641
6642 /* Put all hash values in HASHCODES. */
6643 elf_link_hash_traverse (elf_hash_table (info),
6644 elf_collect_gnu_hash_codes, &cinfo);
6645 if (cinfo.error)
6646 {
6647 free (cinfo.hashcodes);
6648 return FALSE;
6649 }
6650
6651 bucketcount
6652 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6653
6654 if (bucketcount == 0)
6655 {
6656 free (cinfo.hashcodes);
6657 return FALSE;
6658 }
6659
6660 s = bfd_get_linker_section (dynobj, ".gnu.hash");
6661 BFD_ASSERT (s != NULL);
6662
6663 if (cinfo.nsyms == 0)
6664 {
6665 /* Empty .gnu.hash section is special. */
6666 BFD_ASSERT (cinfo.min_dynindx == -1);
6667 free (cinfo.hashcodes);
6668 s->size = 5 * 4 + bed->s->arch_size / 8;
6669 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6670 if (contents == NULL)
6671 return FALSE;
6672 s->contents = contents;
6673 /* 1 empty bucket. */
6674 bfd_put_32 (output_bfd, 1, contents);
6675 /* SYMIDX above the special symbol 0. */
6676 bfd_put_32 (output_bfd, 1, contents + 4);
6677 /* Just one word for bitmask. */
6678 bfd_put_32 (output_bfd, 1, contents + 8);
6679 /* Only hash fn bloom filter. */
6680 bfd_put_32 (output_bfd, 0, contents + 12);
6681 /* No hashes are valid - empty bitmask. */
6682 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6683 /* No hashes in the only bucket. */
6684 bfd_put_32 (output_bfd, 0,
6685 contents + 16 + bed->s->arch_size / 8);
6686 }
6687 else
6688 {
6689 unsigned long int maskwords, maskbitslog2, x;
6690 BFD_ASSERT (cinfo.min_dynindx != -1);
6691
6692 x = cinfo.nsyms;
6693 maskbitslog2 = 1;
6694 while ((x >>= 1) != 0)
6695 ++maskbitslog2;
6696 if (maskbitslog2 < 3)
6697 maskbitslog2 = 5;
6698 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6699 maskbitslog2 = maskbitslog2 + 3;
6700 else
6701 maskbitslog2 = maskbitslog2 + 2;
6702 if (bed->s->arch_size == 64)
6703 {
6704 if (maskbitslog2 == 5)
6705 maskbitslog2 = 6;
6706 cinfo.shift1 = 6;
6707 }
6708 else
6709 cinfo.shift1 = 5;
6710 cinfo.mask = (1 << cinfo.shift1) - 1;
6711 cinfo.shift2 = maskbitslog2;
6712 cinfo.maskbits = 1 << maskbitslog2;
6713 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6714 amt = bucketcount * sizeof (unsigned long int) * 2;
6715 amt += maskwords * sizeof (bfd_vma);
6716 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
6717 if (cinfo.bitmask == NULL)
6718 {
6719 free (cinfo.hashcodes);
6720 return FALSE;
6721 }
6722
6723 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
6724 cinfo.indx = cinfo.counts + bucketcount;
6725 cinfo.symindx = dynsymcount - cinfo.nsyms;
6726 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6727
6728 /* Determine how often each hash bucket is used. */
6729 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6730 for (i = 0; i < cinfo.nsyms; ++i)
6731 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6732
6733 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6734 if (cinfo.counts[i] != 0)
6735 {
6736 cinfo.indx[i] = cnt;
6737 cnt += cinfo.counts[i];
6738 }
6739 BFD_ASSERT (cnt == dynsymcount);
6740 cinfo.bucketcount = bucketcount;
6741 cinfo.local_indx = cinfo.min_dynindx;
6742
6743 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6744 s->size += cinfo.maskbits / 8;
6745 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6746 if (contents == NULL)
6747 {
6748 free (cinfo.bitmask);
6749 free (cinfo.hashcodes);
6750 return FALSE;
6751 }
6752
6753 s->contents = contents;
6754 bfd_put_32 (output_bfd, bucketcount, contents);
6755 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6756 bfd_put_32 (output_bfd, maskwords, contents + 8);
6757 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6758 contents += 16 + cinfo.maskbits / 8;
6759
6760 for (i = 0; i < bucketcount; ++i)
6761 {
6762 if (cinfo.counts[i] == 0)
6763 bfd_put_32 (output_bfd, 0, contents);
6764 else
6765 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6766 contents += 4;
6767 }
6768
6769 cinfo.contents = contents;
6770
6771 /* Renumber dynamic symbols, populate .gnu.hash section. */
6772 elf_link_hash_traverse (elf_hash_table (info),
6773 elf_renumber_gnu_hash_syms, &cinfo);
6774
6775 contents = s->contents + 16;
6776 for (i = 0; i < maskwords; ++i)
6777 {
6778 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6779 contents);
6780 contents += bed->s->arch_size / 8;
6781 }
6782
6783 free (cinfo.bitmask);
6784 free (cinfo.hashcodes);
6785 }
6786 }
6787
6788 s = bfd_get_linker_section (dynobj, ".dynstr");
6789 BFD_ASSERT (s != NULL);
6790
6791 elf_finalize_dynstr (output_bfd, info);
6792
6793 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6794
6795 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6796 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6797 return FALSE;
6798 }
6799
6800 return TRUE;
6801 }
6802 \f
6803 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
6804
6805 static void
6806 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6807 asection *sec)
6808 {
6809 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
6810 sec->sec_info_type = SEC_INFO_TYPE_NONE;
6811 }
6812
6813 /* Finish SHF_MERGE section merging. */
6814
6815 bfd_boolean
6816 _bfd_elf_merge_sections (bfd *abfd, struct bfd_link_info *info)
6817 {
6818 bfd *ibfd;
6819 asection *sec;
6820
6821 if (!is_elf_hash_table (info->hash))
6822 return FALSE;
6823
6824 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6825 if ((ibfd->flags & DYNAMIC) == 0)
6826 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6827 if ((sec->flags & SEC_MERGE) != 0
6828 && !bfd_is_abs_section (sec->output_section))
6829 {
6830 struct bfd_elf_section_data *secdata;
6831
6832 secdata = elf_section_data (sec);
6833 if (! _bfd_add_merge_section (abfd,
6834 &elf_hash_table (info)->merge_info,
6835 sec, &secdata->sec_info))
6836 return FALSE;
6837 else if (secdata->sec_info)
6838 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
6839 }
6840
6841 if (elf_hash_table (info)->merge_info != NULL)
6842 _bfd_merge_sections (abfd, info, elf_hash_table (info)->merge_info,
6843 merge_sections_remove_hook);
6844 return TRUE;
6845 }
6846
6847 /* Create an entry in an ELF linker hash table. */
6848
6849 struct bfd_hash_entry *
6850 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6851 struct bfd_hash_table *table,
6852 const char *string)
6853 {
6854 /* Allocate the structure if it has not already been allocated by a
6855 subclass. */
6856 if (entry == NULL)
6857 {
6858 entry = (struct bfd_hash_entry *)
6859 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
6860 if (entry == NULL)
6861 return entry;
6862 }
6863
6864 /* Call the allocation method of the superclass. */
6865 entry = _bfd_link_hash_newfunc (entry, table, string);
6866 if (entry != NULL)
6867 {
6868 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6869 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6870
6871 /* Set local fields. */
6872 ret->indx = -1;
6873 ret->dynindx = -1;
6874 ret->got = htab->init_got_refcount;
6875 ret->plt = htab->init_plt_refcount;
6876 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
6877 - offsetof (struct elf_link_hash_entry, size)));
6878 /* Assume that we have been called by a non-ELF symbol reader.
6879 This flag is then reset by the code which reads an ELF input
6880 file. This ensures that a symbol created by a non-ELF symbol
6881 reader will have the flag set correctly. */
6882 ret->non_elf = 1;
6883 }
6884
6885 return entry;
6886 }
6887
6888 /* Copy data from an indirect symbol to its direct symbol, hiding the
6889 old indirect symbol. Also used for copying flags to a weakdef. */
6890
6891 void
6892 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
6893 struct elf_link_hash_entry *dir,
6894 struct elf_link_hash_entry *ind)
6895 {
6896 struct elf_link_hash_table *htab;
6897
6898 /* Copy down any references that we may have already seen to the
6899 symbol which just became indirect if DIR isn't a hidden versioned
6900 symbol. */
6901
6902 if (dir->versioned != versioned_hidden)
6903 {
6904 dir->ref_dynamic |= ind->ref_dynamic;
6905 dir->ref_regular |= ind->ref_regular;
6906 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
6907 dir->non_got_ref |= ind->non_got_ref;
6908 dir->needs_plt |= ind->needs_plt;
6909 dir->pointer_equality_needed |= ind->pointer_equality_needed;
6910 }
6911
6912 if (ind->root.type != bfd_link_hash_indirect)
6913 return;
6914
6915 /* Copy over the global and procedure linkage table refcount entries.
6916 These may have been already set up by a check_relocs routine. */
6917 htab = elf_hash_table (info);
6918 if (ind->got.refcount > htab->init_got_refcount.refcount)
6919 {
6920 if (dir->got.refcount < 0)
6921 dir->got.refcount = 0;
6922 dir->got.refcount += ind->got.refcount;
6923 ind->got.refcount = htab->init_got_refcount.refcount;
6924 }
6925
6926 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
6927 {
6928 if (dir->plt.refcount < 0)
6929 dir->plt.refcount = 0;
6930 dir->plt.refcount += ind->plt.refcount;
6931 ind->plt.refcount = htab->init_plt_refcount.refcount;
6932 }
6933
6934 if (ind->dynindx != -1)
6935 {
6936 if (dir->dynindx != -1)
6937 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
6938 dir->dynindx = ind->dynindx;
6939 dir->dynstr_index = ind->dynstr_index;
6940 ind->dynindx = -1;
6941 ind->dynstr_index = 0;
6942 }
6943 }
6944
6945 void
6946 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
6947 struct elf_link_hash_entry *h,
6948 bfd_boolean force_local)
6949 {
6950 /* STT_GNU_IFUNC symbol must go through PLT. */
6951 if (h->type != STT_GNU_IFUNC)
6952 {
6953 h->plt = elf_hash_table (info)->init_plt_offset;
6954 h->needs_plt = 0;
6955 }
6956 if (force_local)
6957 {
6958 h->forced_local = 1;
6959 if (h->dynindx != -1)
6960 {
6961 h->dynindx = -1;
6962 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
6963 h->dynstr_index);
6964 }
6965 }
6966 }
6967
6968 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
6969 caller. */
6970
6971 bfd_boolean
6972 _bfd_elf_link_hash_table_init
6973 (struct elf_link_hash_table *table,
6974 bfd *abfd,
6975 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
6976 struct bfd_hash_table *,
6977 const char *),
6978 unsigned int entsize,
6979 enum elf_target_id target_id)
6980 {
6981 bfd_boolean ret;
6982 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
6983
6984 table->init_got_refcount.refcount = can_refcount - 1;
6985 table->init_plt_refcount.refcount = can_refcount - 1;
6986 table->init_got_offset.offset = -(bfd_vma) 1;
6987 table->init_plt_offset.offset = -(bfd_vma) 1;
6988 /* The first dynamic symbol is a dummy. */
6989 table->dynsymcount = 1;
6990
6991 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
6992
6993 table->root.type = bfd_link_elf_hash_table;
6994 table->hash_table_id = target_id;
6995
6996 return ret;
6997 }
6998
6999 /* Create an ELF linker hash table. */
7000
7001 struct bfd_link_hash_table *
7002 _bfd_elf_link_hash_table_create (bfd *abfd)
7003 {
7004 struct elf_link_hash_table *ret;
7005 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7006
7007 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7008 if (ret == NULL)
7009 return NULL;
7010
7011 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7012 sizeof (struct elf_link_hash_entry),
7013 GENERIC_ELF_DATA))
7014 {
7015 free (ret);
7016 return NULL;
7017 }
7018 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7019
7020 return &ret->root;
7021 }
7022
7023 /* Destroy an ELF linker hash table. */
7024
7025 void
7026 _bfd_elf_link_hash_table_free (bfd *obfd)
7027 {
7028 struct elf_link_hash_table *htab;
7029
7030 htab = (struct elf_link_hash_table *) obfd->link.hash;
7031 if (htab->dynstr != NULL)
7032 _bfd_elf_strtab_free (htab->dynstr);
7033 _bfd_merge_sections_free (htab->merge_info);
7034 _bfd_generic_link_hash_table_free (obfd);
7035 }
7036
7037 /* This is a hook for the ELF emulation code in the generic linker to
7038 tell the backend linker what file name to use for the DT_NEEDED
7039 entry for a dynamic object. */
7040
7041 void
7042 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7043 {
7044 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7045 && bfd_get_format (abfd) == bfd_object)
7046 elf_dt_name (abfd) = name;
7047 }
7048
7049 int
7050 bfd_elf_get_dyn_lib_class (bfd *abfd)
7051 {
7052 int lib_class;
7053 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7054 && bfd_get_format (abfd) == bfd_object)
7055 lib_class = elf_dyn_lib_class (abfd);
7056 else
7057 lib_class = 0;
7058 return lib_class;
7059 }
7060
7061 void
7062 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7063 {
7064 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7065 && bfd_get_format (abfd) == bfd_object)
7066 elf_dyn_lib_class (abfd) = lib_class;
7067 }
7068
7069 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7070 the linker ELF emulation code. */
7071
7072 struct bfd_link_needed_list *
7073 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7074 struct bfd_link_info *info)
7075 {
7076 if (! is_elf_hash_table (info->hash))
7077 return NULL;
7078 return elf_hash_table (info)->needed;
7079 }
7080
7081 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7082 hook for the linker ELF emulation code. */
7083
7084 struct bfd_link_needed_list *
7085 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7086 struct bfd_link_info *info)
7087 {
7088 if (! is_elf_hash_table (info->hash))
7089 return NULL;
7090 return elf_hash_table (info)->runpath;
7091 }
7092
7093 /* Get the name actually used for a dynamic object for a link. This
7094 is the SONAME entry if there is one. Otherwise, it is the string
7095 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7096
7097 const char *
7098 bfd_elf_get_dt_soname (bfd *abfd)
7099 {
7100 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7101 && bfd_get_format (abfd) == bfd_object)
7102 return elf_dt_name (abfd);
7103 return NULL;
7104 }
7105
7106 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7107 the ELF linker emulation code. */
7108
7109 bfd_boolean
7110 bfd_elf_get_bfd_needed_list (bfd *abfd,
7111 struct bfd_link_needed_list **pneeded)
7112 {
7113 asection *s;
7114 bfd_byte *dynbuf = NULL;
7115 unsigned int elfsec;
7116 unsigned long shlink;
7117 bfd_byte *extdyn, *extdynend;
7118 size_t extdynsize;
7119 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7120
7121 *pneeded = NULL;
7122
7123 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7124 || bfd_get_format (abfd) != bfd_object)
7125 return TRUE;
7126
7127 s = bfd_get_section_by_name (abfd, ".dynamic");
7128 if (s == NULL || s->size == 0)
7129 return TRUE;
7130
7131 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7132 goto error_return;
7133
7134 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7135 if (elfsec == SHN_BAD)
7136 goto error_return;
7137
7138 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7139
7140 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7141 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7142
7143 extdyn = dynbuf;
7144 extdynend = extdyn + s->size;
7145 for (; extdyn < extdynend; extdyn += extdynsize)
7146 {
7147 Elf_Internal_Dyn dyn;
7148
7149 (*swap_dyn_in) (abfd, extdyn, &dyn);
7150
7151 if (dyn.d_tag == DT_NULL)
7152 break;
7153
7154 if (dyn.d_tag == DT_NEEDED)
7155 {
7156 const char *string;
7157 struct bfd_link_needed_list *l;
7158 unsigned int tagv = dyn.d_un.d_val;
7159 bfd_size_type amt;
7160
7161 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7162 if (string == NULL)
7163 goto error_return;
7164
7165 amt = sizeof *l;
7166 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7167 if (l == NULL)
7168 goto error_return;
7169
7170 l->by = abfd;
7171 l->name = string;
7172 l->next = *pneeded;
7173 *pneeded = l;
7174 }
7175 }
7176
7177 free (dynbuf);
7178
7179 return TRUE;
7180
7181 error_return:
7182 if (dynbuf != NULL)
7183 free (dynbuf);
7184 return FALSE;
7185 }
7186
7187 struct elf_symbuf_symbol
7188 {
7189 unsigned long st_name; /* Symbol name, index in string tbl */
7190 unsigned char st_info; /* Type and binding attributes */
7191 unsigned char st_other; /* Visibilty, and target specific */
7192 };
7193
7194 struct elf_symbuf_head
7195 {
7196 struct elf_symbuf_symbol *ssym;
7197 bfd_size_type count;
7198 unsigned int st_shndx;
7199 };
7200
7201 struct elf_symbol
7202 {
7203 union
7204 {
7205 Elf_Internal_Sym *isym;
7206 struct elf_symbuf_symbol *ssym;
7207 } u;
7208 const char *name;
7209 };
7210
7211 /* Sort references to symbols by ascending section number. */
7212
7213 static int
7214 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7215 {
7216 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7217 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7218
7219 return s1->st_shndx - s2->st_shndx;
7220 }
7221
7222 static int
7223 elf_sym_name_compare (const void *arg1, const void *arg2)
7224 {
7225 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7226 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7227 return strcmp (s1->name, s2->name);
7228 }
7229
7230 static struct elf_symbuf_head *
7231 elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
7232 {
7233 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7234 struct elf_symbuf_symbol *ssym;
7235 struct elf_symbuf_head *ssymbuf, *ssymhead;
7236 bfd_size_type i, shndx_count, total_size;
7237
7238 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7239 if (indbuf == NULL)
7240 return NULL;
7241
7242 for (ind = indbuf, i = 0; i < symcount; i++)
7243 if (isymbuf[i].st_shndx != SHN_UNDEF)
7244 *ind++ = &isymbuf[i];
7245 indbufend = ind;
7246
7247 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7248 elf_sort_elf_symbol);
7249
7250 shndx_count = 0;
7251 if (indbufend > indbuf)
7252 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7253 if (ind[0]->st_shndx != ind[1]->st_shndx)
7254 shndx_count++;
7255
7256 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7257 + (indbufend - indbuf) * sizeof (*ssym));
7258 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7259 if (ssymbuf == NULL)
7260 {
7261 free (indbuf);
7262 return NULL;
7263 }
7264
7265 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7266 ssymbuf->ssym = NULL;
7267 ssymbuf->count = shndx_count;
7268 ssymbuf->st_shndx = 0;
7269 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7270 {
7271 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7272 {
7273 ssymhead++;
7274 ssymhead->ssym = ssym;
7275 ssymhead->count = 0;
7276 ssymhead->st_shndx = (*ind)->st_shndx;
7277 }
7278 ssym->st_name = (*ind)->st_name;
7279 ssym->st_info = (*ind)->st_info;
7280 ssym->st_other = (*ind)->st_other;
7281 ssymhead->count++;
7282 }
7283 BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
7284 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7285 == total_size));
7286
7287 free (indbuf);
7288 return ssymbuf;
7289 }
7290
7291 /* Check if 2 sections define the same set of local and global
7292 symbols. */
7293
7294 static bfd_boolean
7295 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7296 struct bfd_link_info *info)
7297 {
7298 bfd *bfd1, *bfd2;
7299 const struct elf_backend_data *bed1, *bed2;
7300 Elf_Internal_Shdr *hdr1, *hdr2;
7301 bfd_size_type symcount1, symcount2;
7302 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7303 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7304 Elf_Internal_Sym *isym, *isymend;
7305 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7306 bfd_size_type count1, count2, i;
7307 unsigned int shndx1, shndx2;
7308 bfd_boolean result;
7309
7310 bfd1 = sec1->owner;
7311 bfd2 = sec2->owner;
7312
7313 /* Both sections have to be in ELF. */
7314 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7315 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7316 return FALSE;
7317
7318 if (elf_section_type (sec1) != elf_section_type (sec2))
7319 return FALSE;
7320
7321 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7322 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7323 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7324 return FALSE;
7325
7326 bed1 = get_elf_backend_data (bfd1);
7327 bed2 = get_elf_backend_data (bfd2);
7328 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7329 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7330 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7331 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7332
7333 if (symcount1 == 0 || symcount2 == 0)
7334 return FALSE;
7335
7336 result = FALSE;
7337 isymbuf1 = NULL;
7338 isymbuf2 = NULL;
7339 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7340 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7341
7342 if (ssymbuf1 == NULL)
7343 {
7344 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7345 NULL, NULL, NULL);
7346 if (isymbuf1 == NULL)
7347 goto done;
7348
7349 if (!info->reduce_memory_overheads)
7350 elf_tdata (bfd1)->symbuf = ssymbuf1
7351 = elf_create_symbuf (symcount1, isymbuf1);
7352 }
7353
7354 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7355 {
7356 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7357 NULL, NULL, NULL);
7358 if (isymbuf2 == NULL)
7359 goto done;
7360
7361 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7362 elf_tdata (bfd2)->symbuf = ssymbuf2
7363 = elf_create_symbuf (symcount2, isymbuf2);
7364 }
7365
7366 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7367 {
7368 /* Optimized faster version. */
7369 bfd_size_type lo, hi, mid;
7370 struct elf_symbol *symp;
7371 struct elf_symbuf_symbol *ssym, *ssymend;
7372
7373 lo = 0;
7374 hi = ssymbuf1->count;
7375 ssymbuf1++;
7376 count1 = 0;
7377 while (lo < hi)
7378 {
7379 mid = (lo + hi) / 2;
7380 if (shndx1 < ssymbuf1[mid].st_shndx)
7381 hi = mid;
7382 else if (shndx1 > ssymbuf1[mid].st_shndx)
7383 lo = mid + 1;
7384 else
7385 {
7386 count1 = ssymbuf1[mid].count;
7387 ssymbuf1 += mid;
7388 break;
7389 }
7390 }
7391
7392 lo = 0;
7393 hi = ssymbuf2->count;
7394 ssymbuf2++;
7395 count2 = 0;
7396 while (lo < hi)
7397 {
7398 mid = (lo + hi) / 2;
7399 if (shndx2 < ssymbuf2[mid].st_shndx)
7400 hi = mid;
7401 else if (shndx2 > ssymbuf2[mid].st_shndx)
7402 lo = mid + 1;
7403 else
7404 {
7405 count2 = ssymbuf2[mid].count;
7406 ssymbuf2 += mid;
7407 break;
7408 }
7409 }
7410
7411 if (count1 == 0 || count2 == 0 || count1 != count2)
7412 goto done;
7413
7414 symtable1
7415 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7416 symtable2
7417 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
7418 if (symtable1 == NULL || symtable2 == NULL)
7419 goto done;
7420
7421 symp = symtable1;
7422 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7423 ssym < ssymend; ssym++, symp++)
7424 {
7425 symp->u.ssym = ssym;
7426 symp->name = bfd_elf_string_from_elf_section (bfd1,
7427 hdr1->sh_link,
7428 ssym->st_name);
7429 }
7430
7431 symp = symtable2;
7432 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7433 ssym < ssymend; ssym++, symp++)
7434 {
7435 symp->u.ssym = ssym;
7436 symp->name = bfd_elf_string_from_elf_section (bfd2,
7437 hdr2->sh_link,
7438 ssym->st_name);
7439 }
7440
7441 /* Sort symbol by name. */
7442 qsort (symtable1, count1, sizeof (struct elf_symbol),
7443 elf_sym_name_compare);
7444 qsort (symtable2, count1, sizeof (struct elf_symbol),
7445 elf_sym_name_compare);
7446
7447 for (i = 0; i < count1; i++)
7448 /* Two symbols must have the same binding, type and name. */
7449 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7450 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7451 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7452 goto done;
7453
7454 result = TRUE;
7455 goto done;
7456 }
7457
7458 symtable1 = (struct elf_symbol *)
7459 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7460 symtable2 = (struct elf_symbol *)
7461 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7462 if (symtable1 == NULL || symtable2 == NULL)
7463 goto done;
7464
7465 /* Count definitions in the section. */
7466 count1 = 0;
7467 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7468 if (isym->st_shndx == shndx1)
7469 symtable1[count1++].u.isym = isym;
7470
7471 count2 = 0;
7472 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7473 if (isym->st_shndx == shndx2)
7474 symtable2[count2++].u.isym = isym;
7475
7476 if (count1 == 0 || count2 == 0 || count1 != count2)
7477 goto done;
7478
7479 for (i = 0; i < count1; i++)
7480 symtable1[i].name
7481 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7482 symtable1[i].u.isym->st_name);
7483
7484 for (i = 0; i < count2; i++)
7485 symtable2[i].name
7486 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7487 symtable2[i].u.isym->st_name);
7488
7489 /* Sort symbol by name. */
7490 qsort (symtable1, count1, sizeof (struct elf_symbol),
7491 elf_sym_name_compare);
7492 qsort (symtable2, count1, sizeof (struct elf_symbol),
7493 elf_sym_name_compare);
7494
7495 for (i = 0; i < count1; i++)
7496 /* Two symbols must have the same binding, type and name. */
7497 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7498 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7499 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7500 goto done;
7501
7502 result = TRUE;
7503
7504 done:
7505 if (symtable1)
7506 free (symtable1);
7507 if (symtable2)
7508 free (symtable2);
7509 if (isymbuf1)
7510 free (isymbuf1);
7511 if (isymbuf2)
7512 free (isymbuf2);
7513
7514 return result;
7515 }
7516
7517 /* Return TRUE if 2 section types are compatible. */
7518
7519 bfd_boolean
7520 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7521 bfd *bbfd, const asection *bsec)
7522 {
7523 if (asec == NULL
7524 || bsec == NULL
7525 || abfd->xvec->flavour != bfd_target_elf_flavour
7526 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7527 return TRUE;
7528
7529 return elf_section_type (asec) == elf_section_type (bsec);
7530 }
7531 \f
7532 /* Final phase of ELF linker. */
7533
7534 /* A structure we use to avoid passing large numbers of arguments. */
7535
7536 struct elf_final_link_info
7537 {
7538 /* General link information. */
7539 struct bfd_link_info *info;
7540 /* Output BFD. */
7541 bfd *output_bfd;
7542 /* Symbol string table. */
7543 struct elf_strtab_hash *symstrtab;
7544 /* .dynsym section. */
7545 asection *dynsym_sec;
7546 /* .hash section. */
7547 asection *hash_sec;
7548 /* symbol version section (.gnu.version). */
7549 asection *symver_sec;
7550 /* Buffer large enough to hold contents of any section. */
7551 bfd_byte *contents;
7552 /* Buffer large enough to hold external relocs of any section. */
7553 void *external_relocs;
7554 /* Buffer large enough to hold internal relocs of any section. */
7555 Elf_Internal_Rela *internal_relocs;
7556 /* Buffer large enough to hold external local symbols of any input
7557 BFD. */
7558 bfd_byte *external_syms;
7559 /* And a buffer for symbol section indices. */
7560 Elf_External_Sym_Shndx *locsym_shndx;
7561 /* Buffer large enough to hold internal local symbols of any input
7562 BFD. */
7563 Elf_Internal_Sym *internal_syms;
7564 /* Array large enough to hold a symbol index for each local symbol
7565 of any input BFD. */
7566 long *indices;
7567 /* Array large enough to hold a section pointer for each local
7568 symbol of any input BFD. */
7569 asection **sections;
7570 /* Buffer for SHT_SYMTAB_SHNDX section. */
7571 Elf_External_Sym_Shndx *symshndxbuf;
7572 /* Number of STT_FILE syms seen. */
7573 size_t filesym_count;
7574 };
7575
7576 /* This struct is used to pass information to elf_link_output_extsym. */
7577
7578 struct elf_outext_info
7579 {
7580 bfd_boolean failed;
7581 bfd_boolean localsyms;
7582 bfd_boolean file_sym_done;
7583 struct elf_final_link_info *flinfo;
7584 };
7585
7586
7587 /* Support for evaluating a complex relocation.
7588
7589 Complex relocations are generalized, self-describing relocations. The
7590 implementation of them consists of two parts: complex symbols, and the
7591 relocations themselves.
7592
7593 The relocations are use a reserved elf-wide relocation type code (R_RELC
7594 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7595 information (start bit, end bit, word width, etc) into the addend. This
7596 information is extracted from CGEN-generated operand tables within gas.
7597
7598 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7599 internal) representing prefix-notation expressions, including but not
7600 limited to those sorts of expressions normally encoded as addends in the
7601 addend field. The symbol mangling format is:
7602
7603 <node> := <literal>
7604 | <unary-operator> ':' <node>
7605 | <binary-operator> ':' <node> ':' <node>
7606 ;
7607
7608 <literal> := 's' <digits=N> ':' <N character symbol name>
7609 | 'S' <digits=N> ':' <N character section name>
7610 | '#' <hexdigits>
7611 ;
7612
7613 <binary-operator> := as in C
7614 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7615
7616 static void
7617 set_symbol_value (bfd *bfd_with_globals,
7618 Elf_Internal_Sym *isymbuf,
7619 size_t locsymcount,
7620 size_t symidx,
7621 bfd_vma val)
7622 {
7623 struct elf_link_hash_entry **sym_hashes;
7624 struct elf_link_hash_entry *h;
7625 size_t extsymoff = locsymcount;
7626
7627 if (symidx < locsymcount)
7628 {
7629 Elf_Internal_Sym *sym;
7630
7631 sym = isymbuf + symidx;
7632 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7633 {
7634 /* It is a local symbol: move it to the
7635 "absolute" section and give it a value. */
7636 sym->st_shndx = SHN_ABS;
7637 sym->st_value = val;
7638 return;
7639 }
7640 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7641 extsymoff = 0;
7642 }
7643
7644 /* It is a global symbol: set its link type
7645 to "defined" and give it a value. */
7646
7647 sym_hashes = elf_sym_hashes (bfd_with_globals);
7648 h = sym_hashes [symidx - extsymoff];
7649 while (h->root.type == bfd_link_hash_indirect
7650 || h->root.type == bfd_link_hash_warning)
7651 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7652 h->root.type = bfd_link_hash_defined;
7653 h->root.u.def.value = val;
7654 h->root.u.def.section = bfd_abs_section_ptr;
7655 }
7656
7657 static bfd_boolean
7658 resolve_symbol (const char *name,
7659 bfd *input_bfd,
7660 struct elf_final_link_info *flinfo,
7661 bfd_vma *result,
7662 Elf_Internal_Sym *isymbuf,
7663 size_t locsymcount)
7664 {
7665 Elf_Internal_Sym *sym;
7666 struct bfd_link_hash_entry *global_entry;
7667 const char *candidate = NULL;
7668 Elf_Internal_Shdr *symtab_hdr;
7669 size_t i;
7670
7671 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7672
7673 for (i = 0; i < locsymcount; ++ i)
7674 {
7675 sym = isymbuf + i;
7676
7677 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7678 continue;
7679
7680 candidate = bfd_elf_string_from_elf_section (input_bfd,
7681 symtab_hdr->sh_link,
7682 sym->st_name);
7683 #ifdef DEBUG
7684 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7685 name, candidate, (unsigned long) sym->st_value);
7686 #endif
7687 if (candidate && strcmp (candidate, name) == 0)
7688 {
7689 asection *sec = flinfo->sections [i];
7690
7691 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7692 *result += sec->output_offset + sec->output_section->vma;
7693 #ifdef DEBUG
7694 printf ("Found symbol with value %8.8lx\n",
7695 (unsigned long) *result);
7696 #endif
7697 return TRUE;
7698 }
7699 }
7700
7701 /* Hmm, haven't found it yet. perhaps it is a global. */
7702 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
7703 FALSE, FALSE, TRUE);
7704 if (!global_entry)
7705 return FALSE;
7706
7707 if (global_entry->type == bfd_link_hash_defined
7708 || global_entry->type == bfd_link_hash_defweak)
7709 {
7710 *result = (global_entry->u.def.value
7711 + global_entry->u.def.section->output_section->vma
7712 + global_entry->u.def.section->output_offset);
7713 #ifdef DEBUG
7714 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7715 global_entry->root.string, (unsigned long) *result);
7716 #endif
7717 return TRUE;
7718 }
7719
7720 return FALSE;
7721 }
7722
7723 static bfd_boolean
7724 resolve_section (const char *name,
7725 asection *sections,
7726 bfd_vma *result)
7727 {
7728 asection *curr;
7729 unsigned int len;
7730
7731 for (curr = sections; curr; curr = curr->next)
7732 if (strcmp (curr->name, name) == 0)
7733 {
7734 *result = curr->vma;
7735 return TRUE;
7736 }
7737
7738 /* Hmm. still haven't found it. try pseudo-section names. */
7739 for (curr = sections; curr; curr = curr->next)
7740 {
7741 len = strlen (curr->name);
7742 if (len > strlen (name))
7743 continue;
7744
7745 if (strncmp (curr->name, name, len) == 0)
7746 {
7747 if (strncmp (".end", name + len, 4) == 0)
7748 {
7749 *result = curr->vma + curr->size;
7750 return TRUE;
7751 }
7752
7753 /* Insert more pseudo-section names here, if you like. */
7754 }
7755 }
7756
7757 return FALSE;
7758 }
7759
7760 static void
7761 undefined_reference (const char *reftype, const char *name)
7762 {
7763 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7764 reftype, name);
7765 }
7766
7767 static bfd_boolean
7768 eval_symbol (bfd_vma *result,
7769 const char **symp,
7770 bfd *input_bfd,
7771 struct elf_final_link_info *flinfo,
7772 bfd_vma dot,
7773 Elf_Internal_Sym *isymbuf,
7774 size_t locsymcount,
7775 int signed_p)
7776 {
7777 size_t len;
7778 size_t symlen;
7779 bfd_vma a;
7780 bfd_vma b;
7781 char symbuf[4096];
7782 const char *sym = *symp;
7783 const char *symend;
7784 bfd_boolean symbol_is_section = FALSE;
7785
7786 len = strlen (sym);
7787 symend = sym + len;
7788
7789 if (len < 1 || len > sizeof (symbuf))
7790 {
7791 bfd_set_error (bfd_error_invalid_operation);
7792 return FALSE;
7793 }
7794
7795 switch (* sym)
7796 {
7797 case '.':
7798 *result = dot;
7799 *symp = sym + 1;
7800 return TRUE;
7801
7802 case '#':
7803 ++sym;
7804 *result = strtoul (sym, (char **) symp, 16);
7805 return TRUE;
7806
7807 case 'S':
7808 symbol_is_section = TRUE;
7809 case 's':
7810 ++sym;
7811 symlen = strtol (sym, (char **) symp, 10);
7812 sym = *symp + 1; /* Skip the trailing ':'. */
7813
7814 if (symend < sym || symlen + 1 > sizeof (symbuf))
7815 {
7816 bfd_set_error (bfd_error_invalid_operation);
7817 return FALSE;
7818 }
7819
7820 memcpy (symbuf, sym, symlen);
7821 symbuf[symlen] = '\0';
7822 *symp = sym + symlen;
7823
7824 /* Is it always possible, with complex symbols, that gas "mis-guessed"
7825 the symbol as a section, or vice-versa. so we're pretty liberal in our
7826 interpretation here; section means "try section first", not "must be a
7827 section", and likewise with symbol. */
7828
7829 if (symbol_is_section)
7830 {
7831 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result)
7832 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
7833 isymbuf, locsymcount))
7834 {
7835 undefined_reference ("section", symbuf);
7836 return FALSE;
7837 }
7838 }
7839 else
7840 {
7841 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
7842 isymbuf, locsymcount)
7843 && !resolve_section (symbuf, flinfo->output_bfd->sections,
7844 result))
7845 {
7846 undefined_reference ("symbol", symbuf);
7847 return FALSE;
7848 }
7849 }
7850
7851 return TRUE;
7852
7853 /* All that remains are operators. */
7854
7855 #define UNARY_OP(op) \
7856 if (strncmp (sym, #op, strlen (#op)) == 0) \
7857 { \
7858 sym += strlen (#op); \
7859 if (*sym == ':') \
7860 ++sym; \
7861 *symp = sym; \
7862 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
7863 isymbuf, locsymcount, signed_p)) \
7864 return FALSE; \
7865 if (signed_p) \
7866 *result = op ((bfd_signed_vma) a); \
7867 else \
7868 *result = op a; \
7869 return TRUE; \
7870 }
7871
7872 #define BINARY_OP(op) \
7873 if (strncmp (sym, #op, strlen (#op)) == 0) \
7874 { \
7875 sym += strlen (#op); \
7876 if (*sym == ':') \
7877 ++sym; \
7878 *symp = sym; \
7879 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
7880 isymbuf, locsymcount, signed_p)) \
7881 return FALSE; \
7882 ++*symp; \
7883 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
7884 isymbuf, locsymcount, signed_p)) \
7885 return FALSE; \
7886 if (signed_p) \
7887 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
7888 else \
7889 *result = a op b; \
7890 return TRUE; \
7891 }
7892
7893 default:
7894 UNARY_OP (0-);
7895 BINARY_OP (<<);
7896 BINARY_OP (>>);
7897 BINARY_OP (==);
7898 BINARY_OP (!=);
7899 BINARY_OP (<=);
7900 BINARY_OP (>=);
7901 BINARY_OP (&&);
7902 BINARY_OP (||);
7903 UNARY_OP (~);
7904 UNARY_OP (!);
7905 BINARY_OP (*);
7906 BINARY_OP (/);
7907 BINARY_OP (%);
7908 BINARY_OP (^);
7909 BINARY_OP (|);
7910 BINARY_OP (&);
7911 BINARY_OP (+);
7912 BINARY_OP (-);
7913 BINARY_OP (<);
7914 BINARY_OP (>);
7915 #undef UNARY_OP
7916 #undef BINARY_OP
7917 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
7918 bfd_set_error (bfd_error_invalid_operation);
7919 return FALSE;
7920 }
7921 }
7922
7923 static void
7924 put_value (bfd_vma size,
7925 unsigned long chunksz,
7926 bfd *input_bfd,
7927 bfd_vma x,
7928 bfd_byte *location)
7929 {
7930 location += (size - chunksz);
7931
7932 for (; size; size -= chunksz, location -= chunksz)
7933 {
7934 switch (chunksz)
7935 {
7936 case 1:
7937 bfd_put_8 (input_bfd, x, location);
7938 x >>= 8;
7939 break;
7940 case 2:
7941 bfd_put_16 (input_bfd, x, location);
7942 x >>= 16;
7943 break;
7944 case 4:
7945 bfd_put_32 (input_bfd, x, location);
7946 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
7947 x >>= 16;
7948 x >>= 16;
7949 break;
7950 #ifdef BFD64
7951 case 8:
7952 bfd_put_64 (input_bfd, x, location);
7953 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
7954 x >>= 32;
7955 x >>= 32;
7956 break;
7957 #endif
7958 default:
7959 abort ();
7960 break;
7961 }
7962 }
7963 }
7964
7965 static bfd_vma
7966 get_value (bfd_vma size,
7967 unsigned long chunksz,
7968 bfd *input_bfd,
7969 bfd_byte *location)
7970 {
7971 int shift;
7972 bfd_vma x = 0;
7973
7974 /* Sanity checks. */
7975 BFD_ASSERT (chunksz <= sizeof (x)
7976 && size >= chunksz
7977 && chunksz != 0
7978 && (size % chunksz) == 0
7979 && input_bfd != NULL
7980 && location != NULL);
7981
7982 if (chunksz == sizeof (x))
7983 {
7984 BFD_ASSERT (size == chunksz);
7985
7986 /* Make sure that we do not perform an undefined shift operation.
7987 We know that size == chunksz so there will only be one iteration
7988 of the loop below. */
7989 shift = 0;
7990 }
7991 else
7992 shift = 8 * chunksz;
7993
7994 for (; size; size -= chunksz, location += chunksz)
7995 {
7996 switch (chunksz)
7997 {
7998 case 1:
7999 x = (x << shift) | bfd_get_8 (input_bfd, location);
8000 break;
8001 case 2:
8002 x = (x << shift) | bfd_get_16 (input_bfd, location);
8003 break;
8004 case 4:
8005 x = (x << shift) | bfd_get_32 (input_bfd, location);
8006 break;
8007 #ifdef BFD64
8008 case 8:
8009 x = (x << shift) | bfd_get_64 (input_bfd, location);
8010 break;
8011 #endif
8012 default:
8013 abort ();
8014 }
8015 }
8016 return x;
8017 }
8018
8019 static void
8020 decode_complex_addend (unsigned long *start, /* in bits */
8021 unsigned long *oplen, /* in bits */
8022 unsigned long *len, /* in bits */
8023 unsigned long *wordsz, /* in bytes */
8024 unsigned long *chunksz, /* in bytes */
8025 unsigned long *lsb0_p,
8026 unsigned long *signed_p,
8027 unsigned long *trunc_p,
8028 unsigned long encoded)
8029 {
8030 * start = encoded & 0x3F;
8031 * len = (encoded >> 6) & 0x3F;
8032 * oplen = (encoded >> 12) & 0x3F;
8033 * wordsz = (encoded >> 18) & 0xF;
8034 * chunksz = (encoded >> 22) & 0xF;
8035 * lsb0_p = (encoded >> 27) & 1;
8036 * signed_p = (encoded >> 28) & 1;
8037 * trunc_p = (encoded >> 29) & 1;
8038 }
8039
8040 bfd_reloc_status_type
8041 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8042 asection *input_section ATTRIBUTE_UNUSED,
8043 bfd_byte *contents,
8044 Elf_Internal_Rela *rel,
8045 bfd_vma relocation)
8046 {
8047 bfd_vma shift, x, mask;
8048 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8049 bfd_reloc_status_type r;
8050
8051 /* Perform this reloc, since it is complex.
8052 (this is not to say that it necessarily refers to a complex
8053 symbol; merely that it is a self-describing CGEN based reloc.
8054 i.e. the addend has the complete reloc information (bit start, end,
8055 word size, etc) encoded within it.). */
8056
8057 decode_complex_addend (&start, &oplen, &len, &wordsz,
8058 &chunksz, &lsb0_p, &signed_p,
8059 &trunc_p, rel->r_addend);
8060
8061 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8062
8063 if (lsb0_p)
8064 shift = (start + 1) - len;
8065 else
8066 shift = (8 * wordsz) - (start + len);
8067
8068 /* FIXME: octets_per_byte. */
8069 x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
8070
8071 #ifdef DEBUG
8072 printf ("Doing complex reloc: "
8073 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8074 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8075 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8076 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8077 oplen, (unsigned long) x, (unsigned long) mask,
8078 (unsigned long) relocation);
8079 #endif
8080
8081 r = bfd_reloc_ok;
8082 if (! trunc_p)
8083 /* Now do an overflow check. */
8084 r = bfd_check_overflow ((signed_p
8085 ? complain_overflow_signed
8086 : complain_overflow_unsigned),
8087 len, 0, (8 * wordsz),
8088 relocation);
8089
8090 /* Do the deed. */
8091 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8092
8093 #ifdef DEBUG
8094 printf (" relocation: %8.8lx\n"
8095 " shifted mask: %8.8lx\n"
8096 " shifted/masked reloc: %8.8lx\n"
8097 " result: %8.8lx\n",
8098 (unsigned long) relocation, (unsigned long) (mask << shift),
8099 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8100 #endif
8101 /* FIXME: octets_per_byte. */
8102 put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
8103 return r;
8104 }
8105
8106 /* qsort comparison functions sorting external relocs by r_offset. */
8107
8108 static int
8109 cmp_ext32l_r_offset (const void *p, const void *q)
8110 {
8111 union aligned32
8112 {
8113 uint32_t v;
8114 unsigned char c[4];
8115 };
8116 const union aligned32 *a
8117 = (const union aligned32 *) ((const Elf32_External_Rel *) p)->r_offset;
8118 const union aligned32 *b
8119 = (const union aligned32 *) ((const Elf32_External_Rel *) q)->r_offset;
8120
8121 uint32_t aval = ( (uint32_t) a->c[0]
8122 | (uint32_t) a->c[1] << 8
8123 | (uint32_t) a->c[2] << 16
8124 | (uint32_t) a->c[3] << 24);
8125 uint32_t bval = ( (uint32_t) b->c[0]
8126 | (uint32_t) b->c[1] << 8
8127 | (uint32_t) b->c[2] << 16
8128 | (uint32_t) b->c[3] << 24);
8129 if (aval < bval)
8130 return -1;
8131 else if (aval > bval)
8132 return 1;
8133 return 0;
8134 }
8135
8136 static int
8137 cmp_ext32b_r_offset (const void *p, const void *q)
8138 {
8139 union aligned32
8140 {
8141 uint32_t v;
8142 unsigned char c[4];
8143 };
8144 const union aligned32 *a
8145 = (const union aligned32 *) ((const Elf32_External_Rel *) p)->r_offset;
8146 const union aligned32 *b
8147 = (const union aligned32 *) ((const Elf32_External_Rel *) q)->r_offset;
8148
8149 uint32_t aval = ( (uint32_t) a->c[0] << 24
8150 | (uint32_t) a->c[1] << 16
8151 | (uint32_t) a->c[2] << 8
8152 | (uint32_t) a->c[3]);
8153 uint32_t bval = ( (uint32_t) b->c[0] << 24
8154 | (uint32_t) b->c[1] << 16
8155 | (uint32_t) b->c[2] << 8
8156 | (uint32_t) b->c[3]);
8157 if (aval < bval)
8158 return -1;
8159 else if (aval > bval)
8160 return 1;
8161 return 0;
8162 }
8163
8164 #ifdef BFD_HOST_64_BIT
8165 static int
8166 cmp_ext64l_r_offset (const void *p, const void *q)
8167 {
8168 union aligned64
8169 {
8170 uint64_t v;
8171 unsigned char c[8];
8172 };
8173 const union aligned64 *a
8174 = (const union aligned64 *) ((const Elf64_External_Rel *) p)->r_offset;
8175 const union aligned64 *b
8176 = (const union aligned64 *) ((const Elf64_External_Rel *) q)->r_offset;
8177
8178 uint64_t aval = ( (uint64_t) a->c[0]
8179 | (uint64_t) a->c[1] << 8
8180 | (uint64_t) a->c[2] << 16
8181 | (uint64_t) a->c[3] << 24
8182 | (uint64_t) a->c[4] << 32
8183 | (uint64_t) a->c[5] << 40
8184 | (uint64_t) a->c[6] << 48
8185 | (uint64_t) a->c[7] << 56);
8186 uint64_t bval = ( (uint64_t) b->c[0]
8187 | (uint64_t) b->c[1] << 8
8188 | (uint64_t) b->c[2] << 16
8189 | (uint64_t) b->c[3] << 24
8190 | (uint64_t) b->c[4] << 32
8191 | (uint64_t) b->c[5] << 40
8192 | (uint64_t) b->c[6] << 48
8193 | (uint64_t) b->c[7] << 56);
8194 if (aval < bval)
8195 return -1;
8196 else if (aval > bval)
8197 return 1;
8198 return 0;
8199 }
8200
8201 static int
8202 cmp_ext64b_r_offset (const void *p, const void *q)
8203 {
8204 union aligned64
8205 {
8206 uint64_t v;
8207 unsigned char c[8];
8208 };
8209 const union aligned64 *a
8210 = (const union aligned64 *) ((const Elf64_External_Rel *) p)->r_offset;
8211 const union aligned64 *b
8212 = (const union aligned64 *) ((const Elf64_External_Rel *) q)->r_offset;
8213
8214 uint64_t aval = ( (uint64_t) a->c[0] << 56
8215 | (uint64_t) a->c[1] << 48
8216 | (uint64_t) a->c[2] << 40
8217 | (uint64_t) a->c[3] << 32
8218 | (uint64_t) a->c[4] << 24
8219 | (uint64_t) a->c[5] << 16
8220 | (uint64_t) a->c[6] << 8
8221 | (uint64_t) a->c[7]);
8222 uint64_t bval = ( (uint64_t) b->c[0] << 56
8223 | (uint64_t) b->c[1] << 48
8224 | (uint64_t) b->c[2] << 40
8225 | (uint64_t) b->c[3] << 32
8226 | (uint64_t) b->c[4] << 24
8227 | (uint64_t) b->c[5] << 16
8228 | (uint64_t) b->c[6] << 8
8229 | (uint64_t) b->c[7]);
8230 if (aval < bval)
8231 return -1;
8232 else if (aval > bval)
8233 return 1;
8234 return 0;
8235 }
8236 #endif
8237
8238 /* When performing a relocatable link, the input relocations are
8239 preserved. But, if they reference global symbols, the indices
8240 referenced must be updated. Update all the relocations found in
8241 RELDATA. */
8242
8243 static void
8244 elf_link_adjust_relocs (bfd *abfd,
8245 struct bfd_elf_section_reloc_data *reldata,
8246 bfd_boolean sort)
8247 {
8248 unsigned int i;
8249 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8250 bfd_byte *erela;
8251 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8252 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8253 bfd_vma r_type_mask;
8254 int r_sym_shift;
8255 unsigned int count = reldata->count;
8256 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8257
8258 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8259 {
8260 swap_in = bed->s->swap_reloc_in;
8261 swap_out = bed->s->swap_reloc_out;
8262 }
8263 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8264 {
8265 swap_in = bed->s->swap_reloca_in;
8266 swap_out = bed->s->swap_reloca_out;
8267 }
8268 else
8269 abort ();
8270
8271 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8272 abort ();
8273
8274 if (bed->s->arch_size == 32)
8275 {
8276 r_type_mask = 0xff;
8277 r_sym_shift = 8;
8278 }
8279 else
8280 {
8281 r_type_mask = 0xffffffff;
8282 r_sym_shift = 32;
8283 }
8284
8285 erela = reldata->hdr->contents;
8286 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8287 {
8288 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8289 unsigned int j;
8290
8291 if (*rel_hash == NULL)
8292 continue;
8293
8294 BFD_ASSERT ((*rel_hash)->indx >= 0);
8295
8296 (*swap_in) (abfd, erela, irela);
8297 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8298 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8299 | (irela[j].r_info & r_type_mask));
8300 (*swap_out) (abfd, irela, erela);
8301 }
8302
8303 if (sort)
8304 {
8305 int (*compare) (const void *, const void *);
8306
8307 if (bed->s->arch_size == 32)
8308 {
8309 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8310 compare = cmp_ext32l_r_offset;
8311 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8312 compare = cmp_ext32b_r_offset;
8313 else
8314 abort ();
8315 }
8316 else
8317 {
8318 #ifdef BFD_HOST_64_BIT
8319 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8320 compare = cmp_ext64l_r_offset;
8321 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8322 compare = cmp_ext64b_r_offset;
8323 else
8324 #endif
8325 abort ();
8326 }
8327 qsort (reldata->hdr->contents, count, reldata->hdr->sh_entsize, compare);
8328 free (reldata->hashes);
8329 reldata->hashes = NULL;
8330 }
8331 }
8332
8333 struct elf_link_sort_rela
8334 {
8335 union {
8336 bfd_vma offset;
8337 bfd_vma sym_mask;
8338 } u;
8339 enum elf_reloc_type_class type;
8340 /* We use this as an array of size int_rels_per_ext_rel. */
8341 Elf_Internal_Rela rela[1];
8342 };
8343
8344 static int
8345 elf_link_sort_cmp1 (const void *A, const void *B)
8346 {
8347 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8348 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8349 int relativea, relativeb;
8350
8351 relativea = a->type == reloc_class_relative;
8352 relativeb = b->type == reloc_class_relative;
8353
8354 if (relativea < relativeb)
8355 return 1;
8356 if (relativea > relativeb)
8357 return -1;
8358 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8359 return -1;
8360 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8361 return 1;
8362 if (a->rela->r_offset < b->rela->r_offset)
8363 return -1;
8364 if (a->rela->r_offset > b->rela->r_offset)
8365 return 1;
8366 return 0;
8367 }
8368
8369 static int
8370 elf_link_sort_cmp2 (const void *A, const void *B)
8371 {
8372 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8373 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8374
8375 if (a->type < b->type)
8376 return -1;
8377 if (a->type > b->type)
8378 return 1;
8379 if (a->u.offset < b->u.offset)
8380 return -1;
8381 if (a->u.offset > b->u.offset)
8382 return 1;
8383 if (a->rela->r_offset < b->rela->r_offset)
8384 return -1;
8385 if (a->rela->r_offset > b->rela->r_offset)
8386 return 1;
8387 return 0;
8388 }
8389
8390 static size_t
8391 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8392 {
8393 asection *dynamic_relocs;
8394 asection *rela_dyn;
8395 asection *rel_dyn;
8396 bfd_size_type count, size;
8397 size_t i, ret, sort_elt, ext_size;
8398 bfd_byte *sort, *s_non_relative, *p;
8399 struct elf_link_sort_rela *sq;
8400 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8401 int i2e = bed->s->int_rels_per_ext_rel;
8402 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8403 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8404 struct bfd_link_order *lo;
8405 bfd_vma r_sym_mask;
8406 bfd_boolean use_rela;
8407
8408 /* Find a dynamic reloc section. */
8409 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8410 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8411 if (rela_dyn != NULL && rela_dyn->size > 0
8412 && rel_dyn != NULL && rel_dyn->size > 0)
8413 {
8414 bfd_boolean use_rela_initialised = FALSE;
8415
8416 /* This is just here to stop gcc from complaining.
8417 It's initialization checking code is not perfect. */
8418 use_rela = TRUE;
8419
8420 /* Both sections are present. Examine the sizes
8421 of the indirect sections to help us choose. */
8422 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8423 if (lo->type == bfd_indirect_link_order)
8424 {
8425 asection *o = lo->u.indirect.section;
8426
8427 if ((o->size % bed->s->sizeof_rela) == 0)
8428 {
8429 if ((o->size % bed->s->sizeof_rel) == 0)
8430 /* Section size is divisible by both rel and rela sizes.
8431 It is of no help to us. */
8432 ;
8433 else
8434 {
8435 /* Section size is only divisible by rela. */
8436 if (use_rela_initialised && (use_rela == FALSE))
8437 {
8438 _bfd_error_handler
8439 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8440 bfd_set_error (bfd_error_invalid_operation);
8441 return 0;
8442 }
8443 else
8444 {
8445 use_rela = TRUE;
8446 use_rela_initialised = TRUE;
8447 }
8448 }
8449 }
8450 else if ((o->size % bed->s->sizeof_rel) == 0)
8451 {
8452 /* Section size is only divisible by rel. */
8453 if (use_rela_initialised && (use_rela == TRUE))
8454 {
8455 _bfd_error_handler
8456 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8457 bfd_set_error (bfd_error_invalid_operation);
8458 return 0;
8459 }
8460 else
8461 {
8462 use_rela = FALSE;
8463 use_rela_initialised = TRUE;
8464 }
8465 }
8466 else
8467 {
8468 /* The section size is not divisible by either - something is wrong. */
8469 _bfd_error_handler
8470 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8471 bfd_set_error (bfd_error_invalid_operation);
8472 return 0;
8473 }
8474 }
8475
8476 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8477 if (lo->type == bfd_indirect_link_order)
8478 {
8479 asection *o = lo->u.indirect.section;
8480
8481 if ((o->size % bed->s->sizeof_rela) == 0)
8482 {
8483 if ((o->size % bed->s->sizeof_rel) == 0)
8484 /* Section size is divisible by both rel and rela sizes.
8485 It is of no help to us. */
8486 ;
8487 else
8488 {
8489 /* Section size is only divisible by rela. */
8490 if (use_rela_initialised && (use_rela == FALSE))
8491 {
8492 _bfd_error_handler
8493 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8494 bfd_set_error (bfd_error_invalid_operation);
8495 return 0;
8496 }
8497 else
8498 {
8499 use_rela = TRUE;
8500 use_rela_initialised = TRUE;
8501 }
8502 }
8503 }
8504 else if ((o->size % bed->s->sizeof_rel) == 0)
8505 {
8506 /* Section size is only divisible by rel. */
8507 if (use_rela_initialised && (use_rela == TRUE))
8508 {
8509 _bfd_error_handler
8510 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8511 bfd_set_error (bfd_error_invalid_operation);
8512 return 0;
8513 }
8514 else
8515 {
8516 use_rela = FALSE;
8517 use_rela_initialised = TRUE;
8518 }
8519 }
8520 else
8521 {
8522 /* The section size is not divisible by either - something is wrong. */
8523 _bfd_error_handler
8524 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8525 bfd_set_error (bfd_error_invalid_operation);
8526 return 0;
8527 }
8528 }
8529
8530 if (! use_rela_initialised)
8531 /* Make a guess. */
8532 use_rela = TRUE;
8533 }
8534 else if (rela_dyn != NULL && rela_dyn->size > 0)
8535 use_rela = TRUE;
8536 else if (rel_dyn != NULL && rel_dyn->size > 0)
8537 use_rela = FALSE;
8538 else
8539 return 0;
8540
8541 if (use_rela)
8542 {
8543 dynamic_relocs = rela_dyn;
8544 ext_size = bed->s->sizeof_rela;
8545 swap_in = bed->s->swap_reloca_in;
8546 swap_out = bed->s->swap_reloca_out;
8547 }
8548 else
8549 {
8550 dynamic_relocs = rel_dyn;
8551 ext_size = bed->s->sizeof_rel;
8552 swap_in = bed->s->swap_reloc_in;
8553 swap_out = bed->s->swap_reloc_out;
8554 }
8555
8556 size = 0;
8557 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8558 if (lo->type == bfd_indirect_link_order)
8559 size += lo->u.indirect.section->size;
8560
8561 if (size != dynamic_relocs->size)
8562 return 0;
8563
8564 sort_elt = (sizeof (struct elf_link_sort_rela)
8565 + (i2e - 1) * sizeof (Elf_Internal_Rela));
8566
8567 count = dynamic_relocs->size / ext_size;
8568 if (count == 0)
8569 return 0;
8570 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
8571
8572 if (sort == NULL)
8573 {
8574 (*info->callbacks->warning)
8575 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8576 return 0;
8577 }
8578
8579 if (bed->s->arch_size == 32)
8580 r_sym_mask = ~(bfd_vma) 0xff;
8581 else
8582 r_sym_mask = ~(bfd_vma) 0xffffffff;
8583
8584 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8585 if (lo->type == bfd_indirect_link_order)
8586 {
8587 bfd_byte *erel, *erelend;
8588 asection *o = lo->u.indirect.section;
8589
8590 if (o->contents == NULL && o->size != 0)
8591 {
8592 /* This is a reloc section that is being handled as a normal
8593 section. See bfd_section_from_shdr. We can't combine
8594 relocs in this case. */
8595 free (sort);
8596 return 0;
8597 }
8598 erel = o->contents;
8599 erelend = o->contents + o->size;
8600 /* FIXME: octets_per_byte. */
8601 p = sort + o->output_offset / ext_size * sort_elt;
8602
8603 while (erel < erelend)
8604 {
8605 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8606
8607 (*swap_in) (abfd, erel, s->rela);
8608 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
8609 s->u.sym_mask = r_sym_mask;
8610 p += sort_elt;
8611 erel += ext_size;
8612 }
8613 }
8614
8615 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8616
8617 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8618 {
8619 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8620 if (s->type != reloc_class_relative)
8621 break;
8622 }
8623 ret = i;
8624 s_non_relative = p;
8625
8626 sq = (struct elf_link_sort_rela *) s_non_relative;
8627 for (; i < count; i++, p += sort_elt)
8628 {
8629 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8630 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8631 sq = sp;
8632 sp->u.offset = sq->rela->r_offset;
8633 }
8634
8635 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8636
8637 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8638 if (lo->type == bfd_indirect_link_order)
8639 {
8640 bfd_byte *erel, *erelend;
8641 asection *o = lo->u.indirect.section;
8642
8643 erel = o->contents;
8644 erelend = o->contents + o->size;
8645 /* FIXME: octets_per_byte. */
8646 p = sort + o->output_offset / ext_size * sort_elt;
8647 while (erel < erelend)
8648 {
8649 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8650 (*swap_out) (abfd, s->rela, erel);
8651 p += sort_elt;
8652 erel += ext_size;
8653 }
8654 }
8655
8656 free (sort);
8657 *psec = dynamic_relocs;
8658 return ret;
8659 }
8660
8661 /* Add a symbol to the output symbol string table. */
8662
8663 static int
8664 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
8665 const char *name,
8666 Elf_Internal_Sym *elfsym,
8667 asection *input_sec,
8668 struct elf_link_hash_entry *h)
8669 {
8670 int (*output_symbol_hook)
8671 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8672 struct elf_link_hash_entry *);
8673 struct elf_link_hash_table *hash_table;
8674 const struct elf_backend_data *bed;
8675 bfd_size_type strtabsize;
8676
8677 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8678
8679 bed = get_elf_backend_data (flinfo->output_bfd);
8680 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8681 if (output_symbol_hook != NULL)
8682 {
8683 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
8684 if (ret != 1)
8685 return ret;
8686 }
8687
8688 if (name == NULL
8689 || *name == '\0'
8690 || (input_sec->flags & SEC_EXCLUDE))
8691 elfsym->st_name = (unsigned long) -1;
8692 else
8693 {
8694 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8695 to get the final offset for st_name. */
8696 elfsym->st_name
8697 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
8698 name, FALSE);
8699 if (elfsym->st_name == (unsigned long) -1)
8700 return 0;
8701 }
8702
8703 hash_table = elf_hash_table (flinfo->info);
8704 strtabsize = hash_table->strtabsize;
8705 if (strtabsize <= hash_table->strtabcount)
8706 {
8707 strtabsize += strtabsize;
8708 hash_table->strtabsize = strtabsize;
8709 strtabsize *= sizeof (*hash_table->strtab);
8710 hash_table->strtab
8711 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
8712 strtabsize);
8713 if (hash_table->strtab == NULL)
8714 return 0;
8715 }
8716 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
8717 hash_table->strtab[hash_table->strtabcount].dest_index
8718 = hash_table->strtabcount;
8719 hash_table->strtab[hash_table->strtabcount].destshndx_index
8720 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
8721
8722 bfd_get_symcount (flinfo->output_bfd) += 1;
8723 hash_table->strtabcount += 1;
8724
8725 return 1;
8726 }
8727
8728 /* Swap symbols out to the symbol table and flush the output symbols to
8729 the file. */
8730
8731 static bfd_boolean
8732 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
8733 {
8734 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
8735 bfd_size_type amt, i;
8736 const struct elf_backend_data *bed;
8737 bfd_byte *symbuf;
8738 Elf_Internal_Shdr *hdr;
8739 file_ptr pos;
8740 bfd_boolean ret;
8741
8742 if (!hash_table->strtabcount)
8743 return TRUE;
8744
8745 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8746
8747 bed = get_elf_backend_data (flinfo->output_bfd);
8748
8749 amt = bed->s->sizeof_sym * hash_table->strtabcount;
8750 symbuf = (bfd_byte *) bfd_malloc (amt);
8751 if (symbuf == NULL)
8752 return FALSE;
8753
8754 if (flinfo->symshndxbuf)
8755 {
8756 amt = (sizeof (Elf_External_Sym_Shndx)
8757 * (bfd_get_symcount (flinfo->output_bfd)));
8758 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
8759 if (flinfo->symshndxbuf == NULL)
8760 {
8761 free (symbuf);
8762 return FALSE;
8763 }
8764 }
8765
8766 for (i = 0; i < hash_table->strtabcount; i++)
8767 {
8768 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
8769 if (elfsym->sym.st_name == (unsigned long) -1)
8770 elfsym->sym.st_name = 0;
8771 else
8772 elfsym->sym.st_name
8773 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
8774 elfsym->sym.st_name);
8775 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
8776 ((bfd_byte *) symbuf
8777 + (elfsym->dest_index
8778 * bed->s->sizeof_sym)),
8779 (flinfo->symshndxbuf
8780 + elfsym->destshndx_index));
8781 }
8782
8783 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
8784 pos = hdr->sh_offset + hdr->sh_size;
8785 amt = hash_table->strtabcount * bed->s->sizeof_sym;
8786 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
8787 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
8788 {
8789 hdr->sh_size += amt;
8790 ret = TRUE;
8791 }
8792 else
8793 ret = FALSE;
8794
8795 free (symbuf);
8796
8797 free (hash_table->strtab);
8798 hash_table->strtab = NULL;
8799
8800 return ret;
8801 }
8802
8803 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8804
8805 static bfd_boolean
8806 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
8807 {
8808 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
8809 && sym->st_shndx < SHN_LORESERVE)
8810 {
8811 /* The gABI doesn't support dynamic symbols in output sections
8812 beyond 64k. */
8813 (*_bfd_error_handler)
8814 (_("%B: Too many sections: %d (>= %d)"),
8815 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
8816 bfd_set_error (bfd_error_nonrepresentable_section);
8817 return FALSE;
8818 }
8819 return TRUE;
8820 }
8821
8822 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8823 allowing an unsatisfied unversioned symbol in the DSO to match a
8824 versioned symbol that would normally require an explicit version.
8825 We also handle the case that a DSO references a hidden symbol
8826 which may be satisfied by a versioned symbol in another DSO. */
8827
8828 static bfd_boolean
8829 elf_link_check_versioned_symbol (struct bfd_link_info *info,
8830 const struct elf_backend_data *bed,
8831 struct elf_link_hash_entry *h)
8832 {
8833 bfd *abfd;
8834 struct elf_link_loaded_list *loaded;
8835
8836 if (!is_elf_hash_table (info->hash))
8837 return FALSE;
8838
8839 /* Check indirect symbol. */
8840 while (h->root.type == bfd_link_hash_indirect)
8841 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8842
8843 switch (h->root.type)
8844 {
8845 default:
8846 abfd = NULL;
8847 break;
8848
8849 case bfd_link_hash_undefined:
8850 case bfd_link_hash_undefweak:
8851 abfd = h->root.u.undef.abfd;
8852 if ((abfd->flags & DYNAMIC) == 0
8853 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
8854 return FALSE;
8855 break;
8856
8857 case bfd_link_hash_defined:
8858 case bfd_link_hash_defweak:
8859 abfd = h->root.u.def.section->owner;
8860 break;
8861
8862 case bfd_link_hash_common:
8863 abfd = h->root.u.c.p->section->owner;
8864 break;
8865 }
8866 BFD_ASSERT (abfd != NULL);
8867
8868 for (loaded = elf_hash_table (info)->loaded;
8869 loaded != NULL;
8870 loaded = loaded->next)
8871 {
8872 bfd *input;
8873 Elf_Internal_Shdr *hdr;
8874 bfd_size_type symcount;
8875 bfd_size_type extsymcount;
8876 bfd_size_type extsymoff;
8877 Elf_Internal_Shdr *versymhdr;
8878 Elf_Internal_Sym *isym;
8879 Elf_Internal_Sym *isymend;
8880 Elf_Internal_Sym *isymbuf;
8881 Elf_External_Versym *ever;
8882 Elf_External_Versym *extversym;
8883
8884 input = loaded->abfd;
8885
8886 /* We check each DSO for a possible hidden versioned definition. */
8887 if (input == abfd
8888 || (input->flags & DYNAMIC) == 0
8889 || elf_dynversym (input) == 0)
8890 continue;
8891
8892 hdr = &elf_tdata (input)->dynsymtab_hdr;
8893
8894 symcount = hdr->sh_size / bed->s->sizeof_sym;
8895 if (elf_bad_symtab (input))
8896 {
8897 extsymcount = symcount;
8898 extsymoff = 0;
8899 }
8900 else
8901 {
8902 extsymcount = symcount - hdr->sh_info;
8903 extsymoff = hdr->sh_info;
8904 }
8905
8906 if (extsymcount == 0)
8907 continue;
8908
8909 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
8910 NULL, NULL, NULL);
8911 if (isymbuf == NULL)
8912 return FALSE;
8913
8914 /* Read in any version definitions. */
8915 versymhdr = &elf_tdata (input)->dynversym_hdr;
8916 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
8917 if (extversym == NULL)
8918 goto error_ret;
8919
8920 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
8921 || (bfd_bread (extversym, versymhdr->sh_size, input)
8922 != versymhdr->sh_size))
8923 {
8924 free (extversym);
8925 error_ret:
8926 free (isymbuf);
8927 return FALSE;
8928 }
8929
8930 ever = extversym + extsymoff;
8931 isymend = isymbuf + extsymcount;
8932 for (isym = isymbuf; isym < isymend; isym++, ever++)
8933 {
8934 const char *name;
8935 Elf_Internal_Versym iver;
8936 unsigned short version_index;
8937
8938 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
8939 || isym->st_shndx == SHN_UNDEF)
8940 continue;
8941
8942 name = bfd_elf_string_from_elf_section (input,
8943 hdr->sh_link,
8944 isym->st_name);
8945 if (strcmp (name, h->root.root.string) != 0)
8946 continue;
8947
8948 _bfd_elf_swap_versym_in (input, ever, &iver);
8949
8950 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
8951 && !(h->def_regular
8952 && h->forced_local))
8953 {
8954 /* If we have a non-hidden versioned sym, then it should
8955 have provided a definition for the undefined sym unless
8956 it is defined in a non-shared object and forced local.
8957 */
8958 abort ();
8959 }
8960
8961 version_index = iver.vs_vers & VERSYM_VERSION;
8962 if (version_index == 1 || version_index == 2)
8963 {
8964 /* This is the base or first version. We can use it. */
8965 free (extversym);
8966 free (isymbuf);
8967 return TRUE;
8968 }
8969 }
8970
8971 free (extversym);
8972 free (isymbuf);
8973 }
8974
8975 return FALSE;
8976 }
8977
8978 /* Add an external symbol to the symbol table. This is called from
8979 the hash table traversal routine. When generating a shared object,
8980 we go through the symbol table twice. The first time we output
8981 anything that might have been forced to local scope in a version
8982 script. The second time we output the symbols that are still
8983 global symbols. */
8984
8985 static bfd_boolean
8986 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
8987 {
8988 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
8989 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8990 struct elf_final_link_info *flinfo = eoinfo->flinfo;
8991 bfd_boolean strip;
8992 Elf_Internal_Sym sym;
8993 asection *input_sec;
8994 const struct elf_backend_data *bed;
8995 long indx;
8996 int ret;
8997 /* A symbol is bound locally if it is forced local or it is locally
8998 defined, hidden versioned, not referenced by shared library and
8999 not exported when linking executable. */
9000 bfd_boolean local_bind = (h->forced_local
9001 || (bfd_link_executable (flinfo->info)
9002 && !flinfo->info->export_dynamic
9003 && !h->dynamic
9004 && !h->ref_dynamic
9005 && h->def_regular
9006 && h->versioned == versioned_hidden));
9007
9008 if (h->root.type == bfd_link_hash_warning)
9009 {
9010 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9011 if (h->root.type == bfd_link_hash_new)
9012 return TRUE;
9013 }
9014
9015 /* Decide whether to output this symbol in this pass. */
9016 if (eoinfo->localsyms)
9017 {
9018 if (!local_bind)
9019 return TRUE;
9020 }
9021 else
9022 {
9023 if (local_bind)
9024 return TRUE;
9025 }
9026
9027 bed = get_elf_backend_data (flinfo->output_bfd);
9028
9029 if (h->root.type == bfd_link_hash_undefined)
9030 {
9031 /* If we have an undefined symbol reference here then it must have
9032 come from a shared library that is being linked in. (Undefined
9033 references in regular files have already been handled unless
9034 they are in unreferenced sections which are removed by garbage
9035 collection). */
9036 bfd_boolean ignore_undef = FALSE;
9037
9038 /* Some symbols may be special in that the fact that they're
9039 undefined can be safely ignored - let backend determine that. */
9040 if (bed->elf_backend_ignore_undef_symbol)
9041 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9042
9043 /* If we are reporting errors for this situation then do so now. */
9044 if (!ignore_undef
9045 && h->ref_dynamic
9046 && (!h->ref_regular || flinfo->info->gc_sections)
9047 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9048 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9049 {
9050 if (!(flinfo->info->callbacks->undefined_symbol
9051 (flinfo->info, h->root.root.string,
9052 h->ref_regular ? NULL : h->root.u.undef.abfd,
9053 NULL, 0,
9054 (flinfo->info->unresolved_syms_in_shared_libs
9055 == RM_GENERATE_ERROR))))
9056 {
9057 bfd_set_error (bfd_error_bad_value);
9058 eoinfo->failed = TRUE;
9059 return FALSE;
9060 }
9061 }
9062 }
9063
9064 /* We should also warn if a forced local symbol is referenced from
9065 shared libraries. */
9066 if (bfd_link_executable (flinfo->info)
9067 && h->forced_local
9068 && h->ref_dynamic
9069 && h->def_regular
9070 && !h->dynamic_def
9071 && h->ref_dynamic_nonweak
9072 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9073 {
9074 bfd *def_bfd;
9075 const char *msg;
9076 struct elf_link_hash_entry *hi = h;
9077
9078 /* Check indirect symbol. */
9079 while (hi->root.type == bfd_link_hash_indirect)
9080 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9081
9082 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9083 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9084 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9085 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9086 else
9087 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
9088 def_bfd = flinfo->output_bfd;
9089 if (hi->root.u.def.section != bfd_abs_section_ptr)
9090 def_bfd = hi->root.u.def.section->owner;
9091 (*_bfd_error_handler) (msg, flinfo->output_bfd, def_bfd,
9092 h->root.root.string);
9093 bfd_set_error (bfd_error_bad_value);
9094 eoinfo->failed = TRUE;
9095 return FALSE;
9096 }
9097
9098 /* We don't want to output symbols that have never been mentioned by
9099 a regular file, or that we have been told to strip. However, if
9100 h->indx is set to -2, the symbol is used by a reloc and we must
9101 output it. */
9102 strip = FALSE;
9103 if (h->indx == -2)
9104 ;
9105 else if ((h->def_dynamic
9106 || h->ref_dynamic
9107 || h->root.type == bfd_link_hash_new)
9108 && !h->def_regular
9109 && !h->ref_regular)
9110 strip = TRUE;
9111 else if (flinfo->info->strip == strip_all)
9112 strip = TRUE;
9113 else if (flinfo->info->strip == strip_some
9114 && bfd_hash_lookup (flinfo->info->keep_hash,
9115 h->root.root.string, FALSE, FALSE) == NULL)
9116 strip = TRUE;
9117 else if ((h->root.type == bfd_link_hash_defined
9118 || h->root.type == bfd_link_hash_defweak)
9119 && ((flinfo->info->strip_discarded
9120 && discarded_section (h->root.u.def.section))
9121 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9122 && h->root.u.def.section->owner != NULL
9123 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9124 strip = TRUE;
9125 else if ((h->root.type == bfd_link_hash_undefined
9126 || h->root.type == bfd_link_hash_undefweak)
9127 && h->root.u.undef.abfd != NULL
9128 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9129 strip = TRUE;
9130
9131 /* If we're stripping it, and it's not a dynamic symbol, there's
9132 nothing else to do. However, if it is a forced local symbol or
9133 an ifunc symbol we need to give the backend finish_dynamic_symbol
9134 function a chance to make it dynamic. */
9135 if (strip
9136 && h->dynindx == -1
9137 && h->type != STT_GNU_IFUNC
9138 && !h->forced_local)
9139 return TRUE;
9140
9141 sym.st_value = 0;
9142 sym.st_size = h->size;
9143 sym.st_other = h->other;
9144 if (local_bind)
9145 {
9146 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
9147 /* Turn off visibility on local symbol. */
9148 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9149 }
9150 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9151 else if (h->unique_global && h->def_regular)
9152 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, h->type);
9153 else if (h->root.type == bfd_link_hash_undefweak
9154 || h->root.type == bfd_link_hash_defweak)
9155 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
9156 else
9157 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
9158 sym.st_target_internal = h->target_internal;
9159
9160 switch (h->root.type)
9161 {
9162 default:
9163 case bfd_link_hash_new:
9164 case bfd_link_hash_warning:
9165 abort ();
9166 return FALSE;
9167
9168 case bfd_link_hash_undefined:
9169 case bfd_link_hash_undefweak:
9170 input_sec = bfd_und_section_ptr;
9171 sym.st_shndx = SHN_UNDEF;
9172 break;
9173
9174 case bfd_link_hash_defined:
9175 case bfd_link_hash_defweak:
9176 {
9177 input_sec = h->root.u.def.section;
9178 if (input_sec->output_section != NULL)
9179 {
9180 sym.st_shndx =
9181 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9182 input_sec->output_section);
9183 if (sym.st_shndx == SHN_BAD)
9184 {
9185 (*_bfd_error_handler)
9186 (_("%B: could not find output section %A for input section %A"),
9187 flinfo->output_bfd, input_sec->output_section, input_sec);
9188 bfd_set_error (bfd_error_nonrepresentable_section);
9189 eoinfo->failed = TRUE;
9190 return FALSE;
9191 }
9192
9193 /* ELF symbols in relocatable files are section relative,
9194 but in nonrelocatable files they are virtual
9195 addresses. */
9196 sym.st_value = h->root.u.def.value + input_sec->output_offset;
9197 if (!bfd_link_relocatable (flinfo->info))
9198 {
9199 sym.st_value += input_sec->output_section->vma;
9200 if (h->type == STT_TLS)
9201 {
9202 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9203 if (tls_sec != NULL)
9204 sym.st_value -= tls_sec->vma;
9205 }
9206 }
9207 }
9208 else
9209 {
9210 BFD_ASSERT (input_sec->owner == NULL
9211 || (input_sec->owner->flags & DYNAMIC) != 0);
9212 sym.st_shndx = SHN_UNDEF;
9213 input_sec = bfd_und_section_ptr;
9214 }
9215 }
9216 break;
9217
9218 case bfd_link_hash_common:
9219 input_sec = h->root.u.c.p->section;
9220 sym.st_shndx = bed->common_section_index (input_sec);
9221 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9222 break;
9223
9224 case bfd_link_hash_indirect:
9225 /* These symbols are created by symbol versioning. They point
9226 to the decorated version of the name. For example, if the
9227 symbol foo@@GNU_1.2 is the default, which should be used when
9228 foo is used with no version, then we add an indirect symbol
9229 foo which points to foo@@GNU_1.2. We ignore these symbols,
9230 since the indirected symbol is already in the hash table. */
9231 return TRUE;
9232 }
9233
9234 /* Give the processor backend a chance to tweak the symbol value,
9235 and also to finish up anything that needs to be done for this
9236 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9237 forced local syms when non-shared is due to a historical quirk.
9238 STT_GNU_IFUNC symbol must go through PLT. */
9239 if ((h->type == STT_GNU_IFUNC
9240 && h->def_regular
9241 && !bfd_link_relocatable (flinfo->info))
9242 || ((h->dynindx != -1
9243 || h->forced_local)
9244 && ((bfd_link_pic (flinfo->info)
9245 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9246 || h->root.type != bfd_link_hash_undefweak))
9247 || !h->forced_local)
9248 && elf_hash_table (flinfo->info)->dynamic_sections_created))
9249 {
9250 if (! ((*bed->elf_backend_finish_dynamic_symbol)
9251 (flinfo->output_bfd, flinfo->info, h, &sym)))
9252 {
9253 eoinfo->failed = TRUE;
9254 return FALSE;
9255 }
9256 }
9257
9258 /* If we are marking the symbol as undefined, and there are no
9259 non-weak references to this symbol from a regular object, then
9260 mark the symbol as weak undefined; if there are non-weak
9261 references, mark the symbol as strong. We can't do this earlier,
9262 because it might not be marked as undefined until the
9263 finish_dynamic_symbol routine gets through with it. */
9264 if (sym.st_shndx == SHN_UNDEF
9265 && h->ref_regular
9266 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9267 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9268 {
9269 int bindtype;
9270 unsigned int type = ELF_ST_TYPE (sym.st_info);
9271
9272 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9273 if (type == STT_GNU_IFUNC)
9274 type = STT_FUNC;
9275
9276 if (h->ref_regular_nonweak)
9277 bindtype = STB_GLOBAL;
9278 else
9279 bindtype = STB_WEAK;
9280 sym.st_info = ELF_ST_INFO (bindtype, type);
9281 }
9282
9283 /* If this is a symbol defined in a dynamic library, don't use the
9284 symbol size from the dynamic library. Relinking an executable
9285 against a new library may introduce gratuitous changes in the
9286 executable's symbols if we keep the size. */
9287 if (sym.st_shndx == SHN_UNDEF
9288 && !h->def_regular
9289 && h->def_dynamic)
9290 sym.st_size = 0;
9291
9292 /* If a non-weak symbol with non-default visibility is not defined
9293 locally, it is a fatal error. */
9294 if (!bfd_link_relocatable (flinfo->info)
9295 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9296 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9297 && h->root.type == bfd_link_hash_undefined
9298 && !h->def_regular)
9299 {
9300 const char *msg;
9301
9302 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9303 msg = _("%B: protected symbol `%s' isn't defined");
9304 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9305 msg = _("%B: internal symbol `%s' isn't defined");
9306 else
9307 msg = _("%B: hidden symbol `%s' isn't defined");
9308 (*_bfd_error_handler) (msg, flinfo->output_bfd, h->root.root.string);
9309 bfd_set_error (bfd_error_bad_value);
9310 eoinfo->failed = TRUE;
9311 return FALSE;
9312 }
9313
9314 /* If this symbol should be put in the .dynsym section, then put it
9315 there now. We already know the symbol index. We also fill in
9316 the entry in the .hash section. */
9317 if (flinfo->dynsym_sec != NULL
9318 && h->dynindx != -1
9319 && elf_hash_table (flinfo->info)->dynamic_sections_created)
9320 {
9321 bfd_byte *esym;
9322
9323 /* Since there is no version information in the dynamic string,
9324 if there is no version info in symbol version section, we will
9325 have a run-time problem if not linking executable, referenced
9326 by shared library, not locally defined, or not bound locally.
9327 */
9328 if (h->verinfo.verdef == NULL
9329 && !local_bind
9330 && (!bfd_link_executable (flinfo->info)
9331 || h->ref_dynamic
9332 || !h->def_regular))
9333 {
9334 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9335
9336 if (p && p [1] != '\0')
9337 {
9338 (*_bfd_error_handler)
9339 (_("%B: No symbol version section for versioned symbol `%s'"),
9340 flinfo->output_bfd, h->root.root.string);
9341 eoinfo->failed = TRUE;
9342 return FALSE;
9343 }
9344 }
9345
9346 sym.st_name = h->dynstr_index;
9347 esym = flinfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
9348 if (!check_dynsym (flinfo->output_bfd, &sym))
9349 {
9350 eoinfo->failed = TRUE;
9351 return FALSE;
9352 }
9353 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
9354
9355 if (flinfo->hash_sec != NULL)
9356 {
9357 size_t hash_entry_size;
9358 bfd_byte *bucketpos;
9359 bfd_vma chain;
9360 size_t bucketcount;
9361 size_t bucket;
9362
9363 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
9364 bucket = h->u.elf_hash_value % bucketcount;
9365
9366 hash_entry_size
9367 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9368 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
9369 + (bucket + 2) * hash_entry_size);
9370 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9371 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9372 bucketpos);
9373 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9374 ((bfd_byte *) flinfo->hash_sec->contents
9375 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9376 }
9377
9378 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
9379 {
9380 Elf_Internal_Versym iversym;
9381 Elf_External_Versym *eversym;
9382
9383 if (!h->def_regular)
9384 {
9385 if (h->verinfo.verdef == NULL
9386 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9387 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
9388 iversym.vs_vers = 0;
9389 else
9390 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9391 }
9392 else
9393 {
9394 if (h->verinfo.vertree == NULL)
9395 iversym.vs_vers = 1;
9396 else
9397 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
9398 if (flinfo->info->create_default_symver)
9399 iversym.vs_vers++;
9400 }
9401
9402 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9403 defined locally. */
9404 if (h->versioned == versioned_hidden && h->def_regular)
9405 iversym.vs_vers |= VERSYM_HIDDEN;
9406
9407 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
9408 eversym += h->dynindx;
9409 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
9410 }
9411 }
9412
9413 /* If the symbol is undefined, and we didn't output it to .dynsym,
9414 strip it from .symtab too. Obviously we can't do this for
9415 relocatable output or when needed for --emit-relocs. */
9416 else if (input_sec == bfd_und_section_ptr
9417 && h->indx != -2
9418 && !bfd_link_relocatable (flinfo->info))
9419 return TRUE;
9420 /* Also strip others that we couldn't earlier due to dynamic symbol
9421 processing. */
9422 if (strip)
9423 return TRUE;
9424 if ((input_sec->flags & SEC_EXCLUDE) != 0)
9425 return TRUE;
9426
9427 /* Output a FILE symbol so that following locals are not associated
9428 with the wrong input file. We need one for forced local symbols
9429 if we've seen more than one FILE symbol or when we have exactly
9430 one FILE symbol but global symbols are present in a file other
9431 than the one with the FILE symbol. We also need one if linker
9432 defined symbols are present. In practice these conditions are
9433 always met, so just emit the FILE symbol unconditionally. */
9434 if (eoinfo->localsyms
9435 && !eoinfo->file_sym_done
9436 && eoinfo->flinfo->filesym_count != 0)
9437 {
9438 Elf_Internal_Sym fsym;
9439
9440 memset (&fsym, 0, sizeof (fsym));
9441 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9442 fsym.st_shndx = SHN_ABS;
9443 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9444 bfd_und_section_ptr, NULL))
9445 return FALSE;
9446
9447 eoinfo->file_sym_done = TRUE;
9448 }
9449
9450 indx = bfd_get_symcount (flinfo->output_bfd);
9451 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9452 input_sec, h);
9453 if (ret == 0)
9454 {
9455 eoinfo->failed = TRUE;
9456 return FALSE;
9457 }
9458 else if (ret == 1)
9459 h->indx = indx;
9460 else if (h->indx == -2)
9461 abort();
9462
9463 return TRUE;
9464 }
9465
9466 /* Return TRUE if special handling is done for relocs in SEC against
9467 symbols defined in discarded sections. */
9468
9469 static bfd_boolean
9470 elf_section_ignore_discarded_relocs (asection *sec)
9471 {
9472 const struct elf_backend_data *bed;
9473
9474 switch (sec->sec_info_type)
9475 {
9476 case SEC_INFO_TYPE_STABS:
9477 case SEC_INFO_TYPE_EH_FRAME:
9478 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
9479 return TRUE;
9480 default:
9481 break;
9482 }
9483
9484 bed = get_elf_backend_data (sec->owner);
9485 if (bed->elf_backend_ignore_discarded_relocs != NULL
9486 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9487 return TRUE;
9488
9489 return FALSE;
9490 }
9491
9492 /* Return a mask saying how ld should treat relocations in SEC against
9493 symbols defined in discarded sections. If this function returns
9494 COMPLAIN set, ld will issue a warning message. If this function
9495 returns PRETEND set, and the discarded section was link-once and the
9496 same size as the kept link-once section, ld will pretend that the
9497 symbol was actually defined in the kept section. Otherwise ld will
9498 zero the reloc (at least that is the intent, but some cooperation by
9499 the target dependent code is needed, particularly for REL targets). */
9500
9501 unsigned int
9502 _bfd_elf_default_action_discarded (asection *sec)
9503 {
9504 if (sec->flags & SEC_DEBUGGING)
9505 return PRETEND;
9506
9507 if (strcmp (".eh_frame", sec->name) == 0)
9508 return 0;
9509
9510 if (strcmp (".gcc_except_table", sec->name) == 0)
9511 return 0;
9512
9513 return COMPLAIN | PRETEND;
9514 }
9515
9516 /* Find a match between a section and a member of a section group. */
9517
9518 static asection *
9519 match_group_member (asection *sec, asection *group,
9520 struct bfd_link_info *info)
9521 {
9522 asection *first = elf_next_in_group (group);
9523 asection *s = first;
9524
9525 while (s != NULL)
9526 {
9527 if (bfd_elf_match_symbols_in_sections (s, sec, info))
9528 return s;
9529
9530 s = elf_next_in_group (s);
9531 if (s == first)
9532 break;
9533 }
9534
9535 return NULL;
9536 }
9537
9538 /* Check if the kept section of a discarded section SEC can be used
9539 to replace it. Return the replacement if it is OK. Otherwise return
9540 NULL. */
9541
9542 asection *
9543 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
9544 {
9545 asection *kept;
9546
9547 kept = sec->kept_section;
9548 if (kept != NULL)
9549 {
9550 if ((kept->flags & SEC_GROUP) != 0)
9551 kept = match_group_member (sec, kept, info);
9552 if (kept != NULL
9553 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9554 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
9555 kept = NULL;
9556 sec->kept_section = kept;
9557 }
9558 return kept;
9559 }
9560
9561 /* Link an input file into the linker output file. This function
9562 handles all the sections and relocations of the input file at once.
9563 This is so that we only have to read the local symbols once, and
9564 don't have to keep them in memory. */
9565
9566 static bfd_boolean
9567 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
9568 {
9569 int (*relocate_section)
9570 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9571 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9572 bfd *output_bfd;
9573 Elf_Internal_Shdr *symtab_hdr;
9574 size_t locsymcount;
9575 size_t extsymoff;
9576 Elf_Internal_Sym *isymbuf;
9577 Elf_Internal_Sym *isym;
9578 Elf_Internal_Sym *isymend;
9579 long *pindex;
9580 asection **ppsection;
9581 asection *o;
9582 const struct elf_backend_data *bed;
9583 struct elf_link_hash_entry **sym_hashes;
9584 bfd_size_type address_size;
9585 bfd_vma r_type_mask;
9586 int r_sym_shift;
9587 bfd_boolean have_file_sym = FALSE;
9588
9589 output_bfd = flinfo->output_bfd;
9590 bed = get_elf_backend_data (output_bfd);
9591 relocate_section = bed->elf_backend_relocate_section;
9592
9593 /* If this is a dynamic object, we don't want to do anything here:
9594 we don't want the local symbols, and we don't want the section
9595 contents. */
9596 if ((input_bfd->flags & DYNAMIC) != 0)
9597 return TRUE;
9598
9599 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9600 if (elf_bad_symtab (input_bfd))
9601 {
9602 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9603 extsymoff = 0;
9604 }
9605 else
9606 {
9607 locsymcount = symtab_hdr->sh_info;
9608 extsymoff = symtab_hdr->sh_info;
9609 }
9610
9611 /* Read the local symbols. */
9612 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9613 if (isymbuf == NULL && locsymcount != 0)
9614 {
9615 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
9616 flinfo->internal_syms,
9617 flinfo->external_syms,
9618 flinfo->locsym_shndx);
9619 if (isymbuf == NULL)
9620 return FALSE;
9621 }
9622
9623 /* Find local symbol sections and adjust values of symbols in
9624 SEC_MERGE sections. Write out those local symbols we know are
9625 going into the output file. */
9626 isymend = isymbuf + locsymcount;
9627 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
9628 isym < isymend;
9629 isym++, pindex++, ppsection++)
9630 {
9631 asection *isec;
9632 const char *name;
9633 Elf_Internal_Sym osym;
9634 long indx;
9635 int ret;
9636
9637 *pindex = -1;
9638
9639 if (elf_bad_symtab (input_bfd))
9640 {
9641 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9642 {
9643 *ppsection = NULL;
9644 continue;
9645 }
9646 }
9647
9648 if (isym->st_shndx == SHN_UNDEF)
9649 isec = bfd_und_section_ptr;
9650 else if (isym->st_shndx == SHN_ABS)
9651 isec = bfd_abs_section_ptr;
9652 else if (isym->st_shndx == SHN_COMMON)
9653 isec = bfd_com_section_ptr;
9654 else
9655 {
9656 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9657 if (isec == NULL)
9658 {
9659 /* Don't attempt to output symbols with st_shnx in the
9660 reserved range other than SHN_ABS and SHN_COMMON. */
9661 *ppsection = NULL;
9662 continue;
9663 }
9664 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
9665 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9666 isym->st_value =
9667 _bfd_merged_section_offset (output_bfd, &isec,
9668 elf_section_data (isec)->sec_info,
9669 isym->st_value);
9670 }
9671
9672 *ppsection = isec;
9673
9674 /* Don't output the first, undefined, symbol. In fact, don't
9675 output any undefined local symbol. */
9676 if (isec == bfd_und_section_ptr)
9677 continue;
9678
9679 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9680 {
9681 /* We never output section symbols. Instead, we use the
9682 section symbol of the corresponding section in the output
9683 file. */
9684 continue;
9685 }
9686
9687 /* If we are stripping all symbols, we don't want to output this
9688 one. */
9689 if (flinfo->info->strip == strip_all)
9690 continue;
9691
9692 /* If we are discarding all local symbols, we don't want to
9693 output this one. If we are generating a relocatable output
9694 file, then some of the local symbols may be required by
9695 relocs; we output them below as we discover that they are
9696 needed. */
9697 if (flinfo->info->discard == discard_all)
9698 continue;
9699
9700 /* If this symbol is defined in a section which we are
9701 discarding, we don't need to keep it. */
9702 if (isym->st_shndx != SHN_UNDEF
9703 && isym->st_shndx < SHN_LORESERVE
9704 && bfd_section_removed_from_list (output_bfd,
9705 isec->output_section))
9706 continue;
9707
9708 /* Get the name of the symbol. */
9709 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9710 isym->st_name);
9711 if (name == NULL)
9712 return FALSE;
9713
9714 /* See if we are discarding symbols with this name. */
9715 if ((flinfo->info->strip == strip_some
9716 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
9717 == NULL))
9718 || (((flinfo->info->discard == discard_sec_merge
9719 && (isec->flags & SEC_MERGE)
9720 && !bfd_link_relocatable (flinfo->info))
9721 || flinfo->info->discard == discard_l)
9722 && bfd_is_local_label_name (input_bfd, name)))
9723 continue;
9724
9725 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
9726 {
9727 if (input_bfd->lto_output)
9728 /* -flto puts a temp file name here. This means builds
9729 are not reproducible. Discard the symbol. */
9730 continue;
9731 have_file_sym = TRUE;
9732 flinfo->filesym_count += 1;
9733 }
9734 if (!have_file_sym)
9735 {
9736 /* In the absence of debug info, bfd_find_nearest_line uses
9737 FILE symbols to determine the source file for local
9738 function symbols. Provide a FILE symbol here if input
9739 files lack such, so that their symbols won't be
9740 associated with a previous input file. It's not the
9741 source file, but the best we can do. */
9742 have_file_sym = TRUE;
9743 flinfo->filesym_count += 1;
9744 memset (&osym, 0, sizeof (osym));
9745 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9746 osym.st_shndx = SHN_ABS;
9747 if (!elf_link_output_symstrtab (flinfo,
9748 (input_bfd->lto_output ? NULL
9749 : input_bfd->filename),
9750 &osym, bfd_abs_section_ptr,
9751 NULL))
9752 return FALSE;
9753 }
9754
9755 osym = *isym;
9756
9757 /* Adjust the section index for the output file. */
9758 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9759 isec->output_section);
9760 if (osym.st_shndx == SHN_BAD)
9761 return FALSE;
9762
9763 /* ELF symbols in relocatable files are section relative, but
9764 in executable files they are virtual addresses. Note that
9765 this code assumes that all ELF sections have an associated
9766 BFD section with a reasonable value for output_offset; below
9767 we assume that they also have a reasonable value for
9768 output_section. Any special sections must be set up to meet
9769 these requirements. */
9770 osym.st_value += isec->output_offset;
9771 if (!bfd_link_relocatable (flinfo->info))
9772 {
9773 osym.st_value += isec->output_section->vma;
9774 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
9775 {
9776 /* STT_TLS symbols are relative to PT_TLS segment base. */
9777 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
9778 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
9779 }
9780 }
9781
9782 indx = bfd_get_symcount (output_bfd);
9783 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
9784 if (ret == 0)
9785 return FALSE;
9786 else if (ret == 1)
9787 *pindex = indx;
9788 }
9789
9790 if (bed->s->arch_size == 32)
9791 {
9792 r_type_mask = 0xff;
9793 r_sym_shift = 8;
9794 address_size = 4;
9795 }
9796 else
9797 {
9798 r_type_mask = 0xffffffff;
9799 r_sym_shift = 32;
9800 address_size = 8;
9801 }
9802
9803 /* Relocate the contents of each section. */
9804 sym_hashes = elf_sym_hashes (input_bfd);
9805 for (o = input_bfd->sections; o != NULL; o = o->next)
9806 {
9807 bfd_byte *contents;
9808
9809 if (! o->linker_mark)
9810 {
9811 /* This section was omitted from the link. */
9812 continue;
9813 }
9814
9815 if (bfd_link_relocatable (flinfo->info)
9816 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
9817 {
9818 /* Deal with the group signature symbol. */
9819 struct bfd_elf_section_data *sec_data = elf_section_data (o);
9820 unsigned long symndx = sec_data->this_hdr.sh_info;
9821 asection *osec = o->output_section;
9822
9823 if (symndx >= locsymcount
9824 || (elf_bad_symtab (input_bfd)
9825 && flinfo->sections[symndx] == NULL))
9826 {
9827 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
9828 while (h->root.type == bfd_link_hash_indirect
9829 || h->root.type == bfd_link_hash_warning)
9830 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9831 /* Arrange for symbol to be output. */
9832 h->indx = -2;
9833 elf_section_data (osec)->this_hdr.sh_info = -2;
9834 }
9835 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
9836 {
9837 /* We'll use the output section target_index. */
9838 asection *sec = flinfo->sections[symndx]->output_section;
9839 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
9840 }
9841 else
9842 {
9843 if (flinfo->indices[symndx] == -1)
9844 {
9845 /* Otherwise output the local symbol now. */
9846 Elf_Internal_Sym sym = isymbuf[symndx];
9847 asection *sec = flinfo->sections[symndx]->output_section;
9848 const char *name;
9849 long indx;
9850 int ret;
9851
9852 name = bfd_elf_string_from_elf_section (input_bfd,
9853 symtab_hdr->sh_link,
9854 sym.st_name);
9855 if (name == NULL)
9856 return FALSE;
9857
9858 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9859 sec);
9860 if (sym.st_shndx == SHN_BAD)
9861 return FALSE;
9862
9863 sym.st_value += o->output_offset;
9864
9865 indx = bfd_get_symcount (output_bfd);
9866 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
9867 NULL);
9868 if (ret == 0)
9869 return FALSE;
9870 else if (ret == 1)
9871 flinfo->indices[symndx] = indx;
9872 else
9873 abort ();
9874 }
9875 elf_section_data (osec)->this_hdr.sh_info
9876 = flinfo->indices[symndx];
9877 }
9878 }
9879
9880 if ((o->flags & SEC_HAS_CONTENTS) == 0
9881 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
9882 continue;
9883
9884 if ((o->flags & SEC_LINKER_CREATED) != 0)
9885 {
9886 /* Section was created by _bfd_elf_link_create_dynamic_sections
9887 or somesuch. */
9888 continue;
9889 }
9890
9891 /* Get the contents of the section. They have been cached by a
9892 relaxation routine. Note that o is a section in an input
9893 file, so the contents field will not have been set by any of
9894 the routines which work on output files. */
9895 if (elf_section_data (o)->this_hdr.contents != NULL)
9896 {
9897 contents = elf_section_data (o)->this_hdr.contents;
9898 if (bed->caches_rawsize
9899 && o->rawsize != 0
9900 && o->rawsize < o->size)
9901 {
9902 memcpy (flinfo->contents, contents, o->rawsize);
9903 contents = flinfo->contents;
9904 }
9905 }
9906 else
9907 {
9908 contents = flinfo->contents;
9909 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
9910 return FALSE;
9911 }
9912
9913 if ((o->flags & SEC_RELOC) != 0)
9914 {
9915 Elf_Internal_Rela *internal_relocs;
9916 Elf_Internal_Rela *rel, *relend;
9917 int action_discarded;
9918 int ret;
9919
9920 /* Get the swapped relocs. */
9921 internal_relocs
9922 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
9923 flinfo->internal_relocs, FALSE);
9924 if (internal_relocs == NULL
9925 && o->reloc_count > 0)
9926 return FALSE;
9927
9928 /* We need to reverse-copy input .ctors/.dtors sections if
9929 they are placed in .init_array/.finit_array for output. */
9930 if (o->size > address_size
9931 && ((strncmp (o->name, ".ctors", 6) == 0
9932 && strcmp (o->output_section->name,
9933 ".init_array") == 0)
9934 || (strncmp (o->name, ".dtors", 6) == 0
9935 && strcmp (o->output_section->name,
9936 ".fini_array") == 0))
9937 && (o->name[6] == 0 || o->name[6] == '.'))
9938 {
9939 if (o->size != o->reloc_count * address_size)
9940 {
9941 (*_bfd_error_handler)
9942 (_("error: %B: size of section %A is not "
9943 "multiple of address size"),
9944 input_bfd, o);
9945 bfd_set_error (bfd_error_on_input);
9946 return FALSE;
9947 }
9948 o->flags |= SEC_ELF_REVERSE_COPY;
9949 }
9950
9951 action_discarded = -1;
9952 if (!elf_section_ignore_discarded_relocs (o))
9953 action_discarded = (*bed->action_discarded) (o);
9954
9955 /* Run through the relocs evaluating complex reloc symbols and
9956 looking for relocs against symbols from discarded sections
9957 or section symbols from removed link-once sections.
9958 Complain about relocs against discarded sections. Zero
9959 relocs against removed link-once sections. */
9960
9961 rel = internal_relocs;
9962 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
9963 for ( ; rel < relend; rel++)
9964 {
9965 unsigned long r_symndx = rel->r_info >> r_sym_shift;
9966 unsigned int s_type;
9967 asection **ps, *sec;
9968 struct elf_link_hash_entry *h = NULL;
9969 const char *sym_name;
9970
9971 if (r_symndx == STN_UNDEF)
9972 continue;
9973
9974 if (r_symndx >= locsymcount
9975 || (elf_bad_symtab (input_bfd)
9976 && flinfo->sections[r_symndx] == NULL))
9977 {
9978 h = sym_hashes[r_symndx - extsymoff];
9979
9980 /* Badly formatted input files can contain relocs that
9981 reference non-existant symbols. Check here so that
9982 we do not seg fault. */
9983 if (h == NULL)
9984 {
9985 char buffer [32];
9986
9987 sprintf_vma (buffer, rel->r_info);
9988 (*_bfd_error_handler)
9989 (_("error: %B contains a reloc (0x%s) for section %A "
9990 "that references a non-existent global symbol"),
9991 input_bfd, o, buffer);
9992 bfd_set_error (bfd_error_bad_value);
9993 return FALSE;
9994 }
9995
9996 while (h->root.type == bfd_link_hash_indirect
9997 || h->root.type == bfd_link_hash_warning)
9998 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9999
10000 s_type = h->type;
10001
10002 /* If a plugin symbol is referenced from a non-IR file,
10003 mark the symbol as undefined. Note that the
10004 linker may attach linker created dynamic sections
10005 to the plugin bfd. Symbols defined in linker
10006 created sections are not plugin symbols. */
10007 if (h->root.non_ir_ref
10008 && (h->root.type == bfd_link_hash_defined
10009 || h->root.type == bfd_link_hash_defweak)
10010 && (h->root.u.def.section->flags
10011 & SEC_LINKER_CREATED) == 0
10012 && h->root.u.def.section->owner != NULL
10013 && (h->root.u.def.section->owner->flags
10014 & BFD_PLUGIN) != 0)
10015 {
10016 h->root.type = bfd_link_hash_undefined;
10017 h->root.u.undef.abfd = h->root.u.def.section->owner;
10018 }
10019
10020 ps = NULL;
10021 if (h->root.type == bfd_link_hash_defined
10022 || h->root.type == bfd_link_hash_defweak)
10023 ps = &h->root.u.def.section;
10024
10025 sym_name = h->root.root.string;
10026 }
10027 else
10028 {
10029 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10030
10031 s_type = ELF_ST_TYPE (sym->st_info);
10032 ps = &flinfo->sections[r_symndx];
10033 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10034 sym, *ps);
10035 }
10036
10037 if ((s_type == STT_RELC || s_type == STT_SRELC)
10038 && !bfd_link_relocatable (flinfo->info))
10039 {
10040 bfd_vma val;
10041 bfd_vma dot = (rel->r_offset
10042 + o->output_offset + o->output_section->vma);
10043 #ifdef DEBUG
10044 printf ("Encountered a complex symbol!");
10045 printf (" (input_bfd %s, section %s, reloc %ld\n",
10046 input_bfd->filename, o->name,
10047 (long) (rel - internal_relocs));
10048 printf (" symbol: idx %8.8lx, name %s\n",
10049 r_symndx, sym_name);
10050 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10051 (unsigned long) rel->r_info,
10052 (unsigned long) rel->r_offset);
10053 #endif
10054 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10055 isymbuf, locsymcount, s_type == STT_SRELC))
10056 return FALSE;
10057
10058 /* Symbol evaluated OK. Update to absolute value. */
10059 set_symbol_value (input_bfd, isymbuf, locsymcount,
10060 r_symndx, val);
10061 continue;
10062 }
10063
10064 if (action_discarded != -1 && ps != NULL)
10065 {
10066 /* Complain if the definition comes from a
10067 discarded section. */
10068 if ((sec = *ps) != NULL && discarded_section (sec))
10069 {
10070 BFD_ASSERT (r_symndx != STN_UNDEF);
10071 if (action_discarded & COMPLAIN)
10072 (*flinfo->info->callbacks->einfo)
10073 (_("%X`%s' referenced in section `%A' of %B: "
10074 "defined in discarded section `%A' of %B\n"),
10075 sym_name, o, input_bfd, sec, sec->owner);
10076
10077 /* Try to do the best we can to support buggy old
10078 versions of gcc. Pretend that the symbol is
10079 really defined in the kept linkonce section.
10080 FIXME: This is quite broken. Modifying the
10081 symbol here means we will be changing all later
10082 uses of the symbol, not just in this section. */
10083 if (action_discarded & PRETEND)
10084 {
10085 asection *kept;
10086
10087 kept = _bfd_elf_check_kept_section (sec,
10088 flinfo->info);
10089 if (kept != NULL)
10090 {
10091 *ps = kept;
10092 continue;
10093 }
10094 }
10095 }
10096 }
10097 }
10098
10099 /* Relocate the section by invoking a back end routine.
10100
10101 The back end routine is responsible for adjusting the
10102 section contents as necessary, and (if using Rela relocs
10103 and generating a relocatable output file) adjusting the
10104 reloc addend as necessary.
10105
10106 The back end routine does not have to worry about setting
10107 the reloc address or the reloc symbol index.
10108
10109 The back end routine is given a pointer to the swapped in
10110 internal symbols, and can access the hash table entries
10111 for the external symbols via elf_sym_hashes (input_bfd).
10112
10113 When generating relocatable output, the back end routine
10114 must handle STB_LOCAL/STT_SECTION symbols specially. The
10115 output symbol is going to be a section symbol
10116 corresponding to the output section, which will require
10117 the addend to be adjusted. */
10118
10119 ret = (*relocate_section) (output_bfd, flinfo->info,
10120 input_bfd, o, contents,
10121 internal_relocs,
10122 isymbuf,
10123 flinfo->sections);
10124 if (!ret)
10125 return FALSE;
10126
10127 if (ret == 2
10128 || bfd_link_relocatable (flinfo->info)
10129 || flinfo->info->emitrelocations)
10130 {
10131 Elf_Internal_Rela *irela;
10132 Elf_Internal_Rela *irelaend, *irelamid;
10133 bfd_vma last_offset;
10134 struct elf_link_hash_entry **rel_hash;
10135 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10136 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10137 unsigned int next_erel;
10138 bfd_boolean rela_normal;
10139 struct bfd_elf_section_data *esdi, *esdo;
10140
10141 esdi = elf_section_data (o);
10142 esdo = elf_section_data (o->output_section);
10143 rela_normal = FALSE;
10144
10145 /* Adjust the reloc addresses and symbol indices. */
10146
10147 irela = internal_relocs;
10148 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
10149 rel_hash = esdo->rel.hashes + esdo->rel.count;
10150 /* We start processing the REL relocs, if any. When we reach
10151 IRELAMID in the loop, we switch to the RELA relocs. */
10152 irelamid = irela;
10153 if (esdi->rel.hdr != NULL)
10154 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10155 * bed->s->int_rels_per_ext_rel);
10156 rel_hash_list = rel_hash;
10157 rela_hash_list = NULL;
10158 last_offset = o->output_offset;
10159 if (!bfd_link_relocatable (flinfo->info))
10160 last_offset += o->output_section->vma;
10161 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10162 {
10163 unsigned long r_symndx;
10164 asection *sec;
10165 Elf_Internal_Sym sym;
10166
10167 if (next_erel == bed->s->int_rels_per_ext_rel)
10168 {
10169 rel_hash++;
10170 next_erel = 0;
10171 }
10172
10173 if (irela == irelamid)
10174 {
10175 rel_hash = esdo->rela.hashes + esdo->rela.count;
10176 rela_hash_list = rel_hash;
10177 rela_normal = bed->rela_normal;
10178 }
10179
10180 irela->r_offset = _bfd_elf_section_offset (output_bfd,
10181 flinfo->info, o,
10182 irela->r_offset);
10183 if (irela->r_offset >= (bfd_vma) -2)
10184 {
10185 /* This is a reloc for a deleted entry or somesuch.
10186 Turn it into an R_*_NONE reloc, at the same
10187 offset as the last reloc. elf_eh_frame.c and
10188 bfd_elf_discard_info rely on reloc offsets
10189 being ordered. */
10190 irela->r_offset = last_offset;
10191 irela->r_info = 0;
10192 irela->r_addend = 0;
10193 continue;
10194 }
10195
10196 irela->r_offset += o->output_offset;
10197
10198 /* Relocs in an executable have to be virtual addresses. */
10199 if (!bfd_link_relocatable (flinfo->info))
10200 irela->r_offset += o->output_section->vma;
10201
10202 last_offset = irela->r_offset;
10203
10204 r_symndx = irela->r_info >> r_sym_shift;
10205 if (r_symndx == STN_UNDEF)
10206 continue;
10207
10208 if (r_symndx >= locsymcount
10209 || (elf_bad_symtab (input_bfd)
10210 && flinfo->sections[r_symndx] == NULL))
10211 {
10212 struct elf_link_hash_entry *rh;
10213 unsigned long indx;
10214
10215 /* This is a reloc against a global symbol. We
10216 have not yet output all the local symbols, so
10217 we do not know the symbol index of any global
10218 symbol. We set the rel_hash entry for this
10219 reloc to point to the global hash table entry
10220 for this symbol. The symbol index is then
10221 set at the end of bfd_elf_final_link. */
10222 indx = r_symndx - extsymoff;
10223 rh = elf_sym_hashes (input_bfd)[indx];
10224 while (rh->root.type == bfd_link_hash_indirect
10225 || rh->root.type == bfd_link_hash_warning)
10226 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10227
10228 /* Setting the index to -2 tells
10229 elf_link_output_extsym that this symbol is
10230 used by a reloc. */
10231 BFD_ASSERT (rh->indx < 0);
10232 rh->indx = -2;
10233
10234 *rel_hash = rh;
10235
10236 continue;
10237 }
10238
10239 /* This is a reloc against a local symbol. */
10240
10241 *rel_hash = NULL;
10242 sym = isymbuf[r_symndx];
10243 sec = flinfo->sections[r_symndx];
10244 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10245 {
10246 /* I suppose the backend ought to fill in the
10247 section of any STT_SECTION symbol against a
10248 processor specific section. */
10249 r_symndx = STN_UNDEF;
10250 if (bfd_is_abs_section (sec))
10251 ;
10252 else if (sec == NULL || sec->owner == NULL)
10253 {
10254 bfd_set_error (bfd_error_bad_value);
10255 return FALSE;
10256 }
10257 else
10258 {
10259 asection *osec = sec->output_section;
10260
10261 /* If we have discarded a section, the output
10262 section will be the absolute section. In
10263 case of discarded SEC_MERGE sections, use
10264 the kept section. relocate_section should
10265 have already handled discarded linkonce
10266 sections. */
10267 if (bfd_is_abs_section (osec)
10268 && sec->kept_section != NULL
10269 && sec->kept_section->output_section != NULL)
10270 {
10271 osec = sec->kept_section->output_section;
10272 irela->r_addend -= osec->vma;
10273 }
10274
10275 if (!bfd_is_abs_section (osec))
10276 {
10277 r_symndx = osec->target_index;
10278 if (r_symndx == STN_UNDEF)
10279 {
10280 irela->r_addend += osec->vma;
10281 osec = _bfd_nearby_section (output_bfd, osec,
10282 osec->vma);
10283 irela->r_addend -= osec->vma;
10284 r_symndx = osec->target_index;
10285 }
10286 }
10287 }
10288
10289 /* Adjust the addend according to where the
10290 section winds up in the output section. */
10291 if (rela_normal)
10292 irela->r_addend += sec->output_offset;
10293 }
10294 else
10295 {
10296 if (flinfo->indices[r_symndx] == -1)
10297 {
10298 unsigned long shlink;
10299 const char *name;
10300 asection *osec;
10301 long indx;
10302
10303 if (flinfo->info->strip == strip_all)
10304 {
10305 /* You can't do ld -r -s. */
10306 bfd_set_error (bfd_error_invalid_operation);
10307 return FALSE;
10308 }
10309
10310 /* This symbol was skipped earlier, but
10311 since it is needed by a reloc, we
10312 must output it now. */
10313 shlink = symtab_hdr->sh_link;
10314 name = (bfd_elf_string_from_elf_section
10315 (input_bfd, shlink, sym.st_name));
10316 if (name == NULL)
10317 return FALSE;
10318
10319 osec = sec->output_section;
10320 sym.st_shndx =
10321 _bfd_elf_section_from_bfd_section (output_bfd,
10322 osec);
10323 if (sym.st_shndx == SHN_BAD)
10324 return FALSE;
10325
10326 sym.st_value += sec->output_offset;
10327 if (!bfd_link_relocatable (flinfo->info))
10328 {
10329 sym.st_value += osec->vma;
10330 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10331 {
10332 /* STT_TLS symbols are relative to PT_TLS
10333 segment base. */
10334 BFD_ASSERT (elf_hash_table (flinfo->info)
10335 ->tls_sec != NULL);
10336 sym.st_value -= (elf_hash_table (flinfo->info)
10337 ->tls_sec->vma);
10338 }
10339 }
10340
10341 indx = bfd_get_symcount (output_bfd);
10342 ret = elf_link_output_symstrtab (flinfo, name,
10343 &sym, sec,
10344 NULL);
10345 if (ret == 0)
10346 return FALSE;
10347 else if (ret == 1)
10348 flinfo->indices[r_symndx] = indx;
10349 else
10350 abort ();
10351 }
10352
10353 r_symndx = flinfo->indices[r_symndx];
10354 }
10355
10356 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10357 | (irela->r_info & r_type_mask));
10358 }
10359
10360 /* Swap out the relocs. */
10361 input_rel_hdr = esdi->rel.hdr;
10362 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
10363 {
10364 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10365 input_rel_hdr,
10366 internal_relocs,
10367 rel_hash_list))
10368 return FALSE;
10369 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10370 * bed->s->int_rels_per_ext_rel);
10371 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
10372 }
10373
10374 input_rela_hdr = esdi->rela.hdr;
10375 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10376 {
10377 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10378 input_rela_hdr,
10379 internal_relocs,
10380 rela_hash_list))
10381 return FALSE;
10382 }
10383 }
10384 }
10385
10386 /* Write out the modified section contents. */
10387 if (bed->elf_backend_write_section
10388 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
10389 contents))
10390 {
10391 /* Section written out. */
10392 }
10393 else switch (o->sec_info_type)
10394 {
10395 case SEC_INFO_TYPE_STABS:
10396 if (! (_bfd_write_section_stabs
10397 (output_bfd,
10398 &elf_hash_table (flinfo->info)->stab_info,
10399 o, &elf_section_data (o)->sec_info, contents)))
10400 return FALSE;
10401 break;
10402 case SEC_INFO_TYPE_MERGE:
10403 if (! _bfd_write_merged_section (output_bfd, o,
10404 elf_section_data (o)->sec_info))
10405 return FALSE;
10406 break;
10407 case SEC_INFO_TYPE_EH_FRAME:
10408 {
10409 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
10410 o, contents))
10411 return FALSE;
10412 }
10413 break;
10414 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10415 {
10416 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10417 flinfo->info,
10418 o, contents))
10419 return FALSE;
10420 }
10421 break;
10422 default:
10423 {
10424 /* FIXME: octets_per_byte. */
10425 if (! (o->flags & SEC_EXCLUDE))
10426 {
10427 file_ptr offset = (file_ptr) o->output_offset;
10428 bfd_size_type todo = o->size;
10429 if ((o->flags & SEC_ELF_REVERSE_COPY))
10430 {
10431 /* Reverse-copy input section to output. */
10432 do
10433 {
10434 todo -= address_size;
10435 if (! bfd_set_section_contents (output_bfd,
10436 o->output_section,
10437 contents + todo,
10438 offset,
10439 address_size))
10440 return FALSE;
10441 if (todo == 0)
10442 break;
10443 offset += address_size;
10444 }
10445 while (1);
10446 }
10447 else if (! bfd_set_section_contents (output_bfd,
10448 o->output_section,
10449 contents,
10450 offset, todo))
10451 return FALSE;
10452 }
10453 }
10454 break;
10455 }
10456 }
10457
10458 return TRUE;
10459 }
10460
10461 /* Generate a reloc when linking an ELF file. This is a reloc
10462 requested by the linker, and does not come from any input file. This
10463 is used to build constructor and destructor tables when linking
10464 with -Ur. */
10465
10466 static bfd_boolean
10467 elf_reloc_link_order (bfd *output_bfd,
10468 struct bfd_link_info *info,
10469 asection *output_section,
10470 struct bfd_link_order *link_order)
10471 {
10472 reloc_howto_type *howto;
10473 long indx;
10474 bfd_vma offset;
10475 bfd_vma addend;
10476 struct bfd_elf_section_reloc_data *reldata;
10477 struct elf_link_hash_entry **rel_hash_ptr;
10478 Elf_Internal_Shdr *rel_hdr;
10479 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10480 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10481 bfd_byte *erel;
10482 unsigned int i;
10483 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
10484
10485 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10486 if (howto == NULL)
10487 {
10488 bfd_set_error (bfd_error_bad_value);
10489 return FALSE;
10490 }
10491
10492 addend = link_order->u.reloc.p->addend;
10493
10494 if (esdo->rel.hdr)
10495 reldata = &esdo->rel;
10496 else if (esdo->rela.hdr)
10497 reldata = &esdo->rela;
10498 else
10499 {
10500 reldata = NULL;
10501 BFD_ASSERT (0);
10502 }
10503
10504 /* Figure out the symbol index. */
10505 rel_hash_ptr = reldata->hashes + reldata->count;
10506 if (link_order->type == bfd_section_reloc_link_order)
10507 {
10508 indx = link_order->u.reloc.p->u.section->target_index;
10509 BFD_ASSERT (indx != 0);
10510 *rel_hash_ptr = NULL;
10511 }
10512 else
10513 {
10514 struct elf_link_hash_entry *h;
10515
10516 /* Treat a reloc against a defined symbol as though it were
10517 actually against the section. */
10518 h = ((struct elf_link_hash_entry *)
10519 bfd_wrapped_link_hash_lookup (output_bfd, info,
10520 link_order->u.reloc.p->u.name,
10521 FALSE, FALSE, TRUE));
10522 if (h != NULL
10523 && (h->root.type == bfd_link_hash_defined
10524 || h->root.type == bfd_link_hash_defweak))
10525 {
10526 asection *section;
10527
10528 section = h->root.u.def.section;
10529 indx = section->output_section->target_index;
10530 *rel_hash_ptr = NULL;
10531 /* It seems that we ought to add the symbol value to the
10532 addend here, but in practice it has already been added
10533 because it was passed to constructor_callback. */
10534 addend += section->output_section->vma + section->output_offset;
10535 }
10536 else if (h != NULL)
10537 {
10538 /* Setting the index to -2 tells elf_link_output_extsym that
10539 this symbol is used by a reloc. */
10540 h->indx = -2;
10541 *rel_hash_ptr = h;
10542 indx = 0;
10543 }
10544 else
10545 {
10546 if (! ((*info->callbacks->unattached_reloc)
10547 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
10548 return FALSE;
10549 indx = 0;
10550 }
10551 }
10552
10553 /* If this is an inplace reloc, we must write the addend into the
10554 object file. */
10555 if (howto->partial_inplace && addend != 0)
10556 {
10557 bfd_size_type size;
10558 bfd_reloc_status_type rstat;
10559 bfd_byte *buf;
10560 bfd_boolean ok;
10561 const char *sym_name;
10562
10563 size = (bfd_size_type) bfd_get_reloc_size (howto);
10564 buf = (bfd_byte *) bfd_zmalloc (size);
10565 if (buf == NULL && size != 0)
10566 return FALSE;
10567 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10568 switch (rstat)
10569 {
10570 case bfd_reloc_ok:
10571 break;
10572
10573 default:
10574 case bfd_reloc_outofrange:
10575 abort ();
10576
10577 case bfd_reloc_overflow:
10578 if (link_order->type == bfd_section_reloc_link_order)
10579 sym_name = bfd_section_name (output_bfd,
10580 link_order->u.reloc.p->u.section);
10581 else
10582 sym_name = link_order->u.reloc.p->u.name;
10583 if (! ((*info->callbacks->reloc_overflow)
10584 (info, NULL, sym_name, howto->name, addend, NULL,
10585 NULL, (bfd_vma) 0)))
10586 {
10587 free (buf);
10588 return FALSE;
10589 }
10590 break;
10591 }
10592 ok = bfd_set_section_contents (output_bfd, output_section, buf,
10593 link_order->offset, size);
10594 free (buf);
10595 if (! ok)
10596 return FALSE;
10597 }
10598
10599 /* The address of a reloc is relative to the section in a
10600 relocatable file, and is a virtual address in an executable
10601 file. */
10602 offset = link_order->offset;
10603 if (! bfd_link_relocatable (info))
10604 offset += output_section->vma;
10605
10606 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10607 {
10608 irel[i].r_offset = offset;
10609 irel[i].r_info = 0;
10610 irel[i].r_addend = 0;
10611 }
10612 if (bed->s->arch_size == 32)
10613 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10614 else
10615 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10616
10617 rel_hdr = reldata->hdr;
10618 erel = rel_hdr->contents;
10619 if (rel_hdr->sh_type == SHT_REL)
10620 {
10621 erel += reldata->count * bed->s->sizeof_rel;
10622 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10623 }
10624 else
10625 {
10626 irel[0].r_addend = addend;
10627 erel += reldata->count * bed->s->sizeof_rela;
10628 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10629 }
10630
10631 ++reldata->count;
10632
10633 return TRUE;
10634 }
10635
10636
10637 /* Get the output vma of the section pointed to by the sh_link field. */
10638
10639 static bfd_vma
10640 elf_get_linked_section_vma (struct bfd_link_order *p)
10641 {
10642 Elf_Internal_Shdr **elf_shdrp;
10643 asection *s;
10644 int elfsec;
10645
10646 s = p->u.indirect.section;
10647 elf_shdrp = elf_elfsections (s->owner);
10648 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10649 elfsec = elf_shdrp[elfsec]->sh_link;
10650 /* PR 290:
10651 The Intel C compiler generates SHT_IA_64_UNWIND with
10652 SHF_LINK_ORDER. But it doesn't set the sh_link or
10653 sh_info fields. Hence we could get the situation
10654 where elfsec is 0. */
10655 if (elfsec == 0)
10656 {
10657 const struct elf_backend_data *bed
10658 = get_elf_backend_data (s->owner);
10659 if (bed->link_order_error_handler)
10660 bed->link_order_error_handler
10661 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
10662 return 0;
10663 }
10664 else
10665 {
10666 s = elf_shdrp[elfsec]->bfd_section;
10667 return s->output_section->vma + s->output_offset;
10668 }
10669 }
10670
10671
10672 /* Compare two sections based on the locations of the sections they are
10673 linked to. Used by elf_fixup_link_order. */
10674
10675 static int
10676 compare_link_order (const void * a, const void * b)
10677 {
10678 bfd_vma apos;
10679 bfd_vma bpos;
10680
10681 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10682 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10683 if (apos < bpos)
10684 return -1;
10685 return apos > bpos;
10686 }
10687
10688
10689 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10690 order as their linked sections. Returns false if this could not be done
10691 because an output section includes both ordered and unordered
10692 sections. Ideally we'd do this in the linker proper. */
10693
10694 static bfd_boolean
10695 elf_fixup_link_order (bfd *abfd, asection *o)
10696 {
10697 int seen_linkorder;
10698 int seen_other;
10699 int n;
10700 struct bfd_link_order *p;
10701 bfd *sub;
10702 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10703 unsigned elfsec;
10704 struct bfd_link_order **sections;
10705 asection *s, *other_sec, *linkorder_sec;
10706 bfd_vma offset;
10707
10708 other_sec = NULL;
10709 linkorder_sec = NULL;
10710 seen_other = 0;
10711 seen_linkorder = 0;
10712 for (p = o->map_head.link_order; p != NULL; p = p->next)
10713 {
10714 if (p->type == bfd_indirect_link_order)
10715 {
10716 s = p->u.indirect.section;
10717 sub = s->owner;
10718 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10719 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
10720 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
10721 && elfsec < elf_numsections (sub)
10722 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
10723 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
10724 {
10725 seen_linkorder++;
10726 linkorder_sec = s;
10727 }
10728 else
10729 {
10730 seen_other++;
10731 other_sec = s;
10732 }
10733 }
10734 else
10735 seen_other++;
10736
10737 if (seen_other && seen_linkorder)
10738 {
10739 if (other_sec && linkorder_sec)
10740 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
10741 o, linkorder_sec,
10742 linkorder_sec->owner, other_sec,
10743 other_sec->owner);
10744 else
10745 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
10746 o);
10747 bfd_set_error (bfd_error_bad_value);
10748 return FALSE;
10749 }
10750 }
10751
10752 if (!seen_linkorder)
10753 return TRUE;
10754
10755 sections = (struct bfd_link_order **)
10756 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
10757 if (sections == NULL)
10758 return FALSE;
10759 seen_linkorder = 0;
10760
10761 for (p = o->map_head.link_order; p != NULL; p = p->next)
10762 {
10763 sections[seen_linkorder++] = p;
10764 }
10765 /* Sort the input sections in the order of their linked section. */
10766 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
10767 compare_link_order);
10768
10769 /* Change the offsets of the sections. */
10770 offset = 0;
10771 for (n = 0; n < seen_linkorder; n++)
10772 {
10773 s = sections[n]->u.indirect.section;
10774 offset &= ~(bfd_vma) 0 << s->alignment_power;
10775 s->output_offset = offset;
10776 sections[n]->offset = offset;
10777 /* FIXME: octets_per_byte. */
10778 offset += sections[n]->size;
10779 }
10780
10781 free (sections);
10782 return TRUE;
10783 }
10784
10785 static void
10786 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
10787 {
10788 asection *o;
10789
10790 if (flinfo->symstrtab != NULL)
10791 _bfd_elf_strtab_free (flinfo->symstrtab);
10792 if (flinfo->contents != NULL)
10793 free (flinfo->contents);
10794 if (flinfo->external_relocs != NULL)
10795 free (flinfo->external_relocs);
10796 if (flinfo->internal_relocs != NULL)
10797 free (flinfo->internal_relocs);
10798 if (flinfo->external_syms != NULL)
10799 free (flinfo->external_syms);
10800 if (flinfo->locsym_shndx != NULL)
10801 free (flinfo->locsym_shndx);
10802 if (flinfo->internal_syms != NULL)
10803 free (flinfo->internal_syms);
10804 if (flinfo->indices != NULL)
10805 free (flinfo->indices);
10806 if (flinfo->sections != NULL)
10807 free (flinfo->sections);
10808 if (flinfo->symshndxbuf != NULL)
10809 free (flinfo->symshndxbuf);
10810 for (o = obfd->sections; o != NULL; o = o->next)
10811 {
10812 struct bfd_elf_section_data *esdo = elf_section_data (o);
10813 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
10814 free (esdo->rel.hashes);
10815 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
10816 free (esdo->rela.hashes);
10817 }
10818 }
10819
10820 /* Do the final step of an ELF link. */
10821
10822 bfd_boolean
10823 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
10824 {
10825 bfd_boolean dynamic;
10826 bfd_boolean emit_relocs;
10827 bfd *dynobj;
10828 struct elf_final_link_info flinfo;
10829 asection *o;
10830 struct bfd_link_order *p;
10831 bfd *sub;
10832 bfd_size_type max_contents_size;
10833 bfd_size_type max_external_reloc_size;
10834 bfd_size_type max_internal_reloc_count;
10835 bfd_size_type max_sym_count;
10836 bfd_size_type max_sym_shndx_count;
10837 Elf_Internal_Sym elfsym;
10838 unsigned int i;
10839 Elf_Internal_Shdr *symtab_hdr;
10840 Elf_Internal_Shdr *symtab_shndx_hdr;
10841 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10842 struct elf_outext_info eoinfo;
10843 bfd_boolean merged;
10844 size_t relativecount = 0;
10845 asection *reldyn = 0;
10846 bfd_size_type amt;
10847 asection *attr_section = NULL;
10848 bfd_vma attr_size = 0;
10849 const char *std_attrs_section;
10850
10851 if (! is_elf_hash_table (info->hash))
10852 return FALSE;
10853
10854 if (bfd_link_pic (info))
10855 abfd->flags |= DYNAMIC;
10856
10857 dynamic = elf_hash_table (info)->dynamic_sections_created;
10858 dynobj = elf_hash_table (info)->dynobj;
10859
10860 emit_relocs = (bfd_link_relocatable (info)
10861 || info->emitrelocations);
10862
10863 flinfo.info = info;
10864 flinfo.output_bfd = abfd;
10865 flinfo.symstrtab = _bfd_elf_strtab_init ();
10866 if (flinfo.symstrtab == NULL)
10867 return FALSE;
10868
10869 if (! dynamic)
10870 {
10871 flinfo.dynsym_sec = NULL;
10872 flinfo.hash_sec = NULL;
10873 flinfo.symver_sec = NULL;
10874 }
10875 else
10876 {
10877 flinfo.dynsym_sec = bfd_get_linker_section (dynobj, ".dynsym");
10878 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
10879 /* Note that dynsym_sec can be NULL (on VMS). */
10880 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
10881 /* Note that it is OK if symver_sec is NULL. */
10882 }
10883
10884 flinfo.contents = NULL;
10885 flinfo.external_relocs = NULL;
10886 flinfo.internal_relocs = NULL;
10887 flinfo.external_syms = NULL;
10888 flinfo.locsym_shndx = NULL;
10889 flinfo.internal_syms = NULL;
10890 flinfo.indices = NULL;
10891 flinfo.sections = NULL;
10892 flinfo.symshndxbuf = NULL;
10893 flinfo.filesym_count = 0;
10894
10895 /* The object attributes have been merged. Remove the input
10896 sections from the link, and set the contents of the output
10897 secton. */
10898 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
10899 for (o = abfd->sections; o != NULL; o = o->next)
10900 {
10901 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
10902 || strcmp (o->name, ".gnu.attributes") == 0)
10903 {
10904 for (p = o->map_head.link_order; p != NULL; p = p->next)
10905 {
10906 asection *input_section;
10907
10908 if (p->type != bfd_indirect_link_order)
10909 continue;
10910 input_section = p->u.indirect.section;
10911 /* Hack: reset the SEC_HAS_CONTENTS flag so that
10912 elf_link_input_bfd ignores this section. */
10913 input_section->flags &= ~SEC_HAS_CONTENTS;
10914 }
10915
10916 attr_size = bfd_elf_obj_attr_size (abfd);
10917 if (attr_size)
10918 {
10919 bfd_set_section_size (abfd, o, attr_size);
10920 attr_section = o;
10921 /* Skip this section later on. */
10922 o->map_head.link_order = NULL;
10923 }
10924 else
10925 o->flags |= SEC_EXCLUDE;
10926 }
10927 }
10928
10929 /* Count up the number of relocations we will output for each output
10930 section, so that we know the sizes of the reloc sections. We
10931 also figure out some maximum sizes. */
10932 max_contents_size = 0;
10933 max_external_reloc_size = 0;
10934 max_internal_reloc_count = 0;
10935 max_sym_count = 0;
10936 max_sym_shndx_count = 0;
10937 merged = FALSE;
10938 for (o = abfd->sections; o != NULL; o = o->next)
10939 {
10940 struct bfd_elf_section_data *esdo = elf_section_data (o);
10941 o->reloc_count = 0;
10942
10943 for (p = o->map_head.link_order; p != NULL; p = p->next)
10944 {
10945 unsigned int reloc_count = 0;
10946 struct bfd_elf_section_data *esdi = NULL;
10947
10948 if (p->type == bfd_section_reloc_link_order
10949 || p->type == bfd_symbol_reloc_link_order)
10950 reloc_count = 1;
10951 else if (p->type == bfd_indirect_link_order)
10952 {
10953 asection *sec;
10954
10955 sec = p->u.indirect.section;
10956 esdi = elf_section_data (sec);
10957
10958 /* Mark all sections which are to be included in the
10959 link. This will normally be every section. We need
10960 to do this so that we can identify any sections which
10961 the linker has decided to not include. */
10962 sec->linker_mark = TRUE;
10963
10964 if (sec->flags & SEC_MERGE)
10965 merged = TRUE;
10966
10967 if (esdo->this_hdr.sh_type == SHT_REL
10968 || esdo->this_hdr.sh_type == SHT_RELA)
10969 /* Some backends use reloc_count in relocation sections
10970 to count particular types of relocs. Of course,
10971 reloc sections themselves can't have relocations. */
10972 reloc_count = 0;
10973 else if (emit_relocs)
10974 reloc_count = sec->reloc_count;
10975 else if (bed->elf_backend_count_relocs)
10976 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
10977
10978 if (sec->rawsize > max_contents_size)
10979 max_contents_size = sec->rawsize;
10980 if (sec->size > max_contents_size)
10981 max_contents_size = sec->size;
10982
10983 /* We are interested in just local symbols, not all
10984 symbols. */
10985 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
10986 && (sec->owner->flags & DYNAMIC) == 0)
10987 {
10988 size_t sym_count;
10989
10990 if (elf_bad_symtab (sec->owner))
10991 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
10992 / bed->s->sizeof_sym);
10993 else
10994 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
10995
10996 if (sym_count > max_sym_count)
10997 max_sym_count = sym_count;
10998
10999 if (sym_count > max_sym_shndx_count
11000 && elf_symtab_shndx (sec->owner) != 0)
11001 max_sym_shndx_count = sym_count;
11002
11003 if ((sec->flags & SEC_RELOC) != 0)
11004 {
11005 size_t ext_size = 0;
11006
11007 if (esdi->rel.hdr != NULL)
11008 ext_size = esdi->rel.hdr->sh_size;
11009 if (esdi->rela.hdr != NULL)
11010 ext_size += esdi->rela.hdr->sh_size;
11011
11012 if (ext_size > max_external_reloc_size)
11013 max_external_reloc_size = ext_size;
11014 if (sec->reloc_count > max_internal_reloc_count)
11015 max_internal_reloc_count = sec->reloc_count;
11016 }
11017 }
11018 }
11019
11020 if (reloc_count == 0)
11021 continue;
11022
11023 o->reloc_count += reloc_count;
11024
11025 if (p->type == bfd_indirect_link_order && emit_relocs)
11026 {
11027 if (esdi->rel.hdr)
11028 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11029 if (esdi->rela.hdr)
11030 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11031 }
11032 else
11033 {
11034 if (o->use_rela_p)
11035 esdo->rela.count += reloc_count;
11036 else
11037 esdo->rel.count += reloc_count;
11038 }
11039 }
11040
11041 if (o->reloc_count > 0)
11042 o->flags |= SEC_RELOC;
11043 else
11044 {
11045 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11046 set it (this is probably a bug) and if it is set
11047 assign_section_numbers will create a reloc section. */
11048 o->flags &=~ SEC_RELOC;
11049 }
11050
11051 /* If the SEC_ALLOC flag is not set, force the section VMA to
11052 zero. This is done in elf_fake_sections as well, but forcing
11053 the VMA to 0 here will ensure that relocs against these
11054 sections are handled correctly. */
11055 if ((o->flags & SEC_ALLOC) == 0
11056 && ! o->user_set_vma)
11057 o->vma = 0;
11058 }
11059
11060 if (! bfd_link_relocatable (info) && merged)
11061 elf_link_hash_traverse (elf_hash_table (info),
11062 _bfd_elf_link_sec_merge_syms, abfd);
11063
11064 /* Figure out the file positions for everything but the symbol table
11065 and the relocs. We set symcount to force assign_section_numbers
11066 to create a symbol table. */
11067 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
11068 BFD_ASSERT (! abfd->output_has_begun);
11069 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11070 goto error_return;
11071
11072 /* Set sizes, and assign file positions for reloc sections. */
11073 for (o = abfd->sections; o != NULL; o = o->next)
11074 {
11075 struct bfd_elf_section_data *esdo = elf_section_data (o);
11076 if ((o->flags & SEC_RELOC) != 0)
11077 {
11078 if (esdo->rel.hdr
11079 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
11080 goto error_return;
11081
11082 if (esdo->rela.hdr
11083 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
11084 goto error_return;
11085 }
11086
11087 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11088 to count upwards while actually outputting the relocations. */
11089 esdo->rel.count = 0;
11090 esdo->rela.count = 0;
11091
11092 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11093 {
11094 /* Cache the section contents so that they can be compressed
11095 later. Use bfd_malloc since it will be freed by
11096 bfd_compress_section_contents. */
11097 unsigned char *contents = esdo->this_hdr.contents;
11098 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11099 abort ();
11100 contents
11101 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11102 if (contents == NULL)
11103 goto error_return;
11104 esdo->this_hdr.contents = contents;
11105 }
11106 }
11107
11108 /* We have now assigned file positions for all the sections except
11109 .symtab, .strtab, and non-loaded reloc sections. We start the
11110 .symtab section at the current file position, and write directly
11111 to it. We build the .strtab section in memory. */
11112 bfd_get_symcount (abfd) = 0;
11113 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11114 /* sh_name is set in prep_headers. */
11115 symtab_hdr->sh_type = SHT_SYMTAB;
11116 /* sh_flags, sh_addr and sh_size all start off zero. */
11117 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11118 /* sh_link is set in assign_section_numbers. */
11119 /* sh_info is set below. */
11120 /* sh_offset is set just below. */
11121 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
11122
11123 if (max_sym_count < 20)
11124 max_sym_count = 20;
11125 elf_hash_table (info)->strtabsize = max_sym_count;
11126 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11127 elf_hash_table (info)->strtab
11128 = (struct elf_sym_strtab *) bfd_malloc (amt);
11129 if (elf_hash_table (info)->strtab == NULL)
11130 goto error_return;
11131 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11132 flinfo.symshndxbuf
11133 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11134 ? (Elf_External_Sym_Shndx *) -1 : NULL);
11135
11136 if (info->strip != strip_all || emit_relocs)
11137 {
11138 file_ptr off = elf_next_file_pos (abfd);
11139
11140 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11141
11142 /* Note that at this point elf_next_file_pos (abfd) is
11143 incorrect. We do not yet know the size of the .symtab section.
11144 We correct next_file_pos below, after we do know the size. */
11145
11146 /* Start writing out the symbol table. The first symbol is always a
11147 dummy symbol. */
11148 elfsym.st_value = 0;
11149 elfsym.st_size = 0;
11150 elfsym.st_info = 0;
11151 elfsym.st_other = 0;
11152 elfsym.st_shndx = SHN_UNDEF;
11153 elfsym.st_target_internal = 0;
11154 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11155 bfd_und_section_ptr, NULL) != 1)
11156 goto error_return;
11157
11158 /* Output a symbol for each section. We output these even if we are
11159 discarding local symbols, since they are used for relocs. These
11160 symbols have no names. We store the index of each one in the
11161 index field of the section, so that we can find it again when
11162 outputting relocs. */
11163
11164 elfsym.st_size = 0;
11165 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11166 elfsym.st_other = 0;
11167 elfsym.st_value = 0;
11168 elfsym.st_target_internal = 0;
11169 for (i = 1; i < elf_numsections (abfd); i++)
11170 {
11171 o = bfd_section_from_elf_index (abfd, i);
11172 if (o != NULL)
11173 {
11174 o->target_index = bfd_get_symcount (abfd);
11175 elfsym.st_shndx = i;
11176 if (!bfd_link_relocatable (info))
11177 elfsym.st_value = o->vma;
11178 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11179 NULL) != 1)
11180 goto error_return;
11181 }
11182 }
11183 }
11184
11185 /* Allocate some memory to hold information read in from the input
11186 files. */
11187 if (max_contents_size != 0)
11188 {
11189 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11190 if (flinfo.contents == NULL)
11191 goto error_return;
11192 }
11193
11194 if (max_external_reloc_size != 0)
11195 {
11196 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11197 if (flinfo.external_relocs == NULL)
11198 goto error_return;
11199 }
11200
11201 if (max_internal_reloc_count != 0)
11202 {
11203 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11204 amt *= sizeof (Elf_Internal_Rela);
11205 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11206 if (flinfo.internal_relocs == NULL)
11207 goto error_return;
11208 }
11209
11210 if (max_sym_count != 0)
11211 {
11212 amt = max_sym_count * bed->s->sizeof_sym;
11213 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11214 if (flinfo.external_syms == NULL)
11215 goto error_return;
11216
11217 amt = max_sym_count * sizeof (Elf_Internal_Sym);
11218 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11219 if (flinfo.internal_syms == NULL)
11220 goto error_return;
11221
11222 amt = max_sym_count * sizeof (long);
11223 flinfo.indices = (long int *) bfd_malloc (amt);
11224 if (flinfo.indices == NULL)
11225 goto error_return;
11226
11227 amt = max_sym_count * sizeof (asection *);
11228 flinfo.sections = (asection **) bfd_malloc (amt);
11229 if (flinfo.sections == NULL)
11230 goto error_return;
11231 }
11232
11233 if (max_sym_shndx_count != 0)
11234 {
11235 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
11236 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11237 if (flinfo.locsym_shndx == NULL)
11238 goto error_return;
11239 }
11240
11241 if (elf_hash_table (info)->tls_sec)
11242 {
11243 bfd_vma base, end = 0;
11244 asection *sec;
11245
11246 for (sec = elf_hash_table (info)->tls_sec;
11247 sec && (sec->flags & SEC_THREAD_LOCAL);
11248 sec = sec->next)
11249 {
11250 bfd_size_type size = sec->size;
11251
11252 if (size == 0
11253 && (sec->flags & SEC_HAS_CONTENTS) == 0)
11254 {
11255 struct bfd_link_order *ord = sec->map_tail.link_order;
11256
11257 if (ord != NULL)
11258 size = ord->offset + ord->size;
11259 }
11260 end = sec->vma + size;
11261 }
11262 base = elf_hash_table (info)->tls_sec->vma;
11263 /* Only align end of TLS section if static TLS doesn't have special
11264 alignment requirements. */
11265 if (bed->static_tls_alignment == 1)
11266 end = align_power (end,
11267 elf_hash_table (info)->tls_sec->alignment_power);
11268 elf_hash_table (info)->tls_size = end - base;
11269 }
11270
11271 /* Reorder SHF_LINK_ORDER sections. */
11272 for (o = abfd->sections; o != NULL; o = o->next)
11273 {
11274 if (!elf_fixup_link_order (abfd, o))
11275 return FALSE;
11276 }
11277
11278 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11279 return FALSE;
11280
11281 /* Since ELF permits relocations to be against local symbols, we
11282 must have the local symbols available when we do the relocations.
11283 Since we would rather only read the local symbols once, and we
11284 would rather not keep them in memory, we handle all the
11285 relocations for a single input file at the same time.
11286
11287 Unfortunately, there is no way to know the total number of local
11288 symbols until we have seen all of them, and the local symbol
11289 indices precede the global symbol indices. This means that when
11290 we are generating relocatable output, and we see a reloc against
11291 a global symbol, we can not know the symbol index until we have
11292 finished examining all the local symbols to see which ones we are
11293 going to output. To deal with this, we keep the relocations in
11294 memory, and don't output them until the end of the link. This is
11295 an unfortunate waste of memory, but I don't see a good way around
11296 it. Fortunately, it only happens when performing a relocatable
11297 link, which is not the common case. FIXME: If keep_memory is set
11298 we could write the relocs out and then read them again; I don't
11299 know how bad the memory loss will be. */
11300
11301 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11302 sub->output_has_begun = FALSE;
11303 for (o = abfd->sections; o != NULL; o = o->next)
11304 {
11305 for (p = o->map_head.link_order; p != NULL; p = p->next)
11306 {
11307 if (p->type == bfd_indirect_link_order
11308 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11309 == bfd_target_elf_flavour)
11310 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11311 {
11312 if (! sub->output_has_begun)
11313 {
11314 if (! elf_link_input_bfd (&flinfo, sub))
11315 goto error_return;
11316 sub->output_has_begun = TRUE;
11317 }
11318 }
11319 else if (p->type == bfd_section_reloc_link_order
11320 || p->type == bfd_symbol_reloc_link_order)
11321 {
11322 if (! elf_reloc_link_order (abfd, info, o, p))
11323 goto error_return;
11324 }
11325 else
11326 {
11327 if (! _bfd_default_link_order (abfd, info, o, p))
11328 {
11329 if (p->type == bfd_indirect_link_order
11330 && (bfd_get_flavour (sub)
11331 == bfd_target_elf_flavour)
11332 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11333 != bed->s->elfclass))
11334 {
11335 const char *iclass, *oclass;
11336
11337 if (bed->s->elfclass == ELFCLASS64)
11338 {
11339 iclass = "ELFCLASS32";
11340 oclass = "ELFCLASS64";
11341 }
11342 else
11343 {
11344 iclass = "ELFCLASS64";
11345 oclass = "ELFCLASS32";
11346 }
11347
11348 bfd_set_error (bfd_error_wrong_format);
11349 (*_bfd_error_handler)
11350 (_("%B: file class %s incompatible with %s"),
11351 sub, iclass, oclass);
11352 }
11353
11354 goto error_return;
11355 }
11356 }
11357 }
11358 }
11359
11360 /* Free symbol buffer if needed. */
11361 if (!info->reduce_memory_overheads)
11362 {
11363 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11364 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11365 && elf_tdata (sub)->symbuf)
11366 {
11367 free (elf_tdata (sub)->symbuf);
11368 elf_tdata (sub)->symbuf = NULL;
11369 }
11370 }
11371
11372 /* Output any global symbols that got converted to local in a
11373 version script or due to symbol visibility. We do this in a
11374 separate step since ELF requires all local symbols to appear
11375 prior to any global symbols. FIXME: We should only do this if
11376 some global symbols were, in fact, converted to become local.
11377 FIXME: Will this work correctly with the Irix 5 linker? */
11378 eoinfo.failed = FALSE;
11379 eoinfo.flinfo = &flinfo;
11380 eoinfo.localsyms = TRUE;
11381 eoinfo.file_sym_done = FALSE;
11382 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11383 if (eoinfo.failed)
11384 return FALSE;
11385
11386 /* If backend needs to output some local symbols not present in the hash
11387 table, do it now. */
11388 if (bed->elf_backend_output_arch_local_syms
11389 && (info->strip != strip_all || emit_relocs))
11390 {
11391 typedef int (*out_sym_func)
11392 (void *, const char *, Elf_Internal_Sym *, asection *,
11393 struct elf_link_hash_entry *);
11394
11395 if (! ((*bed->elf_backend_output_arch_local_syms)
11396 (abfd, info, &flinfo,
11397 (out_sym_func) elf_link_output_symstrtab)))
11398 return FALSE;
11399 }
11400
11401 /* That wrote out all the local symbols. Finish up the symbol table
11402 with the global symbols. Even if we want to strip everything we
11403 can, we still need to deal with those global symbols that got
11404 converted to local in a version script. */
11405
11406 /* The sh_info field records the index of the first non local symbol. */
11407 symtab_hdr->sh_info = bfd_get_symcount (abfd);
11408
11409 if (dynamic
11410 && flinfo.dynsym_sec != NULL
11411 && flinfo.dynsym_sec->output_section != bfd_abs_section_ptr)
11412 {
11413 Elf_Internal_Sym sym;
11414 bfd_byte *dynsym = flinfo.dynsym_sec->contents;
11415 long last_local = 0;
11416
11417 /* Write out the section symbols for the output sections. */
11418 if (bfd_link_pic (info)
11419 || elf_hash_table (info)->is_relocatable_executable)
11420 {
11421 asection *s;
11422
11423 sym.st_size = 0;
11424 sym.st_name = 0;
11425 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11426 sym.st_other = 0;
11427 sym.st_target_internal = 0;
11428
11429 for (s = abfd->sections; s != NULL; s = s->next)
11430 {
11431 int indx;
11432 bfd_byte *dest;
11433 long dynindx;
11434
11435 dynindx = elf_section_data (s)->dynindx;
11436 if (dynindx <= 0)
11437 continue;
11438 indx = elf_section_data (s)->this_idx;
11439 BFD_ASSERT (indx > 0);
11440 sym.st_shndx = indx;
11441 if (! check_dynsym (abfd, &sym))
11442 return FALSE;
11443 sym.st_value = s->vma;
11444 dest = dynsym + dynindx * bed->s->sizeof_sym;
11445 if (last_local < dynindx)
11446 last_local = dynindx;
11447 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11448 }
11449 }
11450
11451 /* Write out the local dynsyms. */
11452 if (elf_hash_table (info)->dynlocal)
11453 {
11454 struct elf_link_local_dynamic_entry *e;
11455 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
11456 {
11457 asection *s;
11458 bfd_byte *dest;
11459
11460 /* Copy the internal symbol and turn off visibility.
11461 Note that we saved a word of storage and overwrote
11462 the original st_name with the dynstr_index. */
11463 sym = e->isym;
11464 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
11465
11466 s = bfd_section_from_elf_index (e->input_bfd,
11467 e->isym.st_shndx);
11468 if (s != NULL)
11469 {
11470 sym.st_shndx =
11471 elf_section_data (s->output_section)->this_idx;
11472 if (! check_dynsym (abfd, &sym))
11473 return FALSE;
11474 sym.st_value = (s->output_section->vma
11475 + s->output_offset
11476 + e->isym.st_value);
11477 }
11478
11479 if (last_local < e->dynindx)
11480 last_local = e->dynindx;
11481
11482 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
11483 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11484 }
11485 }
11486
11487 elf_section_data (flinfo.dynsym_sec->output_section)->this_hdr.sh_info =
11488 last_local + 1;
11489 }
11490
11491 /* We get the global symbols from the hash table. */
11492 eoinfo.failed = FALSE;
11493 eoinfo.localsyms = FALSE;
11494 eoinfo.flinfo = &flinfo;
11495 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11496 if (eoinfo.failed)
11497 return FALSE;
11498
11499 /* If backend needs to output some symbols not present in the hash
11500 table, do it now. */
11501 if (bed->elf_backend_output_arch_syms
11502 && (info->strip != strip_all || emit_relocs))
11503 {
11504 typedef int (*out_sym_func)
11505 (void *, const char *, Elf_Internal_Sym *, asection *,
11506 struct elf_link_hash_entry *);
11507
11508 if (! ((*bed->elf_backend_output_arch_syms)
11509 (abfd, info, &flinfo,
11510 (out_sym_func) elf_link_output_symstrtab)))
11511 return FALSE;
11512 }
11513
11514 /* Finalize the .strtab section. */
11515 _bfd_elf_strtab_finalize (flinfo.symstrtab);
11516
11517 /* Swap out the .strtab section. */
11518 if (!elf_link_swap_symbols_out (&flinfo))
11519 return FALSE;
11520
11521 /* Now we know the size of the symtab section. */
11522 if (bfd_get_symcount (abfd) > 0)
11523 {
11524 /* Finish up and write out the symbol string table (.strtab)
11525 section. */
11526 Elf_Internal_Shdr *symstrtab_hdr;
11527 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
11528
11529 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
11530 if (symtab_shndx_hdr->sh_name != 0)
11531 {
11532 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
11533 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
11534 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
11535 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
11536 symtab_shndx_hdr->sh_size = amt;
11537
11538 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
11539 off, TRUE);
11540
11541 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
11542 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
11543 return FALSE;
11544 }
11545
11546 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
11547 /* sh_name was set in prep_headers. */
11548 symstrtab_hdr->sh_type = SHT_STRTAB;
11549 symstrtab_hdr->sh_flags = 0;
11550 symstrtab_hdr->sh_addr = 0;
11551 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
11552 symstrtab_hdr->sh_entsize = 0;
11553 symstrtab_hdr->sh_link = 0;
11554 symstrtab_hdr->sh_info = 0;
11555 /* sh_offset is set just below. */
11556 symstrtab_hdr->sh_addralign = 1;
11557
11558 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
11559 off, TRUE);
11560 elf_next_file_pos (abfd) = off;
11561
11562 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
11563 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
11564 return FALSE;
11565 }
11566
11567 /* Adjust the relocs to have the correct symbol indices. */
11568 for (o = abfd->sections; o != NULL; o = o->next)
11569 {
11570 struct bfd_elf_section_data *esdo = elf_section_data (o);
11571 bfd_boolean sort;
11572 if ((o->flags & SEC_RELOC) == 0)
11573 continue;
11574
11575 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
11576 if (esdo->rel.hdr != NULL)
11577 elf_link_adjust_relocs (abfd, &esdo->rel, sort);
11578 if (esdo->rela.hdr != NULL)
11579 elf_link_adjust_relocs (abfd, &esdo->rela, sort);
11580
11581 /* Set the reloc_count field to 0 to prevent write_relocs from
11582 trying to swap the relocs out itself. */
11583 o->reloc_count = 0;
11584 }
11585
11586 if (dynamic && info->combreloc && dynobj != NULL)
11587 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
11588
11589 /* If we are linking against a dynamic object, or generating a
11590 shared library, finish up the dynamic linking information. */
11591 if (dynamic)
11592 {
11593 bfd_byte *dyncon, *dynconend;
11594
11595 /* Fix up .dynamic entries. */
11596 o = bfd_get_linker_section (dynobj, ".dynamic");
11597 BFD_ASSERT (o != NULL);
11598
11599 dyncon = o->contents;
11600 dynconend = o->contents + o->size;
11601 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11602 {
11603 Elf_Internal_Dyn dyn;
11604 const char *name;
11605 unsigned int type;
11606
11607 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11608
11609 switch (dyn.d_tag)
11610 {
11611 default:
11612 continue;
11613 case DT_NULL:
11614 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
11615 {
11616 switch (elf_section_data (reldyn)->this_hdr.sh_type)
11617 {
11618 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
11619 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
11620 default: continue;
11621 }
11622 dyn.d_un.d_val = relativecount;
11623 relativecount = 0;
11624 break;
11625 }
11626 continue;
11627
11628 case DT_INIT:
11629 name = info->init_function;
11630 goto get_sym;
11631 case DT_FINI:
11632 name = info->fini_function;
11633 get_sym:
11634 {
11635 struct elf_link_hash_entry *h;
11636
11637 h = elf_link_hash_lookup (elf_hash_table (info), name,
11638 FALSE, FALSE, TRUE);
11639 if (h != NULL
11640 && (h->root.type == bfd_link_hash_defined
11641 || h->root.type == bfd_link_hash_defweak))
11642 {
11643 dyn.d_un.d_ptr = h->root.u.def.value;
11644 o = h->root.u.def.section;
11645 if (o->output_section != NULL)
11646 dyn.d_un.d_ptr += (o->output_section->vma
11647 + o->output_offset);
11648 else
11649 {
11650 /* The symbol is imported from another shared
11651 library and does not apply to this one. */
11652 dyn.d_un.d_ptr = 0;
11653 }
11654 break;
11655 }
11656 }
11657 continue;
11658
11659 case DT_PREINIT_ARRAYSZ:
11660 name = ".preinit_array";
11661 goto get_size;
11662 case DT_INIT_ARRAYSZ:
11663 name = ".init_array";
11664 goto get_size;
11665 case DT_FINI_ARRAYSZ:
11666 name = ".fini_array";
11667 get_size:
11668 o = bfd_get_section_by_name (abfd, name);
11669 if (o == NULL)
11670 {
11671 (*_bfd_error_handler)
11672 (_("%B: could not find output section %s"), abfd, name);
11673 goto error_return;
11674 }
11675 if (o->size == 0)
11676 (*_bfd_error_handler)
11677 (_("warning: %s section has zero size"), name);
11678 dyn.d_un.d_val = o->size;
11679 break;
11680
11681 case DT_PREINIT_ARRAY:
11682 name = ".preinit_array";
11683 goto get_vma;
11684 case DT_INIT_ARRAY:
11685 name = ".init_array";
11686 goto get_vma;
11687 case DT_FINI_ARRAY:
11688 name = ".fini_array";
11689 goto get_vma;
11690
11691 case DT_HASH:
11692 name = ".hash";
11693 goto get_vma;
11694 case DT_GNU_HASH:
11695 name = ".gnu.hash";
11696 goto get_vma;
11697 case DT_STRTAB:
11698 name = ".dynstr";
11699 goto get_vma;
11700 case DT_SYMTAB:
11701 name = ".dynsym";
11702 goto get_vma;
11703 case DT_VERDEF:
11704 name = ".gnu.version_d";
11705 goto get_vma;
11706 case DT_VERNEED:
11707 name = ".gnu.version_r";
11708 goto get_vma;
11709 case DT_VERSYM:
11710 name = ".gnu.version";
11711 get_vma:
11712 o = bfd_get_section_by_name (abfd, name);
11713 if (o == NULL)
11714 {
11715 (*_bfd_error_handler)
11716 (_("%B: could not find output section %s"), abfd, name);
11717 goto error_return;
11718 }
11719 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
11720 {
11721 (*_bfd_error_handler)
11722 (_("warning: section '%s' is being made into a note"), name);
11723 bfd_set_error (bfd_error_nonrepresentable_section);
11724 goto error_return;
11725 }
11726 dyn.d_un.d_ptr = o->vma;
11727 break;
11728
11729 case DT_REL:
11730 case DT_RELA:
11731 case DT_RELSZ:
11732 case DT_RELASZ:
11733 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
11734 type = SHT_REL;
11735 else
11736 type = SHT_RELA;
11737 dyn.d_un.d_val = 0;
11738 dyn.d_un.d_ptr = 0;
11739 for (i = 1; i < elf_numsections (abfd); i++)
11740 {
11741 Elf_Internal_Shdr *hdr;
11742
11743 hdr = elf_elfsections (abfd)[i];
11744 if (hdr->sh_type == type
11745 && (hdr->sh_flags & SHF_ALLOC) != 0)
11746 {
11747 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
11748 dyn.d_un.d_val += hdr->sh_size;
11749 else
11750 {
11751 if (dyn.d_un.d_ptr == 0
11752 || hdr->sh_addr < dyn.d_un.d_ptr)
11753 dyn.d_un.d_ptr = hdr->sh_addr;
11754 }
11755 }
11756 }
11757 break;
11758 }
11759 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
11760 }
11761 }
11762
11763 /* If we have created any dynamic sections, then output them. */
11764 if (dynobj != NULL)
11765 {
11766 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
11767 goto error_return;
11768
11769 /* Check for DT_TEXTREL (late, in case the backend removes it). */
11770 if (((info->warn_shared_textrel && bfd_link_pic (info))
11771 || info->error_textrel)
11772 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
11773 {
11774 bfd_byte *dyncon, *dynconend;
11775
11776 dyncon = o->contents;
11777 dynconend = o->contents + o->size;
11778 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11779 {
11780 Elf_Internal_Dyn dyn;
11781
11782 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11783
11784 if (dyn.d_tag == DT_TEXTREL)
11785 {
11786 if (info->error_textrel)
11787 info->callbacks->einfo
11788 (_("%P%X: read-only segment has dynamic relocations.\n"));
11789 else
11790 info->callbacks->einfo
11791 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
11792 break;
11793 }
11794 }
11795 }
11796
11797 for (o = dynobj->sections; o != NULL; o = o->next)
11798 {
11799 if ((o->flags & SEC_HAS_CONTENTS) == 0
11800 || o->size == 0
11801 || o->output_section == bfd_abs_section_ptr)
11802 continue;
11803 if ((o->flags & SEC_LINKER_CREATED) == 0)
11804 {
11805 /* At this point, we are only interested in sections
11806 created by _bfd_elf_link_create_dynamic_sections. */
11807 continue;
11808 }
11809 if (elf_hash_table (info)->stab_info.stabstr == o)
11810 continue;
11811 if (elf_hash_table (info)->eh_info.hdr_sec == o)
11812 continue;
11813 if (strcmp (o->name, ".dynstr") != 0)
11814 {
11815 /* FIXME: octets_per_byte. */
11816 if (! bfd_set_section_contents (abfd, o->output_section,
11817 o->contents,
11818 (file_ptr) o->output_offset,
11819 o->size))
11820 goto error_return;
11821 }
11822 else
11823 {
11824 /* The contents of the .dynstr section are actually in a
11825 stringtab. */
11826 file_ptr off;
11827
11828 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
11829 if (bfd_seek (abfd, off, SEEK_SET) != 0
11830 || ! _bfd_elf_strtab_emit (abfd,
11831 elf_hash_table (info)->dynstr))
11832 goto error_return;
11833 }
11834 }
11835 }
11836
11837 if (bfd_link_relocatable (info))
11838 {
11839 bfd_boolean failed = FALSE;
11840
11841 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
11842 if (failed)
11843 goto error_return;
11844 }
11845
11846 /* If we have optimized stabs strings, output them. */
11847 if (elf_hash_table (info)->stab_info.stabstr != NULL)
11848 {
11849 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
11850 goto error_return;
11851 }
11852
11853 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
11854 goto error_return;
11855
11856 elf_final_link_free (abfd, &flinfo);
11857
11858 elf_linker (abfd) = TRUE;
11859
11860 if (attr_section)
11861 {
11862 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
11863 if (contents == NULL)
11864 return FALSE; /* Bail out and fail. */
11865 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
11866 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
11867 free (contents);
11868 }
11869
11870 return TRUE;
11871
11872 error_return:
11873 elf_final_link_free (abfd, &flinfo);
11874 return FALSE;
11875 }
11876 \f
11877 /* Initialize COOKIE for input bfd ABFD. */
11878
11879 static bfd_boolean
11880 init_reloc_cookie (struct elf_reloc_cookie *cookie,
11881 struct bfd_link_info *info, bfd *abfd)
11882 {
11883 Elf_Internal_Shdr *symtab_hdr;
11884 const struct elf_backend_data *bed;
11885
11886 bed = get_elf_backend_data (abfd);
11887 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11888
11889 cookie->abfd = abfd;
11890 cookie->sym_hashes = elf_sym_hashes (abfd);
11891 cookie->bad_symtab = elf_bad_symtab (abfd);
11892 if (cookie->bad_symtab)
11893 {
11894 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11895 cookie->extsymoff = 0;
11896 }
11897 else
11898 {
11899 cookie->locsymcount = symtab_hdr->sh_info;
11900 cookie->extsymoff = symtab_hdr->sh_info;
11901 }
11902
11903 if (bed->s->arch_size == 32)
11904 cookie->r_sym_shift = 8;
11905 else
11906 cookie->r_sym_shift = 32;
11907
11908 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
11909 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
11910 {
11911 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
11912 cookie->locsymcount, 0,
11913 NULL, NULL, NULL);
11914 if (cookie->locsyms == NULL)
11915 {
11916 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
11917 return FALSE;
11918 }
11919 if (info->keep_memory)
11920 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
11921 }
11922 return TRUE;
11923 }
11924
11925 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
11926
11927 static void
11928 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
11929 {
11930 Elf_Internal_Shdr *symtab_hdr;
11931
11932 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11933 if (cookie->locsyms != NULL
11934 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
11935 free (cookie->locsyms);
11936 }
11937
11938 /* Initialize the relocation information in COOKIE for input section SEC
11939 of input bfd ABFD. */
11940
11941 static bfd_boolean
11942 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11943 struct bfd_link_info *info, bfd *abfd,
11944 asection *sec)
11945 {
11946 const struct elf_backend_data *bed;
11947
11948 if (sec->reloc_count == 0)
11949 {
11950 cookie->rels = NULL;
11951 cookie->relend = NULL;
11952 }
11953 else
11954 {
11955 bed = get_elf_backend_data (abfd);
11956
11957 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
11958 info->keep_memory);
11959 if (cookie->rels == NULL)
11960 return FALSE;
11961 cookie->rel = cookie->rels;
11962 cookie->relend = (cookie->rels
11963 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
11964 }
11965 cookie->rel = cookie->rels;
11966 return TRUE;
11967 }
11968
11969 /* Free the memory allocated by init_reloc_cookie_rels,
11970 if appropriate. */
11971
11972 static void
11973 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11974 asection *sec)
11975 {
11976 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
11977 free (cookie->rels);
11978 }
11979
11980 /* Initialize the whole of COOKIE for input section SEC. */
11981
11982 static bfd_boolean
11983 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11984 struct bfd_link_info *info,
11985 asection *sec)
11986 {
11987 if (!init_reloc_cookie (cookie, info, sec->owner))
11988 goto error1;
11989 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
11990 goto error2;
11991 return TRUE;
11992
11993 error2:
11994 fini_reloc_cookie (cookie, sec->owner);
11995 error1:
11996 return FALSE;
11997 }
11998
11999 /* Free the memory allocated by init_reloc_cookie_for_section,
12000 if appropriate. */
12001
12002 static void
12003 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12004 asection *sec)
12005 {
12006 fini_reloc_cookie_rels (cookie, sec);
12007 fini_reloc_cookie (cookie, sec->owner);
12008 }
12009 \f
12010 /* Garbage collect unused sections. */
12011
12012 /* Default gc_mark_hook. */
12013
12014 asection *
12015 _bfd_elf_gc_mark_hook (asection *sec,
12016 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12017 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12018 struct elf_link_hash_entry *h,
12019 Elf_Internal_Sym *sym)
12020 {
12021 const char *sec_name;
12022
12023 if (h != NULL)
12024 {
12025 switch (h->root.type)
12026 {
12027 case bfd_link_hash_defined:
12028 case bfd_link_hash_defweak:
12029 return h->root.u.def.section;
12030
12031 case bfd_link_hash_common:
12032 return h->root.u.c.p->section;
12033
12034 case bfd_link_hash_undefined:
12035 case bfd_link_hash_undefweak:
12036 /* To work around a glibc bug, keep all XXX input sections
12037 when there is an as yet undefined reference to __start_XXX
12038 or __stop_XXX symbols. The linker will later define such
12039 symbols for orphan input sections that have a name
12040 representable as a C identifier. */
12041 if (strncmp (h->root.root.string, "__start_", 8) == 0)
12042 sec_name = h->root.root.string + 8;
12043 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
12044 sec_name = h->root.root.string + 7;
12045 else
12046 sec_name = NULL;
12047
12048 if (sec_name && *sec_name != '\0')
12049 {
12050 bfd *i;
12051
12052 for (i = info->input_bfds; i; i = i->link.next)
12053 {
12054 sec = bfd_get_section_by_name (i, sec_name);
12055 if (sec)
12056 sec->flags |= SEC_KEEP;
12057 }
12058 }
12059 break;
12060
12061 default:
12062 break;
12063 }
12064 }
12065 else
12066 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12067
12068 return NULL;
12069 }
12070
12071 /* COOKIE->rel describes a relocation against section SEC, which is
12072 a section we've decided to keep. Return the section that contains
12073 the relocation symbol, or NULL if no section contains it. */
12074
12075 asection *
12076 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12077 elf_gc_mark_hook_fn gc_mark_hook,
12078 struct elf_reloc_cookie *cookie)
12079 {
12080 unsigned long r_symndx;
12081 struct elf_link_hash_entry *h;
12082
12083 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
12084 if (r_symndx == STN_UNDEF)
12085 return NULL;
12086
12087 if (r_symndx >= cookie->locsymcount
12088 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12089 {
12090 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
12091 if (h == NULL)
12092 {
12093 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12094 sec->owner);
12095 return NULL;
12096 }
12097 while (h->root.type == bfd_link_hash_indirect
12098 || h->root.type == bfd_link_hash_warning)
12099 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12100 h->mark = 1;
12101 /* If this symbol is weak and there is a non-weak definition, we
12102 keep the non-weak definition because many backends put
12103 dynamic reloc info on the non-weak definition for code
12104 handling copy relocs. */
12105 if (h->u.weakdef != NULL)
12106 h->u.weakdef->mark = 1;
12107 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12108 }
12109
12110 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12111 &cookie->locsyms[r_symndx]);
12112 }
12113
12114 /* COOKIE->rel describes a relocation against section SEC, which is
12115 a section we've decided to keep. Mark the section that contains
12116 the relocation symbol. */
12117
12118 bfd_boolean
12119 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12120 asection *sec,
12121 elf_gc_mark_hook_fn gc_mark_hook,
12122 struct elf_reloc_cookie *cookie)
12123 {
12124 asection *rsec;
12125
12126 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie);
12127 if (rsec && !rsec->gc_mark)
12128 {
12129 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12130 || (rsec->owner->flags & DYNAMIC) != 0)
12131 rsec->gc_mark = 1;
12132 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12133 return FALSE;
12134 }
12135 return TRUE;
12136 }
12137
12138 /* The mark phase of garbage collection. For a given section, mark
12139 it and any sections in this section's group, and all the sections
12140 which define symbols to which it refers. */
12141
12142 bfd_boolean
12143 _bfd_elf_gc_mark (struct bfd_link_info *info,
12144 asection *sec,
12145 elf_gc_mark_hook_fn gc_mark_hook)
12146 {
12147 bfd_boolean ret;
12148 asection *group_sec, *eh_frame;
12149
12150 sec->gc_mark = 1;
12151
12152 /* Mark all the sections in the group. */
12153 group_sec = elf_section_data (sec)->next_in_group;
12154 if (group_sec && !group_sec->gc_mark)
12155 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
12156 return FALSE;
12157
12158 /* Look through the section relocs. */
12159 ret = TRUE;
12160 eh_frame = elf_eh_frame_section (sec->owner);
12161 if ((sec->flags & SEC_RELOC) != 0
12162 && sec->reloc_count > 0
12163 && sec != eh_frame)
12164 {
12165 struct elf_reloc_cookie cookie;
12166
12167 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12168 ret = FALSE;
12169 else
12170 {
12171 for (; cookie.rel < cookie.relend; cookie.rel++)
12172 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
12173 {
12174 ret = FALSE;
12175 break;
12176 }
12177 fini_reloc_cookie_for_section (&cookie, sec);
12178 }
12179 }
12180
12181 if (ret && eh_frame && elf_fde_list (sec))
12182 {
12183 struct elf_reloc_cookie cookie;
12184
12185 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12186 ret = FALSE;
12187 else
12188 {
12189 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12190 gc_mark_hook, &cookie))
12191 ret = FALSE;
12192 fini_reloc_cookie_for_section (&cookie, eh_frame);
12193 }
12194 }
12195
12196 eh_frame = elf_section_eh_frame_entry (sec);
12197 if (ret && eh_frame && !eh_frame->gc_mark)
12198 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12199 ret = FALSE;
12200
12201 return ret;
12202 }
12203
12204 /* Scan and mark sections in a special or debug section group. */
12205
12206 static void
12207 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12208 {
12209 /* Point to first section of section group. */
12210 asection *ssec;
12211 /* Used to iterate the section group. */
12212 asection *msec;
12213
12214 bfd_boolean is_special_grp = TRUE;
12215 bfd_boolean is_debug_grp = TRUE;
12216
12217 /* First scan to see if group contains any section other than debug
12218 and special section. */
12219 ssec = msec = elf_next_in_group (grp);
12220 do
12221 {
12222 if ((msec->flags & SEC_DEBUGGING) == 0)
12223 is_debug_grp = FALSE;
12224
12225 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12226 is_special_grp = FALSE;
12227
12228 msec = elf_next_in_group (msec);
12229 }
12230 while (msec != ssec);
12231
12232 /* If this is a pure debug section group or pure special section group,
12233 keep all sections in this group. */
12234 if (is_debug_grp || is_special_grp)
12235 {
12236 do
12237 {
12238 msec->gc_mark = 1;
12239 msec = elf_next_in_group (msec);
12240 }
12241 while (msec != ssec);
12242 }
12243 }
12244
12245 /* Keep debug and special sections. */
12246
12247 bfd_boolean
12248 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12249 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12250 {
12251 bfd *ibfd;
12252
12253 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12254 {
12255 asection *isec;
12256 bfd_boolean some_kept;
12257 bfd_boolean debug_frag_seen;
12258
12259 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12260 continue;
12261
12262 /* Ensure all linker created sections are kept,
12263 see if any other section is already marked,
12264 and note if we have any fragmented debug sections. */
12265 debug_frag_seen = some_kept = FALSE;
12266 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12267 {
12268 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12269 isec->gc_mark = 1;
12270 else if (isec->gc_mark)
12271 some_kept = TRUE;
12272
12273 if (debug_frag_seen == FALSE
12274 && (isec->flags & SEC_DEBUGGING)
12275 && CONST_STRNEQ (isec->name, ".debug_line."))
12276 debug_frag_seen = TRUE;
12277 }
12278
12279 /* If no section in this file will be kept, then we can
12280 toss out the debug and special sections. */
12281 if (!some_kept)
12282 continue;
12283
12284 /* Keep debug and special sections like .comment when they are
12285 not part of a group. Also keep section groups that contain
12286 just debug sections or special sections. */
12287 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12288 {
12289 if ((isec->flags & SEC_GROUP) != 0)
12290 _bfd_elf_gc_mark_debug_special_section_group (isec);
12291 else if (((isec->flags & SEC_DEBUGGING) != 0
12292 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12293 && elf_next_in_group (isec) == NULL)
12294 isec->gc_mark = 1;
12295 }
12296
12297 if (! debug_frag_seen)
12298 continue;
12299
12300 /* Look for CODE sections which are going to be discarded,
12301 and find and discard any fragmented debug sections which
12302 are associated with that code section. */
12303 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12304 if ((isec->flags & SEC_CODE) != 0
12305 && isec->gc_mark == 0)
12306 {
12307 unsigned int ilen;
12308 asection *dsec;
12309
12310 ilen = strlen (isec->name);
12311
12312 /* Association is determined by the name of the debug section
12313 containing the name of the code section as a suffix. For
12314 example .debug_line.text.foo is a debug section associated
12315 with .text.foo. */
12316 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12317 {
12318 unsigned int dlen;
12319
12320 if (dsec->gc_mark == 0
12321 || (dsec->flags & SEC_DEBUGGING) == 0)
12322 continue;
12323
12324 dlen = strlen (dsec->name);
12325
12326 if (dlen > ilen
12327 && strncmp (dsec->name + (dlen - ilen),
12328 isec->name, ilen) == 0)
12329 {
12330 dsec->gc_mark = 0;
12331 }
12332 }
12333 }
12334 }
12335 return TRUE;
12336 }
12337
12338 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
12339
12340 struct elf_gc_sweep_symbol_info
12341 {
12342 struct bfd_link_info *info;
12343 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
12344 bfd_boolean);
12345 };
12346
12347 static bfd_boolean
12348 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
12349 {
12350 if (!h->mark
12351 && (((h->root.type == bfd_link_hash_defined
12352 || h->root.type == bfd_link_hash_defweak)
12353 && !((h->def_regular || ELF_COMMON_DEF_P (h))
12354 && h->root.u.def.section->gc_mark))
12355 || h->root.type == bfd_link_hash_undefined
12356 || h->root.type == bfd_link_hash_undefweak))
12357 {
12358 struct elf_gc_sweep_symbol_info *inf;
12359
12360 inf = (struct elf_gc_sweep_symbol_info *) data;
12361 (*inf->hide_symbol) (inf->info, h, TRUE);
12362 h->def_regular = 0;
12363 h->ref_regular = 0;
12364 h->ref_regular_nonweak = 0;
12365 }
12366
12367 return TRUE;
12368 }
12369
12370 /* The sweep phase of garbage collection. Remove all garbage sections. */
12371
12372 typedef bfd_boolean (*gc_sweep_hook_fn)
12373 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
12374
12375 static bfd_boolean
12376 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
12377 {
12378 bfd *sub;
12379 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12380 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
12381 unsigned long section_sym_count;
12382 struct elf_gc_sweep_symbol_info sweep_info;
12383
12384 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12385 {
12386 asection *o;
12387
12388 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12389 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
12390 continue;
12391
12392 for (o = sub->sections; o != NULL; o = o->next)
12393 {
12394 /* When any section in a section group is kept, we keep all
12395 sections in the section group. If the first member of
12396 the section group is excluded, we will also exclude the
12397 group section. */
12398 if (o->flags & SEC_GROUP)
12399 {
12400 asection *first = elf_next_in_group (o);
12401 o->gc_mark = first->gc_mark;
12402 }
12403
12404 if (o->gc_mark)
12405 continue;
12406
12407 /* Skip sweeping sections already excluded. */
12408 if (o->flags & SEC_EXCLUDE)
12409 continue;
12410
12411 /* Since this is early in the link process, it is simple
12412 to remove a section from the output. */
12413 o->flags |= SEC_EXCLUDE;
12414
12415 if (info->print_gc_sections && o->size != 0)
12416 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
12417
12418 /* But we also have to update some of the relocation
12419 info we collected before. */
12420 if (gc_sweep_hook
12421 && (o->flags & SEC_RELOC) != 0
12422 && o->reloc_count != 0
12423 && !((info->strip == strip_all || info->strip == strip_debugger)
12424 && (o->flags & SEC_DEBUGGING) != 0)
12425 && !bfd_is_abs_section (o->output_section))
12426 {
12427 Elf_Internal_Rela *internal_relocs;
12428 bfd_boolean r;
12429
12430 internal_relocs
12431 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
12432 info->keep_memory);
12433 if (internal_relocs == NULL)
12434 return FALSE;
12435
12436 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
12437
12438 if (elf_section_data (o)->relocs != internal_relocs)
12439 free (internal_relocs);
12440
12441 if (!r)
12442 return FALSE;
12443 }
12444 }
12445 }
12446
12447 /* Remove the symbols that were in the swept sections from the dynamic
12448 symbol table. GCFIXME: Anyone know how to get them out of the
12449 static symbol table as well? */
12450 sweep_info.info = info;
12451 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
12452 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
12453 &sweep_info);
12454
12455 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
12456 return TRUE;
12457 }
12458
12459 /* Propagate collected vtable information. This is called through
12460 elf_link_hash_traverse. */
12461
12462 static bfd_boolean
12463 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
12464 {
12465 /* Those that are not vtables. */
12466 if (h->vtable == NULL || h->vtable->parent == NULL)
12467 return TRUE;
12468
12469 /* Those vtables that do not have parents, we cannot merge. */
12470 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
12471 return TRUE;
12472
12473 /* If we've already been done, exit. */
12474 if (h->vtable->used && h->vtable->used[-1])
12475 return TRUE;
12476
12477 /* Make sure the parent's table is up to date. */
12478 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
12479
12480 if (h->vtable->used == NULL)
12481 {
12482 /* None of this table's entries were referenced. Re-use the
12483 parent's table. */
12484 h->vtable->used = h->vtable->parent->vtable->used;
12485 h->vtable->size = h->vtable->parent->vtable->size;
12486 }
12487 else
12488 {
12489 size_t n;
12490 bfd_boolean *cu, *pu;
12491
12492 /* Or the parent's entries into ours. */
12493 cu = h->vtable->used;
12494 cu[-1] = TRUE;
12495 pu = h->vtable->parent->vtable->used;
12496 if (pu != NULL)
12497 {
12498 const struct elf_backend_data *bed;
12499 unsigned int log_file_align;
12500
12501 bed = get_elf_backend_data (h->root.u.def.section->owner);
12502 log_file_align = bed->s->log_file_align;
12503 n = h->vtable->parent->vtable->size >> log_file_align;
12504 while (n--)
12505 {
12506 if (*pu)
12507 *cu = TRUE;
12508 pu++;
12509 cu++;
12510 }
12511 }
12512 }
12513
12514 return TRUE;
12515 }
12516
12517 static bfd_boolean
12518 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
12519 {
12520 asection *sec;
12521 bfd_vma hstart, hend;
12522 Elf_Internal_Rela *relstart, *relend, *rel;
12523 const struct elf_backend_data *bed;
12524 unsigned int log_file_align;
12525
12526 /* Take care of both those symbols that do not describe vtables as
12527 well as those that are not loaded. */
12528 if (h->vtable == NULL || h->vtable->parent == NULL)
12529 return TRUE;
12530
12531 BFD_ASSERT (h->root.type == bfd_link_hash_defined
12532 || h->root.type == bfd_link_hash_defweak);
12533
12534 sec = h->root.u.def.section;
12535 hstart = h->root.u.def.value;
12536 hend = hstart + h->size;
12537
12538 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
12539 if (!relstart)
12540 return *(bfd_boolean *) okp = FALSE;
12541 bed = get_elf_backend_data (sec->owner);
12542 log_file_align = bed->s->log_file_align;
12543
12544 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
12545
12546 for (rel = relstart; rel < relend; ++rel)
12547 if (rel->r_offset >= hstart && rel->r_offset < hend)
12548 {
12549 /* If the entry is in use, do nothing. */
12550 if (h->vtable->used
12551 && (rel->r_offset - hstart) < h->vtable->size)
12552 {
12553 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
12554 if (h->vtable->used[entry])
12555 continue;
12556 }
12557 /* Otherwise, kill it. */
12558 rel->r_offset = rel->r_info = rel->r_addend = 0;
12559 }
12560
12561 return TRUE;
12562 }
12563
12564 /* Mark sections containing dynamically referenced symbols. When
12565 building shared libraries, we must assume that any visible symbol is
12566 referenced. */
12567
12568 bfd_boolean
12569 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
12570 {
12571 struct bfd_link_info *info = (struct bfd_link_info *) inf;
12572 struct bfd_elf_dynamic_list *d = info->dynamic_list;
12573
12574 if ((h->root.type == bfd_link_hash_defined
12575 || h->root.type == bfd_link_hash_defweak)
12576 && (h->ref_dynamic
12577 || ((h->def_regular || ELF_COMMON_DEF_P (h))
12578 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
12579 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
12580 && (!bfd_link_executable (info)
12581 || info->export_dynamic
12582 || (h->dynamic
12583 && d != NULL
12584 && (*d->match) (&d->head, NULL, h->root.root.string)))
12585 && (h->versioned >= versioned
12586 || !bfd_hide_sym_by_version (info->version_info,
12587 h->root.root.string)))))
12588 h->root.u.def.section->flags |= SEC_KEEP;
12589
12590 return TRUE;
12591 }
12592
12593 /* Keep all sections containing symbols undefined on the command-line,
12594 and the section containing the entry symbol. */
12595
12596 void
12597 _bfd_elf_gc_keep (struct bfd_link_info *info)
12598 {
12599 struct bfd_sym_chain *sym;
12600
12601 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
12602 {
12603 struct elf_link_hash_entry *h;
12604
12605 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
12606 FALSE, FALSE, FALSE);
12607
12608 if (h != NULL
12609 && (h->root.type == bfd_link_hash_defined
12610 || h->root.type == bfd_link_hash_defweak)
12611 && !bfd_is_abs_section (h->root.u.def.section))
12612 h->root.u.def.section->flags |= SEC_KEEP;
12613 }
12614 }
12615
12616 bfd_boolean
12617 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
12618 struct bfd_link_info *info)
12619 {
12620 bfd *ibfd = info->input_bfds;
12621
12622 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12623 {
12624 asection *sec;
12625 struct elf_reloc_cookie cookie;
12626
12627 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12628 continue;
12629
12630 if (!init_reloc_cookie (&cookie, info, ibfd))
12631 return FALSE;
12632
12633 for (sec = ibfd->sections; sec; sec = sec->next)
12634 {
12635 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
12636 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
12637 {
12638 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
12639 fini_reloc_cookie_rels (&cookie, sec);
12640 }
12641 }
12642 }
12643 return TRUE;
12644 }
12645
12646 /* Do mark and sweep of unused sections. */
12647
12648 bfd_boolean
12649 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
12650 {
12651 bfd_boolean ok = TRUE;
12652 bfd *sub;
12653 elf_gc_mark_hook_fn gc_mark_hook;
12654 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12655 struct elf_link_hash_table *htab;
12656
12657 if (!bed->can_gc_sections
12658 || !is_elf_hash_table (info->hash))
12659 {
12660 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
12661 return TRUE;
12662 }
12663
12664 bed->gc_keep (info);
12665 htab = elf_hash_table (info);
12666
12667 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
12668 at the .eh_frame section if we can mark the FDEs individually. */
12669 for (sub = info->input_bfds;
12670 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
12671 sub = sub->link.next)
12672 {
12673 asection *sec;
12674 struct elf_reloc_cookie cookie;
12675
12676 sec = bfd_get_section_by_name (sub, ".eh_frame");
12677 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
12678 {
12679 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
12680 if (elf_section_data (sec)->sec_info
12681 && (sec->flags & SEC_LINKER_CREATED) == 0)
12682 elf_eh_frame_section (sub) = sec;
12683 fini_reloc_cookie_for_section (&cookie, sec);
12684 sec = bfd_get_next_section_by_name (sec);
12685 }
12686 }
12687
12688 /* Apply transitive closure to the vtable entry usage info. */
12689 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
12690 if (!ok)
12691 return FALSE;
12692
12693 /* Kill the vtable relocations that were not used. */
12694 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
12695 if (!ok)
12696 return FALSE;
12697
12698 /* Mark dynamically referenced symbols. */
12699 if (htab->dynamic_sections_created)
12700 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
12701
12702 /* Grovel through relocs to find out who stays ... */
12703 gc_mark_hook = bed->gc_mark_hook;
12704 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12705 {
12706 asection *o;
12707
12708 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12709 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
12710 continue;
12711
12712 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
12713 Also treat note sections as a root, if the section is not part
12714 of a group. */
12715 for (o = sub->sections; o != NULL; o = o->next)
12716 if (!o->gc_mark
12717 && (o->flags & SEC_EXCLUDE) == 0
12718 && ((o->flags & SEC_KEEP) != 0
12719 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
12720 && elf_next_in_group (o) == NULL )))
12721 {
12722 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12723 return FALSE;
12724 }
12725 }
12726
12727 /* Allow the backend to mark additional target specific sections. */
12728 bed->gc_mark_extra_sections (info, gc_mark_hook);
12729
12730 /* ... and mark SEC_EXCLUDE for those that go. */
12731 return elf_gc_sweep (abfd, info);
12732 }
12733 \f
12734 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
12735
12736 bfd_boolean
12737 bfd_elf_gc_record_vtinherit (bfd *abfd,
12738 asection *sec,
12739 struct elf_link_hash_entry *h,
12740 bfd_vma offset)
12741 {
12742 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
12743 struct elf_link_hash_entry **search, *child;
12744 bfd_size_type extsymcount;
12745 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12746
12747 /* The sh_info field of the symtab header tells us where the
12748 external symbols start. We don't care about the local symbols at
12749 this point. */
12750 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
12751 if (!elf_bad_symtab (abfd))
12752 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
12753
12754 sym_hashes = elf_sym_hashes (abfd);
12755 sym_hashes_end = sym_hashes + extsymcount;
12756
12757 /* Hunt down the child symbol, which is in this section at the same
12758 offset as the relocation. */
12759 for (search = sym_hashes; search != sym_hashes_end; ++search)
12760 {
12761 if ((child = *search) != NULL
12762 && (child->root.type == bfd_link_hash_defined
12763 || child->root.type == bfd_link_hash_defweak)
12764 && child->root.u.def.section == sec
12765 && child->root.u.def.value == offset)
12766 goto win;
12767 }
12768
12769 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
12770 abfd, sec, (unsigned long) offset);
12771 bfd_set_error (bfd_error_invalid_operation);
12772 return FALSE;
12773
12774 win:
12775 if (!child->vtable)
12776 {
12777 child->vtable = ((struct elf_link_virtual_table_entry *)
12778 bfd_zalloc (abfd, sizeof (*child->vtable)));
12779 if (!child->vtable)
12780 return FALSE;
12781 }
12782 if (!h)
12783 {
12784 /* This *should* only be the absolute section. It could potentially
12785 be that someone has defined a non-global vtable though, which
12786 would be bad. It isn't worth paging in the local symbols to be
12787 sure though; that case should simply be handled by the assembler. */
12788
12789 child->vtable->parent = (struct elf_link_hash_entry *) -1;
12790 }
12791 else
12792 child->vtable->parent = h;
12793
12794 return TRUE;
12795 }
12796
12797 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
12798
12799 bfd_boolean
12800 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
12801 asection *sec ATTRIBUTE_UNUSED,
12802 struct elf_link_hash_entry *h,
12803 bfd_vma addend)
12804 {
12805 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12806 unsigned int log_file_align = bed->s->log_file_align;
12807
12808 if (!h->vtable)
12809 {
12810 h->vtable = ((struct elf_link_virtual_table_entry *)
12811 bfd_zalloc (abfd, sizeof (*h->vtable)));
12812 if (!h->vtable)
12813 return FALSE;
12814 }
12815
12816 if (addend >= h->vtable->size)
12817 {
12818 size_t size, bytes, file_align;
12819 bfd_boolean *ptr = h->vtable->used;
12820
12821 /* While the symbol is undefined, we have to be prepared to handle
12822 a zero size. */
12823 file_align = 1 << log_file_align;
12824 if (h->root.type == bfd_link_hash_undefined)
12825 size = addend + file_align;
12826 else
12827 {
12828 size = h->size;
12829 if (addend >= size)
12830 {
12831 /* Oops! We've got a reference past the defined end of
12832 the table. This is probably a bug -- shall we warn? */
12833 size = addend + file_align;
12834 }
12835 }
12836 size = (size + file_align - 1) & -file_align;
12837
12838 /* Allocate one extra entry for use as a "done" flag for the
12839 consolidation pass. */
12840 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
12841
12842 if (ptr)
12843 {
12844 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
12845
12846 if (ptr != NULL)
12847 {
12848 size_t oldbytes;
12849
12850 oldbytes = (((h->vtable->size >> log_file_align) + 1)
12851 * sizeof (bfd_boolean));
12852 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
12853 }
12854 }
12855 else
12856 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
12857
12858 if (ptr == NULL)
12859 return FALSE;
12860
12861 /* And arrange for that done flag to be at index -1. */
12862 h->vtable->used = ptr + 1;
12863 h->vtable->size = size;
12864 }
12865
12866 h->vtable->used[addend >> log_file_align] = TRUE;
12867
12868 return TRUE;
12869 }
12870
12871 /* Map an ELF section header flag to its corresponding string. */
12872 typedef struct
12873 {
12874 char *flag_name;
12875 flagword flag_value;
12876 } elf_flags_to_name_table;
12877
12878 static elf_flags_to_name_table elf_flags_to_names [] =
12879 {
12880 { "SHF_WRITE", SHF_WRITE },
12881 { "SHF_ALLOC", SHF_ALLOC },
12882 { "SHF_EXECINSTR", SHF_EXECINSTR },
12883 { "SHF_MERGE", SHF_MERGE },
12884 { "SHF_STRINGS", SHF_STRINGS },
12885 { "SHF_INFO_LINK", SHF_INFO_LINK},
12886 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
12887 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
12888 { "SHF_GROUP", SHF_GROUP },
12889 { "SHF_TLS", SHF_TLS },
12890 { "SHF_MASKOS", SHF_MASKOS },
12891 { "SHF_EXCLUDE", SHF_EXCLUDE },
12892 };
12893
12894 /* Returns TRUE if the section is to be included, otherwise FALSE. */
12895 bfd_boolean
12896 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
12897 struct flag_info *flaginfo,
12898 asection *section)
12899 {
12900 const bfd_vma sh_flags = elf_section_flags (section);
12901
12902 if (!flaginfo->flags_initialized)
12903 {
12904 bfd *obfd = info->output_bfd;
12905 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
12906 struct flag_info_list *tf = flaginfo->flag_list;
12907 int with_hex = 0;
12908 int without_hex = 0;
12909
12910 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
12911 {
12912 unsigned i;
12913 flagword (*lookup) (char *);
12914
12915 lookup = bed->elf_backend_lookup_section_flags_hook;
12916 if (lookup != NULL)
12917 {
12918 flagword hexval = (*lookup) ((char *) tf->name);
12919
12920 if (hexval != 0)
12921 {
12922 if (tf->with == with_flags)
12923 with_hex |= hexval;
12924 else if (tf->with == without_flags)
12925 without_hex |= hexval;
12926 tf->valid = TRUE;
12927 continue;
12928 }
12929 }
12930 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
12931 {
12932 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
12933 {
12934 if (tf->with == with_flags)
12935 with_hex |= elf_flags_to_names[i].flag_value;
12936 else if (tf->with == without_flags)
12937 without_hex |= elf_flags_to_names[i].flag_value;
12938 tf->valid = TRUE;
12939 break;
12940 }
12941 }
12942 if (!tf->valid)
12943 {
12944 info->callbacks->einfo
12945 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
12946 return FALSE;
12947 }
12948 }
12949 flaginfo->flags_initialized = TRUE;
12950 flaginfo->only_with_flags |= with_hex;
12951 flaginfo->not_with_flags |= without_hex;
12952 }
12953
12954 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
12955 return FALSE;
12956
12957 if ((flaginfo->not_with_flags & sh_flags) != 0)
12958 return FALSE;
12959
12960 return TRUE;
12961 }
12962
12963 struct alloc_got_off_arg {
12964 bfd_vma gotoff;
12965 struct bfd_link_info *info;
12966 };
12967
12968 /* We need a special top-level link routine to convert got reference counts
12969 to real got offsets. */
12970
12971 static bfd_boolean
12972 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
12973 {
12974 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
12975 bfd *obfd = gofarg->info->output_bfd;
12976 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
12977
12978 if (h->got.refcount > 0)
12979 {
12980 h->got.offset = gofarg->gotoff;
12981 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
12982 }
12983 else
12984 h->got.offset = (bfd_vma) -1;
12985
12986 return TRUE;
12987 }
12988
12989 /* And an accompanying bit to work out final got entry offsets once
12990 we're done. Should be called from final_link. */
12991
12992 bfd_boolean
12993 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
12994 struct bfd_link_info *info)
12995 {
12996 bfd *i;
12997 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12998 bfd_vma gotoff;
12999 struct alloc_got_off_arg gofarg;
13000
13001 BFD_ASSERT (abfd == info->output_bfd);
13002
13003 if (! is_elf_hash_table (info->hash))
13004 return FALSE;
13005
13006 /* The GOT offset is relative to the .got section, but the GOT header is
13007 put into the .got.plt section, if the backend uses it. */
13008 if (bed->want_got_plt)
13009 gotoff = 0;
13010 else
13011 gotoff = bed->got_header_size;
13012
13013 /* Do the local .got entries first. */
13014 for (i = info->input_bfds; i; i = i->link.next)
13015 {
13016 bfd_signed_vma *local_got;
13017 bfd_size_type j, locsymcount;
13018 Elf_Internal_Shdr *symtab_hdr;
13019
13020 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13021 continue;
13022
13023 local_got = elf_local_got_refcounts (i);
13024 if (!local_got)
13025 continue;
13026
13027 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13028 if (elf_bad_symtab (i))
13029 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13030 else
13031 locsymcount = symtab_hdr->sh_info;
13032
13033 for (j = 0; j < locsymcount; ++j)
13034 {
13035 if (local_got[j] > 0)
13036 {
13037 local_got[j] = gotoff;
13038 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
13039 }
13040 else
13041 local_got[j] = (bfd_vma) -1;
13042 }
13043 }
13044
13045 /* Then the global .got entries. .plt refcounts are handled by
13046 adjust_dynamic_symbol */
13047 gofarg.gotoff = gotoff;
13048 gofarg.info = info;
13049 elf_link_hash_traverse (elf_hash_table (info),
13050 elf_gc_allocate_got_offsets,
13051 &gofarg);
13052 return TRUE;
13053 }
13054
13055 /* Many folk need no more in the way of final link than this, once
13056 got entry reference counting is enabled. */
13057
13058 bfd_boolean
13059 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13060 {
13061 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13062 return FALSE;
13063
13064 /* Invoke the regular ELF backend linker to do all the work. */
13065 return bfd_elf_final_link (abfd, info);
13066 }
13067
13068 bfd_boolean
13069 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13070 {
13071 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
13072
13073 if (rcookie->bad_symtab)
13074 rcookie->rel = rcookie->rels;
13075
13076 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13077 {
13078 unsigned long r_symndx;
13079
13080 if (! rcookie->bad_symtab)
13081 if (rcookie->rel->r_offset > offset)
13082 return FALSE;
13083 if (rcookie->rel->r_offset != offset)
13084 continue;
13085
13086 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
13087 if (r_symndx == STN_UNDEF)
13088 return TRUE;
13089
13090 if (r_symndx >= rcookie->locsymcount
13091 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13092 {
13093 struct elf_link_hash_entry *h;
13094
13095 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13096
13097 while (h->root.type == bfd_link_hash_indirect
13098 || h->root.type == bfd_link_hash_warning)
13099 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13100
13101 if ((h->root.type == bfd_link_hash_defined
13102 || h->root.type == bfd_link_hash_defweak)
13103 && (h->root.u.def.section->owner != rcookie->abfd
13104 || h->root.u.def.section->kept_section != NULL
13105 || discarded_section (h->root.u.def.section)))
13106 return TRUE;
13107 }
13108 else
13109 {
13110 /* It's not a relocation against a global symbol,
13111 but it could be a relocation against a local
13112 symbol for a discarded section. */
13113 asection *isec;
13114 Elf_Internal_Sym *isym;
13115
13116 /* Need to: get the symbol; get the section. */
13117 isym = &rcookie->locsyms[r_symndx];
13118 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
13119 if (isec != NULL
13120 && (isec->kept_section != NULL
13121 || discarded_section (isec)))
13122 return TRUE;
13123 }
13124 return FALSE;
13125 }
13126 return FALSE;
13127 }
13128
13129 /* Discard unneeded references to discarded sections.
13130 Returns -1 on error, 1 if any section's size was changed, 0 if
13131 nothing changed. This function assumes that the relocations are in
13132 sorted order, which is true for all known assemblers. */
13133
13134 int
13135 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13136 {
13137 struct elf_reloc_cookie cookie;
13138 asection *o;
13139 bfd *abfd;
13140 int changed = 0;
13141
13142 if (info->traditional_format
13143 || !is_elf_hash_table (info->hash))
13144 return 0;
13145
13146 o = bfd_get_section_by_name (output_bfd, ".stab");
13147 if (o != NULL)
13148 {
13149 asection *i;
13150
13151 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13152 {
13153 if (i->size == 0
13154 || i->reloc_count == 0
13155 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13156 continue;
13157
13158 abfd = i->owner;
13159 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13160 continue;
13161
13162 if (!init_reloc_cookie_for_section (&cookie, info, i))
13163 return -1;
13164
13165 if (_bfd_discard_section_stabs (abfd, i,
13166 elf_section_data (i)->sec_info,
13167 bfd_elf_reloc_symbol_deleted_p,
13168 &cookie))
13169 changed = 1;
13170
13171 fini_reloc_cookie_for_section (&cookie, i);
13172 }
13173 }
13174
13175 o = NULL;
13176 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13177 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
13178 if (o != NULL)
13179 {
13180 asection *i;
13181
13182 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13183 {
13184 if (i->size == 0)
13185 continue;
13186
13187 abfd = i->owner;
13188 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13189 continue;
13190
13191 if (!init_reloc_cookie_for_section (&cookie, info, i))
13192 return -1;
13193
13194 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13195 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
13196 bfd_elf_reloc_symbol_deleted_p,
13197 &cookie))
13198 changed = 1;
13199
13200 fini_reloc_cookie_for_section (&cookie, i);
13201 }
13202 }
13203
13204 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13205 {
13206 const struct elf_backend_data *bed;
13207
13208 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13209 continue;
13210
13211 bed = get_elf_backend_data (abfd);
13212
13213 if (bed->elf_backend_discard_info != NULL)
13214 {
13215 if (!init_reloc_cookie (&cookie, info, abfd))
13216 return -1;
13217
13218 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
13219 changed = 1;
13220
13221 fini_reloc_cookie (&cookie, abfd);
13222 }
13223 }
13224
13225 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13226 _bfd_elf_end_eh_frame_parsing (info);
13227
13228 if (info->eh_frame_hdr_type
13229 && !bfd_link_relocatable (info)
13230 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
13231 changed = 1;
13232
13233 return changed;
13234 }
13235
13236 bfd_boolean
13237 _bfd_elf_section_already_linked (bfd *abfd,
13238 asection *sec,
13239 struct bfd_link_info *info)
13240 {
13241 flagword flags;
13242 const char *name, *key;
13243 struct bfd_section_already_linked *l;
13244 struct bfd_section_already_linked_hash_entry *already_linked_list;
13245
13246 if (sec->output_section == bfd_abs_section_ptr)
13247 return FALSE;
13248
13249 flags = sec->flags;
13250
13251 /* Return if it isn't a linkonce section. A comdat group section
13252 also has SEC_LINK_ONCE set. */
13253 if ((flags & SEC_LINK_ONCE) == 0)
13254 return FALSE;
13255
13256 /* Don't put group member sections on our list of already linked
13257 sections. They are handled as a group via their group section. */
13258 if (elf_sec_group (sec) != NULL)
13259 return FALSE;
13260
13261 /* For a SHT_GROUP section, use the group signature as the key. */
13262 name = sec->name;
13263 if ((flags & SEC_GROUP) != 0
13264 && elf_next_in_group (sec) != NULL
13265 && elf_group_name (elf_next_in_group (sec)) != NULL)
13266 key = elf_group_name (elf_next_in_group (sec));
13267 else
13268 {
13269 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
13270 if (CONST_STRNEQ (name, ".gnu.linkonce.")
13271 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13272 key++;
13273 else
13274 /* Must be a user linkonce section that doesn't follow gcc's
13275 naming convention. In this case we won't be matching
13276 single member groups. */
13277 key = name;
13278 }
13279
13280 already_linked_list = bfd_section_already_linked_table_lookup (key);
13281
13282 for (l = already_linked_list->entry; l != NULL; l = l->next)
13283 {
13284 /* We may have 2 different types of sections on the list: group
13285 sections with a signature of <key> (<key> is some string),
13286 and linkonce sections named .gnu.linkonce.<type>.<key>.
13287 Match like sections. LTO plugin sections are an exception.
13288 They are always named .gnu.linkonce.t.<key> and match either
13289 type of section. */
13290 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13291 && ((flags & SEC_GROUP) != 0
13292 || strcmp (name, l->sec->name) == 0))
13293 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
13294 {
13295 /* The section has already been linked. See if we should
13296 issue a warning. */
13297 if (!_bfd_handle_already_linked (sec, l, info))
13298 return FALSE;
13299
13300 if (flags & SEC_GROUP)
13301 {
13302 asection *first = elf_next_in_group (sec);
13303 asection *s = first;
13304
13305 while (s != NULL)
13306 {
13307 s->output_section = bfd_abs_section_ptr;
13308 /* Record which group discards it. */
13309 s->kept_section = l->sec;
13310 s = elf_next_in_group (s);
13311 /* These lists are circular. */
13312 if (s == first)
13313 break;
13314 }
13315 }
13316
13317 return TRUE;
13318 }
13319 }
13320
13321 /* A single member comdat group section may be discarded by a
13322 linkonce section and vice versa. */
13323 if ((flags & SEC_GROUP) != 0)
13324 {
13325 asection *first = elf_next_in_group (sec);
13326
13327 if (first != NULL && elf_next_in_group (first) == first)
13328 /* Check this single member group against linkonce sections. */
13329 for (l = already_linked_list->entry; l != NULL; l = l->next)
13330 if ((l->sec->flags & SEC_GROUP) == 0
13331 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13332 {
13333 first->output_section = bfd_abs_section_ptr;
13334 first->kept_section = l->sec;
13335 sec->output_section = bfd_abs_section_ptr;
13336 break;
13337 }
13338 }
13339 else
13340 /* Check this linkonce section against single member groups. */
13341 for (l = already_linked_list->entry; l != NULL; l = l->next)
13342 if (l->sec->flags & SEC_GROUP)
13343 {
13344 asection *first = elf_next_in_group (l->sec);
13345
13346 if (first != NULL
13347 && elf_next_in_group (first) == first
13348 && bfd_elf_match_symbols_in_sections (first, sec, info))
13349 {
13350 sec->output_section = bfd_abs_section_ptr;
13351 sec->kept_section = first;
13352 break;
13353 }
13354 }
13355
13356 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13357 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13358 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13359 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13360 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13361 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13362 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13363 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13364 The reverse order cannot happen as there is never a bfd with only the
13365 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13366 matter as here were are looking only for cross-bfd sections. */
13367
13368 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
13369 for (l = already_linked_list->entry; l != NULL; l = l->next)
13370 if ((l->sec->flags & SEC_GROUP) == 0
13371 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
13372 {
13373 if (abfd != l->sec->owner)
13374 sec->output_section = bfd_abs_section_ptr;
13375 break;
13376 }
13377
13378 /* This is the first section with this name. Record it. */
13379 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
13380 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
13381 return sec->output_section == bfd_abs_section_ptr;
13382 }
13383
13384 bfd_boolean
13385 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
13386 {
13387 return sym->st_shndx == SHN_COMMON;
13388 }
13389
13390 unsigned int
13391 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
13392 {
13393 return SHN_COMMON;
13394 }
13395
13396 asection *
13397 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
13398 {
13399 return bfd_com_section_ptr;
13400 }
13401
13402 bfd_vma
13403 _bfd_elf_default_got_elt_size (bfd *abfd,
13404 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13405 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
13406 bfd *ibfd ATTRIBUTE_UNUSED,
13407 unsigned long symndx ATTRIBUTE_UNUSED)
13408 {
13409 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13410 return bed->s->arch_size / 8;
13411 }
13412
13413 /* Routines to support the creation of dynamic relocs. */
13414
13415 /* Returns the name of the dynamic reloc section associated with SEC. */
13416
13417 static const char *
13418 get_dynamic_reloc_section_name (bfd * abfd,
13419 asection * sec,
13420 bfd_boolean is_rela)
13421 {
13422 char *name;
13423 const char *old_name = bfd_get_section_name (NULL, sec);
13424 const char *prefix = is_rela ? ".rela" : ".rel";
13425
13426 if (old_name == NULL)
13427 return NULL;
13428
13429 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
13430 sprintf (name, "%s%s", prefix, old_name);
13431
13432 return name;
13433 }
13434
13435 /* Returns the dynamic reloc section associated with SEC.
13436 If necessary compute the name of the dynamic reloc section based
13437 on SEC's name (looked up in ABFD's string table) and the setting
13438 of IS_RELA. */
13439
13440 asection *
13441 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
13442 asection * sec,
13443 bfd_boolean is_rela)
13444 {
13445 asection * reloc_sec = elf_section_data (sec)->sreloc;
13446
13447 if (reloc_sec == NULL)
13448 {
13449 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13450
13451 if (name != NULL)
13452 {
13453 reloc_sec = bfd_get_linker_section (abfd, name);
13454
13455 if (reloc_sec != NULL)
13456 elf_section_data (sec)->sreloc = reloc_sec;
13457 }
13458 }
13459
13460 return reloc_sec;
13461 }
13462
13463 /* Returns the dynamic reloc section associated with SEC. If the
13464 section does not exist it is created and attached to the DYNOBJ
13465 bfd and stored in the SRELOC field of SEC's elf_section_data
13466 structure.
13467
13468 ALIGNMENT is the alignment for the newly created section and
13469 IS_RELA defines whether the name should be .rela.<SEC's name>
13470 or .rel.<SEC's name>. The section name is looked up in the
13471 string table associated with ABFD. */
13472
13473 asection *
13474 _bfd_elf_make_dynamic_reloc_section (asection *sec,
13475 bfd *dynobj,
13476 unsigned int alignment,
13477 bfd *abfd,
13478 bfd_boolean is_rela)
13479 {
13480 asection * reloc_sec = elf_section_data (sec)->sreloc;
13481
13482 if (reloc_sec == NULL)
13483 {
13484 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13485
13486 if (name == NULL)
13487 return NULL;
13488
13489 reloc_sec = bfd_get_linker_section (dynobj, name);
13490
13491 if (reloc_sec == NULL)
13492 {
13493 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
13494 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
13495 if ((sec->flags & SEC_ALLOC) != 0)
13496 flags |= SEC_ALLOC | SEC_LOAD;
13497
13498 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
13499 if (reloc_sec != NULL)
13500 {
13501 /* _bfd_elf_get_sec_type_attr chooses a section type by
13502 name. Override as it may be wrong, eg. for a user
13503 section named "auto" we'll get ".relauto" which is
13504 seen to be a .rela section. */
13505 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
13506 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
13507 reloc_sec = NULL;
13508 }
13509 }
13510
13511 elf_section_data (sec)->sreloc = reloc_sec;
13512 }
13513
13514 return reloc_sec;
13515 }
13516
13517 /* Copy the ELF symbol type and other attributes for a linker script
13518 assignment from HSRC to HDEST. Generally this should be treated as
13519 if we found a strong non-dynamic definition for HDEST (except that
13520 ld ignores multiple definition errors). */
13521 void
13522 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
13523 struct bfd_link_hash_entry *hdest,
13524 struct bfd_link_hash_entry *hsrc)
13525 {
13526 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
13527 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
13528 Elf_Internal_Sym isym;
13529
13530 ehdest->type = ehsrc->type;
13531 ehdest->target_internal = ehsrc->target_internal;
13532
13533 isym.st_other = ehsrc->other;
13534 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
13535 }
13536
13537 /* Append a RELA relocation REL to section S in BFD. */
13538
13539 void
13540 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13541 {
13542 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13543 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
13544 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
13545 bed->s->swap_reloca_out (abfd, rel, loc);
13546 }
13547
13548 /* Append a REL relocation REL to section S in BFD. */
13549
13550 void
13551 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13552 {
13553 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13554 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
13555 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
13556 bed->s->swap_reloc_out (abfd, rel, loc);
13557 }