Treat common symbol in executable as definition
[deliverable/binutils-gdb.git] / bfd / elflink.c
1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2016 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
20
21 #include "sysdep.h"
22 #include "bfd.h"
23 #include "bfd_stdint.h"
24 #include "bfdlink.h"
25 #include "libbfd.h"
26 #define ARCH_SIZE 0
27 #include "elf-bfd.h"
28 #include "safe-ctype.h"
29 #include "libiberty.h"
30 #include "objalloc.h"
31
32 /* This struct is used to pass information to routines called via
33 elf_link_hash_traverse which must return failure. */
34
35 struct elf_info_failed
36 {
37 struct bfd_link_info *info;
38 bfd_boolean failed;
39 };
40
41 /* This structure is used to pass information to
42 _bfd_elf_link_find_version_dependencies. */
43
44 struct elf_find_verdep_info
45 {
46 /* General link information. */
47 struct bfd_link_info *info;
48 /* The number of dependencies. */
49 unsigned int vers;
50 /* Whether we had a failure. */
51 bfd_boolean failed;
52 };
53
54 static bfd_boolean _bfd_elf_fix_symbol_flags
55 (struct elf_link_hash_entry *, struct elf_info_failed *);
56
57 asection *
58 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
59 unsigned long r_symndx,
60 bfd_boolean discard)
61 {
62 if (r_symndx >= cookie->locsymcount
63 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
64 {
65 struct elf_link_hash_entry *h;
66
67 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
68
69 while (h->root.type == bfd_link_hash_indirect
70 || h->root.type == bfd_link_hash_warning)
71 h = (struct elf_link_hash_entry *) h->root.u.i.link;
72
73 if ((h->root.type == bfd_link_hash_defined
74 || h->root.type == bfd_link_hash_defweak)
75 && discarded_section (h->root.u.def.section))
76 return h->root.u.def.section;
77 else
78 return NULL;
79 }
80 else
81 {
82 /* It's not a relocation against a global symbol,
83 but it could be a relocation against a local
84 symbol for a discarded section. */
85 asection *isec;
86 Elf_Internal_Sym *isym;
87
88 /* Need to: get the symbol; get the section. */
89 isym = &cookie->locsyms[r_symndx];
90 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
91 if (isec != NULL
92 && discard ? discarded_section (isec) : 1)
93 return isec;
94 }
95 return NULL;
96 }
97
98 /* Define a symbol in a dynamic linkage section. */
99
100 struct elf_link_hash_entry *
101 _bfd_elf_define_linkage_sym (bfd *abfd,
102 struct bfd_link_info *info,
103 asection *sec,
104 const char *name)
105 {
106 struct elf_link_hash_entry *h;
107 struct bfd_link_hash_entry *bh;
108 const struct elf_backend_data *bed;
109
110 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
111 if (h != NULL)
112 {
113 /* Zap symbol defined in an as-needed lib that wasn't linked.
114 This is a symptom of a larger problem: Absolute symbols
115 defined in shared libraries can't be overridden, because we
116 lose the link to the bfd which is via the symbol section. */
117 h->root.type = bfd_link_hash_new;
118 }
119
120 bh = &h->root;
121 bed = get_elf_backend_data (abfd);
122 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
123 sec, 0, NULL, FALSE, bed->collect,
124 &bh))
125 return NULL;
126 h = (struct elf_link_hash_entry *) bh;
127 h->def_regular = 1;
128 h->non_elf = 0;
129 h->root.linker_def = 1;
130 h->type = STT_OBJECT;
131 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
132 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
133
134 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
135 return h;
136 }
137
138 bfd_boolean
139 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
140 {
141 flagword flags;
142 asection *s;
143 struct elf_link_hash_entry *h;
144 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
145 struct elf_link_hash_table *htab = elf_hash_table (info);
146
147 /* This function may be called more than once. */
148 s = bfd_get_linker_section (abfd, ".got");
149 if (s != NULL)
150 return TRUE;
151
152 flags = bed->dynamic_sec_flags;
153
154 s = bfd_make_section_anyway_with_flags (abfd,
155 (bed->rela_plts_and_copies_p
156 ? ".rela.got" : ".rel.got"),
157 (bed->dynamic_sec_flags
158 | SEC_READONLY));
159 if (s == NULL
160 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
161 return FALSE;
162 htab->srelgot = s;
163
164 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
165 if (s == NULL
166 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
167 return FALSE;
168 htab->sgot = s;
169
170 if (bed->want_got_plt)
171 {
172 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
173 if (s == NULL
174 || !bfd_set_section_alignment (abfd, s,
175 bed->s->log_file_align))
176 return FALSE;
177 htab->sgotplt = s;
178 }
179
180 /* The first bit of the global offset table is the header. */
181 s->size += bed->got_header_size;
182
183 if (bed->want_got_sym)
184 {
185 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
186 (or .got.plt) section. We don't do this in the linker script
187 because we don't want to define the symbol if we are not creating
188 a global offset table. */
189 h = _bfd_elf_define_linkage_sym (abfd, info, s,
190 "_GLOBAL_OFFSET_TABLE_");
191 elf_hash_table (info)->hgot = h;
192 if (h == NULL)
193 return FALSE;
194 }
195
196 return TRUE;
197 }
198 \f
199 /* Create a strtab to hold the dynamic symbol names. */
200 static bfd_boolean
201 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
202 {
203 struct elf_link_hash_table *hash_table;
204
205 hash_table = elf_hash_table (info);
206 if (hash_table->dynobj == NULL)
207 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) && !info->nointerp)
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 elf_hash_table (info)->dynsym = s;
283
284 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
285 flags | SEC_READONLY);
286 if (s == NULL)
287 return FALSE;
288
289 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
290 if (s == NULL
291 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
292 return FALSE;
293
294 /* The special symbol _DYNAMIC is always set to the start of the
295 .dynamic section. We could set _DYNAMIC in a linker script, but we
296 only want to define it if we are, in fact, creating a .dynamic
297 section. We don't want to define it if there is no .dynamic
298 section, since on some ELF platforms the start up code examines it
299 to decide how to initialize the process. */
300 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
301 elf_hash_table (info)->hdynamic = h;
302 if (h == NULL)
303 return FALSE;
304
305 if (info->emit_hash)
306 {
307 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
308 flags | SEC_READONLY);
309 if (s == NULL
310 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
311 return FALSE;
312 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
313 }
314
315 if (info->emit_gnu_hash)
316 {
317 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
318 flags | SEC_READONLY);
319 if (s == NULL
320 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
321 return FALSE;
322 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
323 4 32-bit words followed by variable count of 64-bit words, then
324 variable count of 32-bit words. */
325 if (bed->s->arch_size == 64)
326 elf_section_data (s)->this_hdr.sh_entsize = 0;
327 else
328 elf_section_data (s)->this_hdr.sh_entsize = 4;
329 }
330
331 /* Let the backend create the rest of the sections. This lets the
332 backend set the right flags. The backend will normally create
333 the .got and .plt sections. */
334 if (bed->elf_backend_create_dynamic_sections == NULL
335 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
336 return FALSE;
337
338 elf_hash_table (info)->dynamic_sections_created = TRUE;
339
340 return TRUE;
341 }
342
343 /* Create dynamic sections when linking against a dynamic object. */
344
345 bfd_boolean
346 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
347 {
348 flagword flags, pltflags;
349 struct elf_link_hash_entry *h;
350 asection *s;
351 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
352 struct elf_link_hash_table *htab = elf_hash_table (info);
353
354 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
355 .rel[a].bss sections. */
356 flags = bed->dynamic_sec_flags;
357
358 pltflags = flags;
359 if (bed->plt_not_loaded)
360 /* We do not clear SEC_ALLOC here because we still want the OS to
361 allocate space for the section; it's just that there's nothing
362 to read in from the object file. */
363 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
364 else
365 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
366 if (bed->plt_readonly)
367 pltflags |= SEC_READONLY;
368
369 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
370 if (s == NULL
371 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
372 return FALSE;
373 htab->splt = s;
374
375 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
376 .plt section. */
377 if (bed->want_plt_sym)
378 {
379 h = _bfd_elf_define_linkage_sym (abfd, info, s,
380 "_PROCEDURE_LINKAGE_TABLE_");
381 elf_hash_table (info)->hplt = h;
382 if (h == NULL)
383 return FALSE;
384 }
385
386 s = bfd_make_section_anyway_with_flags (abfd,
387 (bed->rela_plts_and_copies_p
388 ? ".rela.plt" : ".rel.plt"),
389 flags | SEC_READONLY);
390 if (s == NULL
391 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
392 return FALSE;
393 htab->srelplt = s;
394
395 if (! _bfd_elf_create_got_section (abfd, info))
396 return FALSE;
397
398 if (bed->want_dynbss)
399 {
400 /* The .dynbss section is a place to put symbols which are defined
401 by dynamic objects, are referenced by regular objects, and are
402 not functions. We must allocate space for them in the process
403 image and use a R_*_COPY reloc to tell the dynamic linker to
404 initialize them at run time. The linker script puts the .dynbss
405 section into the .bss section of the final image. */
406 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
407 (SEC_ALLOC | SEC_LINKER_CREATED));
408 if (s == NULL)
409 return FALSE;
410
411 /* The .rel[a].bss section holds copy relocs. This section is not
412 normally needed. We need to create it here, though, so that the
413 linker will map it to an output section. We can't just create it
414 only if we need it, because we will not know whether we need it
415 until we have seen all the input files, and the first time the
416 main linker code calls BFD after examining all the input files
417 (size_dynamic_sections) the input sections have already been
418 mapped to the output sections. If the section turns out not to
419 be needed, we can discard it later. We will never need this
420 section when generating a shared object, since they do not use
421 copy relocs. */
422 if (! bfd_link_pic (info))
423 {
424 s = bfd_make_section_anyway_with_flags (abfd,
425 (bed->rela_plts_and_copies_p
426 ? ".rela.bss" : ".rel.bss"),
427 flags | SEC_READONLY);
428 if (s == NULL
429 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
430 return FALSE;
431 }
432 }
433
434 return TRUE;
435 }
436 \f
437 /* Record a new dynamic symbol. We record the dynamic symbols as we
438 read the input files, since we need to have a list of all of them
439 before we can determine the final sizes of the output sections.
440 Note that we may actually call this function even though we are not
441 going to output any dynamic symbols; in some cases we know that a
442 symbol should be in the dynamic symbol table, but only if there is
443 one. */
444
445 bfd_boolean
446 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
447 struct elf_link_hash_entry *h)
448 {
449 if (h->dynindx == -1)
450 {
451 struct elf_strtab_hash *dynstr;
452 char *p;
453 const char *name;
454 bfd_size_type indx;
455
456 /* XXX: The ABI draft says the linker must turn hidden and
457 internal symbols into STB_LOCAL symbols when producing the
458 DSO. However, if ld.so honors st_other in the dynamic table,
459 this would not be necessary. */
460 switch (ELF_ST_VISIBILITY (h->other))
461 {
462 case STV_INTERNAL:
463 case STV_HIDDEN:
464 if (h->root.type != bfd_link_hash_undefined
465 && h->root.type != bfd_link_hash_undefweak)
466 {
467 h->forced_local = 1;
468 if (!elf_hash_table (info)->is_relocatable_executable)
469 return TRUE;
470 }
471
472 default:
473 break;
474 }
475
476 h->dynindx = elf_hash_table (info)->dynsymcount;
477 ++elf_hash_table (info)->dynsymcount;
478
479 dynstr = elf_hash_table (info)->dynstr;
480 if (dynstr == NULL)
481 {
482 /* Create a strtab to hold the dynamic symbol names. */
483 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
484 if (dynstr == NULL)
485 return FALSE;
486 }
487
488 /* We don't put any version information in the dynamic string
489 table. */
490 name = h->root.root.string;
491 p = strchr (name, ELF_VER_CHR);
492 if (p != NULL)
493 /* We know that the p points into writable memory. In fact,
494 there are only a few symbols that have read-only names, being
495 those like _GLOBAL_OFFSET_TABLE_ that are created specially
496 by the backends. Most symbols will have names pointing into
497 an ELF string table read from a file, or to objalloc memory. */
498 *p = 0;
499
500 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
501
502 if (p != NULL)
503 *p = ELF_VER_CHR;
504
505 if (indx == (bfd_size_type) -1)
506 return FALSE;
507 h->dynstr_index = indx;
508 }
509
510 return TRUE;
511 }
512 \f
513 /* Mark a symbol dynamic. */
514
515 static void
516 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
517 struct elf_link_hash_entry *h,
518 Elf_Internal_Sym *sym)
519 {
520 struct bfd_elf_dynamic_list *d = info->dynamic_list;
521
522 /* It may be called more than once on the same H. */
523 if(h->dynamic || bfd_link_relocatable (info))
524 return;
525
526 if ((info->dynamic_data
527 && (h->type == STT_OBJECT
528 || h->type == STT_COMMON
529 || (sym != NULL
530 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
531 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
532 || (d != NULL
533 && h->root.type == bfd_link_hash_new
534 && (*d->match) (&d->head, NULL, h->root.root.string)))
535 h->dynamic = 1;
536 }
537
538 /* Record an assignment to a symbol made by a linker script. We need
539 this in case some dynamic object refers to this symbol. */
540
541 bfd_boolean
542 bfd_elf_record_link_assignment (bfd *output_bfd,
543 struct bfd_link_info *info,
544 const char *name,
545 bfd_boolean provide,
546 bfd_boolean hidden)
547 {
548 struct elf_link_hash_entry *h, *hv;
549 struct elf_link_hash_table *htab;
550 const struct elf_backend_data *bed;
551
552 if (!is_elf_hash_table (info->hash))
553 return TRUE;
554
555 htab = elf_hash_table (info);
556 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
557 if (h == NULL)
558 return provide;
559
560 if (h->versioned == unknown)
561 {
562 /* Set versioned if symbol version is unknown. */
563 char *version = strrchr (name, ELF_VER_CHR);
564 if (version)
565 {
566 if (version > name && version[-1] != ELF_VER_CHR)
567 h->versioned = versioned_hidden;
568 else
569 h->versioned = versioned;
570 }
571 }
572
573 switch (h->root.type)
574 {
575 case bfd_link_hash_defined:
576 case bfd_link_hash_defweak:
577 case bfd_link_hash_common:
578 break;
579 case bfd_link_hash_undefweak:
580 case bfd_link_hash_undefined:
581 /* Since we're defining the symbol, don't let it seem to have not
582 been defined. record_dynamic_symbol and size_dynamic_sections
583 may depend on this. */
584 h->root.type = bfd_link_hash_new;
585 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
586 bfd_link_repair_undef_list (&htab->root);
587 break;
588 case bfd_link_hash_new:
589 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
590 h->non_elf = 0;
591 break;
592 case bfd_link_hash_indirect:
593 /* We had a versioned symbol in a dynamic library. We make the
594 the versioned symbol point to this one. */
595 bed = get_elf_backend_data (output_bfd);
596 hv = h;
597 while (hv->root.type == bfd_link_hash_indirect
598 || hv->root.type == bfd_link_hash_warning)
599 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
600 /* We don't need to update h->root.u since linker will set them
601 later. */
602 h->root.type = bfd_link_hash_undefined;
603 hv->root.type = bfd_link_hash_indirect;
604 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
605 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
606 break;
607 case bfd_link_hash_warning:
608 abort ();
609 break;
610 }
611
612 /* If this symbol is being provided by the linker script, and it is
613 currently defined by a dynamic object, but not by a regular
614 object, then mark it as undefined so that the generic linker will
615 force the correct value. */
616 if (provide
617 && h->def_dynamic
618 && !h->def_regular)
619 h->root.type = bfd_link_hash_undefined;
620
621 /* If this symbol is not being provided by the linker script, and it is
622 currently defined by a dynamic object, but not by a regular object,
623 then clear out any version information because the symbol will not be
624 associated with the dynamic object any more. */
625 if (!provide
626 && h->def_dynamic
627 && !h->def_regular)
628 h->verinfo.verdef = NULL;
629
630 h->def_regular = 1;
631
632 if (hidden)
633 {
634 bed = get_elf_backend_data (output_bfd);
635 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
636 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
637 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
638 }
639
640 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
641 and executables. */
642 if (!bfd_link_relocatable (info)
643 && h->dynindx != -1
644 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
645 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
646 h->forced_local = 1;
647
648 if ((h->def_dynamic
649 || h->ref_dynamic
650 || bfd_link_dll (info)
651 || elf_hash_table (info)->is_relocatable_executable)
652 && h->dynindx == -1)
653 {
654 if (! bfd_elf_link_record_dynamic_symbol (info, h))
655 return FALSE;
656
657 /* If this is a weak defined symbol, and we know a corresponding
658 real symbol from the same dynamic object, make sure the real
659 symbol is also made into a dynamic symbol. */
660 if (h->u.weakdef != NULL
661 && h->u.weakdef->dynindx == -1)
662 {
663 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
664 return FALSE;
665 }
666 }
667
668 return TRUE;
669 }
670
671 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
672 success, and 2 on a failure caused by attempting to record a symbol
673 in a discarded section, eg. a discarded link-once section symbol. */
674
675 int
676 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
677 bfd *input_bfd,
678 long input_indx)
679 {
680 bfd_size_type amt;
681 struct elf_link_local_dynamic_entry *entry;
682 struct elf_link_hash_table *eht;
683 struct elf_strtab_hash *dynstr;
684 unsigned long dynstr_index;
685 char *name;
686 Elf_External_Sym_Shndx eshndx;
687 char esym[sizeof (Elf64_External_Sym)];
688
689 if (! is_elf_hash_table (info->hash))
690 return 0;
691
692 /* See if the entry exists already. */
693 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
694 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
695 return 1;
696
697 amt = sizeof (*entry);
698 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
699 if (entry == NULL)
700 return 0;
701
702 /* Go find the symbol, so that we can find it's name. */
703 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
704 1, input_indx, &entry->isym, esym, &eshndx))
705 {
706 bfd_release (input_bfd, entry);
707 return 0;
708 }
709
710 if (entry->isym.st_shndx != SHN_UNDEF
711 && entry->isym.st_shndx < SHN_LORESERVE)
712 {
713 asection *s;
714
715 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
716 if (s == NULL || bfd_is_abs_section (s->output_section))
717 {
718 /* We can still bfd_release here as nothing has done another
719 bfd_alloc. We can't do this later in this function. */
720 bfd_release (input_bfd, entry);
721 return 2;
722 }
723 }
724
725 name = (bfd_elf_string_from_elf_section
726 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
727 entry->isym.st_name));
728
729 dynstr = elf_hash_table (info)->dynstr;
730 if (dynstr == NULL)
731 {
732 /* Create a strtab to hold the dynamic symbol names. */
733 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
734 if (dynstr == NULL)
735 return 0;
736 }
737
738 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
739 if (dynstr_index == (unsigned long) -1)
740 return 0;
741 entry->isym.st_name = dynstr_index;
742
743 eht = elf_hash_table (info);
744
745 entry->next = eht->dynlocal;
746 eht->dynlocal = entry;
747 entry->input_bfd = input_bfd;
748 entry->input_indx = input_indx;
749 eht->dynsymcount++;
750
751 /* Whatever binding the symbol had before, it's now local. */
752 entry->isym.st_info
753 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
754
755 /* The dynindx will be set at the end of size_dynamic_sections. */
756
757 return 1;
758 }
759
760 /* Return the dynindex of a local dynamic symbol. */
761
762 long
763 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
764 bfd *input_bfd,
765 long input_indx)
766 {
767 struct elf_link_local_dynamic_entry *e;
768
769 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
770 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
771 return e->dynindx;
772 return -1;
773 }
774
775 /* This function is used to renumber the dynamic symbols, if some of
776 them are removed because they are marked as local. This is called
777 via elf_link_hash_traverse. */
778
779 static bfd_boolean
780 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
781 void *data)
782 {
783 size_t *count = (size_t *) data;
784
785 if (h->forced_local)
786 return TRUE;
787
788 if (h->dynindx != -1)
789 h->dynindx = ++(*count);
790
791 return TRUE;
792 }
793
794
795 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
796 STB_LOCAL binding. */
797
798 static bfd_boolean
799 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
800 void *data)
801 {
802 size_t *count = (size_t *) data;
803
804 if (!h->forced_local)
805 return TRUE;
806
807 if (h->dynindx != -1)
808 h->dynindx = ++(*count);
809
810 return TRUE;
811 }
812
813 /* Return true if the dynamic symbol for a given section should be
814 omitted when creating a shared library. */
815 bfd_boolean
816 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
817 struct bfd_link_info *info,
818 asection *p)
819 {
820 struct elf_link_hash_table *htab;
821 asection *ip;
822
823 switch (elf_section_data (p)->this_hdr.sh_type)
824 {
825 case SHT_PROGBITS:
826 case SHT_NOBITS:
827 /* If sh_type is yet undecided, assume it could be
828 SHT_PROGBITS/SHT_NOBITS. */
829 case SHT_NULL:
830 htab = elf_hash_table (info);
831 if (p == htab->tls_sec)
832 return FALSE;
833
834 if (htab->text_index_section != NULL)
835 return p != htab->text_index_section && p != htab->data_index_section;
836
837 return (htab->dynobj != NULL
838 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
839 && ip->output_section == p);
840
841 /* There shouldn't be section relative relocations
842 against any other section. */
843 default:
844 return TRUE;
845 }
846 }
847
848 /* Assign dynsym indices. In a shared library we generate a section
849 symbol for each output section, which come first. Next come symbols
850 which have been forced to local binding. Then all of the back-end
851 allocated local dynamic syms, followed by the rest of the global
852 symbols. */
853
854 static unsigned long
855 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
856 struct bfd_link_info *info,
857 unsigned long *section_sym_count)
858 {
859 unsigned long dynsymcount = 0;
860
861 if (bfd_link_pic (info)
862 || elf_hash_table (info)->is_relocatable_executable)
863 {
864 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
865 asection *p;
866 for (p = output_bfd->sections; p ; p = p->next)
867 if ((p->flags & SEC_EXCLUDE) == 0
868 && (p->flags & SEC_ALLOC) != 0
869 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
870 elf_section_data (p)->dynindx = ++dynsymcount;
871 else
872 elf_section_data (p)->dynindx = 0;
873 }
874 *section_sym_count = dynsymcount;
875
876 elf_link_hash_traverse (elf_hash_table (info),
877 elf_link_renumber_local_hash_table_dynsyms,
878 &dynsymcount);
879
880 if (elf_hash_table (info)->dynlocal)
881 {
882 struct elf_link_local_dynamic_entry *p;
883 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
884 p->dynindx = ++dynsymcount;
885 }
886
887 elf_link_hash_traverse (elf_hash_table (info),
888 elf_link_renumber_hash_table_dynsyms,
889 &dynsymcount);
890
891 /* There is an unused NULL entry at the head of the table which
892 we must account for in our count. We always create the dynsym
893 section, even if it is empty, with dynamic sections. */
894 if (elf_hash_table (info)->dynamic_sections_created)
895 ++dynsymcount;
896
897 elf_hash_table (info)->dynsymcount = dynsymcount;
898 return dynsymcount;
899 }
900
901 /* Merge st_other field. */
902
903 static void
904 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
905 const Elf_Internal_Sym *isym, asection *sec,
906 bfd_boolean definition, bfd_boolean dynamic)
907 {
908 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
909
910 /* If st_other has a processor-specific meaning, specific
911 code might be needed here. */
912 if (bed->elf_backend_merge_symbol_attribute)
913 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
914 dynamic);
915
916 if (!dynamic)
917 {
918 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
919 unsigned hvis = ELF_ST_VISIBILITY (h->other);
920
921 /* Keep the most constraining visibility. Leave the remainder
922 of the st_other field to elf_backend_merge_symbol_attribute. */
923 if (symvis - 1 < hvis - 1)
924 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
925 }
926 else if (definition
927 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
928 && (sec->flags & SEC_READONLY) == 0)
929 h->protected_def = 1;
930 }
931
932 /* This function is called when we want to merge a new symbol with an
933 existing symbol. It handles the various cases which arise when we
934 find a definition in a dynamic object, or when there is already a
935 definition in a dynamic object. The new symbol is described by
936 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
937 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
938 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
939 of an old common symbol. We set OVERRIDE if the old symbol is
940 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
941 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
942 to change. By OK to change, we mean that we shouldn't warn if the
943 type or size does change. */
944
945 static bfd_boolean
946 _bfd_elf_merge_symbol (bfd *abfd,
947 struct bfd_link_info *info,
948 const char *name,
949 Elf_Internal_Sym *sym,
950 asection **psec,
951 bfd_vma *pvalue,
952 struct elf_link_hash_entry **sym_hash,
953 bfd **poldbfd,
954 bfd_boolean *pold_weak,
955 unsigned int *pold_alignment,
956 bfd_boolean *skip,
957 bfd_boolean *override,
958 bfd_boolean *type_change_ok,
959 bfd_boolean *size_change_ok,
960 bfd_boolean *matched)
961 {
962 asection *sec, *oldsec;
963 struct elf_link_hash_entry *h;
964 struct elf_link_hash_entry *hi;
965 struct elf_link_hash_entry *flip;
966 int bind;
967 bfd *oldbfd;
968 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
969 bfd_boolean newweak, oldweak, newfunc, oldfunc;
970 const struct elf_backend_data *bed;
971 char *new_version;
972
973 *skip = FALSE;
974 *override = FALSE;
975
976 sec = *psec;
977 bind = ELF_ST_BIND (sym->st_info);
978
979 if (! bfd_is_und_section (sec))
980 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
981 else
982 h = ((struct elf_link_hash_entry *)
983 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
984 if (h == NULL)
985 return FALSE;
986 *sym_hash = h;
987
988 bed = get_elf_backend_data (abfd);
989
990 /* NEW_VERSION is the symbol version of the new symbol. */
991 if (h->versioned != unversioned)
992 {
993 /* Symbol version is unknown or versioned. */
994 new_version = strrchr (name, ELF_VER_CHR);
995 if (new_version)
996 {
997 if (h->versioned == unknown)
998 {
999 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1000 h->versioned = versioned_hidden;
1001 else
1002 h->versioned = versioned;
1003 }
1004 new_version += 1;
1005 if (new_version[0] == '\0')
1006 new_version = NULL;
1007 }
1008 else
1009 h->versioned = unversioned;
1010 }
1011 else
1012 new_version = NULL;
1013
1014 /* For merging, we only care about real symbols. But we need to make
1015 sure that indirect symbol dynamic flags are updated. */
1016 hi = h;
1017 while (h->root.type == bfd_link_hash_indirect
1018 || h->root.type == bfd_link_hash_warning)
1019 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1020
1021 if (!*matched)
1022 {
1023 if (hi == h || h->root.type == bfd_link_hash_new)
1024 *matched = TRUE;
1025 else
1026 {
1027 /* OLD_HIDDEN is true if the existing symbol is only visible
1028 to the symbol with the same symbol version. NEW_HIDDEN is
1029 true if the new symbol is only visible to the symbol with
1030 the same symbol version. */
1031 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1032 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1033 if (!old_hidden && !new_hidden)
1034 /* The new symbol matches the existing symbol if both
1035 aren't hidden. */
1036 *matched = TRUE;
1037 else
1038 {
1039 /* OLD_VERSION is the symbol version of the existing
1040 symbol. */
1041 char *old_version;
1042
1043 if (h->versioned >= versioned)
1044 old_version = strrchr (h->root.root.string,
1045 ELF_VER_CHR) + 1;
1046 else
1047 old_version = NULL;
1048
1049 /* The new symbol matches the existing symbol if they
1050 have the same symbol version. */
1051 *matched = (old_version == new_version
1052 || (old_version != NULL
1053 && new_version != NULL
1054 && strcmp (old_version, new_version) == 0));
1055 }
1056 }
1057 }
1058
1059 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1060 existing symbol. */
1061
1062 oldbfd = NULL;
1063 oldsec = NULL;
1064 switch (h->root.type)
1065 {
1066 default:
1067 break;
1068
1069 case bfd_link_hash_undefined:
1070 case bfd_link_hash_undefweak:
1071 oldbfd = h->root.u.undef.abfd;
1072 break;
1073
1074 case bfd_link_hash_defined:
1075 case bfd_link_hash_defweak:
1076 oldbfd = h->root.u.def.section->owner;
1077 oldsec = h->root.u.def.section;
1078 break;
1079
1080 case bfd_link_hash_common:
1081 oldbfd = h->root.u.c.p->section->owner;
1082 oldsec = h->root.u.c.p->section;
1083 if (pold_alignment)
1084 *pold_alignment = h->root.u.c.p->alignment_power;
1085 break;
1086 }
1087 if (poldbfd && *poldbfd == NULL)
1088 *poldbfd = oldbfd;
1089
1090 /* Differentiate strong and weak symbols. */
1091 newweak = bind == STB_WEAK;
1092 oldweak = (h->root.type == bfd_link_hash_defweak
1093 || h->root.type == bfd_link_hash_undefweak);
1094 if (pold_weak)
1095 *pold_weak = oldweak;
1096
1097 /* This code is for coping with dynamic objects, and is only useful
1098 if we are doing an ELF link. */
1099 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1100 return TRUE;
1101
1102 /* We have to check it for every instance since the first few may be
1103 references and not all compilers emit symbol type for undefined
1104 symbols. */
1105 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1106
1107 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1108 respectively, is from a dynamic object. */
1109
1110 newdyn = (abfd->flags & DYNAMIC) != 0;
1111
1112 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1113 syms and defined syms in dynamic libraries respectively.
1114 ref_dynamic on the other hand can be set for a symbol defined in
1115 a dynamic library, and def_dynamic may not be set; When the
1116 definition in a dynamic lib is overridden by a definition in the
1117 executable use of the symbol in the dynamic lib becomes a
1118 reference to the executable symbol. */
1119 if (newdyn)
1120 {
1121 if (bfd_is_und_section (sec))
1122 {
1123 if (bind != STB_WEAK)
1124 {
1125 h->ref_dynamic_nonweak = 1;
1126 hi->ref_dynamic_nonweak = 1;
1127 }
1128 }
1129 else
1130 {
1131 /* Update the existing symbol only if they match. */
1132 if (*matched)
1133 h->dynamic_def = 1;
1134 hi->dynamic_def = 1;
1135 }
1136 }
1137
1138 /* If we just created the symbol, mark it as being an ELF symbol.
1139 Other than that, there is nothing to do--there is no merge issue
1140 with a newly defined symbol--so we just return. */
1141
1142 if (h->root.type == bfd_link_hash_new)
1143 {
1144 h->non_elf = 0;
1145 return TRUE;
1146 }
1147
1148 /* In cases involving weak versioned symbols, we may wind up trying
1149 to merge a symbol with itself. Catch that here, to avoid the
1150 confusion that results if we try to override a symbol with
1151 itself. The additional tests catch cases like
1152 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1153 dynamic object, which we do want to handle here. */
1154 if (abfd == oldbfd
1155 && (newweak || oldweak)
1156 && ((abfd->flags & DYNAMIC) == 0
1157 || !h->def_regular))
1158 return TRUE;
1159
1160 olddyn = FALSE;
1161 if (oldbfd != NULL)
1162 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1163 else if (oldsec != NULL)
1164 {
1165 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1166 indices used by MIPS ELF. */
1167 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1168 }
1169
1170 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1171 respectively, appear to be a definition rather than reference. */
1172
1173 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1174
1175 /* The old common symbol in executable is a definition if the new
1176 definition comes from a shared library. */
1177 olddef = (h->root.type != bfd_link_hash_undefined
1178 && h->root.type != bfd_link_hash_undefweak
1179 && (h->root.type != bfd_link_hash_common
1180 || (!olddyn && newdyn && bfd_link_executable (info))));
1181
1182 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1183 respectively, appear to be a function. */
1184
1185 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1186 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1187
1188 oldfunc = (h->type != STT_NOTYPE
1189 && bed->is_function_type (h->type));
1190
1191 /* When we try to create a default indirect symbol from the dynamic
1192 definition with the default version, we skip it if its type and
1193 the type of existing regular definition mismatch. */
1194 if (pold_alignment == NULL
1195 && newdyn
1196 && newdef
1197 && !olddyn
1198 && (((olddef || h->root.type == bfd_link_hash_common)
1199 && ELF_ST_TYPE (sym->st_info) != h->type
1200 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1201 && h->type != STT_NOTYPE
1202 && !(newfunc && oldfunc))
1203 || (olddef
1204 && ((h->type == STT_GNU_IFUNC)
1205 != (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))))
1206 {
1207 *skip = TRUE;
1208 return TRUE;
1209 }
1210
1211 /* Check TLS symbols. We don't check undefined symbols introduced
1212 by "ld -u" which have no type (and oldbfd NULL), and we don't
1213 check symbols from plugins because they also have no type. */
1214 if (oldbfd != NULL
1215 && (oldbfd->flags & BFD_PLUGIN) == 0
1216 && (abfd->flags & BFD_PLUGIN) == 0
1217 && ELF_ST_TYPE (sym->st_info) != h->type
1218 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1219 {
1220 bfd *ntbfd, *tbfd;
1221 bfd_boolean ntdef, tdef;
1222 asection *ntsec, *tsec;
1223
1224 if (h->type == STT_TLS)
1225 {
1226 ntbfd = abfd;
1227 ntsec = sec;
1228 ntdef = newdef;
1229 tbfd = oldbfd;
1230 tsec = oldsec;
1231 tdef = olddef;
1232 }
1233 else
1234 {
1235 ntbfd = oldbfd;
1236 ntsec = oldsec;
1237 ntdef = olddef;
1238 tbfd = abfd;
1239 tsec = sec;
1240 tdef = newdef;
1241 }
1242
1243 if (tdef && ntdef)
1244 (*_bfd_error_handler)
1245 (_("%s: TLS definition in %B section %A "
1246 "mismatches non-TLS definition in %B section %A"),
1247 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1248 else if (!tdef && !ntdef)
1249 (*_bfd_error_handler)
1250 (_("%s: TLS reference in %B "
1251 "mismatches non-TLS reference in %B"),
1252 tbfd, ntbfd, h->root.root.string);
1253 else if (tdef)
1254 (*_bfd_error_handler)
1255 (_("%s: TLS definition in %B section %A "
1256 "mismatches non-TLS reference in %B"),
1257 tbfd, tsec, ntbfd, h->root.root.string);
1258 else
1259 (*_bfd_error_handler)
1260 (_("%s: TLS reference in %B "
1261 "mismatches non-TLS definition in %B section %A"),
1262 tbfd, ntbfd, ntsec, h->root.root.string);
1263
1264 bfd_set_error (bfd_error_bad_value);
1265 return FALSE;
1266 }
1267
1268 /* If the old symbol has non-default visibility, we ignore the new
1269 definition from a dynamic object. */
1270 if (newdyn
1271 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1272 && !bfd_is_und_section (sec))
1273 {
1274 *skip = TRUE;
1275 /* Make sure this symbol is dynamic. */
1276 h->ref_dynamic = 1;
1277 hi->ref_dynamic = 1;
1278 /* A protected symbol has external availability. Make sure it is
1279 recorded as dynamic.
1280
1281 FIXME: Should we check type and size for protected symbol? */
1282 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1283 return bfd_elf_link_record_dynamic_symbol (info, h);
1284 else
1285 return TRUE;
1286 }
1287 else if (!newdyn
1288 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1289 && h->def_dynamic)
1290 {
1291 /* If the new symbol with non-default visibility comes from a
1292 relocatable file and the old definition comes from a dynamic
1293 object, we remove the old definition. */
1294 if (hi->root.type == bfd_link_hash_indirect)
1295 {
1296 /* Handle the case where the old dynamic definition is
1297 default versioned. We need to copy the symbol info from
1298 the symbol with default version to the normal one if it
1299 was referenced before. */
1300 if (h->ref_regular)
1301 {
1302 hi->root.type = h->root.type;
1303 h->root.type = bfd_link_hash_indirect;
1304 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1305
1306 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1307 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1308 {
1309 /* If the new symbol is hidden or internal, completely undo
1310 any dynamic link state. */
1311 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1312 h->forced_local = 0;
1313 h->ref_dynamic = 0;
1314 }
1315 else
1316 h->ref_dynamic = 1;
1317
1318 h->def_dynamic = 0;
1319 /* FIXME: Should we check type and size for protected symbol? */
1320 h->size = 0;
1321 h->type = 0;
1322
1323 h = hi;
1324 }
1325 else
1326 h = hi;
1327 }
1328
1329 /* If the old symbol was undefined before, then it will still be
1330 on the undefs list. If the new symbol is undefined or
1331 common, we can't make it bfd_link_hash_new here, because new
1332 undefined or common symbols will be added to the undefs list
1333 by _bfd_generic_link_add_one_symbol. Symbols may not be
1334 added twice to the undefs list. Also, if the new symbol is
1335 undefweak then we don't want to lose the strong undef. */
1336 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1337 {
1338 h->root.type = bfd_link_hash_undefined;
1339 h->root.u.undef.abfd = abfd;
1340 }
1341 else
1342 {
1343 h->root.type = bfd_link_hash_new;
1344 h->root.u.undef.abfd = NULL;
1345 }
1346
1347 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1348 {
1349 /* If the new symbol is hidden or internal, completely undo
1350 any dynamic link state. */
1351 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1352 h->forced_local = 0;
1353 h->ref_dynamic = 0;
1354 }
1355 else
1356 h->ref_dynamic = 1;
1357 h->def_dynamic = 0;
1358 /* FIXME: Should we check type and size for protected symbol? */
1359 h->size = 0;
1360 h->type = 0;
1361 return TRUE;
1362 }
1363
1364 /* If a new weak symbol definition comes from a regular file and the
1365 old symbol comes from a dynamic library, we treat the new one as
1366 strong. Similarly, an old weak symbol definition from a regular
1367 file is treated as strong when the new symbol comes from a dynamic
1368 library. Further, an old weak symbol from a dynamic library is
1369 treated as strong if the new symbol is from a dynamic library.
1370 This reflects the way glibc's ld.so works.
1371
1372 Do this before setting *type_change_ok or *size_change_ok so that
1373 we warn properly when dynamic library symbols are overridden. */
1374
1375 if (newdef && !newdyn && olddyn)
1376 newweak = FALSE;
1377 if (olddef && newdyn)
1378 oldweak = FALSE;
1379
1380 /* Allow changes between different types of function symbol. */
1381 if (newfunc && oldfunc)
1382 *type_change_ok = TRUE;
1383
1384 /* It's OK to change the type if either the existing symbol or the
1385 new symbol is weak. A type change is also OK if the old symbol
1386 is undefined and the new symbol is defined. */
1387
1388 if (oldweak
1389 || newweak
1390 || (newdef
1391 && h->root.type == bfd_link_hash_undefined))
1392 *type_change_ok = TRUE;
1393
1394 /* It's OK to change the size if either the existing symbol or the
1395 new symbol is weak, or if the old symbol is undefined. */
1396
1397 if (*type_change_ok
1398 || h->root.type == bfd_link_hash_undefined)
1399 *size_change_ok = TRUE;
1400
1401 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1402 symbol, respectively, appears to be a common symbol in a dynamic
1403 object. If a symbol appears in an uninitialized section, and is
1404 not weak, and is not a function, then it may be a common symbol
1405 which was resolved when the dynamic object was created. We want
1406 to treat such symbols specially, because they raise special
1407 considerations when setting the symbol size: if the symbol
1408 appears as a common symbol in a regular object, and the size in
1409 the regular object is larger, we must make sure that we use the
1410 larger size. This problematic case can always be avoided in C,
1411 but it must be handled correctly when using Fortran shared
1412 libraries.
1413
1414 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1415 likewise for OLDDYNCOMMON and OLDDEF.
1416
1417 Note that this test is just a heuristic, and that it is quite
1418 possible to have an uninitialized symbol in a shared object which
1419 is really a definition, rather than a common symbol. This could
1420 lead to some minor confusion when the symbol really is a common
1421 symbol in some regular object. However, I think it will be
1422 harmless. */
1423
1424 if (newdyn
1425 && newdef
1426 && !newweak
1427 && (sec->flags & SEC_ALLOC) != 0
1428 && (sec->flags & SEC_LOAD) == 0
1429 && sym->st_size > 0
1430 && !newfunc)
1431 newdyncommon = TRUE;
1432 else
1433 newdyncommon = FALSE;
1434
1435 if (olddyn
1436 && olddef
1437 && h->root.type == bfd_link_hash_defined
1438 && h->def_dynamic
1439 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1440 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1441 && h->size > 0
1442 && !oldfunc)
1443 olddyncommon = TRUE;
1444 else
1445 olddyncommon = FALSE;
1446
1447 /* We now know everything about the old and new symbols. We ask the
1448 backend to check if we can merge them. */
1449 if (bed->merge_symbol != NULL)
1450 {
1451 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1452 return FALSE;
1453 sec = *psec;
1454 }
1455
1456 /* If both the old and the new symbols look like common symbols in a
1457 dynamic object, set the size of the symbol to the larger of the
1458 two. */
1459
1460 if (olddyncommon
1461 && newdyncommon
1462 && sym->st_size != h->size)
1463 {
1464 /* Since we think we have two common symbols, issue a multiple
1465 common warning if desired. Note that we only warn if the
1466 size is different. If the size is the same, we simply let
1467 the old symbol override the new one as normally happens with
1468 symbols defined in dynamic objects. */
1469
1470 if (! ((*info->callbacks->multiple_common)
1471 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
1472 return FALSE;
1473
1474 if (sym->st_size > h->size)
1475 h->size = sym->st_size;
1476
1477 *size_change_ok = TRUE;
1478 }
1479
1480 /* If we are looking at a dynamic object, and we have found a
1481 definition, we need to see if the symbol was already defined by
1482 some other object. If so, we want to use the existing
1483 definition, and we do not want to report a multiple symbol
1484 definition error; we do this by clobbering *PSEC to be
1485 bfd_und_section_ptr.
1486
1487 We treat a common symbol as a definition if the symbol in the
1488 shared library is a function, since common symbols always
1489 represent variables; this can cause confusion in principle, but
1490 any such confusion would seem to indicate an erroneous program or
1491 shared library. We also permit a common symbol in a regular
1492 object to override a weak symbol in a shared object. */
1493
1494 if (newdyn
1495 && newdef
1496 && (olddef
1497 || (h->root.type == bfd_link_hash_common
1498 && (newweak || newfunc))))
1499 {
1500 *override = TRUE;
1501 newdef = FALSE;
1502 newdyncommon = FALSE;
1503
1504 *psec = sec = bfd_und_section_ptr;
1505 *size_change_ok = TRUE;
1506
1507 /* If we get here when the old symbol is a common symbol, then
1508 we are explicitly letting it override a weak symbol or
1509 function in a dynamic object, and we don't want to warn about
1510 a type change. If the old symbol is a defined symbol, a type
1511 change warning may still be appropriate. */
1512
1513 if (h->root.type == bfd_link_hash_common)
1514 *type_change_ok = TRUE;
1515 }
1516
1517 /* Handle the special case of an old common symbol merging with a
1518 new symbol which looks like a common symbol in a shared object.
1519 We change *PSEC and *PVALUE to make the new symbol look like a
1520 common symbol, and let _bfd_generic_link_add_one_symbol do the
1521 right thing. */
1522
1523 if (newdyncommon
1524 && h->root.type == bfd_link_hash_common)
1525 {
1526 *override = TRUE;
1527 newdef = FALSE;
1528 newdyncommon = FALSE;
1529 *pvalue = sym->st_size;
1530 *psec = sec = bed->common_section (oldsec);
1531 *size_change_ok = TRUE;
1532 }
1533
1534 /* Skip weak definitions of symbols that are already defined. */
1535 if (newdef && olddef && newweak)
1536 {
1537 /* Don't skip new non-IR weak syms. */
1538 if (!(oldbfd != NULL
1539 && (oldbfd->flags & BFD_PLUGIN) != 0
1540 && (abfd->flags & BFD_PLUGIN) == 0))
1541 {
1542 newdef = FALSE;
1543 *skip = TRUE;
1544 }
1545
1546 /* Merge st_other. If the symbol already has a dynamic index,
1547 but visibility says it should not be visible, turn it into a
1548 local symbol. */
1549 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1550 if (h->dynindx != -1)
1551 switch (ELF_ST_VISIBILITY (h->other))
1552 {
1553 case STV_INTERNAL:
1554 case STV_HIDDEN:
1555 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1556 break;
1557 }
1558 }
1559
1560 /* If the old symbol is from a dynamic object, and the new symbol is
1561 a definition which is not from a dynamic object, then the new
1562 symbol overrides the old symbol. Symbols from regular files
1563 always take precedence over symbols from dynamic objects, even if
1564 they are defined after the dynamic object in the link.
1565
1566 As above, we again permit a common symbol in a regular object to
1567 override a definition in a shared object if the shared object
1568 symbol is a function or is weak. */
1569
1570 flip = NULL;
1571 if (!newdyn
1572 && (newdef
1573 || (bfd_is_com_section (sec)
1574 && (oldweak || oldfunc)))
1575 && olddyn
1576 && olddef
1577 && h->def_dynamic)
1578 {
1579 /* Change the hash table entry to undefined, and let
1580 _bfd_generic_link_add_one_symbol do the right thing with the
1581 new definition. */
1582
1583 h->root.type = bfd_link_hash_undefined;
1584 h->root.u.undef.abfd = h->root.u.def.section->owner;
1585 *size_change_ok = TRUE;
1586
1587 olddef = FALSE;
1588 olddyncommon = FALSE;
1589
1590 /* We again permit a type change when a common symbol may be
1591 overriding a function. */
1592
1593 if (bfd_is_com_section (sec))
1594 {
1595 if (oldfunc)
1596 {
1597 /* If a common symbol overrides a function, make sure
1598 that it isn't defined dynamically nor has type
1599 function. */
1600 h->def_dynamic = 0;
1601 h->type = STT_NOTYPE;
1602 }
1603 *type_change_ok = TRUE;
1604 }
1605
1606 if (hi->root.type == bfd_link_hash_indirect)
1607 flip = hi;
1608 else
1609 /* This union may have been set to be non-NULL when this symbol
1610 was seen in a dynamic object. We must force the union to be
1611 NULL, so that it is correct for a regular symbol. */
1612 h->verinfo.vertree = NULL;
1613 }
1614
1615 /* Handle the special case of a new common symbol merging with an
1616 old symbol that looks like it might be a common symbol defined in
1617 a shared object. Note that we have already handled the case in
1618 which a new common symbol should simply override the definition
1619 in the shared library. */
1620
1621 if (! newdyn
1622 && bfd_is_com_section (sec)
1623 && olddyncommon)
1624 {
1625 /* It would be best if we could set the hash table entry to a
1626 common symbol, but we don't know what to use for the section
1627 or the alignment. */
1628 if (! ((*info->callbacks->multiple_common)
1629 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
1630 return FALSE;
1631
1632 /* If the presumed common symbol in the dynamic object is
1633 larger, pretend that the new symbol has its size. */
1634
1635 if (h->size > *pvalue)
1636 *pvalue = h->size;
1637
1638 /* We need to remember the alignment required by the symbol
1639 in the dynamic object. */
1640 BFD_ASSERT (pold_alignment);
1641 *pold_alignment = h->root.u.def.section->alignment_power;
1642
1643 olddef = FALSE;
1644 olddyncommon = FALSE;
1645
1646 h->root.type = bfd_link_hash_undefined;
1647 h->root.u.undef.abfd = h->root.u.def.section->owner;
1648
1649 *size_change_ok = TRUE;
1650 *type_change_ok = TRUE;
1651
1652 if (hi->root.type == bfd_link_hash_indirect)
1653 flip = hi;
1654 else
1655 h->verinfo.vertree = NULL;
1656 }
1657
1658 if (flip != NULL)
1659 {
1660 /* Handle the case where we had a versioned symbol in a dynamic
1661 library and now find a definition in a normal object. In this
1662 case, we make the versioned symbol point to the normal one. */
1663 flip->root.type = h->root.type;
1664 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1665 h->root.type = bfd_link_hash_indirect;
1666 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1667 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1668 if (h->def_dynamic)
1669 {
1670 h->def_dynamic = 0;
1671 flip->ref_dynamic = 1;
1672 }
1673 }
1674
1675 return TRUE;
1676 }
1677
1678 /* This function is called to create an indirect symbol from the
1679 default for the symbol with the default version if needed. The
1680 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1681 set DYNSYM if the new indirect symbol is dynamic. */
1682
1683 static bfd_boolean
1684 _bfd_elf_add_default_symbol (bfd *abfd,
1685 struct bfd_link_info *info,
1686 struct elf_link_hash_entry *h,
1687 const char *name,
1688 Elf_Internal_Sym *sym,
1689 asection *sec,
1690 bfd_vma value,
1691 bfd **poldbfd,
1692 bfd_boolean *dynsym)
1693 {
1694 bfd_boolean type_change_ok;
1695 bfd_boolean size_change_ok;
1696 bfd_boolean skip;
1697 char *shortname;
1698 struct elf_link_hash_entry *hi;
1699 struct bfd_link_hash_entry *bh;
1700 const struct elf_backend_data *bed;
1701 bfd_boolean collect;
1702 bfd_boolean dynamic;
1703 bfd_boolean override;
1704 char *p;
1705 size_t len, shortlen;
1706 asection *tmp_sec;
1707 bfd_boolean matched;
1708
1709 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1710 return TRUE;
1711
1712 /* If this symbol has a version, and it is the default version, we
1713 create an indirect symbol from the default name to the fully
1714 decorated name. This will cause external references which do not
1715 specify a version to be bound to this version of the symbol. */
1716 p = strchr (name, ELF_VER_CHR);
1717 if (h->versioned == unknown)
1718 {
1719 if (p == NULL)
1720 {
1721 h->versioned = unversioned;
1722 return TRUE;
1723 }
1724 else
1725 {
1726 if (p[1] != ELF_VER_CHR)
1727 {
1728 h->versioned = versioned_hidden;
1729 return TRUE;
1730 }
1731 else
1732 h->versioned = versioned;
1733 }
1734 }
1735 else
1736 {
1737 /* PR ld/19073: We may see an unversioned definition after the
1738 default version. */
1739 if (p == NULL)
1740 return TRUE;
1741 }
1742
1743 bed = get_elf_backend_data (abfd);
1744 collect = bed->collect;
1745 dynamic = (abfd->flags & DYNAMIC) != 0;
1746
1747 shortlen = p - name;
1748 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1749 if (shortname == NULL)
1750 return FALSE;
1751 memcpy (shortname, name, shortlen);
1752 shortname[shortlen] = '\0';
1753
1754 /* We are going to create a new symbol. Merge it with any existing
1755 symbol with this name. For the purposes of the merge, act as
1756 though we were defining the symbol we just defined, although we
1757 actually going to define an indirect symbol. */
1758 type_change_ok = FALSE;
1759 size_change_ok = FALSE;
1760 matched = TRUE;
1761 tmp_sec = sec;
1762 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1763 &hi, poldbfd, NULL, NULL, &skip, &override,
1764 &type_change_ok, &size_change_ok, &matched))
1765 return FALSE;
1766
1767 if (skip)
1768 goto nondefault;
1769
1770 if (! override)
1771 {
1772 /* Add the default symbol if not performing a relocatable link. */
1773 if (! bfd_link_relocatable (info))
1774 {
1775 bh = &hi->root;
1776 if (! (_bfd_generic_link_add_one_symbol
1777 (info, abfd, shortname, BSF_INDIRECT,
1778 bfd_ind_section_ptr,
1779 0, name, FALSE, collect, &bh)))
1780 return FALSE;
1781 hi = (struct elf_link_hash_entry *) bh;
1782 }
1783 }
1784 else
1785 {
1786 /* In this case the symbol named SHORTNAME is overriding the
1787 indirect symbol we want to add. We were planning on making
1788 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1789 is the name without a version. NAME is the fully versioned
1790 name, and it is the default version.
1791
1792 Overriding means that we already saw a definition for the
1793 symbol SHORTNAME in a regular object, and it is overriding
1794 the symbol defined in the dynamic object.
1795
1796 When this happens, we actually want to change NAME, the
1797 symbol we just added, to refer to SHORTNAME. This will cause
1798 references to NAME in the shared object to become references
1799 to SHORTNAME in the regular object. This is what we expect
1800 when we override a function in a shared object: that the
1801 references in the shared object will be mapped to the
1802 definition in the regular object. */
1803
1804 while (hi->root.type == bfd_link_hash_indirect
1805 || hi->root.type == bfd_link_hash_warning)
1806 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1807
1808 h->root.type = bfd_link_hash_indirect;
1809 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1810 if (h->def_dynamic)
1811 {
1812 h->def_dynamic = 0;
1813 hi->ref_dynamic = 1;
1814 if (hi->ref_regular
1815 || hi->def_regular)
1816 {
1817 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1818 return FALSE;
1819 }
1820 }
1821
1822 /* Now set HI to H, so that the following code will set the
1823 other fields correctly. */
1824 hi = h;
1825 }
1826
1827 /* Check if HI is a warning symbol. */
1828 if (hi->root.type == bfd_link_hash_warning)
1829 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1830
1831 /* If there is a duplicate definition somewhere, then HI may not
1832 point to an indirect symbol. We will have reported an error to
1833 the user in that case. */
1834
1835 if (hi->root.type == bfd_link_hash_indirect)
1836 {
1837 struct elf_link_hash_entry *ht;
1838
1839 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1840 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1841
1842 /* A reference to the SHORTNAME symbol from a dynamic library
1843 will be satisfied by the versioned symbol at runtime. In
1844 effect, we have a reference to the versioned symbol. */
1845 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1846 hi->dynamic_def |= ht->dynamic_def;
1847
1848 /* See if the new flags lead us to realize that the symbol must
1849 be dynamic. */
1850 if (! *dynsym)
1851 {
1852 if (! dynamic)
1853 {
1854 if (! bfd_link_executable (info)
1855 || hi->def_dynamic
1856 || hi->ref_dynamic)
1857 *dynsym = TRUE;
1858 }
1859 else
1860 {
1861 if (hi->ref_regular)
1862 *dynsym = TRUE;
1863 }
1864 }
1865 }
1866
1867 /* We also need to define an indirection from the nondefault version
1868 of the symbol. */
1869
1870 nondefault:
1871 len = strlen (name);
1872 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
1873 if (shortname == NULL)
1874 return FALSE;
1875 memcpy (shortname, name, shortlen);
1876 memcpy (shortname + shortlen, p + 1, len - shortlen);
1877
1878 /* Once again, merge with any existing symbol. */
1879 type_change_ok = FALSE;
1880 size_change_ok = FALSE;
1881 tmp_sec = sec;
1882 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1883 &hi, poldbfd, NULL, NULL, &skip, &override,
1884 &type_change_ok, &size_change_ok, &matched))
1885 return FALSE;
1886
1887 if (skip)
1888 return TRUE;
1889
1890 if (override)
1891 {
1892 /* Here SHORTNAME is a versioned name, so we don't expect to see
1893 the type of override we do in the case above unless it is
1894 overridden by a versioned definition. */
1895 if (hi->root.type != bfd_link_hash_defined
1896 && hi->root.type != bfd_link_hash_defweak)
1897 (*_bfd_error_handler)
1898 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1899 abfd, shortname);
1900 }
1901 else
1902 {
1903 bh = &hi->root;
1904 if (! (_bfd_generic_link_add_one_symbol
1905 (info, abfd, shortname, BSF_INDIRECT,
1906 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1907 return FALSE;
1908 hi = (struct elf_link_hash_entry *) bh;
1909
1910 /* If there is a duplicate definition somewhere, then HI may not
1911 point to an indirect symbol. We will have reported an error
1912 to the user in that case. */
1913
1914 if (hi->root.type == bfd_link_hash_indirect)
1915 {
1916 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1917 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1918 hi->dynamic_def |= h->dynamic_def;
1919
1920 /* See if the new flags lead us to realize that the symbol
1921 must be dynamic. */
1922 if (! *dynsym)
1923 {
1924 if (! dynamic)
1925 {
1926 if (! bfd_link_executable (info)
1927 || hi->ref_dynamic)
1928 *dynsym = TRUE;
1929 }
1930 else
1931 {
1932 if (hi->ref_regular)
1933 *dynsym = TRUE;
1934 }
1935 }
1936 }
1937 }
1938
1939 return TRUE;
1940 }
1941 \f
1942 /* This routine is used to export all defined symbols into the dynamic
1943 symbol table. It is called via elf_link_hash_traverse. */
1944
1945 static bfd_boolean
1946 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1947 {
1948 struct elf_info_failed *eif = (struct elf_info_failed *) data;
1949
1950 /* Ignore indirect symbols. These are added by the versioning code. */
1951 if (h->root.type == bfd_link_hash_indirect)
1952 return TRUE;
1953
1954 /* Ignore this if we won't export it. */
1955 if (!eif->info->export_dynamic && !h->dynamic)
1956 return TRUE;
1957
1958 if (h->dynindx == -1
1959 && (h->def_regular || h->ref_regular)
1960 && ! bfd_hide_sym_by_version (eif->info->version_info,
1961 h->root.root.string))
1962 {
1963 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1964 {
1965 eif->failed = TRUE;
1966 return FALSE;
1967 }
1968 }
1969
1970 return TRUE;
1971 }
1972 \f
1973 /* Look through the symbols which are defined in other shared
1974 libraries and referenced here. Update the list of version
1975 dependencies. This will be put into the .gnu.version_r section.
1976 This function is called via elf_link_hash_traverse. */
1977
1978 static bfd_boolean
1979 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1980 void *data)
1981 {
1982 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
1983 Elf_Internal_Verneed *t;
1984 Elf_Internal_Vernaux *a;
1985 bfd_size_type amt;
1986
1987 /* We only care about symbols defined in shared objects with version
1988 information. */
1989 if (!h->def_dynamic
1990 || h->def_regular
1991 || h->dynindx == -1
1992 || h->verinfo.verdef == NULL
1993 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
1994 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
1995 return TRUE;
1996
1997 /* See if we already know about this version. */
1998 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
1999 t != NULL;
2000 t = t->vn_nextref)
2001 {
2002 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2003 continue;
2004
2005 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2006 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2007 return TRUE;
2008
2009 break;
2010 }
2011
2012 /* This is a new version. Add it to tree we are building. */
2013
2014 if (t == NULL)
2015 {
2016 amt = sizeof *t;
2017 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2018 if (t == NULL)
2019 {
2020 rinfo->failed = TRUE;
2021 return FALSE;
2022 }
2023
2024 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2025 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2026 elf_tdata (rinfo->info->output_bfd)->verref = t;
2027 }
2028
2029 amt = sizeof *a;
2030 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2031 if (a == NULL)
2032 {
2033 rinfo->failed = TRUE;
2034 return FALSE;
2035 }
2036
2037 /* Note that we are copying a string pointer here, and testing it
2038 above. If bfd_elf_string_from_elf_section is ever changed to
2039 discard the string data when low in memory, this will have to be
2040 fixed. */
2041 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2042
2043 a->vna_flags = h->verinfo.verdef->vd_flags;
2044 a->vna_nextptr = t->vn_auxptr;
2045
2046 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2047 ++rinfo->vers;
2048
2049 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2050
2051 t->vn_auxptr = a;
2052
2053 return TRUE;
2054 }
2055
2056 /* Figure out appropriate versions for all the symbols. We may not
2057 have the version number script until we have read all of the input
2058 files, so until that point we don't know which symbols should be
2059 local. This function is called via elf_link_hash_traverse. */
2060
2061 static bfd_boolean
2062 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2063 {
2064 struct elf_info_failed *sinfo;
2065 struct bfd_link_info *info;
2066 const struct elf_backend_data *bed;
2067 struct elf_info_failed eif;
2068 char *p;
2069 bfd_size_type amt;
2070
2071 sinfo = (struct elf_info_failed *) data;
2072 info = sinfo->info;
2073
2074 /* Fix the symbol flags. */
2075 eif.failed = FALSE;
2076 eif.info = info;
2077 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2078 {
2079 if (eif.failed)
2080 sinfo->failed = TRUE;
2081 return FALSE;
2082 }
2083
2084 /* We only need version numbers for symbols defined in regular
2085 objects. */
2086 if (!h->def_regular)
2087 return TRUE;
2088
2089 bed = get_elf_backend_data (info->output_bfd);
2090 p = strchr (h->root.root.string, ELF_VER_CHR);
2091 if (p != NULL && h->verinfo.vertree == NULL)
2092 {
2093 struct bfd_elf_version_tree *t;
2094
2095 ++p;
2096 if (*p == ELF_VER_CHR)
2097 ++p;
2098
2099 /* If there is no version string, we can just return out. */
2100 if (*p == '\0')
2101 return TRUE;
2102
2103 /* Look for the version. If we find it, it is no longer weak. */
2104 for (t = sinfo->info->version_info; t != NULL; t = t->next)
2105 {
2106 if (strcmp (t->name, p) == 0)
2107 {
2108 size_t len;
2109 char *alc;
2110 struct bfd_elf_version_expr *d;
2111
2112 len = p - h->root.root.string;
2113 alc = (char *) bfd_malloc (len);
2114 if (alc == NULL)
2115 {
2116 sinfo->failed = TRUE;
2117 return FALSE;
2118 }
2119 memcpy (alc, h->root.root.string, len - 1);
2120 alc[len - 1] = '\0';
2121 if (alc[len - 2] == ELF_VER_CHR)
2122 alc[len - 2] = '\0';
2123
2124 h->verinfo.vertree = t;
2125 t->used = TRUE;
2126 d = NULL;
2127
2128 if (t->globals.list != NULL)
2129 d = (*t->match) (&t->globals, NULL, alc);
2130
2131 /* See if there is anything to force this symbol to
2132 local scope. */
2133 if (d == NULL && t->locals.list != NULL)
2134 {
2135 d = (*t->match) (&t->locals, NULL, alc);
2136 if (d != NULL
2137 && h->dynindx != -1
2138 && ! info->export_dynamic)
2139 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2140 }
2141
2142 free (alc);
2143 break;
2144 }
2145 }
2146
2147 /* If we are building an application, we need to create a
2148 version node for this version. */
2149 if (t == NULL && bfd_link_executable (info))
2150 {
2151 struct bfd_elf_version_tree **pp;
2152 int version_index;
2153
2154 /* If we aren't going to export this symbol, we don't need
2155 to worry about it. */
2156 if (h->dynindx == -1)
2157 return TRUE;
2158
2159 amt = sizeof *t;
2160 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, amt);
2161 if (t == NULL)
2162 {
2163 sinfo->failed = TRUE;
2164 return FALSE;
2165 }
2166
2167 t->name = p;
2168 t->name_indx = (unsigned int) -1;
2169 t->used = TRUE;
2170
2171 version_index = 1;
2172 /* Don't count anonymous version tag. */
2173 if (sinfo->info->version_info != NULL
2174 && sinfo->info->version_info->vernum == 0)
2175 version_index = 0;
2176 for (pp = &sinfo->info->version_info;
2177 *pp != NULL;
2178 pp = &(*pp)->next)
2179 ++version_index;
2180 t->vernum = version_index;
2181
2182 *pp = t;
2183
2184 h->verinfo.vertree = t;
2185 }
2186 else if (t == NULL)
2187 {
2188 /* We could not find the version for a symbol when
2189 generating a shared archive. Return an error. */
2190 (*_bfd_error_handler)
2191 (_("%B: version node not found for symbol %s"),
2192 info->output_bfd, h->root.root.string);
2193 bfd_set_error (bfd_error_bad_value);
2194 sinfo->failed = TRUE;
2195 return FALSE;
2196 }
2197 }
2198
2199 /* If we don't have a version for this symbol, see if we can find
2200 something. */
2201 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
2202 {
2203 bfd_boolean hide;
2204
2205 h->verinfo.vertree
2206 = bfd_find_version_for_sym (sinfo->info->version_info,
2207 h->root.root.string, &hide);
2208 if (h->verinfo.vertree != NULL && hide)
2209 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2210 }
2211
2212 return TRUE;
2213 }
2214 \f
2215 /* Read and swap the relocs from the section indicated by SHDR. This
2216 may be either a REL or a RELA section. The relocations are
2217 translated into RELA relocations and stored in INTERNAL_RELOCS,
2218 which should have already been allocated to contain enough space.
2219 The EXTERNAL_RELOCS are a buffer where the external form of the
2220 relocations should be stored.
2221
2222 Returns FALSE if something goes wrong. */
2223
2224 static bfd_boolean
2225 elf_link_read_relocs_from_section (bfd *abfd,
2226 asection *sec,
2227 Elf_Internal_Shdr *shdr,
2228 void *external_relocs,
2229 Elf_Internal_Rela *internal_relocs)
2230 {
2231 const struct elf_backend_data *bed;
2232 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2233 const bfd_byte *erela;
2234 const bfd_byte *erelaend;
2235 Elf_Internal_Rela *irela;
2236 Elf_Internal_Shdr *symtab_hdr;
2237 size_t nsyms;
2238
2239 /* Position ourselves at the start of the section. */
2240 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2241 return FALSE;
2242
2243 /* Read the relocations. */
2244 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2245 return FALSE;
2246
2247 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2248 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2249
2250 bed = get_elf_backend_data (abfd);
2251
2252 /* Convert the external relocations to the internal format. */
2253 if (shdr->sh_entsize == bed->s->sizeof_rel)
2254 swap_in = bed->s->swap_reloc_in;
2255 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2256 swap_in = bed->s->swap_reloca_in;
2257 else
2258 {
2259 bfd_set_error (bfd_error_wrong_format);
2260 return FALSE;
2261 }
2262
2263 erela = (const bfd_byte *) external_relocs;
2264 erelaend = erela + shdr->sh_size;
2265 irela = internal_relocs;
2266 while (erela < erelaend)
2267 {
2268 bfd_vma r_symndx;
2269
2270 (*swap_in) (abfd, erela, irela);
2271 r_symndx = ELF32_R_SYM (irela->r_info);
2272 if (bed->s->arch_size == 64)
2273 r_symndx >>= 24;
2274 if (nsyms > 0)
2275 {
2276 if ((size_t) r_symndx >= nsyms)
2277 {
2278 (*_bfd_error_handler)
2279 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2280 " for offset 0x%lx in section `%A'"),
2281 abfd, sec,
2282 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2283 bfd_set_error (bfd_error_bad_value);
2284 return FALSE;
2285 }
2286 }
2287 else if (r_symndx != STN_UNDEF)
2288 {
2289 (*_bfd_error_handler)
2290 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2291 " when the object file has no symbol table"),
2292 abfd, sec,
2293 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2294 bfd_set_error (bfd_error_bad_value);
2295 return FALSE;
2296 }
2297 irela += bed->s->int_rels_per_ext_rel;
2298 erela += shdr->sh_entsize;
2299 }
2300
2301 return TRUE;
2302 }
2303
2304 /* Read and swap the relocs for a section O. They may have been
2305 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2306 not NULL, they are used as buffers to read into. They are known to
2307 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2308 the return value is allocated using either malloc or bfd_alloc,
2309 according to the KEEP_MEMORY argument. If O has two relocation
2310 sections (both REL and RELA relocations), then the REL_HDR
2311 relocations will appear first in INTERNAL_RELOCS, followed by the
2312 RELA_HDR relocations. */
2313
2314 Elf_Internal_Rela *
2315 _bfd_elf_link_read_relocs (bfd *abfd,
2316 asection *o,
2317 void *external_relocs,
2318 Elf_Internal_Rela *internal_relocs,
2319 bfd_boolean keep_memory)
2320 {
2321 void *alloc1 = NULL;
2322 Elf_Internal_Rela *alloc2 = NULL;
2323 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2324 struct bfd_elf_section_data *esdo = elf_section_data (o);
2325 Elf_Internal_Rela *internal_rela_relocs;
2326
2327 if (esdo->relocs != NULL)
2328 return esdo->relocs;
2329
2330 if (o->reloc_count == 0)
2331 return NULL;
2332
2333 if (internal_relocs == NULL)
2334 {
2335 bfd_size_type size;
2336
2337 size = o->reloc_count;
2338 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2339 if (keep_memory)
2340 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2341 else
2342 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2343 if (internal_relocs == NULL)
2344 goto error_return;
2345 }
2346
2347 if (external_relocs == NULL)
2348 {
2349 bfd_size_type size = 0;
2350
2351 if (esdo->rel.hdr)
2352 size += esdo->rel.hdr->sh_size;
2353 if (esdo->rela.hdr)
2354 size += esdo->rela.hdr->sh_size;
2355
2356 alloc1 = bfd_malloc (size);
2357 if (alloc1 == NULL)
2358 goto error_return;
2359 external_relocs = alloc1;
2360 }
2361
2362 internal_rela_relocs = internal_relocs;
2363 if (esdo->rel.hdr)
2364 {
2365 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2366 external_relocs,
2367 internal_relocs))
2368 goto error_return;
2369 external_relocs = (((bfd_byte *) external_relocs)
2370 + esdo->rel.hdr->sh_size);
2371 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2372 * bed->s->int_rels_per_ext_rel);
2373 }
2374
2375 if (esdo->rela.hdr
2376 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2377 external_relocs,
2378 internal_rela_relocs)))
2379 goto error_return;
2380
2381 /* Cache the results for next time, if we can. */
2382 if (keep_memory)
2383 esdo->relocs = internal_relocs;
2384
2385 if (alloc1 != NULL)
2386 free (alloc1);
2387
2388 /* Don't free alloc2, since if it was allocated we are passing it
2389 back (under the name of internal_relocs). */
2390
2391 return internal_relocs;
2392
2393 error_return:
2394 if (alloc1 != NULL)
2395 free (alloc1);
2396 if (alloc2 != NULL)
2397 {
2398 if (keep_memory)
2399 bfd_release (abfd, alloc2);
2400 else
2401 free (alloc2);
2402 }
2403 return NULL;
2404 }
2405
2406 /* Compute the size of, and allocate space for, REL_HDR which is the
2407 section header for a section containing relocations for O. */
2408
2409 static bfd_boolean
2410 _bfd_elf_link_size_reloc_section (bfd *abfd,
2411 struct bfd_elf_section_reloc_data *reldata)
2412 {
2413 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2414
2415 /* That allows us to calculate the size of the section. */
2416 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2417
2418 /* The contents field must last into write_object_contents, so we
2419 allocate it with bfd_alloc rather than malloc. Also since we
2420 cannot be sure that the contents will actually be filled in,
2421 we zero the allocated space. */
2422 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2423 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2424 return FALSE;
2425
2426 if (reldata->hashes == NULL && reldata->count)
2427 {
2428 struct elf_link_hash_entry **p;
2429
2430 p = ((struct elf_link_hash_entry **)
2431 bfd_zmalloc (reldata->count * sizeof (*p)));
2432 if (p == NULL)
2433 return FALSE;
2434
2435 reldata->hashes = p;
2436 }
2437
2438 return TRUE;
2439 }
2440
2441 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2442 originated from the section given by INPUT_REL_HDR) to the
2443 OUTPUT_BFD. */
2444
2445 bfd_boolean
2446 _bfd_elf_link_output_relocs (bfd *output_bfd,
2447 asection *input_section,
2448 Elf_Internal_Shdr *input_rel_hdr,
2449 Elf_Internal_Rela *internal_relocs,
2450 struct elf_link_hash_entry **rel_hash
2451 ATTRIBUTE_UNUSED)
2452 {
2453 Elf_Internal_Rela *irela;
2454 Elf_Internal_Rela *irelaend;
2455 bfd_byte *erel;
2456 struct bfd_elf_section_reloc_data *output_reldata;
2457 asection *output_section;
2458 const struct elf_backend_data *bed;
2459 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2460 struct bfd_elf_section_data *esdo;
2461
2462 output_section = input_section->output_section;
2463
2464 bed = get_elf_backend_data (output_bfd);
2465 esdo = elf_section_data (output_section);
2466 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2467 {
2468 output_reldata = &esdo->rel;
2469 swap_out = bed->s->swap_reloc_out;
2470 }
2471 else if (esdo->rela.hdr
2472 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2473 {
2474 output_reldata = &esdo->rela;
2475 swap_out = bed->s->swap_reloca_out;
2476 }
2477 else
2478 {
2479 (*_bfd_error_handler)
2480 (_("%B: relocation size mismatch in %B section %A"),
2481 output_bfd, input_section->owner, input_section);
2482 bfd_set_error (bfd_error_wrong_format);
2483 return FALSE;
2484 }
2485
2486 erel = output_reldata->hdr->contents;
2487 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2488 irela = internal_relocs;
2489 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2490 * bed->s->int_rels_per_ext_rel);
2491 while (irela < irelaend)
2492 {
2493 (*swap_out) (output_bfd, irela, erel);
2494 irela += bed->s->int_rels_per_ext_rel;
2495 erel += input_rel_hdr->sh_entsize;
2496 }
2497
2498 /* Bump the counter, so that we know where to add the next set of
2499 relocations. */
2500 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2501
2502 return TRUE;
2503 }
2504 \f
2505 /* Make weak undefined symbols in PIE dynamic. */
2506
2507 bfd_boolean
2508 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2509 struct elf_link_hash_entry *h)
2510 {
2511 if (bfd_link_pie (info)
2512 && h->dynindx == -1
2513 && h->root.type == bfd_link_hash_undefweak)
2514 return bfd_elf_link_record_dynamic_symbol (info, h);
2515
2516 return TRUE;
2517 }
2518
2519 /* Fix up the flags for a symbol. This handles various cases which
2520 can only be fixed after all the input files are seen. This is
2521 currently called by both adjust_dynamic_symbol and
2522 assign_sym_version, which is unnecessary but perhaps more robust in
2523 the face of future changes. */
2524
2525 static bfd_boolean
2526 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2527 struct elf_info_failed *eif)
2528 {
2529 const struct elf_backend_data *bed;
2530
2531 /* If this symbol was mentioned in a non-ELF file, try to set
2532 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2533 permit a non-ELF file to correctly refer to a symbol defined in
2534 an ELF dynamic object. */
2535 if (h->non_elf)
2536 {
2537 while (h->root.type == bfd_link_hash_indirect)
2538 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2539
2540 if (h->root.type != bfd_link_hash_defined
2541 && h->root.type != bfd_link_hash_defweak)
2542 {
2543 h->ref_regular = 1;
2544 h->ref_regular_nonweak = 1;
2545 }
2546 else
2547 {
2548 if (h->root.u.def.section->owner != NULL
2549 && (bfd_get_flavour (h->root.u.def.section->owner)
2550 == bfd_target_elf_flavour))
2551 {
2552 h->ref_regular = 1;
2553 h->ref_regular_nonweak = 1;
2554 }
2555 else
2556 h->def_regular = 1;
2557 }
2558
2559 if (h->dynindx == -1
2560 && (h->def_dynamic
2561 || h->ref_dynamic))
2562 {
2563 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2564 {
2565 eif->failed = TRUE;
2566 return FALSE;
2567 }
2568 }
2569 }
2570 else
2571 {
2572 /* Unfortunately, NON_ELF is only correct if the symbol
2573 was first seen in a non-ELF file. Fortunately, if the symbol
2574 was first seen in an ELF file, we're probably OK unless the
2575 symbol was defined in a non-ELF file. Catch that case here.
2576 FIXME: We're still in trouble if the symbol was first seen in
2577 a dynamic object, and then later in a non-ELF regular object. */
2578 if ((h->root.type == bfd_link_hash_defined
2579 || h->root.type == bfd_link_hash_defweak)
2580 && !h->def_regular
2581 && (h->root.u.def.section->owner != NULL
2582 ? (bfd_get_flavour (h->root.u.def.section->owner)
2583 != bfd_target_elf_flavour)
2584 : (bfd_is_abs_section (h->root.u.def.section)
2585 && !h->def_dynamic)))
2586 h->def_regular = 1;
2587 }
2588
2589 /* Backend specific symbol fixup. */
2590 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2591 if (bed->elf_backend_fixup_symbol
2592 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2593 return FALSE;
2594
2595 /* If this is a final link, and the symbol was defined as a common
2596 symbol in a regular object file, and there was no definition in
2597 any dynamic object, then the linker will have allocated space for
2598 the symbol in a common section but the DEF_REGULAR
2599 flag will not have been set. */
2600 if (h->root.type == bfd_link_hash_defined
2601 && !h->def_regular
2602 && h->ref_regular
2603 && !h->def_dynamic
2604 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2605 h->def_regular = 1;
2606
2607 /* If -Bsymbolic was used (which means to bind references to global
2608 symbols to the definition within the shared object), and this
2609 symbol was defined in a regular object, then it actually doesn't
2610 need a PLT entry. Likewise, if the symbol has non-default
2611 visibility. If the symbol has hidden or internal visibility, we
2612 will force it local. */
2613 if (h->needs_plt
2614 && bfd_link_pic (eif->info)
2615 && is_elf_hash_table (eif->info->hash)
2616 && (SYMBOLIC_BIND (eif->info, h)
2617 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2618 && h->def_regular)
2619 {
2620 bfd_boolean force_local;
2621
2622 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2623 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2624 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2625 }
2626
2627 /* If a weak undefined symbol has non-default visibility, we also
2628 hide it from the dynamic linker. */
2629 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2630 && h->root.type == bfd_link_hash_undefweak)
2631 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2632
2633 /* If this is a weak defined symbol in a dynamic object, and we know
2634 the real definition in the dynamic object, copy interesting flags
2635 over to the real definition. */
2636 if (h->u.weakdef != NULL)
2637 {
2638 /* If the real definition is defined by a regular object file,
2639 don't do anything special. See the longer description in
2640 _bfd_elf_adjust_dynamic_symbol, below. */
2641 if (h->u.weakdef->def_regular)
2642 h->u.weakdef = NULL;
2643 else
2644 {
2645 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2646
2647 while (h->root.type == bfd_link_hash_indirect)
2648 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2649
2650 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2651 || h->root.type == bfd_link_hash_defweak);
2652 BFD_ASSERT (weakdef->def_dynamic);
2653 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2654 || weakdef->root.type == bfd_link_hash_defweak);
2655 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2656 }
2657 }
2658
2659 return TRUE;
2660 }
2661
2662 /* Make the backend pick a good value for a dynamic symbol. This is
2663 called via elf_link_hash_traverse, and also calls itself
2664 recursively. */
2665
2666 static bfd_boolean
2667 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2668 {
2669 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2670 bfd *dynobj;
2671 const struct elf_backend_data *bed;
2672
2673 if (! is_elf_hash_table (eif->info->hash))
2674 return FALSE;
2675
2676 /* Ignore indirect symbols. These are added by the versioning code. */
2677 if (h->root.type == bfd_link_hash_indirect)
2678 return TRUE;
2679
2680 /* Fix the symbol flags. */
2681 if (! _bfd_elf_fix_symbol_flags (h, eif))
2682 return FALSE;
2683
2684 /* If this symbol does not require a PLT entry, and it is not
2685 defined by a dynamic object, or is not referenced by a regular
2686 object, ignore it. We do have to handle a weak defined symbol,
2687 even if no regular object refers to it, if we decided to add it
2688 to the dynamic symbol table. FIXME: Do we normally need to worry
2689 about symbols which are defined by one dynamic object and
2690 referenced by another one? */
2691 if (!h->needs_plt
2692 && h->type != STT_GNU_IFUNC
2693 && (h->def_regular
2694 || !h->def_dynamic
2695 || (!h->ref_regular
2696 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2697 {
2698 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2699 return TRUE;
2700 }
2701
2702 /* If we've already adjusted this symbol, don't do it again. This
2703 can happen via a recursive call. */
2704 if (h->dynamic_adjusted)
2705 return TRUE;
2706
2707 /* Don't look at this symbol again. Note that we must set this
2708 after checking the above conditions, because we may look at a
2709 symbol once, decide not to do anything, and then get called
2710 recursively later after REF_REGULAR is set below. */
2711 h->dynamic_adjusted = 1;
2712
2713 /* If this is a weak definition, and we know a real definition, and
2714 the real symbol is not itself defined by a regular object file,
2715 then get a good value for the real definition. We handle the
2716 real symbol first, for the convenience of the backend routine.
2717
2718 Note that there is a confusing case here. If the real definition
2719 is defined by a regular object file, we don't get the real symbol
2720 from the dynamic object, but we do get the weak symbol. If the
2721 processor backend uses a COPY reloc, then if some routine in the
2722 dynamic object changes the real symbol, we will not see that
2723 change in the corresponding weak symbol. This is the way other
2724 ELF linkers work as well, and seems to be a result of the shared
2725 library model.
2726
2727 I will clarify this issue. Most SVR4 shared libraries define the
2728 variable _timezone and define timezone as a weak synonym. The
2729 tzset call changes _timezone. If you write
2730 extern int timezone;
2731 int _timezone = 5;
2732 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2733 you might expect that, since timezone is a synonym for _timezone,
2734 the same number will print both times. However, if the processor
2735 backend uses a COPY reloc, then actually timezone will be copied
2736 into your process image, and, since you define _timezone
2737 yourself, _timezone will not. Thus timezone and _timezone will
2738 wind up at different memory locations. The tzset call will set
2739 _timezone, leaving timezone unchanged. */
2740
2741 if (h->u.weakdef != NULL)
2742 {
2743 /* If we get to this point, there is an implicit reference to
2744 H->U.WEAKDEF by a regular object file via the weak symbol H. */
2745 h->u.weakdef->ref_regular = 1;
2746
2747 /* Ensure that the backend adjust_dynamic_symbol function sees
2748 H->U.WEAKDEF before H by recursively calling ourselves. */
2749 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2750 return FALSE;
2751 }
2752
2753 /* If a symbol has no type and no size and does not require a PLT
2754 entry, then we are probably about to do the wrong thing here: we
2755 are probably going to create a COPY reloc for an empty object.
2756 This case can arise when a shared object is built with assembly
2757 code, and the assembly code fails to set the symbol type. */
2758 if (h->size == 0
2759 && h->type == STT_NOTYPE
2760 && !h->needs_plt)
2761 (*_bfd_error_handler)
2762 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2763 h->root.root.string);
2764
2765 dynobj = elf_hash_table (eif->info)->dynobj;
2766 bed = get_elf_backend_data (dynobj);
2767
2768 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2769 {
2770 eif->failed = TRUE;
2771 return FALSE;
2772 }
2773
2774 return TRUE;
2775 }
2776
2777 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2778 DYNBSS. */
2779
2780 bfd_boolean
2781 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2782 struct elf_link_hash_entry *h,
2783 asection *dynbss)
2784 {
2785 unsigned int power_of_two;
2786 bfd_vma mask;
2787 asection *sec = h->root.u.def.section;
2788
2789 /* The section aligment of definition is the maximum alignment
2790 requirement of symbols defined in the section. Since we don't
2791 know the symbol alignment requirement, we start with the
2792 maximum alignment and check low bits of the symbol address
2793 for the minimum alignment. */
2794 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2795 mask = ((bfd_vma) 1 << power_of_two) - 1;
2796 while ((h->root.u.def.value & mask) != 0)
2797 {
2798 mask >>= 1;
2799 --power_of_two;
2800 }
2801
2802 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2803 dynbss))
2804 {
2805 /* Adjust the section alignment if needed. */
2806 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2807 power_of_two))
2808 return FALSE;
2809 }
2810
2811 /* We make sure that the symbol will be aligned properly. */
2812 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2813
2814 /* Define the symbol as being at this point in DYNBSS. */
2815 h->root.u.def.section = dynbss;
2816 h->root.u.def.value = dynbss->size;
2817
2818 /* Increment the size of DYNBSS to make room for the symbol. */
2819 dynbss->size += h->size;
2820
2821 /* No error if extern_protected_data is true. */
2822 if (h->protected_def
2823 && (!info->extern_protected_data
2824 || (info->extern_protected_data < 0
2825 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
2826 info->callbacks->einfo
2827 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2828 h->root.root.string);
2829
2830 return TRUE;
2831 }
2832
2833 /* Adjust all external symbols pointing into SEC_MERGE sections
2834 to reflect the object merging within the sections. */
2835
2836 static bfd_boolean
2837 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2838 {
2839 asection *sec;
2840
2841 if ((h->root.type == bfd_link_hash_defined
2842 || h->root.type == bfd_link_hash_defweak)
2843 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2844 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
2845 {
2846 bfd *output_bfd = (bfd *) data;
2847
2848 h->root.u.def.value =
2849 _bfd_merged_section_offset (output_bfd,
2850 &h->root.u.def.section,
2851 elf_section_data (sec)->sec_info,
2852 h->root.u.def.value);
2853 }
2854
2855 return TRUE;
2856 }
2857
2858 /* Returns false if the symbol referred to by H should be considered
2859 to resolve local to the current module, and true if it should be
2860 considered to bind dynamically. */
2861
2862 bfd_boolean
2863 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2864 struct bfd_link_info *info,
2865 bfd_boolean not_local_protected)
2866 {
2867 bfd_boolean binding_stays_local_p;
2868 const struct elf_backend_data *bed;
2869 struct elf_link_hash_table *hash_table;
2870
2871 if (h == NULL)
2872 return FALSE;
2873
2874 while (h->root.type == bfd_link_hash_indirect
2875 || h->root.type == bfd_link_hash_warning)
2876 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2877
2878 /* If it was forced local, then clearly it's not dynamic. */
2879 if (h->dynindx == -1)
2880 return FALSE;
2881 if (h->forced_local)
2882 return FALSE;
2883
2884 /* Identify the cases where name binding rules say that a
2885 visible symbol resolves locally. */
2886 binding_stays_local_p = (bfd_link_executable (info)
2887 || SYMBOLIC_BIND (info, h));
2888
2889 switch (ELF_ST_VISIBILITY (h->other))
2890 {
2891 case STV_INTERNAL:
2892 case STV_HIDDEN:
2893 return FALSE;
2894
2895 case STV_PROTECTED:
2896 hash_table = elf_hash_table (info);
2897 if (!is_elf_hash_table (hash_table))
2898 return FALSE;
2899
2900 bed = get_elf_backend_data (hash_table->dynobj);
2901
2902 /* Proper resolution for function pointer equality may require
2903 that these symbols perhaps be resolved dynamically, even though
2904 we should be resolving them to the current module. */
2905 if (!not_local_protected || !bed->is_function_type (h->type))
2906 binding_stays_local_p = TRUE;
2907 break;
2908
2909 default:
2910 break;
2911 }
2912
2913 /* If it isn't defined locally, then clearly it's dynamic. */
2914 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2915 return TRUE;
2916
2917 /* Otherwise, the symbol is dynamic if binding rules don't tell
2918 us that it remains local. */
2919 return !binding_stays_local_p;
2920 }
2921
2922 /* Return true if the symbol referred to by H should be considered
2923 to resolve local to the current module, and false otherwise. Differs
2924 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2925 undefined symbols. The two functions are virtually identical except
2926 for the place where forced_local and dynindx == -1 are tested. If
2927 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2928 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2929 the symbol is local only for defined symbols.
2930 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2931 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2932 treatment of undefined weak symbols. For those that do not make
2933 undefined weak symbols dynamic, both functions may return false. */
2934
2935 bfd_boolean
2936 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2937 struct bfd_link_info *info,
2938 bfd_boolean local_protected)
2939 {
2940 const struct elf_backend_data *bed;
2941 struct elf_link_hash_table *hash_table;
2942
2943 /* If it's a local sym, of course we resolve locally. */
2944 if (h == NULL)
2945 return TRUE;
2946
2947 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2948 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2949 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2950 return TRUE;
2951
2952 /* Common symbols that become definitions don't get the DEF_REGULAR
2953 flag set, so test it first, and don't bail out. */
2954 if (ELF_COMMON_DEF_P (h))
2955 /* Do nothing. */;
2956 /* If we don't have a definition in a regular file, then we can't
2957 resolve locally. The sym is either undefined or dynamic. */
2958 else if (!h->def_regular)
2959 return FALSE;
2960
2961 /* Forced local symbols resolve locally. */
2962 if (h->forced_local)
2963 return TRUE;
2964
2965 /* As do non-dynamic symbols. */
2966 if (h->dynindx == -1)
2967 return TRUE;
2968
2969 /* At this point, we know the symbol is defined and dynamic. In an
2970 executable it must resolve locally, likewise when building symbolic
2971 shared libraries. */
2972 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
2973 return TRUE;
2974
2975 /* Now deal with defined dynamic symbols in shared libraries. Ones
2976 with default visibility might not resolve locally. */
2977 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2978 return FALSE;
2979
2980 hash_table = elf_hash_table (info);
2981 if (!is_elf_hash_table (hash_table))
2982 return TRUE;
2983
2984 bed = get_elf_backend_data (hash_table->dynobj);
2985
2986 /* If extern_protected_data is false, STV_PROTECTED non-function
2987 symbols are local. */
2988 if ((!info->extern_protected_data
2989 || (info->extern_protected_data < 0
2990 && !bed->extern_protected_data))
2991 && !bed->is_function_type (h->type))
2992 return TRUE;
2993
2994 /* Function pointer equality tests may require that STV_PROTECTED
2995 symbols be treated as dynamic symbols. If the address of a
2996 function not defined in an executable is set to that function's
2997 plt entry in the executable, then the address of the function in
2998 a shared library must also be the plt entry in the executable. */
2999 return local_protected;
3000 }
3001
3002 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3003 aligned. Returns the first TLS output section. */
3004
3005 struct bfd_section *
3006 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3007 {
3008 struct bfd_section *sec, *tls;
3009 unsigned int align = 0;
3010
3011 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3012 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3013 break;
3014 tls = sec;
3015
3016 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3017 if (sec->alignment_power > align)
3018 align = sec->alignment_power;
3019
3020 elf_hash_table (info)->tls_sec = tls;
3021
3022 /* Ensure the alignment of the first section is the largest alignment,
3023 so that the tls segment starts aligned. */
3024 if (tls != NULL)
3025 tls->alignment_power = align;
3026
3027 return tls;
3028 }
3029
3030 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3031 static bfd_boolean
3032 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3033 Elf_Internal_Sym *sym)
3034 {
3035 const struct elf_backend_data *bed;
3036
3037 /* Local symbols do not count, but target specific ones might. */
3038 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3039 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3040 return FALSE;
3041
3042 bed = get_elf_backend_data (abfd);
3043 /* Function symbols do not count. */
3044 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3045 return FALSE;
3046
3047 /* If the section is undefined, then so is the symbol. */
3048 if (sym->st_shndx == SHN_UNDEF)
3049 return FALSE;
3050
3051 /* If the symbol is defined in the common section, then
3052 it is a common definition and so does not count. */
3053 if (bed->common_definition (sym))
3054 return FALSE;
3055
3056 /* If the symbol is in a target specific section then we
3057 must rely upon the backend to tell us what it is. */
3058 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3059 /* FIXME - this function is not coded yet:
3060
3061 return _bfd_is_global_symbol_definition (abfd, sym);
3062
3063 Instead for now assume that the definition is not global,
3064 Even if this is wrong, at least the linker will behave
3065 in the same way that it used to do. */
3066 return FALSE;
3067
3068 return TRUE;
3069 }
3070
3071 /* Search the symbol table of the archive element of the archive ABFD
3072 whose archive map contains a mention of SYMDEF, and determine if
3073 the symbol is defined in this element. */
3074 static bfd_boolean
3075 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3076 {
3077 Elf_Internal_Shdr * hdr;
3078 bfd_size_type symcount;
3079 bfd_size_type extsymcount;
3080 bfd_size_type extsymoff;
3081 Elf_Internal_Sym *isymbuf;
3082 Elf_Internal_Sym *isym;
3083 Elf_Internal_Sym *isymend;
3084 bfd_boolean result;
3085
3086 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3087 if (abfd == NULL)
3088 return FALSE;
3089
3090 /* Return FALSE if the object has been claimed by plugin. */
3091 if (abfd->plugin_format == bfd_plugin_yes)
3092 return FALSE;
3093
3094 if (! bfd_check_format (abfd, bfd_object))
3095 return FALSE;
3096
3097 /* Select the appropriate symbol table. */
3098 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3099 hdr = &elf_tdata (abfd)->symtab_hdr;
3100 else
3101 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3102
3103 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3104
3105 /* The sh_info field of the symtab header tells us where the
3106 external symbols start. We don't care about the local symbols. */
3107 if (elf_bad_symtab (abfd))
3108 {
3109 extsymcount = symcount;
3110 extsymoff = 0;
3111 }
3112 else
3113 {
3114 extsymcount = symcount - hdr->sh_info;
3115 extsymoff = hdr->sh_info;
3116 }
3117
3118 if (extsymcount == 0)
3119 return FALSE;
3120
3121 /* Read in the symbol table. */
3122 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3123 NULL, NULL, NULL);
3124 if (isymbuf == NULL)
3125 return FALSE;
3126
3127 /* Scan the symbol table looking for SYMDEF. */
3128 result = FALSE;
3129 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3130 {
3131 const char *name;
3132
3133 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3134 isym->st_name);
3135 if (name == NULL)
3136 break;
3137
3138 if (strcmp (name, symdef->name) == 0)
3139 {
3140 result = is_global_data_symbol_definition (abfd, isym);
3141 break;
3142 }
3143 }
3144
3145 free (isymbuf);
3146
3147 return result;
3148 }
3149 \f
3150 /* Add an entry to the .dynamic table. */
3151
3152 bfd_boolean
3153 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3154 bfd_vma tag,
3155 bfd_vma val)
3156 {
3157 struct elf_link_hash_table *hash_table;
3158 const struct elf_backend_data *bed;
3159 asection *s;
3160 bfd_size_type newsize;
3161 bfd_byte *newcontents;
3162 Elf_Internal_Dyn dyn;
3163
3164 hash_table = elf_hash_table (info);
3165 if (! is_elf_hash_table (hash_table))
3166 return FALSE;
3167
3168 bed = get_elf_backend_data (hash_table->dynobj);
3169 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3170 BFD_ASSERT (s != NULL);
3171
3172 newsize = s->size + bed->s->sizeof_dyn;
3173 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3174 if (newcontents == NULL)
3175 return FALSE;
3176
3177 dyn.d_tag = tag;
3178 dyn.d_un.d_val = val;
3179 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3180
3181 s->size = newsize;
3182 s->contents = newcontents;
3183
3184 return TRUE;
3185 }
3186
3187 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3188 otherwise just check whether one already exists. Returns -1 on error,
3189 1 if a DT_NEEDED tag already exists, and 0 on success. */
3190
3191 static int
3192 elf_add_dt_needed_tag (bfd *abfd,
3193 struct bfd_link_info *info,
3194 const char *soname,
3195 bfd_boolean do_it)
3196 {
3197 struct elf_link_hash_table *hash_table;
3198 bfd_size_type strindex;
3199
3200 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3201 return -1;
3202
3203 hash_table = elf_hash_table (info);
3204 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3205 if (strindex == (bfd_size_type) -1)
3206 return -1;
3207
3208 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3209 {
3210 asection *sdyn;
3211 const struct elf_backend_data *bed;
3212 bfd_byte *extdyn;
3213
3214 bed = get_elf_backend_data (hash_table->dynobj);
3215 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3216 if (sdyn != NULL)
3217 for (extdyn = sdyn->contents;
3218 extdyn < sdyn->contents + sdyn->size;
3219 extdyn += bed->s->sizeof_dyn)
3220 {
3221 Elf_Internal_Dyn dyn;
3222
3223 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3224 if (dyn.d_tag == DT_NEEDED
3225 && dyn.d_un.d_val == strindex)
3226 {
3227 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3228 return 1;
3229 }
3230 }
3231 }
3232
3233 if (do_it)
3234 {
3235 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3236 return -1;
3237
3238 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3239 return -1;
3240 }
3241 else
3242 /* We were just checking for existence of the tag. */
3243 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3244
3245 return 0;
3246 }
3247
3248 static bfd_boolean
3249 on_needed_list (const char *soname, struct bfd_link_needed_list *needed)
3250 {
3251 for (; needed != NULL; needed = needed->next)
3252 if ((elf_dyn_lib_class (needed->by) & DYN_AS_NEEDED) == 0
3253 && strcmp (soname, needed->name) == 0)
3254 return TRUE;
3255
3256 return FALSE;
3257 }
3258
3259 /* Sort symbol by value, section, and size. */
3260 static int
3261 elf_sort_symbol (const void *arg1, const void *arg2)
3262 {
3263 const struct elf_link_hash_entry *h1;
3264 const struct elf_link_hash_entry *h2;
3265 bfd_signed_vma vdiff;
3266
3267 h1 = *(const struct elf_link_hash_entry **) arg1;
3268 h2 = *(const struct elf_link_hash_entry **) arg2;
3269 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3270 if (vdiff != 0)
3271 return vdiff > 0 ? 1 : -1;
3272 else
3273 {
3274 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3275 if (sdiff != 0)
3276 return sdiff > 0 ? 1 : -1;
3277 }
3278 vdiff = h1->size - h2->size;
3279 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3280 }
3281
3282 /* This function is used to adjust offsets into .dynstr for
3283 dynamic symbols. This is called via elf_link_hash_traverse. */
3284
3285 static bfd_boolean
3286 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3287 {
3288 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3289
3290 if (h->dynindx != -1)
3291 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3292 return TRUE;
3293 }
3294
3295 /* Assign string offsets in .dynstr, update all structures referencing
3296 them. */
3297
3298 static bfd_boolean
3299 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3300 {
3301 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3302 struct elf_link_local_dynamic_entry *entry;
3303 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3304 bfd *dynobj = hash_table->dynobj;
3305 asection *sdyn;
3306 bfd_size_type size;
3307 const struct elf_backend_data *bed;
3308 bfd_byte *extdyn;
3309
3310 _bfd_elf_strtab_finalize (dynstr);
3311 size = _bfd_elf_strtab_size (dynstr);
3312
3313 bed = get_elf_backend_data (dynobj);
3314 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3315 BFD_ASSERT (sdyn != NULL);
3316
3317 /* Update all .dynamic entries referencing .dynstr strings. */
3318 for (extdyn = sdyn->contents;
3319 extdyn < sdyn->contents + sdyn->size;
3320 extdyn += bed->s->sizeof_dyn)
3321 {
3322 Elf_Internal_Dyn dyn;
3323
3324 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3325 switch (dyn.d_tag)
3326 {
3327 case DT_STRSZ:
3328 dyn.d_un.d_val = size;
3329 break;
3330 case DT_NEEDED:
3331 case DT_SONAME:
3332 case DT_RPATH:
3333 case DT_RUNPATH:
3334 case DT_FILTER:
3335 case DT_AUXILIARY:
3336 case DT_AUDIT:
3337 case DT_DEPAUDIT:
3338 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3339 break;
3340 default:
3341 continue;
3342 }
3343 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3344 }
3345
3346 /* Now update local dynamic symbols. */
3347 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3348 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3349 entry->isym.st_name);
3350
3351 /* And the rest of dynamic symbols. */
3352 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3353
3354 /* Adjust version definitions. */
3355 if (elf_tdata (output_bfd)->cverdefs)
3356 {
3357 asection *s;
3358 bfd_byte *p;
3359 bfd_size_type i;
3360 Elf_Internal_Verdef def;
3361 Elf_Internal_Verdaux defaux;
3362
3363 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3364 p = s->contents;
3365 do
3366 {
3367 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3368 &def);
3369 p += sizeof (Elf_External_Verdef);
3370 if (def.vd_aux != sizeof (Elf_External_Verdef))
3371 continue;
3372 for (i = 0; i < def.vd_cnt; ++i)
3373 {
3374 _bfd_elf_swap_verdaux_in (output_bfd,
3375 (Elf_External_Verdaux *) p, &defaux);
3376 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3377 defaux.vda_name);
3378 _bfd_elf_swap_verdaux_out (output_bfd,
3379 &defaux, (Elf_External_Verdaux *) p);
3380 p += sizeof (Elf_External_Verdaux);
3381 }
3382 }
3383 while (def.vd_next);
3384 }
3385
3386 /* Adjust version references. */
3387 if (elf_tdata (output_bfd)->verref)
3388 {
3389 asection *s;
3390 bfd_byte *p;
3391 bfd_size_type i;
3392 Elf_Internal_Verneed need;
3393 Elf_Internal_Vernaux needaux;
3394
3395 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3396 p = s->contents;
3397 do
3398 {
3399 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3400 &need);
3401 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3402 _bfd_elf_swap_verneed_out (output_bfd, &need,
3403 (Elf_External_Verneed *) p);
3404 p += sizeof (Elf_External_Verneed);
3405 for (i = 0; i < need.vn_cnt; ++i)
3406 {
3407 _bfd_elf_swap_vernaux_in (output_bfd,
3408 (Elf_External_Vernaux *) p, &needaux);
3409 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3410 needaux.vna_name);
3411 _bfd_elf_swap_vernaux_out (output_bfd,
3412 &needaux,
3413 (Elf_External_Vernaux *) p);
3414 p += sizeof (Elf_External_Vernaux);
3415 }
3416 }
3417 while (need.vn_next);
3418 }
3419
3420 return TRUE;
3421 }
3422 \f
3423 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3424 The default is to only match when the INPUT and OUTPUT are exactly
3425 the same target. */
3426
3427 bfd_boolean
3428 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3429 const bfd_target *output)
3430 {
3431 return input == output;
3432 }
3433
3434 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3435 This version is used when different targets for the same architecture
3436 are virtually identical. */
3437
3438 bfd_boolean
3439 _bfd_elf_relocs_compatible (const bfd_target *input,
3440 const bfd_target *output)
3441 {
3442 const struct elf_backend_data *obed, *ibed;
3443
3444 if (input == output)
3445 return TRUE;
3446
3447 ibed = xvec_get_elf_backend_data (input);
3448 obed = xvec_get_elf_backend_data (output);
3449
3450 if (ibed->arch != obed->arch)
3451 return FALSE;
3452
3453 /* If both backends are using this function, deem them compatible. */
3454 return ibed->relocs_compatible == obed->relocs_compatible;
3455 }
3456
3457 /* Make a special call to the linker "notice" function to tell it that
3458 we are about to handle an as-needed lib, or have finished
3459 processing the lib. */
3460
3461 bfd_boolean
3462 _bfd_elf_notice_as_needed (bfd *ibfd,
3463 struct bfd_link_info *info,
3464 enum notice_asneeded_action act)
3465 {
3466 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3467 }
3468
3469 /* Add symbols from an ELF object file to the linker hash table. */
3470
3471 static bfd_boolean
3472 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3473 {
3474 Elf_Internal_Ehdr *ehdr;
3475 Elf_Internal_Shdr *hdr;
3476 bfd_size_type symcount;
3477 bfd_size_type extsymcount;
3478 bfd_size_type extsymoff;
3479 struct elf_link_hash_entry **sym_hash;
3480 bfd_boolean dynamic;
3481 Elf_External_Versym *extversym = NULL;
3482 Elf_External_Versym *ever;
3483 struct elf_link_hash_entry *weaks;
3484 struct elf_link_hash_entry **nondeflt_vers = NULL;
3485 bfd_size_type nondeflt_vers_cnt = 0;
3486 Elf_Internal_Sym *isymbuf = NULL;
3487 Elf_Internal_Sym *isym;
3488 Elf_Internal_Sym *isymend;
3489 const struct elf_backend_data *bed;
3490 bfd_boolean add_needed;
3491 struct elf_link_hash_table *htab;
3492 bfd_size_type amt;
3493 void *alloc_mark = NULL;
3494 struct bfd_hash_entry **old_table = NULL;
3495 unsigned int old_size = 0;
3496 unsigned int old_count = 0;
3497 void *old_tab = NULL;
3498 void *old_ent;
3499 struct bfd_link_hash_entry *old_undefs = NULL;
3500 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3501 long old_dynsymcount = 0;
3502 bfd_size_type old_dynstr_size = 0;
3503 size_t tabsize = 0;
3504 asection *s;
3505 bfd_boolean just_syms;
3506
3507 htab = elf_hash_table (info);
3508 bed = get_elf_backend_data (abfd);
3509
3510 if ((abfd->flags & DYNAMIC) == 0)
3511 dynamic = FALSE;
3512 else
3513 {
3514 dynamic = TRUE;
3515
3516 /* You can't use -r against a dynamic object. Also, there's no
3517 hope of using a dynamic object which does not exactly match
3518 the format of the output file. */
3519 if (bfd_link_relocatable (info)
3520 || !is_elf_hash_table (htab)
3521 || info->output_bfd->xvec != abfd->xvec)
3522 {
3523 if (bfd_link_relocatable (info))
3524 bfd_set_error (bfd_error_invalid_operation);
3525 else
3526 bfd_set_error (bfd_error_wrong_format);
3527 goto error_return;
3528 }
3529 }
3530
3531 ehdr = elf_elfheader (abfd);
3532 if (info->warn_alternate_em
3533 && bed->elf_machine_code != ehdr->e_machine
3534 && ((bed->elf_machine_alt1 != 0
3535 && ehdr->e_machine == bed->elf_machine_alt1)
3536 || (bed->elf_machine_alt2 != 0
3537 && ehdr->e_machine == bed->elf_machine_alt2)))
3538 info->callbacks->einfo
3539 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3540 ehdr->e_machine, abfd, bed->elf_machine_code);
3541
3542 /* As a GNU extension, any input sections which are named
3543 .gnu.warning.SYMBOL are treated as warning symbols for the given
3544 symbol. This differs from .gnu.warning sections, which generate
3545 warnings when they are included in an output file. */
3546 /* PR 12761: Also generate this warning when building shared libraries. */
3547 for (s = abfd->sections; s != NULL; s = s->next)
3548 {
3549 const char *name;
3550
3551 name = bfd_get_section_name (abfd, s);
3552 if (CONST_STRNEQ (name, ".gnu.warning."))
3553 {
3554 char *msg;
3555 bfd_size_type sz;
3556
3557 name += sizeof ".gnu.warning." - 1;
3558
3559 /* If this is a shared object, then look up the symbol
3560 in the hash table. If it is there, and it is already
3561 been defined, then we will not be using the entry
3562 from this shared object, so we don't need to warn.
3563 FIXME: If we see the definition in a regular object
3564 later on, we will warn, but we shouldn't. The only
3565 fix is to keep track of what warnings we are supposed
3566 to emit, and then handle them all at the end of the
3567 link. */
3568 if (dynamic)
3569 {
3570 struct elf_link_hash_entry *h;
3571
3572 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3573
3574 /* FIXME: What about bfd_link_hash_common? */
3575 if (h != NULL
3576 && (h->root.type == bfd_link_hash_defined
3577 || h->root.type == bfd_link_hash_defweak))
3578 continue;
3579 }
3580
3581 sz = s->size;
3582 msg = (char *) bfd_alloc (abfd, sz + 1);
3583 if (msg == NULL)
3584 goto error_return;
3585
3586 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3587 goto error_return;
3588
3589 msg[sz] = '\0';
3590
3591 if (! (_bfd_generic_link_add_one_symbol
3592 (info, abfd, name, BSF_WARNING, s, 0, msg,
3593 FALSE, bed->collect, NULL)))
3594 goto error_return;
3595
3596 if (bfd_link_executable (info))
3597 {
3598 /* Clobber the section size so that the warning does
3599 not get copied into the output file. */
3600 s->size = 0;
3601
3602 /* Also set SEC_EXCLUDE, so that symbols defined in
3603 the warning section don't get copied to the output. */
3604 s->flags |= SEC_EXCLUDE;
3605 }
3606 }
3607 }
3608
3609 just_syms = ((s = abfd->sections) != NULL
3610 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3611
3612 add_needed = TRUE;
3613 if (! dynamic)
3614 {
3615 /* If we are creating a shared library, create all the dynamic
3616 sections immediately. We need to attach them to something,
3617 so we attach them to this BFD, provided it is the right
3618 format and is not from ld --just-symbols. Always create the
3619 dynamic sections for -E/--dynamic-list. FIXME: If there
3620 are no input BFD's of the same format as the output, we can't
3621 make a shared library. */
3622 if (!just_syms
3623 && (bfd_link_pic (info)
3624 || info->export_dynamic
3625 || info->dynamic)
3626 && is_elf_hash_table (htab)
3627 && info->output_bfd->xvec == abfd->xvec
3628 && !htab->dynamic_sections_created)
3629 {
3630 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3631 goto error_return;
3632 }
3633 }
3634 else if (!is_elf_hash_table (htab))
3635 goto error_return;
3636 else
3637 {
3638 const char *soname = NULL;
3639 char *audit = NULL;
3640 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3641 int ret;
3642
3643 /* ld --just-symbols and dynamic objects don't mix very well.
3644 ld shouldn't allow it. */
3645 if (just_syms)
3646 abort ();
3647
3648 /* If this dynamic lib was specified on the command line with
3649 --as-needed in effect, then we don't want to add a DT_NEEDED
3650 tag unless the lib is actually used. Similary for libs brought
3651 in by another lib's DT_NEEDED. When --no-add-needed is used
3652 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3653 any dynamic library in DT_NEEDED tags in the dynamic lib at
3654 all. */
3655 add_needed = (elf_dyn_lib_class (abfd)
3656 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3657 | DYN_NO_NEEDED)) == 0;
3658
3659 s = bfd_get_section_by_name (abfd, ".dynamic");
3660 if (s != NULL)
3661 {
3662 bfd_byte *dynbuf;
3663 bfd_byte *extdyn;
3664 unsigned int elfsec;
3665 unsigned long shlink;
3666
3667 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3668 {
3669 error_free_dyn:
3670 free (dynbuf);
3671 goto error_return;
3672 }
3673
3674 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3675 if (elfsec == SHN_BAD)
3676 goto error_free_dyn;
3677 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3678
3679 for (extdyn = dynbuf;
3680 extdyn < dynbuf + s->size;
3681 extdyn += bed->s->sizeof_dyn)
3682 {
3683 Elf_Internal_Dyn dyn;
3684
3685 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3686 if (dyn.d_tag == DT_SONAME)
3687 {
3688 unsigned int tagv = dyn.d_un.d_val;
3689 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3690 if (soname == NULL)
3691 goto error_free_dyn;
3692 }
3693 if (dyn.d_tag == DT_NEEDED)
3694 {
3695 struct bfd_link_needed_list *n, **pn;
3696 char *fnm, *anm;
3697 unsigned int tagv = dyn.d_un.d_val;
3698
3699 amt = sizeof (struct bfd_link_needed_list);
3700 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3701 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3702 if (n == NULL || fnm == NULL)
3703 goto error_free_dyn;
3704 amt = strlen (fnm) + 1;
3705 anm = (char *) bfd_alloc (abfd, amt);
3706 if (anm == NULL)
3707 goto error_free_dyn;
3708 memcpy (anm, fnm, amt);
3709 n->name = anm;
3710 n->by = abfd;
3711 n->next = NULL;
3712 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3713 ;
3714 *pn = n;
3715 }
3716 if (dyn.d_tag == DT_RUNPATH)
3717 {
3718 struct bfd_link_needed_list *n, **pn;
3719 char *fnm, *anm;
3720 unsigned int tagv = dyn.d_un.d_val;
3721
3722 amt = sizeof (struct bfd_link_needed_list);
3723 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3724 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3725 if (n == NULL || fnm == NULL)
3726 goto error_free_dyn;
3727 amt = strlen (fnm) + 1;
3728 anm = (char *) bfd_alloc (abfd, amt);
3729 if (anm == NULL)
3730 goto error_free_dyn;
3731 memcpy (anm, fnm, amt);
3732 n->name = anm;
3733 n->by = abfd;
3734 n->next = NULL;
3735 for (pn = & runpath;
3736 *pn != NULL;
3737 pn = &(*pn)->next)
3738 ;
3739 *pn = n;
3740 }
3741 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3742 if (!runpath && dyn.d_tag == DT_RPATH)
3743 {
3744 struct bfd_link_needed_list *n, **pn;
3745 char *fnm, *anm;
3746 unsigned int tagv = dyn.d_un.d_val;
3747
3748 amt = sizeof (struct bfd_link_needed_list);
3749 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3750 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3751 if (n == NULL || fnm == NULL)
3752 goto error_free_dyn;
3753 amt = strlen (fnm) + 1;
3754 anm = (char *) bfd_alloc (abfd, amt);
3755 if (anm == NULL)
3756 goto error_free_dyn;
3757 memcpy (anm, fnm, amt);
3758 n->name = anm;
3759 n->by = abfd;
3760 n->next = NULL;
3761 for (pn = & rpath;
3762 *pn != NULL;
3763 pn = &(*pn)->next)
3764 ;
3765 *pn = n;
3766 }
3767 if (dyn.d_tag == DT_AUDIT)
3768 {
3769 unsigned int tagv = dyn.d_un.d_val;
3770 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3771 }
3772 }
3773
3774 free (dynbuf);
3775 }
3776
3777 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3778 frees all more recently bfd_alloc'd blocks as well. */
3779 if (runpath)
3780 rpath = runpath;
3781
3782 if (rpath)
3783 {
3784 struct bfd_link_needed_list **pn;
3785 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3786 ;
3787 *pn = rpath;
3788 }
3789
3790 /* We do not want to include any of the sections in a dynamic
3791 object in the output file. We hack by simply clobbering the
3792 list of sections in the BFD. This could be handled more
3793 cleanly by, say, a new section flag; the existing
3794 SEC_NEVER_LOAD flag is not the one we want, because that one
3795 still implies that the section takes up space in the output
3796 file. */
3797 bfd_section_list_clear (abfd);
3798
3799 /* Find the name to use in a DT_NEEDED entry that refers to this
3800 object. If the object has a DT_SONAME entry, we use it.
3801 Otherwise, if the generic linker stuck something in
3802 elf_dt_name, we use that. Otherwise, we just use the file
3803 name. */
3804 if (soname == NULL || *soname == '\0')
3805 {
3806 soname = elf_dt_name (abfd);
3807 if (soname == NULL || *soname == '\0')
3808 soname = bfd_get_filename (abfd);
3809 }
3810
3811 /* Save the SONAME because sometimes the linker emulation code
3812 will need to know it. */
3813 elf_dt_name (abfd) = soname;
3814
3815 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3816 if (ret < 0)
3817 goto error_return;
3818
3819 /* If we have already included this dynamic object in the
3820 link, just ignore it. There is no reason to include a
3821 particular dynamic object more than once. */
3822 if (ret > 0)
3823 return TRUE;
3824
3825 /* Save the DT_AUDIT entry for the linker emulation code. */
3826 elf_dt_audit (abfd) = audit;
3827 }
3828
3829 /* If this is a dynamic object, we always link against the .dynsym
3830 symbol table, not the .symtab symbol table. The dynamic linker
3831 will only see the .dynsym symbol table, so there is no reason to
3832 look at .symtab for a dynamic object. */
3833
3834 if (! dynamic || elf_dynsymtab (abfd) == 0)
3835 hdr = &elf_tdata (abfd)->symtab_hdr;
3836 else
3837 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3838
3839 symcount = hdr->sh_size / bed->s->sizeof_sym;
3840
3841 /* The sh_info field of the symtab header tells us where the
3842 external symbols start. We don't care about the local symbols at
3843 this point. */
3844 if (elf_bad_symtab (abfd))
3845 {
3846 extsymcount = symcount;
3847 extsymoff = 0;
3848 }
3849 else
3850 {
3851 extsymcount = symcount - hdr->sh_info;
3852 extsymoff = hdr->sh_info;
3853 }
3854
3855 sym_hash = elf_sym_hashes (abfd);
3856 if (extsymcount != 0)
3857 {
3858 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3859 NULL, NULL, NULL);
3860 if (isymbuf == NULL)
3861 goto error_return;
3862
3863 if (sym_hash == NULL)
3864 {
3865 /* We store a pointer to the hash table entry for each
3866 external symbol. */
3867 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3868 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
3869 if (sym_hash == NULL)
3870 goto error_free_sym;
3871 elf_sym_hashes (abfd) = sym_hash;
3872 }
3873 }
3874
3875 if (dynamic)
3876 {
3877 /* Read in any version definitions. */
3878 if (!_bfd_elf_slurp_version_tables (abfd,
3879 info->default_imported_symver))
3880 goto error_free_sym;
3881
3882 /* Read in the symbol versions, but don't bother to convert them
3883 to internal format. */
3884 if (elf_dynversym (abfd) != 0)
3885 {
3886 Elf_Internal_Shdr *versymhdr;
3887
3888 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3889 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
3890 if (extversym == NULL)
3891 goto error_free_sym;
3892 amt = versymhdr->sh_size;
3893 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3894 || bfd_bread (extversym, amt, abfd) != amt)
3895 goto error_free_vers;
3896 }
3897 }
3898
3899 /* If we are loading an as-needed shared lib, save the symbol table
3900 state before we start adding symbols. If the lib turns out
3901 to be unneeded, restore the state. */
3902 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3903 {
3904 unsigned int i;
3905 size_t entsize;
3906
3907 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3908 {
3909 struct bfd_hash_entry *p;
3910 struct elf_link_hash_entry *h;
3911
3912 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3913 {
3914 h = (struct elf_link_hash_entry *) p;
3915 entsize += htab->root.table.entsize;
3916 if (h->root.type == bfd_link_hash_warning)
3917 entsize += htab->root.table.entsize;
3918 }
3919 }
3920
3921 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
3922 old_tab = bfd_malloc (tabsize + entsize);
3923 if (old_tab == NULL)
3924 goto error_free_vers;
3925
3926 /* Remember the current objalloc pointer, so that all mem for
3927 symbols added can later be reclaimed. */
3928 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3929 if (alloc_mark == NULL)
3930 goto error_free_vers;
3931
3932 /* Make a special call to the linker "notice" function to
3933 tell it that we are about to handle an as-needed lib. */
3934 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
3935 goto error_free_vers;
3936
3937 /* Clone the symbol table. Remember some pointers into the
3938 symbol table, and dynamic symbol count. */
3939 old_ent = (char *) old_tab + tabsize;
3940 memcpy (old_tab, htab->root.table.table, tabsize);
3941 old_undefs = htab->root.undefs;
3942 old_undefs_tail = htab->root.undefs_tail;
3943 old_table = htab->root.table.table;
3944 old_size = htab->root.table.size;
3945 old_count = htab->root.table.count;
3946 old_dynsymcount = htab->dynsymcount;
3947 old_dynstr_size = _bfd_elf_strtab_size (htab->dynstr);
3948
3949 for (i = 0; i < htab->root.table.size; i++)
3950 {
3951 struct bfd_hash_entry *p;
3952 struct elf_link_hash_entry *h;
3953
3954 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3955 {
3956 memcpy (old_ent, p, htab->root.table.entsize);
3957 old_ent = (char *) old_ent + htab->root.table.entsize;
3958 h = (struct elf_link_hash_entry *) p;
3959 if (h->root.type == bfd_link_hash_warning)
3960 {
3961 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3962 old_ent = (char *) old_ent + htab->root.table.entsize;
3963 }
3964 }
3965 }
3966 }
3967
3968 weaks = NULL;
3969 ever = extversym != NULL ? extversym + extsymoff : NULL;
3970 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3971 isym < isymend;
3972 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3973 {
3974 int bind;
3975 bfd_vma value;
3976 asection *sec, *new_sec;
3977 flagword flags;
3978 const char *name;
3979 struct elf_link_hash_entry *h;
3980 struct elf_link_hash_entry *hi;
3981 bfd_boolean definition;
3982 bfd_boolean size_change_ok;
3983 bfd_boolean type_change_ok;
3984 bfd_boolean new_weakdef;
3985 bfd_boolean new_weak;
3986 bfd_boolean old_weak;
3987 bfd_boolean override;
3988 bfd_boolean common;
3989 unsigned int old_alignment;
3990 bfd *old_bfd;
3991 bfd_boolean matched;
3992
3993 override = FALSE;
3994
3995 flags = BSF_NO_FLAGS;
3996 sec = NULL;
3997 value = isym->st_value;
3998 common = bed->common_definition (isym);
3999
4000 bind = ELF_ST_BIND (isym->st_info);
4001 switch (bind)
4002 {
4003 case STB_LOCAL:
4004 /* This should be impossible, since ELF requires that all
4005 global symbols follow all local symbols, and that sh_info
4006 point to the first global symbol. Unfortunately, Irix 5
4007 screws this up. */
4008 continue;
4009
4010 case STB_GLOBAL:
4011 if (isym->st_shndx != SHN_UNDEF && !common)
4012 flags = BSF_GLOBAL;
4013 break;
4014
4015 case STB_WEAK:
4016 flags = BSF_WEAK;
4017 break;
4018
4019 case STB_GNU_UNIQUE:
4020 flags = BSF_GNU_UNIQUE;
4021 break;
4022
4023 default:
4024 /* Leave it up to the processor backend. */
4025 break;
4026 }
4027
4028 if (isym->st_shndx == SHN_UNDEF)
4029 sec = bfd_und_section_ptr;
4030 else if (isym->st_shndx == SHN_ABS)
4031 sec = bfd_abs_section_ptr;
4032 else if (isym->st_shndx == SHN_COMMON)
4033 {
4034 sec = bfd_com_section_ptr;
4035 /* What ELF calls the size we call the value. What ELF
4036 calls the value we call the alignment. */
4037 value = isym->st_size;
4038 }
4039 else
4040 {
4041 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4042 if (sec == NULL)
4043 sec = bfd_abs_section_ptr;
4044 else if (discarded_section (sec))
4045 {
4046 /* Symbols from discarded section are undefined. We keep
4047 its visibility. */
4048 sec = bfd_und_section_ptr;
4049 isym->st_shndx = SHN_UNDEF;
4050 }
4051 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4052 value -= sec->vma;
4053 }
4054
4055 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4056 isym->st_name);
4057 if (name == NULL)
4058 goto error_free_vers;
4059
4060 if (isym->st_shndx == SHN_COMMON
4061 && (abfd->flags & BFD_PLUGIN) != 0)
4062 {
4063 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4064
4065 if (xc == NULL)
4066 {
4067 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4068 | SEC_EXCLUDE);
4069 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4070 if (xc == NULL)
4071 goto error_free_vers;
4072 }
4073 sec = xc;
4074 }
4075 else if (isym->st_shndx == SHN_COMMON
4076 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4077 && !bfd_link_relocatable (info))
4078 {
4079 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4080
4081 if (tcomm == NULL)
4082 {
4083 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4084 | SEC_LINKER_CREATED);
4085 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4086 if (tcomm == NULL)
4087 goto error_free_vers;
4088 }
4089 sec = tcomm;
4090 }
4091 else if (bed->elf_add_symbol_hook)
4092 {
4093 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4094 &sec, &value))
4095 goto error_free_vers;
4096
4097 /* The hook function sets the name to NULL if this symbol
4098 should be skipped for some reason. */
4099 if (name == NULL)
4100 continue;
4101 }
4102
4103 /* Sanity check that all possibilities were handled. */
4104 if (sec == NULL)
4105 {
4106 bfd_set_error (bfd_error_bad_value);
4107 goto error_free_vers;
4108 }
4109
4110 /* Silently discard TLS symbols from --just-syms. There's
4111 no way to combine a static TLS block with a new TLS block
4112 for this executable. */
4113 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4114 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4115 continue;
4116
4117 if (bfd_is_und_section (sec)
4118 || bfd_is_com_section (sec))
4119 definition = FALSE;
4120 else
4121 definition = TRUE;
4122
4123 size_change_ok = FALSE;
4124 type_change_ok = bed->type_change_ok;
4125 old_weak = FALSE;
4126 matched = FALSE;
4127 old_alignment = 0;
4128 old_bfd = NULL;
4129 new_sec = sec;
4130
4131 if (is_elf_hash_table (htab))
4132 {
4133 Elf_Internal_Versym iver;
4134 unsigned int vernum = 0;
4135 bfd_boolean skip;
4136
4137 if (ever == NULL)
4138 {
4139 if (info->default_imported_symver)
4140 /* Use the default symbol version created earlier. */
4141 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4142 else
4143 iver.vs_vers = 0;
4144 }
4145 else
4146 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4147
4148 vernum = iver.vs_vers & VERSYM_VERSION;
4149
4150 /* If this is a hidden symbol, or if it is not version
4151 1, we append the version name to the symbol name.
4152 However, we do not modify a non-hidden absolute symbol
4153 if it is not a function, because it might be the version
4154 symbol itself. FIXME: What if it isn't? */
4155 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4156 || (vernum > 1
4157 && (!bfd_is_abs_section (sec)
4158 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4159 {
4160 const char *verstr;
4161 size_t namelen, verlen, newlen;
4162 char *newname, *p;
4163
4164 if (isym->st_shndx != SHN_UNDEF)
4165 {
4166 if (vernum > elf_tdata (abfd)->cverdefs)
4167 verstr = NULL;
4168 else if (vernum > 1)
4169 verstr =
4170 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4171 else
4172 verstr = "";
4173
4174 if (verstr == NULL)
4175 {
4176 (*_bfd_error_handler)
4177 (_("%B: %s: invalid version %u (max %d)"),
4178 abfd, name, vernum,
4179 elf_tdata (abfd)->cverdefs);
4180 bfd_set_error (bfd_error_bad_value);
4181 goto error_free_vers;
4182 }
4183 }
4184 else
4185 {
4186 /* We cannot simply test for the number of
4187 entries in the VERNEED section since the
4188 numbers for the needed versions do not start
4189 at 0. */
4190 Elf_Internal_Verneed *t;
4191
4192 verstr = NULL;
4193 for (t = elf_tdata (abfd)->verref;
4194 t != NULL;
4195 t = t->vn_nextref)
4196 {
4197 Elf_Internal_Vernaux *a;
4198
4199 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4200 {
4201 if (a->vna_other == vernum)
4202 {
4203 verstr = a->vna_nodename;
4204 break;
4205 }
4206 }
4207 if (a != NULL)
4208 break;
4209 }
4210 if (verstr == NULL)
4211 {
4212 (*_bfd_error_handler)
4213 (_("%B: %s: invalid needed version %d"),
4214 abfd, name, vernum);
4215 bfd_set_error (bfd_error_bad_value);
4216 goto error_free_vers;
4217 }
4218 }
4219
4220 namelen = strlen (name);
4221 verlen = strlen (verstr);
4222 newlen = namelen + verlen + 2;
4223 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4224 && isym->st_shndx != SHN_UNDEF)
4225 ++newlen;
4226
4227 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4228 if (newname == NULL)
4229 goto error_free_vers;
4230 memcpy (newname, name, namelen);
4231 p = newname + namelen;
4232 *p++ = ELF_VER_CHR;
4233 /* If this is a defined non-hidden version symbol,
4234 we add another @ to the name. This indicates the
4235 default version of the symbol. */
4236 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4237 && isym->st_shndx != SHN_UNDEF)
4238 *p++ = ELF_VER_CHR;
4239 memcpy (p, verstr, verlen + 1);
4240
4241 name = newname;
4242 }
4243
4244 /* If this symbol has default visibility and the user has
4245 requested we not re-export it, then mark it as hidden. */
4246 if (!bfd_is_und_section (sec)
4247 && !dynamic
4248 && abfd->no_export
4249 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4250 isym->st_other = (STV_HIDDEN
4251 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4252
4253 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4254 sym_hash, &old_bfd, &old_weak,
4255 &old_alignment, &skip, &override,
4256 &type_change_ok, &size_change_ok,
4257 &matched))
4258 goto error_free_vers;
4259
4260 if (skip)
4261 continue;
4262
4263 /* Override a definition only if the new symbol matches the
4264 existing one. */
4265 if (override && matched)
4266 definition = FALSE;
4267
4268 h = *sym_hash;
4269 while (h->root.type == bfd_link_hash_indirect
4270 || h->root.type == bfd_link_hash_warning)
4271 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4272
4273 if (elf_tdata (abfd)->verdef != NULL
4274 && vernum > 1
4275 && definition)
4276 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4277 }
4278
4279 if (! (_bfd_generic_link_add_one_symbol
4280 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4281 (struct bfd_link_hash_entry **) sym_hash)))
4282 goto error_free_vers;
4283
4284 h = *sym_hash;
4285 /* We need to make sure that indirect symbol dynamic flags are
4286 updated. */
4287 hi = h;
4288 while (h->root.type == bfd_link_hash_indirect
4289 || h->root.type == bfd_link_hash_warning)
4290 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4291
4292 *sym_hash = h;
4293
4294 new_weak = (flags & BSF_WEAK) != 0;
4295 new_weakdef = FALSE;
4296 if (dynamic
4297 && definition
4298 && new_weak
4299 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4300 && is_elf_hash_table (htab)
4301 && h->u.weakdef == NULL)
4302 {
4303 /* Keep a list of all weak defined non function symbols from
4304 a dynamic object, using the weakdef field. Later in this
4305 function we will set the weakdef field to the correct
4306 value. We only put non-function symbols from dynamic
4307 objects on this list, because that happens to be the only
4308 time we need to know the normal symbol corresponding to a
4309 weak symbol, and the information is time consuming to
4310 figure out. If the weakdef field is not already NULL,
4311 then this symbol was already defined by some previous
4312 dynamic object, and we will be using that previous
4313 definition anyhow. */
4314
4315 h->u.weakdef = weaks;
4316 weaks = h;
4317 new_weakdef = TRUE;
4318 }
4319
4320 /* Set the alignment of a common symbol. */
4321 if ((common || bfd_is_com_section (sec))
4322 && h->root.type == bfd_link_hash_common)
4323 {
4324 unsigned int align;
4325
4326 if (common)
4327 align = bfd_log2 (isym->st_value);
4328 else
4329 {
4330 /* The new symbol is a common symbol in a shared object.
4331 We need to get the alignment from the section. */
4332 align = new_sec->alignment_power;
4333 }
4334 if (align > old_alignment)
4335 h->root.u.c.p->alignment_power = align;
4336 else
4337 h->root.u.c.p->alignment_power = old_alignment;
4338 }
4339
4340 if (is_elf_hash_table (htab))
4341 {
4342 /* Set a flag in the hash table entry indicating the type of
4343 reference or definition we just found. A dynamic symbol
4344 is one which is referenced or defined by both a regular
4345 object and a shared object. */
4346 bfd_boolean dynsym = FALSE;
4347
4348 /* Plugin symbols aren't normal. Don't set def_regular or
4349 ref_regular for them, or make them dynamic. */
4350 if ((abfd->flags & BFD_PLUGIN) != 0)
4351 ;
4352 else if (! dynamic)
4353 {
4354 if (! definition)
4355 {
4356 h->ref_regular = 1;
4357 if (bind != STB_WEAK)
4358 h->ref_regular_nonweak = 1;
4359 }
4360 else
4361 {
4362 h->def_regular = 1;
4363 if (h->def_dynamic)
4364 {
4365 h->def_dynamic = 0;
4366 h->ref_dynamic = 1;
4367 }
4368 }
4369
4370 /* If the indirect symbol has been forced local, don't
4371 make the real symbol dynamic. */
4372 if ((h == hi || !hi->forced_local)
4373 && (bfd_link_dll (info)
4374 || h->def_dynamic
4375 || h->ref_dynamic))
4376 dynsym = TRUE;
4377 }
4378 else
4379 {
4380 if (! definition)
4381 {
4382 h->ref_dynamic = 1;
4383 hi->ref_dynamic = 1;
4384 }
4385 else
4386 {
4387 h->def_dynamic = 1;
4388 hi->def_dynamic = 1;
4389 }
4390
4391 /* If the indirect symbol has been forced local, don't
4392 make the real symbol dynamic. */
4393 if ((h == hi || !hi->forced_local)
4394 && (h->def_regular
4395 || h->ref_regular
4396 || (h->u.weakdef != NULL
4397 && ! new_weakdef
4398 && h->u.weakdef->dynindx != -1)))
4399 dynsym = TRUE;
4400 }
4401
4402 /* Check to see if we need to add an indirect symbol for
4403 the default name. */
4404 if (definition
4405 || (!override && h->root.type == bfd_link_hash_common))
4406 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4407 sec, value, &old_bfd, &dynsym))
4408 goto error_free_vers;
4409
4410 /* Check the alignment when a common symbol is involved. This
4411 can change when a common symbol is overridden by a normal
4412 definition or a common symbol is ignored due to the old
4413 normal definition. We need to make sure the maximum
4414 alignment is maintained. */
4415 if ((old_alignment || common)
4416 && h->root.type != bfd_link_hash_common)
4417 {
4418 unsigned int common_align;
4419 unsigned int normal_align;
4420 unsigned int symbol_align;
4421 bfd *normal_bfd;
4422 bfd *common_bfd;
4423
4424 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4425 || h->root.type == bfd_link_hash_defweak);
4426
4427 symbol_align = ffs (h->root.u.def.value) - 1;
4428 if (h->root.u.def.section->owner != NULL
4429 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4430 {
4431 normal_align = h->root.u.def.section->alignment_power;
4432 if (normal_align > symbol_align)
4433 normal_align = symbol_align;
4434 }
4435 else
4436 normal_align = symbol_align;
4437
4438 if (old_alignment)
4439 {
4440 common_align = old_alignment;
4441 common_bfd = old_bfd;
4442 normal_bfd = abfd;
4443 }
4444 else
4445 {
4446 common_align = bfd_log2 (isym->st_value);
4447 common_bfd = abfd;
4448 normal_bfd = old_bfd;
4449 }
4450
4451 if (normal_align < common_align)
4452 {
4453 /* PR binutils/2735 */
4454 if (normal_bfd == NULL)
4455 (*_bfd_error_handler)
4456 (_("Warning: alignment %u of common symbol `%s' in %B is"
4457 " greater than the alignment (%u) of its section %A"),
4458 common_bfd, h->root.u.def.section,
4459 1 << common_align, name, 1 << normal_align);
4460 else
4461 (*_bfd_error_handler)
4462 (_("Warning: alignment %u of symbol `%s' in %B"
4463 " is smaller than %u in %B"),
4464 normal_bfd, common_bfd,
4465 1 << normal_align, name, 1 << common_align);
4466 }
4467 }
4468
4469 /* Remember the symbol size if it isn't undefined. */
4470 if (isym->st_size != 0
4471 && isym->st_shndx != SHN_UNDEF
4472 && (definition || h->size == 0))
4473 {
4474 if (h->size != 0
4475 && h->size != isym->st_size
4476 && ! size_change_ok)
4477 (*_bfd_error_handler)
4478 (_("Warning: size of symbol `%s' changed"
4479 " from %lu in %B to %lu in %B"),
4480 old_bfd, abfd,
4481 name, (unsigned long) h->size,
4482 (unsigned long) isym->st_size);
4483
4484 h->size = isym->st_size;
4485 }
4486
4487 /* If this is a common symbol, then we always want H->SIZE
4488 to be the size of the common symbol. The code just above
4489 won't fix the size if a common symbol becomes larger. We
4490 don't warn about a size change here, because that is
4491 covered by --warn-common. Allow changes between different
4492 function types. */
4493 if (h->root.type == bfd_link_hash_common)
4494 h->size = h->root.u.c.size;
4495
4496 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4497 && ((definition && !new_weak)
4498 || (old_weak && h->root.type == bfd_link_hash_common)
4499 || h->type == STT_NOTYPE))
4500 {
4501 unsigned int type = ELF_ST_TYPE (isym->st_info);
4502
4503 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4504 symbol. */
4505 if (type == STT_GNU_IFUNC
4506 && (abfd->flags & DYNAMIC) != 0)
4507 type = STT_FUNC;
4508
4509 if (h->type != type)
4510 {
4511 if (h->type != STT_NOTYPE && ! type_change_ok)
4512 (*_bfd_error_handler)
4513 (_("Warning: type of symbol `%s' changed"
4514 " from %d to %d in %B"),
4515 abfd, name, h->type, type);
4516
4517 h->type = type;
4518 }
4519 }
4520
4521 /* Merge st_other field. */
4522 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4523
4524 /* We don't want to make debug symbol dynamic. */
4525 if (definition
4526 && (sec->flags & SEC_DEBUGGING)
4527 && !bfd_link_relocatable (info))
4528 dynsym = FALSE;
4529
4530 /* Nor should we make plugin symbols dynamic. */
4531 if ((abfd->flags & BFD_PLUGIN) != 0)
4532 dynsym = FALSE;
4533
4534 if (definition)
4535 {
4536 h->target_internal = isym->st_target_internal;
4537 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4538 }
4539
4540 if (definition && !dynamic)
4541 {
4542 char *p = strchr (name, ELF_VER_CHR);
4543 if (p != NULL && p[1] != ELF_VER_CHR)
4544 {
4545 /* Queue non-default versions so that .symver x, x@FOO
4546 aliases can be checked. */
4547 if (!nondeflt_vers)
4548 {
4549 amt = ((isymend - isym + 1)
4550 * sizeof (struct elf_link_hash_entry *));
4551 nondeflt_vers
4552 = (struct elf_link_hash_entry **) bfd_malloc (amt);
4553 if (!nondeflt_vers)
4554 goto error_free_vers;
4555 }
4556 nondeflt_vers[nondeflt_vers_cnt++] = h;
4557 }
4558 }
4559
4560 if (dynsym && h->dynindx == -1)
4561 {
4562 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4563 goto error_free_vers;
4564 if (h->u.weakdef != NULL
4565 && ! new_weakdef
4566 && h->u.weakdef->dynindx == -1)
4567 {
4568 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4569 goto error_free_vers;
4570 }
4571 }
4572 else if (dynsym && h->dynindx != -1)
4573 /* If the symbol already has a dynamic index, but
4574 visibility says it should not be visible, turn it into
4575 a local symbol. */
4576 switch (ELF_ST_VISIBILITY (h->other))
4577 {
4578 case STV_INTERNAL:
4579 case STV_HIDDEN:
4580 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4581 dynsym = FALSE;
4582 break;
4583 }
4584
4585 /* Don't add DT_NEEDED for references from the dummy bfd nor
4586 for unmatched symbol. */
4587 if (!add_needed
4588 && matched
4589 && definition
4590 && ((dynsym
4591 && h->ref_regular_nonweak
4592 && (old_bfd == NULL
4593 || (old_bfd->flags & BFD_PLUGIN) == 0))
4594 || (h->ref_dynamic_nonweak
4595 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4596 && !on_needed_list (elf_dt_name (abfd), htab->needed))))
4597 {
4598 int ret;
4599 const char *soname = elf_dt_name (abfd);
4600
4601 info->callbacks->minfo ("%!", soname, old_bfd,
4602 h->root.root.string);
4603
4604 /* A symbol from a library loaded via DT_NEEDED of some
4605 other library is referenced by a regular object.
4606 Add a DT_NEEDED entry for it. Issue an error if
4607 --no-add-needed is used and the reference was not
4608 a weak one. */
4609 if (old_bfd != NULL
4610 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4611 {
4612 (*_bfd_error_handler)
4613 (_("%B: undefined reference to symbol '%s'"),
4614 old_bfd, name);
4615 bfd_set_error (bfd_error_missing_dso);
4616 goto error_free_vers;
4617 }
4618
4619 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4620 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
4621
4622 add_needed = TRUE;
4623 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4624 if (ret < 0)
4625 goto error_free_vers;
4626
4627 BFD_ASSERT (ret == 0);
4628 }
4629 }
4630 }
4631
4632 if (extversym != NULL)
4633 {
4634 free (extversym);
4635 extversym = NULL;
4636 }
4637
4638 if (isymbuf != NULL)
4639 {
4640 free (isymbuf);
4641 isymbuf = NULL;
4642 }
4643
4644 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4645 {
4646 unsigned int i;
4647
4648 /* Restore the symbol table. */
4649 old_ent = (char *) old_tab + tabsize;
4650 memset (elf_sym_hashes (abfd), 0,
4651 extsymcount * sizeof (struct elf_link_hash_entry *));
4652 htab->root.table.table = old_table;
4653 htab->root.table.size = old_size;
4654 htab->root.table.count = old_count;
4655 memcpy (htab->root.table.table, old_tab, tabsize);
4656 htab->root.undefs = old_undefs;
4657 htab->root.undefs_tail = old_undefs_tail;
4658 _bfd_elf_strtab_restore_size (htab->dynstr, old_dynstr_size);
4659 for (i = 0; i < htab->root.table.size; i++)
4660 {
4661 struct bfd_hash_entry *p;
4662 struct elf_link_hash_entry *h;
4663 bfd_size_type size;
4664 unsigned int alignment_power;
4665
4666 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4667 {
4668 h = (struct elf_link_hash_entry *) p;
4669 if (h->root.type == bfd_link_hash_warning)
4670 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4671 if (h->dynindx >= old_dynsymcount
4672 && h->dynstr_index < old_dynstr_size)
4673 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
4674
4675 /* Preserve the maximum alignment and size for common
4676 symbols even if this dynamic lib isn't on DT_NEEDED
4677 since it can still be loaded at run time by another
4678 dynamic lib. */
4679 if (h->root.type == bfd_link_hash_common)
4680 {
4681 size = h->root.u.c.size;
4682 alignment_power = h->root.u.c.p->alignment_power;
4683 }
4684 else
4685 {
4686 size = 0;
4687 alignment_power = 0;
4688 }
4689 memcpy (p, old_ent, htab->root.table.entsize);
4690 old_ent = (char *) old_ent + htab->root.table.entsize;
4691 h = (struct elf_link_hash_entry *) p;
4692 if (h->root.type == bfd_link_hash_warning)
4693 {
4694 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4695 old_ent = (char *) old_ent + htab->root.table.entsize;
4696 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4697 }
4698 if (h->root.type == bfd_link_hash_common)
4699 {
4700 if (size > h->root.u.c.size)
4701 h->root.u.c.size = size;
4702 if (alignment_power > h->root.u.c.p->alignment_power)
4703 h->root.u.c.p->alignment_power = alignment_power;
4704 }
4705 }
4706 }
4707
4708 /* Make a special call to the linker "notice" function to
4709 tell it that symbols added for crefs may need to be removed. */
4710 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
4711 goto error_free_vers;
4712
4713 free (old_tab);
4714 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4715 alloc_mark);
4716 if (nondeflt_vers != NULL)
4717 free (nondeflt_vers);
4718 return TRUE;
4719 }
4720
4721 if (old_tab != NULL)
4722 {
4723 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
4724 goto error_free_vers;
4725 free (old_tab);
4726 old_tab = NULL;
4727 }
4728
4729 /* Now that all the symbols from this input file are created, if
4730 not performing a relocatable link, handle .symver foo, foo@BAR
4731 such that any relocs against foo become foo@BAR. */
4732 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4733 {
4734 bfd_size_type cnt, symidx;
4735
4736 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4737 {
4738 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4739 char *shortname, *p;
4740
4741 p = strchr (h->root.root.string, ELF_VER_CHR);
4742 if (p == NULL
4743 || (h->root.type != bfd_link_hash_defined
4744 && h->root.type != bfd_link_hash_defweak))
4745 continue;
4746
4747 amt = p - h->root.root.string;
4748 shortname = (char *) bfd_malloc (amt + 1);
4749 if (!shortname)
4750 goto error_free_vers;
4751 memcpy (shortname, h->root.root.string, amt);
4752 shortname[amt] = '\0';
4753
4754 hi = (struct elf_link_hash_entry *)
4755 bfd_link_hash_lookup (&htab->root, shortname,
4756 FALSE, FALSE, FALSE);
4757 if (hi != NULL
4758 && hi->root.type == h->root.type
4759 && hi->root.u.def.value == h->root.u.def.value
4760 && hi->root.u.def.section == h->root.u.def.section)
4761 {
4762 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4763 hi->root.type = bfd_link_hash_indirect;
4764 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4765 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4766 sym_hash = elf_sym_hashes (abfd);
4767 if (sym_hash)
4768 for (symidx = 0; symidx < extsymcount; ++symidx)
4769 if (sym_hash[symidx] == hi)
4770 {
4771 sym_hash[symidx] = h;
4772 break;
4773 }
4774 }
4775 free (shortname);
4776 }
4777 free (nondeflt_vers);
4778 nondeflt_vers = NULL;
4779 }
4780
4781 /* Now set the weakdefs field correctly for all the weak defined
4782 symbols we found. The only way to do this is to search all the
4783 symbols. Since we only need the information for non functions in
4784 dynamic objects, that's the only time we actually put anything on
4785 the list WEAKS. We need this information so that if a regular
4786 object refers to a symbol defined weakly in a dynamic object, the
4787 real symbol in the dynamic object is also put in the dynamic
4788 symbols; we also must arrange for both symbols to point to the
4789 same memory location. We could handle the general case of symbol
4790 aliasing, but a general symbol alias can only be generated in
4791 assembler code, handling it correctly would be very time
4792 consuming, and other ELF linkers don't handle general aliasing
4793 either. */
4794 if (weaks != NULL)
4795 {
4796 struct elf_link_hash_entry **hpp;
4797 struct elf_link_hash_entry **hppend;
4798 struct elf_link_hash_entry **sorted_sym_hash;
4799 struct elf_link_hash_entry *h;
4800 size_t sym_count;
4801
4802 /* Since we have to search the whole symbol list for each weak
4803 defined symbol, search time for N weak defined symbols will be
4804 O(N^2). Binary search will cut it down to O(NlogN). */
4805 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4806 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4807 if (sorted_sym_hash == NULL)
4808 goto error_return;
4809 sym_hash = sorted_sym_hash;
4810 hpp = elf_sym_hashes (abfd);
4811 hppend = hpp + extsymcount;
4812 sym_count = 0;
4813 for (; hpp < hppend; hpp++)
4814 {
4815 h = *hpp;
4816 if (h != NULL
4817 && h->root.type == bfd_link_hash_defined
4818 && !bed->is_function_type (h->type))
4819 {
4820 *sym_hash = h;
4821 sym_hash++;
4822 sym_count++;
4823 }
4824 }
4825
4826 qsort (sorted_sym_hash, sym_count,
4827 sizeof (struct elf_link_hash_entry *),
4828 elf_sort_symbol);
4829
4830 while (weaks != NULL)
4831 {
4832 struct elf_link_hash_entry *hlook;
4833 asection *slook;
4834 bfd_vma vlook;
4835 size_t i, j, idx = 0;
4836
4837 hlook = weaks;
4838 weaks = hlook->u.weakdef;
4839 hlook->u.weakdef = NULL;
4840
4841 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4842 || hlook->root.type == bfd_link_hash_defweak
4843 || hlook->root.type == bfd_link_hash_common
4844 || hlook->root.type == bfd_link_hash_indirect);
4845 slook = hlook->root.u.def.section;
4846 vlook = hlook->root.u.def.value;
4847
4848 i = 0;
4849 j = sym_count;
4850 while (i != j)
4851 {
4852 bfd_signed_vma vdiff;
4853 idx = (i + j) / 2;
4854 h = sorted_sym_hash[idx];
4855 vdiff = vlook - h->root.u.def.value;
4856 if (vdiff < 0)
4857 j = idx;
4858 else if (vdiff > 0)
4859 i = idx + 1;
4860 else
4861 {
4862 int sdiff = slook->id - h->root.u.def.section->id;
4863 if (sdiff < 0)
4864 j = idx;
4865 else if (sdiff > 0)
4866 i = idx + 1;
4867 else
4868 break;
4869 }
4870 }
4871
4872 /* We didn't find a value/section match. */
4873 if (i == j)
4874 continue;
4875
4876 /* With multiple aliases, or when the weak symbol is already
4877 strongly defined, we have multiple matching symbols and
4878 the binary search above may land on any of them. Step
4879 one past the matching symbol(s). */
4880 while (++idx != j)
4881 {
4882 h = sorted_sym_hash[idx];
4883 if (h->root.u.def.section != slook
4884 || h->root.u.def.value != vlook)
4885 break;
4886 }
4887
4888 /* Now look back over the aliases. Since we sorted by size
4889 as well as value and section, we'll choose the one with
4890 the largest size. */
4891 while (idx-- != i)
4892 {
4893 h = sorted_sym_hash[idx];
4894
4895 /* Stop if value or section doesn't match. */
4896 if (h->root.u.def.section != slook
4897 || h->root.u.def.value != vlook)
4898 break;
4899 else if (h != hlook)
4900 {
4901 hlook->u.weakdef = h;
4902
4903 /* If the weak definition is in the list of dynamic
4904 symbols, make sure the real definition is put
4905 there as well. */
4906 if (hlook->dynindx != -1 && h->dynindx == -1)
4907 {
4908 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4909 {
4910 err_free_sym_hash:
4911 free (sorted_sym_hash);
4912 goto error_return;
4913 }
4914 }
4915
4916 /* If the real definition is in the list of dynamic
4917 symbols, make sure the weak definition is put
4918 there as well. If we don't do this, then the
4919 dynamic loader might not merge the entries for the
4920 real definition and the weak definition. */
4921 if (h->dynindx != -1 && hlook->dynindx == -1)
4922 {
4923 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4924 goto err_free_sym_hash;
4925 }
4926 break;
4927 }
4928 }
4929 }
4930
4931 free (sorted_sym_hash);
4932 }
4933
4934 if (bed->check_directives
4935 && !(*bed->check_directives) (abfd, info))
4936 return FALSE;
4937
4938 /* If this object is the same format as the output object, and it is
4939 not a shared library, then let the backend look through the
4940 relocs.
4941
4942 This is required to build global offset table entries and to
4943 arrange for dynamic relocs. It is not required for the
4944 particular common case of linking non PIC code, even when linking
4945 against shared libraries, but unfortunately there is no way of
4946 knowing whether an object file has been compiled PIC or not.
4947 Looking through the relocs is not particularly time consuming.
4948 The problem is that we must either (1) keep the relocs in memory,
4949 which causes the linker to require additional runtime memory or
4950 (2) read the relocs twice from the input file, which wastes time.
4951 This would be a good case for using mmap.
4952
4953 I have no idea how to handle linking PIC code into a file of a
4954 different format. It probably can't be done. */
4955 if (! dynamic
4956 && is_elf_hash_table (htab)
4957 && bed->check_relocs != NULL
4958 && elf_object_id (abfd) == elf_hash_table_id (htab)
4959 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4960 {
4961 asection *o;
4962
4963 for (o = abfd->sections; o != NULL; o = o->next)
4964 {
4965 Elf_Internal_Rela *internal_relocs;
4966 bfd_boolean ok;
4967
4968 if ((o->flags & SEC_RELOC) == 0
4969 || o->reloc_count == 0
4970 || ((info->strip == strip_all || info->strip == strip_debugger)
4971 && (o->flags & SEC_DEBUGGING) != 0)
4972 || bfd_is_abs_section (o->output_section))
4973 continue;
4974
4975 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4976 info->keep_memory);
4977 if (internal_relocs == NULL)
4978 goto error_return;
4979
4980 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4981
4982 if (elf_section_data (o)->relocs != internal_relocs)
4983 free (internal_relocs);
4984
4985 if (! ok)
4986 goto error_return;
4987 }
4988 }
4989
4990 /* If this is a non-traditional link, try to optimize the handling
4991 of the .stab/.stabstr sections. */
4992 if (! dynamic
4993 && ! info->traditional_format
4994 && is_elf_hash_table (htab)
4995 && (info->strip != strip_all && info->strip != strip_debugger))
4996 {
4997 asection *stabstr;
4998
4999 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5000 if (stabstr != NULL)
5001 {
5002 bfd_size_type string_offset = 0;
5003 asection *stab;
5004
5005 for (stab = abfd->sections; stab; stab = stab->next)
5006 if (CONST_STRNEQ (stab->name, ".stab")
5007 && (!stab->name[5] ||
5008 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5009 && (stab->flags & SEC_MERGE) == 0
5010 && !bfd_is_abs_section (stab->output_section))
5011 {
5012 struct bfd_elf_section_data *secdata;
5013
5014 secdata = elf_section_data (stab);
5015 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5016 stabstr, &secdata->sec_info,
5017 &string_offset))
5018 goto error_return;
5019 if (secdata->sec_info)
5020 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5021 }
5022 }
5023 }
5024
5025 if (is_elf_hash_table (htab) && add_needed)
5026 {
5027 /* Add this bfd to the loaded list. */
5028 struct elf_link_loaded_list *n;
5029
5030 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5031 if (n == NULL)
5032 goto error_return;
5033 n->abfd = abfd;
5034 n->next = htab->loaded;
5035 htab->loaded = n;
5036 }
5037
5038 return TRUE;
5039
5040 error_free_vers:
5041 if (old_tab != NULL)
5042 free (old_tab);
5043 if (nondeflt_vers != NULL)
5044 free (nondeflt_vers);
5045 if (extversym != NULL)
5046 free (extversym);
5047 error_free_sym:
5048 if (isymbuf != NULL)
5049 free (isymbuf);
5050 error_return:
5051 return FALSE;
5052 }
5053
5054 /* Return the linker hash table entry of a symbol that might be
5055 satisfied by an archive symbol. Return -1 on error. */
5056
5057 struct elf_link_hash_entry *
5058 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5059 struct bfd_link_info *info,
5060 const char *name)
5061 {
5062 struct elf_link_hash_entry *h;
5063 char *p, *copy;
5064 size_t len, first;
5065
5066 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5067 if (h != NULL)
5068 return h;
5069
5070 /* If this is a default version (the name contains @@), look up the
5071 symbol again with only one `@' as well as without the version.
5072 The effect is that references to the symbol with and without the
5073 version will be matched by the default symbol in the archive. */
5074
5075 p = strchr (name, ELF_VER_CHR);
5076 if (p == NULL || p[1] != ELF_VER_CHR)
5077 return h;
5078
5079 /* First check with only one `@'. */
5080 len = strlen (name);
5081 copy = (char *) bfd_alloc (abfd, len);
5082 if (copy == NULL)
5083 return (struct elf_link_hash_entry *) 0 - 1;
5084
5085 first = p - name + 1;
5086 memcpy (copy, name, first);
5087 memcpy (copy + first, name + first + 1, len - first);
5088
5089 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5090 if (h == NULL)
5091 {
5092 /* We also need to check references to the symbol without the
5093 version. */
5094 copy[first - 1] = '\0';
5095 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5096 FALSE, FALSE, TRUE);
5097 }
5098
5099 bfd_release (abfd, copy);
5100 return h;
5101 }
5102
5103 /* Add symbols from an ELF archive file to the linker hash table. We
5104 don't use _bfd_generic_link_add_archive_symbols because we need to
5105 handle versioned symbols.
5106
5107 Fortunately, ELF archive handling is simpler than that done by
5108 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5109 oddities. In ELF, if we find a symbol in the archive map, and the
5110 symbol is currently undefined, we know that we must pull in that
5111 object file.
5112
5113 Unfortunately, we do have to make multiple passes over the symbol
5114 table until nothing further is resolved. */
5115
5116 static bfd_boolean
5117 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5118 {
5119 symindex c;
5120 unsigned char *included = NULL;
5121 carsym *symdefs;
5122 bfd_boolean loop;
5123 bfd_size_type amt;
5124 const struct elf_backend_data *bed;
5125 struct elf_link_hash_entry * (*archive_symbol_lookup)
5126 (bfd *, struct bfd_link_info *, const char *);
5127
5128 if (! bfd_has_map (abfd))
5129 {
5130 /* An empty archive is a special case. */
5131 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5132 return TRUE;
5133 bfd_set_error (bfd_error_no_armap);
5134 return FALSE;
5135 }
5136
5137 /* Keep track of all symbols we know to be already defined, and all
5138 files we know to be already included. This is to speed up the
5139 second and subsequent passes. */
5140 c = bfd_ardata (abfd)->symdef_count;
5141 if (c == 0)
5142 return TRUE;
5143 amt = c;
5144 amt *= sizeof (*included);
5145 included = (unsigned char *) bfd_zmalloc (amt);
5146 if (included == NULL)
5147 return FALSE;
5148
5149 symdefs = bfd_ardata (abfd)->symdefs;
5150 bed = get_elf_backend_data (abfd);
5151 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5152
5153 do
5154 {
5155 file_ptr last;
5156 symindex i;
5157 carsym *symdef;
5158 carsym *symdefend;
5159
5160 loop = FALSE;
5161 last = -1;
5162
5163 symdef = symdefs;
5164 symdefend = symdef + c;
5165 for (i = 0; symdef < symdefend; symdef++, i++)
5166 {
5167 struct elf_link_hash_entry *h;
5168 bfd *element;
5169 struct bfd_link_hash_entry *undefs_tail;
5170 symindex mark;
5171
5172 if (included[i])
5173 continue;
5174 if (symdef->file_offset == last)
5175 {
5176 included[i] = TRUE;
5177 continue;
5178 }
5179
5180 h = archive_symbol_lookup (abfd, info, symdef->name);
5181 if (h == (struct elf_link_hash_entry *) 0 - 1)
5182 goto error_return;
5183
5184 if (h == NULL)
5185 continue;
5186
5187 if (h->root.type == bfd_link_hash_common)
5188 {
5189 /* We currently have a common symbol. The archive map contains
5190 a reference to this symbol, so we may want to include it. We
5191 only want to include it however, if this archive element
5192 contains a definition of the symbol, not just another common
5193 declaration of it.
5194
5195 Unfortunately some archivers (including GNU ar) will put
5196 declarations of common symbols into their archive maps, as
5197 well as real definitions, so we cannot just go by the archive
5198 map alone. Instead we must read in the element's symbol
5199 table and check that to see what kind of symbol definition
5200 this is. */
5201 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5202 continue;
5203 }
5204 else if (h->root.type != bfd_link_hash_undefined)
5205 {
5206 if (h->root.type != bfd_link_hash_undefweak)
5207 /* Symbol must be defined. Don't check it again. */
5208 included[i] = TRUE;
5209 continue;
5210 }
5211
5212 /* We need to include this archive member. */
5213 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5214 if (element == NULL)
5215 goto error_return;
5216
5217 if (! bfd_check_format (element, bfd_object))
5218 goto error_return;
5219
5220 undefs_tail = info->hash->undefs_tail;
5221
5222 if (!(*info->callbacks
5223 ->add_archive_element) (info, element, symdef->name, &element))
5224 goto error_return;
5225 if (!bfd_link_add_symbols (element, info))
5226 goto error_return;
5227
5228 /* If there are any new undefined symbols, we need to make
5229 another pass through the archive in order to see whether
5230 they can be defined. FIXME: This isn't perfect, because
5231 common symbols wind up on undefs_tail and because an
5232 undefined symbol which is defined later on in this pass
5233 does not require another pass. This isn't a bug, but it
5234 does make the code less efficient than it could be. */
5235 if (undefs_tail != info->hash->undefs_tail)
5236 loop = TRUE;
5237
5238 /* Look backward to mark all symbols from this object file
5239 which we have already seen in this pass. */
5240 mark = i;
5241 do
5242 {
5243 included[mark] = TRUE;
5244 if (mark == 0)
5245 break;
5246 --mark;
5247 }
5248 while (symdefs[mark].file_offset == symdef->file_offset);
5249
5250 /* We mark subsequent symbols from this object file as we go
5251 on through the loop. */
5252 last = symdef->file_offset;
5253 }
5254 }
5255 while (loop);
5256
5257 free (included);
5258
5259 return TRUE;
5260
5261 error_return:
5262 if (included != NULL)
5263 free (included);
5264 return FALSE;
5265 }
5266
5267 /* Given an ELF BFD, add symbols to the global hash table as
5268 appropriate. */
5269
5270 bfd_boolean
5271 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5272 {
5273 switch (bfd_get_format (abfd))
5274 {
5275 case bfd_object:
5276 return elf_link_add_object_symbols (abfd, info);
5277 case bfd_archive:
5278 return elf_link_add_archive_symbols (abfd, info);
5279 default:
5280 bfd_set_error (bfd_error_wrong_format);
5281 return FALSE;
5282 }
5283 }
5284 \f
5285 struct hash_codes_info
5286 {
5287 unsigned long *hashcodes;
5288 bfd_boolean error;
5289 };
5290
5291 /* This function will be called though elf_link_hash_traverse to store
5292 all hash value of the exported symbols in an array. */
5293
5294 static bfd_boolean
5295 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5296 {
5297 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5298 const char *name;
5299 unsigned long ha;
5300 char *alc = NULL;
5301
5302 /* Ignore indirect symbols. These are added by the versioning code. */
5303 if (h->dynindx == -1)
5304 return TRUE;
5305
5306 name = h->root.root.string;
5307 if (h->versioned >= versioned)
5308 {
5309 char *p = strchr (name, ELF_VER_CHR);
5310 if (p != NULL)
5311 {
5312 alc = (char *) bfd_malloc (p - name + 1);
5313 if (alc == NULL)
5314 {
5315 inf->error = TRUE;
5316 return FALSE;
5317 }
5318 memcpy (alc, name, p - name);
5319 alc[p - name] = '\0';
5320 name = alc;
5321 }
5322 }
5323
5324 /* Compute the hash value. */
5325 ha = bfd_elf_hash (name);
5326
5327 /* Store the found hash value in the array given as the argument. */
5328 *(inf->hashcodes)++ = ha;
5329
5330 /* And store it in the struct so that we can put it in the hash table
5331 later. */
5332 h->u.elf_hash_value = ha;
5333
5334 if (alc != NULL)
5335 free (alc);
5336
5337 return TRUE;
5338 }
5339
5340 struct collect_gnu_hash_codes
5341 {
5342 bfd *output_bfd;
5343 const struct elf_backend_data *bed;
5344 unsigned long int nsyms;
5345 unsigned long int maskbits;
5346 unsigned long int *hashcodes;
5347 unsigned long int *hashval;
5348 unsigned long int *indx;
5349 unsigned long int *counts;
5350 bfd_vma *bitmask;
5351 bfd_byte *contents;
5352 long int min_dynindx;
5353 unsigned long int bucketcount;
5354 unsigned long int symindx;
5355 long int local_indx;
5356 long int shift1, shift2;
5357 unsigned long int mask;
5358 bfd_boolean error;
5359 };
5360
5361 /* This function will be called though elf_link_hash_traverse to store
5362 all hash value of the exported symbols in an array. */
5363
5364 static bfd_boolean
5365 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5366 {
5367 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5368 const char *name;
5369 unsigned long ha;
5370 char *alc = NULL;
5371
5372 /* Ignore indirect symbols. These are added by the versioning code. */
5373 if (h->dynindx == -1)
5374 return TRUE;
5375
5376 /* Ignore also local symbols and undefined symbols. */
5377 if (! (*s->bed->elf_hash_symbol) (h))
5378 return TRUE;
5379
5380 name = h->root.root.string;
5381 if (h->versioned >= versioned)
5382 {
5383 char *p = strchr (name, ELF_VER_CHR);
5384 if (p != NULL)
5385 {
5386 alc = (char *) bfd_malloc (p - name + 1);
5387 if (alc == NULL)
5388 {
5389 s->error = TRUE;
5390 return FALSE;
5391 }
5392 memcpy (alc, name, p - name);
5393 alc[p - name] = '\0';
5394 name = alc;
5395 }
5396 }
5397
5398 /* Compute the hash value. */
5399 ha = bfd_elf_gnu_hash (name);
5400
5401 /* Store the found hash value in the array for compute_bucket_count,
5402 and also for .dynsym reordering purposes. */
5403 s->hashcodes[s->nsyms] = ha;
5404 s->hashval[h->dynindx] = ha;
5405 ++s->nsyms;
5406 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5407 s->min_dynindx = h->dynindx;
5408
5409 if (alc != NULL)
5410 free (alc);
5411
5412 return TRUE;
5413 }
5414
5415 /* This function will be called though elf_link_hash_traverse to do
5416 final dynaminc symbol renumbering. */
5417
5418 static bfd_boolean
5419 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5420 {
5421 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5422 unsigned long int bucket;
5423 unsigned long int val;
5424
5425 /* Ignore indirect symbols. */
5426 if (h->dynindx == -1)
5427 return TRUE;
5428
5429 /* Ignore also local symbols and undefined symbols. */
5430 if (! (*s->bed->elf_hash_symbol) (h))
5431 {
5432 if (h->dynindx >= s->min_dynindx)
5433 h->dynindx = s->local_indx++;
5434 return TRUE;
5435 }
5436
5437 bucket = s->hashval[h->dynindx] % s->bucketcount;
5438 val = (s->hashval[h->dynindx] >> s->shift1)
5439 & ((s->maskbits >> s->shift1) - 1);
5440 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5441 s->bitmask[val]
5442 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5443 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5444 if (s->counts[bucket] == 1)
5445 /* Last element terminates the chain. */
5446 val |= 1;
5447 bfd_put_32 (s->output_bfd, val,
5448 s->contents + (s->indx[bucket] - s->symindx) * 4);
5449 --s->counts[bucket];
5450 h->dynindx = s->indx[bucket]++;
5451 return TRUE;
5452 }
5453
5454 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5455
5456 bfd_boolean
5457 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5458 {
5459 return !(h->forced_local
5460 || h->root.type == bfd_link_hash_undefined
5461 || h->root.type == bfd_link_hash_undefweak
5462 || ((h->root.type == bfd_link_hash_defined
5463 || h->root.type == bfd_link_hash_defweak)
5464 && h->root.u.def.section->output_section == NULL));
5465 }
5466
5467 /* Array used to determine the number of hash table buckets to use
5468 based on the number of symbols there are. If there are fewer than
5469 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5470 fewer than 37 we use 17 buckets, and so forth. We never use more
5471 than 32771 buckets. */
5472
5473 static const size_t elf_buckets[] =
5474 {
5475 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5476 16411, 32771, 0
5477 };
5478
5479 /* Compute bucket count for hashing table. We do not use a static set
5480 of possible tables sizes anymore. Instead we determine for all
5481 possible reasonable sizes of the table the outcome (i.e., the
5482 number of collisions etc) and choose the best solution. The
5483 weighting functions are not too simple to allow the table to grow
5484 without bounds. Instead one of the weighting factors is the size.
5485 Therefore the result is always a good payoff between few collisions
5486 (= short chain lengths) and table size. */
5487 static size_t
5488 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5489 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5490 unsigned long int nsyms,
5491 int gnu_hash)
5492 {
5493 size_t best_size = 0;
5494 unsigned long int i;
5495
5496 /* We have a problem here. The following code to optimize the table
5497 size requires an integer type with more the 32 bits. If
5498 BFD_HOST_U_64_BIT is set we know about such a type. */
5499 #ifdef BFD_HOST_U_64_BIT
5500 if (info->optimize)
5501 {
5502 size_t minsize;
5503 size_t maxsize;
5504 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5505 bfd *dynobj = elf_hash_table (info)->dynobj;
5506 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5507 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5508 unsigned long int *counts;
5509 bfd_size_type amt;
5510 unsigned int no_improvement_count = 0;
5511
5512 /* Possible optimization parameters: if we have NSYMS symbols we say
5513 that the hashing table must at least have NSYMS/4 and at most
5514 2*NSYMS buckets. */
5515 minsize = nsyms / 4;
5516 if (minsize == 0)
5517 minsize = 1;
5518 best_size = maxsize = nsyms * 2;
5519 if (gnu_hash)
5520 {
5521 if (minsize < 2)
5522 minsize = 2;
5523 if ((best_size & 31) == 0)
5524 ++best_size;
5525 }
5526
5527 /* Create array where we count the collisions in. We must use bfd_malloc
5528 since the size could be large. */
5529 amt = maxsize;
5530 amt *= sizeof (unsigned long int);
5531 counts = (unsigned long int *) bfd_malloc (amt);
5532 if (counts == NULL)
5533 return 0;
5534
5535 /* Compute the "optimal" size for the hash table. The criteria is a
5536 minimal chain length. The minor criteria is (of course) the size
5537 of the table. */
5538 for (i = minsize; i < maxsize; ++i)
5539 {
5540 /* Walk through the array of hashcodes and count the collisions. */
5541 BFD_HOST_U_64_BIT max;
5542 unsigned long int j;
5543 unsigned long int fact;
5544
5545 if (gnu_hash && (i & 31) == 0)
5546 continue;
5547
5548 memset (counts, '\0', i * sizeof (unsigned long int));
5549
5550 /* Determine how often each hash bucket is used. */
5551 for (j = 0; j < nsyms; ++j)
5552 ++counts[hashcodes[j] % i];
5553
5554 /* For the weight function we need some information about the
5555 pagesize on the target. This is information need not be 100%
5556 accurate. Since this information is not available (so far) we
5557 define it here to a reasonable default value. If it is crucial
5558 to have a better value some day simply define this value. */
5559 # ifndef BFD_TARGET_PAGESIZE
5560 # define BFD_TARGET_PAGESIZE (4096)
5561 # endif
5562
5563 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5564 and the chains. */
5565 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5566
5567 # if 1
5568 /* Variant 1: optimize for short chains. We add the squares
5569 of all the chain lengths (which favors many small chain
5570 over a few long chains). */
5571 for (j = 0; j < i; ++j)
5572 max += counts[j] * counts[j];
5573
5574 /* This adds penalties for the overall size of the table. */
5575 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5576 max *= fact * fact;
5577 # else
5578 /* Variant 2: Optimize a lot more for small table. Here we
5579 also add squares of the size but we also add penalties for
5580 empty slots (the +1 term). */
5581 for (j = 0; j < i; ++j)
5582 max += (1 + counts[j]) * (1 + counts[j]);
5583
5584 /* The overall size of the table is considered, but not as
5585 strong as in variant 1, where it is squared. */
5586 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5587 max *= fact;
5588 # endif
5589
5590 /* Compare with current best results. */
5591 if (max < best_chlen)
5592 {
5593 best_chlen = max;
5594 best_size = i;
5595 no_improvement_count = 0;
5596 }
5597 /* PR 11843: Avoid futile long searches for the best bucket size
5598 when there are a large number of symbols. */
5599 else if (++no_improvement_count == 100)
5600 break;
5601 }
5602
5603 free (counts);
5604 }
5605 else
5606 #endif /* defined (BFD_HOST_U_64_BIT) */
5607 {
5608 /* This is the fallback solution if no 64bit type is available or if we
5609 are not supposed to spend much time on optimizations. We select the
5610 bucket count using a fixed set of numbers. */
5611 for (i = 0; elf_buckets[i] != 0; i++)
5612 {
5613 best_size = elf_buckets[i];
5614 if (nsyms < elf_buckets[i + 1])
5615 break;
5616 }
5617 if (gnu_hash && best_size < 2)
5618 best_size = 2;
5619 }
5620
5621 return best_size;
5622 }
5623
5624 /* Size any SHT_GROUP section for ld -r. */
5625
5626 bfd_boolean
5627 _bfd_elf_size_group_sections (struct bfd_link_info *info)
5628 {
5629 bfd *ibfd;
5630
5631 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
5632 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5633 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5634 return FALSE;
5635 return TRUE;
5636 }
5637
5638 /* Set a default stack segment size. The value in INFO wins. If it
5639 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5640 undefined it is initialized. */
5641
5642 bfd_boolean
5643 bfd_elf_stack_segment_size (bfd *output_bfd,
5644 struct bfd_link_info *info,
5645 const char *legacy_symbol,
5646 bfd_vma default_size)
5647 {
5648 struct elf_link_hash_entry *h = NULL;
5649
5650 /* Look for legacy symbol. */
5651 if (legacy_symbol)
5652 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5653 FALSE, FALSE, FALSE);
5654 if (h && (h->root.type == bfd_link_hash_defined
5655 || h->root.type == bfd_link_hash_defweak)
5656 && h->def_regular
5657 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5658 {
5659 /* The symbol has no type if specified on the command line. */
5660 h->type = STT_OBJECT;
5661 if (info->stacksize)
5662 (*_bfd_error_handler) (_("%B: stack size specified and %s set"),
5663 output_bfd, legacy_symbol);
5664 else if (h->root.u.def.section != bfd_abs_section_ptr)
5665 (*_bfd_error_handler) (_("%B: %s not absolute"),
5666 output_bfd, legacy_symbol);
5667 else
5668 info->stacksize = h->root.u.def.value;
5669 }
5670
5671 if (!info->stacksize)
5672 /* If the user didn't set a size, or explicitly inhibit the
5673 size, set it now. */
5674 info->stacksize = default_size;
5675
5676 /* Provide the legacy symbol, if it is referenced. */
5677 if (h && (h->root.type == bfd_link_hash_undefined
5678 || h->root.type == bfd_link_hash_undefweak))
5679 {
5680 struct bfd_link_hash_entry *bh = NULL;
5681
5682 if (!(_bfd_generic_link_add_one_symbol
5683 (info, output_bfd, legacy_symbol,
5684 BSF_GLOBAL, bfd_abs_section_ptr,
5685 info->stacksize >= 0 ? info->stacksize : 0,
5686 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5687 return FALSE;
5688
5689 h = (struct elf_link_hash_entry *) bh;
5690 h->def_regular = 1;
5691 h->type = STT_OBJECT;
5692 }
5693
5694 return TRUE;
5695 }
5696
5697 /* Set up the sizes and contents of the ELF dynamic sections. This is
5698 called by the ELF linker emulation before_allocation routine. We
5699 must set the sizes of the sections before the linker sets the
5700 addresses of the various sections. */
5701
5702 bfd_boolean
5703 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5704 const char *soname,
5705 const char *rpath,
5706 const char *filter_shlib,
5707 const char *audit,
5708 const char *depaudit,
5709 const char * const *auxiliary_filters,
5710 struct bfd_link_info *info,
5711 asection **sinterpptr)
5712 {
5713 bfd_size_type soname_indx;
5714 bfd *dynobj;
5715 const struct elf_backend_data *bed;
5716 struct elf_info_failed asvinfo;
5717
5718 *sinterpptr = NULL;
5719
5720 soname_indx = (bfd_size_type) -1;
5721
5722 if (!is_elf_hash_table (info->hash))
5723 return TRUE;
5724
5725 bed = get_elf_backend_data (output_bfd);
5726
5727 /* Any syms created from now on start with -1 in
5728 got.refcount/offset and plt.refcount/offset. */
5729 elf_hash_table (info)->init_got_refcount
5730 = elf_hash_table (info)->init_got_offset;
5731 elf_hash_table (info)->init_plt_refcount
5732 = elf_hash_table (info)->init_plt_offset;
5733
5734 if (bfd_link_relocatable (info)
5735 && !_bfd_elf_size_group_sections (info))
5736 return FALSE;
5737
5738 /* The backend may have to create some sections regardless of whether
5739 we're dynamic or not. */
5740 if (bed->elf_backend_always_size_sections
5741 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5742 return FALSE;
5743
5744 /* Determine any GNU_STACK segment requirements, after the backend
5745 has had a chance to set a default segment size. */
5746 if (info->execstack)
5747 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
5748 else if (info->noexecstack)
5749 elf_stack_flags (output_bfd) = PF_R | PF_W;
5750 else
5751 {
5752 bfd *inputobj;
5753 asection *notesec = NULL;
5754 int exec = 0;
5755
5756 for (inputobj = info->input_bfds;
5757 inputobj;
5758 inputobj = inputobj->link.next)
5759 {
5760 asection *s;
5761
5762 if (inputobj->flags
5763 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5764 continue;
5765 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5766 if (s)
5767 {
5768 if (s->flags & SEC_CODE)
5769 exec = PF_X;
5770 notesec = s;
5771 }
5772 else if (bed->default_execstack)
5773 exec = PF_X;
5774 }
5775 if (notesec || info->stacksize > 0)
5776 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
5777 if (notesec && exec && bfd_link_relocatable (info)
5778 && notesec->output_section != bfd_abs_section_ptr)
5779 notesec->output_section->flags |= SEC_CODE;
5780 }
5781
5782 dynobj = elf_hash_table (info)->dynobj;
5783
5784 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5785 {
5786 struct elf_info_failed eif;
5787 struct elf_link_hash_entry *h;
5788 asection *dynstr;
5789 struct bfd_elf_version_tree *t;
5790 struct bfd_elf_version_expr *d;
5791 asection *s;
5792 bfd_boolean all_defined;
5793
5794 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
5795 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
5796
5797 if (soname != NULL)
5798 {
5799 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5800 soname, TRUE);
5801 if (soname_indx == (bfd_size_type) -1
5802 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5803 return FALSE;
5804 }
5805
5806 if (info->symbolic)
5807 {
5808 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5809 return FALSE;
5810 info->flags |= DF_SYMBOLIC;
5811 }
5812
5813 if (rpath != NULL)
5814 {
5815 bfd_size_type indx;
5816 bfd_vma tag;
5817
5818 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5819 TRUE);
5820 if (indx == (bfd_size_type) -1)
5821 return FALSE;
5822
5823 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
5824 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
5825 return FALSE;
5826 }
5827
5828 if (filter_shlib != NULL)
5829 {
5830 bfd_size_type indx;
5831
5832 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5833 filter_shlib, TRUE);
5834 if (indx == (bfd_size_type) -1
5835 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5836 return FALSE;
5837 }
5838
5839 if (auxiliary_filters != NULL)
5840 {
5841 const char * const *p;
5842
5843 for (p = auxiliary_filters; *p != NULL; p++)
5844 {
5845 bfd_size_type indx;
5846
5847 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5848 *p, TRUE);
5849 if (indx == (bfd_size_type) -1
5850 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5851 return FALSE;
5852 }
5853 }
5854
5855 if (audit != NULL)
5856 {
5857 bfd_size_type indx;
5858
5859 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5860 TRUE);
5861 if (indx == (bfd_size_type) -1
5862 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5863 return FALSE;
5864 }
5865
5866 if (depaudit != NULL)
5867 {
5868 bfd_size_type indx;
5869
5870 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
5871 TRUE);
5872 if (indx == (bfd_size_type) -1
5873 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
5874 return FALSE;
5875 }
5876
5877 eif.info = info;
5878 eif.failed = FALSE;
5879
5880 /* If we are supposed to export all symbols into the dynamic symbol
5881 table (this is not the normal case), then do so. */
5882 if (info->export_dynamic
5883 || (bfd_link_executable (info) && info->dynamic))
5884 {
5885 elf_link_hash_traverse (elf_hash_table (info),
5886 _bfd_elf_export_symbol,
5887 &eif);
5888 if (eif.failed)
5889 return FALSE;
5890 }
5891
5892 /* Make all global versions with definition. */
5893 for (t = info->version_info; t != NULL; t = t->next)
5894 for (d = t->globals.list; d != NULL; d = d->next)
5895 if (!d->symver && d->literal)
5896 {
5897 const char *verstr, *name;
5898 size_t namelen, verlen, newlen;
5899 char *newname, *p, leading_char;
5900 struct elf_link_hash_entry *newh;
5901
5902 leading_char = bfd_get_symbol_leading_char (output_bfd);
5903 name = d->pattern;
5904 namelen = strlen (name) + (leading_char != '\0');
5905 verstr = t->name;
5906 verlen = strlen (verstr);
5907 newlen = namelen + verlen + 3;
5908
5909 newname = (char *) bfd_malloc (newlen);
5910 if (newname == NULL)
5911 return FALSE;
5912 newname[0] = leading_char;
5913 memcpy (newname + (leading_char != '\0'), name, namelen);
5914
5915 /* Check the hidden versioned definition. */
5916 p = newname + namelen;
5917 *p++ = ELF_VER_CHR;
5918 memcpy (p, verstr, verlen + 1);
5919 newh = elf_link_hash_lookup (elf_hash_table (info),
5920 newname, FALSE, FALSE,
5921 FALSE);
5922 if (newh == NULL
5923 || (newh->root.type != bfd_link_hash_defined
5924 && newh->root.type != bfd_link_hash_defweak))
5925 {
5926 /* Check the default versioned definition. */
5927 *p++ = ELF_VER_CHR;
5928 memcpy (p, verstr, verlen + 1);
5929 newh = elf_link_hash_lookup (elf_hash_table (info),
5930 newname, FALSE, FALSE,
5931 FALSE);
5932 }
5933 free (newname);
5934
5935 /* Mark this version if there is a definition and it is
5936 not defined in a shared object. */
5937 if (newh != NULL
5938 && !newh->def_dynamic
5939 && (newh->root.type == bfd_link_hash_defined
5940 || newh->root.type == bfd_link_hash_defweak))
5941 d->symver = 1;
5942 }
5943
5944 /* Attach all the symbols to their version information. */
5945 asvinfo.info = info;
5946 asvinfo.failed = FALSE;
5947
5948 elf_link_hash_traverse (elf_hash_table (info),
5949 _bfd_elf_link_assign_sym_version,
5950 &asvinfo);
5951 if (asvinfo.failed)
5952 return FALSE;
5953
5954 if (!info->allow_undefined_version)
5955 {
5956 /* Check if all global versions have a definition. */
5957 all_defined = TRUE;
5958 for (t = info->version_info; t != NULL; t = t->next)
5959 for (d = t->globals.list; d != NULL; d = d->next)
5960 if (d->literal && !d->symver && !d->script)
5961 {
5962 (*_bfd_error_handler)
5963 (_("%s: undefined version: %s"),
5964 d->pattern, t->name);
5965 all_defined = FALSE;
5966 }
5967
5968 if (!all_defined)
5969 {
5970 bfd_set_error (bfd_error_bad_value);
5971 return FALSE;
5972 }
5973 }
5974
5975 /* Find all symbols which were defined in a dynamic object and make
5976 the backend pick a reasonable value for them. */
5977 elf_link_hash_traverse (elf_hash_table (info),
5978 _bfd_elf_adjust_dynamic_symbol,
5979 &eif);
5980 if (eif.failed)
5981 return FALSE;
5982
5983 /* Add some entries to the .dynamic section. We fill in some of the
5984 values later, in bfd_elf_final_link, but we must add the entries
5985 now so that we know the final size of the .dynamic section. */
5986
5987 /* If there are initialization and/or finalization functions to
5988 call then add the corresponding DT_INIT/DT_FINI entries. */
5989 h = (info->init_function
5990 ? elf_link_hash_lookup (elf_hash_table (info),
5991 info->init_function, FALSE,
5992 FALSE, FALSE)
5993 : NULL);
5994 if (h != NULL
5995 && (h->ref_regular
5996 || h->def_regular))
5997 {
5998 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5999 return FALSE;
6000 }
6001 h = (info->fini_function
6002 ? elf_link_hash_lookup (elf_hash_table (info),
6003 info->fini_function, FALSE,
6004 FALSE, FALSE)
6005 : NULL);
6006 if (h != NULL
6007 && (h->ref_regular
6008 || h->def_regular))
6009 {
6010 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6011 return FALSE;
6012 }
6013
6014 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6015 if (s != NULL && s->linker_has_input)
6016 {
6017 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6018 if (! bfd_link_executable (info))
6019 {
6020 bfd *sub;
6021 asection *o;
6022
6023 for (sub = info->input_bfds; sub != NULL;
6024 sub = sub->link.next)
6025 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
6026 for (o = sub->sections; o != NULL; o = o->next)
6027 if (elf_section_data (o)->this_hdr.sh_type
6028 == SHT_PREINIT_ARRAY)
6029 {
6030 (*_bfd_error_handler)
6031 (_("%B: .preinit_array section is not allowed in DSO"),
6032 sub);
6033 break;
6034 }
6035
6036 bfd_set_error (bfd_error_nonrepresentable_section);
6037 return FALSE;
6038 }
6039
6040 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6041 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6042 return FALSE;
6043 }
6044 s = bfd_get_section_by_name (output_bfd, ".init_array");
6045 if (s != NULL && s->linker_has_input)
6046 {
6047 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6048 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6049 return FALSE;
6050 }
6051 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6052 if (s != NULL && s->linker_has_input)
6053 {
6054 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6055 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6056 return FALSE;
6057 }
6058
6059 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6060 /* If .dynstr is excluded from the link, we don't want any of
6061 these tags. Strictly, we should be checking each section
6062 individually; This quick check covers for the case where
6063 someone does a /DISCARD/ : { *(*) }. */
6064 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6065 {
6066 bfd_size_type strsize;
6067
6068 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6069 if ((info->emit_hash
6070 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6071 || (info->emit_gnu_hash
6072 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6073 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6074 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6075 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6076 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6077 bed->s->sizeof_sym))
6078 return FALSE;
6079 }
6080 }
6081
6082 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6083 return FALSE;
6084
6085 /* The backend must work out the sizes of all the other dynamic
6086 sections. */
6087 if (dynobj != NULL
6088 && bed->elf_backend_size_dynamic_sections != NULL
6089 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6090 return FALSE;
6091
6092 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6093 {
6094 unsigned long section_sym_count;
6095 struct bfd_elf_version_tree *verdefs;
6096 asection *s;
6097
6098 /* Set up the version definition section. */
6099 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6100 BFD_ASSERT (s != NULL);
6101
6102 /* We may have created additional version definitions if we are
6103 just linking a regular application. */
6104 verdefs = info->version_info;
6105
6106 /* Skip anonymous version tag. */
6107 if (verdefs != NULL && verdefs->vernum == 0)
6108 verdefs = verdefs->next;
6109
6110 if (verdefs == NULL && !info->create_default_symver)
6111 s->flags |= SEC_EXCLUDE;
6112 else
6113 {
6114 unsigned int cdefs;
6115 bfd_size_type size;
6116 struct bfd_elf_version_tree *t;
6117 bfd_byte *p;
6118 Elf_Internal_Verdef def;
6119 Elf_Internal_Verdaux defaux;
6120 struct bfd_link_hash_entry *bh;
6121 struct elf_link_hash_entry *h;
6122 const char *name;
6123
6124 cdefs = 0;
6125 size = 0;
6126
6127 /* Make space for the base version. */
6128 size += sizeof (Elf_External_Verdef);
6129 size += sizeof (Elf_External_Verdaux);
6130 ++cdefs;
6131
6132 /* Make space for the default version. */
6133 if (info->create_default_symver)
6134 {
6135 size += sizeof (Elf_External_Verdef);
6136 ++cdefs;
6137 }
6138
6139 for (t = verdefs; t != NULL; t = t->next)
6140 {
6141 struct bfd_elf_version_deps *n;
6142
6143 /* Don't emit base version twice. */
6144 if (t->vernum == 0)
6145 continue;
6146
6147 size += sizeof (Elf_External_Verdef);
6148 size += sizeof (Elf_External_Verdaux);
6149 ++cdefs;
6150
6151 for (n = t->deps; n != NULL; n = n->next)
6152 size += sizeof (Elf_External_Verdaux);
6153 }
6154
6155 s->size = size;
6156 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6157 if (s->contents == NULL && s->size != 0)
6158 return FALSE;
6159
6160 /* Fill in the version definition section. */
6161
6162 p = s->contents;
6163
6164 def.vd_version = VER_DEF_CURRENT;
6165 def.vd_flags = VER_FLG_BASE;
6166 def.vd_ndx = 1;
6167 def.vd_cnt = 1;
6168 if (info->create_default_symver)
6169 {
6170 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6171 def.vd_next = sizeof (Elf_External_Verdef);
6172 }
6173 else
6174 {
6175 def.vd_aux = sizeof (Elf_External_Verdef);
6176 def.vd_next = (sizeof (Elf_External_Verdef)
6177 + sizeof (Elf_External_Verdaux));
6178 }
6179
6180 if (soname_indx != (bfd_size_type) -1)
6181 {
6182 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6183 soname_indx);
6184 def.vd_hash = bfd_elf_hash (soname);
6185 defaux.vda_name = soname_indx;
6186 name = soname;
6187 }
6188 else
6189 {
6190 bfd_size_type indx;
6191
6192 name = lbasename (output_bfd->filename);
6193 def.vd_hash = bfd_elf_hash (name);
6194 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6195 name, FALSE);
6196 if (indx == (bfd_size_type) -1)
6197 return FALSE;
6198 defaux.vda_name = indx;
6199 }
6200 defaux.vda_next = 0;
6201
6202 _bfd_elf_swap_verdef_out (output_bfd, &def,
6203 (Elf_External_Verdef *) p);
6204 p += sizeof (Elf_External_Verdef);
6205 if (info->create_default_symver)
6206 {
6207 /* Add a symbol representing this version. */
6208 bh = NULL;
6209 if (! (_bfd_generic_link_add_one_symbol
6210 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6211 0, NULL, FALSE,
6212 get_elf_backend_data (dynobj)->collect, &bh)))
6213 return FALSE;
6214 h = (struct elf_link_hash_entry *) bh;
6215 h->non_elf = 0;
6216 h->def_regular = 1;
6217 h->type = STT_OBJECT;
6218 h->verinfo.vertree = NULL;
6219
6220 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6221 return FALSE;
6222
6223 /* Create a duplicate of the base version with the same
6224 aux block, but different flags. */
6225 def.vd_flags = 0;
6226 def.vd_ndx = 2;
6227 def.vd_aux = sizeof (Elf_External_Verdef);
6228 if (verdefs)
6229 def.vd_next = (sizeof (Elf_External_Verdef)
6230 + sizeof (Elf_External_Verdaux));
6231 else
6232 def.vd_next = 0;
6233 _bfd_elf_swap_verdef_out (output_bfd, &def,
6234 (Elf_External_Verdef *) p);
6235 p += sizeof (Elf_External_Verdef);
6236 }
6237 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6238 (Elf_External_Verdaux *) p);
6239 p += sizeof (Elf_External_Verdaux);
6240
6241 for (t = verdefs; t != NULL; t = t->next)
6242 {
6243 unsigned int cdeps;
6244 struct bfd_elf_version_deps *n;
6245
6246 /* Don't emit the base version twice. */
6247 if (t->vernum == 0)
6248 continue;
6249
6250 cdeps = 0;
6251 for (n = t->deps; n != NULL; n = n->next)
6252 ++cdeps;
6253
6254 /* Add a symbol representing this version. */
6255 bh = NULL;
6256 if (! (_bfd_generic_link_add_one_symbol
6257 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6258 0, NULL, FALSE,
6259 get_elf_backend_data (dynobj)->collect, &bh)))
6260 return FALSE;
6261 h = (struct elf_link_hash_entry *) bh;
6262 h->non_elf = 0;
6263 h->def_regular = 1;
6264 h->type = STT_OBJECT;
6265 h->verinfo.vertree = t;
6266
6267 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6268 return FALSE;
6269
6270 def.vd_version = VER_DEF_CURRENT;
6271 def.vd_flags = 0;
6272 if (t->globals.list == NULL
6273 && t->locals.list == NULL
6274 && ! t->used)
6275 def.vd_flags |= VER_FLG_WEAK;
6276 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6277 def.vd_cnt = cdeps + 1;
6278 def.vd_hash = bfd_elf_hash (t->name);
6279 def.vd_aux = sizeof (Elf_External_Verdef);
6280 def.vd_next = 0;
6281
6282 /* If a basever node is next, it *must* be the last node in
6283 the chain, otherwise Verdef construction breaks. */
6284 if (t->next != NULL && t->next->vernum == 0)
6285 BFD_ASSERT (t->next->next == NULL);
6286
6287 if (t->next != NULL && t->next->vernum != 0)
6288 def.vd_next = (sizeof (Elf_External_Verdef)
6289 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6290
6291 _bfd_elf_swap_verdef_out (output_bfd, &def,
6292 (Elf_External_Verdef *) p);
6293 p += sizeof (Elf_External_Verdef);
6294
6295 defaux.vda_name = h->dynstr_index;
6296 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6297 h->dynstr_index);
6298 defaux.vda_next = 0;
6299 if (t->deps != NULL)
6300 defaux.vda_next = sizeof (Elf_External_Verdaux);
6301 t->name_indx = defaux.vda_name;
6302
6303 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6304 (Elf_External_Verdaux *) p);
6305 p += sizeof (Elf_External_Verdaux);
6306
6307 for (n = t->deps; n != NULL; n = n->next)
6308 {
6309 if (n->version_needed == NULL)
6310 {
6311 /* This can happen if there was an error in the
6312 version script. */
6313 defaux.vda_name = 0;
6314 }
6315 else
6316 {
6317 defaux.vda_name = n->version_needed->name_indx;
6318 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6319 defaux.vda_name);
6320 }
6321 if (n->next == NULL)
6322 defaux.vda_next = 0;
6323 else
6324 defaux.vda_next = sizeof (Elf_External_Verdaux);
6325
6326 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6327 (Elf_External_Verdaux *) p);
6328 p += sizeof (Elf_External_Verdaux);
6329 }
6330 }
6331
6332 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6333 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6334 return FALSE;
6335
6336 elf_tdata (output_bfd)->cverdefs = cdefs;
6337 }
6338
6339 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6340 {
6341 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6342 return FALSE;
6343 }
6344 else if (info->flags & DF_BIND_NOW)
6345 {
6346 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6347 return FALSE;
6348 }
6349
6350 if (info->flags_1)
6351 {
6352 if (bfd_link_executable (info))
6353 info->flags_1 &= ~ (DF_1_INITFIRST
6354 | DF_1_NODELETE
6355 | DF_1_NOOPEN);
6356 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6357 return FALSE;
6358 }
6359
6360 /* Work out the size of the version reference section. */
6361
6362 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6363 BFD_ASSERT (s != NULL);
6364 {
6365 struct elf_find_verdep_info sinfo;
6366
6367 sinfo.info = info;
6368 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6369 if (sinfo.vers == 0)
6370 sinfo.vers = 1;
6371 sinfo.failed = FALSE;
6372
6373 elf_link_hash_traverse (elf_hash_table (info),
6374 _bfd_elf_link_find_version_dependencies,
6375 &sinfo);
6376 if (sinfo.failed)
6377 return FALSE;
6378
6379 if (elf_tdata (output_bfd)->verref == NULL)
6380 s->flags |= SEC_EXCLUDE;
6381 else
6382 {
6383 Elf_Internal_Verneed *t;
6384 unsigned int size;
6385 unsigned int crefs;
6386 bfd_byte *p;
6387
6388 /* Build the version dependency section. */
6389 size = 0;
6390 crefs = 0;
6391 for (t = elf_tdata (output_bfd)->verref;
6392 t != NULL;
6393 t = t->vn_nextref)
6394 {
6395 Elf_Internal_Vernaux *a;
6396
6397 size += sizeof (Elf_External_Verneed);
6398 ++crefs;
6399 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6400 size += sizeof (Elf_External_Vernaux);
6401 }
6402
6403 s->size = size;
6404 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6405 if (s->contents == NULL)
6406 return FALSE;
6407
6408 p = s->contents;
6409 for (t = elf_tdata (output_bfd)->verref;
6410 t != NULL;
6411 t = t->vn_nextref)
6412 {
6413 unsigned int caux;
6414 Elf_Internal_Vernaux *a;
6415 bfd_size_type indx;
6416
6417 caux = 0;
6418 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6419 ++caux;
6420
6421 t->vn_version = VER_NEED_CURRENT;
6422 t->vn_cnt = caux;
6423 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6424 elf_dt_name (t->vn_bfd) != NULL
6425 ? elf_dt_name (t->vn_bfd)
6426 : lbasename (t->vn_bfd->filename),
6427 FALSE);
6428 if (indx == (bfd_size_type) -1)
6429 return FALSE;
6430 t->vn_file = indx;
6431 t->vn_aux = sizeof (Elf_External_Verneed);
6432 if (t->vn_nextref == NULL)
6433 t->vn_next = 0;
6434 else
6435 t->vn_next = (sizeof (Elf_External_Verneed)
6436 + caux * sizeof (Elf_External_Vernaux));
6437
6438 _bfd_elf_swap_verneed_out (output_bfd, t,
6439 (Elf_External_Verneed *) p);
6440 p += sizeof (Elf_External_Verneed);
6441
6442 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6443 {
6444 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6445 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6446 a->vna_nodename, FALSE);
6447 if (indx == (bfd_size_type) -1)
6448 return FALSE;
6449 a->vna_name = indx;
6450 if (a->vna_nextptr == NULL)
6451 a->vna_next = 0;
6452 else
6453 a->vna_next = sizeof (Elf_External_Vernaux);
6454
6455 _bfd_elf_swap_vernaux_out (output_bfd, a,
6456 (Elf_External_Vernaux *) p);
6457 p += sizeof (Elf_External_Vernaux);
6458 }
6459 }
6460
6461 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6462 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6463 return FALSE;
6464
6465 elf_tdata (output_bfd)->cverrefs = crefs;
6466 }
6467 }
6468
6469 if ((elf_tdata (output_bfd)->cverrefs == 0
6470 && elf_tdata (output_bfd)->cverdefs == 0)
6471 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6472 &section_sym_count) == 0)
6473 {
6474 s = bfd_get_linker_section (dynobj, ".gnu.version");
6475 s->flags |= SEC_EXCLUDE;
6476 }
6477 }
6478 return TRUE;
6479 }
6480
6481 /* Find the first non-excluded output section. We'll use its
6482 section symbol for some emitted relocs. */
6483 void
6484 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6485 {
6486 asection *s;
6487
6488 for (s = output_bfd->sections; s != NULL; s = s->next)
6489 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6490 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6491 {
6492 elf_hash_table (info)->text_index_section = s;
6493 break;
6494 }
6495 }
6496
6497 /* Find two non-excluded output sections, one for code, one for data.
6498 We'll use their section symbols for some emitted relocs. */
6499 void
6500 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6501 {
6502 asection *s;
6503
6504 /* Data first, since setting text_index_section changes
6505 _bfd_elf_link_omit_section_dynsym. */
6506 for (s = output_bfd->sections; s != NULL; s = s->next)
6507 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6508 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6509 {
6510 elf_hash_table (info)->data_index_section = s;
6511 break;
6512 }
6513
6514 for (s = output_bfd->sections; s != NULL; s = s->next)
6515 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6516 == (SEC_ALLOC | SEC_READONLY))
6517 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6518 {
6519 elf_hash_table (info)->text_index_section = s;
6520 break;
6521 }
6522
6523 if (elf_hash_table (info)->text_index_section == NULL)
6524 elf_hash_table (info)->text_index_section
6525 = elf_hash_table (info)->data_index_section;
6526 }
6527
6528 bfd_boolean
6529 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6530 {
6531 const struct elf_backend_data *bed;
6532
6533 if (!is_elf_hash_table (info->hash))
6534 return TRUE;
6535
6536 bed = get_elf_backend_data (output_bfd);
6537 (*bed->elf_backend_init_index_section) (output_bfd, info);
6538
6539 if (elf_hash_table (info)->dynamic_sections_created)
6540 {
6541 bfd *dynobj;
6542 asection *s;
6543 bfd_size_type dynsymcount;
6544 unsigned long section_sym_count;
6545 unsigned int dtagcount;
6546
6547 dynobj = elf_hash_table (info)->dynobj;
6548
6549 /* Assign dynsym indicies. In a shared library we generate a
6550 section symbol for each output section, which come first.
6551 Next come all of the back-end allocated local dynamic syms,
6552 followed by the rest of the global symbols. */
6553
6554 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6555 &section_sym_count);
6556
6557 /* Work out the size of the symbol version section. */
6558 s = bfd_get_linker_section (dynobj, ".gnu.version");
6559 BFD_ASSERT (s != NULL);
6560 if (dynsymcount != 0
6561 && (s->flags & SEC_EXCLUDE) == 0)
6562 {
6563 s->size = dynsymcount * sizeof (Elf_External_Versym);
6564 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6565 if (s->contents == NULL)
6566 return FALSE;
6567
6568 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6569 return FALSE;
6570 }
6571
6572 /* Set the size of the .dynsym and .hash sections. We counted
6573 the number of dynamic symbols in elf_link_add_object_symbols.
6574 We will build the contents of .dynsym and .hash when we build
6575 the final symbol table, because until then we do not know the
6576 correct value to give the symbols. We built the .dynstr
6577 section as we went along in elf_link_add_object_symbols. */
6578 s = elf_hash_table (info)->dynsym;
6579 BFD_ASSERT (s != NULL);
6580 s->size = dynsymcount * bed->s->sizeof_sym;
6581
6582 if (dynsymcount != 0)
6583 {
6584 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6585 if (s->contents == NULL)
6586 return FALSE;
6587
6588 /* The first entry in .dynsym is a dummy symbol.
6589 Clear all the section syms, in case we don't output them all. */
6590 ++section_sym_count;
6591 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6592 }
6593
6594 elf_hash_table (info)->bucketcount = 0;
6595
6596 /* Compute the size of the hashing table. As a side effect this
6597 computes the hash values for all the names we export. */
6598 if (info->emit_hash)
6599 {
6600 unsigned long int *hashcodes;
6601 struct hash_codes_info hashinf;
6602 bfd_size_type amt;
6603 unsigned long int nsyms;
6604 size_t bucketcount;
6605 size_t hash_entry_size;
6606
6607 /* Compute the hash values for all exported symbols. At the same
6608 time store the values in an array so that we could use them for
6609 optimizations. */
6610 amt = dynsymcount * sizeof (unsigned long int);
6611 hashcodes = (unsigned long int *) bfd_malloc (amt);
6612 if (hashcodes == NULL)
6613 return FALSE;
6614 hashinf.hashcodes = hashcodes;
6615 hashinf.error = FALSE;
6616
6617 /* Put all hash values in HASHCODES. */
6618 elf_link_hash_traverse (elf_hash_table (info),
6619 elf_collect_hash_codes, &hashinf);
6620 if (hashinf.error)
6621 {
6622 free (hashcodes);
6623 return FALSE;
6624 }
6625
6626 nsyms = hashinf.hashcodes - hashcodes;
6627 bucketcount
6628 = compute_bucket_count (info, hashcodes, nsyms, 0);
6629 free (hashcodes);
6630
6631 if (bucketcount == 0)
6632 return FALSE;
6633
6634 elf_hash_table (info)->bucketcount = bucketcount;
6635
6636 s = bfd_get_linker_section (dynobj, ".hash");
6637 BFD_ASSERT (s != NULL);
6638 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6639 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6640 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6641 if (s->contents == NULL)
6642 return FALSE;
6643
6644 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6645 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6646 s->contents + hash_entry_size);
6647 }
6648
6649 if (info->emit_gnu_hash)
6650 {
6651 size_t i, cnt;
6652 unsigned char *contents;
6653 struct collect_gnu_hash_codes cinfo;
6654 bfd_size_type amt;
6655 size_t bucketcount;
6656
6657 memset (&cinfo, 0, sizeof (cinfo));
6658
6659 /* Compute the hash values for all exported symbols. At the same
6660 time store the values in an array so that we could use them for
6661 optimizations. */
6662 amt = dynsymcount * 2 * sizeof (unsigned long int);
6663 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
6664 if (cinfo.hashcodes == NULL)
6665 return FALSE;
6666
6667 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6668 cinfo.min_dynindx = -1;
6669 cinfo.output_bfd = output_bfd;
6670 cinfo.bed = bed;
6671
6672 /* Put all hash values in HASHCODES. */
6673 elf_link_hash_traverse (elf_hash_table (info),
6674 elf_collect_gnu_hash_codes, &cinfo);
6675 if (cinfo.error)
6676 {
6677 free (cinfo.hashcodes);
6678 return FALSE;
6679 }
6680
6681 bucketcount
6682 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6683
6684 if (bucketcount == 0)
6685 {
6686 free (cinfo.hashcodes);
6687 return FALSE;
6688 }
6689
6690 s = bfd_get_linker_section (dynobj, ".gnu.hash");
6691 BFD_ASSERT (s != NULL);
6692
6693 if (cinfo.nsyms == 0)
6694 {
6695 /* Empty .gnu.hash section is special. */
6696 BFD_ASSERT (cinfo.min_dynindx == -1);
6697 free (cinfo.hashcodes);
6698 s->size = 5 * 4 + bed->s->arch_size / 8;
6699 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6700 if (contents == NULL)
6701 return FALSE;
6702 s->contents = contents;
6703 /* 1 empty bucket. */
6704 bfd_put_32 (output_bfd, 1, contents);
6705 /* SYMIDX above the special symbol 0. */
6706 bfd_put_32 (output_bfd, 1, contents + 4);
6707 /* Just one word for bitmask. */
6708 bfd_put_32 (output_bfd, 1, contents + 8);
6709 /* Only hash fn bloom filter. */
6710 bfd_put_32 (output_bfd, 0, contents + 12);
6711 /* No hashes are valid - empty bitmask. */
6712 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6713 /* No hashes in the only bucket. */
6714 bfd_put_32 (output_bfd, 0,
6715 contents + 16 + bed->s->arch_size / 8);
6716 }
6717 else
6718 {
6719 unsigned long int maskwords, maskbitslog2, x;
6720 BFD_ASSERT (cinfo.min_dynindx != -1);
6721
6722 x = cinfo.nsyms;
6723 maskbitslog2 = 1;
6724 while ((x >>= 1) != 0)
6725 ++maskbitslog2;
6726 if (maskbitslog2 < 3)
6727 maskbitslog2 = 5;
6728 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6729 maskbitslog2 = maskbitslog2 + 3;
6730 else
6731 maskbitslog2 = maskbitslog2 + 2;
6732 if (bed->s->arch_size == 64)
6733 {
6734 if (maskbitslog2 == 5)
6735 maskbitslog2 = 6;
6736 cinfo.shift1 = 6;
6737 }
6738 else
6739 cinfo.shift1 = 5;
6740 cinfo.mask = (1 << cinfo.shift1) - 1;
6741 cinfo.shift2 = maskbitslog2;
6742 cinfo.maskbits = 1 << maskbitslog2;
6743 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6744 amt = bucketcount * sizeof (unsigned long int) * 2;
6745 amt += maskwords * sizeof (bfd_vma);
6746 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
6747 if (cinfo.bitmask == NULL)
6748 {
6749 free (cinfo.hashcodes);
6750 return FALSE;
6751 }
6752
6753 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
6754 cinfo.indx = cinfo.counts + bucketcount;
6755 cinfo.symindx = dynsymcount - cinfo.nsyms;
6756 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6757
6758 /* Determine how often each hash bucket is used. */
6759 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6760 for (i = 0; i < cinfo.nsyms; ++i)
6761 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6762
6763 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6764 if (cinfo.counts[i] != 0)
6765 {
6766 cinfo.indx[i] = cnt;
6767 cnt += cinfo.counts[i];
6768 }
6769 BFD_ASSERT (cnt == dynsymcount);
6770 cinfo.bucketcount = bucketcount;
6771 cinfo.local_indx = cinfo.min_dynindx;
6772
6773 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6774 s->size += cinfo.maskbits / 8;
6775 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6776 if (contents == NULL)
6777 {
6778 free (cinfo.bitmask);
6779 free (cinfo.hashcodes);
6780 return FALSE;
6781 }
6782
6783 s->contents = contents;
6784 bfd_put_32 (output_bfd, bucketcount, contents);
6785 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6786 bfd_put_32 (output_bfd, maskwords, contents + 8);
6787 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6788 contents += 16 + cinfo.maskbits / 8;
6789
6790 for (i = 0; i < bucketcount; ++i)
6791 {
6792 if (cinfo.counts[i] == 0)
6793 bfd_put_32 (output_bfd, 0, contents);
6794 else
6795 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6796 contents += 4;
6797 }
6798
6799 cinfo.contents = contents;
6800
6801 /* Renumber dynamic symbols, populate .gnu.hash section. */
6802 elf_link_hash_traverse (elf_hash_table (info),
6803 elf_renumber_gnu_hash_syms, &cinfo);
6804
6805 contents = s->contents + 16;
6806 for (i = 0; i < maskwords; ++i)
6807 {
6808 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6809 contents);
6810 contents += bed->s->arch_size / 8;
6811 }
6812
6813 free (cinfo.bitmask);
6814 free (cinfo.hashcodes);
6815 }
6816 }
6817
6818 s = bfd_get_linker_section (dynobj, ".dynstr");
6819 BFD_ASSERT (s != NULL);
6820
6821 elf_finalize_dynstr (output_bfd, info);
6822
6823 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6824
6825 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6826 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6827 return FALSE;
6828 }
6829
6830 return TRUE;
6831 }
6832 \f
6833 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
6834
6835 static void
6836 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6837 asection *sec)
6838 {
6839 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
6840 sec->sec_info_type = SEC_INFO_TYPE_NONE;
6841 }
6842
6843 /* Finish SHF_MERGE section merging. */
6844
6845 bfd_boolean
6846 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
6847 {
6848 bfd *ibfd;
6849 asection *sec;
6850
6851 if (!is_elf_hash_table (info->hash))
6852 return FALSE;
6853
6854 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6855 if ((ibfd->flags & DYNAMIC) == 0
6856 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6857 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
6858 == get_elf_backend_data (obfd)->s->elfclass))
6859 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6860 if ((sec->flags & SEC_MERGE) != 0
6861 && !bfd_is_abs_section (sec->output_section))
6862 {
6863 struct bfd_elf_section_data *secdata;
6864
6865 secdata = elf_section_data (sec);
6866 if (! _bfd_add_merge_section (obfd,
6867 &elf_hash_table (info)->merge_info,
6868 sec, &secdata->sec_info))
6869 return FALSE;
6870 else if (secdata->sec_info)
6871 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
6872 }
6873
6874 if (elf_hash_table (info)->merge_info != NULL)
6875 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
6876 merge_sections_remove_hook);
6877 return TRUE;
6878 }
6879
6880 /* Create an entry in an ELF linker hash table. */
6881
6882 struct bfd_hash_entry *
6883 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6884 struct bfd_hash_table *table,
6885 const char *string)
6886 {
6887 /* Allocate the structure if it has not already been allocated by a
6888 subclass. */
6889 if (entry == NULL)
6890 {
6891 entry = (struct bfd_hash_entry *)
6892 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
6893 if (entry == NULL)
6894 return entry;
6895 }
6896
6897 /* Call the allocation method of the superclass. */
6898 entry = _bfd_link_hash_newfunc (entry, table, string);
6899 if (entry != NULL)
6900 {
6901 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6902 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6903
6904 /* Set local fields. */
6905 ret->indx = -1;
6906 ret->dynindx = -1;
6907 ret->got = htab->init_got_refcount;
6908 ret->plt = htab->init_plt_refcount;
6909 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
6910 - offsetof (struct elf_link_hash_entry, size)));
6911 /* Assume that we have been called by a non-ELF symbol reader.
6912 This flag is then reset by the code which reads an ELF input
6913 file. This ensures that a symbol created by a non-ELF symbol
6914 reader will have the flag set correctly. */
6915 ret->non_elf = 1;
6916 }
6917
6918 return entry;
6919 }
6920
6921 /* Copy data from an indirect symbol to its direct symbol, hiding the
6922 old indirect symbol. Also used for copying flags to a weakdef. */
6923
6924 void
6925 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
6926 struct elf_link_hash_entry *dir,
6927 struct elf_link_hash_entry *ind)
6928 {
6929 struct elf_link_hash_table *htab;
6930
6931 /* Copy down any references that we may have already seen to the
6932 symbol which just became indirect if DIR isn't a hidden versioned
6933 symbol. */
6934
6935 if (dir->versioned != versioned_hidden)
6936 {
6937 dir->ref_dynamic |= ind->ref_dynamic;
6938 dir->ref_regular |= ind->ref_regular;
6939 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
6940 dir->non_got_ref |= ind->non_got_ref;
6941 dir->needs_plt |= ind->needs_plt;
6942 dir->pointer_equality_needed |= ind->pointer_equality_needed;
6943 }
6944
6945 if (ind->root.type != bfd_link_hash_indirect)
6946 return;
6947
6948 /* Copy over the global and procedure linkage table refcount entries.
6949 These may have been already set up by a check_relocs routine. */
6950 htab = elf_hash_table (info);
6951 if (ind->got.refcount > htab->init_got_refcount.refcount)
6952 {
6953 if (dir->got.refcount < 0)
6954 dir->got.refcount = 0;
6955 dir->got.refcount += ind->got.refcount;
6956 ind->got.refcount = htab->init_got_refcount.refcount;
6957 }
6958
6959 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
6960 {
6961 if (dir->plt.refcount < 0)
6962 dir->plt.refcount = 0;
6963 dir->plt.refcount += ind->plt.refcount;
6964 ind->plt.refcount = htab->init_plt_refcount.refcount;
6965 }
6966
6967 if (ind->dynindx != -1)
6968 {
6969 if (dir->dynindx != -1)
6970 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
6971 dir->dynindx = ind->dynindx;
6972 dir->dynstr_index = ind->dynstr_index;
6973 ind->dynindx = -1;
6974 ind->dynstr_index = 0;
6975 }
6976 }
6977
6978 void
6979 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
6980 struct elf_link_hash_entry *h,
6981 bfd_boolean force_local)
6982 {
6983 /* STT_GNU_IFUNC symbol must go through PLT. */
6984 if (h->type != STT_GNU_IFUNC)
6985 {
6986 h->plt = elf_hash_table (info)->init_plt_offset;
6987 h->needs_plt = 0;
6988 }
6989 if (force_local)
6990 {
6991 h->forced_local = 1;
6992 if (h->dynindx != -1)
6993 {
6994 h->dynindx = -1;
6995 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
6996 h->dynstr_index);
6997 }
6998 }
6999 }
7000
7001 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7002 caller. */
7003
7004 bfd_boolean
7005 _bfd_elf_link_hash_table_init
7006 (struct elf_link_hash_table *table,
7007 bfd *abfd,
7008 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7009 struct bfd_hash_table *,
7010 const char *),
7011 unsigned int entsize,
7012 enum elf_target_id target_id)
7013 {
7014 bfd_boolean ret;
7015 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7016
7017 table->init_got_refcount.refcount = can_refcount - 1;
7018 table->init_plt_refcount.refcount = can_refcount - 1;
7019 table->init_got_offset.offset = -(bfd_vma) 1;
7020 table->init_plt_offset.offset = -(bfd_vma) 1;
7021 /* The first dynamic symbol is a dummy. */
7022 table->dynsymcount = 1;
7023
7024 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7025
7026 table->root.type = bfd_link_elf_hash_table;
7027 table->hash_table_id = target_id;
7028
7029 return ret;
7030 }
7031
7032 /* Create an ELF linker hash table. */
7033
7034 struct bfd_link_hash_table *
7035 _bfd_elf_link_hash_table_create (bfd *abfd)
7036 {
7037 struct elf_link_hash_table *ret;
7038 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7039
7040 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7041 if (ret == NULL)
7042 return NULL;
7043
7044 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7045 sizeof (struct elf_link_hash_entry),
7046 GENERIC_ELF_DATA))
7047 {
7048 free (ret);
7049 return NULL;
7050 }
7051 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7052
7053 return &ret->root;
7054 }
7055
7056 /* Destroy an ELF linker hash table. */
7057
7058 void
7059 _bfd_elf_link_hash_table_free (bfd *obfd)
7060 {
7061 struct elf_link_hash_table *htab;
7062
7063 htab = (struct elf_link_hash_table *) obfd->link.hash;
7064 if (htab->dynstr != NULL)
7065 _bfd_elf_strtab_free (htab->dynstr);
7066 _bfd_merge_sections_free (htab->merge_info);
7067 _bfd_generic_link_hash_table_free (obfd);
7068 }
7069
7070 /* This is a hook for the ELF emulation code in the generic linker to
7071 tell the backend linker what file name to use for the DT_NEEDED
7072 entry for a dynamic object. */
7073
7074 void
7075 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7076 {
7077 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7078 && bfd_get_format (abfd) == bfd_object)
7079 elf_dt_name (abfd) = name;
7080 }
7081
7082 int
7083 bfd_elf_get_dyn_lib_class (bfd *abfd)
7084 {
7085 int lib_class;
7086 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7087 && bfd_get_format (abfd) == bfd_object)
7088 lib_class = elf_dyn_lib_class (abfd);
7089 else
7090 lib_class = 0;
7091 return lib_class;
7092 }
7093
7094 void
7095 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7096 {
7097 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7098 && bfd_get_format (abfd) == bfd_object)
7099 elf_dyn_lib_class (abfd) = lib_class;
7100 }
7101
7102 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7103 the linker ELF emulation code. */
7104
7105 struct bfd_link_needed_list *
7106 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7107 struct bfd_link_info *info)
7108 {
7109 if (! is_elf_hash_table (info->hash))
7110 return NULL;
7111 return elf_hash_table (info)->needed;
7112 }
7113
7114 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7115 hook for the linker ELF emulation code. */
7116
7117 struct bfd_link_needed_list *
7118 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7119 struct bfd_link_info *info)
7120 {
7121 if (! is_elf_hash_table (info->hash))
7122 return NULL;
7123 return elf_hash_table (info)->runpath;
7124 }
7125
7126 /* Get the name actually used for a dynamic object for a link. This
7127 is the SONAME entry if there is one. Otherwise, it is the string
7128 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7129
7130 const char *
7131 bfd_elf_get_dt_soname (bfd *abfd)
7132 {
7133 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7134 && bfd_get_format (abfd) == bfd_object)
7135 return elf_dt_name (abfd);
7136 return NULL;
7137 }
7138
7139 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7140 the ELF linker emulation code. */
7141
7142 bfd_boolean
7143 bfd_elf_get_bfd_needed_list (bfd *abfd,
7144 struct bfd_link_needed_list **pneeded)
7145 {
7146 asection *s;
7147 bfd_byte *dynbuf = NULL;
7148 unsigned int elfsec;
7149 unsigned long shlink;
7150 bfd_byte *extdyn, *extdynend;
7151 size_t extdynsize;
7152 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7153
7154 *pneeded = NULL;
7155
7156 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7157 || bfd_get_format (abfd) != bfd_object)
7158 return TRUE;
7159
7160 s = bfd_get_section_by_name (abfd, ".dynamic");
7161 if (s == NULL || s->size == 0)
7162 return TRUE;
7163
7164 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7165 goto error_return;
7166
7167 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7168 if (elfsec == SHN_BAD)
7169 goto error_return;
7170
7171 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7172
7173 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7174 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7175
7176 extdyn = dynbuf;
7177 extdynend = extdyn + s->size;
7178 for (; extdyn < extdynend; extdyn += extdynsize)
7179 {
7180 Elf_Internal_Dyn dyn;
7181
7182 (*swap_dyn_in) (abfd, extdyn, &dyn);
7183
7184 if (dyn.d_tag == DT_NULL)
7185 break;
7186
7187 if (dyn.d_tag == DT_NEEDED)
7188 {
7189 const char *string;
7190 struct bfd_link_needed_list *l;
7191 unsigned int tagv = dyn.d_un.d_val;
7192 bfd_size_type amt;
7193
7194 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7195 if (string == NULL)
7196 goto error_return;
7197
7198 amt = sizeof *l;
7199 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7200 if (l == NULL)
7201 goto error_return;
7202
7203 l->by = abfd;
7204 l->name = string;
7205 l->next = *pneeded;
7206 *pneeded = l;
7207 }
7208 }
7209
7210 free (dynbuf);
7211
7212 return TRUE;
7213
7214 error_return:
7215 if (dynbuf != NULL)
7216 free (dynbuf);
7217 return FALSE;
7218 }
7219
7220 struct elf_symbuf_symbol
7221 {
7222 unsigned long st_name; /* Symbol name, index in string tbl */
7223 unsigned char st_info; /* Type and binding attributes */
7224 unsigned char st_other; /* Visibilty, and target specific */
7225 };
7226
7227 struct elf_symbuf_head
7228 {
7229 struct elf_symbuf_symbol *ssym;
7230 bfd_size_type count;
7231 unsigned int st_shndx;
7232 };
7233
7234 struct elf_symbol
7235 {
7236 union
7237 {
7238 Elf_Internal_Sym *isym;
7239 struct elf_symbuf_symbol *ssym;
7240 } u;
7241 const char *name;
7242 };
7243
7244 /* Sort references to symbols by ascending section number. */
7245
7246 static int
7247 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7248 {
7249 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7250 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7251
7252 return s1->st_shndx - s2->st_shndx;
7253 }
7254
7255 static int
7256 elf_sym_name_compare (const void *arg1, const void *arg2)
7257 {
7258 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7259 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7260 return strcmp (s1->name, s2->name);
7261 }
7262
7263 static struct elf_symbuf_head *
7264 elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
7265 {
7266 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7267 struct elf_symbuf_symbol *ssym;
7268 struct elf_symbuf_head *ssymbuf, *ssymhead;
7269 bfd_size_type i, shndx_count, total_size;
7270
7271 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7272 if (indbuf == NULL)
7273 return NULL;
7274
7275 for (ind = indbuf, i = 0; i < symcount; i++)
7276 if (isymbuf[i].st_shndx != SHN_UNDEF)
7277 *ind++ = &isymbuf[i];
7278 indbufend = ind;
7279
7280 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7281 elf_sort_elf_symbol);
7282
7283 shndx_count = 0;
7284 if (indbufend > indbuf)
7285 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7286 if (ind[0]->st_shndx != ind[1]->st_shndx)
7287 shndx_count++;
7288
7289 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7290 + (indbufend - indbuf) * sizeof (*ssym));
7291 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7292 if (ssymbuf == NULL)
7293 {
7294 free (indbuf);
7295 return NULL;
7296 }
7297
7298 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7299 ssymbuf->ssym = NULL;
7300 ssymbuf->count = shndx_count;
7301 ssymbuf->st_shndx = 0;
7302 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7303 {
7304 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7305 {
7306 ssymhead++;
7307 ssymhead->ssym = ssym;
7308 ssymhead->count = 0;
7309 ssymhead->st_shndx = (*ind)->st_shndx;
7310 }
7311 ssym->st_name = (*ind)->st_name;
7312 ssym->st_info = (*ind)->st_info;
7313 ssym->st_other = (*ind)->st_other;
7314 ssymhead->count++;
7315 }
7316 BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
7317 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7318 == total_size));
7319
7320 free (indbuf);
7321 return ssymbuf;
7322 }
7323
7324 /* Check if 2 sections define the same set of local and global
7325 symbols. */
7326
7327 static bfd_boolean
7328 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7329 struct bfd_link_info *info)
7330 {
7331 bfd *bfd1, *bfd2;
7332 const struct elf_backend_data *bed1, *bed2;
7333 Elf_Internal_Shdr *hdr1, *hdr2;
7334 bfd_size_type symcount1, symcount2;
7335 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7336 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7337 Elf_Internal_Sym *isym, *isymend;
7338 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7339 bfd_size_type count1, count2, i;
7340 unsigned int shndx1, shndx2;
7341 bfd_boolean result;
7342
7343 bfd1 = sec1->owner;
7344 bfd2 = sec2->owner;
7345
7346 /* Both sections have to be in ELF. */
7347 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7348 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7349 return FALSE;
7350
7351 if (elf_section_type (sec1) != elf_section_type (sec2))
7352 return FALSE;
7353
7354 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7355 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7356 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7357 return FALSE;
7358
7359 bed1 = get_elf_backend_data (bfd1);
7360 bed2 = get_elf_backend_data (bfd2);
7361 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7362 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7363 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7364 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7365
7366 if (symcount1 == 0 || symcount2 == 0)
7367 return FALSE;
7368
7369 result = FALSE;
7370 isymbuf1 = NULL;
7371 isymbuf2 = NULL;
7372 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7373 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7374
7375 if (ssymbuf1 == NULL)
7376 {
7377 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7378 NULL, NULL, NULL);
7379 if (isymbuf1 == NULL)
7380 goto done;
7381
7382 if (!info->reduce_memory_overheads)
7383 elf_tdata (bfd1)->symbuf = ssymbuf1
7384 = elf_create_symbuf (symcount1, isymbuf1);
7385 }
7386
7387 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7388 {
7389 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7390 NULL, NULL, NULL);
7391 if (isymbuf2 == NULL)
7392 goto done;
7393
7394 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7395 elf_tdata (bfd2)->symbuf = ssymbuf2
7396 = elf_create_symbuf (symcount2, isymbuf2);
7397 }
7398
7399 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7400 {
7401 /* Optimized faster version. */
7402 bfd_size_type lo, hi, mid;
7403 struct elf_symbol *symp;
7404 struct elf_symbuf_symbol *ssym, *ssymend;
7405
7406 lo = 0;
7407 hi = ssymbuf1->count;
7408 ssymbuf1++;
7409 count1 = 0;
7410 while (lo < hi)
7411 {
7412 mid = (lo + hi) / 2;
7413 if (shndx1 < ssymbuf1[mid].st_shndx)
7414 hi = mid;
7415 else if (shndx1 > ssymbuf1[mid].st_shndx)
7416 lo = mid + 1;
7417 else
7418 {
7419 count1 = ssymbuf1[mid].count;
7420 ssymbuf1 += mid;
7421 break;
7422 }
7423 }
7424
7425 lo = 0;
7426 hi = ssymbuf2->count;
7427 ssymbuf2++;
7428 count2 = 0;
7429 while (lo < hi)
7430 {
7431 mid = (lo + hi) / 2;
7432 if (shndx2 < ssymbuf2[mid].st_shndx)
7433 hi = mid;
7434 else if (shndx2 > ssymbuf2[mid].st_shndx)
7435 lo = mid + 1;
7436 else
7437 {
7438 count2 = ssymbuf2[mid].count;
7439 ssymbuf2 += mid;
7440 break;
7441 }
7442 }
7443
7444 if (count1 == 0 || count2 == 0 || count1 != count2)
7445 goto done;
7446
7447 symtable1
7448 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7449 symtable2
7450 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
7451 if (symtable1 == NULL || symtable2 == NULL)
7452 goto done;
7453
7454 symp = symtable1;
7455 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7456 ssym < ssymend; ssym++, symp++)
7457 {
7458 symp->u.ssym = ssym;
7459 symp->name = bfd_elf_string_from_elf_section (bfd1,
7460 hdr1->sh_link,
7461 ssym->st_name);
7462 }
7463
7464 symp = symtable2;
7465 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7466 ssym < ssymend; ssym++, symp++)
7467 {
7468 symp->u.ssym = ssym;
7469 symp->name = bfd_elf_string_from_elf_section (bfd2,
7470 hdr2->sh_link,
7471 ssym->st_name);
7472 }
7473
7474 /* Sort symbol by name. */
7475 qsort (symtable1, count1, sizeof (struct elf_symbol),
7476 elf_sym_name_compare);
7477 qsort (symtable2, count1, sizeof (struct elf_symbol),
7478 elf_sym_name_compare);
7479
7480 for (i = 0; i < count1; i++)
7481 /* Two symbols must have the same binding, type and name. */
7482 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7483 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7484 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7485 goto done;
7486
7487 result = TRUE;
7488 goto done;
7489 }
7490
7491 symtable1 = (struct elf_symbol *)
7492 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7493 symtable2 = (struct elf_symbol *)
7494 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7495 if (symtable1 == NULL || symtable2 == NULL)
7496 goto done;
7497
7498 /* Count definitions in the section. */
7499 count1 = 0;
7500 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7501 if (isym->st_shndx == shndx1)
7502 symtable1[count1++].u.isym = isym;
7503
7504 count2 = 0;
7505 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7506 if (isym->st_shndx == shndx2)
7507 symtable2[count2++].u.isym = isym;
7508
7509 if (count1 == 0 || count2 == 0 || count1 != count2)
7510 goto done;
7511
7512 for (i = 0; i < count1; i++)
7513 symtable1[i].name
7514 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7515 symtable1[i].u.isym->st_name);
7516
7517 for (i = 0; i < count2; i++)
7518 symtable2[i].name
7519 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7520 symtable2[i].u.isym->st_name);
7521
7522 /* Sort symbol by name. */
7523 qsort (symtable1, count1, sizeof (struct elf_symbol),
7524 elf_sym_name_compare);
7525 qsort (symtable2, count1, sizeof (struct elf_symbol),
7526 elf_sym_name_compare);
7527
7528 for (i = 0; i < count1; i++)
7529 /* Two symbols must have the same binding, type and name. */
7530 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7531 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7532 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7533 goto done;
7534
7535 result = TRUE;
7536
7537 done:
7538 if (symtable1)
7539 free (symtable1);
7540 if (symtable2)
7541 free (symtable2);
7542 if (isymbuf1)
7543 free (isymbuf1);
7544 if (isymbuf2)
7545 free (isymbuf2);
7546
7547 return result;
7548 }
7549
7550 /* Return TRUE if 2 section types are compatible. */
7551
7552 bfd_boolean
7553 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7554 bfd *bbfd, const asection *bsec)
7555 {
7556 if (asec == NULL
7557 || bsec == NULL
7558 || abfd->xvec->flavour != bfd_target_elf_flavour
7559 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7560 return TRUE;
7561
7562 return elf_section_type (asec) == elf_section_type (bsec);
7563 }
7564 \f
7565 /* Final phase of ELF linker. */
7566
7567 /* A structure we use to avoid passing large numbers of arguments. */
7568
7569 struct elf_final_link_info
7570 {
7571 /* General link information. */
7572 struct bfd_link_info *info;
7573 /* Output BFD. */
7574 bfd *output_bfd;
7575 /* Symbol string table. */
7576 struct elf_strtab_hash *symstrtab;
7577 /* .hash section. */
7578 asection *hash_sec;
7579 /* symbol version section (.gnu.version). */
7580 asection *symver_sec;
7581 /* Buffer large enough to hold contents of any section. */
7582 bfd_byte *contents;
7583 /* Buffer large enough to hold external relocs of any section. */
7584 void *external_relocs;
7585 /* Buffer large enough to hold internal relocs of any section. */
7586 Elf_Internal_Rela *internal_relocs;
7587 /* Buffer large enough to hold external local symbols of any input
7588 BFD. */
7589 bfd_byte *external_syms;
7590 /* And a buffer for symbol section indices. */
7591 Elf_External_Sym_Shndx *locsym_shndx;
7592 /* Buffer large enough to hold internal local symbols of any input
7593 BFD. */
7594 Elf_Internal_Sym *internal_syms;
7595 /* Array large enough to hold a symbol index for each local symbol
7596 of any input BFD. */
7597 long *indices;
7598 /* Array large enough to hold a section pointer for each local
7599 symbol of any input BFD. */
7600 asection **sections;
7601 /* Buffer for SHT_SYMTAB_SHNDX section. */
7602 Elf_External_Sym_Shndx *symshndxbuf;
7603 /* Number of STT_FILE syms seen. */
7604 size_t filesym_count;
7605 };
7606
7607 /* This struct is used to pass information to elf_link_output_extsym. */
7608
7609 struct elf_outext_info
7610 {
7611 bfd_boolean failed;
7612 bfd_boolean localsyms;
7613 bfd_boolean file_sym_done;
7614 struct elf_final_link_info *flinfo;
7615 };
7616
7617
7618 /* Support for evaluating a complex relocation.
7619
7620 Complex relocations are generalized, self-describing relocations. The
7621 implementation of them consists of two parts: complex symbols, and the
7622 relocations themselves.
7623
7624 The relocations are use a reserved elf-wide relocation type code (R_RELC
7625 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7626 information (start bit, end bit, word width, etc) into the addend. This
7627 information is extracted from CGEN-generated operand tables within gas.
7628
7629 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7630 internal) representing prefix-notation expressions, including but not
7631 limited to those sorts of expressions normally encoded as addends in the
7632 addend field. The symbol mangling format is:
7633
7634 <node> := <literal>
7635 | <unary-operator> ':' <node>
7636 | <binary-operator> ':' <node> ':' <node>
7637 ;
7638
7639 <literal> := 's' <digits=N> ':' <N character symbol name>
7640 | 'S' <digits=N> ':' <N character section name>
7641 | '#' <hexdigits>
7642 ;
7643
7644 <binary-operator> := as in C
7645 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7646
7647 static void
7648 set_symbol_value (bfd *bfd_with_globals,
7649 Elf_Internal_Sym *isymbuf,
7650 size_t locsymcount,
7651 size_t symidx,
7652 bfd_vma val)
7653 {
7654 struct elf_link_hash_entry **sym_hashes;
7655 struct elf_link_hash_entry *h;
7656 size_t extsymoff = locsymcount;
7657
7658 if (symidx < locsymcount)
7659 {
7660 Elf_Internal_Sym *sym;
7661
7662 sym = isymbuf + symidx;
7663 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7664 {
7665 /* It is a local symbol: move it to the
7666 "absolute" section and give it a value. */
7667 sym->st_shndx = SHN_ABS;
7668 sym->st_value = val;
7669 return;
7670 }
7671 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7672 extsymoff = 0;
7673 }
7674
7675 /* It is a global symbol: set its link type
7676 to "defined" and give it a value. */
7677
7678 sym_hashes = elf_sym_hashes (bfd_with_globals);
7679 h = sym_hashes [symidx - extsymoff];
7680 while (h->root.type == bfd_link_hash_indirect
7681 || h->root.type == bfd_link_hash_warning)
7682 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7683 h->root.type = bfd_link_hash_defined;
7684 h->root.u.def.value = val;
7685 h->root.u.def.section = bfd_abs_section_ptr;
7686 }
7687
7688 static bfd_boolean
7689 resolve_symbol (const char *name,
7690 bfd *input_bfd,
7691 struct elf_final_link_info *flinfo,
7692 bfd_vma *result,
7693 Elf_Internal_Sym *isymbuf,
7694 size_t locsymcount)
7695 {
7696 Elf_Internal_Sym *sym;
7697 struct bfd_link_hash_entry *global_entry;
7698 const char *candidate = NULL;
7699 Elf_Internal_Shdr *symtab_hdr;
7700 size_t i;
7701
7702 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7703
7704 for (i = 0; i < locsymcount; ++ i)
7705 {
7706 sym = isymbuf + i;
7707
7708 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7709 continue;
7710
7711 candidate = bfd_elf_string_from_elf_section (input_bfd,
7712 symtab_hdr->sh_link,
7713 sym->st_name);
7714 #ifdef DEBUG
7715 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7716 name, candidate, (unsigned long) sym->st_value);
7717 #endif
7718 if (candidate && strcmp (candidate, name) == 0)
7719 {
7720 asection *sec = flinfo->sections [i];
7721
7722 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7723 *result += sec->output_offset + sec->output_section->vma;
7724 #ifdef DEBUG
7725 printf ("Found symbol with value %8.8lx\n",
7726 (unsigned long) *result);
7727 #endif
7728 return TRUE;
7729 }
7730 }
7731
7732 /* Hmm, haven't found it yet. perhaps it is a global. */
7733 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
7734 FALSE, FALSE, TRUE);
7735 if (!global_entry)
7736 return FALSE;
7737
7738 if (global_entry->type == bfd_link_hash_defined
7739 || global_entry->type == bfd_link_hash_defweak)
7740 {
7741 *result = (global_entry->u.def.value
7742 + global_entry->u.def.section->output_section->vma
7743 + global_entry->u.def.section->output_offset);
7744 #ifdef DEBUG
7745 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7746 global_entry->root.string, (unsigned long) *result);
7747 #endif
7748 return TRUE;
7749 }
7750
7751 return FALSE;
7752 }
7753
7754 static bfd_boolean
7755 resolve_section (const char *name,
7756 asection *sections,
7757 bfd_vma *result)
7758 {
7759 asection *curr;
7760 unsigned int len;
7761
7762 for (curr = sections; curr; curr = curr->next)
7763 if (strcmp (curr->name, name) == 0)
7764 {
7765 *result = curr->vma;
7766 return TRUE;
7767 }
7768
7769 /* Hmm. still haven't found it. try pseudo-section names. */
7770 for (curr = sections; curr; curr = curr->next)
7771 {
7772 len = strlen (curr->name);
7773 if (len > strlen (name))
7774 continue;
7775
7776 if (strncmp (curr->name, name, len) == 0)
7777 {
7778 if (strncmp (".end", name + len, 4) == 0)
7779 {
7780 *result = curr->vma + curr->size;
7781 return TRUE;
7782 }
7783
7784 /* Insert more pseudo-section names here, if you like. */
7785 }
7786 }
7787
7788 return FALSE;
7789 }
7790
7791 static void
7792 undefined_reference (const char *reftype, const char *name)
7793 {
7794 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7795 reftype, name);
7796 }
7797
7798 static bfd_boolean
7799 eval_symbol (bfd_vma *result,
7800 const char **symp,
7801 bfd *input_bfd,
7802 struct elf_final_link_info *flinfo,
7803 bfd_vma dot,
7804 Elf_Internal_Sym *isymbuf,
7805 size_t locsymcount,
7806 int signed_p)
7807 {
7808 size_t len;
7809 size_t symlen;
7810 bfd_vma a;
7811 bfd_vma b;
7812 char symbuf[4096];
7813 const char *sym = *symp;
7814 const char *symend;
7815 bfd_boolean symbol_is_section = FALSE;
7816
7817 len = strlen (sym);
7818 symend = sym + len;
7819
7820 if (len < 1 || len > sizeof (symbuf))
7821 {
7822 bfd_set_error (bfd_error_invalid_operation);
7823 return FALSE;
7824 }
7825
7826 switch (* sym)
7827 {
7828 case '.':
7829 *result = dot;
7830 *symp = sym + 1;
7831 return TRUE;
7832
7833 case '#':
7834 ++sym;
7835 *result = strtoul (sym, (char **) symp, 16);
7836 return TRUE;
7837
7838 case 'S':
7839 symbol_is_section = TRUE;
7840 case 's':
7841 ++sym;
7842 symlen = strtol (sym, (char **) symp, 10);
7843 sym = *symp + 1; /* Skip the trailing ':'. */
7844
7845 if (symend < sym || symlen + 1 > sizeof (symbuf))
7846 {
7847 bfd_set_error (bfd_error_invalid_operation);
7848 return FALSE;
7849 }
7850
7851 memcpy (symbuf, sym, symlen);
7852 symbuf[symlen] = '\0';
7853 *symp = sym + symlen;
7854
7855 /* Is it always possible, with complex symbols, that gas "mis-guessed"
7856 the symbol as a section, or vice-versa. so we're pretty liberal in our
7857 interpretation here; section means "try section first", not "must be a
7858 section", and likewise with symbol. */
7859
7860 if (symbol_is_section)
7861 {
7862 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result)
7863 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
7864 isymbuf, locsymcount))
7865 {
7866 undefined_reference ("section", symbuf);
7867 return FALSE;
7868 }
7869 }
7870 else
7871 {
7872 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
7873 isymbuf, locsymcount)
7874 && !resolve_section (symbuf, flinfo->output_bfd->sections,
7875 result))
7876 {
7877 undefined_reference ("symbol", symbuf);
7878 return FALSE;
7879 }
7880 }
7881
7882 return TRUE;
7883
7884 /* All that remains are operators. */
7885
7886 #define UNARY_OP(op) \
7887 if (strncmp (sym, #op, strlen (#op)) == 0) \
7888 { \
7889 sym += strlen (#op); \
7890 if (*sym == ':') \
7891 ++sym; \
7892 *symp = sym; \
7893 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
7894 isymbuf, locsymcount, signed_p)) \
7895 return FALSE; \
7896 if (signed_p) \
7897 *result = op ((bfd_signed_vma) a); \
7898 else \
7899 *result = op a; \
7900 return TRUE; \
7901 }
7902
7903 #define BINARY_OP(op) \
7904 if (strncmp (sym, #op, strlen (#op)) == 0) \
7905 { \
7906 sym += strlen (#op); \
7907 if (*sym == ':') \
7908 ++sym; \
7909 *symp = sym; \
7910 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
7911 isymbuf, locsymcount, signed_p)) \
7912 return FALSE; \
7913 ++*symp; \
7914 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
7915 isymbuf, locsymcount, signed_p)) \
7916 return FALSE; \
7917 if (signed_p) \
7918 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
7919 else \
7920 *result = a op b; \
7921 return TRUE; \
7922 }
7923
7924 default:
7925 UNARY_OP (0-);
7926 BINARY_OP (<<);
7927 BINARY_OP (>>);
7928 BINARY_OP (==);
7929 BINARY_OP (!=);
7930 BINARY_OP (<=);
7931 BINARY_OP (>=);
7932 BINARY_OP (&&);
7933 BINARY_OP (||);
7934 UNARY_OP (~);
7935 UNARY_OP (!);
7936 BINARY_OP (*);
7937 BINARY_OP (/);
7938 BINARY_OP (%);
7939 BINARY_OP (^);
7940 BINARY_OP (|);
7941 BINARY_OP (&);
7942 BINARY_OP (+);
7943 BINARY_OP (-);
7944 BINARY_OP (<);
7945 BINARY_OP (>);
7946 #undef UNARY_OP
7947 #undef BINARY_OP
7948 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
7949 bfd_set_error (bfd_error_invalid_operation);
7950 return FALSE;
7951 }
7952 }
7953
7954 static void
7955 put_value (bfd_vma size,
7956 unsigned long chunksz,
7957 bfd *input_bfd,
7958 bfd_vma x,
7959 bfd_byte *location)
7960 {
7961 location += (size - chunksz);
7962
7963 for (; size; size -= chunksz, location -= chunksz)
7964 {
7965 switch (chunksz)
7966 {
7967 case 1:
7968 bfd_put_8 (input_bfd, x, location);
7969 x >>= 8;
7970 break;
7971 case 2:
7972 bfd_put_16 (input_bfd, x, location);
7973 x >>= 16;
7974 break;
7975 case 4:
7976 bfd_put_32 (input_bfd, x, location);
7977 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
7978 x >>= 16;
7979 x >>= 16;
7980 break;
7981 #ifdef BFD64
7982 case 8:
7983 bfd_put_64 (input_bfd, x, location);
7984 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
7985 x >>= 32;
7986 x >>= 32;
7987 break;
7988 #endif
7989 default:
7990 abort ();
7991 break;
7992 }
7993 }
7994 }
7995
7996 static bfd_vma
7997 get_value (bfd_vma size,
7998 unsigned long chunksz,
7999 bfd *input_bfd,
8000 bfd_byte *location)
8001 {
8002 int shift;
8003 bfd_vma x = 0;
8004
8005 /* Sanity checks. */
8006 BFD_ASSERT (chunksz <= sizeof (x)
8007 && size >= chunksz
8008 && chunksz != 0
8009 && (size % chunksz) == 0
8010 && input_bfd != NULL
8011 && location != NULL);
8012
8013 if (chunksz == sizeof (x))
8014 {
8015 BFD_ASSERT (size == chunksz);
8016
8017 /* Make sure that we do not perform an undefined shift operation.
8018 We know that size == chunksz so there will only be one iteration
8019 of the loop below. */
8020 shift = 0;
8021 }
8022 else
8023 shift = 8 * chunksz;
8024
8025 for (; size; size -= chunksz, location += chunksz)
8026 {
8027 switch (chunksz)
8028 {
8029 case 1:
8030 x = (x << shift) | bfd_get_8 (input_bfd, location);
8031 break;
8032 case 2:
8033 x = (x << shift) | bfd_get_16 (input_bfd, location);
8034 break;
8035 case 4:
8036 x = (x << shift) | bfd_get_32 (input_bfd, location);
8037 break;
8038 #ifdef BFD64
8039 case 8:
8040 x = (x << shift) | bfd_get_64 (input_bfd, location);
8041 break;
8042 #endif
8043 default:
8044 abort ();
8045 }
8046 }
8047 return x;
8048 }
8049
8050 static void
8051 decode_complex_addend (unsigned long *start, /* in bits */
8052 unsigned long *oplen, /* in bits */
8053 unsigned long *len, /* in bits */
8054 unsigned long *wordsz, /* in bytes */
8055 unsigned long *chunksz, /* in bytes */
8056 unsigned long *lsb0_p,
8057 unsigned long *signed_p,
8058 unsigned long *trunc_p,
8059 unsigned long encoded)
8060 {
8061 * start = encoded & 0x3F;
8062 * len = (encoded >> 6) & 0x3F;
8063 * oplen = (encoded >> 12) & 0x3F;
8064 * wordsz = (encoded >> 18) & 0xF;
8065 * chunksz = (encoded >> 22) & 0xF;
8066 * lsb0_p = (encoded >> 27) & 1;
8067 * signed_p = (encoded >> 28) & 1;
8068 * trunc_p = (encoded >> 29) & 1;
8069 }
8070
8071 bfd_reloc_status_type
8072 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8073 asection *input_section ATTRIBUTE_UNUSED,
8074 bfd_byte *contents,
8075 Elf_Internal_Rela *rel,
8076 bfd_vma relocation)
8077 {
8078 bfd_vma shift, x, mask;
8079 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8080 bfd_reloc_status_type r;
8081
8082 /* Perform this reloc, since it is complex.
8083 (this is not to say that it necessarily refers to a complex
8084 symbol; merely that it is a self-describing CGEN based reloc.
8085 i.e. the addend has the complete reloc information (bit start, end,
8086 word size, etc) encoded within it.). */
8087
8088 decode_complex_addend (&start, &oplen, &len, &wordsz,
8089 &chunksz, &lsb0_p, &signed_p,
8090 &trunc_p, rel->r_addend);
8091
8092 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8093
8094 if (lsb0_p)
8095 shift = (start + 1) - len;
8096 else
8097 shift = (8 * wordsz) - (start + len);
8098
8099 /* FIXME: octets_per_byte. */
8100 x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
8101
8102 #ifdef DEBUG
8103 printf ("Doing complex reloc: "
8104 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8105 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8106 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8107 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8108 oplen, (unsigned long) x, (unsigned long) mask,
8109 (unsigned long) relocation);
8110 #endif
8111
8112 r = bfd_reloc_ok;
8113 if (! trunc_p)
8114 /* Now do an overflow check. */
8115 r = bfd_check_overflow ((signed_p
8116 ? complain_overflow_signed
8117 : complain_overflow_unsigned),
8118 len, 0, (8 * wordsz),
8119 relocation);
8120
8121 /* Do the deed. */
8122 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8123
8124 #ifdef DEBUG
8125 printf (" relocation: %8.8lx\n"
8126 " shifted mask: %8.8lx\n"
8127 " shifted/masked reloc: %8.8lx\n"
8128 " result: %8.8lx\n",
8129 (unsigned long) relocation, (unsigned long) (mask << shift),
8130 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8131 #endif
8132 /* FIXME: octets_per_byte. */
8133 put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
8134 return r;
8135 }
8136
8137 /* Functions to read r_offset from external (target order) reloc
8138 entry. Faster than bfd_getl32 et al, because we let the compiler
8139 know the value is aligned. */
8140
8141 static bfd_vma
8142 ext32l_r_offset (const void *p)
8143 {
8144 union aligned32
8145 {
8146 uint32_t v;
8147 unsigned char c[4];
8148 };
8149 const union aligned32 *a
8150 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8151
8152 uint32_t aval = ( (uint32_t) a->c[0]
8153 | (uint32_t) a->c[1] << 8
8154 | (uint32_t) a->c[2] << 16
8155 | (uint32_t) a->c[3] << 24);
8156 return aval;
8157 }
8158
8159 static bfd_vma
8160 ext32b_r_offset (const void *p)
8161 {
8162 union aligned32
8163 {
8164 uint32_t v;
8165 unsigned char c[4];
8166 };
8167 const union aligned32 *a
8168 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8169
8170 uint32_t aval = ( (uint32_t) a->c[0] << 24
8171 | (uint32_t) a->c[1] << 16
8172 | (uint32_t) a->c[2] << 8
8173 | (uint32_t) a->c[3]);
8174 return aval;
8175 }
8176
8177 #ifdef BFD_HOST_64_BIT
8178 static bfd_vma
8179 ext64l_r_offset (const void *p)
8180 {
8181 union aligned64
8182 {
8183 uint64_t v;
8184 unsigned char c[8];
8185 };
8186 const union aligned64 *a
8187 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8188
8189 uint64_t aval = ( (uint64_t) a->c[0]
8190 | (uint64_t) a->c[1] << 8
8191 | (uint64_t) a->c[2] << 16
8192 | (uint64_t) a->c[3] << 24
8193 | (uint64_t) a->c[4] << 32
8194 | (uint64_t) a->c[5] << 40
8195 | (uint64_t) a->c[6] << 48
8196 | (uint64_t) a->c[7] << 56);
8197 return aval;
8198 }
8199
8200 static bfd_vma
8201 ext64b_r_offset (const void *p)
8202 {
8203 union aligned64
8204 {
8205 uint64_t v;
8206 unsigned char c[8];
8207 };
8208 const union aligned64 *a
8209 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8210
8211 uint64_t aval = ( (uint64_t) a->c[0] << 56
8212 | (uint64_t) a->c[1] << 48
8213 | (uint64_t) a->c[2] << 40
8214 | (uint64_t) a->c[3] << 32
8215 | (uint64_t) a->c[4] << 24
8216 | (uint64_t) a->c[5] << 16
8217 | (uint64_t) a->c[6] << 8
8218 | (uint64_t) a->c[7]);
8219 return aval;
8220 }
8221 #endif
8222
8223 /* When performing a relocatable link, the input relocations are
8224 preserved. But, if they reference global symbols, the indices
8225 referenced must be updated. Update all the relocations found in
8226 RELDATA. */
8227
8228 static bfd_boolean
8229 elf_link_adjust_relocs (bfd *abfd,
8230 struct bfd_elf_section_reloc_data *reldata,
8231 bfd_boolean sort)
8232 {
8233 unsigned int i;
8234 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8235 bfd_byte *erela;
8236 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8237 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8238 bfd_vma r_type_mask;
8239 int r_sym_shift;
8240 unsigned int count = reldata->count;
8241 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8242
8243 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8244 {
8245 swap_in = bed->s->swap_reloc_in;
8246 swap_out = bed->s->swap_reloc_out;
8247 }
8248 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8249 {
8250 swap_in = bed->s->swap_reloca_in;
8251 swap_out = bed->s->swap_reloca_out;
8252 }
8253 else
8254 abort ();
8255
8256 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8257 abort ();
8258
8259 if (bed->s->arch_size == 32)
8260 {
8261 r_type_mask = 0xff;
8262 r_sym_shift = 8;
8263 }
8264 else
8265 {
8266 r_type_mask = 0xffffffff;
8267 r_sym_shift = 32;
8268 }
8269
8270 erela = reldata->hdr->contents;
8271 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8272 {
8273 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8274 unsigned int j;
8275
8276 if (*rel_hash == NULL)
8277 continue;
8278
8279 BFD_ASSERT ((*rel_hash)->indx >= 0);
8280
8281 (*swap_in) (abfd, erela, irela);
8282 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8283 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8284 | (irela[j].r_info & r_type_mask));
8285 (*swap_out) (abfd, irela, erela);
8286 }
8287
8288 if (sort && count != 0)
8289 {
8290 bfd_vma (*ext_r_off) (const void *);
8291 bfd_vma r_off;
8292 size_t elt_size;
8293 bfd_byte *base, *end, *p, *loc;
8294 bfd_byte *buf = NULL;
8295
8296 if (bed->s->arch_size == 32)
8297 {
8298 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8299 ext_r_off = ext32l_r_offset;
8300 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8301 ext_r_off = ext32b_r_offset;
8302 else
8303 abort ();
8304 }
8305 else
8306 {
8307 #ifdef BFD_HOST_64_BIT
8308 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8309 ext_r_off = ext64l_r_offset;
8310 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8311 ext_r_off = ext64b_r_offset;
8312 else
8313 #endif
8314 abort ();
8315 }
8316
8317 /* Must use a stable sort here. A modified insertion sort,
8318 since the relocs are mostly sorted already. */
8319 elt_size = reldata->hdr->sh_entsize;
8320 base = reldata->hdr->contents;
8321 end = base + count * elt_size;
8322 if (elt_size > sizeof (Elf64_External_Rela))
8323 abort ();
8324
8325 /* Ensure the first element is lowest. This acts as a sentinel,
8326 speeding the main loop below. */
8327 r_off = (*ext_r_off) (base);
8328 for (p = loc = base; (p += elt_size) < end; )
8329 {
8330 bfd_vma r_off2 = (*ext_r_off) (p);
8331 if (r_off > r_off2)
8332 {
8333 r_off = r_off2;
8334 loc = p;
8335 }
8336 }
8337 if (loc != base)
8338 {
8339 /* Don't just swap *base and *loc as that changes the order
8340 of the original base[0] and base[1] if they happen to
8341 have the same r_offset. */
8342 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8343 memcpy (onebuf, loc, elt_size);
8344 memmove (base + elt_size, base, loc - base);
8345 memcpy (base, onebuf, elt_size);
8346 }
8347
8348 for (p = base + elt_size; (p += elt_size) < end; )
8349 {
8350 /* base to p is sorted, *p is next to insert. */
8351 r_off = (*ext_r_off) (p);
8352 /* Search the sorted region for location to insert. */
8353 loc = p - elt_size;
8354 while (r_off < (*ext_r_off) (loc))
8355 loc -= elt_size;
8356 loc += elt_size;
8357 if (loc != p)
8358 {
8359 /* Chances are there is a run of relocs to insert here,
8360 from one of more input files. Files are not always
8361 linked in order due to the way elf_link_input_bfd is
8362 called. See pr17666. */
8363 size_t sortlen = p - loc;
8364 bfd_vma r_off2 = (*ext_r_off) (loc);
8365 size_t runlen = elt_size;
8366 size_t buf_size = 96 * 1024;
8367 while (p + runlen < end
8368 && (sortlen <= buf_size
8369 || runlen + elt_size <= buf_size)
8370 && r_off2 > (*ext_r_off) (p + runlen))
8371 runlen += elt_size;
8372 if (buf == NULL)
8373 {
8374 buf = bfd_malloc (buf_size);
8375 if (buf == NULL)
8376 return FALSE;
8377 }
8378 if (runlen < sortlen)
8379 {
8380 memcpy (buf, p, runlen);
8381 memmove (loc + runlen, loc, sortlen);
8382 memcpy (loc, buf, runlen);
8383 }
8384 else
8385 {
8386 memcpy (buf, loc, sortlen);
8387 memmove (loc, p, runlen);
8388 memcpy (loc + runlen, buf, sortlen);
8389 }
8390 p += runlen - elt_size;
8391 }
8392 }
8393 /* Hashes are no longer valid. */
8394 free (reldata->hashes);
8395 reldata->hashes = NULL;
8396 free (buf);
8397 }
8398 return TRUE;
8399 }
8400
8401 struct elf_link_sort_rela
8402 {
8403 union {
8404 bfd_vma offset;
8405 bfd_vma sym_mask;
8406 } u;
8407 enum elf_reloc_type_class type;
8408 /* We use this as an array of size int_rels_per_ext_rel. */
8409 Elf_Internal_Rela rela[1];
8410 };
8411
8412 static int
8413 elf_link_sort_cmp1 (const void *A, const void *B)
8414 {
8415 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8416 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8417 int relativea, relativeb;
8418
8419 relativea = a->type == reloc_class_relative;
8420 relativeb = b->type == reloc_class_relative;
8421
8422 if (relativea < relativeb)
8423 return 1;
8424 if (relativea > relativeb)
8425 return -1;
8426 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8427 return -1;
8428 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8429 return 1;
8430 if (a->rela->r_offset < b->rela->r_offset)
8431 return -1;
8432 if (a->rela->r_offset > b->rela->r_offset)
8433 return 1;
8434 return 0;
8435 }
8436
8437 static int
8438 elf_link_sort_cmp2 (const void *A, const void *B)
8439 {
8440 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8441 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8442
8443 if (a->type < b->type)
8444 return -1;
8445 if (a->type > b->type)
8446 return 1;
8447 if (a->u.offset < b->u.offset)
8448 return -1;
8449 if (a->u.offset > b->u.offset)
8450 return 1;
8451 if (a->rela->r_offset < b->rela->r_offset)
8452 return -1;
8453 if (a->rela->r_offset > b->rela->r_offset)
8454 return 1;
8455 return 0;
8456 }
8457
8458 static size_t
8459 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8460 {
8461 asection *dynamic_relocs;
8462 asection *rela_dyn;
8463 asection *rel_dyn;
8464 bfd_size_type count, size;
8465 size_t i, ret, sort_elt, ext_size;
8466 bfd_byte *sort, *s_non_relative, *p;
8467 struct elf_link_sort_rela *sq;
8468 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8469 int i2e = bed->s->int_rels_per_ext_rel;
8470 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8471 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8472 struct bfd_link_order *lo;
8473 bfd_vma r_sym_mask;
8474 bfd_boolean use_rela;
8475
8476 /* Find a dynamic reloc section. */
8477 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8478 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8479 if (rela_dyn != NULL && rela_dyn->size > 0
8480 && rel_dyn != NULL && rel_dyn->size > 0)
8481 {
8482 bfd_boolean use_rela_initialised = FALSE;
8483
8484 /* This is just here to stop gcc from complaining.
8485 It's initialization checking code is not perfect. */
8486 use_rela = TRUE;
8487
8488 /* Both sections are present. Examine the sizes
8489 of the indirect sections to help us choose. */
8490 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8491 if (lo->type == bfd_indirect_link_order)
8492 {
8493 asection *o = lo->u.indirect.section;
8494
8495 if ((o->size % bed->s->sizeof_rela) == 0)
8496 {
8497 if ((o->size % bed->s->sizeof_rel) == 0)
8498 /* Section size is divisible by both rel and rela sizes.
8499 It is of no help to us. */
8500 ;
8501 else
8502 {
8503 /* Section size is only divisible by rela. */
8504 if (use_rela_initialised && (use_rela == FALSE))
8505 {
8506 _bfd_error_handler
8507 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8508 bfd_set_error (bfd_error_invalid_operation);
8509 return 0;
8510 }
8511 else
8512 {
8513 use_rela = TRUE;
8514 use_rela_initialised = TRUE;
8515 }
8516 }
8517 }
8518 else if ((o->size % bed->s->sizeof_rel) == 0)
8519 {
8520 /* Section size is only divisible by rel. */
8521 if (use_rela_initialised && (use_rela == TRUE))
8522 {
8523 _bfd_error_handler
8524 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8525 bfd_set_error (bfd_error_invalid_operation);
8526 return 0;
8527 }
8528 else
8529 {
8530 use_rela = FALSE;
8531 use_rela_initialised = TRUE;
8532 }
8533 }
8534 else
8535 {
8536 /* The section size is not divisible by either - something is wrong. */
8537 _bfd_error_handler
8538 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8539 bfd_set_error (bfd_error_invalid_operation);
8540 return 0;
8541 }
8542 }
8543
8544 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8545 if (lo->type == bfd_indirect_link_order)
8546 {
8547 asection *o = lo->u.indirect.section;
8548
8549 if ((o->size % bed->s->sizeof_rela) == 0)
8550 {
8551 if ((o->size % bed->s->sizeof_rel) == 0)
8552 /* Section size is divisible by both rel and rela sizes.
8553 It is of no help to us. */
8554 ;
8555 else
8556 {
8557 /* Section size is only divisible by rela. */
8558 if (use_rela_initialised && (use_rela == FALSE))
8559 {
8560 _bfd_error_handler
8561 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8562 bfd_set_error (bfd_error_invalid_operation);
8563 return 0;
8564 }
8565 else
8566 {
8567 use_rela = TRUE;
8568 use_rela_initialised = TRUE;
8569 }
8570 }
8571 }
8572 else if ((o->size % bed->s->sizeof_rel) == 0)
8573 {
8574 /* Section size is only divisible by rel. */
8575 if (use_rela_initialised && (use_rela == TRUE))
8576 {
8577 _bfd_error_handler
8578 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8579 bfd_set_error (bfd_error_invalid_operation);
8580 return 0;
8581 }
8582 else
8583 {
8584 use_rela = FALSE;
8585 use_rela_initialised = TRUE;
8586 }
8587 }
8588 else
8589 {
8590 /* The section size is not divisible by either - something is wrong. */
8591 _bfd_error_handler
8592 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8593 bfd_set_error (bfd_error_invalid_operation);
8594 return 0;
8595 }
8596 }
8597
8598 if (! use_rela_initialised)
8599 /* Make a guess. */
8600 use_rela = TRUE;
8601 }
8602 else if (rela_dyn != NULL && rela_dyn->size > 0)
8603 use_rela = TRUE;
8604 else if (rel_dyn != NULL && rel_dyn->size > 0)
8605 use_rela = FALSE;
8606 else
8607 return 0;
8608
8609 if (use_rela)
8610 {
8611 dynamic_relocs = rela_dyn;
8612 ext_size = bed->s->sizeof_rela;
8613 swap_in = bed->s->swap_reloca_in;
8614 swap_out = bed->s->swap_reloca_out;
8615 }
8616 else
8617 {
8618 dynamic_relocs = rel_dyn;
8619 ext_size = bed->s->sizeof_rel;
8620 swap_in = bed->s->swap_reloc_in;
8621 swap_out = bed->s->swap_reloc_out;
8622 }
8623
8624 size = 0;
8625 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8626 if (lo->type == bfd_indirect_link_order)
8627 size += lo->u.indirect.section->size;
8628
8629 if (size != dynamic_relocs->size)
8630 return 0;
8631
8632 sort_elt = (sizeof (struct elf_link_sort_rela)
8633 + (i2e - 1) * sizeof (Elf_Internal_Rela));
8634
8635 count = dynamic_relocs->size / ext_size;
8636 if (count == 0)
8637 return 0;
8638 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
8639
8640 if (sort == NULL)
8641 {
8642 (*info->callbacks->warning)
8643 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8644 return 0;
8645 }
8646
8647 if (bed->s->arch_size == 32)
8648 r_sym_mask = ~(bfd_vma) 0xff;
8649 else
8650 r_sym_mask = ~(bfd_vma) 0xffffffff;
8651
8652 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8653 if (lo->type == bfd_indirect_link_order)
8654 {
8655 bfd_byte *erel, *erelend;
8656 asection *o = lo->u.indirect.section;
8657
8658 if (o->contents == NULL && o->size != 0)
8659 {
8660 /* This is a reloc section that is being handled as a normal
8661 section. See bfd_section_from_shdr. We can't combine
8662 relocs in this case. */
8663 free (sort);
8664 return 0;
8665 }
8666 erel = o->contents;
8667 erelend = o->contents + o->size;
8668 /* FIXME: octets_per_byte. */
8669 p = sort + o->output_offset / ext_size * sort_elt;
8670
8671 while (erel < erelend)
8672 {
8673 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8674
8675 (*swap_in) (abfd, erel, s->rela);
8676 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
8677 s->u.sym_mask = r_sym_mask;
8678 p += sort_elt;
8679 erel += ext_size;
8680 }
8681 }
8682
8683 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8684
8685 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8686 {
8687 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8688 if (s->type != reloc_class_relative)
8689 break;
8690 }
8691 ret = i;
8692 s_non_relative = p;
8693
8694 sq = (struct elf_link_sort_rela *) s_non_relative;
8695 for (; i < count; i++, p += sort_elt)
8696 {
8697 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8698 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8699 sq = sp;
8700 sp->u.offset = sq->rela->r_offset;
8701 }
8702
8703 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8704
8705 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8706 if (lo->type == bfd_indirect_link_order)
8707 {
8708 bfd_byte *erel, *erelend;
8709 asection *o = lo->u.indirect.section;
8710
8711 erel = o->contents;
8712 erelend = o->contents + o->size;
8713 /* FIXME: octets_per_byte. */
8714 p = sort + o->output_offset / ext_size * sort_elt;
8715 while (erel < erelend)
8716 {
8717 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8718 (*swap_out) (abfd, s->rela, erel);
8719 p += sort_elt;
8720 erel += ext_size;
8721 }
8722 }
8723
8724 free (sort);
8725 *psec = dynamic_relocs;
8726 return ret;
8727 }
8728
8729 /* Add a symbol to the output symbol string table. */
8730
8731 static int
8732 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
8733 const char *name,
8734 Elf_Internal_Sym *elfsym,
8735 asection *input_sec,
8736 struct elf_link_hash_entry *h)
8737 {
8738 int (*output_symbol_hook)
8739 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8740 struct elf_link_hash_entry *);
8741 struct elf_link_hash_table *hash_table;
8742 const struct elf_backend_data *bed;
8743 bfd_size_type strtabsize;
8744
8745 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8746
8747 bed = get_elf_backend_data (flinfo->output_bfd);
8748 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8749 if (output_symbol_hook != NULL)
8750 {
8751 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
8752 if (ret != 1)
8753 return ret;
8754 }
8755
8756 if (name == NULL
8757 || *name == '\0'
8758 || (input_sec->flags & SEC_EXCLUDE))
8759 elfsym->st_name = (unsigned long) -1;
8760 else
8761 {
8762 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8763 to get the final offset for st_name. */
8764 elfsym->st_name
8765 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
8766 name, FALSE);
8767 if (elfsym->st_name == (unsigned long) -1)
8768 return 0;
8769 }
8770
8771 hash_table = elf_hash_table (flinfo->info);
8772 strtabsize = hash_table->strtabsize;
8773 if (strtabsize <= hash_table->strtabcount)
8774 {
8775 strtabsize += strtabsize;
8776 hash_table->strtabsize = strtabsize;
8777 strtabsize *= sizeof (*hash_table->strtab);
8778 hash_table->strtab
8779 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
8780 strtabsize);
8781 if (hash_table->strtab == NULL)
8782 return 0;
8783 }
8784 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
8785 hash_table->strtab[hash_table->strtabcount].dest_index
8786 = hash_table->strtabcount;
8787 hash_table->strtab[hash_table->strtabcount].destshndx_index
8788 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
8789
8790 bfd_get_symcount (flinfo->output_bfd) += 1;
8791 hash_table->strtabcount += 1;
8792
8793 return 1;
8794 }
8795
8796 /* Swap symbols out to the symbol table and flush the output symbols to
8797 the file. */
8798
8799 static bfd_boolean
8800 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
8801 {
8802 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
8803 bfd_size_type amt, i;
8804 const struct elf_backend_data *bed;
8805 bfd_byte *symbuf;
8806 Elf_Internal_Shdr *hdr;
8807 file_ptr pos;
8808 bfd_boolean ret;
8809
8810 if (!hash_table->strtabcount)
8811 return TRUE;
8812
8813 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8814
8815 bed = get_elf_backend_data (flinfo->output_bfd);
8816
8817 amt = bed->s->sizeof_sym * hash_table->strtabcount;
8818 symbuf = (bfd_byte *) bfd_malloc (amt);
8819 if (symbuf == NULL)
8820 return FALSE;
8821
8822 if (flinfo->symshndxbuf)
8823 {
8824 amt = (sizeof (Elf_External_Sym_Shndx)
8825 * (bfd_get_symcount (flinfo->output_bfd)));
8826 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
8827 if (flinfo->symshndxbuf == NULL)
8828 {
8829 free (symbuf);
8830 return FALSE;
8831 }
8832 }
8833
8834 for (i = 0; i < hash_table->strtabcount; i++)
8835 {
8836 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
8837 if (elfsym->sym.st_name == (unsigned long) -1)
8838 elfsym->sym.st_name = 0;
8839 else
8840 elfsym->sym.st_name
8841 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
8842 elfsym->sym.st_name);
8843 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
8844 ((bfd_byte *) symbuf
8845 + (elfsym->dest_index
8846 * bed->s->sizeof_sym)),
8847 (flinfo->symshndxbuf
8848 + elfsym->destshndx_index));
8849 }
8850
8851 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
8852 pos = hdr->sh_offset + hdr->sh_size;
8853 amt = hash_table->strtabcount * bed->s->sizeof_sym;
8854 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
8855 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
8856 {
8857 hdr->sh_size += amt;
8858 ret = TRUE;
8859 }
8860 else
8861 ret = FALSE;
8862
8863 free (symbuf);
8864
8865 free (hash_table->strtab);
8866 hash_table->strtab = NULL;
8867
8868 return ret;
8869 }
8870
8871 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8872
8873 static bfd_boolean
8874 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
8875 {
8876 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
8877 && sym->st_shndx < SHN_LORESERVE)
8878 {
8879 /* The gABI doesn't support dynamic symbols in output sections
8880 beyond 64k. */
8881 (*_bfd_error_handler)
8882 (_("%B: Too many sections: %d (>= %d)"),
8883 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
8884 bfd_set_error (bfd_error_nonrepresentable_section);
8885 return FALSE;
8886 }
8887 return TRUE;
8888 }
8889
8890 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8891 allowing an unsatisfied unversioned symbol in the DSO to match a
8892 versioned symbol that would normally require an explicit version.
8893 We also handle the case that a DSO references a hidden symbol
8894 which may be satisfied by a versioned symbol in another DSO. */
8895
8896 static bfd_boolean
8897 elf_link_check_versioned_symbol (struct bfd_link_info *info,
8898 const struct elf_backend_data *bed,
8899 struct elf_link_hash_entry *h)
8900 {
8901 bfd *abfd;
8902 struct elf_link_loaded_list *loaded;
8903
8904 if (!is_elf_hash_table (info->hash))
8905 return FALSE;
8906
8907 /* Check indirect symbol. */
8908 while (h->root.type == bfd_link_hash_indirect)
8909 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8910
8911 switch (h->root.type)
8912 {
8913 default:
8914 abfd = NULL;
8915 break;
8916
8917 case bfd_link_hash_undefined:
8918 case bfd_link_hash_undefweak:
8919 abfd = h->root.u.undef.abfd;
8920 if ((abfd->flags & DYNAMIC) == 0
8921 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
8922 return FALSE;
8923 break;
8924
8925 case bfd_link_hash_defined:
8926 case bfd_link_hash_defweak:
8927 abfd = h->root.u.def.section->owner;
8928 break;
8929
8930 case bfd_link_hash_common:
8931 abfd = h->root.u.c.p->section->owner;
8932 break;
8933 }
8934 BFD_ASSERT (abfd != NULL);
8935
8936 for (loaded = elf_hash_table (info)->loaded;
8937 loaded != NULL;
8938 loaded = loaded->next)
8939 {
8940 bfd *input;
8941 Elf_Internal_Shdr *hdr;
8942 bfd_size_type symcount;
8943 bfd_size_type extsymcount;
8944 bfd_size_type extsymoff;
8945 Elf_Internal_Shdr *versymhdr;
8946 Elf_Internal_Sym *isym;
8947 Elf_Internal_Sym *isymend;
8948 Elf_Internal_Sym *isymbuf;
8949 Elf_External_Versym *ever;
8950 Elf_External_Versym *extversym;
8951
8952 input = loaded->abfd;
8953
8954 /* We check each DSO for a possible hidden versioned definition. */
8955 if (input == abfd
8956 || (input->flags & DYNAMIC) == 0
8957 || elf_dynversym (input) == 0)
8958 continue;
8959
8960 hdr = &elf_tdata (input)->dynsymtab_hdr;
8961
8962 symcount = hdr->sh_size / bed->s->sizeof_sym;
8963 if (elf_bad_symtab (input))
8964 {
8965 extsymcount = symcount;
8966 extsymoff = 0;
8967 }
8968 else
8969 {
8970 extsymcount = symcount - hdr->sh_info;
8971 extsymoff = hdr->sh_info;
8972 }
8973
8974 if (extsymcount == 0)
8975 continue;
8976
8977 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
8978 NULL, NULL, NULL);
8979 if (isymbuf == NULL)
8980 return FALSE;
8981
8982 /* Read in any version definitions. */
8983 versymhdr = &elf_tdata (input)->dynversym_hdr;
8984 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
8985 if (extversym == NULL)
8986 goto error_ret;
8987
8988 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
8989 || (bfd_bread (extversym, versymhdr->sh_size, input)
8990 != versymhdr->sh_size))
8991 {
8992 free (extversym);
8993 error_ret:
8994 free (isymbuf);
8995 return FALSE;
8996 }
8997
8998 ever = extversym + extsymoff;
8999 isymend = isymbuf + extsymcount;
9000 for (isym = isymbuf; isym < isymend; isym++, ever++)
9001 {
9002 const char *name;
9003 Elf_Internal_Versym iver;
9004 unsigned short version_index;
9005
9006 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9007 || isym->st_shndx == SHN_UNDEF)
9008 continue;
9009
9010 name = bfd_elf_string_from_elf_section (input,
9011 hdr->sh_link,
9012 isym->st_name);
9013 if (strcmp (name, h->root.root.string) != 0)
9014 continue;
9015
9016 _bfd_elf_swap_versym_in (input, ever, &iver);
9017
9018 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9019 && !(h->def_regular
9020 && h->forced_local))
9021 {
9022 /* If we have a non-hidden versioned sym, then it should
9023 have provided a definition for the undefined sym unless
9024 it is defined in a non-shared object and forced local.
9025 */
9026 abort ();
9027 }
9028
9029 version_index = iver.vs_vers & VERSYM_VERSION;
9030 if (version_index == 1 || version_index == 2)
9031 {
9032 /* This is the base or first version. We can use it. */
9033 free (extversym);
9034 free (isymbuf);
9035 return TRUE;
9036 }
9037 }
9038
9039 free (extversym);
9040 free (isymbuf);
9041 }
9042
9043 return FALSE;
9044 }
9045
9046 /* Convert ELF common symbol TYPE. */
9047
9048 static int
9049 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9050 {
9051 /* Commom symbol can only appear in relocatable link. */
9052 if (!bfd_link_relocatable (info))
9053 abort ();
9054 switch (info->elf_stt_common)
9055 {
9056 case unchanged:
9057 break;
9058 case elf_stt_common:
9059 type = STT_COMMON;
9060 break;
9061 case no_elf_stt_common:
9062 type = STT_OBJECT;
9063 break;
9064 }
9065 return type;
9066 }
9067
9068 /* Add an external symbol to the symbol table. This is called from
9069 the hash table traversal routine. When generating a shared object,
9070 we go through the symbol table twice. The first time we output
9071 anything that might have been forced to local scope in a version
9072 script. The second time we output the symbols that are still
9073 global symbols. */
9074
9075 static bfd_boolean
9076 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9077 {
9078 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9079 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9080 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9081 bfd_boolean strip;
9082 Elf_Internal_Sym sym;
9083 asection *input_sec;
9084 const struct elf_backend_data *bed;
9085 long indx;
9086 int ret;
9087 unsigned int type;
9088 /* A symbol is bound locally if it is forced local or it is locally
9089 defined, hidden versioned, not referenced by shared library and
9090 not exported when linking executable. */
9091 bfd_boolean local_bind = (h->forced_local
9092 || (bfd_link_executable (flinfo->info)
9093 && !flinfo->info->export_dynamic
9094 && !h->dynamic
9095 && !h->ref_dynamic
9096 && h->def_regular
9097 && h->versioned == versioned_hidden));
9098
9099 if (h->root.type == bfd_link_hash_warning)
9100 {
9101 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9102 if (h->root.type == bfd_link_hash_new)
9103 return TRUE;
9104 }
9105
9106 /* Decide whether to output this symbol in this pass. */
9107 if (eoinfo->localsyms)
9108 {
9109 if (!local_bind)
9110 return TRUE;
9111 }
9112 else
9113 {
9114 if (local_bind)
9115 return TRUE;
9116 }
9117
9118 bed = get_elf_backend_data (flinfo->output_bfd);
9119
9120 if (h->root.type == bfd_link_hash_undefined)
9121 {
9122 /* If we have an undefined symbol reference here then it must have
9123 come from a shared library that is being linked in. (Undefined
9124 references in regular files have already been handled unless
9125 they are in unreferenced sections which are removed by garbage
9126 collection). */
9127 bfd_boolean ignore_undef = FALSE;
9128
9129 /* Some symbols may be special in that the fact that they're
9130 undefined can be safely ignored - let backend determine that. */
9131 if (bed->elf_backend_ignore_undef_symbol)
9132 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9133
9134 /* If we are reporting errors for this situation then do so now. */
9135 if (!ignore_undef
9136 && h->ref_dynamic
9137 && (!h->ref_regular || flinfo->info->gc_sections)
9138 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9139 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9140 {
9141 if (!(flinfo->info->callbacks->undefined_symbol
9142 (flinfo->info, h->root.root.string,
9143 h->ref_regular ? NULL : h->root.u.undef.abfd,
9144 NULL, 0,
9145 (flinfo->info->unresolved_syms_in_shared_libs
9146 == RM_GENERATE_ERROR))))
9147 {
9148 bfd_set_error (bfd_error_bad_value);
9149 eoinfo->failed = TRUE;
9150 return FALSE;
9151 }
9152 }
9153 }
9154
9155 /* We should also warn if a forced local symbol is referenced from
9156 shared libraries. */
9157 if (bfd_link_executable (flinfo->info)
9158 && h->forced_local
9159 && h->ref_dynamic
9160 && h->def_regular
9161 && !h->dynamic_def
9162 && h->ref_dynamic_nonweak
9163 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9164 {
9165 bfd *def_bfd;
9166 const char *msg;
9167 struct elf_link_hash_entry *hi = h;
9168
9169 /* Check indirect symbol. */
9170 while (hi->root.type == bfd_link_hash_indirect)
9171 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9172
9173 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9174 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9175 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9176 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9177 else
9178 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
9179 def_bfd = flinfo->output_bfd;
9180 if (hi->root.u.def.section != bfd_abs_section_ptr)
9181 def_bfd = hi->root.u.def.section->owner;
9182 (*_bfd_error_handler) (msg, flinfo->output_bfd, def_bfd,
9183 h->root.root.string);
9184 bfd_set_error (bfd_error_bad_value);
9185 eoinfo->failed = TRUE;
9186 return FALSE;
9187 }
9188
9189 /* We don't want to output symbols that have never been mentioned by
9190 a regular file, or that we have been told to strip. However, if
9191 h->indx is set to -2, the symbol is used by a reloc and we must
9192 output it. */
9193 strip = FALSE;
9194 if (h->indx == -2)
9195 ;
9196 else if ((h->def_dynamic
9197 || h->ref_dynamic
9198 || h->root.type == bfd_link_hash_new)
9199 && !h->def_regular
9200 && !h->ref_regular)
9201 strip = TRUE;
9202 else if (flinfo->info->strip == strip_all)
9203 strip = TRUE;
9204 else if (flinfo->info->strip == strip_some
9205 && bfd_hash_lookup (flinfo->info->keep_hash,
9206 h->root.root.string, FALSE, FALSE) == NULL)
9207 strip = TRUE;
9208 else if ((h->root.type == bfd_link_hash_defined
9209 || h->root.type == bfd_link_hash_defweak)
9210 && ((flinfo->info->strip_discarded
9211 && discarded_section (h->root.u.def.section))
9212 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9213 && h->root.u.def.section->owner != NULL
9214 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9215 strip = TRUE;
9216 else if ((h->root.type == bfd_link_hash_undefined
9217 || h->root.type == bfd_link_hash_undefweak)
9218 && h->root.u.undef.abfd != NULL
9219 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9220 strip = TRUE;
9221
9222 type = h->type;
9223
9224 /* If we're stripping it, and it's not a dynamic symbol, there's
9225 nothing else to do. However, if it is a forced local symbol or
9226 an ifunc symbol we need to give the backend finish_dynamic_symbol
9227 function a chance to make it dynamic. */
9228 if (strip
9229 && h->dynindx == -1
9230 && type != STT_GNU_IFUNC
9231 && !h->forced_local)
9232 return TRUE;
9233
9234 sym.st_value = 0;
9235 sym.st_size = h->size;
9236 sym.st_other = h->other;
9237 switch (h->root.type)
9238 {
9239 default:
9240 case bfd_link_hash_new:
9241 case bfd_link_hash_warning:
9242 abort ();
9243 return FALSE;
9244
9245 case bfd_link_hash_undefined:
9246 case bfd_link_hash_undefweak:
9247 input_sec = bfd_und_section_ptr;
9248 sym.st_shndx = SHN_UNDEF;
9249 break;
9250
9251 case bfd_link_hash_defined:
9252 case bfd_link_hash_defweak:
9253 {
9254 input_sec = h->root.u.def.section;
9255 if (input_sec->output_section != NULL)
9256 {
9257 sym.st_shndx =
9258 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9259 input_sec->output_section);
9260 if (sym.st_shndx == SHN_BAD)
9261 {
9262 (*_bfd_error_handler)
9263 (_("%B: could not find output section %A for input section %A"),
9264 flinfo->output_bfd, input_sec->output_section, input_sec);
9265 bfd_set_error (bfd_error_nonrepresentable_section);
9266 eoinfo->failed = TRUE;
9267 return FALSE;
9268 }
9269
9270 /* ELF symbols in relocatable files are section relative,
9271 but in nonrelocatable files they are virtual
9272 addresses. */
9273 sym.st_value = h->root.u.def.value + input_sec->output_offset;
9274 if (!bfd_link_relocatable (flinfo->info))
9275 {
9276 sym.st_value += input_sec->output_section->vma;
9277 if (h->type == STT_TLS)
9278 {
9279 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9280 if (tls_sec != NULL)
9281 sym.st_value -= tls_sec->vma;
9282 }
9283 }
9284 }
9285 else
9286 {
9287 BFD_ASSERT (input_sec->owner == NULL
9288 || (input_sec->owner->flags & DYNAMIC) != 0);
9289 sym.st_shndx = SHN_UNDEF;
9290 input_sec = bfd_und_section_ptr;
9291 }
9292 }
9293 break;
9294
9295 case bfd_link_hash_common:
9296 input_sec = h->root.u.c.p->section;
9297 sym.st_shndx = bed->common_section_index (input_sec);
9298 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9299 break;
9300
9301 case bfd_link_hash_indirect:
9302 /* These symbols are created by symbol versioning. They point
9303 to the decorated version of the name. For example, if the
9304 symbol foo@@GNU_1.2 is the default, which should be used when
9305 foo is used with no version, then we add an indirect symbol
9306 foo which points to foo@@GNU_1.2. We ignore these symbols,
9307 since the indirected symbol is already in the hash table. */
9308 return TRUE;
9309 }
9310
9311 if (type == STT_COMMON || type == STT_OBJECT)
9312 switch (h->root.type)
9313 {
9314 case bfd_link_hash_common:
9315 type = elf_link_convert_common_type (flinfo->info, type);
9316 break;
9317 case bfd_link_hash_defined:
9318 case bfd_link_hash_defweak:
9319 if (bed->common_definition (&sym))
9320 type = elf_link_convert_common_type (flinfo->info, type);
9321 else
9322 type = STT_OBJECT;
9323 break;
9324 case bfd_link_hash_undefined:
9325 case bfd_link_hash_undefweak:
9326 break;
9327 default:
9328 abort ();
9329 }
9330
9331 if (local_bind)
9332 {
9333 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9334 /* Turn off visibility on local symbol. */
9335 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9336 }
9337 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9338 else if (h->unique_global && h->def_regular)
9339 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9340 else if (h->root.type == bfd_link_hash_undefweak
9341 || h->root.type == bfd_link_hash_defweak)
9342 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9343 else
9344 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9345 sym.st_target_internal = h->target_internal;
9346
9347 /* Give the processor backend a chance to tweak the symbol value,
9348 and also to finish up anything that needs to be done for this
9349 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9350 forced local syms when non-shared is due to a historical quirk.
9351 STT_GNU_IFUNC symbol must go through PLT. */
9352 if ((h->type == STT_GNU_IFUNC
9353 && h->def_regular
9354 && !bfd_link_relocatable (flinfo->info))
9355 || ((h->dynindx != -1
9356 || h->forced_local)
9357 && ((bfd_link_pic (flinfo->info)
9358 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9359 || h->root.type != bfd_link_hash_undefweak))
9360 || !h->forced_local)
9361 && elf_hash_table (flinfo->info)->dynamic_sections_created))
9362 {
9363 if (! ((*bed->elf_backend_finish_dynamic_symbol)
9364 (flinfo->output_bfd, flinfo->info, h, &sym)))
9365 {
9366 eoinfo->failed = TRUE;
9367 return FALSE;
9368 }
9369 }
9370
9371 /* If we are marking the symbol as undefined, and there are no
9372 non-weak references to this symbol from a regular object, then
9373 mark the symbol as weak undefined; if there are non-weak
9374 references, mark the symbol as strong. We can't do this earlier,
9375 because it might not be marked as undefined until the
9376 finish_dynamic_symbol routine gets through with it. */
9377 if (sym.st_shndx == SHN_UNDEF
9378 && h->ref_regular
9379 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9380 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9381 {
9382 int bindtype;
9383 type = ELF_ST_TYPE (sym.st_info);
9384
9385 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9386 if (type == STT_GNU_IFUNC)
9387 type = STT_FUNC;
9388
9389 if (h->ref_regular_nonweak)
9390 bindtype = STB_GLOBAL;
9391 else
9392 bindtype = STB_WEAK;
9393 sym.st_info = ELF_ST_INFO (bindtype, type);
9394 }
9395
9396 /* If this is a symbol defined in a dynamic library, don't use the
9397 symbol size from the dynamic library. Relinking an executable
9398 against a new library may introduce gratuitous changes in the
9399 executable's symbols if we keep the size. */
9400 if (sym.st_shndx == SHN_UNDEF
9401 && !h->def_regular
9402 && h->def_dynamic)
9403 sym.st_size = 0;
9404
9405 /* If a non-weak symbol with non-default visibility is not defined
9406 locally, it is a fatal error. */
9407 if (!bfd_link_relocatable (flinfo->info)
9408 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9409 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9410 && h->root.type == bfd_link_hash_undefined
9411 && !h->def_regular)
9412 {
9413 const char *msg;
9414
9415 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9416 msg = _("%B: protected symbol `%s' isn't defined");
9417 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9418 msg = _("%B: internal symbol `%s' isn't defined");
9419 else
9420 msg = _("%B: hidden symbol `%s' isn't defined");
9421 (*_bfd_error_handler) (msg, flinfo->output_bfd, h->root.root.string);
9422 bfd_set_error (bfd_error_bad_value);
9423 eoinfo->failed = TRUE;
9424 return FALSE;
9425 }
9426
9427 /* If this symbol should be put in the .dynsym section, then put it
9428 there now. We already know the symbol index. We also fill in
9429 the entry in the .hash section. */
9430 if (elf_hash_table (flinfo->info)->dynsym != NULL
9431 && h->dynindx != -1
9432 && elf_hash_table (flinfo->info)->dynamic_sections_created)
9433 {
9434 bfd_byte *esym;
9435
9436 /* Since there is no version information in the dynamic string,
9437 if there is no version info in symbol version section, we will
9438 have a run-time problem if not linking executable, referenced
9439 by shared library, not locally defined, or not bound locally.
9440 */
9441 if (h->verinfo.verdef == NULL
9442 && !local_bind
9443 && (!bfd_link_executable (flinfo->info)
9444 || h->ref_dynamic
9445 || !h->def_regular))
9446 {
9447 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9448
9449 if (p && p [1] != '\0')
9450 {
9451 (*_bfd_error_handler)
9452 (_("%B: No symbol version section for versioned symbol `%s'"),
9453 flinfo->output_bfd, h->root.root.string);
9454 eoinfo->failed = TRUE;
9455 return FALSE;
9456 }
9457 }
9458
9459 sym.st_name = h->dynstr_index;
9460 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9461 + h->dynindx * bed->s->sizeof_sym);
9462 if (!check_dynsym (flinfo->output_bfd, &sym))
9463 {
9464 eoinfo->failed = TRUE;
9465 return FALSE;
9466 }
9467 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
9468
9469 if (flinfo->hash_sec != NULL)
9470 {
9471 size_t hash_entry_size;
9472 bfd_byte *bucketpos;
9473 bfd_vma chain;
9474 size_t bucketcount;
9475 size_t bucket;
9476
9477 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
9478 bucket = h->u.elf_hash_value % bucketcount;
9479
9480 hash_entry_size
9481 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9482 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
9483 + (bucket + 2) * hash_entry_size);
9484 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9485 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9486 bucketpos);
9487 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9488 ((bfd_byte *) flinfo->hash_sec->contents
9489 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9490 }
9491
9492 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
9493 {
9494 Elf_Internal_Versym iversym;
9495 Elf_External_Versym *eversym;
9496
9497 if (!h->def_regular)
9498 {
9499 if (h->verinfo.verdef == NULL
9500 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9501 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
9502 iversym.vs_vers = 0;
9503 else
9504 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9505 }
9506 else
9507 {
9508 if (h->verinfo.vertree == NULL)
9509 iversym.vs_vers = 1;
9510 else
9511 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
9512 if (flinfo->info->create_default_symver)
9513 iversym.vs_vers++;
9514 }
9515
9516 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9517 defined locally. */
9518 if (h->versioned == versioned_hidden && h->def_regular)
9519 iversym.vs_vers |= VERSYM_HIDDEN;
9520
9521 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
9522 eversym += h->dynindx;
9523 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
9524 }
9525 }
9526
9527 /* If the symbol is undefined, and we didn't output it to .dynsym,
9528 strip it from .symtab too. Obviously we can't do this for
9529 relocatable output or when needed for --emit-relocs. */
9530 else if (input_sec == bfd_und_section_ptr
9531 && h->indx != -2
9532 && !bfd_link_relocatable (flinfo->info))
9533 return TRUE;
9534 /* Also strip others that we couldn't earlier due to dynamic symbol
9535 processing. */
9536 if (strip)
9537 return TRUE;
9538 if ((input_sec->flags & SEC_EXCLUDE) != 0)
9539 return TRUE;
9540
9541 /* Output a FILE symbol so that following locals are not associated
9542 with the wrong input file. We need one for forced local symbols
9543 if we've seen more than one FILE symbol or when we have exactly
9544 one FILE symbol but global symbols are present in a file other
9545 than the one with the FILE symbol. We also need one if linker
9546 defined symbols are present. In practice these conditions are
9547 always met, so just emit the FILE symbol unconditionally. */
9548 if (eoinfo->localsyms
9549 && !eoinfo->file_sym_done
9550 && eoinfo->flinfo->filesym_count != 0)
9551 {
9552 Elf_Internal_Sym fsym;
9553
9554 memset (&fsym, 0, sizeof (fsym));
9555 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9556 fsym.st_shndx = SHN_ABS;
9557 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9558 bfd_und_section_ptr, NULL))
9559 return FALSE;
9560
9561 eoinfo->file_sym_done = TRUE;
9562 }
9563
9564 indx = bfd_get_symcount (flinfo->output_bfd);
9565 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9566 input_sec, h);
9567 if (ret == 0)
9568 {
9569 eoinfo->failed = TRUE;
9570 return FALSE;
9571 }
9572 else if (ret == 1)
9573 h->indx = indx;
9574 else if (h->indx == -2)
9575 abort();
9576
9577 return TRUE;
9578 }
9579
9580 /* Return TRUE if special handling is done for relocs in SEC against
9581 symbols defined in discarded sections. */
9582
9583 static bfd_boolean
9584 elf_section_ignore_discarded_relocs (asection *sec)
9585 {
9586 const struct elf_backend_data *bed;
9587
9588 switch (sec->sec_info_type)
9589 {
9590 case SEC_INFO_TYPE_STABS:
9591 case SEC_INFO_TYPE_EH_FRAME:
9592 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
9593 return TRUE;
9594 default:
9595 break;
9596 }
9597
9598 bed = get_elf_backend_data (sec->owner);
9599 if (bed->elf_backend_ignore_discarded_relocs != NULL
9600 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9601 return TRUE;
9602
9603 return FALSE;
9604 }
9605
9606 /* Return a mask saying how ld should treat relocations in SEC against
9607 symbols defined in discarded sections. If this function returns
9608 COMPLAIN set, ld will issue a warning message. If this function
9609 returns PRETEND set, and the discarded section was link-once and the
9610 same size as the kept link-once section, ld will pretend that the
9611 symbol was actually defined in the kept section. Otherwise ld will
9612 zero the reloc (at least that is the intent, but some cooperation by
9613 the target dependent code is needed, particularly for REL targets). */
9614
9615 unsigned int
9616 _bfd_elf_default_action_discarded (asection *sec)
9617 {
9618 if (sec->flags & SEC_DEBUGGING)
9619 return PRETEND;
9620
9621 if (strcmp (".eh_frame", sec->name) == 0)
9622 return 0;
9623
9624 if (strcmp (".gcc_except_table", sec->name) == 0)
9625 return 0;
9626
9627 return COMPLAIN | PRETEND;
9628 }
9629
9630 /* Find a match between a section and a member of a section group. */
9631
9632 static asection *
9633 match_group_member (asection *sec, asection *group,
9634 struct bfd_link_info *info)
9635 {
9636 asection *first = elf_next_in_group (group);
9637 asection *s = first;
9638
9639 while (s != NULL)
9640 {
9641 if (bfd_elf_match_symbols_in_sections (s, sec, info))
9642 return s;
9643
9644 s = elf_next_in_group (s);
9645 if (s == first)
9646 break;
9647 }
9648
9649 return NULL;
9650 }
9651
9652 /* Check if the kept section of a discarded section SEC can be used
9653 to replace it. Return the replacement if it is OK. Otherwise return
9654 NULL. */
9655
9656 asection *
9657 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
9658 {
9659 asection *kept;
9660
9661 kept = sec->kept_section;
9662 if (kept != NULL)
9663 {
9664 if ((kept->flags & SEC_GROUP) != 0)
9665 kept = match_group_member (sec, kept, info);
9666 if (kept != NULL
9667 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9668 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
9669 kept = NULL;
9670 sec->kept_section = kept;
9671 }
9672 return kept;
9673 }
9674
9675 /* Link an input file into the linker output file. This function
9676 handles all the sections and relocations of the input file at once.
9677 This is so that we only have to read the local symbols once, and
9678 don't have to keep them in memory. */
9679
9680 static bfd_boolean
9681 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
9682 {
9683 int (*relocate_section)
9684 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9685 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9686 bfd *output_bfd;
9687 Elf_Internal_Shdr *symtab_hdr;
9688 size_t locsymcount;
9689 size_t extsymoff;
9690 Elf_Internal_Sym *isymbuf;
9691 Elf_Internal_Sym *isym;
9692 Elf_Internal_Sym *isymend;
9693 long *pindex;
9694 asection **ppsection;
9695 asection *o;
9696 const struct elf_backend_data *bed;
9697 struct elf_link_hash_entry **sym_hashes;
9698 bfd_size_type address_size;
9699 bfd_vma r_type_mask;
9700 int r_sym_shift;
9701 bfd_boolean have_file_sym = FALSE;
9702
9703 output_bfd = flinfo->output_bfd;
9704 bed = get_elf_backend_data (output_bfd);
9705 relocate_section = bed->elf_backend_relocate_section;
9706
9707 /* If this is a dynamic object, we don't want to do anything here:
9708 we don't want the local symbols, and we don't want the section
9709 contents. */
9710 if ((input_bfd->flags & DYNAMIC) != 0)
9711 return TRUE;
9712
9713 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9714 if (elf_bad_symtab (input_bfd))
9715 {
9716 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9717 extsymoff = 0;
9718 }
9719 else
9720 {
9721 locsymcount = symtab_hdr->sh_info;
9722 extsymoff = symtab_hdr->sh_info;
9723 }
9724
9725 /* Read the local symbols. */
9726 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9727 if (isymbuf == NULL && locsymcount != 0)
9728 {
9729 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
9730 flinfo->internal_syms,
9731 flinfo->external_syms,
9732 flinfo->locsym_shndx);
9733 if (isymbuf == NULL)
9734 return FALSE;
9735 }
9736
9737 /* Find local symbol sections and adjust values of symbols in
9738 SEC_MERGE sections. Write out those local symbols we know are
9739 going into the output file. */
9740 isymend = isymbuf + locsymcount;
9741 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
9742 isym < isymend;
9743 isym++, pindex++, ppsection++)
9744 {
9745 asection *isec;
9746 const char *name;
9747 Elf_Internal_Sym osym;
9748 long indx;
9749 int ret;
9750
9751 *pindex = -1;
9752
9753 if (elf_bad_symtab (input_bfd))
9754 {
9755 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9756 {
9757 *ppsection = NULL;
9758 continue;
9759 }
9760 }
9761
9762 if (isym->st_shndx == SHN_UNDEF)
9763 isec = bfd_und_section_ptr;
9764 else if (isym->st_shndx == SHN_ABS)
9765 isec = bfd_abs_section_ptr;
9766 else if (isym->st_shndx == SHN_COMMON)
9767 isec = bfd_com_section_ptr;
9768 else
9769 {
9770 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9771 if (isec == NULL)
9772 {
9773 /* Don't attempt to output symbols with st_shnx in the
9774 reserved range other than SHN_ABS and SHN_COMMON. */
9775 *ppsection = NULL;
9776 continue;
9777 }
9778 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
9779 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9780 isym->st_value =
9781 _bfd_merged_section_offset (output_bfd, &isec,
9782 elf_section_data (isec)->sec_info,
9783 isym->st_value);
9784 }
9785
9786 *ppsection = isec;
9787
9788 /* Don't output the first, undefined, symbol. In fact, don't
9789 output any undefined local symbol. */
9790 if (isec == bfd_und_section_ptr)
9791 continue;
9792
9793 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9794 {
9795 /* We never output section symbols. Instead, we use the
9796 section symbol of the corresponding section in the output
9797 file. */
9798 continue;
9799 }
9800
9801 /* If we are stripping all symbols, we don't want to output this
9802 one. */
9803 if (flinfo->info->strip == strip_all)
9804 continue;
9805
9806 /* If we are discarding all local symbols, we don't want to
9807 output this one. If we are generating a relocatable output
9808 file, then some of the local symbols may be required by
9809 relocs; we output them below as we discover that they are
9810 needed. */
9811 if (flinfo->info->discard == discard_all)
9812 continue;
9813
9814 /* If this symbol is defined in a section which we are
9815 discarding, we don't need to keep it. */
9816 if (isym->st_shndx != SHN_UNDEF
9817 && isym->st_shndx < SHN_LORESERVE
9818 && bfd_section_removed_from_list (output_bfd,
9819 isec->output_section))
9820 continue;
9821
9822 /* Get the name of the symbol. */
9823 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9824 isym->st_name);
9825 if (name == NULL)
9826 return FALSE;
9827
9828 /* See if we are discarding symbols with this name. */
9829 if ((flinfo->info->strip == strip_some
9830 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
9831 == NULL))
9832 || (((flinfo->info->discard == discard_sec_merge
9833 && (isec->flags & SEC_MERGE)
9834 && !bfd_link_relocatable (flinfo->info))
9835 || flinfo->info->discard == discard_l)
9836 && bfd_is_local_label_name (input_bfd, name)))
9837 continue;
9838
9839 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
9840 {
9841 if (input_bfd->lto_output)
9842 /* -flto puts a temp file name here. This means builds
9843 are not reproducible. Discard the symbol. */
9844 continue;
9845 have_file_sym = TRUE;
9846 flinfo->filesym_count += 1;
9847 }
9848 if (!have_file_sym)
9849 {
9850 /* In the absence of debug info, bfd_find_nearest_line uses
9851 FILE symbols to determine the source file for local
9852 function symbols. Provide a FILE symbol here if input
9853 files lack such, so that their symbols won't be
9854 associated with a previous input file. It's not the
9855 source file, but the best we can do. */
9856 have_file_sym = TRUE;
9857 flinfo->filesym_count += 1;
9858 memset (&osym, 0, sizeof (osym));
9859 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9860 osym.st_shndx = SHN_ABS;
9861 if (!elf_link_output_symstrtab (flinfo,
9862 (input_bfd->lto_output ? NULL
9863 : input_bfd->filename),
9864 &osym, bfd_abs_section_ptr,
9865 NULL))
9866 return FALSE;
9867 }
9868
9869 osym = *isym;
9870
9871 /* Adjust the section index for the output file. */
9872 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9873 isec->output_section);
9874 if (osym.st_shndx == SHN_BAD)
9875 return FALSE;
9876
9877 /* ELF symbols in relocatable files are section relative, but
9878 in executable files they are virtual addresses. Note that
9879 this code assumes that all ELF sections have an associated
9880 BFD section with a reasonable value for output_offset; below
9881 we assume that they also have a reasonable value for
9882 output_section. Any special sections must be set up to meet
9883 these requirements. */
9884 osym.st_value += isec->output_offset;
9885 if (!bfd_link_relocatable (flinfo->info))
9886 {
9887 osym.st_value += isec->output_section->vma;
9888 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
9889 {
9890 /* STT_TLS symbols are relative to PT_TLS segment base. */
9891 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
9892 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
9893 }
9894 }
9895
9896 indx = bfd_get_symcount (output_bfd);
9897 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
9898 if (ret == 0)
9899 return FALSE;
9900 else if (ret == 1)
9901 *pindex = indx;
9902 }
9903
9904 if (bed->s->arch_size == 32)
9905 {
9906 r_type_mask = 0xff;
9907 r_sym_shift = 8;
9908 address_size = 4;
9909 }
9910 else
9911 {
9912 r_type_mask = 0xffffffff;
9913 r_sym_shift = 32;
9914 address_size = 8;
9915 }
9916
9917 /* Relocate the contents of each section. */
9918 sym_hashes = elf_sym_hashes (input_bfd);
9919 for (o = input_bfd->sections; o != NULL; o = o->next)
9920 {
9921 bfd_byte *contents;
9922
9923 if (! o->linker_mark)
9924 {
9925 /* This section was omitted from the link. */
9926 continue;
9927 }
9928
9929 if (bfd_link_relocatable (flinfo->info)
9930 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
9931 {
9932 /* Deal with the group signature symbol. */
9933 struct bfd_elf_section_data *sec_data = elf_section_data (o);
9934 unsigned long symndx = sec_data->this_hdr.sh_info;
9935 asection *osec = o->output_section;
9936
9937 if (symndx >= locsymcount
9938 || (elf_bad_symtab (input_bfd)
9939 && flinfo->sections[symndx] == NULL))
9940 {
9941 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
9942 while (h->root.type == bfd_link_hash_indirect
9943 || h->root.type == bfd_link_hash_warning)
9944 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9945 /* Arrange for symbol to be output. */
9946 h->indx = -2;
9947 elf_section_data (osec)->this_hdr.sh_info = -2;
9948 }
9949 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
9950 {
9951 /* We'll use the output section target_index. */
9952 asection *sec = flinfo->sections[symndx]->output_section;
9953 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
9954 }
9955 else
9956 {
9957 if (flinfo->indices[symndx] == -1)
9958 {
9959 /* Otherwise output the local symbol now. */
9960 Elf_Internal_Sym sym = isymbuf[symndx];
9961 asection *sec = flinfo->sections[symndx]->output_section;
9962 const char *name;
9963 long indx;
9964 int ret;
9965
9966 name = bfd_elf_string_from_elf_section (input_bfd,
9967 symtab_hdr->sh_link,
9968 sym.st_name);
9969 if (name == NULL)
9970 return FALSE;
9971
9972 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9973 sec);
9974 if (sym.st_shndx == SHN_BAD)
9975 return FALSE;
9976
9977 sym.st_value += o->output_offset;
9978
9979 indx = bfd_get_symcount (output_bfd);
9980 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
9981 NULL);
9982 if (ret == 0)
9983 return FALSE;
9984 else if (ret == 1)
9985 flinfo->indices[symndx] = indx;
9986 else
9987 abort ();
9988 }
9989 elf_section_data (osec)->this_hdr.sh_info
9990 = flinfo->indices[symndx];
9991 }
9992 }
9993
9994 if ((o->flags & SEC_HAS_CONTENTS) == 0
9995 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
9996 continue;
9997
9998 if ((o->flags & SEC_LINKER_CREATED) != 0)
9999 {
10000 /* Section was created by _bfd_elf_link_create_dynamic_sections
10001 or somesuch. */
10002 continue;
10003 }
10004
10005 /* Get the contents of the section. They have been cached by a
10006 relaxation routine. Note that o is a section in an input
10007 file, so the contents field will not have been set by any of
10008 the routines which work on output files. */
10009 if (elf_section_data (o)->this_hdr.contents != NULL)
10010 {
10011 contents = elf_section_data (o)->this_hdr.contents;
10012 if (bed->caches_rawsize
10013 && o->rawsize != 0
10014 && o->rawsize < o->size)
10015 {
10016 memcpy (flinfo->contents, contents, o->rawsize);
10017 contents = flinfo->contents;
10018 }
10019 }
10020 else
10021 {
10022 contents = flinfo->contents;
10023 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10024 return FALSE;
10025 }
10026
10027 if ((o->flags & SEC_RELOC) != 0)
10028 {
10029 Elf_Internal_Rela *internal_relocs;
10030 Elf_Internal_Rela *rel, *relend;
10031 int action_discarded;
10032 int ret;
10033
10034 /* Get the swapped relocs. */
10035 internal_relocs
10036 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10037 flinfo->internal_relocs, FALSE);
10038 if (internal_relocs == NULL
10039 && o->reloc_count > 0)
10040 return FALSE;
10041
10042 /* We need to reverse-copy input .ctors/.dtors sections if
10043 they are placed in .init_array/.finit_array for output. */
10044 if (o->size > address_size
10045 && ((strncmp (o->name, ".ctors", 6) == 0
10046 && strcmp (o->output_section->name,
10047 ".init_array") == 0)
10048 || (strncmp (o->name, ".dtors", 6) == 0
10049 && strcmp (o->output_section->name,
10050 ".fini_array") == 0))
10051 && (o->name[6] == 0 || o->name[6] == '.'))
10052 {
10053 if (o->size != o->reloc_count * address_size)
10054 {
10055 (*_bfd_error_handler)
10056 (_("error: %B: size of section %A is not "
10057 "multiple of address size"),
10058 input_bfd, o);
10059 bfd_set_error (bfd_error_on_input);
10060 return FALSE;
10061 }
10062 o->flags |= SEC_ELF_REVERSE_COPY;
10063 }
10064
10065 action_discarded = -1;
10066 if (!elf_section_ignore_discarded_relocs (o))
10067 action_discarded = (*bed->action_discarded) (o);
10068
10069 /* Run through the relocs evaluating complex reloc symbols and
10070 looking for relocs against symbols from discarded sections
10071 or section symbols from removed link-once sections.
10072 Complain about relocs against discarded sections. Zero
10073 relocs against removed link-once sections. */
10074
10075 rel = internal_relocs;
10076 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
10077 for ( ; rel < relend; rel++)
10078 {
10079 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10080 unsigned int s_type;
10081 asection **ps, *sec;
10082 struct elf_link_hash_entry *h = NULL;
10083 const char *sym_name;
10084
10085 if (r_symndx == STN_UNDEF)
10086 continue;
10087
10088 if (r_symndx >= locsymcount
10089 || (elf_bad_symtab (input_bfd)
10090 && flinfo->sections[r_symndx] == NULL))
10091 {
10092 h = sym_hashes[r_symndx - extsymoff];
10093
10094 /* Badly formatted input files can contain relocs that
10095 reference non-existant symbols. Check here so that
10096 we do not seg fault. */
10097 if (h == NULL)
10098 {
10099 char buffer [32];
10100
10101 sprintf_vma (buffer, rel->r_info);
10102 (*_bfd_error_handler)
10103 (_("error: %B contains a reloc (0x%s) for section %A "
10104 "that references a non-existent global symbol"),
10105 input_bfd, o, buffer);
10106 bfd_set_error (bfd_error_bad_value);
10107 return FALSE;
10108 }
10109
10110 while (h->root.type == bfd_link_hash_indirect
10111 || h->root.type == bfd_link_hash_warning)
10112 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10113
10114 s_type = h->type;
10115
10116 /* If a plugin symbol is referenced from a non-IR file,
10117 mark the symbol as undefined. Note that the
10118 linker may attach linker created dynamic sections
10119 to the plugin bfd. Symbols defined in linker
10120 created sections are not plugin symbols. */
10121 if (h->root.non_ir_ref
10122 && (h->root.type == bfd_link_hash_defined
10123 || h->root.type == bfd_link_hash_defweak)
10124 && (h->root.u.def.section->flags
10125 & SEC_LINKER_CREATED) == 0
10126 && h->root.u.def.section->owner != NULL
10127 && (h->root.u.def.section->owner->flags
10128 & BFD_PLUGIN) != 0)
10129 {
10130 h->root.type = bfd_link_hash_undefined;
10131 h->root.u.undef.abfd = h->root.u.def.section->owner;
10132 }
10133
10134 ps = NULL;
10135 if (h->root.type == bfd_link_hash_defined
10136 || h->root.type == bfd_link_hash_defweak)
10137 ps = &h->root.u.def.section;
10138
10139 sym_name = h->root.root.string;
10140 }
10141 else
10142 {
10143 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10144
10145 s_type = ELF_ST_TYPE (sym->st_info);
10146 ps = &flinfo->sections[r_symndx];
10147 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10148 sym, *ps);
10149 }
10150
10151 if ((s_type == STT_RELC || s_type == STT_SRELC)
10152 && !bfd_link_relocatable (flinfo->info))
10153 {
10154 bfd_vma val;
10155 bfd_vma dot = (rel->r_offset
10156 + o->output_offset + o->output_section->vma);
10157 #ifdef DEBUG
10158 printf ("Encountered a complex symbol!");
10159 printf (" (input_bfd %s, section %s, reloc %ld\n",
10160 input_bfd->filename, o->name,
10161 (long) (rel - internal_relocs));
10162 printf (" symbol: idx %8.8lx, name %s\n",
10163 r_symndx, sym_name);
10164 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10165 (unsigned long) rel->r_info,
10166 (unsigned long) rel->r_offset);
10167 #endif
10168 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10169 isymbuf, locsymcount, s_type == STT_SRELC))
10170 return FALSE;
10171
10172 /* Symbol evaluated OK. Update to absolute value. */
10173 set_symbol_value (input_bfd, isymbuf, locsymcount,
10174 r_symndx, val);
10175 continue;
10176 }
10177
10178 if (action_discarded != -1 && ps != NULL)
10179 {
10180 /* Complain if the definition comes from a
10181 discarded section. */
10182 if ((sec = *ps) != NULL && discarded_section (sec))
10183 {
10184 BFD_ASSERT (r_symndx != STN_UNDEF);
10185 if (action_discarded & COMPLAIN)
10186 (*flinfo->info->callbacks->einfo)
10187 (_("%X`%s' referenced in section `%A' of %B: "
10188 "defined in discarded section `%A' of %B\n"),
10189 sym_name, o, input_bfd, sec, sec->owner);
10190
10191 /* Try to do the best we can to support buggy old
10192 versions of gcc. Pretend that the symbol is
10193 really defined in the kept linkonce section.
10194 FIXME: This is quite broken. Modifying the
10195 symbol here means we will be changing all later
10196 uses of the symbol, not just in this section. */
10197 if (action_discarded & PRETEND)
10198 {
10199 asection *kept;
10200
10201 kept = _bfd_elf_check_kept_section (sec,
10202 flinfo->info);
10203 if (kept != NULL)
10204 {
10205 *ps = kept;
10206 continue;
10207 }
10208 }
10209 }
10210 }
10211 }
10212
10213 /* Relocate the section by invoking a back end routine.
10214
10215 The back end routine is responsible for adjusting the
10216 section contents as necessary, and (if using Rela relocs
10217 and generating a relocatable output file) adjusting the
10218 reloc addend as necessary.
10219
10220 The back end routine does not have to worry about setting
10221 the reloc address or the reloc symbol index.
10222
10223 The back end routine is given a pointer to the swapped in
10224 internal symbols, and can access the hash table entries
10225 for the external symbols via elf_sym_hashes (input_bfd).
10226
10227 When generating relocatable output, the back end routine
10228 must handle STB_LOCAL/STT_SECTION symbols specially. The
10229 output symbol is going to be a section symbol
10230 corresponding to the output section, which will require
10231 the addend to be adjusted. */
10232
10233 ret = (*relocate_section) (output_bfd, flinfo->info,
10234 input_bfd, o, contents,
10235 internal_relocs,
10236 isymbuf,
10237 flinfo->sections);
10238 if (!ret)
10239 return FALSE;
10240
10241 if (ret == 2
10242 || bfd_link_relocatable (flinfo->info)
10243 || flinfo->info->emitrelocations)
10244 {
10245 Elf_Internal_Rela *irela;
10246 Elf_Internal_Rela *irelaend, *irelamid;
10247 bfd_vma last_offset;
10248 struct elf_link_hash_entry **rel_hash;
10249 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10250 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10251 unsigned int next_erel;
10252 bfd_boolean rela_normal;
10253 struct bfd_elf_section_data *esdi, *esdo;
10254
10255 esdi = elf_section_data (o);
10256 esdo = elf_section_data (o->output_section);
10257 rela_normal = FALSE;
10258
10259 /* Adjust the reloc addresses and symbol indices. */
10260
10261 irela = internal_relocs;
10262 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
10263 rel_hash = esdo->rel.hashes + esdo->rel.count;
10264 /* We start processing the REL relocs, if any. When we reach
10265 IRELAMID in the loop, we switch to the RELA relocs. */
10266 irelamid = irela;
10267 if (esdi->rel.hdr != NULL)
10268 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10269 * bed->s->int_rels_per_ext_rel);
10270 rel_hash_list = rel_hash;
10271 rela_hash_list = NULL;
10272 last_offset = o->output_offset;
10273 if (!bfd_link_relocatable (flinfo->info))
10274 last_offset += o->output_section->vma;
10275 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10276 {
10277 unsigned long r_symndx;
10278 asection *sec;
10279 Elf_Internal_Sym sym;
10280
10281 if (next_erel == bed->s->int_rels_per_ext_rel)
10282 {
10283 rel_hash++;
10284 next_erel = 0;
10285 }
10286
10287 if (irela == irelamid)
10288 {
10289 rel_hash = esdo->rela.hashes + esdo->rela.count;
10290 rela_hash_list = rel_hash;
10291 rela_normal = bed->rela_normal;
10292 }
10293
10294 irela->r_offset = _bfd_elf_section_offset (output_bfd,
10295 flinfo->info, o,
10296 irela->r_offset);
10297 if (irela->r_offset >= (bfd_vma) -2)
10298 {
10299 /* This is a reloc for a deleted entry or somesuch.
10300 Turn it into an R_*_NONE reloc, at the same
10301 offset as the last reloc. elf_eh_frame.c and
10302 bfd_elf_discard_info rely on reloc offsets
10303 being ordered. */
10304 irela->r_offset = last_offset;
10305 irela->r_info = 0;
10306 irela->r_addend = 0;
10307 continue;
10308 }
10309
10310 irela->r_offset += o->output_offset;
10311
10312 /* Relocs in an executable have to be virtual addresses. */
10313 if (!bfd_link_relocatable (flinfo->info))
10314 irela->r_offset += o->output_section->vma;
10315
10316 last_offset = irela->r_offset;
10317
10318 r_symndx = irela->r_info >> r_sym_shift;
10319 if (r_symndx == STN_UNDEF)
10320 continue;
10321
10322 if (r_symndx >= locsymcount
10323 || (elf_bad_symtab (input_bfd)
10324 && flinfo->sections[r_symndx] == NULL))
10325 {
10326 struct elf_link_hash_entry *rh;
10327 unsigned long indx;
10328
10329 /* This is a reloc against a global symbol. We
10330 have not yet output all the local symbols, so
10331 we do not know the symbol index of any global
10332 symbol. We set the rel_hash entry for this
10333 reloc to point to the global hash table entry
10334 for this symbol. The symbol index is then
10335 set at the end of bfd_elf_final_link. */
10336 indx = r_symndx - extsymoff;
10337 rh = elf_sym_hashes (input_bfd)[indx];
10338 while (rh->root.type == bfd_link_hash_indirect
10339 || rh->root.type == bfd_link_hash_warning)
10340 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10341
10342 /* Setting the index to -2 tells
10343 elf_link_output_extsym that this symbol is
10344 used by a reloc. */
10345 BFD_ASSERT (rh->indx < 0);
10346 rh->indx = -2;
10347
10348 *rel_hash = rh;
10349
10350 continue;
10351 }
10352
10353 /* This is a reloc against a local symbol. */
10354
10355 *rel_hash = NULL;
10356 sym = isymbuf[r_symndx];
10357 sec = flinfo->sections[r_symndx];
10358 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10359 {
10360 /* I suppose the backend ought to fill in the
10361 section of any STT_SECTION symbol against a
10362 processor specific section. */
10363 r_symndx = STN_UNDEF;
10364 if (bfd_is_abs_section (sec))
10365 ;
10366 else if (sec == NULL || sec->owner == NULL)
10367 {
10368 bfd_set_error (bfd_error_bad_value);
10369 return FALSE;
10370 }
10371 else
10372 {
10373 asection *osec = sec->output_section;
10374
10375 /* If we have discarded a section, the output
10376 section will be the absolute section. In
10377 case of discarded SEC_MERGE sections, use
10378 the kept section. relocate_section should
10379 have already handled discarded linkonce
10380 sections. */
10381 if (bfd_is_abs_section (osec)
10382 && sec->kept_section != NULL
10383 && sec->kept_section->output_section != NULL)
10384 {
10385 osec = sec->kept_section->output_section;
10386 irela->r_addend -= osec->vma;
10387 }
10388
10389 if (!bfd_is_abs_section (osec))
10390 {
10391 r_symndx = osec->target_index;
10392 if (r_symndx == STN_UNDEF)
10393 {
10394 irela->r_addend += osec->vma;
10395 osec = _bfd_nearby_section (output_bfd, osec,
10396 osec->vma);
10397 irela->r_addend -= osec->vma;
10398 r_symndx = osec->target_index;
10399 }
10400 }
10401 }
10402
10403 /* Adjust the addend according to where the
10404 section winds up in the output section. */
10405 if (rela_normal)
10406 irela->r_addend += sec->output_offset;
10407 }
10408 else
10409 {
10410 if (flinfo->indices[r_symndx] == -1)
10411 {
10412 unsigned long shlink;
10413 const char *name;
10414 asection *osec;
10415 long indx;
10416
10417 if (flinfo->info->strip == strip_all)
10418 {
10419 /* You can't do ld -r -s. */
10420 bfd_set_error (bfd_error_invalid_operation);
10421 return FALSE;
10422 }
10423
10424 /* This symbol was skipped earlier, but
10425 since it is needed by a reloc, we
10426 must output it now. */
10427 shlink = symtab_hdr->sh_link;
10428 name = (bfd_elf_string_from_elf_section
10429 (input_bfd, shlink, sym.st_name));
10430 if (name == NULL)
10431 return FALSE;
10432
10433 osec = sec->output_section;
10434 sym.st_shndx =
10435 _bfd_elf_section_from_bfd_section (output_bfd,
10436 osec);
10437 if (sym.st_shndx == SHN_BAD)
10438 return FALSE;
10439
10440 sym.st_value += sec->output_offset;
10441 if (!bfd_link_relocatable (flinfo->info))
10442 {
10443 sym.st_value += osec->vma;
10444 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10445 {
10446 /* STT_TLS symbols are relative to PT_TLS
10447 segment base. */
10448 BFD_ASSERT (elf_hash_table (flinfo->info)
10449 ->tls_sec != NULL);
10450 sym.st_value -= (elf_hash_table (flinfo->info)
10451 ->tls_sec->vma);
10452 }
10453 }
10454
10455 indx = bfd_get_symcount (output_bfd);
10456 ret = elf_link_output_symstrtab (flinfo, name,
10457 &sym, sec,
10458 NULL);
10459 if (ret == 0)
10460 return FALSE;
10461 else if (ret == 1)
10462 flinfo->indices[r_symndx] = indx;
10463 else
10464 abort ();
10465 }
10466
10467 r_symndx = flinfo->indices[r_symndx];
10468 }
10469
10470 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10471 | (irela->r_info & r_type_mask));
10472 }
10473
10474 /* Swap out the relocs. */
10475 input_rel_hdr = esdi->rel.hdr;
10476 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
10477 {
10478 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10479 input_rel_hdr,
10480 internal_relocs,
10481 rel_hash_list))
10482 return FALSE;
10483 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10484 * bed->s->int_rels_per_ext_rel);
10485 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
10486 }
10487
10488 input_rela_hdr = esdi->rela.hdr;
10489 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10490 {
10491 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10492 input_rela_hdr,
10493 internal_relocs,
10494 rela_hash_list))
10495 return FALSE;
10496 }
10497 }
10498 }
10499
10500 /* Write out the modified section contents. */
10501 if (bed->elf_backend_write_section
10502 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
10503 contents))
10504 {
10505 /* Section written out. */
10506 }
10507 else switch (o->sec_info_type)
10508 {
10509 case SEC_INFO_TYPE_STABS:
10510 if (! (_bfd_write_section_stabs
10511 (output_bfd,
10512 &elf_hash_table (flinfo->info)->stab_info,
10513 o, &elf_section_data (o)->sec_info, contents)))
10514 return FALSE;
10515 break;
10516 case SEC_INFO_TYPE_MERGE:
10517 if (! _bfd_write_merged_section (output_bfd, o,
10518 elf_section_data (o)->sec_info))
10519 return FALSE;
10520 break;
10521 case SEC_INFO_TYPE_EH_FRAME:
10522 {
10523 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
10524 o, contents))
10525 return FALSE;
10526 }
10527 break;
10528 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10529 {
10530 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10531 flinfo->info,
10532 o, contents))
10533 return FALSE;
10534 }
10535 break;
10536 default:
10537 {
10538 /* FIXME: octets_per_byte. */
10539 if (! (o->flags & SEC_EXCLUDE))
10540 {
10541 file_ptr offset = (file_ptr) o->output_offset;
10542 bfd_size_type todo = o->size;
10543 if ((o->flags & SEC_ELF_REVERSE_COPY))
10544 {
10545 /* Reverse-copy input section to output. */
10546 do
10547 {
10548 todo -= address_size;
10549 if (! bfd_set_section_contents (output_bfd,
10550 o->output_section,
10551 contents + todo,
10552 offset,
10553 address_size))
10554 return FALSE;
10555 if (todo == 0)
10556 break;
10557 offset += address_size;
10558 }
10559 while (1);
10560 }
10561 else if (! bfd_set_section_contents (output_bfd,
10562 o->output_section,
10563 contents,
10564 offset, todo))
10565 return FALSE;
10566 }
10567 }
10568 break;
10569 }
10570 }
10571
10572 return TRUE;
10573 }
10574
10575 /* Generate a reloc when linking an ELF file. This is a reloc
10576 requested by the linker, and does not come from any input file. This
10577 is used to build constructor and destructor tables when linking
10578 with -Ur. */
10579
10580 static bfd_boolean
10581 elf_reloc_link_order (bfd *output_bfd,
10582 struct bfd_link_info *info,
10583 asection *output_section,
10584 struct bfd_link_order *link_order)
10585 {
10586 reloc_howto_type *howto;
10587 long indx;
10588 bfd_vma offset;
10589 bfd_vma addend;
10590 struct bfd_elf_section_reloc_data *reldata;
10591 struct elf_link_hash_entry **rel_hash_ptr;
10592 Elf_Internal_Shdr *rel_hdr;
10593 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10594 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10595 bfd_byte *erel;
10596 unsigned int i;
10597 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
10598
10599 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10600 if (howto == NULL)
10601 {
10602 bfd_set_error (bfd_error_bad_value);
10603 return FALSE;
10604 }
10605
10606 addend = link_order->u.reloc.p->addend;
10607
10608 if (esdo->rel.hdr)
10609 reldata = &esdo->rel;
10610 else if (esdo->rela.hdr)
10611 reldata = &esdo->rela;
10612 else
10613 {
10614 reldata = NULL;
10615 BFD_ASSERT (0);
10616 }
10617
10618 /* Figure out the symbol index. */
10619 rel_hash_ptr = reldata->hashes + reldata->count;
10620 if (link_order->type == bfd_section_reloc_link_order)
10621 {
10622 indx = link_order->u.reloc.p->u.section->target_index;
10623 BFD_ASSERT (indx != 0);
10624 *rel_hash_ptr = NULL;
10625 }
10626 else
10627 {
10628 struct elf_link_hash_entry *h;
10629
10630 /* Treat a reloc against a defined symbol as though it were
10631 actually against the section. */
10632 h = ((struct elf_link_hash_entry *)
10633 bfd_wrapped_link_hash_lookup (output_bfd, info,
10634 link_order->u.reloc.p->u.name,
10635 FALSE, FALSE, TRUE));
10636 if (h != NULL
10637 && (h->root.type == bfd_link_hash_defined
10638 || h->root.type == bfd_link_hash_defweak))
10639 {
10640 asection *section;
10641
10642 section = h->root.u.def.section;
10643 indx = section->output_section->target_index;
10644 *rel_hash_ptr = NULL;
10645 /* It seems that we ought to add the symbol value to the
10646 addend here, but in practice it has already been added
10647 because it was passed to constructor_callback. */
10648 addend += section->output_section->vma + section->output_offset;
10649 }
10650 else if (h != NULL)
10651 {
10652 /* Setting the index to -2 tells elf_link_output_extsym that
10653 this symbol is used by a reloc. */
10654 h->indx = -2;
10655 *rel_hash_ptr = h;
10656 indx = 0;
10657 }
10658 else
10659 {
10660 if (! ((*info->callbacks->unattached_reloc)
10661 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
10662 return FALSE;
10663 indx = 0;
10664 }
10665 }
10666
10667 /* If this is an inplace reloc, we must write the addend into the
10668 object file. */
10669 if (howto->partial_inplace && addend != 0)
10670 {
10671 bfd_size_type size;
10672 bfd_reloc_status_type rstat;
10673 bfd_byte *buf;
10674 bfd_boolean ok;
10675 const char *sym_name;
10676
10677 size = (bfd_size_type) bfd_get_reloc_size (howto);
10678 buf = (bfd_byte *) bfd_zmalloc (size);
10679 if (buf == NULL && size != 0)
10680 return FALSE;
10681 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10682 switch (rstat)
10683 {
10684 case bfd_reloc_ok:
10685 break;
10686
10687 default:
10688 case bfd_reloc_outofrange:
10689 abort ();
10690
10691 case bfd_reloc_overflow:
10692 if (link_order->type == bfd_section_reloc_link_order)
10693 sym_name = bfd_section_name (output_bfd,
10694 link_order->u.reloc.p->u.section);
10695 else
10696 sym_name = link_order->u.reloc.p->u.name;
10697 if (! ((*info->callbacks->reloc_overflow)
10698 (info, NULL, sym_name, howto->name, addend, NULL,
10699 NULL, (bfd_vma) 0)))
10700 {
10701 free (buf);
10702 return FALSE;
10703 }
10704 break;
10705 }
10706 ok = bfd_set_section_contents (output_bfd, output_section, buf,
10707 link_order->offset, size);
10708 free (buf);
10709 if (! ok)
10710 return FALSE;
10711 }
10712
10713 /* The address of a reloc is relative to the section in a
10714 relocatable file, and is a virtual address in an executable
10715 file. */
10716 offset = link_order->offset;
10717 if (! bfd_link_relocatable (info))
10718 offset += output_section->vma;
10719
10720 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10721 {
10722 irel[i].r_offset = offset;
10723 irel[i].r_info = 0;
10724 irel[i].r_addend = 0;
10725 }
10726 if (bed->s->arch_size == 32)
10727 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10728 else
10729 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10730
10731 rel_hdr = reldata->hdr;
10732 erel = rel_hdr->contents;
10733 if (rel_hdr->sh_type == SHT_REL)
10734 {
10735 erel += reldata->count * bed->s->sizeof_rel;
10736 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10737 }
10738 else
10739 {
10740 irel[0].r_addend = addend;
10741 erel += reldata->count * bed->s->sizeof_rela;
10742 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10743 }
10744
10745 ++reldata->count;
10746
10747 return TRUE;
10748 }
10749
10750
10751 /* Get the output vma of the section pointed to by the sh_link field. */
10752
10753 static bfd_vma
10754 elf_get_linked_section_vma (struct bfd_link_order *p)
10755 {
10756 Elf_Internal_Shdr **elf_shdrp;
10757 asection *s;
10758 int elfsec;
10759
10760 s = p->u.indirect.section;
10761 elf_shdrp = elf_elfsections (s->owner);
10762 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10763 elfsec = elf_shdrp[elfsec]->sh_link;
10764 /* PR 290:
10765 The Intel C compiler generates SHT_IA_64_UNWIND with
10766 SHF_LINK_ORDER. But it doesn't set the sh_link or
10767 sh_info fields. Hence we could get the situation
10768 where elfsec is 0. */
10769 if (elfsec == 0)
10770 {
10771 const struct elf_backend_data *bed
10772 = get_elf_backend_data (s->owner);
10773 if (bed->link_order_error_handler)
10774 bed->link_order_error_handler
10775 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
10776 return 0;
10777 }
10778 else
10779 {
10780 s = elf_shdrp[elfsec]->bfd_section;
10781 return s->output_section->vma + s->output_offset;
10782 }
10783 }
10784
10785
10786 /* Compare two sections based on the locations of the sections they are
10787 linked to. Used by elf_fixup_link_order. */
10788
10789 static int
10790 compare_link_order (const void * a, const void * b)
10791 {
10792 bfd_vma apos;
10793 bfd_vma bpos;
10794
10795 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10796 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10797 if (apos < bpos)
10798 return -1;
10799 return apos > bpos;
10800 }
10801
10802
10803 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10804 order as their linked sections. Returns false if this could not be done
10805 because an output section includes both ordered and unordered
10806 sections. Ideally we'd do this in the linker proper. */
10807
10808 static bfd_boolean
10809 elf_fixup_link_order (bfd *abfd, asection *o)
10810 {
10811 int seen_linkorder;
10812 int seen_other;
10813 int n;
10814 struct bfd_link_order *p;
10815 bfd *sub;
10816 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10817 unsigned elfsec;
10818 struct bfd_link_order **sections;
10819 asection *s, *other_sec, *linkorder_sec;
10820 bfd_vma offset;
10821
10822 other_sec = NULL;
10823 linkorder_sec = NULL;
10824 seen_other = 0;
10825 seen_linkorder = 0;
10826 for (p = o->map_head.link_order; p != NULL; p = p->next)
10827 {
10828 if (p->type == bfd_indirect_link_order)
10829 {
10830 s = p->u.indirect.section;
10831 sub = s->owner;
10832 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10833 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
10834 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
10835 && elfsec < elf_numsections (sub)
10836 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
10837 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
10838 {
10839 seen_linkorder++;
10840 linkorder_sec = s;
10841 }
10842 else
10843 {
10844 seen_other++;
10845 other_sec = s;
10846 }
10847 }
10848 else
10849 seen_other++;
10850
10851 if (seen_other && seen_linkorder)
10852 {
10853 if (other_sec && linkorder_sec)
10854 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
10855 o, linkorder_sec,
10856 linkorder_sec->owner, other_sec,
10857 other_sec->owner);
10858 else
10859 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
10860 o);
10861 bfd_set_error (bfd_error_bad_value);
10862 return FALSE;
10863 }
10864 }
10865
10866 if (!seen_linkorder)
10867 return TRUE;
10868
10869 sections = (struct bfd_link_order **)
10870 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
10871 if (sections == NULL)
10872 return FALSE;
10873 seen_linkorder = 0;
10874
10875 for (p = o->map_head.link_order; p != NULL; p = p->next)
10876 {
10877 sections[seen_linkorder++] = p;
10878 }
10879 /* Sort the input sections in the order of their linked section. */
10880 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
10881 compare_link_order);
10882
10883 /* Change the offsets of the sections. */
10884 offset = 0;
10885 for (n = 0; n < seen_linkorder; n++)
10886 {
10887 s = sections[n]->u.indirect.section;
10888 offset &= ~(bfd_vma) 0 << s->alignment_power;
10889 s->output_offset = offset;
10890 sections[n]->offset = offset;
10891 /* FIXME: octets_per_byte. */
10892 offset += sections[n]->size;
10893 }
10894
10895 free (sections);
10896 return TRUE;
10897 }
10898
10899 static void
10900 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
10901 {
10902 asection *o;
10903
10904 if (flinfo->symstrtab != NULL)
10905 _bfd_elf_strtab_free (flinfo->symstrtab);
10906 if (flinfo->contents != NULL)
10907 free (flinfo->contents);
10908 if (flinfo->external_relocs != NULL)
10909 free (flinfo->external_relocs);
10910 if (flinfo->internal_relocs != NULL)
10911 free (flinfo->internal_relocs);
10912 if (flinfo->external_syms != NULL)
10913 free (flinfo->external_syms);
10914 if (flinfo->locsym_shndx != NULL)
10915 free (flinfo->locsym_shndx);
10916 if (flinfo->internal_syms != NULL)
10917 free (flinfo->internal_syms);
10918 if (flinfo->indices != NULL)
10919 free (flinfo->indices);
10920 if (flinfo->sections != NULL)
10921 free (flinfo->sections);
10922 if (flinfo->symshndxbuf != NULL)
10923 free (flinfo->symshndxbuf);
10924 for (o = obfd->sections; o != NULL; o = o->next)
10925 {
10926 struct bfd_elf_section_data *esdo = elf_section_data (o);
10927 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
10928 free (esdo->rel.hashes);
10929 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
10930 free (esdo->rela.hashes);
10931 }
10932 }
10933
10934 /* Do the final step of an ELF link. */
10935
10936 bfd_boolean
10937 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
10938 {
10939 bfd_boolean dynamic;
10940 bfd_boolean emit_relocs;
10941 bfd *dynobj;
10942 struct elf_final_link_info flinfo;
10943 asection *o;
10944 struct bfd_link_order *p;
10945 bfd *sub;
10946 bfd_size_type max_contents_size;
10947 bfd_size_type max_external_reloc_size;
10948 bfd_size_type max_internal_reloc_count;
10949 bfd_size_type max_sym_count;
10950 bfd_size_type max_sym_shndx_count;
10951 Elf_Internal_Sym elfsym;
10952 unsigned int i;
10953 Elf_Internal_Shdr *symtab_hdr;
10954 Elf_Internal_Shdr *symtab_shndx_hdr;
10955 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10956 struct elf_outext_info eoinfo;
10957 bfd_boolean merged;
10958 size_t relativecount = 0;
10959 asection *reldyn = 0;
10960 bfd_size_type amt;
10961 asection *attr_section = NULL;
10962 bfd_vma attr_size = 0;
10963 const char *std_attrs_section;
10964
10965 if (! is_elf_hash_table (info->hash))
10966 return FALSE;
10967
10968 if (bfd_link_pic (info))
10969 abfd->flags |= DYNAMIC;
10970
10971 dynamic = elf_hash_table (info)->dynamic_sections_created;
10972 dynobj = elf_hash_table (info)->dynobj;
10973
10974 emit_relocs = (bfd_link_relocatable (info)
10975 || info->emitrelocations);
10976
10977 flinfo.info = info;
10978 flinfo.output_bfd = abfd;
10979 flinfo.symstrtab = _bfd_elf_strtab_init ();
10980 if (flinfo.symstrtab == NULL)
10981 return FALSE;
10982
10983 if (! dynamic)
10984 {
10985 flinfo.hash_sec = NULL;
10986 flinfo.symver_sec = NULL;
10987 }
10988 else
10989 {
10990 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
10991 /* Note that dynsym_sec can be NULL (on VMS). */
10992 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
10993 /* Note that it is OK if symver_sec is NULL. */
10994 }
10995
10996 flinfo.contents = NULL;
10997 flinfo.external_relocs = NULL;
10998 flinfo.internal_relocs = NULL;
10999 flinfo.external_syms = NULL;
11000 flinfo.locsym_shndx = NULL;
11001 flinfo.internal_syms = NULL;
11002 flinfo.indices = NULL;
11003 flinfo.sections = NULL;
11004 flinfo.symshndxbuf = NULL;
11005 flinfo.filesym_count = 0;
11006
11007 /* The object attributes have been merged. Remove the input
11008 sections from the link, and set the contents of the output
11009 secton. */
11010 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11011 for (o = abfd->sections; o != NULL; o = o->next)
11012 {
11013 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11014 || strcmp (o->name, ".gnu.attributes") == 0)
11015 {
11016 for (p = o->map_head.link_order; p != NULL; p = p->next)
11017 {
11018 asection *input_section;
11019
11020 if (p->type != bfd_indirect_link_order)
11021 continue;
11022 input_section = p->u.indirect.section;
11023 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11024 elf_link_input_bfd ignores this section. */
11025 input_section->flags &= ~SEC_HAS_CONTENTS;
11026 }
11027
11028 attr_size = bfd_elf_obj_attr_size (abfd);
11029 if (attr_size)
11030 {
11031 bfd_set_section_size (abfd, o, attr_size);
11032 attr_section = o;
11033 /* Skip this section later on. */
11034 o->map_head.link_order = NULL;
11035 }
11036 else
11037 o->flags |= SEC_EXCLUDE;
11038 }
11039 }
11040
11041 /* Count up the number of relocations we will output for each output
11042 section, so that we know the sizes of the reloc sections. We
11043 also figure out some maximum sizes. */
11044 max_contents_size = 0;
11045 max_external_reloc_size = 0;
11046 max_internal_reloc_count = 0;
11047 max_sym_count = 0;
11048 max_sym_shndx_count = 0;
11049 merged = FALSE;
11050 for (o = abfd->sections; o != NULL; o = o->next)
11051 {
11052 struct bfd_elf_section_data *esdo = elf_section_data (o);
11053 o->reloc_count = 0;
11054
11055 for (p = o->map_head.link_order; p != NULL; p = p->next)
11056 {
11057 unsigned int reloc_count = 0;
11058 unsigned int additional_reloc_count = 0;
11059 struct bfd_elf_section_data *esdi = NULL;
11060
11061 if (p->type == bfd_section_reloc_link_order
11062 || p->type == bfd_symbol_reloc_link_order)
11063 reloc_count = 1;
11064 else if (p->type == bfd_indirect_link_order)
11065 {
11066 asection *sec;
11067
11068 sec = p->u.indirect.section;
11069 esdi = elf_section_data (sec);
11070
11071 /* Mark all sections which are to be included in the
11072 link. This will normally be every section. We need
11073 to do this so that we can identify any sections which
11074 the linker has decided to not include. */
11075 sec->linker_mark = TRUE;
11076
11077 if (sec->flags & SEC_MERGE)
11078 merged = TRUE;
11079
11080 if (esdo->this_hdr.sh_type == SHT_REL
11081 || esdo->this_hdr.sh_type == SHT_RELA)
11082 /* Some backends use reloc_count in relocation sections
11083 to count particular types of relocs. Of course,
11084 reloc sections themselves can't have relocations. */
11085 reloc_count = 0;
11086 else if (emit_relocs)
11087 {
11088 reloc_count = sec->reloc_count;
11089 if (bed->elf_backend_count_additional_relocs)
11090 {
11091 int c;
11092 c = (*bed->elf_backend_count_additional_relocs) (sec);
11093 additional_reloc_count += c;
11094 }
11095 }
11096 else if (bed->elf_backend_count_relocs)
11097 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11098
11099 if (sec->rawsize > max_contents_size)
11100 max_contents_size = sec->rawsize;
11101 if (sec->size > max_contents_size)
11102 max_contents_size = sec->size;
11103
11104 /* We are interested in just local symbols, not all
11105 symbols. */
11106 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11107 && (sec->owner->flags & DYNAMIC) == 0)
11108 {
11109 size_t sym_count;
11110
11111 if (elf_bad_symtab (sec->owner))
11112 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11113 / bed->s->sizeof_sym);
11114 else
11115 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11116
11117 if (sym_count > max_sym_count)
11118 max_sym_count = sym_count;
11119
11120 if (sym_count > max_sym_shndx_count
11121 && elf_symtab_shndx_list (sec->owner) != NULL)
11122 max_sym_shndx_count = sym_count;
11123
11124 if ((sec->flags & SEC_RELOC) != 0)
11125 {
11126 size_t ext_size = 0;
11127
11128 if (esdi->rel.hdr != NULL)
11129 ext_size = esdi->rel.hdr->sh_size;
11130 if (esdi->rela.hdr != NULL)
11131 ext_size += esdi->rela.hdr->sh_size;
11132
11133 if (ext_size > max_external_reloc_size)
11134 max_external_reloc_size = ext_size;
11135 if (sec->reloc_count > max_internal_reloc_count)
11136 max_internal_reloc_count = sec->reloc_count;
11137 }
11138 }
11139 }
11140
11141 if (reloc_count == 0)
11142 continue;
11143
11144 reloc_count += additional_reloc_count;
11145 o->reloc_count += reloc_count;
11146
11147 if (p->type == bfd_indirect_link_order && emit_relocs)
11148 {
11149 if (esdi->rel.hdr)
11150 {
11151 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11152 esdo->rel.count += additional_reloc_count;
11153 }
11154 if (esdi->rela.hdr)
11155 {
11156 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11157 esdo->rela.count += additional_reloc_count;
11158 }
11159 }
11160 else
11161 {
11162 if (o->use_rela_p)
11163 esdo->rela.count += reloc_count;
11164 else
11165 esdo->rel.count += reloc_count;
11166 }
11167 }
11168
11169 if (o->reloc_count > 0)
11170 o->flags |= SEC_RELOC;
11171 else
11172 {
11173 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11174 set it (this is probably a bug) and if it is set
11175 assign_section_numbers will create a reloc section. */
11176 o->flags &=~ SEC_RELOC;
11177 }
11178
11179 /* If the SEC_ALLOC flag is not set, force the section VMA to
11180 zero. This is done in elf_fake_sections as well, but forcing
11181 the VMA to 0 here will ensure that relocs against these
11182 sections are handled correctly. */
11183 if ((o->flags & SEC_ALLOC) == 0
11184 && ! o->user_set_vma)
11185 o->vma = 0;
11186 }
11187
11188 if (! bfd_link_relocatable (info) && merged)
11189 elf_link_hash_traverse (elf_hash_table (info),
11190 _bfd_elf_link_sec_merge_syms, abfd);
11191
11192 /* Figure out the file positions for everything but the symbol table
11193 and the relocs. We set symcount to force assign_section_numbers
11194 to create a symbol table. */
11195 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
11196 BFD_ASSERT (! abfd->output_has_begun);
11197 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11198 goto error_return;
11199
11200 /* Set sizes, and assign file positions for reloc sections. */
11201 for (o = abfd->sections; o != NULL; o = o->next)
11202 {
11203 struct bfd_elf_section_data *esdo = elf_section_data (o);
11204 if ((o->flags & SEC_RELOC) != 0)
11205 {
11206 if (esdo->rel.hdr
11207 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
11208 goto error_return;
11209
11210 if (esdo->rela.hdr
11211 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
11212 goto error_return;
11213 }
11214
11215 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11216 to count upwards while actually outputting the relocations. */
11217 esdo->rel.count = 0;
11218 esdo->rela.count = 0;
11219
11220 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11221 {
11222 /* Cache the section contents so that they can be compressed
11223 later. Use bfd_malloc since it will be freed by
11224 bfd_compress_section_contents. */
11225 unsigned char *contents = esdo->this_hdr.contents;
11226 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11227 abort ();
11228 contents
11229 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11230 if (contents == NULL)
11231 goto error_return;
11232 esdo->this_hdr.contents = contents;
11233 }
11234 }
11235
11236 /* We have now assigned file positions for all the sections except
11237 .symtab, .strtab, and non-loaded reloc sections. We start the
11238 .symtab section at the current file position, and write directly
11239 to it. We build the .strtab section in memory. */
11240 bfd_get_symcount (abfd) = 0;
11241 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11242 /* sh_name is set in prep_headers. */
11243 symtab_hdr->sh_type = SHT_SYMTAB;
11244 /* sh_flags, sh_addr and sh_size all start off zero. */
11245 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11246 /* sh_link is set in assign_section_numbers. */
11247 /* sh_info is set below. */
11248 /* sh_offset is set just below. */
11249 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
11250
11251 if (max_sym_count < 20)
11252 max_sym_count = 20;
11253 elf_hash_table (info)->strtabsize = max_sym_count;
11254 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11255 elf_hash_table (info)->strtab
11256 = (struct elf_sym_strtab *) bfd_malloc (amt);
11257 if (elf_hash_table (info)->strtab == NULL)
11258 goto error_return;
11259 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11260 flinfo.symshndxbuf
11261 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11262 ? (Elf_External_Sym_Shndx *) -1 : NULL);
11263
11264 if (info->strip != strip_all || emit_relocs)
11265 {
11266 file_ptr off = elf_next_file_pos (abfd);
11267
11268 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11269
11270 /* Note that at this point elf_next_file_pos (abfd) is
11271 incorrect. We do not yet know the size of the .symtab section.
11272 We correct next_file_pos below, after we do know the size. */
11273
11274 /* Start writing out the symbol table. The first symbol is always a
11275 dummy symbol. */
11276 elfsym.st_value = 0;
11277 elfsym.st_size = 0;
11278 elfsym.st_info = 0;
11279 elfsym.st_other = 0;
11280 elfsym.st_shndx = SHN_UNDEF;
11281 elfsym.st_target_internal = 0;
11282 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11283 bfd_und_section_ptr, NULL) != 1)
11284 goto error_return;
11285
11286 /* Output a symbol for each section. We output these even if we are
11287 discarding local symbols, since they are used for relocs. These
11288 symbols have no names. We store the index of each one in the
11289 index field of the section, so that we can find it again when
11290 outputting relocs. */
11291
11292 elfsym.st_size = 0;
11293 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11294 elfsym.st_other = 0;
11295 elfsym.st_value = 0;
11296 elfsym.st_target_internal = 0;
11297 for (i = 1; i < elf_numsections (abfd); i++)
11298 {
11299 o = bfd_section_from_elf_index (abfd, i);
11300 if (o != NULL)
11301 {
11302 o->target_index = bfd_get_symcount (abfd);
11303 elfsym.st_shndx = i;
11304 if (!bfd_link_relocatable (info))
11305 elfsym.st_value = o->vma;
11306 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11307 NULL) != 1)
11308 goto error_return;
11309 }
11310 }
11311 }
11312
11313 /* Allocate some memory to hold information read in from the input
11314 files. */
11315 if (max_contents_size != 0)
11316 {
11317 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11318 if (flinfo.contents == NULL)
11319 goto error_return;
11320 }
11321
11322 if (max_external_reloc_size != 0)
11323 {
11324 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11325 if (flinfo.external_relocs == NULL)
11326 goto error_return;
11327 }
11328
11329 if (max_internal_reloc_count != 0)
11330 {
11331 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11332 amt *= sizeof (Elf_Internal_Rela);
11333 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11334 if (flinfo.internal_relocs == NULL)
11335 goto error_return;
11336 }
11337
11338 if (max_sym_count != 0)
11339 {
11340 amt = max_sym_count * bed->s->sizeof_sym;
11341 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11342 if (flinfo.external_syms == NULL)
11343 goto error_return;
11344
11345 amt = max_sym_count * sizeof (Elf_Internal_Sym);
11346 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11347 if (flinfo.internal_syms == NULL)
11348 goto error_return;
11349
11350 amt = max_sym_count * sizeof (long);
11351 flinfo.indices = (long int *) bfd_malloc (amt);
11352 if (flinfo.indices == NULL)
11353 goto error_return;
11354
11355 amt = max_sym_count * sizeof (asection *);
11356 flinfo.sections = (asection **) bfd_malloc (amt);
11357 if (flinfo.sections == NULL)
11358 goto error_return;
11359 }
11360
11361 if (max_sym_shndx_count != 0)
11362 {
11363 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
11364 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11365 if (flinfo.locsym_shndx == NULL)
11366 goto error_return;
11367 }
11368
11369 if (elf_hash_table (info)->tls_sec)
11370 {
11371 bfd_vma base, end = 0;
11372 asection *sec;
11373
11374 for (sec = elf_hash_table (info)->tls_sec;
11375 sec && (sec->flags & SEC_THREAD_LOCAL);
11376 sec = sec->next)
11377 {
11378 bfd_size_type size = sec->size;
11379
11380 if (size == 0
11381 && (sec->flags & SEC_HAS_CONTENTS) == 0)
11382 {
11383 struct bfd_link_order *ord = sec->map_tail.link_order;
11384
11385 if (ord != NULL)
11386 size = ord->offset + ord->size;
11387 }
11388 end = sec->vma + size;
11389 }
11390 base = elf_hash_table (info)->tls_sec->vma;
11391 /* Only align end of TLS section if static TLS doesn't have special
11392 alignment requirements. */
11393 if (bed->static_tls_alignment == 1)
11394 end = align_power (end,
11395 elf_hash_table (info)->tls_sec->alignment_power);
11396 elf_hash_table (info)->tls_size = end - base;
11397 }
11398
11399 /* Reorder SHF_LINK_ORDER sections. */
11400 for (o = abfd->sections; o != NULL; o = o->next)
11401 {
11402 if (!elf_fixup_link_order (abfd, o))
11403 return FALSE;
11404 }
11405
11406 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11407 return FALSE;
11408
11409 /* Since ELF permits relocations to be against local symbols, we
11410 must have the local symbols available when we do the relocations.
11411 Since we would rather only read the local symbols once, and we
11412 would rather not keep them in memory, we handle all the
11413 relocations for a single input file at the same time.
11414
11415 Unfortunately, there is no way to know the total number of local
11416 symbols until we have seen all of them, and the local symbol
11417 indices precede the global symbol indices. This means that when
11418 we are generating relocatable output, and we see a reloc against
11419 a global symbol, we can not know the symbol index until we have
11420 finished examining all the local symbols to see which ones we are
11421 going to output. To deal with this, we keep the relocations in
11422 memory, and don't output them until the end of the link. This is
11423 an unfortunate waste of memory, but I don't see a good way around
11424 it. Fortunately, it only happens when performing a relocatable
11425 link, which is not the common case. FIXME: If keep_memory is set
11426 we could write the relocs out and then read them again; I don't
11427 know how bad the memory loss will be. */
11428
11429 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11430 sub->output_has_begun = FALSE;
11431 for (o = abfd->sections; o != NULL; o = o->next)
11432 {
11433 for (p = o->map_head.link_order; p != NULL; p = p->next)
11434 {
11435 if (p->type == bfd_indirect_link_order
11436 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11437 == bfd_target_elf_flavour)
11438 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11439 {
11440 if (! sub->output_has_begun)
11441 {
11442 if (! elf_link_input_bfd (&flinfo, sub))
11443 goto error_return;
11444 sub->output_has_begun = TRUE;
11445 }
11446 }
11447 else if (p->type == bfd_section_reloc_link_order
11448 || p->type == bfd_symbol_reloc_link_order)
11449 {
11450 if (! elf_reloc_link_order (abfd, info, o, p))
11451 goto error_return;
11452 }
11453 else
11454 {
11455 if (! _bfd_default_link_order (abfd, info, o, p))
11456 {
11457 if (p->type == bfd_indirect_link_order
11458 && (bfd_get_flavour (sub)
11459 == bfd_target_elf_flavour)
11460 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11461 != bed->s->elfclass))
11462 {
11463 const char *iclass, *oclass;
11464
11465 switch (bed->s->elfclass)
11466 {
11467 case ELFCLASS64: oclass = "ELFCLASS64"; break;
11468 case ELFCLASS32: oclass = "ELFCLASS32"; break;
11469 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
11470 default: abort ();
11471 }
11472
11473 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
11474 {
11475 case ELFCLASS64: iclass = "ELFCLASS64"; break;
11476 case ELFCLASS32: iclass = "ELFCLASS32"; break;
11477 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
11478 default: abort ();
11479 }
11480
11481 bfd_set_error (bfd_error_wrong_format);
11482 (*_bfd_error_handler)
11483 (_("%B: file class %s incompatible with %s"),
11484 sub, iclass, oclass);
11485 }
11486
11487 goto error_return;
11488 }
11489 }
11490 }
11491 }
11492
11493 /* Free symbol buffer if needed. */
11494 if (!info->reduce_memory_overheads)
11495 {
11496 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11497 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11498 && elf_tdata (sub)->symbuf)
11499 {
11500 free (elf_tdata (sub)->symbuf);
11501 elf_tdata (sub)->symbuf = NULL;
11502 }
11503 }
11504
11505 /* Output any global symbols that got converted to local in a
11506 version script or due to symbol visibility. We do this in a
11507 separate step since ELF requires all local symbols to appear
11508 prior to any global symbols. FIXME: We should only do this if
11509 some global symbols were, in fact, converted to become local.
11510 FIXME: Will this work correctly with the Irix 5 linker? */
11511 eoinfo.failed = FALSE;
11512 eoinfo.flinfo = &flinfo;
11513 eoinfo.localsyms = TRUE;
11514 eoinfo.file_sym_done = FALSE;
11515 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11516 if (eoinfo.failed)
11517 return FALSE;
11518
11519 /* If backend needs to output some local symbols not present in the hash
11520 table, do it now. */
11521 if (bed->elf_backend_output_arch_local_syms
11522 && (info->strip != strip_all || emit_relocs))
11523 {
11524 typedef int (*out_sym_func)
11525 (void *, const char *, Elf_Internal_Sym *, asection *,
11526 struct elf_link_hash_entry *);
11527
11528 if (! ((*bed->elf_backend_output_arch_local_syms)
11529 (abfd, info, &flinfo,
11530 (out_sym_func) elf_link_output_symstrtab)))
11531 return FALSE;
11532 }
11533
11534 /* That wrote out all the local symbols. Finish up the symbol table
11535 with the global symbols. Even if we want to strip everything we
11536 can, we still need to deal with those global symbols that got
11537 converted to local in a version script. */
11538
11539 /* The sh_info field records the index of the first non local symbol. */
11540 symtab_hdr->sh_info = bfd_get_symcount (abfd);
11541
11542 if (dynamic
11543 && elf_hash_table (info)->dynsym != NULL
11544 && (elf_hash_table (info)->dynsym->output_section
11545 != bfd_abs_section_ptr))
11546 {
11547 Elf_Internal_Sym sym;
11548 bfd_byte *dynsym = elf_hash_table (info)->dynsym->contents;
11549 long last_local = 0;
11550
11551 /* Write out the section symbols for the output sections. */
11552 if (bfd_link_pic (info)
11553 || elf_hash_table (info)->is_relocatable_executable)
11554 {
11555 asection *s;
11556
11557 sym.st_size = 0;
11558 sym.st_name = 0;
11559 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11560 sym.st_other = 0;
11561 sym.st_target_internal = 0;
11562
11563 for (s = abfd->sections; s != NULL; s = s->next)
11564 {
11565 int indx;
11566 bfd_byte *dest;
11567 long dynindx;
11568
11569 dynindx = elf_section_data (s)->dynindx;
11570 if (dynindx <= 0)
11571 continue;
11572 indx = elf_section_data (s)->this_idx;
11573 BFD_ASSERT (indx > 0);
11574 sym.st_shndx = indx;
11575 if (! check_dynsym (abfd, &sym))
11576 return FALSE;
11577 sym.st_value = s->vma;
11578 dest = dynsym + dynindx * bed->s->sizeof_sym;
11579 if (last_local < dynindx)
11580 last_local = dynindx;
11581 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11582 }
11583 }
11584
11585 /* Write out the local dynsyms. */
11586 if (elf_hash_table (info)->dynlocal)
11587 {
11588 struct elf_link_local_dynamic_entry *e;
11589 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
11590 {
11591 asection *s;
11592 bfd_byte *dest;
11593
11594 /* Copy the internal symbol and turn off visibility.
11595 Note that we saved a word of storage and overwrote
11596 the original st_name with the dynstr_index. */
11597 sym = e->isym;
11598 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
11599
11600 s = bfd_section_from_elf_index (e->input_bfd,
11601 e->isym.st_shndx);
11602 if (s != NULL)
11603 {
11604 sym.st_shndx =
11605 elf_section_data (s->output_section)->this_idx;
11606 if (! check_dynsym (abfd, &sym))
11607 return FALSE;
11608 sym.st_value = (s->output_section->vma
11609 + s->output_offset
11610 + e->isym.st_value);
11611 }
11612
11613 if (last_local < e->dynindx)
11614 last_local = e->dynindx;
11615
11616 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
11617 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11618 }
11619 }
11620
11621 elf_section_data (elf_hash_table (info)->dynsym->output_section)->this_hdr.sh_info =
11622 last_local + 1;
11623 }
11624
11625 /* We get the global symbols from the hash table. */
11626 eoinfo.failed = FALSE;
11627 eoinfo.localsyms = FALSE;
11628 eoinfo.flinfo = &flinfo;
11629 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11630 if (eoinfo.failed)
11631 return FALSE;
11632
11633 /* If backend needs to output some symbols not present in the hash
11634 table, do it now. */
11635 if (bed->elf_backend_output_arch_syms
11636 && (info->strip != strip_all || emit_relocs))
11637 {
11638 typedef int (*out_sym_func)
11639 (void *, const char *, Elf_Internal_Sym *, asection *,
11640 struct elf_link_hash_entry *);
11641
11642 if (! ((*bed->elf_backend_output_arch_syms)
11643 (abfd, info, &flinfo,
11644 (out_sym_func) elf_link_output_symstrtab)))
11645 return FALSE;
11646 }
11647
11648 /* Finalize the .strtab section. */
11649 _bfd_elf_strtab_finalize (flinfo.symstrtab);
11650
11651 /* Swap out the .strtab section. */
11652 if (!elf_link_swap_symbols_out (&flinfo))
11653 return FALSE;
11654
11655 /* Now we know the size of the symtab section. */
11656 if (bfd_get_symcount (abfd) > 0)
11657 {
11658 /* Finish up and write out the symbol string table (.strtab)
11659 section. */
11660 Elf_Internal_Shdr *symstrtab_hdr;
11661 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
11662
11663 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
11664 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
11665 {
11666 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
11667 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
11668 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
11669 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
11670 symtab_shndx_hdr->sh_size = amt;
11671
11672 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
11673 off, TRUE);
11674
11675 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
11676 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
11677 return FALSE;
11678 }
11679
11680 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
11681 /* sh_name was set in prep_headers. */
11682 symstrtab_hdr->sh_type = SHT_STRTAB;
11683 symstrtab_hdr->sh_flags = 0;
11684 symstrtab_hdr->sh_addr = 0;
11685 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
11686 symstrtab_hdr->sh_entsize = 0;
11687 symstrtab_hdr->sh_link = 0;
11688 symstrtab_hdr->sh_info = 0;
11689 /* sh_offset is set just below. */
11690 symstrtab_hdr->sh_addralign = 1;
11691
11692 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
11693 off, TRUE);
11694 elf_next_file_pos (abfd) = off;
11695
11696 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
11697 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
11698 return FALSE;
11699 }
11700
11701 /* Adjust the relocs to have the correct symbol indices. */
11702 for (o = abfd->sections; o != NULL; o = o->next)
11703 {
11704 struct bfd_elf_section_data *esdo = elf_section_data (o);
11705 bfd_boolean sort;
11706 if ((o->flags & SEC_RELOC) == 0)
11707 continue;
11708
11709 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
11710 if (esdo->rel.hdr != NULL
11711 && !elf_link_adjust_relocs (abfd, &esdo->rel, sort))
11712 return FALSE;
11713 if (esdo->rela.hdr != NULL
11714 && !elf_link_adjust_relocs (abfd, &esdo->rela, sort))
11715 return FALSE;
11716
11717 /* Set the reloc_count field to 0 to prevent write_relocs from
11718 trying to swap the relocs out itself. */
11719 o->reloc_count = 0;
11720 }
11721
11722 if (dynamic && info->combreloc && dynobj != NULL)
11723 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
11724
11725 /* If we are linking against a dynamic object, or generating a
11726 shared library, finish up the dynamic linking information. */
11727 if (dynamic)
11728 {
11729 bfd_byte *dyncon, *dynconend;
11730
11731 /* Fix up .dynamic entries. */
11732 o = bfd_get_linker_section (dynobj, ".dynamic");
11733 BFD_ASSERT (o != NULL);
11734
11735 dyncon = o->contents;
11736 dynconend = o->contents + o->size;
11737 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11738 {
11739 Elf_Internal_Dyn dyn;
11740 const char *name;
11741 unsigned int type;
11742
11743 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11744
11745 switch (dyn.d_tag)
11746 {
11747 default:
11748 continue;
11749 case DT_NULL:
11750 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
11751 {
11752 switch (elf_section_data (reldyn)->this_hdr.sh_type)
11753 {
11754 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
11755 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
11756 default: continue;
11757 }
11758 dyn.d_un.d_val = relativecount;
11759 relativecount = 0;
11760 break;
11761 }
11762 continue;
11763
11764 case DT_INIT:
11765 name = info->init_function;
11766 goto get_sym;
11767 case DT_FINI:
11768 name = info->fini_function;
11769 get_sym:
11770 {
11771 struct elf_link_hash_entry *h;
11772
11773 h = elf_link_hash_lookup (elf_hash_table (info), name,
11774 FALSE, FALSE, TRUE);
11775 if (h != NULL
11776 && (h->root.type == bfd_link_hash_defined
11777 || h->root.type == bfd_link_hash_defweak))
11778 {
11779 dyn.d_un.d_ptr = h->root.u.def.value;
11780 o = h->root.u.def.section;
11781 if (o->output_section != NULL)
11782 dyn.d_un.d_ptr += (o->output_section->vma
11783 + o->output_offset);
11784 else
11785 {
11786 /* The symbol is imported from another shared
11787 library and does not apply to this one. */
11788 dyn.d_un.d_ptr = 0;
11789 }
11790 break;
11791 }
11792 }
11793 continue;
11794
11795 case DT_PREINIT_ARRAYSZ:
11796 name = ".preinit_array";
11797 goto get_size;
11798 case DT_INIT_ARRAYSZ:
11799 name = ".init_array";
11800 goto get_size;
11801 case DT_FINI_ARRAYSZ:
11802 name = ".fini_array";
11803 get_size:
11804 o = bfd_get_section_by_name (abfd, name);
11805 if (o == NULL)
11806 {
11807 (*_bfd_error_handler)
11808 (_("%B: could not find output section %s"), abfd, name);
11809 goto error_return;
11810 }
11811 if (o->size == 0)
11812 (*_bfd_error_handler)
11813 (_("warning: %s section has zero size"), name);
11814 dyn.d_un.d_val = o->size;
11815 break;
11816
11817 case DT_PREINIT_ARRAY:
11818 name = ".preinit_array";
11819 goto get_vma;
11820 case DT_INIT_ARRAY:
11821 name = ".init_array";
11822 goto get_vma;
11823 case DT_FINI_ARRAY:
11824 name = ".fini_array";
11825 goto get_vma;
11826
11827 case DT_HASH:
11828 name = ".hash";
11829 goto get_vma;
11830 case DT_GNU_HASH:
11831 name = ".gnu.hash";
11832 goto get_vma;
11833 case DT_STRTAB:
11834 name = ".dynstr";
11835 goto get_vma;
11836 case DT_SYMTAB:
11837 name = ".dynsym";
11838 goto get_vma;
11839 case DT_VERDEF:
11840 name = ".gnu.version_d";
11841 goto get_vma;
11842 case DT_VERNEED:
11843 name = ".gnu.version_r";
11844 goto get_vma;
11845 case DT_VERSYM:
11846 name = ".gnu.version";
11847 get_vma:
11848 o = bfd_get_section_by_name (abfd, name);
11849 if (o == NULL)
11850 {
11851 (*_bfd_error_handler)
11852 (_("%B: could not find output section %s"), abfd, name);
11853 goto error_return;
11854 }
11855 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
11856 {
11857 (*_bfd_error_handler)
11858 (_("warning: section '%s' is being made into a note"), name);
11859 bfd_set_error (bfd_error_nonrepresentable_section);
11860 goto error_return;
11861 }
11862 dyn.d_un.d_ptr = o->vma;
11863 break;
11864
11865 case DT_REL:
11866 case DT_RELA:
11867 case DT_RELSZ:
11868 case DT_RELASZ:
11869 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
11870 type = SHT_REL;
11871 else
11872 type = SHT_RELA;
11873 dyn.d_un.d_val = 0;
11874 dyn.d_un.d_ptr = 0;
11875 for (i = 1; i < elf_numsections (abfd); i++)
11876 {
11877 Elf_Internal_Shdr *hdr;
11878
11879 hdr = elf_elfsections (abfd)[i];
11880 if (hdr->sh_type == type
11881 && (hdr->sh_flags & SHF_ALLOC) != 0)
11882 {
11883 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
11884 dyn.d_un.d_val += hdr->sh_size;
11885 else
11886 {
11887 if (dyn.d_un.d_ptr == 0
11888 || hdr->sh_addr < dyn.d_un.d_ptr)
11889 dyn.d_un.d_ptr = hdr->sh_addr;
11890 }
11891 }
11892 }
11893 break;
11894 }
11895 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
11896 }
11897 }
11898
11899 /* If we have created any dynamic sections, then output them. */
11900 if (dynobj != NULL)
11901 {
11902 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
11903 goto error_return;
11904
11905 /* Check for DT_TEXTREL (late, in case the backend removes it). */
11906 if (((info->warn_shared_textrel && bfd_link_pic (info))
11907 || info->error_textrel)
11908 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
11909 {
11910 bfd_byte *dyncon, *dynconend;
11911
11912 dyncon = o->contents;
11913 dynconend = o->contents + o->size;
11914 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11915 {
11916 Elf_Internal_Dyn dyn;
11917
11918 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11919
11920 if (dyn.d_tag == DT_TEXTREL)
11921 {
11922 if (info->error_textrel)
11923 info->callbacks->einfo
11924 (_("%P%X: read-only segment has dynamic relocations.\n"));
11925 else
11926 info->callbacks->einfo
11927 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
11928 break;
11929 }
11930 }
11931 }
11932
11933 for (o = dynobj->sections; o != NULL; o = o->next)
11934 {
11935 if ((o->flags & SEC_HAS_CONTENTS) == 0
11936 || o->size == 0
11937 || o->output_section == bfd_abs_section_ptr)
11938 continue;
11939 if ((o->flags & SEC_LINKER_CREATED) == 0)
11940 {
11941 /* At this point, we are only interested in sections
11942 created by _bfd_elf_link_create_dynamic_sections. */
11943 continue;
11944 }
11945 if (elf_hash_table (info)->stab_info.stabstr == o)
11946 continue;
11947 if (elf_hash_table (info)->eh_info.hdr_sec == o)
11948 continue;
11949 if (strcmp (o->name, ".dynstr") != 0)
11950 {
11951 /* FIXME: octets_per_byte. */
11952 if (! bfd_set_section_contents (abfd, o->output_section,
11953 o->contents,
11954 (file_ptr) o->output_offset,
11955 o->size))
11956 goto error_return;
11957 }
11958 else
11959 {
11960 /* The contents of the .dynstr section are actually in a
11961 stringtab. */
11962 file_ptr off;
11963
11964 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
11965 if (bfd_seek (abfd, off, SEEK_SET) != 0
11966 || ! _bfd_elf_strtab_emit (abfd,
11967 elf_hash_table (info)->dynstr))
11968 goto error_return;
11969 }
11970 }
11971 }
11972
11973 if (bfd_link_relocatable (info))
11974 {
11975 bfd_boolean failed = FALSE;
11976
11977 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
11978 if (failed)
11979 goto error_return;
11980 }
11981
11982 /* If we have optimized stabs strings, output them. */
11983 if (elf_hash_table (info)->stab_info.stabstr != NULL)
11984 {
11985 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
11986 goto error_return;
11987 }
11988
11989 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
11990 goto error_return;
11991
11992 elf_final_link_free (abfd, &flinfo);
11993
11994 elf_linker (abfd) = TRUE;
11995
11996 if (attr_section)
11997 {
11998 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
11999 if (contents == NULL)
12000 return FALSE; /* Bail out and fail. */
12001 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12002 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12003 free (contents);
12004 }
12005
12006 return TRUE;
12007
12008 error_return:
12009 elf_final_link_free (abfd, &flinfo);
12010 return FALSE;
12011 }
12012 \f
12013 /* Initialize COOKIE for input bfd ABFD. */
12014
12015 static bfd_boolean
12016 init_reloc_cookie (struct elf_reloc_cookie *cookie,
12017 struct bfd_link_info *info, bfd *abfd)
12018 {
12019 Elf_Internal_Shdr *symtab_hdr;
12020 const struct elf_backend_data *bed;
12021
12022 bed = get_elf_backend_data (abfd);
12023 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12024
12025 cookie->abfd = abfd;
12026 cookie->sym_hashes = elf_sym_hashes (abfd);
12027 cookie->bad_symtab = elf_bad_symtab (abfd);
12028 if (cookie->bad_symtab)
12029 {
12030 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12031 cookie->extsymoff = 0;
12032 }
12033 else
12034 {
12035 cookie->locsymcount = symtab_hdr->sh_info;
12036 cookie->extsymoff = symtab_hdr->sh_info;
12037 }
12038
12039 if (bed->s->arch_size == 32)
12040 cookie->r_sym_shift = 8;
12041 else
12042 cookie->r_sym_shift = 32;
12043
12044 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12045 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12046 {
12047 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12048 cookie->locsymcount, 0,
12049 NULL, NULL, NULL);
12050 if (cookie->locsyms == NULL)
12051 {
12052 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12053 return FALSE;
12054 }
12055 if (info->keep_memory)
12056 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12057 }
12058 return TRUE;
12059 }
12060
12061 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12062
12063 static void
12064 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12065 {
12066 Elf_Internal_Shdr *symtab_hdr;
12067
12068 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12069 if (cookie->locsyms != NULL
12070 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12071 free (cookie->locsyms);
12072 }
12073
12074 /* Initialize the relocation information in COOKIE for input section SEC
12075 of input bfd ABFD. */
12076
12077 static bfd_boolean
12078 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12079 struct bfd_link_info *info, bfd *abfd,
12080 asection *sec)
12081 {
12082 const struct elf_backend_data *bed;
12083
12084 if (sec->reloc_count == 0)
12085 {
12086 cookie->rels = NULL;
12087 cookie->relend = NULL;
12088 }
12089 else
12090 {
12091 bed = get_elf_backend_data (abfd);
12092
12093 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12094 info->keep_memory);
12095 if (cookie->rels == NULL)
12096 return FALSE;
12097 cookie->rel = cookie->rels;
12098 cookie->relend = (cookie->rels
12099 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
12100 }
12101 cookie->rel = cookie->rels;
12102 return TRUE;
12103 }
12104
12105 /* Free the memory allocated by init_reloc_cookie_rels,
12106 if appropriate. */
12107
12108 static void
12109 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12110 asection *sec)
12111 {
12112 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12113 free (cookie->rels);
12114 }
12115
12116 /* Initialize the whole of COOKIE for input section SEC. */
12117
12118 static bfd_boolean
12119 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12120 struct bfd_link_info *info,
12121 asection *sec)
12122 {
12123 if (!init_reloc_cookie (cookie, info, sec->owner))
12124 goto error1;
12125 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12126 goto error2;
12127 return TRUE;
12128
12129 error2:
12130 fini_reloc_cookie (cookie, sec->owner);
12131 error1:
12132 return FALSE;
12133 }
12134
12135 /* Free the memory allocated by init_reloc_cookie_for_section,
12136 if appropriate. */
12137
12138 static void
12139 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12140 asection *sec)
12141 {
12142 fini_reloc_cookie_rels (cookie, sec);
12143 fini_reloc_cookie (cookie, sec->owner);
12144 }
12145 \f
12146 /* Garbage collect unused sections. */
12147
12148 /* Default gc_mark_hook. */
12149
12150 asection *
12151 _bfd_elf_gc_mark_hook (asection *sec,
12152 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12153 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12154 struct elf_link_hash_entry *h,
12155 Elf_Internal_Sym *sym)
12156 {
12157 if (h != NULL)
12158 {
12159 switch (h->root.type)
12160 {
12161 case bfd_link_hash_defined:
12162 case bfd_link_hash_defweak:
12163 return h->root.u.def.section;
12164
12165 case bfd_link_hash_common:
12166 return h->root.u.c.p->section;
12167
12168 default:
12169 break;
12170 }
12171 }
12172 else
12173 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12174
12175 return NULL;
12176 }
12177
12178 /* COOKIE->rel describes a relocation against section SEC, which is
12179 a section we've decided to keep. Return the section that contains
12180 the relocation symbol, or NULL if no section contains it. */
12181
12182 asection *
12183 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12184 elf_gc_mark_hook_fn gc_mark_hook,
12185 struct elf_reloc_cookie *cookie,
12186 bfd_boolean *start_stop)
12187 {
12188 unsigned long r_symndx;
12189 struct elf_link_hash_entry *h;
12190
12191 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
12192 if (r_symndx == STN_UNDEF)
12193 return NULL;
12194
12195 if (r_symndx >= cookie->locsymcount
12196 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12197 {
12198 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
12199 if (h == NULL)
12200 {
12201 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12202 sec->owner);
12203 return NULL;
12204 }
12205 while (h->root.type == bfd_link_hash_indirect
12206 || h->root.type == bfd_link_hash_warning)
12207 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12208 h->mark = 1;
12209 /* If this symbol is weak and there is a non-weak definition, we
12210 keep the non-weak definition because many backends put
12211 dynamic reloc info on the non-weak definition for code
12212 handling copy relocs. */
12213 if (h->u.weakdef != NULL)
12214 h->u.weakdef->mark = 1;
12215
12216 if (start_stop != NULL
12217 && (h->root.type == bfd_link_hash_undefined
12218 || h->root.type == bfd_link_hash_undefweak))
12219 {
12220 /* To work around a glibc bug, mark all XXX input sections
12221 when there is an as yet undefined reference to __start_XXX
12222 or __stop_XXX symbols. The linker will later define such
12223 symbols for orphan input sections that have a name
12224 representable as a C identifier. */
12225 const char *sec_name = NULL;
12226 if (strncmp (h->root.root.string, "__start_", 8) == 0)
12227 sec_name = h->root.root.string + 8;
12228 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
12229 sec_name = h->root.root.string + 7;
12230
12231 if (sec_name != NULL && *sec_name != '\0')
12232 {
12233 bfd *i;
12234
12235 for (i = info->input_bfds; i != NULL; i = i->link.next)
12236 {
12237 asection *s = bfd_get_section_by_name (i, sec_name);
12238 if (s != NULL && !s->gc_mark)
12239 {
12240 *start_stop = TRUE;
12241 return s;
12242 }
12243 }
12244 }
12245 }
12246
12247 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12248 }
12249
12250 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12251 &cookie->locsyms[r_symndx]);
12252 }
12253
12254 /* COOKIE->rel describes a relocation against section SEC, which is
12255 a section we've decided to keep. Mark the section that contains
12256 the relocation symbol. */
12257
12258 bfd_boolean
12259 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12260 asection *sec,
12261 elf_gc_mark_hook_fn gc_mark_hook,
12262 struct elf_reloc_cookie *cookie)
12263 {
12264 asection *rsec;
12265 bfd_boolean start_stop = FALSE;
12266
12267 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12268 while (rsec != NULL)
12269 {
12270 if (!rsec->gc_mark)
12271 {
12272 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12273 || (rsec->owner->flags & DYNAMIC) != 0)
12274 rsec->gc_mark = 1;
12275 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12276 return FALSE;
12277 }
12278 if (!start_stop)
12279 break;
12280 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
12281 }
12282 return TRUE;
12283 }
12284
12285 /* The mark phase of garbage collection. For a given section, mark
12286 it and any sections in this section's group, and all the sections
12287 which define symbols to which it refers. */
12288
12289 bfd_boolean
12290 _bfd_elf_gc_mark (struct bfd_link_info *info,
12291 asection *sec,
12292 elf_gc_mark_hook_fn gc_mark_hook)
12293 {
12294 bfd_boolean ret;
12295 asection *group_sec, *eh_frame;
12296
12297 sec->gc_mark = 1;
12298
12299 /* Mark all the sections in the group. */
12300 group_sec = elf_section_data (sec)->next_in_group;
12301 if (group_sec && !group_sec->gc_mark)
12302 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
12303 return FALSE;
12304
12305 /* Look through the section relocs. */
12306 ret = TRUE;
12307 eh_frame = elf_eh_frame_section (sec->owner);
12308 if ((sec->flags & SEC_RELOC) != 0
12309 && sec->reloc_count > 0
12310 && sec != eh_frame)
12311 {
12312 struct elf_reloc_cookie cookie;
12313
12314 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12315 ret = FALSE;
12316 else
12317 {
12318 for (; cookie.rel < cookie.relend; cookie.rel++)
12319 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
12320 {
12321 ret = FALSE;
12322 break;
12323 }
12324 fini_reloc_cookie_for_section (&cookie, sec);
12325 }
12326 }
12327
12328 if (ret && eh_frame && elf_fde_list (sec))
12329 {
12330 struct elf_reloc_cookie cookie;
12331
12332 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12333 ret = FALSE;
12334 else
12335 {
12336 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12337 gc_mark_hook, &cookie))
12338 ret = FALSE;
12339 fini_reloc_cookie_for_section (&cookie, eh_frame);
12340 }
12341 }
12342
12343 eh_frame = elf_section_eh_frame_entry (sec);
12344 if (ret && eh_frame && !eh_frame->gc_mark)
12345 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12346 ret = FALSE;
12347
12348 return ret;
12349 }
12350
12351 /* Scan and mark sections in a special or debug section group. */
12352
12353 static void
12354 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12355 {
12356 /* Point to first section of section group. */
12357 asection *ssec;
12358 /* Used to iterate the section group. */
12359 asection *msec;
12360
12361 bfd_boolean is_special_grp = TRUE;
12362 bfd_boolean is_debug_grp = TRUE;
12363
12364 /* First scan to see if group contains any section other than debug
12365 and special section. */
12366 ssec = msec = elf_next_in_group (grp);
12367 do
12368 {
12369 if ((msec->flags & SEC_DEBUGGING) == 0)
12370 is_debug_grp = FALSE;
12371
12372 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12373 is_special_grp = FALSE;
12374
12375 msec = elf_next_in_group (msec);
12376 }
12377 while (msec != ssec);
12378
12379 /* If this is a pure debug section group or pure special section group,
12380 keep all sections in this group. */
12381 if (is_debug_grp || is_special_grp)
12382 {
12383 do
12384 {
12385 msec->gc_mark = 1;
12386 msec = elf_next_in_group (msec);
12387 }
12388 while (msec != ssec);
12389 }
12390 }
12391
12392 /* Keep debug and special sections. */
12393
12394 bfd_boolean
12395 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12396 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12397 {
12398 bfd *ibfd;
12399
12400 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12401 {
12402 asection *isec;
12403 bfd_boolean some_kept;
12404 bfd_boolean debug_frag_seen;
12405
12406 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12407 continue;
12408
12409 /* Ensure all linker created sections are kept,
12410 see if any other section is already marked,
12411 and note if we have any fragmented debug sections. */
12412 debug_frag_seen = some_kept = FALSE;
12413 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12414 {
12415 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12416 isec->gc_mark = 1;
12417 else if (isec->gc_mark)
12418 some_kept = TRUE;
12419
12420 if (debug_frag_seen == FALSE
12421 && (isec->flags & SEC_DEBUGGING)
12422 && CONST_STRNEQ (isec->name, ".debug_line."))
12423 debug_frag_seen = TRUE;
12424 }
12425
12426 /* If no section in this file will be kept, then we can
12427 toss out the debug and special sections. */
12428 if (!some_kept)
12429 continue;
12430
12431 /* Keep debug and special sections like .comment when they are
12432 not part of a group. Also keep section groups that contain
12433 just debug sections or special sections. */
12434 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12435 {
12436 if ((isec->flags & SEC_GROUP) != 0)
12437 _bfd_elf_gc_mark_debug_special_section_group (isec);
12438 else if (((isec->flags & SEC_DEBUGGING) != 0
12439 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12440 && elf_next_in_group (isec) == NULL)
12441 isec->gc_mark = 1;
12442 }
12443
12444 if (! debug_frag_seen)
12445 continue;
12446
12447 /* Look for CODE sections which are going to be discarded,
12448 and find and discard any fragmented debug sections which
12449 are associated with that code section. */
12450 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12451 if ((isec->flags & SEC_CODE) != 0
12452 && isec->gc_mark == 0)
12453 {
12454 unsigned int ilen;
12455 asection *dsec;
12456
12457 ilen = strlen (isec->name);
12458
12459 /* Association is determined by the name of the debug section
12460 containing the name of the code section as a suffix. For
12461 example .debug_line.text.foo is a debug section associated
12462 with .text.foo. */
12463 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12464 {
12465 unsigned int dlen;
12466
12467 if (dsec->gc_mark == 0
12468 || (dsec->flags & SEC_DEBUGGING) == 0)
12469 continue;
12470
12471 dlen = strlen (dsec->name);
12472
12473 if (dlen > ilen
12474 && strncmp (dsec->name + (dlen - ilen),
12475 isec->name, ilen) == 0)
12476 {
12477 dsec->gc_mark = 0;
12478 }
12479 }
12480 }
12481 }
12482 return TRUE;
12483 }
12484
12485 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
12486
12487 struct elf_gc_sweep_symbol_info
12488 {
12489 struct bfd_link_info *info;
12490 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
12491 bfd_boolean);
12492 };
12493
12494 static bfd_boolean
12495 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
12496 {
12497 if (!h->mark
12498 && (((h->root.type == bfd_link_hash_defined
12499 || h->root.type == bfd_link_hash_defweak)
12500 && !((h->def_regular || ELF_COMMON_DEF_P (h))
12501 && h->root.u.def.section->gc_mark))
12502 || h->root.type == bfd_link_hash_undefined
12503 || h->root.type == bfd_link_hash_undefweak))
12504 {
12505 struct elf_gc_sweep_symbol_info *inf;
12506
12507 inf = (struct elf_gc_sweep_symbol_info *) data;
12508 (*inf->hide_symbol) (inf->info, h, TRUE);
12509 h->def_regular = 0;
12510 h->ref_regular = 0;
12511 h->ref_regular_nonweak = 0;
12512 }
12513
12514 return TRUE;
12515 }
12516
12517 /* The sweep phase of garbage collection. Remove all garbage sections. */
12518
12519 typedef bfd_boolean (*gc_sweep_hook_fn)
12520 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
12521
12522 static bfd_boolean
12523 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
12524 {
12525 bfd *sub;
12526 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12527 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
12528 unsigned long section_sym_count;
12529 struct elf_gc_sweep_symbol_info sweep_info;
12530
12531 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12532 {
12533 asection *o;
12534
12535 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12536 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
12537 continue;
12538
12539 for (o = sub->sections; o != NULL; o = o->next)
12540 {
12541 /* When any section in a section group is kept, we keep all
12542 sections in the section group. If the first member of
12543 the section group is excluded, we will also exclude the
12544 group section. */
12545 if (o->flags & SEC_GROUP)
12546 {
12547 asection *first = elf_next_in_group (o);
12548 o->gc_mark = first->gc_mark;
12549 }
12550
12551 if (o->gc_mark)
12552 continue;
12553
12554 /* Skip sweeping sections already excluded. */
12555 if (o->flags & SEC_EXCLUDE)
12556 continue;
12557
12558 /* Since this is early in the link process, it is simple
12559 to remove a section from the output. */
12560 o->flags |= SEC_EXCLUDE;
12561
12562 if (info->print_gc_sections && o->size != 0)
12563 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
12564
12565 /* But we also have to update some of the relocation
12566 info we collected before. */
12567 if (gc_sweep_hook
12568 && (o->flags & SEC_RELOC) != 0
12569 && o->reloc_count != 0
12570 && !((info->strip == strip_all || info->strip == strip_debugger)
12571 && (o->flags & SEC_DEBUGGING) != 0)
12572 && !bfd_is_abs_section (o->output_section))
12573 {
12574 Elf_Internal_Rela *internal_relocs;
12575 bfd_boolean r;
12576
12577 internal_relocs
12578 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
12579 info->keep_memory);
12580 if (internal_relocs == NULL)
12581 return FALSE;
12582
12583 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
12584
12585 if (elf_section_data (o)->relocs != internal_relocs)
12586 free (internal_relocs);
12587
12588 if (!r)
12589 return FALSE;
12590 }
12591 }
12592 }
12593
12594 /* Remove the symbols that were in the swept sections from the dynamic
12595 symbol table. GCFIXME: Anyone know how to get them out of the
12596 static symbol table as well? */
12597 sweep_info.info = info;
12598 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
12599 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
12600 &sweep_info);
12601
12602 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
12603 return TRUE;
12604 }
12605
12606 /* Propagate collected vtable information. This is called through
12607 elf_link_hash_traverse. */
12608
12609 static bfd_boolean
12610 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
12611 {
12612 /* Those that are not vtables. */
12613 if (h->vtable == NULL || h->vtable->parent == NULL)
12614 return TRUE;
12615
12616 /* Those vtables that do not have parents, we cannot merge. */
12617 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
12618 return TRUE;
12619
12620 /* If we've already been done, exit. */
12621 if (h->vtable->used && h->vtable->used[-1])
12622 return TRUE;
12623
12624 /* Make sure the parent's table is up to date. */
12625 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
12626
12627 if (h->vtable->used == NULL)
12628 {
12629 /* None of this table's entries were referenced. Re-use the
12630 parent's table. */
12631 h->vtable->used = h->vtable->parent->vtable->used;
12632 h->vtable->size = h->vtable->parent->vtable->size;
12633 }
12634 else
12635 {
12636 size_t n;
12637 bfd_boolean *cu, *pu;
12638
12639 /* Or the parent's entries into ours. */
12640 cu = h->vtable->used;
12641 cu[-1] = TRUE;
12642 pu = h->vtable->parent->vtable->used;
12643 if (pu != NULL)
12644 {
12645 const struct elf_backend_data *bed;
12646 unsigned int log_file_align;
12647
12648 bed = get_elf_backend_data (h->root.u.def.section->owner);
12649 log_file_align = bed->s->log_file_align;
12650 n = h->vtable->parent->vtable->size >> log_file_align;
12651 while (n--)
12652 {
12653 if (*pu)
12654 *cu = TRUE;
12655 pu++;
12656 cu++;
12657 }
12658 }
12659 }
12660
12661 return TRUE;
12662 }
12663
12664 static bfd_boolean
12665 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
12666 {
12667 asection *sec;
12668 bfd_vma hstart, hend;
12669 Elf_Internal_Rela *relstart, *relend, *rel;
12670 const struct elf_backend_data *bed;
12671 unsigned int log_file_align;
12672
12673 /* Take care of both those symbols that do not describe vtables as
12674 well as those that are not loaded. */
12675 if (h->vtable == NULL || h->vtable->parent == NULL)
12676 return TRUE;
12677
12678 BFD_ASSERT (h->root.type == bfd_link_hash_defined
12679 || h->root.type == bfd_link_hash_defweak);
12680
12681 sec = h->root.u.def.section;
12682 hstart = h->root.u.def.value;
12683 hend = hstart + h->size;
12684
12685 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
12686 if (!relstart)
12687 return *(bfd_boolean *) okp = FALSE;
12688 bed = get_elf_backend_data (sec->owner);
12689 log_file_align = bed->s->log_file_align;
12690
12691 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
12692
12693 for (rel = relstart; rel < relend; ++rel)
12694 if (rel->r_offset >= hstart && rel->r_offset < hend)
12695 {
12696 /* If the entry is in use, do nothing. */
12697 if (h->vtable->used
12698 && (rel->r_offset - hstart) < h->vtable->size)
12699 {
12700 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
12701 if (h->vtable->used[entry])
12702 continue;
12703 }
12704 /* Otherwise, kill it. */
12705 rel->r_offset = rel->r_info = rel->r_addend = 0;
12706 }
12707
12708 return TRUE;
12709 }
12710
12711 /* Mark sections containing dynamically referenced symbols. When
12712 building shared libraries, we must assume that any visible symbol is
12713 referenced. */
12714
12715 bfd_boolean
12716 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
12717 {
12718 struct bfd_link_info *info = (struct bfd_link_info *) inf;
12719 struct bfd_elf_dynamic_list *d = info->dynamic_list;
12720
12721 if ((h->root.type == bfd_link_hash_defined
12722 || h->root.type == bfd_link_hash_defweak)
12723 && (h->ref_dynamic
12724 || ((h->def_regular || ELF_COMMON_DEF_P (h))
12725 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
12726 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
12727 && (!bfd_link_executable (info)
12728 || info->export_dynamic
12729 || (h->dynamic
12730 && d != NULL
12731 && (*d->match) (&d->head, NULL, h->root.root.string)))
12732 && (h->versioned >= versioned
12733 || !bfd_hide_sym_by_version (info->version_info,
12734 h->root.root.string)))))
12735 h->root.u.def.section->flags |= SEC_KEEP;
12736
12737 return TRUE;
12738 }
12739
12740 /* Keep all sections containing symbols undefined on the command-line,
12741 and the section containing the entry symbol. */
12742
12743 void
12744 _bfd_elf_gc_keep (struct bfd_link_info *info)
12745 {
12746 struct bfd_sym_chain *sym;
12747
12748 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
12749 {
12750 struct elf_link_hash_entry *h;
12751
12752 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
12753 FALSE, FALSE, FALSE);
12754
12755 if (h != NULL
12756 && (h->root.type == bfd_link_hash_defined
12757 || h->root.type == bfd_link_hash_defweak)
12758 && !bfd_is_abs_section (h->root.u.def.section))
12759 h->root.u.def.section->flags |= SEC_KEEP;
12760 }
12761 }
12762
12763 bfd_boolean
12764 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
12765 struct bfd_link_info *info)
12766 {
12767 bfd *ibfd = info->input_bfds;
12768
12769 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12770 {
12771 asection *sec;
12772 struct elf_reloc_cookie cookie;
12773
12774 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12775 continue;
12776
12777 if (!init_reloc_cookie (&cookie, info, ibfd))
12778 return FALSE;
12779
12780 for (sec = ibfd->sections; sec; sec = sec->next)
12781 {
12782 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
12783 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
12784 {
12785 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
12786 fini_reloc_cookie_rels (&cookie, sec);
12787 }
12788 }
12789 }
12790 return TRUE;
12791 }
12792
12793 /* Do mark and sweep of unused sections. */
12794
12795 bfd_boolean
12796 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
12797 {
12798 bfd_boolean ok = TRUE;
12799 bfd *sub;
12800 elf_gc_mark_hook_fn gc_mark_hook;
12801 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12802 struct elf_link_hash_table *htab;
12803
12804 if (!bed->can_gc_sections
12805 || !is_elf_hash_table (info->hash))
12806 {
12807 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
12808 return TRUE;
12809 }
12810
12811 bed->gc_keep (info);
12812 htab = elf_hash_table (info);
12813
12814 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
12815 at the .eh_frame section if we can mark the FDEs individually. */
12816 for (sub = info->input_bfds;
12817 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
12818 sub = sub->link.next)
12819 {
12820 asection *sec;
12821 struct elf_reloc_cookie cookie;
12822
12823 sec = bfd_get_section_by_name (sub, ".eh_frame");
12824 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
12825 {
12826 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
12827 if (elf_section_data (sec)->sec_info
12828 && (sec->flags & SEC_LINKER_CREATED) == 0)
12829 elf_eh_frame_section (sub) = sec;
12830 fini_reloc_cookie_for_section (&cookie, sec);
12831 sec = bfd_get_next_section_by_name (NULL, sec);
12832 }
12833 }
12834
12835 /* Apply transitive closure to the vtable entry usage info. */
12836 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
12837 if (!ok)
12838 return FALSE;
12839
12840 /* Kill the vtable relocations that were not used. */
12841 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
12842 if (!ok)
12843 return FALSE;
12844
12845 /* Mark dynamically referenced symbols. */
12846 if (htab->dynamic_sections_created)
12847 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
12848
12849 /* Grovel through relocs to find out who stays ... */
12850 gc_mark_hook = bed->gc_mark_hook;
12851 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12852 {
12853 asection *o;
12854
12855 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12856 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
12857 continue;
12858
12859 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
12860 Also treat note sections as a root, if the section is not part
12861 of a group. */
12862 for (o = sub->sections; o != NULL; o = o->next)
12863 if (!o->gc_mark
12864 && (o->flags & SEC_EXCLUDE) == 0
12865 && ((o->flags & SEC_KEEP) != 0
12866 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
12867 && elf_next_in_group (o) == NULL )))
12868 {
12869 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12870 return FALSE;
12871 }
12872 }
12873
12874 /* Allow the backend to mark additional target specific sections. */
12875 bed->gc_mark_extra_sections (info, gc_mark_hook);
12876
12877 /* ... and mark SEC_EXCLUDE for those that go. */
12878 return elf_gc_sweep (abfd, info);
12879 }
12880 \f
12881 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
12882
12883 bfd_boolean
12884 bfd_elf_gc_record_vtinherit (bfd *abfd,
12885 asection *sec,
12886 struct elf_link_hash_entry *h,
12887 bfd_vma offset)
12888 {
12889 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
12890 struct elf_link_hash_entry **search, *child;
12891 bfd_size_type extsymcount;
12892 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12893
12894 /* The sh_info field of the symtab header tells us where the
12895 external symbols start. We don't care about the local symbols at
12896 this point. */
12897 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
12898 if (!elf_bad_symtab (abfd))
12899 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
12900
12901 sym_hashes = elf_sym_hashes (abfd);
12902 sym_hashes_end = sym_hashes + extsymcount;
12903
12904 /* Hunt down the child symbol, which is in this section at the same
12905 offset as the relocation. */
12906 for (search = sym_hashes; search != sym_hashes_end; ++search)
12907 {
12908 if ((child = *search) != NULL
12909 && (child->root.type == bfd_link_hash_defined
12910 || child->root.type == bfd_link_hash_defweak)
12911 && child->root.u.def.section == sec
12912 && child->root.u.def.value == offset)
12913 goto win;
12914 }
12915
12916 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
12917 abfd, sec, (unsigned long) offset);
12918 bfd_set_error (bfd_error_invalid_operation);
12919 return FALSE;
12920
12921 win:
12922 if (!child->vtable)
12923 {
12924 child->vtable = ((struct elf_link_virtual_table_entry *)
12925 bfd_zalloc (abfd, sizeof (*child->vtable)));
12926 if (!child->vtable)
12927 return FALSE;
12928 }
12929 if (!h)
12930 {
12931 /* This *should* only be the absolute section. It could potentially
12932 be that someone has defined a non-global vtable though, which
12933 would be bad. It isn't worth paging in the local symbols to be
12934 sure though; that case should simply be handled by the assembler. */
12935
12936 child->vtable->parent = (struct elf_link_hash_entry *) -1;
12937 }
12938 else
12939 child->vtable->parent = h;
12940
12941 return TRUE;
12942 }
12943
12944 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
12945
12946 bfd_boolean
12947 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
12948 asection *sec ATTRIBUTE_UNUSED,
12949 struct elf_link_hash_entry *h,
12950 bfd_vma addend)
12951 {
12952 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12953 unsigned int log_file_align = bed->s->log_file_align;
12954
12955 if (!h->vtable)
12956 {
12957 h->vtable = ((struct elf_link_virtual_table_entry *)
12958 bfd_zalloc (abfd, sizeof (*h->vtable)));
12959 if (!h->vtable)
12960 return FALSE;
12961 }
12962
12963 if (addend >= h->vtable->size)
12964 {
12965 size_t size, bytes, file_align;
12966 bfd_boolean *ptr = h->vtable->used;
12967
12968 /* While the symbol is undefined, we have to be prepared to handle
12969 a zero size. */
12970 file_align = 1 << log_file_align;
12971 if (h->root.type == bfd_link_hash_undefined)
12972 size = addend + file_align;
12973 else
12974 {
12975 size = h->size;
12976 if (addend >= size)
12977 {
12978 /* Oops! We've got a reference past the defined end of
12979 the table. This is probably a bug -- shall we warn? */
12980 size = addend + file_align;
12981 }
12982 }
12983 size = (size + file_align - 1) & -file_align;
12984
12985 /* Allocate one extra entry for use as a "done" flag for the
12986 consolidation pass. */
12987 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
12988
12989 if (ptr)
12990 {
12991 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
12992
12993 if (ptr != NULL)
12994 {
12995 size_t oldbytes;
12996
12997 oldbytes = (((h->vtable->size >> log_file_align) + 1)
12998 * sizeof (bfd_boolean));
12999 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13000 }
13001 }
13002 else
13003 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
13004
13005 if (ptr == NULL)
13006 return FALSE;
13007
13008 /* And arrange for that done flag to be at index -1. */
13009 h->vtable->used = ptr + 1;
13010 h->vtable->size = size;
13011 }
13012
13013 h->vtable->used[addend >> log_file_align] = TRUE;
13014
13015 return TRUE;
13016 }
13017
13018 /* Map an ELF section header flag to its corresponding string. */
13019 typedef struct
13020 {
13021 char *flag_name;
13022 flagword flag_value;
13023 } elf_flags_to_name_table;
13024
13025 static elf_flags_to_name_table elf_flags_to_names [] =
13026 {
13027 { "SHF_WRITE", SHF_WRITE },
13028 { "SHF_ALLOC", SHF_ALLOC },
13029 { "SHF_EXECINSTR", SHF_EXECINSTR },
13030 { "SHF_MERGE", SHF_MERGE },
13031 { "SHF_STRINGS", SHF_STRINGS },
13032 { "SHF_INFO_LINK", SHF_INFO_LINK},
13033 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13034 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13035 { "SHF_GROUP", SHF_GROUP },
13036 { "SHF_TLS", SHF_TLS },
13037 { "SHF_MASKOS", SHF_MASKOS },
13038 { "SHF_EXCLUDE", SHF_EXCLUDE },
13039 };
13040
13041 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13042 bfd_boolean
13043 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
13044 struct flag_info *flaginfo,
13045 asection *section)
13046 {
13047 const bfd_vma sh_flags = elf_section_flags (section);
13048
13049 if (!flaginfo->flags_initialized)
13050 {
13051 bfd *obfd = info->output_bfd;
13052 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13053 struct flag_info_list *tf = flaginfo->flag_list;
13054 int with_hex = 0;
13055 int without_hex = 0;
13056
13057 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13058 {
13059 unsigned i;
13060 flagword (*lookup) (char *);
13061
13062 lookup = bed->elf_backend_lookup_section_flags_hook;
13063 if (lookup != NULL)
13064 {
13065 flagword hexval = (*lookup) ((char *) tf->name);
13066
13067 if (hexval != 0)
13068 {
13069 if (tf->with == with_flags)
13070 with_hex |= hexval;
13071 else if (tf->with == without_flags)
13072 without_hex |= hexval;
13073 tf->valid = TRUE;
13074 continue;
13075 }
13076 }
13077 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13078 {
13079 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
13080 {
13081 if (tf->with == with_flags)
13082 with_hex |= elf_flags_to_names[i].flag_value;
13083 else if (tf->with == without_flags)
13084 without_hex |= elf_flags_to_names[i].flag_value;
13085 tf->valid = TRUE;
13086 break;
13087 }
13088 }
13089 if (!tf->valid)
13090 {
13091 info->callbacks->einfo
13092 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
13093 return FALSE;
13094 }
13095 }
13096 flaginfo->flags_initialized = TRUE;
13097 flaginfo->only_with_flags |= with_hex;
13098 flaginfo->not_with_flags |= without_hex;
13099 }
13100
13101 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13102 return FALSE;
13103
13104 if ((flaginfo->not_with_flags & sh_flags) != 0)
13105 return FALSE;
13106
13107 return TRUE;
13108 }
13109
13110 struct alloc_got_off_arg {
13111 bfd_vma gotoff;
13112 struct bfd_link_info *info;
13113 };
13114
13115 /* We need a special top-level link routine to convert got reference counts
13116 to real got offsets. */
13117
13118 static bfd_boolean
13119 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13120 {
13121 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13122 bfd *obfd = gofarg->info->output_bfd;
13123 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13124
13125 if (h->got.refcount > 0)
13126 {
13127 h->got.offset = gofarg->gotoff;
13128 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13129 }
13130 else
13131 h->got.offset = (bfd_vma) -1;
13132
13133 return TRUE;
13134 }
13135
13136 /* And an accompanying bit to work out final got entry offsets once
13137 we're done. Should be called from final_link. */
13138
13139 bfd_boolean
13140 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13141 struct bfd_link_info *info)
13142 {
13143 bfd *i;
13144 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13145 bfd_vma gotoff;
13146 struct alloc_got_off_arg gofarg;
13147
13148 BFD_ASSERT (abfd == info->output_bfd);
13149
13150 if (! is_elf_hash_table (info->hash))
13151 return FALSE;
13152
13153 /* The GOT offset is relative to the .got section, but the GOT header is
13154 put into the .got.plt section, if the backend uses it. */
13155 if (bed->want_got_plt)
13156 gotoff = 0;
13157 else
13158 gotoff = bed->got_header_size;
13159
13160 /* Do the local .got entries first. */
13161 for (i = info->input_bfds; i; i = i->link.next)
13162 {
13163 bfd_signed_vma *local_got;
13164 bfd_size_type j, locsymcount;
13165 Elf_Internal_Shdr *symtab_hdr;
13166
13167 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13168 continue;
13169
13170 local_got = elf_local_got_refcounts (i);
13171 if (!local_got)
13172 continue;
13173
13174 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13175 if (elf_bad_symtab (i))
13176 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13177 else
13178 locsymcount = symtab_hdr->sh_info;
13179
13180 for (j = 0; j < locsymcount; ++j)
13181 {
13182 if (local_got[j] > 0)
13183 {
13184 local_got[j] = gotoff;
13185 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
13186 }
13187 else
13188 local_got[j] = (bfd_vma) -1;
13189 }
13190 }
13191
13192 /* Then the global .got entries. .plt refcounts are handled by
13193 adjust_dynamic_symbol */
13194 gofarg.gotoff = gotoff;
13195 gofarg.info = info;
13196 elf_link_hash_traverse (elf_hash_table (info),
13197 elf_gc_allocate_got_offsets,
13198 &gofarg);
13199 return TRUE;
13200 }
13201
13202 /* Many folk need no more in the way of final link than this, once
13203 got entry reference counting is enabled. */
13204
13205 bfd_boolean
13206 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13207 {
13208 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13209 return FALSE;
13210
13211 /* Invoke the regular ELF backend linker to do all the work. */
13212 return bfd_elf_final_link (abfd, info);
13213 }
13214
13215 bfd_boolean
13216 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13217 {
13218 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
13219
13220 if (rcookie->bad_symtab)
13221 rcookie->rel = rcookie->rels;
13222
13223 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13224 {
13225 unsigned long r_symndx;
13226
13227 if (! rcookie->bad_symtab)
13228 if (rcookie->rel->r_offset > offset)
13229 return FALSE;
13230 if (rcookie->rel->r_offset != offset)
13231 continue;
13232
13233 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
13234 if (r_symndx == STN_UNDEF)
13235 return TRUE;
13236
13237 if (r_symndx >= rcookie->locsymcount
13238 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13239 {
13240 struct elf_link_hash_entry *h;
13241
13242 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13243
13244 while (h->root.type == bfd_link_hash_indirect
13245 || h->root.type == bfd_link_hash_warning)
13246 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13247
13248 if ((h->root.type == bfd_link_hash_defined
13249 || h->root.type == bfd_link_hash_defweak)
13250 && (h->root.u.def.section->owner != rcookie->abfd
13251 || h->root.u.def.section->kept_section != NULL
13252 || discarded_section (h->root.u.def.section)))
13253 return TRUE;
13254 }
13255 else
13256 {
13257 /* It's not a relocation against a global symbol,
13258 but it could be a relocation against a local
13259 symbol for a discarded section. */
13260 asection *isec;
13261 Elf_Internal_Sym *isym;
13262
13263 /* Need to: get the symbol; get the section. */
13264 isym = &rcookie->locsyms[r_symndx];
13265 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
13266 if (isec != NULL
13267 && (isec->kept_section != NULL
13268 || discarded_section (isec)))
13269 return TRUE;
13270 }
13271 return FALSE;
13272 }
13273 return FALSE;
13274 }
13275
13276 /* Discard unneeded references to discarded sections.
13277 Returns -1 on error, 1 if any section's size was changed, 0 if
13278 nothing changed. This function assumes that the relocations are in
13279 sorted order, which is true for all known assemblers. */
13280
13281 int
13282 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13283 {
13284 struct elf_reloc_cookie cookie;
13285 asection *o;
13286 bfd *abfd;
13287 int changed = 0;
13288
13289 if (info->traditional_format
13290 || !is_elf_hash_table (info->hash))
13291 return 0;
13292
13293 o = bfd_get_section_by_name (output_bfd, ".stab");
13294 if (o != NULL)
13295 {
13296 asection *i;
13297
13298 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13299 {
13300 if (i->size == 0
13301 || i->reloc_count == 0
13302 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13303 continue;
13304
13305 abfd = i->owner;
13306 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13307 continue;
13308
13309 if (!init_reloc_cookie_for_section (&cookie, info, i))
13310 return -1;
13311
13312 if (_bfd_discard_section_stabs (abfd, i,
13313 elf_section_data (i)->sec_info,
13314 bfd_elf_reloc_symbol_deleted_p,
13315 &cookie))
13316 changed = 1;
13317
13318 fini_reloc_cookie_for_section (&cookie, i);
13319 }
13320 }
13321
13322 o = NULL;
13323 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13324 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
13325 if (o != NULL)
13326 {
13327 asection *i;
13328
13329 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13330 {
13331 if (i->size == 0)
13332 continue;
13333
13334 abfd = i->owner;
13335 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13336 continue;
13337
13338 if (!init_reloc_cookie_for_section (&cookie, info, i))
13339 return -1;
13340
13341 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13342 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
13343 bfd_elf_reloc_symbol_deleted_p,
13344 &cookie))
13345 changed = 1;
13346
13347 fini_reloc_cookie_for_section (&cookie, i);
13348 }
13349 }
13350
13351 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13352 {
13353 const struct elf_backend_data *bed;
13354
13355 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13356 continue;
13357
13358 bed = get_elf_backend_data (abfd);
13359
13360 if (bed->elf_backend_discard_info != NULL)
13361 {
13362 if (!init_reloc_cookie (&cookie, info, abfd))
13363 return -1;
13364
13365 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
13366 changed = 1;
13367
13368 fini_reloc_cookie (&cookie, abfd);
13369 }
13370 }
13371
13372 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13373 _bfd_elf_end_eh_frame_parsing (info);
13374
13375 if (info->eh_frame_hdr_type
13376 && !bfd_link_relocatable (info)
13377 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
13378 changed = 1;
13379
13380 return changed;
13381 }
13382
13383 bfd_boolean
13384 _bfd_elf_section_already_linked (bfd *abfd,
13385 asection *sec,
13386 struct bfd_link_info *info)
13387 {
13388 flagword flags;
13389 const char *name, *key;
13390 struct bfd_section_already_linked *l;
13391 struct bfd_section_already_linked_hash_entry *already_linked_list;
13392
13393 if (sec->output_section == bfd_abs_section_ptr)
13394 return FALSE;
13395
13396 flags = sec->flags;
13397
13398 /* Return if it isn't a linkonce section. A comdat group section
13399 also has SEC_LINK_ONCE set. */
13400 if ((flags & SEC_LINK_ONCE) == 0)
13401 return FALSE;
13402
13403 /* Don't put group member sections on our list of already linked
13404 sections. They are handled as a group via their group section. */
13405 if (elf_sec_group (sec) != NULL)
13406 return FALSE;
13407
13408 /* For a SHT_GROUP section, use the group signature as the key. */
13409 name = sec->name;
13410 if ((flags & SEC_GROUP) != 0
13411 && elf_next_in_group (sec) != NULL
13412 && elf_group_name (elf_next_in_group (sec)) != NULL)
13413 key = elf_group_name (elf_next_in_group (sec));
13414 else
13415 {
13416 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
13417 if (CONST_STRNEQ (name, ".gnu.linkonce.")
13418 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13419 key++;
13420 else
13421 /* Must be a user linkonce section that doesn't follow gcc's
13422 naming convention. In this case we won't be matching
13423 single member groups. */
13424 key = name;
13425 }
13426
13427 already_linked_list = bfd_section_already_linked_table_lookup (key);
13428
13429 for (l = already_linked_list->entry; l != NULL; l = l->next)
13430 {
13431 /* We may have 2 different types of sections on the list: group
13432 sections with a signature of <key> (<key> is some string),
13433 and linkonce sections named .gnu.linkonce.<type>.<key>.
13434 Match like sections. LTO plugin sections are an exception.
13435 They are always named .gnu.linkonce.t.<key> and match either
13436 type of section. */
13437 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13438 && ((flags & SEC_GROUP) != 0
13439 || strcmp (name, l->sec->name) == 0))
13440 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
13441 {
13442 /* The section has already been linked. See if we should
13443 issue a warning. */
13444 if (!_bfd_handle_already_linked (sec, l, info))
13445 return FALSE;
13446
13447 if (flags & SEC_GROUP)
13448 {
13449 asection *first = elf_next_in_group (sec);
13450 asection *s = first;
13451
13452 while (s != NULL)
13453 {
13454 s->output_section = bfd_abs_section_ptr;
13455 /* Record which group discards it. */
13456 s->kept_section = l->sec;
13457 s = elf_next_in_group (s);
13458 /* These lists are circular. */
13459 if (s == first)
13460 break;
13461 }
13462 }
13463
13464 return TRUE;
13465 }
13466 }
13467
13468 /* A single member comdat group section may be discarded by a
13469 linkonce section and vice versa. */
13470 if ((flags & SEC_GROUP) != 0)
13471 {
13472 asection *first = elf_next_in_group (sec);
13473
13474 if (first != NULL && elf_next_in_group (first) == first)
13475 /* Check this single member group against linkonce sections. */
13476 for (l = already_linked_list->entry; l != NULL; l = l->next)
13477 if ((l->sec->flags & SEC_GROUP) == 0
13478 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13479 {
13480 first->output_section = bfd_abs_section_ptr;
13481 first->kept_section = l->sec;
13482 sec->output_section = bfd_abs_section_ptr;
13483 break;
13484 }
13485 }
13486 else
13487 /* Check this linkonce section against single member groups. */
13488 for (l = already_linked_list->entry; l != NULL; l = l->next)
13489 if (l->sec->flags & SEC_GROUP)
13490 {
13491 asection *first = elf_next_in_group (l->sec);
13492
13493 if (first != NULL
13494 && elf_next_in_group (first) == first
13495 && bfd_elf_match_symbols_in_sections (first, sec, info))
13496 {
13497 sec->output_section = bfd_abs_section_ptr;
13498 sec->kept_section = first;
13499 break;
13500 }
13501 }
13502
13503 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13504 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13505 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13506 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13507 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13508 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13509 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13510 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13511 The reverse order cannot happen as there is never a bfd with only the
13512 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13513 matter as here were are looking only for cross-bfd sections. */
13514
13515 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
13516 for (l = already_linked_list->entry; l != NULL; l = l->next)
13517 if ((l->sec->flags & SEC_GROUP) == 0
13518 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
13519 {
13520 if (abfd != l->sec->owner)
13521 sec->output_section = bfd_abs_section_ptr;
13522 break;
13523 }
13524
13525 /* This is the first section with this name. Record it. */
13526 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
13527 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
13528 return sec->output_section == bfd_abs_section_ptr;
13529 }
13530
13531 bfd_boolean
13532 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
13533 {
13534 return sym->st_shndx == SHN_COMMON;
13535 }
13536
13537 unsigned int
13538 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
13539 {
13540 return SHN_COMMON;
13541 }
13542
13543 asection *
13544 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
13545 {
13546 return bfd_com_section_ptr;
13547 }
13548
13549 bfd_vma
13550 _bfd_elf_default_got_elt_size (bfd *abfd,
13551 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13552 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
13553 bfd *ibfd ATTRIBUTE_UNUSED,
13554 unsigned long symndx ATTRIBUTE_UNUSED)
13555 {
13556 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13557 return bed->s->arch_size / 8;
13558 }
13559
13560 /* Routines to support the creation of dynamic relocs. */
13561
13562 /* Returns the name of the dynamic reloc section associated with SEC. */
13563
13564 static const char *
13565 get_dynamic_reloc_section_name (bfd * abfd,
13566 asection * sec,
13567 bfd_boolean is_rela)
13568 {
13569 char *name;
13570 const char *old_name = bfd_get_section_name (NULL, sec);
13571 const char *prefix = is_rela ? ".rela" : ".rel";
13572
13573 if (old_name == NULL)
13574 return NULL;
13575
13576 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
13577 sprintf (name, "%s%s", prefix, old_name);
13578
13579 return name;
13580 }
13581
13582 /* Returns the dynamic reloc section associated with SEC.
13583 If necessary compute the name of the dynamic reloc section based
13584 on SEC's name (looked up in ABFD's string table) and the setting
13585 of IS_RELA. */
13586
13587 asection *
13588 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
13589 asection * sec,
13590 bfd_boolean is_rela)
13591 {
13592 asection * reloc_sec = elf_section_data (sec)->sreloc;
13593
13594 if (reloc_sec == NULL)
13595 {
13596 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13597
13598 if (name != NULL)
13599 {
13600 reloc_sec = bfd_get_linker_section (abfd, name);
13601
13602 if (reloc_sec != NULL)
13603 elf_section_data (sec)->sreloc = reloc_sec;
13604 }
13605 }
13606
13607 return reloc_sec;
13608 }
13609
13610 /* Returns the dynamic reloc section associated with SEC. If the
13611 section does not exist it is created and attached to the DYNOBJ
13612 bfd and stored in the SRELOC field of SEC's elf_section_data
13613 structure.
13614
13615 ALIGNMENT is the alignment for the newly created section and
13616 IS_RELA defines whether the name should be .rela.<SEC's name>
13617 or .rel.<SEC's name>. The section name is looked up in the
13618 string table associated with ABFD. */
13619
13620 asection *
13621 _bfd_elf_make_dynamic_reloc_section (asection *sec,
13622 bfd *dynobj,
13623 unsigned int alignment,
13624 bfd *abfd,
13625 bfd_boolean is_rela)
13626 {
13627 asection * reloc_sec = elf_section_data (sec)->sreloc;
13628
13629 if (reloc_sec == NULL)
13630 {
13631 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13632
13633 if (name == NULL)
13634 return NULL;
13635
13636 reloc_sec = bfd_get_linker_section (dynobj, name);
13637
13638 if (reloc_sec == NULL)
13639 {
13640 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
13641 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
13642 if ((sec->flags & SEC_ALLOC) != 0)
13643 flags |= SEC_ALLOC | SEC_LOAD;
13644
13645 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
13646 if (reloc_sec != NULL)
13647 {
13648 /* _bfd_elf_get_sec_type_attr chooses a section type by
13649 name. Override as it may be wrong, eg. for a user
13650 section named "auto" we'll get ".relauto" which is
13651 seen to be a .rela section. */
13652 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
13653 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
13654 reloc_sec = NULL;
13655 }
13656 }
13657
13658 elf_section_data (sec)->sreloc = reloc_sec;
13659 }
13660
13661 return reloc_sec;
13662 }
13663
13664 /* Copy the ELF symbol type and other attributes for a linker script
13665 assignment from HSRC to HDEST. Generally this should be treated as
13666 if we found a strong non-dynamic definition for HDEST (except that
13667 ld ignores multiple definition errors). */
13668 void
13669 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
13670 struct bfd_link_hash_entry *hdest,
13671 struct bfd_link_hash_entry *hsrc)
13672 {
13673 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
13674 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
13675 Elf_Internal_Sym isym;
13676
13677 ehdest->type = ehsrc->type;
13678 ehdest->target_internal = ehsrc->target_internal;
13679
13680 isym.st_other = ehsrc->other;
13681 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
13682 }
13683
13684 /* Append a RELA relocation REL to section S in BFD. */
13685
13686 void
13687 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13688 {
13689 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13690 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
13691 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
13692 bed->s->swap_reloca_out (abfd, rel, loc);
13693 }
13694
13695 /* Append a REL relocation REL to section S in BFD. */
13696
13697 void
13698 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13699 {
13700 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13701 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
13702 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
13703 bed->s->swap_reloc_out (abfd, rel, loc);
13704 }
This page took 0.378928 seconds and 5 git commands to generate.